[svn] / trunk / xvidcore / src / motion / estimation_bvop.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/src/motion/estimation_bvop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1441 - (view) (download)

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

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4