[svn] / trunk / xvidcore / src / motion / estimation_pvop.c Repository:
ViewVC logotype

Diff of /trunk/xvidcore/src/motion/estimation_pvop.c

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

revision 1478, Thu Jul 8 07:12:54 2004 UTC revision 1682, Fri Feb 24 08:46:22 2006 UTC
# Line 21  Line 21 
21   *  along with this program ; if not, write to the Free Software   *  along with this program ; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23   *   *
24   * $Id: estimation_pvop.c,v 1.8 2004-07-08 07:12:54 syskin Exp $   * $Id: estimation_pvop.c,v 1.19 2006-02-24 08:46:22 syskin Exp $
25   *   *
26   ****************************************************************************/   ****************************************************************************/
27    
# Line 39  Line 39 
39  #include "motion.h"  #include "motion.h"
40  #include "sad.h"  #include "sad.h"
41  #include "motion_inlines.h"  #include "motion_inlines.h"
42    #include "motion_smp.h"
43    
44    
45  static const int xvid_me_lambda_vec8[32] =  static const int xvid_me_lambda_vec8[32] =
46          {     0    ,(int)(1.00235 * NEIGH_TEND_8X8 + 0.5),          {     0    ,(int)(1.0 * NEIGH_TEND_8X8 + 0.5),
47          (int)(1.15582*NEIGH_TEND_8X8 + 0.5), (int)(1.31976*NEIGH_TEND_8X8 + 0.5),          (int)(2.0*NEIGH_TEND_8X8 + 0.5), (int)(3.0*NEIGH_TEND_8X8 + 0.5),
48          (int)(1.49591*NEIGH_TEND_8X8 + 0.5), (int)(1.68601*NEIGH_TEND_8X8 + 0.5),          (int)(4.0*NEIGH_TEND_8X8 + 0.5), (int)(5.0*NEIGH_TEND_8X8 + 0.5),
49          (int)(1.89187*NEIGH_TEND_8X8 + 0.5), (int)(2.11542*NEIGH_TEND_8X8 + 0.5),          (int)(6.0*NEIGH_TEND_8X8 + 0.5), (int)(7.0*NEIGH_TEND_8X8 + 0.5),
50          (int)(2.35878*NEIGH_TEND_8X8 + 0.5), (int)(2.62429*NEIGH_TEND_8X8 + 0.5),          (int)(8.0*NEIGH_TEND_8X8 + 0.5), (int)(9.0*NEIGH_TEND_8X8 + 0.5),
51          (int)(2.91455*NEIGH_TEND_8X8 + 0.5), (int)(3.23253*NEIGH_TEND_8X8 + 0.5),          (int)(10.0*NEIGH_TEND_8X8 + 0.5), (int)(11.0*NEIGH_TEND_8X8 + 0.5),
52          (int)(3.58158*NEIGH_TEND_8X8 + 0.5), (int)(3.96555*NEIGH_TEND_8X8 + 0.5),          (int)(12.0*NEIGH_TEND_8X8 + 0.5), (int)(13.0*NEIGH_TEND_8X8 + 0.5),
53          (int)(4.38887*NEIGH_TEND_8X8 + 0.5), (int)(4.85673*NEIGH_TEND_8X8 + 0.5),          (int)(14.0*NEIGH_TEND_8X8 + 0.5), (int)(15.0*NEIGH_TEND_8X8 + 0.5),
54          (int)(5.37519*NEIGH_TEND_8X8 + 0.5), (int)(5.95144*NEIGH_TEND_8X8 + 0.5),          (int)(16.0*NEIGH_TEND_8X8 + 0.5), (int)(17.0*NEIGH_TEND_8X8 + 0.5),
55          (int)(6.59408*NEIGH_TEND_8X8 + 0.5), (int)(7.31349*NEIGH_TEND_8X8 + 0.5),          (int)(18.0*NEIGH_TEND_8X8 + 0.5), (int)(19.0*NEIGH_TEND_8X8 + 0.5),
56          (int)(8.12242*NEIGH_TEND_8X8 + 0.5), (int)(9.03669*NEIGH_TEND_8X8 + 0.5),          (int)(20.0*NEIGH_TEND_8X8 + 0.5), (int)(21.0*NEIGH_TEND_8X8 + 0.5),
57          (int)(10.0763*NEIGH_TEND_8X8 + 0.5), (int)(11.2669*NEIGH_TEND_8X8 + 0.5),          (int)(22.0*NEIGH_TEND_8X8 + 0.5), (int)(23.0*NEIGH_TEND_8X8 + 0.5),
58          (int)(12.6426*NEIGH_TEND_8X8 + 0.5), (int)(14.2493*NEIGH_TEND_8X8 + 0.5),          (int)(24.0*NEIGH_TEND_8X8 + 0.5), (int)(25.0*NEIGH_TEND_8X8 + 0.5),
59          (int)(16.1512*NEIGH_TEND_8X8 + 0.5), (int)(18.442*NEIGH_TEND_8X8 + 0.5),          (int)(26.0*NEIGH_TEND_8X8 + 0.5), (int)(27.0*NEIGH_TEND_8X8 + 0.5),
60          (int)(21.2656*NEIGH_TEND_8X8 + 0.5), (int)(24.8580*NEIGH_TEND_8X8 + 0.5),          (int)(28.0*NEIGH_TEND_8X8 + 0.5), (int)(29.0*NEIGH_TEND_8X8 + 0.5),
61          (int)(29.6436*NEIGH_TEND_8X8 + 0.5), (int)(36.4949*NEIGH_TEND_8X8 + 0.5)          (int)(30.0*NEIGH_TEND_8X8 + 0.5), (int)(31.0*NEIGH_TEND_8X8 + 0.5)
62  };  };
63    
64  static void  static void
# Line 80  Line 82 
82          }          }
83    
84          sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp);          sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp);
85          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);
86    
87          sad += (data->lambda16 * t * sad)>>10;          sad += (data->lambda16 * t);
88          data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10;          data->temp[0] += (data->lambda8 * t);
89    
90          if (data->chroma) {          if (data->chroma) {
91                  if (sad >= data->iMinSAD[0]) goto no16;                  if (sad >= data->iMinSAD[0]) goto no16;
# Line 127  Line 129 
129          }          }
130    
131          sad = sad8(data->Cur, Reference, data->iEdgedWidth);          sad = sad8(data->Cur, Reference, data->iEdgedWidth);
132          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);
133    
134          sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10;          sad += (data->lambda8 * t);
135    
136          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
137                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
# Line 138  Line 140 
140          }          }
141  }  }
142    
 static void  
 CheckCandidate32(const int x, const int y, SearchData * const data, const unsigned int Direction)  
 {  
         uint32_t t;  
         const uint8_t * Reference;  
         int sad;  
   
         if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || /* non-zero even value */  
                 (x > data->max_dx) || (x < data->min_dx)  
                 || (y > data->max_dy) || (y < data->min_dy) ) return;  
   
         Reference = GetReference(x, y, data);  
         t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 1);  
   
         sad = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp);  
   
         sad += (data->lambda16 * t * sad) >> 10;  
         data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10;  
   
         if (sad < data->iMinSAD[0]) {  
                 data->iMinSAD[0] = sad;  
                 data->currentMV[0].x = x; data->currentMV[0].y = y;  
                 data->dir = Direction;  
         }  
   
         if (data->temp[0] < data->iMinSAD[1]) {  
                 data->iMinSAD[1] = data->temp[0]; data->currentMV[1].x = x; data->currentMV[1].y = y; }  
         if (data->temp[1] < data->iMinSAD[2]) {  
                 data->iMinSAD[2] = data->temp[1]; data->currentMV[2].x = x; data->currentMV[2].y = y; }  
         if (data->temp[2] < data->iMinSAD[3]) {  
                 data->iMinSAD[3] = data->temp[2]; data->currentMV[3].x = x; data->currentMV[3].y = y; }  
         if (data->temp[3] < data->iMinSAD[4]) {  
                 data->iMinSAD[4] = data->temp[3]; data->currentMV[4].x = x; data->currentMV[4].y = y; }  
 }  
   
