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.26 2002-12-09 10:47:05 suxen_drol 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> |
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 |
229 |
|
|
230 |
pEnc->bitrate = pParam->rc_bitrate; |
pEnc->bitrate = pParam->rc_bitrate; |
231 |
|
|
232 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = -1; |
233 |
pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval; |
pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval; |
234 |
|
|
235 |
/* try to allocate frame memory */ |
/* try to allocate frame memory */ |
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 |
|
|
|
/* B Frames specific init */ |
|
328 |
|
|
329 |
pEnc->mbParam.global = pParam->global; |
pEnc->mbParam.global = pParam->global; |
330 |
|
|
331 |
|
/* B Frames specific init */ |
332 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
333 |
pEnc->mbParam.bquant_ratio = pParam->bquant_ratio; |
pEnc->mbParam.bquant_ratio = pParam->bquant_ratio; |
334 |
pEnc->mbParam.bquant_offset = pParam->bquant_offset; |
pEnc->mbParam.bquant_offset = pParam->bquant_offset; |
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 |
|
|
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; |
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; |
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(); |
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 = 2; |
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 |
|
|
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 = 4; |
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 |
|
|
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->mbParam.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"); |
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 |
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 = 5; |
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(); |
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 |
|
|
1007 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1008 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1009 |
|
|
1010 |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 || |
1011 |
(pFrame->intra < 0 && pEnc->mbParam.iMaxKeyInterval > 0 && |
(mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 && |
1012 |
pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval) |
pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) |
1013 |
|| 2 == (mode = MEanalysis(&pEnc->reference->image, pEnc->current, |
{ |
1014 |
|
mode = I_VOP; |
1015 |
|
}else{ |
1016 |
|
mode = MEanalysis(&pEnc->reference->image, pEnc->current, |
1017 |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1018 |
(pFrame->intra < 0) ? pEnc->iFrameNum : 0, |
(mode < 0) ? pEnc->iFrameNum : 0, |
1019 |
bframes_count++))) { |
bframes_count++); |
1020 |
|
} |
1021 |
|
|
1022 |
|
if (mode == I_VOP) { |
1023 |
/* |
/* |
1024 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
1025 |
*/ |
*/ |
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->mbParam.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; |
bframes_count = 0; |
|
|
|
1062 |
pFrame->intra = 0; |
pFrame->intra = 0; |
1063 |
|
|
1064 |
} else { |
} else { |
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) { |
|
// } else if (pFrame->intra == 0 || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
|
1086 |
/* |
/* |
1087 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
1088 |
*/ |
*/ |
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 |
*/ |
*/ |
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 |
|
|
1146 |
goto bvop_loop; |
goto bvop_loop; |
1147 |
} |
} |
1148 |
|
|
1149 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1150 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1151 |
|
|
1152 |
if (pResult) { |
if (pResult) { |
1155 |
pResult->kblks = pEnc->current->sStat.kblks; |
pResult->kblks = pEnc->current->sStat.kblks; |
1156 |
pResult->mblks = pEnc->current->sStat.mblks; |
pResult->mblks = pEnc->current->sStat.mblks; |
1157 |
pResult->ublks = pEnc->current->sStat.ublks; |
pResult->ublks = pEnc->current->sStat.ublks; |
|
} |
|
|
|
|
|
emms(); |
|
1158 |
|
|
1159 |
#ifdef _DEBUG_PSNR |
if (pFrame->general & XVID_EXTRASTATS) |
1160 |
psnr = |
{ pResult->sse_y = |
1161 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
plane_sse( pEnc->sOriginal.y, pEnc->current->image.y, |
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, |
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; |
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 |
|
|
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) { |
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; |
1635 |
|
|
1636 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1637 |
|
|
|
/* XXX: move this stuff to BitstreamWriteVolHeader */ |
|
|
#define DIVX501B481P "DivX501b481p" |
|
|
if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) { |
|
|
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
|
|
} |
|
|
|
|
|
#define XVID_ID "XviD" XVID_BS_VERSION |
|
|
BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID)); |
|
|
|
|
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); |
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; |
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, |
1720 |
|
|
1721 |
if ((pEnc->current->global_flags & XVID_REDUCED)) |
if ((pEnc->current->global_flags & XVID_REDUCED)) |
1722 |
{ |
{ |
1723 |
// mb_width = (pEnc->mbParam.width + 31) / 32; |
mb_width = (pEnc->mbParam.width + 31) / 32; |
1724 |
// mb_height = (pEnc->mbParam.height + 31) / 32; |
mb_height = (pEnc->mbParam.height + 31) / 32; |
|
|
|
|
/* XXX: reduced resoltion not yet supported */ |
|
|
pEnc->current->global_flags &= ~XVID_REDUCED; |
|
1725 |
} |
} |
1726 |
|
|
1727 |
|
|
1750 |
stop_inter_timer(); |
stop_inter_timer(); |
1751 |
} |
} |
1752 |
|
|
|
if (pEnc->current->global_flags & XVID_GMC) { |
|
|
// printf("Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel); |
|
|
DPRINTF(DPRINTF_HEADER, "Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel); |
|
|
pEnc->current->coding_type = S_VOP; |
|
|
} else |
|
1753 |
pEnc->current->coding_type = P_VOP; |
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 |
if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) ) |
if ( ( pEnc->current->global_flags & XVID_GMC ) |
1769 |
pEnc->current->coding_type = P_VOP; /* no global motion -> no GMC */ |
&& ( (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); |
1795 |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
1796 |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
1797 |
|
|
1798 |
|
|
1799 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
1800 |
for (x = 0; x < mb_width; x++) { |
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, |
pEnc->mbParam.m_quarterpel, |
1865 |
(pEnc->current->global_flags & XVID_REDUCED), |
(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->current->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->current->sStat.mblks++; |
pEnc->current->sStat.mblks++; |
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 |
skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) & |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
1903 |
(pMB->dquant == NO_CHANGE); |
(pMB->dquant == NO_CHANGE); |
1904 |
|
|
1905 |
|
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) |
if(pEnc->mbParam.m_quarterpel) |
1909 |
{ skip_possible &= (pMB->qmvs[0].x == pEnc->current->GMC_MV.x) & (pMB->qmvs[0].y == pEnc->current->GMC_MV.y); |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
|
} |
|
1910 |
else |
else |
1911 |
{ skip_possible &= (pMB->mvs[0].x == pEnc->current->GMC_MV.x) & (pMB->mvs[0].y == pEnc->current->GMC_MV.y); |
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
1912 |
} |
} |
1913 |
|
|
1914 |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
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 */ |
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
|
int bSkip = 1; |
|
1917 |
|
|
1918 |
if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
1919 |
|
{ |
1920 |
|
int bSkip = 1; |
1921 |
|
|
1922 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1923 |
{ |
{ |
1924 |
int iSAD; |
int iSAD; |
1931 |
} |
} |
1932 |
} |
} |
1933 |
|
|
1934 |
if (!bSkip) |
if (!bSkip) { /* no SKIP, but trivial block */ |
|
{ |
|
|
VECTOR predMV; |
|
1935 |
if(pEnc->mbParam.m_quarterpel) { |
if(pEnc->mbParam.m_quarterpel) { |
1936 |
predMV = get_qpmv2(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 = pMB->qmvs[0].x - predMV.x; /* with GMC, qmvs doesn't have to be (0,0)! */ |
pMB->pmvs[0].x = - predMV.x; |
1938 |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1939 |
} |
} |
1940 |
else { |
else { |
1941 |
predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1942 |
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; /* with GMC, mvs doesn't have to be (0,0)! */ |
pMB->pmvs[0].x = - predMV.x; |
1943 |
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
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->current->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1948 |
|
stop_coding_timer(); |
1949 |
|
|
1950 |
|
continue; /* next MB */ |
1951 |
} |
} |
1952 |
else |
} |
1953 |
{ |
/* do SKIP */ |
1954 |
|
|
1955 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
1956 |
MBSkip(bs); |
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->current->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(); |
2014 |
|
|
2015 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
2040 |
pEnc->fMvPrevSigma = fSigma; |
pEnc->fMvPrevSigma = fSigma; |
2041 |
|
|
2042 |
/* frame drop code */ |
/* frame drop code */ |
2043 |
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); |
DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); |
2044 |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
2045 |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
2046 |
{ |
{ |
2078 |
} |
} |
2079 |
*/ |
*/ |
2080 |
|
|
2081 |
|
|
2082 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
2083 |
|
|
2084 |
return 0; // inter |
return 0; // inter |
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; |
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); |
|
|
} |
|
|
} |
|
|
*/ |
|