[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.23, Wed Feb 12 11:48:21 2003 UTC revision 1.25, Tue Dec 28 19:19:43 2010 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   *  XVID MPEG-4 VIDEO CODEC
4  // 14.04.2002   bframe compensation   *  - Motion Compensation related code  -
5     *
6     *  Copyright(C) 2002 Peter Ross <pross@xvid.org>
7     *               2003 Christoph Lampert <gruel@web.de>
8     *
9     *  This program is free software ; you can redistribute it and/or modify
10     *  it under the terms of the GNU General Public License as published by
11     *  the Free Software Foundation ; either version 2 of the License, or
12     *  (at your option) any later version.
13     *
14     *  This program is distributed in the hope that it will be useful,
15     *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
16     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     *  GNU General Public License for more details.
18     *
19     *  You should have received a copy of the GNU General Public License
20     *  along with this program ; if not, write to the Free Software
21     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22     *
23     * $Id$
24     *
25     ****************************************************************************/
26    
27  #include <stdio.h>  #include <stdio.h>
28    
29  #include "../encoder.h"  #include "../encoder.h"
30  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
31  #include "../image/interpolate8x8.h"  #include "../image/interpolate8x8.h"
32  #include "../image/reduced.h"  #include "../image/qpel.h"
33  #include "../utils/timer.h"  #include "../utils/timer.h"
34  #include "motion.h"  #include "motion.h"
35    
36  #ifndef ABS   /*
37  #define ABS(X) (((X)>0)?(X):-(X))   * getref: calculate reference image pointer
38  #endif   * the decision to use interpolation h/v/hv or the normal image is
39  #ifndef SIGN   * based on dx & dy.
40  #define SIGN(X) (((X)>0)?1:-1)   */
 #endif  
   
 #ifndef RSHIFT  
 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))  
 #endif  
   
 /* assume b>0 */  
 #ifndef RDIV  
 #define RDIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))  
 #endif  
   
   
 /* This is borrowed from    decoder.c   */  
 static __inline int gmc_sanitize(int value, int quarterpel, int fcode)  
 {  
         int length = 1 << (fcode+4);  
   
 //      if (quarterpel) value *= 2;  
   
         if (value < -length)  
                 return -length;  
         else if (value >= length)  
                 return length-1;  
         else return value;  
 }  
   
 /* And this is borrowed from   bitstream.c  until we find a common solution */  
41    
42  static uint32_t __inline  static __inline const uint8_t *
43  log2bin(uint32_t value)  get_ref(const uint8_t * const refn,
44                    const uint8_t * const refh,
45                    const uint8_t * const refv,
46                    const uint8_t * const refhv,
47                    const uint32_t x,
48                    const uint32_t y,
49                    const uint32_t block,
50                    const int32_t dx,
51                    const int32_t dy,
52                    const int32_t stride)
53  {  {
54  /* Changed by Chenm001 */          switch (((dx & 1) << 1) + (dy & 1)) {
55  #if !defined(_MSC_VER)          case 0:
56          int n = 0;                  return refn + (int) (((int)x * (int)block + dx / 2) + ((int)y * (int)block + dy / 2) * (int)stride);
57            case 1:
58          while (value) {                  return refv + (int) (((int)x * (int)block + dx / 2) + ((int)y * (int)block + (dy - 1) / 2) * (int)stride);
59                  value >>= 1;          case 2:
60                  n++;                  return refh + (int) (((int)x * (int)block + (dx - 1) / 2) + ((int)y * (int)block + dy / 2) * (int)stride);
61          }          default:
62          return n;                  return refhv + (int) (((int)x * (int)block + (dx - 1) / 2) + ((int)y * (int)block + (dy - 1) / 2) * (int)stride);
 #else  
         __asm {  
                 bsr eax, value  
                 inc eax  
63          }          }
 #endif  
64  }  }
65    
   
66  static __inline void  static __inline void
67  compensate16x16_interpolate(int16_t * const dct_codes,  compensate16x16_interpolate(int16_t * const dct_codes,
68                                                          uint8_t * const cur,                                                          uint8_t * const cur,
# Line 80  Line 77 
77                                                          const int32_t dy,                                                          const int32_t dy,
78                                                          const int32_t stride,                                                          const int32_t stride,
79                                                          const int quarterpel,                                                          const int quarterpel,
                                                         const int reduced_resolution,  
80                                                          const int32_t rounding)                                                          const int32_t rounding)
81  {  {
82          const uint8_t * ptr;          const uint8_t * ptr;
83    
         if (!reduced_resolution) {  
84    
85                  if(quarterpel) {                  if(quarterpel) {
86                          if ((dx&3) | (dy&3)) {                          if ((dx&3) | (dy&3)) {
# Line 93  Line 88 
88                                                                                          (uint8_t *) ref, tmp + 32,                                                                                          (uint8_t *) ref, tmp + 32,
89                                                                                          tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding);                                                                                          tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding);
90                                  ptr = tmp;                                  ptr = tmp;
91                          } else ptr =  ref + (y + dy/4)*stride + x + dx/4; // fullpixel position                  } else ptr =  ref + ((int)y + dy/4)*(int)stride + (int)x + dx/4; /* fullpixel position */
92    
93                  } else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride);                  } else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride);
94    
# Line 106  Line 101 
101                  transfer_8to16sub(dct_codes+192, cur + y * stride + x + 8*stride+8,                  transfer_8to16sub(dct_codes+192, cur + y * stride + x + 8*stride+8,
102                                                            ptr + 8*stride + 8, stride);                                                            ptr + 8*stride + 8, stride);
103    
         } else { //reduced_resolution  
   
                 x *= 2; y *= 2;  
   
                 ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride);  
   
                 filter_18x18_to_8x8(dct_codes, cur+y*stride + x, stride);  
                 filter_diff_18x18_to_8x8(dct_codes, ptr, stride);  
   
                 filter_18x18_to_8x8(dct_codes+64, cur+y*stride + x + 16, stride);  
                 filter_diff_18x18_to_8x8(dct_codes+64, ptr + 16, stride);  
   
                 filter_18x18_to_8x8(dct_codes+128, cur+(y+16)*stride + x, stride);  
                 filter_diff_18x18_to_8x8(dct_codes+128, ptr + 16*stride, stride);  
   
                 filter_18x18_to_8x8(dct_codes+192, cur+(y+16)*stride + x + 16, stride);  
                 filter_diff_18x18_to_8x8(dct_codes+192, ptr + 16*stride + 16, stride);  
   
                 transfer32x32_copy(cur + y*stride + x, ptr, stride);  
         }  
104  }  }
105    
106  static __inline void  static __inline void
# Line 142  Line 117 
117                                                          const int32_t dy,                                                          const int32_t dy,
118                                                          const int32_t stride,                                                          const int32_t stride,
119                                                          const int32_t quarterpel,                                                          const int32_t quarterpel,
                                                         const int reduced_resolution,  
120                                                          const int32_t rounding)                                                          const int32_t rounding)
121  {  {
122          const uint8_t * ptr;          const uint8_t * ptr;
123    
         if (!reduced_resolution) {  
   
124                  if(quarterpel) {                  if(quarterpel) {
125                          if ((dx&3) | (dy&3)) {                          if ((dx&3) | (dy&3)) {
126                                  interpolate8x8_quarterpel(tmp - y*stride - x,                                  interpolate8x8_quarterpel(tmp - y*stride - x,
127                                                                                  (uint8_t *) ref, tmp + 32,                                                                                  (uint8_t *) ref, tmp + 32,
128                                                                                  tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding);                                                                                  tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding);
129                                  ptr = tmp;                                  ptr = tmp;
130                          } else ptr = ref + (y + dy/4)*stride + x + dx/4; // fullpixel position                  } else ptr = ref + ((int)y + dy/4)*(int)stride + (int)x + dx/4; /* fullpixel position */
131                  } else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride);                  } else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride);
132    
133                          transfer_8to16sub(dct_codes, cur + y * stride + x, ptr, stride);                          transfer_8to16sub(dct_codes, cur + y * stride + x, ptr, stride);
   
         } else { //reduced_resolution  
   
                 x *= 2; y *= 2;  
   
                 ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride);  
   
                 filter_18x18_to_8x8(dct_codes, cur+y*stride + x, stride);  
                 filter_diff_18x18_to_8x8(dct_codes, ptr, stride);  
   
                 transfer16x16_copy(cur + y*stride + x, ptr, stride);  
         }  
 }  
   
   
 static __inline void  
 compensate16x16_interpolate_ro(int16_t * const dct_codes,  
                                                                 const uint8_t * const cur,  
                                                                 const uint8_t * const ref,  
                                                                 const uint8_t * const refh,  
                                                                 const uint8_t * const refv,  
                                                                 const uint8_t * const refhv,  
                                                                 uint8_t * const tmp,  
                                                                 const uint32_t x, const uint32_t y,  
                                                                 const int32_t dx, const int32_t dy,  
                                                                 const int32_t stride,  
                                                                 const int quarterpel)  
 {  
         const uint8_t * ptr;  
   
         if(quarterpel) {  
                 if ((dx&3) | (dy&3)) {  
                         interpolate16x16_quarterpel(tmp - y * stride - x,  
                                                                                 (uint8_t *) ref, tmp + 32,  
                                                                                 tmp + 64, tmp + 96, x, y, dx, dy, stride, 0);  
                         ptr = tmp;  
                 } else ptr =  ref + (y + dy/4)*stride + x + dx/4; // fullpixel position  
   
         } else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride);  
   
         transfer_8to16subro(dct_codes, cur + y * stride + x,  
                                                   ptr, stride);  
         transfer_8to16subro(dct_codes+64, cur + y * stride + x + 8,  
                                                   ptr + 8, stride);  
         transfer_8to16subro(dct_codes+128, cur + y * stride + x + 8*stride,  
                                                   ptr + 8*stride, stride);  
         transfer_8to16subro(dct_codes+192, cur + y * stride + x + 8*stride+8,  
                                                   ptr + 8*stride + 8, stride);  
   
134  }  }
135    
136    
 /* XXX: slow, inelegant... */  
 static void  
 interpolate18x18_switch(uint8_t * const cur,  
                                                 const uint8_t * const refn,  
                                                 const uint32_t x,  
                                                 const uint32_t y,  
                                                 const int32_t dx,  
                                                 const int dy,  
                                                 const int32_t stride,  
                                                 const int32_t rounding)  
 {  
         interpolate8x8_switch(cur, refn, x-1, y-1, dx, dy, stride, rounding);  
         interpolate8x8_switch(cur, refn, x+7, y-1, dx, dy, stride, rounding);  
         interpolate8x8_switch(cur, refn, x+9, y-1, dx, dy, stride, rounding);  
   
         interpolate8x8_switch(cur, refn, x-1, y+7, dx, dy, stride, rounding);  
         interpolate8x8_switch(cur, refn, x+7, y+7, dx, dy, stride, rounding);  
         interpolate8x8_switch(cur, refn, x+9, y+7, dx, dy, stride, rounding);  
   
         interpolate8x8_switch(cur, refn, x-1, y+9, dx, dy, stride, rounding);  
         interpolate8x8_switch(cur, refn, x+7, y+9, dx, dy, stride, rounding);  
         interpolate8x8_switch(cur, refn, x+9, y+9, dx, dy, stride, rounding);  
 }  
   
137  static void  static void
138  CompensateChroma(       int dx, int dy,  CompensateChroma(       int dx, int dy,
139                                          const int i, const int j,                                          const int i, const int j,
# Line 243  Line 142 
142                                          uint8_t * const temp,                                          uint8_t * const temp,
143                                          int16_t * const coeff,                                          int16_t * const coeff,
144                                          const int32_t stride,                                          const int32_t stride,
145                                          const int rounding,                                          const int rounding)
                                         const int rrv)  
