[cvs] / xvidcore / src / motion / motion_est.h Repository:
ViewVC logotype

Diff of /xvidcore/src/motion/motion_est.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1.2.7, Mon Nov 4 10:58:24 2002 UTC revision 1.9, Thu Jun 19 10:06:40 2003 UTC
# Line 35  Line 35 
35    
36  #include "../portab.h"  #include "../portab.h"
37  #include "../global.h"  #include "../global.h"
38    #include "../image/reduced.h"
39    
40  /* hard coded motion search parameters for motion_est and smp_motion_est */  /* hard coded motion search parameters for motion_est and smp_motion_est */
41    
# Line 46  Line 47 
47    
48  /* vector map (vlc delta size) smoother parameters ! float !*/  /* vector map (vlc delta size) smoother parameters ! float !*/
49  #define NEIGH_TEND_16X16        10.5  #define NEIGH_TEND_16X16        10.5
50  #define NEIGH_TEND_8X8          4.0  #define NEIGH_TEND_8X8          40.0
51  #define NEIGH_8X8_BIAS          30  #define NEIGH_8X8_BIAS          30
52    
53    #define BITS_MULT                       16
54    
55  /* Parameters which control inter/inter4v decision */  /* Parameters which control inter/inter4v decision */
56  #define IMV16X16                        2  #define IMV16X16                        2
57    
# Line 89  Line 92 
92          (int)(29.6436*NEIGH_TEND_8X8 + 0.5), (int)(36.4949*NEIGH_TEND_8X8 + 0.5)        };          (int)(29.6436*NEIGH_TEND_8X8 + 0.5), (int)(36.4949*NEIGH_TEND_8X8 + 0.5)        };
93    
94  // mv.length table  // mv.length table
95  static const uint32_t mvtab[33] = {  static const int mvtab[64] = {
96          1, 2, 3, 4, 6, 7, 7, 7,          1, 2, 3, 4, 6, 7, 7, 7,
97          9, 9, 9, 10, 10, 10, 10, 10,          9, 9, 9, 10, 10, 10, 10, 10,
98          10, 10, 10, 10, 10, 10, 10, 10,          10, 10, 10, 10, 10, 10, 10, 10,
99          10, 11, 11, 11, 11, 11, 11, 12, 12                  10, 11, 11, 11, 11, 11, 11, 12,
100  };                  12, 12, 12, 12, 12, 12, 12, 12,
101                    12, 12, 12, 12, 12, 12, 12, 12,
102                    12, 12, 12, 12, 12, 12, 12, 12, 12 };
103    
104  static const int DQtab[4] = {  static const int DQtab[4] = {
105          -1, -2, 1, 2          -1, -2, 1, 2
106  };  };
107    
108    #define RRV_MV_SCALEDOWN(a)     ( (a)>=0 ? (a+1)/2 : (a-1)/2 )
109    
110  typedef struct  typedef struct
111          {          {
# Line 107  Line 113 
113                  int max_dx, min_dx, max_dy, min_dy;                  int max_dx, min_dx, max_dy, min_dy;
114                  uint32_t rounding;                  uint32_t rounding;
115                  VECTOR predMV;                  VECTOR predMV;
                 VECTOR predQMV;  
116                  VECTOR *currentMV;                  VECTOR *currentMV;
117                  VECTOR *currentQMV;                  VECTOR *currentQMV;
118                  int32_t *iMinSAD;                  int32_t *iMinSAD;
119                  const uint8_t * Ref;          const uint8_t * RefP[6]; // N, V, H, HV, cU, cV
                 const uint8_t * RefH;  
                 const uint8_t * RefV;  
                 const uint8_t * RefHV;  
                 const uint8_t * RefCU;  
                 const uint8_t * RefCV;  
120                  const uint8_t * CurU;                  const uint8_t * CurU;
121                  const uint8_t * CurV;                  const uint8_t * CurV;
122                  uint8_t * RefQ;                  uint8_t * RefQ;
# Line 126  Line 126 
126                  uint32_t iEdgedWidth;                  uint32_t iEdgedWidth;
127                  uint32_t iFcode;                  uint32_t iFcode;
128                  int * temp;                  int * temp;
129                  int qpel;          int qpel, qpel_precision;
130                  int chroma;                  int chroma;
131  //fields for interpolate and direct mode          int rrv;
132                  const uint8_t *bRef;  //fields for interpolate and direct modes
133                  const uint8_t *bRefH;          const uint8_t * b_RefP[6]; // N, V, H, HV, cU, cV
                 const uint8_t *bRefV;  
                 const uint8_t *bRefHV;  
134                  VECTOR bpredMV;                  VECTOR bpredMV;
135                  uint32_t bFcode;                  uint32_t bFcode;
136  // fields for direct mode  // fields for direct mode
137                  VECTOR directmvF[4];                  VECTOR directmvF[4];
138                  VECTOR directmvB[4];                  VECTOR directmvB[4];
139                  const VECTOR * referencemv;                  const VECTOR * referencemv;
140          }  // BITS/R-D stuff
141          SearchData;          int16_t * dctSpace;
142            uint32_t iQuant;
143            uint32_t quant_type;
144    
145    } SearchData;
146    
147    
148  typedef void(CheckFunc)(const int x, const int y,  typedef void(CheckFunc)(const int x, const int y,
149                                                  const int Direction, int * const dir,                                                  const int Direction, int * const dir,
150                                                  const SearchData * const Data);                                                  const SearchData * const Data);
   
 static CheckFunc CheckCandidate16, CheckCandidate16no4v, CheckCandidateInt,  
                         CheckCandidateDirect, CheckCandidateDirectno4v,  
                         CheckCandidate8;  
