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

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

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

revision 949, Wed Mar 26 14:56:49 2003 UTC revision 1053, Mon Jun 9 01:25:19 2003 UTC
# Line 31  Line 31 
31  #include <assert.h>  #include <assert.h>
32  #include <stdio.h>  #include <stdio.h>
33  #include <stdlib.h>  #include <stdlib.h>
34  #include <string.h>     // memcpy  #include <string.h>     /* memcpy */
35  #include <math.h>       // lrint  #include <math.h>       /* lrint */
36    
37  #include "../encoder.h"  #include "../encoder.h"
38  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
# Line 81  Line 81 
81  static __inline uint32_t  static __inline uint32_t
82  d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv)  d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv)
83  {  {
84          int xb, yb;          int bits;
85          x = qpel ? x<<1 : x;          const int q = (1 << (iFcode - 1)) - 1;
86          y = qpel ? y<<1 : y;  
87            x <<= qpel;
88            y <<= qpel;
89          if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); }          if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); }
90    
91          x -= pred.x;          x -= pred.x;
92          y -= pred.y;          bits = (x != 0 ? iFcode:0);
93            x = abs(x);
94          if (x) {          x += q;
                 x = ABS(x);  
                 x += (1 << (iFcode - 1)) - 1;  
95                  x >>= (iFcode - 1);                  x >>= (iFcode - 1);
96                  if (x > 32) x = 32;          bits += mvtab[x];
97                  xb = mvtab[x] + iFcode;  
98          } else xb = 1;          y -= pred.y;
99            bits += (y != 0 ? iFcode:0);
100          if (y) {          y = abs(y);
101                  y = ABS(y);          y += q;
                 y += (1 << (iFcode - 1)) - 1;  
102                  y >>= (iFcode - 1);                  y >>= (iFcode - 1);
103                  if (y > 32) y = 32;          bits += mvtab[y];
104                  yb = mvtab[y] + iFcode;  
105          } else yb = 1;          return bits;
         return xb + yb;  
106  }  }
107    
108  static int32_t ChromaSAD2(int fx, int fy, int bx, int by, const SearchData * const data)  static int32_t ChromaSAD2(const int fx, const int fy, const int bx, const int by,
109                                                            const SearchData * const data)
110  {  {
111          int sad;          int sad;
112          const uint32_t stride = data->iEdgedWidth/2;          const uint32_t stride = data->iEdgedWidth/2;
# Line 115  Line 114 
114                  * f_refv = data->RefQ + 8,                  * f_refv = data->RefQ + 8,
115                  * b_refu = data->RefQ + 16,                  * b_refu = data->RefQ + 16,
116                  * b_refv = data->RefQ + 24;                  * b_refv = data->RefQ + 24;
117            int offset = (fx>>1) + (fy>>1)*stride;
118    
119          switch (((fx & 1) << 1) | (fy & 1))     {          switch (((fx & 1) << 1) | (fy & 1))     {
120                  case 0:                  case 0:
121                          fx = fx / 2; fy = fy / 2;                          f_refu = (uint8_t*)data->RefP[4] + offset;
122                          f_refu = (uint8_t*)data->RefCU + fy * stride + fx, stride;                          f_refv = (uint8_t*)data->RefP[5] + offset;
                         f_refv = (uint8_t*)data->RefCV + fy * stride + fx, stride;  
123                          break;                          break;
124                  case 1:                  case 1:
125                          fx = fx / 2; fy = (fy - 1) / 2;                          interpolate8x8_halfpel_v(f_refu, data->RefP[4] + offset, stride, data->rounding);
126                          interpolate8x8_halfpel_v(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding);                          interpolate8x8_halfpel_v(f_refv, data->RefP[5] + offset, stride, data->rounding);
                         interpolate8x8_halfpel_v(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding);  
127                          break;                          break;
128                  case 2:                  case 2:
129                          fx = (fx - 1) / 2; fy = fy / 2;                          interpolate8x8_halfpel_h(f_refu, data->RefP[4] + offset, stride, data->rounding);
130                          interpolate8x8_halfpel_h(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding);                          interpolate8x8_halfpel_h(f_refv, data->RefP[5] + offset, stride, data->rounding);
                         interpolate8x8_halfpel_h(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding);  
131                          break;                          break;
132                  default:                  default:
133                          fx = (fx - 1) / 2; fy = (fy - 1) / 2;                          interpolate8x8_halfpel_hv(f_refu, data->RefP[4] + offset, stride, data->rounding);
134                          interpolate8x8_halfpel_hv(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding);                          interpolate8x8_halfpel_hv(f_refv, data->RefP[5] + offset, stride, data->rounding);
                         interpolate8x8_halfpel_hv(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding);  
135                          break;                          break;
136          }          }
137    
138            offset = (bx>>1) + (by>>1)*stride;
139          switch (((bx & 1) << 1) | (by & 1))     {          switch (((bx & 1) << 1) | (by & 1))     {
140                  case 0:                  case 0:
141                          bx = bx / 2; by = by / 2;                          b_refu = (uint8_t*)data->b_RefP[4] + offset;
142                          b_refu = (uint8_t*)data->b_RefCU + by * stride + bx, stride;                          b_refv = (uint8_t*)data->b_RefP[5] + offset;
                         b_refv = (uint8_t*)data->b_RefCV + by * stride + bx, stride;  
143                          break;                          break;
144                  case 1:                  case 1:
145                          bx = bx / 2; by = (by - 1) / 2;                          interpolate8x8_halfpel_v(b_refu, data->b_RefP[4] + offset, stride, data->rounding);
146                          interpolate8x8_halfpel_v(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding);                          interpolate8x8_halfpel_v(b_refv, data->b_RefP[5] + offset, stride, data->rounding);
                         interpolate8x8_halfpel_v(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding);  
147                          break;                          break;
148                  case 2:                  case 2:
149                          bx = (bx - 1) / 2; by = by / 2;                          interpolate8x8_halfpel_h(b_refu, data->b_RefP[4] + offset, stride, data->rounding);
150                          interpolate8x8_halfpel_h(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding);                          interpolate8x8_halfpel_h(b_refv, data->b_RefP[5] + offset, stride, data->rounding);
                         interpolate8x8_halfpel_h(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding);  
151                          break;                          break;
152                  default:                  default:
153                          bx = (bx - 1) / 2; by = (by - 1) / 2;                          interpolate8x8_halfpel_hv(b_refu, data->b_RefP[4] + offset, stride, data->rounding);
154                          interpolate8x8_halfpel_hv(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding);                          interpolate8x8_halfpel_hv(b_refv, data->b_RefP[5] + offset, stride, data->rounding);
                         interpolate8x8_halfpel_hv(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding);  
155                          break;                          break;
156          }          }
157    
# Line 168  Line 161 
161          return sad;          return sad;
162  }  }
163    
   
164  static int32_t  static int32_t
165  ChromaSAD(int dx, int dy, const SearchData * const data)  ChromaSAD(const int dx, const int dy, const SearchData * const data)
166  {  {
167          int sad;          int sad;
168          const uint32_t stride = data->iEdgedWidth/2;          const uint32_t stride = data->iEdgedWidth/2;
169            int offset = (dx>>1) + (dy>>1)*stride;
170    
171          if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; //it has been checked recently          if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; /* it has been checked recently */
172          data->temp[5] = dx; data->temp[6] = dy; // backup          data->temp[5] = dx; data->temp[6] = dy; /* backup */
173    
174          switch (((dx & 1) << 1) | (dy & 1))     {          switch (((dx & 1) << 1) | (dy & 1))     {
175                  case 0:                  case 0:
176                          dx = dx / 2; dy = dy / 2;                          sad = sad8(data->CurU, data->RefP[4] + offset, stride);
177                          sad = sad8(data->CurU, data->RefCU + dy * stride + dx, stride);                          sad += sad8(data->CurV, data->RefP[5] + offset, stride);
                         sad += sad8(data->CurV, data->RefCV + dy * stride + dx, stride);  
178                          break;                          break;
179                  case 1:                  case 1:
180                          dx = dx / 2; dy = (dy - 1) / 2;                          sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + stride, stride);
181                          sad = sad8bi(data->CurU, data->RefCU + dy * stride + dx, data->RefCU + (dy+1) * stride + dx, stride);                          sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + stride, stride);
                         sad += sad8bi(data->CurV, data->RefCV + dy * stride + dx, data->RefCV + (dy+1) * stride + dx, stride);  
182                          break;                          break;
183                  case 2:                  case 2:
184                          dx = (dx - 1) / 2; dy = dy / 2;                          sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + 1, stride);
185                          sad = sad8bi(data->CurU, data->RefCU + dy * stride + dx, data->RefCU + dy * stride + dx+1, stride);                          sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + 1, stride);
                         sad += sad8bi(data->CurV, data->RefCV + dy * stride + dx, data->RefCV + dy * stride + dx+1, stride);  
186                          break;                          break;
187                  default:                  default:
188                          dx = (dx - 1) / 2; dy = (dy - 1) / 2;                          interpolate8x8_halfpel_hv(data->RefQ, data->RefP[4] + offset, stride, data->rounding);
                         interpolate8x8_halfpel_hv(data->RefQ, data->RefCU + dy * stride + dx, stride, data->rounding);  
189                          sad = sad8(data->CurU, data->RefQ, stride);                          sad = sad8(data->CurU, data->RefQ, stride);
190    
191                          interpolate8x8_halfpel_hv(data->RefQ, data->RefCV + dy * stride + dx, stride, data->rounding);                          interpolate8x8_halfpel_hv(data->RefQ, data->RefP[5] + offset, stride, data->rounding);
192                          sad += sad8(data->CurV, data->RefQ, stride);                          sad += sad8(data->CurV, data->RefQ, stride);
193                          break;                          break;
194          }          }
195          data->temp[7] = sad; //backup, part 2          data->temp[7] = sad; /* backup, part 2 */
196          return sad;          return sad;
197  }  }
198    
199  static __inline const uint8_t *  static __inline const uint8_t *
200  GetReferenceB(const int x, const int y, const uint32_t dir, const SearchData * const data)  GetReferenceB(const int x, const int y, const uint32_t dir, const SearchData * const data)
201  {  {
202  //      dir : 0 = forward, 1 = backward          /* dir : 0 = forward, 1 = backward */
203          switch ( (dir << 2) | ((x&1)<<1) | (y&1) ) {          const uint8_t *const *const direction = ( dir == 0 ? data->RefP : data->b_RefP );
204                  case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth);          const int picture = ((x&1)<<1) | (y&1);
205                  case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth);          const int offset = (x>>1) + (y>>1)*data->iEdgedWidth;
206                  case 2 : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth);          return direction[picture] + offset;
                 case 3 : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth);  
                 case 4 : return data->bRef + x/2 + (y/2)*(data->iEdgedWidth);  
                 case 5 : return data->bRefV + x/2 + ((y-1)/2)*(data->iEdgedWidth);  
                 case 6 : return data->bRefH + (x-1)/2 + (y/2)*(data->iEdgedWidth);  
                 default : return data->bRefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth);  
         }  
207  }  }
208    
209  // this is a simpler copy of GetReferenceB, but as it's __inline anyway, we can keep the two separate  /* this is a simpler copy of GetReferenceB, but as it's __inline anyway, we can keep the two separate */
210  static __inline const uint8_t *  static __inline const uint8_t *
211  GetReference(const int x, const int y, const SearchData * const data)  GetReference(const int x, const int y, const SearchData * const data)
212  {  {
213          switch ( ((x&1)<<1) | (y&1) ) {          const int picture = ((x&1)<<1) | (y&1);
214                  case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth);          const int offset = (x>>1) + (y>>1)*data->iEdgedWidth;
215                  case 3 : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth);          return data->RefP[picture] + offset;
                 case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth);  
                 default : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth);      //case 2  
         }  
216  }  }
217    
218  static uint8_t *  static uint8_t *
219  Interpolate8x8qpel(const int x, const int y, const uint32_t block, const uint32_t dir, const SearchData * const data)  Interpolate8x8qpel(const int x, const int y, const uint32_t block, const uint32_t dir, const SearchData * const data)
220  {  {
221  // create or find a qpel-precision reference picture; return pointer to it          /* create or find a qpel-precision reference picture; return pointer to it */
222          uint8_t * Reference = data->RefQ + 16*dir;          uint8_t * Reference = data->RefQ + 16*dir;
223          const uint32_t iEdgedWidth = data->iEdgedWidth;          const uint32_t iEdgedWidth = data->iEdgedWidth;
224          const uint32_t rounding = data->rounding;          const uint32_t rounding = data->rounding;
# Line 249  Line 229 
229          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);
230          ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;          ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
231          switch( ((x&1)<<1) + (y&1) ) {          switch( ((x&1)<<1) + (y&1) ) {
232          case 0: // pure halfpel position          case 3: /* x and y in qpel resolution - the "corners" (top left/right and */
233                  return (uint8_t *) ref1;                          /* bottom left/right) during qpel refinement */
234                    ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
235                    ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
236                    ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);
237                    ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
238                    ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
239                    ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
240                    interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
241                  break;                  break;
242    
243          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          case 1: /* x halfpel, y qpel - top or bottom during qpel refinement */
244                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
245                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
246                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
247                  break;                  break;
248    
249          case 2: // x qpel, y halfpel - left or right during qpel refinement          case 2: /* x qpel, y halfpel - left or right during qpel refinement */
250                  ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);                  ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
251                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
252                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
253                  break;                  break;
254    
255          default: // x and y in qpel resolution - the "corners" (top left/right and          default: /* pure halfpel position */
256                           // bottom left/right) during qpel refinement                  return (uint8_t *) ref1;
257                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);  
                 ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);  
                 ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);  
                 ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);  
                 break;  
