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