143  int  int
144  xvid_me_SkipDecisionP(const IMAGE * current, const IMAGE * reference,  xvid_me_SkipDecisionP(const IMAGE * current, const IMAGE * reference,
145                                                          const int x, const int y,                                                          const int x, const int y,
146                                                          const uint32_t stride, const uint32_t iQuant, int rrv)                                                          const uint32_t stride, const uint32_t iQuant)
147  {  {
148          int offset = (x + y*stride)*8;          int offset = (x + y*stride)*8;
         if(!rrv) {  
149                  uint32_t sadC = sad8(current->u + offset,                  uint32_t sadC = sad8(current->u + offset,
150                                                  reference->u + offset, stride);                                                  reference->u + offset, stride);
151                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
# Line 187  Line 153 
153                                                  reference->v + offset, stride);                                                  reference->v + offset, stride);
154                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
155                  return 1;                  return 1;
   
         } else {  
                 uint32_t sadC = sad16(current->u + 2*offset,  
                                                 reference->u + 2*offset, stride, 256*4096);  
                 if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;  
                 sadC += sad16(current->v + 2*offset,  
                                                 reference->v + 2*offset, stride, 256*4096);  
                 if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;  
                 return 1;  
         }  
156  }  }
157    
158          /*          /*
# Line 341  Line 297 
297          /* 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?" */
298          if (skip_possible && (skip_sad < (int)iQuant * MAX_SAD00_FOR_SKIP))          if (skip_possible && (skip_sad < (int)iQuant * MAX_SAD00_FOR_SKIP))
299                  if ( (100*skip_sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH)                  if ( (100*skip_sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH)
300                          if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) {                          if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant)) {
301                                  mode = MODE_NOT_CODED;                                  mode = MODE_NOT_CODED;
302                                  sad = 0;                                  sad = 0;
303                          }                          }
# Line 373  Line 329 
329                  if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;                  if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
330    
331          if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */          if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */
         if (Data->rrv) InterBias *= 4;  
332    
333          if (InterBias < sad) {          if (InterBias < sad) {
334                  int32_t deviation;                  int32_t deviation = dev16(Data->Cur, Data->iEdgedWidth);
                 if (!Data->rrv)  
                         deviation = dev16(Data->Cur, Data->iEdgedWidth);  
                 else  
                         deviation = dev16(Data->Cur, Data->iEdgedWidth) + /* dev32() */  
                                                 dev16(Data->Cur+16, Data->iEdgedWidth) +  
                                                 dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) +  
                                                 dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth);  
   
335                  if (deviation < (sad - InterBias)) mode = MODE_INTRA;                  if (deviation < (sad - InterBias)) mode = MODE_INTRA;
336          }          }
337    
338          pMB->cbp = 63;          pMB->cbp = 63;
339          pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;          pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
340    
         if (Data->rrv) {  
                         Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);  
                         Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);  
         }  
   
341          if (mode == MODE_INTER && mcsel == 0) {          if (mode == MODE_INTER && mcsel == 0) {
342                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
343    
# Line 429  Line 371 
371    
372  static __inline void  static __inline void
373  PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,  PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,
374                          int iHcount, const MACROBLOCK * const prevMB, int rrv)                          int iHcount, const MACROBLOCK * const prevMB)
375  {  {
         /* this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself */  
         if (rrv) { iWcount /= 2; iHcount /= 2; }  
376    
377          if ( (y != 0) && (x < (iWcount-1)) ) {          /* [5] top-right neighbour */          if ( (y != 0) && (x < (iWcount-1)) ) {          /* [5] top-right neighbour */
378                  pmv[5].x = EVEN(pmv[3].x);                  pmv[5].x = EVEN(pmv[3].x);
# Line 457  Line 397 
397                  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 */
398                  pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);                  pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);
399          } else pmv[6].x = pmv[6].y = 0;          } else pmv[6].x = pmv[6].y = 0;
   
         if (rrv) {  
                 int i;  
                 for (i = 0; i < 7; i++) {  
                         pmv[i].x = RRV_MV_SCALEUP(pmv[i].x);  
                         pmv[i].y = RRV_MV_SCALEUP(pmv[i].y);  
                 }  
         }  
400  }  }
401    
402  static void  static void
# Line 479  Line 411 
411  {  {
412          int i = 0;          int i = 0;
413          VECTOR vbest_q; int32_t sbest_q;          VECTOR vbest_q; int32_t sbest_q;
         CheckFunc * CheckCandidate;  
414          *Data->iMinSAD = *(OldData->iMinSAD + 1 + block);          *Data->iMinSAD = *(OldData->iMinSAD + 1 + block);
415          *Data->currentMV = *(OldData->currentMV + 1 + block);          *Data->currentMV = *(OldData->currentMV + 1 + block);
416          *Data->currentQMV = *(OldData->currentQMV + 1 + block);          *Data->currentQMV = *(OldData->currentQMV + 1 + block);
# Line 487  Line 418 
418          if(Data->qpel) {          if(Data->qpel) {
419                  Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block);                  Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block);
420                  if (block != 0) i = d_mv_bits(  Data->currentQMV->x, Data->currentQMV->y,                  if (block != 0) i = d_mv_bits(  Data->currentQMV->x, Data->currentQMV->y,
421                                                                                  Data->predMV, Data->iFcode, 0, 0);                                                                                  Data->predMV, Data->iFcode, 0);
422          } else {          } else {
423                  Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block);                  Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block);
424                  if (block != 0) i = d_mv_bits(  Data->currentMV->x, Data->currentMV->y,                  if (block != 0) i = d_mv_bits(  Data->currentMV->x, Data->currentMV->y,
425                                                                                  Data->predMV, Data->iFcode, 0, Data->rrv);                                                                                  Data->predMV, Data->iFcode, 0);
426          }          }
427    
428          *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;          *(Data->iMinSAD) += (Data->lambda8 * i);
429    
430          if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {          if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {
431    
432                  vbest_q = Data->currentQMV[0];                  vbest_q = Data->currentQMV[0];
433                  sbest_q = Data->iMinSAD[0];                  sbest_q = Data->iMinSAD[0];
434    
                 if (Data->rrv) i = 16; else i = 8;  
435    
436                  Data->RefP[0] = OldData->RefP[0] + i * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->RefP[0] = OldData->RefP[0] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
437                  Data->RefP[1] = OldData->RefP[1] + i * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->RefP[1] = OldData->RefP[1] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
438                  Data->RefP[2] = OldData->RefP[2] + i * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->RefP[2] = OldData->RefP[2] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
439                  Data->RefP[3] = OldData->RefP[3] + i * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->RefP[3] = OldData->RefP[3] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
440    
441                  Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->Cur = OldData->Cur + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
442                  Data->qpel_precision = 0;                  Data->qpel_precision = 0;
443    
444                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,
445                                          pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);                                          pParam->width, pParam->height, Data->iFcode - Data->qpel, 1);
   
                 if (!Data->rrv) CheckCandidate = CheckCandidate8;  
                 else CheckCandidate = CheckCandidate16no4v;  
