39 |
* MinChen <chenm001@163.com> |
* MinChen <chenm001@163.com> |
40 |
* 14.04.2002 added FrameCodeB() |
* 14.04.2002 added FrameCodeB() |
41 |
* |
* |
42 |
* $Id: encoder.c,v 1.76.2.10 2002-10-03 08:24:27 syskin Exp $ |
* $Id: encoder.c,v 1.76.2.41 2003-01-25 22:44:50 chl Exp $ |
43 |
* |
* |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
|
|
|
46 |
#include <stdlib.h> |
#include <stdlib.h> |
47 |
#include <stdio.h> |
#include <stdio.h> |
48 |
#include <math.h> |
#include <math.h> |
53 |
#include "global.h" |
#include "global.h" |
54 |
#include "utils/timer.h" |
#include "utils/timer.h" |
55 |
#include "image/image.h" |
#include "image/image.h" |
|
#ifdef BFRAMES |
|
56 |
#include "image/font.h" |
#include "image/font.h" |
57 |
#include "motion/sad.h" |
#include "motion/sad.h" |
|
#endif |
|
58 |
#include "motion/motion.h" |
#include "motion/motion.h" |
59 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
60 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
72 |
****************************************************************************/ |
****************************************************************************/ |
73 |
|
|
74 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
75 |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
#define SWAP(_T_,A,B) { _T_ tmp = A; A = B; B = tmp; } |
76 |
|
|
77 |
/***************************************************************************** |
/***************************************************************************** |
78 |
* Local function prototypes |
* Local function prototypes |
106 |
}; |
}; |
107 |
|
|
108 |
|
|
|
static void __inline |
|
|
image_null(IMAGE * image) |
|
|
{ |
|
|
image->y = image->u = image->v = NULL; |
|
|
} |
|
|
|
|
|
|
|
109 |
/***************************************************************************** |
/***************************************************************************** |
110 |
* Encoder creation |
* Encoder creation |
111 |
* |
* |
223 |
|
|
224 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
225 |
|
|
226 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
227 |
|
|
228 |
/* Fill rate control parameters */ |
/* Fill rate control parameters */ |
229 |
|
|
230 |
pEnc->bitrate = pParam->rc_bitrate; |
pEnc->bitrate = pParam->rc_bitrate; |
231 |
|
|
232 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = -1; |
233 |
pEnc->iMaxKeyInterval = pParam->max_key_interval; |
pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval; |
234 |
|
|
235 |
/* try to allocate frame memory */ |
/* try to allocate frame memory */ |
236 |
|
|
254 |
|
|
255 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
256 |
|
|
257 |
#ifdef _DEBUG_PSNR |
if (pParam->global & XVID_GLOBAL_EXTRASTATS) |
258 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
|
#endif |
|
259 |
|
|
260 |
image_null(&pEnc->f_refh); |
image_null(&pEnc->f_refh); |
261 |
image_null(&pEnc->f_refv); |
image_null(&pEnc->f_refv); |
269 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
270 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
271 |
|
|
272 |
#ifdef _DEBUG_PSNR |
if (pParam->global & XVID_GLOBAL_EXTRASTATS) |
273 |
if (image_create |
{ if (image_create |
274 |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
275 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
276 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
277 |
#endif |
} |
278 |
|
|
279 |
if (image_create |
if (image_create |
280 |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
318 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
319 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
320 |
|
|
321 |
|
/* Create full bitplane for GMC, this might be wasteful */ |
322 |
|
if (image_create |
323 |
|
(&pEnc->vGMC, pEnc->mbParam.edged_width, |
324 |
|
pEnc->mbParam.edged_height) < 0) |
325 |
|
goto xvid_err_memory3; |
326 |
|
|
327 |
|
|
328 |
|
|
329 |
/* B Frames specific init */ |
pEnc->mbParam.global = pParam->global; |
330 |
|
|
331 |
pEnc->global = pParam->global; |
/* B Frames specific init */ |
332 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
333 |
pEnc->bquant_ratio = pParam->bquant_ratio; |
pEnc->mbParam.bquant_ratio = pParam->bquant_ratio; |
334 |
pEnc->frame_drop_ratio = pParam->frame_drop_ratio; |
pEnc->mbParam.bquant_offset = pParam->bquant_offset; |
335 |
|
pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio; |
336 |
pEnc->bframes = NULL; |
pEnc->bframes = NULL; |
337 |
|
|
338 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
464 |
} |
} |
465 |
|
|
466 |
xvid_err_memory3: |
xvid_err_memory3: |
467 |
#ifdef _DEBUG_PSNR |
|
468 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS) |
469 |
|
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
470 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
471 |
#endif |
} |
472 |
|
|
473 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
474 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
492 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
493 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
494 |
|
|
495 |
|
/* destroy GMC image */ |
496 |
|
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
497 |
|
pEnc->mbParam.edged_height); |
498 |
|
|
499 |
|
|
500 |
xvid_err_memory2: |
xvid_err_memory2: |
501 |
xvid_free(pEnc->current->mbs); |
xvid_free(pEnc->current->mbs); |
502 |
xvid_free(pEnc->reference->mbs); |
xvid_free(pEnc->reference->mbs); |
584 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
585 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
586 |
|
|
587 |
#ifdef _DEBUG_PSNR |
if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS) |
588 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
589 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
590 |
#endif |
} |
591 |
|
|
592 |
/* Encoder structure */ |
/* Encoder structure */ |
593 |
|
|
627 |
start_timer(); |
start_timer(); |
628 |
if (image_input |
if (image_input |
629 |
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
630 |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING)) |
631 |
return; |
return; |
632 |
stop_conv_timer(); |
stop_conv_timer(); |
633 |
|
|
656 |
|
|
657 |
|
|
658 |
|
|
659 |
|
/* convert pFrame->intra to coding_type */ |
660 |
|
static int intra2coding_type(int intra) |
661 |
|
{ |
662 |
|
if (intra < 0) return -1; |
663 |
|
if (intra == 1) return I_VOP; |
664 |
|
if (intra == 2) return B_VOP; |
665 |
|
|
666 |
|
return P_VOP; |
667 |
|
} |
668 |
|
|
669 |
|
|
670 |
|
|
671 |
/***************************************************************************** |
/***************************************************************************** |
684 |
{ |
{ |
685 |
uint16_t x, y; |
uint16_t x, y; |
686 |
Bitstream bs; |
Bitstream bs; |
687 |
uint32_t bits, mode; |
uint32_t bits; |
688 |
|
int mode; |
689 |
|
|
690 |
int input_valid = 1; |
int input_valid = 1; |
691 |
|
int bframes_count = 0; |
692 |
|
|
|
#ifdef _DEBUG_PSNR |
|
693 |
float psnr; |
float psnr; |
694 |
char temp[128]; |
char temp[128]; |
|
#endif |
|
695 |
|
|
696 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
697 |
ENC_CHECK(pFrame); |
ENC_CHECK(pFrame); |
722 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
723 |
|
|
724 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
725 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
726 |
|
|
727 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
728 |
|
|
729 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
730 |
|
bframes_count = 0; |
731 |
|
|
732 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
733 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
734 |
pFrame->intra = 0; |
pFrame->intra = 0; |
735 |
|
|
736 |
|
|
737 |
|
emms(); |
738 |
|
|
739 |
|
if (pResult) { |
740 |
|
pResult->quant = pEnc->current->quant; |
741 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
742 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
743 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
744 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
745 |
|
} |
746 |
|
|
747 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
748 |
} |
} |
749 |
|
|
755 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
756 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
757 |
|
|
758 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
759 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
760 |
pFrame->intra = 0; |
pFrame->intra = 2; |
761 |
|
|
762 |
|
if (pResult) { |
763 |
|
pResult->quant = pEnc->current->quant; |
764 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
765 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
766 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
767 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
768 |
|
} |
769 |
|
|
770 |
if (input_valid) |
if (input_valid) |
771 |
queue_image(pEnc, pFrame); |
queue_image(pEnc, pFrame); |
772 |
|
|
773 |
|
emms(); |
774 |
|
|
775 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
776 |
} |
} |
777 |
|
|
785 |
indentical to the future-referece frame. |
indentical to the future-referece frame. |
786 |
*/ |
*/ |
787 |
|
|
788 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) { |
789 |
int tmp; |
int tmp; |
790 |
|
|
791 |
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
792 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
793 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
794 |
|
|
|
BitstreamPad(&bs); |
|
795 |
|
|
796 |
tmp = pEnc->current->seconds; |
tmp = pEnc->current->seconds; |
797 |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
798 |
|
|
799 |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
800 |
pEnc->current->seconds = tmp; |
pEnc->current->seconds = tmp; |
801 |
|
|
802 |
|
BitstreamPadAlways(&bs); |
803 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
804 |
pFrame->intra = 0; |
pFrame->intra = 4; |
805 |
|
|
806 |
|
if (pResult) { |
807 |
|
pResult->quant = pEnc->current->quant; |
808 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
809 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
810 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
811 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
812 |
|
} |
813 |
|
|
814 |
if (input_valid) |
if (input_valid) |
815 |
queue_image(pEnc, pFrame); |
queue_image(pEnc, pFrame); |
816 |
|
|
817 |
|
emms(); |
818 |
|
|
819 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
820 |
} |
} |
821 |
} |
} |
826 |
if (pEnc->bframenum_dx50bvop != -1) |
if (pEnc->bframenum_dx50bvop != -1) |
827 |
{ |
{ |
828 |
|
|
829 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
830 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
831 |
|
|
832 |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
833 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
834 |
} |
} |
835 |
|
|
841 |
|
|
842 |
} else if (input_valid) { |
} else if (input_valid) { |
843 |
|
|
844 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
845 |
|
|
846 |
start_timer(); |
start_timer(); |
847 |
if (image_input |
if (image_input |
848 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
849 |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING)) |
850 |
|
{ |
851 |
|
emms(); |
852 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
853 |
|
} |
854 |
stop_conv_timer(); |
stop_conv_timer(); |
855 |
|
|
856 |
// queue input frame, and dequue next image |
// queue input frame, and dequue next image |
867 |
|
|
868 |
} else if (pEnc->queue_size > 0) { |
} else if (pEnc->queue_size > 0) { |
869 |
|
|
870 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
871 |
|
|
872 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
873 |
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
884 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
885 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
886 |
|
|
887 |
BitstreamPutBits(&bs, 0x7f, 8); |
// BitstreamPutBits(&bs, 0x7f, 8); |
888 |
pFrame->intra = 0; |
pFrame->intra = 5; |
889 |
|
|
890 |
|
if (pResult) { |
891 |
|
/* |
892 |
|
* We must decide what to put there because i know some apps |
893 |
|
* are storing statistics about quantizers and just do |
894 |
|
* stats[quant]++ or stats[quant-1]++ |
895 |
|
* transcode is one of these app with its 2pass module |
896 |
|
*/ |
897 |
|
|
898 |
|
/* |
899 |
|
* For now i prefer 31 than 0 that could lead to a segfault |
900 |
|
* in transcode |
901 |
|
*/ |
902 |
|
pResult->quant = 31; |
903 |
|
|
904 |
|
pResult->hlength = 0; |
905 |
|
pResult->kblks = 0; |
906 |
|
pResult->mblks = 0; |
907 |
|
pResult->ublks = 0; |
908 |
|
} |
909 |
|
|
910 |
|
} else { |
911 |
|
|
912 |
|
if (pResult) { |
913 |
|
pResult->quant = pEnc->current->quant; |
914 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
915 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
916 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
917 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
918 |
|
} |
919 |
|
|
920 |
} |
} |
921 |
|
|
922 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
923 |
|
|
924 |
|
emms(); |
925 |
|
|
926 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
927 |
} |
} |
928 |
|
|
933 |
// only inc frame num, adapt quant, etc. if we havent seen it before |
// only inc frame num, adapt quant, etc. if we havent seen it before |
934 |
if (pEnc->bframenum_dx50bvop < 0 ) |
if (pEnc->bframenum_dx50bvop < 0 ) |
935 |
{ |
{ |
936 |
|
mode = intra2coding_type(pFrame->intra); |
937 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
938 |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
939 |
else |
else |
954 |
|
|
955 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
956 |
|
|
957 |
#ifdef _DEBUG_PSNR |
if (pFrame->general & XVID_EXTRASTATS) |
958 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
{ image_copy(&pEnc->sOriginal, &pEnc->current->image, |
959 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
960 |
#endif |
} |
961 |
|
|
962 |
emms(); |
emms(); |
963 |
|
|
964 |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
965 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
966 |
"%i if:%i st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks); |
"%i if:%i st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp); |
967 |
} |
} |
968 |
|
|
969 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1005 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1006 |
* ivop/pvop/bvop selection |
* ivop/pvop/bvop selection |
1007 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1008 |
|
pEnc->iFrameNum++; |
1009 |
|
|
1010 |
|
if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 || |
1011 |
|
(mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 && |
1012 |
|
pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) |
1013 |
|
{ |
1014 |
|
mode = I_VOP; |
1015 |
|
}else{ |
1016 |
|
mode = MEanalysis(&pEnc->reference->image, pEnc->current, |
1017 |
|
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1018 |
|
(mode < 0) ? pEnc->iFrameNum : 0, |
1019 |
|
bframes_count++); |
1020 |
|
} |
1021 |
|
|
1022 |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
if (mode == I_VOP) { |
|
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
|
|
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
|
|
|| /*image_mad(&pEnc->reference->image, &pEnc->current->image, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
|
|
pEnc->mbParam.height) > 30) {*/ |
|
|
2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image, |
|
|
&pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) { |
|
|
|
|
1023 |
/* |
/* |
1024 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
1025 |
*/ |
*/ |
1026 |
|
if ((pEnc->current->global_flags & XVID_QUARTERPEL)) |
1027 |
|
pEnc->mbParam.m_quarterpel = 1; |
1028 |
|
else |
1029 |
|
pEnc->mbParam.m_quarterpel = 0; |
1030 |
|
|
1031 |
|
if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
1032 |
|
|
1033 |
|
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
1034 |
|
if (pFrame->quant_intra_matrix != NULL) |
1035 |
|
set_intra_matrix(pFrame->quant_intra_matrix); |
1036 |
|
if (pFrame->quant_inter_matrix != NULL) |
1037 |
|
set_inter_matrix(pFrame->quant_inter_matrix); |
1038 |
|
} |
1039 |
|
|
1040 |
|
|
1041 |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
1042 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1043 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1044 |
|
|
1045 |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
1046 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
1047 |
} |
} |
1048 |
|
|
1049 |
// when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe |
// when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe |
1050 |
|
|
1051 |
if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
1052 |
|
|
1053 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
1054 |
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
1055 |
|
|
1056 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
1057 |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
1058 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
1059 |
} |
} |
1060 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
1061 |
|
bframes_count = 0; |
1062 |
pFrame->intra = 0; |
pFrame->intra = 0; |
1063 |
|
|
1064 |
} else { |
} else { |
1065 |
|
|
1066 |
FrameCodeI(pEnc, &bs, &bits); |
FrameCodeI(pEnc, &bs, &bits); |
1067 |
|
bframes_count = 0; |
1068 |
pFrame->intra = 1; |
pFrame->intra = 1; |
1069 |
|
|
1070 |
pEnc->bframenum_dx50bvop = -1; |
pEnc->bframenum_dx50bvop = -1; |
1072 |
|
|
1073 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1074 |
|
|
1075 |
if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
1076 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1077 |
input_valid = 0; |
input_valid = 0; |
1078 |
goto ipvop_loop; |
goto ipvop_loop; |
1079 |
} |
} |
1082 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
1083 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
1084 |
*/ |
*/ |
1085 |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
} else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1086 |
/* |
/* |
1087 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
1088 |
*/ |
*/ |
1091 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1092 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1093 |
|
|
1094 |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
1095 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
1096 |
} |
} |
1097 |
|
|
1098 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
1099 |
|
bframes_count = 0; |
1100 |
pFrame->intra = 0; |
pFrame->intra = 0; |
1101 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1102 |
|
|
1103 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) { |
1104 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1105 |
input_valid = 0; |
input_valid = 0; |
1106 |
goto ipvop_loop; |
goto ipvop_loop; |
1107 |
} |
} |
1108 |
|
|
1109 |
} else { |
} else { /* mode == B_VOP */ |
1110 |
/* |
/* |
1111 |
* This will be coded as a Bidirectional Frame |
* This will be coded as a Bidirectional Frame |
1112 |
*/ |
*/ |
1113 |
|
|
1114 |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
1115 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1116 |
} |
} |
1117 |
|
|
1118 |
if (pFrame->bquant < 1) { |
if (pFrame->bquant < 1) { |
1119 |
pEnc->current->quant = |
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
1120 |
((pEnc->reference->quant + |
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
1121 |
pEnc->current->quant) * pEnc->bquant_ratio) / 200; |
|
1122 |
} else { |
} else { |
1123 |
pEnc->current->quant = pFrame->bquant; |
pEnc->current->quant = pFrame->bquant; |
1124 |
} |
} |
1125 |
|
|
1126 |
if (pEnc->current->quant < 1) |
if (pEnc->current->quant < 1) |
1127 |
pEnc->current->quant = 1; |
pEnc->current->quant = 1; |
1128 |
|
else if (pEnc->current->quant > 31) |
|
if (pEnc->current->quant > 31) |
|
1129 |
pEnc->current->quant = 31; |
pEnc->current->quant = 31; |
1130 |
|
|
|
|
|
1131 |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
1132 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1133 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1134 |
|
|
|
|
|
|
|
|
1135 |
/* store frame into bframe buffer & swap ref back to current */ |
/* store frame into bframe buffer & swap ref back to current */ |
1136 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1137 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
1138 |
|
|
1139 |
pEnc->bframenum_tail++; |
pEnc->bframenum_tail++; |
1140 |
|
|
1141 |
pFrame->intra = 0; |
// bframe report by koepi |
1142 |
|
pFrame->intra = 2; |
1143 |
pFrame->length = 0; |
pFrame->length = 0; |
1144 |
|
|
1145 |
input_valid = 0; |
input_valid = 0; |
1146 |
goto bvop_loop; |
goto bvop_loop; |
1147 |
} |
} |
1148 |
|
|
1149 |
pEnc->iFrameNum++; |
BitstreamPadAlways(&bs); |
|
|
|
|
BitstreamPad(&bs); |
|
1150 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1151 |
|
|
1152 |
if (pResult) { |
if (pResult) { |
1153 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
1154 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
1155 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->current->sStat.kblks; |
1156 |
pResult->mblks = pEnc->sStat.mblks; |
pResult->mblks = pEnc->current->sStat.mblks; |
1157 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->current->sStat.ublks; |
1158 |
} |
|
1159 |
|
if (pFrame->general & XVID_EXTRASTATS) |
1160 |
emms(); |
{ pResult->sse_y = |
1161 |
|
plane_sse( pEnc->sOriginal.y, pEnc->current->image.y, |
|
#ifdef _DEBUG_PSNR |
|
|
psnr = |
|
|
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
|
1162 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1163 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1164 |
|
|
1165 |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
pResult->sse_u = |
1166 |
DEBUG(temp); |
plane_sse( pEnc->sOriginal.u, pEnc->current->image.u, |
1167 |
#endif |
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
1168 |
|
pEnc->mbParam.height/2); |
1169 |
|
|
1170 |
|
pResult->sse_v = |
1171 |
|
plane_sse( pEnc->sOriginal.v, pEnc->current->image.v, |
1172 |
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
1173 |
|
pEnc->mbParam.height/2); |
1174 |
|
} |
1175 |
|
} |
1176 |
|
|
1177 |
|
emms(); |
1178 |
|
|
1179 |
if (pFrame->quant == 0) { |
if (pFrame->quant == 0) { |
1180 |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1184 |
stop_global_timer(); |
stop_global_timer(); |
1185 |
write_timer(); |
write_timer(); |
1186 |
|
|
1187 |
|
emms(); |
1188 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1189 |
} |
} |
1190 |
|
|
1209 |
uint32_t bits; |
uint32_t bits; |
1210 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
1211 |
|
|
|
#ifdef _DEBUG_PSNR |
|
1212 |
float psnr; |
float psnr; |
1213 |
uint8_t temp[128]; |
uint8_t temp[128]; |
|
#endif |
|
1214 |
|
|
1215 |
start_global_timer(); |
start_global_timer(); |
1216 |
|
|
1219 |
ENC_CHECK(pFrame->bitstream); |
ENC_CHECK(pFrame->bitstream); |
1220 |
ENC_CHECK(pFrame->image); |
ENC_CHECK(pFrame->image); |
1221 |
|
|
1222 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
1223 |
|
|
1224 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1225 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
1237 |
start_timer(); |
start_timer(); |
1238 |
if (image_input |
if (image_input |
1239 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
1240 |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0) |
pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0) |
1241 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
1242 |
stop_conv_timer(); |
stop_conv_timer(); |
1243 |
|
|
1244 |
#ifdef _DEBUG_PSNR |
if (pFrame->general & XVID_EXTRASTATS) |
1245 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
{ image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1246 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1247 |
#endif |
} |
1248 |
|
|
1249 |
emms(); |
emms(); |
1250 |
|
|
1256 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
1257 |
} |
} |
1258 |
|
|
1259 |
|
if ((pEnc->current->global_flags & XVID_QUARTERPEL)) |
1260 |
|
pEnc->mbParam.m_quarterpel = 1; |
1261 |
|
else |
1262 |
|
pEnc->mbParam.m_quarterpel = 0; |
1263 |
|
|
1264 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1265 |
int *temp_dquants = |
int *temp_dquants = |
1266 |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
1322 |
|
|
1323 |
if (pFrame->intra < 0) { |
if (pFrame->intra < 0) { |
1324 |
if ((pEnc->iFrameNum == 0) |
if ((pEnc->iFrameNum == 0) |
1325 |
|| ((pEnc->iMaxKeyInterval > 0) |
|| ((pEnc->mbParam.iMaxKeyInterval > 0) |
1326 |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) { |
&& (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) { |
1327 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
1328 |
} else { |
} else { |
1329 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
1337 |
|
|
1338 |
} |
} |
1339 |
|
|
1340 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
// BitstreamPutBits(&bs, 0xFFFF, 16); |
1341 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
// BitstreamPutBits(&bs, 0xFFFF, 16); |
1342 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1343 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1344 |
|
|
1345 |
if (pResult) { |
if (pResult) { |
1346 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
1347 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
1348 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->current->sStat.kblks; |
1349 |
pResult->mblks = pEnc->sStat.mblks; |
pResult->mblks = pEnc->current->sStat.mblks; |
1350 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->current->sStat.ublks; |
1351 |
} |
} |
1352 |
|
|
1353 |
emms(); |
emms(); |
1356 |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1357 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1358 |
} |
} |
1359 |
#ifdef _DEBUG_PSNR |
if (pFrame->general & XVID_EXTRASTATS) |
1360 |
|
{ |
1361 |
psnr = |
psnr = |
1362 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1363 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1364 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1365 |
|
|
1366 |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
1367 |
DEBUG(temp); |
} |
|
#endif |
|
1368 |
|
|
1369 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1370 |
|
|
1530 |
|
|
1531 |
if (intra) { |
if (intra) { |
1532 |
if (!hint->rawhints) { |
if (!hint->rawhints) { |
1533 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1534 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1535 |
} |
} |
1536 |
return; |
return; |
1605 |
Bitstream * bs, |
Bitstream * bs, |
1606 |
uint32_t * pBits) |
uint32_t * pBits) |
1607 |
{ |
{ |
1608 |
|
int mb_width = pEnc->mbParam.mb_width; |
1609 |
|
int mb_height = pEnc->mbParam.mb_height; |
1610 |
|
|
1611 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1612 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1613 |
|
|
1614 |
uint16_t x, y; |
uint16_t x, y; |
1615 |
|
|
1616 |
|
if ((pEnc->current->global_flags & XVID_REDUCED)) |
1617 |
|
{ |
1618 |
|
mb_width = (pEnc->mbParam.width + 31) / 32; |
1619 |
|
mb_height = (pEnc->mbParam.height + 31) / 32; |
1620 |
|
|
1621 |
|
/* 16x16->8x8 downsample requires 1 additional edge pixel*/ |
1622 |
|
/* XXX: setedges is overkill */ |
1623 |
|
start_timer(); |
1624 |
|
image_setedges(&pEnc->current->image, |
1625 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1626 |
|
pEnc->mbParam.width, pEnc->mbParam.height); |
1627 |
|
stop_edges_timer(); |
1628 |
|
} |
1629 |
|
|
1630 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
1631 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1632 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1633 |
|
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1634 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1635 |
|
|
1636 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1637 |
|
|
|
#define DIVX501B481P "DivX501b481p" |
|
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
|
|
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
|
|
} |
|
|
|
|
1638 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1639 |
|
|
1640 |
|
BitstreamPadAlways(bs); |
1641 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1642 |
|
|
1643 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1644 |
|
|
1645 |
pEnc->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = 0; |
1646 |
pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
pEnc->current->sStat.kblks = mb_width * mb_height; |
1647 |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
1648 |
|
|
1649 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < mb_height; y++) |
1650 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1651 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1652 |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1653 |
|
|
1666 |
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1667 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1668 |
} |
} |
1669 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1670 |
stop_coding_timer(); |
stop_coding_timer(); |
1671 |
} |
} |
1672 |
|
|
1673 |
|
if ((pEnc->current->global_flags & XVID_REDUCED)) |
1674 |
|
{ |
1675 |
|
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
1676 |
|
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
1677 |
|
16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); |
1678 |
|
} |
1679 |
emms(); |
emms(); |
1680 |
|
|
1681 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1682 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
|
pEnc->sStat.iMvSum = 0; |
|
|
pEnc->sStat.iMvCount = 0; |
|
1683 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1684 |
|
|
1685 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1693 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1694 |
#define BFRAME_SKIP_THRESHHOLD 30 |
#define BFRAME_SKIP_THRESHHOLD 30 |
1695 |
|
|
1696 |
|
|
1697 |
|
/* FrameCodeP also handles S(GMC)-VOPs */ |
1698 |
static int |
static int |
1699 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1700 |
Bitstream * bs, |
Bitstream * bs, |
1707 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1708 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1709 |
|
|
1710 |
|
int mb_width = pEnc->mbParam.mb_width; |
1711 |
|
int mb_height = pEnc->mbParam.mb_height; |
1712 |
|
|
1713 |
int iLimit; |
int iLimit; |
1714 |
int x, y, k; |
int x, y, k; |
1715 |
int iSearchRange; |
int iSearchRange; |
1716 |
int bIntra; |
int bIntra, skip_possible; |
1717 |
|
|
1718 |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
1719 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
1720 |
|
|
1721 |
|
if ((pEnc->current->global_flags & XVID_REDUCED)) |
1722 |
|
{ |
1723 |
|
mb_width = (pEnc->mbParam.width + 31) / 32; |
1724 |
|
mb_height = (pEnc->mbParam.height + 31) / 32; |
1725 |
|
} |
1726 |
|
|
1727 |
|
|
1728 |
start_timer(); |
start_timer(); |
1729 |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1730 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pEnc->mbParam.width, pEnc->mbParam.height); |
1732 |
|
|
1733 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1734 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1735 |
|
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1736 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1737 |
|
|
1738 |
if (!force_inter) |
if (!force_inter) |
1739 |
iLimit = |
iLimit = (int)(mb_width * mb_height * INTRA_THRESHOLD); |
|
(int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * |
|
|
INTRA_THRESHOLD); |
|
1740 |
else |
else |
1741 |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
iLimit = mb_width * mb_height + 1; |
1742 |
|
|
1743 |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
1744 |
start_timer(); |
start_timer(); |
1745 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1746 |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
1747 |
pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_height, |
1748 |
|
pEnc->mbParam.m_quarterpel, |
1749 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1750 |
stop_inter_timer(); |
stop_inter_timer(); |
1751 |
} |
} |
1752 |
|
|
1753 |
|
pEnc->current->coding_type = P_VOP; |
1754 |
|
|
1755 |
start_timer(); |
start_timer(); |
1756 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
1757 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1758 |
if (bIntra == 0) { |
else |
|
pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current); |
|
|
MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference, |
|
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
1759 |
bIntra = |
bIntra = |
1760 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1761 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1762 |
iLimit); |
iLimit); |
1763 |
|
|
|
} |
|
1764 |
stop_motion_timer(); |
stop_motion_timer(); |
1765 |
|
|
1766 |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
1767 |
|
|
1768 |
pEnc->current->coding_type = P_VOP; |
if ( ( pEnc->current->global_flags & XVID_GMC ) |
1769 |
|
&& ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) |
1770 |
|
{ |
1771 |
|
pEnc->current->coding_type = S_VOP; |
1772 |
|
|
1773 |
if (vol_header) |
generate_GMCparameters( 2, 16, &pEnc->current->warp, |
1774 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
pEnc->mbParam.width, pEnc->mbParam.height, |
1775 |
|
&pEnc->current->gmc_data); |
1776 |
|
|
1777 |
|
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
1778 |
|
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
1779 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
1780 |
|
pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0, |
1781 |
|
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
1782 |
|
|
1783 |
|
} |
1784 |
|
|
1785 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1786 |
|
if (vol_header) |
1787 |
|
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1788 |
|
BitstreamPadAlways(bs); |
1789 |
|
} |
1790 |
|
|
1791 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1792 |
|
|
1793 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1794 |
|
|
1795 |
pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount = |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
1796 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
1797 |
|
|
1798 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
|
1799 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (y = 0; y < mb_height; y++) { |
1800 |
|
for (x = 0; x < mb_width; x++) { |
1801 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1802 |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1803 |
|
|
1804 |
|
/* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */ |
1805 |
|
/* For a start, leave INTRA decision as is, only choose only between INTER/GMC - gruel, 9.1.2002 */ |
1806 |
|
|
1807 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1808 |
|
|
1809 |
if (!bIntra) { |
if (bIntra) { |
1810 |
|
CodeIntraMB(pEnc, pMB); |
1811 |
|
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1812 |
|
dct_codes, qcoeff); |
1813 |
|
|
1814 |
|
start_timer(); |
1815 |
|
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1816 |
|
stop_prediction_timer(); |
1817 |
|
|
1818 |
|
pEnc->current->sStat.kblks++; |
1819 |
|
|
1820 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1821 |
|
stop_coding_timer(); |
1822 |
|
continue; |
1823 |
|
} |
1824 |
|
|
1825 |
|
if (pEnc->current->coding_type == S_VOP) { |
1826 |
|
|
1827 |
|
int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1828 |
|
pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1829 |
|
pEnc->mbParam.edged_width, 65536); |
1830 |
|
|
1831 |
|
if (pEnc->current->motion_flags & PMV_CHROMA16) { |
1832 |
|
iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
1833 |
|
pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
1834 |
|
|
1835 |
|
iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
1836 |
|
pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
1837 |
|
} |
1838 |
|
|
1839 |
|
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
1840 |
|
|
1841 |
|
if (pEnc->mbParam.m_quarterpel) |
1842 |
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
1843 |
|
else |
1844 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
1845 |
|
|
1846 |
|
pMB->mode = MODE_INTER; |
1847 |
|
pMB->mcsel = 1; |
1848 |
|
pMB->sad16 = iSAD; |
1849 |
|
} else { |
1850 |
|
pMB->mcsel = 0; |
1851 |
|
} |
1852 |
|
} else { |
1853 |
|
pMB->mcsel = 0; /* just a precaution */ |
1854 |
|
} |
1855 |
|
|
1856 |
start_timer(); |
start_timer(); |
1857 |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1858 |
&pEnc->vInterH, &pEnc->vInterV, |
&pEnc->vInterH, &pEnc->vInterV, |
1859 |
&pEnc->vInterHV, &pEnc->current->image, |
&pEnc->vInterHV, &pEnc->vGMC, |
1860 |
|
&pEnc->current->image, |
1861 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pEnc->mbParam.width, |
1862 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1863 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1864 |
|
pEnc->mbParam.m_quarterpel, |
1865 |
|
(pEnc->current->global_flags & XVID_REDUCED), |
1866 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1867 |
|
|
1868 |
stop_comp_timer(); |
stop_comp_timer(); |
1869 |
|
|
1870 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1882 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1883 |
|
|
1884 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->mode != MODE_NOT_CODED) |
1885 |
pMB->cbp = |
{ pMB->cbp = |
1886 |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1887 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
|
} else { |
|
|
CodeIntraMB(pEnc, pMB); |
|
|
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
|
|
dct_codes, qcoeff); |
|
1888 |
} |
} |
1889 |
|
|
1890 |
start_timer(); |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
|
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
|
|
stop_prediction_timer(); |
|
|
|
|
|
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
|
|
pEnc->sStat.kblks++; |
|
|
} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
|
1891 |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1892 |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
1893 |
pEnc->sStat.mblks++; |
pEnc->current->sStat.mblks++; |
1894 |
} else { |
} else { |
1895 |
pEnc->sStat.ublks++; |
pEnc->current->sStat.ublks++; |
1896 |
} |
} |
1897 |
|
|
1898 |
start_timer(); |
start_timer(); |
1899 |
|
|
1900 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1901 |
|
|
1902 |
if ((pMB->mode == MODE_NOT_CODED) || |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
1903 |
(pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && |
(pMB->dquant == NO_CHANGE); |
|
pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) { |
|
1904 |
|
|
1905 |
/* This is a candidate for SKIPping, but check intermediate B-frames first */ |
if (pEnc->current->coding_type == S_VOP) |
1906 |
|
skip_possible &= (pMB->mcsel == 1); |
1907 |
|
else if (pEnc->current->coding_type == P_VOP) { |
1908 |
|
if (pEnc->mbParam.m_quarterpel) |
1909 |
|
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
1910 |
|
else |
1911 |
|
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
1912 |
|
} |
1913 |
|
|
1914 |
|
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
1915 |
|
|
1916 |
|
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
1917 |
|
|
1918 |
|
if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
1919 |
|
{ |
1920 |
int bSkip = 1; |
int bSkip = 1; |
|
pMB->mode = MODE_NOT_CODED; |
|
1921 |
|
|
1922 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1923 |
{ |
{ |
1930 |
break; |
break; |
1931 |
} |
} |
1932 |
} |
} |
1933 |
if (!bSkip) |
|
1934 |
{ |
if (!bSkip) { /* no SKIP, but trivial block */ |
1935 |
VECTOR predMV; |
if(pEnc->mbParam.m_quarterpel) { |
1936 |
predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1937 |
pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y; |
pMB->pmvs[0].x = - predMV.x; |
1938 |
|
pMB->pmvs[0].y = - predMV.y; |
1939 |
|
} |
1940 |
|
else { |
1941 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1942 |
|
pMB->pmvs[0].x = - predMV.x; |
1943 |
|
pMB->pmvs[0].y = - predMV.y; |
1944 |
|
} |
1945 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1946 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1947 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1948 |
|
stop_coding_timer(); |
1949 |
|
|
1950 |
|
continue; /* next MB */ |
1951 |
|
} |
1952 |
} |
} |
1953 |
else MBSkip(bs); |
/* do SKIP */ |
1954 |
|
|
1955 |
|
pMB->mode = MODE_NOT_CODED; |
1956 |
|
MBSkip(bs); |
1957 |
|
stop_coding_timer(); |
1958 |
|
continue; /* next MB */ |
1959 |
|
} |
1960 |
|
/* ordinary case: normal coded INTER/INTER4V block */ |
1961 |
|
|
|
} else { |
|
1962 |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1963 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1964 |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1965 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1966 |
} |
} |
1967 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
|
1968 |
|
if(pEnc->mbParam.m_quarterpel) { |
1969 |
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1970 |
|
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
1971 |
|
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1972 |
|
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); |
1973 |
|
} else { |
1974 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1975 |
|
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
1976 |
|
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
1977 |
|
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); |
1978 |
|
} |
1979 |
|
|
1980 |
|
|
1981 |
|
if (pMB->mode == MODE_INTER4V) |
1982 |
|
{ int k; |
1983 |
|
for (k=1;k<4;k++) |
1984 |
|
{ |
1985 |
|
if(pEnc->mbParam.m_quarterpel) { |
1986 |
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1987 |
|
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
1988 |
|
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
1989 |
|
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); |
1990 |
|
} else { |
1991 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1992 |
|
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
1993 |
|
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; |
1994 |
|
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); |
1995 |
|
} |
1996 |
|
|
1997 |
|
} |
1998 |
} |
} |
1999 |
|
|
2000 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
2001 |
stop_coding_timer(); |
stop_coding_timer(); |
2002 |
|
|
2003 |
|
} |
2004 |
} |
} |
2005 |
|
|
2006 |
|
if ((pEnc->current->global_flags & XVID_REDUCED)) |
2007 |
|
{ |
2008 |
|
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
2009 |
|
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
2010 |
|
16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); |
2011 |
} |
} |
2012 |
|
|
2013 |
emms(); |
emms(); |
2016 |
HintedMEGet(pEnc, 0); |
HintedMEGet(pEnc, 0); |
2017 |
} |
} |
2018 |
|
|
2019 |
if (pEnc->sStat.iMvCount == 0) |
if (pEnc->current->sStat.iMvCount == 0) |
2020 |
pEnc->sStat.iMvCount = 1; |
pEnc->current->sStat.iMvCount = 1; |
2021 |
|
|
2022 |
fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount); |
2023 |
|
|
2024 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
2025 |
|
|
2026 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
2027 |
&& (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 |
&& (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128 |
2028 |
{ |
{ |
2029 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
2030 |
iSearchRange *= 2; |
iSearchRange *= 2; |
2031 |
} else if ((fSigma < iSearchRange / 6) |
} else if ((fSigma < iSearchRange / 6) |
2032 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->fMvPrevSigma >= 0) |
2033 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
2034 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16 |
2035 |
{ |
{ |
2036 |
pEnc->mbParam.m_fcode--; |
pEnc->mbParam.m_fcode--; |
2037 |
iSearchRange /= 2; |
iSearchRange /= 2; |
2038 |
} |
} |
2039 |
|
|
2040 |
pEnc->sStat.fMvPrevSigma = fSigma; |
pEnc->fMvPrevSigma = fSigma; |
2041 |
|
|
|
#ifdef FRAMEDROP |
|
2042 |
/* frame drop code */ |
/* frame drop code */ |
2043 |
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks); |
DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); |
2044 |
if (pEnc->sStat.kblks + pEnc->sStat.mblks < |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
2045 |
(pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100) |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
2046 |
{ |
{ |
2047 |
pEnc->sStat.kblks = pEnc->sStat.mblks = 0; |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0; |
2048 |
pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
pEnc->current->sStat.ublks = mb_width * mb_height; |
2049 |
|
|
2050 |
BitstreamReset(bs); |
BitstreamReset(bs); |
2051 |
|
|
2056 |
pEnc->current->quant = pEnc->reference->quant; |
pEnc->current->quant = pEnc->reference->quant; |
2057 |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
2058 |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
2059 |
|
pEnc->current->quarterpel = pEnc->reference->quarterpel; |
2060 |
pEnc->current->fcode = pEnc->reference->fcode; |
pEnc->current->fcode = pEnc->reference->fcode; |
2061 |
pEnc->current->bcode = pEnc->reference->bcode; |
pEnc->current->bcode = pEnc->reference->bcode; |
2062 |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
2063 |
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
2064 |
|
} |
2065 |
|
|
2066 |
|
/* XXX: debug |
2067 |
|
{ |
2068 |
|
char s[100]; |
2069 |
|
sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum); |
2070 |
|
image_dump_yuvpgm(&pEnc->current->image, |
2071 |
|
pEnc->mbParam.edged_width, |
2072 |
|
pEnc->mbParam.width, pEnc->mbParam.height, s); |
2073 |
|
|
2074 |
|
sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum); |
2075 |
|
image_dump_yuvpgm(&pEnc->reference->image, |
2076 |
|
pEnc->mbParam.edged_width, |
2077 |
|
pEnc->mbParam.width, pEnc->mbParam.height, s); |
2078 |
} |
} |
2079 |
#endif |
*/ |
2080 |
|
|
2081 |
|
|
2082 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
2083 |
|
|
2085 |
} |
} |
2086 |
|
|
2087 |
|
|
2088 |
static __inline void |
static void |
2089 |
FrameCodeB(Encoder * pEnc, |
FrameCodeB(Encoder * pEnc, |
2090 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
2091 |
Bitstream * bs, |
Bitstream * bs, |
2092 |
uint32_t * pBits) |
uint32_t * pBits) |
2093 |
{ |
{ |
2094 |
int16_t dct_codes[6 * 64]; |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
2095 |
int16_t qcoeff[6 * 64]; |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
2096 |
uint32_t x, y; |
uint32_t x, y; |
2097 |
|
|
2098 |
IMAGE *f_ref = &pEnc->reference->image; |
IMAGE *f_ref = &pEnc->reference->image; |
2105 |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
2106 |
} |
} |
2107 |
|
|
2108 |
|
pEnc->current->global_flags &= ~XVID_REDUCED; /* reduced resoltion not yet supported */ |
2109 |
|
|
2110 |
if (!first){ |
if (!first){ |
2111 |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
2112 |
} |
} |
2113 |
#endif |
#endif |
2114 |
|
|
2115 |
|
frame->quarterpel = pEnc->mbParam.m_quarterpel; |
2116 |
|
|
2117 |
// forward |
// forward |
2118 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
2119 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
2121 |
start_timer(); |
start_timer(); |
2122 |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
2123 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
2124 |
0); |
pEnc->mbParam.m_quarterpel, 0); |
2125 |
stop_inter_timer(); |
stop_inter_timer(); |
2126 |
|
|
2127 |
// backward |
// backward |
2131 |
start_timer(); |
start_timer(); |
2132 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
2133 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
2134 |
0); |
pEnc->mbParam.m_quarterpel, 0); |
2135 |
stop_inter_timer(); |
stop_inter_timer(); |
2136 |
|
|
2137 |
start_timer(); |
start_timer(); |
2141 |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), // time_pp |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), // time_pp |
2142 |
pEnc->reference->mbs, f_ref, |
pEnc->reference->mbs, f_ref, |
2143 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
2144 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
pEnc->current, b_ref, &pEnc->vInterH, |
2145 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
2146 |
|
|
2147 |
|
|
2159 |
|
|
2160 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
2161 |
|
|
2162 |
pEnc->sStat.iTextBits = 0; |
frame->sStat.iTextBits = 0; |
2163 |
pEnc->sStat.iMvSum = 0; |
frame->sStat.iMvSum = 0; |
2164 |
pEnc->sStat.iMvCount = 0; |
frame->sStat.iMvCount = 0; |
2165 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0; |
2166 |
|
|
2167 |
|
|
2168 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
2169 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
2170 |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
2171 |
int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0; |
int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0; |
2172 |
|
|
2173 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
2174 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
2200 |
#endif |
#endif |
2201 |
start_timer(); |
start_timer(); |
2202 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
2203 |
&pEnc->sStat, direction); |
&frame->sStat, direction); |
2204 |
stop_coding_timer(); |
stop_coding_timer(); |
2205 |
} |
} |
2206 |
} |
} |
2219 |
} |
} |
2220 |
#endif |
#endif |
2221 |
} |
} |
|
|
|
|
|
|
|
/* in case internal output is needed somewhere... */ |
|
|
/* { |
|
|
FILE *filehandle; |
|
|
filehandle=fopen("last-b.pgm","wb"); |
|
|
if (filehandle) |
|
|
{ |
|
|
fprintf(filehandle,"P5\n\n"); // |
|
|
fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height); |
|
|
fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle); |
|
|
fclose(filehandle); |
|
|
} |
|
|
} |
|
|
*/ |
|