258          }          }
259          return Reference;          return Reference;
260  }  }
# Line 282  Line 262 
262  static uint8_t *  static uint8_t *
263  Interpolate16x16qpel(const int x, const int y, const uint32_t dir, const SearchData * const data)  Interpolate16x16qpel(const int x, const int y, const uint32_t dir, const SearchData * const data)
264  {  {
265  // create or find a qpel-precision reference picture; return pointer to it          /* create or find a qpel-precision reference picture; return pointer to it */
266          uint8_t * Reference = data->RefQ + 16*dir;          uint8_t * Reference = data->RefQ + 16*dir;
267          const uint32_t iEdgedWidth = data->iEdgedWidth;          const uint32_t iEdgedWidth = data->iEdgedWidth;
268          const uint32_t rounding = data->rounding;          const uint32_t rounding = data->rounding;
# Line 292  Line 272 
272    
273          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);
274          switch( ((x&1)<<1) + (y&1) ) {          switch( ((x&1)<<1) + (y&1) ) {
275          case 3: // x and y in qpel resolution - the "corners" (top left/right and          case 3:
276                           // bottom left/right) during qpel refinement                  /*
277                     * x and y in qpel resolution - the "corners" (top left/right and
278                     * bottom left/right) during qpel refinement
279                     */
280                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
281                  ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);                  ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
282                  ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);                  ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);
# Line 303  Line 286 
286                  interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);                  interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);
287                  break;                  break;
288    
289          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          case 1: /* x halfpel, y qpel - top or bottom during qpel refinement */
290                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
291                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
292                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
# Line 311  Line 294 
294                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);
295                  break;                  break;
296    
297          case 2: // x qpel, y halfpel - left or right during qpel refinement          case 2: /* x qpel, y halfpel - left or right during qpel refinement */
298                  ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);                  ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
299                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
300                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
# Line 319  Line 302 
302                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);
303                  break;                  break;
304    
305          case 0: // pure halfpel position          default: /* pure halfpel position */
306                  return (uint8_t *) ref1;                  return (uint8_t *) ref1;
307          }          }
308          return Reference;          return Reference;
# Line 342  Line 325 
325                  Reference = GetReference(x, y, data);                  Reference = GetReference(x, y, data);
326                  current = data->currentMV;                  current = data->currentMV;
327                  xc = x; yc = y;                  xc = x; yc = y;
328          } else { // x and y are in 1/4 precision          } else { /* x and y are in 1/4 precision */
329                  Reference = Interpolate16x16qpel(x, y, 0, data);                  Reference = Interpolate16x16qpel(x, y, 0, data);
330                  xc = x/2; yc = y/2; //for chroma sad                  xc = x/2; yc = y/2; /* for chroma sad */
331                  current = data->currentQMV;                  current = data->currentQMV;
332          }          }
333    
# Line 371  Line 354 
354                  data->iMinSAD[3] = data->temp[3]; current[3].x = x; current[3].y = y; }                  data->iMinSAD[3] = data->temp[3]; current[3].x = x; current[3].y = y; }
355          if (data->temp[4] < data->iMinSAD[4]) {          if (data->temp[4] < data->iMinSAD[4]) {
356                  data->iMinSAD[4] = data->temp[4]; current[4].x = x; current[4].y = y; }                  data->iMinSAD[4] = data->temp[4]; current[4].x = x; current[4].y = y; }
   
357  }  }
358    
359  static void  static void
# Line 379  Line 361 
361  {  {
362          int32_t sad; uint32_t t;          int32_t sad; uint32_t t;
363          const uint8_t * Reference;          const uint8_t * Reference;
364            VECTOR * current;
365    
366          if ( (x > data->max_dx) || (x < data->min_dx)          if ( (x > data->max_dx) || (x < data->min_dx)
367                  || (y > data->max_dy) || (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
368    
369          if (!data->qpel_precision) Reference = GetReference(x, y, data);          if (!data->qpel_precision) {
370          else Reference = Interpolate8x8qpel(x, y, 0, 0, data);                  Reference = GetReference(x, y, data);
371                    current = data->currentMV;
372            } else { /* x and y are in 1/4 precision */
373                    Reference = Interpolate8x8qpel(x, y, 0, 0, data);
374                    current = data->currentQMV;
375            }
376    
377          sad = sad8(data->Cur, Reference, data->iEdgedWidth);          sad = sad8(data->Cur, Reference, data->iEdgedWidth);
378          t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);          t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
# Line 393  Line 381 
381    
382          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
383                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
384                  data->currentMV->x = x; data->currentMV->y = y;                  current->x = x; current->y = y;
385                  *dir = Direction;                  *dir = Direction;
386          }          }
387  }  }
388    
   
389  static void  static void
390  CheckCandidate32(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidate32(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
391  {  {
392          uint32_t t;          uint32_t t;
393          const uint8_t * Reference;          const uint8_t * Reference;
394    
395          if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero integer value          if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || /* non-zero even value */
396                  (x > data->max_dx) || (x < data->min_dx)                  (x > data->max_dx) || (x < data->min_dx)
397                  || (y > data->max_dy) || (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
398    
# Line 440  Line 427 
427          uint32_t t;          uint32_t t;
428          VECTOR * current;          VECTOR * current;
429    
430          if ( (x > data->max_dx) | ( x < data->min_dx)          if ( (x > data->max_dx) || ( x < data->min_dx)
431                  | (y > data->max_dy) | (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
432    
433          if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; //non-zero even value          if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; /* non-zero even value */
434    
435          if (data->qpel_precision) { // x and y are in 1/4 precision          if (data->qpel_precision) { /* x and y are in 1/4 precision */
436                  Reference = Interpolate16x16qpel(x, y, 0, data);                  Reference = Interpolate16x16qpel(x, y, 0, data);
437                  current = data->currentQMV;                  current = data->currentQMV;
438                  xc = x/2; yc = y/2;                  xc = x/2; yc = y/2;
# Line 473  Line 460 
460  static void  static void
461  CheckCandidate32I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidate32I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
462  {  {
463  // maximum speed - for P/B/I decision          /* maximum speed - for P/B/I decision */
464          int32_t sad;          int32_t sad;
465    
466          if ( (x > data->max_dx) || (x < data->min_dx)          if ( (x > data->max_dx) || (x < data->min_dx)
467                  || (y > data->max_dy) || (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
468    
469          sad = sad32v_c(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth),          sad = sad32v_c(data->Cur, data->RefP[0] + (x>>1) + (y>>1)*(data->iEdgedWidth),
470                                                          data->iEdgedWidth, data->temp+1);                                                          data->iEdgedWidth, data->temp+1);
471    
472          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
# Line 506  Line 493 
493          const uint8_t *ReferenceF, *ReferenceB;          const uint8_t *ReferenceF, *ReferenceB;
494          VECTOR *current;          VECTOR *current;
495    
496          if ( (xf > data->max_dx) | (xf < data->min_dx)          if ((xf > data->max_dx) || (xf < data->min_dx) ||
497                  | (yf > data->max_dy) | (yf < data->min_dy) ) return;                  (yf > data->max_dy) || (yf < data->min_dy))
498                    return;
499    
500          if (!data->qpel_precision) {          if (!data->qpel_precision) {
501                  ReferenceF = GetReference(xf, yf, data);                  ReferenceF = GetReference(xf, yf, data);
# Line 552  Line 540 
540          const uint8_t *ReferenceB;          const uint8_t *ReferenceB;
541          VECTOR mvs, b_mvs;          VECTOR mvs, b_mvs;
542    
543          if (( x > 31) | ( x < -32) | ( y > 31) | (y < -32)) return;          if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
544    
545          for (k = 0; k < 4; k++) {          for (k = 0; k < 4; k++) {
546                  mvs.x = data->directmvF[k].x + x;                  mvs.x = data->directmvF[k].x + x;
# Line 565  Line 553 
553                          data->directmvB[k].y                          data->directmvB[k].y
554                          : mvs.y - data->referencemv[k].y);                          : mvs.y - data->referencemv[k].y);
555    
556                  if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx)                  if ((mvs.x > data->max_dx)   || (mvs.x < data->min_dx)   ||
557                          | (mvs.y > data->max_dy) | (mvs.y < data->min_dy)                          (mvs.y > data->max_dy)   || (mvs.y < data->min_dy)   ||
558                          | (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx)                          (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) ||
559                          | (b_mvs.y > data->max_dy) | (b_mvs.y < data->min_dy) ) return;                          (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) )
560                            return;
561    
562                  if (data->qpel) {                  if (data->qpel) {
563                          xcf += mvs.x/2; ycf += mvs.y/2;                          xcf += mvs.x/2; ycf += mvs.y/2;
# Line 576  Line 565 
565                  } else {                  } else {
566                          xcf += mvs.x; ycf += mvs.y;                          xcf += mvs.x; ycf += mvs.y;
567                          xcb += b_mvs.x; ycb += b_mvs.y;                          xcb += b_mvs.x; ycb += b_mvs.y;
568                          mvs.x *= 2; mvs.y *= 2; //we move to qpel precision anyway                          mvs.x *= 2; mvs.y *= 2; /* we move to qpel precision anyway */
569                          b_mvs.x *= 2; b_mvs.y *= 2;                          b_mvs.x *= 2; b_mvs.y *= 2;
570                  }                  }
571    
# Line 610  Line 599 
599          const uint8_t *ReferenceB;          const uint8_t *ReferenceB;
600          VECTOR mvs, b_mvs;          VECTOR mvs, b_mvs;
601    
602          if (( x > 31) | ( x < -32) | ( y > 31) | (y < -32)) return;          if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
603    
604          mvs.x = data->directmvF[0].x + x;          mvs.x = data->directmvF[0].x + x;
605          b_mvs.x = ((x == 0) ?          b_mvs.x = ((x == 0) ?
# Line 622  Line 611 
611                  data->directmvB[0].y                  data->directmvB[0].y
612                  : mvs.y - data->referencemv[0].y);                  : mvs.y - data->referencemv[0].y);
613    
614          if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx)          if ( (mvs.x > data->max_dx) || (mvs.x < data->min_dx)
615                  | (mvs.y > data->max_dy) | (mvs.y < data->min_dy)                  || (mvs.y > data->max_dy) || (mvs.y < data->min_dy)
616                  | (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx)                  || (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx)
617                  | (b_mvs.y > data->max_dy) | (b_mvs.y < data->min_dy) ) return;                  || (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) return;
618    
619          if (data->qpel) {          if (data->qpel) {
620                  xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2);                  xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2);
# Line 659  Line 648 
648  CheckCandidateBits16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateBits16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
649  {  {
650    
651          static int16_t in[64], coeff[64];          int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
652          int32_t bits = 0, sum;          int32_t bits = 0;
653          VECTOR * current;          VECTOR * current;
654          const uint8_t * ptr;          const uint8_t * ptr;
655          int i, cbp = 0, t, xc, yc;          int i, cbp = 0, t, xc, yc;
# Line 672  Line 661 
661                  ptr = GetReference(x, y, data);                  ptr = GetReference(x, y, data);
662                  current = data->currentMV;                  current = data->currentMV;
663                  xc = x; yc = y;                  xc = x; yc = y;
664          } else { // x and y are in 1/4 precision          } else { /* x and y are in 1/4 precision */
665                  ptr = Interpolate16x16qpel(x, y, 0, data);                  ptr = Interpolate16x16qpel(x, y, 0, data);
666                  current = data->currentQMV;                  current = data->currentQMV;
667                  xc = x/2; yc = y/2;                  xc = x/2; yc = y/2;
# Line 681  Line 670 
670          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
671                  int s = 8*((i&1) + (i>>1)*data->iEdgedWidth);                  int s = 8*((i&1) + (i>>1)*data->iEdgedWidth);
672                  transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth);                  transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth);
673                  fdct(in);                  bits += data->temp[i] = Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, i);
                 if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16);  
                 else sum = quant4_inter(coeff, in, data->lambda16);  
                 if (sum > 0) {  
                         cbp |= 1 << (5 - i);  
                         bits += data->temp[i] = CodeCoeffInter_CalcBits(coeff, scan_tables[0]);  
                 } else data->temp[i] = 0;  
674          }          }
675    
676          bits += t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);          bits += t = BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
677    
678            bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;
679    
680          if (bits < data->iMinSAD[0]) { // there is still a chance, adding chroma          if (bits >= data->iMinSAD[0]) return;
681    
682            /* chroma */
683                  xc = (xc >> 1) + roundtab_79[xc & 0x3];                  xc = (xc >> 1) + roundtab_79[xc & 0x3];
684                  yc = (yc >> 1) + roundtab_79[yc & 0x3];                  yc = (yc >> 1) + roundtab_79[yc & 0x3];
685    
686                  //chroma U          /* chroma U */
687                  ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefCU, 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);          ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[4], 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);
688                  transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2);                  transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2);
689                  fdct(in);          bits += Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 4);
690                  if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16);          if (bits >= data->iMinSAD[0]) return;
                 else sum = quant4_inter(coeff, in, data->lambda16);  
                 if (sum > 0) {  
                         cbp |= 1 << (5 - 4);  
                         bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);  
                 }  
691    
692                  if (bits < data->iMinSAD[0]) {          /* chroma V */
693                          //chroma V          ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[5], 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);
                         ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefCV, 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);  
694                          transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2);                          transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2);
695                          fdct(in);          bits += Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 5);
                         if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16);  
                         else sum = quant4_inter(coeff, in, data->lambda16);  
                         if (sum > 0) {  
                                 cbp |= 1 << (5 - 5);  
                                 bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);  
                         }  
                 }  
         }  
696    
697          bits += cbpy_tab[15-(cbp>>2)].len;          bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len;
         bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len;  
698    
699          if (bits < data->iMinSAD[0]) {          if (bits < data->iMinSAD[0]) {
700                  data->iMinSAD[0] = bits;                  data->iMinSAD[0] = bits;
# Line 744  Line 716 
716  CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
717  {  {
718    
719          static int16_t in[64], coeff[64];          int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
720          int32_t sum, bits;          int32_t bits;
721          VECTOR * current;          VECTOR * current;
722          const uint8_t * ptr;          const uint8_t * ptr;
723          int cbp;          int cbp = 0;
724    
725          if ( (x > data->max_dx) || (x < data->min_dx)          if ( (x > data->max_dx) || (x < data->min_dx)
726                  || (y > data->max_dy) || (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
# Line 756  Line 728 
728          if (!data->qpel_precision) {          if (!data->qpel_precision) {
729                  ptr = GetReference(x, y, data);                  ptr = GetReference(x, y, data);
730                  current = data->currentMV;                  current = data->currentMV;
731          } else { // x and y are in 1/4 precision          } else { /* x and y are in 1/4 precision */
732                  ptr = Interpolate8x8qpel(x, y, 0, 0, data);                  ptr = Interpolate8x8qpel(x, y, 0, 0, data);
733                  current = data->currentQMV;                  current = data->currentQMV;
734          }          }
735    
736          transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth);          transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth);
737          fdct(in);          bits = Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 5);
738          if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16);          bits += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
         else sum = quant4_inter(coeff, in, data->lambda16);  
         if (sum > 0) {  
                 bits = CodeCoeffInter_CalcBits(coeff, scan_tables[0]);  
                 cbp = 1;  
         } else cbp = bits = 0;  
   
         bits += sum = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);  
739    
740          if (bits < data->iMinSAD[0]) {          if (bits < data->iMinSAD[0]) {
741                  data->temp[0] = cbp;                  data->temp[0] = cbp;
# Line 792  Line 757 
757    
758          int iDirection;          int iDirection;
759    
760          for(;;) { //forever          for(;;) { /* forever */
761                  iDirection = 0;                  iDirection = 0;
762                  if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);                  if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);
763                  if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);                  if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);
# Line 801  Line 766 
766    
767                  /* now we're doing diagonal checks near our candidate */                  /* now we're doing diagonal checks near our candidate */
768    
769                  if (iDirection) {               //if anything found                  if (iDirection) {               /* if anything found */
770                          bDirection = iDirection;                          bDirection = iDirection;
771                          iDirection = 0;                          iDirection = 0;
772                          x = data->currentMV->x; y = data->currentMV->y;                          x = data->currentMV->x; y = data->currentMV->y;
773                          if (bDirection & 3) {   //our candidate is left or right                          if (bDirection & 3) {   /* our candidate is left or right */
774                                  CHECK_CANDIDATE(x, y + iDiamondSize, 8);                                  CHECK_CANDIDATE(x, y + iDiamondSize, 8);
775                                  CHECK_CANDIDATE(x, y - iDiamondSize, 4);                                  CHECK_CANDIDATE(x, y - iDiamondSize, 4);
776                          } else {                        // what remains here is up or down                          } else {                        /* what remains here is up or down */
777                                  CHECK_CANDIDATE(x + iDiamondSize, y, 2);                                  CHECK_CANDIDATE(x + iDiamondSize, y, 2);
778                                  CHECK_CANDIDATE(x - iDiamondSize, y, 1);                                  CHECK_CANDIDATE(x - iDiamondSize, y, 1);
779                          }                          }
# Line 817  Line 782 
782                                  bDirection += iDirection;                                  bDirection += iDirection;
783                                  x = data->currentMV->x; y = data->currentMV->y;                                  x = data->currentMV->x; y = data->currentMV->y;
784                          }                          }
785                  } else {                                //about to quit, eh? not so fast....                  } else {                                /* about to quit, eh? not so fast.... */
786                          switch (bDirection) {                          switch (bDirection) {
787                          case 2:                          case 2:
788                                  CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);                                  CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
# Line 855  Line 820 
820                                  CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);                                  CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
821                                  CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);                                  CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
822                                  break;                                  break;
823                          default:                //1+2+4+8 == we didn't find anything at all                          default:                /* 1+2+4+8 == we didn't find anything at all */
824                                  CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);                                  CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
825                                  CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);                                  CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
826                                  CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);                                  CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
827                                  CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);                                  CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
828                                  break;                                  break;
829                          }                          }
830                          if (!iDirection) break;         //ok, the end. really                          if (!iDirection) break;         /* ok, the end. really */
831                          bDirection = iDirection;                          bDirection = iDirection;
832                          x = data->currentMV->x; y = data->currentMV->y;                          x = data->currentMV->x; y = data->currentMV->y;
833                  }                  }
# Line 907  Line 872 
872    
873                  /* now we're doing diagonal checks near our candidate */                  /* now we're doing diagonal checks near our candidate */
874    
875                  if (iDirection) {               //checking if anything found                  if (iDirection) {               /* checking if anything found */
876                          bDirection = iDirection;                          bDirection = iDirection;
877                          iDirection = 0;                          iDirection = 0;
878                          x = data->currentMV->x; y = data->currentMV->y;                          x = data->currentMV->x; y = data->currentMV->y;
879                          if (bDirection & 3) {   //our candidate is left or right                          if (bDirection & 3) {   /* our candidate is left or right */
880                                  CHECK_CANDIDATE(x, y + iDiamondSize, 8);                                  CHECK_CANDIDATE(x, y + iDiamondSize, 8);
881                                  CHECK_CANDIDATE(x, y - iDiamondSize, 4);                                  CHECK_CANDIDATE(x, y - iDiamondSize, 4);
882                          } else {                        // what remains here is up or down                          } else {                        /* what remains here is up or down */
883                                  CHECK_CANDIDATE(x + iDiamondSize, y, 2);                                  CHECK_CANDIDATE(x + iDiamondSize, y, 2);
884                                  CHECK_CANDIDATE(x - iDiamondSize, y, 1);                                  CHECK_CANDIDATE(x - iDiamondSize, y, 1);
885                          }                          }
# Line 932  Line 897 
897  {  {
898  /* Do a half-pel or q-pel refinement */  /* Do a half-pel or q-pel refinement */
899          const VECTOR centerMV = data->qpel_precision ? *data->currentQMV : *data->currentMV;          const VECTOR centerMV = data->qpel_precision ? *data->currentQMV : *data->currentMV;
900          int iDirection; //only needed because macro expects it          int iDirection; /* only needed because macro expects it */
901    
902          CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, 0);          CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, 0);
903          CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, 0);          CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, 0);
# Line 950  Line 915 
915                                                          const uint32_t stride, const uint32_t iQuant, int rrv)                                                          const uint32_t stride, const uint32_t iQuant, int rrv)
916    
917  {  {
918            int offset = (x + y*stride)*8;
919          if(!rrv) {          if(!rrv) {
920                  uint32_t sadC = sad8(current->u + x*8 + y*stride*8,                  uint32_t sadC = sad8(current->u + offset,
921                                                  reference->u + x*8 + y*stride*8, stride);                                                  reference->u + offset, stride);
922                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
923                  sadC += sad8(current->v + (x + y*stride)*8,                  sadC += sad8(current->v + offset,
924                                                  reference->v + (x + y*stride)*8, stride);                                                  reference->v + offset, stride);
925                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
926                  return 1;                  return 1;
927    
928          } else {          } else {
929                  uint32_t sadC = sad16(current->u + x*16 + y*stride*16,                  uint32_t sadC = sad16(current->u + 2*offset,
930                                                  reference->u + x*16 + y*stride*16, stride, 256*4096);                                                  reference->u + 2*offset, stride, 256*4096);
931                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;
932                  sadC += sad16(current->v + (x + y*stride)*16,                  sadC += sad16(current->v + 2*offset,
933                                                  reference->v + (x + y*stride)*16, stride, 256*4096);                                                  reference->v + 2*offset, stride, 256*4096);
934                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;
935                  return 1;                  return 1;
936          }          }
# Line 979  Line 945 
945          pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;          pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
946  }  }
947    
948    static __inline void
949    ModeDecision(SearchData * const Data,
950                            MACROBLOCK * const pMB,
951                            const MACROBLOCK * const pMBs,
952                            const int x, const int y,
953                            const MBParam * const pParam,
954                            const uint32_t MotionFlags,
955                            const uint32_t VopFlags,
956                            const uint32_t VolFlags,
957                            const IMAGE * const pCurrent,
958                            const IMAGE * const pRef)
959    {
960            int mode = MODE_INTER;
961            int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);
962            const uint32_t iQuant = pMB->quant;
963    
964            const int skip_possible = (!(VolFlags & XVID_VOL_GMC)) && (pMB->dquant == 0);
965    
966            if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) { /* normal, fast, SAD-based mode decision */
967                    int sad;
968                    int InterBias = MV16_INTER_BIAS;
969                    if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
970                            Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) {
971                            mode = MODE_INTER;
972                            sad = Data->iMinSAD[0];
973                    } else {
974                            mode = MODE_INTER4V;
975                            sad = Data->iMinSAD[1] + Data->iMinSAD[2] +
976                                                    Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant;
977                            Data->iMinSAD[0] = sad;
978                    }
979    
980                    /* final skip decision, a.k.a. "the vector you found, really that good?" */
981                    if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP))
982                            if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH)
983                                    if (Data->chroma || SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) {
984                                            mode = MODE_NOT_CODED;
985                                            sad = 0;
986                                    }
987    
988                    /* intra decision */
989    
990                    if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */
991                    if (y != 0)
992                            if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;
993                    if (x != 0)
994                            if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
995    
996                    if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? */
997                    if (Data->rrv) InterBias *= 4;
998    
999                    if (InterBias < pMB->sad16) {
1000                            int32_t deviation;
1001                            if (!Data->rrv) deviation = dev16(Data->Cur, Data->iEdgedWidth);
1002                            else deviation = dev16(Data->Cur, Data->iEdgedWidth) +
1003                                    dev16(Data->Cur+16, Data->iEdgedWidth) +
1004                                    dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) +
1005                                    dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth);
1006    
1007                            if (deviation < (sad - InterBias)) mode = MODE_INTRA;
1008                    }
1009    
1010            } else { /* BITS */
1011    
1012                    int bits, intra, i;
1013                    VECTOR backup[5], *v;
1014                    Data->iQuant = iQuant;
1015    
1016                    v = Data->qpel ? Data->currentQMV : Data->currentMV;
1017                    for (i = 0; i < 5; i++) {
1018                            Data->iMinSAD[i] = 256*4096;
1019                            backup[i] = v[i];
1020                    }
1021    
1022                    bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags);
1023                    if (bits == 0)
1024                            mode = MODE_INTER; /* quick stop */
1025                    else {
1026                            if (inter4v) {
1027                                    int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup);
1028                                    if (bits_inter4v < bits) { Data->iMinSAD[0] = bits = bits_inter4v; mode = MODE_INTER4V; }
1029                            }
1030    
1031                            intra = CountMBBitsIntra(Data);
1032    
1033                            if (intra < bits) { *Data->iMinSAD = bits = intra; mode = MODE_INTRA; }
1034                    }
1035            }
1036    
1037            if (Data->rrv) {
1038                            Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);
1039                            Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);
1040            }
1041    
1042            if (mode == MODE_INTER) {
1043                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
1044                    pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];
1045    
1046                    if(Data->qpel) {
1047                            pMB->qmvs[0] = pMB->qmvs[1]
1048                                    = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
1049                            pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;
1050                            pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;
1051                    } else {
1052                            pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1053                            pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
1054                    }
1055    
1056            } else if (mode == MODE_INTER4V)
1057                    pMB->sad16 = Data->iMinSAD[0];
1058            else /* INTRA, NOT_CODED */
1059                    SkipMacroblockP(pMB, 0);
1060    
1061            pMB->mode = mode;
1062    }
1063    
1064  bool  bool
1065  MotionEstimation(MBParam * const pParam,  MotionEstimation(MBParam * const pParam,
1066                                   FRAMEINFO * const current,                                   FRAMEINFO * const current,
# Line 999  Line 1081 
1081    
1082          uint32_t x, y;          uint32_t x, y;
1083          uint32_t iIntra = 0;          uint32_t iIntra = 0;
1084      int32_t sad00;          int32_t quant = current->quant, sad00;
1085            int skip_thresh = \
1086                    INITIAL_SKIP_THRESH * \
1087                    (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \
1088                    (current->vop_flags & XVID_VOP_MODEDECISION_BITS ? 2:1);
1089    
1090          // some pre-initialized thingies for SearchP          /* some pre-initialized thingies for SearchP */
1091          int32_t temp[8];          int32_t temp[8];
1092          VECTOR currentMV[5];          VECTOR currentMV[5];
1093          VECTOR currentQMV[5];          VECTOR currentQMV[5];
1094          int32_t iMinSAD[5];          int32_t iMinSAD[5];
1095            DECLARE_ALIGNED_MATRIX(dct_space, 2, 64, int16_t, CACHE_LINE);
1096          SearchData Data;          SearchData Data;
1097          memset(&Data, 0, sizeof(SearchData));          memset(&Data, 0, sizeof(SearchData));
1098          Data.iEdgedWidth = iEdgedWidth;          Data.iEdgedWidth = iEdgedWidth;
# Line 1015  Line 1102 
1102          Data.temp = temp;          Data.temp = temp;
1103          Data.iFcode = current->fcode;          Data.iFcode = current->fcode;
1104          Data.rounding = pParam->m_rounding_type;          Data.rounding = pParam->m_rounding_type;
1105          Data.qpel = current->vol_flags & XVID_VOL_QUARTERPEL;          Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);
1106          Data.chroma = MotionFlags & XVID_ME_CHROMA16;          Data.chroma = MotionFlags & XVID_ME_CHROMA16;
1107          Data.rrv = current->vop_flags & XVID_VOP_REDUCED;          Data.rrv = (current->vop_flags & XVID_VOP_REDUCED ? 1:0);
1108            Data.dctSpace = dct_space;
1109            Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
1110    
1111          if ((current->vop_flags & XVID_VOP_REDUCED)) {          if ((current->vop_flags & XVID_VOP_REDUCED)) {
1112                  mb_width = (pParam->width + 31) / 32;                  mb_width = (pParam->width + 31) / 32;
# Line 1025  Line 1114 
1114                  Data.qpel = 0;                  Data.qpel = 0;
1115          }          }
1116    
1117          Data.RefQ = pRefV->u; // a good place, also used in MC (for similar purpose)          Data.RefQ = pRefV->u; /* a good place, also used in MC (for similar purpose) */
1118          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();
1119    
1120          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
# Line 1052  Line 1141 
1141    
1142                          sad00 = pMB->sad16;                          sad00 = pMB->sad16;
1143    
1144  //initial skip decision                          if (pMB->dquant != 0) {
1145                                    quant += DQtab[pMB->dquant];
1146                                    if (quant > 31) quant = 31;
1147                                    else if (quant < 1) quant = 1;
1148                            }
1149                            pMB->quant = quant;
1150    
1151                            /* initial skip decision */
1152  /* no early skip for GMC (global vector = skip vector is unknown!)  */  /* no early skip for GMC (global vector = skip vector is unknown!)  */
1153                          if (!(current->vol_flags & XVID_VOL_GMC))       { /* no fast SKIP for S(GMC)-VOPs */                          if (!(current->vol_flags & XVID_VOL_GMC))       { /* no fast SKIP for S(GMC)-VOPs */
1154                                  if (pMB->dquant == 0 && sad00 < pMB->quant * INITIAL_SKIP_THRESH * (Data.rrv ? 4:1) )                                  if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)
1155                                          if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {                                          if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {
1156                                                  SkipMacroblockP(pMB, sad00);                                                  SkipMacroblockP(pMB, sad00);
1157                                                  continue;                                                  continue;
# Line 1063  Line 1159 
1159                          }                          }
1160    
1161                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
1162                                                  y, MotionFlags, current->vol_flags, pMB->quant,                                          y, MotionFlags, current->vop_flags, current->vol_flags,
1163                                                  &Data, pParam, pMBs, reference->mbs,                                          &Data, pParam, pMBs, reference->mbs, pMB);
                                                 current->vop_flags & XVID_VOP_INTER4V, pMB);  
