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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 248 - (view) (download)

1 : Isibaar 3 /**************************************************************************
2 :     *
3 :     * Modifications:
4 :     *
5 : suxen_drol 152 * 01.05.2002 updated MotionEstimationBVOP
6 : suxen_drol 136 * 25.04.2002 partial prevMB conversion
7 : chenm001 132 * 22.04.2002 remove some compile warning by chenm001 <chenm001@163.com>
8 :     * 14.04.2002 added MotionEstimationBVOP()
9 : chl 96 * 02.04.2002 add EPZS(^2) as ME algorithm, use PMV_USESQUARES to choose between
10 :     * EPZS and EPZS^2
11 : Isibaar 3 * 08.02.2002 split up PMVfast into three routines: PMVFast, PMVFast_MainLoop
12 :     * PMVFast_Refine to support multiple searches with different start points
13 : edgomez 78 * 07.01.2002 uv-block-based interpolation
14 : Isibaar 3 * 06.01.2002 INTER/INTRA-decision is now done before any SEARCH8 (speedup)
15 : edgomez 78 * changed INTER_BIAS to 150 (as suggested by suxen_drol)
16 :     * removed halfpel refinement step in PMVfastSearch8 + quality=5
17 :     * added new quality mode = 6 which performs halfpel refinement
18 :     * filesize difference between quality 5 and 6 is smaller than 1%
19 : Isibaar 3 * (Isibaar)
20 :     * 31.12.2001 PMVfastSearch16 and PMVfastSearch8 (gruel)
21 : edgomez 78 * 30.12.2001 get_range/MotionSearchX simplified; blue/green bug fix
22 :     * 22.12.2001 commented best_point==99 check
23 :     * 19.12.2001 modified get_range (purple bug fix)
24 : Isibaar 3 * 15.12.2001 moved pmv displacement from mbprediction
25 :     * 02.12.2001 motion estimation/compensation split (Isibaar)
26 : edgomez 78 * 16.11.2001 rewrote/tweaked search algorithms; pross@cs.rmit.edu.au
27 : Isibaar 3 * 10.11.2001 support for sad16/sad8 functions
28 :     * 28.08.2001 reactivated MODE_INTER4V for EXT_MODE
29 :     * 24.08.2001 removed MODE_INTER4V_Q, disabled MODE_INTER4V for EXT_MODE
30 : edgomez 78 * 22.08.2001 added MODE_INTER4V_Q
31 : Isibaar 3 * 20.08.2001 added pragma to get rid of internal compiler error with VC6
32 :     * idea by Cyril. Thanks.
33 :     *
34 :     * Michael Militzer <isibaar@videocoding.de>
35 :     *
36 :     **************************************************************************/
37 :    
38 :     #include <assert.h>
39 :     #include <stdio.h>
40 : chl 96 #include <stdlib.h>
41 : Isibaar 3
42 :     #include "../encoder.h"
43 :     #include "../utils/mbfunctions.h"
44 :     #include "../prediction/mbprediction.h"
45 :     #include "../global.h"
46 :     #include "../utils/timer.h"
47 : suxen_drol 118 #include "motion.h"
48 : Isibaar 3 #include "sad.h"
49 :    
50 :     // very large value
51 :     #define MV_MAX_ERROR (4096 * 256)
52 :    
53 :     // stop search if sdelta < THRESHOLD
54 :     #define MV16_THRESHOLD 192
55 :     #define MV8_THRESHOLD 56
56 :    
57 : chl 175 #define NEIGH_MOVE_THRESH 0
58 : chl 174 // how much a block's MV must differ from his neighbour
59 :     // to be search for INTER4V. The more, the faster...
60 :    
61 : Isibaar 3 /* sad16(0,0) bias; mpeg4 spec suggests nb/2+1 */
62 :     /* nb = vop pixels * 2^(bpp-8) */
63 :     #define MV16_00_BIAS (128+1)
64 : chl 140 #define MV8_00_BIAS (0)
65 : Isibaar 3
66 :     /* INTER bias for INTER/INTRA decision; mpeg4 spec suggests 2*nb */
67 : chl 172 #define MV16_INTER_BIAS 512
68 : Isibaar 3
69 :     /* Parameters which control inter/inter4v decision */
70 :     #define IMV16X16 5
71 :    
72 :     /* vector map (vlc delta size) smoother parameters */
73 :     #define NEIGH_TEND_16X16 2
74 :     #define NEIGH_TEND_8X8 2
75 :    
76 :     // fast ((A)/2)*2
77 :     #define EVEN(A) (((A)<0?(A)+1:(A)) & ~1)
78 :    
79 : chl 172 #define MVzero(A) ( ((A).x)==(0) && ((A).y)==(0) )
80 :     #define MVequal(A,B) ( ((A).x)==((B).x) && ((A).y)==((B).y) )
81 : Isibaar 3
82 : edgomez 195 int32_t PMVfastSearch16(const uint8_t * const pRef,
83 :     const uint8_t * const pRefH,
84 :     const uint8_t * const pRefV,
85 :     const uint8_t * const pRefHV,
86 :     const IMAGE * const pCur,
87 :     const int x,
88 :     const int y,
89 :     const uint32_t MotionFlags,
90 :     const uint32_t iQuant,
91 :     const uint32_t iFcode,
92 :     const MBParam * const pParam,
93 :     const MACROBLOCK * const pMBs,
94 :     const MACROBLOCK * const prevMBs,
95 :     VECTOR * const currMV,
96 :     VECTOR * const currPMV);
97 : Isibaar 3
98 : edgomez 195 int32_t EPZSSearch16(const uint8_t * const pRef,
99 :     const uint8_t * const pRefH,
100 :     const uint8_t * const pRefV,
101 :     const uint8_t * const pRefHV,
102 :     const IMAGE * const pCur,
103 :     const int x,
104 :     const int y,
105 :     const uint32_t MotionFlags,
106 :     const uint32_t iQuant,
107 :     const uint32_t iFcode,
108 :     const MBParam * const pParam,
109 :     const MACROBLOCK * const pMBs,
110 :     const MACROBLOCK * const prevMBs,
111 :     VECTOR * const currMV,
112 :     VECTOR * const currPMV);
113 : chl 96
114 :    
115 : edgomez 195 int32_t PMVfastSearch8(const uint8_t * const pRef,
116 :     const uint8_t * const pRefH,
117 :     const uint8_t * const pRefV,
118 :     const uint8_t * const pRefHV,
119 :     const IMAGE * const pCur,
120 :     const int x,
121 :     const int y,
122 :     const int start_x,
123 :     const int start_y,
124 :     const uint32_t MotionFlags,
125 :     const uint32_t iQuant,
126 :     const uint32_t iFcode,
127 :     const MBParam * const pParam,
128 :     const MACROBLOCK * const pMBs,
129 :     const MACROBLOCK * const prevMBs,
130 :     VECTOR * const currMV,
131 :     VECTOR * const currPMV);
132 : chl 96
133 : edgomez 195 int32_t EPZSSearch8(const uint8_t * const pRef,
134 : chl 96 const uint8_t * const pRefH,
135 :     const uint8_t * const pRefV,
136 :     const uint8_t * const pRefHV,
137 :     const IMAGE * const pCur,
138 : edgomez 195 const int x,
139 :     const int y,
140 :     const int start_x,
141 :     const int start_y,
142 : chl 96 const uint32_t MotionFlags,
143 : suxen_drol 136 const uint32_t iQuant,
144 :     const uint32_t iFcode,
145 : chl 96 const MBParam * const pParam,
146 : suxen_drol 136 const MACROBLOCK * const pMBs,
147 :     const MACROBLOCK * const prevMBs,
148 : chl 96 VECTOR * const currMV,
149 :     VECTOR * const currPMV);
150 :    
151 :    
152 : edgomez 195 typedef int32_t(MainSearch16Func) (const uint8_t * const pRef,
153 :     const uint8_t * const pRefH,
154 :     const uint8_t * const pRefV,
155 :     const uint8_t * const pRefHV,
156 :     const uint8_t * const cur,
157 :     const int x,
158 :     const int y,
159 :     int32_t startx,
160 :     int32_t starty,
161 :     int32_t iMinSAD,
162 :     VECTOR * const currMV,
163 :     const VECTOR * const pmv,
164 :     const int32_t min_dx,
165 :     const int32_t max_dx,
166 :     const int32_t min_dy,
167 :     const int32_t max_dy,
168 :     const int32_t iEdgedWidth,
169 :     const int32_t iDiamondSize,
170 :     const int32_t iFcode,
171 :     const int32_t iQuant,
172 :     int iFound);
173 : Isibaar 3
174 : edgomez 195 typedef MainSearch16Func *MainSearch16FuncPtr;
175 : chl 96
176 :    
177 : edgomez 195 typedef int32_t(MainSearch8Func) (const uint8_t * const pRef,
178 :     const uint8_t * const pRefH,
179 :     const uint8_t * const pRefV,
180 :     const uint8_t * const pRefHV,
181 :     const uint8_t * const cur,
182 :     const int x,
183 :     const int y,
184 :     int32_t startx,
185 :     int32_t starty,
186 :     int32_t iMinSAD,
187 :     VECTOR * const currMV,
188 :     const VECTOR * const pmv,
189 :     const int32_t min_dx,
190 :     const int32_t max_dx,
191 :     const int32_t min_dy,
192 :     const int32_t max_dy,
193 :     const int32_t iEdgedWidth,
194 :     const int32_t iDiamondSize,
195 :     const int32_t iFcode,
196 :     const int32_t iQuant,
197 :     int iFound);
198 : Isibaar 3
199 : edgomez 195 typedef MainSearch8Func *MainSearch8FuncPtr;
200 : Isibaar 3
201 : edgomez 195 static int32_t lambda_vec16[32] = /* rounded values for lambda param for weight of motion bits as in modified H.26L */
202 :     { 0, (int) (1.00235 + 0.5), (int) (1.15582 + 0.5), (int) (1.31976 + 0.5),
203 :     (int) (1.49591 + 0.5), (int) (1.68601 + 0.5),
204 :     (int) (1.89187 + 0.5), (int) (2.11542 + 0.5), (int) (2.35878 + 0.5),
205 :     (int) (2.62429 + 0.5), (int) (2.91455 + 0.5),
206 :     (int) (3.23253 + 0.5), (int) (3.58158 + 0.5), (int) (3.96555 + 0.5),
207 :     (int) (4.38887 + 0.5), (int) (4.85673 + 0.5),
208 :     (int) (5.37519 + 0.5), (int) (5.95144 + 0.5), (int) (6.59408 + 0.5),
209 :     (int) (7.31349 + 0.5), (int) (8.12242 + 0.5),
210 :     (int) (9.03669 + 0.5), (int) (10.0763 + 0.5), (int) (11.2669 + 0.5),
211 :     (int) (12.6426 + 0.5), (int) (14.2493 + 0.5),
212 :     (int) (16.1512 + 0.5), (int) (18.442 + 0.5), (int) (21.2656 + 0.5),
213 :     (int) (24.8580 + 0.5), (int) (29.6436 + 0.5),
214 :     (int) (36.4949 + 0.5)
215 :     };
216 : chl 141
217 : edgomez 195 static int32_t *lambda_vec8 = lambda_vec16; /* same table for INTER and INTER4V for now */
218 : chl 141
219 :    
220 :    
221 : Isibaar 3 // mv.length table
222 :     static const uint32_t mvtab[33] = {
223 : edgomez 195 1, 2, 3, 4, 6, 7, 7, 7,
224 :     9, 9, 9, 10, 10, 10, 10, 10,
225 :     10, 10, 10, 10, 10, 10, 10, 10,
226 :     10, 11, 11, 11, 11, 11, 11, 12, 12
227 : Isibaar 3 };
228 :    
229 :    
230 : edgomez 195 static __inline uint32_t
231 :     mv_bits(int32_t component,
232 :     const uint32_t iFcode)
233 : Isibaar 3 {
234 : edgomez 195 if (component == 0)
235 : Isibaar 3 return 1;
236 :    
237 : edgomez 195 if (component < 0)
238 : Isibaar 3 component = -component;
239 :    
240 : edgomez 195 if (iFcode == 1) {
241 : Isibaar 3 if (component > 32)
242 : edgomez 195 component = 32;
243 : Isibaar 3
244 :     return mvtab[component] + 1;
245 : edgomez 195 }
246 : Isibaar 3
247 : edgomez 195 component += (1 << (iFcode - 1)) - 1;
248 :     component >>= (iFcode - 1);
249 : Isibaar 3
250 : edgomez 195 if (component > 32)
251 : Isibaar 3 component = 32;
252 :    
253 : edgomez 195 return mvtab[component] + 1 + iFcode - 1;
254 : Isibaar 3 }
255 :    
256 :    
257 : edgomez 195 static __inline uint32_t
258 :     calc_delta_16(const int32_t dx,
259 :     const int32_t dy,
260 :     const uint32_t iFcode,
261 :     const uint32_t iQuant)
262 : Isibaar 3 {
263 : edgomez 195 return NEIGH_TEND_16X16 * lambda_vec16[iQuant] * (mv_bits(dx, iFcode) +
264 :     mv_bits(dy, iFcode));
265 : Isibaar 3 }
266 :    
267 : edgomez 195 static __inline uint32_t
268 :     calc_delta_8(const int32_t dx,
269 :     const int32_t dy,
270 :     const uint32_t iFcode,
271 :     const uint32_t iQuant)
272 : Isibaar 3 {
273 : edgomez 195 return NEIGH_TEND_8X8 * lambda_vec8[iQuant] * (mv_bits(dx, iFcode) +
274 :     mv_bits(dy, iFcode));
275 : Isibaar 3 }
276 :    
277 :    
278 :    
279 :    
280 :    
281 :     #ifndef SEARCH16
282 :     #define SEARCH16 PMVfastSearch16
283 : edgomez 195 //#define SEARCH16 FullSearch16
284 :     //#define SEARCH16 EPZSSearch16
285 : Isibaar 3 #endif
286 :    
287 :     #ifndef SEARCH8
288 :     #define SEARCH8 PMVfastSearch8
289 : edgomez 195 //#define SEARCH8 EPZSSearch8
290 : Isibaar 3 #endif
291 :    
292 : edgomez 195 bool
293 :     MotionEstimation(MBParam * const pParam,
294 :     FRAMEINFO * const current,
295 :     FRAMEINFO * const reference,
296 :     const IMAGE * const pRefH,
297 :     const IMAGE * const pRefV,
298 :     const IMAGE * const pRefHV,
299 :     const uint32_t iLimit)
300 : Isibaar 3 {
301 : edgomez 78 const uint32_t iWcount = pParam->mb_width;
302 :     const uint32_t iHcount = pParam->mb_height;
303 : edgomez 195 MACROBLOCK *const pMBs = current->mbs;
304 :     MACROBLOCK *const prevMBs = reference->mbs;
305 :     const IMAGE *const pCurrent = &current->image;
306 :     const IMAGE *const pRef = &reference->image;
307 : suxen_drol 136
308 : edgomez 195 const VECTOR zeroMV = { 0, 0 };
309 : chl 184
310 : chl 172 int32_t x, y;
311 :     int32_t iIntra = 0;
312 :     VECTOR pmv;
313 : suxen_drol 136
314 : chl 184 if (sadInit)
315 : edgomez 195 (*sadInit) ();
316 :    
317 : chl 172 for (y = 0; y < iHcount; y++)
318 : edgomez 195 for (x = 0; x < iWcount; x++) {
319 :     MACROBLOCK *const pMB = &pMBs[x + y * iWcount];
320 : Isibaar 3
321 : edgomez 195 pMB->sad16 =
322 :     SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
323 :     y, current->motion_flags, current->quant,
324 :     current->fcode, pParam, pMBs, prevMBs, &pMB->mv16,
325 :     &pMB->pmvs[0]);
326 : chl 184
327 : edgomez 195 if (0 < (pMB->sad16 - MV16_INTER_BIAS)) {
328 : chl 172 int32_t deviation;
329 : chl 184
330 : edgomez 195 deviation =
331 :     dev16(pCurrent->y + x * 16 + y * 16 * pParam->edged_width,
332 :     pParam->edged_width);
333 :    
334 :     if (deviation < (pMB->sad16 - MV16_INTER_BIAS)) {
335 : chl 172 pMB->mode = MODE_INTRA;
336 : edgomez 195 pMB->mv16 = pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] =
337 :     pMB->mvs[3] = zeroMV;
338 :     pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] =
339 :     pMB->sad8[3] = 0;
340 :    
341 : chl 172 iIntra++;
342 :     if (iIntra >= iLimit)
343 : edgomez 195 return 1;
344 :    
345 : chl 172 continue;
346 :     }
347 : edgomez 195 }
348 :    
349 : chl 184 pmv = pMB->pmvs[0];
350 :     if (current->global_flags & XVID_INTER4V)
351 : edgomez 195 if ((!(current->global_flags & XVID_LUMIMASKING) ||
352 :     pMB->dquant == NO_CHANGE)) {
353 :     int32_t sad8 = IMV16X16 * current->quant;
354 :    
355 : chl 175 if (sad8 < pMB->sad16)
356 : edgomez 195
357 :     sad8 += pMB->sad8[0] =
358 :     SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
359 :     pCurrent, 2 * x, 2 * y, pMB->mv16.x,
360 :     pMB->mv16.y, current->motion_flags,
361 :     current->quant, current->fcode, pParam,
362 :     pMBs, prevMBs, &pMB->mvs[0],
363 :     &pMB->pmvs[0]);
364 :    
365 : chl 175 if (sad8 < pMB->sad16)
366 : edgomez 195 sad8 += pMB->sad8[1] =
367 :     SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
368 :     pCurrent, 2 * x + 1, 2 * y, pMB->mv16.x,
369 :     pMB->mv16.y, current->motion_flags,
370 :     current->quant, current->fcode, pParam,
371 :     pMBs, prevMBs, &pMB->mvs[1],
372 :     &pMB->pmvs[1]);
373 : Isibaar 3
374 : chl 175 if (sad8 < pMB->sad16)
375 : edgomez 195 sad8 += pMB->sad8[2] =
376 :     SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
377 :     pCurrent, 2 * x, 2 * y + 1, pMB->mv16.x,
378 :     pMB->mv16.y, current->motion_flags,
379 :     current->quant, current->fcode, pParam,
380 :     pMBs, prevMBs, &pMB->mvs[2],
381 :     &pMB->pmvs[2]);
382 :    
383 :     if (sad8 < pMB->sad16)
384 :     sad8 += pMB->sad8[3] =
385 :     SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
386 :     pCurrent, 2 * x + 1, 2 * y + 1,
387 :     pMB->mv16.x, pMB->mv16.y,
388 :     current->motion_flags, current->quant,
389 :     current->fcode, pParam, pMBs, prevMBs,
390 :     &pMB->mvs[3], &pMB->pmvs[3]);
391 :    
392 : chl 184 /* decide: MODE_INTER or MODE_INTER4V
393 : edgomez 195 mpeg4: if (sad8 < pMB->sad16 - nb/2+1) use_inter4v
394 :     */
395 :    
396 :     if (sad8 < pMB->sad16) {
397 : chl 175 pMB->mode = MODE_INTER4V;
398 : edgomez 195 pMB->sad8[0] *= 4;
399 : chl 175 pMB->sad8[1] *= 4;
400 :     pMB->sad8[2] *= 4;
401 :     pMB->sad8[3] *= 4;
402 :     continue;
403 :     }
404 :    
405 : edgomez 195 }
406 :    
407 :     pMB->mode = MODE_INTER;
408 :     pMB->pmvs[0] = pmv; /* pMB->pmvs[1] = pMB->pmvs[2] = pMB->pmvs[3] are not needed for INTER */
409 :     pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mv16;
410 :     pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] =
411 :     pMB->sad16;
412 :    
413 : chl 184 }
414 : edgomez 195 return 0;
415 : Isibaar 3 }
416 :    
417 :     #define CHECK_MV16_ZERO {\
418 :     if ( (0 <= max_dx) && (0 >= min_dx) \
419 :     && (0 <= max_dy) && (0 >= min_dy) ) \
420 :     { \
421 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0, 0 , iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); \
422 : chl 141 iSAD += calc_delta_16(-pmv[0].x, -pmv[0].y, (uint8_t)iFcode, iQuant);\
423 : Isibaar 3 if (iSAD < iMinSAD) \
424 :     { iMinSAD=iSAD; currMV->x=0; currMV->y=0; } } \
425 :     }
426 :    
427 : chl 96 #define NOCHECK_MV16_CANDIDATE(X,Y) { \
428 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
429 : chl 141 iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\
430 : chl 96 if (iSAD < iMinSAD) \
431 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \
432 :     }
433 : Isibaar 3
434 :     #define CHECK_MV16_CANDIDATE(X,Y) { \
435 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
436 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
437 :     { \
438 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
439 : chl 141 iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\
440 : Isibaar 3 if (iSAD < iMinSAD) \
441 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \
442 :     }
443 :    
444 :     #define CHECK_MV16_CANDIDATE_DIR(X,Y,D) { \
445 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
446 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
447 :     { \
448 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
449 : chl 141 iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\
450 : Isibaar 3 if (iSAD < iMinSAD) \
451 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \
452 :     }
453 :    
454 :     #define CHECK_MV16_CANDIDATE_FOUND(X,Y,D) { \
455 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
456 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
457 :     { \
458 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
459 : chl 141 iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\
460 : Isibaar 3 if (iSAD < iMinSAD) \
461 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \
462 :     }
463 :    
464 :    
465 :     #define CHECK_MV8_ZERO {\
466 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, 0, 0 , iEdgedWidth), iEdgedWidth); \
467 : chl 141 iSAD += calc_delta_8(-pmv[0].x, -pmv[0].y, (uint8_t)iFcode, iQuant);\
468 : Isibaar 3 if (iSAD < iMinSAD) \
469 :     { iMinSAD=iSAD; currMV->x=0; currMV->y=0; } \
470 :     }
471 : edgomez 195
472 : chl 96 #define NOCHECK_MV8_CANDIDATE(X,Y) \
473 :     { \
474 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
475 : chl 141 iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\
476 : chl 96 if (iSAD < iMinSAD) \
477 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \
478 :     }
479 : Isibaar 3
480 :     #define CHECK_MV8_CANDIDATE(X,Y) { \
481 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
482 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
483 :     { \
484 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
485 : chl 141 iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\
486 : Isibaar 3 if (iSAD < iMinSAD) \
487 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \
488 :     }
489 :    
490 :     #define CHECK_MV8_CANDIDATE_DIR(X,Y,D) { \
491 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
492 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
493 :     { \
494 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
495 : chl 141 iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\
496 : Isibaar 3 if (iSAD < iMinSAD) \
497 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \
498 :     }
499 :    
500 :     #define CHECK_MV8_CANDIDATE_FOUND(X,Y,D) { \
501 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
502 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
503 :     { \
504 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
505 : chl 141 iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\
506 : Isibaar 3 if (iSAD < iMinSAD) \
507 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \
508 :     }
509 :    
510 :     /* too slow and not fully functional at the moment */
511 :     /*
512 :     int32_t ZeroSearch16(
513 :     const uint8_t * const pRef,
514 :     const uint8_t * const pRefH,
515 :     const uint8_t * const pRefV,
516 :     const uint8_t * const pRefHV,
517 :     const IMAGE * const pCur,
518 :     const int x, const int y,
519 :     const uint32_t MotionFlags,
520 : suxen_drol 136 const uint32_t iQuant,
521 :     const uint32_t iFcode,
522 : Isibaar 3 MBParam * const pParam,
523 : suxen_drol 136 const MACROBLOCK * const pMBs,
524 :     const MACROBLOCK * const prevMBs,
525 : Isibaar 3 VECTOR * const currMV,
526 :     VECTOR * const currPMV)
527 :     {
528 :     const int32_t iEdgedWidth = pParam->edged_width;
529 :     const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth;
530 :     int32_t iSAD;
531 :     int32_t pred_x,pred_y;
532 :    
533 :     get_pmv(pMBs, x, y, pParam->mb_width, 0, &pred_x, &pred_y);
534 :    
535 :     iSAD = sad16( cur,
536 :     get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0,0, iEdgedWidth),
537 :     iEdgedWidth, MV_MAX_ERROR);
538 :     if (iSAD <= iQuant * 96)
539 :     iSAD -= MV16_00_BIAS;
540 :    
541 :     currMV->x = 0;
542 :     currMV->y = 0;
543 :     currPMV->x = -pred_x;
544 :     currPMV->y = -pred_y;
545 :    
546 :     return iSAD;
547 :    
548 :     }
549 :     */
550 :    
551 : edgomez 195 int32_t
552 :     Diamond16_MainSearch(const uint8_t * const pRef,
553 :     const uint8_t * const pRefH,
554 :     const uint8_t * const pRefV,
555 :     const uint8_t * const pRefHV,
556 :     const uint8_t * const cur,
557 :     const int x,
558 :     const int y,
559 :     int32_t startx,
560 :     int32_t starty,
561 :     int32_t iMinSAD,
562 :     VECTOR * const currMV,
563 :     const VECTOR * const pmv,
564 :     const int32_t min_dx,
565 :     const int32_t max_dx,
566 :     const int32_t min_dy,
567 :     const int32_t max_dy,
568 :     const int32_t iEdgedWidth,
569 :     const int32_t iDiamondSize,
570 :     const int32_t iFcode,
571 :     const int32_t iQuant,
572 :     int iFound)
573 : Isibaar 3 {
574 :     /* Do a diamond search around given starting point, return SAD of best */
575 :    
576 : edgomez 195 int32_t iDirection = 0;
577 : Isibaar 3 int32_t iSAD;
578 :     VECTOR backupMV;
579 : edgomez 195
580 : Isibaar 3 backupMV.x = startx;
581 :     backupMV.y = starty;
582 : edgomez 195
583 : Isibaar 3 /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
584 :    
585 : edgomez 195 CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
586 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
587 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
588 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
589 : Isibaar 3
590 :     if (iDirection)
591 : edgomez 195 while (!iFound) {
592 :     iFound = 1;
593 :     backupMV = *currMV;
594 :    
595 :     if (iDirection != 2)
596 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
597 :     backupMV.y, 1);
598 :     if (iDirection != 1)
599 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
600 :     backupMV.y, 2);
601 :     if (iDirection != 4)
602 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x,
603 :     backupMV.y - iDiamondSize, 3);
604 :     if (iDirection != 3)
605 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x,
606 :     backupMV.y + iDiamondSize, 4);
607 :     } else {
608 : edgomez 78 currMV->x = startx;
609 :     currMV->y = starty;
610 :     }
611 : Isibaar 3 return iMinSAD;
612 :     }
613 :    
614 : edgomez 195 int32_t
615 :     Square16_MainSearch(const uint8_t * const pRef,
616 : chl 96 const uint8_t * const pRefH,
617 :     const uint8_t * const pRefV,
618 :     const uint8_t * const pRefHV,
619 :     const uint8_t * const cur,
620 : edgomez 195 const int x,
621 :     const int y,
622 :     int32_t startx,
623 :     int32_t starty,
624 : chl 96 int32_t iMinSAD,
625 :     VECTOR * const currMV,
626 :     const VECTOR * const pmv,
627 : edgomez 195 const int32_t min_dx,
628 :     const int32_t max_dx,
629 :     const int32_t min_dy,
630 :     const int32_t max_dy,
631 :     const int32_t iEdgedWidth,
632 :     const int32_t iDiamondSize,
633 : chl 96 const int32_t iFcode,
634 :     const int32_t iQuant,
635 :     int iFound)
636 :     {
637 :     /* Do a square search around given starting point, return SAD of best */
638 :    
639 : edgomez 195 int32_t iDirection = 0;
640 : chl 96 int32_t iSAD;
641 :     VECTOR backupMV;
642 : edgomez 195
643 : chl 96 backupMV.x = startx;
644 :     backupMV.y = starty;
645 : edgomez 195
646 : chl 96 /* It's one search with full square pattern, and new parts for all following diamonds */
647 :    
648 :     /* new direction are extra, so 1-4 is normal diamond
649 :     537
650 :     1*2
651 :     648
652 :     */
653 :    
654 : edgomez 195 CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
655 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
656 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
657 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
658 : chl 96
659 : edgomez 195 CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
660 :     backupMV.y - iDiamondSize, 5);
661 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
662 :     backupMV.y + iDiamondSize, 6);
663 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
664 :     backupMV.y - iDiamondSize, 7);
665 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
666 :     backupMV.y + iDiamondSize, 8);
667 : chl 96
668 : edgomez 195
669 : chl 96 if (iDirection)
670 : edgomez 195 while (!iFound) {
671 :     iFound = 1;
672 :     backupMV = *currMV;
673 : chl 96
674 : edgomez 195 switch (iDirection) {
675 :     case 1:
676 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
677 :     backupMV.y, 1);
678 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
679 :     backupMV.y - iDiamondSize, 5);
680 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
681 :     backupMV.y - iDiamondSize, 7);
682 :     break;
683 :     case 2:
684 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y,
685 :     2);
686 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
687 :     backupMV.y + iDiamondSize, 6);
688 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
689 :     backupMV.y + iDiamondSize, 8);
690 :     break;
691 :    
692 :     case 3:
693 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize,
694 :     4);
695 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
696 :     backupMV.y - iDiamondSize, 7);
697 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
698 :     backupMV.y + iDiamondSize, 8);
699 :     break;
700 :    
701 :     case 4:
702 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize,
703 :     3);
704 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
705 :     backupMV.y - iDiamondSize, 5);
706 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
707 :     backupMV.y + iDiamondSize, 6);
708 :     break;
709 :    
710 :     case 5:
711 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y,
712 :     1);
713 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize,
714 :     3);
715 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
716 :     backupMV.y - iDiamondSize, 5);
717 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
718 :     backupMV.y + iDiamondSize, 6);
719 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
720 :     backupMV.y - iDiamondSize, 7);
721 :     break;
722 :    
723 :     case 6:
724 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y,
725 :     2);
726 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize,
727 :     3);
728 :    
729 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
730 :     backupMV.y - iDiamondSize, 5);
731 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
732 :     backupMV.y + iDiamondSize, 6);
733 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
734 :     backupMV.y + iDiamondSize, 8);
735 :    
736 :     break;
737 :    
738 :     case 7:
739 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
740 :     backupMV.y, 1);
741 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize,
742 :     4);
743 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
744 :     backupMV.y - iDiamondSize, 5);
745 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
746 :     backupMV.y - iDiamondSize, 7);
747 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
748 :     backupMV.y + iDiamondSize, 8);
749 :     break;
750 :    
751 :     case 8:
752 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y,
753 :     2);
754 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize,
755 :     4);
756 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
757 :     backupMV.y + iDiamondSize, 6);
758 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
759 :     backupMV.y - iDiamondSize, 7);
760 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
761 :     backupMV.y + iDiamondSize, 8);
762 :     break;
763 : chl 96 default:
764 : edgomez 195 CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y,
765 :     1);
766 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y,
767 :     2);
768 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize,
769 :     3);
770 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize,
771 :     4);
772 :    
773 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
774 :     backupMV.y - iDiamondSize, 5);
775 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
776 :     backupMV.y + iDiamondSize, 6);
777 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
778 :     backupMV.y - iDiamondSize, 7);
779 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
780 :     backupMV.y + iDiamondSize, 8);
781 :     break;
782 : chl 96 }
783 : edgomez 195 } else {
784 :     currMV->x = startx;
785 :     currMV->y = starty;
786 :     }
787 : chl 96 return iMinSAD;
788 :     }
789 :    
790 :    
791 : edgomez 195 int32_t
792 :     Full16_MainSearch(const uint8_t * const pRef,
793 :     const uint8_t * const pRefH,
794 :     const uint8_t * const pRefV,
795 :     const uint8_t * const pRefHV,
796 :     const uint8_t * const cur,
797 :     const int x,
798 :     const int y,
799 :     int32_t startx,
800 :     int32_t starty,
801 :     int32_t iMinSAD,
802 :     VECTOR * const currMV,
803 :     const VECTOR * const pmv,
804 :     const int32_t min_dx,
805 :     const int32_t max_dx,
806 :     const int32_t min_dy,
807 :     const int32_t max_dy,
808 :     const int32_t iEdgedWidth,
809 :     const int32_t iDiamondSize,
810 :     const int32_t iFcode,
811 :     const int32_t iQuant,
812 :     int iFound)
813 : chl 96 {
814 :     int32_t iSAD;
815 : edgomez 195 int32_t dx, dy;
816 : chl 96 VECTOR backupMV;
817 : edgomez 195
818 : chl 96 backupMV.x = startx;
819 :     backupMV.y = starty;
820 :    
821 : edgomez 195 for (dx = min_dx; dx <= max_dx; dx += iDiamondSize)
822 :     for (dy = min_dy; dy <= max_dy; dy += iDiamondSize)
823 :     NOCHECK_MV16_CANDIDATE(dx, dy);
824 :    
825 : chl 96 return iMinSAD;
826 :     }
827 :    
828 : edgomez 195 int32_t
829 :     AdvDiamond16_MainSearch(const uint8_t * const pRef,
830 :     const uint8_t * const pRefH,
831 :     const uint8_t * const pRefV,
832 :     const uint8_t * const pRefHV,
833 :     const uint8_t * const cur,
834 :     const int x,
835 :     const int y,
836 :     int32_t startx,
837 :     int32_t starty,
838 :     int32_t iMinSAD,
839 :     VECTOR * const currMV,
840 :     const VECTOR * const pmv,
841 :     const int32_t min_dx,
842 :     const int32_t max_dx,
843 :     const int32_t min_dy,
844 :     const int32_t max_dy,
845 :     const int32_t iEdgedWidth,
846 :     const int32_t iDiamondSize,
847 :     const int32_t iFcode,
848 :     const int32_t iQuant,
849 :     int iDirection)
850 : chl 181 {
851 :    
852 :     int32_t iSAD;
853 :    
854 :     /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
855 :    
856 : edgomez 195 if (iDirection) {
857 :     CHECK_MV16_CANDIDATE(startx - iDiamondSize, starty);
858 :     CHECK_MV16_CANDIDATE(startx + iDiamondSize, starty);
859 :     CHECK_MV16_CANDIDATE(startx, starty - iDiamondSize);
860 :     CHECK_MV16_CANDIDATE(startx, starty + iDiamondSize);
861 :     } else {
862 :     int bDirection = 1 + 2 + 4 + 8;
863 :    
864 :     do {
865 : chl 181 iDirection = 0;
866 : edgomez 195 if (bDirection & 1) //we only want to check left if we came from the right (our last motion was to the left, up-left or down-left)
867 :     CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, starty, 1);
868 : chl 181
869 : edgomez 195 if (bDirection & 2)
870 :     CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, starty, 2);
871 : chl 181
872 : edgomez 195 if (bDirection & 4)
873 :     CHECK_MV16_CANDIDATE_DIR(startx, starty - iDiamondSize, 4);
874 : chl 181
875 : edgomez 195 if (bDirection & 8)
876 :     CHECK_MV16_CANDIDATE_DIR(startx, starty + iDiamondSize, 8);
877 : chl 181
878 :     /* now we're doing diagonal checks near our candidate */
879 :    
880 : edgomez 195 if (iDirection) //checking if anything found
881 : chl 181 {
882 :     bDirection = iDirection;
883 :     iDirection = 0;
884 : edgomez 195 startx = currMV->x;
885 :     starty = currMV->y;
886 :     if (bDirection & 3) //our candidate is left or right
887 : chl 181 {
888 : edgomez 195 CHECK_MV16_CANDIDATE_DIR(startx, starty + iDiamondSize, 8);
889 :     CHECK_MV16_CANDIDATE_DIR(startx, starty - iDiamondSize, 4);
890 :     } else // what remains here is up or down
891 : chl 181 {
892 : edgomez 195 CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, starty, 2);
893 :     CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, starty, 1);
894 : chl 181 }
895 :    
896 : edgomez 195 if (iDirection) {
897 :     bDirection += iDirection;
898 :     startx = currMV->x;
899 :     starty = currMV->y;
900 : chl 181 }
901 : edgomez 195 } else //about to quit, eh? not so fast....
902 : chl 181 {
903 : edgomez 195 switch (bDirection) {
904 : chl 181 case 2:
905 : edgomez 195 CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,
906 :     starty - iDiamondSize, 2 + 4);
907 :     CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,
908 :     starty + iDiamondSize, 2 + 8);
909 : chl 181 break;
910 :     case 1:
911 : edgomez 195 CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,
912 :     starty - iDiamondSize, 1 + 4);
913 :     CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,
914 :     starty + iDiamondSize, 1 + 8);
915 : chl 181 break;
916 : edgomez 195 case 2 + 4:
917 :     CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,
918 :     starty - iDiamondSize, 1 + 4);
919 :     CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,
920 :     starty - iDiamondSize, 2 + 4);
921 :     CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,
922 :     starty + iDiamondSize, 2 + 8);
923 : chl 181 break;
924 :     case 4:
925 : edgomez 195 CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,
926 :     starty - iDiamondSize, 2 + 4);
927 :     CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,
928 :     starty - iDiamondSize, 1 + 4);
929 : chl 181 break;
930 :     case 8:
931 : edgomez 195 CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,
932 :     starty + iDiamondSize, 2 + 8);
933 :     CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,
934 :     starty + iDiamondSize, 1 + 8);
935 : chl 181 break;
936 : edgomez 195 case 1 + 4:
937 :     CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,
938 :     starty + iDiamondSize, 1 + 8);
939 :     CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,
940 :     starty - iDiamondSize, 1 + 4);
941 :     CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,
942 :     starty - iDiamondSize, 2 + 4);
943 : chl 181 break;
944 : edgomez 195 case 2 + 8:
945 :     CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,
946 :     starty - iDiamondSize, 1 + 4);
947 :     CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,
948 :     starty + iDiamondSize, 1 + 8);
949 :     CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,
950 :     starty + iDiamondSize, 2 + 8);
951 : chl 181 break;
952 : edgomez 195 case 1 + 8:
953 :     CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,
954 :     starty - iDiamondSize, 2 + 4);
955 :     CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,
956 :     starty + iDiamondSize, 2 + 8);
957 :     CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,
958 :     starty + iDiamondSize, 1 + 8);
959 : chl 181 break;
960 : edgomez 195 default: //1+2+4+8 == we didn't find anything at all
961 :     CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,
962 :     starty - iDiamondSize, 1 + 4);
963 :     CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,
964 :     starty + iDiamondSize, 1 + 8);
965 :     CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,
966 :     starty - iDiamondSize, 2 + 4);
967 :     CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,
968 :     starty + iDiamondSize, 2 + 8);
969 : chl 181 break;
970 :     }
971 : edgomez 195 if (!iDirection)
972 :     break; //ok, the end. really
973 :     else {
974 :     bDirection = iDirection;
975 :     startx = currMV->x;
976 :     starty = currMV->y;
977 : chl 181 }
978 :     }
979 :     }
980 : edgomez 195 while (1); //forever
981 : chl 181 }
982 :     return iMinSAD;
983 :     }
984 :    
985 : edgomez 195 int32_t
986 :     AdvDiamond8_MainSearch(const uint8_t * const pRef,
987 :     const uint8_t * const pRefH,
988 :     const uint8_t * const pRefV,
989 :     const uint8_t * const pRefHV,
990 :     const uint8_t * const cur,
991 :     const int x,
992 :     const int y,
993 :     int32_t startx,
994 :     int32_t starty,
995 :     int32_t iMinSAD,
996 :     VECTOR * const currMV,
997 :     const VECTOR * const pmv,
998 :     const int32_t min_dx,
999 :     const int32_t max_dx,
1000 :     const int32_t min_dy,
1001 :     const int32_t max_dy,
1002 :     const int32_t iEdgedWidth,
1003 :     const int32_t iDiamondSize,
1004 :     const int32_t iFcode,
1005 :     const int32_t iQuant,
1006 :     int iDirection)
1007 : chl 181 {
1008 :    
1009 :     int32_t iSAD;
1010 :    
1011 :     /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
1012 :    
1013 : edgomez 195 if (iDirection) {
1014 :     CHECK_MV8_CANDIDATE(startx - iDiamondSize, starty);
1015 :     CHECK_MV8_CANDIDATE(startx + iDiamondSize, starty);
1016 :     CHECK_MV8_CANDIDATE(startx, starty - iDiamondSize);
1017 :     CHECK_MV8_CANDIDATE(startx, starty + iDiamondSize);
1018 :     } else {
1019 :     int bDirection = 1 + 2 + 4 + 8;
1020 :    
1021 :     do {
1022 : chl 181 iDirection = 0;
1023 : edgomez 195 if (bDirection & 1) //we only want to check left if we came from the right (our last motion was to the left, up-left or down-left)
1024 :     CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, starty, 1);
1025 : chl 181
1026 : edgomez 195 if (bDirection & 2)
1027 :     CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, starty, 2);
1028 : chl 181
1029 : edgomez 195 if (bDirection & 4)
1030 :     CHECK_MV8_CANDIDATE_DIR(startx, starty - iDiamondSize, 4);
1031 : chl 181
1032 : edgomez 195 if (bDirection & 8)
1033 :     CHECK_MV8_CANDIDATE_DIR(startx, starty + iDiamondSize, 8);
1034 : chl 181
1035 :     /* now we're doing diagonal checks near our candidate */
1036 :    
1037 : edgomez 195 if (iDirection) //checking if anything found
1038 : chl 181 {
1039 :     bDirection = iDirection;
1040 :     iDirection = 0;
1041 : edgomez 195 startx = currMV->x;
1042 :     starty = currMV->y;
1043 :     if (bDirection & 3) //our candidate is left or right
1044 : chl 181 {
1045 : edgomez 195 CHECK_MV8_CANDIDATE_DIR(startx, starty + iDiamondSize, 8);
1046 :     CHECK_MV8_CANDIDATE_DIR(startx, starty - iDiamondSize, 4);
1047 :     } else // what remains here is up or down
1048 : chl 181 {
1049 : edgomez 195 CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, starty, 2);
1050 :     CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, starty, 1);
1051 : chl 181 }
1052 :    
1053 : edgomez 195 if (iDirection) {
1054 :     bDirection += iDirection;
1055 :     startx = currMV->x;
1056 :     starty = currMV->y;
1057 : chl 181 }
1058 : edgomez 195 } else //about to quit, eh? not so fast....
1059 : chl 181 {
1060 : edgomez 195 switch (bDirection) {
1061 : chl 181 case 2:
1062 : edgomez 195 CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,
1063 :     starty - iDiamondSize, 2 + 4);
1064 :     CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,
1065 :     starty + iDiamondSize, 2 + 8);
1066 : chl 181 break;
1067 :     case 1:
1068 : edgomez 195 CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,
1069 :     starty - iDiamondSize, 1 + 4);
1070 :     CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,
1071 :     starty + iDiamondSize, 1 + 8);
1072 : chl 181 break;
1073 : edgomez 195 case 2 + 4:
1074 :     CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,
1075 :     starty - iDiamondSize, 1 + 4);
1076 :     CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,
1077 :     starty - iDiamondSize, 2 + 4);
1078 :     CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,
1079 :     starty + iDiamondSize, 2 + 8);
1080 : chl 181 break;
1081 :     case 4:
1082 : edgomez 195 CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,
1083 :     starty - iDiamondSize, 2 + 4);
1084 :     CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,
1085 :     starty - iDiamondSize, 1 + 4);
1086 : chl 181 break;
1087 :     case 8:
1088 : edgomez 195 CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,
1089 :     starty + iDiamondSize, 2 + 8);
1090 :     CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,
1091 :     starty + iDiamondSize, 1 + 8);
1092 : chl 181 break;
1093 : edgomez 195 case 1 + 4:
1094 :     CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,
1095 :     starty + iDiamondSize, 1 + 8);
1096 :     CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,
1097 :     starty - iDiamondSize, 1 + 4);
1098 :     CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,
1099 :     starty - iDiamondSize, 2 + 4);
1100 : chl 181 break;
1101 : edgomez 195 case 2 + 8:
1102 :     CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,
1103 :     starty - iDiamondSize, 1 + 4);
1104 :     CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,
1105 :     starty + iDiamondSize, 1 + 8);
1106 :     CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,
1107 :     starty + iDiamondSize, 2 + 8);
1108 : chl 181 break;
1109 : edgomez 195 case 1 + 8:
1110 :     CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,
1111 :     starty - iDiamondSize, 2 + 4);
1112 :     CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,
1113 :     starty + iDiamondSize, 2 + 8);
1114 :     CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,
1115 :     starty + iDiamondSize, 1 + 8);
1116 : chl 181 break;
1117 : edgomez 195 default: //1+2+4+8 == we didn't find anything at all
1118 :     CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,
1119 :     starty - iDiamondSize, 1 + 4);
1120 :     CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,
1121 :     starty + iDiamondSize, 1 + 8);
1122 :     CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,
1123 :     starty - iDiamondSize, 2 + 4);
1124 :     CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,
1125 :     starty + iDiamondSize, 2 + 8);
1126 : chl 181 break;
1127 :     }
1128 : edgomez 195 if (!(iDirection))
1129 :     break; //ok, the end. really
1130 :     else {
1131 :     bDirection = iDirection;
1132 :     startx = currMV->x;
1133 :     starty = currMV->y;
1134 : chl 181 }
1135 :     }
1136 :     }
1137 : edgomez 195 while (1); //forever
1138 : chl 181 }
1139 :     return iMinSAD;
1140 :     }
1141 :    
1142 :    
1143 : edgomez 195 int32_t
1144 :     Full8_MainSearch(const uint8_t * const pRef,
1145 :     const uint8_t * const pRefH,
1146 :     const uint8_t * const pRefV,
1147 :     const uint8_t * const pRefHV,
1148 :     const uint8_t * const cur,
1149 :     const int x,
1150 :     const int y,
1151 :     int32_t startx,
1152 :     int32_t starty,
1153 :     int32_t iMinSAD,
1154 :     VECTOR * const currMV,
1155 :     const VECTOR * const pmv,
1156 :     const int32_t min_dx,
1157 :     const int32_t max_dx,
1158 :     const int32_t min_dy,
1159 :     const int32_t max_dy,
1160 :     const int32_t iEdgedWidth,
1161 :     const int32_t iDiamondSize,
1162 :     const int32_t iFcode,
1163 :     const int32_t iQuant,
1164 :     int iFound)
1165 : chl 96 {
1166 :     int32_t iSAD;
1167 : edgomez 195 int32_t dx, dy;
1168 : chl 96 VECTOR backupMV;
1169 : edgomez 195
1170 : chl 96 backupMV.x = startx;
1171 :     backupMV.y = starty;
1172 :    
1173 : edgomez 195 for (dx = min_dx; dx <= max_dx; dx += iDiamondSize)
1174 :     for (dy = min_dy; dy <= max_dy; dy += iDiamondSize)
1175 :     NOCHECK_MV8_CANDIDATE(dx, dy);
1176 :    
1177 : chl 96 return iMinSAD;
1178 :     }
1179 :    
1180 :    
1181 :    
1182 : edgomez 195 int32_t
1183 :     Halfpel16_Refine(const uint8_t * const pRef,
1184 :     const uint8_t * const pRefH,
1185 :     const uint8_t * const pRefV,
1186 :     const uint8_t * const pRefHV,
1187 :     const uint8_t * const cur,
1188 :     const int x,
1189 :     const int y,
1190 :     VECTOR * const currMV,
1191 :     int32_t iMinSAD,
1192 :     const VECTOR * const pmv,
1193 :     const int32_t min_dx,
1194 :     const int32_t max_dx,
1195 :     const int32_t min_dy,
1196 :     const int32_t max_dy,
1197 :     const int32_t iFcode,
1198 :     const int32_t iQuant,
1199 :     const int32_t iEdgedWidth)
1200 : Isibaar 3 {
1201 :     /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */
1202 :    
1203 :     int32_t iSAD;
1204 :     VECTOR backupMV = *currMV;
1205 : edgomez 195
1206 :     CHECK_MV16_CANDIDATE(backupMV.x - 1, backupMV.y - 1);
1207 :     CHECK_MV16_CANDIDATE(backupMV.x, backupMV.y - 1);
1208 :     CHECK_MV16_CANDIDATE(backupMV.x + 1, backupMV.y - 1);
1209 :     CHECK_MV16_CANDIDATE(backupMV.x - 1, backupMV.y);
1210 :     CHECK_MV16_CANDIDATE(backupMV.x + 1, backupMV.y);
1211 :     CHECK_MV16_CANDIDATE(backupMV.x - 1, backupMV.y + 1);
1212 :     CHECK_MV16_CANDIDATE(backupMV.x, backupMV.y + 1);
1213 :     CHECK_MV16_CANDIDATE(backupMV.x + 1, backupMV.y + 1);
1214 :    
1215 : Isibaar 3 return iMinSAD;
1216 :     }
1217 :    
1218 :     #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16)
1219 :    
1220 : chl 96
1221 : edgomez 195 int32_t
1222 :     PMVfastSearch16(const uint8_t * const pRef,
1223 :     const uint8_t * const pRefH,
1224 :     const uint8_t * const pRefV,
1225 :     const uint8_t * const pRefHV,
1226 :     const IMAGE * const pCur,
1227 :     const int x,
1228 :     const int y,
1229 :     const uint32_t MotionFlags,
1230 :     const uint32_t iQuant,
1231 :     const uint32_t iFcode,
1232 :     const MBParam * const pParam,
1233 :     const MACROBLOCK * const pMBs,
1234 :     const MACROBLOCK * const prevMBs,
1235 :     VECTOR * const currMV,
1236 :     VECTOR * const currPMV)
1237 : Isibaar 3 {
1238 : edgomez 195 const uint32_t iWcount = pParam->mb_width;
1239 : Isibaar 3 const int32_t iWidth = pParam->width;
1240 :     const int32_t iHeight = pParam->height;
1241 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
1242 : Isibaar 3
1243 : edgomez 195 const uint8_t *cur = pCur->y + x * 16 + y * 16 * iEdgedWidth;
1244 : Isibaar 3
1245 :     int32_t iDiamondSize;
1246 : edgomez 195
1247 : Isibaar 3 int32_t min_dx;
1248 :     int32_t max_dx;
1249 :     int32_t min_dy;
1250 :     int32_t max_dy;
1251 : edgomez 195
1252 : Isibaar 3 int32_t iFound;
1253 :    
1254 :     VECTOR newMV;
1255 : edgomez 195 VECTOR backupMV; /* just for PMVFAST */
1256 :    
1257 : Isibaar 3 VECTOR pmv[4];
1258 :     int32_t psad[4];
1259 : chl 181
1260 :     MainSearch16FuncPtr MainSearchPtr;
1261 : Isibaar 3
1262 : edgomez 195 // const MACROBLOCK * const pMB = pMBs + x + y * iWcount;
1263 :     const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;
1264 : Isibaar 3
1265 : edgomez 195 static int32_t threshA, threshB;
1266 :     int32_t bPredEq;
1267 :     int32_t iMinSAD, iSAD;
1268 :    
1269 : Isibaar 3 /* Get maximum range */
1270 : edgomez 195 get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight,
1271 :     iFcode);
1272 : Isibaar 3
1273 :     /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */
1274 :    
1275 : edgomez 195 if (!(MotionFlags & PMV_HALFPEL16)) {
1276 :     min_dx = EVEN(min_dx);
1277 :     max_dx = EVEN(max_dx);
1278 :     min_dy = EVEN(min_dy);
1279 :     max_dy = EVEN(max_dy);
1280 :     }
1281 : Isibaar 3
1282 : edgomez 195 /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
1283 : suxen_drol 248 bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad, 0, 0);
1284 : Isibaar 3
1285 : edgomez 195 if ((x == 0) && (y == 0)) {
1286 :     threshA = 512;
1287 : Isibaar 3 threshB = 1024;
1288 : edgomez 195
1289 :     } else {
1290 : Isibaar 3 threshA = psad[0];
1291 : edgomez 195 threshB = threshA + 256;
1292 :     if (threshA < 512)
1293 :     threshA = 512;
1294 :     if (threshA > 1024)
1295 :     threshA = 1024;
1296 :     if (threshB > 1792)
1297 :     threshB = 1792;
1298 : Isibaar 3 }
1299 :    
1300 : edgomez 195 iFound = 0;
1301 :    
1302 : Isibaar 3 /* Step 4: Calculate SAD around the Median prediction.
1303 : edgomez 78 MinSAD=SAD
1304 :     If Motion Vector equal to Previous frame motion vector
1305 :     and MinSAD<PrevFrmSAD goto Step 10.
1306 :     If SAD<=256 goto Step 10.
1307 : edgomez 195 */
1308 : Isibaar 3
1309 : edgomez 195 *currMV = pmv[0]; /* current best := prediction */
1310 :     if (!(MotionFlags & PMV_HALFPEL16)) { /* This should NOT be necessary! */
1311 : Isibaar 3 currMV->x = EVEN(currMV->x);
1312 :     currMV->y = EVEN(currMV->y);
1313 :     }
1314 : edgomez 195
1315 :     if (currMV->x > max_dx) {
1316 :     currMV->x = max_dx;
1317 : edgomez 78 }
1318 : edgomez 195 if (currMV->x < min_dx) {
1319 :     currMV->x = min_dx;
1320 : edgomez 78 }
1321 : edgomez 195 if (currMV->y > max_dy) {
1322 :     currMV->y = max_dy;
1323 : edgomez 78 }
1324 : edgomez 195 if (currMV->y < min_dy) {
1325 :     currMV->y = min_dy;
1326 : edgomez 78 }
1327 : edgomez 195
1328 :     iMinSAD =
1329 :     sad16(cur,
1330 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV,
1331 :     iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);
1332 :     iMinSAD +=
1333 :     calc_delta_16(currMV->x - pmv[0].x, currMV->y - pmv[0].y,
1334 :     (uint8_t) iFcode, iQuant);
1335 :    
1336 :     if ((iMinSAD < 256) ||
1337 :     ((MVequal(*currMV, prevMB->mvs[0])) &&
1338 :     ((uint32_t) iMinSAD < prevMB->sad16))) {
1339 :     if (iMinSAD < 2 * iQuant) // high chances for SKIP-mode
1340 :     {
1341 :     if (!MVzero(*currMV)) {
1342 : chl 169 iMinSAD += MV16_00_BIAS;
1343 : edgomez 195 CHECK_MV16_ZERO; // (0,0) saves space for letterboxed pictures
1344 : chl 169 iMinSAD -= MV16_00_BIAS;
1345 :     }
1346 :     }
1347 :    
1348 : edgomez 195 if (MotionFlags & PMV_QUICKSTOP16)
1349 : chl 96 goto PMVfast16_Terminate_without_Refine;
1350 :     if (MotionFlags & PMV_EARLYSTOP16)
1351 :     goto PMVfast16_Terminate_with_Refine;
1352 : edgomez 78 }
1353 : Isibaar 3
1354 : chl 169
1355 :     /* Step 2 (lazy eval): Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion
1356 :     vector of the median.
1357 :     If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2
1358 :     */
1359 :    
1360 : edgomez 195 if ((bPredEq) && (MVequal(pmv[0], prevMB->mvs[0])))
1361 :     iFound = 2;
1362 : chl 169
1363 :     /* Step 3 (lazy eval): If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search.
1364 :     Otherwise select large Diamond Search.
1365 :     */
1366 :    
1367 : edgomez 195 if ((!MVzero(pmv[0])) || (threshB < 1536) || (bPredEq))
1368 :     iDiamondSize = 1; // halfpel!
1369 : chl 169 else
1370 : edgomez 195 iDiamondSize = 2; // halfpel!
1371 : chl 169
1372 : edgomez 195 if (!(MotionFlags & PMV_HALFPELDIAMOND16))
1373 :     iDiamondSize *= 2;
1374 : chl 169
1375 : Isibaar 3 /*
1376 : edgomez 78 Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block.
1377 :     Also calculate (0,0) but do not subtract offset.
1378 :     Let MinSAD be the smallest SAD up to this point.
1379 : chl 140 If MV is (0,0) subtract offset.
1380 : Isibaar 3 */
1381 :    
1382 :     // (0,0) is always possible
1383 :    
1384 : chl 169 if (!MVzero(pmv[0]))
1385 :     CHECK_MV16_ZERO;
1386 : Isibaar 3
1387 :     // previous frame MV is always possible
1388 : chl 169
1389 :     if (!MVzero(prevMB->mvs[0]))
1390 : edgomez 195 if (!MVequal(prevMB->mvs[0], pmv[0]))
1391 :     CHECK_MV16_CANDIDATE(prevMB->mvs[0].x, prevMB->mvs[0].y);
1392 :    
1393 : Isibaar 3 // left neighbour, if allowed
1394 : chl 169
1395 :     if (!MVzero(pmv[1]))
1396 : edgomez 195 if (!MVequal(pmv[1], prevMB->mvs[0]))
1397 :     if (!MVequal(pmv[1], pmv[0])) {
1398 :     if (!(MotionFlags & PMV_HALFPEL16)) {
1399 :     pmv[1].x = EVEN(pmv[1].x);
1400 :     pmv[1].y = EVEN(pmv[1].y);
1401 :     }
1402 : chl 169
1403 : edgomez 195 CHECK_MV16_CANDIDATE(pmv[1].x, pmv[1].y);
1404 :     }
1405 : Isibaar 3 // top neighbour, if allowed
1406 : chl 169 if (!MVzero(pmv[2]))
1407 : edgomez 195 if (!MVequal(pmv[2], prevMB->mvs[0]))
1408 :     if (!MVequal(pmv[2], pmv[0]))
1409 :     if (!MVequal(pmv[2], pmv[1])) {
1410 :     if (!(MotionFlags & PMV_HALFPEL16)) {
1411 :     pmv[2].x = EVEN(pmv[2].x);
1412 :     pmv[2].y = EVEN(pmv[2].y);
1413 :     }
1414 :     CHECK_MV16_CANDIDATE(pmv[2].x, pmv[2].y);
1415 :    
1416 : Isibaar 3 // top right neighbour, if allowed
1417 : edgomez 195 if (!MVzero(pmv[3]))
1418 :     if (!MVequal(pmv[3], prevMB->mvs[0]))
1419 :     if (!MVequal(pmv[3], pmv[0]))
1420 :     if (!MVequal(pmv[3], pmv[1]))
1421 :     if (!MVequal(pmv[3], pmv[2])) {
1422 :     if (!(MotionFlags & PMV_HALFPEL16)) {
1423 :     pmv[3].x = EVEN(pmv[3].x);
1424 :     pmv[3].y = EVEN(pmv[3].y);
1425 :     }
1426 :     CHECK_MV16_CANDIDATE(pmv[3].x,
1427 :     pmv[3].y);
1428 :     }
1429 :     }
1430 :    
1431 :     if ((MVzero(*currMV)) &&
1432 :     (!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96) */ )
1433 : chl 140 iMinSAD -= MV16_00_BIAS;
1434 : Isibaar 3
1435 : edgomez 195
1436 : Isibaar 3 /* Step 6: If MinSAD <= thresa goto Step 10.
1437 :     If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10.
1438 :     */
1439 :    
1440 : edgomez 195 if ((iMinSAD <= threshA) ||
1441 :     (MVequal(*currMV, prevMB->mvs[0]) &&
1442 :     ((uint32_t) iMinSAD < prevMB->sad16))) {
1443 :     if (MotionFlags & PMV_QUICKSTOP16)
1444 : chl 96 goto PMVfast16_Terminate_without_Refine;
1445 :     if (MotionFlags & PMV_EARLYSTOP16)
1446 :     goto PMVfast16_Terminate_with_Refine;
1447 : edgomez 78 }
1448 : Isibaar 3
1449 :    
1450 :     /************ (Diamond Search) **************/
1451 :     /*
1452 : edgomez 78 Step 7: Perform Diamond search, with either the small or large diamond.
1453 :     If Found=2 only examine one Diamond pattern, and afterwards goto step 10
1454 :     Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond.
1455 :     If center then goto step 10.
1456 :     Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center.
1457 :     Refine by using small diamond and goto step 10.
1458 : Isibaar 3 */
1459 :    
1460 : chl 181 if (MotionFlags & PMV_USESQUARES16)
1461 :     MainSearchPtr = Square16_MainSearch;
1462 : edgomez 195 else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
1463 :     MainSearchPtr = AdvDiamond16_MainSearch;
1464 : chl 181 else
1465 : edgomez 195 MainSearchPtr = Diamond16_MainSearch;
1466 : chl 181
1467 : edgomez 195 backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
1468 : Isibaar 3
1469 :     /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
1470 : edgomez 195 iSAD =
1471 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
1472 :     currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx,
1473 :     min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
1474 :     iQuant, iFound);
1475 :    
1476 :     if (iSAD < iMinSAD) {
1477 : Isibaar 3 *currMV = newMV;
1478 :     iMinSAD = iSAD;
1479 :     }
1480 :    
1481 : edgomez 195 if (MotionFlags & PMV_EXTSEARCH16) {
1482 : Isibaar 3 /* extended: search (up to) two more times: orignal prediction and (0,0) */
1483 :    
1484 : edgomez 195 if (!(MVequal(pmv[0], backupMV))) {
1485 :     iSAD =
1486 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
1487 :     pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv,
1488 :     min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
1489 :     iDiamondSize, iFcode, iQuant, iFound);
1490 :    
1491 :     if (iSAD < iMinSAD) {
1492 :     *currMV = newMV;
1493 :     iMinSAD = iSAD;
1494 :     }
1495 : Isibaar 3 }
1496 :    
1497 : edgomez 195 if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
1498 :     iSAD =
1499 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
1500 :     iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy,
1501 :     max_dy, iEdgedWidth, iDiamondSize, iFcode,
1502 :     iQuant, iFound);
1503 :    
1504 :     if (iSAD < iMinSAD) {
1505 :     *currMV = newMV;
1506 :     iMinSAD = iSAD;
1507 :     }
1508 : Isibaar 3 }
1509 :     }
1510 :    
1511 :     /*
1512 : edgomez 78 Step 10: The motion vector is chosen according to the block corresponding to MinSAD.
1513 : Isibaar 3 */
1514 :    
1515 : edgomez 195 PMVfast16_Terminate_with_Refine:
1516 :     if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step
1517 :     iMinSAD =
1518 :     Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
1519 :     iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy,
1520 :     iFcode, iQuant, iEdgedWidth);
1521 : Isibaar 3
1522 : edgomez 195 PMVfast16_Terminate_without_Refine:
1523 : Isibaar 3 currPMV->x = currMV->x - pmv[0].x;
1524 :     currPMV->y = currMV->y - pmv[0].y;
1525 :     return iMinSAD;
1526 :     }
1527 :    
1528 :    
1529 :    
1530 :    
1531 :    
1532 :    
1533 : edgomez 195 int32_t
1534 :     Diamond8_MainSearch(const uint8_t * const pRef,
1535 :     const uint8_t * const pRefH,
1536 :     const uint8_t * const pRefV,
1537 :     const uint8_t * const pRefHV,
1538 :     const uint8_t * const cur,
1539 :     const int x,
1540 :     const int y,
1541 :     int32_t startx,
1542 :     int32_t starty,
1543 :     int32_t iMinSAD,
1544 :     VECTOR * const currMV,
1545 :     const VECTOR * const pmv,
1546 :     const int32_t min_dx,
1547 :     const int32_t max_dx,
1548 :     const int32_t min_dy,
1549 :     const int32_t max_dy,
1550 :     const int32_t iEdgedWidth,
1551 :     const int32_t iDiamondSize,
1552 :     const int32_t iFcode,
1553 :     const int32_t iQuant,
1554 :     int iFound)
1555 : Isibaar 3 {
1556 :     /* Do a diamond search around given starting point, return SAD of best */
1557 :    
1558 : edgomez 195 int32_t iDirection = 0;
1559 : Isibaar 3 int32_t iSAD;
1560 :     VECTOR backupMV;
1561 : edgomez 195
1562 : Isibaar 3 backupMV.x = startx;
1563 :     backupMV.y = starty;
1564 : edgomez 195
1565 : Isibaar 3 /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
1566 :    
1567 : edgomez 195 CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
1568 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
1569 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
1570 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
1571 : Isibaar 3
1572 :     if (iDirection)
1573 : edgomez 195 while (!iFound) {
1574 :     iFound = 1;
1575 :     backupMV = *currMV; // since iDirection!=0, this is well defined!
1576 :    
1577 :     if (iDirection != 2)
1578 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1579 :     backupMV.y, 1);
1580 :     if (iDirection != 1)
1581 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1582 :     backupMV.y, 2);
1583 :     if (iDirection != 4)
1584 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x,
1585 :     backupMV.y - iDiamondSize, 3);
1586 :     if (iDirection != 3)
1587 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x,
1588 :     backupMV.y + iDiamondSize, 4);
1589 :     } else {
1590 : edgomez 78 currMV->x = startx;
1591 :     currMV->y = starty;
1592 :     }
1593 : Isibaar 3 return iMinSAD;
1594 :     }
1595 :    
1596 : edgomez 195 int32_t
1597 :     Halfpel8_Refine(const uint8_t * const pRef,
1598 :     const uint8_t * const pRefH,
1599 :     const uint8_t * const pRefV,
1600 :     const uint8_t * const pRefHV,
1601 :     const uint8_t * const cur,
1602 :     const int x,
1603 :     const int y,
1604 :     VECTOR * const currMV,
1605 :     int32_t iMinSAD,
1606 :     const VECTOR * const pmv,
1607 :     const int32_t min_dx,
1608 :     const int32_t max_dx,
1609 :     const int32_t min_dy,
1610 :     const int32_t max_dy,
1611 :     const int32_t iFcode,
1612 :     const int32_t iQuant,
1613 :     const int32_t iEdgedWidth)
1614 : Isibaar 3 {
1615 :     /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */
1616 :    
1617 :     int32_t iSAD;
1618 :     VECTOR backupMV = *currMV;
1619 : edgomez 195
1620 :     CHECK_MV8_CANDIDATE(backupMV.x - 1, backupMV.y - 1);
1621 :     CHECK_MV8_CANDIDATE(backupMV.x, backupMV.y - 1);
1622 :     CHECK_MV8_CANDIDATE(backupMV.x + 1, backupMV.y - 1);
1623 :     CHECK_MV8_CANDIDATE(backupMV.x - 1, backupMV.y);
1624 :     CHECK_MV8_CANDIDATE(backupMV.x + 1, backupMV.y);
1625 :     CHECK_MV8_CANDIDATE(backupMV.x - 1, backupMV.y + 1);
1626 :     CHECK_MV8_CANDIDATE(backupMV.x, backupMV.y + 1);
1627 :     CHECK_MV8_CANDIDATE(backupMV.x + 1, backupMV.y + 1);
1628 :    
1629 : Isibaar 3 return iMinSAD;
1630 :     }
1631 :    
1632 :    
1633 :     #define PMV_HALFPEL8 (PMV_HALFPELDIAMOND8|PMV_HALFPELREFINE8)
1634 :    
1635 : edgomez 195 int32_t
1636 :     PMVfastSearch8(const uint8_t * const pRef,
1637 :     const uint8_t * const pRefH,
1638 :     const uint8_t * const pRefV,
1639 :     const uint8_t * const pRefHV,
1640 :     const IMAGE * const pCur,
1641 :     const int x,
1642 :     const int y,
1643 :     const int start_x,
1644 :     const int start_y,
1645 :     const uint32_t MotionFlags,
1646 :     const uint32_t iQuant,
1647 :     const uint32_t iFcode,
1648 :     const MBParam * const pParam,
1649 :     const MACROBLOCK * const pMBs,
1650 :     const MACROBLOCK * const prevMBs,
1651 :     VECTOR * const currMV,
1652 :     VECTOR * const currPMV)
1653 : Isibaar 3 {
1654 : edgomez 195 const uint32_t iWcount = pParam->mb_width;
1655 : Isibaar 3 const int32_t iWidth = pParam->width;
1656 :     const int32_t iHeight = pParam->height;
1657 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
1658 : Isibaar 3
1659 : edgomez 195 const uint8_t *cur = pCur->y + x * 8 + y * 8 * iEdgedWidth;
1660 : Isibaar 3
1661 :     int32_t iDiamondSize;
1662 :    
1663 :     int32_t min_dx;
1664 :     int32_t max_dx;
1665 :     int32_t min_dy;
1666 :     int32_t max_dy;
1667 : edgomez 195
1668 : Isibaar 3 VECTOR pmv[4];
1669 :     int32_t psad[4];
1670 :     VECTOR newMV;
1671 :     VECTOR backupMV;
1672 : edgomez 170 VECTOR startMV;
1673 : Isibaar 3
1674 : edgomez 195 // const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount;
1675 :     const MACROBLOCK *const prevMB = prevMBs + (x >> 1) + (y >> 1) * iWcount;
1676 : Isibaar 3
1677 : edgomez 195 static int32_t threshA, threshB;
1678 :     int32_t iFound, bPredEq;
1679 :     int32_t iMinSAD, iSAD;
1680 : Isibaar 3
1681 : edgomez 195 int32_t iSubBlock = (y & 1) + (y & 1) + (x & 1);
1682 :    
1683 : chl 181 MainSearch8FuncPtr MainSearchPtr;
1684 :    
1685 : edgomez 170 /* Init variables */
1686 :     startMV.x = start_x;
1687 :     startMV.y = start_y;
1688 :    
1689 :     /* Get maximum range */
1690 : edgomez 195 get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 8, iWidth, iHeight,
1691 :     iFcode);
1692 : Isibaar 3
1693 : edgomez 195 if (!(MotionFlags & PMV_HALFPELDIAMOND8)) {
1694 :     min_dx = EVEN(min_dx);
1695 :     max_dx = EVEN(max_dx);
1696 :     min_dy = EVEN(min_dy);
1697 :     max_dy = EVEN(max_dy);
1698 :     }
1699 : Isibaar 3
1700 : edgomez 195 /* because we might use IF (dx>max_dx) THEN dx=max_dx; */
1701 :     bPredEq =
1702 : suxen_drol 248 get_pmvdata(pMBs, (x >> 1), (y >> 1), iWcount, iSubBlock, pmv, psad, 0, 0);
1703 : Isibaar 3
1704 : edgomez 195 if ((x == 0) && (y == 0)) {
1705 :     threshA = 512 / 4;
1706 :     threshB = 1024 / 4;
1707 :    
1708 :     } else {
1709 :     threshA = psad[0] / 4; /* good estimate */
1710 :     threshB = threshA + 256 / 4;
1711 :     if (threshA < 512 / 4)
1712 :     threshA = 512 / 4;
1713 :     if (threshA > 1024 / 4)
1714 :     threshA = 1024 / 4;
1715 :     if (threshB > 1792 / 4)
1716 :     threshB = 1792 / 4;
1717 : Isibaar 3 }
1718 :    
1719 : edgomez 195 iFound = 0;
1720 :    
1721 : Isibaar 3 /* Step 4: Calculate SAD around the Median prediction.
1722 : edgomez 78 MinSAD=SAD
1723 :     If Motion Vector equal to Previous frame motion vector
1724 :     and MinSAD<PrevFrmSAD goto Step 10.
1725 :     If SAD<=256 goto Step 10.
1726 : edgomez 195 */
1727 : Isibaar 3
1728 :    
1729 :     // Prepare for main loop
1730 :    
1731 : edgomez 195 // if (MotionFlags & PMV_USESQUARES8)
1732 :     // MainSearchPtr = Square8_MainSearch;
1733 :     // else
1734 : chl 181
1735 :     if (MotionFlags & PMV_ADVANCEDDIAMOND8)
1736 :     MainSearchPtr = AdvDiamond8_MainSearch;
1737 :     else
1738 :     MainSearchPtr = Diamond8_MainSearch;
1739 :    
1740 :    
1741 : chl 169 *currMV = startMV;
1742 : edgomez 195
1743 :     iMinSAD =
1744 :     sad8(cur,
1745 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV,
1746 :     iEdgedWidth), iEdgedWidth);
1747 :     iMinSAD +=
1748 :     calc_delta_8(currMV->x - pmv[0].x, currMV->y - pmv[0].y,
1749 :     (uint8_t) iFcode, iQuant);
1750 :    
1751 :     if ((iMinSAD < 256 / 4) || ((MVequal(*currMV, prevMB->mvs[iSubBlock]))
1752 :     && ((uint32_t) iMinSAD <
1753 :     prevMB->sad8[iSubBlock]))) {
1754 :     if (MotionFlags & PMV_QUICKSTOP16)
1755 : chl 96 goto PMVfast8_Terminate_without_Refine;
1756 :     if (MotionFlags & PMV_EARLYSTOP16)
1757 :     goto PMVfast8_Terminate_with_Refine;
1758 : edgomez 78 }
1759 : Isibaar 3
1760 : chl 169 /* Step 2 (lazy eval): Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion
1761 :     vector of the median.
1762 :     If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2
1763 :     */
1764 : chl 96
1765 : edgomez 195 if ((bPredEq) && (MVequal(pmv[0], prevMB->mvs[iSubBlock])))
1766 :     iFound = 2;
1767 : chl 169
1768 :     /* Step 3 (lazy eval): If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search.
1769 :     Otherwise select large Diamond Search.
1770 :     */
1771 :    
1772 : edgomez 195 if ((!MVzero(pmv[0])) || (threshB < 1536 / 4) || (bPredEq))
1773 :     iDiamondSize = 1; // 1 halfpel!
1774 : chl 169 else
1775 : edgomez 195 iDiamondSize = 2; // 2 halfpel = 1 full pixel!
1776 : chl 169
1777 : edgomez 195 if (!(MotionFlags & PMV_HALFPELDIAMOND8))
1778 :     iDiamondSize *= 2;
1779 : chl 169
1780 :    
1781 : Isibaar 3 /*
1782 : edgomez 78 Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block.
1783 :     Also calculate (0,0) but do not subtract offset.
1784 :     Let MinSAD be the smallest SAD up to this point.
1785 : chl 140 If MV is (0,0) subtract offset.
1786 : Isibaar 3 */
1787 :    
1788 : chl 169 // the median prediction might be even better than mv16
1789 : Isibaar 3
1790 : edgomez 195 if (!MVequal(pmv[0], startMV))
1791 :     CHECK_MV8_CANDIDATE(pmv[0].x, pmv[0].y);
1792 : chl 169
1793 :     // (0,0) if needed
1794 :     if (!MVzero(pmv[0]))
1795 : edgomez 195 if (!MVzero(startMV))
1796 :     CHECK_MV8_ZERO;
1797 : Isibaar 3
1798 : chl 169 // previous frame MV if needed
1799 :     if (!MVzero(prevMB->mvs[iSubBlock]))
1800 : edgomez 195 if (!MVequal(prevMB->mvs[iSubBlock], startMV))
1801 :     if (!MVequal(prevMB->mvs[iSubBlock], pmv[0]))
1802 :     CHECK_MV8_CANDIDATE(prevMB->mvs[iSubBlock].x,
1803 :     prevMB->mvs[iSubBlock].y);
1804 : chl 169
1805 : edgomez 195 if ((iMinSAD <= threshA) ||
1806 :     (MVequal(*currMV, prevMB->mvs[iSubBlock]) &&
1807 :     ((uint32_t) iMinSAD < prevMB->sad8[iSubBlock]))) {
1808 :     if (MotionFlags & PMV_QUICKSTOP16)
1809 : chl 169 goto PMVfast8_Terminate_without_Refine;
1810 :     if (MotionFlags & PMV_EARLYSTOP16)
1811 :     goto PMVfast8_Terminate_with_Refine;
1812 :     }
1813 :    
1814 :     // left neighbour, if allowed and needed
1815 :     if (!MVzero(pmv[1]))
1816 : edgomez 195 if (!MVequal(pmv[1], startMV))
1817 :     if (!MVequal(pmv[1], prevMB->mvs[iSubBlock]))
1818 :     if (!MVequal(pmv[1], pmv[0])) {
1819 :     if (!(MotionFlags & PMV_HALFPEL8)) {
1820 :     pmv[1].x = EVEN(pmv[1].x);
1821 :     pmv[1].y = EVEN(pmv[1].y);
1822 :     }
1823 :     CHECK_MV8_CANDIDATE(pmv[1].x, pmv[1].y);
1824 :     }
1825 : chl 169 // top neighbour, if allowed and needed
1826 :     if (!MVzero(pmv[2]))
1827 : edgomez 195 if (!MVequal(pmv[2], startMV))
1828 :     if (!MVequal(pmv[2], prevMB->mvs[iSubBlock]))
1829 :     if (!MVequal(pmv[2], pmv[0]))
1830 :     if (!MVequal(pmv[2], pmv[1])) {
1831 :     if (!(MotionFlags & PMV_HALFPEL8)) {
1832 :     pmv[2].x = EVEN(pmv[2].x);
1833 :     pmv[2].y = EVEN(pmv[2].y);
1834 :     }
1835 :     CHECK_MV8_CANDIDATE(pmv[2].x, pmv[2].y);
1836 :    
1837 : chl 169 // top right neighbour, if allowed and needed
1838 : edgomez 195 if (!MVzero(pmv[3]))
1839 :     if (!MVequal(pmv[3], startMV))
1840 :     if (!MVequal(pmv[3], prevMB->mvs[iSubBlock]))
1841 :     if (!MVequal(pmv[3], pmv[0]))
1842 :     if (!MVequal(pmv[3], pmv[1]))
1843 :     if (!MVequal(pmv[3], pmv[2])) {
1844 :     if (!
1845 :     (MotionFlags &
1846 :     PMV_HALFPEL8)) {
1847 :     pmv[3].x = EVEN(pmv[3].x);
1848 :     pmv[3].y = EVEN(pmv[3].y);
1849 :     }
1850 :     CHECK_MV8_CANDIDATE(pmv[3].x,
1851 :     pmv[3].y);
1852 :     }
1853 :     }
1854 : Isibaar 3
1855 : edgomez 195 if ((MVzero(*currMV)) &&
1856 :     (!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96) */ )
1857 : chl 140 iMinSAD -= MV8_00_BIAS;
1858 :    
1859 :    
1860 : Isibaar 3 /* Step 6: If MinSAD <= thresa goto Step 10.
1861 :     If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10.
1862 :     */
1863 :    
1864 : edgomez 195 if ((iMinSAD <= threshA) ||
1865 :     (MVequal(*currMV, prevMB->mvs[iSubBlock]) &&
1866 :     ((uint32_t) iMinSAD < prevMB->sad8[iSubBlock]))) {
1867 :     if (MotionFlags & PMV_QUICKSTOP16)
1868 : chl 96 goto PMVfast8_Terminate_without_Refine;
1869 :     if (MotionFlags & PMV_EARLYSTOP16)
1870 :     goto PMVfast8_Terminate_with_Refine;
1871 : edgomez 78 }
1872 : Isibaar 3
1873 :     /************ (Diamond Search) **************/
1874 :     /*
1875 : edgomez 78 Step 7: Perform Diamond search, with either the small or large diamond.
1876 :     If Found=2 only examine one Diamond pattern, and afterwards goto step 10
1877 :     Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond.
1878 :     If center then goto step 10.
1879 :     Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center.
1880 :     Refine by using small diamond and goto step 10.
1881 : Isibaar 3 */
1882 :    
1883 : edgomez 195 backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
1884 : Isibaar 3
1885 :     /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
1886 : edgomez 195 iSAD =
1887 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
1888 :     currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx,
1889 :     min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
1890 :     iQuant, iFound);
1891 :    
1892 :     if (iSAD < iMinSAD) {
1893 : Isibaar 3 *currMV = newMV;
1894 :     iMinSAD = iSAD;
1895 :     }
1896 :    
1897 : edgomez 195 if (MotionFlags & PMV_EXTSEARCH8) {
1898 : Isibaar 3 /* extended: search (up to) two more times: orignal prediction and (0,0) */
1899 :    
1900 : edgomez 195 if (!(MVequal(pmv[0], backupMV))) {
1901 :     iSAD =
1902 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
1903 :     pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv,
1904 :     min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
1905 :     iDiamondSize, iFcode, iQuant, iFound);
1906 :    
1907 :     if (iSAD < iMinSAD) {
1908 :     *currMV = newMV;
1909 :     iMinSAD = iSAD;
1910 :     }
1911 : Isibaar 3 }
1912 :    
1913 : edgomez 195 if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
1914 :     iSAD =
1915 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
1916 :     iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy,
1917 :     max_dy, iEdgedWidth, iDiamondSize, iFcode,
1918 :     iQuant, iFound);
1919 :    
1920 :     if (iSAD < iMinSAD) {
1921 :     *currMV = newMV;
1922 :     iMinSAD = iSAD;
1923 :     }
1924 : Isibaar 3 }
1925 :     }
1926 :    
1927 :     /* Step 10: The motion vector is chosen according to the block corresponding to MinSAD.
1928 : edgomez 78 By performing an optional local half-pixel search, we can refine this result even further.
1929 : Isibaar 3 */
1930 :    
1931 : edgomez 195 PMVfast8_Terminate_with_Refine:
1932 :     if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step
1933 :     iMinSAD =
1934 :     Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
1935 :     iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy,
1936 :     iFcode, iQuant, iEdgedWidth);
1937 : Isibaar 3
1938 : edgomez 195
1939 :     PMVfast8_Terminate_without_Refine:
1940 : Isibaar 3 currPMV->x = currMV->x - pmv[0].x;
1941 :     currPMV->y = currMV->y - pmv[0].y;
1942 : edgomez 195
1943 : Isibaar 3 return iMinSAD;
1944 :     }
1945 : chl 96
1946 : edgomez 195 int32_t
1947 :     EPZSSearch16(const uint8_t * const pRef,
1948 :     const uint8_t * const pRefH,
1949 :     const uint8_t * const pRefV,
1950 :     const uint8_t * const pRefHV,
1951 :     const IMAGE * const pCur,
1952 :     const int x,
1953 :     const int y,
1954 :     const uint32_t MotionFlags,
1955 :     const uint32_t iQuant,
1956 :     const uint32_t iFcode,
1957 :     const MBParam * const pParam,
1958 :     const MACROBLOCK * const pMBs,
1959 :     const MACROBLOCK * const prevMBs,
1960 :     VECTOR * const currMV,
1961 :     VECTOR * const currPMV)
1962 : chl 96 {
1963 : edgomez 195 const uint32_t iWcount = pParam->mb_width;
1964 :     const uint32_t iHcount = pParam->mb_height;
1965 : chl 96
1966 :     const int32_t iWidth = pParam->width;
1967 :     const int32_t iHeight = pParam->height;
1968 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
1969 : chl 96
1970 : edgomez 195 const uint8_t *cur = pCur->y + x * 16 + y * 16 * iEdgedWidth;
1971 : chl 96
1972 :     int32_t min_dx;
1973 :     int32_t max_dx;
1974 :     int32_t min_dy;
1975 :     int32_t max_dy;
1976 : edgomez 195
1977 : chl 96 VECTOR newMV;
1978 :     VECTOR backupMV;
1979 : edgomez 195
1980 : chl 96 VECTOR pmv[4];
1981 :     int32_t psad[8];
1982 :    
1983 : edgomez 195 static MACROBLOCK *oldMBs = NULL;
1984 :    
1985 :     // const MACROBLOCK * const pMB = pMBs + x + y * iWcount;
1986 :     const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;
1987 :     MACROBLOCK *oldMB = NULL;
1988 :    
1989 : chl 96 static int32_t thresh2;
1990 : edgomez 195 int32_t bPredEq;
1991 :     int32_t iMinSAD, iSAD = 9999;
1992 : chl 96
1993 : chl 181 MainSearch16FuncPtr MainSearchPtr;
1994 : chl 96
1995 : edgomez 195 if (oldMBs == NULL) {
1996 :     oldMBs = (MACROBLOCK *) calloc(iWcount * iHcount, sizeof(MACROBLOCK));
1997 :     // fprintf(stderr,"allocated %d bytes for oldMBs\n",iWcount*iHcount*sizeof(MACROBLOCK));
1998 : chl 96 }
1999 :     oldMB = oldMBs + x + y * iWcount;
2000 :    
2001 :     /* Get maximum range */
2002 : edgomez 195 get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight,
2003 :     iFcode);
2004 : chl 96
2005 : edgomez 195 if (!(MotionFlags & PMV_HALFPEL16)) {
2006 :     min_dx = EVEN(min_dx);
2007 :     max_dx = EVEN(max_dx);
2008 :     min_dy = EVEN(min_dy);
2009 :     max_dy = EVEN(max_dy);
2010 :     }
2011 :     /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
2012 : suxen_drol 248 bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad, 0, 0);
2013 : chl 96
2014 :     /* Step 4: Calculate SAD around the Median prediction.
2015 :     MinSAD=SAD
2016 :     If Motion Vector equal to Previous frame motion vector
2017 :     and MinSAD<PrevFrmSAD goto Step 10.
2018 :     If SAD<=256 goto Step 10.
2019 : edgomez 195 */
2020 : chl 96
2021 :     // Prepare for main loop
2022 :    
2023 : edgomez 195 *currMV = pmv[0]; /* current best := median prediction */
2024 :     if (!(MotionFlags & PMV_HALFPEL16)) {
2025 : chl 96 currMV->x = EVEN(currMV->x);
2026 :     currMV->y = EVEN(currMV->y);
2027 :     }
2028 :    
2029 : edgomez 195 if (currMV->x > max_dx)
2030 :     currMV->x = max_dx;
2031 :     if (currMV->x < min_dx)
2032 :     currMV->x = min_dx;
2033 :     if (currMV->y > max_dy)
2034 :     currMV->y = max_dy;
2035 :     if (currMV->y < min_dy)
2036 :     currMV->y = min_dy;
2037 :    
2038 :     /***************** This is predictor SET A: only median prediction ******************/
2039 :    
2040 :     iMinSAD =
2041 :     sad16(cur,
2042 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV,
2043 :     iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);
2044 :     iMinSAD +=
2045 :     calc_delta_16(currMV->x - pmv[0].x, currMV->y - pmv[0].y,
2046 :     (uint8_t) iFcode, iQuant);
2047 :    
2048 : chl 96 // thresh1 is fixed to 256
2049 : edgomez 195 if ((iMinSAD < 256) ||
2050 :     ((MVequal(*currMV, prevMB->mvs[0])) &&
2051 :     ((uint32_t) iMinSAD < prevMB->sad16))) {
2052 :     if (MotionFlags & PMV_QUICKSTOP16)
2053 :     goto EPZS16_Terminate_without_Refine;
2054 :     if (MotionFlags & PMV_EARLYSTOP16)
2055 :     goto EPZS16_Terminate_with_Refine;
2056 :     }
2057 : chl 96
2058 : edgomez 195 /************** This is predictor SET B: (0,0), prev.frame MV, neighbours **************/
2059 : chl 96
2060 :     // previous frame MV
2061 : edgomez 195 CHECK_MV16_CANDIDATE(prevMB->mvs[0].x, prevMB->mvs[0].y);
2062 : chl 96
2063 :     // set threshhold based on Min of Prediction and SAD of collocated block
2064 :     // CHECK_MV16 always uses iSAD for the SAD of last vector to check, so now iSAD is what we want
2065 :    
2066 : edgomez 195 if ((x == 0) && (y == 0)) {
2067 :     thresh2 = 512;
2068 :     } else {
2069 : chl 96 /* T_k = 1.2 * MIN(SAD_top,SAD_left,SAD_topleft,SAD_coll) +128; [Tourapis, 2002] */
2070 :    
2071 : edgomez 195 thresh2 = MIN(psad[0], iSAD) * 6 / 5 + 128;
2072 : chl 96 }
2073 :    
2074 :     // MV=(0,0) is often a good choice
2075 :    
2076 :     CHECK_MV16_ZERO;
2077 :    
2078 : edgomez 195
2079 : chl 96 // left neighbour, if allowed
2080 : edgomez 195 if (x != 0) {
2081 :     if (!(MotionFlags & PMV_HALFPEL16)) {
2082 :     pmv[1].x = EVEN(pmv[1].x);
2083 : chl 96 pmv[1].y = EVEN(pmv[1].y);
2084 :     }
2085 : edgomez 195 CHECK_MV16_CANDIDATE(pmv[1].x, pmv[1].y);
2086 : chl 96 }
2087 :     // top neighbour, if allowed
2088 : edgomez 195 if (y != 0) {
2089 :     if (!(MotionFlags & PMV_HALFPEL16)) {
2090 :     pmv[2].x = EVEN(pmv[2].x);
2091 : chl 96 pmv[2].y = EVEN(pmv[2].y);
2092 :     }
2093 : edgomez 195 CHECK_MV16_CANDIDATE(pmv[2].x, pmv[2].y);
2094 :    
2095 : chl 96 // top right neighbour, if allowed
2096 : edgomez 195 if ((uint32_t) x != (iWcount - 1)) {
2097 :     if (!(MotionFlags & PMV_HALFPEL16)) {
2098 :     pmv[3].x = EVEN(pmv[3].x);
2099 : chl 96 pmv[3].y = EVEN(pmv[3].y);
2100 :     }
2101 : edgomez 195 CHECK_MV16_CANDIDATE(pmv[3].x, pmv[3].y);
2102 : chl 96 }
2103 :     }
2104 :    
2105 :     /* Terminate if MinSAD <= T_2
2106 :     Terminate if MV[t] == MV[t-1] and MinSAD[t] <= MinSAD[t-1]
2107 :     */
2108 :    
2109 : edgomez 195 if ((iMinSAD <= thresh2)
2110 :     || (MVequal(*currMV, prevMB->mvs[0]) &&
2111 :     ((uint32_t) iMinSAD <= prevMB->sad16))) {
2112 :     if (MotionFlags & PMV_QUICKSTOP16)
2113 :     goto EPZS16_Terminate_without_Refine;
2114 :     if (MotionFlags & PMV_EARLYSTOP16)
2115 :     goto EPZS16_Terminate_with_Refine;
2116 :     }
2117 : chl 96
2118 :     /***** predictor SET C: acceleration MV (new!), neighbours in prev. frame(new!) ****/
2119 :    
2120 : edgomez 195 backupMV = prevMB->mvs[0]; // collocated MV
2121 :     backupMV.x += (prevMB->mvs[0].x - oldMB->mvs[0].x); // acceleration X
2122 :     backupMV.y += (prevMB->mvs[0].y - oldMB->mvs[0].y); // acceleration Y
2123 : chl 96
2124 : edgomez 195 CHECK_MV16_CANDIDATE(backupMV.x, backupMV.y);
2125 : chl 96
2126 :     // left neighbour
2127 : edgomez 195 if (x != 0)
2128 :     CHECK_MV16_CANDIDATE((prevMB - 1)->mvs[0].x, (prevMB - 1)->mvs[0].y);
2129 : chl 96
2130 :     // top neighbour
2131 :     if (y != 0)
2132 : edgomez 195 CHECK_MV16_CANDIDATE((prevMB - iWcount)->mvs[0].x,
2133 :     (prevMB - iWcount)->mvs[0].y);
2134 : chl 96
2135 :     // right neighbour, if allowed (this value is not written yet, so take it from pMB->mvs
2136 :    
2137 : edgomez 195 if ((uint32_t) x != iWcount - 1)
2138 :     CHECK_MV16_CANDIDATE((prevMB + 1)->mvs[0].x, (prevMB + 1)->mvs[0].y);
2139 : chl 96
2140 :     // bottom neighbour, dito
2141 : edgomez 195 if ((uint32_t) y != iHcount - 1)
2142 :     CHECK_MV16_CANDIDATE((prevMB + iWcount)->mvs[0].x,
2143 :     (prevMB + iWcount)->mvs[0].y);
2144 : chl 96
2145 :     /* Terminate if MinSAD <= T_3 (here T_3 = T_2) */
2146 : edgomez 195 if (iMinSAD <= thresh2) {
2147 :     if (MotionFlags & PMV_QUICKSTOP16)
2148 :     goto EPZS16_Terminate_without_Refine;
2149 :     if (MotionFlags & PMV_EARLYSTOP16)
2150 :     goto EPZS16_Terminate_with_Refine;
2151 :     }
2152 : chl 96
2153 :     /************ (if Diamond Search) **************/
2154 :    
2155 : edgomez 195 backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
2156 : chl 96
2157 : chl 184 if (MotionFlags & PMV_USESQUARES8)
2158 :     MainSearchPtr = Square16_MainSearch;
2159 :     else
2160 : edgomez 195 if (MotionFlags & PMV_ADVANCEDDIAMOND8)
2161 : chl 184 MainSearchPtr = AdvDiamond16_MainSearch;
2162 :     else
2163 :     MainSearchPtr = Diamond16_MainSearch;
2164 :    
2165 : chl 96 /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
2166 :    
2167 : edgomez 195 iSAD =
2168 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
2169 :     currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx,
2170 :     min_dy, max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);
2171 :    
2172 :     if (iSAD < iMinSAD) {
2173 : chl 96 *currMV = newMV;
2174 :     iMinSAD = iSAD;
2175 :     }
2176 :    
2177 :    
2178 : edgomez 195 if (MotionFlags & PMV_EXTSEARCH16) {
2179 : chl 96 /* extended mode: search (up to) two more times: orignal prediction and (0,0) */
2180 :    
2181 : edgomez 195 if (!(MVequal(pmv[0], backupMV))) {
2182 :     iSAD =
2183 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
2184 :     pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv,
2185 :     min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
2186 :     2, iFcode, iQuant, 0);
2187 : chl 96 }
2188 : edgomez 195
2189 :     if (iSAD < iMinSAD) {
2190 : chl 96 *currMV = newMV;
2191 :     iMinSAD = iSAD;
2192 :     }
2193 : edgomez 195
2194 :     if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2195 :     iSAD =
2196 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2197 :     iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy,
2198 :     max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);
2199 :    
2200 :     if (iSAD < iMinSAD) {
2201 : chl 96 *currMV = newMV;
2202 :     iMinSAD = iSAD;
2203 :     }
2204 :     }
2205 :     }
2206 :    
2207 :     /*************** Choose best MV found **************/
2208 :    
2209 : edgomez 195 EPZS16_Terminate_with_Refine:
2210 :     if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step
2211 :     iMinSAD =
2212 :     Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2213 :     iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy,
2214 :     iFcode, iQuant, iEdgedWidth);
2215 : chl 96
2216 : edgomez 195 EPZS16_Terminate_without_Refine:
2217 : chl 96
2218 : chl 141 *oldMB = *prevMB;
2219 : edgomez 195
2220 : chl 96 currPMV->x = currMV->x - pmv[0].x;
2221 :     currPMV->y = currMV->y - pmv[0].y;
2222 :     return iMinSAD;
2223 :     }
2224 :    
2225 :    
2226 : edgomez 195 int32_t
2227 :     EPZSSearch8(const uint8_t * const pRef,
2228 :     const uint8_t * const pRefH,
2229 :     const uint8_t * const pRefV,
2230 :     const uint8_t * const pRefHV,
2231 :     const IMAGE * const pCur,
2232 :     const int x,
2233 :     const int y,
2234 :     const int start_x,
2235 :     const int start_y,
2236 :     const uint32_t MotionFlags,
2237 :     const uint32_t iQuant,
2238 :     const uint32_t iFcode,
2239 :     const MBParam * const pParam,
2240 :     const MACROBLOCK * const pMBs,
2241 :     const MACROBLOCK * const prevMBs,
2242 :     VECTOR * const currMV,
2243 :     VECTOR * const currPMV)
2244 : chl 96 {
2245 : chl 141 /* Please not that EPZS might not be a good choice for 8x8-block motion search ! */
2246 :    
2247 :     const uint32_t iWcount = pParam->mb_width;
2248 : chl 96 const int32_t iWidth = pParam->width;
2249 :     const int32_t iHeight = pParam->height;
2250 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
2251 : chl 96
2252 : edgomez 195 const uint8_t *cur = pCur->y + x * 8 + y * 8 * iEdgedWidth;
2253 : chl 96
2254 : edgomez 195 int32_t iDiamondSize = 1;
2255 :    
2256 : chl 96 int32_t min_dx;
2257 :     int32_t max_dx;
2258 :     int32_t min_dy;
2259 :     int32_t max_dy;
2260 : edgomez 195
2261 : chl 96 VECTOR newMV;
2262 :     VECTOR backupMV;
2263 : edgomez 195
2264 : chl 96 VECTOR pmv[4];
2265 :     int32_t psad[8];
2266 :    
2267 : edgomez 195 const int32_t iSubBlock = ((y & 1) << 1) + (x & 1);
2268 : chl 96
2269 : edgomez 195 // const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount;
2270 :     const MACROBLOCK *const prevMB = prevMBs + (x >> 1) + (y >> 1) * iWcount;
2271 : chl 96
2272 : edgomez 195 int32_t bPredEq;
2273 :     int32_t iMinSAD, iSAD = 9999;
2274 :    
2275 : chl 181 MainSearch8FuncPtr MainSearchPtr;
2276 : chl 96
2277 :     /* Get maximum range */
2278 : edgomez 195 get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 8, iWidth, iHeight,
2279 :     iFcode);
2280 : chl 96
2281 :     /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */
2282 :    
2283 : edgomez 195 if (!(MotionFlags & PMV_HALFPEL8)) {
2284 :     min_dx = EVEN(min_dx);
2285 :     max_dx = EVEN(max_dx);
2286 :     min_dy = EVEN(min_dy);
2287 :     max_dy = EVEN(max_dy);
2288 :     }
2289 :     /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
2290 : suxen_drol 248 bPredEq = get_pmvdata(pMBs, x >> 1, y >> 1, iWcount, iSubBlock, pmv, psad, 0, 0);
2291 : chl 96
2292 :    
2293 :     /* Step 4: Calculate SAD around the Median prediction.
2294 :     MinSAD=SAD
2295 :     If Motion Vector equal to Previous frame motion vector
2296 :     and MinSAD<PrevFrmSAD goto Step 10.
2297 :     If SAD<=256 goto Step 10.
2298 :     */
2299 :    
2300 :     // Prepare for main loop
2301 :    
2302 : edgomez 195
2303 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2304 : chl 96 currMV->x = EVEN(currMV->x);
2305 :     currMV->y = EVEN(currMV->y);
2306 :     }
2307 :    
2308 : edgomez 195 if (currMV->x > max_dx)
2309 :     currMV->x = max_dx;
2310 :     if (currMV->x < min_dx)
2311 :     currMV->x = min_dx;
2312 :     if (currMV->y > max_dy)
2313 :     currMV->y = max_dy;
2314 :     if (currMV->y < min_dy)
2315 :     currMV->y = min_dy;
2316 : chl 96
2317 : edgomez 195 /***************** This is predictor SET A: only median prediction ******************/
2318 : chl 96
2319 : edgomez 195
2320 :     iMinSAD =
2321 :     sad8(cur,
2322 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV,
2323 :     iEdgedWidth), iEdgedWidth);
2324 :     iMinSAD +=
2325 :     calc_delta_8(currMV->x - pmv[0].x, currMV->y - pmv[0].y,
2326 :     (uint8_t) iFcode, iQuant);
2327 :    
2328 :    
2329 : chl 96 // thresh1 is fixed to 256
2330 : edgomez 195 if (iMinSAD < 256 / 4) {
2331 :     if (MotionFlags & PMV_QUICKSTOP8)
2332 :     goto EPZS8_Terminate_without_Refine;
2333 :     if (MotionFlags & PMV_EARLYSTOP8)
2334 :     goto EPZS8_Terminate_with_Refine;
2335 :     }
2336 : chl 96
2337 : edgomez 195 /************** This is predictor SET B: (0,0), prev.frame MV, neighbours **************/
2338 : chl 96
2339 :    
2340 :     // MV=(0,0) is often a good choice
2341 :     CHECK_MV8_ZERO;
2342 :    
2343 : chl 141 // previous frame MV
2344 : edgomez 195 CHECK_MV8_CANDIDATE(prevMB->mvs[iSubBlock].x, prevMB->mvs[iSubBlock].y);
2345 :    
2346 : chl 141 // left neighbour, if allowed
2347 : edgomez 195 if (psad[1] != MV_MAX_ERROR) {
2348 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2349 :     pmv[1].x = EVEN(pmv[1].x);
2350 : chl 141 pmv[1].y = EVEN(pmv[1].y);
2351 :     }
2352 : edgomez 195 CHECK_MV8_CANDIDATE(pmv[1].x, pmv[1].y);
2353 : chl 141 }
2354 :     // top neighbour, if allowed
2355 : edgomez 195 if (psad[2] != MV_MAX_ERROR) {
2356 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2357 :     pmv[2].x = EVEN(pmv[2].x);
2358 : chl 141 pmv[2].y = EVEN(pmv[2].y);
2359 :     }
2360 : edgomez 195 CHECK_MV8_CANDIDATE(pmv[2].x, pmv[2].y);
2361 :    
2362 : chl 141 // top right neighbour, if allowed
2363 : edgomez 195 if (psad[3] != MV_MAX_ERROR) {
2364 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2365 :     pmv[3].x = EVEN(pmv[3].x);
2366 : chl 141 pmv[3].y = EVEN(pmv[3].y);
2367 :     }
2368 : edgomez 195 CHECK_MV8_CANDIDATE(pmv[3].x, pmv[3].y);
2369 : chl 141 }
2370 :     }
2371 :    
2372 :     /* // this bias is zero anyway, at the moment!
2373 :    
2374 :     if ( (MVzero(*currMV)) && (!MVzero(pmv[0])) ) // && (iMinSAD <= iQuant * 96)
2375 :     iMinSAD -= MV8_00_BIAS;
2376 :    
2377 : edgomez 195 */
2378 : chl 141
2379 : chl 96 /* Terminate if MinSAD <= T_2
2380 :     Terminate if MV[t] == MV[t-1] and MinSAD[t] <= MinSAD[t-1]
2381 :     */
2382 :    
2383 : edgomez 195 if (iMinSAD < 512 / 4) { /* T_2 == 512/4 hardcoded */
2384 :     if (MotionFlags & PMV_QUICKSTOP8)
2385 :     goto EPZS8_Terminate_without_Refine;
2386 :     if (MotionFlags & PMV_EARLYSTOP8)
2387 :     goto EPZS8_Terminate_with_Refine;
2388 :     }
2389 : chl 96
2390 : chl 141 /************ (Diamond Search) **************/
2391 : chl 96
2392 : edgomez 195 backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
2393 : chl 96
2394 :     if (!(MotionFlags & PMV_HALFPELDIAMOND8))
2395 :     iDiamondSize *= 2;
2396 : edgomez 195
2397 : chl 141 /* default: use best prediction as starting point for one call of EPZS_MainSearch */
2398 : chl 96
2399 : chl 184 // there is no EPZS^2 for inter4v at the moment
2400 : chl 141
2401 : edgomez 195 // if (MotionFlags & PMV_USESQUARES8)
2402 :     // MainSearchPtr = Square8_MainSearch;
2403 :     // else
2404 : chl 181
2405 :     if (MotionFlags & PMV_ADVANCEDDIAMOND8)
2406 :     MainSearchPtr = AdvDiamond8_MainSearch;
2407 :     else
2408 :     MainSearchPtr = Diamond8_MainSearch;
2409 :    
2410 : edgomez 195 iSAD =
2411 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
2412 :     currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx,
2413 :     min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
2414 :     iQuant, 0);
2415 : chl 96
2416 : chl 141
2417 : edgomez 195 if (iSAD < iMinSAD) {
2418 : chl 96 *currMV = newMV;
2419 :     iMinSAD = iSAD;
2420 :     }
2421 :    
2422 : edgomez 195 if (MotionFlags & PMV_EXTSEARCH8) {
2423 : chl 96 /* extended mode: search (up to) two more times: orignal prediction and (0,0) */
2424 :    
2425 : edgomez 195 if (!(MVequal(pmv[0], backupMV))) {
2426 :     iSAD =
2427 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
2428 :     pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv,
2429 :     min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
2430 :     iDiamondSize, iFcode, iQuant, 0);
2431 :    
2432 :     if (iSAD < iMinSAD) {
2433 : chl 96 *currMV = newMV;
2434 :     iMinSAD = iSAD;
2435 :     }
2436 :     }
2437 :    
2438 : edgomez 195 if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2439 :     iSAD =
2440 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2441 :     iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy,
2442 :     max_dy, iEdgedWidth, iDiamondSize, iFcode,
2443 :     iQuant, 0);
2444 :    
2445 :     if (iSAD < iMinSAD) {
2446 : chl 96 *currMV = newMV;
2447 :     iMinSAD = iSAD;
2448 :     }
2449 :     }
2450 :     }
2451 :    
2452 :     /*************** Choose best MV found **************/
2453 :    
2454 : edgomez 195 EPZS8_Terminate_with_Refine:
2455 :     if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step
2456 :     iMinSAD =
2457 :     Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2458 :     iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy,
2459 :     iFcode, iQuant, iEdgedWidth);
2460 : chl 96
2461 : edgomez 195 EPZS8_Terminate_without_Refine:
2462 : chl 96
2463 :     currPMV->x = currMV->x - pmv[0].x;
2464 :     currPMV->y = currMV->y - pmv[0].y;
2465 :     return iMinSAD;
2466 :     }
2467 :    
2468 : suxen_drol 118
2469 :    
2470 :    
2471 :    
2472 :     /* ***********************************************************
2473 :     bvop motion estimation
2474 :     // TODO: need to incorporate prediction here (eg. sad += calc_delta_16)
2475 :     ***************************************************************/
2476 :    
2477 : suxen_drol 152
2478 : edgomez 195 void
2479 :     MotionEstimationBVOP(MBParam * const pParam,
2480 :     FRAMEINFO * const frame,
2481 :     // forward (past) reference
2482 :     const MACROBLOCK * const f_mbs,
2483 :     const IMAGE * const f_ref,
2484 :     const IMAGE * const f_refH,
2485 :     const IMAGE * const f_refV,
2486 :     const IMAGE * const f_refHV,
2487 :     // backward (future) reference
2488 :     const MACROBLOCK * const b_mbs,
2489 :     const IMAGE * const b_ref,
2490 :     const IMAGE * const b_refH,
2491 :     const IMAGE * const b_refV,
2492 :     const IMAGE * const b_refHV)
2493 : suxen_drol 118 {
2494 : edgomez 195 const uint32_t mb_width = pParam->mb_width;
2495 :     const uint32_t mb_height = pParam->mb_height;
2496 : suxen_drol 118 const int32_t edged_width = pParam->edged_width;
2497 :    
2498 : edgomez 195 uint32_t i, j;
2499 :    
2500 : suxen_drol 118 int32_t f_sad16;
2501 :     int32_t b_sad16;
2502 :     int32_t i_sad16;
2503 :     int32_t d_sad16;
2504 :     int32_t best_sad;
2505 :    
2506 :     VECTOR pmv_dontcare;
2507 :    
2508 :     // note: i==horizontal, j==vertical
2509 : edgomez 195 for (j = 0; j < mb_height; j++) {
2510 :     for (i = 0; i < mb_width; i++) {
2511 :     MACROBLOCK *mb = &frame->mbs[i + j * mb_width];
2512 :     const MACROBLOCK *f_mb = &f_mbs[i + j * mb_width];
2513 :     const MACROBLOCK *b_mb = &b_mbs[i + j * mb_width];
2514 : suxen_drol 118
2515 : edgomez 195 if (b_mb->mode == MODE_INTER && b_mb->cbp == 0 &&
2516 :     b_mb->mvs[0].x == 0 && b_mb->mvs[0].y == 0) {
2517 : suxen_drol 152 mb->mode = MODE_NOT_CODED;
2518 : suxen_drol 118 mb->mvs[0].x = 0;
2519 :     mb->mvs[0].y = 0;
2520 :     mb->b_mvs[0].x = 0;
2521 :     mb->b_mvs[0].y = 0;
2522 :     continue;
2523 :     }
2524 : suxen_drol 234 /* force F_SAD16
2525 :     f_sad16 = 100;
2526 :     b_sad16 = 65535;
2527 :    
2528 :     mb->mode = MODE_FORWARD;
2529 :     mb->mvs[0].x = 1;
2530 :     mb->mvs[0].y = 1;
2531 :     mb->b_mvs[0].x = 1;
2532 :     mb->b_mvs[0].y = 1;
2533 :     continue;
2534 :     ^^ force F_SAD16 */
2535 : suxen_drol 118
2536 : suxen_drol 234
2537 : suxen_drol 118 // forward search
2538 : edgomez 195 f_sad16 =
2539 :     SEARCH16(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,
2540 :     &frame->image, i, j, frame->motion_flags,
2541 : suxen_drol 234 frame->quant, frame->fcode, pParam,
2542 :     f_mbs, f_mbs, /* todo */
2543 : edgomez 195 &mb->mvs[0], &pmv_dontcare); // ignore pmv
2544 : suxen_drol 118
2545 :     // backward search
2546 : suxen_drol 234 b_sad16 = SEARCH16(b_ref->y, b_refH->y, b_refV->y, b_refHV->y,
2547 :     &frame->image, i, j, frame->motion_flags,
2548 :     frame->quant, frame->bcode, pParam,
2549 :     b_mbs, b_mbs, /* todo */
2550 :     &mb->b_mvs[0], &pmv_dontcare); // ignore pmv
2551 : suxen_drol 118
2552 :     // interpolate search (simple, but effective)
2553 : suxen_drol 232 i_sad16 = 65535;
2554 :    
2555 :     /*
2556 :     x/y range somewhat buggy
2557 : edgomez 195 i_sad16 =
2558 :     sad16bi_c(frame->image.y + i * 16 + j * 16 * edged_width,
2559 :     get_ref(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,
2560 :     i, j, 16, mb->mvs[0].x, mb->mvs[0].y,
2561 :     edged_width), get_ref(b_ref->y, b_refH->y,
2562 :     b_refV->y, b_refHV->y,
2563 :     i, j, 16,
2564 :     mb->b_mvs[0].x,
2565 :     mb->b_mvs[0].x,
2566 :     edged_width),
2567 :     edged_width);
2568 : suxen_drol 232 */
2569 : suxen_drol 118
2570 :     // TODO: direct search
2571 :     // predictor + range of [-32,32]
2572 :     d_sad16 = 65535;
2573 : edgomez 195
2574 :    
2575 :     if (f_sad16 < b_sad16) {
2576 : suxen_drol 118 best_sad = f_sad16;
2577 : suxen_drol 152 mb->mode = MODE_FORWARD;
2578 : edgomez 195 } else {
2579 : suxen_drol 118 best_sad = b_sad16;
2580 : suxen_drol 152 mb->mode = MODE_BACKWARD;
2581 : suxen_drol 118 }
2582 : edgomez 195
2583 :     if (i_sad16 < best_sad) {
2584 : suxen_drol 118 best_sad = i_sad16;
2585 : suxen_drol 152 mb->mode = MODE_INTERPOLATE;
2586 : suxen_drol 118 }
2587 :    
2588 : edgomez 195 if (d_sad16 < best_sad) {
2589 : suxen_drol 118 best_sad = d_sad16;
2590 : suxen_drol 152 mb->mode = MODE_DIRECT;
2591 : suxen_drol 118 }
2592 :    
2593 :     }
2594 :     }
2595 :     }

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