19 |
* along with this program ; if not, write to the Free Software |
* along with this program ; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
* |
* |
22 |
* $Id: estimation_rd_based_bvop.c,v 1.7 2004-12-19 13:53:54 edgomez Exp $ |
* $Id: estimation_rd_based_bvop.c,v 1.9 2005-11-22 10:23:01 suxen_drol Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
56 |
const uint16_t * scan_table, |
const uint16_t * scan_table, |
57 |
const unsigned int lambda, |
const unsigned int lambda, |
58 |
const uint16_t * mpeg_quant_matrices, |
const uint16_t * mpeg_quant_matrices, |
59 |
const unsigned int quant_sq) |
const unsigned int quant_sq, |
60 |
|
int * const cbpcost) |
61 |
{ |
{ |
62 |
int sum; |
int sum; |
63 |
int bits; |
int bits; |
64 |
int distortion = 0; |
int distortion = 0; |
65 |
|
|
66 |
fdct(data); |
fdct((short * const)data); |
67 |
|
|
68 |
if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices); |
if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices); |
69 |
else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); |
else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); |
71 |
if ((sum >= 3) || (coeff[1] != 0) || (coeff[8] != 0) || (coeff[0] != 0)) { |
if ((sum >= 3) || (coeff[1] != 0) || (coeff[8] != 0) || (coeff[0] != 0)) { |
72 |
*cbp |= 1 << (5 - block); |
*cbp |= 1 << (5 - block); |
73 |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); |
74 |
|
bits += *cbpcost; |
75 |
|
*cbpcost = 0; /* don't add cbp cost second time */ |
76 |
|
|
77 |
if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
78 |
else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
108 |
const uint16_t * scan_table, |
const uint16_t * scan_table, |
109 |
const unsigned int lambda, |
const unsigned int lambda, |
110 |
const uint16_t * mpeg_quant_matrices, |
const uint16_t * mpeg_quant_matrices, |
111 |
const unsigned int quant_sq) |
const unsigned int quant_sq, |
112 |
|
int * const cbpcost) |
113 |
{ |
{ |
114 |
int sum; |
int sum; |
115 |
int bits; |
int bits; |
116 |
int distortion = 0; |
int distortion = 0; |
117 |
|
|
118 |
fdct(data); |
fdct((short * const)data); |
119 |
|
|
120 |
if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices); |
if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices); |
121 |
else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); |
else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); |
123 |
if ((sum >= 3) || (coeff[1] != 0) || (coeff[8] != 0) || (coeff[0] > 0) || (coeff[0] < -1)) { |
if ((sum >= 3) || (coeff[1] != 0) || (coeff[8] != 0) || (coeff[0] > 0) || (coeff[0] < -1)) { |
124 |
*cbp |= 1 << (5 - block); |
*cbp |= 1 << (5 - block); |
125 |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); |
126 |
|
bits += *cbpcost; |
127 |
|
*cbpcost = 0; |
128 |
|
|
129 |
if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
130 |
else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
159 |
const uint8_t * ptr; |
const uint8_t * ptr; |
160 |
int i, xc, yc; |
int i, xc, yc; |
161 |
unsigned cbp = 0; |
unsigned cbp = 0; |
162 |
|
int cbpcost = 7*BITS_MULT; /* how much to add if cbp turns out to be non-zero */ |
163 |
|
|
164 |
if ( (x > data->max_dx) || (x < data->min_dx) |
if ( (x > data->max_dx) || (x < data->min_dx) |
165 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
180 |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
181 |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
182 |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
183 |
&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, |
184 |
|
data->quant_sq, &cbpcost); |
185 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
186 |
} |
} |
187 |
|
|
193 |
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
194 |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
195 |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
196 |
&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, |
197 |
|
data->quant_sq, &cbpcost); |
198 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
199 |
|
|
200 |
/* chroma V */ |
/* chroma V */ |
201 |
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
202 |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
203 |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
204 |
&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, |
205 |
|
data->quant_sq, &cbpcost); |
|
if (cbp) rd += BITS_MULT * 7; |
|
206 |
|
|
207 |
if (rd < data->iMinSAD[0]) { |
if (rd < data->iMinSAD[0]) { |
208 |
data->iMinSAD[0] = rd; |
data->iMinSAD[0] = rd; |
221 |
unsigned int cbp = 0; |
unsigned int cbp = 0; |
222 |
unsigned int k; |
unsigned int k; |
223 |
VECTOR mvs, b_mvs; |
VECTOR mvs, b_mvs; |
224 |
|
int cbpcost = 6*BITS_MULT; /* how much to add if cbp turns out to be non-zero */ |
225 |
|
|
226 |
const uint8_t *ReferenceF, *ReferenceB; |
const uint8_t *ReferenceF, *ReferenceB; |
227 |
|
|
260 |
|
|
261 |
transfer_8to16sub2ro(in, data->Cur + s, ReferenceF, ReferenceB, data->iEdgedWidth); |
transfer_8to16sub2ro(in, data->Cur + s, ReferenceF, ReferenceB, data->iEdgedWidth); |
262 |
rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
263 |
&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, |
264 |
|
data->quant_sq, &cbpcost); |
265 |
if (rd > *(data->iMinSAD)) return; |
if (rd > *(data->iMinSAD)) return; |
266 |
} |
} |
267 |
|
|
276 |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[4], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[4], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
277 |
transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
278 |
rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
279 |
&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, |
280 |
|
data->quant_sq, &cbpcost); |
281 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
282 |
|
|
283 |
/* chroma V */ |
/* chroma V */ |
285 |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[5], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[5], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
286 |
transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
287 |
rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
288 |
&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, |
289 |
|
data->quant_sq, &cbpcost); |
290 |
|
|
|
if (cbp) |
|
|
rd += BITS_MULT * 6; |
|
291 |
if (cbp || x != 0 || y != 0) |
if (cbp || x != 0 || y != 0) |
292 |
rd += BITS_MULT * d_mv_bits(x, y, zeroMV, 1, 0); |
rd += BITS_MULT * d_mv_bits(x, y, zeroMV, 1, 0); |
293 |
|
|
307 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
308 |
unsigned int cbp = 0; |
unsigned int cbp = 0; |
309 |
unsigned int i; |
unsigned int i; |
310 |
|
int cbpcost = 7*BITS_MULT; /* how much to add if cbp turns out to be non-zero */ |
311 |
|
|
312 |
const uint8_t *ReferenceF, *ReferenceB; |
const uint8_t *ReferenceF, *ReferenceB; |
313 |
VECTOR *current; |
VECTOR *current; |
350 |
if (rd >= *data->iMinSAD) return; |
if (rd >= *data->iMinSAD) return; |
351 |
transfer_8to16sub2ro(in, data->Cur + s, ReferenceF + s, ReferenceB + s, data->iEdgedWidth); |
transfer_8to16sub2ro(in, data->Cur + s, ReferenceF + s, ReferenceB + s, data->iEdgedWidth); |
352 |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, |
353 |
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, |
354 |
|
data->quant_sq, &cbpcost); |
355 |
} |
} |
356 |
|
|
357 |
/* chroma */ |
/* chroma */ |
365 |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[4], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[4], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
366 |
transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
367 |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, |
368 |
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, |
369 |
|
data->quant_sq, &cbpcost); |
370 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
371 |
|
|
372 |
|
|
375 |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[5], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[5], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
376 |
transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
377 |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, |
378 |
5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, data->quant_sq); |
5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, |
379 |
|
data->quant_sq, &cbpcost); |
|
if (cbp) rd += BITS_MULT * 7; |
|
380 |
|
|
381 |
if (rd < *(data->iMinSAD)) { |
if (rd < *(data->iMinSAD)) { |
382 |
*data->iMinSAD = rd; |
*data->iMinSAD = rd; |