1 |
|
/***************************************************************************** |
2 |
|
* |
3 |
|
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - Motion Estimation for B-VOPs - |
5 |
|
* |
6 |
|
* Copyright(C) 2002 Christoph Lampert <gruel@web.de> |
7 |
|
* 2002 Michael Militzer <michael@xvid.org> |
8 |
|
* 2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net> |
9 |
|
* |
10 |
|
* This program is free software ; you can redistribute it and/or modify |
11 |
|
* it under the terms of the GNU General Public License as published by |
12 |
|
* the Free Software Foundation ; either version 2 of the License, or |
13 |
|
* (at your option) any later version. |
14 |
|
* |
15 |
|
* This program is distributed in the hope that it will be useful, |
16 |
|
* but WITHOUT ANY WARRANTY ; without even the implied warranty of |
17 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
|
* GNU General Public License for more details. |
19 |
|
* |
20 |
|
* You should have received a copy of the GNU General Public License |
21 |
|
* along with this program ; if not, write to the Free Software |
22 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
|
* |
24 |
|
* $Id$ |
25 |
|
* |
26 |
|
****************************************************************************/ |
27 |
|
|
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 "../global.h" |
36 |
|
#include "../image/interpolate8x8.h" |
37 |
|
#include "estimation.h" |
38 |
|
#include "motion.h" |
39 |
|
#include "sad.h" |
40 |
|
#include "motion_inlines.h" |
41 |
|
|
42 |
|
|
43 |
|
static int32_t |
44 |
|
ChromaSAD2(const int fx, const int fy, const int bx, const int by, |
45 |
|
SearchData * const data) |
46 |
|
{ |
47 |
|
int sad; |
48 |
|
const uint32_t stride = data->iEdgedWidth/2; |
49 |
|
uint8_t *f_refu, *f_refv, *b_refu, *b_refv; |
50 |
|
|
51 |
|
const INTERPOLATE8X8_PTR interpolate8x8_halfpel[] = { |
52 |
|
NULL, |
53 |
|
interpolate8x8_halfpel_v, |
54 |
|
interpolate8x8_halfpel_h, |
55 |
|
interpolate8x8_halfpel_hv |
56 |
|
}; |
57 |
|
|
58 |
|
int offset = (fx>>1) + (fy>>1)*stride; |
59 |
|
int filter = ((fx & 1) << 1) | (fy & 1); |
60 |
|
|
61 |
|
if (filter != 0) { |
62 |
|
f_refu = data->RefQ; |
63 |
|
f_refv = data->RefQ + 8; |
64 |
|
interpolate8x8_halfpel[filter](f_refu, data->RefP[4] + offset, stride, data->rounding); |
65 |
|
interpolate8x8_halfpel[filter](f_refv, data->RefP[5] + offset, stride, data->rounding); |
66 |
|
} else { |
67 |
|
f_refu = (uint8_t*)data->RefP[4] + offset; |
68 |
|
f_refv = (uint8_t*)data->RefP[5] + offset; |
69 |
|
} |
70 |
|
|
71 |
|
offset = (bx>>1) + (by>>1)*stride; |
72 |
|
filter = ((bx & 1) << 1) | (by & 1); |
73 |
|
|
74 |
|
if (filter != 0) { |
75 |
|
b_refu = data->RefQ + 16; |
76 |
|
b_refv = data->RefQ + 24; |
77 |
|
interpolate8x8_halfpel[filter](b_refu, data->b_RefP[4] + offset, stride, data->rounding); |
78 |
|
interpolate8x8_halfpel[filter](b_refv, data->b_RefP[5] + offset, stride, data->rounding); |
79 |
|
} else { |
80 |
|
b_refu = (uint8_t*)data->b_RefP[4] + offset; |
81 |
|
b_refv = (uint8_t*)data->b_RefP[5] + offset; |
82 |
|
} |
83 |
|
|
84 |
|
sad = sad8bi(data->CurU, b_refu, f_refu, stride); |
85 |
|
sad += sad8bi(data->CurV, b_refv, f_refv, stride); |
86 |
|
|
87 |
|
return sad; |
88 |
|
} |
89 |
|
|
90 |
|
static void |
91 |
|
CheckCandidateInt(const int x, const int y, SearchData * const data, const unsigned int Direction) |
92 |
|
{ |
93 |
|
int32_t sad, xf, yf, xb, yb, xcf, ycf, xcb, ycb; |
94 |
|
uint32_t t; |
95 |
|
|
96 |
|
const uint8_t *ReferenceF, *ReferenceB; |
97 |
|
VECTOR *current; |
98 |
|
|
99 |
|
if ((x > data->max_dx) || (x < data->min_dx) || |
100 |
|
(y > data->max_dy) || (y < data->min_dy)) |
101 |
|
return; |
102 |
|
|
103 |
|
if (Direction == 1) { /* x and y mean forward vector */ |
104 |
|
VECTOR backward = data->qpel_precision ? data->currentQMV[1] : data->currentMV[1]; |
105 |
|
xb = backward.x; |
106 |
|
yb = backward.y; |
107 |
|
xf = x; yf = y; |
108 |
|
} else { /* x and y mean backward vector */ |
109 |
|
VECTOR forward = data->qpel_precision ? data->currentQMV[0] : data->currentMV[0]; |
110 |
|
xf = forward.x; |
111 |
|
yf = forward.y; |
112 |
|
xb = x; yb = y; |
113 |
|
} |
114 |
|
|
115 |
|
if (!data->qpel_precision) { |
116 |
|
ReferenceF = GetReference(xf, yf, data); |
117 |
|
ReferenceB = GetReferenceB(xb, yb, 1, data); |
118 |
|
current = data->currentMV + Direction - 1; |
119 |
|
xcf = xf; ycf = yf; |
120 |
|
xcb = xb; ycb = yb; |
121 |
|
} else { |
122 |
|
ReferenceF = xvid_me_interpolate16x16qpel(xf, yf, 0, data); |
123 |
|
current = data->currentQMV + Direction - 1; |
124 |
|
ReferenceB = xvid_me_interpolate16x16qpel(xb, yb, 1, data); |
125 |
|
xcf = xf/2; ycf = yf/2; |
126 |
|
xcb = xb/2; ycb = yb/2; |
127 |
|
} |
128 |
|
|
129 |
|
t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0) |
130 |
|
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
131 |
|
|
132 |
|
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
133 |
|
sad += (data->lambda16 * t * sad)>>10; |
134 |
|
|
135 |
|
if (data->chroma && sad < *data->iMinSAD) |
136 |
|
sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], |
137 |
|
(ycf >> 1) + roundtab_79[ycf & 0x3], |
138 |
|
(xcb >> 1) + roundtab_79[xcb & 0x3], |
139 |
|
(ycb >> 1) + roundtab_79[ycb & 0x3], data); |
140 |
|
|
141 |
|
if (sad < *(data->iMinSAD)) { |
142 |
|
*data->iMinSAD = sad; |
143 |
|
current->x = x; current->y = y; |
144 |
|
data->dir = Direction; |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
|
static void |
149 |
|
CheckCandidateDirect(const int x, const int y, SearchData * const data, const unsigned int Direction) |
150 |
|
{ |
151 |
|
int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0; |
152 |
|
uint32_t k; |
153 |
|
const uint8_t *ReferenceF; |
154 |
|
const uint8_t *ReferenceB; |
155 |
|
VECTOR mvs, b_mvs; |
156 |
|
|
157 |
|
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
158 |
|
|
159 |
|
for (k = 0; k < 4; k++) { |
160 |
|
mvs.x = data->directmvF[k].x + x; |
161 |
|
b_mvs.x = ((x == 0) ? |
162 |
|
data->directmvB[k].x |
163 |
|
: mvs.x - data->referencemv[k].x); |
164 |
|
|
165 |
|
mvs.y = data->directmvF[k].y + y; |
166 |
|
b_mvs.y = ((y == 0) ? |
167 |
|
data->directmvB[k].y |
168 |
|
: mvs.y - data->referencemv[k].y); |
169 |
|
|
170 |
|
if ((mvs.x > data->max_dx) || (mvs.x < data->min_dx) || |
171 |
|
(mvs.y > data->max_dy) || (mvs.y < data->min_dy) || |
172 |
|
(b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) || |
173 |
|
(b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) |
174 |
|
return; |
175 |
|
|
176 |
|
if (data->qpel) { |
177 |
|
xcf += mvs.x/2; ycf += mvs.y/2; |
178 |
|
xcb += b_mvs.x/2; ycb += b_mvs.y/2; |
179 |
|
} else { |
180 |
|
xcf += mvs.x; ycf += mvs.y; |
181 |
|
xcb += b_mvs.x; ycb += b_mvs.y; |
182 |
|
mvs.x *= 2; mvs.y *= 2; /* we move to qpel precision anyway */ |
183 |
|
b_mvs.x *= 2; b_mvs.y *= 2; |
184 |
|
} |
185 |
|
|
186 |
|
ReferenceF = xvid_me_interpolate8x8qpel(mvs.x, mvs.y, k, 0, data); |
187 |
|
ReferenceB = xvid_me_interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data); |
188 |
|
|
189 |
|
sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
190 |
|
ReferenceF, ReferenceB, data->iEdgedWidth); |
191 |
|
if (sad > *(data->iMinSAD)) return; |
192 |
|
} |
193 |
|
|
194 |
|
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
195 |
|
|
196 |
|
if (data->chroma && sad < *data->iMinSAD) |
197 |
|
sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
198 |
|
(ycf >> 3) + roundtab_76[ycf & 0xf], |
199 |
|
(xcb >> 3) + roundtab_76[xcb & 0xf], |
200 |
|
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
201 |
|
|
202 |
|
if (sad < *(data->iMinSAD)) { |
203 |
|
data->iMinSAD[0] = sad; |
204 |
|
data->currentMV->x = x; data->currentMV->y = y; |
205 |
|
data->dir = Direction; |
206 |
|
} |
207 |
|
} |
208 |
|
|
209 |
|
static void |
210 |
|
CheckCandidateDirectno4v(const int x, const int y, SearchData * const data, const unsigned int Direction) |
211 |
|
{ |
212 |
|
int32_t sad, xcf, ycf, xcb, ycb; |
213 |
|
const uint8_t *ReferenceF; |
214 |
|
const uint8_t *ReferenceB; |
215 |
|
VECTOR mvs, b_mvs; |
216 |
|
|
217 |
|
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
218 |
|
|
219 |
|
mvs.x = data->directmvF[0].x + x; |
220 |
|
b_mvs.x = ((x == 0) ? |
221 |
|
data->directmvB[0].x |
222 |
|
: mvs.x - data->referencemv[0].x); |
223 |
|
|
224 |
|
mvs.y = data->directmvF[0].y + y; |
225 |
|
b_mvs.y = ((y == 0) ? |
226 |
|
data->directmvB[0].y |
227 |
|
: mvs.y - data->referencemv[0].y); |
228 |
|
|
229 |
|
if ( (mvs.x > data->max_dx) || (mvs.x < data->min_dx) |
230 |
|
|| (mvs.y > data->max_dy) || (mvs.y < data->min_dy) |
231 |
|
|| (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) |
232 |
|
|| (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) return; |
233 |
|
|
234 |
|
if (data->qpel) { |
235 |
|
xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2); |
236 |
|
xcb = 4*(b_mvs.x/2); ycb = 4*(b_mvs.y/2); |
237 |
|
ReferenceF = xvid_me_interpolate16x16qpel(mvs.x, mvs.y, 0, data); |
238 |
|
ReferenceB = xvid_me_interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); |
239 |
|
} else { |
240 |
|
xcf = 4*mvs.x; ycf = 4*mvs.y; |
241 |
|
xcb = 4*b_mvs.x; ycb = 4*b_mvs.y; |
242 |
|
ReferenceF = GetReference(mvs.x, mvs.y, data); |
243 |
|
ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data); |
244 |
|
} |
245 |
|
|
246 |
|
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
247 |
|
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
248 |
|
|
249 |
|
if (data->chroma && sad < *data->iMinSAD) |
250 |
|
sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
251 |
|
(ycf >> 3) + roundtab_76[ycf & 0xf], |
252 |
|
(xcb >> 3) + roundtab_76[xcb & 0xf], |
253 |
|
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
254 |
|
|
255 |
|
if (sad < *(data->iMinSAD)) { |
256 |
|
*(data->iMinSAD) = sad; |
257 |
|
data->currentMV->x = x; data->currentMV->y = y; |
258 |
|
data->dir = Direction; |
259 |
|
} |
260 |
|
} |
261 |
|
|
262 |
|
void |
263 |
|
CheckCandidate16no4v(const int x, const int y, SearchData * const data, const unsigned int Direction) |
264 |
|
{ |
265 |
|
int32_t sad, xc, yc; |
266 |
|
const uint8_t * Reference; |
267 |
|
uint32_t t; |
268 |
|
VECTOR * current; |
269 |
|
|
270 |
|
if ( (x > data->max_dx) || ( x < data->min_dx) |
271 |
|
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
272 |
|
|
273 |
|
if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; /* non-zero even value */ |
274 |
|
|
275 |
|
if (data->qpel_precision) { /* x and y are in 1/4 precision */ |
276 |
|
Reference = xvid_me_interpolate16x16qpel(x, y, 0, data); |
277 |
|
current = data->currentQMV; |
278 |
|
xc = x/2; yc = y/2; |
279 |
|
} else { |
280 |
|
Reference = GetReference(x, y, data); |
281 |
|
current = data->currentMV; |
282 |
|
xc = x; yc = y; |
283 |
|
} |
284 |
|
t = d_mv_bits(x, y, data->predMV, data->iFcode, |
285 |
|
data->qpel^data->qpel_precision, data->rrv); |
286 |
|
|
287 |
|
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
288 |
|
sad += (data->lambda16 * t * sad)>>10; |
289 |
|
|
290 |
|
if (data->chroma && sad < *data->iMinSAD) |
291 |
|
sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
292 |
|
(yc >> 1) + roundtab_79[yc & 0x3], data); |
293 |
|
|
294 |
|
if (sad < *(data->iMinSAD)) { |
295 |
|
*(data->iMinSAD) = sad; |
296 |
|
current->x = x; current->y = y; |
297 |
|
data->dir = Direction; |
298 |
|
} |
299 |
|
} |
300 |
|
|
301 |
|
static __inline VECTOR |
302 |
|
ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode) |
303 |
|
{ |
304 |
|
/* the stupidiest function ever */ |
305 |
|
return (mode == MODE_FORWARD ? pMB->mvs[0] : pMB->b_mvs[0]); |
306 |
|
} |
307 |
|
|
308 |
|
static void __inline |
309 |
|
PreparePredictionsBF(VECTOR * const pmv, const int x, const int y, |
310 |
|
const uint32_t iWcount, |
311 |
|
const MACROBLOCK * const pMB, |
312 |
|
const uint32_t mode_curr) |
313 |
|
{ |
314 |
|
|
315 |
|
/* [0] is prediction */ |
316 |
|
pmv[0].x = EVEN(pmv[0].x); pmv[0].y = EVEN(pmv[0].y); |
317 |
|
|
318 |
|
pmv[1].x = pmv[1].y = 0; /* [1] is zero */ |
319 |
|
|
320 |
|
pmv[2] = ChoosePred(pMB, mode_curr); |
321 |
|
pmv[2].x = EVEN(pmv[2].x); pmv[2].y = EVEN(pmv[2].y); |
322 |
|
|
323 |
|
if ((y != 0)&&(x != (int)(iWcount+1))) { /* [3] top-right neighbour */ |
324 |
|
pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr); |
325 |
|
pmv[3].x = EVEN(pmv[3].x); pmv[3].y = EVEN(pmv[3].y); |
326 |
|
} else pmv[3].x = pmv[3].y = 0; |
327 |
|
|
328 |
|
if (y != 0) { |
329 |
|
pmv[4] = ChoosePred(pMB-iWcount, mode_curr); |
330 |
|
pmv[4].x = EVEN(pmv[4].x); pmv[4].y = EVEN(pmv[4].y); |
331 |
|
} else pmv[4].x = pmv[4].y = 0; |
332 |
|
|
333 |
|
if (x != 0) { |
334 |
|
pmv[5] = ChoosePred(pMB-1, mode_curr); |
335 |
|
pmv[5].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y); |
336 |
|
} else pmv[5].x = pmv[5].y = 0; |
337 |
|
|
338 |
|
if (x != 0 && y != 0) { |
339 |
|
pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr); |
340 |
|
pmv[6].x = EVEN(pmv[6].x); pmv[6].y = EVEN(pmv[6].y); |
341 |
|
} else pmv[6].x = pmv[6].y = 0; |
342 |
|
} |
343 |
|
|
344 |
|
|
345 |
|
/* search backward or forward */ |
346 |
|
static void |
347 |
|
SearchBF( const IMAGE * const pRef, |
348 |
|
const uint8_t * const pRefH, |
349 |
|
const uint8_t * const pRefV, |
350 |
|
const uint8_t * const pRefHV, |
351 |
|
const int x, const int y, |
352 |
|
const uint32_t MotionFlags, |
353 |
|
const uint32_t iFcode, |
354 |
|
const MBParam * const pParam, |
355 |
|
MACROBLOCK * const pMB, |
356 |
|
const VECTOR * const predMV, |
357 |
|
int32_t * const best_sad, |
358 |
|
const int32_t mode_current, |
359 |
|
SearchData * const Data) |
360 |
|
{ |
361 |
|
|
362 |
|
int i; |
363 |
|
VECTOR pmv[7]; |
364 |
|
MainSearchFunc *MainSearchPtr; |
365 |
|
*Data->iMinSAD = MV_MAX_ERROR; |
366 |
|
Data->iFcode = iFcode; |
367 |
|
Data->qpel_precision = 0; |
368 |
|
Data->chromaX = Data->chromaX = Data->chromaSAD = 256*4096; /* reset chroma-sad cache */ |
369 |
|
|
370 |
|
Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16; |
371 |
|
Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16; |
372 |
|
Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16; |
373 |
|
Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
374 |
|
Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
375 |
|
Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
376 |
|
|
377 |
|
Data->predMV = *predMV; |
378 |
|
|
379 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
380 |
|
pParam->width, pParam->height, iFcode - Data->qpel, 1, 0); |
381 |
|
|
382 |
|
pmv[0] = Data->predMV; |
383 |
|
if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; } |
384 |
|
|
385 |
|
PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current); |
386 |
|
|
387 |
|
Data->currentMV->x = Data->currentMV->y = 0; |
388 |
|
|
389 |
|
/* main loop. checking all predictions */ |
390 |
|
for (i = 0; i < 7; i++) |
391 |
|
if (!vector_repeats(pmv, i) ) |
392 |
|
CheckCandidate16no4v(pmv[i].x, pmv[i].y, Data, i); |
393 |
|
|
394 |
|
if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch; |
395 |
|
else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch; |
396 |
|
else MainSearchPtr = xvid_me_DiamondSearch; |
397 |
|
|
398 |
|
if (*Data->iMinSAD > 512) { |
399 |
|
unsigned int mask = make_mask(pmv, 7, Data->dir); |
400 |
|
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v); |
401 |
|
} |
402 |
|
|
403 |
|
xvid_me_SubpelRefine(Data, CheckCandidate16no4v); |
404 |
|
|
405 |
|
if (Data->qpel && *Data->iMinSAD < *best_sad + 300) { |
406 |
|
Data->currentQMV->x = 2*Data->currentMV->x; |
407 |
|
Data->currentQMV->y = 2*Data->currentMV->y; |
408 |
|
Data->qpel_precision = 1; |
409 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
410 |
|
pParam->width, pParam->height, iFcode, 2, 0); |
411 |
|
xvid_me_SubpelRefine(Data, CheckCandidate16no4v); |
412 |
|
} |
413 |
|
|
414 |
|
/* three bits are needed to code backward mode. four for forward */ |
415 |
|
|
416 |
|
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16; |
417 |
|
else *Data->iMinSAD += 3 * Data->lambda16; |
418 |
|
|
419 |
|
if (*Data->iMinSAD < *best_sad) { |
420 |
|
*best_sad = *Data->iMinSAD; |
421 |
|
pMB->mode = mode_current; |
422 |
|
if (Data->qpel) { |
423 |
|
pMB->pmvs[0].x = Data->currentQMV->x - predMV->x; |
424 |
|
pMB->pmvs[0].y = Data->currentQMV->y - predMV->y; |
425 |
|
if (mode_current == MODE_FORWARD) |
426 |
|
pMB->qmvs[0] = *Data->currentQMV; |
427 |
|
else |
428 |
|
pMB->b_qmvs[0] = *Data->currentQMV; |
429 |
|
} else { |
430 |
|
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
431 |
|
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
432 |
|
} |
433 |
|
if (mode_current == MODE_FORWARD) pMB->mvs[0] = *Data->currentMV; |
434 |
|
else pMB->b_mvs[0] = *Data->currentMV; |
435 |
|
} |
436 |
|
|
437 |
|
if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV; |
438 |
|
else *(Data->currentMV+1) = *Data->currentMV; /* we store currmv for interpolate search */ |
439 |
|
} |
440 |
|
|
441 |
|
static void |
442 |
|
SkipDecisionB(const IMAGE * const pCur, |
443 |
|
const IMAGE * const f_Ref, |
444 |
|
const IMAGE * const b_Ref, |
445 |
|
MACROBLOCK * const pMB, |
446 |
|
const uint32_t x, const uint32_t y, |
447 |
|
const SearchData * const Data) |
448 |
|
{ |
449 |
|
int dx = 0, dy = 0, b_dx = 0, b_dy = 0; |
450 |
|
int32_t sum; |
451 |
|
int k; |
452 |
|
const uint32_t stride = Data->iEdgedWidth/2; |
453 |
|
/* this is not full chroma compensation, only it's fullpel approximation. should work though */ |
454 |
|
|
455 |
|
for (k = 0; k < 4; k++) { |
456 |
|
dy += Data->directmvF[k].y >> Data->qpel; |
457 |
|
dx += Data->directmvF[k].x >> Data->qpel; |
458 |
|
b_dy += Data->directmvB[k].y >> Data->qpel; |
459 |
|
b_dx += Data->directmvB[k].x >> Data->qpel; |
460 |
|
} |
461 |
|
|
462 |
|
dy = (dy >> 3) + roundtab_76[dy & 0xf]; |
463 |
|
dx = (dx >> 3) + roundtab_76[dx & 0xf]; |
464 |
|
b_dy = (b_dy >> 3) + roundtab_76[b_dy & 0xf]; |
465 |
|
b_dx = (b_dx >> 3) + roundtab_76[b_dx & 0xf]; |
466 |
|
|
467 |
|
sum = sad8bi(pCur->u + 8 * x + 8 * y * stride, |
468 |
|
f_Ref->u + (y*8 + dy/2) * stride + x*8 + dx/2, |
469 |
|
b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
470 |
|
stride); |
471 |
|
|
472 |
|
if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ |
473 |
|
|
474 |
|
sum += sad8bi(pCur->v + 8*x + 8 * y * stride, |
475 |
|
f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2, |
476 |
|
b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
477 |
|
stride); |
478 |
|
|
479 |
|
if (sum < MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) { |
480 |
|
pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
481 |
|
for (k = 0; k < 4; k++) { |
482 |
|
pMB->qmvs[k] = pMB->mvs[k] = Data->directmvF[k]; |
483 |
|
pMB->b_qmvs[k] = pMB->b_mvs[k] = Data->directmvB[k]; |
484 |
|
} |
485 |
|
} |
486 |
|
} |
487 |
|
|
488 |
|
static uint32_t |
489 |
|
SearchDirect(const IMAGE * const f_Ref, |
490 |
|
const uint8_t * const f_RefH, |
491 |
|
const uint8_t * const f_RefV, |
492 |
|
const uint8_t * const f_RefHV, |
493 |
|
const IMAGE * const b_Ref, |
494 |
|
const uint8_t * const b_RefH, |
495 |
|
const uint8_t * const b_RefV, |
496 |
|
const uint8_t * const b_RefHV, |
497 |
|
const IMAGE * const pCur, |
498 |
|
const int x, const int y, |
499 |
|
const uint32_t MotionFlags, |
500 |
|
const int32_t TRB, const int32_t TRD, |
501 |
|
const MBParam * const pParam, |
502 |
|
MACROBLOCK * const pMB, |
503 |
|
const MACROBLOCK * const b_mb, |
504 |
|
int32_t * const best_sad, |
505 |
|
SearchData * const Data) |
506 |
|
|
507 |
|
{ |
508 |
|
int32_t skip_sad; |
509 |
|
int k = (x + Data->iEdgedWidth*y) * 16; |
510 |
|
MainSearchFunc *MainSearchPtr; |
511 |
|
CheckFunc * CheckCandidate; |
512 |
|
|
513 |
|
*Data->iMinSAD = 256*4096; |
514 |
|
Data->RefP[0] = f_Ref->y + k; |
515 |
|
Data->RefP[2] = f_RefH + k; |
516 |
|
Data->RefP[1] = f_RefV + k; |
517 |
|
Data->RefP[3] = f_RefHV + k; |
518 |
|
Data->b_RefP[0] = b_Ref->y + k; |
519 |
|
Data->b_RefP[2] = b_RefH + k; |
520 |
|
Data->b_RefP[1] = b_RefV + k; |
521 |
|
Data->b_RefP[3] = b_RefHV + k; |
522 |
|
Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
523 |
|
Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
524 |
|
Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
525 |
|
Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
526 |
|
|
527 |
|
k = Data->qpel ? 4 : 2; |
528 |
|
Data->max_dx = k * (pParam->width - x * 16); |
529 |
|
Data->max_dy = k * (pParam->height - y * 16); |
530 |
|
Data->min_dx = -k * (16 + x * 16); |
531 |
|
Data->min_dy = -k * (16 + y * 16); |
532 |
|
|
533 |
|
Data->referencemv = Data->qpel ? b_mb->qmvs : b_mb->mvs; |
534 |
|
Data->qpel_precision = 0; |
535 |
|
|
536 |
|
for (k = 0; k < 4; k++) { |
537 |
|
pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD); |
538 |
|
pMB->b_mvs[k].x = Data->directmvB[k].x = ((TRB - TRD) * Data->referencemv[k].x) / TRD; |
539 |
|
pMB->mvs[k].y = Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD); |
540 |
|
pMB->b_mvs[k].y = Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD; |
541 |
|
|
542 |
|
if ( (pMB->b_mvs[k].x > Data->max_dx) | (pMB->b_mvs[k].x < Data->min_dx) |
543 |
|
| (pMB->b_mvs[k].y > Data->max_dy) | (pMB->b_mvs[k].y < Data->min_dy) ) { |
544 |
|
|
545 |
|
*best_sad = 256*4096; /* in that case, we won't use direct mode */ |
546 |
|
pMB->mode = MODE_DIRECT; /* just to make sure it doesn't say "MODE_DIRECT_NONE_MV" */ |
547 |
|
pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0; |
548 |
|
return 256*4096; |
549 |
|
} |
550 |
|
if (b_mb->mode != MODE_INTER4V) { |
551 |
|
pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mvs[0]; |
552 |
|
pMB->b_mvs[1] = pMB->b_mvs[2] = pMB->b_mvs[3] = pMB->b_mvs[0]; |
553 |
|
Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0]; |
554 |
|
Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0]; |
555 |
|
break; |
556 |
|
} |
557 |
|
} |
558 |
|
|
559 |
|
CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v; |
560 |
|
|
561 |
|
CheckCandidate(0, 0, Data, 255); |
562 |
|
|
563 |
|
/* initial (fast) skip decision */ |
564 |
|
if (*Data->iMinSAD < (int)Data->iQuant * INITIAL_SKIP_THRESH * (Data->chroma?3:2)) { |
565 |
|
/* possible skip */ |
566 |
|
if (Data->chroma) { |
567 |
|
pMB->mode = MODE_DIRECT_NONE_MV; |
568 |
|
return *Data->iMinSAD; /* skip. */ |
569 |
|
} else { |
570 |
|
SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data); |
571 |
|
if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; /* skip. */ |
572 |
|
} |
573 |
|
} |
574 |
|
|
575 |
|
*Data->iMinSAD += Data->lambda16; |
576 |
|
skip_sad = *Data->iMinSAD; |
577 |
|
|
578 |
|
/* |
579 |
|
* DIRECT MODE DELTA VECTOR SEARCH. |
580 |
|
* This has to be made more effective, but at the moment I'm happy it's running at all |
581 |
|
*/ |
582 |
|
|
583 |
|
if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch; |
584 |
|
else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch; |
585 |
|
else MainSearchPtr = xvid_me_DiamondSearch; |
586 |
|
|
587 |
|
MainSearchPtr(0, 0, Data, 255, CheckCandidate); |
588 |
|
|
589 |
|
xvid_me_SubpelRefine(Data, CheckCandidate); |
590 |
|
|
591 |
|
*best_sad = *Data->iMinSAD; |
592 |
|
|
593 |
|
if (Data->qpel || b_mb->mode == MODE_INTER4V) pMB->mode = MODE_DIRECT; |
594 |
|
else pMB->mode = MODE_DIRECT_NO4V; /* for faster compensation */ |
595 |
|
|
596 |
|
pMB->pmvs[3] = *Data->currentMV; |
597 |
|
|
598 |
|
for (k = 0; k < 4; k++) { |
599 |
|
pMB->mvs[k].x = Data->directmvF[k].x + Data->currentMV->x; |
600 |
|
pMB->b_mvs[k].x = ( (Data->currentMV->x == 0) |
601 |
|
? Data->directmvB[k].x |
602 |
|
:pMB->mvs[k].x - Data->referencemv[k].x); |
603 |
|
pMB->mvs[k].y = (Data->directmvF[k].y + Data->currentMV->y); |
604 |
|
pMB->b_mvs[k].y = ((Data->currentMV->y == 0) |
605 |
|
? Data->directmvB[k].y |
606 |
|
: pMB->mvs[k].y - Data->referencemv[k].y); |
607 |
|
if (Data->qpel) { |
608 |
|
pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2; |
609 |
|
pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2; |
610 |
|
pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2; |
611 |
|
pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2; |
612 |
|
} |
613 |
|
|
614 |
|
if (b_mb->mode != MODE_INTER4V) { |
615 |
|
pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0]; |
616 |
|
pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0]; |
617 |
|
pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0]; |
618 |
|
pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0]; |
619 |
|
break; |
620 |
|
} |
621 |
|
} |
622 |
|
return skip_sad; |
623 |
|
} |
624 |
|
|
625 |
|
|
626 |
|
static void set_range(int * range, SearchData * Data) |
627 |
|
{ |
628 |
|
Data->min_dx = range[0]; |
629 |
|
Data->max_dx = range[1]; |
630 |
|
Data->min_dy = range[2]; |
631 |
|
Data->max_dy = range[3]; |
632 |
|
} |
633 |
|
|
634 |
|
static void |
635 |
|
SubpelRefine_dir(SearchData * const data, CheckFunc * const CheckCandidate, const int dir) |
636 |
|
{ |
637 |
|
/* Do a half-pel or q-pel refinement */ |
638 |
|
const VECTOR centerMV = data->qpel_precision ? |
639 |
|
data->currentQMV[dir-1] : data->currentMV[dir-1]; |
640 |
|
|
641 |
|
CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, dir); |
642 |
|
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, dir); |
643 |
|
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y, dir); |
644 |
|
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y + 1, dir); |
645 |
|
CHECK_CANDIDATE(centerMV.x, centerMV.y + 1, dir); |
646 |
|
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y + 1, dir); |
647 |
|
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y, dir); |
648 |
|
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y - 1, dir); |
649 |
|
} |
650 |
|
|
651 |
|
static void |
652 |
|
SearchInterpolate(const IMAGE * const f_Ref, |
653 |
|
const uint8_t * const f_RefH, |
654 |
|
const uint8_t * const f_RefV, |
655 |
|
const uint8_t * const f_RefHV, |
656 |
|
const IMAGE * const b_Ref, |
657 |
|
const uint8_t * const b_RefH, |
658 |
|
const uint8_t * const b_RefV, |
659 |
|
const uint8_t * const b_RefHV, |
660 |
|
const int x, const int y, |
661 |
|
const uint32_t fcode, |
662 |
|
const uint32_t bcode, |
663 |
|
const uint32_t MotionFlags, |
664 |
|
const MBParam * const pParam, |
665 |
|
const VECTOR * const f_predMV, |
666 |
|
const VECTOR * const b_predMV, |
667 |
|
MACROBLOCK * const pMB, |
668 |
|
int32_t * const best_sad, |
669 |
|
SearchData * const Data) |
670 |
|
|
671 |
|
{ |
672 |
|
int i, j; |
673 |
|
int b_range[4], f_range[4]; |
674 |
|
|
675 |
|
Data->qpel_precision = 0; |
676 |
|
*Data->iMinSAD = 4096*256; |
677 |
|
Data->iFcode = fcode; Data->bFcode = bcode; |
678 |
|
|
679 |
|
i = (x + y * Data->iEdgedWidth) * 16; |
680 |
|
|
681 |
|
Data->RefP[0] = f_Ref->y + i; |
682 |
|
Data->RefP[2] = f_RefH + i; |
683 |
|
Data->RefP[1] = f_RefV + i; |
684 |
|
Data->RefP[3] = f_RefHV + i; |
685 |
|
Data->b_RefP[0] = b_Ref->y + i; |
686 |
|
Data->b_RefP[2] = b_RefH + i; |
687 |
|
Data->b_RefP[1] = b_RefV + i; |
688 |
|
Data->b_RefP[3] = b_RefHV + i; |
689 |
|
Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
690 |
|
Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
691 |
|
Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
692 |
|
Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
693 |
|
|
694 |
|
Data->predMV = *f_predMV; |
695 |
|
Data->bpredMV = *b_predMV; |
696 |
|
|
697 |
|
Data->currentMV[0] = Data->currentMV[2]; /* forward search left its vector here */ |
698 |
|
|
699 |
|
get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, fcode - Data->qpel, 1, 0); |
700 |
|
get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, bcode - Data->qpel, 1, 0); |
701 |
|
|
702 |
|
if (Data->currentMV[0].x > f_range[1]) Data->currentMV[0].x = f_range[1]; |
703 |
|
if (Data->currentMV[0].x < f_range[0]) Data->currentMV[0].x = f_range[0]; |
704 |
|
if (Data->currentMV[0].y > f_range[3]) Data->currentMV[0].y = f_range[3]; |
705 |
|
if (Data->currentMV[0].y < f_range[2]) Data->currentMV[0].y = f_range[2]; |
706 |
|
|
707 |
|
if (Data->currentMV[1].x > b_range[1]) Data->currentMV[1].x = b_range[1]; |
708 |
|
if (Data->currentMV[1].x < b_range[0]) Data->currentMV[1].x = b_range[0]; |
709 |
|
if (Data->currentMV[1].y > b_range[3]) Data->currentMV[1].y = b_range[3]; |
710 |
|
if (Data->currentMV[1].y < b_range[2]) Data->currentMV[1].y = b_range[2]; |
711 |
|
|
712 |
|
set_range(f_range, Data); |
713 |
|
|
714 |
|
CheckCandidateInt(Data->currentMV[0].x, Data->currentMV[0].y, Data, 1); |
715 |
|
|
716 |
|
/* diamond */ |
717 |
|
do { |
718 |
|
Data->dir = 0; |
719 |
|
/* forward MV moves */ |
720 |
|
i = Data->currentMV[0].x; j = Data->currentMV[0].y; |
721 |
|
|
722 |
|
CheckCandidateInt(i + 1, j, Data, 1); |
723 |
|
CheckCandidateInt(i, j + 1, Data, 1); |
724 |
|
CheckCandidateInt(i - 1, j, Data, 1); |
725 |
|
CheckCandidateInt(i, j - 1, Data, 1); |
726 |
|
|
727 |
|
/* backward MV moves */ |
728 |
|
set_range(b_range, Data); |
729 |
|
i = Data->currentMV[1].x; j = Data->currentMV[1].y; |
730 |
|
|
731 |
|
CheckCandidateInt(i + 1, j, Data, 2); |
732 |
|
CheckCandidateInt(i, j + 1, Data, 2); |
733 |
|
CheckCandidateInt(i - 1, j, Data, 2); |
734 |
|
CheckCandidateInt(i, j - 1, Data, 2); |
735 |
|
|
736 |
|
set_range(f_range, Data); |
737 |
|
|
738 |
|
} while (Data->dir != 0); |
739 |
|
|
740 |
|
/* qpel refinement */ |
741 |
|
if (Data->qpel) { |
742 |
|
if (*Data->iMinSAD > *best_sad + 500) return; |
743 |
|
Data->qpel_precision = 1; |
744 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, fcode, 2, 0); |
745 |
|
|
746 |
|
Data->currentQMV[0].x = 2 * Data->currentMV[0].x; |
747 |
|
Data->currentQMV[0].y = 2 * Data->currentMV[0].y; |
748 |
|
Data->currentQMV[1].x = 2 * Data->currentMV[1].x; |
749 |
|
Data->currentQMV[1].y = 2 * Data->currentMV[1].y; |
750 |
|
SubpelRefine_dir(Data, CheckCandidateInt, 1); |
751 |
|
if (*Data->iMinSAD > *best_sad + 300) return; |
752 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0); |
753 |
|
SubpelRefine_dir(Data, CheckCandidateInt, 2); |
754 |
|
} |
755 |
|
|
756 |
|
*Data->iMinSAD += (2+3) * Data->lambda16; /* two bits are needed to code interpolate mode. */ |
757 |
|
|
758 |
|
if (*Data->iMinSAD < *best_sad) { |
759 |
|
*best_sad = *Data->iMinSAD; |
760 |
|
pMB->mvs[0] = Data->currentMV[0]; |
761 |
|
pMB->b_mvs[0] = Data->currentMV[1]; |
762 |
|
pMB->mode = MODE_INTERPOLATE; |
763 |
|
if (Data->qpel) { |
764 |
|
pMB->qmvs[0] = Data->currentQMV[0]; |
765 |
|
pMB->b_qmvs[0] = Data->currentQMV[1]; |
766 |
|
pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x; |
767 |
|
pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y; |
768 |
|
pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x; |
769 |
|
pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y; |
770 |
|
} else { |
771 |
|
pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x; |
772 |
|
pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y; |
773 |
|
pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x; |
774 |
|
pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y; |
775 |
|
} |
776 |
|
} |
777 |
|
} |
778 |
|
|
779 |
|
void |
780 |
|
MotionEstimationBVOP(MBParam * const pParam, |
781 |
|
FRAMEINFO * const frame, |
782 |
|
const int32_t time_bp, |
783 |
|
const int32_t time_pp, |
784 |
|
/* forward (past) reference */ |
785 |
|
const MACROBLOCK * const f_mbs, |
786 |
|
const IMAGE * const f_ref, |
787 |
|
const IMAGE * const f_refH, |
788 |
|
const IMAGE * const f_refV, |
789 |
|
const IMAGE * const f_refHV, |
790 |
|
/* backward (future) reference */ |
791 |
|
const FRAMEINFO * const b_reference, |
792 |
|
const IMAGE * const b_ref, |
793 |
|
const IMAGE * const b_refH, |
794 |
|
const IMAGE * const b_refV, |
795 |
|
const IMAGE * const b_refHV) |
796 |
|
{ |
797 |
|
uint32_t i, j; |
798 |
|
int32_t best_sad; |
799 |
|
uint32_t skip_sad; |
800 |
|
|
801 |
|
const MACROBLOCK * const b_mbs = b_reference->mbs; |
802 |
|
|
803 |
|
VECTOR f_predMV, b_predMV; |
804 |
|
|
805 |
|
const int32_t TRB = time_pp - time_bp; |
806 |
|
const int32_t TRD = time_pp; |
807 |
|
|
808 |
|
/* some pre-inintialized data for the rest of the search */ |
809 |
|
|
810 |
|
SearchData Data; |
811 |
|
memset(&Data, 0, sizeof(SearchData)); |
812 |
|
|
813 |
|
Data.iEdgedWidth = pParam->edged_width; |
814 |
|
Data.lambda16 = xvid_me_lambda_vec16[MAX(frame->quant-2, 2)]; |
815 |
|
Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0; |
816 |
|
Data.rounding = 0; |
817 |
|
Data.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP; |
818 |
|
Data.iQuant = frame->quant; |
819 |
|
|
820 |
|
Data.RefQ = f_refV->u; /* a good place, also used in MC (for similar purpose) */ |
821 |
|
|
822 |
|
/* note: i==horizontal, j==vertical */ |
823 |
|
for (j = 0; j < pParam->mb_height; j++) { |
824 |
|
|
825 |
|
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
826 |
|
|
827 |
|
for (i = 0; i < pParam->mb_width; i++) { |
828 |
|
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
829 |
|
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
830 |
|
|
831 |
|
/* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */ |
832 |
|
if (b_reference->coding_type != S_VOP) |
833 |
|
if (b_mb->mode == MODE_NOT_CODED) { |
834 |
|
pMB->mode = MODE_NOT_CODED; |
835 |
|
pMB->mvs[0] = pMB->b_mvs[0] = zeroMV; |
836 |
|
continue; |
837 |
|
} |
838 |
|
|
839 |
|
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
840 |
|
Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8; |
841 |
|
Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8; |
842 |
|
|
843 |
|
/* direct search comes first, because it (1) checks for SKIP-mode |
844 |
|
and (2) sets very good predictions for forward and backward search */ |
845 |
|
skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
846 |
|
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
847 |
|
&frame->image, |
848 |
|
i, j, |
849 |
|
frame->motion_flags, |
850 |
|
TRB, TRD, |
851 |
|
pParam, |
852 |
|
pMB, b_mb, |
853 |
|
&best_sad, |
854 |
|
&Data); |
855 |
|
|
856 |
|
if (pMB->mode == MODE_DIRECT_NONE_MV) continue; |
857 |
|
|
858 |
|
/* forward search */ |
859 |
|
SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
860 |
|
i, j, |
861 |
|
frame->motion_flags, |
862 |
|
frame->fcode, pParam, |
863 |
|
pMB, &f_predMV, &best_sad, |
864 |
|
MODE_FORWARD, &Data); |
865 |
|
|
866 |
|
/* backward search */ |
867 |
|
SearchBF(b_ref, b_refH->y, b_refV->y, b_refHV->y, |
868 |
|
i, j, |
869 |
|
frame->motion_flags, |
870 |
|
frame->bcode, pParam, |
871 |
|
pMB, &b_predMV, &best_sad, |
872 |
|
MODE_BACKWARD, &Data); |
873 |
|
|
874 |
|
/* interpolate search comes last, because it uses data from forward and backward as prediction */ |
875 |
|
SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
876 |
|
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
877 |
|
i, j, |
878 |
|
frame->fcode, frame->bcode, |
879 |
|
frame->motion_flags, |
880 |
|
pParam, |
881 |
|
&f_predMV, &b_predMV, |
882 |
|
pMB, &best_sad, |
883 |
|
&Data); |
884 |
|
|
885 |
|
/* final skip decision */ |
886 |
|
if ( (skip_sad < Data.iQuant * MAX_SAD00_FOR_SKIP * 2) |
887 |
|
&& ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) |
888 |
|
SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data); |
889 |
|
|
890 |
|
switch (pMB->mode) { |
891 |
|
case MODE_FORWARD: |
892 |
|
f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0]; |
893 |
|
break; |
894 |
|
case MODE_BACKWARD: |
895 |
|
b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0]; |
896 |
|
break; |
897 |
|
case MODE_INTERPOLATE: |
898 |
|
f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0]; |
899 |
|
b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0]; |
900 |
|
break; |
901 |
|
default: |
902 |
|
break; |
903 |
|
} |
904 |
|
} |
905 |
|
} |
906 |
|
} |
907 |
|
|