446    
447                  if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_RD))) {                  if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_RD))) {
448    
# Line 524  Line 451 
451                                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = xvid_me_AdvDiamondSearch;                                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = xvid_me_AdvDiamondSearch;
452                                          else MainSearchPtr = xvid_me_DiamondSearch;                                          else MainSearchPtr = xvid_me_DiamondSearch;
453    
454                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidate);                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidate8);
455                  }                  }
456    
457                  if(!Data->qpel) {                  if(!Data->qpel) {
458                          /* halfpel mode */                          /* halfpel mode */
459                          if (MotionFlags & XVID_ME_HALFPELREFINE8)                          if (MotionFlags & XVID_ME_HALFPELREFINE8)
460                                  /* perform halfpel refine of current best vector */                                  /* perform halfpel refine of current best vector */
461                                  xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0);                                  xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate8, 0);
462                  } else {                  } else {
463                          /* qpel mode */                          /* qpel mode */
464                          Data->currentQMV->x = 2*Data->currentMV->x;                          Data->currentQMV->x = 2*Data->currentMV->x;
# Line 540  Line 467 
467                          if(MotionFlags & XVID_ME_FASTREFINE8) {                          if(MotionFlags & XVID_ME_FASTREFINE8) {
468                                  /* fast */                                  /* fast */
469                                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,                                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,
470                                          pParam->width, pParam->height, Data->iFcode, 2, 0);                                          pParam->width, pParam->height, Data->iFcode, 2);
471                                  FullRefine_Fast(Data, CheckCandidate8, 0);                                  FullRefine_Fast(Data, CheckCandidate8, 0);
472                          } else if(MotionFlags & XVID_ME_QUARTERPELREFINE8) {                          } else if(MotionFlags & XVID_ME_QUARTERPELREFINE8) {
473                                  /* full */                                  /* full */
# Line 551  Line 478 
478                                  }                                  }
479    
480                                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,                                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,
481                                          pParam->width, pParam->height, Data->iFcode, 2, 0);                                          pParam->width, pParam->height, Data->iFcode, 2);
482                                  Data->qpel_precision = 1;                                  Data->qpel_precision = 1;
483    
484                                  xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate8, 0); /* qpel part */                                  xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate8, 0); /* qpel part */
# Line 563  Line 490 
490    
491          }          }
492    
493          if (Data->rrv) {          if(Data->qpel) {
                         Data->currentMV->x = RRV_MV_SCALEDOWN(Data->currentMV->x);  
                         Data->currentMV->y = RRV_MV_SCALEDOWN(Data->currentMV->y);  
         } else if(Data->qpel) {  
494                  pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x;                  pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x;
495                  pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y;                  pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y;
496                  pMB->qmvs[block] = *Data->currentQMV;                  pMB->qmvs[block] = *Data->currentQMV;
# Line 608  Line 532 
532          CheckFunc * CheckCandidate;          CheckFunc * CheckCandidate;
533    
534          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
535                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 1);
536    
537          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, pmv, Data->temp);          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, pmv, Data->temp);
538    
539          Data->chromaX = Data->chromaY = 0; /* chroma-sad cache */          Data->chromaX = Data->chromaY = 0; /* chroma-sad cache */
540          i = Data->rrv ? 2 : 1;          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
541          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i;          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;
542          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;          Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;
543          Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;  
544            Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16;
545          Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16;
546          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16;
547          Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16;
548          Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
549          Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;          Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;
         Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;  
550    
551          Data->lambda16 = xvid_me_lambda_vec16[pMB->quant];          Data->lambda16 = xvid_me_lambda_vec16[pMB->quant];
552          Data->lambda8 = xvid_me_lambda_vec8[pMB->quant];          Data->lambda8 = xvid_me_lambda_vec8[pMB->quant];
# Line 635  Line 558 
558          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);
559          else Data->predMV = pmv[0];          else Data->predMV = pmv[0];
560    
561          i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0, 0);          i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0);
562          Data->iMinSAD[0] = pMB->sad16 + ((Data->lambda16 * i * pMB->sad16)>>10);          Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i);
563          Data->iMinSAD[1] = pMB->sad8[0] + ((Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS)) >> 10);          Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i);
564          Data->iMinSAD[2] = pMB->sad8[1];          Data->iMinSAD[2] = pMB->sad8[1];
565          Data->iMinSAD[3] = pMB->sad8[2];          Data->iMinSAD[3] = pMB->sad8[2];
566          Data->iMinSAD[4] = pMB->sad8[3];          Data->iMinSAD[4] = pMB->sad8[3];
# Line 650  Line 573 
573                  threshA = 512;                  threshA = 512;
574    
575          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
576                                          prevMBs + x + y * pParam->mb_width, Data->rrv);                                          prevMBs + x + y * pParam->mb_width);
577    
         if (!Data->rrv) {  
578                  if (inter4v) CheckCandidate = CheckCandidate16;                  if (inter4v) CheckCandidate = CheckCandidate16;
579                          else CheckCandidate = CheckCandidate16no4v; /* for extra speed */                          else CheckCandidate = CheckCandidate16no4v; /* for extra speed */
         } else CheckCandidate = CheckCandidate32;  
