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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.54 - (view) (download)

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

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