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

Diff of /branches/dev-api-4/xvidcore/src/motion/motion_comp.c

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

trunk/xvidcore/src/motion/motion_comp.c revision 437, Sat Sep 7 09:12:22 2002 UTC branches/dev-api-4/xvidcore/src/motion/motion_comp.c revision 1053, Mon Jun 9 01:25:19 2003 UTC
# Line 1  Line 1 
1  /*****************************************************************************  /*
2   *   * 30.10.2002   corrected qpel chroma rounding
3   *  XVID MPEG-4 VIDEO CODEC   * 04.10.2002   added qpel support to MBMotionCompensation
4   *  - Motion Compensation module -   * 01.05.2002   updated MBMotionCompensationBVOP
5   *   * 14.04.2002   bframe compensation
6   *  Copyright(C) 2002 Michael Militzer <michael@xvid.org>   */
7   *  Copyright(C) 2002 Edouard Gomez <ed.gomez@wanadoo.fr>  
8   *  Copyright(C) 2002 Christoph Lampert <gruel@web.de>  #include <stdio.h>
  *  
  *  This program is an implementation of a part of one or more MPEG-4  
  *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending  
  *  to use this software module in hardware or software products are  
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
  *  
  *  This program is free software; you can redistribute it and/or modify  
  *  it under the terms of the GNU General Public License as published by  
  *  the Free Software Foundation; either version 2 of the License, or  
  *  (at your option) any later version.  
  *  
  *  This program is distributed in the hope that it will be useful,  
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
  *  GNU General Public License for more details.  
  *  
  *  You should have received a copy of the GNU General Public License  
  *  along with this program; if not, write to the Free Software  
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  
  *  
  *************************************************************************/  
