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

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

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

revision 662, Tue Nov 19 13:48:42 2002 UTC revision 700, Sun Dec 8 14:57:09 2002 UTC
# Line 52  Line 52 
52  #define CHECK_CANDIDATE(X,Y,D) { \  #define CHECK_CANDIDATE(X,Y,D) { \
53  (*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); }  (*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); }
54    
55  #define GET_REFERENCE(X, Y, REF) { \  #define iDiamondSize 2
         switch ( (((X)&1)<<1) + ((Y)&1) ) \  
         { \  
                 case 0 : REF = (uint8_t *)data->Ref + (X)/2 + ((Y)/2)*(data->iEdgedWidth); break; \  
                 case 1 : REF = (uint8_t *)data->RefV + (X)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \  
                 case 2 : REF = (uint8_t *)data->RefH + ((X)-1)/2 + ((Y)/2)*(data->iEdgedWidth); break; \  
                 default : REF = (uint8_t *)data->RefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \  
         } \  
 }  
 // I hate those macros :/  
 #define GET_REFERENCE2(X, Y, REF) { \  
         switch ( (((X)&1)<<1) + ((Y)&1) ) \  
         { \  
                 case 0 : REF = (uint8_t *)data->bRef + (X)/2 + ((Y)/2)*(data->iEdgedWidth); break; \  
                 case 1 : REF = (uint8_t *)data->bRefV + (X)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \  
                 case 2 : REF = (uint8_t *)data->bRefH + ((X)-1)/2 + ((Y)/2)*(data->iEdgedWidth); break; \  
                 default : REF = (uint8_t *)data->bRefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \  
         } \  
 }  
56    
57    static VECTOR
58    GlobalMotionEst(const MACROBLOCK * const pMBs,
59                                    const MBParam * const pParam, const uint32_t iFcode);
60    
 #define iDiamondSize 2  
61    
62  static __inline int  static __inline int
63  d_mv_bits(int x, int y, const uint32_t iFcode)  d_mv_bits(int x, int y, const uint32_t iFcode)
# Line 137  Line 121 
121          return sad;          return sad;
122  }  }
123    
124    static __inline const uint8_t *
125  /* CHECK_CANDIATE FUNCTIONS START */  GetReference(const int x, const int y, const int dir, const SearchData * const data)
   
   
 static void  
 CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  
126  {  {
127          int t;  //      dir : 0 = forward, 1 = backward
128          const uint8_t * Reference;          switch ( (dir << 2) | ((x&1)<<1) | (y&1) ) {
129                    case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth);
130          if (( x > data->max_dx) || ( x < data->min_dx)                  case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth);
131                  || ( y > data->max_dy) || (y < data->min_dy)) return;                  case 2 : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth);
132                    case 3 : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth);
133                    case 4 : return data->bRef + x/2 + (y/2)*(data->iEdgedWidth);
134                    case 5 : return data->bRefV + x/2 + ((y-1)/2)*(data->iEdgedWidth);
135                    case 6 : return data->bRefH + (x-1)/2 + (y/2)*(data->iEdgedWidth);
136                    default : return data->bRefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth);
137    
         switch ( ((x&1)<<1) + (y&1) ) {  
                 case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;  
                 case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
                 case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;  
                 default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
138          }          }
   
         data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1);  
   
         if (data->qpel) t = d_mv_bits(2*x - data->predQMV.x, 2*y - data->predQMV.y, data->iFcode);  
         else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);  
   
         data->temp[0] += (data->lambda16 * t * data->temp[0])/1000;  
         data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100;  
   
         if (data->chroma) data->temp[0] += ChromaSAD(x, y, data);  
   
         if (data->temp[0] < data->iMinSAD[0]) {  
                 data->iMinSAD[0] = data->temp[0];  
                 data->currentMV[0].x = x; data->currentMV[0].y = y;  
                 *dir = Direction; }  
   
         if (data->temp[1] < data->iMinSAD[1]) {  
                 data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; }  
         if (data->temp[2] < data->iMinSAD[2]) {  
                 data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; }  
         if (data->temp[3] < data->iMinSAD[3]) {  
                 data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; }  
         if (data->temp[4] < data->iMinSAD[4]) {  
                 data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; }  
   
139  }  }
140    
141  static void  static uint8_t *
142  CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  Interpolate8x8qpel(const int x, const int y, const int block, const int dir, const SearchData * const data)
143  {  {
144          int32_t sad;  // create or find a qpel-precision reference picture; return pointer to it
145          const uint8_t * Reference;          uint8_t * Reference = (uint8_t *)data->RefQ + 16*dir;
146            const int32_t iEdgedWidth = data->iEdgedWidth;
147            const uint32_t rounding = data->rounding;
148            const int halfpel_x = x/2;
149            const int halfpel_y = y/2;
150            const uint8_t *ref1, *ref2, *ref3, *ref4;
151    
152          if (( x > data->max_dx) || ( x < data->min_dx)          ref1 = GetReference(halfpel_x, halfpel_y, dir, data); // this reference is used in all cases
153                  || ( y > data->max_dy) || (y < data->min_dy)) return;          ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
154            switch( ((x&1)<<1) + (y&1) ) {
155            case 0: // pure halfpel position
156                    Reference = (uint8_t *) GetReference(halfpel_x, halfpel_y, dir, data);
157                    Reference += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
158                    break;
159    
160          switch ( ((x&1)<<1) + (y&1) )          case 1: // x halfpel, y qpel - top or bottom during qpel refinement
161          {                  ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);
162                  case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
163                  case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
164                  case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;                  break;
                 default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
         }  
165    
166          sad = sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR);          case 2: // x qpel, y halfpel - left or right during qpel refinement
167          if (data->qpel) //only to be used in b-frames' ME                  ref2 = GetReference(x - halfpel_x, halfpel_y, dir, data);
168                  sad += (data->lambda16 * d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode) * sad)/1000;                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
169          else                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
170                  sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000;                  break;
171    
172          if (sad < *(data->iMinSAD)) {          default: // x and y in qpel resolution - the "corners" (top left/right and
173                  *(data->iMinSAD) = sad;                           // bottom left/right) during qpel refinement
174                  data->currentMV[0].x = x; data->currentMV[0].y = y;                  ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);
175                  *dir = Direction; }                  ref3 = GetReference(x - halfpel_x, halfpel_y, dir, data);
176                    ref4 = GetReference(x - halfpel_x, y - halfpel_y, dir, data);
177                    ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
178                    ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
179                    ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
180                    interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
181                    break;
182            }
183            return Reference;
184  }  }
185    
186  static void  static uint8_t *
187  CheckCandidate16_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  Interpolate16x16qpel(const int x, const int y, const int dir, const SearchData * const data)
   
 // CheckCandidate16 variant which expects x and y in quarter pixel resolution  
 // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)  
 // around currentMV!  
188  {  {
189          int t;  // create or find a qpel-precision reference picture; return pointer to it
190          uint8_t * Reference = (uint8_t *)data->RefQ;          uint8_t * Reference = (uint8_t *)data->RefQ + 16*dir;
191            const int32_t iEdgedWidth = data->iEdgedWidth;
192            const uint32_t rounding = data->rounding;
193            const int halfpel_x = x/2;
194            const int halfpel_y = y/2;
195          const uint8_t *ref1, *ref2, *ref3, *ref4;          const uint8_t *ref1, *ref2, *ref3, *ref4;
         VECTOR halfpelMV = *(data->currentMV);  
   
         int32_t iEdgedWidth = data->iEdgedWidth;  
         uint32_t rounding = data->rounding;  
   
         if (( x > data->max_dx) || ( x < data->min_dx)  
                 || ( y > data->max_dy) || (y < data->min_dy)) return;  
   
         GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this refenrence is used in all cases  
         switch( ((x&1)<<1) + (y&1) )  
         {  
         case 0: // pure halfpel position - shouldn't happen during a refinement step  
                 GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);  
                 break;  
196    
197            ref1 = GetReference(halfpel_x, halfpel_y, dir, data); // this reference is used in all cases
198            switch( ((x&1)<<1) + (y&1) ) {
199            case 0: // pure halfpel position
200                    return (uint8_t *) GetReference(halfpel_x, halfpel_y, dir, data);
201          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          case 1: // x halfpel, y qpel - top or bottom during qpel refinement
202                  GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);                  ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);
203                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
204                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
205                  interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8);
206                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);
207                  break;                  break;
208    
209          case 2: // x qpel, y halfpel - left or right during qpel refinement          case 2: // x qpel, y halfpel - left or right during qpel refinement
210                  GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);                  ref2 = GetReference(x - halfpel_x, halfpel_y, dir, data);
211                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
212                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
213                  interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8);
214                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);
215                  break;                  break;
216    
217          default: // x and y in qpel resolution - the "corners" (top left/right and          default: // x and y in qpel resolution - the "corners" (top left/right and
218                           // bottom left/right) during qpel refinement                           // bottom left/right) during qpel refinement
219                  GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);                  ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);
220                  GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3);                  ref3 = GetReference(x - halfpel_x, halfpel_y, dir, data);
221                  GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4);                  ref4 = GetReference(x - halfpel_x, y - halfpel_y, dir, data);
   
222                  interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);                  interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
223                  interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);                  interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);
224                  interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);                  interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);
225                  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);
226                  break;                  break;
227          }          }
228            return Reference;
229    }
230    
231    /* CHECK_CANDIATE FUNCTIONS START */
232    
233    static void
234    CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
235    {
236            int t, xc, yc;
237            const uint8_t * Reference;
238            VECTOR * current;
239    
240            if (( x > data->max_dx) || ( x < data->min_dx)
241                    || ( y > data->max_dy) || (y < data->min_dy)) return;
242    
243            if (data->qpel_precision) { // x and y are in 1/4 precision
244                    Reference = Interpolate16x16qpel(x, y, 0, data);
245                    t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
246                    xc = x/2; yc = y/2; //for chroma sad
247                    current = data->currentQMV;
248            } else {
249                    switch ( ((x&1)<<1) + (y&1) ) {
250                            case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
251                            case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
252                            case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
253                            default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
254                    }
255                    if (data->qpel) t = d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode);
256                    else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
257                    current = data->currentMV;
258                    xc = x; yc = y;
259            }
260    
261          data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1);          data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1);
262    
         t = d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode);  
263          data->temp[0] += (data->lambda16 * t * data->temp[0])/1000;          data->temp[0] += (data->lambda16 * t * data->temp[0])/1000;
264          data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100;          data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100;
265    
266          if (data->chroma)          if (data->chroma) data->temp[0] += ChromaSAD(xc, yc, data);
                 data->temp[0] += ChromaSAD(x/2, y/2, data);  
267    
268          if (data->temp[0] < data->iMinSAD[0]) {          if (data->temp[0] < data->iMinSAD[0]) {
269                  data->iMinSAD[0] = data->temp[0];                  data->iMinSAD[0] = data->temp[0];
270                  data->currentQMV[0].x = x; data->currentQMV[0].y = y;                  current[0].x = x; current[0].y = y;
271          /*      *dir = Direction;*/ }                  *dir = Direction; }
272    
273          if (data->temp[1] < data->iMinSAD[1]) {          if (data->temp[1] < data->iMinSAD[1]) {
274                  data->iMinSAD[1] = data->temp[1]; data->currentQMV[1].x = x; data->currentQMV[1].y = y; }                  data->iMinSAD[1] = data->temp[1]; current[1].x = x; current[1].y= y; }
275          if (data->temp[2] < data->iMinSAD[2]) {          if (data->temp[2] < data->iMinSAD[2]) {
276                  data->iMinSAD[2] = data->temp[2]; data->currentQMV[2].x = x; data->currentQMV[2].y = y; }                  data->iMinSAD[2] = data->temp[2]; current[2].x = x; current[2].y = y; }
277          if (data->temp[3] < data->iMinSAD[3]) {          if (data->temp[3] < data->iMinSAD[3]) {
278                  data->iMinSAD[3] = data->temp[3]; data->currentQMV[3].x = x; data->currentQMV[3].y = y; }                  data->iMinSAD[3] = data->temp[3]; current[3].x = x; current[3].y = y; }
279          if (data->temp[4] < data->iMinSAD[4]) {          if (data->temp[4] < data->iMinSAD[4]) {
280                  data->iMinSAD[4] = data->temp[4]; data->currentQMV[4].x = x; data->currentQMV[4].y = y; }                  data->iMinSAD[4] = data->temp[4]; current[4].x = x; current[4].y = y; }
281    
282  }  }
283    
284  static void  static void
285  CheckCandidate16no4v_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
   
 // CheckCandidate16no4v variant which expects x and y in quarter pixel resolution  
 // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)  
 // around currentMV!  
 // this function is for B-frames' search only  
286  {  {
         uint8_t * Reference = (uint8_t *)data->RefQ;  
         const uint8_t *ref1, *ref2, *ref3, *ref4;  
         VECTOR halfpelMV = *(data->currentMV);  
   
         int32_t iEdgedWidth = data->iEdgedWidth;  
287          int32_t sad;          int32_t sad;
288            const uint8_t * Reference;
289            int t;
290            VECTOR * current;
291    
292          if (( x > data->max_dx) || ( x < data->min_dx)          if (( x > data->max_dx) || ( x < data->min_dx)
293                  || ( y > data->max_dy) || (y < data->min_dy)) return;                  || ( y > data->max_dy) || (y < data->min_dy)) return;
294    
295          GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this refenrence is used in all cases          if (data->qpel_precision) { // x and y are in 1/4 precision
296          switch( ((x&1)<<1) + (y&1) )                  Reference = Interpolate16x16qpel(x, y, 0, data);
297          {                  t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
298          case 0: // pure halfpel position - shouldn't happen during a refinement step                  current = data->currentQMV;
299                  GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);          } else {
300                  break;                  switch ( ((x&1)<<1) + (y&1) ) {
301                            case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
302          case 1: // x halfpel, y qpel - top or bottom during qpel refinement                          case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
303                  GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);                          case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
304                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, 0);                          default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
305                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, 0);                  }
306                  interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);                  if (data->qpel) t = d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode);
307                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);                  else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
308                  break;                  current = data->currentMV;
   
         case 2: // x qpel, y halfpel - left or right during qpel refinement  
                 GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);  
                 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, 0);  
                 interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, 0);  
                 interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
   
         default: // x and y in qpel resolution - the "corners" (top left/right and  
                          // bottom left/right) during qpel refinement  
                 GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);  
                 GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3);  
                 GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4);  
   
                 interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, 0);  
                 interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0);  
                 interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
309          }          }
310    
311          sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);          sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);
312          sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000;          sad += (data->lambda16 * t * sad)/1000;
313    
314          if (sad < data->iMinSAD[0]) {          if (sad < *(data->iMinSAD)) {
315                  data->iMinSAD[0] = sad;                  *(data->iMinSAD) = sad;
316                  data->currentQMV[0].x = x; data->currentQMV[0].y = y;                  current->x = x; current->y = y;
317          /*      *dir = Direction;*/ }                  *dir = Direction; }
318  }  }
319    
320  static void  static void
# Line 376  Line 340 
340  CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)
341  {  {
342          int32_t sad;          int32_t sad;
343          const int xb = data->currentMV[1].x;          int xb, yb, t;
         const int yb = data->currentMV[1].y;  
344          const uint8_t *ReferenceF, *ReferenceB;          const uint8_t *ReferenceF, *ReferenceB;
345            VECTOR *current;
346    
347          if (( xf > data->max_dx) || ( xf < data->min_dx)          if (( xf > data->max_dx) || ( xf < data->min_dx)
348                  || ( yf > data->max_dy) || (yf < data->min_dy)) return;                  || ( yf > data->max_dy) || (yf < data->min_dy)) return;
349    
350          switch ( ((xf&1)<<1) + (yf&1) ) {          if (data->qpel_precision) {
351                  case 0 : ReferenceF = data->Ref + xf/2 + (yf/2)*(data->iEdgedWidth); break;                  ReferenceF = Interpolate16x16qpel(xf, yf, 0, data);
352                  case 1 : ReferenceF = data->RefV + xf/2 + ((yf-1)/2)*(data->iEdgedWidth); break;                  xb = data->currentQMV[1].x; yb = data->currentQMV[1].y;
353                  case 2 : ReferenceF = data->RefH + (xf-1)/2 + (yf/2)*(data->iEdgedWidth); break;                  current = data->currentQMV;
354                  default : ReferenceF = data->RefHV + (xf-1)/2 + ((yf-1)/2)*(data->iEdgedWidth); break;                  ReferenceB = Interpolate16x16qpel(xb, yb, 1, data);
355          }                  t = d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode)
356                                     + d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode);
357          switch ( ((xb&1)<<1) + (yb&1) ) {          } else {
358                  case 0 : ReferenceB = data->bRef + xb/2 + (yb/2)*(data->iEdgedWidth); break;                  ReferenceF = Interpolate16x16qpel(2*xf, 2*yf, 0, data);
359                  case 1 : ReferenceB = data->bRefV + xb/2 + ((yb-1)/2)*(data->iEdgedWidth); break;                  xb = data->currentMV[1].x; yb = data->currentMV[1].y;
360                  case 2 : ReferenceB = data->bRefH + (xb-1)/2 + (yb/2)*(data->iEdgedWidth); break;                  ReferenceB = Interpolate16x16qpel(2*xb, 2*yb, 1, data);
361                  default : ReferenceB = data->bRefHV + (xb-1)/2 + ((yb-1)/2)*(data->iEdgedWidth); break;                  current = data->currentMV;
         }  
   
         sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);  
   
362          if (data->qpel)          if (data->qpel)
363                  sad += (data->lambda16 *                          t = d_mv_bits(2*xf - data->predMV.x, 2*yf - data->predMV.y, data->iFcode)
364                          ( d_mv_bits(2*xf - data->predMV.x, 2*yf - data->predMV.y, data->iFcode) +                                           + d_mv_bits(2*xb - data->bpredMV.x, 2*yb - data->bpredMV.y, data->iFcode);
                           d_mv_bits(2*xb - data->bpredMV.x, 2*yb - data->bpredMV.y, data->iFcode)) * sad)/1000;  
365          else          else
366                  sad += (data->lambda16 *                          t = d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode)
367                          ( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) +                                           + d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode);
                           d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode)) * sad)/1000;  
   
         if (sad < *(data->iMinSAD)) {  
                 *(data->iMinSAD) = sad;  
                 data->currentMV->x = xf; data->currentMV->y = yf;  
                 *dir = Direction; }  
 }  
   
   
 static void  
 CheckCandidateInt_qpel(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)  
 {  
 // CheckCandidateInt variant which expects x and y in quarter pixel resolution  
   
         int32_t sad;  
         const int xb = data->currentQMV[1].x;  
         const int yb = data->currentQMV[1].y;  
         uint8_t * ReferenceF = (uint8_t *)data->RefQ;  
         uint8_t * ReferenceB = (uint8_t *)data->RefQ + 16;  
         const uint8_t *ref1, *ref2, *ref3, *ref4;  
         VECTOR halfpelMV;  
         const int32_t iEdgedWidth = data->iEdgedWidth;  
   
         if (( xf > data->max_dx) || ( xf < data->min_dx)  
                 || ( yf > data->max_dy) || (yf < data->min_dy)) return;  
   
         halfpelMV.x = xf/2; //forward first  
         halfpelMV.y = yf/2;  
         GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases  
         switch( ((xf&1)<<1) + (yf&1) )  
         {  
         case 0: // pure halfpel position - shouldn't happen during a refinement step  
                 GET_REFERENCE(halfpelMV.x, halfpelMV.y, ReferenceF);  
                 break;  
   
         case 1: // x halfpel, y qpel - top or bottom during qpel refinement  
                 GET_REFERENCE(halfpelMV.x, yf - halfpelMV.y, ref2);  
                 interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
   
         case 2: // x qpel, y halfpel - left or right during qpel refinement  
                 GET_REFERENCE(xf - halfpelMV.x, halfpelMV.y, ref2);  
                 interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
   
         default: // x and y in qpel resolution - the "corners" (top left/right and  
                          // bottom left/right) during qpel refinement  
                 GET_REFERENCE(halfpelMV.x, yf - halfpelMV.y, ref2);  
                 GET_REFERENCE(xf - halfpelMV.x, halfpelMV.y, ref3);  
                 GET_REFERENCE(xf - halfpelMV.x, yf - halfpelMV.y, ref4);  
   
                 interpolate8x8_avg4(ReferenceF, ref1, ref2, ref3, ref4, iEdgedWidth, 0);  
                 interpolate8x8_avg4(ReferenceF+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0);  
                 interpolate8x8_avg4(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg4(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
         }  
   
         halfpelMV.x = xb/2; //backward  
         halfpelMV.y = yb/2;  
         GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases  
         switch( ((xb&1)<<1) + (yb&1) )  
         {  
         case 0: // pure halfpel position - shouldn't happen during a refinement step  
                 GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ReferenceB);  
                 break;  
   
         case 1: // x halfpel, y qpel - top or bottom during qpel refinement  
                 GET_REFERENCE2(halfpelMV.x, yb - halfpelMV.y, ref2);  
                 interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
   
         case 2: // x qpel, y halfpel - left or right during qpel refinement  
                 GET_REFERENCE2(xb - halfpelMV.x, halfpelMV.y, ref2);  
                 interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
   
         default: // x and y in qpel resolution - the "corners" (top left/right and  
                          // bottom left/right) during qpel refinement  
                 GET_REFERENCE2(halfpelMV.x, yb - halfpelMV.y, ref2);  
                 GET_REFERENCE2(xb - halfpelMV.x, halfpelMV.y, ref3);  
                 GET_REFERENCE2(xb - halfpelMV.x, yb - halfpelMV.y, ref4);  
   
                 interpolate8x8_avg4(ReferenceB, ref1, ref2, ref3, ref4, iEdgedWidth, 0);  
                 interpolate8x8_avg4(ReferenceB+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0);  
                 interpolate8x8_avg4(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg4(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
368          }          }
369    
370          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
371            sad += (data->lambda16 * t * sad)/1000;
         sad += (data->lambda16 *  
                         ( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) +  
                           d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode)) * sad)/1000;  
372    
373          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
374                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
375                  data->currentQMV->x = xf; data->currentQMV->y = yf;                  current->x = xf; current->y = yf;
376                  *dir = Direction; }                  *dir = Direction; }
377  }  }
378    
# Line 547  Line 403 
403                          || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )                          || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
404                          || ( 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;
405    
406                  switch ( ((mvs.x&1)<<1) + (mvs.y&1) ) {                  if (!data->qpel) {
407                          case 0 : ReferenceF = data->Ref + mvs.x/2 + (mvs.y/2)*(data->iEdgedWidth); break;                          mvs.x *= 2; mvs.y *= 2;
408                          case 1 : ReferenceF = data->RefV + mvs.x/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;                          b_mvs.x *= 2; b_mvs.y *= 2; //we move to qpel precision anyway
                         case 2 : ReferenceF = data->RefH + (mvs.x-1)/2 + (mvs.y/2)*(data->iEdgedWidth); break;  
                         default : ReferenceF = data->RefHV + (mvs.x-1)/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;  
                 }  
   
                 switch ( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {  
                         case 0 : ReferenceB = data->bRef + b_mvs.x/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;  
                         case 1 : ReferenceB = data->bRefV + b_mvs.x/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;  
                         case 2 : ReferenceB = data->bRefH + (b_mvs.x-1)/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;  
                         default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;  
409                  }                  }
410                    ReferenceF = Interpolate8x8qpel(mvs.x, mvs.y, k, 0, data);
411                    ReferenceB = Interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data);
412    
413                  sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),                  sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
414                                                  ReferenceF + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),                                                  ReferenceF, ReferenceB,
                                                 ReferenceB + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
415                                                  data->iEdgedWidth);                                                  data->iEdgedWidth);
416                  if (sad > *(data->iMinSAD)) return;                  if (sad > *(data->iMinSAD)) return;
417          }          }
# Line 576  Line 424 
424                  *dir = Direction; }                  *dir = Direction; }
425  }  }
426    
   
 static void  
 CheckCandidateDirect_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  
 {  
         int32_t sad = 0;  
         int k;  
         VECTOR mvs, b_mvs, halfpelMV;  
         const uint8_t *ref1, *ref2, *ref3, *ref4;  
         uint8_t *ReferenceF, *ReferenceB;  
         const uint32_t iEdgedWidth = data->iEdgedWidth;  
   
         if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;  
   
         for (k = 0; k < 4; k++) {  
                 ReferenceF = (uint8_t *)data->RefQ;  
                 ReferenceB = (uint8_t *)data->RefQ + 64;  
   
                 mvs.x = data->directmvF[k].x + x;  
                 b_mvs.x = ((x == 0) ?  
                         data->directmvB[k].x  
                         : mvs.x - data->referencemv[k].x);  
   
                 mvs.y = data->directmvF[k].y + y;  
                 b_mvs.y = ((y == 0) ?  
                         data->directmvB[k].y  
                         : mvs.y - data->referencemv[k].y);  
   
                 if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )  
                         || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )  
                         || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )  
                         || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;  
   
                 halfpelMV.x = mvs.x/2; //forward first  
                 halfpelMV.y = mvs.y/2;  
                 GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases  
                 switch( ((mvs.x&1)<<1) + (mvs.y&1) ) {  
                 case 0: // pure halfpel position  
                         GET_REFERENCE(halfpelMV.x + 16*(k&1), halfpelMV.y + 16*(k>>1), ReferenceF);  
                         break;  
   
                 case 1: // x halfpel, y qpel - top or bottom during qpel refinement  
                         GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2);  
                         interpolate8x8_avg2(ReferenceF, ref1+8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
                                                         ref2+ 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0);  
                         break;  
   
                 case 2: // x qpel, y halfpel - left or right during qpel refinement  
                         GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref2);  
                         interpolate8x8_avg2(ReferenceF, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
                                                         ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0);  
                         break;  
   
                 default: // x and y in qpel resolution - the "corners" (top left/right and  
                                  // bottom left/right) during qpel refinement  
                         GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2);  
                         GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref3);  
                         GET_REFERENCE(mvs.x - halfpelMV.x, mvs.y - halfpelMV.y, ref4);  
                         interpolate8x8_avg4(ReferenceF, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
                                                                 ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
                                                                 ref3 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
                                                                 ref4 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0);  
                         break;  
                 }  
   
                 halfpelMV.x = b_mvs.x/2;  
                 halfpelMV.y = b_mvs.y/2;  
                 GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in most cases  
                 switch( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {  
                 case 0: // pure halfpel position  
                         GET_REFERENCE2(halfpelMV.x + 16*(k&1), halfpelMV.y + 16*(k>>1), ReferenceB);  
                         break;  
   
                 case 1: // x halfpel, y qpel - top or bottom during qpel refinement  
                         GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2);  
                         interpolate8x8_avg2(ReferenceB, ref1+8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
                                                                 ref2+ 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0);  
                         break;  
   
                 case 2: // x qpel, y halfpel - left or right during qpel refinement  
                         GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref2);  
                         interpolate8x8_avg2(ReferenceB, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
                                                                 ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0);  
                         break;  
   
                 default: // x and y in qpel resolution - the "corners" (top left/right and  
                                  // bottom left/right) during qpel refinement  
                         GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2);  
                         GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref3);  
                         GET_REFERENCE2(b_mvs.x - halfpelMV.x, b_mvs.y - halfpelMV.y, ref4);  
                         interpolate8x8_avg4(ReferenceB, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
                                                                 ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
                                                                 ref3 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
                                                                 ref4 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0);  
                         break;  
                 }  
   
                 sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
                                                 ReferenceF,  
                                                 ReferenceB,  
                                                 data->iEdgedWidth);  
                 if (sad > *(data->iMinSAD)) return;  
         }  
   
         sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;  
   
         if (sad < *(data->iMinSAD)) {  
                 *(data->iMinSAD) = sad;  
                 data->currentMV->x = x; data->currentMV->y = y;  
                 *dir = Direction; }  
 }  
   
 static void  
 CheckCandidateDirectno4v_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  
 {  
         int32_t sad = 0;  
         VECTOR mvs, b_mvs, halfpelMV;  
         const uint8_t *ref1, *ref2, *ref3, *ref4;  
         const uint32_t iEdgedWidth = data->iEdgedWidth;  
         uint8_t * ReferenceF = (uint8_t *)data->RefQ;  
         uint8_t * ReferenceB = (uint8_t *)data->RefQ + 64;  
   
         if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;  
   
         mvs.x = data->directmvF[0].x + x;  
         b_mvs.x = ((x == 0) ?  
                         data->directmvB[0].x  
                         : mvs.x - data->referencemv[0].x);  
   
         mvs.y = data->directmvF[0].y + y;  
         b_mvs.y = ((y == 0) ?  
                         data->directmvB[0].y  
                         : mvs.y - data->referencemv[0].y);  
   
         if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )  
                         || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )  
                         || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )  
                         || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;  
   
         halfpelMV.x = mvs.x/2; //forward first  
         halfpelMV.y = mvs.y/2;  
         GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases  
         switch( ((mvs.x&1)<<1) + (mvs.y&1) ) {  
         case 0: // pure halfpel position  
                 GET_REFERENCE(halfpelMV.x, halfpelMV.y, ReferenceF);  
                 break;  
   
         case 1: // x halfpel, y qpel - top or bottom during qpel refinement  
                 GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2);  
                 interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
   
         case 2: // x qpel, y halfpel - left or right during qpel refinement  
                 GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref2);  
                 interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
   
         default: // x and y in qpel resolution  
                 GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2);  
                 GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref3);  
                 GET_REFERENCE(mvs.x - halfpelMV.x, mvs.y - halfpelMV.y, ref4);  
   
                 interpolate8x8_avg4(ReferenceF, ref1, ref2, ref3, ref4, iEdgedWidth, 0);  
                 interpolate8x8_avg4(ReferenceF+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0);  
                 interpolate8x8_avg4(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg4(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
         }  
   
         halfpelMV.x = b_mvs.x/2; //backward  
         halfpelMV.y = b_mvs.y/2;  
         GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1);  
         switch( ((b_mvs.x&1)<<1) + (b_mvs.y&1) )  
         {  
         case 0: // pure halfpel position  
                 GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ReferenceB);  
                 break;  
   
         case 1: // x halfpel, y qpel - top or bottom during qpel refinement  
                 GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2);  
                 interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
   
         case 2: // x qpel, y halfpel - left or right during qpel refinement  
                 GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref2);  
                 interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
   
         default: // x and y in qpel resolution - the "corners" (top left/right and  
                          // bottom left/right) during qpel refinement  
                 GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2);  
                 GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref3);  
                 GET_REFERENCE2(b_mvs.x - halfpelMV.x, b_mvs.y - halfpelMV.y, ref4);  
   
                 interpolate8x8_avg4(ReferenceB, ref1, ref2, ref3, ref4, iEdgedWidth, 0);  
                 interpolate8x8_avg4(ReferenceB+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0);  
                 interpolate8x8_avg4(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0);  
                 interpolate8x8_avg4(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
         }  
   
         sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);  
         sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;  
   
         if (sad < *(data->iMinSAD)) {  
                 *(data->iMinSAD) = sad;  
                 data->currentMV->x = x; data->currentMV->y = y;  
                 *dir = Direction; }  
 }  
   
   
427  static void  static void
428  CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
429  {  {
# Line 823  Line 449 
449                  || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )                  || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
450                  || ( 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;
451    
452          switch ( ((mvs.x&1)<<1) + (mvs.y&1) ) {          if (!data->qpel) {
453                  case 0 : ReferenceF = data->Ref + mvs.x/2 + (mvs.y/2)*(data->iEdgedWidth); break;                          mvs.x *= 2; mvs.y *= 2;
454                  case 1 : ReferenceF = data->RefV + mvs.x/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;                          b_mvs.x *= 2; b_mvs.y *= 2; //we move to qpel precision anyway
                 case 2 : ReferenceF = data->RefH + (mvs.x-1)/2 + (mvs.y/2)*(data->iEdgedWidth); break;  
                 default : ReferenceF = data->RefHV + (mvs.x-1)/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;  
         }  
   
         switch ( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {  
                 case 0 : ReferenceB = data->bRef + b_mvs.x/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;  
                 case 1 : ReferenceB = data->bRefV + b_mvs.x/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;  
                 case 2 : ReferenceB = data->bRefH + (b_mvs.x-1)/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;  
                 default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;  
455          }          }
456            ReferenceF = Interpolate16x16qpel(mvs.x, mvs.y, 0, data);
457            ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data);
458    
459          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
460          sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;          sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;
# Line 855  Line 474 
474          if (( x > data->max_dx) || ( x < data->min_dx)          if (( x > data->max_dx) || ( x < data->min_dx)
475                  || ( y > data->max_dy) || (y < data->min_dy)) return;                  || ( y > data->max_dy) || (y < data->min_dy)) return;
476    
477          switch ( ((x&1)<<1) + (y&1) )          if (data->qpel) Reference = Interpolate16x16qpel(x, y, 0, data);
478          {          else Reference = Interpolate16x16qpel(2*x, 2*y, 0, data);
                 case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;  
                 case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
                 case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;  
                 default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
         }  
479    
480          sad = sad8(data->Cur, Reference, data->iEdgedWidth);          sad = sad8(data->Cur, Reference, data->iEdgedWidth);
481          if (data->qpel) t = d_mv_bits(2 * x - data->predQMV.x, 2 * y - data->predQMV.y, data->iFcode);          if (data->qpel) t = d_mv_bits(2 * x - data->predMV.x, 2 * y - data->predMV.y, data->iFcode);
482          else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);          else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
483    
484          sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100;          sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100;
# Line 875  Line 489 
489                  *dir = Direction; }                  *dir = Direction; }
490  }  }
491    
 static void  
 CheckCandidate8_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  
 // CheckCandidate8 variant which expects x and y in quarter pixel resolution  
 // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)  
 // around currentMV!  
   
 {  
         int32_t sad;  
         uint8_t *Reference = (uint8_t *) data->RefQ;  
         const uint8_t *ref1, *ref2, *ref3, *ref4;  
         VECTOR halfpelMV = *(data->currentMV);  
   
         int32_t iEdgedWidth = data->iEdgedWidth;  
         uint32_t rounding = data->rounding;  
   
         if (( x > data->max_dx) || ( x < data->min_dx)  
                 || ( y > data->max_dy) || (y < data->min_dy)) return;  
   
         GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);  
         switch( ((x&1)<<1) + (y&1) )  
         {  
         case 0: // pure halfpel position - shouldn't happen during a refinement step  
                 GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);  
                 break;  
   
         case 1: // x halfpel, y qpel - top or bottom during qpel refinement  
                 GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);  
   
                 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);  
                 break;  
   
         case 2: // x qpel, y halfpel - left or right during qpel refinement  
                 GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);  
   
                 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);  
                 break;  
   
         default: // x and y in qpel resolution - the "corners" (top left/right and  
                          // bottom left/right) during qpel refinement  
                 GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);  
                 GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3);  
                 GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4);  
   
                 interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);  
                 break;  
         }  
   
         sad = sad8(data->Cur, Reference, data->iEdgedWidth);  
         sad += (data->lambda8 * d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode) * (sad+NEIGH_8X8_BIAS))/100;  
   
         if (sad < *(data->iMinSAD)) {  
                 *(data->iMinSAD) = sad;  
                 data->currentQMV->x = x; data->currentQMV->y = y;  
                 *dir = Direction; }  
 }  
   
492  /* CHECK_CANDIATE FUNCTIONS END */  /* CHECK_CANDIATE FUNCTIONS END */
493    
494  /* MAINSEARCH FUNCTIONS START */  /* MAINSEARCH FUNCTIONS START */
# Line 1080  Line 638 
638  /* HALFPELREFINE COULD BE A MAINSEARCH FUNCTION, BUT THERE IS NO NEED FOR IT */  /* HALFPELREFINE COULD BE A MAINSEARCH FUNCTION, BUT THERE IS NO NEED FOR IT */
639    
640  static void  static void
641  HalfpelRefine(const SearchData * const data)  SubpelRefine(const SearchData * const data)
642  {  {
643  /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */  /* Do a half-pel or q-pel refinement */
644            VECTOR backupMV;
         VECTOR backupMV = *(data->currentMV);  
645          int iDirection; //not needed          int iDirection; //not needed
646    
647          CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);          if (data->qpel_precision)
648          CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);                  backupMV = *(data->currentQMV);
649          CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0);          else backupMV = *(data->currentMV);
         CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0);  
   
         CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0);  
         CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0);  
   
         CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);  
         CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);  
 }  
   
   
 static void  
 QuarterpelRefine(const SearchData * const data)  
 {  
 /* Perform quarter pixel refinement*/  
   
         VECTOR backupMV = *(data->currentQMV);  
         int iDirection; //not needed  
650    
651          CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);          CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);
652          CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);          CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);
# Line 1118  Line 658 
658    
659          CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);          CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);
660          CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);          CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);
   
661  }  }
662    
663  static __inline int  static __inline int
# Line 1368  Line 907 
907    
908          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);  //has to be changed to get_pmv(2)()          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);  //has to be changed to get_pmv(2)()
909          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,
910                                  pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);                  pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel);
   
         Data->predMV = pmv[0];  
911    
912          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
913          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;
# Line 1385  Line 922 
922    
923          Data->lambda16 = lambda_vec16[iQuant];          Data->lambda16 = lambda_vec16[iQuant];
924          Data->lambda8 = lambda_vec8[iQuant];          Data->lambda8 = lambda_vec8[iQuant];
925            Data->qpel_precision = 0;
926    
927          if (!(MotionFlags & PMV_HALFPEL16)) {          if (!(MotionFlags & PMV_HALFPEL16)) {
928                  Data->min_dx = EVEN(Data->min_dx);                  Data->min_dx = EVEN(Data->min_dx);
# Line 1397  Line 935 
935          for(i = 0;  i < 5; i++)          for(i = 0;  i < 5; i++)
936                  Data->currentMV[i].x = Data->currentMV[i].y = 0;                  Data->currentMV[i].x = Data->currentMV[i].y = 0;
937    
938          if (pParam->m_quarterpel) {          if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
939                  Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);          else Data->predMV = pmv[0];
                 i = d_mv_bits(Data->predQMV.x, Data->predQMV.y, Data->iFcode);  
         } else i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode);  
940    
941            i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode);
942          Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000;          Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000;
943          Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100;          Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100;
944          Data->iMinSAD[2] = pMB->sad8[1];          Data->iMinSAD[2] = pMB->sad8[1];
# Line 1417  Line 954 
954          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
955                                          prevMBs + x + y * pParam->mb_width);                                          prevMBs + x + y * pParam->mb_width);
956    
957          if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16;          if (inter4v || Data->chroma) CheckCandidate = CheckCandidate16;
958          else CheckCandidate = CheckCandidate16no4v;          else CheckCandidate = CheckCandidate16no4v; //for extra speed
959    
960  /* main loop. checking all predictions */  /* main loop. checking all predictions */
961    
# Line 1475  Line 1012 
1012                  }                  }
1013          }          }
1014    
1015          if (MotionFlags & PMV_HALFPELREFINE16) HalfpelRefine(Data);          if (MotionFlags & PMV_HALFPELREFINE16) SubpelRefine(Data);
1016    
1017          for(i = 0; i < 5; i++) {          for(i = 0; i < 5; i++) {
1018                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
# Line 1484  Line 1021 
1021    
1022          if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {          if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {
1023    
1024                  CheckCandidate = CheckCandidate16_qpel;                  Data->qpel_precision = 1;
1025                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,                  get_range_qpel(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1026                                  pParam->width, pParam->height, Data->iFcode, 0);                                  pParam->width, pParam->height, Data->iFcode);
1027    
1028                  QuarterpelRefine(Data);                  SubpelRefine(Data);
1029          }          }
1030    
1031          if (Data->iMinSAD[0] < (int32_t)iQuant * 30 ) inter4v = 0;          if (Data->iMinSAD[0] < (int32_t)iQuant * 30 ) inter4v = 0;
# Line 1505  Line 1042 
1042                  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);
1043    
1044                  if (Data->chroma) {                  if (Data->chroma) {
1045                          int sum, dx, dy;                          int sumx, sumy, dx, dy;
1046    
1047                          if(pParam->m_quarterpel) {                          if(pParam->m_quarterpel) {
1048                                  sum = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2;                                  sumx= pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2;
1049                          } else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                                  sumy = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2;
1050                          dy = (sum >> 3) + roundtab_76[sum & 0xf];                          } else {
1051                                    sumx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1052                          if(pParam->m_quarterpel) {                                  sumy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1053                                  sum = pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2;                          }
1054                          } else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                          dx = (sumx >> 3) + roundtab_76[sumx & 0xf];
1055                          dx = (sum >> 3) + roundtab_76[sum & 0xf];                          dy = (sumy >> 3) + roundtab_76[sumy & 0xf];
1056    
1057                          Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);                          Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);
1058                  }                  }
# Line 1529  Line 1066 
1066                  pMB->mvs[0] = pMB->mvs[1]                  pMB->mvs[0] = pMB->mvs[1]
1067                          = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];                          = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
1068    
                 pMB->qmvs[0] = pMB->qmvs[1]  
                         = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];  
   
1069                  pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =                  pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =
1070                          pMB->sad8[2] = pMB->sad8[3] =  Data->iMinSAD[0];                          pMB->sad8[2] = pMB->sad8[3] =  Data->iMinSAD[0];
1071    
1072                  if(pParam->m_quarterpel) {                  if(pParam->m_quarterpel) {
1073                          pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x;                          pMB->qmvs[0] = pMB->qmvs[1]
1074                          pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y;                                  = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
1075                            pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;
1076                            pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;
1077                  } else {                  } else {
1078                          pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;                          pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1079                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
# Line 1565  Line 1101 
1101          Data->currentQMV = OldData->currentQMV + 1 + block;          Data->currentQMV = OldData->currentQMV + 1 + block;
1102    
1103          if(pParam->m_quarterpel) {          if(pParam->m_quarterpel) {
1104                  Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);                  Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
1105                  if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *                  if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *
1106                                                                          d_mv_bits(      Data->currentQMV->x - Data->predQMV.x,                                                                          d_mv_bits(      Data->currentQMV->x - Data->predMV.x,
1107                                                                                                  Data->currentQMV->y - Data->predQMV.y,                                                                                                  Data->currentQMV->y - Data->predMV.y,
1108                                                                                                  Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;                                                                                                  Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;
1109          } else {          } else {
1110                  Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);                  Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
# Line 1586  Line 1122 
1122                  Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1));                  Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1));
1123    
1124                  Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1));                  Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1));
1125                    Data->qpel_precision = 0;
1126    
1127                  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,
1128                                  pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);                                  pParam->width, pParam->height, OldData->iFcode - pParam->m_quarterpel);
1129                  CheckCandidate = CheckCandidate8;                  CheckCandidate = CheckCandidate8;
1130    
1131                  if (MotionFlags & PMV_EXTSEARCH8) {                  if (MotionFlags & PMV_EXTSEARCH8) {
# Line 1610  Line 1147 
1147                  if (MotionFlags & PMV_HALFPELREFINE8) {                  if (MotionFlags & PMV_HALFPELREFINE8) {
1148                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1149    
1150                          HalfpelRefine(Data); // perform halfpel refine of current best vector                          SubpelRefine(Data); // perform halfpel refine of current best vector
1151    
1152                          if(*(Data->iMinSAD) < temp_sad) { // we have found a better match                          if(*(Data->iMinSAD) < temp_sad) { // we have found a better match
1153                                  Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector                                  Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
# Line 1621  Line 1158 
1158                  if(pParam->m_quarterpel) {                  if(pParam->m_quarterpel) {
1159                          if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) &&                          if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) &&
1160                                  (MotionFlags & PMV_QUARTERPELREFINE8)) {                                  (MotionFlags & PMV_QUARTERPELREFINE8)) {
1161                          CheckCandidate = CheckCandidate8_qpel;                          Data->qpel_precision = 1;
1162                          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,                          get_range_qpel(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,
1163                                  pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);                                  pParam->width, pParam->height, OldData->iFcode);
1164                          QuarterpelRefine(Data);                          SubpelRefine(Data);
1165                          }                          }
1166                  }                  }
1167          }          }
1168    
1169          if(pParam->m_quarterpel) {          if(pParam->m_quarterpel) {
1170                  pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x;                  pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x;
1171                  pMB->pmvs[block].y = Data->currentQMV->y - Data->predQMV.y;                  pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y;
1172                    pMB->qmvs[block] = *(Data->currentQMV);
1173          }          }
1174          else {          else {
1175                  pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;                  pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;
# Line 1639  Line 1177 
1177          }          }
1178    
1179          pMB->mvs[block] = *(Data->currentMV);          pMB->mvs[block] = *(Data->currentMV);
         pMB->qmvs[block] = *(Data->currentQMV);  
   
1180          pMB->sad8[block] =  4 * (*Data->iMinSAD);          pMB->sad8[block] =  4 * (*Data->iMinSAD);
1181  }  }
1182    
# Line 1718  Line 1254 
1254          MainSearchFunc *MainSearchPtr;          MainSearchFunc *MainSearchPtr;
1255          *Data->iMinSAD = MV_MAX_ERROR;          *Data->iMinSAD = MV_MAX_ERROR;
1256          Data->iFcode = iFcode;          Data->iFcode = iFcode;
1257            Data->qpel_precision = 0;
1258    
1259          Data->Ref = pRef + (x + y * iEdgedWidth) * 16;          Data->Ref = pRef + (x + y * iEdgedWidth) * 16;
1260          Data->RefH = pRefH + (x + y * iEdgedWidth) * 16;          Data->RefH = pRefH + (x + y * iEdgedWidth) * 16;
# Line 1727  Line 1264 
1264          Data->predMV = *predMV;          Data->predMV = *predMV;
1265    
1266          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,
1267                                  pParam->width, pParam->height, iFcode, pParam->m_quarterpel);                                  pParam->width, pParam->height, iFcode - pParam->m_quarterpel);
1268    
1269          pmv[0] = Data->predMV;          pmv[0] = Data->predMV;
1270          if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; }          if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; }
1271          PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current);          PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current);
1272    
1273          Data->currentMV->x = Data->currentMV->y = 0;          Data->currentMV->x = Data->currentMV->y = 0;
   
1274          CheckCandidate = CheckCandidate16no4v;          CheckCandidate = CheckCandidate16no4v;
1275    
1276  // main loop. checking all predictions  // main loop. checking all predictions
# Line 1751  Line 1287 
1287    
1288          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1289    
1290          HalfpelRefine(Data);          SubpelRefine(Data);
1291    
1292          if (Data->qpel) {          if (Data->qpel) {
1293                  Data->currentQMV->x = 2*Data->currentMV->x;                  Data->currentQMV->x = 2*Data->currentMV->x;
1294                  Data->currentQMV->y = 2*Data->currentMV->y;                  Data->currentQMV->y = 2*Data->currentMV->y;
1295                  CheckCandidate = CheckCandidate16no4v_qpel;                  Data->qpel_precision = 1;
1296                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,                  get_range_qpel(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1297                                          pParam->width, pParam->height, iFcode, pParam->m_quarterpel);                                          pParam->width, pParam->height, iFcode);
1298                  QuarterpelRefine(Data);                  SubpelRefine(Data);
1299          }          }
1300    
1301  // three bits are needed to code backward mode. four for forward  // three bits are needed to code backward mode. four for forward
# Line 1837  Line 1373 
1373                  Data->min_dy *= 2;                  Data->min_dy *= 2;
1374                  Data->referencemv = b_mb->qmvs;                  Data->referencemv = b_mb->qmvs;
1375          } else Data->referencemv = b_mb->mvs;          } else Data->referencemv = b_mb->mvs;
1376            Data->qpel_precision = 0; // it's a trick. it's 1 not 0, but we need 0 here
1377    
1378          for (k = 0; k < 4; k++) {          for (k = 0; k < 4; k++) {
1379                  pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD);                  pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD);
# Line 1861  Line 1398 
1398                  }                  }
1399          }          }
1400    
1401          if (Data->qpel) {  
                         if (b_mb->mode == MODE_INTER4V)  
                 CheckCandidate = CheckCandidateDirect_qpel;  
                         else CheckCandidate = CheckCandidateDirectno4v_qpel;  
         } else {  
1402                          if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect;                          if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect;
1403                          else CheckCandidate = CheckCandidateDirectno4v;                          else CheckCandidate = CheckCandidateDirectno4v;
         }  
1404    
1405          (*CheckCandidate)(0, 0, 255, &k, Data);          (*CheckCandidate)(0, 0, 255, &k, Data);
1406    
# Line 1926  Line 1458 
1458    
1459          (*MainSearchPtr)(0, 0, Data, 255);          (*MainSearchPtr)(0, 0, Data, 255);
1460    
1461          HalfpelRefine(Data); //or qpel refine, if we're in qpel mode          SubpelRefine(Data);
1462    
1463          *Data->iMinSAD +=  1 * Data->lambda16; // one bit is needed to code direct mode  //      *Data->iMinSAD +=  1 * Data->lambda16; // one bit is needed to code direct mode
1464          *best_sad = *Data->iMinSAD;          *best_sad = *Data->iMinSAD;
1465    
1466          if (b_mb->mode == MODE_INTER4V)          if (b_mb->mode == MODE_INTER4V)
# Line 2008  Line 1540 
1540          bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16;          bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16;
1541          bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16;          bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16;
1542          bData.RefQ = fData->RefQ;          bData.RefQ = fData->RefQ;
1543            fData->qpel_precision = bData.qpel_precision = 0; bData.qpel = fData->qpel;
1544            bData.rounding = 0;
1545    
1546          bData.bpredMV = fData->predMV = *f_predMV;          bData.bpredMV = fData->predMV = *f_predMV;
1547          fData->bpredMV = bData.predMV = *b_predMV;          fData->bpredMV = bData.predMV = *b_predMV;
1548    
1549          fData->currentMV[0] = fData->currentMV[2];          fData->currentMV[0] = fData->currentMV[2];
1550          get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, pParam->m_quarterpel);          get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode - pParam->m_quarterpel);
1551          get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, pParam->m_quarterpel);          get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode - pParam->m_quarterpel);
1552    
1553          if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx;          if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx;
1554          if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dy;          if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dx;
1555          if (fData->currentMV[0].y > fData->max_dy) fData->currentMV[0].y = fData->max_dx;          if (fData->currentMV[0].y > fData->max_dy) fData->currentMV[0].y = fData->max_dy;
1556          if (fData->currentMV[0].y > fData->min_dy) fData->currentMV[0].y = fData->min_dy;          if (fData->currentMV[0].y < fData->min_dy) fData->currentMV[0].y = fData->min_dy;
1557    
1558          if (fData->currentMV[1].x > bData.max_dx) fData->currentMV[1].x = bData.max_dx;          if (fData->currentMV[1].x > bData.max_dx) fData->currentMV[1].x = bData.max_dx;
1559          if (fData->currentMV[1].x < bData.min_dx) fData->currentMV[1].x = bData.min_dy;          if (fData->currentMV[1].x < bData.min_dx) fData->currentMV[1].x = bData.min_dx;
1560          if (fData->currentMV[1].y > bData.max_dy) fData->currentMV[1].y = bData.max_dx;          if (fData->currentMV[1].y > bData.max_dy) fData->currentMV[1].y = bData.max_dy;
1561          if (fData->currentMV[1].y > bData.min_dy) fData->currentMV[1].y = bData.min_dy;          if (fData->currentMV[1].y < bData.min_dy) fData->currentMV[1].y = bData.min_dy;
1562    
1563          CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);          CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);
1564    
# Line 2050  Line 1584 
1584    
1585          } while (!(iDirection));          } while (!(iDirection));
1586    
         *fData->iMinSAD +=  2 * fData->lambda16; // two bits are needed to code interpolate mode.  
   
