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

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

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

revision 601, Thu Oct 17 13:50:23 2002 UTC revision 702, Tue Dec 10 11:13:50 2002 UTC
# Line 52  Line 52 
52  #define CHECK_CANDIDATE(X,Y,D) { \  #define CHECK_CANDIDATE(X,Y,D) { \
53  (*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); }  (*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); }
54    
 #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; \  
         } \  
 }  
   
55  #define iDiamondSize 2  #define iDiamondSize 2
56    
57    static VECTOR
58    GlobalMotionEst(const MACROBLOCK * const pMBs,
59                                    const MBParam * const pParam, const uint32_t iFcode);
60    
61    
62  static __inline int  static __inline int
63  d_mv_bits(int x, int y, const uint32_t iFcode)  d_mv_bits(int x, int y, const uint32_t iFcode)
64  {  {
# Line 89  Line 84 
84          return xb + yb;          return xb + yb;
85  }  }
86    
87    static int32_t
88  /* CHECK_CANDIATE FUNCTIONS START */  ChromaSAD(int dx, int dy, const SearchData * const data)
   
 static void  
 CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  
89  {  {
90          int t;          int sad;
91          const uint8_t * Reference;          dx = (dx >> 1) + roundtab_79[dx & 0x3];
92            dy = (dy >> 1) + roundtab_79[dy & 0x3];
93          if (( x > data->max_dx) || ( x < data->min_dx)  
94                  || ( y > data->max_dy) || (y < data->min_dy)) return;          switch (((dx & 1) << 1) + (dy & 1))     { // ((dx%2)?2:0)+((dy%2)?1:0)
95                    case 0:
96                            sad = sad8(data->CurU, data->RefCU + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2);
97                            sad += sad8(data->CurV, data->RefCV + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2);
98                            break;
99                    case 1:
100                            dx = dx / 2; dy = (dy - 1) / 2;
101                            sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2);
102                            sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2);
103                            break;
104                    case 2:
105                            dx = (dx - 1) / 2; dy = dy / 2;
106                            sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2);
107                            sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2);
108                            break;
109                    default:
110                            dx = (dx - 1) / 2; dy = (dy - 1) / 2;
111                            interpolate8x8_halfpel_hv(data->RefQ,
112                                                                             data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2,
113                                                                             data->rounding);
114                            sad = sad8(data->CurU, data->RefQ, data->iEdgedWidth/2);
115                            interpolate8x8_halfpel_hv(data->RefQ,
116                                                                             data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2,
117                                                                             data->rounding);
118                            sad += sad8(data->CurV, data->RefQ, data->iEdgedWidth/2);
119                            break;
120            }
121            return sad;
122    }
123    
124    static __inline const uint8_t *
125    GetReference(const int x, const int y, const int dir, const SearchData * const data)
126    {
127    //      dir : 0 = forward, 1 = backward
128            switch ( (dir << 2) | ((x&1)<<1) | (y&1) ) {
129                    case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth);
130                    case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth);
131                    case 2 : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth);
132                    case 3 : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth);
133                    case 4 : return data->bRef + x/2 + (y/2)*(data->iEdgedWidth);
134                    case 5 : return data->bRefV + x/2 + ((y-1)/2)*(data->iEdgedWidth);
135                    case 6 : return data->bRefH + (x-1)/2 + (y/2)*(data->iEdgedWidth);
136                    default : return data->bRefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth);
137    
         switch ( ((x&1)<<1) + (y&1) ) {  
                 case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;  
                 case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
                 case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;  
                 default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
138          }          }
   
         data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1);  
   
         t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);  
         data->temp[0] += data->lambda16 * t;  
         data->temp[1] += data->lambda8 * t;  
   
         if (data->temp[0] < data->iMinSAD[0]) {  
                 data->iMinSAD[0] = data->temp[0];  
                 data->currentMV[0].x = x; data->currentMV[0].y = y;  
                 *dir = Direction; }  
   
         if (data->temp[1] < data->iMinSAD[1]) {  
                 data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; }  
         if (data->temp[2] < data->iMinSAD[2]) {  
                 data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; }  
         if (data->temp[3] < data->iMinSAD[3]) {  
                 data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; }  
         if (data->temp[4] < data->iMinSAD[4]) {  
                 data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; }  
   
139  }  }
140    
141  static void  static uint8_t *
142  CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  Interpolate8x8qpel(const int x, const int y, const int block, const int dir, const SearchData * const data)
143  {  {
144          int32_t sad;  // create or find a qpel-precision reference picture; return pointer to it
145          const uint8_t * Reference;          uint8_t * Reference = (uint8_t *)data->RefQ + 16*dir;
146            const int32_t iEdgedWidth = data->iEdgedWidth;
147            const uint32_t rounding = data->rounding;
148            const int halfpel_x = x/2;
149            const int halfpel_y = y/2;
150            const uint8_t *ref1, *ref2, *ref3, *ref4;
151    
152          if (( x > data->max_dx) || ( x < data->min_dx)          ref1 = GetReference(halfpel_x, halfpel_y, dir, data); // this reference is used in all cases
153                  || ( y > data->max_dy) || (y < data->min_dy)) return;          ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
154            switch( ((x&1)<<1) + (y&1) ) {
155            case 0: // pure halfpel position
156                    Reference = (uint8_t *) GetReference(halfpel_x, halfpel_y, dir, data);
157                    Reference += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
158                    break;
159    
160          switch ( ((x&1)<<1) + (y&1) )          case 1: // x halfpel, y qpel - top or bottom during qpel refinement
161          {                  ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);
162                  case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
163                  case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
164                  case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;                  break;
                 default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
         }  
165    
166          sad = data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);          case 2: // x qpel, y halfpel - left or right during qpel refinement
167          sad += sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR);                  ref2 = GetReference(x - halfpel_x, halfpel_y, dir, data);
168                    ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
169                    interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
170                    break;
171    
172          if (sad < *(data->iMinSAD)) {          default: // x and y in qpel resolution - the "corners" (top left/right and
173                  *(data->iMinSAD) = sad;                           // bottom left/right) during qpel refinement
174                  data->currentMV[0].x = x; data->currentMV[0].y = y;                  ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);
175                  *dir = Direction; }                  ref3 = GetReference(x - halfpel_x, halfpel_y, dir, data);
176                    ref4 = GetReference(x - halfpel_x, y - halfpel_y, dir, data);
177                    ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
178                    ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
179                    ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
180                    interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
181                    break;
182            }
183            return Reference;
184  }  }
185    
186  static void  static uint8_t *
187  CheckCandidate16_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  Interpolate16x16qpel(const int x, const int y, const int dir, const SearchData * const data)
   
 // CheckCandidate16 variant which expects x and y in quarter pixel resolution  
 // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)  
 // around currentMV!  
188  {  {
189          int t;  // create or find a qpel-precision reference picture; return pointer to it
190          uint8_t * Reference = (uint8_t *)data->RefQ;          uint8_t * Reference = (uint8_t *)data->RefQ + 16*dir;
191            const int32_t iEdgedWidth = data->iEdgedWidth;
192            const uint32_t rounding = data->rounding;
193            const int halfpel_x = x/2;
194            const int halfpel_y = y/2;
195          const uint8_t *ref1, *ref2, *ref3, *ref4;          const uint8_t *ref1, *ref2, *ref3, *ref4;
         VECTOR halfpelMV = *(data->currentMV);  
   
         int32_t iEdgedWidth = data->iEdgedWidth;  
         uint32_t rounding = data->rounding;  
   
         if (( x > data->max_dx) || ( x < data->min_dx)  
                 || ( y > data->max_dy) || (y < data->min_dy)) return;  
   
         switch( ((x&1)<<1) + (y&1) )  
         {  
         case 0: // pure halfpel position - shouldn't happen during a refinement step  
                 GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);  
                 break;  
196    
197            ref1 = GetReference(halfpel_x, halfpel_y, dir, data); // this reference is used in all cases
198            switch( ((x&1)<<1) + (y&1) ) {
199            case 0: // pure halfpel position
200                    return (uint8_t *) GetReference(halfpel_x, halfpel_y, dir, data);
201          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          case 1: // x halfpel, y qpel - top or bottom during qpel refinement
202                  GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);                  ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);
203                  GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
204                    interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
205                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8);
206                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, 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);  
207                  break;                  break;
208    
209          case 2: // x qpel, y halfpel - left or right during qpel refinement          case 2: // x qpel, y halfpel - left or right during qpel refinement
210                  GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);                  ref2 = GetReference(x - halfpel_x, halfpel_y, dir, data);
211                  GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
212                    interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
213                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8);
214                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, 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);  
215                  break;                  break;
216    
217          default: // x and y in qpel resolution - the "corners" (top left/right and          default: // x and y in qpel resolution - the "corners" (top left/right and
218                           // bottom left/right) during qpel refinement                           // bottom left/right) during qpel refinement
219                  GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);                  ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);
220                  GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);                  ref3 = GetReference(x - halfpel_x, halfpel_y, dir, data);
221                  GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3);                  ref4 = GetReference(x - halfpel_x, y - halfpel_y, dir, data);
                 GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4);  
   
222                  interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);                  interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
223                  interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);                  interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);
224                  interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);                  interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);
225                  interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);                  interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);
226                  break;                  break;
227          }          }
228            return Reference;
229    }
230    
231    /* CHECK_CANDIATE FUNCTIONS START */
232    
233    static void
234    CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
235    {
236            int t, xc, yc;
237            const uint8_t * Reference;
238            VECTOR * current;
239    
240            if (( x > data->max_dx) || ( x < data->min_dx)
241                    || ( y > data->max_dy) || (y < data->min_dy)) return;
242    
243            if (data->qpel_precision) { // x and y are in 1/4 precision
244                    Reference = Interpolate16x16qpel(x, y, 0, data);
245                    t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
246                    xc = x/2; yc = y/2; //for chroma sad
247                    current = data->currentQMV;
248            } else {
249                    switch ( ((x&1)<<1) + (y&1) ) {
250                            case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
251                            case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
252                            case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
253                            default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
254                    }
255                    if (data->qpel) t = d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode);
256                    else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
257                    current = data->currentMV;
258                    xc = x; yc = y;
259            }
260    
261          data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1);          data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1);
262    
263          t = d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode);          data->temp[0] += (data->lambda16 * t * data->temp[0])/1000;
264          data->temp[0] += data->lambda16 * t;          data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100;
265          data->temp[1] += data->lambda8 * t;  
266            if (data->chroma) data->temp[0] += ChromaSAD(xc, yc, data);
267    
268          if (data->temp[0] < data->iMinSAD[0]) {          if (data->temp[0] < data->iMinSAD[0]) {
269                  data->iMinSAD[0] = data->temp[0];                  data->iMinSAD[0] = data->temp[0];
270                  data->currentQMV[0].x = x; data->currentQMV[0].y = y;                  current[0].x = x; current[0].y = y;
271          /*      *dir = Direction;*/ }                  *dir = Direction; }
272    
273          if (data->temp[1] < data->iMinSAD[1]) {          if (data->temp[1] < data->iMinSAD[1]) {
274                  data->iMinSAD[1] = data->temp[1]; data->currentQMV[1].x = x; data->currentQMV[1].y = y; }                  data->iMinSAD[1] = data->temp[1]; current[1].x = x; current[1].y= y; }
275          if (data->temp[2] < data->iMinSAD[2]) {          if (data->temp[2] < data->iMinSAD[2]) {
276                  data->iMinSAD[2] = data->temp[2]; data->currentQMV[2].x = x; data->currentQMV[2].y = y; }                  data->iMinSAD[2] = data->temp[2]; current[2].x = x; current[2].y = y; }
277          if (data->temp[3] < data->iMinSAD[3]) {          if (data->temp[3] < data->iMinSAD[3]) {
278                  data->iMinSAD[3] = data->temp[3]; data->currentQMV[3].x = x; data->currentQMV[3].y = y; }                  data->iMinSAD[3] = data->temp[3]; current[3].x = x; current[3].y = y; }
279          if (data->temp[4] < data->iMinSAD[4]) {          if (data->temp[4] < data->iMinSAD[4]) {
280                  data->iMinSAD[4] = data->temp[4]; data->currentQMV[4].x = x; data->currentQMV[4].y = y; }                  data->iMinSAD[4] = data->temp[4]; current[4].x = x; current[4].y = y; }
281    
282  }  }
283    
284  static void  static void
285  CheckCandidate16no4v_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
   
 // CheckCandidate16no4v variant which expects x and y in quarter pixel resolution  
 // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)  
 // around currentMV!  
286  {  {
287          int32_t sad;          int32_t sad;
288          uint8_t * Reference = (uint8_t *) data->RefQ;          const uint8_t * Reference;
289          const uint8_t *ref1, *ref2, *ref3, *ref4;          int t;
290          VECTOR halfpelMV = *(data->currentMV);          VECTOR * current;
   
         int32_t iEdgedWidth = data->iEdgedWidth;  
         uint32_t rounding = data->rounding;  
291    
292          if (( x > data->max_dx) || ( x < data->min_dx)          if (( x > data->max_dx) || ( x < data->min_dx)
293                  || ( y > data->max_dy) || (y < data->min_dy)) return;                  || ( y > data->max_dy) || (y < data->min_dy)) return;
294    
295          switch( ((x&1)<<1) + (y&1) )          if (data->qpel_precision) { // x and y are in 1/4 precision
296          {                  Reference = Interpolate16x16qpel(x, y, 0, data);
297          case 0: // pure halfpel position - shouldn't happen during a refinement step                  t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
298                  GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);                  current = data->currentQMV;
299                  break;          } else {
300                    switch ( ((x&1)<<1) + (y&1) ) {
301          case 1: // x halfpel, y qpel - top or bottom during qpel refinement                          case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;
302                  GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);                          case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;
303                  GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);                          case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;
304                            default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;
305                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);                  }
306                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);                  if (data->qpel) t = d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode);
307                  interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding);                  else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
308                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding);                  current = data->currentMV;
                 break;  
   
         case 2: // x qpel, y halfpel - left or right during qpel refinement  
                 GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);  
                 GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);  
   
                 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);  
                 interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding);  
                 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);  
                 break;  
   
         default: // x and y in qpel resolution - the "corners" (top left/right and  
                          // bottom left/right) during qpel refinement  
                 GET_REFERENCE(halfpelMV.x, halfpelMV.y, 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, 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;  
309          }          }
310    
311          sad = data->lambda16 *          sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);
312                          d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode);          sad += (data->lambda16 * t * sad)/1000;
         sad += sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR);  
313    
314          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
315                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
316                  data->currentQMV[0].x = x; data->currentQMV[0].y = y;                  current->x = x; current->y = y;
317  //              *dir = Direction;                  *dir = Direction; }
         }  
318  }  }
319    
320  static void  static void
321  CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
322  {  {
323    // maximum speed - for P/B/I decision
324          int32_t sad;          int32_t sad;
325    
326          if (( x > data->max_dx) || ( x < data->min_dx)          if (( x > data->max_dx) || ( x < data->min_dx)
# Line 326  Line 340 
340  CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)
341  {  {
342          int32_t sad;          int32_t sad;
343          const int xb = data->currentMV[1].x;          int xb, yb, t;
         const int yb = data->currentMV[1].y;  
344          const uint8_t *ReferenceF, *ReferenceB;          const uint8_t *ReferenceF, *ReferenceB;
345            VECTOR *current;
346    
347          if (( xf > data->max_dx) || ( xf < data->min_dx)          if (( xf > data->max_dx) || ( xf < data->min_dx)
348                  || ( yf > data->max_dy) || (yf < data->min_dy)) return;                  || ( yf > data->max_dy) || (yf < data->min_dy)) return;
349    
350          switch ( ((xf&1)<<1) + (yf&1) ) {          if (data->qpel_precision) {
351                  case 0 : ReferenceF = data->Ref + xf/2 + (yf/2)*(data->iEdgedWidth); break;                  ReferenceF = Interpolate16x16qpel(xf, yf, 0, data);
352                  case 1 : ReferenceF = data->RefV + xf/2 + ((yf-1)/2)*(data->iEdgedWidth); break;                  xb = data->currentQMV[1].x; yb = data->currentQMV[1].y;
353                  case 2 : ReferenceF = data->RefH + (xf-1)/2 + (yf/2)*(data->iEdgedWidth); break;                  current = data->currentQMV;
354                  default : ReferenceF = data->RefHV + (xf-1)/2 + ((yf-1)/2)*(data->iEdgedWidth); break;                  ReferenceB = Interpolate16x16qpel(xb, yb, 1, data);
355          }                  t = d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode)
356                                     + d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode);
357          switch ( ((xb&1)<<1) + (yb&1) ) {          } else {
358                  case 0 : ReferenceB = data->bRef + xb/2 + (yb/2)*(data->iEdgedWidth); break;                  ReferenceF = Interpolate16x16qpel(2*xf, 2*yf, 0, data);
359                  case 1 : ReferenceB = data->bRefV + xb/2 + ((yb-1)/2)*(data->iEdgedWidth); break;                  xb = data->currentMV[1].x; yb = data->currentMV[1].y;
360                  case 2 : ReferenceB = data->bRefH + (xb-1)/2 + (yb/2)*(data->iEdgedWidth); break;                  ReferenceB = Interpolate16x16qpel(2*xb, 2*yb, 1, data);
361                  default : ReferenceB = data->bRefHV + (xb-1)/2 + ((yb-1)/2)*(data->iEdgedWidth); break;                  current = data->currentMV;
362                    if (data->qpel)
363                            t = d_mv_bits(2*xf - data->predMV.x, 2*yf - data->predMV.y, data->iFcode)
364                                             + d_mv_bits(2*xb - data->bpredMV.x, 2*yb - data->bpredMV.y, data->iFcode);
365                    else
366                            t = d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode)
367                                             + d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode);
368          }          }
369    
370          sad = data->lambda16 *          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
371                          ( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) +          sad += (data->lambda16 * t * sad)/1000;
                           d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode) );  
   
         sad += sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);  
372    
373          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
374                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
375                  data->currentMV->x = xf; data->currentMV->y = yf;                  current->x = xf; current->y = yf;
376                  *dir = Direction; }                  *dir = Direction; }
377  }  }
378    
379  static void  static void
380  CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
381  {  {
382          int32_t sad;          int32_t sad = 0;
383          int k;          int k;
384          const uint8_t *ReferenceF;          const uint8_t *ReferenceF;
385          const uint8_t *ReferenceB;          const uint8_t *ReferenceB;
# Line 370  Line 387 
387    
388          if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;          if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
389    
         sad = data->lambda16 * d_mv_bits(x, y, 1);  
   
390          for (k = 0; k < 4; k++) {          for (k = 0; k < 4; k++) {
391                  mvs.x = data->directmvF[k].x + x;                  mvs.x = data->directmvF[k].x + x;
392                  b_mvs.x = ((x == 0) ?                  b_mvs.x = ((x == 0) ?
# Line 388  Line 403 
403                          || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )                          || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
404                          || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;                          || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;
405    
406                  switch ( ((mvs.x&1)<<1) + (mvs.y&1) ) {                  if (!data->qpel) {
407                          case 0 : ReferenceF = data->Ref + mvs.x/2 + (mvs.y/2)*(data->iEdgedWidth); break;                          mvs.x *= 2; mvs.y *= 2;
408                          case 1 : ReferenceF = data->RefV + mvs.x/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;                          b_mvs.x *= 2; b_mvs.y *= 2; //we move to qpel precision anyway
                         case 2 : ReferenceF = data->RefH + (mvs.x-1)/2 + (mvs.y/2)*(data->iEdgedWidth); break;  
                         default : ReferenceF = data->RefHV + (mvs.x-1)/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;  
                 }  
   
                 switch ( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {  
                         case 0 : ReferenceB = data->bRef + b_mvs.x/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;  
                         case 1 : ReferenceB = data->bRefV + b_mvs.x/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;  
                         case 2 : ReferenceB = data->bRefH + (b_mvs.x-1)/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;  
                         default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;  
409                  }                  }
410                    ReferenceF = Interpolate8x8qpel(mvs.x, mvs.y, k, 0, data);
411                    ReferenceB = Interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data);
412    
413                  sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),                  sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
414                                                  ReferenceF + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),                                                  ReferenceF, ReferenceB,
                                                 ReferenceB + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),  
415                                                  data->iEdgedWidth);                                                  data->iEdgedWidth);
416                  if (sad > *(data->iMinSAD)) return;                  if (sad > *(data->iMinSAD)) return;
417          }          }
418    
419            sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;
420    
421          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
422                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
423                  data->currentMV->x = x; data->currentMV->y = y;                  data->currentMV->x = x; data->currentMV->y = y;
# Line 425  Line 434 
434    
435          if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;          if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
436    
                 sad = data->lambda16 * d_mv_bits(x, y, 1);  
   
437          mvs.x = data->directmvF[0].x + x;          mvs.x = data->directmvF[0].x + x;
438          b_mvs.x = ((x == 0) ?          b_mvs.x = ((x == 0) ?
439                  data->directmvB[0].x                  data->directmvB[0].x
# Line 442  Line 449 
449                  || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )                  || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )
450                  || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;                  || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;
451    
452          switch ( ((mvs.x&1)<<1) + (mvs.y&1) ) {          if (!data->qpel) {
453                  case 0 : ReferenceF = data->Ref + mvs.x/2 + (mvs.y/2)*(data->iEdgedWidth); break;                          mvs.x *= 2; mvs.y *= 2;
454                  case 1 : ReferenceF = data->RefV + mvs.x/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;                          b_mvs.x *= 2; b_mvs.y *= 2; //we move to qpel precision anyway
                 case 2 : ReferenceF = data->RefH + (mvs.x-1)/2 + (mvs.y/2)*(data->iEdgedWidth); break;  
                 default : ReferenceF = data->RefHV + (mvs.x-1)/2 + ((mvs.y-1)/2)*(data->iEdgedWidth); break;  
         }  
   
         switch ( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) {  
                 case 0 : ReferenceB = data->bRef + b_mvs.x/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;  
                 case 1 : ReferenceB = data->bRefV + b_mvs.x/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;  
                 case 2 : ReferenceB = data->bRefH + (b_mvs.x-1)/2 + (b_mvs.y/2)*(data->iEdgedWidth); break;  
                 default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break;  
455          }          }
456            ReferenceF = Interpolate16x16qpel(mvs.x, mvs.y, 0, data);
457            ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data);
458    
459          sad += sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
460            sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;
461    
462          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
463                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
# Line 467  Line 468 
468  static void  static void
469  CheckCandidate8(const int x, const int y, 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)
470  {  {
471          int32_t sad;          int32_t sad; int t;
472          const uint8_t * Reference;          const uint8_t * Reference;
473    
474          if (( x > data->max_dx) || ( x < data->min_dx)          if (( x > data->max_dx) || ( x < data->min_dx)
475                  || ( y > data->max_dy) || (y < data->min_dy)) return;                  || ( y > data->max_dy) || (y < data->min_dy)) return;
476    
477          switch ( ((x&1)<<1) + (y&1) )          if (data->qpel) Reference = Interpolate16x16qpel(x, y, 0, data);
478          {          else Reference = Interpolate16x16qpel(2*x, 2*y, 0, data);
                 case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;  
                 case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
                 case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;  
                 default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
         }  
479    
480          sad = sad8(data->Cur, Reference, data->iEdgedWidth);          sad = sad8(data->Cur, Reference, data->iEdgedWidth);
481          sad += data->lambda8 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);          if (data->qpel) t = d_mv_bits(2 * x - data->predMV.x, 2 * y - data->predMV.y, data->iFcode);
482            else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);
         if (sad < *(data->iMinSAD)) {  
                 *(data->iMinSAD) = sad;  
                 data->currentMV->x = x; data->currentMV->y = y;  
                 *dir = Direction; }  
 }  
   
 static void  
 CheckCandidate8_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  
 // CheckCandidate16no4v variant which expects x and y in quarter pixel resolution  
 // Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!)  
 // around currentMV!  
   
 {  
         int32_t sad;  
         uint8_t *Reference = (uint8_t *) data->RefQ;  
         const uint8_t *ref1, *ref2, *ref3, *ref4;  
         VECTOR halfpelMV = *(data->currentMV);  
   
         int32_t iEdgedWidth = data->iEdgedWidth;  
         uint32_t rounding = data->rounding;  
   
         if (( x > data->max_dx) || ( x < data->min_dx)  
                 || ( y > data->max_dy) || (y < data->min_dy)) return;  
   
         switch( ((x&1)<<1) + (y&1) )  
         {  
         case 0: // pure halfpel position - shouldn't happen during a refinement step  
                 GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference);  
                 break;  
   
         case 1: // x halfpel, y qpel - top or bottom during qpel refinement  
                 GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);  
                 GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2);  
   
                 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);  
                 break;  
   
         case 2: // x qpel, y halfpel - left or right during qpel refinement  
                 GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1);  
                 GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2);  
   
                 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding);  
                 break;  
483    
484          default: // x and y in qpel resolution - the "corners" (top left/right and          sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100;
                          // bottom left/right) during qpel refinement  
                 GET_REFERENCE(halfpelMV.x, halfpelMV.y, 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, rounding);  
                 break;  
         }  
   
         sad = sad8(data->Cur, Reference, data->iEdgedWidth);  
         sad += data->lambda8 * d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode);  
485    
486          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
487                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
488                  data->currentQMV->x = x; data->currentQMV->y = y;                  data->currentMV->x = x; data->currentMV->y = y;
489                  *dir = Direction; }                  *dir = Direction; }
490  }  }
491    
# Line 697  Line 638 
638  /* HALFPELREFINE COULD BE A MAINSEARCH FUNCTION, BUT THERE IS NO NEED FOR IT */  /* HALFPELREFINE COULD BE A MAINSEARCH FUNCTION, BUT THERE IS NO NEED FOR IT */
639    
640  static void  static void
641  HalfpelRefine(const SearchData * const data)  SubpelRefine(const SearchData * const data)
642  {  {
643  /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */  /* Do a half-pel or q-pel refinement */
644            VECTOR backupMV;
         VECTOR backupMV = *(data->currentMV);  
645          int iDirection; //not needed          int iDirection; //not needed
646    
647          CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);          if (data->qpel_precision)
648          CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);                  backupMV = *(data->currentQMV);
649          CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0);          else backupMV = *(data->currentMV);
         CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0);  
   
         CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0);  
         CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0);  
   
         CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);  
         CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);  
 }  
   
   
 static void  
 QuarterpelRefine(const SearchData * const data)  
 {  
 /* Perform quarter pixel refinement*/  
   
         VECTOR backupMV = *(data->currentQMV);  
         int iDirection; //not needed  
650    
651          CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);          CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);
652          CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);          CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);
# Line 735  Line 658 
658    
659          CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);          CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);
660          CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);          CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);
   
661  }  }
662    
663  static __inline int  static __inline int
# Line 786  Line 708 
708    
709          const VECTOR zeroMV = { 0, 0 };          const VECTOR zeroMV = { 0, 0 };
710    
711            int mb_width = pParam->mb_width;
712            int mb_height = pParam->mb_height;
713    
714          uint32_t x, y;          uint32_t x, y;
715          uint32_t iIntra = 0;          uint32_t iIntra = 0;
716          int32_t InterBias, quant = current->quant;          int32_t InterBias, quant = current->quant, sad00;
717          uint8_t *qimage;          uint8_t *qimage;
718    
719          // some pre-initialized thingies for SearchP          // some pre-initialized thingies for SearchP
# Line 804  Line 729 
729          Data.temp = temp;          Data.temp = temp;
730          Data.iFcode = current->fcode;          Data.iFcode = current->fcode;
731          Data.rounding = pParam->m_rounding_type;          Data.rounding = pParam->m_rounding_type;
732            Data.qpel = pParam->m_quarterpel;
733            Data.chroma = current->global_flags & XVID_ME_COLOUR;
734    
735            if ((current->global_flags & XVID_REDUCED))
736            {
737                    mb_width = (pParam->width + 31) / 32;
738                    mb_height = (pParam->height + 31) / 32;
739            }
740    
741          if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)          if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
742                  return 1; // allocate some mem for qpel interpolated blocks                  return 1; // allocate some mem for qpel interpolated blocks
743                                    // somehow this is dirty since I think we shouldn't use malloc outside                                    // somehow this is dirty since I think we shouldn't use malloc outside
744                                    // encoder_create() - so please fix me!                                    // encoder_create() - so please fix me!
745            Data.RefQ = qimage;
746          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();
747    
748          for (y = 0; y < pParam->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
749                  for (x = 0; x < pParam->mb_width; x++)  {                  for (x = 0; x < mb_width; x++)  {
   
750                          MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];                          MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
751                          int32_t sad00 =  pMB->sad16  
752                            pMB->sad16
753                                  = sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16,                                  = sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16,
754                                                          pRef->y + (x + y * pParam->edged_width) * 16,                                                          pRef->y + (x + y * pParam->edged_width) * 16,
755                                                          pParam->edged_width, pMB->sad8 );                                                          pParam->edged_width, pMB->sad8 );
756    
757                            if (Data.chroma) {
758                                    pMB->sad16 += sad8(pCurrent->u + x*8 + y*(pParam->edged_width/2)*8,
759                                                                    pRef->u + x*8 + y*(pParam->edged_width/2)*8, pParam->edged_width/2);
760    
761                                    pMB->sad16 += sad8(pCurrent->v + (x + y*(pParam->edged_width/2))*8,
762                                                                    pRef->v + (x + y*(pParam->edged_width/2))*8, pParam->edged_width/2);
763                            }
764    
765                            sad00 = pMB->sad16; //if no gmc; else sad00 = (..)
766    
767                          if (!(current->global_flags & XVID_LUMIMASKING)) {                          if (!(current->global_flags & XVID_LUMIMASKING)) {
768                                  pMB->dquant = NO_CHANGE;                                  pMB->dquant = NO_CHANGE;
769                                  pMB->quant = current->quant;                                  pMB->quant = current->quant;
# Line 834  Line 777 
777                          }                          }
778    
779  //initial skip decision  //initial skip decision
780    /* no early skip for GMC (global vector = skip vector is unknown!)  */
781                            if (current->coding_type == P_VOP)      { /* no fast SKIP for S(GMC)-VOPs */
782                          if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH)                          if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH)
783                                  if (SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {                                          if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {
784                                          SkipMacroblockP(pMB, sad00);                                          SkipMacroblockP(pMB, sad00);
785                                          continue;                                          continue;
786                                  }                                  }
787                            }
788    
789                          SearchP(pRef->y, pRefH->y, pRefV->y, pRefHV->y, qimage, pCurrent, x,                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
790                                                  y, current->motion_flags, pMB->quant,                                                  y, current->motion_flags, pMB->quant,
791                                                  &Data, pParam, pMBs, reference->mbs,                                                  &Data, pParam, pMBs, reference->mbs,
792                                                  current->global_flags & XVID_INTER4V, pMB);                                                  current->global_flags & XVID_INTER4V, pMB);
793    
794  /* 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?" */
795                          if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP                          if (current->coding_type == P_VOP)      {
796                                    if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP)
797                                  && ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH) )                                  && ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH) )
798                                  if (SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {                                          if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {
799                                          SkipMacroblockP(pMB, sad00);                                          SkipMacroblockP(pMB, sad00);
800                                          continue;                                          continue;
801                                  }                                  }
802                            }
803    
804  /* finally, intra decision */  /* finally, intra decision */
805    
806                          InterBias = MV16_INTER_BIAS;                          InterBias = MV16_INTER_BIAS;
807                          if (pMB->quant > 8)  InterBias += 50 * (pMB->quant - 8); // to make high quants work                          if (pMB->quant > 8)  InterBias += 100 * (pMB->quant - 8); // to make high quants work
808                          if (y != 0)                          if (y != 0)
809                                  if ((pMB - pParam->mb_width)->mode == MODE_INTER ) InterBias -= 50;                                  if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;
810                          if (x != 0)                          if (x != 0)
811                                  if ((pMB - 1)->mode == MODE_INTER ) InterBias -= 50;                                  if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
812    
813                            if (Data.chroma) InterBias += 50; // to compensate bigger SAD
814    
815                          if (InterBias < pMB->sad16)  {                          if (InterBias < pMB->sad16)  {
816                                  const int32_t deviation =                                  const int32_t deviation =
# Line 882  Line 831 
831                  }                  }
832          }          }
833          free(qimage);          free(qimage);
834    
835            if (current->coding_type == S_VOP)      /* first GMC step only for S(GMC)-VOPs */
836                    current->GMC_MV = GlobalMotionEst( pMBs, pParam, current->fcode );
837            else
838                    current->GMC_MV = zeroMV;
839    
840          return 0;          return 0;
841  }  }
842    
# Line 939  Line 894 
894  }  }
895    
896  static void  static void
897  SearchP(const uint8_t * const pRef,  SearchP(const IMAGE * const pRef,
898                  const uint8_t * const pRefH,                  const uint8_t * const pRefH,
899                  const uint8_t * const pRefV,                  const uint8_t * const pRefV,
900                  const uint8_t * const pRefHV,                  const uint8_t * const pRefHV,
                 const uint8_t * const pRefQ,  
901                  const IMAGE * const pCur,                  const IMAGE * const pCur,
902                  const int x,                  const int x,
903                  const int y,                  const int y,
# Line 960  Line 914 
914          int i, iDirection = 255, mask, threshA;          int i, iDirection = 255, mask, threshA;
915          VECTOR pmv[7];          VECTOR pmv[7];
916    
         Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);  
   
917          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);  //has to be changed to get_pmv(2)()          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);  //has to be changed to get_pmv(2)()
918          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,
919                                  pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);                  pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel);
   
         Data->predMV = pmv[0];  
