--- trunk/xvidcore/src/motion/estimation_rd_based_bvop.c 2004/12/19 13:16:50 1583 +++ trunk/xvidcore/src/motion/estimation_rd_based_bvop.c 2010/12/24 13:21:35 1919 @@ -1,3 +1,28 @@ +/***************************************************************************** + * + * XVID MPEG-4 VIDEO CODEC + * - Rate-Distortion Based Motion Estimation for B- VOPs - + * + * Copyright(C) 2004 Radoslaw Czyz + * Copyright(C) 2010 Michael Militzer + * + * This program is free software ; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation ; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY ; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program ; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id: estimation_rd_based_bvop.c,v 1.12 2010-12-24 13:21:35 Isibaar Exp $ + * + ****************************************************************************/ #include #include @@ -21,7 +46,6 @@ /* rd = BITS_MULT*bits + LAMBDA*distortion */ #define LAMBDA ( (int)(BITS_MULT*1.0) ) - static __inline unsigned int Block_CalcBits_BVOP(int16_t * const coeff, int16_t * const data, @@ -32,13 +56,16 @@ const uint16_t * scan_table, const unsigned int lambda, const uint16_t * mpeg_quant_matrices, - const unsigned int quant_sq) + const unsigned int quant_sq, + int * const cbpcost, + const unsigned int rel_var8, + const unsigned int metric) { int sum; int bits; int distortion = 0; - fdct(data); + fdct((short * const)data); if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices); else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); @@ -46,11 +73,15 @@ if ((sum >= 3) || (coeff[1] != 0) || (coeff[8] != 0) || (coeff[0] != 0)) { *cbp |= 1 << (5 - block); bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); + bits += *cbpcost; + *cbpcost = 0; /* don't add cbp cost second time */ if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); - distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t)); + if (metric) distortion = masked_sseh8_16bit(data, dqcoeff, rel_var8); + else distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t)); + } else { const static int16_t zero_block[64] = { @@ -64,7 +95,10 @@ 0, 0, 0, 0, 0, 0, 0, 0, }; bits = 0; - distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); + + if (metric) distortion = masked_sseh8_16bit(data, (int16_t * const) zero_block, rel_var8); + else distortion = sse8_16bit(data, (int16_t * const) zero_block, 8*sizeof(int16_t)); + } return bits + (lambda*distortion)/quant_sq; @@ -81,13 +115,16 @@ const uint16_t * scan_table, const unsigned int lambda, const uint16_t * mpeg_quant_matrices, - const unsigned int quant_sq) + const unsigned int quant_sq, + int * const cbpcost, + const unsigned int rel_var8, + const unsigned int metric) { int sum; int bits; int distortion = 0; - fdct(data); + fdct((short * const)data); if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices); else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); @@ -95,11 +132,15 @@ if ((sum >= 3) || (coeff[1] != 0) || (coeff[8] != 0) || (coeff[0] > 0) || (coeff[0] < -1)) { *cbp |= 1 << (5 - block); bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); + bits += *cbpcost; + *cbpcost = 0; if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); - distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t)); + if (metric) distortion = masked_sseh8_16bit(data, dqcoeff, rel_var8); + else distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t)); + } else { const static int16_t zero_block[64] = { @@ -113,7 +154,10 @@ 0, 0, 0, 0, 0, 0, 0, 0, }; bits = 0; - distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); + + if (metric) distortion = masked_sseh8_16bit(data, (int16_t * const) zero_block, rel_var8); + else distortion = sse8_16bit(data, (int16_t * const) zero_block, 8*sizeof(int16_t)); + } return bits + (lambda*distortion)/quant_sq; @@ -129,6 +173,7 @@ const uint8_t * ptr; int i, xc, yc; unsigned cbp = 0; + int cbpcost = 7*BITS_MULT; /* how much to add if cbp turns out to be non-zero */ if ( (x > data->max_dx) || (x < data->min_dx) || (y > data->max_dy) || (y < data->min_dy) ) return; @@ -149,7 +194,8 @@ int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, - &cbp, i, data->scan_table, data->lambda[i], data->mpeg_quant_matrices, data->quant_sq); + &cbp, i, data->scan_table, data->lambda[i], data->mpeg_quant_matrices, + data->quant_sq, &cbpcost, data->rel_var8[i], data->metric); if (rd >= data->iMinSAD[0]) return; } @@ -161,16 +207,16 @@ ptr = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, - &cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices, data->quant_sq); + &cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices, + data->quant_sq, &cbpcost, data->rel_var8[4], data->metric); if (rd >= data->iMinSAD[0]) return; /* chroma V */ ptr = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, - &cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, data->quant_sq); - - if (cbp) rd += BITS_MULT * 7; + &cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, + data->quant_sq, &cbpcost, data->rel_var8[5], data->metric); if (rd < data->iMinSAD[0]) { data->iMinSAD[0] = rd; @@ -189,6 +235,7 @@ unsigned int cbp = 0; unsigned int k; VECTOR mvs, b_mvs; + int cbpcost = 6*BITS_MULT; /* how much to add if cbp turns out to be non-zero */ const uint8_t *ReferenceF, *ReferenceB; @@ -227,7 +274,8 @@ transfer_8to16sub2ro(in, data->Cur + s, ReferenceF, ReferenceB, data->iEdgedWidth); rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, - &cbp, k, data->scan_table, data->lambda[k], data->mpeg_quant_matrices, data->quant_sq); + &cbp, k, data->scan_table, data->lambda[k], data->mpeg_quant_matrices, + data->quant_sq, &cbpcost, data->rel_var8[k], data->metric); if (rd > *(data->iMinSAD)) return; } @@ -242,7 +290,8 @@ ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[4], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, - &cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices, data->quant_sq); + &cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices, + data->quant_sq, &cbpcost, data->rel_var8[4], data->metric); if (rd >= data->iMinSAD[0]) return; /* chroma V */ @@ -250,10 +299,9 @@ ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[5], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, - &cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, data->quant_sq); + &cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, + data->quant_sq, &cbpcost, data->rel_var8[5], data->metric); - if (cbp) - rd += BITS_MULT * 6; if (cbp || x != 0 || y != 0) rd += BITS_MULT * d_mv_bits(x, y, zeroMV, 1, 0); @@ -273,6 +321,7 @@ int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; unsigned int cbp = 0; unsigned int i; + int cbpcost = 7*BITS_MULT; /* how much to add if cbp turns out to be non-zero */ const uint8_t *ReferenceF, *ReferenceB; VECTOR *current; @@ -315,9 +364,10 @@ if (rd >= *data->iMinSAD) return; transfer_8to16sub2ro(in, data->Cur + s, ReferenceF + s, ReferenceB + s, data->iEdgedWidth); rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, - i, data->scan_table, data->lambda[i], data->mpeg_quant_matrices, data->quant_sq); + i, data->scan_table, data->lambda[i], data->mpeg_quant_matrices, + data->quant_sq, &cbpcost, data->rel_var8[i], data->metric); } - + /* chroma */ xcf = (xcf >> 1) + roundtab_79[xcf & 0x3]; ycf = (ycf >> 1) + roundtab_79[ycf & 0x3]; @@ -329,7 +379,8 @@ ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[4], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, - 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices, data->quant_sq); + 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices, + data->quant_sq, &cbpcost, data->rel_var8[4], data->metric); if (rd >= data->iMinSAD[0]) return; @@ -338,9 +389,8 @@ ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[5], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, - 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, data->quant_sq); - - if (cbp) rd += BITS_MULT * 7; + 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, + data->quant_sq, &cbpcost, data->rel_var8[5], data->metric); if (rd < *(data->iMinSAD)) { *data->iMinSAD = rd; @@ -446,9 +496,11 @@ VECTOR * f_predMV, VECTOR * b_predMV, const uint32_t MotionFlags, + const uint32_t VopFlags, const MBParam * const pParam, int x, int y, - int best_sad) + int best_sad, + int force_direct) { int mode = MODE_DIRECT, k; int f_rd, b_rd, i_rd, d_rd, best_rd; @@ -460,6 +512,8 @@ int order[4] = {MODE_DIRECT, MODE_FORWARD, MODE_BACKWARD, MODE_INTERPOLATE}; + Data_d->metric = Data_b->metric = Data_f->metric = Data_i->metric = !!(VopFlags & XVID_VOP_RD_PSNRHVSM); + Data_d->scan_table = Data_b->scan_table = Data_f->scan_table = Data_i->scan_table = /*VopFlags & XVID_VOP_ALTERNATESCAN ? scan_tables[2] : */scan_tables[0]; *Data_f->cbp = *Data_b->cbp = *Data_i->cbp = *Data_d->cbp = 63; @@ -467,11 +521,23 @@ f_rd = b_rd = i_rd = d_rd = best_rd = 256*4096; for (i = 0; i < 6; i++) { - int lam = (LAMBDA*iQuant*iQuant)/(ref_quant*(ref_quant+1)); /* re-calculate as if it was p-frame's quant +.5 */ + /* re-calculate as if it was p-frame's quant +.5 */ + int lam = (pMB->lambda[i]*LAMBDA*iQuant*iQuant)/(ref_quant*(ref_quant+1)); + lam >>= LAMBDA_EXP; Data_d->lambda[i] = lam; Data_b->lambda[i] = lam; Data_f->lambda[i] = lam; Data_i->lambda[i] = lam; + + Data_d->rel_var8[i] = pMB->rel_var8[i]; + Data_b->rel_var8[i] = pMB->rel_var8[i]; + Data_f->rel_var8[i] = pMB->rel_var8[i]; + Data_i->rel_var8[i] = pMB->rel_var8[i]; + } + + if (force_direct) { + best_rd = 0; + goto set_mode; /* bypass checks for non-direct modes */ } /* find the best order of evaluation - smallest SAD comes first, because *if* it means smaller RD, @@ -527,6 +593,8 @@ best_rd = 0; } + +set_mode: pMB->sad16 = best_rd; pMB->mode = mode;