[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 261 - (view) (download)
Original Path: trunk/xvidcore/src/motion/motion_est.c

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

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