[svn] / trunk / xvidcore / src / motion / gmc.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/src/motion/gmc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1708 - (view) (download)

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

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