151  CheckFunc *CheckCandidate;  CheckFunc *CheckCandidate;
152    
153  /*  /*
154   * Calculate the min/max range (in halfpixels)   * Calculate the min/max range
155   * relative to the _MACROBLOCK_ position   * relative to the _MACROBLOCK_ position
156   */   */
157  static void __inline  static void __inline
# Line 163  Line 161 
161                    int32_t * const max_dy,                    int32_t * const max_dy,
162                    const uint32_t x,                    const uint32_t x,
163                    const uint32_t y,                    const uint32_t y,
164                    const uint32_t block_sz,      /* block dimension, 8 or 16 */                    uint32_t block_sz, /* block dimension, 8 or 16 */
165                    const uint32_t width,                    const uint32_t width,
166                    const uint32_t height,                    const uint32_t height,
167                    const uint32_t fcode,                    const uint32_t fcode,
168                    const uint32_t quarterpel)                    const int qpel, /* 1 if the resulting range should be in qpel precision; otherwise 0 */
169                      const int rrv)
170  {  {
171            int k, m = qpel ? 4 : 2;
172            const int search_range = 32 << (fcode - 1);
173            int high = search_range - 1;
174            int low = -search_range;
175    
176            if (rrv) {
177                    high = RRV_MV_SCALEUP(high);
178                    low = RRV_MV_SCALEUP(low);
179                    block_sz *= 2;
180            }
181    
182          int k;          k = m * (int)(width - x * block_sz);
         const int search_range = 32 << (fcode - 1 - quarterpel);  
         const int high = search_range - 1;  
         const int low = -search_range;  
   
         k = 2 * (int)(width - x*block_sz);  
183          *max_dx = MIN(high, k);          *max_dx = MIN(high, k);
184          k = 2 * (int)(height -  y*block_sz);          k = m * (int)(height -  y * block_sz);
185          *max_dy = MIN(high, k);          *max_dy = MIN(high, k);
186    
187          k = -2 * (int)((x+1) * block_sz);          k = -m * (int)((x+1) * block_sz);
188          *min_dx = MAX(low, k);          *min_dx = MAX(low, k);
189          k = -2 * (int)((y+1) * block_sz);          k = -m * (int)((y+1) * block_sz);
190          *min_dy = MAX(low, k);          *min_dy = MAX(low, k);
   
191  }  }
192    
   
