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.11 2003-03-22 13:49:49 syskin 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 |
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 |
for (i=0; i<pEnc->num_plugins;i++) { |
for (i=0; i<pEnc->num_plugins;i++) { |
601 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
617 |
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, |
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, |
618 |
int opt, int * type, int * quant, xvid_enc_stats_t * stats) |
int opt, int * type, int * quant, xvid_enc_stats_t * stats) |
619 |
{ |
{ |
620 |
int i; |
unsigned int i, j; |
621 |
xvid_plg_data_t data; |
xvid_plg_data_t data; |
622 |
|
|
623 |
|
/* set data struct */ |
624 |
|
|
625 |
memset(&data, 0, sizeof(xvid_plg_data_t)); |
memset(&data, 0, sizeof(xvid_plg_data_t)); |
626 |
data.version = XVID_VERSION; |
data.version = XVID_VERSION; |
627 |
|
|
628 |
data.width = pEnc->mbParam.width; |
data.width = pEnc->mbParam.width; |
629 |
data.height = pEnc->mbParam.height; |
data.height = pEnc->mbParam.height; |
630 |
|
data.mb_width = pEnc->mbParam.mb_width; |
631 |
|
data.mb_height = pEnc->mbParam.mb_height; |
632 |
data.fincr = frame->fincr; |
data.fincr = frame->fincr; |
633 |
data.fbase = pEnc->mbParam.fbase; |
data.fbase = pEnc->mbParam.fbase; |
634 |
|
|
653 |
if (opt == XVID_PLG_BEFORE) { |
if (opt == XVID_PLG_BEFORE) { |
654 |
data.type = XVID_TYPE_AUTO; |
data.type = XVID_TYPE_AUTO; |
655 |
data.quant = 2; |
data.quant = 2; |
656 |
//memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height); |
|
657 |
//data.qscale_stride = pEnc->mbParam.width; |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
658 |
//data.qscale_table = pEnc->temp_dquants; |
data.dquant = pEnc->temp_dquants; |
659 |
/* todo: vol,vop,motion flags */ |
data.dquant_stride = pEnc->mbParam.mb_width; |
660 |
|
memset(data.dquant, 0, data.mb_width*data.mb_height); |
661 |
|
} |
662 |
|
|
663 |
|
/* todo: [vol,vop,motion]_flags*/ |
664 |
|
|
665 |
} else { // XVID_PLG_AFTER |
} else { // XVID_PLG_AFTER |
666 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
694 |
|
|
695 |
data.type = coding2type(frame->coding_type); |
data.type = coding2type(frame->coding_type); |
696 |
data.quant = frame->quant; |
data.quant = frame->quant; |
697 |
/* todo: data.qscale */ |
|
698 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
699 |
|
data.dquant = pEnc->temp_dquants; |
700 |
|
data.dquant_stride = pEnc->mbParam.mb_width; |
701 |
|
|
702 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
703 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
704 |
|
data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;; |
705 |
|
} |
706 |
|
} |
707 |
|
|
708 |
data.vol_flags = frame->vol_flags; |
data.vol_flags = frame->vol_flags; |
709 |
data.vop_flags = frame->vop_flags; |
data.vop_flags = frame->vop_flags; |
710 |
data.motion_flags = frame->motion_flags; |
data.motion_flags = frame->motion_flags; |
714 |
data.mblks = frame->sStat.mblks; |
data.mblks = frame->sStat.mblks; |
715 |
data.ublks = frame->sStat.ublks; |
data.ublks = frame->sStat.ublks; |
716 |
|
|
717 |
if (stats) |
if (stats) { |
|
{ |
|
718 |
stats->type = coding2type(frame->coding_type); |
stats->type = coding2type(frame->coding_type); |
719 |
stats->quant = frame->quant; |
stats->quant = frame->quant; |
720 |
stats->vol_flags = frame->vol_flags; |
stats->vol_flags = frame->vol_flags; |
730 |
} |
} |
731 |
} |
} |
732 |
|
|
733 |
emms(); |
/* call plugins */ |
734 |
for (i=0; i<pEnc->num_plugins;i++) { |
for (i=0; i<pEnc->num_plugins;i++) { |
735 |
|
emms(); |
736 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
737 |
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) { |
738 |
continue; |
continue; |
741 |
} |
} |
742 |
emms(); |
emms(); |
743 |
|
|
744 |
|
/* copy modified values back into frame*/ |
745 |
if (opt == XVID_PLG_BEFORE) { |
if (opt == XVID_PLG_BEFORE) { |
746 |
*type = data.type; |
*type = data.type; |
747 |
*quant = data.quant; |
*quant = data.quant; |
|
/* todo: copy modified qscale,vol,vop,motion flags into the frame*/ |
|
|
} |
|
748 |
|
|
749 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
750 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
751 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
752 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i]; |
753 |
|
} |
754 |
|
}else{ |
755 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
756 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
757 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
758 |
|
} |
759 |
|
} |
760 |
|
/* todo: [vol,vop,motion]_flags*/ |
761 |
|
} |
762 |
} |
} |
763 |
|
|
764 |
|
|
996 |
* init pEnc->current fields |
* init pEnc->current fields |
997 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
998 |
|
|
|
|
|
999 |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
1000 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
1001 |
pEnc->current->vop_flags = frame->vop_flags; |
pEnc->current->vop_flags = frame->vop_flags; |
1008 |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
1009 |
} |
} |
1010 |
|
|
|
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 */ |
|
|
|
|
1011 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1012 |
* frame type & quant selection |
* frame type & quant selection |
1013 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1063 |
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"); |
1064 |
} |
} |
1065 |
|
|
1066 |
if (frame->bquant < 1) { |
if (frame->quant < 1) { |
1067 |
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
1068 |
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
1069 |
|
|
1070 |
} else { |
} else { |
1071 |
pEnc->current->quant = frame->bquant; |
pEnc->current->quant = frame->quant; |
1072 |
} |
} |
1073 |
|
|
1074 |
if (pEnc->current->quant < 1) |
if (pEnc->current->quant < 1) |
1228 |
} |
} |
1229 |
|
|
1230 |
|
|
1231 |
|
static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
1232 |
|
{ |
1233 |
|
unsigned int i,j; |
1234 |
|
int quant = frame->quant; |
1235 |
|
|
1236 |
|
for (j=0; j<pParam->mb_height; j++) |
1237 |
|
for (i=0; i<pParam->mb_width; i++) { |
1238 |
|
MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i]; |
1239 |
|
quant += pMB->dquant; |
1240 |
|
if (quant > 31) |
1241 |
|
quant = 31; |
1242 |
|
if (quant < 1) |
1243 |
|
quant = 1; |
1244 |
|
pMB->quant = quant; |
1245 |
|
} |
1246 |
|
} |
1247 |
|
|
1248 |
|
|
1249 |
static __inline void |
static __inline void |
1250 |
CodeIntraMB(Encoder * pEnc, |
CodeIntraMB(Encoder * pEnc, |
1251 |
MACROBLOCK * pMB) |
MACROBLOCK * pMB) |
1259 |
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; |
1260 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
1261 |
|
|
1262 |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
if (pMB->dquant != 0) { |
|
if (pMB->dquant != NO_CHANGE) { |
|
1263 |
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; |
|
1264 |
} |
} |
1265 |
} |
} |
1266 |
|
|
|
pMB->quant = pEnc->current->quant; |
|
|
} |
|
|
|
|
1267 |
|
|
1268 |
|
|
1269 |
static int |
static int |
1297 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1298 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1299 |
|
|
1300 |
|
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1301 |
|
|
1302 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
1303 |
|
|
1304 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1423 |
|
|
1424 |
pEnc->current->coding_type = P_VOP; |
pEnc->current->coding_type = P_VOP; |
1425 |
|
|
1426 |
|
|
1427 |
|
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1428 |
|
|
1429 |
start_timer(); |
start_timer(); |
1430 |
/*if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
/*if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
1431 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1539 |
|
|
1540 |
stop_comp_timer(); |
stop_comp_timer(); |
1541 |
|
|
1542 |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
if (pMB->dquant != 0) { |
|
if (pMB->dquant != NO_CHANGE) { |
|
1543 |
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; |
|
|
} |
|
1544 |
} |
} |
|
pMB->quant = pEnc->current->quant; |
|
1545 |
|
|
1546 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1547 |
|
|
1564 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1565 |
|
|
1566 |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
1567 |
(pMB->dquant == NO_CHANGE); |
(pMB->dquant == 0); |
1568 |
|
|
1569 |
if (pEnc->current->coding_type == S_VOP) |
if (pEnc->current->coding_type == S_VOP) |
1570 |
skip_possible &= (pMB->mcsel == 1); |
skip_possible &= (pMB->mcsel == 1); |
1846 |
continue; |
continue; |
1847 |
} |
} |
1848 |
|
|
1849 |
if (mb->mode != MODE_DIRECT_NONE_MV) { |
if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
1850 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1851 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1852 |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1856 |
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
1857 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
1858 |
|
|
1859 |
mb->cbp = |
if (mb->mode != MODE_DIRECT_NONE_MV) |
1860 |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
1861 |
|
|
1862 |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
1863 |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |