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