[svn] / branches / Isibaar / xvidcore / src / motion / motion_est.c Repository:
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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

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