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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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

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