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.54 2002-07-11 00:15:59 chenm001 Exp $ |
* $Id: encoder.c,v 1.76.2.15 2002-11-02 16:12:27 chl Exp $ |
43 |
* |
* |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
46 |
|
|
47 |
#include <stdlib.h> |
#include <stdlib.h> |
48 |
#include <stdio.h> |
#include <stdio.h> |
49 |
#include <math.h> |
#include <math.h> |
54 |
#include "global.h" |
#include "global.h" |
55 |
#include "utils/timer.h" |
#include "utils/timer.h" |
56 |
#include "image/image.h" |
#include "image/image.h" |
|
#ifdef BFRAMES |
|
57 |
#include "image/font.h" |
#include "image/font.h" |
58 |
#endif |
#include "motion/sad.h" |
59 |
#include "motion/motion.h" |
#include "motion/motion.h" |
60 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
61 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
68 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
69 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
70 |
|
|
|
#ifdef _SMP |
|
|
#include "motion/smp_motion_est.h" |
|
|
#endif |
|
71 |
/***************************************************************************** |
/***************************************************************************** |
72 |
* Local macros |
* Local macros |
73 |
****************************************************************************/ |
****************************************************************************/ |
89 |
bool force_inter, |
bool force_inter, |
90 |
bool vol_header); |
bool vol_header); |
91 |
|
|
|
#ifdef BFRAMES |
|
92 |
static void FrameCodeB(Encoder * pEnc, |
static void FrameCodeB(Encoder * pEnc, |
93 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
94 |
Bitstream * bs, |
Bitstream * bs, |
95 |
uint32_t * pBits); |
uint32_t * pBits); |
|
#endif |
|
96 |
|
|
97 |
/***************************************************************************** |
/***************************************************************************** |
98 |
* Local data |
* Local data |
204 |
|
|
205 |
/* 1 keyframe each 10 seconds */ |
/* 1 keyframe each 10 seconds */ |
206 |
|
|
207 |
if (pParam->max_key_interval == 0) |
if (pParam->max_key_interval <= 0) |
208 |
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
209 |
|
|
|
|
|
210 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
211 |
if (pEnc == NULL) |
if (pEnc == NULL) |
212 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
265 |
#ifdef _DEBUG_PSNR |
#ifdef _DEBUG_PSNR |
266 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
267 |
#endif |
#endif |
268 |
#ifdef BFRAMES |
|
269 |
image_null(&pEnc->f_refh); |
image_null(&pEnc->f_refh); |
270 |
image_null(&pEnc->f_refv); |
image_null(&pEnc->f_refv); |
271 |
image_null(&pEnc->f_refhv); |
image_null(&pEnc->f_refhv); |
272 |
#endif |
|
273 |
image_null(&pEnc->current->image); |
image_null(&pEnc->current->image); |
274 |
image_null(&pEnc->reference->image); |
image_null(&pEnc->reference->image); |
275 |
image_null(&pEnc->vInterH); |
image_null(&pEnc->vInterH); |
284 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
285 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
286 |
#endif |
#endif |
287 |
#ifdef BFRAMES |
|
288 |
if (image_create |
if (image_create |
289 |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
290 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
297 |
(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
298 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
299 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
300 |
#endif |
|
301 |
if (image_create |
if (image_create |
302 |
(&pEnc->current->image, pEnc->mbParam.edged_width, |
(&pEnc->current->image, pEnc->mbParam.edged_width, |
303 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
330 |
|
|
331 |
|
|
332 |
/* B Frames specific init */ |
/* B Frames specific init */ |
|
#ifdef BFRAMES |
|
333 |
|
|
334 |
pEnc->global = pParam->global; |
pEnc->global = pParam->global; |
335 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
336 |
pEnc->bquant_ratio = pParam->bquant_ratio; |
pEnc->bquant_ratio = pParam->bquant_ratio; |
337 |
|
pEnc->frame_drop_ratio = pParam->frame_drop_ratio; |
338 |
pEnc->bframes = NULL; |
pEnc->bframes = NULL; |
339 |
|
|
340 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
408 |
pEnc->queue_tail = 0; |
pEnc->queue_tail = 0; |
409 |
pEnc->queue_size = 0; |
pEnc->queue_size = 0; |
410 |
|
|
411 |
|
pEnc->mbParam.m_stamp = 0; |
412 |
|
|
|
pEnc->mbParam.m_seconds = 0; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
413 |
pEnc->m_framenum = 0; |
pEnc->m_framenum = 0; |
414 |
#endif |
pEnc->current->stamp = 0; |
415 |
|
pEnc->reference->stamp = 0; |
416 |
|
|
417 |
pParam->handle = (void *) pEnc; |
pParam->handle = (void *) pEnc; |
418 |
|
|
431 |
/* |
/* |
432 |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
433 |
*/ |
*/ |
434 |
#ifdef BFRAMES |
|
435 |
xvid_err_memory5: |
xvid_err_memory5: |
436 |
|
|
437 |
|
|
465 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
466 |
} |
} |
467 |
|
|
|
#endif |
|
|
|
|
468 |
xvid_err_memory3: |
xvid_err_memory3: |
469 |
#ifdef _DEBUG_PSNR |
#ifdef _DEBUG_PSNR |
470 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
471 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
472 |
#endif |
#endif |
473 |
|
|
|
#ifdef BFRAMES |
|
474 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
475 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
476 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
477 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
478 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
479 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
#endif |
|
480 |
|
|
481 |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
482 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
521 |
int |
int |
522 |
encoder_destroy(Encoder * pEnc) |
encoder_destroy(Encoder * pEnc) |
523 |
{ |
{ |
|
#ifdef BFRAMES |
|
524 |
int i; |
int i; |
|
#endif |
|
525 |
|
|
526 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
527 |
|
|
528 |
/* B Frames specific */ |
/* B Frames specific */ |
|
#ifdef BFRAMES |
|
529 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
530 |
|
|
531 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
555 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
556 |
|
|
557 |
} |
} |
|
#endif |
|
558 |
|
|
559 |
/* All images, reference, current etc ... */ |
/* All images, reference, current etc ... */ |
560 |
|
|
572 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
573 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
574 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
575 |
#ifdef BFRAMES |
|
576 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
577 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
578 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
579 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
580 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
581 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
582 |
#endif |
|
583 |
#ifdef _DEBUG_PSNR |
#ifdef _DEBUG_PSNR |
584 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
585 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
599 |
} |
} |
600 |
|
|
601 |
|
|
602 |
#ifdef BFRAMES |
static __inline void inc_frame_num(Encoder * pEnc) |
|
void inc_frame_num(Encoder * pEnc) |
|
603 |
{ |
{ |
604 |
pEnc->iFrameNum++; |
pEnc->current->stamp = pEnc->mbParam.m_stamp; // first frame is zero |
605 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
|
|
|
|
pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
|
|
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
|
606 |
} |
} |
|
#endif |
|
607 |
|
|
608 |
|
|
609 |
#ifdef BFRAMES |
static __inline void |
610 |
void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
611 |
{ |
{ |
612 |
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
613 |
{ |
{ |
630 |
pEnc->queue_size++; |
pEnc->queue_size++; |
631 |
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
632 |
} |
} |
633 |
#endif |
|
634 |
|
static __inline void |
635 |
|
set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base) |
636 |
|
{ |
637 |
|
|
638 |
|
pCur->ticks = (int32_t)pCur->stamp % time_base; |
639 |
|
pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; |
640 |
|
|
641 |
|
//HEAVY DEBUG OUTPUT remove when timecodes prove to be stable |
642 |
|
|
643 |
|
/* fprintf(stderr,"WriteVop: %d - %d \n", |
644 |
|
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
645 |
|
fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", |
646 |
|
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
647 |
|
fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", |
648 |
|
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
649 |
|
|
650 |
|
*/ |
651 |
|
} |
652 |
|
|
653 |
|
|
654 |
|
|
655 |
|
|
656 |
|
|
|
#ifdef BFRAMES |
|
657 |
/***************************************************************************** |
/***************************************************************************** |
658 |
* IPB frame encoder entry point |
* IPB frame encoder entry point |
659 |
* |
* |
670 |
{ |
{ |
671 |
uint16_t x, y; |
uint16_t x, y; |
672 |
Bitstream bs; |
Bitstream bs; |
673 |
uint32_t bits; |
uint32_t bits, mode; |
674 |
|
|
675 |
int input_valid = 1; |
int input_valid = 1; |
676 |
|
|
742 |
if (pEnc->bframenum_head > 0) { |
if (pEnc->bframenum_head > 0) { |
743 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
744 |
|
|
745 |
|
/* write an empty marker to the bitstream. |
746 |
|
|
747 |
|
for divx5 decoder compatibility, this marker must consist |
748 |
|
of a not-coded p-vop, with a time_base of zero, and time_increment |
749 |
|
indentical to the future-referece frame. |
750 |
|
*/ |
751 |
|
|
752 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
753 |
|
int tmp; |
754 |
|
|
755 |
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", |
756 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
757 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
758 |
|
|
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
|
759 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
760 |
BitstreamPutBits(&bs, 0x7f, 8); |
|
761 |
|
tmp = pEnc->current->seconds; |
762 |
|
pEnc->current->seconds = 0; /* force time_base = 0 */ |
763 |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
764 |
|
pEnc->current->seconds = tmp; |
765 |
|
|
766 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
767 |
pFrame->intra = 0; |
pFrame->intra = 0; |
823 |
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
824 |
pEnc->queue_size--; |
pEnc->queue_size--; |
825 |
|
|
826 |
} else if (BitstreamPos(&bs) == 0) { |
} else { |
827 |
|
|
828 |
|
/* if nothing was encoded, write an 'ignore this frame' flag |
829 |
|
to the bitstream */ |
830 |
|
|
831 |
|
if (BitstreamPos(&bs) == 0) { |
832 |
|
|
833 |
DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
834 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
835 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
836 |
|
|
|
pFrame->intra = 0; |
|
|
|
|
837 |
BitstreamPutBits(&bs, 0x7f, 8); |
BitstreamPutBits(&bs, 0x7f, 8); |
838 |
BitstreamPad(&bs); |
pFrame->intra = 0; |
839 |
pFrame->length = BitstreamLength(&bs); |
} |
|
|
|
|
return XVID_ERR_OK; |
|
|
|
|
|
} else { |
|
840 |
|
|
841 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
842 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
844 |
|
|
845 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
846 |
|
|
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
* Well there was a separation here so i put it in ANSI C |
|
|
* comment style :-) |
|
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
|
|
|
|
847 |
emms(); |
emms(); |
848 |
|
|
849 |
// 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 |
854 |
else |
else |
855 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
856 |
|
|
857 |
if (pEnc->current->quant < 1) |
/* if (pEnc->current->quant < 1) |
858 |
pEnc->current->quant = 1; |
pEnc->current->quant = 1; |
859 |
|
|
860 |
if (pEnc->current->quant > 31) |
if (pEnc->current->quant > 31) |
861 |
pEnc->current->quant = 31; |
pEnc->current->quant = 31; |
862 |
|
*/ |
863 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
864 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
865 |
|
|
867 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
868 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
869 |
|
|
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
|
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
|
|
|
870 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
871 |
|
|
872 |
#ifdef _DEBUG_PSNR |
#ifdef _DEBUG_PSNR |
878 |
|
|
879 |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
880 |
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, |
881 |
"%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); |
882 |
} |
} |
883 |
|
|
884 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
925 |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
926 |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
927 |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
928 |
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
|| /*image_mad(&pEnc->reference->image, &pEnc->current->image, |
929 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
930 |
pEnc->mbParam.height) > 30) { |
pEnc->mbParam.height) > 30) {*/ |
931 |
|
2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image, |
932 |
|
&pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) { |
933 |
|
|
934 |
/* |
/* |
935 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
936 |
*/ |
*/ |
969 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
970 |
|
|
971 |
if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
972 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
973 |
input_valid = 0; |
input_valid = 0; |
974 |
goto ipvop_loop; |
goto ipvop_loop; |
975 |
} |
} |
978 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
979 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
980 |
*/ |
*/ |
981 |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
982 |
/* |
/* |
983 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
984 |
*/ |
*/ |
996 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
997 |
|
|
998 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
999 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1000 |
input_valid = 0; |
input_valid = 0; |
1001 |
goto ipvop_loop; |
goto ipvop_loop; |
1002 |
} |
} |
1006 |
* This will be coded as a Bidirectional Frame |
* This will be coded as a Bidirectional Frame |
1007 |
*/ |
*/ |
1008 |
|
|
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
|
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
|
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
|
|
|
|
1009 |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
1010 |
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"); |
1011 |
} |
} |
1017 |
} else { |
} else { |
1018 |
pEnc->current->quant = pFrame->bquant; |
pEnc->current->quant = pFrame->bquant; |
1019 |
} |
} |
1020 |
|
if (pEnc->current->quant < 1) |
1021 |
|
pEnc->current->quant = 1; |
1022 |
|
|
1023 |
|
if (pEnc->current->quant > 31) |
1024 |
|
pEnc->current->quant = 31; |
1025 |
|
|
1026 |
|
|
1027 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
1028 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1029 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1030 |
|
|
1031 |
|
|
1032 |
|
|
1033 |
/* store frame into bframe buffer & swap ref back to current */ |
/* store frame into bframe buffer & swap ref back to current */ |
1034 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1043 |
goto bvop_loop; |
goto bvop_loop; |
1044 |
} |
} |
1045 |
|
|
1046 |
|
pEnc->iFrameNum++; |
1047 |
|
|
1048 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1049 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1050 |
|
|
1073 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1074 |
} |
} |
1075 |
|
|
|
|
|
1076 |
stop_global_timer(); |
stop_global_timer(); |
1077 |
write_timer(); |
write_timer(); |
1078 |
|
|
1079 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1080 |
} |
} |
1081 |
|
|
|
#endif |
|
|
|
|
1082 |
|
|
1083 |
|
|
1084 |
/***************************************************************************** |
/***************************************************************************** |
1116 |
|
|
1117 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1118 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
|
#ifdef BFRAMES |
|
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
|
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
|
#endif |
|
1119 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1120 |
|
|
1121 |
|
inc_frame_num(pEnc); |
1122 |
|
|
1123 |
|
/* disable alternate scan flag if interlacing is not enabled */ |
1124 |
|
if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) && |
1125 |
|
!(pEnc->current->global_flags & XVID_INTERLACING)) |
1126 |
|
{ |
1127 |
|
pEnc->current->global_flags -= XVID_ALTERNATESCAN; |
1128 |
|
} |
1129 |
|
|
1130 |
start_timer(); |
start_timer(); |
1131 |
if (image_input |
if (image_input |
1132 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
1149 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
1150 |
} |
} |
1151 |
|
|
1152 |
|
if ((pEnc->current->global_flags & XVID_QUARTERPEL)) |
1153 |
|
pEnc->mbParam.m_quarterpel = 1; |
1154 |
|
else |
1155 |
|
pEnc->mbParam.m_quarterpel = 0; |
1156 |
|
|
1157 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1158 |
int *temp_dquants = |
int *temp_dquants = |
1159 |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
1259 |
DEBUG(temp); |
DEBUG(temp); |
1260 |
#endif |
#endif |
1261 |
|
|
|
#ifdef BFRAMES |
|
|
inc_frame_num(pEnc); |
|
|
#else |
|
1262 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
|
#endif |
|
|
|
|
1263 |
|
|
1264 |
stop_global_timer(); |
stop_global_timer(); |
1265 |
write_timer(); |
write_timer(); |
1507 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
1508 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1509 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1510 |
|
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1511 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1512 |
|
|
1513 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1514 |
#ifdef BFRAMES |
|
1515 |
#define DIVX501B481P "DivX501b481p" |
#define DIVX501B481P "DivX501b481p" |
1516 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
1517 |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1518 |
} |
} |
1519 |
#endif |
|
1520 |
|
#define XVID_ID "XviD" XVID_BS_VERSION |
1521 |
|
BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID)); |
1522 |
|
|
1523 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1524 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1525 |
|
|
1526 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1544 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1545 |
|
|
1546 |
start_timer(); |
start_timer(); |
1547 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1548 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1549 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1550 |
|
qcoeff[5*64+0]=0; |
1551 |
|
} |
1552 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1553 |
stop_coding_timer(); |
stop_coding_timer(); |
1554 |
} |
} |
1570 |
|
|
1571 |
|
|
1572 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1573 |
|
#define BFRAME_SKIP_THRESHHOLD 30 |
1574 |
|
|
1575 |
static int |
static int |
1576 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1585 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1586 |
|
|
1587 |
int iLimit; |
int iLimit; |
1588 |
uint32_t x, y; |
int x, y, k; |
1589 |
int iSearchRange; |
int iSearchRange; |
1590 |
int bIntra; |
int bIntra, skip_possible; |
1591 |
|
|
1592 |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
1593 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
1594 |
|
|
1595 |
start_timer(); |
start_timer(); |
1596 |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1597 |
pEnc->mbParam.width, pEnc->mbParam.height, |
pEnc->mbParam.width, pEnc->mbParam.height); |
|
pEnc->current->global_flags & XVID_INTERLACING); |
|
1598 |
stop_edges_timer(); |
stop_edges_timer(); |
1599 |
|
|
1600 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1601 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1602 |
|
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1603 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1604 |
|
|
1605 |
if (!force_inter) |
if (!force_inter) |
1614 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1615 |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
1616 |
pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_height, |
1617 |
|
pEnc->mbParam.m_quarterpel, |
1618 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1619 |
stop_inter_timer(); |
stop_inter_timer(); |
1620 |
} |
} |
1621 |
|
|
1622 |
|
if (pEnc->current->global_flags & XVID_GMC) { |
1623 |
|
// printf("Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel); |
1624 |
|
pEnc->current->coding_type = S_VOP; |
1625 |
|
} else |
1626 |
|
pEnc->current->coding_type = P_VOP; |
1627 |
|
|
1628 |
start_timer(); |
start_timer(); |
1629 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
1630 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1631 |
} else { |
if (bIntra == 0) { |
1632 |
|
pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current); |
1633 |
|
MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1634 |
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
1635 |
|
} |
1636 |
|
|
1637 |
#ifdef _SMP |
} else { |
|
if (NUMTHREADS > 1) |
|
|
bIntra = |
|
|
SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
|
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
|
iLimit); |
|
|
else |
|
|
#endif |
|
1638 |
|
|
1639 |
bIntra = |
bIntra = |
1640 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1641 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1642 |
iLimit); |
iLimit); |
|
|
|
|
|
|
1643 |
} |
} |
1644 |
stop_motion_timer(); |
stop_motion_timer(); |
1645 |
|
|
1646 |
if (bIntra == 1) { |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
1647 |
return FrameCodeI(pEnc, bs, pBits); |
|
1648 |
} |
if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) ) |
1649 |
|
pEnc->current->coding_type = P_VOP; /* no global motion -> no GMC */ |
1650 |
|
|
|
pEnc->current->coding_type = P_VOP; |
|
1651 |
|
|
1652 |
if (vol_header) |
if (vol_header) |
1653 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1654 |
|
|
1655 |
|
|
1656 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1657 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1658 |
|
|
1659 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1660 |
|
|
1661 |
pEnc->sStat.iTextBits = 0; |
pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount = |
|
pEnc->sStat.iMvSum = 0; |
|
|
pEnc->sStat.iMvCount = 0; |
|
1662 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1663 |
|
|
1664 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1676 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pEnc->mbParam.width, |
1677 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1678 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1679 |
|
pEnc->mbParam.m_quarterpel, |
1680 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1681 |
stop_comp_timer(); |
stop_comp_timer(); |
1682 |
|
|
1694 |
|
|
1695 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1696 |
|
|
1697 |
|
if (pMB->mode != MODE_NOT_CODED) |
1698 |
pMB->cbp = |
pMB->cbp = |
1699 |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1700 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1719 |
} |
} |
1720 |
|
|
1721 |
start_timer(); |
start_timer(); |
1722 |
|
|
1723 |
|
/* Finished processing the MB, now check if to CODE or SKIP */ |
1724 |
|
|
1725 |
|
skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) & |
1726 |
|
(pMB->dquant == NO_CHANGE); |
1727 |
|
|
1728 |
|
if(pEnc->mbParam.m_quarterpel) |
1729 |
|
{ skip_possible &= (pMB->qmvs[0].x == pEnc->current->GMC_MV.x) & (pMB->qmvs[0].y == pEnc->current->GMC_MV.y); |
1730 |
|
} |
1731 |
|
else |
1732 |
|
{ skip_possible &= (pMB->mvs[0].x == pEnc->current->GMC_MV.x) & (pMB->mvs[0].y == pEnc->current->GMC_MV.y); |
1733 |
|
} |
1734 |
|
|
1735 |
|
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
1736 |
|
|
1737 |
|
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
1738 |
|
int bSkip = 1; |
1739 |
|
|
1740 |
|
if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
1741 |
|
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1742 |
|
{ |
1743 |
|
int iSAD; |
1744 |
|
iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1745 |
|
pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1746 |
|
pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); |
1747 |
|
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) |
1748 |
|
{ bSkip = 0; |
1749 |
|
break; |
1750 |
|
} |
1751 |
|
} |
1752 |
|
|
1753 |
|
if (!bSkip) |
1754 |
|
{ |
1755 |
|
VECTOR predMV; |
1756 |
|
if(pEnc->mbParam.m_quarterpel) { |
1757 |
|
predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1758 |
|
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; /* with GMC, qmvs doesn't have to be (0,0)! */ |
1759 |
|
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1760 |
|
} |
1761 |
|
else { |
1762 |
|
predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1763 |
|
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; /* with GMC, mvs doesn't have to be (0,0)! */ |
1764 |
|
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
1765 |
|
} |
1766 |
|
pMB->mode = MODE_INTER; |
1767 |
|
pMB->cbp = 0; |
1768 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1769 |
|
} |
1770 |
|
else |
1771 |
|
{ |
1772 |
|
pMB->mode = MODE_NOT_CODED; |
1773 |
|
MBSkip(bs); |
1774 |
|
} |
1775 |
|
|
1776 |
|
} else { |
1777 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1778 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1779 |
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1780 |
|
qcoeff[5*64+0]=0; |
1781 |
|
} |
1782 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1783 |
|
} |
1784 |
|
|
1785 |
stop_coding_timer(); |
stop_coding_timer(); |
1786 |
} |
} |
1787 |
} |
} |
1800 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
1801 |
|
|
1802 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
1803 |
&& (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 |
&& (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128 |
1804 |
{ |
{ |
1805 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1806 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1807 |
} else if ((fSigma < iSearchRange / 6) |
} else if ((fSigma < iSearchRange / 6) |
1808 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
1809 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
1810 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16 |
1811 |
{ |
{ |
1812 |
pEnc->mbParam.m_fcode--; |
pEnc->mbParam.m_fcode--; |
1813 |
iSearchRange /= 2; |
iSearchRange /= 2; |
1815 |
|
|
1816 |
pEnc->sStat.fMvPrevSigma = fSigma; |
pEnc->sStat.fMvPrevSigma = fSigma; |
1817 |
|
|
1818 |
|
#ifdef FRAMEDROP |
1819 |
|
/* frame drop code */ |
1820 |
|
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks); |
1821 |
|
if (pEnc->sStat.kblks + pEnc->sStat.mblks < |
1822 |
|
(pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100) |
1823 |
|
{ |
1824 |
|
pEnc->sStat.kblks = pEnc->sStat.mblks = 0; |
1825 |
|
pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
1826 |
|
|
1827 |
|
BitstreamReset(bs); |
1828 |
|
|
1829 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1830 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
1831 |
|
|
1832 |
|
// copy reference frame details into the current frame |
1833 |
|
pEnc->current->quant = pEnc->reference->quant; |
1834 |
|
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
1835 |
|
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
1836 |
|
pEnc->current->quarterpel = pEnc->reference->quarterpel; |
1837 |
|
pEnc->current->fcode = pEnc->reference->fcode; |
1838 |
|
pEnc->current->bcode = pEnc->reference->bcode; |
1839 |
|
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1840 |
|
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
1841 |
|
|
1842 |
|
} |
1843 |
|
#endif |
1844 |
|
|
1845 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1846 |
|
|
1847 |
return 0; // inter |
return 0; // inter |
1848 |
} |
} |
1849 |
|
|
1850 |
|
|
1851 |
#ifdef BFRAMES |
static __inline void |
|
static void |
|
1852 |
FrameCodeB(Encoder * pEnc, |
FrameCodeB(Encoder * pEnc, |
1853 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
1854 |
Bitstream * bs, |
Bitstream * bs, |
1857 |
int16_t dct_codes[6 * 64]; |
int16_t dct_codes[6 * 64]; |
1858 |
int16_t qcoeff[6 * 64]; |
int16_t qcoeff[6 * 64]; |
1859 |
uint32_t x, y; |
uint32_t x, y; |
|
VECTOR forward; |
|
|
VECTOR backward; |
|
1860 |
|
|
1861 |
IMAGE *f_ref = &pEnc->reference->image; |
IMAGE *f_ref = &pEnc->reference->image; |
1862 |
IMAGE *b_ref = &pEnc->current->image; |
IMAGE *b_ref = &pEnc->current->image; |
1873 |
} |
} |
1874 |
#endif |
#endif |
1875 |
|
|
1876 |
|
frame->quarterpel = pEnc->mbParam.m_quarterpel; |
1877 |
|
|
1878 |
// forward |
// forward |
1879 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1880 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1881 |
pEnc->mbParam.height, |
pEnc->mbParam.height); |
|
frame->global_flags & XVID_INTERLACING); |
|
1882 |
start_timer(); |
start_timer(); |
1883 |
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, |
1884 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1885 |
0); |
pEnc->mbParam.m_quarterpel, 0); |
1886 |
stop_inter_timer(); |
stop_inter_timer(); |
1887 |
|
|
1888 |
// backward |
// backward |
1889 |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1890 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1891 |
pEnc->mbParam.height, |
pEnc->mbParam.height); |
|
frame->global_flags & XVID_INTERLACING); |
|
1892 |
start_timer(); |
start_timer(); |
1893 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1894 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1895 |
0); |
pEnc->mbParam.m_quarterpel, 0); |
1896 |
stop_inter_timer(); |
stop_inter_timer(); |
1897 |
|
|
1898 |
start_timer(); |
start_timer(); |
1899 |
MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref, |
|
1900 |
|
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1901 |
|
((int32_t)(pEnc->current->stamp - frame->stamp)), // time_bp |
1902 |
|
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), // time_pp |
1903 |
|
pEnc->reference->mbs, f_ref, |
1904 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1905 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
1906 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
1914 |
} */ |
} */ |
1915 |
|
|
1916 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1917 |
|
|
1918 |
|
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
1919 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
1920 |
|
|
1921 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1927 |
|
|
1928 |
|
|
1929 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
|
// reset prediction |
|
|
|
|
|
forward.x = 0; |
|
|
forward.y = 0; |
|
|
backward.x = 0; |
|
|
backward.y = 0; |
|
|
|
|
1930 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1931 |
MACROBLOCK *f_mb = |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
1932 |
&pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0; |
|
MACROBLOCK *b_mb = |
|
|
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
|
MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1933 |
|
|
1934 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
1935 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1936 |
mb->mvs[0].x = 0; |
//mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; |
|
mb->mvs[0].y = 0; |
|
|
|
|
|
mb->cbp = 0; |
|
|
#ifdef BFRAMES_DEC_DEBUG |
|
|
BFRAME_DEBUG |
|
|
#endif |
|
1937 |
continue; |
continue; |
1938 |
} |
} |
1939 |
|
|
1940 |
|
if (mb->mode != MODE_DIRECT_NONE_MV) { |
1941 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1942 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1943 |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1944 |
&pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterV, &pEnc->vInterHV, |
1945 |
dct_codes); |
dct_codes); |
1946 |
|
|
1947 |
|
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
1948 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
|
mb->cbp = |
|
|
MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, |
|
|
qcoeff); |
|
|
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
|
|
|
|
1949 |
|
|
1950 |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) |
mb->cbp = |
1951 |
&& mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) { |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); |
|
mb->mode = 5; // skipped |
|
|
} |
|
1952 |
|
|
1953 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
1954 |
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
1955 |
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
mb->mode = MODE_DIRECT_NONE_MV; // skipped |
|
forward.x = mb->mvs[0].x; |
|
|
forward.y = mb->mvs[0].y; |
|
1956 |
} |
} |
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
|
|
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
|
|
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
|
|
backward.x = mb->b_mvs[0].x; |
|
|
backward.y = mb->b_mvs[0].y; |
|
1957 |
} |
} |
|
// DPRINTF("%05i : [%i %i] M=%i CBP=%i MVS=%i,%i forward=%i,%i", pEnc->m_framenum, x, y, mb->mode, mb->cbp, mb->mvs[0].x, mb->mvs[0].y, forward.x, forward.y); |
|
1958 |
|
|
1959 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |
1960 |
BFRAME_DEBUG |
BFRAME_DEBUG |
1961 |
#endif |
#endif |
1962 |
start_timer(); |
start_timer(); |
1963 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1964 |
&pEnc->sStat); |
&pEnc->sStat, direction); |
1965 |
stop_coding_timer(); |
stop_coding_timer(); |
1966 |
} |
} |
1967 |
} |
} |
1980 |
} |
} |
1981 |
#endif |
#endif |
1982 |
} |
} |
1983 |
#endif |
|
1984 |
|
|
1985 |
|
/* in case internal output is needed somewhere... */ |
1986 |
|
/* { |
1987 |
|
FILE *filehandle; |
1988 |
|
filehandle=fopen("last-b.pgm","wb"); |
1989 |
|
if (filehandle) |
1990 |
|
{ |
1991 |
|
fprintf(filehandle,"P5\n\n"); // |
1992 |
|
fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height); |
1993 |
|
fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle); |
1994 |
|
fclose(filehandle); |
1995 |
|
} |
1996 |
|
} |
1997 |
|
*/ |