1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Motion estimation header - |
* - Motion Estimation related header - |
5 |
* |
* |
6 |
* This program is an implementation of a part of one or more MPEG-4 |
* Copyright(C) 2002 Christoph Lampert <gruel@web.de> |
7 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* 2002 Michael Militzer <michael@xvid.org> |
8 |
* to use this software module in hardware or software products are |
* 2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net> |
|
* advised that its use may infringe existing patents or copyrights, and |
|
|
* any such use would be at such party's own risk. The original |
|
|
* developer of this software module and his/her company, and subsequent |
|
|
* editors and their companies, will have no liability for use of this |
|
|
* software or modifications or derivatives thereof. |
|
9 |
* |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
11 |
* 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 |
23 |
* |
* |
24 |
* $Id$ |
* $Id$ |
25 |
* |
* |
26 |
***************************************************************************/ |
****************************************************************************/ |
27 |
|
|
28 |
#ifndef _MOTION_EST_H_ |
#ifndef _MOTION_EST_H_ |
29 |
#define _MOTION_EST_H_ |
#define _MOTION_EST_H_ |
34 |
|
|
35 |
/* hard coded motion search parameters for motion_est and smp_motion_est */ |
/* hard coded motion search parameters for motion_est and smp_motion_est */ |
36 |
|
|
37 |
// very large value |
/* very large value */ |
38 |
#define MV_MAX_ERROR (4096 * 256) |
#define MV_MAX_ERROR (4096 * 256) |
39 |
|
|
40 |
/* INTER bias for INTER/INTRA decision; mpeg4 spec suggests 2*nb */ |
/* INTER bias for INTER/INTRA decision; mpeg4 spec suggests 2*nb */ |
45 |
#define NEIGH_TEND_8X8 40.0 |
#define NEIGH_TEND_8X8 40.0 |
46 |
#define NEIGH_8X8_BIAS 30 |
#define NEIGH_8X8_BIAS 30 |
47 |
|
|
48 |
|
#define BITS_MULT 16 |
49 |
|
|
50 |
/* Parameters which control inter/inter4v decision */ |
/* Parameters which control inter/inter4v decision */ |
51 |
#define IMV16X16 2 |
#define IMV16X16 2 |
52 |
|
|
86 |
(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), |
87 |
(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) }; |
88 |
|
|
89 |
// mv.length table |
/* mv.length table */ |
90 |
static const uint32_t mvtab[33] = { |
static const int mvtab[64] = { |
91 |
1, 2, 3, 4, 6, 7, 7, 7, |
1, 2, 3, 4, 6, 7, 7, 7, |
92 |
9, 9, 9, 10, 10, 10, 10, 10, |
9, 9, 9, 10, 10, 10, 10, 10, |
93 |
10, 10, 10, 10, 10, 10, 10, 10, |
10, 10, 10, 10, 10, 10, 10, 10, |
94 |
10, 11, 11, 11, 11, 11, 11, 12, 12 |
10, 11, 11, 11, 11, 11, 11, 12, |
95 |
}; |
12, 12, 12, 12, 12, 12, 12, 12, |
96 |
|
12, 12, 12, 12, 12, 12, 12, 12, |
97 |
|
12, 12, 12, 12, 12, 12, 12, 12, 12 }; |
98 |
|
|
99 |
static const int DQtab[4] = { |
static const int DQtab[4] = { |
100 |
-1, -2, 1, 2 |
-1, -2, 1, 2 |
104 |
|
|
105 |
typedef struct |
typedef struct |
106 |
{ |
{ |
107 |
// general fields |
/* general fields */ |
108 |
int max_dx, min_dx, max_dy, min_dy; |
int max_dx, min_dx, max_dy, min_dy; |
109 |
uint32_t rounding; |
uint32_t rounding; |
110 |
VECTOR predMV; |
VECTOR predMV; |
111 |
VECTOR * currentMV; |
VECTOR * currentMV; |
112 |
VECTOR * currentQMV; |
VECTOR * currentQMV; |
113 |
int32_t * iMinSAD; |
int32_t * iMinSAD; |
114 |
const uint8_t * Ref; |
const uint8_t * RefP[6]; /* N, V, H, HV, cU, cV */ |
|
const uint8_t * RefH; |
|
|
const uint8_t * RefV; |
|
|
const uint8_t * RefHV; |
|
|
const uint8_t * RefCU; |
|
|
const uint8_t * RefCV; |
|
115 |
const uint8_t * CurU; |
const uint8_t * CurU; |
116 |
const uint8_t * CurV; |
const uint8_t * CurV; |
117 |
uint8_t * RefQ; |
uint8_t * RefQ; |
124 |
int qpel, qpel_precision; |
int qpel, qpel_precision; |
125 |
int chroma; |
int chroma; |
126 |
int rrv; |
int rrv; |
|
//fields for interpolate and direct modes |
|
|
const uint8_t * bRef; |
|
|
const uint8_t * bRefH; |
|
|
const uint8_t * bRefV; |
|
|
const uint8_t * bRefHV; |
|
|
const uint8_t * b_RefCU; |
|
|
const uint8_t * b_RefCV; |
|
127 |
|
|
128 |
|
/* fields for interpolate and direct modes */ |
129 |
|
const uint8_t * b_RefP[6]; /* N, V, H, HV, cU, cV */ |
130 |
VECTOR bpredMV; |
VECTOR bpredMV; |
131 |
uint32_t bFcode; |
uint32_t bFcode; |
132 |
// fields for direct mode |
|
133 |
|
/* fields for direct mode */ |
134 |
VECTOR directmvF[4]; |
VECTOR directmvF[4]; |
135 |
VECTOR directmvB[4]; |
VECTOR directmvB[4]; |
136 |
const VECTOR * referencemv; |
const VECTOR * referencemv; |
137 |
|
|
138 |
|
/* BITS/R-D stuff */ |
139 |
|
int16_t * dctSpace; |
140 |
|
uint32_t iQuant; |
141 |
|
uint32_t quant_type; |
142 |
|
int * cbp; |
143 |
|
|
144 |
} SearchData; |
} SearchData; |
145 |
|
|
146 |
|
|
160 |
int32_t * const max_dy, |
int32_t * const max_dy, |
161 |
const uint32_t x, |
const uint32_t x, |
162 |
const uint32_t y, |
const uint32_t y, |
163 |
uint32_t block_sz, /* block dimension, 8 or 16 */ |
uint32_t block_sz, /* block dimension, 3(8) or 4(16) */ |
164 |
const uint32_t width, |
const uint32_t width, |
165 |
const uint32_t height, |
const uint32_t height, |
166 |
const uint32_t fcode, |
const uint32_t fcode, |
167 |
const int qpel, /* 1 if the resulting range should be in qpel precision; otherwise 0 */ |
const int precision, /* 2 for qpel, 1 for halfpel */ |
168 |
const int rrv) |
const int rrv) |
169 |
{ |
{ |
170 |
int k, m = qpel ? 4 : 2; |
int k; |
171 |
const int search_range = 32 << (fcode - 1); |
const int search_range = 16 << fcode; |
172 |
int high = search_range - 1; |
int high = search_range - 1; |
173 |
int low = -search_range; |
int low = -search_range; |
174 |
|
|
175 |
if (rrv) { |
if (rrv) { |
176 |
high = RRV_MV_SCALEUP(high); |
high = RRV_MV_SCALEUP(high); |
177 |
low = RRV_MV_SCALEUP(low); |
low = RRV_MV_SCALEUP(low); |
178 |
block_sz *= 2; |
block_sz++; |
179 |
} |
} |
180 |
|
|
181 |
k = m * (int)(width - x * block_sz); |
k = (int)(width - (x<<block_sz))<<precision; |
182 |
*max_dx = MIN(high, k); |
*max_dx = min(high, k); |
183 |
k = m * (int)(height - y * block_sz); |
k = (int)(height - (y<<block_sz))<<precision; |
184 |
*max_dy = MIN(high, k); |
*max_dy = min(high, k); |
185 |
|
|
186 |
k = -m * (int)((x+1) * block_sz); |
k = (-(int)((x+1)<<block_sz))<<precision; |
187 |
*min_dx = MAX(low, k); |
*min_dx = max(low, k); |
188 |
k = -m * (int)((y+1) * block_sz); |
k = (-(int)((y+1)<<block_sz))<<precision; |
189 |
*min_dy = MAX(low, k); |
*min_dy = max(low, k); |
190 |
} |
} |
191 |
|
|
192 |
typedef void MainSearchFunc(int x, int y, const SearchData * const Data, int bDirection); |
typedef void MainSearchFunc(int x, int y, const SearchData * const Data, int bDirection); |
209 |
const IMAGE * const pRefH, |
const IMAGE * const pRefH, |
210 |
const IMAGE * const pRefV, |
const IMAGE * const pRefV, |
211 |
const IMAGE * const pRefHV, |
const IMAGE * const pRefHV, |
212 |
|
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 GlobalFlags, |
const uint32_t VopFlags, |
225 |
const uint32_t iQuant, |
const uint32_t VolFlags, |
226 |
SearchData * const Data, |
SearchData * const Data, |
227 |
const MBParam * const pParam, |
const MBParam * const pParam, |
228 |
const MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
229 |
const MACROBLOCK * const prevMBs, |
const MACROBLOCK * const prevMBs, |
|
int inter4v, |
|
230 |
MACROBLOCK * const pMB); |
MACROBLOCK * const pMB); |
231 |
|
|
232 |
|
static __inline void |
233 |
|
GMEanalyzeMB (const uint8_t * const pCur, |
234 |
|
const uint8_t * const pRef, |
235 |
|
const uint8_t * const pRefH, |
236 |
|
const uint8_t * const pRefV, |
237 |
|
const uint8_t * const pRefHV, |
238 |
|
const int x, |
239 |
|
const int y, |
240 |
|
const MBParam * const pParam, |
241 |
|
MACROBLOCK * const pMBs, |
242 |
|
SearchData * const Data); |
243 |
|
|
244 |
|
void |
245 |
|
GMEanalysis(const MBParam * const pParam, |
246 |
|
const FRAMEINFO * const current, |
247 |
|
const FRAMEINFO * const reference, |
248 |
|
const IMAGE * const pRefH, |
249 |
|
const IMAGE * const pRefV, |
250 |
|
const IMAGE * const pRefHV); |
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
WARPPOINTS |
255 |
|
GlobalMotionEst(MACROBLOCK * const pMBs, |
256 |
|
const MBParam * const pParam, |
257 |
|
const FRAMEINFO * const current, |
258 |
|
const FRAMEINFO * const reference, |
259 |
|
const IMAGE * const pRefH, |
260 |
|
const IMAGE * const pRefV, |
261 |
|
const IMAGE * const pRefHV ); |
262 |
|
|
263 |
|
|
264 |
static WARPPOINTS |
int |
265 |
GlobalMotionEst(const MACROBLOCK * const pMBs, |
GlobalMotionEstRefine(WARPPOINTS *const startwp, |
266 |
|
MACROBLOCK * const pMBs, |
267 |
const MBParam * const pParam, |
const MBParam * const pParam, |
268 |
const FRAMEINFO * const current, |
const FRAMEINFO * const current, |
269 |
const FRAMEINFO * const reference, |
const FRAMEINFO * const reference, |
270 |
|
const IMAGE * const pCurr, |
271 |
|
const IMAGE * const pRef, |
272 |
const IMAGE * const pRefH, |
const IMAGE * const pRefH, |
273 |
const IMAGE * const pRefV, |
const IMAGE * const pRefV, |
274 |
const IMAGE * const pRefHV ); |
const IMAGE * const pRefHV ); |
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
int |
280 |
|
globalSAD(const WARPPOINTS *const wp, |
281 |
|
const MBParam * const pParam, |
282 |
|
const MACROBLOCK * const pMBs, |
283 |
|
const FRAMEINFO * const current, |
284 |
|
const IMAGE * const pRef, |
285 |
|
const IMAGE * const pCurr, |
286 |
|
uint8_t *const GMCblock); |
287 |
|
|
288 |
#define iDiamondSize 2 |
#define iDiamondSize 2 |
289 |
|
|
290 |
static __inline uint32_t |
static __inline uint32_t |
291 |
MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t GlobalFlags) |
MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags) |
292 |
{ |
{ |
293 |
uint32_t Flags = MotionFlags; |
uint32_t Flags = MotionFlags; |
294 |
|
|
295 |
if (!(GlobalFlags & XVID_MODEDECISION_BITS)) |
if (!(VopFlags & XVID_VOP_MODEDECISION_RD)) |
296 |
Flags &= ~(QUARTERPELREFINE16_BITS+QUARTERPELREFINE8_BITS+HALFPELREFINE16_BITS+HALFPELREFINE8_BITS+EXTSEARCH_BITS); |
Flags &= ~(XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD+XVID_ME_EXTSEARCH_RD); |
297 |
|
|
298 |
if (Flags & EXTSEARCH_BITS) |
if (Flags & XVID_ME_EXTSEARCH_RD) |
299 |
Flags |= HALFPELREFINE16_BITS; |
Flags |= XVID_ME_HALFPELREFINE16_RD; |
300 |
|
|
301 |
if (Flags & EXTSEARCH_BITS && MotionFlags & PMV_EXTSEARCH8) |
if (Flags & XVID_ME_EXTSEARCH_RD && MotionFlags & XVID_ME_EXTSEARCH8) |
302 |
Flags |= HALFPELREFINE8_BITS; |
Flags |= XVID_ME_HALFPELREFINE8_RD; |
303 |
|
|
304 |
if (Flags & HALFPELREFINE16_BITS) |
if (Flags & XVID_ME_HALFPELREFINE16_RD) |
305 |
Flags |= QUARTERPELREFINE16_BITS; |
Flags |= XVID_ME_QUARTERPELREFINE16_RD; |
306 |
|
|
307 |
if (Flags & HALFPELREFINE8_BITS) { |
if (Flags & XVID_ME_HALFPELREFINE8_RD) { |
308 |
Flags |= QUARTERPELREFINE8_BITS; |
Flags |= XVID_ME_QUARTERPELREFINE8_RD; |
309 |
Flags &= ~PMV_HALFPELREFINE8; |
Flags &= ~XVID_ME_HALFPELREFINE8; |
310 |
} |
} |
311 |
|
|
312 |
if (Flags & QUARTERPELREFINE8_BITS) |
if (Flags & XVID_ME_QUARTERPELREFINE8_RD) |
313 |
Flags &= ~PMV_QUARTERPELREFINE8; |
Flags &= ~XVID_ME_QUARTERPELREFINE8; |
314 |
|
|
315 |
if (!(GlobalFlags & XVID_QUARTERPEL)) |
if (!(VolFlags & XVID_VOL_QUARTERPEL)) |
316 |
Flags &= ~(PMV_QUARTERPELREFINE16+PMV_QUARTERPELREFINE8+QUARTERPELREFINE16_BITS+QUARTERPELREFINE8_BITS); |
Flags &= ~(XVID_ME_QUARTERPELREFINE16+XVID_ME_QUARTERPELREFINE8+XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD); |
317 |
|
|
318 |
if (!(GlobalFlags & XVID_HALFPEL)) |
if (!(VopFlags & XVID_VOP_HALFPEL)) |
319 |
Flags &= ~(PMV_EXTSEARCH16+PMV_HALFPELREFINE16+PMV_HALFPELREFINE8+HALFPELREFINE16_BITS+HALFPELREFINE8_BITS); |
Flags &= ~(XVID_ME_EXTSEARCH16+XVID_ME_HALFPELREFINE16+XVID_ME_HALFPELREFINE8+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD); |
320 |
|
|
321 |
if (GlobalFlags & (XVID_GREYSCALE + XVID_REDUCED)) |
if ((VopFlags & XVID_VOP_GREYSCALE) || (VopFlags & XVID_VOP_REDUCED)) |
322 |
Flags &= ~(PMV_CHROMA16 + PMV_CHROMA8); |
Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP); |
323 |
|
|
324 |
return Flags; |
return Flags; |
325 |
} |
} |
326 |
|
|
327 |
/* BITS mode decision and search */ |
/* RD mode decision and search */ |
328 |
|
|
329 |
#include "../bitstream/zigzag.h" |
#include "../bitstream/zigzag.h" |
330 |
#include "../quant/quant_mpeg4.h" |
#include "../quant/quant_mpeg4.h" |
331 |
#include "../quant/quant_h263.h" |
#include "../quant/quant_h263.h" |
332 |
#include "../bitstream/vlc_codes.h" |
#include "../bitstream/vlc_codes.h" |
333 |
|
#include "../dct/fdct.h" |
334 |
|
|
335 |
static int |
static int |
336 |
CountMBBitsInter(SearchData * const Data, |
findRDinter(SearchData * const Data, |
337 |
const MACROBLOCK * const pMBs, const int x, const int y, |
const MACROBLOCK * const pMBs, const int x, const int y, |
338 |
const MBParam * const pParam, |
const MBParam * const pParam, |
339 |
const uint32_t MotionFlags); |
const uint32_t MotionFlags); |
340 |
|
|
341 |
static int |
static int |
342 |
CountMBBitsInter4v(const SearchData * const Data, |
findRDinter4v(const SearchData * const Data, |
343 |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
344 |
const int x, const int y, |
const int x, const int y, |
345 |
const MBParam * const pParam, const uint32_t MotionFlags, |
const MBParam * const pParam, const uint32_t MotionFlags, |
346 |
const VECTOR * const backup); |
const VECTOR * const backup); |
347 |
|
|
348 |
static int |
static int |
349 |
CountMBBitsIntra(const SearchData * const Data); |
findRDintra(const SearchData * const Data); |
350 |
|
|
351 |
|
static int |
352 |
|
findRDgmc(const SearchData * const Data, const IMAGE * const vGMC, const int x, const int y); |
353 |
|
|
354 |
int CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); |
int CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); |
355 |
int CodeCoeffInter_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); |
int CodeCoeffInter_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); |
356 |
|
|
357 |
|
/* one over lambda for R-D mode decision and motion search */ |
358 |
|
#define LAMBDA ( (int)(BITS_MULT/1.0) ) |
359 |
|
|
360 |
|
static __inline unsigned int |
361 |
|
Block_CalcBits( int16_t * const coeff, |
362 |
|
int16_t * const data, |
363 |
|
int16_t * const dqcoeff, |
364 |
|
const uint32_t quant, const int quant_type, |
365 |
|
uint32_t * cbp, |
366 |
|
const int block) |
367 |
|
{ |
368 |
|
int sum; |
369 |
|
int bits; |
370 |
|
int distortion = 0; |
371 |
|
int i; |
372 |
|
|
373 |
|
fdct(data); |
374 |
|
|
375 |
|
if (quant_type) sum = quant_inter(coeff, data, quant); |
376 |
|
else sum = quant4_inter(coeff, data, quant); |
377 |
|
|
378 |
|
if (sum > 0) { |
379 |
|
*cbp |= 1 << (5 - block); |
380 |
|
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
381 |
|
|
382 |
|
if (quant_type) dequant_inter(dqcoeff, coeff, quant); |
383 |
|
else dequant4_inter(dqcoeff, coeff, quant); |
384 |
|
|
385 |
|
for (i = 0; i < 64; i++) |
386 |
|
distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]); |
387 |
|
|
388 |
|
} else { |
389 |
|
bits = 0; |
390 |
|
for (i = 0; i < 64; i++) |
391 |
|
distortion += data[i]*data[i]; |
392 |
|
} |
393 |
|
|
394 |
|
return bits + (LAMBDA*distortion)/(quant*quant); |
395 |
|
} |
396 |
|
|
397 |
|
static __inline unsigned int |
398 |
|
Block_CalcBitsIntra(int16_t * const coeff, |
399 |
|
int16_t * const data, |
400 |
|
int16_t * const dqcoeff, |
401 |
|
const uint32_t quant, const int quant_type, |
402 |
|
uint32_t * cbp, |
403 |
|
const int block, |
404 |
|
int * dcpred) |
405 |
|
{ |
406 |
|
int bits, i; |
407 |
|
int distortion = 0; |
408 |
|
uint32_t iDcScaler = get_dc_scaler(quant, block < 4); |
409 |
|
int b_dc; |
410 |
|
|
411 |
|
fdct(data); |
412 |
|
data[0] -= 1024; |
413 |
|
|
414 |
|
if (quant_type) quant_intra(coeff, data, quant, iDcScaler); |
415 |
|
else quant4_intra(coeff, data, quant, iDcScaler); |
416 |
|
|
417 |
|
b_dc = coeff[0]; |
418 |
|
if (block < 4) { |
419 |
|
coeff[0] -= *dcpred; |
420 |
|
*dcpred = b_dc; |
421 |
|
} |
422 |
|
|
423 |
|
bits = BITS_MULT*CodeCoeffIntra_CalcBits(coeff, scan_tables[0]); |
424 |
|
if (bits != 0) *cbp |= 1 << (5 - block); |
425 |
|
|
426 |
|
if (block < 4) bits += BITS_MULT*dcy_tab[coeff[0] + 255].len; |
427 |
|
else bits += BITS_MULT*dcc_tab[coeff[0] + 255].len; |
428 |
|
|
429 |
|
coeff[0] = b_dc; |
430 |
|
if (quant_type) dequant_intra(dqcoeff, coeff, quant, iDcScaler); |
431 |
|
else dequant4_intra(dqcoeff, coeff, quant, iDcScaler); |
432 |
|
|
433 |
|
for (i = 0; i < 64; i++) |
434 |
|
distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]); |
435 |
|
|
436 |
|
return bits + (LAMBDA*distortion)/(quant*quant); |
437 |
|
} |
438 |
|
|
439 |
#endif /* _MOTION_EST_H_ */ |
#endif /* _MOTION_EST_H_ */ |