[svn] / branches / release-1_3-branch / xvidcore / src / motion / estimation_bvop.c Repository:
ViewVC logotype

Annotation of /branches/release-1_3-branch/xvidcore/src/motion/estimation_bvop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1919 - (view) (download)
Original Path: trunk/xvidcore/src/motion/estimation_bvop.c

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 : Isibaar 1909 * 2002-2010 Michael Militzer <michael@xvid.org>
8 : edgomez 1382 * 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 : Isibaar 1919 * $Id: estimation_bvop.c,v 1.28 2010-12-24 13:21:35 Isibaar 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 : syskin 1443 if (data->chromaX == fx && data->chromaY == fy &&
59 :     data->b_chromaX == bx && data->b_chromaY == by)
60 :     return data->chromaSAD;
61 :    
62 : syskin 1441 offset = (fx>>1) + (fy>>1)*stride;
63 :     filter = ((fx & 1) << 1) | (fy & 1);
64 : edgomez 1382
65 :     if (filter != 0) {
66 : syskin 1475 f_refu = data->RefQ + 64;
67 :     f_refv = data->RefQ + 64 + 8;
68 : syskin 1443 if (data->chromaX != fx || data->chromaY != fy) {
69 :     interpolate8x8_halfpel[filter](f_refu, data->RefP[4] + offset, stride, data->rounding);
70 :     interpolate8x8_halfpel[filter](f_refv, data->RefP[5] + offset, stride, data->rounding);
71 :     }
72 : edgomez 1382 } else {
73 :     f_refu = (uint8_t*)data->RefP[4] + offset;
74 :     f_refv = (uint8_t*)data->RefP[5] + offset;
75 :     }
76 : syskin 1443 data->chromaX = fx; data->chromaY = fy;
77 : edgomez 1382
78 :     offset = (bx>>1) + (by>>1)*stride;
79 :     filter = ((bx & 1) << 1) | (by & 1);
80 :    
81 :     if (filter != 0) {
82 : syskin 1475 b_refu = data->RefQ + 64 + 16;
83 :     b_refv = data->RefQ + 64 + 24;
84 : syskin 1443 if (data->b_chromaX != bx || data->b_chromaY != by) {
85 :     interpolate8x8_halfpel[filter](b_refu, data->b_RefP[4] + offset, stride, data->rounding);
86 :     interpolate8x8_halfpel[filter](b_refv, data->b_RefP[5] + offset, stride, data->rounding);
87 :     }
88 : edgomez 1382 } else {
89 :     b_refu = (uint8_t*)data->b_RefP[4] + offset;
90 :     b_refv = (uint8_t*)data->b_RefP[5] + offset;
91 :     }
92 : syskin 1443 data->b_chromaX = bx; data->b_chromaY = by;
93 : edgomez 1382
94 :     sad = sad8bi(data->CurU, b_refu, f_refu, stride);
95 :     sad += sad8bi(data->CurV, b_refv, f_refv, stride);
96 :    
97 : syskin 1443 data->chromaSAD = sad;
98 : edgomez 1382 return sad;
99 :     }
100 :    
101 :     static void
102 :     CheckCandidateInt(const int x, const int y, SearchData * const data, const unsigned int Direction)
103 :     {
104 :     int32_t sad, xf, yf, xb, yb, xcf, ycf, xcb, ycb;
105 :     uint32_t t;
106 : syskin 1441
107 : edgomez 1382 const uint8_t *ReferenceF, *ReferenceB;
108 :     VECTOR *current;
109 :    
110 :     if ((x > data->max_dx) || (x < data->min_dx) ||
111 :     (y > data->max_dy) || (y < data->min_dy))
112 :     return;
113 :    
114 :     if (Direction == 1) { /* x and y mean forward vector */
115 :     VECTOR backward = data->qpel_precision ? data->currentQMV[1] : data->currentMV[1];
116 :     xb = backward.x;
117 :     yb = backward.y;
118 :     xf = x; yf = y;
119 :     } else { /* x and y mean backward vector */
120 :     VECTOR forward = data->qpel_precision ? data->currentQMV[0] : data->currentMV[0];
121 :     xf = forward.x;
122 :     yf = forward.y;
123 :     xb = x; yb = y;
124 :     }
125 : syskin 1478
126 : edgomez 1382 if (!data->qpel_precision) {
127 :     ReferenceF = GetReference(xf, yf, data);
128 :     ReferenceB = GetReferenceB(xb, yb, 1, data);
129 :     current = data->currentMV + Direction - 1;
130 :     xcf = xf; ycf = yf;
131 :     xcb = xb; ycb = yb;
132 :     } else {
133 :     ReferenceF = xvid_me_interpolate16x16qpel(xf, yf, 0, data);
134 :     current = data->currentQMV + Direction - 1;
135 :     ReferenceB = xvid_me_interpolate16x16qpel(xb, yb, 1, data);
136 :     xcf = xf/2; ycf = yf/2;
137 :     xcb = xb/2; ycb = yb/2;
138 :     }
139 :    
140 : syskin 1564 t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision)
141 :     + d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision);
142 : edgomez 1382
143 :     sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
144 : Isibaar 1604 sad += (data->lambda16 * t);
145 : edgomez 1382
146 :     if (data->chroma && sad < *data->iMinSAD)
147 :     sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3],
148 :     (ycf >> 1) + roundtab_79[ycf & 0x3],
149 :     (xcb >> 1) + roundtab_79[xcb & 0x3],
150 :     (ycb >> 1) + roundtab_79[ycb & 0x3], data);
151 :    
152 :     if (sad < *(data->iMinSAD)) {
153 :     *data->iMinSAD = sad;
154 :     current->x = x; current->y = y;
155 :     data->dir = Direction;
156 :     }
157 :     }
158 :    
159 :     static void
160 :     CheckCandidateDirect(const int x, const int y, SearchData * const data, const unsigned int Direction)
161 :     {
162 :     int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0;
163 :     uint32_t k;
164 :     const uint8_t *ReferenceF;
165 :     const uint8_t *ReferenceB;
166 :     VECTOR mvs, b_mvs;
167 : syskin 1478 const int blocks[4] = {0, 8, 8*data->iEdgedWidth, 8*data->iEdgedWidth+8};
168 : edgomez 1382
169 :     if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
170 :    
171 :     for (k = 0; k < 4; k++) {
172 :     mvs.x = data->directmvF[k].x + x;
173 :     b_mvs.x = ((x == 0) ?
174 :     data->directmvB[k].x
175 :     : mvs.x - data->referencemv[k].x);
176 :    
177 :     mvs.y = data->directmvF[k].y + y;
178 :     b_mvs.y = ((y == 0) ?
179 :     data->directmvB[k].y
180 :     : mvs.y - data->referencemv[k].y);
181 :    
182 :     if ((mvs.x > data->max_dx) || (mvs.x < data->min_dx) ||
183 :     (mvs.y > data->max_dy) || (mvs.y < data->min_dy) ||
184 :     (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) ||
185 :     (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) )
186 :     return;
187 :    
188 :     if (data->qpel) {
189 :     xcf += mvs.x/2; ycf += mvs.y/2;
190 :     xcb += b_mvs.x/2; ycb += b_mvs.y/2;
191 : syskin 1478 if (data->qpel_precision) {
192 :     ReferenceF = xvid_me_interpolate8x8qpel(mvs.x, mvs.y, k, 0, data);
193 :     ReferenceB = xvid_me_interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data);
194 :     goto done;
195 :     }
196 :     mvs.x >>=1; mvs.y >>=1; b_mvs.x >>=1; b_mvs.y >>=1; // qpel->hpel
197 : edgomez 1382 } else {
198 :     xcf += mvs.x; ycf += mvs.y;
199 :     xcb += b_mvs.x; ycb += b_mvs.y;
200 :     }
201 : syskin 1478 ReferenceF = GetReference(mvs.x, mvs.y, data) + blocks[k];
202 :     ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data) + blocks[k];
203 :     done:
204 : syskin 1443 sad += data->iMinSAD[k+1] =
205 : syskin 1478 sad8bi(data->Cur + blocks[k],
206 : syskin 1443 ReferenceF, ReferenceB, data->iEdgedWidth);
207 : edgomez 1382 if (sad > *(data->iMinSAD)) return;
208 :     }
209 :    
210 : Isibaar 1604 sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0));
211 : edgomez 1382
212 :     if (data->chroma && sad < *data->iMinSAD)
213 :     sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],
214 :     (ycf >> 3) + roundtab_76[ycf & 0xf],
215 :     (xcb >> 3) + roundtab_76[xcb & 0xf],
216 :     (ycb >> 3) + roundtab_76[ycb & 0xf], data);
217 :    
218 :     if (sad < *(data->iMinSAD)) {
219 :     data->iMinSAD[0] = sad;
220 :     data->currentMV->x = x; data->currentMV->y = y;
221 :     data->dir = Direction;
222 :     }
223 :     }
224 :    
225 :     static void
226 :     CheckCandidateDirectno4v(const int x, const int y, SearchData * const data, const unsigned int Direction)
227 :     {
228 :     int32_t sad, xcf, ycf, xcb, ycb;
229 :     const uint8_t *ReferenceF;
230 :     const uint8_t *ReferenceB;
231 :     VECTOR mvs, b_mvs;
232 :    
233 :     if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
234 :    
235 :     mvs.x = data->directmvF[0].x + x;
236 :     b_mvs.x = ((x == 0) ?
237 :     data->directmvB[0].x
238 :     : mvs.x - data->referencemv[0].x);
239 :    
240 :     mvs.y = data->directmvF[0].y + y;
241 :     b_mvs.y = ((y == 0) ?
242 :     data->directmvB[0].y
243 :     : mvs.y - data->referencemv[0].y);
244 :    
245 :     if ( (mvs.x > data->max_dx) || (mvs.x < data->min_dx)
246 :     || (mvs.y > data->max_dy) || (mvs.y < data->min_dy)
247 :     || (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx)
248 :     || (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) return;
249 :    
250 :     if (data->qpel) {
251 :     xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2);
252 :     xcb = 4*(b_mvs.x/2); ycb = 4*(b_mvs.y/2);
253 : syskin 1478 if (data->qpel_precision) {
254 :     ReferenceF = xvid_me_interpolate16x16qpel(mvs.x, mvs.y, 0, data);
255 :     ReferenceB = xvid_me_interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data);
256 :     goto done;
257 :     }
258 :     mvs.x >>=1; mvs.y >>=1; b_mvs.x >>=1; b_mvs.y >>=1; // qpel->hpel
259 : edgomez 1382 } else {
260 :     xcf = 4*mvs.x; ycf = 4*mvs.y;
261 :     xcb = 4*b_mvs.x; ycb = 4*b_mvs.y;
262 :     }
263 : syskin 1478 ReferenceF = GetReference(mvs.x, mvs.y, data);
264 :     ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data);
265 : edgomez 1382
266 : syskin 1478 done:
267 : edgomez 1382 sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
268 : Isibaar 1604 sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0));
269 : edgomez 1382
270 :     if (data->chroma && sad < *data->iMinSAD)
271 :     sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],
272 :     (ycf >> 3) + roundtab_76[ycf & 0xf],
273 :     (xcb >> 3) + roundtab_76[xcb & 0xf],
274 :     (ycb >> 3) + roundtab_76[ycb & 0xf], data);
275 :    
276 :     if (sad < *(data->iMinSAD)) {
277 :     *(data->iMinSAD) = sad;
278 :     data->currentMV->x = x; data->currentMV->y = y;
279 :     data->dir = Direction;
280 :     }
281 :     }
282 :    
283 :     void
284 :     CheckCandidate16no4v(const int x, const int y, SearchData * const data, const unsigned int Direction)
285 :     {
286 :     int32_t sad, xc, yc;
287 :     const uint8_t * Reference;
288 :     uint32_t t;
289 :     VECTOR * current;
290 :    
291 :     if ( (x > data->max_dx) || ( x < data->min_dx)
292 :     || (y > data->max_dy) || (y < data->min_dy) ) return;
293 :    
294 :     if (data->qpel_precision) { /* x and y are in 1/4 precision */
295 :     Reference = xvid_me_interpolate16x16qpel(x, y, 0, data);
296 :     current = data->currentQMV;
297 :     xc = x/2; yc = y/2;
298 :     } else {
299 :     Reference = GetReference(x, y, data);
300 :     current = data->currentMV;
301 :     xc = x; yc = y;
302 :     }
303 :     t = d_mv_bits(x, y, data->predMV, data->iFcode,
304 : syskin 1564 data->qpel^data->qpel_precision);
305 : edgomez 1382
306 :     sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);
307 : Isibaar 1604 sad += (data->lambda16 * t);
308 : edgomez 1382
309 :     if (data->chroma && sad < *data->iMinSAD)
310 :     sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],
311 :     (yc >> 1) + roundtab_79[yc & 0x3], data);
312 :    
313 :     if (sad < *(data->iMinSAD)) {
314 :     *(data->iMinSAD) = sad;
315 :     current->x = x; current->y = y;
316 :     data->dir = Direction;
317 :     }
318 :     }
319 :    
320 : syskin 1478
321 :     static void
322 :     initialize_searchData(SearchData * Data_d,
323 :     SearchData * Data_f,
324 :     SearchData * Data_b,
325 :     SearchData * Data_i,
326 :     int x, int y,
327 :     const IMAGE * const f_Ref,
328 :     const uint8_t * const f_RefH,
329 :     const uint8_t * const f_RefV,
330 :     const uint8_t * const f_RefHV,
331 :     const IMAGE * const b_Ref,
332 :     const uint8_t * const b_RefH,
333 :     const uint8_t * const b_RefV,
334 :     const uint8_t * const b_RefHV,
335 :     const IMAGE * const pCur,
336 :     const MACROBLOCK * const b_mb)
337 :     {
338 :    
339 :     /* per-macroblock SearchData initialization - too many things would be repeated 4 times */
340 :     const uint8_t * RefP[6], * b_RefP[6], * Cur[3];
341 :     const uint32_t iEdgedWidth = Data_d->iEdgedWidth;
342 :     unsigned int lambda;
343 :     int i;
344 :    
345 :     /* luma */
346 :     int offset = (x + iEdgedWidth*y) * 16;
347 :     RefP[0] = f_Ref->y + offset;
348 :     RefP[2] = f_RefH + offset;
349 :     RefP[1] = f_RefV + offset;
350 :     RefP[3] = f_RefHV + offset;
351 :     b_RefP[0] = b_Ref->y + offset;
352 :     b_RefP[2] = b_RefH + offset;
353 :     b_RefP[1] = b_RefV + offset;
354 :     b_RefP[3] = b_RefHV + offset;
355 :     Cur[0] = pCur->y + offset;
356 :    
357 :     /* chroma */
358 :     offset = (x + (iEdgedWidth/2)*y) * 8;
359 :     RefP[4] = f_Ref->u + offset;
360 :     RefP[5] = f_Ref->v + offset;
361 :     b_RefP[4] = b_Ref->u + offset;
362 :     b_RefP[5] = b_Ref->v + offset;
363 :     Cur[1] = pCur->u + offset;
364 :     Cur[2] = pCur->v + offset;
365 :    
366 :     lambda = xvid_me_lambda_vec16[b_mb->quant];
367 :    
368 :     for (i = 0; i < 6; i++) {
369 :     Data_d->RefP[i] = Data_f->RefP[i] = Data_i->RefP[i] = RefP[i];
370 :     Data_d->b_RefP[i] = Data_b->RefP[i] = Data_i->b_RefP[i] = b_RefP[i];
371 :     }
372 :     Data_d->Cur = Data_f->Cur = Data_b->Cur = Data_i->Cur = Cur[0];
373 :     Data_d->CurU = Data_f->CurU = Data_b->CurU = Data_i->CurU = Cur[1];
374 :     Data_d->CurV = Data_f->CurV = Data_b->CurV = Data_i->CurV = Cur[2];
375 :    
376 : syskin 1515 Data_d->lambda16 = Data_f->lambda16 = Data_b->lambda16 = Data_i->lambda16 = lambda;
377 : syskin 1478
378 :     /* reset chroma-sad cache */
379 :     Data_d->b_chromaX = Data_d->b_chromaY = Data_d->chromaX = Data_d->chromaY = Data_d->chromaSAD = 256*4096;
380 :     Data_i->b_chromaX = Data_i->b_chromaY = Data_i->chromaX = Data_i->chromaY = Data_i->chromaSAD = 256*4096;
381 :     Data_f->chromaX = Data_f->chromaY = Data_f->chromaSAD = 256*4096;
382 :     Data_b->chromaX = Data_b->chromaY = Data_b->chromaSAD = 256*4096;
383 :    
384 :     *Data_d->iMinSAD = *Data_b->iMinSAD = *Data_f->iMinSAD = *Data_i->iMinSAD = 4096*256;
385 :     }
386 :    
387 : edgomez 1382 static __inline VECTOR
388 :     ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode)
389 :     {
390 :     /* the stupidiest function ever */
391 :     return (mode == MODE_FORWARD ? pMB->mvs[0] : pMB->b_mvs[0]);
392 :     }
393 :    
394 :     static void __inline
395 :     PreparePredictionsBF(VECTOR * const pmv, const int x, const int y,
396 :     const uint32_t iWcount,
397 :     const MACROBLOCK * const pMB,
398 : syskin 1478 const uint32_t mode_curr,
399 :     const VECTOR hint)
400 : edgomez 1382 {
401 :     /* [0] is prediction */
402 : syskin 1515 /* [1] is zero */
403 :     pmv[1].x = pmv[1].y = 0;
404 : edgomez 1382
405 : syskin 1478 pmv[2].x = hint.x; pmv[2].y = hint.y;
406 : edgomez 1382
407 :     if ((y != 0)&&(x != (int)(iWcount+1))) { /* [3] top-right neighbour */
408 :     pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr);
409 :     } else pmv[3].x = pmv[3].y = 0;
410 :    
411 :     if (y != 0) {
412 :     pmv[4] = ChoosePred(pMB-iWcount, mode_curr);
413 :     } else pmv[4].x = pmv[4].y = 0;
414 :    
415 :     if (x != 0) {
416 :     pmv[5] = ChoosePred(pMB-1, mode_curr);
417 :     } else pmv[5].x = pmv[5].y = 0;
418 :    
419 :     if (x != 0 && y != 0) {
420 :     pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr);
421 :     } else pmv[6].x = pmv[6].y = 0;
422 :     }
423 :    
424 :     /* search backward or forward */
425 :     static void
426 : syskin 1478 SearchBF_initial(const int x, const int y,
427 : edgomez 1382 const uint32_t MotionFlags,
428 :     const uint32_t iFcode,
429 :     const MBParam * const pParam,
430 :     MACROBLOCK * const pMB,
431 :     const VECTOR * const predMV,
432 :     int32_t * const best_sad,
433 :     const int32_t mode_current,
434 : syskin 1478 SearchData * const Data,
435 :     VECTOR hint)
436 : edgomez 1382 {
437 :    
438 :     int i;
439 :     VECTOR pmv[7];
440 :     *Data->iMinSAD = MV_MAX_ERROR;
441 :     Data->qpel_precision = 0;
442 :    
443 :     Data->predMV = *predMV;
444 :    
445 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
446 : syskin 1564 pParam->width, pParam->height, iFcode - Data->qpel, 1);
447 : edgomez 1382
448 :     pmv[0] = Data->predMV;
449 : syskin 1478 if (Data->qpel) {
450 :     pmv[0].x /= 2; pmv[0].y /= 2;
451 :     hint.x /= 2; hint.y /= 2;
452 :     }
453 : edgomez 1382
454 : syskin 1478 PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current, hint);
455 : edgomez 1382
456 :     Data->currentMV->x = Data->currentMV->y = 0;
457 :    
458 :     /* main loop. checking all predictions */
459 :     for (i = 0; i < 7; i++)
460 :     if (!vector_repeats(pmv, i) )
461 :     CheckCandidate16no4v(pmv[i].x, pmv[i].y, Data, i);
462 :    
463 :     if (*Data->iMinSAD > 512) {
464 :     unsigned int mask = make_mask(pmv, 7, Data->dir);
465 :    
466 :     MainSearchFunc *MainSearchPtr;
467 :     if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;
468 :     else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;
469 :     else MainSearchPtr = xvid_me_DiamondSearch;
470 :    
471 :     MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v);
472 :     }
473 :    
474 : syskin 1478 if (Data->iMinSAD[0] < *best_sad) *best_sad = Data->iMinSAD[0];
475 :     }
476 : edgomez 1382
477 : syskin 1478 static void
478 :     SearchBF_final(const int x, const int y,
479 :     const uint32_t MotionFlags,
480 :     const MBParam * const pParam,
481 :     int32_t * const best_sad,
482 :     SearchData * const Data)
483 :     {
484 : syskin 1441 if(!Data->qpel) {
485 :     /* halfpel mode */
486 :     if (MotionFlags & XVID_ME_HALFPELREFINE16)
487 : syskin 1478 xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate16no4v, 0);
488 : syskin 1441 } else {
489 :     /* qpel mode */
490 :     if(MotionFlags & XVID_ME_FASTREFINE16) {
491 :     /* fast */
492 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
493 : syskin 1564 pParam->width, pParam->height, Data->iFcode, 2);
494 : syskin 1478 FullRefine_Fast(Data, CheckCandidate16no4v, 0);
495 :    
496 : syskin 1441 } else {
497 : syskin 1478
498 : syskin 1441 Data->currentQMV->x = 2*Data->currentMV->x;
499 :     Data->currentQMV->y = 2*Data->currentMV->y;
500 :     if(MotionFlags & XVID_ME_QUARTERPELREFINE16) {
501 :     /* full */
502 :     if (MotionFlags & XVID_ME_HALFPELREFINE16) {
503 : syskin 1478 xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate16no4v, 0); /* hpel part */
504 : syskin 1441 Data->currentQMV->x = 2*Data->currentMV->x;
505 :     Data->currentQMV->y = 2*Data->currentMV->y;
506 :     }
507 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
508 : syskin 1564 pParam->width, pParam->height, Data->iFcode, 2);
509 : syskin 1441 Data->qpel_precision = 1;
510 : syskin 1478 xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate16no4v, 0); /* qpel part */
511 : syskin 1441 }
512 : edgomez 1382 }
513 :     }
514 : syskin 1478 if (Data->iMinSAD[0] < *best_sad) *best_sad = Data->iMinSAD[0];
515 : edgomez 1382
516 :     }
517 :    
518 :     static void
519 : syskin 1478 SkipDecisionB(MACROBLOCK * const pMB, const SearchData * const Data)
520 : edgomez 1382 {
521 :     int k;
522 :    
523 :     if (!Data->chroma) {
524 :     int dx = 0, dy = 0, b_dx = 0, b_dy = 0;
525 :     int32_t sum;
526 :     const uint32_t stride = Data->iEdgedWidth/2;
527 :     /* this is not full chroma compensation, only it's fullpel approximation. should work though */
528 :    
529 :     for (k = 0; k < 4; k++) {
530 :     dy += Data->directmvF[k].y >> Data->qpel;
531 :     dx += Data->directmvF[k].x >> Data->qpel;
532 :     b_dy += Data->directmvB[k].y >> Data->qpel;
533 :     b_dx += Data->directmvB[k].x >> Data->qpel;
534 :     }
535 :    
536 :     dy = (dy >> 3) + roundtab_76[dy & 0xf];
537 :     dx = (dx >> 3) + roundtab_76[dx & 0xf];
538 :     b_dy = (b_dy >> 3) + roundtab_76[b_dy & 0xf];
539 :     b_dx = (b_dx >> 3) + roundtab_76[b_dx & 0xf];
540 :    
541 : syskin 1478 sum = sad8bi(Data->CurU,
542 : edgomez 1556 Data->RefP[4] + (dy/2) * (int)stride + dx/2,
543 :     Data->b_RefP[4] + (b_dy/2) * (int)stride + b_dx/2,
544 : edgomez 1382 stride);
545 :    
546 :     if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */
547 :    
548 : syskin 1478 sum += sad8bi(Data->CurV,
549 : edgomez 1556 Data->RefP[5] + (dy/2) * (int)stride + dx/2,
550 :     Data->b_RefP[5] + (b_dy/2) * (int)stride + b_dx/2,
551 : edgomez 1382 stride);
552 : syskin 1481
553 : edgomez 1382 if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */
554 : syskin 1481 } else {
555 :     int sum = Data->chromaSAD; /* chroma-sad SAD caching keeps it there */
556 :    
557 :     if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */
558 : edgomez 1382 }
559 :    
560 :     /* skip */
561 :     pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */
562 :     for (k = 0; k < 4; k++) {
563 :     pMB->qmvs[k] = pMB->mvs[k] = Data->directmvF[k];
564 :     pMB->b_qmvs[k] = pMB->b_mvs[k] = Data->directmvB[k];
565 : syskin 1517 if (Data->qpel) {
566 :     pMB->mvs[k].x /= 2; pMB->mvs[k].y /= 2; /* it's a hint for future searches */
567 :     pMB->b_mvs[k].x /= 2; pMB->b_mvs[k].y /= 2;
568 :     }
569 : edgomez 1382 }
570 :     }
571 :    
572 :     static uint32_t
573 : syskin 1478 SearchDirect_initial(const int x, const int y,
574 : edgomez 1382 const uint32_t MotionFlags,
575 :     const int32_t TRB, const int32_t TRD,
576 :     const MBParam * const pParam,
577 :     MACROBLOCK * const pMB,
578 :     const MACROBLOCK * const b_mb,
579 :     int32_t * const best_sad,
580 :     SearchData * const Data)
581 :    
582 :     {
583 :     int32_t skip_sad;
584 :     int k = (x + Data->iEdgedWidth*y) * 16;
585 :    
586 :     k = Data->qpel ? 4 : 2;
587 :     Data->max_dx = k * (pParam->width - x * 16);
588 :     Data->max_dy = k * (pParam->height - y * 16);
589 :     Data->min_dx = -k * (16 + x * 16);
590 :     Data->min_dy = -k * (16 + y * 16);
591 :    
592 :     Data->referencemv = Data->qpel ? b_mb->qmvs : b_mb->mvs;
593 :    
594 :     for (k = 0; k < 4; k++) {
595 : syskin 1478 Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD);
596 :     Data->directmvB[k].x = ((TRB - TRD) * Data->referencemv[k].x) / TRD;
597 :     Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD);
598 :     Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD;
599 : edgomez 1382
600 : syskin 1478 if ( (Data->directmvB[k].x > Data->max_dx) | (Data->directmvB[k].x < Data->min_dx)
601 :     | (Data->directmvB[k].y > Data->max_dy) | (Data->directmvB[k].y < Data->min_dy) ) {
602 : edgomez 1382
603 : syskin 1478 Data->iMinSAD[0] = *best_sad = 256*4096; /* in that case, we won't use direct mode */
604 : edgomez 1382 return 256*4096;
605 :     }
606 :     if (b_mb->mode != MODE_INTER4V) {
607 :     Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0];
608 :     Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0];
609 :     break;
610 :     }
611 :     }
612 : syskin 1478 Data->qpel_precision = Data->qpel; /* this initial check is done with full precision, to find real
613 :     SKIP sad */
614 : edgomez 1382
615 : syskin 1443 CheckCandidateDirect(0, 0, Data, 255); /* will also fill iMinSAD[1..4] with 8x8 SADs */
616 : edgomez 1382
617 :     /* initial (fast) skip decision */
618 : syskin 1443 if (Data->iMinSAD[1] < (int)Data->iQuant * INITIAL_SKIP_THRESH
619 :     && Data->iMinSAD[2] < (int)Data->iQuant * INITIAL_SKIP_THRESH
620 :     && Data->iMinSAD[3] < (int)Data->iQuant * INITIAL_SKIP_THRESH
621 :     && Data->iMinSAD[4] < (int)Data->iQuant * INITIAL_SKIP_THRESH) {
622 : edgomez 1382 /* possible skip */
623 : syskin 1478 SkipDecisionB(pMB, Data);
624 : syskin 1443 if (pMB->mode == MODE_DIRECT_NONE_MV)
625 :     return *Data->iMinSAD; /* skipped */
626 : edgomez 1382 }
627 :    
628 : syskin 1516 if (Data->chroma && Data->chromaSAD >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) /* chroma doesn't allow skip */
629 :     skip_sad = 256*4096;
630 :     else
631 :     skip_sad = 4*MAX(MAX(Data->iMinSAD[1],Data->iMinSAD[2]), MAX(Data->iMinSAD[3],Data->iMinSAD[4]));
632 : edgomez 1382
633 : syskin 1478 Data->currentMV[1].x = Data->directmvF[0].x + Data->currentMV->x; /* hints for forward and backward searches */
634 :     Data->currentMV[1].y = Data->directmvF[0].y + Data->currentMV->y;
635 : syskin 1443
636 : syskin 1478 Data->currentMV[2].x = ((Data->currentMV->x == 0) ?
637 :     Data->directmvB[0].x
638 :     : Data->currentMV[1].x - Data->referencemv[0].x);
639 : edgomez 1382
640 : syskin 1478 Data->currentMV[2].y = ((Data->currentMV->y == 0) ?
641 :     Data->directmvB[0].y
642 :     : Data->currentMV[1].y - Data->referencemv[0].y);
643 : edgomez 1382
644 : syskin 1548 *best_sad = Data->iMinSAD[0];
645 :    
646 : syskin 1478 return skip_sad;
647 :     }
648 : edgomez 1382
649 : syskin 1478 static void
650 :     SearchDirect_final( const uint32_t MotionFlags,
651 :     const MACROBLOCK * const b_mb,
652 :     int32_t * const best_sad,
653 :     SearchData * const Data)
654 : edgomez 1382
655 : syskin 1478 {
656 :     CheckFunc * CheckCandidate = b_mb->mode == MODE_INTER4V ?
657 :     CheckCandidateDirect : CheckCandidateDirectno4v;
658 :     MainSearchFunc *MainSearchPtr;
659 : edgomez 1382
660 : syskin 1478 if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;
661 :     else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;
662 :     else MainSearchPtr = xvid_me_DiamondSearch;
663 : edgomez 1382
664 : syskin 1478 Data->qpel_precision = 0;
665 :     MainSearchPtr(0, 0, Data, 255, CheckCandidate);
666 : edgomez 1382
667 : syskin 1478 Data->qpel_precision = Data->qpel;
668 :     if(Data->qpel) {
669 :     *Data->iMinSAD = 256*4096; /* this old SAD was not real, it was in hpel precision */
670 :     CheckCandidate(Data->currentMV->x, Data->currentMV->y, Data, 255);
671 : edgomez 1382 }
672 : syskin 1478
673 :     xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0);
674 :    
675 :     if (Data->iMinSAD[0] < *best_sad) {
676 :     *best_sad = Data->iMinSAD[0];
677 :     }
678 :    
679 : edgomez 1382 }
680 :    
681 :    
682 : syskin 1478 static __inline void
683 :     set_range(int * range, SearchData * Data)
684 : edgomez 1382 {
685 :     Data->min_dx = range[0];
686 :     Data->max_dx = range[1];
687 :     Data->min_dy = range[2];
688 :     Data->max_dy = range[3];
689 :     }
690 :    
691 :     static void
692 : syskin 1478 SearchInterpolate_initial(
693 : edgomez 1382 const int x, const int y,
694 :     const uint32_t MotionFlags,
695 :     const MBParam * const pParam,
696 :     const VECTOR * const f_predMV,
697 :     const VECTOR * const b_predMV,
698 :     int32_t * const best_sad,
699 : syskin 1478 SearchData * const Data,
700 :     const VECTOR startF,
701 :     const VECTOR startB)
702 : edgomez 1382
703 :     {
704 :     int b_range[4], f_range[4];
705 :    
706 :     Data->qpel_precision = 0;
707 :    
708 :     Data->predMV = *f_predMV;
709 :     Data->bpredMV = *b_predMV;
710 :    
711 : syskin 1478 Data->currentMV[0] = startF;
712 :     Data->currentMV[1] = startB;
713 : edgomez 1382
714 : syskin 1564 get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, Data->iFcode - Data->qpel, 1);
715 :     get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, Data->bFcode - Data->qpel, 1);
716 : edgomez 1382
717 :     if (Data->currentMV[0].x > f_range[1]) Data->currentMV[0].x = f_range[1];
718 :     if (Data->currentMV[0].x < f_range[0]) Data->currentMV[0].x = f_range[0];
719 :     if (Data->currentMV[0].y > f_range[3]) Data->currentMV[0].y = f_range[3];
720 :     if (Data->currentMV[0].y < f_range[2]) Data->currentMV[0].y = f_range[2];
721 :    
722 :     if (Data->currentMV[1].x > b_range[1]) Data->currentMV[1].x = b_range[1];
723 :     if (Data->currentMV[1].x < b_range[0]) Data->currentMV[1].x = b_range[0];
724 :     if (Data->currentMV[1].y > b_range[3]) Data->currentMV[1].y = b_range[3];
725 :     if (Data->currentMV[1].y < b_range[2]) Data->currentMV[1].y = b_range[2];
726 :    
727 :     set_range(f_range, Data);
728 :    
729 :     CheckCandidateInt(Data->currentMV[0].x, Data->currentMV[0].y, Data, 1);
730 :    
731 : syskin 1478 if (Data->iMinSAD[0] < *best_sad) *best_sad = Data->iMinSAD[0];
732 :     }
733 :    
734 :     static void
735 :     SearchInterpolate_final(const int x, const int y,
736 :     const uint32_t MotionFlags,
737 :     const MBParam * const pParam,
738 :     int32_t * const best_sad,
739 :     SearchData * const Data)
740 :     {
741 :     int i, j;
742 :     int b_range[4], f_range[4];
743 :    
744 : syskin 1564 get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, Data->iFcode - Data->qpel, 1);
745 :     get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, Data->bFcode - Data->qpel, 1);
746 : syskin 1478
747 : edgomez 1382 /* diamond */
748 :     do {
749 :     Data->dir = 0;
750 :     /* forward MV moves */
751 :     i = Data->currentMV[0].x; j = Data->currentMV[0].y;
752 :    
753 :     CheckCandidateInt(i + 1, j, Data, 1);
754 :     CheckCandidateInt(i, j + 1, Data, 1);
755 :     CheckCandidateInt(i - 1, j, Data, 1);
756 :     CheckCandidateInt(i, j - 1, Data, 1);
757 :    
758 :     /* backward MV moves */
759 :     set_range(b_range, Data);
760 :     i = Data->currentMV[1].x; j = Data->currentMV[1].y;
761 :    
762 :     CheckCandidateInt(i + 1, j, Data, 2);
763 :     CheckCandidateInt(i, j + 1, Data, 2);
764 :     CheckCandidateInt(i - 1, j, Data, 2);
765 :     CheckCandidateInt(i, j - 1, Data, 2);
766 :    
767 :     set_range(f_range, Data);
768 :    
769 :     } while (Data->dir != 0);
770 :    
771 :     /* qpel refinement */
772 :     if (Data->qpel) {
773 :     Data->qpel_precision = 1;
774 : syskin 1478 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy,
775 : syskin 1564 x, y, 4, pParam->width, pParam->height, Data->iFcode, 2);
776 : edgomez 1382
777 :     Data->currentQMV[0].x = 2 * Data->currentMV[0].x;
778 :     Data->currentQMV[0].y = 2 * Data->currentMV[0].y;
779 :     Data->currentQMV[1].x = 2 * Data->currentMV[1].x;
780 :     Data->currentQMV[1].y = 2 * Data->currentMV[1].y;
781 :    
782 : syskin 1478 if (MotionFlags & XVID_ME_QUARTERPELREFINE16) {
783 :     xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidateInt, 1);
784 : edgomez 1382
785 : syskin 1478 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy,
786 : syskin 1564 x, y, 4, pParam->width, pParam->height, Data->bFcode, 2);
787 : edgomez 1382
788 : syskin 1478 xvid_me_SubpelRefine(Data->currentQMV[1], Data, CheckCandidateInt, 2);
789 :     }
790 : edgomez 1382 }
791 :    
792 : syskin 1478 if (Data->iMinSAD[0] < *best_sad) *best_sad = Data->iMinSAD[0];
793 :     }
794 : edgomez 1382
795 : syskin 1478 static void
796 :     ModeDecision_BVOP_SAD(const SearchData * const Data_d,
797 :     const SearchData * const Data_b,
798 :     const SearchData * const Data_f,
799 :     const SearchData * const Data_i,
800 :     MACROBLOCK * const pMB,
801 :     const MACROBLOCK * const b_mb,
802 :     VECTOR * f_predMV,
803 : Isibaar 1919 VECTOR * b_predMV,
804 :     int force_direct)
805 : syskin 1478 {
806 :     int mode = MODE_DIRECT, k;
807 :     int best_sad, f_sad, b_sad, i_sad;
808 :     const int qpel = Data_d->qpel;
809 :    
810 :     /* evaluate cost of all modes - quite simple in SAD */
811 :     best_sad = Data_d->iMinSAD[0] + 1*Data_d->lambda16;
812 :     b_sad = Data_b->iMinSAD[0] + 3*Data_d->lambda16;
813 :     f_sad = Data_f->iMinSAD[0] + 4*Data_d->lambda16;
814 :     i_sad = Data_i->iMinSAD[0] + 2*Data_d->lambda16;
815 :    
816 : Isibaar 1919 if (force_direct)
817 :     goto set_mode; /* bypass checks for non-direct modes */
818 :    
819 : syskin 1478 if (b_sad < best_sad) {
820 :     mode = MODE_BACKWARD;
821 :     best_sad = b_sad;
822 :     }
823 :    
824 :     if (f_sad < best_sad) {
825 :     mode = MODE_FORWARD;
826 :     best_sad = f_sad;
827 :     }
828 :    
829 :     if (i_sad < best_sad) {
830 :     mode = MODE_INTERPOLATE;
831 :     best_sad = i_sad;
832 :     }
833 :    
834 : Isibaar 1919 set_mode:
835 : syskin 1478 pMB->sad16 = best_sad;
836 :     pMB->mode = mode;
837 : syskin 1568 pMB->cbp = 63;
838 : syskin 1478
839 :     switch (mode) {
840 :    
841 :     case MODE_DIRECT:
842 :     if (!qpel && b_mb->mode != MODE_INTER4V) pMB->mode = MODE_DIRECT_NO4V; /* for faster compensation */
843 :    
844 :     pMB->pmvs[3] = Data_d->currentMV[0];
845 :    
846 :     for (k = 0; k < 4; k++) {
847 :     pMB->mvs[k].x = Data_d->directmvF[k].x + Data_d->currentMV->x;
848 :     pMB->b_mvs[k].x = ( (Data_d->currentMV->x == 0)
849 :     ? Data_d->directmvB[k].x
850 :     :pMB->mvs[k].x - Data_d->referencemv[k].x);
851 :     pMB->mvs[k].y = (Data_d->directmvF[k].y + Data_d->currentMV->y);
852 :     pMB->b_mvs[k].y = ((Data_d->currentMV->y == 0)
853 :     ? Data_d->directmvB[k].y
854 :     : pMB->mvs[k].y - Data_d->referencemv[k].y);
855 :     if (qpel) {
856 :     pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2;
857 :     pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2;
858 :     pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2;
859 :     pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2;
860 :     }
861 :    
862 :     if (b_mb->mode != MODE_INTER4V) {
863 :     pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0];
864 :     pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0];
865 :     pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0];
866 :     pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0];
867 :     break;
868 :     }
869 :     }
870 :     break;
871 :    
872 :     case MODE_FORWARD:
873 :     if (qpel) {
874 :     pMB->pmvs[0].x = Data_f->currentQMV->x - f_predMV->x;
875 :     pMB->pmvs[0].y = Data_f->currentQMV->y - f_predMV->y;
876 :     pMB->qmvs[0] = *Data_f->currentQMV;
877 :     *f_predMV = Data_f->currentQMV[0];
878 :     } else {
879 :     pMB->pmvs[0].x = Data_f->currentMV->x - f_predMV->x;
880 :     pMB->pmvs[0].y = Data_f->currentMV->y - f_predMV->y;
881 :     *f_predMV = Data_f->currentMV[0];
882 :     }
883 :     pMB->mvs[0] = *Data_f->currentMV;
884 : syskin 1515 pMB->b_mvs[0] = *Data_b->currentMV; /* hint for future searches */
885 : syskin 1478 break;
886 :    
887 :     case MODE_BACKWARD:
888 :     if (qpel) {
889 :     pMB->pmvs[0].x = Data_b->currentQMV->x - b_predMV->x;
890 :     pMB->pmvs[0].y = Data_b->currentQMV->y - b_predMV->y;
891 :     pMB->b_qmvs[0] = *Data_b->currentQMV;
892 :     *b_predMV = Data_b->currentQMV[0];
893 :     } else {
894 :     pMB->pmvs[0].x = Data_b->currentMV->x - b_predMV->x;
895 :     pMB->pmvs[0].y = Data_b->currentMV->y - b_predMV->y;
896 :     *b_predMV = Data_b->currentMV[0];
897 :     }
898 :     pMB->b_mvs[0] = *Data_b->currentMV;
899 : syskin 1515 pMB->mvs[0] = *Data_f->currentMV; /* hint for future searches */
900 : syskin 1478 break;
901 :    
902 :    
903 :     case MODE_INTERPOLATE:
904 :     pMB->mvs[0] = Data_i->currentMV[0];
905 :     pMB->b_mvs[0] = Data_i->currentMV[1];
906 :     if (qpel) {
907 :     pMB->qmvs[0] = Data_i->currentQMV[0];
908 :     pMB->b_qmvs[0] = Data_i->currentQMV[1];
909 : edgomez 1382 pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x;
910 :     pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y;
911 :     pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x;
912 :     pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y;
913 : syskin 1478 *f_predMV = Data_i->currentQMV[0];
914 :     *b_predMV = Data_i->currentQMV[1];
915 : edgomez 1382 } else {
916 :     pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x;
917 :     pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y;
918 :     pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x;
919 :     pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y;
920 : syskin 1478 *f_predMV = Data_i->currentMV[0];
921 :     *b_predMV = Data_i->currentMV[1];
922 : edgomez 1382 }
923 : syskin 1478 break;
924 : edgomez 1382 }
925 :     }
926 :    
927 : syskin 1567 static __inline void
928 :     maxMotionBVOP(int * const MVmaxF, int * const MVmaxB, const MACROBLOCK * const pMB, const int qpel)
929 :     {
930 :     if (pMB->mode == MODE_FORWARD || pMB->mode == MODE_INTERPOLATE) {
931 :     const VECTOR * const mv = qpel ? pMB->qmvs : pMB->mvs;
932 :     int max = *MVmaxF;
933 :     if (mv[0].x > max) max = mv[0].x;
934 :     else if (-mv[0].x - 1 > max) max = -mv[0].x - 1;
935 :     if (mv[0].y > max) max = mv[0].y;
936 :     else if (-mv[0].y - 1 > max) max = -mv[0].y - 1;
937 :    
938 :     *MVmaxF = max;
939 :     }
940 :    
941 :     if (pMB->mode == MODE_BACKWARD || pMB->mode == MODE_INTERPOLATE) {
942 :     const VECTOR * const mv = qpel ? pMB->b_qmvs : pMB->b_mvs;
943 :     int max = *MVmaxB;
944 :     if (mv[0].x > max) max = mv[0].x;
945 :     else if (-mv[0].x - 1 > max) max = -mv[0].x - 1;
946 :     if (mv[0].y > max) max = mv[0].y;
947 :     else if (-mv[0].y - 1 > max) max = -mv[0].y - 1;
948 :     *MVmaxB = max;
949 :     }
950 :     }
951 :    
952 :    
953 : edgomez 1382 void
954 :     MotionEstimationBVOP(MBParam * const pParam,
955 :     FRAMEINFO * const frame,
956 :     const int32_t time_bp,
957 :     const int32_t time_pp,
958 :     /* forward (past) reference */
959 :     const MACROBLOCK * const f_mbs,
960 :     const IMAGE * const f_ref,
961 :     const IMAGE * const f_refH,
962 :     const IMAGE * const f_refV,
963 :     const IMAGE * const f_refHV,
964 :     /* backward (future) reference */
965 :     const FRAMEINFO * const b_reference,
966 :     const IMAGE * const b_ref,
967 :     const IMAGE * const b_refH,
968 :     const IMAGE * const b_refV,
969 : Isibaar 1919 const IMAGE * const b_refHV,
970 :     const int num_slices)
971 : edgomez 1382 {
972 :     uint32_t i, j;
973 : edgomez 1547 int32_t best_sad = 256*4096;
974 : edgomez 1382 uint32_t skip_sad;
975 : syskin 1575 int fb_thresh;
976 : edgomez 1382 const MACROBLOCK * const b_mbs = b_reference->mbs;
977 :    
978 :     VECTOR f_predMV, b_predMV;
979 :    
980 : Isibaar 1919 int mb_width = pParam->mb_width;
981 :     int mb_height = pParam->mb_height;
982 : syskin 1567 int MVmaxF = 0, MVmaxB = 0;
983 : edgomez 1382 const int32_t TRB = time_pp - time_bp;
984 :     const int32_t TRD = time_pp;
985 : syskin 1506 DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);
986 : edgomez 1382
987 :     /* some pre-inintialized data for the rest of the search */
988 : syskin 1478 SearchData Data_d, Data_f, Data_b, Data_i;
989 :     memset(&Data_d, 0, sizeof(SearchData));
990 : edgomez 1382
991 : syskin 1478 Data_d.iEdgedWidth = pParam->edged_width;
992 :     Data_d.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0;
993 :     Data_d.rounding = 0;
994 :     Data_d.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP;
995 :     Data_d.iQuant = frame->quant;
996 : syskin 1569 Data_d.quant_sq = frame->quant*frame->quant;
997 : syskin 1506 Data_d.dctSpace = dct_space;
998 :     Data_d.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
999 :     Data_d.mpeg_quant_matrices = pParam->mpeg_quant_matrices;
1000 : edgomez 1382
1001 : syskin 1478 Data_d.RefQ = f_refV->u; /* a good place, also used in MC (for similar purpose) */
1002 : edgomez 1382
1003 : syskin 1478 memcpy(&Data_f, &Data_d, sizeof(SearchData));
1004 :     memcpy(&Data_b, &Data_d, sizeof(SearchData));
1005 :     memcpy(&Data_i, &Data_d, sizeof(SearchData));
1006 : edgomez 1382
1007 : syskin 1567 Data_f.iFcode = Data_i.iFcode = frame->fcode = b_reference->fcode;
1008 :     Data_b.iFcode = Data_i.bFcode = frame->bcode = b_reference->fcode;
1009 : syskin 1478
1010 : edgomez 1382 for (j = 0; j < pParam->mb_height; j++) {
1011 : Isibaar 1919 int new_bound = mb_width * ((((j*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1012 : edgomez 1382
1013 :     f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */
1014 :    
1015 :     for (i = 0; i < pParam->mb_width; i++) {
1016 :     MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;
1017 :     const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;
1018 : Isibaar 1919 int force_direct = (((j*mb_width+i)==new_bound) && (j > 0)) ? 1 : 0; /* MTK decoder chipsets do NOT reset predMVs upon resync marker in BVOPs. We workaround this problem
1019 :     by placing the slice border on second MB in a row and then force the first MB to be direct mode */
1020 :    
1021 : syskin 1478 pMB->mode = -1;
1022 : edgomez 1382
1023 : syskin 1478 initialize_searchData(&Data_d, &Data_f, &Data_b, &Data_i,
1024 :     i, j, f_ref, f_refH->y, f_refV->y, f_refHV->y,
1025 :     b_ref, b_refH->y, b_refV->y, b_refHV->y,
1026 :     &frame->image, b_mb);
1027 :    
1028 : edgomez 1382 /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */
1029 :     if (b_reference->coding_type != S_VOP)
1030 :     if (b_mb->mode == MODE_NOT_CODED) {
1031 :     pMB->mode = MODE_NOT_CODED;
1032 :     pMB->mvs[0] = pMB->b_mvs[0] = zeroMV;
1033 :     pMB->sad16 = 0;
1034 :     continue;
1035 :     }
1036 :    
1037 :     /* direct search comes first, because it (1) checks for SKIP-mode
1038 :     and (2) sets very good predictions for forward and backward search */
1039 : syskin 1478 skip_sad = SearchDirect_initial(i, j, frame->motion_flags, TRB, TRD, pParam, pMB,
1040 :     b_mb, &best_sad, &Data_d);
1041 : edgomez 1382
1042 :     if (pMB->mode == MODE_DIRECT_NONE_MV) {
1043 :     pMB->sad16 = best_sad;
1044 : syskin 1568 pMB->cbp = 0;
1045 : edgomez 1382 continue;
1046 :     }
1047 : syskin 1515
1048 : syskin 1478 SearchBF_initial(i, j, frame->motion_flags, frame->fcode, pParam, pMB,
1049 :     &f_predMV, &best_sad, MODE_FORWARD, &Data_f, Data_d.currentMV[1]);
1050 : edgomez 1382
1051 : syskin 1478 SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB,
1052 :     &b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2]);
1053 : edgomez 1382
1054 : syskin 1575 if (frame->motion_flags&XVID_ME_BFRAME_EARLYSTOP)
1055 :     fb_thresh = best_sad;
1056 :     else
1057 :     fb_thresh = best_sad + (best_sad>>1);
1058 : edgomez 1382
1059 : syskin 1575 if (Data_f.iMinSAD[0] <= fb_thresh)
1060 : syskin 1478 SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_f);
1061 : edgomez 1382
1062 : syskin 1575 if (Data_b.iMinSAD[0] <= fb_thresh)
1063 : syskin 1478 SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_b);
1064 : edgomez 1382
1065 : syskin 1478 SearchInterpolate_initial(i, j, frame->motion_flags, pParam, &f_predMV, &b_predMV, &best_sad,
1066 :     &Data_i, Data_f.currentMV[0], Data_b.currentMV[0]);
1067 : edgomez 1382
1068 : syskin 1575 if (((Data_i.iMinSAD[0] < best_sad +(best_sad>>3)) && !(frame->motion_flags&XVID_ME_FAST_MODEINTERPOLATE))
1069 : edgomez 1485 || Data_i.iMinSAD[0] <= best_sad)
1070 : edgomez 1382
1071 : syskin 1478 SearchInterpolate_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_i);
1072 :    
1073 : syskin 1575 if (Data_d.iMinSAD[0] <= 2*best_sad)
1074 :     if ((!(frame->motion_flags&XVID_ME_SKIP_DELTASEARCH) && (best_sad > 750))
1075 :     || (best_sad > 1000))
1076 :    
1077 :     SearchDirect_final(frame->motion_flags, b_mb, &best_sad, &Data_d);
1078 : edgomez 1382
1079 : syskin 1478 /* final skip decision */
1080 : syskin 1516 if ( (skip_sad < 2 * Data_d.iQuant * MAX_SAD00_FOR_SKIP )
1081 : syskin 1478 && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) {
1082 : edgomez 1382
1083 : syskin 1517 Data_d.chromaSAD = 0; /* green light for chroma check */
1084 :    
1085 :     SkipDecisionB(pMB, &Data_d);
1086 :    
1087 :     if (pMB->mode == MODE_DIRECT_NONE_MV) { /* skipped? */
1088 : syskin 1516 pMB->sad16 = skip_sad;
1089 : syskin 1568 pMB->cbp = 0;
1090 : syskin 1478 continue;
1091 :     }
1092 : edgomez 1382 }
1093 :    
1094 : syskin 1506 if (frame->vop_flags & XVID_VOP_RD_BVOP)
1095 :     ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i,
1096 : Isibaar 1919 pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, frame->vop_flags, pParam, i, j, best_sad, force_direct);
1097 : syskin 1506 else
1098 : Isibaar 1919 ModeDecision_BVOP_SAD(&Data_d, &Data_b, &Data_f, &Data_i, pMB, b_mb, &f_predMV, &b_predMV, force_direct);
1099 : edgomez 1382
1100 : syskin 1567 maxMotionBVOP(&MVmaxF, &MVmaxB, pMB, Data_d.qpel);
1101 :    
1102 : edgomez 1382 }
1103 :     }
1104 : syskin 1567
1105 :     frame->fcode = getMinFcode(MVmaxF);
1106 :     frame->bcode = getMinFcode(MVmaxB);
1107 : edgomez 1382 }
1108 : syskin 1682
1109 :    
1110 :    
1111 :     void
1112 : Isibaar 1913 SMPMotionEstimationBVOP(SMPData * h)
1113 : syskin 1682 {
1114 : Isibaar 1913 Encoder *pEnc = (Encoder *) h->pEnc;
1115 :    
1116 :     const MBParam * const pParam = &pEnc->mbParam;
1117 : syskin 1682 const FRAMEINFO * const frame = h->current;
1118 : Isibaar 1913 const int32_t time_bp = (int32_t)(pEnc->current->stamp - frame->stamp);
1119 :     const int32_t time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp);
1120 : syskin 1682 /* forward (past) reference */
1121 : Isibaar 1913 const IMAGE * const f_ref = &pEnc->reference->image;
1122 :     const IMAGE * const f_refH = &pEnc->f_refh;
1123 :     const IMAGE * const f_refV = &pEnc->f_refv;
1124 :     const IMAGE * const f_refHV = &pEnc->f_refhv;
1125 : syskin 1682 /* backward (future) reference */
1126 : Isibaar 1913 const FRAMEINFO * const b_reference = pEnc->current;
1127 :     const IMAGE * const b_ref = &pEnc->current->image;
1128 :     const IMAGE * const b_refH = &pEnc->vInterH;
1129 :     const IMAGE * const b_refV = &pEnc->vInterV;
1130 :     const IMAGE * const b_refHV = &pEnc->vInterHV;
1131 : syskin 1682
1132 : Isibaar 1919 int mb_width = pParam->mb_width;
1133 :     int mb_height = pParam->mb_height;
1134 :     int num_slices = pEnc->num_slices;
1135 : Isibaar 1913 int y_row = h->y_row;
1136 : syskin 1682 int y_step = h->y_step;
1137 :     int start_y = h->start_y;
1138 : Isibaar 1913 int stop_y = h->stop_y;
1139 : syskin 1682 int * complete_count_self = h->complete_count_self;
1140 :     const int * complete_count_above = h->complete_count_above;
1141 :     int max_mbs;
1142 :     int current_mb = 0;
1143 :    
1144 : syskin 1687 int32_t i, j;
1145 : syskin 1682 int32_t best_sad = 256*4096;
1146 :     uint32_t skip_sad;
1147 :     int fb_thresh;
1148 :     const MACROBLOCK * const b_mbs = b_reference->mbs;
1149 :    
1150 :     VECTOR f_predMV, b_predMV;
1151 :    
1152 :     int MVmaxF = 0, MVmaxB = 0;
1153 :     const int32_t TRB = time_pp - time_bp;
1154 :     const int32_t TRD = time_pp;
1155 :     DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);
1156 :    
1157 :     /* some pre-inintialized data for the rest of the search */
1158 :     SearchData Data_d, Data_f, Data_b, Data_i;
1159 :     memset(&Data_d, 0, sizeof(SearchData));
1160 :    
1161 :     Data_d.iEdgedWidth = pParam->edged_width;
1162 :     Data_d.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0;
1163 :     Data_d.rounding = 0;
1164 :     Data_d.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP;
1165 :     Data_d.iQuant = frame->quant;
1166 :     Data_d.quant_sq = frame->quant*frame->quant;
1167 :     Data_d.dctSpace = dct_space;
1168 :     Data_d.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
1169 :     Data_d.mpeg_quant_matrices = pParam->mpeg_quant_matrices;
1170 :    
1171 :     Data_d.RefQ = h->RefQ;
1172 :    
1173 :     memcpy(&Data_f, &Data_d, sizeof(SearchData));
1174 :     memcpy(&Data_b, &Data_d, sizeof(SearchData));
1175 :     memcpy(&Data_i, &Data_d, sizeof(SearchData));
1176 :    
1177 :     Data_f.iFcode = Data_i.iFcode = frame->fcode;
1178 :     Data_b.iFcode = Data_i.bFcode = frame->bcode;
1179 :    
1180 :     max_mbs = 0;
1181 :    
1182 : Isibaar 1913 for (j = (start_y+y_row); j < stop_y; j += y_step) {
1183 : Isibaar 1919 int new_bound = mb_width * ((((j*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1184 :    
1185 : Isibaar 1913 if (j == start_y) max_mbs = pParam->mb_width; /* we can process all blocks of the first row */
1186 : syskin 1682
1187 :     f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */
1188 :    
1189 : Isibaar 1913 for (i = 0; i < (int) pParam->mb_width; i++) {
1190 : syskin 1682 MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;
1191 :     const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;
1192 : Isibaar 1919 int force_direct = (((j*mb_width+i)==new_bound) && (j > 0)) ? 1 : 0; /* MTK decoder chipsets do NOT reset predMVs upon resync marker in BVOPs. We workaround this problem
1193 :     by placing the slice border on second MB in a row and then force the first MB to be direct mode */
1194 : syskin 1682 pMB->mode = -1;
1195 :    
1196 :     initialize_searchData(&Data_d, &Data_f, &Data_b, &Data_i,
1197 :     i, j, f_ref, f_refH->y, f_refV->y, f_refHV->y,
1198 :     b_ref, b_refH->y, b_refV->y, b_refHV->y,
1199 :     &frame->image, b_mb);
1200 :    
1201 :     if (current_mb >= max_mbs) {
1202 :     /* we ME-ed all macroblocks we safely could. grab next portion */
1203 :     int above_count = *complete_count_above; /* sync point */
1204 :     if (above_count == pParam->mb_width) {
1205 :     /* full line above is ready */
1206 :     above_count = pParam->mb_width+1;
1207 : Isibaar 1913 if (j < stop_y-y_step) {
1208 : syskin 1682 /* this is not last line, grab a portion of MBs from the next line too */
1209 :     above_count += MAX(0, complete_count_above[1] - 1);
1210 :     }
1211 :     }
1212 :    
1213 :     max_mbs = current_mb + above_count - i - 1;
1214 :    
1215 :     if (current_mb >= max_mbs) {
1216 :     /* current workload is zero */
1217 :     i--;
1218 :     sched_yield();
1219 :     continue;
1220 :     }
1221 :     }
1222 :    
1223 :     /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */
1224 :     if (b_reference->coding_type != S_VOP)
1225 :     if (b_mb->mode == MODE_NOT_CODED) {
1226 :     pMB->mode = MODE_NOT_CODED;
1227 :     pMB->mvs[0] = pMB->b_mvs[0] = zeroMV;
1228 :     pMB->sad16 = 0;
1229 :     *complete_count_self = i+1;
1230 :     current_mb++;
1231 :     continue;
1232 :     }
1233 :    
1234 :     /* direct search comes first, because it (1) checks for SKIP-mode
1235 :     and (2) sets very good predictions for forward and backward search */
1236 :     skip_sad = SearchDirect_initial(i, j, frame->motion_flags, TRB, TRD, pParam, pMB,
1237 :     b_mb, &best_sad, &Data_d);
1238 :    
1239 :     if (pMB->mode == MODE_DIRECT_NONE_MV) {
1240 :     pMB->sad16 = best_sad;
1241 :     pMB->cbp = 0;
1242 :     *complete_count_self = i+1;
1243 :     current_mb++;
1244 :     continue;
1245 :     }
1246 :    
1247 :     SearchBF_initial(i, j, frame->motion_flags, frame->fcode, pParam, pMB,
1248 :     &f_predMV, &best_sad, MODE_FORWARD, &Data_f, Data_d.currentMV[1]);
1249 :    
1250 :     SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB,
1251 :     &b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2]);
1252 :    
1253 :     if (frame->motion_flags&XVID_ME_BFRAME_EARLYSTOP)
1254 :     fb_thresh = best_sad;
1255 :     else
1256 :     fb_thresh = best_sad + (best_sad>>1);
1257 :    
1258 :     if (Data_f.iMinSAD[0] <= fb_thresh)
1259 :     SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_f);
1260 :    
1261 :     if (Data_b.iMinSAD[0] <= fb_thresh)
1262 :     SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_b);
1263 :    
1264 :     SearchInterpolate_initial(i, j, frame->motion_flags, pParam, &f_predMV, &b_predMV, &best_sad,
1265 :     &Data_i, Data_f.currentMV[0], Data_b.currentMV[0]);
1266 :    
1267 :     if (((Data_i.iMinSAD[0] < best_sad +(best_sad>>3)) && !(frame->motion_flags&XVID_ME_FAST_MODEINTERPOLATE))
1268 :     || Data_i.iMinSAD[0] <= best_sad)
1269 :    
1270 :     SearchInterpolate_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_i);
1271 :    
1272 :     if (Data_d.iMinSAD[0] <= 2*best_sad)
1273 :     if ((!(frame->motion_flags&XVID_ME_SKIP_DELTASEARCH) && (best_sad > 750))
1274 :     || (best_sad > 1000))
1275 :    
1276 :     SearchDirect_final(frame->motion_flags, b_mb, &best_sad, &Data_d);
1277 :    
1278 :     /* final skip decision */
1279 :     if ( (skip_sad < 2 * Data_d.iQuant * MAX_SAD00_FOR_SKIP )
1280 :     && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) {
1281 :    
1282 :     Data_d.chromaSAD = 0; /* green light for chroma check */
1283 :    
1284 :     SkipDecisionB(pMB, &Data_d);
1285 :    
1286 :     if (pMB->mode == MODE_DIRECT_NONE_MV) { /* skipped? */
1287 :     pMB->sad16 = skip_sad;
1288 :     pMB->cbp = 0;
1289 :     *complete_count_self = i+1;
1290 :     current_mb++;
1291 :     continue;
1292 :     }
1293 :     }
1294 :    
1295 :     if (frame->vop_flags & XVID_VOP_RD_BVOP)
1296 :     ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i,
1297 : Isibaar 1919 pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, frame->vop_flags, pParam, i, j, best_sad, force_direct);
1298 : syskin 1682 else
1299 : Isibaar 1919 ModeDecision_BVOP_SAD(&Data_d, &Data_b, &Data_f, &Data_i, pMB, b_mb, &f_predMV, &b_predMV, force_direct);
1300 : syskin 1682
1301 :     *complete_count_self = i+1;
1302 :     current_mb++;
1303 : syskin 1687 maxMotionBVOP(&MVmaxF, &MVmaxB, pMB, Data_d.qpel);
1304 : syskin 1682 }
1305 :    
1306 :     complete_count_self++;
1307 :     complete_count_above++;
1308 :     }
1309 : syskin 1687
1310 :     h->minfcode = getMinFcode(MVmaxF);
1311 :     h->minbcode = getMinFcode(MVmaxB);
1312 : syskin 1682 }

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