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

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

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

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

Legend:
Removed from v.1.11.2.3  
changed lines
  Added in v.1.19.2.1

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