146  { /* uv-block-based compensation */  { /* uv-block-based compensation */
147    
         if (!rrv) {  
148                  transfer_8to16sub(coeff, Cur->u + 8 * j * stride + 8 * i,                  transfer_8to16sub(coeff, Cur->u + 8 * j * stride + 8 * i,
149                                                          interpolate8x8_switch2(temp, Ref->u, 8 * i, 8 * j,                                                          interpolate8x8_switch2(temp, Ref->u, 8 * i, 8 * j,
150                                                                                                          dx, dy, stride, rounding),                                                                                                          dx, dy, stride, rounding),
# Line 256  Line 153 
153                                                          interpolate8x8_switch2(temp, Ref->v, 8 * i, 8 * j,                                                          interpolate8x8_switch2(temp, Ref->v, 8 * i, 8 * j,
154                                                                                                          dx, dy, stride, rounding),                                                                                                          dx, dy, stride, rounding),
155                                                          stride);                                                          stride);
         } else {  
                 uint8_t * current, * reference;  
   
                 current = Cur->u + 16*j*stride + 16*i;  
                 reference = temp - 16*j*stride - 16*i;  
                 interpolate18x18_switch(reference, Ref->u, 16*i, 16*j, dx, dy, stride, rounding);  
                 filter_18x18_to_8x8(coeff, current, stride);  
                 filter_diff_18x18_to_8x8(coeff, temp, stride);  
                 transfer16x16_copy(current, temp, stride);  
   
                 current = Cur->v + 16*j*stride + 16*i;  
                 interpolate18x18_switch(reference, Ref->v, 16*i, 16*j, dx, dy, stride, rounding);  
                 filter_18x18_to_8x8(coeff + 64, current, stride);  
                 filter_diff_18x18_to_8x8(coeff + 64, temp, stride);  
                 transfer16x16_copy(current, temp, stride);  
         }  
156  }  }
157    
158  void  void
# Line 289  Line 170 
170                                           const uint32_t height,                                           const uint32_t height,
171                                           const uint32_t edged_width,                                           const uint32_t edged_width,
172                                           const int32_t quarterpel,                                           const int32_t quarterpel,
173                                           const int reduced_resolution,                                          const int32_t rounding,
174                                           const int32_t rounding)                                          uint8_t * const tmp)
175  {  {
176          int32_t dx;          int32_t dx;
177          int32_t dy;          int32_t dy;
178    
179            if (mb->mode == MODE_NOT_CODED) {       /* quick copy for early SKIP */
         uint8_t * const tmp = refv->u;  
   
         if ( (!reduced_resolution) && (mb->mode == MODE_NOT_CODED) ) {  /* quick copy for early SKIP */  
180  /* early SKIP is only activated in P-VOPs, not in S-VOPs, so mcsel can never be 1 */  /* early SKIP is only activated in P-VOPs, not in S-VOPs, so mcsel can never be 1 */
181    
182                  transfer16x16_copy(cur->y + 16 * (i + j * edged_width),                  transfer16x16_copy(cur->y + 16 * (i + j * edged_width),
# Line 317  Line 195 
195          if ((mb->mode == MODE_NOT_CODED || mb->mode == MODE_INTER          if ((mb->mode == MODE_NOT_CODED || mb->mode == MODE_INTER
196                                  || mb->mode == MODE_INTER_Q)) {                                  || mb->mode == MODE_INTER_Q)) {
197    
         /* reduced resolution + GMC:  not possible */  
   
198                  if (mb->mcsel) {                  if (mb->mcsel) {
199    
200                          /* call normal routine once, easier than "if (mcsel)"ing all the time */                          /* call normal routine once, easier than "if (mcsel)"ing all the time */
# Line 332  Line 208 
208                          transfer_8to16sub(&dct_codes[3*64], cur->y + (16*j+8)*edged_width + 16*i+8,                          transfer_8to16sub(&dct_codes[3*64], cur->y + (16*j+8)*edged_width + 16*i+8,
209                                                                                           refGMC->y + (16*j+8)*edged_width + 16*i+8, edged_width);                                                                                           refGMC->y + (16*j+8)*edged_width + 16*i+8, edged_width);
210    
 /* lumi is needed earlier for mode decision, but chroma should be done block-based, but it isn't, yet. */  
   
211                          transfer_8to16sub(&dct_codes[4 * 64], cur->u + 8 *j*edged_width/2 + 8*i,                          transfer_8to16sub(&dct_codes[4 * 64], cur->u + 8 *j*edged_width/2 + 8*i,
212                                                                  refGMC->u + 8 *j*edged_width/2 + 8*i, edged_width/2);                                                                  refGMC->u + 8 *j*edged_width/2 + 8*i, edged_width/2);
213    
# Line 348  Line 222 
222                  dx = (quarterpel ? mb->qmvs[0].x : mb->mvs[0].x);                  dx = (quarterpel ? mb->qmvs[0].x : mb->mvs[0].x);
223                  dy = (quarterpel ? mb->qmvs[0].y : mb->mvs[0].y);                  dy = (quarterpel ? mb->qmvs[0].y : mb->mvs[0].y);
224    
                 if (reduced_resolution) {  
                         dx = RRV_MV_SCALEUP(dx);  
                         dy = RRV_MV_SCALEUP(dy);  
                 }  
   
225                  compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, ref->y, refh->y,                  compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, ref->y, refh->y,
226                                                          refv->y, refhv->y, tmp, 16 * i, 16 * j, dx, dy,                                                          refv->y, refhv->y, tmp, 16 * i, 16 * j, dx, dy,
227                                                          edged_width, quarterpel, reduced_resolution, rounding);                                                          edged_width, quarterpel, rounding);
228    
229                  dx /= (int)(1 + quarterpel);                  if (quarterpel) { dx /= 2; dy /= 2; }
                 dy /= (int)(1 + quarterpel);  
230    
231                  dx = (dx >> 1) + roundtab_79[dx & 0x3];                  dx = (dx >> 1) + roundtab_79[dx & 0x3];
232                  dy = (dy >> 1) + roundtab_79[dy & 0x3];                  dy = (dy >> 1) + roundtab_79[dy & 0x3];
233    
234          } else {                                        // mode == MODE_INTER4V          } else {                                        /* mode == MODE_INTER4V */
235                  int k, sumx = 0, sumy = 0;                  int k, sumx = 0, sumy = 0;
236                  const VECTOR * const mvs = (quarterpel ? mb->qmvs : mb->mvs);                  const VECTOR * const mvs = (quarterpel ? mb->qmvs : mb->mvs);
237    
238                  for (k = 0; k < 4; k++) {                  for (k = 0; k < 4; k++) {
239                          dx = mvs[k].x;                          dx = mvs[k].x;
240                          dy = mvs[k].y;                          dy = mvs[k].y;
241                          sumx += dx / (1 + quarterpel);                          sumx += quarterpel ? dx/2 : dx;
242                          sumy += dy / (1 + quarterpel);                          sumy += quarterpel ? dy/2 : dy;
   
                         if (reduced_resolution){  
                                 dx = RRV_MV_SCALEUP(dx);  
                                 dy = RRV_MV_SCALEUP(dy);  
                         }  
243    
244                          compensate8x8_interpolate(&dct_codes[k * 64], cur->y, ref->y, refh->y,                          compensate8x8_interpolate(&dct_codes[k * 64], cur->y, ref->y, refh->y,
245                                                                          refv->y, refhv->y, tmp, 16 * i + 8*(k&1), 16 * j + 8*(k>>1), dx,                                                                          refv->y, refhv->y, tmp, 16 * i + 8*(k&1), 16 * j + 8*(k>>1), dx,
246                                                                          dy, edged_width, quarterpel, reduced_resolution, rounding);                                                                          dy, edged_width, quarterpel, rounding);
247                  }                  }
248                  dx = (sumx >> 3) + roundtab_76[sumx & 0xf];                  dx = (sumx >> 3) + roundtab_76[sumx & 0xf];
249                  dy = (sumy >> 3) + roundtab_76[sumy & 0xf];                  dy = (sumy >> 3) + roundtab_76[sumy & 0xf];
250          }          }
251    
252          CompensateChroma(dx, dy, i, j, cur, ref, tmp,          CompensateChroma(dx, dy, i, j, cur, ref, tmp,
253                                          &dct_codes[4 * 64], edged_width / 2, rounding, reduced_resolution);                                          &dct_codes[4 * 64], edged_width / 2, rounding);
254  }  }
255    
256    
# Line 405  Line 268 
268                                                  const IMAGE * const b_refh,                                                  const IMAGE * const b_refh,
269                                                  const IMAGE * const b_refv,                                                  const IMAGE * const b_refv,
270                                                  const IMAGE * const b_refhv,                                                  const IMAGE * const b_refhv,
271                                                  int16_t * dct_codes)                                                  int16_t * dct_codes,
272                                                    uint8_t * const tmp)
273  {  {
274          const uint32_t edged_width = pParam->edged_width;          const uint32_t edged_width = pParam->edged_width;
275          int32_t dx, dy, b_dx, b_dy, sumx, sumy, b_sumx, b_sumy;          int32_t dx, dy, b_dx, b_dy, sumx, sumy, b_sumx, b_sumy;
276          int k;          int k;
277          const int quarterpel = pParam->m_quarterpel;          const int quarterpel = pParam->vol_flags & XVID_VOL_QUARTERPEL;
278          const uint8_t * ptr1, * ptr2;          const uint8_t * ptr1, * ptr2;
         uint8_t * const tmp = f_refv->u;  
279          const VECTOR * const fmvs = (quarterpel ? mb->qmvs : mb->mvs);          const VECTOR * const fmvs = (quarterpel ? mb->qmvs : mb->mvs);
280          const VECTOR * const bmvs = (quarterpel ? mb->b_qmvs : mb->b_mvs);          const VECTOR * const bmvs = (quarterpel ? mb->b_qmvs : mb->b_mvs);
281    
# Line 422  Line 285 
285    
286                  compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, f_ref->y, f_refh->y,                  compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, f_ref->y, f_refh->y,
287                                                          f_refv->y, f_refhv->y, tmp, 16 * i, 16 * j, dx,                                                          f_refv->y, f_refhv->y, tmp, 16 * i, 16 * j, dx,
288                                                          dy, edged_width, quarterpel, 0, 0);                                                          dy, edged_width, quarterpel, 0);
289    
290                  if (quarterpel) { dx /= 2; dy /= 2; }                  if (quarterpel) { dx /= 2; dy /= 2; }
291    
292                  CompensateChroma(       (dx >> 1) + roundtab_79[dx & 0x3],                  CompensateChroma(       (dx >> 1) + roundtab_79[dx & 0x3],
293                                                          (dy >> 1) + roundtab_79[dy & 0x3],                                                          (dy >> 1) + roundtab_79[dy & 0x3],
294                                                          i, j, cur, f_ref, tmp,                                                          i, j, cur, f_ref, tmp,
295                                                          &dct_codes[4 * 64], edged_width / 2, 0, 0);                                                          &dct_codes[4 * 64], edged_width / 2, 0);
296    
297                  return;                  return;
298    
299          case MODE_BACKWARD:          case MODE_BACKWARD:
300                  b_dx = bmvs->x; b_dy = bmvs->y;                  b_dx = bmvs->x; b_dy = bmvs->y;
301    
302                  compensate16x16_interpolate_ro(&dct_codes[0 * 64], cur->y, b_ref->y, b_refh->y,                  compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, b_ref->y, b_refh->y,
303                                                                                  b_refv->y, b_refhv->y, tmp, 16 * i, 16 * j, b_dx,                                                                                  b_refv->y, b_refhv->y, tmp, 16 * i, 16 * j, b_dx,
304                                                                                  b_dy, edged_width, quarterpel);                                                                                  b_dy, edged_width, quarterpel, 0);
305    
306                  if (quarterpel) { b_dx /= 2; b_dy /= 2; }                  if (quarterpel) { b_dx /= 2; b_dy /= 2; }
307    
308                  CompensateChroma(       (b_dx >> 1) + roundtab_79[b_dx & 0x3],                  CompensateChroma(       (b_dx >> 1) + roundtab_79[b_dx & 0x3],
309                                                          (b_dy >> 1) + roundtab_79[b_dy & 0x3],                                                          (b_dy >> 1) + roundtab_79[b_dy & 0x3],
310                                                          i, j, cur, b_ref, tmp,                                                          i, j, cur, b_ref, tmp,
311                                                          &dct_codes[4 * 64], edged_width / 2, 0, 0);                                                          &dct_codes[4 * 64], edged_width / 2, 0);
312    
313                  return;                  return;
314    
315          case MODE_INTERPOLATE: /* _could_ use DIRECT, but would be overkill (no 4MV there) */          case MODE_INTERPOLATE:
316          case MODE_DIRECT_NO4V:          case MODE_DIRECT_NO4V:
317                  dx = fmvs->x; dy = fmvs->y;                  dx = fmvs->x; dy = fmvs->y;
318                  b_dx = bmvs->x; b_dy = bmvs->y;                  b_dx = bmvs->x; b_dy = bmvs->y;
# Line 461  Line 324 
324                                          (uint8_t *) f_ref->y, tmp + 32,                                          (uint8_t *) f_ref->y, tmp + 32,
325                                          tmp + 64, tmp + 96, 16*i, 16*j, dx, dy, edged_width, 0);                                          tmp + 64, tmp + 96, 16*i, 16*j, dx, dy, edged_width, 0);
326                                  ptr1 = tmp;                                  ptr1 = tmp;
327                          } else ptr1 = f_ref->y + (16*j + dy/4)*edged_width + 16*i + dx/4; // fullpixel position                          } else ptr1 = f_ref->y + (16*(int)j + dy/4)*(int)edged_width + 16*(int)i + dx/4; /* fullpixel position */
328    
329                          if ((b_dx&3) | (b_dy&3)) {                          if ((b_dx&3) | (b_dy&3)) {
330                                  interpolate16x16_quarterpel(tmp - i * 16 - j * 16 * edged_width + 16,                                  interpolate16x16_quarterpel(tmp - i * 16 - j * 16 * edged_width + 16,
331                                          (uint8_t *) b_ref->y, tmp + 32,                                          (uint8_t *) b_ref->y, tmp + 32,
332                                          tmp + 64, tmp + 96, 16*i, 16*j, b_dx, b_dy, edged_width, 0);                                          tmp + 64, tmp + 96, 16*i, 16*j, b_dx, b_dy, edged_width, 0);
333                                  ptr2 = tmp + 16;                                  ptr2 = tmp + 16;
334                          } else ptr2 = b_ref->y + (16*j + b_dy/4)*edged_width + 16*i + b_dx/4; // fullpixel position                          } else ptr2 = b_ref->y + (16*(int)j + b_dy/4)*(int)edged_width + 16*(int)i + b_dx/4; /* fullpixel position */
335    
336                          b_dx /= 2;                          b_dx /= 2;
337                          b_dy /= 2;                          b_dy /= 2;
# Line 497  Line 360 
360    
361                  break;                  break;
362    
363          default: // MODE_DIRECT          default: /* MODE_DIRECT (or MODE_DIRECT_NONE_MV in case of bframes decoding) */
364                  sumx = sumy = b_sumx = b_sumy = 0;                  sumx = sumy = b_sumx = b_sumy = 0;
365    
366                  for (k = 0; k < 4; k++) {                  for (k = 0; k < 4; k++) {
# Line 515  Line 378 
378                                                  tmp + 32, tmp + 64, tmp + 96,                                                  tmp + 32, tmp + 64, tmp + 96,
379                                                  16*i + (k&1)*8, 16*j + (k>>1)*8, dx, dy, edged_width, 0);                                                  16*i + (k&1)*8, 16*j + (k>>1)*8, dx, dy, edged_width, 0);
380                                          ptr1 = tmp;                                          ptr1 = tmp;
381                                  } else ptr1 = f_ref->y + (16*j + (k>>1)*8 + dy/4)*edged_width + 16*i + (k&1)*8 + dx/4;                                  } else ptr1 = f_ref->y + (16*(int)j + (k>>1)*8 + dy/4)*(int)edged_width + 16*(int)i + (k&1)*8 + dx/4;
382    
383                                  if ((b_dx&3) | (b_dy&3)) {                                  if ((b_dx&3) | (b_dy&3)) {
384                                          interpolate8x8_quarterpel(tmp - (i * 16+(k&1)*8) - (j * 16+((k>>1)*8)) * edged_width + 16,                                          interpolate8x8_quarterpel(tmp - (i * 16+(k&1)*8) - (j * 16+((k>>1)*8)) * edged_width + 16,
# Line 523  Line 386 
386                                                  tmp + 16, tmp + 32, tmp + 48,                                                  tmp + 16, tmp + 32, tmp + 48,
387                                                  16*i + (k&1)*8, 16*j + (k>>1)*8, b_dx, b_dy, edged_width, 0);                                                  16*i + (k&1)*8, 16*j + (k>>1)*8, b_dx, b_dy, edged_width, 0);
388                                          ptr2 = tmp + 16;                                          ptr2 = tmp + 16;
389                                  } else ptr2 = b_ref->y + (16*j + (k>>1)*8 + b_dy/4)*edged_width + 16*i + (k&1)*8 + b_dx/4;                                  } else ptr2 = b_ref->y + (16*(int)j + (k>>1)*8 + b_dy/4)*(int)edged_width + 16*(int)i + (k&1)*8 + b_dx/4;
390                          } else {                          } else {
391                                  sumx += dx; sumy += dy;                                  sumx += dx; sumy += dy;
392                                  b_sumx += b_dx; b_sumy += b_dy;                                  b_sumx += b_dx; b_sumy += b_dy;
# Line 547  Line 410 
410                  break;                  break;
411          }          }
412    
413          // uv block-based chroma interpolation for direct and interpolate modes          /* block-based chroma interpolation for direct and interpolate modes */
414          transfer_8to16sub2(&dct_codes[4 * 64],          transfer_8to16sub2(&dct_codes[4 * 64],
415                                                  cur->u + (j * 8) * edged_width / 2 + (i * 8),                                                  cur->u + (j * 8) * edged_width / 2 + (i * 8),
416                                                  interpolate8x8_switch2(tmp, b_ref->u, 8 * i, 8 * j,                                                  interpolate8x8_switch2(tmp, b_ref->u, 8 * i, 8 * j,
# Line 564  Line 427 
427                                                                                                  dx, dy, edged_width / 2, 0),                                                                                                  dx, dy, edged_width / 2, 0),
428                                                  edged_width / 2);                                                  edged_width / 2);
429  }  }
   
   
   
 void generate_GMCparameters( const int num_wp, const int res,  
                        const WARPPOINTS *const warp,  
                        const int width, const int height,  
                        GMC_DATA *const gmc)  
 {  
   const int du0 = warp->duv[0].x;  
   const int dv0 = warp->duv[0].y;  
   const int du1 = warp->duv[1].x;  
   const int dv1 = warp->duv[1].y;  
   const int du2 = warp->duv[2].x;  
   const int dv2 = warp->duv[2].y;  
   
   gmc->W = width;  
   gmc->H = height;  
   
   gmc->rho = 4 - log2bin(res-1);  // = {3,2,1,0} for res={2,4,8,16}  
   
   gmc->alpha = log2bin(gmc->W-1);  
   gmc->Ws = (1 << gmc->alpha);  
   
   gmc->dxF = 16*gmc->Ws + RDIV( 8*gmc->Ws*du1, gmc->W );  
   gmc->dxG =              RDIV( 8*gmc->Ws*dv1, gmc->W );  
   gmc->Fo  = (res*du0 + 1) << (gmc->alpha+gmc->rho-1);  
   gmc->Go  = (res*dv0 + 1) << (gmc->alpha+gmc->rho-1);  
   
   if (num_wp==2) {  
     gmc->dyF = -gmc->dxG;  
     gmc->dyG =  gmc->dxF;  
   }  
   else if (num_wp==3) {  
     gmc->beta = log2bin(gmc->H-1);  
     gmc->Hs = (1 << gmc->beta);  
     gmc->dyF =              RDIV( 8*gmc->Hs*du2, gmc->H );  
     gmc->dyG = 16*gmc->Hs + RDIV( 8*gmc->Hs*dv2, gmc->H );  
     if (gmc->beta > gmc->alpha) {  
       gmc->dxF <<= (gmc->beta - gmc->alpha);  
       gmc->dxG <<= (gmc->beta - gmc->alpha);  
       gmc->alpha = gmc->beta;  
       gmc->Ws = 1<< gmc->beta;  
     }  
     else {  
       gmc->dyF <<= gmc->alpha - gmc->beta;  
       gmc->dyG <<= gmc->alpha - gmc->beta;  
     }  
   }  
   
   gmc->cFo = gmc->dxF + gmc->dyF + (1 << (gmc->alpha+gmc->rho+1));  
   gmc->cFo += 16*gmc->Ws*(du0-1);  
   
   gmc->cGo = gmc->dxG + gmc->dyG + (1 << (gmc->alpha+gmc->rho+1));  
   gmc->cGo += 16*gmc->Ws*(dv0-1);  
 }  
   
 void  
 generate_GMCimage(      const GMC_DATA *const gmc_data,         // [input] precalculated data  
                                         const IMAGE *const pRef,                        // [input]  
                                         const int mb_width,  
                                         const int mb_height,  
                                         const int stride,  
                                         const int stride2,  
                                         const int fcode,                                        // [input] some parameters...  
                                         const int32_t quarterpel,                       // [input] for rounding avgMV  
                                         const int reduced_resolution,           // [input] ignored  
                                         const int32_t rounding,                 // [input] for rounding image data  
                                         MACROBLOCK *const pMBs,         // [output] average motion vectors  
                                         IMAGE *const pGMC)                      // [output] full warped image  
 {  
   
         unsigned int mj,mi;  
         VECTOR avgMV;  
   
         for (mj=0;mj<mb_height;mj++)  
         for (mi=0;mi<mb_width; mi++)  
         {  
                 avgMV = generate_GMCimageMB(gmc_data, pRef, mi, mj,  
                                         stride, stride2, quarterpel, rounding, pGMC);  
   
                 pMBs[mj*mb_width+mi].amv.x = gmc_sanitize(avgMV.x, quarterpel, fcode);  
                 pMBs[mj*mb_width+mi].amv.y = gmc_sanitize(avgMV.y, quarterpel, fcode);  
                 pMBs[mj*mb_width+mi].mcsel = 0; /* until mode decision */  
         }  
 }  
   
   
   
 #define MLT(i)  (((16-(i))<<16) + (i))  
 static const uint32_t MTab[16] = {  
   MLT( 0), MLT( 1), MLT( 2), MLT( 3), MLT( 4), MLT( 5), MLT( 6), MLT(7),  
   MLT( 8), MLT( 9), MLT(10), MLT(11), MLT(12), MLT(13), MLT(14), MLT(15)  
 };  
 #undef MLT  
   
 VECTOR generate_GMCimageMB( const GMC_DATA *const gmc_data,  
                       const IMAGE *const pRef,  
                       const int mi, const int mj,  
                       const int stride,  
                       const int stride2,  
                       const int quarterpel,  
                       const int rounding,  
                       IMAGE *const pGMC)  
 {  
   const int W = gmc_data->W;  
   const int H = gmc_data->H;  
   
   const int rho = gmc_data->rho;  
   const int alpha = gmc_data->alpha;  
   
   const int rounder = ( 128 - (rounding<<(rho+rho)) ) << 16;  
   
   const int dxF = gmc_data->dxF;  
   const int dyF = gmc_data->dyF;  
   const int dxG = gmc_data->dxG;  
   const int dyG = gmc_data->dyG;  
   
   uint8_t *dstY, *dstU, *dstV;  
   
   int I,J;  
   VECTOR avgMV = {0,0};  
   
   int32_t Fj, Gj;  
   
   dstY = &pGMC->y[(mj*16)*stride+mi*16] + 16;  
   
   Fj = gmc_data->Fo + dyF*mj*16 + dxF*mi*16;  
   Gj = gmc_data->Go + dyG*mj*16 + dxG*mi*16;  
   for (J=16; J>0; --J)  
   {  
     int32_t Fi, Gi;  
   
     Fi = Fj; Fj += dyF;  
     Gi = Gj; Gj += dyG;  
     for (I=-16; I<0; ++I)  
     {  
       int32_t F, G;  
       uint32_t ri, rj;  
   
       F = ( Fi >> (alpha+rho) ) << rho; Fi += dxF;  
       G = ( Gi >> (alpha+rho) ) << rho; Gi += dxG;  
   
       avgMV.x += F;  
       avgMV.y += G;  
   
       ri = MTab[F&15];  
       rj = MTab[G&15];  
   
       F >>= 4;  
       G >>= 4;  
   
       if (F< -1) F=-1;  
       else if (F>W) F=W;  
       if (G< -1) G=-1;  
       else if (G>H) G=H;  
   
       {     // MMX-like bilinear...  
         const int offset = G*stride + F;  
         uint32_t f0, f1;  
         f0  = pRef->y[ offset +0 ];  
         f0 |= pRef->y[ offset +1 ] << 16;  
         f1  = pRef->y[ offset+stride +0 ];  
         f1 |= pRef->y[ offset+stride +1 ] << 16;  
         f0 = (ri*f0)>>16;  
         f1 = (ri*f1) & 0x0fff0000;  
         f0 |= f1;  
         f0 = ( rj*f0 + rounder ) >> 24;  
   
         dstY[I] = (uint8_t)f0;  
       }  
     }  
     dstY += stride;  
   }  
   
   dstU = &pGMC->u[(mj*8)*stride2+mi*8] + 8;  
   dstV = &pGMC->v[(mj*8)*stride2+mi*8] + 8;  
   
   Fj = gmc_data->cFo + dyF*4 *mj*8 + dxF*4 *mi*8;  
   Gj = gmc_data->cGo + dyG*4 *mj*8 + dxG*4 *mi*8;  
   for (J=8; J>0; --J)  
   {  
     int32_t Fi, Gi;  
     Fi = Fj; Fj += 4*dyF;  
     Gi = Gj; Gj += 4*dyG;  
   
     for (I=-8; I<0; ++I)  
     {  
       int32_t F, G;  
       uint32_t ri, rj;  
   
       F = ( Fi >> (alpha+rho+2) ) << rho; Fi += 4*dxF;  
       G = ( Gi >> (alpha+rho+2) ) << rho; Gi += 4*dxG;  
   
       ri = MTab[F&15];  
       rj = MTab[G&15];  
   
       F >>= 4;  
       G >>= 4;  
   
       if (F< -1) F=-1;  
       else if (F>=W/2) F=W/2;  
       if (G< -1) G=-1;  
       else if (G>=H/2) G=H/2;  
   
       {  
         const int offset = G*stride2 + F;  
         uint32_t f0, f1;  
   
         f0  = pRef->u[ offset         +0 ];  
         f0 |= pRef->u[ offset         +1 ] << 16;  
         f1  = pRef->u[ offset+stride2 +0 ];  
         f1 |= pRef->u[ offset+stride2 +1 ] << 16;  
         f0 = (ri*f0)>>16;  
         f1 = (ri*f1) & 0x0fff0000;  
         f0 |= f1;  
         f0 = ( rj*f0 + rounder ) >> 24;  
   
         dstU[I] = (uint8_t)f0;  
   
   
         f0  = pRef->v[ offset         +0 ];  
         f0 |= pRef->v[ offset         +1 ] << 16;  
         f1  = pRef->v[ offset+stride2 +0 ];  
         f1 |= pRef->v[ offset+stride2 +1 ] << 16;  
         f0 = (ri*f0)>>16;  
         f1 = (ri*f1) & 0x0fff0000;  
         f0 |= f1;  
         f0 = ( rj*f0 + rounder ) >> 24;  
   
         dstV[I] = (uint8_t)f0;  
       }  
     }  
     dstU += stride2;  
     dstV += stride2;  
   }  
   
   
   avgMV.x -= 16*((256*mi+120)<<4);    // 120 = 15*16/2  
   avgMV.y -= 16*((256*mj+120)<<4);  
   
   avgMV.x = RSHIFT( avgMV.x, (4+7-quarterpel) );  
   avgMV.y = RSHIFT( avgMV.y, (4+7-quarterpel) );  
   
   return avgMV;  
 }  
   
   
   
 #ifdef OLD_GRUEL_GMC  
 void  
 generate_GMCparameters( const int num_wp,                       // [input]: number of warppoints  
                                                 const int res,                  // [input]: resolution  
                                                 const WARPPOINTS *const warp, // [input]: warp points  
                                                 const int width, const int height,  
                                                 GMC_DATA *const gmc)    // [output] precalculated parameters  
 {  
   
 /* We follow mainly two sources: The original standard, which is ugly, and the  
    thesis from Andreas Dehnhardt, which is much nicer.  
   
         Notation is: indices are written next to the variable,  
                                  primes in the standard are denoted by a suffix 'p'.  
         types are   "c"=constant, "i"=input parameter, "f"=calculated, then fixed,  
                 "o"=output data, " "=other, "u" = unused, "p"=calc for every pixel  
   
 type | variable name  |   ISO name (TeX-style) |  value or range  |  usage  
 -------------------------------------------------------------------------------------  
  c   | H                          |   H                                    |  [16 , ?]            |  image width (w/o edges)  
  c   | W                          |   W                                    |  [16 , ?]            |  image height (w/o edges)  
   
  c   | i0                         |   i_0                                  |  0                           |  ref. point #1, X  
  c   | j0                         |   j_0                                  |  0                           |  ref. point #1, Y  
  c   | i1                         |   i_1                                  |  W                           |  ref. point #2, X  
  c   | j1                         |   j_1                                  |  0                           |  ref. point #2, Y  
  cu  | i2                         |   i_2                                  |  0                           |  ref. point #3, X  
  cu  | i2                         |   j_2                                  |  H                           |  ref. point #3, Y  
   
  i   | du0                |   du[0]                        |  [-16863,16863]  |  warp vector #1, Y  
  i   | dv0                |   dv[0]                        |  [-16863,16863]  |  warp vector #1, Y  
  i   | du1                |   du[1]                        |  [-16863,16863]  |  warp vector #2, Y  
  i   | dv1                |   dv[1]                        |  [-16863,16863]  |  warp vector #2, Y  
  iu  | du2                |   du[2]                        |  [-16863,16863]  |  warp vector #3, Y  
  iu  | dv2                |   dv[2]                        |  [-16863,16863]  |  warp vector #3, Y  
   
  i   | s              |   s                    |  {2,4,8,16}      |  interpol. resolution  
  f   | sigma          |        -               |  log2(s)         |  X / s == X >> sigma  
  f   | r              |   r                    |  =16/s           |  complementary res.  
  f   | rho            |   \rho                 |  log2(r)         |  X / r == X >> rho  
   
  f   | i0s            |   i'_0                 |                  |  
  f   | j0s            |   j'_0                 |                  |  
  f       | i1s            |   i'_1                 |                  |  
  f       | j1s            |   j'_1                 |                  |  
  f       | i2s            |   i'_2                 |                  |  
  f       | j2s            |   j'_2                 |                  |  
   
  f   | alpha          |   \alpha               |                  |  2^{alpha-1} < W <= 2^alpha  
  f   | beta           |   \beta                |                  |  2^{beta-1} < H <= 2^beta  
   
  f   | Ws             |   W'                   | W = 2^{alpha}    |  scaled width  
  f   | Hs             |   H'                   | W = 2^{beta}     |  scaled height  
   
  f   | i1ss           |   i''_1                |  "virtual sprite stuff"  
  f   | j1ss           |   j''_1                |  "virtual sprite stuff"  
  f   | i2ss           |   i''_2                |  "virtual sprite stuff"  
  f   | j2ss           |   j''_2                |  "virtual sprite stuff"  
 */  
   
 /* Some calculations are disabled because we only use 2 warppoints at the moment */  
   
         int du0 = warp->duv[0].x;  
         int dv0 = warp->duv[0].y;  
         int du1 = warp->duv[1].x;  
         int dv1 = warp->duv[1].y;  
 //      int du2 = warp->duv[2].x;  
 //      int dv2 = warp->duv[2].y;  
   
         gmc->num_wp = num_wp;  
   
         gmc->s = res;                                           /* scaling parameters 2,4,8 or 16 */  
         gmc->sigma = log2bin(res-1);    /* log2bin(15)=4, log2bin(16)=5, log2bin(17)=5  */  
         gmc->r = 16/res;  
         gmc->rho = 4 - gmc->sigma;              /* = log2bin(r-1) */  
   
         gmc->W = width;  
         gmc->H = height;                        /* fixed reference coordinates */  
   
         gmc->alpha = log2bin(gmc->W-1);  
         gmc->Ws= 1<<gmc->alpha;  
   
 //      gmc->beta = log2bin(gmc->H-1);  
 //      gmc->Hs= 1<<gmc->beta;  
   
 //      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);  
   
         /* i2s is only needed for num_wp >= 3, etc.  */  
         /* the 's' values are in 1/s pel resolution */  
         gmc->i0s = res/2 * ( du0 );  
         gmc->j0s = res/2 * ( dv0 );  
         gmc->i1s = res/2 * (2*width + du1 + du0 );  
         gmc->j1s = res/2 * ( dv1 + dv0 );  
 //      gmc->i2s = res/2 * ( du2 + du0 );  
 //      gmc->j2s = res/2 * (2*height + dv2 + dv0 );  
   
         /* i2s and i2ss are only needed for num_wp == 3, etc.  */  
   
         /* the 'ss' values are in 1/16 pel resolution */  
         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);  
         gmc->j1ss = ROUNDED_DIV( ((gmc->W - gmc->Ws)*(gmc->r*gmc->j0s) + gmc->Ws*gmc->r*gmc->j1s) ,gmc->W );  
   
 //      gmc->i2ss = ROUNDED_DIV( ((gmc->H - gmc->Hs)*(gmc->r*gmc->i0s) + gmc->Hs*(gmc->r*gmc->i2s)), gmc->H);  
 //      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);  
   
         return;  
 }  
   
   
   
 void  
 generate_GMCimage(      const GMC_DATA *const gmc_data,         // [input] precalculated data  
                                         const IMAGE *const pRef,                        // [input]  
                                         const int mb_width,  
                                         const int mb_height,  
                                         const int stride,  
                                         const int stride2,  
                                         const int fcode,                                        // [input] some parameters...  
                                         const int32_t quarterpel,                       // [input] for rounding avgMV  
                                         const int reduced_resolution,           // [input] ignored  
                                         const int32_t rounding,                 // [input] for rounding image data  
                                         MACROBLOCK *const pMBs,         // [output] average motion vectors  
                                         IMAGE *const pGMC)                      // [output] full warped image  
 {  
   
         unsigned int mj,mi;  
         VECTOR avgMV;  
   
         for (mj=0;mj<mb_height;mj++)  
         for (mi=0;mi<mb_width; mi++)  
         {  
                 avgMV = generate_GMCimageMB(gmc_data, pRef, mi, mj,  
                                         stride, stride2, quarterpel, rounding, pGMC);  
   
                 pMBs[mj*mb_width+mi].amv.x = gmc_sanitize(avgMV.x, quarterpel, fcode);  
                 pMBs[mj*mb_width+mi].amv.y = gmc_sanitize(avgMV.y, quarterpel, fcode);  
                 pMBs[mj*mb_width+mi].mcsel = 0; /* until mode decision */  
         }  
 }  
   
   
 VECTOR generate_GMCimageMB(     const GMC_DATA *const gmc_data, /* [input] all precalc data */  
                                                         const IMAGE *const pRef,                /* [input] */  
                                                         const int mi, const int mj,     /* [input] MB position  */  
                                                         const int stride,                               /* [input] Lumi stride */  
                                                         const int stride2,                              /* [input] chroma stride */  
                                                         const int quarterpel,                   /* [input] for rounding of avgMV */  
                                                         const int rounding,                     /* [input] for rounding of imgae data */  
                                                         IMAGE *const pGMC)                              /* [outut] generate image */  
   
 /*  
 type | variable name  |   ISO name (TeX-style) |  value or range  |  usage  
 -------------------------------------------------------------------------------------  
  p   | F              |   F(i,j)               |                  | pelwise motion vector X in s-th pel  
  p   | G              |   G(i,j)               |                  | pelwise motion vector Y in s-th pel  
  p   | Fc             |   F_c(i,j)             |                  |  
  p   | Gc             |   G_c(i,j)             |                  | same for chroma  
   
  p   | Y00            |   Y_{00}               |  [0,255*s*s]     | first: 4 neighbouring Y-values  
  p   | Y01            |   Y_{01}               |  [0,255]         | at fullpel position, around the  
  p   | Y10            |   Y_{10}               |  [0,255*s]       | position where pelweise MV points to  
  p   | Y11            |   Y_{11}               |  [0,255]         | later: bilinear interpol Y-values in Y00  
   
  p   | C00            |   C_{00}               |  [0,255*s*s]     | same for chroma Cb and Cr  
  p   | C01            |   C_{01}               |  [0,255]         |  
  p   | C10            |   C_{10}               |  [0,255*s]       |  
  p   | C11            |   C_{11}               |  [0,255]         |  
   
 */  
 {  
         const int W = gmc_data->W;  
         const int H = gmc_data->H;  
   
         const int s = gmc_data->s;  
         const int sigma = gmc_data->sigma;  
   
         const int r = gmc_data->r;  
         const int rho = gmc_data->rho;  
   
         const int i0s = gmc_data->i0s;  
         const int j0s = gmc_data->j0s;  
   
         const int i1ss = gmc_data->i1ss;  
         const int j1ss = gmc_data->j1ss;  
 //      const int i2ss = gmc_data->i2ss;  
 //      const int j2ss = gmc_data->j2ss;  
   
         const int alpha = gmc_data->alpha;  
         const int Ws    = gmc_data->Ws;  
   
 //      const int beta  = gmc_data->beta;  
 //      const int Hs    = gmc_data->Hs;  
   
         int I,J;  
         VECTOR avgMV = {0,0};  
   
         for (J=16*mj;J<16*(mj+1);J++)  
         for (I=16*mi;I<16*(mi+1);I++)  
         {  
                 int F= i0s + ( ((-r*i0s+i1ss)*I + (r*j0s-j1ss)*J + (1<<(alpha+rho-1))) >>  (alpha+rho) );  
                 int G= j0s + ( ((-r*j0s+j1ss)*I + (-r*i0s+i1ss)*J + (1<<(alpha+rho-1))) >> (alpha+rho) );  
   
 /* this naive implementation (with lots of multiplications) isn't slower (rather faster) than  
    working incremental. Don't ask me why... maybe the whole this is memory bound? */  
   
                 const int ri= F & (s-1); // fractional part of pelwise MV X  
                 const int rj= G & (s-1); // fractional part of pelwise MV Y  
   
                 int Y00,Y01,Y10,Y11;  
   
 /* unclipped values are used for avgMV */  
                 avgMV.x += F-(I<<sigma);                /* shift position to 1/s-pel, as the MV is */  
                 avgMV.y += G-(J<<sigma);                /* TODO: don't do this (of course) */  
   
                 F >>= sigma;  
                 G >>= sigma;  
   
 /* clip values to be in range. Since we have edges, clip to 1 less than lower boundary  
    this way positions F+1/G+1 are still right */  
   
                 if (F< -1)  
                         F=-1;  
                 else if (F>W)  
                         F=W;    /* W or W-1 doesn't matter, so save 1 subtract ;-) */  
                 if (G< -1)  
                         G=-1;  
                 else if (G>H)  
                         G=H;            /* dito */  
   
                 Y00 = pRef->y[ G*stride + F ];                          // Lumi values  
                 Y01 = pRef->y[ G*stride + F+1 ];  
                 Y10 = pRef->y[ G*stride + F+stride ];  
                 Y11 = pRef->y[ G*stride + F+stride+1 ];  
   
                 /* bilinear interpolation */  
                 Y00 = ((s-ri)*Y00 + ri*Y01);  
                 Y10 = ((s-ri)*Y10 + ri*Y11);  
                 Y00 = ((s-rj)*Y00 + rj*Y10 + s*s/2 - rounding ) >> (sigma+sigma);  
   
                 pGMC->y[J*stride+I] = (uint8_t)Y00;                                                                             /* output 1 Y-pixel */  
         }  
   
   
 /* doing chroma _here_ is even more stupid and slow, because won't be used until Compensation and  
         most likely not even then (only if the block really _is_ GMC)  
 */  
   
         for (J=8*mj;J<8*(mj+1);J++)             /* this plays the role of j_c,i_c in the standard */  
         for (I=8*mi;I<8*(mi+1);I++)             /* For I_c we have to use I_c = 4*i_c+1 ! */  
         {  
                 /* same positions for both chroma components, U=Cb and V=Cr */  
                 int Fc=((-r*i0s+i1ss)*(4*I+1) + (r*j0s-j1ss)*(4*J+1) +2*Ws*r*i0s  
                                                 -16*Ws +(1<<(alpha+rho+1)))>>(alpha+rho+2);  
                 int Gc=((-r*j0s+j1ss)*(4*I+1) +(-r*i0s+i1ss)*(4*J+1) +2*Ws*r*j0s  
                                                 -16*Ws +(1<<(alpha+rho+1))) >>(alpha+rho+2);  
   
                 const int ri= Fc & (s-1); // fractional part of pelwise MV X  
                 const int rj= Gc & (s-1); // fractional part of pelwise MV Y  
   
                 int C00,C01,C10,C11;  
   
                 Fc >>= sigma;  
                 Gc >>= sigma;  
   
                 if (Fc< -1)  
                         Fc=-1;  
                 else if (Fc>=W/2)  
                         Fc=W/2;         /* W or W-1 doesn't matter, so save 1 subtraction ;-) */  
                 if (Gc< -1)  
                         Gc=-1;  
                 else if (Gc>=H/2)  
                         Gc=H/2;         /* dito */  
   
 /* now calculate U data */  
                 C00 = pRef->u[ Gc*stride2 + Fc ];                               // chroma-value Cb  
                 C01 = pRef->u[ Gc*stride2 + Fc+1 ];  
                 C10 = pRef->u[ (Gc+1)*stride2 + Fc ];  
                 C11 = pRef->u[ (Gc+1)*stride2 + Fc+1 ];  
   
                 /* bilinear interpolation */  
                 C00 = ((s-ri)*C00 + ri*C01);  
                 C10 = ((s-ri)*C10 + ri*C11);  
                 C00 = ((s-rj)*C00 + rj*C10 + s*s/2 - rounding ) >> (sigma+sigma);  
   
                 pGMC->u[J*stride2+I] = (uint8_t)C00;                                                                            /* output 1 U-pixel */  
   
 /* now calculate V data */  
                 C00 = pRef->v[ Gc*stride2 + Fc ];                               // chroma-value Cr  
                 C01 = pRef->v[ Gc*stride2 + Fc+1 ];  
                 C10 = pRef->v[ (Gc+1)*stride2 + Fc ];  
                 C11 = pRef->v[ (Gc+1)*stride2 + Fc+1 ];  
   
                 /* bilinear interpolation */  
                 C00 = ((s-ri)*C00 + ri*C01);  
                 C10 = ((s-ri)*C10 + ri*C11);  
                 C00 = ((s-rj)*C00 + rj*C10 + s*s/2 - rounding ) >> (sigma+sigma);  
   
                 pGMC->v[J*stride2+I] = (uint8_t)C00;                                                                            /* output 1 V-pixel */  
         }  
   
 /* The average vector is rounded from 1/s-pel to 1/2 or 1/4 using the '//' operator*/  
   
         avgMV.x = RSHIFT( avgMV.x, (sigma+7-quarterpel) );  
         avgMV.y = RSHIFT( avgMV.y, (sigma+7-quarterpel) );  
   
         /* ^^^^ this is the way MS Reference Software does it */  
   
         return avgMV;   /* clipping to fcode area is done outside! */  
 }  
   
 #endif  

Legend:
Removed from v.1.11.2.23  
changed lines
  Added in v.1.25

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