--- motion_est.h 2003/07/13 09:57:28 1.3.2.13 +++ motion_est.h 2003/04/08 11:12:07 1.6 @@ -1,29 +1,34 @@ -/***************************************************************************** +/************************************************************************** * * XVID MPEG-4 VIDEO CODEC - * - Motion Estimation related header - + * - Motion estimation header - * - * Copyright(C) 2002 Christoph Lampert - * 2002 Michael Militzer - * 2002-2003 Radoslaw Czyz + * This program is an implementation of a part of one or more MPEG-4 + * Video tools as specified in ISO/IEC 14496-2 standard. Those intending + * to use this software module in hardware or software products are + * 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. * - * This program is free software ; you can redistribute it and/or modify + * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation ; either version 2 of the License, or + * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY ; without even the implied warranty of + * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program ; if not, write to the Free Software + * 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.3.2.13 2003/07/13 09:57:28 syskin Exp $ + * $Id: motion_est.h,v 1.6 2003/04/08 11:12:07 syskin Exp $ * - ****************************************************************************/ + ***************************************************************************/ #ifndef _MOTION_EST_H_ #define _MOTION_EST_H_ @@ -34,7 +39,7 @@ /* hard coded motion search parameters for motion_est and smp_motion_est */ -/* very large value */ +// very large value #define MV_MAX_ERROR (4096 * 256) /* INTER bias for INTER/INTRA decision; mpeg4 spec suggests 2*nb */ @@ -45,8 +50,6 @@ #define NEIGH_TEND_8X8 40.0 #define NEIGH_8X8_BIAS 30 -#define BITS_MULT 16 - /* Parameters which control inter/inter4v decision */ #define IMV16X16 2 @@ -86,7 +89,7 @@ (int)(21.2656*NEIGH_TEND_8X8 + 0.5), (int)(24.8580*NEIGH_TEND_8X8 + 0.5), (int)(29.6436*NEIGH_TEND_8X8 + 0.5), (int)(36.4949*NEIGH_TEND_8X8 + 0.5) }; -/* mv.length table */ +// mv.length table static const int mvtab[64] = { 1, 2, 3, 4, 6, 7, 7, 7, 9, 9, 9, 10, 10, 10, 10, 10, @@ -104,14 +107,14 @@ typedef struct { - /* general fields */ +// 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 * RefP[6]; /* N, V, H, HV, cU, cV */ + const uint8_t * RefP[6]; // N, V, H, HV, cU, cV const uint8_t * CurU; const uint8_t * CurV; uint8_t * RefQ; @@ -124,22 +127,16 @@ int qpel, qpel_precision; int chroma; int rrv; - - /* fields for interpolate and direct modes */ - const uint8_t * b_RefP[6]; /* N, V, H, HV, cU, cV */ +//fields for interpolate and direct modes + const uint8_t * b_RefP[6]; // N, V, H, HV, cU, cV VECTOR bpredMV; uint32_t bFcode; - - /* fields for direct mode */ +// fields for direct mode VECTOR directmvF[4]; VECTOR directmvB[4]; const VECTOR * referencemv; - - /* BITS/R-D stuff */ +// _BITS stuff int16_t * dctSpace; - uint32_t iQuant; - uint32_t quant_type; - int * cbp; } SearchData; @@ -209,7 +206,6 @@ const IMAGE * const pRefH, const IMAGE * const pRefV, const IMAGE * const pRefHV, - const IMAGE * const pGMC, const uint32_t iLimit); static void @@ -221,38 +217,18 @@ const int x, const int y, const uint32_t MotionFlags, - const uint32_t VopFlags, - const uint32_t VolFlags, + const uint32_t GlobalFlags, + const uint32_t iQuant, SearchData * const Data, const MBParam * const pParam, const MACROBLOCK * const pMBs, const MACROBLOCK * const prevMBs, + int inter4v, MACROBLOCK * const pMB); -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, +static WARPPOINTS +GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const FRAMEINFO * const current, const FRAMEINFO * const reference, @@ -260,66 +236,41 @@ const IMAGE * const pRefV, const IMAGE * const pRefHV ); - -int -GlobalMotionEstRefine(WARPPOINTS *const startwp, - MACROBLOCK * const pMBs, - const MBParam * const pParam, - const FRAMEINFO * const current, - const FRAMEINFO * const reference, - const IMAGE * const pCurr, - const IMAGE * const pRef, - const IMAGE * const pRefH, - const IMAGE * const pRefV, - const IMAGE * const pRefHV); - - - - -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); - #define iDiamondSize 2 static __inline uint32_t -MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags) +MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t GlobalFlags) { uint32_t Flags = MotionFlags; - if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) - Flags &= ~(XVID_ME_QUARTERPELREFINE16_BITS+XVID_ME_QUARTERPELREFINE8_BITS+XVID_ME_HALFPELREFINE16_BITS+XVID_ME_HALFPELREFINE8_BITS+XVID_ME_EXTSEARCH_BITS); + if (!(GlobalFlags & XVID_MODEDECISION_BITS)) + Flags &= ~(QUARTERPELREFINE16_BITS+QUARTERPELREFINE8_BITS+HALFPELREFINE16_BITS+HALFPELREFINE8_BITS+EXTSEARCH_BITS); - if (Flags & XVID_ME_EXTSEARCH_BITS) - Flags |= XVID_ME_HALFPELREFINE16_BITS; + if (Flags & EXTSEARCH_BITS) + Flags |= HALFPELREFINE16_BITS; - if (Flags & XVID_ME_EXTSEARCH_BITS && MotionFlags & XVID_ME_EXTSEARCH8) - Flags |= XVID_ME_HALFPELREFINE8_BITS; + if (Flags & EXTSEARCH_BITS && MotionFlags & PMV_EXTSEARCH8) + Flags |= HALFPELREFINE8_BITS; - if (Flags & XVID_ME_HALFPELREFINE16_BITS) - Flags |= XVID_ME_QUARTERPELREFINE16_BITS; + if (Flags & HALFPELREFINE16_BITS) + Flags |= QUARTERPELREFINE16_BITS; - if (Flags & XVID_ME_HALFPELREFINE8_BITS) { - Flags |= XVID_ME_QUARTERPELREFINE8_BITS; - Flags &= ~XVID_ME_HALFPELREFINE8; + if (Flags & HALFPELREFINE8_BITS) { + Flags |= QUARTERPELREFINE8_BITS; + Flags &= ~PMV_HALFPELREFINE8; } - if (Flags & XVID_ME_QUARTERPELREFINE8_BITS) - Flags &= ~XVID_ME_QUARTERPELREFINE8; + if (Flags & QUARTERPELREFINE8_BITS) + Flags &= ~PMV_QUARTERPELREFINE8; - if (!(VolFlags & XVID_VOL_QUARTERPEL)) - Flags &= ~(XVID_ME_QUARTERPELREFINE16+XVID_ME_QUARTERPELREFINE8+XVID_ME_QUARTERPELREFINE16_BITS+XVID_ME_QUARTERPELREFINE8_BITS); + if (!(GlobalFlags & XVID_QUARTERPEL)) + Flags &= ~(PMV_QUARTERPELREFINE16+PMV_QUARTERPELREFINE8+QUARTERPELREFINE16_BITS+QUARTERPELREFINE8_BITS); - if (!(VopFlags & XVID_VOP_HALFPEL)) - Flags &= ~(XVID_ME_EXTSEARCH16+XVID_ME_HALFPELREFINE16+XVID_ME_HALFPELREFINE8+XVID_ME_HALFPELREFINE16_BITS+XVID_ME_HALFPELREFINE8_BITS); + if (!(GlobalFlags & XVID_HALFPEL)) + Flags &= ~(PMV_EXTSEARCH16+PMV_HALFPELREFINE16+PMV_HALFPELREFINE8+HALFPELREFINE16_BITS+HALFPELREFINE8_BITS); - if ((VopFlags & XVID_VOP_GREYSCALE) || (VopFlags & XVID_VOP_REDUCED)) - Flags &= ~(XVID_ME_CHROMA16 + XVID_ME_CHROMA8); + if (GlobalFlags & (XVID_GREYSCALE + XVID_REDUCED)) + Flags &= ~(PMV_CHROMA16 + PMV_CHROMA8); return Flags; } @@ -330,7 +281,6 @@ #include "../quant/quant_mpeg4.h" #include "../quant/quant_h263.h" #include "../bitstream/vlc_codes.h" -#include "../dct/fdct.h" static int CountMBBitsInter(SearchData * const Data, @@ -348,98 +298,7 @@ static int CountMBBitsIntra(const SearchData * const Data); -static int -CountMBBitsGMC(const SearchData * const Data, const IMAGE * const vGMC, const int x, const int y); - int CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); int CodeCoeffInter_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag); -/* 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; -} - #endif /* _MOTION_EST_H_ */