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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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