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

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

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

revision 1.44.2.19, Tue Nov 19 13:04:34 2002 UTC revision 1.58.2.12, Fri Apr 25 14:53:37 2003 UTC
# Line 31  Line 31 
31  #include <assert.h>  #include <assert.h>
32  #include <stdio.h>  #include <stdio.h>
33  #include <stdlib.h>  #include <stdlib.h>
34    #include <string.h>     // memcpy
35    #include <math.h>       // lrint
36    
37  #include "../encoder.h"  #include "../encoder.h"
38  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
# Line 42  Line 44 
44  #include "motion.h"  #include "motion.h"
45  #include "sad.h"  #include "sad.h"
46  #include "../utils/emms.h"  #include "../utils/emms.h"
47    #include "../dct/fdct.h"
48    
49    /*****************************************************************************
50     * Modified rounding tables -- declared in motion.h
51     * Original tables see ISO spec tables 7-6 -> 7-9
52     ****************************************************************************/
53    
54    const uint32_t roundtab[16] =
55    {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };
56    
57    /* K = 4 */
58    const uint32_t roundtab_76[16] =
59    { 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1 };
60    
61    /* K = 2 */
62    const uint32_t roundtab_78[8] =
63    { 0, 0, 1, 1, 0, 0, 0, 1  };
64    
65    /* K = 1 */
66    const uint32_t roundtab_79[4] =
67    { 0, 1, 0, 0 };
68    
69  #define INITIAL_SKIP_THRESH     (10)  #define INITIAL_SKIP_THRESH     (10)
70  #define FINAL_SKIP_THRESH       (50)  #define FINAL_SKIP_THRESH       (50)
71  #define MAX_SAD00_FOR_SKIP      (20)  #define MAX_SAD00_FOR_SKIP      (20)
72  #define MAX_CHROMA_SAD_FOR_SKIP (22)  #define MAX_CHROMA_SAD_FOR_SKIP (22)
 #define SKIP_THRESH_B (25)  
73    
74  #define CHECK_CANDIDATE(X,Y,D) { \  #define CHECK_CANDIDATE(X,Y,D) { \
75  (*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); }  CheckCandidate((X),(Y), (D), &iDirection, data ); }
   
 #define GET_REFERENCE(X, Y, REF) { \  
         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; \  
         } \  
 }  
76    
77    /*****************************************************************************
78  #define iDiamondSize 2   * Code
79     ****************************************************************************/
80  static __inline int  
81  d_mv_bits(int x, int y, const uint32_t iFcode)  static __inline uint32_t
82  {  d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv)
83          int xb, yb;  {
84            int bits;
85          if (x == 0) xb = 1;          const int q = (1 << (iFcode - 1)) - 1;
86          else {  
87                  if (x < 0) x = -x;          x <<= qpel;
88                  x += (1 << (iFcode - 1)) - 1;          y <<= qpel;
89            if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); }
90    
91            x -= pred.x;
92            bits = (x != 0 ? iFcode:0);
93            x = abs(x);
94            x += q;
95                  x >>= (iFcode - 1);                  x >>= (iFcode - 1);
96                  if (x > 32) x = 32;          bits += mvtab[x];
                 xb = mvtab[x] + iFcode;  
         }  
97    
98          if (y == 0) yb = 1;          y -= pred.y;
99          else {          bits += (y != 0 ? iFcode:0);
100                  if (y < 0) y = -y;          y = abs(y);
101                  y += (1 << (iFcode - 1)) - 1;          y += q;
102                  y >>= (iFcode - 1);                  y >>= (iFcode - 1);
103                  if (y > 32) y = 32;          bits += mvtab[y];
104                  yb = mvtab[y] + iFcode;  
105          }          return bits;
         return xb + yb;  
106  }  }
107    
108  static int32_t  static int32_t ChromaSAD2(const int fx, const int fy, const int bx, const int by,
109  ChromaSAD(int dx, int dy, const SearchData * const data)                                                          const SearchData * const data)
110  {  {
111          int sad;          int sad;
112          dx = (dx >> 1) + roundtab_79[dx & 0x3];          const uint32_t stride = data->iEdgedWidth/2;
113          dy = (dy >> 1) + roundtab_79[dy & 0x3];          uint8_t * f_refu = data->RefQ,
114                    * f_refv = data->RefQ + 8,
115                    * b_refu = data->RefQ + 16,
116                    * b_refv = data->RefQ + 24;
117            int offset = (fx>>1) + (fy>>1)*stride;
118    
119          switch (((dx & 1) << 1) + (dy & 1))     { // ((dx%2)?2:0)+((dy%2)?1:0)          switch (((fx & 1) << 1) | (fy & 1))     {
120                  case 0:                  case 0:
121                          sad = sad8(data->CurU, data->RefCU + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2);                          f_refu = (uint8_t*)data->RefP[4] + offset;
122                          sad += sad8(data->CurV, data->RefCV + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2);                          f_refv = (uint8_t*)data->RefP[5] + offset;
123                          break;                          break;
124                  case 1:                  case 1:
125                          dx = dx / 2; dy = (dy - 1) / 2;                          interpolate8x8_halfpel_v(f_refu, data->RefP[4] + offset, stride, data->rounding);
126                          sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2);                          interpolate8x8_halfpel_v(f_refv, data->RefP[5] + offset, stride, data->rounding);
                         sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2);  
127                          break;                          break;
128                  case 2:                  case 2:
129                          dx = (dx - 1) / 2; dy = dy / 2;                          interpolate8x8_halfpel_h(f_refu, data->RefP[4] + offset, stride, data->rounding);
130                          sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2);                          interpolate8x8_halfpel_h(f_refv, data->RefP[5] + offset, stride, data->rounding);
                         sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2);  
131                          break;                          break;
132                  default:                  default:
133                          dx = (dx - 1) / 2; dy = (dy - 1) / 2;                          interpolate8x8_halfpel_hv(f_refu, data->RefP[4] + offset, stride, data->rounding);
134                          interpolate8x8_halfpel_hv(data->RefQ,                          interpolate8x8_halfpel_hv(f_refv, data->RefP[5] + offset, stride, data->rounding);
                                                                          data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2,  
                                                                          data->rounding);  
                         sad = sad8(data->CurU, data->RefQ, data->iEdgedWidth/2);  
                         interpolate8x8_halfpel_hv(data->RefQ,  
                                                                          data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2,  
                                                                          data->rounding);  
                         sad += sad8(data->CurV, data->RefQ, data->iEdgedWidth/2);  
135                          break;                          break;
136          }          }
         return sad;  
 }  
   
137    
138  /* CHECK_CANDIATE FUNCTIONS START */          offset = (bx>>1) + (by>>1)*stride;
139            switch (((bx & 1) << 1) | (by & 1))     {
140                    case 0:
141  static void                          b_refu = (uint8_t*)data->b_RefP[4] + offset;
142  CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)                          b_refv = (uint8_t*)data->b_RefP[5] + offset;
143  {                          break;
144          int t;                  case 1:
145          const uint8_t * Reference;                          interpolate8x8_halfpel_v(b_refu, data->b_RefP[4] + offset, stride, data->rounding);
146                            interpolate8x8_halfpel_v(b_refv, data->b_RefP[5] + offset, stride, data->rounding);
147          if (( x > data->max_dx) || ( x < data->min_dx)                          break;
148                  || ( y > data->max_dy) || (y < data->min_dy)) return;                  case 2:
149                            interpolate8x8_halfpel_h(b_refu, data->b_RefP[4] + offset, stride, data->rounding);
150          switch ( ((x&1)<<1) + (y&1) ) {                          interpolate8x8_halfpel_h(b_refv, data->b_RefP[5] + offset, stride, data->rounding);
151                  case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;                          break;
152                  case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;                  default:
153                  case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;                          interpolate8x8_halfpel_hv(b_refu, data->b_RefP[4] + offset, stride, data->rounding);
154                  default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;                          interpolate8x8_halfpel_hv(b_refv, data->b_RefP[5] + offset, stride, data->rounding);
155                            break;
156          }          }
157    
158          data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1);          sad = sad8bi(data->CurU, b_refu, f_refu, stride);
159            sad += sad8bi(data->CurV, b_refv, f_refv, stride);
         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);  
160    
161          data->temp[0] += (data->lambda16 * t * data->temp[0])/1000;          return sad;
162          data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100;  }
163    
164          if (data->chroma) data->temp[0] += ChromaSAD(x, y, data);  static int32_t
165    ChromaSAD(const int dx, const int dy, const SearchData * const data)
166    {
167            int sad;
168            const uint32_t stride = data->iEdgedWidth/2;
169            int offset = (dx>>1) + (dy>>1)*stride;
170    
171          if (data->temp[0] < data->iMinSAD[0]) {          if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; //it has been checked recently
172                  data->iMinSAD[0] = data->temp[0];          data->temp[5] = dx; data->temp[6] = dy; // backup
                 data->currentMV[0].x = x; data->currentMV[0].y = y;  
                 *dir = Direction; }  
173    
174          if (data->temp[1] < data->iMinSAD[1]) {          switch (((dx & 1) << 1) | (dy & 1))     {
175                  data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; }                  case 0:
176          if (data->temp[2] < data->iMinSAD[2]) {                          sad = sad8(data->CurU, data->RefP[4] + offset, stride);
177                  data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; }                          sad += sad8(data->CurV, data->RefP[5] + offset, stride);
178          if (data->temp[3] < data->iMinSAD[3]) {                          break;
179                  data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; }                  case 1:
180          if (data->temp[4] < data->iMinSAD[4]) {                          sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + stride, stride);
181                  data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; }                          sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + stride, stride);
182                            break;
183                    case 2:
184                            sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + 1, stride);
185                            sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + 1, stride);
186                            break;
187                    default:
188                            interpolate8x8_halfpel_hv(data->RefQ, data->RefP[4] + offset, stride, data->rounding);
189                            sad = sad8(data->CurU, data->RefQ, stride);
190    
191                            interpolate8x8_halfpel_hv(data->RefQ, data->RefP[5] + offset, stride, data->rounding);
192                            sad += sad8(data->CurV, data->RefQ, stride);
193                            break;
194            }
195            data->temp[7] = sad; //backup, part 2
196            return sad;
197  }  }
198    
199  static void  static __inline const uint8_t *
200  CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  GetReferenceB(const int x, const int y, const uint32_t dir, const SearchData * const data)
 {  
         int32_t sad;  
         const uint8_t * Reference;  
   
         if (( x > data->max_dx) || ( x < data->min_dx)  
                 || ( y > data->max_dy) || (y < data->min_dy)) return;  
   
         switch ( ((x&1)<<1) + (y&1) )  
201          {          {
202                  case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;  //      dir : 0 = forward, 1 = backward
203                  case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;          const uint8_t *const *const direction = ( dir == 0 ? data->RefP : data->b_RefP );
204                  case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;          const int picture = ((x&1)<<1) | (y&1);
205                  default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;          const int offset = (x>>1) + (y>>1)*data->iEdgedWidth;
206            return direction[picture] + offset;
207          }          }
208    
209          sad = sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR);  // this is a simpler copy of GetReferenceB, but as it's __inline anyway, we can keep the two separate
210          if (data->qpel) //only to be used in b-frames' ME  static __inline const uint8_t *
211                  sad += (data->lambda16 * d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode) * sad)/1000;  GetReference(const int x, const int y, const SearchData * const data)
212          else  {
213                  sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000;          const int picture = ((x&1)<<1) | (y&1);
214            const int offset = (x>>1) + (y>>1)*data->iEdgedWidth;
215          if (sad < *(data->iMinSAD)) {          return data->RefP[picture] + offset;
                 *(data->iMinSAD) = sad;  
                 data->currentMV[0].x = x; data->currentMV[0].y = y;  
                 *dir = Direction; }  
216  }  }
217    
218  static void  static uint8_t *
219  CheckCandidate16_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  Interpolate8x8qpel(const int x, const int y, const uint32_t block, const uint32_t 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!  
220  {  {
221          int t;  // create or find a qpel-precision reference picture; return pointer to it
222          uint8_t * Reference = (uint8_t *)data->RefQ;          uint8_t * Reference = data->RefQ + 16*dir;
223            const uint32_t iEdgedWidth = data->iEdgedWidth;
224            const uint32_t rounding = data->rounding;
225            const int halfpel_x = x/2;
226            const int halfpel_y = y/2;
227          const uint8_t *ref1, *ref2, *ref3, *ref4;          const uint8_t *ref1, *ref2, *ref3, *ref4;
         VECTOR halfpelMV = *(data->currentMV);  
228    
229          int32_t iEdgedWidth = data->iEdgedWidth;          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);
230          uint32_t rounding = data->rounding;          ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
231            switch( ((x&1)<<1) + (y&1) ) {
232          if (( x > data->max_dx) || ( x < data->min_dx)          case 3: // x and y in qpel resolution - the "corners" (top left/right and
233                  || ( y > data->max_dy) || (y < data->min_dy)) return;                          // bottom left/right) during qpel refinement
234                    ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
235          GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this refenrence is used in all cases                  ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
236          switch( ((x&1)<<1) + (y&1) )                  ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);
237          {                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
238          case 0: // pure halfpel position - shouldn't happen during a refinement step                  ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
239                  GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);                  ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
240                    interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
241                  break;                  break;
242    
243          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          case 1: // x halfpel, y qpel - top or bottom during qpel refinement
244                  GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
245                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
246                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
                 interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding);  
                 interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding);  
247                  break;                  break;
248    
249          case 2: // x qpel, y halfpel - left or right during qpel refinement          case 2: // x qpel, y halfpel - left or right during qpel refinement
250                  GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);                  ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
251                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
252                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
                 interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding);  
                 interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding);  
253                  break;                  break;
254    
255          default: // x and y in qpel resolution - the "corners" (top left/right and          default: // pure halfpel position
256                           // bottom left/right) during qpel refinement                  return (uint8_t *) ref1;
                 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);  
257    
                 interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);  
                 interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);  
                 interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, 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);  
                 break;  
258          }          }
259            return Reference;
         data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1);  
   
         t = d_mv_bits(x - data->predQMV.x, y - data->predQMV.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/2, y/2, data);  
   
         if (data->temp[0] < data->iMinSAD[0]) {  
                 data->iMinSAD[0] = data->temp[0];  
                 data->currentQMV[0].x = x; data->currentQMV[0].y = y;  
         /*      *dir = Direction;*/ }  
   
         if (data->temp[1] < data->iMinSAD[1]) {  
                 data->iMinSAD[1] = data->temp[1]; data->currentQMV[1].x = x; data->currentQMV[1].y = y; }  
         if (data->temp[2] < data->iMinSAD[2]) {  
                 data->iMinSAD[2] = data->temp[2]; data->currentQMV[2].x = x; data->currentQMV[2].y = y; }  
         if (data->temp[3] < data->iMinSAD[3]) {  
                 data->iMinSAD[3] = data->temp[3]; data->currentQMV[3].x = x; data->currentQMV[3].y = y; }  
         if (data->temp[4] < data->iMinSAD[4]) {  
                 data->iMinSAD[4] = data->temp[4]; data->currentQMV[4].x = x; data->currentQMV[4].y = y; }  
260  }  }
261    
262  static void  static uint8_t *
263  CheckCandidate16no4v_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 uint32_t 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  
264  {  {
265          uint8_t * Reference = (uint8_t *)data->RefQ;  // create or find a qpel-precision reference picture; return pointer to it
266            uint8_t * Reference = data->RefQ + 16*dir;
267            const uint32_t iEdgedWidth = data->iEdgedWidth;
268            const uint32_t rounding = data->rounding;
269            const int halfpel_x = x/2;
270            const int halfpel_y = y/2;
271          const uint8_t *ref1, *ref2, *ref3, *ref4;          const uint8_t *ref1, *ref2, *ref3, *ref4;
         VECTOR halfpelMV = *(data->currentMV);  
   
         int32_t iEdgedWidth = data->iEdgedWidth;  
         int32_t sad;  
272    
273          if (( x > data->max_dx) || ( x < data->min_dx)          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);
274                  || ( y > data->max_dy) || (y < data->min_dy)) return;          switch( ((x&1)<<1) + (y&1) ) {
275            case 3: // x and y in qpel resolution - the "corners" (top left/right and
276          GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this refenrence is used in all cases                          // bottom left/right) during qpel refinement
277          switch( ((x&1)<<1) + (y&1) )                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
278          {                  ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
279          case 0: // pure halfpel position - shouldn't happen during a refinement step                  ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);
280                  GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);                  interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
281                    interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);
282                    interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);
283                    interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);
284                  break;                  break;
285    
286          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          case 1: // x halfpel, y qpel - top or bottom during qpel refinement
287                  GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
288                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, 0);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
289                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, 0);                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
290                  interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);                  interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8);
291                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);
292                  break;                  break;
293    
294          case 2: // x qpel, y halfpel - left or right during qpel refinement          case 2: // x qpel, y halfpel - left or right during qpel refinement
295                  GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);                  ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
296                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, 0);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
297                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, 0);                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
298                  interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);                  interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8);
299                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);
300                  break;                  break;
301    
302          default: // x and y in qpel resolution - the "corners" (top left/right and          default: // pure halfpel position
303                           // bottom left/right) during qpel refinement                  return (uint8_t *) ref1;
                 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;  
304          }          }
305            return Reference;
         sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);  
         sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000;  
   
         if (sad < data->iMinSAD[0]) {  
                 data->iMinSAD[0] = sad;  
                 data->currentQMV[0].x = x; data->currentQMV[0].y = y;  
         /*      *dir = Direction;*/ }  
306  }  }
307    
308    /* CHECK_CANDIATE FUNCTIONS START */
309    
310  static void  static void
311  CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
312  {  {
313  // maximum speed - for P/B/I decision          int xc, yc;
314          int32_t sad;          const uint8_t * Reference;
315            VECTOR * current;
316            int32_t sad; uint32_t t;
317    
318          if (( x > data->max_dx) || ( x < data->min_dx)          if (( x > data->max_dx) || ( x < data->min_dx)
319                  || ( y > data->max_dy) || (y < data->min_dy)) return;                  || ( y > data->max_dy) || (y < data->min_dy)) return;
320    
321          sad = sad16(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth),          if (!data->qpel_precision) {
322                                          data->iEdgedWidth, 256*4096);                  Reference = GetReference(x, y, data);
323                    current = data->currentMV;
324          if (sad < *(data->iMinSAD)) {                  xc = x; yc = y;
325                  *(data->iMinSAD) = sad;          } else { // x and y are in 1/4 precision
326                  data->currentMV[0].x = x; data->currentMV[0].y = y;                  Reference = Interpolate16x16qpel(x, y, 0, data);
327                  *dir = Direction; }                  xc = x/2; yc = y/2; //for chroma sad
328                    current = data->currentQMV;
329  }  }
330    
331            sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1);
332            t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
333    
334  static void          sad += (data->lambda16 * t * sad)>>10;
335  CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)          data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10;
 {  
         int32_t sad;  
         const int xb = data->currentMV[1].x;  
         const int yb = data->currentMV[1].y;  
         const uint8_t *ReferenceF, *ReferenceB;  
   
         if (( xf > data->max_dx) || ( xf < data->min_dx)  
                 || ( yf > data->max_dy) || (yf < data->min_dy)) return;  
336    
337          switch ( ((xf&1)<<1) + (yf&1) ) {          if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],
338                  case 0 : ReferenceF = data->Ref + xf/2 + (yf/2)*(data->iEdgedWidth); break;                                                                             (yc >> 1) + roundtab_79[yc & 0x3], data);
                 case 1 : ReferenceF = data->RefV + xf/2 + ((yf-1)/2)*(data->iEdgedWidth); break;  
                 case 2 : ReferenceF = data->RefH + (xf-1)/2 + (yf/2)*(data->iEdgedWidth); break;  
                 default : ReferenceF = data->RefHV + (xf-1)/2 + ((yf-1)/2)*(data->iEdgedWidth); break;  
         }  
