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() |
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 |
|
#include "image/font.h" |
55 |
#include "motion/motion.h" |
#include "motion/motion.h" |
56 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
57 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
134 |
encoder_create(XVID_ENC_PARAM * pParam) |
encoder_create(XVID_ENC_PARAM * pParam) |
135 |
{ |
{ |
136 |
Encoder *pEnc; |
Encoder *pEnc; |
137 |
uint32_t i; |
int i; |
138 |
|
|
139 |
pParam->handle = NULL; |
pParam->handle = NULL; |
140 |
|
|
261 |
|
|
262 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
263 |
|
|
264 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
265 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
266 |
#endif |
#endif |
267 |
#ifdef BFRAMES |
#ifdef BFRAMES |
277 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
278 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
279 |
|
|
280 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
281 |
if (image_create |
if (image_create |
282 |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
283 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
331 |
/* B Frames specific init */ |
/* B Frames specific init */ |
332 |
#ifdef BFRAMES |
#ifdef BFRAMES |
333 |
|
|
334 |
|
pEnc->global = pParam->global; |
335 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
336 |
pEnc->bquant_ratio = pParam->bquant_ratio; |
pEnc->bquant_ratio = pParam->bquant_ratio; |
337 |
pEnc->bframes = NULL; |
pEnc->bframes = NULL; |
377 |
pEnc->bframenum_tail = 0; |
pEnc->bframenum_tail = 0; |
378 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
379 |
|
|
380 |
|
pEnc->queue = NULL; |
381 |
|
|
382 |
|
|
383 |
|
if (pEnc->mbParam.max_bframes > 0) { |
384 |
|
int n; |
385 |
|
|
386 |
|
pEnc->queue = |
387 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE), |
388 |
|
CACHE_LINE); |
389 |
|
|
390 |
|
if (pEnc->queue == NULL) |
391 |
|
goto xvid_err_memory4; |
392 |
|
|
393 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
394 |
|
image_null(&pEnc->queue[n]); |
395 |
|
|
396 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
397 |
|
if (image_create |
398 |
|
(&pEnc->queue[n], pEnc->mbParam.edged_width, |
399 |
|
pEnc->mbParam.edged_height) < 0) |
400 |
|
goto xvid_err_memory5; |
401 |
|
|
402 |
|
} |
403 |
|
} |
404 |
|
|
405 |
|
pEnc->queue_head = 0; |
406 |
|
pEnc->queue_tail = 0; |
407 |
|
pEnc->queue_size = 0; |
408 |
|
|
409 |
|
|
410 |
pEnc->mbParam.m_seconds = 0; |
pEnc->mbParam.m_seconds = 0; |
411 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
412 |
|
pEnc->m_framenum = 0; |
413 |
#endif |
#endif |
414 |
|
|
415 |
pParam->handle = (void *) pEnc; |
pParam->handle = (void *) pEnc; |
430 |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
431 |
*/ |
*/ |
432 |
#ifdef BFRAMES |
#ifdef BFRAMES |
433 |
|
xvid_err_memory5: |
434 |
|
|
435 |
|
|
436 |
|
if (pEnc->mbParam.max_bframes > 0) { |
437 |
|
|
438 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
439 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
440 |
|
pEnc->mbParam.edged_height); |
441 |
|
} |
442 |
|
xvid_free(pEnc->queue); |
443 |
|
} |
444 |
|
|
445 |
xvid_err_memory4: |
xvid_err_memory4: |
446 |
|
|
447 |
|
if (pEnc->mbParam.max_bframes > 0) { |
448 |
|
|
449 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
450 |
|
|
451 |
if (pEnc->bframes[i] == NULL) |
if (pEnc->bframes[i] == NULL) |
461 |
} |
} |
462 |
|
|
463 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
464 |
|
} |
465 |
|
|
466 |
#endif |
#endif |
467 |
|
|
468 |
xvid_err_memory3: |
xvid_err_memory3: |
469 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
470 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
471 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
472 |
#endif |
#endif |
523 |
int |
int |
524 |
encoder_destroy(Encoder * pEnc) |
encoder_destroy(Encoder * pEnc) |
525 |
{ |
{ |
526 |
|
#ifdef BFRAMES |
527 |
|
int i; |
528 |
|
#endif |
529 |
|
|
530 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
531 |
|
|
532 |
/* B Frames specific */ |
/* B Frames specific */ |
533 |
#ifdef BFRAMES |
#ifdef BFRAMES |
534 |
int i; |
if (pEnc->mbParam.max_bframes > 0) { |
535 |
|
|
536 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
537 |
|
|
538 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
539 |
|
pEnc->mbParam.edged_height); |
540 |
|
} |
541 |
|
xvid_free(pEnc->queue); |
542 |
|
} |
543 |
|
|
544 |
|
|
545 |
|
if (pEnc->mbParam.max_bframes > 0) { |
546 |
|
|
547 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
548 |
|
|
555 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
556 |
|
|
557 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
|
|
|
558 |
} |
} |
559 |
|
|
560 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
561 |
|
|
562 |
|
} |
563 |
#endif |
#endif |
564 |
|
|
565 |
/* All images, reference, current etc ... */ |
/* All images, reference, current etc ... */ |
586 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
587 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
588 |
#endif |
#endif |
589 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
590 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
591 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
592 |
#endif |
#endif |
604 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
605 |
} |
} |
606 |
|
|
607 |
|
|
608 |
|
#ifdef BFRAMES |
609 |
|
void inc_frame_num(Encoder * pEnc) |
610 |
|
{ |
611 |
|
pEnc->iFrameNum++; |
612 |
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
613 |
|
|
614 |
|
pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
615 |
|
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
616 |
|
} |
617 |
|
#endif |
618 |
|
|
619 |
|
|
620 |
|
#ifdef BFRAMES |
621 |
|
void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
622 |
|
{ |
623 |
|
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
624 |
|
{ |
625 |
|
DPRINTF("FATAL: QUEUE FULL"); |
626 |
|
return; |
627 |
|
} |
628 |
|
|
629 |
|
DPRINTF("*** QUEUE bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
630 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
631 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
632 |
|
|
633 |
|
|
634 |
|
start_timer(); |
635 |
|
if (image_input |
636 |
|
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
637 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
638 |
|
return; |
639 |
|
stop_conv_timer(); |
640 |
|
|
641 |
|
pEnc->queue_size++; |
642 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
643 |
|
} |
644 |
|
#endif |
645 |
|
|
646 |
|
|
647 |
|
#ifdef BFRAMES |
648 |
/***************************************************************************** |
/***************************************************************************** |
649 |
* 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. |
|
650 |
* |
* |
651 |
* Returned values : |
* Returned values : |
652 |
* - XVID_ERR_OK - no errors |
* - XVID_ERR_OK - no errors |
654 |
* format |
* format |
655 |
****************************************************************************/ |
****************************************************************************/ |
656 |
|
|
|
|
|
|
#ifdef BFRAMES |
|
|
/***************************************************************************** |
|
|
* Frame encoder entry point for IPB capable encoder |
|
|
****************************************************************************/ |
|
657 |
int |
int |
658 |
encoder_encode(Encoder * pEnc, |
encoder_encode_bframes(Encoder * pEnc, |
659 |
XVID_ENC_FRAME * pFrame, |
XVID_ENC_FRAME * pFrame, |
660 |
XVID_ENC_STATS * pResult) |
XVID_ENC_STATS * pResult) |
661 |
{ |
{ |
663 |
Bitstream bs; |
Bitstream bs; |
664 |
uint32_t bits; |
uint32_t bits; |
665 |
|
|
666 |
#ifdef _DEBUG |
int input_valid = 1; |
667 |
|
|
668 |
|
#ifdef _DEBUG_PSNR |
669 |
float psnr; |
float psnr; |
670 |
char temp[128]; |
char temp[128]; |
671 |
#endif |
#endif |
672 |
|
|
673 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
674 |
ENC_CHECK(pFrame); |
ENC_CHECK(pFrame); |
675 |
|
ENC_CHECK(pFrame->image); |
676 |
|
|
677 |
start_global_timer(); |
start_global_timer(); |
678 |
|
|
679 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
680 |
|
|
681 |
|
ipvop_loop: |
682 |
|
|
683 |
/* |
/* |
684 |
* bframe "flush" code |
* bframe "flush" code |
685 |
*/ |
*/ |
694 |
* frame as a pframe |
* frame as a pframe |
695 |
*/ |
*/ |
696 |
|
|
697 |
/* ToDo : remove dprintf calls */ |
DPRINTF("*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
698 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
699 |
dprintf("--- PFRAME (final frame correction) --- "); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
700 |
*/ |
|
701 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
702 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
703 |
|
|
707 |
|
|
708 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
709 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
710 |
pFrame->intra = 0; |
pFrame->intra = 0; |
711 |
|
|
712 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
713 |
} |
} |
714 |
|
|
715 |
/* ToDo : remove dprintf calls */ |
|
716 |
/* |
DPRINTF("*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
717 |
dprintf("--- BFRAME (flush) --- "); |
pEnc->bframenum_head, pEnc->bframenum_tail, |
718 |
*/ |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
719 |
|
|
720 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
721 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
722 |
|
|
|
|
|
723 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
724 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
725 |
pFrame->intra = 0; |
pFrame->intra = 0; |
726 |
|
|
727 |
|
if (input_valid) |
728 |
|
queue_image(pEnc, pFrame); |
729 |
|
|
730 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
731 |
} |
} |
732 |
|
|
733 |
if (pFrame->image == NULL) { |
if (pEnc->bframenum_head > 0) { |
734 |
pFrame->length = 0; |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
735 |
pFrame->input_consumed = 1; |
|
736 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
737 |
|
|
738 |
|
DPRINTF("*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
739 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
740 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
741 |
|
|
742 |
|
|
743 |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
744 |
|
BitstreamPad(&bs); |
745 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
746 |
|
|
747 |
|
pFrame->length = BitstreamLength(&bs); |
748 |
pFrame->intra = 0; |
pFrame->intra = 0; |
749 |
|
|
750 |
|
if (input_valid) |
751 |
|
queue_image(pEnc, pFrame); |
752 |
|
|
753 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
754 |
} |
} |
755 |
|
} |
756 |
|
|
757 |
if (pEnc->bframenum_head > 0) { |
|
758 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
bvop_loop: |
759 |
|
|
760 |
|
if (input_valid) { |
761 |
|
|
762 |
|
SWAP(pEnc->current, pEnc->reference); |
763 |
|
|
764 |
|
start_timer(); |
765 |
|
if (image_input |
766 |
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
767 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
768 |
|
return XVID_ERR_FORMAT; |
769 |
|
stop_conv_timer(); |
770 |
|
|
771 |
|
// queue input frame, and dequue next image |
772 |
|
if (pEnc->queue_size > 0) |
773 |
|
{ |
774 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); |
775 |
|
if (pEnc->queue_head != pEnc->queue_tail) |
776 |
|
{ |
777 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
778 |
|
} |
779 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
780 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
781 |
|
} |
782 |
|
|
783 |
|
} else if (pEnc->queue_size > 0) { |
784 |
|
|
785 |
|
SWAP(pEnc->current, pEnc->reference); |
786 |
|
|
787 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
788 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
789 |
|
pEnc->queue_size--; |
790 |
|
|
791 |
|
} else if (BitstreamPos(&bs) == 0) { |
792 |
|
|
793 |
|
DPRINTF("*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
794 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
795 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
796 |
|
|
797 |
|
|
798 |
|
pFrame->intra = 0; |
799 |
|
|
800 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
801 |
|
BitstreamPad(&bs); |
802 |
|
pFrame->length = BitstreamLength(&bs); |
803 |
|
|
804 |
|
return XVID_ERR_OK; |
805 |
|
|
806 |
|
} else { |
807 |
|
|
808 |
|
pFrame->length = BitstreamLength(&bs); |
809 |
|
return XVID_ERR_OK; |
810 |
} |
} |
811 |
|
|
812 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
816 |
* comment style :-) |
* comment style :-) |
817 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
818 |
|
|
819 |
SWAP(pEnc->current, pEnc->reference); |
//$$ SWAP(pEnc->current, pEnc->reference); |
820 |
|
|
821 |
emms(); |
emms(); |
822 |
|
|
839 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
840 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
841 |
|
|
842 |
start_timer(); |
//$$$ start_timer(); |
843 |
if (image_input |
//$$$ if (image_input |
844 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
//$$$ (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
845 |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
//$$$ pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
846 |
return XVID_ERR_FORMAT; |
//$$$ return XVID_ERR_FORMAT; |
847 |
stop_conv_timer(); |
//$$$ stop_conv_timer(); |
848 |
|
|
849 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
850 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
851 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
852 |
#endif |
#endif |
853 |
|
|
854 |
emms(); |
emms(); |
855 |
|
|
856 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
857 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
858 |
|
"%i if:%i st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks); |
859 |
|
} |
860 |
|
|
861 |
|
|
862 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
863 |
* Luminance masking |
* Luminance masking |
864 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
909 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
910 |
*/ |
*/ |
911 |
|
|
912 |
/* ToDo : Remove dprintf calls */ |
DPRINTF("*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
913 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
914 |
dprintf("--- IFRAME ---"); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
915 |
*/ |
|
916 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
917 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
918 |
|
} |
919 |
|
|
920 |
FrameCodeI(pEnc, &bs, &bits); |
FrameCodeI(pEnc, &bs, &bits); |
921 |
|
|
922 |
pFrame->intra = 1; |
pFrame->intra = 1; |
923 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
924 |
|
|
925 |
|
inc_frame_num(pEnc); |
926 |
|
|
927 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
928 |
|
BitstreamPad(&bs); |
929 |
|
input_valid = 0; |
930 |
|
goto ipvop_loop; |
931 |
|
} |
932 |
|
|
933 |
/* |
/* |
934 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
935 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
939 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
940 |
*/ |
*/ |
941 |
|
|
942 |
/* ToDo : Remove dprintf calls */ |
DPRINTF("*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
943 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
944 |
dprintf("--- PFRAME ---"); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
945 |
*/ |
|
946 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
947 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
948 |
|
} |
949 |
|
|
950 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
951 |
pFrame->intra = 0; |
pFrame->intra = 0; |
952 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
953 |
|
|
954 |
|
inc_frame_num(pEnc); |
955 |
|
|
956 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
957 |
|
BitstreamPad(&bs); |
958 |
|
input_valid = 0; |
959 |
|
goto ipvop_loop; |
960 |
|
} |
961 |
|
|
962 |
} else { |
} else { |
963 |
/* |
/* |
964 |
* This will be coded as a Bidirectional Frame |
* This will be coded as a Bidirectional Frame |
965 |
*/ |
*/ |
966 |
|
|
967 |
/* ToDo : Remove dprintf calls */ |
DPRINTF("*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
968 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
969 |
dprintf("--- BFRAME (store) --- head=%i tail=%i", |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
970 |
pEnc->bframenum_head, |
|
971 |
pEnc->bframenum_tail); |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
972 |
*/ |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
973 |
|
} |
974 |
|
|
975 |
if (pFrame->bquant < 1) { |
if (pFrame->bquant < 1) { |
976 |
pEnc->current->quant = |
pEnc->current->quant = |
988 |
|
|
989 |
pFrame->intra = 0; |
pFrame->intra = 0; |
990 |
pFrame->length = 0; |
pFrame->length = 0; |
|
pFrame->input_consumed = 1; |
|
991 |
|
|
992 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
inc_frame_num(pEnc); |
|
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
|
|
pEnc->mbParam.m_seconds++; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
|
} |
|
993 |
|
|
994 |
return XVID_ERR_OK; |
input_valid = 0; |
995 |
|
goto bvop_loop; |
996 |
} |
} |
997 |
|
|
998 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1008 |
|
|
1009 |
emms(); |
emms(); |
1010 |
|
|
1011 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1012 |
psnr = |
psnr = |
1013 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1014 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1023 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1024 |
} |
} |
1025 |
|
|
|
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; |
|
1026 |
|
|
1027 |
stop_global_timer(); |
stop_global_timer(); |
1028 |
write_timer(); |
write_timer(); |
1029 |
|
|
1030 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1031 |
} |
} |
1032 |
#else |
|
1033 |
|
#endif |
1034 |
|
|
1035 |
|
|
1036 |
|
|
1037 |
/***************************************************************************** |
/***************************************************************************** |
1038 |
* Frame encoder entry point for IP capable encoder |
* "original" IP frame encoder entry point |
1039 |
|
* |
1040 |
|
* Returned values : |
1041 |
|
* - XVID_ERR_OK - no errors |
1042 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
1043 |
|
* format |
1044 |
****************************************************************************/ |
****************************************************************************/ |
1045 |
|
|
1046 |
int |
int |
1047 |
encoder_encode(Encoder * pEnc, |
encoder_encode(Encoder * pEnc, |
1048 |
XVID_ENC_FRAME * pFrame, |
XVID_ENC_FRAME * pFrame, |
1053 |
uint32_t bits; |
uint32_t bits; |
1054 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
1055 |
|
|
1056 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1057 |
float psnr; |
float psnr; |
1058 |
uint8_t temp[128]; |
uint8_t temp[128]; |
1059 |
#endif |
#endif |
1069 |
|
|
1070 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1071 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
1072 |
|
#ifdef BFRAMES |
1073 |
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
1074 |
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
1075 |
|
#endif |
1076 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1077 |
|
|
1078 |
start_timer(); |
start_timer(); |
1082 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
1083 |
stop_conv_timer(); |
stop_conv_timer(); |
1084 |
|
|
1085 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1086 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1087 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1088 |
#endif |
#endif |
1192 |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1193 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1194 |
} |
} |
1195 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1196 |
psnr = |
psnr = |
1197 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1198 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1202 |
DEBUG(temp); |
DEBUG(temp); |
1203 |
#endif |
#endif |
1204 |
|
|
1205 |
|
#ifdef BFRAMES |
1206 |
|
inc_frame_num(pEnc); |
1207 |
|
#else |
1208 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1209 |
|
#endif |
1210 |
|
|
1211 |
|
|
1212 |
stop_global_timer(); |
stop_global_timer(); |
1213 |
write_timer(); |
write_timer(); |
1214 |
|
|
1215 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1216 |
} |
} |
|
#endif |
|
1217 |
|
|
1218 |
|
|
1219 |
static __inline void |
static __inline void |
1461 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1462 |
|
|
1463 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1464 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
#ifdef BFRAMES |
1465 |
|
#define DIVX501B481P "DivX501b481p" |
1466 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
1467 |
|
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1468 |
|
} |
1469 |
|
#endif |
1470 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1471 |
|
|
1472 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1473 |
|
|
1578 |
if (vol_header) |
if (vol_header) |
1579 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1580 |
|
|
1581 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1582 |
|
|
1583 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1584 |
|
|
1736 |
} */ |
} */ |
1737 |
|
|
1738 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1739 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
1740 |
|
|
1741 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1742 |
|
|
1799 |
backward.x = mb->b_mvs[0].x; |
backward.x = mb->b_mvs[0].x; |
1800 |
backward.y = mb->b_mvs[0].y; |
backward.y = mb->b_mvs[0].y; |
1801 |
} |
} |
1802 |
// 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); |
1803 |
|
|
1804 |
start_timer(); |
start_timer(); |
1805 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |