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 |
* $Id: encoder.c,v 1.95.2.8 2003-03-15 17:06:53 suxen_drol Exp $ |
* $Id: encoder.c,v 1.95.2.17 2003-03-30 00:36:53 edgomez Exp $ |
30 |
* |
* |
31 |
****************************************************************************/ |
****************************************************************************/ |
32 |
|
|
54 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
55 |
|
|
56 |
/***************************************************************************** |
/***************************************************************************** |
|
* Local macros |
|
|
****************************************************************************/ |
|
|
|
|
|
#define SWAP(_T_,A,B) { _T_ tmp = A; A = B; B = tmp; } |
|
|
|
|
|
/***************************************************************************** |
|
57 |
* Local function prototypes |
* Local function prototypes |
58 |
****************************************************************************/ |
****************************************************************************/ |
59 |
|
|
69 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
70 |
Bitstream * bs); |
Bitstream * bs); |
71 |
|
|
|
/***************************************************************************** |
|
|
* Local data |
|
|
****************************************************************************/ |
|
|
|
|
|
static int DQtab[4] = { |
|
|
-1, -2, 1, 2 |
|
|
}; |
|
|
|
|
|
static int iDQtab[5] = { |
|
|
1, 0, NO_CHANGE, 2, 3 |
|
|
}; |
|
|
|
|
72 |
|
|
73 |
/***************************************************************************** |
/***************************************************************************** |
74 |
* Encoder creation |
* Encoder creation |
116 |
|
|
117 |
|
|
118 |
int |
int |
119 |
enc_create(xvid_enc_create_t * create, xvid_enc_rc_t * rc) |
enc_create(xvid_enc_create_t * create) |
120 |
{ |
{ |
121 |
Encoder *pEnc; |
Encoder *pEnc; |
122 |
int n; |
int n; |
123 |
|
|
124 |
if (XVID_MAJOR(create->version) != 1 || (rc && XVID_MAJOR(rc->version) != 1)) /* v1.x.x */ |
if (XVID_MAJOR(create->version) != 1) /* v1.x.x */ |
125 |
return XVID_ERR_VERSION; |
return XVID_ERR_VERSION; |
126 |
|
|
127 |
if (create->width%2 || create->height%2) |
if (create->width%2 || create->height%2) |
181 |
} |
} |
182 |
} |
} |
183 |
|
|
184 |
if ((pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) || |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_EXTRASTATS_ENABLE) || |
185 |
(pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { |
(pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { |
186 |
pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */ |
pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */ |
187 |
} |
} |
188 |
|
|
189 |
/* temp dquants */ |
/* temp dquants */ |
190 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
191 |
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
192 |
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
193 |
|
} |
194 |
/* XXX: error checking */ |
/* XXX: error checking */ |
195 |
|
|
196 |
/* bframes */ |
/* bframes */ |
488 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
489 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
490 |
|
|
491 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
492 |
xvid_free(pEnc->temp_dquants); |
xvid_free(pEnc->temp_dquants); |
493 |
|
} |
494 |
|
|
495 |
xvid_err_memory0: |
xvid_err_memory0: |
496 |
for (n=0; n<pEnc->num_plugins;n++) { |
for (n=0; n<pEnc->num_plugins;n++) { |
593 |
xvid_free(pEnc->reference->mbs); |
xvid_free(pEnc->reference->mbs); |
594 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
595 |
|
|
596 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
597 |
xvid_free(pEnc->temp_dquants); |
xvid_free(pEnc->temp_dquants); |
598 |
|
} |
599 |
|
|
600 |
|
|
601 |
|
if (pEnc->num_plugins>0) |
602 |
|
{ |
603 |
|
xvid_plg_destroy_t pdestroy; |
604 |
|
memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t)); |
605 |
|
|
606 |
|
pdestroy.version = XVID_VERSION; |
607 |
|
pdestroy.num_frames = pEnc->m_framenum; |
608 |
|
|
609 |
for (i=0; i<pEnc->num_plugins;i++) { |
for (i=0; i<pEnc->num_plugins;i++) { |
610 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
611 |
pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0); |
pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, 0); |
612 |
} |
} |
613 |
} |
} |
614 |
xvid_free(pEnc->plugins); |
xvid_free(pEnc->plugins); |
615 |
|
} |
616 |
|
|
617 |
xvid_free(pEnc); |
xvid_free(pEnc); |
618 |
|
|
627 |
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, |
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, |
628 |
int opt, int * type, int * quant, xvid_enc_stats_t * stats) |
int opt, int * type, int * quant, xvid_enc_stats_t * stats) |
629 |
{ |
{ |
630 |
int i; |
unsigned int i, j; |
631 |
xvid_plg_data_t data; |
xvid_plg_data_t data; |
632 |
|
|
633 |
|
/* set data struct */ |
634 |
|
|
635 |
memset(&data, 0, sizeof(xvid_plg_data_t)); |
memset(&data, 0, sizeof(xvid_plg_data_t)); |
636 |
data.version = XVID_VERSION; |
data.version = XVID_VERSION; |
637 |
|
|
638 |
data.width = pEnc->mbParam.width; |
data.width = pEnc->mbParam.width; |
639 |
data.height = pEnc->mbParam.height; |
data.height = pEnc->mbParam.height; |
640 |
|
data.mb_width = pEnc->mbParam.mb_width; |
641 |
|
data.mb_height = pEnc->mbParam.mb_height; |
642 |
data.fincr = frame->fincr; |
data.fincr = frame->fincr; |
643 |
data.fbase = pEnc->mbParam.fbase; |
data.fbase = pEnc->mbParam.fbase; |
644 |
|
|
663 |
if (opt == XVID_PLG_BEFORE) { |
if (opt == XVID_PLG_BEFORE) { |
664 |
data.type = XVID_TYPE_AUTO; |
data.type = XVID_TYPE_AUTO; |
665 |
data.quant = 2; |
data.quant = 2; |
666 |
//memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height); |
|
667 |
//data.qscale_stride = pEnc->mbParam.width; |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
668 |
//data.qscale_table = pEnc->temp_dquants; |
data.dquant = pEnc->temp_dquants; |
669 |
/* todo: vol,vop,motion flags */ |
data.dquant_stride = pEnc->mbParam.mb_width; |
670 |
|
memset(data.dquant, 0, data.mb_width*data.mb_height); |
671 |
|
} |
672 |
|
|
673 |
|
/* todo: [vol,vop,motion]_flags*/ |
674 |
|
|
675 |
} else { // XVID_PLG_AFTER |
} else { // XVID_PLG_AFTER |
676 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
683 |
data.original.stride[2] = pEnc->mbParam.edged_width/2; |
data.original.stride[2] = pEnc->mbParam.edged_width/2; |
684 |
} |
} |
685 |
|
|
686 |
if ((frame->vol_flags & XVID_EXTRASTATS) || |
if ((frame->vol_flags & XVID_VOL_EXTRASTATS) || |
687 |
(pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { |
(pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { |
688 |
|
|
689 |
data.sse_y = |
data.sse_y = |
704 |
|
|
705 |
data.type = coding2type(frame->coding_type); |
data.type = coding2type(frame->coding_type); |
706 |
data.quant = frame->quant; |
data.quant = frame->quant; |
707 |
/* todo: data.qscale */ |
|
708 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
709 |
|
data.dquant = pEnc->temp_dquants; |
710 |
|
data.dquant_stride = pEnc->mbParam.mb_width; |
711 |
|
|
712 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
713 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
714 |
|
data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;; |
715 |
|
} |
716 |
|
} |
717 |
|
|
718 |
data.vol_flags = frame->vol_flags; |
data.vol_flags = frame->vol_flags; |
719 |
data.vop_flags = frame->vop_flags; |
data.vop_flags = frame->vop_flags; |
720 |
data.motion_flags = frame->motion_flags; |
data.motion_flags = frame->motion_flags; |
724 |
data.mblks = frame->sStat.mblks; |
data.mblks = frame->sStat.mblks; |
725 |
data.ublks = frame->sStat.ublks; |
data.ublks = frame->sStat.ublks; |
726 |
|
|
727 |
if (stats) |
if (stats) { |
|
{ |
|
728 |
stats->type = coding2type(frame->coding_type); |
stats->type = coding2type(frame->coding_type); |
729 |
stats->quant = frame->quant; |
stats->quant = frame->quant; |
730 |
stats->vol_flags = frame->vol_flags; |
stats->vol_flags = frame->vol_flags; |
740 |
} |
} |
741 |
} |
} |
742 |
|
|
743 |
emms(); |
/* call plugins */ |
744 |
for (i=0; i<pEnc->num_plugins;i++) { |
for (i=0; i<pEnc->num_plugins;i++) { |
745 |
|
emms(); |
746 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
747 |
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { |
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { |
748 |
continue; |
continue; |
751 |
} |
} |
752 |
emms(); |
emms(); |
753 |
|
|
754 |
|
/* copy modified values back into frame*/ |
755 |
if (opt == XVID_PLG_BEFORE) { |
if (opt == XVID_PLG_BEFORE) { |
756 |
*type = data.type; |
*type = data.type; |
757 |
*quant = data.quant; |
*quant = data.quant; |
|
/* todo: copy modified qscale,vol,vop,motion flags into the frame*/ |
|
|
} |
|
758 |
|
|
759 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
760 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
761 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
762 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i]; |
763 |
|
} |
764 |
|
}else{ |
765 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
766 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
767 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
768 |
|
} |
769 |
|
} |
770 |
|
/* todo: [vol,vop,motion]_flags*/ |
771 |
|
} |
772 |
} |
} |
773 |
|
|
774 |
|
|
855 |
if (image_input |
if (image_input |
856 |
(&q->image, pEnc->mbParam.width, pEnc->mbParam.height, |
(&q->image, pEnc->mbParam.width, pEnc->mbParam.height, |
857 |
pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride, |
pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride, |
858 |
xFrame->input.csp, xFrame->vol_flags & XVID_INTERLACING)) |
xFrame->input.csp, xFrame->vol_flags & XVID_VOL_INTERLACING)) |
859 |
{ |
{ |
860 |
emms(); |
emms(); |
861 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
862 |
} |
} |
863 |
stop_conv_timer(); |
stop_conv_timer(); |
864 |
|
|
865 |
if ((xFrame->vop_flags & XVID_CHROMAOPT)) { |
if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) { |
866 |
image_chroma_optimize(&q->image, |
image_chroma_optimize(&q->image, |
867 |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
868 |
} |
} |
919 |
indentical to the future-referece frame. |
indentical to the future-referece frame. |
920 |
*/ |
*/ |
921 |
|
|
922 |
if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) { |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED && pEnc->bframenum_tail > 0)) { |
923 |
int tmp; |
int tmp; |
924 |
int bits; |
int bits; |
925 |
|
|
961 |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
962 |
{ |
{ |
963 |
|
|
964 |
if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->mbParam.max_bframes > 0) { |
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) { |
965 |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
966 |
} |
} |
967 |
|
|
1006 |
* init pEnc->current fields |
* init pEnc->current fields |
1007 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1008 |
|
|
|
|
|
1009 |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
1010 |
|
inc_frame_num(pEnc); |
1011 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
1012 |
pEnc->current->vop_flags = frame->vop_flags; |
pEnc->current->vop_flags = frame->vop_flags; |
1013 |
pEnc->current->motion_flags = frame->motion; |
pEnc->current->motion_flags = frame->motion; |
1014 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1015 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
1016 |
|
|
1017 |
if ((xFrame->vop_flags & XVID_CHROMAOPT)) { |
|
1018 |
|
if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) { |
1019 |
image_chroma_optimize(&pEnc->current->image, |
image_chroma_optimize(&pEnc->current->image, |
1020 |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
1021 |
} |
} |
1022 |
|
|
|
emms(); /* float-point region */ |
|
|
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
|
|
unsigned int x, y; |
|
|
|
|
|
pEnc->current->quant = |
|
|
adaptive_quantization(pEnc->current->image.y, |
|
|
pEnc->mbParam.edged_width, pEnc->temp_dquants, |
|
|
pEnc->current->quant, pEnc->current->quant, |
|
|
2 * pEnc->current->quant, |
|
|
pEnc->mbParam.mb_width, |
|
|
pEnc->mbParam.mb_height); |
|
|
|
|
|
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
|
|
|
|
|
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
|
|
|
|
|
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
|
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
|
|
|
|
|
pMB->dquant = iDQtab[pEnc->temp_dquants[OFFSET(x, y)] + 2]; |
|
|
} |
|
|
|
|
|
#undef OFFSET |
|
|
} |
|
|
|
|
|
} |
|
|
emms(); /* END floating-point region */ |
|
|
|
|
1023 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1024 |
* frame type & quant selection |
* frame type & quant selection |
1025 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1041 |
}else{ |
}else{ |
1042 |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
1043 |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1044 |
pEnc->iFrameNum, pEnc->bframenum_tail); |
pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold); |
1045 |
|
|
1046 |
if (type == B_VOP && !(pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES)) { |
if (type == B_VOP && !(pEnc->current->vop_flags & XVID_VOP_DYNAMIC_BFRAMES)) { |
1047 |
type = P_VOP; /* disable dynamic bframes */ |
type = P_VOP; /* disable dynamic bframes */ |
1048 |
} |
} |
1049 |
} |
} |
1058 |
} |
} |
1059 |
} |
} |
1060 |
|
|
|
inc_frame_num(pEnc); |
|
1061 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1062 |
|
|
1063 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1064 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
1065 |
"%i st:%i if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum); |
"%i st:%i if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum); |
1066 |
} |
} |
1070 |
* (we dont encode here, rather we store the frame in the bframes queue, to be encoded later) |
* (we dont encode here, rather we store the frame in the bframes queue, to be encoded later) |
1071 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1072 |
if (type == B_VOP) { |
if (type == B_VOP) { |
1073 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1074 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1075 |
} |
} |
1076 |
|
|
1077 |
if (frame->bquant < 1) { |
if (frame->quant < 1) { |
1078 |
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
1079 |
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
1080 |
|
|
1081 |
} else { |
} else { |
1082 |
pEnc->current->quant = frame->bquant; |
pEnc->current->quant = frame->quant; |
1083 |
} |
} |
1084 |
|
|
1085 |
if (pEnc->current->quant < 1) |
if (pEnc->current->quant < 1) |
1101 |
} |
} |
1102 |
|
|
1103 |
/* for unpacked bframes, output the stats for the last encoded frame */ |
/* for unpacked bframes, output the stats for the last encoded frame */ |
1104 |
if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) |
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
1105 |
{ |
{ |
1106 |
if (pEnc->current->stamp > 0) { |
if (pEnc->current->stamp > 0) { |
1107 |
call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1115 |
* if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe |
* if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe |
1116 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1117 |
|
|
1118 |
if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
1119 |
|
|
1120 |
// place this frame back on the encoding-queue (head) |
// place this frame back on the encoding-queue (head) |
1121 |
// we will deal with it next time |
// we will deal with it next time |
1131 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
1132 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1133 |
|
|
1134 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1135 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
1136 |
} |
} |
1137 |
|
|
1151 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1152 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1153 |
|
|
1154 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1155 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
1156 |
} |
} |
1157 |
|
|
1159 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1160 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
1161 |
|
|
1162 |
if ((pEnc->mbParam.vol_flags & XVID_MPEGQUANT)) { |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
1163 |
if (frame->quant_intra_matrix != NULL) |
if (frame->quant_intra_matrix != NULL) |
1164 |
set_intra_matrix(frame->quant_intra_matrix); |
set_intra_matrix(frame->quant_intra_matrix); |
1165 |
if (frame->quant_inter_matrix != NULL) |
if (frame->quant_inter_matrix != NULL) |
1168 |
|
|
1169 |
/* prevent vol/vop misuse */ |
/* prevent vol/vop misuse */ |
1170 |
|
|
1171 |
if (!(pEnc->current->vol_flags & XVID_REDUCED_ENABLE)) |
if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) |
1172 |
pEnc->current->vop_flags &= ~XVID_REDUCED; |
pEnc->current->vop_flags &= ~XVID_VOP_REDUCED; |
1173 |
|
|
1174 |
if (!(pEnc->current->vol_flags & XVID_INTERLACING)) |
if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING)) |
1175 |
pEnc->current->vop_flags &= ~(XVID_TOPFIELDFIRST|XVID_ALTERNATESCAN); |
pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN); |
1176 |
|
|
1177 |
/* ^^^------------------------ */ |
/* ^^^------------------------ */ |
1178 |
|
|
1194 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1195 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1196 |
|
|
1197 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1198 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
1199 |
} |
} |
1200 |
|
|
1216 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1217 |
|
|
1218 |
/* packed & queued_bframes: dont bother outputting stats, we do so after the flush */ |
/* packed & queued_bframes: dont bother outputting stats, we do so after the flush */ |
1219 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) { |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
1220 |
goto repeat; |
goto repeat; |
1221 |
} |
} |
1222 |
|
|
1223 |
/* packed or no-bframes: output stats */ |
/* packed or no-bframes: output stats */ |
1224 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) { |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0) { |
1225 |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1226 |
} |
} |
1227 |
|
|
1239 |
} |
} |
1240 |
|
|
1241 |
|
|
1242 |
|
static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
1243 |
|
{ |
1244 |
|
unsigned int i,j; |
1245 |
|
int quant = frame->quant; |
1246 |
|
|
1247 |
|
for (j=0; j<pParam->mb_height; j++) |
1248 |
|
for (i=0; i<pParam->mb_width; i++) { |
1249 |
|
MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i]; |
1250 |
|
quant += pMB->dquant; |
1251 |
|
if (quant > 31) |
1252 |
|
quant = 31; |
1253 |
|
if (quant < 1) |
1254 |
|
quant = 1; |
1255 |
|
pMB->quant = quant; |
1256 |
|
} |
1257 |
|
} |
1258 |
|
|
1259 |
|
|
1260 |
static __inline void |
static __inline void |
1261 |
CodeIntraMB(Encoder * pEnc, |
CodeIntraMB(Encoder * pEnc, |
1262 |
MACROBLOCK * pMB) |
MACROBLOCK * pMB) |
1270 |
pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
1271 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
1272 |
|
|
1273 |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
if (pMB->dquant != 0) { |
|
if (pMB->dquant != NO_CHANGE) { |
|
1274 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
|
pEnc->current->quant += DQtab[pMB->dquant]; |
|
|
|
|
|
if (pEnc->current->quant > 31) |
|
|
pEnc->current->quant = 31; |
|
|
if (pEnc->current->quant < 1) |
|
|
pEnc->current->quant = 1; |
|
1275 |
} |
} |
1276 |
} |
} |
1277 |
|
|
|
pMB->quant = pEnc->current->quant; |
|
|
} |
|
|
|
|
1278 |
|
|
1279 |
|
|
1280 |
static int |
static int |
1290 |
|
|
1291 |
uint16_t x, y; |
uint16_t x, y; |
1292 |
|
|
1293 |
if ((pEnc->current->vol_flags & XVID_REDUCED_ENABLE)) |
if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) |
1294 |
{ |
{ |
1295 |
mb_width = (pEnc->mbParam.width + 31) / 32; |
mb_width = (pEnc->mbParam.width + 31) / 32; |
1296 |
mb_height = (pEnc->mbParam.height + 31) / 32; |
mb_height = (pEnc->mbParam.height + 31) / 32; |
1308 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1309 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1310 |
|
|
1311 |
|
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1312 |
|
|
1313 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
1314 |
|
|
1315 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1336 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1337 |
|
|
1338 |
start_timer(); |
start_timer(); |
1339 |
if (pEnc->current->vop_flags & XVID_GREYSCALE) |
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) |
1340 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1341 |
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1342 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1345 |
stop_coding_timer(); |
stop_coding_timer(); |
1346 |
} |
} |
1347 |
|
|
1348 |
if ((pEnc->current->vop_flags & XVID_REDUCED)) |
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
1349 |
{ |
{ |
1350 |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
1351 |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
1353 |
} |
} |
1354 |
emms(); |
emms(); |
1355 |
|
|
1356 |
|
/* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */ |
1357 |
|
#if 0 |
1358 |
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
1359 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
1360 |
|
#endif |
1361 |
BitstreamPadAlways(bs); |
BitstreamPadAlways(bs); |
1362 |
|
#if 0 |
1363 |
else |
else |
1364 |
BitstreamPad(bs); |
BitstreamPad(bs); |
1365 |
|
#endif |
1366 |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
1367 |
|
|
1368 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
1405 |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
1406 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
1407 |
|
|
1408 |
if ((pEnc->current->vop_flags & XVID_REDUCED)) |
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
1409 |
{ |
{ |
1410 |
mb_width = (pEnc->mbParam.width + 31) / 32; |
mb_width = (pEnc->mbParam.width + 31) / 32; |
1411 |
mb_height = (pEnc->mbParam.height + 31) / 32; |
mb_height = (pEnc->mbParam.height + 31) / 32; |
1427 |
else |
else |
1428 |
iLimit = mb_width * mb_height + 1; |
iLimit = mb_width * mb_height + 1; |
1429 |
|
|
1430 |
if ((pEnc->current->vop_flags & XVID_HALFPEL)) { |
if ((pEnc->current->vop_flags & XVID_VOP_HALFPEL)) { |
1431 |
start_timer(); |
start_timer(); |
1432 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1433 |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
1434 |
pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_height, |
1435 |
(pEnc->mbParam.vol_flags & XVID_QUARTERPEL), |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), |
1436 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1437 |
stop_inter_timer(); |
stop_inter_timer(); |
1438 |
} |
} |
1439 |
|
|
1440 |
pEnc->current->coding_type = P_VOP; |
pEnc->current->coding_type = P_VOP; |
1441 |
|
|
1442 |
|
|
1443 |
|
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1444 |
|
|
1445 |
start_timer(); |
start_timer(); |
1446 |
/*if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
/*if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
1447 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1455 |
|
|
1456 |
if (bIntra == 1) return FrameCodeI(pEnc, bs); |
if (bIntra == 1) return FrameCodeI(pEnc, bs); |
1457 |
|
|
1458 |
if ( ( pEnc->current->vol_flags & XVID_GMC ) |
if ( ( pEnc->current->vol_flags & XVID_VOL_GMC ) |
1459 |
&& ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) |
&& ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) |
1460 |
{ |
{ |
1461 |
pEnc->current->coding_type = S_VOP; |
pEnc->current->coding_type = S_VOP; |
1467 |
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
1468 |
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
1469 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
1470 |
pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0, |
pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0, |
1471 |
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
1472 |
|
|
1473 |
} |
} |
1516 |
pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1517 |
pEnc->mbParam.edged_width, 65536); |
pEnc->mbParam.edged_width, 65536); |
1518 |
|
|
1519 |
if (pEnc->current->motion_flags & PMV_CHROMA16) { |
if (pEnc->current->motion_flags & XVID_ME_CHROMA16) { |
1520 |
iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
1521 |
pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
1522 |
|
|
1526 |
|
|
1527 |
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
1528 |
|
|
1529 |
if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) |
if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) |
1530 |
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
1531 |
else |
else |
1532 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
1549 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pEnc->mbParam.width, |
1550 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1551 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1552 |
(pEnc->current->vol_flags & XVID_QUARTERPEL), |
(pEnc->current->vol_flags & XVID_VOL_QUARTERPEL), |
1553 |
(pEnc->current->vop_flags & XVID_REDUCED), |
(pEnc->current->vop_flags & XVID_VOP_REDUCED), |
1554 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1555 |
|
|
1556 |
stop_comp_timer(); |
stop_comp_timer(); |
1557 |
|
|
1558 |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
if (pMB->dquant != 0) { |
|
if (pMB->dquant != NO_CHANGE) { |
|
1559 |
pMB->mode = MODE_INTER_Q; |
pMB->mode = MODE_INTER_Q; |
|
pEnc->current->quant += DQtab[pMB->dquant]; |
|
|
if (pEnc->current->quant > 31) |
|
|
pEnc->current->quant = 31; |
|
|
else if (pEnc->current->quant < 1) |
|
|
pEnc->current->quant = 1; |
|
|
} |
|
1560 |
} |
} |
|
pMB->quant = pEnc->current->quant; |
|
1561 |
|
|
1562 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1563 |
|
|
1580 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1581 |
|
|
1582 |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
1583 |
(pMB->dquant == NO_CHANGE); |
(pMB->dquant == 0); |
1584 |
|
|
1585 |
if (pEnc->current->coding_type == S_VOP) |
if (pEnc->current->coding_type == S_VOP) |
1586 |
skip_possible &= (pMB->mcsel == 1); |
skip_possible &= (pMB->mcsel == 1); |
1587 |
else if (pEnc->current->coding_type == P_VOP) { |
else if (pEnc->current->coding_type == P_VOP) { |
1588 |
if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) |
if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) |
1589 |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
1590 |
else |
else |
1591 |
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
1612 |
} |
} |
1613 |
|
|
1614 |
if (!bSkip) { /* no SKIP, but trivial block */ |
if (!bSkip) { /* no SKIP, but trivial block */ |
1615 |
if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) { |
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
1616 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1617 |
pMB->pmvs[0].x = - predMV.x; |
pMB->pmvs[0].x = - predMV.x; |
1618 |
pMB->pmvs[0].y = - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1639 |
} |
} |
1640 |
/* ordinary case: normal coded INTER/INTER4V block */ |
/* ordinary case: normal coded INTER/INTER4V block */ |
1641 |
|
|
1642 |
if ((pEnc->current->vop_flags & XVID_GREYSCALE)) |
if ((pEnc->current->vop_flags & XVID_VOP_GREYSCALE)) |
1643 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1644 |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1645 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1646 |
} |
} |
1647 |
|
|
1648 |
if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) { |
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
1649 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1650 |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
1651 |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1662 |
{ int k; |
{ int k; |
1663 |
for (k=1;k<4;k++) |
for (k=1;k<4;k++) |
1664 |
{ |
{ |
1665 |
if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) { |
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
1666 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1667 |
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
1668 |
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
1683 |
} |
} |
1684 |
} |
} |
1685 |
|
|
1686 |
if ((pEnc->current->vop_flags & XVID_REDUCED)) |
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
1687 |
{ |
{ |
1688 |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
1689 |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
1705 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
1706 |
|
|
1707 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
1708 |
&& (pEnc->mbParam.m_fcode <= (3 + (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0) ))) /* maximum search range 128 */ |
&& (pEnc->mbParam.m_fcode <= (3 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* maximum search range 128 */ |
1709 |
{ |
{ |
1710 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1711 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1712 |
} else if ((fSigma < iSearchRange / 6) |
} else if ((fSigma < iSearchRange / 6) |
1713 |
&& (pEnc->fMvPrevSigma >= 0) |
&& (pEnc->fMvPrevSigma >= 0) |
1714 |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
1715 |
&& (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0) ))) /* minimum search range 16 */ |
&& (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* minimum search range 16 */ |
1716 |
{ |
{ |
1717 |
pEnc->mbParam.m_fcode--; |
pEnc->mbParam.m_fcode--; |
1718 |
iSearchRange /= 2; |
iSearchRange /= 2; |
1759 |
} |
} |
1760 |
*/ |
*/ |
1761 |
|
|
1762 |
|
/* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */ |
1763 |
|
#if 0 |
1764 |
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
1765 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
1766 |
|
#endif |
1767 |
BitstreamPadAlways(bs); |
BitstreamPadAlways(bs); |
1768 |
|
#if 0 |
1769 |
else |
else |
1770 |
BitstreamPad(bs); |
BitstreamPad(bs); |
1771 |
|
#endif |
1772 |
|
|
1773 |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
1774 |
|
|
1796 |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
1797 |
} |
} |
1798 |
|
|
1799 |
/* XXX: pEnc->current->global_flags &= ~XVID_REDUCED; reduced resoltion not yet supported */ |
/* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED; reduced resoltion not yet supported */ |
1800 |
|
|
1801 |
if (!first){ |
if (!first){ |
1802 |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
1812 |
start_timer(); |
start_timer(); |
1813 |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1814 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1815 |
(pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1816 |
stop_inter_timer(); |
stop_inter_timer(); |
1817 |
|
|
1818 |
/* backward */ |
/* backward */ |
1822 |
start_timer(); |
start_timer(); |
1823 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1824 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1825 |
(pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1826 |
stop_inter_timer(); |
stop_inter_timer(); |
1827 |
|
|
1828 |
start_timer(); |
start_timer(); |
1859 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1860 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1861 |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
1862 |
int direction = frame->vop_flags & XVID_ALTERNATESCAN ? 2 : 0; |
int direction = frame->vop_flags & XVID_VOP_ALTERNATESCAN ? 2 : 0; |
1863 |
|
|
1864 |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
1865 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1867 |
continue; |
continue; |
1868 |
} |
} |
1869 |
|
|
1870 |
if (mb->mode != MODE_DIRECT_NONE_MV) { |
if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
1871 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1872 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1873 |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1877 |
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
1878 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
1879 |
|
|
1880 |
mb->cbp = |
if (mb->mode != MODE_DIRECT_NONE_MV) |
1881 |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
1882 |
|
|
1883 |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
1884 |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
1900 |
|
|
1901 |
/* TODO: dynamic fcode/bcode ??? */ |
/* TODO: dynamic fcode/bcode ??? */ |
1902 |
|
|
1903 |
BitstreamPad(bs); |
BitstreamPadAlways(bs); |
1904 |
frame->length = (BitstreamPos(bs) - bits) / 8; |
frame->length = (BitstreamPos(bs) - bits) / 8; |
1905 |
|
|
1906 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |