1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* Modifications: |
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - Motion Estimation module - |
5 |
* |
* |
6 |
* 01.05.2002 updated MotionEstimationBVOP |
* Copyright(C) 2002 Christoph Lampert <gruel@web.de> |
7 |
* 25.04.2002 partial prevMB conversion |
* 2002 Michael Militzer <michael@xvid.org> |
|
* 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. |
|
8 |
* |
* |
9 |
* Michael Militzer <isibaar@videocoding.de> |
* This program is an implementation of a part of one or more MPEG-4 |
10 |
|
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
11 |
|
* to use this software module in hardware or software products are |
12 |
|
* advised that its use may infringe existing patents or copyrights, and |
13 |
|
* any such use would be at such party's own risk. The original |
14 |
|
* developer of this software module and his/her company, and subsequent |
15 |
|
* editors and their companies, will have no liability for use of this |
16 |
|
* software or modifications or derivatives thereof. |
17 |
* |
* |
18 |
**************************************************************************/ |
* This program is free software; you can redistribute it and/or modify |
19 |
|
* it under the terms of the GNU General Public License as published by |
20 |
|
* the Free Software Foundation; either version 2 of the License, or |
21 |
|
* (at your option) any later version. |
22 |
|
* |
23 |
|
* This program is distributed in the hope that it will be useful, |
24 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
25 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
26 |
|
* GNU General Public License for more details. |
27 |
|
* |
28 |
|
* You should have received a copy of the GNU General Public License |
29 |
|
* along with this program; if not, write to the Free Software |
30 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
31 |
|
* |
32 |
|
*************************************************************************/ |
33 |
|
|
34 |
#include <assert.h> |
#include <assert.h> |
35 |
#include <stdio.h> |
#include <stdio.h> |
43 |
#include "motion.h" |
#include "motion.h" |
44 |
#include "sad.h" |
#include "sad.h" |
45 |
|
|
|
// very large value |
|
|
#define MV_MAX_ERROR (4096 * 256) |
|
|
|
|
|
// stop search if sdelta < THRESHOLD |
|
|
#define MV16_THRESHOLD 192 |
|
|
#define MV8_THRESHOLD 56 |
|
|
|
|
|
#define NEIGH_MOVE_THRESH 0 |
|
|
// how much a block's MV must differ from his neighbour |
|
|
// to be search for INTER4V. The more, the faster... |
|
|
|
|
|
/* sad16(0,0) bias; mpeg4 spec suggests nb/2+1 */ |
|
|
/* nb = vop pixels * 2^(bpp-8) */ |
|
|
#define MV16_00_BIAS (128+1) |
|
|
#define MV8_00_BIAS (0) |
|
|
|
|
|
/* INTER bias for INTER/INTRA decision; mpeg4 spec suggests 2*nb */ |
|
|
#define MV16_INTER_BIAS 512 |
|
|
|
|
|
/* Parameters which control inter/inter4v decision */ |
|
|
#define IMV16X16 5 |
|
|
|
|
|
/* vector map (vlc delta size) smoother parameters */ |
|
|
#define NEIGH_TEND_16X16 2 |
|
|
#define NEIGH_TEND_8X8 2 |
|
|
|
|
|
// fast ((A)/2)*2 |
|
|
#define EVEN(A) (((A)<0?(A)+1:(A)) & ~1) |
|
|
|
|
|
#define MVzero(A) ( ((A).x)==(0) && ((A).y)==(0) ) |
|
|
#define MVequal(A,B) ( ((A).x)==((B).x) && ((A).y)==((B).y) ) |
|
|
|
|
|
int32_t PMVfastSearch16(const uint8_t * const pRef, |
|
|
const uint8_t * const pRefH, |
|
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const IMAGE * const pCur, |
|
|
const int x, |
|
|
const int y, |
|
|
const uint32_t MotionFlags, |
|
|
const uint32_t iQuant, |
|
|
const uint32_t iFcode, |
|
|
const MBParam * const pParam, |
|
|
const MACROBLOCK * const pMBs, |
|
|
const MACROBLOCK * const prevMBs, |
|
|
VECTOR * const currMV, |
|
|
VECTOR * const currPMV); |
|
|
|
|
|
int32_t EPZSSearch16(const uint8_t * const pRef, |
|
|
const uint8_t * const pRefH, |
|
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const IMAGE * const pCur, |
|
|
const int x, |
|
|
const int y, |
|
|
const uint32_t MotionFlags, |
|
|
const uint32_t iQuant, |
|
|
const uint32_t iFcode, |
|
|
const MBParam * const pParam, |
|
|
const MACROBLOCK * const pMBs, |
|
|
const MACROBLOCK * const prevMBs, |
|
|
VECTOR * const currMV, |
|
|
VECTOR * const currPMV); |
|
|
|
|
|
|
|
|
int32_t PMVfastSearch8(const uint8_t * const pRef, |
|
|
const uint8_t * const pRefH, |
|
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const IMAGE * const pCur, |
|
|
const int x, |
|
|
const int y, |
|
|
const int start_x, |
|
|
const int start_y, |
|
|
const uint32_t MotionFlags, |
|
|
const uint32_t iQuant, |
|
|
const uint32_t iFcode, |
|
|
const MBParam * const pParam, |
|
|
const MACROBLOCK * const pMBs, |
|
|
const MACROBLOCK * const prevMBs, |
|
|
VECTOR * const currMV, |
|
|
VECTOR * const currPMV); |
|
|
|
|
|
int32_t EPZSSearch8(const uint8_t * const pRef, |
|
|
const uint8_t * const pRefH, |
|
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const IMAGE * const pCur, |
|
|
const int x, |
|
|
const int y, |
|
|
const int start_x, |
|
|
const int start_y, |
|
|
const uint32_t MotionFlags, |
|
|
const uint32_t iQuant, |
|
|
const uint32_t iFcode, |
|
|
const MBParam * const pParam, |
|
|
const MACROBLOCK * const pMBs, |
|
|
const MACROBLOCK * const prevMBs, |
|
|
VECTOR * const currMV, |
|
|
VECTOR * const currPMV); |
|
46 |
|
|
47 |
|
|
|
typedef int32_t(MainSearch16Func) (const uint8_t * const pRef, |
|
|
const uint8_t * const pRefH, |
|
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const uint8_t * const cur, |
|
|
const int x, |
|
|
const int y, |
|
|
int32_t startx, |
|
|
int32_t starty, |
|
|
int32_t iMinSAD, |
|
|
VECTOR * const currMV, |
|
|
const VECTOR * const pmv, |
|
|
const int32_t min_dx, |
|
|
const int32_t max_dx, |
|
|
const int32_t min_dy, |
|
|
const int32_t max_dy, |
|
|
const int32_t iEdgedWidth, |
|
|
const int32_t iDiamondSize, |
|
|
const int32_t iFcode, |
|
|
const int32_t iQuant, |
|
|
int iFound); |
|
|
|
|
|
typedef MainSearch16Func *MainSearch16FuncPtr; |
|
|
|
|
|
|
|
|
typedef int32_t(MainSearch8Func) (const uint8_t * const pRef, |
|
|
const uint8_t * const pRefH, |
|
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const uint8_t * const cur, |
|
|
const int x, |
|
|
const int y, |
|
|
int32_t startx, |
|
|
int32_t starty, |
|
|
int32_t iMinSAD, |
|
|
VECTOR * const currMV, |
|
|
const VECTOR * const pmv, |
|
|
const int32_t min_dx, |
|
|
const int32_t max_dx, |
|
|
const int32_t min_dy, |
|
|
const int32_t max_dy, |
|
|
const int32_t iEdgedWidth, |
|
|
const int32_t iDiamondSize, |
|
|
const int32_t iFcode, |
|
|
const int32_t iQuant, |
|
|
int iFound); |
|
|
|
|
|
typedef MainSearch8Func *MainSearch8FuncPtr; |
|
|
|
|
48 |
static int32_t lambda_vec16[32] = /* rounded values for lambda param for weight of motion bits as in modified H.26L */ |
static int32_t lambda_vec16[32] = /* rounded values for lambda param for weight of motion bits as in modified H.26L */ |
49 |
{ 0, (int) (1.00235 + 0.5), (int) (1.15582 + 0.5), (int) (1.31976 + 0.5), |
{ 0, (int) (1.00235 + 0.5), (int) (1.15582 + 0.5), (int) (1.31976 + 0.5), |
50 |
(int) (1.49591 + 0.5), (int) (1.68601 + 0.5), |
(int) (1.49591 + 0.5), (int) (1.68601 + 0.5), |
121 |
mv_bits(dy, iFcode)); |
mv_bits(dy, iFcode)); |
122 |
} |
} |
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef SEARCH16 |
|
|
#define SEARCH16 PMVfastSearch16 |
|
|
//#define SEARCH16 FullSearch16 |
|
|
//#define SEARCH16 EPZSSearch16 |
|
|
#endif |
|
|
|
|
|
#ifndef SEARCH8 |
|
|
#define SEARCH8 PMVfastSearch8 |
|
|
//#define SEARCH8 EPZSSearch8 |
|
|
#endif |
|
|
|
|
124 |
bool |
bool |
125 |
MotionEstimation(MBParam * const pParam, |
MotionEstimation(MBParam * const pParam, |
126 |
FRAMEINFO * const current, |
FRAMEINFO * const current, |
137 |
const IMAGE *const pCurrent = ¤t->image; |
const IMAGE *const pCurrent = ¤t->image; |
138 |
const IMAGE *const pRef = &reference->image; |
const IMAGE *const pRef = &reference->image; |
139 |
|
|
140 |
const VECTOR zeroMV = { 0, 0 }; |
static const VECTOR zeroMV = { 0, 0 }; |
141 |
|
VECTOR predMV; |
142 |
|
|
143 |
int32_t x, y; |
uint32_t x, y; |
144 |
int32_t iIntra = 0; |
uint32_t iIntra = 0; |
145 |
VECTOR pmv; |
VECTOR pmv; |
146 |
|
|
147 |
if (sadInit) |
if (sadInit) |
148 |
(*sadInit) (); |
(*sadInit) (); |
149 |
|
|
150 |
for (y = 0; y < iHcount; y++) |
for (y = 0; y < iHcount; y++) { |
151 |
for (x = 0; x < iWcount; x++) { |
for (x = 0; x < iWcount; x++) { |
152 |
|
|
153 |
MACROBLOCK *const pMB = &pMBs[x + y * iWcount]; |
MACROBLOCK *const pMB = &pMBs[x + y * iWcount]; |
154 |
|
|
155 |
|
predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
156 |
|
|
157 |
pMB->sad16 = |
pMB->sad16 = |
158 |
SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
159 |
y, current->motion_flags, current->quant, |
x, y, predMV.x, predMV.y, predMV.x, predMV.y, |
160 |
|
current->motion_flags, current->quant, |
161 |
current->fcode, pParam, pMBs, prevMBs, &pMB->mv16, |
current->fcode, pParam, pMBs, prevMBs, &pMB->mv16, |
162 |
&pMB->pmvs[0]); |
&pMB->pmvs[0]); |
163 |
|
|
189 |
pMB->dquant == NO_CHANGE)) { |
pMB->dquant == NO_CHANGE)) { |
190 |
int32_t sad8 = IMV16X16 * current->quant; |
int32_t sad8 = IMV16X16 * current->quant; |
191 |
|
|
192 |
if (sad8 < pMB->sad16) |
if (sad8 < pMB->sad16) { |
|
|
|
193 |
sad8 += pMB->sad8[0] = |
sad8 += pMB->sad8[0] = |
194 |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
195 |
pCurrent, 2 * x, 2 * y, pMB->mv16.x, |
pCurrent, 2 * x, 2 * y, |
196 |
pMB->mv16.y, current->motion_flags, |
pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y, |
197 |
|
current->motion_flags, |
198 |
current->quant, current->fcode, pParam, |
current->quant, current->fcode, pParam, |
199 |
pMBs, prevMBs, &pMB->mvs[0], |
pMBs, prevMBs, &pMB->mvs[0], |
200 |
&pMB->pmvs[0]); |
&pMB->pmvs[0]); |
201 |
|
} |
202 |
|
if (sad8 < pMB->sad16) { |
203 |
|
|
204 |
if (sad8 < pMB->sad16) |
predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 1); |
205 |
sad8 += pMB->sad8[1] = |
sad8 += pMB->sad8[1] = |
206 |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
207 |
pCurrent, 2 * x + 1, 2 * y, pMB->mv16.x, |
pCurrent, 2 * x + 1, 2 * y, |
208 |
pMB->mv16.y, current->motion_flags, |
pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y, |
209 |
|
current->motion_flags, |
210 |
current->quant, current->fcode, pParam, |
current->quant, current->fcode, pParam, |
211 |
pMBs, prevMBs, &pMB->mvs[1], |
pMBs, prevMBs, &pMB->mvs[1], |
212 |
&pMB->pmvs[1]); |
&pMB->pmvs[1]); |
213 |
|
} |
214 |
if (sad8 < pMB->sad16) |
if (sad8 < pMB->sad16) { |
215 |
|
predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 2); |
216 |
sad8 += pMB->sad8[2] = |
sad8 += pMB->sad8[2] = |
217 |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
218 |
pCurrent, 2 * x, 2 * y + 1, pMB->mv16.x, |
pCurrent, 2 * x, 2 * y + 1, |
219 |
pMB->mv16.y, current->motion_flags, |
pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y, |
220 |
|
current->motion_flags, |
221 |
current->quant, current->fcode, pParam, |
current->quant, current->fcode, pParam, |
222 |
pMBs, prevMBs, &pMB->mvs[2], |
pMBs, prevMBs, &pMB->mvs[2], |
223 |
&pMB->pmvs[2]); |
&pMB->pmvs[2]); |
224 |
|
} |
225 |
if (sad8 < pMB->sad16) |
if (sad8 < pMB->sad16) { |
226 |
|
predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 3); |
227 |
sad8 += pMB->sad8[3] = |
sad8 += pMB->sad8[3] = |
228 |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, |
229 |
pCurrent, 2 * x + 1, 2 * y + 1, |
pCurrent, 2 * x + 1, 2 * y + 1, |
230 |
pMB->mv16.x, pMB->mv16.y, |
pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y, |
231 |
current->motion_flags, current->quant, |
current->motion_flags, |
232 |
current->fcode, pParam, pMBs, prevMBs, |
current->quant, current->fcode, pParam, |
233 |
&pMB->mvs[3], &pMB->pmvs[3]); |
pMBs, prevMBs, |
234 |
|
&pMB->mvs[3], |
235 |
|
&pMB->pmvs[3]); |
236 |
|
} |
237 |
|
|
238 |
/* decide: MODE_INTER or MODE_INTER4V |
/* decide: MODE_INTER or MODE_INTER4V |
239 |
mpeg4: if (sad8 < pMB->sad16 - nb/2+1) use_inter4v |
mpeg4: if (sad8 < pMB->sad16 - nb/2+1) use_inter4v |
255 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mv16; |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mv16; |
256 |
pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = |
pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = |
257 |
pMB->sad16; |
pMB->sad16; |
|
|
|
258 |
} |
} |
259 |
|
} |
260 |
|
|
261 |
return 0; |
return 0; |
262 |
} |
} |
263 |
|
|
264 |
|
|
265 |
#define CHECK_MV16_ZERO {\ |
#define CHECK_MV16_ZERO {\ |
266 |
if ( (0 <= max_dx) && (0 >= min_dx) \ |
if ( (0 <= max_dx) && (0 >= min_dx) \ |
267 |
&& (0 <= max_dy) && (0 >= min_dy) ) \ |
&& (0 <= max_dy) && (0 >= min_dy) ) \ |
268 |
{ \ |
{ \ |
269 |
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); \ |
270 |
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);\ |
271 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
272 |
{ iMinSAD=iSAD; currMV->x=0; currMV->y=0; } } \ |
{ iMinSAD=iSAD; currMV->x=0; currMV->y=0; } } \ |
273 |
} |
} |
274 |
|
|
275 |
#define NOCHECK_MV16_CANDIDATE(X,Y) { \ |
#define NOCHECK_MV16_CANDIDATE(X,Y) { \ |
276 |
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); \ |
277 |
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);\ |
278 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
279 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \ |
280 |
} |
} |
284 |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
285 |
{ \ |
{ \ |
286 |
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); \ |
287 |
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);\ |
288 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
289 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \ |
290 |
} |
} |
294 |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
295 |
{ \ |
{ \ |
296 |
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); \ |
297 |
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);\ |
298 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
299 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \ |
300 |
} |
} |
304 |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
305 |
{ \ |
{ \ |
306 |
iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ |
iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \ |
307 |
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);\ |
308 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
309 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \ |
310 |
} |
} |
312 |
|
|
313 |
#define CHECK_MV8_ZERO {\ |
#define CHECK_MV8_ZERO {\ |
314 |
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); \ |
315 |
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);\ |
316 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
317 |
{ iMinSAD=iSAD; currMV->x=0; currMV->y=0; } \ |
{ iMinSAD=iSAD; currMV->x=0; currMV->y=0; } \ |
318 |
} |
} |
320 |
#define NOCHECK_MV8_CANDIDATE(X,Y) \ |
#define NOCHECK_MV8_CANDIDATE(X,Y) \ |
321 |
{ \ |
{ \ |
322 |
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); \ |
323 |
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);\ |
324 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
325 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \ |
326 |
} |
} |
330 |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
331 |
{ \ |
{ \ |
332 |
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); \ |
333 |
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);\ |
334 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
335 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \ |
336 |
} |
} |
340 |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
341 |
{ \ |
{ \ |
342 |
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); \ |
343 |
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);\ |
344 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
345 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \ |
346 |
} |
} |
350 |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
&& ((Y) <= max_dy) && ((Y) >= min_dy) ) \ |
351 |
{ \ |
{ \ |
352 |
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); \ |
353 |
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);\ |
354 |
if (iSAD < iMinSAD) \ |
if (iSAD < iMinSAD) \ |
355 |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \ |
{ iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \ |
356 |
} |
} |
357 |
|
|
358 |
|
#if 0 |
359 |
/* too slow and not fully functional at the moment */ |
/* too slow and not fully functional at the moment */ |
|
/* |
|
360 |
int32_t ZeroSearch16( |
int32_t ZeroSearch16( |
361 |
const uint8_t * const pRef, |
const uint8_t * const pRef, |
362 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
376 |
const int32_t iEdgedWidth = pParam->edged_width; |
const int32_t iEdgedWidth = pParam->edged_width; |
377 |
const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth; |
const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth; |
378 |
int32_t iSAD; |
int32_t iSAD; |
379 |
int32_t pred_x,pred_y; |
VECTOR pred; |
380 |
|
|
381 |
get_pmv(pMBs, x, y, pParam->mb_width, 0, &pred_x, &pred_y); |
|
382 |
|
pred = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
383 |
|
|
384 |
iSAD = sad16( cur, |
iSAD = sad16( cur, |
385 |
get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0,0, iEdgedWidth), |
get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0,0, iEdgedWidth), |
389 |
|
|
390 |
currMV->x = 0; |
currMV->x = 0; |
391 |
currMV->y = 0; |
currMV->y = 0; |
392 |
currPMV->x = -pred_x; |
currPMV->x = -pred.x; |
393 |
currPMV->y = -pred_y; |
currPMV->y = -pred.y; |
394 |
|
|
395 |
return iSAD; |
return iSAD; |
396 |
|
|
397 |
} |
} |
398 |
*/ |
#endif /* 0 */ |
399 |
|
|
400 |
int32_t |
int32_t |
401 |
Diamond16_MainSearch(const uint8_t * const pRef, |
Diamond16_MainSearch(const uint8_t * const pRef, |
405 |
const uint8_t * const cur, |
const uint8_t * const cur, |
406 |
const int x, |
const int x, |
407 |
const int y, |
const int y, |
408 |
int32_t startx, |
const int start_x, |
409 |
int32_t starty, |
const int start_y, |
410 |
int32_t iMinSAD, |
int iMinSAD, |
411 |
VECTOR * const currMV, |
VECTOR * const currMV, |
412 |
const VECTOR * const pmv, |
const int center_x, |
413 |
|
const int center_y, |
414 |
const int32_t min_dx, |
const int32_t min_dx, |
415 |
const int32_t max_dx, |
const int32_t max_dx, |
416 |
const int32_t min_dy, |
const int32_t min_dy, |
424 |
/* Do a diamond search around given starting point, return SAD of best */ |
/* Do a diamond search around given starting point, return SAD of best */ |
425 |
|
|
426 |
int32_t iDirection = 0; |
int32_t iDirection = 0; |
427 |
|
int32_t iDirectionBackup; |
428 |
int32_t iSAD; |
int32_t iSAD; |
429 |
VECTOR backupMV; |
VECTOR backupMV; |
430 |
|
|
431 |
backupMV.x = startx; |
backupMV.x = start_x; |
432 |
backupMV.y = starty; |
backupMV.y = start_y; |
433 |
|
|
434 |
/* 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 */ |
435 |
|
|
438 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3); |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3); |
439 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4); |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4); |
440 |
|
|
441 |
if (iDirection) |
if (iDirection) { |
442 |
while (!iFound) { |
while (!iFound) { |
443 |
iFound = 1; |
iFound = 1; |
444 |
backupMV = *currMV; |
backupMV = *currMV; |
445 |
|
iDirectionBackup = iDirection; |
446 |
|
|
447 |
if (iDirection != 2) |
if (iDirectionBackup != 2) |
448 |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
449 |
backupMV.y, 1); |
backupMV.y, 1); |
450 |
if (iDirection != 1) |
if (iDirectionBackup != 1) |
451 |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
452 |
backupMV.y, 2); |
backupMV.y, 2); |
453 |
if (iDirection != 4) |
if (iDirectionBackup != 4) |
454 |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, |
455 |
backupMV.y - iDiamondSize, 3); |
backupMV.y - iDiamondSize, 3); |
456 |
if (iDirection != 3) |
if (iDirectionBackup != 3) |
457 |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, |
458 |
backupMV.y + iDiamondSize, 4); |
backupMV.y + iDiamondSize, 4); |
459 |
|
} |
460 |
} else { |
} else { |
461 |
currMV->x = startx; |
currMV->x = start_x; |
462 |
currMV->y = starty; |
currMV->y = start_y; |
463 |
} |
} |
464 |
return iMinSAD; |
return iMinSAD; |
465 |
} |
} |
472 |
const uint8_t * const cur, |
const uint8_t * const cur, |
473 |
const int x, |
const int x, |
474 |
const int y, |
const int y, |
475 |
int32_t startx, |
const int start_x, |
476 |
int32_t starty, |
const int start_y, |
477 |
int32_t iMinSAD, |
int iMinSAD, |
478 |
VECTOR * const currMV, |
VECTOR * const currMV, |
479 |
const VECTOR * const pmv, |
const int center_x, |
480 |
|
const int center_y, |
481 |
const int32_t min_dx, |
const int32_t min_dx, |
482 |
const int32_t max_dx, |
const int32_t max_dx, |
483 |
const int32_t min_dy, |
const int32_t min_dy, |
494 |
int32_t iSAD; |
int32_t iSAD; |
495 |
VECTOR backupMV; |
VECTOR backupMV; |
496 |
|
|
497 |
backupMV.x = startx; |
backupMV.x = start_x; |
498 |
backupMV.y = starty; |
backupMV.y = start_y; |
499 |
|
|
500 |
/* 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 */ |
501 |
|
|
520 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
521 |
|
|
522 |
|
|
523 |
if (iDirection) |
if (iDirection) { |
524 |
while (!iFound) { |
while (!iFound) { |
525 |
iFound = 1; |
iFound = 1; |
526 |
backupMV = *currMV; |
backupMV = *currMV; |
529 |
case 1: |
case 1: |
530 |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
531 |
backupMV.y, 1); |
backupMV.y, 1); |
532 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
533 |
backupMV.y - iDiamondSize, 5); |
backupMV.y - iDiamondSize, 5); |
534 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
535 |
backupMV.y - iDiamondSize, 7); |
backupMV.y - iDiamondSize, 7); |
536 |
break; |
break; |
537 |
case 2: |
case 2: |
538 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
539 |
2); |
2); |
540 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
541 |
backupMV.y + iDiamondSize, 6); |
backupMV.y + iDiamondSize, 6); |
542 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
543 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
544 |
break; |
break; |
545 |
|
|
546 |
case 3: |
case 3: |
547 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
548 |
4); |
4); |
549 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
550 |
backupMV.y - iDiamondSize, 7); |
backupMV.y - iDiamondSize, 7); |
551 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
552 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
553 |
break; |
break; |
554 |
|
|
555 |
case 4: |
case 4: |
556 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
557 |
3); |
3); |
558 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
559 |
backupMV.y - iDiamondSize, 5); |
backupMV.y - iDiamondSize, 5); |
560 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
561 |
backupMV.y + iDiamondSize, 6); |
backupMV.y + iDiamondSize, 6); |
562 |
break; |
break; |
563 |
|
|
564 |
case 5: |
case 5: |
565 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y, |
566 |
1); |
1); |
567 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
568 |
3); |
3); |
569 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
570 |
backupMV.y - iDiamondSize, 5); |
backupMV.y - iDiamondSize, 5); |
571 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
572 |
backupMV.y + iDiamondSize, 6); |
backupMV.y + iDiamondSize, 6); |
573 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
574 |
backupMV.y - iDiamondSize, 7); |
backupMV.y - iDiamondSize, 7); |
575 |
break; |
break; |
576 |
|
|
577 |
case 6: |
case 6: |
578 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
579 |
2); |
2); |
580 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
581 |
3); |
3); |
582 |
|
|
583 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
584 |
backupMV.y - iDiamondSize, 5); |
backupMV.y - iDiamondSize, 5); |
585 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
586 |
backupMV.y + iDiamondSize, 6); |
backupMV.y + iDiamondSize, 6); |
587 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
588 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
589 |
|
|
590 |
break; |
break; |
592 |
case 7: |
case 7: |
593 |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
594 |
backupMV.y, 1); |
backupMV.y, 1); |
595 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
596 |
4); |
4); |
597 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
598 |
backupMV.y - iDiamondSize, 5); |
backupMV.y - iDiamondSize, 5); |
599 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
600 |
backupMV.y - iDiamondSize, 7); |
backupMV.y - iDiamondSize, 7); |
601 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
602 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
603 |
break; |
break; |
604 |
|
|
605 |
case 8: |
case 8: |
606 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
607 |
2); |
2); |
608 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
609 |
4); |
4); |
610 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
611 |
backupMV.y + iDiamondSize, 6); |
backupMV.y + iDiamondSize, 6); |
612 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
613 |
backupMV.y - iDiamondSize, 7); |
backupMV.y - iDiamondSize, 7); |
614 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
615 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
616 |
break; |
break; |
617 |
default: |
default: |
618 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y, |
619 |
1); |
1); |
620 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
621 |
2); |
2); |
622 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
623 |
3); |
3); |
624 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
625 |
4); |
4); |
626 |
|
|
627 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
628 |
backupMV.y - iDiamondSize, 5); |
backupMV.y - iDiamondSize, 5); |
629 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
630 |
backupMV.y + iDiamondSize, 6); |
backupMV.y + iDiamondSize, 6); |
631 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
632 |
backupMV.y - iDiamondSize, 7); |
backupMV.y - iDiamondSize, 7); |
633 |
CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
634 |
backupMV.y + iDiamondSize, 8); |
backupMV.y + iDiamondSize, 8); |
635 |
break; |
break; |
636 |
} |
} |
637 |
|
} |
638 |
} else { |
} else { |
639 |
currMV->x = startx; |
currMV->x = start_x; |
640 |
currMV->y = starty; |
currMV->y = start_y; |
641 |
} |
} |
642 |
return iMinSAD; |
return iMinSAD; |
643 |
} |
} |
651 |
const uint8_t * const cur, |
const uint8_t * const cur, |
652 |
const int x, |
const int x, |
653 |
const int y, |
const int y, |
654 |
int32_t startx, |
const int start_x, |
655 |
int32_t starty, |
const int start_y, |
656 |
int32_t iMinSAD, |
int iMinSAD, |
657 |
VECTOR * const currMV, |
VECTOR * const currMV, |
658 |
const VECTOR * const pmv, |
const int center_x, |
659 |
|
const int center_y, |
660 |
const int32_t min_dx, |
const int32_t min_dx, |
661 |
const int32_t max_dx, |
const int32_t max_dx, |
662 |
const int32_t min_dy, |
const int32_t min_dy, |
671 |
int32_t dx, dy; |
int32_t dx, dy; |
672 |
VECTOR backupMV; |
VECTOR backupMV; |
673 |
|
|
674 |
backupMV.x = startx; |
backupMV.x = start_x; |
675 |
backupMV.y = starty; |
backupMV.y = start_y; |
676 |
|
|
677 |
for (dx = min_dx; dx <= max_dx; dx += iDiamondSize) |
for (dx = min_dx; dx <= max_dx; dx += iDiamondSize) |
678 |
for (dy = min_dy; dy <= max_dy; dy += iDiamondSize) |
for (dy = min_dy; dy <= max_dy; dy += iDiamondSize) |
689 |
const uint8_t * const cur, |
const uint8_t * const cur, |
690 |
const int x, |
const int x, |
691 |
const int y, |
const int y, |
692 |
int32_t startx, |
const int start_xi, |
693 |
int32_t starty, |
const int start_yi, |
694 |
int32_t iMinSAD, |
int iMinSAD, |
695 |
VECTOR * const currMV, |
VECTOR * const currMV, |
696 |
const VECTOR * const pmv, |
const int center_x, |
697 |
|
const int center_y, |
698 |
const int32_t min_dx, |
const int32_t min_dx, |
699 |
const int32_t max_dx, |
const int32_t max_dx, |
700 |
const int32_t min_dy, |
const int32_t min_dy, |
707 |
{ |
{ |
708 |
|
|
709 |
int32_t iSAD; |
int32_t iSAD; |
710 |
|
int start_x = start_xi, start_y = start_yi; |
711 |
|
|
712 |
/* 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) */ |
713 |
|
|
714 |
if (iDirection) { |
if (iDirection) { |
715 |
CHECK_MV16_CANDIDATE(startx - iDiamondSize, starty); |
CHECK_MV16_CANDIDATE(start_x - iDiamondSize, start_y); |
716 |
CHECK_MV16_CANDIDATE(startx + iDiamondSize, starty); |
CHECK_MV16_CANDIDATE(start_x + iDiamondSize, start_y); |
717 |
CHECK_MV16_CANDIDATE(startx, starty - iDiamondSize); |
CHECK_MV16_CANDIDATE(start_x, start_y - iDiamondSize); |
718 |
CHECK_MV16_CANDIDATE(startx, starty + iDiamondSize); |
CHECK_MV16_CANDIDATE(start_x, start_y + iDiamondSize); |
719 |
} else { |
} else { |
720 |
int bDirection = 1 + 2 + 4 + 8; |
int bDirection = 1 + 2 + 4 + 8; |
721 |
|
|
722 |
do { |
do { |
723 |
iDirection = 0; |
iDirection = 0; |
724 |
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) |
725 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, starty, 1); |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1); |
726 |
|
|
727 |
if (bDirection & 2) |
if (bDirection & 2) |
728 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, starty, 2); |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2); |
729 |
|
|
730 |
if (bDirection & 4) |
if (bDirection & 4) |
731 |
CHECK_MV16_CANDIDATE_DIR(startx, starty - iDiamondSize, 4); |
CHECK_MV16_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4); |
732 |
|
|
733 |
if (bDirection & 8) |
if (bDirection & 8) |
734 |
CHECK_MV16_CANDIDATE_DIR(startx, starty + iDiamondSize, 8); |
CHECK_MV16_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8); |
735 |
|
|
736 |
/* now we're doing diagonal checks near our candidate */ |
/* now we're doing diagonal checks near our candidate */ |
737 |
|
|
739 |
{ |
{ |
740 |
bDirection = iDirection; |
bDirection = iDirection; |
741 |
iDirection = 0; |
iDirection = 0; |
742 |
startx = currMV->x; |
start_x = currMV->x; |
743 |
starty = currMV->y; |
start_y = currMV->y; |
744 |
if (bDirection & 3) //our candidate is left or right |
if (bDirection & 3) //our candidate is left or right |
745 |
{ |
{ |
746 |
CHECK_MV16_CANDIDATE_DIR(startx, starty + iDiamondSize, 8); |
CHECK_MV16_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8); |
747 |
CHECK_MV16_CANDIDATE_DIR(startx, starty - iDiamondSize, 4); |
CHECK_MV16_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4); |
748 |
} else // what remains here is up or down |
} else // what remains here is up or down |
749 |
{ |
{ |
750 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, starty, 2); |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2); |
751 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, starty, 1); |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1); |
752 |
} |
} |
753 |
|
|
754 |
if (iDirection) { |
if (iDirection) { |
755 |
bDirection += iDirection; |
bDirection += iDirection; |
756 |
startx = currMV->x; |
start_x = currMV->x; |
757 |
starty = currMV->y; |
start_y = currMV->y; |
758 |
} |
} |
759 |
} else //about to quit, eh? not so fast.... |
} else //about to quit, eh? not so fast.... |
760 |
{ |
{ |
761 |
switch (bDirection) { |
switch (bDirection) { |
762 |
case 2: |
case 2: |
763 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
764 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
765 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
766 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
767 |
break; |
break; |
768 |
case 1: |
case 1: |
769 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
|
770 |
starty - iDiamondSize, 1 + 4); |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
771 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
start_y - iDiamondSize, 1 + 4); |
772 |
starty + iDiamondSize, 1 + 8); |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
773 |
|
start_y + iDiamondSize, 1 + 8); |
774 |
break; |
break; |
775 |
case 2 + 4: |
case 2 + 4: |
776 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
777 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
778 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
779 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
780 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
781 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
782 |
break; |
break; |
783 |
case 4: |
case 4: |
784 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
785 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
786 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
787 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
788 |
break; |
break; |
789 |
case 8: |
case 8: |
790 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
791 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
792 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
793 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
794 |
break; |
break; |
795 |
case 1 + 4: |
case 1 + 4: |
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 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
799 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
800 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
801 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
802 |
break; |
break; |
803 |
case 2 + 8: |
case 2 + 8: |
804 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
805 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
806 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
807 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
808 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
809 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
810 |
break; |
break; |
811 |
case 1 + 8: |
case 1 + 8: |
812 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
813 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
814 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
815 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
816 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
817 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
818 |
break; |
break; |
819 |
default: //1+2+4+8 == we didn't find anything at all |
default: //1+2+4+8 == we didn't find anything at all |
820 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
821 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
822 |
CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, |
823 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
824 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
825 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
826 |
CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, |
827 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
828 |
break; |
break; |
829 |
} |
} |
830 |
if (!iDirection) |
if (!iDirection) |
831 |
break; //ok, the end. really |
break; //ok, the end. really |
832 |
else { |
else { |
833 |
bDirection = iDirection; |
bDirection = iDirection; |
834 |
startx = currMV->x; |
start_x = currMV->x; |
835 |
starty = currMV->y; |
start_y = currMV->y; |
836 |
} |
} |
837 |
} |
} |
838 |
} |
} |
839 |
while (1); //forever |
while (1); //forever |
840 |
} |
} |
841 |
|
|
842 |
return iMinSAD; |
return iMinSAD; |
843 |
} |
} |
844 |
|
|
850 |
const uint8_t * const cur, |
const uint8_t * const cur, |
851 |
const int x, |
const int x, |
852 |
const int y, |
const int y, |
853 |
int32_t startx, |
const int start_xi, |
854 |
int32_t starty, |
const int start_yi, |
855 |
int32_t iMinSAD, |
int iMinSAD, |
856 |
VECTOR * const currMV, |
VECTOR * const currMV, |
857 |
const VECTOR * const pmv, |
const int center_x, |
858 |
|
const int center_y, |
859 |
const int32_t min_dx, |
const int32_t min_dx, |
860 |
const int32_t max_dx, |
const int32_t max_dx, |
861 |
const int32_t min_dy, |
const int32_t min_dy, |
868 |
{ |
{ |
869 |
|
|
870 |
int32_t iSAD; |
int32_t iSAD; |
871 |
|
int start_x = start_xi, start_y = start_yi; |
872 |
|
|
873 |
/* 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) */ |
874 |
|
|
875 |
if (iDirection) { |
if (iDirection) { |
876 |
CHECK_MV8_CANDIDATE(startx - iDiamondSize, starty); |
CHECK_MV8_CANDIDATE(start_x - iDiamondSize, start_y); |
877 |
CHECK_MV8_CANDIDATE(startx + iDiamondSize, starty); |
CHECK_MV8_CANDIDATE(start_x + iDiamondSize, start_y); |
878 |
CHECK_MV8_CANDIDATE(startx, starty - iDiamondSize); |
CHECK_MV8_CANDIDATE(start_x, start_y - iDiamondSize); |
879 |
CHECK_MV8_CANDIDATE(startx, starty + iDiamondSize); |
CHECK_MV8_CANDIDATE(start_x, start_y + iDiamondSize); |
880 |
} else { |
} else { |
881 |
int bDirection = 1 + 2 + 4 + 8; |
int bDirection = 1 + 2 + 4 + 8; |
882 |
|
|
883 |
do { |
do { |
884 |
iDirection = 0; |
iDirection = 0; |
885 |
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) |
886 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, starty, 1); |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1); |
887 |
|
|
888 |
if (bDirection & 2) |
if (bDirection & 2) |
889 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, starty, 2); |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2); |
890 |
|
|
891 |
if (bDirection & 4) |
if (bDirection & 4) |
892 |
CHECK_MV8_CANDIDATE_DIR(startx, starty - iDiamondSize, 4); |
CHECK_MV8_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4); |
893 |
|
|
894 |
if (bDirection & 8) |
if (bDirection & 8) |
895 |
CHECK_MV8_CANDIDATE_DIR(startx, starty + iDiamondSize, 8); |
CHECK_MV8_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8); |
896 |
|
|
897 |
/* now we're doing diagonal checks near our candidate */ |
/* now we're doing diagonal checks near our candidate */ |
898 |
|
|
900 |
{ |
{ |
901 |
bDirection = iDirection; |
bDirection = iDirection; |
902 |
iDirection = 0; |
iDirection = 0; |
903 |
startx = currMV->x; |
start_x = currMV->x; |
904 |
starty = currMV->y; |
start_y = currMV->y; |
905 |
if (bDirection & 3) //our candidate is left or right |
if (bDirection & 3) //our candidate is left or right |
906 |
{ |
{ |
907 |
CHECK_MV8_CANDIDATE_DIR(startx, starty + iDiamondSize, 8); |
CHECK_MV8_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8); |
908 |
CHECK_MV8_CANDIDATE_DIR(startx, starty - iDiamondSize, 4); |
CHECK_MV8_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4); |
909 |
} else // what remains here is up or down |
} else // what remains here is up or down |
910 |
{ |
{ |
911 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, starty, 2); |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2); |
912 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, starty, 1); |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1); |
913 |
} |
} |
914 |
|
|
915 |
if (iDirection) { |
if (iDirection) { |
916 |
bDirection += iDirection; |
bDirection += iDirection; |
917 |
startx = currMV->x; |
start_x = currMV->x; |
918 |
starty = currMV->y; |
start_y = currMV->y; |
919 |
} |
} |
920 |
} else //about to quit, eh? not so fast.... |
} else //about to quit, eh? not so fast.... |
921 |
{ |
{ |
922 |
switch (bDirection) { |
switch (bDirection) { |
923 |
case 2: |
case 2: |
924 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
925 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
926 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
927 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
928 |
break; |
break; |
929 |
case 1: |
case 1: |
930 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
931 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
932 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
933 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
934 |
break; |
break; |
935 |
case 2 + 4: |
case 2 + 4: |
936 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
937 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
938 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
939 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
940 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
941 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
942 |
break; |
break; |
943 |
case 4: |
case 4: |
944 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
945 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
946 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
947 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
948 |
break; |
break; |
949 |
case 8: |
case 8: |
950 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
951 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
952 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
953 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
954 |
break; |
break; |
955 |
case 1 + 4: |
case 1 + 4: |
956 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
957 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
958 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
959 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
960 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
961 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
962 |
break; |
break; |
963 |
case 2 + 8: |
case 2 + 8: |
964 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
965 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
966 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
967 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
968 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
969 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
970 |
break; |
break; |
971 |
case 1 + 8: |
case 1 + 8: |
972 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
973 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
974 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
975 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
976 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
977 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
978 |
break; |
break; |
979 |
default: //1+2+4+8 == we didn't find anything at all |
default: //1+2+4+8 == we didn't find anything at all |
980 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
981 |
starty - iDiamondSize, 1 + 4); |
start_y - iDiamondSize, 1 + 4); |
982 |
CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, |
983 |
starty + iDiamondSize, 1 + 8); |
start_y + iDiamondSize, 1 + 8); |
984 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
985 |
starty - iDiamondSize, 2 + 4); |
start_y - iDiamondSize, 2 + 4); |
986 |
CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, |
CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, |
987 |
starty + iDiamondSize, 2 + 8); |
start_y + iDiamondSize, 2 + 8); |
988 |
break; |
break; |
989 |
} |
} |
990 |
if (!(iDirection)) |
if (!(iDirection)) |
991 |
break; //ok, the end. really |
break; //ok, the end. really |
992 |
else { |
else { |
993 |
bDirection = iDirection; |
bDirection = iDirection; |
994 |
startx = currMV->x; |
start_x = currMV->x; |
995 |
starty = currMV->y; |
start_y = currMV->y; |
996 |
} |
} |
997 |
} |
} |
998 |
} |
} |
1010 |
const uint8_t * const cur, |
const uint8_t * const cur, |
1011 |
const int x, |
const int x, |
1012 |
const int y, |
const int y, |
1013 |
int32_t startx, |
const int start_x, |
1014 |
int32_t starty, |
const int start_y, |
1015 |
int32_t iMinSAD, |
int iMinSAD, |
1016 |
VECTOR * const currMV, |
VECTOR * const currMV, |
1017 |
const VECTOR * const pmv, |
const int center_x, |
1018 |
|
const int center_y, |
1019 |
const int32_t min_dx, |
const int32_t min_dx, |
1020 |
const int32_t max_dx, |
const int32_t max_dx, |
1021 |
const int32_t min_dy, |
const int32_t min_dy, |
1030 |
int32_t dx, dy; |
int32_t dx, dy; |
1031 |
VECTOR backupMV; |
VECTOR backupMV; |
1032 |
|
|
1033 |
backupMV.x = startx; |
backupMV.x = start_x; |
1034 |
backupMV.y = starty; |
backupMV.y = start_y; |
1035 |
|
|
1036 |
for (dx = min_dx; dx <= max_dx; dx += iDiamondSize) |
for (dx = min_dx; dx <= max_dx; dx += iDiamondSize) |
1037 |
for (dy = min_dy; dy <= max_dy; dy += iDiamondSize) |
for (dy = min_dy; dy <= max_dy; dy += iDiamondSize) |
1040 |
return iMinSAD; |
return iMinSAD; |
1041 |
} |
} |
1042 |
|
|
1043 |
|
Halfpel8_RefineFuncPtr Halfpel8_Refine; |
1044 |
|
|
1045 |
int32_t |
int32_t |
1046 |
Halfpel16_Refine(const uint8_t * const pRef, |
Halfpel16_Refine(const uint8_t * const pRef, |
1052 |
const int y, |
const int y, |
1053 |
VECTOR * const currMV, |
VECTOR * const currMV, |
1054 |
int32_t iMinSAD, |
int32_t iMinSAD, |
1055 |
const VECTOR * const pmv, |
const int center_x, |
1056 |
|
const int center_y, |
1057 |
const int32_t min_dx, |
const int32_t min_dx, |
1058 |
const int32_t max_dx, |
const int32_t max_dx, |
1059 |
const int32_t min_dy, |
const int32_t min_dy, |
1082 |
#define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16) |
#define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16) |
1083 |
|
|
1084 |
|
|
1085 |
|
|
1086 |
int32_t |
int32_t |
1087 |
PMVfastSearch16(const uint8_t * const pRef, |
PMVfastSearch16(const uint8_t * const pRef, |
1088 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
1091 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
1092 |
const int x, |
const int x, |
1093 |
const int y, |
const int y, |
1094 |
|
const int start_x, /* start is searched first, so it should contain the most */ |
1095 |
|
const int start_y, /* likely motion vector for this block */ |
1096 |
|
const int center_x, /* center is from where length of MVs is measured */ |
1097 |
|
const int center_y, |
1098 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
1099 |
const uint32_t iQuant, |
const uint32_t iQuant, |
1100 |
const uint32_t iFcode, |
const uint32_t iFcode, |
1128 |
|
|
1129 |
MainSearch16FuncPtr MainSearchPtr; |
MainSearch16FuncPtr MainSearchPtr; |
1130 |
|
|
|
// const MACROBLOCK * const pMB = pMBs + x + y * iWcount; |
|
1131 |
const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount; |
const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount; |
1132 |
|
|
1133 |
static int32_t threshA, threshB; |
int32_t threshA, threshB; |
1134 |
int32_t bPredEq; |
int32_t bPredEq; |
1135 |
int32_t iMinSAD, iSAD; |
int32_t iMinSAD, iSAD; |
1136 |
|
|
1148 |
} |
} |
1149 |
|
|
1150 |
/* 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; */ |
1151 |
bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad); |
bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad); |
1152 |
|
|
1153 |
if ((x == 0) && (y == 0)) { |
if ((x == 0) && (y == 0)) { |
1154 |
threshA = 512; |
threshA = 512; |
1155 |
threshB = 1024; |
threshB = 1024; |
|
|
|
1156 |
} else { |
} else { |
1157 |
threshA = psad[0]; |
threshA = psad[0]; |
1158 |
threshB = threshA + 256; |
threshB = threshA + 256; |
1173 |
If SAD<=256 goto Step 10. |
If SAD<=256 goto Step 10. |
1174 |
*/ |
*/ |
1175 |
|
|
1176 |
*currMV = pmv[0]; /* current best := prediction */ |
currMV->x = start_x; |
1177 |
|
currMV->y = start_y; |
1178 |
|
|
1179 |
if (!(MotionFlags & PMV_HALFPEL16)) { /* This should NOT be necessary! */ |
if (!(MotionFlags & PMV_HALFPEL16)) { /* This should NOT be necessary! */ |
1180 |
currMV->x = EVEN(currMV->x); |
currMV->x = EVEN(currMV->x); |
1181 |
currMV->y = EVEN(currMV->y); |
currMV->y = EVEN(currMV->y); |
1199 |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, |
1200 |
iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); |
iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); |
1201 |
iMinSAD += |
iMinSAD += |
1202 |
calc_delta_16(currMV->x - pmv[0].x, currMV->y - pmv[0].y, |
calc_delta_16(currMV->x - center_x, currMV->y - center_y, |
1203 |
(uint8_t) iFcode, iQuant); |
(uint8_t) iFcode, iQuant); |
1204 |
|
|
1205 |
if ((iMinSAD < 256) || |
if ((iMinSAD < 256) || |
1206 |
((MVequal(*currMV, prevMB->mvs[0])) && |
((MVequal(*currMV, prevMB->mvs[0])) && |
1207 |
((uint32_t) iMinSAD < prevMB->sad16))) { |
((int32_t) iMinSAD < prevMB->sad16))) { |
1208 |
if (iMinSAD < 2 * iQuant) // high chances for SKIP-mode |
if (iMinSAD < (int)(2 * iQuant)) // high chances for SKIP-mode |
1209 |
{ |
{ |
1210 |
if (!MVzero(*currMV)) { |
if (!MVzero(*currMV)) { |
1211 |
iMinSAD += MV16_00_BIAS; |
iMinSAD += MV16_00_BIAS; |
1308 |
|
|
1309 |
if ((iMinSAD <= threshA) || |
if ((iMinSAD <= threshA) || |
1310 |
(MVequal(*currMV, prevMB->mvs[0]) && |
(MVequal(*currMV, prevMB->mvs[0]) && |
1311 |
((uint32_t) iMinSAD < prevMB->sad16))) { |
((int32_t) iMinSAD < prevMB->sad16))) { |
1312 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1313 |
goto PMVfast16_Terminate_without_Refine; |
goto PMVfast16_Terminate_without_Refine; |
1314 |
if (MotionFlags & PMV_EARLYSTOP16) |
if (MotionFlags & PMV_EARLYSTOP16) |
1335 |
|
|
1336 |
backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */ |
backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */ |
1337 |
|
|
1338 |
|
|
1339 |
/* 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 */ |
1340 |
iSAD = |
iSAD = |
1341 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
1342 |
currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx, |
currMV->x, currMV->y, iMinSAD, &newMV, center_x, center_y, |
1343 |
|
min_dx, max_dx, |
1344 |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
1345 |
iQuant, iFound); |
iQuant, iFound); |
1346 |
|
|
1355 |
if (!(MVequal(pmv[0], backupMV))) { |
if (!(MVequal(pmv[0], backupMV))) { |
1356 |
iSAD = |
iSAD = |
1357 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
1358 |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv, |
center_x, center_y, iMinSAD, &newMV, center_x, center_y, |
1359 |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
1360 |
iDiamondSize, iFcode, iQuant, iFound); |
iDiamondSize, iFcode, iQuant, iFound); |
1361 |
|
|
1368 |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
1369 |
iSAD = |
iSAD = |
1370 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
1371 |
iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy, |
iMinSAD, &newMV, center_x, center_y, |
1372 |
max_dy, iEdgedWidth, iDiamondSize, iFcode, |
min_dx, max_dx, min_dy, max_dy, |
1373 |
|
iEdgedWidth, iDiamondSize, iFcode, |
1374 |
iQuant, iFound); |
iQuant, iFound); |
1375 |
|
|
1376 |
if (iSAD < iMinSAD) { |
if (iSAD < iMinSAD) { |
1388 |
if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step |
if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step |
1389 |
iMinSAD = |
iMinSAD = |
1390 |
Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
1391 |
iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy, |
iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy, |
1392 |
iFcode, iQuant, iEdgedWidth); |
iFcode, iQuant, iEdgedWidth); |
1393 |
|
|
1394 |
PMVfast16_Terminate_without_Refine: |
PMVfast16_Terminate_without_Refine: |
1395 |
currPMV->x = currMV->x - pmv[0].x; |
currPMV->x = currMV->x - center_x; |
1396 |
currPMV->y = currMV->y - pmv[0].y; |
currPMV->y = currMV->y - center_y; |
1397 |
return iMinSAD; |
return iMinSAD; |
1398 |
} |
} |
1399 |
|
|
1410 |
const uint8_t * const cur, |
const uint8_t * const cur, |
1411 |
const int x, |
const int x, |
1412 |
const int y, |
const int y, |
1413 |
int32_t startx, |
const int32_t start_x, |
1414 |
int32_t starty, |
const int32_t start_y, |
1415 |
int32_t iMinSAD, |
int32_t iMinSAD, |
1416 |
VECTOR * const currMV, |
VECTOR * const currMV, |
1417 |
const VECTOR * const pmv, |
const int center_x, |
1418 |
|
const int center_y, |
1419 |
const int32_t min_dx, |
const int32_t min_dx, |
1420 |
const int32_t max_dx, |
const int32_t max_dx, |
1421 |
const int32_t min_dy, |
const int32_t min_dy, |
1429 |
/* Do a diamond search around given starting point, return SAD of best */ |
/* Do a diamond search around given starting point, return SAD of best */ |
1430 |
|
|
1431 |
int32_t iDirection = 0; |
int32_t iDirection = 0; |
1432 |
|
int32_t iDirectionBackup; |
1433 |
int32_t iSAD; |
int32_t iSAD; |
1434 |
VECTOR backupMV; |
VECTOR backupMV; |
1435 |
|
|
1436 |
backupMV.x = startx; |
backupMV.x = start_x; |
1437 |
backupMV.y = starty; |
backupMV.y = start_y; |
1438 |
|
|
1439 |
/* 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 */ |
1440 |
|
|
1443 |
CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3); |
CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3); |
1444 |
CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4); |
CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4); |
1445 |
|
|
1446 |
if (iDirection) |
if (iDirection) { |
1447 |
while (!iFound) { |
while (!iFound) { |
1448 |
iFound = 1; |
iFound = 1; |
1449 |
backupMV = *currMV; // since iDirection!=0, this is well defined! |
backupMV = *currMV; // since iDirection!=0, this is well defined! |
1450 |
|
iDirectionBackup = iDirection; |
1451 |
|
|
1452 |
if (iDirection != 2) |
if (iDirectionBackup != 2) |
1453 |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1454 |
backupMV.y, 1); |
backupMV.y, 1); |
1455 |
if (iDirection != 1) |
if (iDirectionBackup != 1) |
1456 |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1457 |
backupMV.y, 2); |
backupMV.y, 2); |
1458 |
if (iDirection != 4) |
if (iDirectionBackup != 4) |
1459 |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, |
1460 |
backupMV.y - iDiamondSize, 3); |
backupMV.y - iDiamondSize, 3); |
1461 |
if (iDirection != 3) |
if (iDirectionBackup != 3) |
1462 |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, |
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, |
1463 |
backupMV.y + iDiamondSize, 4); |
backupMV.y + iDiamondSize, 4); |
1464 |
|
} |
1465 |
|
} else { |
1466 |
|
currMV->x = start_x; |
1467 |
|
currMV->y = start_y; |
1468 |
|
} |
1469 |
|
return iMinSAD; |
1470 |
|
} |
1471 |
|
|
1472 |
|
|
1473 |
|
|
1474 |
|
|
1475 |
|
int32_t |
1476 |
|
Square8_MainSearch(const uint8_t * const pRef, |
1477 |
|
const uint8_t * const pRefH, |
1478 |
|
const uint8_t * const pRefV, |
1479 |
|
const uint8_t * const pRefHV, |
1480 |
|
const uint8_t * const cur, |
1481 |
|
const int x, |
1482 |
|
const int y, |
1483 |
|
const int32_t start_x, |
1484 |
|
const int32_t start_y, |
1485 |
|
int32_t iMinSAD, |
1486 |
|
VECTOR * const currMV, |
1487 |
|
const int center_x, |
1488 |
|
const int center_y, |
1489 |
|
const int32_t min_dx, |
1490 |
|
const int32_t max_dx, |
1491 |
|
const int32_t min_dy, |
1492 |
|
const int32_t max_dy, |
1493 |
|
const int32_t iEdgedWidth, |
1494 |
|
const int32_t iDiamondSize, |
1495 |
|
const int32_t iFcode, |
1496 |
|
const int32_t iQuant, |
1497 |
|
int iFound) |
1498 |
|
{ |
1499 |
|
/* Do a square search around given starting point, return SAD of best */ |
1500 |
|
|
1501 |
|
int32_t iDirection = 0; |
1502 |
|
int32_t iSAD; |
1503 |
|
VECTOR backupMV; |
1504 |
|
|
1505 |
|
backupMV.x = start_x; |
1506 |
|
backupMV.y = start_y; |
1507 |
|
|
1508 |
|
/* It's one search with full square pattern, and new parts for all following diamonds */ |
1509 |
|
|
1510 |
|
/* new direction are extra, so 1-4 is normal diamond |
1511 |
|
537 |
1512 |
|
1*2 |
1513 |
|
648 |
1514 |
|
*/ |
1515 |
|
|
1516 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1); |
1517 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2); |
1518 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3); |
1519 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4); |
1520 |
|
|
1521 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
1522 |
|
backupMV.y - iDiamondSize, 5); |
1523 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, |
1524 |
|
backupMV.y + iDiamondSize, 6); |
1525 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
1526 |
|
backupMV.y - iDiamondSize, 7); |
1527 |
|
CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, |
1528 |
|
backupMV.y + iDiamondSize, 8); |
1529 |
|
|
1530 |
|
|
1531 |
|
if (iDirection) { |
1532 |
|
while (!iFound) { |
1533 |
|
iFound = 1; |
1534 |
|
backupMV = *currMV; |
1535 |
|
|
1536 |
|
switch (iDirection) { |
1537 |
|
case 1: |
1538 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1539 |
|
backupMV.y, 1); |
1540 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1541 |
|
backupMV.y - iDiamondSize, 5); |
1542 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1543 |
|
backupMV.y - iDiamondSize, 7); |
1544 |
|
break; |
1545 |
|
case 2: |
1546 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
1547 |
|
2); |
1548 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1549 |
|
backupMV.y + iDiamondSize, 6); |
1550 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1551 |
|
backupMV.y + iDiamondSize, 8); |
1552 |
|
break; |
1553 |
|
|
1554 |
|
case 3: |
1555 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
1556 |
|
4); |
1557 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1558 |
|
backupMV.y - iDiamondSize, 7); |
1559 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1560 |
|
backupMV.y + iDiamondSize, 8); |
1561 |
|
break; |
1562 |
|
|
1563 |
|
case 4: |
1564 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
1565 |
|
3); |
1566 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1567 |
|
backupMV.y - iDiamondSize, 5); |
1568 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1569 |
|
backupMV.y + iDiamondSize, 6); |
1570 |
|
break; |
1571 |
|
|
1572 |
|
case 5: |
1573 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y, |
1574 |
|
1); |
1575 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
1576 |
|
3); |
1577 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1578 |
|
backupMV.y - iDiamondSize, 5); |
1579 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1580 |
|
backupMV.y + iDiamondSize, 6); |
1581 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1582 |
|
backupMV.y - iDiamondSize, 7); |
1583 |
|
break; |
1584 |
|
|
1585 |
|
case 6: |
1586 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
1587 |
|
2); |
1588 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
1589 |
|
3); |
1590 |
|
|
1591 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1592 |
|
backupMV.y - iDiamondSize, 5); |
1593 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1594 |
|
backupMV.y + iDiamondSize, 6); |
1595 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1596 |
|
backupMV.y + iDiamondSize, 8); |
1597 |
|
|
1598 |
|
break; |
1599 |
|
|
1600 |
|
case 7: |
1601 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1602 |
|
backupMV.y, 1); |
1603 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
1604 |
|
4); |
1605 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1606 |
|
backupMV.y - iDiamondSize, 5); |
1607 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1608 |
|
backupMV.y - iDiamondSize, 7); |
1609 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1610 |
|
backupMV.y + iDiamondSize, 8); |
1611 |
|
break; |
1612 |
|
|
1613 |
|
case 8: |
1614 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
1615 |
|
2); |
1616 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
1617 |
|
4); |
1618 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1619 |
|
backupMV.y + iDiamondSize, 6); |
1620 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1621 |
|
backupMV.y - iDiamondSize, 7); |
1622 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1623 |
|
backupMV.y + iDiamondSize, 8); |
1624 |
|
break; |
1625 |
|
default: |
1626 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y, |
1627 |
|
1); |
1628 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y, |
1629 |
|
2); |
1630 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize, |
1631 |
|
3); |
1632 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize, |
1633 |
|
4); |
1634 |
|
|
1635 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1636 |
|
backupMV.y - iDiamondSize, 5); |
1637 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, |
1638 |
|
backupMV.y + iDiamondSize, 6); |
1639 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1640 |
|
backupMV.y - iDiamondSize, 7); |
1641 |
|
CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, |
1642 |
|
backupMV.y + iDiamondSize, 8); |
1643 |
|
break; |
1644 |
|
} |
1645 |
|
} |
1646 |
} else { |
} else { |
1647 |
currMV->x = startx; |
currMV->x = start_x; |
1648 |
currMV->y = starty; |
currMV->y = start_y; |
1649 |
} |
} |
1650 |
return iMinSAD; |
return iMinSAD; |
1651 |
} |
} |
1652 |
|
|
1653 |
|
|
1654 |
|
|
1655 |
|
|
1656 |
|
|
1657 |
int32_t |
int32_t |
1658 |
Halfpel8_Refine(const uint8_t * const pRef, |
Halfpel8_Refine_c(const uint8_t * const pRef, |
1659 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
1660 |
const uint8_t * const pRefV, |
const uint8_t * const pRefV, |
1661 |
const uint8_t * const pRefHV, |
const uint8_t * const pRefHV, |
1664 |
const int y, |
const int y, |
1665 |
VECTOR * const currMV, |
VECTOR * const currMV, |
1666 |
int32_t iMinSAD, |
int32_t iMinSAD, |
1667 |
const VECTOR * const pmv, |
const int center_x, |
1668 |
|
const int center_y, |
1669 |
const int32_t min_dx, |
const int32_t min_dx, |
1670 |
const int32_t max_dx, |
const int32_t max_dx, |
1671 |
const int32_t min_dy, |
const int32_t min_dy, |
1704 |
const int y, |
const int y, |
1705 |
const int start_x, |
const int start_x, |
1706 |
const int start_y, |
const int start_y, |
1707 |
|
const int center_x, |
1708 |
|
const int center_y, |
1709 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
1710 |
const uint32_t iQuant, |
const uint32_t iQuant, |
1711 |
const uint32_t iFcode, |
const uint32_t iFcode, |
1738 |
// const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount; |
// const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount; |
1739 |
const MACROBLOCK *const prevMB = prevMBs + (x >> 1) + (y >> 1) * iWcount; |
const MACROBLOCK *const prevMB = prevMBs + (x >> 1) + (y >> 1) * iWcount; |
1740 |
|
|
1741 |
static int32_t threshA, threshB; |
int32_t threshA, threshB; |
1742 |
int32_t iFound, bPredEq; |
int32_t iFound, bPredEq; |
1743 |
int32_t iMinSAD, iSAD; |
int32_t iMinSAD, iSAD; |
1744 |
|
|
1762 |
} |
} |
1763 |
|
|
1764 |
/* because we might use IF (dx>max_dx) THEN dx=max_dx; */ |
/* because we might use IF (dx>max_dx) THEN dx=max_dx; */ |
1765 |
bPredEq = |
bPredEq = get_pmvdata2(pMBs, iWcount, 0, (x >> 1), (y >> 1), iSubBlock, pmv, psad); |
|
get_pmvdata(pMBs, (x >> 1), (y >> 1), iWcount, iSubBlock, pmv, psad); |
|
1766 |
|
|
1767 |
if ((x == 0) && (y == 0)) { |
if ((x == 0) && (y == 0)) { |
1768 |
threshA = 512 / 4; |
threshA = 512 / 4; |
1769 |
threshB = 1024 / 4; |
threshB = 1024 / 4; |
1770 |
|
|
1771 |
} else { |
} else { |
1772 |
threshA = psad[0] / 4; /* good estimate */ |
threshA = psad[0] / 4; /* good estimate? */ |
1773 |
threshB = threshA + 256 / 4; |
threshB = threshA + 256 / 4; |
1774 |
if (threshA < 512 / 4) |
if (threshA < 512 / 4) |
1775 |
threshA = 512 / 4; |
threshA = 512 / 4; |
1791 |
|
|
1792 |
// Prepare for main loop |
// Prepare for main loop |
1793 |
|
|
1794 |
// if (MotionFlags & PMV_USESQUARES8) |
if (MotionFlags & PMV_USESQUARES8) |
1795 |
// MainSearchPtr = Square8_MainSearch; |
MainSearchPtr = Square8_MainSearch; |
1796 |
// else |
else |
1797 |
|
|
1798 |
if (MotionFlags & PMV_ADVANCEDDIAMOND8) |
if (MotionFlags & PMV_ADVANCEDDIAMOND8) |
1799 |
MainSearchPtr = AdvDiamond8_MainSearch; |
MainSearchPtr = AdvDiamond8_MainSearch; |
1808 |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, |
1809 |
iEdgedWidth), iEdgedWidth); |
iEdgedWidth), iEdgedWidth); |
1810 |
iMinSAD += |
iMinSAD += |
1811 |
calc_delta_8(currMV->x - pmv[0].x, currMV->y - pmv[0].y, |
calc_delta_8(currMV->x - center_x, currMV->y - center_y, |
1812 |
(uint8_t) iFcode, iQuant); |
(uint8_t) iFcode, iQuant); |
1813 |
|
|
1814 |
if ((iMinSAD < 256 / 4) || ((MVequal(*currMV, prevMB->mvs[iSubBlock])) |
if ((iMinSAD < 256 / 4) || ((MVequal(*currMV, prevMB->mvs[iSubBlock])) |
1815 |
&& ((uint32_t) iMinSAD < |
&& ((int32_t) iMinSAD < |
1816 |
prevMB->sad8[iSubBlock]))) { |
prevMB->sad8[iSubBlock]))) { |
1817 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1818 |
goto PMVfast8_Terminate_without_Refine; |
goto PMVfast8_Terminate_without_Refine; |
1851 |
// the median prediction might be even better than mv16 |
// the median prediction might be even better than mv16 |
1852 |
|
|
1853 |
if (!MVequal(pmv[0], startMV)) |
if (!MVequal(pmv[0], startMV)) |
1854 |
CHECK_MV8_CANDIDATE(pmv[0].x, pmv[0].y); |
CHECK_MV8_CANDIDATE(center_x, center_y); |
1855 |
|
|
1856 |
// (0,0) if needed |
// (0,0) if needed |
1857 |
if (!MVzero(pmv[0])) |
if (!MVzero(pmv[0])) |
1867 |
|
|
1868 |
if ((iMinSAD <= threshA) || |
if ((iMinSAD <= threshA) || |
1869 |
(MVequal(*currMV, prevMB->mvs[iSubBlock]) && |
(MVequal(*currMV, prevMB->mvs[iSubBlock]) && |
1870 |
((uint32_t) iMinSAD < prevMB->sad8[iSubBlock]))) { |
((int32_t) iMinSAD < prevMB->sad8[iSubBlock]))) { |
1871 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1872 |
goto PMVfast8_Terminate_without_Refine; |
goto PMVfast8_Terminate_without_Refine; |
1873 |
if (MotionFlags & PMV_EARLYSTOP16) |
if (MotionFlags & PMV_EARLYSTOP16) |
1926 |
|
|
1927 |
if ((iMinSAD <= threshA) || |
if ((iMinSAD <= threshA) || |
1928 |
(MVequal(*currMV, prevMB->mvs[iSubBlock]) && |
(MVequal(*currMV, prevMB->mvs[iSubBlock]) && |
1929 |
((uint32_t) iMinSAD < prevMB->sad8[iSubBlock]))) { |
((int32_t) iMinSAD < prevMB->sad8[iSubBlock]))) { |
1930 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1931 |
goto PMVfast8_Terminate_without_Refine; |
goto PMVfast8_Terminate_without_Refine; |
1932 |
if (MotionFlags & PMV_EARLYSTOP16) |
if (MotionFlags & PMV_EARLYSTOP16) |
1948 |
/* 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 */ |
1949 |
iSAD = |
iSAD = |
1950 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
1951 |
currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx, |
currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, |
1952 |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
1953 |
iQuant, iFound); |
iQuant, iFound); |
1954 |
|
|
1963 |
if (!(MVequal(pmv[0], backupMV))) { |
if (!(MVequal(pmv[0], backupMV))) { |
1964 |
iSAD = |
iSAD = |
1965 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
1966 |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv, |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y, |
1967 |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
1968 |
iDiamondSize, iFcode, iQuant, iFound); |
iDiamondSize, iFcode, iQuant, iFound); |
1969 |
|
|
1976 |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
1977 |
iSAD = |
iSAD = |
1978 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
1979 |
iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy, |
iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy, |
1980 |
max_dy, iEdgedWidth, iDiamondSize, iFcode, |
max_dy, iEdgedWidth, iDiamondSize, iFcode, |
1981 |
iQuant, iFound); |
iQuant, iFound); |
1982 |
|
|
1995 |
if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step |
if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step |
1996 |
iMinSAD = |
iMinSAD = |
1997 |
Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
1998 |
iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy, |
iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy, |
1999 |
iFcode, iQuant, iEdgedWidth); |
iFcode, iQuant, iEdgedWidth); |
2000 |
|
|
2001 |
|
|
2002 |
PMVfast8_Terminate_without_Refine: |
PMVfast8_Terminate_without_Refine: |
2003 |
currPMV->x = currMV->x - pmv[0].x; |
currPMV->x = currMV->x - center_x; |
2004 |
currPMV->y = currMV->y - pmv[0].y; |
currPMV->y = currMV->y - center_y; |
2005 |
|
|
2006 |
return iMinSAD; |
return iMinSAD; |
2007 |
} |
} |
2014 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
2015 |
const int x, |
const int x, |
2016 |
const int y, |
const int y, |
2017 |
|
const int start_x, |
2018 |
|
const int start_y, |
2019 |
|
const int center_x, |
2020 |
|
const int center_y, |
2021 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
2022 |
const uint32_t iQuant, |
const uint32_t iQuant, |
2023 |
const uint32_t iFcode, |
const uint32_t iFcode, |
2053 |
const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount; |
const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount; |
2054 |
MACROBLOCK *oldMB = NULL; |
MACROBLOCK *oldMB = NULL; |
2055 |
|
|
2056 |
static int32_t thresh2; |
int32_t thresh2; |
2057 |
int32_t bPredEq; |
int32_t bPredEq; |
2058 |
int32_t iMinSAD, iSAD = 9999; |
int32_t iMinSAD, iSAD = 9999; |
2059 |
|
|
2076 |
max_dy = EVEN(max_dy); |
max_dy = EVEN(max_dy); |
2077 |
} |
} |
2078 |
/* 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; */ |
2079 |
bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad); |
bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad); |
2080 |
|
|
2081 |
/* Step 4: Calculate SAD around the Median prediction. |
/* Step 4: Calculate SAD around the Median prediction. |
2082 |
MinSAD=SAD |
MinSAD=SAD |
2087 |
|
|
2088 |
// Prepare for main loop |
// Prepare for main loop |
2089 |
|
|
2090 |
*currMV = pmv[0]; /* current best := median prediction */ |
currMV->x = start_x; |
2091 |
|
currMV->y = start_y; |
2092 |
|
|
2093 |
if (!(MotionFlags & PMV_HALFPEL16)) { |
if (!(MotionFlags & PMV_HALFPEL16)) { |
2094 |
currMV->x = EVEN(currMV->x); |
currMV->x = EVEN(currMV->x); |
2095 |
currMV->y = EVEN(currMV->y); |
currMV->y = EVEN(currMV->y); |
2111 |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, |
2112 |
iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); |
iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); |
2113 |
iMinSAD += |
iMinSAD += |
2114 |
calc_delta_16(currMV->x - pmv[0].x, currMV->y - pmv[0].y, |
calc_delta_16(currMV->x - center_x, currMV->y - center_y, |
2115 |
(uint8_t) iFcode, iQuant); |
(uint8_t) iFcode, iQuant); |
2116 |
|
|
2117 |
// thresh1 is fixed to 256 |
// thresh1 is fixed to 256 |
2118 |
if ((iMinSAD < 256) || |
if ((iMinSAD < 256) || |
2119 |
((MVequal(*currMV, prevMB->mvs[0])) && |
((MVequal(*currMV, prevMB->mvs[0])) && |
2120 |
((uint32_t) iMinSAD < prevMB->sad16))) { |
((int32_t) iMinSAD < prevMB->sad16))) { |
2121 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
2122 |
goto EPZS16_Terminate_without_Refine; |
goto EPZS16_Terminate_without_Refine; |
2123 |
if (MotionFlags & PMV_EARLYSTOP16) |
if (MotionFlags & PMV_EARLYSTOP16) |
2177 |
|
|
2178 |
if ((iMinSAD <= thresh2) |
if ((iMinSAD <= thresh2) |
2179 |
|| (MVequal(*currMV, prevMB->mvs[0]) && |
|| (MVequal(*currMV, prevMB->mvs[0]) && |
2180 |
((uint32_t) iMinSAD <= prevMB->sad16))) { |
((int32_t) iMinSAD <= prevMB->sad16))) { |
2181 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
2182 |
goto EPZS16_Terminate_without_Refine; |
goto EPZS16_Terminate_without_Refine; |
2183 |
if (MotionFlags & PMV_EARLYSTOP16) |
if (MotionFlags & PMV_EARLYSTOP16) |
2223 |
|
|
2224 |
backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */ |
backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */ |
2225 |
|
|
2226 |
if (MotionFlags & PMV_USESQUARES8) |
if (MotionFlags & PMV_USESQUARES16) |
2227 |
MainSearchPtr = Square16_MainSearch; |
MainSearchPtr = Square16_MainSearch; |
2228 |
else |
else |
2229 |
if (MotionFlags & PMV_ADVANCEDDIAMOND8) |
if (MotionFlags & PMV_ADVANCEDDIAMOND16) |
2230 |
MainSearchPtr = AdvDiamond16_MainSearch; |
MainSearchPtr = AdvDiamond16_MainSearch; |
2231 |
else |
else |
2232 |
MainSearchPtr = Diamond16_MainSearch; |
MainSearchPtr = Diamond16_MainSearch; |
2235 |
|
|
2236 |
iSAD = |
iSAD = |
2237 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
2238 |
currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx, |
currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, |
2239 |
min_dy, max_dy, iEdgedWidth, 2, iFcode, iQuant, 0); |
min_dy, max_dy, iEdgedWidth, 2, iFcode, iQuant, 0); |
2240 |
|
|
2241 |
if (iSAD < iMinSAD) { |
if (iSAD < iMinSAD) { |
2250 |
if (!(MVequal(pmv[0], backupMV))) { |
if (!(MVequal(pmv[0], backupMV))) { |
2251 |
iSAD = |
iSAD = |
2252 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
2253 |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv, |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y, |
2254 |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
2255 |
2, iFcode, iQuant, 0); |
2, iFcode, iQuant, 0); |
2256 |
} |
} |
2263 |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
2264 |
iSAD = |
iSAD = |
2265 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
2266 |
iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy, |
iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy, |
2267 |
max_dy, iEdgedWidth, 2, iFcode, iQuant, 0); |
max_dy, iEdgedWidth, 2, iFcode, iQuant, 0); |
2268 |
|
|
2269 |
if (iSAD < iMinSAD) { |
if (iSAD < iMinSAD) { |
2279 |
if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step |
if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step |
2280 |
iMinSAD = |
iMinSAD = |
2281 |
Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
2282 |
iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy, |
iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy, |
2283 |
iFcode, iQuant, iEdgedWidth); |
iFcode, iQuant, iEdgedWidth); |
2284 |
|
|
2285 |
EPZS16_Terminate_without_Refine: |
EPZS16_Terminate_without_Refine: |
2286 |
|
|
2287 |
*oldMB = *prevMB; |
*oldMB = *prevMB; |
2288 |
|
|
2289 |
currPMV->x = currMV->x - pmv[0].x; |
currPMV->x = currMV->x - center_x; |
2290 |
currPMV->y = currMV->y - pmv[0].y; |
currPMV->y = currMV->y - center_y; |
2291 |
return iMinSAD; |
return iMinSAD; |
2292 |
} |
} |
2293 |
|
|
2302 |
const int y, |
const int y, |
2303 |
const int start_x, |
const int start_x, |
2304 |
const int start_y, |
const int start_y, |
2305 |
|
const int center_x, |
2306 |
|
const int center_y, |
2307 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
2308 |
const uint32_t iQuant, |
const uint32_t iQuant, |
2309 |
const uint32_t iFcode, |
const uint32_t iFcode, |
2358 |
max_dy = EVEN(max_dy); |
max_dy = EVEN(max_dy); |
2359 |
} |
} |
2360 |
/* 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; */ |
2361 |
bPredEq = get_pmvdata(pMBs, x >> 1, y >> 1, iWcount, iSubBlock, pmv, psad); |
bPredEq = get_pmvdata2(pMBs, iWcount, 0, x >> 1, y >> 1, iSubBlock, pmv, psad); |
2362 |
|
|
2363 |
|
|
2364 |
/* Step 4: Calculate SAD around the Median prediction. |
/* Step 4: Calculate SAD around the Median prediction. |
2393 |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, |
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, |
2394 |
iEdgedWidth), iEdgedWidth); |
iEdgedWidth), iEdgedWidth); |
2395 |
iMinSAD += |
iMinSAD += |
2396 |
calc_delta_8(currMV->x - pmv[0].x, currMV->y - pmv[0].y, |
calc_delta_8(currMV->x - center_x, currMV->y - center_y, |
2397 |
(uint8_t) iFcode, iQuant); |
(uint8_t) iFcode, iQuant); |
2398 |
|
|
2399 |
|
|
2469 |
|
|
2470 |
// there is no EPZS^2 for inter4v at the moment |
// there is no EPZS^2 for inter4v at the moment |
2471 |
|
|
2472 |
// if (MotionFlags & PMV_USESQUARES8) |
if (MotionFlags & PMV_USESQUARES8) |
2473 |
// MainSearchPtr = Square8_MainSearch; |
MainSearchPtr = Square8_MainSearch; |
2474 |
// else |
else |
2475 |
|
|
2476 |
if (MotionFlags & PMV_ADVANCEDDIAMOND8) |
if (MotionFlags & PMV_ADVANCEDDIAMOND8) |
2477 |
MainSearchPtr = AdvDiamond8_MainSearch; |
MainSearchPtr = AdvDiamond8_MainSearch; |
2480 |
|
|
2481 |
iSAD = |
iSAD = |
2482 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
2483 |
currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx, |
currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, |
2484 |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
2485 |
iQuant, 0); |
iQuant, 0); |
2486 |
|
|
2496 |
if (!(MVequal(pmv[0], backupMV))) { |
if (!(MVequal(pmv[0], backupMV))) { |
2497 |
iSAD = |
iSAD = |
2498 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
2499 |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv, |
pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y, |
2500 |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
2501 |
iDiamondSize, iFcode, iQuant, 0); |
iDiamondSize, iFcode, iQuant, 0); |
2502 |
|
|
2509 |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
2510 |
iSAD = |
iSAD = |
2511 |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
2512 |
iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy, |
iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy, |
2513 |
max_dy, iEdgedWidth, iDiamondSize, iFcode, |
max_dy, iEdgedWidth, iDiamondSize, iFcode, |
2514 |
iQuant, 0); |
iQuant, 0); |
2515 |
|
|
2526 |
if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step |
if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step |
2527 |
iMinSAD = |
iMinSAD = |
2528 |
Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
2529 |
iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy, |
iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy, |
2530 |
iFcode, iQuant, iEdgedWidth); |
iFcode, iQuant, iEdgedWidth); |
2531 |
|
|
2532 |
EPZS8_Terminate_without_Refine: |
EPZS8_Terminate_without_Refine: |
2533 |
|
|
2534 |
currPMV->x = currMV->x - pmv[0].x; |
currPMV->x = currMV->x - center_x; |
2535 |
currPMV->y = currMV->y - pmv[0].y; |
currPMV->y = currMV->y - center_y; |
2536 |
return iMinSAD; |
return iMinSAD; |
2537 |
} |
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* *********************************************************** |
|
|
bvop motion estimation |
|
|
// TODO: need to incorporate prediction here (eg. sad += calc_delta_16) |
|
|
***************************************************************/ |
|
|
|
|
|
|
|
|
void |
|
|
MotionEstimationBVOP(MBParam * const pParam, |
|
|
FRAMEINFO * const frame, |
|
|
// forward (past) reference |
|
|
const MACROBLOCK * const f_mbs, |
|
|
const IMAGE * const f_ref, |
|
|
const IMAGE * const f_refH, |
|
|
const IMAGE * const f_refV, |
|
|
const IMAGE * const f_refHV, |
|
|
// backward (future) reference |
|
|
const MACROBLOCK * const b_mbs, |
|
|
const IMAGE * const b_ref, |
|
|
const IMAGE * const b_refH, |
|
|
const IMAGE * const b_refV, |
|
|
const IMAGE * const b_refHV) |
|
|
{ |
|
|
const uint32_t mb_width = pParam->mb_width; |
|
|
const uint32_t mb_height = pParam->mb_height; |
|
|
const int32_t edged_width = pParam->edged_width; |
|
|
|
|
|
uint32_t i, j; |
|
|
|
|
|
int32_t f_sad16; |
|
|
int32_t b_sad16; |
|
|
int32_t i_sad16; |
|
|
int32_t d_sad16; |
|
|
int32_t best_sad; |
|
|
|
|
|
VECTOR pmv_dontcare; |
|
|
|
|
|
// note: i==horizontal, j==vertical |
|
|
for (j = 0; j < mb_height; j++) { |
|
|
for (i = 0; i < mb_width; i++) { |
|
|
MACROBLOCK *mb = &frame->mbs[i + j * mb_width]; |
|
|
const MACROBLOCK *f_mb = &f_mbs[i + j * mb_width]; |
|
|
const MACROBLOCK *b_mb = &b_mbs[i + j * mb_width]; |
|
|
|
|
|
if (b_mb->mode == MODE_INTER && b_mb->cbp == 0 && |
|
|
b_mb->mvs[0].x == 0 && b_mb->mvs[0].y == 0) { |
|
|
mb->mode = MODE_NOT_CODED; |
|
|
mb->mvs[0].x = 0; |
|
|
mb->mvs[0].y = 0; |
|
|
mb->b_mvs[0].x = 0; |
|
|
mb->b_mvs[0].y = 0; |
|
|
continue; |
|
|
} |
|
|
|
|
|
// forward search |
|
|
f_sad16 = |
|
|
SEARCH16(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
|
|
&frame->image, i, j, frame->motion_flags, |
|
|
frame->quant, frame->fcode, pParam, f_mbs, |
|
|
f_mbs /* todo */ , |
|
|
&mb->mvs[0], &pmv_dontcare); // ignore pmv |
|
|
|
|
|
// backward search |
|
|
b_sad16 = SEARCH16(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, &frame->image, i, j, frame->motion_flags, frame->quant, frame->bcode, pParam, b_mbs, b_mbs, /* todo */ |
|
|
&mb->b_mvs[0], &pmv_dontcare); // ignore pmv |
|
|
|
|
|
// interpolate search (simple, but effective) |
|
|
i_sad16 = 65535; |
|
|
|
|
|
/* |
|
|
x/y range somewhat buggy |
|
|
i_sad16 = |
|
|
sad16bi_c(frame->image.y + i * 16 + j * 16 * edged_width, |
|
|
get_ref(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
|
|
i, j, 16, mb->mvs[0].x, mb->mvs[0].y, |
|
|
edged_width), get_ref(b_ref->y, b_refH->y, |
|
|
b_refV->y, b_refHV->y, |
|
|
i, j, 16, |
|
|
mb->b_mvs[0].x, |
|
|
mb->b_mvs[0].x, |
|
|
edged_width), |
|
|
edged_width); |
|
|
*/ |
|
|
|
|
|
// TODO: direct search |
|
|
// predictor + range of [-32,32] |
|
|
d_sad16 = 65535; |
|
|
|
|
|
|
|
|
if (f_sad16 < b_sad16) { |
|
|
best_sad = f_sad16; |
|
|
mb->mode = MODE_FORWARD; |
|
|
} else { |
|
|
best_sad = b_sad16; |
|
|
mb->mode = MODE_BACKWARD; |
|
|
} |
|
|
|
|
|
if (i_sad16 < best_sad) { |
|
|
best_sad = i_sad16; |
|
|
mb->mode = MODE_INTERPOLATE; |
|
|
} |
|
|
|
|
|
if (d_sad16 < best_sad) { |
|
|
best_sad = d_sad16; |
|
|
mb->mode = MODE_DIRECT; |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|