1164    
1165  /* final skip decision, a.k.a. "the vector you found, really that good?" */                          ModeDecision(&Data, pMB, pMBs, x, y, pParam,
1166                          if (!(current->vol_flags & XVID_VOL_GMC))       {                                                   MotionFlags, current->vop_flags, current->vol_flags,
1167                                  if ( pMB->dquant == 0 && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) {                                                   pCurrent, pRef);
                                         if (!(current->vop_flags & XVID_VOP_MODEDECISION_BITS)) {  
                                                 if ( (100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1) )  
                                                         if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv))  
                                                                 SkipMacroblockP(pMB, sad00);  
                                         } else { // BITS mode decision  
                                                 if (pMB->sad16 > 10)  
                                                         SkipMacroblockP(pMB, sad00);  // more than 10 bits would be used for this MB - skip  
1168    
                                         }  
                                 }  
                         }  
1169                          if (pMB->mode == MODE_INTRA)                          if (pMB->mode == MODE_INTRA)
1170                                  if (++iIntra > iLimit) return 1;                                  if (++iIntra > iLimit) return 1;
1171                  }                  }
# Line 1099  Line 1184 
1184  {  {
1185          int mask = 255, j;          int mask = 255, j;
1186          for (j = 0; j < i; j++) {          for (j = 0; j < i; j++) {
1187                  if (MVequal(pmv[i], pmv[j])) return 0; // same vector has been checked already                  if (MVequal(pmv[i], pmv[j])) return 0; /* same vector has been checked already */
1188                  if (pmv[i].x == pmv[j].x) {                  if (pmv[i].x == pmv[j].x) {
1189                          if (pmv[i].y == pmv[j].y + iDiamondSize) mask &= ~4;                          if (pmv[i].y == pmv[j].y + iDiamondSize) mask &= ~4;
1190                          else if (pmv[i].y == pmv[j].y - iDiamondSize) mask &= ~8;                          else if (pmv[i].y == pmv[j].y - iDiamondSize) mask &= ~8;
# Line 1116  Line 1201 
1201  PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,  PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,
1202                          int iHcount, const MACROBLOCK * const prevMB, int rrv)                          int iHcount, const MACROBLOCK * const prevMB, int rrv)
1203  {  {
1204            /* this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself */
 //this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself  
1205          if (rrv) { iWcount /= 2; iHcount /= 2; }          if (rrv) { iWcount /= 2; iHcount /= 2; }
1206    
1207          if ( (y != 0) && (x < (iWcount-1)) ) {          // [5] top-right neighbour          if ( (y != 0) && (x < (iWcount-1)) ) {          /* [5] top-right neighbour */
1208                  pmv[5].x = EVEN(pmv[3].x);                  pmv[5].x = EVEN(pmv[3].x);
1209                  pmv[5].y = EVEN(pmv[3].y);                  pmv[5].y = EVEN(pmv[3].y);
1210          } else pmv[5].x = pmv[5].y = 0;          } else pmv[5].x = pmv[5].y = 0;
1211    
1212          if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }// pmv[3] is left neighbour          if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }/* pmv[3] is left neighbour */
1213          else pmv[3].x = pmv[3].y = 0;          else pmv[3].x = pmv[3].y = 0;
1214    
1215          if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }// [4] top neighbour          if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }/* [4] top neighbour */
1216          else pmv[4].x = pmv[4].y = 0;          else pmv[4].x = pmv[4].y = 0;
1217    
1218          // [1] median prediction          /* [1] median prediction */
1219          pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y);          pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y);
1220    
1221          pmv[0].x = pmv[0].y = 0; // [0] is zero; not used in the loop (checked before) but needed here for make_mask          pmv[0].x = pmv[0].y = 0; /* [0] is zero; not used in the loop (checked before) but needed here for make_mask */
1222    
1223          pmv[2].x = EVEN(prevMB->mvs[0].x); // [2] is last frame          pmv[2].x = EVEN(prevMB->mvs[0].x); /* [2] is last frame */
1224          pmv[2].y = EVEN(prevMB->mvs[0].y);          pmv[2].y = EVEN(prevMB->mvs[0].y);
1225    
1226          if ((x < iWcount-1) && (y < iHcount-1)) {          if ((x < iWcount-1) && (y < iHcount-1)) {
1227                  pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); //[6] right-down neighbour in last frame                  pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); /* [6] right-down neighbour in last frame */
1228                  pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);                  pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);
1229          } else pmv[6].x = pmv[6].y = 0;          } else pmv[6].x = pmv[6].y = 0;
1230    
# Line 1153  Line 1237 
1237          }          }
1238  }  }
1239    
 static int  
 ModeDecision(const uint32_t iQuant, SearchData * const Data,  
                 int inter4v,  
                 MACROBLOCK * const pMB,  
                 const MACROBLOCK * const pMBs,  
                 const int x, const int y,  
                 const MBParam * const pParam,  
                 const uint32_t MotionFlags,  
                 const uint32_t VopFlags)  
 {  
   
         int mode = MODE_INTER;  
   
         if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision  
 //              int intra = 0;  
                 int sad;  
                 int InterBias = MV16_INTER_BIAS;  
                 if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +  
                         Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) {  
                                 mode = 0; //inter  
                                 sad = Data->iMinSAD[0];  
                 } else {  
                         mode = MODE_INTER4V;  
                         sad = Data->iMinSAD[1] + Data->iMinSAD[2] +  
                                                 Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant;  
                         Data->iMinSAD[0] = sad;  
                 }  
   
                 /* intra decision */  
   
                 if (iQuant > 8) InterBias += 100 * (iQuant - 8); // to make high quants work  
                 if (y != 0)  
                         if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;  
                 if (x != 0)  
                         if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;  
   
                 if (Data->chroma) InterBias += 50; // to compensate bigger SAD  
                 if (Data->rrv) InterBias *= 4;  
   
                 if (InterBias < pMB->sad16) {  
                         int32_t deviation;  
                         if (!Data->rrv) deviation = dev16(Data->Cur, Data->iEdgedWidth);  
                         else deviation = dev16(Data->Cur, Data->iEdgedWidth) +  
                                 dev16(Data->Cur+8, Data->iEdgedWidth) +  
                                 dev16(Data->Cur + 8*Data->iEdgedWidth, Data->iEdgedWidth) +  
                                 dev16(Data->Cur+8+8*Data->iEdgedWidth, Data->iEdgedWidth);  
   
                         if (deviation < (sad - InterBias))  return MODE_INTRA;// intra  
                 }  
                 return mode;  
   
         } else {  
   
                 int bits, intra, i;  
                 VECTOR backup[5], *v;  
                 Data->lambda16 = iQuant;  
         Data->lambda8 = (pParam->vol_flags & XVID_VOL_MPEGQUANT)?1:0;  
   
                 v = Data->qpel ? Data->currentQMV : Data->currentMV;  
                 for (i = 0; i < 5; i++) {  
                         Data->iMinSAD[i] = 256*4096;  
                         backup[i] = v[i];  
                 }  
   
                 bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags);  
                 if (bits == 0) return MODE_INTER; // quick stop  
   
                 if (inter4v) {  
                         int inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup);  
                         if (inter4v < bits) { Data->iMinSAD[0] = bits = inter4v; mode = MODE_INTER4V; }  
                 }  
   
   
                 intra = CountMBBitsIntra(Data);  
   
                 if (intra < bits) { *Data->iMinSAD = bits = intra; return MODE_INTRA; }  
   
                 return mode;  
         }  
 }  
   
1240  static void  static void
1241  SearchP(const IMAGE * const pRef,  SearchP(const IMAGE * const pRef,
1242                  const uint8_t * const pRefH,                  const uint8_t * const pRefH,
# Line 1244  Line 1247 
1247                  const int y,                  const int y,
1248                  const uint32_t MotionFlags,                  const uint32_t MotionFlags,
1249                  const uint32_t VopFlags,                  const uint32_t VopFlags,
1250                  const uint32_t iQuant,                  const uint32_t VolFlags,
1251                  SearchData * const Data,                  SearchData * const Data,
1252                  const MBParam * const pParam,                  const MBParam * const pParam,
1253                  const MACROBLOCK * const pMBs,                  const MACROBLOCK * const pMBs,
1254                  const MACROBLOCK * const prevMBs,                  const MACROBLOCK * const prevMBs,
                 int inter4v,  
1255                  MACROBLOCK * const pMB)                  MACROBLOCK * const pMB)
1256  {  {
1257    
1258          int i, iDirection = 255, mask, threshA;          int i, iDirection = 255, mask, threshA;
1259          VECTOR pmv[7];          VECTOR pmv[7];
1260            int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);
1261    
1262          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1263                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);
1264    
1265          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);
1266    
1267          Data->temp[5] = Data->temp[6] = 0; // chroma-sad cache          Data->temp[5] = Data->temp[6] = 0; /* chroma-sad cache */
1268          i = Data->rrv ? 2 : 1;          i = Data->rrv ? 2 : 1;
1269          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i;          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i;
1270          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;
1271          Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;          Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;
1272    
1273          Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16*i;
1274          Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16*i;
1275          Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16*i;
1276          Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16*i;
1277          Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;          Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;
1278          Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;          Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;
1279    
1280          Data->lambda16 = lambda_vec16[iQuant];          Data->lambda16 = lambda_vec16[pMB->quant];
1281          Data->lambda8 = lambda_vec8[iQuant];          Data->lambda8 = lambda_vec8[pMB->quant];
1282          Data->qpel_precision = 0;          Data->qpel_precision = 0;
1283    
1284          if (pMB->dquant != 0) inter4v = 0;          memset(Data->currentMV, 0, 5*sizeof(VECTOR));
   
         for(i = 0; i < 5; i++)  
                 Data->currentMV[i].x = Data->currentMV[i].y = 0;  
1285    
1286          if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);          if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1287          else Data->predMV = pmv[0];          else Data->predMV = pmv[0];
# Line 1294  Line 1294 
1294          Data->iMinSAD[4] = pMB->sad8[3];          Data->iMinSAD[4] = pMB->sad8[3];
1295    
1296          if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) || (x | y)) {          if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) || (x | y)) {
1297                  threshA = Data->temp[0]; // that's where we keep this SAD atm                  threshA = Data->temp[0]; /* that's where we keep this SAD atm */
1298                  if (threshA < 512) threshA = 512;                  if (threshA < 512) threshA = 512;
1299                  else if (threshA > 1024) threshA = 1024;                  else if (threshA > 1024) threshA = 1024;
1300          } else          } else
# Line 1305  Line 1305 
1305    
1306          if (!Data->rrv) {          if (!Data->rrv) {
1307                  if (inter4v | Data->chroma) CheckCandidate = CheckCandidate16;                  if (inter4v | Data->chroma) CheckCandidate = CheckCandidate16;
1308                          else CheckCandidate = CheckCandidate16no4v; //for extra speed                          else CheckCandidate = CheckCandidate16no4v; /* for extra speed */
1309          } else CheckCandidate = CheckCandidate32;          } else CheckCandidate = CheckCandidate32;
1310    
1311  /* main loop. checking all predictions (but first, which is 0,0 and has been checked in MotionEstimation())*/  /* main loop. checking all predictions (but first, which is 0,0 and has been checked in MotionEstimation())*/
# Line 1318  Line 1318 
1318    
1319          if ((Data->iMinSAD[0] <= threshA) ||          if ((Data->iMinSAD[0] <= threshA) ||
1320                          (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&                          (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&
1321                          (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) {                          (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16)))
1322                  if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) inter4v = 0;      }                  inter4v = 0;
1323          else {          else {
1324    
1325                  MainSearchFunc * MainSearchPtr;                  MainSearchFunc * MainSearchPtr;
# Line 1365  Line 1365 
1365          }          }
1366    
1367          if (MotionFlags & XVID_ME_HALFPELREFINE16)          if (MotionFlags & XVID_ME_HALFPELREFINE16)
                 if ((!(MotionFlags & XVID_ME_HALFPELREFINE16_BITS)) || Data->iMinSAD[0] < 200*(int)iQuant)  
1368                          SubpelRefine(Data);                          SubpelRefine(Data);
1369    
1370          for(i = 0; i < 5; i++) {          for(i = 0; i < 5; i++) {
1371                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */
1372                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
1373          }          }
1374    
1375          if (MotionFlags & XVID_ME_QUARTERPELREFINE16)          if (Data->qpel) {
                 if ((!(MotionFlags & XVID_ME_QUARTERPELREFINE16_BITS)) || (Data->iMinSAD[0] < 200*(int)iQuant)) {  
                         Data->qpel_precision = 1;  
1376                          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,                          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1377                                          pParam->width, pParam->height, Data->iFcode, 1, 0);                                          pParam->width, pParam->height, Data->iFcode, 1, 0);
1378                    Data->qpel_precision = 1;
1379                    if (MotionFlags & XVID_ME_QUARTERPELREFINE16)
1380                          SubpelRefine(Data);                          SubpelRefine(Data);
1381                  }                  }
1382    
1383          if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0;          if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30)
1384                    inter4v = 0;
         if (inter4v && (!(VopFlags & XVID_VOP_MODEDECISION_BITS) ||  
                         (!(MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS)) || (!(MotionFlags & XVID_ME_HALFPELREFINE8_BITS)) ||  
                         ((!(MotionFlags & XVID_ME_EXTSEARCH_BITS)) && (!(MotionFlags&XVID_ME_EXTSEARCH8)) ))) {  
                 // if decision is BITS-based and all refinement steps will be done in BITS domain, there is no reason to call this loop  
1385    
1386            if (inter4v) {
1387                  SearchData Data8;                  SearchData Data8;
1388                  memcpy(&Data8, Data, sizeof(SearchData)); //quick copy of common data                  memcpy(&Data8, Data, sizeof(SearchData)); /* quick copy of common data */
1389    
1390                  Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);                  Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
1391                  Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);                  Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
# Line 1398  Line 1393 
1393                  Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);                  Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
1394    
1395                  if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_BITS))) {                  if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_BITS))) {
1396                          // chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, there is no reason to compute it                          /* chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, it will not be used */
1397                          int sumx = 0, sumy = 0;                          int sumx = 0, sumy = 0;
                         const int div = 1 + Data->qpel;  
                         const VECTOR * const mv = Data->qpel ? pMB->qmvs : pMB->mvs;  
1398    
1399                          for (i = 0; i < 4; i++) {                          if (Data->qpel)
1400                                  sumx += mv[i].x / div;                                  for (i = 1; i < 5; i++) {
1401                                  sumy += mv[i].y / div;                                          sumx += Data->currentQMV[i].x/2;
1402                                            sumy += Data->currentQMV[i].y/2;
1403                                    }
1404                            else
1405                                    for (i = 1; i < 5; i++) {
1406                                            sumx += Data->currentMV[i].x;
1407                                            sumy += Data->currentMV[i].y;
1408                          }                          }
1409    
1410                          Data->iMinSAD[1] += ChromaSAD(  (sumx >> 3) + roundtab_76[sumx & 0xf],                          Data->iMinSAD[1] += ChromaSAD(  (sumx >> 3) + roundtab_76[sumx & 0xf],
1411                                                                                          (sumy >> 3) + roundtab_76[sumy & 0xf], Data);                                                                                          (sumy >> 3) + roundtab_76[sumy & 0xf], Data);
1412                  }                  }
1413          }          } else Data->iMinSAD[1] = 4096*256;
   
         inter4v = ModeDecision(iQuant, Data, inter4v, pMB, pMBs, x, y, pParam, MotionFlags, VopFlags);  
   
         if (Data->rrv) {  
                         Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);  
                         Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);  
         }  
   
         if (inter4v == MODE_INTER) {  
                 pMB->mode = MODE_INTER;  
                 pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];  
                 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];  
   
                 if(Data->qpel) {  
                         pMB->qmvs[0] = pMB->qmvs[1]  
                                 = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];  
                         pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;  
                         pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;  
                 } else {  
                         pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;  
                         pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;  
                 }  
   
         } else if (inter4v == MODE_INTER4V) {  
                 pMB->mode = MODE_INTER4V;  
                 pMB->sad16 = Data->iMinSAD[0];  
         } else { // INTRA mode  
                 SkipMacroblockP(pMB, 0); // not skip, but similar enough  
                 pMB->mode = MODE_INTRA;  
         }  
   
1414  }  }
1415    
1416  static void  static void
# Line 1473  Line 1441 
1441          *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;          *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;
1442    
1443          if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {          if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {
                 if (Data->rrv) i = 2; else i = 1;  
1444    
1445                  Data->Ref = OldData->Ref + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1));                  if (Data->rrv) i = 16; else i = 8;
1446                  Data->RefH = OldData->RefH + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1));  
1447                  Data->RefV = OldData->RefV + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->RefP[0] = OldData->RefP[0] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
1448                  Data->RefHV = OldData->RefHV + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->RefP[1] = OldData->RefP[1] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
1449                    Data->RefP[2] = OldData->RefP[2] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
1450                    Data->RefP[3] = OldData->RefP[3] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
1451    
1452                  Data->Cur = OldData->Cur + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1));
1453                  Data->qpel_precision = 0;                  Data->qpel_precision = 0;
1454    
1455                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,
# Line 1490  Line 1459 
1459                  else CheckCandidate = CheckCandidate16no4v;                  else CheckCandidate = CheckCandidate16no4v;
1460    
1461                  if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_BITS))) {                  if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_BITS))) {
1462                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD                          int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */
1463    
1464                          MainSearchFunc *MainSearchPtr;                          MainSearchFunc *MainSearchPtr;
1465                          if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = SquareSearch;                          if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = SquareSearch;
# Line 1500  Line 1469 
1469                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255);                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255);
1470    
1471                          if(*(Data->iMinSAD) < temp_sad) {                          if(*(Data->iMinSAD) < temp_sad) {
1472                                          Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector                                          Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */
1473                                          Data->currentQMV->y = 2 * Data->currentMV->y;                                          Data->currentQMV->y = 2 * Data->currentMV->y;
1474                          }                          }
1475                  }                  }
1476    
1477                  if (MotionFlags & XVID_ME_HALFPELREFINE8) {                  if (MotionFlags & XVID_ME_HALFPELREFINE8) {
1478                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD                          int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */
1479    
1480                          SubpelRefine(Data); // perform halfpel refine of current best vector                          SubpelRefine(Data); /* perform halfpel refine of current best vector */
1481    
1482                          if(*(Data->iMinSAD) < temp_sad) { // we have found a better match                          if(*(Data->iMinSAD) < temp_sad) { /* we have found a better match */
1483                                  Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector                                  Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */
1484                                  Data->currentQMV->y = 2 * Data->currentMV->y;                                  Data->currentQMV->y = 2 * Data->currentMV->y;
1485                          }                          }
1486                  }                  }
# Line 1558  Line 1527 
1527                                                          const uint32_t mode_curr)                                                          const uint32_t mode_curr)
1528  {  {
1529    
1530          // [0] is prediction          /* [0] is prediction */
1531          pmv[0].x = EVEN(pmv[0].x); pmv[0].y = EVEN(pmv[0].y);          pmv[0].x = EVEN(pmv[0].x); pmv[0].y = EVEN(pmv[0].y);
1532    
1533          pmv[1].x = pmv[1].y = 0; // [1] is zero          pmv[1].x = pmv[1].y = 0; /* [1] is zero */
1534    
1535          pmv[2] = ChoosePred(pMB, mode_curr);          pmv[2] = ChoosePred(pMB, mode_curr);
1536          pmv[2].x = EVEN(pmv[2].x); pmv[2].y = EVEN(pmv[2].y);          pmv[2].x = EVEN(pmv[2].x); pmv[2].y = EVEN(pmv[2].y);
1537    
1538          if ((y != 0)&&(x != (int)(iWcount+1))) {                        // [3] top-right neighbour          if ((y != 0)&&(x != (int)(iWcount+1))) {                        /* [3] top-right neighbour */
1539                  pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr);                  pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr);
1540                  pmv[3].x = EVEN(pmv[3].x); pmv[3].y = EVEN(pmv[3].y);                  pmv[3].x = EVEN(pmv[3].x); pmv[3].y = EVEN(pmv[3].y);
1541          } else pmv[3].x = pmv[3].y = 0;          } else pmv[3].x = pmv[3].y = 0;
# Line 1612  Line 1581 
1581          *Data->iMinSAD = MV_MAX_ERROR;          *Data->iMinSAD = MV_MAX_ERROR;
1582          Data->iFcode = iFcode;          Data->iFcode = iFcode;
1583          Data->qpel_precision = 0;          Data->qpel_precision = 0;
1584          Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; // reset chroma-sad cache          Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; /* reset chroma-sad cache */
1585    
1586          Data->Ref = pRef->y + (x + y * Data->iEdgedWidth) * 16;          Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16;
1587          Data->RefH = pRefH + (x + y * Data->iEdgedWidth) * 16;          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16;
1588          Data->RefV = pRefV + (x + y * Data->iEdgedWidth) * 16;          Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16;
1589          Data->RefHV = pRefHV + (x + y * Data->iEdgedWidth) * 16;          Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16;
1590          Data->RefCU = pRef->u + (x + y * Data->iEdgedWidth/2) * 8;          Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
1591          Data->RefCV = pRef->v + (x + y * Data->iEdgedWidth/2) * 8;          Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;
1592    
1593          Data->predMV = *predMV;          Data->predMV = *predMV;
1594    
# Line 1634  Line 1603 
1603          Data->currentMV->x = Data->currentMV->y = 0;          Data->currentMV->x = Data->currentMV->y = 0;
1604          CheckCandidate = CheckCandidate16no4v;          CheckCandidate = CheckCandidate16no4v;
1605    
1606  // main loop. checking all predictions          /* main loop. checking all predictions */
1607          for (i = 0; i < 7; i++) {          for (i = 0; i < 7; i++) {
1608                  if (!(mask = make_mask(pmv, i)) ) continue;                  if (!(mask = make_mask(pmv, i)) ) continue;
1609                  CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);                  CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
# Line 1657  Line 1626 
1626                  SubpelRefine(Data);                  SubpelRefine(Data);
1627          }          }
1628    
1629  // three bits are needed to code backward mode. four for forward          /* three bits are needed to code backward mode. four for forward */
1630    
1631          if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16;          if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16;
1632          else *Data->iMinSAD += 3 * Data->lambda16;          else *Data->iMinSAD += 3 * Data->lambda16;
# Line 1681  Line 1650 
1650          }          }
1651    
1652          if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV;          if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV;
1653          else *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search          else *(Data->currentMV+1) = *Data->currentMV; /* we store currmv for interpolate search */
1654  }  }
1655    
1656  static void  static void
# Line 1697  Line 1666 
1666          const int div = 1 + Data->qpel;          const int div = 1 + Data->qpel;
1667          int k;          int k;
1668          const uint32_t stride = Data->iEdgedWidth/2;          const uint32_t stride = Data->iEdgedWidth/2;
1669  //this is not full chroma compensation, only it's fullpel approximation. should work though          /* this is not full chroma compensation, only it's fullpel approximation. should work though */
1670    
1671          for (k = 0; k < 4; k++) {          for (k = 0; k < 4; k++) {
1672                  dy += Data->directmvF[k].y / div;                  dy += Data->directmvF[k].y / div;
1673                  dx += Data->directmvF[0].x / div;                  dx += Data->directmvF[k].x / div;
1674                  b_dy += Data->directmvB[0].y / div;                  b_dy += Data->directmvB[k].y / div;
1675                  b_dx += Data->directmvB[0].x / div;                  b_dx += Data->directmvB[k].x / div;
1676          }          }
1677    
1678          dy = (dy >> 3) + roundtab_76[dy & 0xf];          dy = (dy >> 3) + roundtab_76[dy & 0xf];
# Line 1716  Line 1685 
1685                                          b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,                                          b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,
1686                                          stride);                                          stride);
1687    
1688          if (sum >= 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) return; //no skip          if (sum >= 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) return; /* no skip */
1689    
1690          sum += sad8bi(pCur->v + 8*x + 8 * y * stride,          sum += sad8bi(pCur->v + 8*x + 8 * y * stride,
1691                                          f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2,                                          f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2,
1692                                          b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,                                          b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,
1693                                          stride);                                          stride);
1694    
1695          if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) pMB->mode = MODE_DIRECT_NONE_MV; //skipped          if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) {
1696                    pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */
1697                    for (k = 0; k < 4; k++) {
1698                            pMB->qmvs[k] = pMB->mvs[k];
1699                            pMB->b_qmvs[k] = pMB->b_mvs[k];
1700                    }
1701            }
1702  }  }
1703    
1704  static __inline uint32_t  static __inline uint32_t
# Line 1751  Line 1726 
1726          MainSearchFunc *MainSearchPtr;          MainSearchFunc *MainSearchPtr;
1727    
1728          *Data->iMinSAD = 256*4096;          *Data->iMinSAD = 256*4096;
1729          Data->Ref = f_Ref->y + k;          Data->RefP[0] = f_Ref->y + k;
1730          Data->RefH = f_RefH + k;          Data->RefP[2] = f_RefH + k;
1731          Data->RefV = f_RefV + k;          Data->RefP[1] = f_RefV + k;
1732          Data->RefHV = f_RefHV + k;          Data->RefP[3] = f_RefHV + k;
1733          Data->bRef = b_Ref->y + k;          Data->b_RefP[0] = b_Ref->y + k;
1734          Data->bRefH = b_RefH + k;          Data->b_RefP[2] = b_RefH + k;
1735          Data->bRefV = b_RefV + k;          Data->b_RefP[1] = b_RefV + k;
1736          Data->bRefHV = b_RefHV + k;          Data->b_RefP[3] = b_RefHV + k;
1737          Data->RefCU = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;          Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;
1738          Data->RefCV = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;          Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;
1739          Data->b_RefCU = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;          Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;
1740          Data->b_RefCV = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;          Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;
1741    
1742          k = Data->qpel ? 4 : 2;          k = Data->qpel ? 4 : 2;
1743          Data->max_dx = k * (pParam->width - x * 16);          Data->max_dx = k * (pParam->width - x * 16);
# Line 1782  Line 1757 
1757                  if ( (pMB->b_mvs[k].x > Data->max_dx) | (pMB->b_mvs[k].x < Data->min_dx)                  if ( (pMB->b_mvs[k].x > Data->max_dx) | (pMB->b_mvs[k].x < Data->min_dx)
1758                          | (pMB->b_mvs[k].y > Data->max_dy) | (pMB->b_mvs[k].y < Data->min_dy) ) {                          | (pMB->b_mvs[k].y > Data->max_dy) | (pMB->b_mvs[k].y < Data->min_dy) ) {
1759    
1760                          *best_sad = 256*4096; // in that case, we won't use direct mode                          *best_sad = 256*4096; /* in that case, we won't use direct mode */
1761                          pMB->mode = MODE_DIRECT; // just to make sure it doesn't say "MODE_DIRECT_NONE_MV"                          pMB->mode = MODE_DIRECT; /* just to make sure it doesn't say "MODE_DIRECT_NONE_MV" */
1762                          pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0;                          pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0;
1763                          return 256*4096;                          return 256*4096;
1764                  }                  }
# Line 1800  Line 1775 
1775    
1776          CheckCandidate(0, 0, 255, &k, Data);          CheckCandidate(0, 0, 255, &k, Data);
1777    
1778  // initial (fast) skip decision          /* initial (fast) skip decision */
1779          if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (2 + Data->chroma?1:0)) {          if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (Data->chroma?3:2)) {
1780                  //possible skip                  /* possible skip */
1781                  if (Data->chroma) {                  if (Data->chroma) {
1782                          pMB->mode = MODE_DIRECT_NONE_MV;                          pMB->mode = MODE_DIRECT_NONE_MV;
1783                          return *Data->iMinSAD; // skip.                          return *Data->iMinSAD; /* skip. */
1784                  } else {                  } else {
1785                          SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data);                          SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data);
1786                          if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; // skip.                          if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; /* skip. */
1787                  }                  }
1788          }          }
1789    
1790            *Data->iMinSAD += Data->lambda16;
1791          skip_sad = *Data->iMinSAD;          skip_sad = *Data->iMinSAD;
1792    
1793  //      DIRECT MODE DELTA VECTOR SEARCH.          /*
1794  //      This has to be made more effective, but at the moment I'm happy it's running at all           * DIRECT MODE DELTA VECTOR SEARCH.
1795             * This has to be made more effective, but at the moment I'm happy it's running at all
1796             */
1797    
1798          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;
1799                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
# Line 1828  Line 1806 
1806          *best_sad = *Data->iMinSAD;          *best_sad = *Data->iMinSAD;
1807    
1808          if (Data->qpel || b_mb->mode == MODE_INTER4V) pMB->mode = MODE_DIRECT;          if (Data->qpel || b_mb->mode == MODE_INTER4V) pMB->mode = MODE_DIRECT;
1809          else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation          else pMB->mode = MODE_DIRECT_NO4V; /* for faster compensation */
1810    
1811          pMB->pmvs[3] = *Data->currentMV;          pMB->pmvs[3] = *Data->currentMV;
1812    
# Line 1886  Line 1864 
1864          SearchData bData;          SearchData bData;
1865    
1866          fData->qpel_precision = 0;          fData->qpel_precision = 0;
1867          memcpy(&bData, fData, sizeof(SearchData)); //quick copy of common data          memcpy(&bData, fData, sizeof(SearchData)); /* quick copy of common data */
1868          *fData->iMinSAD = 4096*256;          *fData->iMinSAD = 4096*256;
1869          bData.currentMV++; bData.currentQMV++;          bData.currentMV++; bData.currentQMV++;
1870          fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;          fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;
1871    
1872          i = (x + y * fData->iEdgedWidth) * 16;          i = (x + y * fData->iEdgedWidth) * 16;
         bData.bRef = fData->Ref = f_Ref->y + i;  
         bData.bRefH = fData->RefH = f_RefH + i;  
         bData.bRefV = fData->RefV = f_RefV + i;  
         bData.bRefHV = fData->RefHV = f_RefHV + i;  
         bData.Ref = fData->bRef = b_Ref->y + i;  
         bData.RefH = fData->bRefH = b_RefH + i;  
         bData.RefV = fData->bRefV = b_RefV + i;  
         bData.RefHV = fData->bRefHV = b_RefHV + i;  
         bData.b_RefCU = fData->RefCU = f_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8;  
         bData.b_RefCV = fData->RefCV = f_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8;  
         bData.RefCU = fData->b_RefCU = b_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8;  
         bData.RefCV = fData->b_RefCV = b_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8;  
1873    
1874            bData.b_RefP[0] = fData->RefP[0] = f_Ref->y + i;
1875            bData.b_RefP[2] = fData->RefP[2] = f_RefH + i;
1876            bData.b_RefP[1] = fData->RefP[1] = f_RefV + i;
1877            bData.b_RefP[3] = fData->RefP[3] = f_RefHV + i;
1878            bData.RefP[0] = fData->b_RefP[0] = b_Ref->y + i;
1879            bData.RefP[2] = fData->b_RefP[2] = b_RefH + i;
1880            bData.RefP[1] = fData->b_RefP[1] = b_RefV + i;
1881            bData.RefP[3] = fData->b_RefP[3] = b_RefHV + i;
1882            bData.b_RefP[4] = fData->RefP[4] = f_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8;
1883            bData.b_RefP[5] = fData->RefP[5] = f_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8;
1884            bData.RefP[4] = fData->b_RefP[4] = b_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8;
1885            bData.RefP[5] = fData->b_RefP[5] = b_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8;
1886    
1887          bData.bpredMV = fData->predMV = *f_predMV;          bData.bpredMV = fData->predMV = *f_predMV;
1888          fData->bpredMV = bData.predMV = *b_predMV;          fData->bpredMV = bData.predMV = *b_predMV;
# Line 1925  Line 1903 
1903    
1904          CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);          CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);
1905    
1906  //diamond          /* diamond */
1907          do {          do {
1908                  iDirection = 255;                  iDirection = 255;
1909                  // forward MV moves                  /* forward MV moves */
1910                  i = fData->currentMV[0].x; j = fData->currentMV[0].y;                  i = fData->currentMV[0].x; j = fData->currentMV[0].y;
1911    
1912                  CheckCandidateInt(i + 1, j, 0, &iDirection, fData);                  CheckCandidateInt(i + 1, j, 0, &iDirection, fData);
# Line 1936  Line 1914 
1914                  CheckCandidateInt(i - 1, j, 0, &iDirection, fData);                  CheckCandidateInt(i - 1, j, 0, &iDirection, fData);
1915                  CheckCandidateInt(i, j - 1, 0, &iDirection, fData);                  CheckCandidateInt(i, j - 1, 0, &iDirection, fData);
1916    
1917                  // backward MV moves                  /* backward MV moves */
1918                  i = fData->currentMV[1].x; j = fData->currentMV[1].y;                  i = fData->currentMV[1].x; j = fData->currentMV[1].y;
1919                  fData->currentMV[2] = fData->currentMV[0];                  fData->currentMV[2] = fData->currentMV[0];
1920                  CheckCandidateInt(i + 1, j, 0, &iDirection, &bData);                  CheckCandidateInt(i + 1, j, 0, &iDirection, &bData);
# Line 1946  Line 1924 
1924    
1925          } while (!(iDirection));          } while (!(iDirection));
1926    
1927  //qpel refinement          /* qpel refinement */
1928          if (fData->qpel) {          if (fData->qpel) {
1929                  if (*fData->iMinSAD > *best_sad + 500) return;                  if (*fData->iMinSAD > *best_sad + 500) return;
1930                  CheckCandidate = CheckCandidateInt;                  CheckCandidate = CheckCandidateInt;
# Line 1963  Line 1941 
1941                  SubpelRefine(&bData);                  SubpelRefine(&bData);
1942          }          }
1943    
1944          *fData->iMinSAD += (2+3) * fData->lambda16; // two bits are needed to code interpolate mode.          *fData->iMinSAD += (2+3) * fData->lambda16; /* two bits are needed to code interpolate mode. */
1945    
1946          if (*fData->iMinSAD < *best_sad) {          if (*fData->iMinSAD < *best_sad) {
1947                  *best_sad = *fData->iMinSAD;                  *best_sad = *fData->iMinSAD;
# Line 1991  Line 1969 
1969                                           FRAMEINFO * const frame,                                           FRAMEINFO * const frame,
1970                                           const int32_t time_bp,                                           const int32_t time_bp,
1971                                           const int32_t time_pp,                                           const int32_t time_pp,
1972                                           // forward (past) reference                                           /* forward (past) reference */
1973                                           const MACROBLOCK * const f_mbs,                                           const MACROBLOCK * const f_mbs,
1974                                           const IMAGE * const f_ref,                                           const IMAGE * const f_ref,
1975                                           const IMAGE * const f_refH,                                           const IMAGE * const f_refH,
1976                                           const IMAGE * const f_refV,                                           const IMAGE * const f_refV,
1977                                           const IMAGE * const f_refHV,                                           const IMAGE * const f_refHV,
1978                                           // backward (future) reference                                           /* backward (future) reference */
1979                                           const FRAMEINFO * const b_reference,                                           const FRAMEINFO * const b_reference,
1980                                           const IMAGE * const b_ref,                                           const IMAGE * const b_ref,
1981                                           const IMAGE * const b_refH,                                           const IMAGE * const b_refH,
# Line 2015  Line 1993 
1993          const int32_t TRB = time_pp - time_bp;          const int32_t TRB = time_pp - time_bp;
1994          const int32_t TRD = time_pp;          const int32_t TRD = time_pp;
1995    
1996  // some pre-inintialized data for the rest of the search          /* some pre-inintialized data for the rest of the search */
1997    
1998          SearchData Data;          SearchData Data;
1999          int32_t iMinSAD;          int32_t iMinSAD;
# Line 2032  Line 2010 
2010          Data.chroma = frame->motion_flags & XVID_ME_CHROMA8;          Data.chroma = frame->motion_flags & XVID_ME_CHROMA8;
2011          Data.temp = temp;          Data.temp = temp;
2012    
2013          Data.RefQ = f_refV->u; // a good place, also used in MC (for similar purpose)          Data.RefQ = f_refV->u; /* a good place, also used in MC (for similar purpose) */
2014          // note: i==horizontal, j==vertical  
2015            /* note: i==horizontal, j==vertical */
2016          for (j = 0; j < pParam->mb_height; j++) {          for (j = 0; j < pParam->mb_height; j++) {
2017    
2018                  f_predMV = b_predMV = zeroMV;   /* prediction is reset at left boundary */                  f_predMV = b_predMV = zeroMV;   /* prediction is reset at left boundary */
# Line 2052  Line 2031 
2031                          Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;                          Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;
2032                          Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8;                          Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8;
2033                          Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8;                          Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8;
2034                            pMB->quant = frame->quant;
2035    
2036  /* direct search comes first, because it (1) checks for SKIP-mode  /* direct search comes first, because it (1) checks for SKIP-mode
2037          and (2) sets very good predictions for forward and backward search */          and (2) sets very good predictions for forward and backward search */
# Line 2068  Line 2048 
2048    
2049                          if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }                          if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }
2050    
2051                          // forward search                          /* forward search */
2052                          SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y,                          SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y,
2053                                                  &frame->image, i, j,                                                  &frame->image, i, j,
2054                                                  frame->motion_flags,                                                  frame->motion_flags,
# Line 2076  Line 2056 
2056                                                  pMB, &f_predMV, &best_sad,                                                  pMB, &f_predMV, &best_sad,
2057                                                  MODE_FORWARD, &Data);                                                  MODE_FORWARD, &Data);
2058    
2059                          // backward search                          /* backward search */
2060                          SearchBF(b_ref, b_refH->y, b_refV->y, b_refHV->y,                          SearchBF(b_ref, b_refH->y, b_refV->y, b_refHV->y,
2061                                                  &frame->image, i, j,                                                  &frame->image, i, j,
2062                                                  frame->motion_flags,                                                  frame->motion_flags,
# Line 2084  Line 2064 
2064                                                  pMB, &b_predMV, &best_sad,                                                  pMB, &b_predMV, &best_sad,
2065                                                  MODE_BACKWARD, &Data);                                                  MODE_BACKWARD, &Data);
2066    
2067                          // interpolate search comes last, because it uses data from forward and backward as prediction                          /* interpolate search comes last, because it uses data from forward and backward as prediction */
2068                          SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y,                          SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y,
2069                                                  b_ref, b_refH->y, b_refV->y, b_refHV->y,                                                  b_ref, b_refH->y, b_refV->y, b_refHV->y,
2070                                                  &frame->image,                                                  &frame->image,
# Line 2096  Line 2076 
2076                                                  pMB, &best_sad,                                                  pMB, &best_sad,
2077                                                  &Data);                                                  &Data);
2078    
2079  // final skip decision                          /* final skip decision */
2080                          if ( (skip_sad < frame->quant * MAX_SAD00_FOR_SKIP * 2)                          if ( (skip_sad < frame->quant * MAX_SAD00_FOR_SKIP * 2)
2081                                          && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) )                                          && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) )
2082                                  SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data);                                  SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data);
# Line 2136  Line 2116 
2116  {  {
2117    
2118          int i, mask;          int i, mask;
2119            int quarterpel = (pParam->vol_flags & XVID_VOL_QUARTERPEL)? 1: 0;
2120          VECTOR pmv[3];          VECTOR pmv[3];
2121          MACROBLOCK * pMB = &pMBs[x + y * pParam->mb_width];          MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width];
2122    
2123          for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;          for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;
2124    
2125          //median is only used as prediction. it doesn't have to be real          /* median is only used as prediction. it doesn't have to be real */
2126          if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0;          if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0;
2127          else          else
2128                  if (x == 1) //left macroblock does not have any vector now                  if (x == 1) /* left macroblock does not have any vector now */
2129                          Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median                          Data->predMV = (pMB - pParam->mb_width)->mvs[0]; /* top instead of median */
2130                  else if (y == 1) // top macroblock doesn't have it's vector                  else if (y == 1) /* top macroblock doesn't have it's vector */
2131                          Data->predMV = (pMB - 1)->mvs[0]; // left instead of median                          Data->predMV = (pMB - 1)->mvs[0]; /* left instead of median */
2132                          else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median                          else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); /* else median */
2133    
2134          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
2135          pParam->width, pParam->height, Data->iFcode - (pParam->vol_flags&XVID_VOL_QUARTERPEL?1:0), 0, Data->rrv);          pParam->width, pParam->height, Data->iFcode - quarterpel, 0, 0);
2136    
2137          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;
2138          Data->Ref = pRef + (x + y * pParam->edged_width) * 16;          Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16;
2139    
2140          pmv[1].x = EVEN(pMB->mvs[0].x);          pmv[1].x = EVEN(pMB->mvs[0].x);
2141          pmv[1].y = EVEN(pMB->mvs[0].y);          pmv[1].y = EVEN(pMB->mvs[0].y);
# Line 2164  Line 2145 
2145    
2146          CheckCandidate32I(0, 0, 255, &i, Data);          CheckCandidate32I(0, 0, 255, &i, Data);
2147    
2148          if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) {          if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) {
2149    
2150                  if (!(mask = make_mask(pmv, 1)))                  if (!(mask = make_mask(pmv, 1)))
2151                          CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data);                          CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data);
2152                  if (!(mask = make_mask(pmv, 2)))                  if (!(mask = make_mask(pmv, 2)))
2153                          CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data);                          CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data);
2154    
2155                  if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) // diamond only if needed                  if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) /* diamond only if needed */
2156                          DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);                          DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);
2157            }
2158    
2159                  for (i = 0; i < 4; i++) {                  for (i = 0; i < 4; i++) {
2160                          MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width];                          MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width];
# Line 2181  Line 2163 
2163                          MB->sad16 = Data->iMinSAD[i+1];                          MB->sad16 = Data->iMinSAD[i+1];
2164                  }                  }
2165          }          }
 }  
