26 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
27 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
28 |
* |
* |
|
****************************************************************************/ |
|
|
|
|
|
/***************************************************************************** |
|
|
* |
|
|
* History |
|
|
* |
|
|
* 10.07.2002 added BFRAMES_DEC_DEBUG support |
|
|
* MinChen <chenm001@163.com> |
|
|
* 20.06.2002 bframe patch |
|
|
* 08.05.2002 fix some problem in DEBUG mode; |
|
|
* MinChen <chenm001@163.com> |
|
|
* 14.04.2002 added FrameCodeB() |
|
|
* |
|
29 |
* $Id$ |
* $Id$ |
30 |
* |
* |
31 |
****************************************************************************/ |
****************************************************************************/ |
32 |
|
|
|
|
|
33 |
#include <stdlib.h> |
#include <stdlib.h> |
34 |
#include <stdio.h> |
#include <stdio.h> |
35 |
#include <math.h> |
#include <math.h> |
59 |
****************************************************************************/ |
****************************************************************************/ |
60 |
|
|
61 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
62 |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
#define SWAP(_T_,A,B) { _T_ tmp = A; A = B; B = tmp; } |
63 |
|
|
64 |
/***************************************************************************** |
/***************************************************************************** |
65 |
* Local function prototypes |
* Local function prototypes |
216 |
|
|
217 |
pEnc->bitrate = pParam->rc_bitrate; |
pEnc->bitrate = pParam->rc_bitrate; |
218 |
|
|
219 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = -1; |
220 |
pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval; |
pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval; |
221 |
|
|
222 |
/* try to allocate frame memory */ |
/* try to allocate frame memory */ |
241 |
|
|
242 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
243 |
|
|
244 |
#ifdef _DEBUG_PSNR |
if (pParam->global & XVID_GLOBAL_EXTRASTATS) |
245 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
|
#endif |
|
246 |
|
|
247 |
image_null(&pEnc->f_refh); |
image_null(&pEnc->f_refh); |
248 |
image_null(&pEnc->f_refv); |
image_null(&pEnc->f_refv); |
256 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
257 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
258 |
|
|
259 |
#ifdef _DEBUG_PSNR |
if (pParam->global & XVID_GLOBAL_EXTRASTATS) |
260 |
if (image_create |
{ if (image_create |
261 |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
262 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
263 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
264 |
#endif |
} |
265 |
|
|
266 |
if (image_create |
if (image_create |
267 |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
305 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
306 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
307 |
|
|
308 |
|
/* Create full bitplane for GMC, this might be wasteful */ |
309 |
|
if (image_create |
310 |
|
(&pEnc->vGMC, pEnc->mbParam.edged_width, |
311 |
|
pEnc->mbParam.edged_height) < 0) |
312 |
|
goto xvid_err_memory3; |
313 |
|
|
314 |
|
|
|
/* B Frames specific init */ |
|
315 |
|
|
316 |
pEnc->mbParam.global = pParam->global; |
pEnc->mbParam.global = pParam->global; |
317 |
|
|
318 |
|
/* B Frames specific init */ |
319 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
320 |
pEnc->mbParam.bquant_ratio = pParam->bquant_ratio; |
pEnc->mbParam.bquant_ratio = pParam->bquant_ratio; |
321 |
pEnc->mbParam.bquant_offset = pParam->bquant_offset; |
pEnc->mbParam.bquant_offset = pParam->bquant_offset; |
451 |
} |
} |
452 |
|
|
453 |
xvid_err_memory3: |
xvid_err_memory3: |
454 |
#ifdef _DEBUG_PSNR |
|
455 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS) |
456 |
|
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
457 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
458 |
#endif |
} |
459 |
|
|
460 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
461 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
479 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
480 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
481 |
|
|
482 |
|
/* destroy GMC image */ |
483 |
|
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
484 |
|
pEnc->mbParam.edged_height); |
485 |
|
|
486 |
|
|
487 |
xvid_err_memory2: |
xvid_err_memory2: |
488 |
xvid_free(pEnc->current->mbs); |
xvid_free(pEnc->current->mbs); |
489 |
xvid_free(pEnc->reference->mbs); |
xvid_free(pEnc->reference->mbs); |
571 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
572 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
573 |
|
|
574 |
#ifdef _DEBUG_PSNR |
if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS) |
575 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
576 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
577 |
#endif |
} |
578 |
|
|
579 |
/* Encoder structure */ |
/* Encoder structure */ |
580 |
|
|
592 |
|
|
593 |
static __inline void inc_frame_num(Encoder * pEnc) |
static __inline void inc_frame_num(Encoder * pEnc) |
594 |
{ |
{ |
595 |
pEnc->current->stamp = pEnc->mbParam.m_stamp; // first frame is zero |
pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */ |
596 |
pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
597 |
} |
} |
598 |
|
|
618 |
return; |
return; |
619 |
stop_conv_timer(); |
stop_conv_timer(); |
620 |
|
|
621 |
|
if ((pFrame->general & XVID_CHROMAOPT)) { |
622 |
|
image_chroma_optimize(&pEnc->queue[pEnc->queue_tail], |
623 |
|
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
624 |
|
} |
625 |
|
|
626 |
pEnc->queue_size++; |
pEnc->queue_size++; |
627 |
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
628 |
} |
} |
634 |
pCur->ticks = (int32_t)pCur->stamp % time_base; |
pCur->ticks = (int32_t)pCur->stamp % time_base; |
635 |
pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; |
pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; |
636 |
|
|
637 |
//HEAVY DEBUG OUTPUT remove when timecodes prove to be stable |
/* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */ |
638 |
|
|
639 |
/* fprintf(stderr,"WriteVop: %d - %d \n", |
/* fprintf(stderr,"WriteVop: %d - %d \n", |
640 |
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
676 |
{ |
{ |
677 |
uint16_t x, y; |
uint16_t x, y; |
678 |
Bitstream bs; |
Bitstream bs; |
679 |
uint32_t bits, mode; |
uint32_t bits; |
680 |
|
int mode = -1; /* Just to shut up compiler warning */ |
681 |
|
|
682 |
int input_valid = 1; |
int input_valid = 1; |
683 |
int bframes_count = 0; |
int bframes_count = 0; |
684 |
|
|
|
#ifdef _DEBUG_PSNR |
|
|
float psnr; |
|
|
char temp[128]; |
|
|
#endif |
|
|
|
|
685 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
686 |
ENC_CHECK(pFrame); |
ENC_CHECK(pFrame); |
687 |
ENC_CHECK(pFrame->image); |
ENC_CHECK(pFrame->image); |
711 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
712 |
|
|
713 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
714 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
715 |
|
|
716 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
717 |
|
|
718 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
719 |
bframes_count = 0; |
bframes_count = 0; |
720 |
|
|
721 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
722 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
723 |
pFrame->intra = 0; |
pFrame->intra = 0; |
724 |
|
|
725 |
|
|
726 |
emms(); |
emms(); |
727 |
|
|
728 |
|
if (pResult) { |
729 |
|
pResult->quant = pEnc->current->quant; |
730 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
731 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
732 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
733 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
734 |
|
} |
735 |
|
|
736 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
737 |
} |
} |
738 |
|
|
744 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
745 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
746 |
|
|
747 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
748 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
749 |
pFrame->intra = 2; |
pFrame->intra = 2; |
750 |
|
|
751 |
|
if (pResult) { |
752 |
|
pResult->quant = pEnc->current->quant; |
753 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
754 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
755 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
756 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
757 |
|
} |
758 |
|
|
759 |
if (input_valid) |
if (input_valid) |
760 |
queue_image(pEnc, pFrame); |
queue_image(pEnc, pFrame); |
761 |
|
|
781 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
782 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
783 |
|
|
|
BitstreamPad(&bs); |
|
784 |
|
|
785 |
tmp = pEnc->current->seconds; |
tmp = pEnc->current->seconds; |
786 |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
787 |
|
|
788 |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
789 |
pEnc->current->seconds = tmp; |
pEnc->current->seconds = tmp; |
790 |
|
|
791 |
|
BitstreamPadAlways(&bs); |
792 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
793 |
pFrame->intra = 4; |
pFrame->intra = 4; |
794 |
|
|
795 |
|
if (pResult) { |
796 |
|
pResult->quant = pEnc->current->quant; |
797 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
798 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
799 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
800 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
801 |
|
} |
802 |
|
|
803 |
if (input_valid) |
if (input_valid) |
804 |
queue_image(pEnc, pFrame); |
queue_image(pEnc, pFrame); |
805 |
|
|
815 |
if (pEnc->bframenum_dx50bvop != -1) |
if (pEnc->bframenum_dx50bvop != -1) |
816 |
{ |
{ |
817 |
|
|
818 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
819 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
820 |
|
|
821 |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
822 |
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"); |
830 |
|
|
831 |
} else if (input_valid) { |
} else if (input_valid) { |
832 |
|
|
833 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
834 |
|
|
835 |
start_timer(); |
start_timer(); |
836 |
if (image_input |
if (image_input |
842 |
} |
} |
843 |
stop_conv_timer(); |
stop_conv_timer(); |
844 |
|
|
845 |
// queue input frame, and dequue next image |
if ((pFrame->general & XVID_CHROMAOPT)) { |
846 |
|
image_chroma_optimize(&pEnc->current->image, |
847 |
|
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
848 |
|
} |
849 |
|
|
850 |
|
/* queue input frame, and dequue next image */ |
851 |
if (pEnc->queue_size > 0) |
if (pEnc->queue_size > 0) |
852 |
{ |
{ |
853 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); |
861 |
|
|
862 |
} else if (pEnc->queue_size > 0) { |
} else if (pEnc->queue_size > 0) { |
863 |
|
|
864 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
865 |
|
|
866 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
867 |
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
878 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
879 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
880 |
|
|
881 |
BitstreamPutBits(&bs, 0x7f, 8); |
/* That disabled line of code was supposed to inform VirtualDub |
882 |
|
* that the frame was a dummy delay frame - now disabled (thx god :-) |
883 |
|
*/ |
884 |
|
/* BitstreamPutBits(&bs, 0x7f, 8); */ |
885 |
pFrame->intra = 5; |
pFrame->intra = 5; |
886 |
|
|
887 |
|
if (pResult) { |
888 |
|
/* |
889 |
|
* We must decide what to put there because i know some apps |
890 |
|
* are storing statistics about quantizers and just do |
891 |
|
* stats[quant]++ or stats[quant-1]++ |
892 |
|
* transcode is one of these app with its 2pass module |
893 |
|
*/ |
894 |
|
|
895 |
|
/* |
896 |
|
* For now i prefer 31 than 0 that could lead to a segfault |
897 |
|
* in transcode |
898 |
|
*/ |
899 |
|
pResult->quant = 31; |
900 |
|
|
901 |
|
pResult->hlength = 0; |
902 |
|
pResult->kblks = 0; |
903 |
|
pResult->mblks = 0; |
904 |
|
pResult->ublks = 0; |
905 |
|
} |
906 |
|
|
907 |
|
} else { |
908 |
|
|
909 |
|
if (pResult) { |
910 |
|
pResult->quant = pEnc->current->quant; |
911 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
912 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
913 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
914 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
915 |
|
} |
916 |
|
|
917 |
} |
} |
918 |
|
|
919 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
920 |
|
|
921 |
emms(); |
emms(); |
922 |
|
|
923 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
924 |
} |
} |
925 |
|
|
927 |
|
|
928 |
emms(); |
emms(); |
929 |
|
|
930 |
// 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 */ |
931 |
if (pEnc->bframenum_dx50bvop < 0 ) |
if (pEnc->bframenum_dx50bvop < 0 ) |
932 |
{ |
{ |
933 |
mode = intra2coding_type(pFrame->intra); |
mode = intra2coding_type(pFrame->intra); |
951 |
|
|
952 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
953 |
|
|
954 |
#ifdef _DEBUG_PSNR |
if (pFrame->general & XVID_EXTRASTATS) |
955 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
{ image_copy(&pEnc->sOriginal, &pEnc->current->image, |
956 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
957 |
#endif |
} |
958 |
|
|
959 |
emms(); |
emms(); |
960 |
|
|
1043 |
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"); |
1044 |
} |
} |
1045 |
|
|
1046 |
// 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 */ |
1047 |
|
|
1048 |
if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
1049 |
|
|
1050 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
1051 |
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
1052 |
|
|
1053 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
1054 |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
1055 |
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"); |
1056 |
} |
} |
1057 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
1058 |
bframes_count = 0; |
bframes_count = 0; |
|
|
|
1059 |
pFrame->intra = 0; |
pFrame->intra = 0; |
1060 |
|
|
1061 |
} else { |
} else { |
1079 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
1080 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
1081 |
*/ |
*/ |
1082 |
} else if (mode == P_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
} else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1083 |
/* |
/* |
1084 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
1085 |
*/ |
*/ |
1130 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1131 |
|
|
1132 |
/* store frame into bframe buffer & swap ref back to current */ |
/* store frame into bframe buffer & swap ref back to current */ |
1133 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1134 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
1135 |
|
|
1136 |
pEnc->bframenum_tail++; |
pEnc->bframenum_tail++; |
1137 |
|
|
1138 |
// bframe report by koepi |
/* bframe report by koepi */ |
1139 |
pFrame->intra = 2; |
pFrame->intra = 2; |
1140 |
pFrame->length = 0; |
pFrame->length = 0; |
1141 |
|
|
1143 |
goto bvop_loop; |
goto bvop_loop; |
1144 |
} |
} |
1145 |
|
|
1146 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1147 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1148 |
|
|
1149 |
if (pResult) { |
if (pResult) { |
1152 |
pResult->kblks = pEnc->current->sStat.kblks; |
pResult->kblks = pEnc->current->sStat.kblks; |
1153 |
pResult->mblks = pEnc->current->sStat.mblks; |
pResult->mblks = pEnc->current->sStat.mblks; |
1154 |
pResult->ublks = pEnc->current->sStat.ublks; |
pResult->ublks = pEnc->current->sStat.ublks; |
|
} |
|
|
|
|
|
emms(); |
|
1155 |
|
|
1156 |
#ifdef _DEBUG_PSNR |
if (pFrame->general & XVID_EXTRASTATS) |
1157 |
psnr = |
{ pResult->sse_y = |
1158 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
plane_sse( pEnc->sOriginal.y, pEnc->current->image.y, |
1159 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1160 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1161 |
|
|
1162 |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
pResult->sse_u = |
1163 |
DEBUG(temp); |
plane_sse( pEnc->sOriginal.u, pEnc->current->image.u, |
1164 |
#endif |
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
1165 |
|
pEnc->mbParam.height/2); |
1166 |
|
|
1167 |
|
pResult->sse_v = |
1168 |
|
plane_sse( pEnc->sOriginal.v, pEnc->current->image.v, |
1169 |
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
1170 |
|
pEnc->mbParam.height/2); |
1171 |
|
} |
1172 |
|
} |
1173 |
|
|
1174 |
|
emms(); |
1175 |
|
|
1176 |
if (pFrame->quant == 0) { |
if (pFrame->quant == 0) { |
1177 |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1206 |
uint32_t bits; |
uint32_t bits; |
1207 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
1208 |
|
|
|
#ifdef _DEBUG_PSNR |
|
1209 |
float psnr; |
float psnr; |
1210 |
uint8_t temp[128]; |
char temp[128]; |
|
#endif |
|
1211 |
|
|
1212 |
start_global_timer(); |
start_global_timer(); |
1213 |
|
|
1216 |
ENC_CHECK(pFrame->bitstream); |
ENC_CHECK(pFrame->bitstream); |
1217 |
ENC_CHECK(pFrame->image); |
ENC_CHECK(pFrame->image); |
1218 |
|
|
1219 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
1220 |
|
|
1221 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1222 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
1238 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
1239 |
stop_conv_timer(); |
stop_conv_timer(); |
1240 |
|
|
1241 |
#ifdef _DEBUG_PSNR |
if ((pFrame->general & XVID_CHROMAOPT)) { |
1242 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_chroma_optimize(&pEnc->current->image, |
1243 |
|
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
1244 |
|
} |
1245 |
|
|
1246 |
|
if (pFrame->general & XVID_EXTRASTATS) |
1247 |
|
{ image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1248 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1249 |
#endif |
} |
1250 |
|
|
1251 |
emms(); |
emms(); |
1252 |
|
|
1323 |
} |
} |
1324 |
|
|
1325 |
if (pFrame->intra < 0) { |
if (pFrame->intra < 0) { |
1326 |
if ((pEnc->iFrameNum == 0) |
if ((pEnc->iFrameNum == -1) |
1327 |
|| ((pEnc->mbParam.iMaxKeyInterval > 0) |
|| ((pEnc->mbParam.iMaxKeyInterval > 0) |
1328 |
&& (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) { |
&& (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) { |
1329 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
1339 |
|
|
1340 |
} |
} |
1341 |
|
|
1342 |
|
/* Relic from OpenDivX - now disabled |
1343 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
1344 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
1345 |
BitstreamPad(&bs); |
*/ |
1346 |
|
|
1347 |
|
BitstreamPadAlways(&bs); |
1348 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1349 |
|
|
1350 |
if (pResult) { |
if (pResult) { |
1361 |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1362 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1363 |
} |
} |
1364 |
#ifdef _DEBUG_PSNR |
if (pFrame->general & XVID_EXTRASTATS) |
1365 |
|
{ |
1366 |
psnr = |
psnr = |
1367 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1368 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1369 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1370 |
|
|
1371 |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
1372 |
DEBUG(temp); |
} |
|
#endif |
|
1373 |
|
|
1374 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1375 |
|
|
1434 |
return; |
return; |
1435 |
} |
} |
1436 |
|
|
1437 |
pEnc->current->fcode = |
pEnc->current->fcode = (hint->rawhints) ? |
1438 |
(hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, |
(uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
|
FCODEBITS); |
|
1439 |
|
|
1440 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1441 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1450 |
VECTOR tmp; |
VECTOR tmp; |
1451 |
int vec; |
int vec; |
1452 |
|
|
1453 |
pMB->mode = |
pMB->mode = (hint->rawhints) ? |
1454 |
(hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, |
(uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS); |
|
MODEBITS); |
|
1455 |
|
|
1456 |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
1457 |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
1458 |
|
|
1459 |
if (pMB->mode == MODE_INTER) { |
if (pMB->mode == MODE_INTER) { |
1460 |
tmp.x = |
tmp.x = (hint->rawhints) ? |
1461 |
(hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, |
bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length); |
1462 |
length); |
tmp.y = (hint->rawhints) ? |
1463 |
tmp.y = |
bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length); |
|
(hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, |
|
|
length); |
|
1464 |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1465 |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
1466 |
|
|
1474 |
} |
} |
1475 |
} else if (pMB->mode == MODE_INTER4V) { |
} else if (pMB->mode == MODE_INTER4V) { |
1476 |
for (vec = 0; vec < 4; ++vec) { |
for (vec = 0; vec < 4; ++vec) { |
1477 |
tmp.x = |
tmp.x = (hint->rawhints) ? |
1478 |
(hint->rawhints) ? bhint->mvs[vec]. |
bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length); |
1479 |
x : BitstreamGetBits(&bs, length); |
tmp.y = (hint->rawhints) ? |
1480 |
tmp.y = |
bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length); |
|
(hint->rawhints) ? bhint->mvs[vec]. |
|
|
y : BitstreamGetBits(&bs, length); |
|
1481 |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1482 |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
1483 |
|
|
1488 |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x; |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x; |
1489 |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y; |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y; |
1490 |
} |
} |
1491 |
} else // intra / stuffing / not_coded |
} else /* intra / stuffing / not_coded */ |
1492 |
{ |
{ |
1493 |
for (vec = 0; vec < 4; ++vec) { |
for (vec = 0; vec < 4; ++vec) { |
1494 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1529 |
|
|
1530 |
if (intra) { |
if (intra) { |
1531 |
if (!hint->rawhints) { |
if (!hint->rawhints) { |
1532 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1533 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1534 |
} |
} |
1535 |
return; |
return; |
1634 |
|
|
1635 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1636 |
|
|
|
/* 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)); |
|
|
|
|
1637 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1638 |
|
|
1639 |
|
BitstreamPadAlways(bs); |
1640 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1641 |
|
|
1642 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1672 |
if ((pEnc->current->global_flags & XVID_REDUCED)) |
if ((pEnc->current->global_flags & XVID_REDUCED)) |
1673 |
{ |
{ |
1674 |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
1675 |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width); |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
1676 |
|
16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); |
1677 |
} |
} |
1678 |
emms(); |
emms(); |
1679 |
|
|
1685 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
1686 |
} |
} |
1687 |
|
|
1688 |
return 1; // intra |
return 1; /* intra */ |
1689 |
} |
} |
1690 |
|
|
1691 |
|
|
1692 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1693 |
#define BFRAME_SKIP_THRESHHOLD 30 |
#define BFRAME_SKIP_THRESHHOLD 30 |
1694 |
|
|
1695 |
|
|
1696 |
|
/* FrameCodeP also handles S(GMC)-VOPs */ |
1697 |
static int |
static int |
1698 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1699 |
Bitstream * bs, |
Bitstream * bs, |
1749 |
stop_inter_timer(); |
stop_inter_timer(); |
1750 |
} |
} |
1751 |
|
|
|
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 |
|
1752 |
pEnc->current->coding_type = P_VOP; |
pEnc->current->coding_type = P_VOP; |
1753 |
|
|
1754 |
start_timer(); |
start_timer(); |
1755 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
1756 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1757 |
else |
else |
|
|
|
1758 |
bIntra = |
bIntra = |
1759 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1760 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1764 |
|
|
1765 |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
1766 |
|
|
1767 |
if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) ) |
if ( ( pEnc->current->global_flags & XVID_GMC ) |
1768 |
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) ) ) |
1769 |
|
{ |
1770 |
|
pEnc->current->coding_type = S_VOP; |
1771 |
|
|
1772 |
if (vol_header) |
generate_GMCparameters( 2, 16, &pEnc->current->warp, |
1773 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
pEnc->mbParam.width, pEnc->mbParam.height, |
1774 |
|
&pEnc->current->gmc_data); |
1775 |
|
|
1776 |
|
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
1777 |
|
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
1778 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
1779 |
|
pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0, |
1780 |
|
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
1781 |
|
|
1782 |
|
} |
1783 |
|
|
1784 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1785 |
|
if (vol_header) |
1786 |
|
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1787 |
|
BitstreamPadAlways(bs); |
1788 |
|
} |
1789 |
|
|
1790 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1791 |
|
|
1792 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1794 |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
1795 |
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; |
1796 |
|
|
1797 |
|
|
1798 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
1799 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1800 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1801 |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1802 |
|
|
1803 |
|
/* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */ |
1804 |
|
/* For a start, leave INTRA decision as is, only choose only between INTER/GMC - gruel, 9.1.2002 */ |
1805 |
|
|
1806 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1807 |
|
|
1808 |
if (!bIntra) { |
if (bIntra) { |
1809 |
|
CodeIntraMB(pEnc, pMB); |
1810 |
|
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1811 |
|
dct_codes, qcoeff); |
1812 |
|
|
1813 |
|
start_timer(); |
1814 |
|
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1815 |
|
stop_prediction_timer(); |
1816 |
|
|
1817 |
|
pEnc->current->sStat.kblks++; |
1818 |
|
|
1819 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1820 |
|
stop_coding_timer(); |
1821 |
|
continue; |
1822 |
|
} |
1823 |
|
|
1824 |
|
if (pEnc->current->coding_type == S_VOP) { |
1825 |
|
|
1826 |
|
int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1827 |
|
pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1828 |
|
pEnc->mbParam.edged_width, 65536); |
1829 |
|
|
1830 |
|
if (pEnc->current->motion_flags & PMV_CHROMA16) { |
1831 |
|
iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
1832 |
|
pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
1833 |
|
|
1834 |
|
iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
1835 |
|
pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
1836 |
|
} |
1837 |
|
|
1838 |
|
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
1839 |
|
|
1840 |
|
if (pEnc->mbParam.m_quarterpel) |
1841 |
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
1842 |
|
else |
1843 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
1844 |
|
|
1845 |
|
pMB->mode = MODE_INTER; |
1846 |
|
pMB->mcsel = 1; |
1847 |
|
pMB->sad16 = iSAD; |
1848 |
|
} else { |
1849 |
|
pMB->mcsel = 0; |
1850 |
|
} |
1851 |
|
} else { |
1852 |
|
pMB->mcsel = 0; /* just a precaution */ |
1853 |
|
} |
1854 |
|
|
1855 |
start_timer(); |
start_timer(); |
1856 |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1857 |
&pEnc->vInterH, &pEnc->vInterV, |
&pEnc->vInterH, &pEnc->vInterV, |
1858 |
&pEnc->vInterHV, &pEnc->current->image, |
&pEnc->vInterHV, &pEnc->vGMC, |
1859 |
|
&pEnc->current->image, |
1860 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pEnc->mbParam.width, |
1861 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1862 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1863 |
pEnc->mbParam.m_quarterpel, |
pEnc->mbParam.m_quarterpel, |
1864 |
(pEnc->current->global_flags & XVID_REDUCED), |
(pEnc->current->global_flags & XVID_REDUCED), |
1865 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1866 |
|
|
1867 |
stop_comp_timer(); |
stop_comp_timer(); |
1868 |
|
|
1869 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1881 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1882 |
|
|
1883 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->mode != MODE_NOT_CODED) |
1884 |
pMB->cbp = |
{ pMB->cbp = |
1885 |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1886 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
|
} else { |
|
|
CodeIntraMB(pEnc, pMB); |
|
|
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
|
|
dct_codes, qcoeff); |
|
1887 |
} |
} |
1888 |
|
|
1889 |
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 || |
|
1890 |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1891 |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
1892 |
pEnc->current->sStat.mblks++; |
pEnc->current->sStat.mblks++; |
1898 |
|
|
1899 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1900 |
|
|
1901 |
skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) & |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
1902 |
(pMB->dquant == NO_CHANGE); |
(pMB->dquant == NO_CHANGE); |
1903 |
|
|
1904 |
|
if (pEnc->current->coding_type == S_VOP) |
1905 |
|
skip_possible &= (pMB->mcsel == 1); |
1906 |
|
else if (pEnc->current->coding_type == P_VOP) { |
1907 |
if(pEnc->mbParam.m_quarterpel) |
if(pEnc->mbParam.m_quarterpel) |
1908 |
{ 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) ); |
|
} |
|
1909 |
else |
else |
1910 |
{ 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) ); |
1911 |
} |
} |
1912 |
|
|
1913 |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
1914 |
|
|
1915 |
/* 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; |
|
1916 |
|
|
1917 |
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 */ |
1918 |
|
{ |
1919 |
|
int bSkip = 1; |
1920 |
|
|
1921 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1922 |
{ |
{ |
1923 |
int iSAD; |
int iSAD; |
1930 |
} |
} |
1931 |
} |
} |
1932 |
|
|
1933 |
if (!bSkip) |
if (!bSkip) { /* no SKIP, but trivial block */ |
|
{ |
|
|
VECTOR predMV; |
|
1934 |
if(pEnc->mbParam.m_quarterpel) { |
if(pEnc->mbParam.m_quarterpel) { |
1935 |
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); |
1936 |
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; |
1937 |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1938 |
} |
} |
1939 |
else { |
else { |
1940 |
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); |
1941 |
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; |
1942 |
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1943 |
} |
} |
1944 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1945 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1946 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1947 |
|
stop_coding_timer(); |
1948 |
|
|
1949 |
|
continue; /* next MB */ |
1950 |
} |
} |
1951 |
else |
} |
1952 |
{ |
/* do SKIP */ |
1953 |
|
|
1954 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
1955 |
MBSkip(bs); |
MBSkip(bs); |
1956 |
|
stop_coding_timer(); |
1957 |
|
continue; /* next MB */ |
1958 |
} |
} |
1959 |
|
/* ordinary case: normal coded INTER/INTER4V block */ |
1960 |
|
|
|
} else { |
|
1961 |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1962 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1963 |
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 */ |
1964 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1965 |
} |
} |
1966 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
|
1967 |
|
if(pEnc->mbParam.m_quarterpel) { |
1968 |
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1969 |
|
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
1970 |
|
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1971 |
|
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); |
1972 |
|
} else { |
1973 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1974 |
|
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
1975 |
|
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
1976 |
|
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); |
1977 |
|
} |
1978 |
|
|
1979 |
|
|
1980 |
|
if (pMB->mode == MODE_INTER4V) |
1981 |
|
{ int k; |
1982 |
|
for (k=1;k<4;k++) |
1983 |
|
{ |
1984 |
|
if(pEnc->mbParam.m_quarterpel) { |
1985 |
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1986 |
|
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
1987 |
|
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
1988 |
|
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); |
1989 |
|
} else { |
1990 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1991 |
|
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
1992 |
|
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; |
1993 |
|
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); |
1994 |
|
} |
1995 |
|
|
1996 |
|
} |
1997 |
} |
} |
1998 |
|
|
1999 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
2000 |
stop_coding_timer(); |
stop_coding_timer(); |
2001 |
|
|
2002 |
} |
} |
2003 |
} |
} |
2004 |
|
|
2005 |
if ((pEnc->current->global_flags & XVID_REDUCED)) |
if ((pEnc->current->global_flags & XVID_REDUCED)) |
2006 |
{ |
{ |
2007 |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
2008 |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width); |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
2009 |
|
16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); |
2010 |
} |
} |
2011 |
|
|
2012 |
emms(); |
emms(); |
2023 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
2024 |
|
|
2025 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
2026 |
&& (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128 |
&& (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) /* maximum search range 128 */ |
2027 |
{ |
{ |
2028 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
2029 |
iSearchRange *= 2; |
iSearchRange *= 2; |
2030 |
} else if ((fSigma < iSearchRange / 6) |
} else if ((fSigma < iSearchRange / 6) |
2031 |
&& (pEnc->fMvPrevSigma >= 0) |
&& (pEnc->fMvPrevSigma >= 0) |
2032 |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
2033 |
&& (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) /* minimum search range 16 */ |
2034 |
{ |
{ |
2035 |
pEnc->mbParam.m_fcode--; |
pEnc->mbParam.m_fcode--; |
2036 |
iSearchRange /= 2; |
iSearchRange /= 2; |
2039 |
pEnc->fMvPrevSigma = fSigma; |
pEnc->fMvPrevSigma = fSigma; |
2040 |
|
|
2041 |
/* frame drop code */ |
/* frame drop code */ |
2042 |
// 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); |
2043 |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
2044 |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
2045 |
{ |
{ |
2051 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
2052 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
2053 |
|
|
2054 |
// copy reference frame details into the current frame |
/* copy reference frame details into the current frame */ |
2055 |
pEnc->current->quant = pEnc->reference->quant; |
pEnc->current->quant = pEnc->reference->quant; |
2056 |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
2057 |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
2080 |
|
|
2081 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
2082 |
|
|
2083 |
return 0; // inter |
return 0; /* inter */ |
2084 |
} |
} |
2085 |
|
|
2086 |
|
|
2090 |
Bitstream * bs, |
Bitstream * bs, |
2091 |
uint32_t * pBits) |
uint32_t * pBits) |
2092 |
{ |
{ |
2093 |
int16_t dct_codes[6 * 64]; |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
2094 |
int16_t qcoeff[6 * 64]; |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
2095 |
uint32_t x, y; |
uint32_t x, y; |
2096 |
|
|
2097 |
IMAGE *f_ref = &pEnc->reference->image; |
IMAGE *f_ref = &pEnc->reference->image; |
2113 |
|
|
2114 |
frame->quarterpel = pEnc->mbParam.m_quarterpel; |
frame->quarterpel = pEnc->mbParam.m_quarterpel; |
2115 |
|
|
2116 |
// forward |
/* forward */ |
2117 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
2118 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
2119 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
2123 |
pEnc->mbParam.m_quarterpel, 0); |
pEnc->mbParam.m_quarterpel, 0); |
2124 |
stop_inter_timer(); |
stop_inter_timer(); |
2125 |
|
|
2126 |
// backward |
/* backward */ |
2127 |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
2128 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
2129 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
2136 |
start_timer(); |
start_timer(); |
2137 |
|
|
2138 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
2139 |
((int32_t)(pEnc->current->stamp - frame->stamp)), // time_bp |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
2140 |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), // time_pp |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
2141 |
pEnc->reference->mbs, f_ref, |
pEnc->reference->mbs, f_ref, |
2142 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
2143 |
pEnc->current, b_ref, &pEnc->vInterH, |
pEnc->current, b_ref, &pEnc->vInterH, |
2146 |
|
|
2147 |
stop_motion_timer(); |
stop_motion_timer(); |
2148 |
|
|
2149 |
/*if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
/* |
2150 |
{ |
if (test_quant_type(&pEnc->mbParam, pEnc->current)) { |
2151 |
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
2152 |
} */ |
} |
2153 |
|
*/ |
2154 |
|
|
2155 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
2156 |
|
|
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->mbParam.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) { |
2175 |
//mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; |
/* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */ |
2176 |
continue; |
continue; |
2177 |
} |
} |
2178 |
|
|
2191 |
|
|
2192 |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
2193 |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
2194 |
mb->mode = MODE_DIRECT_NONE_MV; // skipped |
mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
2195 |
} |
} |
2196 |
} |
} |
2197 |
|
|
2207 |
|
|
2208 |
emms(); |
emms(); |
2209 |
|
|
2210 |
// TODO: dynamic fcode/bcode ??? |
/* TODO: dynamic fcode/bcode ??? */ |
2211 |
|
|
2212 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
2213 |
|
|
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); |
|
|
} |
|
|
} |
|
|
*/ |
|