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

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

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

revision 1.1.2.4, Sun Nov 16 15:32:37 2003 UTC revision 1.6, Sat Jul 3 08:46:39 2004 UTC
# Line 39  Line 39 
39  #include "sad.h"  #include "sad.h"
40  #include "motion_inlines.h"  #include "motion_inlines.h"
41    
   
42  static int32_t  static int32_t
43  ChromaSAD2(const int fx, const int fy, const int bx, const int by,  ChromaSAD2(const int fx, const int fy, const int bx, const int by,
44                          const SearchData * const data)                          SearchData * const data)
45  {  {
46          int sad;          int sad;
47          const uint32_t stride = data->iEdgedWidth/2;          const uint32_t stride = data->iEdgedWidth/2;
48          uint8_t *f_refu, *f_refv, *b_refu, *b_refv;          uint8_t *f_refu, *f_refv, *b_refu, *b_refv;
49            int offset, filter;
50    
51          const INTERPOLATE8X8_PTR interpolate8x8_halfpel[] = {          const INTERPOLATE8X8_PTR interpolate8x8_halfpel[] = {
52                  NULL,                  NULL,
# Line 55  Line 55 
55                  interpolate8x8_halfpel_hv                  interpolate8x8_halfpel_hv
56          };          };
57    
58          int offset = (fx>>1) + (fy>>1)*stride;          if (data->chromaX == fx && data->chromaY == fy &&
59          int filter = ((fx & 1) << 1) | (fy & 1);                  data->b_chromaX == bx && data->b_chromaY == by)
60                    return data->chromaSAD;
61    
62            offset = (fx>>1) + (fy>>1)*stride;
63            filter = ((fx & 1) << 1) | (fy & 1);
64    
65          if (filter != 0) {          if (filter != 0) {
66                  f_refu = data->RefQ;                  f_refu = data->RefQ + 64;
67                  f_refv = data->RefQ + 8;                  f_refv = data->RefQ + 64 + 8;
68                    if (data->chromaX != fx || data->chromaY != fy) {
69                  interpolate8x8_halfpel[filter](f_refu, data->RefP[4] + offset, stride, data->rounding);                  interpolate8x8_halfpel[filter](f_refu, data->RefP[4] + offset, stride, data->rounding);
70                  interpolate8x8_halfpel[filter](f_refv, data->RefP[5] + offset, stride, data->rounding);                  interpolate8x8_halfpel[filter](f_refv, data->RefP[5] + offset, stride, data->rounding);
71                    }
72          } else {          } else {
73                  f_refu = (uint8_t*)data->RefP[4] + offset;                  f_refu = (uint8_t*)data->RefP[4] + offset;
74                  f_refv = (uint8_t*)data->RefP[5] + offset;                  f_refv = (uint8_t*)data->RefP[5] + offset;
75          }          }
76            data->chromaX = fx; data->chromaY = fy;
77    
78          offset = (bx>>1) + (by>>1)*stride;          offset = (bx>>1) + (by>>1)*stride;
79          filter = ((bx & 1) << 1) | (by & 1);          filter = ((bx & 1) << 1) | (by & 1);
80    
81          if (filter != 0) {          if (filter != 0) {
82                  b_refu = data->RefQ + 16;                  b_refu = data->RefQ + 64 + 16;
83                  b_refv = data->RefQ + 24;                  b_refv = data->RefQ + 64 + 24;
84                    if (data->b_chromaX != bx || data->b_chromaY != by) {
85                  interpolate8x8_halfpel[filter](b_refu, data->b_RefP[4] + offset, stride, data->rounding);                  interpolate8x8_halfpel[filter](b_refu, data->b_RefP[4] + offset, stride, data->rounding);
86                  interpolate8x8_halfpel[filter](b_refv, data->b_RefP[5] + offset, stride, data->rounding);                  interpolate8x8_halfpel[filter](b_refv, data->b_RefP[5] + offset, stride, data->rounding);
87                    }
88          } else {          } else {
89                  b_refu = (uint8_t*)data->b_RefP[4] + offset;                  b_refu = (uint8_t*)data->b_RefP[4] + offset;
90                  b_refv = (uint8_t*)data->b_RefP[5] + offset;                  b_refv = (uint8_t*)data->b_RefP[5] + offset;
91          }          }
92            data->b_chromaX = bx; data->b_chromaY = by;
93    
94          sad = sad8bi(data->CurU, b_refu, f_refu, stride);          sad = sad8bi(data->CurU, b_refu, f_refu, stride);
95          sad += sad8bi(data->CurV, b_refv, f_refv, stride);          sad += sad8bi(data->CurV, b_refv, f_refv, stride);
96    
97            data->chromaSAD = sad;
98          return sad;          return sad;
99  }  }
100    
101  static void  static void
102  CheckCandidateInt(const int xf, const int yf, const SearchData * const data, const unsigned int Direction)  CheckCandidateInt(const int x, const int y, SearchData * const data, const unsigned int Direction)
103  {  {
104          int32_t sad, xb, yb, xcf, ycf, xcb, ycb;          int32_t sad, xf, yf, xb, yb, xcf, ycf, xcb, ycb;
105          uint32_t t;          uint32_t t;
106    
107          const uint8_t *ReferenceF, *ReferenceB;          const uint8_t *ReferenceF, *ReferenceB;
108          VECTOR *current;          VECTOR *current;
109    
110          if ((xf > data->max_dx) || (xf < data->min_dx) ||          if ((x > data->max_dx) || (x < data->min_dx) ||
111                  (yf > data->max_dy) || (yf < data->min_dy))                  (y > data->max_dy) || (y < data->min_dy))
112                  return;                  return;
113    
114            if (Direction == 1) { /* x and y mean forward vector */
115                    VECTOR backward = data->qpel_precision ? data->currentQMV[1] : data->currentMV[1];
116                    xb = backward.x;
117                    yb = backward.y;
118                    xf = x; yf = y;
119            } else { /* x and y mean backward vector */
120                    VECTOR forward = data->qpel_precision ? data->currentQMV[0] : data->currentMV[0];
121                    xf = forward.x;
122                    yf = forward.y;
123                    xb = x; yb = y;
124            }
125    
126          if (!data->qpel_precision) {          if (!data->qpel_precision) {
127                  ReferenceF = GetReference(xf, yf, data);                  ReferenceF = GetReference(xf, yf, data);
                 xb = data->currentMV[1].x; yb = data->currentMV[1].y;  
128                  ReferenceB = GetReferenceB(xb, yb, 1, data);                  ReferenceB = GetReferenceB(xb, yb, 1, data);
129                  current = data->currentMV;                  current = data->currentMV + Direction - 1;
130                  xcf = xf; ycf = yf;                  xcf = xf; ycf = yf;
131                  xcb = xb; ycb = yb;                  xcb = xb; ycb = yb;
132          } else {          } else {
133                  ReferenceF = xvid_me_interpolate16x16qpel(xf, yf, 0, data);                  ReferenceF = xvid_me_interpolate16x16qpel(xf, yf, 0, data);
134                  xb = data->currentQMV[1].x; yb = data->currentQMV[1].y;                  current = data->currentQMV + Direction - 1;
                 current = data->currentQMV;  
135                  ReferenceB = xvid_me_interpolate16x16qpel(xb, yb, 1, data);                  ReferenceB = xvid_me_interpolate16x16qpel(xb, yb, 1, data);
136                  xcf = xf/2; ycf = yf/2;                  xcf = xf/2; ycf = yf/2;
137                  xcb = xb/2; ycb = yb/2;                  xcb = xb/2; ycb = yb/2;
# Line 128  Line 150 
150                                                          (ycb >> 1) + roundtab_79[ycb & 0x3], data);                                                          (ycb >> 1) + roundtab_79[ycb & 0x3], data);
151    
152          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
153                  *(data->iMinSAD) = sad;                  *data->iMinSAD = sad;
154                  current->x = xf; current->y = yf;                  current->x = x; current->y = y;
155                  *data->dir = Direction;                  data->dir = Direction;
156          }          }
157  }  }
158    
159  static void  static void
160  CheckCandidateDirect(const int x, const int y, const SearchData * const data, const unsigned int Direction)  CheckCandidateDirect(const int x, const int y, SearchData * const data, const unsigned int Direction)
161  {  {
162          int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0;          int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0;
163          uint32_t k;          uint32_t k;
# Line 175  Line 197 
197                  ReferenceF = xvid_me_interpolate8x8qpel(mvs.x, mvs.y, k, 0, data);                  ReferenceF = xvid_me_interpolate8x8qpel(mvs.x, mvs.y, k, 0, data);
198                  ReferenceB = xvid_me_interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data);                  ReferenceB = xvid_me_interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data);
199    
200                  sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),                  sad += data->iMinSAD[k+1] =
201                            sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),
202                                                  ReferenceF, ReferenceB, data->iEdgedWidth);                                                  ReferenceF, ReferenceB, data->iEdgedWidth);
203                  if (sad > *(data->iMinSAD)) return;                  if (sad > *(data->iMinSAD)) return;
204          }          }
# Line 189  Line 212 
212                                                          (ycb >> 3) + roundtab_76[ycb & 0xf], data);                                                          (ycb >> 3) + roundtab_76[ycb & 0xf], data);
213    
214          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
215                  *(data->iMinSAD) = sad;                  data->iMinSAD[0] = sad;
216                  data->currentMV->x = x; data->currentMV->y = y;                  data->currentMV->x = x; data->currentMV->y = y;
217                  *data->dir = Direction;                  data->dir = Direction;
218          }          }
219  }  }
220    
221  static void  static void
222  CheckCandidateDirectno4v(const int x, const int y, const SearchData * const data, const unsigned int Direction)  CheckCandidateDirectno4v(const int x, const int y, SearchData * const data, const unsigned int Direction)
223  {  {
224          int32_t sad, xcf, ycf, xcb, ycb;          int32_t sad, xcf, ycf, xcb, ycb;
225          const uint8_t *ReferenceF;          const uint8_t *ReferenceF;
# Line 244  Line 267 
267          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
268                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
269                  data->currentMV->x = x; data->currentMV->y = y;                  data->currentMV->x = x; data->currentMV->y = y;
270                  *data->dir = Direction;                  data->dir = Direction;
271          }          }
272  }  }
273    
274  void  void
275  CheckCandidate16no4v(const int x, const int y, const SearchData * const data, const unsigned int Direction)  CheckCandidate16no4v(const int x, const int y, SearchData * const data, const unsigned int Direction)
276  {  {
277          int32_t sad, xc, yc;          int32_t sad, xc, yc;
278          const uint8_t * Reference;          const uint8_t * Reference;
# Line 283  Line 306 
306          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
307                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
308                  current->x = x; current->y = y;                  current->x = x; current->y = y;
309                  *data->dir = Direction;                  data->dir = Direction;
310          }          }
311  }  }
312    
# Line 291  Line 314 
314  ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode)  ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode)
315  {  {
316  /* the stupidiest function ever */  /* the stupidiest function ever */
317          return (mode == MODE_FORWARD) ? pMB->mvs[0] : pMB->b_mvs[0];          return (mode == MODE_FORWARD ? pMB->mvs[0] : pMB->b_mvs[0]);
318  }  }
319    
320  static void __inline  static void __inline
# Line 350  Line 373 
373    
374          int i;          int i;
375          VECTOR pmv[7];          VECTOR pmv[7];
376          MainSearchFunc *MainSearchPtr;          int threshA = (MotionFlags & XVID_ME_FASTREFINE16) ? 150 : 300;
377          *Data->iMinSAD = MV_MAX_ERROR;          *Data->iMinSAD = MV_MAX_ERROR;
378          Data->iFcode = iFcode;          Data->iFcode = iFcode;
379          Data->qpel_precision = 0;          Data->qpel_precision = 0;
380          Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; /* reset chroma-sad cache */          Data->chromaX = Data->chromaY = Data->chromaSAD = 256*4096; /* reset chroma-sad cache */
381    
382          Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16;          Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16;
383          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16;          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16;
# Line 380  Line 403 
403                  if (!vector_repeats(pmv, i) )                  if (!vector_repeats(pmv, i) )
404                          CheckCandidate16no4v(pmv[i].x, pmv[i].y, Data, i);                          CheckCandidate16no4v(pmv[i].x, pmv[i].y, Data, i);
405    
406            if (*Data->iMinSAD > 512) {
407                    unsigned int mask = make_mask(pmv, 7, Data->dir);
408    
409                    MainSearchFunc *MainSearchPtr;
410          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;
411          else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;          else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;
412                  else MainSearchPtr = xvid_me_DiamondSearch;                  else MainSearchPtr = xvid_me_DiamondSearch;
413    
         if (*Data->iMinSAD > 512) {  
                 unsigned int mask = make_mask(pmv, 7, *Data->dir);  
414                  MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v);                  MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v);
415          }          }
416    
         xvid_me_SubpelRefine(Data, CheckCandidate16no4v);  
417    
418          if (Data->qpel && *Data->iMinSAD < *best_sad + 300) {  
419    
420            if(!Data->qpel) {
421                    /* halfpel mode */
422                    if (MotionFlags & XVID_ME_HALFPELREFINE16)
423                                    xvid_me_SubpelRefine(Data, CheckCandidate16no4v, 0);
424            } else {
425                    /* qpel mode */
426                    if(MotionFlags & XVID_ME_FASTREFINE16) {
427                            /* fast */
428                            get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
429                                                    pParam->width, pParam->height, Data->iFcode, 2, 0);
430                            if (*Data->iMinSAD < *best_sad + 2*threshA)
431                                    FullRefine_Fast(Data, CheckCandidate16no4v, 0);
432                    } else {
433                  Data->currentQMV->x = 2*Data->currentMV->x;                  Data->currentQMV->x = 2*Data->currentMV->x;
434                  Data->currentQMV->y = 2*Data->currentMV->y;                  Data->currentQMV->y = 2*Data->currentMV->y;
435                  Data->qpel_precision = 1;                          if(MotionFlags & XVID_ME_QUARTERPELREFINE16) {
436                                    /* full */
437                                    if (MotionFlags & XVID_ME_HALFPELREFINE16) {
438                                            xvid_me_SubpelRefine(Data, CheckCandidate16no4v, 0); /* hpel part */
439                                            Data->currentQMV->x = 2*Data->currentMV->x;
440                                            Data->currentQMV->y = 2*Data->currentMV->y;
441                                    }
442                  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,
443                                          pParam->width, pParam->height, iFcode, 2, 0);                                                          pParam->width, pParam->height, Data->iFcode, 2, 0);
444                  xvid_me_SubpelRefine(Data, CheckCandidate16no4v);                                  Data->qpel_precision = 1;
445                                    if (*Data->iMinSAD < *best_sad + threshA)
446                                            xvid_me_SubpelRefine(Data, CheckCandidate16no4v, 0); /* qpel part */
447                            }
448                    }
449          }          }
450    
451          /* three bits are needed to code backward mode. four for forward */          /* three bits are needed to code backward mode. four for forward */
# Line 435  Line 483 
483                                  const uint32_t x, const uint32_t y,                                  const uint32_t x, const uint32_t y,
484                                  const SearchData * const Data)                                  const SearchData * const Data)
485  {  {
486            int k;
487    
488            if (!Data->chroma) {
489          int dx = 0, dy = 0, b_dx = 0, b_dy = 0;          int dx = 0, dy = 0, b_dx = 0, b_dy = 0;
490          int32_t sum;          int32_t sum;
         int k;  
491          const uint32_t stride = Data->iEdgedWidth/2;          const uint32_t stride = Data->iEdgedWidth/2;
492          /* this is not full chroma compensation, only it's fullpel approximation. should work though */          /* this is not full chroma compensation, only it's fullpel approximation. should work though */
493    
# Line 465  Line 515 
515                                          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,
516                                          stride);                                          stride);
517    
518          if (sum < MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) {                  if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */
519            }
520    
521            /* skip */
522                  pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */                  pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */
523                  for (k = 0; k < 4; k++) {                  for (k = 0; k < 4; k++) {
524                          pMB->qmvs[k] = pMB->mvs[k] = Data->directmvF[k];                          pMB->qmvs[k] = pMB->mvs[k] = Data->directmvF[k];
525                          pMB->b_qmvs[k] = pMB->b_mvs[k] =  Data->directmvB[k];                          pMB->b_qmvs[k] = pMB->b_mvs[k] =  Data->directmvB[k];
526                  }                  }
527          }          }
 }  