339    
340          switch ( ((xb&1)<<1) + (yb&1) ) {          if (sad < data->iMinSAD[0]) {
341                  case 0 : ReferenceB = data->bRef + xb/2 + (yb/2)*(data->iEdgedWidth); break;                  data->iMinSAD[0] = sad;
342                  case 1 : ReferenceB = data->bRefV + xb/2 + ((yb-1)/2)*(data->iEdgedWidth); break;                  current[0].x = x; current[0].y = y;
343                  case 2 : ReferenceB = data->bRefH + (xb-1)/2 + (yb/2)*(data->iEdgedWidth); break;                  *dir = Direction;
                 default : ReferenceB = data->bRefHV + (xb-1)/2 + ((yb-1)/2)*(data->iEdgedWidth); break;  
344          }          }
345    
346          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);          if (data->temp[1] < data->iMinSAD[1]) {
347                    data->iMinSAD[1] = data->temp[1]; current[1].x = x; current[1].y = y; }
348          if (data->qpel)          if (data->temp[2] < data->iMinSAD[2]) {
349                  sad += (data->lambda16 *                  data->iMinSAD[2] = data->temp[2]; current[2].x = x; current[2].y = y; }
350                          ( d_mv_bits(2*xf - data->predMV.x, 2*yf - data->predMV.y, data->iFcode) +          if (data->temp[3] < data->iMinSAD[3]) {
351                            d_mv_bits(2*xb - data->bpredMV.x, 2*yb - data->bpredMV.y, data->iFcode)) * sad)/1000;                  data->iMinSAD[3] = data->temp[3]; current[3].x = x; current[3].y = y; }
352          else          if (data->temp[4] < data->iMinSAD[4]) {
353                  sad += (data->lambda16 *                  data->iMinSAD[4] = data->temp[4]; current[4].x = x; current[4].y = y; }
                         ( 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;  
   
         if (sad < *(data->iMinSAD)) {  
                 *(data->iMinSAD) = sad;  
                 data->currentMV->x = xf; data->currentMV->y = yf;  
                 *dir = Direction; }  
354  }  }
355    
   
356  static void  static void
357  CheckCandidateInt_qpel(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)  CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
358  {  {
359  // CheckCandidateInt variant which expects x and y in quarter pixel resolution          int32_t sad; uint32_t t;
360            const uint8_t * Reference;
361          int32_t sad;          VECTOR * current;
         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;  
362    
363          halfpelMV.x = xf/2; //forward first          if ( (x > data->max_dx) || (x < data->min_dx)
364          halfpelMV.y = yf/2;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
         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;  
365    
366          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          if (!data->qpel_precision) {
367                  GET_REFERENCE(halfpelMV.x, yf - halfpelMV.y, ref2);                  Reference = GetReference(x, y, data);
368                  interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0);                  current = data->currentMV;
369                  interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0);          } else { // x and y are in 1/4 precision
370                  interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);                  Reference = Interpolate8x8qpel(x, y, 0, 0, data);
371                  interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);                  current = data->currentQMV;
372                  break;          }
373    
374          case 2: // x qpel, y halfpel - left or right during qpel refinement          sad = sad8(data->Cur, Reference, data->iEdgedWidth);
375                  GET_REFERENCE(xf - halfpelMV.x, halfpelMV.y, ref2);          t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
                 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;  
376    
377          default: // x and y in qpel resolution - the "corners" (top left/right and          sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10;
                          // 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);  
378    
379                  interpolate8x8_avg4(ReferenceF, ref1, ref2, ref3, ref4, iEdgedWidth, 0);          if (sad < *(data->iMinSAD)) {
380                  interpolate8x8_avg4(ReferenceF+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0);                  *(data->iMinSAD) = sad;
381                  interpolate8x8_avg4(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0);                  current->x = x; current->y = y;
382                  interpolate8x8_avg4(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0);                  *dir = Direction;
383                  break;          }
384          }          }
385    
386          halfpelMV.x = xb/2; //backward  static void
387          halfpelMV.y = yb/2;  CheckCandidate32(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
         GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases  
         switch( ((xb&1)<<1) + (yb&1) )  
388          {          {
389          case 0: // pure halfpel position - shouldn't happen during a refinement step          uint32_t t;
390                  GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ReferenceB);          const uint8_t * Reference;
                 break;  
391    
392          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero even value
393                  GET_REFERENCE2(halfpelMV.x, yb - halfpelMV.y, ref2);                  (x > data->max_dx) || (x < data->min_dx)
394                  interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0);                  || (y > data->max_dy) || (y < data->min_dy) ) return;
                 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;  
395    
396          case 2: // x qpel, y halfpel - left or right during qpel refinement          Reference = GetReference(x, y, data);
397                  GET_REFERENCE2(xb - halfpelMV.x, halfpelMV.y, ref2);          t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 1);
                 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;  
398    
399          default: // x and y in qpel resolution - the "corners" (top left/right and          data->temp[0] = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp + 1);
                          // 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;  
         }  
   
         sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);  
400    
401          sad += (data->lambda16 *          data->temp[0] += (data->lambda16 * t * data->temp[0]) >> 10;
402                          ( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) +          data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10;
                           d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode)) * sad)/1000;  
403    
404          if (sad < *(data->iMinSAD)) {          if (data->temp[0] < data->iMinSAD[0]) {
405                  *(data->iMinSAD) = sad;                  data->iMinSAD[0] = data->temp[0];
406                  data->currentQMV->x = xf; data->currentQMV->y = yf;                  data->currentMV[0].x = x; data->currentMV[0].y = y;
407                  *dir = Direction; }                  *dir = Direction; }
408    
409            if (data->temp[1] < data->iMinSAD[1]) {
410                    data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; }
411            if (data->temp[2] < data->iMinSAD[2]) {
412                    data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; }
413            if (data->temp[3] < data->iMinSAD[3]) {
414                    data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; }
415            if (data->temp[4] < data->iMinSAD[4]) {
416                    data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; }
417  }  }
418    
419  static void  static void
420  CheckCandidateDirect(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)
421  {  {
422          int32_t sad = 0;          int32_t sad, xc, yc;
423          int k;          const uint8_t * Reference;
424          const uint8_t *ReferenceF;          uint32_t t;
425          const uint8_t *ReferenceB;          VECTOR * current;
         VECTOR mvs, b_mvs;  
   
         if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;  
   
         for (k = 0; k < 4; k++) {  
                 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);  
426    
427                  if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )          if ( (x > data->max_dx) || ( x < data->min_dx)
428                          || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )                  || (y > data->max_dy) || (y < data->min_dy) ) return;
                         || ( 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;  
429    
430                  switch ( ((mvs.x&1)<<1) + (mvs.y&1) ) {          if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; //non-zero even value
                         case 0 : ReferenceF = data->Ref + mvs.x/2 + (mvs.y/2)*(data->iEdgedWidth); break;  
                         case 1 : ReferenceF = data->RefV + mvs.x/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;  
                         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;  
                 }  
431    
432                  switch ( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {          if (data->qpel_precision) { // x and y are in 1/4 precision
433                          case 0 : ReferenceB = data->bRef + b_mvs.x/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;                  Reference = Interpolate16x16qpel(x, y, 0, data);
434                          case 1 : ReferenceB = data->bRefV + b_mvs.x/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;                  current = data->currentQMV;
435                          case 2 : ReferenceB = data->bRefH + (b_mvs.x-1)/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;                  xc = x/2; yc = y/2;
436                          default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;          } else {
437                    Reference = GetReference(x, y, data);
438                    current = data->currentMV;
439                    xc = x; yc = y;
440                  }                  }
441            t = d_mv_bits(x, y, data->predMV, data->iFcode,
442                                            data->qpel^data->qpel_precision, data->rrv);
443    
444                  sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),          sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);
445                                                  ReferenceF + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),          sad += (data->lambda16 * t * sad)>>10;
                                                 ReferenceB + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
                                                 data->iEdgedWidth);  
                 if (sad > *(data->iMinSAD)) return;  
         }  
446    
447          sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;          if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],
448                                                                                    (yc >> 1) + roundtab_79[yc & 0x3], data);
449    
450          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
451                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
452                  data->currentMV->x = x; data->currentMV->y = y;                  current->x = x; current->y = y;
453                  *dir = Direction; }                  *dir = Direction;
454            }
455  }  }
   
456    
457  static void  static void
458  CheckCandidateDirect_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidate32I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
459  {  {
460          int32_t sad = 0;  // maximum speed - for P/B/I decision
461          int k;          int32_t sad;
         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;  
                 }  
462    
463                  sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),          if ( (x > data->max_dx) || (x < data->min_dx)
464                                                  ReferenceF,                  || (y > data->max_dy) || (y < data->min_dy) ) return;
                                                 ReferenceB,  
                                                 data->iEdgedWidth);  
                 if (sad > *(data->iMinSAD)) return;  
         }  
465    
466          sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;          sad = sad32v_c(data->Cur, data->RefP[0] + (x>>1) + (y>>1)*(data->iEdgedWidth),
467                                            data->iEdgedWidth, data->temp+1);
468    
469          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
470                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
471                  data->currentMV->x = x; data->currentMV->y = y;                  data->currentMV[0].x = x; data->currentMV[0].y = y;
472                  *dir = Direction; }                  *dir = Direction;
473            }
474            if (data->temp[1] < data->iMinSAD[1]) {
475                    data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; }
476            if (data->temp[2] < data->iMinSAD[2]) {
477                    data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; }
478            if (data->temp[3] < data->iMinSAD[3]) {
479                    data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; }
480            if (data->temp[4] < data->iMinSAD[4]) {
481                    data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; }
482    
483  }  }
484    
485  static void  static void
486  CheckCandidateDirectno4v_qpel(const int x, const int y, 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)
487  {  {
488          int32_t sad = 0;          int32_t sad, xb, yb, xcf, ycf, xcb, ycb;
489          VECTOR mvs, b_mvs, halfpelMV;          uint32_t t;
490          const uint8_t *ref1, *ref2, *ref3, *ref4;          const uint8_t *ReferenceF, *ReferenceB;
491          const uint32_t iEdgedWidth = data->iEdgedWidth;          VECTOR *current;
         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;  
492    
493          halfpelMV.x = mvs.x/2; //forward first          if ((xf > data->max_dx) || (xf < data->min_dx) ||
494          halfpelMV.y = mvs.y/2;                  (yf > data->max_dy) || (yf < data->min_dy))
495          GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases                  return;
496          switch( ((mvs.x&1)<<1) + (mvs.y&1) ) {  
497          case 0: // pure halfpel position          if (!data->qpel_precision) {
498                  GET_REFERENCE(halfpelMV.x, halfpelMV.y, ReferenceF);                  ReferenceF = GetReference(xf, yf, data);
499                  break;                  xb = data->currentMV[1].x; yb = data->currentMV[1].y;
500                    ReferenceB = GetReferenceB(xb, yb, 1, data);
501                    current = data->currentMV;
502                    xcf = xf; ycf = yf;
503                    xcb = xb; ycb = yb;
504            } else {
505                    ReferenceF = Interpolate16x16qpel(xf, yf, 0, data);
506                    xb = data->currentQMV[1].x; yb = data->currentQMV[1].y;
507                    current = data->currentQMV;
508                    ReferenceB = Interpolate16x16qpel(xb, yb, 1, data);
509                    xcf = xf/2; ycf = yf/2;
510                    xcb = xb/2; ycb = yb/2;
511            }
512    
513          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0)
514                  GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2);                   + d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision, 0);
                 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;  
515    
516          case 2: // x qpel, y halfpel - left or right during qpel refinement          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
517                  GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref2);          sad += (data->lambda16 * t * sad)>>10;
                 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;  
518    
519          default: // x and y in qpel resolution          if (data->chroma) sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3],
520                  GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2);                                                                                  (ycf >> 1) + roundtab_79[ycf & 0x3],
521                  GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref3);                                                                                  (xcb >> 1) + roundtab_79[xcb & 0x3],
522                  GET_REFERENCE(mvs.x - halfpelMV.x, mvs.y - halfpelMV.y, ref4);                                                                                  (ycb >> 1) + roundtab_79[ycb & 0x3], data);
523    
524                  interpolate8x8_avg4(ReferenceF, ref1, ref2, ref3, ref4, iEdgedWidth, 0);          if (sad < *(data->iMinSAD)) {
525                  interpolate8x8_avg4(ReferenceF+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0);                  *(data->iMinSAD) = sad;
526                  interpolate8x8_avg4(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0);                  current->x = xf; current->y = yf;
527                  interpolate8x8_avg4(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0);                  *dir = Direction;
528                  break;          }
529          }          }
530    
531          halfpelMV.x = b_mvs.x/2; //backward  static void
532          halfpelMV.y = b_mvs.y/2;  CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
         GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1);  
         switch( ((b_mvs.x&1)<<1) + (b_mvs.y&1) )  
533          {          {
534          case 0: // pure halfpel position          int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0;
535                  GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ReferenceB);          uint32_t k;
536                  break;          const uint8_t *ReferenceF;
537            const uint8_t *ReferenceB;
538            VECTOR mvs, b_mvs;
539    
540          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
                 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;  
541    
542          case 2: // x qpel, y halfpel - left or right during qpel refinement          for (k = 0; k < 4; k++) {
543                  GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref2);                  mvs.x = data->directmvF[k].x + x;
544                  interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0);                  b_mvs.x = ((x == 0) ?
545                  interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0);                          data->directmvB[k].x
546                  interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0);                          : mvs.x - data->referencemv[k].x);
                 interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0);  
                 break;  
547    
548          default: // x and y in qpel resolution - the "corners" (top left/right and                  mvs.y = data->directmvF[k].y + y;
549                           // bottom left/right) during qpel refinement                  b_mvs.y = ((y == 0) ?
550                  GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2);                          data->directmvB[k].y
551                  GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref3);                          : mvs.y - data->referencemv[k].y);
552                  GET_REFERENCE2(b_mvs.x - halfpelMV.x, b_mvs.y - halfpelMV.y, ref4);  
553                    if ((mvs.x > data->max_dx)   || (mvs.x < data->min_dx)   ||
554                  interpolate8x8_avg4(ReferenceB, ref1, ref2, ref3, ref4, iEdgedWidth, 0);                          (mvs.y > data->max_dy)   || (mvs.y < data->min_dy)   ||
555                  interpolate8x8_avg4(ReferenceB+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0);                          (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) ||
556                  interpolate8x8_avg4(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0);                          (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) )
557                  interpolate8x8_avg4(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0);                          return;
558                  break;  
559                    if (data->qpel) {
560                            xcf += mvs.x/2; ycf += mvs.y/2;
561                            xcb += b_mvs.x/2; ycb += b_mvs.y/2;
562                    } else {
563                            xcf += mvs.x; ycf += mvs.y;
564                            xcb += b_mvs.x; ycb += b_mvs.y;
565                            mvs.x *= 2; mvs.y *= 2; //we move to qpel precision anyway
566                            b_mvs.x *= 2; b_mvs.y *= 2;
567          }          }
568    
569          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);                  ReferenceF = Interpolate8x8qpel(mvs.x, mvs.y, k, 0, data);
570          sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;                  ReferenceB = Interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data);
571    
572                    sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
573                                                    ReferenceF, ReferenceB, data->iEdgedWidth);
574                    if (sad > *(data->iMinSAD)) return;
575            }
576    
577            sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10;
578    
579            if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],
580                                                                                    (ycf >> 3) + roundtab_76[ycf & 0xf],
581                                                                                    (xcb >> 3) + roundtab_76[xcb & 0xf],
582                                                                                    (ycb >> 3) + roundtab_76[ycb & 0xf], data);
583    
584          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
585                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
586                  data->currentMV->x = x; data->currentMV->y = y;                  data->currentMV->x = x; data->currentMV->y = y;
587                  *dir = Direction; }                  *dir = Direction;
588            }
589  }  }
   
590    
591  static void  static void
592  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)
593  {  {
594          int32_t sad;          int32_t sad, xcf, ycf, xcb, ycb;
595          const uint8_t *ReferenceF;          const uint8_t *ReferenceF;
596          const uint8_t *ReferenceB;          const uint8_t *ReferenceB;
597          VECTOR mvs, b_mvs;          VECTOR mvs, b_mvs;
# Line 823  Line 613 
613                  || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )                  || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
614                  || ( 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;
615    
616          switch ( ((mvs.x&1)<<1) + (mvs.y&1) ) {          if (data->qpel) {
617                  case 0 : ReferenceF = data->Ref + mvs.x/2 + (mvs.y/2)*(data->iEdgedWidth); break;                  xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2);
618                  case 1 : ReferenceF = data->RefV + mvs.x/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;                  xcb = 4*(b_mvs.x/2); ycb = 4*(b_mvs.y/2);
619                  case 2 : ReferenceF = data->RefH + (mvs.x-1)/2 + (mvs.y/2)*(data->iEdgedWidth); break;                  ReferenceF = Interpolate16x16qpel(mvs.x, mvs.y, 0, data);
620                  default : ReferenceF = data->RefHV + (mvs.x-1)/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;                  ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data);
621          }          } else {
622                    xcf = 4*mvs.x; ycf = 4*mvs.y;
623          switch ( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {                  xcb = 4*b_mvs.x; ycb = 4*b_mvs.y;
624                  case 0 : ReferenceB = data->bRef + b_mvs.x/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;                  ReferenceF = GetReference(mvs.x, mvs.y, data);
625                  case 1 : ReferenceB = data->bRefV + b_mvs.x/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;                  ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data);
                 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;  
626          }          }
627    
628          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
629          sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;          sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10;
630    
631            if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],
632                                                                                    (ycf >> 3) + roundtab_76[ycf & 0xf],
633                                                                                    (xcb >> 3) + roundtab_76[xcb & 0xf],
634                                                                                    (ycb >> 3) + roundtab_76[ycb & 0xf], data);
635    
636          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
637                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
638                  data->currentMV->x = x; data->currentMV->y = y;                  data->currentMV->x = x; data->currentMV->y = y;
639                  *dir = Direction; }                  *dir = Direction;
640  }  }
   
 static void  
 CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  
 {  
         int32_t sad; int t;  
         const uint8_t * Reference;  
   
         if (( x > data->max_dx) || ( x < data->min_dx)  
                 || ( y > data->max_dy) || (y < data->min_dy)) return;  
   
         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;  
641          }          }
642    
         sad = sad8(data->Cur, Reference, data->iEdgedWidth);  
         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);  
   
         sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100;  
   
         if (sad < *(data->iMinSAD)) {  
                 *(data->iMinSAD) = sad;  
                 data->currentMV->x = x; data->currentMV->y = y;  
                 *dir = Direction; }  
 }  