2166    
2167  #define INTRA_BIAS              2500  #define INTRA_THRESH    1700
2168  #define INTRA_THRESH    1500  #define INTER_THRESH    1200
 #define INTER_THRESH    1400  
2169    
2170  int  int
2171  MEanalysis(     const IMAGE * const pRef,  MEanalysis(     const IMAGE * const pRef,
2172                          FRAMEINFO * const Current,                          const FRAMEINFO * const Current,
2173                          MBParam * const pParam,                          const MBParam * const pParam,
2174                          int maxIntra, //maximum number if non-I frames                          const int maxIntra, /* maximum number if non-I frames */
2175                          int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame                          const int intraCount, /* number of non-I frames after last I frame; 0 if we force P/B frame */
2176                          int bCount) // number of B frames in a row                          const int bCount,  /* number of B frames in a row */
2177                            const int b_thresh)
2178  {  {
2179          uint32_t x, y, intra = 0;          uint32_t x, y, intra = 0;
2180          int sSAD = 0;          int sSAD = 0;
2181          MACROBLOCK * const pMBs = Current->mbs;          MACROBLOCK * const pMBs = Current->mbs;
2182          const IMAGE * const pCurrent = &Current->image;          const IMAGE * const pCurrent = &Current->image;
2183          int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH;          int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + 10*b_thresh;
2184            int s = 0, blocks = 0;
2185    
2186          int32_t iMinSAD[5], temp[5];          int32_t iMinSAD[5], temp[5];
2187          VECTOR currentMV[5];          VECTOR currentMV[5];
# Line 2208  Line 2190 
2190          Data.currentMV = currentMV;          Data.currentMV = currentMV;
2191          Data.iMinSAD = iMinSAD;          Data.iMinSAD = iMinSAD;
2192          Data.iFcode = Current->fcode;          Data.iFcode = Current->fcode;
         Data.rrv = Current->vop_flags & XVID_VOP_REDUCED;  
2193          Data.temp = temp;          Data.temp = temp;
2194          CheckCandidate = CheckCandidate32I;          CheckCandidate = CheckCandidate32I;
2195    
2196          if (intraCount != 0 && intraCount < 10) // we're right after an I frame          if (intraCount != 0) {
2197                  IntraThresh += 4 * (intraCount - 10) * (intraCount - 10);                  if (intraCount < 10) /* we're right after an I frame */
2198                            IntraThresh += 15* (intraCount - 10) * (intraCount - 10);
2199          else          else
2200                  if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec                          if ( 5*(maxIntra - intraCount) < maxIntra) /* we're close to maximum. 2 sec when max is 10 sec */
2201                          IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra;                                  IntraThresh -= (IntraThresh * (maxIntra - 8*(maxIntra - intraCount)))/maxIntra;
2202            }
2203    
2204          InterThresh += 400 * (1 - bCount);          InterThresh -= (350 - 8*b_thresh) * bCount;
2205          if (InterThresh < 300) InterThresh = 300;          if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh;
2206    
2207          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();
2208    
2209          for (y = 1; y < pParam->mb_height-1; y += 2) {          for (y = 1; y < pParam->mb_height-1; y += 2) {
2210                  for (x = 1; x < pParam->mb_width-1; x += 2) {                  for (x = 1; x < pParam->mb_width-1; x += 2) {
2211                          int i;                          int i;
2212                            blocks += 4;
2213    
2214                          if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV;                          if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV;
2215                            else { /* extrapolation of the vector found for last frame */
2216                                    pMBs[x + y * pParam->mb_width].mvs[0].x =
2217                                            (pMBs[x + y * pParam->mb_width].mvs[0].x * (bCount+1) ) / bCount;
2218                                    pMBs[x + y * pParam->mb_width].mvs[0].y =
2219                                            (pMBs[x + y * pParam->mb_width].mvs[0].y * (bCount+1) ) / bCount;
2220                            }
2221    
2222                          MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data);                          MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data);
2223    
# Line 2239  Line 2229 
2229                                                                          pParam->edged_width);                                                                          pParam->edged_width);
2230                                          if (dev + IntraThresh < pMB->sad16) {                                          if (dev + IntraThresh < pMB->sad16) {
2231                                                  pMB->mode = MODE_INTRA;                                                  pMB->mode = MODE_INTRA;
2232                                                  if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return I_VOP;                                                  if (++intra > ((pParam->mb_height-2)*(pParam->mb_width-2))/2) return I_VOP;
2233                                          }                                          }
2234                                  }                                  }
2235                                    if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) s++;
2236    
2237                                  sSAD += pMB->sad16;                                  sSAD += pMB->sad16;
2238                          }                          }
2239                  }                  }
2240          }          }
2241          sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2);  
2242  //      if (sSAD > IntraThresh + INTRA_BIAS) return I_VOP;          sSAD /= blocks;
2243    
2244            if (b_thresh < 20) {
2245                    s = (10*s) / blocks;
2246                    if (s > 4) sSAD += (s - 2) * (40 - 2*b_thresh); /* static block - looks bad when in bframe... */
2247            }
2248    
2249          if (sSAD > InterThresh ) return P_VOP;          if (sSAD > InterThresh ) return P_VOP;
2250          emms();          emms();
2251          return B_VOP;          return B_VOP;
   
2252  }  }
2253    
2254    
# Line 2265  Line 2262 
2262                                  const IMAGE * const pRefHV      )                                  const IMAGE * const pRefHV      )
2263  {  {
2264    
2265          const int deltax=8;             // upper bound for difference between a MV and it's neighbour MVs          const int deltax=8;             /* upper bound for difference between a MV and it's neighbour MVs */
2266          const int deltay=8;          const int deltay=8;
2267          const int grad=512;             // lower bound for deviation in MB          const int grad=512;             /* lower bound for deviation in MB */
2268    
2269          WARPPOINTS gmc;          WARPPOINTS gmc;
2270    
# Line 2289  Line 2286 
2286                                                  gmc.duv[2].x= gmc.duv[2].y = 0;                                                  gmc.duv[2].x= gmc.duv[2].y = 0;
2287                                          return gmc; }                                          return gmc; }
2288    
2289  // filter mask of all blocks          /* filter mask of all blocks */
2290    
2291          for (my = 1; my < (uint32_t)MBh-1; my++)          for (my = 1; my < (uint32_t)MBh-1; my++)
2292          for (mx = 1; mx < (uint32_t)MBw-1; mx++)          for (mx = 1; mx < (uint32_t)MBw-1; mx++)
# Line 2301  Line 2298 
2298                  if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED)                  if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED)
2299                          continue;                          continue;
2300    
2301                  if ( ( (ABS(mv.x -   (pMB-1)->mvs[0].x) < deltax) && (ABS(mv.y -   (pMB-1)->mvs[0].y) < deltay) )                  if ( ( (abs(mv.x -   (pMB-1)->mvs[0].x) < deltax) && (abs(mv.y -   (pMB-1)->mvs[0].y) < deltay) )
2302                  &&   ( (ABS(mv.x -   (pMB+1)->mvs[0].x) < deltax) && (ABS(mv.y -   (pMB+1)->mvs[0].y) < deltay) )                  &&   ( (abs(mv.x -   (pMB+1)->mvs[0].x) < deltax) && (abs(mv.y -   (pMB+1)->mvs[0].y) < deltay) )
2303                  &&   ( (ABS(mv.x - (pMB-MBw)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB-MBw)->mvs[0].y) < deltay) )                  &&   ( (abs(mv.x - (pMB-MBw)->mvs[0].x) < deltax) && (abs(mv.y - (pMB-MBw)->mvs[0].y) < deltay) )
2304                  &&   ( (ABS(mv.x - (pMB+MBw)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB+MBw)->mvs[0].y) < deltay) ) )                  &&   ( (abs(mv.x - (pMB+MBw)->mvs[0].x) < deltax) && (abs(mv.y - (pMB+MBw)->mvs[0].y) < deltay) ) )
2305                          MBmask[mbnum]=1;                          MBmask[mbnum]=1;
2306          }          }
2307    
# Line 2379  Line 2376 
2376                                  continue;                                  continue;
2377    
2378                          oldnum++;                          oldnum++;
2379                          meanx += ABS(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x );                          meanx += fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x );
2380                          meany += ABS(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y );                          meany += fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y );
2381                  }                  }
2382    
2383          if (4*meanx > oldnum)   /* better fit than 0.25 is useless */          if (4*meanx > oldnum)   /* better fit than 0.25 is useless */
# Line 2407  Line 2404 
2404                          if (!MBmask[mbnum])                          if (!MBmask[mbnum])
2405                                  continue;                                  continue;
2406    
2407                          if  ( ( ABS(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ) > meanx )                          if  ( ( fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ) > meanx )
2408                             || ( ABS(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ) > meany ) )                                  || ( fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ) > meany ) )
2409                                  MBmask[mbnum]=0;                                  MBmask[mbnum]=0;
2410                          else                          else
2411                                  num++;                                  num++;
# Line 2430  Line 2427 
2427                  gmc.duv[2].x=0;                  gmc.duv[2].x=0;
2428                  gmc.duv[2].y=0;                  gmc.duv[2].y=0;
2429          }          }
2430  //      fprintf(stderr,"wp1 = ( %4d, %4d)  wp2 = ( %4d, %4d) \n", gmc.duv[0].x, gmc.duv[0].y, gmc.duv[1].x, gmc.duv[1].y);  /*      fprintf(stderr,"wp1 = ( %4d, %4d)  wp2 = ( %4d, %4d) \n", gmc.duv[0].x, gmc.duv[0].y, gmc.duv[1].x, gmc.duv[1].y); */
2431    
2432          free(MBmask);          free(MBmask);
2433    
2434          return gmc;          return gmc;
2435  }  }
2436    
2437  // functions which perform BITS-based search/bitcount  /* functions which perform BITS-based search/bitcount */
2438    
2439  static int  static int
2440  CountMBBitsInter(SearchData * const Data,  CountMBBitsInter(SearchData * const Data,
# Line 2458  Line 2455 
2455                  Data->qpel_precision = 1;                  Data->qpel_precision = 1;
2456                  CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data);                  CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data);
2457    
2458                  //checking if this vector is perfect. if it is, we stop.                  if (MotionFlags & (XVID_ME_HALFPELREFINE16_BITS | XVID_ME_EXTSEARCH_BITS)) { /* we have to prepare for halfpixel-precision search */
                 if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0)  
                         return 0; //quick stop  
   
                 if (MotionFlags & (XVID_ME_HALFPELREFINE16_BITS | XVID_ME_EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search  
2459                          for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i];                          for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i];
2460                          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,                          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
2461                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);
# Line 2471  Line 2464 
2464                                  CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);                                  CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);
2465                  }                  }
2466    
2467          } else { // not qpel          } else { /* not qpel */
2468    
2469                  CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);                  CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);
                 //checking if this vector is perfect. if it is, we stop.  
                 if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0) {  
                         return 0; //inter  
                 }  
