--- branches/dev-api-4/xvidcore/src/encoder.c 2003/03/15 14:32:56 920 +++ branches/dev-api-4/xvidcore/src/encoder.c 2003/03/22 13:49:49 936 @@ -26,7 +26,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * $Id: encoder.c,v 1.95.2.5 2003-03-15 14:32:56 suxen_drol Exp $ + * $Id: encoder.c,v 1.95.2.11 2003-03-22 13:49:49 syskin Exp $ * ****************************************************************************/ @@ -54,12 +54,6 @@ #include "utils/mem_align.h" /***************************************************************************** - * Local macros - ****************************************************************************/ - -#define SWAP(_T_,A,B) { _T_ tmp = A; A = B; B = tmp; } - -/***************************************************************************** * Local function prototypes ****************************************************************************/ @@ -75,18 +69,6 @@ FRAMEINFO * frame, Bitstream * bs); -/***************************************************************************** - * Local data - ****************************************************************************/ - -static int DQtab[4] = { - -1, -2, 1, 2 -}; - -static int iDQtab[5] = { - 1, 0, NO_CHANGE, 2, 3 -}; - /***************************************************************************** * Encoder creation @@ -166,7 +148,8 @@ /* framerate */ pEnc->mbParam.fincr = MAX(create->fincr, 0); pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; - simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase); + if (pEnc->mbParam.fincr>0) + simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase); /* plugin */ pEnc->num_plugins = create->num_plugins; @@ -198,9 +181,16 @@ } } + if ((pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) || + (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { + pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */ + } + /* temp dquants */ - pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * - pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); + if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { + pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * + pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); + } /* XXX: error checking */ /* bframes */ @@ -498,7 +488,9 @@ xvid_free(pEnc->current); xvid_free(pEnc->reference); - xvid_free(pEnc->temp_dquants); + if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { + xvid_free(pEnc->temp_dquants); + } xvid_err_memory0: for (n=0; nnum_plugins;n++) { @@ -601,7 +593,9 @@ xvid_free(pEnc->reference->mbs); xvid_free(pEnc->reference); - xvid_free(pEnc->temp_dquants); + if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { + xvid_free(pEnc->temp_dquants); + } for (i=0; inum_plugins;i++) { if (pEnc->plugins[i].func) { @@ -620,17 +614,22 @@ call the plugins */ -static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, int opt, int * type, int * quant) +static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, + int opt, int * type, int * quant, xvid_enc_stats_t * stats) { - int i; + unsigned int i, j; xvid_plg_data_t data; + /* set data struct */ + memset(&data, 0, sizeof(xvid_plg_data_t)); data.version = XVID_VERSION; data.width = pEnc->mbParam.width; data.height = pEnc->mbParam.height; - data.fincr = pEnc->mbParam.fincr; + data.mb_width = pEnc->mbParam.mb_width; + data.mb_height = pEnc->mbParam.mb_height; + data.fincr = frame->fincr; data.fbase = pEnc->mbParam.fbase; data.reference.csp = XVID_CSP_USER; @@ -654,10 +653,14 @@ if (opt == XVID_PLG_BEFORE) { data.type = XVID_TYPE_AUTO; data.quant = 2; - //memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height); - //data.qscale_stride = pEnc->mbParam.width; - //data.qscale_table = pEnc->temp_dquants; - /* todo: vol,vop,motion flags */ + + if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { + data.dquant = pEnc->temp_dquants; + data.dquant_stride = pEnc->mbParam.mb_width; + memset(data.dquant, 0, data.mb_width*data.mb_height); + } + + /* todo: [vol,vop,motion]_flags*/ } else { // XVID_PLG_AFTER if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { @@ -670,9 +673,38 @@ data.original.stride[2] = pEnc->mbParam.edged_width/2; } + if ((frame->vol_flags & XVID_EXTRASTATS) || + (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { + + data.sse_y = + plane_sse( original->y, frame->image.y, + pEnc->mbParam.edged_width, pEnc->mbParam.width, + pEnc->mbParam.height); + + data.sse_u = + plane_sse( original->u, frame->image.u, + pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, + pEnc->mbParam.height/2); + + data.sse_v = + plane_sse( original->v, frame->image.v, + pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, + pEnc->mbParam.height/2); + } + data.type = coding2type(frame->coding_type); data.quant = frame->quant; - /* todo: data.qscale */ + + if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { + data.dquant = pEnc->temp_dquants; + data.dquant_stride = pEnc->mbParam.mb_width; + + for (j=0; jmbParam.mb_height; j++) + for (i=0; imbParam.mb_width; i++) { + data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;; + } + } + data.vol_flags = frame->vol_flags; data.vop_flags = frame->vop_flags; data.motion_flags = frame->motion_flags; @@ -681,22 +713,52 @@ data.kblks = frame->sStat.kblks; data.mblks = frame->sStat.mblks; data.ublks = frame->sStat.ublks; + + if (stats) { + stats->type = coding2type(frame->coding_type); + stats->quant = frame->quant; + stats->vol_flags = frame->vol_flags; + stats->vop_flags = frame->vop_flags; + stats->length = frame->length; + stats->hlength = frame->length - (frame->sStat.iTextBits / 8); + stats->kblks = frame->sStat.kblks; + stats->mblks = frame->sStat.mblks; + stats->ublks = frame->sStat.ublks; + stats->sse_y = data.sse_y; + stats->sse_u = data.sse_u; + stats->sse_v = data.sse_v; + } } + /* call plugins */ for (i=0; inum_plugins;i++) { + emms(); if (pEnc->plugins[i].func) { if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { continue; } } } + emms(); + /* copy modified values back into frame*/ if (opt == XVID_PLG_BEFORE) { *type = data.type; *quant = data.quant; - /* todo: copy modified qscale,vol,vop,motion flags into the frame*/ - } + if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { + for (j=0; jmbParam.mb_height; j++) + for (i=0; imbParam.mb_width; i++) { + frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i]; + } + }else{ + for (j=0; jmbParam.mb_height; j++) + for (i=0; imbParam.mb_width; i++) { + frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; + } + } + /* todo: [vol,vop,motion]_flags*/ + } } @@ -707,7 +769,7 @@ pEnc->current->frame_num = pEnc->m_framenum; pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */ - pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; + pEnc->mbParam.m_stamp += pEnc->current->fincr; pEnc->m_framenum++; /* debug ticker */ } @@ -739,24 +801,6 @@ } -static void -set_stats(xvid_enc_stats_t * stats, const MBParam * pParam, const FRAMEINFO * frame) -{ - if (stats) - { - stats->type = coding2type(frame->coding_type); - stats->quant = frame->quant; - stats->vol_flags = frame->vol_flags; - stats->vop_flags = frame->vop_flags; - stats->length = frame->length; - stats->hlength = frame->length - (frame->sStat.iTextBits / 8); - stats->kblks = frame->sStat.kblks; - stats->mblks = frame->sStat.mblks; - stats->ublks = frame->sStat.ublks; - } -} - - /***************************************************************************** * IPB frame encoder entry point @@ -852,12 +896,7 @@ } FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); - - emms(); - set_stats(stats, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]); - call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0); - emms(); - + call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats); pEnc->bframenum_head++; goto done; @@ -889,10 +928,7 @@ /* add the not-coded length to the reference frame size */ pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; - emms(); - set_stats(stats, &pEnc->mbParam, pEnc->current); - call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0); - emms(); + call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); /* flush complete: reset counters */ pEnc->flush_bframes = 0; @@ -916,10 +952,7 @@ { if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->mbParam.max_bframes > 0) { - emms(); - set_stats(stats, &pEnc->mbParam, pEnc->current); - call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0); - emms(); + call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); } /* if the very last frame is to be b-vop, we must change it to a p-vop */ @@ -963,6 +996,7 @@ * init pEnc->current fields * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ + pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; pEnc->current->vol_flags = pEnc->mbParam.vol_flags; pEnc->current->vop_flags = frame->vop_flags; pEnc->current->motion_flags = frame->motion; @@ -974,41 +1008,11 @@ pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); } - 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 - } - - } - /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% * frame type & quant selection * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ - emms(); /* END floating-point region */ - call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant); - emms(); + call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats); if (frame->type > 0) type = frame->type; @@ -1059,12 +1063,12 @@ image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); } - if (frame->bquant < 1) { + if (frame->quant < 1) { pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; } else { - pEnc->current->quant = frame->bquant; + pEnc->current->quant = frame->quant; } if (pEnc->current->quant < 1) @@ -1089,10 +1093,7 @@ if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) { if (pEnc->current->stamp > 0) { - emms(); - set_stats(stats, &pEnc->mbParam, pEnc->reference); - call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0); - emms(); + call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); } else stats->type = XVID_TYPE_NOTHING; @@ -1107,13 +1108,13 @@ // place this frame back on the encoding-queue (head) // we will deal with it next time - pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1); - pEnc->queue_size++; - image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); - dec_frame_num(pEnc); pEnc->iFrameNum--; + pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1); + pEnc->queue_size++; + image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); + // grab the last frame from the bframe-queue pEnc->bframenum_tail--; @@ -1169,7 +1170,6 @@ pEnc->mbParam.edged_width, pEnc->mbParam.height); } - FrameCodeI(pEnc, &bs); xFrame->out_flags |= XVID_KEYFRAME; @@ -1210,12 +1210,8 @@ } /* packed or no-bframes: output stats */ - if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) - { - emms(); - set_stats(stats, &pEnc->mbParam, pEnc->current); - call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0); - emms(); + if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) { + call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -1232,6 +1228,24 @@ } +static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) +{ + unsigned int i,j; + int quant = frame->quant; + + for (j=0; jmb_height; j++) + for (i=0; imb_width; i++) { + MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i]; + quant += pMB->dquant; + if (quant > 31) + quant = 31; + if (quant < 1) + quant = 1; + pMB->quant = quant; + } +} + + static __inline void CodeIntraMB(Encoder * pEnc, MACROBLOCK * pMB) @@ -1245,19 +1259,9 @@ pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; pMB->sad16 = 0; - if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { - if (pMB->dquant != NO_CHANGE) { - 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; - } - } - - pMB->quant = pEnc->current->quant; + if (pMB->dquant != 0) { + pMB->mode = MODE_INTRA_Q; + } } @@ -1292,6 +1296,8 @@ pEnc->mbParam.m_rounding_type = 1; pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; pEnc->current->coding_type = I_VOP; + + SetMacroblockQuants(&pEnc->mbParam, pEnc->current); BitstreamWriteVolHeader(bs, &pEnc->mbParam); @@ -1416,6 +1422,9 @@ } pEnc->current->coding_type = P_VOP; + + + SetMacroblockQuants(&pEnc->mbParam, pEnc->current); start_timer(); /*if (pEnc->current->global_flags & XVID_HINTEDME_SET) @@ -1530,17 +1539,9 @@ stop_comp_timer(); - if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { - if (pMB->dquant != NO_CHANGE) { - 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; - } + if (pMB->dquant != 0) { + pMB->mode = MODE_INTER_Q; } - pMB->quant = pEnc->current->quant; pMB->field_pred = 0; @@ -1563,7 +1564,7 @@ /* Finished processing the MB, now check if to CODE or SKIP */ skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && - (pMB->dquant == NO_CHANGE); + (pMB->dquant == 0); if (pEnc->current->coding_type == S_VOP) skip_possible &= (pMB->mcsel == 1); @@ -1845,7 +1846,7 @@ continue; } - if (mb->mode != MODE_DIRECT_NONE_MV) { + if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, b_ref, &pEnc->vInterH, @@ -1855,8 +1856,8 @@ if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; mb->quant = frame->quant; - mb->cbp = - MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); + if (mb->mode != MODE_DIRECT_NONE_MV) + mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {