[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.16, Mon Jun 9 01:22:11 2003 UTC revision 1.72, Thu Jun 26 10:37:09 2003 UTC
# Line 31  Line 31 
31  #include <assert.h>  #include <assert.h>
32  #include <stdio.h>  #include <stdio.h>
33  #include <stdlib.h>  #include <stdlib.h>
34  #include <string.h>     /* memcpy */  #include <string.h>     // memcpy
35  #include <math.h>       /* lrint */  #include <math.h>       // lrint
36    
37  #include "../encoder.h"  #include "../encoder.h"
38  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
# Line 168  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 192  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 218  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 229  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 240  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 262  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 272  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 286  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 294  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 302  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 325  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 369  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 392  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 430  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 460  Line 457 
457  static void  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 565  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 661  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 670  Line 667 
667          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
668                  int s = 8*((i&1) + (i>>1)*data->iEdgedWidth);                  int s = 8*((i&1) + (i>>1)*data->iEdgedWidth);
669                  transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth);                  transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth);
670                  bits += data->temp[i] = Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, i);                  bits += data->temp[i] = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i);
671          }          }
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]) {
677                    data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; }
678            if (data->temp[1] < data->iMinSAD[2]) {
679                    data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; }
680            if (data->temp[2] < data->iMinSAD[3]) {
681                    data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; }
682            if (data->temp[3] < data->iMinSAD[4]) {
683                    data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; }
684    
685          bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;          bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;
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->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->iQuant, data->quant_type, &cbp, 5);          bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5);
703    
704          bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len;          bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len;
705    
# Line 701  Line 708 
708                  current[0].x = x; current[0].y = y;                  current[0].x = x; current[0].y = y;
709                  *dir = Direction;                  *dir = Direction;
710          }          }
   
         if (data->temp[0] + t < data->iMinSAD[1]) {  
                 data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; }  
         if (data->temp[1] < data->iMinSAD[2]) {  
                 data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; }  
         if (data->temp[2] < data->iMinSAD[3]) {  
                 data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; }  
         if (data->temp[3] < data->iMinSAD[4]) {  
                 data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; }  
   
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)
# Line 728  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          }          }
732    
733          transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth);          transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth);
734          bits = Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 5);          bits = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5);
735          bits += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);          bits += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
736    
737          if (bits < data->iMinSAD[0]) {          if (bits < data->iMinSAD[0]) {
# Line 757  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 766  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 782  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 820  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 872  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 897  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 952  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 977  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 985  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 1007  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 1021  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 1055  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 1077  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 = \          int skip_thresh = INITIAL_SKIP_THRESH *
1082                  INITIAL_SKIP_THRESH * \                  (current->global_flags & XVID_REDUCED ? 4:1) *
1083                  (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \                  (current->global_flags & XVID_MODEDECISION_BITS ? 2:1);
                 (current->vop_flags & XVID_VOP_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];
1089          int32_t iMinSAD[5];          int32_t iMinSAD[5];
1090          DECLARE_ALIGNED_MATRIX(dct_space, 2, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);
1091          SearchData Data;          SearchData Data;
1092          memset(&Data, 0, sizeof(SearchData));          memset(&Data, 0, sizeof(SearchData));
1093          Data.iEdgedWidth = iEdgedWidth;          Data.iEdgedWidth = iEdgedWidth;
# Line 1102  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 1141  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 1159  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 1171  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          }          }
# Line 1184  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 1201  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 1246  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 1257  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 1293  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 1305  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 1323  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 1333  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 1364  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 1376  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 1385  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 1440  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 1458  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 1527  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 1581  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 1603  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 1626  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 1650  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 1666  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 1685  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 1693  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 1757  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 1775  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 1806  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 1864  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 1903  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 1914  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 1924  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 1941  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 1969  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 1993  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 2005  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 2048  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 2056  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 2064  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 2076  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 2116  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 2152  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 2164  Line 2159 
2159          }          }
2160  }  }
2161    
2162  #define INTRA_THRESH    1700  #define INTRA_THRESH    2200
2163  #define INTER_THRESH    1200  #define INTER_THRESH    50
2164    #define INTRA_THRESH2   95
2165    
2166  int  int
2167  MEanalysis(     const IMAGE * const pRef,  MEanalysis(     const IMAGE * const pRef,
2168                          const FRAMEINFO * const Current,                          const FRAMEINFO * const Current,
2169                          const MBParam * const pParam,                          const MBParam * const pParam,
2170                          const int maxIntra, /* maximum number if non-I frames */                          const int maxIntra, //maximum number if non-I frames
2171                          const int intraCount, /* number of non-I frames after last I frame; 0 if we force P/B frame */                          const int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame
2172                          const int bCount,  /* number of B frames in a row */                          const int bCount,  // number of B frames in a row
2173                          const int b_thresh)                          const int b_thresh)
2174  {  {
2175          uint32_t x, y, intra = 0;          uint32_t x, y, intra = 0;
2176          int sSAD = 0;          int sSAD = 0;
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 + 10*b_thresh;          int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + b_thresh;
2180          int s = 0, blocks = 0;          int s = 0, blocks = 0;
2181            int complexity = 0;
2182    
2183          int32_t iMinSAD[5], temp[5];          int32_t iMinSAD[5], temp[5];
2184          VECTOR currentMV[5];          VECTOR currentMV[5];
# Line 2193  Line 2190 
2190          Data.temp = temp;          Data.temp = temp;
2191          CheckCandidate = CheckCandidate32I;          CheckCandidate = CheckCandidate32I;
2192    
2193    
2194          if (intraCount != 0) {          if (intraCount != 0) {
2195                  if (intraCount < 10) /* we're right after an I frame */                  if (intraCount < 10) // we're right after an I frame
2196                          IntraThresh += 15* (intraCount - 10) * (intraCount - 10);                          IntraThresh += 15* (intraCount - 10) * (intraCount - 10);
2197                  else                  else
2198                          if ( 5*(maxIntra - intraCount) < maxIntra) /* we're close to maximum. 2 sec when max is 10 sec */                          if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec
2199                                  IntraThresh -= (IntraThresh * (maxIntra - 8*(maxIntra - intraCount)))/maxIntra;                                  IntraThresh -= (IntraThresh * (maxIntra - 8*(maxIntra - intraCount)))/maxIntra;
2200          }          }
2201    
2202          InterThresh -= (350 - 8*b_thresh) * bCount;          InterThresh -= 12 * bCount;
2203          if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh;          if (InterThresh < 15 + b_thresh) InterThresh = 15 + b_thresh;
2204    
2205          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();
2206    
2207          for (y = 1; y < pParam->mb_height-1; y += 2) {          for (y = 1; y < pParam->mb_height-1; y += 2) {
2208                  for (x = 1; x < pParam->mb_width-1; x += 2) {                  for (x = 1; x < pParam->mb_width-1; x += 2) {
2209                          int i;                          int i;
2210                          blocks += 4;                          blocks += 10;
2211    
2212                          if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV;                          if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV;
2213                          else { /* extrapolation of the vector found for last frame */                          else { //extrapolation of the vector found for last frame
2214                                  pMBs[x + y * pParam->mb_width].mvs[0].x =                                  pMBs[x + y * pParam->mb_width].mvs[0].x =
2215                                          (pMBs[x + y * pParam->mb_width].mvs[0].x * (bCount+1) ) / bCount;                                          (pMBs[x + y * pParam->mb_width].mvs[0].x * (bCount+1) ) / bCount;
2216                                  pMBs[x + y * pParam->mb_width].mvs[0].y =                                  pMBs[x + y * pParam->mb_width].mvs[0].y =
# Line 2224  Line 2222 
2222                          for (i = 0; i < 4; i++) {                          for (i = 0; i < 4; i++) {
2223                                  int dev;                                  int dev;
2224                                  MACROBLOCK *pMB = &pMBs[x+(i&1) + (y+(i>>1)) * pParam->mb_width];                                  MACROBLOCK *pMB = &pMBs[x+(i&1) + (y+(i>>1)) * pParam->mb_width];
                                 if (pMB->sad16 > IntraThresh) {  
2225                                          dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1)) * pParam->edged_width) * 16,                                          dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1)) * pParam->edged_width) * 16,
2226                                                                          pParam->edged_width);                                                                          pParam->edged_width);
2227    
2228                                    complexity += dev;
2229                                          if (dev + IntraThresh < pMB->sad16) {                                          if (dev + IntraThresh < pMB->sad16) {
2230                                                  pMB->mode = MODE_INTRA;                                                  pMB->mode = MODE_INTRA;
2231                                                  if (++intra > ((pParam->mb_height-2)*(pParam->mb_width-2))/2) return I_VOP;                                                  if (++intra > ((pParam->mb_height-2)*(pParam->mb_width-2))/2) return I_VOP;
2232                                          }                                          }
2233                                  }  
2234                                  if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) s++;                                  if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)
2235                                            if (dev > 500 && pMB->sad16 < 1000)
2236                                                    sSAD += 1000;
2237    
2238                                  sSAD += pMB->sad16;                                  sSAD += pMB->sad16;
2239                          }                          }
2240                  }                  }
2241          }          }
2242            complexity >>= 7;
2243    
2244          sSAD /= blocks;          sSAD /= complexity + 4*blocks;
   
         if (b_thresh < 20) {  
                 s = (10*s) / blocks;  
                 if (s > 4) sSAD += (s - 2) * (40 - 2*b_thresh); /* static block - looks bad when in bframe... */  
         }  
