[svn] / branches / dev-api-3 / xvidcore / src / motion / motion_est.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-3/xvidcore/src/motion/motion_est.c

Parent Directory Parent Directory | Revision Log Revision Log


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

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 :     /* sad16(0,0) bias; mpeg4 spec suggests nb/2+1 */
58 :     /* nb = vop pixels * 2^(bpp-8) */
59 :     #define MV16_00_BIAS (128+1)
60 : chl 140 #define MV8_00_BIAS (0)
61 : Isibaar 3
62 :     /* INTER bias for INTER/INTRA decision; mpeg4 spec suggests 2*nb */
63 :     #define INTER_BIAS 512
64 :    
65 :     /* Parameters which control inter/inter4v decision */
66 :     #define IMV16X16 5
67 :    
68 :     /* vector map (vlc delta size) smoother parameters */
69 :     #define NEIGH_TEND_16X16 2
70 :     #define NEIGH_TEND_8X8 2
71 :    
72 :    
73 :     // fast ((A)/2)*2
74 :     #define EVEN(A) (((A)<0?(A)+1:(A)) & ~1)
75 :    
76 :    
77 : chl 96 int32_t PMVfastSearch16(
78 :     const uint8_t * const pRef,
79 :     const uint8_t * const pRefH,
80 :     const uint8_t * const pRefV,
81 :     const uint8_t * const pRefHV,
82 :     const IMAGE * const pCur,
83 :     const int x, const int y,
84 :     const uint32_t MotionFlags,
85 : suxen_drol 136 const uint32_t iQuant,
86 :     const uint32_t iFcode,
87 : chl 96 const MBParam * const pParam,
88 : suxen_drol 136 const MACROBLOCK * const pMBs,
89 :     const MACROBLOCK * const prevMBs,
90 : chl 96 VECTOR * const currMV,
91 :     VECTOR * const currPMV);
92 : Isibaar 3
93 : chl 96 int32_t EPZSSearch16(
94 :     const uint8_t * const pRef,
95 :     const uint8_t * const pRefH,
96 :     const uint8_t * const pRefV,
97 :     const uint8_t * const pRefHV,
98 :     const IMAGE * const pCur,
99 :     const int x, const int y,
100 :     const uint32_t MotionFlags,
101 : suxen_drol 136 const uint32_t iQuant,
102 :     const uint32_t iFcode,
103 : chl 96 const MBParam * const pParam,
104 : suxen_drol 136 const MACROBLOCK * const pMBs,
105 :     const MACROBLOCK * const prevMBs,
106 : chl 96 VECTOR * const currMV,
107 :     VECTOR * const currPMV);
108 :    
109 :    
110 : Isibaar 3 int32_t PMVfastSearch8(
111 : chl 96 const uint8_t * const pRef,
112 :     const uint8_t * const pRefH,
113 :     const uint8_t * const pRefV,
114 :     const uint8_t * const pRefHV,
115 :     const IMAGE * const pCur,
116 :     const int x, const int y,
117 : suxen_drol 136 const int start_x, const int start_y,
118 : chl 96 const uint32_t MotionFlags,
119 : suxen_drol 136 const uint32_t iQuant,
120 :     const uint32_t iFcode,
121 : chl 96 const MBParam * const pParam,
122 : suxen_drol 136 const MACROBLOCK * const pMBs,
123 :     const MACROBLOCK * const prevMBs,
124 : chl 96 VECTOR * const currMV,
125 :     VECTOR * const currPMV);
126 :    
127 :     int32_t EPZSSearch8(
128 :     const uint8_t * const pRef,
129 :     const uint8_t * const pRefH,
130 :     const uint8_t * const pRefV,
131 :     const uint8_t * const pRefHV,
132 :     const IMAGE * const pCur,
133 :     const int x, const int y,
134 : suxen_drol 136 const int start_x, const int start_y,
135 : chl 96 const uint32_t MotionFlags,
136 : suxen_drol 136 const uint32_t iQuant,
137 :     const uint32_t iFcode,
138 : chl 96 const MBParam * const pParam,
139 : suxen_drol 136 const MACROBLOCK * const pMBs,
140 :     const MACROBLOCK * const prevMBs,
141 : chl 96 VECTOR * const currMV,
142 :     VECTOR * const currPMV);
143 :    
144 :    
145 :     typedef int32_t (MainSearch16Func)(
146 : edgomez 78 const uint8_t * const pRef,
147 :     const uint8_t * const pRefH,
148 :     const uint8_t * const pRefV,
149 :     const uint8_t * const pRefHV,
150 : chl 96 const uint8_t * const cur,
151 : edgomez 78 const int x, const int y,
152 : chl 96 int32_t startx, int32_t starty,
153 :     int32_t iMinSAD,
154 : edgomez 78 VECTOR * const currMV,
155 : chl 96 const VECTOR * const pmv,
156 :     const int32_t min_dx, const int32_t max_dx,
157 :     const int32_t min_dy, const int32_t max_dy,
158 :     const int32_t iEdgedWidth,
159 :     const int32_t iDiamondSize,
160 :     const int32_t iFcode,
161 :     const int32_t iQuant,
162 :     int iFound);
163 : Isibaar 3
164 : chl 96 typedef MainSearch16Func* MainSearch16FuncPtr;
165 :    
166 :    
167 :     typedef int32_t (MainSearch8Func)(
168 : edgomez 78 const uint8_t * const pRef,
169 :     const uint8_t * const pRefH,
170 :     const uint8_t * const pRefV,
171 :     const uint8_t * const pRefHV,
172 : chl 96 const uint8_t * const cur,
173 : edgomez 78 const int x, const int y,
174 : chl 96 int32_t startx, int32_t starty,
175 :     int32_t iMinSAD,
176 : edgomez 78 VECTOR * const currMV,
177 : chl 96 const VECTOR * const pmv,
178 :     const int32_t min_dx, const int32_t max_dx,
179 :     const int32_t min_dy, const int32_t max_dy,
180 :     const int32_t iEdgedWidth,
181 :     const int32_t iDiamondSize,
182 :     const int32_t iFcode,
183 :     const int32_t iQuant,
184 :     int iFound);
185 : Isibaar 3
186 : chl 96 typedef MainSearch8Func* MainSearch8FuncPtr;
187 : Isibaar 3
188 : chl 141 static int32_t lambda_vec16[32] = /* rounded values for lambda param for weight of motion bits as in modified H.26L */
189 :     { 0 ,(int)(1.00235+0.5), (int)(1.15582+0.5), (int)(1.31976+0.5), (int)(1.49591+0.5), (int)(1.68601+0.5),
190 :     (int)(1.89187+0.5), (int)(2.11542+0.5), (int)(2.35878+0.5), (int)(2.62429+0.5), (int)(2.91455+0.5),
191 :     (int)(3.23253+0.5), (int)(3.58158+0.5), (int)(3.96555+0.5), (int)(4.38887+0.5), (int)(4.85673+0.5),
192 :     (int)(5.37519+0.5), (int)(5.95144+0.5), (int)(6.59408+0.5), (int)(7.31349+0.5), (int)(8.12242+0.5),
193 :     (int)(9.03669+0.5), (int)(10.0763+0.5), (int)(11.2669+0.5), (int)(12.6426+0.5), (int)(14.2493+0.5),
194 :     (int)(16.1512+0.5), (int)(18.442+0.5), (int)(21.2656+0.5), (int)(24.8580+0.5), (int)(29.6436+0.5),
195 :     (int)(36.4949+0.5) };
196 :    
197 :     static int32_t *lambda_vec8 = lambda_vec16; /* same table for INTER and INTER4V for now*/
198 :    
199 :    
200 :    
201 : Isibaar 3 // mv.length table
202 :     static const uint32_t mvtab[33] = {
203 :     1, 2, 3, 4, 6, 7, 7, 7,
204 :     9, 9, 9, 10, 10, 10, 10, 10,
205 :     10, 10, 10, 10, 10, 10, 10, 10,
206 :     10, 11, 11, 11, 11, 11, 11, 12, 12
207 :     };
208 :    
209 :    
210 :     static __inline uint32_t mv_bits(int32_t component, const uint32_t iFcode)
211 :     {
212 :     if (component == 0)
213 :     return 1;
214 :    
215 :     if (component < 0)
216 :     component = -component;
217 :    
218 :     if (iFcode == 1)
219 :     {
220 :     if (component > 32)
221 :     component = 32;
222 :    
223 :     return mvtab[component] + 1;
224 :     }
225 :    
226 :     component += (1 << (iFcode - 1)) - 1;
227 :     component >>= (iFcode - 1);
228 :    
229 :     if (component > 32)
230 :     component = 32;
231 :    
232 :     return mvtab[component] + 1 + iFcode - 1;
233 :     }
234 :    
235 :    
236 : chl 141 static __inline uint32_t calc_delta_16(const int32_t dx, const int32_t dy, const uint32_t iFcode, const uint32_t iQuant)
237 : Isibaar 3 {
238 : chl 141 return NEIGH_TEND_16X16 * lambda_vec16[iQuant] * (mv_bits(dx, iFcode) + mv_bits(dy, iFcode));
239 : Isibaar 3 }
240 :    
241 : chl 141 static __inline uint32_t calc_delta_8(const int32_t dx, const int32_t dy, const uint32_t iFcode, const uint32_t iQuant)
242 : Isibaar 3
243 :     {
244 : chl 141 return NEIGH_TEND_8X8 * lambda_vec8[iQuant] * (mv_bits(dx, iFcode) + mv_bits(dy, iFcode));
245 : Isibaar 3 }
246 :    
247 :    
248 :    
249 :    
250 :    
251 :     #ifndef SEARCH16
252 :     #define SEARCH16 PMVfastSearch16
253 : chl 96 //#define SEARCH16 FullSearch16
254 :     //#define SEARCH16 EPZSSearch16
255 : Isibaar 3 #endif
256 :    
257 :     #ifndef SEARCH8
258 :     #define SEARCH8 PMVfastSearch8
259 : chl 96 //#define SEARCH8 EPZSSearch8
260 : Isibaar 3 #endif
261 :    
262 :     bool MotionEstimation(
263 : edgomez 78 MBParam * const pParam,
264 : suxen_drol 136 FRAMEINFO * const current,
265 :     FRAMEINFO * const reference,
266 : edgomez 78 const IMAGE * const pRefH,
267 :     const IMAGE * const pRefV,
268 :     const IMAGE * const pRefHV,
269 :     const uint32_t iLimit)
270 : Isibaar 3
271 :     {
272 : edgomez 78 const uint32_t iWcount = pParam->mb_width;
273 :     const uint32_t iHcount = pParam->mb_height;
274 : suxen_drol 136 MACROBLOCK * pMBs = current->mbs;
275 :     IMAGE * pCurrent = &current->image;
276 :    
277 :     MACROBLOCK * prevMBs = reference->mbs; // previous frame
278 :     IMAGE * pRef = &reference->image;
279 :    
280 : Isibaar 3
281 :     uint32_t i, j, iIntra = 0;
282 :    
283 : edgomez 78 VECTOR mv16;
284 :     VECTOR pmv16;
285 : Isibaar 3
286 : edgomez 78 int32_t sad8 = 0;
287 :     int32_t sad16;
288 :     int32_t deviation;
289 : Isibaar 3
290 : canard 117 if (sadInit)
291 : canard 115 (*sadInit)();
292 : suxen_drol 136
293 :    
294 :     /* eventhough we have a seperate prevMBs,
295 :     pmvfast/epsz does something "funny" with the previous frames data */
296 :    
297 : chl 140 /* for (i = 0; i < iHcount; i++)
298 : suxen_drol 136 for (j = 0; j < iWcount; j++)
299 :     {
300 :     pMBs[j + i * iWcount].mvs[0] = prevMBs[j + i * iWcount].mvs[0];
301 :     pMBs[j + i * iWcount].mvs[1] = prevMBs[j + i * iWcount].mvs[1];
302 :     pMBs[j + i * iWcount].mvs[2] = prevMBs[j + i * iWcount].mvs[2];
303 :     pMBs[j + i * iWcount].mvs[3] = prevMBs[j + i * iWcount].mvs[3];
304 :     }
305 : chl 140 */
306 : suxen_drol 136 /*dprintf("*** BEFORE ***");
307 :     for (i = 0; i < iHcount; i++)
308 :     for (j = 0; j < iWcount; j++)
309 :     {
310 :     dprintf(" [%i,%i] mode=%i dquant=%i mvs=(%i %i %i %i) sad8=(%i %i %i %i) sad16=(%i)", j,i,
311 :     pMBs[j + i * iWcount].mode,
312 :     pMBs[j + i * iWcount].dquant,
313 :     pMBs[j + i * iWcount].mvs[0],
314 :     pMBs[j + i * iWcount].mvs[1],
315 :     pMBs[j + i * iWcount].mvs[2],
316 :     pMBs[j + i * iWcount].mvs[3],
317 :     prevMBs[j + i * iWcount].sad8[0],
318 :     prevMBs[j + i * iWcount].sad8[1],
319 :     prevMBs[j + i * iWcount].sad8[2],
320 :     prevMBs[j + i * iWcount].sad8[3],
321 :     prevMBs[j + i * iWcount].sad16);
322 :     }
323 :     */
324 : canard 115
325 : Isibaar 3 // note: i==horizontal, j==vertical
326 : edgomez 78 for (i = 0; i < iHcount; i++)
327 : Isibaar 3 for (j = 0; j < iWcount; j++)
328 :     {
329 :     MACROBLOCK *pMB = &pMBs[j + i * iWcount];
330 : suxen_drol 136 MACROBLOCK *prevMB = &prevMBs[j + i * iWcount];
331 : Isibaar 3
332 :     sad16 = SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent,
333 : suxen_drol 136 j, i, current->motion_flags, current->quant, current->fcode,
334 :     pParam, pMBs, prevMBs, &mv16, &pmv16);
335 : Isibaar 3 pMB->sad16=sad16;
336 :    
337 :    
338 : edgomez 78 /* decide: MODE_INTER or MODE_INTRA
339 :     if (dev_intra < sad_inter - 2 * nb) use_intra
340 :     */
341 : Isibaar 3
342 : edgomez 78 deviation = dev16(pCurrent->y + j*16 + i*16*pParam->edged_width, pParam->edged_width);
343 : Isibaar 3
344 : edgomez 78 if (deviation < (sad16 - INTER_BIAS))
345 :     {
346 :     pMB->mode = MODE_INTRA;
347 :     pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
348 :     pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
349 : Isibaar 3
350 : suxen_drol 136 pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
351 :    
352 : edgomez 78 iIntra++;
353 :     if(iIntra >= iLimit)
354 :     return 1;
355 : Isibaar 3
356 : edgomez 78 continue;
357 :     }
358 : Isibaar 3
359 : suxen_drol 136 if (current->global_flags & XVID_INTER4V)
360 : edgomez 78 {
361 :     pMB->sad8[0] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent,
362 : suxen_drol 136 2 * j, 2 * i, mv16.x, mv16.y,
363 :     current->motion_flags, current->quant, current->fcode,
364 :     pParam, pMBs, prevMBs, &pMB->mvs[0], &pMB->pmvs[0]);
365 : Isibaar 3
366 : edgomez 78 pMB->sad8[1] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent,
367 : suxen_drol 136 2 * j + 1, 2 * i, mv16.x, mv16.y,
368 :     current->motion_flags, current->quant, current->fcode,
369 :     pParam, pMBs, prevMBs, &pMB->mvs[1], &pMB->pmvs[1]);
370 : Isibaar 3
371 : edgomez 78 pMB->sad8[2] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent,
372 : suxen_drol 136 2 * j, 2 * i + 1, mv16.x, mv16.y,
373 :     current->motion_flags, current->quant, current->fcode,
374 :     pParam, pMBs, prevMBs, &pMB->mvs[2], &pMB->pmvs[2]);
375 : Isibaar 3
376 : edgomez 78 pMB->sad8[3] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent,
377 : suxen_drol 136 2 * j + 1, 2 * i + 1, mv16.x, mv16.y,
378 :     current->motion_flags, current->quant, current->fcode,
379 :     pParam, pMBs, prevMBs, &pMB->mvs[3], &pMB->pmvs[3]);
380 : Isibaar 3
381 : edgomez 78 sad8 = pMB->sad8[0] + pMB->sad8[1] + pMB->sad8[2] + pMB->sad8[3];
382 :     }
383 : Isibaar 3
384 :    
385 : edgomez 78 /* decide: MODE_INTER or MODE_INTER4V
386 :     mpeg4: if (sad8 < sad16 - nb/2+1) use_inter4v
387 :     */
388 : Isibaar 3
389 : suxen_drol 136 if (!(current->global_flags & XVID_LUMIMASKING) || pMB->dquant == NO_CHANGE)
390 :     {
391 :     if (((current->global_flags & XVID_INTER4V)==0) ||
392 :     (sad16 < (sad8 + (int32_t)(IMV16X16 * current->quant))))
393 :     {
394 : Isibaar 3
395 : edgomez 78 sad8 = sad16;
396 :     pMB->mode = MODE_INTER;
397 :     pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = mv16.x;
398 :     pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = mv16.y;
399 :     pMB->pmvs[0].x = pmv16.x;
400 :     pMB->pmvs[0].y = pmv16.y;
401 :     }
402 :     else
403 :     pMB->mode = MODE_INTER4V;
404 :     }
405 :     else
406 :     {
407 : Isibaar 3 sad8 = sad16;
408 :     pMB->mode = MODE_INTER;
409 :     pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = mv16.x;
410 :     pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = mv16.y;
411 :     pMB->pmvs[0].x = pmv16.x;
412 :     pMB->pmvs[0].y = pmv16.y;
413 :     }
414 :     }
415 :    
416 : suxen_drol 136 /* dprintf("*** AFTER ***", pMBs[0].b_mvs[0].x);
417 :     for (i = 0; i < iHcount; i++)
418 :     for (j = 0; j < iWcount; j++)
419 :     {
420 :     dprintf(" [%i,%i] mode=%i dquant=%i mvs=(%i %i %i %i) sad8=(%i %i %i %i) sad16=(%i)", j,i,
421 :     pMBs[j + i * iWcount].mode,
422 :     pMBs[j + i * iWcount].dquant,
423 :     pMBs[j + i * iWcount].mvs[0],
424 :     pMBs[j + i * iWcount].mvs[1],
425 :     pMBs[j + i * iWcount].mvs[2],
426 :     pMBs[j + i * iWcount].mvs[3],
427 :     pMBs[j + i * iWcount].sad8[0],
428 :     pMBs[j + i * iWcount].sad8[1],
429 :     pMBs[j + i * iWcount].sad8[2],
430 :     pMBs[j + i * iWcount].sad8[3],
431 :     pMBs[j + i * iWcount].sad16);
432 :     }
433 :     */
434 :    
435 : Isibaar 3 return 0;
436 :     }
437 :    
438 :     #define MVzero(A) ( ((A).x)==(0) && ((A).y)==(0) )
439 :    
440 :     #define MVequal(A,B) ( ((A).x)==((B).x) && ((A).y)==((B).y) )
441 :    
442 :    
443 :     #define CHECK_MV16_ZERO {\
444 :     if ( (0 <= max_dx) && (0 >= min_dx) \
445 :     && (0 <= max_dy) && (0 >= min_dy) ) \
446 :     { \
447 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0, 0 , iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); \
448 : chl 141 iSAD += calc_delta_16(-pmv[0].x, -pmv[0].y, (uint8_t)iFcode, iQuant);\
449 : Isibaar 3 if (iSAD < iMinSAD) \
450 :     { iMinSAD=iSAD; currMV->x=0; currMV->y=0; } } \
451 :     }
452 :    
453 : chl 96 #define NOCHECK_MV16_CANDIDATE(X,Y) { \
454 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
455 : chl 141 iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\
456 : chl 96 if (iSAD < iMinSAD) \
457 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \
458 :     }
459 : Isibaar 3
460 :     #define CHECK_MV16_CANDIDATE(X,Y) { \
461 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
462 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
463 :     { \
464 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
465 : chl 141 iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\
466 : Isibaar 3 if (iSAD < iMinSAD) \
467 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \
468 :     }
469 :    
470 :     #define CHECK_MV16_CANDIDATE_DIR(X,Y,D) { \
471 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
472 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
473 :     { \
474 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
475 : chl 141 iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\
476 : Isibaar 3 if (iSAD < iMinSAD) \
477 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \
478 :     }
479 :    
480 :     #define CHECK_MV16_CANDIDATE_FOUND(X,Y,D) { \
481 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
482 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
483 :     { \
484 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
485 : chl 141 iSAD += calc_delta_16((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); iDirection=(D); iFound=0; } } \
488 :     }
489 :    
490 :    
491 :     #define CHECK_MV8_ZERO {\
492 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, 0, 0 , iEdgedWidth), iEdgedWidth); \
493 : chl 141 iSAD += calc_delta_8(-pmv[0].x, -pmv[0].y, (uint8_t)iFcode, iQuant);\
494 : Isibaar 3 if (iSAD < iMinSAD) \
495 :     { iMinSAD=iSAD; currMV->x=0; currMV->y=0; } \
496 :     }
497 :    
498 : chl 96 #define NOCHECK_MV8_CANDIDATE(X,Y) \
499 :     { \
500 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
501 : chl 141 iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\
502 : chl 96 if (iSAD < iMinSAD) \
503 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \
504 :     }
505 : Isibaar 3
506 :     #define CHECK_MV8_CANDIDATE(X,Y) { \
507 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
508 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
509 :     { \
510 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
511 : chl 141 iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\
512 : Isibaar 3 if (iSAD < iMinSAD) \
513 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \
514 :     }
515 :    
516 :     #define CHECK_MV8_CANDIDATE_DIR(X,Y,D) { \
517 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
518 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
519 :     { \
520 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
521 : chl 141 iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\
522 : Isibaar 3 if (iSAD < iMinSAD) \
523 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \
524 :     }
525 :    
526 :     #define CHECK_MV8_CANDIDATE_FOUND(X,Y,D) { \
527 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
528 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
529 :     { \
530 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
531 : chl 141 iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\
532 : Isibaar 3 if (iSAD < iMinSAD) \
533 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \
534 :     }
535 :    
536 :     /* too slow and not fully functional at the moment */
537 :     /*
538 :     int32_t ZeroSearch16(
539 :     const uint8_t * const pRef,
540 :     const uint8_t * const pRefH,
541 :     const uint8_t * const pRefV,
542 :     const uint8_t * const pRefHV,
543 :     const IMAGE * const pCur,
544 :     const int x, const int y,
545 :     const uint32_t MotionFlags,
546 : suxen_drol 136 const uint32_t iQuant,
547 :     const uint32_t iFcode,
548 : Isibaar 3 MBParam * const pParam,
549 : suxen_drol 136 const MACROBLOCK * const pMBs,
550 :     const MACROBLOCK * const prevMBs,
551 : Isibaar 3 VECTOR * const currMV,
552 :     VECTOR * const currPMV)
553 :     {
554 :     const int32_t iEdgedWidth = pParam->edged_width;
555 :     const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth;
556 :     int32_t iSAD;
557 :     int32_t pred_x,pred_y;
558 :    
559 :     get_pmv(pMBs, x, y, pParam->mb_width, 0, &pred_x, &pred_y);
560 :    
561 :     iSAD = sad16( cur,
562 :     get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0,0, iEdgedWidth),
563 :     iEdgedWidth, MV_MAX_ERROR);
564 :     if (iSAD <= iQuant * 96)
565 :     iSAD -= MV16_00_BIAS;
566 :    
567 :     currMV->x = 0;
568 :     currMV->y = 0;
569 :     currPMV->x = -pred_x;
570 :     currPMV->y = -pred_y;
571 :    
572 :     return iSAD;
573 :    
574 :     }
575 :     */
576 :    
577 : chl 96 int32_t Diamond16_MainSearch(
578 : edgomez 78 const uint8_t * const pRef,
579 :     const uint8_t * const pRefH,
580 :     const uint8_t * const pRefV,
581 :     const uint8_t * const pRefHV,
582 :     const uint8_t * const cur,
583 :     const int x, const int y,
584 :     int32_t startx, int32_t starty,
585 :     int32_t iMinSAD,
586 :     VECTOR * const currMV,
587 :     const VECTOR * const pmv,
588 :     const int32_t min_dx, const int32_t max_dx,
589 :     const int32_t min_dy, const int32_t max_dy,
590 :     const int32_t iEdgedWidth,
591 :     const int32_t iDiamondSize,
592 :     const int32_t iFcode,
593 :     const int32_t iQuant,
594 :     int iFound)
595 : Isibaar 3 {
596 :     /* Do a diamond search around given starting point, return SAD of best */
597 :    
598 :     int32_t iDirection=0;
599 :     int32_t iSAD;
600 :     VECTOR backupMV;
601 :     backupMV.x = startx;
602 :     backupMV.y = starty;
603 :    
604 :     /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
605 :    
606 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y,1);
607 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2);
608 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3);
609 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4);
610 :    
611 :     if (iDirection)
612 :     while (!iFound)
613 :     {
614 :     iFound = 1;
615 :     backupMV=*currMV;
616 :    
617 :     if ( iDirection != 2)
618 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x-iDiamondSize,backupMV.y,1);
619 :     if ( iDirection != 1)
620 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x+iDiamondSize,backupMV.y,2);
621 :     if ( iDirection != 4)
622 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x,backupMV.y-iDiamondSize,3);
623 :     if ( iDirection != 3)
624 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x,backupMV.y+iDiamondSize,4);
625 :     }
626 :     else
627 : edgomez 78 {
628 :     currMV->x = startx;
629 :     currMV->y = starty;
630 :     }
631 : Isibaar 3 return iMinSAD;
632 :     }
633 :    
634 : chl 96 int32_t Square16_MainSearch(
635 :     const uint8_t * const pRef,
636 :     const uint8_t * const pRefH,
637 :     const uint8_t * const pRefV,
638 :     const uint8_t * const pRefHV,
639 :     const uint8_t * const cur,
640 :     const int x, const int y,
641 :     int32_t startx, int32_t starty,
642 :     int32_t iMinSAD,
643 :     VECTOR * const currMV,
644 :     const VECTOR * const pmv,
645 :     const int32_t min_dx, const int32_t max_dx,
646 :     const int32_t min_dy, const int32_t max_dy,
647 :     const int32_t iEdgedWidth,
648 :     const int32_t iDiamondSize,
649 :     const int32_t iFcode,
650 :     const int32_t iQuant,
651 :     int iFound)
652 :     {
653 :     /* Do a square search around given starting point, return SAD of best */
654 :    
655 :     int32_t iDirection=0;
656 :     int32_t iSAD;
657 :     VECTOR backupMV;
658 :     backupMV.x = startx;
659 :     backupMV.y = starty;
660 :    
661 :     /* It's one search with full square pattern, and new parts for all following diamonds */
662 :    
663 :     /* new direction are extra, so 1-4 is normal diamond
664 :     537
665 :     1*2
666 :     648
667 :     */
668 :    
669 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y,1);
670 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2);
671 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3);
672 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4);
673 :    
674 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5);
675 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6);
676 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7);
677 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8);
678 :    
679 :    
680 :     if (iDirection)
681 :     while (!iFound)
682 :     {
683 :     iFound = 1;
684 :     backupMV=*currMV;
685 :    
686 :     switch (iDirection)
687 :     {
688 :     case 1:
689 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x-iDiamondSize,backupMV.y,1);
690 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5);
691 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7);
692 :     break;
693 :     case 2:
694 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2);
695 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6);
696 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8);
697 :     break;
698 :    
699 :     case 3:
700 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4);
701 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7);
702 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8);
703 :     break;
704 :    
705 :     case 4:
706 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3);
707 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5);
708 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6);
709 :     break;
710 :    
711 :     case 5:
712 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y,1);
713 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3);
714 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5);
715 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6);
716 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7);
717 :     break;
718 :    
719 :     case 6:
720 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2);
721 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3);
722 :    
723 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5);
724 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6);
725 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8);
726 :    
727 :     break;
728 :    
729 :     case 7:
730 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x-iDiamondSize,backupMV.y,1);
731 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4);
732 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5);
733 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7);
734 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8);
735 :     break;
736 :    
737 :     case 8:
738 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2);
739 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4);
740 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6);
741 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7);
742 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8);
743 :     break;
744 :     default:
745 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y,1);
746 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2);
747 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3);
748 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4);
749 :    
750 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5);
751 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6);
752 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7);
753 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8);
754 :     break;
755 :     }
756 :     }
757 :     else
758 :     {
759 :     currMV->x = startx;
760 :     currMV->y = starty;
761 :     }
762 :     return iMinSAD;
763 :     }
764 :    
765 :    
766 :     int32_t Full16_MainSearch(
767 :     const uint8_t * const pRef,
768 :     const uint8_t * const pRefH,
769 :     const uint8_t * const pRefV,
770 :     const uint8_t * const pRefHV,
771 :     const uint8_t * const cur,
772 :     const int x, const int y,
773 :     int32_t startx, int32_t starty,
774 :     int32_t iMinSAD,
775 :     VECTOR * const currMV,
776 :     const VECTOR * const pmv,
777 :     const int32_t min_dx, const int32_t max_dx,
778 :     const int32_t min_dy, const int32_t max_dy,
779 :     const int32_t iEdgedWidth,
780 :     const int32_t iDiamondSize,
781 :     const int32_t iFcode,
782 :     const int32_t iQuant,
783 :     int iFound)
784 :     {
785 :     int32_t iSAD;
786 :     int32_t dx,dy;
787 :     VECTOR backupMV;
788 :     backupMV.x = startx;
789 :     backupMV.y = starty;
790 :    
791 :     for (dx = min_dx; dx<=max_dx; dx+=iDiamondSize)
792 :     for (dy = min_dy; dy<= max_dy; dy+=iDiamondSize)
793 :     NOCHECK_MV16_CANDIDATE(dx,dy);
794 :    
795 :     return iMinSAD;
796 :     }
797 :    
798 :     int32_t Full8_MainSearch(
799 :     const uint8_t * const pRef,
800 :     const uint8_t * const pRefH,
801 :     const uint8_t * const pRefV,
802 :     const uint8_t * const pRefHV,
803 :     const uint8_t * const cur,
804 :     const int x, const int y,
805 :     int32_t startx, int32_t starty,
806 :     int32_t iMinSAD,
807 :     VECTOR * const currMV,
808 :     const VECTOR * const pmv,
809 :     const int32_t min_dx, const int32_t max_dx,
810 :     const int32_t min_dy, const int32_t max_dy,
811 :     const int32_t iEdgedWidth,
812 :     const int32_t iDiamondSize,
813 :     const int32_t iFcode,
814 :     const int32_t iQuant,
815 :     int iFound)
816 :     {
817 :     int32_t iSAD;
818 :     int32_t dx,dy;
819 :     VECTOR backupMV;
820 :     backupMV.x = startx;
821 :     backupMV.y = starty;
822 :    
823 :     for (dx = min_dx; dx<=max_dx; dx+=iDiamondSize)
824 :     for (dy = min_dy; dy<= max_dy; dy+=iDiamondSize)
825 :     NOCHECK_MV8_CANDIDATE(dx,dy);
826 :    
827 :     return iMinSAD;
828 :     }
829 :    
830 :    
831 :    
832 :     int32_t Halfpel16_Refine(
833 : edgomez 78 const uint8_t * const pRef,
834 :     const uint8_t * const pRefH,
835 :     const uint8_t * const pRefV,
836 :     const uint8_t * const pRefHV,
837 :     const uint8_t * const cur,
838 :     const int x, const int y,
839 :     VECTOR * const currMV,
840 :     int32_t iMinSAD,
841 :     const VECTOR * const pmv,
842 :     const int32_t min_dx, const int32_t max_dx,
843 :     const int32_t min_dy, const int32_t max_dy,
844 :     const int32_t iFcode,
845 :     const int32_t iQuant,
846 :     const int32_t iEdgedWidth)
847 : Isibaar 3 {
848 :     /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */
849 :    
850 :     int32_t iSAD;
851 :     VECTOR backupMV = *currMV;
852 :    
853 :     CHECK_MV16_CANDIDATE(backupMV.x-1,backupMV.y-1);
854 :     CHECK_MV16_CANDIDATE(backupMV.x ,backupMV.y-1);
855 :     CHECK_MV16_CANDIDATE(backupMV.x+1,backupMV.y-1);
856 :     CHECK_MV16_CANDIDATE(backupMV.x-1,backupMV.y);
857 :     CHECK_MV16_CANDIDATE(backupMV.x+1,backupMV.y);
858 :     CHECK_MV16_CANDIDATE(backupMV.x-1,backupMV.y+1);
859 :     CHECK_MV16_CANDIDATE(backupMV.x ,backupMV.y+1);
860 :     CHECK_MV16_CANDIDATE(backupMV.x+1,backupMV.y+1);
861 :    
862 :     return iMinSAD;
863 :     }
864 :    
865 :     #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16)
866 :    
867 : chl 96
868 : Isibaar 3 int32_t PMVfastSearch16(
869 : chl 96 const uint8_t * const pRef,
870 :     const uint8_t * const pRefH,
871 :     const uint8_t * const pRefV,
872 :     const uint8_t * const pRefHV,
873 :     const IMAGE * const pCur,
874 :     const int x, const int y,
875 :     const uint32_t MotionFlags,
876 : suxen_drol 136 const uint32_t iQuant,
877 :     const uint32_t iFcode,
878 : chl 96 const MBParam * const pParam,
879 : suxen_drol 136 const MACROBLOCK * const pMBs,
880 :     const MACROBLOCK * const prevMBs,
881 : chl 96 VECTOR * const currMV,
882 :     VECTOR * const currPMV)
883 : Isibaar 3 {
884 : chenm001 132 const uint32_t iWcount = pParam->mb_width;
885 : Isibaar 3 const int32_t iWidth = pParam->width;
886 :     const int32_t iHeight = pParam->height;
887 :     const int32_t iEdgedWidth = pParam->edged_width;
888 :    
889 :     const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth;
890 :    
891 :     int32_t iDiamondSize;
892 :    
893 :     int32_t min_dx;
894 :     int32_t max_dx;
895 :     int32_t min_dy;
896 :     int32_t max_dy;
897 :    
898 :     int32_t iFound;
899 :    
900 :     VECTOR newMV;
901 :     VECTOR backupMV; /* just for PMVFAST */
902 :    
903 :     VECTOR pmv[4];
904 :     int32_t psad[4];
905 :    
906 : suxen_drol 136 const MACROBLOCK * const pMB = pMBs + x + y * iWcount;
907 :     const MACROBLOCK * const prevMB = prevMBs + x + y * iWcount;
908 : Isibaar 3
909 :     static int32_t threshA,threshB;
910 :     int32_t bPredEq;
911 :     int32_t iMinSAD,iSAD;
912 :    
913 :     /* Get maximum range */
914 :     get_range(&min_dx, &max_dx, &min_dy, &max_dy,
915 : edgomez 78 x, y, 16, iWidth, iHeight, iFcode);
916 : Isibaar 3
917 :     /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */
918 :    
919 :     if (!(MotionFlags & PMV_HALFPEL16 ))
920 :     { min_dx = EVEN(min_dx);
921 : edgomez 78 max_dx = EVEN(max_dx);
922 :     min_dy = EVEN(min_dy);
923 :     max_dy = EVEN(max_dy);
924 : Isibaar 3 } /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
925 :    
926 :    
927 :     bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad);
928 :    
929 :     if ((x==0) && (y==0) )
930 :     {
931 :     threshA = 512;
932 :     threshB = 1024;
933 :    
934 :     }
935 :     else
936 :     {
937 :     threshA = psad[0];
938 :     threshB = threshA+256;
939 :     if (threshA< 512) threshA = 512;
940 :     if (threshA>1024) threshA = 1024;
941 :     if (threshB>1792) threshB = 1792;
942 :     }
943 :    
944 :     iFound=0;
945 :    
946 :     /* Step 2: Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion
947 : edgomez 78 vector of the median.
948 :     If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2
949 : Isibaar 3 */
950 :    
951 : suxen_drol 136 if ((bPredEq) && (MVequal(pmv[0],prevMB->mvs[0]) ) )
952 : Isibaar 3 iFound=2;
953 :    
954 :     /* Step 3: If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search.
955 : edgomez 78 Otherwise select large Diamond Search.
956 : Isibaar 3 */
957 :    
958 :     if ( (pmv[0].x != 0) || (pmv[0].y != 0) || (threshB<1536) || (bPredEq) )
959 :     iDiamondSize=1; // halfpel!
960 :     else
961 :     iDiamondSize=2; // halfpel!
962 :    
963 :     if (!(MotionFlags & PMV_HALFPELDIAMOND16) )
964 :     iDiamondSize*=2;
965 :    
966 :     /* Step 4: Calculate SAD around the Median prediction.
967 : edgomez 78 MinSAD=SAD
968 :     If Motion Vector equal to Previous frame motion vector
969 :     and MinSAD<PrevFrmSAD goto Step 10.
970 :     If SAD<=256 goto Step 10.
971 : Isibaar 3 */
972 :    
973 :    
974 :     // Prepare for main loop
975 :    
976 :     *currMV=pmv[0]; /* current best := prediction */
977 :     if (!(MotionFlags & PMV_HALFPEL16 ))
978 :     { /* This should NOT be necessary! */
979 :     currMV->x = EVEN(currMV->x);
980 :     currMV->y = EVEN(currMV->y);
981 :     }
982 :    
983 :     if (currMV->x > max_dx)
984 : edgomez 78 {
985 :     currMV->x=max_dx;
986 :     }
987 : Isibaar 3 if (currMV->x < min_dx)
988 : edgomez 78 {
989 :     currMV->x=min_dx;
990 :     }
991 : Isibaar 3 if (currMV->y > max_dy)
992 : edgomez 78 {
993 :     currMV->y=max_dy;
994 :     }
995 : Isibaar 3 if (currMV->y < min_dy)
996 : edgomez 78 {
997 :     currMV->y=min_dy;
998 :     }
999 : Isibaar 3
1000 :     iMinSAD = sad16( cur,
1001 : edgomez 78 get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, iEdgedWidth),
1002 :     iEdgedWidth, MV_MAX_ERROR);
1003 : chl 141 iMinSAD += calc_delta_16(currMV->x-pmv[0].x, currMV->y-pmv[0].y, (uint8_t)iFcode, iQuant);
1004 : Isibaar 3
1005 : suxen_drol 136 if ( (iMinSAD < 256 ) || ( (MVequal(*currMV,prevMB->mvs[0])) && ((uint32_t)iMinSAD < prevMB->sad16) ) )
1006 : edgomez 78 {
1007 : Isibaar 3
1008 : edgomez 78 if (MotionFlags & PMV_QUICKSTOP16)
1009 : chl 96 goto PMVfast16_Terminate_without_Refine;
1010 :     if (MotionFlags & PMV_EARLYSTOP16)
1011 :     goto PMVfast16_Terminate_with_Refine;
1012 : edgomez 78 }
1013 : Isibaar 3
1014 :     /*
1015 : edgomez 78 Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block.
1016 :     Also calculate (0,0) but do not subtract offset.
1017 :     Let MinSAD be the smallest SAD up to this point.
1018 : chl 140 If MV is (0,0) subtract offset.
1019 : Isibaar 3 */
1020 :    
1021 :     // (0,0) is always possible
1022 :    
1023 :     CHECK_MV16_ZERO;
1024 :    
1025 :     // previous frame MV is always possible
1026 : suxen_drol 136 CHECK_MV16_CANDIDATE(prevMB->mvs[0].x,prevMB->mvs[0].y);
1027 : Isibaar 3
1028 :     // left neighbour, if allowed
1029 :     if (x != 0)
1030 :     {
1031 :     if (!(MotionFlags & PMV_HALFPEL16 ))
1032 :     { pmv[1].x = EVEN(pmv[1].x);
1033 : edgomez 78 pmv[1].y = EVEN(pmv[1].y);
1034 : Isibaar 3 }
1035 :     CHECK_MV16_CANDIDATE(pmv[1].x,pmv[1].y);
1036 :     }
1037 :    
1038 :     // top neighbour, if allowed
1039 :     if (y != 0)
1040 :     {
1041 :     if (!(MotionFlags & PMV_HALFPEL16 ))
1042 :     { pmv[2].x = EVEN(pmv[2].x);
1043 : edgomez 78 pmv[2].y = EVEN(pmv[2].y);
1044 : Isibaar 3 }
1045 :     CHECK_MV16_CANDIDATE(pmv[2].x,pmv[2].y);
1046 :    
1047 :     // top right neighbour, if allowed
1048 : chenm001 132 if ((uint32_t)x != (iWcount-1))
1049 : Isibaar 3 {
1050 :     if (!(MotionFlags & PMV_HALFPEL16 ))
1051 :     { pmv[3].x = EVEN(pmv[3].x);
1052 : edgomez 78 pmv[3].y = EVEN(pmv[3].y);
1053 : Isibaar 3 }
1054 :     CHECK_MV16_CANDIDATE(pmv[3].x,pmv[3].y);
1055 :     }
1056 :     }
1057 : chl 140
1058 : chl 141 if ( (MVzero(*currMV)) && (!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96)*/ )
1059 : chl 140 iMinSAD -= MV16_00_BIAS;
1060 : Isibaar 3
1061 : chl 140
1062 : Isibaar 3 /* Step 6: If MinSAD <= thresa goto Step 10.
1063 :     If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10.
1064 :     */
1065 :    
1066 : suxen_drol 136 if ( (iMinSAD <= threshA) || ( MVequal(*currMV,prevMB->mvs[0]) && ((uint32_t)iMinSAD < prevMB->sad16) ) )
1067 : edgomez 78 {
1068 :     if (MotionFlags & PMV_QUICKSTOP16)
1069 : chl 96 goto PMVfast16_Terminate_without_Refine;
1070 :     if (MotionFlags & PMV_EARLYSTOP16)
1071 :     goto PMVfast16_Terminate_with_Refine;
1072 : edgomez 78 }
1073 : Isibaar 3
1074 :    
1075 :     /************ (Diamond Search) **************/
1076 :     /*
1077 : edgomez 78 Step 7: Perform Diamond search, with either the small or large diamond.
1078 :     If Found=2 only examine one Diamond pattern, and afterwards goto step 10
1079 :     Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond.
1080 :     If center then goto step 10.
1081 :     Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center.
1082 :     Refine by using small diamond and goto step 10.
1083 : Isibaar 3 */
1084 :    
1085 :     backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
1086 :    
1087 :     /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
1088 : chl 96 iSAD = Diamond16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur,
1089 : edgomez 78 x, y,
1090 :     currMV->x, currMV->y, iMinSAD, &newMV,
1091 :     pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound);
1092 : Isibaar 3
1093 :     if (iSAD < iMinSAD)
1094 :     {
1095 :     *currMV = newMV;
1096 :     iMinSAD = iSAD;
1097 :     }
1098 :    
1099 :     if (MotionFlags & PMV_EXTSEARCH16)
1100 :     {
1101 :     /* extended: search (up to) two more times: orignal prediction and (0,0) */
1102 :    
1103 :     if (!(MVequal(pmv[0],backupMV)) )
1104 : chl 96 { iSAD = Diamond16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur,
1105 : edgomez 78 x, y,
1106 :     pmv[0].x, pmv[0].y, iMinSAD, &newMV,
1107 :     pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound);
1108 : Isibaar 3
1109 : edgomez 78 if (iSAD < iMinSAD)
1110 :     {
1111 :     *currMV = newMV;
1112 :     iMinSAD = iSAD;
1113 : Isibaar 3 }
1114 : edgomez 78 }
1115 : Isibaar 3
1116 :     if ( (!(MVzero(pmv[0]))) && (!(MVzero(backupMV))) )
1117 : chl 96 { iSAD = Diamond16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur,
1118 : edgomez 78 x, y,
1119 :     0, 0, iMinSAD, &newMV,
1120 :     pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound);
1121 : Isibaar 3
1122 : edgomez 78 if (iSAD < iMinSAD)
1123 :     {
1124 :     *currMV = newMV;
1125 :     iMinSAD = iSAD;
1126 : Isibaar 3 }
1127 : edgomez 78 }
1128 : Isibaar 3 }
1129 :    
1130 :     /*
1131 : edgomez 78 Step 10: The motion vector is chosen according to the block corresponding to MinSAD.
1132 : Isibaar 3 */
1133 :    
1134 : chl 96 PMVfast16_Terminate_with_Refine:
1135 : Isibaar 3 if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step
1136 : chl 96 iMinSAD = Halfpel16_Refine( pRef, pRefH, pRefV, pRefHV, cur,
1137 :     x, y,
1138 :     currMV, iMinSAD,
1139 :     pmv, min_dx, max_dx, min_dy, max_dy, iFcode, iQuant, iEdgedWidth);
1140 : Isibaar 3
1141 : chl 96 PMVfast16_Terminate_without_Refine:
1142 : Isibaar 3 currPMV->x = currMV->x - pmv[0].x;
1143 :     currPMV->y = currMV->y - pmv[0].y;
1144 :     return iMinSAD;
1145 :     }
1146 :    
1147 :    
1148 :    
1149 :    
1150 :    
1151 :    
1152 : chl 96 int32_t Diamond8_MainSearch(
1153 : edgomez 78 const uint8_t * const pRef,
1154 :     const uint8_t * const pRefH,
1155 :     const uint8_t * const pRefV,
1156 :     const uint8_t * const pRefHV,
1157 :     const uint8_t * const cur,
1158 :     const int x, const int y,
1159 :     int32_t startx, int32_t starty,
1160 :     int32_t iMinSAD,
1161 :     VECTOR * const currMV,
1162 :     const VECTOR * const pmv,
1163 :     const int32_t min_dx, const int32_t max_dx,
1164 :     const int32_t min_dy, const int32_t max_dy,
1165 :     const int32_t iEdgedWidth,
1166 :     const int32_t iDiamondSize,
1167 :     const int32_t iFcode,
1168 :     const int32_t iQuant,
1169 :     int iFound)
1170 : Isibaar 3 {
1171 :     /* Do a diamond search around given starting point, return SAD of best */
1172 :    
1173 :     int32_t iDirection=0;
1174 :     int32_t iSAD;
1175 :     VECTOR backupMV;
1176 :     backupMV.x = startx;
1177 :     backupMV.y = starty;
1178 :    
1179 :     /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
1180 :    
1181 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y,1);
1182 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2);
1183 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3);
1184 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4);
1185 :    
1186 :     if (iDirection)
1187 :     while (!iFound)
1188 :     {
1189 :     iFound = 1;
1190 :     backupMV=*currMV; // since iDirection!=0, this is well defined!
1191 :    
1192 :     if ( iDirection != 2)
1193 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x-iDiamondSize,backupMV.y,1);
1194 :     if ( iDirection != 1)
1195 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x+iDiamondSize,backupMV.y,2);
1196 :     if ( iDirection != 4)
1197 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x,backupMV.y-iDiamondSize,3);
1198 :     if ( iDirection != 3)
1199 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x,backupMV.y+iDiamondSize,4);
1200 :     }
1201 :     else
1202 : edgomez 78 {
1203 :     currMV->x = startx;
1204 :     currMV->y = starty;
1205 :     }
1206 : Isibaar 3 return iMinSAD;
1207 :     }
1208 :    
1209 : chl 96 int32_t Halfpel8_Refine(
1210 : edgomez 78 const uint8_t * const pRef,
1211 :     const uint8_t * const pRefH,
1212 :     const uint8_t * const pRefV,
1213 :     const uint8_t * const pRefHV,
1214 :     const uint8_t * const cur,
1215 :     const int x, const int y,
1216 :     VECTOR * const currMV,
1217 :     int32_t iMinSAD,
1218 :     const VECTOR * const pmv,
1219 :     const int32_t min_dx, const int32_t max_dx,
1220 :     const int32_t min_dy, const int32_t max_dy,
1221 :     const int32_t iFcode,
1222 :     const int32_t iQuant,
1223 :     const int32_t iEdgedWidth)
1224 : Isibaar 3 {
1225 :     /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */
1226 :    
1227 :     int32_t iSAD;
1228 :     VECTOR backupMV = *currMV;
1229 :    
1230 :     CHECK_MV8_CANDIDATE(backupMV.x-1,backupMV.y-1);
1231 :     CHECK_MV8_CANDIDATE(backupMV.x ,backupMV.y-1);
1232 :     CHECK_MV8_CANDIDATE(backupMV.x+1,backupMV.y-1);
1233 :     CHECK_MV8_CANDIDATE(backupMV.x-1,backupMV.y);
1234 :     CHECK_MV8_CANDIDATE(backupMV.x+1,backupMV.y);
1235 :     CHECK_MV8_CANDIDATE(backupMV.x-1,backupMV.y+1);
1236 :     CHECK_MV8_CANDIDATE(backupMV.x ,backupMV.y+1);
1237 :     CHECK_MV8_CANDIDATE(backupMV.x+1,backupMV.y+1);
1238 :    
1239 :     return iMinSAD;
1240 :     }
1241 :    
1242 :    
1243 :     #define PMV_HALFPEL8 (PMV_HALFPELDIAMOND8|PMV_HALFPELREFINE8)
1244 :    
1245 :     int32_t PMVfastSearch8(
1246 : chl 96 const uint8_t * const pRef,
1247 :     const uint8_t * const pRefH,
1248 :     const uint8_t * const pRefV,
1249 :     const uint8_t * const pRefHV,
1250 :     const IMAGE * const pCur,
1251 :     const int x, const int y,
1252 : suxen_drol 136 const int start_x, const int start_y,
1253 : chl 96 const uint32_t MotionFlags,
1254 : suxen_drol 136 const uint32_t iQuant,
1255 :     const uint32_t iFcode,
1256 : chl 96 const MBParam * const pParam,
1257 : suxen_drol 136 const MACROBLOCK * const pMBs,
1258 :     const MACROBLOCK * const prevMBs,
1259 : chl 96 VECTOR * const currMV,
1260 :     VECTOR * const currPMV)
1261 : Isibaar 3 {
1262 : suxen_drol 136 const uint32_t iWcount = pParam->mb_width;
1263 : Isibaar 3 const int32_t iWidth = pParam->width;
1264 :     const int32_t iHeight = pParam->height;
1265 :     const int32_t iEdgedWidth = pParam->edged_width;
1266 :    
1267 :     const uint8_t * cur = pCur->y + x*8 + y*8*iEdgedWidth;
1268 :    
1269 :     int32_t iDiamondSize;
1270 :    
1271 :     int32_t min_dx;
1272 :     int32_t max_dx;
1273 :     int32_t min_dy;
1274 :     int32_t max_dy;
1275 :    
1276 :     VECTOR pmv[4];
1277 :     int32_t psad[4];
1278 :     VECTOR newMV;
1279 :     VECTOR backupMV;
1280 :    
1281 : suxen_drol 136 const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount;
1282 :     const MACROBLOCK * const prevMB = prevMBs + (x>>1) + (y>>1) * iWcount;
1283 : Isibaar 3
1284 :     static int32_t threshA,threshB;
1285 :     int32_t iFound,bPredEq;
1286 :     int32_t iMinSAD,iSAD;
1287 :    
1288 :     int32_t iSubBlock = ((y&1)<<1) + (x&1);
1289 :    
1290 :     /* Get maximum range */
1291 : edgomez 78 get_range(&min_dx, &max_dx, &min_dy, &max_dy,
1292 :     x, y, 8, iWidth, iHeight, iFcode);
1293 : Isibaar 3
1294 :     if (!(MotionFlags & PMV_HALFPELDIAMOND8 ))
1295 :     { min_dx = EVEN(min_dx);
1296 : edgomez 78 max_dx = EVEN(max_dx);
1297 :     min_dy = EVEN(min_dy);
1298 :     max_dy = EVEN(max_dy);
1299 : Isibaar 3 } /* because we might use IF (dx>max_dx) THEN dx=max_dx; */
1300 :    
1301 :    
1302 :     bPredEq = get_pmvdata(pMBs, (x>>1), (y>>1), iWcount, iSubBlock, pmv, psad);
1303 :    
1304 :     if ((x==0) && (y==0) )
1305 :     {
1306 :     threshA = 512/4;
1307 :     threshB = 1024/4;
1308 :    
1309 :     }
1310 :     else
1311 :     {
1312 :     threshA = psad[0]/4; /* good estimate */
1313 :     threshB = threshA+256/4;
1314 :     if (threshA< 512/4) threshA = 512/4;
1315 :     if (threshA>1024/4) threshA = 1024/4;
1316 :     if (threshB>1792/4) threshB = 1792/4;
1317 :     }
1318 :    
1319 :     iFound=0;
1320 :    
1321 :     /* Step 2: Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion
1322 : edgomez 78 vector of the median.
1323 :     If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2
1324 : Isibaar 3 */
1325 :    
1326 : chl 140 if ((bPredEq) && (MVequal(pmv[0],prevMB->mvs[iSubBlock]) ) )
1327 : Isibaar 3 iFound=2;
1328 :    
1329 :     /* Step 3: If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search.
1330 : edgomez 78 Otherwise select large Diamond Search.
1331 : Isibaar 3 */
1332 :    
1333 :     if ( (pmv[0].x != 0) || (pmv[0].y != 0) || (threshB<1536/4) || (bPredEq) )
1334 :     iDiamondSize=1; // 1 halfpel!
1335 :     else
1336 :     iDiamondSize=2; // 2 halfpel = 1 full pixel!
1337 :    
1338 :     if (!(MotionFlags & PMV_HALFPELDIAMOND8) )
1339 :     iDiamondSize*=2;
1340 :    
1341 :     /* Step 4: Calculate SAD around the Median prediction.
1342 : edgomez 78 MinSAD=SAD
1343 :     If Motion Vector equal to Previous frame motion vector
1344 :     and MinSAD<PrevFrmSAD goto Step 10.
1345 :     If SAD<=256 goto Step 10.
1346 : Isibaar 3 */
1347 :    
1348 :    
1349 :     // Prepare for main loop
1350 :    
1351 :     currMV->x=start_x; /* start with mv16 */
1352 :     currMV->y=start_y;
1353 :    
1354 :     iMinSAD = sad8( cur,
1355 : edgomez 78 get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, iEdgedWidth),
1356 :     iEdgedWidth);
1357 : chl 141 iMinSAD += calc_delta_8(currMV->x - pmv[0].x, currMV->y - pmv[0].y, (uint8_t)iFcode, iQuant);
1358 : Isibaar 3
1359 : chl 140 if ( (iMinSAD < 256/4 ) || ( (MVequal(*currMV,prevMB->mvs[iSubBlock])) && ((uint32_t)iMinSAD < prevMB->sad8[iSubBlock]) ) )
1360 : edgomez 78 {
1361 : chl 96 if (MotionFlags & PMV_QUICKSTOP16)
1362 :     goto PMVfast8_Terminate_without_Refine;
1363 :     if (MotionFlags & PMV_EARLYSTOP16)
1364 :     goto PMVfast8_Terminate_with_Refine;
1365 : edgomez 78 }
1366 : Isibaar 3
1367 : chl 96
1368 : Isibaar 3 /*
1369 : edgomez 78 Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block.
1370 :     Also calculate (0,0) but do not subtract offset.
1371 :     Let MinSAD be the smallest SAD up to this point.
1372 : chl 140 If MV is (0,0) subtract offset.
1373 : Isibaar 3 */
1374 :    
1375 :     // the prediction might be even better than mv16
1376 :     CHECK_MV8_CANDIDATE(pmv[0].x,pmv[0].y);
1377 :    
1378 :     // (0,0) is always possible
1379 :     CHECK_MV8_ZERO;
1380 :    
1381 :     // previous frame MV is always possible
1382 : chl 140 CHECK_MV8_CANDIDATE(prevMB->mvs[iSubBlock].x,prevMB->mvs[iSubBlock].y);
1383 : Isibaar 3
1384 :     // left neighbour, if allowed
1385 :     if (psad[1] != MV_MAX_ERROR)
1386 :     {
1387 :     if (!(MotionFlags & PMV_HALFPEL8 ))
1388 :     { pmv[1].x = EVEN(pmv[1].x);
1389 : edgomez 78 pmv[1].y = EVEN(pmv[1].y);
1390 : Isibaar 3 }
1391 :     CHECK_MV8_CANDIDATE(pmv[1].x,pmv[1].y);
1392 :     }
1393 :    
1394 :     // top neighbour, if allowed
1395 :     if (psad[2] != MV_MAX_ERROR)
1396 :     {
1397 :     if (!(MotionFlags & PMV_HALFPEL8 ))
1398 :     { pmv[2].x = EVEN(pmv[2].x);
1399 : edgomez 78 pmv[2].y = EVEN(pmv[2].y);
1400 : Isibaar 3 }
1401 :     CHECK_MV8_CANDIDATE(pmv[2].x,pmv[2].y);
1402 :    
1403 :     // top right neighbour, if allowed
1404 :     if (psad[3] != MV_MAX_ERROR)
1405 :     {
1406 : edgomez 78 if (!(MotionFlags & PMV_HALFPEL8 ))
1407 :     { pmv[3].x = EVEN(pmv[3].x);
1408 : Isibaar 3 pmv[3].y = EVEN(pmv[3].y);
1409 : edgomez 78 }
1410 : Isibaar 3 CHECK_MV8_CANDIDATE(pmv[3].x,pmv[3].y);
1411 :     }
1412 :     }
1413 :    
1414 : chl 141 if ( (MVzero(*currMV)) && (!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96) */ )
1415 : chl 140 iMinSAD -= MV8_00_BIAS;
1416 :    
1417 :    
1418 : Isibaar 3 /* Step 6: If MinSAD <= thresa goto Step 10.
1419 :     If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10.
1420 :     */
1421 :    
1422 : chl 140 if ( (iMinSAD <= threshA) || ( MVequal(*currMV,prevMB->mvs[iSubBlock]) && ((uint32_t)iMinSAD < prevMB->sad8[iSubBlock]) ) )
1423 : edgomez 78 {
1424 : chl 96 if (MotionFlags & PMV_QUICKSTOP16)
1425 :     goto PMVfast8_Terminate_without_Refine;
1426 :     if (MotionFlags & PMV_EARLYSTOP16)
1427 :     goto PMVfast8_Terminate_with_Refine;
1428 : edgomez 78 }
1429 : Isibaar 3
1430 :     /************ (Diamond Search) **************/
1431 :     /*
1432 : edgomez 78 Step 7: Perform Diamond search, with either the small or large diamond.
1433 :     If Found=2 only examine one Diamond pattern, and afterwards goto step 10
1434 :     Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond.
1435 :     If center then goto step 10.
1436 :     Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center.
1437 :     Refine by using small diamond and goto step 10.
1438 : Isibaar 3 */
1439 :    
1440 :     backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
1441 :    
1442 :     /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
1443 : chl 96 iSAD = Diamond8_MainSearch(pRef, pRefH, pRefV, pRefHV, cur,
1444 : edgomez 78 x, y,
1445 :     currMV->x, currMV->y, iMinSAD, &newMV,
1446 :     pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound);
1447 : Isibaar 3
1448 :     if (iSAD < iMinSAD)
1449 :     {
1450 :     *currMV = newMV;
1451 :     iMinSAD = iSAD;
1452 :     }
1453 :    
1454 :     if (MotionFlags & PMV_EXTSEARCH8)
1455 :     {
1456 :     /* extended: search (up to) two more times: orignal prediction and (0,0) */
1457 :    
1458 :     if (!(MVequal(pmv[0],backupMV)) )
1459 : chl 96 { iSAD = Diamond16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur,
1460 : edgomez 78 x, y,
1461 :     pmv[0].x, pmv[0].y, iMinSAD, &newMV,
1462 :     pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound);
1463 : Isibaar 3
1464 : edgomez 78 if (iSAD < iMinSAD)
1465 :     {
1466 :     *currMV = newMV;
1467 :     iMinSAD = iSAD;
1468 : Isibaar 3 }
1469 : edgomez 78 }
1470 : Isibaar 3
1471 :     if ( (!(MVzero(pmv[0]))) && (!(MVzero(backupMV))) )
1472 : chl 96 { iSAD = Diamond16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur,
1473 : edgomez 78 x, y,
1474 :     0, 0, iMinSAD, &newMV,
1475 :     pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound);
1476 : Isibaar 3
1477 : edgomez 78 if (iSAD < iMinSAD)
1478 :     {
1479 :     *currMV = newMV;
1480 :     iMinSAD = iSAD;
1481 : Isibaar 3 }
1482 : edgomez 78 }
1483 : Isibaar 3 }
1484 :    
1485 :     /* Step 10: The motion vector is chosen according to the block corresponding to MinSAD.
1486 : edgomez 78 By performing an optional local half-pixel search, we can refine this result even further.
1487 : Isibaar 3 */
1488 :    
1489 : chl 96 PMVfast8_Terminate_with_Refine:
1490 : Isibaar 3 if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step
1491 : chl 96 iMinSAD = Halfpel8_Refine( pRef, pRefH, pRefV, pRefHV, cur,
1492 : edgomez 78 x, y,
1493 :     currMV, iMinSAD,
1494 :     pmv, min_dx, max_dx, min_dy, max_dy, iFcode, iQuant, iEdgedWidth);
1495 : Isibaar 3
1496 :    
1497 : chl 96 PMVfast8_Terminate_without_Refine:
1498 : Isibaar 3 currPMV->x = currMV->x - pmv[0].x;
1499 :     currPMV->y = currMV->y - pmv[0].y;
1500 :    
1501 :     return iMinSAD;
1502 :     }
1503 : chl 96
1504 :     int32_t EPZSSearch16(
1505 :     const uint8_t * const pRef,
1506 :     const uint8_t * const pRefH,
1507 :     const uint8_t * const pRefV,
1508 :     const uint8_t * const pRefHV,
1509 :     const IMAGE * const pCur,
1510 :     const int x, const int y,
1511 :     const uint32_t MotionFlags,
1512 : suxen_drol 136 const uint32_t iQuant,
1513 :     const uint32_t iFcode,
1514 : chl 96 const MBParam * const pParam,
1515 : suxen_drol 136 const MACROBLOCK * const pMBs,
1516 :     const MACROBLOCK * const prevMBs,
1517 : chl 96 VECTOR * const currMV,
1518 :     VECTOR * const currPMV)
1519 :     {
1520 : chenm001 132 const uint32_t iWcount = pParam->mb_width;
1521 :     const uint32_t iHcount = pParam->mb_height;
1522 : chl 96
1523 :     const int32_t iWidth = pParam->width;
1524 :     const int32_t iHeight = pParam->height;
1525 :     const int32_t iEdgedWidth = pParam->edged_width;
1526 :    
1527 :     const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth;
1528 :    
1529 :     int32_t min_dx;
1530 :     int32_t max_dx;
1531 :     int32_t min_dy;
1532 :     int32_t max_dy;
1533 :    
1534 :     VECTOR newMV;
1535 :     VECTOR backupMV;
1536 :    
1537 :     VECTOR pmv[4];
1538 :     int32_t psad[8];
1539 :    
1540 :     static MACROBLOCK * oldMBs = NULL;
1541 : suxen_drol 136 const MACROBLOCK * const pMB = pMBs + x + y * iWcount;
1542 :     const MACROBLOCK * const prevMB = prevMBs + x + y * iWcount;
1543 : chl 96 MACROBLOCK * oldMB = NULL;
1544 :    
1545 :     static int32_t thresh2;
1546 :     int32_t bPredEq;
1547 :     int32_t iMinSAD,iSAD=9999;
1548 :    
1549 :     MainSearch16FuncPtr EPZSMainSearchPtr;
1550 :    
1551 :     if (oldMBs == NULL)
1552 : chl 141 { oldMBs = (MACROBLOCK*) calloc(iWcount*iHcount,sizeof(MACROBLOCK));
1553 : chl 140 // fprintf(stderr,"allocated %d bytes for oldMBs\n",iWcount*iHcount*sizeof(MACROBLOCK));
1554 : chl 96 }
1555 :     oldMB = oldMBs + x + y * iWcount;
1556 :    
1557 :     /* Get maximum range */
1558 :     get_range(&min_dx, &max_dx, &min_dy, &max_dy,
1559 :     x, y, 16, iWidth, iHeight, iFcode);
1560 :    
1561 :     if (!(MotionFlags & PMV_HALFPEL16 ))
1562 :     { min_dx = EVEN(min_dx);
1563 :     max_dx = EVEN(max_dx);
1564 :     min_dy = EVEN(min_dy);
1565 :     max_dy = EVEN(max_dy);
1566 :     } /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
1567 :    
1568 :     bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad);
1569 :    
1570 :     /* Step 4: Calculate SAD around the Median prediction.
1571 :     MinSAD=SAD
1572 :     If Motion Vector equal to Previous frame motion vector
1573 :     and MinSAD<PrevFrmSAD goto Step 10.
1574 :     If SAD<=256 goto Step 10.
1575 :     */
1576 :    
1577 :     // Prepare for main loop
1578 :    
1579 :     *currMV=pmv[0]; /* current best := median prediction */
1580 :     if (!(MotionFlags & PMV_HALFPEL16))
1581 :     {
1582 :     currMV->x = EVEN(currMV->x);
1583 :     currMV->y = EVEN(currMV->y);
1584 :     }
1585 :    
1586 :     if (currMV->x > max_dx)
1587 :     currMV->x=max_dx;
1588 :     if (currMV->x < min_dx)
1589 :     currMV->x=min_dx;
1590 :     if (currMV->y > max_dy)
1591 :     currMV->y=max_dy;
1592 :     if (currMV->y < min_dy)
1593 :     currMV->y=min_dy;
1594 :    
1595 :     /***************** This is predictor SET A: only median prediction ******************/
1596 :    
1597 :     iMinSAD = sad16( cur,
1598 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, iEdgedWidth),
1599 :     iEdgedWidth, MV_MAX_ERROR);
1600 : chl 141 iMinSAD += calc_delta_16(currMV->x-pmv[0].x, currMV->y-pmv[0].y, (uint8_t)iFcode, iQuant);
1601 : chl 96
1602 :     // thresh1 is fixed to 256
1603 : chl 141 if ( (iMinSAD < 256 ) || ( (MVequal(*currMV, prevMB->mvs[0])) && ((uint32_t)iMinSAD < prevMB->sad16) ) )
1604 : chl 96 {
1605 :     if (MotionFlags & PMV_QUICKSTOP16)
1606 :     goto EPZS16_Terminate_without_Refine;
1607 :     if (MotionFlags & PMV_EARLYSTOP16)
1608 :     goto EPZS16_Terminate_with_Refine;
1609 :     }
1610 :    
1611 :     /************** This is predictor SET B: (0,0), prev.frame MV, neighbours **************/
1612 :    
1613 :     // previous frame MV
1614 : chl 141 CHECK_MV16_CANDIDATE(prevMB->mvs[0].x,prevMB->mvs[0].y);
1615 : chl 96
1616 :     // set threshhold based on Min of Prediction and SAD of collocated block
1617 :     // CHECK_MV16 always uses iSAD for the SAD of last vector to check, so now iSAD is what we want
1618 :    
1619 :     if ((x==0) && (y==0) )
1620 :     {
1621 :     thresh2 = 512;
1622 :     }
1623 :     else
1624 :     {
1625 :     /* T_k = 1.2 * MIN(SAD_top,SAD_left,SAD_topleft,SAD_coll) +128; [Tourapis, 2002] */
1626 :    
1627 :     thresh2 = MIN(psad[0],iSAD)*6/5 + 128;
1628 :     }
1629 :    
1630 :     // MV=(0,0) is often a good choice
1631 :    
1632 :     CHECK_MV16_ZERO;
1633 :    
1634 :    
1635 :     // left neighbour, if allowed
1636 :     if (x != 0)
1637 :     {
1638 :     if (!(MotionFlags & PMV_HALFPEL16 ))
1639 :     { pmv[1].x = EVEN(pmv[1].x);
1640 :     pmv[1].y = EVEN(pmv[1].y);
1641 :     }
1642 :     CHECK_MV16_CANDIDATE(pmv[1].x,pmv[1].y);
1643 :     }
1644 :    
1645 :     // top neighbour, if allowed
1646 :     if (y != 0)
1647 :     {
1648 :     if (!(MotionFlags & PMV_HALFPEL16 ))
1649 :     { pmv[2].x = EVEN(pmv[2].x);
1650 :     pmv[2].y = EVEN(pmv[2].y);
1651 :     }
1652 :     CHECK_MV16_CANDIDATE(pmv[2].x,pmv[2].y);
1653 :    
1654 :     // top right neighbour, if allowed
1655 : chenm001 132 if ((uint32_t)x != (iWcount-1))
1656 : chl 96 {
1657 :     if (!(MotionFlags & PMV_HALFPEL16 ))
1658 :     { pmv[3].x = EVEN(pmv[3].x);
1659 :     pmv[3].y = EVEN(pmv[3].y);
1660 :     }
1661 :     CHECK_MV16_CANDIDATE(pmv[3].x,pmv[3].y);
1662 :     }
1663 :     }
1664 :    
1665 :     /* Terminate if MinSAD <= T_2
1666 :     Terminate if MV[t] == MV[t-1] and MinSAD[t] <= MinSAD[t-1]
1667 :     */
1668 :    
1669 :     if ( (iMinSAD <= thresh2)
1670 : chl 141 || ( MVequal(*currMV,prevMB->mvs[0]) && ((uint32_t)iMinSAD <= prevMB->sad16) ) )
1671 : chl 96 {
1672 :     if (MotionFlags & PMV_QUICKSTOP16)
1673 :     goto EPZS16_Terminate_without_Refine;
1674 :     if (MotionFlags & PMV_EARLYSTOP16)
1675 :     goto EPZS16_Terminate_with_Refine;
1676 :     }
1677 :    
1678 :     /***** predictor SET C: acceleration MV (new!), neighbours in prev. frame(new!) ****/
1679 :    
1680 : chl 141 backupMV = prevMB->mvs[0]; // collocated MV
1681 :     backupMV.x += (prevMB->mvs[0].x - oldMB->mvs[0].x ); // acceleration X
1682 :     backupMV.y += (prevMB->mvs[0].y - oldMB->mvs[0].y ); // acceleration Y
1683 : chl 96
1684 : chl 141 CHECK_MV16_CANDIDATE(backupMV.x,backupMV.y);
1685 : chl 96
1686 :     // left neighbour
1687 :     if (x != 0)
1688 : chl 141 CHECK_MV16_CANDIDATE((prevMB-1)->mvs[0].x,(prevMB-1)->mvs[0].y);
1689 : chl 96
1690 :     // top neighbour
1691 :     if (y != 0)
1692 : chl 141 CHECK_MV16_CANDIDATE((prevMB-iWcount)->mvs[0].x,(prevMB-iWcount)->mvs[0].y);
1693 : chl 96
1694 :     // right neighbour, if allowed (this value is not written yet, so take it from pMB->mvs
1695 :    
1696 : chenm001 132 if ((uint32_t)x != iWcount-1)
1697 : chl 141 CHECK_MV16_CANDIDATE((prevMB+1)->mvs[0].x,(prevMB+1)->mvs[0].y);
1698 : chl 96
1699 :     // bottom neighbour, dito
1700 : chenm001 132 if ((uint32_t)y != iHcount-1)
1701 : chl 141 CHECK_MV16_CANDIDATE((prevMB+iWcount)->mvs[0].x,(prevMB+iWcount)->mvs[0].y);
1702 : chl 96
1703 :     /* Terminate if MinSAD <= T_3 (here T_3 = T_2) */
1704 :     if (iMinSAD <= thresh2)
1705 :     {
1706 :     if (MotionFlags & PMV_QUICKSTOP16)
1707 :     goto EPZS16_Terminate_without_Refine;
1708 :     if (MotionFlags & PMV_EARLYSTOP16)
1709 :     goto EPZS16_Terminate_with_Refine;
1710 :     }
1711 :    
1712 :     /************ (if Diamond Search) **************/
1713 :    
1714 :     backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
1715 :    
1716 :     /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
1717 :    
1718 :     if (MotionFlags & PMV_USESQUARES16)
1719 :     EPZSMainSearchPtr = Square16_MainSearch;
1720 :     else
1721 :     EPZSMainSearchPtr = Diamond16_MainSearch;
1722 :    
1723 :     iSAD = (*EPZSMainSearchPtr)(pRef, pRefH, pRefV, pRefHV, cur,
1724 :     x, y,
1725 :     currMV->x, currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
1726 :     2, iFcode, iQuant, 0);
1727 :    
1728 :     if (iSAD < iMinSAD)
1729 :     {
1730 :     *currMV = newMV;
1731 :     iMinSAD = iSAD;
1732 :     }
1733 :    
1734 :    
1735 :     if (MotionFlags & PMV_EXTSEARCH16)
1736 :     {
1737 :     /* extended mode: search (up to) two more times: orignal prediction and (0,0) */
1738 :    
1739 :     if (!(MVequal(pmv[0],backupMV)) )
1740 :     {
1741 :     iSAD = (*EPZSMainSearchPtr)(pRef, pRefH, pRefV, pRefHV, cur,
1742 :     x, y,
1743 :     pmv[0].x, pmv[0].y, iMinSAD, &newMV,
1744 :     pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);
1745 :     }
1746 :    
1747 :     if (iSAD < iMinSAD)
1748 :     {
1749 :     *currMV = newMV;
1750 :     iMinSAD = iSAD;
1751 :     }
1752 :    
1753 :     if ( (!(MVzero(pmv[0]))) && (!(MVzero(backupMV))) )
1754 :     {
1755 :     iSAD = (*EPZSMainSearchPtr)(pRef, pRefH, pRefV, pRefHV, cur,
1756 :     x, y,
1757 :     0, 0, iMinSAD, &newMV,
1758 : chl 141 pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);
1759 : chl 96
1760 :     if (iSAD < iMinSAD)
1761 :     {
1762 :     *currMV = newMV;
1763 :     iMinSAD = iSAD;
1764 :     }
1765 :     }
1766 :     }
1767 :    
1768 :     /*************** Choose best MV found **************/
1769 :    
1770 :     EPZS16_Terminate_with_Refine:
1771 :     if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step
1772 :     iMinSAD = Halfpel16_Refine( pRef, pRefH, pRefV, pRefHV, cur,
1773 :     x, y,
1774 :     currMV, iMinSAD,
1775 :     pmv, min_dx, max_dx, min_dy, max_dy, iFcode, iQuant, iEdgedWidth);
1776 :    
1777 :     EPZS16_Terminate_without_Refine:
1778 :    
1779 : chl 141 *oldMB = *prevMB;
1780 : chl 96
1781 :     currPMV->x = currMV->x - pmv[0].x;
1782 :     currPMV->y = currMV->y - pmv[0].y;
1783 :     return iMinSAD;
1784 :     }
1785 :    
1786 :    
1787 :     int32_t EPZSSearch8(
1788 :     const uint8_t * const pRef,
1789 :     const uint8_t * const pRefH,
1790 :     const uint8_t * const pRefV,
1791 :     const uint8_t * const pRefHV,
1792 :     const IMAGE * const pCur,
1793 :     const int x, const int y,
1794 :     const int start_x, const int start_y,
1795 :     const uint32_t MotionFlags,
1796 : suxen_drol 136 const uint32_t iQuant,
1797 :     const uint32_t iFcode,
1798 : chl 96 const MBParam * const pParam,
1799 : suxen_drol 136 const MACROBLOCK * const pMBs,
1800 :     const MACROBLOCK * const prevMBs,
1801 : chl 96 VECTOR * const currMV,
1802 :     VECTOR * const currPMV)
1803 :     {
1804 : chl 141 /* Please not that EPZS might not be a good choice for 8x8-block motion search ! */
1805 :    
1806 :     const uint32_t iWcount = pParam->mb_width;
1807 : chl 96 const int32_t iWidth = pParam->width;
1808 :     const int32_t iHeight = pParam->height;
1809 :     const int32_t iEdgedWidth = pParam->edged_width;
1810 :    
1811 :     const uint8_t * cur = pCur->y + x*8 + y*8*iEdgedWidth;
1812 :    
1813 :     int32_t iDiamondSize=1;
1814 :    
1815 :     int32_t min_dx;
1816 :     int32_t max_dx;
1817 :     int32_t min_dy;
1818 :     int32_t max_dy;
1819 :    
1820 :     VECTOR newMV;
1821 :     VECTOR backupMV;
1822 :    
1823 :     VECTOR pmv[4];
1824 :     int32_t psad[8];
1825 :    
1826 :     const int32_t iSubBlock = ((y&1)<<1) + (x&1);
1827 :    
1828 : suxen_drol 136 const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount;
1829 :     const MACROBLOCK * const prevMB = prevMBs + (x>>1) + (y>>1) * iWcount;
1830 : chl 96
1831 :     int32_t bPredEq;
1832 :     int32_t iMinSAD,iSAD=9999;
1833 :    
1834 :     MainSearch8FuncPtr EPZSMainSearchPtr;
1835 :    
1836 :     /* Get maximum range */
1837 :     get_range(&min_dx, &max_dx, &min_dy, &max_dy,
1838 :     x, y, 8, iWidth, iHeight, iFcode);
1839 :    
1840 :     /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */
1841 :    
1842 :     if (!(MotionFlags & PMV_HALFPEL8 ))
1843 :     { min_dx = EVEN(min_dx);
1844 :     max_dx = EVEN(max_dx);
1845 :     min_dy = EVEN(min_dy);
1846 :     max_dy = EVEN(max_dy);
1847 :     } /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
1848 :    
1849 :     bPredEq = get_pmvdata(pMBs, x>>1, y>>1, iWcount, iSubBlock, pmv, psad);
1850 :    
1851 :    
1852 :     /* Step 4: Calculate SAD around the Median prediction.
1853 :     MinSAD=SAD
1854 :     If Motion Vector equal to Previous frame motion vector
1855 :     and MinSAD<PrevFrmSAD goto Step 10.
1856 :     If SAD<=256 goto Step 10.
1857 :     */
1858 :    
1859 :     // Prepare for main loop
1860 :    
1861 :    
1862 :     if (!(MotionFlags & PMV_HALFPEL8))
1863 :     {
1864 :     currMV->x = EVEN(currMV->x);
1865 :     currMV->y = EVEN(currMV->y);
1866 :     }
1867 :    
1868 :     if (currMV->x > max_dx)
1869 :     currMV->x=max_dx;
1870 :     if (currMV->x < min_dx)
1871 :     currMV->x=min_dx;
1872 :     if (currMV->y > max_dy)
1873 :     currMV->y=max_dy;
1874 :     if (currMV->y < min_dy)
1875 :     currMV->y=min_dy;
1876 :    
1877 :     /***************** This is predictor SET A: only median prediction ******************/
1878 :    
1879 :    
1880 :     iMinSAD = sad8( cur,
1881 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, iEdgedWidth),
1882 :     iEdgedWidth);
1883 : chl 141 iMinSAD += calc_delta_8(currMV->x-pmv[0].x, currMV->y-pmv[0].y, (uint8_t)iFcode, iQuant);
1884 : chl 96
1885 :    
1886 :     // thresh1 is fixed to 256
1887 :     if (iMinSAD < 256/4 )
1888 :     {
1889 :     if (MotionFlags & PMV_QUICKSTOP8)
1890 :     goto EPZS8_Terminate_without_Refine;
1891 :     if (MotionFlags & PMV_EARLYSTOP8)
1892 :     goto EPZS8_Terminate_with_Refine;
1893 :     }
1894 :    
1895 :     /************** This is predictor SET B: (0,0), prev.frame MV, neighbours **************/
1896 :    
1897 :    
1898 :     // MV=(0,0) is often a good choice
1899 :     CHECK_MV8_ZERO;
1900 :    
1901 : chl 141 // previous frame MV
1902 :     CHECK_MV8_CANDIDATE(prevMB->mvs[iSubBlock].x,prevMB->mvs[iSubBlock].y);
1903 :    
1904 :     // left neighbour, if allowed
1905 :     if (psad[1] != MV_MAX_ERROR)
1906 :     {
1907 :     if (!(MotionFlags & PMV_HALFPEL8 ))
1908 :     { pmv[1].x = EVEN(pmv[1].x);
1909 :     pmv[1].y = EVEN(pmv[1].y);
1910 :     }
1911 :     CHECK_MV8_CANDIDATE(pmv[1].x,pmv[1].y);
1912 :     }
1913 :    
1914 :     // top neighbour, if allowed
1915 :     if (psad[2] != MV_MAX_ERROR)
1916 :     {
1917 :     if (!(MotionFlags & PMV_HALFPEL8 ))
1918 :     { pmv[2].x = EVEN(pmv[2].x);
1919 :     pmv[2].y = EVEN(pmv[2].y);
1920 :     }
1921 :     CHECK_MV8_CANDIDATE(pmv[2].x,pmv[2].y);
1922 :    
1923 :     // top right neighbour, if allowed
1924 :     if (psad[3] != MV_MAX_ERROR)
1925 :     {
1926 :     if (!(MotionFlags & PMV_HALFPEL8 ))
1927 :     { pmv[3].x = EVEN(pmv[3].x);
1928 :     pmv[3].y = EVEN(pmv[3].y);
1929 :     }
1930 :     CHECK_MV8_CANDIDATE(pmv[3].x,pmv[3].y);
1931 :     }
1932 :     }
1933 :    
1934 :     /* // this bias is zero anyway, at the moment!
1935 :    
1936 :     if ( (MVzero(*currMV)) && (!MVzero(pmv[0])) ) // && (iMinSAD <= iQuant * 96)
1937 :     iMinSAD -= MV8_00_BIAS;
1938 :    
1939 :     */
1940 :    
1941 : chl 96 /* Terminate if MinSAD <= T_2
1942 :     Terminate if MV[t] == MV[t-1] and MinSAD[t] <= MinSAD[t-1]
1943 :     */
1944 :    
1945 :     if (iMinSAD < 512/4) /* T_2 == 512/4 hardcoded */
1946 :     {
1947 :     if (MotionFlags & PMV_QUICKSTOP8)
1948 :     goto EPZS8_Terminate_without_Refine;
1949 :     if (MotionFlags & PMV_EARLYSTOP8)
1950 :     goto EPZS8_Terminate_with_Refine;
1951 :     }
1952 :    
1953 : chl 141 /************ (Diamond Search) **************/
1954 : chl 96
1955 :     backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
1956 :    
1957 :     if (!(MotionFlags & PMV_HALFPELDIAMOND8))
1958 :     iDiamondSize *= 2;
1959 :    
1960 : chl 141 /* default: use best prediction as starting point for one call of EPZS_MainSearch */
1961 : chl 96
1962 : chl 141 /* // there is no EPZS^2 for inter4v at the moment
1963 :    
1964 :     if (MotionFlags & PMV_USESQUARES8)
1965 :     EPZSMainSearchPtr = Square8_MainSearch;
1966 :     else
1967 :     */
1968 :    
1969 :     EPZSMainSearchPtr = Diamond8_MainSearch;
1970 : chl 96
1971 :     iSAD = (*EPZSMainSearchPtr)(pRef, pRefH, pRefV, pRefHV, cur,
1972 :     x, y,
1973 :     currMV->x, currMV->y, iMinSAD, &newMV,
1974 :     pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
1975 : chl 141 iDiamondSize, iFcode, iQuant, 0);
1976 : chl 96
1977 : chl 141
1978 : chl 96 if (iSAD < iMinSAD)
1979 :     {
1980 :     *currMV = newMV;
1981 :     iMinSAD = iSAD;
1982 :     }
1983 :    
1984 :     if (MotionFlags & PMV_EXTSEARCH8)
1985 :     {
1986 :     /* extended mode: search (up to) two more times: orignal prediction and (0,0) */
1987 :    
1988 :     if (!(MVequal(pmv[0],backupMV)) )
1989 :     {
1990 :     iSAD = (*EPZSMainSearchPtr)(pRef, pRefH, pRefV, pRefHV, cur,
1991 :     x, y,
1992 :     pmv[0].x, pmv[0].y, iMinSAD, &newMV,
1993 :     pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, 0);
1994 :    
1995 :     if (iSAD < iMinSAD)
1996 :     {
1997 :     *currMV = newMV;
1998 :     iMinSAD = iSAD;
1999 :     }
2000 :     }
2001 :    
2002 :     if ( (!(MVzero(pmv[0]))) && (!(MVzero(backupMV))) )
2003 :     {
2004 :     iSAD = (*EPZSMainSearchPtr)(pRef, pRefH, pRefV, pRefHV, cur,
2005 :     x, y,
2006 :     0, 0, iMinSAD, &newMV,
2007 :     pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, 0);
2008 :    
2009 :     if (iSAD < iMinSAD)
2010 :     {
2011 :     *currMV = newMV;
2012 :     iMinSAD = iSAD;
2013 :     }
2014 :     }
2015 :     }
2016 :    
2017 :     /*************** Choose best MV found **************/
2018 :    
2019 :     EPZS8_Terminate_with_Refine:
2020 :     if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step
2021 :     iMinSAD = Halfpel8_Refine( pRef, pRefH, pRefV, pRefHV, cur,
2022 :     x, y,
2023 :     currMV, iMinSAD,
2024 :     pmv, min_dx, max_dx, min_dy, max_dy, iFcode, iQuant, iEdgedWidth);
2025 :    
2026 :     EPZS8_Terminate_without_Refine:
2027 :    
2028 :     currPMV->x = currMV->x - pmv[0].x;
2029 :     currPMV->y = currMV->y - pmv[0].y;
2030 :     return iMinSAD;
2031 :     }
2032 :    
2033 : suxen_drol 118
2034 :    
2035 :    
2036 :    
2037 :     /* ***********************************************************
2038 :     bvop motion estimation
2039 :     // TODO: need to incorporate prediction here (eg. sad += calc_delta_16)
2040 :     ***************************************************************/
2041 :    
2042 : suxen_drol 152
2043 : suxen_drol 118 void MotionEstimationBVOP(
2044 :     MBParam * const pParam,
2045 :     FRAMEINFO * const frame,
2046 :    
2047 :     // forward (past) reference
2048 :     const MACROBLOCK * const f_mbs,
2049 :     const IMAGE * const f_ref,
2050 :     const IMAGE * const f_refH,
2051 :     const IMAGE * const f_refV,
2052 :     const IMAGE * const f_refHV,
2053 :     // backward (future) reference
2054 :     const MACROBLOCK * const b_mbs,
2055 :     const IMAGE * const b_ref,
2056 :     const IMAGE * const b_refH,
2057 :     const IMAGE * const b_refV,
2058 :     const IMAGE * const b_refHV)
2059 :     {
2060 :     const uint32_t mb_width = pParam->mb_width;
2061 :     const uint32_t mb_height = pParam->mb_height;
2062 :     const int32_t edged_width = pParam->edged_width;
2063 :    
2064 : suxen_drol 152 uint32_t i,j;
2065 : suxen_drol 118
2066 :     int32_t f_sad16;
2067 :     int32_t b_sad16;
2068 :     int32_t i_sad16;
2069 :     int32_t d_sad16;
2070 :     int32_t best_sad;
2071 :    
2072 :     VECTOR pmv_dontcare;
2073 :    
2074 :     // note: i==horizontal, j==vertical
2075 :     for (j = 0; j < mb_height; j++)
2076 :     {
2077 :     for (i = 0; i < mb_width; i++)
2078 :     {
2079 :     MACROBLOCK *mb = &frame->mbs[i + j*mb_width];
2080 :     const MACROBLOCK *f_mb = &f_mbs[i + j*mb_width];
2081 :     const MACROBLOCK *b_mb = &b_mbs[i + j*mb_width];
2082 :    
2083 :     if (b_mb->mode == MODE_INTER
2084 :     && b_mb->cbp == 0
2085 :     && b_mb->mvs[0].x == 0
2086 :     && b_mb->mvs[0].y == 0)
2087 :     {
2088 : suxen_drol 152 mb->mode = MODE_NOT_CODED;
2089 : suxen_drol 118 mb->mvs[0].x = 0;
2090 :     mb->mvs[0].y = 0;
2091 :     mb->b_mvs[0].x = 0;
2092 :     mb->b_mvs[0].y = 0;
2093 :     continue;
2094 :     }
2095 :    
2096 :    
2097 :     // forward search
2098 :     f_sad16 = SEARCH16(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,
2099 :     &frame->image,
2100 :     i, j,
2101 :     frame->motion_flags, frame->quant, frame->fcode,
2102 :     pParam,
2103 : suxen_drol 152 f_mbs, f_mbs /* todo */,
2104 : suxen_drol 118 &mb->mvs[0], &pmv_dontcare); // ignore pmv
2105 :    
2106 :     // backward search
2107 :     b_sad16 = SEARCH16(b_ref->y, b_refH->y, b_refV->y, b_refHV->y,
2108 :     &frame->image,
2109 :     i, j,
2110 :     frame->motion_flags, frame->quant, frame->bcode,
2111 :     pParam,
2112 : suxen_drol 152 b_mbs, b_mbs, /* todo */
2113 : suxen_drol 118 &mb->b_mvs[0], &pmv_dontcare); // ignore pmv
2114 :    
2115 :     // interpolate search (simple, but effective)
2116 :     i_sad16 = sad16bi_c(
2117 :     frame->image.y + i*16 + j*16*edged_width,
2118 :     get_ref(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,
2119 :     i, j, 16, mb->mvs[0].x, mb->mvs[0].y, edged_width),
2120 :     get_ref(b_ref->y, b_refH->y, b_refV->y, b_refHV->y,
2121 :     i, j, 16, mb->b_mvs[0].x, mb->b_mvs[0].x, edged_width),
2122 :     edged_width);
2123 :    
2124 :     // TODO: direct search
2125 :     // predictor + range of [-32,32]
2126 :     d_sad16 = 65535;
2127 :    
2128 :    
2129 :     if (f_sad16 < b_sad16)
2130 :     {
2131 :     best_sad = f_sad16;
2132 : suxen_drol 152 mb->mode = MODE_FORWARD;
2133 : suxen_drol 118 }
2134 :     else
2135 :     {
2136 :     best_sad = b_sad16;
2137 : suxen_drol 152 mb->mode = MODE_BACKWARD;
2138 : suxen_drol 118 }
2139 :    
2140 :     if (i_sad16 < best_sad)
2141 :     {
2142 :     best_sad = i_sad16;
2143 : suxen_drol 152 mb->mode = MODE_INTERPOLATE;
2144 : suxen_drol 118 }
2145 :    
2146 :     if (d_sad16 < best_sad)
2147 :     {
2148 :     best_sad = d_sad16;
2149 : suxen_drol 152 mb->mode = MODE_DIRECT;
2150 : suxen_drol 118 }
2151 :    
2152 :     }
2153 :     }
2154 :     }

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