40 |
#include "motion.h" |
#include "motion.h" |
41 |
#include "sad.h" |
#include "sad.h" |
42 |
#include "../bitstream/zigzag.h" |
#include "../bitstream/zigzag.h" |
43 |
#include "../quant/quant_mpeg4.h" |
#include "../quant/quant.h" |
|
#include "../quant/quant_h263.h" |
|
44 |
#include "../bitstream/vlc_codes.h" |
#include "../bitstream/vlc_codes.h" |
45 |
#include "../dct/fdct.h" |
#include "../dct/fdct.h" |
46 |
#include "motion_inlines.h" |
#include "motion_inlines.h" |
54 |
int16_t * const dqcoeff, |
int16_t * const dqcoeff, |
55 |
const uint32_t quant, const int quant_type, |
const uint32_t quant, const int quant_type, |
56 |
uint32_t * cbp, |
uint32_t * cbp, |
57 |
const int block) |
const int block, |
58 |
|
const uint16_t * scan_table, |
59 |
|
const uint16_t * mpeg_quant_matrices) |
60 |
{ |
{ |
61 |
int sum; |
int sum; |
62 |
int bits; |
int bits; |
63 |
int distortion = 0; |
int distortion = 0; |
|
int i; |
|
64 |
|
|
65 |
fdct(data); |
fdct(data); |
66 |
|
|
67 |
if (quant_type) sum = quant_inter(coeff, data, quant); |
if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices); |
68 |
else sum = quant4_inter(coeff, data, quant); |
else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); |
69 |
|
|
70 |
if (sum > 0) { |
if (sum > 0) { |
71 |
*cbp |= 1 << (5 - block); |
*cbp |= 1 << (5 - block); |
72 |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, data->scan_table); |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); |
|
|
|
|
if (quant_type) dequant_inter(dqcoeff, coeff, quant); |
|
|
else dequant4_inter(dqcoeff, coeff, quant); |
|
73 |
|
|
74 |
for (i = 0; i < 64; i++) |
if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
75 |
distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]); |
else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
76 |
|
|
77 |
|
distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t)); |
78 |
} else { |
} else { |
79 |
|
const static int16_t zero_block[64] = |
80 |
|
{ |
81 |
|
0, 0, 0, 0, 0, 0, 0, 0, |
82 |
|
0, 0, 0, 0, 0, 0, 0, 0, |
83 |
|
0, 0, 0, 0, 0, 0, 0, 0, |
84 |
|
0, 0, 0, 0, 0, 0, 0, 0, |
85 |
|
0, 0, 0, 0, 0, 0, 0, 0, |
86 |
|
0, 0, 0, 0, 0, 0, 0, 0, |
87 |
|
0, 0, 0, 0, 0, 0, 0, 0, |
88 |
|
0, 0, 0, 0, 0, 0, 0, 0, |
89 |
|
}; |
90 |
bits = 0; |
bits = 0; |
91 |
for (i = 0; i < 64; i++) |
distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); |
|
distortion += data[i]*data[i]; |
|
92 |
} |
} |
93 |
|
|
94 |
|
|
95 |
return bits + (LAMBDA*distortion)/(quant*quant); |
return bits + (LAMBDA*distortion)/(quant*quant); |
96 |
} |
} |
97 |
|
|
98 |
static __inline unsigned int |
static __inline unsigned int |
99 |
Block_CalcBitsIntra(int16_t * const coeff, |
Block_CalcBitsIntra(MACROBLOCK * pMB, |
100 |
int16_t * const data, |
const unsigned int x, |
101 |
int16_t * const dqcoeff, |
const unsigned int y, |
102 |
const uint32_t quant, const int quant_type, |
const unsigned int mb_width, |
103 |
uint32_t * cbp, |
const uint32_t block, |
104 |
const int block, |
int16_t coeff[64], |
105 |
int * dcpred) |
int16_t qcoeff[64], |
106 |
{ |
int16_t dqcoeff[64], |
107 |
int bits, i; |
int16_t predictors[8], |
108 |
int distortion = 0; |
const uint32_t quant, |
109 |
uint32_t iDcScaler = get_dc_scaler(quant, block < 4); |
const int quant_type, |
110 |
int b_dc; |
unsigned int bits[2], |
111 |
|
unsigned int cbp[2], |
112 |
|
const uint16_t * mpeg_quant_matrices) |
113 |
|
{ |
114 |
|
int direction; |
115 |
|
int16_t *pCurrent; |
116 |
|
unsigned int i, coded; |
117 |
|
unsigned int distortion = 0; |
118 |
|
const uint32_t iDcScaler = get_dc_scaler(quant, block < 4); |
119 |
|
|
120 |
|
fdct(coeff); |
121 |
|
|
122 |
|
if (quant_type) { |
123 |
|
quant_h263_intra(qcoeff, coeff, quant, iDcScaler, mpeg_quant_matrices); |
124 |
|
dequant_h263_intra(dqcoeff, qcoeff, quant, iDcScaler, mpeg_quant_matrices); |
125 |
|
} else { |
126 |
|
quant_mpeg_intra(qcoeff, coeff, quant, iDcScaler, mpeg_quant_matrices); |
127 |
|
dequant_mpeg_intra(dqcoeff, qcoeff, quant, iDcScaler, mpeg_quant_matrices); |
128 |
|
} |
129 |
|
|
130 |
fdct(data); |
predict_acdc(pMB-(x+mb_width*y), x, y, mb_width, block, qcoeff, |
131 |
data[0] -= 1024; |
quant, iDcScaler, predictors, 0); |
132 |
|
|
133 |
if (quant_type) quant_intra(coeff, data, quant, iDcScaler); |
direction = pMB->acpred_directions[block]; |
134 |
else quant4_intra(coeff, data, quant, iDcScaler); |
pCurrent = pMB->pred_values[block]; |
135 |
|
|
136 |
b_dc = coeff[0]; |
/* store current coeffs to pred_values[] for future prediction */ |
137 |
if (block < 4) { |
pCurrent[0] = qcoeff[0] * iDcScaler; |
138 |
coeff[0] -= *dcpred; |
pCurrent[0] = CLIP(pCurrent[0], -2048, 2047); |
139 |
*dcpred = b_dc; |
for (i = 1; i < 8; i++) { |
140 |
|
pCurrent[i] = qcoeff[i]; |
141 |
|
pCurrent[i + 7] = qcoeff[i * 8]; |
142 |
} |
} |
143 |
|
|
144 |
bits = BITS_MULT*CodeCoeffIntra_CalcBits(coeff, data->scan_table); |
/* dc prediction */ |
145 |
if (bits != 0) *cbp |= 1 << (5 - block); |
qcoeff[0] = qcoeff[0] - predictors[0]; |
146 |
|
|
147 |
if (block < 4) bits += BITS_MULT*dcy_tab[coeff[0] + 255].len; |
if (block < 4) bits[1] = bits[0] = dcy_tab[qcoeff[0] + 255].len; |
148 |
else bits += BITS_MULT*dcc_tab[coeff[0] + 255].len; |
else bits[1] = bits[0] = dcc_tab[qcoeff[0] + 255].len; |
149 |
|
|
150 |
coeff[0] = b_dc; |
/* calc cost before ac prediction */ |
151 |
if (quant_type) dequant_intra(dqcoeff, coeff, quant, iDcScaler); |
bits[0] += coded = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]); |
152 |
else dequant4_intra(dqcoeff, coeff, quant, iDcScaler); |
if (coded > 0) cbp[0] |= 1 << (5 - block); |
153 |
|
|
154 |
for (i = 0; i < 64; i++) |
/* apply ac prediction & calc cost*/ |
155 |
distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]); |
if (direction == 1) { |
156 |
|
for (i = 1; i < 8; i++) { |
157 |
|
qcoeff[i] -= predictors[i]; |
158 |
|
predictors[i] = qcoeff[i]; |
159 |
|
} |
160 |
|
} else { /* acpred_direction == 2 */ |
161 |
|
for (i = 1; i < 8; i++) { |
162 |
|
qcoeff[i*8] -= predictors[i]; |
163 |
|
predictors[i] = qcoeff[i*8]; |
164 |
|
} |
165 |
|
} |
166 |
|
|
167 |
return bits + (LAMBDA*distortion)/(quant*quant); |
bits[1] += coded = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]); |
168 |
|
if (coded > 0) cbp[1] |= 1 << (5 - block); |
169 |
|
|
170 |
|
distortion = sse8_16bit(coeff, dqcoeff, 8*sizeof(int16_t)); |
171 |
|
|
172 |
|
return (LAMBDA*distortion)/(quant*quant); |
173 |
} |
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
static void |
static void |
178 |
CheckCandidateRD16(const int x, const int y, const SearchData * const data, const unsigned int Direction) |
CheckCandidateRD16(const int x, const int y, SearchData * const data, const unsigned int Direction) |
179 |
{ |
{ |
180 |
|
|
181 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
182 |
int32_t rd = 0; |
int32_t rd = 0; |
183 |
VECTOR * current; |
VECTOR * current; |
184 |
const uint8_t * ptr; |
const uint8_t * ptr; |
185 |
int i, cbp = 0, t, xc, yc; |
int i, t, xc, yc; |
186 |
|
unsigned cbp = 0; |
187 |
|
|
188 |
if ( (x > data->max_dx) || (x < data->min_dx) |
if ( (x > data->max_dx) || (x < data->min_dx) |
189 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
201 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
202 |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
203 |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
204 |
rd += data->temp[i] = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i); |
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); |
205 |
} |
} |
206 |
|
|
207 |
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, 0); |
226 |
/* chroma U */ |
/* chroma U */ |
227 |
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); |
228 |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
229 |
rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4); |
rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4, data->scan_table, data->mpeg_quant_matrices); |
230 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
231 |
|
|
232 |
/* chroma V */ |
/* chroma V */ |
233 |
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); |
234 |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
235 |
rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5); |
rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5, data->scan_table, data->mpeg_quant_matrices); |
236 |
|
|
237 |
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; |
238 |
|
|
239 |
if (rd < data->iMinSAD[0]) { |
if (rd < data->iMinSAD[0]) { |
240 |
data->iMinSAD[0] = rd; |
data->iMinSAD[0] = rd; |
241 |
current[0].x = x; current[0].y = y; |
current[0].x = x; current[0].y = y; |
242 |
*data->dir = Direction; |
data->dir = Direction; |
243 |
*data->cbp = cbp; |
*data->cbp = cbp; |
244 |
} |
} |
245 |
} |
} |
246 |
|
|
247 |
static void |
static void |
248 |
CheckCandidateRD8(const int x, const int y, const SearchData * const data, const unsigned int Direction) |
CheckCandidateRD8(const int x, const int y, SearchData * const data, const unsigned int Direction) |
249 |
{ |
{ |
250 |
|
|
251 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
252 |
int32_t rd; |
int32_t rd; |
253 |
VECTOR * current; |
VECTOR * current; |
254 |
const uint8_t * ptr; |
const uint8_t * ptr; |
255 |
int cbp = 0; |
unsigned int cbp = 0; |
256 |
|
|
257 |
if ( (x > data->max_dx) || (x < data->min_dx) |
if ( (x > data->max_dx) || (x < data->min_dx) |
258 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
266 |
} |
} |
267 |
|
|
268 |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
269 |
rd = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5); |
rd = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5, data->scan_table, data->mpeg_quant_matrices); |
270 |
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, 0); |
271 |
|
|
272 |
if (rd < data->iMinSAD[0]) { |
if (rd < data->iMinSAD[0]) { |
273 |
*data->cbp = cbp; |
*data->cbp = cbp; |
274 |
data->iMinSAD[0] = rd; |
data->iMinSAD[0] = rd; |
275 |
current[0].x = x; current[0].y = y; |
current[0].x = x; current[0].y = y; |
276 |
*data->dir = Direction; |
data->dir = Direction; |
277 |
} |
} |
278 |
} |
} |
279 |
|
|
313 |
xvid_me_SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidateRD16); |
xvid_me_SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidateRD16); |
314 |
|
|
315 |
if (MotionFlags&XVID_ME_HALFPELREFINE16_RD) |
if (MotionFlags&XVID_ME_HALFPELREFINE16_RD) |
316 |
xvid_me_SubpelRefine(Data, CheckCandidateRD16); |
xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidateRD16, 0); |
317 |
|
|
318 |
if (Data->qpel) { |
if (Data->qpel) { |
319 |
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 */ |
327 |
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, |
328 |
pParam->width, pParam->height, Data->iFcode, 2, 0); |
pParam->width, pParam->height, Data->iFcode, 2, 0); |
329 |
} |
} |
330 |
if (MotionFlags&XVID_ME_QUARTERPELREFINE16_RD) |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16_RD) { |
331 |
xvid_me_SubpelRefine(Data, CheckCandidateRD16); |
if (MotionFlags & XVID_ME_FASTREFINE16) |
332 |
|
FullRefine_Fast(Data, CheckCandidateRD16, 0); |
333 |
|
else |
334 |
|
xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidateRD16, 0); |
335 |
|
} |
336 |
} |
} |
337 |
|
|
338 |
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 */ |
344 |
} |
} |
345 |
|
|
346 |
static int |
static int |
347 |
findRD_inter4v(const SearchData * const Data, |
findRD_inter4v(SearchData * const Data, |
348 |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
349 |
const int x, const int y, |
const int x, const int y, |
350 |
const MBParam * const pParam, const uint32_t MotionFlags, |
const MBParam * const pParam, const uint32_t MotionFlags, |
351 |
const VECTOR * const backup) |
const VECTOR * const backup) |
352 |
{ |
{ |
353 |
|
|
354 |
int cbp = 0, bits = 0, t = 0, i; |
unsigned int cbp = 0, bits = 0, t = 0, i; |
355 |
SearchData Data2, *Data8 = &Data2; |
SearchData Data2, *Data8 = &Data2; |
356 |
int sumx = 0, sumy = 0; |
int sumx = 0, sumy = 0; |
357 |
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
361 |
|
|
362 |
for (i = 0; i < 4; i++) { /* for all luma blocks */ |
for (i = 0; i < 4; i++) { /* for all luma blocks */ |
363 |
|
|
364 |
Data8->iMinSAD = Data->iMinSAD + i + 1; |
*Data8->iMinSAD = *(Data->iMinSAD + i + 1); |
365 |
Data8->currentMV = Data->currentMV + i + 1; |
*Data8->currentMV = *(Data->currentMV + i + 1); |
366 |
Data8->currentQMV = Data->currentQMV + i + 1; |
*Data8->currentQMV = *(Data->currentQMV + i + 1); |
367 |
Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
368 |
Data8->RefP[0] = Data->RefP[0] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[0] = Data->RefP[0] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
369 |
Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
395 |
} |
} |
396 |
|
|
397 |
if (Data8->qpel) { |
if (Data8->qpel) { |
398 |
if (MotionFlags&XVID_ME_HALFPELREFINE8_RD || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_RD)) { /* halfpixel motion search follows */ |
int bsad = Data8->iMinSAD[0]; |
399 |
int32_t s = *Data8->iMinSAD; |
int bx = Data8->currentQMV->x; |
400 |
|
int by = Data8->currentQMV->y; |
401 |
|
|
402 |
Data8->currentMV->x = Data8->currentQMV->x/2; |
Data8->currentMV->x = Data8->currentQMV->x/2; |
403 |
Data8->currentMV->y = Data8->currentQMV->y/2; |
Data8->currentMV->y = Data8->currentQMV->y/2; |
404 |
|
|
405 |
|
if (MotionFlags&XVID_ME_HALFPELREFINE8_RD || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_RD)) { /* halfpixel motion search follows */ |
406 |
Data8->qpel_precision = 0; |
Data8->qpel_precision = 0; |
407 |
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, |
408 |
pParam->width, pParam->height, Data8->iFcode - 1, 1, 0); |
pParam->width, pParam->height, Data8->iFcode - 1, 1, 0); |
414 |
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
415 |
|
|
416 |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
417 |
xvid_me_SubpelRefine(Data8, CheckCandidateRD8); |
xvid_me_SubpelRefine(Data->currentMV[0], Data8, CheckCandidateRD8, 0); |
418 |
|
|
419 |
if(s > *Data8->iMinSAD) { /* we have found a better match */ |
if(bsad > *Data8->iMinSAD) { /* we have found a better match */ |
420 |
Data8->currentQMV->x = 2*Data8->currentMV->x; |
bx = Data8->currentQMV->x = 2*Data8->currentMV->x; |
421 |
Data8->currentQMV->y = 2*Data8->currentMV->y; |
by = Data8->currentQMV->y = 2*Data8->currentMV->y; |
422 |
|
bsad = Data8->iMinSAD[0]; |
423 |
} |
} |
424 |
|
|
425 |
Data8->qpel_precision = 1; |
Data8->qpel_precision = 1; |
427 |
pParam->width, pParam->height, Data8->iFcode, 2, 0); |
pParam->width, pParam->height, Data8->iFcode, 2, 0); |
428 |
|
|
429 |
} |
} |
430 |
if (MotionFlags & XVID_ME_QUARTERPELREFINE8_RD) |
|
431 |
xvid_me_SubpelRefine(Data8, CheckCandidateRD8); |
if (MotionFlags & XVID_ME_QUARTERPELREFINE8_RD) { |
432 |
|
if (MotionFlags & XVID_ME_FASTREFINE8) |
433 |
|
FullRefine_Fast(Data8, CheckCandidateRD8, 0); |
434 |
|
else xvid_me_SubpelRefine(Data->currentQMV[0], Data8, CheckCandidateRD8, 0); |
435 |
|
} |
436 |
|
|
437 |
|
if (bsad <= Data->iMinSAD[0]) { |
438 |
|
/* we have not found a better match */ |
439 |
|
Data8->iMinSAD[0] = bsad; |
440 |
|
Data8->currentQMV->x = bx; |
441 |
|
Data8->currentQMV->y = by; |
442 |
|
} |
443 |
|
|
444 |
} else { /* not qpel */ |
} else { /* not qpel */ |
445 |
|
|
447 |
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
448 |
|
|
449 |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
450 |
xvid_me_SubpelRefine(Data8, CheckCandidateRD8); /* halfpel refinement */ |
xvid_me_SubpelRefine(Data->currentMV[0], Data8, CheckCandidateRD8, 0); /* halfpel refinement */ |
451 |
} |
} |
452 |
|
|
453 |
/* checking vector equal to predicion */ |
/* checking vector equal to predicion */ |
488 |
/* chroma U */ |
/* chroma U */ |
489 |
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); |
490 |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
491 |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4); |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, Data->scan_table, Data->mpeg_quant_matrices); |
492 |
|
|
493 |
if (bits >= *Data->iMinSAD) return bits; |
if (bits >= *Data->iMinSAD) return bits; |
494 |
|
|
495 |
/* chroma V */ |
/* chroma V */ |
496 |
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); |
497 |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
498 |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5); |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, Data->scan_table, Data->mpeg_quant_matrices); |
499 |
|
|
500 |
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; |
501 |
|
|
504 |
} |
} |
505 |
|
|
506 |
static int |
static int |
507 |
findRD_intra(const SearchData * const Data) |
findRD_intra(SearchData * const Data, MACROBLOCK * pMB, |
508 |
|
const int x, const int y, const int mb_width) |
509 |
{ |
{ |
510 |
int bits = BITS_MULT*1; /* this one is ac/dc prediction flag bit */ |
unsigned int cbp[2] = {0, 0}, bits[2], i; |
511 |
int cbp = 0, i, dc = 0; |
unsigned int bits1 = BITS_MULT*1, bits2 = BITS_MULT*1; /* this one is ac/dc prediction flag bit */ |
512 |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
unsigned int distortion = 0; |
513 |
|
|
514 |
|
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64, * dqcoeff = Data->dctSpace + 128; |
515 |
|
const uint32_t iQuant = Data->iQuant; |
516 |
|
int16_t predictors[6][8]; |
517 |
|
|
518 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
519 |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
520 |
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
|
bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i, &dc); |
|
521 |
|
|
522 |
if (bits >= Data->iMinSAD[0]) return bits; |
|
523 |
|
distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, i, in, coeff, dqcoeff, |
524 |
|
predictors[i], iQuant, Data->quant_type, bits, cbp, Data->mpeg_quant_matrices); |
525 |
|
bits1 += distortion + BITS_MULT * bits[0]; |
526 |
|
bits2 += distortion + BITS_MULT * bits[1]; |
527 |
|
|
528 |
|
if (bits1 >= Data->iMinSAD[0] && bits2 >= Data->iMinSAD[0]) |
529 |
|
return bits1; |
530 |
} |
} |
531 |
|
|
532 |
bits += BITS_MULT*xvid_cbpy_tab[cbp>>2].len; |
bits1 += BITS_MULT*xvid_cbpy_tab[cbp[0]>>2].len; |
533 |
|
bits2 += BITS_MULT*xvid_cbpy_tab[cbp[1]>>2].len; |
534 |
|
|
535 |
/*chroma U */ |
/*chroma U */ |
536 |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
537 |
bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, &dc); |
distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, 4, in, coeff, dqcoeff, |
538 |
|
predictors[4], iQuant, Data->quant_type, bits, cbp, Data->mpeg_quant_matrices); |
539 |
|
bits1 += distortion + BITS_MULT * bits[0]; |
540 |
|
bits2 += distortion + BITS_MULT * bits[1]; |
541 |
|
|
542 |
if (bits >= Data->iMinSAD[0]) return bits; |
if (bits1 >= Data->iMinSAD[0] && bits2 >= Data->iMinSAD[0]) |
543 |
|
return bits1; |
544 |
|
|
545 |
/* chroma V */ |
/* chroma V */ |
546 |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
547 |
bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, &dc); |
distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, 5, in, coeff, dqcoeff, |
548 |
|
predictors[5], iQuant, Data->quant_type, bits, cbp, Data->mpeg_quant_matrices); |
549 |
|
|
550 |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
bits1 += distortion + BITS_MULT * bits[0]; |
551 |
|
bits2 += distortion + BITS_MULT * bits[1]; |
552 |
|
|
553 |
return bits; |
bits1 += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp[0] & 3) << 3)].len; |
554 |
|
bits2 += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp[1] & 3) << 3)].len; |
555 |
|
|
556 |
|
*Data->cbp = bits1 <= bits2 ? cbp[0] : cbp[1]; |
557 |
|
|
558 |
|
return MIN(bits1, bits2); |
559 |
} |
} |
560 |
|
|
561 |
|
|
562 |
static int |
static int |
563 |
findRD_gmc(const 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) |
564 |
{ |
{ |
565 |
int bits = BITS_MULT*1; /* this one is mcsel */ |
int bits = BITS_MULT*1; /* this one is mcsel */ |
566 |
int cbp = 0, i; |
unsigned int cbp = 0, i; |
567 |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
568 |
|
|
569 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
570 |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
571 |
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); |
572 |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i); |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i, Data->scan_table, Data->mpeg_quant_matrices); |
573 |
if (bits >= Data->iMinSAD[0]) return bits; |
if (bits >= Data->iMinSAD[0]) return bits; |
574 |
} |
} |
575 |
|
|
577 |
|
|
578 |
/*chroma U */ |
/*chroma U */ |
579 |
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); |
580 |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4); |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, Data->scan_table, Data->mpeg_quant_matrices); |
581 |
|
|
582 |
if (bits >= Data->iMinSAD[0]) return bits; |
if (bits >= Data->iMinSAD[0]) return bits; |
583 |
|
|
584 |
/* chroma V */ |
/* chroma V */ |
585 |
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); |
586 |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5); |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, Data->scan_table, Data->mpeg_quant_matrices); |
587 |
|
|
588 |
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; |
589 |
|
|
611 |
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
612 |
const uint32_t iQuant = pMB->quant; |
const uint32_t iQuant = pMB->quant; |
613 |
|
|
614 |
int min_rd, intra_rd, i, cbp, c[2] = {0, 0}; |
int min_rd, intra_rd, i, cbp; |
615 |
VECTOR backup[5], *v; |
VECTOR backup[5], *v; |
616 |
Data->iQuant = iQuant; |
Data->iQuant = iQuant; |
|
Data->cbp = c; |
|
617 |
Data->scan_table = VopFlags & XVID_VOP_ALTERNATESCAN ? |
Data->scan_table = VopFlags & XVID_VOP_ALTERNATESCAN ? |
618 |
scan_tables[2] : scan_tables[0]; |
scan_tables[2] : scan_tables[0]; |
619 |
|
|
650 |
} |
} |
651 |
} |
} |
652 |
|
|
653 |
intra_rd = findRD_intra(Data); |
intra_rd = findRD_intra(Data, pMB, x, y, pParam->mb_width); |
654 |
if (intra_rd < min_rd) { |
if (intra_rd < min_rd) { |
655 |
*Data->iMinSAD = min_rd = intra_rd; |
*Data->iMinSAD = min_rd = intra_rd; |
656 |
mode = MODE_INTRA; |
mode = MODE_INTRA; |
657 |
|
cbp = *Data->cbp; |
658 |
} |
} |
659 |
|
|
660 |
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; |
717 |
const uint32_t iQuant = pMB->quant; |
const uint32_t iQuant = pMB->quant; |
718 |
const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0); |
const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0); |
719 |
int sad; |
int sad; |
720 |
int min_rd = -1, intra_rd, i, cbp = 63, c[2] = {0, 0}; |
int min_rd = -1, intra_rd, i, cbp = 63; |
721 |
VECTOR backup[5], *v; |
VECTOR backup[5], *v; |
722 |
int sad_backup[5]; |
int sad_backup[5]; |
723 |
int InterBias = MV16_INTER_BIAS; |
int InterBias = MV16_INTER_BIAS; |
771 |
} |
} |
772 |
} else { /* Rate-Distortion INTER<->INTER4V */ |
} else { /* Rate-Distortion INTER<->INTER4V */ |
773 |
Data->iQuant = iQuant; |
Data->iQuant = iQuant; |
|
Data->cbp = c; |
|
774 |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
775 |
|
|
776 |
/* 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?" */ |
871 |
} else { /* Rate-Distortion INTRA<->INTER */ |
} else { /* Rate-Distortion INTRA<->INTER */ |
872 |
if(min_rd < 0) { |
if(min_rd < 0) { |
873 |
Data->iQuant = iQuant; |
Data->iQuant = iQuant; |
|
Data->cbp = c; |
|
874 |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
875 |
|
|
876 |
for (i = 0; i < 5; i++) { |
for (i = 0; i < 5; i++) { |
907 |
} |
} |
908 |
} |
} |
909 |
|
|
910 |
intra_rd = findRD_intra(Data); |
intra_rd = findRD_intra(Data, pMB, x, y, pParam->mb_width); |
911 |
if (intra_rd < min_rd) { |
if (intra_rd < min_rd) { |
912 |
*Data->iMinSAD = min_rd = intra_rd; |
*Data->iMinSAD = min_rd = intra_rd; |
913 |
mode = MODE_INTRA; |
mode = MODE_INTRA; |