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