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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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

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