32 |
* |
* |
33 |
* History |
* History |
34 |
* |
* |
35 |
|
* 20.06.2002 bframe patch |
36 |
* 08.05.2002 fix some problem in DEBUG mode; |
* 08.05.2002 fix some problem in DEBUG mode; |
37 |
* MinChen <chenm001@163.com> |
* MinChen <chenm001@163.com> |
38 |
* 14.04.2002 added FrameCodeB() |
* 14.04.2002 added FrameCodeB() |
39 |
* |
* |
40 |
* $Id: encoder.c,v 1.43 2002-06-14 13:21:35 Isibaar Exp $ |
* $Id: encoder.c,v 1.52 2002-07-06 17:04:57 chl Exp $ |
41 |
* |
* |
42 |
****************************************************************************/ |
****************************************************************************/ |
43 |
|
|
51 |
#include "global.h" |
#include "global.h" |
52 |
#include "utils/timer.h" |
#include "utils/timer.h" |
53 |
#include "image/image.h" |
#include "image/image.h" |
54 |
|
#ifdef BFRAMES |
55 |
|
#include "image/font.h" |
56 |
|
#endif |
57 |
#include "motion/motion.h" |
#include "motion/motion.h" |
58 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
59 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
66 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
67 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
68 |
|
|
69 |
|
#ifdef _SMP |
70 |
|
#include "motion/smp_motion_est.h" |
71 |
|
#endif |
72 |
/***************************************************************************** |
/***************************************************************************** |
73 |
* Local macros |
* Local macros |
74 |
****************************************************************************/ |
****************************************************************************/ |
139 |
encoder_create(XVID_ENC_PARAM * pParam) |
encoder_create(XVID_ENC_PARAM * pParam) |
140 |
{ |
{ |
141 |
Encoder *pEnc; |
Encoder *pEnc; |
142 |
uint32_t i; |
int i; |
143 |
|
|
144 |
pParam->handle = NULL; |
pParam->handle = NULL; |
145 |
|
|
266 |
|
|
267 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
268 |
|
|
269 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
270 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
271 |
#endif |
#endif |
272 |
#ifdef BFRAMES |
#ifdef BFRAMES |
282 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
283 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
284 |
|
|
285 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
286 |
if (image_create |
if (image_create |
287 |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
288 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
336 |
/* B Frames specific init */ |
/* B Frames specific init */ |
337 |
#ifdef BFRAMES |
#ifdef BFRAMES |
338 |
|
|
339 |
|
pEnc->global = pParam->global; |
340 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
341 |
pEnc->bquant_ratio = pParam->bquant_ratio; |
pEnc->bquant_ratio = pParam->bquant_ratio; |
342 |
pEnc->bframes = NULL; |
pEnc->bframes = NULL; |
381 |
pEnc->bframenum_head = 0; |
pEnc->bframenum_head = 0; |
382 |
pEnc->bframenum_tail = 0; |
pEnc->bframenum_tail = 0; |
383 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
384 |
|
pEnc->bframenum_dx50bvop = -1; |
385 |
|
|
386 |
|
pEnc->queue = NULL; |
387 |
|
|
388 |
|
|
389 |
|
if (pEnc->mbParam.max_bframes > 0) { |
390 |
|
int n; |
391 |
|
|
392 |
|
pEnc->queue = |
393 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE), |
394 |
|
CACHE_LINE); |
395 |
|
|
396 |
|
if (pEnc->queue == NULL) |
397 |
|
goto xvid_err_memory4; |
398 |
|
|
399 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
400 |
|
image_null(&pEnc->queue[n]); |
401 |
|
|
402 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
403 |
|
if (image_create |
404 |
|
(&pEnc->queue[n], pEnc->mbParam.edged_width, |
405 |
|
pEnc->mbParam.edged_height) < 0) |
406 |
|
goto xvid_err_memory5; |
407 |
|
|
408 |
|
} |
409 |
|
} |
410 |
|
|
411 |
|
pEnc->queue_head = 0; |
412 |
|
pEnc->queue_tail = 0; |
413 |
|
pEnc->queue_size = 0; |
414 |
|
|
415 |
|
|
416 |
pEnc->mbParam.m_seconds = 0; |
pEnc->mbParam.m_seconds = 0; |
417 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
418 |
|
pEnc->m_framenum = 0; |
419 |
#endif |
#endif |
420 |
|
|
421 |
pParam->handle = (void *) pEnc; |
pParam->handle = (void *) pEnc; |
436 |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
437 |
*/ |
*/ |
438 |
#ifdef BFRAMES |
#ifdef BFRAMES |
439 |
|
xvid_err_memory5: |
440 |
|
|
441 |
|
|
442 |
|
if (pEnc->mbParam.max_bframes > 0) { |
443 |
|
|
444 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
445 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
446 |
|
pEnc->mbParam.edged_height); |
447 |
|
} |
448 |
|
xvid_free(pEnc->queue); |
449 |
|
} |
450 |
|
|
451 |
xvid_err_memory4: |
xvid_err_memory4: |
452 |
|
|
453 |
|
if (pEnc->mbParam.max_bframes > 0) { |
454 |
|
|
455 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
456 |
|
|
457 |
if (pEnc->bframes[i] == NULL) |
if (pEnc->bframes[i] == NULL) |
467 |
} |
} |
468 |
|
|
469 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
470 |
|
} |
471 |
|
|
472 |
#endif |
#endif |
473 |
|
|
474 |
xvid_err_memory3: |
xvid_err_memory3: |
475 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
476 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
477 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
478 |
#endif |
#endif |
529 |
int |
int |
530 |
encoder_destroy(Encoder * pEnc) |
encoder_destroy(Encoder * pEnc) |
531 |
{ |
{ |
532 |
|
#ifdef BFRAMES |
533 |
|
int i; |
534 |
|
#endif |
535 |
|
|
536 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
537 |
|
|
538 |
/* B Frames specific */ |
/* B Frames specific */ |
539 |
#ifdef BFRAMES |
#ifdef BFRAMES |
540 |
int i; |
if (pEnc->mbParam.max_bframes > 0) { |
541 |
|
|
542 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
543 |
|
|
544 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
545 |
|
pEnc->mbParam.edged_height); |
546 |
|
} |
547 |
|
xvid_free(pEnc->queue); |
548 |
|
} |
549 |
|
|
550 |
|
|
551 |
|
if (pEnc->mbParam.max_bframes > 0) { |
552 |
|
|
553 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
554 |
|
|
561 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
562 |
|
|
563 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
|
|
|
564 |
} |
} |
565 |
|
|
566 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
567 |
|
|
568 |
|
} |
569 |
#endif |
#endif |
570 |
|
|
571 |
/* All images, reference, current etc ... */ |
/* All images, reference, current etc ... */ |
592 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
593 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
594 |
#endif |
#endif |
595 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
596 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
597 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
598 |
#endif |
#endif |
610 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
611 |
} |
} |
612 |
|
|
613 |
|
|
614 |
|
#ifdef BFRAMES |
615 |
|
void inc_frame_num(Encoder * pEnc) |
616 |
|
{ |
617 |
|
pEnc->iFrameNum++; |
618 |
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
619 |
|
|
620 |
|
pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
621 |
|
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
622 |
|
} |
623 |
|
#endif |
624 |
|
|
625 |
|
|
626 |
|
#ifdef BFRAMES |
627 |
|
void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
628 |
|
{ |
629 |
|
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
630 |
|
{ |
631 |
|
DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL"); |
632 |
|
return; |
633 |
|
} |
634 |
|
|
635 |
|
DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
636 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
637 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
638 |
|
|
639 |
|
|
640 |
|
start_timer(); |
641 |
|
if (image_input |
642 |
|
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
643 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
644 |
|
return; |
645 |
|
stop_conv_timer(); |
646 |
|
|
647 |
|
pEnc->queue_size++; |
648 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
649 |
|
} |
650 |
|
#endif |
651 |
|
|
652 |
|
|
653 |
|
#ifdef BFRAMES |
654 |
/***************************************************************************** |
/***************************************************************************** |
655 |
* 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. |
|
656 |
* |
* |
657 |
* Returned values : |
* Returned values : |
658 |
* - XVID_ERR_OK - no errors |
* - XVID_ERR_OK - no errors |
660 |
* format |
* format |
661 |
****************************************************************************/ |
****************************************************************************/ |
662 |
|
|
|
|
|
|
#ifdef BFRAMES |
|
|
/***************************************************************************** |
|
|
* Frame encoder entry point for IPB capable encoder |
|
|
****************************************************************************/ |
|
663 |
int |
int |
664 |
encoder_encode(Encoder * pEnc, |
encoder_encode_bframes(Encoder * pEnc, |
665 |
XVID_ENC_FRAME * pFrame, |
XVID_ENC_FRAME * pFrame, |
666 |
XVID_ENC_STATS * pResult) |
XVID_ENC_STATS * pResult) |
667 |
{ |
{ |
669 |
Bitstream bs; |
Bitstream bs; |
670 |
uint32_t bits; |
uint32_t bits; |
671 |
|
|
672 |
#ifdef _DEBUG |
int input_valid = 1; |
673 |
|
|
674 |
|
#ifdef _DEBUG_PSNR |
675 |
float psnr; |
float psnr; |
676 |
char temp[128]; |
char temp[128]; |
677 |
#endif |
#endif |
678 |
|
|
679 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
680 |
ENC_CHECK(pFrame); |
ENC_CHECK(pFrame); |
681 |
|
ENC_CHECK(pFrame->image); |
682 |
|
|
683 |
start_global_timer(); |
start_global_timer(); |
684 |
|
|
685 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
686 |
|
|
687 |
|
ipvop_loop: |
688 |
|
|
689 |
/* |
/* |
690 |
* bframe "flush" code |
* bframe "flush" code |
691 |
*/ |
*/ |
700 |
* frame as a pframe |
* frame as a pframe |
701 |
*/ |
*/ |
702 |
|
|
703 |
/* ToDo : remove dprintf calls */ |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
704 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
705 |
dprintf("--- PFRAME (final frame correction) --- "); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
706 |
*/ |
|
707 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
708 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
709 |
|
|
713 |
|
|
714 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
715 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
716 |
pFrame->intra = 0; |
pFrame->intra = 0; |
717 |
|
|
718 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
719 |
} |
} |
720 |
|
|
721 |
/* ToDo : remove dprintf calls */ |
|
722 |
/* |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
723 |
dprintf("--- BFRAME (flush) --- "); |
pEnc->bframenum_head, pEnc->bframenum_tail, |
724 |
*/ |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
725 |
|
|
726 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
727 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
728 |
|
|
|
|
|
729 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
730 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
731 |
pFrame->intra = 0; |
pFrame->intra = 0; |
732 |
|
|
733 |
|
if (input_valid) |
734 |
|
queue_image(pEnc, pFrame); |
735 |
|
|
736 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
737 |
} |
} |
738 |
|
|
739 |
if (pFrame->image == NULL) { |
if (pEnc->bframenum_head > 0) { |
740 |
pFrame->length = 0; |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
741 |
pFrame->input_consumed = 1; |
|
742 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
743 |
|
|
744 |
|
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
745 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
746 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
747 |
|
|
748 |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
749 |
|
BitstreamPad(&bs); |
750 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
751 |
|
|
752 |
|
pFrame->length = BitstreamLength(&bs); |
753 |
pFrame->intra = 0; |
pFrame->intra = 0; |
754 |
|
|
755 |
|
if (input_valid) |
756 |
|
queue_image(pEnc, pFrame); |
757 |
|
|
758 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
759 |
} |
} |
760 |
|
} |
761 |
|
|
762 |
if (pEnc->bframenum_head > 0) { |
|
763 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
bvop_loop: |
764 |
|
|
765 |
|
if (pEnc->bframenum_dx50bvop != -1) |
766 |
|
{ |
767 |
|
|
768 |
|
SWAP(pEnc->current, pEnc->reference); |
769 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
770 |
|
|
771 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
772 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
773 |
|
} |
774 |
|
|
775 |
|
if (input_valid) |
776 |
|
{ |
777 |
|
queue_image(pEnc, pFrame); |
778 |
|
input_valid = 0; |
779 |
|
} |
780 |
|
|
781 |
|
} else if (input_valid) { |
782 |
|
|
783 |
|
SWAP(pEnc->current, pEnc->reference); |
784 |
|
|
785 |
|
start_timer(); |
786 |
|
if (image_input |
787 |
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
788 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
789 |
|
return XVID_ERR_FORMAT; |
790 |
|
stop_conv_timer(); |
791 |
|
|
792 |
|
// queue input frame, and dequue next image |
793 |
|
if (pEnc->queue_size > 0) |
794 |
|
{ |
795 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); |
796 |
|
if (pEnc->queue_head != pEnc->queue_tail) |
797 |
|
{ |
798 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
799 |
|
} |
800 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
801 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
802 |
|
} |
803 |
|
|
804 |
|
} else if (pEnc->queue_size > 0) { |
805 |
|
|
806 |
|
SWAP(pEnc->current, pEnc->reference); |
807 |
|
|
808 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
809 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
810 |
|
pEnc->queue_size--; |
811 |
|
|
812 |
|
} else if (BitstreamPos(&bs) == 0) { |
813 |
|
|
814 |
|
DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
815 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
816 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
817 |
|
|
818 |
|
pFrame->intra = 0; |
819 |
|
|
820 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
821 |
|
BitstreamPad(&bs); |
822 |
|
pFrame->length = BitstreamLength(&bs); |
823 |
|
|
824 |
|
return XVID_ERR_OK; |
825 |
|
|
826 |
|
} else { |
827 |
|
|
828 |
|
pFrame->length = BitstreamLength(&bs); |
829 |
|
return XVID_ERR_OK; |
830 |
} |
} |
831 |
|
|
832 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
836 |
* comment style :-) |
* comment style :-) |
837 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
838 |
|
|
|
SWAP(pEnc->current, pEnc->reference); |
|
|
|
|
839 |
emms(); |
emms(); |
840 |
|
|
841 |
|
// only inc frame num, adapt quant, etc. if we havent seen it before |
842 |
|
if (pEnc->bframenum_dx50bvop < 0 ) |
843 |
|
{ |
844 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
845 |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
846 |
else |
else |
854 |
|
|
855 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
856 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
857 |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
|
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
858 |
/* ToDo : dynamic fcode (in both directions) */ |
/* ToDo : dynamic fcode (in both directions) */ |
859 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
860 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
861 |
|
|
862 |
start_timer(); |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
863 |
if (image_input |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
864 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
|
865 |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
inc_frame_num(pEnc); |
|
return XVID_ERR_FORMAT; |
|
|
stop_conv_timer(); |
|
866 |
|
|
867 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
868 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
869 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
870 |
#endif |
#endif |
871 |
|
|
872 |
emms(); |
emms(); |
873 |
|
|
874 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
875 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
876 |
|
"%i if:%i st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks); |
877 |
|
} |
878 |
|
|
879 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
880 |
* Luminance masking |
* Luminance masking |
881 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
905 |
} |
} |
906 |
|
|
907 |
#undef OFFSET |
#undef OFFSET |
|
|
|
908 |
} |
} |
909 |
|
|
910 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
911 |
} |
} |
912 |
|
|
913 |
|
} |
914 |
|
|
915 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
916 |
* ivop/pvop/bvop selection |
* ivop/pvop/bvop selection |
917 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
918 |
|
|
919 |
|
|
920 |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
921 |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
922 |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
923 |
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
927 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
928 |
*/ |
*/ |
929 |
|
|
930 |
/* ToDo : Remove dprintf calls */ |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
931 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
932 |
dprintf("--- IFRAME ---"); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
|
*/ |
|
933 |
|
|
934 |
FrameCodeI(pEnc, &bs, &bits); |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
935 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
936 |
|
} |
937 |
|
|
938 |
|
// when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe |
939 |
|
|
940 |
|
if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
941 |
|
|
942 |
|
pEnc->bframenum_tail--; |
943 |
|
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
944 |
|
|
945 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
946 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
947 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
948 |
|
} |
949 |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
950 |
|
|
951 |
|
pFrame->intra = 0; |
952 |
|
|
953 |
|
} else { |
954 |
|
|
955 |
|
FrameCodeI(pEnc, &bs, &bits); |
956 |
pFrame->intra = 1; |
pFrame->intra = 1; |
957 |
|
|
958 |
|
pEnc->bframenum_dx50bvop = -1; |
959 |
|
} |
960 |
|
|
961 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
962 |
|
|
963 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
964 |
|
BitstreamPad(&bs); |
965 |
|
input_valid = 0; |
966 |
|
goto ipvop_loop; |
967 |
|
} |
968 |
|
|
969 |
/* |
/* |
970 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
971 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
975 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
976 |
*/ |
*/ |
977 |
|
|
978 |
/* ToDo : Remove dprintf calls */ |
DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
979 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
980 |
dprintf("--- PFRAME ---"); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
981 |
*/ |
|
982 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
983 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
984 |
|
} |
985 |
|
|
986 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
987 |
pFrame->intra = 0; |
pFrame->intra = 0; |
988 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
989 |
|
|
990 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
991 |
|
BitstreamPad(&bs); |
992 |
|
input_valid = 0; |
993 |
|
goto ipvop_loop; |
994 |
|
} |
995 |
|
|
996 |
} else { |
} else { |
997 |
/* |
/* |
998 |
* This will be coded as a Bidirectional Frame |
* This will be coded as a Bidirectional Frame |
999 |
*/ |
*/ |
1000 |
|
|
1001 |
/* ToDo : Remove dprintf calls */ |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
1002 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1003 |
dprintf("--- BFRAME (store) --- head=%i tail=%i", |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1004 |
pEnc->bframenum_head, |
|
1005 |
pEnc->bframenum_tail); |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
1006 |
*/ |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1007 |
|
} |
1008 |
|
|
1009 |
if (pFrame->bquant < 1) { |
if (pFrame->bquant < 1) { |
1010 |
pEnc->current->quant = |
pEnc->current->quant = |
1022 |
|
|
1023 |
pFrame->intra = 0; |
pFrame->intra = 0; |
1024 |
pFrame->length = 0; |
pFrame->length = 0; |
|
pFrame->input_consumed = 1; |
|
1025 |
|
|
1026 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
input_valid = 0; |
1027 |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
goto bvop_loop; |
|
pEnc->mbParam.m_seconds++; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
|
} |
|
|
|
|
|
return XVID_ERR_OK; |
|
1028 |
} |
} |
1029 |
|
|
1030 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1040 |
|
|
1041 |
emms(); |
emms(); |
1042 |
|
|
1043 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1044 |
psnr = |
psnr = |
1045 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1046 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1055 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1056 |
} |
} |
1057 |
|
|
|
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; |
|
1058 |
|
|
1059 |
stop_global_timer(); |
stop_global_timer(); |
1060 |
write_timer(); |
write_timer(); |
1061 |
|
|
1062 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1063 |
} |
} |
1064 |
#else |
|
1065 |
|
#endif |
1066 |
|
|
1067 |
|
|
1068 |
|
|
1069 |
/***************************************************************************** |
/***************************************************************************** |
1070 |
* Frame encoder entry point for IP capable encoder |
* "original" IP frame encoder entry point |
1071 |
|
* |
1072 |
|
* Returned values : |
1073 |
|
* - XVID_ERR_OK - no errors |
1074 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
1075 |
|
* format |
1076 |
****************************************************************************/ |
****************************************************************************/ |
1077 |
|
|
1078 |
int |
int |
1079 |
encoder_encode(Encoder * pEnc, |
encoder_encode(Encoder * pEnc, |
1080 |
XVID_ENC_FRAME * pFrame, |
XVID_ENC_FRAME * pFrame, |
1085 |
uint32_t bits; |
uint32_t bits; |
1086 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
1087 |
|
|
1088 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1089 |
float psnr; |
float psnr; |
1090 |
uint8_t temp[128]; |
uint8_t temp[128]; |
1091 |
#endif |
#endif |
1101 |
|
|
1102 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1103 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
1104 |
|
#ifdef BFRAMES |
1105 |
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
1106 |
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
1107 |
|
#endif |
1108 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1109 |
|
|
1110 |
start_timer(); |
start_timer(); |
1114 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
1115 |
stop_conv_timer(); |
stop_conv_timer(); |
1116 |
|
|
1117 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1118 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1119 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1120 |
#endif |
#endif |
1224 |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1225 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1226 |
} |
} |
1227 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1228 |
psnr = |
psnr = |
1229 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1230 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1234 |
DEBUG(temp); |
DEBUG(temp); |
1235 |
#endif |
#endif |
1236 |
|
|
1237 |
|
#ifdef BFRAMES |
1238 |
|
inc_frame_num(pEnc); |
1239 |
|
#else |
1240 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1241 |
|
#endif |
1242 |
|
|
1243 |
|
|
1244 |
stop_global_timer(); |
stop_global_timer(); |
1245 |
write_timer(); |
write_timer(); |
1246 |
|
|
1247 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1248 |
} |
} |
|
#endif |
|
1249 |
|
|
1250 |
|
|
1251 |
static __inline void |
static __inline void |
1493 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1494 |
|
|
1495 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1496 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
#ifdef BFRAMES |
1497 |
|
#define DIVX501B481P "DivX501b481p" |
1498 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
1499 |
|
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1500 |
|
} |
1501 |
|
#endif |
1502 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1503 |
|
|
1504 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1505 |
|
|
1594 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
1595 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1596 |
} else { |
} else { |
1597 |
|
|
1598 |
|
#ifdef _SMP |
1599 |
|
if (NUMTHREADS > 1) |
1600 |
|
bIntra = |
1601 |
|
SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1602 |
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1603 |
|
iLimit); |
1604 |
|
else |
1605 |
|
#endif |
1606 |
|
|
1607 |
bIntra = |
bIntra = |
1608 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1609 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1610 |
iLimit); |
iLimit); |
1611 |
|
|
1612 |
|
|
1613 |
} |
} |
1614 |
stop_motion_timer(); |
stop_motion_timer(); |
1615 |
|
|
1622 |
if (vol_header) |
if (vol_header) |
1623 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1624 |
|
|
1625 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1626 |
|
|
1627 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1628 |
|
|
1780 |
} */ |
} */ |
1781 |
|
|
1782 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1783 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
1784 |
|
|
1785 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1786 |
|
|
1843 |
backward.x = mb->b_mvs[0].x; |
backward.x = mb->b_mvs[0].x; |
1844 |
backward.y = mb->b_mvs[0].y; |
backward.y = mb->b_mvs[0].y; |
1845 |
} |
} |
1846 |
// 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); |
1847 |
|
|
1848 |
start_timer(); |
start_timer(); |
1849 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |