[svn] / branches / dev-api-4 / xvidcore / src / motion / gmc.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/src/motion/gmc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1160 - (view) (download)

1 : edgomez 1142 /*****************************************************************************
2 : chl 1077 *
3 : edgomez 1142 * XVID MPEG-4 VIDEO CODEC
4 :     * - GMC interpolation module -
5 : chl 1077 *
6 : edgomez 1142 * Copyright(C) 2002-2003 Pascal Massimino <skal@planet-d.net>
7 : chl 1077 *
8 : edgomez 1142 * This program is free software ; you can redistribute it and/or modify
9 :     * it under the terms of the GNU General Public License as published by
10 :     * the Free Software Foundation ; either version 2 of the License, or
11 :     * (at your option) any later version.
12 : chl 1077 *
13 : edgomez 1142 * This program is distributed in the hope that it will be useful,
14 :     * but WITHOUT ANY WARRANTY ; without even the implied warranty of
15 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 :     * GNU General Public License for more details.
17 : chl 1077 *
18 : edgomez 1142 * You should have received a copy of the GNU General Public License
19 :     * along with this program ; if not, write to the Free Software
20 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 :     *
22 : edgomez 1160 * $Id: gmc.c,v 1.1.2.5 2003-09-30 18:20:31 edgomez Exp $
23 : edgomez 1142 *
24 :     ****************************************************************************/
25 : chl 1077
26 :     #include "../portab.h"
27 :     #include "../global.h"
28 :     #include "../encoder.h"
29 :     #include "gmc.h"
30 :    
31 :     #include <stdio.h>
32 :    
33 : edgomez 1160 /* ************************************************************
34 :     * Pts = 2 or 3
35 :     *
36 :     * Warning! *src is the global frame pointer (that is: adress
37 :     * of pixel 0,0), not the macroblock one.
38 :     * Conversely, *dst is the macroblock top-left adress.
39 :     */
40 : chl 1077
41 :     void Predict_16x16_C(const NEW_GMC_DATA * const This,
42 : syskin 1085 uint8_t *dst, const uint8_t *src,
43 :     int dststride, int srcstride, int x, int y, int rounding)
44 : chl 1077 {
45 : syskin 1085 const int W = This->sW;
46 :     const int H = This->sH;
47 :     const int rho = 3 - This->accuracy;
48 :     const int Rounder = ( (1<<7) - (rounding<<(2*rho)) ) << 16;
49 : chl 1077
50 : syskin 1085 const int dUx = This->dU[0];
51 :     const int dVx = This->dV[0];
52 :     const int dUy = This->dU[1];
53 :     const int dVy = This->dV[1];
54 : chl 1077
55 : syskin 1085 int Uo = This->Uo + 16*(dUy*y + dUx*x);
56 :     int Vo = This->Vo + 16*(dVy*y + dVx*x);
57 : chl 1077
58 : syskin 1085 int i, j;
59 : chl 1077
60 : syskin 1085 dst += 16;
61 : syskin 1117 for (j=16; j>0; --j) {
62 :     int U = Uo, V = Vo;
63 :     Uo += dUy; Vo += dVy;
64 :     for (i=-16; i<0; ++i) {
65 :     unsigned int f0, f1, ri = 16, rj = 16;
66 :     int Offset;
67 :     int u = ( U >> 16 ) << rho;
68 :     int v = ( V >> 16 ) << rho;
69 : chl 1077
70 : syskin 1117 U += dUx; V += dVx;
71 : chl 1077
72 : syskin 1117 if (u > 0 && u <= W) { ri = MTab[u&15]; Offset = u>>4; }
73 :     else if (u > W) Offset = W>>4;
74 :     else Offset = -1;
75 :    
76 :     if (v > 0 && v <= H) { rj = MTab[v&15]; Offset += (v>>4)*srcstride; }
77 :     else if (v > H) Offset += (H>>4)*srcstride;
78 :     else Offset -= srcstride;
79 : chl 1077
80 : syskin 1117 f0 = src[Offset + 0];
81 :     f0 |= src[Offset + 1] << 16;
82 :     f1 = src[Offset + srcstride + 0];
83 :     f1 |= src[Offset + srcstride + 1] << 16;
84 :     f0 = (ri*f0)>>16;
85 :     f1 = (ri*f1) & 0x0fff0000;
86 :     f0 |= f1;
87 :     f0 = (rj*f0 + Rounder) >> 24;
88 : chl 1077
89 : syskin 1117 dst[i] = (uint8_t)f0;
90 :     }
91 :     dst += dststride;
92 : syskin 1085 }
93 : chl 1077 }
94 :    
95 :     void Predict_8x8_C(const NEW_GMC_DATA * const This,
96 : syskin 1085 uint8_t *uDst, const uint8_t *uSrc,
97 :     uint8_t *vDst, const uint8_t *vSrc,
98 :     int dststride, int srcstride, int x, int y, int rounding)
99 : chl 1077 {
100 : syskin 1085 const int W = This->sW >> 1;
101 :     const int H = This->sH >> 1;
102 :     const int rho = 3-This->accuracy;
103 :     const int32_t Rounder = ( 128 - (rounding<<(2*rho)) ) << 16;
104 : chl 1077
105 : syskin 1085 const int32_t dUx = This->dU[0];
106 :     const int32_t dVx = This->dV[0];
107 :     const int32_t dUy = This->dU[1];
108 :     const int32_t dVy = This->dV[1];
109 : chl 1077
110 : syskin 1085 int32_t Uo = This->Uco + 8*(dUy*y + dUx*x);
111 :     int32_t Vo = This->Vco + 8*(dVy*y + dVx*x);
112 : chl 1077
113 : syskin 1085 int i, j;
114 : chl 1077
115 : syskin 1085 uDst += 8;
116 :     vDst += 8;
117 : syskin 1117 for (j=8; j>0; --j) {
118 :     int32_t U = Uo, V = Vo;
119 :     Uo += dUy; Vo += dVy;
120 : chl 1077
121 : syskin 1117 for (i=-8; i<0; ++i) {
122 :     int Offset;
123 :     uint32_t f0, f1, ri, rj;
124 :     int32_t u, v;
125 : chl 1077
126 : syskin 1117 u = ( U >> 16 ) << rho;
127 :     v = ( V >> 16 ) << rho;
128 :     U += dUx; V += dVx;
129 : chl 1077
130 : syskin 1117 if (u > 0 && u <= W) {
131 :     ri = MTab[u&15];
132 :     Offset = u>>4;
133 :     } else {
134 :     ri = 16;
135 :     if (u>W) Offset = W>>4;
136 :     else Offset = -1;
137 :     }
138 :    
139 :     if (v > 0 && v <= H) {
140 :     rj = MTab[v&15];
141 :     Offset += (v>>4)*srcstride;
142 :     } else {
143 :     rj = 16;
144 :     if (v>H) Offset += (H>>4)*srcstride;
145 :     else Offset -= srcstride;
146 :     }
147 : chl 1077
148 : syskin 1117 f0 = uSrc[Offset + 0];
149 :     f0 |= uSrc[Offset + 1] << 16;
150 :     f1 = uSrc[Offset + srcstride + 0];
151 :     f1 |= uSrc[Offset + srcstride + 1] << 16;
152 :     f0 = (ri*f0)>>16;
153 :     f1 = (ri*f1) & 0x0fff0000;
154 :     f0 |= f1;
155 :     f0 = (rj*f0 + Rounder) >> 24;
156 : chl 1077
157 : syskin 1117 uDst[i] = (uint8_t)f0;
158 : chl 1077
159 : syskin 1117 f0 = vSrc[Offset + 0];
160 :     f0 |= vSrc[Offset + 1] << 16;
161 :     f1 = vSrc[Offset + srcstride + 0];
162 :     f1 |= vSrc[Offset + srcstride + 1] << 16;
163 :     f0 = (ri*f0)>>16;
164 :     f1 = (ri*f1) & 0x0fff0000;
165 :     f0 |= f1;
166 :     f0 = (rj*f0 + Rounder) >> 24;
167 : chl 1077
168 : syskin 1117 vDst[i] = (uint8_t)f0;
169 :     }
170 :     uDst += dststride;
171 :     vDst += dststride;
172 : syskin 1085 }
173 : chl 1077 }
174 :    
175 : syskin 1085 void get_average_mv_C(const NEW_GMC_DATA * const Dsp, VECTOR * const mv,
176 :     int x, int y, int qpel)
177 : chl 1077 {
178 : syskin 1085 int i, j;
179 :     int vx = 0, vy = 0;
180 :     int32_t uo = Dsp->Uo + 16*(Dsp->dU[1]*y + Dsp->dU[0]*x);
181 :     int32_t vo = Dsp->Vo + 16*(Dsp->dV[1]*y + Dsp->dV[0]*x);
182 :     for (j=16; j>0; --j)
183 :     {
184 :     int32_t U, V;
185 :     U = uo; uo += Dsp->dU[1];
186 :     V = vo; vo += Dsp->dV[1];
187 :     for (i=16; i>0; --i)
188 :     {
189 :     int32_t u,v;
190 :     u = U >> 16; U += Dsp->dU[0]; vx += u;
191 :     v = V >> 16; V += Dsp->dV[0]; vy += v;
192 :     }
193 :     }
194 : edgomez 1160 vx -= (256*x+120) << (5+Dsp->accuracy); /* 120 = 15*16/2 */
195 : syskin 1085 vy -= (256*y+120) << (5+Dsp->accuracy);
196 : chl 1077
197 : syskin 1085 mv->x = RSHIFT( vx, 8+Dsp->accuracy - qpel );
198 :     mv->y = RSHIFT( vy, 8+Dsp->accuracy - qpel );
199 : chl 1077 }
200 :    
201 : edgomez 1160 /* ************************************************************
202 :     * simplified version for 1 warp point
203 :     */
204 : chl 1077
205 :     void Predict_1pt_16x16_C(const NEW_GMC_DATA * const This,
206 : syskin 1085 uint8_t *Dst, const uint8_t *Src,
207 :     int dststride, int srcstride, int x, int y, int rounding)
208 : chl 1077 {
209 : syskin 1085 const int W = This->sW;
210 :     const int H = This->sH;
211 :     const int rho = 3-This->accuracy;
212 :     const int32_t Rounder = ( 128 - (rounding<<(2*rho)) ) << 16;
213 : chl 1077
214 :    
215 : edgomez 1160 int32_t uo = This->Uo + (x<<8); /* ((16*x)<<4) */
216 : syskin 1085 int32_t vo = This->Vo + (y<<8);
217 :     const uint32_t ri = MTab[uo & 15];
218 :     const uint32_t rj = MTab[vo & 15];
219 :     int i, j;
220 : chl 1077
221 : syskin 1085 int32_t Offset;
222 :     if ((uint32_t)vo<=(uint32_t)H) Offset = (vo>>4)*srcstride;
223 :     else if (vo>H) Offset = ( H>>4)*srcstride;
224 :     else Offset =-16*srcstride;
225 :     if ((uint32_t)uo<=(uint32_t)W) Offset += (uo>>4);
226 :     else if (uo>W) Offset += ( W>>4);
227 :     else Offset -= 16;
228 : chl 1077
229 : syskin 1085 Dst += 16;
230 : chl 1077
231 : syskin 1085 for(j=16; j>0; --j, Offset+=srcstride-16)
232 :     {
233 :     for(i=-16; i<0; ++i, ++Offset)
234 :     {
235 :     uint32_t f0, f1;
236 :     f0 = Src[ Offset +0 ];
237 :     f0 |= Src[ Offset +1 ] << 16;
238 :     f1 = Src[ Offset+srcstride +0 ];
239 :     f1 |= Src[ Offset+srcstride +1 ] << 16;
240 :     f0 = (ri*f0)>>16;
241 :     f1 = (ri*f1) & 0x0fff0000;
242 :     f0 |= f1;
243 :     f0 = ( rj*f0 + Rounder ) >> 24;
244 :     Dst[i] = (uint8_t)f0;
245 :     }
246 :     Dst += dststride;
247 :     }
248 :     }
249 : chl 1077
250 :     void Predict_1pt_8x8_C(const NEW_GMC_DATA * const This,
251 : syskin 1085 uint8_t *uDst, const uint8_t *uSrc,
252 :     uint8_t *vDst, const uint8_t *vSrc,
253 :     int dststride, int srcstride, int x, int y, int rounding)
254 : chl 1077 {
255 : syskin 1085 const int W = This->sW >> 1;
256 :     const int H = This->sH >> 1;
257 :     const int rho = 3-This->accuracy;
258 :     const int32_t Rounder = ( 128 - (rounding<<(2*rho)) ) << 16;
259 : chl 1077
260 : syskin 1085 int32_t uo = This->Uco + (x<<7);
261 :     int32_t vo = This->Vco + (y<<7);
262 :     const uint32_t rri = MTab[uo & 15];
263 :     const uint32_t rrj = MTab[vo & 15];
264 :     int i, j;
265 : chl 1077
266 : syskin 1085 int32_t Offset;
267 :     if ((uint32_t)vo<=(uint32_t)H) Offset = (vo>>4)*srcstride;
268 :     else if (vo>H) Offset = ( H>>4)*srcstride;
269 :     else Offset =-8*srcstride;
270 :     if ((uint32_t)uo<=(uint32_t)W) Offset += (uo>>4);
271 :     else if (uo>W) Offset += (W>>4);
272 :     else Offset -= 8;
273 : chl 1077
274 : syskin 1085 uDst += 8;
275 :     vDst += 8;
276 :     for(j=8; j>0; --j, Offset+=srcstride-8)
277 :     {
278 :     for(i=-8; i<0; ++i, Offset++)
279 :     {
280 :     uint32_t f0, f1;
281 :     f0 = uSrc[ Offset + 0 ];
282 :     f0 |= uSrc[ Offset + 1 ] << 16;
283 :     f1 = uSrc[ Offset + srcstride + 0 ];
284 :     f1 |= uSrc[ Offset + srcstride + 1 ] << 16;
285 :     f0 = (rri*f0)>>16;
286 :     f1 = (rri*f1) & 0x0fff0000;
287 :     f0 |= f1;
288 :     f0 = ( rrj*f0 + Rounder ) >> 24;
289 :     uDst[i] = (uint8_t)f0;
290 : chl 1077
291 : syskin 1085 f0 = vSrc[ Offset + 0 ];
292 :     f0 |= vSrc[ Offset + 1 ] << 16;
293 :     f1 = vSrc[ Offset + srcstride + 0 ];
294 :     f1 |= vSrc[ Offset + srcstride + 1 ] << 16;
295 :     f0 = (rri*f0)>>16;
296 :     f1 = (rri*f1) & 0x0fff0000;
297 :     f0 |= f1;
298 :     f0 = ( rrj*f0 + Rounder ) >> 24;
299 :     vDst[i] = (uint8_t)f0;
300 :     }
301 :     uDst += dststride;
302 :     vDst += dststride;
303 :     }
304 : chl 1077 }
305 :    
306 : syskin 1085 void get_average_mv_1pt_C(const NEW_GMC_DATA * const Dsp, VECTOR * const mv,
307 :     int x, int y, int qpel)
308 : chl 1077 {
309 : syskin 1085 mv->x = RSHIFT(Dsp->Uo<<qpel, 3);
310 :     mv->y = RSHIFT(Dsp->Vo<<qpel, 3);
311 : chl 1077 }
312 :    
313 : edgomez 1160 /* *************************************************************
314 :     * Warning! It's Accuracy being passed, not 'resolution'!
315 :     */
316 : chl 1077
317 :     void generate_GMCparameters( int nb_pts, const int accuracy,
318 : syskin 1085 const WARPPOINTS *const pts,
319 :     const int width, const int height,
320 :     NEW_GMC_DATA *const gmc)
321 : chl 1077 {
322 : syskin 1085 gmc->sW = width << 4;
323 :     gmc->sH = height << 4;
324 :     gmc->accuracy = accuracy;
325 :     gmc->num_wp = nb_pts;
326 : chl 1077
327 : edgomez 1160 /* reduce the number of points, if possible */
328 : syskin 1085 if (nb_pts<3 || (pts->duv[2].x==-pts->duv[1].y && pts->duv[2].y==pts->duv[1].x)) {
329 :     if (nb_pts<2 || (pts->duv[1].x==0 && pts->duv[1].y==0)) {
330 :     if (nb_pts<1 || (pts->duv[0].x==0 && pts->duv[0].y==0)) {
331 :     nb_pts = 0;
332 :     }
333 :     else nb_pts = 1;
334 :     }
335 :     else nb_pts = 2;
336 :     }
337 :     else nb_pts = 3;
338 :    
339 : edgomez 1160 /* now, nb_pts stores the actual number of points required for interpolation */
340 : chl 1077
341 : syskin 1085 if (nb_pts<=1)
342 :     {
343 :     if (nb_pts==1) {
344 : edgomez 1160 /* store as 4b fixed point */
345 : syskin 1085 gmc->Uo = pts->duv[0].x << accuracy;
346 :     gmc->Vo = pts->duv[0].y << accuracy;
347 : edgomez 1160 gmc->Uco = ((pts->duv[0].x>>1) | (pts->duv[0].x&1)) << accuracy; /* DIV2RND() */
348 :     gmc->Vco = ((pts->duv[0].y>>1) | (pts->duv[0].y&1)) << accuracy; /* DIV2RND() */
349 : syskin 1085 }
350 : edgomez 1160 else { /* zero points?! */
351 : syskin 1085 gmc->Uo = gmc->Vo = 0;
352 :     gmc->Uco = gmc->Vco = 0;
353 :     }
354 : chl 1077
355 : syskin 1085 gmc->predict_16x16 = Predict_1pt_16x16_C;
356 :     gmc->predict_8x8 = Predict_1pt_8x8_C;
357 :     gmc->get_average_mv = get_average_mv_1pt_C;
358 :     }
359 : edgomez 1160 else { /* 2 or 3 points */
360 :     const int rho = 3 - accuracy; /* = {3,2,1,0} for Acc={0,1,2,3} */
361 : syskin 1085 int Alpha = log2bin(width-1);
362 :     int Ws = 1 << Alpha;
363 : chl 1077
364 : edgomez 1160 gmc->dU[0] = 16*Ws + RDIV( 8*Ws*pts->duv[1].x, width ); /* dU/dx */
365 :     gmc->dV[0] = RDIV( 8*Ws*pts->duv[1].y, width ); /* dV/dx */
366 : chl 1077
367 : syskin 1085 /* disabled, because possibly buggy? */
368 : chl 1077
369 : edgomez 1160 #if 0
370 :     if (nb_pts==2) {
371 :     gmc->dU[1] = -gmc->dV[0]; /* -Sin */
372 :     gmc->dV[1] = gmc->dU[0] ; /* Cos */
373 : syskin 1085 }
374 : edgomez 1160 else
375 :     #endif
376 : chl 1077 {
377 : syskin 1085 const int Beta = log2bin(height-1);
378 :     const int Hs = 1<<Beta;
379 : edgomez 1160 gmc->dU[1] = RDIV( 8*Hs*pts->duv[2].x, height ); /* dU/dy */
380 :     gmc->dV[1] = 16*Hs + RDIV( 8*Hs*pts->duv[2].y, height ); /* dV/dy */
381 : syskin 1085 if (Beta>Alpha) {
382 :     gmc->dU[0] <<= (Beta-Alpha);
383 :     gmc->dV[0] <<= (Beta-Alpha);
384 :     Alpha = Beta;
385 :     Ws = Hs;
386 :     }
387 :     else {
388 :     gmc->dU[1] <<= Alpha - Beta;
389 :     gmc->dV[1] <<= Alpha - Beta;
390 :     }
391 :     }
392 : edgomez 1160 /* upscale to 16b fixed-point */
393 : syskin 1085 gmc->dU[0] <<= (16-Alpha - rho);
394 :     gmc->dU[1] <<= (16-Alpha - rho);
395 :     gmc->dV[0] <<= (16-Alpha - rho);
396 :     gmc->dV[1] <<= (16-Alpha - rho);
397 : chl 1077
398 : syskin 1085 gmc->Uo = ( pts->duv[0].x <<(16+ accuracy)) + (1<<15);
399 :     gmc->Vo = ( pts->duv[0].y <<(16+ accuracy)) + (1<<15);
400 :     gmc->Uco = ((pts->duv[0].x-1)<<(17+ accuracy)) + (1<<17);
401 :     gmc->Vco = ((pts->duv[0].y-1)<<(17+ accuracy)) + (1<<17);
402 :     gmc->Uco = (gmc->Uco + gmc->dU[0] + gmc->dU[1])>>2;
403 :     gmc->Vco = (gmc->Vco + gmc->dV[0] + gmc->dV[1])>>2;
404 : chl 1077
405 : syskin 1085 gmc->predict_16x16 = Predict_16x16_C;
406 :     gmc->predict_8x8 = Predict_8x8_C;
407 :     gmc->get_average_mv = get_average_mv_C;
408 :     }
409 : chl 1077 }
410 :    
411 : edgomez 1160 /* *******************************************************************
412 :     * quick and dirty routine to generate the full warped image
413 :     * (pGMC != NULL) or just all average Motion Vectors (pGMC == NULL) */
414 : chl 1077
415 :     void
416 : edgomez 1160 generate_GMCimage( const NEW_GMC_DATA *const gmc_data, /* [input] precalculated data */
417 :     const IMAGE *const pRef, /* [input] */
418 : chl 1077 const int mb_width,
419 :     const int mb_height,
420 :     const int stride,
421 :     const int stride2,
422 : edgomez 1160 const int fcode, /* [input] some parameters... */
423 :     const int32_t quarterpel, /* [input] for rounding avgMV */
424 :     const int reduced_resolution, /* [input] ignored */
425 :     const int32_t rounding, /* [input] for rounding image data */
426 :     MACROBLOCK *const pMBs, /* [output] average motion vectors */
427 :     IMAGE *const pGMC) /* [output] full warped image */
428 : chl 1077 {
429 :    
430 :     unsigned int mj,mi;
431 :     VECTOR avgMV;
432 :    
433 :     for (mj = 0; mj < (unsigned int)mb_height; mj++)
434 :     for (mi = 0; mi < (unsigned int)mb_width; mi++) {
435 :     const int mbnum = mj*mb_width+mi;
436 :     if (pGMC)
437 :     {
438 :     gmc_data->predict_16x16(gmc_data,
439 :     pGMC->y + mj*16*stride + mi*16, pRef->y,
440 :     stride, stride, mi, mj, rounding);
441 :    
442 :     gmc_data->predict_8x8(gmc_data,
443 :     pGMC->u + mj*8*stride2 + mi*8, pRef->u,
444 :     pGMC->v + mj*8*stride2 + mi*8, pRef->v,
445 :     stride2, stride2, mi, mj, rounding);
446 :     }
447 :     gmc_data->get_average_mv(gmc_data, &avgMV, mi, mj, quarterpel);
448 :    
449 :     pMBs[mbnum].amv.x = gmc_sanitize(avgMV.x, quarterpel, fcode);
450 :     pMBs[mbnum].amv.y = gmc_sanitize(avgMV.y, quarterpel, fcode);
451 :    
452 :     pMBs[mbnum].mcsel = 0; /* until mode decision */
453 :     }
454 :     }

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