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