580    
581  /* 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())*/
582    
# Line 690  Line 611 
611                          if (Data->qpel) {                          if (Data->qpel) {
612                                  startMV.x /= 2;                                  startMV.x /= 2;
613                                  startMV.y /= 2;                                  startMV.y /= 2;
                         } else if (Data->rrv) {  
                                 startMV.x = RRV_MV_SCALEUP(startMV.x);  
                                 startMV.y = RRV_MV_SCALEUP(startMV.y);  
614                          }                          }
615                          if (!(MVequal(startMV, backupMV))) {                          if (!(MVequal(startMV, backupMV))) {
616                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
# Line 701  Line 619 
619                                  xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate);                                  xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate);
620                                  if (bSAD < Data->iMinSAD[0]) {                                  if (bSAD < Data->iMinSAD[0]) {
621                                          Data->currentMV[0] = backupMV;                                          Data->currentMV[0] = backupMV;
622                                          Data->iMinSAD[0] = bSAD; }                                          Data->iMinSAD[0] = bSAD;
623                                    }
624                          }                          }
625    
626                          backupMV = Data->currentMV[0];                          backupMV = Data->currentMV[0];
# Line 734  Line 653 
653                  if(MotionFlags & XVID_ME_FASTREFINE16 && MotionFlags & XVID_ME_QUARTERPELREFINE16) {                  if(MotionFlags & XVID_ME_FASTREFINE16 && MotionFlags & XVID_ME_QUARTERPELREFINE16) {
654                          /* fast */                          /* fast */
655                          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,                          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
656                                                  pParam->width, pParam->height, Data->iFcode, 2, 0);                                                  pParam->width, pParam->height, Data->iFcode, 2);
657                          FullRefine_Fast(Data, CheckCandidate, 0);                          FullRefine_Fast(Data, CheckCandidate, 0);
658                  } else {                  } else {
659                          if(MotionFlags & (XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE16_RD)) {                          if(MotionFlags & (XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE16_RD)) {
# Line 747  Line 666 
666                                          }                                          }
667                                  }                                  }
668                                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,                                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
669                                                          pParam->width, pParam->height, Data->iFcode, 2, 0);                                                          pParam->width, pParam->height, Data->iFcode, 2);
670                                  Data->qpel_precision = 1;                                  Data->qpel_precision = 1;
671                                  if(MotionFlags & XVID_ME_QUARTERPELREFINE16)                                  if(MotionFlags & XVID_ME_QUARTERPELREFINE16)
672                                          xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate, 0); /* qpel part */                                          xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate, 0); /* qpel part */
# Line 788  Line 707 
707          } else Data->iMinSAD[1] = 4096*256;          } else Data->iMinSAD[1] = 4096*256;
708  }  }
709    
710    static int
711    InitialSkipDecisionP(int sad00,
712                                             const MBParam * pParam,
713                                             const FRAMEINFO * current,
714                                             MACROBLOCK * pMB,
715                                             const MACROBLOCK * prevMB,
716                                             int x, int y,
717                                             const SearchData * Data,
718                                             const IMAGE * const pGMC,
719                                             const IMAGE * const pCurrent,
720                                             const IMAGE * const pRef,
721                                             const uint32_t MotionFlags)
722    {
723            const unsigned int iEdgedWidth = pParam->edged_width;
724    
725            int skip_thresh = INITIAL_SKIP_THRESH * \
726                    (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);
727            int stat_thresh = 0;
728    
729            /* initial skip decision */
730            if (current->coding_type != S_VOP)      { /* no fast SKIP for S(GMC)-VOPs */
731                    if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)
732                            if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant)) {
733                                    ZeroMacroblockP(pMB, sad00);
734                                    pMB->mode = MODE_NOT_CODED;
735                                    return 1;
736                            }
737            }
738    
739            if(MotionFlags & XVID_ME_DETECT_STATIC_MOTION) {
740                    VECTOR *cmpMV;
741                    VECTOR staticMV = { 0, 0 };
742                    const MACROBLOCK * pMBs = current->mbs;
743    
744                    if (current->coding_type == S_VOP)
745                            cmpMV = &pMB->amv;
746                    else
747                            cmpMV = &staticMV;
748    
749                    if(x > 0 && y > 0 && x < pParam->mb_width) {
750                            if(MVequal((&pMBs[(x-1) + y * pParam->mb_width])->mvs[0], *cmpMV) &&
751                               MVequal((&pMBs[x + (y-1) * pParam->mb_width])->mvs[0], *cmpMV) &&
752                               MVequal((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mvs[0], *cmpMV) &&
753                               MVequal(prevMB->mvs[0], *cmpMV)) {
754                                    stat_thresh = MAX((&pMBs[(x-1) + y * pParam->mb_width])->sad16,
755                                                              MAX((&pMBs[x + (y-1) * pParam->mb_width])->sad16,
756                                                              MAX((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,
757                                                              prevMB->sad16)));
758                            } else {
759                                    stat_thresh = MIN((&pMBs[(x-1) + y * pParam->mb_width])->sad16,
760                                                              MIN((&pMBs[x + (y-1) * pParam->mb_width])->sad16,
761                                                              MIN((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,
762                                                              prevMB->sad16)));
763                            }
764                    }
765            }
766    
767             /* favorize (0,0) or global vector for cartoons */
768            if (current->vop_flags & XVID_VOP_CARTOON) {
769                    if (current->coding_type == S_VOP) {
770                            int32_t iSAD = sad16(pCurrent->y + (x + y * iEdgedWidth) * 16,
771                            pGMC->y + 16*y*iEdgedWidth + 16*x, iEdgedWidth, 65536);
772    
773                            if (Data->chroma) {
774                                    iSAD += sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8, pGMC->u + 8*y*(iEdgedWidth/2) + 8*x, iEdgedWidth/2);
775                                    iSAD += sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8, pGMC->v + 8*y*(iEdgedWidth/2) + 8*x, iEdgedWidth/2);
776                            }
777    
778                            if (iSAD <= stat_thresh) {              /* mode decision GMC */
779                                    pMB->mode = MODE_INTER;
780                                    pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = iSAD;
781                                    pMB->mcsel = 1;
782                                    if (Data->qpel) {
783                                            pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
784                                            pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2;
785                                            pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2;
786                                    } else
787                                            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
788    
789                                    return 1;
790                            }
791                    }
792                    else if (sad00 < stat_thresh) {
793                            VECTOR predMV;
794                            if (Data->qpel)
795                                    predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
796                            else
797                                    predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
798    
799                            ZeroMacroblockP(pMB, sad00);
800                            pMB->cbp = 0x3f;
801                            pMB->pmvs[0].x = - predMV.x;
802                            pMB->pmvs[0].y = - predMV.y;
803                            return 1;
804                    }
805            }
806    
807            return 0;
808    }
809    
810  static __inline uint32_t  static __inline uint32_t
811  MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags)  MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags)
812  {  {
# Line 822  Line 841 
841          if (!(VopFlags & XVID_VOP_HALFPEL))          if (!(VopFlags & XVID_VOP_HALFPEL))
842                  Flags &= ~(XVID_ME_EXTSEARCH16+XVID_ME_HALFPELREFINE16+XVID_ME_HALFPELREFINE8+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD);                  Flags &= ~(XVID_ME_EXTSEARCH16+XVID_ME_HALFPELREFINE16+XVID_ME_HALFPELREFINE8+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD);
843    
844          if ((VopFlags & XVID_VOP_GREYSCALE) || (VopFlags & XVID_VOP_REDUCED))          if (VopFlags & XVID_VOP_GREYSCALE)
845                  Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP);                  Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP);
846    
847          if (Flags & XVID_ME_FASTREFINE8)          if (Flags & XVID_ME_FASTREFINE8)
# Line 834  Line 853 
853          return Flags;          return Flags;
854  }  }
855    
856  bool  static __inline void
857    motionStatsPVOP(int * const MVmax, int * const mvCount, int * const mvSum,
858                                    const MACROBLOCK * const pMB, const int qpel)
859    {
860            const VECTOR * const mv = qpel ? pMB->qmvs : pMB->mvs;
861            int i;
862            int max = *MVmax;
863    
864            switch (pMB->mode) {
865            case MODE_INTER4V:
866                    *mvCount += 3;
867                    for(i = 3; i; i--) {
868                            if (mv[i].x > max) max = mv[i].x;
869                            else if (-mv[i].x - 1 > max) max = -mv[i].x - 1;
870                            *mvSum += mv[i].x * mv[i].x;
871                            if (mv[i].y > max) max = mv[i].y;
872                            else if (-mv[i].y - 1 > max) max = -mv[i].y - 1;
873                            *mvSum += mv[i].y * mv[i].y;
874                    }
875            case MODE_INTER:
876                    (*mvCount)++;
877                    *mvSum += mv[0].x * mv[0].x;
878                    *mvSum += mv[0].y * mv[0].y;
879                    if (mv[0].x > max) max = mv[0].x;
880                    else if (-mv[0].x - 1 > max) max = -mv[0].x - 1;
881                    if (mv[0].y > max) max = mv[0].y;
882                    else if (-mv[0].y - 1 > max) max = -mv[0].y - 1;
883                    *MVmax = max;
884            default:
885                    break;
886            }
887    }
888    
889    void
890  MotionEstimation(MBParam * const pParam,  MotionEstimation(MBParam * const pParam,
891                                   FRAMEINFO * const current,                                   FRAMEINFO * const current,
892                                   FRAMEINFO * const reference,                                   FRAMEINFO * const reference,
# Line 848  Line 900 
900          const IMAGE *const pCurrent = &current->image;          const IMAGE *const pCurrent = &current->image;
901          const IMAGE *const pRef = &reference->image;          const IMAGE *const pRef = &reference->image;
902    
903          uint32_t mb_width = pParam->mb_width;          const uint32_t mb_width = pParam->mb_width;
904          uint32_t mb_height = pParam->mb_height;          const uint32_t mb_height = pParam->mb_height;
905          const uint32_t iEdgedWidth = pParam->edged_width;          const uint32_t iEdgedWidth = pParam->edged_width;
906          const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags);          const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags);
907          int stat_thresh = 0;          int stat_thresh = 0;
908            int MVmax = 0, mvSum = 0, mvCount = 0;
909    
910          uint32_t x, y;          uint32_t x, y;
911          uint32_t iIntra = 0;          int sad00;
         int32_t sad00;  
