--- branches/dev-api-4/xvidcore/src/motion/motion_est.h 2003/04/08 15:35:52 978 +++ branches/dev-api-4/xvidcore/src/motion/motion_est.h 2003/09/04 18:40:02 1136 @@ -1,34 +1,29 @@ -/************************************************************************** +/***************************************************************************** * * XVID MPEG-4 VIDEO CODEC - * - Motion estimation header - + * - Motion Estimation related header - * - * 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. + * Copyright(C) 2002 Christoph Lampert + * 2002 Michael Militzer + * 2002-2003 Radoslaw Czyz * - * 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.5 2003-04-08 15:35:52 edgomez Exp $ + * $Id: motion_est.h,v 1.3.2.19 2003-09-04 18:40:02 Isibaar Exp $ * - ***************************************************************************/ + ****************************************************************************/ #ifndef _MOTION_EST_H_ #define _MOTION_EST_H_ @@ -39,7 +34,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 */ @@ -50,6 +45,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 @@ -89,7 +86,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, @@ -107,14 +104,17 @@ typedef struct { -// general fields + /* general fields */ int max_dx, min_dx, max_dy, min_dy; uint32_t rounding; VECTOR predMV; VECTOR * currentMV; VECTOR * currentQMV; + VECTOR * currentMV2; + VECTOR * currentQMV2; int32_t * iMinSAD; - const uint8_t * RefP[6]; // N, V, H, HV, cU, cV + int32_t * iMinSAD2; + const uint8_t * RefP[6]; /* N, V, H, HV, cU, cV */ const uint8_t * CurU; const uint8_t * CurV; uint8_t * RefQ; @@ -124,27 +124,33 @@ uint32_t iEdgedWidth; uint32_t iFcode; int * temp; + int * dir; 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 stuff + + /* BITS/R-D stuff */ int16_t * dctSpace; + uint32_t iQuant; + uint32_t quant_type; + int * cbp; } SearchData; typedef void(CheckFunc)(const int x, const int y, - const int Direction, int * const dir, - const SearchData * const Data); -CheckFunc *CheckCandidate; + const SearchData * const Data, + const int Direction); /* * Calculate the min/max range @@ -157,36 +163,38 @@ int32_t * const max_dy, const uint32_t x, const uint32_t y, - uint32_t block_sz, /* block dimension, 8 or 16 */ + uint32_t block_sz, /* block dimension, 3(8) or 4(16) */ const uint32_t width, const uint32_t height, const uint32_t fcode, - const int qpel, /* 1 if the resulting range should be in qpel precision; otherwise 0 */ + const int precision, /* 2 for qpel, 1 for halfpel */ const int rrv) { - int k, m = qpel ? 4 : 2; - const int search_range = 32 << (fcode - 1); + int k; + const int search_range = 16 << fcode; int high = search_range - 1; int low = -search_range; if (rrv) { high = RRV_MV_SCALEUP(high); low = RRV_MV_SCALEUP(low); - block_sz *= 2; + block_sz++; } - k = m * (int)(width - x * block_sz); + k = (int)(width - (x< 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]; + } + + return bits + (LAMBDA*distortion)/(quant*quant); +} + +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]); + + return bits + (LAMBDA*distortion)/(quant*quant); +} + #endif /* _MOTION_EST_H_ */