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 |
|
CheckCandidateInt_qpel(const int x, const int y, SearchData * const data, const unsigned int Direction) |
149 |
|
{ |
150 |
|
int32_t sad, xf, yf, xb, yb, xcf, ycf, xcb, ycb; |
151 |
|
uint32_t t; |
152 |
|
|
153 |
|
const uint8_t *ReferenceF, *ReferenceB; |
154 |
|
VECTOR *current; |
155 |
|
|
156 |
|
if ((x > data->max_dx) || (x < data->min_dx) || |
157 |
|
(y > data->max_dy) || (y < data->min_dy)) |
158 |
|
return; |
159 |
|
|
160 |
|
if (Direction == 1) { /* x and y mean forward vector */ |
161 |
|
VECTOR backward = data->qpel_precision ? data->currentQMV[1] : data->currentMV[1]; |
162 |
|
xb = backward.x; |
163 |
|
yb = backward.y; |
164 |
|
xf = x; yf = y; |
165 |
|
} else { /* x and y mean backward vector */ |
166 |
|
VECTOR forward = data->qpel_precision ? data->currentQMV[0] : data->currentMV[0]; |
167 |
|
xf = forward.x; |
168 |
|
yf = forward.y; |
169 |
|
xb = x; yb = y; |
170 |
|
} |
171 |
|
|
172 |
|
ReferenceF = xvid_me_interpolate16x16qpel(xf, yf, 0, data); |
173 |
|
current = data->currentQMV + Direction - 1; |
174 |
|
ReferenceB = xvid_me_interpolate16x16qpel(xb, yb, 1, data); |
175 |
|
xcf = xf/2; ycf = yf/2; |
176 |
|
xcb = xb/2; ycb = yb/2; |
177 |
|
|
178 |
|
t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0) |
179 |
|
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
180 |
|
|
181 |
|
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
182 |
|
sad += (data->lambda16 * t * sad)>>10; |
183 |
|
|
184 |
|
if (data->chroma && sad < *data->iMinSAD) |
185 |
|
sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], |
186 |
|
(ycf >> 1) + roundtab_79[ycf & 0x3], |
187 |
|
(xcb >> 1) + roundtab_79[xcb & 0x3], |
188 |
|
(ycb >> 1) + roundtab_79[ycb & 0x3], data); |
189 |
|
|
190 |
|
if (sad < *(data->iMinSAD)) { |
191 |
|
*data->iMinSAD = sad; |
192 |
|
current->x = x; current->y = y; |
193 |
|
data->dir = Direction; |
194 |
|
} |
195 |
|
|
196 |
|
if (sad < *(data->iMinSAD)) { |
197 |
|
data->iMinSAD2 = *(data->iMinSAD); |
198 |
|
data->currentQMV2.x = current->x; |
199 |
|
data->currentQMV2.y = current->y; |
200 |
|
|
201 |
|
*data->iMinSAD = sad; |
202 |
|
current->x = x; current->y = y; |
203 |
|
} else if (sad < data->iMinSAD2) { |
204 |
|
data->iMinSAD2 = sad; |
205 |
|
data->currentQMV2.x = x; data->currentQMV2.y = y; |
206 |
|
} |
207 |
|
} |
208 |
|
|
209 |
|
static void |
210 |
|
CheckCandidateDirect(const int x, const int y, SearchData * const data, const unsigned int Direction) |
211 |
|
{ |
212 |
|
int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0; |
213 |
|
uint32_t k; |
214 |
|
const uint8_t *ReferenceF; |
215 |
|
const uint8_t *ReferenceB; |
216 |
|
VECTOR mvs, b_mvs; |
217 |
|
|
218 |
|
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
219 |
|
|
220 |
|
for (k = 0; k < 4; k++) { |
221 |
|
mvs.x = data->directmvF[k].x + x; |
222 |
|
b_mvs.x = ((x == 0) ? |
223 |
|
data->directmvB[k].x |
224 |
|
: mvs.x - data->referencemv[k].x); |
225 |
|
|
226 |
|
mvs.y = data->directmvF[k].y + y; |
227 |
|
b_mvs.y = ((y == 0) ? |
228 |
|
data->directmvB[k].y |
229 |
|
: mvs.y - data->referencemv[k].y); |
230 |
|
|
231 |
|
if ((mvs.x > data->max_dx) || (mvs.x < data->min_dx) || |
232 |
|
(mvs.y > data->max_dy) || (mvs.y < data->min_dy) || |
233 |
|
(b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) || |
234 |
|
(b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) |
235 |
|
return; |
236 |
|
|
237 |
|
if (data->qpel) { |
238 |
|
xcf += mvs.x/2; ycf += mvs.y/2; |
239 |
|
xcb += b_mvs.x/2; ycb += b_mvs.y/2; |
240 |
|
} else { |
241 |
|
xcf += mvs.x; ycf += mvs.y; |
242 |
|
xcb += b_mvs.x; ycb += b_mvs.y; |
243 |
|
mvs.x *= 2; mvs.y *= 2; /* we move to qpel precision anyway */ |
244 |
|
b_mvs.x *= 2; b_mvs.y *= 2; |
245 |
|
} |
246 |
|
|
247 |
|
ReferenceF = xvid_me_interpolate8x8qpel(mvs.x, mvs.y, k, 0, data); |
248 |
|
ReferenceB = xvid_me_interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data); |
249 |
|
|
250 |
|
sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
251 |
|
ReferenceF, ReferenceB, data->iEdgedWidth); |
252 |
|
if (sad > *(data->iMinSAD)) return; |
253 |
|
} |
254 |
|
|
255 |
|
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
256 |
|
|
257 |
|
if (data->chroma && sad < *data->iMinSAD) |
258 |
|
sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
259 |
|
(ycf >> 3) + roundtab_76[ycf & 0xf], |
260 |
|
(xcb >> 3) + roundtab_76[xcb & 0xf], |
261 |
|
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
262 |
|
|
263 |
|
if (sad < *(data->iMinSAD)) { |
264 |
|
data->iMinSAD[0] = sad; |
265 |
|
data->currentMV->x = x; data->currentMV->y = y; |
266 |
|
data->dir = Direction; |
267 |
|
} |
268 |
|
} |
269 |
|
|
270 |
|
static void |
271 |
|
CheckCandidateDirectno4v(const int x, const int y, SearchData * const data, const unsigned int Direction) |
272 |
|
{ |
273 |
|
int32_t sad, xcf, ycf, xcb, ycb; |
274 |
|
const uint8_t *ReferenceF; |
275 |
|
const uint8_t *ReferenceB; |
276 |
|
VECTOR mvs, b_mvs; |
277 |
|
|
278 |
|
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
279 |
|
|
280 |
|
mvs.x = data->directmvF[0].x + x; |
281 |
|
b_mvs.x = ((x == 0) ? |
282 |
|
data->directmvB[0].x |
283 |
|
: mvs.x - data->referencemv[0].x); |
284 |
|
|
285 |
|
mvs.y = data->directmvF[0].y + y; |
286 |
|
b_mvs.y = ((y == 0) ? |
287 |
|
data->directmvB[0].y |
288 |
|
: mvs.y - data->referencemv[0].y); |
289 |
|
|
290 |
|
if ( (mvs.x > data->max_dx) || (mvs.x < data->min_dx) |
291 |
|
|| (mvs.y > data->max_dy) || (mvs.y < data->min_dy) |
292 |
|
|| (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) |
293 |
|
|| (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) return; |
294 |
|
|
295 |
|
if (data->qpel) { |
296 |
|
xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2); |
297 |
|
xcb = 4*(b_mvs.x/2); ycb = 4*(b_mvs.y/2); |
298 |
|
ReferenceF = xvid_me_interpolate16x16qpel(mvs.x, mvs.y, 0, data); |
299 |
|
ReferenceB = xvid_me_interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); |
300 |
|
} else { |
301 |
|
xcf = 4*mvs.x; ycf = 4*mvs.y; |
302 |
|
xcb = 4*b_mvs.x; ycb = 4*b_mvs.y; |
303 |
|
ReferenceF = GetReference(mvs.x, mvs.y, data); |
304 |
|
ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data); |
305 |
|
} |
306 |
|
|
307 |
|
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
308 |
|
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
309 |
|
|
310 |
|
if (data->chroma && sad < *data->iMinSAD) |
311 |
|
sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
312 |
|
(ycf >> 3) + roundtab_76[ycf & 0xf], |
313 |
|
(xcb >> 3) + roundtab_76[xcb & 0xf], |
314 |
|
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
315 |
|
|
316 |
|
if (sad < *(data->iMinSAD)) { |
317 |
|
*(data->iMinSAD) = sad; |
318 |
|
data->currentMV->x = x; data->currentMV->y = y; |
319 |
|
data->dir = Direction; |
320 |
|
} |
321 |
|
} |
322 |
|
|
323 |
|
void |
324 |
|
CheckCandidate16no4v(const int x, const int y, SearchData * const data, const unsigned int Direction) |
325 |
|
{ |
326 |
|
int32_t sad, xc, yc; |
327 |
|
const uint8_t * Reference; |
328 |
|
uint32_t t; |
329 |
|
VECTOR * current; |
330 |
|
|
331 |
|
if ( (x > data->max_dx) || ( x < data->min_dx) |
332 |
|
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
333 |
|
|
334 |
|
if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; /* non-zero even value */ |
335 |
|
|
336 |
|
if (data->qpel_precision) { /* x and y are in 1/4 precision */ |
337 |
|
Reference = xvid_me_interpolate16x16qpel(x, y, 0, data); |
338 |
|
current = data->currentQMV; |
339 |
|
xc = x/2; yc = y/2; |
340 |
|
} else { |
341 |
|
Reference = GetReference(x, y, data); |
342 |
|
current = data->currentMV; |
343 |
|
xc = x; yc = y; |
344 |
|
} |
345 |
|
t = d_mv_bits(x, y, data->predMV, data->iFcode, |
346 |
|
data->qpel^data->qpel_precision, data->rrv); |
347 |
|
|
348 |
|
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
349 |
|
sad += (data->lambda16 * t * sad)>>10; |
350 |
|
|
351 |
|
if (data->chroma && sad < *data->iMinSAD) |
352 |
|
sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
353 |
|
(yc >> 1) + roundtab_79[yc & 0x3], data); |
354 |
|
|
355 |
|
if (sad < *(data->iMinSAD)) { |
356 |
|
*(data->iMinSAD) = sad; |
357 |
|
current->x = x; current->y = y; |
358 |
|
data->dir = Direction; |
359 |
|
} |
360 |
|
} |
361 |
|
|
362 |
|
void |
363 |
|
CheckCandidate16no4v_qpel(const int x, const int y, SearchData * const data, const unsigned int Direction) |
364 |
|
{ |
365 |
|
int32_t sad, xc, yc; |
366 |
|
const uint8_t * Reference; |
367 |
|
uint32_t t; |
368 |
|
|
369 |
|
if ( (x > data->max_dx) || ( x < data->min_dx) |
370 |
|
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
371 |
|
|
372 |
|
if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; /* non-zero even value */ |
373 |
|
|
374 |
|
Reference = xvid_me_interpolate16x16qpel(x, y, 0, data); |
375 |
|
|
376 |
|
xc = x/2; yc = y/2; |
377 |
|
t = d_mv_bits(x, y, data->predMV, data->iFcode, |
378 |
|
data->qpel^data->qpel_precision, data->rrv); |
379 |
|
|
380 |
|
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
381 |
|
sad += (data->lambda16 * t * sad)>>10; |
382 |
|
|
383 |
|
if (data->chroma && sad < *data->iMinSAD) |
384 |
|
sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
385 |
|
(yc >> 1) + roundtab_79[yc & 0x3], data); |
386 |
|
|
387 |
|
if (sad < *(data->iMinSAD)) { |
388 |
|
data->iMinSAD2 = *(data->iMinSAD); |
389 |
|
data->currentQMV2.x = data->currentQMV->x; |
390 |
|
data->currentQMV2.y = data->currentQMV->y; |
391 |
|
|
392 |
|
data->iMinSAD[0] = sad; |
393 |
|
data->currentQMV[0].x = x; data->currentQMV[0].y = y; |
394 |
|
} else if (sad < data->iMinSAD2) { |
395 |
|
data->iMinSAD2 = sad; |
396 |
|
data->currentQMV2.x = x; data->currentQMV2.y = y; |
397 |
|
} |
398 |
|
} |
399 |
|
|
400 |
|
static __inline VECTOR |
401 |
|
ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode) |
402 |
|
{ |
403 |
|
/* the stupidiest function ever */ |
404 |
|
return (mode == MODE_FORWARD ? pMB->mvs[0] : pMB->b_mvs[0]); |
405 |
|
} |
406 |
|
|
407 |
|
static void __inline |
408 |
|
PreparePredictionsBF(VECTOR * const pmv, const int x, const int y, |
409 |
|
const uint32_t iWcount, |
410 |
|
const MACROBLOCK * const pMB, |
411 |
|
const uint32_t mode_curr) |
412 |
|
{ |
413 |
|
|
414 |
|
/* [0] is prediction */ |
415 |
|
pmv[0].x = EVEN(pmv[0].x); pmv[0].y = EVEN(pmv[0].y); |
416 |
|
|
417 |
|
pmv[1].x = pmv[1].y = 0; /* [1] is zero */ |
418 |
|
|
419 |
|
pmv[2] = ChoosePred(pMB, mode_curr); |
420 |
|
pmv[2].x = EVEN(pmv[2].x); pmv[2].y = EVEN(pmv[2].y); |
421 |
|
|
422 |
|
if ((y != 0)&&(x != (int)(iWcount+1))) { /* [3] top-right neighbour */ |
423 |
|
pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr); |
424 |
|
pmv[3].x = EVEN(pmv[3].x); pmv[3].y = EVEN(pmv[3].y); |
425 |
|
} else pmv[3].x = pmv[3].y = 0; |
426 |
|
|
427 |
|
if (y != 0) { |
428 |
|
pmv[4] = ChoosePred(pMB-iWcount, mode_curr); |
429 |
|
pmv[4].x = EVEN(pmv[4].x); pmv[4].y = EVEN(pmv[4].y); |
430 |
|
} else pmv[4].x = pmv[4].y = 0; |
431 |
|
|
432 |
|
if (x != 0) { |
433 |
|
pmv[5] = ChoosePred(pMB-1, mode_curr); |
434 |
|
pmv[5].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y); |
435 |
|
} else pmv[5].x = pmv[5].y = 0; |
436 |
|
|
437 |
|
if (x != 0 && y != 0) { |
438 |
|
pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr); |
439 |
|
pmv[6].x = EVEN(pmv[6].x); pmv[6].y = EVEN(pmv[6].y); |
440 |
|
} else pmv[6].x = pmv[6].y = 0; |
441 |
|
} |
442 |
|
|
443 |
|
|
444 |
|
/* search backward or forward */ |
445 |
|
static void |
446 |
|
SearchBF( const IMAGE * const pRef, |
447 |
|
const uint8_t * const pRefH, |
448 |
|
const uint8_t * const pRefV, |
449 |
|
const uint8_t * const pRefHV, |
450 |
|
const int x, const int y, |
451 |
|
const uint32_t MotionFlags, |
452 |
|
const uint32_t iFcode, |
453 |
|
const MBParam * const pParam, |
454 |
|
MACROBLOCK * const pMB, |
455 |
|
const VECTOR * const predMV, |
456 |
|
int32_t * const best_sad, |
457 |
|
const int32_t mode_current, |
458 |
|
SearchData * const Data) |
459 |
|
{ |
460 |
|
|
461 |
|
int i; |
462 |
|
VECTOR pmv[7]; |
463 |
|
int threshA = (MotionFlags & XVID_ME_FASTREFINE16) ? 150 : 300; |
464 |
|
*Data->iMinSAD = MV_MAX_ERROR; |
465 |
|
Data->iFcode = iFcode; |
466 |
|
Data->qpel_precision = 0; |
467 |
|
Data->chromaX = Data->chromaY = Data->chromaSAD = 256*4096; /* reset chroma-sad cache */ |
468 |
|
|
469 |
|
Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16; |
470 |
|
Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16; |
471 |
|
Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16; |
472 |
|
Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
473 |
|
Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
474 |
|
Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
475 |
|
|
476 |
|
Data->predMV = *predMV; |
477 |
|
|
478 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
479 |
|
pParam->width, pParam->height, iFcode - Data->qpel, 1, 0); |
480 |
|
|
481 |
|
pmv[0] = Data->predMV; |
482 |
|
if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; } |
483 |
|
|
484 |
|
PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current); |
485 |
|
|
486 |
|
Data->currentMV->x = Data->currentMV->y = 0; |
487 |
|
|
488 |
|
/* main loop. checking all predictions */ |
489 |
|
for (i = 0; i < 7; i++) |
490 |
|
if (!vector_repeats(pmv, i) ) |
491 |
|
CheckCandidate16no4v(pmv[i].x, pmv[i].y, Data, i); |
492 |
|
|
493 |
|
if (*Data->iMinSAD > 512) { |
494 |
|
unsigned int mask = make_mask(pmv, 7, Data->dir); |
495 |
|
|
496 |
|
MainSearchFunc *MainSearchPtr; |
497 |
|
if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch; |
498 |
|
else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch; |
499 |
|
else MainSearchPtr = xvid_me_DiamondSearch; |
500 |
|
|
501 |
|
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v); |
502 |
|
} |
503 |
|
|
504 |
|
xvid_me_SubpelRefine(Data, CheckCandidate16no4v); |
505 |
|
|
506 |
|
if (Data->qpel && (*Data->iMinSAD < *best_sad + threshA)) { |
507 |
|
Data->currentQMV->x = 2*Data->currentMV->x; |
508 |
|
Data->currentQMV->y = 2*Data->currentMV->y; |
509 |
|
Data->qpel_precision = 1; |
510 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
511 |
|
pParam->width, pParam->height, iFcode, 2, 0); |
512 |
|
|
513 |
|
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
514 |
|
if (MotionFlags & XVID_ME_FASTREFINE16) |
515 |
|
SubpelRefine_Fast(Data, CheckCandidate16no4v_qpel); |
516 |
|
else |
517 |
|
xvid_me_SubpelRefine(Data, CheckCandidate16no4v); |
518 |
|
} |
519 |
|
} |
520 |
|
|
521 |
|
/* three bits are needed to code backward mode. four for forward */ |
522 |
|
|
523 |
|
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16; |
524 |
|
else *Data->iMinSAD += 3 * Data->lambda16; |
525 |
|
|
526 |
|
if (*Data->iMinSAD < *best_sad) { |
527 |
|
*best_sad = *Data->iMinSAD; |
528 |
|
pMB->mode = mode_current; |
529 |
|
if (Data->qpel) { |
530 |
|
pMB->pmvs[0].x = Data->currentQMV->x - predMV->x; |
531 |
|
pMB->pmvs[0].y = Data->currentQMV->y - predMV->y; |
532 |
|
if (mode_current == MODE_FORWARD) |
533 |
|
pMB->qmvs[0] = *Data->currentQMV; |
534 |
|
else |
535 |
|
pMB->b_qmvs[0] = *Data->currentQMV; |
536 |
|
} else { |
537 |
|
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
538 |
|
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
539 |
|
} |
540 |
|
if (mode_current == MODE_FORWARD) pMB->mvs[0] = *Data->currentMV; |
541 |
|
else pMB->b_mvs[0] = *Data->currentMV; |
542 |
|
} |
543 |
|
|
544 |
|
if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV; |
545 |
|
else *(Data->currentMV+1) = *Data->currentMV; /* we store currmv for interpolate search */ |
546 |
|
} |
547 |
|
|
548 |
|
static void |
549 |
|
SkipDecisionB(const IMAGE * const pCur, |
550 |
|
const IMAGE * const f_Ref, |
551 |
|
const IMAGE * const b_Ref, |
552 |
|
MACROBLOCK * const pMB, |
553 |
|
const uint32_t x, const uint32_t y, |
554 |
|
const SearchData * const Data) |
555 |
|
{ |
556 |
|
int k; |
557 |
|
|
558 |
|
if (!Data->chroma) { |
559 |
|
int dx = 0, dy = 0, b_dx = 0, b_dy = 0; |
560 |
|
int32_t sum; |
561 |
|
const uint32_t stride = Data->iEdgedWidth/2; |
562 |
|
/* this is not full chroma compensation, only it's fullpel approximation. should work though */ |
563 |
|
|
564 |
|
for (k = 0; k < 4; k++) { |
565 |
|
dy += Data->directmvF[k].y >> Data->qpel; |
566 |
|
dx += Data->directmvF[k].x >> Data->qpel; |
567 |
|
b_dy += Data->directmvB[k].y >> Data->qpel; |
568 |
|
b_dx += Data->directmvB[k].x >> Data->qpel; |
569 |
|
} |
570 |
|
|
571 |
|
dy = (dy >> 3) + roundtab_76[dy & 0xf]; |
572 |
|
dx = (dx >> 3) + roundtab_76[dx & 0xf]; |
573 |
|
b_dy = (b_dy >> 3) + roundtab_76[b_dy & 0xf]; |
574 |
|
b_dx = (b_dx >> 3) + roundtab_76[b_dx & 0xf]; |
575 |
|
|
576 |
|
sum = sad8bi(pCur->u + 8 * x + 8 * y * stride, |
577 |
|
f_Ref->u + (y*8 + dy/2) * stride + x*8 + dx/2, |
578 |
|
b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
579 |
|
stride); |
580 |
|
|
581 |
|
if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ |
582 |
|
|
583 |
|
sum += sad8bi(pCur->v + 8*x + 8 * y * stride, |
584 |
|
f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2, |
585 |
|
b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
586 |
|
stride); |
587 |
|
|
588 |
|
if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ |
589 |
|
} |
590 |
|
|
591 |
|
/* skip */ |
592 |
|
pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
593 |
|
for (k = 0; k < 4; k++) { |
594 |
|
pMB->qmvs[k] = pMB->mvs[k] = Data->directmvF[k]; |
595 |
|
pMB->b_qmvs[k] = pMB->b_mvs[k] = Data->directmvB[k]; |
596 |
|
} |
597 |
|
} |
598 |
|
|
599 |
|
static uint32_t |
600 |
|
SearchDirect(const IMAGE * const f_Ref, |
601 |
|
const uint8_t * const f_RefH, |
602 |
|
const uint8_t * const f_RefV, |
603 |
|
const uint8_t * const f_RefHV, |
604 |
|
const IMAGE * const b_Ref, |
605 |
|
const uint8_t * const b_RefH, |
606 |
|
const uint8_t * const b_RefV, |
607 |
|
const uint8_t * const b_RefHV, |
608 |
|
const IMAGE * const pCur, |
609 |
|
const int x, const int y, |
610 |
|
const uint32_t MotionFlags, |
611 |
|
const int32_t TRB, const int32_t TRD, |
612 |
|
const MBParam * const pParam, |
613 |
|
MACROBLOCK * const pMB, |
614 |
|
const MACROBLOCK * const b_mb, |
615 |
|
int32_t * const best_sad, |
616 |
|
SearchData * const Data) |
617 |
|
|
618 |
|
{ |
619 |
|
int32_t skip_sad; |
620 |
|
int k = (x + Data->iEdgedWidth*y) * 16; |
621 |
|
MainSearchFunc *MainSearchPtr; |
622 |
|
CheckFunc * CheckCandidate; |
623 |
|
|
624 |
|
*Data->iMinSAD = 256*4096; |
625 |
|
Data->RefP[0] = f_Ref->y + k; |
626 |
|
Data->RefP[2] = f_RefH + k; |
627 |
|
Data->RefP[1] = f_RefV + k; |
628 |
|
Data->RefP[3] = f_RefHV + k; |
629 |
|
Data->b_RefP[0] = b_Ref->y + k; |
630 |
|
Data->b_RefP[2] = b_RefH + k; |
631 |
|
Data->b_RefP[1] = b_RefV + k; |
632 |
|
Data->b_RefP[3] = b_RefHV + k; |
633 |
|
Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
634 |
|
Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
635 |
|
Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
636 |
|
Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
637 |
|
|
638 |
|
k = Data->qpel ? 4 : 2; |
639 |
|
Data->max_dx = k * (pParam->width - x * 16); |
640 |
|
Data->max_dy = k * (pParam->height - y * 16); |
641 |
|
Data->min_dx = -k * (16 + x * 16); |
642 |
|
Data->min_dy = -k * (16 + y * 16); |
643 |
|
|
644 |
|
Data->referencemv = Data->qpel ? b_mb->qmvs : b_mb->mvs; |
645 |
|
Data->qpel_precision = 0; |
646 |
|
|
647 |
|
for (k = 0; k < 4; k++) { |
648 |
|
pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD); |
649 |
|
pMB->b_mvs[k].x = Data->directmvB[k].x = ((TRB - TRD) * Data->referencemv[k].x) / TRD; |
650 |
|
pMB->mvs[k].y = Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD); |
651 |
|
pMB->b_mvs[k].y = Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD; |
652 |
|
|
653 |
|
if ( (pMB->b_mvs[k].x > Data->max_dx) | (pMB->b_mvs[k].x < Data->min_dx) |
654 |
|
| (pMB->b_mvs[k].y > Data->max_dy) | (pMB->b_mvs[k].y < Data->min_dy) ) { |
655 |
|
|
656 |
|
*best_sad = 256*4096; /* in that case, we won't use direct mode */ |
657 |
|
pMB->mode = MODE_DIRECT; /* just to make sure it doesn't say "MODE_DIRECT_NONE_MV" */ |
658 |
|
pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0; |
659 |
|
return 256*4096; |
660 |
|
} |
661 |
|
if (b_mb->mode != MODE_INTER4V) { |
662 |
|
pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mvs[0]; |
663 |
|
pMB->b_mvs[1] = pMB->b_mvs[2] = pMB->b_mvs[3] = pMB->b_mvs[0]; |
664 |
|
Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0]; |
665 |
|
Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0]; |
666 |
|
break; |
667 |
|
} |
668 |
|
} |
669 |
|
|
670 |
|
CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v; |
671 |
|
|
672 |
|
CheckCandidate(0, 0, Data, 255); |
673 |
|
|
674 |
|
/* initial (fast) skip decision */ |
675 |
|
if (*Data->iMinSAD < (int)Data->iQuant * INITIAL_SKIP_THRESH) { |
676 |
|
/* possible skip */ |
677 |
|
SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data); |
678 |
|
if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; /* skipped */ |
679 |
|
} |
680 |
|
|
681 |
|
*Data->iMinSAD += Data->lambda16; |
682 |
|
skip_sad = *Data->iMinSAD; |
683 |
|
|
684 |
|
if (!(MotionFlags & XVID_ME_SKIP_DELTASEARCH)) { |
685 |
|
if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch; |
686 |
|
else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch; |
687 |
|
else MainSearchPtr = xvid_me_DiamondSearch; |
688 |
|
|
689 |
|
MainSearchPtr(0, 0, Data, 255, CheckCandidate); |
690 |
|
|
691 |
|
xvid_me_SubpelRefine(Data, CheckCandidate); |
692 |
|
} |
693 |
|
|
694 |
|
*best_sad = *Data->iMinSAD; |
695 |
|
|
696 |
|
if (Data->qpel || b_mb->mode == MODE_INTER4V) pMB->mode = MODE_DIRECT; |
697 |
|
else pMB->mode = MODE_DIRECT_NO4V; /* for faster compensation */ |
698 |
|
|
699 |
|
pMB->pmvs[3] = *Data->currentMV; |
700 |
|
|
701 |
|
for (k = 0; k < 4; k++) { |
702 |
|
pMB->mvs[k].x = Data->directmvF[k].x + Data->currentMV->x; |
703 |
|
pMB->b_mvs[k].x = ( (Data->currentMV->x == 0) |
704 |
|
? Data->directmvB[k].x |
705 |
|
:pMB->mvs[k].x - Data->referencemv[k].x); |
706 |
|
pMB->mvs[k].y = (Data->directmvF[k].y + Data->currentMV->y); |
707 |
|
pMB->b_mvs[k].y = ((Data->currentMV->y == 0) |
708 |
|
? Data->directmvB[k].y |
709 |
|
: pMB->mvs[k].y - Data->referencemv[k].y); |
710 |
|
if (Data->qpel) { |
711 |
|
pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2; |
712 |
|
pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2; |
713 |
|
pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2; |
714 |
|
pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2; |
715 |
|
} |
716 |
|
|
717 |
|
if (b_mb->mode != MODE_INTER4V) { |
718 |
|
pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0]; |
719 |
|
pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0]; |
720 |
|
pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0]; |
721 |
|
pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0]; |
722 |
|
break; |
723 |
|
} |
724 |
|
} |
725 |
|
return skip_sad; |
726 |
|
} |
727 |
|
|
728 |
|
|
729 |
|
static void set_range(int * range, SearchData * Data) |
730 |
|
{ |
731 |
|
Data->min_dx = range[0]; |
732 |
|
Data->max_dx = range[1]; |
733 |
|
Data->min_dy = range[2]; |
734 |
|
Data->max_dy = range[3]; |
735 |
|
} |
736 |
|
|
737 |
|
static void |
738 |
|
SubpelRefine_dir(SearchData * const data, CheckFunc * const CheckCandidate, const int dir) |
739 |
|
{ |
740 |
|
/* Do a half-pel or q-pel refinement */ |
741 |
|
const VECTOR centerMV = data->qpel_precision ? |
742 |
|
data->currentQMV[dir-1] : data->currentMV[dir-1]; |
743 |
|
|
744 |
|
CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, dir); |
745 |
|
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, dir); |
746 |
|
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y, dir); |
747 |
|
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y + 1, dir); |
748 |
|
CHECK_CANDIDATE(centerMV.x, centerMV.y + 1, dir); |
749 |
|
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y + 1, dir); |
750 |
|
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y, dir); |
751 |
|
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y - 1, dir); |
752 |
|
} |
753 |
|
|
754 |
|
/* Pretty much redundant code, just as SubpelRefine_dir above too |
755 |
|
* |
756 |
|
* TODO: Get rid off all the redundancy (SubpelRefine_Fast_dir, |
757 |
|
* CheckCandidate16no4v_qpel etc.) */ |
758 |
|
|
759 |
|
void |
760 |
|
SubpelRefine_Fast_dir(SearchData * data, CheckFunc * CheckCandidate, const int dir) |
761 |
|
{ |
762 |
|
/* Do a fast q-pel refinement */ |
763 |
|
VECTOR centerMV; |
764 |
|
VECTOR second_best; |
765 |
|
int best_sad = *data->iMinSAD; |
766 |
|
int xo, yo, xo2, yo2; |
767 |
|
int size = 2; |
768 |
|
data->iMinSAD2 = 0; |
769 |
|
|
770 |
|
/* check all halfpixel positions near our best halfpel position */ |
771 |
|
centerMV = data->currentQMV[dir-1]; |
772 |
|
*data->iMinSAD = 256 * 4096; |
773 |
|
|
774 |
|
CHECK_CANDIDATE(centerMV.x, centerMV.y - size, dir); |
775 |
|
CHECK_CANDIDATE(centerMV.x + size, centerMV.y - size, dir); |
776 |
|
CHECK_CANDIDATE(centerMV.x + size, centerMV.y, dir); |
777 |
|
CHECK_CANDIDATE(centerMV.x + size, centerMV.y + size, dir); |
778 |
|
|
779 |
|
CHECK_CANDIDATE(centerMV.x, centerMV.y + size, dir); |
780 |
|
CHECK_CANDIDATE(centerMV.x - size, centerMV.y + size, dir); |
781 |
|
CHECK_CANDIDATE(centerMV.x - size, centerMV.y, dir); |
782 |
|
CHECK_CANDIDATE(centerMV.x - size, centerMV.y - size, dir); |
783 |
|
|
784 |
|
second_best = data->currentQMV[dir-1]; |
785 |
|
|
786 |
|
/* after second_best has been found, go back to the vector we began with */ |
787 |
|
|
788 |
|
data->currentQMV[dir-1] = centerMV; |
789 |
|
*data->iMinSAD = best_sad; |
790 |
|
|
791 |
|
xo = centerMV.x; |
792 |
|
yo = centerMV.y; |
793 |
|
xo2 = second_best.x; |
794 |
|
yo2 = second_best.y; |
795 |
|
|
796 |
|
data->iMinSAD2 = 256 * 4096; |
797 |
|
|
798 |
|
if (yo == yo2) { |
799 |
|
CHECK_CANDIDATE((xo+xo2)>>1, yo, dir); |
800 |
|
CHECK_CANDIDATE(xo, yo-1, dir); |
801 |
|
CHECK_CANDIDATE(xo, yo+1, dir); |
802 |
|
|
803 |
|
if(best_sad <= data->iMinSAD2) return; |
804 |
|
|
805 |
|
if(data->currentQMV[dir-1].x == data->currentQMV2.x) { |
806 |
|
CHECK_CANDIDATE((xo+xo2)>>1, yo-1, dir); |
807 |
|
CHECK_CANDIDATE((xo+xo2)>>1, yo+1, dir); |
808 |
|
} else { |
809 |
|
CHECK_CANDIDATE((xo+xo2)>>1, |
810 |
|
(data->currentQMV[dir-1].x == xo) ? data->currentQMV[dir-1].y : data->currentQMV2.y, dir); |
811 |
|
} |
812 |
|
return; |
813 |
|
} |
814 |
|
|
815 |
|
if (xo == xo2) { |
816 |
|
CHECK_CANDIDATE(xo, (yo+yo2)>>1, dir); |
817 |
|
CHECK_CANDIDATE(xo-1, yo, dir); |
818 |
|
CHECK_CANDIDATE(xo+1, yo, dir); |
819 |
|
|
820 |
|
if(best_sad < data->iMinSAD2) return; |
821 |
|
|
822 |
|
if(data->currentQMV[dir-1].y == data->currentQMV2.y) { |
823 |
|
CHECK_CANDIDATE(xo-1, (yo+yo2)>>1, dir); |
824 |
|
CHECK_CANDIDATE(xo+1, (yo+yo2)>>1, dir); |
825 |
|
} else { |
826 |
|
CHECK_CANDIDATE((data->currentQMV[dir-1].y == yo) ? data->currentQMV[dir-1].x : data->currentQMV2.x, (yo+yo2)>>1, dir); |
827 |
|
} |
828 |
|
return; |
829 |
|
} |
830 |
|
|
831 |
|
CHECK_CANDIDATE(xo, (yo+yo2)>>1, dir); |
832 |
|
CHECK_CANDIDATE((xo+xo2)>>1, yo, dir); |
833 |
|
|
834 |
|
if(best_sad <= data->iMinSAD2) return; |
835 |
|
|
836 |
|
CHECK_CANDIDATE((xo+xo2)>>1, (yo+yo2)>>1, dir); |
837 |
|
} |
838 |
|
|
839 |
|
static void |
840 |
|
SearchInterpolate(const IMAGE * const f_Ref, |
841 |
|
const uint8_t * const f_RefH, |
842 |
|
const uint8_t * const f_RefV, |
843 |
|
const uint8_t * const f_RefHV, |
844 |
|
const IMAGE * const b_Ref, |
845 |
|
const uint8_t * const b_RefH, |
846 |
|
const uint8_t * const b_RefV, |
847 |
|
const uint8_t * const b_RefHV, |
848 |
|
const int x, const int y, |
849 |
|
const uint32_t fcode, |
850 |
|
const uint32_t bcode, |
851 |
|
const uint32_t MotionFlags, |
852 |
|
const MBParam * const pParam, |
853 |
|
const VECTOR * const f_predMV, |
854 |
|
const VECTOR * const b_predMV, |
855 |
|
MACROBLOCK * const pMB, |
856 |
|
int32_t * const best_sad, |
857 |
|
SearchData * const Data) |
858 |
|
|
859 |
|
{ |
860 |
|
int i, j; |
861 |
|
int b_range[4], f_range[4]; |
862 |
|
int threshA = (MotionFlags & XVID_ME_FAST_MODEINTERPOLATE) ? 250 : 500; |
863 |
|
int threshB = (MotionFlags & XVID_ME_FAST_MODEINTERPOLATE) ? 150 : 300; |
864 |
|
|
865 |
|
Data->qpel_precision = 0; |
866 |
|
*Data->iMinSAD = 4096*256; |
867 |
|
Data->iFcode = fcode; Data->bFcode = bcode; |
868 |
|
|
869 |
|
i = (x + y * Data->iEdgedWidth) * 16; |
870 |
|
|
871 |
|
Data->RefP[0] = f_Ref->y + i; |
872 |
|
Data->RefP[2] = f_RefH + i; |
873 |
|
Data->RefP[1] = f_RefV + i; |
874 |
|
Data->RefP[3] = f_RefHV + i; |
875 |
|
Data->b_RefP[0] = b_Ref->y + i; |
876 |
|
Data->b_RefP[2] = b_RefH + i; |
877 |
|
Data->b_RefP[1] = b_RefV + i; |
878 |
|
Data->b_RefP[3] = b_RefHV + i; |
879 |
|
Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
880 |
|
Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
881 |
|
Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
882 |
|
Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
883 |
|
|
884 |
|
Data->predMV = *f_predMV; |
885 |
|
Data->bpredMV = *b_predMV; |
886 |
|
|
887 |
|
Data->currentMV[0] = Data->currentMV[2]; /* forward search left its vector here */ |
888 |
|
|
889 |
|
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); |
890 |
|
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); |
891 |
|
|
892 |
|
if (Data->currentMV[0].x > f_range[1]) Data->currentMV[0].x = f_range[1]; |
893 |
|
if (Data->currentMV[0].x < f_range[0]) Data->currentMV[0].x = f_range[0]; |
894 |
|
if (Data->currentMV[0].y > f_range[3]) Data->currentMV[0].y = f_range[3]; |
895 |
|
if (Data->currentMV[0].y < f_range[2]) Data->currentMV[0].y = f_range[2]; |
896 |
|
|
897 |
|
if (Data->currentMV[1].x > b_range[1]) Data->currentMV[1].x = b_range[1]; |
898 |
|
if (Data->currentMV[1].x < b_range[0]) Data->currentMV[1].x = b_range[0]; |
899 |
|
if (Data->currentMV[1].y > b_range[3]) Data->currentMV[1].y = b_range[3]; |
900 |
|
if (Data->currentMV[1].y < b_range[2]) Data->currentMV[1].y = b_range[2]; |
901 |
|
|
902 |
|
set_range(f_range, Data); |
903 |
|
|
904 |
|
CheckCandidateInt(Data->currentMV[0].x, Data->currentMV[0].y, Data, 1); |
905 |
|
|
906 |
|
/* diamond */ |
907 |
|
do { |
908 |
|
Data->dir = 0; |
909 |
|
/* forward MV moves */ |
910 |
|
i = Data->currentMV[0].x; j = Data->currentMV[0].y; |
911 |
|
|
912 |
|
CheckCandidateInt(i + 1, j, Data, 1); |
913 |
|
CheckCandidateInt(i, j + 1, Data, 1); |
914 |
|
CheckCandidateInt(i - 1, j, Data, 1); |
915 |
|
CheckCandidateInt(i, j - 1, Data, 1); |
916 |
|
|
917 |
|
/* backward MV moves */ |
918 |
|
set_range(b_range, Data); |
919 |
|
i = Data->currentMV[1].x; j = Data->currentMV[1].y; |
920 |
|
|
921 |
|
CheckCandidateInt(i + 1, j, Data, 2); |
922 |
|
CheckCandidateInt(i, j + 1, Data, 2); |
923 |
|
CheckCandidateInt(i - 1, j, Data, 2); |
924 |
|
CheckCandidateInt(i, j - 1, Data, 2); |
925 |
|
|
926 |
|
set_range(f_range, Data); |
927 |
|
|
928 |
|
} while (Data->dir != 0); |
929 |
|
|
930 |
|
/* qpel refinement */ |
931 |
|
if (Data->qpel) { |
932 |
|
if (*Data->iMinSAD > *best_sad + threshA) return; |
933 |
|
Data->qpel_precision = 1; |
934 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, fcode, 2, 0); |
935 |
|
|
936 |
|
Data->currentQMV[0].x = 2 * Data->currentMV[0].x; |
937 |
|
Data->currentQMV[0].y = 2 * Data->currentMV[0].y; |
938 |
|
Data->currentQMV[1].x = 2 * Data->currentMV[1].x; |
939 |
|
Data->currentQMV[1].y = 2 * Data->currentMV[1].y; |
940 |
|
|
941 |
|
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
942 |
|
if (MotionFlags & XVID_ME_FASTREFINE16) |
943 |
|
SubpelRefine_Fast_dir(Data, CheckCandidateInt_qpel, 1); |
944 |
|
else |
945 |
|
SubpelRefine_dir(Data, CheckCandidateInt, 1); |
946 |
|
} |
947 |
|
|
948 |
|
if (*Data->iMinSAD > *best_sad + threshB) return; |
949 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0); |
950 |
|
|
951 |
|
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
952 |
|
if (MotionFlags & XVID_ME_FASTREFINE16) |
953 |
|
SubpelRefine_Fast_dir(Data, CheckCandidateInt_qpel, 2); |
954 |
|
else |
955 |
|
SubpelRefine_dir(Data, CheckCandidateInt, 2); |
956 |
|
} |
957 |
|
} |
958 |
|
|
959 |
|
*Data->iMinSAD += 2 * Data->lambda16; /* two bits are needed to code interpolate mode. */ |
960 |
|
|
961 |
|
if (*Data->iMinSAD < *best_sad) { |
962 |
|
*best_sad = *Data->iMinSAD; |
963 |
|
pMB->mvs[0] = Data->currentMV[0]; |
964 |
|
pMB->b_mvs[0] = Data->currentMV[1]; |
965 |
|
pMB->mode = MODE_INTERPOLATE; |
966 |
|
if (Data->qpel) { |
967 |
|
pMB->qmvs[0] = Data->currentQMV[0]; |
968 |
|
pMB->b_qmvs[0] = Data->currentQMV[1]; |
969 |
|
pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x; |
970 |
|
pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y; |
971 |
|
pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x; |
972 |
|
pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y; |
973 |
|
} else { |
974 |
|
pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x; |
975 |
|
pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y; |
976 |
|
pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x; |
977 |
|
pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y; |
978 |
|
} |
979 |
|
} |
980 |
|
} |
981 |
|
|
982 |
|
void |
983 |
|
MotionEstimationBVOP(MBParam * const pParam, |
984 |
|
FRAMEINFO * const frame, |
985 |
|
const int32_t time_bp, |
986 |
|
const int32_t time_pp, |
987 |
|
/* forward (past) reference */ |
988 |
|
const MACROBLOCK * const f_mbs, |
989 |
|
const IMAGE * const f_ref, |
990 |
|
const IMAGE * const f_refH, |
991 |
|
const IMAGE * const f_refV, |
992 |
|
const IMAGE * const f_refHV, |
993 |
|
/* backward (future) reference */ |
994 |
|
const FRAMEINFO * const b_reference, |
995 |
|
const IMAGE * const b_ref, |
996 |
|
const IMAGE * const b_refH, |
997 |
|
const IMAGE * const b_refV, |
998 |
|
const IMAGE * const b_refHV) |
999 |
|
{ |
1000 |
|
uint32_t i, j; |
1001 |
|
int32_t best_sad; |
1002 |
|
uint32_t skip_sad; |
1003 |
|
|
1004 |
|
const MACROBLOCK * const b_mbs = b_reference->mbs; |
1005 |
|
MACROBLOCK *const pMBs = frame->mbs; |
1006 |
|
|
1007 |
|
VECTOR f_predMV, b_predMV; |
1008 |
|
|
1009 |
|
const int32_t TRB = time_pp - time_bp; |
1010 |
|
const int32_t TRD = time_pp; |
1011 |
|
|
1012 |
|
/* some pre-inintialized data for the rest of the search */ |
1013 |
|
|
1014 |
|
SearchData Data; |
1015 |
|
memset(&Data, 0, sizeof(SearchData)); |
1016 |
|
|
1017 |
|
Data.iEdgedWidth = pParam->edged_width; |
1018 |
|
Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0; |
1019 |
|
Data.rounding = 0; |
1020 |
|
Data.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP; |
1021 |
|
Data.iQuant = frame->quant; |
1022 |
|
|
1023 |
|
Data.RefQ = f_refV->u; /* a good place, also used in MC (for similar purpose) */ |
1024 |
|
|
1025 |
|
/* note: i==horizontal, j==vertical */ |
1026 |
|
for (j = 0; j < pParam->mb_height; j++) { |
1027 |
|
|
1028 |
|
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
1029 |
|
|
1030 |
|
for (i = 0; i < pParam->mb_width; i++) { |
1031 |
|
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
1032 |
|
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
1033 |
|
int interpol_search = 0; |
1034 |
|
int bf_search = 0; |
1035 |
|
int bf_thresh = 0; |
1036 |
|
|
1037 |
|
/* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */ |
1038 |
|
if (b_reference->coding_type != S_VOP) |
1039 |
|
if (b_mb->mode == MODE_NOT_CODED) { |
1040 |
|
pMB->mode = MODE_NOT_CODED; |
1041 |
|
pMB->mvs[0] = pMB->b_mvs[0] = zeroMV; |
1042 |
|
pMB->sad16 = 0; |
1043 |
|
continue; |
1044 |
|
} |
1045 |
|
|
1046 |
|
Data.lambda16 = xvid_me_lambda_vec16[b_mb->quant]; |
1047 |
|
|
1048 |
|
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
1049 |
|
Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8; |
1050 |
|
Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8; |
1051 |
|
|
1052 |
|
/* direct search comes first, because it (1) checks for SKIP-mode |
1053 |
|
and (2) sets very good predictions for forward and backward search */ |
1054 |
|
skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
1055 |
|
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
1056 |
|
&frame->image, |
1057 |
|
i, j, |
1058 |
|
frame->motion_flags, |
1059 |
|
TRB, TRD, |
1060 |
|
pParam, |
1061 |
|
pMB, b_mb, |
1062 |
|
&best_sad, |
1063 |
|
&Data); |
1064 |
|
|
1065 |
|
if (pMB->mode == MODE_DIRECT_NONE_MV) { |
1066 |
|
pMB->sad16 = best_sad; |
1067 |
|
continue; |
1068 |
|
} |
1069 |
|
|
1070 |
|
if (frame->motion_flags & XVID_ME_BFRAME_EARLYSTOP) { |
1071 |
|
if(i > 0 && j > 0 && i < pParam->mb_width) { |
1072 |
|
bf_thresh = MIN((&pMBs[(i-1) + j * pParam->mb_width])->sad16, |
1073 |
|
MIN((&pMBs[i + (j-1) * pParam->mb_width])->sad16, |
1074 |
|
(&pMBs[(i+1) + (j-1) * pParam->mb_width])->sad16)); |
1075 |
|
|
1076 |
|
if (((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_FORWARD) && |
1077 |
|
((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_BACKWARD) && |
1078 |
|
((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_INTERPOLATE)) |
1079 |
|
bf_search++; |
1080 |
|
|
1081 |
|
if (((&pMBs[i + (j - 1) * pParam->mb_width])->mode != MODE_FORWARD) && |
1082 |
|
((&pMBs[i + (j - 1) * pParam->mb_width])->mode != MODE_BACKWARD) && |
1083 |
|
((&pMBs[i + (j - 1) * pParam->mb_width])->mode != MODE_INTERPOLATE)) |
1084 |
|
bf_search++; |
1085 |
|
|
1086 |
|
if (((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode != MODE_FORWARD) && |
1087 |
|
((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode != MODE_BACKWARD) && |
1088 |
|
((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode != MODE_INTERPOLATE)) |
1089 |
|
bf_search++; |
1090 |
|
} |
1091 |
|
|
1092 |
|
if ((best_sad < bf_thresh) && (bf_search == 3)) |
1093 |
|
continue; |
1094 |
|
} |
1095 |
|
|
1096 |
|
/* forward search */ |
1097 |
|
SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
1098 |
|
i, j, |
1099 |
|
frame->motion_flags, |
1100 |
|
frame->fcode, pParam, |
1101 |
|
pMB, &f_predMV, &best_sad, |
1102 |
|
MODE_FORWARD, &Data); |
1103 |
|
|
1104 |
|
/* backward search */ |
1105 |
|
SearchBF(b_ref, b_refH->y, b_refV->y, b_refHV->y, |
1106 |
|
i, j, |
1107 |
|
frame->motion_flags, |
1108 |
|
frame->bcode, pParam, |
1109 |
|
pMB, &b_predMV, &best_sad, |
1110 |
|
MODE_BACKWARD, &Data); |
1111 |
|
|
1112 |
|
/* interpolate search comes last, because it uses data from forward and backward as prediction */ |
1113 |
|
if (frame->motion_flags & XVID_ME_FAST_MODEINTERPOLATE) { |
1114 |
|
|
1115 |
|
if(i > 0 && j > 0 && i < pParam->mb_width) { |
1116 |
|
if ((&pMBs[(i-1) + j * pParam->mb_width])->mode == MODE_INTERPOLATE) |
1117 |
|
interpol_search++; |
1118 |
|
if ((&pMBs[i + (j - 1) * pParam->mb_width])->mode == MODE_INTERPOLATE) |
1119 |
|
interpol_search++; |
1120 |
|
if ((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode == MODE_INTERPOLATE) |
1121 |
|
interpol_search++; |
1122 |
|
} |
1123 |
|
else |
1124 |
|
interpol_search = 1; |
1125 |
|
|
1126 |
|
interpol_search |= !(best_sad < 3 * Data.iQuant * MAX_SAD00_FOR_SKIP * (Data.chroma ? 3:2)); |
1127 |
|
} |
1128 |
|
else |
1129 |
|
interpol_search = 1; |
1130 |
|
|
1131 |
|
if (interpol_search) { |
1132 |
|
SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
1133 |
|
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
1134 |
|
i, j, |
1135 |
|
frame->fcode, frame->bcode, |
1136 |
|
frame->motion_flags, |
1137 |
|
pParam, |
1138 |
|
&f_predMV, &b_predMV, |
1139 |
|
pMB, &best_sad, |
1140 |
|
&Data); |
1141 |
|
} |
1142 |
|
|
1143 |
|
/* final skip decision */ |
1144 |
|
if ( (skip_sad < Data.iQuant * MAX_SAD00_FOR_SKIP * (Data.chroma ? 3:2) ) |
1145 |
|
&& ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) |
1146 |
|
|
1147 |
|
SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data); |
1148 |
|
|
1149 |
|
switch (pMB->mode) { |
1150 |
|
case MODE_FORWARD: |
1151 |
|
f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0]; |
1152 |
|
pMB->sad16 = best_sad; |
1153 |
|
break; |
1154 |
|
case MODE_BACKWARD: |
1155 |
|
b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0]; |
1156 |
|
pMB->sad16 = best_sad; |
1157 |
|
break; |
1158 |
|
case MODE_INTERPOLATE: |
1159 |
|
f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0]; |
1160 |
|
b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0]; |
1161 |
|
pMB->sad16 = best_sad; |
1162 |
|
break; |
1163 |
|
default: |
1164 |
|
pMB->sad16 = best_sad; |
1165 |
|
break; |
1166 |
|
} |
1167 |
|
} |
1168 |
|
} |
1169 |
|
} |