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

Diff of /xvidcore/src/motion/gmc.c

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

revision 1.1, Sat Jun 28 15:54:10 2003 UTC revision 1.1.2.5, Tue Sep 30 18:20:31 2003 UTC
# Line 0  Line 1 
1    /*****************************************************************************
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     * $Id$
23     *
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 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    
80                            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    
89                            dst[i] = (uint8_t)f0;
90                    }
91                    dst += dststride;
92            }
93    }
94    
95    void Predict_8x8_C(const NEW_GMC_DATA * const This,
96                                             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    {
100            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    
105            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    
110            int32_t Uo = This->Uco + 8*(dUy*y + dUx*x);
111            int32_t Vo = This->Vco + 8*(dVy*y + dVx*x);
112    
113            int i, j;
114    
115            uDst += 8;
116            vDst += 8;
117            for (j=8; j>0; --j) {
118                    int32_t U = Uo, V = Vo;
119                    Uo += dUy; Vo += dVy;
120    
121                    for (i=-8; i<0; ++i) {
122                            int Offset;
123                            uint32_t f0, f1, ri, rj;
124                            int32_t u, v;
125    
126                            u = ( U >> 16 ) << rho;
127                            v = ( V >> 16 ) << rho;
128                            U += dUx; V += dVx;
129    
130                            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    
148                            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    
157                            uDst[i] = (uint8_t)f0;
158    
159                            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    
168                            vDst[i] = (uint8_t)f0;
169                    }
170                    uDst += dststride;
171                    vDst += dststride;
172            }
173    }
174    
175    void get_average_mv_C(const NEW_GMC_DATA * const Dsp, VECTOR * const mv,
176                                                    int x, int y, int qpel)
177    {
178            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            vx -= (256*x+120) << (5+Dsp->accuracy); /* 120 = 15*16/2 */
195            vy -= (256*y+120) << (5+Dsp->accuracy);
196    
197            mv->x = RSHIFT( vx, 8+Dsp->accuracy - qpel );
198            mv->y = RSHIFT( vy, 8+Dsp->accuracy - qpel );
199    }
200    
201    /* ************************************************************
202     * simplified version for 1 warp point
203     */
204    
205    void Predict_1pt_16x16_C(const NEW_GMC_DATA * const This,
206                                                     uint8_t *Dst, const uint8_t *Src,
207                                                     int dststride, int srcstride, int x, int y, int rounding)
208    {
209            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    
214    
215            int32_t uo = This->Uo + (x<<8);  /* ((16*x)<<4) */
216            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    
221            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    
229            Dst += 16;
230    
231            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    
250    void Predict_1pt_8x8_C(const NEW_GMC_DATA * const This,
251                                                     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    {
255            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    
260            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    
266            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    
274            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    
291                    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    }
305    
306    void get_average_mv_1pt_C(const NEW_GMC_DATA * const Dsp, VECTOR * const mv,
307                                                            int x, int y, int qpel)
308    {
309            mv->x = RSHIFT(Dsp->Uo<<qpel, 3);
310            mv->y = RSHIFT(Dsp->Vo<<qpel, 3);
311    }
312    
313    /* *************************************************************
314     * Warning! It's Accuracy being passed, not 'resolution'!
315     */
316    
317    void generate_GMCparameters( int nb_pts, const int accuracy,
318                                                                     const WARPPOINTS *const pts,
319                                                                     const int width, const int height,
320                                                                     NEW_GMC_DATA *const gmc)
321    {
322            gmc->sW = width << 4;
323            gmc->sH = height << 4;
324            gmc->accuracy = accuracy;
325            gmc->num_wp = nb_pts;
326    
327            /* reduce the number of points, if possible */
328            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            /* now, nb_pts stores the actual number of points required for interpolation */
340    
341            if (nb_pts<=1)
342            {
343            if (nb_pts==1) {
344                    /* store as 4b fixed point */
345                    gmc->Uo = pts->duv[0].x << accuracy;
346                    gmc->Vo = pts->duv[0].y << accuracy;
347                    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            }
350            else {  /* zero points?! */
351                    gmc->Uo = gmc->Vo       = 0;
352                    gmc->Uco = gmc->Vco = 0;
353            }
354    
355            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            else {          /* 2 or 3 points */
360            const int rho    = 3 - accuracy;        /* = {3,2,1,0} for Acc={0,1,2,3} */
361            int Alpha = log2bin(width-1);
362            int Ws = 1 << Alpha;
363    
364            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    
367    /*       disabled, because possibly buggy? */
368    
369    #if 0
370            if (nb_pts==2) {
371                    gmc->dU[1] = -gmc->dV[0];       /* -Sin */
372                    gmc->dV[1] =    gmc->dU[0] ;    /* Cos */
373            }
374            else
375    #endif
376            {
377                    const int Beta = log2bin(height-1);
378                    const int Hs = 1<<Beta;
379                    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                    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            /* upscale to 16b fixed-point */
393            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    
398            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    
405            gmc->predict_16x16      = Predict_16x16_C;
406            gmc->predict_8x8        = Predict_8x8_C;
407            gmc->get_average_mv = get_average_mv_C;
408            }
409    }
410    
411    /* *******************************************************************
412     * quick and dirty routine to generate the full warped image
413     * (pGMC != NULL) or just all average Motion Vectors (pGMC == NULL) */
414    
415    void
416    generate_GMCimage(      const NEW_GMC_DATA *const gmc_data, /* [input] precalculated data */
417                                            const IMAGE *const pRef,                /* [input] */
418                                            const int mb_width,
419                                            const int mb_height,
420                                            const int stride,
421                                            const int stride2,
422                                            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    {
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    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.5

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