1 |
/***************************************************************************** |
/************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Motion Estimation header - |
* - Motion sad header - |
|
* |
|
|
* Copyright(C) 2002 Christoph Lampert <gruel@web.de> |
|
5 |
* |
* |
6 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
* This program is an implementation of a part of one or more MPEG-4 |
7 |
|
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
8 |
|
* 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 |
* XviD is free software; you can redistribute it and/or modify it |
* This program is free software; you can redistribute it and/or modify |
16 |
* 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 |
17 |
* the Free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
18 |
* (at your option) any later version. |
* (at your option) any later version. |
19 |
* |
* |
26 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
27 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
28 |
* |
* |
|
* Under section 8 of the GNU General Public License, the copyright |
|
|
* holders of XVID explicitly forbid distribution in the following |
|
|
* countries: |
|
|
* |
|
|
* - Japan |
|
|
* - United States of America |
|
|
* |
|
|
* Linking XviD statically or dynamically with other modules is making a |
|
|
* combined work based on XviD. Thus, the terms and conditions of the |
|
|
* GNU General Public License cover the whole combination. |
|
|
* |
|
|
* As a special exception, the copyright holders of XviD give you |
|
|
* permission to link XviD with independent modules that communicate with |
|
|
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
|
|
* license terms of these independent modules, and to copy and distribute |
|
|
* the resulting combined work under terms of your choice, provided that |
|
|
* every copy of the combined work is accompanied by a complete copy of |
|
|
* the source code of XviD (the version of XviD used to produce the |
|
|
* combined work), being distributed under the terms of the GNU General |
|
|
* Public License plus this exception. An independent module is a module |
|
|
* which is not derived from or based on XviD. |
|
|
* |
|
|
* Note that people who make modified versions of XviD are not obligated |
|
|
* to grant this special exception for their modified versions; it is |
|
|
* their choice whether to do so. The GNU General Public License gives |
|
|
* permission to release a modified version without this exception; this |
|
|
* exception also makes it possible to release a modified version which |
|
|
* carries forward this exception. |
|
|
* |
|
29 |
* $Id$ |
* $Id$ |
30 |
* |
* |
31 |
***************************************************************************/ |
***************************************************************************/ |
36 |
#include "../portab.h" |
#include "../portab.h" |
37 |
#include "../global.h" |
#include "../global.h" |
38 |
|
|
39 |
/* hard coded motion search parameters for motion_est */ |
/* fast ((A)/2)*2 */ |
|
|
|
|
// very large value |
|
|
#define MV_MAX_ERROR (4096 * 256) |
|
|
|
|
|
// stop search if sdelta < THRESHOLD |
|
|
#define MV16_THRESHOLD 192 |
|
|
#define MV8_THRESHOLD 56 |
|
|
|
|
|
#define NEIGH_MOVE_THRESH 0 |
|
|
// how much a block's MV must differ from his neighbour |
|
|
// to be search for INTER4V. The more, the faster... |
|
|
|
|
|
/* sad16(0,0) bias; mpeg4 spec suggests nb/2+1 */ |
|
|
/* nb = vop pixels * 2^(bpp-8) */ |
|
|
#define MV16_00_BIAS (128+1) |
|
|
#define MV8_00_BIAS (0) |
|
|
|
|
|
/* INTER bias for INTER/INTRA decision; mpeg4 spec suggests 2*nb */ |
|
|
#define MV16_INTER_BIAS 512 |
|
|
|
|
|
/* Parameters which control inter/inter4v decision */ |
|
|
#define IMV16X16 5 |
|
|
|
|
|
/* vector map (vlc delta size) smoother parameters */ |
|
|
#define NEIGH_TEND_16X16 2 |
|
|
#define NEIGH_TEND_8X8 2 |
|
|
|
|
|
// fast ((A)/2)*2 |
|
40 |
#define EVEN(A) (((A)<0?(A)+1:(A)) & ~1) |
#define EVEN(A) (((A)<0?(A)+1:(A)) & ~1) |
41 |
|
|
42 |
#define MVzero(A) ( ((A).x)==(0) && ((A).y)==(0) ) |
#define MVzero(A) ( ((A).x)==(0) && ((A).y)==(0) ) |
43 |
#define MVequal(A,B) ( ((A).x)==((B).x) && ((A).y)==((B).y) ) |
#define MVequal(A,B) ( ((A).x)==((B).x) && ((A).y)==((B).y) ) |
44 |
|
|
45 |
/* default methods of Search, will be changed to function variable */ |
/***************************************************************************** |
46 |
|
* Modified rounding tables -- defined in motion_est.c |
47 |
#ifndef SEARCH16 |
* Original tables see ISO spec tables 7-6 -> 7-9 |
48 |
#define SEARCH16 PMVfastSearch16 |
****************************************************************************/ |
49 |
//#define SEARCH16 FullSearch16 |
|
50 |
//#define SEARCH16 EPZSSearch16 |
extern const uint32_t roundtab[16]; |
51 |
#endif |
/* K = 4 */ |
52 |
|
extern const uint32_t roundtab_76[16]; |
53 |
#ifndef SEARCH8 |
/* K = 2 */ |
54 |
#define SEARCH8 PMVfastSearch8 |
extern const uint32_t roundtab_78[8]; |
55 |
//#define SEARCH8 EPZSSearch8 |
/* K = 1 */ |
56 |
#endif |
extern const uint32_t roundtab_79[4]; |
|
|
|
|
|
|
|
/* |
|
|
* Calculate the min/max range (in halfpixels) |
|
|
* relative to the _MACROBLOCK_ position |
|
|
*/ |
|
|
|
|
|
static void __inline |
|
|
get_range(int32_t * const min_dx, |
|
|
int32_t * const max_dx, |
|
|
int32_t * const min_dy, |
|
|
int32_t * const max_dy, |
|
|
const uint32_t x, |
|
|
const uint32_t y, |
|
|
const uint32_t block_sz, /* block dimension, 8 or 16 */ |
|
|
|
|
|
const uint32_t width, |
|
|
const uint32_t height, |
|
|
const uint32_t fcode) |
|
|
{ |
|
|
|
|
|
const int search_range = 32 << (fcode - 1); |
|
|
const int high = search_range - 1; |
|
|
const int low = -search_range; |
|
|
|
|
|
/* convert full-pixel measurements to half pixel */ |
|
|
const int hp_width = 2 * width; |
|
|
const int hp_height = 2 * height; |
|
|
const int hp_edge = 2 * block_sz; |
|
|
|
|
|
/* we need _right end_ of block, not x-coordinate */ |
|
|
const int hp_x = 2 * (x) * block_sz; |
|
|
|
|
|
/* same for _bottom end_ */ |
|
|
const int hp_y = 2 * (y) * block_sz; |
|
|
|
|
|
*max_dx = MIN(high, hp_width - hp_x); |
|
|
*max_dy = MIN(high, hp_height - hp_y); |
|
|
*min_dx = MAX(low, -(hp_edge + hp_x)); |
|
|
*min_dy = MAX(low, -(hp_edge + hp_y)); |
|
|
|
|
|
} |
|
|
|
|
57 |
|
|
58 |
/* |
/* |
59 |
* getref: calculate reference image pointer |
* getref: calculate reference image pointer |
69 |
const uint32_t x, |
const uint32_t x, |
70 |
const uint32_t y, |
const uint32_t y, |
71 |
const uint32_t block, /* block dimension, 8 or 16 */ |
const uint32_t block, /* block dimension, 8 or 16 */ |
|
|
|
72 |
const int32_t dx, |
const int32_t dx, |
73 |
const int32_t dy, |
const int32_t dy, |
74 |
const uint32_t stride) |
const int32_t stride) |
75 |
{ |
{ |
|
|
|
|
|
|
76 |
switch (((dx & 1) << 1) + (dy & 1)) { /* ((dx%2)?2:0)+((dy%2)?1:0) */ |
switch (((dx & 1) << 1) + (dy & 1)) { /* ((dx%2)?2:0)+((dy%2)?1:0) */ |
77 |
case 0: |
case 0: |
78 |
return refn + (int) ((x * block + dx / 2) + (y * block + dy / 2) * stride); |
return refn + (int) ((x * block + dx / 2) + (y * block + dy / 2) * stride); |
79 |
case 1: |
case 1: |
80 |
return refv + (int) ((x * block + dx / 2) + (y * block + |
return refv + (int) ((x * block + dx / 2) + (y * block + (dy - 1) / 2) * stride); |
|
(dy - 1) / 2) * stride); |
|
81 |
case 2: |
case 2: |
82 |
return refh + (int) ((x * block + (dx - 1) / 2) + (y * block + |
return refh + (int) ((x * block + (dx - 1) / 2) + (y * block + dy / 2) * stride); |
|
dy / 2) * stride); |
|
83 |
default: |
default: |
84 |
case 3: |
return refhv + (int) ((x * block + (dx - 1) / 2) + (y * block + (dy - 1) / 2) * stride); |
|
return refhv + (int) ((x * block + (dx - 1) / 2) + (y * block + |
|
|
(dy - 1) / 2) * stride); |
|
85 |
} |
} |
|
|
|
86 |
} |
} |
87 |
|
|
88 |
|
void MotionEstimationBVOP(MBParam * const pParam, |
89 |
/* This is somehow a copy of get_ref, but with MV instead of X,Y */ |
FRAMEINFO * const frame, |
90 |
|
/* forward (past) reference */ |
91 |
static __inline const uint8_t * |
const int32_t time_bp, |
92 |
get_ref_mv(const uint8_t * const refn, |
const int32_t time_pp, |
93 |
const uint8_t * const refh, |
const MACROBLOCK * const f_mbs, |
94 |
const uint8_t * const refv, |
const IMAGE * const f_ref, |
95 |
const uint8_t * const refhv, |
const IMAGE * const f_refH, |
96 |
const uint32_t x, |
const IMAGE * const f_refV, |
97 |
const uint32_t y, |
const IMAGE * const f_refHV, |
98 |
const uint32_t block, /* block dimension, 8 or 16 */ |
/* backward (future) reference */ |
99 |
|
const FRAMEINFO * const b_reference, |
100 |
const VECTOR * mv, /* measured in half-pel! */ |
const IMAGE * const b_ref, |
101 |
|
const IMAGE * const b_refH, |
102 |
const uint32_t stride) |
const IMAGE * const b_refV, |
103 |
{ |
const IMAGE * const b_refHV); |
104 |
|
|
105 |
switch ((((mv->x) & 1) << 1) + ((mv->y) & 1)) { |
void MBMotionCompensationBVOP(MBParam * pParam, |
106 |
case 0: |
MACROBLOCK * const mb, |
107 |
return refn + (int) ((x * block + (mv->x) / 2) + (y * block + |
const uint32_t i, |
108 |
(mv->y) / 2) * stride); |
const uint32_t j, |
109 |
case 1: |
IMAGE * const cur, |
110 |
return refv + (int) ((x * block + (mv->x) / 2) + (y * block + |
const IMAGE * const f_ref, |
111 |
((mv->y) - 1) / 2) * stride); |
const IMAGE * const f_refh, |
112 |
case 2: |
const IMAGE * const f_refv, |
113 |
return refh + (int) ((x * block + ((mv->x) - 1) / 2) + (y * block + |
const IMAGE * const f_refhv, |
114 |
(mv->y) / 2) * stride); |
const IMAGE * const b_ref, |
115 |
default: |
const IMAGE * const b_refh, |
116 |
case 3: |
const IMAGE * const b_refv, |
117 |
return refhv + (int) ((x * block + ((mv->x) - 1) / 2) + (y * block + |
const IMAGE * const b_refhv, |
118 |
((mv->y) - |
int16_t * dct_codes); |
119 |
1) / 2) * stride); |
|
120 |
} |
|
121 |
|
/* GMC stuff. Maybe better put it into a separate file */ |
122 |
} |
|
123 |
|
void |
124 |
|
generate_GMCparameters( const int num_wp, /* [input]: number of warppoints */ |
125 |
static __inline const uint8_t * |
const int res, /* [input]: resolution */ |
126 |
get_iref(const uint8_t * const ref, |
const WARPPOINTS *const warp, /* [input]: warp points */ |
127 |
const uint32_t x, |
const int width, const int height, /* [input]: without edges! */ |
128 |
const uint32_t y, |
GMC_DATA *const gmc); /* [output] precalculated parameters */ |
129 |
const uint32_t block, /* block dimension, 8 or 16 */ |
|
130 |
|
void |
131 |
const int32_t dx, |
generate_GMCimage( const GMC_DATA *const gmc_data, /* [input] precalculated data */ |
132 |
const int32_t dy, |
const IMAGE *const pRef, /* [input] */ |
133 |
const uint32_t stride) |
const int mb_width, |
134 |
{ |
const int mb_height, |
135 |
return ref + (int) ((x * block + dx / 2) + (y * block + dy / 2) * stride); |
const int stride, |
136 |
} |
const int stride2, |
137 |
|
const int fcode, /* [input] some parameters... */ |
138 |
static __inline const uint8_t * |
const int32_t quarterpel, /* [input] for rounding avgMV */ |
139 |
get_iref_mv(const uint8_t * const ref, |
const int reduced_resolution, /* [input] ignored */ |
140 |
const uint32_t x, |
const int32_t rounding, /* [input] for rounding image data */ |
141 |
const uint32_t y, |
MACROBLOCK *const pMBs, /* [output] average motion vectors */ |
142 |
const uint32_t block, /* block dimension, 8 or 16 */ |
IMAGE *const pGMC); /* [output] full warped image */ |
143 |
|
|
144 |
const VECTOR * mv, /* as usual measured in half-pel */ |
VECTOR generate_GMCimageMB( const GMC_DATA *const gmc_data, /* [input] all precalc data */ |
145 |
|
const IMAGE *const pRef, /* [input] */ |
146 |
const uint32_t stride) |
const int mi, const int mj, /* [input] MB position */ |
147 |
{ |
const int stride, /* [input] Lumi stride */ |
148 |
return ref + (int) ((x * block + (mv->x) / 2) + (y * block + (mv->y) / 2) * stride); |
const int stride2, /* [input] chroma stride */ |
149 |
} |
const int quarterpel, /* [input] for rounding of AvgMV */ |
150 |
|
const int rounding, |
151 |
|
IMAGE *const pGMC); /* [outut] generate image */ |
152 |
/* prototypes for MainSearch functions, i.e. Diamondsearch, FullSearch or whatever */ |
|
153 |
|
int |
154 |
typedef int32_t(MainSearch16Func) (const uint8_t * const pRef, |
MEanalysis( const IMAGE * const pRef, |
155 |
const uint8_t * const pRefH, |
const FRAMEINFO * const Current, |
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const uint8_t * const cur, |
|
|
const int x, |
|
|
const int y, |
|
|
const int start_x, |
|
|
const int start_y, |
|
|
int iMinSAD, |
|
|
VECTOR * const currMV, |
|
|
const int center_x, |
|
|
const int center_y, |
|
|
const int32_t min_dx, |
|
|
const int32_t max_dx, |
|
|
const int32_t min_dy, |
|
|
const int32_t max_dy, |
|
|
const int32_t iEdgedWidth, |
|
|
const int32_t iDiamondSize, |
|
|
const int32_t iFcode, |
|
|
const int32_t iQuant, |
|
|
int iFound); |
|
|
|
|
|
typedef MainSearch16Func *MainSearch16FuncPtr; |
|
|
|
|
|
|
|
|
typedef int32_t(MainSearch8Func) (const uint8_t * const pRef, |
|
|
const uint8_t * const pRefH, |
|
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const uint8_t * const cur, |
|
|
const int x, |
|
|
const int y, |
|
|
const int start_x, |
|
|
const int start_y, |
|
|
int iMinSAD, |
|
|
VECTOR * const currMV, |
|
|
const int center_x, |
|
|
const int center_y, |
|
|
const int32_t min_dx, |
|
|
const int32_t max_dx, |
|
|
const int32_t min_dy, |
|
|
const int32_t max_dy, |
|
|
const int32_t iEdgedWidth, |
|
|
const int32_t iDiamondSize, |
|
|
const int32_t iFcode, |
|
|
const int32_t iQuant, |
|
|
int iFound); |
|
|
|
|
|
typedef MainSearch8Func *MainSearch8FuncPtr; |
|
|
|
|
|
|
|
|
/* prototypes for MotionEstimation functions, i.e. PMVfast, EPZS or whatever */ |
|
|
|
|
|
typedef int32_t(Search16Func) ( const uint8_t * const pRef, |
|
|
const uint8_t * const pRefH, |
|
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const IMAGE * const pCur, |
|
|
const int x, |
|
|
const int y, |
|
|
const int start_x, |
|
|
const int start_y, |
|
|
const int center_x, |
|
|
const int center_y, |
|
|
const uint32_t MotionFlags, |
|
|
const uint32_t iQuant, |
|
|
const uint32_t iFcode, |
|
156 |
const MBParam * const pParam, |
const MBParam * const pParam, |
157 |
const MACROBLOCK * const pMBs, |
const int maxIntra, |
158 |
const MACROBLOCK * const prevMBs, |
const int intraCount, |
159 |
VECTOR * const currMV, |
const int bCount, |
160 |
VECTOR * const currPMV); |
const int b_thresh); |
|
|
|
|
typedef Search16Func *Search16FuncPtr; |
|
|
|
|
|
typedef int32_t(Search8Func) ( const uint8_t * const pRef, |
|
|
const uint8_t * const pRefH, |
|
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const IMAGE * const pCur, |
|
|
const int x, |
|
|
const int y, |
|
|
const int start_x, |
|
|
const int start_y, |
|
|
const int center_x, |
|
|
const int center_y, |
|
|
const uint32_t MotionFlags, |
|
|
const uint32_t iQuant, |
|
|
const uint32_t iFcode, |
|
|
const MBParam * const pParam, |
|
|
const MACROBLOCK * const pMBs, |
|
|
const MACROBLOCK * const prevMBs, |
|
|
VECTOR * const currMV, |
|
|
VECTOR * const currPMV); |
|
|
|
|
|
typedef Search8Func *Search8FuncPtr; |
|
|
|
|
|
Search16Func PMVfastSearch16; |
|
|
Search16Func EPZSSearch16; |
|
|
Search16Func PMVfastIntSearch16; |
|
|
|
|
|
Search8Func PMVfastSearch8; |
|
|
Search8Func EPZSSearch8; |
|
|
|
|
|
|
|
|
bool |
|
|
MotionEstimation(MBParam * const pParam, |
|
|
FRAMEINFO * const current, |
|
|
FRAMEINFO * const reference, |
|
|
const IMAGE * const pRefH, |
|
|
const IMAGE * const pRefV, |
|
|
const IMAGE * const pRefHV, |
|
|
const uint32_t iLimit); |
|
|
|
|
|
typedef int32_t(Halfpel8_RefineFunc) (const uint8_t * const pRef, |
|
|
const uint8_t * const pRefH, |
|
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const uint8_t * const cur, |
|
|
const int x, |
|
|
const int y, |
|
|
VECTOR * const currMV, |
|
|
int32_t iMinSAD, |
|
|
const int center_x, |
|
|
const int center_y, |
|
|
const int32_t min_dx, |
|
|
const int32_t max_dx, |
|
|
const int32_t min_dy, |
|
|
const int32_t max_dy, |
|
|
const int32_t iFcode, |
|
|
const int32_t iQuant, |
|
|
const int32_t iEdgedWidth); |
|
|
|
|
|
typedef Halfpel8_RefineFunc *Halfpel8_RefineFuncPtr; |
|
|
extern Halfpel8_RefineFuncPtr Halfpel8_Refine; |
|
|
Halfpel8_RefineFunc Halfpel8_Refine_c; |
|
|
Halfpel8_RefineFunc Halfpel8_Refine_ia64; |
|
|
|
|
161 |
|
|
162 |
#endif /* _MOTION_H_ */ |
#endif /* _MOTION_H_ */ |