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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 486 - (view) (download)

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 :     /* too slow and not fully functional at the moment */
360 :     /*
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 :     */
400 :    
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 :     return iMinSAD;
843 :     }
844 :    
845 : chl 346 #define CHECK_MV16_F_INTERPOL(X,Y) { \
846 :     if ( ((X) <= f_max_dx) && ((X) >= f_min_dx) \
847 :     && ((Y) <= f_max_dy) && ((Y) >= f_min_dy) ) \
848 : chl 326 { \
849 : chl 346 iSAD = sad16bi( cur, \
850 :     get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, X, Y, iEdgedWidth), \
851 :     get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, b_currMV->x, b_currMV->y, iEdgedWidth), \
852 :     iEdgedWidth); \
853 :     iSAD += calc_delta_16((X) - f_center_x, (Y) - f_center_y, (uint8_t)f_iFcode, iQuant);\
854 :     iSAD += calc_delta_16(b_currMV->x - b_center_x, b_currMV->y - b_center_y, (uint8_t)b_iFcode, iQuant);\
855 : chl 326 if (iSAD < iMinSAD) \
856 : chl 346 { iMinSAD=iSAD; f_currMV->x=(X); f_currMV->y=(Y); } } \
857 : chl 326 }
858 :    
859 : chl 346 #define CHECK_MV16_F_INTERPOL_FOUND(X,Y) { \
860 :     if ( ((X) <= f_max_dx) && ((X) >= f_min_dx) \
861 :     && ((Y) <= f_max_dy) && ((Y) >= f_min_dy) ) \
862 : chl 326 { \
863 : chl 346 iSAD = sad16bi( cur, \
864 :     get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, X, Y, iEdgedWidth), \
865 :     get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, b_currMV->x, b_currMV->y, iEdgedWidth), \
866 :     iEdgedWidth); \
867 :     iSAD += calc_delta_16((X) - f_center_x, (Y) - f_center_y, (uint8_t)f_iFcode, iQuant);\
868 :     iSAD += calc_delta_16(b_currMV->x - b_center_x, b_currMV->y - b_center_y, (uint8_t)b_iFcode, iQuant);\
869 : chl 326 if (iSAD < iMinSAD) \
870 : chl 346 { iMinSAD=iSAD; f_currMV->x=(X); f_currMV->y=(Y); iFound=0;} } \
871 : chl 326 }
872 :    
873 : chl 346 #define CHECK_MV16_B_INTERPOL(X,Y) { \
874 :     if ( ((X) <= b_max_dx) && ((X) >= b_min_dx) \
875 :     && ((Y) <= b_max_dy) && ((Y) >= b_min_dy) ) \
876 : chl 326 { \
877 : chl 346 iSAD = sad16bi( cur, \
878 :     get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, f_currMV->x, f_currMV->y, iEdgedWidth), \
879 :     get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, X, Y, iEdgedWidth), \
880 :     iEdgedWidth); \
881 :     iSAD += calc_delta_16(f_currMV->x - f_center_x, f_currMV->y - f_center_y, (uint8_t)f_iFcode, iQuant);\
882 :     iSAD += calc_delta_16((X) - b_center_x, (Y) - b_center_y, (uint8_t)b_iFcode, iQuant);\
883 : chl 326 if (iSAD < iMinSAD) \
884 : chl 346 { iMinSAD=iSAD; b_currMV->x=(X); b_currMV->y=(Y); } } \
885 : chl 326 }
886 :    
887 : chl 346 #define CHECK_MV16_B_INTERPOL_FOUND(X,Y) { \
888 :     if ( ((X) <= b_max_dx) && ((X) >= b_min_dx) \
889 :     && ((Y) <= b_max_dy) && ((Y) >= b_min_dy) ) \
890 : chl 326 { \
891 : chl 346 iSAD = sad16bi( cur, \
892 :     get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, f_currMV->x, f_currMV->y, iEdgedWidth), \
893 :     get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, X, Y, iEdgedWidth), \
894 :     iEdgedWidth); \
895 :     iSAD += calc_delta_16(f_currMV->x - f_center_x, f_currMV->y - f_center_y, (uint8_t)f_iFcode, iQuant);\
896 :     iSAD += calc_delta_16((X) - b_center_x, (Y) - b_center_y, (uint8_t)b_iFcode, iQuant);\
897 : chl 326 if (iSAD < iMinSAD) \
898 : chl 346 { iMinSAD=iSAD; b_currMV->x=(X); b_currMV->y=(Y); iFound=0;} } \
899 : chl 326 }
900 :    
901 : edgomez 195 int32_t
902 : edgomez 476 Diamond16_InterpolMainSearch(const uint8_t * const f_pRef,
903 :     const uint8_t * const f_pRefH,
904 :     const uint8_t * const f_pRefV,
905 :     const uint8_t * const f_pRefHV,
906 : chl 346
907 : edgomez 476 const uint8_t * const cur,
908 : chl 326
909 : edgomez 476 const uint8_t * const b_pRef,
910 :     const uint8_t * const b_pRefH,
911 :     const uint8_t * const b_pRefV,
912 :     const uint8_t * const b_pRefHV,
913 : chl 326
914 : edgomez 476 const int x,
915 :     const int y,
916 : chl 326
917 : edgomez 476 const int f_start_x,
918 :     const int f_start_y,
919 :     const int b_start_x,
920 :     const int b_start_y,
921 : chl 326
922 : edgomez 476 int iMinSAD,
923 :     VECTOR * const f_currMV,
924 :     VECTOR * const b_currMV,
925 : chl 326
926 : edgomez 476 const int f_center_x,
927 :     const int f_center_y,
928 :     const int b_center_x,
929 :     const int b_center_y,
930 : chl 326
931 : edgomez 476 const int32_t f_min_dx,
932 :     const int32_t f_max_dx,
933 :     const int32_t f_min_dy,
934 :     const int32_t f_max_dy,
935 : chl 326
936 : edgomez 476 const int32_t b_min_dx,
937 :     const int32_t b_max_dx,
938 :     const int32_t b_min_dy,
939 :     const int32_t b_max_dy,
940 : chl 326
941 : edgomez 476 const int32_t iEdgedWidth,
942 :     const int32_t iDiamondSize,
943 : chl 346
944 : edgomez 476 const int32_t f_iFcode,
945 :     const int32_t b_iFcode,
946 : chl 346
947 : edgomez 476 const int32_t iQuant,
948 :     int iFound)
949 : chl 326 {
950 :     /* Do a diamond search around given starting point, return SAD of best */
951 :    
952 :     int32_t iSAD;
953 :    
954 :     VECTOR f_backupMV;
955 :     VECTOR b_backupMV;
956 :    
957 : chl 346 f_currMV->x = f_start_x;
958 :     f_currMV->y = f_start_y;
959 :     b_currMV->x = b_start_x;
960 :     b_currMV->y = b_start_y;
961 : chl 326
962 : chl 346 do
963 :     {
964 :     iFound = 1;
965 :    
966 :     f_backupMV = *f_currMV;
967 :    
968 :     CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x - iDiamondSize, f_backupMV.y);
969 :     CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x + iDiamondSize, f_backupMV.y);
970 :     CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x, f_backupMV.y - iDiamondSize);
971 :     CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x, f_backupMV.y + iDiamondSize);
972 :    
973 :     b_backupMV = *b_currMV;
974 :    
975 :     CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x - iDiamondSize, b_backupMV.y);
976 :     CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x + iDiamondSize, b_backupMV.y);
977 :     CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x, b_backupMV.y - iDiamondSize);
978 :     CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x, b_backupMV.y + iDiamondSize);
979 :    
980 :     } while (!iFound);
981 :    
982 :     return iMinSAD;
983 :     }
984 :    
985 :     /* Sorry, these MACROS really got too large... I'll turn them into function soon! */
986 :    
987 :     #define CHECK_MV16_DIRECT_FOUND(X,Y) \
988 :     if ( (X)>=(-32) && (X)<=(31) && ((Y)>=-32) && ((Y)<=31) ) \
989 :     { int k;\
990 :     VECTOR mvs,b_mvs; \
991 :     iSAD = 0;\
992 :     for (k = 0; k < 4; k++) { \
993 :     mvs.x = (int32_t) ((TRB * directmv[k].x) / TRD + (X)); \
994 :     b_mvs.x = (int32_t) (((X) == 0) \
995 :     ? ((TRB - TRD) * directmv[k].x) / TRD \
996 :     : mvs.x - directmv[k].x); \
997 :     \
998 :     mvs.y = (int32_t) ((TRB * directmv[k].y) / TRD + (Y)); \
999 :     b_mvs.y = (int32_t) (((Y) == 0) \
1000 :     ? ((TRB - TRD) * directmv[k].y) / TRD \
1001 :     : mvs.y - directmv[k].y); \
1002 :     \
1003 :     if ( (mvs.x <= max_dx) && (mvs.x >= min_dx) \
1004 :     && (mvs.y <= max_dy) && (mvs.y >= min_dy) \
1005 :     && (b_mvs.x <= max_dx) && (b_mvs.x >= min_dx) \
1006 :     && (b_mvs.y <= max_dy) && (b_mvs.y >= min_dy) ) { \
1007 :     iSAD += sad8bi( cur + 8*(k&1) + 8*(k>>1)*iEdgedWidth, \
1008 :     get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, 2*x+(k&1), 2*y+(k>>1), 8, \
1009 :     mvs.x, mvs.y, iEdgedWidth), \
1010 :     get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, 2*x+(k&1), 2*y+(k>>1), 8, \
1011 :     b_mvs.x, b_mvs.y, iEdgedWidth), \
1012 :     iEdgedWidth); \
1013 :     } \
1014 :     else \
1015 :     iSAD = 65535; \
1016 :     } \
1017 :     iSAD += calc_delta_16((X),(Y), 1, iQuant);\
1018 :     if (iSAD < iMinSAD) \
1019 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iFound=0; } \
1020 :     }
1021 :    
1022 :    
1023 :    
1024 :     int32_t
1025 :     Diamond16_DirectMainSearch(
1026 :     const uint8_t * const f_pRef,
1027 :     const uint8_t * const f_pRefH,
1028 :     const uint8_t * const f_pRefV,
1029 :     const uint8_t * const f_pRefHV,
1030 :    
1031 :     const uint8_t * const cur,
1032 :    
1033 :     const uint8_t * const b_pRef,
1034 :     const uint8_t * const b_pRefH,
1035 :     const uint8_t * const b_pRefV,
1036 :     const uint8_t * const b_pRefHV,
1037 :    
1038 :     const int x,
1039 :     const int y,
1040 :    
1041 :     const int TRB,
1042 :     const int TRD,
1043 :    
1044 :     const int start_x,
1045 :     const int start_y,
1046 :    
1047 :     int iMinSAD,
1048 :     VECTOR * const currMV,
1049 :     const VECTOR * const directmv,
1050 :    
1051 :     const int32_t min_dx,
1052 :     const int32_t max_dx,
1053 :     const int32_t min_dy,
1054 :     const int32_t max_dy,
1055 :    
1056 :     const int32_t iEdgedWidth,
1057 :     const int32_t iDiamondSize,
1058 :    
1059 :     const int32_t iQuant,
1060 :     int iFound)
1061 :     {
1062 :     /* Do a diamond search around given starting point, return SAD of best */
1063 :    
1064 :     int32_t iSAD;
1065 :    
1066 :     VECTOR backupMV;
1067 :    
1068 :     currMV->x = start_x;
1069 :     currMV->y = start_y;
1070 :    
1071 : chl 326 /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
1072 :    
1073 : chl 346 do
1074 :     {
1075 :     iFound = 1;
1076 :    
1077 :     backupMV = *currMV;
1078 :    
1079 :     CHECK_MV16_DIRECT_FOUND(backupMV.x - iDiamondSize, backupMV.y);
1080 :     CHECK_MV16_DIRECT_FOUND(backupMV.x + iDiamondSize, backupMV.y);
1081 :     CHECK_MV16_DIRECT_FOUND(backupMV.x, backupMV.y - iDiamondSize);
1082 :     CHECK_MV16_DIRECT_FOUND(backupMV.x, backupMV.y + iDiamondSize);
1083 : chl 326
1084 : chl 346 } while (!iFound);
1085 : chl 326
1086 :     return iMinSAD;
1087 :     }
1088 :    
1089 :    
1090 :     int32_t
1091 : edgomez 195 AdvDiamond8_MainSearch(const uint8_t * const pRef,
1092 :     const uint8_t * const pRefH,
1093 :     const uint8_t * const pRefV,
1094 :     const uint8_t * const pRefHV,
1095 :     const uint8_t * const cur,
1096 :     const int x,
1097 :     const int y,
1098 : edgomez 476 const int start_xi,
1099 :     const int start_yi,
1100 : chl 326 int iMinSAD,
1101 : edgomez 195 VECTOR * const currMV,
1102 : chl 326 const int center_x,
1103 :     const int center_y,
1104 : edgomez 195 const int32_t min_dx,
1105 :     const int32_t max_dx,
1106 :     const int32_t min_dy,
1107 :     const int32_t max_dy,
1108 :     const int32_t iEdgedWidth,
1109 :     const int32_t iDiamondSize,
1110 :     const int32_t iFcode,
1111 :     const int32_t iQuant,
1112 :     int iDirection)
1113 : chl 181 {
1114 :    
1115 :     int32_t iSAD;
1116 : edgomez 476 int start_x = start_xi, start_y = start_yi;
1117 : chl 181
1118 :     /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
1119 :    
1120 : edgomez 195 if (iDirection) {
1121 : chl 326 CHECK_MV8_CANDIDATE(start_x - iDiamondSize, start_y);
1122 :     CHECK_MV8_CANDIDATE(start_x + iDiamondSize, start_y);
1123 :     CHECK_MV8_CANDIDATE(start_x, start_y - iDiamondSize);
1124 :     CHECK_MV8_CANDIDATE(start_x, start_y + iDiamondSize);
1125 : edgomez 195 } else {
1126 :     int bDirection = 1 + 2 + 4 + 8;
1127 :    
1128 :     do {
1129 : chl 181 iDirection = 0;
1130 : 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)
1131 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
1132 : chl 181
1133 : edgomez 195 if (bDirection & 2)
1134 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
1135 : chl 181
1136 : edgomez 195 if (bDirection & 4)
1137 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
1138 : chl 181
1139 : edgomez 195 if (bDirection & 8)
1140 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
1141 : chl 181
1142 :     /* now we're doing diagonal checks near our candidate */
1143 :    
1144 : edgomez 195 if (iDirection) //checking if anything found
1145 : chl 181 {
1146 :     bDirection = iDirection;
1147 :     iDirection = 0;
1148 : chl 326 start_x = currMV->x;
1149 :     start_y = currMV->y;
1150 : edgomez 195 if (bDirection & 3) //our candidate is left or right
1151 : chl 181 {
1152 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
1153 :     CHECK_MV8_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
1154 : edgomez 195 } else // what remains here is up or down
1155 : chl 181 {
1156 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
1157 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
1158 : chl 181 }
1159 :    
1160 : edgomez 195 if (iDirection) {
1161 :     bDirection += iDirection;
1162 : chl 326 start_x = currMV->x;
1163 :     start_y = currMV->y;
1164 : chl 181 }
1165 : edgomez 195 } else //about to quit, eh? not so fast....
1166 : chl 181 {
1167 : edgomez 195 switch (bDirection) {
1168 : chl 181 case 2:
1169 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1170 :     start_y - iDiamondSize, 2 + 4);
1171 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1172 :     start_y + iDiamondSize, 2 + 8);
1173 : chl 181 break;
1174 :     case 1:
1175 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1176 :     start_y - iDiamondSize, 1 + 4);
1177 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1178 :     start_y + iDiamondSize, 1 + 8);
1179 : chl 181 break;
1180 : edgomez 195 case 2 + 4:
1181 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1182 :     start_y - iDiamondSize, 1 + 4);
1183 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1184 :     start_y - iDiamondSize, 2 + 4);
1185 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1186 :     start_y + iDiamondSize, 2 + 8);
1187 : chl 181 break;
1188 :     case 4:
1189 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1190 :     start_y - iDiamondSize, 2 + 4);
1191 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1192 :     start_y - iDiamondSize, 1 + 4);
1193 : chl 181 break;
1194 :     case 8:
1195 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1196 :     start_y + iDiamondSize, 2 + 8);
1197 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1198 :     start_y + iDiamondSize, 1 + 8);
1199 : chl 181 break;
1200 : edgomez 195 case 1 + 4:
1201 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1202 :     start_y + iDiamondSize, 1 + 8);
1203 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1204 :     start_y - iDiamondSize, 1 + 4);
1205 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1206 :     start_y - iDiamondSize, 2 + 4);
1207 : chl 181 break;
1208 : edgomez 195 case 2 + 8:
1209 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1210 :     start_y - iDiamondSize, 1 + 4);
1211 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1212 :     start_y + iDiamondSize, 1 + 8);
1213 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1214 :     start_y + iDiamondSize, 2 + 8);
1215 : chl 181 break;
1216 : edgomez 195 case 1 + 8:
1217 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1218 :     start_y - iDiamondSize, 2 + 4);
1219 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1220 :     start_y + iDiamondSize, 2 + 8);
1221 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1222 :     start_y + iDiamondSize, 1 + 8);
1223 : chl 181 break;
1224 : edgomez 195 default: //1+2+4+8 == we didn't find anything at all
1225 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1226 :     start_y - iDiamondSize, 1 + 4);
1227 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1228 :     start_y + iDiamondSize, 1 + 8);
1229 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1230 :     start_y - iDiamondSize, 2 + 4);
1231 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1232 :     start_y + iDiamondSize, 2 + 8);
1233 : chl 181 break;
1234 :     }
1235 : edgomez 195 if (!(iDirection))
1236 :     break; //ok, the end. really
1237 :     else {
1238 :     bDirection = iDirection;
1239 : chl 326 start_x = currMV->x;
1240 :     start_y = currMV->y;
1241 : chl 181 }
1242 :     }
1243 :     }
1244 : edgomez 195 while (1); //forever
1245 : chl 181 }
1246 :     return iMinSAD;
1247 :     }
1248 :    
1249 :    
1250 : edgomez 195 int32_t
1251 :     Full8_MainSearch(const uint8_t * const pRef,
1252 :     const uint8_t * const pRefH,
1253 :     const uint8_t * const pRefV,
1254 :     const uint8_t * const pRefHV,
1255 :     const uint8_t * const cur,
1256 :     const int x,
1257 :     const int y,
1258 : chl 326 const int start_x,
1259 :     const int start_y,
1260 :     int iMinSAD,
1261 :     VECTOR * const currMV,
1262 :     const int center_x,
1263 :     const int center_y,
1264 : edgomez 195 const int32_t min_dx,
1265 :     const int32_t max_dx,
1266 :     const int32_t min_dy,
1267 :     const int32_t max_dy,
1268 :     const int32_t iEdgedWidth,
1269 :     const int32_t iDiamondSize,
1270 :     const int32_t iFcode,
1271 :     const int32_t iQuant,
1272 :     int iFound)
1273 : chl 96 {
1274 :     int32_t iSAD;
1275 : edgomez 195 int32_t dx, dy;
1276 : chl 96 VECTOR backupMV;
1277 : edgomez 195
1278 : chl 326 backupMV.x = start_x;
1279 :     backupMV.y = start_y;
1280 : chl 96
1281 : edgomez 195 for (dx = min_dx; dx <= max_dx; dx += iDiamondSize)
1282 :     for (dy = min_dy; dy <= max_dy; dy += iDiamondSize)
1283 :     NOCHECK_MV8_CANDIDATE(dx, dy);
1284 :    
1285 : chl 96 return iMinSAD;
1286 :     }
1287 :    
1288 : ia64p 300 Halfpel8_RefineFuncPtr Halfpel8_Refine;
1289 : chl 96
1290 : edgomez 195 int32_t
1291 :     Halfpel16_Refine(const uint8_t * const pRef,
1292 :     const uint8_t * const pRefH,
1293 :     const uint8_t * const pRefV,
1294 :     const uint8_t * const pRefHV,
1295 :     const uint8_t * const cur,
1296 :     const int x,
1297 :     const int y,
1298 :     VECTOR * const currMV,
1299 :     int32_t iMinSAD,
1300 : chl 326 const int center_x,
1301 :     const int center_y,
1302 : edgomez 195 const int32_t min_dx,
1303 :     const int32_t max_dx,
1304 :     const int32_t min_dy,
1305 :     const int32_t max_dy,
1306 :     const int32_t iFcode,
1307 :     const int32_t iQuant,
1308 :     const int32_t iEdgedWidth)
1309 : Isibaar 3 {
1310 :     /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */
1311 :    
1312 :     int32_t iSAD;
1313 :     VECTOR backupMV = *currMV;
1314 : edgomez 195
1315 :     CHECK_MV16_CANDIDATE(backupMV.x - 1, backupMV.y - 1);
1316 :     CHECK_MV16_CANDIDATE(backupMV.x, backupMV.y - 1);
1317 :     CHECK_MV16_CANDIDATE(backupMV.x + 1, backupMV.y - 1);
1318 :     CHECK_MV16_CANDIDATE(backupMV.x - 1, backupMV.y);
1319 :     CHECK_MV16_CANDIDATE(backupMV.x + 1, backupMV.y);
1320 :     CHECK_MV16_CANDIDATE(backupMV.x - 1, backupMV.y + 1);
1321 :     CHECK_MV16_CANDIDATE(backupMV.x, backupMV.y + 1);
1322 :     CHECK_MV16_CANDIDATE(backupMV.x + 1, backupMV.y + 1);
1323 :    
1324 : Isibaar 3 return iMinSAD;
1325 :     }
1326 :    
1327 :     #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16)
1328 :    
1329 : chl 96
1330 : chl 326
1331 : edgomez 195 int32_t
1332 :     PMVfastSearch16(const uint8_t * const pRef,
1333 :     const uint8_t * const pRefH,
1334 :     const uint8_t * const pRefV,
1335 :     const uint8_t * const pRefHV,
1336 :     const IMAGE * const pCur,
1337 :     const int x,
1338 :     const int y,
1339 : chl 346 const int start_x, /* start is searched first, so it should contain the most */
1340 :     const int start_y, /* likely motion vector for this block */
1341 :     const int center_x, /* center is from where length of MVs is measured */
1342 : chl 326 const int center_y,
1343 : edgomez 195 const uint32_t MotionFlags,
1344 :     const uint32_t iQuant,
1345 :     const uint32_t iFcode,
1346 :     const MBParam * const pParam,
1347 :     const MACROBLOCK * const pMBs,
1348 :     const MACROBLOCK * const prevMBs,
1349 :     VECTOR * const currMV,
1350 :     VECTOR * const currPMV)
1351 : Isibaar 3 {
1352 : edgomez 195 const uint32_t iWcount = pParam->mb_width;
1353 : Isibaar 3 const int32_t iWidth = pParam->width;
1354 :     const int32_t iHeight = pParam->height;
1355 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
1356 : Isibaar 3
1357 : edgomez 195 const uint8_t *cur = pCur->y + x * 16 + y * 16 * iEdgedWidth;
1358 : Isibaar 3
1359 :     int32_t iDiamondSize;
1360 : edgomez 195
1361 : Isibaar 3 int32_t min_dx;
1362 :     int32_t max_dx;
1363 :     int32_t min_dy;
1364 :     int32_t max_dy;
1365 : edgomez 195
1366 : Isibaar 3 int32_t iFound;
1367 :    
1368 :     VECTOR newMV;
1369 : edgomez 195 VECTOR backupMV; /* just for PMVFAST */
1370 :    
1371 : Isibaar 3 VECTOR pmv[4];
1372 :     int32_t psad[4];
1373 : chl 181
1374 :     MainSearch16FuncPtr MainSearchPtr;
1375 : Isibaar 3
1376 : edgomez 195 const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;
1377 : Isibaar 3
1378 : chl 259 int32_t threshA, threshB;
1379 : edgomez 195 int32_t bPredEq;
1380 :     int32_t iMinSAD, iSAD;
1381 :    
1382 : Isibaar 3 /* Get maximum range */
1383 : edgomez 195 get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight,
1384 :     iFcode);
1385 : Isibaar 3
1386 :     /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */
1387 :    
1388 : edgomez 195 if (!(MotionFlags & PMV_HALFPEL16)) {
1389 :     min_dx = EVEN(min_dx);
1390 :     max_dx = EVEN(max_dx);
1391 :     min_dy = EVEN(min_dy);
1392 :     max_dy = EVEN(max_dy);
1393 :     }
1394 : Isibaar 3
1395 : edgomez 195 /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
1396 : chl 285 bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad);
1397 : Isibaar 3
1398 : edgomez 195 if ((x == 0) && (y == 0)) {
1399 :     threshA = 512;
1400 : Isibaar 3 threshB = 1024;
1401 : edgomez 195 } else {
1402 : Isibaar 3 threshA = psad[0];
1403 : edgomez 195 threshB = threshA + 256;
1404 :     if (threshA < 512)
1405 :     threshA = 512;
1406 :     if (threshA > 1024)
1407 :     threshA = 1024;
1408 :     if (threshB > 1792)
1409 :     threshB = 1792;
1410 : Isibaar 3 }
1411 :    
1412 : edgomez 195 iFound = 0;
1413 :    
1414 : Isibaar 3 /* Step 4: Calculate SAD around the Median prediction.
1415 : edgomez 78 MinSAD=SAD
1416 :     If Motion Vector equal to Previous frame motion vector
1417 :     and MinSAD<PrevFrmSAD goto Step 10.
1418 :     If SAD<=256 goto Step 10.
1419 : edgomez 195 */
1420 : Isibaar 3
1421 : chl 326 currMV->x = start_x;
1422 :     currMV->y = start_y;
1423 :    
1424 : edgomez 195 if (!(MotionFlags & PMV_HALFPEL16)) { /* This should NOT be necessary! */
1425 : Isibaar 3 currMV->x = EVEN(currMV->x);
1426 :     currMV->y = EVEN(currMV->y);
1427 :     }
1428 : edgomez 195
1429 :     if (currMV->x > max_dx) {
1430 :     currMV->x = max_dx;
1431 : edgomez 78 }
1432 : edgomez 195 if (currMV->x < min_dx) {
1433 :     currMV->x = min_dx;
1434 : edgomez 78 }
1435 : edgomez 195 if (currMV->y > max_dy) {
1436 :     currMV->y = max_dy;
1437 : edgomez 78 }
1438 : edgomez 195 if (currMV->y < min_dy) {
1439 :     currMV->y = min_dy;
1440 : edgomez 78 }
1441 : edgomez 195
1442 :     iMinSAD =
1443 :     sad16(cur,
1444 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV,
1445 :     iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);
1446 :     iMinSAD +=
1447 : chl 326 calc_delta_16(currMV->x - center_x, currMV->y - center_y,
1448 : edgomez 195 (uint8_t) iFcode, iQuant);
1449 :    
1450 :     if ((iMinSAD < 256) ||
1451 :     ((MVequal(*currMV, prevMB->mvs[0])) &&
1452 : chl 289 ((int32_t) iMinSAD < prevMB->sad16))) {
1453 : edgomez 476 if (iMinSAD < (int)(2 * iQuant)) // high chances for SKIP-mode
1454 : edgomez 195 {
1455 :     if (!MVzero(*currMV)) {
1456 : chl 169 iMinSAD += MV16_00_BIAS;
1457 : edgomez 195 CHECK_MV16_ZERO; // (0,0) saves space for letterboxed pictures
1458 : chl 169 iMinSAD -= MV16_00_BIAS;
1459 :     }
1460 :     }
1461 :    
1462 : edgomez 195 if (MotionFlags & PMV_QUICKSTOP16)
1463 : chl 96 goto PMVfast16_Terminate_without_Refine;
1464 :     if (MotionFlags & PMV_EARLYSTOP16)
1465 :     goto PMVfast16_Terminate_with_Refine;
1466 : edgomez 78 }
1467 : Isibaar 3
1468 : chl 169
1469 :     /* Step 2 (lazy eval): Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion
1470 :     vector of the median.
1471 :     If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2
1472 :     */
1473 :    
1474 : edgomez 195 if ((bPredEq) && (MVequal(pmv[0], prevMB->mvs[0])))
1475 :     iFound = 2;
1476 : chl 169
1477 :     /* Step 3 (lazy eval): If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search.
1478 :     Otherwise select large Diamond Search.
1479 :     */
1480 :    
1481 : edgomez 195 if ((!MVzero(pmv[0])) || (threshB < 1536) || (bPredEq))
1482 :     iDiamondSize = 1; // halfpel!
1483 : chl 169 else
1484 : edgomez 195 iDiamondSize = 2; // halfpel!
1485 : chl 169
1486 : edgomez 195 if (!(MotionFlags & PMV_HALFPELDIAMOND16))
1487 :     iDiamondSize *= 2;
1488 : chl 169
1489 : Isibaar 3 /*
1490 : edgomez 78 Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block.
1491 :     Also calculate (0,0) but do not subtract offset.
1492 :     Let MinSAD be the smallest SAD up to this point.
1493 : chl 140 If MV is (0,0) subtract offset.
1494 : Isibaar 3 */
1495 :    
1496 :     // (0,0) is always possible
1497 :    
1498 : chl 169 if (!MVzero(pmv[0]))
1499 :     CHECK_MV16_ZERO;
1500 : Isibaar 3
1501 :     // previous frame MV is always possible
1502 : chl 169
1503 :     if (!MVzero(prevMB->mvs[0]))
1504 : edgomez 195 if (!MVequal(prevMB->mvs[0], pmv[0]))
1505 :     CHECK_MV16_CANDIDATE(prevMB->mvs[0].x, prevMB->mvs[0].y);
1506 :    
1507 : Isibaar 3 // left neighbour, if allowed
1508 : chl 169
1509 :     if (!MVzero(pmv[1]))
1510 : edgomez 195 if (!MVequal(pmv[1], prevMB->mvs[0]))
1511 :     if (!MVequal(pmv[1], pmv[0])) {
1512 :     if (!(MotionFlags & PMV_HALFPEL16)) {
1513 :     pmv[1].x = EVEN(pmv[1].x);
1514 :     pmv[1].y = EVEN(pmv[1].y);
1515 :     }
1516 : chl 169
1517 : edgomez 195 CHECK_MV16_CANDIDATE(pmv[1].x, pmv[1].y);
1518 :     }
1519 : Isibaar 3 // top neighbour, if allowed
1520 : chl 169 if (!MVzero(pmv[2]))
1521 : edgomez 195 if (!MVequal(pmv[2], prevMB->mvs[0]))
1522 :     if (!MVequal(pmv[2], pmv[0]))
1523 :     if (!MVequal(pmv[2], pmv[1])) {
1524 :     if (!(MotionFlags & PMV_HALFPEL16)) {
1525 :     pmv[2].x = EVEN(pmv[2].x);
1526 :     pmv[2].y = EVEN(pmv[2].y);
1527 :     }
1528 :     CHECK_MV16_CANDIDATE(pmv[2].x, pmv[2].y);
1529 :    
1530 : Isibaar 3 // top right neighbour, if allowed
1531 : edgomez 195 if (!MVzero(pmv[3]))
1532 :     if (!MVequal(pmv[3], prevMB->mvs[0]))
1533 :     if (!MVequal(pmv[3], pmv[0]))
1534 :     if (!MVequal(pmv[3], pmv[1]))
1535 :     if (!MVequal(pmv[3], pmv[2])) {
1536 :     if (!(MotionFlags & PMV_HALFPEL16)) {
1537 :     pmv[3].x = EVEN(pmv[3].x);
1538 :     pmv[3].y = EVEN(pmv[3].y);
1539 :     }
1540 :     CHECK_MV16_CANDIDATE(pmv[3].x,
1541 :     pmv[3].y);
1542 :     }
1543 :     }
1544 :    
1545 :     if ((MVzero(*currMV)) &&
1546 :     (!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96) */ )
1547 : chl 140 iMinSAD -= MV16_00_BIAS;
1548 : Isibaar 3
1549 : edgomez 195
1550 : Isibaar 3 /* Step 6: If MinSAD <= thresa goto Step 10.
1551 :     If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10.
1552 :     */
1553 :    
1554 : edgomez 195 if ((iMinSAD <= threshA) ||
1555 :     (MVequal(*currMV, prevMB->mvs[0]) &&
1556 : chl 289 ((int32_t) iMinSAD < prevMB->sad16))) {
1557 : edgomez 195 if (MotionFlags & PMV_QUICKSTOP16)
1558 : chl 96 goto PMVfast16_Terminate_without_Refine;
1559 :     if (MotionFlags & PMV_EARLYSTOP16)
1560 :     goto PMVfast16_Terminate_with_Refine;
1561 : edgomez 78 }
1562 : Isibaar 3
1563 :    
1564 :     /************ (Diamond Search) **************/
1565 :     /*
1566 : edgomez 78 Step 7: Perform Diamond search, with either the small or large diamond.
1567 :     If Found=2 only examine one Diamond pattern, and afterwards goto step 10
1568 :     Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond.
1569 :     If center then goto step 10.
1570 :     Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center.
1571 :     Refine by using small diamond and goto step 10.
1572 : Isibaar 3 */
1573 :    
1574 : chl 181 if (MotionFlags & PMV_USESQUARES16)
1575 :     MainSearchPtr = Square16_MainSearch;
1576 : edgomez 195 else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
1577 :     MainSearchPtr = AdvDiamond16_MainSearch;
1578 : chl 181 else
1579 : edgomez 195 MainSearchPtr = Diamond16_MainSearch;
1580 : chl 181
1581 : edgomez 195 backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
1582 : Isibaar 3
1583 : chl 259
1584 : Isibaar 3 /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
1585 : edgomez 195 iSAD =
1586 : chl 326 (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
1587 :     currMV->x, currMV->y, iMinSAD, &newMV, center_x, center_y,
1588 :     min_dx, max_dx,
1589 : edgomez 195 min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
1590 :     iQuant, iFound);
1591 :    
1592 :     if (iSAD < iMinSAD) {
1593 : Isibaar 3 *currMV = newMV;
1594 :     iMinSAD = iSAD;
1595 :     }
1596 :    
1597 : edgomez 195 if (MotionFlags & PMV_EXTSEARCH16) {
1598 : Isibaar 3 /* extended: search (up to) two more times: orignal prediction and (0,0) */
1599 :    
1600 : edgomez 195 if (!(MVequal(pmv[0], backupMV))) {
1601 :     iSAD =
1602 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
1603 : chl 326 center_x, center_y, iMinSAD, &newMV, center_x, center_y,
1604 : edgomez 195 min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
1605 :     iDiamondSize, iFcode, iQuant, iFound);
1606 :    
1607 :     if (iSAD < iMinSAD) {
1608 :     *currMV = newMV;
1609 :     iMinSAD = iSAD;
1610 :     }
1611 : Isibaar 3 }
1612 :    
1613 : edgomez 195 if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
1614 :     iSAD =
1615 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
1616 : chl 326 iMinSAD, &newMV, center_x, center_y,
1617 :     min_dx, max_dx, min_dy, max_dy,
1618 :     iEdgedWidth, iDiamondSize, iFcode,
1619 : edgomez 195 iQuant, iFound);
1620 :    
1621 :     if (iSAD < iMinSAD) {
1622 :     *currMV = newMV;
1623 :     iMinSAD = iSAD;
1624 :     }
1625 : Isibaar 3 }
1626 :     }
1627 :    
1628 :     /*
1629 : edgomez 78 Step 10: The motion vector is chosen according to the block corresponding to MinSAD.
1630 : Isibaar 3 */
1631 :    
1632 : edgomez 195 PMVfast16_Terminate_with_Refine:
1633 :     if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step
1634 :     iMinSAD =
1635 :     Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
1636 : chl 326 iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
1637 : edgomez 195 iFcode, iQuant, iEdgedWidth);
1638 : Isibaar 3
1639 : edgomez 195 PMVfast16_Terminate_without_Refine:
1640 : chl 326 currPMV->x = currMV->x - center_x;
1641 :     currPMV->y = currMV->y - center_y;
1642 : Isibaar 3 return iMinSAD;
1643 :     }
1644 :    
1645 :    
1646 :    
1647 :    
1648 :    
1649 :    
1650 : edgomez 195 int32_t
1651 :     Diamond8_MainSearch(const uint8_t * const pRef,
1652 :     const uint8_t * const pRefH,
1653 :     const uint8_t * const pRefV,
1654 :     const uint8_t * const pRefHV,
1655 :     const uint8_t * const cur,
1656 :     const int x,
1657 :     const int y,
1658 : edgomez 476 const int32_t start_x,
1659 :     const int32_t start_y,
1660 : edgomez 195 int32_t iMinSAD,
1661 :     VECTOR * const currMV,
1662 : edgomez 476 const int center_x,
1663 :     const int center_y,
1664 : edgomez 195 const int32_t min_dx,
1665 :     const int32_t max_dx,
1666 :     const int32_t min_dy,
1667 :     const int32_t max_dy,
1668 :     const int32_t iEdgedWidth,
1669 :     const int32_t iDiamondSize,
1670 :     const int32_t iFcode,
1671 :     const int32_t iQuant,
1672 :     int iFound)
1673 : Isibaar 3 {
1674 :     /* Do a diamond search around given starting point, return SAD of best */
1675 :    
1676 : edgomez 195 int32_t iDirection = 0;
1677 : chl 344 int32_t iDirectionBackup;
1678 : Isibaar 3 int32_t iSAD;
1679 :     VECTOR backupMV;
1680 : edgomez 195
1681 : chl 326 backupMV.x = start_x;
1682 :     backupMV.y = start_y;
1683 : edgomez 195
1684 : Isibaar 3 /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
1685 :    
1686 : edgomez 195 CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
1687 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
1688 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
1689 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
1690 : Isibaar 3
1691 : chl 344 if (iDirection) {
1692 : edgomez 195 while (!iFound) {
1693 :     iFound = 1;
1694 :     backupMV = *currMV; // since iDirection!=0, this is well defined!
1695 : chl 344 iDirectionBackup = iDirection;
1696 : edgomez 195
1697 : chl 344 if (iDirectionBackup != 2)
1698 : edgomez 195 CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1699 :     backupMV.y, 1);
1700 : chl 344 if (iDirectionBackup != 1)
1701 : edgomez 195 CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1702 :     backupMV.y, 2);
1703 : chl 344 if (iDirectionBackup != 4)
1704 : edgomez 195 CHECK_MV8_CANDIDATE_FOUND(backupMV.x,
1705 :     backupMV.y - iDiamondSize, 3);
1706 : chl 344 if (iDirectionBackup != 3)
1707 : edgomez 195 CHECK_MV8_CANDIDATE_FOUND(backupMV.x,
1708 :     backupMV.y + iDiamondSize, 4);
1709 : chl 344 }
1710 : edgomez 195 } else {
1711 : chl 326 currMV->x = start_x;
1712 :     currMV->y = start_y;
1713 : edgomez 78 }
1714 : Isibaar 3 return iMinSAD;
1715 :     }
1716 :    
1717 : chl 345
1718 :    
1719 :    
1720 : edgomez 195 int32_t
1721 : chl 345 Square8_MainSearch(const uint8_t * const pRef,
1722 : edgomez 476 const uint8_t * const pRefH,
1723 :     const uint8_t * const pRefV,
1724 :     const uint8_t * const pRefHV,
1725 :     const uint8_t * const cur,
1726 :     const int x,
1727 :     const int y,
1728 :     const int32_t start_x,
1729 :     const int32_t start_y,
1730 :     int32_t iMinSAD,
1731 :     VECTOR * const currMV,
1732 : chl 345 const int center_x,
1733 :     const int center_y,
1734 : edgomez 476 const int32_t min_dx,
1735 :     const int32_t max_dx,
1736 :     const int32_t min_dy,
1737 :     const int32_t max_dy,
1738 :     const int32_t iEdgedWidth,
1739 :     const int32_t iDiamondSize,
1740 :     const int32_t iFcode,
1741 :     const int32_t iQuant,
1742 :     int iFound)
1743 : chl 345 {
1744 :     /* Do a square search around given starting point, return SAD of best */
1745 :    
1746 :     int32_t iDirection = 0;
1747 :     int32_t iSAD;
1748 :     VECTOR backupMV;
1749 :    
1750 :     backupMV.x = start_x;
1751 :     backupMV.y = start_y;
1752 :    
1753 :     /* It's one search with full square pattern, and new parts for all following diamonds */
1754 :    
1755 :     /* new direction are extra, so 1-4 is normal diamond
1756 :     537
1757 :     1*2
1758 :     648
1759 :     */
1760 :    
1761 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
1762 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
1763 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
1764 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
1765 :    
1766 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize,
1767 :     backupMV.y - iDiamondSize, 5);
1768 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize,
1769 :     backupMV.y + iDiamondSize, 6);
1770 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize,
1771 :     backupMV.y - iDiamondSize, 7);
1772 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize,
1773 :     backupMV.y + iDiamondSize, 8);
1774 :    
1775 :    
1776 :     if (iDirection) {
1777 :     while (!iFound) {
1778 :     iFound = 1;
1779 :     backupMV = *currMV;
1780 :    
1781 :     switch (iDirection) {
1782 :     case 1:
1783 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1784 :     backupMV.y, 1);
1785 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1786 :     backupMV.y - iDiamondSize, 5);
1787 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1788 :     backupMV.y - iDiamondSize, 7);
1789 :     break;
1790 :     case 2:
1791 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1792 :     2);
1793 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1794 :     backupMV.y + iDiamondSize, 6);
1795 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1796 :     backupMV.y + iDiamondSize, 8);
1797 :     break;
1798 :    
1799 :     case 3:
1800 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1801 :     4);
1802 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1803 :     backupMV.y - iDiamondSize, 7);
1804 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1805 :     backupMV.y + iDiamondSize, 8);
1806 :     break;
1807 :    
1808 :     case 4:
1809 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1810 :     3);
1811 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1812 :     backupMV.y - iDiamondSize, 5);
1813 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1814 :     backupMV.y + iDiamondSize, 6);
1815 :     break;
1816 :    
1817 :     case 5:
1818 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
1819 :     1);
1820 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1821 :     3);
1822 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1823 :     backupMV.y - iDiamondSize, 5);
1824 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1825 :     backupMV.y + iDiamondSize, 6);
1826 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1827 :     backupMV.y - iDiamondSize, 7);
1828 :     break;
1829 :    
1830 :     case 6:
1831 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1832 :     2);
1833 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1834 :     3);
1835 :    
1836 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1837 :     backupMV.y - iDiamondSize, 5);
1838 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1839 :     backupMV.y + iDiamondSize, 6);
1840 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1841 :     backupMV.y + iDiamondSize, 8);
1842 :    
1843 :     break;
1844 :    
1845 :     case 7:
1846 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1847 :     backupMV.y, 1);
1848 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1849 :     4);
1850 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1851 :     backupMV.y - iDiamondSize, 5);
1852 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1853 :     backupMV.y - iDiamondSize, 7);
1854 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1855 :     backupMV.y + iDiamondSize, 8);
1856 :     break;
1857 :    
1858 :     case 8:
1859 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1860 :     2);
1861 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1862 :     4);
1863 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1864 :     backupMV.y + iDiamondSize, 6);
1865 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1866 :     backupMV.y - iDiamondSize, 7);
1867 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1868 :     backupMV.y + iDiamondSize, 8);
1869 :     break;
1870 :     default:
1871 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
1872 :     1);
1873 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1874 :     2);
1875 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1876 :     3);
1877 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1878 :     4);
1879 :    
1880 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1881 :     backupMV.y - iDiamondSize, 5);
1882 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1883 :     backupMV.y + iDiamondSize, 6);
1884 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1885 :     backupMV.y - iDiamondSize, 7);
1886 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1887 :     backupMV.y + iDiamondSize, 8);
1888 :     break;
1889 :     }
1890 :     }
1891 :     } else {
1892 :     currMV->x = start_x;
1893 :     currMV->y = start_y;
1894 :     }
1895 :     return iMinSAD;
1896 :     }
1897 :    
1898 :    
1899 :    
1900 :    
1901 :    
1902 :     int32_t
1903 : ia64p 300 Halfpel8_Refine_c(const uint8_t * const pRef,
1904 : edgomez 476 const uint8_t * const pRefH,
1905 :     const uint8_t * const pRefV,
1906 :     const uint8_t * const pRefHV,
1907 :     const uint8_t * const cur,
1908 :     const int x,
1909 :     const int y,
1910 :     VECTOR * const currMV,
1911 :     int32_t iMinSAD,
1912 :     const int center_x,
1913 :     const int center_y,
1914 :     const int32_t min_dx,
1915 :     const int32_t max_dx,
1916 :     const int32_t min_dy,
1917 :     const int32_t max_dy,
1918 :     const int32_t iFcode,
1919 :     const int32_t iQuant,
1920 :     const int32_t iEdgedWidth)
1921 : Isibaar 3 {
1922 :     /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */
1923 :    
1924 :     int32_t iSAD;
1925 :     VECTOR backupMV = *currMV;
1926 : edgomez 195
1927 :     CHECK_MV8_CANDIDATE(backupMV.x - 1, backupMV.y - 1);
1928 :     CHECK_MV8_CANDIDATE(backupMV.x, backupMV.y - 1);
1929 :     CHECK_MV8_CANDIDATE(backupMV.x + 1, backupMV.y - 1);
1930 :     CHECK_MV8_CANDIDATE(backupMV.x - 1, backupMV.y);
1931 :     CHECK_MV8_CANDIDATE(backupMV.x + 1, backupMV.y);
1932 :     CHECK_MV8_CANDIDATE(backupMV.x - 1, backupMV.y + 1);
1933 :     CHECK_MV8_CANDIDATE(backupMV.x, backupMV.y + 1);
1934 :     CHECK_MV8_CANDIDATE(backupMV.x + 1, backupMV.y + 1);
1935 :    
1936 : Isibaar 3 return iMinSAD;
1937 :     }
1938 :    
1939 :    
1940 :     #define PMV_HALFPEL8 (PMV_HALFPELDIAMOND8|PMV_HALFPELREFINE8)
1941 :    
1942 : edgomez 195 int32_t
1943 :     PMVfastSearch8(const uint8_t * const pRef,
1944 :     const uint8_t * const pRefH,
1945 :     const uint8_t * const pRefV,
1946 :     const uint8_t * const pRefHV,
1947 :     const IMAGE * const pCur,
1948 :     const int x,
1949 :     const int y,
1950 :     const int start_x,
1951 :     const int start_y,
1952 : chl 326 const int center_x,
1953 :     const int center_y,
1954 : edgomez 195 const uint32_t MotionFlags,
1955 :     const uint32_t iQuant,
1956 :     const uint32_t iFcode,
1957 :     const MBParam * const pParam,
1958 :     const MACROBLOCK * const pMBs,
1959 :     const MACROBLOCK * const prevMBs,
1960 :     VECTOR * const currMV,
1961 :     VECTOR * const currPMV)
1962 : Isibaar 3 {
1963 : edgomez 195 const uint32_t iWcount = pParam->mb_width;
1964 : Isibaar 3 const int32_t iWidth = pParam->width;
1965 :     const int32_t iHeight = pParam->height;
1966 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
1967 : Isibaar 3
1968 : edgomez 195 const uint8_t *cur = pCur->y + x * 8 + y * 8 * iEdgedWidth;
1969 : Isibaar 3
1970 :     int32_t iDiamondSize;
1971 :    
1972 :     int32_t min_dx;
1973 :     int32_t max_dx;
1974 :     int32_t min_dy;
1975 :     int32_t max_dy;
1976 : edgomez 195
1977 : Isibaar 3 VECTOR pmv[4];
1978 :     int32_t psad[4];
1979 :     VECTOR newMV;
1980 :     VECTOR backupMV;
1981 : edgomez 170 VECTOR startMV;
1982 : Isibaar 3
1983 : edgomez 195 // const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount;
1984 :     const MACROBLOCK *const prevMB = prevMBs + (x >> 1) + (y >> 1) * iWcount;
1985 : Isibaar 3
1986 : chl 259 int32_t threshA, threshB;
1987 : edgomez 195 int32_t iFound, bPredEq;
1988 :     int32_t iMinSAD, iSAD;
1989 : Isibaar 3
1990 : edgomez 195 int32_t iSubBlock = (y & 1) + (y & 1) + (x & 1);
1991 :    
1992 : chl 181 MainSearch8FuncPtr MainSearchPtr;
1993 :    
1994 : edgomez 170 /* Init variables */
1995 :     startMV.x = start_x;
1996 :     startMV.y = start_y;
1997 :    
1998 :     /* Get maximum range */
1999 : edgomez 195 get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 8, iWidth, iHeight,
2000 :     iFcode);
2001 : Isibaar 3
2002 : edgomez 195 if (!(MotionFlags & PMV_HALFPELDIAMOND8)) {
2003 :     min_dx = EVEN(min_dx);
2004 :     max_dx = EVEN(max_dx);
2005 :     min_dy = EVEN(min_dy);
2006 :     max_dy = EVEN(max_dy);
2007 :     }
2008 : Isibaar 3
2009 : edgomez 195 /* because we might use IF (dx>max_dx) THEN dx=max_dx; */
2010 : chl 285 bPredEq = get_pmvdata2(pMBs, iWcount, 0, (x >> 1), (y >> 1), iSubBlock, pmv, psad);
2011 : Isibaar 3
2012 : edgomez 195 if ((x == 0) && (y == 0)) {
2013 :     threshA = 512 / 4;
2014 :     threshB = 1024 / 4;
2015 :    
2016 :     } else {
2017 : chl 326 threshA = psad[0] / 4; /* good estimate? */
2018 : edgomez 195 threshB = threshA + 256 / 4;
2019 :     if (threshA < 512 / 4)
2020 :     threshA = 512 / 4;
2021 :     if (threshA > 1024 / 4)
2022 :     threshA = 1024 / 4;
2023 :     if (threshB > 1792 / 4)
2024 :     threshB = 1792 / 4;
2025 : Isibaar 3 }
2026 :    
2027 : edgomez 195 iFound = 0;
2028 :    
2029 : Isibaar 3 /* Step 4: Calculate SAD around the Median prediction.
2030 : edgomez 78 MinSAD=SAD
2031 :     If Motion Vector equal to Previous frame motion vector
2032 :     and MinSAD<PrevFrmSAD goto Step 10.
2033 :     If SAD<=256 goto Step 10.
2034 : edgomez 195 */
2035 : Isibaar 3
2036 :    
2037 :     // Prepare for main loop
2038 :    
2039 : chl 345 if (MotionFlags & PMV_USESQUARES8)
2040 :     MainSearchPtr = Square8_MainSearch;
2041 :     else
2042 : chl 181
2043 :     if (MotionFlags & PMV_ADVANCEDDIAMOND8)
2044 :     MainSearchPtr = AdvDiamond8_MainSearch;
2045 :     else
2046 :     MainSearchPtr = Diamond8_MainSearch;
2047 :    
2048 :    
2049 : chl 169 *currMV = startMV;
2050 : edgomez 195
2051 :     iMinSAD =
2052 :     sad8(cur,
2053 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV,
2054 :     iEdgedWidth), iEdgedWidth);
2055 :     iMinSAD +=
2056 : chl 326 calc_delta_8(currMV->x - center_x, currMV->y - center_y,
2057 : edgomez 195 (uint8_t) iFcode, iQuant);
2058 :    
2059 :     if ((iMinSAD < 256 / 4) || ((MVequal(*currMV, prevMB->mvs[iSubBlock]))
2060 : chl 289 && ((int32_t) iMinSAD <
2061 : edgomez 195 prevMB->sad8[iSubBlock]))) {
2062 :     if (MotionFlags & PMV_QUICKSTOP16)
2063 : chl 96 goto PMVfast8_Terminate_without_Refine;
2064 :     if (MotionFlags & PMV_EARLYSTOP16)
2065 :     goto PMVfast8_Terminate_with_Refine;
2066 : edgomez 78 }
2067 : Isibaar 3
2068 : chl 169 /* Step 2 (lazy eval): Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion
2069 :     vector of the median.
2070 :     If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2
2071 :     */
2072 : chl 96
2073 : edgomez 195 if ((bPredEq) && (MVequal(pmv[0], prevMB->mvs[iSubBlock])))
2074 :     iFound = 2;
2075 : chl 169
2076 :     /* Step 3 (lazy eval): If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search.
2077 :     Otherwise select large Diamond Search.
2078 :     */
2079 :    
2080 : edgomez 195 if ((!MVzero(pmv[0])) || (threshB < 1536 / 4) || (bPredEq))
2081 :     iDiamondSize = 1; // 1 halfpel!
2082 : chl 169 else
2083 : edgomez 195 iDiamondSize = 2; // 2 halfpel = 1 full pixel!
2084 : chl 169
2085 : edgomez 195 if (!(MotionFlags & PMV_HALFPELDIAMOND8))
2086 :     iDiamondSize *= 2;
2087 : chl 169
2088 :    
2089 : Isibaar 3 /*
2090 : edgomez 78 Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block.
2091 :     Also calculate (0,0) but do not subtract offset.
2092 :     Let MinSAD be the smallest SAD up to this point.
2093 : chl 140 If MV is (0,0) subtract offset.
2094 : Isibaar 3 */
2095 :    
2096 : chl 169 // the median prediction might be even better than mv16
2097 : Isibaar 3
2098 : edgomez 195 if (!MVequal(pmv[0], startMV))
2099 : chl 326 CHECK_MV8_CANDIDATE(center_x, center_y);
2100 : chl 169
2101 :     // (0,0) if needed
2102 :     if (!MVzero(pmv[0]))
2103 : edgomez 195 if (!MVzero(startMV))
2104 :     CHECK_MV8_ZERO;
2105 : Isibaar 3
2106 : chl 169 // previous frame MV if needed
2107 :     if (!MVzero(prevMB->mvs[iSubBlock]))
2108 : edgomez 195 if (!MVequal(prevMB->mvs[iSubBlock], startMV))
2109 :     if (!MVequal(prevMB->mvs[iSubBlock], pmv[0]))
2110 :     CHECK_MV8_CANDIDATE(prevMB->mvs[iSubBlock].x,
2111 :     prevMB->mvs[iSubBlock].y);
2112 : chl 169
2113 : edgomez 195 if ((iMinSAD <= threshA) ||
2114 :     (MVequal(*currMV, prevMB->mvs[iSubBlock]) &&
2115 : chl 289 ((int32_t) iMinSAD < prevMB->sad8[iSubBlock]))) {
2116 : edgomez 195 if (MotionFlags & PMV_QUICKSTOP16)
2117 : chl 169 goto PMVfast8_Terminate_without_Refine;
2118 :     if (MotionFlags & PMV_EARLYSTOP16)
2119 :     goto PMVfast8_Terminate_with_Refine;
2120 :     }
2121 :    
2122 :     // left neighbour, if allowed and needed
2123 :     if (!MVzero(pmv[1]))
2124 : edgomez 195 if (!MVequal(pmv[1], startMV))
2125 :     if (!MVequal(pmv[1], prevMB->mvs[iSubBlock]))
2126 :     if (!MVequal(pmv[1], pmv[0])) {
2127 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2128 :     pmv[1].x = EVEN(pmv[1].x);
2129 :     pmv[1].y = EVEN(pmv[1].y);
2130 :     }
2131 :     CHECK_MV8_CANDIDATE(pmv[1].x, pmv[1].y);
2132 :     }
2133 : chl 169 // top neighbour, if allowed and needed
2134 :     if (!MVzero(pmv[2]))
2135 : edgomez 195 if (!MVequal(pmv[2], startMV))
2136 :     if (!MVequal(pmv[2], prevMB->mvs[iSubBlock]))
2137 :     if (!MVequal(pmv[2], pmv[0]))
2138 :     if (!MVequal(pmv[2], pmv[1])) {
2139 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2140 :     pmv[2].x = EVEN(pmv[2].x);
2141 :     pmv[2].y = EVEN(pmv[2].y);
2142 :     }
2143 :     CHECK_MV8_CANDIDATE(pmv[2].x, pmv[2].y);
2144 :    
2145 : chl 169 // top right neighbour, if allowed and needed
2146 : edgomez 195 if (!MVzero(pmv[3]))
2147 :     if (!MVequal(pmv[3], startMV))
2148 :     if (!MVequal(pmv[3], prevMB->mvs[iSubBlock]))
2149 :     if (!MVequal(pmv[3], pmv[0]))
2150 :     if (!MVequal(pmv[3], pmv[1]))
2151 :     if (!MVequal(pmv[3], pmv[2])) {
2152 :     if (!
2153 :     (MotionFlags &
2154 :     PMV_HALFPEL8)) {
2155 :     pmv[3].x = EVEN(pmv[3].x);
2156 :     pmv[3].y = EVEN(pmv[3].y);
2157 :     }
2158 :     CHECK_MV8_CANDIDATE(pmv[3].x,
2159 :     pmv[3].y);
2160 :     }
2161 :     }
2162 : Isibaar 3
2163 : edgomez 195 if ((MVzero(*currMV)) &&
2164 :     (!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96) */ )
2165 : chl 140 iMinSAD -= MV8_00_BIAS;
2166 :    
2167 :    
2168 : Isibaar 3 /* Step 6: If MinSAD <= thresa goto Step 10.
2169 :     If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10.
2170 :     */
2171 :    
2172 : edgomez 195 if ((iMinSAD <= threshA) ||
2173 :     (MVequal(*currMV, prevMB->mvs[iSubBlock]) &&
2174 : chl 289 ((int32_t) iMinSAD < prevMB->sad8[iSubBlock]))) {
2175 : edgomez 195 if (MotionFlags & PMV_QUICKSTOP16)
2176 : chl 96 goto PMVfast8_Terminate_without_Refine;
2177 :     if (MotionFlags & PMV_EARLYSTOP16)
2178 :     goto PMVfast8_Terminate_with_Refine;
2179 : edgomez 78 }
2180 : Isibaar 3
2181 :     /************ (Diamond Search) **************/
2182 :     /*
2183 : edgomez 78 Step 7: Perform Diamond search, with either the small or large diamond.
2184 :     If Found=2 only examine one Diamond pattern, and afterwards goto step 10
2185 :     Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond.
2186 :     If center then goto step 10.
2187 :     Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center.
2188 :     Refine by using small diamond and goto step 10.
2189 : Isibaar 3 */
2190 :    
2191 : edgomez 195 backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
2192 : Isibaar 3
2193 :     /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
2194 : edgomez 195 iSAD =
2195 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
2196 : chl 326 currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
2197 : edgomez 195 min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
2198 :     iQuant, iFound);
2199 :    
2200 :     if (iSAD < iMinSAD) {
2201 : Isibaar 3 *currMV = newMV;
2202 :     iMinSAD = iSAD;
2203 :     }
2204 :    
2205 : edgomez 195 if (MotionFlags & PMV_EXTSEARCH8) {
2206 : Isibaar 3 /* extended: search (up to) two more times: orignal prediction and (0,0) */
2207 :    
2208 : edgomez 195 if (!(MVequal(pmv[0], backupMV))) {
2209 :     iSAD =
2210 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
2211 : chl 326 pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
2212 : edgomez 195 min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
2213 :     iDiamondSize, iFcode, iQuant, iFound);
2214 :    
2215 :     if (iSAD < iMinSAD) {
2216 :     *currMV = newMV;
2217 :     iMinSAD = iSAD;
2218 :     }
2219 : Isibaar 3 }
2220 :    
2221 : edgomez 195 if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2222 :     iSAD =
2223 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2224 : chl 326 iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
2225 : edgomez 195 max_dy, iEdgedWidth, iDiamondSize, iFcode,
2226 :     iQuant, iFound);
2227 :    
2228 :     if (iSAD < iMinSAD) {
2229 :     *currMV = newMV;
2230 :     iMinSAD = iSAD;
2231 :     }
2232 : Isibaar 3 }
2233 :     }
2234 :    
2235 :     /* Step 10: The motion vector is chosen according to the block corresponding to MinSAD.
2236 : edgomez 78 By performing an optional local half-pixel search, we can refine this result even further.
2237 : Isibaar 3 */
2238 :    
2239 : edgomez 195 PMVfast8_Terminate_with_Refine:
2240 :     if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step
2241 :     iMinSAD =
2242 :     Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2243 : chl 326 iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
2244 : edgomez 195 iFcode, iQuant, iEdgedWidth);
2245 : Isibaar 3
2246 : edgomez 195
2247 :     PMVfast8_Terminate_without_Refine:
2248 : chl 326 currPMV->x = currMV->x - center_x;
2249 :     currPMV->y = currMV->y - center_y;
2250 : edgomez 195
2251 : Isibaar 3 return iMinSAD;
2252 :     }
2253 : chl 96
2254 : edgomez 195 int32_t
2255 :     EPZSSearch16(const uint8_t * const pRef,
2256 :     const uint8_t * const pRefH,
2257 :     const uint8_t * const pRefV,
2258 :     const uint8_t * const pRefHV,
2259 :     const IMAGE * const pCur,
2260 :     const int x,
2261 :     const int y,
2262 : chl 326 const int start_x,
2263 :     const int start_y,
2264 :     const int center_x,
2265 :     const int center_y,
2266 : edgomez 195 const uint32_t MotionFlags,
2267 :     const uint32_t iQuant,
2268 :     const uint32_t iFcode,
2269 :     const MBParam * const pParam,
2270 :     const MACROBLOCK * const pMBs,
2271 :     const MACROBLOCK * const prevMBs,
2272 :     VECTOR * const currMV,
2273 :     VECTOR * const currPMV)
2274 : chl 96 {
2275 : edgomez 195 const uint32_t iWcount = pParam->mb_width;
2276 :     const uint32_t iHcount = pParam->mb_height;
2277 : chl 96
2278 :     const int32_t iWidth = pParam->width;
2279 :     const int32_t iHeight = pParam->height;
2280 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
2281 : chl 96
2282 : edgomez 195 const uint8_t *cur = pCur->y + x * 16 + y * 16 * iEdgedWidth;
2283 : chl 96
2284 :     int32_t min_dx;
2285 :     int32_t max_dx;
2286 :     int32_t min_dy;
2287 :     int32_t max_dy;
2288 : edgomez 195
2289 : chl 96 VECTOR newMV;
2290 :     VECTOR backupMV;
2291 : edgomez 195
2292 : chl 96 VECTOR pmv[4];
2293 :     int32_t psad[8];
2294 :    
2295 : edgomez 195 static MACROBLOCK *oldMBs = NULL;
2296 :    
2297 :     // const MACROBLOCK * const pMB = pMBs + x + y * iWcount;
2298 :     const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;
2299 :     MACROBLOCK *oldMB = NULL;
2300 :    
2301 : chl 259 int32_t thresh2;
2302 : edgomez 195 int32_t bPredEq;
2303 :     int32_t iMinSAD, iSAD = 9999;
2304 : chl 96
2305 : chl 181 MainSearch16FuncPtr MainSearchPtr;
2306 : chl 96
2307 : edgomez 195 if (oldMBs == NULL) {
2308 :     oldMBs = (MACROBLOCK *) calloc(iWcount * iHcount, sizeof(MACROBLOCK));
2309 :     // fprintf(stderr,"allocated %d bytes for oldMBs\n",iWcount*iHcount*sizeof(MACROBLOCK));
2310 : chl 96 }
2311 :     oldMB = oldMBs + x + y * iWcount;
2312 :    
2313 :     /* Get maximum range */
2314 : edgomez 195 get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight,
2315 :     iFcode);
2316 : chl 96
2317 : edgomez 195 if (!(MotionFlags & PMV_HALFPEL16)) {
2318 :     min_dx = EVEN(min_dx);
2319 :     max_dx = EVEN(max_dx);
2320 :     min_dy = EVEN(min_dy);
2321 :     max_dy = EVEN(max_dy);
2322 :     }
2323 :     /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
2324 : chl 285 bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad);
2325 : chl 96
2326 :     /* Step 4: Calculate SAD around the Median prediction.
2327 :     MinSAD=SAD
2328 :     If Motion Vector equal to Previous frame motion vector
2329 :     and MinSAD<PrevFrmSAD goto Step 10.
2330 :     If SAD<=256 goto Step 10.
2331 : edgomez 195 */
2332 : chl 96
2333 :     // Prepare for main loop
2334 :    
2335 : chl 326 currMV->x = start_x;
2336 :     currMV->y = start_y;
2337 :    
2338 : edgomez 195 if (!(MotionFlags & PMV_HALFPEL16)) {
2339 : chl 96 currMV->x = EVEN(currMV->x);
2340 :     currMV->y = EVEN(currMV->y);
2341 :     }
2342 :    
2343 : edgomez 195 if (currMV->x > max_dx)
2344 :     currMV->x = max_dx;
2345 :     if (currMV->x < min_dx)
2346 :     currMV->x = min_dx;
2347 :     if (currMV->y > max_dy)
2348 :     currMV->y = max_dy;
2349 :     if (currMV->y < min_dy)
2350 :     currMV->y = min_dy;
2351 :    
2352 :     /***************** This is predictor SET A: only median prediction ******************/
2353 :    
2354 :     iMinSAD =
2355 :     sad16(cur,
2356 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV,
2357 :     iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);
2358 :     iMinSAD +=
2359 : chl 326 calc_delta_16(currMV->x - center_x, currMV->y - center_y,
2360 : edgomez 195 (uint8_t) iFcode, iQuant);
2361 :    
2362 : chl 96 // thresh1 is fixed to 256
2363 : edgomez 195 if ((iMinSAD < 256) ||
2364 :     ((MVequal(*currMV, prevMB->mvs[0])) &&
2365 : chl 289 ((int32_t) iMinSAD < prevMB->sad16))) {
2366 : edgomez 195 if (MotionFlags & PMV_QUICKSTOP16)
2367 :     goto EPZS16_Terminate_without_Refine;
2368 :     if (MotionFlags & PMV_EARLYSTOP16)
2369 :     goto EPZS16_Terminate_with_Refine;
2370 :     }
2371 : chl 96
2372 : edgomez 195 /************** This is predictor SET B: (0,0), prev.frame MV, neighbours **************/
2373 : chl 96
2374 :     // previous frame MV
2375 : edgomez 195 CHECK_MV16_CANDIDATE(prevMB->mvs[0].x, prevMB->mvs[0].y);
2376 : chl 96
2377 :     // set threshhold based on Min of Prediction and SAD of collocated block
2378 :     // CHECK_MV16 always uses iSAD for the SAD of last vector to check, so now iSAD is what we want
2379 :    
2380 : edgomez 195 if ((x == 0) && (y == 0)) {
2381 :     thresh2 = 512;
2382 :     } else {
2383 : chl 96 /* T_k = 1.2 * MIN(SAD_top,SAD_left,SAD_topleft,SAD_coll) +128; [Tourapis, 2002] */
2384 :    
2385 : edgomez 195 thresh2 = MIN(psad[0], iSAD) * 6 / 5 + 128;
2386 : chl 96 }
2387 :    
2388 :     // MV=(0,0) is often a good choice
2389 :    
2390 :     CHECK_MV16_ZERO;
2391 :    
2392 : edgomez 195
2393 : chl 96 // left neighbour, if allowed
2394 : edgomez 195 if (x != 0) {
2395 :     if (!(MotionFlags & PMV_HALFPEL16)) {
2396 :     pmv[1].x = EVEN(pmv[1].x);
2397 : chl 96 pmv[1].y = EVEN(pmv[1].y);
2398 :     }
2399 : edgomez 195 CHECK_MV16_CANDIDATE(pmv[1].x, pmv[1].y);
2400 : chl 96 }
2401 :     // top neighbour, if allowed
2402 : edgomez 195 if (y != 0) {
2403 :     if (!(MotionFlags & PMV_HALFPEL16)) {
2404 :     pmv[2].x = EVEN(pmv[2].x);
2405 : chl 96 pmv[2].y = EVEN(pmv[2].y);
2406 :     }
2407 : edgomez 195 CHECK_MV16_CANDIDATE(pmv[2].x, pmv[2].y);
2408 :    
2409 : chl 96 // top right neighbour, if allowed
2410 : edgomez 195 if ((uint32_t) x != (iWcount - 1)) {
2411 :     if (!(MotionFlags & PMV_HALFPEL16)) {
2412 :     pmv[3].x = EVEN(pmv[3].x);
2413 : chl 96 pmv[3].y = EVEN(pmv[3].y);
2414 :     }
2415 : edgomez 195 CHECK_MV16_CANDIDATE(pmv[3].x, pmv[3].y);
2416 : chl 96 }
2417 :     }
2418 :    
2419 :     /* Terminate if MinSAD <= T_2
2420 :     Terminate if MV[t] == MV[t-1] and MinSAD[t] <= MinSAD[t-1]
2421 :     */
2422 :    
2423 : edgomez 195 if ((iMinSAD <= thresh2)
2424 :     || (MVequal(*currMV, prevMB->mvs[0]) &&
2425 : chl 289 ((int32_t) iMinSAD <= prevMB->sad16))) {
2426 : edgomez 195 if (MotionFlags & PMV_QUICKSTOP16)
2427 :     goto EPZS16_Terminate_without_Refine;
2428 :     if (MotionFlags & PMV_EARLYSTOP16)
2429 :     goto EPZS16_Terminate_with_Refine;
2430 :     }
2431 : chl 96
2432 :     /***** predictor SET C: acceleration MV (new!), neighbours in prev. frame(new!) ****/
2433 :    
2434 : edgomez 195 backupMV = prevMB->mvs[0]; // collocated MV
2435 :     backupMV.x += (prevMB->mvs[0].x - oldMB->mvs[0].x); // acceleration X
2436 :     backupMV.y += (prevMB->mvs[0].y - oldMB->mvs[0].y); // acceleration Y
2437 : chl 96
2438 : edgomez 195 CHECK_MV16_CANDIDATE(backupMV.x, backupMV.y);
2439 : chl 96
2440 :     // left neighbour
2441 : edgomez 195 if (x != 0)
2442 :     CHECK_MV16_CANDIDATE((prevMB - 1)->mvs[0].x, (prevMB - 1)->mvs[0].y);
2443 : chl 96
2444 :     // top neighbour
2445 :     if (y != 0)
2446 : edgomez 195 CHECK_MV16_CANDIDATE((prevMB - iWcount)->mvs[0].x,
2447 :     (prevMB - iWcount)->mvs[0].y);
2448 : chl 96
2449 :     // right neighbour, if allowed (this value is not written yet, so take it from pMB->mvs
2450 :    
2451 : edgomez 195 if ((uint32_t) x != iWcount - 1)
2452 :     CHECK_MV16_CANDIDATE((prevMB + 1)->mvs[0].x, (prevMB + 1)->mvs[0].y);
2453 : chl 96
2454 :     // bottom neighbour, dito
2455 : edgomez 195 if ((uint32_t) y != iHcount - 1)
2456 :     CHECK_MV16_CANDIDATE((prevMB + iWcount)->mvs[0].x,
2457 :     (prevMB + iWcount)->mvs[0].y);
2458 : chl 96
2459 :     /* Terminate if MinSAD <= T_3 (here T_3 = T_2) */
2460 : edgomez 195 if (iMinSAD <= thresh2) {
2461 :     if (MotionFlags & PMV_QUICKSTOP16)
2462 :     goto EPZS16_Terminate_without_Refine;
2463 :     if (MotionFlags & PMV_EARLYSTOP16)
2464 :     goto EPZS16_Terminate_with_Refine;
2465 :     }
2466 : chl 96
2467 :     /************ (if Diamond Search) **************/
2468 :    
2469 : edgomez 195 backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
2470 : chl 96
2471 : chl 261 if (MotionFlags & PMV_USESQUARES16)
2472 : chl 184 MainSearchPtr = Square16_MainSearch;
2473 :     else
2474 : chl 261 if (MotionFlags & PMV_ADVANCEDDIAMOND16)
2475 : chl 184 MainSearchPtr = AdvDiamond16_MainSearch;
2476 :     else
2477 :     MainSearchPtr = Diamond16_MainSearch;
2478 :    
2479 : chl 96 /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
2480 :    
2481 : edgomez 195 iSAD =
2482 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
2483 : chl 326 currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
2484 : edgomez 195 min_dy, max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);
2485 :    
2486 :     if (iSAD < iMinSAD) {
2487 : chl 96 *currMV = newMV;
2488 :     iMinSAD = iSAD;
2489 :     }
2490 :    
2491 :    
2492 : edgomez 195 if (MotionFlags & PMV_EXTSEARCH16) {
2493 : chl 96 /* extended mode: search (up to) two more times: orignal prediction and (0,0) */
2494 :    
2495 : edgomez 195 if (!(MVequal(pmv[0], backupMV))) {
2496 :     iSAD =
2497 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
2498 : chl 326 pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
2499 : edgomez 195 min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
2500 :     2, iFcode, iQuant, 0);
2501 : chl 96 }
2502 : edgomez 195
2503 :     if (iSAD < iMinSAD) {
2504 : chl 96 *currMV = newMV;
2505 :     iMinSAD = iSAD;
2506 :     }
2507 : edgomez 195
2508 :     if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2509 :     iSAD =
2510 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2511 : chl 326 iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
2512 : edgomez 195 max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);
2513 :    
2514 :     if (iSAD < iMinSAD) {
2515 : chl 96 *currMV = newMV;
2516 :     iMinSAD = iSAD;
2517 :     }
2518 :     }
2519 :     }
2520 :    
2521 :     /*************** Choose best MV found **************/
2522 :    
2523 : edgomez 195 EPZS16_Terminate_with_Refine:
2524 :     if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step
2525 :     iMinSAD =
2526 :     Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2527 : chl 326 iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
2528 : edgomez 195 iFcode, iQuant, iEdgedWidth);
2529 : chl 96
2530 : edgomez 195 EPZS16_Terminate_without_Refine:
2531 : chl 96
2532 : chl 141 *oldMB = *prevMB;
2533 : edgomez 195
2534 : chl 326 currPMV->x = currMV->x - center_x;
2535 :     currPMV->y = currMV->y - center_y;
2536 : chl 96 return iMinSAD;
2537 :     }
2538 :    
2539 :    
2540 : edgomez 195 int32_t
2541 :     EPZSSearch8(const uint8_t * const pRef,
2542 :     const uint8_t * const pRefH,
2543 :     const uint8_t * const pRefV,
2544 :     const uint8_t * const pRefHV,
2545 :     const IMAGE * const pCur,
2546 :     const int x,
2547 :     const int y,
2548 :     const int start_x,
2549 :     const int start_y,
2550 : chl 326 const int center_x,
2551 :     const int center_y,
2552 : edgomez 195 const uint32_t MotionFlags,
2553 :     const uint32_t iQuant,
2554 :     const uint32_t iFcode,
2555 :     const MBParam * const pParam,
2556 :     const MACROBLOCK * const pMBs,
2557 :     const MACROBLOCK * const prevMBs,
2558 :     VECTOR * const currMV,
2559 :     VECTOR * const currPMV)
2560 : chl 96 {
2561 : chl 141 /* Please not that EPZS might not be a good choice for 8x8-block motion search ! */
2562 :    
2563 :     const uint32_t iWcount = pParam->mb_width;
2564 : chl 96 const int32_t iWidth = pParam->width;
2565 :     const int32_t iHeight = pParam->height;
2566 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
2567 : chl 96
2568 : edgomez 195 const uint8_t *cur = pCur->y + x * 8 + y * 8 * iEdgedWidth;
2569 : chl 96
2570 : edgomez 195 int32_t iDiamondSize = 1;
2571 :    
2572 : chl 96 int32_t min_dx;
2573 :     int32_t max_dx;
2574 :     int32_t min_dy;
2575 :     int32_t max_dy;
2576 : edgomez 195
2577 : chl 96 VECTOR newMV;
2578 :     VECTOR backupMV;
2579 : edgomez 195
2580 : chl 96 VECTOR pmv[4];
2581 :     int32_t psad[8];
2582 :    
2583 : edgomez 195 const int32_t iSubBlock = ((y & 1) << 1) + (x & 1);
2584 : chl 96
2585 : edgomez 195 // const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount;
2586 :     const MACROBLOCK *const prevMB = prevMBs + (x >> 1) + (y >> 1) * iWcount;
2587 : chl 96
2588 : edgomez 195 int32_t bPredEq;
2589 :     int32_t iMinSAD, iSAD = 9999;
2590 :    
2591 : chl 181 MainSearch8FuncPtr MainSearchPtr;
2592 : chl 96
2593 :     /* Get maximum range */
2594 : edgomez 195 get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 8, iWidth, iHeight,
2595 :     iFcode);
2596 : chl 96
2597 :     /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */
2598 :    
2599 : edgomez 195 if (!(MotionFlags & PMV_HALFPEL8)) {
2600 :     min_dx = EVEN(min_dx);
2601 :     max_dx = EVEN(max_dx);
2602 :     min_dy = EVEN(min_dy);
2603 :     max_dy = EVEN(max_dy);
2604 :     }
2605 :     /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
2606 : chl 285 bPredEq = get_pmvdata2(pMBs, iWcount, 0, x >> 1, y >> 1, iSubBlock, pmv, psad);
2607 : chl 96
2608 :    
2609 :     /* Step 4: Calculate SAD around the Median prediction.
2610 :     MinSAD=SAD
2611 :     If Motion Vector equal to Previous frame motion vector
2612 :     and MinSAD<PrevFrmSAD goto Step 10.
2613 :     If SAD<=256 goto Step 10.
2614 :     */
2615 :    
2616 :     // Prepare for main loop
2617 :    
2618 : edgomez 195
2619 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2620 : chl 96 currMV->x = EVEN(currMV->x);
2621 :     currMV->y = EVEN(currMV->y);
2622 :     }
2623 :    
2624 : edgomez 195 if (currMV->x > max_dx)
2625 :     currMV->x = max_dx;
2626 :     if (currMV->x < min_dx)
2627 :     currMV->x = min_dx;
2628 :     if (currMV->y > max_dy)
2629 :     currMV->y = max_dy;
2630 :     if (currMV->y < min_dy)
2631 :     currMV->y = min_dy;
2632 : chl 96
2633 : edgomez 195 /***************** This is predictor SET A: only median prediction ******************/
2634 : chl 96
2635 : edgomez 195
2636 :     iMinSAD =
2637 :     sad8(cur,
2638 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV,
2639 :     iEdgedWidth), iEdgedWidth);
2640 :     iMinSAD +=
2641 : chl 326 calc_delta_8(currMV->x - center_x, currMV->y - center_y,
2642 : edgomez 195 (uint8_t) iFcode, iQuant);
2643 :    
2644 :    
2645 : chl 96 // thresh1 is fixed to 256
2646 : edgomez 195 if (iMinSAD < 256 / 4) {
2647 :     if (MotionFlags & PMV_QUICKSTOP8)
2648 :     goto EPZS8_Terminate_without_Refine;
2649 :     if (MotionFlags & PMV_EARLYSTOP8)
2650 :     goto EPZS8_Terminate_with_Refine;
2651 :     }
2652 : chl 96
2653 : edgomez 195 /************** This is predictor SET B: (0,0), prev.frame MV, neighbours **************/
2654 : chl 96
2655 :    
2656 :     // MV=(0,0) is often a good choice
2657 :     CHECK_MV8_ZERO;
2658 :    
2659 : chl 141 // previous frame MV
2660 : edgomez 195 CHECK_MV8_CANDIDATE(prevMB->mvs[iSubBlock].x, prevMB->mvs[iSubBlock].y);
2661 :    
2662 : chl 141 // left neighbour, if allowed
2663 : edgomez 195 if (psad[1] != MV_MAX_ERROR) {
2664 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2665 :     pmv[1].x = EVEN(pmv[1].x);
2666 : chl 141 pmv[1].y = EVEN(pmv[1].y);
2667 :     }
2668 : edgomez 195 CHECK_MV8_CANDIDATE(pmv[1].x, pmv[1].y);
2669 : chl 141 }
2670 :     // top neighbour, if allowed
2671 : edgomez 195 if (psad[2] != MV_MAX_ERROR) {
2672 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2673 :     pmv[2].x = EVEN(pmv[2].x);
2674 : chl 141 pmv[2].y = EVEN(pmv[2].y);
2675 :     }
2676 : edgomez 195 CHECK_MV8_CANDIDATE(pmv[2].x, pmv[2].y);
2677 :    
2678 : chl 141 // top right neighbour, if allowed
2679 : edgomez 195 if (psad[3] != MV_MAX_ERROR) {
2680 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2681 :     pmv[3].x = EVEN(pmv[3].x);
2682 : chl 141 pmv[3].y = EVEN(pmv[3].y);
2683 :     }
2684 : edgomez 195 CHECK_MV8_CANDIDATE(pmv[3].x, pmv[3].y);
2685 : chl 141 }
2686 :     }
2687 :    
2688 :     /* // this bias is zero anyway, at the moment!
2689 :    
2690 :     if ( (MVzero(*currMV)) && (!MVzero(pmv[0])) ) // && (iMinSAD <= iQuant * 96)
2691 :     iMinSAD -= MV8_00_BIAS;
2692 :    
2693 : edgomez 195 */
2694 : chl 141
2695 : chl 96 /* Terminate if MinSAD <= T_2
2696 :     Terminate if MV[t] == MV[t-1] and MinSAD[t] <= MinSAD[t-1]
2697 :     */
2698 :    
2699 : edgomez 195 if (iMinSAD < 512 / 4) { /* T_2 == 512/4 hardcoded */
2700 :     if (MotionFlags & PMV_QUICKSTOP8)
2701 :     goto EPZS8_Terminate_without_Refine;
2702 :     if (MotionFlags & PMV_EARLYSTOP8)
2703 :     goto EPZS8_Terminate_with_Refine;
2704 :     }
2705 : chl 96
2706 : chl 141 /************ (Diamond Search) **************/
2707 : chl 96
2708 : edgomez 195 backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
2709 : chl 96
2710 :     if (!(MotionFlags & PMV_HALFPELDIAMOND8))
2711 :     iDiamondSize *= 2;
2712 : edgomez 195
2713 : chl 141 /* default: use best prediction as starting point for one call of EPZS_MainSearch */
2714 : chl 96
2715 : chl 184 // there is no EPZS^2 for inter4v at the moment
2716 : chl 141
2717 : chl 345 if (MotionFlags & PMV_USESQUARES8)
2718 :     MainSearchPtr = Square8_MainSearch;
2719 :     else
2720 : chl 181
2721 :     if (MotionFlags & PMV_ADVANCEDDIAMOND8)
2722 :     MainSearchPtr = AdvDiamond8_MainSearch;
2723 :     else
2724 :     MainSearchPtr = Diamond8_MainSearch;
2725 :    
2726 : edgomez 195 iSAD =
2727 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
2728 : chl 326 currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
2729 : edgomez 195 min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
2730 :     iQuant, 0);
2731 : chl 96
2732 : chl 141
2733 : edgomez 195 if (iSAD < iMinSAD) {
2734 : chl 96 *currMV = newMV;
2735 :     iMinSAD = iSAD;
2736 :     }
2737 :    
2738 : edgomez 195 if (MotionFlags & PMV_EXTSEARCH8) {
2739 : chl 96 /* extended mode: search (up to) two more times: orignal prediction and (0,0) */
2740 :    
2741 : edgomez 195 if (!(MVequal(pmv[0], backupMV))) {
2742 :     iSAD =
2743 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
2744 : chl 326 pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
2745 : edgomez 195 min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
2746 :     iDiamondSize, iFcode, iQuant, 0);
2747 :    
2748 :     if (iSAD < iMinSAD) {
2749 : chl 96 *currMV = newMV;
2750 :     iMinSAD = iSAD;
2751 :     }
2752 :     }
2753 :    
2754 : edgomez 195 if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2755 :     iSAD =
2756 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2757 : chl 326 iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
2758 : edgomez 195 max_dy, iEdgedWidth, iDiamondSize, iFcode,
2759 :     iQuant, 0);
2760 :    
2761 :     if (iSAD < iMinSAD) {
2762 : chl 96 *currMV = newMV;
2763 :     iMinSAD = iSAD;
2764 :     }
2765 :     }
2766 :     }
2767 :    
2768 :     /*************** Choose best MV found **************/
2769 :    
2770 : edgomez 195 EPZS8_Terminate_with_Refine:
2771 :     if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step
2772 :     iMinSAD =
2773 :     Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2774 : chl 326 iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
2775 : edgomez 195 iFcode, iQuant, iEdgedWidth);
2776 : chl 96
2777 : edgomez 195 EPZS8_Terminate_without_Refine:
2778 : chl 96
2779 : chl 326 currPMV->x = currMV->x - center_x;
2780 :     currPMV->y = currMV->y - center_y;
2781 : chl 96 return iMinSAD;
2782 :     }
2783 :    
2784 : suxen_drol 118
2785 :    
2786 : chl 289 int32_t
2787 :     PMVfastIntSearch16(const uint8_t * const pRef,
2788 :     const uint8_t * const pRefH,
2789 :     const uint8_t * const pRefV,
2790 :     const uint8_t * const pRefHV,
2791 :     const IMAGE * const pCur,
2792 :     const int x,
2793 :     const int y,
2794 : chl 346 const int start_x, /* start should be most likely vector */
2795 :     const int start_y,
2796 :     const int center_x, /* center is from where length of MVs is measured */
2797 :     const int center_y,
2798 : chl 289 const uint32_t MotionFlags,
2799 :     const uint32_t iQuant,
2800 :     const uint32_t iFcode,
2801 :     const MBParam * const pParam,
2802 :     const MACROBLOCK * const pMBs,
2803 :     const MACROBLOCK * const prevMBs,
2804 :     VECTOR * const currMV,
2805 :     VECTOR * const currPMV)
2806 :     {
2807 :     const uint32_t iWcount = pParam->mb_width;
2808 :     const int32_t iWidth = pParam->width;
2809 :     const int32_t iHeight = pParam->height;
2810 :     const int32_t iEdgedWidth = pParam->edged_width;
2811 : suxen_drol 118
2812 : chl 289 const uint8_t *cur = pCur->y + x * 16 + y * 16 * iEdgedWidth;
2813 :     const VECTOR zeroMV = { 0, 0 };
2814 : suxen_drol 118
2815 : chl 289 int32_t iDiamondSize;
2816 :    
2817 :     int32_t min_dx;
2818 :     int32_t max_dx;
2819 :     int32_t min_dy;
2820 :     int32_t max_dy;
2821 :    
2822 :     int32_t iFound;
2823 :    
2824 :     VECTOR newMV;
2825 : chl 346 VECTOR backupMV;
2826 : chl 289
2827 :     VECTOR pmv[4];
2828 :     int32_t psad[4];
2829 :    
2830 :     MainSearch16FuncPtr MainSearchPtr;
2831 :    
2832 : edgomez 476 MACROBLOCK *const prevMB = (MACROBLOCK *const)prevMBs + x + y * iWcount;
2833 :     MACROBLOCK *const pMB = (MACROBLOCK *const)(pMBs + x + y * iWcount);
2834 : chl 289
2835 :     int32_t threshA, threshB;
2836 :     int32_t bPredEq;
2837 :     int32_t iMinSAD, iSAD;
2838 : chl 326
2839 : chl 289
2840 :     /* Get maximum range */
2841 :     get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight,
2842 :     iFcode);
2843 :    
2844 :     /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */
2845 :    
2846 :     if ((x == 0) && (y == 0)) {
2847 :     threshA = 512;
2848 :     threshB = 1024;
2849 :    
2850 :     bPredEq = 0;
2851 :     psad[0] = psad[1] = psad[2] = psad[3] = 0;
2852 :     *currMV = pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV;
2853 :    
2854 :     } else {
2855 : chl 370
2856 :     bPredEq = get_ipmvdata(pMBs, iWcount, 0, x, y, 0, pmv, psad);
2857 :    
2858 : chl 289 threshA = psad[0];
2859 :     threshB = threshA + 256;
2860 :     if (threshA < 512)
2861 :     threshA = 512;
2862 :     if (threshA > 1024)
2863 :     threshA = 1024;
2864 :     if (threshB > 1792)
2865 :     threshB = 1792;
2866 :    
2867 :     *currMV = pmv[0]; /* current best := prediction */
2868 :     }
2869 :    
2870 :     iFound = 0;
2871 :    
2872 :     /* Step 4: Calculate SAD around the Median prediction.
2873 :     MinSAD=SAD
2874 :     If Motion Vector equal to Previous frame motion vector
2875 :     and MinSAD<PrevFrmSAD goto Step 10.
2876 :     If SAD<=256 goto Step 10.
2877 :     */
2878 :    
2879 :     if (currMV->x > max_dx) {
2880 :     currMV->x = EVEN(max_dx);
2881 :     }
2882 :     if (currMV->x < min_dx) {
2883 :     currMV->x = EVEN(min_dx);
2884 :     }
2885 :     if (currMV->y > max_dy) {
2886 :     currMV->y = EVEN(max_dy);
2887 :     }
2888 :     if (currMV->y < min_dy) {
2889 :     currMV->y = EVEN(min_dy);
2890 :     }
2891 :    
2892 :     iMinSAD =
2893 :     sad16(cur,
2894 : chl 312 get_iref_mv(pRef, x, y, 16, currMV,
2895 : chl 289 iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);
2896 :     iMinSAD +=
2897 : chl 326 calc_delta_16(currMV->x - center_x, currMV->y - center_y,
2898 : chl 289 (uint8_t) iFcode, iQuant);
2899 :    
2900 :     if ((iMinSAD < 256) ||
2901 :     ((MVequal(*currMV, prevMB->i_mvs[0])) &&
2902 :     ((int32_t) iMinSAD < prevMB->i_sad16))) {
2903 : edgomez 476 if (iMinSAD < (int)(2 * iQuant)) // high chances for SKIP-mode
2904 : chl 289 {
2905 :     if (!MVzero(*currMV)) {
2906 :     iMinSAD += MV16_00_BIAS;
2907 :     CHECK_MV16_ZERO; // (0,0) saves space for letterboxed pictures
2908 :     iMinSAD -= MV16_00_BIAS;
2909 :     }
2910 :     }
2911 :    
2912 :     if (MotionFlags & PMV_EARLYSTOP16)
2913 :     goto PMVfastInt16_Terminate_with_Refine;
2914 :     }
2915 :    
2916 :    
2917 :     /* Step 2 (lazy eval): Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion
2918 :     vector of the median.
2919 :     If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2
2920 :     */
2921 :    
2922 :     if ((bPredEq) && (MVequal(pmv[0], prevMB->i_mvs[0])))
2923 :     iFound = 2;
2924 :    
2925 :     /* Step 3 (lazy eval): If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search.
2926 :     Otherwise select large Diamond Search.
2927 :     */
2928 :    
2929 :     if ((!MVzero(pmv[0])) || (threshB < 1536) || (bPredEq))
2930 :     iDiamondSize = 2; // halfpel units!
2931 :     else
2932 :     iDiamondSize = 4; // halfpel units!
2933 :    
2934 :     /*
2935 :     Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block.
2936 :     Also calculate (0,0) but do not subtract offset.
2937 :     Let MinSAD be the smallest SAD up to this point.
2938 :     If MV is (0,0) subtract offset.
2939 :     */
2940 :    
2941 :     // (0,0) is often a good choice
2942 :    
2943 :     if (!MVzero(pmv[0]))
2944 :     CHECK_MV16_ZERO;
2945 :    
2946 :     // previous frame MV is always possible
2947 :    
2948 :     if (!MVzero(prevMB->i_mvs[0]))
2949 :     if (!MVequal(prevMB->i_mvs[0], pmv[0]))
2950 :     CHECK_MV16_CANDIDATE(prevMB->i_mvs[0].x, prevMB->i_mvs[0].y);
2951 :    
2952 :     // left neighbour, if allowed
2953 :    
2954 :     if (!MVzero(pmv[1]))
2955 :     if (!MVequal(pmv[1], prevMB->i_mvs[0]))
2956 :     if (!MVequal(pmv[1], pmv[0]))
2957 :     CHECK_MV16_CANDIDATE(pmv[1].x, pmv[1].y);
2958 :    
2959 :     // top neighbour, if allowed
2960 :     if (!MVzero(pmv[2]))
2961 :     if (!MVequal(pmv[2], prevMB->i_mvs[0]))
2962 :     if (!MVequal(pmv[2], pmv[0]))
2963 :     if (!MVequal(pmv[2], pmv[1]))
2964 :     CHECK_MV16_CANDIDATE(pmv[2].x, pmv[2].y);
2965 :    
2966 :     // top right neighbour, if allowed
2967 :     if (!MVzero(pmv[3]))
2968 :     if (!MVequal(pmv[3], prevMB->i_mvs[0]))
2969 :     if (!MVequal(pmv[3], pmv[0]))
2970 :     if (!MVequal(pmv[3], pmv[1]))
2971 :     if (!MVequal(pmv[3], pmv[2]))
2972 :     CHECK_MV16_CANDIDATE(pmv[3].x,
2973 :     pmv[3].y);
2974 :    
2975 :     if ((MVzero(*currMV)) &&
2976 :     (!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96) */ )
2977 :     iMinSAD -= MV16_00_BIAS;
2978 :    
2979 :    
2980 :     /* Step 6: If MinSAD <= thresa goto Step 10.
2981 :     If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10.
2982 :     */
2983 :    
2984 :     if ((iMinSAD <= threshA) ||
2985 :     (MVequal(*currMV, prevMB->i_mvs[0]) &&
2986 :     ((int32_t) iMinSAD < prevMB->i_sad16))) {
2987 :    
2988 :     if (MotionFlags & PMV_EARLYSTOP16)
2989 :     goto PMVfastInt16_Terminate_with_Refine;
2990 :     }
2991 :    
2992 :    
2993 :     /************ (Diamond Search) **************/
2994 :     /*
2995 :     Step 7: Perform Diamond search, with either the small or large diamond.
2996 :     If Found=2 only examine one Diamond pattern, and afterwards goto step 10
2997 :     Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond.
2998 :     If center then goto step 10.
2999 :     Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center.
3000 :     Refine by using small diamond and goto step 10.
3001 :     */
3002 :    
3003 :     if (MotionFlags & PMV_USESQUARES16)
3004 :     MainSearchPtr = Square16_MainSearch;
3005 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
3006 :     MainSearchPtr = AdvDiamond16_MainSearch;
3007 :     else
3008 :     MainSearchPtr = Diamond16_MainSearch;
3009 :    
3010 :     backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
3011 :    
3012 :    
3013 :     /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
3014 :     iSAD =
3015 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
3016 : chl 326 currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
3017 : chl 289 min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
3018 :     iQuant, iFound);
3019 :    
3020 :     if (iSAD < iMinSAD) {
3021 :     *currMV = newMV;
3022 :     iMinSAD = iSAD;
3023 :     }
3024 :    
3025 :     if (MotionFlags & PMV_EXTSEARCH16) {
3026 :     /* extended: search (up to) two more times: orignal prediction and (0,0) */
3027 :    
3028 :     if (!(MVequal(pmv[0], backupMV))) {
3029 :     iSAD =
3030 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
3031 : chl 326 pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
3032 : chl 289 min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
3033 :     iDiamondSize, iFcode, iQuant, iFound);
3034 :    
3035 :     if (iSAD < iMinSAD) {
3036 :     *currMV = newMV;
3037 :     iMinSAD = iSAD;
3038 :     }
3039 :     }
3040 :    
3041 :     if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
3042 :     iSAD =
3043 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
3044 : chl 326 iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
3045 : chl 289 max_dy, iEdgedWidth, iDiamondSize, iFcode,
3046 :     iQuant, iFound);
3047 :    
3048 :     if (iSAD < iMinSAD) {
3049 :     *currMV = newMV;
3050 :     iMinSAD = iSAD;
3051 :     }
3052 :     }
3053 :     }
3054 :    
3055 :     /*
3056 :     Step 10: The motion vector is chosen according to the block corresponding to MinSAD.
3057 :     */
3058 :    
3059 :     PMVfastInt16_Terminate_with_Refine:
3060 :    
3061 :     pMB->i_mvs[0] = pMB->i_mvs[1] = pMB->i_mvs[2] = pMB->i_mvs[3] = pMB->i_mv16 = *currMV;
3062 :     pMB->i_sad8[0] = pMB->i_sad8[1] = pMB->i_sad8[2] = pMB->i_sad8[3] = pMB->i_sad16 = iMinSAD;
3063 :    
3064 :     if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step
3065 :     iMinSAD =
3066 :     Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
3067 : chl 326 iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
3068 : chl 289 iFcode, iQuant, iEdgedWidth);
3069 :    
3070 :     pmv[0] = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); // get _REAL_ prediction (halfpel possible)
3071 :    
3072 : chl 326 currPMV->x = currMV->x - center_x;
3073 :     currPMV->y = currMV->y - center_y;
3074 : chl 289 return iMinSAD;
3075 :     }
3076 :    

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