1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* motion estimation |
* - Motion Estimation module - |
5 |
|
* |
6 |
|
* Copyright(C) 2002 Christoph Lampert <gruel@web.de> |
7 |
|
* Copyright(C) 2002 Michael Militzer <michael@xvid.org> |
8 |
|
* Copyright(C) 2002 Edouard Gomez <ed.gomez@wanadoo.fr> |
9 |
|
* Copyright(C) 2002 chenm001 <chenm001@163.com> |
10 |
* |
* |
11 |
* This program is an implementation of a part of one or more MPEG-4 |
* This program is an implementation of a part of one or more MPEG-4 |
12 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
29 |
* |
* |
30 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
31 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
32 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
33 |
* |
* |
34 |
*************************************************************************/ |
*************************************************************************/ |
35 |
|
|
|
/************************************************************************** |
|
|
* |
|
|
* Modifications: |
|
|
* |
|
|
* 01.05.2002 updated MotionEstimationBVOP |
|
|
* 25.04.2002 partial prevMB conversion |
|
|
* 22.04.2002 remove some compile warning by chenm001 <chenm001@163.com> |
|
|
* 14.04.2002 added MotionEstimationBVOP() |
|
|
* 02.04.2002 add EPZS(^2) as ME algorithm, use PMV_USESQUARES to choose between |
|
|
* EPZS and EPZS^2 |
|
|
* 08.02.2002 split up PMVfast into three routines: PMVFast, PMVFast_MainLoop |
|
|
* PMVFast_Refine to support multiple searches with different start points |
|
|
* 07.01.2002 uv-block-based interpolation |
|
|
* 06.01.2002 INTER/INTRA-decision is now done before any SEARCH8 (speedup) |
|
|
* changed INTER_BIAS to 150 (as suggested by suxen_drol) |
|
|
* removed halfpel refinement step in PMVfastSearch8 + quality=5 |
|
|
* added new quality mode = 6 which performs halfpel refinement |
|
|
* filesize difference between quality 5 and 6 is smaller than 1% |
|
|
* (Isibaar) |
|
|
* 31.12.2001 PMVfastSearch16 and PMVfastSearch8 (gruel) |
|
|
* 30.12.2001 get_range/MotionSearchX simplified; blue/green bug fix |
|
|
* 22.12.2001 commented best_point==99 check |
|
|
* 19.12.2001 modified get_range (purple bug fix) |
|
|
* 15.12.2001 moved pmv displacement from mbprediction |
|
|
* 02.12.2001 motion estimation/compensation split (Isibaar) |
|
|
* 16.11.2001 rewrote/tweaked search algorithms; pross@cs.rmit.edu.au |
|
|
* 10.11.2001 support for sad16/sad8 functions |
|
|
* 28.08.2001 reactivated MODE_INTER4V for EXT_MODE |
|
|
* 24.08.2001 removed MODE_INTER4V_Q, disabled MODE_INTER4V for EXT_MODE |
|
|
* 22.08.2001 added MODE_INTER4V_Q |
|
|
* 20.08.2001 added pragma to get rid of internal compiler error with VC6 |
|
|
* idea by Cyril. Thanks. |
|
|
* |
|
|
* Michael Militzer <isibaar@videocoding.de> |
|
|
* |
|
|
**************************************************************************/ |
|
|
|
|
36 |
#include <assert.h> |
#include <assert.h> |
37 |
#include <stdio.h> |
#include <stdio.h> |
38 |
#include <stdlib.h> |
#include <stdlib.h> |
139 |
const IMAGE *const pCurrent = ¤t->image; |
const IMAGE *const pCurrent = ¤t->image; |
140 |
const IMAGE *const pRef = &reference->image; |
const IMAGE *const pRef = &reference->image; |
141 |
|
|
142 |
const VECTOR zeroMV = { 0, 0 }; |
static const VECTOR zeroMV = { 0, 0 }; |
143 |
|
VECTOR predMV; |
144 |
|
|
145 |
int32_t x, y; |
int32_t x, y; |
146 |
int32_t iIntra = 0; |
int32_t iIntra = 0; |
154 |
|
|
155 |
MACROBLOCK *const pMB = &pMBs[x + y * iWcount]; |
MACROBLOCK *const pMB = &pMBs[x + y * iWcount]; |
156 |
|
|
157 |
|
if (pMB->mode == MODE_NOT_CODED) |
158 |
|
continue; |
159 |
|
|
160 |
|
predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
161 |
|
|
162 |
pMB->sad16 = |
pMB->sad16 = |
163 |
SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
164 |
y, current->motion_flags, current->quant, |
x, y, predMV.x, predMV.y, predMV.x, predMV.y, |
165 |
|
current->motion_flags, current->quant, |
166 |
current->fcode, pParam, pMBs, prevMBs, &pMB->mv16, |
current->fcode, pParam, pMBs, prevMBs, &pMB->mv16, |
167 |
&pMB->pmvs[0]); |
&pMB->pmvs[0]); |
168 |
|
|
194 |
pMB->dquant == NO_CHANGE)) { |
pMB->dquant == NO_CHANGE)) { |
195 |
int32_t sad8 = IMV16X16 * current->quant; |
int32_t sad8 = IMV16X16 * current->quant; |
196 |
|
|
197 |
if (sad8 < pMB->sad16) |
if (sad8 < pMB->sad16) { |
|
|
|
198 |
sad8 += pMB->sad8[0] = |
sad8 += pMB->sad8[0] = |
199 |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
200 |
pCurrent, 2 * x, 2 * y, pMB->mv16.x, |
pCurrent, 2 * x, 2 * y, |
201 |
pMB->mv16.y, current->motion_flags, |
pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y, |
202 |
|
current->motion_flags, |
203 |
current->quant, current->fcode, pParam, |
current->quant, current->fcode, pParam, |
204 |
pMBs, prevMBs, &pMB->mvs[0], |
pMBs, prevMBs, &pMB->mvs[0], |
205 |
&pMB->pmvs[0]); |
&pMB->pmvs[0]); |
206 |
|
} |
207 |
|
if (sad8 < pMB->sad16) { |
208 |
|
|
209 |
if (sad8 < pMB->sad16) |
predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 1); |
210 |
sad8 += pMB->sad8[1] = |
sad8 += pMB->sad8[1] = |
211 |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
212 |
pCurrent, 2 * x + 1, 2 * y, pMB->mv16.x, |
pCurrent, 2 * x + 1, 2 * y, |
213 |
pMB->mv16.y, current->motion_flags, |
pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y, |
214 |
|
current->motion_flags, |
215 |
current->quant, current->fcode, pParam, |
current->quant, current->fcode, pParam, |
216 |
pMBs, prevMBs, &pMB->mvs[1], |
pMBs, prevMBs, &pMB->mvs[1], |
217 |
&pMB->pmvs[1]); |
&pMB->pmvs[1]); |
218 |
|
} |
219 |
if (sad8 < pMB->sad16) |
if (sad8 < pMB->sad16) { |
220 |
|
predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 2); |
221 |
sad8 += pMB->sad8[2] = |
sad8 += pMB->sad8[2] = |
222 |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
223 |
pCurrent, 2 * x, 2 * y + 1, pMB->mv16.x, |
pCurrent, 2 * x, 2 * y + 1, |
224 |
pMB->mv16.y, current->motion_flags, |
pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y, |
225 |
|
current->motion_flags, |
226 |
current->quant, current->fcode, pParam, |
current->quant, current->fcode, pParam, |
227 |
pMBs, prevMBs, &pMB->mvs[2], |
pMBs, prevMBs, &pMB->mvs[2], |
228 |
&pMB->pmvs[2]); |
&pMB->pmvs[2]); |
229 |
|
} |
230 |
if (sad8 < pMB->sad16) |
if (sad8 < pMB->sad16) { |
231 |
|
predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 3); |
232 |
sad8 += pMB->sad8[3] = |
sad8 += pMB->sad8[3] = |
233 |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
234 |
pCurrent, 2 * x + 1, 2 * y + 1, |
pCurrent, 2 * x + 1, 2 * y + 1, |
235 |
pMB->mv16.x, pMB->mv16.y, |
pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y, |
236 |
current->motion_flags, current->quant, |
current->motion_flags, |
237 |
current->fcode, pParam, pMBs, prevMBs, |
current->quant, current->fcode, pParam, |
238 |
&pMB->mvs[3], &pMB->pmvs[3]); |
pMBs, prevMBs, |
239 |
|
&pMB->mvs[3], |
240 |
|
&pMB->pmvs[3]); |
241 |
|
} |
242 |
|
|
243 |
/* decide: MODE_INTER or MODE_INTER4V |
/* decide: MODE_INTER or MODE_INTER4V |
244 |
mpeg4: if (sad8 < pMB->sad16 - nb/2+1) use_inter4v |
mpeg4: if (sad8 < pMB->sad16 - nb/2+1) use_inter4v |
266 |
return 0; |
return 0; |
267 |
} |
} |
268 |
|
|
269 |
|
|
270 |
#define CHECK_MV16_ZERO {\ |
#define CHECK_MV16_ZERO {\ |
271 |
if ( (0 <= max_dx) && (0 >= min_dx) \ |
if ( (0 <= max_dx) && (0 >= min_dx) \ |
272 |
&& (0 <= max_dy) && (0 >= min_dy) ) \ |
&& (0 <= max_dy) && (0 >= min_dy) ) \ |
273 |
{ \ |
{ \ |
274 |
iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0, 0 , iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); \ |
iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0, 0 , iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); \ |
275 |
iSAD += calc_delta_16(-pmv[0].x, -pmv[0].y, (uint8_t)iFcode, iQuant);\ |
iSAD += calc_delta_16(-center_x, -center_y, (uint8_t)iFcode, iQuant);\ |
276 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
277 |
{ iMinSAD=iSAD; currMV->x=0; currMV->y=0; } } \ |
{ iMinSAD=iSAD; currMV->x=0; currMV->y=0; } } \ |
278 |
} |
} |
279 |
|
|
280 |
#define NOCHECK_MV16_CANDIDATE(X,Y) { \ |
#define NOCHECK_MV16_CANDIDATE(X,Y) { \ |
281 |
iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ |
iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ |
282 |
iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\ |
iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\ |
283 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
284 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \ |
285 |
} |
} |
289 |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
290 |
{ \ |
{ \ |
291 |
iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ |
iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ |
292 |
iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\ |
iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\ |
293 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
294 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \ |
295 |
} |
} |
299 |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
300 |
{ \ |
{ \ |
301 |
iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ |
iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ |
302 |
iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\ |
iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\ |
303 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
304 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \ |
305 |
} |
} |
309 |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
310 |
{ \ |
{ \ |
311 |
iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ |
iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ |
312 |
iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\ |
iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\ |
313 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
314 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \ |
315 |
} |
} |
317 |
|
|
318 |
#define CHECK_MV8_ZERO {\ |
#define CHECK_MV8_ZERO {\ |
319 |
iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, 0, 0 , iEdgedWidth), iEdgedWidth); \ |
iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, 0, 0 , iEdgedWidth), iEdgedWidth); \ |
320 |
iSAD += calc_delta_8(-pmv[0].x, -pmv[0].y, (uint8_t)iFcode, iQuant);\ |
iSAD += calc_delta_8(-center_x, -center_y, (uint8_t)iFcode, iQuant);\ |
321 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
322 |
{ iMinSAD=iSAD; currMV->x=0; currMV->y=0; } \ |
{ iMinSAD=iSAD; currMV->x=0; currMV->y=0; } \ |
323 |
} |
} |
325 |
#define NOCHECK_MV8_CANDIDATE(X,Y) \ |
#define NOCHECK_MV8_CANDIDATE(X,Y) \ |
326 |
{ \ |
{ \ |
327 |
iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \ |
iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \ |
328 |
iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\ |
iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\ |
329 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
330 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \ |
331 |
} |
} |
335 |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
336 |
{ \ |
{ \ |
337 |
iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \ |
iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \ |
338 |
iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\ |
iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\ |
339 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
340 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \ |
341 |
} |
} |
345 |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
346 |
{ \ |
{ \ |
347 |
iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \ |
iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \ |
348 |
iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\ |
iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\ |
349 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
350 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \ |
351 |
} |
} |
355 |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
356 |
{ \ |
{ \ |
357 |
iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \ |
iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \ |
358 |
iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\ |
iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\ |
359 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
360 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \ |
361 |
} |
} |
410 |
const uint8_t * const cur, |
const uint8_t * const cur, |
411 |
const int x, |
const int x, |
412 |
const int y, |
const int y, |
413 |
int32_t startx, |
const int start_x, |
414 |
int32_t starty, |
const int start_y, |
415 |
int32_t iMinSAD, |
int iMinSAD, |
416 |
VECTOR * const currMV, |
VECTOR * const currMV, |
417 |
const VECTOR * const pmv, |
const int center_x, |
418 |
|
const int center_y, |
419 |
const int32_t min_dx, |
const int32_t min_dx, |
420 |
const int32_t max_dx, |
const int32_t max_dx, |
421 |
const int32_t min_dy, |
const int32_t min_dy, |
429 |
/* Do a diamond search around given starting point, return SAD of best */ |
/* Do a diamond search around given starting point, return SAD of best */ |
430 |
|
|
431 |
int32_t iDirection = 0; |
int32_t iDirection = 0; |
432 |
|
int32_t iDirectionBackup; |
433 |
int32_t iSAD; |
int32_t iSAD; |
434 |
VECTOR backupMV; |
VECTOR backupMV; |
435 |
|
|
436 |
backupMV.x = startx; |
backupMV.x = start_x; |
437 |
backupMV.y = starty; |
backupMV.y = start_y; |
438 |
|
|
439 |
/* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */ |
/* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */ |
440 |
|
|
443 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3); |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3); |
444 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4); |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4); |
445 |
|
|
446 |
if (iDirection) |
if (iDirection) { |
447 |
while (!iFound) { |
while (!iFound) { |
448 |
iFound = 1; |
iFound = 1; |
449 |
backupMV = *currMV; |
backupMV = *currMV; |
450 |
|
iDirectionBackup = iDirection; |
451 |
|
|
452 |
if (iDirection != 2) |
if (iDirectionBackup != 2) |
453 |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
454 |
backupMV.y, 1); |
backupMV.y, 1); |
455 |
if (iDirection != 1) |
if (iDirectionBackup != 1) |
456 |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
457 |
backupMV.y, 2); |
backupMV.y, 2); |
458 |
if (iDirection != 4) |
if (iDirectionBackup != 4) |
459 |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, |
460 |
backupMV.y - iDiamondSize, 3); |
backupMV.y - iDiamondSize, 3); |
461 |
if (iDirection != 3) |
if (iDirectionBackup != 3) |
462 |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, |
463 |
backupMV.y + iDiamondSize, 4); |
backupMV.y + iDiamondSize, 4); |
464 |
|
} |
465 |
} else { |
} else { |
466 |
currMV->x = startx; |
currMV->x = start_x; |
467 |
currMV->y = starty; |
currMV->y = start_y; |
468 |
} |
} |
469 |
return iMinSAD; |
return iMinSAD; |
470 |
} |
} |
477 |
const uint8_t * const cur, |
const uint8_t * const cur, |
478 |
const int x, |
const int x, |
479 |
const int y, |
const int y, |
480 |
int32_t startx, |
const int start_x, |
481 |
int32_t starty, |
const int start_y, |
482 |
int32_t iMinSAD, |
int iMinSAD, |
483 |
VECTOR * const currMV, |
VECTOR * const currMV, |
484 |
const VECTOR * const pmv, |
const int center_x, |
485 |
|
const int center_y, |
486 |
const int32_t min_dx, |
const int32_t min_dx, |
487 |
const int32_t max_dx, |
const int32_t max_dx, |
488 |
const int32_t min_dy, |
const int32_t min_dy, |
499 |
int32_t iSAD; |
int32_t iSAD; |
500 |
VECTOR backupMV; |
VECTOR backupMV; |
501 |
|
|
502 |
backupMV.x = startx; |
backupMV.x = start_x; |
503 |
backupMV.y = starty; |
backupMV.y = start_y; |
504 |
|
|
505 |
/* It's one search with full square pattern, and new parts for all following diamonds */ |
/* It's one search with full square pattern, and new parts for all following diamonds */ |
506 |
|
|
525 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
526 |
|
|
527 |
|
|
528 |
if (iDirection) |
if (iDirection) { |
529 |
while (!iFound) { |
while (!iFound) { |
530 |
iFound = 1; |
iFound = 1; |
531 |
backupMV = *currMV; |
backupMV = *currMV; |
534 |
case 1: |
case 1: |
535 |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
536 |
backupMV.y, 1); |
backupMV.y, 1); |
537 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
538 |
backupMV.y - iDiamondSize, 5); |
backupMV.y - iDiamondSize, 5); |
539 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
540 |
backupMV.y - iDiamondSize, 7); |
backupMV.y - iDiamondSize, 7); |
541 |
break; |
break; |
542 |
case 2: |
case 2: |
543 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
544 |
2); |
2); |
545 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
546 |
backupMV.y + iDiamondSize, 6); |
backupMV.y + iDiamondSize, 6); |
547 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
548 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
549 |
break; |
break; |
550 |
|
|
551 |
case 3: |
case 3: |
552 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
553 |
4); |
4); |
554 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
555 |
backupMV.y - iDiamondSize, 7); |
backupMV.y - iDiamondSize, 7); |
556 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
557 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
558 |
break; |
break; |
559 |
|
|
560 |
case 4: |
case 4: |
561 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
562 |
3); |
3); |
563 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
564 |
backupMV.y - iDiamondSize, 5); |
backupMV.y - iDiamondSize, 5); |
565 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
566 |
backupMV.y + iDiamondSize, 6); |
backupMV.y + iDiamondSize, 6); |
567 |
break; |
break; |
568 |
|
|
569 |
case 5: |
case 5: |
570 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y, |
571 |
1); |
1); |
572 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
573 |
3); |
3); |
574 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
575 |
backupMV.y - iDiamondSize, 5); |
backupMV.y - iDiamondSize, 5); |
576 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
577 |
backupMV.y + iDiamondSize, 6); |
backupMV.y + iDiamondSize, 6); |
578 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
579 |
backupMV.y - iDiamondSize, 7); |
backupMV.y - iDiamondSize, 7); |
580 |
break; |
break; |
581 |
|
|
582 |
case 6: |
case 6: |
583 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
584 |
2); |
2); |
585 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
586 |
3); |
3); |
587 |
|
|
588 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
589 |
backupMV.y - iDiamondSize, 5); |
backupMV.y - iDiamondSize, 5); |
590 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
591 |
backupMV.y + iDiamondSize, 6); |
backupMV.y + iDiamondSize, 6); |
592 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
593 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
594 |
|
|
595 |
break; |
break; |
597 |
case 7: |
case 7: |
598 |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
599 |
backupMV.y, 1); |
backupMV.y, 1); |
600 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
601 |
4); |
4); |
602 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
603 |
backupMV.y - iDiamondSize, 5); |
backupMV.y - iDiamondSize, 5); |
604 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
605 |
backupMV.y - iDiamondSize, 7); |
backupMV.y - iDiamondSize, 7); |
606 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
607 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
608 |
break; |
break; |
609 |
|
|
610 |
case 8: |
case 8: |
611 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
612 |
2); |
2); |
613 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
614 |
4); |
4); |
615 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
616 |
backupMV.y + iDiamondSize, 6); |
backupMV.y + iDiamondSize, 6); |
617 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
618 |
backupMV.y - iDiamondSize, 7); |
backupMV.y - iDiamondSize, 7); |
619 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
620 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
621 |
break; |
break; |
622 |
default: |
default: |
623 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y, |
624 |
1); |
1); |
625 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
626 |
2); |
2); |
627 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
628 |
3); |
3); |
629 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
630 |
4); |
4); |
631 |
|
|
632 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
633 |
backupMV.y - iDiamondSize, 5); |
backupMV.y - iDiamondSize, 5); |
634 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
635 |
backupMV.y + iDiamondSize, 6); |
backupMV.y + iDiamondSize, 6); |
636 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
637 |
backupMV.y - iDiamondSize, 7); |
backupMV.y - iDiamondSize, 7); |
638 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
639 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
640 |
break; |
break; |
641 |
} |
} |
642 |
|
} |
643 |
} else { |
} else { |
644 |
currMV->x = startx; |
currMV->x = start_x; |
645 |
currMV->y = starty; |
currMV->y = start_y; |
646 |
} |
} |
647 |
return iMinSAD; |
return iMinSAD; |
648 |
} |
} |
656 |
const uint8_t * const cur, |
const uint8_t * const cur, |
657 |
const int x, |
const int x, |
658 |
const int y, |
const int y, |
659 |
int32_t startx, |
const int start_x, |
660 |
int32_t starty, |
const int start_y, |
661 |
int32_t iMinSAD, |
int iMinSAD, |
662 |
VECTOR * const currMV, |
VECTOR * const currMV, |
663 |
const VECTOR * const pmv, |
const int center_x, |
664 |
|
const int center_y, |
665 |
const int32_t min_dx, |
const int32_t min_dx, |
666 |
const int32_t max_dx, |
const int32_t max_dx, |
667 |
const int32_t min_dy, |
const int32_t min_dy, |
676 |
int32_t dx, dy; |
int32_t dx, dy; |
677 |
VECTOR backupMV; |
VECTOR backupMV; |
678 |
|
|
679 |
backupMV.x = startx; |
backupMV.x = start_x; |
680 |
backupMV.y = starty; |
backupMV.y = start_y; |
681 |
|
|
682 |
for (dx = min_dx; dx <= max_dx; dx += iDiamondSize) |
for (dx = min_dx; dx <= max_dx; dx += iDiamondSize) |
683 |
for (dy = min_dy; dy <= max_dy; dy += iDiamondSize) |
for (dy = min_dy; dy <= max_dy; dy += iDiamondSize) |
694 |
const uint8_t * const cur, |
const uint8_t * const cur, |
695 |
const int x, |
const int x, |
696 |
const int y, |
const int y, |
697 |
int32_t startx, |
int start_x, |
698 |
int32_t starty, |
int start_y, |
699 |
int32_t iMinSAD, |
int iMinSAD, |
700 |
VECTOR * const currMV, |
VECTOR * const currMV, |
701 |
const VECTOR * const pmv, |
const int center_x, |
702 |
|
const int center_y, |
703 |
const int32_t min_dx, |
const int32_t min_dx, |
704 |
const int32_t max_dx, |
const int32_t max_dx, |
705 |
const int32_t min_dy, |
const int32_t min_dy, |
716 |
/* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */ |
/* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */ |
717 |
|
|
718 |
if (iDirection) { |
if (iDirection) { |
719 |
CHECK_MV16_CANDIDATE(startx - iDiamondSize, starty); |
CHECK_MV16_CANDIDATE(start_x - iDiamondSize, start_y); |
720 |
CHECK_MV16_CANDIDATE(startx + iDiamondSize, starty); |
CHECK_MV16_CANDIDATE(start_x + iDiamondSize, start_y); |
721 |
CHECK_MV16_CANDIDATE(startx, starty - iDiamondSize); |
CHECK_MV16_CANDIDATE(start_x, start_y - iDiamondSize); |
722 |
CHECK_MV16_CANDIDATE(startx, starty + iDiamondSize); |
CHECK_MV16_CANDIDATE(start_x, start_y + iDiamondSize); |
723 |
} else { |
} else { |
724 |
int bDirection = 1 + 2 + 4 + 8; |
int bDirection = 1 + 2 + 4 + 8; |
725 |
|
|
726 |
do { |
do { |
727 |
iDirection = 0; |
iDirection = 0; |
728 |
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) |
if (bDirection & 1) //we only want to check left if we came from the right (our last motion was to the left, up-left or down-left) |
729 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, starty, 1); |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1); |
730 |
|
|
731 |
if (bDirection & 2) |
if (bDirection & 2) |
732 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, starty, 2); |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2); |
733 |
|
|
734 |
if (bDirection & 4) |
if (bDirection & 4) |
735 |
CHECK_MV16_CANDIDATE_DIR(startx, starty - iDiamondSize, 4); |
CHECK_MV16_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4); |
736 |
|
|
737 |
if (bDirection & 8) |
if (bDirection & 8) |
738 |
CHECK_MV16_CANDIDATE_DIR(startx, starty + iDiamondSize, 8); |
CHECK_MV16_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8); |
739 |
|
|
740 |
/* now we're doing diagonal checks near our candidate */ |
/* now we're doing diagonal checks near our candidate */ |
741 |
|
|
743 |
{ |
{ |
744 |
bDirection = iDirection; |
bDirection = iDirection; |
745 |
iDirection = 0; |
iDirection = 0; |
746 |
startx = currMV->x; |
start_x = currMV->x; |
747 |
starty = currMV->y; |
start_y = currMV->y; |
748 |
if (bDirection & 3) //our candidate is left or right |
if (bDirection & 3) //our candidate is left or right |
749 |
{ |
{ |
750 |
CHECK_MV16_CANDIDATE_DIR(startx, starty + iDiamondSize, 8); |
CHECK_MV16_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8); |
751 |
CHECK_MV16_CANDIDATE_DIR(startx, starty - iDiamondSize, 4); |
CHECK_MV16_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4); |
752 |
} else // what remains here is up or down |
} else // what remains here is up or down |
753 |
{ |
{ |
754 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, starty, 2); |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2); |
755 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, starty, 1); |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1); |
756 |
} |
} |
757 |
|
|
758 |
if (iDirection) { |
if (iDirection) { |
759 |
bDirection += iDirection; |
bDirection += iDirection; |
760 |
startx = currMV->x; |
start_x = currMV->x; |
761 |
starty = currMV->y; |
start_y = currMV->y; |
762 |
} |
} |
763 |
} else //about to quit, eh? not so fast.... |
} else //about to quit, eh? not so fast.... |
764 |
{ |
{ |
765 |
switch (bDirection) { |
switch (bDirection) { |
766 |
case 2: |
case 2: |
767 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
768 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
769 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
770 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
771 |
break; |
break; |
772 |
case 1: |
case 1: |
773 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
|
774 |
starty - iDiamondSize, 1 + 4); |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
775 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
start_y - iDiamondSize, 1 + 4); |
776 |
starty + iDiamondSize, 1 + 8); |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
777 |
|
start_y + iDiamondSize, 1 + 8); |
778 |
break; |
break; |
779 |
case 2 + 4: |
case 2 + 4: |
780 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
781 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
782 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
783 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
784 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
785 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
786 |
break; |
break; |
787 |
case 4: |
case 4: |
788 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
789 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
790 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
791 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
792 |
break; |
break; |
793 |
case 8: |
case 8: |
794 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
795 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
796 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
797 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
798 |
break; |
break; |
799 |
case 1 + 4: |
case 1 + 4: |
800 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
801 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
802 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
803 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
804 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
805 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
806 |
break; |
break; |
807 |
case 2 + 8: |
case 2 + 8: |
808 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
809 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
810 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
811 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
812 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
813 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
814 |
break; |
break; |
815 |
case 1 + 8: |
case 1 + 8: |
816 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
817 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
818 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
819 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
820 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
821 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
822 |
break; |
break; |
823 |
default: //1+2+4+8 == we didn't find anything at all |
default: //1+2+4+8 == we didn't find anything at all |
824 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
825 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
826 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
827 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
828 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
829 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
830 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
831 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
832 |
break; |
break; |
833 |
} |
} |
834 |
if (!iDirection) |
if (!iDirection) |
835 |
break; //ok, the end. really |
break; //ok, the end. really |
836 |
else { |
else { |
837 |
bDirection = iDirection; |
bDirection = iDirection; |
838 |
startx = currMV->x; |
start_x = currMV->x; |
839 |
starty = currMV->y; |
start_y = currMV->y; |
840 |
} |
} |
841 |
} |
} |
842 |
} |
} |
845 |
return iMinSAD; |
return iMinSAD; |
846 |
} |
} |
847 |
|
|
848 |
|
#define CHECK_MV16_F_INTERPOL(X,Y) { \ |
849 |
|
if ( ((X) <= f_max_dx) && ((X) >= f_min_dx) \ |
850 |
|
&& ((Y) <= f_max_dy) && ((Y) >= f_min_dy) ) \ |
851 |
|
{ \ |
852 |
|
iSAD = sad16bi( cur, \ |
853 |
|
get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, X, Y, iEdgedWidth), \ |
854 |
|
get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, b_currMV->x, b_currMV->y, iEdgedWidth), \ |
855 |
|
iEdgedWidth); \ |
856 |
|
iSAD += calc_delta_16((X) - f_center_x, (Y) - f_center_y, (uint8_t)f_iFcode, iQuant);\ |
857 |
|
iSAD += calc_delta_16(b_currMV->x - b_center_x, b_currMV->y - b_center_y, (uint8_t)b_iFcode, iQuant);\ |
858 |
|
if (iSAD < iMinSAD) \ |
859 |
|
{ iMinSAD=iSAD; f_currMV->x=(X); f_currMV->y=(Y); } } \ |
860 |
|
} |
861 |
|
|
862 |
|
#define CHECK_MV16_F_INTERPOL_FOUND(X,Y) { \ |
863 |
|
if ( ((X) <= f_max_dx) && ((X) >= f_min_dx) \ |
864 |
|
&& ((Y) <= f_max_dy) && ((Y) >= f_min_dy) ) \ |
865 |
|
{ \ |
866 |
|
iSAD = sad16bi( cur, \ |
867 |
|
get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, X, Y, iEdgedWidth), \ |
868 |
|
get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, b_currMV->x, b_currMV->y, iEdgedWidth), \ |
869 |
|
iEdgedWidth); \ |
870 |
|
iSAD += calc_delta_16((X) - f_center_x, (Y) - f_center_y, (uint8_t)f_iFcode, iQuant);\ |
871 |
|
iSAD += calc_delta_16(b_currMV->x - b_center_x, b_currMV->y - b_center_y, (uint8_t)b_iFcode, iQuant);\ |
872 |
|
if (iSAD < iMinSAD) \ |
873 |
|
{ iMinSAD=iSAD; f_currMV->x=(X); f_currMV->y=(Y); iFound=0;} } \ |
874 |
|
} |
875 |
|
|
876 |
|
#define CHECK_MV16_B_INTERPOL(X,Y) { \ |
877 |
|
if ( ((X) <= b_max_dx) && ((X) >= b_min_dx) \ |
878 |
|
&& ((Y) <= b_max_dy) && ((Y) >= b_min_dy) ) \ |
879 |
|
{ \ |
880 |
|
iSAD = sad16bi( cur, \ |
881 |
|
get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, f_currMV->x, f_currMV->y, iEdgedWidth), \ |
882 |
|
get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, X, Y, iEdgedWidth), \ |
883 |
|
iEdgedWidth); \ |
884 |
|
iSAD += calc_delta_16(f_currMV->x - f_center_x, f_currMV->y - f_center_y, (uint8_t)f_iFcode, iQuant);\ |
885 |
|
iSAD += calc_delta_16((X) - b_center_x, (Y) - b_center_y, (uint8_t)b_iFcode, iQuant);\ |
886 |
|
if (iSAD < iMinSAD) \ |
887 |
|
{ iMinSAD=iSAD; b_currMV->x=(X); b_currMV->y=(Y); } } \ |
888 |
|
} |
889 |
|
|
890 |
|
#define CHECK_MV16_B_INTERPOL_FOUND(X,Y) { \ |
891 |
|
if ( ((X) <= b_max_dx) && ((X) >= b_min_dx) \ |
892 |
|
&& ((Y) <= b_max_dy) && ((Y) >= b_min_dy) ) \ |
893 |
|
{ \ |
894 |
|
iSAD = sad16bi( cur, \ |
895 |
|
get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, f_currMV->x, f_currMV->y, iEdgedWidth), \ |
896 |
|
get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, X, Y, iEdgedWidth), \ |
897 |
|
iEdgedWidth); \ |
898 |
|
iSAD += calc_delta_16(f_currMV->x - f_center_x, f_currMV->y - f_center_y, (uint8_t)f_iFcode, iQuant);\ |
899 |
|
iSAD += calc_delta_16((X) - b_center_x, (Y) - b_center_y, (uint8_t)b_iFcode, iQuant);\ |
900 |
|
if (iSAD < iMinSAD) \ |
901 |
|
{ iMinSAD=iSAD; b_currMV->x=(X); b_currMV->y=(Y); iFound=0;} } \ |
902 |
|
} |
903 |
|
|
904 |
|
int32_t |
905 |
|
Diamond16_InterpolMainSearch( |
906 |
|
const uint8_t * const f_pRef, |
907 |
|
const uint8_t * const f_pRefH, |
908 |
|
const uint8_t * const f_pRefV, |
909 |
|
const uint8_t * const f_pRefHV, |
910 |
|
|
911 |
|
const uint8_t * const cur, |
912 |
|
|
913 |
|
const uint8_t * const b_pRef, |
914 |
|
const uint8_t * const b_pRefH, |
915 |
|
const uint8_t * const b_pRefV, |
916 |
|
const uint8_t * const b_pRefHV, |
917 |
|
|
918 |
|
const int x, |
919 |
|
const int y, |
920 |
|
|
921 |
|
const int f_start_x, |
922 |
|
const int f_start_y, |
923 |
|
const int b_start_x, |
924 |
|
const int b_start_y, |
925 |
|
|
926 |
|
int iMinSAD, |
927 |
|
VECTOR * const f_currMV, |
928 |
|
VECTOR * const b_currMV, |
929 |
|
|
930 |
|
const int f_center_x, |
931 |
|
const int f_center_y, |
932 |
|
const int b_center_x, |
933 |
|
const int b_center_y, |
934 |
|
|
935 |
|
const int32_t f_min_dx, |
936 |
|
const int32_t f_max_dx, |
937 |
|
const int32_t f_min_dy, |
938 |
|
const int32_t f_max_dy, |
939 |
|
|
940 |
|
const int32_t b_min_dx, |
941 |
|
const int32_t b_max_dx, |
942 |
|
const int32_t b_min_dy, |
943 |
|
const int32_t b_max_dy, |
944 |
|
|
945 |
|
const int32_t iEdgedWidth, |
946 |
|
const int32_t iDiamondSize, |
947 |
|
|
948 |
|
const int32_t f_iFcode, |
949 |
|
const int32_t b_iFcode, |
950 |
|
|
951 |
|
const int32_t iQuant, |
952 |
|
int iFound) |
953 |
|
{ |
954 |
|
/* Do a diamond search around given starting point, return SAD of best */ |
955 |
|
|
956 |
|
int32_t iSAD; |
957 |
|
|
958 |
|
VECTOR f_backupMV; |
959 |
|
VECTOR b_backupMV; |
960 |
|
|
961 |
|
f_currMV->x = f_start_x; |
962 |
|
f_currMV->y = f_start_y; |
963 |
|
b_currMV->x = b_start_x; |
964 |
|
b_currMV->y = b_start_y; |
965 |
|
|
966 |
|
do |
967 |
|
{ |
968 |
|
iFound = 1; |
969 |
|
|
970 |
|
f_backupMV = *f_currMV; |
971 |
|
|
972 |
|
CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x - iDiamondSize, f_backupMV.y); |
973 |
|
CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x + iDiamondSize, f_backupMV.y); |
974 |
|
CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x, f_backupMV.y - iDiamondSize); |
975 |
|
CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x, f_backupMV.y + iDiamondSize); |
976 |
|
|
977 |
|
b_backupMV = *b_currMV; |
978 |
|
|
979 |
|
CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x - iDiamondSize, b_backupMV.y); |
980 |
|
CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x + iDiamondSize, b_backupMV.y); |
981 |
|
CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x, b_backupMV.y - iDiamondSize); |
982 |
|
CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x, b_backupMV.y + iDiamondSize); |
983 |
|
|
984 |
|
} while (!iFound); |
985 |
|
|
986 |
|
return iMinSAD; |
987 |
|
} |
988 |
|
|
989 |
|
/* Sorry, these MACROS really got too large... I'll turn them into function soon! */ |
990 |
|
|
991 |
|
#define CHECK_MV16_DIRECT_FOUND(X,Y) \ |
992 |
|
if ( (X)>=(-32) && (X)<=(31) && ((Y)>=-32) && ((Y)<=31) ) \ |
993 |
|
{ int k;\ |
994 |
|
VECTOR mvs,b_mvs; \ |
995 |
|
iSAD = 0;\ |
996 |
|
for (k = 0; k < 4; k++) { \ |
997 |
|
mvs.x = (int32_t) ((TRB * directmv[k].x) / TRD + (X)); \ |
998 |
|
b_mvs.x = (int32_t) (((X) == 0) \ |
999 |
|
? ((TRB - TRD) * directmv[k].x) / TRD \ |
1000 |
|
: mvs.x - directmv[k].x); \ |
1001 |
|
\ |
1002 |
|
mvs.y = (int32_t) ((TRB * directmv[k].y) / TRD + (Y)); \ |
1003 |
|
b_mvs.y = (int32_t) (((Y) == 0) \ |
1004 |
|
? ((TRB - TRD) * directmv[k].y) / TRD \ |
1005 |
|
: mvs.y - directmv[k].y); \ |
1006 |
|
\ |
1007 |
|
if ( (mvs.x <= max_dx) && (mvs.x >= min_dx) \ |
1008 |
|
&& (mvs.y <= max_dy) && (mvs.y >= min_dy) \ |
1009 |
|
&& (b_mvs.x <= max_dx) && (b_mvs.x >= min_dx) \ |
1010 |
|
&& (b_mvs.y <= max_dy) && (b_mvs.y >= min_dy) ) { \ |
1011 |
|
iSAD += sad8bi( cur + 8*(k&1) + 8*(k>>1)*iEdgedWidth, \ |
1012 |
|
get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, 2*x+(k&1), 2*y+(k>>1), 8, \ |
1013 |
|
mvs.x, mvs.y, iEdgedWidth), \ |
1014 |
|
get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, 2*x+(k&1), 2*y+(k>>1), 8, \ |
1015 |
|
b_mvs.x, b_mvs.y, iEdgedWidth), \ |
1016 |
|
iEdgedWidth); \ |
1017 |
|
} \ |
1018 |
|
else \ |
1019 |
|
iSAD = 65535; \ |
1020 |
|
} \ |
1021 |
|
iSAD += calc_delta_16((X),(Y), 1, iQuant);\ |
1022 |
|
if (iSAD < iMinSAD) \ |
1023 |
|
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iFound=0; } \ |
1024 |
|
} |
1025 |
|
|
1026 |
|
|
1027 |
|
|
1028 |
|
int32_t |
1029 |
|
Diamond16_DirectMainSearch( |
1030 |
|
const uint8_t * const f_pRef, |
1031 |
|
const uint8_t * const f_pRefH, |
1032 |
|
const uint8_t * const f_pRefV, |
1033 |
|
const uint8_t * const f_pRefHV, |
1034 |
|
|
1035 |
|
const uint8_t * const cur, |
1036 |
|
|
1037 |
|
const uint8_t * const b_pRef, |
1038 |
|
const uint8_t * const b_pRefH, |
1039 |
|
const uint8_t * const b_pRefV, |
1040 |
|
const uint8_t * const b_pRefHV, |
1041 |
|
|
1042 |
|
const int x, |
1043 |
|
const int y, |
1044 |
|
|
1045 |
|
const int TRB, |
1046 |
|
const int TRD, |
1047 |
|
|
1048 |
|
const int start_x, |
1049 |
|
const int start_y, |
1050 |
|
|
1051 |
|
int iMinSAD, |
1052 |
|
VECTOR * const currMV, |
1053 |
|
const VECTOR * const directmv, |
1054 |
|
|
1055 |
|
const int32_t min_dx, |
1056 |
|
const int32_t max_dx, |
1057 |
|
const int32_t min_dy, |
1058 |
|
const int32_t max_dy, |
1059 |
|
|
1060 |
|
const int32_t iEdgedWidth, |
1061 |
|
const int32_t iDiamondSize, |
1062 |
|
|
1063 |
|
const int32_t iQuant, |
1064 |
|
int iFound) |
1065 |
|
{ |
1066 |
|
/* Do a diamond search around given starting point, return SAD of best */ |
1067 |
|
|
1068 |
|
int32_t iSAD; |
1069 |
|
|
1070 |
|
VECTOR backupMV; |
1071 |
|
|
1072 |
|
currMV->x = start_x; |
1073 |
|
currMV->y = start_y; |
1074 |
|
|
1075 |
|
/* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */ |
1076 |
|
|
1077 |
|
do |
1078 |
|
{ |
1079 |
|
iFound = 1; |
1080 |
|
|
1081 |
|
backupMV = *currMV; |
1082 |
|
|
1083 |
|
CHECK_MV16_DIRECT_FOUND(backupMV.x - iDiamondSize, backupMV.y); |
1084 |
|
CHECK_MV16_DIRECT_FOUND(backupMV.x + iDiamondSize, backupMV.y); |
1085 |
|
CHECK_MV16_DIRECT_FOUND(backupMV.x, backupMV.y - iDiamondSize); |
1086 |
|
CHECK_MV16_DIRECT_FOUND(backupMV.x, backupMV.y + iDiamondSize); |
1087 |
|
|
1088 |
|
} while (!iFound); |
1089 |
|
|
1090 |
|
return iMinSAD; |
1091 |
|
} |
1092 |
|
|
1093 |
|
|
1094 |
int32_t |
int32_t |
1095 |
AdvDiamond8_MainSearch(const uint8_t * const pRef, |
AdvDiamond8_MainSearch(const uint8_t * const pRef, |
1096 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
1099 |
const uint8_t * const cur, |
const uint8_t * const cur, |
1100 |
const int x, |
const int x, |
1101 |
const int y, |
const int y, |
1102 |
int32_t startx, |
int start_x, |
1103 |
int32_t starty, |
int start_y, |
1104 |
int32_t iMinSAD, |
int iMinSAD, |
1105 |
VECTOR * const currMV, |
VECTOR * const currMV, |
1106 |
const VECTOR * const pmv, |
const int center_x, |
1107 |
|
const int center_y, |
1108 |
const int32_t min_dx, |
const int32_t min_dx, |
1109 |
const int32_t max_dx, |
const int32_t max_dx, |
1110 |
const int32_t min_dy, |
const int32_t min_dy, |
1121 |
/* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */ |
/* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */ |
1122 |
|
|
1123 |
if (iDirection) { |
if (iDirection) { |
1124 |
CHECK_MV8_CANDIDATE(startx - iDiamondSize, starty); |
CHECK_MV8_CANDIDATE(start_x - iDiamondSize, start_y); |
1125 |
CHECK_MV8_CANDIDATE(startx + iDiamondSize, starty); |
CHECK_MV8_CANDIDATE(start_x + iDiamondSize, start_y); |
1126 |
CHECK_MV8_CANDIDATE(startx, starty - iDiamondSize); |
CHECK_MV8_CANDIDATE(start_x, start_y - iDiamondSize); |
1127 |
CHECK_MV8_CANDIDATE(startx, starty + iDiamondSize); |
CHECK_MV8_CANDIDATE(start_x, start_y + iDiamondSize); |
1128 |
} else { |
} else { |
1129 |
int bDirection = 1 + 2 + 4 + 8; |
int bDirection = 1 + 2 + 4 + 8; |
1130 |
|
|
1131 |
do { |
do { |
1132 |
iDirection = 0; |
iDirection = 0; |
1133 |
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) |
if (bDirection & 1) //we only want to check left if we came from the right (our last motion was to the left, up-left or down-left) |
1134 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, starty, 1); |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1); |
1135 |
|
|
1136 |
if (bDirection & 2) |
if (bDirection & 2) |
1137 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, starty, 2); |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2); |
1138 |
|
|
1139 |
if (bDirection & 4) |
if (bDirection & 4) |
1140 |
CHECK_MV8_CANDIDATE_DIR(startx, starty - iDiamondSize, 4); |
CHECK_MV8_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4); |
1141 |
|
|
1142 |
if (bDirection & 8) |
if (bDirection & 8) |
1143 |
CHECK_MV8_CANDIDATE_DIR(startx, starty + iDiamondSize, 8); |
CHECK_MV8_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8); |
1144 |
|
|
1145 |
/* now we're doing diagonal checks near our candidate */ |
/* now we're doing diagonal checks near our candidate */ |
1146 |
|
|
1148 |
{ |
{ |
1149 |
bDirection = iDirection; |
bDirection = iDirection; |
1150 |
iDirection = 0; |
iDirection = 0; |
1151 |
startx = currMV->x; |
start_x = currMV->x; |
1152 |
starty = currMV->y; |
start_y = currMV->y; |
1153 |
if (bDirection & 3) //our candidate is left or right |
if (bDirection & 3) //our candidate is left or right |
1154 |
{ |
{ |
1155 |
CHECK_MV8_CANDIDATE_DIR(startx, starty + iDiamondSize, 8); |
CHECK_MV8_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8); |
1156 |
CHECK_MV8_CANDIDATE_DIR(startx, starty - iDiamondSize, 4); |
CHECK_MV8_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4); |
1157 |
} else // what remains here is up or down |
} else // what remains here is up or down |
1158 |
{ |
{ |
1159 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, starty, 2); |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2); |
1160 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, starty, 1); |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1); |
1161 |
} |
} |
1162 |
|
|
1163 |
if (iDirection) { |
if (iDirection) { |
1164 |
bDirection += iDirection; |
bDirection += iDirection; |
1165 |
startx = currMV->x; |
start_x = currMV->x; |
1166 |
starty = currMV->y; |
start_y = currMV->y; |
1167 |
} |
} |
1168 |
} else //about to quit, eh? not so fast.... |
} else //about to quit, eh? not so fast.... |
1169 |
{ |
{ |
1170 |
switch (bDirection) { |
switch (bDirection) { |
1171 |
case 2: |
case 2: |
1172 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
1173 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
1174 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
1175 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
1176 |
break; |
break; |
1177 |
case 1: |
case 1: |
1178 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
1179 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
1180 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
1181 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
1182 |
break; |
break; |
1183 |
case 2 + 4: |
case 2 + 4: |
1184 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
1185 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
1186 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
1187 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
1188 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
1189 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
1190 |
break; |
break; |
1191 |
case 4: |
case 4: |
1192 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
1193 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
1194 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
1195 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
1196 |
break; |
break; |
1197 |
case 8: |
case 8: |
1198 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
1199 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
1200 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
1201 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
1202 |
break; |
break; |
1203 |
case 1 + 4: |
case 1 + 4: |
1204 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
1205 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
1206 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
1207 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
1208 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
1209 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
1210 |
break; |
break; |
1211 |
case 2 + 8: |
case 2 + 8: |
1212 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
1213 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
1214 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
1215 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
1216 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
1217 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
1218 |
break; |
break; |
1219 |
case 1 + 8: |
case 1 + 8: |
1220 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
1221 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
1222 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
1223 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
1224 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
1225 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
1226 |
break; |
break; |
1227 |
default: //1+2+4+8 == we didn't find anything at all |
default: //1+2+4+8 == we didn't find anything at all |
1228 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
1229 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
1230 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
1231 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
1232 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
1233 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
1234 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
1235 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
1236 |
break; |
break; |
1237 |
} |
} |
1238 |
if (!(iDirection)) |
if (!(iDirection)) |
1239 |
break; //ok, the end. really |
break; //ok, the end. really |
1240 |
else { |
else { |
1241 |
bDirection = iDirection; |
bDirection = iDirection; |
1242 |
startx = currMV->x; |
start_x = currMV->x; |
1243 |
starty = currMV->y; |
start_y = currMV->y; |
1244 |
} |
} |
1245 |
} |
} |
1246 |
} |
} |
1258 |
const uint8_t * const cur, |
const uint8_t * const cur, |
1259 |
const int x, |
const int x, |
1260 |
const int y, |
const int y, |
1261 |
int32_t startx, |
const int start_x, |
1262 |
int32_t starty, |
const int start_y, |
1263 |
int32_t iMinSAD, |
int iMinSAD, |
1264 |
VECTOR * const currMV, |
VECTOR * const currMV, |
1265 |
const VECTOR * const pmv, |
const int center_x, |
1266 |
|
const int center_y, |
1267 |
const int32_t min_dx, |
const int32_t min_dx, |
1268 |
const int32_t max_dx, |
const int32_t max_dx, |
1269 |
const int32_t min_dy, |
const int32_t min_dy, |
1278 |
int32_t dx, dy; |
int32_t dx, dy; |
1279 |
VECTOR backupMV; |
VECTOR backupMV; |
1280 |
|
|
1281 |
backupMV.x = startx; |
backupMV.x = start_x; |
1282 |
backupMV.y = starty; |
backupMV.y = start_y; |
1283 |
|
|
1284 |
for (dx = min_dx; dx <= max_dx; dx += iDiamondSize) |
for (dx = min_dx; dx <= max_dx; dx += iDiamondSize) |
1285 |
for (dy = min_dy; dy <= max_dy; dy += iDiamondSize) |
for (dy = min_dy; dy <= max_dy; dy += iDiamondSize) |
1300 |
const int y, |
const int y, |
1301 |
VECTOR * const currMV, |
VECTOR * const currMV, |
1302 |
int32_t iMinSAD, |
int32_t iMinSAD, |
1303 |
const VECTOR * const pmv, |
const int center_x, |
1304 |
|
const int center_y, |
1305 |
const int32_t min_dx, |
const int32_t min_dx, |
1306 |
const int32_t max_dx, |
const int32_t max_dx, |
1307 |
const int32_t min_dy, |
const int32_t min_dy, |
1330 |
#define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16) |
#define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16) |
1331 |
|
|
1332 |
|
|
1333 |
|
|
1334 |
int32_t |
int32_t |
1335 |
PMVfastSearch16(const uint8_t * const pRef, |
PMVfastSearch16(const uint8_t * const pRef, |
1336 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
1339 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
1340 |
const int x, |
const int x, |
1341 |
const int y, |
const int y, |
1342 |
|
const int start_x, /* start is searched first, so it should contain the most */ |
1343 |
|
const int start_y, /* likely motion vector for this block */ |
1344 |
|
const int center_x, /* center is from where length of MVs is measured */ |
1345 |
|
const int center_y, |
1346 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
1347 |
const uint32_t iQuant, |
const uint32_t iQuant, |
1348 |
const uint32_t iFcode, |
const uint32_t iFcode, |
1399 |
//bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad); |
//bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad); |
1400 |
bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad); |
bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad); |
1401 |
|
|
|
/* fprintf(stderr,"pmv: %d %d / %d --- %d %d %d %d %d %d - %d %d %d\n", |
|
|
pmv[0].x,pmv[0].y,psad[0], |
|
|
pmv[1].x,pmv[1].y,pmv[2].x,pmv[2].y,pmv[3].x,pmv[3].y, |
|
|
psad[1],psad[2],psad[3]); |
|
|
*/ |
|
1402 |
if ((x == 0) && (y == 0)) { |
if ((x == 0) && (y == 0)) { |
1403 |
threshA = 512; |
threshA = 512; |
1404 |
threshB = 1024; |
threshB = 1024; |
1422 |
If SAD<=256 goto Step 10. |
If SAD<=256 goto Step 10. |
1423 |
*/ |
*/ |
1424 |
|
|
1425 |
*currMV = pmv[0]; /* current best := prediction */ |
currMV->x = start_x; |
1426 |
|
currMV->y = start_y; |
1427 |
|
|
1428 |
if (!(MotionFlags & PMV_HALFPEL16)) { /* This should NOT be necessary! */ |
if (!(MotionFlags & PMV_HALFPEL16)) { /* This should NOT be necessary! */ |
1429 |
currMV->x = EVEN(currMV->x); |
currMV->x = EVEN(currMV->x); |
1430 |
currMV->y = EVEN(currMV->y); |
currMV->y = EVEN(currMV->y); |
1448 |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, |
1449 |
iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); |
iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); |
1450 |
iMinSAD += |
iMinSAD += |
1451 |
calc_delta_16(currMV->x - pmv[0].x, currMV->y - pmv[0].y, |
calc_delta_16(currMV->x - center_x, currMV->y - center_y, |
1452 |
(uint8_t) iFcode, iQuant); |
(uint8_t) iFcode, iQuant); |
1453 |
|
|
1454 |
if ((iMinSAD < 256) || |
if ((iMinSAD < 256) || |
1585 |
backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */ |
backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */ |
1586 |
|
|
1587 |
|
|
|
// fprintf(stderr,"Entering Diamond %d %d (%d):\n",x,y,iMinSAD); |
|
|
|
|
1588 |
/* default: use best prediction as starting point for one call of PMVfast_MainSearch */ |
/* default: use best prediction as starting point for one call of PMVfast_MainSearch */ |
1589 |
iSAD = |
iSAD = |
1590 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
1591 |
currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx, |
currMV->x, currMV->y, iMinSAD, &newMV, center_x, center_y, |
1592 |
|
min_dx, max_dx, |
1593 |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
1594 |
iQuant, iFound); |
iQuant, iFound); |
1595 |
|
|
1604 |
if (!(MVequal(pmv[0], backupMV))) { |
if (!(MVequal(pmv[0], backupMV))) { |
1605 |
iSAD = |
iSAD = |
1606 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
1607 |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv, |
center_x, center_y, iMinSAD, &newMV, center_x, center_y, |
1608 |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
1609 |
iDiamondSize, iFcode, iQuant, iFound); |
iDiamondSize, iFcode, iQuant, iFound); |
1610 |
|
|
1617 |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
1618 |
iSAD = |
iSAD = |
1619 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
1620 |
iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy, |
iMinSAD, &newMV, center_x, center_y, |
1621 |
max_dy, iEdgedWidth, iDiamondSize, iFcode, |
min_dx, max_dx, min_dy, max_dy, |
1622 |
|
iEdgedWidth, iDiamondSize, iFcode, |
1623 |
iQuant, iFound); |
iQuant, iFound); |
1624 |
|
|
1625 |
if (iSAD < iMinSAD) { |
if (iSAD < iMinSAD) { |
1637 |
if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step |
if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step |
1638 |
iMinSAD = |
iMinSAD = |
1639 |
Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
1640 |
iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy, |
iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy, |
1641 |
iFcode, iQuant, iEdgedWidth); |
iFcode, iQuant, iEdgedWidth); |
1642 |
|
|
|
/*fprintf(stderr,"Chosen for %d %d: %d %d - %d %d\n",x,y,currMV->x,currMV->y,pmv[0].x,pmv[0].y); |
|
|
*/ |
|
1643 |
PMVfast16_Terminate_without_Refine: |
PMVfast16_Terminate_without_Refine: |
1644 |
currPMV->x = currMV->x - pmv[0].x; |
currPMV->x = currMV->x - center_x; |
1645 |
currPMV->y = currMV->y - pmv[0].y; |
currPMV->y = currMV->y - center_y; |
1646 |
return iMinSAD; |
return iMinSAD; |
1647 |
} |
} |
1648 |
|
|
1659 |
const uint8_t * const cur, |
const uint8_t * const cur, |
1660 |
const int x, |
const int x, |
1661 |
const int y, |
const int y, |
1662 |
int32_t startx, |
int32_t start_x, |
1663 |
int32_t starty, |
int32_t start_y, |
1664 |
int32_t iMinSAD, |
int32_t iMinSAD, |
1665 |
VECTOR * const currMV, |
VECTOR * const currMV, |
1666 |
const VECTOR * const pmv, |
const int center_x, |
1667 |
|
const int center_y, |
1668 |
const int32_t min_dx, |
const int32_t min_dx, |
1669 |
const int32_t max_dx, |
const int32_t max_dx, |
1670 |
const int32_t min_dy, |
const int32_t min_dy, |
1678 |
/* Do a diamond search around given starting point, return SAD of best */ |
/* Do a diamond search around given starting point, return SAD of best */ |
1679 |
|
|
1680 |
int32_t iDirection = 0; |
int32_t iDirection = 0; |
1681 |
|
int32_t iDirectionBackup; |
1682 |
int32_t iSAD; |
int32_t iSAD; |
1683 |
VECTOR backupMV; |
VECTOR backupMV; |
1684 |
|
|
1685 |
backupMV.x = startx; |
backupMV.x = start_x; |
1686 |
backupMV.y = starty; |
backupMV.y = start_y; |
1687 |
|
|
1688 |
/* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */ |
/* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */ |
1689 |
|
|
1692 |
CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3); |
CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3); |
1693 |
CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4); |
CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4); |
1694 |
|
|
1695 |
if (iDirection) |
if (iDirection) { |
1696 |
while (!iFound) { |
while (!iFound) { |
1697 |
iFound = 1; |
iFound = 1; |
1698 |
backupMV = *currMV; // since iDirection!=0, this is well defined! |
backupMV = *currMV; // since iDirection!=0, this is well defined! |
1699 |
|
iDirectionBackup = iDirection; |
1700 |
|
|
1701 |
if (iDirection != 2) |
if (iDirectionBackup != 2) |
1702 |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1703 |
backupMV.y, 1); |
backupMV.y, 1); |
1704 |
if (iDirection != 1) |
if (iDirectionBackup != 1) |
1705 |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1706 |
backupMV.y, 2); |
backupMV.y, 2); |
1707 |
if (iDirection != 4) |
if (iDirectionBackup != 4) |
1708 |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, |
1709 |
backupMV.y - iDiamondSize, 3); |
backupMV.y - iDiamondSize, 3); |
1710 |
if (iDirection != 3) |
if (iDirectionBackup != 3) |
1711 |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, |
1712 |
backupMV.y + iDiamondSize, 4); |
backupMV.y + iDiamondSize, 4); |
1713 |
|
} |
1714 |
} else { |
} else { |
1715 |
currMV->x = startx; |
currMV->x = start_x; |
1716 |
currMV->y = starty; |
currMV->y = start_y; |
1717 |
} |
} |
1718 |
return iMinSAD; |
return iMinSAD; |
1719 |
} |
} |
1720 |
|
|
1721 |
|
|
1722 |
|
|
1723 |
|
|
1724 |
|
int32_t |
1725 |
|
Square8_MainSearch(const uint8_t * const pRef, |
1726 |
|
const uint8_t * const pRefH, |
1727 |
|
const uint8_t * const pRefV, |
1728 |
|
const uint8_t * const pRefHV, |
1729 |
|
const uint8_t * const cur, |
1730 |
|
const int x, |
1731 |
|
const int y, |
1732 |
|
int32_t start_x, |
1733 |
|
int32_t start_y, |
1734 |
|
int32_t iMinSAD, |
1735 |
|
VECTOR * const currMV, |
1736 |
|
const int center_x, |
1737 |
|
const int center_y, |
1738 |
|
const int32_t min_dx, |
1739 |
|
const int32_t max_dx, |
1740 |
|
const int32_t min_dy, |
1741 |
|
const int32_t max_dy, |
1742 |
|
const int32_t iEdgedWidth, |
1743 |
|
const int32_t iDiamondSize, |
1744 |
|
const int32_t iFcode, |
1745 |
|
const int32_t iQuant, |
1746 |
|
int iFound) |
1747 |
|
{ |
1748 |
|
/* Do a square search around given starting point, return SAD of best */ |
1749 |
|
|
1750 |
|
int32_t iDirection = 0; |
1751 |
|
int32_t iSAD; |
1752 |
|
VECTOR backupMV; |
1753 |
|
|
1754 |
|
backupMV.x = start_x; |
1755 |
|
backupMV.y = start_y; |
1756 |
|
|
1757 |
|
/* It's one search with full square pattern, and new parts for all following diamonds */ |
1758 |
|
|
1759 |
|
/* new direction are extra, so 1-4 is normal diamond |
1760 |
|
537 |
1761 |
|
1*2 |
1762 |
|
648 |
1763 |
|
*/ |
1764 |
|
|
1765 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1); |
1766 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2); |
1767 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3); |
1768 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4); |
1769 |
|
|
1770 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
1771 |
|
backupMV.y - iDiamondSize, 5); |
1772 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
1773 |
|
backupMV.y + iDiamondSize, 6); |
1774 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
1775 |
|
backupMV.y - iDiamondSize, 7); |
1776 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
1777 |
|
backupMV.y + iDiamondSize, 8); |
1778 |
|
|
1779 |
|
|
1780 |
|
if (iDirection) { |
1781 |
|
while (!iFound) { |
1782 |
|
iFound = 1; |
1783 |
|
backupMV = *currMV; |
1784 |
|
|
1785 |
|
switch (iDirection) { |
1786 |
|
case 1: |
1787 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1788 |
|
backupMV.y, 1); |
1789 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1790 |
|
backupMV.y - iDiamondSize, 5); |
1791 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1792 |
|
backupMV.y - iDiamondSize, 7); |
1793 |
|
break; |
1794 |
|
case 2: |
1795 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
1796 |
|
2); |
1797 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1798 |
|
backupMV.y + iDiamondSize, 6); |
1799 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1800 |
|
backupMV.y + iDiamondSize, 8); |
1801 |
|
break; |
1802 |
|
|
1803 |
|
case 3: |
1804 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
1805 |
|
4); |
1806 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1807 |
|
backupMV.y - iDiamondSize, 7); |
1808 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1809 |
|
backupMV.y + iDiamondSize, 8); |
1810 |
|
break; |
1811 |
|
|
1812 |
|
case 4: |
1813 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
1814 |
|
3); |
1815 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1816 |
|
backupMV.y - iDiamondSize, 5); |
1817 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1818 |
|
backupMV.y + iDiamondSize, 6); |
1819 |
|
break; |
1820 |
|
|
1821 |
|
case 5: |
1822 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y, |
1823 |
|
1); |
1824 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
1825 |
|
3); |
1826 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1827 |
|
backupMV.y - iDiamondSize, 5); |
1828 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1829 |
|
backupMV.y + iDiamondSize, 6); |
1830 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1831 |
|
backupMV.y - iDiamondSize, 7); |
1832 |
|
break; |
1833 |
|
|
1834 |
|
case 6: |
1835 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
1836 |
|
2); |
1837 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
1838 |
|
3); |
1839 |
|
|
1840 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1841 |
|
backupMV.y - iDiamondSize, 5); |
1842 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1843 |
|
backupMV.y + iDiamondSize, 6); |
1844 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1845 |
|
backupMV.y + iDiamondSize, 8); |
1846 |
|
|
1847 |
|
break; |
1848 |
|
|
1849 |
|
case 7: |
1850 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1851 |
|
backupMV.y, 1); |
1852 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
1853 |
|
4); |
1854 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1855 |
|
backupMV.y - iDiamondSize, 5); |
1856 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1857 |
|
backupMV.y - iDiamondSize, 7); |
1858 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1859 |
|
backupMV.y + iDiamondSize, 8); |
1860 |
|
break; |
1861 |
|
|
1862 |
|
case 8: |
1863 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
1864 |
|
2); |
1865 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
1866 |
|
4); |
1867 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1868 |
|
backupMV.y + iDiamondSize, 6); |
1869 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1870 |
|
backupMV.y - iDiamondSize, 7); |
1871 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1872 |
|
backupMV.y + iDiamondSize, 8); |
1873 |
|
break; |
1874 |
|
default: |
1875 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y, |
1876 |
|
1); |
1877 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
1878 |
|
2); |
1879 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
1880 |
|
3); |
1881 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
1882 |
|
4); |
1883 |
|
|
1884 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1885 |
|
backupMV.y - iDiamondSize, 5); |
1886 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1887 |
|
backupMV.y + iDiamondSize, 6); |
1888 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1889 |
|
backupMV.y - iDiamondSize, 7); |
1890 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1891 |
|
backupMV.y + iDiamondSize, 8); |
1892 |
|
break; |
1893 |
|
} |
1894 |
|
} |
1895 |
|
} else { |
1896 |
|
currMV->x = start_x; |
1897 |
|
currMV->y = start_y; |
1898 |
|
} |
1899 |
|
return iMinSAD; |
1900 |
|
} |
1901 |
|
|
1902 |
|
|
1903 |
|
|
1904 |
|
|
1905 |
|
|
1906 |
int32_t |
int32_t |
1907 |
Halfpel8_Refine_c(const uint8_t * const pRef, |
Halfpel8_Refine_c(const uint8_t * const pRef, |
1908 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
1913 |
const int y, |
const int y, |
1914 |
VECTOR * const currMV, |
VECTOR * const currMV, |
1915 |
int32_t iMinSAD, |
int32_t iMinSAD, |
1916 |
const VECTOR * const pmv, |
const int center_x, |
1917 |
|
const int center_y, |
1918 |
const int32_t min_dx, |
const int32_t min_dx, |
1919 |
const int32_t max_dx, |
const int32_t max_dx, |
1920 |
const int32_t min_dy, |
const int32_t min_dy, |
1953 |
const int y, |
const int y, |
1954 |
const int start_x, |
const int start_x, |
1955 |
const int start_y, |
const int start_y, |
1956 |
|
const int center_x, |
1957 |
|
const int center_y, |
1958 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
1959 |
const uint32_t iQuant, |
const uint32_t iQuant, |
1960 |
const uint32_t iFcode, |
const uint32_t iFcode, |
2019 |
threshB = 1024 / 4; |
threshB = 1024 / 4; |
2020 |
|
|
2021 |
} else { |
} else { |
2022 |
threshA = psad[0] / 4; /* good estimate */ |
threshA = psad[0] / 4; /* good estimate? */ |
2023 |
threshB = threshA + 256 / 4; |
threshB = threshA + 256 / 4; |
2024 |
if (threshA < 512 / 4) |
if (threshA < 512 / 4) |
2025 |
threshA = 512 / 4; |
threshA = 512 / 4; |
2041 |
|
|
2042 |
// Prepare for main loop |
// Prepare for main loop |
2043 |
|
|
2044 |
// if (MotionFlags & PMV_USESQUARES8) |
if (MotionFlags & PMV_USESQUARES8) |
2045 |
// MainSearchPtr = Square8_MainSearch; |
MainSearchPtr = Square8_MainSearch; |
2046 |
// else |
else |
2047 |
|
|
2048 |
if (MotionFlags & PMV_ADVANCEDDIAMOND8) |
if (MotionFlags & PMV_ADVANCEDDIAMOND8) |
2049 |
MainSearchPtr = AdvDiamond8_MainSearch; |
MainSearchPtr = AdvDiamond8_MainSearch; |
2058 |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, |
2059 |
iEdgedWidth), iEdgedWidth); |
iEdgedWidth), iEdgedWidth); |
2060 |
iMinSAD += |
iMinSAD += |
2061 |
calc_delta_8(currMV->x - pmv[0].x, currMV->y - pmv[0].y, |
calc_delta_8(currMV->x - center_x, currMV->y - center_y, |
2062 |
(uint8_t) iFcode, iQuant); |
(uint8_t) iFcode, iQuant); |
2063 |
|
|
2064 |
if ((iMinSAD < 256 / 4) || ((MVequal(*currMV, prevMB->mvs[iSubBlock])) |
if ((iMinSAD < 256 / 4) || ((MVequal(*currMV, prevMB->mvs[iSubBlock])) |
2101 |
// the median prediction might be even better than mv16 |
// the median prediction might be even better than mv16 |
2102 |
|
|
2103 |
if (!MVequal(pmv[0], startMV)) |
if (!MVequal(pmv[0], startMV)) |
2104 |
CHECK_MV8_CANDIDATE(pmv[0].x, pmv[0].y); |
CHECK_MV8_CANDIDATE(center_x, center_y); |
2105 |
|
|
2106 |
// (0,0) if needed |
// (0,0) if needed |
2107 |
if (!MVzero(pmv[0])) |
if (!MVzero(pmv[0])) |
2198 |
/* default: use best prediction as starting point for one call of PMVfast_MainSearch */ |
/* default: use best prediction as starting point for one call of PMVfast_MainSearch */ |
2199 |
iSAD = |
iSAD = |
2200 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
2201 |
currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx, |
currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, |
2202 |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
2203 |
iQuant, iFound); |
iQuant, iFound); |
2204 |
|
|
2213 |
if (!(MVequal(pmv[0], backupMV))) { |
if (!(MVequal(pmv[0], backupMV))) { |
2214 |
iSAD = |
iSAD = |
2215 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
2216 |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv, |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y, |
2217 |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
2218 |
iDiamondSize, iFcode, iQuant, iFound); |
iDiamondSize, iFcode, iQuant, iFound); |
2219 |
|
|
2226 |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
2227 |
iSAD = |
iSAD = |
2228 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
2229 |
iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy, |
iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy, |
2230 |
max_dy, iEdgedWidth, iDiamondSize, iFcode, |
max_dy, iEdgedWidth, iDiamondSize, iFcode, |
2231 |
iQuant, iFound); |
iQuant, iFound); |
2232 |
|
|
2245 |
if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step |
if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step |
2246 |
iMinSAD = |
iMinSAD = |
2247 |
Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
2248 |
iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy, |
iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy, |
2249 |
iFcode, iQuant, iEdgedWidth); |
iFcode, iQuant, iEdgedWidth); |
2250 |
|
|
2251 |
|
|
2252 |
PMVfast8_Terminate_without_Refine: |
PMVfast8_Terminate_without_Refine: |
2253 |
currPMV->x = currMV->x - pmv[0].x; |
currPMV->x = currMV->x - center_x; |
2254 |
currPMV->y = currMV->y - pmv[0].y; |
currPMV->y = currMV->y - center_y; |
2255 |
|
|
2256 |
return iMinSAD; |
return iMinSAD; |
2257 |
} |
} |
2264 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
2265 |
const int x, |
const int x, |
2266 |
const int y, |
const int y, |
2267 |
|
const int start_x, |
2268 |
|
const int start_y, |
2269 |
|
const int center_x, |
2270 |
|
const int center_y, |
2271 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
2272 |
const uint32_t iQuant, |
const uint32_t iQuant, |
2273 |
const uint32_t iFcode, |
const uint32_t iFcode, |
2338 |
|
|
2339 |
// Prepare for main loop |
// Prepare for main loop |
2340 |
|
|
2341 |
*currMV = pmv[0]; /* current best := median prediction */ |
currMV->x = start_x; |
2342 |
|
currMV->y = start_y; |
2343 |
|
|
2344 |
if (!(MotionFlags & PMV_HALFPEL16)) { |
if (!(MotionFlags & PMV_HALFPEL16)) { |
2345 |
currMV->x = EVEN(currMV->x); |
currMV->x = EVEN(currMV->x); |
2346 |
currMV->y = EVEN(currMV->y); |
currMV->y = EVEN(currMV->y); |
2362 |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, |
2363 |
iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); |
iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); |
2364 |
iMinSAD += |
iMinSAD += |
2365 |
calc_delta_16(currMV->x - pmv[0].x, currMV->y - pmv[0].y, |
calc_delta_16(currMV->x - center_x, currMV->y - center_y, |
2366 |
(uint8_t) iFcode, iQuant); |
(uint8_t) iFcode, iQuant); |
2367 |
|
|
2368 |
// thresh1 is fixed to 256 |
// thresh1 is fixed to 256 |
2486 |
|
|
2487 |
iSAD = |
iSAD = |
2488 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
2489 |
currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx, |
currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, |
2490 |
min_dy, max_dy, iEdgedWidth, 2, iFcode, iQuant, 0); |
min_dy, max_dy, iEdgedWidth, 2, iFcode, iQuant, 0); |
2491 |
|
|
2492 |
if (iSAD < iMinSAD) { |
if (iSAD < iMinSAD) { |
2501 |
if (!(MVequal(pmv[0], backupMV))) { |
if (!(MVequal(pmv[0], backupMV))) { |
2502 |
iSAD = |
iSAD = |
2503 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
2504 |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv, |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y, |
2505 |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
2506 |
2, iFcode, iQuant, 0); |
2, iFcode, iQuant, 0); |
2507 |
} |
} |
2514 |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
2515 |
iSAD = |
iSAD = |
2516 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
2517 |
iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy, |
iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy, |
2518 |
max_dy, iEdgedWidth, 2, iFcode, iQuant, 0); |
max_dy, iEdgedWidth, 2, iFcode, iQuant, 0); |
2519 |
|
|
2520 |
if (iSAD < iMinSAD) { |
if (iSAD < iMinSAD) { |
2530 |
if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step |
if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step |
2531 |
iMinSAD = |
iMinSAD = |
2532 |
Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
2533 |
iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy, |
iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy, |
2534 |
iFcode, iQuant, iEdgedWidth); |
iFcode, iQuant, iEdgedWidth); |
2535 |
|
|
2536 |
EPZS16_Terminate_without_Refine: |
EPZS16_Terminate_without_Refine: |
2537 |
|
|
2538 |
*oldMB = *prevMB; |
*oldMB = *prevMB; |
2539 |
|
|
2540 |
currPMV->x = currMV->x - pmv[0].x; |
currPMV->x = currMV->x - center_x; |
2541 |
currPMV->y = currMV->y - pmv[0].y; |
currPMV->y = currMV->y - center_y; |
2542 |
return iMinSAD; |
return iMinSAD; |
2543 |
} |
} |
2544 |
|
|
2553 |
const int y, |
const int y, |
2554 |
const int start_x, |
const int start_x, |
2555 |
const int start_y, |
const int start_y, |
2556 |
|
const int center_x, |
2557 |
|
const int center_y, |
2558 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
2559 |
const uint32_t iQuant, |
const uint32_t iQuant, |
2560 |
const uint32_t iFcode, |
const uint32_t iFcode, |
2609 |
max_dy = EVEN(max_dy); |
max_dy = EVEN(max_dy); |
2610 |
} |
} |
2611 |
/* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */ |
/* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */ |
2612 |
//bPredEq = get_pmvdata(pMBs, x >> 1, y >> 1, iWcount, iSubBlock, pmv, psad); |
//bPredEq = get_pmvdata(pMBs, x >> 1, y >> 1, iWcount, iSubBlock, pmv[0].x, pmv[0].y, psad); |
2613 |
bPredEq = get_pmvdata2(pMBs, iWcount, 0, x >> 1, y >> 1, iSubBlock, pmv, psad); |
bPredEq = get_pmvdata2(pMBs, iWcount, 0, x >> 1, y >> 1, iSubBlock, pmv, psad); |
2614 |
|
|
2615 |
|
|
2645 |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, |
2646 |
iEdgedWidth), iEdgedWidth); |
iEdgedWidth), iEdgedWidth); |
2647 |
iMinSAD += |
iMinSAD += |
2648 |
calc_delta_8(currMV->x - pmv[0].x, currMV->y - pmv[0].y, |
calc_delta_8(currMV->x - center_x, currMV->y - center_y, |
2649 |
(uint8_t) iFcode, iQuant); |
(uint8_t) iFcode, iQuant); |
2650 |
|
|
2651 |
|
|
2721 |
|
|
2722 |
// there is no EPZS^2 for inter4v at the moment |
// there is no EPZS^2 for inter4v at the moment |
2723 |
|
|
2724 |
// if (MotionFlags & PMV_USESQUARES8) |
if (MotionFlags & PMV_USESQUARES8) |
2725 |
// MainSearchPtr = Square8_MainSearch; |
MainSearchPtr = Square8_MainSearch; |
2726 |
// else |
else |
2727 |
|
|
2728 |
if (MotionFlags & PMV_ADVANCEDDIAMOND8) |
if (MotionFlags & PMV_ADVANCEDDIAMOND8) |
2729 |
MainSearchPtr = AdvDiamond8_MainSearch; |
MainSearchPtr = AdvDiamond8_MainSearch; |
2732 |
|
|
2733 |
iSAD = |
iSAD = |
2734 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
2735 |
currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx, |
currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, |
2736 |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
2737 |
iQuant, 0); |
iQuant, 0); |
2738 |
|
|
2748 |
if (!(MVequal(pmv[0], backupMV))) { |
if (!(MVequal(pmv[0], backupMV))) { |
2749 |
iSAD = |
iSAD = |
2750 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
2751 |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv, |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y, |
2752 |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
2753 |
iDiamondSize, iFcode, iQuant, 0); |
iDiamondSize, iFcode, iQuant, 0); |
2754 |
|
|
2761 |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
2762 |
iSAD = |
iSAD = |
2763 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
2764 |
iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy, |
iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy, |
2765 |
max_dy, iEdgedWidth, iDiamondSize, iFcode, |
max_dy, iEdgedWidth, iDiamondSize, iFcode, |
2766 |
iQuant, 0); |
iQuant, 0); |
2767 |
|
|
2778 |
if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step |
if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step |
2779 |
iMinSAD = |
iMinSAD = |
2780 |
Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
2781 |
iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy, |
iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy, |
2782 |
iFcode, iQuant, iEdgedWidth); |
iFcode, iQuant, iEdgedWidth); |
2783 |
|
|
2784 |
EPZS8_Terminate_without_Refine: |
EPZS8_Terminate_without_Refine: |
2785 |
|
|
2786 |
currPMV->x = currMV->x - pmv[0].x; |
currPMV->x = currMV->x - center_x; |
2787 |
currPMV->y = currMV->y - pmv[0].y; |
currPMV->y = currMV->y - center_y; |
2788 |
return iMinSAD; |
return iMinSAD; |
2789 |
} |
} |
2790 |
|
|
2798 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
2799 |
const int x, |
const int x, |
2800 |
const int y, |
const int y, |
2801 |
|
const int start_x, /* start should be most likely vector */ |
2802 |
|
const int start_y, |
2803 |
|
const int center_x, /* center is from where length of MVs is measured */ |
2804 |
|
const int center_y, |
2805 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
2806 |
const uint32_t iQuant, |
const uint32_t iQuant, |
2807 |
const uint32_t iFcode, |
const uint32_t iFcode, |
2829 |
int32_t iFound; |
int32_t iFound; |
2830 |
|
|
2831 |
VECTOR newMV; |
VECTOR newMV; |
2832 |
VECTOR backupMV; /* just for PMVFAST */ |
VECTOR backupMV; |
2833 |
|
|
2834 |
VECTOR pmv[4]; |
VECTOR pmv[4]; |
2835 |
int32_t psad[4]; |
int32_t psad[4]; |
2843 |
int32_t bPredEq; |
int32_t bPredEq; |
2844 |
int32_t iMinSAD, iSAD; |
int32_t iMinSAD, iSAD; |
2845 |
|
|
2846 |
|
|
2847 |
/* Get maximum range */ |
/* Get maximum range */ |
2848 |
get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight, |
get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight, |
2849 |
iFcode); |
iFcode); |
2859 |
*currMV = pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV; |
*currMV = pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV; |
2860 |
|
|
2861 |
} else { |
} else { |
2862 |
|
|
2863 |
|
bPredEq = get_ipmvdata(pMBs, iWcount, 0, x, y, 0, pmv, psad); |
2864 |
|
|
2865 |
threshA = psad[0]; |
threshA = psad[0]; |
2866 |
threshB = threshA + 256; |
threshB = threshA + 256; |
2867 |
if (threshA < 512) |
if (threshA < 512) |
2871 |
if (threshB > 1792) |
if (threshB > 1792) |
2872 |
threshB = 1792; |
threshB = 1792; |
2873 |
|
|
|
bPredEq = get_ipmvdata(pMBs, iWcount, 0, x, y, 0, pmv, psad); |
|
2874 |
*currMV = pmv[0]; /* current best := prediction */ |
*currMV = pmv[0]; /* current best := prediction */ |
2875 |
} |
} |
2876 |
|
|
2901 |
get_iref_mv(pRef, x, y, 16, currMV, |
get_iref_mv(pRef, x, y, 16, currMV, |
2902 |
iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); |
iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); |
2903 |
iMinSAD += |
iMinSAD += |
2904 |
calc_delta_16(currMV->x - pmv[0].x, currMV->y - pmv[0].y, |
calc_delta_16(currMV->x - center_x, currMV->y - center_y, |
2905 |
(uint8_t) iFcode, iQuant); |
(uint8_t) iFcode, iQuant); |
2906 |
|
|
2907 |
if ((iMinSAD < 256) || |
if ((iMinSAD < 256) || |
3020 |
/* default: use best prediction as starting point for one call of PMVfast_MainSearch */ |
/* default: use best prediction as starting point for one call of PMVfast_MainSearch */ |
3021 |
iSAD = |
iSAD = |
3022 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
3023 |
currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx, |
currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, |
3024 |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
3025 |
iQuant, iFound); |
iQuant, iFound); |
3026 |
|
|
3035 |
if (!(MVequal(pmv[0], backupMV))) { |
if (!(MVequal(pmv[0], backupMV))) { |
3036 |
iSAD = |
iSAD = |
3037 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
3038 |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv, |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y, |
3039 |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
3040 |
iDiamondSize, iFcode, iQuant, iFound); |
iDiamondSize, iFcode, iQuant, iFound); |
3041 |
|
|
3048 |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
3049 |
iSAD = |
iSAD = |
3050 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
3051 |
iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy, |
iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy, |
3052 |
max_dy, iEdgedWidth, iDiamondSize, iFcode, |
max_dy, iEdgedWidth, iDiamondSize, iFcode, |
3053 |
iQuant, iFound); |
iQuant, iFound); |
3054 |
|
|
3071 |
if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step |
if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step |
3072 |
iMinSAD = |
iMinSAD = |
3073 |
Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
3074 |
iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy, |
iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy, |
3075 |
iFcode, iQuant, iEdgedWidth); |
iFcode, iQuant, iEdgedWidth); |
3076 |
|
|
3077 |
pmv[0] = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); // get _REAL_ prediction (halfpel possible) |
pmv[0] = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); // get _REAL_ prediction (halfpel possible) |
3078 |
|
|
3079 |
PMVfastInt16_Terminate_without_Refine: |
PMVfastInt16_Terminate_without_Refine: |
3080 |
currPMV->x = currMV->x - pmv[0].x; |
currPMV->x = currMV->x - center_x; |
3081 |
currPMV->y = currMV->y - pmv[0].y; |
currPMV->y = currMV->y - center_y; |
3082 |
return iMinSAD; |
return iMinSAD; |
3083 |
} |
} |
3084 |
|
|
3086 |
|
|
3087 |
/* *********************************************************** |
/* *********************************************************** |
3088 |
bvop motion estimation |
bvop motion estimation |
|
// TODO: need to incorporate prediction here (eg. sad += calc_delta_16) |
|
3089 |
***************************************************************/ |
***************************************************************/ |
3090 |
|
|
|
|
|
3091 |
void |
void |
3092 |
MotionEstimationBVOP(MBParam * const pParam, |
MotionEstimationBVOP(MBParam * const pParam, |
3093 |
FRAMEINFO * const frame, |
FRAMEINFO * const frame, |
3094 |
// const float scalefactor, |
const int32_t time_bp, |
3095 |
|
const int32_t time_pp, |
3096 |
// forward (past) reference |
// forward (past) reference |
3097 |
const MACROBLOCK * const f_mbs, |
const MACROBLOCK * const f_mbs, |
3098 |
const IMAGE * const f_ref, |
const IMAGE * const f_ref, |
3110 |
const int mb_height = pParam->mb_height; |
const int mb_height = pParam->mb_height; |
3111 |
const int edged_width = pParam->edged_width; |
const int edged_width = pParam->edged_width; |
3112 |
|
|
3113 |
const float scalefactor=0.666; |
const int32_t iWidth = pParam->width; |
3114 |
int i, j; |
const int32_t iHeight = pParam->height; |
3115 |
|
|
3116 |
|
int i, j, k; |
3117 |
|
|
3118 |
// static const VECTOR zeroMV={0,0}; |
static const VECTOR zeroMV={0,0}; |
3119 |
|
|
3120 |
int f_sad16; /* forward (as usual) search */ |
int f_sad16; /* forward (as usual) search */ |
3121 |
int b_sad16; /* backward (only in b-frames) search */ |
int b_sad16; /* backward (only in b-frames) search */ |
3122 |
int i_sad16; /* interpolated (both direction, b-frames only) */ |
int i_sad16; /* interpolated (both direction, b-frames only) */ |
3123 |
int d_sad16; /* direct mode (assume linear motion) */ |
int d_sad16; /* direct mode (assume almost linear motion) */ |
3124 |
|
|
3125 |
int best_sad; |
int best_sad; |
3126 |
|
|
3127 |
|
VECTOR f_predMV, b_predMV; /* there is no prediction for direct mode*/ |
3128 |
|
VECTOR f_interpolMV, b_interpolMV; |
3129 |
VECTOR pmv_dontcare; |
VECTOR pmv_dontcare; |
3130 |
|
|
3131 |
|
int min_dx, max_dx, min_dy, max_dy; |
3132 |
|
int f_min_dx, f_max_dx, f_min_dy, f_max_dy; |
3133 |
|
int b_min_dx, b_max_dx, b_min_dy, b_max_dy; |
3134 |
|
|
3135 |
int f_count=0; |
int f_count=0; |
3136 |
int b_count=0; |
int b_count=0; |
3137 |
int i_count=0; |
int i_count=0; |
3138 |
int d_count=0; |
int d_count=0; |
|
int dnv_count=0; |
|
|
int s_count=0; |
|
3139 |
|
|
3140 |
|
const int64_t TRB = (int32_t)time_pp - (int32_t)time_bp; |
3141 |
|
const int64_t TRD = (int32_t)time_pp; |
3142 |
|
|
3143 |
|
// fprintf(stderr,"TRB = %lld TRD = %lld time_bp =%d time_pp =%d\n\n",TRB,TRD,time_bp,time_pp); |
3144 |
// note: i==horizontal, j==vertical |
// note: i==horizontal, j==vertical |
3145 |
for (j = 0; j < mb_height; j++) { |
for (j = 0; j < mb_height; j++) { |
3146 |
|
|
3147 |
|
f_predMV = zeroMV; /* prediction is reset at left boundary */ |
3148 |
|
b_predMV = zeroMV; |
3149 |
|
|
3150 |
for (i = 0; i < mb_width; i++) { |
for (i = 0; i < mb_width; i++) { |
3151 |
MACROBLOCK *mb = &frame->mbs[i + j * mb_width]; |
MACROBLOCK *mb = &frame->mbs[i + j * mb_width]; |
3152 |
const MACROBLOCK *f_mb = &f_mbs[i + j * mb_width]; |
const MACROBLOCK *f_mb = &f_mbs[i + j * mb_width]; |
3153 |
const MACROBLOCK *b_mb = &b_mbs[i + j * mb_width]; |
const MACROBLOCK *b_mb = &b_mbs[i + j * mb_width]; |
3154 |
|
|
3155 |
VECTOR directMV=b_mb->mvs[0]; /* direct mode: presume linear motion */ |
mb->deltamv=zeroMV; |
3156 |
|
|
3157 |
/* special case, if collocated block is SKIPed: encoding is forward(0,0) */ |
/* special case, if collocated block is SKIPed: encoding is forward (0,0), cpb=0 without further ado */ |
3158 |
|
|
3159 |
if (b_mb->mode == MODE_INTER && b_mb->cbp == 0 && |
if (b_mb->mode == MODE_INTER && b_mb->cbp == 0 && |
3160 |
b_mb->mvs[0].x == 0 && b_mb->mvs[0].y == 0) { |
b_mb->mvs[0].x == 0 && b_mb->mvs[0].y == 0) { |
3161 |
mb->mode = MODE_NOT_CODED; |
mb->mode = MODE_NOT_CODED; |
3162 |
mb->mvs[0].x = 0; |
mb->b_mvs[0] = mb->mvs[0] = zeroMV; |
|
mb->mvs[0].y = 0; |
|
|
mb->b_mvs[0].x = 0; |
|
|
mb->b_mvs[0].y = 0; |
|
3163 |
continue; |
continue; |
3164 |
} |
} |
3165 |
|
|
3166 |
/* candidates for best MV assumes linear motion */ |
if (b_mb->mode == MODE_INTER4V) |
3167 |
/* next vector is linearly scaled (factor depends on distance to that frame) */ |
{ |
3168 |
|
d_sad16 = 0; |
3169 |
mb->mvs[0].x = (int)(directMV.x * scalefactor + 0.75); |
/* same method of scaling as in decoder.c, so we copy from there */ |
3170 |
mb->mvs[0].y = (int)(directMV.y * scalefactor + 0.75); |
for (k = 0; k < 4; k++) { |
3171 |
|
|
3172 |
|
mb->directmv[k] = b_mb->mvs[k]; |
3173 |
|
|
3174 |
|
mb->mvs[k].x = (int32_t) ((TRB * mb->directmv[k].x) / TRD + mb->deltamv.x); |
3175 |
|
mb->b_mvs[k].x = (int32_t) ((mb->deltamv.x == 0) |
3176 |
|
? ((TRB - TRD) * mb->directmv[k].x) / TRD |
3177 |
|
: mb->mvs[k].x - mb->directmv[k].x); |
3178 |
|
|
3179 |
|
mb->mvs[k].y = (int32_t) ((TRB * mb->directmv[k].y) / TRD + mb->deltamv.y); |
3180 |
|
mb->b_mvs[k].y = (int32_t) ((mb->deltamv.y == 0) |
3181 |
|
? ((TRB - TRD) * mb->directmv[k].y) / TRD |
3182 |
|
: mb->mvs[k].y - mb->directmv[k].y); |
3183 |
|
|
3184 |
mb->b_mvs[0].x = (int)(directMV.x * (scalefactor-1.) + 0.75); /* opposite direction! */ |
d_sad16 += |
3185 |
mb->b_mvs[0].y = (int)(directMV.y * (scalefactor-1.) + 0.75); |
sad8bi(frame->image.y + (2*i+(k&1))*8 + (2*j+(k>>1))*8*edged_width, |
3186 |
|
get_ref_mv(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
3187 |
|
(2*i+(k&1)), (2*j+(k>>1)), 8, &mb->mvs[k], edged_width), |
3188 |
|
get_ref_mv(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
3189 |
|
(2*i+(k&1)), (2*j+(k>>1)), 8, &mb->b_mvs[k], edged_width), |
3190 |
|
edged_width); |
3191 |
|
} |
3192 |
|
} |
3193 |
|
else |
3194 |
|
{ |
3195 |
|
mb->directmv[3] = mb->directmv[2] = mb->directmv[1] = |
3196 |
|
mb->directmv[0] = b_mb->mvs[0]; |
3197 |
|
|
3198 |
DEBUG2("last: ",f_mb->mvs[0].x,f_mb->mvs[0].y); |
mb->mvs[0].x = (int32_t) ((TRB * mb->directmv[0].x) / TRD + mb->deltamv.x); |
3199 |
DEBUG2("next: ",b_mb->mvs[0].x,b_mb->mvs[0].y); |
mb->b_mvs[0].x = (int32_t) ((mb->deltamv.x == 0) |
3200 |
DEBUG2("directF: ",mb->mvs[0].x,mb->mvs[0].y); |
? ((TRB - TRD) * mb->directmv[0].x) / TRD |
3201 |
DEBUG2("directB: ",mb->b_mvs[0].x,mb->b_mvs[0].y); |
: mb->mvs[0].x - mb->directmv[0].x); |
3202 |
|
|
3203 |
|
mb->mvs[0].y = (int32_t) ((TRB * mb->directmv[0].y) / TRD + mb->deltamv.y); |
3204 |
|
mb->b_mvs[0].y = (int32_t) ((mb->directmv[0].y == 0) |
3205 |
|
? ((TRB - TRD) * mb->directmv[0].y) / TRD |
3206 |
|
: mb->mvs[0].y - mb->directmv[0].y); |
3207 |
|
|
3208 |
d_sad16 = |
d_sad16 = sad16bi(frame->image.y + i * 16 + j * 16 * edged_width, |
|
sad16bi(frame->image.y + i * 16 + j * 16 * edged_width, |
|
3209 |
get_ref_mv(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
get_ref_mv(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
3210 |
i, j, 16, &mb->mvs[0], edged_width), |
i, j, 16, &mb->mvs[0], edged_width), |
3211 |
get_ref_mv(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
get_ref_mv(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
3212 |
i, j, 16, &mb->b_mvs[0], edged_width), |
i, j, 16, &mb->b_mvs[0], edged_width), |
3213 |
edged_width); |
edged_width); |
3214 |
|
|
3215 |
|
} |
3216 |
|
d_sad16 += calc_delta_16(mb->deltamv.x, mb->deltamv.y, 1, frame->quant); |
3217 |
|
|
3218 |
// forward search |
// forward search |
3219 |
f_sad16 = SEARCH16(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
f_sad16 = SEARCH16(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
3220 |
&frame->image, i, j, frame->motion_flags, |
&frame->image, i, j, |
3221 |
|
mb->mvs[0].x, mb->mvs[0].y, /* start point f_directMV */ |
3222 |
|
f_predMV.x, f_predMV.y, /* center is f-prediction */ |
3223 |
|
frame->motion_flags, |
3224 |
frame->quant, frame->fcode, pParam, |
frame->quant, frame->fcode, pParam, |
3225 |
f_mbs, f_mbs, /* todo */ |
f_mbs, f_mbs, |
3226 |
&mb->mvs[0], &pmv_dontcare); // ignore pmv (why?) |
&mb->mvs[0], &pmv_dontcare); |
3227 |
|
|
3228 |
|
|
3229 |
// backward search |
// backward search |
3230 |
b_sad16 = SEARCH16(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
b_sad16 = SEARCH16(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
3231 |
&frame->image, i, j, frame->motion_flags, |
&frame->image, i, j, |
3232 |
|
mb->b_mvs[0].x, mb->b_mvs[0].y, /* start point b_directMV */ |
3233 |
|
b_predMV.x, b_predMV.y, /* center is b-prediction */ |
3234 |
|
frame->motion_flags, |
3235 |
frame->quant, frame->bcode, pParam, |
frame->quant, frame->bcode, pParam, |
3236 |
b_mbs, b_mbs, /* todo */ |
b_mbs, b_mbs, |
3237 |
&mb->b_mvs[0], &pmv_dontcare); // ignore pmv |
&mb->b_mvs[0], &pmv_dontcare); |
3238 |
|
|
3239 |
i_sad16 = |
i_sad16 = |
3240 |
sad16bi(frame->image.y + i * 16 + j * 16 * edged_width, |
sad16bi(frame->image.y + i * 16 + j * 16 * edged_width, |
3241 |
get_ref_mv(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
get_ref_mv(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
3242 |
i, j, 16, &mb->mvs[0], edged_width), |
i, j, 16, &mb->mvs[0], edged_width), |
3243 |
get_ref(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
get_ref_mv(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
3244 |
i, j, 16, -mb->b_mvs[0].x, -mb->b_mvs[0].y, edged_width), |
i, j, 16, &mb->b_mvs[0], edged_width), |
3245 |
edged_width); |
edged_width); |
3246 |
|
i_sad16 += calc_delta_16(mb->mvs[0].x-f_predMV.x, mb->mvs[0].y-f_predMV.y, |
3247 |
|
frame->fcode, frame->quant); |
3248 |
|
i_sad16 += calc_delta_16(mb->b_mvs[0].x-b_predMV.x, mb->b_mvs[0].y-b_predMV.y, |
3249 |
|
frame->bcode, frame->quant); |
3250 |
|
|
3251 |
|
get_range(&f_min_dx, &f_max_dx, &f_min_dy, &f_max_dy, i, j, 16, iWidth, iHeight, |
3252 |
|
frame->fcode); |
3253 |
|
get_range(&b_min_dx, &b_max_dx, &b_min_dy, &b_max_dy, i, j, 16, iWidth, iHeight, |
3254 |
|
frame->bcode); |
3255 |
|
|
3256 |
|
/* Interpolated MC motion vector search, this is tedious and more complicated because there are |
3257 |
|
two values for everything, always one for backward and one for forward ME. Still, we don't gain |
3258 |
|
much from this search, maybe it should simply be skipped and simply current i_sad16 value used |
3259 |
|
as "optimal". */ |
3260 |
|
|
3261 |
|
i_sad16 = Diamond16_InterpolMainSearch( |
3262 |
|
f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
3263 |
|
frame->image.y + i * 16 + j * 16 * edged_width, |
3264 |
|
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
3265 |
|
i, j, |
3266 |
|
mb->mvs[0].x, mb->mvs[0].y, |
3267 |
|
mb->b_mvs[0].x, mb->b_mvs[0].y, |
3268 |
|
i_sad16, |
3269 |
|
&f_interpolMV, &b_interpolMV, |
3270 |
|
f_predMV.x, f_predMV.y, b_predMV.x, b_predMV.y, |
3271 |
|
f_min_dx, f_max_dx, f_min_dy, f_max_dy, |
3272 |
|
b_min_dx, b_max_dx, b_min_dy, b_max_dy, |
3273 |
|
edged_width, 2, |
3274 |
|
frame->fcode, frame->bcode,frame->quant,0); |
3275 |
|
|
3276 |
|
i_sad16 = Diamond16_InterpolMainSearch( |
3277 |
|
f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
3278 |
|
frame->image.y + i * 16 + j * 16 * edged_width, |
3279 |
|
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
3280 |
|
i, j, |
3281 |
|
f_interpolMV.x, f_interpolMV.y, |
3282 |
|
b_interpolMV.x, b_interpolMV.y, |
3283 |
|
i_sad16, |
3284 |
|
&f_interpolMV, &b_interpolMV, |
3285 |
|
f_predMV.x, f_predMV.y, b_predMV.x, b_predMV.y, |
3286 |
|
f_min_dx, f_max_dx, f_min_dy, f_max_dy, |
3287 |
|
b_min_dx, b_max_dx, b_min_dy, b_max_dy, |
3288 |
|
edged_width, 1, |
3289 |
|
frame->fcode, frame->bcode,frame->quant,0); // equiv to halfpel refine |
3290 |
|
|
3291 |
|
|
3292 |
|
/* DIRECT MODE DELTA VECTOR SEARCH. |
3293 |
|
This has to be made more effective, but at the moment I'm happy it's running at all */ |
3294 |
|
|
3295 |
|
/* There are two range restrictions for direct mode: deltaMV is limited to [-32,31] in halfpel units, and |
3296 |
|
absolute vector must not lie outside of image dimensions. Constraint one is dealt with by CHECK_MV16_DIRECT |
3297 |
|
and for constraint two we need distance to boundary. This is done by get_range very large fcode (hack!) */ |
3298 |
|
|
3299 |
|
get_range(&min_dx, &max_dx, &min_dy, &max_dy, i, j, 16, iWidth, iHeight, 19); |
3300 |
|
|
3301 |
|
d_sad16 = Diamond16_DirectMainSearch( |
3302 |
|
f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
3303 |
|
frame->image.y + i*16 + j*16*edged_width, |
3304 |
|
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
3305 |
|
i, j, |
3306 |
|
TRB,TRD, |
3307 |
|
0,0, |
3308 |
|
d_sad16, |
3309 |
|
&mb->deltamv, |
3310 |
|
mb->directmv, // this has to be pre-initialized with b_mb->mvs[] |
3311 |
|
min_dx, max_dx, min_dy, max_dy, |
3312 |
|
edged_width, 2, frame->quant, 0); |
3313 |
|
|
3314 |
|
d_sad16 = Diamond16_DirectMainSearch( |
3315 |
|
f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
3316 |
|
frame->image.y + i*16 + j*16*edged_width, |
3317 |
|
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
3318 |
|
i, j, |
3319 |
|
TRB,TRD, |
3320 |
|
mb->deltamv.x, mb->deltamv.y, |
3321 |
|
d_sad16, |
3322 |
|
&mb->deltamv, |
3323 |
|
mb->directmv, // this has to be pre-initialized with b_mb->mvs[] |
3324 |
|
min_dx, max_dx, min_dy, max_dy, |
3325 |
|
edged_width, 1, frame->quant, 0); // equiv to halfpel refine |
3326 |
|
|
|
// TODO: direct search |
|
|
// predictor + range of [-32,32] |
|
|
|
|
|
DEBUG2("f_MV: ",mb->mvs[0].x,mb->mvs[0].y); |
|
|
DEBUG2("b_MV: ",mb->b_mvs[0].x,mb->b_mvs[0].y); |
|
3327 |
|
|
3328 |
/* fprintf(stderr,"f_sad16 = %d, b_sad16 = %d, i_sad16 = %d, d_sad16 = %d\n", |
// i_sad16 = 65535; /* remove the comment to disable any of the MODEs */ |
3329 |
f_sad16,b_sad16,i_sad16,d_sad16); |
// f_sad16 = 65535; |
|
*/ |
|
|
|
|
|
// d_sad16 -= 50; |
|
|
// d_sad16 = 65535; |
|
|
// i_sad16 = 65535; |
|
3330 |
// b_sad16 = 65535; |
// b_sad16 = 65535; |
3331 |
|
// d_sad16 = 65535; |
3332 |
|
|
3333 |
if (f_sad16 < b_sad16) { |
if (f_sad16 < b_sad16) { |
3334 |
best_sad = f_sad16; |
best_sad = f_sad16; |
3344 |
} |
} |
3345 |
|
|
3346 |
if (d_sad16 < best_sad) { |
if (d_sad16 < best_sad) { |
3347 |
|
|
3348 |
|
if (b_mb->mode == MODE_INTER4V) |
3349 |
|
{ |
3350 |
|
|
3351 |
|
/* how to calc vectors is defined in standard. mvs[] and b_mvs[] are only for motion compensation */ |
3352 |
|
/* for the bitstream, the value mb->deltamv is read directly */ |
3353 |
|
|
3354 |
|
for (k = 0; k < 4; k++) { |
3355 |
|
|
3356 |
|
mb->mvs[k].x = (int32_t) ((TRB * mb->directmv[k].x) / TRD + mb->deltamv.x); |
3357 |
|
mb->b_mvs[k].x = (int32_t) ((mb->deltamv.x == 0) |
3358 |
|
? ((TRB - TRD) * mb->directmv[k].x) / TRD |
3359 |
|
: mb->mvs[k].x - mb->directmv[k].x); |
3360 |
|
|
3361 |
|
mb->mvs[k].y = (int32_t) ((TRB * mb->directmv[k].y) / TRD + mb->deltamv.y); |
3362 |
|
mb->b_mvs[k].y = (int32_t) ((mb->deltamv.y == 0) |
3363 |
|
? ((TRB - TRD) * mb->directmv[k].y) / TRD |
3364 |
|
: mb->mvs[k].y - mb->directmv[k].y); |
3365 |
|
} |
3366 |
|
} |
3367 |
|
else |
3368 |
|
{ |
3369 |
|
mb->mvs[0].x = (int32_t) ((TRB * mb->directmv[0].x) / TRD + mb->deltamv.x); |
3370 |
|
|
3371 |
|
mb->b_mvs[0].x = (int32_t) ((mb->deltamv.x == 0) |
3372 |
|
? ((TRB - TRD) * mb->directmv[0].x) / TRD |
3373 |
|
: mb->mvs[0].x - mb->directmv[0].x); |
3374 |
|
|
3375 |
|
mb->mvs[0].y = (int32_t) ((TRB * mb->directmv[0].y) / TRD + mb->deltamv.y); |
3376 |
|
|
3377 |
|
mb->b_mvs[0].y = (int32_t) ((mb->deltamv.y == 0) |
3378 |
|
? ((TRB - TRD) * mb->directmv[0].y) / TRD |
3379 |
|
: mb->mvs[0].y - mb->directmv[0].y); |
3380 |
|
|
3381 |
|
mb->mvs[3] = mb->mvs[2] = mb->mvs[1] = mb->mvs[0]; |
3382 |
|
mb->b_mvs[3] = mb->b_mvs[2] = mb->b_mvs[1] = mb->b_mvs[0]; |
3383 |
|
} |
3384 |
|
|
3385 |
best_sad = d_sad16; |
best_sad = d_sad16; |
3386 |
mb->mode = MODE_DIRECT_NONE_MV; |
mb->mode = MODE_DIRECT; |
3387 |
} |
} |
3388 |
|
|
3389 |
switch (mb->mode) |
switch (mb->mode) |
3390 |
{ |
{ |
3391 |
case MODE_FORWARD: |
case MODE_FORWARD: |
3392 |
f_count++; break; |
f_count++; |
3393 |
|
f_predMV = mb->mvs[0]; |
3394 |
|
break; |
3395 |
case MODE_BACKWARD: |
case MODE_BACKWARD: |
3396 |
b_count++; break; |
b_count++; |
3397 |
|
b_predMV = mb->b_mvs[0]; |
3398 |
|
|
3399 |
|
break; |
3400 |
case MODE_INTERPOLATE: |
case MODE_INTERPOLATE: |
3401 |
i_count++; break; |
i_count++; |
3402 |
|
mb->mvs[0] = f_interpolMV; |
3403 |
|
mb->b_mvs[0] = b_interpolMV; |
3404 |
|
f_predMV = mb->mvs[0]; |
3405 |
|
b_predMV = mb->b_mvs[0]; |
3406 |
|
break; |
3407 |
case MODE_DIRECT: |
case MODE_DIRECT: |
3408 |
d_count++; break; |
d_count++; |
3409 |
case MODE_DIRECT_NONE_MV: |
break; |
|
dnv_count++; break; |
|
3410 |
default: |
default: |
3411 |
s_count++; break; |
break; |
3412 |
} |
} |
3413 |
|
|
3414 |
} |
} |
3415 |
} |
} |
3416 |
|
|
3417 |
#ifdef _DEBUG_BFRAME_STAT |
#ifdef _DEBUG_BFRAME_STAT |
3418 |
fprintf(stderr,"B-Stat: F: %04d B: %04d I: %04d D0: %04d D: %04d S: %04d\n", |
fprintf(stderr,"B-Stat: F: %04d B: %04d I: %04d D: %04d\n", |
3419 |
f_count,b_count,i_count,dnv_count,d_count,s_count); |
f_count,b_count,i_count,d_count); |
3420 |
#endif |
#endif |
3421 |
|
|
3422 |
} |
} |