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

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

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

revision 1.58.2.20, Sat Jun 28 15:52:10 2003 UTC revision 1.72, Thu Jun 26 10:37:09 2003 UTC
# Line 1  Line 1 
1  /*****************************************************************************  /**************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  - Motion Estimation related code  -   *      motion estimation
5   *   *
6   *  Copyright(C) 2002 Christoph Lampert <gruel@web.de>   *      This program is an implementation of a part of one or more MPEG-4
7   *               2002 Michael Militzer <michael@xvid.org>   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
8   *               2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net>   *      to use this software module in hardware or software products are
9     *      advised that its use may infringe existing patents or copyrights, and
10     *      any such use would be at such party's own risk.  The original
11     *      developer of this software module and his/her company, and subsequent
12     *      editors and their companies, will have no liability for use of this
13     *      software or modifications or derivatives thereof.
14   *   *
15   *  This program is free software ; you can redistribute it and/or modify   *  This program is free software ; you can redistribute it and/or modify
16   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 19  Line 24 
24   *   *
25   *  You should have received a copy of the GNU General Public License   *  You should have received a copy of the GNU General Public License
26   *  along with this program ; if not, write to the Free Software   *  along with this program ; if not, write to the Free Software
27   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *  
  * $Id$  
28   *   *
29   ****************************************************************************/   *************************************************************************/
30    
31  #include <assert.h>  #include <assert.h>
32  #include <stdio.h>  #include <stdio.h>
33  #include <stdlib.h>  #include <stdlib.h>
34  #include <string.h>     /* memcpy */  #include <string.h>     // memcpy
35  #include <math.h>       /* lrint */  #include <math.h>       // lrint
36    
37  #include "../encoder.h"  #include "../encoder.h"
38  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
# Line 40  Line 43 
43  #include "motion_est.h"  #include "motion_est.h"
44  #include "motion.h"  #include "motion.h"
45  #include "sad.h"  #include "sad.h"
 #include "gmc.h"  
46  #include "../utils/emms.h"  #include "../utils/emms.h"
47  #include "../dct/fdct.h"  #include "../dct/fdct.h"
48    
# Line 166  Line 168 
168          const uint32_t stride = data->iEdgedWidth/2;          const uint32_t stride = data->iEdgedWidth/2;
169          int offset = (dx>>1) + (dy>>1)*stride;          int offset = (dx>>1) + (dy>>1)*stride;
170    
171          if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; /* it has been checked recently */          if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; //it has been checked recently
172          data->temp[5] = dx; data->temp[6] = dy; /* backup */          data->temp[5] = dx; data->temp[6] = dy; // backup
173    
174          switch (((dx & 1) << 1) | (dy & 1))     {          switch (((dx & 1) << 1) | (dy & 1))     {
175                  case 0:                  case 0:
# Line 190  Line 192 
192                          sad += sad8(data->CurV, data->RefQ, stride);                          sad += sad8(data->CurV, data->RefQ, stride);
193                          break;                          break;
194          }          }
195          data->temp[7] = sad; /* backup, part 2 */          data->temp[7] = sad; //backup, part 2
196          return sad;          return sad;
197  }  }
198    
199  static __inline const uint8_t *  static __inline const uint8_t *
200  GetReferenceB(const int x, const int y, const uint32_t dir, const SearchData * const data)  GetReferenceB(const int x, const int y, const uint32_t dir, const SearchData * const data)
201  {  {
202          /* dir : 0 = forward, 1 = backward */  //      dir : 0 = forward, 1 = backward
203          const uint8_t *const *const direction = ( dir == 0 ? data->RefP : data->b_RefP );          const uint8_t *const *const direction = ( dir == 0 ? data->RefP : data->b_RefP );
204          const int picture = ((x&1)<<1) | (y&1);          const int picture = ((x&1)<<1) | (y&1);
205          const int offset = (x>>1) + (y>>1)*data->iEdgedWidth;          const int offset = (x>>1) + (y>>1)*data->iEdgedWidth;
206          return direction[picture] + offset;          return direction[picture] + offset;
207  }  }
208    
209  /* this is a simpler copy of GetReferenceB, but as it's __inline anyway, we can keep the two separate */  // this is a simpler copy of GetReferenceB, but as it's __inline anyway, we can keep the two separate
210  static __inline const uint8_t *  static __inline const uint8_t *
211  GetReference(const int x, const int y, const SearchData * const data)  GetReference(const int x, const int y, const SearchData * const data)
212  {  {
# Line 216  Line 218 
218  static uint8_t *  static uint8_t *
219  Interpolate8x8qpel(const int x, const int y, const uint32_t block, const uint32_t dir, const SearchData * const data)  Interpolate8x8qpel(const int x, const int y, const uint32_t block, const uint32_t dir, const SearchData * const data)
220  {  {
221          /* create or find a qpel-precision reference picture; return pointer to it */  // create or find a qpel-precision reference picture; return pointer to it
222          uint8_t * Reference = data->RefQ + 16*dir;          uint8_t * Reference = data->RefQ + 16*dir;
223          const uint32_t iEdgedWidth = data->iEdgedWidth;          const uint32_t iEdgedWidth = data->iEdgedWidth;
224          const uint32_t rounding = data->rounding;          const uint32_t rounding = data->rounding;
# Line 227  Line 229 
229          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);
230          ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;          ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
231          switch( ((x&1)<<1) + (y&1) ) {          switch( ((x&1)<<1) + (y&1) ) {
232          case 3: /* x and y in qpel resolution - the "corners" (top left/right and */          case 3: // x and y in qpel resolution - the "corners" (top left/right and
233                          /* bottom left/right) during qpel refinement */                          // bottom left/right) during qpel refinement
234                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
235                  ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);                  ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
236                  ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);                  ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);
# Line 238  Line 240 
240                  interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);                  interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
241                  break;                  break;
242    
243          case 1: /* x halfpel, y qpel - top or bottom during qpel refinement */          case 1: // x halfpel, y qpel - top or bottom during qpel refinement
244                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
245                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
246                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
247                  break;                  break;
248    
249          case 2: /* x qpel, y halfpel - left or right during qpel refinement */          case 2: // x qpel, y halfpel - left or right during qpel refinement
250                  ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);                  ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
251                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;                  ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
252                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
253                  break;                  break;
254    
255          default: /* pure halfpel position */          default: // pure halfpel position
256                  return (uint8_t *) ref1;                  return (uint8_t *) ref1;
257    
258          }          }
# Line 260  Line 262 
262  static uint8_t *  static uint8_t *
263  Interpolate16x16qpel(const int x, const int y, const uint32_t dir, const SearchData * const data)  Interpolate16x16qpel(const int x, const int y, const uint32_t dir, const SearchData * const data)
264  {  {
265          /* create or find a qpel-precision reference picture; return pointer to it */  // create or find a qpel-precision reference picture; return pointer to it
266          uint8_t * Reference = data->RefQ + 16*dir;          uint8_t * Reference = data->RefQ + 16*dir;
267          const uint32_t iEdgedWidth = data->iEdgedWidth;          const uint32_t iEdgedWidth = data->iEdgedWidth;
268          const uint32_t rounding = data->rounding;          const uint32_t rounding = data->rounding;
# Line 270  Line 272 
272    
273          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);
274          switch( ((x&1)<<1) + (y&1) ) {          switch( ((x&1)<<1) + (y&1) ) {
275          case 3:          case 3: // x and y in qpel resolution - the "corners" (top left/right and
276                  /*                          // bottom left/right) during qpel refinement
                  * x and y in qpel resolution - the "corners" (top left/right and  
                  * bottom left/right) during qpel refinement  
                  */  
277                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
278                  ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);                  ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
279                  ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);                  ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);
# Line 284  Line 283 
283                  interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);                  interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);
284                  break;                  break;
285    
286          case 1: /* x halfpel, y qpel - top or bottom during qpel refinement */          case 1: // x halfpel, y qpel - top or bottom during qpel refinement
287                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
288                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
289                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
# Line 292  Line 291 
291                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);
292                  break;                  break;
293    
294          case 2: /* x qpel, y halfpel - left or right during qpel refinement */          case 2: // x qpel, y halfpel - left or right during qpel refinement
295                  ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);                  ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
296                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
297                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
# Line 300  Line 299 
299                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);
300                  break;                  break;
301    
302          default: /* pure halfpel position */          default: // pure halfpel position
303                  return (uint8_t *) ref1;                  return (uint8_t *) ref1;
304          }          }
305          return Reference;          return Reference;
# Line 323  Line 322 
322                  Reference = GetReference(x, y, data);                  Reference = GetReference(x, y, data);
323                  current = data->currentMV;                  current = data->currentMV;
324                  xc = x; yc = y;                  xc = x; yc = y;
325          } else { /* x and y are in 1/4 precision */          } else { // x and y are in 1/4 precision
326                  Reference = Interpolate16x16qpel(x, y, 0, data);                  Reference = Interpolate16x16qpel(x, y, 0, data);
327                  xc = x/2; yc = y/2; /* for chroma sad */                  xc = x/2; yc = y/2; //for chroma sad
328                  current = data->currentQMV;                  current = data->currentQMV;
329          }          }
330    
# Line 367  Line 366 
366          if (!data->qpel_precision) {          if (!data->qpel_precision) {
367                  Reference = GetReference(x, y, data);                  Reference = GetReference(x, y, data);
368                  current = data->currentMV;                  current = data->currentMV;
369          } else { /* x and y are in 1/4 precision */          } else { // x and y are in 1/4 precision
370                  Reference = Interpolate8x8qpel(x, y, 0, 0, data);                  Reference = Interpolate8x8qpel(x, y, 0, 0, data);
371                  current = data->currentQMV;                  current = data->currentQMV;
372          }          }
# Line 390  Line 389 
389          uint32_t t;          uint32_t t;
390          const uint8_t * Reference;          const uint8_t * Reference;
391    
392          if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || /* non-zero even value */          if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero even value
393                  (x > data->max_dx) || (x < data->min_dx)                  (x > data->max_dx) || (x < data->min_dx)
394                  || (y > data->max_dy) || (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
395    
# Line 428  Line 427 
427          if ( (x > data->max_dx) || ( x < data->min_dx)          if ( (x > data->max_dx) || ( x < data->min_dx)
428                  || (y > data->max_dy) || (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
429    
430          if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; /* non-zero even value */          if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; //non-zero even value
431    
432          if (data->qpel_precision) { /* x and y are in 1/4 precision */          if (data->qpel_precision) { // x and y are in 1/4 precision
433                  Reference = Interpolate16x16qpel(x, y, 0, data);                  Reference = Interpolate16x16qpel(x, y, 0, data);
434                  current = data->currentQMV;                  current = data->currentQMV;
435                  xc = x/2; yc = y/2;                  xc = x/2; yc = y/2;
# Line 456  Line 455 
455  }  }
456    
457  static void  static void
 CheckCandidate16I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  
 {  
         int sad;  
 //      int xc, yc;  
         const uint8_t * Reference;  
 //      VECTOR * current;  
   
         if ( (x > data->max_dx) || ( x < data->min_dx)  
                 || (y > data->max_dy) || (y < data->min_dy) ) return;  
   
         Reference = GetReference(x, y, data);  
 //      xc = x; yc = y;  
   
         sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);  
 //      sad += d_mv_bits(x, y, data->predMV, data->iFcode, 0, 0);  
   
 /*      if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],  
                                                                                 (yc >> 1) + roundtab_79[yc & 0x3], data);  
 */  
   
         if (sad < data->iMinSAD[0]) {  
                 data->iMinSAD[0] = sad;  
                 data->currentMV[0].x = x; data->currentMV[0].y = y;  
                 *dir = Direction;  
         }  
 }  
   
 static void  
458  CheckCandidate32I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidate32I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
459  {  {
460          /* maximum speed - for P/B/I decision */  // maximum speed - for P/B/I decision
461          int32_t sad;          int32_t sad;
462    
463          if ( (x > data->max_dx) || (x < data->min_dx)          if ( (x > data->max_dx) || (x < data->min_dx)
# Line 591  Line 562 
562                  } else {                  } else {
563                          xcf += mvs.x; ycf += mvs.y;                          xcf += mvs.x; ycf += mvs.y;
564                          xcb += b_mvs.x; ycb += b_mvs.y;                          xcb += b_mvs.x; ycb += b_mvs.y;
565                          mvs.x *= 2; mvs.y *= 2; /* we move to qpel precision anyway */                          mvs.x *= 2; mvs.y *= 2; //we move to qpel precision anyway
566                          b_mvs.x *= 2; b_mvs.y *= 2;                          b_mvs.x *= 2; b_mvs.y *= 2;
567                  }                  }
568    
# Line 687  Line 658 
658                  ptr = GetReference(x, y, data);                  ptr = GetReference(x, y, data);
659                  current = data->currentMV;                  current = data->currentMV;
660                  xc = x; yc = y;                  xc = x; yc = y;
661          } else { /* x and y are in 1/4 precision */          } else { // x and y are in 1/4 precision
662                  ptr = Interpolate16x16qpel(x, y, 0, data);                  ptr = Interpolate16x16qpel(x, y, 0, data);
663                  current = data->currentQMV;                  current = data->currentQMV;
664                  xc = x/2; yc = y/2;                  xc = x/2; yc = y/2;
# Line 701  Line 672 
672    
673          bits += t = BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);          bits += t = BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
674    
675            //8x8 blocks for inter4v mode
676          if (data->temp[0] + t < data->iMinSAD[1]) {          if (data->temp[0] + t < data->iMinSAD[1]) {
677                  data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; }                  data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; }
678          if (data->temp[1] < data->iMinSAD[2]) {          if (data->temp[1] < data->iMinSAD[2]) {
# Line 714  Line 686 
686    
687          if (bits >= data->iMinSAD[0]) return;          if (bits >= data->iMinSAD[0]) return;
688    
689          /* chroma */          //chroma
690          xc = (xc >> 1) + roundtab_79[xc & 0x3];          xc = (xc >> 1) + roundtab_79[xc & 0x3];
691          yc = (yc >> 1) + roundtab_79[yc & 0x3];          yc = (yc >> 1) + roundtab_79[yc & 0x3];
692    
693          /* chroma U */          //chroma U
694          ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[4], 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);          ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[4], 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);
695          transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2);          transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2);
696          bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4);          bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4);
697          if (bits >= data->iMinSAD[0]) return;          if (bits >= data->iMinSAD[0]) return;
698    
699          /* chroma V */          //chroma V
700          ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[5], 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);          ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[5], 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);
701          transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2);          transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2);
702          bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5);          bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5);
# Line 737  Line 709 
709                  *dir = Direction;                  *dir = Direction;
710          }          }
711  }  }
   
712  static void  static void
713  CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
714  {  {
# Line 754  Line 725 
725          if (!data->qpel_precision) {          if (!data->qpel_precision) {
726                  ptr = GetReference(x, y, data);                  ptr = GetReference(x, y, data);
727                  current = data->currentMV;                  current = data->currentMV;
728          } else { /* x and y are in 1/4 precision */          } else { // x and y are in 1/4 precision
729                  ptr = Interpolate8x8qpel(x, y, 0, 0, data);                  ptr = Interpolate8x8qpel(x, y, 0, 0, data);
730                  current = data->currentQMV;                  current = data->currentQMV;
731          }          }
# Line 783  Line 754 
754    
755          int iDirection;          int iDirection;
756    
757          for(;;) { /* forever */          for(;;) { //forever
758                  iDirection = 0;                  iDirection = 0;
759                  if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);                  if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);
760                  if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);                  if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);
# Line 792  Line 763 
763    
764                  /* now we're doing diagonal checks near our candidate */                  /* now we're doing diagonal checks near our candidate */
765    
766                  if (iDirection) {               /* if anything found */                  if (iDirection) {               //if anything found
767                          bDirection = iDirection;                          bDirection = iDirection;
768                          iDirection = 0;                          iDirection = 0;
769                          x = data->currentMV->x; y = data->currentMV->y;                          x = data->currentMV->x; y = data->currentMV->y;
770                          if (bDirection & 3) {   /* our candidate is left or right */                          if (bDirection & 3) {   //our candidate is left or right
771                                  CHECK_CANDIDATE(x, y + iDiamondSize, 8);                                  CHECK_CANDIDATE(x, y + iDiamondSize, 8);
772                                  CHECK_CANDIDATE(x, y - iDiamondSize, 4);                                  CHECK_CANDIDATE(x, y - iDiamondSize, 4);
773                          } else {                        /* what remains here is up or down */                          } else {                        // what remains here is up or down
774                                  CHECK_CANDIDATE(x + iDiamondSize, y, 2);                                  CHECK_CANDIDATE(x + iDiamondSize, y, 2);
775                                  CHECK_CANDIDATE(x - iDiamondSize, y, 1);                                  CHECK_CANDIDATE(x - iDiamondSize, y, 1);
776                          }                          }
# Line 808  Line 779 
779                                  bDirection += iDirection;                                  bDirection += iDirection;
780                                  x = data->currentMV->x; y = data->currentMV->y;                                  x = data->currentMV->x; y = data->currentMV->y;
781                          }                          }
782                  } else {                                /* about to quit, eh? not so fast.... */                  } else {                                //about to quit, eh? not so fast....
783                          switch (bDirection) {                          switch (bDirection) {
784                          case 2:                          case 2:
785                                  CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);                                  CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
# Line 846  Line 817 
817                                  CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);                                  CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
818                                  CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);                                  CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
819                                  break;                                  break;
820                          default:                /* 1+2+4+8 == we didn't find anything at all */                          default:                //1+2+4+8 == we didn't find anything at all
821                                  CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);                                  CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
822                                  CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);                                  CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
823                                  CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);                                  CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
824                                  CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);                                  CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
825                                  break;                                  break;
826                          }                          }
827                          if (!iDirection) break;         /* ok, the end. really */                          if (!iDirection) break;         //ok, the end. really
828                          bDirection = iDirection;                          bDirection = iDirection;
829                          x = data->currentMV->x; y = data->currentMV->y;                          x = data->currentMV->x; y = data->currentMV->y;
830                  }                  }
# Line 898  Line 869 
869    
870                  /* now we're doing diagonal checks near our candidate */                  /* now we're doing diagonal checks near our candidate */
871    
872                  if (iDirection) {               /* checking if anything found */                  if (iDirection) {               //checking if anything found
873                          bDirection = iDirection;                          bDirection = iDirection;
874                          iDirection = 0;                          iDirection = 0;
875                          x = data->currentMV->x; y = data->currentMV->y;                          x = data->currentMV->x; y = data->currentMV->y;
876                          if (bDirection & 3) {   /* our candidate is left or right */                          if (bDirection & 3) {   //our candidate is left or right
877                                  CHECK_CANDIDATE(x, y + iDiamondSize, 8);                                  CHECK_CANDIDATE(x, y + iDiamondSize, 8);
878                                  CHECK_CANDIDATE(x, y - iDiamondSize, 4);                                  CHECK_CANDIDATE(x, y - iDiamondSize, 4);
879                          } else {                        /* what remains here is up or down */                          } else {                        // what remains here is up or down
880                                  CHECK_CANDIDATE(x + iDiamondSize, y, 2);                                  CHECK_CANDIDATE(x + iDiamondSize, y, 2);
881                                  CHECK_CANDIDATE(x - iDiamondSize, y, 1);                                  CHECK_CANDIDATE(x - iDiamondSize, y, 1);
882                          }                          }
# Line 923  Line 894 
894  {  {
895  /* Do a half-pel or q-pel refinement */  /* Do a half-pel or q-pel refinement */
896          const VECTOR centerMV = data->qpel_precision ? *data->currentQMV : *data->currentMV;          const VECTOR centerMV = data->qpel_precision ? *data->currentQMV : *data->currentMV;
897          int iDirection; /* only needed because macro expects it */          int iDirection; //only needed because macro expects it
898    
899          CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, 0);          CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, 0);
900          CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, 0);          CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, 0);
# Line 978  Line 949 
949                          const int x, const int y,                          const int x, const int y,
950                          const MBParam * const pParam,                          const MBParam * const pParam,
951                          const uint32_t MotionFlags,                          const uint32_t MotionFlags,
952                          const uint32_t VopFlags,                          const uint32_t GlobalFlags,
                         const uint32_t VolFlags,  
953                          const IMAGE * const pCurrent,                          const IMAGE * const pCurrent,
954                          const IMAGE * const pRef)                          const IMAGE * const pRef)
955  {  {
956          int mode = MODE_INTER;          int mode = MODE_INTER;
957          int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);          int inter4v = (GlobalFlags & XVID_INTER4V) && (pMB->dquant == NO_CHANGE);
958          const uint32_t iQuant = pMB->quant;          const uint32_t iQuant = pMB->quant;
959    
960          const int skip_possible = (!(VolFlags & XVID_VOL_GMC)) && (pMB->dquant == 0);          const int skip_possible = (!(GlobalFlags & XVID_GMC)) && (pMB->dquant == NO_CHANGE);
961    
962          if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) { /* normal, fast, SAD-based mode decision */          if (!(GlobalFlags & XVID_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision
963                  int sad;                  int sad;
964                  int InterBias = MV16_INTER_BIAS;                  int InterBias = MV16_INTER_BIAS;
965                  if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +                  if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
# Line 1003  Line 973 
973                          Data->iMinSAD[0] = sad;                          Data->iMinSAD[0] = sad;
974                  }                  }
975    
976                  /* 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?"
977                  if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP))                  if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP))
978                          if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH)                          if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH)
979                                  if (Data->chroma || SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) {                                  if (Data->chroma || SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) {
# Line 1011  Line 981 
981                                          sad = 0;                                          sad = 0;
982                                  }                                  }
983    
984                  /* intra decision */                  // intra decision
985    
986                  if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */                  if (iQuant > 8) InterBias += 100 * (iQuant - 8); // to make high quants work
987                  if (y != 0)                  if (y != 0)
988                          if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;                          if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;
989                  if (x != 0)                  if (x != 0)
990                          if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;                          if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
991    
992                  if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? */                  if (Data->chroma) InterBias += 50; // dev8(chroma) ???
993                  if (Data->rrv) InterBias *= 4;                  if (Data->rrv) InterBias *= 4;
994    
995                  if (InterBias < pMB->sad16) {                  if (InterBias < pMB->sad16) {
# Line 1033  Line 1003 
1003                          if (deviation < (sad - InterBias)) mode = MODE_INTRA;                          if (deviation < (sad - InterBias)) mode = MODE_INTRA;
1004                  }                  }
1005    
1006          } else { /* BITS */          } else { // BITS
1007    
1008                  int bits, intra, i;                  int bits, intra, i;
1009                  VECTOR backup[5], *v;                  VECTOR backup[5], *v;
# Line 1047  Line 1017 
1017    
1018                  bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags);                  bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags);
1019                  if (bits == 0)                  if (bits == 0)
1020                          mode = MODE_INTER; /* quick stop */                          mode = MODE_INTER; // quick stop
1021                  else {                  else {
1022                          if (inter4v) {                          if (inter4v) {
1023                                  int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup);                                  int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup);
# Line 1081  Line 1051 
1051    
1052          } else if (mode == MODE_INTER4V)          } else if (mode == MODE_INTER4V)
1053                  pMB->sad16 = Data->iMinSAD[0];                  pMB->sad16 = Data->iMinSAD[0];
1054          else /* INTRA, NOT_CODED */          else // INTRA, NOT_CODED
1055                  SkipMacroblockP(pMB, 0);                  SkipMacroblockP(pMB, 0);
1056    
1057          pMB->mode = mode;          pMB->mode = mode;
# Line 1094  Line 1064 
1064                                   const IMAGE * const pRefH,                                   const IMAGE * const pRefH,
1065                                   const IMAGE * const pRefV,                                   const IMAGE * const pRefV,
1066                                   const IMAGE * const pRefHV,                                   const IMAGE * const pRefHV,
                                 const IMAGE * const pGMC,  
1067                                   const uint32_t iLimit)                                   const uint32_t iLimit)
1068  {  {
1069          MACROBLOCK *const pMBs = current->mbs;          MACROBLOCK *const pMBs = current->mbs;
# Line 1104  Line 1073 
1073          uint32_t mb_width = pParam->mb_width;          uint32_t mb_width = pParam->mb_width;
1074          uint32_t mb_height = pParam->mb_height;          uint32_t mb_height = pParam->mb_height;
1075          const uint32_t iEdgedWidth = pParam->edged_width;          const uint32_t iEdgedWidth = pParam->edged_width;
1076          const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags);          const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->global_flags);
1077    
1078          uint32_t x, y;          uint32_t x, y;
1079          uint32_t iIntra = 0;          uint32_t iIntra = 0;
1080          int32_t quant = current->quant, sad00;          int32_t quant = current->quant, sad00;
1081          int skip_thresh = INITIAL_SKIP_THRESH * \          int skip_thresh = INITIAL_SKIP_THRESH *
1082                  (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \                  (current->global_flags & XVID_REDUCED ? 4:1) *
1083                  (current->vop_flags & XVID_VOP_MODEDECISION_BITS ? 2:1);                  (current->global_flags & XVID_MODEDECISION_BITS ? 2:1);
1084    
1085          /* some pre-initialized thingies for SearchP */          // some pre-initialized thingies for SearchP
1086          int32_t temp[8];          int32_t temp[8];
1087          VECTOR currentMV[5];          VECTOR currentMV[5];
1088          VECTOR currentQMV[5];          VECTOR currentQMV[5];
# Line 1128  Line 1097 
1097          Data.temp = temp;          Data.temp = temp;
1098          Data.iFcode = current->fcode;          Data.iFcode = current->fcode;
1099          Data.rounding = pParam->m_rounding_type;          Data.rounding = pParam->m_rounding_type;
1100          Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);          Data.qpel = pParam->m_quarterpel;
1101          Data.chroma = MotionFlags & XVID_ME_CHROMA16;          Data.chroma = MotionFlags & PMV_CHROMA16;
1102          Data.rrv = (current->vop_flags & XVID_VOP_REDUCED) ? 1:0;          Data.rrv = current->global_flags & XVID_REDUCED;
1103          Data.dctSpace = dct_space;          Data.dctSpace = dct_space;
1104          Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);          Data.quant_type = pParam->m_quant_type;
1105    
1106          if ((current->vop_flags & XVID_VOP_REDUCED)) {          if ((current->global_flags & XVID_REDUCED)) {
1107                  mb_width = (pParam->width + 31) / 32;                  mb_width = (pParam->width + 31) / 32;
1108                  mb_height = (pParam->height + 31) / 32;                  mb_height = (pParam->height + 31) / 32;
1109                  Data.qpel = 0;                  Data.qpel = 0;
1110          }          }
1111    
1112          Data.RefQ = pRefV->u; /* a good place, also used in MC (for similar purpose) */          Data.RefQ = pRefV->u; // a good place, also used in MC (for similar purpose)
1113          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();
1114    
1115          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
# Line 1167  Line 1136 
1136    
1137                          sad00 = pMB->sad16;                          sad00 = pMB->sad16;
1138    
1139                          if (pMB->dquant != 0) {                          if (!(current->global_flags & XVID_LUMIMASKING))
1140                                    pMB->dquant = NO_CHANGE;
1141                            else {
1142                                    if (pMB->dquant != NO_CHANGE) {
1143                                  quant += DQtab[pMB->dquant];                                  quant += DQtab[pMB->dquant];
1144                                  if (quant > 31) quant = 31;                                  if (quant > 31) quant = 31;
1145                                  else if (quant < 1) quant = 1;                                  else if (quant < 1) quant = 1;
1146                          }                          }
1147                            }
1148                          pMB->quant = quant;                          pMB->quant = quant;
1149    
1150                          /* initial skip decision */  //initial skip decision
1151                          /* no early skip for GMC (global vector = skip vector is unknown!)  */                          /* no early skip for GMC (global vector = skip vector is unknown!)  */
1152                          if (!(current->vol_flags & XVID_VOL_GMC))       { /* no fast SKIP for S(GMC)-VOPs */                          if (!(current->global_flags & XVID_GMC))        { /* no fast SKIP for S(GMC)-VOPs */
1153                                  if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)                                  if (pMB->dquant == NO_CHANGE && sad00 < quant * skip_thresh)
1154                                          if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {                                          if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {
1155                                                  SkipMacroblockP(pMB, sad00);                                                  SkipMacroblockP(pMB, sad00);
1156                                                  continue;                                                  continue;
# Line 1185  Line 1158 
1158                          }                          }
1159    
1160                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
1161                                          y, MotionFlags, current->vop_flags, current->vol_flags,                                                  y, MotionFlags, current->global_flags,
1162                                          &Data, pParam, pMBs, reference->mbs, pMB);                                          &Data, pParam, pMBs, reference->mbs, pMB);
1163    
1164                          ModeDecision(&Data, pMB, pMBs, x, y, pParam,                          ModeDecision(&Data, pMB, pMBs, x, y, pParam,
1165                                                   MotionFlags, current->vop_flags, current->vol_flags,                                                          MotionFlags, current->global_flags,
1166                                                   pCurrent, pRef);                                                   pCurrent, pRef);
1167    
1168                          if (pMB->mode == MODE_INTRA)                          if (pMB->mode == MODE_INTRA)
# Line 1197  Line 1170 
1170                  }                  }
1171          }          }
1172    
1173  //      if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */          if (current->global_flags & XVID_GMC )  /* GMC only for S(GMC)-VOPs */
1174  //      {          {
1175  //              current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV);                  current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV);
1176  //      }          }
1177          return 0;          return 0;
1178  }  }
1179    
# Line 1210  Line 1183 
1183  {  {
1184          int mask = 255, j;          int mask = 255, j;
1185          for (j = 0; j < i; j++) {          for (j = 0; j < i; j++) {
1186                  if (MVequal(pmv[i], pmv[j])) return 0; /* same vector has been checked already */                  if (MVequal(pmv[i], pmv[j])) return 0; // same vector has been checked already
1187                  if (pmv[i].x == pmv[j].x) {                  if (pmv[i].x == pmv[j].x) {
1188                          if (pmv[i].y == pmv[j].y + iDiamondSize) mask &= ~4;                          if (pmv[i].y == pmv[j].y + iDiamondSize) mask &= ~4;
1189                          else if (pmv[i].y == pmv[j].y - iDiamondSize) mask &= ~8;                          else if (pmv[i].y == pmv[j].y - iDiamondSize) mask &= ~8;
# Line 1227  Line 1200 
1200  PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,  PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,
1201                          int iHcount, const MACROBLOCK * const prevMB, int rrv)                          int iHcount, const MACROBLOCK * const prevMB, int rrv)
1202  {  {
1203          /* this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself */  
1204    //this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself
1205          if (rrv) { iWcount /= 2; iHcount /= 2; }          if (rrv) { iWcount /= 2; iHcount /= 2; }
1206    
1207          if ( (y != 0) && (x < (iWcount-1)) ) {          /* [5] top-right neighbour */          if ( (y != 0) && (x < (iWcount-1)) ) {          // [5] top-right neighbour
1208                  pmv[5].x = EVEN(pmv[3].x);                  pmv[5].x = EVEN(pmv[3].x);
1209                  pmv[5].y = EVEN(pmv[3].y);                  pmv[5].y = EVEN(pmv[3].y);
1210          } else pmv[5].x = pmv[5].y = 0;          } else pmv[5].x = pmv[5].y = 0;
1211    
1212          if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }/* pmv[3] is left neighbour */          if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }// pmv[3] is left neighbour
1213          else pmv[3].x = pmv[3].y = 0;          else pmv[3].x = pmv[3].y = 0;
1214    
1215          if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }/* [4] top neighbour */          if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }// [4] top neighbour
1216          else pmv[4].x = pmv[4].y = 0;          else pmv[4].x = pmv[4].y = 0;
1217    
1218          /* [1] median prediction */          // [1] median prediction
1219          pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y);          pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y);
1220    
1221          pmv[0].x = pmv[0].y = 0; /* [0] is zero; not used in the loop (checked before) but needed here for make_mask */          pmv[0].x = pmv[0].y = 0; // [0] is zero; not used in the loop (checked before) but needed here for make_mask
1222    
1223          pmv[2].x = EVEN(prevMB->mvs[0].x); /* [2] is last frame */          pmv[2].x = EVEN(prevMB->mvs[0].x); // [2] is last frame
1224          pmv[2].y = EVEN(prevMB->mvs[0].y);          pmv[2].y = EVEN(prevMB->mvs[0].y);
1225    
1226          if ((x < iWcount-1) && (y < iHcount-1)) {          if ((x < iWcount-1) && (y < iHcount-1)) {
1227                  pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); /* [6] right-down neighbour in last frame */                  pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); //[6] right-down neighbour in last frame
1228                  pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);                  pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);
1229          } else pmv[6].x = pmv[6].y = 0;          } else pmv[6].x = pmv[6].y = 0;
1230    
# Line 1272  Line 1246 
1246                  const int x,                  const int x,
1247                  const int y,                  const int y,
1248                  const uint32_t MotionFlags,                  const uint32_t MotionFlags,
1249                  const uint32_t VopFlags,                  const uint32_t GlobalFlags,
                 const uint32_t VolFlags,  
1250                  SearchData * const Data,                  SearchData * const Data,
1251                  const MBParam * const pParam,                  const MBParam * const pParam,
1252                  const MACROBLOCK * const pMBs,                  const MACROBLOCK * const pMBs,
# Line 1283  Line 1256 
1256    
1257          int i, iDirection = 255, mask, threshA;          int i, iDirection = 255, mask, threshA;
1258          VECTOR pmv[7];          VECTOR pmv[7];
1259          int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);          int inter4v = (GlobalFlags & XVID_INTER4V) && (pMB->dquant == NO_CHANGE);
1260    
1261          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,
1262                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);
1263    
1264          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);
1265    
1266          Data->temp[5] = Data->temp[6] = 0; /* chroma-sad cache */          Data->temp[5] = Data->temp[6] = 0; // chroma-sad cache
1267          i = Data->rrv ? 2 : 1;          i = Data->rrv ? 2 : 1;
1268          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i;          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i;
1269          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;
# Line 1319  Line 1292 
1292          Data->iMinSAD[3] = pMB->sad8[2];          Data->iMinSAD[3] = pMB->sad8[2];
1293          Data->iMinSAD[4] = pMB->sad8[3];          Data->iMinSAD[4] = pMB->sad8[3];
1294    
1295          if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) && (x | y)) {          if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (x | y)) {
1296                  threshA = Data->temp[0]; /* that's where we keep this SAD atm */                  threshA = Data->temp[0]; // that's where we keep this SAD atm
1297                  if (threshA < 512) threshA = 512;                  if (threshA < 512) threshA = 512;
1298                  else if (threshA > 1024) threshA = 1024;                  else if (threshA > 1024) threshA = 1024;
1299          } else          } else
# Line 1331  Line 1304 
1304    
1305          if (!Data->rrv) {          if (!Data->rrv) {
1306                  if (inter4v | Data->chroma) CheckCandidate = CheckCandidate16;                  if (inter4v | Data->chroma) CheckCandidate = CheckCandidate16;
1307                          else CheckCandidate = CheckCandidate16no4v; /* for extra speed */                          else CheckCandidate = CheckCandidate16no4v; //for extra speed
1308          } else CheckCandidate = CheckCandidate32;          } else CheckCandidate = CheckCandidate32;
1309    
1310  /* main loop. checking all predictions (but first, which is 0,0 and has been checked in MotionEstimation())*/  /* main loop. checking all predictions (but first, which is 0,0 and has been checked in MotionEstimation())*/
# Line 1349  Line 1322 
1322          else {          else {
1323    
1324                  MainSearchFunc * MainSearchPtr;                  MainSearchFunc * MainSearchPtr;
1325                  if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;                  if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;
1326                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;                  else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1327                          else MainSearchPtr = DiamondSearch;                          else MainSearchPtr = DiamondSearch;
1328    
1329                  MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection);                  MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
# Line 1359  Line 1332 
1332          note that this search is/might be done in halfpel positions,          note that this search is/might be done in halfpel positions,
1333          which makes it more different than the diamond above */          which makes it more different than the diamond above */
1334    
1335                  if (MotionFlags & XVID_ME_EXTSEARCH16) {                  if (MotionFlags & PMV_EXTSEARCH16) {
1336                          int32_t bSAD;                          int32_t bSAD;
1337                          VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];                          VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];
1338                          if (Data->rrv) {                          if (Data->rrv) {
# Line 1390  Line 1363 
1363                  }                  }
1364          }          }
1365    
1366          if (MotionFlags & XVID_ME_HALFPELREFINE16)          if (MotionFlags & PMV_HALFPELREFINE16)
1367                          SubpelRefine(Data);                          SubpelRefine(Data);
1368    
1369          for(i = 0; i < 5; i++) {          for(i = 0; i < 5; i++) {
1370                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
1371                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
1372          }          }
1373    
# Line 1402  Line 1375 
1375                  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,
1376                                  pParam->width, pParam->height, Data->iFcode, 1, 0);                                  pParam->width, pParam->height, Data->iFcode, 1, 0);
1377                  Data->qpel_precision = 1;                  Data->qpel_precision = 1;
1378                  if (MotionFlags & XVID_ME_QUARTERPELREFINE16)                  if (MotionFlags & PMV_QUARTERPELREFINE16)
1379                          SubpelRefine(Data);                          SubpelRefine(Data);
1380          }          }
1381    
# Line 1411  Line 1384 
1384    
1385          if (inter4v) {          if (inter4v) {
1386                  SearchData Data8;                  SearchData Data8;
1387                  memcpy(&Data8, Data, sizeof(SearchData)); /* quick copy of common data */                  memcpy(&Data8, Data, sizeof(SearchData)); //quick copy of common data
1388    
1389                  Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);                  Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);
1390                  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);
1391                  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);
1392                  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);
1393    
1394                  if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_BITS))) {                  if ((Data->chroma) && (!(GlobalFlags & XVID_MODEDECISION_BITS))) {
1395                          /* chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, it will not be used */                          // chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, it will not be used
1396                          int sumx = 0, sumy = 0;                          int sumx = 0, sumy = 0;
1397    
1398                          if (Data->qpel)                          if (Data->qpel)
# Line 1466  Line 1439 
1439    
1440          *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;          *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;
1441    
1442          if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {          if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8|PMV_QUARTERPELREFINE8)) {
1443    
1444                  if (Data->rrv) i = 16; else i = 8;                  if (Data->rrv) i = 16; else i = 8;
1445    
# Line 1484  Line 1457 
1457                  if (!Data->rrv) CheckCandidate = CheckCandidate8;                  if (!Data->rrv) CheckCandidate = CheckCandidate8;
1458                  else CheckCandidate = CheckCandidate16no4v;                  else CheckCandidate = CheckCandidate16no4v;
1459    
1460                  if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_BITS))) {                  if (MotionFlags & PMV_EXTSEARCH8 && (!(MotionFlags & EXTSEARCH_BITS))) {
1461                          int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1462    
1463                          MainSearchFunc *MainSearchPtr;                          MainSearchFunc *MainSearchPtr;
1464                          if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = SquareSearch;                          if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch;
1465                                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;                                  else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;
1466                                          else MainSearchPtr = DiamondSearch;                                          else MainSearchPtr = DiamondSearch;
1467    
1468                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255);                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255);
1469    
1470                          if(*(Data->iMinSAD) < temp_sad) {                          if(*(Data->iMinSAD) < temp_sad) {
1471                                          Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */                                          Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
1472                                          Data->currentQMV->y = 2 * Data->currentMV->y;                                          Data->currentQMV->y = 2 * Data->currentMV->y;
1473                          }                          }
1474                  }                  }
1475    
1476                  if (MotionFlags & XVID_ME_HALFPELREFINE8) {                  if (MotionFlags & PMV_HALFPELREFINE8) {
1477                          int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1478    
1479                          SubpelRefine(Data); /* perform halfpel refine of current best vector */                          SubpelRefine(Data); // perform halfpel refine of current best vector
1480    
1481                          if(*(Data->iMinSAD) < temp_sad) { /* we have found a better match */                          if(*(Data->iMinSAD) < temp_sad) { // we have found a better match
1482                                  Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */                                  Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
1483                                  Data->currentQMV->y = 2 * Data->currentMV->y;                                  Data->currentQMV->y = 2 * Data->currentMV->y;
1484                          }                          }
1485                  }                  }
1486    
1487                  if (Data->qpel && MotionFlags & XVID_ME_QUARTERPELREFINE8) {                  if (Data->qpel && MotionFlags & PMV_QUARTERPELREFINE8) {
1488                                  Data->qpel_precision = 1;                                  Data->qpel_precision = 1;
1489                                  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,
1490                                          pParam->width, pParam->height, Data->iFcode, 1, 0);                                          pParam->width, pParam->height, Data->iFcode, 1, 0);
# Line 1553  Line 1526 
1526                                                          const uint32_t mode_curr)                                                          const uint32_t mode_curr)
1527  {  {
1528    
1529          /* [0] is prediction */          // [0] is prediction
1530          pmv[0].x = EVEN(pmv[0].x); pmv[0].y = EVEN(pmv[0].y);          pmv[0].x = EVEN(pmv[0].x); pmv[0].y = EVEN(pmv[0].y);
1531    
1532          pmv[1].x = pmv[1].y = 0; /* [1] is zero */          pmv[1].x = pmv[1].y = 0; // [1] is zero
1533    
1534          pmv[2] = ChoosePred(pMB, mode_curr);          pmv[2] = ChoosePred(pMB, mode_curr);
1535          pmv[2].x = EVEN(pmv[2].x); pmv[2].y = EVEN(pmv[2].y);          pmv[2].x = EVEN(pmv[2].x); pmv[2].y = EVEN(pmv[2].y);
1536    
1537          if ((y != 0)&&(x != (int)(iWcount+1))) {                        /* [3] top-right neighbour */          if ((y != 0)&&(x != (int)(iWcount+1))) {                        // [3] top-right neighbour
1538                  pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr);                  pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr);
1539                  pmv[3].x = EVEN(pmv[3].x); pmv[3].y = EVEN(pmv[3].y);                  pmv[3].x = EVEN(pmv[3].x); pmv[3].y = EVEN(pmv[3].y);
1540          } else pmv[3].x = pmv[3].y = 0;          } else pmv[3].x = pmv[3].y = 0;
# Line 1607  Line 1580 
1580          *Data->iMinSAD = MV_MAX_ERROR;          *Data->iMinSAD = MV_MAX_ERROR;
1581          Data->iFcode = iFcode;          Data->iFcode = iFcode;
1582          Data->qpel_precision = 0;          Data->qpel_precision = 0;
1583          Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; /* reset chroma-sad cache */          Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; // reset chroma-sad cache
1584    
1585          Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16;          Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16;
1586          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16;          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16;
# Line 1629  Line 1602 
1602          Data->currentMV->x = Data->currentMV->y = 0;          Data->currentMV->x = Data->currentMV->y = 0;
1603          CheckCandidate = CheckCandidate16no4v;          CheckCandidate = CheckCandidate16no4v;
1604    
1605          /* main loop. checking all predictions */  // main loop. checking all predictions
1606          for (i = 0; i < 7; i++) {          for (i = 0; i < 7; i++) {
1607                  if (!(mask = make_mask(pmv, i)) ) continue;                  if (!(mask = make_mask(pmv, i)) ) continue;
1608                  CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);                  CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1609          }          }
1610    
1611          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;          if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;
1612          else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;          else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1613                  else MainSearchPtr = DiamondSearch;                  else MainSearchPtr = DiamondSearch;
1614    
1615          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection);          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
# Line 1652  Line 1625 
1625                  SubpelRefine(Data);                  SubpelRefine(Data);
1626          }          }
1627    
1628          /* three bits are needed to code backward mode. four for forward */  // three bits are needed to code backward mode. four for forward
1629    
1630          if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16;          if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16;
1631          else *Data->iMinSAD += 3 * Data->lambda16;          else *Data->iMinSAD += 3 * Data->lambda16;
# Line 1676  Line 1649 
1649          }          }
1650    
1651          if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV;          if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV;
1652          else *(Data->currentMV+1) = *Data->currentMV; /* we store currmv for interpolate search */          else *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search
1653  }  }
1654    
1655  static void  static void
# Line 1692  Line 1665 
1665          const int div = 1 + Data->qpel;          const int div = 1 + Data->qpel;
1666          int k;          int k;
1667          const uint32_t stride = Data->iEdgedWidth/2;          const uint32_t stride = Data->iEdgedWidth/2;
1668          /* 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
1669    
1670          for (k = 0; k < 4; k++) {          for (k = 0; k < 4; k++) {
1671                  dy += Data->directmvF[k].y / div;                  dy += Data->directmvF[k].y / div;
# Line 1711  Line 1684 
1684                                          b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,                                          b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,
1685                                          stride);                                          stride);
1686    
1687          if (sum >= 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) return; /* no skip */          if (sum >= 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) return; //no skip
1688    
1689          sum += sad8bi(pCur->v + 8*x + 8 * y * stride,          sum += sad8bi(pCur->v + 8*x + 8 * y * stride,
1690                                          f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2,                                          f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2,
# Line 1719  Line 1692 
1692                                          stride);                                          stride);
1693    
1694          if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) {          if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) {
1695                  pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */                  pMB->mode = MODE_DIRECT_NONE_MV; //skipped
1696                  for (k = 0; k < 4; k++) {                  for (k = 0; k < 4; k++) {
1697                          pMB->qmvs[k] = pMB->mvs[k];                          pMB->qmvs[k] = pMB->mvs[k];
1698                          pMB->b_qmvs[k] = pMB->b_mvs[k];                          pMB->b_qmvs[k] = pMB->b_mvs[k];
# Line 1783  Line 1756 
1756                  if ( (pMB->b_mvs[k].x > Data->max_dx) | (pMB->b_mvs[k].x < Data->min_dx)                  if ( (pMB->b_mvs[k].x > Data->max_dx) | (pMB->b_mvs[k].x < Data->min_dx)
1757                          | (pMB->b_mvs[k].y > Data->max_dy) | (pMB->b_mvs[k].y < Data->min_dy) ) {                          | (pMB->b_mvs[k].y > Data->max_dy) | (pMB->b_mvs[k].y < Data->min_dy) ) {
1758    
1759                          *best_sad = 256*4096; /* in that case, we won't use direct mode */                          *best_sad = 256*4096; // in that case, we won't use direct mode
1760                          pMB->mode = MODE_DIRECT; /* just to make sure it doesn't say "MODE_DIRECT_NONE_MV" */                          pMB->mode = MODE_DIRECT; // just to make sure it doesn't say "MODE_DIRECT_NONE_MV"
1761                          pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0;                          pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0;
1762                          return 256*4096;                          return 256*4096;
1763                  }                  }
# Line 1801  Line 1774 
1774    
1775          CheckCandidate(0, 0, 255, &k, Data);          CheckCandidate(0, 0, 255, &k, Data);
1776    
1777          /* initial (fast) skip decision */  // initial (fast) skip decision
1778          if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (Data->chroma?3:2)) {          if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (Data->chroma?3:2)) {
1779                  /* possible skip */                  //possible skip
1780                  if (Data->chroma) {                  if (Data->chroma) {
1781                          pMB->mode = MODE_DIRECT_NONE_MV;                          pMB->mode = MODE_DIRECT_NONE_MV;
1782                          return *Data->iMinSAD; /* skip. */                          return *Data->iMinSAD; // skip.
1783                  } else {                  } else {
1784                          SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data);                          SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data);
1785                          if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; /* skip. */                          if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; // skip.
1786                  }                  }
1787          }          }
1788    
1789          *Data->iMinSAD += Data->lambda16;          *Data->iMinSAD += Data->lambda16;
1790          skip_sad = *Data->iMinSAD;          skip_sad = *Data->iMinSAD;
1791    
1792          /*  //      DIRECT MODE DELTA VECTOR SEARCH.
1793           * DIRECT MODE DELTA VECTOR SEARCH.  //      This has to be made more effective, but at the moment I'm happy it's running at all
          * This has to be made more effective, but at the moment I'm happy it's running at all  
          */  
