Parent Directory
|
Revision Log
Revision 96 - (view) (download)
1 : | Isibaar | 3 | /************************************************************************** |
2 : | * | ||
3 : | * Modifications: | ||
4 : | * | ||
5 : | chl | 96 | * 02.04.2002 add EPZS(^2) as ME algorithm, use PMV_USESQUARES to choose between |
6 : | * EPZS and EPZS^2 | ||
7 : | Isibaar | 3 | * 08.02.2002 split up PMVfast into three routines: PMVFast, PMVFast_MainLoop |
8 : | * PMVFast_Refine to support multiple searches with different start points | ||
9 : | edgomez | 78 | * 07.01.2002 uv-block-based interpolation |
10 : | Isibaar | 3 | * 06.01.2002 INTER/INTRA-decision is now done before any SEARCH8 (speedup) |
11 : | edgomez | 78 | * changed INTER_BIAS to 150 (as suggested by suxen_drol) |
12 : | * removed halfpel refinement step in PMVfastSearch8 + quality=5 | ||
13 : | * added new quality mode = 6 which performs halfpel refinement | ||
14 : | * filesize difference between quality 5 and 6 is smaller than 1% | ||
15 : | Isibaar | 3 | * (Isibaar) |
16 : | * 31.12.2001 PMVfastSearch16 and PMVfastSearch8 (gruel) | ||
17 : | edgomez | 78 | * 30.12.2001 get_range/MotionSearchX simplified; blue/green bug fix |
18 : | * 22.12.2001 commented best_point==99 check | ||
19 : | * 19.12.2001 modified get_range (purple bug fix) | ||
20 : | Isibaar | 3 | * 15.12.2001 moved pmv displacement from mbprediction |
21 : | * 02.12.2001 motion estimation/compensation split (Isibaar) | ||
22 : | edgomez | 78 | * 16.11.2001 rewrote/tweaked search algorithms; pross@cs.rmit.edu.au |
23 : | Isibaar | 3 | * 10.11.2001 support for sad16/sad8 functions |
24 : | * 28.08.2001 reactivated MODE_INTER4V for EXT_MODE | ||
25 : | * 24.08.2001 removed MODE_INTER4V_Q, disabled MODE_INTER4V for EXT_MODE | ||
26 : | edgomez | 78 | * 22.08.2001 added MODE_INTER4V_Q |
27 : | Isibaar | 3 | * 20.08.2001 added pragma to get rid of internal compiler error with VC6 |
28 : | * idea by Cyril. Thanks. | ||
29 : | * | ||
30 : | * Michael Militzer <isibaar@videocoding.de> | ||
31 : | * | ||
32 : | **************************************************************************/ | ||
33 : | |||
34 : | #include <assert.h> | ||
35 : | #include <stdio.h> | ||
36 : | chl | 96 | #include <stdlib.h> |
37 : | Isibaar | 3 | |
38 : | #include "../encoder.h" | ||
39 : | #include "../utils/mbfunctions.h" | ||
40 : | #include "../prediction/mbprediction.h" | ||
41 : | #include "../global.h" | ||
42 : | #include "../utils/timer.h" | ||
43 : | #include "sad.h" | ||
44 : | |||
45 : | // very large value | ||
46 : | #define MV_MAX_ERROR (4096 * 256) | ||
47 : | |||
48 : | // stop search if sdelta < THRESHOLD | ||
49 : | #define MV16_THRESHOLD 192 | ||
50 : | #define MV8_THRESHOLD 56 | ||
51 : | |||
52 : | /* sad16(0,0) bias; mpeg4 spec suggests nb/2+1 */ | ||
53 : | /* nb = vop pixels * 2^(bpp-8) */ | ||
54 : | #define MV16_00_BIAS (128+1) | ||
55 : | |||
56 : | /* INTER bias for INTER/INTRA decision; mpeg4 spec suggests 2*nb */ | ||
57 : | #define INTER_BIAS 512 | ||
58 : | |||
59 : | /* Parameters which control inter/inter4v decision */ | ||
60 : | #define IMV16X16 5 | ||
61 : | |||
62 : | /* vector map (vlc delta size) smoother parameters */ | ||
63 : | #define NEIGH_TEND_16X16 2 | ||
64 : | #define NEIGH_TEND_8X8 2 | ||
65 : | |||
66 : | |||
67 : | // fast ((A)/2)*2 | ||
68 : | #define EVEN(A) (((A)<0?(A)+1:(A)) & ~1) | ||
69 : | |||
70 : | |||
71 : | #define MIN(X, Y) ((X)<(Y)?(X):(Y)) | ||
72 : | #define MAX(X, Y) ((X)>(Y)?(X):(Y)) | ||
73 : | edgomez | 78 | #define ABS(X) (((X)>0)?(X):-(X)) |
74 : | #define SIGN(X) (((X)>0)?1:-1) | ||
75 : | Isibaar | 3 | |
76 : | chl | 96 | int32_t PMVfastSearch16( |
77 : | const uint8_t * const pRef, | ||
78 : | const uint8_t * const pRefH, | ||
79 : | const uint8_t * const pRefV, | ||
80 : | const uint8_t * const pRefHV, | ||
81 : | const IMAGE * const pCur, | ||
82 : | const int x, const int y, | ||
83 : | const uint32_t MotionFlags, | ||
84 : | const MBParam * const pParam, | ||
85 : | MACROBLOCK * const pMBs, | ||
86 : | VECTOR * const currMV, | ||
87 : | VECTOR * const currPMV); | ||
88 : | Isibaar | 3 | |
89 : | chl | 96 | int32_t EPZSSearch16( |
90 : | const uint8_t * const pRef, | ||
91 : | const uint8_t * const pRefH, | ||
92 : | const uint8_t * const pRefV, | ||
93 : | const uint8_t * const pRefHV, | ||
94 : | const IMAGE * const pCur, | ||
95 : | const int x, const int y, | ||
96 : | const uint32_t MotionFlags, | ||
97 : | const MBParam * const pParam, | ||
98 : | MACROBLOCK * const pMBs, | ||
99 : | VECTOR * const currMV, | ||
100 : | VECTOR * const currPMV); | ||
101 : | |||
102 : | |||
103 : | Isibaar | 3 | int32_t PMVfastSearch8( |
104 : | chl | 96 | const uint8_t * const pRef, |
105 : | const uint8_t * const pRefH, | ||
106 : | const uint8_t * const pRefV, | ||
107 : | const uint8_t * const pRefHV, | ||
108 : | const IMAGE * const pCur, | ||
109 : | const int x, const int y, | ||
110 : | const int start_x, int start_y, | ||
111 : | const uint32_t MotionFlags, | ||
112 : | const MBParam * const pParam, | ||
113 : | MACROBLOCK * const pMBs, | ||
114 : | VECTOR * const currMV, | ||
115 : | VECTOR * const currPMV); | ||
116 : | |||
117 : | int32_t EPZSSearch8( | ||
118 : | const uint8_t * const pRef, | ||
119 : | const uint8_t * const pRefH, | ||
120 : | const uint8_t * const pRefV, | ||
121 : | const uint8_t * const pRefHV, | ||
122 : | const IMAGE * const pCur, | ||
123 : | const int x, const int y, | ||
124 : | const int start_x, int start_y, | ||
125 : | const uint32_t MotionFlags, | ||
126 : | const MBParam * const pParam, | ||
127 : | MACROBLOCK * const pMBs, | ||
128 : | VECTOR * const currMV, | ||
129 : | VECTOR * const currPMV); | ||
130 : | |||
131 : | |||
132 : | typedef int32_t (MainSearch16Func)( | ||
133 : | edgomez | 78 | const uint8_t * const pRef, |
134 : | const uint8_t * const pRefH, | ||
135 : | const uint8_t * const pRefV, | ||
136 : | const uint8_t * const pRefHV, | ||
137 : | chl | 96 | const uint8_t * const cur, |
138 : | edgomez | 78 | const int x, const int y, |
139 : | chl | 96 | int32_t startx, int32_t starty, |
140 : | int32_t iMinSAD, | ||
141 : | edgomez | 78 | VECTOR * const currMV, |
142 : | chl | 96 | const VECTOR * const pmv, |
143 : | const int32_t min_dx, const int32_t max_dx, | ||
144 : | const int32_t min_dy, const int32_t max_dy, | ||
145 : | const int32_t iEdgedWidth, | ||
146 : | const int32_t iDiamondSize, | ||
147 : | const int32_t iFcode, | ||
148 : | const int32_t iQuant, | ||
149 : | int iFound); | ||
150 : | Isibaar | 3 | |
151 : | chl | 96 | typedef MainSearch16Func* MainSearch16FuncPtr; |
152 : | |||
153 : | |||
154 : | typedef int32_t (MainSearch8Func)( | ||
155 : | edgomez | 78 | const uint8_t * const pRef, |
156 : | const uint8_t * const pRefH, | ||
157 : | const uint8_t * const pRefV, | ||
158 : | const uint8_t * const pRefHV, | ||
159 : | chl | 96 | const uint8_t * const cur, |
160 : | edgomez | 78 | const int x, const int y, |
161 : | chl | 96 | int32_t startx, int32_t starty, |
162 : | int32_t iMinSAD, | ||
163 : | edgomez | 78 | VECTOR * const currMV, |
164 : | chl | 96 | const VECTOR * const pmv, |
165 : | const int32_t min_dx, const int32_t max_dx, | ||
166 : | const int32_t min_dy, const int32_t max_dy, | ||
167 : | const int32_t iEdgedWidth, | ||
168 : | const int32_t iDiamondSize, | ||
169 : | const int32_t iFcode, | ||
170 : | const int32_t iQuant, | ||
171 : | int iFound); | ||
172 : | Isibaar | 3 | |
173 : | chl | 96 | typedef MainSearch8Func* MainSearch8FuncPtr; |
174 : | Isibaar | 3 | |
175 : | // mv.length table | ||
176 : | static const uint32_t mvtab[33] = { | ||
177 : | 1, 2, 3, 4, 6, 7, 7, 7, | ||
178 : | 9, 9, 9, 10, 10, 10, 10, 10, | ||
179 : | 10, 10, 10, 10, 10, 10, 10, 10, | ||
180 : | 10, 11, 11, 11, 11, 11, 11, 12, 12 | ||
181 : | }; | ||
182 : | |||
183 : | |||
184 : | static __inline uint32_t mv_bits(int32_t component, const uint32_t iFcode) | ||
185 : | { | ||
186 : | if (component == 0) | ||
187 : | return 1; | ||
188 : | |||
189 : | if (component < 0) | ||
190 : | component = -component; | ||
191 : | |||
192 : | if (iFcode == 1) | ||
193 : | { | ||
194 : | if (component > 32) | ||
195 : | component = 32; | ||
196 : | |||
197 : | return mvtab[component] + 1; | ||
198 : | } | ||
199 : | |||
200 : | component += (1 << (iFcode - 1)) - 1; | ||
201 : | component >>= (iFcode - 1); | ||
202 : | |||
203 : | if (component > 32) | ||
204 : | component = 32; | ||
205 : | |||
206 : | return mvtab[component] + 1 + iFcode - 1; | ||
207 : | } | ||
208 : | |||
209 : | |||
210 : | static __inline uint32_t calc_delta_16(const int32_t dx, const int32_t dy, const uint32_t iFcode) | ||
211 : | { | ||
212 : | return NEIGH_TEND_16X16 * (mv_bits(dx, iFcode) + mv_bits(dy, iFcode)); | ||
213 : | } | ||
214 : | |||
215 : | static __inline uint32_t calc_delta_8(const int32_t dx, const int32_t dy, const uint32_t iFcode) | ||
216 : | |||
217 : | { | ||
218 : | return NEIGH_TEND_8X8 * (mv_bits(dx, iFcode) + mv_bits(dy, iFcode)); | ||
219 : | } | ||
220 : | |||
221 : | |||
222 : | |||
223 : | |||
224 : | /* calculate the min/max range (in halfpixels) | ||
225 : | relative to the _MACROBLOCK_ position | ||
226 : | */ | ||
227 : | |||
228 : | static void __inline get_range( | ||
229 : | edgomez | 78 | int32_t * const min_dx, int32_t * const max_dx, |
230 : | int32_t * const min_dy, int32_t * const max_dy, | ||
231 : | const uint32_t x, const uint32_t y, | ||
232 : | const uint32_t block_sz, // block dimension, 8 or 16 | ||
233 : | const uint32_t width, const uint32_t height, | ||
234 : | const uint32_t fcode) | ||
235 : | Isibaar | 3 | { |
236 : | edgomez | 78 | |
237 : | Isibaar | 3 | const int search_range = 32 << (fcode - 1); |
238 : | edgomez | 78 | const int high = search_range - 1; |
239 : | const int low = -search_range; | ||
240 : | Isibaar | 3 | |
241 : | // convert full-pixel measurements to half pixel | ||
242 : | const int hp_width = 2 * width; | ||
243 : | const int hp_height = 2 * height; | ||
244 : | const int hp_edge = 2 * block_sz; | ||
245 : | const int hp_x = 2 * (x) * block_sz; // we need _right end_ of block, not x-coordinate | ||
246 : | const int hp_y = 2 * (y) * block_sz; // same for _bottom end_ | ||
247 : | |||
248 : | edgomez | 78 | *max_dx = MIN(high, hp_width - hp_x); |
249 : | *max_dy = MIN(high, hp_height - hp_y); | ||
250 : | *min_dx = MAX(low, -(hp_edge + hp_x)); | ||
251 : | *min_dy = MAX(low, -(hp_edge + hp_y)); | ||
252 : | |||
253 : | Isibaar | 3 | } |
254 : | |||
255 : | |||
256 : | edgomez | 78 | /* |
257 : | * getref: calculate reference image pointer | ||
258 : | * the decision to use interpolation h/v/hv or the normal image is | ||
259 : | * based on dx & dy. | ||
260 : | */ | ||
261 : | Isibaar | 3 | |
262 : | static __inline const uint8_t * get_ref( | ||
263 : | edgomez | 78 | const uint8_t * const refn, |
264 : | const uint8_t * const refh, | ||
265 : | const uint8_t * const refv, | ||
266 : | const uint8_t * const refhv, | ||
267 : | const uint32_t x, const uint32_t y, | ||
268 : | const uint32_t block, // block dimension, 8 or 16 | ||
269 : | const int32_t dx, const int32_t dy, | ||
270 : | const uint32_t stride) | ||
271 : | Isibaar | 3 | { |
272 : | edgomez | 78 | |
273 : | Isibaar | 3 | switch ( ((dx&1)<<1) + (dy&1) ) // ((dx%2)?2:0)+((dy%2)?1:0) |
274 : | edgomez | 78 | { |
275 : | case 0 : return refn + (x*block+dx/2) + (y*block+dy/2)*stride; | ||
276 : | case 1 : return refv + (x*block+dx/2) + (y*block+(dy-1)/2)*stride; | ||
277 : | case 2 : return refh + (x*block+(dx-1)/2) + (y*block+dy/2)*stride; | ||
278 : | Isibaar | 3 | default : |
279 : | edgomez | 78 | case 3 : return refhv + (x*block+(dx-1)/2) + (y*block+(dy-1)/2)*stride; |
280 : | Isibaar | 3 | } |
281 : | edgomez | 78 | |
282 : | Isibaar | 3 | } |
283 : | |||
284 : | |||
285 : | /* This is somehow a copy of get_ref, but with MV instead of X,Y */ | ||
286 : | |||
287 : | static __inline const uint8_t * get_ref_mv( | ||
288 : | edgomez | 78 | const uint8_t * const refn, |
289 : | const uint8_t * const refh, | ||
290 : | const uint8_t * const refv, | ||
291 : | const uint8_t * const refhv, | ||
292 : | const uint32_t x, const uint32_t y, | ||
293 : | const uint32_t block, // block dimension, 8 or 16 | ||
294 : | const VECTOR* mv, // measured in half-pel! | ||
295 : | const uint32_t stride) | ||
296 : | Isibaar | 3 | { |
297 : | edgomez | 78 | |
298 : | Isibaar | 3 | switch ( (((mv->x)&1)<<1) + ((mv->y)&1) ) |
299 : | edgomez | 78 | { |
300 : | case 0 : return refn + (x*block+(mv->x)/2) + (y*block+(mv->y)/2)*stride; | ||
301 : | case 1 : return refv + (x*block+(mv->x)/2) + (y*block+((mv->y)-1)/2)*stride; | ||
302 : | case 2 : return refh + (x*block+((mv->x)-1)/2) + (y*block+(mv->y)/2)*stride; | ||
303 : | Isibaar | 3 | default : |
304 : | edgomez | 78 | case 3 : return refhv + (x*block+((mv->x)-1)/2) + (y*block+((mv->y)-1)/2)*stride; |
305 : | Isibaar | 3 | } |
306 : | edgomez | 78 | |
307 : | Isibaar | 3 | } |
308 : | |||
309 : | #ifndef SEARCH16 | ||
310 : | #define SEARCH16 PMVfastSearch16 | ||
311 : | chl | 96 | //#define SEARCH16 FullSearch16 |
312 : | //#define SEARCH16 EPZSSearch16 | ||
313 : | Isibaar | 3 | #endif |
314 : | |||
315 : | #ifndef SEARCH8 | ||
316 : | #define SEARCH8 PMVfastSearch8 | ||
317 : | chl | 96 | //#define SEARCH8 EPZSSearch8 |
318 : | Isibaar | 3 | #endif |
319 : | |||
320 : | bool MotionEstimation( | ||
321 : | edgomez | 78 | MACROBLOCK * const pMBs, |
322 : | MBParam * const pParam, | ||
323 : | const IMAGE * const pRef, | ||
324 : | const IMAGE * const pRefH, | ||
325 : | const IMAGE * const pRefV, | ||
326 : | const IMAGE * const pRefHV, | ||
327 : | IMAGE * const pCurrent, | ||
328 : | const uint32_t iLimit) | ||
329 : | Isibaar | 3 | |
330 : | { | ||
331 : | edgomez | 78 | const uint32_t iWcount = pParam->mb_width; |
332 : | const uint32_t iHcount = pParam->mb_height; | ||
333 : | Isibaar | 3 | |
334 : | uint32_t i, j, iIntra = 0; | ||
335 : | |||
336 : | edgomez | 78 | VECTOR mv16; |
337 : | VECTOR pmv16; | ||
338 : | Isibaar | 3 | |
339 : | edgomez | 78 | int32_t sad8 = 0; |
340 : | int32_t sad16; | ||
341 : | int32_t deviation; | ||
342 : | Isibaar | 3 | |
343 : | // note: i==horizontal, j==vertical | ||
344 : | edgomez | 78 | for (i = 0; i < iHcount; i++) |
345 : | Isibaar | 3 | for (j = 0; j < iWcount; j++) |
346 : | { | ||
347 : | MACROBLOCK *pMB = &pMBs[j + i * iWcount]; | ||
348 : | |||
349 : | sad16 = SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, | ||
350 : | edgomez | 78 | j, i, pParam->motion_flags, |
351 : | pParam, pMBs, &mv16, &pmv16); | ||
352 : | Isibaar | 3 | pMB->sad16=sad16; |
353 : | |||
354 : | |||
355 : | edgomez | 78 | /* decide: MODE_INTER or MODE_INTRA |
356 : | if (dev_intra < sad_inter - 2 * nb) use_intra | ||
357 : | */ | ||
358 : | Isibaar | 3 | |
359 : | edgomez | 78 | deviation = dev16(pCurrent->y + j*16 + i*16*pParam->edged_width, pParam->edged_width); |
360 : | Isibaar | 3 | |
361 : | edgomez | 78 | if (deviation < (sad16 - INTER_BIAS)) |
362 : | { | ||
363 : | pMB->mode = MODE_INTRA; | ||
364 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; | ||
365 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; | ||
366 : | Isibaar | 3 | |
367 : | edgomez | 78 | iIntra++; |
368 : | if(iIntra >= iLimit) | ||
369 : | return 1; | ||
370 : | Isibaar | 3 | |
371 : | edgomez | 78 | continue; |
372 : | } | ||
373 : | Isibaar | 3 | |
374 : | edgomez | 78 | if (pParam->global_flags & XVID_INTER4V) |
375 : | { | ||
376 : | pMB->sad8[0] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, | ||
377 : | 2 * j, 2 * i, mv16.x, mv16.y, pParam->motion_flags, | ||
378 : | pParam, pMBs, &pMB->mvs[0], &pMB->pmvs[0]); | ||
379 : | Isibaar | 3 | |
380 : | edgomez | 78 | pMB->sad8[1] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
381 : | 2 * j + 1, 2 * i, mv16.x, mv16.y, pParam->motion_flags, | ||
382 : | pParam, pMBs, &pMB->mvs[1], &pMB->pmvs[1]); | ||
383 : | Isibaar | 3 | |
384 : | edgomez | 78 | pMB->sad8[2] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
385 : | 2 * j, 2 * i + 1, mv16.x, mv16.y, pParam->motion_flags, | ||
386 : | pParam, pMBs, &pMB->mvs[2], &pMB->pmvs[2]); | ||
387 : | Isibaar | 3 | |
388 : | edgomez | 78 | pMB->sad8[3] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
389 : | 2 * j + 1, 2 * i + 1, mv16.x, mv16.y, pParam->motion_flags, | ||
390 : | pParam, pMBs, &pMB->mvs[3], &pMB->pmvs[3]); | ||
391 : | Isibaar | 3 | |
392 : | edgomez | 78 | sad8 = pMB->sad8[0] + pMB->sad8[1] + pMB->sad8[2] + pMB->sad8[3]; |
393 : | } | ||
394 : | Isibaar | 3 | |
395 : | |||
396 : | edgomez | 78 | /* decide: MODE_INTER or MODE_INTER4V |
397 : | mpeg4: if (sad8 < sad16 - nb/2+1) use_inter4v | ||
398 : | */ | ||
399 : | Isibaar | 3 | |
400 : | edgomez | 78 | if (pMB->dquant == NO_CHANGE) { |
401 : | if (((pParam->global_flags & XVID_INTER4V)==0) || | ||
402 : | (sad16 < (sad8 + (int32_t)(IMV16X16 * pParam->quant)))) { | ||
403 : | Isibaar | 3 | |
404 : | edgomez | 78 | sad8 = sad16; |
405 : | pMB->mode = MODE_INTER; | ||
406 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = mv16.x; | ||
407 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = mv16.y; | ||
408 : | pMB->pmvs[0].x = pmv16.x; | ||
409 : | pMB->pmvs[0].y = pmv16.y; | ||
410 : | } | ||
411 : | else | ||
412 : | pMB->mode = MODE_INTER4V; | ||
413 : | } | ||
414 : | else | ||
415 : | { | ||
416 : | Isibaar | 3 | sad8 = sad16; |
417 : | pMB->mode = MODE_INTER; | ||
418 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = mv16.x; | ||
419 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = mv16.y; | ||
420 : | pMB->pmvs[0].x = pmv16.x; | ||
421 : | pMB->pmvs[0].y = pmv16.y; | ||
422 : | } | ||
423 : | } | ||
424 : | |||
425 : | return 0; | ||
426 : | } | ||
427 : | |||
428 : | #define MVzero(A) ( ((A).x)==(0) && ((A).y)==(0) ) | ||
429 : | |||
430 : | #define MVequal(A,B) ( ((A).x)==((B).x) && ((A).y)==((B).y) ) | ||
431 : | |||
432 : | |||
433 : | #define CHECK_MV16_ZERO {\ | ||
434 : | if ( (0 <= max_dx) && (0 >= min_dx) \ | ||
435 : | && (0 <= max_dy) && (0 >= min_dy) ) \ | ||
436 : | { \ | ||
437 : | iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0, 0 , iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); \ | ||
438 : | iSAD += calc_delta_16(-pmv[0].x, -pmv[0].y, (uint8_t)iFcode) * iQuant;\ | ||
439 : | if (iSAD <= iQuant * 96) \ | ||
440 : | iSAD -= MV16_00_BIAS; \ | ||
441 : | if (iSAD < iMinSAD) \ | ||
442 : | { iMinSAD=iSAD; currMV->x=0; currMV->y=0; } } \ | ||
443 : | } | ||
444 : | |||
445 : | chl | 96 | #define NOCHECK_MV16_CANDIDATE(X,Y) { \ |
446 : | iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ | ||
447 : | iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode) * iQuant;\ | ||
448 : | if (iSAD < iMinSAD) \ | ||
449 : | { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \ | ||
450 : | } | ||
451 : | Isibaar | 3 | |
452 : | #define CHECK_MV16_CANDIDATE(X,Y) { \ | ||
453 : | if ( ((X) <= max_dx) && ((X) >= min_dx) \ | ||
454 : | && ((Y) <= max_dy) && ((Y) >= min_dy) ) \ | ||
455 : | { \ | ||
456 : | iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ | ||
457 : | iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode) * iQuant;\ | ||
458 : | if (iSAD < iMinSAD) \ | ||
459 : | { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \ | ||
460 : | } | ||
461 : | |||
462 : | #define CHECK_MV16_CANDIDATE_DIR(X,Y,D) { \ | ||
463 : | if ( ((X) <= max_dx) && ((X) >= min_dx) \ | ||
464 : | && ((Y) <= max_dy) && ((Y) >= min_dy) ) \ | ||
465 : | { \ | ||
466 : | iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ | ||
467 : | iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode) * iQuant;\ | ||
468 : | if (iSAD < iMinSAD) \ | ||
469 : | { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \ | ||
470 : | } | ||
471 : | |||
472 : | #define CHECK_MV16_CANDIDATE_FOUND(X,Y,D) { \ | ||
473 : | if ( ((X) <= max_dx) && ((X) >= min_dx) \ | ||
474 : | && ((Y) <= max_dy) && ((Y) >= min_dy) ) \ | ||
475 : | { \ | ||
476 : | iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ | ||
477 : | iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode) * iQuant;\ | ||
478 : | if (iSAD < iMinSAD) \ | ||
479 : | { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \ | ||
480 : | } | ||
481 : | |||
482 : | |||
483 : | #define CHECK_MV8_ZERO {\ | ||
484 : | iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, 0, 0 , iEdgedWidth), iEdgedWidth); \ | ||
485 : | iSAD += calc_delta_8(-pmv[0].x, -pmv[0].y, (uint8_t)iFcode) * iQuant;\ | ||
486 : | if (iSAD < iMinSAD) \ | ||
487 : | { iMinSAD=iSAD; currMV->x=0; currMV->y=0; } \ | ||
488 : | } | ||
489 : | |||
490 : | chl | 96 | #define NOCHECK_MV8_CANDIDATE(X,Y) \ |
491 : | { \ | ||
492 : | iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \ | ||
493 : | iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode) * iQuant;\ | ||
494 : | if (iSAD < iMinSAD) \ | ||
495 : | { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \ | ||
496 : | } | ||
497 : | Isibaar | 3 | |
498 : | #define CHECK_MV8_CANDIDATE(X,Y) { \ | ||
499 : | if ( ((X) <= max_dx) && ((X) >= min_dx) \ | ||
500 : | && ((Y) <= max_dy) && ((Y) >= min_dy) ) \ | ||
501 : | { \ | ||
502 : | iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \ | ||
503 : | iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode) * iQuant;\ | ||
504 : | if (iSAD < iMinSAD) \ | ||
505 : | { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \ | ||
506 : | } | ||
507 : | |||
508 : | #define CHECK_MV8_CANDIDATE_DIR(X,Y,D) { \ | ||
509 : | if ( ((X) <= max_dx) && ((X) >= min_dx) \ | ||
510 : | && ((Y) <= max_dy) && ((Y) >= min_dy) ) \ | ||
511 : | { \ | ||
512 : | iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \ | ||
513 : | iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode) * iQuant;\ | ||
514 : | if (iSAD < iMinSAD) \ | ||
515 : | { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \ | ||
516 : | } | ||
517 : | |||
518 : | #define CHECK_MV8_CANDIDATE_FOUND(X,Y,D) { \ | ||
519 : | if ( ((X) <= max_dx) && ((X) >= min_dx) \ | ||
520 : | && ((Y) <= max_dy) && ((Y) >= min_dy) ) \ | ||
521 : | { \ | ||
522 : | iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \ | ||
523 : | iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode) * iQuant;\ | ||
524 : | if (iSAD < iMinSAD) \ | ||
525 : | { iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \ | ||
526 : | } | ||
527 : | |||
528 : | /* too slow and not fully functional at the moment */ | ||
529 : | /* | ||
530 : | int32_t ZeroSearch16( | ||
531 : | const uint8_t * const pRef, | ||
532 : | const uint8_t * const pRefH, | ||
533 : | const uint8_t * const pRefV, | ||
534 : | const uint8_t * const pRefHV, | ||
535 : | const IMAGE * const pCur, | ||
536 : | const int x, const int y, | ||
537 : | const uint32_t MotionFlags, | ||
538 : | MBParam * const pParam, | ||
539 : | MACROBLOCK * const pMBs, | ||
540 : | VECTOR * const currMV, | ||
541 : | VECTOR * const currPMV) | ||
542 : | { | ||
543 : | const int32_t iEdgedWidth = pParam->edged_width; | ||
544 : | const int32_t iQuant = pParam->quant; | ||
545 : | const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth; | ||
546 : | int32_t iSAD; | ||
547 : | int32_t pred_x,pred_y; | ||
548 : | |||
549 : | get_pmv(pMBs, x, y, pParam->mb_width, 0, &pred_x, &pred_y); | ||
550 : | |||
551 : | iSAD = sad16( cur, | ||
552 : | get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0,0, iEdgedWidth), | ||
553 : | iEdgedWidth, MV_MAX_ERROR); | ||
554 : | if (iSAD <= iQuant * 96) | ||
555 : | iSAD -= MV16_00_BIAS; | ||
556 : | |||
557 : | currMV->x = 0; | ||
558 : | currMV->y = 0; | ||
559 : | currPMV->x = -pred_x; | ||
560 : | currPMV->y = -pred_y; | ||
561 : | |||
562 : | return iSAD; | ||
563 : | |||
564 : | } | ||
565 : | */ | ||
566 : | |||
567 : | chl | 96 | int32_t Diamond16_MainSearch( |
568 : | edgomez | 78 | const uint8_t * const pRef, |
569 : | const uint8_t * const pRefH, | ||
570 : | const uint8_t * const pRefV, | ||
571 : | const uint8_t * const pRefHV, | ||
572 : | const uint8_t * const cur, | ||
573 : | const int x, const int y, | ||
574 : | int32_t startx, int32_t starty, | ||
575 : | int32_t iMinSAD, | ||
576 : | VECTOR * const currMV, | ||
577 : | const VECTOR * const pmv, | ||
578 : | const int32_t min_dx, const int32_t max_dx, | ||
579 : | const int32_t min_dy, const int32_t max_dy, | ||
580 : | const int32_t iEdgedWidth, | ||
581 : | const int32_t iDiamondSize, | ||
582 : | const int32_t iFcode, | ||
583 : | const int32_t iQuant, | ||
584 : | int iFound) | ||
585 : | Isibaar | 3 | { |
586 : | /* Do a diamond search around given starting point, return SAD of best */ | ||
587 : | |||
588 : | int32_t iDirection=0; | ||
589 : | int32_t iSAD; | ||
590 : | VECTOR backupMV; | ||
591 : | backupMV.x = startx; | ||
592 : | backupMV.y = starty; | ||
593 : | |||
594 : | /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */ | ||
595 : | |||
596 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y,1); | ||
597 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2); | ||
598 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3); | ||
599 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4); | ||
600 : | |||
601 : | if (iDirection) | ||
602 : | while (!iFound) | ||
603 : | { | ||
604 : | iFound = 1; | ||
605 : | backupMV=*currMV; | ||
606 : | |||
607 : | if ( iDirection != 2) | ||
608 : | CHECK_MV16_CANDIDATE_FOUND(backupMV.x-iDiamondSize,backupMV.y,1); | ||
609 : | if ( iDirection != 1) | ||
610 : | CHECK_MV16_CANDIDATE_FOUND(backupMV.x+iDiamondSize,backupMV.y,2); | ||
611 : | if ( iDirection != 4) | ||
612 : | CHECK_MV16_CANDIDATE_FOUND(backupMV.x,backupMV.y-iDiamondSize,3); | ||
613 : | if ( iDirection != 3) | ||
614 : | CHECK_MV16_CANDIDATE_FOUND(backupMV.x,backupMV.y+iDiamondSize,4); | ||
615 : | } | ||
616 : | else | ||
617 : | edgomez | 78 | { |
618 : | currMV->x = startx; | ||
619 : | currMV->y = starty; | ||
620 : | } | ||
621 : | Isibaar | 3 | return iMinSAD; |
622 : | } | ||
623 : | |||
624 : | chl | 96 | int32_t Square16_MainSearch( |
625 : | const uint8_t * const pRef, | ||
626 : | const uint8_t * const pRefH, | ||
627 : | const uint8_t * const pRefV, | ||
628 : | const uint8_t * const pRefHV, | ||
629 : | const uint8_t * const cur, | ||
630 : | const int x, const int y, | ||
631 : | int32_t startx, int32_t starty, | ||
632 : | int32_t iMinSAD, | ||
633 : | VECTOR * const currMV, | ||
634 : | const VECTOR * const pmv, | ||
635 : | const int32_t min_dx, const int32_t max_dx, | ||
636 : | const int32_t min_dy, const int32_t max_dy, | ||
637 : | const int32_t iEdgedWidth, | ||
638 : | const int32_t iDiamondSize, | ||
639 : | const int32_t iFcode, | ||
640 : | const int32_t iQuant, | ||
641 : | int iFound) | ||
642 : | { | ||
643 : | /* Do a square search around given starting point, return SAD of best */ | ||
644 : | |||
645 : | int32_t iDirection=0; | ||
646 : | int32_t iSAD; | ||
647 : | VECTOR backupMV; | ||
648 : | backupMV.x = startx; | ||
649 : | backupMV.y = starty; | ||
650 : | |||
651 : | /* It's one search with full square pattern, and new parts for all following diamonds */ | ||
652 : | |||
653 : | /* new direction are extra, so 1-4 is normal diamond | ||
654 : | 537 | ||
655 : | 1*2 | ||
656 : | 648 | ||
657 : | */ | ||
658 : | |||
659 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y,1); | ||
660 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2); | ||
661 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3); | ||
662 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4); | ||
663 : | |||
664 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5); | ||
665 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6); | ||
666 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7); | ||
667 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8); | ||
668 : | |||
669 : | |||
670 : | if (iDirection) | ||
671 : | while (!iFound) | ||
672 : | { | ||
673 : | iFound = 1; | ||
674 : | backupMV=*currMV; | ||
675 : | |||
676 : | switch (iDirection) | ||
677 : | { | ||
678 : | case 1: | ||
679 : | CHECK_MV16_CANDIDATE_FOUND(backupMV.x-iDiamondSize,backupMV.y,1); | ||
680 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5); | ||
681 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7); | ||
682 : | break; | ||
683 : | case 2: | ||
684 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2); | ||
685 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6); | ||
686 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8); | ||
687 : | break; | ||
688 : | |||
689 : | case 3: | ||
690 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4); | ||
691 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7); | ||
692 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8); | ||
693 : | break; | ||
694 : | |||
695 : | case 4: | ||
696 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3); | ||
697 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5); | ||
698 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6); | ||
699 : | break; | ||
700 : | |||
701 : | case 5: | ||
702 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y,1); | ||
703 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3); | ||
704 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5); | ||
705 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6); | ||
706 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7); | ||
707 : | break; | ||
708 : | |||
709 : | case 6: | ||
710 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2); | ||
711 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3); | ||
712 : | |||
713 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5); | ||
714 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6); | ||
715 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8); | ||
716 : | |||
717 : | break; | ||
718 : | |||
719 : | case 7: | ||
720 : | CHECK_MV16_CANDIDATE_FOUND(backupMV.x-iDiamondSize,backupMV.y,1); | ||
721 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4); | ||
722 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5); | ||
723 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7); | ||
724 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8); | ||
725 : | break; | ||
726 : | |||
727 : | case 8: | ||
728 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2); | ||
729 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4); | ||
730 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6); | ||
731 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7); | ||
732 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8); | ||
733 : | break; | ||
734 : | default: | ||
735 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y,1); | ||
736 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2); | ||
737 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3); | ||
738 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4); | ||
739 : | |||
740 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y-iDiamondSize,5); | ||
741 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y+iDiamondSize,6); | ||
742 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y-iDiamondSize,7); | ||
743 : | CHECK_MV16_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y+iDiamondSize,8); | ||
744 : | break; | ||
745 : | } | ||
746 : | } | ||
747 : | else | ||
748 : | { | ||
749 : | currMV->x = startx; | ||
750 : | currMV->y = starty; | ||
751 : | } | ||
752 : | return iMinSAD; | ||
753 : | } | ||
754 : | |||
755 : | |||
756 : | int32_t Full16_MainSearch( | ||
757 : | const uint8_t * const pRef, | ||
758 : | const uint8_t * const pRefH, | ||
759 : | const uint8_t * const pRefV, | ||
760 : | const uint8_t * const pRefHV, | ||
761 : | const uint8_t * const cur, | ||
762 : | const int x, const int y, | ||
763 : | int32_t startx, int32_t starty, | ||
764 : | int32_t iMinSAD, | ||
765 : | VECTOR * const currMV, | ||
766 : | const VECTOR * const pmv, | ||
767 : | const int32_t min_dx, const int32_t max_dx, | ||
768 : | const int32_t min_dy, const int32_t max_dy, | ||
769 : | const int32_t iEdgedWidth, | ||
770 : | const int32_t iDiamondSize, | ||
771 : | const int32_t iFcode, | ||
772 : | const int32_t iQuant, | ||
773 : | int iFound) | ||
774 : | { | ||
775 : | int32_t iSAD; | ||
776 : | int32_t dx,dy; | ||
777 : | VECTOR backupMV; | ||
778 : | backupMV.x = startx; | ||
779 : | backupMV.y = starty; | ||
780 : | |||
781 : | for (dx = min_dx; dx<=max_dx; dx+=iDiamondSize) | ||
782 : | for (dy = min_dy; dy<= max_dy; dy+=iDiamondSize) | ||
783 : | NOCHECK_MV16_CANDIDATE(dx,dy); | ||
784 : | |||
785 : | return iMinSAD; | ||
786 : | } | ||
787 : | |||
788 : | int32_t Full8_MainSearch( | ||
789 : | const uint8_t * const pRef, | ||
790 : | const uint8_t * const pRefH, | ||
791 : | const uint8_t * const pRefV, | ||
792 : | const uint8_t * const pRefHV, | ||
793 : | const uint8_t * const cur, | ||
794 : | const int x, const int y, | ||
795 : | int32_t startx, int32_t starty, | ||
796 : | int32_t iMinSAD, | ||
797 : | VECTOR * const currMV, | ||
798 : | const VECTOR * const pmv, | ||
799 : | const int32_t min_dx, const int32_t max_dx, | ||
800 : | const int32_t min_dy, const int32_t max_dy, | ||
801 : | const int32_t iEdgedWidth, | ||
802 : | const int32_t iDiamondSize, | ||
803 : | const int32_t iFcode, | ||
804 : | const int32_t iQuant, | ||
805 : | int iFound) | ||
806 : | { | ||
807 : | int32_t iSAD; | ||
808 : | int32_t dx,dy; | ||
809 : | VECTOR backupMV; | ||
810 : | backupMV.x = startx; | ||
811 : | backupMV.y = starty; | ||
812 : | |||
813 : | for (dx = min_dx; dx<=max_dx; dx+=iDiamondSize) | ||
814 : | for (dy = min_dy; dy<= max_dy; dy+=iDiamondSize) | ||
815 : | NOCHECK_MV8_CANDIDATE(dx,dy); | ||
816 : | |||
817 : | return iMinSAD; | ||
818 : | } | ||
819 : | |||
820 : | |||
821 : | |||
822 : | int32_t Halfpel16_Refine( | ||
823 : | edgomez | 78 | const uint8_t * const pRef, |
824 : | const uint8_t * const pRefH, | ||
825 : | const uint8_t * const pRefV, | ||
826 : | const uint8_t * const pRefHV, | ||
827 : | const uint8_t * const cur, | ||
828 : | const int x, const int y, | ||
829 : | VECTOR * const currMV, | ||
830 : | int32_t iMinSAD, | ||
831 : | const VECTOR * const pmv, | ||
832 : | const int32_t min_dx, const int32_t max_dx, | ||
833 : | const int32_t min_dy, const int32_t max_dy, | ||
834 : | const int32_t iFcode, | ||
835 : | const int32_t iQuant, | ||
836 : | const int32_t iEdgedWidth) | ||
837 : | Isibaar | 3 | { |
838 : | /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */ | ||
839 : | |||
840 : | int32_t iSAD; | ||
841 : | VECTOR backupMV = *currMV; | ||
842 : | |||
843 : | CHECK_MV16_CANDIDATE(backupMV.x-1,backupMV.y-1); | ||
844 : | CHECK_MV16_CANDIDATE(backupMV.x ,backupMV.y-1); | ||
845 : | CHECK_MV16_CANDIDATE(backupMV.x+1,backupMV.y-1); | ||
846 : | CHECK_MV16_CANDIDATE(backupMV.x-1,backupMV.y); | ||
847 : | CHECK_MV16_CANDIDATE(backupMV.x+1,backupMV.y); | ||
848 : | CHECK_MV16_CANDIDATE(backupMV.x-1,backupMV.y+1); | ||
849 : | CHECK_MV16_CANDIDATE(backupMV.x ,backupMV.y+1); | ||
850 : | CHECK_MV16_CANDIDATE(backupMV.x+1,backupMV.y+1); | ||
851 : | |||
852 : | return iMinSAD; | ||
853 : | } | ||
854 : | |||
855 : | #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16) | ||
856 : | |||
857 : | chl | 96 | |
858 : | Isibaar | 3 | int32_t PMVfastSearch16( |
859 : | chl | 96 | const uint8_t * const pRef, |
860 : | const uint8_t * const pRefH, | ||
861 : | const uint8_t * const pRefV, | ||
862 : | const uint8_t * const pRefHV, | ||
863 : | const IMAGE * const pCur, | ||
864 : | const int x, const int y, | ||
865 : | const uint32_t MotionFlags, | ||
866 : | const MBParam * const pParam, | ||
867 : | MACROBLOCK * const pMBs, | ||
868 : | VECTOR * const currMV, | ||
869 : | VECTOR * const currPMV) | ||
870 : | Isibaar | 3 | { |
871 : | const uint32_t iWcount = pParam->mb_width; | ||
872 : | const int32_t iFcode = pParam->fixed_code; | ||
873 : | const int32_t iQuant = pParam->quant; | ||
874 : | const int32_t iWidth = pParam->width; | ||
875 : | const int32_t iHeight = pParam->height; | ||
876 : | const int32_t iEdgedWidth = pParam->edged_width; | ||
877 : | |||
878 : | const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth; | ||
879 : | |||
880 : | int32_t iDiamondSize; | ||
881 : | |||
882 : | int32_t min_dx; | ||
883 : | int32_t max_dx; | ||
884 : | int32_t min_dy; | ||
885 : | int32_t max_dy; | ||
886 : | |||
887 : | int32_t iFound; | ||
888 : | |||
889 : | VECTOR newMV; | ||
890 : | VECTOR backupMV; /* just for PMVFAST */ | ||
891 : | |||
892 : | VECTOR pmv[4]; | ||
893 : | int32_t psad[4]; | ||
894 : | |||
895 : | MACROBLOCK * const pMB = pMBs + x + y * iWcount; | ||
896 : | |||
897 : | static int32_t threshA,threshB; | ||
898 : | int32_t bPredEq; | ||
899 : | int32_t iMinSAD,iSAD; | ||
900 : | |||
901 : | /* Get maximum range */ | ||
902 : | get_range(&min_dx, &max_dx, &min_dy, &max_dy, | ||
903 : | edgomez | 78 | x, y, 16, iWidth, iHeight, iFcode); |
904 : | Isibaar | 3 | |
905 : | /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */ | ||
906 : | |||
907 : | if (!(MotionFlags & PMV_HALFPEL16 )) | ||
908 : | { min_dx = EVEN(min_dx); | ||
909 : | edgomez | 78 | max_dx = EVEN(max_dx); |
910 : | min_dy = EVEN(min_dy); | ||
911 : | max_dy = EVEN(max_dy); | ||
912 : | Isibaar | 3 | } /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */ |
913 : | |||
914 : | |||
915 : | bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad); | ||
916 : | |||
917 : | if ((x==0) && (y==0) ) | ||
918 : | { | ||
919 : | threshA = 512; | ||
920 : | threshB = 1024; | ||
921 : | |||
922 : | } | ||
923 : | else | ||
924 : | { | ||
925 : | threshA = psad[0]; | ||
926 : | threshB = threshA+256; | ||
927 : | if (threshA< 512) threshA = 512; | ||
928 : | if (threshA>1024) threshA = 1024; | ||
929 : | if (threshB>1792) threshB = 1792; | ||
930 : | } | ||
931 : | |||
932 : | iFound=0; | ||
933 : | |||
934 : | /* Step 2: Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion | ||
935 : | edgomez | 78 | vector of the median. |
936 : | If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2 | ||
937 : | Isibaar | 3 | */ |
938 : | |||
939 : | if ((bPredEq) && (MVequal(pmv[0],pMB->mvs[0]) ) ) | ||
940 : | iFound=2; | ||
941 : | |||
942 : | /* Step 3: If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search. | ||
943 : | edgomez | 78 | Otherwise select large Diamond Search. |
944 : | Isibaar | 3 | */ |
945 : | |||
946 : | if ( (pmv[0].x != 0) || (pmv[0].y != 0) || (threshB<1536) || (bPredEq) ) | ||
947 : | iDiamondSize=1; // halfpel! | ||
948 : | else | ||
949 : | iDiamondSize=2; // halfpel! | ||
950 : | |||
951 : | if (!(MotionFlags & PMV_HALFPELDIAMOND16) ) | ||
952 : | iDiamondSize*=2; | ||
953 : | |||
954 : | /* Step 4: Calculate SAD around the Median prediction. | ||
955 : | edgomez | 78 | MinSAD=SAD |
956 : | If Motion Vector equal to Previous frame motion vector | ||
957 : | and MinSAD<PrevFrmSAD goto Step 10. | ||
958 : | If SAD<=256 goto Step 10. | ||
959 : | Isibaar | 3 | */ |
960 : | |||
961 : | |||
962 : | // Prepare for main loop | ||
963 : | |||
964 : | *currMV=pmv[0]; /* current best := prediction */ | ||
965 : | if (!(MotionFlags & PMV_HALFPEL16 )) | ||
966 : | { /* This should NOT be necessary! */ | ||
967 : | currMV->x = EVEN(currMV->x); | ||
968 : | currMV->y = EVEN(currMV->y); | ||
969 : | } | ||
970 : | |||
971 : | if (currMV->x > max_dx) | ||
972 : | edgomez | 78 | { |
973 : | currMV->x=max_dx; | ||
974 : | } | ||
975 : | Isibaar | 3 | if (currMV->x < min_dx) |
976 : | edgomez | 78 | { |
977 : | currMV->x=min_dx; | ||
978 : | } | ||
979 : | Isibaar | 3 | if (currMV->y > max_dy) |
980 : | edgomez | 78 | { |
981 : | currMV->y=max_dy; | ||
982 : | } | ||
983 : | Isibaar | 3 | if (currMV->y < min_dy) |
984 : | edgomez | 78 | { |
985 : | currMV->y=min_dy; | ||
986 : | } | ||
987 : | Isibaar | 3 | |
988 : | iMinSAD = sad16( cur, | ||
989 : | edgomez | 78 | get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, iEdgedWidth), |
990 : | iEdgedWidth, MV_MAX_ERROR); | ||
991 : | Isibaar | 3 | iMinSAD += calc_delta_16(currMV->x-pmv[0].x, currMV->y-pmv[0].y, (uint8_t)iFcode) * iQuant; |
992 : | |||
993 : | if ( (iMinSAD < 256 ) || ( (MVequal(*currMV,pMB->mvs[0])) && (iMinSAD < pMB->sad16) ) ) | ||
994 : | edgomez | 78 | { |
995 : | Isibaar | 3 | |
996 : | edgomez | 78 | if (MotionFlags & PMV_QUICKSTOP16) |
997 : | chl | 96 | goto PMVfast16_Terminate_without_Refine; |
998 : | if (MotionFlags & PMV_EARLYSTOP16) | ||
999 : | goto PMVfast16_Terminate_with_Refine; | ||
1000 : | edgomez | 78 | } |
1001 : | Isibaar | 3 | |
1002 : | /* | ||
1003 : | edgomez | 78 | Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block. |
1004 : | Also calculate (0,0) but do not subtract offset. | ||
1005 : | Let MinSAD be the smallest SAD up to this point. | ||
1006 : | If MV is (0,0) subtract offset. ******** WHAT'S THIS 'OFFSET' ??? *********** | ||
1007 : | Isibaar | 3 | */ |
1008 : | |||
1009 : | // (0,0) is always possible | ||
1010 : | |||
1011 : | CHECK_MV16_ZERO; | ||
1012 : | |||
1013 : | // previous frame MV is always possible | ||
1014 : | CHECK_MV16_CANDIDATE(pMB->mvs[0].x,pMB->mvs[0].y); | ||
1015 : | |||
1016 : | // left neighbour, if allowed | ||
1017 : | if (x != 0) | ||
1018 : | { | ||
1019 : | if (!(MotionFlags & PMV_HALFPEL16 )) | ||
1020 : | { pmv[1].x = EVEN(pmv[1].x); | ||
1021 : | edgomez | 78 | pmv[1].y = EVEN(pmv[1].y); |
1022 : | Isibaar | 3 | } |
1023 : | CHECK_MV16_CANDIDATE(pmv[1].x,pmv[1].y); | ||
1024 : | } | ||
1025 : | |||
1026 : | // top neighbour, if allowed | ||
1027 : | if (y != 0) | ||
1028 : | { | ||
1029 : | if (!(MotionFlags & PMV_HALFPEL16 )) | ||
1030 : | { pmv[2].x = EVEN(pmv[2].x); | ||
1031 : | edgomez | 78 | pmv[2].y = EVEN(pmv[2].y); |
1032 : | Isibaar | 3 | } |
1033 : | CHECK_MV16_CANDIDATE(pmv[2].x,pmv[2].y); | ||
1034 : | |||
1035 : | // top right neighbour, if allowed | ||
1036 : | if (x != (iWcount-1)) | ||
1037 : | { | ||
1038 : | if (!(MotionFlags & PMV_HALFPEL16 )) | ||
1039 : | { pmv[3].x = EVEN(pmv[3].x); | ||
1040 : | edgomez | 78 | pmv[3].y = EVEN(pmv[3].y); |
1041 : | Isibaar | 3 | } |
1042 : | CHECK_MV16_CANDIDATE(pmv[3].x,pmv[3].y); | ||
1043 : | } | ||
1044 : | } | ||
1045 : | |||
1046 : | /* Step 6: If MinSAD <= thresa goto Step 10. | ||
1047 : | If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10. | ||
1048 : | */ | ||
1049 : | |||
1050 : | if ( (iMinSAD <= threshA) || ( MVequal(*currMV,pMB->mvs[0]) && (iMinSAD < pMB->sad16) ) ) | ||
1051 : | edgomez | 78 | { |
1052 : | if (MotionFlags & PMV_QUICKSTOP16) | ||
1053 : | chl | 96 | goto PMVfast16_Terminate_without_Refine; |
1054 : | if (MotionFlags & PMV_EARLYSTOP16) | ||
1055 : | goto PMVfast16_Terminate_with_Refine; | ||
1056 : | edgomez | 78 | } |
1057 : | Isibaar | 3 | |
1058 : | |||
1059 : | /************ (Diamond Search) **************/ | ||
1060 : | /* | ||
1061 : | edgomez | 78 | Step 7: Perform Diamond search, with either the small or large diamond. |
1062 : | If Found=2 only examine one Diamond pattern, and afterwards goto step 10 | ||
1063 : | Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond. | ||
1064 : | If center then goto step 10. | ||
1065 : | Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center. | ||
1066 : | Refine by using small diamond and goto step 10. | ||
1067 : | Isibaar | 3 | */ |
1068 : | |||
1069 : | backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */ | ||
1070 : | |||
1071 : | /* default: use best prediction as starting point for one call of PMVfast_MainSearch */ | ||
1072 : | chl | 96 | iSAD = Diamond16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur, |
1073 : | edgomez | 78 | x, y, |
1074 : | currMV->x, currMV->y, iMinSAD, &newMV, | ||
1075 : | pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); | ||
1076 : | Isibaar | 3 | |
1077 : | if (iSAD < iMinSAD) | ||
1078 : | { | ||
1079 : | *currMV = newMV; | ||
1080 : | iMinSAD = iSAD; | ||
1081 : | } | ||
1082 : | |||
1083 : | if (MotionFlags & PMV_EXTSEARCH16) | ||
1084 : | { | ||
1085 : | /* extended: search (up to) two more times: orignal prediction and (0,0) */ | ||
1086 : | |||
1087 : | if (!(MVequal(pmv[0],backupMV)) ) | ||
1088 : | chl | 96 | { iSAD = Diamond16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur, |
1089 : | edgomez | 78 | x, y, |
1090 : | pmv[0].x, pmv[0].y, iMinSAD, &newMV, | ||
1091 : | pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); | ||
1092 : | Isibaar | 3 | |
1093 : | edgomez | 78 | if (iSAD < iMinSAD) |
1094 : | { | ||
1095 : | *currMV = newMV; | ||
1096 : | iMinSAD = iSAD; | ||
1097 : | Isibaar | 3 | } |
1098 : | edgomez | 78 | } |
1099 : | Isibaar | 3 | |
1100 : | if ( (!(MVzero(pmv[0]))) && (!(MVzero(backupMV))) ) | ||
1101 : | chl | 96 | { iSAD = Diamond16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur, |
1102 : | edgomez | 78 | x, y, |
1103 : | 0, 0, iMinSAD, &newMV, | ||
1104 : | pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); | ||
1105 : | Isibaar | 3 | |
1106 : | edgomez | 78 | if (iSAD < iMinSAD) |
1107 : | { | ||
1108 : | *currMV = newMV; | ||
1109 : | iMinSAD = iSAD; | ||
1110 : | Isibaar | 3 | } |
1111 : | edgomez | 78 | } |
1112 : | Isibaar | 3 | } |
1113 : | |||
1114 : | /* | ||
1115 : | edgomez | 78 | Step 10: The motion vector is chosen according to the block corresponding to MinSAD. |
1116 : | Isibaar | 3 | */ |
1117 : | |||
1118 : | chl | 96 | PMVfast16_Terminate_with_Refine: |
1119 : | Isibaar | 3 | if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step |
1120 : | chl | 96 | iMinSAD = Halfpel16_Refine( pRef, pRefH, pRefV, pRefHV, cur, |
1121 : | x, y, | ||
1122 : | currMV, iMinSAD, | ||
1123 : | pmv, min_dx, max_dx, min_dy, max_dy, iFcode, iQuant, iEdgedWidth); | ||
1124 : | Isibaar | 3 | |
1125 : | chl | 96 | PMVfast16_Terminate_without_Refine: |
1126 : | Isibaar | 3 | currPMV->x = currMV->x - pmv[0].x; |
1127 : | currPMV->y = currMV->y - pmv[0].y; | ||
1128 : | return iMinSAD; | ||
1129 : | } | ||
1130 : | |||
1131 : | |||
1132 : | |||
1133 : | |||
1134 : | |||
1135 : | |||
1136 : | chl | 96 | int32_t Diamond8_MainSearch( |
1137 : | edgomez | 78 | const uint8_t * const pRef, |
1138 : | const uint8_t * const pRefH, | ||
1139 : | const uint8_t * const pRefV, | ||
1140 : | const uint8_t * const pRefHV, | ||
1141 : | const uint8_t * const cur, | ||
1142 : | const int x, const int y, | ||
1143 : | int32_t startx, int32_t starty, | ||
1144 : | int32_t iMinSAD, | ||
1145 : | VECTOR * const currMV, | ||
1146 : | const VECTOR * const pmv, | ||
1147 : | const int32_t min_dx, const int32_t max_dx, | ||
1148 : | const int32_t min_dy, const int32_t max_dy, | ||
1149 : | const int32_t iEdgedWidth, | ||
1150 : | const int32_t iDiamondSize, | ||
1151 : | const int32_t iFcode, | ||
1152 : | const int32_t iQuant, | ||
1153 : | int iFound) | ||
1154 : | Isibaar | 3 | { |
1155 : | /* Do a diamond search around given starting point, return SAD of best */ | ||
1156 : | |||
1157 : | int32_t iDirection=0; | ||
1158 : | int32_t iSAD; | ||
1159 : | VECTOR backupMV; | ||
1160 : | backupMV.x = startx; | ||
1161 : | backupMV.y = starty; | ||
1162 : | |||
1163 : | /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */ | ||
1164 : | |||
1165 : | CHECK_MV8_CANDIDATE_DIR(backupMV.x-iDiamondSize,backupMV.y,1); | ||
1166 : | CHECK_MV8_CANDIDATE_DIR(backupMV.x+iDiamondSize,backupMV.y,2); | ||
1167 : | CHECK_MV8_CANDIDATE_DIR(backupMV.x,backupMV.y-iDiamondSize,3); | ||
1168 : | CHECK_MV8_CANDIDATE_DIR(backupMV.x,backupMV.y+iDiamondSize,4); | ||
1169 : | |||
1170 : | if (iDirection) | ||
1171 : | while (!iFound) | ||
1172 : | { | ||
1173 : | iFound = 1; | ||
1174 : | backupMV=*currMV; // since iDirection!=0, this is well defined! | ||
1175 : | |||
1176 : | if ( iDirection != 2) | ||
1177 : | CHECK_MV8_CANDIDATE_FOUND(backupMV.x-iDiamondSize,backupMV.y,1); | ||
1178 : | if ( iDirection != 1) | ||
1179 : | CHECK_MV8_CANDIDATE_FOUND(backupMV.x+iDiamondSize,backupMV.y,2); | ||
1180 : | if ( iDirection != 4) | ||
1181 : | CHECK_MV8_CANDIDATE_FOUND(backupMV.x,backupMV.y-iDiamondSize,3); | ||
1182 : | if ( iDirection != 3) | ||
1183 : | CHECK_MV8_CANDIDATE_FOUND(backupMV.x,backupMV.y+iDiamondSize,4); | ||
1184 : | } | ||
1185 : | else | ||
1186 : | edgomez | 78 | { |
1187 : | currMV->x = startx; | ||
1188 : | currMV->y = starty; | ||
1189 : | } | ||
1190 : | Isibaar | 3 | return iMinSAD; |
1191 : | } | ||
1192 : | |||
1193 : | chl | 96 | int32_t Halfpel8_Refine( |
1194 : | edgomez | 78 | const uint8_t * const pRef, |
1195 : | const uint8_t * const pRefH, | ||
1196 : | const uint8_t * const pRefV, | ||
1197 : | const uint8_t * const pRefHV, | ||
1198 : | const uint8_t * const cur, | ||
1199 : | const int x, const int y, | ||
1200 : | VECTOR * const currMV, | ||
1201 : | int32_t iMinSAD, | ||
1202 : | const VECTOR * const pmv, | ||
1203 : | const int32_t min_dx, const int32_t max_dx, | ||
1204 : | const int32_t min_dy, const int32_t max_dy, | ||
1205 : | const int32_t iFcode, | ||
1206 : | const int32_t iQuant, | ||
1207 : | const int32_t iEdgedWidth) | ||
1208 : | Isibaar | 3 | { |
1209 : | /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */ | ||
1210 : | |||
1211 : | int32_t iSAD; | ||
1212 : | VECTOR backupMV = *currMV; | ||
1213 : | |||
1214 : | CHECK_MV8_CANDIDATE(backupMV.x-1,backupMV.y-1); | ||
1215 : | CHECK_MV8_CANDIDATE(backupMV.x ,backupMV.y-1); | ||
1216 : | CHECK_MV8_CANDIDATE(backupMV.x+1,backupMV.y-1); | ||
1217 : | CHECK_MV8_CANDIDATE(backupMV.x-1,backupMV.y); | ||
1218 : | CHECK_MV8_CANDIDATE(backupMV.x+1,backupMV.y); | ||
1219 : | CHECK_MV8_CANDIDATE(backupMV.x-1,backupMV.y+1); | ||
1220 : | CHECK_MV8_CANDIDATE(backupMV.x ,backupMV.y+1); | ||
1221 : | CHECK_MV8_CANDIDATE(backupMV.x+1,backupMV.y+1); | ||
1222 : | |||
1223 : | return iMinSAD; | ||
1224 : | } | ||
1225 : | |||
1226 : | |||
1227 : | #define PMV_HALFPEL8 (PMV_HALFPELDIAMOND8|PMV_HALFPELREFINE8) | ||
1228 : | |||
1229 : | int32_t PMVfastSearch8( | ||
1230 : | chl | 96 | const uint8_t * const pRef, |
1231 : | const uint8_t * const pRefH, | ||
1232 : | const uint8_t * const pRefV, | ||
1233 : | const uint8_t * const pRefHV, | ||
1234 : | const IMAGE * const pCur, | ||
1235 : | const int x, const int y, | ||
1236 : | const int start_x, int start_y, | ||
1237 : | const uint32_t MotionFlags, | ||
1238 : | const MBParam * const pParam, | ||
1239 : | MACROBLOCK * const pMBs, | ||
1240 : | VECTOR * const currMV, | ||
1241 : | VECTOR * const currPMV) | ||
1242 : | Isibaar | 3 | { |
1243 : | const uint32_t iWcount = pParam->mb_width; | ||
1244 : | |||
1245 : | const int32_t iFcode = pParam->fixed_code; | ||
1246 : | const int32_t iQuant = pParam->quant; | ||
1247 : | const int32_t iWidth = pParam->width; | ||
1248 : | const int32_t iHeight = pParam->height; | ||
1249 : | const int32_t iEdgedWidth = pParam->edged_width; | ||
1250 : | |||
1251 : | const uint8_t * cur = pCur->y + x*8 + y*8*iEdgedWidth; | ||
1252 : | |||
1253 : | int32_t iDiamondSize; | ||
1254 : | |||
1255 : | int32_t min_dx; | ||
1256 : | int32_t max_dx; | ||
1257 : | int32_t min_dy; | ||
1258 : | int32_t max_dy; | ||
1259 : | |||
1260 : | VECTOR pmv[4]; | ||
1261 : | int32_t psad[4]; | ||
1262 : | VECTOR newMV; | ||
1263 : | VECTOR backupMV; | ||
1264 : | |||
1265 : | MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount; | ||
1266 : | |||
1267 : | static int32_t threshA,threshB; | ||
1268 : | int32_t iFound,bPredEq; | ||
1269 : | int32_t iMinSAD,iSAD; | ||
1270 : | |||
1271 : | int32_t iSubBlock = ((y&1)<<1) + (x&1); | ||
1272 : | |||
1273 : | /* Get maximum range */ | ||
1274 : | edgomez | 78 | get_range(&min_dx, &max_dx, &min_dy, &max_dy, |
1275 : | x, y, 8, iWidth, iHeight, iFcode); | ||
1276 : | Isibaar | 3 | |
1277 : | /* we work with abs. MVs, not relative to prediction, so range is relative to 0,0 */ | ||
1278 : | |||
1279 : | if (!(MotionFlags & PMV_HALFPELDIAMOND8 )) | ||
1280 : | { min_dx = EVEN(min_dx); | ||
1281 : | edgomez | 78 | max_dx = EVEN(max_dx); |
1282 : | min_dy = EVEN(min_dy); | ||
1283 : | max_dy = EVEN(max_dy); | ||
1284 : | Isibaar | 3 | } /* because we might use IF (dx>max_dx) THEN dx=max_dx; */ |
1285 : | |||
1286 : | |||
1287 : | bPredEq = get_pmvdata(pMBs, (x>>1), (y>>1), iWcount, iSubBlock, pmv, psad); | ||
1288 : | |||
1289 : | if ((x==0) && (y==0) ) | ||
1290 : | { | ||
1291 : | threshA = 512/4; | ||
1292 : | threshB = 1024/4; | ||
1293 : | |||
1294 : | } | ||
1295 : | else | ||
1296 : | { | ||
1297 : | threshA = psad[0]/4; /* good estimate */ | ||
1298 : | threshB = threshA+256/4; | ||
1299 : | if (threshA< 512/4) threshA = 512/4; | ||
1300 : | if (threshA>1024/4) threshA = 1024/4; | ||
1301 : | if (threshB>1792/4) threshB = 1792/4; | ||
1302 : | } | ||
1303 : | |||
1304 : | iFound=0; | ||
1305 : | |||
1306 : | /* Step 2: Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion | ||
1307 : | edgomez | 78 | vector of the median. |
1308 : | If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2 | ||
1309 : | Isibaar | 3 | */ |
1310 : | |||
1311 : | if ((bPredEq) && (MVequal(pmv[0],pMB->mvs[iSubBlock]) ) ) | ||
1312 : | iFound=2; | ||
1313 : | |||
1314 : | /* Step 3: If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search. | ||
1315 : | edgomez | 78 | Otherwise select large Diamond Search. |
1316 : | Isibaar | 3 | */ |
1317 : | |||
1318 : | if ( (pmv[0].x != 0) || (pmv[0].y != 0) || (threshB<1536/4) || (bPredEq) ) | ||
1319 : | iDiamondSize=1; // 1 halfpel! | ||
1320 : | else | ||
1321 : | iDiamondSize=2; // 2 halfpel = 1 full pixel! | ||
1322 : | |||
1323 : | if (!(MotionFlags & PMV_HALFPELDIAMOND8) ) | ||
1324 : | iDiamondSize*=2; | ||
1325 : | |||
1326 : | /* Step 4: Calculate SAD around the Median prediction. | ||
1327 : | edgomez | 78 | MinSAD=SAD |
1328 : | If Motion Vector equal to Previous frame motion vector | ||
1329 : | and MinSAD<PrevFrmSAD goto Step 10. | ||
1330 : | If SAD<=256 goto Step 10. | ||
1331 : | Isibaar | 3 | */ |
1332 : | |||
1333 : | |||
1334 : | // Prepare for main loop | ||
1335 : | |||
1336 : | currMV->x=start_x; /* start with mv16 */ | ||
1337 : | currMV->y=start_y; | ||
1338 : | |||
1339 : | iMinSAD = sad8( cur, | ||
1340 : | edgomez | 78 | get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, iEdgedWidth), |
1341 : | iEdgedWidth); | ||
1342 : | Isibaar | 3 | iMinSAD += calc_delta_8(currMV->x - pmv[0].x, currMV->y - pmv[0].y, (uint8_t)iFcode) * iQuant; |
1343 : | |||
1344 : | if ( (iMinSAD < 256/4 ) || ( (MVequal(*currMV,pMB->mvs[iSubBlock])) && (iMinSAD < pMB->sad8[iSubBlock]) ) ) | ||
1345 : | edgomez | 78 | { |
1346 : | chl | 96 | if (MotionFlags & PMV_QUICKSTOP16) |
1347 : | goto PMVfast8_Terminate_without_Refine; | ||
1348 : | if (MotionFlags & PMV_EARLYSTOP16) | ||
1349 : | goto PMVfast8_Terminate_with_Refine; | ||
1350 : | edgomez | 78 | } |
1351 : | Isibaar | 3 | |
1352 : | chl | 96 | |
1353 : | Isibaar | 3 | /* |
1354 : | edgomez | 78 | Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block. |
1355 : | Also calculate (0,0) but do not subtract offset. | ||
1356 : | Let MinSAD be the smallest SAD up to this point. | ||
1357 : | If MV is (0,0) subtract offset. ******** WHAT'S THIS 'OFFSET' ??? *********** | ||
1358 : | Isibaar | 3 | */ |
1359 : | |||
1360 : | // the prediction might be even better than mv16 | ||
1361 : | CHECK_MV8_CANDIDATE(pmv[0].x,pmv[0].y); | ||
1362 : | |||
1363 : | // (0,0) is always possible | ||
1364 : | CHECK_MV8_ZERO; | ||
1365 : | |||
1366 : | // previous frame MV is always possible | ||
1367 : | CHECK_MV8_CANDIDATE(pMB->mvs[iSubBlock].x,pMB->mvs[iSubBlock].y); | ||
1368 : | |||
1369 : | // left neighbour, if allowed | ||
1370 : | if (psad[1] != MV_MAX_ERROR) | ||
1371 : | { | ||
1372 : | if (!(MotionFlags & PMV_HALFPEL8 )) | ||
1373 : | { pmv[1].x = EVEN(pmv[1].x); | ||
1374 : | edgomez | 78 | pmv[1].y = EVEN(pmv[1].y); |
1375 : | Isibaar | 3 | } |
1376 : | CHECK_MV8_CANDIDATE(pmv[1].x,pmv[1].y); | ||
1377 : | } | ||
1378 : | |||
1379 : | // top neighbour, if allowed | ||
1380 : | if (psad[2] != MV_MAX_ERROR) | ||
1381 : | { | ||
1382 : | if (!(MotionFlags & PMV_HALFPEL8 )) | ||
1383 : | { pmv[2].x = EVEN(pmv[2].x); | ||
1384 : | edgomez | 78 | pmv[2].y = EVEN(pmv[2].y); |
1385 : | Isibaar | 3 | } |
1386 : | CHECK_MV8_CANDIDATE(pmv[2].x,pmv[2].y); | ||
1387 : | |||
1388 : | // top right neighbour, if allowed | ||
1389 : | if (psad[3] != MV_MAX_ERROR) | ||
1390 : | { | ||
1391 : | edgomez | 78 | if (!(MotionFlags & PMV_HALFPEL8 )) |
1392 : | { pmv[3].x = EVEN(pmv[3].x); | ||
1393 : | Isibaar | 3 | pmv[3].y = EVEN(pmv[3].y); |
1394 : | edgomez | 78 | } |
1395 : | Isibaar | 3 | CHECK_MV8_CANDIDATE(pmv[3].x,pmv[3].y); |
1396 : | } | ||
1397 : | } | ||
1398 : | |||
1399 : | /* Step 6: If MinSAD <= thresa goto Step 10. | ||
1400 : | If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10. | ||
1401 : | */ | ||
1402 : | |||
1403 : | if ( (iMinSAD <= threshA) || ( MVequal(*currMV,pMB->mvs[iSubBlock]) && (iMinSAD < pMB->sad8[iSubBlock]) ) ) | ||
1404 : | edgomez | 78 | { |
1405 : | chl | 96 | if (MotionFlags & PMV_QUICKSTOP16) |
1406 : | goto PMVfast8_Terminate_without_Refine; | ||
1407 : | if (MotionFlags & PMV_EARLYSTOP16) | ||
1408 : | goto PMVfast8_Terminate_with_Refine; | ||
1409 : | edgomez | 78 | } |
1410 : | Isibaar | 3 | |
1411 : | /************ (Diamond Search) **************/ | ||
1412 : | /* | ||
1413 : | edgomez | 78 | Step 7: Perform Diamond search, with either the small or large diamond. |
1414 : | If Found=2 only examine one Diamond pattern, and afterwards goto step 10 | ||
1415 : | Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond. | ||
1416 : | If center then goto step 10. | ||
1417 : | Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center. | ||
1418 : | Refine by using small diamond and goto step 10. | ||
1419 : | Isibaar | 3 | */ |
1420 : | |||
1421 : | backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */ | ||
1422 : | |||
1423 : | /* default: use best prediction as starting point for one call of PMVfast_MainSearch */ | ||
1424 : | chl | 96 | iSAD = Diamond8_MainSearch(pRef, pRefH, pRefV, pRefHV, cur, |
1425 : | edgomez | 78 | x, y, |
1426 : | currMV->x, currMV->y, iMinSAD, &newMV, | ||
1427 : | pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); | ||
1428 : | Isibaar | 3 | |
1429 : | if (iSAD < iMinSAD) | ||
1430 : | { | ||
1431 : | *currMV = newMV; | ||
1432 : | iMinSAD = iSAD; | ||
1433 : | } | ||
1434 : | |||
1435 : | if (MotionFlags & PMV_EXTSEARCH8) | ||
1436 : | { | ||
1437 : | /* extended: search (up to) two more times: orignal prediction and (0,0) */ | ||
1438 : | |||
1439 : | if (!(MVequal(pmv[0],backupMV)) ) | ||
1440 : | chl | 96 | { iSAD = Diamond16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur, |
1441 : | edgomez | 78 | x, y, |
1442 : | pmv[0].x, pmv[0].y, iMinSAD, &newMV, | ||
1443 : | pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); | ||
1444 : | Isibaar | 3 | |
1445 : | edgomez | 78 | if (iSAD < iMinSAD) |
1446 : | { | ||
1447 : | *currMV = newMV; | ||
1448 : | iMinSAD = iSAD; | ||
1449 : | Isibaar | 3 | } |
1450 : | edgomez | 78 | } |
1451 : | Isibaar | 3 | |
1452 : | if ( (!(MVzero(pmv[0]))) && (!(MVzero(backupMV))) ) | ||
1453 : | chl | 96 | { iSAD = Diamond16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur, |
1454 : | edgomez | 78 | x, y, |
1455 : | 0, 0, iMinSAD, &newMV, | ||
1456 : | pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); | ||
1457 : | Isibaar | 3 | |
1458 : | edgomez | 78 | if (iSAD < iMinSAD) |
1459 : | { | ||
1460 : | *currMV = newMV; | ||
1461 : | iMinSAD = iSAD; | ||
1462 : | Isibaar | 3 | } |
1463 : | edgomez | 78 | } |
1464 : | Isibaar | 3 | } |
1465 : | |||
1466 : | /* Step 10: The motion vector is chosen according to the block corresponding to MinSAD. | ||
1467 : | edgomez | 78 | By performing an optional local half-pixel search, we can refine this result even further. |
1468 : | Isibaar | 3 | */ |
1469 : | |||
1470 : | chl | 96 | PMVfast8_Terminate_with_Refine: |
1471 : | Isibaar | 3 | if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step |
1472 : | chl | 96 | iMinSAD = Halfpel8_Refine( pRef, pRefH, pRefV, pRefHV, cur, |
1473 : | edgomez | 78 | x, y, |
1474 : | currMV, iMinSAD, | ||
1475 : | pmv, min_dx, max_dx, min_dy, max_dy, iFcode, iQuant, iEdgedWidth); | ||
1476 : | Isibaar | 3 | |
1477 : | |||
1478 : | chl | 96 | PMVfast8_Terminate_without_Refine: |
1479 : | Isibaar | 3 | currPMV->x = currMV->x - pmv[0].x; |
1480 : | currPMV->y = currMV->y - pmv[0].y; | ||
1481 : | |||
1482 : | return iMinSAD; | ||
1483 : | } | ||
1484 : | chl | 96 | |
1485 : | int32_t EPZSSearch16( | ||
1486 : | const uint8_t * const pRef, | ||
1487 : | const uint8_t * const pRefH, | ||
1488 : | const uint8_t * const pRefV, | ||
1489 : | const uint8_t * const pRefHV, | ||
1490 : | const IMAGE * const pCur, | ||
1491 : | const int x, const int y, | ||
1492 : | const uint32_t MotionFlags, | ||
1493 : | const MBParam * const pParam, | ||
1494 : | MACROBLOCK * const pMBs, | ||
1495 : | VECTOR * const currMV, | ||
1496 : | VECTOR * const currPMV) | ||
1497 : | { | ||
1498 : | const uint32_t iWcount = pParam->mb_width; | ||
1499 : | const uint32_t iHcount = pParam->mb_height; | ||
1500 : | const int32_t iFcode = pParam->fixed_code; | ||
1501 : | const int32_t iQuant = pParam->quant; | ||
1502 : | |||
1503 : | const int32_t iWidth = pParam->width; | ||
1504 : | const int32_t iHeight = pParam->height; | ||
1505 : | const int32_t iEdgedWidth = pParam->edged_width; | ||
1506 : | |||
1507 : | const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth; | ||
1508 : | |||
1509 : | int32_t min_dx; | ||
1510 : | int32_t max_dx; | ||
1511 : | int32_t min_dy; | ||
1512 : | int32_t max_dy; | ||
1513 : | |||
1514 : | VECTOR newMV; | ||
1515 : | VECTOR backupMV; | ||
1516 : | |||
1517 : | VECTOR pmv[4]; | ||
1518 : | int32_t psad[8]; | ||
1519 : | |||
1520 : | static MACROBLOCK * oldMBs = NULL; | ||
1521 : | MACROBLOCK * const pMB = pMBs + x + y * iWcount; | ||
1522 : | MACROBLOCK * oldMB = NULL; | ||
1523 : | |||
1524 : | static int32_t thresh2; | ||
1525 : | int32_t bPredEq; | ||
1526 : | int32_t iMinSAD,iSAD=9999; | ||
1527 : | |||
1528 : | MainSearch16FuncPtr EPZSMainSearchPtr; | ||
1529 : | |||
1530 : | if (oldMBs == NULL) | ||
1531 : | { oldMBs = (MACROBLOCK*) calloc(1,iWcount*iHcount*sizeof(MACROBLOCK)); | ||
1532 : | fprintf(stderr,"allocated %d bytes for oldMBs\n",iWcount*iHcount*sizeof(MACROBLOCK)); | ||
1533 : | } | ||
1534 : | oldMB = oldMBs + x + y * iWcount; | ||
1535 : | |||
1536 : | /* Get maximum range */ | ||
1537 : | get_range(&min_dx, &max_dx, &min_dy, &max_dy, | ||
1538 : | x, y, 16, iWidth, iHeight, iFcode); | ||
1539 : | |||
1540 : | /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */ | ||
1541 : | |||
1542 : | if (!(MotionFlags & PMV_HALFPEL16 )) | ||
1543 : | { min_dx = EVEN(min_dx); | ||
1544 : | max_dx = EVEN(max_dx); | ||
1545 : | min_dy = EVEN(min_dy); | ||
1546 : | max_dy = EVEN(max_dy); | ||
1547 : | } /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */ | ||
1548 : | |||
1549 : | bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad); | ||
1550 : | |||
1551 : | /* Step 4: Calculate SAD around the Median prediction. | ||
1552 : | MinSAD=SAD | ||
1553 : | If Motion Vector equal to Previous frame motion vector | ||
1554 : | and MinSAD<PrevFrmSAD goto Step 10. | ||
1555 : | If SAD<=256 goto Step 10. | ||
1556 : | */ | ||
1557 : | |||
1558 : | // Prepare for main loop | ||
1559 : | |||
1560 : | *currMV=pmv[0]; /* current best := median prediction */ | ||
1561 : | if (!(MotionFlags & PMV_HALFPEL16)) | ||
1562 : | { | ||
1563 : | currMV->x = EVEN(currMV->x); | ||
1564 : | currMV->y = EVEN(currMV->y); | ||
1565 : | } | ||
1566 : | |||
1567 : | if (currMV->x > max_dx) | ||
1568 : | currMV->x=max_dx; | ||
1569 : | if (currMV->x < min_dx) | ||
1570 : | currMV->x=min_dx; | ||
1571 : | if (currMV->y > max_dy) | ||
1572 : | currMV->y=max_dy; | ||
1573 : | if (currMV->y < min_dy) | ||
1574 : | currMV->y=min_dy; | ||
1575 : | |||
1576 : | /***************** This is predictor SET A: only median prediction ******************/ | ||
1577 : | |||
1578 : | iMinSAD = sad16( cur, | ||
1579 : | get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, iEdgedWidth), | ||
1580 : | iEdgedWidth, MV_MAX_ERROR); | ||
1581 : | iMinSAD += calc_delta_16(currMV->x-pmv[0].x, currMV->y-pmv[0].y, (uint8_t)iFcode) * iQuant; | ||
1582 : | |||
1583 : | // thresh1 is fixed to 256 | ||
1584 : | if ( (iMinSAD < 256 ) || ( (MVequal(*currMV,pMB->mvs[0])) && (iMinSAD < pMB->sad16) ) ) | ||
1585 : | { | ||
1586 : | if (MotionFlags & PMV_QUICKSTOP16) | ||
1587 : | goto EPZS16_Terminate_without_Refine; | ||
1588 : | if (MotionFlags & PMV_EARLYSTOP16) | ||
1589 : | goto EPZS16_Terminate_with_Refine; | ||
1590 : | } | ||
1591 : | |||
1592 : | /************** This is predictor SET B: (0,0), prev.frame MV, neighbours **************/ | ||
1593 : | |||
1594 : | // previous frame MV | ||
1595 : | CHECK_MV16_CANDIDATE(pMB->mvs[0].x,pMB->mvs[0].y); | ||
1596 : | |||
1597 : | // set threshhold based on Min of Prediction and SAD of collocated block | ||
1598 : | // CHECK_MV16 always uses iSAD for the SAD of last vector to check, so now iSAD is what we want | ||
1599 : | |||
1600 : | if ((x==0) && (y==0) ) | ||
1601 : | { | ||
1602 : | thresh2 = 512; | ||
1603 : | } | ||
1604 : | else | ||
1605 : | { | ||
1606 : | /* T_k = 1.2 * MIN(SAD_top,SAD_left,SAD_topleft,SAD_coll) +128; [Tourapis, 2002] */ | ||
1607 : | |||
1608 : | thresh2 = MIN(psad[0],iSAD)*6/5 + 128; | ||
1609 : | } | ||
1610 : | |||
1611 : | // MV=(0,0) is often a good choice | ||
1612 : | |||
1613 : | CHECK_MV16_ZERO; | ||
1614 : | |||
1615 : | |||
1616 : | // left neighbour, if allowed | ||
1617 : | if (x != 0) | ||
1618 : | { | ||
1619 : | if (!(MotionFlags & PMV_HALFPEL16 )) | ||
1620 : | { pmv[1].x = EVEN(pmv[1].x); | ||
1621 : | pmv[1].y = EVEN(pmv[1].y); | ||
1622 : | } | ||
1623 : | CHECK_MV16_CANDIDATE(pmv[1].x,pmv[1].y); | ||
1624 : | } | ||
1625 : | |||
1626 : | // top neighbour, if allowed | ||
1627 : | if (y != 0) | ||
1628 : | { | ||
1629 : | if (!(MotionFlags & PMV_HALFPEL16 )) | ||
1630 : | { pmv[2].x = EVEN(pmv[2].x); | ||
1631 : | pmv[2].y = EVEN(pmv[2].y); | ||
1632 : | } | ||
1633 : | CHECK_MV16_CANDIDATE(pmv[2].x,pmv[2].y); | ||
1634 : | |||
1635 : | // top right neighbour, if allowed | ||
1636 : | if (x != (iWcount-1)) | ||
1637 : | { | ||
1638 : | if (!(MotionFlags & PMV_HALFPEL16 )) | ||
1639 : | { pmv[3].x = EVEN(pmv[3].x); | ||
1640 : | pmv[3].y = EVEN(pmv[3].y); | ||
1641 : | } | ||
1642 : | CHECK_MV16_CANDIDATE(pmv[3].x,pmv[3].y); | ||
1643 : | } | ||
1644 : | } | ||
1645 : | |||
1646 : | /* Terminate if MinSAD <= T_2 | ||
1647 : | Terminate if MV[t] == MV[t-1] and MinSAD[t] <= MinSAD[t-1] | ||
1648 : | */ | ||
1649 : | |||
1650 : | if ( (iMinSAD <= thresh2) | ||
1651 : | || ( MVequal(*currMV,pMB->mvs[0]) && (iMinSAD <= pMB->sad16) ) ) | ||
1652 : | { | ||
1653 : | if (MotionFlags & PMV_QUICKSTOP16) | ||
1654 : | goto EPZS16_Terminate_without_Refine; | ||
1655 : | if (MotionFlags & PMV_EARLYSTOP16) | ||
1656 : | goto EPZS16_Terminate_with_Refine; | ||
1657 : | } | ||
1658 : | |||
1659 : | /***** predictor SET C: acceleration MV (new!), neighbours in prev. frame(new!) ****/ | ||
1660 : | |||
1661 : | backupMV = pMB->mvs[0]; // last MV | ||
1662 : | backupMV.x += (pMB->mvs[0].x - oldMB->mvs[0].x ); // acceleration X | ||
1663 : | backupMV.y += (pMB->mvs[0].y - oldMB->mvs[0].y ); // acceleration Y | ||
1664 : | |||
1665 : | CHECK_MV16_CANDIDATE(backupMV.x,backupMV.y); | ||
1666 : | |||
1667 : | // left neighbour | ||
1668 : | if (x != 0) | ||
1669 : | CHECK_MV16_CANDIDATE((oldMB-1)->mvs[0].x,oldMB->mvs[0].y); | ||
1670 : | |||
1671 : | // top neighbour | ||
1672 : | if (y != 0) | ||
1673 : | CHECK_MV16_CANDIDATE((oldMB-iWcount)->mvs[0].x,oldMB->mvs[0].y); | ||
1674 : | |||
1675 : | // right neighbour, if allowed (this value is not written yet, so take it from pMB->mvs | ||
1676 : | |||
1677 : | if (x != iWcount-1) | ||
1678 : | CHECK_MV16_CANDIDATE((pMB+1)->mvs[0].x,oldMB->mvs[0].y); | ||
1679 : | |||
1680 : | // bottom neighbour, dito | ||
1681 : | if (y != iHcount-1) | ||
1682 : | CHECK_MV16_CANDIDATE((pMB+iWcount)->mvs[0].x,oldMB->mvs[0].y); | ||
1683 : | |||
1684 : | /* Terminate if MinSAD <= T_3 (here T_3 = T_2) */ | ||
1685 : | if (iMinSAD <= thresh2) | ||
1686 : | { | ||
1687 : | if (MotionFlags & PMV_QUICKSTOP16) | ||
1688 : | goto EPZS16_Terminate_without_Refine; | ||
1689 : | if (MotionFlags & PMV_EARLYSTOP16) | ||
1690 : | goto EPZS16_Terminate_with_Refine; | ||
1691 : | } | ||
1692 : | |||
1693 : | /************ (if Diamond Search) **************/ | ||
1694 : | |||
1695 : | backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */ | ||
1696 : | |||
1697 : | /* default: use best prediction as starting point for one call of PMVfast_MainSearch */ | ||
1698 : | |||
1699 : | if (MotionFlags & PMV_USESQUARES16) | ||
1700 : | EPZSMainSearchPtr = Square16_MainSearch; | ||
1701 : | else | ||
1702 : | EPZSMainSearchPtr = Diamond16_MainSearch; | ||
1703 : | |||
1704 : | iSAD = (*EPZSMainSearchPtr)(pRef, pRefH, pRefV, pRefHV, cur, | ||
1705 : | x, y, | ||
1706 : | currMV->x, currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, | ||
1707 : | 2, iFcode, iQuant, 0); | ||
1708 : | |||
1709 : | if (iSAD < iMinSAD) | ||
1710 : | { | ||
1711 : | *currMV = newMV; | ||
1712 : | iMinSAD = iSAD; | ||
1713 : | } | ||
1714 : | |||
1715 : | |||
1716 : | if (MotionFlags & PMV_EXTSEARCH16) | ||
1717 : | { | ||
1718 : | /* extended mode: search (up to) two more times: orignal prediction and (0,0) */ | ||
1719 : | |||
1720 : | if (!(MVequal(pmv[0],backupMV)) ) | ||
1721 : | { | ||
1722 : | iSAD = (*EPZSMainSearchPtr)(pRef, pRefH, pRefV, pRefHV, cur, | ||
1723 : | x, y, | ||
1724 : | pmv[0].x, pmv[0].y, iMinSAD, &newMV, | ||
1725 : | pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, 2, iFcode, iQuant, 0); | ||
1726 : | } | ||
1727 : | |||
1728 : | if (iSAD < iMinSAD) | ||
1729 : | { | ||
1730 : | *currMV = newMV; | ||
1731 : | iMinSAD = iSAD; | ||
1732 : | } | ||
1733 : | |||
1734 : | if ( (!(MVzero(pmv[0]))) && (!(MVzero(backupMV))) ) | ||
1735 : | { | ||
1736 : | iSAD = (*EPZSMainSearchPtr)(pRef, pRefH, pRefV, pRefHV, cur, | ||
1737 : | x, y, | ||
1738 : | 0, 0, iMinSAD, &newMV, | ||
1739 : | pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, /*iDiamondSize*/ 2, iFcode, iQuant, 0); | ||
1740 : | |||
1741 : | if (iSAD < iMinSAD) | ||
1742 : | { | ||
1743 : | *currMV = newMV; | ||
1744 : | iMinSAD = iSAD; | ||
1745 : | } | ||
1746 : | } | ||
1747 : | } | ||
1748 : | |||
1749 : | /*************** Choose best MV found **************/ | ||
1750 : | |||
1751 : | EPZS16_Terminate_with_Refine: | ||
1752 : | if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step | ||
1753 : | iMinSAD = Halfpel16_Refine( pRef, pRefH, pRefV, pRefHV, cur, | ||
1754 : | x, y, | ||
1755 : | currMV, iMinSAD, | ||
1756 : | pmv, min_dx, max_dx, min_dy, max_dy, iFcode, iQuant, iEdgedWidth); | ||
1757 : | |||
1758 : | EPZS16_Terminate_without_Refine: | ||
1759 : | |||
1760 : | *oldMB = *pMB; | ||
1761 : | |||
1762 : | currPMV->x = currMV->x - pmv[0].x; | ||
1763 : | currPMV->y = currMV->y - pmv[0].y; | ||
1764 : | return iMinSAD; | ||
1765 : | } | ||
1766 : | |||
1767 : | |||
1768 : | int32_t EPZSSearch8( | ||
1769 : | const uint8_t * const pRef, | ||
1770 : | const uint8_t * const pRefH, | ||
1771 : | const uint8_t * const pRefV, | ||
1772 : | const uint8_t * const pRefHV, | ||
1773 : | const IMAGE * const pCur, | ||
1774 : | const int x, const int y, | ||
1775 : | const int start_x, const int start_y, | ||
1776 : | const uint32_t MotionFlags, | ||
1777 : | const MBParam * const pParam, | ||
1778 : | MACROBLOCK * const pMBs, | ||
1779 : | VECTOR * const currMV, | ||
1780 : | VECTOR * const currPMV) | ||
1781 : | { | ||
1782 : | const uint32_t iWcount = pParam->mb_width; | ||
1783 : | const int32_t iFcode = pParam->fixed_code; | ||
1784 : | const int32_t iQuant = pParam->quant; | ||
1785 : | |||
1786 : | const int32_t iWidth = pParam->width; | ||
1787 : | const int32_t iHeight = pParam->height; | ||
1788 : | const int32_t iEdgedWidth = pParam->edged_width; | ||
1789 : | |||
1790 : | const uint8_t * cur = pCur->y + x*8 + y*8*iEdgedWidth; | ||
1791 : | |||
1792 : | int32_t iDiamondSize=1; | ||
1793 : | |||
1794 : | int32_t min_dx; | ||
1795 : | int32_t max_dx; | ||
1796 : | int32_t min_dy; | ||
1797 : | int32_t max_dy; | ||
1798 : | |||
1799 : | VECTOR newMV; | ||
1800 : | VECTOR backupMV; | ||
1801 : | |||
1802 : | VECTOR pmv[4]; | ||
1803 : | int32_t psad[8]; | ||
1804 : | |||
1805 : | const int32_t iSubBlock = ((y&1)<<1) + (x&1); | ||
1806 : | |||
1807 : | MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount; | ||
1808 : | |||
1809 : | int32_t bPredEq; | ||
1810 : | int32_t iMinSAD,iSAD=9999; | ||
1811 : | |||
1812 : | MainSearch8FuncPtr EPZSMainSearchPtr; | ||
1813 : | |||
1814 : | /* Get maximum range */ | ||
1815 : | get_range(&min_dx, &max_dx, &min_dy, &max_dy, | ||
1816 : | x, y, 8, iWidth, iHeight, iFcode); | ||
1817 : | |||
1818 : | /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */ | ||
1819 : | |||
1820 : | if (!(MotionFlags & PMV_HALFPEL8 )) | ||
1821 : | { min_dx = EVEN(min_dx); | ||
1822 : | max_dx = EVEN(max_dx); | ||
1823 : | min_dy = EVEN(min_dy); | ||
1824 : | max_dy = EVEN(max_dy); | ||
1825 : | } /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */ | ||
1826 : | |||
1827 : | bPredEq = get_pmvdata(pMBs, x>>1, y>>1, iWcount, iSubBlock, pmv, psad); | ||
1828 : | |||
1829 : | |||
1830 : | /* Step 4: Calculate SAD around the Median prediction. | ||
1831 : | MinSAD=SAD | ||
1832 : | If Motion Vector equal to Previous frame motion vector | ||
1833 : | and MinSAD<PrevFrmSAD goto Step 10. | ||
1834 : | If SAD<=256 goto Step 10. | ||
1835 : | */ | ||
1836 : | |||
1837 : | // Prepare for main loop | ||
1838 : | |||
1839 : | |||
1840 : | if (!(MotionFlags & PMV_HALFPEL8)) | ||
1841 : | { | ||
1842 : | currMV->x = EVEN(currMV->x); | ||
1843 : | currMV->y = EVEN(currMV->y); | ||
1844 : | } | ||
1845 : | |||
1846 : | if (currMV->x > max_dx) | ||
1847 : | currMV->x=max_dx; | ||
1848 : | if (currMV->x < min_dx) | ||
1849 : | currMV->x=min_dx; | ||
1850 : | if (currMV->y > max_dy) | ||
1851 : | currMV->y=max_dy; | ||
1852 : | if (currMV->y < min_dy) | ||
1853 : | currMV->y=min_dy; | ||
1854 : | |||
1855 : | /***************** This is predictor SET A: only median prediction ******************/ | ||
1856 : | |||
1857 : | |||
1858 : | iMinSAD = sad8( cur, | ||
1859 : | get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, iEdgedWidth), | ||
1860 : | iEdgedWidth); | ||
1861 : | iMinSAD += calc_delta_8(currMV->x-pmv[0].x, currMV->y-pmv[0].y, (uint8_t)iFcode) * iQuant; | ||
1862 : | |||
1863 : | |||
1864 : | // thresh1 is fixed to 256 | ||
1865 : | if (iMinSAD < 256/4 ) | ||
1866 : | { | ||
1867 : | if (MotionFlags & PMV_QUICKSTOP8) | ||
1868 : | goto EPZS8_Terminate_without_Refine; | ||
1869 : | if (MotionFlags & PMV_EARLYSTOP8) | ||
1870 : | goto EPZS8_Terminate_with_Refine; | ||
1871 : | } | ||
1872 : | |||
1873 : | /************** This is predictor SET B: (0,0), prev.frame MV, neighbours **************/ | ||
1874 : | |||
1875 : | // previous frame MV | ||
1876 : | CHECK_MV8_CANDIDATE(pMB->mvs[0].x,pMB->mvs[0].y); | ||
1877 : | |||
1878 : | // MV=(0,0) is often a good choice | ||
1879 : | |||
1880 : | CHECK_MV8_ZERO; | ||
1881 : | |||
1882 : | /* Terminate if MinSAD <= T_2 | ||
1883 : | Terminate if MV[t] == MV[t-1] and MinSAD[t] <= MinSAD[t-1] | ||
1884 : | */ | ||
1885 : | |||
1886 : | if (iMinSAD < 512/4) /* T_2 == 512/4 hardcoded */ | ||
1887 : | { | ||
1888 : | if (MotionFlags & PMV_QUICKSTOP8) | ||
1889 : | goto EPZS8_Terminate_without_Refine; | ||
1890 : | if (MotionFlags & PMV_EARLYSTOP8) | ||
1891 : | goto EPZS8_Terminate_with_Refine; | ||
1892 : | } | ||
1893 : | |||
1894 : | /************ (if Diamond Search) **************/ | ||
1895 : | |||
1896 : | backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */ | ||
1897 : | |||
1898 : | if (!(MotionFlags & PMV_HALFPELDIAMOND8)) | ||
1899 : | iDiamondSize *= 2; | ||
1900 : | |||
1901 : | /* default: use best prediction as starting point for one call of PMVfast_MainSearch */ | ||
1902 : | |||
1903 : | // if (MotionFlags & PMV_USESQUARES8) | ||
1904 : | // EPZSMainSearchPtr = Square8_MainSearch; | ||
1905 : | // else | ||
1906 : | EPZSMainSearchPtr = Diamond8_MainSearch; | ||
1907 : | |||
1908 : | iSAD = (*EPZSMainSearchPtr)(pRef, pRefH, pRefV, pRefHV, cur, | ||
1909 : | x, y, | ||
1910 : | currMV->x, currMV->y, iMinSAD, &newMV, | ||
1911 : | pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, | ||
1912 : | iDiamondSize, iFcode, iQuant, 00); | ||
1913 : | |||
1914 : | |||
1915 : | if (iSAD < iMinSAD) | ||
1916 : | { | ||
1917 : | *currMV = newMV; | ||
1918 : | iMinSAD = iSAD; | ||
1919 : | } | ||
1920 : | |||
1921 : | if (MotionFlags & PMV_EXTSEARCH8) | ||
1922 : | { | ||
1923 : | /* extended mode: search (up to) two more times: orignal prediction and (0,0) */ | ||
1924 : | |||
1925 : | if (!(MVequal(pmv[0],backupMV)) ) | ||
1926 : | { | ||
1927 : | iSAD = (*EPZSMainSearchPtr)(pRef, pRefH, pRefV, pRefHV, cur, | ||
1928 : | x, y, | ||
1929 : | pmv[0].x, pmv[0].y, iMinSAD, &newMV, | ||
1930 : | pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, 0); | ||
1931 : | |||
1932 : | if (iSAD < iMinSAD) | ||
1933 : | { | ||
1934 : | *currMV = newMV; | ||
1935 : | iMinSAD = iSAD; | ||
1936 : | } | ||
1937 : | } | ||
1938 : | |||
1939 : | if ( (!(MVzero(pmv[0]))) && (!(MVzero(backupMV))) ) | ||
1940 : | { | ||
1941 : | iSAD = (*EPZSMainSearchPtr)(pRef, pRefH, pRefV, pRefHV, cur, | ||
1942 : | x, y, | ||
1943 : | 0, 0, iMinSAD, &newMV, | ||
1944 : | pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, 0); | ||
1945 : | |||
1946 : | if (iSAD < iMinSAD) | ||
1947 : | { | ||
1948 : | *currMV = newMV; | ||
1949 : | iMinSAD = iSAD; | ||
1950 : | } | ||
1951 : | } | ||
1952 : | } | ||
1953 : | |||
1954 : | /*************** Choose best MV found **************/ | ||
1955 : | |||
1956 : | EPZS8_Terminate_with_Refine: | ||
1957 : | if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step | ||
1958 : | iMinSAD = Halfpel8_Refine( pRef, pRefH, pRefV, pRefHV, cur, | ||
1959 : | x, y, | ||
1960 : | currMV, iMinSAD, | ||
1961 : | pmv, min_dx, max_dx, min_dy, max_dy, iFcode, iQuant, iEdgedWidth); | ||
1962 : | |||
1963 : | EPZS8_Terminate_without_Refine: | ||
1964 : | |||
1965 : | currPMV->x = currMV->x - pmv[0].x; | ||
1966 : | currPMV->y = currMV->y - pmv[0].y; | ||
1967 : | return iMinSAD; | ||
1968 : | } | ||
1969 : |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |