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

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

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

trunk/xvidcore/src/motion/motion_est.c revision 872, Wed Feb 19 20:12:43 2003 UTC branches/dev-api-4/xvidcore/src/motion/motion_est.c revision 974, Sat Apr 5 16:47:44 2003 UTC
# Line 81  Line 81 
81  static __inline uint32_t  static __inline uint32_t
82  d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv)  d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv)
83  {  {
84          int xb, yb;          int bits;
85          x = qpel ? x<<1 : x;          const int q = (1 << (iFcode - 1)) - 1;
86          y = qpel ? y<<1 : y;  
87            x <<= qpel;
88            y <<= qpel;
89          if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); }          if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); }
90    
91          x -= pred.x;          x -= pred.x;
92          y -= pred.y;          bits = (x != 0 ? iFcode:0);
93            x = abs(x);
94          if (x) {          x += q;
                 x = ABS(x);  
                 x += (1 << (iFcode - 1)) - 1;  
95                  x >>= (iFcode - 1);                  x >>= (iFcode - 1);
96                  if (x > 32) x = 32;          bits += mvtab[x];
97                  xb = mvtab[x] + iFcode;  
98          } else xb = 1;          y -= pred.y;
99            bits += (y != 0 ? iFcode:0);
100          if (y) {          y = abs(y);
101                  y = ABS(y);          y += q;
                 y += (1 << (iFcode - 1)) - 1;  
102                  y >>= (iFcode - 1);                  y >>= (iFcode - 1);
103                  if (y > 32) y = 32;          bits += mvtab[y];
104                  yb = mvtab[y] + iFcode;  
105          } else yb = 1;          return bits;
         return xb + yb;  
106  }  }
107    
108  static int32_t ChromaSAD2(int fx, int fy, int bx, int by, const SearchData * const data)  static int32_t ChromaSAD2(int fx, int fy, int bx, int by, const SearchData * const data)
# Line 249  Line 247 
247          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);
248          ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;          ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
249          switch( ((x&1)<<1) + (y&1) ) {          switch( ((x&1)<<1) + (y&1) ) {
250          case 0: // pure halfpel position          case 3: // x and y in qpel resolution - the "corners" (top left/right and
251                  return (uint8_t *) ref1;                          // bottom left/right) during qpel refinement
252                    ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
253                    ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
254                    ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);
255                    ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
256                    ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
257                    ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
258                    interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
259                  break;                  break;
260    
261          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          case 1: // x halfpel, y qpel - top or bottom during qpel refinement
# Line 265  Line 270 
270                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
271                  break;                  break;
272    
273          default: // x and y in qpel resolution - the "corners" (top left/right and          default: // pure halfpel position
274                           // bottom left/right) during qpel refinement                  return (uint8_t *) ref1;
275                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);  
                 ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);  
                 ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);  
                 ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);  
                 break;  
276          }          }
277          return Reference;          return Reference;
278  }  }
# Line 319  Line 317 
317                  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);
318                  break;                  break;
319    
320          case 0: // pure halfpel position          default: // pure halfpel position
321                  return (uint8_t *) ref1;                  return (uint8_t *) ref1;
322          }          }
323          return Reference;          return Reference;
# Line 379  Line 377 
377  {  {
378          int32_t sad; uint32_t t;          int32_t sad; uint32_t t;
379          const uint8_t * Reference;          const uint8_t * Reference;
380            VECTOR * current;
381    
382          if ( (x > data->max_dx) || (x < data->min_dx)          if ( (x > data->max_dx) || (x < data->min_dx)
383                  || (y > data->max_dy) || (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
384    
385          if (!data->qpel_precision) Reference = GetReference(x, y, data);          if (!data->qpel_precision) {
386          else Reference = Interpolate8x8qpel(x, y, 0, 0, data);                  Reference = GetReference(x, y, data);
387                    current = data->currentMV;
388            } else { // x and y are in 1/4 precision
389                    Reference = Interpolate8x8qpel(x, y, 0, 0, data);
390                    current = data->currentQMV;
391            }
392    
393          sad = sad8(data->Cur, Reference, data->iEdgedWidth);          sad = sad8(data->Cur, Reference, data->iEdgedWidth);
394          t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);          t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
# Line 393  Line 397 
397    
398          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
399                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
400                  data->currentMV->x = x; data->currentMV->y = y;                  current->x = x; current->y = y;
401                  *dir = Direction;                  *dir = Direction;
402          }          }
403  }  }
# Line 405  Line 409 
409          uint32_t t;          uint32_t t;
410          const uint8_t * Reference;          const uint8_t * Reference;
411    
412          if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero integer value          if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero even value
413                  (x > data->max_dx) || (x < data->min_dx)                  (x > data->max_dx) || (x < data->min_dx)
414                  || (y > data->max_dy) || (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
415    
# Line 440  Line 444 
444          uint32_t t;          uint32_t t;
445          VECTOR * current;          VECTOR * current;
446    
447          if ( (x > data->max_dx) | ( x < data->min_dx)          if ( (x > data->max_dx) || ( x < data->min_dx)
448                  | (y > data->max_dy) | (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
449    
450          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
451    
# Line 506  Line 510 
510          const uint8_t *ReferenceF, *ReferenceB;          const uint8_t *ReferenceF, *ReferenceB;
511          VECTOR *current;          VECTOR *current;
512    
513          if ( (xf > data->max_dx) | (xf < data->min_dx)          if ((xf > data->max_dx) || (xf < data->min_dx) ||
514                  | (yf > data->max_dy) | (yf < data->min_dy) ) return;                  (yf > data->max_dy) || (yf < data->min_dy))
515                    return;
516    
517          if (!data->qpel_precision) {          if (!data->qpel_precision) {
518                  ReferenceF = GetReference(xf, yf, data);                  ReferenceF = GetReference(xf, yf, data);
# Line 552  Line 557 
557          const uint8_t *ReferenceB;          const uint8_t *ReferenceB;
558          VECTOR mvs, b_mvs;          VECTOR mvs, b_mvs;
559    
560          if (( x > 31) | ( x < -32) | ( y > 31) | (y < -32)) return;          if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
561    
562          for (k = 0; k < 4; k++) {          for (k = 0; k < 4; k++) {
563                  mvs.x = data->directmvF[k].x + x;                  mvs.x = data->directmvF[k].x + x;
# Line 565  Line 570 
570                          data->directmvB[k].y                          data->directmvB[k].y
571                          : mvs.y - data->referencemv[k].y);                          : mvs.y - data->referencemv[k].y);
572    
573                  if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx)                  if ((mvs.x > data->max_dx)   || (mvs.x < data->min_dx)   ||
574                          | (mvs.y > data->max_dy) | (mvs.y < data->min_dy)                          (mvs.y > data->max_dy)   || (mvs.y < data->min_dy)   ||
575                          | (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx)                          (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) ||
576                          | (b_mvs.y > data->max_dy) | (b_mvs.y < data->min_dy) ) return;                          (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) )
577                            return;
578    
579                  if (data->qpel) {                  if (data->qpel) {
580                          xcf += mvs.x/2; ycf += mvs.y/2;                          xcf += mvs.x/2; ycf += mvs.y/2;
# Line 610  Line 616 
616          const uint8_t *ReferenceB;          const uint8_t *ReferenceB;
617          VECTOR mvs, b_mvs;          VECTOR mvs, b_mvs;
618    
619          if (( x > 31) | ( x < -32) | ( y > 31) | (y < -32)) return;          if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
620    
621          mvs.x = data->directmvF[0].x + x;          mvs.x = data->directmvF[0].x + x;
622          b_mvs.x = ((x == 0) ?          b_mvs.x = ((x == 0) ?
# Line 622  Line 628 
628                  data->directmvB[0].y                  data->directmvB[0].y
629                  : mvs.y - data->referencemv[0].y);                  : mvs.y - data->referencemv[0].y);
630    
631          if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx)          if ( (mvs.x > data->max_dx) || (mvs.x < data->min_dx)
632                  | (mvs.y > data->max_dy) | (mvs.y < data->min_dy)                  || (mvs.y > data->max_dy) || (mvs.y < data->min_dy)
633                  | (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx)                  || (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx)
634                  | (b_mvs.y > data->max_dy) | (b_mvs.y < data->min_dy) ) return;                  || (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) return;
635    
636          if (data->qpel) {          if (data->qpel) {
637                  xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2);                  xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2);
# Line 659  Line 665 
665  CheckCandidateBits16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateBits16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
666  {  {
667    
668          static int16_t in[64], coeff[64];          int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
669          int32_t bits = 0, sum;          int32_t bits = 0, sum;
670          VECTOR * current;          VECTOR * current;
671          const uint8_t * ptr;          const uint8_t * ptr;
# Line 721  Line 727 
727                  }                  }
728          }          }
729    
730          bits += cbpy_tab[15-(cbp>>2)].len;          bits += xvid_cbpy_tab[15-(cbp>>2)].len;
731          bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len;          bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len;
732    
733          if (bits < data->iMinSAD[0]) {          if (bits < data->iMinSAD[0]) {
# Line 744  Line 750 
750  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)
751  {  {
752    
753          static int16_t in[64], coeff[64];          int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
754          int32_t sum, bits;          int32_t sum, bits;
755          VECTOR * current;          VECTOR * current;
756          const uint8_t * ptr;          const uint8_t * ptr;
# Line 950  Line 956 
956                                                          const uint32_t stride, const uint32_t iQuant, int rrv)                                                          const uint32_t stride, const uint32_t iQuant, int rrv)
957    
958  {  {
959            int offset = (x + y*stride)*8;
960          if(!rrv) {          if(!rrv) {
961                  uint32_t sadC = sad8(current->u + x*8 + y*stride*8,                  uint32_t sadC = sad8(current->u + offset,
962                                                  reference->u + x*8 + y*stride*8, stride);                                                  reference->u + offset, stride);
963                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
964                  sadC += sad8(current->v + (x + y*stride)*8,                  sadC += sad8(current->v + offset,
965                                                  reference->v + (x + y*stride)*8, stride);                                                  reference->v + offset, stride);
966                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
967                  return 1;                  return 1;
968    
969          } else {          } else {
970                  uint32_t sadC = sad16(current->u + x*16 + y*stride*16,                  uint32_t sadC = sad16(current->u + 2*offset,
971                                                  reference->u + x*16 + y*stride*16, stride, 256*4096);                                                  reference->u + 2*offset, stride, 256*4096);
972                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;
973                  sadC += sad16(current->v + (x + y*stride)*16,                  sadC += sad16(current->v + 2*offset,
974                                                  reference->v + (x + y*stride)*16, stride, 256*4096);                                                  reference->v + 2*offset, stride, 256*4096);
975                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;
976                  return 1;                  return 1;
977          }          }
# Line 995  Line 1002 
1002          uint32_t mb_width = pParam->mb_width;          uint32_t mb_width = pParam->mb_width;
1003          uint32_t mb_height = pParam->mb_height;          uint32_t mb_height = pParam->mb_height;
1004          const uint32_t iEdgedWidth = pParam->edged_width;          const uint32_t iEdgedWidth = pParam->edged_width;
1005          const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->global_flags);          const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags);
1006    
1007          uint32_t x, y;          uint32_t x, y;
1008          uint32_t iIntra = 0;          uint32_t iIntra = 0;
1009          int32_t quant = current->quant, sad00;          int32_t quant = current->quant, sad00;
1010            int skip_thresh = \
1011                    INITIAL_SKIP_THRESH * \
1012                    (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \
1013                    (current->vop_flags & XVID_VOP_MODEDECISION_BITS ? 2:1);
1014    
1015          // some pre-initialized thingies for SearchP          // some pre-initialized thingies for SearchP
1016          int32_t temp[8];          int32_t temp[8];
1017          VECTOR currentMV[5];          VECTOR currentMV[5];
1018          VECTOR currentQMV[5];          VECTOR currentQMV[5];
1019          int32_t iMinSAD[5];          int32_t iMinSAD[5];
1020            DECLARE_ALIGNED_MATRIX(dct_space, 2, 64, int16_t, CACHE_LINE);
1021          SearchData Data;          SearchData Data;
1022          memset(&Data, 0, sizeof(SearchData));          memset(&Data, 0, sizeof(SearchData));
1023          Data.iEdgedWidth = iEdgedWidth;          Data.iEdgedWidth = iEdgedWidth;
# Line 1015  Line 1027 
1027          Data.temp = temp;          Data.temp = temp;
1028          Data.iFcode = current->fcode;          Data.iFcode = current->fcode;
1029          Data.rounding = pParam->m_rounding_type;          Data.rounding = pParam->m_rounding_type;
1030          Data.qpel = pParam->m_quarterpel;          Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);
1031          Data.chroma = MotionFlags & PMV_CHROMA16;          Data.chroma = MotionFlags & XVID_ME_CHROMA16;
1032          Data.rrv = current->global_flags & XVID_REDUCED;          Data.rrv = (current->vop_flags & XVID_VOP_REDUCED ? 1:0);
1033            Data.dctSpace = dct_space;
1034    
1035          if ((current->global_flags & XVID_REDUCED)) {          if ((current->vop_flags & XVID_VOP_REDUCED)) {
1036                  mb_width = (pParam->width + 31) / 32;                  mb_width = (pParam->width + 31) / 32;
1037                  mb_height = (pParam->height + 31) / 32;                  mb_height = (pParam->height + 31) / 32;
1038                  Data.qpel = 0;                  Data.qpel = 0;
# Line 1052  Line 1065 
1065    
1066                          sad00 = pMB->sad16;                          sad00 = pMB->sad16;
1067    
1068                          if (!(current->global_flags & XVID_LUMIMASKING)) {                          if (pMB->dquant != 0) {
                                 pMB->dquant = NO_CHANGE;  
                         } else {  
                                 if (pMB->dquant != NO_CHANGE) {  
1069                                          quant += DQtab[pMB->dquant];                                          quant += DQtab[pMB->dquant];
1070                                          if (quant > 31) quant = 31;                                          if (quant > 31) quant = 31;
1071                                          else if (quant < 1) quant = 1;                                          else if (quant < 1) quant = 1;
1072                                  }                                  }
1073                          }  
1074                          pMB->quant = current->quant;                          pMB->quant = current->quant;
1075    
1076  //initial skip decision  //initial skip decision
1077  /* no early skip for GMC (global vector = skip vector is unknown!)  */  /* no early skip for GMC (global vector = skip vector is unknown!)  */
1078                          if (!(current->global_flags & XVID_GMC))        { /* no fast SKIP for S(GMC)-VOPs */                          if (!(current->vol_flags & XVID_VOL_GMC))       { /* no fast SKIP for S(GMC)-VOPs */
1079                                  if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH * (Data.rrv ? 4:1) )                                  if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)
1080                                          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)) {
1081                                                  SkipMacroblockP(pMB, sad00);                                                  SkipMacroblockP(pMB, sad00);
1082                                                  continue;                                                  continue;
# Line 1074  Line 1084 
1084                          }                          }
1085    
1086                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
1087                                                  y, MotionFlags, current->global_flags, pMB->quant,                                                  y, MotionFlags, current->vol_flags, pMB->quant,
1088                                                  &Data, pParam, pMBs, reference->mbs,                                                  &Data, pParam, pMBs, reference->mbs,
1089                                                  current->global_flags & XVID_INTER4V, pMB);                                                  current->vop_flags & XVID_VOP_INTER4V, pMB);
1090    
1091  /* 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?" */
1092                          if (!(current->global_flags & XVID_GMC))        {                          if (!(current->vol_flags & XVID_VOL_GMC || current->vop_flags & XVID_VOP_MODEDECISION_BITS)) {
1093                                  if ( pMB->dquant == NO_CHANGE && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) {                                  if ( pMB->dquant == 0 && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) {
                                         if (!(current->global_flags & XVID_MODEDECISION_BITS)) {  
1094                                                  if ( (100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1) )                                                  if ( (100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1) )
1095                                                          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))
1096                                                                  SkipMacroblockP(pMB, sad00);                                                                  SkipMacroblockP(pMB, sad00);
                                         } else { // BITS mode decision  
                                                 if (pMB->sad16 > 10)  
                                                         SkipMacroblockP(pMB, sad00);  // more than 10 bits would be used for this MB - skip  
   
                                         }  
1097                                  }                                  }
1098                          }                          }
1099                          if (pMB->mode == MODE_INTRA)                          if (pMB->mode == MODE_INTRA)
# Line 1097  Line 1101 
1101                  }                  }
1102          }          }
1103    
1104          if (current->global_flags & XVID_GMC )  /* GMC only for S(GMC)-VOPs */          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
         {  
1105                  current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV);                  current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV);
1106          }  
1107          return 0;          return 0;
1108  }  }
1109    
# Line 1172  Line 1175 
1175                  const int x, const int y,                  const int x, const int y,
1176                  const MBParam * const pParam,                  const MBParam * const pParam,
1177                  const uint32_t MotionFlags,                  const uint32_t MotionFlags,
1178                  const uint32_t GlobalFlags)                  const uint32_t VopFlags)
1179  {  {
1180    
1181          int mode = MODE_INTER;          int mode = MODE_INTER;
1182    
1183          if (!(GlobalFlags & XVID_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision          if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision
 //              int intra = 0;  
1184                  int sad;                  int sad;
1185                  int InterBias = MV16_INTER_BIAS;                  int InterBias = MV16_INTER_BIAS;
1186                  if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +                  if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
1187                          Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) {                          Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) {
1188                                  mode = 0; //inter                          mode = MODE_INTER;
1189                                  sad = Data->iMinSAD[0];                                  sad = Data->iMinSAD[0];
1190                  } else {                  } else {
1191                          mode = MODE_INTER4V;                          mode = MODE_INTER4V;
# Line 1211  Line 1213 
1213                                  dev16(Data->Cur + 8*Data->iEdgedWidth, Data->iEdgedWidth) +                                  dev16(Data->Cur + 8*Data->iEdgedWidth, Data->iEdgedWidth) +
1214                                  dev16(Data->Cur+8+8*Data->iEdgedWidth, Data->iEdgedWidth);                                  dev16(Data->Cur+8+8*Data->iEdgedWidth, Data->iEdgedWidth);
1215    
1216                          if (deviation < (sad - InterBias))  return MODE_INTRA;// intra                          if (deviation < (sad - InterBias)) return MODE_INTRA;
1217                  }                  }
1218                  return mode;                  return mode;
1219    
# Line 1220  Line 1222 
1222                  int bits, intra, i;                  int bits, intra, i;
1223                  VECTOR backup[5], *v;                  VECTOR backup[5], *v;
1224                  Data->lambda16 = iQuant;                  Data->lambda16 = iQuant;
1225                  Data->lambda8 = pParam->m_quant_type;          Data->lambda8 = (pParam->vol_flags & XVID_VOL_MPEGQUANT)?1:0;
1226    
1227                  v = Data->qpel ? Data->currentQMV : Data->currentMV;                  v = Data->qpel ? Data->currentQMV : Data->currentMV;
1228                  for (i = 0; i < 5; i++) {                  for (i = 0; i < 5; i++) {
# Line 1232  Line 1234 
1234                  if (bits == 0) return MODE_INTER; // quick stop                  if (bits == 0) return MODE_INTER; // quick stop
1235    
1236                  if (inter4v) {                  if (inter4v) {
1237                          int inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup);                          int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup);
1238                          if (inter4v < bits) { Data->iMinSAD[0] = bits = inter4v; mode = MODE_INTER4V; }                          if (bits_inter4v < bits) { Data->iMinSAD[0] = bits = bits_inter4v; mode = MODE_INTER4V; }
1239                  }                  }
1240    
1241    
# Line 1254  Line 1256 
1256                  const int x,                  const int x,
1257                  const int y,                  const int y,
1258                  const uint32_t MotionFlags,                  const uint32_t MotionFlags,
1259                  const uint32_t GlobalFlags,                  const uint32_t VopFlags,
1260                  const uint32_t iQuant,                  const uint32_t iQuant,
1261                  SearchData * const Data,                  SearchData * const Data,
1262                  const MBParam * const pParam,                  const MBParam * const pParam,
# Line 1289  Line 1291 
1291          Data->lambda8 = lambda_vec8[iQuant];          Data->lambda8 = lambda_vec8[iQuant];
1292          Data->qpel_precision = 0;          Data->qpel_precision = 0;
1293    
1294          if (pMB->dquant != NO_CHANGE) inter4v = 0;          if (pMB->dquant != 0) inter4v = 0;
1295    
1296          for(i = 0; i < 5; i++)          memset(Data->currentMV, 0, 5*sizeof(VECTOR));
                 Data->currentMV[i].x = Data->currentMV[i].y = 0;  
1297    
1298          if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);          if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1299          else Data->predMV = pmv[0];          else Data->predMV = pmv[0];
# Line 1304  Line 1305 
1305          Data->iMinSAD[3] = pMB->sad8[2];          Data->iMinSAD[3] = pMB->sad8[2];
1306          Data->iMinSAD[4] = pMB->sad8[3];          Data->iMinSAD[4] = pMB->sad8[3];
1307    
1308          if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) || (x | y)) {          if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) || (x | y)) {
1309                  threshA = Data->temp[0]; // that's where we keep this SAD atm                  threshA = Data->temp[0]; // that's where we keep this SAD atm
1310                  if (threshA < 512) threshA = 512;                  if (threshA < 512) threshA = 512;
1311                  else if (threshA > 1024) threshA = 1024;                  else if (threshA > 1024) threshA = 1024;
# Line 1330  Line 1331 
1331          if ((Data->iMinSAD[0] <= threshA) ||          if ((Data->iMinSAD[0] <= threshA) ||
1332                          (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&                          (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&
1333                          (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) {                          (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) {
1334                  if (!(GlobalFlags & XVID_MODEDECISION_BITS)) inter4v = 0;       }                  if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) inter4v = 0;      }
1335          else {          else {
1336    
1337                  MainSearchFunc * MainSearchPtr;                  MainSearchFunc * MainSearchPtr;
1338                  if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;                  if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;
1339                  else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1340                          else MainSearchPtr = DiamondSearch;                          else MainSearchPtr = DiamondSearch;
1341    
1342                  MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection);                  MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
# Line 1344  Line 1345 
1345          note that this search is/might be done in halfpel positions,          note that this search is/might be done in halfpel positions,
1346          which makes it more different than the diamond above */          which makes it more different than the diamond above */
1347    
1348                  if (MotionFlags & PMV_EXTSEARCH16) {                  if (MotionFlags & XVID_ME_EXTSEARCH16) {
1349                          int32_t bSAD;                          int32_t bSAD;
1350                          VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];                          VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];
1351                          if (Data->rrv) {                          if (Data->rrv) {
# Line 1375  Line 1376 
1376                  }                  }
1377          }          }
1378    
1379          if (MotionFlags & PMV_HALFPELREFINE16)          if (MotionFlags & XVID_ME_HALFPELREFINE16)
1380                  if ((!(MotionFlags & HALFPELREFINE16_BITS)) || Data->iMinSAD[0] < 200*(int)iQuant)                  if ((!(MotionFlags & XVID_ME_HALFPELREFINE16_BITS)) || Data->iMinSAD[0] < 200*(int)iQuant)
1381                          SubpelRefine(Data);                          SubpelRefine(Data);
1382    
1383          for(i = 0; i < 5; i++) {          for(i = 0; i < 5; i++) {
# Line 1384  Line 1385 
1385                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
1386          }          }
1387    
1388          if (MotionFlags & PMV_QUARTERPELREFINE16)          if (MotionFlags & XVID_ME_QUARTERPELREFINE16) {
1389                  if ((!(MotionFlags & QUARTERPELREFINE16_BITS)) || (Data->iMinSAD[0] < 200*(int)iQuant)) {  
                         Data->qpel_precision = 1;  
1390                          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,
1391                                          pParam->width, pParam->height, Data->iFcode, 1, 0);                                          pParam->width, pParam->height, Data->iFcode, 1, 0);
1392    
1393                    if ((!(MotionFlags & XVID_ME_QUARTERPELREFINE16_BITS)) || (Data->iMinSAD[0] < 200*(int)iQuant)) {
1394                            Data->qpel_precision = 1;
1395                          SubpelRefine(Data);                          SubpelRefine(Data);
1396                  }                  }
1397            }
1398    
1399          if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0;          if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0;
1400    
1401          if (inter4v && (!(GlobalFlags & XVID_MODEDECISION_BITS) ||          if (inter4v && (!(VopFlags & XVID_VOP_MODEDECISION_BITS) ||
1402                          (!(MotionFlags & QUARTERPELREFINE8_BITS)) || (!(MotionFlags & HALFPELREFINE8_BITS)) ||                          (!(MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS)) || (!(MotionFlags & XVID_ME_HALFPELREFINE8_BITS)) ||
1403                          ((!(MotionFlags & EXTSEARCH_BITS)) && (!(MotionFlags&PMV_EXTSEARCH8)) ))) {                          ((!(MotionFlags & XVID_ME_EXTSEARCH_BITS)) && (!(MotionFlags&XVID_ME_EXTSEARCH8)) ))) {
1404                  // if decision is BITS-based and all refinement steps will be done in BITS domain, there is no reason to call this loop                  // if decision is BITS-based and all refinement steps will be done in BITS domain, there is no reason to call this loop
1405    
1406                  SearchData Data8;                  SearchData Data8;
# Line 1408  Line 1411 
1411                  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);
1412                  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);
1413    
1414                  if ((Data->chroma) && (!(GlobalFlags & XVID_MODEDECISION_BITS))) {                  if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_BITS))) {
1415                          // chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, there is no reason to compute it                          // chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, there is no reason to compute it
1416                          int sumx = 0, sumy = 0;                          int sumx = 0, sumy = 0;
1417                          const int div = 1 + Data->qpel;                          const int div = 1 + Data->qpel;
# Line 1424  Line 1427 
1427                  }                  }
1428          }          }
1429    
1430          inter4v = ModeDecision(iQuant, Data, inter4v, pMB, pMBs, x, y, pParam, MotionFlags, GlobalFlags);          inter4v = ModeDecision(iQuant, Data, inter4v, pMB, pMBs, x, y, pParam, MotionFlags, VopFlags);
1431    
1432          if (Data->rrv) {          if (Data->rrv) {
1433                          Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);                          Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);
# Line 1483  Line 1486 
1486    
1487          *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;          *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;
1488    
1489          if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8|PMV_QUARTERPELREFINE8)) {          if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {
1490                  if (Data->rrv) i = 2; else i = 1;                  if (Data->rrv) i = 2; else i = 1;
1491    
1492                  Data->Ref = OldData->Ref + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->Ref = OldData->Ref + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
# Line 1500  Line 1503 
1503                  if (!Data->rrv) CheckCandidate = CheckCandidate8;                  if (!Data->rrv) CheckCandidate = CheckCandidate8;
1504                  else CheckCandidate = CheckCandidate16no4v;                  else CheckCandidate = CheckCandidate16no4v;
1505    
1506                  if (MotionFlags & PMV_EXTSEARCH8 && (!(MotionFlags & EXTSEARCH_BITS))) {                  if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_BITS))) {
1507                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1508    
1509                          MainSearchFunc *MainSearchPtr;                          MainSearchFunc *MainSearchPtr;
1510                          if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch;                          if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = SquareSearch;
1511                                  else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;                                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;
1512                                          else MainSearchPtr = DiamondSearch;                                          else MainSearchPtr = DiamondSearch;
1513    
1514                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255);                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255);
# Line 1516  Line 1519 
1519                          }                          }
1520                  }                  }
1521    
1522                  if (MotionFlags & PMV_HALFPELREFINE8) {                  if (MotionFlags & XVID_ME_HALFPELREFINE8) {
1523                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1524    
1525                          SubpelRefine(Data); // perform halfpel refine of current best vector                          SubpelRefine(Data); // perform halfpel refine of current best vector
# Line 1527  Line 1530 
1530                          }                          }
1531                  }                  }
1532    
1533                  if (Data->qpel && MotionFlags & PMV_QUARTERPELREFINE8) {                  if (Data->qpel && MotionFlags & XVID_ME_QUARTERPELREFINE8) {
1534                                  Data->qpel_precision = 1;                                  Data->qpel_precision = 1;
1535                                  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,
1536                                          pParam->width, pParam->height, Data->iFcode, 1, 0);                                          pParam->width, pParam->height, Data->iFcode, 1, 0);
# Line 1651  Line 1654 
1654                  CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);                  CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1655          }          }
1656    
1657          if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;
1658          else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;          else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1659                  else MainSearchPtr = DiamondSearch;                  else MainSearchPtr = DiamondSearch;
1660    
1661          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection);          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
# Line 1712  Line 1715 
1715    
1716          for (k = 0; k < 4; k++) {          for (k = 0; k < 4; k++) {
1717                  dy += Data->directmvF[k].y / div;                  dy += Data->directmvF[k].y / div;
1718                  dx += Data->directmvF[0].x / div;                  dx += Data->directmvF[k].x / div;
1719                  b_dy += Data->directmvB[0].y / div;                  b_dy += Data->directmvB[k].y / div;
1720                  b_dx += Data->directmvB[0].x / div;                  b_dx += Data->directmvB[k].x / div;
1721          }          }
1722    
1723          dy = (dy >> 3) + roundtab_76[dy & 0xf];          dy = (dy >> 3) + roundtab_76[dy & 0xf];
# Line 1734  Line 1737 
1737                                          b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,                                          b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,
1738                                          stride);                                          stride);
1739    
1740          if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) pMB->mode = MODE_DIRECT_NONE_MV; //skipped          if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) {
1741                    pMB->mode = MODE_DIRECT_NONE_MV; //skipped
1742                    for (k = 0; k < 4; k++) {
1743                            pMB->qmvs[k] = pMB->mvs[k];
1744                            pMB->b_qmvs[k] = pMB->b_mvs[k];
1745                    }
1746            }
1747  }  }
1748    
1749  static __inline uint32_t  static __inline uint32_t
# Line 1823  Line 1832 
1832                  }                  }
1833          }          }
1834    
1835            *Data->iMinSAD += Data->lambda16;
1836          skip_sad = *Data->iMinSAD;          skip_sad = *Data->iMinSAD;
1837    
1838  //      DIRECT MODE DELTA VECTOR SEARCH.  //      DIRECT MODE DELTA VECTOR SEARCH.
1839  //      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
1840    
1841          if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;
1842                  else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1843                          else MainSearchPtr = DiamondSearch;                          else MainSearchPtr = DiamondSearch;
1844    
1845          MainSearchPtr(0, 0, Data, 255);          MainSearchPtr(0, 0, Data, 255);
# Line 2038  Line 2048 
2048          Data.currentMV = currentMV; Data.currentQMV = currentQMV;          Data.currentMV = currentMV; Data.currentQMV = currentQMV;
2049          Data.iMinSAD = &iMinSAD;          Data.iMinSAD = &iMinSAD;
2050          Data.lambda16 = lambda_vec16[frame->quant];          Data.lambda16 = lambda_vec16[frame->quant];
2051          Data.qpel = pParam->m_quarterpel;          Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL;
2052          Data.rounding = 0;          Data.rounding = 0;
2053          Data.chroma = frame->motion_flags & PMV_CHROMA8;          Data.chroma = frame->motion_flags & XVID_ME_CHROMA8;
2054          Data.temp = temp;          Data.temp = temp;
2055    
2056          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)
# Line 2148  Line 2158 
2158  {  {
2159    
2160          int i, mask;          int i, mask;
2161            int quarterpel = (pParam->vol_flags & XVID_VOL_QUARTERPEL)? 1: 0;
2162          VECTOR pmv[3];          VECTOR pmv[3];
2163          MACROBLOCK * pMB = &pMBs[x + y * pParam->mb_width];          MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width];
2164    
2165          for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;          for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;
2166    
# Line 2163  Line 2174 
2174                          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
2175    
2176          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,
2177                                  pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, Data->rrv);          pParam->width, pParam->height, Data->iFcode - quarterpel, 0, 0);
2178    
2179          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;
2180          Data->Ref = pRef + (x + y * pParam->edged_width) * 16;          Data->Ref = pRef + (x + y * pParam->edged_width) * 16;
# Line 2175  Line 2186 
2186          pmv[0].x = pmv[0].y = 0;          pmv[0].x = pmv[0].y = 0;
2187    
2188          CheckCandidate32I(0, 0, 255, &i, Data);          CheckCandidate32I(0, 0, 255, &i, Data);
2189            Data->iMinSAD[1] -= 50;
2190            Data->iMinSAD[2] -= 50;
2191            Data->iMinSAD[3] -= 50;
2192            Data->iMinSAD[4] -= 50;
2193    
2194          if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) {          if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) {
2195    
2196                  if (!(mask = make_mask(pmv, 1)))                  if (!(mask = make_mask(pmv, 1)))
2197                          CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data);                          CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data);
2198                  if (!(mask = make_mask(pmv, 2)))                  if (!(mask = make_mask(pmv, 2)))
2199                          CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data);                          CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data);
2200    
2201                  if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) // diamond only if needed                  if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) // diamond only if needed
2202                          DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);                          DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);
2203            }
2204    
2205                  for (i = 0; i < 4; i++) {                  for (i = 0; i < 4; i++) {
2206                          MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width];                          MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width];
# Line 2193  Line 2209 
2209                          MB->sad16 = Data->iMinSAD[i+1];                          MB->sad16 = Data->iMinSAD[i+1];
2210                  }                  }
2211          }          }
 }  
2212    
2213  #define INTRA_BIAS              2500  #define INTRA_THRESH    2400
2214  #define INTRA_THRESH    1500  #define INTER_THRESH    1100
 #define INTER_THRESH    1400  
2215    
2216  int  int
2217  MEanalysis(     const IMAGE * const pRef,  MEanalysis(     const IMAGE * const pRef,
2218                          FRAMEINFO * const Current,                          const FRAMEINFO * const Current,
2219                          MBParam * const pParam,                          const MBParam * const pParam,
2220                          int maxIntra, //maximum number if non-I frames                          const int maxIntra, //maximum number if non-I frames
2221                          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
2222                          int bCount) // number of B frames in a row                          const int bCount,  // number of B frames in a row
2223                            const int b_thresh)
2224  {  {
2225          uint32_t x, y, intra = 0;          uint32_t x, y, intra = 0;
2226          int sSAD = 0;          int sSAD = 0;
2227          MACROBLOCK * const pMBs = Current->mbs;          MACROBLOCK * const pMBs = Current->mbs;
2228          const IMAGE * const pCurrent = &Current->image;          const IMAGE * const pCurrent = &Current->image;
2229          int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH;          int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + 10*b_thresh;
2230            int s = 0, blocks = 0;
2231    
2232          int32_t iMinSAD[5], temp[5];          int32_t iMinSAD[5], temp[5];
2233          VECTOR currentMV[5];          VECTOR currentMV[5];
# Line 2220  Line 2236 
2236          Data.currentMV = currentMV;          Data.currentMV = currentMV;
2237          Data.iMinSAD = iMinSAD;          Data.iMinSAD = iMinSAD;
2238          Data.iFcode = Current->fcode;          Data.iFcode = Current->fcode;
         Data.rrv = Current->global_flags & XVID_REDUCED;  
2239          Data.temp = temp;          Data.temp = temp;
2240          CheckCandidate = CheckCandidate32I;          CheckCandidate = CheckCandidate32I;
2241    
2242          if (intraCount != 0 && intraCount < 10) // we're right after an I frame          if (intraCount != 0 && intraCount < 10) // we're right after an I frame
2243                  IntraThresh += 4 * (intraCount - 10) * (intraCount - 10);                  IntraThresh += 8 * (intraCount - 10) * (intraCount - 10);
2244          else          else
2245                  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
2246                          IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra;                          IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra;
2247    
2248          InterThresh += 400 * (1 - bCount);          InterThresh -= (350 - 8*b_thresh) * bCount;
2249          if (InterThresh < 300) InterThresh = 300;          if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh;
2250    
2251          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();
2252    
2253          for (y = 1; y < pParam->mb_height-1; y += 2) {          for (y = 1; y < pParam->mb_height-1; y += 2) {
2254                  for (x = 1; x < pParam->mb_width-1; x += 2) {                  for (x = 1; x < pParam->mb_width-1; x += 2) {
2255                          int i;                          int i;
2256                            blocks += 4;
2257    
2258                          if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV;                          if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV;
2259                            else { //extrapolation of the vector found for last frame
2260                                    pMBs[x + y * pParam->mb_width].mvs[0].x =
2261                                            (pMBs[x + y * pParam->mb_width].mvs[0].x * (bCount+1) ) / bCount;
2262                                    pMBs[x + y * pParam->mb_width].mvs[0].y =
2263                                            (pMBs[x + y * pParam->mb_width].mvs[0].y * (bCount+1) ) / bCount;
2264                            }
2265    
2266                          MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data);                          MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data);
2267    
# Line 2251  Line 2273 
2273                                                                          pParam->edged_width);                                                                          pParam->edged_width);
2274                                          if (dev + IntraThresh < pMB->sad16) {                                          if (dev + IntraThresh < pMB->sad16) {
2275                                                  pMB->mode = MODE_INTRA;                                                  pMB->mode = MODE_INTRA;
2276                                                  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;
2277                                          }                                          }
2278                                  }                                  }
2279                                    if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) s++;
2280    
2281                                  sSAD += pMB->sad16;                                  sSAD += pMB->sad16;
2282                          }                          }
2283                  }                  }
2284          }          }
2285          sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2);  
2286  //      if (sSAD > IntraThresh + INTRA_BIAS) return I_VOP;          sSAD /= blocks;
2287            s = (10*s) / blocks;
2288    
2289            if (s > 5) sSAD += (s - 4) * (180 - 2*b_thresh); //static block - looks bad when in bframe...
2290    
2291          if (sSAD > InterThresh ) return P_VOP;          if (sSAD > InterThresh ) return P_VOP;
2292          emms();          emms();
2293          return B_VOP;          return B_VOP;
   
2294  }  }
2295    
2296    
# Line 2303  Line 2330 
2330    
2331  // filter mask of all blocks  // filter mask of all blocks
2332    
2333          for (my = 1; my < MBh-1; my++)          for (my = 1; my < (uint32_t)MBh-1; my++)
2334          for (mx = 1; mx < MBw-1; mx++)          for (mx = 1; mx < (uint32_t)MBw-1; mx++)
2335          {          {
2336                  const int mbnum = mx + my * MBw;                  const int mbnum = mx + my * MBw;
2337                  const MACROBLOCK *pMB = &pMBs[mbnum];                  const MACROBLOCK *pMB = &pMBs[mbnum];
# Line 2320  Line 2347 
2347                          MBmask[mbnum]=1;                          MBmask[mbnum]=1;
2348          }          }
2349    
2350          for (my = 1; my < MBh-1; my++)          for (my = 1; my < (uint32_t)MBh-1; my++)
2351          for (mx = 1; mx < MBw-1; mx++)          for (mx = 1; mx < (uint32_t)MBw-1; mx++)
2352          {          {
2353                  const uint8_t *const pCur = current->image.y + 16*my*pParam->edged_width + 16*mx;                  const uint8_t *const pCur = current->image.y + 16*my*pParam->edged_width + 16*mx;
2354    
# Line 2329  Line 2356 
2356                  if (!MBmask[mbnum])                  if (!MBmask[mbnum])
2357                          continue;                          continue;
2358    
2359                  if (sad16 ( pCur, pCur+1 , pParam->edged_width, 65536) <= grad )                  if (sad16 ( pCur, pCur+1 , pParam->edged_width, 65536) <= (uint32_t)grad )
2360                          MBmask[mbnum] = 0;                          MBmask[mbnum] = 0;
2361                  if (sad16 ( pCur, pCur+pParam->edged_width, pParam->edged_width, 65536) <= grad )                  if (sad16 ( pCur, pCur+pParam->edged_width, pParam->edged_width, 65536) <= (uint32_t)grad )
2362                          MBmask[mbnum] = 0;                          MBmask[mbnum] = 0;
2363    
2364          }          }
# Line 2342  Line 2369 
2369    
2370          a = b = c = n = 0;          a = b = c = n = 0;
2371          DtimesF[0] = DtimesF[1] = DtimesF[2] = DtimesF[3] = 0.;          DtimesF[0] = DtimesF[1] = DtimesF[2] = DtimesF[3] = 0.;
2372          for (my = 0; my < MBh; my++)          for (my = 0; my < (uint32_t)MBh; my++)
2373                  for (mx = 0; mx < MBw; mx++)                  for (mx = 0; mx < (uint32_t)MBw; mx++)
2374                  {                  {
2375                          const int mbnum = mx + my * MBw;                          const int mbnum = mx + my * MBw;
2376                          const MACROBLOCK *pMB = &pMBs[mbnum];                          const MACROBLOCK *pMB = &pMBs[mbnum];
# Line 2380  Line 2407 
2407    
2408          meanx = meany = 0.;          meanx = meany = 0.;
2409          oldnum = 0;          oldnum = 0;
2410          for (my = 0; my < MBh; my++)          for (my = 0; my < (uint32_t)MBh; my++)
2411                  for (mx = 0; mx < MBw; mx++)                  for (mx = 0; mx < (uint32_t)MBw; mx++)
2412                  {                  {
2413                          const int mbnum = mx + my * MBw;                          const int mbnum = mx + my * MBw;
2414                          const MACROBLOCK *pMB = &pMBs[mbnum];                          const MACROBLOCK *pMB = &pMBs[mbnum];
# Line 2409  Line 2436 
2436          fprintf(stderr,"meanx = %8.5f  meany = %8.5f   %d\n",meanx,meany, oldnum);          fprintf(stderr,"meanx = %8.5f  meany = %8.5f   %d\n",meanx,meany, oldnum);
2437  */  */
2438          num = 0;          num = 0;
2439          for (my = 0; my < MBh; my++)          for (my = 0; my < (uint32_t)MBh; my++)
2440                  for (mx = 0; mx < MBw; mx++)                  for (mx = 0; mx < (uint32_t)MBw; mx++)
2441                  {                  {
2442                          const int mbnum = mx + my * MBw;                          const int mbnum = mx + my * MBw;
2443                          const MACROBLOCK *pMB = &pMBs[mbnum];                          const MACROBLOCK *pMB = &pMBs[mbnum];
# Line 2474  Line 2501 
2501                  if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0)                  if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0)
2502                          return 0; //quick stop                          return 0; //quick stop
2503    
2504                  if (MotionFlags & (HALFPELREFINE16_BITS | EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search                  if (MotionFlags & (XVID_ME_HALFPELREFINE16_BITS | XVID_ME_EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search
2505                          for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i];                          for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i];
2506                          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,
2507                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);
# Line 2492  Line 2519 
2519                  }                  }
2520          }          }
2521    
2522          if (MotionFlags&EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection);          if (MotionFlags&XVID_ME_EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
2523    
2524          if (MotionFlags&HALFPELREFINE16_BITS) SubpelRefine(Data);          if (MotionFlags&XVID_ME_HALFPELREFINE16_BITS) SubpelRefine(Data);
2525    
2526          if (Data->qpel) {          if (Data->qpel) {
2527                  if (MotionFlags&(EXTSEARCH_BITS | HALFPELREFINE16_BITS)) { // there was halfpel-precision search                  if (MotionFlags&(XVID_ME_EXTSEARCH_BITS | XVID_ME_HALFPELREFINE16_BITS)) { // there was halfpel-precision search
2528                          for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) {                          for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) {
2529                                  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
2530                                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;                                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
# Line 2508  Line 2535 
2535                          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,
2536                                          pParam->width, pParam->height, Data->iFcode, 1, 0);                                          pParam->width, pParam->height, Data->iFcode, 1, 0);
2537                  }                  }
2538                  if (MotionFlags&QUARTERPELREFINE16_BITS) SubpelRefine(Data);                  if (MotionFlags&XVID_ME_QUARTERPELREFINE16_BITS) SubpelRefine(Data);
2539          }          }
2540    
2541          if (MotionFlags&CHECKPREDICTION_BITS) { //let's check vector equal to prediction          if (MotionFlags&XVID_ME_CHECKPREDICTION_BITS) { //let's check vector equal to prediction
2542                  VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV;                  VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV;
2543                  if (!(Data->predMV.x == v->x && Data->predMV.y == v->y))                  if (!(Data->predMV.x == v->x && Data->predMV.y == v->y))
2544                          CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data);                          CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data);
# Line 2531  Line 2558 
2558          int cbp = 0, bits = 0, t = 0, i, iDirection;          int cbp = 0, bits = 0, t = 0, i, iDirection;
2559          SearchData Data2, *Data8 = &Data2;          SearchData Data2, *Data8 = &Data2;
2560          int sumx = 0, sumy = 0;          int sumx = 0, sumy = 0;
2561          int16_t in[64], coeff[64];          int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64;
2562    
2563          memcpy(Data8, Data, sizeof(SearchData));          memcpy(Data8, Data, sizeof(SearchData));
2564          CheckCandidate = CheckCandidateBits8;          CheckCandidate = CheckCandidateBits8;
# Line 2572  Line 2599 
2599                  }                  }
2600    
2601                  if (Data8->qpel) {                  if (Data8->qpel) {
2602                          if (MotionFlags&HALFPELREFINE8_BITS || (MotionFlags&PMV_EXTSEARCH8 && MotionFlags&EXTSEARCH_BITS)) { // halfpixel motion search follows                          if (MotionFlags&XVID_ME_HALFPELREFINE8_BITS || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_BITS)) { // halfpixel motion search follows
2603                                  int32_t s = *Data8->iMinSAD;                                  int32_t s = *Data8->iMinSAD;
2604                                  Data8->currentMV->x = Data8->currentQMV->x/2;                                  Data8->currentMV->x = Data8->currentQMV->x/2;
2605                                  Data8->currentMV->y = Data8->currentQMV->y/2;                                  Data8->currentMV->y = Data8->currentQMV->y/2;
# Line 2583  Line 2610 
2610                                  if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1)                                  if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1)
2611                                          CheckCandidateBits8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8);                                          CheckCandidateBits8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8);
2612    
2613                                  if (MotionFlags & PMV_EXTSEARCH8 && MotionFlags & EXTSEARCH_BITS)                                  if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS)
2614                                          SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);                                          SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);
2615    
2616                                  if (MotionFlags & HALFPELREFINE8_BITS) SubpelRefine(Data8);                                  if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8);
2617    
2618                                  if(s > *Data8->iMinSAD) { //we have found a better match                                  if(s > *Data8->iMinSAD) { //we have found a better match
2619                                          Data8->currentQMV->x = 2*Data8->currentMV->x;                                          Data8->currentQMV->x = 2*Data8->currentMV->x;
# Line 2598  Line 2625 
2625                                                          pParam->width, pParam->height, Data8->iFcode, 1, 0);                                                          pParam->width, pParam->height, Data8->iFcode, 1, 0);
2626    
2627                          }                          }
2628                          if (MotionFlags & QUARTERPELREFINE8_BITS) SubpelRefine(Data8);                          if (MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS) SubpelRefine(Data8);
2629    
2630                  } else // not qpel                  } else // not qpel
2631                          if (MotionFlags & HALFPELREFINE8_BITS) SubpelRefine(Data8); //halfpel mode, halfpel refinement                          if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8); //halfpel mode, halfpel refinement
2632    
2633                  //checking vector equal to predicion                  //checking vector equal to predicion
2634                  if (i != 0 && MotionFlags & CHECKPREDICTION_BITS) {                  if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_BITS) {
2635                          const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV;                          const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV;
2636                          if (!(Data8->predMV.x == v->x && Data8->predMV.y == v->y))                          if (!(Data8->predMV.x == v->x && Data8->predMV.y == v->y))
2637                                  CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8);                                  CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8);
# Line 2658  Line 2685 
2685                                  bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);                                  bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
2686                                  cbp |= 1 << (5 - 5);                                  cbp |= 1 << (5 - 5);
2687                          }                          }
2688                          bits += cbpy_tab[15-(cbp>>2)].len;                          bits += xvid_cbpy_tab[15-(cbp>>2)].len;
2689                          bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len;                          bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len;
2690                  }                  }
2691          }          }
# Line 2673  Line 2700 
2700          int bits = 1; //this one is ac/dc prediction flag. always 1.          int bits = 1; //this one is ac/dc prediction flag. always 1.
2701          int cbp = 0, i, t, dc = 0, b_dc = 1024;          int cbp = 0, i, t, dc = 0, b_dc = 1024;
2702          const uint32_t iQuant = Data->lambda16;          const uint32_t iQuant = Data->lambda16;
2703          int16_t in[64], coeff[64];          int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64;
2704    
2705          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
2706                  uint32_t iDcScaler = get_dc_scaler(iQuant, 1);                  uint32_t iDcScaler = get_dc_scaler(iQuant, 1);
# Line 2708  Line 2735 
2735    
2736                  bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len;                  bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len;
2737                  if (t != 0) cbp |= 1 << (5 - 4);                  if (t != 0) cbp |= 1 << (5 - 4);
                 Data->temp[4] = t;  
2738    
2739                  if (bits < Data->iMinSAD[0]) {                  if (bits < Data->iMinSAD[0]) {
2740                            iDcScaler = get_dc_scaler(iQuant, 1);
2741                          //chroma V                          //chroma V
2742                          transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2);                          transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2);
2743                          fdct(in);                          fdct(in);
# Line 2721  Line 2748 
2748                          bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len;                          bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len;
2749                          if (t != 0) cbp |= 1 << (5 - 5);                          if (t != 0) cbp |= 1 << (5 - 5);
2750    
2751                          Data->temp[5] = t;                          bits += xvid_cbpy_tab[cbp>>2].len;
2752                            bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len;
                         bits += t = cbpy_tab[cbp>>2].len;  
                         Data->temp[6] = t;  
   
                         bits += t = mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len;  
                         Data->temp[7] = t;  
   
2753                  }                  }
2754          }          }
   
2755          return bits;          return bits;
2756  }  }

Legend:
Removed from v.872  
changed lines
  Added in v.974

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