[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

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

Legend:
Removed from v.195  
changed lines
  Added in v.851

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