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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (view) (download)

1 : edgomez 1.2 /**************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Motion estimation header -
5 :     *
6 :     * This program is an implementation of a part of one or more MPEG-4
7 :     * Video tools as specified in ISO/IEC 14496-2 standard. Those intending
8 :     * to use this software module in hardware or software products are
9 :     * advised that its use may infringe existing patents or copyrights, and
10 :     * any such use would be at such party's own risk. The original
11 :     * developer of this software module and his/her company, and subsequent
12 :     * editors and their companies, will have no liability for use of this
13 :     * software or modifications or derivatives thereof.
14 :     *
15 :     * This program is free software; you can redistribute it and/or modify
16 :     * it under the terms of the GNU General Public License as published by
17 :     * the Free Software Foundation; either version 2 of the License, or
18 :     * (at your option) any later version.
19 :     *
20 :     * This program is distributed in the hope that it will be useful,
21 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 :     * GNU General Public License for more details.
24 :     *
25 :     * You should have received a copy of the GNU General Public License
26 :     * along with this program; if not, write to the Free Software
27 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 :     *
29 : syskin 1.9 * $Id: motion_est.h,v 1.8 2003/05/14 13:21:47 syskin Exp $
30 : edgomez 1.2 *
31 :     ***************************************************************************/
32 :    
33 :     #ifndef _MOTION_EST_H_
34 :     #define _MOTION_EST_H_
35 :    
36 :     #include "../portab.h"
37 :     #include "../global.h"
38 :     #include "../image/reduced.h"
39 :    
40 :     /* hard coded motion search parameters for motion_est and smp_motion_est */
41 :    
42 :     // very large value
43 :     #define MV_MAX_ERROR (4096 * 256)
44 :    
45 :     /* INTER bias for INTER/INTRA decision; mpeg4 spec suggests 2*nb */
46 :     #define MV16_INTER_BIAS 512
47 :    
48 :     /* vector map (vlc delta size) smoother parameters ! float !*/
49 :     #define NEIGH_TEND_16X16 10.5
50 :     #define NEIGH_TEND_8X8 40.0
51 :     #define NEIGH_8X8_BIAS 30
52 :    
53 : syskin 1.8 #define BITS_MULT 16
54 :    
55 : edgomez 1.2 /* Parameters which control inter/inter4v decision */
56 :     #define IMV16X16 2
57 :    
58 :     static const int lambda_vec16[32] =
59 :     { 0 ,(int)(1.00235 * NEIGH_TEND_16X16 + 0.5),
60 :     (int)(1.15582*NEIGH_TEND_16X16 + 0.5), (int)(1.31976*NEIGH_TEND_16X16 + 0.5),
61 :     (int)(1.49591*NEIGH_TEND_16X16 + 0.5), (int)(1.68601*NEIGH_TEND_16X16 + 0.5),
62 :     (int)(1.89187*NEIGH_TEND_16X16 + 0.5), (int)(2.11542*NEIGH_TEND_16X16 + 0.5),
63 :     (int)(2.35878*NEIGH_TEND_16X16 + 0.5), (int)(2.62429*NEIGH_TEND_16X16 + 0.5),
64 :     (int)(2.91455*NEIGH_TEND_16X16 + 0.5), (int)(3.23253*NEIGH_TEND_16X16 + 0.5),
65 :     (int)(3.58158*NEIGH_TEND_16X16 + 0.5), (int)(3.96555*NEIGH_TEND_16X16 + 0.5),
66 :     (int)(4.38887*NEIGH_TEND_16X16 + 0.5), (int)(4.85673*NEIGH_TEND_16X16 + 0.5),
67 :     (int)(5.37519*NEIGH_TEND_16X16 + 0.5), (int)(5.95144*NEIGH_TEND_16X16 + 0.5),
68 :     (int)(6.59408*NEIGH_TEND_16X16 + 0.5), (int)(7.31349*NEIGH_TEND_16X16 + 0.5),
69 :     (int)(8.12242*NEIGH_TEND_16X16 + 0.5), (int)(9.03669*NEIGH_TEND_16X16 + 0.5),
70 :     (int)(10.0763*NEIGH_TEND_16X16 + 0.5), (int)(11.2669*NEIGH_TEND_16X16 + 0.5),
71 :     (int)(12.6426*NEIGH_TEND_16X16 + 0.5), (int)(14.2493*NEIGH_TEND_16X16 + 0.5),
72 :     (int)(16.1512*NEIGH_TEND_16X16 + 0.5), (int)(18.442*NEIGH_TEND_16X16 + 0.5),
73 :     (int)(21.2656*NEIGH_TEND_16X16 + 0.5), (int)(24.8580*NEIGH_TEND_16X16 + 0.5),
74 :     (int)(29.6436*NEIGH_TEND_16X16 + 0.5), (int)(36.4949*NEIGH_TEND_16X16 + 0.5) };
75 :    
76 :     static const int lambda_vec8[32] =
77 :     { 0 ,(int)(1.00235 * NEIGH_TEND_8X8 + 0.5),
78 :     (int)(1.15582 + NEIGH_TEND_8X8 + 0.5), (int)(1.31976*NEIGH_TEND_8X8 + 0.5),
79 :     (int)(1.49591*NEIGH_TEND_8X8 + 0.5), (int)(1.68601*NEIGH_TEND_8X8 + 0.5),
80 :     (int)(1.89187*NEIGH_TEND_8X8 + 0.5), (int)(2.11542*NEIGH_TEND_8X8 + 0.5),
81 :     (int)(2.35878*NEIGH_TEND_8X8 + 0.5), (int)(2.62429*NEIGH_TEND_8X8 + 0.5),
82 :     (int)(2.91455*NEIGH_TEND_8X8 + 0.5), (int)(3.23253*NEIGH_TEND_8X8 + 0.5),
83 :     (int)(3.58158*NEIGH_TEND_8X8 + 0.5), (int)(3.96555*NEIGH_TEND_8X8 + 0.5),
84 :     (int)(4.38887*NEIGH_TEND_8X8 + 0.5), (int)(4.85673*NEIGH_TEND_8X8 + 0.5),
85 :     (int)(5.37519*NEIGH_TEND_8X8 + 0.5), (int)(5.95144*NEIGH_TEND_8X8 + 0.5),
86 :     (int)(6.59408*NEIGH_TEND_8X8 + 0.5), (int)(7.31349*NEIGH_TEND_8X8 + 0.5),
87 :     (int)(8.12242*NEIGH_TEND_8X8 + 0.5), (int)(9.03669*NEIGH_TEND_8X8 + 0.5),
88 :     (int)(10.0763*NEIGH_TEND_8X8 + 0.5), (int)(11.2669*NEIGH_TEND_8X8 + 0.5),
89 :     (int)(12.6426*NEIGH_TEND_8X8 + 0.5), (int)(14.2493*NEIGH_TEND_8X8 + 0.5),
90 :     (int)(16.1512*NEIGH_TEND_8X8 + 0.5), (int)(18.442*NEIGH_TEND_8X8 + 0.5),
91 :     (int)(21.2656*NEIGH_TEND_8X8 + 0.5), (int)(24.8580*NEIGH_TEND_8X8 + 0.5),
92 :     (int)(29.6436*NEIGH_TEND_8X8 + 0.5), (int)(36.4949*NEIGH_TEND_8X8 + 0.5) };
93 :    
94 :     // mv.length table
95 : syskin 1.5 static const int mvtab[64] = {
96 :     1, 2, 3, 4, 6, 7, 7, 7,
97 :     9, 9, 9, 10, 10, 10, 10, 10,
98 :     10, 10, 10, 10, 10, 10, 10, 10,
99 :     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 : edgomez 1.2
104 :     static const int DQtab[4] = {
105 :     -1, -2, 1, 2
106 :     };
107 :    
108 :     #define RRV_MV_SCALEDOWN(a) ( (a)>=0 ? (a+1)/2 : (a-1)/2 )
109 :    
110 :     typedef struct
111 :     {
112 :     // general fields
113 :     int max_dx, min_dx, max_dy, min_dy;
114 :     uint32_t rounding;
115 :     VECTOR predMV;
116 :     VECTOR * currentMV;
117 :     VECTOR * currentQMV;
118 :     int32_t * iMinSAD;
119 : syskin 1.6 const uint8_t * RefP[6]; // N, V, H, HV, cU, cV
120 : edgomez 1.2 const uint8_t * CurU;
121 :     const uint8_t * CurV;
122 :     uint8_t * RefQ;
123 :     const uint8_t * Cur;
124 :     uint32_t lambda16;
125 :     uint32_t lambda8;
126 :     uint32_t iEdgedWidth;
127 :     uint32_t iFcode;
128 :     int * temp;
129 :     int qpel, qpel_precision;
130 :     int chroma;
131 :     int rrv;
132 :     //fields for interpolate and direct modes
133 : syskin 1.6 const uint8_t * b_RefP[6]; // N, V, H, HV, cU, cV
134 : edgomez 1.2 VECTOR bpredMV;
135 :     uint32_t bFcode;
136 :     // fields for direct mode
137 :     VECTOR directmvF[4];
138 :     VECTOR directmvB[4];
139 :     const VECTOR * referencemv;
140 : syskin 1.8 // BITS/R-D stuff
141 : edgomez 1.4 int16_t * dctSpace;
142 : syskin 1.7 uint32_t iQuant;
143 :     uint32_t quant_type;
144 : edgomez 1.2
145 :     } SearchData;
146 :    
147 :    
148 :     typedef void(CheckFunc)(const int x, const int y,
149 :     const int Direction, int * const dir,
150 :     const SearchData * const Data);
151 :     CheckFunc *CheckCandidate;
152 :    
153 :     /*
154 :     * Calculate the min/max range
155 :     * relative to the _MACROBLOCK_ position
156 :     */
157 :     static void __inline
158 :     get_range(int32_t * const min_dx,
159 :     int32_t * const max_dx,
160 :     int32_t * const min_dy,
161 :     int32_t * const max_dy,
162 :     const uint32_t x,
163 :     const uint32_t y,
164 :     uint32_t block_sz, /* block dimension, 8 or 16 */
165 :     const uint32_t width,
166 :     const uint32_t height,
167 :     const uint32_t fcode,
168 :     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 :     k = m * (int)(width - x * block_sz);
183 :     *max_dx = MIN(high, k);
184 :     k = m * (int)(height - y * block_sz);
185 :     *max_dy = MIN(high, k);
186 :    
187 :     k = -m * (int)((x+1) * block_sz);
188 :     *min_dx = MAX(low, k);
189 :     k = -m * (int)((y+1) * block_sz);
190 :     *min_dy = MAX(low, k);
191 :     }
192 :    
193 :     typedef void MainSearchFunc(int x, int y, const SearchData * const Data, int bDirection);
194 :    
195 :     static MainSearchFunc DiamondSearch, AdvDiamondSearch, SquareSearch;
196 :    
197 :     static void Search8(const SearchData * const OldData,
198 :     const int x, const int y,
199 :     const uint32_t MotionFlags,
200 :     const MBParam * const pParam,
201 :     MACROBLOCK * const pMB,
202 :     const MACROBLOCK * const pMBs,
203 :     const int block,
204 :     SearchData * const Data);
205 :    
206 :     bool
207 :     MotionEstimation(MBParam * const pParam,
208 : syskin 1.3 FRAMEINFO * const current,
209 :     FRAMEINFO * const reference,
210 :     const IMAGE * const pRefH,
211 :     const IMAGE * const pRefV,
212 :     const IMAGE * const pRefHV,
213 :     const uint32_t iLimit);
214 : edgomez 1.2
215 :     static void
216 :     SearchP(const IMAGE * const pRef,
217 :     const uint8_t * const pRefH,
218 :     const uint8_t * const pRefV,
219 :     const uint8_t * const pRefHV,
220 :     const IMAGE * const pCur,
221 :     const int x,
222 :     const int y,
223 :     const uint32_t MotionFlags,
224 :     const uint32_t GlobalFlags,
225 :     SearchData * const Data,
226 :     const MBParam * const pParam,
227 :     const MACROBLOCK * const pMBs,
228 :     const MACROBLOCK * const prevMBs,
229 :     MACROBLOCK * const pMB);
230 :    
231 :    
232 :     static WARPPOINTS
233 :     GlobalMotionEst(const MACROBLOCK * const pMBs,
234 :     const MBParam * const pParam,
235 :     const FRAMEINFO * const current,
236 :     const FRAMEINFO * const reference,
237 :     const IMAGE * const pRefH,
238 :     const IMAGE * const pRefV,
239 :     const IMAGE * const pRefHV );
240 :    
241 :     #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 : syskin 1.8 if (Flags & QUARTERPELREFINE16_BITS)
269 :     Flags &= ~PMV_QUARTERPELREFINE16;
270 :    
271 : edgomez 1.2 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 : syskin 1.7 #include "../dct/fdct.h"
290 : edgomez 1.2
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 : syskin 1.8
310 : syskin 1.10 /* one over lambda for R-D mode decision and motion search */
311 :     #define LAMBDA ( (int)(BITS_MULT/1.0) )
312 : syskin 1.8
313 : syskin 1.7 static __inline unsigned int
314 : syskin 1.8 Block_CalcBits( int16_t * const coeff,
315 :     int16_t * const data,
316 : syskin 1.10 int16_t * const dqcoeff,
317 : syskin 1.8 const uint32_t quant, const int quant_type,
318 :     uint32_t * cbp,
319 :     const int block)
320 : syskin 1.7 {
321 :     int sum;
322 : syskin 1.8 int bits;
323 :     int distortion = 0;
324 :     int i;
325 : syskin 1.7
326 :     fdct(data);
327 :    
328 :     if (quant_type == 0) sum = quant_inter(coeff, data, quant);
329 :     else sum = quant4_inter(coeff, data, quant);
330 :    
331 :     if (sum > 0) {
332 :     *cbp |= 1 << (5 - block);
333 : syskin 1.8 bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
334 :     } else bits = 0;
335 :    
336 : syskin 1.10 if (quant_type == 0) dequant_inter(dqcoeff, coeff, quant);
337 :     else dequant4_inter(dqcoeff, coeff, quant);
338 : syskin 1.8
339 :     for (i = 0; i < 64; i++) {
340 : syskin 1.10 distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]);
341 : syskin 1.8 }
342 : syskin 1.10
343 :     bits += (LAMBDA*distortion)/(quant*quant);
344 : syskin 1.8
345 :     return bits;
346 : syskin 1.7 }
347 :    
348 :     static __inline unsigned int
349 : syskin 1.8 Block_CalcBitsIntra(int16_t * const coeff,
350 :     int16_t * const data,
351 : syskin 1.10 int16_t * const dqcoeff,
352 : syskin 1.8 const uint32_t quant, const int quant_type,
353 :     uint32_t * cbp,
354 :     const int block,
355 :     int * dcpred)
356 : syskin 1.7 {
357 : syskin 1.8 int bits, i;
358 : syskin 1.7 int distortion = 0;
359 : syskin 1.9 uint32_t iDcScaler = get_dc_scaler(quant, block < 4);
360 : syskin 1.8 int b_dc;
361 : syskin 1.7
362 :     fdct(data);
363 : syskin 1.8 data[0] -= 1024;
364 :    
365 : syskin 1.10 if (quant_type == 0) quant_intra(coeff, data, quant, iDcScaler);
366 :     else quant4_intra(coeff, data, quant, iDcScaler);
367 : syskin 1.7
368 : syskin 1.8 b_dc = coeff[0];
369 :     if (block < 4) {
370 :     coeff[0] -= *dcpred;
371 :     *dcpred = b_dc;
372 :     }
373 : syskin 1.7
374 : syskin 1.8 bits = BITS_MULT*CodeCoeffIntra_CalcBits(coeff, scan_tables[0]);
375 :     if (bits != 0) *cbp |= 1 << (5 - block);
376 : syskin 1.9
377 :     if (block < 4) bits += BITS_MULT*dcy_tab[coeff[0] + 255].len;
378 :     else bits += BITS_MULT*dcc_tab[coeff[0] + 255].len;
379 : syskin 1.8
380 :     coeff[0] = b_dc;
381 : syskin 1.10 if (quant_type == 0) dequant_intra(dqcoeff, coeff, quant, iDcScaler);
382 :     else dequant4_intra(dqcoeff, coeff, quant, iDcScaler);
383 : syskin 1.7
384 : syskin 1.8 for (i = 0; i < 64; i++) {
385 : syskin 1.10 distortion += (data[i] - dqcoeff[i])*(data[i] - dqcoeff[i]);
386 : syskin 1.7 }
387 :    
388 : syskin 1.10 bits += (LAMBDA*distortion)/(quant*quant);
389 : syskin 1.7
390 :     return bits;
391 :     }
392 : edgomez 1.2
393 :     #endif /* _MOTION_EST_H_ */

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