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