[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 444 - (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 : chl 172 int32_t x, y;
145 :     int32_t iIntra = 0;
146 :     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 370 if (pMB->mode == MODE_NOT_CODED)
157 :     continue;
158 :    
159 : chl 326 predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
160 :    
161 : edgomez 195 pMB->sad16 =
162 : chl 326 SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent,
163 :     x, y, predMV.x, predMV.y, predMV.x, predMV.y,
164 :     current->motion_flags, current->quant,
165 : edgomez 195 current->fcode, pParam, pMBs, prevMBs, &pMB->mv16,
166 :     &pMB->pmvs[0]);
167 : chl 184
168 : edgomez 195 if (0 < (pMB->sad16 - MV16_INTER_BIAS)) {
169 : chl 172 int32_t deviation;
170 : chl 184
171 : edgomez 195 deviation =
172 :     dev16(pCurrent->y + x * 16 + y * 16 * pParam->edged_width,
173 :     pParam->edged_width);
174 :    
175 :     if (deviation < (pMB->sad16 - MV16_INTER_BIAS)) {
176 : chl 172 pMB->mode = MODE_INTRA;
177 : edgomez 195 pMB->mv16 = pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] =
178 :     pMB->mvs[3] = zeroMV;
179 :     pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] =
180 :     pMB->sad8[3] = 0;
181 :    
182 : chl 172 iIntra++;
183 :     if (iIntra >= iLimit)
184 : edgomez 195 return 1;
185 :    
186 : chl 172 continue;
187 :     }
188 : edgomez 195 }
189 :    
190 : chl 184 pmv = pMB->pmvs[0];
191 :     if (current->global_flags & XVID_INTER4V)
192 : edgomez 195 if ((!(current->global_flags & XVID_LUMIMASKING) ||
193 :     pMB->dquant == NO_CHANGE)) {
194 :     int32_t sad8 = IMV16X16 * current->quant;
195 :    
196 : chl 326 if (sad8 < pMB->sad16) {
197 : edgomez 195 sad8 += pMB->sad8[0] =
198 :     SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
199 : chl 326 pCurrent, 2 * x, 2 * y,
200 :     pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y,
201 :     current->motion_flags,
202 : edgomez 195 current->quant, current->fcode, pParam,
203 :     pMBs, prevMBs, &pMB->mvs[0],
204 :     &pMB->pmvs[0]);
205 : chl 326 }
206 :     if (sad8 < pMB->sad16) {
207 : edgomez 195
208 : chl 326 predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 1);
209 : edgomez 195 sad8 += pMB->sad8[1] =
210 :     SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
211 : chl 326 pCurrent, 2 * x + 1, 2 * y,
212 :     pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y,
213 :     current->motion_flags,
214 : edgomez 195 current->quant, current->fcode, pParam,
215 :     pMBs, prevMBs, &pMB->mvs[1],
216 :     &pMB->pmvs[1]);
217 : chl 326 }
218 :     if (sad8 < pMB->sad16) {
219 :     predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 2);
220 : edgomez 195 sad8 += pMB->sad8[2] =
221 :     SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
222 : chl 326 pCurrent, 2 * x, 2 * y + 1,
223 :     pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y,
224 :     current->motion_flags,
225 : edgomez 195 current->quant, current->fcode, pParam,
226 :     pMBs, prevMBs, &pMB->mvs[2],
227 :     &pMB->pmvs[2]);
228 : chl 326 }
229 :     if (sad8 < pMB->sad16) {
230 :     predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 3);
231 : edgomez 195 sad8 += pMB->sad8[3] =
232 :     SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
233 :     pCurrent, 2 * x + 1, 2 * y + 1,
234 : chl 326 pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y,
235 :     current->motion_flags,
236 :     current->quant, current->fcode, pParam,
237 :     pMBs, prevMBs,
238 :     &pMB->mvs[3],
239 :     &pMB->pmvs[3]);
240 :     }
241 :    
242 : chl 184 /* decide: MODE_INTER or MODE_INTER4V
243 : edgomez 195 mpeg4: if (sad8 < pMB->sad16 - nb/2+1) use_inter4v
244 :     */
245 :    
246 :     if (sad8 < pMB->sad16) {
247 : chl 175 pMB->mode = MODE_INTER4V;
248 : edgomez 195 pMB->sad8[0] *= 4;
249 : chl 175 pMB->sad8[1] *= 4;
250 :     pMB->sad8[2] *= 4;
251 :     pMB->sad8[3] *= 4;
252 :     continue;
253 :     }
254 :    
255 : edgomez 195 }
256 :    
257 :     pMB->mode = MODE_INTER;
258 :     pMB->pmvs[0] = pmv; /* pMB->pmvs[1] = pMB->pmvs[2] = pMB->pmvs[3] are not needed for INTER */
259 :     pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mv16;
260 :     pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] =
261 :     pMB->sad16;
262 : chl 259 }
263 :     }
264 : edgomez 195
265 :     return 0;
266 : Isibaar 3 }
267 :    
268 : chl 326
269 : Isibaar 3 #define CHECK_MV16_ZERO {\
270 :     if ( (0 <= max_dx) && (0 >= min_dx) \
271 :     && (0 <= max_dy) && (0 >= min_dy) ) \
272 :     { \
273 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0, 0 , iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); \
274 : chl 326 iSAD += calc_delta_16(-center_x, -center_y, (uint8_t)iFcode, iQuant);\
275 : Isibaar 3 if (iSAD < iMinSAD) \
276 :     { iMinSAD=iSAD; currMV->x=0; currMV->y=0; } } \
277 :     }
278 :    
279 : chl 96 #define NOCHECK_MV16_CANDIDATE(X,Y) { \
280 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
281 : chl 326 iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\
282 : chl 96 if (iSAD < iMinSAD) \
283 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \
284 :     }
285 : Isibaar 3
286 :     #define CHECK_MV16_CANDIDATE(X,Y) { \
287 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
288 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
289 :     { \
290 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
291 : chl 326 iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\
292 : Isibaar 3 if (iSAD < iMinSAD) \
293 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \
294 :     }
295 :    
296 :     #define CHECK_MV16_CANDIDATE_DIR(X,Y,D) { \
297 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
298 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
299 :     { \
300 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
301 : chl 326 iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\
302 : Isibaar 3 if (iSAD < iMinSAD) \
303 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \
304 :     }
305 :    
306 :     #define CHECK_MV16_CANDIDATE_FOUND(X,Y,D) { \
307 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
308 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
309 :     { \
310 :     iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
311 : chl 326 iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\
312 : Isibaar 3 if (iSAD < iMinSAD) \
313 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \
314 :     }
315 :    
316 :    
317 :     #define CHECK_MV8_ZERO {\
318 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, 0, 0 , iEdgedWidth), iEdgedWidth); \
319 : chl 326 iSAD += calc_delta_8(-center_x, -center_y, (uint8_t)iFcode, iQuant);\
320 : Isibaar 3 if (iSAD < iMinSAD) \
321 :     { iMinSAD=iSAD; currMV->x=0; currMV->y=0; } \
322 :     }
323 : edgomez 195
324 : chl 96 #define NOCHECK_MV8_CANDIDATE(X,Y) \
325 :     { \
326 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
327 : chl 326 iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\
328 : chl 96 if (iSAD < iMinSAD) \
329 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \
330 :     }
331 : Isibaar 3
332 :     #define CHECK_MV8_CANDIDATE(X,Y) { \
333 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
334 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
335 :     { \
336 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
337 : chl 326 iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\
338 : Isibaar 3 if (iSAD < iMinSAD) \
339 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \
340 :     }
341 :    
342 :     #define CHECK_MV8_CANDIDATE_DIR(X,Y,D) { \
343 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
344 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
345 :     { \
346 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
347 : chl 326 iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\
348 : Isibaar 3 if (iSAD < iMinSAD) \
349 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \
350 :     }
351 :    
352 :     #define CHECK_MV8_CANDIDATE_FOUND(X,Y,D) { \
353 :     if ( ((X) <= max_dx) && ((X) >= min_dx) \
354 :     && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
355 :     { \
356 :     iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
357 : chl 326 iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\
358 : Isibaar 3 if (iSAD < iMinSAD) \
359 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \
360 :     }
361 :    
362 :     /* too slow and not fully functional at the moment */
363 :     /*
364 :     int32_t ZeroSearch16(
365 :     const uint8_t * const pRef,
366 :     const uint8_t * const pRefH,
367 :     const uint8_t * const pRefV,
368 :     const uint8_t * const pRefHV,
369 :     const IMAGE * const pCur,
370 :     const int x, const int y,
371 :     const uint32_t MotionFlags,
372 : suxen_drol 136 const uint32_t iQuant,
373 :     const uint32_t iFcode,
374 : Isibaar 3 MBParam * const pParam,
375 : suxen_drol 136 const MACROBLOCK * const pMBs,
376 :     const MACROBLOCK * const prevMBs,
377 : Isibaar 3 VECTOR * const currMV,
378 :     VECTOR * const currPMV)
379 :     {
380 :     const int32_t iEdgedWidth = pParam->edged_width;
381 :     const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth;
382 :     int32_t iSAD;
383 : suxen_drol 254 VECTOR pred;
384 :    
385 : Isibaar 3
386 : suxen_drol 254 pred = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
387 : Isibaar 3
388 :     iSAD = sad16( cur,
389 :     get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0,0, iEdgedWidth),
390 :     iEdgedWidth, MV_MAX_ERROR);
391 :     if (iSAD <= iQuant * 96)
392 :     iSAD -= MV16_00_BIAS;
393 :    
394 :     currMV->x = 0;
395 :     currMV->y = 0;
396 : suxen_drol 254 currPMV->x = -pred.x;
397 :     currPMV->y = -pred.y;
398 : Isibaar 3
399 :     return iSAD;
400 :    
401 :     }
402 :     */
403 :    
404 : edgomez 195 int32_t
405 :     Diamond16_MainSearch(const uint8_t * const pRef,
406 :     const uint8_t * const pRefH,
407 :     const uint8_t * const pRefV,
408 :     const uint8_t * const pRefHV,
409 :     const uint8_t * const cur,
410 :     const int x,
411 :     const int y,
412 : chl 326 const int start_x,
413 :     const int start_y,
414 :     int iMinSAD,
415 :     VECTOR * const currMV,
416 :     const int center_x,
417 :     const int center_y,
418 : edgomez 195 const int32_t min_dx,
419 :     const int32_t max_dx,
420 :     const int32_t min_dy,
421 :     const int32_t max_dy,
422 :     const int32_t iEdgedWidth,
423 :     const int32_t iDiamondSize,
424 :     const int32_t iFcode,
425 :     const int32_t iQuant,
426 :     int iFound)
427 : Isibaar 3 {
428 :     /* Do a diamond search around given starting point, return SAD of best */
429 :    
430 : edgomez 195 int32_t iDirection = 0;
431 : chl 344 int32_t iDirectionBackup;
432 : Isibaar 3 int32_t iSAD;
433 :     VECTOR backupMV;
434 : edgomez 195
435 : chl 326 backupMV.x = start_x;
436 :     backupMV.y = start_y;
437 : edgomez 195
438 : Isibaar 3 /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
439 :    
440 : edgomez 195 CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
441 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
442 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
443 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
444 : Isibaar 3
445 : chl 344 if (iDirection) {
446 : edgomez 195 while (!iFound) {
447 :     iFound = 1;
448 :     backupMV = *currMV;
449 : chl 344 iDirectionBackup = iDirection;
450 : edgomez 195
451 : chl 344 if (iDirectionBackup != 2)
452 : edgomez 195 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
453 :     backupMV.y, 1);
454 : chl 344 if (iDirectionBackup != 1)
455 : edgomez 195 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
456 :     backupMV.y, 2);
457 : chl 344 if (iDirectionBackup != 4)
458 : edgomez 195 CHECK_MV16_CANDIDATE_FOUND(backupMV.x,
459 :     backupMV.y - iDiamondSize, 3);
460 : chl 344 if (iDirectionBackup != 3)
461 : edgomez 195 CHECK_MV16_CANDIDATE_FOUND(backupMV.x,
462 :     backupMV.y + iDiamondSize, 4);
463 : chl 344 }
464 : edgomez 195 } else {
465 : chl 326 currMV->x = start_x;
466 :     currMV->y = start_y;
467 : edgomez 78 }
468 : Isibaar 3 return iMinSAD;
469 :     }
470 :    
471 : edgomez 195 int32_t
472 :     Square16_MainSearch(const uint8_t * const pRef,
473 : chl 96 const uint8_t * const pRefH,
474 :     const uint8_t * const pRefV,
475 :     const uint8_t * const pRefHV,
476 :     const uint8_t * const cur,
477 : edgomez 195 const int x,
478 :     const int y,
479 : chl 326 const int start_x,
480 :     const int start_y,
481 :     int iMinSAD,
482 :     VECTOR * const currMV,
483 :     const int center_x,
484 :     const int center_y,
485 : edgomez 195 const int32_t min_dx,
486 :     const int32_t max_dx,
487 :     const int32_t min_dy,
488 :     const int32_t max_dy,
489 :     const int32_t iEdgedWidth,
490 :     const int32_t iDiamondSize,
491 : chl 96 const int32_t iFcode,
492 :     const int32_t iQuant,
493 :     int iFound)
494 :     {
495 :     /* Do a square search around given starting point, return SAD of best */
496 :    
497 : edgomez 195 int32_t iDirection = 0;
498 : chl 96 int32_t iSAD;
499 :     VECTOR backupMV;
500 : edgomez 195
501 : chl 326 backupMV.x = start_x;
502 :     backupMV.y = start_y;
503 : edgomez 195
504 : chl 96 /* It's one search with full square pattern, and new parts for all following diamonds */
505 :    
506 :     /* new direction are extra, so 1-4 is normal diamond
507 :     537
508 :     1*2
509 :     648
510 :     */
511 :    
512 : edgomez 195 CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
513 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
514 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
515 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
516 : chl 96
517 : edgomez 195 CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
518 :     backupMV.y - iDiamondSize, 5);
519 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
520 :     backupMV.y + iDiamondSize, 6);
521 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
522 :     backupMV.y - iDiamondSize, 7);
523 :     CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
524 :     backupMV.y + iDiamondSize, 8);
525 : chl 96
526 : edgomez 195
527 : chl 345 if (iDirection) {
528 : edgomez 195 while (!iFound) {
529 :     iFound = 1;
530 :     backupMV = *currMV;
531 : chl 96
532 : edgomez 195 switch (iDirection) {
533 :     case 1:
534 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
535 :     backupMV.y, 1);
536 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
537 : edgomez 195 backupMV.y - iDiamondSize, 5);
538 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
539 : edgomez 195 backupMV.y - iDiamondSize, 7);
540 :     break;
541 :     case 2:
542 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
543 : edgomez 195 2);
544 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
545 : edgomez 195 backupMV.y + iDiamondSize, 6);
546 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
547 : edgomez 195 backupMV.y + iDiamondSize, 8);
548 :     break;
549 :    
550 :     case 3:
551 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
552 : edgomez 195 4);
553 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
554 : edgomez 195 backupMV.y - iDiamondSize, 7);
555 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
556 : edgomez 195 backupMV.y + iDiamondSize, 8);
557 :     break;
558 :    
559 :     case 4:
560 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
561 : edgomez 195 3);
562 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
563 : edgomez 195 backupMV.y - iDiamondSize, 5);
564 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
565 : edgomez 195 backupMV.y + iDiamondSize, 6);
566 :     break;
567 :    
568 :     case 5:
569 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
570 : edgomez 195 1);
571 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
572 : edgomez 195 3);
573 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
574 : edgomez 195 backupMV.y - iDiamondSize, 5);
575 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
576 : edgomez 195 backupMV.y + iDiamondSize, 6);
577 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
578 : edgomez 195 backupMV.y - iDiamondSize, 7);
579 :     break;
580 :    
581 :     case 6:
582 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
583 : edgomez 195 2);
584 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
585 : edgomez 195 3);
586 :    
587 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
588 : edgomez 195 backupMV.y - iDiamondSize, 5);
589 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
590 : edgomez 195 backupMV.y + iDiamondSize, 6);
591 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
592 : edgomez 195 backupMV.y + iDiamondSize, 8);
593 :    
594 :     break;
595 :    
596 :     case 7:
597 :     CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
598 :     backupMV.y, 1);
599 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
600 : edgomez 195 4);
601 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
602 : edgomez 195 backupMV.y - iDiamondSize, 5);
603 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
604 : edgomez 195 backupMV.y - iDiamondSize, 7);
605 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
606 : edgomez 195 backupMV.y + iDiamondSize, 8);
607 :     break;
608 :    
609 :     case 8:
610 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
611 : edgomez 195 2);
612 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
613 : edgomez 195 4);
614 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
615 : edgomez 195 backupMV.y + iDiamondSize, 6);
616 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
617 : edgomez 195 backupMV.y - iDiamondSize, 7);
618 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
619 : edgomez 195 backupMV.y + iDiamondSize, 8);
620 :     break;
621 : chl 96 default:
622 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
623 : edgomez 195 1);
624 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
625 : edgomez 195 2);
626 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
627 : edgomez 195 3);
628 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
629 : edgomez 195 4);
630 :    
631 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
632 : edgomez 195 backupMV.y - iDiamondSize, 5);
633 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
634 : edgomez 195 backupMV.y + iDiamondSize, 6);
635 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
636 : edgomez 195 backupMV.y - iDiamondSize, 7);
637 : chl 345 CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
638 : edgomez 195 backupMV.y + iDiamondSize, 8);
639 :     break;
640 : chl 96 }
641 : chl 345 }
642 : edgomez 195 } else {
643 : chl 326 currMV->x = start_x;
644 :     currMV->y = start_y;
645 : edgomez 195 }
646 : chl 96 return iMinSAD;
647 :     }
648 :    
649 :    
650 : edgomez 195 int32_t
651 :     Full16_MainSearch(const uint8_t * const pRef,
652 :     const uint8_t * const pRefH,
653 :     const uint8_t * const pRefV,
654 :     const uint8_t * const pRefHV,
655 :     const uint8_t * const cur,
656 :     const int x,
657 :     const int y,
658 : chl 326 const int start_x,
659 :     const int start_y,
660 :     int iMinSAD,
661 :     VECTOR * const currMV,
662 :     const int center_x,
663 :     const int center_y,
664 : edgomez 195 const int32_t min_dx,
665 :     const int32_t max_dx,
666 :     const int32_t min_dy,
667 :     const int32_t max_dy,
668 :     const int32_t iEdgedWidth,
669 :     const int32_t iDiamondSize,
670 :     const int32_t iFcode,
671 :     const int32_t iQuant,
672 :     int iFound)
673 : chl 96 {
674 :     int32_t iSAD;
675 : edgomez 195 int32_t dx, dy;
676 : chl 96 VECTOR backupMV;
677 : edgomez 195
678 : chl 326 backupMV.x = start_x;
679 :     backupMV.y = start_y;
680 : chl 96
681 : edgomez 195 for (dx = min_dx; dx <= max_dx; dx += iDiamondSize)
682 :     for (dy = min_dy; dy <= max_dy; dy += iDiamondSize)
683 :     NOCHECK_MV16_CANDIDATE(dx, dy);
684 :    
685 : chl 96 return iMinSAD;
686 :     }
687 :    
688 : edgomez 195 int32_t
689 :     AdvDiamond16_MainSearch(const uint8_t * const pRef,
690 :     const uint8_t * const pRefH,
691 :     const uint8_t * const pRefV,
692 :     const uint8_t * const pRefHV,
693 :     const uint8_t * const cur,
694 :     const int x,
695 :     const int y,
696 : chl 326 int start_x,
697 :     int start_y,
698 :     int iMinSAD,
699 :     VECTOR * const currMV,
700 :     const int center_x,
701 :     const int center_y,
702 : edgomez 195 const int32_t min_dx,
703 :     const int32_t max_dx,
704 :     const int32_t min_dy,
705 :     const int32_t max_dy,
706 :     const int32_t iEdgedWidth,
707 :     const int32_t iDiamondSize,
708 :     const int32_t iFcode,
709 :     const int32_t iQuant,
710 :     int iDirection)
711 : chl 181 {
712 :    
713 :     int32_t iSAD;
714 :    
715 :     /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
716 :    
717 : edgomez 195 if (iDirection) {
718 : chl 326 CHECK_MV16_CANDIDATE(start_x - iDiamondSize, start_y);
719 :     CHECK_MV16_CANDIDATE(start_x + iDiamondSize, start_y);
720 :     CHECK_MV16_CANDIDATE(start_x, start_y - iDiamondSize);
721 :     CHECK_MV16_CANDIDATE(start_x, start_y + iDiamondSize);
722 : edgomez 195 } else {
723 :     int bDirection = 1 + 2 + 4 + 8;
724 :    
725 :     do {
726 : chl 181 iDirection = 0;
727 : 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)
728 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
729 : chl 181
730 : edgomez 195 if (bDirection & 2)
731 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
732 : chl 181
733 : edgomez 195 if (bDirection & 4)
734 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
735 : chl 181
736 : edgomez 195 if (bDirection & 8)
737 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
738 : chl 181
739 :     /* now we're doing diagonal checks near our candidate */
740 :    
741 : edgomez 195 if (iDirection) //checking if anything found
742 : chl 181 {
743 :     bDirection = iDirection;
744 :     iDirection = 0;
745 : chl 326 start_x = currMV->x;
746 :     start_y = currMV->y;
747 : edgomez 195 if (bDirection & 3) //our candidate is left or right
748 : chl 181 {
749 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
750 :     CHECK_MV16_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
751 : edgomez 195 } else // what remains here is up or down
752 : chl 181 {
753 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
754 :     CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
755 : chl 181 }
756 :    
757 : edgomez 195 if (iDirection) {
758 :     bDirection += iDirection;
759 : chl 326 start_x = currMV->x;
760 :     start_y = currMV->y;
761 : chl 181 }
762 : edgomez 195 } else //about to quit, eh? not so fast....
763 : chl 181 {
764 : edgomez 195 switch (bDirection) {
765 : chl 181 case 2:
766 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
767 :     start_y - iDiamondSize, 2 + 4);
768 :     CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
769 :     start_y + iDiamondSize, 2 + 8);
770 : chl 181 break;
771 :     case 1:
772 : chl 326
773 :     CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
774 :     start_y - iDiamondSize, 1 + 4);
775 :     CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
776 :     start_y + iDiamondSize, 1 + 8);
777 : chl 181 break;
778 : edgomez 195 case 2 + 4:
779 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
780 :     start_y - iDiamondSize, 1 + 4);
781 :     CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
782 :     start_y - iDiamondSize, 2 + 4);
783 :     CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
784 :     start_y + iDiamondSize, 2 + 8);
785 : chl 181 break;
786 :     case 4:
787 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
788 :     start_y - iDiamondSize, 2 + 4);
789 :     CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
790 :     start_y - iDiamondSize, 1 + 4);
791 : chl 181 break;
792 :     case 8:
793 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
794 :     start_y + iDiamondSize, 2 + 8);
795 :     CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
796 :     start_y + iDiamondSize, 1 + 8);
797 : chl 181 break;
798 : edgomez 195 case 1 + 4:
799 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
800 :     start_y + iDiamondSize, 1 + 8);
801 :     CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
802 :     start_y - iDiamondSize, 1 + 4);
803 :     CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
804 :     start_y - iDiamondSize, 2 + 4);
805 : chl 181 break;
806 : edgomez 195 case 2 + 8:
807 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
808 :     start_y - iDiamondSize, 1 + 4);
809 :     CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
810 :     start_y + iDiamondSize, 1 + 8);
811 :     CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
812 :     start_y + iDiamondSize, 2 + 8);
813 : chl 181 break;
814 : edgomez 195 case 1 + 8:
815 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
816 :     start_y - iDiamondSize, 2 + 4);
817 :     CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
818 :     start_y + iDiamondSize, 2 + 8);
819 :     CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
820 :     start_y + iDiamondSize, 1 + 8);
821 : chl 181 break;
822 : edgomez 195 default: //1+2+4+8 == we didn't find anything at all
823 : chl 326 CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
824 :     start_y - iDiamondSize, 1 + 4);
825 :     CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
826 :     start_y + iDiamondSize, 1 + 8);
827 :     CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
828 :     start_y - iDiamondSize, 2 + 4);
829 :     CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
830 :     start_y + iDiamondSize, 2 + 8);
831 : chl 181 break;
832 :     }
833 : edgomez 195 if (!iDirection)
834 :     break; //ok, the end. really
835 :     else {
836 :     bDirection = iDirection;
837 : chl 326 start_x = currMV->x;
838 :     start_y = currMV->y;
839 : chl 181 }
840 :     }
841 :     }
842 : edgomez 195 while (1); //forever
843 : chl 181 }
844 :     return iMinSAD;
845 :     }
846 :    
847 : chl 346 #define CHECK_MV16_F_INTERPOL(X,Y) { \
848 :     if ( ((X) <= f_max_dx) && ((X) >= f_min_dx) \
849 :     && ((Y) <= f_max_dy) && ((Y) >= f_min_dy) ) \
850 : chl 326 { \
851 : chl 346 iSAD = sad16bi( cur, \
852 :     get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, X, Y, iEdgedWidth), \
853 :     get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, b_currMV->x, b_currMV->y, iEdgedWidth), \
854 :     iEdgedWidth); \
855 :     iSAD += calc_delta_16((X) - f_center_x, (Y) - f_center_y, (uint8_t)f_iFcode, iQuant);\
856 :     iSAD += calc_delta_16(b_currMV->x - b_center_x, b_currMV->y - b_center_y, (uint8_t)b_iFcode, iQuant);\
857 : chl 326 if (iSAD < iMinSAD) \
858 : chl 346 { iMinSAD=iSAD; f_currMV->x=(X); f_currMV->y=(Y); } } \
859 : chl 326 }
860 :    
861 : chl 346 #define CHECK_MV16_F_INTERPOL_FOUND(X,Y) { \
862 :     if ( ((X) <= f_max_dx) && ((X) >= f_min_dx) \
863 :     && ((Y) <= f_max_dy) && ((Y) >= f_min_dy) ) \
864 : chl 326 { \
865 : chl 346 iSAD = sad16bi( cur, \
866 :     get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, X, Y, iEdgedWidth), \
867 :     get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, b_currMV->x, b_currMV->y, iEdgedWidth), \
868 :     iEdgedWidth); \
869 :     iSAD += calc_delta_16((X) - f_center_x, (Y) - f_center_y, (uint8_t)f_iFcode, iQuant);\
870 :     iSAD += calc_delta_16(b_currMV->x - b_center_x, b_currMV->y - b_center_y, (uint8_t)b_iFcode, iQuant);\
871 : chl 326 if (iSAD < iMinSAD) \
872 : chl 346 { iMinSAD=iSAD; f_currMV->x=(X); f_currMV->y=(Y); iFound=0;} } \
873 : chl 326 }
874 :    
875 : chl 346 #define CHECK_MV16_B_INTERPOL(X,Y) { \
876 :     if ( ((X) <= b_max_dx) && ((X) >= b_min_dx) \
877 :     && ((Y) <= b_max_dy) && ((Y) >= b_min_dy) ) \
878 : chl 326 { \
879 : chl 346 iSAD = sad16bi( cur, \
880 :     get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, f_currMV->x, f_currMV->y, iEdgedWidth), \
881 :     get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, X, Y, iEdgedWidth), \
882 :     iEdgedWidth); \
883 :     iSAD += calc_delta_16(f_currMV->x - f_center_x, f_currMV->y - f_center_y, (uint8_t)f_iFcode, iQuant);\
884 :     iSAD += calc_delta_16((X) - b_center_x, (Y) - b_center_y, (uint8_t)b_iFcode, iQuant);\
885 : chl 326 if (iSAD < iMinSAD) \
886 : chl 346 { iMinSAD=iSAD; b_currMV->x=(X); b_currMV->y=(Y); } } \
887 : chl 326 }
888 :    
889 : chl 346 #define CHECK_MV16_B_INTERPOL_FOUND(X,Y) { \
890 :     if ( ((X) <= b_max_dx) && ((X) >= b_min_dx) \
891 :     && ((Y) <= b_max_dy) && ((Y) >= b_min_dy) ) \
892 : chl 326 { \
893 : chl 346 iSAD = sad16bi( cur, \
894 :     get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, f_currMV->x, f_currMV->y, iEdgedWidth), \
895 :     get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, X, Y, iEdgedWidth), \
896 :     iEdgedWidth); \
897 :     iSAD += calc_delta_16(f_currMV->x - f_center_x, f_currMV->y - f_center_y, (uint8_t)f_iFcode, iQuant);\
898 :     iSAD += calc_delta_16((X) - b_center_x, (Y) - b_center_y, (uint8_t)b_iFcode, iQuant);\
899 : chl 326 if (iSAD < iMinSAD) \
900 : chl 346 { iMinSAD=iSAD; b_currMV->x=(X); b_currMV->y=(Y); iFound=0;} } \
901 : chl 326 }
902 :    
903 : edgomez 195 int32_t
904 : chl 326 Diamond16_InterpolMainSearch(
905 :     const uint8_t * const f_pRef,
906 :     const uint8_t * const f_pRefH,
907 :     const uint8_t * const f_pRefV,
908 :     const uint8_t * const f_pRefHV,
909 : chl 346
910 : chl 326 const uint8_t * const cur,
911 :    
912 :     const uint8_t * const b_pRef,
913 :     const uint8_t * const b_pRefH,
914 :     const uint8_t * const b_pRefV,
915 :     const uint8_t * const b_pRefHV,
916 :    
917 :     const int x,
918 :     const int y,
919 :    
920 :     const int f_start_x,
921 :     const int f_start_y,
922 :     const int b_start_x,
923 :     const int b_start_y,
924 :    
925 :     int iMinSAD,
926 :     VECTOR * const f_currMV,
927 :     VECTOR * const b_currMV,
928 :    
929 :     const int f_center_x,
930 :     const int f_center_y,
931 :     const int b_center_x,
932 :     const int b_center_y,
933 :    
934 : chl 346 const int32_t f_min_dx,
935 :     const int32_t f_max_dx,
936 :     const int32_t f_min_dy,
937 :     const int32_t f_max_dy,
938 : chl 326
939 : chl 346 const int32_t b_min_dx,
940 :     const int32_t b_max_dx,
941 :     const int32_t b_min_dy,
942 :     const int32_t b_max_dy,
943 : chl 326
944 : chl 346 const int32_t iEdgedWidth,
945 :     const int32_t iDiamondSize,
946 :    
947 :     const int32_t f_iFcode,
948 :     const int32_t b_iFcode,
949 :    
950 :     const int32_t iQuant,
951 :     int iFound)
952 : chl 326 {
953 :     /* Do a diamond search around given starting point, return SAD of best */
954 :    
955 :     int32_t iSAD;
956 :    
957 :     VECTOR f_backupMV;
958 :     VECTOR b_backupMV;
959 :    
960 : chl 346 f_currMV->x = f_start_x;
961 :     f_currMV->y = f_start_y;
962 :     b_currMV->x = b_start_x;
963 :     b_currMV->y = b_start_y;
964 : chl 326
965 : chl 346 do
966 :     {
967 :     iFound = 1;
968 :    
969 :     f_backupMV = *f_currMV;
970 :    
971 :     CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x - iDiamondSize, f_backupMV.y);
972 :     CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x + iDiamondSize, f_backupMV.y);
973 :     CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x, f_backupMV.y - iDiamondSize);
974 :     CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x, f_backupMV.y + iDiamondSize);
975 :    
976 :     b_backupMV = *b_currMV;
977 :    
978 :     CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x - iDiamondSize, b_backupMV.y);
979 :     CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x + iDiamondSize, b_backupMV.y);
980 :     CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x, b_backupMV.y - iDiamondSize);
981 :     CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x, b_backupMV.y + iDiamondSize);
982 :    
983 :     } while (!iFound);
984 :    
985 :     return iMinSAD;
986 :     }
987 :    
988 :     /* Sorry, these MACROS really got too large... I'll turn them into function soon! */
989 :    
990 :     #define CHECK_MV16_DIRECT_FOUND(X,Y) \
991 :     if ( (X)>=(-32) && (X)<=(31) && ((Y)>=-32) && ((Y)<=31) ) \
992 :     { int k;\
993 :     VECTOR mvs,b_mvs; \
994 :     iSAD = 0;\
995 :     for (k = 0; k < 4; k++) { \
996 :     mvs.x = (int32_t) ((TRB * directmv[k].x) / TRD + (X)); \
997 :     b_mvs.x = (int32_t) (((X) == 0) \
998 :     ? ((TRB - TRD) * directmv[k].x) / TRD \
999 :     : mvs.x - directmv[k].x); \
1000 :     \
1001 :     mvs.y = (int32_t) ((TRB * directmv[k].y) / TRD + (Y)); \
1002 :     b_mvs.y = (int32_t) (((Y) == 0) \
1003 :     ? ((TRB - TRD) * directmv[k].y) / TRD \
1004 :     : mvs.y - directmv[k].y); \
1005 :     \
1006 :     if ( (mvs.x <= max_dx) && (mvs.x >= min_dx) \
1007 :     && (mvs.y <= max_dy) && (mvs.y >= min_dy) \
1008 :     && (b_mvs.x <= max_dx) && (b_mvs.x >= min_dx) \
1009 :     && (b_mvs.y <= max_dy) && (b_mvs.y >= min_dy) ) { \
1010 :     iSAD += sad8bi( cur + 8*(k&1) + 8*(k>>1)*iEdgedWidth, \
1011 :     get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, 2*x+(k&1), 2*y+(k>>1), 8, \
1012 :     mvs.x, mvs.y, iEdgedWidth), \
1013 :     get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, 2*x+(k&1), 2*y+(k>>1), 8, \
1014 :     b_mvs.x, b_mvs.y, iEdgedWidth), \
1015 :     iEdgedWidth); \
1016 :     } \
1017 :     else \
1018 :     iSAD = 65535; \
1019 :     } \
1020 :     iSAD += calc_delta_16((X),(Y), 1, iQuant);\
1021 :     if (iSAD < iMinSAD) \
1022 :     { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iFound=0; } \
1023 :     }
1024 :    
1025 :    
1026 :    
1027 :     int32_t
1028 :     Diamond16_DirectMainSearch(
1029 :     const uint8_t * const f_pRef,
1030 :     const uint8_t * const f_pRefH,
1031 :     const uint8_t * const f_pRefV,
1032 :     const uint8_t * const f_pRefHV,
1033 :    
1034 :     const uint8_t * const cur,
1035 :    
1036 :     const uint8_t * const b_pRef,
1037 :     const uint8_t * const b_pRefH,
1038 :     const uint8_t * const b_pRefV,
1039 :     const uint8_t * const b_pRefHV,
1040 :    
1041 :     const int x,
1042 :     const int y,
1043 :    
1044 :     const int TRB,
1045 :     const int TRD,
1046 :    
1047 :     const int start_x,
1048 :     const int start_y,
1049 :    
1050 :     int iMinSAD,
1051 :     VECTOR * const currMV,
1052 :     const VECTOR * const directmv,
1053 :    
1054 :     const int32_t min_dx,
1055 :     const int32_t max_dx,
1056 :     const int32_t min_dy,
1057 :     const int32_t max_dy,
1058 :    
1059 :     const int32_t iEdgedWidth,
1060 :     const int32_t iDiamondSize,
1061 :    
1062 :     const int32_t iQuant,
1063 :     int iFound)
1064 :     {
1065 :     /* Do a diamond search around given starting point, return SAD of best */
1066 :    
1067 :     int32_t iSAD;
1068 :    
1069 :     VECTOR backupMV;
1070 :    
1071 :     currMV->x = start_x;
1072 :     currMV->y = start_y;
1073 :    
1074 : chl 326 /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
1075 :    
1076 : chl 346 do
1077 :     {
1078 :     iFound = 1;
1079 :    
1080 :     backupMV = *currMV;
1081 :    
1082 :     CHECK_MV16_DIRECT_FOUND(backupMV.x - iDiamondSize, backupMV.y);
1083 :     CHECK_MV16_DIRECT_FOUND(backupMV.x + iDiamondSize, backupMV.y);
1084 :     CHECK_MV16_DIRECT_FOUND(backupMV.x, backupMV.y - iDiamondSize);
1085 :     CHECK_MV16_DIRECT_FOUND(backupMV.x, backupMV.y + iDiamondSize);
1086 : chl 326
1087 : chl 346 } while (!iFound);
1088 : chl 326
1089 :     return iMinSAD;
1090 :     }
1091 :    
1092 :    
1093 :     int32_t
1094 : edgomez 195 AdvDiamond8_MainSearch(const uint8_t * const pRef,
1095 :     const uint8_t * const pRefH,
1096 :     const uint8_t * const pRefV,
1097 :     const uint8_t * const pRefHV,
1098 :     const uint8_t * const cur,
1099 :     const int x,
1100 :     const int y,
1101 : chl 326 int start_x,
1102 :     int start_y,
1103 :     int iMinSAD,
1104 : edgomez 195 VECTOR * const currMV,
1105 : chl 326 const int center_x,
1106 :     const int center_y,
1107 : edgomez 195 const int32_t min_dx,
1108 :     const int32_t max_dx,
1109 :     const int32_t min_dy,
1110 :     const int32_t max_dy,
1111 :     const int32_t iEdgedWidth,
1112 :     const int32_t iDiamondSize,
1113 :     const int32_t iFcode,
1114 :     const int32_t iQuant,
1115 :     int iDirection)
1116 : chl 181 {
1117 :    
1118 :     int32_t iSAD;
1119 :    
1120 :     /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
1121 :    
1122 : edgomez 195 if (iDirection) {
1123 : chl 326 CHECK_MV8_CANDIDATE(start_x - iDiamondSize, start_y);
1124 :     CHECK_MV8_CANDIDATE(start_x + iDiamondSize, start_y);
1125 :     CHECK_MV8_CANDIDATE(start_x, start_y - iDiamondSize);
1126 :     CHECK_MV8_CANDIDATE(start_x, start_y + iDiamondSize);
1127 : edgomez 195 } else {
1128 :     int bDirection = 1 + 2 + 4 + 8;
1129 :    
1130 :     do {
1131 : chl 181 iDirection = 0;
1132 : 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)
1133 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
1134 : chl 181
1135 : edgomez 195 if (bDirection & 2)
1136 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
1137 : chl 181
1138 : edgomez 195 if (bDirection & 4)
1139 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
1140 : chl 181
1141 : edgomez 195 if (bDirection & 8)
1142 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
1143 : chl 181
1144 :     /* now we're doing diagonal checks near our candidate */
1145 :    
1146 : edgomez 195 if (iDirection) //checking if anything found
1147 : chl 181 {
1148 :     bDirection = iDirection;
1149 :     iDirection = 0;
1150 : chl 326 start_x = currMV->x;
1151 :     start_y = currMV->y;
1152 : edgomez 195 if (bDirection & 3) //our candidate is left or right
1153 : chl 181 {
1154 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
1155 :     CHECK_MV8_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
1156 : edgomez 195 } else // what remains here is up or down
1157 : chl 181 {
1158 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
1159 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
1160 : chl 181 }
1161 :    
1162 : edgomez 195 if (iDirection) {
1163 :     bDirection += iDirection;
1164 : chl 326 start_x = currMV->x;
1165 :     start_y = currMV->y;
1166 : chl 181 }
1167 : edgomez 195 } else //about to quit, eh? not so fast....
1168 : chl 181 {
1169 : edgomez 195 switch (bDirection) {
1170 : chl 181 case 2:
1171 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1172 :     start_y - iDiamondSize, 2 + 4);
1173 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1174 :     start_y + iDiamondSize, 2 + 8);
1175 : chl 181 break;
1176 :     case 1:
1177 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1178 :     start_y - iDiamondSize, 1 + 4);
1179 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1180 :     start_y + iDiamondSize, 1 + 8);
1181 : chl 181 break;
1182 : edgomez 195 case 2 + 4:
1183 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1184 :     start_y - iDiamondSize, 1 + 4);
1185 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1186 :     start_y - iDiamondSize, 2 + 4);
1187 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1188 :     start_y + iDiamondSize, 2 + 8);
1189 : chl 181 break;
1190 :     case 4:
1191 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1192 :     start_y - iDiamondSize, 2 + 4);
1193 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1194 :     start_y - iDiamondSize, 1 + 4);
1195 : chl 181 break;
1196 :     case 8:
1197 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1198 :     start_y + iDiamondSize, 2 + 8);
1199 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1200 :     start_y + iDiamondSize, 1 + 8);
1201 : chl 181 break;
1202 : edgomez 195 case 1 + 4:
1203 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1204 :     start_y + iDiamondSize, 1 + 8);
1205 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1206 :     start_y - iDiamondSize, 1 + 4);
1207 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1208 :     start_y - iDiamondSize, 2 + 4);
1209 : chl 181 break;
1210 : edgomez 195 case 2 + 8:
1211 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1212 :     start_y - iDiamondSize, 1 + 4);
1213 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1214 :     start_y + iDiamondSize, 1 + 8);
1215 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1216 :     start_y + iDiamondSize, 2 + 8);
1217 : chl 181 break;
1218 : edgomez 195 case 1 + 8:
1219 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1220 :     start_y - iDiamondSize, 2 + 4);
1221 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1222 :     start_y + iDiamondSize, 2 + 8);
1223 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1224 :     start_y + iDiamondSize, 1 + 8);
1225 : chl 181 break;
1226 : edgomez 195 default: //1+2+4+8 == we didn't find anything at all
1227 : chl 326 CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1228 :     start_y - iDiamondSize, 1 + 4);
1229 :     CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1230 :     start_y + iDiamondSize, 1 + 8);
1231 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1232 :     start_y - iDiamondSize, 2 + 4);
1233 :     CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1234 :     start_y + iDiamondSize, 2 + 8);
1235 : chl 181 break;
1236 :     }
1237 : edgomez 195 if (!(iDirection))
1238 :     break; //ok, the end. really
1239 :     else {
1240 :     bDirection = iDirection;
1241 : chl 326 start_x = currMV->x;
1242 :     start_y = currMV->y;
1243 : chl 181 }
1244 :     }
1245 :     }
1246 : edgomez 195 while (1); //forever
1247 : chl 181 }
1248 :     return iMinSAD;
1249 :     }
1250 :    
1251 :    
1252 : edgomez 195 int32_t
1253 :     Full8_MainSearch(const uint8_t * const pRef,
1254 :     const uint8_t * const pRefH,
1255 :     const uint8_t * const pRefV,
1256 :     const uint8_t * const pRefHV,
1257 :     const uint8_t * const cur,
1258 :     const int x,
1259 :     const int y,
1260 : chl 326 const int start_x,
1261 :     const int start_y,
1262 :     int iMinSAD,
1263 :     VECTOR * const currMV,
1264 :     const int center_x,
1265 :     const int center_y,
1266 : edgomez 195 const int32_t min_dx,
1267 :     const int32_t max_dx,
1268 :     const int32_t min_dy,
1269 :     const int32_t max_dy,
1270 :     const int32_t iEdgedWidth,
1271 :     const int32_t iDiamondSize,
1272 :     const int32_t iFcode,
1273 :     const int32_t iQuant,
1274 :     int iFound)
1275 : chl 96 {
1276 :     int32_t iSAD;
1277 : edgomez 195 int32_t dx, dy;
1278 : chl 96 VECTOR backupMV;
1279 : edgomez 195
1280 : chl 326 backupMV.x = start_x;
1281 :     backupMV.y = start_y;
1282 : chl 96
1283 : edgomez 195 for (dx = min_dx; dx <= max_dx; dx += iDiamondSize)
1284 :     for (dy = min_dy; dy <= max_dy; dy += iDiamondSize)
1285 :     NOCHECK_MV8_CANDIDATE(dx, dy);
1286 :    
1287 : chl 96 return iMinSAD;
1288 :     }
1289 :    
1290 : ia64p 300 Halfpel8_RefineFuncPtr Halfpel8_Refine;
1291 : chl 96
1292 : edgomez 195 int32_t
1293 :     Halfpel16_Refine(const uint8_t * const pRef,
1294 :     const uint8_t * const pRefH,
1295 :     const uint8_t * const pRefV,
1296 :     const uint8_t * const pRefHV,
1297 :     const uint8_t * const cur,
1298 :     const int x,
1299 :     const int y,
1300 :     VECTOR * const currMV,
1301 :     int32_t iMinSAD,
1302 : chl 326 const int center_x,
1303 :     const int center_y,
1304 : edgomez 195 const int32_t min_dx,
1305 :     const int32_t max_dx,
1306 :     const int32_t min_dy,
1307 :     const int32_t max_dy,
1308 :     const int32_t iFcode,
1309 :     const int32_t iQuant,
1310 :     const int32_t iEdgedWidth)
1311 : Isibaar 3 {
1312 :     /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */
1313 :    
1314 :     int32_t iSAD;
1315 :     VECTOR backupMV = *currMV;
1316 : edgomez 195
1317 :     CHECK_MV16_CANDIDATE(backupMV.x - 1, backupMV.y - 1);
1318 :     CHECK_MV16_CANDIDATE(backupMV.x, backupMV.y - 1);
1319 :     CHECK_MV16_CANDIDATE(backupMV.x + 1, backupMV.y - 1);
1320 :     CHECK_MV16_CANDIDATE(backupMV.x - 1, backupMV.y);
1321 :     CHECK_MV16_CANDIDATE(backupMV.x + 1, backupMV.y);
1322 :     CHECK_MV16_CANDIDATE(backupMV.x - 1, backupMV.y + 1);
1323 :     CHECK_MV16_CANDIDATE(backupMV.x, backupMV.y + 1);
1324 :     CHECK_MV16_CANDIDATE(backupMV.x + 1, backupMV.y + 1);
1325 :    
1326 : Isibaar 3 return iMinSAD;
1327 :     }
1328 :    
1329 :     #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16)
1330 :    
1331 : chl 96
1332 : chl 326
1333 : edgomez 195 int32_t
1334 :     PMVfastSearch16(const uint8_t * const pRef,
1335 :     const uint8_t * const pRefH,
1336 :     const uint8_t * const pRefV,
1337 :     const uint8_t * const pRefHV,
1338 :     const IMAGE * const pCur,
1339 :     const int x,
1340 :     const int y,
1341 : chl 346 const int start_x, /* start is searched first, so it should contain the most */
1342 :     const int start_y, /* likely motion vector for this block */
1343 :     const int center_x, /* center is from where length of MVs is measured */
1344 : chl 326 const int center_y,
1345 : edgomez 195 const uint32_t MotionFlags,
1346 :     const uint32_t iQuant,
1347 :     const uint32_t iFcode,
1348 :     const MBParam * const pParam,
1349 :     const MACROBLOCK * const pMBs,
1350 :     const MACROBLOCK * const prevMBs,
1351 :     VECTOR * const currMV,
1352 :     VECTOR * const currPMV)
1353 : Isibaar 3 {
1354 : edgomez 195 const uint32_t iWcount = pParam->mb_width;
1355 : Isibaar 3 const int32_t iWidth = pParam->width;
1356 :     const int32_t iHeight = pParam->height;
1357 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
1358 : Isibaar 3
1359 : edgomez 195 const uint8_t *cur = pCur->y + x * 16 + y * 16 * iEdgedWidth;
1360 : Isibaar 3
1361 :     int32_t iDiamondSize;
1362 : edgomez 195
1363 : Isibaar 3 int32_t min_dx;
1364 :     int32_t max_dx;
1365 :     int32_t min_dy;
1366 :     int32_t max_dy;
1367 : edgomez 195
1368 : Isibaar 3 int32_t iFound;
1369 :    
1370 :     VECTOR newMV;
1371 : edgomez 195 VECTOR backupMV; /* just for PMVFAST */
1372 :    
1373 : Isibaar 3 VECTOR pmv[4];
1374 :     int32_t psad[4];
1375 : chl 181
1376 :     MainSearch16FuncPtr MainSearchPtr;
1377 : Isibaar 3
1378 : edgomez 195 const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;
1379 : Isibaar 3
1380 : chl 259 int32_t threshA, threshB;
1381 : edgomez 195 int32_t bPredEq;
1382 :     int32_t iMinSAD, iSAD;
1383 :    
1384 : Isibaar 3 /* Get maximum range */
1385 : edgomez 195 get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight,
1386 :     iFcode);
1387 : Isibaar 3
1388 :     /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */
1389 :    
1390 : edgomez 195 if (!(MotionFlags & PMV_HALFPEL16)) {
1391 :     min_dx = EVEN(min_dx);
1392 :     max_dx = EVEN(max_dx);
1393 :     min_dy = EVEN(min_dy);
1394 :     max_dy = EVEN(max_dy);
1395 :     }
1396 : Isibaar 3
1397 : edgomez 195 /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
1398 : chl 285 bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad);
1399 : Isibaar 3
1400 : edgomez 195 if ((x == 0) && (y == 0)) {
1401 :     threshA = 512;
1402 : Isibaar 3 threshB = 1024;
1403 : edgomez 195 } else {
1404 : Isibaar 3 threshA = psad[0];
1405 : edgomez 195 threshB = threshA + 256;
1406 :     if (threshA < 512)
1407 :     threshA = 512;
1408 :     if (threshA > 1024)
1409 :     threshA = 1024;
1410 :     if (threshB > 1792)
1411 :     threshB = 1792;
1412 : Isibaar 3 }
1413 :    
1414 : edgomez 195 iFound = 0;
1415 :    
1416 : Isibaar 3 /* Step 4: Calculate SAD around the Median prediction.
1417 : edgomez 78 MinSAD=SAD
1418 :     If Motion Vector equal to Previous frame motion vector
1419 :     and MinSAD<PrevFrmSAD goto Step 10.
1420 :     If SAD<=256 goto Step 10.
1421 : edgomez 195 */
1422 : Isibaar 3
1423 : chl 326 currMV->x = start_x;
1424 :     currMV->y = start_y;
1425 :    
1426 : edgomez 195 if (!(MotionFlags & PMV_HALFPEL16)) { /* This should NOT be necessary! */
1427 : Isibaar 3 currMV->x = EVEN(currMV->x);
1428 :     currMV->y = EVEN(currMV->y);
1429 :     }
1430 : edgomez 195
1431 :     if (currMV->x > max_dx) {
1432 :     currMV->x = max_dx;
1433 : edgomez 78 }
1434 : edgomez 195 if (currMV->x < min_dx) {
1435 :     currMV->x = min_dx;
1436 : edgomez 78 }
1437 : edgomez 195 if (currMV->y > max_dy) {
1438 :     currMV->y = max_dy;
1439 : edgomez 78 }
1440 : edgomez 195 if (currMV->y < min_dy) {
1441 :     currMV->y = min_dy;
1442 : edgomez 78 }
1443 : edgomez 195
1444 :     iMinSAD =
1445 :     sad16(cur,
1446 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV,
1447 :     iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);
1448 :     iMinSAD +=
1449 : chl 326 calc_delta_16(currMV->x - center_x, currMV->y - center_y,
1450 : edgomez 195 (uint8_t) iFcode, iQuant);
1451 :    
1452 :     if ((iMinSAD < 256) ||
1453 :     ((MVequal(*currMV, prevMB->mvs[0])) &&
1454 : chl 289 ((int32_t) iMinSAD < prevMB->sad16))) {
1455 : edgomez 195 if (iMinSAD < 2 * iQuant) // high chances for SKIP-mode
1456 :     {
1457 :     if (!MVzero(*currMV)) {
1458 : chl 169 iMinSAD += MV16_00_BIAS;
1459 : edgomez 195 CHECK_MV16_ZERO; // (0,0) saves space for letterboxed pictures
1460 : chl 169 iMinSAD -= MV16_00_BIAS;
1461 :     }
1462 :     }
1463 :    
1464 : edgomez 195 if (MotionFlags & PMV_QUICKSTOP16)
1465 : chl 96 goto PMVfast16_Terminate_without_Refine;
1466 :     if (MotionFlags & PMV_EARLYSTOP16)
1467 :     goto PMVfast16_Terminate_with_Refine;
1468 : edgomez 78 }
1469 : Isibaar 3
1470 : chl 169
1471 :     /* Step 2 (lazy eval): Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion
1472 :     vector of the median.
1473 :     If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2
1474 :     */
1475 :    
1476 : edgomez 195 if ((bPredEq) && (MVequal(pmv[0], prevMB->mvs[0])))
1477 :     iFound = 2;
1478 : chl 169
1479 :     /* Step 3 (lazy eval): If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search.
1480 :     Otherwise select large Diamond Search.
1481 :     */
1482 :    
1483 : edgomez 195 if ((!MVzero(pmv[0])) || (threshB < 1536) || (bPredEq))
1484 :     iDiamondSize = 1; // halfpel!
1485 : chl 169 else
1486 : edgomez 195 iDiamondSize = 2; // halfpel!
1487 : chl 169
1488 : edgomez 195 if (!(MotionFlags & PMV_HALFPELDIAMOND16))
1489 :     iDiamondSize *= 2;
1490 : chl 169
1491 : Isibaar 3 /*
1492 : edgomez 78 Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block.
1493 :     Also calculate (0,0) but do not subtract offset.
1494 :     Let MinSAD be the smallest SAD up to this point.
1495 : chl 140 If MV is (0,0) subtract offset.
1496 : Isibaar 3 */
1497 :    
1498 :     // (0,0) is always possible
1499 :    
1500 : chl 169 if (!MVzero(pmv[0]))
1501 :     CHECK_MV16_ZERO;
1502 : Isibaar 3
1503 :     // previous frame MV is always possible
1504 : chl 169
1505 :     if (!MVzero(prevMB->mvs[0]))
1506 : edgomez 195 if (!MVequal(prevMB->mvs[0], pmv[0]))
1507 :     CHECK_MV16_CANDIDATE(prevMB->mvs[0].x, prevMB->mvs[0].y);
1508 :    
1509 : Isibaar 3 // left neighbour, if allowed
1510 : chl 169
1511 :     if (!MVzero(pmv[1]))
1512 : edgomez 195 if (!MVequal(pmv[1], prevMB->mvs[0]))
1513 :     if (!MVequal(pmv[1], pmv[0])) {
1514 :     if (!(MotionFlags & PMV_HALFPEL16)) {
1515 :     pmv[1].x = EVEN(pmv[1].x);
1516 :     pmv[1].y = EVEN(pmv[1].y);
1517 :     }
1518 : chl 169
1519 : edgomez 195 CHECK_MV16_CANDIDATE(pmv[1].x, pmv[1].y);
1520 :     }
1521 : Isibaar 3 // top neighbour, if allowed
1522 : chl 169 if (!MVzero(pmv[2]))
1523 : edgomez 195 if (!MVequal(pmv[2], prevMB->mvs[0]))
1524 :     if (!MVequal(pmv[2], pmv[0]))
1525 :     if (!MVequal(pmv[2], pmv[1])) {
1526 :     if (!(MotionFlags & PMV_HALFPEL16)) {
1527 :     pmv[2].x = EVEN(pmv[2].x);
1528 :     pmv[2].y = EVEN(pmv[2].y);
1529 :     }
1530 :     CHECK_MV16_CANDIDATE(pmv[2].x, pmv[2].y);
1531 :    
1532 : Isibaar 3 // top right neighbour, if allowed
1533 : edgomez 195 if (!MVzero(pmv[3]))
1534 :     if (!MVequal(pmv[3], prevMB->mvs[0]))
1535 :     if (!MVequal(pmv[3], pmv[0]))
1536 :     if (!MVequal(pmv[3], pmv[1]))
1537 :     if (!MVequal(pmv[3], pmv[2])) {
1538 :     if (!(MotionFlags & PMV_HALFPEL16)) {
1539 :     pmv[3].x = EVEN(pmv[3].x);
1540 :     pmv[3].y = EVEN(pmv[3].y);
1541 :     }
1542 :     CHECK_MV16_CANDIDATE(pmv[3].x,
1543 :     pmv[3].y);
1544 :     }
1545 :     }
1546 :    
1547 :     if ((MVzero(*currMV)) &&
1548 :     (!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96) */ )
1549 : chl 140 iMinSAD -= MV16_00_BIAS;
1550 : Isibaar 3
1551 : edgomez 195
1552 : Isibaar 3 /* Step 6: If MinSAD <= thresa goto Step 10.
1553 :     If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10.
1554 :     */
1555 :    
1556 : edgomez 195 if ((iMinSAD <= threshA) ||
1557 :     (MVequal(*currMV, prevMB->mvs[0]) &&
1558 : chl 289 ((int32_t) iMinSAD < prevMB->sad16))) {
1559 : edgomez 195 if (MotionFlags & PMV_QUICKSTOP16)
1560 : chl 96 goto PMVfast16_Terminate_without_Refine;
1561 :     if (MotionFlags & PMV_EARLYSTOP16)
1562 :     goto PMVfast16_Terminate_with_Refine;
1563 : edgomez 78 }
1564 : Isibaar 3
1565 :    
1566 :     /************ (Diamond Search) **************/
1567 :     /*
1568 : edgomez 78 Step 7: Perform Diamond search, with either the small or large diamond.
1569 :     If Found=2 only examine one Diamond pattern, and afterwards goto step 10
1570 :     Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond.
1571 :     If center then goto step 10.
1572 :     Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center.
1573 :     Refine by using small diamond and goto step 10.
1574 : Isibaar 3 */
1575 :    
1576 : chl 181 if (MotionFlags & PMV_USESQUARES16)
1577 :     MainSearchPtr = Square16_MainSearch;
1578 : edgomez 195 else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
1579 :     MainSearchPtr = AdvDiamond16_MainSearch;
1580 : chl 181 else
1581 : edgomez 195 MainSearchPtr = Diamond16_MainSearch;
1582 : chl 181
1583 : edgomez 195 backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
1584 : Isibaar 3
1585 : chl 259
1586 : Isibaar 3 /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
1587 : edgomez 195 iSAD =
1588 : chl 326 (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
1589 :     currMV->x, currMV->y, iMinSAD, &newMV, center_x, center_y,
1590 :     min_dx, max_dx,
1591 : edgomez 195 min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
1592 :     iQuant, iFound);
1593 :    
1594 :     if (iSAD < iMinSAD) {
1595 : Isibaar 3 *currMV = newMV;
1596 :     iMinSAD = iSAD;
1597 :     }
1598 :    
1599 : edgomez 195 if (MotionFlags & PMV_EXTSEARCH16) {
1600 : Isibaar 3 /* extended: search (up to) two more times: orignal prediction and (0,0) */
1601 :    
1602 : edgomez 195 if (!(MVequal(pmv[0], backupMV))) {
1603 :     iSAD =
1604 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
1605 : chl 326 center_x, center_y, iMinSAD, &newMV, center_x, center_y,
1606 : edgomez 195 min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
1607 :     iDiamondSize, iFcode, iQuant, iFound);
1608 :    
1609 :     if (iSAD < iMinSAD) {
1610 :     *currMV = newMV;
1611 :     iMinSAD = iSAD;
1612 :     }
1613 : Isibaar 3 }
1614 :    
1615 : edgomez 195 if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
1616 :     iSAD =
1617 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
1618 : chl 326 iMinSAD, &newMV, center_x, center_y,
1619 :     min_dx, max_dx, min_dy, max_dy,
1620 :     iEdgedWidth, iDiamondSize, iFcode,
1621 : edgomez 195 iQuant, iFound);
1622 :    
1623 :     if (iSAD < iMinSAD) {
1624 :     *currMV = newMV;
1625 :     iMinSAD = iSAD;
1626 :     }
1627 : Isibaar 3 }
1628 :     }
1629 :    
1630 :     /*
1631 : edgomez 78 Step 10: The motion vector is chosen according to the block corresponding to MinSAD.
1632 : Isibaar 3 */
1633 :    
1634 : edgomez 195 PMVfast16_Terminate_with_Refine:
1635 :     if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step
1636 :     iMinSAD =
1637 :     Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
1638 : chl 326 iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
1639 : edgomez 195 iFcode, iQuant, iEdgedWidth);
1640 : Isibaar 3
1641 : edgomez 195 PMVfast16_Terminate_without_Refine:
1642 : chl 326 currPMV->x = currMV->x - center_x;
1643 :     currPMV->y = currMV->y - center_y;
1644 : Isibaar 3 return iMinSAD;
1645 :     }
1646 :    
1647 :    
1648 :    
1649 :    
1650 :    
1651 :    
1652 : edgomez 195 int32_t
1653 :     Diamond8_MainSearch(const uint8_t * const pRef,
1654 :     const uint8_t * const pRefH,
1655 :     const uint8_t * const pRefV,
1656 :     const uint8_t * const pRefHV,
1657 :     const uint8_t * const cur,
1658 :     const int x,
1659 :     const int y,
1660 : chl 326 int32_t start_x,
1661 :     int32_t start_y,
1662 : edgomez 195 int32_t iMinSAD,
1663 :     VECTOR * const currMV,
1664 : chl 326 const int center_x,
1665 :     const int center_y,
1666 : edgomez 195 const int32_t min_dx,
1667 :     const int32_t max_dx,
1668 :     const int32_t min_dy,
1669 :     const int32_t max_dy,
1670 :     const int32_t iEdgedWidth,
1671 :     const int32_t iDiamondSize,
1672 :     const int32_t iFcode,
1673 :     const int32_t iQuant,
1674 :     int iFound)
1675 : Isibaar 3 {
1676 :     /* Do a diamond search around given starting point, return SAD of best */
1677 :    
1678 : edgomez 195 int32_t iDirection = 0;
1679 : chl 344 int32_t iDirectionBackup;
1680 : Isibaar 3 int32_t iSAD;
1681 :     VECTOR backupMV;
1682 : edgomez 195
1683 : chl 326 backupMV.x = start_x;
1684 :     backupMV.y = start_y;
1685 : edgomez 195
1686 : Isibaar 3 /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
1687 :    
1688 : edgomez 195 CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
1689 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
1690 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
1691 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
1692 : Isibaar 3
1693 : chl 344 if (iDirection) {
1694 : edgomez 195 while (!iFound) {
1695 :     iFound = 1;
1696 :     backupMV = *currMV; // since iDirection!=0, this is well defined!
1697 : chl 344 iDirectionBackup = iDirection;
1698 : edgomez 195
1699 : chl 344 if (iDirectionBackup != 2)
1700 : edgomez 195 CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1701 :     backupMV.y, 1);
1702 : chl 344 if (iDirectionBackup != 1)
1703 : edgomez 195 CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1704 :     backupMV.y, 2);
1705 : chl 344 if (iDirectionBackup != 4)
1706 : edgomez 195 CHECK_MV8_CANDIDATE_FOUND(backupMV.x,
1707 :     backupMV.y - iDiamondSize, 3);
1708 : chl 344 if (iDirectionBackup != 3)
1709 : edgomez 195 CHECK_MV8_CANDIDATE_FOUND(backupMV.x,
1710 :     backupMV.y + iDiamondSize, 4);
1711 : chl 344 }
1712 : edgomez 195 } else {
1713 : chl 326 currMV->x = start_x;
1714 :     currMV->y = start_y;
1715 : edgomez 78 }
1716 : Isibaar 3 return iMinSAD;
1717 :     }
1718 :    
1719 : chl 345
1720 :    
1721 :    
1722 : edgomez 195 int32_t
1723 : chl 345 Square8_MainSearch(const uint8_t * const pRef,
1724 :     const uint8_t * const pRefH,
1725 :     const uint8_t * const pRefV,
1726 :     const uint8_t * const pRefHV,
1727 :     const uint8_t * const cur,
1728 :     const int x,
1729 :     const int y,
1730 :     int32_t start_x,
1731 :     int32_t start_y,
1732 :     int32_t iMinSAD,
1733 :     VECTOR * const currMV,
1734 :     const int center_x,
1735 :     const int center_y,
1736 :     const int32_t min_dx,
1737 :     const int32_t max_dx,
1738 :     const int32_t min_dy,
1739 :     const int32_t max_dy,
1740 :     const int32_t iEdgedWidth,
1741 :     const int32_t iDiamondSize,
1742 :     const int32_t iFcode,
1743 :     const int32_t iQuant,
1744 :     int iFound)
1745 :     {
1746 :     /* Do a square search around given starting point, return SAD of best */
1747 :    
1748 :     int32_t iDirection = 0;
1749 :     int32_t iSAD;
1750 :     VECTOR backupMV;
1751 :    
1752 :     backupMV.x = start_x;
1753 :     backupMV.y = start_y;
1754 :    
1755 :     /* It's one search with full square pattern, and new parts for all following diamonds */
1756 :    
1757 :     /* new direction are extra, so 1-4 is normal diamond
1758 :     537
1759 :     1*2
1760 :     648
1761 :     */
1762 :    
1763 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
1764 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
1765 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
1766 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
1767 :    
1768 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize,
1769 :     backupMV.y - iDiamondSize, 5);
1770 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize,
1771 :     backupMV.y + iDiamondSize, 6);
1772 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize,
1773 :     backupMV.y - iDiamondSize, 7);
1774 :     CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize,
1775 :     backupMV.y + iDiamondSize, 8);
1776 :    
1777 :    
1778 :     if (iDirection) {
1779 :     while (!iFound) {
1780 :     iFound = 1;
1781 :     backupMV = *currMV;
1782 :    
1783 :     switch (iDirection) {
1784 :     case 1:
1785 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1786 :     backupMV.y, 1);
1787 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1788 :     backupMV.y - iDiamondSize, 5);
1789 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1790 :     backupMV.y - iDiamondSize, 7);
1791 :     break;
1792 :     case 2:
1793 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1794 :     2);
1795 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1796 :     backupMV.y + iDiamondSize, 6);
1797 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1798 :     backupMV.y + iDiamondSize, 8);
1799 :     break;
1800 :    
1801 :     case 3:
1802 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1803 :     4);
1804 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1805 :     backupMV.y - iDiamondSize, 7);
1806 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1807 :     backupMV.y + iDiamondSize, 8);
1808 :     break;
1809 :    
1810 :     case 4:
1811 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1812 :     3);
1813 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1814 :     backupMV.y - iDiamondSize, 5);
1815 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1816 :     backupMV.y + iDiamondSize, 6);
1817 :     break;
1818 :    
1819 :     case 5:
1820 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
1821 :     1);
1822 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1823 :     3);
1824 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1825 :     backupMV.y - iDiamondSize, 5);
1826 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1827 :     backupMV.y + iDiamondSize, 6);
1828 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1829 :     backupMV.y - iDiamondSize, 7);
1830 :     break;
1831 :    
1832 :     case 6:
1833 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1834 :     2);
1835 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1836 :     3);
1837 :    
1838 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1839 :     backupMV.y - iDiamondSize, 5);
1840 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1841 :     backupMV.y + iDiamondSize, 6);
1842 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1843 :     backupMV.y + iDiamondSize, 8);
1844 :    
1845 :     break;
1846 :    
1847 :     case 7:
1848 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1849 :     backupMV.y, 1);
1850 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1851 :     4);
1852 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1853 :     backupMV.y - iDiamondSize, 5);
1854 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1855 :     backupMV.y - iDiamondSize, 7);
1856 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1857 :     backupMV.y + iDiamondSize, 8);
1858 :     break;
1859 :    
1860 :     case 8:
1861 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1862 :     2);
1863 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1864 :     4);
1865 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1866 :     backupMV.y + iDiamondSize, 6);
1867 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1868 :     backupMV.y - iDiamondSize, 7);
1869 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1870 :     backupMV.y + iDiamondSize, 8);
1871 :     break;
1872 :     default:
1873 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
1874 :     1);
1875 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1876 :     2);
1877 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1878 :     3);
1879 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1880 :     4);
1881 :    
1882 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1883 :     backupMV.y - iDiamondSize, 5);
1884 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1885 :     backupMV.y + iDiamondSize, 6);
1886 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1887 :     backupMV.y - iDiamondSize, 7);
1888 :     CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1889 :     backupMV.y + iDiamondSize, 8);
1890 :     break;
1891 :     }
1892 :     }
1893 :     } else {
1894 :     currMV->x = start_x;
1895 :     currMV->y = start_y;
1896 :     }
1897 :     return iMinSAD;
1898 :     }
1899 :    
1900 :    
1901 :    
1902 :    
1903 :    
1904 :     int32_t
1905 : ia64p 300 Halfpel8_Refine_c(const uint8_t * const pRef,
1906 : edgomez 195 const uint8_t * const pRefH,
1907 :     const uint8_t * const pRefV,
1908 :     const uint8_t * const pRefHV,
1909 :     const uint8_t * const cur,
1910 :     const int x,
1911 :     const int y,
1912 :     VECTOR * const currMV,
1913 :     int32_t iMinSAD,
1914 : chl 326 const int center_x,
1915 :     const int center_y,
1916 : edgomez 195 const int32_t min_dx,
1917 :     const int32_t max_dx,
1918 :     const int32_t min_dy,
1919 :     const int32_t max_dy,
1920 :     const int32_t iFcode,
1921 :     const int32_t iQuant,
1922 :     const int32_t iEdgedWidth)
1923 : Isibaar 3 {
1924 :     /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */
1925 :    
1926 :     int32_t iSAD;
1927 :     VECTOR backupMV = *currMV;
1928 : edgomez 195
1929 :     CHECK_MV8_CANDIDATE(backupMV.x - 1, backupMV.y - 1);
1930 :     CHECK_MV8_CANDIDATE(backupMV.x, backupMV.y - 1);
1931 :     CHECK_MV8_CANDIDATE(backupMV.x + 1, backupMV.y - 1);
1932 :     CHECK_MV8_CANDIDATE(backupMV.x - 1, backupMV.y);
1933 :     CHECK_MV8_CANDIDATE(backupMV.x + 1, backupMV.y);
1934 :     CHECK_MV8_CANDIDATE(backupMV.x - 1, backupMV.y + 1);
1935 :     CHECK_MV8_CANDIDATE(backupMV.x, backupMV.y + 1);
1936 :     CHECK_MV8_CANDIDATE(backupMV.x + 1, backupMV.y + 1);
1937 :    
1938 : Isibaar 3 return iMinSAD;
1939 :     }
1940 :    
1941 :    
1942 :     #define PMV_HALFPEL8 (PMV_HALFPELDIAMOND8|PMV_HALFPELREFINE8)
1943 :    
1944 : edgomez 195 int32_t
1945 :     PMVfastSearch8(const uint8_t * const pRef,
1946 :     const uint8_t * const pRefH,
1947 :     const uint8_t * const pRefV,
1948 :     const uint8_t * const pRefHV,
1949 :     const IMAGE * const pCur,
1950 :     const int x,
1951 :     const int y,
1952 :     const int start_x,
1953 :     const int start_y,
1954 : chl 326 const int center_x,
1955 :     const int center_y,
1956 : edgomez 195 const uint32_t MotionFlags,
1957 :     const uint32_t iQuant,
1958 :     const uint32_t iFcode,
1959 :     const MBParam * const pParam,
1960 :     const MACROBLOCK * const pMBs,
1961 :     const MACROBLOCK * const prevMBs,
1962 :     VECTOR * const currMV,
1963 :     VECTOR * const currPMV)
1964 : Isibaar 3 {
1965 : edgomez 195 const uint32_t iWcount = pParam->mb_width;
1966 : Isibaar 3 const int32_t iWidth = pParam->width;
1967 :     const int32_t iHeight = pParam->height;
1968 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
1969 : Isibaar 3
1970 : edgomez 195 const uint8_t *cur = pCur->y + x * 8 + y * 8 * iEdgedWidth;
1971 : Isibaar 3
1972 :     int32_t iDiamondSize;
1973 :    
1974 :     int32_t min_dx;
1975 :     int32_t max_dx;
1976 :     int32_t min_dy;
1977 :     int32_t max_dy;
1978 : edgomez 195
1979 : Isibaar 3 VECTOR pmv[4];
1980 :     int32_t psad[4];
1981 :     VECTOR newMV;
1982 :     VECTOR backupMV;
1983 : edgomez 170 VECTOR startMV;
1984 : Isibaar 3
1985 : edgomez 195 // const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount;
1986 :     const MACROBLOCK *const prevMB = prevMBs + (x >> 1) + (y >> 1) * iWcount;
1987 : Isibaar 3
1988 : chl 259 int32_t threshA, threshB;
1989 : edgomez 195 int32_t iFound, bPredEq;
1990 :     int32_t iMinSAD, iSAD;
1991 : Isibaar 3
1992 : edgomez 195 int32_t iSubBlock = (y & 1) + (y & 1) + (x & 1);
1993 :    
1994 : chl 181 MainSearch8FuncPtr MainSearchPtr;
1995 :    
1996 : edgomez 170 /* Init variables */
1997 :     startMV.x = start_x;
1998 :     startMV.y = start_y;
1999 :    
2000 :     /* Get maximum range */
2001 : edgomez 195 get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 8, iWidth, iHeight,
2002 :     iFcode);
2003 : Isibaar 3
2004 : edgomez 195 if (!(MotionFlags & PMV_HALFPELDIAMOND8)) {
2005 :     min_dx = EVEN(min_dx);
2006 :     max_dx = EVEN(max_dx);
2007 :     min_dy = EVEN(min_dy);
2008 :     max_dy = EVEN(max_dy);
2009 :     }
2010 : Isibaar 3
2011 : edgomez 195 /* because we might use IF (dx>max_dx) THEN dx=max_dx; */
2012 : chl 285 bPredEq = get_pmvdata2(pMBs, iWcount, 0, (x >> 1), (y >> 1), iSubBlock, pmv, psad);
2013 : Isibaar 3
2014 : edgomez 195 if ((x == 0) && (y == 0)) {
2015 :     threshA = 512 / 4;
2016 :     threshB = 1024 / 4;
2017 :    
2018 :     } else {
2019 : chl 326 threshA = psad[0] / 4; /* good estimate? */
2020 : edgomez 195 threshB = threshA + 256 / 4;
2021 :     if (threshA < 512 / 4)
2022 :     threshA = 512 / 4;
2023 :     if (threshA > 1024 / 4)
2024 :     threshA = 1024 / 4;
2025 :     if (threshB > 1792 / 4)
2026 :     threshB = 1792 / 4;
2027 : Isibaar 3 }
2028 :    
2029 : edgomez 195 iFound = 0;
2030 :    
2031 : Isibaar 3 /* Step 4: Calculate SAD around the Median prediction.
2032 : edgomez 78 MinSAD=SAD
2033 :     If Motion Vector equal to Previous frame motion vector
2034 :     and MinSAD<PrevFrmSAD goto Step 10.
2035 :     If SAD<=256 goto Step 10.
2036 : edgomez 195 */
2037 : Isibaar 3
2038 :    
2039 :     // Prepare for main loop
2040 :    
2041 : chl 345 if (MotionFlags & PMV_USESQUARES8)
2042 :     MainSearchPtr = Square8_MainSearch;
2043 :     else
2044 : chl 181
2045 :     if (MotionFlags & PMV_ADVANCEDDIAMOND8)
2046 :     MainSearchPtr = AdvDiamond8_MainSearch;
2047 :     else
2048 :     MainSearchPtr = Diamond8_MainSearch;
2049 :    
2050 :    
2051 : chl 169 *currMV = startMV;
2052 : edgomez 195
2053 :     iMinSAD =
2054 :     sad8(cur,
2055 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV,
2056 :     iEdgedWidth), iEdgedWidth);
2057 :     iMinSAD +=
2058 : chl 326 calc_delta_8(currMV->x - center_x, currMV->y - center_y,
2059 : edgomez 195 (uint8_t) iFcode, iQuant);
2060 :    
2061 :     if ((iMinSAD < 256 / 4) || ((MVequal(*currMV, prevMB->mvs[iSubBlock]))
2062 : chl 289 && ((int32_t) iMinSAD <
2063 : edgomez 195 prevMB->sad8[iSubBlock]))) {
2064 :     if (MotionFlags & PMV_QUICKSTOP16)
2065 : chl 96 goto PMVfast8_Terminate_without_Refine;
2066 :     if (MotionFlags & PMV_EARLYSTOP16)
2067 :     goto PMVfast8_Terminate_with_Refine;
2068 : edgomez 78 }
2069 : Isibaar 3
2070 : chl 169 /* Step 2 (lazy eval): Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion
2071 :     vector of the median.
2072 :     If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2
2073 :     */
2074 : chl 96
2075 : edgomez 195 if ((bPredEq) && (MVequal(pmv[0], prevMB->mvs[iSubBlock])))
2076 :     iFound = 2;
2077 : chl 169
2078 :     /* Step 3 (lazy eval): If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search.
2079 :     Otherwise select large Diamond Search.
2080 :     */
2081 :    
2082 : edgomez 195 if ((!MVzero(pmv[0])) || (threshB < 1536 / 4) || (bPredEq))
2083 :     iDiamondSize = 1; // 1 halfpel!
2084 : chl 169 else
2085 : edgomez 195 iDiamondSize = 2; // 2 halfpel = 1 full pixel!
2086 : chl 169
2087 : edgomez 195 if (!(MotionFlags & PMV_HALFPELDIAMOND8))
2088 :     iDiamondSize *= 2;
2089 : chl 169
2090 :    
2091 : Isibaar 3 /*
2092 : edgomez 78 Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block.
2093 :     Also calculate (0,0) but do not subtract offset.
2094 :     Let MinSAD be the smallest SAD up to this point.
2095 : chl 140 If MV is (0,0) subtract offset.
2096 : Isibaar 3 */
2097 :    
2098 : chl 169 // the median prediction might be even better than mv16
2099 : Isibaar 3
2100 : edgomez 195 if (!MVequal(pmv[0], startMV))
2101 : chl 326 CHECK_MV8_CANDIDATE(center_x, center_y);
2102 : chl 169
2103 :     // (0,0) if needed
2104 :     if (!MVzero(pmv[0]))
2105 : edgomez 195 if (!MVzero(startMV))
2106 :     CHECK_MV8_ZERO;
2107 : Isibaar 3
2108 : chl 169 // previous frame MV if needed
2109 :     if (!MVzero(prevMB->mvs[iSubBlock]))
2110 : edgomez 195 if (!MVequal(prevMB->mvs[iSubBlock], startMV))
2111 :     if (!MVequal(prevMB->mvs[iSubBlock], pmv[0]))
2112 :     CHECK_MV8_CANDIDATE(prevMB->mvs[iSubBlock].x,
2113 :     prevMB->mvs[iSubBlock].y);
2114 : chl 169
2115 : edgomez 195 if ((iMinSAD <= threshA) ||
2116 :     (MVequal(*currMV, prevMB->mvs[iSubBlock]) &&
2117 : chl 289 ((int32_t) iMinSAD < prevMB->sad8[iSubBlock]))) {
2118 : edgomez 195 if (MotionFlags & PMV_QUICKSTOP16)
2119 : chl 169 goto PMVfast8_Terminate_without_Refine;
2120 :     if (MotionFlags & PMV_EARLYSTOP16)
2121 :     goto PMVfast8_Terminate_with_Refine;
2122 :     }
2123 :    
2124 :     // left neighbour, if allowed and needed
2125 :     if (!MVzero(pmv[1]))
2126 : edgomez 195 if (!MVequal(pmv[1], startMV))
2127 :     if (!MVequal(pmv[1], prevMB->mvs[iSubBlock]))
2128 :     if (!MVequal(pmv[1], pmv[0])) {
2129 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2130 :     pmv[1].x = EVEN(pmv[1].x);
2131 :     pmv[1].y = EVEN(pmv[1].y);
2132 :     }
2133 :     CHECK_MV8_CANDIDATE(pmv[1].x, pmv[1].y);
2134 :     }
2135 : chl 169 // top neighbour, if allowed and needed
2136 :     if (!MVzero(pmv[2]))
2137 : edgomez 195 if (!MVequal(pmv[2], startMV))
2138 :     if (!MVequal(pmv[2], prevMB->mvs[iSubBlock]))
2139 :     if (!MVequal(pmv[2], pmv[0]))
2140 :     if (!MVequal(pmv[2], pmv[1])) {
2141 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2142 :     pmv[2].x = EVEN(pmv[2].x);
2143 :     pmv[2].y = EVEN(pmv[2].y);
2144 :     }
2145 :     CHECK_MV8_CANDIDATE(pmv[2].x, pmv[2].y);
2146 :    
2147 : chl 169 // top right neighbour, if allowed and needed
2148 : edgomez 195 if (!MVzero(pmv[3]))
2149 :     if (!MVequal(pmv[3], startMV))
2150 :     if (!MVequal(pmv[3], prevMB->mvs[iSubBlock]))
2151 :     if (!MVequal(pmv[3], pmv[0]))
2152 :     if (!MVequal(pmv[3], pmv[1]))
2153 :     if (!MVequal(pmv[3], pmv[2])) {
2154 :     if (!
2155 :     (MotionFlags &
2156 :     PMV_HALFPEL8)) {
2157 :     pmv[3].x = EVEN(pmv[3].x);
2158 :     pmv[3].y = EVEN(pmv[3].y);
2159 :     }
2160 :     CHECK_MV8_CANDIDATE(pmv[3].x,
2161 :     pmv[3].y);
2162 :     }
2163 :     }
2164 : Isibaar 3
2165 : edgomez 195 if ((MVzero(*currMV)) &&
2166 :     (!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96) */ )
2167 : chl 140 iMinSAD -= MV8_00_BIAS;
2168 :    
2169 :    
2170 : Isibaar 3 /* Step 6: If MinSAD <= thresa goto Step 10.
2171 :     If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10.
2172 :     */
2173 :    
2174 : edgomez 195 if ((iMinSAD <= threshA) ||
2175 :     (MVequal(*currMV, prevMB->mvs[iSubBlock]) &&
2176 : chl 289 ((int32_t) iMinSAD < prevMB->sad8[iSubBlock]))) {
2177 : edgomez 195 if (MotionFlags & PMV_QUICKSTOP16)
2178 : chl 96 goto PMVfast8_Terminate_without_Refine;
2179 :     if (MotionFlags & PMV_EARLYSTOP16)
2180 :     goto PMVfast8_Terminate_with_Refine;
2181 : edgomez 78 }
2182 : Isibaar 3
2183 :     /************ (Diamond Search) **************/
2184 :     /*
2185 : edgomez 78 Step 7: Perform Diamond search, with either the small or large diamond.
2186 :     If Found=2 only examine one Diamond pattern, and afterwards goto step 10
2187 :     Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond.
2188 :     If center then goto step 10.
2189 :     Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center.
2190 :     Refine by using small diamond and goto step 10.
2191 : Isibaar 3 */
2192 :    
2193 : edgomez 195 backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
2194 : Isibaar 3
2195 :     /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
2196 : edgomez 195 iSAD =
2197 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
2198 : chl 326 currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
2199 : edgomez 195 min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
2200 :     iQuant, iFound);
2201 :    
2202 :     if (iSAD < iMinSAD) {
2203 : Isibaar 3 *currMV = newMV;
2204 :     iMinSAD = iSAD;
2205 :     }
2206 :    
2207 : edgomez 195 if (MotionFlags & PMV_EXTSEARCH8) {
2208 : Isibaar 3 /* extended: search (up to) two more times: orignal prediction and (0,0) */
2209 :    
2210 : edgomez 195 if (!(MVequal(pmv[0], backupMV))) {
2211 :     iSAD =
2212 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
2213 : chl 326 pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
2214 : edgomez 195 min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
2215 :     iDiamondSize, iFcode, iQuant, iFound);
2216 :    
2217 :     if (iSAD < iMinSAD) {
2218 :     *currMV = newMV;
2219 :     iMinSAD = iSAD;
2220 :     }
2221 : Isibaar 3 }
2222 :    
2223 : edgomez 195 if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2224 :     iSAD =
2225 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2226 : chl 326 iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
2227 : edgomez 195 max_dy, iEdgedWidth, iDiamondSize, iFcode,
2228 :     iQuant, iFound);
2229 :    
2230 :     if (iSAD < iMinSAD) {
2231 :     *currMV = newMV;
2232 :     iMinSAD = iSAD;
2233 :     }
2234 : Isibaar 3 }
2235 :     }
2236 :    
2237 :     /* Step 10: The motion vector is chosen according to the block corresponding to MinSAD.
2238 : edgomez 78 By performing an optional local half-pixel search, we can refine this result even further.
2239 : Isibaar 3 */
2240 :    
2241 : edgomez 195 PMVfast8_Terminate_with_Refine:
2242 :     if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step
2243 :     iMinSAD =
2244 :     Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2245 : chl 326 iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
2246 : edgomez 195 iFcode, iQuant, iEdgedWidth);
2247 : Isibaar 3
2248 : edgomez 195
2249 :     PMVfast8_Terminate_without_Refine:
2250 : chl 326 currPMV->x = currMV->x - center_x;
2251 :     currPMV->y = currMV->y - center_y;
2252 : edgomez 195
2253 : Isibaar 3 return iMinSAD;
2254 :     }
2255 : chl 96
2256 : edgomez 195 int32_t
2257 :     EPZSSearch16(const uint8_t * const pRef,
2258 :     const uint8_t * const pRefH,
2259 :     const uint8_t * const pRefV,
2260 :     const uint8_t * const pRefHV,
2261 :     const IMAGE * const pCur,
2262 :     const int x,
2263 :     const int y,
2264 : chl 326 const int start_x,
2265 :     const int start_y,
2266 :     const int center_x,
2267 :     const int center_y,
2268 : edgomez 195 const uint32_t MotionFlags,
2269 :     const uint32_t iQuant,
2270 :     const uint32_t iFcode,
2271 :     const MBParam * const pParam,
2272 :     const MACROBLOCK * const pMBs,
2273 :     const MACROBLOCK * const prevMBs,
2274 :     VECTOR * const currMV,
2275 :     VECTOR * const currPMV)
2276 : chl 96 {
2277 : edgomez 195 const uint32_t iWcount = pParam->mb_width;
2278 :     const uint32_t iHcount = pParam->mb_height;
2279 : chl 96
2280 :     const int32_t iWidth = pParam->width;
2281 :     const int32_t iHeight = pParam->height;
2282 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
2283 : chl 96
2284 : edgomez 195 const uint8_t *cur = pCur->y + x * 16 + y * 16 * iEdgedWidth;
2285 : chl 96
2286 :     int32_t min_dx;
2287 :     int32_t max_dx;
2288 :     int32_t min_dy;
2289 :     int32_t max_dy;
2290 : edgomez 195
2291 : chl 96 VECTOR newMV;
2292 :     VECTOR backupMV;
2293 : edgomez 195
2294 : chl 96 VECTOR pmv[4];
2295 :     int32_t psad[8];
2296 :    
2297 : edgomez 195 static MACROBLOCK *oldMBs = NULL;
2298 :    
2299 :     // const MACROBLOCK * const pMB = pMBs + x + y * iWcount;
2300 :     const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;
2301 :     MACROBLOCK *oldMB = NULL;
2302 :    
2303 : chl 259 int32_t thresh2;
2304 : edgomez 195 int32_t bPredEq;
2305 :     int32_t iMinSAD, iSAD = 9999;
2306 : chl 96
2307 : chl 181 MainSearch16FuncPtr MainSearchPtr;
2308 : chl 96
2309 : edgomez 195 if (oldMBs == NULL) {
2310 :     oldMBs = (MACROBLOCK *) calloc(iWcount * iHcount, sizeof(MACROBLOCK));
2311 :     // fprintf(stderr,"allocated %d bytes for oldMBs\n",iWcount*iHcount*sizeof(MACROBLOCK));
2312 : chl 96 }
2313 :     oldMB = oldMBs + x + y * iWcount;
2314 :    
2315 :     /* Get maximum range */
2316 : edgomez 195 get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight,
2317 :     iFcode);
2318 : chl 96
2319 : edgomez 195 if (!(MotionFlags & PMV_HALFPEL16)) {
2320 :     min_dx = EVEN(min_dx);
2321 :     max_dx = EVEN(max_dx);
2322 :     min_dy = EVEN(min_dy);
2323 :     max_dy = EVEN(max_dy);
2324 :     }
2325 :     /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
2326 : chl 285 bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad);
2327 : chl 96
2328 :     /* Step 4: Calculate SAD around the Median prediction.
2329 :     MinSAD=SAD
2330 :     If Motion Vector equal to Previous frame motion vector
2331 :     and MinSAD<PrevFrmSAD goto Step 10.
2332 :     If SAD<=256 goto Step 10.
2333 : edgomez 195 */
2334 : chl 96
2335 :     // Prepare for main loop
2336 :    
2337 : chl 326 currMV->x = start_x;
2338 :     currMV->y = start_y;
2339 :    
2340 : edgomez 195 if (!(MotionFlags & PMV_HALFPEL16)) {
2341 : chl 96 currMV->x = EVEN(currMV->x);
2342 :     currMV->y = EVEN(currMV->y);
2343 :     }
2344 :    
2345 : edgomez 195 if (currMV->x > max_dx)
2346 :     currMV->x = max_dx;
2347 :     if (currMV->x < min_dx)
2348 :     currMV->x = min_dx;
2349 :     if (currMV->y > max_dy)
2350 :     currMV->y = max_dy;
2351 :     if (currMV->y < min_dy)
2352 :     currMV->y = min_dy;
2353 :    
2354 :     /***************** This is predictor SET A: only median prediction ******************/
2355 :    
2356 :     iMinSAD =
2357 :     sad16(cur,
2358 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV,
2359 :     iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);
2360 :     iMinSAD +=
2361 : chl 326 calc_delta_16(currMV->x - center_x, currMV->y - center_y,
2362 : edgomez 195 (uint8_t) iFcode, iQuant);
2363 :    
2364 : chl 96 // thresh1 is fixed to 256
2365 : edgomez 195 if ((iMinSAD < 256) ||
2366 :     ((MVequal(*currMV, prevMB->mvs[0])) &&
2367 : chl 289 ((int32_t) iMinSAD < prevMB->sad16))) {
2368 : edgomez 195 if (MotionFlags & PMV_QUICKSTOP16)
2369 :     goto EPZS16_Terminate_without_Refine;
2370 :     if (MotionFlags & PMV_EARLYSTOP16)
2371 :     goto EPZS16_Terminate_with_Refine;
2372 :     }
2373 : chl 96
2374 : edgomez 195 /************** This is predictor SET B: (0,0), prev.frame MV, neighbours **************/
2375 : chl 96
2376 :     // previous frame MV
2377 : edgomez 195 CHECK_MV16_CANDIDATE(prevMB->mvs[0].x, prevMB->mvs[0].y);
2378 : chl 96
2379 :     // set threshhold based on Min of Prediction and SAD of collocated block
2380 :     // CHECK_MV16 always uses iSAD for the SAD of last vector to check, so now iSAD is what we want
2381 :    
2382 : edgomez 195 if ((x == 0) && (y == 0)) {
2383 :     thresh2 = 512;
2384 :     } else {
2385 : chl 96 /* T_k = 1.2 * MIN(SAD_top,SAD_left,SAD_topleft,SAD_coll) +128; [Tourapis, 2002] */
2386 :    
2387 : edgomez 195 thresh2 = MIN(psad[0], iSAD) * 6 / 5 + 128;
2388 : chl 96 }
2389 :    
2390 :     // MV=(0,0) is often a good choice
2391 :    
2392 :     CHECK_MV16_ZERO;
2393 :    
2394 : edgomez 195
2395 : chl 96 // left neighbour, if allowed
2396 : edgomez 195 if (x != 0) {
2397 :     if (!(MotionFlags & PMV_HALFPEL16)) {
2398 :     pmv[1].x = EVEN(pmv[1].x);
2399 : chl 96 pmv[1].y = EVEN(pmv[1].y);
2400 :     }
2401 : edgomez 195 CHECK_MV16_CANDIDATE(pmv[1].x, pmv[1].y);
2402 : chl 96 }
2403 :     // top neighbour, if allowed
2404 : edgomez 195 if (y != 0) {
2405 :     if (!(MotionFlags & PMV_HALFPEL16)) {
2406 :     pmv[2].x = EVEN(pmv[2].x);
2407 : chl 96 pmv[2].y = EVEN(pmv[2].y);
2408 :     }
2409 : edgomez 195 CHECK_MV16_CANDIDATE(pmv[2].x, pmv[2].y);
2410 :    
2411 : chl 96 // top right neighbour, if allowed
2412 : edgomez 195 if ((uint32_t) x != (iWcount - 1)) {
2413 :     if (!(MotionFlags & PMV_HALFPEL16)) {
2414 :     pmv[3].x = EVEN(pmv[3].x);
2415 : chl 96 pmv[3].y = EVEN(pmv[3].y);
2416 :     }
2417 : edgomez 195 CHECK_MV16_CANDIDATE(pmv[3].x, pmv[3].y);
2418 : chl 96 }
2419 :     }
2420 :    
2421 :     /* Terminate if MinSAD <= T_2
2422 :     Terminate if MV[t] == MV[t-1] and MinSAD[t] <= MinSAD[t-1]
2423 :     */
2424 :    
2425 : edgomez 195 if ((iMinSAD <= thresh2)
2426 :     || (MVequal(*currMV, prevMB->mvs[0]) &&
2427 : chl 289 ((int32_t) iMinSAD <= prevMB->sad16))) {
2428 : edgomez 195 if (MotionFlags & PMV_QUICKSTOP16)
2429 :     goto EPZS16_Terminate_without_Refine;
2430 :     if (MotionFlags & PMV_EARLYSTOP16)
2431 :     goto EPZS16_Terminate_with_Refine;
2432 :     }
2433 : chl 96
2434 :     /***** predictor SET C: acceleration MV (new!), neighbours in prev. frame(new!) ****/
2435 :    
2436 : edgomez 195 backupMV = prevMB->mvs[0]; // collocated MV
2437 :     backupMV.x += (prevMB->mvs[0].x - oldMB->mvs[0].x); // acceleration X
2438 :     backupMV.y += (prevMB->mvs[0].y - oldMB->mvs[0].y); // acceleration Y
2439 : chl 96
2440 : edgomez 195 CHECK_MV16_CANDIDATE(backupMV.x, backupMV.y);
2441 : chl 96
2442 :     // left neighbour
2443 : edgomez 195 if (x != 0)
2444 :     CHECK_MV16_CANDIDATE((prevMB - 1)->mvs[0].x, (prevMB - 1)->mvs[0].y);
2445 : chl 96
2446 :     // top neighbour
2447 :     if (y != 0)
2448 : edgomez 195 CHECK_MV16_CANDIDATE((prevMB - iWcount)->mvs[0].x,
2449 :     (prevMB - iWcount)->mvs[0].y);
2450 : chl 96
2451 :     // right neighbour, if allowed (this value is not written yet, so take it from pMB->mvs
2452 :    
2453 : edgomez 195 if ((uint32_t) x != iWcount - 1)
2454 :     CHECK_MV16_CANDIDATE((prevMB + 1)->mvs[0].x, (prevMB + 1)->mvs[0].y);
2455 : chl 96
2456 :     // bottom neighbour, dito
2457 : edgomez 195 if ((uint32_t) y != iHcount - 1)
2458 :     CHECK_MV16_CANDIDATE((prevMB + iWcount)->mvs[0].x,
2459 :     (prevMB + iWcount)->mvs[0].y);
2460 : chl 96
2461 :     /* Terminate if MinSAD <= T_3 (here T_3 = T_2) */
2462 : edgomez 195 if (iMinSAD <= thresh2) {
2463 :     if (MotionFlags & PMV_QUICKSTOP16)
2464 :     goto EPZS16_Terminate_without_Refine;
2465 :     if (MotionFlags & PMV_EARLYSTOP16)
2466 :     goto EPZS16_Terminate_with_Refine;
2467 :     }
2468 : chl 96
2469 :     /************ (if Diamond Search) **************/
2470 :    
2471 : edgomez 195 backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
2472 : chl 96
2473 : chl 261 if (MotionFlags & PMV_USESQUARES16)
2474 : chl 184 MainSearchPtr = Square16_MainSearch;
2475 :     else
2476 : chl 261 if (MotionFlags & PMV_ADVANCEDDIAMOND16)
2477 : chl 184 MainSearchPtr = AdvDiamond16_MainSearch;
2478 :     else
2479 :     MainSearchPtr = Diamond16_MainSearch;
2480 :    
2481 : chl 96 /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
2482 :    
2483 : edgomez 195 iSAD =
2484 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
2485 : chl 326 currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
2486 : edgomez 195 min_dy, max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);
2487 :    
2488 :     if (iSAD < iMinSAD) {
2489 : chl 96 *currMV = newMV;
2490 :     iMinSAD = iSAD;
2491 :     }
2492 :    
2493 :    
2494 : edgomez 195 if (MotionFlags & PMV_EXTSEARCH16) {
2495 : chl 96 /* extended mode: search (up to) two more times: orignal prediction and (0,0) */
2496 :    
2497 : edgomez 195 if (!(MVequal(pmv[0], backupMV))) {
2498 :     iSAD =
2499 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
2500 : chl 326 pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
2501 : edgomez 195 min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
2502 :     2, iFcode, iQuant, 0);
2503 : chl 96 }
2504 : edgomez 195
2505 :     if (iSAD < iMinSAD) {
2506 : chl 96 *currMV = newMV;
2507 :     iMinSAD = iSAD;
2508 :     }
2509 : edgomez 195
2510 :     if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2511 :     iSAD =
2512 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2513 : chl 326 iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
2514 : edgomez 195 max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);
2515 :    
2516 :     if (iSAD < iMinSAD) {
2517 : chl 96 *currMV = newMV;
2518 :     iMinSAD = iSAD;
2519 :     }
2520 :     }
2521 :     }
2522 :    
2523 :     /*************** Choose best MV found **************/
2524 :    
2525 : edgomez 195 EPZS16_Terminate_with_Refine:
2526 :     if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step
2527 :     iMinSAD =
2528 :     Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2529 : chl 326 iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
2530 : edgomez 195 iFcode, iQuant, iEdgedWidth);
2531 : chl 96
2532 : edgomez 195 EPZS16_Terminate_without_Refine:
2533 : chl 96
2534 : chl 141 *oldMB = *prevMB;
2535 : edgomez 195
2536 : chl 326 currPMV->x = currMV->x - center_x;
2537 :     currPMV->y = currMV->y - center_y;
2538 : chl 96 return iMinSAD;
2539 :     }
2540 :    
2541 :    
2542 : edgomez 195 int32_t
2543 :     EPZSSearch8(const uint8_t * const pRef,
2544 :     const uint8_t * const pRefH,
2545 :     const uint8_t * const pRefV,
2546 :     const uint8_t * const pRefHV,
2547 :     const IMAGE * const pCur,
2548 :     const int x,
2549 :     const int y,
2550 :     const int start_x,
2551 :     const int start_y,
2552 : chl 326 const int center_x,
2553 :     const int center_y,
2554 : edgomez 195 const uint32_t MotionFlags,
2555 :     const uint32_t iQuant,
2556 :     const uint32_t iFcode,
2557 :     const MBParam * const pParam,
2558 :     const MACROBLOCK * const pMBs,
2559 :     const MACROBLOCK * const prevMBs,
2560 :     VECTOR * const currMV,
2561 :     VECTOR * const currPMV)
2562 : chl 96 {
2563 : chl 141 /* Please not that EPZS might not be a good choice for 8x8-block motion search ! */
2564 :    
2565 :     const uint32_t iWcount = pParam->mb_width;
2566 : chl 96 const int32_t iWidth = pParam->width;
2567 :     const int32_t iHeight = pParam->height;
2568 : edgomez 195 const int32_t iEdgedWidth = pParam->edged_width;
2569 : chl 96
2570 : edgomez 195 const uint8_t *cur = pCur->y + x * 8 + y * 8 * iEdgedWidth;
2571 : chl 96
2572 : edgomez 195 int32_t iDiamondSize = 1;
2573 :    
2574 : chl 96 int32_t min_dx;
2575 :     int32_t max_dx;
2576 :     int32_t min_dy;
2577 :     int32_t max_dy;
2578 : edgomez 195
2579 : chl 96 VECTOR newMV;
2580 :     VECTOR backupMV;
2581 : edgomez 195
2582 : chl 96 VECTOR pmv[4];
2583 :     int32_t psad[8];
2584 :    
2585 : edgomez 195 const int32_t iSubBlock = ((y & 1) << 1) + (x & 1);
2586 : chl 96
2587 : edgomez 195 // const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount;
2588 :     const MACROBLOCK *const prevMB = prevMBs + (x >> 1) + (y >> 1) * iWcount;
2589 : chl 96
2590 : edgomez 195 int32_t bPredEq;
2591 :     int32_t iMinSAD, iSAD = 9999;
2592 :    
2593 : chl 181 MainSearch8FuncPtr MainSearchPtr;
2594 : chl 96
2595 :     /* Get maximum range */
2596 : edgomez 195 get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 8, iWidth, iHeight,
2597 :     iFcode);
2598 : chl 96
2599 :     /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */
2600 :    
2601 : edgomez 195 if (!(MotionFlags & PMV_HALFPEL8)) {
2602 :     min_dx = EVEN(min_dx);
2603 :     max_dx = EVEN(max_dx);
2604 :     min_dy = EVEN(min_dy);
2605 :     max_dy = EVEN(max_dy);
2606 :     }
2607 :     /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
2608 : chl 285 bPredEq = get_pmvdata2(pMBs, iWcount, 0, x >> 1, y >> 1, iSubBlock, pmv, psad);
2609 : chl 96
2610 :    
2611 :     /* Step 4: Calculate SAD around the Median prediction.
2612 :     MinSAD=SAD
2613 :     If Motion Vector equal to Previous frame motion vector
2614 :     and MinSAD<PrevFrmSAD goto Step 10.
2615 :     If SAD<=256 goto Step 10.
2616 :     */
2617 :    
2618 :     // Prepare for main loop
2619 :    
2620 : edgomez 195
2621 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2622 : chl 96 currMV->x = EVEN(currMV->x);
2623 :     currMV->y = EVEN(currMV->y);
2624 :     }
2625 :    
2626 : edgomez 195 if (currMV->x > max_dx)
2627 :     currMV->x = max_dx;
2628 :     if (currMV->x < min_dx)
2629 :     currMV->x = min_dx;
2630 :     if (currMV->y > max_dy)
2631 :     currMV->y = max_dy;
2632 :     if (currMV->y < min_dy)
2633 :     currMV->y = min_dy;
2634 : chl 96
2635 : edgomez 195 /***************** This is predictor SET A: only median prediction ******************/
2636 : chl 96
2637 : edgomez 195
2638 :     iMinSAD =
2639 :     sad8(cur,
2640 :     get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV,
2641 :     iEdgedWidth), iEdgedWidth);
2642 :     iMinSAD +=
2643 : chl 326 calc_delta_8(currMV->x - center_x, currMV->y - center_y,
2644 : edgomez 195 (uint8_t) iFcode, iQuant);
2645 :    
2646 :    
2647 : chl 96 // thresh1 is fixed to 256
2648 : edgomez 195 if (iMinSAD < 256 / 4) {
2649 :     if (MotionFlags & PMV_QUICKSTOP8)
2650 :     goto EPZS8_Terminate_without_Refine;
2651 :     if (MotionFlags & PMV_EARLYSTOP8)
2652 :     goto EPZS8_Terminate_with_Refine;
2653 :     }
2654 : chl 96
2655 : edgomez 195 /************** This is predictor SET B: (0,0), prev.frame MV, neighbours **************/
2656 : chl 96
2657 :    
2658 :     // MV=(0,0) is often a good choice
2659 :     CHECK_MV8_ZERO;
2660 :    
2661 : chl 141 // previous frame MV
2662 : edgomez 195 CHECK_MV8_CANDIDATE(prevMB->mvs[iSubBlock].x, prevMB->mvs[iSubBlock].y);
2663 :    
2664 : chl 141 // left neighbour, if allowed
2665 : edgomez 195 if (psad[1] != MV_MAX_ERROR) {
2666 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2667 :     pmv[1].x = EVEN(pmv[1].x);
2668 : chl 141 pmv[1].y = EVEN(pmv[1].y);
2669 :     }
2670 : edgomez 195 CHECK_MV8_CANDIDATE(pmv[1].x, pmv[1].y);
2671 : chl 141 }
2672 :     // top neighbour, if allowed
2673 : edgomez 195 if (psad[2] != MV_MAX_ERROR) {
2674 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2675 :     pmv[2].x = EVEN(pmv[2].x);
2676 : chl 141 pmv[2].y = EVEN(pmv[2].y);
2677 :     }
2678 : edgomez 195 CHECK_MV8_CANDIDATE(pmv[2].x, pmv[2].y);
2679 :    
2680 : chl 141 // top right neighbour, if allowed
2681 : edgomez 195 if (psad[3] != MV_MAX_ERROR) {
2682 :     if (!(MotionFlags & PMV_HALFPEL8)) {
2683 :     pmv[3].x = EVEN(pmv[3].x);
2684 : chl 141 pmv[3].y = EVEN(pmv[3].y);
2685 :     }
2686 : edgomez 195 CHECK_MV8_CANDIDATE(pmv[3].x, pmv[3].y);
2687 : chl 141 }
2688 :     }
2689 :    
2690 :     /* // this bias is zero anyway, at the moment!
2691 :    
2692 :     if ( (MVzero(*currMV)) && (!MVzero(pmv[0])) ) // && (iMinSAD <= iQuant * 96)
2693 :     iMinSAD -= MV8_00_BIAS;
2694 :    
2695 : edgomez 195 */
2696 : chl 141
2697 : chl 96 /* Terminate if MinSAD <= T_2
2698 :     Terminate if MV[t] == MV[t-1] and MinSAD[t] <= MinSAD[t-1]
2699 :     */
2700 :    
2701 : edgomez 195 if (iMinSAD < 512 / 4) { /* T_2 == 512/4 hardcoded */
2702 :     if (MotionFlags & PMV_QUICKSTOP8)
2703 :     goto EPZS8_Terminate_without_Refine;
2704 :     if (MotionFlags & PMV_EARLYSTOP8)
2705 :     goto EPZS8_Terminate_with_Refine;
2706 :     }
2707 : chl 96
2708 : chl 141 /************ (Diamond Search) **************/
2709 : chl 96
2710 : edgomez 195 backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
2711 : chl 96
2712 :     if (!(MotionFlags & PMV_HALFPELDIAMOND8))
2713 :     iDiamondSize *= 2;
2714 : edgomez 195
2715 : chl 141 /* default: use best prediction as starting point for one call of EPZS_MainSearch */
2716 : chl 96
2717 : chl 184 // there is no EPZS^2 for inter4v at the moment
2718 : chl 141
2719 : chl 345 if (MotionFlags & PMV_USESQUARES8)
2720 :     MainSearchPtr = Square8_MainSearch;
2721 :     else
2722 : chl 181
2723 :     if (MotionFlags & PMV_ADVANCEDDIAMOND8)
2724 :     MainSearchPtr = AdvDiamond8_MainSearch;
2725 :     else
2726 :     MainSearchPtr = Diamond8_MainSearch;
2727 :    
2728 : edgomez 195 iSAD =
2729 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
2730 : chl 326 currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
2731 : edgomez 195 min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
2732 :     iQuant, 0);
2733 : chl 96
2734 : chl 141
2735 : edgomez 195 if (iSAD < iMinSAD) {
2736 : chl 96 *currMV = newMV;
2737 :     iMinSAD = iSAD;
2738 :     }
2739 :    
2740 : edgomez 195 if (MotionFlags & PMV_EXTSEARCH8) {
2741 : chl 96 /* extended mode: search (up to) two more times: orignal prediction and (0,0) */
2742 :    
2743 : edgomez 195 if (!(MVequal(pmv[0], backupMV))) {
2744 :     iSAD =
2745 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
2746 : chl 326 pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
2747 : edgomez 195 min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
2748 :     iDiamondSize, iFcode, iQuant, 0);
2749 :    
2750 :     if (iSAD < iMinSAD) {
2751 : chl 96 *currMV = newMV;
2752 :     iMinSAD = iSAD;
2753 :     }
2754 :     }
2755 :    
2756 : edgomez 195 if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2757 :     iSAD =
2758 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2759 : chl 326 iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
2760 : edgomez 195 max_dy, iEdgedWidth, iDiamondSize, iFcode,
2761 :     iQuant, 0);
2762 :    
2763 :     if (iSAD < iMinSAD) {
2764 : chl 96 *currMV = newMV;
2765 :     iMinSAD = iSAD;
2766 :     }
2767 :     }
2768 :     }
2769 :    
2770 :     /*************** Choose best MV found **************/
2771 :    
2772 : edgomez 195 EPZS8_Terminate_with_Refine:
2773 :     if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step
2774 :     iMinSAD =
2775 :     Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2776 : chl 326 iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
2777 : edgomez 195 iFcode, iQuant, iEdgedWidth);
2778 : chl 96
2779 : edgomez 195 EPZS8_Terminate_without_Refine:
2780 : chl 96
2781 : chl 326 currPMV->x = currMV->x - center_x;
2782 :     currPMV->y = currMV->y - center_y;
2783 : chl 96 return iMinSAD;
2784 :     }
2785 :    
2786 : suxen_drol 118
2787 :    
2788 : chl 289 int32_t
2789 :     PMVfastIntSearch16(const uint8_t * const pRef,
2790 :     const uint8_t * const pRefH,
2791 :     const uint8_t * const pRefV,
2792 :     const uint8_t * const pRefHV,
2793 :     const IMAGE * const pCur,
2794 :     const int x,
2795 :     const int y,
2796 : chl 346 const int start_x, /* start should be most likely vector */
2797 :     const int start_y,
2798 :     const int center_x, /* center is from where length of MVs is measured */
2799 :     const int center_y,
2800 : chl 289 const uint32_t MotionFlags,
2801 :     const uint32_t iQuant,
2802 :     const uint32_t iFcode,
2803 :     const MBParam * const pParam,
2804 :     const MACROBLOCK * const pMBs,
2805 :     const MACROBLOCK * const prevMBs,
2806 :     VECTOR * const currMV,
2807 :     VECTOR * const currPMV)
2808 :     {
2809 :     const uint32_t iWcount = pParam->mb_width;
2810 :     const int32_t iWidth = pParam->width;
2811 :     const int32_t iHeight = pParam->height;
2812 :     const int32_t iEdgedWidth = pParam->edged_width;
2813 : suxen_drol 118
2814 : chl 289 const uint8_t *cur = pCur->y + x * 16 + y * 16 * iEdgedWidth;
2815 :     const VECTOR zeroMV = { 0, 0 };
2816 : suxen_drol 118
2817 : chl 289 int32_t iDiamondSize;
2818 :    
2819 :     int32_t min_dx;
2820 :     int32_t max_dx;
2821 :     int32_t min_dy;
2822 :     int32_t max_dy;
2823 :    
2824 :     int32_t iFound;
2825 :    
2826 :     VECTOR newMV;
2827 : chl 346 VECTOR backupMV;
2828 : chl 289
2829 :     VECTOR pmv[4];
2830 :     int32_t psad[4];
2831 :    
2832 :     MainSearch16FuncPtr MainSearchPtr;
2833 :    
2834 :     const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;
2835 :     MACROBLOCK *const pMB = pMBs + x + y * iWcount;
2836 :    
2837 :     int32_t threshA, threshB;
2838 :     int32_t bPredEq;
2839 :     int32_t iMinSAD, iSAD;
2840 : chl 326
2841 : chl 289
2842 :     /* Get maximum range */
2843 :     get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight,
2844 :     iFcode);
2845 :    
2846 :     /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */
2847 :    
2848 :     if ((x == 0) && (y == 0)) {
2849 :     threshA = 512;
2850 :     threshB = 1024;
2851 :    
2852 :     bPredEq = 0;
2853 :     psad[0] = psad[1] = psad[2] = psad[3] = 0;
2854 :     *currMV = pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV;
2855 :    
2856 :     } else {
2857 : chl 370
2858 :     bPredEq = get_ipmvdata(pMBs, iWcount, 0, x, y, 0, pmv, psad);
2859 :    
2860 : chl 289 threshA = psad[0];
2861 :     threshB = threshA + 256;
2862 :     if (threshA < 512)
2863 :     threshA = 512;
2864 :     if (threshA > 1024)
2865 :     threshA = 1024;
2866 :     if (threshB > 1792)
2867 :     threshB = 1792;
2868 :    
2869 :     *currMV = pmv[0]; /* current best := prediction */
2870 :     }
2871 :    
2872 :     iFound = 0;
2873 :    
2874 :     /* Step 4: Calculate SAD around the Median prediction.
2875 :     MinSAD=SAD
2876 :     If Motion Vector equal to Previous frame motion vector
2877 :     and MinSAD<PrevFrmSAD goto Step 10.
2878 :     If SAD<=256 goto Step 10.
2879 :     */
2880 :    
2881 :     if (currMV->x > max_dx) {
2882 :     currMV->x = EVEN(max_dx);
2883 :     }
2884 :     if (currMV->x < min_dx) {
2885 :     currMV->x = EVEN(min_dx);
2886 :     }
2887 :     if (currMV->y > max_dy) {
2888 :     currMV->y = EVEN(max_dy);
2889 :     }
2890 :     if (currMV->y < min_dy) {
2891 :     currMV->y = EVEN(min_dy);
2892 :     }
2893 :    
2894 :     iMinSAD =
2895 :     sad16(cur,
2896 : chl 312 get_iref_mv(pRef, x, y, 16, currMV,
2897 : chl 289 iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);
2898 :     iMinSAD +=
2899 : chl 326 calc_delta_16(currMV->x - center_x, currMV->y - center_y,
2900 : chl 289 (uint8_t) iFcode, iQuant);
2901 :    
2902 :     if ((iMinSAD < 256) ||
2903 :     ((MVequal(*currMV, prevMB->i_mvs[0])) &&
2904 :     ((int32_t) iMinSAD < prevMB->i_sad16))) {
2905 :     if (iMinSAD < 2 * iQuant) // high chances for SKIP-mode
2906 :     {
2907 :     if (!MVzero(*currMV)) {
2908 :     iMinSAD += MV16_00_BIAS;
2909 :     CHECK_MV16_ZERO; // (0,0) saves space for letterboxed pictures
2910 :     iMinSAD -= MV16_00_BIAS;
2911 :     }
2912 :     }
2913 :    
2914 :     if (MotionFlags & PMV_EARLYSTOP16)
2915 :     goto PMVfastInt16_Terminate_with_Refine;
2916 :     }
2917 :    
2918 :    
2919 :     /* Step 2 (lazy eval): Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion
2920 :     vector of the median.
2921 :     If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2
2922 :     */
2923 :    
2924 :     if ((bPredEq) && (MVequal(pmv[0], prevMB->i_mvs[0])))
2925 :     iFound = 2;
2926 :    
2927 :     /* Step 3 (lazy eval): If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search.
2928 :     Otherwise select large Diamond Search.
2929 :     */
2930 :    
2931 :     if ((!MVzero(pmv[0])) || (threshB < 1536) || (bPredEq))
2932 :     iDiamondSize = 2; // halfpel units!
2933 :     else
2934 :     iDiamondSize = 4; // halfpel units!
2935 :    
2936 :     /*
2937 :     Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block.
2938 :     Also calculate (0,0) but do not subtract offset.
2939 :     Let MinSAD be the smallest SAD up to this point.
2940 :     If MV is (0,0) subtract offset.
2941 :     */
2942 :    
2943 :     // (0,0) is often a good choice
2944 :    
2945 :     if (!MVzero(pmv[0]))
2946 :     CHECK_MV16_ZERO;
2947 :    
2948 :     // previous frame MV is always possible
2949 :    
2950 :     if (!MVzero(prevMB->i_mvs[0]))
2951 :     if (!MVequal(prevMB->i_mvs[0], pmv[0]))
2952 :     CHECK_MV16_CANDIDATE(prevMB->i_mvs[0].x, prevMB->i_mvs[0].y);
2953 :    
2954 :     // left neighbour, if allowed
2955 :    
2956 :     if (!MVzero(pmv[1]))
2957 :     if (!MVequal(pmv[1], prevMB->i_mvs[0]))
2958 :     if (!MVequal(pmv[1], pmv[0]))
2959 :     CHECK_MV16_CANDIDATE(pmv[1].x, pmv[1].y);
2960 :    
2961 :     // top neighbour, if allowed
2962 :     if (!MVzero(pmv[2]))
2963 :     if (!MVequal(pmv[2], prevMB->i_mvs[0]))
2964 :     if (!MVequal(pmv[2], pmv[0]))
2965 :     if (!MVequal(pmv[2], pmv[1]))
2966 :     CHECK_MV16_CANDIDATE(pmv[2].x, pmv[2].y);
2967 :    
2968 :     // top right neighbour, if allowed
2969 :     if (!MVzero(pmv[3]))
2970 :     if (!MVequal(pmv[3], prevMB->i_mvs[0]))
2971 :     if (!MVequal(pmv[3], pmv[0]))
2972 :     if (!MVequal(pmv[3], pmv[1]))
2973 :     if (!MVequal(pmv[3], pmv[2]))
2974 :     CHECK_MV16_CANDIDATE(pmv[3].x,
2975 :     pmv[3].y);
2976 :    
2977 :     if ((MVzero(*currMV)) &&
2978 :     (!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96) */ )
2979 :     iMinSAD -= MV16_00_BIAS;
2980 :    
2981 :    
2982 :     /* Step 6: If MinSAD <= thresa goto Step 10.
2983 :     If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10.
2984 :     */
2985 :    
2986 :     if ((iMinSAD <= threshA) ||
2987 :     (MVequal(*currMV, prevMB->i_mvs[0]) &&
2988 :     ((int32_t) iMinSAD < prevMB->i_sad16))) {
2989 :    
2990 :     if (MotionFlags & PMV_EARLYSTOP16)
2991 :     goto PMVfastInt16_Terminate_with_Refine;
2992 :     }
2993 :    
2994 :    
2995 :     /************ (Diamond Search) **************/
2996 :     /*
2997 :     Step 7: Perform Diamond search, with either the small or large diamond.
2998 :     If Found=2 only examine one Diamond pattern, and afterwards goto step 10
2999 :     Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond.
3000 :     If center then goto step 10.
3001 :     Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center.
3002 :     Refine by using small diamond and goto step 10.
3003 :     */
3004 :    
3005 :     if (MotionFlags & PMV_USESQUARES16)
3006 :     MainSearchPtr = Square16_MainSearch;
3007 :     else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
3008 :     MainSearchPtr = AdvDiamond16_MainSearch;
3009 :     else
3010 :     MainSearchPtr = Diamond16_MainSearch;
3011 :    
3012 :     backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */
3013 :    
3014 :    
3015 :     /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
3016 :     iSAD =
3017 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
3018 : chl 326 currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
3019 : chl 289 min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
3020 :     iQuant, iFound);
3021 :    
3022 :     if (iSAD < iMinSAD) {
3023 :     *currMV = newMV;
3024 :     iMinSAD = iSAD;
3025 :     }
3026 :    
3027 :     if (MotionFlags & PMV_EXTSEARCH16) {
3028 :     /* extended: search (up to) two more times: orignal prediction and (0,0) */
3029 :    
3030 :     if (!(MVequal(pmv[0], backupMV))) {
3031 :     iSAD =
3032 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
3033 : chl 326 pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
3034 : chl 289 min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
3035 :     iDiamondSize, iFcode, iQuant, iFound);
3036 :    
3037 :     if (iSAD < iMinSAD) {
3038 :     *currMV = newMV;
3039 :     iMinSAD = iSAD;
3040 :     }
3041 :     }
3042 :    
3043 :     if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
3044 :     iSAD =
3045 :     (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
3046 : chl 326 iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
3047 : chl 289 max_dy, iEdgedWidth, iDiamondSize, iFcode,
3048 :     iQuant, iFound);
3049 :    
3050 :     if (iSAD < iMinSAD) {
3051 :     *currMV = newMV;
3052 :     iMinSAD = iSAD;
3053 :     }
3054 :     }
3055 :     }
3056 :    
3057 :     /*
3058 :     Step 10: The motion vector is chosen according to the block corresponding to MinSAD.
3059 :     */
3060 :    
3061 :     PMVfastInt16_Terminate_with_Refine:
3062 :    
3063 :     pMB->i_mvs[0] = pMB->i_mvs[1] = pMB->i_mvs[2] = pMB->i_mvs[3] = pMB->i_mv16 = *currMV;
3064 :     pMB->i_sad8[0] = pMB->i_sad8[1] = pMB->i_sad8[2] = pMB->i_sad8[3] = pMB->i_sad16 = iMinSAD;
3065 :    
3066 :     if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step
3067 :     iMinSAD =
3068 :     Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
3069 : chl 326 iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
3070 : chl 289 iFcode, iQuant, iEdgedWidth);
3071 :    
3072 :     pmv[0] = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); // get _REAL_ prediction (halfpel possible)
3073 :    
3074 :     PMVfastInt16_Terminate_without_Refine:
3075 : chl 326 currPMV->x = currMV->x - center_x;
3076 :     currPMV->y = currMV->y - center_y;
3077 : chl 289 return iMinSAD;
3078 :     }
3079 :    

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