32 |
* |
* |
33 |
* History |
* History |
34 |
* |
* |
35 |
|
* 10.07.2002 added BFRAMES_DEC_DEBUG support |
36 |
|
* MinChen <chenm001@163.com> |
37 |
|
* 20.06.2002 bframe patch |
38 |
* 08.05.2002 fix some problem in DEBUG mode; |
* 08.05.2002 fix some problem in DEBUG mode; |
39 |
* MinChen <chenm001@163.com> |
* MinChen <chenm001@163.com> |
40 |
* 14.04.2002 added FrameCodeB() |
* 14.04.2002 added FrameCodeB() |
46 |
#include <stdlib.h> |
#include <stdlib.h> |
47 |
#include <stdio.h> |
#include <stdio.h> |
48 |
#include <math.h> |
#include <math.h> |
49 |
|
#include <string.h> |
50 |
|
|
51 |
#include "encoder.h" |
#include "encoder.h" |
52 |
#include "prediction/mbprediction.h" |
#include "prediction/mbprediction.h" |
53 |
#include "global.h" |
#include "global.h" |
54 |
#include "utils/timer.h" |
#include "utils/timer.h" |
55 |
#include "image/image.h" |
#include "image/image.h" |
56 |
|
#ifdef BFRAMES |
57 |
|
#include "image/font.h" |
58 |
|
#endif |
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 |
|
|
71 |
|
#ifdef _SMP |
72 |
|
#include "motion/smp_motion_est.h" |
73 |
|
#endif |
74 |
/***************************************************************************** |
/***************************************************************************** |
75 |
* Local macros |
* Local macros |
76 |
****************************************************************************/ |
****************************************************************************/ |
141 |
encoder_create(XVID_ENC_PARAM * pParam) |
encoder_create(XVID_ENC_PARAM * pParam) |
142 |
{ |
{ |
143 |
Encoder *pEnc; |
Encoder *pEnc; |
144 |
uint32_t i; |
int i; |
145 |
|
|
146 |
pParam->handle = NULL; |
pParam->handle = NULL; |
147 |
|
|
209 |
|
|
210 |
/* 1 keyframe each 10 seconds */ |
/* 1 keyframe each 10 seconds */ |
211 |
|
|
212 |
if (pParam->max_key_interval == 0) |
if (pParam->max_key_interval <= 0) |
213 |
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
214 |
|
|
|
|
|
215 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
216 |
if (pEnc == NULL) |
if (pEnc == NULL) |
217 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
218 |
|
|
219 |
|
/* Zero the Encoder Structure */ |
220 |
|
|
221 |
|
memset(pEnc, 0, sizeof(Encoder)); |
222 |
|
|
223 |
/* Fill members of Encoder structure */ |
/* Fill members of Encoder structure */ |
224 |
|
|
225 |
pEnc->mbParam.width = pParam->width; |
pEnc->mbParam.width = pParam->width; |
234 |
pEnc->mbParam.fbase = pParam->fbase; |
pEnc->mbParam.fbase = pParam->fbase; |
235 |
pEnc->mbParam.fincr = pParam->fincr; |
pEnc->mbParam.fincr = pParam->fincr; |
236 |
|
|
237 |
|
pEnc->mbParam.m_quant_type = H263_QUANT; |
238 |
|
|
239 |
|
#ifdef _SMP |
240 |
|
pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS); |
241 |
|
#endif |
242 |
|
|
243 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->sStat.fMvPrevSigma = -1; |
244 |
|
|
245 |
/* Fill rate control parameters */ |
/* Fill rate control parameters */ |
271 |
|
|
272 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
273 |
|
|
274 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
275 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
276 |
#endif |
#endif |
277 |
#ifdef BFRAMES |
#ifdef BFRAMES |
287 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
288 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
289 |
|
|
290 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
291 |
if (image_create |
if (image_create |
292 |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
293 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
341 |
/* B Frames specific init */ |
/* B Frames specific init */ |
342 |
#ifdef BFRAMES |
#ifdef BFRAMES |
343 |
|
|
344 |
|
pEnc->global = pParam->global; |
345 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
346 |
pEnc->bquant_ratio = pParam->bquant_ratio; |
pEnc->bquant_ratio = pParam->bquant_ratio; |
347 |
|
pEnc->frame_drop_ratio = pParam->frame_drop_ratio; |
348 |
pEnc->bframes = NULL; |
pEnc->bframes = NULL; |
349 |
|
|
350 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
387 |
pEnc->bframenum_head = 0; |
pEnc->bframenum_head = 0; |
388 |
pEnc->bframenum_tail = 0; |
pEnc->bframenum_tail = 0; |
389 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
390 |
|
pEnc->bframenum_dx50bvop = -1; |
391 |
|
|
392 |
|
pEnc->queue = NULL; |
393 |
|
|
394 |
|
|
395 |
|
if (pEnc->mbParam.max_bframes > 0) { |
396 |
|
int n; |
397 |
|
|
398 |
|
pEnc->queue = |
399 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE), |
400 |
|
CACHE_LINE); |
401 |
|
|
402 |
|
if (pEnc->queue == NULL) |
403 |
|
goto xvid_err_memory4; |
404 |
|
|
405 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
406 |
|
image_null(&pEnc->queue[n]); |
407 |
|
|
408 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
409 |
|
if (image_create |
410 |
|
(&pEnc->queue[n], pEnc->mbParam.edged_width, |
411 |
|
pEnc->mbParam.edged_height) < 0) |
412 |
|
goto xvid_err_memory5; |
413 |
|
|
414 |
|
} |
415 |
|
} |
416 |
|
|
417 |
|
pEnc->queue_head = 0; |
418 |
|
pEnc->queue_tail = 0; |
419 |
|
pEnc->queue_size = 0; |
420 |
|
|
421 |
|
|
422 |
pEnc->mbParam.m_seconds = 0; |
pEnc->mbParam.m_seconds = 0; |
423 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
424 |
|
pEnc->m_framenum = 0; |
425 |
|
pEnc->last_pframe = 1; |
426 |
#endif |
#endif |
427 |
|
|
428 |
pParam->handle = (void *) pEnc; |
pParam->handle = (void *) pEnc; |
443 |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
444 |
*/ |
*/ |
445 |
#ifdef BFRAMES |
#ifdef BFRAMES |
446 |
|
xvid_err_memory5: |
447 |
|
|
448 |
|
|
449 |
|
if (pEnc->mbParam.max_bframes > 0) { |
450 |
|
|
451 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
452 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
453 |
|
pEnc->mbParam.edged_height); |
454 |
|
} |
455 |
|
xvid_free(pEnc->queue); |
456 |
|
} |
457 |
|
|
458 |
xvid_err_memory4: |
xvid_err_memory4: |
459 |
|
|
460 |
|
if (pEnc->mbParam.max_bframes > 0) { |
461 |
|
|
462 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
463 |
|
|
464 |
if (pEnc->bframes[i] == NULL) |
if (pEnc->bframes[i] == NULL) |
474 |
} |
} |
475 |
|
|
476 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
477 |
|
} |
478 |
|
|
479 |
#endif |
#endif |
480 |
|
|
481 |
xvid_err_memory3: |
xvid_err_memory3: |
482 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
483 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
484 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
485 |
#endif |
#endif |
536 |
int |
int |
537 |
encoder_destroy(Encoder * pEnc) |
encoder_destroy(Encoder * pEnc) |
538 |
{ |
{ |
539 |
|
#ifdef BFRAMES |
540 |
|
int i; |
541 |
|
#endif |
542 |
|
|
543 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
544 |
|
|
545 |
/* B Frames specific */ |
/* B Frames specific */ |
546 |
#ifdef BFRAMES |
#ifdef BFRAMES |
547 |
int i; |
if (pEnc->mbParam.max_bframes > 0) { |
548 |
|
|
549 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
550 |
|
|
551 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
552 |
|
pEnc->mbParam.edged_height); |
553 |
|
} |
554 |
|
xvid_free(pEnc->queue); |
555 |
|
} |
556 |
|
|
557 |
|
|
558 |
|
if (pEnc->mbParam.max_bframes > 0) { |
559 |
|
|
560 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
561 |
|
|
568 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
569 |
|
|
570 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
|
|
|
571 |
} |
} |
572 |
|
|
573 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
574 |
|
|
575 |
|
} |
576 |
#endif |
#endif |
577 |
|
|
578 |
/* All images, reference, current etc ... */ |
/* All images, reference, current etc ... */ |
599 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
600 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
601 |
#endif |
#endif |
602 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
603 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
604 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
605 |
#endif |
#endif |
617 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
618 |
} |
} |
619 |
|
|
620 |
|
|
621 |
|
#ifdef BFRAMES |
622 |
|
void inc_frame_num(Encoder * pEnc) |
623 |
|
{ |
624 |
|
pEnc->iFrameNum++; |
625 |
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
626 |
|
|
627 |
|
pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
628 |
|
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
629 |
|
} |
630 |
|
#endif |
631 |
|
|
632 |
|
|
633 |
|
#ifdef BFRAMES |
634 |
|
void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
635 |
|
{ |
636 |
|
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
637 |
|
{ |
638 |
|
DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL"); |
639 |
|
return; |
640 |
|
} |
641 |
|
|
642 |
|
DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
643 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
644 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
645 |
|
|
646 |
|
|
647 |
|
start_timer(); |
648 |
|
if (image_input |
649 |
|
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
650 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
651 |
|
return; |
652 |
|
stop_conv_timer(); |
653 |
|
|
654 |
|
pEnc->queue_size++; |
655 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
656 |
|
} |
657 |
|
#endif |
658 |
|
|
659 |
|
|
660 |
|
#ifdef BFRAMES |
661 |
/***************************************************************************** |
/***************************************************************************** |
662 |
* Frame encoder entry point |
* IPB frame encoder entry point |
|
* |
|
|
* At this moment 2 versions coexist : one for IPB compatible encoder, |
|
|
* another one for the old IP encoder. |
|
663 |
* |
* |
664 |
* Returned values : |
* Returned values : |
665 |
* - XVID_ERR_OK - no errors |
* - XVID_ERR_OK - no errors |
667 |
* format |
* format |
668 |
****************************************************************************/ |
****************************************************************************/ |
669 |
|
|
|
|
|
|
#ifdef BFRAMES |
|
|
/***************************************************************************** |
|
|
* Frame encoder entry point for IPB capable encoder |
|
|
****************************************************************************/ |
|
670 |
int |
int |
671 |
encoder_encode(Encoder * pEnc, |
encoder_encode_bframes(Encoder * pEnc, |
672 |
XVID_ENC_FRAME * pFrame, |
XVID_ENC_FRAME * pFrame, |
673 |
XVID_ENC_STATS * pResult) |
XVID_ENC_STATS * pResult) |
674 |
{ |
{ |
676 |
Bitstream bs; |
Bitstream bs; |
677 |
uint32_t bits; |
uint32_t bits; |
678 |
|
|
679 |
#ifdef _DEBUG |
int input_valid = 1; |
680 |
|
|
681 |
|
#ifdef _DEBUG_PSNR |
682 |
float psnr; |
float psnr; |
683 |
char temp[128]; |
char temp[128]; |
684 |
#endif |
#endif |
685 |
|
|
686 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
687 |
ENC_CHECK(pFrame); |
ENC_CHECK(pFrame); |
688 |
|
ENC_CHECK(pFrame->image); |
689 |
|
|
690 |
start_global_timer(); |
start_global_timer(); |
691 |
|
|
692 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
693 |
|
|
694 |
|
ipvop_loop: |
695 |
|
|
696 |
/* |
/* |
697 |
* bframe "flush" code |
* bframe "flush" code |
698 |
*/ |
*/ |
707 |
* frame as a pframe |
* frame as a pframe |
708 |
*/ |
*/ |
709 |
|
|
710 |
/* ToDo : remove dprintf calls */ |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
711 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
712 |
dprintf("--- PFRAME (final frame correction) --- "); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
713 |
*/ |
|
714 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
715 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
716 |
|
|
720 |
|
|
721 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
722 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
723 |
pFrame->intra = 0; |
pFrame->intra = 0; |
724 |
|
|
725 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
726 |
} |
} |
727 |
|
|
728 |
/* ToDo : remove dprintf calls */ |
|
729 |
/* |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
730 |
dprintf("--- BFRAME (flush) --- "); |
pEnc->bframenum_head, pEnc->bframenum_tail, |
731 |
*/ |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
732 |
|
|
733 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
734 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
735 |
|
|
|
|
|
736 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
737 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
738 |
pFrame->intra = 0; |
pFrame->intra = 0; |
739 |
|
|
740 |
|
if (input_valid) |
741 |
|
queue_image(pEnc, pFrame); |
742 |
|
|
743 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
744 |
} |
} |
745 |
|
|
746 |
if (pFrame->image == NULL) { |
if (pEnc->bframenum_head > 0) { |
747 |
pFrame->length = 0; |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
748 |
pFrame->input_consumed = 1; |
|
749 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
750 |
|
|
751 |
|
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
752 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
753 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
754 |
|
|
755 |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
756 |
|
BitstreamPad(&bs); |
757 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
758 |
|
|
759 |
|
pFrame->length = BitstreamLength(&bs); |
760 |
pFrame->intra = 0; |
pFrame->intra = 0; |
761 |
|
|
762 |
|
if (input_valid) |
763 |
|
queue_image(pEnc, pFrame); |
764 |
|
|
765 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
766 |
} |
} |
767 |
|
} |
768 |
|
|
769 |
if (pEnc->bframenum_head > 0) { |
|
770 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
bvop_loop: |
771 |
|
|
772 |
|
if (pEnc->bframenum_dx50bvop != -1) |
773 |
|
{ |
774 |
|
|
775 |
|
SWAP(pEnc->current, pEnc->reference); |
776 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
777 |
|
|
778 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
779 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
780 |
|
} |
781 |
|
|
782 |
|
if (input_valid) |
783 |
|
{ |
784 |
|
queue_image(pEnc, pFrame); |
785 |
|
input_valid = 0; |
786 |
|
} |
787 |
|
|
788 |
|
} else if (input_valid) { |
789 |
|
|
790 |
|
SWAP(pEnc->current, pEnc->reference); |
791 |
|
|
792 |
|
start_timer(); |
793 |
|
if (image_input |
794 |
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
795 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
796 |
|
return XVID_ERR_FORMAT; |
797 |
|
stop_conv_timer(); |
798 |
|
|
799 |
|
// queue input frame, and dequue next image |
800 |
|
if (pEnc->queue_size > 0) |
801 |
|
{ |
802 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); |
803 |
|
if (pEnc->queue_head != pEnc->queue_tail) |
804 |
|
{ |
805 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
806 |
|
} |
807 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
808 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
809 |
|
} |
810 |
|
|
811 |
|
} else if (pEnc->queue_size > 0) { |
812 |
|
|
813 |
|
SWAP(pEnc->current, pEnc->reference); |
814 |
|
|
815 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
816 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
817 |
|
pEnc->queue_size--; |
818 |
|
|
819 |
|
} else if (BitstreamPos(&bs) == 0) { |
820 |
|
|
821 |
|
DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
822 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
823 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
824 |
|
|
825 |
|
pFrame->intra = 0; |
826 |
|
|
827 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
828 |
|
BitstreamPad(&bs); |
829 |
|
pFrame->length = BitstreamLength(&bs); |
830 |
|
|
831 |
|
return XVID_ERR_OK; |
832 |
|
|
833 |
|
} else { |
834 |
|
|
835 |
|
pFrame->length = BitstreamLength(&bs); |
836 |
|
return XVID_ERR_OK; |
837 |
} |
} |
838 |
|
|
839 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
843 |
* comment style :-) |
* comment style :-) |
844 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
845 |
|
|
846 |
SWAP(pEnc->current, pEnc->reference); |
emms(); |
|
|
|
|
EMMS(); |
|
847 |
|
|
848 |
|
// only inc frame num, adapt quant, etc. if we havent seen it before |
849 |
|
if (pEnc->bframenum_dx50bvop < 0 ) |
850 |
|
{ |
851 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
852 |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
853 |
else |
else |
854 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
855 |
|
|
856 |
if (pEnc->current->quant < 1) |
/* if (pEnc->current->quant < 1) |
857 |
pEnc->current->quant = 1; |
pEnc->current->quant = 1; |
858 |
|
|
859 |
if (pEnc->current->quant > 31) |
if (pEnc->current->quant > 31) |
860 |
pEnc->current->quant = 31; |
pEnc->current->quant = 31; |
861 |
|
*/ |
862 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
863 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
864 |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
|
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
865 |
/* ToDo : dynamic fcode (in both directions) */ |
/* ToDo : dynamic fcode (in both directions) */ |
866 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
867 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
868 |
|
|
869 |
start_timer(); |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
870 |
if (image_input |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
871 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
|
872 |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
inc_frame_num(pEnc); |
|
return XVID_ERR_FORMAT; |
|
|
stop_conv_timer(); |
|
873 |
|
|
874 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
875 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
876 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
877 |
#endif |
#endif |
878 |
|
|
879 |
EMMS(); |
emms(); |
880 |
|
|
881 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
882 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
883 |
|
"%i if:%i st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks); |
884 |
|
} |
885 |
|
|
886 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
887 |
* Luminance masking |
* Luminance masking |
912 |
} |
} |
913 |
|
|
914 |
#undef OFFSET |
#undef OFFSET |
|
|
|
915 |
} |
} |
916 |
|
|
917 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
918 |
} |
} |
919 |
|
|
920 |
|
} |
921 |
|
|
922 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
923 |
* ivop/pvop/bvop selection |
* ivop/pvop/bvop selection |
924 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
925 |
|
|
926 |
|
|
927 |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
928 |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
929 |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
930 |
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
934 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
935 |
*/ |
*/ |
936 |
|
|
937 |
/* ToDo : Remove dprintf calls */ |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
938 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
939 |
dprintf("--- IFRAME ---"); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
|
*/ |
|
940 |
|
|
941 |
FrameCodeI(pEnc, &bs, &bits); |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
942 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
943 |
|
} |
944 |
|
|
945 |
|
// when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe |
946 |
|
|
947 |
|
if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
948 |
|
|
949 |
|
pEnc->bframenum_tail--; |
950 |
|
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
951 |
|
|
952 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
953 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
954 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
955 |
|
} |
956 |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
957 |
|
|
958 |
|
pFrame->intra = 0; |
959 |
|
|
960 |
|
} else { |
961 |
|
|
962 |
|
FrameCodeI(pEnc, &bs, &bits); |
963 |
pFrame->intra = 1; |
pFrame->intra = 1; |
964 |
|
|
965 |
|
pEnc->bframenum_dx50bvop = -1; |
966 |
|
} |
967 |
|
|
968 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
969 |
|
|
970 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
971 |
|
BitstreamPad(&bs); |
972 |
|
input_valid = 0; |
973 |
|
goto ipvop_loop; |
974 |
|
} |
975 |
|
|
976 |
/* |
/* |
977 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
978 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
982 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
983 |
*/ |
*/ |
984 |
|
|
985 |
/* ToDo : Remove dprintf calls */ |
DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
986 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
987 |
dprintf("--- PFRAME ---"); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
988 |
*/ |
|
989 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
990 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
991 |
|
} |
992 |
|
|
993 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
994 |
pFrame->intra = 0; |
pFrame->intra = 0; |
995 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
996 |
|
|
997 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
998 |
|
BitstreamPad(&bs); |
999 |
|
input_valid = 0; |
1000 |
|
goto ipvop_loop; |
1001 |
|
} |
1002 |
|
|
1003 |
} else { |
} else { |
1004 |
/* |
/* |
1005 |
* This will be coded as a Bidirectional Frame |
* This will be coded as a Bidirectional Frame |
1006 |
*/ |
*/ |
1007 |
|
|
1008 |
/* ToDo : Remove dprintf calls */ |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
1009 |
/* |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1010 |
dprintf("--- BFRAME (store) --- head=%i tail=%i", |
} |
|
pEnc->bframenum_head, |
|
|
pEnc->bframenum_tail); |
|
|
*/ |
|
1011 |
|
|
1012 |
if (pFrame->bquant < 1) { |
if (pFrame->bquant < 1) { |
1013 |
pEnc->current->quant = |
pEnc->current->quant = |
1016 |
} else { |
} else { |
1017 |
pEnc->current->quant = pFrame->bquant; |
pEnc->current->quant = pFrame->bquant; |
1018 |
} |
} |
1019 |
|
/* if (pEnc->current->quant < 1) |
1020 |
|
pEnc->current->quant = 1; |
1021 |
|
|
1022 |
|
if (pEnc->current->quant > 31) |
1023 |
|
pEnc->current->quant = 31; |
1024 |
|
|
1025 |
|
*/ |
1026 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
1027 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1028 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1029 |
|
|
1030 |
|
|
1031 |
|
|
1032 |
/* store frame into bframe buffer & swap ref back to current */ |
/* store frame into bframe buffer & swap ref back to current */ |
1033 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1037 |
|
|
1038 |
pFrame->intra = 0; |
pFrame->intra = 0; |
1039 |
pFrame->length = 0; |
pFrame->length = 0; |
|
pFrame->input_consumed = 1; |
|
1040 |
|
|
1041 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
input_valid = 0; |
1042 |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
goto bvop_loop; |
|
pEnc->mbParam.m_seconds++; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
|
} |
|
|
|
|
|
return XVID_ERR_OK; |
|
1043 |
} |
} |
1044 |
|
|
1045 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1053 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
1054 |
} |
} |
1055 |
|
|
1056 |
EMMS(); |
emms(); |
1057 |
|
|
1058 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1059 |
psnr = |
psnr = |
1060 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1061 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1070 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1071 |
} |
} |
1072 |
|
|
|
pEnc->iFrameNum++; |
|
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
|
|
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
|
|
pEnc->mbParam.m_seconds++; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
|
} |
|
|
pFrame->input_consumed = 1; |
|
1073 |
|
|
1074 |
stop_global_timer(); |
stop_global_timer(); |
1075 |
write_timer(); |
write_timer(); |
1076 |
|
|
1077 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1078 |
} |
} |
1079 |
#else |
|
1080 |
|
#endif |
1081 |
|
|
1082 |
|
|
1083 |
|
|
1084 |
/***************************************************************************** |
/***************************************************************************** |
1085 |
* Frame encoder entry point for IP capable encoder |
* "original" IP frame encoder entry point |
1086 |
|
* |
1087 |
|
* Returned values : |
1088 |
|
* - XVID_ERR_OK - no errors |
1089 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
1090 |
|
* format |
1091 |
****************************************************************************/ |
****************************************************************************/ |
1092 |
|
|
1093 |
int |
int |
1094 |
encoder_encode(Encoder * pEnc, |
encoder_encode(Encoder * pEnc, |
1095 |
XVID_ENC_FRAME * pFrame, |
XVID_ENC_FRAME * pFrame, |
1100 |
uint32_t bits; |
uint32_t bits; |
1101 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
1102 |
|
|
1103 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1104 |
float psnr; |
float psnr; |
1105 |
uint8_t temp[128]; |
uint8_t temp[128]; |
1106 |
#endif |
#endif |
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; |
1119 |
|
#ifdef BFRAMES |
1120 |
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
1121 |
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
1122 |
|
#endif |
1123 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1124 |
|
|
1125 |
start_timer(); |
start_timer(); |
1129 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
1130 |
stop_conv_timer(); |
stop_conv_timer(); |
1131 |
|
|
1132 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1133 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1134 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1135 |
#endif |
#endif |
1136 |
|
|
1137 |
EMMS(); |
emms(); |
1138 |
|
|
1139 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
1140 |
|
|
1233 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
1234 |
} |
} |
1235 |
|
|
1236 |
EMMS(); |
emms(); |
1237 |
|
|
1238 |
if (pFrame->quant == 0) { |
if (pFrame->quant == 0) { |
1239 |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1240 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1241 |
} |
} |
1242 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1243 |
psnr = |
psnr = |
1244 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1245 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1249 |
DEBUG(temp); |
DEBUG(temp); |
1250 |
#endif |
#endif |
1251 |
|
|
1252 |
|
#ifdef BFRAMES |
1253 |
|
inc_frame_num(pEnc); |
1254 |
|
#else |
1255 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1256 |
|
#endif |
1257 |
|
|
1258 |
|
|
1259 |
stop_global_timer(); |
stop_global_timer(); |
1260 |
write_timer(); |
write_timer(); |
1261 |
|
|
1262 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1263 |
} |
} |
|
#endif |
|
1264 |
|
|
1265 |
|
|
1266 |
static __inline void |
static __inline void |
1330 |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1331 |
MVBLOCKHINT *bhint = |
MVBLOCKHINT *bhint = |
1332 |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1333 |
VECTOR pred[4]; |
VECTOR pred; |
1334 |
VECTOR tmp; |
VECTOR tmp; |
|
int32_t dummy[4]; |
|
1335 |
int vec; |
int vec; |
1336 |
|
|
1337 |
pMB->mode = |
pMB->mode = |
1351 |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1352 |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
1353 |
|
|
1354 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, |
pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0); |
|
0, pred, dummy); |
|
1355 |
|
|
1356 |
for (vec = 0; vec < 4; ++vec) { |
for (vec = 0; vec < 4; ++vec) { |
1357 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1358 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1359 |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x; |
1360 |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y; |
1361 |
} |
} |
1362 |
} else if (pMB->mode == MODE_INTER4V) { |
} else if (pMB->mode == MODE_INTER4V) { |
1363 |
for (vec = 0; vec < 4; ++vec) { |
for (vec = 0; vec < 4; ++vec) { |
1370 |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1371 |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
1372 |
|
|
1373 |
get_pmvdata(pEnc->current->mbs, x, y, |
pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec); |
|
pEnc->mbParam.mb_width, vec, pred, dummy); |
|
1374 |
|
|
1375 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1376 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1377 |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x; |
1378 |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y; |
1379 |
} |
} |
1380 |
} else // intra / stuffing / not_coded |
} else // intra / stuffing / not_coded |
1381 |
{ |
{ |
1505 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1506 |
|
|
1507 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1508 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
#ifdef BFRAMES |
1509 |
|
#define DIVX501B481P "DivX501b481p" |
1510 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
1511 |
|
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1512 |
|
} |
1513 |
|
#endif |
1514 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1515 |
|
|
1516 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1517 |
|
|
1606 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
1607 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1608 |
} else { |
} else { |
1609 |
|
|
1610 |
|
#ifdef _SMP |
1611 |
|
if (pEnc->mbParam.num_threads > 1) |
1612 |
|
bIntra = |
1613 |
|
SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1614 |
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1615 |
|
iLimit); |
1616 |
|
else |
1617 |
|
#endif |
1618 |
bIntra = |
bIntra = |
1619 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1620 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1621 |
iLimit); |
iLimit); |
1622 |
|
|
1623 |
} |
} |
1624 |
stop_motion_timer(); |
stop_motion_timer(); |
1625 |
|
|
1632 |
if (vol_header) |
if (vol_header) |
1633 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1634 |
|
|
1635 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1636 |
|
|
1637 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1638 |
|
|
1731 |
|
|
1732 |
pEnc->sStat.fMvPrevSigma = fSigma; |
pEnc->sStat.fMvPrevSigma = fSigma; |
1733 |
|
|
1734 |
|
#ifdef BFRAMES |
1735 |
|
/* frame drop code */ |
1736 |
|
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks); |
1737 |
|
if (pEnc->sStat.kblks + pEnc->sStat.mblks <= |
1738 |
|
(pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100) |
1739 |
|
{ |
1740 |
|
pEnc->sStat.kblks = pEnc->sStat.mblks = 0; |
1741 |
|
pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
1742 |
|
|
1743 |
|
BitstreamReset(bs); |
1744 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
1745 |
|
|
1746 |
|
// copy reference frame details into the current frame |
1747 |
|
pEnc->current->quant = pEnc->reference->quant; |
1748 |
|
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
1749 |
|
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
1750 |
|
pEnc->current->fcode = pEnc->reference->fcode; |
1751 |
|
pEnc->current->bcode = pEnc->reference->bcode; |
1752 |
|
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1753 |
|
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
1754 |
|
|
1755 |
|
} |
1756 |
|
#endif |
1757 |
|
|
1758 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1759 |
|
|
1760 |
|
#ifdef BFRAMES |
1761 |
|
pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->mbParam.m_ticks) % (int32_t)pEnc->mbParam.fbase; |
1762 |
|
pEnc->last_pframe = pEnc->mbParam.m_ticks; |
1763 |
|
#endif |
1764 |
|
|
1765 |
return 0; // inter |
return 0; // inter |
1766 |
} |
} |
1767 |
|
|
1782 |
IMAGE *f_ref = &pEnc->reference->image; |
IMAGE *f_ref = &pEnc->reference->image; |
1783 |
IMAGE *b_ref = &pEnc->current->image; |
IMAGE *b_ref = &pEnc->current->image; |
1784 |
|
|
1785 |
|
#ifdef BFRAMES_DEC_DEBUG |
1786 |
|
FILE *fp; |
1787 |
|
static char first=0; |
1788 |
|
#define BFRAME_DEBUG if (!first && fp){ \ |
1789 |
|
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
1790 |
|
} |
1791 |
|
|
1792 |
|
if (!first){ |
1793 |
|
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
1794 |
|
} |
1795 |
|
#endif |
1796 |
|
|
1797 |
// forward |
// forward |
1798 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1799 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1817 |
stop_inter_timer(); |
stop_inter_timer(); |
1818 |
|
|
1819 |
start_timer(); |
start_timer(); |
1820 |
MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1821 |
|
((int32_t)pEnc->mbParam.fbase + (int32_t)pEnc->mbParam.m_ticks + 1 - (int32_t)pEnc->last_pframe) % pEnc->mbParam.fbase, |
1822 |
|
pEnc->time_pp, |
1823 |
|
pEnc->reference->mbs, f_ref, |
1824 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1825 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
1826 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
1834 |
} */ |
} */ |
1835 |
|
|
1836 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1837 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
1838 |
|
|
1839 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1840 |
|
|
1863 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1864 |
mb->mvs[0].x = 0; |
mb->mvs[0].x = 0; |
1865 |
mb->mvs[0].y = 0; |
mb->mvs[0].y = 0; |
1866 |
|
|
1867 |
|
mb->cbp = 0; |
1868 |
|
#ifdef BFRAMES_DEC_DEBUG |
1869 |
|
BFRAME_DEBUG |
1870 |
|
#endif |
1871 |
continue; |
continue; |
1872 |
} |
} |
1873 |
|
|
1886 |
|
|
1887 |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) |
1888 |
&& mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) { |
&& mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) { |
1889 |
mb->mode = 5; // skipped |
mb->mode = MODE_DIRECT_NONE_MV; // skipped |
1890 |
} |
} |
1891 |
|
|
1892 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
1902 |
backward.x = mb->b_mvs[0].x; |
backward.x = mb->b_mvs[0].x; |
1903 |
backward.y = mb->b_mvs[0].y; |
backward.y = mb->b_mvs[0].y; |
1904 |
} |
} |
1905 |
// printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y); |
// 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); |
1906 |
|
|
1907 |
|
#ifdef BFRAMES_DEC_DEBUG |
1908 |
|
BFRAME_DEBUG |
1909 |
|
#endif |
1910 |
start_timer(); |
start_timer(); |
1911 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1912 |
&pEnc->sStat); |
&pEnc->sStat); |
1919 |
// TODO: dynamic fcode/bcode ??? |
// TODO: dynamic fcode/bcode ??? |
1920 |
|
|
1921 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1922 |
|
|
1923 |
|
#ifdef BFRAMES_DEC_DEBUG |
1924 |
|
if (!first){ |
1925 |
|
first=1; |
1926 |
|
if (fp) |
1927 |
|
fclose(fp); |
1928 |
|
} |
1929 |
|
#endif |
1930 |
} |
} |
1931 |
#endif |
#endif |