[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.2.1, Sat Jun 28 15:54:10 2003 UTC revision 1.2.2.1, Wed Mar 31 19:35:31 2004 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *      XVID MPEG-4 VIDEO CODEC   *      XVID MPEG-4 VIDEO CODEC
4   *      GMC interpolation module   *  - 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   *      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   *      it under the terms of the GNU General Public License as published by
# Line 15  Line 17 
17   *   *
18   *      You should have received a copy of the GNU General Public License   *      You should have received a copy of the GNU General Public License
19   *      along with this program; if not, write to the Free Software   *      along with this program; if not, write to the Free Software
20   *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21     *
22     * $Id$
23   *   *
24   *************************************************************************/   ****************************************************************************/
25    
26  #include "../portab.h"  #include "../portab.h"
27  #include "../global.h"  #include "../global.h"
28  #include "../encoder.h"  #include "../encoder.h"
29  #include "gmc.h"  #include "gmc.h"
 #include "motion_est.h"  
30    
31  #include <stdio.h>  #include <stdio.h>
32    
33  /* These are mainly the new GMC routines by -Skal- (C) 2003 */  /* ************************************************************
34     * Pts = 2 or 3
35  //////////////////////////////////////////////////////////   *
36  // Pts = 2 or 3   * Warning! *src is the global frame pointer (that is: adress
37     * of pixel 0,0), not the macroblock one.
38  // Warning! *src is the global frame pointer (that is: adress   * Conversely, *dst is the macroblock top-left adress.
39  // of pixel 0,0), not the macroblock one.   */
 // Conversely, *dst is the macroblock top-left adress.  
   
40    
41  void Predict_16x16_C(const NEW_GMC_DATA * const This,  void Predict_16x16_C(const NEW_GMC_DATA * const This,
42                       uint8_t *dst, const uint8_t *src,                       uint8_t *dst, const uint8_t *src,
# Line 57  Line 58 
58    int i, j;    int i, j;
59    
60    dst += 16;    dst += 16;
61    for (j=16; j>0; --j)          for (j=16; j>0; --j) {
   {  
62      int U = Uo, V = Vo;      int U = Uo, V = Vo;
63      Uo += dUy; Vo += dVy;      Uo += dUy; Vo += dVy;
64      for (i=-16; i<0; ++i)                  for (i=-16; i<0; ++i) {
65      {                          unsigned int f0, f1, ri = 16, rj = 16;
       unsigned int f0, f1, ri, rj;  
66        int Offset;        int Offset;
   
67        int u = ( U >> 16 ) << rho;        int u = ( U >> 16 ) << rho;
68        int v = ( V >> 16 ) << rho;        int v = ( V >> 16 ) << rho;
69    
70        U += dUx; V += dVx;        U += dUx; V += dVx;
71    
72        ri = 16;                          if (u > 0 && u <= W) { ri = MTab[u&15]; Offset = u>>4;  }
73        if ((uint32_t)u<=(uint32_t)W) { ri = MTab[u&15]; Offset = u>>4;  }                          else {
74        else if (u>W) Offset = W>>4;                                  if (u > W) Offset = W>>4;
75        else Offset = -1;                                  else Offset = 0;
76                                    ri = MTab[0];
77        rj = 16;                          }
78        if ((uint32_t)v<=(uint32_t)H) { rj = MTab[v&15]; Offset += (v>>4)*srcstride; }  
79        else if (v>H) Offset += (H>>4)*srcstride;                          if (v > 0 && v <= H) { rj = MTab[v&15]; Offset += (v>>4)*srcstride; }
80            else Offset -= srcstride;                          else {
81                                    if (v > H) Offset += (H>>4)*srcstride;
82                                    rj = MTab[0];
83                            }
84    
85        f0  = src[ Offset     +0 ];        f0  = src[ Offset     +0 ];
86        f0 |= src[ Offset     +1 ] << 16;        f0 |= src[ Offset     +1 ] << 16;
# Line 95  Line 97 
97    }    }
98  }  }
99    
   
100  void Predict_8x8_C(const NEW_GMC_DATA * const This,  void Predict_8x8_C(const NEW_GMC_DATA * const This,
101                     uint8_t *uDst, const uint8_t *uSrc,                     uint8_t *uDst, const uint8_t *uSrc,
102                     uint8_t *vDst, const uint8_t *vSrc,                     uint8_t *vDst, const uint8_t *vSrc,
# Line 118  Line 119 
119    
120    uDst += 8;    uDst += 8;
121    vDst += 8;    vDst += 8;
122    for (j=8; j>0; --j)          for (j=8; j>0; --j) {
   {  
123      int32_t U = Uo, V = Vo;      int32_t U = Uo, V = Vo;
124      Uo += dUy; Vo += dVy;      Uo += dUy; Vo += dVy;
125    
126      for (i=-8; i<0; ++i)                  for (i=-8; i<0; ++i) {
     {  
127        int Offset;        int Offset;
128        uint32_t f0, f1, ri, rj;        uint32_t f0, f1, ri, rj;
129        int32_t u, v;        int32_t u, v;
# Line 133  Line 132 
132        v = ( V >> 16 ) << rho;        v = ( V >> 16 ) << rho;
133        U += dUx; V += dVx;        U += dUx; V += dVx;
134    
135        if ((uint32_t)u<=(uint32_t)W) {                          if (u > 0 && u <= W) {
136          ri = MTab[u&15];          ri = MTab[u&15];
137          Offset = u>>4;          Offset = u>>4;
138        }                          } else {
       else {  
         ri = 16;  
139          if (u>W) Offset = W>>4;          if (u>W) Offset = W>>4;
140          else Offset = -1;                                  else Offset = 0;
141                                    ri = MTab[0];
142        }        }
143        if ((uint32_t)v<=(uint32_t)H) {  
144                            if (v > 0 && v <= H) {
145          rj = MTab[v&15];          rj = MTab[v&15];
146          Offset += (v>>4)*srcstride;          Offset += (v>>4)*srcstride;
147        }                          } else {
       else {  
         rj = 16;  
148          if (v>H) Offset += (H>>4)*srcstride;          if (v>H) Offset += (H>>4)*srcstride;
149                  else Offset -= srcstride;                                  rj = MTab[0];
150        }        }
151    
152        f0  = uSrc[ Offset        +0 ];        f0  = uSrc[ Offset        +0 ];
# Line 179  Line 176 
176    }    }
177  }  }
178    
179    void get_average_mv_C(const NEW_GMC_DATA * const Dsp, VECTOR * const mv,
 void get_average_mv_C(NEW_GMC_DATA *Dsp, VECTOR * const mv,  
180                        int x, int y, int qpel)                        int x, int y, int qpel)
181  {  {
182    int i, j;    int i, j;
# Line 199  Line 195 
195        v = V >> 16; V += Dsp->dV[0]; vy += v;        v = V >> 16; V += Dsp->dV[0]; vy += v;
196      }      }
197    }    }
198    vx -= (256*x+120) << (5+Dsp->accuracy);  // 120 = 15*16/2          vx -= (256*x+120) << (5+Dsp->accuracy); /* 120 = 15*16/2 */
199    vy -= (256*y+120) << (5+Dsp->accuracy);    vy -= (256*y+120) << (5+Dsp->accuracy);
200    
201    mv->x = RSHIFT( vx, 8+Dsp->accuracy - qpel );    mv->x = RSHIFT( vx, 8+Dsp->accuracy - qpel );
202    mv->y = RSHIFT( vy, 8+Dsp->accuracy - qpel );    mv->y = RSHIFT( vy, 8+Dsp->accuracy - qpel );
203  }  }
204    
205  //////////////////////////////////////////////////////////  /* ************************************************************
206  // simplified version for 1 warp point   * simplified version for 1 warp point
207     */
208    
209  void Predict_1pt_16x16_C(const NEW_GMC_DATA * const This,  void Predict_1pt_16x16_C(const NEW_GMC_DATA * const This,
210                           uint8_t *Dst, const uint8_t *Src,                           uint8_t *Dst, const uint8_t *Src,
# Line 220  Line 216 
216    const int32_t Rounder = ( 128 - (rounding<<(2*rho)) ) << 16;    const int32_t Rounder = ( 128 - (rounding<<(2*rho)) ) << 16;
217    
218    
219    int32_t uo = This->Uo + (x<<8);     // ((16*x)<<4)          int32_t uo = This->Uo + (x<<8);  /* ((16*x)<<4) */
220    int32_t vo = This->Vo + (y<<8);    int32_t vo = This->Vo + (y<<8);
221    const uint32_t ri = MTab[uo & 15];          uint32_t ri = MTab[uo & 15];
222    const uint32_t rj = MTab[vo & 15];          uint32_t rj = MTab[vo & 15];
223    int i, j;    int i, j;
224    
225    int32_t Offset;    int32_t Offset;
226    if ((uint32_t)vo<=(uint32_t)H) Offset  = (vo>>4)*srcstride;          if (vo>=(-16*4) && vo<=H) Offset = (vo>>4)*srcstride;
227    else if (vo>H)                 Offset  = ( H>>4)*srcstride;          else {
228                    if (vo>H) Offset = ( H>>4)*srcstride;
229    else                           Offset  =-16*srcstride;    else                           Offset  =-16*srcstride;
230    if ((uint32_t)uo<=(uint32_t)W) Offset += (uo>>4);                  rj = MTab[0];
231    else if (uo>W)                 Offset += ( W>>4);          }
232            if (uo>=(-16*4) && uo<=W) Offset += (uo>>4);
233            else {
234                    if (uo>W) Offset += (W>>4);
235    else                           Offset -= 16;    else                           Offset -= 16;
236                    ri = MTab[0];
237            }
238    
239    Dst += 16;    Dst += 16;
240    
# Line 255  Line 257 
257    }    }
258  }  }
259    
   
