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 */ |
50 |
#define NEIGH_TEND_8X8 40.0 |
#define NEIGH_TEND_8X8 40.0 |
51 |
#define NEIGH_8X8_BIAS 30 |
#define NEIGH_8X8_BIAS 30 |
52 |
|
|
|
#define BITS_MULT 16 |
|
|
|
|
53 |
/* Parameters which control inter/inter4v decision */ |
/* Parameters which control inter/inter4v decision */ |
54 |
#define IMV16X16 2 |
#define IMV16X16 2 |
55 |
|
|
89 |
(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), |
90 |
(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) }; |
91 |
|
|
92 |
/* mv.length table */ |
// mv.length table |
93 |
static const int mvtab[64] = { |
static const int mvtab[64] = { |
94 |
1, 2, 3, 4, 6, 7, 7, 7, |
1, 2, 3, 4, 6, 7, 7, 7, |
95 |
9, 9, 9, 10, 10, 10, 10, 10, |
9, 9, 9, 10, 10, 10, 10, 10, |
107 |
|
|
108 |
typedef struct |
typedef struct |
109 |
{ |
{ |
110 |
/* general fields */ |
// general fields |
111 |
int max_dx, min_dx, max_dy, min_dy; |
int max_dx, min_dx, max_dy, min_dy; |
112 |
uint32_t rounding; |
uint32_t rounding; |
113 |
VECTOR predMV; |
VECTOR predMV; |
114 |
VECTOR * currentMV; |
VECTOR * currentMV; |
115 |
VECTOR * currentQMV; |
VECTOR * currentQMV; |
116 |
int32_t * iMinSAD; |
int32_t * iMinSAD; |
117 |
const uint8_t * RefP[6]; /* N, V, H, HV, cU, cV */ |
const uint8_t * RefP[6]; // N, V, H, HV, cU, cV |
118 |
const uint8_t * CurU; |
const uint8_t * CurU; |
119 |
const uint8_t * CurV; |
const uint8_t * CurV; |
120 |
uint8_t * RefQ; |
uint8_t * RefQ; |
127 |
int qpel, qpel_precision; |
int qpel, qpel_precision; |
128 |
int chroma; |
int chroma; |
129 |
int rrv; |
int rrv; |
130 |
|
//fields for interpolate and direct modes |
131 |
/* 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 */ |
|
132 |
VECTOR bpredMV; |
VECTOR bpredMV; |
133 |
uint32_t bFcode; |
uint32_t bFcode; |
134 |
|
// fields for direct mode |
|
/* fields for direct mode */ |
|
135 |
VECTOR directmvF[4]; |
VECTOR directmvF[4]; |
136 |
VECTOR directmvB[4]; |
VECTOR directmvB[4]; |
137 |
const VECTOR * referencemv; |
const VECTOR * referencemv; |
138 |
|
// _BITS stuff |
|
/* BITS/R-D stuff */ |
|
139 |
int16_t * dctSpace; |
int16_t * dctSpace; |
|
uint32_t iQuant; |
|
|
uint32_t quant_type; |
|
|
int * cbp; |
|
140 |
|
|
141 |
} SearchData; |
} SearchData; |
142 |
|
|
206 |
const IMAGE * const pRefH, |
const IMAGE * const pRefH, |
207 |
const IMAGE * const pRefV, |
const IMAGE * const pRefV, |
208 |
const IMAGE * const pRefHV, |
const IMAGE * const pRefHV, |
|
const IMAGE * const pGMC, |
|
209 |
const uint32_t iLimit); |
const uint32_t iLimit); |
210 |
|
|
211 |
static void |
static void |
217 |
const int x, |
const int x, |
218 |
const int y, |
const int y, |
219 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
220 |
const uint32_t VopFlags, |
const uint32_t GlobalFlags, |
221 |
const uint32_t VolFlags, |
const uint32_t iQuant, |
222 |
SearchData * const Data, |
SearchData * const Data, |
223 |
const MBParam * const pParam, |
const MBParam * const pParam, |
224 |
const MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
225 |
const MACROBLOCK * const prevMBs, |
const MACROBLOCK * const prevMBs, |
226 |
|
int inter4v, |
227 |
MACROBLOCK * const pMB); |
MACROBLOCK * const pMB); |
228 |
|
|
|
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 ); |
|
|
|
|
229 |
|
|
230 |
int |
static WARPPOINTS |
231 |
GlobalMotionEstRefine(WARPPOINTS *const startwp, |
GlobalMotionEst(const MACROBLOCK * const pMBs, |
|
MACROBLOCK * const pMBs, |
|
232 |
const MBParam * const pParam, |
const MBParam * const pParam, |
233 |
const FRAMEINFO * const current, |
const FRAMEINFO * const current, |
234 |
const FRAMEINFO * const reference, |
const FRAMEINFO * const reference, |
|
const IMAGE * const pCurr, |
|
|
const IMAGE * const pRef, |
|
235 |
const IMAGE * const pRefH, |
const IMAGE * const pRefH, |
236 |
const IMAGE * const pRefV, |
const IMAGE * const pRefV, |
237 |
const IMAGE * const pRefHV); |
const IMAGE * const pRefHV); |
238 |
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
239 |
#define iDiamondSize 2 |
#define iDiamondSize 2 |
240 |
|
|
241 |
static __inline uint32_t |
static __inline uint32_t |
242 |
MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags) |
MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t GlobalFlags) |
243 |
{ |
{ |
244 |
uint32_t Flags = MotionFlags; |
uint32_t Flags = MotionFlags; |
245 |
|
|
246 |
if (!(VopFlags & XVID_VOP_MODEDECISION_RD)) |
if (!(GlobalFlags & XVID_MODEDECISION_BITS)) |
247 |
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); |
248 |
|
|
249 |
if (Flags & XVID_ME_EXTSEARCH_RD) |
if (Flags & EXTSEARCH_BITS) |
250 |
Flags |= XVID_ME_HALFPELREFINE16_RD; |
Flags |= HALFPELREFINE16_BITS; |
251 |
|
|
252 |
if (Flags & XVID_ME_EXTSEARCH_RD && MotionFlags & XVID_ME_EXTSEARCH8) |
if (Flags & EXTSEARCH_BITS && MotionFlags & PMV_EXTSEARCH8) |
253 |
Flags |= XVID_ME_HALFPELREFINE8_RD; |
Flags |= HALFPELREFINE8_BITS; |
254 |
|
|
255 |
if (Flags & XVID_ME_HALFPELREFINE16_RD) |
if (Flags & HALFPELREFINE16_BITS) |
256 |
Flags |= XVID_ME_QUARTERPELREFINE16_RD; |
Flags |= QUARTERPELREFINE16_BITS; |
257 |
|
|
258 |
if (Flags & XVID_ME_HALFPELREFINE8_RD) { |
if (Flags & HALFPELREFINE8_BITS) { |
259 |
Flags |= XVID_ME_QUARTERPELREFINE8_RD; |
Flags |= QUARTERPELREFINE8_BITS; |
260 |
Flags &= ~XVID_ME_HALFPELREFINE8; |
Flags &= ~PMV_HALFPELREFINE8; |
261 |
} |
} |
262 |
|
|
263 |
if (Flags & XVID_ME_QUARTERPELREFINE8_RD) |
if (Flags & QUARTERPELREFINE8_BITS) |
264 |
Flags &= ~XVID_ME_QUARTERPELREFINE8; |
Flags &= ~PMV_QUARTERPELREFINE8; |
265 |
|
|
266 |
if (!(VolFlags & XVID_VOL_QUARTERPEL)) |
if (!(GlobalFlags & XVID_QUARTERPEL)) |
267 |
Flags &= ~(XVID_ME_QUARTERPELREFINE16+XVID_ME_QUARTERPELREFINE8+XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD); |
Flags &= ~(PMV_QUARTERPELREFINE16+PMV_QUARTERPELREFINE8+QUARTERPELREFINE16_BITS+QUARTERPELREFINE8_BITS); |
268 |
|
|
269 |
if (!(VopFlags & XVID_VOP_HALFPEL)) |
if (!(GlobalFlags & XVID_HALFPEL)) |
270 |
Flags &= ~(XVID_ME_EXTSEARCH16+XVID_ME_HALFPELREFINE16+XVID_ME_HALFPELREFINE8+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD); |
Flags &= ~(PMV_EXTSEARCH16+PMV_HALFPELREFINE16+PMV_HALFPELREFINE8+HALFPELREFINE16_BITS+HALFPELREFINE8_BITS); |
271 |
|
|
272 |
if ((VopFlags & XVID_VOP_GREYSCALE) || (VopFlags & XVID_VOP_REDUCED)) |
if (GlobalFlags & (XVID_GREYSCALE + XVID_REDUCED)) |
273 |
Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP); |
Flags &= ~(PMV_CHROMA16 + PMV_CHROMA8); |
274 |
|
|
275 |
return Flags; |
return Flags; |
276 |
} |
} |
277 |
|
|
278 |
/* RD mode decision and search */ |
/* BITS mode decision and search */ |
279 |
|
|
280 |
#include "../bitstream/zigzag.h" |
#include "../bitstream/zigzag.h" |
281 |
#include "../quant/quant_mpeg4.h" |
#include "../quant/quant_mpeg4.h" |
282 |
#include "../quant/quant_h263.h" |
#include "../quant/quant_h263.h" |
283 |
#include "../bitstream/vlc_codes.h" |
#include "../bitstream/vlc_codes.h" |
|
#include "../dct/fdct.h" |
|
284 |
|
|
285 |
static int |
static int |
286 |
CountMBBitsInter(SearchData * const Data, |
CountMBBitsInter(SearchData * const Data, |
298 |
static int |
static int |
299 |
CountMBBitsIntra(const SearchData * const Data); |
CountMBBitsIntra(const SearchData * const Data); |
300 |
|
|
|
static int |
|
|
CountMBBitsGMC(const SearchData * const Data, const IMAGE * const vGMC, const int x, const int y); |
|
|
|
|
301 |
int CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); |
int CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); |
302 |
int CodeCoeffInter_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); |
int CodeCoeffInter_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); |
303 |
|
|
|
/* one over lambda for R-D mode decision and motion search */ |
|
|
#define LAMBDA ( (int)(BITS_MULT/1.0) ) |
|
|
|
|
|
static __inline unsigned int |
|
|
Block_CalcBits( int16_t * const coeff, |
|
|
int16_t * const data, |
|
|
int16_t * const dqcoeff, |
|
|
const uint32_t quant, const int quant_type, |
|
|
uint32_t * cbp, |
|
|
const int block) |
|
|
{ |
|
|
int sum; |
|
|
int bits; |
|
|
int distortion = 0; |
|
|
int i; |
|
|
|
|
|
fdct(data); |
|
|
|
|
|
if (quant_type) sum = quant_inter(coeff, data, quant); |
|
|
else sum = quant4_inter(coeff, data, quant); |
|
|
|
|
|
if (sum > 0) { |
|
|
*cbp |= 1 << (5 - block); |
|
|
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
|
|
|
if (quant_type) dequant_inter(dqcoeff, coeff, quant); |
|
|
else dequant4_inter(dqcoeff, coeff, quant); |
|
|
|
|
|
for (i = 0; i < 64; i++) |
|
|
distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]); |
|
|
|
|
|
} else { |
|
|
bits = 0; |
|
|
for (i = 0; i < 64; i++) |
|
|
distortion += data[i]*data[i]; |
|
|
} |
|
|
|
|
|
|
|
|
bits += (LAMBDA*distortion)/(quant*quant); |
|
|
|
|
|
return bits; |
|
|
} |
|
|
|
|
|
static __inline unsigned int |
|
|
Block_CalcBitsIntra(int16_t * const coeff, |
|
|
int16_t * const data, |
|
|
int16_t * const dqcoeff, |
|
|
const uint32_t quant, const int quant_type, |
|
|
uint32_t * cbp, |
|
|
const int block, |
|
|
int * dcpred) |
|
|
{ |
|
|
int bits, i; |
|
|
int distortion = 0; |
|
|
uint32_t iDcScaler = get_dc_scaler(quant, block < 4); |
|
|
int b_dc; |
|
|
|
|
|
fdct(data); |
|
|
data[0] -= 1024; |
|
|
|
|
|
if (quant_type) quant_intra(coeff, data, quant, iDcScaler); |
|
|
else quant4_intra(coeff, data, quant, iDcScaler); |
|
|
|
|
|
b_dc = coeff[0]; |
|
|
if (block < 4) { |
|
|
coeff[0] -= *dcpred; |
|
|
*dcpred = b_dc; |
|
|
} |
|
|
|
|
|
bits = BITS_MULT*CodeCoeffIntra_CalcBits(coeff, scan_tables[0]); |
|
|
if (bits != 0) *cbp |= 1 << (5 - block); |
|
|
|
|
|
if (block < 4) bits += BITS_MULT*dcy_tab[coeff[0] + 255].len; |
|
|
else bits += BITS_MULT*dcc_tab[coeff[0] + 255].len; |
|
|
|
|
|
coeff[0] = b_dc; |
|
|
if (quant_type) dequant_intra(dqcoeff, coeff, quant, iDcScaler); |
|
|
else dequant4_intra(dqcoeff, coeff, quant, iDcScaler); |
|
|
|
|
|
for (i = 0; i < 64; i++) { |
|
|
distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]); |
|
|
} |
|
|
|
|
|
bits += (LAMBDA*distortion)/(quant*quant); |
|
|
|
|
|
return bits; |
|
|
} |
|
|
|
|
304 |
#endif /* _MOTION_EST_H_ */ |
#endif /* _MOTION_EST_H_ */ |