528    
529  static uint32_t  static uint32_t
530  SearchDirect(const IMAGE * const f_Ref,  SearchDirect(const IMAGE * const f_Ref,
# Line 518  Line 570 
570          Data->max_dy = k * (pParam->height - y * 16);          Data->max_dy = k * (pParam->height - y * 16);
571          Data->min_dx = -k * (16 + x * 16);          Data->min_dx = -k * (16 + x * 16);
572          Data->min_dy = -k * (16 + y * 16);          Data->min_dy = -k * (16 + y * 16);
573            Data->chromaX = Data->chromaY = Data->b_chromaX = Data->b_chromaY = 256*4096;
574    
575          Data->referencemv = Data->qpel ? b_mb->qmvs : b_mb->mvs;          Data->referencemv = Data->qpel ? b_mb->qmvs : b_mb->mvs;
576          Data->qpel_precision = 0;          Data->qpel_precision = 0;
# Line 545  Line 598 
598                  }                  }
599          }          }
600    
601          CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v;          CheckCandidateDirect(0, 0, Data, 255);  /* will also fill iMinSAD[1..4] with 8x8 SADs */
   
         CheckCandidate(0, 0, Data, 255);  
602    
603          /* initial (fast) skip decision */          /* initial (fast) skip decision */
604          if (*Data->iMinSAD < (int)Data->iQuant * INITIAL_SKIP_THRESH * (Data->chroma?3:2)) {          if (Data->iMinSAD[1] < (int)Data->iQuant * INITIAL_SKIP_THRESH
605                    && Data->iMinSAD[2] < (int)Data->iQuant * INITIAL_SKIP_THRESH
606                    && Data->iMinSAD[3] < (int)Data->iQuant * INITIAL_SKIP_THRESH
607                    && Data->iMinSAD[4] < (int)Data->iQuant * INITIAL_SKIP_THRESH) {
608                  /* possible skip */                  /* possible skip */
                 if (Data->chroma) {  
                         pMB->mode = MODE_DIRECT_NONE_MV;  
                         return *Data->iMinSAD; /* skip. */  
                 } else {  
609                          SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data);                          SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data);
610                          if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; /* skip. */                  if (pMB->mode == MODE_DIRECT_NONE_MV)
611                  }                          return *Data->iMinSAD; /* skipped */
612          }          }
613    
614          *Data->iMinSAD += Data->lambda16;          skip_sad = 4*MAX(MAX(Data->iMinSAD[1],Data->iMinSAD[2]), MAX(Data->iMinSAD[3],Data->iMinSAD[4]));
615          skip_sad = *Data->iMinSAD;          if (Data->chroma) skip_sad += Data->chromaSAD;
616    
617          /*          CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v;
          * DIRECT MODE DELTA VECTOR SEARCH.  
          * This has to be made more effective, but at the moment I'm happy it's running at all  
          */  