260  void Predict_1pt_8x8_C(const NEW_GMC_DATA * const This,  void Predict_1pt_8x8_C(const NEW_GMC_DATA * const This,
261                         uint8_t *uDst, const uint8_t *uSrc,                         uint8_t *uDst, const uint8_t *uSrc,
262                         uint8_t *vDst, const uint8_t *vSrc,                         uint8_t *vDst, const uint8_t *vSrc,
# Line 268  Line 269 
269    
270    int32_t uo = This->Uco + (x<<7);    int32_t uo = This->Uco + (x<<7);
271    int32_t vo = This->Vco + (y<<7);    int32_t vo = This->Vco + (y<<7);
272    const uint32_t rri = MTab[uo & 15];          uint32_t rri = MTab[uo & 15];
273    const uint32_t rrj = MTab[vo & 15];          uint32_t rrj = MTab[vo & 15];
274    int i, j;    int i, j;
275    
276    int32_t Offset;    int32_t Offset;
277    if ((uint32_t)vo<=(uint32_t)H) Offset  = (vo>>4)*srcstride;          if (vo>=(-8*4) && vo<=H) Offset = (vo>>4)*srcstride;
278    else if (vo>H)                 Offset  = ( H>>4)*srcstride;          else {
279                    if (vo>H) Offset = ( H>>4)*srcstride;
280    else                           Offset  =-8*srcstride;    else                           Offset  =-8*srcstride;
281    if ((uint32_t)uo<=(uint32_t)W) Offset += (uo>>4);                  rrj = MTab[0];
282    else if (uo>W)                 Offset += ( W>>4);          }
283            if (uo>=(-8*4) && uo<=W) Offset += (uo>>4);
284            else {
285                    if (uo>W) Offset += ( W>>4);
286    else                           Offset -= 8;    else                           Offset -= 8;
287                    rri = MTab[0];
288            }
289    
290    uDst += 8;    uDst += 8;
291    vDst += 8;    vDst += 8;
# Line 312  Line 319 
319    }    }
320  }  }
321    
322    void get_average_mv_1pt_C(const NEW_GMC_DATA * const Dsp, VECTOR * const mv,
 void get_average_mv_1pt_C(NEW_GMC_DATA *Dsp, VECTOR * const mv,  
323                            int x, int y, int qpel)                            int x, int y, int qpel)
324  {  {
325    mv->x = RSHIFT(Dsp->Uo<<qpel, 3);    mv->x = RSHIFT(Dsp->Uo<<qpel, 3);
326    mv->y = RSHIFT(Dsp->Vo<<qpel, 3);    mv->y = RSHIFT(Dsp->Vo<<qpel, 3);
327  }  }
328    
329  //////////////////////////////////////////////////////////  /* *************************************************************
330     * Warning! It's Accuracy being passed, not 'resolution'!
331     */
   // Warning! It's Accuracy being passed, not 'resolution'!  