2245    
2246            if (intraCount > 12 && sSAD > INTRA_THRESH2 ) return I_VOP;
2247          if (sSAD > InterThresh ) return P_VOP;          if (sSAD > InterThresh ) return P_VOP;
2248          emms();          emms();
2249          return B_VOP;          return B_VOP;
# Line 2262  Line 2260 
2260                                  const IMAGE * const pRefHV      )                                  const IMAGE * const pRefHV      )
2261  {  {
2262    
2263          const int deltax=8;             /* upper bound for difference between a MV and it's neighbour MVs */          const int deltax=8;             // upper bound for difference between a MV and it's neighbour MVs
2264          const int deltay=8;          const int deltay=8;
2265          const int grad=512;             /* lower bound for deviation in MB */          const int grad=512;             // lower bound for deviation in MB
2266    
2267          WARPPOINTS gmc;          WARPPOINTS gmc;
2268    
# Line 2286  Line 2284 
2284                                                  gmc.duv[2].x= gmc.duv[2].y = 0;                                                  gmc.duv[2].x= gmc.duv[2].y = 0;
2285                                          return gmc; }                                          return gmc; }
2286    
2287          /* filter mask of all blocks */  // filter mask of all blocks
2288    
2289          for (my = 1; my < (uint32_t)MBh-1; my++)          for (my = 1; my < (uint32_t)MBh-1; my++)
2290          for (mx = 1; mx < (uint32_t)MBw-1; mx++)          for (mx = 1; mx < (uint32_t)MBw-1; mx++)
# Line 2427  Line 2425 
2425                  gmc.duv[2].x=0;                  gmc.duv[2].x=0;
2426                  gmc.duv[2].y=0;                  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); */  //      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);          free(MBmask);
2431    
2432          return gmc;          return gmc;
2433  }  }
2434    
2435  /* functions which perform BITS-based search/bitcount */  // functions which perform BITS-based search/bitcount
2436    
2437  static int  static int
2438  CountMBBitsInter(SearchData * const Data,  CountMBBitsInter(SearchData * const Data,
# Line 2455  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 2464  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 2513  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 2540  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 2548  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 2559  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 2575  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 2613  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->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->iQuant, Data->quant_type, &cbp, 5);          bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5);
2633    
2634          bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len;          bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len;
2635    
# Line 2641  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    
2646          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
2647                  int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth);                  int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2648                  transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth);                  transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth);
2649                  bits += Block_CalcBitsIntra(coeff, in, Data->iQuant, Data->quant_type, &cbp, i, &dc);                  bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i, &dc);
2650    
2651                  if (bits >= Data->iMinSAD[0]) return bits;                  if (bits >= Data->iMinSAD[0]) return bits;
2652          }          }
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->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->iQuant, Data->quant_type, &cbp, 5, &dc);          bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, &dc);
2665    
2666          bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len;          bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len;
2667    

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

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