--- branches/dev-api-4/xvidcore/src/encoder.c 2003/03/15 16:04:38 922 +++ branches/dev-api-4/xvidcore/src/encoder.c 2003/04/27 21:48:39 1000 @@ -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.6 2003-03-15 16:04:38 suxen_drol Exp $ + * $Id: encoder.c,v 1.95.2.20 2003-04-27 21:48:39 edgomez 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 @@ -134,12 +116,12 @@ int -enc_create(xvid_enc_create_t * create, xvid_enc_rc_t * rc) +enc_create(xvid_enc_create_t * create) { Encoder *pEnc; int n; - if (XVID_MAJOR(create->version) != 1 || (rc && XVID_MAJOR(rc->version) != 1)) /* v1.x.x */ + if (XVID_MAJOR(create->version) != 1) /* v1.x.x */ return XVID_ERR_VERSION; if (create->width%2 || create->height%2) @@ -166,13 +148,19 @@ /* 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; - pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE); - if (pEnc->plugins == NULL) - goto xvid_err_memory0; + /* plugins */ + if(create->num_plugins > 0) { + pEnc->num_plugins = create->num_plugins; + pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE); + if (pEnc->plugins == NULL) + goto xvid_err_memory0; + } else { + pEnc->num_plugins = 0; + pEnc->plugins = NULL; + } for (n=0; nnum_plugins;n++) { xvid_plg_create_t pcreate; @@ -198,14 +186,16 @@ } } - if ((pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) || + if ((pEnc->mbParam.global_flags & XVID_GLOBAL_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 */ @@ -217,8 +207,8 @@ pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); /* max keyframe interval */ - pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <=0 ? 250 : create->max_key_interval; - /*XXX: replace 250 hard code with "10seconds * framerate" */ + pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? + 10 * pEnc->mbParam.fbase / pEnc->mbParam.fincr : create->max_key_interval; /* Bitrate allocator defaults @@ -503,7 +493,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++) { @@ -590,6 +582,8 @@ pEnc->mbParam.edged_height); image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); + image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, + pEnc->mbParam.edged_height); if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, @@ -606,14 +600,25 @@ 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) { - pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0); + + if (pEnc->num_plugins>0) { + xvid_plg_destroy_t pdestroy; + memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t)); + + pdestroy.version = XVID_VERSION; + pdestroy.num_frames = pEnc->m_framenum; + + for (i=0; inum_plugins;i++) { + if (pEnc->plugins[i].func) { + pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, 0); + } } + xvid_free(pEnc->plugins); } - xvid_free(pEnc->plugins); xvid_free(pEnc); @@ -628,17 +633,21 @@ 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; data.reference.plane[0] = pEnc->reference->image.y; data.reference.plane[1] = pEnc->reference->image.u; @@ -660,10 +669,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)) { @@ -676,7 +689,7 @@ data.original.stride[2] = pEnc->mbParam.edged_width/2; } - if ((frame->vol_flags & XVID_EXTRASTATS) || + if ((frame->vol_flags & XVID_VOL_EXTRASTATS) || (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { data.sse_y = @@ -697,7 +710,17 @@ 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; @@ -707,8 +730,7 @@ data.mblks = frame->sStat.mblks; data.ublks = frame->sStat.ublks; - if (stats) - { + if (stats) { stats->type = coding2type(frame->coding_type); stats->quant = frame->quant; stats->vol_flags = frame->vol_flags; @@ -724,8 +746,9 @@ } } - emms(); + /* 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; @@ -734,12 +757,24 @@ } 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*/ + } } @@ -750,7 +785,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 */ } @@ -766,7 +801,7 @@ set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base) { - pCur->ticks = (int32_t)pCur->stamp % time_base; + pCur->ticks = (int32_t)pCur->stamp % time_base; pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */ @@ -826,14 +861,14 @@ if (image_input (&q->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride, - xFrame->input.csp, xFrame->vol_flags & XVID_INTERLACING)) + xFrame->input.csp, xFrame->vol_flags & XVID_VOL_INTERLACING)) { emms(); return XVID_ERR_FORMAT; } stop_conv_timer(); - if ((xFrame->vop_flags & XVID_CHROMAOPT)) { + if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) { image_chroma_optimize(&q->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); } @@ -890,7 +925,7 @@ indentical to the future-referece frame. */ - if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) { + if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED && pEnc->bframenum_tail > 0)) { int tmp; int bits; @@ -932,7 +967,7 @@ if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ { - 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) { call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); } @@ -977,45 +1012,20 @@ * init pEnc->current fields * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ + pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; + inc_frame_num(pEnc); pEnc->current->vol_flags = pEnc->mbParam.vol_flags; pEnc->current->vop_flags = frame->vop_flags; pEnc->current->motion_flags = frame->motion; pEnc->current->fcode = pEnc->mbParam.m_fcode; pEnc->current->bcode = pEnc->mbParam.m_fcode; - if ((xFrame->vop_flags & XVID_CHROMAOPT)) { + + if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) { image_chroma_optimize(&pEnc->current->image, 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 - } - - } - emms(); /* END floating-point region */ - /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% * frame type & quant selection * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ @@ -1037,9 +1047,9 @@ }else{ type = MEanalysis(&pEnc->reference->image, pEnc->current, &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, - pEnc->iFrameNum, pEnc->bframenum_tail); + pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold); - if (type == B_VOP && !(pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES)) { + if (type == B_VOP && !(pEnc->current->vop_flags & XVID_VOP_DYNAMIC_BFRAMES)) { type = P_VOP; /* disable dynamic bframes */ } } @@ -1054,10 +1064,9 @@ } } - inc_frame_num(pEnc); pEnc->iFrameNum++; - if ((pEnc->current->vop_flags & XVID_DEBUG)) { + if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, "%i st:%i if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum); } @@ -1067,16 +1076,16 @@ * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later) * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ if (type == B_VOP) { - if ((pEnc->current->vop_flags & XVID_DEBUG)) { + if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { 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) @@ -1098,7 +1107,7 @@ } /* for unpacked bframes, output the stats for the last encoded frame */ - if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) + if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { if (pEnc->current->stamp > 0) { call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); @@ -1112,23 +1121,23 @@ * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ - 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) { // 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--; SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); - if ((pEnc->current->vop_flags & XVID_DEBUG)) { + if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); } @@ -1148,7 +1157,7 @@ pEnc->bframenum_head, pEnc->bframenum_tail, pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); - if ((pEnc->current->vop_flags & XVID_DEBUG)) { + if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); } @@ -1156,7 +1165,7 @@ /* ---- update vol flags at IVOP ----------- */ pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; - if ((pEnc->mbParam.vol_flags & XVID_MPEGQUANT)) { + if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { if (frame->quant_intra_matrix != NULL) set_intra_matrix(frame->quant_intra_matrix); if (frame->quant_inter_matrix != NULL) @@ -1165,11 +1174,11 @@ /* prevent vol/vop misuse */ - if (!(pEnc->current->vol_flags & XVID_REDUCED_ENABLE)) - pEnc->current->vop_flags &= ~XVID_REDUCED; + if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) + pEnc->current->vop_flags &= ~XVID_VOP_REDUCED; - if (!(pEnc->current->vol_flags & XVID_INTERLACING)) - pEnc->current->vop_flags &= ~(XVID_TOPFIELDFIRST|XVID_ALTERNATESCAN); + if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING)) + pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN); /* ^^^------------------------ */ @@ -1191,7 +1200,7 @@ pEnc->bframenum_head, pEnc->bframenum_tail, pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); - if ((pEnc->current->vop_flags & XVID_DEBUG)) { + if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); } @@ -1213,12 +1222,12 @@ pEnc->flush_bframes = 1; /* packed & queued_bframes: dont bother outputting stats, we do so after the flush */ - if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) { + if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { goto repeat; } /* packed or no-bframes: output stats */ - 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) { call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); } @@ -1236,6 +1245,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) @@ -1249,19 +1276,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; + } } @@ -1279,7 +1296,7 @@ uint16_t x, y; - if ((pEnc->current->vol_flags & XVID_REDUCED_ENABLE)) + if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) { mb_width = (pEnc->mbParam.width + 31) / 32; mb_height = (pEnc->mbParam.height + 31) / 32; @@ -1296,6 +1313,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); @@ -1323,7 +1342,7 @@ stop_prediction_timer(); start_timer(); - if (pEnc->current->vop_flags & XVID_GREYSCALE) + if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) { pMB->cbp &= 0x3C; /* keep only bits 5-2 */ qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ qcoeff[5*64+0]=0; @@ -1332,7 +1351,7 @@ stop_coding_timer(); } - if ((pEnc->current->vop_flags & XVID_REDUCED)) + if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) { image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, @@ -1340,11 +1359,16 @@ } emms(); +/* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */ +#if 0 /* for divx5 compatibility, we must always pad between the packed p and b frames */ - if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) + if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) +#endif BitstreamPadAlways(bs); +#if 0 else BitstreamPad(bs); +#endif pEnc->current->length = (BitstreamPos(bs) - bits) / 8; pEnc->fMvPrevSigma = -1; @@ -1387,7 +1411,7 @@ /* IMAGE *pCurrent = &pEnc->current->image; */ IMAGE *pRef = &pEnc->reference->image; - if ((pEnc->current->vop_flags & XVID_REDUCED)) + if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) { mb_width = (pEnc->mbParam.width + 31) / 32; mb_height = (pEnc->mbParam.height + 31) / 32; @@ -1409,17 +1433,20 @@ else iLimit = mb_width * mb_height + 1; - if ((pEnc->current->vop_flags & XVID_HALFPEL)) { + if ((pEnc->current->vop_flags & XVID_VOP_HALFPEL)) { start_timer(); image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, - (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), pEnc->current->rounding_type); stop_inter_timer(); } pEnc->current->coding_type = P_VOP; + + + SetMacroblockQuants(&pEnc->mbParam, pEnc->current); start_timer(); /*if (pEnc->current->global_flags & XVID_HINTEDME_SET) @@ -1434,7 +1461,7 @@ if (bIntra == 1) return FrameCodeI(pEnc, bs); - if ( ( pEnc->current->vol_flags & XVID_GMC ) + if ( ( pEnc->current->vol_flags & XVID_VOL_GMC ) && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) { pEnc->current->coding_type = S_VOP; @@ -1446,7 +1473,7 @@ generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, - pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0, + pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0, pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); } @@ -1495,7 +1522,7 @@ pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, pEnc->mbParam.edged_width, 65536); - if (pEnc->current->motion_flags & PMV_CHROMA16) { + if (pEnc->current->motion_flags & XVID_ME_CHROMA16) { iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); @@ -1505,7 +1532,7 @@ if (iSAD <= pMB->sad16) { /* mode decision GMC */ - if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) + if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; else pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; @@ -1528,23 +1555,15 @@ dct_codes, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, - (pEnc->current->vol_flags & XVID_QUARTERPEL), - (pEnc->current->vop_flags & XVID_REDUCED), + (pEnc->current->vol_flags & XVID_VOL_QUARTERPEL), + (pEnc->current->vop_flags & XVID_VOP_REDUCED), pEnc->current->rounding_type); 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; @@ -1567,12 +1586,12 @@ /* 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); else if (pEnc->current->coding_type == P_VOP) { - if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) + if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); else skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); @@ -1599,7 +1618,7 @@ } if (!bSkip) { /* no SKIP, but trivial block */ - if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) { + if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); pMB->pmvs[0].x = - predMV.x; pMB->pmvs[0].y = - predMV.y; @@ -1626,13 +1645,13 @@ } /* ordinary case: normal coded INTER/INTER4V block */ - if ((pEnc->current->vop_flags & XVID_GREYSCALE)) + if ((pEnc->current->vop_flags & XVID_VOP_GREYSCALE)) { pMB->cbp &= 0x3C; /* keep only bits 5-2 */ qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ qcoeff[5*64+0]=0; } - if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) { + if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; @@ -1649,7 +1668,7 @@ { int k; for (k=1;k<4;k++) { - if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) { + if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; @@ -1670,7 +1689,7 @@ } } - if ((pEnc->current->vop_flags & XVID_REDUCED)) + if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) { image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, @@ -1692,14 +1711,14 @@ iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); if ((fSigma > iSearchRange / 3) - && (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 */ { pEnc->mbParam.m_fcode++; iSearchRange *= 2; } else if ((fSigma < iSearchRange / 6) && (pEnc->fMvPrevSigma >= 0) && (pEnc->fMvPrevSigma < iSearchRange / 6) - && (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 */ { pEnc->mbParam.m_fcode--; iSearchRange /= 2; @@ -1746,11 +1765,16 @@ } */ +/* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */ +#if 0 /* for divx5 compatibility, we must always pad between the packed p and b frames */ - if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) + if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) +#endif BitstreamPadAlways(bs); +#if 0 else BitstreamPad(bs); +#endif pEnc->current->length = (BitstreamPos(bs) - bits) / 8; @@ -1778,7 +1802,7 @@ fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ } - /* 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 */ if (!first){ fp=fopen("C:\\XVIDDBGE.TXT","w"); @@ -1794,7 +1818,7 @@ start_timer(); image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, - (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0); + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); stop_inter_timer(); /* backward */ @@ -1804,7 +1828,7 @@ start_timer(); image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, - (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0); + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); stop_inter_timer(); start_timer(); @@ -1841,7 +1865,7 @@ for (y = 0; y < pEnc->mbParam.mb_height; y++) { for (x = 0; x < pEnc->mbParam.mb_width; x++) { MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; - int direction = frame->vop_flags & XVID_ALTERNATESCAN ? 2 : 0; + int direction = frame->vop_flags & XVID_VOP_ALTERNATESCAN ? 2 : 0; /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ if (mb->mode == MODE_NOT_CODED) { @@ -1849,7 +1873,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, @@ -1859,8 +1883,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) ) { @@ -1882,7 +1906,7 @@ /* TODO: dynamic fcode/bcode ??? */ - BitstreamPad(bs); + BitstreamPadAlways(bs); frame->length = (BitstreamPos(bs) - bits) / 8; #ifdef BFRAMES_DEC_DEBUG