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 unsigned int lambda, |
60 |
|
const uint16_t * mpeg_quant_matrices) |
61 |
{ |
{ |
62 |
int sum; |
int sum; |
63 |
int bits; |
int bits; |
64 |
int distortion = 0; |
int distortion = 0; |
|
int i; |
|
65 |
|
|
66 |
fdct(data); |
fdct(data); |
67 |
|
|
68 |
if (quant_type) sum = quant_inter(coeff, data, quant); |
if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices); |
69 |
else sum = quant4_inter(coeff, data, quant); |
else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); |
70 |
|
|
71 |
if (sum > 0) { |
if (sum > 0) { |
72 |
*cbp |= 1 << (5 - block); |
*cbp |= 1 << (5 - block); |
73 |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); |
|
|
|
|
if (quant_type) dequant_inter(dqcoeff, coeff, quant); |
|
|
else dequant4_inter(dqcoeff, coeff, quant); |
|
74 |
|
|
75 |
for (i = 0; i < 64; i++) |
if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
76 |
distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]); |
else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
77 |
|
|
78 |
|
distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t)); |
79 |
} else { |
} else { |
80 |
|
const static int16_t zero_block[64] = |
81 |
|
{ |
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 |
|
0, 0, 0, 0, 0, 0, 0, 0, |
90 |
|
}; |
91 |
bits = 0; |
bits = 0; |
92 |
for (i = 0; i < 64; i++) |
distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); |
|
distortion += data[i]*data[i]; |
|
93 |
} |
} |
94 |
|
|
95 |
return bits + (LAMBDA*distortion)/(quant*quant); |
|
96 |
|
return bits + (lambda*distortion)/(quant*quant); |
97 |
} |
} |
98 |
|
|
99 |
static __inline unsigned int |
static __inline unsigned int |
100 |
Block_CalcBitsIntra(int16_t * const coeff, |
Block_CalcBitsIntra(MACROBLOCK * pMB, |
101 |
int16_t * const data, |
const unsigned int x, |
102 |
int16_t * const dqcoeff, |
const unsigned int y, |
103 |
const uint32_t quant, const int quant_type, |
const unsigned int mb_width, |
104 |
uint32_t * cbp, |
const uint32_t block, |
105 |
const int block, |
int16_t coeff[64], |
106 |
int * dcpred) |
int16_t qcoeff[64], |
107 |
{ |
int16_t dqcoeff[64], |
108 |
int bits, i; |
int16_t predictors[8], |
109 |
int distortion = 0; |
const uint32_t quant, |
110 |
uint32_t iDcScaler = get_dc_scaler(quant, block < 4); |
const int quant_type, |
111 |
int b_dc; |
unsigned int bits[2], |
112 |
|
unsigned int cbp[2], |
113 |
|
unsigned int lambda, |
114 |
|
const uint16_t * mpeg_quant_matrices) |
115 |
|
{ |
116 |
|
int direction; |
117 |
|
int16_t *pCurrent; |
118 |
|
unsigned int i, coded; |
119 |
|
unsigned int distortion = 0; |
120 |
|
const uint32_t iDcScaler = get_dc_scaler(quant, block < 4); |
121 |
|
|
122 |
|
fdct(coeff); |
123 |
|
|
124 |
|
if (quant_type) { |
125 |
|
quant_h263_intra(qcoeff, coeff, quant, iDcScaler, mpeg_quant_matrices); |
126 |
|
dequant_h263_intra(dqcoeff, qcoeff, quant, iDcScaler, mpeg_quant_matrices); |
127 |
|
} else { |
128 |
|
quant_mpeg_intra(qcoeff, coeff, quant, iDcScaler, mpeg_quant_matrices); |
129 |
|
dequant_mpeg_intra(dqcoeff, qcoeff, quant, iDcScaler, mpeg_quant_matrices); |
130 |
|
} |
131 |
|
|
132 |
fdct(data); |
predict_acdc(pMB-(x+mb_width*y), x, y, mb_width, block, qcoeff, |
133 |
data[0] -= 1024; |
quant, iDcScaler, predictors, 0); |
134 |
|
|
135 |
if (quant_type) quant_intra(coeff, data, quant, iDcScaler); |
direction = pMB->acpred_directions[block]; |
136 |
else quant4_intra(coeff, data, quant, iDcScaler); |
pCurrent = pMB->pred_values[block]; |
137 |
|
|
138 |
b_dc = coeff[0]; |
/* store current coeffs to pred_values[] for future prediction */ |
139 |
if (block < 4) { |
pCurrent[0] = qcoeff[0] * iDcScaler; |
140 |
coeff[0] -= *dcpred; |
pCurrent[0] = CLIP(pCurrent[0], -2048, 2047); |
141 |
*dcpred = b_dc; |
for (i = 1; i < 8; i++) { |
142 |
|
pCurrent[i] = qcoeff[i]; |
143 |
|
pCurrent[i + 7] = qcoeff[i * 8]; |
144 |
} |
} |
145 |
|
|
146 |
bits = BITS_MULT*CodeCoeffIntra_CalcBits(coeff, scan_tables[0]); |
/* dc prediction */ |
147 |
if (bits != 0) *cbp |= 1 << (5 - block); |
qcoeff[0] = qcoeff[0] - predictors[0]; |
148 |
|
|
149 |
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; |
150 |
else bits += BITS_MULT*dcc_tab[coeff[0] + 255].len; |
else bits[1] = bits[0] = dcc_tab[qcoeff[0] + 255].len; |
151 |
|
|
152 |
coeff[0] = b_dc; |
/* calc cost before ac prediction */ |
153 |
if (quant_type) dequant_intra(dqcoeff, coeff, quant, iDcScaler); |
bits[0] += coded = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]); |
154 |
else dequant4_intra(dqcoeff, coeff, quant, iDcScaler); |
if (coded > 0) cbp[0] |= 1 << (5 - block); |
155 |
|
|
156 |
for (i = 0; i < 64; i++) |
/* apply ac prediction & calc cost*/ |
157 |
distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]); |
if (direction == 1) { |
158 |
|
for (i = 1; i < 8; i++) { |
159 |
|
qcoeff[i] -= predictors[i]; |
160 |
|
predictors[i] = qcoeff[i]; |
161 |
|
} |
162 |
|
} else { /* acpred_direction == 2 */ |
163 |
|
for (i = 1; i < 8; i++) { |
164 |
|
qcoeff[i*8] -= predictors[i]; |
165 |
|
predictors[i] = qcoeff[i*8]; |
166 |
|
} |
167 |
|
} |
168 |
|
|
169 |
|
bits[1] += coded = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]); |
170 |
|
if (coded > 0) cbp[1] |= 1 << (5 - block); |
171 |
|
|
172 |
|
distortion = sse8_16bit(coeff, dqcoeff, 8*sizeof(int16_t)); |
173 |
|
|
174 |
return bits + (LAMBDA*distortion)/(quant*quant); |
return (lambda*distortion)/(quant*quant); |
175 |
} |
} |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
static void |
static void |
180 |
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) |
181 |
{ |
{ |
182 |
|
|
183 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
184 |
int32_t rd = 0; |
int32_t rd = 0; |
185 |
VECTOR * current; |
VECTOR * current; |
186 |
const uint8_t * ptr; |
const uint8_t * ptr; |
187 |
int i, cbp = 0, t, xc, yc; |
int i, t, xc, yc; |
188 |
|
unsigned cbp = 0; |
189 |
|
|
190 |
if ( (x > data->max_dx) || (x < data->min_dx) |
if ( (x > data->max_dx) || (x < data->min_dx) |
191 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
203 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
204 |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
205 |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
206 |
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->lambda[i], data->mpeg_quant_matrices); |
207 |
} |
} |
208 |
|
|
209 |
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); |
228 |
/* chroma U */ |
/* chroma U */ |
229 |
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); |
230 |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
231 |
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->lambda[4], data->mpeg_quant_matrices); |
232 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
233 |
|
|
234 |
/* chroma V */ |
/* chroma V */ |
235 |
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); |
236 |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
237 |
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->lambda[5], data->mpeg_quant_matrices); |
238 |
|
|
239 |
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; |
240 |
|
|
241 |
if (rd < data->iMinSAD[0]) { |
if (rd < data->iMinSAD[0]) { |
242 |
data->iMinSAD[0] = rd; |
data->iMinSAD[0] = rd; |
243 |
current[0].x = x; current[0].y = y; |
current[0].x = x; current[0].y = y; |
244 |
*data->dir = Direction; |
data->dir = Direction; |
245 |
*data->cbp = cbp; |
*data->cbp = cbp; |
246 |
} |
} |
247 |
} |
} |
248 |
|
|
249 |
static void |
static void |
250 |
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) |
251 |
{ |
{ |
252 |
|
|
253 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
254 |
int32_t rd; |
int32_t rd; |
255 |
VECTOR * current; |
VECTOR * current; |
256 |
const uint8_t * ptr; |
const uint8_t * ptr; |
257 |
int cbp = 0; |
unsigned int cbp = 0; |
258 |
|
|
259 |
if ( (x > data->max_dx) || (x < data->min_dx) |
if ( (x > data->max_dx) || (x < data->min_dx) |
260 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
268 |
} |
} |
269 |
|
|
270 |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
271 |
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->lambda[0], data->mpeg_quant_matrices); |
272 |
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); |
273 |
|
|
274 |
if (rd < data->iMinSAD[0]) { |
if (rd < data->iMinSAD[0]) { |
275 |
*data->cbp = cbp; |
*data->cbp = cbp; |
276 |
data->iMinSAD[0] = rd; |
data->iMinSAD[0] = rd; |
277 |
current[0].x = x; current[0].y = y; |
current[0].x = x; current[0].y = y; |
278 |
*data->dir = Direction; |
data->dir = Direction; |
279 |
} |
} |
280 |
} |
} |
281 |
|
|
315 |
xvid_me_SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidateRD16); |
xvid_me_SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidateRD16); |
316 |
|
|
317 |
if (MotionFlags&XVID_ME_HALFPELREFINE16_RD) |
if (MotionFlags&XVID_ME_HALFPELREFINE16_RD) |
318 |
xvid_me_SubpelRefine(Data, CheckCandidateRD16); |
xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidateRD16, 0); |
319 |
|
|
320 |
if (Data->qpel) { |
if (Data->qpel) { |
321 |
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 */ |
329 |
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, |
330 |
pParam->width, pParam->height, Data->iFcode, 2, 0); |
pParam->width, pParam->height, Data->iFcode, 2, 0); |
331 |
} |
} |
332 |
if (MotionFlags&XVID_ME_QUARTERPELREFINE16_RD) |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16_RD) { |
333 |
xvid_me_SubpelRefine(Data, CheckCandidateRD16); |
if (MotionFlags & XVID_ME_FASTREFINE16) |
334 |
|
FullRefine_Fast(Data, CheckCandidateRD16, 0); |
335 |
|
else |
336 |
|
xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidateRD16, 0); |
337 |
|
} |
338 |
} |
} |
339 |
|
|
340 |
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 */ |
346 |
} |
} |
347 |
|
|
348 |
static int |
static int |
349 |
findRD_inter4v(const SearchData * const Data, |
findRD_inter4v(SearchData * const Data, |
350 |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
351 |
const int x, const int y, |
const int x, const int y, |
352 |
const MBParam * const pParam, const uint32_t MotionFlags, |
const MBParam * const pParam, const uint32_t MotionFlags, |
353 |
const VECTOR * const backup) |
const VECTOR * const backup) |
354 |
{ |
{ |
355 |
|
|
356 |
int cbp = 0, bits = 0, t = 0, i; |
unsigned int cbp = 0, bits = 0, t = 0, i; |
357 |
SearchData Data2, *Data8 = &Data2; |
SearchData Data2, *Data8 = &Data2; |
358 |
int sumx = 0, sumy = 0; |
int sumx = 0, sumy = 0; |
359 |
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
363 |
|
|
364 |
for (i = 0; i < 4; i++) { /* for all luma blocks */ |
for (i = 0; i < 4; i++) { /* for all luma blocks */ |
365 |
|
|
366 |
Data8->iMinSAD = Data->iMinSAD + i + 1; |
*Data8->iMinSAD = *(Data->iMinSAD + i + 1); |
367 |
Data8->currentMV = Data->currentMV + i + 1; |
*Data8->currentMV = *(Data->currentMV + i + 1); |
368 |
Data8->currentQMV = Data->currentQMV + i + 1; |
*Data8->currentQMV = *(Data->currentQMV + i + 1); |
369 |
Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
370 |
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); |
371 |
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); |
372 |
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); |
373 |
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); |
374 |
*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 */ |
375 |
|
Data8->lambda[0] = Data->lambda[i]; |
376 |
|
|
377 |
if(Data->qpel) { |
if(Data->qpel) { |
378 |
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); |
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); |
398 |
} |
} |
399 |
|
|
400 |
if (Data8->qpel) { |
if (Data8->qpel) { |
401 |
if (MotionFlags&XVID_ME_HALFPELREFINE8_RD || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_RD)) { /* halfpixel motion search follows */ |
int bsad = Data8->iMinSAD[0]; |
402 |
int32_t s = *Data8->iMinSAD; |
int bx = Data8->currentQMV->x; |
403 |
|
int by = Data8->currentQMV->y; |
404 |
|
|
405 |
Data8->currentMV->x = Data8->currentQMV->x/2; |
Data8->currentMV->x = Data8->currentQMV->x/2; |
406 |
Data8->currentMV->y = Data8->currentQMV->y/2; |
Data8->currentMV->y = Data8->currentQMV->y/2; |
407 |
|
|
408 |
|
if (MotionFlags&XVID_ME_HALFPELREFINE8_RD || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_RD)) { /* halfpixel motion search follows */ |
409 |
Data8->qpel_precision = 0; |
Data8->qpel_precision = 0; |
410 |
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, |
411 |
pParam->width, pParam->height, Data8->iFcode - 1, 1, 0); |
pParam->width, pParam->height, Data8->iFcode - 1, 1, 0); |
417 |
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
418 |
|
|
419 |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
420 |
xvid_me_SubpelRefine(Data8, CheckCandidateRD8); |
xvid_me_SubpelRefine(Data->currentMV[0], Data8, CheckCandidateRD8, 0); |
421 |
|
|
422 |
if(s > *Data8->iMinSAD) { /* we have found a better match */ |
if(bsad > *Data8->iMinSAD) { /* we have found a better match */ |
423 |
Data8->currentQMV->x = 2*Data8->currentMV->x; |
bx = Data8->currentQMV->x = 2*Data8->currentMV->x; |
424 |
Data8->currentQMV->y = 2*Data8->currentMV->y; |
by = Data8->currentQMV->y = 2*Data8->currentMV->y; |
425 |
|
bsad = Data8->iMinSAD[0]; |
426 |
} |
} |
427 |
|
|
428 |
Data8->qpel_precision = 1; |
Data8->qpel_precision = 1; |
430 |
pParam->width, pParam->height, Data8->iFcode, 2, 0); |
pParam->width, pParam->height, Data8->iFcode, 2, 0); |
431 |
|
|
432 |
} |
} |
433 |
if (MotionFlags & XVID_ME_QUARTERPELREFINE8_RD) |
|
434 |
xvid_me_SubpelRefine(Data8, CheckCandidateRD8); |
if (MotionFlags & XVID_ME_QUARTERPELREFINE8_RD) { |
435 |
|
if (MotionFlags & XVID_ME_FASTREFINE8) |
436 |
|
FullRefine_Fast(Data8, CheckCandidateRD8, 0); |
437 |
|
else xvid_me_SubpelRefine(Data->currentQMV[0], Data8, CheckCandidateRD8, 0); |
438 |
|
} |
439 |
|
|
440 |
|
if (bsad <= Data->iMinSAD[0]) { |
441 |
|
/* we have not found a better match */ |
442 |
|
Data8->iMinSAD[0] = bsad; |
443 |
|
Data8->currentQMV->x = bx; |
444 |
|
Data8->currentQMV->y = by; |
445 |
|
} |
446 |
|
|
447 |
} else { /* not qpel */ |
} else { /* not qpel */ |
448 |
|
|
450 |
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
451 |
|
|
452 |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
453 |
xvid_me_SubpelRefine(Data8, CheckCandidateRD8); /* halfpel refinement */ |
xvid_me_SubpelRefine(Data->currentMV[0], Data8, CheckCandidateRD8, 0); /* halfpel refinement */ |
454 |
} |
} |
455 |
|
|
456 |
/* checking vector equal to predicion */ |
/* checking vector equal to predicion */ |
491 |
/* chroma U */ |
/* chroma U */ |
492 |
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); |
493 |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
494 |
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->lambda[4], Data->mpeg_quant_matrices); |
495 |
|
|
496 |
if (bits >= *Data->iMinSAD) return bits; |
if (bits >= *Data->iMinSAD) return bits; |
497 |
|
|
498 |
/* chroma V */ |
/* chroma V */ |
499 |
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); |
500 |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
501 |
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->lambda[5], Data->mpeg_quant_matrices); |
502 |
|
|
503 |
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; |
504 |
|
|
507 |
} |
} |
508 |
|
|
509 |
static int |
static int |
510 |
findRD_intra(const SearchData * const Data) |
findRD_intra(SearchData * const Data, MACROBLOCK * pMB, |
511 |
|
const int x, const int y, const int mb_width) |
512 |
{ |
{ |
513 |
int bits = BITS_MULT*1; /* this one is ac/dc prediction flag bit */ |
unsigned int cbp[2] = {0, 0}, bits[2], i; |
514 |
int cbp = 0, i, dc = 0; |
unsigned int bits1 = BITS_MULT*1, bits2 = BITS_MULT*1; /* this one is ac/dc prediction flag bit */ |
515 |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
unsigned int distortion = 0; |
516 |
|
|
517 |
|
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64, * dqcoeff = Data->dctSpace + 128; |
518 |
|
const uint32_t iQuant = Data->iQuant; |
519 |
|
int16_t predictors[6][8]; |
520 |
|
|
521 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
522 |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
523 |
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); |
|
524 |
|
|
525 |
if (bits >= Data->iMinSAD[0]) return bits; |
|
526 |
|
distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, i, in, coeff, dqcoeff, |
527 |
|
predictors[i], iQuant, Data->quant_type, bits, cbp, Data->lambda[i], Data->mpeg_quant_matrices); |
528 |
|
bits1 += distortion + BITS_MULT * bits[0]; |
529 |
|
bits2 += distortion + BITS_MULT * bits[1]; |
530 |
|
|
531 |
|
if (bits1 >= Data->iMinSAD[0] && bits2 >= Data->iMinSAD[0]) |
532 |
|
return bits1; |
533 |
} |
} |
534 |
|
|
535 |
bits += BITS_MULT*xvid_cbpy_tab[cbp>>2].len; |
bits1 += BITS_MULT*xvid_cbpy_tab[cbp[0]>>2].len; |
536 |
|
bits2 += BITS_MULT*xvid_cbpy_tab[cbp[1]>>2].len; |
537 |
|
|
538 |
/*chroma U */ |
/*chroma U */ |
539 |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
540 |
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, |
541 |
|
predictors[4], iQuant, Data->quant_type, bits, cbp, Data->lambda[4], Data->mpeg_quant_matrices); |
542 |
|
bits1 += distortion + BITS_MULT * bits[0]; |
543 |
|
bits2 += distortion + BITS_MULT * bits[1]; |
544 |
|
|
545 |
if (bits >= Data->iMinSAD[0]) return bits; |
if (bits1 >= Data->iMinSAD[0] && bits2 >= Data->iMinSAD[0]) |
546 |
|
return bits1; |
547 |
|
|
548 |
/* chroma V */ |
/* chroma V */ |
549 |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
550 |
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, |
551 |
|
predictors[5], iQuant, Data->quant_type, bits, cbp, Data->lambda[5], Data->mpeg_quant_matrices); |
552 |
|
|
553 |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
bits1 += distortion + BITS_MULT * bits[0]; |
554 |
|
bits2 += distortion + BITS_MULT * bits[1]; |
555 |
|
|
556 |
return bits; |
bits1 += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp[0] & 3) << 3)].len; |
557 |
|
bits2 += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp[1] & 3) << 3)].len; |
558 |
|
|
559 |
|
*Data->cbp = bits1 <= bits2 ? cbp[0] : cbp[1]; |
560 |
|
|
561 |
|
return MIN(bits1, bits2); |
562 |
} |
} |
563 |
|
|
564 |
|
|
565 |
static int |
static int |
566 |
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) |
567 |
{ |
{ |
568 |
int bits = BITS_MULT*1; /* this one is mcsel */ |
int bits = BITS_MULT*1; /* this one is mcsel */ |
569 |
int cbp = 0, i; |
unsigned int cbp = 0, i; |
570 |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
571 |
|
|
572 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
573 |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
574 |
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); |
575 |
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->lambda[i], Data->mpeg_quant_matrices); |
576 |
if (bits >= Data->iMinSAD[0]) return bits; |
if (bits >= Data->iMinSAD[0]) return bits; |
577 |
} |
} |
578 |
|
|
580 |
|
|
581 |
/*chroma U */ |
/*chroma U */ |
582 |
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); |
583 |
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->lambda[4], Data->mpeg_quant_matrices); |
584 |
|
|
585 |
if (bits >= Data->iMinSAD[0]) return bits; |
if (bits >= Data->iMinSAD[0]) return bits; |
586 |
|
|
587 |
/* chroma V */ |
/* chroma V */ |
588 |
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); |
589 |
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->lambda[5], Data->mpeg_quant_matrices); |
590 |
|
|
591 |
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; |
592 |
|
|
614 |
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
615 |
const uint32_t iQuant = pMB->quant; |
const uint32_t iQuant = pMB->quant; |
616 |
|
|
617 |
int min_rd, intra_rd, i, cbp, c[2] = {0, 0}; |
int min_rd, intra_rd, i, cbp; |
618 |
VECTOR backup[5], *v; |
VECTOR backup[5], *v; |
619 |
Data->iQuant = iQuant; |
Data->iQuant = iQuant; |
620 |
Data->cbp = c; |
Data->scan_table = VopFlags & XVID_VOP_ALTERNATESCAN ? |
621 |
|
scan_tables[2] : scan_tables[0]; |
622 |
|
|
623 |
pMB->mcsel = 0; |
pMB->mcsel = 0; |
624 |
|
|
628 |
backup[i] = v[i]; |
backup[i] = v[i]; |
629 |
} |
} |
630 |
|
|
631 |
|
for (i = 0; i < 6; i++) { |
632 |
|
/* HVS models, anyone ? */ |
633 |
|
Data->lambda[i] = LAMBDA; |
634 |
|
} |
635 |
|
|
636 |
min_rd = findRD_inter(Data, x, y, pParam, MotionFlags); |
min_rd = findRD_inter(Data, x, y, pParam, MotionFlags); |
637 |
cbp = *Data->cbp; |
cbp = *Data->cbp; |
638 |
|
|
658 |
} |
} |
659 |
} |
} |
660 |
|
|
661 |
intra_rd = findRD_intra(Data); |
intra_rd = findRD_intra(Data, pMB, x, y, pParam->mb_width); |
662 |
if (intra_rd < min_rd) { |
if (intra_rd < min_rd) { |
663 |
*Data->iMinSAD = min_rd = intra_rd; |
*Data->iMinSAD = min_rd = intra_rd; |
664 |
mode = MODE_INTRA; |
mode = MODE_INTRA; |
665 |
|
cbp = *Data->cbp; |
666 |
} |
} |
667 |
|
|
668 |
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; |
725 |
const uint32_t iQuant = pMB->quant; |
const uint32_t iQuant = pMB->quant; |
726 |
const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0); |
const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0); |
727 |
int sad; |
int sad; |
728 |
int min_rd = -1, intra_rd, i, cbp = 63, c[2] = {0, 0}; |
int min_rd = -1, intra_rd, i, cbp = 63; |
729 |
VECTOR backup[5], *v; |
VECTOR backup[5], *v; |
730 |
int sad_backup[5]; |
int sad_backup[5]; |
731 |
int InterBias = MV16_INTER_BIAS; |
int InterBias = MV16_INTER_BIAS; |
732 |
int thresh = 0; |
int thresh = 0; |
733 |
int top = 0, top_right = 0, left = 0; |
int top = 0, top_right = 0, left = 0; |
734 |
|
Data->scan_table = VopFlags & XVID_VOP_ALTERNATESCAN ? |
735 |
|
scan_tables[2] : scan_tables[0]; |
736 |
|
|
737 |
pMB->mcsel = 0; |
pMB->mcsel = 0; |
738 |
|
|
779 |
} |
} |
780 |
} else { /* Rate-Distortion INTER<->INTER4V */ |
} else { /* Rate-Distortion INTER<->INTER4V */ |
781 |
Data->iQuant = iQuant; |
Data->iQuant = iQuant; |
|
Data->cbp = c; |
|
782 |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
783 |
|
|
784 |
/* 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?" */ |
879 |
} else { /* Rate-Distortion INTRA<->INTER */ |
} else { /* Rate-Distortion INTRA<->INTER */ |
880 |
if(min_rd < 0) { |
if(min_rd < 0) { |
881 |
Data->iQuant = iQuant; |
Data->iQuant = iQuant; |
|
Data->cbp = c; |
|
882 |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
883 |
|
|
884 |
for (i = 0; i < 5; i++) { |
for (i = 0; i < 5; i++) { |
915 |
} |
} |
916 |
} |
} |
917 |
|
|
918 |
intra_rd = findRD_intra(Data); |
intra_rd = findRD_intra(Data, pMB, x, y, pParam->mb_width); |
919 |
if (intra_rd < min_rd) { |
if (intra_rd < min_rd) { |
920 |
*Data->iMinSAD = min_rd = intra_rd; |
*Data->iMinSAD = min_rd = intra_rd; |
921 |
mode = MODE_INTRA; |
mode = MODE_INTRA; |