4 |
* - Rate-Distortion Based Motion Estimation for P- and S- VOPs - |
* - Rate-Distortion Based Motion Estimation for P- and S- VOPs - |
5 |
* |
* |
6 |
* Copyright(C) 2003 Radoslaw Czyz <xvid@syskin.cjb.net> |
* Copyright(C) 2003 Radoslaw Czyz <xvid@syskin.cjb.net> |
7 |
* 2003 Michael Militzer <michael@xvid.org> |
* 2003-2010 Michael Militzer <michael@xvid.org> |
8 |
* |
* |
9 |
* This program is free software ; you can redistribute it and/or modify |
* This program is free software ; you can redistribute it and/or modify |
10 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
56 |
uint32_t * cbp, |
uint32_t * cbp, |
57 |
const int block, |
const int block, |
58 |
const uint16_t * scan_table, |
const uint16_t * scan_table, |
59 |
const uint16_t * mpeg_quant_matrices) |
const unsigned int lambda, |
60 |
|
const uint16_t * mpeg_quant_matrices, |
61 |
|
const unsigned int quant_sq, |
62 |
|
const unsigned int rel_var8, |
63 |
|
const unsigned int metric) |
64 |
{ |
{ |
65 |
int sum; |
int sum; |
66 |
int bits; |
int bits; |
67 |
int distortion = 0; |
int distortion = 0; |
68 |
|
|
69 |
fdct(data); |
fdct((short * const)data); |
70 |
|
|
71 |
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); |
72 |
else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); |
else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); |
78 |
if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
79 |
else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
80 |
|
|
81 |
distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t)); |
if (metric) distortion = masked_sseh8_16bit(data, dqcoeff, rel_var8); |
82 |
|
else distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t)); |
83 |
|
|
84 |
} else { |
} else { |
85 |
const static int16_t zero_block[64] = |
const static int16_t zero_block[64] = |
86 |
{ |
{ |
94 |
0, 0, 0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 0, 0, |
95 |
}; |
}; |
96 |
bits = 0; |
bits = 0; |
|
distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); |
|
|
} |
|
97 |
|
|
98 |
|
if (metric) distortion = masked_sseh8_16bit(data, (int16_t * const) zero_block, rel_var8); |
99 |
|
else distortion = sse8_16bit(data, (int16_t * const) zero_block, 8*sizeof(int16_t)); |
100 |
|
|
101 |
|
} |
102 |
|
|
103 |
return bits + (LAMBDA*distortion)/(quant*quant); |
return bits + (lambda*distortion)/quant_sq; |
104 |
} |
} |
105 |
|
|
106 |
static __inline unsigned int |
static __inline unsigned int |
117 |
const int quant_type, |
const int quant_type, |
118 |
unsigned int bits[2], |
unsigned int bits[2], |
119 |
unsigned int cbp[2], |
unsigned int cbp[2], |
120 |
const uint16_t * mpeg_quant_matrices) |
unsigned int lambda, |
121 |
|
const uint16_t * mpeg_quant_matrices, |
122 |
|
const unsigned int quant_sq, |
123 |
|
const unsigned int metric, |
124 |
|
const int bound) |
125 |
{ |
{ |
126 |
int direction; |
int direction; |
127 |
int16_t *pCurrent; |
int16_t *pCurrent; |
129 |
unsigned int distortion = 0; |
unsigned int distortion = 0; |
130 |
const uint32_t iDcScaler = get_dc_scaler(quant, block < 4); |
const uint32_t iDcScaler = get_dc_scaler(quant, block < 4); |
131 |
|
|
132 |
fdct(coeff); |
fdct((short * const)coeff); |
133 |
|
|
134 |
if (quant_type) { |
if (quant_type) { |
135 |
quant_h263_intra(qcoeff, coeff, quant, iDcScaler, mpeg_quant_matrices); |
quant_h263_intra(qcoeff, coeff, quant, iDcScaler, mpeg_quant_matrices); |
140 |
} |
} |
141 |
|
|
142 |
predict_acdc(pMB-(x+mb_width*y), x, y, mb_width, block, qcoeff, |
predict_acdc(pMB-(x+mb_width*y), x, y, mb_width, block, qcoeff, |
143 |
quant, iDcScaler, predictors, 0); |
quant, iDcScaler, predictors, bound); |
144 |
|
|
145 |
direction = pMB->acpred_directions[block]; |
direction = pMB->acpred_directions[block]; |
146 |
pCurrent = pMB->pred_values[block]; |
pCurrent = (int16_t*)pMB->pred_values[block]; |
147 |
|
|
148 |
/* store current coeffs to pred_values[] for future prediction */ |
/* store current coeffs to pred_values[] for future prediction */ |
149 |
pCurrent[0] = qcoeff[0] * iDcScaler; |
pCurrent[0] = qcoeff[0] * iDcScaler; |
156 |
/* dc prediction */ |
/* dc prediction */ |
157 |
qcoeff[0] = qcoeff[0] - predictors[0]; |
qcoeff[0] = qcoeff[0] - predictors[0]; |
158 |
|
|
159 |
if (block < 4) bits[1] = bits[0] = dcy_tab[qcoeff[0] + 255].len; |
if (block < 4) bits[1] = bits[0] = dcy_tab[qcoeff[0] + 255].len - 3; /* 3 bits added before (4 times) */ |
160 |
else bits[1] = bits[0] = dcc_tab[qcoeff[0] + 255].len; |
else bits[1] = bits[0] = dcc_tab[qcoeff[0] + 255].len - 2; /* 2 bits added before (2 times)*/ |
161 |
|
|
162 |
/* calc cost before ac prediction */ |
/* calc cost before ac prediction */ |
163 |
bits[0] += coded = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]); |
bits[0] += coded = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]); |
179 |
bits[1] += coded = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]); |
bits[1] += coded = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]); |
180 |
if (coded > 0) cbp[1] |= 1 << (5 - block); |
if (coded > 0) cbp[1] |= 1 << (5 - block); |
181 |
|
|
182 |
distortion = sse8_16bit(coeff, dqcoeff, 8*sizeof(int16_t)); |
if (metric) distortion = masked_sseh8_16bit(coeff, dqcoeff, pMB->rel_var8[block]); |
183 |
|
else distortion = sse8_16bit(coeff, dqcoeff, 8*sizeof(int16_t)); |
184 |
|
|
185 |
return (LAMBDA*distortion)/(quant*quant); |
return (lambda*distortion)/quant_sq; |
186 |
} |
} |
187 |
|
|
188 |
|
|
192 |
{ |
{ |
193 |
|
|
194 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
195 |
int32_t rd = 0; |
/* minimum nuber of bits INTER can take is 1 (mcbpc) + 2 (cby) + 2 (vector) */ |
196 |
|
int32_t rd = BITS_MULT * (1+2+2); |
197 |
VECTOR * current; |
VECTOR * current; |
198 |
const uint8_t * ptr; |
const uint8_t * ptr; |
199 |
int i, t, xc, yc; |
int i, t, xc, yc; |
215 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
216 |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
217 |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
218 |
rd += data->temp[i] = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i, data->scan_table, data->mpeg_quant_matrices); |
rd += data->temp[i] = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, |
219 |
|
data->quant_type, &cbp, i, data->scan_table, data->lambda[i], |
220 |
|
data->mpeg_quant_matrices, data->quant_sq, data->rel_var8[i], data->metric); |
221 |
} |
} |
222 |
|
|
223 |
rd += t = BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
rd += t = BITS_MULT * (d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision) - 2); |
224 |
|
|
225 |
if (data->temp[0] + t < data->iMinSAD[1]) { |
if (data->temp[0] + t < data->iMinSAD[1]) { |
226 |
data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; data->cbp[1] = (data->cbp[1]&~32) | (cbp&32); } |
data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; data->cbp[1] = (data->cbp[1]&~32) | (cbp&32); } |
231 |
if (data->temp[3] < data->iMinSAD[4]) { |
if (data->temp[3] < data->iMinSAD[4]) { |
232 |
data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; data->cbp[1] = (data->cbp[1]&~4) | (cbp&4); } |
data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; data->cbp[1] = (data->cbp[1]&~4) | (cbp&4); } |
233 |
|
|
234 |
rd += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
rd += BITS_MULT * (xvid_cbpy_tab[15-(cbp>>2)].len - 2); |
235 |
|
|
236 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
237 |
|
|
242 |
/* chroma U */ |
/* chroma U */ |
243 |
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); |
244 |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
245 |
rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4, data->scan_table, data->mpeg_quant_matrices); |
rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
246 |
|
&cbp, 4, data->scan_table, data->lambda[4], |
247 |
|
data->mpeg_quant_matrices, data->quant_sq, data->rel_var8[4], data->metric); |
248 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
249 |
|
|
250 |
/* chroma V */ |
/* chroma V */ |
251 |
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); |
252 |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
253 |
rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5, data->scan_table, data->mpeg_quant_matrices); |
rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
254 |
|
&cbp, 5, data->scan_table, data->lambda[5], |
255 |
|
data->mpeg_quant_matrices, data->quant_sq, data->rel_var8[5], data->metric); |
256 |
|
|
257 |
rd += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
rd += BITS_MULT * (mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len - 1); /* one was added before */ |
258 |
|
|
259 |
if (rd < data->iMinSAD[0]) { |
if (rd < data->iMinSAD[0]) { |
260 |
data->iMinSAD[0] = rd; |
data->iMinSAD[0] = rd; |
286 |
} |
} |
287 |
|
|
288 |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
289 |
rd = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5, data->scan_table, data->mpeg_quant_matrices); |
rd = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
290 |
rd += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
&cbp, 5, data->scan_table, data->lambda[0], |
291 |
|
data->mpeg_quant_matrices, data->quant_sq, data->rel_var8[0], data->metric); |
292 |
|
/* we took 2 bits into account before */ |
293 |
|
rd += BITS_MULT * (d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision) - 2); |
294 |
|
|
295 |
|
|
296 |
if (rd < data->iMinSAD[0]) { |
if (rd < data->iMinSAD[0]) { |
297 |
*data->cbp = cbp; |
*data->cbp = cbp; |
322 |
if (MotionFlags & (XVID_ME_HALFPELREFINE16_RD | XVID_ME_EXTSEARCH_RD)) { /* we have to prepare for halfpixel-precision search */ |
if (MotionFlags & (XVID_ME_HALFPELREFINE16_RD | XVID_ME_EXTSEARCH_RD)) { /* we have to prepare for halfpixel-precision search */ |
323 |
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
324 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
325 |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv); |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 1); |
326 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
327 |
if (Data->currentQMV->x & 1 || Data->currentQMV->y & 1) |
if (Data->currentQMV->x & 1 || Data->currentQMV->y & 1) |
328 |
CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255); |
CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255); |
337 |
xvid_me_SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidateRD16); |
xvid_me_SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidateRD16); |
338 |
|
|
339 |
if (MotionFlags&XVID_ME_HALFPELREFINE16_RD) |
if (MotionFlags&XVID_ME_HALFPELREFINE16_RD) |
340 |
xvid_me_SubpelRefine(Data, CheckCandidateRD16); |
xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidateRD16, 0); |
341 |
|
|
342 |
if (Data->qpel) { |
if (Data->qpel) { |
343 |
if (MotionFlags&(XVID_ME_EXTSEARCH_RD | XVID_ME_HALFPELREFINE16_RD)) { /* there was halfpel-precision search */ |
if (MotionFlags&(XVID_ME_EXTSEARCH_RD | XVID_ME_HALFPELREFINE16_RD)) { /* there was halfpel-precision search */ |
349 |
/* preparing for qpel-precision search */ |
/* preparing for qpel-precision search */ |
350 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
351 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
352 |
pParam->width, pParam->height, Data->iFcode, 2, 0); |
pParam->width, pParam->height, Data->iFcode, 2); |
353 |
|
} |
354 |
|
if (MotionFlags & XVID_ME_QUARTERPELREFINE16_RD) { |
355 |
|
if (MotionFlags & XVID_ME_FASTREFINE16) |
356 |
|
FullRefine_Fast(Data, CheckCandidateRD16, 0); |
357 |
|
else |
358 |
|
xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidateRD16, 0); |
359 |
} |
} |
|
if (MotionFlags&XVID_ME_QUARTERPELREFINE16_RD) |
|
|
xvid_me_SubpelRefine(Data, CheckCandidateRD16); |
|
360 |
} |
} |
361 |
|
|
362 |
if (MotionFlags&XVID_ME_CHECKPREDICTION_RD) { /* let's check vector equal to prediction */ |
if (MotionFlags&XVID_ME_CHECKPREDICTION_RD) { /* let's check vector equal to prediction */ |
372 |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
373 |
const int x, const int y, |
const int x, const int y, |
374 |
const MBParam * const pParam, const uint32_t MotionFlags, |
const MBParam * const pParam, const uint32_t MotionFlags, |
375 |
const VECTOR * const backup) |
const VECTOR * const backup, const int bound) |
376 |
{ |
{ |
377 |
|
|
378 |
unsigned int cbp = 0, bits = 0, t = 0, i; |
unsigned int cbp = 0, t = 0, i; |
379 |
|
|
380 |
|
/* minimum number of bits INTER4V can take is 2 (cbpy) + 3 (mcbpc) + 4*2 (vectors)*/ |
381 |
|
int bits = (2+3+4*2)*BITS_MULT; |
382 |
SearchData Data2, *Data8 = &Data2; |
SearchData Data2, *Data8 = &Data2; |
383 |
int sumx = 0, sumy = 0; |
int sumx = 0, sumy = 0; |
384 |
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
397 |
Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
398 |
Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
399 |
*Data8->cbp = (Data->cbp[1] & (1<<(5-i))) ? 1:0; /* copy corresponding cbp bit */ |
*Data8->cbp = (Data->cbp[1] & (1<<(5-i))) ? 1:0; /* copy corresponding cbp bit */ |
400 |
|
Data8->lambda[0] = Data->lambda[i]; |
401 |
|
Data8->rel_var8[0] = Data->rel_var8[i]; |
402 |
|
|
403 |
if(Data->qpel) { |
if(Data->qpel) { |
404 |
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); |
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, bound, x, y, i); |
405 |
if (i != 0) t = d_mv_bits( Data8->currentQMV->x, Data8->currentQMV->y, |
if (i != 0) t = d_mv_bits( Data8->currentQMV->x, Data8->currentQMV->y, |
406 |
Data8->predMV, Data8->iFcode, 0, 0); |
Data8->predMV, Data8->iFcode, 0) - 2; |
407 |
} else { |
} else { |
408 |
Data8->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, i); |
Data8->predMV = get_pmv2(pMBs, pParam->mb_width, bound, x, y, i); |
409 |
if (i != 0) t = d_mv_bits( Data8->currentMV->x, Data8->currentMV->y, |
if (i != 0) t = d_mv_bits( Data8->currentMV->x, Data8->currentMV->y, |
410 |
Data8->predMV, Data8->iFcode, 0, 0); |
Data8->predMV, Data8->iFcode, 0) - 2; |
411 |
} |
} |
412 |
|
|
413 |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, |
414 |
pParam->width, pParam->height, Data8->iFcode, Data8->qpel+1, 0); |
pParam->width, pParam->height, Data8->iFcode, Data8->qpel+1); |
415 |
|
|
416 |
*Data8->iMinSAD += BITS_MULT*t; |
*Data8->iMinSAD += BITS_MULT*t; |
417 |
|
|
424 |
} |
} |
425 |
|
|
426 |
if (Data8->qpel) { |
if (Data8->qpel) { |
427 |
if (MotionFlags&XVID_ME_HALFPELREFINE8_RD || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_RD)) { /* halfpixel motion search follows */ |
int bsad = Data8->iMinSAD[0]; |
428 |
int32_t s = *Data8->iMinSAD; |
int bx = Data8->currentQMV->x; |
429 |
|
int by = Data8->currentQMV->y; |
430 |
|
|
431 |
Data8->currentMV->x = Data8->currentQMV->x/2; |
Data8->currentMV->x = Data8->currentQMV->x/2; |
432 |
Data8->currentMV->y = Data8->currentQMV->y/2; |
Data8->currentMV->y = Data8->currentQMV->y/2; |
433 |
|
|
434 |
|
if (MotionFlags&XVID_ME_HALFPELREFINE8_RD || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_RD)) { /* halfpixel motion search follows */ |
435 |
Data8->qpel_precision = 0; |
Data8->qpel_precision = 0; |
436 |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, |
437 |
pParam->width, pParam->height, Data8->iFcode - 1, 1, 0); |
pParam->width, pParam->height, Data8->iFcode - 1, 1); |
438 |
|
|
439 |
if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1) |
if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1) |
440 |
CheckCandidateRD8(Data8->currentMV->x, Data8->currentMV->y, Data8, 255); |
CheckCandidateRD8(Data8->currentMV->x, Data8->currentMV->y, Data8, 255); |
443 |
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
444 |
|
|
445 |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
446 |
xvid_me_SubpelRefine(Data8, CheckCandidateRD8); |
xvid_me_SubpelRefine(Data->currentMV[0], Data8, CheckCandidateRD8, 0); |
447 |
|
|
448 |
if(s > *Data8->iMinSAD) { /* we have found a better match */ |
if(bsad > *Data8->iMinSAD) { /* we have found a better match */ |
449 |
Data8->currentQMV->x = 2*Data8->currentMV->x; |
bx = Data8->currentQMV->x = 2*Data8->currentMV->x; |
450 |
Data8->currentQMV->y = 2*Data8->currentMV->y; |
by = Data8->currentQMV->y = 2*Data8->currentMV->y; |
451 |
|
bsad = Data8->iMinSAD[0]; |
452 |
} |
} |
453 |
|
|
454 |
Data8->qpel_precision = 1; |
Data8->qpel_precision = 1; |
455 |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, |
456 |
pParam->width, pParam->height, Data8->iFcode, 2, 0); |
pParam->width, pParam->height, Data8->iFcode, 2); |
457 |
|
|
458 |
|
} |
459 |
|
|
460 |
|
if (MotionFlags & XVID_ME_QUARTERPELREFINE8_RD) { |
461 |
|
if (MotionFlags & XVID_ME_FASTREFINE8) |
462 |
|
FullRefine_Fast(Data8, CheckCandidateRD8, 0); |
463 |
|
else xvid_me_SubpelRefine(Data->currentQMV[0], Data8, CheckCandidateRD8, 0); |
464 |
|
} |
465 |
|
|
466 |
|
if (bsad <= Data->iMinSAD[0]) { |
467 |
|
/* we have not found a better match */ |
468 |
|
Data8->iMinSAD[0] = bsad; |
469 |
|
Data8->currentQMV->x = bx; |
470 |
|
Data8->currentQMV->y = by; |
471 |
} |
} |
|
if (MotionFlags & XVID_ME_QUARTERPELREFINE8_RD) |
|
|
xvid_me_SubpelRefine(Data8, CheckCandidateRD8); |
|
472 |
|
|
473 |
} else { /* not qpel */ |
} else { /* not qpel */ |
474 |
|
|
476 |
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
477 |
|
|
478 |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
479 |
xvid_me_SubpelRefine(Data8, CheckCandidateRD8); /* halfpel refinement */ |
xvid_me_SubpelRefine(Data->currentMV[0], Data8, CheckCandidateRD8, 0); /* halfpel refinement */ |
480 |
} |
} |
481 |
|
|
482 |
/* checking vector equal to predicion */ |
/* checking vector equal to predicion */ |
508 |
|
|
509 |
} /* end - for all luma blocks */ |
} /* end - for all luma blocks */ |
510 |
|
|
511 |
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
bits += BITS_MULT * (xvid_cbpy_tab[15-(cbp>>2)].len - 2); /* 2 were added before */ |
512 |
|
|
513 |
/* let's check chroma */ |
/* let's check chroma */ |
514 |
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
517 |
/* chroma U */ |
/* chroma U */ |
518 |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
519 |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
520 |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, Data->scan_table, Data->mpeg_quant_matrices); |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, |
521 |
|
Data->scan_table, Data->lambda[4], Data->mpeg_quant_matrices, Data->quant_sq, Data->rel_var8[4], Data->metric); |
522 |
|
|
523 |
if (bits >= *Data->iMinSAD) return bits; |
if (bits >= *Data->iMinSAD) return bits; |
524 |
|
|
525 |
/* chroma V */ |
/* chroma V */ |
526 |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
527 |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
528 |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, Data->scan_table, Data->mpeg_quant_matrices); |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, |
529 |
|
Data->scan_table, Data->lambda[5], Data->mpeg_quant_matrices, Data->quant_sq, Data->rel_var8[5], Data->metric); |
530 |
|
|
531 |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
bits += BITS_MULT*(mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len - 3); /* 3 were added before */ |
532 |
|
|
533 |
*Data->cbp = cbp; |
*Data->cbp = cbp; |
534 |
return bits; |
return bits; |
536 |
|
|
537 |
static int |
static int |
538 |
findRD_intra(SearchData * const Data, MACROBLOCK * pMB, |
findRD_intra(SearchData * const Data, MACROBLOCK * pMB, |
539 |
const int x, const int y, const int mb_width) |
const int x, const int y, const int mb_width, const int bound) |
540 |
{ |
{ |
541 |
unsigned int cbp[2] = {0, 0}, bits[2], i; |
unsigned int cbp[2] = {0, 0}, bits[2], i; |
542 |
unsigned int bits1 = BITS_MULT*1, bits2 = BITS_MULT*1; /* this one is ac/dc prediction flag bit */ |
/* minimum number of bits that WILL be coded in intra - mcbpc 5, cby 2 acdc flag - 1 and DC coeffs - 4*3+2*2 */ |
543 |
|
int bits1 = BITS_MULT*(5+2+1+4*3+2*2), bits2 = BITS_MULT*(5+2+1+4*3+2*2); |
544 |
unsigned int distortion = 0; |
unsigned int distortion = 0; |
545 |
|
|
546 |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64, * dqcoeff = Data->dctSpace + 128; |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64, * dqcoeff = Data->dctSpace + 128; |
553 |
|
|
554 |
|
|
555 |
distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, i, in, coeff, dqcoeff, |
distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, i, in, coeff, dqcoeff, |
556 |
predictors[i], iQuant, Data->quant_type, bits, cbp, Data->mpeg_quant_matrices); |
predictors[i], iQuant, Data->quant_type, bits, cbp, |
557 |
|
Data->lambda[i], Data->mpeg_quant_matrices, Data->quant_sq, |
558 |
|
Data->metric, bound); |
559 |
bits1 += distortion + BITS_MULT * bits[0]; |
bits1 += distortion + BITS_MULT * bits[0]; |
560 |
bits2 += distortion + BITS_MULT * bits[1]; |
bits2 += distortion + BITS_MULT * bits[1]; |
561 |
|
|
563 |
return bits1; |
return bits1; |
564 |
} |
} |
565 |
|
|
566 |
bits1 += BITS_MULT*xvid_cbpy_tab[cbp[0]>>2].len; |
bits1 += BITS_MULT * (xvid_cbpy_tab[cbp[0]>>2].len - 2); /* two bits were added before */ |
567 |
bits2 += BITS_MULT*xvid_cbpy_tab[cbp[1]>>2].len; |
bits2 += BITS_MULT * (xvid_cbpy_tab[cbp[1]>>2].len - 2); |
568 |
|
|
569 |
/*chroma U */ |
/*chroma U */ |
570 |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
571 |
distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, 4, in, coeff, dqcoeff, |
distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, 4, in, coeff, dqcoeff, |
572 |
predictors[4], iQuant, Data->quant_type, bits, cbp, Data->mpeg_quant_matrices); |
predictors[4], iQuant, Data->quant_type, bits, cbp, |
573 |
|
Data->lambda[4], Data->mpeg_quant_matrices, Data->quant_sq, |
574 |
|
Data->metric, bound); |
575 |
bits1 += distortion + BITS_MULT * bits[0]; |
bits1 += distortion + BITS_MULT * bits[0]; |
576 |
bits2 += distortion + BITS_MULT * bits[1]; |
bits2 += distortion + BITS_MULT * bits[1]; |
577 |
|
|
581 |
/* chroma V */ |
/* chroma V */ |
582 |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
583 |
distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, 5, in, coeff, dqcoeff, |
distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, 5, in, coeff, dqcoeff, |
584 |
predictors[5], iQuant, Data->quant_type, bits, cbp, Data->mpeg_quant_matrices); |
predictors[5], iQuant, Data->quant_type, bits, cbp, |
585 |
|
Data->lambda[5], Data->mpeg_quant_matrices, Data->quant_sq, |
586 |
|
Data->metric, bound); |
587 |
|
|
588 |
bits1 += distortion + BITS_MULT * bits[0]; |
bits1 += distortion + BITS_MULT * bits[0]; |
589 |
bits2 += distortion + BITS_MULT * bits[1]; |
bits2 += distortion + BITS_MULT * bits[1]; |
590 |
|
|
591 |
bits1 += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp[0] & 3) << 3)].len; |
bits1 += BITS_MULT * (mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp[0] & 3) << 3)].len - 5); /* 5 bits were added before */ |
592 |
bits2 += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp[1] & 3) << 3)].len; |
bits2 += BITS_MULT * (mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp[1] & 3) << 3)].len - 5); |
593 |
|
|
594 |
*Data->cbp = bits1 <= bits2 ? cbp[0] : cbp[1]; |
*Data->cbp = bits1 <= bits2 ? cbp[0] : cbp[1]; |
595 |
|
|
600 |
static int |
static int |
601 |
findRD_gmc(SearchData * const Data, const IMAGE * const vGMC, const int x, const int y) |
findRD_gmc(SearchData * const Data, const IMAGE * const vGMC, const int x, const int y) |
602 |
{ |
{ |
603 |
int bits = BITS_MULT*1; /* this one is mcsel */ |
/* minimum nubler of bits - 1 (mcbpc) + 2 (cby) + 1 (mcsel) */ |
604 |
|
int bits = BITS_MULT * (1+2+1); |
605 |
unsigned int cbp = 0, i; |
unsigned int cbp = 0, i; |
606 |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
607 |
|
|
608 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
609 |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
610 |
transfer_8to16subro(in, Data->Cur + s, vGMC->y + s + 16*(x+y*Data->iEdgedWidth), Data->iEdgedWidth); |
transfer_8to16subro(in, Data->Cur + s, vGMC->y + s + 16*(x+y*Data->iEdgedWidth), Data->iEdgedWidth); |
611 |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i, Data->scan_table, Data->mpeg_quant_matrices); |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i, |
612 |
|
Data->scan_table, Data->lambda[i], Data->mpeg_quant_matrices, |
613 |
|
Data->quant_sq, Data->rel_var8[i], Data->metric); |
614 |
if (bits >= Data->iMinSAD[0]) return bits; |
if (bits >= Data->iMinSAD[0]) return bits; |
615 |
} |
} |
616 |
|
|
617 |
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
bits += BITS_MULT * (xvid_cbpy_tab[15-(cbp>>2)].len - 2); |
618 |
|
|
619 |
/*chroma U */ |
/*chroma U */ |
620 |
transfer_8to16subro(in, Data->CurU, vGMC->u + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurU, vGMC->u + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2); |
621 |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, Data->scan_table, Data->mpeg_quant_matrices); |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, |
622 |
|
Data->scan_table, Data->lambda[4], Data->mpeg_quant_matrices, |
623 |
|
Data->quant_sq, Data->rel_var8[4], Data->metric); |
624 |
|
|
625 |
if (bits >= Data->iMinSAD[0]) return bits; |
if (bits >= Data->iMinSAD[0]) return bits; |
626 |
|
|
627 |
/* chroma V */ |
/* chroma V */ |
628 |
transfer_8to16subro(in, Data->CurV , vGMC->v + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurV , vGMC->v + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2); |
629 |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, Data->scan_table, Data->mpeg_quant_matrices); |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, |
630 |
|
Data->scan_table, Data->lambda[5], Data->mpeg_quant_matrices, |
631 |
|
Data->quant_sq, Data->rel_var8[5], Data->metric); |
632 |
|
|
633 |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
bits += BITS_MULT * (mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len - 1); |
634 |
|
|
635 |
*Data->cbp = cbp; |
*Data->cbp = cbp; |
636 |
|
|
649 |
const IMAGE * const pCurrent, |
const IMAGE * const pCurrent, |
650 |
const IMAGE * const pRef, |
const IMAGE * const pRef, |
651 |
const IMAGE * const vGMC, |
const IMAGE * const vGMC, |
652 |
const int coding_type) |
const int coding_type, |
653 |
|
const int bound) |
654 |
{ |
{ |
655 |
int mode = MODE_INTER; |
int mode = MODE_INTER; |
656 |
int mcsel = 0; |
int mcsel = 0; |
657 |
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
658 |
const uint32_t iQuant = pMB->quant; |
const uint32_t iQuant = pMB->quant; |
|
|
|
659 |
int min_rd, intra_rd, i, cbp; |
int min_rd, intra_rd, i, cbp; |
660 |
VECTOR backup[5], *v; |
VECTOR backup[5], *v; |
661 |
Data->iQuant = iQuant; |
Data->iQuant = iQuant; |
662 |
|
Data->quant_sq = iQuant*iQuant; |
663 |
Data->scan_table = VopFlags & XVID_VOP_ALTERNATESCAN ? |
Data->scan_table = VopFlags & XVID_VOP_ALTERNATESCAN ? |
664 |
scan_tables[2] : scan_tables[0]; |
scan_tables[2] : scan_tables[0]; |
665 |
|
Data->metric = !!(VopFlags & XVID_VOP_RD_PSNRHVSM); |
666 |
|
|
667 |
pMB->mcsel = 0; |
pMB->mcsel = 0; |
668 |
|
|
672 |
backup[i] = v[i]; |
backup[i] = v[i]; |
673 |
} |
} |
674 |
|
|
675 |
|
for (i = 0; i < 6; i++) { |
676 |
|
Data->lambda[i] = (LAMBDA*pMB->lambda[i])>>LAMBDA_EXP; |
677 |
|
Data->rel_var8[i] = pMB->rel_var8[i]; |
678 |
|
} |
679 |
|
|
680 |
min_rd = findRD_inter(Data, x, y, pParam, MotionFlags); |
min_rd = findRD_inter(Data, x, y, pParam, MotionFlags); |
681 |
cbp = *Data->cbp; |
cbp = *Data->cbp; |
682 |
|
|
694 |
|
|
695 |
if (inter4v) { |
if (inter4v) { |
696 |
int v4_rd; |
int v4_rd; |
697 |
v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup, bound); |
698 |
if (v4_rd < min_rd) { |
if (v4_rd < min_rd) { |
699 |
Data->iMinSAD[0] = min_rd = v4_rd; |
Data->iMinSAD[0] = min_rd = v4_rd; |
700 |
mode = MODE_INTER4V; |
mode = MODE_INTER4V; |
702 |
} |
} |
703 |
} |
} |
704 |
|
|
705 |
intra_rd = findRD_intra(Data, pMB, x, y, pParam->mb_width); |
/* there is no way for INTRA to take less than 24 bits - go to findRD_intra() for calculations */ |
706 |
|
if (min_rd > 24*BITS_MULT) { |
707 |
|
intra_rd = findRD_intra(Data, pMB, x, y, pParam->mb_width, bound); |
708 |
if (intra_rd < min_rd) { |
if (intra_rd < min_rd) { |
709 |
*Data->iMinSAD = min_rd = intra_rd; |
*Data->iMinSAD = min_rd = intra_rd; |
710 |
mode = MODE_INTRA; |
mode = MODE_INTRA; |
711 |
cbp = *Data->cbp; |
cbp = *Data->cbp; |
712 |
} |
} |
713 |
|
} |
714 |
|
|
715 |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
716 |
pMB->cbp = cbp; |
pMB->cbp = cbp; |
717 |
|
|
|
|
|
|
if (Data->rrv) { |
|
|
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
|
|
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
|
|
} |
|
|
|
|
718 |
if (mode == MODE_INTER && mcsel == 0) { |
if (mode == MODE_INTER && mcsel == 0) { |
719 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
720 |
|
|
758 |
const IMAGE * const pCurrent, |
const IMAGE * const pCurrent, |
759 |
const IMAGE * const pRef, |
const IMAGE * const pRef, |
760 |
const IMAGE * const vGMC, |
const IMAGE * const vGMC, |
761 |
const int coding_type) |
const int coding_type, |
762 |
|
const int bound) |
763 |
{ |
{ |
764 |
int mode = MODE_INTER; |
int mode = MODE_INTER; |
765 |
int mcsel = 0; |
int mcsel = 0; |
775 |
int top = 0, top_right = 0, left = 0; |
int top = 0, top_right = 0, left = 0; |
776 |
Data->scan_table = VopFlags & XVID_VOP_ALTERNATESCAN ? |
Data->scan_table = VopFlags & XVID_VOP_ALTERNATESCAN ? |
777 |
scan_tables[2] : scan_tables[0]; |
scan_tables[2] : scan_tables[0]; |
778 |
|
Data->metric = !!(VopFlags & XVID_VOP_RD_PSNRHVSM); |
779 |
|
|
780 |
pMB->mcsel = 0; |
pMB->mcsel = 0; |
781 |
|
Data->iQuant = iQuant; |
782 |
|
Data->quant_sq = iQuant*iQuant; |
783 |
|
|
784 |
|
for (i = 0; i < 6; i++) { |
785 |
|
Data->lambda[i] = (LAMBDA*pMB->lambda[i])>>LAMBDA_EXP; |
786 |
|
Data->rel_var8[i] = pMB->rel_var8[i]; |
787 |
|
} |
788 |
|
|
789 |
/* INTER <-> INTER4V decision */ |
/* INTER <-> INTER4V decision */ |
790 |
if ((Data->iMinSAD[0] + 75 < Data->iMinSAD[1] + |
if ((Data->iMinSAD[0] + 75 < Data->iMinSAD[1] + |
803 |
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
804 |
if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP)) |
if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP)) |
805 |
if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH) |
if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH) |
806 |
if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) { |
if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant)) { |
807 |
mode = MODE_NOT_CODED; |
mode = MODE_NOT_CODED; |
808 |
sad = 0; /* Compiler warning */ |
sad = 0; /* Compiler warning */ |
809 |
goto early_out; |
goto early_out; |
834 |
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
835 |
if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP)) |
if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP)) |
836 |
if ( (100*Data->iMinSAD[0])/(pMB->sad16+1) > FINAL_SKIP_THRESH) |
if ( (100*Data->iMinSAD[0])/(pMB->sad16+1) > FINAL_SKIP_THRESH) |
837 |
if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) { |
if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant)) { |
838 |
mode = MODE_NOT_CODED; |
mode = MODE_NOT_CODED; |
839 |
sad = 0; /* Compiler warning */ |
sad = 0; /* Compiler warning */ |
840 |
goto early_out; |
goto early_out; |
871 |
|
|
872 |
if (inter4v) { |
if (inter4v) { |
873 |
int v4_rd; |
int v4_rd; |
874 |
v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup, bound); |
875 |
if (v4_rd < min_rd) { |
if (v4_rd < min_rd) { |
876 |
Data->iMinSAD[0] = min_rd = v4_rd; |
Data->iMinSAD[0] = min_rd = v4_rd; |
877 |
mode = MODE_INTER4V; |
mode = MODE_INTER4V; |
910 |
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
911 |
|
|
912 |
if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */ |
if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */ |
|
if (Data->rrv) InterBias *= 4; |
|
913 |
|
|
914 |
if (InterBias < sad) { |
if (InterBias < sad) { |
915 |
int32_t deviation; |
int32_t deviation = dev16(Data->Cur, Data->iEdgedWidth); |
|
if (!Data->rrv) |
|
|
deviation = dev16(Data->Cur, Data->iEdgedWidth); |
|
|
else |
|
|
deviation = dev16(Data->Cur, Data->iEdgedWidth) + /* dev32() */ |
|
|
dev16(Data->Cur+16, Data->iEdgedWidth) + |
|
|
dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) + |
|
|
dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth); |
|
|
|
|
916 |
if (deviation < (sad - InterBias)) mode = MODE_INTRA; |
if (deviation < (sad - InterBias)) mode = MODE_INTRA; |
917 |
} |
} |
918 |
|
|
947 |
|
|
948 |
if(mode == MODE_INTER4V) { |
if(mode == MODE_INTER4V) { |
949 |
int v4_rd; |
int v4_rd; |
950 |
v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup, bound); |
951 |
if (v4_rd < min_rd) { |
if (v4_rd < min_rd) { |
952 |
Data->iMinSAD[0] = min_rd = v4_rd; |
Data->iMinSAD[0] = min_rd = v4_rd; |
953 |
mode = MODE_INTER4V; |
mode = MODE_INTER4V; |
956 |
} |
} |
957 |
} |
} |
958 |
|
|
959 |
intra_rd = findRD_intra(Data, pMB, x, y, pParam->mb_width); |
intra_rd = findRD_intra(Data, pMB, x, y, pParam->mb_width, bound); |
960 |
if (intra_rd < min_rd) { |
if (intra_rd < min_rd) { |
961 |
*Data->iMinSAD = min_rd = intra_rd; |
*Data->iMinSAD = min_rd = intra_rd; |
962 |
mode = MODE_INTRA; |
mode = MODE_INTRA; |
968 |
early_out: |
early_out: |
969 |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; |
970 |
|
|
|
if (Data->rrv) { |
|
|
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
|
|
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
|
|
} |
|
|
|
|
971 |
if (mode == MODE_INTER && mcsel == 0) { |
if (mode == MODE_INTER && mcsel == 0) { |
972 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
973 |
|
|