2470          }          }
2471    
2472          if (MotionFlags&XVID_ME_EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection);          if (MotionFlags&XVID_ME_EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
# Line 2485  Line 2474 
2474          if (MotionFlags&XVID_ME_HALFPELREFINE16_BITS) SubpelRefine(Data);          if (MotionFlags&XVID_ME_HALFPELREFINE16_BITS) SubpelRefine(Data);
2475    
2476          if (Data->qpel) {          if (Data->qpel) {
2477                  if (MotionFlags&(XVID_ME_EXTSEARCH_BITS | XVID_ME_HALFPELREFINE16_BITS)) { // there was halfpel-precision search                  if (MotionFlags&(XVID_ME_EXTSEARCH_BITS | XVID_ME_HALFPELREFINE16_BITS)) { /* there was halfpel-precision search */
2478                          for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) {                          for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) {
2479                                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // we have found a better match                                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* we have found a better match */
2480                                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;                                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
2481                          }                          }
2482    
2483                          // preparing for qpel-precision search                          /* preparing for qpel-precision search */
2484                          Data->qpel_precision = 1;                          Data->qpel_precision = 1;
2485                          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,                          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
2486                                          pParam->width, pParam->height, Data->iFcode, 1, 0);                                          pParam->width, pParam->height, Data->iFcode, 1, 0);
# Line 2499  Line 2488 
2488                  if (MotionFlags&XVID_ME_QUARTERPELREFINE16_BITS) SubpelRefine(Data);                  if (MotionFlags&XVID_ME_QUARTERPELREFINE16_BITS) SubpelRefine(Data);
2489          }          }
2490    
2491          if (MotionFlags&XVID_ME_CHECKPREDICTION_BITS) { //let's check vector equal to prediction          if (MotionFlags&XVID_ME_CHECKPREDICTION_BITS) { /* let's check vector equal to prediction */
2492                  VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV;                  VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV;
2493                  if (!(Data->predMV.x == v->x && Data->predMV.y == v->y))                  if (!(Data->predMV.x == v->x && Data->predMV.y == v->y))
2494                          CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data);                          CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data);
# Line 2507  Line 2496 
2496          return Data->iMinSAD[0];          return Data->iMinSAD[0];
2497  }  }
2498    
   
2499  static int  static int
2500  CountMBBitsInter4v(const SearchData * const Data,  CountMBBitsInter4v(const SearchData * const Data,
2501                                          MACROBLOCK * const pMB, const MACROBLOCK * const pMBs,                                          MACROBLOCK * const pMB, const MACROBLOCK * const pMBs,
# Line 2519  Line 2507 
2507          int cbp = 0, bits = 0, t = 0, i, iDirection;          int cbp = 0, bits = 0, t = 0, i, iDirection;
2508          SearchData Data2, *Data8 = &Data2;          SearchData Data2, *Data8 = &Data2;
2509          int sumx = 0, sumy = 0;          int sumx = 0, sumy = 0;
2510          int16_t in[64], coeff[64];          int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64;
2511            uint8_t * ptr;
2512    
2513          memcpy(Data8, Data, sizeof(SearchData));          memcpy(Data8, Data, sizeof(SearchData));
2514          CheckCandidate = CheckCandidateBits8;          CheckCandidate = CheckCandidateBits8;
2515    
2516          for (i = 0; i < 4; i++) {          for (i = 0; i < 4; i++) { /* for all luma blocks */
2517    
2518                  Data8->iMinSAD = Data->iMinSAD + i + 1;                  Data8->iMinSAD = Data->iMinSAD + i + 1;
2519                  Data8->currentMV = Data->currentMV + i + 1;                  Data8->currentMV = Data->currentMV + i + 1;
2520                  Data8->currentQMV = Data->currentQMV + i + 1;                  Data8->currentQMV = Data->currentQMV + i + 1;
2521                  Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth);                  Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2522                  Data8->Ref = Data->Ref + 8*((i&1) + (i>>1)*Data->iEdgedWidth);                  Data8->RefP[0] = Data->RefP[0] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2523                  Data8->RefH = Data->RefH + 8*((i&1) + (i>>1)*Data->iEdgedWidth);                  Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2524                  Data8->RefV = Data->RefV + 8*((i&1) + (i>>1)*Data->iEdgedWidth);                  Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2525                  Data8->RefHV = Data->RefHV + 8*((i&1) + (i>>1)*Data->iEdgedWidth);                  Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2526    
2527                  if(Data->qpel) {                  if(Data->qpel) {
2528                          Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i);                          Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i);
# Line 2547  Line 2537 
2537                  get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8,                  get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8,
2538                                          pParam->width, pParam->height, Data8->iFcode, Data8->qpel, 0);                                          pParam->width, pParam->height, Data8->iFcode, Data8->qpel, 0);
2539    
2540                  *Data8->iMinSAD += t;                  *Data8->iMinSAD += BITS_MULT*t;
2541    
2542                  Data8->qpel_precision = Data8->qpel;                  Data8->qpel_precision = Data8->qpel;
2543                  // checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far)                  /* checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) */
2544                  if (Data8->qpel) {                  {
2545                          if (!(Data8->currentQMV->x == backup[i+1].x && Data8->currentQMV->y == backup[i+1].y))                          VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV;
2546                                  CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8);                          if (!MVequal (*v, backup[i+1]) )
                 } else {  
                         if (!(Data8->currentMV->x == backup[i+1].x && Data8->currentMV->y == backup[i+1].y))  
2547                                  CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8);                                  CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8);
2548                  }                  }
2549    
2550                  if (Data8->qpel) {                  if (Data8->qpel) {
2551                          if (MotionFlags&XVID_ME_HALFPELREFINE8_BITS || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_BITS)) { // halfpixel motion search follows                          if (MotionFlags&XVID_ME_HALFPELREFINE8_BITS || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_BITS)) { /* halfpixel motion search follows */
2552                                  int32_t s = *Data8->iMinSAD;                                  int32_t s = *Data8->iMinSAD;
2553                                  Data8->currentMV->x = Data8->currentQMV->x/2;                                  Data8->currentMV->x = Data8->currentQMV->x/2;
2554                                  Data8->currentMV->y = Data8->currentQMV->y/2;                                  Data8->currentMV->y = Data8->currentQMV->y/2;
# Line 2574  Line 2562 
2562                                  if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS)                                  if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS)
2563                                          SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);                                          SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);
2564    
2565                                  if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8);                                  if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS)
2566                                            SubpelRefine(Data8);
2567    
2568                                  if(s > *Data8->iMinSAD) { //we have found a better match                                  if(s > *Data8->iMinSAD) { /* we have found a better match */
2569                                          Data8->currentQMV->x = 2*Data8->currentMV->x;                                          Data8->currentQMV->x = 2*Data8->currentMV->x;
2570                                          Data8->currentQMV->y = 2*Data8->currentMV->y;                                          Data8->currentQMV->y = 2*Data8->currentMV->y;
2571                                  }                                  }
# Line 2588  Line 2577 
2577                          }                          }
2578                          if (MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS) SubpelRefine(Data8);                          if (MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS) SubpelRefine(Data8);
2579    
2580                  } else // not qpel                  } else { /* not qpel */
                         if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8); //halfpel mode, halfpel refinement  
2581    
2582                  //checking vector equal to predicion                          if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS) /* extsearch */
2583                                    SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);
2584    
2585                            if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS)
2586                                    SubpelRefine(Data8); /* halfpel refinement */
2587                    }
2588    
2589                    /* checking vector equal to predicion */
2590                  if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_BITS) {                  if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_BITS) {
2591                          const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV;                          const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV;
2592                          if (!(Data8->predMV.x == v->x && Data8->predMV.y == v->y))                          if (!MVequal(*v, Data8->predMV))
2593                                  CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8);                                  CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8);
2594                  }                  }
2595    
2596                  bits += *Data8->iMinSAD;                  bits += *Data8->iMinSAD;
2597                  if (bits >= Data->iMinSAD[0]) break; // no chances for INTER4V                  if (bits >= Data->iMinSAD[0]) return bits; /* no chances for INTER4V */
2598    
2599                  // MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else                  /* MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else */
2600                  if(Data->qpel) {                  if(Data->qpel) {
2601                          pMB->pmvs[i].x = Data8->currentQMV->x - Data8->predMV.x;                          pMB->pmvs[i].x = Data8->currentQMV->x - Data8->predMV.x;
2602                          pMB->pmvs[i].y = Data8->currentQMV->y - Data8->predMV.y;                          pMB->pmvs[i].y = Data8->currentQMV->y - Data8->predMV.y;
# Line 2617  Line 2612 
2612                  pMB->mvs[i] = *Data8->currentMV;                  pMB->mvs[i] = *Data8->currentMV;
2613                  pMB->sad8[i] = 4 * *Data8->iMinSAD;                  pMB->sad8[i] = 4 * *Data8->iMinSAD;
2614                  if (Data8->temp[0]) cbp |= 1 << (5 - i);                  if (Data8->temp[0]) cbp |= 1 << (5 - i);
         }  
2615    
2616          if (bits < *Data->iMinSAD) { // there is still a chance for inter4v mode. let's check chroma          } /* /for all luma blocks */
2617                  const uint8_t * ptr;  
2618            bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;
2619    
2620            /* let's check chroma */
2621                  sumx = (sumx >> 3) + roundtab_76[sumx & 0xf];                  sumx = (sumx >> 3) + roundtab_76[sumx & 0xf];
2622                  sumy = (sumy >> 3) + roundtab_76[sumy & 0xf];                  sumy = (sumy >> 3) + roundtab_76[sumy & 0xf];
2623    
2624                  //chroma U          /* chroma U */
2625                  ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefCU, 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);          ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);
2626                  transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2);                  transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2);
2627                  fdct(in);          bits += Block_CalcBits(coeff, in, Data->iQuant, Data->quant_type, &cbp, 4);
2628                  if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16);  
2629                  else i = quant4_inter(coeff, in, Data->lambda16);          if (bits >= *Data->iMinSAD) return bits;
                 if (i > 0) {  
                         bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);  
                         cbp |= 1 << (5 - 4);  
                 }  
2630    
2631                  if (bits < *Data->iMinSAD) { // still possible          /* chroma V */
2632                          //chroma V          ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);
                         ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefCV, 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);  
2633                          transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2);                          transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2);
2634                          fdct(in);          bits += Block_CalcBits(coeff, in, Data->iQuant, Data->quant_type, &cbp, 5);
2635                          if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16);  
2636                          else i = quant4_inter(coeff, in, Data->lambda16);          bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len;
                         if (i > 0) {  
                                 bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);  
                                 cbp |= 1 << (5 - 5);  
                         }  
                         bits += cbpy_tab[15-(cbp>>2)].len;  
                         bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len;  
                 }  
         }  
2637    
2638          return bits;          return bits;
2639  }  }
2640    
   
2641  static int  static int
2642  CountMBBitsIntra(const SearchData * const Data)  CountMBBitsIntra(const SearchData * const Data)
2643  {  {
2644          int bits = 1; //this one is ac/dc prediction flag. always 1.          int bits = BITS_MULT*1; /* this one is ac/dc prediction flag bit */
2645          int cbp = 0, i, t, dc = 0, b_dc = 1024;          int cbp = 0, i, dc = 0;
2646          const uint32_t iQuant = Data->lambda16;          int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64;
         int16_t in[64], coeff[64];  
2647    
2648          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
                 uint32_t iDcScaler = get_dc_scaler(iQuant, 1);  
   
2649                  int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth);                  int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2650                  transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth);                  transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth);
2651                  fdct(in);                  bits += Block_CalcBitsIntra(coeff, in, Data->iQuant, Data->quant_type, &cbp, i, &dc);
                 b_dc = dc;  
                 dc = in[0];  
                 in[0] -= b_dc;  
                 if (Data->lambda8 == 0) quant_intra_c(coeff, in, iQuant, iDcScaler);  
                 else quant4_intra_c(coeff, in, iQuant, iDcScaler);  
   
                 b_dc = dc;  
                 dc = coeff[0];  
                 if (i != 0) coeff[0] -= b_dc;  
   
                 bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcy_tab[coeff[0] + 255].len;;  
                 Data->temp[i] = t;  
                 if (t != 0)  cbp |= 1 << (5 - i);  
                 if (bits >= Data->iMinSAD[0]) break;  
         }  
   
         if (bits < Data->iMinSAD[0]) { // INTRA still looks good, let's add chroma  
                 uint32_t iDcScaler = get_dc_scaler(iQuant, 0);  
                 //chroma U  
                 transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2);  
                 fdct(in);  
                 in[0] -= 1024;  
                 if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler);  
                 else quant4_intra(coeff, in, iQuant, iDcScaler);  
   
                 bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len;  
                 if (t != 0) cbp |= 1 << (5 - 4);  
                 Data->temp[4] = t;  
2652    
2653                  if (bits < Data->iMinSAD[0]) {                  if (bits >= Data->iMinSAD[0]) return bits;
2654                          //chroma V          }
                         transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2);  
                         fdct(in);  
                         in[0] -= 1024;  
                         if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler);  
                         else quant4_intra(coeff, in, iQuant, iDcScaler);  
2655    
2656                          bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len;          bits += BITS_MULT*xvid_cbpy_tab[cbp>>2].len;
                         if (t != 0) cbp |= 1 << (5 - 5);  
2657    
2658                          Data->temp[5] = t;          /*chroma U */
2659            transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2);
2660            bits += Block_CalcBitsIntra(coeff, in, Data->iQuant, Data->quant_type, &cbp, 4, &dc);
2661    
2662                          bits += t = cbpy_tab[cbp>>2].len;          if (bits >= Data->iMinSAD[0]) return bits;
                         Data->temp[6] = t;  
2663    
2664                          bits += t = mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len;          /* chroma V */
2665                          Data->temp[7] = t;          transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2);
2666            bits += Block_CalcBitsIntra(coeff, in, Data->iQuant, Data->quant_type, &cbp, 5, &dc);
2667    
2668                  }          bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len;
         }  
2669    
2670          return bits;          return bits;
2671  }  }

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

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