1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Encoder main module - |
* - Encoder main module - |
26 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
27 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
28 |
* |
* |
29 |
***************************************************************************/ |
****************************************************************************/ |
30 |
|
|
31 |
/**************************************************************************** |
/***************************************************************************** |
32 |
* |
* |
33 |
* History |
* History |
34 |
* |
* |
36 |
* MinChen <chenm001@163.com> |
* MinChen <chenm001@163.com> |
37 |
* 14.04.2002 added FrameCodeB() |
* 14.04.2002 added FrameCodeB() |
38 |
* |
* |
39 |
* $Id: encoder.c,v 1.39 2002-06-09 13:16:26 edgomez Exp $ |
* $Id: encoder.c,v 1.40 2002-06-09 23:30:50 edgomez Exp $ |
40 |
* |
* |
41 |
***************************************************************************/ |
****************************************************************************/ |
42 |
|
|
43 |
#include <stdlib.h> |
#include <stdlib.h> |
44 |
#include <stdio.h> |
#include <stdio.h> |
61 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
62 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
63 |
|
|
64 |
|
/***************************************************************************** |
65 |
|
* Local macros |
66 |
|
****************************************************************************/ |
67 |
|
|
68 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
69 |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
70 |
|
|
71 |
|
/***************************************************************************** |
72 |
|
* Local function prototypes |
73 |
|
****************************************************************************/ |
74 |
|
|
75 |
static int FrameCodeI(Encoder * pEnc, |
static int FrameCodeI(Encoder * pEnc, |
76 |
Bitstream * bs, |
Bitstream * bs, |
77 |
uint32_t *pBits); |
uint32_t *pBits); |
89 |
uint32_t *pBits); |
uint32_t *pBits); |
90 |
#endif |
#endif |
91 |
|
|
92 |
|
/***************************************************************************** |
93 |
|
* Local data |
94 |
|
****************************************************************************/ |
95 |
|
|
96 |
static int DQtab[4] = |
static int DQtab[4] = |
97 |
{ |
{ |
98 |
-1, -2, 1, 2 |
-1, -2, 1, 2 |
104 |
}; |
}; |
105 |
|
|
106 |
|
|
107 |
void static image_null(IMAGE * image) |
static void __inline image_null(IMAGE * image) |
108 |
{ |
{ |
109 |
image->y = image->u = image->v = NULL; |
image->y = image->u = image->v = NULL; |
110 |
} |
} |
111 |
|
|
112 |
|
|
113 |
int encoder_create(XVID_ENC_PARAM * pParam) |
/***************************************************************************** |
114 |
|
* Encoder creation |
115 |
|
* |
116 |
|
* This function creates an Encoder instance, it allocates all necessary |
117 |
|
* image buffers (reference, current and bframes) and initialize the internal |
118 |
|
* xvid encoder paremeters according to the XVID_ENC_PARAM input parameter. |
119 |
|
* |
120 |
|
* The code seems to be very long but is very basic, mainly memory allocation |
121 |
|
* and cleaning code. |
122 |
|
* |
123 |
|
* Returned values : |
124 |
|
* - XVID_ERR_OK - no errors |
125 |
|
* - XVID_ERR_MEMORY - the libc could not allocate memory, the function |
126 |
|
* cleans the structure before exiting. |
127 |
|
* pParam->handle is also set to NULL. |
128 |
|
* |
129 |
|
****************************************************************************/ |
130 |
|
|
131 |
|
int |
132 |
|
encoder_create(XVID_ENC_PARAM * pParam) |
133 |
{ |
{ |
134 |
Encoder *pEnc; |
Encoder *pEnc; |
135 |
uint32_t i; |
uint32_t i; |
401 |
|
|
402 |
/* |
/* |
403 |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
|
* |
|
|
* ToDo?: We could avoid that piece of code if encoder_destroy could |
|
|
* handle different destroy levels |
|
404 |
*/ |
*/ |
405 |
#ifdef BFRAMES |
#ifdef BFRAMES |
406 |
xvid_err_memory4: |
xvid_err_memory4: |
473 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
474 |
xvid_free(pEnc); |
xvid_free(pEnc); |
475 |
|
|
476 |
|
pParam->handle = NULL; |
477 |
|
|
478 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
479 |
} |
} |
480 |
|
|
481 |
int encoder_destroy(Encoder * pEnc) |
/***************************************************************************** |
482 |
|
* Encoder destruction |
483 |
|
* |
484 |
|
* This function destroy the entire encoder structure created by a previous |
485 |
|
* successful encoder_create call. |
486 |
|
* |
487 |
|
* Returned values (for now only one returned value) : |
488 |
|
* - XVID_ERR_OK - no errors |
489 |
|
* |
490 |
|
****************************************************************************/ |
491 |
|
|
492 |
|
int |
493 |
|
encoder_destroy(Encoder * pEnc) |
494 |
{ |
{ |
495 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
496 |
|
|
570 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
571 |
} |
} |
572 |
|
|
573 |
|
/***************************************************************************** |
574 |
|
* Frame encoder entry point |
575 |
|
* |
576 |
|
* At this moment 2 versions coexist : one for IPB compatible encoder, |
577 |
|
* another one for the old IP encoder. |
578 |
|
* |
579 |
|
* Returned values : |
580 |
|
* - XVID_ERR_OK - no errors |
581 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
582 |
|
* format |
583 |
|
****************************************************************************/ |
584 |
|
|
585 |
|
|
|
// ================================================================== |
|
586 |
#ifdef BFRAMES |
#ifdef BFRAMES |
587 |
int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) |
/***************************************************************************** |
588 |
|
* Frame encoder entry point for IPB capable encoder |
589 |
|
****************************************************************************/ |
590 |
|
int |
591 |
|
encoder_encode(Encoder * pEnc, |
592 |
|
XVID_ENC_FRAME * pFrame, |
593 |
|
XVID_ENC_STATS * pResult) |
594 |
{ |
{ |
595 |
uint16_t x, y; |
uint16_t x, y; |
596 |
Bitstream bs; |
Bitstream bs; |
608 |
|
|
609 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
610 |
|
|
611 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* |
612 |
// bframe "flush" code |
* bframe "flush" code |
613 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
*/ |
614 |
|
|
615 |
if ((pFrame->image == NULL || pEnc->flush_bframes) && |
if ((pFrame->image == NULL || pEnc->flush_bframes) && |
616 |
pEnc->bframenum_head < pEnc->bframenum_tail) |
(pEnc->bframenum_head < pEnc->bframenum_tail)) |
617 |
{ |
{ |
618 |
|
|
619 |
if (pEnc->flush_bframes == 0) |
if (pEnc->flush_bframes == 0) |
620 |
{ |
{ |
621 |
// we have reached end of stream without getting a future reference |
/* |
622 |
// .. so encode last final frame a pframe |
* we have reached the end of stream without getting |
623 |
//dprintf("--- PFRAME (final frame correction) --- "); |
* a future reference frame... so encode last final |
624 |
|
* frame as a pframe |
625 |
|
*/ |
626 |
|
|
627 |
|
/* ToDo : remove dprintf calls */ |
628 |
|
/* |
629 |
|
dprintf("--- PFRAME (final frame correction) --- "); |
630 |
|
*/ |
631 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
632 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
633 |
|
|
634 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(pEnc->current, |
635 |
|
pEnc->bframes[pEnc->bframenum_tail]); |
636 |
|
|
637 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
638 |
|
|
640 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
641 |
pFrame->input_consumed = 0; |
pFrame->input_consumed = 0; |
642 |
pFrame->intra = 0; |
pFrame->intra = 0; |
643 |
|
|
644 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
645 |
} |
} |
646 |
|
|
647 |
//dprintf("--- BFRAME (flush) --- "); |
/* ToDo : remove dprintf calls */ |
648 |
|
/* |
649 |
|
dprintf("--- BFRAME (flush) --- "); |
650 |
|
*/ |
651 |
FrameCodeB(pEnc, |
FrameCodeB(pEnc, |
652 |
pEnc->bframes[pEnc->bframenum_head], |
pEnc->bframes[pEnc->bframenum_head], |
653 |
&bs, &bits); |
&bs, |
654 |
|
&bits); |
655 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
656 |
|
|
657 |
|
|
659 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
660 |
pFrame->input_consumed = 0; |
pFrame->input_consumed = 0; |
661 |
pFrame->intra = 0; |
pFrame->intra = 0; |
662 |
|
|
663 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
664 |
} |
} |
665 |
|
|
668 |
pFrame->length = 0; |
pFrame->length = 0; |
669 |
pFrame->input_consumed = 1; |
pFrame->input_consumed = 1; |
670 |
pFrame->intra = 0; |
pFrame->intra = 0; |
671 |
|
|
672 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
673 |
} |
} |
674 |
|
|
679 |
|
|
680 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
681 |
|
|
682 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
683 |
|
* Well there was a separation here so i put it in ANSI C |
684 |
|
* comment style :-) |
685 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
686 |
|
|
687 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
688 |
|
|
689 |
pEnc->current->quant = (pFrame->quant == 0) ? RateControlGetQ(&pEnc->rate_control, 0) : pFrame->quant; |
EMMS(); |
690 |
|
|
691 |
|
if (pFrame->quant == 0) |
692 |
|
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
693 |
|
else |
694 |
|
pEnc->current->quant = pFrame->quant; |
695 |
|
|
696 |
if(pEnc->current->quant < 1) |
if(pEnc->current->quant < 1) |
697 |
pEnc->current->quant = 1; |
pEnc->current->quant = 1; |
703 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
704 |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
705 |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
706 |
//@@@ TODO: dyanmic fcode (in both directions) |
/* ToDo : dynamic fcode (in both directions) */ |
707 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
708 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
709 |
|
|
710 |
start_timer(); |
start_timer(); |
711 |
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
if (image_input(&pEnc->current->image, |
712 |
pFrame->image, pFrame->colorspace)) |
pEnc->mbParam.width, |
713 |
{ |
pEnc->mbParam.height, |
714 |
|
pEnc->mbParam.edged_width, |
715 |
|
pFrame->image, |
716 |
|
pFrame->colorspace)) |
717 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
|
} |
|
718 |
stop_conv_timer(); |
stop_conv_timer(); |
719 |
|
|
720 |
#ifdef _DEBUG |
#ifdef _DEBUG |
721 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->sOriginal, |
722 |
|
&pEnc->current->image, |
723 |
|
pEnc->mbParam.edged_width, |
724 |
|
pEnc->mbParam.height); |
725 |
#endif |
#endif |
726 |
|
|
727 |
|
EMMS(); |
728 |
|
|
729 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
730 |
// lumi masking |
* Luminance masking |
731 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
732 |
|
|
733 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
734 |
{ |
{ |
735 |
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
int *temp_dquants = |
736 |
|
(int *) xvid_malloc(pEnc->mbParam.mb_width * \ |
737 |
|
pEnc->mbParam.mb_height * \ |
738 |
|
sizeof(int), |
739 |
|
CACHE_LINE); |
740 |
|
|
741 |
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
pEnc->current->quant = |
742 |
pEnc->mbParam.edged_width, // stride |
adaptive_quantization(pEnc->current->image.y, |
743 |
|
pEnc->mbParam.edged_width, |
744 |
temp_dquants, |
temp_dquants, |
745 |
pEnc->current->quant, |
pEnc->current->quant, |
746 |
pEnc->current->quant, // min_quant |
pEnc->current->quant, |
747 |
2*pEnc->current->quant, // max_quant |
2*pEnc->current->quant, |
748 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
749 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
750 |
|
|
751 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
752 |
|
{ |
753 |
|
|
754 |
|
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
755 |
|
|
756 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
757 |
{ |
{ |
758 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK *pMB = |
759 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
&pEnc->current->mbs[OFFSET(x,y)]; |
760 |
|
pMB->dquant = |
761 |
|
iDQtab[temp_dquants[OFFSET(x,y)] + 2]; |
762 |
} |
} |
763 |
|
|
764 |
|
#undef OFFSET |
765 |
|
|
766 |
|
} |
767 |
|
|
768 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
769 |
} |
} |
770 |
|
|
771 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
772 |
|
* ivop/pvop/bvop selection |
773 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
774 |
|
|
|
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
// ivop/pvop/bvop selection |
|
|
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
775 |
|
|
776 |
if (pEnc->iFrameNum == 0 || |
if (pEnc->iFrameNum == 0 || |
777 |
pFrame->intra == 1 || |
pFrame->intra == 1 || |
778 |
(pFrame->intra < 0 && (pEnc->iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->iMaxKeyInterval)) || |
|
779 |
image_mad(&pEnc->reference->image, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.width, pEnc->mbParam.height) > 30) |
(pFrame->intra < 0 && |
780 |
|
pEnc->iMaxKeyInterval > 0 && |
781 |
|
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) || |
782 |
|
|
783 |
|
image_mad(&pEnc->reference->image, |
784 |
|
&pEnc->current->image, |
785 |
|
pEnc->mbParam.edged_width, |
786 |
|
pEnc->mbParam.width, |
787 |
|
pEnc->mbParam.height) > 30) |
788 |
{ |
{ |
789 |
//dprintf("--- IFRAME ---"); |
/* |
790 |
|
* This will be coded as an Intra Frame |
791 |
|
*/ |
792 |
|
|
793 |
|
/* ToDo : Remove dprintf calls */ |
794 |
|
/* |
795 |
|
dprintf("--- IFRAME ---"); |
796 |
|
*/ |
797 |
|
|
798 |
FrameCodeI(pEnc, &bs, &bits); |
FrameCodeI(pEnc, &bs, &bits); |
799 |
|
|
800 |
pFrame->intra = 1; |
pFrame->intra = 1; |
801 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
802 |
|
|
803 |
/* note: sequences like "IIBB" decode fine with msfdam but, |
/* |
804 |
go screwy with divx5.00 */ |
* NB : sequences like "IIBB" decode fine with msfdam but, |
805 |
|
* go screwy with divx 5.00 |
806 |
|
*/ |
807 |
} |
} |
808 |
else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) |
else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) |
809 |
{ |
{ |
810 |
//dprintf("--- PFRAME ---"); |
/* |
811 |
|
* This will be coded as a Predicted Frame |
812 |
|
*/ |
813 |
|
|
814 |
|
/* ToDo : Remove dprintf calls */ |
815 |
|
/* |
816 |
|
dprintf("--- PFRAME ---"); |
817 |
|
*/ |
818 |
|
|
819 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
820 |
pFrame->intra = 0; |
pFrame->intra = 0; |
822 |
} |
} |
823 |
else |
else |
824 |
{ |
{ |
825 |
//dprintf("--- BFRAME (store) --- head=%i tail=%i", pEnc->bframenum_head, pEnc->bframenum_tail); |
/* |
826 |
|
* This will be coded as a Bidirectional Frame |
827 |
|
*/ |
828 |
|
|
829 |
|
/* ToDo : Remove dprintf calls */ |
830 |
|
/* |
831 |
|
dprintf("--- BFRAME (store) --- head=%i tail=%i", |
832 |
|
pEnc->bframenum_head, |
833 |
|
pEnc->bframenum_tail); |
834 |
|
*/ |
835 |
|
|
836 |
if (pFrame->bquant < 1) |
if (pFrame->bquant < 1) |
837 |
{ |
{ |
838 |
pEnc->current->quant = ((pEnc->reference->quant + pEnc->current->quant) * pEnc->bquant_ratio) / 200; |
pEnc->current->quant = |
839 |
|
((pEnc->reference->quant+pEnc->current->quant)*\ |
840 |
|
pEnc->bquant_ratio) / 200; |
841 |
} |
} |
842 |
else |
else |
843 |
{ |
{ |
844 |
pEnc->current->quant = pFrame->bquant; |
pEnc->current->quant = pFrame->bquant; |
845 |
} |
} |
846 |
|
|
847 |
// store frame into bframe buffer & swap ref back to current |
/* store frame into bframe buffer & swap ref back to current */ |
848 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
849 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
850 |
|
|
860 |
pEnc->mbParam.m_seconds++; |
pEnc->mbParam.m_seconds++; |
861 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
862 |
} |
} |
863 |
|
|
864 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
865 |
} |
} |
866 |
|
|
876 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
877 |
} |
} |
878 |
|
|
879 |
|
EMMS(); |
880 |
|
|
881 |
#ifdef _DEBUG |
#ifdef _DEBUG |
882 |
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
psnr = image_psnr(&pEnc->sOriginal, |
883 |
pEnc->mbParam.width, pEnc->mbParam.height); |
&pEnc->current->image, |
884 |
|
pEnc->mbParam.edged_width, |
885 |
|
pEnc->mbParam.width, |
886 |
|
pEnc->mbParam.height); |
887 |
|
|
888 |
sprintf(temp, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
889 |
DEBUG(temp); |
DEBUG(temp); |
890 |
#endif |
#endif |
891 |
|
|
911 |
|
|
912 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
913 |
} |
} |
|
// ================================================================== |
|
914 |
#else |
#else |
915 |
int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) |
/***************************************************************************** |
916 |
|
* Frame encoder entry point for IP capable encoder |
917 |
|
****************************************************************************/ |
918 |
|
int |
919 |
|
encoder_encode(Encoder * pEnc, |
920 |
|
XVID_ENC_FRAME * pFrame, |
921 |
|
XVID_ENC_STATS * pResult) |
922 |
{ |
{ |
923 |
uint16_t x, y; |
uint16_t x, y; |
924 |
Bitstream bs; |
Bitstream bs; |
926 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
927 |
#ifdef _DEBUG |
#ifdef _DEBUG |
928 |
float psnr; |
float psnr; |
929 |
uint8_t temp[100]; |
uint8_t temp[128]; |
930 |
#endif |
#endif |
931 |
|
|
932 |
start_global_timer(); |
start_global_timer(); |
943 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
944 |
|
|
945 |
start_timer(); |
start_timer(); |
946 |
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
if (image_input(&pEnc->current->image, |
947 |
pFrame->image, pFrame->colorspace)) |
pEnc->mbParam.width, |
948 |
{ |
pEnc->mbParam.height, |
949 |
|
pEnc->mbParam.edged_width, |
950 |
|
pFrame->image, |
951 |
|
pFrame->colorspace) < 0) |
952 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
|
} |
|
953 |
stop_conv_timer(); |
stop_conv_timer(); |
954 |
|
|
955 |
#ifdef _DEBUG |
#ifdef _DEBUG |
956 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->sOriginal, |
957 |
|
&pEnc->current->image, |
958 |
|
pEnc->mbParam.edged_width, |
959 |
|
pEnc->mbParam.height); |
960 |
#endif |
#endif |
961 |
|
|
962 |
EMMS(); |
EMMS(); |
974 |
|
|
975 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
976 |
{ |
{ |
977 |
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
int * temp_dquants = (int *) |
978 |
|
xvid_malloc(pEnc->mbParam.mb_width * \ |
979 |
|
pEnc->mbParam.mb_height * \ |
980 |
|
sizeof(int), |
981 |
|
CACHE_LINE); |
982 |
|
|
983 |
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
pEnc->current->quant = |
984 |
pEnc->mbParam.edged_width, // stride |
adaptive_quantization(pEnc->current->image.y, |
985 |
|
pEnc->mbParam.edged_width, |
986 |
temp_dquants, |
temp_dquants, |
987 |
pEnc->current->quant, |
pEnc->current->quant, |
988 |
pEnc->current->quant, // min_quant |
pEnc->current->quant, |
989 |
2*pEnc->current->quant, // max_quant |
2*pEnc->current->quant, |
990 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
991 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
992 |
|
|
993 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
994 |
|
{ |
995 |
|
|
996 |
|
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
997 |
|
|
998 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
999 |
{ |
{ |
1000 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1001 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
|
1002 |
|
MACROBLOCK *pMB = |
1003 |
|
&pEnc->current->mbs[OFFSET(x,y)]; |
1004 |
|
pMB->dquant = |
1005 |
|
iDQtab[temp_dquants[OFFSET(x,y)] + 2]; |
1006 |
} |
} |
1007 |
|
|
1008 |
|
#undef OFFSET |
1009 |
|
} |
1010 |
|
|
1011 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
1012 |
} |
} |
1013 |
|
|
1014 |
if (pEnc->current->global_flags & XVID_H263QUANT) { |
if (pEnc->current->global_flags & XVID_H263QUANT) |
1015 |
|
{ |
1016 |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
1017 |
write_vol_header = 1; |
write_vol_header = 1; |
1018 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
1019 |
} |
} |
1020 |
else if(pEnc->current->global_flags & XVID_MPEGQUANT) { |
else if (pEnc->current->global_flags & XVID_MPEGQUANT) |
1021 |
int ret1, ret2; |
{ |
1022 |
|
int matrix1_changed, matrix2_changed; |
1023 |
|
|
1024 |
ret1 = ret2 = 0; |
matrix1_changed = matrix2_changed = 0; |
1025 |
|
|
1026 |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
1027 |
write_vol_header = 1; |
write_vol_header = 1; |
1030 |
|
|
1031 |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
1032 |
if(pFrame->quant_intra_matrix != NULL) |
if(pFrame->quant_intra_matrix != NULL) |
1033 |
ret1 = set_intra_matrix(pFrame->quant_intra_matrix); |
matrix1_changed = |
1034 |
|
set_intra_matrix(pFrame->quant_intra_matrix); |
1035 |
if(pFrame->quant_inter_matrix != NULL) |
if(pFrame->quant_inter_matrix != NULL) |
1036 |
ret2 = set_inter_matrix(pFrame->quant_inter_matrix); |
matrix2_changed |
1037 |
|
= set_inter_matrix(pFrame->quant_inter_matrix); |
1038 |
} |
} |
1039 |
else { |
else { |
1040 |
ret1 = set_intra_matrix(get_default_intra_matrix()); |
matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
1041 |
ret2 = set_inter_matrix(get_default_inter_matrix()); |
matrix2_changed = set_inter_matrix(get_default_inter_matrix()); |
1042 |
} |
} |
1043 |
if(write_vol_header == 0) |
if(write_vol_header == 0) |
1044 |
write_vol_header = ret1 | ret2; |
write_vol_header = matrix1_changed | matrix2_changed; |
1045 |
} |
} |
1046 |
|
|
1047 |
if (pFrame->intra < 0) |
if (pFrame->intra < 0) |
1048 |
{ |
{ |
1049 |
if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0) |
if ((pEnc->iFrameNum == 0) || |
|
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
|
1050 |
|
|
1051 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
((pEnc->iMaxKeyInterval > 0) && |
1052 |
|
(pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
1053 |
|
{ |
1054 |
|
pFrame->intra = FrameCodeI(pEnc, |
1055 |
|
&bs, |
1056 |
|
&bits); |
1057 |
|
} |
1058 |
else |
else |
1059 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
{ |
1060 |
|
pFrame->intra = FrameCodeP(pEnc, |
1061 |
|
&bs, |
1062 |
|
&bits, |
1063 |
|
0, |
1064 |
|
write_vol_header); |
1065 |
|
} |
1066 |
} |
} |
1067 |
else |
else |
1068 |
{ |
{ |
1069 |
if (pFrame->intra == 1) |
if (pFrame->intra == 1) |
1070 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
{ |
1071 |
|
pFrame->intra = FrameCodeI(pEnc, |
1072 |
|
&bs, |
1073 |
|
&bits); |
1074 |
|
} |
1075 |
else |
else |
1076 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
{ |
1077 |
|
pFrame->intra = FrameCodeP(pEnc, |
1078 |
|
&bs, |
1079 |
|
&bits, |
1080 |
|
1, |
1081 |
|
write_vol_header); |
1082 |
|
} |
1083 |
|
|
1084 |
} |
} |
1085 |
|
|
1086 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
1108 |
} |
} |
1109 |
|
|
1110 |
#ifdef _DEBUG |
#ifdef _DEBUG |
1111 |
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
psnr = image_psnr(&pEnc->sOriginal, |
1112 |
pEnc->mbParam.width, pEnc->mbParam.height); |
&pEnc->current->image, |
1113 |
|
pEnc->mbParam.edged_width, |
1114 |
|
pEnc->mbParam.width, |
1115 |
|
pEnc->mbParam.height); |
1116 |
|
|
1117 |
sprintf(temp, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
1118 |
DEBUG(temp); |
DEBUG(temp); |
1119 |
#endif |
#endif |
1120 |
|
|