643    
644  static void  static void
645  CheckCandidate8_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateBits16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
 // 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!  
   
646  {  {
         int32_t sad;  
         uint8_t *Reference = (uint8_t *) data->RefQ;  
         const uint8_t *ref1, *ref2, *ref3, *ref4;  
         VECTOR halfpelMV = *(data->currentMV);  
647    
648          int32_t iEdgedWidth = data->iEdgedWidth;          int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
649          uint32_t rounding = data->rounding;          int32_t bits = 0, sum;
650            VECTOR * current;
651            const uint8_t * ptr;
652            int i, cbp = 0, t, xc, yc;
653    
654          if (( x > data->max_dx) || ( x < data->min_dx)          if (( x > data->max_dx) || ( x < data->min_dx)
655                  || ( y > data->max_dy) || (y < data->min_dy)) return;                  || ( y > data->max_dy) || (y < data->min_dy)) return;
656    
657          GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);          if (!data->qpel_precision) {
658          switch( ((x&1)<<1) + (y&1) )                  ptr = GetReference(x, y, data);
659          {                  current = data->currentMV;
660          case 0: // pure halfpel position - shouldn't happen during a refinement step                  xc = x; yc = y;
661                  GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);          } else { // x and y are in 1/4 precision
662                  break;                  ptr = Interpolate16x16qpel(x, y, 0, data);
663                    current = data->currentQMV;
664          case 1: // x halfpel, y qpel - top or bottom during qpel refinement                  xc = x/2; yc = y/2;
665                  GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);          }
666    
667                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);          for(i = 0; i < 4; i++) {
668                  break;                  int s = 8*((i&1) + (i>>1)*data->iEdgedWidth);
669                    transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth);
670          case 2: // x qpel, y halfpel - left or right during qpel refinement                  fdct(in);
671                  GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);                  if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16);
672                    else sum = quant4_inter(coeff, in, data->lambda16);
673                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);                  if (sum > 0) {
674                  break;                          cbp |= 1 << (5 - i);
675                            bits += data->temp[i] = CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
676                    } else data->temp[i] = 0;
677            }
678    
679            bits += t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
680    
681            if (bits < data->iMinSAD[0]) { // there is still a chance, adding chroma
682                    xc = (xc >> 1) + roundtab_79[xc & 0x3];
683                    yc = (yc >> 1) + roundtab_79[yc & 0x3];
684    
685                    //chroma U
686                    ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[4], 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);
687                    transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2);
688                    fdct(in);
689                    if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16);
690                    else sum = quant4_inter(coeff, in, data->lambda16);
691                    if (sum > 0) {
692                            cbp |= 1 << (5 - 4);
693                            bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
694                    }
695    
696                    if (bits < data->iMinSAD[0]) {
697                            //chroma V
698                            ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[5], 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);
699                            transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2);
700                            fdct(in);
701                            if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16);
702                            else sum = quant4_inter(coeff, in, data->lambda16);
703                            if (sum > 0) {
704                                    cbp |= 1 << (5 - 5);
705                                    bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
706                            }
707                    }
708            }
709    
710            bits += xvid_cbpy_tab[15-(cbp>>2)].len;
711            bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len;
712    
713            if (bits < data->iMinSAD[0]) {
714                    data->iMinSAD[0] = bits;
715                    current[0].x = x; current[0].y = y;
716                    *dir = Direction;
717            }
718    
719            if (data->temp[0] + t < data->iMinSAD[1]) {
720                    data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; }
721            if (data->temp[1] < data->iMinSAD[2]) {
722                    data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; }
723            if (data->temp[2] < data->iMinSAD[3]) {
724                    data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; }
725            if (data->temp[3] < data->iMinSAD[4]) {
726                    data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; }
727    
728    }
729    static void
730    CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
731    {
732    
733            int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
734            int32_t sum, bits;
735            VECTOR * current;
736            const uint8_t * ptr;
737            int cbp;
738    
739          default: // x and y in qpel resolution - the "corners" (top left/right and          if ( (x > data->max_dx) || (x < data->min_dx)
740                           // bottom left/right) during qpel refinement                  || (y > data->max_dy) || (y < data->min_dy) ) return;
                 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);  
741    
742                  interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);          if (!data->qpel_precision) {
743                  break;                  ptr = GetReference(x, y, data);
744                    current = data->currentMV;
745            } else { // x and y are in 1/4 precision
746                    ptr = Interpolate8x8qpel(x, y, 0, 0, data);
747                    current = data->currentQMV;
748            }
749    
750            transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth);
751            fdct(in);
752            if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16);
753            else sum = quant4_inter(coeff, in, data->lambda16);
754            if (sum > 0) {
755                    bits = CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
756                    cbp = 1;
757            } else cbp = bits = 0;
758    
759            bits += sum = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
760    
761            if (bits < data->iMinSAD[0]) {
762                    data->temp[0] = cbp;
763                    data->iMinSAD[0] = bits;
764                    current[0].x = x; current[0].y = y;
765                    *dir = Direction;
766          }          }
   
         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; }  
767  }  }
768    
769  /* CHECK_CANDIATE FUNCTIONS END */  /* CHECK_CANDIATE FUNCTIONS END */
# Line 943  Line 778 
778    
779                  int iDirection;                  int iDirection;
780    
781                  do {          for(;;) { //forever
782                          iDirection = 0;                          iDirection = 0;
783                          if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);                          if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);
784                          if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);                          if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);
# Line 952  Line 787 
787    
788                          /* now we're doing diagonal checks near our candidate */                          /* now we're doing diagonal checks near our candidate */
789    
790                          if (iDirection) {               //checking if anything found                  if (iDirection) {               //if anything found
791                                  bDirection = iDirection;                                  bDirection = iDirection;
792                                  iDirection = 0;                                  iDirection = 0;
793                                  x = data->currentMV->x; y = data->currentMV->y;                                  x = data->currentMV->x; y = data->currentMV->y;
# Line 961  Line 796 
796                                          CHECK_CANDIDATE(x, y - iDiamondSize, 4);                                          CHECK_CANDIDATE(x, y - iDiamondSize, 4);
797                                  } else {                        // what remains here is up or down                                  } else {                        // what remains here is up or down
798                                          CHECK_CANDIDATE(x + iDiamondSize, y, 2);                                          CHECK_CANDIDATE(x + iDiamondSize, y, 2);
799                                          CHECK_CANDIDATE(x - iDiamondSize, y, 1); }                                  CHECK_CANDIDATE(x - iDiamondSize, y, 1);
800                            }
801    
802                                  if (iDirection) {                                  if (iDirection) {
803                                          bDirection += iDirection;                                          bDirection += iDirection;
804                                          x = data->currentMV->x; y = data->currentMV->y; }                                  x = data->currentMV->x; y = data->currentMV->y;
805                            }
806                          } else {                                //about to quit, eh? not so fast....                          } else {                                //about to quit, eh? not so fast....
807                                  switch (bDirection) {                                  switch (bDirection) {
808                                  case 2:                                  case 2:
# Line 1016  Line 853 
853                                  x = data->currentMV->x; y = data->currentMV->y;                                  x = data->currentMV->x; y = data->currentMV->y;
854                          }                          }
855                  }                  }
                 while (1);                              //forever  
856  }  }
857    
858  static void  static void
# Line 1066  Line 902 
902                                          CHECK_CANDIDATE(x, y - iDiamondSize, 4);                                          CHECK_CANDIDATE(x, y - iDiamondSize, 4);
903                                  } else {                        // what remains here is up or down                                  } else {                        // what remains here is up or down
904                                          CHECK_CANDIDATE(x + iDiamondSize, y, 2);                                          CHECK_CANDIDATE(x + iDiamondSize, y, 2);
905                                          CHECK_CANDIDATE(x - iDiamondSize, y, 1); }                                  CHECK_CANDIDATE(x - iDiamondSize, y, 1);
906                            }
907                                  bDirection += iDirection;                                  bDirection += iDirection;
908                                  x = data->currentMV->x; y = data->currentMV->y;                                  x = data->currentMV->x; y = data->currentMV->y;
909                          }                          }
# Line 1077  Line 913 
913    
914  /* MAINSEARCH FUNCTIONS END */  /* MAINSEARCH FUNCTIONS END */
915    
 /* HALFPELREFINE COULD BE A MAINSEARCH FUNCTION, BUT THERE IS NO NEED FOR IT */  
   
 static void  
 HalfpelRefine(const SearchData * const data)  
 {  
 /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */  
   
         VECTOR backupMV = *(data->currentMV);  
         int iDirection; //not needed  
   
         CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);  
         CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);  
         CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0);  
         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);  
 }  
   
   
916  static void  static void
917  QuarterpelRefine(const SearchData * const data)  SubpelRefine(const SearchData * const data)
918  {  {
919  /* Perform quarter pixel refinement*/  /* Do a half-pel or q-pel refinement */
920            const VECTOR centerMV = data->qpel_precision ? *data->currentQMV : *data->currentMV;
921          VECTOR backupMV = *(data->currentQMV);          int iDirection; //only needed because macro expects it
922          int iDirection; //not needed  
923            CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, 0);
924          CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);          CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, 0);
925          CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);          CHECK_CANDIDATE(centerMV.x + 1, centerMV.y, 0);
926          CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0);          CHECK_CANDIDATE(centerMV.x + 1, centerMV.y + 1, 0);
927          CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0);          CHECK_CANDIDATE(centerMV.x, centerMV.y + 1, 0);
928            CHECK_CANDIDATE(centerMV.x - 1, centerMV.y + 1, 0);
929          CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0);          CHECK_CANDIDATE(centerMV.x - 1, centerMV.y, 0);
930          CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0);          CHECK_CANDIDATE(centerMV.x - 1, centerMV.y - 1, 0);
   
         CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);  
         CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);  
   
931  }  }
932    
933  static __inline int  static __inline int
934  SkipDecisionP(const IMAGE * current, const IMAGE * reference,  SkipDecisionP(const IMAGE * current, const IMAGE * reference,
935                                                          const int x, const int y,                                                          const int x, const int y,
936                                                          const uint32_t iEdgedWidth, const uint32_t iQuant)                                                          const uint32_t stride, const uint32_t iQuant, int rrv)
937    
938  {  {
939  /*      keep repeating checks for all b-frames before this P frame,          int offset = (x + y*stride)*8;
940          to make sure that SKIP is possible (todo)          if(!rrv) {
941          how: if skip is not possible set sad00 to a very high value */                  uint32_t sadC = sad8(current->u + offset,
942                                                    reference->u + offset, stride);
         uint32_t sadC = sad8(current->u + x*8 + y*(iEdgedWidth/2)*8,  
                                         reference->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2);  
943          if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;          if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
944          sadC += sad8(current->v + (x + y*(iEdgedWidth/2))*8,                  sadC += sad8(current->v + offset,
945                                          reference->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);                                                  reference->v + offset, stride);
946          if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;          if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
947                    return 1;
948    
949            } else {
950                    uint32_t sadC = sad16(current->u + 2*offset,
951                                                    reference->u + 2*offset, stride, 256*4096);
952                    if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;
953                    sadC += sad16(current->v + 2*offset,
954                                                    reference->v + 2*offset, stride, 256*4096);
955                    if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;
956          return 1;          return 1;
957  }  }
958    }
959    
960  static __inline void  static __inline void
961  SkipMacroblockP(MACROBLOCK *pMB, const int32_t sad)  SkipMacroblockP(MACROBLOCK *pMB, const int32_t sad)
962  {  {
963          pMB->mode = MODE_NOT_CODED;          pMB->mode = MODE_NOT_CODED;
964          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = zeroMV;
965          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;          pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = zeroMV;
   
         pMB->qmvs[0].x = pMB->qmvs[1].x = pMB->qmvs[2].x = pMB->qmvs[3].x = 0;  
         pMB->qmvs[0].y = pMB->qmvs[1].y = pMB->qmvs[2].y = pMB->qmvs[3].y = 0;  
   
966          pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;          pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
967  }  }
968    
# Line 1167  Line 979 
979          const IMAGE *const pCurrent = &current->image;          const IMAGE *const pCurrent = &current->image;
980          const IMAGE *const pRef = &reference->image;          const IMAGE *const pRef = &reference->image;
981    
982          const VECTOR zeroMV = { 0, 0 };          uint32_t mb_width = pParam->mb_width;
983            uint32_t mb_height = pParam->mb_height;
984            const uint32_t iEdgedWidth = pParam->edged_width;
985            const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags);
986    
987          uint32_t x, y;          uint32_t x, y;
988          uint32_t iIntra = 0;          uint32_t iIntra = 0;
989          int32_t InterBias, quant = current->quant, sad00;          int32_t quant = current->quant, sad00;
990          uint8_t *qimage;          int skip_thresh = \
991                    INITIAL_SKIP_THRESH * \
992                    (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \
993                    (current->vop_flags & XVID_VOP_MODEDECISION_BITS ? 2:1);
994    
995          // some pre-initialized thingies for SearchP          // some pre-initialized thingies for SearchP
996          int32_t temp[5];          int32_t temp[8];
997          VECTOR currentMV[5];          VECTOR currentMV[5];
998          VECTOR currentQMV[5];          VECTOR currentQMV[5];
999          int32_t iMinSAD[5];          int32_t iMinSAD[5];
1000            DECLARE_ALIGNED_MATRIX(dct_space, 2, 64, int16_t, CACHE_LINE);
1001          SearchData Data;          SearchData Data;
1002          Data.iEdgedWidth = pParam->edged_width;          memset(&Data, 0, sizeof(SearchData));
1003            Data.iEdgedWidth = iEdgedWidth;
1004          Data.currentMV = currentMV;          Data.currentMV = currentMV;
1005          Data.currentQMV = currentQMV;          Data.currentQMV = currentQMV;
1006          Data.iMinSAD = iMinSAD;          Data.iMinSAD = iMinSAD;
1007          Data.temp = temp;          Data.temp = temp;
1008          Data.iFcode = current->fcode;          Data.iFcode = current->fcode;
1009          Data.rounding = pParam->m_rounding_type;          Data.rounding = pParam->m_rounding_type;
1010          Data.qpel = pParam->m_quarterpel;          Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);
1011          Data.chroma = current->global_flags & XVID_ME_COLOUR;          Data.chroma = MotionFlags & XVID_ME_CHROMA16;
1012            Data.rrv = (current->vop_flags & XVID_VOP_REDUCED ? 1:0);
1013            Data.dctSpace = dct_space;
1014    
1015            if ((current->vop_flags & XVID_VOP_REDUCED)) {
1016                    mb_width = (pParam->width + 31) / 32;
1017                    mb_height = (pParam->height + 31) / 32;
1018                    Data.qpel = 0;
1019            }
1020    
1021          if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)          Data.RefQ = pRefV->u; // a good place, also used in MC (for similar purpose)
                 return 1; // allocate some mem for qpel interpolated blocks  
                                   // somehow this is dirty since I think we shouldn't use malloc outside  
                                   // encoder_create() - so please fix me!  
         Data.RefQ = qimage;  
1022          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();
1023    
1024          for (y = 0; y < pParam->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
1025                  for (x = 0; x < pParam->mb_width; x++)  {                  for (x = 0; x < mb_width; x++)  {
1026                          MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];                          MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
1027    
1028                          pMB->sad16                          if (!Data.rrv) pMB->sad16 =
1029                                  = sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16,                                  sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16,
1030                                                          pRef->y + (x + y * pParam->edged_width) * 16,                                                          pRef->y + (x + y * iEdgedWidth) * 16,
1031                                                          pParam->edged_width, pMB->sad8 );                                                          pParam->edged_width, pMB->sad8 );
1032    
1033                          if (Data.chroma) {                          else pMB->sad16 =
1034                                  pMB->sad16 += sad8(pCurrent->u + x*8 + y*(pParam->edged_width/2)*8,                                  sad32v_c(pCurrent->y + (x + y * iEdgedWidth) * 32,
1035                                                                  pRef->u + x*8 + y*(pParam->edged_width/2)*8, pParam->edged_width/2);                                                          pRef->y + (x + y * iEdgedWidth) * 32,
1036                                                            pParam->edged_width, pMB->sad8 );
1037    
1038                                  pMB->sad16 += sad8(pCurrent->v + (x + y*(pParam->edged_width/2))*8,                          if (Data.chroma) {
1039                                                                  pRef->v + (x + y*(pParam->edged_width/2))*8, pParam->edged_width/2);                                  Data.temp[7] = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8,
1040                                                                            pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2)
1041                                                                    + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8,
1042                                                                            pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);
1043                                    pMB->sad16 += Data.temp[7];
1044                          }                          }
1045    
1046                          sad00 = pMB->sad16; //if no gmc; else sad00 = (..)                          sad00 = pMB->sad16;
1047    
1048                          if (!(current->global_flags & XVID_LUMIMASKING)) {                          if (pMB->dquant != 0) {
                                 pMB->dquant = NO_CHANGE;  
                                 pMB->quant = current->quant;  
                         } else {  
                                 if (pMB->dquant != NO_CHANGE) {  
1049                                          quant += DQtab[pMB->dquant];                                          quant += DQtab[pMB->dquant];
1050                                          if (quant > 31) quant = 31;                                          if (quant > 31) quant = 31;
1051                                          else if (quant < 1) quant = 1;                                          else if (quant < 1) quant = 1;
1052                                  }                                  }
1053                                  pMB->quant = quant;                          pMB->quant = current->quant;
                         }  
