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.42 2002-06-13 11:42:15 edgomez Exp $ |
* $Id: encoder.c,v 1.48 2002-06-24 09:53:17 suxen_drol 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" |
136 |
encoder_create(XVID_ENC_PARAM * pParam) |
encoder_create(XVID_ENC_PARAM * pParam) |
137 |
{ |
{ |
138 |
Encoder *pEnc; |
Encoder *pEnc; |
139 |
uint32_t i; |
int i; |
140 |
|
|
141 |
pParam->handle = NULL; |
pParam->handle = NULL; |
142 |
|
|
230 |
pEnc->mbParam.fbase = pParam->fbase; |
pEnc->mbParam.fbase = pParam->fbase; |
231 |
pEnc->mbParam.fincr = pParam->fincr; |
pEnc->mbParam.fincr = pParam->fincr; |
232 |
|
|
233 |
|
pEnc->mbParam.m_quant_type = H263_QUANT; |
234 |
|
|
235 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->sStat.fMvPrevSigma = -1; |
236 |
|
|
237 |
/* Fill rate control parameters */ |
/* Fill rate control parameters */ |
263 |
|
|
264 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
265 |
|
|
266 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
267 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
268 |
#endif |
#endif |
269 |
#ifdef BFRAMES |
#ifdef BFRAMES |
279 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
280 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
281 |
|
|
282 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
283 |
if (image_create |
if (image_create |
284 |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
285 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
333 |
/* B Frames specific init */ |
/* B Frames specific init */ |
334 |
#ifdef BFRAMES |
#ifdef BFRAMES |
335 |
|
|
336 |
|
pEnc->global = pParam->global; |
337 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
338 |
pEnc->bquant_ratio = pParam->bquant_ratio; |
pEnc->bquant_ratio = pParam->bquant_ratio; |
339 |
pEnc->bframes = NULL; |
pEnc->bframes = NULL; |
378 |
pEnc->bframenum_head = 0; |
pEnc->bframenum_head = 0; |
379 |
pEnc->bframenum_tail = 0; |
pEnc->bframenum_tail = 0; |
380 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
381 |
|
pEnc->bframenum_dx50bvop = -1; |
382 |
|
|
383 |
|
pEnc->queue = NULL; |
384 |
|
|
385 |
|
|
386 |
|
if (pEnc->mbParam.max_bframes > 0) { |
387 |
|
int n; |
388 |
|
|
389 |
|
pEnc->queue = |
390 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE), |
391 |
|
CACHE_LINE); |
392 |
|
|
393 |
|
if (pEnc->queue == NULL) |
394 |
|
goto xvid_err_memory4; |
395 |
|
|
396 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
397 |
|
image_null(&pEnc->queue[n]); |
398 |
|
|
399 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
400 |
|
if (image_create |
401 |
|
(&pEnc->queue[n], pEnc->mbParam.edged_width, |
402 |
|
pEnc->mbParam.edged_height) < 0) |
403 |
|
goto xvid_err_memory5; |
404 |
|
|
405 |
|
} |
406 |
|
} |
407 |
|
|
408 |
|
pEnc->queue_head = 0; |
409 |
|
pEnc->queue_tail = 0; |
410 |
|
pEnc->queue_size = 0; |
411 |
|
|
412 |
|
|
413 |
pEnc->mbParam.m_seconds = 0; |
pEnc->mbParam.m_seconds = 0; |
414 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
415 |
|
pEnc->m_framenum = 0; |
416 |
#endif |
#endif |
417 |
|
|
418 |
pParam->handle = (void *) pEnc; |
pParam->handle = (void *) pEnc; |
433 |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
434 |
*/ |
*/ |
435 |
#ifdef BFRAMES |
#ifdef BFRAMES |
436 |
|
xvid_err_memory5: |
437 |
|
|
438 |
|
|
439 |
|
if (pEnc->mbParam.max_bframes > 0) { |
440 |
|
|
441 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
442 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
443 |
|
pEnc->mbParam.edged_height); |
444 |
|
} |
445 |
|
xvid_free(pEnc->queue); |
446 |
|
} |
447 |
|
|
448 |
xvid_err_memory4: |
xvid_err_memory4: |
449 |
|
|
450 |
|
if (pEnc->mbParam.max_bframes > 0) { |
451 |
|
|
452 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
453 |
|
|
454 |
if (pEnc->bframes[i] == NULL) |
if (pEnc->bframes[i] == NULL) |
464 |
} |
} |
465 |
|
|
466 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
467 |
|
} |
468 |
|
|
469 |
#endif |
#endif |
470 |
|
|
471 |
xvid_err_memory3: |
xvid_err_memory3: |
472 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
473 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
474 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
475 |
#endif |
#endif |
526 |
int |
int |
527 |
encoder_destroy(Encoder * pEnc) |
encoder_destroy(Encoder * pEnc) |
528 |
{ |
{ |
529 |
|
#ifdef BFRAMES |
530 |
|
int i; |
531 |
|
#endif |
532 |
|
|
533 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
534 |
|
|
535 |
/* B Frames specific */ |
/* B Frames specific */ |
536 |
#ifdef BFRAMES |
#ifdef BFRAMES |
537 |
int i; |
if (pEnc->mbParam.max_bframes > 0) { |
538 |
|
|
539 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
540 |
|
|
541 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
542 |
|
pEnc->mbParam.edged_height); |
543 |
|
} |
544 |
|
xvid_free(pEnc->queue); |
545 |
|
} |
546 |
|
|
547 |
|
|
548 |
|
if (pEnc->mbParam.max_bframes > 0) { |
549 |
|
|
550 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
551 |
|
|
558 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
559 |
|
|
560 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
|
|
|
561 |
} |
} |
562 |
|
|
563 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
564 |
|
|
565 |
|
} |
566 |
#endif |
#endif |
567 |
|
|
568 |
/* All images, reference, current etc ... */ |
/* All images, reference, current etc ... */ |
589 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
590 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
591 |
#endif |
#endif |
592 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
593 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
594 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
595 |
#endif |
#endif |
607 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
608 |
} |
} |
609 |
|
|
610 |
|
|
611 |
|
#ifdef BFRAMES |
612 |
|
void inc_frame_num(Encoder * pEnc) |
613 |
|
{ |
614 |
|
pEnc->iFrameNum++; |
615 |
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
616 |
|
|
617 |
|
pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
618 |
|
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
619 |
|
} |
620 |
|
#endif |
621 |
|
|
622 |
|
|
623 |
|
#ifdef BFRAMES |
624 |
|
void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
625 |
|
{ |
626 |
|
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
627 |
|
{ |
628 |
|
DPRINTF("FATAL: QUEUE FULL"); |
629 |
|
return; |
630 |
|
} |
631 |
|
|
632 |
|
DPRINTF("*** QUEUE bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
633 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
634 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
635 |
|
|
636 |
|
|
637 |
|
start_timer(); |
638 |
|
if (image_input |
639 |
|
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
640 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
641 |
|
return; |
642 |
|
stop_conv_timer(); |
643 |
|
|
644 |
|
pEnc->queue_size++; |
645 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
646 |
|
} |
647 |
|
#endif |
648 |
|
|
649 |
|
|
650 |
|
#ifdef BFRAMES |
651 |
/***************************************************************************** |
/***************************************************************************** |
652 |
* 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. |
|
653 |
* |
* |
654 |
* Returned values : |
* Returned values : |
655 |
* - XVID_ERR_OK - no errors |
* - XVID_ERR_OK - no errors |
657 |
* format |
* format |
658 |
****************************************************************************/ |
****************************************************************************/ |
659 |
|
|
|
|
|
|
#ifdef BFRAMES |
|
|
/***************************************************************************** |
|
|
* Frame encoder entry point for IPB capable encoder |
|
|
****************************************************************************/ |
|
660 |
int |
int |
661 |
encoder_encode(Encoder * pEnc, |
encoder_encode_bframes(Encoder * pEnc, |
662 |
XVID_ENC_FRAME * pFrame, |
XVID_ENC_FRAME * pFrame, |
663 |
XVID_ENC_STATS * pResult) |
XVID_ENC_STATS * pResult) |
664 |
{ |
{ |
666 |
Bitstream bs; |
Bitstream bs; |
667 |
uint32_t bits; |
uint32_t bits; |
668 |
|
|
669 |
#ifdef _DEBUG |
int input_valid = 1; |
670 |
|
|
671 |
|
#ifdef _DEBUG_PSNR |
672 |
float psnr; |
float psnr; |
673 |
char temp[128]; |
char temp[128]; |
674 |
#endif |
#endif |
675 |
|
|
676 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
677 |
ENC_CHECK(pFrame); |
ENC_CHECK(pFrame); |
678 |
|
ENC_CHECK(pFrame->image); |
679 |
|
|
680 |
start_global_timer(); |
start_global_timer(); |
681 |
|
|
682 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
683 |
|
|
684 |
|
ipvop_loop: |
685 |
|
|
686 |
/* |
/* |
687 |
* bframe "flush" code |
* bframe "flush" code |
688 |
*/ |
*/ |
697 |
* frame as a pframe |
* frame as a pframe |
698 |
*/ |
*/ |
699 |
|
|
700 |
/* ToDo : remove dprintf calls */ |
DPRINTF("*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
701 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
702 |
dprintf("--- PFRAME (final frame correction) --- "); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
703 |
*/ |
|
704 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
705 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
706 |
|
|
710 |
|
|
711 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
712 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
713 |
pFrame->intra = 0; |
pFrame->intra = 0; |
714 |
|
|
715 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
716 |
} |
} |
717 |
|
|
718 |
/* ToDo : remove dprintf calls */ |
|
719 |
/* |
DPRINTF("*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
720 |
dprintf("--- BFRAME (flush) --- "); |
pEnc->bframenum_head, pEnc->bframenum_tail, |
721 |
*/ |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
722 |
|
|
723 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
724 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
725 |
|
|
|
|
|
726 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
727 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
728 |
pFrame->intra = 0; |
pFrame->intra = 0; |
729 |
|
|
730 |
|
if (input_valid) |
731 |
|
queue_image(pEnc, pFrame); |
732 |
|
|
733 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
734 |
} |
} |
735 |
|
|
736 |
if (pFrame->image == NULL) { |
if (pEnc->bframenum_head > 0) { |
737 |
pFrame->length = 0; |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
738 |
pFrame->input_consumed = 1; |
|
739 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
740 |
|
|
741 |
|
DPRINTF("*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
742 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
743 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
744 |
|
|
745 |
|
|
746 |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
747 |
|
BitstreamPad(&bs); |
748 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
749 |
|
|
750 |
|
pFrame->length = BitstreamLength(&bs); |
751 |
pFrame->intra = 0; |
pFrame->intra = 0; |
752 |
|
|
753 |
|
if (input_valid) |
754 |
|
queue_image(pEnc, pFrame); |
755 |
|
|
756 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
757 |
} |
} |
758 |
|
} |
759 |
|
|
760 |
if (pEnc->bframenum_head > 0) { |
|
761 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
bvop_loop: |
762 |
|
|
763 |
|
if (pEnc->bframenum_dx50bvop != -1) |
764 |
|
{ |
765 |
|
|
766 |
|
SWAP(pEnc->current, pEnc->reference); |
767 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
768 |
|
|
769 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
770 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
771 |
|
} |
772 |
|
|
773 |
|
if (input_valid) |
774 |
|
{ |
775 |
|
queue_image(pEnc, pFrame); |
776 |
|
input_valid = 0; |
777 |
|
} |
778 |
|
|
779 |
|
} else if (input_valid) { |
780 |
|
|
781 |
|
SWAP(pEnc->current, pEnc->reference); |
782 |
|
|
783 |
|
start_timer(); |
784 |
|
if (image_input |
785 |
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
786 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
787 |
|
return XVID_ERR_FORMAT; |
788 |
|
stop_conv_timer(); |
789 |
|
|
790 |
|
// queue input frame, and dequue next image |
791 |
|
if (pEnc->queue_size > 0) |
792 |
|
{ |
793 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); |
794 |
|
if (pEnc->queue_head != pEnc->queue_tail) |
795 |
|
{ |
796 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
797 |
|
} |
798 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
799 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
800 |
|
} |
801 |
|
|
802 |
|
} else if (pEnc->queue_size > 0) { |
803 |
|
|
804 |
|
SWAP(pEnc->current, pEnc->reference); |
805 |
|
|
806 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
807 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
808 |
|
pEnc->queue_size--; |
809 |
|
|
810 |
|
} else if (BitstreamPos(&bs) == 0) { |
811 |
|
|
812 |
|
DPRINTF("*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
813 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
814 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
815 |
|
|
816 |
|
|
817 |
|
pFrame->intra = 0; |
818 |
|
|
819 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
820 |
|
BitstreamPad(&bs); |
821 |
|
pFrame->length = BitstreamLength(&bs); |
822 |
|
|
823 |
|
return XVID_ERR_OK; |
824 |
|
|
825 |
|
} else { |
826 |
|
|
827 |
|
pFrame->length = BitstreamLength(&bs); |
828 |
|
return XVID_ERR_OK; |
829 |
} |
} |
830 |
|
|
831 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
835 |
* comment style :-) |
* comment style :-) |
836 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
837 |
|
|
|
SWAP(pEnc->current, pEnc->reference); |
|
|
|
|
838 |
emms(); |
emms(); |
839 |
|
|
840 |
|
// only inc frame num, adapt quant, etc. if we havent seen it before |
841 |
|
if (pEnc->bframenum_dx50bvop < 0 ) |
842 |
|
{ |
843 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
844 |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
845 |
else |
else |
853 |
|
|
854 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
855 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
856 |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
|
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
857 |
/* ToDo : dynamic fcode (in both directions) */ |
/* ToDo : dynamic fcode (in both directions) */ |
858 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
859 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
860 |
|
|
861 |
start_timer(); |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
862 |
if (image_input |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
|
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
|
|
return XVID_ERR_FORMAT; |
|
|
stop_conv_timer(); |
|
863 |
|
|
864 |
#ifdef _DEBUG |
inc_frame_num(pEnc); |
865 |
|
|
866 |
|
#ifdef _DEBUG_PSNR |
867 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
868 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
869 |
#endif |
#endif |
870 |
|
|
871 |
emms(); |
emms(); |
872 |
|
|
873 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
874 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
875 |
|
"%i if:%i st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks); |
876 |
|
} |
877 |
|
|
878 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
879 |
* Luminance masking |
* Luminance masking |
880 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
904 |
} |
} |
905 |
|
|
906 |
#undef OFFSET |
#undef OFFSET |
|
|
|
907 |
} |
} |
908 |
|
|
909 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
910 |
} |
} |
911 |
|
|
912 |
|
} |
913 |
|
|
914 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
915 |
* ivop/pvop/bvop selection |
* ivop/pvop/bvop selection |
916 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
917 |
|
|
918 |
|
|
919 |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop != -1 || |
920 |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
921 |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
922 |
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
926 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
927 |
*/ |
*/ |
928 |
|
|
929 |
/* ToDo : Remove dprintf calls */ |
DPRINTF("*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
930 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
931 |
dprintf("--- IFRAME ---"); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
|
*/ |
|
932 |
|
|
933 |
FrameCodeI(pEnc, &bs, &bits); |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
934 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
935 |
|
} |
936 |
|
|
937 |
|
// when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe |
938 |
|
|
939 |
|
if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
940 |
|
|
941 |
|
pEnc->bframenum_tail--; |
942 |
|
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
943 |
|
|
944 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
945 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
946 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
947 |
|
} |
948 |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
949 |
|
|
950 |
|
pFrame->intra = 0; |
951 |
|
|
952 |
|
} else { |
953 |
|
pEnc->bframenum_dx50bvop = -1; |
954 |
|
FrameCodeI(pEnc, &bs, &bits); |
955 |
pFrame->intra = 1; |
pFrame->intra = 1; |
956 |
|
} |
957 |
|
|
958 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
959 |
|
|
960 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
961 |
|
BitstreamPad(&bs); |
962 |
|
input_valid = 0; |
963 |
|
goto ipvop_loop; |
964 |
|
} |
965 |
|
|
966 |
/* |
/* |
967 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
968 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
972 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
973 |
*/ |
*/ |
974 |
|
|
975 |
/* ToDo : Remove dprintf calls */ |
DPRINTF("*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
976 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
977 |
dprintf("--- PFRAME ---"); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
978 |
*/ |
|
979 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
980 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
981 |
|
} |
982 |
|
|
983 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
984 |
pFrame->intra = 0; |
pFrame->intra = 0; |
985 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
986 |
|
|
987 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
988 |
|
BitstreamPad(&bs); |
989 |
|
input_valid = 0; |
990 |
|
goto ipvop_loop; |
991 |
|
} |
992 |
|
|
993 |
} else { |
} else { |
994 |
/* |
/* |
995 |
* This will be coded as a Bidirectional Frame |
* This will be coded as a Bidirectional Frame |
996 |
*/ |
*/ |
997 |
|
|
998 |
/* ToDo : Remove dprintf calls */ |
DPRINTF("*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
999 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1000 |
dprintf("--- BFRAME (store) --- head=%i tail=%i", |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1001 |
pEnc->bframenum_head, |
|
1002 |
pEnc->bframenum_tail); |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
1003 |
*/ |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1004 |
|
} |
1005 |
|
|
1006 |
if (pFrame->bquant < 1) { |
if (pFrame->bquant < 1) { |
1007 |
pEnc->current->quant = |
pEnc->current->quant = |
1019 |
|
|
1020 |
pFrame->intra = 0; |
pFrame->intra = 0; |
1021 |
pFrame->length = 0; |
pFrame->length = 0; |
|
pFrame->input_consumed = 1; |
|
|
|
|
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
|
|
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
|
|
pEnc->mbParam.m_seconds++; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
|
} |
|
1022 |
|
|
1023 |
return XVID_ERR_OK; |
input_valid = 0; |
1024 |
|
goto bvop_loop; |
1025 |
} |
} |
1026 |
|
|
1027 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1037 |
|
|
1038 |
emms(); |
emms(); |
1039 |
|
|
1040 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1041 |
psnr = |
psnr = |
1042 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1043 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1052 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1053 |
} |
} |
1054 |
|
|
|
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; |
|
1055 |
|
|
1056 |
stop_global_timer(); |
stop_global_timer(); |
1057 |
write_timer(); |
write_timer(); |
1058 |
|
|
1059 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1060 |
} |
} |
1061 |
#else |
|
1062 |
|
#endif |
1063 |
|
|
1064 |
|
|
1065 |
|
|
1066 |
/***************************************************************************** |
/***************************************************************************** |
1067 |
* Frame encoder entry point for IP capable encoder |
* "original" IP frame encoder entry point |
1068 |
|
* |
1069 |
|
* Returned values : |
1070 |
|
* - XVID_ERR_OK - no errors |
1071 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
1072 |
|
* format |
1073 |
****************************************************************************/ |
****************************************************************************/ |
1074 |
|
|
1075 |
int |
int |
1076 |
encoder_encode(Encoder * pEnc, |
encoder_encode(Encoder * pEnc, |
1077 |
XVID_ENC_FRAME * pFrame, |
XVID_ENC_FRAME * pFrame, |
1082 |
uint32_t bits; |
uint32_t bits; |
1083 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
1084 |
|
|
1085 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1086 |
float psnr; |
float psnr; |
1087 |
uint8_t temp[128]; |
uint8_t temp[128]; |
1088 |
#endif |
#endif |
1098 |
|
|
1099 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1100 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
1101 |
|
#ifdef BFRAMES |
1102 |
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
1103 |
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
1104 |
|
#endif |
1105 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1106 |
|
|
1107 |
start_timer(); |
start_timer(); |
1111 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
1112 |
stop_conv_timer(); |
stop_conv_timer(); |
1113 |
|
|
1114 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1115 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1116 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1117 |
#endif |
#endif |
1221 |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1222 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1223 |
} |
} |
1224 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1225 |
psnr = |
psnr = |
1226 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1227 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1231 |
DEBUG(temp); |
DEBUG(temp); |
1232 |
#endif |
#endif |
1233 |
|
|
1234 |
|
#ifdef BFRAMES |
1235 |
|
inc_frame_num(pEnc); |
1236 |
|
#else |
1237 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1238 |
|
#endif |
1239 |
|
|
1240 |
|
|
1241 |
stop_global_timer(); |
stop_global_timer(); |
1242 |
write_timer(); |
write_timer(); |
1243 |
|
|
1244 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1245 |
} |
} |
|
#endif |
|
1246 |
|
|
1247 |
|
|
1248 |
static __inline void |
static __inline void |
1490 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1491 |
|
|
1492 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1493 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
#ifdef BFRAMES |
1494 |
|
#define DIVX501B481P "DivX501b481p" |
1495 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
1496 |
|
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1497 |
|
} |
1498 |
|
#endif |
1499 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1500 |
|
|
1501 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1502 |
|
|
1607 |
if (vol_header) |
if (vol_header) |
1608 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1609 |
|
|
1610 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1611 |
|
|
1612 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1613 |
|
|
1765 |
} */ |
} */ |
1766 |
|
|
1767 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1768 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
1769 |
|
|
1770 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1771 |
|
|
1828 |
backward.x = mb->b_mvs[0].x; |
backward.x = mb->b_mvs[0].x; |
1829 |
backward.y = mb->b_mvs[0].y; |
backward.y = mb->b_mvs[0].y; |
1830 |
} |
} |
1831 |
// 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); |
1832 |
|
|
1833 |
start_timer(); |
start_timer(); |
1834 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |