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