1054    
1055  //initial skip decision  //initial skip decision
1056  /* no early skip for GMC (global vector = skip vector is unknown!)  */  /* no early skip for GMC (global vector = skip vector is unknown!)  */
1057                          if (current->coding_type == P_VOP)      { /* no fast SKIP for S(GMC)-VOPs */                          if (!(current->vol_flags & XVID_VOL_GMC))       { /* no fast SKIP for S(GMC)-VOPs */
1058                                  if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH)                                  if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)
1059                                          if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {                                          if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {
1060                                                  SkipMacroblockP(pMB, sad00);                                                  SkipMacroblockP(pMB, sad00);
1061                                                  continue;                                                  continue;
1062                                          }                                          }
1063                          }                          }
1064    
1065                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
1066                                                  y, current->motion_flags, pMB->quant,                                                  y, MotionFlags, current->vol_flags, pMB->quant,
1067                                                  &Data, pParam, pMBs, reference->mbs,                                                  &Data, pParam, pMBs, reference->mbs,
1068                                                  current->global_flags & XVID_INTER4V, pMB);                                                  current->vop_flags & XVID_VOP_INTER4V, pMB);
1069    
1070  /* final skip decision, a.k.a. "the vector you found, really that good?" */  /* final skip decision, a.k.a. "the vector you found, really that good?" */
1071                          if (current->coding_type == P_VOP)      {                          if (!(current->vol_flags & XVID_VOL_GMC || current->vop_flags & XVID_VOP_MODEDECISION_BITS)) {
1072                                  if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP)                                  if ( pMB->dquant == 0 && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) {
1073                                  && ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH) )                                          if ( (100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1) )
1074                                          if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {                                                  if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv))
1075                                                  SkipMacroblockP(pMB, sad00);                                                  SkipMacroblockP(pMB, sad00);
                                                 continue;  
                                         }  
                         }  
   
 /* finally, intra decision */  
   
                         InterBias = MV16_INTER_BIAS;  
                         if (pMB->quant > 8)  InterBias += 100 * (pMB->quant - 8); // to make high quants work  
                         if (y != 0)  
                                 if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;  
                         if (x != 0)  
                                 if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;  
   
                         if (Data.chroma) InterBias += 50; // to compensate bigger SAD  
   
                         if (InterBias < pMB->sad16)  {  
                                 const int32_t deviation =  
                                         dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,  
                                                   pParam->edged_width);  
   
                                 if (deviation < (pMB->sad16 - InterBias)) {  
                                         if (++iIntra >= iLimit) { free(qimage); return 1; }  
                                         pMB->mode = MODE_INTRA;  
                                         pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] =  
                                                         pMB->mvs[3] = zeroMV;  
                                         pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] =  
                                                         pMB->qmvs[3] = zeroMV;  
                                         pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] =  
                                                 pMB->sad8[3] = 0;  
1076                                  }                                  }
1077                          }                          }
1078                            if (pMB->mode == MODE_INTRA)
1079                                    if (++iIntra > iLimit) return 1;
1080                  }                  }
1081          }          }
         free(qimage);  
   
         if (current->coding_type == S_VOP)      /* first GMC step only for S(GMC)-VOPs */  
                 current->GMC_MV = GlobalMotionEst( pMBs, pParam, current->fcode );  
         else  
                 current->GMC_MV = zeroMV;  
1082    
1083            if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
1084            {
1085                    current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV);
1086            }
1087          return 0;          return 0;
1088  }  }
1089    
1090    
 #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16)  
   
1091  static __inline int  static __inline int
1092  make_mask(const VECTOR * const pmv, const int i)  make_mask(const VECTOR * const pmv, const int i)
1093  {  {
# Line 1302  Line 1095 
1095          for (j = 0; j < i; j++) {          for (j = 0; j < i; j++) {
1096                  if (MVequal(pmv[i], pmv[j])) return 0; // same vector has been checked already                  if (MVequal(pmv[i], pmv[j])) return 0; // same vector has been checked already
1097                  if (pmv[i].x == pmv[j].x) {                  if (pmv[i].x == pmv[j].x) {
1098                          if (pmv[i].y == pmv[j].y + iDiamondSize) { mask &= ~4; continue; }                          if (pmv[i].y == pmv[j].y + iDiamondSize) mask &= ~4;
1099                          if (pmv[i].y == pmv[j].y - iDiamondSize) { mask &= ~8; continue; }                          else if (pmv[i].y == pmv[j].y - iDiamondSize) mask &= ~8;
1100                  } else                  } else
1101                          if (pmv[i].y == pmv[j].y) {                          if (pmv[i].y == pmv[j].y) {
1102                                  if (pmv[i].x == pmv[j].x + iDiamondSize) { mask &= ~1; continue; }                                  if (pmv[i].x == pmv[j].x + iDiamondSize) mask &= ~1;
1103                                  if (pmv[i].x == pmv[j].x - iDiamondSize) { mask &= ~2; continue; }                                  else if (pmv[i].x == pmv[j].x - iDiamondSize) mask &= ~2;
1104                          }                          }
1105          }          }
1106          return mask;          return mask;
1107  }  }
1108    
1109  static __inline void  static __inline void
1110  PreparePredictionsP(VECTOR * const pmv, int x, int y, const int iWcount,  PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,
1111                          const int iHcount, const MACROBLOCK * const prevMB)                          int iHcount, const MACROBLOCK * const prevMB, int rrv)
1112  {  {
1113    
1114  //this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself  //this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself
1115            if (rrv) { iWcount /= 2; iHcount /= 2; }
1116    
1117          if ( (y != 0) && (x != (iWcount-1)) ) {         // [5] top-right neighbour          if ( (y != 0) && (x < (iWcount-1)) ) {          // [5] top-right neighbour
1118                  pmv[5].x = EVEN(pmv[3].x);                  pmv[5].x = EVEN(pmv[3].x);
1119                  pmv[5].y = EVEN(pmv[3].y);                  pmv[5].y = EVEN(pmv[3].y);
1120          } else pmv[5].x = pmv[5].y = 0;          } else pmv[5].x = pmv[5].y = 0;
# Line 1339  Line 1133 
1133          pmv[2].x = EVEN(prevMB->mvs[0].x); // [2] is last frame          pmv[2].x = EVEN(prevMB->mvs[0].x); // [2] is last frame
1134          pmv[2].y = EVEN(prevMB->mvs[0].y);          pmv[2].y = EVEN(prevMB->mvs[0].y);
1135    
1136          if ((x != iWcount-1) && (y != iHcount-1)) {          if ((x < iWcount-1) && (y < iHcount-1)) {
1137                  pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); //[6] right-down neighbour in last frame                  pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); //[6] right-down neighbour in last frame
1138                  pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);                  pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);
1139          } else pmv[6].x = pmv[6].y = 0;          } else pmv[6].x = pmv[6].y = 0;
1140    
1141            if (rrv) {
1142                    int i;
1143                    for (i = 0; i < 7; i++) {
1144                            pmv[i].x = RRV_MV_SCALEUP(pmv[i].x);
1145                            pmv[i].y = RRV_MV_SCALEUP(pmv[i].y);
1146                    }
1147            }
1148    }
1149    
1150    static int
1151    ModeDecision(const uint32_t iQuant, SearchData * const Data,
1152                    int inter4v,
1153                    MACROBLOCK * const pMB,
1154                    const MACROBLOCK * const pMBs,
1155                    const int x, const int y,
1156                    const MBParam * const pParam,
1157                    const uint32_t MotionFlags,
1158                    const uint32_t VopFlags)
1159    {
1160    
1161            int mode = MODE_INTER;
1162    
1163            if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision
1164                    int sad;
1165                    int InterBias = MV16_INTER_BIAS;
1166                    if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
1167                            Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) {
1168                            mode = MODE_INTER;
1169                            sad = Data->iMinSAD[0];
1170                    } else {
1171                            mode = MODE_INTER4V;
1172                            sad = Data->iMinSAD[1] + Data->iMinSAD[2] +
1173                                                    Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant;
1174                            Data->iMinSAD[0] = sad;
1175                    }
1176    
1177                    /* intra decision */
1178    
1179                    if (iQuant > 8) InterBias += 100 * (iQuant - 8); // to make high quants work
1180                    if (y != 0)
1181                            if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;
1182                    if (x != 0)
1183                            if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
1184    
1185                    if (Data->chroma) InterBias += 50; // to compensate bigger SAD
1186                    if (Data->rrv) InterBias *= 4;
1187    
1188                    if (InterBias < pMB->sad16) {
1189                            int32_t deviation;
1190                            if (!Data->rrv) deviation = dev16(Data->Cur, Data->iEdgedWidth);
1191                            else deviation = dev16(Data->Cur, Data->iEdgedWidth) +
1192                                    dev16(Data->Cur+8, Data->iEdgedWidth) +
1193                                    dev16(Data->Cur + 8*Data->iEdgedWidth, Data->iEdgedWidth) +
1194                                    dev16(Data->Cur+8+8*Data->iEdgedWidth, Data->iEdgedWidth);
1195    
1196                            if (deviation < (sad - InterBias)) return MODE_INTRA;
1197                    }
1198                    return mode;
1199    
1200            } else {
1201    
1202                    int bits, intra, i;
1203                    VECTOR backup[5], *v;
1204                    Data->lambda16 = iQuant;
1205            Data->lambda8 = (pParam->vol_flags & XVID_VOL_MPEGQUANT)?1:0;
1206    
1207                    v = Data->qpel ? Data->currentQMV : Data->currentMV;
1208                    for (i = 0; i < 5; i++) {
1209                            Data->iMinSAD[i] = 256*4096;
1210                            backup[i] = v[i];
1211                    }
1212    
1213                    bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags);
1214                    if (bits == 0) return MODE_INTER; // quick stop
1215    
1216                    if (inter4v) {
1217                            int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup);
1218                            if (bits_inter4v < bits) { Data->iMinSAD[0] = bits = bits_inter4v; mode = MODE_INTER4V; }
1219                    }
1220    
1221                    intra = CountMBBitsIntra(Data);
1222    
1223                    if (intra < bits) { *Data->iMinSAD = bits = intra; return MODE_INTRA; }
1224    
1225                    return mode;
1226            }
1227  }  }
1228    
1229  static void  static void
# Line 1354  Line 1235 
1235                  const int x,                  const int x,
1236                  const int y,                  const int y,
1237                  const uint32_t MotionFlags,                  const uint32_t MotionFlags,
1238                    const uint32_t VopFlags,
1239                  const uint32_t iQuant,                  const uint32_t iQuant,
1240                  SearchData * const Data,                  SearchData * const Data,
1241                  const MBParam * const pParam,                  const MBParam * const pParam,
# Line 1366  Line 1248 
1248          int i, iDirection = 255, mask, threshA;          int i, iDirection = 255, mask, threshA;
1249          VECTOR pmv[7];          VECTOR pmv[7];
1250    
         get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);  //has to be changed to get_pmv(2)()  
1251          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,
1252                                  pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);
1253    
1254          Data->predMV = pmv[0];          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);
1255    
1256          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;          Data->temp[5] = Data->temp[6] = 0; // chroma-sad cache
1257          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;          i = Data->rrv ? 2 : 1;
1258          Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i;
1259            Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;
1260          Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16;          Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;
1261          Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;  
1262          Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;          Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16*i;
1263          Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16*i;
1264          Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;          Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16*i;
1265          Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;          Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16*i;
1266            Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;
1267            Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;
1268    
1269          Data->lambda16 = lambda_vec16[iQuant];          Data->lambda16 = lambda_vec16[iQuant];
1270          Data->lambda8 = lambda_vec8[iQuant];          Data->lambda8 = lambda_vec8[iQuant];
1271            Data->qpel_precision = 0;
1272    
1273            if (pMB->dquant != 0) inter4v = 0;
1274    
1275            memset(Data->currentMV, 0, 5*sizeof(VECTOR));
1276    
1277          if (!(MotionFlags & PMV_HALFPEL16)) {          if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1278                  Data->min_dx = EVEN(Data->min_dx);          else Data->predMV = pmv[0];
                 Data->max_dx = EVEN(Data->max_dx);  
                 Data->min_dy = EVEN(Data->min_dy);  
                 Data->max_dy = EVEN(Data->max_dy); }  
   
         if (pMB->dquant != NO_CHANGE) inter4v = 0;  
   
         for(i = 0;  i < 5; i++)  
                 Data->currentMV[i].x = Data->currentMV[i].y = 0;  
   
         if (pParam->m_quarterpel) {  
                 Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 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);  
1279    
1280          Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000;          i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0, 0);
1281          Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100;          Data->iMinSAD[0] = pMB->sad16 + ((Data->lambda16 * i * pMB->sad16)>>10);
1282            Data->iMinSAD[1] = pMB->sad8[0] + ((Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS)) >> 10);
1283          Data->iMinSAD[2] = pMB->sad8[1];          Data->iMinSAD[2] = pMB->sad8[1];
1284          Data->iMinSAD[3] = pMB->sad8[2];          Data->iMinSAD[3] = pMB->sad8[2];
1285          Data->iMinSAD[4] = pMB->sad8[3];          Data->iMinSAD[4] = pMB->sad8[3];
1286    
1287          if ((x == 0) && (y == 0)) threshA = 512;          if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) || (x | y)) {
1288          else {                  threshA = Data->temp[0]; // that's where we keep this SAD atm
                 threshA = Data->temp[0]; // that's when we keep this SAD atm  
1289                  if (threshA < 512) threshA = 512;                  if (threshA < 512) threshA = 512;
1290                  if (threshA > 1024) threshA = 1024; }                  else if (threshA > 1024) threshA = 1024;
1291            } else
1292                    threshA = 512;
1293    
1294          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
1295                                          prevMBs + x + y * pParam->mb_width);                                          prevMBs + x + y * pParam->mb_width, Data->rrv);
1296    
1297          if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16;          if (!Data->rrv) {
1298          else CheckCandidate = CheckCandidate16no4v;                  if (inter4v | Data->chroma) CheckCandidate = CheckCandidate16;
1299                            else CheckCandidate = CheckCandidate16no4v; //for extra speed
1300            } else CheckCandidate = CheckCandidate32;
1301    
1302  /* main loop. checking all predictions */  /* main loop. checking all predictions (but first, which is 0,0 and has been checked in MotionEstimation())*/
1303    
1304          for (i = 1; i < 7; i++) {          for (i = 1; i < 7; i++) {
1305                  if (!(mask = make_mask(pmv, i)) ) continue;                  if (!(mask = make_mask(pmv, i)) ) continue;
1306                  (*CheckCandidate)(pmv[i].x, pmv[i].y, mask, &iDirection, Data);                  CheckCandidate(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1307                  if (Data->iMinSAD[0] <= threshA) break;                  if (Data->iMinSAD[0] <= threshA) break;
1308          }          }
1309    
1310          if ((Data->iMinSAD[0] <= threshA) ||          if ((Data->iMinSAD[0] <= threshA) ||
1311                          (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&                          (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&
1312                          (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) {                          (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) {
1313                  inter4v = 0;                  if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) inter4v = 0;      }
1314          } else {          else {
1315    
1316                  MainSearchFunc * MainSearchPtr;                  MainSearchFunc * MainSearchPtr;
1317                  if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;                  if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;
1318                  else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1319                          else MainSearchPtr = DiamondSearch;                          else MainSearchPtr = DiamondSearch;
1320    
1321                  (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection);                  MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
1322    
1323  /* extended search, diamond starting in 0,0 and in prediction.  /* extended search, diamond starting in 0,0 and in prediction.
1324          note that this search is/might be done in halfpel positions,          note that this search is/might be done in halfpel positions,
1325          which makes it more different than the diamond above */          which makes it more different than the diamond above */
1326    
1327                  if (MotionFlags & PMV_EXTSEARCH16) {                  if (MotionFlags & XVID_ME_EXTSEARCH16) {
1328                          int32_t bSAD;                          int32_t bSAD;
1329                          VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];                          VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];
1330                          if (!(MotionFlags & PMV_HALFPELREFINE16)) // who's gonna use extsearch and no halfpel?                          if (Data->rrv) {
1331                                  startMV.x = EVEN(startMV.x); startMV.y = EVEN(startMV.y);                                  startMV.x = RRV_MV_SCALEUP(startMV.x);
1332                                    startMV.y = RRV_MV_SCALEUP(startMV.y);
1333                            }
1334                          if (!(MVequal(startMV, backupMV))) {                          if (!(MVequal(startMV, backupMV))) {
1335                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1336    
1337                                  (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);                                  CheckCandidate(startMV.x, startMV.y, 255, &iDirection, Data);
1338                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);                                  MainSearchPtr(startMV.x, startMV.y, Data, 255);
1339                                  if (bSAD < Data->iMinSAD[0]) {                                  if (bSAD < Data->iMinSAD[0]) {
1340                                          Data->currentMV[0] = backupMV;                                          Data->currentMV[0] = backupMV;
1341                                          Data->iMinSAD[0] = bSAD; }                                          Data->iMinSAD[0] = bSAD; }
1342                          }                          }
1343    
1344                          backupMV = Data->currentMV[0];                          backupMV = Data->currentMV[0];
1345                          if (MotionFlags & PMV_HALFPELREFINE16) startMV.x = startMV.y = 1;                          startMV.x = startMV.y = 1;
                         else startMV.x = startMV.y = 0;  
1346                          if (!(MVequal(startMV, backupMV))) {                          if (!(MVequal(startMV, backupMV))) {
1347                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1348    
1349                                  (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);                                  CheckCandidate(startMV.x, startMV.y, 255, &iDirection, Data);
1350                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);                                  MainSearchPtr(startMV.x, startMV.y, Data, 255);
1351                                  if (bSAD < Data->iMinSAD[0]) {                                  if (bSAD < Data->iMinSAD[0]) {
1352                                          Data->currentMV[0] = backupMV;                                          Data->currentMV[0] = backupMV;
1353                                          Data->iMinSAD[0] = bSAD; }                                          Data->iMinSAD[0] = bSAD; }
# Line 1475  Line 1355 
1355                  }                  }
1356          }          }
1357    
1358          if (MotionFlags & PMV_HALFPELREFINE16) HalfpelRefine(Data);          if (MotionFlags & XVID_ME_HALFPELREFINE16)
1359                            SubpelRefine(Data);
1360    
1361          for(i = 0; i < 5; i++) {          for(i = 0; i < 5; i++) {
1362                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
1363                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
1364          }          }
1365    
1366          if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {          if (MotionFlags & XVID_ME_QUARTERPELREFINE16) {
1367    
                 CheckCandidate = CheckCandidate16_qpel;  
1368                  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,
1369                                  pParam->width, pParam->height, Data->iFcode, 0);                                  pParam->width, pParam->height, Data->iFcode, 1, 0);
1370                    Data->qpel_precision = 1;
1371                  QuarterpelRefine(Data);                  SubpelRefine(Data);
1372          }          }
1373    
1374          if (Data->iMinSAD[0] < (int32_t)iQuant * 30 ) inter4v = 0;          if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0;
1375    
1376          if (inter4v) {          if (inter4v) {
1377                  SearchData Data8;                  SearchData Data8;
1378                  Data8.iFcode = Data->iFcode;                  memcpy(&Data8, Data, sizeof(SearchData)); //quick copy of common data
1379                  Data8.lambda8 = Data->lambda8;  
                 Data8.iEdgedWidth = Data->iEdgedWidth;  
                 Data8.RefQ = Data->RefQ;  
                 Data8.qpel = Data->qpel;  
1380                  Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);                  Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
1381                  Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);                  Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
1382                  Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);                  Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
1383                  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);
1384    
1385                  if (Data->chroma) {                  if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_BITS))) {
1386                          int sum, dx, dy;                          // chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, there is no reason to compute it
1387                            int sumx = 0, sumy = 0;
1388                          if(pParam->m_quarterpel) {                          const int div = Data->qpel ? 2 : 1;
1389                                  sum = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2;                          const VECTOR * const mv = Data->qpel ? pMB->qmvs : pMB->mvs;
                         } else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
                         dy = (sum >> 3) + roundtab_76[sum & 0xf];  
1390    
1391                          if(pParam->m_quarterpel) {                          for (i = 0; i < 4; i++) {
1392                                  sum = pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2;                                  sumx += mv[i].x / div;
1393                          } else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                                  sumy += mv[i].y / div;
1394                          dx = (sum >> 3) + roundtab_76[sum & 0xf];                          }
1395    
1396                          Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);                          Data->iMinSAD[1] += ChromaSAD(  (sumx >> 3) + roundtab_76[sumx & 0xf],
1397                                                                                            (sumy >> 3) + roundtab_76[sumy & 0xf], Data);
1398                  }                  }
1399          }          }
1400    
1401          if (!(inter4v) ||          inter4v = ModeDecision(iQuant, Data, inter4v, pMB, pMBs, x, y, pParam, MotionFlags, VopFlags);
1402                  (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +  
1403                          Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {          if (Data->rrv) {
1404  // INTER MODE                          Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);
1405                            Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);
1406            }
1407    
1408            if (inter4v == MODE_INTER) {
1409                  pMB->mode = MODE_INTER;                  pMB->mode = MODE_INTER;
1410                  pMB->mvs[0] = pMB->mvs[1]                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
1411                          = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];                  pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];
1412    
1413                    if(Data->qpel) {
1414                  pMB->qmvs[0] = pMB->qmvs[1]                  pMB->qmvs[0] = pMB->qmvs[1]
1415                          = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];                          = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
1416                            pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;
1417                  pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =                          pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;
                         pMB->sad8[2] = pMB->sad8[3] =  Data->iMinSAD[0];  
   
                 if(pParam->m_quarterpel) {  
                         pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x;  
                         pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y;  
1418                  } else {                  } else {
1419                          pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;                          pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1420                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
1421                  }                  }
1422          } else {  
1423  // INTER4V MODE; all other things are already set in Search8          } else if (inter4v == MODE_INTER4V) {
1424                  pMB->mode = MODE_INTER4V;                  pMB->mode = MODE_INTER4V;
1425                  pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] +                  pMB->sad16 = Data->iMinSAD[0];
1426                          Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * iQuant;          } else { // INTRA mode
1427                    SkipMacroblockP(pMB, 0); // not skip, but similar enough
1428                    pMB->mode = MODE_INTRA;
1429          }          }
1430    
1431  }  }
1432    
1433  static void  static void
# Line 1560  Line 1440 
1440                  const int block,                  const int block,
1441                  SearchData * const Data)                  SearchData * const Data)
1442  {  {
1443            int i = 0;
1444          Data->iMinSAD = OldData->iMinSAD + 1 + block;          Data->iMinSAD = OldData->iMinSAD + 1 + block;
1445          Data->currentMV = OldData->currentMV + 1 + block;          Data->currentMV = OldData->currentMV + 1 + block;
1446          Data->currentQMV = OldData->currentQMV + 1 + block;          Data->currentQMV = OldData->currentQMV + 1 + block;
1447    
1448          if(pParam->m_quarterpel) {          if(Data->qpel) {
1449                  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);
1450                  if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *                  if (block != 0) i = d_mv_bits(  Data->currentQMV->x, Data->currentQMV->y,
1451                                                                          d_mv_bits(      Data->currentQMV->x - Data->predQMV.x,                                                                                  Data->predMV, Data->iFcode, 0, 0);
                                                                                                 Data->currentQMV->y - Data->predQMV.y,  
                                                                                                 Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;  
1452          } else {          } else {
1453                  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);
1454                  if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *                  if (block != 0) i = d_mv_bits(  Data->currentMV->x, Data->currentMV->y,
1455                                                                          d_mv_bits(      Data->currentMV->x - Data->predMV.x,                                                                                  Data->predMV, Data->iFcode, 0, Data->rrv);
                                                                                                 Data->currentMV->y - Data->predMV.y,  
                                                                                                 Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;  
1456          }          }
1457    
1458          if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) {          *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;
1459    
1460                  Data->Ref = OldData->Ref + 8 * ((block&1) + pParam->edged_width*(block>>1));          if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {
                 Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1));  
                 Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1));  
                 Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1));  
1461    
1462                  Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1));                  if (Data->rrv) i = 16; else i = 8;
1463    
1464                    Data->RefP[0] = OldData->RefP[0] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
1465                    Data->RefP[1] = OldData->RefP[1] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
1466                    Data->RefP[2] = OldData->RefP[2] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
1467                    Data->RefP[3] = OldData->RefP[3] + i * ((block&1) + Data->iEdgedWidth*(block>>1));
1468    
1469                    Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1));
1470                    Data->qpel_precision = 0;
1471    
1472                  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,
1473                                  pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);                                          pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);
1474                  CheckCandidate = CheckCandidate8;  
1475                    if (!Data->rrv) CheckCandidate = CheckCandidate8;
1476                    else CheckCandidate = CheckCandidate16no4v;
1477    
1478                  if (MotionFlags & PMV_EXTSEARCH8) {                  if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_BITS))) {
1479                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1480    
1481                          MainSearchFunc *MainSearchPtr;                          MainSearchFunc *MainSearchPtr;
1482                          if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch;                          if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = SquareSearch;
1483                                  else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;                                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;
1484                                          else MainSearchPtr = DiamondSearch;                                          else MainSearchPtr = DiamondSearch;
1485    
1486                          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255);
1487    
1488                          if(*(Data->iMinSAD) < temp_sad) {                          if(*(Data->iMinSAD) < temp_sad) {
1489                                          Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector                                          Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
# Line 1607  Line 1491 
1491                          }                          }
1492                  }                  }
1493    
1494                  if (MotionFlags & PMV_HALFPELREFINE8) {                  if (MotionFlags & XVID_ME_HALFPELREFINE8) {
1495                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1496    
1497                          HalfpelRefine(Data); // perform halfpel refine of current best vector                          SubpelRefine(Data); // perform halfpel refine of current best vector
1498    
1499                          if(*(Data->iMinSAD) < temp_sad) { // we have found a better match                          if(*(Data->iMinSAD) < temp_sad) { // we have found a better match
1500                                  Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector                                  Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
# Line 1618  Line 1502 
1502                          }                          }
1503                  }                  }
1504    
1505                  if(pParam->m_quarterpel) {                  if (Data->qpel && MotionFlags & XVID_ME_QUARTERPELREFINE8) {
1506                          if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) &&                                  Data->qpel_precision = 1;
                                 (MotionFlags & PMV_QUARTERPELREFINE8)) {  
                         CheckCandidate = CheckCandidate8_qpel;  
1507                          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,
1508                                  pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);                                          pParam->width, pParam->height, Data->iFcode, 1, 0);
1509                          QuarterpelRefine(Data);                                  SubpelRefine(Data);
                         }  
1510                  }                  }
1511          }          }
1512    
1513          if(pParam->m_quarterpel) {          if (Data->rrv) {
1514                  pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x;                          Data->currentMV->x = RRV_MV_SCALEDOWN(Data->currentMV->x);
1515                  pMB->pmvs[block].y = Data->currentQMV->y - Data->predQMV.y;                          Data->currentMV->y = RRV_MV_SCALEDOWN(Data->currentMV->y);
1516          }          }
1517          else {  
1518            if(Data->qpel) {
1519                    pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x;
1520                    pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y;
1521                    pMB->qmvs[block] = *Data->currentQMV;
1522            } else {
1523                  pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;                  pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;
1524                  pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;                  pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;
1525          }          }
1526    
1527          pMB->mvs[block] = *(Data->currentMV);          pMB->mvs[block] = *Data->currentMV;
1528          pMB->qmvs[block] = *(Data->currentQMV);          pMB->sad8[block] = 4 * *Data->iMinSAD;
   
         pMB->sad8[block] =  4 * (*Data->iMinSAD);  
1529  }  }
1530    
1531  /* B-frames code starts here */  /* motion estimation for B-frames */
1532    
1533  static __inline VECTOR  static __inline VECTOR
1534  ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode)  ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode)
1535  {  {
1536  /* the stupidiest function ever */  /* the stupidiest function ever */
1537          if (mode == MODE_FORWARD) return pMB->mvs[0];          return (mode == MODE_FORWARD ? pMB->mvs[0] : pMB->b_mvs[0]);
         else return pMB->b_mvs[0];  
1538  }  }
1539    
1540  static void __inline  static void __inline
# Line 1684  Line 1567 
1567                  pmv[5].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);                  pmv[5].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);
1568          } else pmv[5].x = pmv[5].y = 0;          } else pmv[5].x = pmv[5].y = 0;
1569    
1570          if ((x != 0)&&(y != 0)) {          if (x != 0 && y != 0) {
1571                  pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr);                  pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr);
1572                  pmv[6].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);                  pmv[6].x = EVEN(pmv[6].x); pmv[6].y = EVEN(pmv[6].y);
1573          } else pmv[6].x = pmv[6].y = 0;          } else pmv[6].x = pmv[6].y = 0;
   
 // more?  
1574  }  }
1575    
1576    
1577  /* search backward or forward, for b-frames */  /* search backward or forward */
1578  static void  static void
1579  SearchBF(       const uint8_t * const pRef,  SearchBF(       const IMAGE * const pRef,
1580                          const uint8_t * const pRefH,                          const uint8_t * const pRefH,
1581                          const uint8_t * const pRefV,                          const uint8_t * const pRefV,
1582                          const uint8_t * const pRefHV,                          const uint8_t * const pRefHV,
# Line 1711  Line 1592 
1592                          SearchData * const Data)                          SearchData * const Data)
1593  {  {
1594    
1595          const int32_t iEdgedWidth = pParam->edged_width;          int i, iDirection = 255, mask;
   
         int i, iDirection, mask;  
1596          VECTOR pmv[7];          VECTOR pmv[7];
1597          MainSearchFunc *MainSearchPtr;          MainSearchFunc *MainSearchPtr;
1598          *Data->iMinSAD = MV_MAX_ERROR;          *Data->iMinSAD = MV_MAX_ERROR;
1599          Data->iFcode = iFcode;          Data->iFcode = iFcode;
1600            Data->qpel_precision = 0;
1601            Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; // reset chroma-sad cache
1602    
1603          Data->Ref = pRef + (x + y * iEdgedWidth) * 16;          Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16;
1604          Data->RefH = pRefH + (x + y * iEdgedWidth) * 16;          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16;
1605          Data->RefV = pRefV + (x + y * iEdgedWidth) * 16;          Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16;
1606          Data->RefHV = pRefHV + (x + y * iEdgedWidth) * 16;          Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16;
1607            Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
1608            Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;
1609    
1610          Data->predMV = *predMV;          Data->predMV = *predMV;
1611    
1612          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,
1613                                  pParam->width, pParam->height, iFcode, pParam->m_quarterpel);                                  pParam->width, pParam->height, iFcode - Data->qpel, 0, 0);
1614    
1615          pmv[0] = Data->predMV;          pmv[0] = Data->predMV;
1616          if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; }          if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; }
1617    
1618          PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current);          PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current);
1619    
1620          Data->currentMV->x = Data->currentMV->y = 0;          Data->currentMV->x = Data->currentMV->y = 0;
   
1621          CheckCandidate = CheckCandidate16no4v;          CheckCandidate = CheckCandidate16no4v;
1622    
1623  // main loop. checking all predictions  // main loop. checking all predictions
1624          for (i = 0; i < 8; i++) {          for (i = 0; i < 7; i++) {
1625                  if (!(mask = make_mask(pmv, i)) ) continue;                  if (!(mask = make_mask(pmv, i)) ) continue;
1626                  CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);                  CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1627          }          }
1628    
1629          if (MotionFlags & PMV_USESQUARES16)          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;
1630                  MainSearchPtr = SquareSearch;          else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
         else if (MotionFlags & PMV_ADVANCEDDIAMOND16)  
                 MainSearchPtr = AdvDiamondSearch;  
1631                  else MainSearchPtr = DiamondSearch;                  else MainSearchPtr = DiamondSearch;
1632    
1633          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
1634    
1635          HalfpelRefine(Data);          SubpelRefine(Data);
1636    
1637          if (Data->qpel) {          if (Data->qpel && *Data->iMinSAD < *best_sad + 300) {
1638                  Data->currentQMV->x = 2*Data->currentMV->x;                  Data->currentQMV->x = 2*Data->currentMV->x;
1639                  Data->currentQMV->y = 2*Data->currentMV->y;                  Data->currentQMV->y = 2*Data->currentMV->y;
1640                  CheckCandidate = CheckCandidate16no4v_qpel;                  Data->qpel_precision = 1;
1641                  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,
1642                                          pParam->width, pParam->height, iFcode, pParam->m_quarterpel);                                          pParam->width, pParam->height, iFcode, 1, 0);
1643                  QuarterpelRefine(Data);                  SubpelRefine(Data);
1644          }          }
1645    
1646  // three bits are needed to code backward mode. four for forward  // three bits are needed to code backward mode. four for forward
1647  // we treat the bits just like they were vector's  
1648          if (mode_current == MODE_FORWARD) *Data->iMinSAD +=  4 * Data->lambda16;          if (mode_current == MODE_FORWARD) *Data->iMinSAD +=  4 * Data->lambda16;
1649          else *Data->iMinSAD +=  3 * Data->lambda16;          else *Data->iMinSAD +=  3 * Data->lambda16;
1650    
# Line 1781  Line 1662 
1662                          pMB->pmvs[0].x = Data->currentMV->x - predMV->x;                          pMB->pmvs[0].x = Data->currentMV->x - predMV->x;
1663                          pMB->pmvs[0].y = Data->currentMV->y - predMV->y;                          pMB->pmvs[0].y = Data->currentMV->y - predMV->y;
1664                  }                  }
1665                  if (mode_current == MODE_FORWARD)                  if (mode_current == MODE_FORWARD) pMB->mvs[0] = *Data->currentMV;
1666                          pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV;                  else pMB->b_mvs[0] = *Data->currentMV;
1667                  else          }
                         pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search  
1668    
1669            if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV;
1670            else *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search
1671          }          }
1672    
1673    static void
1674    SkipDecisionB(const IMAGE * const pCur,
1675                                    const IMAGE * const f_Ref,
1676                                    const IMAGE * const b_Ref,
1677                                    MACROBLOCK * const pMB,
1678                                    const uint32_t x, const uint32_t y,
1679                                    const SearchData * const Data)
1680    {
1681            int dx = 0, dy = 0, b_dx = 0, b_dy = 0;
1682            int32_t sum;
1683            const int div = 1 + Data->qpel;
1684            int k;
1685            const uint32_t stride = Data->iEdgedWidth/2;
1686    //this is not full chroma compensation, only it's fullpel approximation. should work though
1687    
1688            for (k = 0; k < 4; k++) {
1689                    dy += Data->directmvF[k].y / div;
1690                    dx += Data->directmvF[k].x / div;
1691                    b_dy += Data->directmvB[k].y / div;
1692                    b_dx += Data->directmvB[k].x / div;
1693            }
1694    
1695            dy = (dy >> 3) + roundtab_76[dy & 0xf];
1696            dx = (dx >> 3) + roundtab_76[dx & 0xf];
1697            b_dy = (b_dy >> 3) + roundtab_76[b_dy & 0xf];
1698            b_dx = (b_dx >> 3) + roundtab_76[b_dx & 0xf];
1699    
1700            sum = sad8bi(pCur->u + 8 * x + 8 * y * stride,
1701                                            f_Ref->u + (y*8 + dy/2) * stride + x*8 + dx/2,
1702                                            b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,
1703                                            stride);
1704    
1705            if (sum >= 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) return; //no skip
1706    
1707            sum += sad8bi(pCur->v + 8*x + 8 * y * stride,
1708                                            f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2,
1709                                            b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,
1710                                            stride);
1711    
1712            if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) {
1713                    pMB->mode = MODE_DIRECT_NONE_MV; //skipped
1714                    for (k = 0; k < 4; k++) {
1715                            pMB->qmvs[k] = pMB->mvs[k];
1716                            pMB->b_qmvs[k] = pMB->b_mvs[k];
1717                    }
1718            }
1719  }  }
1720    
1721  static int32_t  static __inline uint32_t
1722  SearchDirect(const IMAGE * const f_Ref,  SearchDirect(const IMAGE * const f_Ref,
1723                                  const uint8_t * const f_RefH,                                  const uint8_t * const f_RefH,
1724                                  const uint8_t * const f_RefV,                                  const uint8_t * const f_RefV,
# Line 1811  Line 1739 
1739    
1740  {  {
1741          int32_t skip_sad;          int32_t skip_sad;
1742          int k;          int k = (x + Data->iEdgedWidth*y) * 16;
   
1743          MainSearchFunc *MainSearchPtr;          MainSearchFunc *MainSearchPtr;
1744    
1745          *Data->iMinSAD = 256*4096;          *Data->iMinSAD = 256*4096;
1746            Data->RefP[0] = f_Ref->y + k;
1747            Data->RefP[2] = f_RefH + k;
1748            Data->RefP[1] = f_RefV + k;
1749            Data->RefP[3] = f_RefHV + k;
1750            Data->b_RefP[0] = b_Ref->y + k;
1751            Data->b_RefP[2] = b_RefH + k;
1752            Data->b_RefP[1] = b_RefV + k;
1753            Data->b_RefP[3] = b_RefHV + k;
1754            Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;
1755            Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;
1756            Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;
1757            Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;
1758    
1759            k = Data->qpel ? 4 : 2;
1760            Data->max_dx = k * (pParam->width - x * 16);
1761            Data->max_dy = k * (pParam->height - y * 16);
1762            Data->min_dx = -k * (16 + x * 16);
1763            Data->min_dy = -k * (16 + y * 16);
1764    
1765          Data->Ref = f_Ref->y + (x + Data->iEdgedWidth*y) * 16;          Data->referencemv = Data->qpel ? b_mb->qmvs : b_mb->mvs;
1766          Data->RefH = f_RefH + (x + Data->iEdgedWidth*y) * 16;          Data->qpel_precision = 0;
         Data->RefV = f_RefV + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefHV = f_RefHV + (x + Data->iEdgedWidth*y) * 16;  
         Data->bRef = b_Ref->y + (x + Data->iEdgedWidth*y) * 16;  
         Data->bRefH = b_RefH + (x + Data->iEdgedWidth*y) * 16;  
         Data->bRefV = b_RefV + (x + Data->iEdgedWidth*y) * 16;  
         Data->bRefHV = b_RefHV + (x + Data->iEdgedWidth*y) * 16;  
   
         Data->max_dx = 2 * pParam->width - 2 * (x) * 16;  
         Data->max_dy = 2 * pParam->height - 2 * (y) * 16;  
         Data->min_dx = -(2 * 16 + 2 * (x) * 16);  
         Data->min_dy = -(2 * 16 + 2 * (y) * 16);  
         if (Data->qpel) { //we measure in qpixels  
                 Data->max_dx *= 2;  
                 Data->max_dy *= 2;  
                 Data->min_dx *= 2;  
                 Data->min_dy *= 2;  
                 Data->referencemv = b_mb->qmvs;  
         } else Data->referencemv = b_mb->mvs;  
1767    
1768          for (k = 0; k < 4; k++) {          for (k = 0; k < 4; k++) {
1769                  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 1844  Line 1771 
1771                  pMB->mvs[k].y = Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD);                  pMB->mvs[k].y = Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD);
1772                  pMB->b_mvs[k].y = Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD;                  pMB->b_mvs[k].y = Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD;
1773    
1774                  if ( ( pMB->b_mvs[k].x > Data->max_dx ) || ( pMB->b_mvs[k].x < Data->min_dx )                  if ( (pMB->b_mvs[k].x > Data->max_dx) | (pMB->b_mvs[k].x < Data->min_dx)
1775                          || ( pMB->b_mvs[k].y > Data->max_dy ) || ( pMB->b_mvs[k].y < Data->min_dy )) {                          | (pMB->b_mvs[k].y > Data->max_dy) | (pMB->b_mvs[k].y < Data->min_dy) ) {
1776    
1777                          *best_sad = 256*4096; // in that case, we won't use direct mode                          *best_sad = 256*4096; // in that case, we won't use direct mode
1778                          pMB->mode = MODE_DIRECT; // just to make sure it doesn't say "MODE_DIRECT_NONE_MV"                          pMB->mode = MODE_DIRECT; // just to make sure it doesn't say "MODE_DIRECT_NONE_MV"
1779                          pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0;                          pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0;
1780                          return 0;                          return 256*4096;
1781                  }                  }
1782                  if (b_mb->mode != MODE_INTER4V) {                  if (b_mb->mode != MODE_INTER4V) {
1783                          pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mvs[0];                          pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mvs[0];
# Line 1858  Line 1785 
1785                          Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0];                          Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0];
1786                          Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0];                          Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0];
1787                          break;                          break;
1788                  }                  }
1789          }          }
   
         if (Data->qpel) {  
                         if (b_mb->mode == MODE_INTER4V)  
                 CheckCandidate = CheckCandidateDirect_qpel;  
                         else CheckCandidate = CheckCandidateDirectno4v_qpel;  
         } else {  
                         if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect;  
                         else CheckCandidate = CheckCandidateDirectno4v;  
         }  
   
         (*CheckCandidate)(0, 0, 255, &k, Data);  
   
 // skip decision  
         if (*Data->iMinSAD < pMB->quant * SKIP_THRESH_B) {  
                 //possible skip - checking chroma. everything copied from MC  
                 //this is not full chroma compensation, only it's fullpel approximation. should work though  
                 int sum, dx, dy, b_dx, b_dy;  
1790    
1791                  if (Data->qpel) {          CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v;
                         sum = pMB->mvs[0].y/2 + pMB->mvs[1].y/2 + pMB->mvs[2].y/2 + pMB->mvs[3].y/2;  
                         dy = (sum >> 3) + roundtab_76[sum & 0xf];  
                         sum = pMB->mvs[0].x/2 + pMB->mvs[1].x/2 + pMB->mvs[2].x/2 + pMB->mvs[3].x/2;  
                         dx = (sum >> 3) + roundtab_76[sum & 0xf];  
   
                         sum = pMB->b_mvs[0].y/2 + pMB->b_mvs[1].y/2 + pMB->b_mvs[2].y/2 + pMB->b_mvs[3].y/2;  
                         b_dy = (sum >> 3) + roundtab_76[sum & 0xf];  
                         sum = pMB->b_mvs[0].x/2 + pMB->b_mvs[1].x/2 + pMB->b_mvs[2].x/2 + pMB->b_mvs[3].x/2;  
                         b_dx = (sum >> 3) + roundtab_76[sum & 0xf];  
1792    
1793                  } else {          CheckCandidate(0, 0, 255, &k, Data);
                         sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
                         dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));  
                         sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
                         dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));  
   
                         sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;  
                         b_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));  
                         sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;  
                         b_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));  
                 }  
                 sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2),  
                                         f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,  
                                         b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,  
                                         Data->iEdgedWidth/2);  
                 sum += sad8bi(pCur->v + 8*x + 8*y*(Data->iEdgedWidth/2),  
                                         f_Ref->v + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,  
                                         b_Ref->v + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,  
                                         Data->iEdgedWidth/2);  
