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