912          int skip_thresh = INITIAL_SKIP_THRESH * \          int skip_thresh = INITIAL_SKIP_THRESH * \
                 (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \  
913                  (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);                  (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);
914            int block = 0;
915    
916          /* some pre-initialized thingies for SearchP */          /* some pre-initialized thingies for SearchP */
917          DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);
# Line 870  Line 922 
922          Data.rounding = pParam->m_rounding_type;          Data.rounding = pParam->m_rounding_type;
923          Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);          Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);
924          Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP;          Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP;
         Data.rrv = (current->vop_flags & XVID_VOP_REDUCED) ? 1:0;  
925          Data.dctSpace = dct_space;          Data.dctSpace = dct_space;
926          Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);          Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
927          Data.mpeg_quant_matrices = pParam->mpeg_quant_matrices;          Data.mpeg_quant_matrices = pParam->mpeg_quant_matrices;
928    
         if ((current->vop_flags & XVID_VOP_REDUCED)) {  
                 mb_width = (pParam->width + 31) / 32;  
                 mb_height = (pParam->height + 31) / 32;  
                 Data.qpel = 0;  
         }  
   
929          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) */
930          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();
931    
932          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
933                  for (x = 0; x < mb_width; x++)  {                  for (x = 0; x < mb_width; x++)  {
934                          MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];                          MACROBLOCK *pMB = &pMBs[block];
935                          MACROBLOCK *prevMB = &reference->mbs[x + y * pParam->mb_width];                          MACROBLOCK *prevMB = &reference->mbs[block];
936                            int skip;
937                            block++;
938    
939                          if (!Data.rrv) pMB->sad16 =                          pMB->sad16 =
940                                  sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16,                                  sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16,
941                                                          pRef->y + (x + y * iEdgedWidth) * 16,                                                          pRef->y + (x + y * iEdgedWidth) * 16,
942                                                          pParam->edged_width, pMB->sad8 );                                                          pParam->edged_width, pMB->sad8 );
943    
944                          else pMB->sad16 =                          sad00 = 4*MAX(MAX(pMB->sad8[0], pMB->sad8[1]), MAX(pMB->sad8[2], pMB->sad8[3]));
945                                  sad32v_c(pCurrent->y + (x + y * iEdgedWidth) * 32,  
946                                                          pRef->y + (x + y * iEdgedWidth) * 32,                          if (Data.chroma) {
947                                    Data.chromaSAD = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8,
948                                                                            pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2)
949                                                                    + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8,
950                                                                            pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);
951                                    pMB->sad16 += Data.chromaSAD;
952                                    sad00 += Data.chromaSAD;
953                            }
954    
955                            skip = InitialSkipDecisionP(sad00, pParam, current, pMB, prevMB, x, y, &Data, pGMC,
956                                                                                    pCurrent, pRef, MotionFlags);
957                            if (skip) continue;
958    
959                            SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
960                                            y, MotionFlags, current->vop_flags,
961                                            &Data, pParam, pMBs, reference->mbs, pMB);
962    
963                            if (current->vop_flags & XVID_VOP_MODEDECISION_RD)
964                                    xvid_me_ModeDecision_RD(&Data, pMB, pMBs, x, y, pParam,
965                                                                    MotionFlags, current->vop_flags, current->vol_flags,
966                                                                    pCurrent, pRef, pGMC, current->coding_type);
967    
968                            else if (current->vop_flags & XVID_VOP_FAST_MODEDECISION_RD)
969                                    xvid_me_ModeDecision_Fast(&Data, pMB, pMBs, x, y, pParam,
970                                                                    MotionFlags, current->vop_flags, current->vol_flags,
971                                                                    pCurrent, pRef, pGMC, current->coding_type);
972                            else
973                                    ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam,
974                                                                    MotionFlags, current->vop_flags, current->vol_flags,
975                                                                    pCurrent, pRef, pGMC, current->coding_type, sad00);
976    
977    
978                            motionStatsPVOP(&MVmax, &mvCount, &mvSum, pMB, Data.qpel);
979                    }
980            }
981    
982            current->fcode = getMinFcode(MVmax);
983            current->sStat.iMvSum = mvSum;
984            current->sStat.iMvCount = mvCount;
985    }
986    
987    void
988    MotionEstimateSMP(SMPmotionData * h)
989    {
990            const MBParam * const pParam = h->pParam;
991            const FRAMEINFO * const current = h->current;
992            const FRAMEINFO * const reference = h->reference;
993            const IMAGE * const pRefH = h->pRefH;
994            const IMAGE * const pRefV = h->pRefV;
995            const IMAGE * const pRefHV = h->pRefHV;
996            const IMAGE * const pGMC = h->pGMC;
997            uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags,
998                                                                                                    current->vop_flags,
999                                                                                                    current->vol_flags);
1000    
1001            MACROBLOCK *const pMBs = current->mbs;
1002            const IMAGE *const pCurrent = &current->image;
1003            const IMAGE *const pRef = &reference->image;
1004    
1005            const uint32_t mb_width = pParam->mb_width;
1006            const uint32_t mb_height = pParam->mb_height;
1007            const uint32_t iEdgedWidth = pParam->edged_width;
1008            int stat_thresh = 0;
1009            int MVmax = 0, mvSum = 0, mvCount = 0;
1010            int y_step = h->y_step;
1011            int start_y = h->start_y;
1012    
1013            uint32_t x, y;
1014            int sad00;
1015            int skip_thresh = INITIAL_SKIP_THRESH * \
1016                    (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);
1017            int block = start_y*mb_width;
1018            int * complete_count_self = h->complete_count_self;
1019            const int * complete_count_above = h->complete_count_above;
1020            int max_mbs;
1021            int current_mb = 0;
1022    
1023            /* some pre-initialized thingies for SearchP */
1024            DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);
1025            SearchData Data;
1026            memset(&Data, 0, sizeof(SearchData));
1027            Data.iEdgedWidth = iEdgedWidth;
1028            Data.iFcode = current->fcode;
1029            Data.rounding = pParam->m_rounding_type;
1030            Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);
1031            Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP;
1032            Data.dctSpace = dct_space;
1033            Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
1034            Data.mpeg_quant_matrices = pParam->mpeg_quant_matrices;
1035    
1036            /* todo: sort out temp memory space */
1037            Data.RefQ = h->RefQ;
1038            if (sadInit) (*sadInit) ();
1039    
1040            max_mbs = 0;
1041    
1042            for (y = start_y; y < mb_height; y += y_step)   {
1043                    if (y == 0) max_mbs = mb_width; /* we can process all blocks of the first row */
1044    
1045                    for (x = 0; x < mb_width; x++)  {
1046    
1047                            MACROBLOCK *pMB, *prevMB;
1048                            int skip;
1049    
1050                            pMB = &pMBs[block];
1051                            prevMB = &reference->mbs[block];
1052    
1053                            pMB->sad16 =
1054                                    sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16,
1055                                                            pRef->y + (x + y * iEdgedWidth) * 16,
1056                                                          pParam->edged_width, pMB->sad8 );                                                          pParam->edged_width, pMB->sad8 );
1057    
1058                          sad00 = 4*MAX(MAX(pMB->sad8[0], pMB->sad8[1]), MAX(pMB->sad8[2], pMB->sad8[3]));                          sad00 = 4*MAX(MAX(pMB->sad8[0], pMB->sad8[1]), MAX(pMB->sad8[2], pMB->sad8[3]));
# Line 910  Line 1066 
1066                                  sad00 += Data.chromaSAD;                                  sad00 += Data.chromaSAD;
1067                          }                          }
1068    
1069                          /* initial skip decision */                          if (current_mb >= max_mbs) {
1070                          /* no early skip for GMC (global vector = skip vector is unknown!)  */                                  /* we ME-ed all macroblocks we safely could. grab next portion */
1071                          if (current->coding_type != S_VOP)      { /* no fast SKIP for S(GMC)-VOPs */                                  int above_count = *complete_count_above; /* sync point */
1072                                  if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)                                  if (above_count == mb_width) {
1073                                          if (Data.chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {                                          /* full line above is ready */
1074                                                  ZeroMacroblockP(pMB, sad00);                                          above_count = mb_width+1;
1075                                                  pMB->mode = MODE_NOT_CODED;                                          if (y < mb_height-y_step) {
1076                                                    /* this is not last line, grab a portion of MBs from the next line too */
1077                                                    above_count += MAX(0, complete_count_above[1] - 1);
1078                                            }
1079                                    }
1080    
1081                                    max_mbs = current_mb + above_count - x - 1;
1082    
1083                                    if (current_mb >= max_mbs) {
1084                                            /* current workload is zero */
1085                                            x--;
1086                                            sched_yield();
1087                                                  continue;                                                  continue;
1088                                          }                                          }
1089                          }                          }
1090    
1091                          if(MotionFlags & XVID_ME_DETECT_STATIC_MOTION) {                          skip = InitialSkipDecisionP(sad00, pParam, current, pMB, prevMB, x, y, &Data, pGMC,
1092                                  if(x > 0 && y > 0 && x < pParam->mb_width) {                                                                                  pCurrent, pRef, MotionFlags);
1093                                          if(MVequal((&pMBs[(x-1) + y * pParam->mb_width])->mvs[0], zeroMV) &&                          if (current_mb >= max_mbs) {
1094                                             MVequal((&pMBs[x + (y-1) * pParam->mb_width])->mvs[0], zeroMV) &&                                  /* we ME-ed all macroblocks we safely could. grab next portion */
1095                                         MVequal((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mvs[0], zeroMV) &&                                  int above_count = *complete_count_above; /* sync point */
1096                                         MVequal(prevMB->mvs[0], zeroMV)) {                                  if (above_count == mb_width) {
1097                                                  stat_thresh = MAX((&pMBs[(x-1) + y * pParam->mb_width])->sad16,                                          /* full line above is ready */
1098                                                                            MAX((&pMBs[x + (y-1) * pParam->mb_width])->sad16,                                          above_count = mb_width+1;
1099                                                                            MAX((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,                                          if (y < mb_height-y_step) {
1100                                                                            prevMB->sad16)));                                                  /* this is not last line, grab a portion of MBs from the next line too */
1101                                          } else {                                                  above_count += MAX(0, complete_count_above[1] - 1);
                                                 stat_thresh = MIN((&pMBs[(x-1) + y * pParam->mb_width])->sad16,  
                                                                           MIN((&pMBs[x + (y-1) * pParam->mb_width])->sad16,  
                                                                           MIN((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,  
                                                                           prevMB->sad16)));  
1102                                          }                                          }
1103                                  }                                  }
1104    
1105                                    max_mbs = current_mb + above_count - x - 1;
1106    
1107                                    if (current_mb >= max_mbs) {
1108                                            /* current workload is zero */
1109                                            x--;
1110                                            sched_yield();
1111                                            continue;
1112                                    }
1113                          }                          }
1114    
1115                           /* favorize (0,0) vector for cartoons */                          if (skip) {
1116                          if ((current->vop_flags & XVID_VOP_CARTOON) &&                                  current_mb++;
1117                                  ((sad00 < pMB->quant * 4 * skip_thresh) || (sad00 < stat_thresh))) {                                  block++;
1118                                  ZeroMacroblockP(pMB, sad00);                                  *complete_count_self = x+1;
1119                                  continue;                                  continue;
1120                          }                          }
1121    
# Line 965  Line 1137 
1137                                                                  MotionFlags, current->vop_flags, current->vol_flags,                                                                  MotionFlags, current->vop_flags, current->vol_flags,
1138                                                                  pCurrent, pRef, pGMC, current->coding_type, sad00);                                                                  pCurrent, pRef, pGMC, current->coding_type, sad00);
1139    
1140                            *complete_count_self = x+1;
1141    
1142                            current_mb++;
1143                            block++;
1144    
1145                            motionStatsPVOP(&MVmax, &mvCount, &mvSum, pMB, Data.qpel);
1146    
                         if (pMB->mode == MODE_INTRA)  
                                 if (++iIntra > iLimit) return 1;  
1147                  }                  }
1148                    block += (y_step-1)*pParam->mb_width;
1149                    complete_count_self++;
1150                    complete_count_above++;
1151          }          }
1152          return 0;  
1153            h->MVmax = MVmax;
1154            h->mvSum = mvSum;
1155            h->mvCount = mvCount;
1156  }  }
1157    
1158    

Legend:
Removed from v.1478  
changed lines
  Added in v.1682

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