1 |
|
/***************************************************************************** |
2 |
|
* |
3 |
|
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - Rate-Distortion Based Motion Estimation for P- and S- VOPs - |
5 |
|
* |
6 |
|
* Copyright(C) 2003 Radoslaw Czyz <xvid@syskin.cjb.net> |
7 |
|
* 2003 Michael Militzer <michael@xvid.org> |
8 |
|
* |
9 |
|
* 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 |
11 |
|
* the Free Software Foundation ; either version 2 of the License, or |
12 |
|
* (at your option) any later version. |
13 |
|
* |
14 |
|
* This program is distributed in the hope that it will be useful, |
15 |
|
* but WITHOUT ANY WARRANTY ; without even the implied warranty of |
16 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
|
* GNU General Public License for more details. |
18 |
|
* |
19 |
|
* You should have received a copy of the GNU General Public License |
20 |
|
* along with this program ; if not, write to the Free Software |
21 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
|
* |
23 |
|
* $Id$ |
24 |
|
* |
25 |
|
****************************************************************************/ |
26 |
|
|
27 |
|
/* RD mode decision and search */ |
28 |
|
|
29 |
|
#include <assert.h> |
30 |
|
#include <stdio.h> |
31 |
|
#include <stdlib.h> |
32 |
|
#include <string.h> /* memcpy */ |
33 |
|
|
34 |
|
#include "../encoder.h" |
35 |
|
#include "../bitstream/mbcoding.h" |
36 |
|
#include "../prediction/mbprediction.h" |
37 |
|
#include "../global.h" |
38 |
|
#include "../image/interpolate8x8.h" |
39 |
|
#include "estimation.h" |
40 |
|
#include "motion.h" |
41 |
|
#include "sad.h" |
42 |
|
#include "../bitstream/zigzag.h" |
43 |
|
#include "../quant/quant.h" |
44 |
|
#include "../bitstream/vlc_codes.h" |
45 |
|
#include "../dct/fdct.h" |
46 |
|
#include "motion_inlines.h" |
47 |
|
|
48 |
|
/* rd = BITS_MULT*bits + LAMBDA*distortion */ |
49 |
|
#define LAMBDA ( (int)(BITS_MULT*1.0) ) |
50 |
|
|
51 |
|
static __inline unsigned int |
52 |
|
Block_CalcBits( int16_t * const coeff, |
53 |
|
int16_t * const data, |
54 |
|
int16_t * const dqcoeff, |
55 |
|
const uint32_t quant, const int quant_type, |
56 |
|
uint32_t * cbp, |
57 |
|
const int block, |
58 |
|
const uint16_t * scan_table) |
59 |
|
{ |
60 |
|
int sum; |
61 |
|
int bits; |
62 |
|
int distortion = 0; |
63 |
|
int i; |
64 |
|
|
65 |
|
fdct(data); |
66 |
|
|
67 |
|
if (quant_type) sum = quant_h263_inter(coeff, data, quant); |
68 |
|
else sum = quant_mpeg_inter(coeff, data, quant); |
69 |
|
|
70 |
|
if (sum > 0) { |
71 |
|
*cbp |= 1 << (5 - block); |
72 |
|
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); |
73 |
|
|
74 |
|
if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant); |
75 |
|
else dequant_mpeg_inter(dqcoeff, coeff, quant); |
76 |
|
|
77 |
|
for (i = 0; i < 64; i++) |
78 |
|
distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]); |
79 |
|
|
80 |
|
} else { |
81 |
|
bits = 0; |
82 |
|
for (i = 0; i < 64; i++) |
83 |
|
distortion += data[i]*data[i]; |
84 |
|
} |
85 |
|
|
86 |
|
return bits + (LAMBDA*distortion)/(quant*quant); |
87 |
|
} |
88 |
|
|
89 |
|
static __inline unsigned int |
90 |
|
Block_CalcBitsIntra(MACROBLOCK * pMB, |
91 |
|
const unsigned int x, |
92 |
|
const unsigned int y, |
93 |
|
const unsigned int mb_width, |
94 |
|
const uint32_t block, |
95 |
|
int16_t coeff[64], |
96 |
|
int16_t qcoeff[64], |
97 |
|
int16_t dqcoeff[64], |
98 |
|
int16_t predictors[8], |
99 |
|
const uint32_t quant, |
100 |
|
const int quant_type, |
101 |
|
unsigned int bits[2], |
102 |
|
unsigned int cbp[2]) |
103 |
|
{ |
104 |
|
int direction; |
105 |
|
int16_t *pCurrent; |
106 |
|
unsigned int i, coded; |
107 |
|
unsigned int distortion = 0; |
108 |
|
const uint32_t iDcScaler = get_dc_scaler(quant, block < 4); |
109 |
|
|
110 |
|
fdct(coeff); |
111 |
|
|
112 |
|
if (quant_type) { |
113 |
|
quant_h263_intra(qcoeff, coeff, quant, iDcScaler); |
114 |
|
dequant_h263_intra(dqcoeff, qcoeff, quant, iDcScaler); |
115 |
|
} else { |
116 |
|
quant_mpeg_intra(qcoeff, coeff, quant, iDcScaler); |
117 |
|
dequant_mpeg_intra(dqcoeff, qcoeff, quant, iDcScaler); |
118 |
|
} |
119 |
|
|
120 |
|
predict_acdc(pMB-(x+mb_width*y), x, y, mb_width, block, qcoeff, |
121 |
|
quant, iDcScaler, predictors, 0); |
122 |
|
|
123 |
|
direction = pMB->acpred_directions[block]; |
124 |
|
pCurrent = pMB->pred_values[block]; |
125 |
|
|
126 |
|
/* store current coeffs to pred_values[] for future prediction */ |
127 |
|
pCurrent[0] = qcoeff[0] * iDcScaler; |
128 |
|
for (i = 1; i < 8; i++) { |
129 |
|
pCurrent[i] = qcoeff[i]; |
130 |
|
pCurrent[i + 7] = qcoeff[i * 8]; |
131 |
|
} |
132 |
|
|
133 |
|
/* dc prediction */ |
134 |
|
qcoeff[0] = qcoeff[0] - predictors[0]; |
135 |
|
|
136 |
|
if (block < 4) bits[1] = bits[0] = dcy_tab[qcoeff[0] + 255].len; |
137 |
|
else bits[1] = bits[0] = dcc_tab[qcoeff[0] + 255].len; |
138 |
|
|
139 |
|
/* calc cost before ac prediction */ |
140 |
|
bits[0] += coded = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]); |
141 |
|
if (coded > 0) cbp[0] |= 1 << (5 - block); |
142 |
|
|
143 |
|
/* apply ac prediction & calc cost*/ |
144 |
|
if (direction == 1) { |
145 |
|
for (i = 1; i < 8; i++) { |
146 |
|
qcoeff[i] -= predictors[i]; |
147 |
|
predictors[i] = qcoeff[i]; |
148 |
|
} |
149 |
|
} else { /* acpred_direction == 2 */ |
150 |
|
for (i = 1; i < 8; i++) { |
151 |
|
qcoeff[i*8] -= predictors[i]; |
152 |
|
predictors[i] = qcoeff[i*8]; |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
|
bits[1] += coded = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]); |
157 |
|
if (coded > 0) cbp[1] |= 1 << (5 - block); |
158 |
|
|
159 |
|
for (i = 0; i < 64; i++) |
160 |
|
distortion += (coeff[i] - dqcoeff[i])*(coeff[i] - dqcoeff[i]); |
161 |
|
|
162 |
|
|
163 |
|
return (LAMBDA*distortion)/(quant*quant); |
164 |
|
|
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
static void |
170 |
|
CheckCandidateRD16(const int x, const int y, const SearchData * const data, const unsigned int Direction) |
171 |
|
{ |
172 |
|
|
173 |
|
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
174 |
|
int32_t rd = 0; |
175 |
|
VECTOR * current; |
176 |
|
const uint8_t * ptr; |
177 |
|
int i, cbp = 0, t, xc, yc; |
178 |
|
|
179 |
|
if ( (x > data->max_dx) || (x < data->min_dx) |
180 |
|
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
181 |
|
|
182 |
|
if (!data->qpel_precision) { |
183 |
|
ptr = GetReference(x, y, data); |
184 |
|
current = data->currentMV; |
185 |
|
xc = x; yc = y; |
186 |
|
} else { /* x and y are in 1/4 precision */ |
187 |
|
ptr = xvid_me_interpolate16x16qpel(x, y, 0, data); |
188 |
|
current = data->currentQMV; |
189 |
|
xc = x/2; yc = y/2; |
190 |
|
} |
191 |
|
|
192 |
|
for(i = 0; i < 4; i++) { |
193 |
|
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
194 |
|
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
195 |
|
rd += data->temp[i] = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i, data->scan_table); |
196 |
|
} |
197 |
|
|
198 |
|
rd += t = BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
199 |
|
|
200 |
|
if (data->temp[0] + t < data->iMinSAD[1]) { |
201 |
|
data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; data->cbp[1] = (data->cbp[1]&~32) | (cbp&32); } |
202 |
|
if (data->temp[1] < data->iMinSAD[2]) { |
203 |
|
data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; data->cbp[1] = (data->cbp[1]&~16) | (cbp&16); } |
204 |
|
if (data->temp[2] < data->iMinSAD[3]) { |
205 |
|
data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; data->cbp[1] = (data->cbp[1]&~8) | (cbp&8); } |
206 |
|
if (data->temp[3] < data->iMinSAD[4]) { |
207 |
|
data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; data->cbp[1] = (data->cbp[1]&~4) | (cbp&4); } |
208 |
|
|
209 |
|
rd += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
210 |
|
|
211 |
|
if (rd >= data->iMinSAD[0]) return; |
212 |
|
|
213 |
|
/* chroma */ |
214 |
|
xc = (xc >> 1) + roundtab_79[xc & 0x3]; |
215 |
|
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
216 |
|
|
217 |
|
/* chroma U */ |
218 |
|
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
219 |
|
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
220 |
|
rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4, data->scan_table); |
221 |
|
if (rd >= data->iMinSAD[0]) return; |
222 |
|
|
223 |
|
/* chroma V */ |
224 |
|
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
225 |
|
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
226 |
|
rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5, data->scan_table); |
227 |
|
|
228 |
|
rd += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
229 |
|
|
230 |
|
if (rd < data->iMinSAD[0]) { |
231 |
|
data->iMinSAD[0] = rd; |
232 |
|
current[0].x = x; current[0].y = y; |
233 |
|
*data->dir = Direction; |
234 |
|
*data->cbp = cbp; |
235 |
|
} |
236 |
|
} |
237 |
|
|
238 |
|
static void |
239 |
|
CheckCandidateRD8(const int x, const int y, const SearchData * const data, const unsigned int Direction) |
240 |
|
{ |
241 |
|
|
242 |
|
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
243 |
|
int32_t rd; |
244 |
|
VECTOR * current; |
245 |
|
const uint8_t * ptr; |
246 |
|
int cbp = 0; |
247 |
|
|
248 |
|
if ( (x > data->max_dx) || (x < data->min_dx) |
249 |
|
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
250 |
|
|
251 |
|
if (!data->qpel_precision) { |
252 |
|
ptr = GetReference(x, y, data); |
253 |
|
current = data->currentMV; |
254 |
|
} else { /* x and y are in 1/4 precision */ |
255 |
|
ptr = xvid_me_interpolate8x8qpel(x, y, 0, 0, data); |
256 |
|
current = data->currentQMV; |
257 |
|
} |
258 |
|
|
259 |
|
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
260 |
|
rd = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5, data->scan_table); |
261 |
|
rd += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
262 |
|
|
263 |
|
if (rd < data->iMinSAD[0]) { |
264 |
|
*data->cbp = cbp; |
265 |
|
data->iMinSAD[0] = rd; |
266 |
|
current[0].x = x; current[0].y = y; |
267 |
|
*data->dir = Direction; |
268 |
|
} |
269 |
|
} |
270 |
|
|
271 |
|
|
272 |
|
static int |
273 |
|
findRD_inter(SearchData * const Data, |
274 |
|
const int x, const int y, |
275 |
|
const MBParam * const pParam, |
276 |
|
const uint32_t MotionFlags) |
277 |
|
{ |
278 |
|
int i; |
279 |
|
int32_t bsad[5]; |
280 |
|
|
281 |
|
if (Data->qpel) { |
282 |
|
for(i = 0; i < 5; i++) { |
283 |
|
Data->currentMV[i].x = Data->currentQMV[i].x/2; |
284 |
|
Data->currentMV[i].y = Data->currentQMV[i].y/2; |
285 |
|
} |
286 |
|
Data->qpel_precision = 1; |
287 |
|
CheckCandidateRD16(Data->currentQMV[0].x, Data->currentQMV[0].y, Data, 255); |
288 |
|
|
289 |
|
if (MotionFlags & (XVID_ME_HALFPELREFINE16_RD | XVID_ME_EXTSEARCH_RD)) { /* we have to prepare for halfpixel-precision search */ |
290 |
|
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
291 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
292 |
|
pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv); |
293 |
|
Data->qpel_precision = 0; |
294 |
|
if (Data->currentQMV->x & 1 || Data->currentQMV->y & 1) |
295 |
|
CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255); |
296 |
|
} |
297 |
|
|
298 |
|
} else { /* not qpel */ |
299 |
|
|
300 |
|
CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255); |
301 |
|
} |
302 |
|
|
303 |
|
if (MotionFlags&XVID_ME_EXTSEARCH_RD) |
304 |
|
xvid_me_SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidateRD16); |
305 |
|
|
306 |
|
if (MotionFlags&XVID_ME_HALFPELREFINE16_RD) |
307 |
|
xvid_me_SubpelRefine(Data, CheckCandidateRD16); |
308 |
|
|
309 |
|
if (Data->qpel) { |
310 |
|
if (MotionFlags&(XVID_ME_EXTSEARCH_RD | XVID_ME_HALFPELREFINE16_RD)) { /* there was halfpel-precision search */ |
311 |
|
for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) { |
312 |
|
Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* we have found a better match */ |
313 |
|
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
314 |
|
} |
315 |
|
|
316 |
|
/* preparing for qpel-precision search */ |
317 |
|
Data->qpel_precision = 1; |
318 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
319 |
|
pParam->width, pParam->height, Data->iFcode, 2, 0); |
320 |
|
} |
321 |
|
if (MotionFlags&XVID_ME_QUARTERPELREFINE16_RD) |
322 |
|
xvid_me_SubpelRefine(Data, CheckCandidateRD16); |
323 |
|
} |
324 |
|
|
325 |
|
if (MotionFlags&XVID_ME_CHECKPREDICTION_RD) { /* let's check vector equal to prediction */ |
326 |
|
VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV; |
327 |
|
if (!MVequal(Data->predMV, *v)) |
328 |
|
CheckCandidateRD16(Data->predMV.x, Data->predMV.y, Data, 255); |
329 |
|
} |
330 |
|
return Data->iMinSAD[0]; |
331 |
|
} |
332 |
|
|
333 |
|
static int |
334 |
|
findRD_inter4v(const SearchData * const Data, |
335 |
|
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
336 |
|
const int x, const int y, |
337 |
|
const MBParam * const pParam, const uint32_t MotionFlags, |
338 |
|
const VECTOR * const backup) |
339 |
|
{ |
340 |
|
|
341 |
|
int cbp = 0, bits = 0, t = 0, i; |
342 |
|
SearchData Data2, *Data8 = &Data2; |
343 |
|
int sumx = 0, sumy = 0; |
344 |
|
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
345 |
|
uint8_t * ptr; |
346 |
|
|
347 |
|
memcpy(Data8, Data, sizeof(SearchData)); |
348 |
|
|
349 |
|
for (i = 0; i < 4; i++) { /* for all luma blocks */ |
350 |
|
|
351 |
|
Data8->iMinSAD = Data->iMinSAD + i + 1; |
352 |
|
Data8->currentMV = Data->currentMV + i + 1; |
353 |
|
Data8->currentQMV = Data->currentQMV + i + 1; |
354 |
|
Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
355 |
|
Data8->RefP[0] = Data->RefP[0] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
356 |
|
Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
357 |
|
Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
358 |
|
Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
359 |
|
*Data8->cbp = (Data->cbp[1] & (1<<(5-i))) ? 1:0; /* copy corresponding cbp bit */ |
360 |
|
|
361 |
|
if(Data->qpel) { |
362 |
|
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); |
363 |
|
if (i != 0) t = d_mv_bits( Data8->currentQMV->x, Data8->currentQMV->y, |
364 |
|
Data8->predMV, Data8->iFcode, 0, 0); |
365 |
|
} else { |
366 |
|
Data8->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, i); |
367 |
|
if (i != 0) t = d_mv_bits( Data8->currentMV->x, Data8->currentMV->y, |
368 |
|
Data8->predMV, Data8->iFcode, 0, 0); |
369 |
|
} |
370 |
|
|
371 |
|
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, |
372 |
|
pParam->width, pParam->height, Data8->iFcode, Data8->qpel+1, 0); |
373 |
|
|
374 |
|
*Data8->iMinSAD += BITS_MULT*t; |
375 |
|
|
376 |
|
Data8->qpel_precision = Data8->qpel; |
377 |
|
/* checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) */ |
378 |
|
{ |
379 |
|
VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV; |
380 |
|
if (!MVequal (*v, backup[i+1]) ) |
381 |
|
CheckCandidateRD8(backup[i+1].x, backup[i+1].y, Data8, 255); |
382 |
|
} |
383 |
|
|
384 |
|
if (Data8->qpel) { |
385 |
|
if (MotionFlags&XVID_ME_HALFPELREFINE8_RD || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_RD)) { /* halfpixel motion search follows */ |
386 |
|
int32_t s = *Data8->iMinSAD; |
387 |
|
Data8->currentMV->x = Data8->currentQMV->x/2; |
388 |
|
Data8->currentMV->y = Data8->currentQMV->y/2; |
389 |
|
Data8->qpel_precision = 0; |
390 |
|
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, |
391 |
|
pParam->width, pParam->height, Data8->iFcode - 1, 1, 0); |
392 |
|
|
393 |
|
if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1) |
394 |
|
CheckCandidateRD8(Data8->currentMV->x, Data8->currentMV->y, Data8, 255); |
395 |
|
|
396 |
|
if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_RD) |
397 |
|
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
398 |
|
|
399 |
|
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
400 |
|
xvid_me_SubpelRefine(Data8, CheckCandidateRD8); |
401 |
|
|
402 |
|
if(s > *Data8->iMinSAD) { /* we have found a better match */ |
403 |
|
Data8->currentQMV->x = 2*Data8->currentMV->x; |
404 |
|
Data8->currentQMV->y = 2*Data8->currentMV->y; |
405 |
|
} |
406 |
|
|
407 |
|
Data8->qpel_precision = 1; |
408 |
|
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, |
409 |
|
pParam->width, pParam->height, Data8->iFcode, 2, 0); |
410 |
|
|
411 |
|
} |
412 |
|
if (MotionFlags & XVID_ME_QUARTERPELREFINE8_RD) |
413 |
|
xvid_me_SubpelRefine(Data8, CheckCandidateRD8); |
414 |
|
|
415 |
|
} else { /* not qpel */ |
416 |
|
|
417 |
|
if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_RD) /* extsearch */ |
418 |
|
xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); |
419 |
|
|
420 |
|
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
421 |
|
xvid_me_SubpelRefine(Data8, CheckCandidateRD8); /* halfpel refinement */ |
422 |
|
} |
423 |
|
|
424 |
|
/* checking vector equal to predicion */ |
425 |
|
if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_RD) { |
426 |
|
const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; |
427 |
|
if (!MVequal(*v, Data8->predMV)) |
428 |
|
CheckCandidateRD8(Data8->predMV.x, Data8->predMV.y, Data8, 255); |
429 |
|
} |
430 |
|
|
431 |
|
bits += *Data8->iMinSAD; |
432 |
|
if (bits >= Data->iMinSAD[0]) return bits; /* no chances for INTER4V */ |
433 |
|
|
434 |
|
/* MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else */ |
435 |
|
if(Data->qpel) { |
436 |
|
pMB->pmvs[i].x = Data8->currentQMV->x - Data8->predMV.x; |
437 |
|
pMB->pmvs[i].y = Data8->currentQMV->y - Data8->predMV.y; |
438 |
|
pMB->qmvs[i] = *Data8->currentQMV; |
439 |
|
sumx += Data8->currentQMV->x/2; |
440 |
|
sumy += Data8->currentQMV->y/2; |
441 |
|
} else { |
442 |
|
pMB->pmvs[i].x = Data8->currentMV->x - Data8->predMV.x; |
443 |
|
pMB->pmvs[i].y = Data8->currentMV->y - Data8->predMV.y; |
444 |
|
sumx += Data8->currentMV->x; |
445 |
|
sumy += Data8->currentMV->y; |
446 |
|
} |
447 |
|
pMB->mvs[i] = *Data8->currentMV; |
448 |
|
pMB->sad8[i] = 4 * *Data8->iMinSAD; |
449 |
|
if (Data8->cbp[0]) cbp |= 1 << (5 - i); |
450 |
|
|
451 |
|
} /* end - for all luma blocks */ |
452 |
|
|
453 |
|
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
454 |
|
|
455 |
|
/* let's check chroma */ |
456 |
|
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
457 |
|
sumy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
458 |
|
|
459 |
|
/* chroma U */ |
460 |
|
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
461 |
|
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
462 |
|
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, Data->scan_table); |
463 |
|
|
464 |
|
if (bits >= *Data->iMinSAD) return bits; |
465 |
|
|
466 |
|
/* chroma V */ |
467 |
|
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
468 |
|
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
469 |
|
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, Data->scan_table); |
470 |
|
|
471 |
|
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
472 |
|
|
473 |
|
*Data->cbp = cbp; |
474 |
|
return bits; |
475 |
|
} |
476 |
|
|
477 |
|
static int |
478 |
|
findRD_intra(const SearchData * const Data, MACROBLOCK * pMB, |
479 |
|
const int x, const int y, const int mb_width) |
480 |
|
{ |
481 |
|
int cbp[2] = {0, 0}, bits[2], i; |
482 |
|
int bits1 = BITS_MULT*1, bits2 = BITS_MULT*1; /* this one is ac/dc prediction flag bit */ |
483 |
|
int distortion = 0; |
484 |
|
|
485 |
|
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64, * dqcoeff = Data->dctSpace + 128; |
486 |
|
const uint32_t iQuant = Data->iQuant; |
487 |
|
int16_t predictors[6][8]; |
488 |
|
|
489 |
|
for(i = 0; i < 4; i++) { |
490 |
|
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
491 |
|
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
492 |
|
|
493 |
|
|
494 |
|
distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, i, in, coeff, dqcoeff, |
495 |
|
predictors[i], iQuant, Data->quant_type, bits, cbp); |
496 |
|
bits1 += distortion + BITS_MULT * bits[0]; |
497 |
|
bits2 += distortion + BITS_MULT * bits[1]; |
498 |
|
|
499 |
|
if (bits1 >= Data->iMinSAD[0] && bits2 >= Data->iMinSAD[0]) |
500 |
|
return bits1; |
501 |
|
} |
502 |
|
|
503 |
|
bits1 += BITS_MULT*xvid_cbpy_tab[cbp[0]>>2].len; |
504 |
|
bits2 += BITS_MULT*xvid_cbpy_tab[cbp[1]>>2].len; |
505 |
|
|
506 |
|
/*chroma U */ |
507 |
|
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
508 |
|
distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, 4, in, coeff, dqcoeff, |
509 |
|
predictors[4], iQuant, Data->quant_type, bits, cbp); |
510 |
|
bits1 += distortion + BITS_MULT * bits[0]; |
511 |
|
bits2 += distortion + BITS_MULT * bits[1]; |
512 |
|
|
513 |
|
if (bits1 >= Data->iMinSAD[0] && bits2 >= Data->iMinSAD[0]) |
514 |
|
return bits1; |
515 |
|
|
516 |
|
/* chroma V */ |
517 |
|
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
518 |
|
distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, 5, in, coeff, dqcoeff, |
519 |
|
predictors[5], iQuant, Data->quant_type, bits, cbp); |
520 |
|
|
521 |
|
bits1 += distortion + BITS_MULT * bits[0]; |
522 |
|
bits2 += distortion + BITS_MULT * bits[1]; |
523 |
|
|
524 |
|
bits1 += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp[0] & 3) << 3)].len; |
525 |
|
bits2 += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp[1] & 3) << 3)].len; |
526 |
|
|
527 |
|
*Data->cbp = bits1 <= bits2 ? cbp[0] : cbp[1]; |
528 |
|
|
529 |
|
return MIN(bits1, bits2); |
530 |
|
} |
531 |
|
|
532 |
|
|
533 |
|
static int |
534 |
|
findRD_gmc(const SearchData * const Data, const IMAGE * const vGMC, const int x, const int y) |
535 |
|
{ |
536 |
|
int bits = BITS_MULT*1; /* this one is mcsel */ |
537 |
|
int cbp = 0, i; |
538 |
|
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
539 |
|
|
540 |
|
for(i = 0; i < 4; i++) { |
541 |
|
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
542 |
|
transfer_8to16subro(in, Data->Cur + s, vGMC->y + s + 16*(x+y*Data->iEdgedWidth), Data->iEdgedWidth); |
543 |
|
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i, Data->scan_table); |
544 |
|
if (bits >= Data->iMinSAD[0]) return bits; |
545 |
|
} |
546 |
|
|
547 |
|
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
548 |
|
|
549 |
|
/*chroma U */ |
550 |
|
transfer_8to16subro(in, Data->CurU, vGMC->u + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2); |
551 |
|
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, Data->scan_table); |
552 |
|
|
553 |
|
if (bits >= Data->iMinSAD[0]) return bits; |
554 |
|
|
555 |
|
/* chroma V */ |
556 |
|
transfer_8to16subro(in, Data->CurV , vGMC->v + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2); |
557 |
|
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, Data->scan_table); |
558 |
|
|
559 |
|
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
560 |
|
|
561 |
|
*Data->cbp = cbp; |
562 |
|
|
563 |
|
return bits; |
564 |
|
} |
565 |
|
|
566 |
|
void |
567 |
|
xvid_me_ModeDecision_RD(SearchData * const Data, |
568 |
|
MACROBLOCK * const pMB, |
569 |
|
const MACROBLOCK * const pMBs, |
570 |
|
const int x, const int y, |
571 |
|
const MBParam * const pParam, |
572 |
|
const uint32_t MotionFlags, |
573 |
|
const uint32_t VopFlags, |
574 |
|
const uint32_t VolFlags, |
575 |
|
const IMAGE * const pCurrent, |
576 |
|
const IMAGE * const pRef, |
577 |
|
const IMAGE * const vGMC, |
578 |
|
const int coding_type) |
579 |
|
{ |
580 |
|
int mode = MODE_INTER; |
581 |
|
int mcsel = 0; |
582 |
|
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
583 |
|
const uint32_t iQuant = pMB->quant; |
584 |
|
|
585 |
|
int min_rd, intra_rd, i, cbp, c[2] = {0, 0}; |
586 |
|
VECTOR backup[5], *v; |
587 |
|
Data->iQuant = iQuant; |
588 |
|
Data->cbp = c; |
589 |
|
Data->scan_table = VopFlags & XVID_VOP_ALTERNATESCAN ? |
590 |
|
scan_tables[2] : scan_tables[0]; |
591 |
|
|
592 |
|
pMB->mcsel = 0; |
593 |
|
|
594 |
|
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
595 |
|
for (i = 0; i < 5; i++) { |
596 |
|
Data->iMinSAD[i] = 256*4096; |
597 |
|
backup[i] = v[i]; |
598 |
|
} |
599 |
|
|
600 |
|
min_rd = findRD_inter(Data, x, y, pParam, MotionFlags); |
601 |
|
cbp = *Data->cbp; |
602 |
|
|
603 |
|
if (coding_type == S_VOP) { |
604 |
|
int gmc_rd; |
605 |
|
*Data->iMinSAD = min_rd += BITS_MULT*1; /* mcsel */ |
606 |
|
gmc_rd = findRD_gmc(Data, vGMC, x, y); |
607 |
|
if (gmc_rd < min_rd) { |
608 |
|
mcsel = 1; |
609 |
|
*Data->iMinSAD = min_rd = gmc_rd; |
610 |
|
mode = MODE_INTER; |
611 |
|
cbp = *Data->cbp; |
612 |
|
} |
613 |
|
} |
614 |
|
|
615 |
|
if (inter4v) { |
616 |
|
int v4_rd; |
617 |
|
v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
618 |
|
if (v4_rd < min_rd) { |
619 |
|
Data->iMinSAD[0] = min_rd = v4_rd; |
620 |
|
mode = MODE_INTER4V; |
621 |
|
cbp = *Data->cbp; |
622 |
|
} |
623 |
|
} |
624 |
|
|
625 |
|
intra_rd = findRD_intra(Data, pMB, x, y, pParam->mb_width); |
626 |
|
if (intra_rd < min_rd) { |
627 |
|
*Data->iMinSAD = min_rd = intra_rd; |
628 |
|
mode = MODE_INTRA; |
629 |
|
cbp = *Data->cbp; |
630 |
|
} |
631 |
|
|
632 |
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
633 |
|
pMB->cbp = cbp; |
634 |
|
|
635 |
|
|
636 |
|
if (Data->rrv) { |
637 |
|
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
638 |
|
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
639 |
|
} |
640 |
|
|
641 |
|
if (mode == MODE_INTER && mcsel == 0) { |
642 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
643 |
|
|
644 |
|
if(Data->qpel) { |
645 |
|
pMB->qmvs[0] = pMB->qmvs[1] |
646 |
|
= pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; |
647 |
|
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x; |
648 |
|
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y; |
649 |
|
} else { |
650 |
|
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
651 |
|
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
652 |
|
} |
653 |
|
|
654 |
|
} else if (mode == MODE_INTER ) { /* but mcsel == 1 */ |
655 |
|
|
656 |
|
pMB->mcsel = 1; |
657 |
|
if (Data->qpel) { |
658 |
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
659 |
|
pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2; |
660 |
|
pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2; |
661 |
|
} else |
662 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
663 |
|
|
664 |
|
} else |
665 |
|
if (mode == MODE_INTER4V) ; /* anything here? */ |
666 |
|
else /* INTRA, NOT_CODED */ |
667 |
|
ZeroMacroblockP(pMB, 0); |
668 |
|
|
669 |
|
pMB->mode = mode; |
670 |
|
} |
671 |
|
|
672 |
|
void |
673 |
|
xvid_me_ModeDecision_Fast(SearchData * const Data, |
674 |
|
MACROBLOCK * const pMB, |
675 |
|
const MACROBLOCK * const pMBs, |
676 |
|
const int x, const int y, |
677 |
|
const MBParam * const pParam, |
678 |
|
const uint32_t MotionFlags, |
679 |
|
const uint32_t VopFlags, |
680 |
|
const uint32_t VolFlags, |
681 |
|
const IMAGE * const pCurrent, |
682 |
|
const IMAGE * const pRef, |
683 |
|
const IMAGE * const vGMC, |
684 |
|
const int coding_type) |
685 |
|
{ |
686 |
|
int mode = MODE_INTER; |
687 |
|
int mcsel = 0; |
688 |
|
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
689 |
|
const uint32_t iQuant = pMB->quant; |
690 |
|
const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0); |
691 |
|
int sad; |
692 |
|
int min_rd = -1, intra_rd, i, cbp = 63, c[2] = {0, 0}; |
693 |
|
VECTOR backup[5], *v; |
694 |
|
int sad_backup[5]; |
695 |
|
int InterBias = MV16_INTER_BIAS; |
696 |
|
int thresh = 0; |
697 |
|
int top = 0, top_right = 0, left = 0; |
698 |
|
Data->scan_table = VopFlags & XVID_VOP_ALTERNATESCAN ? |
699 |
|
scan_tables[2] : scan_tables[0]; |
700 |
|
|
701 |
|
pMB->mcsel = 0; |
702 |
|
|
703 |
|
/* INTER <-> INTER4V decision */ |
704 |
|
if ((Data->iMinSAD[0] + 75 < Data->iMinSAD[1] + |
705 |
|
Data->iMinSAD[2] + Data->iMinSAD[3] + Data->iMinSAD[4])) { /* normal, fast, SAD-based mode decision */ |
706 |
|
if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
707 |
|
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) { |
708 |
|
mode = MODE_INTER; |
709 |
|
sad = Data->iMinSAD[0]; |
710 |
|
} else { |
711 |
|
mode = MODE_INTER4V; |
712 |
|
sad = Data->iMinSAD[1] + Data->iMinSAD[2] + |
713 |
|
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant; |
714 |
|
Data->iMinSAD[0] = sad; |
715 |
|
} |
716 |
|
|
717 |
|
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
718 |
|
if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP)) |
719 |
|
if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH) |
720 |
|
if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) { |
721 |
|
mode = MODE_NOT_CODED; |
722 |
|
sad = 0; /* Compiler warning */ |
723 |
|
goto early_out; |
724 |
|
} |
725 |
|
|
726 |
|
/* mcsel */ |
727 |
|
if (coding_type == S_VOP) { |
728 |
|
|
729 |
|
int32_t iSAD = sad16(Data->Cur, |
730 |
|
vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536); |
731 |
|
|
732 |
|
if (Data->chroma) { |
733 |
|
iSAD += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); |
734 |
|
iSAD += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); |
735 |
|
} |
736 |
|
|
737 |
|
if (iSAD <= sad) { /* mode decision GMC */ |
738 |
|
mode = MODE_INTER; |
739 |
|
mcsel = 1; |
740 |
|
sad = iSAD; |
741 |
|
} |
742 |
|
|
743 |
|
} |
744 |
|
} else { /* Rate-Distortion INTER<->INTER4V */ |
745 |
|
Data->iQuant = iQuant; |
746 |
|
Data->cbp = c; |
747 |
|
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
748 |
|
|
749 |
|
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
750 |
|
if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP)) |
751 |
|
if ( (100*Data->iMinSAD[0])/(pMB->sad16+1) > FINAL_SKIP_THRESH) |
752 |
|
if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) { |
753 |
|
mode = MODE_NOT_CODED; |
754 |
|
sad = 0; /* Compiler warning */ |
755 |
|
goto early_out; |
756 |
|
} |
757 |
|
|
758 |
|
for (i = 0; i < 5; i++) { |
759 |
|
sad_backup[i] = Data->iMinSAD[i]; |
760 |
|
Data->iMinSAD[i] = 256*4096; |
761 |
|
backup[i] = v[i]; |
762 |
|
} |
763 |
|
|
764 |
|
min_rd = findRD_inter(Data, x, y, pParam, MotionFlags); |
765 |
|
cbp = *Data->cbp; |
766 |
|
sad = sad_backup[0]; |
767 |
|
|
768 |
|
if (coding_type == S_VOP) { |
769 |
|
int gmc_rd; |
770 |
|
|
771 |
|
*Data->iMinSAD = min_rd += BITS_MULT*1; /* mcsel */ |
772 |
|
gmc_rd = findRD_gmc(Data, vGMC, x, y); |
773 |
|
if (gmc_rd < min_rd) { |
774 |
|
mcsel = 1; |
775 |
|
*Data->iMinSAD = min_rd = gmc_rd; |
776 |
|
mode = MODE_INTER; |
777 |
|
cbp = *Data->cbp; |
778 |
|
sad = sad16(Data->Cur, |
779 |
|
vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536); |
780 |
|
if (Data->chroma) { |
781 |
|
sad += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); |
782 |
|
sad += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); |
783 |
|
} |
784 |
|
} |
785 |
|
} |
786 |
|
|
787 |
|
if (inter4v) { |
788 |
|
int v4_rd; |
789 |
|
v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
790 |
|
if (v4_rd < min_rd) { |
791 |
|
Data->iMinSAD[0] = min_rd = v4_rd; |
792 |
|
mode = MODE_INTER4V; |
793 |
|
cbp = *Data->cbp; |
794 |
|
sad = sad_backup[1] + sad_backup[2] + |
795 |
|
sad_backup[3] + sad_backup[4] + IMV16X16 * (int32_t)iQuant; |
796 |
|
} |
797 |
|
} |
798 |
|
} |
799 |
|
|
800 |
|
left = top = top_right = -1; |
801 |
|
thresh = 0; |
802 |
|
|
803 |
|
if((x > 0) && (y > 0) && (x < (int32_t) pParam->mb_width)) { |
804 |
|
left = (&pMBs[(x-1) + y * pParam->mb_width])->sad16; /* left */ |
805 |
|
top = (&pMBs[x + (y-1) * pParam->mb_width])->sad16; /* top */ |
806 |
|
top_right = (&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16; /* top right */ |
807 |
|
|
808 |
|
if(((&pMBs[(x-1) + y * pParam->mb_width])->mode != MODE_INTRA) && |
809 |
|
((&pMBs[x + (y-1) * pParam->mb_width])->mode != MODE_INTRA) && |
810 |
|
((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mode != MODE_INTRA)) { |
811 |
|
thresh = MAX(MAX(top, left), top_right); |
812 |
|
} |
813 |
|
else |
814 |
|
thresh = MIN(MIN(top, left), top_right); |
815 |
|
} |
816 |
|
|
817 |
|
/* INTRA <-> INTER decision */ |
818 |
|
if (sad < thresh) { /* normal, fast, SAD-based mode decision */ |
819 |
|
/* intra decision */ |
820 |
|
|
821 |
|
if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */ |
822 |
|
if (y != 0) |
823 |
|
if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80; |
824 |
|
if (x != 0) |
825 |
|
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
826 |
|
|
827 |
|
if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */ |
828 |
|
if (Data->rrv) InterBias *= 4; |
829 |
|
|
830 |
|
if (InterBias < sad) { |
831 |
|
int32_t deviation; |
832 |
|
if (!Data->rrv) |
833 |
|
deviation = dev16(Data->Cur, Data->iEdgedWidth); |
834 |
|
else |
835 |
|
deviation = dev16(Data->Cur, Data->iEdgedWidth) + /* dev32() */ |
836 |
|
dev16(Data->Cur+16, Data->iEdgedWidth) + |
837 |
|
dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) + |
838 |
|
dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth); |
839 |
|
|
840 |
|
if (deviation < (sad - InterBias)) mode = MODE_INTRA; |
841 |
|
} |
842 |
|
|
843 |
|
pMB->cbp = 63; |
844 |
|
} else { /* Rate-Distortion INTRA<->INTER */ |
845 |
|
if(min_rd < 0) { |
846 |
|
Data->iQuant = iQuant; |
847 |
|
Data->cbp = c; |
848 |
|
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
849 |
|
|
850 |
|
for (i = 0; i < 5; i++) { |
851 |
|
Data->iMinSAD[i] = 256*4096; |
852 |
|
backup[i] = v[i]; |
853 |
|
} |
854 |
|
|
855 |
|
if(mode == MODE_INTER) { |
856 |
|
min_rd = findRD_inter(Data, x, y, pParam, MotionFlags); |
857 |
|
cbp = *Data->cbp; |
858 |
|
|
859 |
|
if (coding_type == S_VOP) { |
860 |
|
int gmc_rd; |
861 |
|
|
862 |
|
*Data->iMinSAD = min_rd += BITS_MULT*1; /* mcsel */ |
863 |
|
gmc_rd = findRD_gmc(Data, vGMC, x, y); |
864 |
|
if (gmc_rd < min_rd) { |
865 |
|
mcsel = 1; |
866 |
|
*Data->iMinSAD = min_rd = gmc_rd; |
867 |
|
mode = MODE_INTER; |
868 |
|
cbp = *Data->cbp; |
869 |
|
} |
870 |
|
} |
871 |
|
} |
872 |
|
|
873 |
|
if(mode == MODE_INTER4V) { |
874 |
|
int v4_rd; |
875 |
|
v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
876 |
|
if (v4_rd < min_rd) { |
877 |
|
Data->iMinSAD[0] = min_rd = v4_rd; |
878 |
|
mode = MODE_INTER4V; |
879 |
|
cbp = *Data->cbp; |
880 |
|
} |
881 |
|
} |
882 |
|
} |
883 |
|
|
884 |
|
intra_rd = findRD_intra(Data, pMB, x, y, pParam->mb_width); |
885 |
|
if (intra_rd < min_rd) { |
886 |
|
*Data->iMinSAD = min_rd = intra_rd; |
887 |
|
mode = MODE_INTRA; |
888 |
|
} |
889 |
|
|
890 |
|
pMB->cbp = cbp; |
891 |
|
} |
892 |
|
|
893 |
|
early_out: |
894 |
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; |
895 |
|
|
896 |
|
if (Data->rrv) { |
897 |
|
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
898 |
|
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
899 |
|
} |
900 |
|
|
901 |
|
if (mode == MODE_INTER && mcsel == 0) { |
902 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
903 |
|
|
904 |
|
if(Data->qpel) { |
905 |
|
pMB->qmvs[0] = pMB->qmvs[1] |
906 |
|
= pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; |
907 |
|
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x; |
908 |
|
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y; |
909 |
|
} else { |
910 |
|
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
911 |
|
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
912 |
|
} |
913 |
|
|
914 |
|
} else if (mode == MODE_INTER ) { /* but mcsel == 1 */ |
915 |
|
|
916 |
|
pMB->mcsel = 1; |
917 |
|
if (Data->qpel) { |
918 |
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
919 |
|
pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2; |
920 |
|
pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2; |
921 |
|
} else |
922 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
923 |
|
|
924 |
|
} else |
925 |
|
if (mode == MODE_INTER4V) ; /* anything here? */ |
926 |
|
else /* INTRA, NOT_CODED */ |
927 |
|
ZeroMacroblockP(pMB, 0); |
928 |
|
|
929 |
|
pMB->mode = mode; |
930 |
|
} |