--- motion_est.h 2003/02/12 12:57:27 1.1.2.15 +++ motion_est.h 2004/03/22 22:36:24 1.11 @@ -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.15 2003/02/12 12:57:27 syskin Exp $ + * $Id: motion_est.h,v 1.11 2004/03/22 22:36:24 edgomez dead $ * ***************************************************************************/ @@ -50,6 +50,8 @@ #define NEIGH_TEND_8X8 40.0 #define NEIGH_8X8_BIAS 30 +#define BITS_MULT 16 + /* Parameters which control inter/inter4v decision */ #define IMV16X16 2 @@ -90,12 +92,14 @@ (int)(29.6436*NEIGH_TEND_8X8 + 0.5), (int)(36.4949*NEIGH_TEND_8X8 + 0.5) }; // mv.length table -static const uint32_t mvtab[33] = { - 1, 2, 3, 4, 6, 7, 7, 7, - 9, 9, 9, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, - 10, 11, 11, 11, 11, 11, 11, 12, 12 -}; +static const int mvtab[64] = { + 1, 2, 3, 4, 6, 7, 7, 7, + 9, 9, 9, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 11, 11, 11, 11, 11, 11, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, 12 }; static const int DQtab[4] = { -1, -2, 1, 2 @@ -103,8 +107,6 @@ #define RRV_MV_SCALEDOWN(a) ( (a)>=0 ? (a+1)/2 : (a-1)/2 ) -static const VECTOR zeroMV = {0,0}; - typedef struct { // general fields @@ -114,12 +116,7 @@ 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 * RefP[6]; // N, V, H, HV, cU, cV const uint8_t * CurU; const uint8_t * CurV; uint8_t * RefQ; @@ -133,19 +130,17 @@ 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; - + const uint8_t * b_RefP[6]; // N, V, H, HV, cU, cV VECTOR bpredMV; uint32_t bFcode; // fields for direct mode VECTOR directmvF[4]; VECTOR directmvB[4]; const VECTOR * referencemv; +// BITS/R-D stuff + int16_t * dctSpace; + uint32_t iQuant; + uint32_t quant_type; } SearchData; @@ -210,12 +205,12 @@ 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); + FRAMEINFO * const current, + FRAMEINFO * const reference, + const IMAGE * const pRefH, + const IMAGE * const pRefV, + const IMAGE * const pRefHV, + const uint32_t iLimit); static void SearchP(const IMAGE * const pRef, @@ -227,12 +222,10 @@ const int y, const uint32_t MotionFlags, 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); @@ -272,6 +265,9 @@ if (Flags & QUARTERPELREFINE8_BITS) Flags &= ~PMV_QUARTERPELREFINE8; + if (Flags & QUARTERPELREFINE16_BITS) + Flags &= ~PMV_QUARTERPELREFINE16; + if (!(GlobalFlags & XVID_QUARTERPEL)) Flags &= ~(PMV_QUARTERPELREFINE16+PMV_QUARTERPELREFINE8+QUARTERPELREFINE16_BITS+QUARTERPELREFINE8_BITS); @@ -290,6 +286,7 @@ #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, @@ -310,4 +307,87 @@ 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 == 0) 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]); + } else bits = 0; + + if (quant_type == 0) 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]); + } + + 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 == 0) 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 == 0) 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_ */