920    
921          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
922          Data->Ref = pRef + (x + Data->iEdgedWidth*y)*16;          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;
923            Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;
924    
925            Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16;
926          Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;          Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;
927          Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;          Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;
928          Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;          Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;
929          Data->RefQ = pRefQ;          Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;
930            Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
931    
932          Data->lambda16 = lambda_vec16[iQuant];          Data->lambda16 = lambda_vec16[iQuant];
933            Data->lambda8 = lambda_vec8[iQuant];
934            Data->qpel_precision = 0;
935    
936          if (!(MotionFlags & PMV_HALFPEL16)) {          if (!(MotionFlags & PMV_HALFPEL16)) {
937                  Data->min_dx = EVEN(Data->min_dx);                  Data->min_dx = EVEN(Data->min_dx);
# Line 985  Line 941 
941    
942          if (pMB->dquant != NO_CHANGE) inter4v = 0;          if (pMB->dquant != NO_CHANGE) inter4v = 0;
943    
         if (inter4v) CheckCandidate = CheckCandidate16;  
         else CheckCandidate = CheckCandidate16no4v;  
   
944          for(i = 0;  i < 5; i++)          for(i = 0;  i < 5; i++)
945                  Data->currentMV[i].x = Data->currentMV[i].y = 0;                  Data->currentMV[i].x = Data->currentMV[i].y = 0;
946    
947            if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
948            else Data->predMV = pmv[0];
949    
950          i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode);          i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode);
951          Data->iMinSAD[0] = pMB->sad16 + lambda_vec16[iQuant] * i;          Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000;
952          Data->iMinSAD[1] = pMB->sad8[0] + lambda_vec8[iQuant] * i;          Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100;
953          Data->iMinSAD[2] = pMB->sad8[1];          Data->iMinSAD[2] = pMB->sad8[1];
954          Data->iMinSAD[3] = pMB->sad8[2];          Data->iMinSAD[3] = pMB->sad8[2];
955          Data->iMinSAD[4] = pMB->sad8[3];          Data->iMinSAD[4] = pMB->sad8[3];
# Line 1007  Line 963 
963          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
964                                          prevMBs + x + y * pParam->mb_width);                                          prevMBs + x + y * pParam->mb_width);
965    
966          if (inter4v) CheckCandidate = CheckCandidate16;          if (inter4v || Data->chroma) CheckCandidate = CheckCandidate16;
967          else CheckCandidate = CheckCandidate16no4v;          else CheckCandidate = CheckCandidate16no4v; //for extra speed
   
968    
969  /* main loop. checking all predictions */  /* main loop. checking all predictions */
970    
# Line 1044  Line 999 
999                          if (!(MVequal(startMV, backupMV))) {                          if (!(MVequal(startMV, backupMV))) {
1000                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1001    
1002                                  CheckCandidate16(startMV.x, startMV.y, 255, &iDirection, Data);                                  (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);
1003                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);
1004                                  if (bSAD < Data->iMinSAD[0]) {                                  if (bSAD < Data->iMinSAD[0]) {
1005                                          Data->currentMV[0] = backupMV;                                          Data->currentMV[0] = backupMV;
# Line 1057  Line 1012 
1012                          if (!(MVequal(startMV, backupMV))) {                          if (!(MVequal(startMV, backupMV))) {
1013                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1014    
1015                                  CheckCandidate16(startMV.x, startMV.y, 255, &iDirection, Data);                                  (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);
1016                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);
1017                                  if (bSAD < Data->iMinSAD[0]) {                                  if (bSAD < Data->iMinSAD[0]) {
1018                                          Data->currentMV[0] = backupMV;                                          Data->currentMV[0] = backupMV;
# Line 1066  Line 1021 
1021                  }                  }
1022          }          }
1023    
1024          if (MotionFlags & PMV_HALFPELREFINE16) HalfpelRefine(Data);          if (MotionFlags & PMV_HALFPELREFINE16) SubpelRefine(Data);
1025    
1026          for(i = 0; i < 5; i++) {          for(i = 0; i < 5; i++) {
1027                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
# Line 1075  Line 1030 
1030    
1031          if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {          if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {
1032    
1033                  if(inter4v)                  Data->qpel_precision = 1;
1034                          CheckCandidate = CheckCandidate16_qpel;                  get_range_qpel(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1035                  else                                  pParam->width, pParam->height, Data->iFcode);
                         CheckCandidate = CheckCandidate16no4v_qpel;  
   
                         Data->iMinSAD[0] -= lambda_vec16[iQuant] *  
                                 d_mv_bits(Data->predMV.x - Data->currentMV[0].x, Data->predMV.y - Data->currentMV[0].y, Data->iFcode);  
                         Data->iMinSAD[1] -= lambda_vec8[iQuant] *  
                                 d_mv_bits(Data->predMV.x - Data->currentMV[1].x, Data->predMV.y - Data->currentMV[1].y, Data->iFcode);  
   
                         Data->iMinSAD[0] += lambda_vec16[iQuant] *  
                                 d_mv_bits(Data->predQMV.x - Data->currentQMV[0].x, Data->predMV.y - Data->currentQMV[0].y, Data->iFcode);  
                         Data->iMinSAD[1] += lambda_vec8[iQuant] *  
                                 d_mv_bits(Data->predQMV.x - Data->currentQMV[1].x, Data->predMV.y - Data->currentQMV[1].y, Data->iFcode);  
1036    
1037                          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,                  SubpelRefine(Data);
                                 pParam->width, pParam->height, Data->iFcode, 0);  
   
                 QuarterpelRefine(Data);  
1038          }          }
1039    
1040            if (Data->iMinSAD[0] < (int32_t)iQuant * 30 ) inter4v = 0;
1041          if (inter4v) {          if (inter4v) {
1042                  SearchData Data8;                  SearchData Data8;
1043                  Data8.iFcode = Data->iFcode;                  Data8.iFcode = Data->iFcode;
1044                  Data8.lambda8 = lambda_vec8[iQuant];                  Data8.lambda8 = Data->lambda8;
1045                  Data8.iEdgedWidth = Data->iEdgedWidth;                  Data8.iEdgedWidth = Data->iEdgedWidth;
1046                    Data8.RefQ = Data->RefQ;
1047                    Data8.qpel = Data->qpel;
1048                  Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);                  Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
1049                  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);
1050                  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);
1051                  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);
1052    
1053                    if (Data->chroma) {
1054                            int sumx, sumy, dx, dy;
1055    
1056                            if(pParam->m_quarterpel) {
1057                                    sumx= pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2;
1058                                    sumy = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2;
1059                            } else {
1060                                    sumx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1061                                    sumy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1062                            }
1063                            dx = (sumx >> 3) + roundtab_76[sumx & 0xf];
1064                            dy = (sumy >> 3) + roundtab_76[sumy & 0xf];
1065    
1066                            Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);
1067                    }
1068          }          }
1069    
1070          if (!(inter4v) ||          if (!(inter4v) ||
# Line 1115  Line 1075 
1075                  pMB->mvs[0] = pMB->mvs[1]                  pMB->mvs[0] = pMB->mvs[1]
1076                          = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];                          = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
1077    
                 pMB->qmvs[0] = pMB->qmvs[1]  
                         = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];  
   
1078                  pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =                  pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =
1079                          pMB->sad8[2] = pMB->sad8[3] =  Data->iMinSAD[0];                          pMB->sad8[2] = pMB->sad8[3] =  Data->iMinSAD[0];
1080    
1081                  if(pParam->m_quarterpel) {                  if(pParam->m_quarterpel) {
1082                          pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x;                          pMB->qmvs[0] = pMB->qmvs[1]
1083                          pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y;                                  = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
1084                  }                          pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;
1085                  else {                          pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;
1086                    } else {
1087                          pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;                          pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1088                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
1089                  }                  }
# Line 1147  Line 1105 
1105                  const int block,                  const int block,
1106                  SearchData * const Data)                  SearchData * const Data)
1107  {  {
         Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);  
         Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);  
1108          Data->iMinSAD = OldData->iMinSAD + 1 + block;          Data->iMinSAD = OldData->iMinSAD + 1 + block;
1109          Data->currentMV = OldData->currentMV + 1 + block;          Data->currentMV = OldData->currentMV + 1 + block;
1110          Data->currentQMV = OldData->currentQMV + 1 + block;          Data->currentQMV = OldData->currentQMV + 1 + block;
1111    
1112          if(pParam->m_quarterpel) {          if(pParam->m_quarterpel) {
1113                  //it is qpel. substract d_mv_bits[qpel] from 0, add d_mv_bits[hpel] everywhere                  Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
1114                  if (block == 0)                  if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *
1115                          *(Data->iMinSAD) -= Data->lambda8 *                                                                          d_mv_bits(      Data->currentQMV->x - Data->predMV.x,
1116                                                                          d_mv_bits(      Data->currentQMV->x - Data->predQMV.x,                                                                                                  Data->currentQMV->y - Data->predMV.y,
1117                                                                                                  Data->currentQMV->y - Data->predQMV.y,                                                                                                  Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;
1118                                                                                                  Data->iFcode);          } else {
1119                    Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);
1120                  *(Data->iMinSAD) += Data->lambda8 *                  if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *
                                                                         d_mv_bits(      Data->currentMV->x - Data->predMV.x,  
                                                                                                 Data->currentMV->y - Data->predMV.y,  
                                                                                                 Data->iFcode);  
         } else //it is not qpel. add d_mv_bits[hpel] everywhere but not in 0 (it's already there)  
                 if (block != 0) *(Data->iMinSAD) += Data->lambda8 *  
1121                                                                          d_mv_bits(      Data->currentMV->x - Data->predMV.x,                                                                          d_mv_bits(      Data->currentMV->x - Data->predMV.x,
1122                                                                                                  Data->currentMV->y - Data->predMV.y,                                                                                                  Data->currentMV->y - Data->predMV.y,
1123                                                                                                  Data->iFcode);                                                                                                  Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;
1124            }
1125    
1126          if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) {          if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) {
1127    
# Line 1178  Line 1129 
1129                  Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1));                  Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1));
1130                  Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1));                  Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1));
1131                  Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1));                  Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1));
                 Data->RefQ = OldData->RefQ;  
1132    
1133                  Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1));                  Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1));
1134                    Data->qpel_precision = 0;
1135    
1136                  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,
1137                                  pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);                                  pParam->width, pParam->height, OldData->iFcode - pParam->m_quarterpel);
   
1138                  CheckCandidate = CheckCandidate8;                  CheckCandidate = CheckCandidate8;
1139    
1140                  if (MotionFlags & PMV_EXTSEARCH8) {                  if (MotionFlags & PMV_EXTSEARCH8) {
# Line 1206  Line 1156 
1156                  if (MotionFlags & PMV_HALFPELREFINE8) {                  if (MotionFlags & PMV_HALFPELREFINE8) {
1157                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1158    
1159                          HalfpelRefine(Data); // perform halfpel refine of current best vector                          SubpelRefine(Data); // perform halfpel refine of current best vector
1160    
1161                          if(*(Data->iMinSAD) < temp_sad) { // we have found a better match                          if(*(Data->iMinSAD) < temp_sad) { // we have found a better match
1162                                  Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector                                  Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
# Line 1217  Line 1167 
1167                  if(pParam->m_quarterpel) {                  if(pParam->m_quarterpel) {
1168                          if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) &&                          if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) &&
1169                                  (MotionFlags & PMV_QUARTERPELREFINE8)) {                                  (MotionFlags & PMV_QUARTERPELREFINE8)) {
1170                            Data->qpel_precision = 1;
1171                          CheckCandidate = CheckCandidate8_qpel;                          get_range_qpel(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,
1172                          Data->iMinSAD[0] -= Data->lambda8 *                                  pParam->width, pParam->height, OldData->iFcode);
1173                                          d_mv_bits(Data->predMV.x - Data->currentMV[0].x, Data->predMV.y - Data->currentMV[0].y, Data->iFcode);                          SubpelRefine(Data);
   
                         Data->iMinSAD[0] += Data->lambda8 *  
                                 d_mv_bits(Data->predQMV.x - Data->currentQMV[0].x, Data->predQMV.y - Data->currentQMV[0].y, Data->iFcode);  
   
                                 QuarterpelRefine(Data);  
1174                          }                          }
1175                  }                  }
1176          }          }
1177    
1178          if(pParam->m_quarterpel) {          if(pParam->m_quarterpel) {
1179                  pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x;                  pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x;
1180                  pMB->pmvs[block].y = Data->currentQMV->y - Data->predQMV.y;                  pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y;
1181                    pMB->qmvs[block] = *(Data->currentQMV);
1182          }          }
1183          else {          else {
1184                  pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;                  pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;
# Line 1240  Line 1186 
1186          }          }
1187    
1188          pMB->mvs[block] = *(Data->currentMV);          pMB->mvs[block] = *(Data->currentMV);
         pMB->qmvs[block] = *(Data->currentQMV);  
   
1189          pMB->sad8[block] =  4 * (*Data->iMinSAD);          pMB->sad8[block] =  4 * (*Data->iMinSAD);
1190  }  }
1191    
# Line 1319  Line 1263 
1263          MainSearchFunc *MainSearchPtr;          MainSearchFunc *MainSearchPtr;
1264          *Data->iMinSAD = MV_MAX_ERROR;          *Data->iMinSAD = MV_MAX_ERROR;
1265          Data->iFcode = iFcode;          Data->iFcode = iFcode;
1266            Data->qpel_precision = 0;
1267    
1268          Data->Ref = pRef + (x + y * iEdgedWidth) * 16;          Data->Ref = pRef + (x + y * iEdgedWidth) * 16;
1269          Data->RefH = pRefH + (x + y * iEdgedWidth) * 16;          Data->RefH = pRefH + (x + y * iEdgedWidth) * 16;
# Line 1328  Line 1273 
1273          Data->predMV = *predMV;          Data->predMV = *predMV;
1274    
1275          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,
1276                                  pParam->width, pParam->height, iFcode, pParam->m_quarterpel);                                  pParam->width, pParam->height, iFcode - pParam->m_quarterpel);
1277    
1278          pmv[0] = Data->predMV;          pmv[0] = Data->predMV;
1279            if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; }
1280          PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current);          PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current);
1281    
1282          Data->currentMV->x = Data->currentMV->y = 0;          Data->currentMV->x = Data->currentMV->y = 0;
   
1283          CheckCandidate = CheckCandidate16no4v;          CheckCandidate = CheckCandidate16no4v;
1284    
1285  // main loop. checking all predictions  // main loop. checking all predictions
# Line 1351  Line 1296 
1296    
1297          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1298    
1299          HalfpelRefine(Data);          SubpelRefine(Data);
1300    
1301            if (Data->qpel) {
1302                    Data->currentQMV->x = 2*Data->currentMV->x;
1303                    Data->currentQMV->y = 2*Data->currentMV->y;
1304                    Data->qpel_precision = 1;
1305                    get_range_qpel(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1306                                            pParam->width, pParam->height, iFcode);
1307                    SubpelRefine(Data);
1308            }
1309    
1310  // three bits are needed to code backward mode. four for forward  // three bits are needed to code backward mode. four for forward
1311  // we treat the bits just like they were vector's  // we treat the bits just like they were vector's
1312          if (mode_current == MODE_FORWARD) *Data->iMinSAD +=  4 * Data->lambda16 * 2;          if (mode_current == MODE_FORWARD) *Data->iMinSAD +=  4 * Data->lambda16;
1313          else *Data->iMinSAD +=  3 * Data->lambda16 * 2;          else *Data->iMinSAD +=  3 * Data->lambda16;
1314    
1315          if (*Data->iMinSAD < *best_sad) {          if (*Data->iMinSAD < *best_sad) {
1316                  *best_sad = *Data->iMinSAD;                  *best_sad = *Data->iMinSAD;
1317                  pMB->mode = mode_current;                  pMB->mode = mode_current;
1318                    if (Data->qpel) {
1319                            pMB->pmvs[0].x = Data->currentQMV->x - predMV->x;
1320                            pMB->pmvs[0].y = Data->currentQMV->y - predMV->y;
1321                            if (mode_current == MODE_FORWARD)
1322                                    pMB->qmvs[0] = *Data->currentQMV;
1323                            else
1324                                    pMB->b_qmvs[0] = *Data->currentQMV;
1325                    } else {
1326                  pMB->pmvs[0].x = Data->currentMV->x - predMV->x;                  pMB->pmvs[0].x = Data->currentMV->x - predMV->x;
1327                  pMB->pmvs[0].y = Data->currentMV->y - predMV->y;                  pMB->pmvs[0].y = Data->currentMV->y - predMV->y;
1328                  if (mode_current == MODE_FORWARD) pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV;                  }
1329                  else pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search                  if (mode_current == MODE_FORWARD)
1330                            pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV;
1331                    else
1332                            pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search
1333    
1334          }          }
1335    
1336  }  }
# Line 1395  Line 1361 
1361          MainSearchFunc *MainSearchPtr;          MainSearchFunc *MainSearchPtr;
1362    
1363          *Data->iMinSAD = 256*4096;          *Data->iMinSAD = 256*4096;
         Data->referencemv = b_mb->mvs;  
1364    
1365          Data->Ref = f_Ref->y + (x + Data->iEdgedWidth*y) * 16;          Data->Ref = f_Ref->y + (x + Data->iEdgedWidth*y) * 16;
1366          Data->RefH = f_RefH + (x + Data->iEdgedWidth*y) * 16;          Data->RefH = f_RefH + (x + Data->iEdgedWidth*y) * 16;
# Line 1410  Line 1375 
1375          Data->max_dy = 2 * pParam->height - 2 * (y) * 16;          Data->max_dy = 2 * pParam->height - 2 * (y) * 16;
1376          Data->min_dx = -(2 * 16 + 2 * (x) * 16);          Data->min_dx = -(2 * 16 + 2 * (x) * 16);
1377          Data->min_dy = -(2 * 16 + 2 * (y) * 16);          Data->min_dy = -(2 * 16 + 2 * (y) * 16);
1378            if (Data->qpel) { //we measure in qpixels
1379                    Data->max_dx *= 2;
1380                    Data->max_dy *= 2;
1381                    Data->min_dx *= 2;
1382                    Data->min_dy *= 2;
1383                    Data->referencemv = b_mb->qmvs;
1384            } else Data->referencemv = b_mb->mvs;
1385            Data->qpel_precision = 0; // it's a trick. it's 1 not 0, but we need 0 here
1386    
1387          for (k = 0; k < 4; k++) {          for (k = 0; k < 4; k++) {
1388                  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 1434  Line 1407 
1407                  }                  }
1408          }          }
1409    
1410          if (b_mb->mode == MODE_INTER4V)  
1411                  CheckCandidate = CheckCandidateDirect;          if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect;
1412          else CheckCandidate = CheckCandidateDirectno4v;          else CheckCandidate = CheckCandidateDirectno4v;
1413    
1414          (*CheckCandidate)(0, 0, 255, &k, Data);          (*CheckCandidate)(0, 0, 255, &k, Data);
1415    
1416  // skip decision  // skip decision
1417          if (*Data->iMinSAD - 2 * Data->lambda16 < (uint32_t)pMB->quant * SKIP_THRESH_B) {          if (*Data->iMinSAD < pMB->quant * SKIP_THRESH_B) {
1418                  //possible skip - checking chroma. everything copied from MC                  //possible skip - checking chroma. everything copied from MC
1419                  //this is not full chroma compensation, only it's fullpel approximation. should work though                  //this is not full chroma compensation, only it's fullpel approximation. should work though
1420                  int sum, dx, dy, b_dx, b_dy;                  int sum, dx, dy, b_dx, b_dy;
1421    
1422                    if (Data->qpel) {
1423                            sum = pMB->mvs[0].y/2 + pMB->mvs[1].y/2 + pMB->mvs[2].y/2 + pMB->mvs[3].y/2;
1424                            dy = (sum >> 3) + roundtab_76[sum & 0xf];
1425                            sum = pMB->mvs[0].x/2 + pMB->mvs[1].x/2 + pMB->mvs[2].x/2 + pMB->mvs[3].x/2;
1426                            dx = (sum >> 3) + roundtab_76[sum & 0xf];
1427    
1428                            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;
1429                            b_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1430                            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;
1431                            b_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1432    
1433                    } else {
1434                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1435                  dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));                  dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
   
1436                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1437                  dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));                  dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1438    
1439                  sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;                  sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1440                  b_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));                  b_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
   
1441                  sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;                  sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1442                  b_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));                  b_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
1443                    }
1444                  sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2),                  sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2),
1445                                          f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,                                          f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,
1446                                          b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,                                          b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,
# Line 1484  Line 1467 
1467    
1468          (*MainSearchPtr)(0, 0, Data, 255);          (*MainSearchPtr)(0, 0, Data, 255);
1469    
1470          HalfpelRefine(Data);          SubpelRefine(Data);
1471    
1472          *Data->iMinSAD +=  1 * Data->lambda16 * 2; // one bit is needed to code direct mode  //      *Data->iMinSAD +=  1 * Data->lambda16; // one bit is needed to code direct mode
1473          *best_sad = *Data->iMinSAD;          *best_sad = *Data->iMinSAD;
1474    
1475          if (b_mb->mode == MODE_INTER4V)          if (b_mb->mode == MODE_INTER4V)
# Line 1504  Line 1487 
1487                  pMB->b_mvs[k].y = ((Data->currentMV->y == 0)                  pMB->b_mvs[k].y = ((Data->currentMV->y == 0)
1488                                                          ? Data->directmvB[k].y                                                          ? Data->directmvB[k].y
1489                                                          : pMB->mvs[k].y - Data->referencemv[k].y);                                                          : pMB->mvs[k].y - Data->referencemv[k].y);
1490                    if (Data->qpel) {
1491                            pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2;
1492                            pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2;
1493                            pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2;
1494                            pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2;
1495                    }
1496    
1497                  if (b_mb->mode != MODE_INTER4V) {                  if (b_mb->mode != MODE_INTER4V) {
1498                          pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0];                          pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0];
1499                          pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0];                          pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0];
1500                            pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0];
1501                            pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0];
1502                          break;                          break;
1503                  }                  }
1504          }          }
# Line 1538  Line 1530 
1530  {  {
1531    
1532          const int32_t iEdgedWidth = pParam->edged_width;          const int32_t iEdgedWidth = pParam->edged_width;
   
1533          int iDirection, i, j;          int iDirection, i, j;
1534          SearchData bData;          SearchData bData;
1535    
1536          *(bData.iMinSAD = fData->iMinSAD) = 4096*256;          *(bData.iMinSAD = fData->iMinSAD) = 4096*256;
1537          bData.Cur = fData->Cur;          bData.Cur = fData->Cur;
1538          fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth;          fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth;
1539          bData.currentMV = fData->currentMV + 1;          bData.currentMV = fData->currentMV + 1; bData.currentQMV = fData->currentQMV + 1;
1540          bData.lambda16 = fData->lambda16;          bData.lambda16 = fData->lambda16;
1541          fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;          fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;
1542    
# Line 1557  Line 1548 
1548          bData.RefH = fData->bRefH = b_RefH + (x + y * iEdgedWidth) * 16;          bData.RefH = fData->bRefH = b_RefH + (x + y * iEdgedWidth) * 16;
1549          bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16;          bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16;
1550          bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16;          bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16;
1551            bData.RefQ = fData->RefQ;
1552            fData->qpel_precision = bData.qpel_precision = 0; bData.qpel = fData->qpel;
1553            bData.rounding = 0;
1554    
1555          bData.bpredMV = fData->predMV = *f_predMV;          bData.bpredMV = fData->predMV = *f_predMV;
1556          fData->bpredMV = bData.predMV = *b_predMV;          fData->bpredMV = bData.predMV = *b_predMV;
1557    
1558          fData->currentMV[0] = fData->currentMV[3]; //forward search stored it's vector here. backward stored it in the place it's needed          fData->currentMV[0] = fData->currentMV[2];
1559          get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, pParam->m_quarterpel);          get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode - pParam->m_quarterpel);
1560          get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, pParam->m_quarterpel);          get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode - pParam->m_quarterpel);
1561    
1562          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;
1563          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;
1564          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;
1565          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;
1566    
1567          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;
1568          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;
1569          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;
1570          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;
1571    
1572          CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);          CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);
1573    
# Line 1592  Line 1586 
1586                  // backward MV moves                  // backward MV moves
1587                  i = fData->currentMV[1].x; j = fData->currentMV[1].y;                  i = fData->currentMV[1].x; j = fData->currentMV[1].y;
1588                  fData->currentMV[2] = fData->currentMV[0];                  fData->currentMV[2] = fData->currentMV[0];
   
1589                  CheckCandidateInt(i + 1, j, 0, &iDirection, &bData);                  CheckCandidateInt(i + 1, j, 0, &iDirection, &bData);
1590                  CheckCandidateInt(i, j + 1, 0, &iDirection, &bData);                  CheckCandidateInt(i, j + 1, 0, &iDirection, &bData);
1591                  CheckCandidateInt(i - 1, j, 0, &iDirection, &bData);                  CheckCandidateInt(i - 1, j, 0, &iDirection, &bData);
# Line 1600  Line 1593 
1593    
1594          } while (!(iDirection));          } while (!(iDirection));
1595    
1596  // two bits are needed to code interpolate mode. we treat the bits just like they were vector's          if (fData->qpel) {
1597          *fData->iMinSAD +=  2 * fData->lambda16 * 2;                  CheckCandidate = CheckCandidateInt;
1598                    fData->qpel_precision = bData.qpel_precision = 1;
1599                    get_range_qpel(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode);
1600                    get_range_qpel(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode);
1601                    fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x;
1602                    fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y;
1603                    fData->currentQMV[1].x = 2 * fData->currentMV[1].x;
1604                    fData->currentQMV[1].y = 2 * fData->currentMV[1].y;
1605                    SubpelRefine(fData);
1606                    fData->currentQMV[2] = fData->currentQMV[0];
1607                    SubpelRefine(&bData);
1608            }
1609    
1610            *fData->iMinSAD +=  2 * fData->lambda16; // two bits are needed to code interpolate mode.
1611    
1612          if (*fData->iMinSAD < *best_sad) {          if (*fData->iMinSAD < *best_sad) {
1613                  *best_sad = *fData->iMinSAD;                  *best_sad = *fData->iMinSAD;
1614                  pMB->mvs[0] = fData->currentMV[0];                  pMB->mvs[0] = fData->currentMV[0];
1615                  pMB->b_mvs[0] = fData->currentMV[1];                  pMB->b_mvs[0] = fData->currentMV[1];
1616                  pMB->mode = MODE_INTERPOLATE;                  pMB->mode = MODE_INTERPOLATE;
1617                    if (fData->qpel) {
1618                            pMB->qmvs[0] = fData->currentQMV[0];
1619                            pMB->b_qmvs[0] = fData->currentQMV[1];
1620                            pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x;
1621                            pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y;
1622                            pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x;
1623                            pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y;
1624                    } else {
1625                  pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x;                  pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x;
1626                  pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y;                  pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y;
1627                  pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x;                  pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x;
1628                  pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y;                  pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y;
1629          }          }
1630  }  }
1631    }
1632    
1633  void  void
1634  MotionEstimationBVOP(MBParam * const pParam,  MotionEstimationBVOP(MBParam * const pParam,
# Line 1628  Line 1642 
1642                                           const IMAGE * const f_refV,                                           const IMAGE * const f_refV,
1643                                           const IMAGE * const f_refHV,                                           const IMAGE * const f_refHV,
1644                                           // backward (future) reference                                           // backward (future) reference
1645                                           const MACROBLOCK * const b_mbs,                                           const FRAMEINFO * const b_reference,
1646                                           const IMAGE * const b_ref,                                           const IMAGE * const b_ref,
1647                                           const IMAGE * const b_refH,                                           const IMAGE * const b_refH,
1648                                           const IMAGE * const b_refV,                                           const IMAGE * const b_refV,
# Line 1638  Line 1652 
1652          int32_t best_sad, skip_sad;          int32_t best_sad, skip_sad;
1653          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;
1654          static const VECTOR zeroMV={0,0};          static const VECTOR zeroMV={0,0};
1655            const MACROBLOCK * const b_mbs = b_reference->mbs;
1656    
1657          VECTOR f_predMV, b_predMV;      /* there is no prediction for direct mode*/          VECTOR f_predMV, b_predMV;      /* there is no prediction for direct mode*/
1658    
1659          const int32_t TRB = time_pp - time_bp;          const int32_t TRB = time_pp - time_bp;
1660          const int32_t TRD = time_pp;          const int32_t TRD = time_pp;
1661            uint8_t * qimage;
1662    
1663  // some pre-inintialized data for the rest of the search  // some pre-inintialized data for the rest of the search
1664    
1665          SearchData Data;          SearchData Data;
1666          int32_t iMinSAD;          int32_t iMinSAD;
1667          VECTOR currentMV[3];          VECTOR currentMV[3];
1668            VECTOR currentQMV[3];
1669          Data.iEdgedWidth = pParam->edged_width;          Data.iEdgedWidth = pParam->edged_width;
1670          Data.currentMV = currentMV;          Data.currentMV = currentMV; Data.currentQMV = currentQMV;
1671          Data.iMinSAD = &iMinSAD;          Data.iMinSAD = &iMinSAD;
1672          Data.lambda16 = lambda_vec16[frame->quant];          Data.lambda16 = lambda_vec16[frame->quant] + 2;
1673            Data.qpel = pParam->m_quarterpel;
1674            Data.rounding = 0;
1675    
1676          // note: i==horizontal, j==vertical          if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
1677                    return; // allocate some mem for qpel interpolated blocks
1678                                      // somehow this is dirty since I think we shouldn't use malloc outside
1679                                      // encoder_create() - so please fix me!
1680            Data.RefQ = qimage;
1681    
1682            // note: i==horizontal, j==vertical
1683          for (j = 0; j < pParam->mb_height; j++) {          for (j = 0; j < pParam->mb_height; j++) {
1684    
1685                  f_predMV = b_predMV = zeroMV;   /* prediction is reset at left boundary */                  f_predMV = b_predMV = zeroMV;   /* prediction is reset at left boundary */
# Line 1664  Line 1688 
1688                          MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;                          MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;
1689                          const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;                          const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;
1690    
1691  /* special case, if collocated block is SKIPed: encoding is forward (0,0), cpb=0 without further ado */  /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */
1692                            if (b_reference->coding_type != S_VOP)
1693                          if (b_mb->mode == MODE_NOT_CODED) {                          if (b_mb->mode == MODE_NOT_CODED) {
1694                                  pMB->mode = MODE_NOT_CODED;                                  pMB->mode = MODE_NOT_CODED;
1695                                  continue;                                  continue;
# Line 1672  Line 1697 
1697    
1698                          Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;                          Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;
1699                          pMB->quant = frame->quant;                          pMB->quant = frame->quant;
1700    
1701  /* direct search comes first, because it (1) checks for SKIP-mode  /* direct search comes first, because it (1) checks for SKIP-mode
1702          and (2) sets very good predictions for forward and backward search */          and (2) sets very good predictions for forward and backward search */
   
1703                          skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y,                          skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y,
1704                                                                          b_ref, b_refH->y, b_refV->y, b_refHV->y,                                                                          b_ref, b_refH->y, b_refV->y, b_refHV->y,
1705                                                                          &frame->image,                                                                          &frame->image,
# Line 1688  Line 1713 
1713    
1714                          if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }                          if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }
1715    
 //                      best_sad = 256*4096; //uncomment to disable Directsearch.  
 //      To disable any other mode, just comment the function call  
   
1716                          // forward search                          // forward search
1717                          SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,                          SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,
1718                                                  &frame->image, i, j,                                                  &frame->image, i, j,
# Line 1723  Line 1745 
1745                          switch (pMB->mode) {                          switch (pMB->mode) {
1746                                  case MODE_FORWARD:                                  case MODE_FORWARD:
1747                                          f_count++;                                          f_count++;
1748                                          f_predMV = pMB->mvs[0];                                          if (pParam->m_quarterpel) f_predMV = pMB->qmvs[0];
1749                                            else f_predMV = pMB->mvs[0];
1750                                          break;                                          break;
1751                                  case MODE_BACKWARD:                                  case MODE_BACKWARD:
1752                                          b_count++;                                          b_count++;
1753                                          b_predMV = pMB->b_mvs[0];                                          if (pParam->m_quarterpel) b_predMV = pMB->b_qmvs[0];
1754                                            else b_predMV = pMB->b_mvs[0];
1755                                          break;                                          break;
1756                                  case MODE_INTERPOLATE:                                  case MODE_INTERPOLATE:
1757                                          i_count++;                                          i_count++;
1758                                            if (pParam->m_quarterpel) {
1759                                                    f_predMV = pMB->qmvs[0];
1760                                                    b_predMV = pMB->b_qmvs[0];
1761                                            } else {
1762                                          f_predMV = pMB->mvs[0];                                          f_predMV = pMB->mvs[0];
1763                                          b_predMV = pMB->b_mvs[0];                                          b_predMV = pMB->b_mvs[0];
1764                                            }
1765                                          break;                                          break;
1766                                  case MODE_DIRECT:                                  case MODE_DIRECT:
1767                                  case MODE_DIRECT_NO4V:                                  case MODE_DIRECT_NO4V:
1768                                          d_count++;                                          d_count++;
                                         break;  
1769                                  default:                                  default:
1770                                          break;                                          break;
1771                          }                          }
1772                  }                  }
1773          }          }
1774            free(qimage);
 //      fprintf(debug,"B-Stat: F: %04d   B: %04d   I: %04d  D: %04d, N: %04d\n",  
 //                              f_count,b_count,i_count,d_count,n_count);  
   
1775  }  }
1776    
1777  /* Hinted ME starts here */  /* Hinted ME starts here */
1778    
1779  static void  static void
1780  Search8hinted(const SearchData * const OldData,  SearchPhinted ( const IMAGE * const pRef,
                         const int x, const int y,  
                         const uint32_t MotionFlags,  
                         const MBParam * const pParam,  
                         MACROBLOCK * const pMB,  
                         const MACROBLOCK * const pMBs,  
                         const int block,  
                         SearchData * const Data)  
 {  
         int32_t temp_sad;  
         MainSearchFunc *MainSearchPtr;  
         Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);  
         Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);  
         Data->iMinSAD = OldData->iMinSAD + 1 + block;  
         Data->currentMV = OldData->currentMV + 1 + block;  
         Data->currentQMV = OldData->currentQMV + 1 + block;  
   
         if(pParam->m_quarterpel) {  
                 //it is qpel. substract d_mv_bits[qpel] from 0, add d_mv_bits[hpel] everywhere  
                 if (block == 0)  
                         *(Data->iMinSAD) -= Data->lambda8 *  
                                                                         d_mv_bits(      Data->currentQMV->x - Data->predQMV.x,  
                                                                                                 Data->currentQMV->y - Data->predQMV.y,  
                                                                                                 Data->iFcode);  
   
                 *(Data->iMinSAD) += Data->lambda8 *  
                                                                         d_mv_bits(      Data->currentMV->x - Data->predMV.x,  
                                                                                                 Data->currentMV->y - Data->predMV.y,  
                                                                                                 Data->iFcode);  
         } else //it is not qpel. add d_mv_bits[hpel] everywhere but not in 0 (it's already there)  
                 if (block != 0) *(Data->iMinSAD) += Data->lambda8 *  
                                                                         d_mv_bits(      Data->currentMV->x - Data->predMV.x,  
                                                                                                 Data->currentMV->y - Data->predMV.y,  
                                                                                                 Data->iFcode);  
   
   
         Data->Ref = OldData->Ref + 8 * ((block&1) + pParam->edged_width*(block>>1));  
         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));  
         Data->RefQ = OldData->RefQ;  
   
         Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1));  
   
         get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,  
                                 pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);  
   
         CheckCandidate = CheckCandidate8;  
   
         temp_sad = *(Data->iMinSAD); // store current MinSAD  
   
         if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch;  
                 else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;  
                         else MainSearchPtr = DiamondSearch;  
   
         (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);  
   
         if(*(Data->iMinSAD) < temp_sad) {  
                 Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector  
                 Data->currentQMV->y = 2 * Data->currentMV->y;  
         }  
   
         if (MotionFlags & PMV_HALFPELREFINE8) {  
                 temp_sad = *(Data->iMinSAD); // store current MinSAD  
   
                 HalfpelRefine(Data); // perform halfpel refine of current best vector  
   
                 if(*(Data->iMinSAD) < temp_sad) { // we have found a better match  
                         Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector  
                         Data->currentQMV->y = 2 * Data->currentMV->y;  
                 }  
         }  
   
         if(pParam->m_quarterpel) {  
                 if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) &&  
                         (MotionFlags & PMV_QUARTERPELREFINE8)) {  
   
                         CheckCandidate = CheckCandidate8_qpel;  
                         Data->iMinSAD[0] -= Data->lambda8 *  
                                 d_mv_bits(Data->predMV.x - Data->currentMV[0].x, Data->predMV.y - Data->currentMV[0].y, Data->iFcode);  
   
                         Data->iMinSAD[0] += Data->lambda8 *  
                                 d_mv_bits(Data->predQMV.x - Data->currentQMV[0].x, Data->predQMV.y - Data->currentQMV[0].y, Data->iFcode);  
   
                                 QuarterpelRefine(Data);  
                 }  
         pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x;  
         pMB->pmvs[block].y = Data->currentQMV->y - Data->predQMV.y;  
         } else {  
                 pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;  
                 pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;  
         }  
   
         pMB->mvs[block] = *(Data->currentMV);  
         pMB->qmvs[block] = *(Data->currentQMV);  
   
         pMB->sad8[block] =  4 * (*Data->iMinSAD);  
 }  
   
   
 static void  
 SearchPhinted ( const uint8_t * const pRef,  
1781                                  const uint8_t * const pRefH,                                  const uint8_t * const pRefH,
1782                                  const uint8_t * const pRefV,                                  const uint8_t * const pRefV,
1783                                  const uint8_t * const pRefHV,                                  const uint8_t * const pRefHV,
# Line 1869  Line 1793 
1793                                  SearchData * const Data)                                  SearchData * const Data)
1794  {  {
1795    
         const int32_t iEdgedWidth = pParam->edged_width;  
   
1796          int i, t;          int i, t;
1797          MainSearchFunc * MainSearchPtr;          MainSearchFunc * MainSearchPtr;
1798    
         Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);  
1799          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,
1800                                  pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);                                  pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel);
1801    
1802          Data->Cur = pCur->y + (x + y * iEdgedWidth) * 16;          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
1803          Data->Ref = pRef + (x + iEdgedWidth*y)*16;          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;
1804          Data->RefH = pRefH + (x + iEdgedWidth*y) * 16;          Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;
1805          Data->RefV = pRefV + (x + iEdgedWidth*y) * 16;  
1806          Data->RefHV = pRefHV + (x + iEdgedWidth*y) * 16;          Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16;
1807          Data->lambda16 = lambda_vec16[iQuant];          Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;
1808            Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;
1809            Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;
1810            Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;
1811            Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
1812            Data->qpel_precision = 0;
1813    
1814          if (!(MotionFlags & PMV_HALFPEL16)) {          if (!(MotionFlags & PMV_HALFPEL16)) {
1815                  Data->min_dx = EVEN(Data->min_dx);                  Data->min_dx = EVEN(Data->min_dx);
# Line 1891  Line 1817 
1817                  Data->min_dy = EVEN(Data->min_dy);                  Data->min_dy = EVEN(Data->min_dy);
1818                  Data->max_dy = EVEN(Data->max_dy);                  Data->max_dy = EVEN(Data->max_dy);
1819          }          }
1820            if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1821            else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1822    
1823          for(i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;          for(i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;
1824    
1825          if (pMB->dquant != NO_CHANGE) inter4v = 0;          if (pMB->dquant != NO_CHANGE) inter4v = 0;
1826    
1827          if (inter4v)          if (inter4v || Data->chroma) CheckCandidate = CheckCandidate16;
                 CheckCandidate = CheckCandidate16;  
1828          else CheckCandidate = CheckCandidate16no4v;          else CheckCandidate = CheckCandidate16no4v;
1829    
   
1830          pMB->mvs[0].x = EVEN(pMB->mvs[0].x);          pMB->mvs[0].x = EVEN(pMB->mvs[0].x);
1831          pMB->mvs[0].y = EVEN(pMB->mvs[0].y);          pMB->mvs[0].y = EVEN(pMB->mvs[0].y);
1832          if (pMB->mvs[0].x > Data->max_dx) pMB->mvs[0].x = Data->max_dx; // this is in case iFcode changed          if (pMB->mvs[0].x > Data->max_dx) pMB->mvs[0].x = Data->max_dx; // this is in case iFcode changed
# Line 1926  Line 1852 
1852    
1853          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);
1854    
1855          if (MotionFlags & PMV_HALFPELREFINE16) HalfpelRefine(Data);          if (MotionFlags & PMV_HALFPELREFINE16) SubpelRefine(Data);
1856    
1857          for(i = 0; i < 5; i++) {          for(i = 0; i < 5; i++) {
1858                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
# Line 1934  Line 1860 
1860          }          }
1861    
1862          if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {          if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {
1863                    get_range_qpel(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1864                  if(inter4v)                                  pParam->width, pParam->height, Data->iFcode);
1865                          CheckCandidate = CheckCandidate16_qpel;                  Data->qpel_precision = 1;
1866                  else                  SubpelRefine(Data);
                         CheckCandidate = CheckCandidate16no4v_qpel;  
   
                 QuarterpelRefine(Data);  
1867          }          }
1868    
   
1869          if (inter4v) {          if (inter4v) {
1870                  SearchData Data8;                  SearchData Data8;
1871                  Data8.iFcode = Data->iFcode;                  Data8.iFcode = Data->iFcode;
1872                  Data8.lambda8 = lambda_vec8[pMB->quant];                  Data8.lambda8 = Data->lambda8;
1873                  Data8.iEdgedWidth = Data->iEdgedWidth;                  Data8.iEdgedWidth = Data->iEdgedWidth;
1874                  Search8hinted(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);                  Data8.RefQ = Data->RefQ;
1875                  Search8hinted(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);                  Data8.qpel = Data->qpel;
1876                  Search8hinted(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);                  Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
1877                  Search8hinted(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);                  Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);
1878                    Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
1879                    Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
1880    
1881                    if (Data->chroma) {
1882                            int sumx, sumy, dx, dy;
1883    
1884                            if(pParam->m_quarterpel) {
1885                                    sumx= pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2;
1886                                    sumy = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2;
1887                            } else {
1888                                    sumx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1889                                    sumy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1890                            }
1891                            dx = (sumx >> 3) + roundtab_76[sumx & 0xf];
1892                            dy = (sumy >> 3) + roundtab_76[sumy & 0xf];
1893    
1894                            Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);
1895                    }
1896          }          }
1897    
1898          if (!(inter4v) ||          if (!(inter4v) ||
1899                  (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] +                  (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] +
1900                                                          Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {                                                          Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {
1901  // INTER MODE  // INTER MODE
   
1902                  pMB->mode = MODE_INTER;                  pMB->mode = MODE_INTER;
1903                  pMB->mvs[0] = pMB->mvs[1]                  pMB->mvs[0] = pMB->mvs[1]
1904                          = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];                          = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
1905    
1906                    pMB->qmvs[0] = pMB->qmvs[1]
1907                            = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];
1908    
1909                  pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =                  pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =
1910                          pMB->sad8[2] = pMB->sad8[3] =  Data->iMinSAD[0];                          pMB->sad8[2] = pMB->sad8[3] =  Data->iMinSAD[0];
1911    
1912                    if(pParam->m_quarterpel) {
1913                            pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;
1914                            pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;
1915                    } else {
1916                  pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;                  pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1917                  pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;                  pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
1918                    }
1919          } else {          } else {
1920  // INTER4V MODE; all other things are already set in Search8hinted  // INTER4V MODE; all other things are already set in Search8
1921                  pMB->mode = MODE_INTER4V;                  pMB->mode = MODE_INTER4V;
1922                  pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3]                  pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3]
1923                                                  + Data->iMinSAD[4] + IMV16X16 * iQuant;                                                  + Data->iMinSAD[4] + IMV16X16 * iQuant;
# Line 1994  Line 1941 
1941          uint8_t * qimage;          uint8_t * qimage;
1942          int32_t temp[5], quant = current->quant;          int32_t temp[5], quant = current->quant;
1943          int32_t iMinSAD[5];          int32_t iMinSAD[5];
1944          VECTOR currentMV[5];          VECTOR currentMV[5], currentQMV[5];
1945          SearchData Data;          SearchData Data;
1946          Data.iEdgedWidth = pParam->edged_width;          Data.iEdgedWidth = pParam->edged_width;
1947          Data.currentMV = currentMV;          Data.currentMV = currentMV;
1948            Data.currentQMV = currentQMV;
1949          Data.iMinSAD = iMinSAD;          Data.iMinSAD = iMinSAD;
1950          Data.temp = temp;          Data.temp = temp;
1951          Data.iFcode = current->fcode;          Data.iFcode = current->fcode;
1952          Data.rounding = pParam->m_rounding_type;          Data.rounding = pParam->m_rounding_type;
1953            Data.qpel = pParam->m_quarterpel;
1954            Data.chroma = current->global_flags & XVID_ME_COLOUR;
1955    
1956          if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)          if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)
1957                  return; // allocate some mem for qpel interpolated blocks                  return; // allocate some mem for qpel interpolated blocks
# Line 2020  Line 1970 
1970  //intra mode is copied from the first pass. At least for the time being  //intra mode is copied from the first pass. At least for the time being
1971                          if  ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue;                          if  ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue;
1972    
   
1973                          if (!(current->global_flags & XVID_LUMIMASKING)) {                          if (!(current->global_flags & XVID_LUMIMASKING)) {
1974                                  pMB->dquant = NO_CHANGE;                                  pMB->dquant = NO_CHANGE;
1975                                  pMB->quant = current->quant; }                                  pMB->quant = current->quant; }
1976                          else                          else {
1977                                  if (pMB->dquant != NO_CHANGE) {                                  if (pMB->dquant != NO_CHANGE) {
1978                                          quant += DQtab[pMB->dquant];                                          quant += DQtab[pMB->dquant];
1979                                          if (quant > 31) quant = 31;                                          if (quant > 31) quant = 31;
1980                                          else if (quant < 1) quant = 1;                                          else if (quant < 1) quant = 1;
1981                                    }
1982                                          pMB->quant = quant;                                          pMB->quant = quant;
1983                                  }                                  }
1984    
1985                          SearchPhinted(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,                          SearchPhinted(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
1986                                                          y, current->motion_flags, pMB->quant,                                                          y, current->motion_flags, pMB->quant,
1987                                                          pParam, pMBs, current->global_flags & XVID_INTER4V, pMB,                                                          pParam, pMBs, current->global_flags & XVID_INTER4V, pMB,
1988                                                          &Data);                                                          &Data);
# Line 2053  Line 2003 
2003                                  SearchData * const Data)                                  SearchData * const Data)
2004  {  {
2005    
2006          int i, mask;          int i = 255, mask;
2007          VECTOR pmv[3];          VECTOR pmv[3];
   
2008          *(Data->iMinSAD) = MV_MAX_ERROR;          *(Data->iMinSAD) = MV_MAX_ERROR;
2009          Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);  
2010            //median is only used as prediction. it doesn't have to be real
2011            if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0;
2012            else
2013                    if (x == 1) //left macroblock does not have any vector now
2014                            Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median
2015                    else if (y == 1) // top macroblock don't have it's vector
2016                            Data->predMV = (pMB - 1)->mvs[0]; // left instead of median
2017                            else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median
2018    
2019          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,
2020                                  pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);                                  pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel);
2021    
2022          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;
2023          Data->Ref = pRef + (x + y * pParam->edged_width) * 16;          Data->Ref = pRef + (x + y * pParam->edged_width) * 16;
2024    
         CheckCandidate = CheckCandidate16no4vI;  
   