1794    
1795                  if (sum < MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) {  // initial (fast) skip decision
1796            if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (2 + Data->chroma?1:0)) {
1797                    //possible skip
1798                    if (Data->chroma) {
1799                          pMB->mode = MODE_DIRECT_NONE_MV;                          pMB->mode = MODE_DIRECT_NONE_MV;
1800                          return *Data->iMinSAD;                          return *Data->iMinSAD; // skip.
1801                    } else {
1802                            SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data);
1803                            if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; // skip.
1804                  }                  }
1805          }          }
1806    
1807            *Data->iMinSAD += Data->lambda16;
1808          skip_sad = *Data->iMinSAD;          skip_sad = *Data->iMinSAD;
1809    
1810  //  DIRECT MODE DELTA VECTOR SEARCH.  //  DIRECT MODE DELTA VECTOR SEARCH.
1811  //      This has to be made more effective, but at the moment I'm happy it's running at all  //      This has to be made more effective, but at the moment I'm happy it's running at all
1812    
1813          if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;
1814                  else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1815                          else MainSearchPtr = DiamondSearch;                          else MainSearchPtr = DiamondSearch;
1816    
1817          (*MainSearchPtr)(0, 0, Data, 255);          MainSearchPtr(0, 0, Data, 255);
1818    
1819          HalfpelRefine(Data); //or qpel refine, if we're in qpel mode          SubpelRefine(Data);
1820    
         *Data->iMinSAD +=  1 * Data->lambda16; // one bit is needed to code direct mode  
1821          *best_sad = *Data->iMinSAD;          *best_sad = *Data->iMinSAD;
1822    
1823          if (b_mb->mode == MODE_INTER4V)          if (Data->qpel || b_mb->mode == MODE_INTER4V) pMB->mode = MODE_DIRECT;
                 pMB->mode = MODE_DIRECT;  
1824          else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation          else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation
1825    
1826          pMB->pmvs[3] = *Data->currentMV;          pMB->pmvs[3] = *Data->currentMV;
# Line 1964  Line 1852 
1852          return skip_sad;          return skip_sad;
1853  }  }
1854    
1855    static void
1856  static __inline void  SearchInterpolate(const IMAGE * const f_Ref,
 SearchInterpolate(const uint8_t * const f_Ref,  
1857                                  const uint8_t * const f_RefH,                                  const uint8_t * const f_RefH,
1858                                  const uint8_t * const f_RefV,                                  const uint8_t * const f_RefV,
1859                                  const uint8_t * const f_RefHV,                                  const uint8_t * const f_RefHV,
1860                                  const uint8_t * const b_Ref,                                  const IMAGE * const b_Ref,
1861                                  const uint8_t * const b_RefH,                                  const uint8_t * const b_RefH,
1862                                  const uint8_t * const b_RefV,                                  const uint8_t * const b_RefV,
1863                                  const uint8_t * const b_RefHV,                                  const uint8_t * const b_RefHV,
# Line 1988  Line 1875 
1875    
1876  {  {
1877    
         const int32_t iEdgedWidth = pParam->edged_width;  
1878          int iDirection, i, j;          int iDirection, i, j;
1879          SearchData bData;          SearchData bData;
1880    
1881          *(bData.iMinSAD = fData->iMinSAD) = 4096*256;          fData->qpel_precision = 0;
1882          bData.Cur = fData->Cur;          memcpy(&bData, fData, sizeof(SearchData)); //quick copy of common data
1883          fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth;          *fData->iMinSAD = 4096*256;
1884          bData.currentMV = fData->currentMV + 1; bData.currentQMV = fData->currentQMV + 1;          bData.currentMV++; bData.currentQMV++;
         bData.lambda16 = fData->lambda16;  
1885          fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;          fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;
1886    
1887          bData.bRef = fData->Ref = f_Ref + (x + y * iEdgedWidth) * 16;          i = (x + y * fData->iEdgedWidth) * 16;
1888          bData.bRefH = fData->RefH = f_RefH + (x + y * iEdgedWidth) * 16;  
1889          bData.bRefV = fData->RefV = f_RefV + (x + y * iEdgedWidth) * 16;          bData.b_RefP[0] = fData->RefP[0] = f_Ref->y + i;
1890          bData.bRefHV = fData->RefHV = f_RefHV + (x + y * iEdgedWidth) * 16;          bData.b_RefP[2] = fData->RefP[2] = f_RefH + i;
1891          bData.Ref = fData->bRef = b_Ref + (x + y * iEdgedWidth) * 16;          bData.b_RefP[1] = fData->RefP[1] = f_RefV + i;
1892          bData.RefH = fData->bRefH = b_RefH + (x + y * iEdgedWidth) * 16;          bData.b_RefP[3] = fData->RefP[3] = f_RefHV + i;
1893          bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16;          bData.RefP[0] = fData->b_RefP[0] = b_Ref->y + i;
1894          bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16;          bData.RefP[2] = fData->b_RefP[2] = b_RefH + i;
1895          bData.RefQ = fData->RefQ;          bData.RefP[1] = fData->b_RefP[1] = b_RefV + i;
1896            bData.RefP[3] = fData->b_RefP[3] = b_RefHV + i;
1897            bData.b_RefP[4] = fData->RefP[4] = f_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8;
1898            bData.b_RefP[5] = fData->RefP[5] = f_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8;
1899            bData.RefP[4] = fData->b_RefP[4] = b_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8;
1900            bData.RefP[5] = fData->b_RefP[5] = b_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8;
1901    
1902          bData.bpredMV = fData->predMV = *f_predMV;          bData.bpredMV = fData->predMV = *f_predMV;
1903          fData->bpredMV = bData.predMV = *b_predMV;          fData->bpredMV = bData.predMV = *b_predMV;
1904            fData->currentMV[0] = fData->currentMV[2];
1905    
1906          fData->currentMV[0] = fData->currentMV[3];          get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode - fData->qpel, 0, 0);
1907          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(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode - fData->qpel, 0, 0);
         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);  
1908    
1909          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;
1910          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;
1911          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;
1912          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;
1913    
1914          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;
1915          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;
1916          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;
1917          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;
1918    
1919          CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);          CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);
1920    
1921  //diamond. I wish we could use normal mainsearch functions (square, advdiamond)  //diamond
   
1922          do {          do {
1923                  iDirection = 255;                  iDirection = 255;
1924                  // forward MV moves                  // forward MV moves
# Line 2050  Line 1939 
1939    
1940          } while (!(iDirection));          } while (!(iDirection));
1941    
1942          *fData->iMinSAD +=  2 * fData->lambda16; // two bits are needed to code interpolate mode.  //qpel refinement
   
1943          if (fData->qpel) {          if (fData->qpel) {
1944                  CheckCandidate = CheckCandidateInt_qpel;                  if (*fData->iMinSAD > *best_sad + 500) return;
1945                  get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 0);                  CheckCandidate = CheckCandidateInt;
1946                  get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, 0);                  fData->qpel_precision = bData.qpel_precision = 1;
1947                    get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 1, 0);
1948                    get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, 1, 0);
1949                  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;
1950                  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;
1951                  fData->currentQMV[1].x = 2 * fData->currentMV[1].x;                  fData->currentQMV[1].x = 2 * fData->currentMV[1].x;
1952                  fData->currentQMV[1].y = 2 * fData->currentMV[1].y;                  fData->currentQMV[1].y = 2 * fData->currentMV[1].y;
1953                  QuarterpelRefine(fData);                  SubpelRefine(fData);
1954                    if (*fData->iMinSAD > *best_sad + 300) return;
1955                  fData->currentQMV[2] = fData->currentQMV[0];                  fData->currentQMV[2] = fData->currentQMV[0];
1956                  QuarterpelRefine(&bData);                  SubpelRefine(&bData);
1957          }          }
1958    
1959            *fData->iMinSAD += (2+3) * fData->lambda16; // two bits are needed to code interpolate mode.
1960    
1961          if (*fData->iMinSAD < *best_sad) {          if (*fData->iMinSAD < *best_sad) {
1962                  *best_sad = *fData->iMinSAD;                  *best_sad = *fData->iMinSAD;
1963                  pMB->mvs[0] = fData->currentMV[0];                  pMB->mvs[0] = fData->currentMV[0];
# Line 2105  Line 1998 
1998                                           const IMAGE * const b_refHV)                                           const IMAGE * const b_refHV)
1999  {  {
2000          uint32_t i, j;          uint32_t i, j;
2001          int32_t best_sad, skip_sad;          int32_t best_sad;
2002            uint32_t skip_sad;
2003          int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0;          int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0;
         static const VECTOR zeroMV={0,0};  
2004          const MACROBLOCK * const b_mbs = b_reference->mbs;          const MACROBLOCK * const b_mbs = b_reference->mbs;
2005    
2006          VECTOR f_predMV, b_predMV;      /* there is no prediction for direct mode*/          VECTOR f_predMV, b_predMV;      /* there is no prediction for direct mode*/
2007    
2008          const int32_t TRB = time_pp - time_bp;          const int32_t TRB = time_pp - time_bp;
2009          const int32_t TRD = time_pp;          const int32_t TRD = time_pp;
         uint8_t * qimage;  
2010    
2011  // some pre-inintialized data for the rest of the search  // some pre-inintialized data for the rest of the search
2012    
# Line 2122  Line 2014 
2014          int32_t iMinSAD;          int32_t iMinSAD;
2015          VECTOR currentMV[3];          VECTOR currentMV[3];
2016          VECTOR currentQMV[3];          VECTOR currentQMV[3];
2017            int32_t temp[8];
2018            memset(&Data, 0, sizeof(SearchData));
2019          Data.iEdgedWidth = pParam->edged_width;          Data.iEdgedWidth = pParam->edged_width;
2020          Data.currentMV = currentMV; Data.currentQMV = currentQMV;          Data.currentMV = currentMV; Data.currentQMV = currentQMV;
2021          Data.iMinSAD = &iMinSAD;          Data.iMinSAD = &iMinSAD;
2022          Data.lambda16 = lambda_vec16[frame->quant];          Data.lambda16 = lambda_vec16[frame->quant];
2023          Data.qpel = pParam->m_quarterpel;          Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL;
2024            Data.rounding = 0;
2025          if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)          Data.chroma = frame->motion_flags & XVID_ME_CHROMA8;
2026                  return; // allocate some mem for qpel interpolated blocks          Data.temp = temp;
                                   // somehow this is dirty since I think we shouldn't use malloc outside  
                                   // encoder_create() - so please fix me!  
         Data.RefQ = qimage;  
2027    
2028            Data.RefQ = f_refV->u; // a good place, also used in MC (for similar purpose)
2029          // note: i==horizontal, j==vertical          // note: i==horizontal, j==vertical
2030          for (j = 0; j < pParam->mb_height; j++) {          for (j = 0; j < pParam->mb_height; j++) {
2031    
# Line 2151  Line 2043 
2043                                  }                                  }
2044    
2045                          Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;                          Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;
2046                            Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8;
2047                            Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8;
2048                          pMB->quant = frame->quant;                          pMB->quant = frame->quant;
2049    
2050  /* direct search comes first, because it (1) checks for SKIP-mode  /* direct search comes first, because it (1) checks for SKIP-mode
# Line 2169  Line 2063 
2063                          if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }                          if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }
2064    
2065                          // forward search                          // forward search
2066                          SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,                          SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y,
2067                                                  &frame->image, i, j,                                                  &frame->image, i, j,
2068                                                  frame->motion_flags,                                                  frame->motion_flags,
2069                                                  frame->fcode, pParam,                                                  frame->fcode, pParam,
# Line 2177  Line 2071 
2071                                                  MODE_FORWARD, &Data);                                                  MODE_FORWARD, &Data);
2072    
2073                          // backward search                          // backward search
2074                          SearchBF(b_ref->y, b_refH->y, b_refV->y, b_refHV->y,                          SearchBF(b_ref, b_refH->y, b_refV->y, b_refHV->y,
2075                                                  &frame->image, i, j,                                                  &frame->image, i, j,
2076                                                  frame->motion_flags,                                                  frame->motion_flags,
2077                                                  frame->bcode, pParam,                                                  frame->bcode, pParam,
# Line 2185  Line 2079 
2079                                                  MODE_BACKWARD, &Data);                                                  MODE_BACKWARD, &Data);
2080    
2081                          // interpolate search comes last, because it uses data from forward and backward as prediction                          // interpolate search comes last, because it uses data from forward and backward as prediction
2082                            SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y,
2083                          SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,                                                  b_ref, b_refH->y, b_refV->y, b_refHV->y,
                                                 b_ref->y, b_refH->y, b_refV->y, b_refHV->y,  
2084                                                  &frame->image,                                                  &frame->image,
2085                                                  i, j,                                                  i, j,
2086                                                  frame->fcode, frame->bcode,                                                  frame->fcode, frame->bcode,
# Line 2197  Line 2090 
2090                                                  pMB, &best_sad,                                                  pMB, &best_sad,
2091                                                  &Data);                                                  &Data);
2092    
2093    // final skip decision
2094                            if ( (skip_sad < frame->quant * MAX_SAD00_FOR_SKIP * 2)
2095                                            && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) )
2096                                    SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data);
2097    
2098                          switch (pMB->mode) {                          switch (pMB->mode) {
2099                                  case MODE_FORWARD:                                  case MODE_FORWARD:
2100                                          f_count++;                                          f_count++;
2101                                          if (pParam->m_quarterpel) f_predMV = pMB->qmvs[0];                                          f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0];
                                         else f_predMV = pMB->mvs[0];  
2102                                          break;                                          break;
2103                                  case MODE_BACKWARD:                                  case MODE_BACKWARD:
2104                                          b_count++;                                          b_count++;
2105                                          if (pParam->m_quarterpel) b_predMV = pMB->b_qmvs[0];                                          b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0];
                                         else b_predMV = pMB->b_mvs[0];  
2106                                          break;                                          break;
2107                                  case MODE_INTERPOLATE:                                  case MODE_INTERPOLATE:
2108                                          i_count++;                                          i_count++;
2109                                          if (pParam->m_quarterpel) {                                          f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0];
2110                                                  f_predMV = pMB->qmvs[0];                                          b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0];
                                                 b_predMV = pMB->b_qmvs[0];  
                                         } else {  
                                                 f_predMV = pMB->mvs[0];  
                                                 b_predMV = pMB->b_mvs[0];  
                                         }  
2111                                          break;                                          break;
2112                                  case MODE_DIRECT:                                  case MODE_DIRECT:
2113                                  case MODE_DIRECT_NO4V:                                  case MODE_DIRECT_NO4V:
2114                                          d_count++;                                          d_count++;
                                         break;  
2115                                  default:                                  default:
2116                                          break;                                          break;
2117                          }                          }
2118                  }                  }
2119          }          }
         free(qimage);  
2120  }  }
2121    
2122  /* Hinted ME starts here */  static __inline void
2123    MEanalyzeMB (   const uint8_t * const pRef,
2124  static void                                  const uint8_t * const pCur,
 SearchPhinted ( const IMAGE * const pRef,  
                                 const uint8_t * const pRefH,  
                                 const uint8_t * const pRefV,  
                                 const uint8_t * const pRefHV,  
                                 const IMAGE * const pCur,  
2125                                  const int x,                                  const int x,
2126                                  const int y,                                  const int y,
                                 const uint32_t MotionFlags,  
                                 const uint32_t iQuant,  
2127                                  const MBParam * const pParam,                                  const MBParam * const pParam,
2128                                  const MACROBLOCK * const pMBs,                                  MACROBLOCK * const pMBs,
                                 int inter4v,  
                                 MACROBLOCK * const pMB,  
2129                                  SearchData * const Data)                                  SearchData * const Data)
2130  {  {
2131    
2132          int i, t;          int i, mask;
2133          MainSearchFunc * MainSearchPtr;          int quarterpel = (pParam->vol_flags & XVID_VOL_QUARTERPEL)? 1: 0;
2134            VECTOR pmv[3];
2135          Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);          MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width];
         Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);  
         get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,  
                                 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);  
   
         Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;  
         Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;  
         Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;  
   
         Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;  
         Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;  
   
         if (!(MotionFlags & PMV_HALFPEL16)) {  
                 Data->min_dx = EVEN(Data->min_dx);  
                 Data->max_dx = EVEN(Data->max_dx);  
                 Data->min_dy = EVEN(Data->min_dy);  
                 Data->max_dy = EVEN(Data->max_dy);  
         }  
