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

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

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