2025          pmv[1].x = EVEN(pMB->mvs[0].x);          pmv[1].x = EVEN(pMB->mvs[0].x);
2026          pmv[1].y = EVEN(pMB->mvs[0].y);          pmv[1].y = EVEN(pMB->mvs[0].y);
2027          pmv[0].x = EVEN(Data->predMV.x);          pmv[2].x = EVEN(Data->predMV.x);
2028          pmv[0].y = EVEN(Data->predMV.y);          pmv[2].y = EVEN(Data->predMV.y);
2029          pmv[2].x = pmv[2].y = 0;          pmv[0].x = pmv[0].y = 0;
2030    
2031            (*CheckCandidate)(0, 0, 255, &i, Data);
2032    
2033    //early skip for 0,0
2034            if (*Data->iMinSAD < MAX_SAD00_FOR_SKIP * 4) {
2035                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
2036                    pMB->mode = MODE_NOT_CODED;
2037                    return 0;
2038            }
2039    
         CheckCandidate16no4vI(pmv[0].x, pmv[0].y, 255, &i, Data);  
2040          if (!(mask = make_mask(pmv, 1)))          if (!(mask = make_mask(pmv, 1)))
2041                  CheckCandidate16no4vI(pmv[1].x, pmv[1].y, mask, &i, Data);                  (*CheckCandidate)(pmv[1].x, pmv[1].y, mask, &i, Data);
2042          if (!(mask = make_mask(pmv, 2)))          if (!(mask = make_mask(pmv, 2)))
2043                  CheckCandidate16no4vI(0, 0, mask, &i, Data);                  (*CheckCandidate)(pmv[2].x, pmv[2].y, mask, &i, Data);
2044    
2045          DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, mask);          if (*Data->iMinSAD > MAX_SAD00_FOR_SKIP * 4) // diamond only if needed
2046                    DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);
         pMB->mvs[0] = pMB->mvs[1]  
                         = pMB->mvs[2] = pMB->mvs[3] = *Data->currentMV; // all, for future get_pmv()  
2047    
2048            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
2049            pMB->mode = MODE_INTER;
2050          return *(Data->iMinSAD);          return *(Data->iMinSAD);
2051  }  }
2052    
2053  #define INTRA_THRESH    1350  #define INTRA_THRESH    1350
2054  #define INTER_THRESH    900  #define INTER_THRESH    1200
2055    
2056    
2057  int  int
2058  MEanalysis(     const IMAGE * const pRef,  MEanalysis(     const IMAGE * const pRef,
2059                          const IMAGE * const pCurrent,                          FRAMEINFO * const Current,
2060                          MBParam * const pParam,                          MBParam * const pParam,
2061                          MACROBLOCK * const pMBs,                          int maxIntra, //maximum number if non-I frames
2062                          const uint32_t iFcode)                          int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame
2063                            int bCount) // number if B frames in a row
2064  {  {
2065            int mb_width = pParam->mb_width;
2066            int mb_height = pParam->mb_height;
2067    
2068          uint32_t x, y, intra = 0;          uint32_t x, y, intra = 0;
2069          int sSAD = 0;          int sSAD = 0;
2070            MACROBLOCK * const pMBs = Current->mbs;
2071            const IMAGE * const pCurrent = &Current->image;
2072            int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH;
2073    
2074          VECTOR currentMV;          VECTOR currentMV;
2075          int32_t iMinSAD;          int32_t iMinSAD;
# Line 2105  Line 2077 
2077          Data.iEdgedWidth = pParam->edged_width;          Data.iEdgedWidth = pParam->edged_width;
2078          Data.currentMV = &currentMV;          Data.currentMV = &currentMV;
2079          Data.iMinSAD = &iMinSAD;          Data.iMinSAD = &iMinSAD;
2080          Data.iFcode = iFcode;          Data.iFcode = Current->fcode;
2081            CheckCandidate = CheckCandidate16no4vI;
2082    
2083            if ((Current->global_flags & XVID_REDUCED))
2084            {
2085                    mb_width = (pParam->width + 31) / 32;
2086                    mb_height = (pParam->height + 31) / 32;
2087            }
2088    
2089    
2090            if (intraCount < 10) // we're right after an I frame
2091                    IntraThresh += 4 * (intraCount - 10) * (intraCount - 10);
2092            else
2093                    if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec
2094                            IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra;
2095    
2096    
2097            InterThresh += 400 * (1 - bCount);
2098            if (InterThresh < 200) InterThresh = 200;
2099    
2100          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();
2101    
2102          for (y = 1; y < pParam->mb_height-1; y++) {          for (y = 1; y < mb_height-1; y++) {
2103                  for (x = 1; x < pParam->mb_width-1; x++) {                  for (x = 1; x < mb_width-1; x++) {
2104                          int sad, dev;                          int sad, dev;
2105                          MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];                          MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
2106    
2107                          sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y,                          sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y,
2108                                                                  pParam, pMBs, pMB, &Data);                                                                  pParam, pMBs, pMB, &Data);
2109    
2110                          if (sad > INTRA_THRESH) {                          if (sad > IntraThresh) {
2111                                  dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,                                  dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,
2112                                                            pParam->edged_width);                                                            pParam->edged_width);
2113                                  if (dev + INTRA_THRESH < sad) intra++;                                  if (dev + IntraThresh < sad) {
2114                                  if (intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2;  // I frame                                          pMB->mode = MODE_INTRA;
2115                                            if (++intra > (mb_height-2)*(mb_width-2)/2) return 2;  // I frame
2116                                    }
2117                                  }                                  }
2118                          sSAD += sad;                          sSAD += sad;
2119                  }                  }
2120          }          }
2121          sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2);          sSAD /= (mb_height-2)*(mb_width-2);
2122          if (sSAD > INTER_THRESH ) return 1; //P frame          if (sSAD > InterThresh ) return 1; //P frame
2123          emms();          emms();
2124          return 0; // B frame          return 0; // B frame
2125    
# Line 2137  Line 2129 
2129  FindFcode(      const MBParam * const pParam,  FindFcode(      const MBParam * const pParam,
2130                          const FRAMEINFO * const current)                          const FRAMEINFO * const current)
2131  {  {
2132            int mb_width = pParam->mb_width;
2133            int mb_height = pParam->mb_height;
2134    
2135          uint32_t x, y;          uint32_t x, y;
2136          int max = 0, min = 0, i;          int max = 0, min = 0, i;
2137    
2138          for (y = 0; y < pParam->mb_height; y++) {  
2139                  for (x = 0; x < pParam->mb_width; x++) {          if ((current->global_flags & XVID_REDUCED))
2140            {
2141                    mb_width = (pParam->width + 31) / 32;
2142                    mb_height = (pParam->height + 31) / 32;
2143            }
2144    
2145    
2146            for (y = 0; y < mb_height; y++) {
2147                    for (x = 0; x < mb_width; x++) {
2148    
2149                          MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];                          MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
2150                          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4:1); i++) {                          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4:1); i++) {
# Line 2157  Line 2160 
2160          min = -min;          min = -min;
2161          max += 1;          max += 1;
2162          if (min > max) max = min;          if (min > max) max = min;
2163            if (pParam->m_quarterpel) max *= 2;
2164    
2165          for (i = 1; (max > 32 << (i - 1)); i++);          for (i = 1; (max > 32 << (i - 1)); i++);
2166          return i;          return i;
2167  }  }
2168    
2169    static void
2170    CheckGMC(int x, int y, const int dir, int * iDirection,
2171                    const MACROBLOCK * const pMBs, uint32_t * bestcount, VECTOR * GMC,
2172                    const MBParam * const pParam)
2173    {
2174            uint32_t mx, my, a, count = 0;
2175    
2176            for (my = 1; my < pParam->mb_height-1; my++)
2177                    for (mx = 1; mx < pParam->mb_width-1; mx++) {
2178                            VECTOR mv;
2179                            const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];
2180                            if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) continue;
2181                            mv = pMB->mvs[0];
2182                            a = ABS(mv.x - x) + ABS(mv.y - y);
2183                            if (a < 6) count += 6 - a;
2184                    }
2185    
2186            if (count > *bestcount) {
2187                    *bestcount = count;
2188                    *iDirection = dir;
2189                    GMC->x = x; GMC->y = y;
2190            }
2191    }
2192    
2193    
2194    static VECTOR
2195    GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode)
2196    {
2197    
2198            uint32_t count, bestcount = 0;
2199            int x, y;
2200            VECTOR gmc = {0,0};
2201            int step, min_x, max_x, min_y, max_y;
2202            uint32_t mx, my;
2203            int iDirection, bDirection;
2204    
2205            min_x = min_y = -32<<iFcode;
2206            max_x = max_y = 32<<iFcode;
2207    
2208    //step1: let's find a rough camera panning
2209            for (step = 32; step >= 2; step /= 2) {
2210                    bestcount = 0;
2211                    for (y = min_y; y <= max_y; y += step)
2212                            for (x = min_x ; x <= max_x; x += step) {
2213                                    count = 0;
2214                                    //for all macroblocks
2215                                    for (my = 1; my < pParam->mb_height-1; my++)
2216                                            for (mx = 1; mx < pParam->mb_width-1; mx++) {
2217                                                    const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];
2218                                                    VECTOR mv;
2219    
2220                                                    if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED)
2221                                                            continue;
2222    
2223                                                    mv = pMB->mvs[0];
2224                                                    if ( ABS(mv.x - x) <= step && ABS(mv.y - y) <= step )   /* GMC translation is always halfpel-res */
2225                                                            count++;
2226                                            }
2227                                    if (count >= bestcount) { bestcount = count; gmc.x = x; gmc.y = y; }
2228                            }
2229                    min_x = gmc.x - step;
2230                    max_x = gmc.x + step;
2231                    min_y = gmc.y - step;
2232                    max_y = gmc.y + step;
2233    
2234            }
2235    
2236            if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10)
2237                    gmc.x = gmc.y = 0; //no camara pan, no GMC
2238    
2239    // step2: let's refine camera panning using gradiend-descent approach.
2240    // TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond)
2241            bestcount = 0;
2242            CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam);
2243            do {
2244                    x = gmc.x; y = gmc.y;
2245                    bDirection = iDirection; iDirection = 0;
2246                    if (bDirection & 1) CheckGMC(x - 1, y, 1+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);
2247                    if (bDirection & 2) CheckGMC(x + 1, y, 2+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);
2248                    if (bDirection & 4) CheckGMC(x, y - 1, 1+2+4, &iDirection, pMBs, &bestcount, &gmc, pParam);
2249                    if (bDirection & 8) CheckGMC(x, y + 1, 1+2+8, &iDirection, pMBs, &bestcount, &gmc, pParam);
2250    
2251            } while (iDirection);
2252    
2253            if (pParam->m_quarterpel) {
2254                    gmc.x *= 2;
2255                    gmc.y *= 2;     /* we store the halfpel value as pseudo-qpel to make comparison easier */
2256            }
2257    
2258            return gmc;
2259    }

Legend:
Removed from v.601  
changed lines
  Added in v.702

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