332    
333  void generate_GMCparameters( int nb_pts, const int accuracy,  void generate_GMCparameters( int nb_pts, const int accuracy,
334                                   const WARPPOINTS *const pts,                                   const WARPPOINTS *const pts,
# Line 335  Line 340 
340    gmc->accuracy  = accuracy;    gmc->accuracy  = accuracy;
341    gmc->num_wp = nb_pts;    gmc->num_wp = nb_pts;
342    
343      // reduce the number of points, if possible          /* reduce the number of points, if possible */
344    if (nb_pts<3 || (pts->duv[2].x==-pts->duv[1].y && pts->duv[2].y==pts->duv[1].x)) {          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)) {      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)) {        if (nb_pts<1 || (pts->duv[0].x==0 && pts->duv[0].y==0)) {
347          nb_pts = 0;          nb_pts = 0;
# Line 345  Line 350 
350      }      }
351      else nb_pts = 2;      else nb_pts = 2;
352    }    }
   else nb_pts = 3;  
353    
354    // now, nb_pts stores the actual number of points required for interpolation          /* now, nb_pts stores the actual number of points required for interpolation */
355    
356    if (nb_pts<=1)    if (nb_pts<=1)
357    {    {
358      if (nb_pts==1) {      if (nb_pts==1) {
359          // store as 4b fixed point                  /* store as 4b fixed point */
360        gmc->Uo = pts->duv[0].x << accuracy;        gmc->Uo = pts->duv[0].x << accuracy;
361        gmc->Vo = pts->duv[0].y << accuracy;        gmc->Vo = pts->duv[0].y << accuracy;
362        gmc->Uco = ((pts->duv[0].x>>1) | (pts->duv[0].x&1)) << accuracy;     // DIV2RND()                  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()                  gmc->Vco = ((pts->duv[0].y>>1) | (pts->duv[0].y&1)) << accuracy;         /* DIV2RND() */
364      }      }
365      else {    // zero points?!          else {  /* zero points?! */
366        gmc->Uo  = gmc->Vo  = 0;        gmc->Uo  = gmc->Vo  = 0;
367        gmc->Uco = gmc->Vco = 0;        gmc->Uco = gmc->Vco = 0;
368      }      }
# Line 367  Line 371 
371      gmc->predict_8x8    = Predict_1pt_8x8_C;      gmc->predict_8x8    = Predict_1pt_8x8_C;
372      gmc->get_average_mv = get_average_mv_1pt_C;      gmc->get_average_mv = get_average_mv_1pt_C;
373    }    }
374    else {      // 2 or 3 points          else {          /* 2 or 3 points */
375      const int rho   = 3 - accuracy;  // = {3,2,1,0} for Acc={0,1,2,3}          const int rho    = 3 - accuracy;        /* = {3,2,1,0} for Acc={0,1,2,3} */
376      int Alpha = log2bin(width-1);      int Alpha = log2bin(width-1);
377      int Ws = 1 << Alpha;      int Ws = 1 << Alpha;
378    
379      gmc->dU[0] = 16*Ws + RDIV( 8*Ws*pts->duv[1].x, width );   // dU/dx          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          gmc->dV[0] =             RDIV( 8*Ws*pts->duv[1].y, width );      /* dV/dx */
   
 /*   disabled, because possibly buggy? */  
381    
382  /* if (nb_pts==2) {          if (nb_pts==2) {
383        gmc->dU[1] = -gmc->dV[0];  // -Sin                  gmc->dU[1] = -gmc->dV[0];       /* -Sin */
384        gmc->dV[1] =  gmc->dU[0] ;  //  Cos                  gmc->dV[1] =    gmc->dU[0] ;    /* Cos */
385      }      }
386      else */          else
387          {          {
388        const int Beta = log2bin(height-1);        const int Beta = log2bin(height-1);
389        const int Hs = 1<<Beta;        const int Hs = 1<<Beta;
390        gmc->dU[1] =         RDIV( 8*Hs*pts->duv[2].x, height );   // dU/dy                  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                  gmc->dV[1] = 16*Hs + RDIV( 8*Hs*pts->duv[2].y, height );         /* dV/dy */
392        if (Beta>Alpha) {        if (Beta>Alpha) {
393          gmc->dU[0] <<= (Beta-Alpha);          gmc->dU[0] <<= (Beta-Alpha);
394          gmc->dV[0] <<= (Beta-Alpha);          gmc->dV[0] <<= (Beta-Alpha);
# Line 398  Line 400 
400          gmc->dV[1] <<= Alpha - Beta;          gmc->dV[1] <<= Alpha - Beta;
401        }        }
402      }      }
403        // upscale to 16b fixed-point          /* upscale to 16b fixed-point */
404      gmc->dU[0] <<= (16-Alpha - rho);      gmc->dU[0] <<= (16-Alpha - rho);
405      gmc->dU[1] <<= (16-Alpha - rho);      gmc->dU[1] <<= (16-Alpha - rho);
406      gmc->dV[0] <<= (16-Alpha - rho);      gmc->dV[0] <<= (16-Alpha - rho);
# Line 417  Line 419 
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) */
 /* quick and dirty routine to generate the full warped image (pGMC != NULL)  
         or just all average Motion Vectors (pGMC == NULL) */  
