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

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