1794    
1795          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;          if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;
1796                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;                  else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1797                          else MainSearchPtr = DiamondSearch;                          else MainSearchPtr = DiamondSearch;
1798    
1799          MainSearchPtr(0, 0, Data, 255);          MainSearchPtr(0, 0, Data, 255);
# Line 1832  Line 1803 
1803          *best_sad = *Data->iMinSAD;          *best_sad = *Data->iMinSAD;
1804    
1805          if (Data->qpel || b_mb->mode == MODE_INTER4V) pMB->mode = MODE_DIRECT;          if (Data->qpel || b_mb->mode == MODE_INTER4V) pMB->mode = MODE_DIRECT;
1806          else pMB->mode = MODE_DIRECT_NO4V; /* for faster compensation */          else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation
1807    
1808          pMB->pmvs[3] = *Data->currentMV;          pMB->pmvs[3] = *Data->currentMV;
1809    
# Line 1890  Line 1861 
1861          SearchData bData;          SearchData bData;
1862    
1863          fData->qpel_precision = 0;          fData->qpel_precision = 0;
1864          memcpy(&bData, fData, sizeof(SearchData)); /* quick copy of common data */          memcpy(&bData, fData, sizeof(SearchData)); //quick copy of common data
1865          *fData->iMinSAD = 4096*256;          *fData->iMinSAD = 4096*256;
1866          bData.currentMV++; bData.currentQMV++;          bData.currentMV++; bData.currentQMV++;
1867          fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;          fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;
# Line 1929  Line 1900 
1900    
1901          CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);          CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);
1902    
1903          /* diamond */  //diamond
1904          do {          do {
1905                  iDirection = 255;                  iDirection = 255;
1906                  /* forward MV moves */                  // forward MV moves
1907                  i = fData->currentMV[0].x; j = fData->currentMV[0].y;                  i = fData->currentMV[0].x; j = fData->currentMV[0].y;
1908    
1909                  CheckCandidateInt(i + 1, j, 0, &iDirection, fData);                  CheckCandidateInt(i + 1, j, 0, &iDirection, fData);
# Line 1940  Line 1911 
1911                  CheckCandidateInt(i - 1, j, 0, &iDirection, fData);                  CheckCandidateInt(i - 1, j, 0, &iDirection, fData);
1912                  CheckCandidateInt(i, j - 1, 0, &iDirection, fData);                  CheckCandidateInt(i, j - 1, 0, &iDirection, fData);
1913    
1914                  /* backward MV moves */                  // backward MV moves
1915                  i = fData->currentMV[1].x; j = fData->currentMV[1].y;                  i = fData->currentMV[1].x; j = fData->currentMV[1].y;
1916                  fData->currentMV[2] = fData->currentMV[0];                  fData->currentMV[2] = fData->currentMV[0];
1917                  CheckCandidateInt(i + 1, j, 0, &iDirection, &bData);                  CheckCandidateInt(i + 1, j, 0, &iDirection, &bData);
# Line 1950  Line 1921 
1921    
1922          } while (!(iDirection));          } while (!(iDirection));
1923    
1924          /* qpel refinement */  //qpel refinement
1925          if (fData->qpel) {          if (fData->qpel) {
1926                  if (*fData->iMinSAD > *best_sad + 500) return;                  if (*fData->iMinSAD > *best_sad + 500) return;
1927                  CheckCandidate = CheckCandidateInt;                  CheckCandidate = CheckCandidateInt;
# Line 1967  Line 1938 
1938                  SubpelRefine(&bData);                  SubpelRefine(&bData);
1939          }          }
1940    
1941          *fData->iMinSAD += (2+3) * fData->lambda16; /* two bits are needed to code interpolate mode. */          *fData->iMinSAD += (2+3) * fData->lambda16; // two bits are needed to code interpolate mode.
1942    
1943          if (*fData->iMinSAD < *best_sad) {          if (*fData->iMinSAD < *best_sad) {
1944                  *best_sad = *fData->iMinSAD;                  *best_sad = *fData->iMinSAD;
# Line 1995  Line 1966 
1966                                           FRAMEINFO * const frame,                                           FRAMEINFO * const frame,
1967                                           const int32_t time_bp,                                           const int32_t time_bp,
1968                                           const int32_t time_pp,                                           const int32_t time_pp,
1969                                           /* forward (past) reference */                                          // forward (past) reference
1970                                           const MACROBLOCK * const f_mbs,                                           const MACROBLOCK * const f_mbs,
1971                                           const IMAGE * const f_ref,                                           const IMAGE * const f_ref,
1972                                           const IMAGE * const f_refH,                                           const IMAGE * const f_refH,
1973                                           const IMAGE * const f_refV,                                           const IMAGE * const f_refV,
1974                                           const IMAGE * const f_refHV,                                           const IMAGE * const f_refHV,
1975                                           /* backward (future) reference */                                          // backward (future) reference
1976                                           const FRAMEINFO * const b_reference,                                           const FRAMEINFO * const b_reference,
1977                                           const IMAGE * const b_ref,                                           const IMAGE * const b_ref,
1978                                           const IMAGE * const b_refH,                                           const IMAGE * const b_refH,
# Line 2019  Line 1990 
1990          const int32_t TRB = time_pp - time_bp;          const int32_t TRB = time_pp - time_bp;
1991          const int32_t TRD = time_pp;          const int32_t TRD = time_pp;
1992    
1993          /* some pre-inintialized data for the rest of the search */  // some pre-inintialized data for the rest of the search
1994    
1995          SearchData Data;          SearchData Data;
1996          int32_t iMinSAD;          int32_t iMinSAD;
# Line 2031  Line 2002 
2002          Data.currentMV = currentMV; Data.currentQMV = currentQMV;          Data.currentMV = currentMV; Data.currentQMV = currentQMV;
2003          Data.iMinSAD = &iMinSAD;          Data.iMinSAD = &iMinSAD;
2004          Data.lambda16 = lambda_vec16[frame->quant];          Data.lambda16 = lambda_vec16[frame->quant];
2005          Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL;          Data.qpel = pParam->m_quarterpel;
2006          Data.rounding = 0;          Data.rounding = 0;
2007          Data.chroma = frame->motion_flags & XVID_ME_CHROMA8;          Data.chroma = frame->motion_flags & PMV_CHROMA8;
2008          Data.temp = temp;          Data.temp = temp;
2009    
2010          Data.RefQ = f_refV->u; /* a good place, also used in MC (for similar purpose) */          Data.RefQ = f_refV->u; // a good place, also used in MC (for similar purpose)
2011            // note: i==horizontal, j==vertical
         /* note: i==horizontal, j==vertical */  
2012          for (j = 0; j < pParam->mb_height; j++) {          for (j = 0; j < pParam->mb_height; j++) {
2013    
2014                  f_predMV = b_predMV = zeroMV;   /* prediction is reset at left boundary */                  f_predMV = b_predMV = zeroMV;   /* prediction is reset at left boundary */
# Line 2074  Line 2044 
2044    
2045                          if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }                          if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }
2046    
2047                          /* forward search */                          // forward search
2048                          SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y,                          SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y,
2049                                                  &frame->image, i, j,                                                  &frame->image, i, j,
2050                                                  frame->motion_flags,                                                  frame->motion_flags,
# Line 2082  Line 2052 
2052                                                  pMB, &f_predMV, &best_sad,                                                  pMB, &f_predMV, &best_sad,
2053                                                  MODE_FORWARD, &Data);                                                  MODE_FORWARD, &Data);
2054    
2055                          /* backward search */                          // backward search
2056                          SearchBF(b_ref, b_refH->y, b_refV->y, b_refHV->y,                          SearchBF(b_ref, b_refH->y, b_refV->y, b_refHV->y,
2057                                                  &frame->image, i, j,                                                  &frame->image, i, j,
2058                                                  frame->motion_flags,                                                  frame->motion_flags,
# Line 2090  Line 2060 
2060                                                  pMB, &b_predMV, &best_sad,                                                  pMB, &b_predMV, &best_sad,
2061                                                  MODE_BACKWARD, &Data);                                                  MODE_BACKWARD, &Data);
2062    
2063                          /* interpolate search comes last, because it uses data from forward and backward as prediction */                          // interpolate search comes last, because it uses data from forward and backward as prediction
2064                          SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y,                          SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y,
2065                                                  b_ref, b_refH->y, b_refV->y, b_refHV->y,                                                  b_ref, b_refH->y, b_refV->y, b_refHV->y,
2066                                                  &frame->image,                                                  &frame->image,
# Line 2102  Line 2072 
2072                                                  pMB, &best_sad,                                                  pMB, &best_sad,
2073                                                  &Data);                                                  &Data);
2074    
2075                          /* final skip decision */  // final skip decision
2076                          if ( (skip_sad < frame->quant * MAX_SAD00_FOR_SKIP * 2)                          if ( (skip_sad < frame->quant * MAX_SAD00_FOR_SKIP * 2)
2077                                          && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) )                                          && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) )
2078                                  SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data);                                  SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data);
# Line 2142  Line 2112 
2112  {  {
2113    
2114          int i, mask;          int i, mask;
         int quarterpel = (pParam->vol_flags & XVID_VOL_QUARTERPEL)? 1: 0;  
2115          VECTOR pmv[3];          VECTOR pmv[3];
2116          MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width];          MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width];
2117    
2118          for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;          for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;
2119    
2120          /* median is only used as prediction. it doesn't have to be real */          //median is only used as prediction. it doesn't have to be real
2121          if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0;          if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0;
2122          else          else
2123                  if (x == 1) /* left macroblock does not have any vector now */                  if (x == 1) //left macroblock does not have any vector now
2124                          Data->predMV = (pMB - pParam->mb_width)->mvs[0]; /* top instead of median */                          Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median
2125                  else if (y == 1) /* top macroblock doesn't have it's vector */                  else if (y == 1) // top macroblock doesn't have it's vector
2126                          Data->predMV = (pMB - 1)->mvs[0]; /* left instead of median */                          Data->predMV = (pMB - 1)->mvs[0]; // left instead of median
2127                          else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); /* else median */                          else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median
2128    
2129          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,
2130          pParam->width, pParam->height, Data->iFcode - quarterpel, 0, 0);                                  pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, 0);
2131    
2132          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;
2133          Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16;          Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16;
# Line 2178  Line 2147 
2147                  if (!(mask = make_mask(pmv, 2)))                  if (!(mask = make_mask(pmv, 2)))
2148                          CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data);                          CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data);
2149    
2150                  if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) /* diamond only if needed */                  if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) // diamond only if needed
2151                          DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);                          DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);
2152          }          }
2153    
# Line 2208  Line 2177 
2177          MACROBLOCK * const pMBs = Current->mbs;          MACROBLOCK * const pMBs = Current->mbs;
2178          const IMAGE * const pCurrent = &Current->image;          const IMAGE * const pCurrent = &Current->image;
2179          int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + b_thresh;          int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + b_thresh;
2180          int blocks = 0;          int s = 0, blocks = 0;
2181          int complexity = 0;          int complexity = 0;
2182    
2183          int32_t iMinSAD[5], temp[5];          int32_t iMinSAD[5], temp[5];
# Line 2281  Line 2250 
2250  }  }
2251    
2252    
2253  /* functions which perform BITS-based search/bitcount */  static WARPPOINTS
2254    GlobalMotionEst(const MACROBLOCK * const pMBs,
2255                                    const MBParam * const pParam,
2256                                    const FRAMEINFO * const current,
2257                                    const FRAMEINFO * const reference,
2258                                    const IMAGE * const pRefH,
2259                                    const IMAGE * const pRefV,
2260                                    const IMAGE * const pRefHV      )
2261    {
2262    
2263            const int deltax=8;             // upper bound for difference between a MV and it's neighbour MVs
2264            const int deltay=8;
2265            const int grad=512;             // lower bound for deviation in MB
2266    
2267            WARPPOINTS gmc;
2268    
2269            uint32_t mx, my;
2270    
2271            int MBh = pParam->mb_height;
2272            int MBw = pParam->mb_width;
2273    
2274            int *MBmask= calloc(MBh*MBw,sizeof(int));
2275            double DtimesF[4] = { 0.,0., 0., 0. };
2276            double sol[4] = { 0., 0., 0., 0. };
2277            double a,b,c,n,denom;
2278            double meanx,meany;
2279            int num,oldnum;
2280    
2281            if (!MBmask) {  fprintf(stderr,"Mem error\n");
2282                                            gmc.duv[0].x= gmc.duv[0].y =
2283                                                    gmc.duv[1].x= gmc.duv[1].y =
2284                                                    gmc.duv[2].x= gmc.duv[2].y = 0;
2285                                            return gmc; }
2286    
2287    // filter mask of all blocks
2288    
2289            for (my = 1; my < (uint32_t)MBh-1; my++)
2290            for (mx = 1; mx < (uint32_t)MBw-1; mx++)
2291            {
2292                    const int mbnum = mx + my * MBw;
2293                    const MACROBLOCK *pMB = &pMBs[mbnum];
2294                    const VECTOR mv = pMB->mvs[0];
2295    
2296                    if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED)
2297                            continue;
2298    
2299                    if ( ( (abs(mv.x -   (pMB-1)->mvs[0].x) < deltax) && (abs(mv.y -   (pMB-1)->mvs[0].y) < deltay) )
2300                    &&   ( (abs(mv.x -   (pMB+1)->mvs[0].x) < deltax) && (abs(mv.y -   (pMB+1)->mvs[0].y) < deltay) )
2301                    &&   ( (abs(mv.x - (pMB-MBw)->mvs[0].x) < deltax) && (abs(mv.y - (pMB-MBw)->mvs[0].y) < deltay) )
2302                    &&   ( (abs(mv.x - (pMB+MBw)->mvs[0].x) < deltax) && (abs(mv.y - (pMB+MBw)->mvs[0].y) < deltay) ) )
2303                            MBmask[mbnum]=1;
2304            }
2305    
2306            for (my = 1; my < (uint32_t)MBh-1; my++)
2307            for (mx = 1; mx < (uint32_t)MBw-1; mx++)
2308            {
2309                    const uint8_t *const pCur = current->image.y + 16*my*pParam->edged_width + 16*mx;
2310    
2311                    const int mbnum = mx + my * MBw;
2312                    if (!MBmask[mbnum])
2313                            continue;
2314    
2315                    if (sad16 ( pCur, pCur+1 , pParam->edged_width, 65536) <= (uint32_t)grad )
2316                            MBmask[mbnum] = 0;
2317                    if (sad16 ( pCur, pCur+pParam->edged_width, pParam->edged_width, 65536) <= (uint32_t)grad )
2318                            MBmask[mbnum] = 0;
2319    
2320            }
2321    
2322            emms();
2323    
2324            do {            /* until convergence */
2325    
2326            a = b = c = n = 0;
2327            DtimesF[0] = DtimesF[1] = DtimesF[2] = DtimesF[3] = 0.;
2328            for (my = 0; my < (uint32_t)MBh; my++)
2329                    for (mx = 0; mx < (uint32_t)MBw; mx++)
2330                    {
2331                            const int mbnum = mx + my * MBw;
2332                            const MACROBLOCK *pMB = &pMBs[mbnum];
2333                            const VECTOR mv = pMB->mvs[0];
2334    
2335                            if (!MBmask[mbnum])
2336                                    continue;
2337    
2338                            n++;
2339                            a += 16*mx+8;
2340                            b += 16*my+8;
2341                            c += (16*mx+8)*(16*mx+8)+(16*my+8)*(16*my+8);
2342    
2343                            DtimesF[0] += (double)mv.x;
2344                            DtimesF[1] += (double)mv.x*(16*mx+8) + (double)mv.y*(16*my+8);
2345                            DtimesF[2] += (double)mv.x*(16*my+8) - (double)mv.y*(16*mx+8);
2346                            DtimesF[3] += (double)mv.y;
2347                    }
2348    
2349            denom = a*a+b*b-c*n;
2350    
2351    /* Solve the system:    sol = (D'*E*D)^{-1} D'*E*F   */
2352    /* D'*E*F has been calculated in the same loop as matrix */
2353    
2354            sol[0] = -c*DtimesF[0] + a*DtimesF[1] + b*DtimesF[2];
2355            sol[1] =  a*DtimesF[0] - n*DtimesF[1]                           + b*DtimesF[3];
2356            sol[2] =  b*DtimesF[0]                          - n*DtimesF[2] - a*DtimesF[3];
2357            sol[3] =                                 b*DtimesF[1] - a*DtimesF[2] - c*DtimesF[3];
2358    
2359            sol[0] /= denom;
2360            sol[1] /= denom;
2361            sol[2] /= denom;
2362            sol[3] /= denom;
2363    
2364            meanx = meany = 0.;
2365            oldnum = 0;
2366            for (my = 0; my < (uint32_t)MBh; my++)
2367                    for (mx = 0; mx < (uint32_t)MBw; mx++)
2368                    {
2369                            const int mbnum = mx + my * MBw;
2370                            const MACROBLOCK *pMB = &pMBs[mbnum];
2371                            const VECTOR mv = pMB->mvs[0];
2372    
2373                            if (!MBmask[mbnum])
2374                                    continue;
2375    
2376                            oldnum++;
2377                            meanx += fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x );
2378                            meany += fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y );
2379                    }
2380    
2381            if (4*meanx > oldnum)   /* better fit than 0.25 is useless */
2382                    meanx /= oldnum;
2383            else
2384                    meanx = 0.25;
2385    
2386            if (4*meany > oldnum)
2387                    meany /= oldnum;
2388            else
2389                    meany = 0.25;
2390    
2391    /*      fprintf(stderr,"sol = (%8.5f, %8.5f, %8.5f, %8.5f)\n",sol[0],sol[1],sol[2],sol[3]);
2392            fprintf(stderr,"meanx = %8.5f  meany = %8.5f   %d\n",meanx,meany, oldnum);
2393    */
2394            num = 0;
2395            for (my = 0; my < (uint32_t)MBh; my++)
2396                    for (mx = 0; mx < (uint32_t)MBw; mx++)
2397                    {
2398                            const int mbnum = mx + my * MBw;
2399                            const MACROBLOCK *pMB = &pMBs[mbnum];
2400                            const VECTOR mv = pMB->mvs[0];
2401    
2402                            if (!MBmask[mbnum])
2403                                    continue;
2404    
2405                            if  ( ( fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ) > meanx )
2406                                    || ( fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ) > meany ) )
2407                                    MBmask[mbnum]=0;
2408                            else
2409                                    num++;
2410                    }
2411    
2412            } while ( (oldnum != num) && (num>=4) );
2413    
2414            if (num < 4)
2415            {
2416                    gmc.duv[0].x= gmc.duv[0].y= gmc.duv[1].x= gmc.duv[1].y= gmc.duv[2].x= gmc.duv[2].y=0;
2417            } else {
2418    
2419                    gmc.duv[0].x=(int)(sol[0]+0.5);
2420                    gmc.duv[0].y=(int)(sol[3]+0.5);
2421    
2422                    gmc.duv[1].x=(int)(sol[1]*pParam->width+0.5);
2423                    gmc.duv[1].y=(int)(-sol[2]*pParam->width+0.5);
2424    
2425                    gmc.duv[2].x=0;
2426                    gmc.duv[2].y=0;
2427            }
2428    //      fprintf(stderr,"wp1 = ( %4d, %4d)  wp2 = ( %4d, %4d) \n", gmc.duv[0].x, gmc.duv[0].y, gmc.duv[1].x, gmc.duv[1].y);
2429    
2430            free(MBmask);
2431    
2432            return gmc;
2433    }
2434    
2435    // functions which perform BITS-based search/bitcount
2436    
2437  static int  static int
2438  CountMBBitsInter(SearchData * const Data,  CountMBBitsInter(SearchData * const Data,
# Line 2302  Line 2453 
2453                  Data->qpel_precision = 1;                  Data->qpel_precision = 1;
2454                  CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data);                  CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data);
2455    
2456                  if (MotionFlags & (XVID_ME_HALFPELREFINE16_BITS | XVID_ME_EXTSEARCH_BITS)) { /* we have to prepare for halfpixel-precision search */                  if (MotionFlags & (HALFPELREFINE16_BITS | EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search
2457                          for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i];                          for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i];
2458                          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,
2459                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);
# Line 2311  Line 2462 
2462                                  CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);                                  CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);
2463                  }                  }
2464    
2465          } else { /* not qpel */          } else { // not qpel
2466    
2467                  CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);                  CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);
2468          }          }
2469    
2470          if (MotionFlags&XVID_ME_EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection);          if (MotionFlags&EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
2471    
2472          if (MotionFlags&XVID_ME_HALFPELREFINE16_BITS) SubpelRefine(Data);          if (MotionFlags&HALFPELREFINE16_BITS) SubpelRefine(Data);
2473    
2474          if (Data->qpel) {          if (Data->qpel) {
2475                  if (MotionFlags&(XVID_ME_EXTSEARCH_BITS | XVID_ME_HALFPELREFINE16_BITS)) { /* there was halfpel-precision search */                  if (MotionFlags&(EXTSEARCH_BITS | HALFPELREFINE16_BITS)) { // there was halfpel-precision search
2476                          for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) {                          for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) {
2477                                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* we have found a better match */                                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // we have found a better match
2478                                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;                                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
2479                          }                          }
2480    
2481                          /* preparing for qpel-precision search */                          // preparing for qpel-precision search
2482                          Data->qpel_precision = 1;                          Data->qpel_precision = 1;
2483                          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,
2484                                          pParam->width, pParam->height, Data->iFcode, 1, 0);                                          pParam->width, pParam->height, Data->iFcode, 1, 0);
2485                  }                  }
2486                  if (MotionFlags&XVID_ME_QUARTERPELREFINE16_BITS) SubpelRefine(Data);                  if (MotionFlags&QUARTERPELREFINE16_BITS) SubpelRefine(Data);
2487          }          }
2488    
2489          if (MotionFlags&XVID_ME_CHECKPREDICTION_BITS) { /* let's check vector equal to prediction */          if (MotionFlags&CHECKPREDICTION_BITS) { //let's check vector equal to prediction
2490                  VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV;                  VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV;
2491                  if (!(Data->predMV.x == v->x && Data->predMV.y == v->y))                  if (!(Data->predMV.x == v->x && Data->predMV.y == v->y))
2492                          CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data);                          CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data);
# Line 2360  Line 2511 
2511          memcpy(Data8, Data, sizeof(SearchData));          memcpy(Data8, Data, sizeof(SearchData));
2512          CheckCandidate = CheckCandidateBits8;          CheckCandidate = CheckCandidateBits8;
2513    
2514          for (i = 0; i < 4; i++) { /* for all luma blocks */          for (i = 0; i < 4; i++) { //for all luma blocks
2515    
2516                  Data8->iMinSAD = Data->iMinSAD + i + 1;                  Data8->iMinSAD = Data->iMinSAD + i + 1;
2517                  Data8->currentMV = Data->currentMV + i + 1;                  Data8->currentMV = Data->currentMV + i + 1;
# Line 2387  Line 2538 
2538                  *Data8->iMinSAD += BITS_MULT*t;                  *Data8->iMinSAD += BITS_MULT*t;
2539    
2540                  Data8->qpel_precision = Data8->qpel;                  Data8->qpel_precision = Data8->qpel;
2541                  /* checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) */                  // checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far)
2542                  {                  {
2543                          VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV;                          VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV;
2544                          if (!MVequal (*v, backup[i+1]) )                          if (!MVequal (*v, backup[i+1]) )
# Line 2395  Line 2546 
2546                  }                  }
2547    
2548                  if (Data8->qpel) {                  if (Data8->qpel) {
2549                          if (MotionFlags&XVID_ME_HALFPELREFINE8_BITS || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_BITS)) { /* halfpixel motion search follows */                          if (MotionFlags&HALFPELREFINE8_BITS || (MotionFlags&PMV_EXTSEARCH8 && MotionFlags&EXTSEARCH_BITS)) { // halfpixel motion search follows
2550                                  int32_t s = *Data8->iMinSAD;                                  int32_t s = *Data8->iMinSAD;
2551                                  Data8->currentMV->x = Data8->currentQMV->x/2;                                  Data8->currentMV->x = Data8->currentQMV->x/2;
2552                                  Data8->currentMV->y = Data8->currentQMV->y/2;                                  Data8->currentMV->y = Data8->currentQMV->y/2;
# Line 2406  Line 2557 
2557                                  if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1)                                  if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1)
2558                                          CheckCandidateBits8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8);                                          CheckCandidateBits8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8);
2559    
2560                                  if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS)                                  if (MotionFlags & PMV_EXTSEARCH8 && MotionFlags & EXTSEARCH_BITS)
2561                                          SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);                                          SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);
2562    
2563                                  if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS)                                  if (MotionFlags & HALFPELREFINE8_BITS)
2564                                          SubpelRefine(Data8);                                          SubpelRefine(Data8);
2565    
2566                                  if(s > *Data8->iMinSAD) { /* we have found a better match */                                  if (s > *Data8->iMinSAD) { //we have found a better match
2567                                          Data8->currentQMV->x = 2*Data8->currentMV->x;                                          Data8->currentQMV->x = 2*Data8->currentMV->x;
2568                                          Data8->currentQMV->y = 2*Data8->currentMV->y;                                          Data8->currentQMV->y = 2*Data8->currentMV->y;
2569                                  }                                  }
# Line 2422  Line 2573 
2573                                                          pParam->width, pParam->height, Data8->iFcode, 1, 0);                                                          pParam->width, pParam->height, Data8->iFcode, 1, 0);
2574    
2575                          }                          }
2576                          if (MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS) SubpelRefine(Data8);                          if (MotionFlags & QUARTERPELREFINE8_BITS) SubpelRefine(Data8);
2577    
2578                  } else { /* not qpel */                  } else { // not qpel
2579    
2580                          if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS) /* extsearch */                          if (MotionFlags & PMV_EXTSEARCH8 && MotionFlags & EXTSEARCH_BITS) //extsearch
2581                                  SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);                                  SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);
2582    
2583                          if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS)                          if (MotionFlags & HALFPELREFINE8_BITS)
2584                                  SubpelRefine(Data8); /* halfpel refinement */                                  SubpelRefine(Data8); //halfpel refinement
2585                  }                  }
2586    
2587                  /* checking vector equal to predicion */                  //checking vector equal to predicion
2588                  if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_BITS) {                  if (i != 0 && MotionFlags & CHECKPREDICTION_BITS) {
2589                          const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV;                          const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV;
2590                          if (!MVequal(*v, Data8->predMV))                          if (!MVequal(*v, Data8->predMV))
2591                                  CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8);                                  CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8);
2592                  }                  }
2593    
2594                  bits += *Data8->iMinSAD;                  bits += *Data8->iMinSAD;
2595                  if (bits >= Data->iMinSAD[0]) return bits; /* no chances for INTER4V */                  if (bits >= Data->iMinSAD[0]) return bits; // no chances for INTER4V
2596    
2597                  /* MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else */                  // MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else
2598                  if(Data->qpel) {                  if(Data->qpel) {
2599                          pMB->pmvs[i].x = Data8->currentQMV->x - Data8->predMV.x;                          pMB->pmvs[i].x = Data8->currentQMV->x - Data8->predMV.x;
2600                          pMB->pmvs[i].y = Data8->currentQMV->y - Data8->predMV.y;                          pMB->pmvs[i].y = Data8->currentQMV->y - Data8->predMV.y;
# Line 2460  Line 2611 
2611                  pMB->sad8[i] = 4 * *Data8->iMinSAD;                  pMB->sad8[i] = 4 * *Data8->iMinSAD;
2612                  if (Data8->temp[0]) cbp |= 1 << (5 - i);                  if (Data8->temp[0]) cbp |= 1 << (5 - i);
2613    
2614          } /* /for all luma blocks */          } // /for all luma blocks
2615    
2616          bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;          bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;
2617    
2618          /* let's check chroma */          // let's check chroma
2619          sumx = (sumx >> 3) + roundtab_76[sumx & 0xf];          sumx = (sumx >> 3) + roundtab_76[sumx & 0xf];
2620          sumy = (sumy >> 3) + roundtab_76[sumy & 0xf];          sumy = (sumy >> 3) + roundtab_76[sumy & 0xf];
2621    
2622          /* chroma U */          //chroma U
2623          ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);          ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);
2624          transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2);          transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2);
2625          bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4);          bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4);
2626    
2627          if (bits >= *Data->iMinSAD) return bits;          if (bits >= *Data->iMinSAD) return bits;
2628    
2629          /* chroma V */          //chroma V
2630          ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);          ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);
2631          transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2);          transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2);
2632          bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5);          bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5);
# Line 2488  Line 2639 
2639  static int  static int
2640  CountMBBitsIntra(const SearchData * const Data)  CountMBBitsIntra(const SearchData * const Data)
2641  {  {
2642          int bits = BITS_MULT*1; /* this one is ac/dc prediction flag bit */          int bits = BITS_MULT*1; //this one is ac/dc prediction flag bit
2643          int cbp = 0, i, dc = 0;          int cbp = 0, i, dc = 0;
2644          int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64;          int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64;
2645    
# Line 2502  Line 2653 
2653    
2654          bits += BITS_MULT*xvid_cbpy_tab[cbp>>2].len;          bits += BITS_MULT*xvid_cbpy_tab[cbp>>2].len;
2655    
2656          /*chroma U */          //chroma U
2657          transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2);          transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2);
2658          bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, &dc);          bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, &dc);
2659    
2660          if (bits >= Data->iMinSAD[0]) return bits;          if (bits >= Data->iMinSAD[0]) return bits;
2661    
2662          /* chroma V */          //chroma V
2663          transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2);          transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2);
2664          bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, &dc);          bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, &dc);
2665    
# Line 2516  Line 2667 
2667    
2668          return bits;          return bits;
2669  }  }
   
   
   
   
   
 static __inline void  
 GMEanalyzeMB (  const uint8_t * const pCur,  
                                 const uint8_t * const pRef,  
                                 const uint8_t * const pRefH,  
                                 const uint8_t * const pRefV,  
                                 const uint8_t * const pRefHV,  
                                 const int x,  
                                 const int y,  
                                 const MBParam * const pParam,  
                                 MACROBLOCK * const pMBs,  
                                 SearchData * const Data)  
 {  
   
         int i=0;  
 //      VECTOR pmv[3];  
         MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width];  
   
         Data->iMinSAD[0] = MV_MAX_ERROR;  
   
         //median is only used as prediction. it doesn't have to be real  
         if (x == 0 && y == 0)  
                 Data->predMV.x = Data->predMV.y = 0;  
         else  
                 if (x == 0) //left macroblock does not have any vector now  
                         Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median  
                 else if (y == 0) // top macroblock doesn't have it's vector  
                         Data->predMV = (pMB-1)->mvs[0]; // left instead of median  
                         else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median  
   
         get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,  
                                 pParam->width, pParam->height, Data->iFcode - ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0, 0);  
   
         Data->Cur = pCur + 16*(x + y * pParam->edged_width);  
         Data->RefP[0] = pRef + 16*(x + y * pParam->edged_width);  
         Data->RefP[1] = pRefV + 16*(x + y * pParam->edged_width);  
         Data->RefP[2] = pRefH + 16*(x + y * pParam->edged_width);  
         Data->RefP[3] = pRefHV + 16*(x + y * pParam->edged_width);  
   
         Data->currentMV[0].x = Data->currentMV[0].y = 0;  
         CheckCandidate16I(0, 0, 255, &i, Data);  
   
         if ( (Data->predMV.x !=0) || (Data->predMV.y != 0) )  
                 CheckCandidate16I(Data->predMV.x, Data->predMV.y, 255, &i, Data);  
   
         if (Data->iMinSAD[0] > 256 /*4 * MAX_SAD00_FOR_SKIP*/) // diamond only if needed  
                 DiamondSearch(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255);  
   
         SubpelRefine(Data);  
   
   
         /* for QPel halfpel positions are worse than in halfpel mode :( */  
 /*      if (Data->qpel) {  
                 Data->currentQMV->x = 2*Data->currentMV->x;  
                 Data->currentQMV->y = 2*Data->currentMV->y;  
                 Data->qpel_precision = 1;  
                 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,  
                                         pParam->width, pParam->height, iFcode, 1, 0);  
                 SubpelRefine(Data);  
         }  
 */  
   
         pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];  
         pMB->sad16 = Data->iMinSAD[0];  
         pMB->sad16 += d_mv_bits(pMB->mvs[0].x, pMB->mvs[0].y, Data->predMV, Data->iFcode, 0, 0);  
         return;  
 }  
   
 void  
 GMEanalysis(const MBParam * const pParam,  
                         const FRAMEINFO * const current,  
                         const FRAMEINFO * const reference,  
                         const IMAGE * const pRefH,  
                         const IMAGE * const pRefV,  
                         const IMAGE * const pRefHV)  
 {  
         uint32_t x, y;  
         MACROBLOCK * const pMBs = current->mbs;  
         const IMAGE * const pCurrent = &current->image;  
         const IMAGE * const pReference = &reference->image;  
   
         int32_t iMinSAD[5], temp[5];  
         VECTOR currentMV[5];  
         SearchData Data;  
         memset(&Data, 0, sizeof(SearchData));  
   
         Data.iEdgedWidth = pParam->edged_width;  
         Data.qpel = ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0);  
         Data.qpel_precision = 0;  
         Data.rounding = pParam->m_rounding_type;  
         Data.chroma = current->motion_flags & XVID_ME_CHROMA16;  
         Data.rrv = current->vop_flags & XVID_VOL_REDUCED_ENABLE;  
   
         Data.currentMV = &currentMV[0];  
         Data.iMinSAD = &iMinSAD[0];  
         Data.iFcode = current->fcode;  
         Data.temp = temp;  
         Data.RefP[0] = pReference->y;  
         Data.RefP[1] = pRefV->y;  
         Data.RefP[2] = pRefH->y;  
         Data.RefP[3] = pRefHV->y;  
   
         CheckCandidate = CheckCandidate16I;  
   
         if (sadInit) (*sadInit) ();  
   
         for (y = 0; y < pParam->mb_height; y ++) {  
                 for (x = 0; x < pParam->mb_width; x ++) {  
   
                         GMEanalyzeMB(pCurrent->y, pReference->y, pRefH->y, pRefV->y, pRefHV->y, x, y, pParam, pMBs, &Data);  
                 }  
         }  
         return;  
 }  
   
   
 WARPPOINTS  
 GlobalMotionEst(MACROBLOCK * const pMBs,  
                                 const MBParam * const pParam,  
                                 const FRAMEINFO * const current,  
                                 const FRAMEINFO * const reference,  
                                 const IMAGE * const pRefH,  
                                 const IMAGE * const pRefV,  
                                 const IMAGE * const pRefHV)  
 {  
   
         const unsigned int deltax=8;            // upper bound for difference between a MV and it's neighbour MVs  
         const unsigned int deltay=8;  
         const unsigned int gradx=512;           // lower bound for gradient in MB (ignore "flat" blocks)  
         const unsigned int grady=512;  
   
         double sol[4] = { 0., 0., 0., 0. };  
   
         WARPPOINTS gmc;  
   
         uint32_t mx, my;  
   
         int MBh = pParam->mb_height;  
         int MBw = pParam->mb_width;  
         const int minblocks = 9; //MBh*MBw/32+3;                /* just some reasonable number 3% + 3 */  
         const int maxblocks = MBh*MBw/4;                /* just some reasonable number 3% + 3 */  
   
         int num=0;  
         int oldnum;  
   
         gmc.duv[0].x = gmc.duv[0].y = gmc.duv[1].x = gmc.duv[1].y = gmc.duv[2].x = gmc.duv[2].y = 0;  
   
         GMEanalysis(pParam,current, reference, pRefH, pRefV, pRefHV);  
   
         /* block based ME isn't done, yet, so do a quick presearch */  
   
 // filter mask of all blocks  
   
         for (my = 0; my < (uint32_t)MBh; my++)  
         for (mx = 0; mx < (uint32_t)MBw; mx++)  
         {  
                 const int mbnum = mx + my * MBw;  
                         pMBs[mbnum].mcsel = 0;  
         }  
   
   
         for (my = 1; my < (uint32_t)MBh-1; my++) /* ignore boundary blocks */  
         for (mx = 1; mx < (uint32_t)MBw-1; mx++) /* theirs MVs are often wrong */  
         {  
                 const int mbnum = mx + my * MBw;  
                 MACROBLOCK *const pMB = &pMBs[mbnum];  
                 const VECTOR mv = pMB->mvs[0];  
   
                 /* don't use object boundaries */  
                 if   ( (abs(mv.x -   (pMB-1)->mvs[0].x) < deltax)  
                         && (abs(mv.y -   (pMB-1)->mvs[0].y) < deltay)  
                         && (abs(mv.x -   (pMB+1)->mvs[0].x) < deltax)  
                         && (abs(mv.y -   (pMB+1)->mvs[0].y) < deltay)  
                         && (abs(mv.x - (pMB-MBw)->mvs[0].x) < deltax)  
                         && (abs(mv.y - (pMB-MBw)->mvs[0].y) < deltay)  
                         && (abs(mv.x - (pMB+MBw)->mvs[0].x) < deltax)  
                         && (abs(mv.y - (pMB+MBw)->mvs[0].y) < deltay) )  
                 {       const int iEdgedWidth = pParam->edged_width;  
                         const uint8_t *const pCur = current->image.y + 16*(my*iEdgedWidth + mx);  
                         if ( (sad16 ( pCur, pCur+1 , iEdgedWidth, 65536) >= gradx )  
                          &&  (sad16 ( pCur, pCur+iEdgedWidth, iEdgedWidth, 65536) >= grady ) )  
                          {      pMB->mcsel = 1;  
                                 num++;  
                          }  
   
                 /* only use "structured" blocks */  
                 }  
         }  
         emms();  
   
         /*      further filtering would be possible, but during iteration, remaining  
                 outliers usually are removed, too */  
   
         if (num>= minblocks)  
         do {            /* until convergence */  
                 double DtimesF[4];  
                 double a,b,c,n,invdenom;  
                 double meanx,meany;  
   
                 a = b = c = n = 0;  
                 DtimesF[0] = DtimesF[1] = DtimesF[2] = DtimesF[3] = 0.;  
                 for (my = 1; my < (uint32_t)MBh-1; my++)  
                 for (mx = 1; mx < (uint32_t)MBw-1; mx++)  
                 {  
                         const int mbnum = mx + my * MBw;  
                         const VECTOR mv = pMBs[mbnum].mvs[0];  
   
                         if (!pMBs[mbnum].mcsel)  
                                 continue;  
   
                         n++;  
                         a += 16*mx+8;  
                         b += 16*my+8;  
                         c += (16*mx+8)*(16*mx+8)+(16*my+8)*(16*my+8);  
   
                         DtimesF[0] += (double)mv.x;  
                         DtimesF[1] += (double)mv.x*(16*mx+8) + (double)mv.y*(16*my+8);  
                         DtimesF[2] += (double)mv.x*(16*my+8) - (double)mv.y*(16*mx+8);  
                         DtimesF[3] += (double)mv.y;  
                 }  
   
         invdenom = a*a+b*b-c*n;  
   
 /* Solve the system:    sol = (D'*E*D)^{-1} D'*E*F   */  
 /* D'*E*F has been calculated in the same loop as matrix */  
   
         sol[0] = -c*DtimesF[0] + a*DtimesF[1] + b*DtimesF[2];  
         sol[1] =  a*DtimesF[0] - n*DtimesF[1]                           + b*DtimesF[3];  
         sol[2] =  b*DtimesF[0]                          - n*DtimesF[2] - a*DtimesF[3];  
         sol[3] =                                 b*DtimesF[1] - a*DtimesF[2] - c*DtimesF[3];  
   
         sol[0] /= invdenom;  
         sol[1] /= invdenom;  
         sol[2] /= invdenom;  
         sol[3] /= invdenom;  
   
         meanx = meany = 0.;  
         oldnum = 0;  
         for (my = 1; my < (uint32_t)MBh-1; my++)  
                 for (mx = 1; mx < (uint32_t)MBw-1; mx++)  
                 {  
                         const int mbnum = mx + my * MBw;  
                         const VECTOR mv = pMBs[mbnum].mvs[0];  
   
                         if (!pMBs[mbnum].mcsel)  
                                 continue;  
   
                         oldnum++;  
                         meanx += fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - (double)mv.x );  
                         meany += fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - (double)mv.y );  
                 }  
   
         if (4*meanx > oldnum)   /* better fit than 0.25 (=1/4pel) is useless */  
                 meanx /= oldnum;  
         else  
                 meanx = 0.25;  
   
         if (4*meany > oldnum)  
                 meany /= oldnum;  
         else  
                 meany = 0.25;  
   
         num = 0;  
         for (my = 0; my < (uint32_t)MBh; my++)  
                 for (mx = 0; mx < (uint32_t)MBw; mx++)  
                 {  
                         const int mbnum = mx + my * MBw;  
                         const VECTOR mv = pMBs[mbnum].mvs[0];  
   
                         if (!pMBs[mbnum].mcsel)  
                                 continue;  
   
                         if  ( ( fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - (double)mv.x ) > meanx )  
                                 || ( fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - (double)mv.y ) > meany ) )  
                                 pMBs[mbnum].mcsel=0;  
                         else  
                                 num++;  
                 }  
   
         } while ( (oldnum != num) && (num>= minblocks) );  
   
         if (num < minblocks)  
         {  
                 const int iEdgedWidth = pParam->edged_width;  
                 num = 0;  
   
 /*              fprintf(stderr,"Warning! Unreliable GME (%d/%d blocks), falling back to translation.\n",num,MBh*MBw);  
 */  
                 gmc.duv[0].x= gmc.duv[0].y= gmc.duv[1].x= gmc.duv[1].y= gmc.duv[2].x= gmc.duv[2].y=0;  
   
                 if (!(current->motion_flags & XVID_GME_REFINE))  
                         return gmc;  
   
                 for (my = 1; my < (uint32_t)MBh-1; my++) /* ignore boundary blocks */  
                 for (mx = 1; mx < (uint32_t)MBw-1; mx++) /* theirs MVs are often wrong */  
                 {  
                         const int mbnum = mx + my * MBw;  
                         MACROBLOCK *const pMB = &pMBs[mbnum];  
                         const uint8_t *const pCur = current->image.y + 16*(my*iEdgedWidth + mx);  
                         if ( (sad16 ( pCur, pCur+1 , iEdgedWidth, 65536) >= gradx )  
                          &&  (sad16 ( pCur, pCur+iEdgedWidth, iEdgedWidth, 65536) >= grady ) )  
                          {      pMB->mcsel = 1;  
                                 gmc.duv[0].x += pMB->mvs[0].x;  
                                 gmc.duv[0].y += pMB->mvs[0].y;  
                                 num++;  
                          }  
                 }  
   
                 if (gmc.duv[0].x)  
                         gmc.duv[0].x /= num;  
                 if (gmc.duv[0].y)  
                         gmc.duv[0].y /= num;  
         } else {  
   
                 gmc.duv[0].x=(int)(sol[0]+0.5);  
                 gmc.duv[0].y=(int)(sol[3]+0.5);  
   
                 gmc.duv[1].x=(int)(sol[1]*pParam->width+0.5);  
                 gmc.duv[1].y=(int)(-sol[2]*pParam->width+0.5);  
   
                 gmc.duv[2].x=-gmc.duv[1].y;             /* two warp points only */  
                 gmc.duv[2].y=gmc.duv[1].x;  
         }  
         if (num>maxblocks)  
         {       for (my = 1; my < (uint32_t)MBh-1; my++)  
                 for (mx = 1; mx < (uint32_t)MBw-1; mx++)  
                 {  
                         const int mbnum = mx + my * MBw;  
                         if (pMBs[mbnum-1].mcsel)  
                                 pMBs[mbnum].mcsel=0;  
                         else  
                                 if (pMBs[mbnum-MBw].mcsel)  
                                         pMBs[mbnum].mcsel=0;  
                 }  
         }  
         return gmc;  
 }  
   
 int  
 GlobalMotionEstRefine(  
                                 WARPPOINTS *const startwp,  
                                 MACROBLOCK * const pMBs,  
                                 const MBParam * const pParam,  
                                 const FRAMEINFO * const current,  
                                 const FRAMEINFO * const reference,  
                                 const IMAGE * const pCurr,  
                                 const IMAGE * const pRef,  
                                 const IMAGE * const pRefH,  
                                 const IMAGE * const pRefV,  
                                 const IMAGE * const pRefHV)  
 {  
         uint8_t* GMCblock = (uint8_t*)malloc(16*pParam->edged_width);  
         WARPPOINTS bestwp=*startwp;  
         WARPPOINTS centerwp,currwp;  
         int gmcminSAD=0;  
         int gmcSAD=0;  
         int direction;  
 //      int mx,my;  
   
 /* use many blocks... */  
 /*              for (my = 0; my < (uint32_t)pParam->mb_height; my++)  
                 for (mx = 0; mx < (uint32_t)pParam->mb_width; mx++)  
                 {  
                         const int mbnum = mx + my * pParam->mb_width;  
                         pMBs[mbnum].mcsel=1;  
                 }  
 */  
   
 /* or rather don't use too many blocks... */  
 /*  
                 for (my = 1; my < (uint32_t)MBh-1; my++)  
                 for (mx = 1; mx < (uint32_t)MBw-1; mx++)  
                 {  
                         const int mbnum = mx + my * MBw;  
                         if (MBmask[mbnum-1])  
                                 MBmask[mbnum-1]=0;  
                         else  
                                 if (MBmask[mbnum-MBw])  
                                         MBmask[mbnum-1]=0;  
   
                 }  
 */  
                 gmcminSAD = globalSAD(&bestwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
   
                 if ( (reference->coding_type == S_VOP)  
                         && ( (reference->warp.duv[1].x != bestwp.duv[1].x)  
                           || (reference->warp.duv[1].y != bestwp.duv[1].y)  
                           || (reference->warp.duv[0].x != bestwp.duv[0].x)  
                           || (reference->warp.duv[0].y != bestwp.duv[0].y)  
                           || (reference->warp.duv[2].x != bestwp.duv[2].x)  
                           || (reference->warp.duv[2].y != bestwp.duv[2].y) ) )  
                 {  
                         gmcSAD = globalSAD(&reference->warp, pParam, pMBs,  
                                                                 current, pRef, pCurr, GMCblock);  
   
                         if (gmcSAD < gmcminSAD)  
                         {       bestwp = reference->warp;  
                                 gmcminSAD = gmcSAD;  
                         }  
                 }  
   
         do {  
                 direction = 0;  
                 centerwp = bestwp;  
   
                 currwp = centerwp;  
   
                 currwp.duv[0].x--;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 1;  
                 }  
                 else  
                 {  
                 currwp = centerwp; currwp.duv[0].x++;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 2;  
                 }  
                 }  
                 if (direction) continue;  
   
                 currwp = centerwp; currwp.duv[0].y--;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 4;  
                 }  
                 else  
                 {  
                 currwp = centerwp; currwp.duv[0].y++;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 8;  
                 }  
                 }  
                 if (direction) continue;  
   
                 currwp = centerwp; currwp.duv[1].x++;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 32;  
                 }  
                 currwp.duv[2].y++;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 1024;  
                 }  
   
                 currwp = centerwp; currwp.duv[1].x--;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 16;  
                 }  
                 else  
                 {  
                 currwp = centerwp; currwp.duv[1].x++;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 32;  
                 }  
                 }  
                 if (direction) continue;  
   
   
                 currwp = centerwp; currwp.duv[1].y--;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 64;  
                 }  
                 else  
                 {  
                 currwp = centerwp; currwp.duv[1].y++;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 128;  
                 }  
                 }  
                 if (direction) continue;  
   
                 currwp = centerwp; currwp.duv[2].x--;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 256;  
                 }  
                 else  
                 {  
                 currwp = centerwp; currwp.duv[2].x++;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 512;  
                 }  
                 }  
                 if (direction) continue;  
   
                 currwp = centerwp; currwp.duv[2].y--;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 1024;  
                 }  
                 else  
                 {  
                 currwp = centerwp; currwp.duv[2].y++;  
                 gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);  
                 if (gmcSAD < gmcminSAD)  
                 {       bestwp = currwp;  
                         gmcminSAD = gmcSAD;  
                         direction = 2048;  
                 }  
                 }  
         } while (direction);  
         free(GMCblock);  
   
         *startwp = bestwp;  
   
         return gmcminSAD;  
 }  
   
 int  
 globalSAD(const WARPPOINTS *const wp,  
                   const MBParam * const pParam,  
                   const MACROBLOCK * const pMBs,  
                   const FRAMEINFO * const current,  
                   const IMAGE * const pRef,  
                   const IMAGE * const pCurr,  
                   uint8_t *const GMCblock)  
 {  
         NEW_GMC_DATA gmc_data;  
         int iSAD, gmcSAD=0;  
         int num=0;  
         unsigned int mx, my;  
   
         generate_GMCparameters( 3, 3, wp, pParam->width, pParam->height, &gmc_data);  
   
         for (my = 0; my < (uint32_t)pParam->mb_height; my++)  
                 for (mx = 0; mx < (uint32_t)pParam->mb_width; mx++) {  
   
                 const int mbnum = mx + my * pParam->mb_width;  
                 const int iEdgedWidth = pParam->edged_width;  
   
                 if (!pMBs[mbnum].mcsel)  
                         continue;  
   
                 gmc_data.predict_16x16(&gmc_data, GMCblock,  
                                                 pRef->y,  
                                                 iEdgedWidth,  
                                                 iEdgedWidth,  
                                                 mx, my,  
                                                 pParam->m_rounding_type);  
   
                 iSAD = sad16 ( pCurr->y + 16*(my*iEdgedWidth + mx),  
                                                   GMCblock , iEdgedWidth, 65536);  
                 iSAD -= pMBs[mbnum].sad16;  
   
                 if (iSAD<0)  
                         gmcSAD += iSAD;  
                 num++;  
         }  
         return gmcSAD;  
 }  
   

Legend:
Removed from v.1.58.2.20  
changed lines
  Added in v.1.72

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