2136    
2137          for(i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;          for(i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;
2138    
2139          if (pMB->dquant != NO_CHANGE) inter4v = 0;          //median is only used as prediction. it doesn't have to be real
2140            if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0;
2141          if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16;          else
2142          else CheckCandidate = CheckCandidate16no4v;                  if (x == 1) //left macroblock does not have any vector now
2143                            Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median
2144          pMB->mvs[0].x = EVEN(pMB->mvs[0].x);                  else if (y == 1) // top macroblock doesn't have it's vector
2145          pMB->mvs[0].y = EVEN(pMB->mvs[0].y);                          Data->predMV = (pMB - 1)->mvs[0]; // left instead of median
2146          if (pMB->mvs[0].x > Data->max_dx) pMB->mvs[0].x = Data->max_dx; // this is in case iFcode changed                          else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median
         if (pMB->mvs[0].x < Data->min_dx) pMB->mvs[0].x = Data->min_dx;  
         if (pMB->mvs[0].y > Data->max_dy) pMB->mvs[0].y = Data->max_dy;  
         if (pMB->mvs[0].y < Data->min_dy) pMB->mvs[0].y = Data->min_dy;  
   
         (*CheckCandidate)(pMB->mvs[0].x, pMB->mvs[0].y, 0, &t, Data);  
   
         if (pMB->mode == MODE_INTER4V)  
                 for (i = 1; i < 4; i++) { // all four vectors will be used as four predictions for 16x16 search  
                         pMB->mvs[i].x = EVEN(pMB->mvs[i].x);  
                         pMB->mvs[i].y = EVEN(pMB->mvs[i].y);  
                         if (!(make_mask(pMB->mvs, i)))  
                                 (*CheckCandidate)(pMB->mvs[i].x, pMB->mvs[i].y, 0, &t, Data);  
                 }  
   
         if (MotionFlags & PMV_USESQUARES16)  
                 MainSearchPtr = SquareSearch;  
         else if (MotionFlags & PMV_ADVANCEDDIAMOND16)  
                 MainSearchPtr = AdvDiamondSearch;  
                 else MainSearchPtr = DiamondSearch;  
   
         (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);  
   
         if (MotionFlags & PMV_HALFPELREFINE16) HalfpelRefine(Data);  
   
         for(i = 0; i < 5; i++) {  
                 Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors  
                 Data->currentQMV[i].y = 2 * Data->currentMV[i].y;  
         }  
2147    
         if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {  
2148                  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,
2149                                  pParam->width, pParam->height, Data->iFcode, 0);          pParam->width, pParam->height, Data->iFcode - quarterpel, 0, 0);
                 CheckCandidate = CheckCandidate16_qpel;  
                 QuarterpelRefine(Data);  
         }  
2150    
2151          if (inter4v) {          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;
2152                  SearchData Data8;          Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16;
                 Data8.iFcode = Data->iFcode;  
                 Data8.lambda8 = Data->lambda8;  
                 Data8.iEdgedWidth = Data->iEdgedWidth;  
                 Data8.RefQ = Data->RefQ;  
                 Data8.qpel = Data->qpel;  
                 Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);  
                 Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);  
                 Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);  
                 Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);  
2153    
2154                  if (Data->chroma) {          pmv[1].x = EVEN(pMB->mvs[0].x);
2155                          int sum, dx, dy;          pmv[1].y = EVEN(pMB->mvs[0].y);
2156            pmv[2].x = EVEN(Data->predMV.x);
2157            pmv[2].y = EVEN(Data->predMV.y);
2158            pmv[0].x = pmv[0].y = 0;
2159    
2160                          if(pParam->m_quarterpel)          CheckCandidate32I(0, 0, 255, &i, Data);
                                 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);  
                         Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);  
                 }  
         }  
2161    
2162          if (!(inter4v) ||          if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) {
                 (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] +  
                                                         Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {  
 // INTER MODE  
                 pMB->mode = MODE_INTER;  
                 pMB->mvs[0] = pMB->mvs[1]  
                         = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];  
2163    
2164                  pMB->qmvs[0] = pMB->qmvs[1]                  if (!(mask = make_mask(pmv, 1)))
2165                          = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];                          CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data);
2166                    if (!(mask = make_mask(pmv, 2)))
2167                            CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data);
2168    
2169                  pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =                  if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) // diamond only if needed
2170                          pMB->sad8[2] = pMB->sad8[3] =  Data->iMinSAD[0];                          DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);
2171            }
2172    
2173                  if(pParam->m_quarterpel) {          for (i = 0; i < 4; i++) {
2174                          pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x;                  MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width];
2175                          pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y;                  MB->mvs[0] = MB->mvs[1] = MB->mvs[2] = MB->mvs[3] = Data->currentMV[i];
2176                  } else {                  MB->mode = MODE_INTER;
2177                          pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;                  MB->sad16 = Data->iMinSAD[i+1];
                         pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;  
2178                  }                  }
         } else {  
 // INTER4V MODE; all other things are already set in Search8  
                 pMB->mode = MODE_INTER4V;  
                 pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3]  
                                                 + Data->iMinSAD[4] + IMV16X16 * iQuant;  
2179          }          }
2180    
2181  }  #define INTRA_THRESH    2050
2182    #define INTER_THRESH    1200
2183    
2184  void  int
2185  MotionEstimationHinted( MBParam * const pParam,  MEanalysis(     const IMAGE * const pRef,
2186                                                  FRAMEINFO * const current,                          const FRAMEINFO * const Current,
2187                                                  FRAMEINFO * const reference,                          const MBParam * const pParam,
2188                                                  const IMAGE * const pRefH,                          const int maxIntra, //maximum number if non-I frames
2189                                                  const IMAGE * const pRefV,                          const int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame
2190                                                  const IMAGE * const pRefHV)                          const int bCount,  // number of B frames in a row
2191                            const int b_thresh)
2192  {  {
2193          MACROBLOCK *const pMBs = current->mbs;          uint32_t x, y, intra = 0;
2194          const IMAGE *const pCurrent = &current->image;          int sSAD = 0;
2195          const IMAGE *const pRef = &reference->image;          MACROBLOCK * const pMBs = Current->mbs;
2196            const IMAGE * const pCurrent = &Current->image;
2197            int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + 10*b_thresh;
2198            int s = 0, blocks = 0;
2199    
2200          uint32_t x, y;          int32_t iMinSAD[5], temp[5];
2201          uint8_t * qimage;          VECTOR currentMV[5];
         int32_t temp[5], quant = current->quant;  
         int32_t iMinSAD[5];  
         VECTOR currentMV[5], currentQMV[5];  
2202          SearchData Data;          SearchData Data;
2203          Data.iEdgedWidth = pParam->edged_width;          Data.iEdgedWidth = pParam->edged_width;
2204          Data.currentMV = currentMV;          Data.currentMV = currentMV;
         Data.currentQMV = currentQMV;  
2205          Data.iMinSAD = iMinSAD;          Data.iMinSAD = iMinSAD;
2206            Data.iFcode = Current->fcode;
2207          Data.temp = temp;          Data.temp = temp;
2208          Data.iFcode = current->fcode;          CheckCandidate = CheckCandidate32I;
         Data.rounding = pParam->m_rounding_type;  
         Data.qpel = pParam->m_quarterpel;  
         Data.chroma = current->global_flags & XVID_ME_COLOUR;  
2209    
2210          if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)          if (intraCount != 0 && intraCount < 10) // we're right after an I frame
2211                  return; // allocate some mem for qpel interpolated blocks                  IntraThresh += 8 * (intraCount - 10) * (intraCount - 10);
2212                                    // somehow this is dirty since I think we shouldn't use malloc outside          else
2213                                    // encoder_create() - so please fix me!                  if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec
2214                            IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra;
2215    
2216          Data.RefQ = qimage;          InterThresh -= (350 - 8*b_thresh) * bCount;
2217            if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh;
2218    
2219          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();
2220    
2221          for (y = 0; y < pParam->mb_height; y++) {          for (y = 1; y < pParam->mb_height-1; y += 2) {
2222                  for (x = 0; x < pParam->mb_width; x++)  {                  for (x = 1; x < pParam->mb_width-1; x += 2) {
2223                            int i;
2224                          MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];                          blocks += 4;
2225    
2226  //intra mode is copied from the first pass. At least for the time being                          if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV;
2227                          if  ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue;                          else { //extrapolation of the vector found for last frame
2228                                    pMBs[x + y * pParam->mb_width].mvs[0].x =
2229                          if (!(current->global_flags & XVID_LUMIMASKING)) {                                          (pMBs[x + y * pParam->mb_width].mvs[0].x * (bCount+1) ) / bCount;
2230                                  pMB->dquant = NO_CHANGE;                                  pMBs[x + y * pParam->mb_width].mvs[0].y =
2231                                  pMB->quant = current->quant; }                                          (pMBs[x + y * pParam->mb_width].mvs[0].y * (bCount+1) ) / bCount;
2232                          else {                          }
2233                                  if (pMB->dquant != NO_CHANGE) {  
2234                                          quant += DQtab[pMB->dquant];                          MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data);
2235                                          if (quant > 31) quant = 31;  
2236                                          else if (quant < 1) quant = 1;                          for (i = 0; i < 4; i++) {
2237                                    int dev;
2238                                    MACROBLOCK *pMB = &pMBs[x+(i&1) + (y+(i>>1)) * pParam->mb_width];
2239                                    if (pMB->sad16 > IntraThresh) {
2240                                            dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1)) * pParam->edged_width) * 16,
2241                                                                            pParam->edged_width);
2242                                            if (dev + IntraThresh < pMB->sad16) {
2243                                                    pMB->mode = MODE_INTRA;
2244                                                    if (++intra > ((pParam->mb_height-2)*(pParam->mb_width-2))/2) return I_VOP;
2245                                  }                                  }
                                 pMB->quant = quant;  
2246                          }                          }
2247                                    if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) s++;
2248    
2249                          SearchPhinted(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,                                  sSAD += pMB->sad16;
                                                         y, current->motion_flags, pMB->quant,  
                                                         pParam, pMBs, current->global_flags & XVID_INTER4V, pMB,  
                                                         &Data);  
   
2250                  }                  }
2251          }          }
         free(qimage);  
2252  }  }
2253    
2254  static __inline int          sSAD /= blocks;
2255  MEanalyzeMB (   const uint8_t * const pRef,          s = (10*s) / blocks;
2256                                  const uint8_t * const pCur,  
2257                                  const int x,          if (s > 4) sSAD += (s - 2) * (60 - 2*b_thresh); //static block - looks bad when in bframe...
2258                                  const int y,  
2259            if (sSAD > InterThresh ) return P_VOP;
2260            emms();
2261            return B_VOP;
2262    }
2263    
2264    
2265    static WARPPOINTS
2266    GlobalMotionEst(const MACROBLOCK * const pMBs,
2267                                  const MBParam * const pParam,                                  const MBParam * const pParam,
2268                                  const MACROBLOCK * const pMBs,                                  const FRAMEINFO * const current,
2269                                  MACROBLOCK * const pMB,                                  const FRAMEINFO * const reference,
2270                                  SearchData * const Data)                                  const IMAGE * const pRefH,
2271                                    const IMAGE * const pRefV,
2272                                    const IMAGE * const pRefHV      )
2273  {  {
2274    
2275          int i = 255, mask;          const int deltax=8;             // upper bound for difference between a MV and it's neighbour MVs
2276          VECTOR pmv[3];          const int deltay=8;
2277          *(Data->iMinSAD) = MV_MAX_ERROR;          const int grad=512;             // lower bound for deviation in MB
2278    
2279          //median is only used as prediction. it doesn't have to be real          WARPPOINTS gmc;
         if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0;  
         else  
                 if (x == 1) //left macroblock does not have any vector now  
                         Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median  
                 else if (y == 1) // top macroblock don't have it's vector  
                         Data->predMV = (pMB - 1)->mvs[0]; // left instead of median  
                         else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median  
2280    
2281          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,          uint32_t mx, my;
                                 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);  
2282    
2283          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;          int MBh = pParam->mb_height;
2284          Data->Ref = pRef + (x + y * pParam->edged_width) * 16;          int MBw = pParam->mb_width;
2285    
2286          pmv[1].x = EVEN(pMB->mvs[0].x);          int *MBmask= calloc(MBh*MBw,sizeof(int));
2287          pmv[1].y = EVEN(pMB->mvs[0].y);          double DtimesF[4] = { 0.,0., 0., 0. };
2288          pmv[2].x = EVEN(Data->predMV.x);          double sol[4] = { 0., 0., 0., 0. };
2289          pmv[2].y = EVEN(Data->predMV.y);          double a,b,c,n,denom;
2290          pmv[0].x = pmv[0].y = 0;          double meanx,meany;
2291            int num,oldnum;
2292    
2293            if (!MBmask) {  fprintf(stderr,"Mem error\n");
2294                                            gmc.duv[0].x= gmc.duv[0].y =
2295                                                    gmc.duv[1].x= gmc.duv[1].y =
2296                                                    gmc.duv[2].x= gmc.duv[2].y = 0;
2297                                            return gmc; }
2298    
2299    // filter mask of all blocks
2300    
2301            for (my = 1; my < (uint32_t)MBh-1; my++)
2302            for (mx = 1; mx < (uint32_t)MBw-1; mx++)
2303            {
2304                    const int mbnum = mx + my * MBw;
2305                    const MACROBLOCK *pMB = &pMBs[mbnum];
2306                    const VECTOR mv = pMB->mvs[0];
2307    
2308          (*CheckCandidate)(0, 0, 255, &i, Data);                  if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED)
2309                            continue;
2310    
2311  //early skip for 0,0                  if ( ( (abs(mv.x -   (pMB-1)->mvs[0].x) < deltax) && (abs(mv.y -   (pMB-1)->mvs[0].y) < deltay) )
2312          if (*Data->iMinSAD < MAX_SAD00_FOR_SKIP * 4) {                  &&   ( (abs(mv.x -   (pMB+1)->mvs[0].x) < deltax) && (abs(mv.y -   (pMB+1)->mvs[0].y) < deltay) )
2313                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];                  &&   ( (abs(mv.x - (pMB-MBw)->mvs[0].x) < deltax) && (abs(mv.y - (pMB-MBw)->mvs[0].y) < deltay) )
2314                  pMB->mode = MODE_NOT_CODED;                  &&   ( (abs(mv.x - (pMB+MBw)->mvs[0].x) < deltax) && (abs(mv.y - (pMB+MBw)->mvs[0].y) < deltay) ) )
2315                  return 0;                          MBmask[mbnum]=1;
2316          }          }
2317    
2318          if (!(mask = make_mask(pmv, 1)))          for (my = 1; my < (uint32_t)MBh-1; my++)
2319                  (*CheckCandidate)(pmv[1].x, pmv[1].y, mask, &i, Data);          for (mx = 1; mx < (uint32_t)MBw-1; mx++)
2320          if (!(mask = make_mask(pmv, 2)))          {
2321                  (*CheckCandidate)(pmv[2].x, pmv[2].y, mask, &i, Data);                  const uint8_t *const pCur = current->image.y + 16*my*pParam->edged_width + 16*mx;
2322    
2323          if (*Data->iMinSAD > MAX_SAD00_FOR_SKIP * 4) // diamond only if needed                  const int mbnum = mx + my * MBw;
2324                  DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);                  if (!MBmask[mbnum])
2325                            continue;
2326    
2327                    if (sad16 ( pCur, pCur+1 , pParam->edged_width, 65536) <= (uint32_t)grad )
2328                            MBmask[mbnum] = 0;
2329                    if (sad16 ( pCur, pCur+pParam->edged_width, pParam->edged_width, 65536) <= (uint32_t)grad )
2330                            MBmask[mbnum] = 0;
2331    
         pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];  
         pMB->mode = MODE_INTER;  
         return *(Data->iMinSAD);  
2332  }  }
2333    
2334  #define INTRA_THRESH    1350          emms();
 #define INTER_THRESH    900  