1587          if (fData->qpel) {          if (fData->qpel) {
1588                  CheckCandidate = CheckCandidateInt_qpel;                  CheckCandidate = CheckCandidateInt;
1589                  get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 0);                  fData->qpel_precision = bData.qpel_precision = 1;
1590                  get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, 0);                  get_range_qpel(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode);
1591                    get_range_qpel(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode);
1592                  fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x;                  fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x;
1593                  fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y;                  fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y;
1594                  fData->currentQMV[1].x = 2 * fData->currentMV[1].x;                  fData->currentQMV[1].x = 2 * fData->currentMV[1].x;
1595                  fData->currentQMV[1].y = 2 * fData->currentMV[1].y;                  fData->currentQMV[1].y = 2 * fData->currentMV[1].y;
1596  //              QuarterpelRefine(fData);                  SubpelRefine(fData);
1597                  fData->currentQMV[2] = fData->currentQMV[0];                  fData->currentQMV[2] = fData->currentQMV[0];
1598  //              QuarterpelRefine(&bData);                  SubpelRefine(&bData);
1599          }          }
1600    
1601            *fData->iMinSAD +=  2 * fData->lambda16; // two bits are needed to code interpolate mode.
1602    
1603          if (*fData->iMinSAD < *best_sad) {          if (*fData->iMinSAD < *best_sad) {
1604                  *best_sad = *fData->iMinSAD;                  *best_sad = *fData->iMinSAD;
1605                  pMB->mvs[0] = fData->currentMV[0];                  pMB->mvs[0] = fData->currentMV[0];
# Line 2125  Line 1660 
1660          Data.iEdgedWidth = pParam->edged_width;          Data.iEdgedWidth = pParam->edged_width;
1661          Data.currentMV = currentMV; Data.currentQMV = currentQMV;          Data.currentMV = currentMV; Data.currentQMV = currentQMV;
1662          Data.iMinSAD = &iMinSAD;          Data.iMinSAD = &iMinSAD;
1663          Data.lambda16 = lambda_vec16[frame->quant];          Data.lambda16 = lambda_vec16[frame->quant] + 2;
1664          Data.qpel = pParam->m_quarterpel;          Data.qpel = pParam->m_quarterpel;
1665            Data.rounding = 0;
1666    
1667          if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)          if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
1668                  return; // allocate some mem for qpel interpolated blocks                  return; // allocate some mem for qpel interpolated blocks
# Line 2221  Line 1757 
1757                                  case MODE_DIRECT:                                  case MODE_DIRECT:
1758                                  case MODE_DIRECT_NO4V:                                  case MODE_DIRECT_NO4V:
1759                                          d_count++;                                          d_count++;
                                         break;  
1760                                  default:                                  default:
1761                                          break;                                          break;
1762                          }                          }
# Line 2252  Line 1787 
1787          int i, t;          int i, t;
1788          MainSearchFunc * MainSearchPtr;          MainSearchFunc * MainSearchPtr;
1789    
         Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);  
         Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);  
1790          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,
1791                                  pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);                                  pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel);
1792    
1793          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
1794          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;
# Line 2267  Line 1800 
1800          Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;          Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;
1801          Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;          Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;
1802          Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;          Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
1803            Data->qpel_precision = 0;
1804    
1805          if (!(MotionFlags & PMV_HALFPEL16)) {          if (!(MotionFlags & PMV_HALFPEL16)) {
1806                  Data->min_dx = EVEN(Data->min_dx);                  Data->min_dx = EVEN(Data->min_dx);
# Line 2274  Line 1808 
1808                  Data->min_dy = EVEN(Data->min_dy);                  Data->min_dy = EVEN(Data->min_dy);
1809                  Data->max_dy = EVEN(Data->max_dy);                  Data->max_dy = EVEN(Data->max_dy);
1810          }          }
1811            if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1812            else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1813    
1814          for(i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;          for(i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;
1815    
1816          if (pMB->dquant != NO_CHANGE) inter4v = 0;          if (pMB->dquant != NO_CHANGE) inter4v = 0;
1817    
1818          if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16;          if (inter4v || Data->chroma) CheckCandidate = CheckCandidate16;
1819          else CheckCandidate = CheckCandidate16no4v;          else CheckCandidate = CheckCandidate16no4v;
1820    
1821          pMB->mvs[0].x = EVEN(pMB->mvs[0].x);          pMB->mvs[0].x = EVEN(pMB->mvs[0].x);
# Line 2307  Line 1843 
1843    
1844          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1845    
1846          if (MotionFlags & PMV_HALFPELREFINE16) HalfpelRefine(Data);          if (MotionFlags & PMV_HALFPELREFINE16) SubpelRefine(Data);
1847    
1848          for(i = 0; i < 5; i++) {          for(i = 0; i < 5; i++) {
1849                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
# Line 2315  Line 1851 
1851          }          }
1852    
1853          if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {          if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {
1854                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,                  get_range_qpel(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1855                                  pParam->width, pParam->height, Data->iFcode, 0);                                  pParam->width, pParam->height, Data->iFcode);
1856                  CheckCandidate = CheckCandidate16_qpel;                  Data->qpel_precision = 1;
1857                  QuarterpelRefine(Data);                  SubpelRefine(Data);
1858          }          }
1859    
1860          if (inter4v) {          if (inter4v) {
# Line 2334  Line 1870 
1870                  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);
1871    
1872                  if (Data->chroma) {                  if (Data->chroma) {
1873                          int sum, dx, dy;                          int sumx, sumy, dx, dy;
1874    
1875                            if(pParam->m_quarterpel) {
1876                                    sumx= pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2;
1877                                    sumy = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2;
1878                            } else {
1879                                    sumx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1880                                    sumy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1881                            }
1882                            dx = (sumx >> 3) + roundtab_76[sumx & 0xf];
1883                            dy = (sumy >> 3) + roundtab_76[sumy & 0xf];
1884    
                         if(pParam->m_quarterpel)  
                                 sum = (pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2);  
                         else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
                         dy = (sum ? SIGN(sum) *  
                                   (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0);  
   
                         if(pParam->m_quarterpel)  
                                 sum = (pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2);  
                         else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
                         dx = (sum ? SIGN(sum) *  
                                   (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0);  
1885                          Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);                          Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);
1886                  }                  }
1887          }          }
# Line 2366  Line 1901 
1901                          pMB->sad8[2] = pMB->sad8[3] =  Data->iMinSAD[0];                          pMB->sad8[2] = pMB->sad8[3] =  Data->iMinSAD[0];
1902    
1903                  if(pParam->m_quarterpel) {                  if(pParam->m_quarterpel) {
1904                          pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x;                          pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;
1905                          pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y;                          pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;
1906                  } else {                  } else {
1907                          pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;                          pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1908                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
# Line 2473  Line 2008 
2008                          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
2009    
2010          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,
2011                                  pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);                                  pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel);
2012    
2013          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;
2014          Data->Ref = pRef + (x + y * pParam->edged_width) * 16;          Data->Ref = pRef + (x + y * pParam->edged_width) * 16;
# Line 2507  Line 2042 
2042  }  }
2043    
2044  #define INTRA_THRESH    1350  #define INTRA_THRESH    1350
2045  #define INTER_THRESH    900  #define INTER_THRESH    1200
2046    
2047    
2048  int  int
# Line 2540  Line 2075 
2075                          IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra;                          IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra;
2076    
2077    
2078          InterThresh += 300 * (1 - bCount);          InterThresh += 400 * (1 - bCount);
2079          if (InterThresh < 200) InterThresh = 200;          if (InterThresh < 200) InterThresh = 200;
2080    
2081          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();

Legend:
Removed from v.662  
changed lines
  Added in v.700

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