618    
619            if (!(MotionFlags & XVID_ME_SKIP_DELTASEARCH)) {
620          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;
621                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;
622                          else MainSearchPtr = xvid_me_DiamondSearch;                          else MainSearchPtr = xvid_me_DiamondSearch;
623    
624          MainSearchPtr(0, 0, Data, 255, CheckCandidate);          MainSearchPtr(0, 0, Data, 255, CheckCandidate);
625    
626          xvid_me_SubpelRefine(Data, CheckCandidate);                  xvid_me_SubpelRefine(Data, CheckCandidate, 0);
627            }
628    
629            *Data->iMinSAD += Data->lambda16;
630          *best_sad = *Data->iMinSAD;          *best_sad = *Data->iMinSAD;
631    
632          if (Data->qpel || b_mb->mode == MODE_INTER4V) pMB->mode = MODE_DIRECT;          if (Data->qpel || b_mb->mode == MODE_INTER4V) pMB->mode = MODE_DIRECT;
# Line 611  Line 661 
661          return skip_sad;          return skip_sad;
662  }  }
663    
664    
665    static void set_range(int * range, SearchData * Data)
666    {
667            Data->min_dx = range[0];
668            Data->max_dx = range[1];
669            Data->min_dy = range[2];
670            Data->max_dy = range[3];
671    }
672    
673  static void  static void
674  SearchInterpolate(const IMAGE * const f_Ref,  SearchInterpolate(const IMAGE * const f_Ref,
675                                  const uint8_t * const f_RefH,                                  const uint8_t * const f_RefH,
# Line 629  Line 688 
688                                  const VECTOR * const b_predMV,                                  const VECTOR * const b_predMV,
689                                  MACROBLOCK * const pMB,                                  MACROBLOCK * const pMB,
690                                  int32_t * const best_sad,                                  int32_t * const best_sad,
691                                  SearchData * const fData)                                  SearchData * const Data)
692    
693  {  {
694          int i, j;          int i, j;
695          SearchData bData;          int b_range[4], f_range[4];
696            int threshA = (MotionFlags & XVID_ME_FAST_MODEINTERPOLATE) ? 250 : 500;
697            int threshB = (MotionFlags & XVID_ME_FAST_MODEINTERPOLATE) ? 150 : 300;
698    
699            Data->qpel_precision = 0;
700            *Data->iMinSAD = 4096*256;
701            Data->iFcode = fcode; Data->bFcode = bcode;
702    
703            i = (x + y * Data->iEdgedWidth) * 16;
704    
705            Data->RefP[0] = f_Ref->y + i;
706            Data->RefP[2] = f_RefH + i;
707            Data->RefP[1] = f_RefV + i;
708            Data->RefP[3] = f_RefHV + i;
709            Data->b_RefP[0] = b_Ref->y + i;
710            Data->b_RefP[2] = b_RefH + i;
711            Data->b_RefP[1] = b_RefV + i;
712            Data->b_RefP[3] = b_RefHV + i;
713            Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;
714            Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;
715            Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;
716            Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;
717    
718            Data->chromaX = Data->chromaY = 256*4096;
719    
720            Data->predMV = *f_predMV;
721            Data->bpredMV = *b_predMV;
722    
723            Data->currentMV[0] = Data->currentMV[2]; /* forward search left its vector here */
724    
725          fData->qpel_precision = 0;          get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, fcode - Data->qpel, 1, 0);
726          memcpy(&bData, fData, sizeof(SearchData)); /* quick copy of common data */          get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, bcode - Data->qpel, 1, 0);
         *fData->iMinSAD = 4096*256;  
         bData.currentMV++; bData.currentQMV++;  
         fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;  
   
         i = (x + y * fData->iEdgedWidth) * 16;  
   
         bData.b_RefP[0] = fData->RefP[0] = f_Ref->y + i;  
         bData.b_RefP[2] = fData->RefP[2] = f_RefH + i;  
         bData.b_RefP[1] = fData->RefP[1] = f_RefV + i;  
         bData.b_RefP[3] = fData->RefP[3] = f_RefHV + i;  
         bData.RefP[0] = fData->b_RefP[0] = b_Ref->y + i;  
         bData.RefP[2] = fData->b_RefP[2] = b_RefH + i;  
         bData.RefP[1] = fData->b_RefP[1] = b_RefV + i;  
         bData.RefP[3] = fData->b_RefP[3] = b_RefHV + i;  
         bData.b_RefP[4] = fData->RefP[4] = f_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8;  
         bData.b_RefP[5] = fData->RefP[5] = f_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8;  
         bData.RefP[4] = fData->b_RefP[4] = b_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8;  
         bData.RefP[5] = fData->b_RefP[5] = b_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8;  
         bData.dir = fData->dir;  
   
         bData.bpredMV = fData->predMV = *f_predMV;  
         fData->bpredMV = bData.predMV = *b_predMV;  
         fData->currentMV[0] = fData->currentMV[2];  
   
         get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 4, pParam->width, pParam->height, fcode - fData->qpel, 1, 0);  
         get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 4, pParam->width, pParam->height, bcode - fData->qpel, 1, 0);  
   
         if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx;  
         if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dx;  
         if (fData->currentMV[0].y > fData->max_dy) fData->currentMV[0].y = fData->max_dy;  
         if (fData->currentMV[0].y < fData->min_dy) fData->currentMV[0].y = fData->min_dy;  
   
         if (fData->currentMV[1].x > bData.max_dx) fData->currentMV[1].x = bData.max_dx;  
         if (fData->currentMV[1].x < bData.min_dx) fData->currentMV[1].x = bData.min_dx;  
         if (fData->currentMV[1].y > bData.max_dy) fData->currentMV[1].y = bData.max_dy;  
         if (fData->currentMV[1].y < bData.min_dy) fData->currentMV[1].y = bData.min_dy;  
