--- motion_est.h 2002/12/11 10:32:29 1.1.2.10 +++ motion_est.h 2003/02/12 12:57:27 1.1.2.15 @@ -26,7 +26,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * $Id: motion_est.h,v 1.1.2.10 2002/12/11 10:32:29 syskin Exp $ + * $Id: motion_est.h,v 1.1.2.15 2003/02/12 12:57:27 syskin Exp $ * ***************************************************************************/ @@ -35,6 +35,7 @@ #include "../portab.h" #include "../global.h" +#include "../image/reduced.h" /* hard coded motion search parameters for motion_est and smp_motion_est */ @@ -46,7 +47,7 @@ /* vector map (vlc delta size) smoother parameters ! float !*/ #define NEIGH_TEND_16X16 10.5 -#define NEIGH_TEND_8X8 4.0 +#define NEIGH_TEND_8X8 40.0 #define NEIGH_8X8_BIAS 30 /* Parameters which control inter/inter4v decision */ @@ -101,61 +102,61 @@ }; #define RRV_MV_SCALEDOWN(a) ( (a)>=0 ? (a+1)/2 : (a-1)/2 ) -#define RRV_MV_SCALEUP(a) ( (a)>0 ? 2*(a)-1 : (a)<0 ? 2*(a)+1 : (a) ) + +static const VECTOR zeroMV = {0,0}; typedef struct - { +{ // general fields - int max_dx, min_dx, max_dy, min_dy; - uint32_t rounding; - VECTOR predMV; - VECTOR *currentMV; - VECTOR *currentQMV; - int32_t *iMinSAD; - const uint8_t * Ref; - const uint8_t * RefH; - const uint8_t * RefV; - const uint8_t * RefHV; - const uint8_t * RefCU; - const uint8_t * RefCV; - const uint8_t * CurU; - const uint8_t * CurV; - uint8_t * RefQ; - const uint8_t * Cur; - uint32_t lambda16; - uint32_t lambda8; - uint32_t iEdgedWidth; - uint32_t iFcode; - int * temp; - int qpel, qpel_precision; - int chroma; - int rrv; -//fields for interpolate and direct mode - const uint8_t *bRef; - const uint8_t *bRefH; - const uint8_t *bRefV; - const uint8_t *bRefHV; - VECTOR bpredMV; - uint32_t bFcode; + int max_dx, min_dx, max_dy, min_dy; + uint32_t rounding; + VECTOR predMV; + VECTOR * currentMV; + VECTOR * currentQMV; + int32_t * iMinSAD; + const uint8_t * Ref; + const uint8_t * RefH; + const uint8_t * RefV; + const uint8_t * RefHV; + const uint8_t * RefCU; + const uint8_t * RefCV; + const uint8_t * CurU; + const uint8_t * CurV; + uint8_t * RefQ; + const uint8_t * Cur; + uint32_t lambda16; + uint32_t lambda8; + uint32_t iEdgedWidth; + uint32_t iFcode; + int * temp; + int qpel, qpel_precision; + int chroma; + 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; + + VECTOR bpredMV; + uint32_t bFcode; // fields for direct mode - VECTOR directmvF[4]; - VECTOR directmvB[4]; - const VECTOR * referencemv; - } - SearchData; + VECTOR directmvF[4]; + VECTOR directmvB[4]; + const VECTOR * referencemv; + +} SearchData; typedef void(CheckFunc)(const int x, const int y, const int Direction, int * const dir, const SearchData * const Data); - -static CheckFunc CheckCandidate16, CheckCandidate16no4v, CheckCandidateInt, - CheckCandidateDirect, CheckCandidateDirectno4v, - CheckCandidate8; CheckFunc *CheckCandidate; /* - * Calculate the min/max range (in halfpixels) + * Calculate the min/max range * relative to the _MACROBLOCK_ position */ static void __inline @@ -165,84 +166,35 @@ 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) -{ - int k; - const int search_range = 32 << (fcode - 1); - const int high = search_range - 1; - const int low = -search_range; - - k = 2 * (int)(width - x*block_sz); - *max_dx = MIN(high, k); - k = 2 * (int)(height - y*block_sz); - *max_dy = MIN(high, k); - - k = -2 * (int)((x+1) * block_sz); - *min_dx = MAX(low, k); - k = -2 * (int)((y+1) * block_sz); - *min_dy = MAX(low, k); -} - -static void __inline -get_range_rrv(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 */ + uint32_t block_sz, /* block dimension, 8 or 16 */ const uint32_t width, const uint32_t height, - const uint32_t fcode) + const uint32_t fcode, + const int qpel, /* 1 if the resulting range should be in qpel precision; otherwise 0 */ + const int rrv) { - int k; + int k, m = qpel ? 4 : 2; const int search_range = 32 << (fcode - 1); - const int high = RRV_MV_SCALEUP(search_range - 1); //halfzero -> halfpel, because we do our search in halfpel - const int low = RRV_MV_SCALEUP(-search_range); - - k = 2 * (int)(width - x*block_sz); - *max_dx = MIN(high, k); - k = 2 * (int)(height - y*block_sz); - *max_dy = MIN(high, k); + int high = search_range - 1; + int low = -search_range; - k = -2 * (int)((x+1) * block_sz); - *min_dx = MAX(low, k); - k = -2 * (int)((y+1) * block_sz); - *min_dy = MAX(low, k); -} - -static void __inline -get_range_qpel(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) -{ - int k; - const int search_range = 32 << (fcode - 1); - const int high = search_range - 1; - const int low = -search_range; + if (rrv) { + high = RRV_MV_SCALEUP(high); + low = RRV_MV_SCALEUP(low); + block_sz *= 2; + } - k = 4 * (int)(width - x*block_sz); + k = m * (int)(width - x * block_sz); *max_dx = MIN(high, k); - k = 4 * (int)(height - y*block_sz); + k = m * (int)(height - y * block_sz); *max_dy = MIN(high, k); - k = -4 * (int)((x+1) * block_sz); + k = -m * (int)((x+1) * block_sz); *min_dx = MAX(low, k); - k = -4 * (int)((y+1) * block_sz); + k = -m * (int)((y+1) * block_sz); *min_dy = MAX(low, k); } - typedef void MainSearchFunc(int x, int y, const SearchData * const Data, int bDirection); static MainSearchFunc DiamondSearch, AdvDiamondSearch, SquareSearch; @@ -274,6 +226,7 @@ const int x, const int y, const uint32_t MotionFlags, + const uint32_t GlobalFlags, const uint32_t iQuant, SearchData * const Data, const MBParam * const pParam, @@ -282,4 +235,79 @@ int inter4v, MACROBLOCK * const pMB); + +static WARPPOINTS +GlobalMotionEst(const 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 ); + +#define iDiamondSize 2 + +static __inline uint32_t +MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t GlobalFlags) +{ + uint32_t Flags = MotionFlags; + + if (!(GlobalFlags & XVID_MODEDECISION_BITS)) + Flags &= ~(QUARTERPELREFINE16_BITS+QUARTERPELREFINE8_BITS+HALFPELREFINE16_BITS+HALFPELREFINE8_BITS+EXTSEARCH_BITS); + + if (Flags & EXTSEARCH_BITS) + Flags |= HALFPELREFINE16_BITS; + + if (Flags & EXTSEARCH_BITS && MotionFlags & PMV_EXTSEARCH8) + Flags |= HALFPELREFINE8_BITS; + + if (Flags & HALFPELREFINE16_BITS) + Flags |= QUARTERPELREFINE16_BITS; + + if (Flags & HALFPELREFINE8_BITS) { + Flags |= QUARTERPELREFINE8_BITS; + Flags &= ~PMV_HALFPELREFINE8; + } + + if (Flags & QUARTERPELREFINE8_BITS) + Flags &= ~PMV_QUARTERPELREFINE8; + + if (!(GlobalFlags & XVID_QUARTERPEL)) + Flags &= ~(PMV_QUARTERPELREFINE16+PMV_QUARTERPELREFINE8+QUARTERPELREFINE16_BITS+QUARTERPELREFINE8_BITS); + + if (!(GlobalFlags & XVID_HALFPEL)) + Flags &= ~(PMV_EXTSEARCH16+PMV_HALFPELREFINE16+PMV_HALFPELREFINE8+HALFPELREFINE16_BITS+HALFPELREFINE8_BITS); + + if (GlobalFlags & (XVID_GREYSCALE + XVID_REDUCED)) + Flags &= ~(PMV_CHROMA16 + PMV_CHROMA8); + + return Flags; +} + +/* BITS mode decision and search */ + +#include "../bitstream/zigzag.h" +#include "../quant/quant_mpeg4.h" +#include "../quant/quant_h263.h" +#include "../bitstream/vlc_codes.h" + +static int +CountMBBitsInter(SearchData * const Data, + const MACROBLOCK * const pMBs, const int x, const int y, + const MBParam * const pParam, + const uint32_t MotionFlags); + +static int +CountMBBitsInter4v(const SearchData * const Data, + MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, + const int x, const int y, + const MBParam * const pParam, const uint32_t MotionFlags, + const VECTOR * const backup); + +static int +CountMBBitsIntra(const SearchData * const Data); + +int CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); +int CodeCoeffInter_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); + #endif /* _MOTION_EST_H_ */