--- estimation_rd_based_bvop.c 2004/07/21 12:50:30 1.3 +++ estimation_rd_based_bvop.c 2004/12/19 14:05:55 1.8 @@ -1,3 +1,27 @@ +/***************************************************************************** + * + * XVID MPEG-4 VIDEO CODEC + * - Rate-Distortion Based Motion Estimation for B- VOPs - + * + * Copyright(C) 2004 Radoslaw Czyz + * + * 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.8 2004/12/19 14:05:55 syskin Exp $ + * + ****************************************************************************/ #include #include @@ -31,7 +55,9 @@ const int block, const uint16_t * scan_table, const unsigned int lambda, - const uint16_t * mpeg_quant_matrices) + const uint16_t * mpeg_quant_matrices, + const unsigned int quant_sq, + int * const cbpcost) { int sum; int bits; @@ -45,6 +71,8 @@ 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); @@ -66,7 +94,7 @@ distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); } - return bits + (lambda*distortion)/(quant*quant); + return bits + (lambda*distortion)/quant_sq; } @@ -79,7 +107,9 @@ const int block, const uint16_t * scan_table, const unsigned int lambda, - const uint16_t * mpeg_quant_matrices) + const uint16_t * mpeg_quant_matrices, + const unsigned int quant_sq, + int * const cbpcost) { int sum; int bits; @@ -93,6 +123,8 @@ 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); @@ -114,26 +146,7 @@ distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); } - return bits + (lambda*distortion)/(quant*quant); -} - - -static void -transfer_8to16sub2ro(int16_t * const dct, - const uint8_t * const cur, - const uint8_t * ref1, - const uint8_t * ref2, - const uint32_t stride) -{ - uint32_t i, j; - - for (j = 0; j < 8; j++) { - for (i = 0; i < 8; i++) { - uint8_t c = cur[j * stride + i]; - int r = (ref1[j * stride + i] + ref2[j * stride + i] + 1) / 2; - dct[j * 8 + i] = (int16_t) c - (int16_t) r; - } - } + return bits + (lambda*distortion)/quant_sq; } static void @@ -141,11 +154,12 @@ { int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; - int32_t rd = 3*BITS_MULT; /* note to self: 3 bits minimum, but maybe 4 if it's forward mode */ + int32_t rd = (3+2)*BITS_MULT; /* 3 bits for mode + 2 for vector (minimum) */ VECTOR * current; 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; @@ -160,12 +174,14 @@ xc = x/2; yc = y/2; } - rd += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); + rd += BITS_MULT*(d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision)-2); for(i = 0; i < 4; i++) { 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); + 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, &cbpcost); if (rd >= data->iMinSAD[0]) return; } @@ -176,15 +192,17 @@ /* chroma U */ 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); + 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, &cbpcost); 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); - - if (cbp) rd += BITS_MULT * 7; + 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, &cbpcost); if (rd < data->iMinSAD[0]) { data->iMinSAD[0] = rd; @@ -194,7 +212,6 @@ } } - static void CheckCandidateRDDirect(const int x, const int y, SearchData * const data, const unsigned int Direction) { @@ -204,6 +221,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; @@ -241,7 +259,9 @@ } 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); + 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, &cbpcost); if (rd > *(data->iMinSAD)) return; } @@ -255,19 +275,21 @@ ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding); 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); + 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, &cbpcost); if (rd >= data->iMinSAD[0]) return; /* chroma V */ ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding); 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); + 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, &cbpcost); - if (cbp) - rd += BITS_MULT * 6; if (cbp || x != 0 || y != 0) - rd += BITS_MULT * d_mv_bits(x, y, zeroMV, 1, 0, 0); + rd += BITS_MULT * d_mv_bits(x, y, zeroMV, 1, 0); if (rd < *(data->iMinSAD)) { *data->iMinSAD = rd; @@ -277,9 +299,6 @@ } } - - - static void CheckCandidateRDInt(const int x, const int y, SearchData * const data, const unsigned int Direction) { @@ -288,6 +307,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; @@ -322,17 +342,18 @@ xcb = xb/2; ycb = yb/2; } - rd += BITS_MULT * (d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0) - + d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision, 0)); + rd += BITS_MULT * (d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision) + + d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision)); - for(i = 0; i < 4; i++) { int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); 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); + 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, &cbpcost); } - + /* chroma */ xcf = (xcf >> 1) + roundtab_79[xcf & 0x3]; ycf = (ycf >> 1) + roundtab_79[ycf & 0x3]; @@ -343,7 +364,9 @@ ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding); 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); + 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, &cbpcost); if (rd >= data->iMinSAD[0]) return; @@ -351,9 +374,9 @@ ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding); 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); - - if (cbp) rd += BITS_MULT * 7; + 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, &cbpcost); if (rd < *(data->iMinSAD)) { *data->iMinSAD = rd; @@ -375,7 +398,7 @@ Data->iMinSAD[0] = *best_sad; get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, - x, y, 4, pParam->width, pParam->height, Data->iFcode, 1 + Data->qpel, 0); + x, y, 4, pParam->width, pParam->height, Data->iFcode, 1 + Data->qpel); Data->qpel_precision = Data->qpel; @@ -418,7 +441,7 @@ Data->iMinSAD[0] = *best_sad; get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, - x, y, 4, pParam->width, pParam->height, Data->iFcode, 1 + Data->qpel, 0); + x, y, 4, pParam->width, pParam->height, Data->iFcode, 1 + Data->qpel); Data->qpel_precision = Data->qpel; @@ -460,7 +483,8 @@ VECTOR * b_predMV, const uint32_t MotionFlags, const MBParam * const pParam, - int x, int y) + int x, int y, + int best_sad) { int mode = MODE_DIRECT, k; int f_rd, b_rd, i_rd, d_rd, best_rd; @@ -468,11 +492,13 @@ const uint32_t iQuant = Data_d->iQuant; int i; int ref_quant = b_mb->quant; + int no_of_checks = 0; int order[4] = {MODE_DIRECT, MODE_FORWARD, MODE_BACKWARD, MODE_INTERPOLATE}; 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; f_rd = b_rd = i_rd = d_rd = best_rd = 256*4096; @@ -500,29 +526,41 @@ } } - /* evaluate cost of all modes */ - for (i = 0; i < 4; i++) { - int rd; - - switch (order[i]) { - case MODE_DIRECT: - rd = d_rd = SearchDirect_RD(x, y, MotionFlags, pParam, &best_rd, Data_d); - break; - case MODE_FORWARD: - rd = f_rd = SearchBF_RD(x, y, MotionFlags, pParam, &best_rd, Data_f) + 1*BITS_MULT; /* extra one bit for FORWARD vs BACKWARD */ - break; - case MODE_BACKWARD: - rd = b_rd = SearchBF_RD(x, y, MotionFlags, pParam, &best_rd, Data_b); - break; - default: - case MODE_INTERPOLATE: - rd = i_rd = SearchInterpolate_RD(x, y, MotionFlags, pParam, &best_rd, Data_i); - break; - } - if (rd < best_rd) { - mode = order[i]; - best_rd = rd; + for(i = 0; i < 4; i++) + if (get_sad_for_mode(order[i], Data_d, Data_b, Data_f, Data_i) < 2*best_sad) + no_of_checks++; + + if (no_of_checks > 1) { + /* evaluate cost of all modes */ + for (i = 0; i < no_of_checks; i++) { + int rd; + if (2*best_sad < get_sad_for_mode(order[i], Data_d, Data_b, Data_f, Data_i)) + break; /* further SADs are too big */ + + switch (order[i]) { + case MODE_DIRECT: + rd = d_rd = SearchDirect_RD(x, y, MotionFlags, pParam, &best_rd, Data_d); + break; + case MODE_FORWARD: + rd = f_rd = SearchBF_RD(x, y, MotionFlags, pParam, &best_rd, Data_f) + 1*BITS_MULT; /* extra one bit for FORWARD vs BACKWARD */ + break; + case MODE_BACKWARD: + rd = b_rd = SearchBF_RD(x, y, MotionFlags, pParam, &best_rd, Data_b); + break; + default: + case MODE_INTERPOLATE: + rd = i_rd = SearchInterpolate_RD(x, y, MotionFlags, pParam, &best_rd, Data_i); + break; + } + if (rd < best_rd) { + mode = order[i]; + best_rd = rd; + } } + } else { + /* only 1 mode is below the threshold */ + mode = order[0]; + best_rd = 0; } pMB->sad16 = best_rd;