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