425    
426  void  void
427  generate_GMCimage(      const NEW_GMC_DATA *const gmc_data, // [input] precalculated data  generate_GMCimage(      const NEW_GMC_DATA *const gmc_data, /* [input] precalculated data */
428                                          const IMAGE *const pRef,                // [input]                                          const IMAGE *const pRef,                /* [input] */
429                                          const int mb_width,                                          const int mb_width,
430                                          const int mb_height,                                          const int mb_height,
431                                          const int stride,                                          const int stride,
432                                          const int stride2,                                          const int stride2,
433                                          const int fcode,                                // [input] some parameters...                                          const int fcode,                                /* [input] some parameters... */
434                                          const int32_t quarterpel,               // [input] for rounding avgMV                                                  const int32_t quarterpel,               /* [input] for rounding avgMV */
435                                          const int reduced_resolution,   // [input] ignored                                          const int reduced_resolution,   /* [input] ignored */
436                                          const int32_t rounding,                 // [input] for rounding image data                                          const int32_t rounding,                 /* [input] for rounding image data */
437                                          MACROBLOCK *const pMBs,                 // [output] average motion vectors                                          MACROBLOCK *const pMBs,                 /* [output] average motion vectors */
438                                          IMAGE *const pGMC)                              // [output] full warped image                                          IMAGE *const pGMC)                              /* [output] full warped image */
439  {  {
440    
441          unsigned int mj,mi;          unsigned int mj,mi;

Legend:
Removed from v.1.1.2.1  
changed lines
  Added in v.1.2.2.1

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