193  typedef void MainSearchFunc(int x, int y, const SearchData * const Data, int bDirection);  typedef void MainSearchFunc(int x, int y, const SearchData * const Data, int bDirection);
194    
195  static MainSearchFunc DiamondSearch, AdvDiamondSearch, SquareSearch;  static MainSearchFunc DiamondSearch, AdvDiamondSearch, SquareSearch;
# Line 219  Line 221 
221                  const int x,                  const int x,
222                  const int y,                  const int y,
223                  const uint32_t MotionFlags,                  const uint32_t MotionFlags,
224                  const uint32_t iQuant,                  const uint32_t GlobalFlags,
225                  SearchData * const Data,                  SearchData * const Data,
226                  const MBParam * const pParam,                  const MBParam * const pParam,
227                  const MACROBLOCK * const pMBs,                  const MACROBLOCK * const pMBs,
228                  const MACROBLOCK * const prevMBs,                  const MACROBLOCK * const prevMBs,
                 int inter4v,  
229                  MACROBLOCK * const pMB);                  MACROBLOCK * const pMB);
230    
231    
232  #ifdef _SMP  static WARPPOINTS
233  bool  GlobalMotionEst(const MACROBLOCK * const pMBs,
234  SMP_MotionEstimation(MBParam * const pParam,                                  const MBParam * const pParam,
235                                   FRAMEINFO * const current,                                  const FRAMEINFO * const current,
236                                   FRAMEINFO * const reference,                                  const FRAMEINFO * const reference,
237                                   const IMAGE * const pRefH,                                   const IMAGE * const pRefH,
238                                   const IMAGE * const pRefV,                                   const IMAGE * const pRefV,
239                                   const IMAGE * const pRefHV,                                  const IMAGE * const pRefHV      );
240                                   const uint32_t iLimit);  
241  #endif  #define iDiamondSize 2
242    
243    static __inline uint32_t
244    MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t GlobalFlags)
245    {
246            uint32_t Flags = MotionFlags;
247    
248            if (!(GlobalFlags & XVID_MODEDECISION_BITS))
249                    Flags &= ~(QUARTERPELREFINE16_BITS+QUARTERPELREFINE8_BITS+HALFPELREFINE16_BITS+HALFPELREFINE8_BITS+EXTSEARCH_BITS);
250    
251            if (Flags & EXTSEARCH_BITS)
252                    Flags |= HALFPELREFINE16_BITS;
253    
254            if (Flags & EXTSEARCH_BITS && MotionFlags & PMV_EXTSEARCH8)
255                    Flags |= HALFPELREFINE8_BITS;
256    
257            if (Flags & HALFPELREFINE16_BITS)
258                    Flags |= QUARTERPELREFINE16_BITS;
259    
260            if (Flags & HALFPELREFINE8_BITS) {
261                    Flags |= QUARTERPELREFINE8_BITS;
262                    Flags &= ~PMV_HALFPELREFINE8;
263            }
264    
265            if (Flags & QUARTERPELREFINE8_BITS)
266                    Flags &= ~PMV_QUARTERPELREFINE8;
267    
268            if (Flags & QUARTERPELREFINE16_BITS)
269                    Flags &= ~PMV_QUARTERPELREFINE16;
270    
271            if (!(GlobalFlags & XVID_QUARTERPEL))
272                    Flags &= ~(PMV_QUARTERPELREFINE16+PMV_QUARTERPELREFINE8+QUARTERPELREFINE16_BITS+QUARTERPELREFINE8_BITS);
273    
274            if (!(GlobalFlags & XVID_HALFPEL))
275                    Flags &= ~(PMV_EXTSEARCH16+PMV_HALFPELREFINE16+PMV_HALFPELREFINE8+HALFPELREFINE16_BITS+HALFPELREFINE8_BITS);
276    
277            if (GlobalFlags & (XVID_GREYSCALE + XVID_REDUCED))
278                    Flags &= ~(PMV_CHROMA16 + PMV_CHROMA8);
279    
280            return Flags;
281    }
282    
283    /* BITS mode decision and search */
284    
285    #include "../bitstream/zigzag.h"
286    #include "../quant/quant_mpeg4.h"
287    #include "../quant/quant_h263.h"
288    #include "../bitstream/vlc_codes.h"
289    #include "../dct/fdct.h"
290    
291    static int
292    CountMBBitsInter(SearchData * const Data,
293                                    const MACROBLOCK * const pMBs, const int x, const int y,
294                                    const MBParam * const pParam,
295                                    const uint32_t MotionFlags);
296    
297    static int
298    CountMBBitsInter4v(const SearchData * const Data,
299                                            MACROBLOCK * const pMB, const MACROBLOCK * const pMBs,
300                                            const int x, const int y,
301                                            const MBParam * const pParam, const uint32_t MotionFlags,
302                                            const VECTOR * const backup);
303    
304    static int
305    CountMBBitsIntra(const SearchData * const Data);
306    
307    int CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag);
308    int CodeCoeffInter_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag);
309    
310    #define LAMBDA          ( (int)(1.0*BITS_MULT) )
311    
312    static __inline unsigned int
313    Block_CalcBits( int16_t * const coeff,
314                                    int16_t * const data,
315                                    const uint32_t quant, const int quant_type,
316                                    uint32_t * cbp,
317                                    const int block)
318    {
319            int sum;
320            int bits;
321            const int lambda = LAMBDA*quant*quant;
322            int distortion = 0;
323            int i;
324    
325            fdct(data);
326    
327            if (quant_type == 0) sum = quant_inter(coeff, data, quant);
328            else sum = quant4_inter(coeff, data, quant);
329    
330            if (sum > 0) {
331                    *cbp |= 1 << (5 - block);
332                    bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
333            } else bits = 0;
334    
335            if (quant_type == 0) dequant_inter(coeff, coeff, quant);
336            else dequant4_inter(coeff, coeff, quant);
337    
338            for (i = 0; i < 64; i++) {
339                    distortion += (data[i] - coeff[i])*(data[i] - coeff[i]);
340            }
341            bits += (BITS_MULT*BITS_MULT*distortion)/lambda;
342    
343            return bits;
344    }
345    
346    static __inline unsigned int
347    Block_CalcBitsIntra(int16_t * const coeff,
348                                            int16_t * const data,
349                                            const uint32_t quant, const int quant_type,
350                                            uint32_t * cbp,
351                                            const int block,
352                                            int * dcpred)
353    {
354            int bits, i;
355            const int lambda = LAMBDA*quant*quant;
356            int distortion = 0;
357            uint32_t iDcScaler = get_dc_scaler(quant, block < 4);
358            int b_dc;
359    
360            fdct(data);
361            data[0] -= 1024;
362    
363            if (quant_type == 0) quant_intra_c(coeff, data, quant, iDcScaler); // MUST BE _C
364            else quant4_intra_c(coeff, data, quant, iDcScaler); // MUST BE _C
365    
366            b_dc = coeff[0];
367            if (block < 4) {
368                    coeff[0] -= *dcpred;
369                    *dcpred = b_dc;
370            }
371    
372            bits = BITS_MULT*CodeCoeffIntra_CalcBits(coeff, scan_tables[0]);
373            if (bits != 0) *cbp |= 1 << (5 - block);
374    
375            if (block < 4) bits += BITS_MULT*dcy_tab[coeff[0] + 255].len;
376            else bits += BITS_MULT*dcc_tab[coeff[0] + 255].len;
377    
378            coeff[0] = b_dc;
379            if (quant_type == 0) dequant_intra(coeff, coeff, quant, iDcScaler);
380            else dequant4_intra(coeff, coeff, quant, iDcScaler);
381    
382            for (i = 0; i < 64; i++) {
383                    distortion += (data[i] - coeff[i])*(data[i] - coeff[i]);
384            }
385    
386            bits += (BITS_MULT*BITS_MULT*distortion)/lambda;
387    
388            return bits;
389    }
390    
391  #endif                                                  /* _MOTION_EST_H_ */  #endif                                                  /* _MOTION_EST_H_ */

Legend:
Removed from v.1.1.2.7  
changed lines
  Added in v.1.9

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4