1 |
/***************************************************************************** |
/************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Motion Estimation related header - |
* - Motion estimation header - |
5 |
* |
* |
6 |
* Copyright(C) 2002 Christoph Lampert <gruel@web.de> |
* This program is an implementation of a part of one or more MPEG-4 |
7 |
* 2002 Michael Militzer <michael@xvid.org> |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
8 |
* 2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net> |
* to use this software module in hardware or software products are |
9 |
|
* advised that its use may infringe existing patents or copyrights, and |
10 |
|
* any such use would be at such party's own risk. The original |
11 |
|
* developer of this software module and his/her company, and subsequent |
12 |
|
* editors and their companies, will have no liability for use of this |
13 |
|
* software or modifications or derivatives thereof. |
14 |
* |
* |
15 |
* This program is free software ; you can redistribute it and/or modify |
* This program is free software ; you can redistribute it and/or modify |
16 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
28 |
* |
* |
29 |
* $Id$ |
* $Id$ |
30 |
* |
* |
31 |
****************************************************************************/ |
***************************************************************************/ |
32 |
|
|
33 |
#ifndef _MOTION_EST_H_ |
#ifndef _MOTION_EST_H_ |
34 |
#define _MOTION_EST_H_ |
#define _MOTION_EST_H_ |
39 |
|
|
40 |
/* hard coded motion search parameters for motion_est and smp_motion_est */ |
/* hard coded motion search parameters for motion_est and smp_motion_est */ |
41 |
|
|
42 |
/* very large value */ |
// very large value |
43 |
#define MV_MAX_ERROR (4096 * 256) |
#define MV_MAX_ERROR (4096 * 256) |
44 |
|
|
45 |
/* INTER bias for INTER/INTRA decision; mpeg4 spec suggests 2*nb */ |
/* INTER bias for INTER/INTRA decision; mpeg4 spec suggests 2*nb */ |
91 |
(int)(21.2656*NEIGH_TEND_8X8 + 0.5), (int)(24.8580*NEIGH_TEND_8X8 + 0.5), |
(int)(21.2656*NEIGH_TEND_8X8 + 0.5), (int)(24.8580*NEIGH_TEND_8X8 + 0.5), |
92 |
(int)(29.6436*NEIGH_TEND_8X8 + 0.5), (int)(36.4949*NEIGH_TEND_8X8 + 0.5) }; |
(int)(29.6436*NEIGH_TEND_8X8 + 0.5), (int)(36.4949*NEIGH_TEND_8X8 + 0.5) }; |
93 |
|
|
94 |
/* mv.length table */ |
// mv.length table |
95 |
static const int mvtab[64] = { |
static const int mvtab[64] = { |
96 |
1, 2, 3, 4, 6, 7, 7, 7, |
1, 2, 3, 4, 6, 7, 7, 7, |
97 |
9, 9, 9, 10, 10, 10, 10, 10, |
9, 9, 9, 10, 10, 10, 10, 10, |
109 |
|
|
110 |
typedef struct |
typedef struct |
111 |
{ |
{ |
112 |
/* general fields */ |
// general fields |
113 |
int max_dx, min_dx, max_dy, min_dy; |
int max_dx, min_dx, max_dy, min_dy; |
114 |
uint32_t rounding; |
uint32_t rounding; |
115 |
VECTOR predMV; |
VECTOR predMV; |
116 |
VECTOR * currentMV; |
VECTOR * currentMV; |
117 |
VECTOR * currentQMV; |
VECTOR * currentQMV; |
|
VECTOR * currentMV2; |
|
|
VECTOR * currentQMV2; |
|
118 |
int32_t * iMinSAD; |
int32_t * iMinSAD; |
119 |
int32_t * iMinSAD2; |
const uint8_t * RefP[6]; // N, V, H, HV, cU, cV |
|
const uint8_t * RefP[6]; /* N, V, H, HV, cU, cV */ |
|
120 |
const uint8_t * CurU; |
const uint8_t * CurU; |
121 |
const uint8_t * CurV; |
const uint8_t * CurV; |
122 |
uint8_t * RefQ; |
uint8_t * RefQ; |
126 |
uint32_t iEdgedWidth; |
uint32_t iEdgedWidth; |
127 |
uint32_t iFcode; |
uint32_t iFcode; |
128 |
int * temp; |
int * temp; |
|
int * dir; |
|
129 |
int qpel, qpel_precision; |
int qpel, qpel_precision; |
130 |
int chroma; |
int chroma; |
131 |
int rrv; |
int rrv; |
132 |
|
//fields for interpolate and direct modes |
133 |
/* fields for interpolate and direct modes */ |
const uint8_t * b_RefP[6]; // N, V, H, HV, cU, cV |
|
const uint8_t * b_RefP[6]; /* N, V, H, HV, cU, cV */ |
|
134 |
VECTOR bpredMV; |
VECTOR bpredMV; |
135 |
uint32_t bFcode; |
uint32_t bFcode; |
136 |
|
// fields for direct mode |
|
/* fields for direct mode */ |
|
137 |
VECTOR directmvF[4]; |
VECTOR directmvF[4]; |
138 |
VECTOR directmvB[4]; |
VECTOR directmvB[4]; |
139 |
const VECTOR * referencemv; |
const VECTOR * referencemv; |
140 |
|
// BITS/R-D stuff |
|
/* BITS/R-D stuff */ |
|
141 |
int16_t * dctSpace; |
int16_t * dctSpace; |
142 |
uint32_t iQuant; |
uint32_t iQuant; |
143 |
uint32_t quant_type; |
uint32_t quant_type; |
|
int * cbp; |
|
144 |
|
|
145 |
} SearchData; |
} SearchData; |
146 |
|
|
147 |
|
|
148 |
typedef void(CheckFunc)(const int x, const int y, |
typedef void(CheckFunc)(const int x, const int y, |
149 |
const SearchData * const Data, |
const int Direction, int * const dir, |
150 |
const int Direction); |
const SearchData * const Data); |
151 |
|
CheckFunc *CheckCandidate; |
152 |
|
|
153 |
/* |
/* |
154 |
* Calculate the min/max range |
* Calculate the min/max range |
161 |
int32_t * const max_dy, |
int32_t * const max_dy, |
162 |
const uint32_t x, |
const uint32_t x, |
163 |
const uint32_t y, |
const uint32_t y, |
164 |
uint32_t block_sz, /* block dimension, 3(8) or 4(16) */ |
uint32_t block_sz, /* block dimension, 8 or 16 */ |
165 |
const uint32_t width, |
const uint32_t width, |
166 |
const uint32_t height, |
const uint32_t height, |
167 |
const uint32_t fcode, |
const uint32_t fcode, |
168 |
const int precision, /* 2 for qpel, 1 for halfpel */ |
const int qpel, /* 1 if the resulting range should be in qpel precision; otherwise 0 */ |
169 |
const int rrv) |
const int rrv) |
170 |
{ |
{ |
171 |
int k; |
int k, m = qpel ? 4 : 2; |
172 |
const int search_range = 16 << fcode; |
const int search_range = 32 << (fcode - 1); |
173 |
int high = search_range - 1; |
int high = search_range - 1; |
174 |
int low = -search_range; |
int low = -search_range; |
175 |
|
|
176 |
if (rrv) { |
if (rrv) { |
177 |
high = RRV_MV_SCALEUP(high); |
high = RRV_MV_SCALEUP(high); |
178 |
low = RRV_MV_SCALEUP(low); |
low = RRV_MV_SCALEUP(low); |
179 |
block_sz++; |
block_sz *= 2; |
180 |
} |
} |
181 |
|
|
182 |
k = (int)(width - (x<<block_sz))<<precision; |
k = m * (int)(width - x * block_sz); |
183 |
*max_dx = MIN(high, k); |
*max_dx = MIN(high, k); |
184 |
k = (int)(height - (y<<block_sz))<<precision; |
k = m * (int)(height - y * block_sz); |
185 |
*max_dy = MIN(high, k); |
*max_dy = MIN(high, k); |
186 |
|
|
187 |
k = (-(int)((x+1)<<block_sz))<<precision; |
k = -m * (int)((x+1) * block_sz); |
188 |
*min_dx = MAX(low, k); |
*min_dx = MAX(low, k); |
189 |
k = (-(int)((y+1)<<block_sz))<<precision; |
k = -m * (int)((y+1) * block_sz); |
190 |
*min_dy = MAX(low, k); |
*min_dy = MAX(low, k); |
191 |
} |
} |
192 |
|
|
193 |
typedef void |
typedef void MainSearchFunc(int x, int y, const SearchData * const Data, int bDirection); |
|
MainSearchFunc(int x, int y, const SearchData * const Data, |
|
|
int bDirection, CheckFunc * const CheckCandidate); |
|
194 |
|
|
195 |
static MainSearchFunc DiamondSearch, AdvDiamondSearch, SquareSearch; |
static MainSearchFunc DiamondSearch, AdvDiamondSearch, SquareSearch; |
196 |
|
|
210 |
const IMAGE * const pRefH, |
const IMAGE * const pRefH, |
211 |
const IMAGE * const pRefV, |
const IMAGE * const pRefV, |
212 |
const IMAGE * const pRefHV, |
const IMAGE * const pRefHV, |
|
const IMAGE * const pGMC, |
|
213 |
const uint32_t iLimit); |
const uint32_t iLimit); |
214 |
|
|
215 |
static void |
static void |
221 |
const int x, |
const int x, |
222 |
const int y, |
const int y, |
223 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
224 |
const uint32_t VopFlags, |
const uint32_t GlobalFlags, |
|
const uint32_t VolFlags, |
|
225 |
SearchData * const Data, |
SearchData * const Data, |
226 |
const MBParam * const pParam, |
const MBParam * const pParam, |
227 |
const MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
228 |
const MACROBLOCK * const prevMBs, |
const MACROBLOCK * const prevMBs, |
229 |
MACROBLOCK * const pMB); |
MACROBLOCK * const pMB); |
230 |
|
|
|
static __inline void |
|
|
GMEanalyzeMB (const uint8_t * const pCur, |
|
|
const uint8_t * const pRef, |
|
|
const uint8_t * const pRefH, |
|
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const int x, |
|
|
const int y, |
|
|
const MBParam * const pParam, |
|
|
MACROBLOCK * const pMBs, |
|
|
SearchData * const Data); |
|
|
|
|
|
void |
|
|
GMEanalysis(const MBParam * const pParam, |
|
|
const FRAMEINFO * const current, |
|
|
const FRAMEINFO * const reference, |
|
|
const IMAGE * const pRefH, |
|
|
const IMAGE * const pRefV, |
|
|
const IMAGE * const pRefHV); |
|
|
|
|
|
|
|
|
|
|
|
WARPPOINTS |
|
|
GlobalMotionEst(MACROBLOCK * const pMBs, |
|
|
const MBParam * const pParam, |
|
|
const FRAMEINFO * const current, |
|
|
const FRAMEINFO * const reference, |
|
|
const IMAGE * const pRefH, |
|
|
const IMAGE * const pRefV, |
|
|
const IMAGE * const pRefHV ); |
|
|
|
|
231 |
|
|
232 |
int |
static WARPPOINTS |
233 |
GlobalMotionEstRefine(WARPPOINTS *const startwp, |
GlobalMotionEst(const MACROBLOCK * const pMBs, |
|
MACROBLOCK * const pMBs, |
|
234 |
const MBParam * const pParam, |
const MBParam * const pParam, |
235 |
const FRAMEINFO * const current, |
const FRAMEINFO * const current, |
236 |
const FRAMEINFO * const reference, |
const FRAMEINFO * const reference, |
|
const IMAGE * const pCurr, |
|
|
const IMAGE * const pRef, |
|
237 |
const IMAGE * const pRefH, |
const IMAGE * const pRefH, |
238 |
const IMAGE * const pRefV, |
const IMAGE * const pRefV, |
239 |
const IMAGE * const pRefHV); |
const IMAGE * const pRefHV); |
240 |
|
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
globalSAD(const WARPPOINTS *const wp, |
|
|
const MBParam * const pParam, |
|
|
const MACROBLOCK * const pMBs, |
|
|
const FRAMEINFO * const current, |
|
|
const IMAGE * const pRef, |
|
|
const IMAGE * const pCurr, |
|
|
uint8_t *const GMCblock); |
|
|
|
|
241 |
#define iDiamondSize 2 |
#define iDiamondSize 2 |
242 |
|
|
243 |
static __inline uint32_t |
static __inline uint32_t |
244 |
MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags) |
MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t GlobalFlags) |
245 |
{ |
{ |
246 |
uint32_t Flags = MotionFlags; |
uint32_t Flags = MotionFlags; |
247 |
|
|
248 |
if (!(VopFlags & XVID_VOP_MODEDECISION_RD)) |
if (!(GlobalFlags & XVID_MODEDECISION_BITS)) |
249 |
Flags &= ~(XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD+XVID_ME_EXTSEARCH_RD); |
Flags &= ~(QUARTERPELREFINE16_BITS+QUARTERPELREFINE8_BITS+HALFPELREFINE16_BITS+HALFPELREFINE8_BITS+EXTSEARCH_BITS); |
250 |
|
|
251 |
if (Flags & XVID_ME_EXTSEARCH_RD) |
if (Flags & EXTSEARCH_BITS) |
252 |
Flags |= XVID_ME_HALFPELREFINE16_RD; |
Flags |= HALFPELREFINE16_BITS; |
253 |
|
|
254 |
if (Flags & XVID_ME_EXTSEARCH_RD && MotionFlags & XVID_ME_EXTSEARCH8) |
if (Flags & EXTSEARCH_BITS && MotionFlags & PMV_EXTSEARCH8) |
255 |
Flags |= XVID_ME_HALFPELREFINE8_RD; |
Flags |= HALFPELREFINE8_BITS; |
256 |
|
|
257 |
if (Flags & XVID_ME_HALFPELREFINE16_RD) |
if (Flags & HALFPELREFINE16_BITS) |
258 |
Flags |= XVID_ME_QUARTERPELREFINE16_RD; |
Flags |= QUARTERPELREFINE16_BITS; |
259 |
|
|
260 |
if (Flags & XVID_ME_HALFPELREFINE8_RD) { |
if (Flags & HALFPELREFINE8_BITS) { |
261 |
Flags |= XVID_ME_QUARTERPELREFINE8_RD; |
Flags |= QUARTERPELREFINE8_BITS; |
262 |
Flags &= ~XVID_ME_HALFPELREFINE8; |
Flags &= ~PMV_HALFPELREFINE8; |
263 |
} |
} |
264 |
|
|
265 |
if (Flags & XVID_ME_QUARTERPELREFINE8_RD) |
if (Flags & QUARTERPELREFINE8_BITS) |
266 |
Flags &= ~XVID_ME_QUARTERPELREFINE8; |
Flags &= ~PMV_QUARTERPELREFINE8; |
267 |
|
|
268 |
if (!(VolFlags & XVID_VOL_QUARTERPEL)) |
if (Flags & QUARTERPELREFINE16_BITS) |
269 |
Flags &= ~(XVID_ME_QUARTERPELREFINE16+XVID_ME_QUARTERPELREFINE8+XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD); |
Flags &= ~PMV_QUARTERPELREFINE16; |
270 |
|
|
271 |
if (!(VopFlags & XVID_VOP_HALFPEL)) |
if (!(GlobalFlags & XVID_QUARTERPEL)) |
272 |
Flags &= ~(XVID_ME_EXTSEARCH16+XVID_ME_HALFPELREFINE16+XVID_ME_HALFPELREFINE8+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD); |
Flags &= ~(PMV_QUARTERPELREFINE16+PMV_QUARTERPELREFINE8+QUARTERPELREFINE16_BITS+QUARTERPELREFINE8_BITS); |
273 |
|
|
274 |
if ((VopFlags & XVID_VOP_GREYSCALE) || (VopFlags & XVID_VOP_REDUCED)) |
if (!(GlobalFlags & XVID_HALFPEL)) |
275 |
Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP); |
Flags &= ~(PMV_EXTSEARCH16+PMV_HALFPELREFINE16+PMV_HALFPELREFINE8+HALFPELREFINE16_BITS+HALFPELREFINE8_BITS); |
276 |
|
|
277 |
|
if (GlobalFlags & (XVID_GREYSCALE + XVID_REDUCED)) |
278 |
|
Flags &= ~(PMV_CHROMA16 + PMV_CHROMA8); |
279 |
|
|
280 |
return Flags; |
return Flags; |
281 |
} |
} |
282 |
|
|
283 |
/* RD mode decision and search */ |
/* BITS mode decision and search */ |
284 |
|
|
285 |
#include "../bitstream/zigzag.h" |
#include "../bitstream/zigzag.h" |
286 |
#include "../quant/quant_mpeg4.h" |
#include "../quant/quant_mpeg4.h" |
289 |
#include "../dct/fdct.h" |
#include "../dct/fdct.h" |
290 |
|
|
291 |
static int |
static int |
292 |
findRDinter(SearchData * const Data, |
CountMBBitsInter(SearchData * const Data, |
293 |
const MACROBLOCK * const pMBs, const int x, const int y, |
const MACROBLOCK * const pMBs, const int x, const int y, |
294 |
const MBParam * const pParam, |
const MBParam * const pParam, |
295 |
const uint32_t MotionFlags); |
const uint32_t MotionFlags); |
296 |
|
|
297 |
static int |
static int |
298 |
findRDinter4v(const SearchData * const Data, |
CountMBBitsInter4v(const SearchData * const Data, |
299 |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
300 |
const int x, const int y, |
const int x, const int y, |
301 |
const MBParam * const pParam, const uint32_t MotionFlags, |
const MBParam * const pParam, const uint32_t MotionFlags, |
302 |
const VECTOR * const backup); |
const VECTOR * const backup); |
303 |
|
|
304 |
static int |
static int |
305 |
findRDintra(const SearchData * const Data); |
CountMBBitsIntra(const SearchData * const Data); |
|
|
|
|
static int |
|
|
findRDgmc(const SearchData * const Data, const IMAGE * const vGMC, const int x, const int y); |
|
306 |
|
|
307 |
int CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); |
int CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); |
308 |
int CodeCoeffInter_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); |
int CodeCoeffInter_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); |
325 |
|
|
326 |
fdct(data); |
fdct(data); |
327 |
|
|
328 |
if (quant_type) sum = quant_inter(coeff, data, quant); |
if (quant_type == 0) sum = quant_inter(coeff, data, quant); |
329 |
else sum = quant4_inter(coeff, data, quant); |
else sum = quant4_inter(coeff, data, quant); |
330 |
|
|
331 |
if (sum > 0) { |
if (sum > 0) { |
332 |
*cbp |= 1 << (5 - block); |
*cbp |= 1 << (5 - block); |
333 |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
334 |
|
} else bits = 0; |
335 |
|
|
336 |
if (quant_type) dequant_inter(dqcoeff, coeff, quant); |
if (quant_type == 0) dequant_inter(dqcoeff, coeff, quant); |
337 |
else dequant4_inter(dqcoeff, coeff, quant); |
else dequant4_inter(dqcoeff, coeff, quant); |
338 |
|
|
339 |
for (i = 0; i < 64; i++) |
for (i = 0; i < 64; i++) { |
340 |
distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]); |
distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]); |
|
|
|
|
} else { |
|
|
bits = 0; |
|
|
for (i = 0; i < 64; i++) |
|
|
distortion += data[i]*data[i]; |
|
341 |
} |
} |
342 |
|
|
343 |
return bits + (LAMBDA*distortion)/(quant*quant); |
bits += (LAMBDA*distortion)/(quant*quant); |
344 |
|
|
345 |
|
return bits; |
346 |
} |
} |
347 |
|
|
348 |
static __inline unsigned int |
static __inline unsigned int |
362 |
fdct(data); |
fdct(data); |
363 |
data[0] -= 1024; |
data[0] -= 1024; |
364 |
|
|
365 |
if (quant_type) quant_intra(coeff, data, quant, iDcScaler); |
if (quant_type == 0) quant_intra(coeff, data, quant, iDcScaler); |
366 |
else quant4_intra(coeff, data, quant, iDcScaler); |
else quant4_intra(coeff, data, quant, iDcScaler); |
367 |
|
|
368 |
b_dc = coeff[0]; |
b_dc = coeff[0]; |
378 |
else bits += BITS_MULT*dcc_tab[coeff[0] + 255].len; |
else bits += BITS_MULT*dcc_tab[coeff[0] + 255].len; |
379 |
|
|
380 |
coeff[0] = b_dc; |
coeff[0] = b_dc; |
381 |
if (quant_type) dequant_intra(dqcoeff, coeff, quant, iDcScaler); |
if (quant_type == 0) dequant_intra(dqcoeff, coeff, quant, iDcScaler); |
382 |
else dequant4_intra(dqcoeff, coeff, quant, iDcScaler); |
else dequant4_intra(dqcoeff, coeff, quant, iDcScaler); |
383 |
|
|
384 |
for (i = 0; i < 64; i++) |
for (i = 0; i < 64; i++) { |
385 |
distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]); |
distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]); |
386 |
|
} |
387 |
|
|
388 |
|
bits += (LAMBDA*distortion)/(quant*quant); |
389 |
|
|
390 |
return bits + (LAMBDA*distortion)/(quant*quant); |
return bits; |
391 |
} |
} |
392 |
|
|
393 |
#endif /* _MOTION_EST_H_ */ |
#endif /* _MOTION_EST_H_ */ |