727    
728          CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, fData, 255);          if (Data->currentMV[0].x > f_range[1]) Data->currentMV[0].x = f_range[1];
729            if (Data->currentMV[0].x < f_range[0]) Data->currentMV[0].x = f_range[0];
730            if (Data->currentMV[0].y > f_range[3]) Data->currentMV[0].y = f_range[3];
731            if (Data->currentMV[0].y < f_range[2]) Data->currentMV[0].y = f_range[2];
732    
733            if (Data->currentMV[1].x > b_range[1]) Data->currentMV[1].x = b_range[1];
734            if (Data->currentMV[1].x < b_range[0]) Data->currentMV[1].x = b_range[0];
735            if (Data->currentMV[1].y > b_range[3]) Data->currentMV[1].y = b_range[3];
736            if (Data->currentMV[1].y < b_range[2]) Data->currentMV[1].y = b_range[2];
737    
738            set_range(f_range, Data);
739    
740            CheckCandidateInt(Data->currentMV[0].x, Data->currentMV[0].y, Data, 1);
741    
742          /* diamond */          /* diamond */
743          do {          do {
744                  *fData->dir = 255;                  Data->dir = 0;
745                  /* forward MV moves */                  /* forward MV moves */
746                  i = fData->currentMV[0].x; j = fData->currentMV[0].y;                  i = Data->currentMV[0].x; j = Data->currentMV[0].y;
747    
748                  CheckCandidateInt(i + 1, j, fData, 0);                  CheckCandidateInt(i + 1, j, Data, 1);
749                  CheckCandidateInt(i, j + 1, fData, 0);                  CheckCandidateInt(i, j + 1, Data, 1);
750                  CheckCandidateInt(i - 1, j, fData, 0);                  CheckCandidateInt(i - 1, j, Data, 1);
751                  CheckCandidateInt(i, j - 1, fData, 0);                  CheckCandidateInt(i, j - 1, Data, 1);
752    
753                  /* backward MV moves */                  /* backward MV moves */
754                  i = fData->currentMV[1].x; j = fData->currentMV[1].y;                  set_range(b_range, Data);
755                  fData->currentMV[2] = fData->currentMV[0];                  i = Data->currentMV[1].x; j = Data->currentMV[1].y;
                 CheckCandidateInt(i + 1, j, &bData, 0);  
                 CheckCandidateInt(i, j + 1, &bData, 0);  
                 CheckCandidateInt(i - 1, j, &bData, 0);  
                 CheckCandidateInt(i, j - 1, &bData, 0);  
756    
757          } while (!(*fData->dir));                  CheckCandidateInt(i + 1, j, Data, 2);
758                    CheckCandidateInt(i, j + 1, Data, 2);
759                    CheckCandidateInt(i - 1, j, Data, 2);
760                    CheckCandidateInt(i, j - 1, Data, 2);
761    
762                    set_range(f_range, Data);
763    
764            } while (Data->dir != 0);
765    
766          /* qpel refinement */          /* qpel refinement */
767          if (fData->qpel) {          if (Data->qpel) {
768                  if (*fData->iMinSAD > *best_sad + 500) return;                  if (*Data->iMinSAD > *best_sad + threshA) return;
769                  fData->qpel_precision = bData.qpel_precision = 1;                  Data->qpel_precision = 1;
770                  get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 4, pParam->width, pParam->height, fcode, 2, 0);                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, fcode, 2, 0);
771                  get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0);  
772                  fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x;                  Data->currentQMV[0].x = 2 * Data->currentMV[0].x;
773                  fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y;                  Data->currentQMV[0].y = 2 * Data->currentMV[0].y;
774                  fData->currentQMV[1].x = 2 * fData->currentMV[1].x;                  Data->currentQMV[1].x = 2 * Data->currentMV[1].x;
775                  fData->currentQMV[1].y = 2 * fData->currentMV[1].y;                  Data->currentQMV[1].y = 2 * Data->currentMV[1].y;
776                  xvid_me_SubpelRefine(fData, CheckCandidateInt);  
777                  if (*fData->iMinSAD > *best_sad + 300) return;                  if (MotionFlags & XVID_ME_QUARTERPELREFINE16)
778                  fData->currentQMV[2] = fData->currentQMV[0];                          xvid_me_SubpelRefine(Data, CheckCandidateInt, 1);
779                  xvid_me_SubpelRefine(&bData, CheckCandidateInt);  
780          }                  if (*Data->iMinSAD > *best_sad + threshB) return;
781                    get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0);
782          *fData->iMinSAD += (2+3) * fData->lambda16; /* two bits are needed to code interpolate mode. */  
783                    if (MotionFlags & XVID_ME_QUARTERPELREFINE16)
784          if (*fData->iMinSAD < *best_sad) {                          xvid_me_SubpelRefine(Data, CheckCandidateInt, 2);
785                  *best_sad = *fData->iMinSAD;          }
786                  pMB->mvs[0] = fData->currentMV[0];  
787                  pMB->b_mvs[0] = fData->currentMV[1];          *Data->iMinSAD += 2 * Data->lambda16; /* two bits are needed to code interpolate mode. */
788    
789            if (*Data->iMinSAD < *best_sad) {
790                    *best_sad = *Data->iMinSAD;
791                    pMB->mvs[0] = Data->currentMV[0];
792                    pMB->b_mvs[0] = Data->currentMV[1];
793                  pMB->mode = MODE_INTERPOLATE;                  pMB->mode = MODE_INTERPOLATE;
794                  if (fData->qpel) {                  if (Data->qpel) {
795                          pMB->qmvs[0] = fData->currentQMV[0];                          pMB->qmvs[0] = Data->currentQMV[0];
796                          pMB->b_qmvs[0] = fData->currentQMV[1];                          pMB->b_qmvs[0] = Data->currentQMV[1];
797                          pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x;                          pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x;
798                          pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y;                          pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y;
799                          pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x;                          pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x;
# Line 759  Line 830 
830          uint32_t skip_sad;          uint32_t skip_sad;
831    
832          const MACROBLOCK * const b_mbs = b_reference->mbs;          const MACROBLOCK * const b_mbs = b_reference->mbs;
833            MACROBLOCK *const pMBs = frame->mbs;
834    
835          VECTOR f_predMV, b_predMV;          VECTOR f_predMV, b_predMV;
836    
# Line 768  Line 840 
840          /* some pre-inintialized data for the rest of the search */          /* some pre-inintialized data for the rest of the search */
841    
842          SearchData Data;          SearchData Data;
         int32_t iMinSAD;  
         uint32_t dir;  
         VECTOR currentMV[3];  
         VECTOR currentQMV[3];  
         int32_t temp[8];  
843          memset(&Data, 0, sizeof(SearchData));          memset(&Data, 0, sizeof(SearchData));
844    
845          Data.iEdgedWidth = pParam->edged_width;          Data.iEdgedWidth = pParam->edged_width;
         Data.currentMV = currentMV; Data.currentQMV = currentQMV;  
         Data.iMinSAD = &iMinSAD;  
         Data.lambda16 = xvid_me_lambda_vec16[MAX(frame->quant-2, 2)];  
846          Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0;          Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0;
847          Data.rounding = 0;          Data.rounding = 0;
848          Data.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP;          Data.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP;
         Data.temp = temp;  
         Data.dir = &dir;  
849          Data.iQuant = frame->quant;          Data.iQuant = frame->quant;
850    
851          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 795  Line 858 
858                  for (i = 0; i < pParam->mb_width; i++) {                  for (i = 0; i < pParam->mb_width; i++) {
859                          MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;                          MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;
860                          const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;                          const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;
861                            int interpol_search = 0;
862                            int bf_search = 0;
863                            int bf_thresh = 0;
864    
865  /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */  /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */
866                          if (b_reference->coding_type != S_VOP)                          if (b_reference->coding_type != S_VOP)
867                                  if (b_mb->mode == MODE_NOT_CODED) {                                  if (b_mb->mode == MODE_NOT_CODED) {
868                                          pMB->mode = MODE_NOT_CODED;                                          pMB->mode = MODE_NOT_CODED;
869                                          pMB->mvs[0] = zeroMV;                                          pMB->mvs[0] = pMB->b_mvs[0] = zeroMV;
870                                          pMB->b_mvs[0] = zeroMV;                                          pMB->sad16 = 0;
871                                          continue;                                          continue;
872                                  }                                  }
873    
874                            Data.lambda16 = xvid_me_lambda_vec16[b_mb->quant];
875                          Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;                          Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;
876                          Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8;                          Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8;
877                          Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8;                          Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8;
# Line 822  Line 889 
889                                                                          &best_sad,                                                                          &best_sad,
890                                                                          &Data);                                                                          &Data);
891    
892                          if (pMB->mode == MODE_DIRECT_NONE_MV) continue;                          if (pMB->mode == MODE_DIRECT_NONE_MV) {
893                                    pMB->sad16 = best_sad;
894                                    continue;
895                            }
896    
897                            if (frame->motion_flags & XVID_ME_BFRAME_EARLYSTOP) {
898                                    if(i > 0 && j > 0 && i < pParam->mb_width) {
899                                            bf_thresh = MIN((&pMBs[(i-1) + j * pParam->mb_width])->sad16,
900                                                                         MIN((&pMBs[i + (j-1) * pParam->mb_width])->sad16,
901                                                                         (&pMBs[(i+1) + (j-1) * pParam->mb_width])->sad16));
902    
903                                            if (((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_FORWARD) &&
904                                                    ((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_BACKWARD) &&
905                                                    ((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_INTERPOLATE))
906                                                            bf_search++;
907    
908                                            if (((&pMBs[i + (j - 1) * pParam->mb_width])->mode != MODE_FORWARD) &&
909                                                    ((&pMBs[i + (j - 1) * pParam->mb_width])->mode != MODE_BACKWARD) &&
910                                                    ((&pMBs[i + (j - 1) * pParam->mb_width])->mode != MODE_INTERPOLATE))
911                                                            bf_search++;
912    
913                                            if (((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode != MODE_FORWARD) &&
914                                                    ((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode != MODE_BACKWARD) &&
915                                                    ((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode != MODE_INTERPOLATE))
916                                                    bf_search++;
917                                    }
918    
919                                    if ((best_sad < bf_thresh) && (bf_search == 3))
920                                            continue;
921                            }
922    
923                          /* forward search */                          /* forward search */
924                          SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y,                          SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y,
# Line 841  Line 937 
937                                                  MODE_BACKWARD, &Data);                                                  MODE_BACKWARD, &Data);
938    
939                          /* interpolate search comes last, because it uses data from forward and backward as prediction */                          /* interpolate search comes last, because it uses data from forward and backward as prediction */
940                            if (frame->motion_flags & XVID_ME_FAST_MODEINTERPOLATE) {
941    
942                                    if(i > 0 && j > 0 && i < pParam->mb_width) {
943                                            if ((&pMBs[(i-1) + j * pParam->mb_width])->mode == MODE_INTERPOLATE)
944                                                    interpol_search++;
945                                            if ((&pMBs[i + (j - 1) * pParam->mb_width])->mode == MODE_INTERPOLATE)
946                                                    interpol_search++;
947                                            if ((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode == MODE_INTERPOLATE)
948                                                    interpol_search++;
949                                    }
950                                    else
951                                            interpol_search = 1;
952    
953                                    interpol_search |= !(best_sad < 3 * Data.iQuant * MAX_SAD00_FOR_SKIP * (Data.chroma ? 3:2));
954                            }
955                            else
956                                    interpol_search = 1;
957    
958                            if (interpol_search) {
959                          SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y,                          SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y,
960                                                  b_ref, b_refH->y, b_refV->y, b_refHV->y,                                                  b_ref, b_refH->y, b_refV->y, b_refHV->y,
961                                                  i, j,                                                  i, j,
# Line 850  Line 965 
965                                                  &f_predMV, &b_predMV,                                                  &f_predMV, &b_predMV,
966                                                  pMB, &best_sad,                                                  pMB, &best_sad,
967                                                  &Data);                                                  &Data);
968                            }
969    
970                          /* final skip decision */                          /* final skip decision */
971                          if ( (skip_sad < Data.iQuant * MAX_SAD00_FOR_SKIP * 2)                          if ( (skip_sad < Data.iQuant * MAX_SAD00_FOR_SKIP )
972                                          && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) )                                          && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) )
973    
974                                  SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data);                                  SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data);
975    
976                          switch (pMB->mode) {                          switch (pMB->mode) {
977                                  case MODE_FORWARD:                                  case MODE_FORWARD:
978                                          f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0];                                          f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0];
979                                            pMB->sad16 = best_sad;
980                                          break;                                          break;
981                                  case MODE_BACKWARD:                                  case MODE_BACKWARD:
982                                          b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0];                                          b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0];
983                                            pMB->sad16 = best_sad;
984                                          break;                                          break;
985                                  case MODE_INTERPOLATE:                                  case MODE_INTERPOLATE:
986                                          f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0];                                          f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0];
987                                          b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0];                                          b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0];
988                                            pMB->sad16 = best_sad;
989                                          break;                                          break;
990                                  default:                                  default:
991                                            pMB->sad16 = best_sad;
992                                          break;                                          break;
993                          }                          }
994                  }                  }
995          }          }
996  }  }
   

Legend:
Removed from v.1.1.2.4  
changed lines
  Added in v.1.6

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