9    
10  #include "../encoder.h"  #include "../encoder.h"
11  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
12  #include "../image/interpolate8x8.h"  #include "../image/interpolate8x8.h"
13    #include "../image/reduced.h"
14  #include "../utils/timer.h"  #include "../utils/timer.h"
15  #include "motion.h"  #include "motion.h"
16    
17  #define ABS(X) (((X)>0)?(X):-(X))  #ifndef RSHIFT
18  #define SIGN(X) (((X)>0)?1:-1)  #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
19    #endif
20    
21    /* assume b>0 */
22    #ifndef RDIV
23    #define RDIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
24    #endif
25    
26    
27    /* This is borrowed from        decoder.c   */
28    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
29    {
30            int length = 1 << (fcode+4);
31    
32    #if 0
33            if (quarterpel) value *= 2;
34    #endif
35    
36            if (value < -length)
37                    return -length;
38            else if (value >= length)
39                    return length-1;
40            else return value;
41    }
42    
43    /* And this is borrowed from   bitstream.c  until we find a common solution */
44    
45    static uint32_t __inline
46    log2bin(uint32_t value)
47    {
48    /* Changed by Chenm001 */
49    #if !defined(_MSC_VER)
50            int n = 0;
51    
52            while (value) {
53                    value >>= 1;
54                    n++;
55            }
56            return n;
57    #else
58            __asm {
59                    bsr eax, value
60                    inc eax
61            }
62    #endif
63    }
64    
65    
66  static __inline void  static __inline void
67  compensate8x8_halfpel(int16_t * const dct_codes,  compensate16x16_interpolate(int16_t * const dct_codes,
68                                            uint8_t * const cur,                                            uint8_t * const cur,
69                                            const uint8_t * const ref,                                            const uint8_t * const ref,
70                                            const uint8_t * const refh,                                            const uint8_t * const refh,
71                                            const uint8_t * const refv,                                            const uint8_t * const refv,
72                                            const uint8_t * const refhv,                                            const uint8_t * const refhv,
73                                            const uint32_t x,                                                          uint8_t * const tmp,
74                                            const uint32_t y,                                                          uint32_t x,
75                                                            uint32_t y,
76                                            const int32_t dx,                                            const int32_t dx,
77                                            const int dy,                                                          const int32_t dy,
78                                            const uint32_t stride)                                                          const int32_t stride,
79                                                            const int quarterpel,
80                                                            const int reduced_resolution,
81                                                            const int32_t rounding)
82  {  {
83          int32_t ddx, ddy;          const uint8_t * ptr;
84    
85          switch (((dx & 1) << 1) + (dy & 1))     // ((dx%2)?2:0)+((dy%2)?1:0)          if (!reduced_resolution) {
         {  
         case 0:  
                 ddx = dx / 2;  
                 ddy = dy / 2;  
                 transfer_8to16sub(dct_codes, cur + y * stride + x,  
                                                   ref + (int) ((y + ddy) * stride + x + ddx), stride);  
                 break;  
86    
87          case 1:                  if(quarterpel) {
88                  ddx = dx / 2;                          if ((dx&3) | (dy&3)) {
89                  ddy = (dy - 1) / 2;                                  interpolate16x16_quarterpel(tmp - y * stride - x,
90                  transfer_8to16sub(dct_codes, cur + y * stride + x,                                                                                          (uint8_t *) ref, tmp + 32,
91                                                    refv + (int) ((y + ddy) * stride + x + ddx), stride);                                                                                          tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding);
92                  break;                                  ptr = tmp;
93                            } else ptr =  ref + (y + dy/4)*stride + x + dx/4; /* fullpixel position */
94    
95          case 2:                  } else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride);
                 ddx = (dx - 1) / 2;  
                 ddy = dy / 2;  
                 transfer_8to16sub(dct_codes, cur + y * stride + x,  
                                                   refh + (int) ((y + ddy) * stride + x + ddx), stride);  
                 break;  
96    
         default:                                        // case 3:  
                 ddx = (dx - 1) / 2;  
                 ddy = (dy - 1) / 2;  
97                  transfer_8to16sub(dct_codes, cur + y * stride + x,                  transfer_8to16sub(dct_codes, cur + y * stride + x,
98                                                    refhv + (int) ((y + ddy) * stride + x + ddx), stride);                                                          ptr, stride);
99                  break;                  transfer_8to16sub(dct_codes+64, cur + y * stride + x + 8,
100                                                            ptr + 8, stride);
101                    transfer_8to16sub(dct_codes+128, cur + y * stride + x + 8*stride,
102                                                            ptr + 8*stride, stride);
103                    transfer_8to16sub(dct_codes+192, cur + y * stride + x + 8*stride+8,
104                                                            ptr + 8*stride + 8, stride);
105    
106            } else { /* reduced_resolution */
107    
108                    x *= 2; y *= 2;
109    
110                    ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride);
111    
112                    filter_18x18_to_8x8(dct_codes, cur+y*stride + x, stride);
113                    filter_diff_18x18_to_8x8(dct_codes, ptr, stride);
114    
115                    filter_18x18_to_8x8(dct_codes+64, cur+y*stride + x + 16, stride);
116                    filter_diff_18x18_to_8x8(dct_codes+64, ptr + 16, stride);
117    
118                    filter_18x18_to_8x8(dct_codes+128, cur+(y+16)*stride + x, stride);
119                    filter_diff_18x18_to_8x8(dct_codes+128, ptr + 16*stride, stride);
120    
121                    filter_18x18_to_8x8(dct_codes+192, cur+(y+16)*stride + x + 16, stride);
122                    filter_diff_18x18_to_8x8(dct_codes+192, ptr + 16*stride + 16, stride);
123    
124                    transfer32x32_copy(cur + y*stride + x, ptr, stride);
125          }          }
126  }  }
127    
128    static __inline void
129    compensate8x8_interpolate(      int16_t * const dct_codes,
130                                                            uint8_t * const cur,
131                                                            const uint8_t * const ref,
132                                                            const uint8_t * const refh,
133                                                            const uint8_t * const refv,
134                                                            const uint8_t * const refhv,
135                                                            uint8_t * const tmp,
136                                                            uint32_t x,
137                                                            uint32_t y,
138                                                            const int32_t dx,
139                                                            const int32_t dy,
140                                                            const int32_t stride,
141                                                            const int32_t quarterpel,
142                                                            const int reduced_resolution,
143                                                            const int32_t rounding)
144    {
145            const uint8_t * ptr;
146    
147            if (!reduced_resolution) {
148    
149                    if(quarterpel) {
150                            if ((dx&3) | (dy&3)) {
151                                    interpolate8x8_quarterpel(tmp - y*stride - x,
152                                                                                    (uint8_t *) ref, tmp + 32,
153                                                                                    tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding);
154                                    ptr = tmp;
155                            } else ptr = ref + (y + dy/4)*stride + x + dx/4; /* fullpixel position */
156                    } else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride);
157    
158                            transfer_8to16sub(dct_codes, cur + y * stride + x, ptr, stride);
159    
160            } else { /* reduced_resolution */
161    
162                    x *= 2; y *= 2;
163    
164                    ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride);
165    
166                    filter_18x18_to_8x8(dct_codes, cur+y*stride + x, stride);
167                    filter_diff_18x18_to_8x8(dct_codes, ptr, stride);
168    
169                    transfer16x16_copy(cur + y*stride + x, ptr, stride);
170            }
171    }
172    
173    /* XXX: slow, inelegant... */
174    static void
175    interpolate18x18_switch(uint8_t * const cur,
176                                                    const uint8_t * const refn,
177                                                    const uint32_t x,
178                                                    const uint32_t y,
179                                                    const int32_t dx,
180                                                    const int dy,
181                                                    const int32_t stride,
182                                                    const int32_t rounding)
183    {
184            interpolate8x8_switch(cur, refn, x-1, y-1, dx, dy, stride, rounding);
185            interpolate8x8_switch(cur, refn, x+7, y-1, dx, dy, stride, rounding);
186            interpolate8x8_switch(cur, refn, x+9, y-1, dx, dy, stride, rounding);
187    
188            interpolate8x8_switch(cur, refn, x-1, y+7, dx, dy, stride, rounding);
189            interpolate8x8_switch(cur, refn, x+7, y+7, dx, dy, stride, rounding);
190            interpolate8x8_switch(cur, refn, x+9, y+7, dx, dy, stride, rounding);
191    
192            interpolate8x8_switch(cur, refn, x-1, y+9, dx, dy, stride, rounding);
193            interpolate8x8_switch(cur, refn, x+7, y+9, dx, dy, stride, rounding);
194            interpolate8x8_switch(cur, refn, x+9, y+9, dx, dy, stride, rounding);
195    }
196    
197    static void
198    CompensateChroma(       int dx, int dy,
199                                            const int i, const int j,
200                                            IMAGE * const Cur,
201                                            const IMAGE * const Ref,
202                                            uint8_t * const temp,
203                                            int16_t * const coeff,
204                                            const int32_t stride,
205                                            const int rounding,
206                                            const int rrv)
207    { /* uv-block-based compensation */
208    
209            if (!rrv) {
210                    transfer_8to16sub(coeff, Cur->u + 8 * j * stride + 8 * i,
211                                                            interpolate8x8_switch2(temp, Ref->u, 8 * i, 8 * j,
212                                                                                                            dx, dy, stride, rounding),
213                                                            stride);
214                    transfer_8to16sub(coeff + 64, Cur->v + 8 * j * stride + 8 * i,
215                                                            interpolate8x8_switch2(temp, Ref->v, 8 * i, 8 * j,
216                                                                                                            dx, dy, stride, rounding),
217                                                            stride);
218            } else {
219                    uint8_t * current, * reference;
220    
221                    current = Cur->u + 16*j*stride + 16*i;
222                    reference = temp - 16*j*stride - 16*i;
223                    interpolate18x18_switch(reference, Ref->u, 16*i, 16*j, dx, dy, stride, rounding);
224                    filter_18x18_to_8x8(coeff, current, stride);
225                    filter_diff_18x18_to_8x8(coeff, temp, stride);
226                    transfer16x16_copy(current, temp, stride);
227    
228                    current = Cur->v + 16*j*stride + 16*i;
229                    interpolate18x18_switch(reference, Ref->v, 16*i, 16*j, dx, dy, stride, rounding);
230                    filter_18x18_to_8x8(coeff + 64, current, stride);
231                    filter_diff_18x18_to_8x8(coeff + 64, temp, stride);
232                    transfer16x16_copy(current, temp, stride);
233            }
234    }
235    
236  void  void
237  MBMotionCompensation(MACROBLOCK * const mb,  MBMotionCompensation(MACROBLOCK * const mb,
# Line 98  Line 241 
241                                           const IMAGE * const refh,                                           const IMAGE * const refh,
242                                           const IMAGE * const refv,                                           const IMAGE * const refv,
243                                           const IMAGE * const refhv,                                           const IMAGE * const refhv,
244                                            const IMAGE * const refGMC,
245                                           IMAGE * const cur,                                           IMAGE * const cur,
246                                           int16_t * dct_codes,                                           int16_t * dct_codes,
247                                           const uint32_t width,                                           const uint32_t width,
248                                           const uint32_t height,                                           const uint32_t height,
249                                           const uint32_t edged_width,                                           const uint32_t edged_width,
250                                           const uint32_t rounding)                                          const int32_t quarterpel,
251                                            const int reduced_resolution,
252                                            const int32_t rounding)
253  {  {
254          static const uint32_t roundtab[16] =          int32_t dx;
255                  { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };          int32_t dy;
256    
257            uint8_t * const tmp = refv->u;
258    
259          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {          if ( (!reduced_resolution) && (mb->mode == MODE_NOT_CODED) ) {  /* quick copy for early SKIP */
260                  int32_t dx = mb->mvs[0].x;  /* early SKIP is only activated in P-VOPs, not in S-VOPs, so mcsel can never be 1 */
                 int32_t dy = mb->mvs[0].y;  
261    
262                  compensate8x8_halfpel(&dct_codes[0 * 64], cur->y, ref->y, refh->y,                  transfer16x16_copy(cur->y + 16 * (i + j * edged_width),
263                                                            refv->y, refhv->y, 16 * i, 16 * j, dx, dy,                                                    ref->y + 16 * (i + j * edged_width),
                                                           edged_width);  
                 compensate8x8_halfpel(&dct_codes[1 * 64], cur->y, ref->y, refh->y,  
                                                           refv->y, refhv->y, 16 * i + 8, 16 * j, dx, dy,  
264                                                            edged_width);                                                            edged_width);
                 compensate8x8_halfpel(&dct_codes[2 * 64], cur->y, ref->y, refh->y,  
                                                           refv->y, refhv->y, 16 * i, 16 * j + 8, dx, dy,  
                                                           edged_width);  
                 compensate8x8_halfpel(&dct_codes[3 * 64], cur->y, ref->y, refh->y,  
                                                           refv->y, refhv->y, 16 * i + 8, 16 * j + 8, dx,  
                                                           dy, edged_width);  
   
                 dx = (dx & 3) ? (dx >> 1) | 1 : dx / 2;  
                 dy = (dy & 3) ? (dy >> 1) | 1 : dy / 2;  
   
                 /* uv-image-based compensation */  
   
                 interpolate8x8_switch(refv->u, ref->u, 8 * i, 8 * j, dx, dy,  
                                                           edged_width / 2, rounding);  
                 transfer_8to16sub(&dct_codes[4 * 64],  
                                                   cur->u + 8 * j * edged_width / 2 + 8 * i,  
                                                   refv->u + 8 * j * edged_width / 2 + 8 * i,  
                                                   edged_width / 2);  
265    
266                  interpolate8x8_switch(refv->v, ref->v, 8 * i, 8 * j, dx, dy,                  transfer8x8_copy(cur->u + 8 * (i + j * edged_width/2),
267                                                            edged_width / 2, rounding);                                                          ref->u + 8 * (i + j * edged_width/2),
268                  transfer_8to16sub(&dct_codes[5 * 64],                                                          edged_width / 2);
269                                                    cur->v + 8 * j * edged_width / 2 + 8 * i,                  transfer8x8_copy(cur->v + 8 * (i + j * edged_width/2),
270                                                    refv->v + 8 * j * edged_width / 2 + 8 * i,                                                          ref->v + 8 * (i + j * edged_width/2),
271                                                    edged_width / 2);                                                    edged_width / 2);
272                    return;
273            }
274    
275            if ((mb->mode == MODE_NOT_CODED || mb->mode == MODE_INTER
276                                    || mb->mode == MODE_INTER_Q)) {
277    
278            /* reduced resolution + GMC:  not possible */
279    
280                    if (mb->mcsel) {
281    
282                            /* call normal routine once, easier than "if (mcsel)"ing all the time */
283    
284                            transfer_8to16sub(&dct_codes[0*64], cur->y + 16*j*edged_width + 16*i,
285                                                                                            refGMC->y + 16*j*edged_width + 16*i, edged_width);
286                            transfer_8to16sub(&dct_codes[1*64], cur->y + 16*j*edged_width + 16*i+8,
287                                                                                            refGMC->y + 16*j*edged_width + 16*i+8, edged_width);
288                            transfer_8to16sub(&dct_codes[2*64], cur->y + (16*j+8)*edged_width + 16*i,
289                                                                                            refGMC->y + (16*j+8)*edged_width + 16*i, edged_width);
290                            transfer_8to16sub(&dct_codes[3*64], cur->y + (16*j+8)*edged_width + 16*i+8,
291                                                                                            refGMC->y + (16*j+8)*edged_width + 16*i+8, edged_width);
292    
293    /* lumi is needed earlier for mode decision, but chroma should be done block-based, but it isn't, yet. */
294    
295                            transfer_8to16sub(&dct_codes[4 * 64], cur->u + 8 *j*edged_width/2 + 8*i,
296                                                                    refGMC->u + 8 *j*edged_width/2 + 8*i, edged_width/2);
297    
298                            transfer_8to16sub(&dct_codes[5 * 64], cur->v + 8*j* edged_width/2 + 8*i,
299                                                                    refGMC->v + 8*j* edged_width/2 + 8*i, edged_width/2);
300    
301                            return;
302                    }
303    
304                    /* ordinary compensation */
305    
306                    dx = (quarterpel ? mb->qmvs[0].x : mb->mvs[0].x);
307                    dy = (quarterpel ? mb->qmvs[0].y : mb->mvs[0].y);
308    
309                    if (reduced_resolution) {
310                            dx = RRV_MV_SCALEUP(dx);
311                            dy = RRV_MV_SCALEUP(dy);
312                    }
313    
314                    compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, ref->y, refh->y,
315                                                            refv->y, refhv->y, tmp, 16 * i, 16 * j, dx, dy,
316                                                            edged_width, quarterpel, reduced_resolution, rounding);
317    
318                    if (quarterpel) { dx /= 2; dy /= 2; }
319    
320                    dx = (dx >> 1) + roundtab_79[dx & 0x3];
321                    dy = (dy >> 1) + roundtab_79[dy & 0x3];
322    
323            } else {                                        /* mode == MODE_INTER4V */
324                    int k, sumx = 0, sumy = 0;
325                    const VECTOR * const mvs = (quarterpel ? mb->qmvs : mb->mvs);
326    
327                    for (k = 0; k < 4; k++) {
328                            dx = mvs[k].x;
329                            dy = mvs[k].y;
330                            sumx += quarterpel ? dx/2 : dx;
331                            sumy += quarterpel ? dy/2 : dy;
332    
333                            if (reduced_resolution){
334                                    dx = RRV_MV_SCALEUP(dx);
335                                    dy = RRV_MV_SCALEUP(dy);
336                            }
337    
338                            compensate8x8_interpolate(&dct_codes[k * 64], cur->y, ref->y, refh->y,
339                                                                            refv->y, refhv->y, tmp, 16 * i + 8*(k&1), 16 * j + 8*(k>>1), dx,
340                                                                            dy, edged_width, quarterpel, reduced_resolution, rounding);
341                    }
342                    dx = (sumx >> 3) + roundtab_76[sumx & 0xf];
343                    dy = (sumy >> 3) + roundtab_76[sumy & 0xf];
344            }
345    
346            CompensateChroma(dx, dy, i, j, cur, ref, tmp,
347                                            &dct_codes[4 * 64], edged_width / 2, rounding, reduced_resolution);
348    }
349    
350    
351          } else                                          // mode == MODE_INTER4V  void
352    MBMotionCompensationBVOP(MBParam * pParam,
353                                                    MACROBLOCK * const mb,
354                                                    const uint32_t i,
355                                                    const uint32_t j,
356                                                    IMAGE * const cur,
357                                                    const IMAGE * const f_ref,
358                                                    const IMAGE * const f_refh,
359                                                    const IMAGE * const f_refv,
360                                                    const IMAGE * const f_refhv,
361                                                    const IMAGE * const b_ref,
362                                                    const IMAGE * const b_refh,
363                                                    const IMAGE * const b_refv,
364                                                    const IMAGE * const b_refhv,
365                                                    int16_t * dct_codes)
366          {          {
367                  int32_t sum, dx, dy;          const uint32_t edged_width = pParam->edged_width;
368            int32_t dx, dy, b_dx, b_dy, sumx, sumy, b_sumx, b_sumy;
369            int k;
370            const int quarterpel = pParam->vol_flags & XVID_VOL_QUARTERPEL;
371            const uint8_t * ptr1, * ptr2;
372            uint8_t * const tmp = f_refv->u;
373            const VECTOR * const fmvs = (quarterpel ? mb->qmvs : mb->mvs);
374            const VECTOR * const bmvs = (quarterpel ? mb->b_qmvs : mb->b_mvs);
375    
376            switch (mb->mode) {
377            case MODE_FORWARD:
378                    dx = fmvs->x; dy = fmvs->y;
379    
380                    compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, f_ref->y, f_refh->y,
381                                                            f_refv->y, f_refhv->y, tmp, 16 * i, 16 * j, dx,
382                                                            dy, edged_width, quarterpel, 0, 0);
383    
384                    if (quarterpel) { dx /= 2; dy /= 2; }
385    
386                    CompensateChroma(       (dx >> 1) + roundtab_79[dx & 0x3],
387                                                            (dy >> 1) + roundtab_79[dy & 0x3],
388                                                            i, j, cur, f_ref, tmp,
389                                                            &dct_codes[4 * 64], edged_width / 2, 0, 0);
390    
391                    return;
392    
393            case MODE_BACKWARD:
394                    b_dx = bmvs->x; b_dy = bmvs->y;
395    
396                    compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, b_ref->y, b_refh->y,
397                                                                                    b_refv->y, b_refhv->y, tmp, 16 * i, 16 * j, b_dx,
398                                                                                    b_dy, edged_width, quarterpel, 0, 0);
399    
400                    if (quarterpel) { b_dx /= 2; b_dy /= 2; }
401    
402                    CompensateChroma(       (b_dx >> 1) + roundtab_79[b_dx & 0x3],
403                                                            (b_dy >> 1) + roundtab_79[b_dy & 0x3],
404                                                            i, j, cur, b_ref, tmp,
405                                                            &dct_codes[4 * 64], edged_width / 2, 0, 0);
406    
407                    return;
408    
409            case MODE_INTERPOLATE: /* _could_ use DIRECT, but would be overkill (no 4MV there) */
410            case MODE_DIRECT_NO4V:
411                    dx = fmvs->x; dy = fmvs->y;
412                    b_dx = bmvs->x; b_dy = bmvs->y;
413    
414                    if (quarterpel) {
415    
416                            if ((dx&3) | (dy&3)) {
417                                    interpolate16x16_quarterpel(tmp - i * 16 - j * 16 * edged_width,
418                                            (uint8_t *) f_ref->y, tmp + 32,
419                                            tmp + 64, tmp + 96, 16*i, 16*j, dx, dy, edged_width, 0);
420                                    ptr1 = tmp;
421                            } else ptr1 = f_ref->y + (16*j + dy/4)*edged_width + 16*i + dx/4; /* fullpixel position */
422    
423                            if ((b_dx&3) | (b_dy&3)) {
424                                    interpolate16x16_quarterpel(tmp - i * 16 - j * 16 * edged_width + 16,
425                                            (uint8_t *) b_ref->y, tmp + 32,
426                                            tmp + 64, tmp + 96, 16*i, 16*j, b_dx, b_dy, edged_width, 0);
427                                    ptr2 = tmp + 16;
428                            } else ptr2 = b_ref->y + (16*j + b_dy/4)*edged_width + 16*i + b_dx/4; /* fullpixel position */
429    
430                            b_dx /= 2;
431                            b_dy /= 2;
432                            dx /= 2;
433                            dy /= 2;
434    
435                    } else {
436                            ptr1 = get_ref(f_ref->y, f_refh->y, f_refv->y, f_refhv->y,
437                                                            i, j, 16, dx, dy, edged_width);
438    
439                            ptr2 = get_ref(b_ref->y, b_refh->y, b_refv->y, b_refhv->y,
440                                                            i, j, 16, b_dx, b_dy, edged_width);
441                    }
442                    for (k = 0; k < 4; k++)
443                                    transfer_8to16sub2(&dct_codes[k * 64],
444                                                                            cur->y + (i * 16+(k&1)*8) + (j * 16+((k>>1)*8)) * edged_width,
445                                                                            ptr1 + (k&1)*8 + (k>>1)*8*edged_width,
446                                                                            ptr2 + (k&1)*8 + (k>>1)*8*edged_width, edged_width);
447    
448    
449                    dx = (dx >> 1) + roundtab_79[dx & 0x3];
450                    dy = (dy >> 1) + roundtab_79[dy & 0x3];
451    
452                    b_dx = (b_dx >> 1) + roundtab_79[b_dx & 0x3];
453                    b_dy = (b_dy >> 1) + roundtab_79[b_dy & 0x3];
454    
455                    break;
456    
457            default: /* MODE_DIRECT (or MODE_DIRECT_NONE_MV in case of bframes decoding) */
458                    sumx = sumy = b_sumx = b_sumy = 0;
459    
460                    for (k = 0; k < 4; k++) {
461    
462                            dx = fmvs[k].x; dy = fmvs[k].y;
463                            b_dx = bmvs[k].x; b_dy = bmvs[k].y;
464    
465                            if (quarterpel) {
466                                    sumx += dx/2; sumy += dy/2;
467                                    b_sumx += b_dx/2; b_sumy += b_dy/2;
468    
469                                    if ((dx&3) | (dy&3)) {
470                                            interpolate8x8_quarterpel(tmp - (i * 16+(k&1)*8) - (j * 16+((k>>1)*8)) * edged_width,
471                                                    (uint8_t *) f_ref->y,
472                                                    tmp + 32, tmp + 64, tmp + 96,
473                                                    16*i + (k&1)*8, 16*j + (k>>1)*8, dx, dy, edged_width, 0);
474                                            ptr1 = tmp;
475                                    } else ptr1 = f_ref->y + (16*j + (k>>1)*8 + dy/4)*edged_width + 16*i + (k&1)*8 + dx/4;
476    
477                                    if ((b_dx&3) | (b_dy&3)) {
478                                            interpolate8x8_quarterpel(tmp - (i * 16+(k&1)*8) - (j * 16+((k>>1)*8)) * edged_width + 16,
479                                                    (uint8_t *) b_ref->y,
480                                                    tmp + 16, tmp + 32, tmp + 48,
481                                                    16*i + (k&1)*8, 16*j + (k>>1)*8, b_dx, b_dy, edged_width, 0);
482                                            ptr2 = tmp + 16;
483                                    } else ptr2 = b_ref->y + (16*j + (k>>1)*8 + b_dy/4)*edged_width + 16*i + (k&1)*8 + b_dx/4;
484                            } else {
485                                    sumx += dx; sumy += dy;
486                                    b_sumx += b_dx; b_sumy += b_dy;
487    
488                                    ptr1 = get_ref(f_ref->y, f_refh->y, f_refv->y, f_refhv->y,
489                                                                    2*i + (k&1), 2*j + (k>>1), 8, dx, dy, edged_width);
490                                    ptr2 = get_ref(b_ref->y, b_refh->y, b_refv->y, b_refhv->y,
491                                                                    2*i + (k&1), 2*j + (k>>1), 8, b_dx, b_dy,  edged_width);
492                            }
493                            transfer_8to16sub2(&dct_codes[k * 64],
494                                                                    cur->y + (i * 16+(k&1)*8) + (j * 16+((k>>1)*8)) * edged_width,
495                                                                    ptr1, ptr2,     edged_width);
496    
497                    }
498    
499                    dx = (sumx >> 3) + roundtab_76[sumx & 0xf];
500                    dy = (sumy >> 3) + roundtab_76[sumy & 0xf];
501                    b_dx = (b_sumx >> 3) + roundtab_76[b_sumx & 0xf];
502                    b_dy = (b_sumy >> 3) + roundtab_76[b_sumy & 0xf];
503    
504                  compensate8x8_halfpel(&dct_codes[0 * 64], cur->y, ref->y, refh->y,                  break;
505                                                            refv->y, refhv->y, 16 * i, 16 * j, mb->mvs[0].x,          }
506                                                            mb->mvs[0].y, edged_width);  
507                  compensate8x8_halfpel(&dct_codes[1 * 64], cur->y, ref->y, refh->y,          /* v block-based chroma interpolation for direct and interpolate modes */
508                                                            refv->y, refhv->y, 16 * i + 8, 16 * j,          transfer_8to16sub2(&dct_codes[4 * 64],
509                                                            mb->mvs[1].x, mb->mvs[1].y, edged_width);                                                  cur->u + (j * 8) * edged_width / 2 + (i * 8),
510                  compensate8x8_halfpel(&dct_codes[2 * 64], cur->y, ref->y, refh->y,                                                  interpolate8x8_switch2(tmp, b_ref->u, 8 * i, 8 * j,
511                                                            refv->y, refhv->y, 16 * i, 16 * j + 8,                                                                                                  b_dx, b_dy, edged_width / 2, 0),
512                                                            mb->mvs[2].x, mb->mvs[2].y, edged_width);                                                  interpolate8x8_switch2(tmp + 8, f_ref->u, 8 * i, 8 * j,
513                  compensate8x8_halfpel(&dct_codes[3 * 64], cur->y, ref->y, refh->y,                                                                                                  dx, dy, edged_width / 2, 0),
                                                           refv->y, refhv->y, 16 * i + 8, 16 * j + 8,  
                                                           mb->mvs[3].x, mb->mvs[3].y, edged_width);  
   
                 sum = mb->mvs[0].x + mb->mvs[1].x + mb->mvs[2].x + mb->mvs[3].x;  
                 dx = (sum ? SIGN(sum) *  
                           (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0);  
   
                 sum = mb->mvs[0].y + mb->mvs[1].y + mb->mvs[2].y + mb->mvs[3].y;  
                 dy = (sum ? SIGN(sum) *  
                           (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0);  
   
                 /* uv-block-based compensation */  
                 interpolate8x8_switch(refv->u, ref->u, 8 * i, 8 * j, dx, dy,  
                                                           edged_width / 2, rounding);  
                 transfer_8to16sub(&dct_codes[4 * 64],  
                                                   cur->u + 8 * j * edged_width / 2 + 8 * i,  
                                                   refv->u + 8 * j * edged_width / 2 + 8 * i,  
514                                                    edged_width / 2);                                                    edged_width / 2);
515    
516                  interpolate8x8_switch(refv->v, ref->v, 8 * i, 8 * j, dx, dy,          transfer_8to16sub2(&dct_codes[5 * 64],
517                                                            edged_width / 2, rounding);                                                  cur->v + (j * 8) * edged_width / 2 + (i * 8),
518                  transfer_8to16sub(&dct_codes[5 * 64],                                                  interpolate8x8_switch2(tmp, b_ref->v, 8 * i, 8 * j,
519                                                    cur->v + 8 * j * edged_width / 2 + 8 * i,                                                                                                  b_dx, b_dy, edged_width / 2, 0),
520                                                    refv->v + 8 * j * edged_width / 2 + 8 * i,                                                  interpolate8x8_switch2(tmp + 8, f_ref->v, 8 * i, 8 * j,
521                                                                                                    dx, dy, edged_width / 2, 0),
522                                                    edged_width / 2);                                                    edged_width / 2);
523          }          }
524    
525    
526    
527    void generate_GMCparameters( const int num_wp, const int res,
528                                                    const WARPPOINTS *const warp,
529                                                    const int width, const int height,
530                                                    GMC_DATA *const gmc)
531    {
532            const int du0 = warp->duv[0].x;
533            const int dv0 = warp->duv[0].y;
534            const int du1 = warp->duv[1].x;
535            const int dv1 = warp->duv[1].y;
536            const int du2 = warp->duv[2].x;
537            const int dv2 = warp->duv[2].y;
538    
539            gmc->W = width;
540            gmc->H = height;
541    
542            gmc->rho = 4 - log2bin(res-1);  /* = {3,2,1,0} for res={2,4,8,16} */
543    
544            gmc->alpha = log2bin(gmc->W-1);
545            gmc->Ws = (1 << gmc->alpha);
546    
547            gmc->dxF = 16*gmc->Ws + RDIV( 8*gmc->Ws*du1, gmc->W );
548            gmc->dxG = RDIV( 8*gmc->Ws*dv1, gmc->W );
549            gmc->Fo  = (res*du0 + 1) << (gmc->alpha+gmc->rho-1);
550            gmc->Go  = (res*dv0 + 1) << (gmc->alpha+gmc->rho-1);
551    
552            if (num_wp==2) {
553                    gmc->dyF = -gmc->dxG;
554                    gmc->dyG =  gmc->dxF;
555            } else if (num_wp==3) {
556                    gmc->beta = log2bin(gmc->H-1);
557                    gmc->Hs = (1 << gmc->beta);
558                    gmc->dyF =                       RDIV( 8*gmc->Hs*du2, gmc->H );
559                    gmc->dyG = 16*gmc->Hs + RDIV( 8*gmc->Hs*dv2, gmc->H );
560                    if (gmc->beta > gmc->alpha) {
561                            gmc->dxF <<= (gmc->beta - gmc->alpha);
562                            gmc->dxG <<= (gmc->beta - gmc->alpha);
563                            gmc->alpha = gmc->beta;
564                            gmc->Ws = 1<< gmc->beta;
565                    } else {
566                            gmc->dyF <<= gmc->alpha - gmc->beta;
567                            gmc->dyG <<= gmc->alpha - gmc->beta;
568                    }
569            }
570    
571            gmc->cFo = gmc->dxF + gmc->dyF + (1 << (gmc->alpha+gmc->rho+1));
572            gmc->cFo += 16*gmc->Ws*(du0-1);
573    
574            gmc->cGo = gmc->dxG + gmc->dyG + (1 << (gmc->alpha+gmc->rho+1));
575            gmc->cGo += 16*gmc->Ws*(dv0-1);
576    }
577    
578    void
579    generate_GMCimage(      const GMC_DATA *const gmc_data, /* [input] precalculated data */
580                                            const IMAGE *const pRef,                /* [input] */
581                                            const int mb_width,
582                                            const int mb_height,
583                                            const int stride,
584                                            const int stride2,
585                                            const int fcode,                                /* [input] some parameters... */
586                                            const int32_t quarterpel,               /* [input] for rounding avgMV */
587                                            const int reduced_resolution,   /* [input] ignored */
588                                            const int32_t rounding,                 /* [input] for rounding image data */
589                                            MACROBLOCK *const pMBs,                 /* [output] average motion vectors */
590                                            IMAGE *const pGMC)                              /* [output] full warped image */
591    {
592    
593            unsigned int mj,mi;
594            VECTOR avgMV;
595    
596            for (mj = 0; mj < (unsigned int)mb_height; mj++)
597                    for (mi = 0; mi < (unsigned int)mb_width; mi++) {
598    
599                            avgMV = generate_GMCimageMB(gmc_data, pRef, mi, mj,
600                                                    stride, stride2, quarterpel, rounding, pGMC);
601    
602                            pMBs[mj*mb_width+mi].amv.x = gmc_sanitize(avgMV.x, quarterpel, fcode);
603                            pMBs[mj*mb_width+mi].amv.y = gmc_sanitize(avgMV.y, quarterpel, fcode);
604                            pMBs[mj*mb_width+mi].mcsel = 0; /* until mode decision */
605            }
606    }
607    
608    
609    
610    #define MLT(i)  (((16-(i))<<16) + (i))
611    static const uint32_t MTab[16] = {
612      MLT( 0), MLT( 1), MLT( 2), MLT( 3), MLT( 4), MLT( 5), MLT( 6), MLT(7),
613      MLT( 8), MLT( 9), MLT(10), MLT(11), MLT(12), MLT(13), MLT(14), MLT(15)
614    };
615    #undef MLT
616    
617    VECTOR generate_GMCimageMB( const GMC_DATA *const gmc_data,
618                                                            const IMAGE *const pRef,
619                                                            const int mi, const int mj,
620                                                            const int stride,
621                                                            const int stride2,
622                                                            const int quarterpel,
623                                                            const int rounding,
624                                                            IMAGE *const pGMC)
625    {
626            const int W = gmc_data->W;
627            const int H = gmc_data->H;
628    
629            const int rho = gmc_data->rho;
630            const int alpha = gmc_data->alpha;
631    
632            const int rounder = ( 128 - (rounding<<(rho+rho)) ) << 16;
633    
634            const int dxF = gmc_data->dxF;
635            const int dyF = gmc_data->dyF;
636            const int dxG = gmc_data->dxG;
637            const int dyG = gmc_data->dyG;
638    
639            uint8_t *dstY, *dstU, *dstV;
640    
641            int I,J;
642            VECTOR avgMV = {0,0};
643    
644            int32_t Fj, Gj;
645    
646            dstY = &pGMC->y[(mj*16)*stride+mi*16] + 16;
647    
648            Fj = gmc_data->Fo + dyF*mj*16 + dxF*mi*16;
649            Gj = gmc_data->Go + dyG*mj*16 + dxG*mi*16;
650    
651            for (J = 16; J > 0; --J) {
652                    int32_t Fi, Gi;
653    
654                    Fi = Fj; Fj += dyF;
655                    Gi = Gj; Gj += dyG;
656                    for (I = -16; I < 0; ++I) {
657                            int32_t F, G;
658                            uint32_t ri, rj;
659    
660                            F = ( Fi >> (alpha+rho) ) << rho; Fi += dxF;
661                            G = ( Gi >> (alpha+rho) ) << rho; Gi += dxG;
662    
663                            avgMV.x += F;
664                            avgMV.y += G;
665    
666                            ri = MTab[F&15];
667                            rj = MTab[G&15];
668    
669                            F >>= 4;
670                            G >>= 4;
671    
672                            if (F < -1) F = -1;
673                            else if (F > W) F = W;
674                            if (G< -1) G=-1;
675                            else if (G>H) G=H;
676    
677                            {        /* MMX-like bilinear... */
678                                    const int offset = G*stride + F;
679                                    uint32_t f0, f1;
680                                    f0 = pRef->y[ offset +0 ];
681                                    f0 |= pRef->y[ offset +1 ] << 16;
682                                    f1 = pRef->y[ offset+stride +0 ];
683                                    f1 |= pRef->y[ offset+stride +1 ] << 16;
684                                    f0 = (ri*f0)>>16;
685                                    f1 = (ri*f1) & 0x0fff0000;
686                                    f0 |= f1;
687                                    f0 = ( rj*f0 + rounder ) >> 24;
688    
689                                    dstY[I] = (uint8_t)f0;
690                            }
691                    }
692    
693                    dstY += stride;
694            }
695    
696            dstU = &pGMC->u[(mj*8)*stride2+mi*8] + 8;
697            dstV = &pGMC->v[(mj*8)*stride2+mi*8] + 8;
698    
699            Fj = gmc_data->cFo + dyF*4 *mj*8 + dxF*4 *mi*8;
700            Gj = gmc_data->cGo + dyG*4 *mj*8 + dxG*4 *mi*8;
701    
702            for (J = 8; J > 0; --J) {
703                    int32_t Fi, Gi;
704                    Fi = Fj; Fj += 4*dyF;
705                    Gi = Gj; Gj += 4*dyG;
706    
707                    for (I = -8; I < 0; ++I) {
708                            int32_t F, G;
709                            uint32_t ri, rj;
710    
711                            F = ( Fi >> (alpha+rho+2) ) << rho; Fi += 4*dxF;
712                            G = ( Gi >> (alpha+rho+2) ) << rho; Gi += 4*dxG;
713    
714                            ri = MTab[F&15];
715                            rj = MTab[G&15];
716    
717                            F >>= 4;
718                            G >>= 4;
719    
720                            if (F < -1) F=-1;
721                            else if (F >= W/2) F = W/2;
722                            if (G < -1) G = -1;
723                            else if (G >= H/2) G = H/2;
724    
725                            {
726                                    const int offset = G*stride2 + F;
727                                    uint32_t f0, f1;
728    
729                                    f0      = pRef->u[ offset                +0 ];
730                                    f0 |= pRef->u[ offset            +1 ] << 16;
731                                    f1      = pRef->u[ offset+stride2 +0 ];
732                                    f1 |= pRef->u[ offset+stride2 +1 ] << 16;
733                                    f0 = (ri*f0)>>16;
734                                    f1 = (ri*f1) & 0x0fff0000;
735                                    f0 |= f1;
736                                    f0 = ( rj*f0 + rounder ) >> 24;
737    
738                                    dstU[I] = (uint8_t)f0;
739    
740    
741                                    f0      = pRef->v[ offset                +0 ];
742                                    f0 |= pRef->v[ offset            +1 ] << 16;
743                                    f1      = pRef->v[ offset+stride2 +0 ];
744                                    f1 |= pRef->v[ offset+stride2 +1 ] << 16;
745                                    f0 = (ri*f0)>>16;
746                                    f1 = (ri*f1) & 0x0fff0000;
747                                    f0 |= f1;
748                                    f0 = ( rj*f0 + rounder ) >> 24;
749    
750                                    dstV[I] = (uint8_t)f0;
751  }  }
752                    }
753                    dstU += stride2;
754                    dstV += stride2;
755            }
756    
757    
758            avgMV.x -= 16*((256*mi+120)<<4);        /* 120 = 15*16/2 */
759            avgMV.y -= 16*((256*mj+120)<<4);
760    
761            avgMV.x = RSHIFT( avgMV.x, (4+7-quarterpel) );
762            avgMV.y = RSHIFT( avgMV.y, (4+7-quarterpel) );
763    
764            return avgMV;
765    }
766    
767    
768    
769    #ifdef OLD_GRUEL_GMC
770    void
771    generate_GMCparameters( const int num_wp,                       /* [input]: number of warppoints */
772                                                    const int res,                  /* [input]: resolution */
773                                                    const WARPPOINTS *const warp, /* [input]: warp points */
774                                                    const int width, const int height,
775                                                    GMC_DATA *const gmc)    /* [output] precalculated parameters */
776    {
777    
778    /* We follow mainly two sources: The original standard, which is ugly, and the
779       thesis from Andreas Dehnhardt, which is much nicer.
780    
781            Notation is: indices are written next to the variable,
782                                     primes in the standard are denoted by a suffix 'p'.
783            types are   "c"=constant, "i"=input parameter, "f"=calculated, then fixed,
784                                    "o"=output data, " "=other, "u" = unused, "p"=calc for every pixel
785    
786    type | variable name  |   ISO name (TeX-style) |  value or range  |  usage
787    -------------------------------------------------------------------------------------
788     c   | H                          |   H                                    |  [16 , ?]            |  image width (w/o edges)
789     c   | W                          |   W                                    |  [16 , ?]            |  image height (w/o edges)
790    
791     c   | i0                         |   i_0                                  |  0                           |  ref. point #1, X
792     c   | j0                         |   j_0                                  |  0                           |  ref. point #1, Y
793     c   | i1                         |   i_1                                  |  W                           |  ref. point #2, X
794     c   | j1                         |   j_1                                  |  0                           |  ref. point #2, Y
795     cu  | i2                         |   i_2                                  |  0                           |  ref. point #3, X
796     cu  | i2                         |   j_2                                  |  H                           |  ref. point #3, Y
797    
798     i   | du0                |   du[0]                        |  [-16863,16863]  |  warp vector #1, Y
799     i   | dv0                |   dv[0]                        |  [-16863,16863]  |  warp vector #1, Y
800     i   | du1                |   du[1]                        |  [-16863,16863]  |  warp vector #2, Y
801     i   | dv1                |   dv[1]                        |  [-16863,16863]  |  warp vector #2, Y
802     iu  | du2                |   du[2]                        |  [-16863,16863]  |  warp vector #3, Y
803     iu  | dv2                |   dv[2]                        |  [-16863,16863]  |  warp vector #3, Y
804    
805     i   | s                          |   s                                 |  {2,4,8,16}     |  interpol. resolution
806     f   | sigma              |             -                          |  log2(s)            |  X / s == X >> sigma
807     f   | r                          |   r                                 |  =16/s                   |  complementary res.
808     f   | rho                      |   \rho                                 |  log2(r)              |  X / r == X >> rho
809    
810     f   | i0s                      |   i'_0                                 |                                |
811     f   | j0s                      |   j'_0                                 |                                |
812     f       | i1s                  |   i'_1                                 |                                |
813     f       | j1s                  |   j'_1                                 |                                |
814     f       | i2s                  |   i'_2                                 |                                |
815     f       | j2s                  |   j'_2                                 |                                |
816    
817     f   | alpha              |   \alpha                       |                              |  2^{alpha-1} < W <= 2^alpha
818     f   | beta                |   \beta                            |                                 |  2^{beta-1} < H <= 2^beta
819    
820     f   | Ws                        |   W'                            | W = 2^{alpha}      |  scaled width
821     f   | Hs                        |   H'                            | W = 2^{beta}        |  scaled height
822    
823     f   | i1ss                |   i''_1                            |  "virtual sprite stuff"
824     f   | j1ss                |   j''_1                            |  "virtual sprite stuff"
825     f   | i2ss                |   i''_2                            |  "virtual sprite stuff"
826     f   | j2ss                |   j''_2                            |  "virtual sprite stuff"
827    */
828    
829    /* Some calculations are disabled because we only use 2 warppoints at the moment */
830    
831            int du0 = warp->duv[0].x;
832            int dv0 = warp->duv[0].y;
833            int du1 = warp->duv[1].x;
834            int dv1 = warp->duv[1].y;
835    #if 0
836            int du2 = warp->duv[2].x;
837            int dv2 = warp->duv[2].y;
838    #endif
839    
840            gmc->num_wp = num_wp;
841    
842            gmc->s = res;                                           /* scaling parameters 2,4,8 or 16 */
843            gmc->sigma = log2bin(res-1);    /* log2bin(15)=4, log2bin(16)=5, log2bin(17)=5  */
844            gmc->r = 16/res;
845            gmc->rho = 4 - gmc->sigma;              /* = log2bin(r-1) */
846    
847            gmc->W = width;
848            gmc->H = height;                        /* fixed reference coordinates */
849    
850            gmc->alpha = log2bin(gmc->W-1);
851            gmc->Ws= 1<<gmc->alpha;
852    
853    #if 0
854            gmc->beta = log2bin(gmc->H-1);
855            gmc->Hs= 1<<gmc->beta;
856    #endif
857    
858    #if 0
859            printf("du0=%d dv0=%d du1=%d dv1=%d s=%d sigma=%d W=%d alpha=%d, Ws=%d, rho=%d\n",du0,dv0,du1,dv1,gmc->s,gmc->sigma,gmc->W,gmc->alpha,gmc->Ws,gmc->rho);
860    #endif
861    
862            /*
863             * i2s is only needed for num_wp >= 3, etc.
864             * the 's' values are in 1/s pel resolution
865             */
866            gmc->i0s = res/2 * ( du0 );
867            gmc->j0s = res/2 * ( dv0 );
868            gmc->i1s = res/2 * (2*width + du1 + du0 );
869            gmc->j1s = res/2 * ( dv1 + dv0 );
870    #if 0
871            gmc->i2s = res/2 * ( du2 + du0 );
872            gmc->j2s = res/2 * (2*height + dv2 + dv0 );
873    #endif
874    
875            /* i2s and i2ss are only needed for num_wp == 3, etc.  */
876    
877            /* the 'ss' values are in 1/16 pel resolution */
878            gmc->i1ss = 16*gmc->Ws + ROUNDED_DIV(((gmc->W-gmc->Ws)*(gmc->r*gmc->i0s) + gmc->Ws*(gmc->r*gmc->i1s - 16*gmc->W)),gmc->W);
879            gmc->j1ss = ROUNDED_DIV( ((gmc->W - gmc->Ws)*(gmc->r*gmc->j0s) + gmc->Ws*gmc->r*gmc->j1s) ,gmc->W );
880    
881    #if 0
882            gmc->i2ss = ROUNDED_DIV( ((gmc->H - gmc->Hs)*(gmc->r*gmc->i0s) + gmc->Hs*(gmc->r*gmc->i2s)), gmc->H);
883            gmc->j2ss = 16*gmc->Hs + ROUNDED_DIV( ((gmc->H-gmc->Hs)*(gmc->r*gmc->j0s) + gmc->Ws*(gmc->r*gmc->j2s - 16*gmc->H)), gmc->H);
884    #endif
885    
886            return;
887    }
888    
889    void
890    generate_GMCimage(      const GMC_DATA *const gmc_data,         /* [input] precalculated data */
891                                            const IMAGE *const pRef,                        /* [input] */
892                                            const int mb_width,
893                                            const int mb_height,
894                                            const int stride,
895                                            const int stride2,
896                                            const int fcode,                                        /* [input] some parameters... */
897                                            const int32_t quarterpel,                       /* [input] for rounding avgMV */
898                                            const int reduced_resolution,           /* [input] ignored */
899                                            const int32_t rounding,                 /* [input] for rounding image data */
900                                            MACROBLOCK *const pMBs,         /* [output] average motion vectors */
901                                            IMAGE *const pGMC)                      /* [output] full warped image */
902    {
903    
904            unsigned int mj,mi;
905            VECTOR avgMV;
906    
907            for (mj = 0;mj < mb_height; mj++)
908            for (mi = 0;mi < mb_width; mi++) {
909    
910                    avgMV = generate_GMCimageMB(gmc_data, pRef, mi, mj,
911                                            stride, stride2, quarterpel, rounding, pGMC);
912    
913                    pMBs[mj*mb_width+mi].amv.x = gmc_sanitize(avgMV.x, quarterpel, fcode);
914                    pMBs[mj*mb_width+mi].amv.y = gmc_sanitize(avgMV.y, quarterpel, fcode);
915                    pMBs[mj*mb_width+mi].mcsel = 0; /* until mode decision */
916            }
917    }
918    
919    
920    VECTOR generate_GMCimageMB(     const GMC_DATA *const gmc_data, /* [input] all precalc data */
921                                                            const IMAGE *const pRef,                /* [input] */
922                                                            const int mi, const int mj,     /* [input] MB position  */
923                                                            const int stride,                               /* [input] Lumi stride */
924                                                            const int stride2,                              /* [input] chroma stride */
925                                                            const int quarterpel,                   /* [input] for rounding of avgMV */
926                                                            const int rounding,                     /* [input] for rounding of imgae data */
927                                                            IMAGE *const pGMC)                              /* [outut] generate image */
928    
929    /*
930    type | variable name  |   ISO name (TeX-style) |  value or range  |  usage
931    -------------------------------------------------------------------------------------
932     p   | F                          |   F(i,j)                       |                              | pelwise motion vector X in s-th pel
933     p   | G                          |   G(i,j)                       |                              | pelwise motion vector Y in s-th pel
934     p   | Fc                        |   F_c(i,j)                    |                                |
935     p   | Gc                        |   G_c(i,j)                    |                                | same for chroma
936    
937     p   | Y00                      |   Y_{00}                         |  [0,255*s*s]        | first: 4 neighbouring Y-values
938     p   | Y01                      |   Y_{01}                         |  [0,255]            | at fullpel position, around the
939     p   | Y10                      |   Y_{10}                         |  [0,255*s]    | position where pelweise MV points to
940     p   | Y11                      |   Y_{11}                         |  [0,255]            | later: bilinear interpol Y-values in Y00
941    
942     p   | C00                      |   C_{00}                         |  [0,255*s*s]        | same for chroma Cb and Cr
943     p   | C01                      |   C_{01}                         |  [0,255]            |
944     p   | C10                      |   C_{10}                         |  [0,255*s]    |
945     p   | C11                      |   C_{11}                         |  [0,255]            |
946    
947    */
948    {
949            const int W = gmc_data->W;
950            const int H = gmc_data->H;
951    
952            const int s = gmc_data->s;
953            const int sigma = gmc_data->sigma;
954    
955            const int r = gmc_data->r;
956            const int rho = gmc_data->rho;
957    
958            const int i0s = gmc_data->i0s;
959            const int j0s = gmc_data->j0s;
960    
961            const int i1ss = gmc_data->i1ss;
962            const int j1ss = gmc_data->j1ss;
963    #if 0
964            const int i2ss = gmc_data->i2ss;
965            const int j2ss = gmc_data->j2ss;
966    #endif
967    
968            const int alpha = gmc_data->alpha;
969            const int Ws    = gmc_data->Ws;
970    
971    #if 0
972            const int beta  = gmc_data->beta;
973            const int Hs    = gmc_data->Hs;
974    #endif
975    
976            int I,J;
977            VECTOR avgMV = {0,0};
978    
979            for (J=16*mj;J<16*(mj+1);J++)
980            for (I=16*mi;I<16*(mi+1);I++)
981            {
982                    int F= i0s + ( ((-r*i0s+i1ss)*I + (r*j0s-j1ss)*J + (1<<(alpha+rho-1))) >>  (alpha+rho) );
983                    int G= j0s + ( ((-r*j0s+j1ss)*I + (-r*i0s+i1ss)*J + (1<<(alpha+rho-1))) >> (alpha+rho) );
984    
985    /* this naive implementation (with lots of multiplications) isn't slower (rather faster) than
986       working incremental. Don't ask me why... maybe the whole this is memory bound? */
987    
988                    const int ri= F & (s-1); /* fractional part of pelwise MV X */
989                    const int rj= G & (s-1); /* fractional part of pelwise MV Y */
990    
991                    int Y00,Y01,Y10,Y11;
992    
993    /* unclipped values are used for avgMV */
994                    avgMV.x += F-(I<<sigma);                /* shift position to 1/s-pel, as the MV is */
995                    avgMV.y += G-(J<<sigma);                /* TODO: don't do this (of course) */
996    
997                    F >>= sigma;
998                    G >>= sigma;
999    
1000    /* clip values to be in range. Since we have edges, clip to 1 less than lower boundary
1001       this way positions F+1/G+1 are still right */
1002    
1003                    if (F< -1)
1004                            F=-1;
1005                    else if (F>W)
1006                            F=W;    /* W or W-1 doesn't matter, so save 1 subtract ;-) */
1007                    if (G< -1)
1008                            G=-1;
1009                    else if (G>H)
1010                            G=H;            /* dito */
1011    
1012                    Y00 = pRef->y[ G*stride + F ];                          /* Lumi values */
1013                    Y01 = pRef->y[ G*stride + F+1 ];
1014                    Y10 = pRef->y[ G*stride + F+stride ];
1015                    Y11 = pRef->y[ G*stride + F+stride+1 ];
1016    
1017                    /* bilinear interpolation */
1018                    Y00 = ((s-ri)*Y00 + ri*Y01);
1019                    Y10 = ((s-ri)*Y10 + ri*Y11);
1020                    Y00 = ((s-rj)*Y00 + rj*Y10 + s*s/2 - rounding ) >> (sigma+sigma);
1021    
1022                    pGMC->y[J*stride+I] = (uint8_t)Y00;                                                                             /* output 1 Y-pixel */
1023            }
1024    
1025    
1026    /* doing chroma _here_ is even more stupid and slow, because won't be used until Compensation and
1027            most likely not even then (only if the block really _is_ GMC)
1028    */
1029    
1030            for (J=8*mj;J<8*(mj+1);J++)             /* this plays the role of j_c,i_c in the standard */
1031            for (I=8*mi;I<8*(mi+1);I++)             /* For I_c we have to use I_c = 4*i_c+1 ! */
1032            {
1033                    /* same positions for both chroma components, U=Cb and V=Cr */
1034                    int Fc=((-r*i0s+i1ss)*(4*I+1) + (r*j0s-j1ss)*(4*J+1) +2*Ws*r*i0s
1035                                                    -16*Ws +(1<<(alpha+rho+1)))>>(alpha+rho+2);
1036                    int Gc=((-r*j0s+j1ss)*(4*I+1) +(-r*i0s+i1ss)*(4*J+1) +2*Ws*r*j0s
1037                                                    -16*Ws +(1<<(alpha+rho+1))) >>(alpha+rho+2);
1038    
1039                    const int ri= Fc & (s-1); /* fractional part of pelwise MV X */
1040                    const int rj= Gc & (s-1); /* fractional part of pelwise MV Y */
1041    
1042                    int C00,C01,C10,C11;
1043    
1044                    Fc >>= sigma;
1045                    Gc >>= sigma;
1046    
1047                    if (Fc< -1)
1048                            Fc=-1;
1049                    else if (Fc>=W/2)
1050                            Fc=W/2;         /* W or W-1 doesn't matter, so save 1 subtraction ;-) */
1051                    if (Gc< -1)
1052                            Gc=-1;
1053                    else if (Gc>=H/2)
1054                            Gc=H/2;         /* dito */
1055    
1056    /* now calculate U data */
1057                    C00 = pRef->u[ Gc*stride2 + Fc ];                               /* chroma-value Cb */
1058                    C01 = pRef->u[ Gc*stride2 + Fc+1 ];
1059                    C10 = pRef->u[ (Gc+1)*stride2 + Fc ];
1060                    C11 = pRef->u[ (Gc+1)*stride2 + Fc+1 ];
1061    
1062                    /* bilinear interpolation */
1063                    C00 = ((s-ri)*C00 + ri*C01);
1064                    C10 = ((s-ri)*C10 + ri*C11);
1065                    C00 = ((s-rj)*C00 + rj*C10 + s*s/2 - rounding ) >> (sigma+sigma);
1066    
1067                    pGMC->u[J*stride2+I] = (uint8_t)C00;                                                                            /* output 1 U-pixel */
1068    
1069    /* now calculate V data */
1070                    C00 = pRef->v[ Gc*stride2 + Fc ];                               /* chroma-value Cr */
1071                    C01 = pRef->v[ Gc*stride2 + Fc+1 ];
1072                    C10 = pRef->v[ (Gc+1)*stride2 + Fc ];
1073                    C11 = pRef->v[ (Gc+1)*stride2 + Fc+1 ];
1074    
1075                    /* bilinear interpolation */
1076                    C00 = ((s-ri)*C00 + ri*C01);
1077                    C10 = ((s-ri)*C10 + ri*C11);
1078                    C00 = ((s-rj)*C00 + rj*C10 + s*s/2 - rounding ) >> (sigma+sigma);
1079    
1080                    pGMC->v[J*stride2+I] = (uint8_t)C00;                                                                            /* output 1 V-pixel */
1081            }
1082    
1083    /* The average vector is rounded from 1/s-pel to 1/2 or 1/4 using the '//' operator */
1084    
1085            avgMV.x = RSHIFT( avgMV.x, (sigma+7-quarterpel) );
1086            avgMV.y = RSHIFT( avgMV.y, (sigma+7-quarterpel) );
1087    
1088            /* ^^^^ this is the way MS Reference Software does it */
1089    
1090            return avgMV;   /* clipping to fcode area is done outside! */
1091    }
1092    
1093    #endif

Legend:
Removed from v.437  
changed lines
  Added in v.1053

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