2335    
2336            do {            /* until convergence */
2337    
2338  int          a = b = c = n = 0;
2339  MEanalysis(     const IMAGE * const pRef,          DtimesF[0] = DtimesF[1] = DtimesF[2] = DtimesF[3] = 0.;
2340                          FRAMEINFO * const Current,          for (my = 0; my < (uint32_t)MBh; my++)
2341                          MBParam * const pParam,                  for (mx = 0; mx < (uint32_t)MBw; mx++)
2342                          int maxIntra, //maximum number if non-I frames                  {
2343                          int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame                          const int mbnum = mx + my * MBw;
2344                          int bCount) // number if B frames in a row                          const MACROBLOCK *pMB = &pMBs[mbnum];
2345  {                          const VECTOR mv = pMB->mvs[0];
         uint32_t x, y, intra = 0;  
         int sSAD = 0;  
         MACROBLOCK * const pMBs = Current->mbs;  
         const IMAGE * const pCurrent = &Current->image;  
         int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH;  
2346    
2347          VECTOR currentMV;                          if (!MBmask[mbnum])
2348          int32_t iMinSAD;                                  continue;
         SearchData Data;  
         Data.iEdgedWidth = pParam->edged_width;  
         Data.currentMV = &currentMV;  
         Data.iMinSAD = &iMinSAD;  
         Data.iFcode = Current->fcode;  
         CheckCandidate = CheckCandidate16no4vI;  
2349    
2350          if (intraCount < 10) // we're right after an I frame                          n++;
2351                  IntraThresh += 4 * (intraCount - 10) * (intraCount - 10);                          a += 16*mx+8;
2352          else                          b += 16*my+8;
2353                  if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec                          c += (16*mx+8)*(16*mx+8)+(16*my+8)*(16*my+8);
2354                          IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra;  
2355                            DtimesF[0] += (double)mv.x;
2356                            DtimesF[1] += (double)mv.x*(16*mx+8) + (double)mv.y*(16*my+8);
2357                            DtimesF[2] += (double)mv.x*(16*my+8) - (double)mv.y*(16*mx+8);
2358                            DtimesF[3] += (double)mv.y;
2359                    }
2360    
2361            denom = a*a+b*b-c*n;
2362    
2363    /* Solve the system:    sol = (D'*E*D)^{-1} D'*E*F   */
2364    /* D'*E*F has been calculated in the same loop as matrix */
2365    
2366            sol[0] = -c*DtimesF[0] + a*DtimesF[1] + b*DtimesF[2];
2367            sol[1] =  a*DtimesF[0] - n*DtimesF[1]                + b*DtimesF[3];
2368            sol[2] =  b*DtimesF[0]                - n*DtimesF[2] - a*DtimesF[3];
2369            sol[3] =                 b*DtimesF[1] - a*DtimesF[2] - c*DtimesF[3];
2370    
2371            sol[0] /= denom;
2372            sol[1] /= denom;
2373            sol[2] /= denom;
2374            sol[3] /= denom;
2375    
2376            meanx = meany = 0.;
2377            oldnum = 0;
2378            for (my = 0; my < (uint32_t)MBh; my++)
2379                    for (mx = 0; mx < (uint32_t)MBw; mx++)
2380                    {
2381                            const int mbnum = mx + my * MBw;
2382                            const MACROBLOCK *pMB = &pMBs[mbnum];
2383                            const VECTOR mv = pMB->mvs[0];
2384    
2385                            if (!MBmask[mbnum])
2386                                    continue;
2387    
2388                            oldnum++;
2389                            meanx += fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x );
2390                            meany += fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y );
2391                    }
2392    
2393          InterThresh += 300 * (1 - bCount);          if (4*meanx > oldnum)   /* better fit than 0.25 is useless */
2394          if (InterThresh < 200) InterThresh = 200;                  meanx /= oldnum;
2395            else
2396                    meanx = 0.25;
2397    
2398          if (sadInit) (*sadInit) ();          if (4*meany > oldnum)
2399                    meany /= oldnum;
2400            else
2401                    meany = 0.25;
2402    
2403          for (y = 1; y < pParam->mb_height-1; y++) {  /*      fprintf(stderr,"sol = (%8.5f, %8.5f, %8.5f, %8.5f)\n",sol[0],sol[1],sol[2],sol[3]);
2404                  for (x = 1; x < pParam->mb_width-1; x++) {          fprintf(stderr,"meanx = %8.5f  meany = %8.5f   %d\n",meanx,meany, oldnum);
2405                          int sad, dev;  */
2406                          MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];          num = 0;
2407            for (my = 0; my < (uint32_t)MBh; my++)
2408                    for (mx = 0; mx < (uint32_t)MBw; mx++)
2409                    {
2410                            const int mbnum = mx + my * MBw;
2411                            const MACROBLOCK *pMB = &pMBs[mbnum];
2412                            const VECTOR mv = pMB->mvs[0];
2413    
2414                          sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y,                          if (!MBmask[mbnum])
2415                                                                  pParam, pMBs, pMB, &Data);                                  continue;
2416    
2417                          if (sad > IntraThresh) {                          if  ( ( fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ) > meanx )
2418                                  dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,                                  || ( fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ) > meany ) )
2419                                                            pParam->edged_width);                                  MBmask[mbnum]=0;
2420                                  if (dev + IntraThresh < sad) {                          else
2421                                          pMB->mode = MODE_INTRA;                                  num++;
                                         if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2;  // I frame  
                                 }  
                         }  
                         sSAD += sad;  
2422                  }                  }
2423    
2424            } while ( (oldnum != num) && (num>=4) );
2425    
2426            if (num < 4)
2427            {
2428                    gmc.duv[0].x= gmc.duv[0].y= gmc.duv[1].x= gmc.duv[1].y= gmc.duv[2].x= gmc.duv[2].y=0;
2429            } else {
2430    
2431                    gmc.duv[0].x=(int)(sol[0]+0.5);
2432                    gmc.duv[0].y=(int)(sol[3]+0.5);
2433    
2434                    gmc.duv[1].x=(int)(sol[1]*pParam->width+0.5);
2435                    gmc.duv[1].y=(int)(-sol[2]*pParam->width+0.5);
2436    
2437                    gmc.duv[2].x=0;
2438                    gmc.duv[2].y=0;
2439          }          }
2440          sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2);  //      fprintf(stderr,"wp1 = ( %4d, %4d)  wp2 = ( %4d, %4d) \n", gmc.duv[0].x, gmc.duv[0].y, gmc.duv[1].x, gmc.duv[1].y);
2441          if (sSAD > InterThresh ) return 1; //P frame  
2442          emms();          free(MBmask);
         return 0; // B frame  
2443    
2444            return gmc;
2445  }  }
2446    
2447  int  // functions which perform BITS-based search/bitcount
2448  FindFcode(      const MBParam * const pParam,  
2449                          const FRAMEINFO * const current)  static int
2450    CountMBBitsInter(SearchData * const Data,
2451                                    const MACROBLOCK * const pMBs, const int x, const int y,
2452                                    const MBParam * const pParam,
2453                                    const uint32_t MotionFlags)
2454  {  {
2455          uint32_t x, y;          int i, iDirection;
2456          int max = 0, min = 0, i;          int32_t bsad[5];
2457    
2458            CheckCandidate = CheckCandidateBits16;
2459    
2460            if (Data->qpel) {
2461                    for(i = 0; i < 5; i++) {
2462                            Data->currentMV[i].x = Data->currentQMV[i].x/2;
2463                            Data->currentMV[i].y = Data->currentQMV[i].y/2;
2464                    }
2465                    Data->qpel_precision = 1;
2466                    CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data);
2467    
2468          for (y = 0; y < pParam->mb_height; y++) {                  //checking if this vector is perfect. if it is, we stop.
2469                  for (x = 0; x < pParam->mb_width; x++) {                  if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0)
2470                            return 0; //quick stop
2471    
2472                    if (MotionFlags & (XVID_ME_HALFPELREFINE16_BITS | XVID_ME_EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search
2473                            for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i];
2474                            get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
2475                                                    pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);
2476                            Data->qpel_precision = 0;
2477                            if (Data->currentQMV->x & 1 || Data->currentQMV->y & 1)
2478                                    CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);
2479                    }
2480    
2481                          MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];          } else { // not qpel
                         for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4:1); i++) {  
                                 if (pMB->mvs[i].x > max) max = pMB->mvs[i].x;  
                                 if (pMB->mvs[i].y > max) max = pMB->mvs[i].y;  
2482    
2483                                  if (pMB->mvs[i].x < min) min = pMB->mvs[i].x;                  CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);
2484                                  if (pMB->mvs[i].y < min) min = pMB->mvs[i].y;                  //checking if this vector is perfect. if it is, we stop.
2485                    if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0) {
2486                            return 0; //inter
2487                          }                          }
2488                  }                  }
2489    
2490            if (MotionFlags&XVID_ME_EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
2491    
2492            if (MotionFlags&XVID_ME_HALFPELREFINE16_BITS) SubpelRefine(Data);
2493    
2494            if (Data->qpel) {
2495                    if (MotionFlags&(XVID_ME_EXTSEARCH_BITS | XVID_ME_HALFPELREFINE16_BITS)) { // there was halfpel-precision search
2496                            for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) {
2497                                    Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // we have found a better match
2498                                    Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
2499          }          }
2500    
2501          min = -min;                          // preparing for qpel-precision search
2502          max += 1;                          Data->qpel_precision = 1;
2503          if (min > max) max = min;                          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
2504          if (pParam->m_quarterpel) max *= 2;                                          pParam->width, pParam->height, Data->iFcode, 1, 0);
2505                    }
2506                    if (MotionFlags&XVID_ME_QUARTERPELREFINE16_BITS) SubpelRefine(Data);
2507            }
2508    
2509          for (i = 1; (max > 32 << (i - 1)); i++);          if (MotionFlags&XVID_ME_CHECKPREDICTION_BITS) { //let's check vector equal to prediction
2510          return i;                  VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV;
2511                    if (!(Data->predMV.x == v->x && Data->predMV.y == v->y))
2512                            CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data);
2513            }
2514            return Data->iMinSAD[0];
2515  }  }
2516    
2517  static void  
2518  CheckGMC(int x, int y, const int dir, int * iDirection,  static int
2519                  const MACROBLOCK * const pMBs, uint32_t * bestcount, VECTOR * GMC,  CountMBBitsInter4v(const SearchData * const Data,
2520                  const MBParam * const pParam)                                          MACROBLOCK * const pMB, const MACROBLOCK * const pMBs,
2521                                            const int x, const int y,
2522                                            const MBParam * const pParam, const uint32_t MotionFlags,
2523                                            const VECTOR * const backup)
2524  {  {
         uint32_t mx, my, a, count = 0;  
2525    
2526          for (my = 1; my < pParam->mb_height-1; my++)          int cbp = 0, bits = 0, t = 0, i, iDirection;
2527                  for (mx = 1; mx < pParam->mb_width-1; mx++) {          SearchData Data2, *Data8 = &Data2;
2528                          VECTOR mv;          int sumx = 0, sumy = 0;
2529                          const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];          int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64;
2530                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) continue;  
2531                          mv = pMB->mvs[0];          memcpy(Data8, Data, sizeof(SearchData));
2532                          a = ABS(mv.x - x) + ABS(mv.y - y);          CheckCandidate = CheckCandidateBits8;
2533                          if (a < 6) count += 6 - a;  
2534                  }          for (i = 0; i < 4; i++) {
2535                    Data8->iMinSAD = Data->iMinSAD + i + 1;
2536                    Data8->currentMV = Data->currentMV + i + 1;
2537                    Data8->currentQMV = Data->currentQMV + i + 1;
2538                    Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2539                    Data8->RefP[0] = Data->RefP[0] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2540                    Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2541                    Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2542                    Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2543    
2544          if (count > *bestcount) {                  if(Data->qpel) {
2545                  *bestcount = count;                          Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i);
2546                  *iDirection = dir;                          if (i != 0)     t = d_mv_bits(  Data8->currentQMV->x, Data8->currentQMV->y,
2547                  GMC->x = x; GMC->y = y;                                                                                  Data8->predMV, Data8->iFcode, 0, 0);
2548          }                  } else {
2549                            Data8->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, i);
2550                            if (i != 0)     t = d_mv_bits(  Data8->currentMV->x, Data8->currentMV->y,
2551                                                                                    Data8->predMV, Data8->iFcode, 0, 0);
2552  }  }
2553    
2554                    get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8,
2555                                            pParam->width, pParam->height, Data8->iFcode, Data8->qpel, 0);
2556    
2557                    *Data8->iMinSAD += t;
2558    
2559  static VECTOR                  Data8->qpel_precision = Data8->qpel;
2560  GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode)                  // checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far)
2561  {  {
2562                            VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV;
2563                            if (!( (v->x == backup[i+1].x) && (v->y == backup[i+1].y) ))
2564                                    CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8);
2565                    }
2566    
2567          uint32_t count, bestcount = 0;                  if (Data8->qpel) {
2568          int x, y;                          if (MotionFlags&XVID_ME_HALFPELREFINE8_BITS || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_BITS)) { // halfpixel motion search follows
2569          VECTOR gmc = {0,0};                                  int32_t s = *Data8->iMinSAD;
2570          int step, min_x, max_x, min_y, max_y;                                  Data8->currentMV->x = Data8->currentQMV->x/2;
2571          uint32_t mx, my;                                  Data8->currentMV->y = Data8->currentQMV->y/2;
2572          int iDirection, bDirection;                                  Data8->qpel_precision = 0;
2573                                    get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8,
2574                                                            pParam->width, pParam->height, Data8->iFcode - 1, 0, 0);
2575    
2576          min_x = min_y = -32<<iFcode;                                  if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1)
2577          max_x = max_y = 32<<iFcode;                                          CheckCandidateBits8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8);
2578    
2579  //step1: let's find a rough camera panning                                  if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS)
2580          for (step = 32; step >= 2; step /= 2) {                                          SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);
                 bestcount = 0;  
                 for (y = min_y; y <= max_y; y += step)  
                         for (x = min_x ; x <= max_x; x += step) {  
                                 count = 0;  
                                 //for all macroblocks  
                                 for (my = 1; my < pParam->mb_height-1; my++)  
                                         for (mx = 1; mx < pParam->mb_width-1; mx++) {  
                                                 const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];  
                                                 VECTOR mv;  
2581    
2582                                                  if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED)                                  if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8);
                                                         continue;  
2583    
2584                                                  mv = pMB->mvs[0];                                  if(s > *Data8->iMinSAD) { //we have found a better match
2585                                                  if ( ABS(mv.x - x) <= step && ABS(mv.y - y) <= step )   /* GMC translation is always halfpel-res */                                          Data8->currentQMV->x = 2*Data8->currentMV->x;
2586                                                          count++;                                          Data8->currentQMV->y = 2*Data8->currentMV->y;
2587                                          }                                          }
2588                                  if (count >= bestcount) { bestcount = count; gmc.x = x; gmc.y = y; }  
2589                                    Data8->qpel_precision = 1;
2590                                    get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8,
2591                                                            pParam->width, pParam->height, Data8->iFcode, 1, 0);
2592    
2593                          }                          }
2594                  min_x = gmc.x - step;                          if (MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS) SubpelRefine(Data8);
                 max_x = gmc.x + step;  
                 min_y = gmc.y - step;  
                 max_y = gmc.y + step;  
2595    
2596                    } else // not qpel
2597                            if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8); //halfpel mode, halfpel refinement
2598    
2599                    //checking vector equal to predicion
2600                    if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_BITS) {
2601                            const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV;
2602                            if (!(Data8->predMV.x == v->x && Data8->predMV.y == v->y))
2603                                    CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8);
2604          }          }
2605    
2606          if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10)                  bits += *Data8->iMinSAD;
2607                  gmc.x = gmc.y = 0; //no camara pan, no GMC                  if (bits >= Data->iMinSAD[0]) break; // no chances for INTER4V
2608    
2609  // step2: let's refine camera panning using gradiend-descent approach.                  // MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else
2610  // TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond)                  if(Data->qpel) {
2611          bestcount = 0;                          pMB->pmvs[i].x = Data8->currentQMV->x - Data8->predMV.x;
2612          CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam);                          pMB->pmvs[i].y = Data8->currentQMV->y - Data8->predMV.y;
2613          do {                          pMB->qmvs[i] = *Data8->currentQMV;
2614                  x = gmc.x; y = gmc.y;                          sumx += Data8->currentQMV->x/2;
2615                  bDirection = iDirection; iDirection = 0;                          sumy += Data8->currentQMV->y/2;
2616                  if (bDirection & 1) CheckGMC(x - 1, y, 1+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);                  } else {
2617                  if (bDirection & 2) CheckGMC(x + 1, y, 2+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);                          pMB->pmvs[i].x = Data8->currentMV->x - Data8->predMV.x;
2618                  if (bDirection & 4) CheckGMC(x, y - 1, 1+2+4, &iDirection, pMBs, &bestcount, &gmc, pParam);                          pMB->pmvs[i].y = Data8->currentMV->y - Data8->predMV.y;
2619                  if (bDirection & 8) CheckGMC(x, y + 1, 1+2+8, &iDirection, pMBs, &bestcount, &gmc, pParam);                          sumx += Data8->currentMV->x;
2620                            sumy += Data8->currentMV->y;
2621                    }
2622                    pMB->mvs[i] = *Data8->currentMV;
2623                    pMB->sad8[i] = 4 * *Data8->iMinSAD;
2624                    if (Data8->temp[0]) cbp |= 1 << (5 - i);
2625            }
2626    
2627            if (bits < *Data->iMinSAD) { // there is still a chance for inter4v mode. let's check chroma
2628                    const uint8_t * ptr;
2629                    sumx = (sumx >> 3) + roundtab_76[sumx & 0xf];
2630                    sumy = (sumy >> 3) + roundtab_76[sumy & 0xf];
2631    
2632                    //chroma U
2633                    ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);
2634                    transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2);
2635                    fdct(in);
2636                    if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16);
2637                    else i = quant4_inter(coeff, in, Data->lambda16);
2638                    if (i > 0) {
2639                            bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
2640                            cbp |= 1 << (5 - 4);
2641                    }
2642    
2643                    if (bits < *Data->iMinSAD) { // still possible
2644                            //chroma V
2645                            ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);
2646                            transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2);
2647                            fdct(in);
2648                            if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16);
2649                            else i = quant4_inter(coeff, in, Data->lambda16);
2650                            if (i > 0) {
2651                                    bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
2652                                    cbp |= 1 << (5 - 5);
2653                            }
2654                            bits += xvid_cbpy_tab[15-(cbp>>2)].len;
2655                            bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len;
2656                    }
2657            }
2658    
2659            return bits;
2660    }
2661    
2662    
2663    static int
2664    CountMBBitsIntra(const SearchData * const Data)
2665    {
2666            int bits = 1; //this one is ac/dc prediction flag. always 1.
2667            int cbp = 0, i, t, dc = 1024, b_dc;
2668            const uint32_t iQuant = Data->lambda16;
2669            int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64;
2670            uint32_t iDcScaler = get_dc_scaler(iQuant, 1);;
2671    
2672            for(i = 0; i < 4; i++) {
2673                    int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2674                    transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth);
2675                    fdct(in);
2676                    b_dc = in[0];
2677                    in[0] -= dc;
2678                    dc = b_dc;
2679                    if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler);
2680                    else quant4_intra(coeff, in, iQuant, iDcScaler);
2681    
2682                    bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcy_tab[coeff[0] + 255].len;;
2683                    Data->temp[i] = t;
2684                    if (t != 0)  cbp |= 1 << (5 - i);
2685                    if (bits >= Data->iMinSAD[0]) break;
2686            }
2687    
2688            if (bits < Data->iMinSAD[0]) { // INTRA still looks good, let's add chroma
2689                    iDcScaler = get_dc_scaler(iQuant, 0);
2690                    //chroma U
2691                    transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2);
2692                    fdct(in);
2693                    in[0] -= 1024;
2694                    if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler);
2695                    else quant4_intra(coeff, in, iQuant, iDcScaler);
2696    
2697                    bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len;
2698                    if (t != 0) cbp |= 1 << (5 - 4);
2699    
2700                    if (bits < Data->iMinSAD[0]) {
2701                            //chroma V
2702                            transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2);
2703                            fdct(in);
2704                            in[0] -= 1024;
2705                            if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler);
2706                            else quant4_intra(coeff, in, iQuant, iDcScaler);
2707    
2708          } while (iDirection);                          bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len;
2709                            if (t != 0) cbp |= 1 << (5 - 5);
2710    
2711          if (pParam->m_quarterpel) {                          bits += xvid_cbpy_tab[cbp>>2].len;
2712                  gmc.x *= 2;                          bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len;
                 gmc.y *= 2;     /* we store the halfpel value as pseudo-qpel to make comparison easier */  
2713          }          }
2714            }
2715          return gmc;          return bits;
2716  }  }

Legend:
Removed from v.1.44.2.19  
changed lines
  Added in v.1.58.2.12

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