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

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

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

revision 1.1.2.1, Wed Sep 10 22:18:59 2003 UTC revision 1.16, Mon Mar 14 00:47:08 2005 UTC
# Line 41  Line 41 
41  #include "motion_inlines.h"  #include "motion_inlines.h"
42    
43  static const int xvid_me_lambda_vec8[32] =  static const int xvid_me_lambda_vec8[32] =
44          {     0    ,(int)(1.00235 * NEIGH_TEND_8X8 + 0.5),          {     0    ,(int)(1.0 * NEIGH_TEND_8X8 + 0.5),
45          (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),
46          (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),
47          (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),
48          (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),
49          (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),
50          (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),
51          (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),
52          (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),
53          (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),
54          (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),
55          (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),
56          (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),
57          (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),
58          (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),
59          (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)
60  };  };
61    
62  static void  static void
63  CheckCandidate16(const int x, const int y, const SearchData * const data, const unsigned int Direction)  CheckCandidate16(const int x, const int y, SearchData * const data, const unsigned int Direction)
64  {  {
         int xc, yc;  
65          const uint8_t * Reference;          const uint8_t * Reference;
66            int32_t sad, xc, yc; uint32_t t;
67          VECTOR * current;          VECTOR * current;
         int32_t sad; uint32_t t;  
68    
69          if ( (x > data->max_dx) || (x < data->min_dx)          if ( (x > data->max_dx) || (x < data->min_dx)
70                  || (y > data->max_dy) || (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
71    
72          if (!data->qpel_precision) {          if (data->qpel_precision) { /* x and y are in 1/4 precision */
73                    Reference = xvid_me_interpolate16x16qpel(x, y, 0, data);
74                    current = data->currentQMV;
75                    xc = x/2; yc = y/2;
76            } else {
77                  Reference = GetReference(x, y, data);                  Reference = GetReference(x, y, data);
78                  current = data->currentMV;                  current = data->currentMV;
79                  xc = x; yc = y;                  xc = x; yc = y;
         } else { /* x and y are in 1/4 precision */  
                 Reference = xvid_me_interpolate16x16qpel(x, y, 0, data);  
                 xc = x/2; yc = y/2; /* for chroma sad */  
                 current = data->currentQMV;  
80          }          }
81    
82          sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp);          sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp);
83          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);
84    
85          sad += (data->lambda16 * t * sad)>>10;          sad += (data->lambda16 * t);
86          data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10;          data->temp[0] += (data->lambda8 * t);
87    
88          if (data->chroma && sad < data->iMinSAD[0])          if (data->chroma) {
89                    if (sad >= data->iMinSAD[0]) goto no16;
90                  sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],                  sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],
91                                                                  (yc >> 1) + roundtab_79[yc & 0x3], data);                                                                  (yc >> 1) + roundtab_79[yc & 0x3], data);
92            }
93    
94          if (sad < data->iMinSAD[0]) {          if (sad < data->iMinSAD[0]) {
95                  data->iMinSAD[0] = sad;                  data->iMinSAD[0] = sad;
96                  current[0].x = x; current[0].y = y;                  current[0].x = x; current[0].y = y;
97                  *data->dir = Direction;                  data->dir = Direction;
98          }          }
99    
100    no16:
101          if (data->temp[0] < data->iMinSAD[1]) {          if (data->temp[0] < data->iMinSAD[1]) {
102                  data->iMinSAD[1] = data->temp[0]; current[1].x = x; current[1].y = y; }                  data->iMinSAD[1] = data->temp[0]; current[1].x = x; current[1].y = y; }
103          if (data->temp[1] < data->iMinSAD[2]) {          if (data->temp[1] < data->iMinSAD[2]) {
# Line 107  Line 109 
109  }  }
110    
111  static void  static void
112  CheckCandidate16_subpel(const int x, const int y, const SearchData * const data, const unsigned int Direction)  CheckCandidate8(const int x, const int y, SearchData * const data, const unsigned int Direction)
 {  
         int xc, yc;  
         const uint8_t *Reference;  
         VECTOR *current, *current2;  
         int32_t sad; uint32_t t;  
   
         if ( (x > data->max_dx) || (x < data->min_dx)  
                 || (y > data->max_dy) || (y < data->min_dy) ) return;  
   
         if (!data->qpel_precision) {  
                 Reference = GetReference(x, y, data);  
                 current = data->currentMV;  
                 current2 = data->currentMV2;  
                 xc = x; yc = y;  
         } else { /* x and y are in 1/4 precision */  
                 Reference = xvid_me_interpolate16x16qpel(x, y, 0, data);  
                 xc = x/2; yc = y/2; /* for chroma sad */  
                 current = data->currentQMV;  
                 current2 = data->currentQMV2;  
         }  
   
         sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp);  
         t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);  
   
         sad += (data->lambda16 * t * sad)>>10;  
         data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10;  
   
         if (data->chroma && sad < data->iMinSAD[0])  
                 sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],  
                                                                 (yc >> 1) + roundtab_79[yc & 0x3], data);  
   
         if (data->temp[0] < data->iMinSAD[1]) {  
                 data->iMinSAD[1] = data->temp[0]; current[1].x = x; current[1].y = y; }  
         if (data->temp[1] < data->iMinSAD[2]) {  
                 data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; }  
         if (data->temp[2] < data->iMinSAD[3]) {  
                 data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; }  
         if (data->temp[3] < data->iMinSAD[4]) {  
                 data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; }  
   
         if (sad < data->iMinSAD[0]) {  
                 *(data->iMinSAD2) = *(data->iMinSAD);  
                 current2->x = current->x; current2->y = current->y;  
   
                 data->iMinSAD[0] = sad;  
                 current[0].x = x; current[0].y = y;  
                 *data->dir = Direction;  
                 return;  
         }  
   
         if (sad < *(data->iMinSAD2)) {  
                 *(data->iMinSAD2) = sad;  
                 current2->x = x; current2->y = y;  
                 *data->dir = Direction;  
         }  
 }  
   
 static void  
 CheckCandidate8(const int x, const int y, const SearchData * const data, const unsigned int Direction)  
113  {  {
114          int32_t sad; uint32_t t;          int32_t sad; uint32_t t;
115          const uint8_t * Reference;          const uint8_t * Reference;
# Line 184  Line 127 
127          }          }
128    
129          sad = sad8(data->Cur, Reference, data->iEdgedWidth);          sad = sad8(data->Cur, Reference, data->iEdgedWidth);
130          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);
131    
132          sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10;          sad += (data->lambda8 * t);
133    
134          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
135                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
136                  current->x = x; current->y = y;                  current->x = x; current->y = y;
137                  *data->dir = Direction;                  data->dir = Direction;
138          }          }
139  }  }
140    
 static void  
 CheckCandidate32(const int x, const int y, const 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; }  
 }  
   
 static void  
 SubpelRefine_Fast(SearchData * data, CheckFunc * CheckCandidate)  
 {  
 /* Do a half-pel or q-pel refinement */  
         VECTOR centerMV;  
         VECTOR second_best;  
         int best_sad = *data->iMinSAD;  
         int xo, yo, xo2, yo2;  
         int size = 2;  
         CheckFunc *backupFunc = CheckCandidate;  
   
         if(data->qpel_precision)  
                 size = 1;  
   
         centerMV = *data->currentMV;  
         *data->iMinSAD = 256 * 4096;  
   
         CHECK_CANDIDATE(centerMV.x, centerMV.y - size, 0);  
         CHECK_CANDIDATE(centerMV.x + size, centerMV.y - size, 0);  
         CHECK_CANDIDATE(centerMV.x + size, centerMV.y, 0);  
         CHECK_CANDIDATE(centerMV.x + size, centerMV.y + size, 0);  
   
         CHECK_CANDIDATE(centerMV.x, centerMV.y + size, 0);  
         CHECK_CANDIDATE(centerMV.x - size, centerMV.y + size, 0);  
         CHECK_CANDIDATE(centerMV.x - size, centerMV.y, 0);  
         CHECK_CANDIDATE(centerMV.x - size, centerMV.y - size, 0);  
   
         second_best = *data->currentMV;  
   
         if(data->qpel_precision) {  
                 second_best.x *= 2;     second_best.y *= 2;  
         }  
   
         data->currentMV[0] = centerMV;  
         *data->iMinSAD = best_sad;  
   
     centerMV = data->qpel_precision ? *data->currentQMV : *data->currentMV;  
   
         xo = centerMV.x;  
         yo = centerMV.y;  
         xo2 = second_best.x;  
         yo2 = second_best.y;  
   
         CheckCandidate = CheckCandidate16_subpel;  
         *data->iMinSAD2 = 256 * 4096;  
   
         if (yo == yo2)  
         {  
                 CHECK_CANDIDATE((xo+xo2)>>1, yo, 0);  
                 CHECK_CANDIDATE(xo, yo-1, 0);  
                 CHECK_CANDIDATE(xo, yo+1, 0);  
   
                 if(best_sad <= *data->iMinSAD2)  
                         goto ende;  
   
                 if(data->currentQMV[0].x == data->currentQMV2[0].x) {  
                         CHECK_CANDIDATE((xo+xo2)>>1, yo-1, 0);  
                         CHECK_CANDIDATE((xo+xo2)>>1, yo+1, 0);  
                         goto ende;  
                 }  
                 else {  
                         CHECK_CANDIDATE((xo+xo2)>>1,  
                                 (data->currentQMV[0].x == xo) ? data->currentQMV[0].y : data->currentQMV2[0].y,  
                                 0);  
                         goto ende;  
                 }  
         }  
   
         if (xo == xo2)  
         {  
                 CHECK_CANDIDATE(xo, (yo+yo2)>>1, 0);  
                 CHECK_CANDIDATE(xo-1, yo, 0);  
                 CHECK_CANDIDATE(xo+1, yo, 0);  
   
                 if(best_sad < *data->iMinSAD2)  
                         goto ende;  
   
                 if(data->currentQMV[0].y == data->currentQMV2[0].y) {  
                         CHECK_CANDIDATE(xo-1, (yo+yo2)>>1, 0);  
                         CHECK_CANDIDATE(xo+1, (yo+yo2)>>1, 0);  
                         goto ende;  
                 }  
                 else {  
                         CHECK_CANDIDATE((data->currentQMV[0].y == yo) ? data->currentQMV[0].x : data->currentQMV2[0].x, (yo+yo2)>>1, 0);  
                         goto ende;  
                 }  
         }  
   
         CHECK_CANDIDATE(xo, (yo+yo2)>>1, 0);  
         CHECK_CANDIDATE((xo+xo2)>>1, yo, 0);  
   
         if(best_sad <= *data->iMinSAD2)  
                 goto ende;  
   
         CHECK_CANDIDATE((xo+xo2)>>1, (yo+yo2)>>1, 0);  
   
 ende:  
         CheckCandidate = backupFunc;  
 }  
   
141  int  int
142  xvid_me_SkipDecisionP(const IMAGE * current, const IMAGE * reference,  xvid_me_SkipDecisionP(const IMAGE * current, const IMAGE * reference,
143                                                          const int x, const int y,                                                          const int x, const int y,
144                                                          const uint32_t stride, const uint32_t iQuant, int rrv)                                                          const uint32_t stride, const uint32_t iQuant)
145  {  {
146          int offset = (x + y*stride)*8;          int offset = (x + y*stride)*8;
         if(!rrv) {  
147                  uint32_t sadC = sad8(current->u + offset,                  uint32_t sadC = sad8(current->u + offset,
148                                                  reference->u + offset, stride);                                                  reference->u + offset, stride);
149                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
# Line 344  Line 151 
151                                                  reference->v + offset, stride);                                                  reference->v + offset, stride);
152                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
153                  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;  
         }  
154  }  }
155    
156          /*          /*
# Line 369  Line 166 
166           *  [3]: topright neighbour's SAD           *  [3]: topright neighbour's SAD
167           */           */
168    
169  static __inline int  static __inline void
170  get_pmvdata2(const MACROBLOCK * const mbs,  get_pmvdata2(const MACROBLOCK * const mbs,
171                  const int mb_width,                  const int mb_width,
172                  const int bound,                  const int bound,
173                  const int x,                  const int x,
174                  const int y,                  const int y,
                 const int block,  
175                  VECTOR * const pmv,                  VECTOR * const pmv,
176                  int32_t * const psad)                  int32_t * const psad)
177  {  {
# Line 385  Line 181 
181          int lpos, tpos, rpos;          int lpos, tpos, rpos;
182          int num_cand = 0, last_cand = 1;          int num_cand = 0, last_cand = 1;
183    
         switch (block) {  
         case 0:  
184                  lx = x - 1;     ly = y;         lz = 1;                  lx = x - 1;     ly = y;         lz = 1;
185                  tx = x;         ty = y - 1;     tz = 2;                  tx = x;         ty = y - 1;     tz = 2;
186                  rx = x + 1;     ry = y - 1;     rz = 2;                  rx = x + 1;     ry = y - 1;     rz = 2;
                 break;  
         case 1:  
                 lx = x;         ly = y;         lz = 0;  
                 tx = x;         ty = y - 1;     tz = 3;  
                 rx = x + 1;     ry = y - 1;     rz = 2;  
                 break;  
         case 2:  
                 lx = x - 1;     ly = y;         lz = 3;  
                 tx = x;         ty = y;         tz = 0;  
                 rx = x;         ry = y;         rz = 1;  
                 break;  
         default:  
                 lx = x;         ly = y;         lz = 2;  
                 tx = x;         ty = y;         tz = 0;  
                 rx = x;         ry = y;         rz = 1;  
         }  
187    
188          lpos = lx + ly * mb_width;          lpos = lx + ly * mb_width;
189          rpos = rx + ry * mb_width;          rpos = rx + ry * mb_width;
# Line 442  Line 220 
220          }          }
221    
222          /* original pmvdata() compatibility hack */          /* original pmvdata() compatibility hack */
223          if (x == 0 && y == 0 && block == 0) {          if (x == 0 && y == 0) {
224                  pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV;                  pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV;
225                  psad[0] = 0;                  psad[0] = 0;
226                  psad[1] = psad[2] = psad[3] = MV_MAX_ERROR;                  psad[1] = psad[2] = psad[3] = MV_MAX_ERROR;
227                  return 0;                  return;
228          }          }
229    
230          /* if only one valid candidate preictor, the invalid candiates are set to the canidate */          /* if only one valid candidate preictor, the invalid candiates are set to the canidate */
231          if (num_cand == 1) {          if (num_cand == 1) {
232                  pmv[0] = pmv[last_cand];                  pmv[0] = pmv[last_cand];
233                  psad[0] = psad[last_cand];                  psad[0] = psad[last_cand];
234  #if 0                  return;
                 return MVequal(pmv[0], zeroMV); /* no point calculating median mv and minimum sad */  
 #endif  
   
                 /* original pmvdata() compatibility hack */  
                 return y==0 && block <= 1 ? 0 : MVequal(pmv[0], zeroMV);  
235          }          }
236    
237          if ((MVequal(pmv[1], pmv[2])) && (MVequal(pmv[1], pmv[3]))) {          if ((MVequal(pmv[1], pmv[2])) && (MVequal(pmv[1], pmv[3]))) {
238                  pmv[0] = pmv[1];                  pmv[0] = pmv[1];
239                  psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);                  psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);
240                  return 1;                  return;
241          }          }
242    
243          /* set median, minimum */          /* set median, minimum */
# Line 478  Line 251 
251    
252          psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);          psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);
253    
         return 0;  
254  }  }
255    
256    
# Line 494  Line 266 
266                                  const IMAGE * const pCurrent,                                  const IMAGE * const pCurrent,
267                                  const IMAGE * const pRef,                                  const IMAGE * const pRef,
268                                  const IMAGE * const vGMC,                                  const IMAGE * const vGMC,
269                                  const int coding_type)                                  const int coding_type,
270                                    const int skip_sad)
271  {  {
272          int mode = MODE_INTER;          int mode = MODE_INTER;
273          int mcsel = 0;          int mcsel = 0;
# Line 520  Line 293 
293          }          }
294    
295          /* 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?" */
296          if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP))          if (skip_possible && (skip_sad < (int)iQuant * MAX_SAD00_FOR_SKIP))
297                  if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH)                  if ( (100*skip_sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH)
298                          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)) {
299                                  mode = MODE_NOT_CODED;                                  mode = MODE_NOT_CODED;
300                                  sad = 0;                                  sad = 0;
301                          }                          }
# Line 547  Line 320 
320    
321          /* intra decision */          /* intra decision */
322    
323          if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */          if (iQuant > 10) InterBias += 60 * (iQuant - 10); /* to make high quants work */
324          if (y != 0)          if (y != 0)
325                  if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;                  if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;
326          if (x != 0)          if (x != 0)
327                  if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;                  if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
328    
329          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;  
330    
331          if (InterBias < sad) {          if (InterBias < sad) {
332                  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);  
   
333                  if (deviation < (sad - InterBias)) mode = MODE_INTRA;                  if (deviation < (sad - InterBias)) mode = MODE_INTRA;
334          }          }
335    
336          pMB->cbp = 63;          pMB->cbp = 63;
337          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;
338    
         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);  
         }  
   
339          if (mode == MODE_INTER && mcsel == 0) {          if (mode == MODE_INTER && mcsel == 0) {
340                  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];
341    
# Line 590  Line 349 
349                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
350                  }                  }
351    
352          } else if (mode == MODE_INTER ) { // but mcsel == 1          } else if (mode == MODE_INTER ) { /* but mcsel == 1 */
353    
354                  pMB->mcsel = 1;                  pMB->mcsel = 1;
355                  if (Data->qpel) {                  if (Data->qpel) {
# Line 610  Line 369 
369    
370  static __inline void  static __inline void
371  PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,  PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,
372                          int iHcount, const MACROBLOCK * const prevMB, int rrv)                          int iHcount, const MACROBLOCK * const prevMB)
373  {  {
         /* this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself */  
         if (rrv) { iWcount /= 2; iHcount /= 2; }  
374    
375          if ( (y != 0) && (x < (iWcount-1)) ) {          /* [5] top-right neighbour */          if ( (y != 0) && (x < (iWcount-1)) ) {          /* [5] top-right neighbour */
376                  pmv[5].x = EVEN(pmv[3].x);                  pmv[5].x = EVEN(pmv[3].x);
# Line 638  Line 395 
395                  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 */
396                  pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);                  pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);
397          } 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);  
                 }  
         }  
398  }  }
399    
400  static void  static void
401  Search8(const SearchData * const OldData,  Search8(SearchData * const OldData,
402                  const int x, const int y,                  const int x, const int y,
403                  const uint32_t MotionFlags,                  const uint32_t MotionFlags,
404                  const MBParam * const pParam,                  const MBParam * const pParam,
# Line 659  Line 408 
408                  SearchData * const Data)                  SearchData * const Data)
409  {  {
410          int i = 0;          int i = 0;
411          CheckFunc * CheckCandidate;          VECTOR vbest_q; int32_t sbest_q;
412          Data->iMinSAD = OldData->iMinSAD + 1 + block;          *Data->iMinSAD = *(OldData->iMinSAD + 1 + block);
413          Data->currentMV = OldData->currentMV + 1 + block;          *Data->currentMV = *(OldData->currentMV + 1 + block);
414          Data->currentQMV = OldData->currentQMV + 1 + block;          *Data->currentQMV = *(OldData->currentQMV + 1 + block);
415    
416          if(Data->qpel) {          if(Data->qpel) {
417                  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);
418                  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,
419                                                                                  Data->predMV, Data->iFcode, 0, 0);                                                                                  Data->predMV, Data->iFcode, 0);
420          } else {          } else {
421                  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);
422                  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,
423                                                                                  Data->predMV, Data->iFcode, 0, Data->rrv);                                                                                  Data->predMV, Data->iFcode, 0);
424          }          }
425    
426          *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;          *(Data->iMinSAD) += (Data->lambda8 * i);
427    
428          if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {          if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {
429    
430                  if (Data->rrv) i = 16; else i = 8;                  vbest_q = Data->currentQMV[0];
431                    sbest_q = Data->iMinSAD[0];
432    
                 Data->RefP[0] = OldData->RefP[0] + i * ((block&1) + Data->iEdgedWidth*(block>>1));  
                 Data->RefP[1] = OldData->RefP[1] + i * ((block&1) + Data->iEdgedWidth*(block>>1));  
                 Data->RefP[2] = OldData->RefP[2] + i * ((block&1) + Data->iEdgedWidth*(block>>1));  
                 Data->RefP[3] = OldData->RefP[3] + i * ((block&1) + Data->iEdgedWidth*(block>>1));  
433    
434                  Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->RefP[0] = OldData->RefP[0] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
435                    Data->RefP[1] = OldData->RefP[1] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
436                    Data->RefP[2] = OldData->RefP[2] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
437                    Data->RefP[3] = OldData->RefP[3] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
438    
439                    Data->Cur = OldData->Cur + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
440                  Data->qpel_precision = 0;                  Data->qpel_precision = 0;
441    
442                  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,
443                                          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;  
444    
445                  if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_RD))) {                  if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_RD))) {
                         int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */  
446    
447                          MainSearchFunc *MainSearchPtr;                          MainSearchFunc *MainSearchPtr;
448                          if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = xvid_me_SquareSearch;                          if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = xvid_me_SquareSearch;
449                                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = xvid_me_AdvDiamondSearch;                                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = xvid_me_AdvDiamondSearch;
450                                          else MainSearchPtr = xvid_me_DiamondSearch;                                          else MainSearchPtr = xvid_me_DiamondSearch;
451    
452                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidate);                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidate8);
453                    }
454    
455                          if(*(Data->iMinSAD) < temp_sad) {                  if(!Data->qpel) {
456                                          Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */                          /* halfpel mode */
457                            if (MotionFlags & XVID_ME_HALFPELREFINE8)
458                                    /* perform halfpel refine of current best vector */
459                                    xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate8, 0);
460                    } else {
461                            /* qpel mode */
462                            Data->currentQMV->x = 2*Data->currentMV->x;
463                                          Data->currentQMV->y = 2 * Data->currentMV->y;                                          Data->currentQMV->y = 2 * Data->currentMV->y;
                         }  
                 }  
464    
465                            if(MotionFlags & XVID_ME_FASTREFINE8) {
466                                    /* fast */
467                                    get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,
468                                            pParam->width, pParam->height, Data->iFcode, 2);
469                                    FullRefine_Fast(Data, CheckCandidate8, 0);
470                            } else if(MotionFlags & XVID_ME_QUARTERPELREFINE8) {
471                                    /* full */
472                  if (MotionFlags & XVID_ME_HALFPELREFINE8) {                  if (MotionFlags & XVID_ME_HALFPELREFINE8) {
473                          int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */                                          xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate8, 0); /* hpel part */
474                                            Data->currentQMV->x = 2*Data->currentMV->x;
                         xvid_me_SubpelRefine(Data, CheckCandidate); /* perform halfpel refine of current best vector */  
   
                         if(*(Data->iMinSAD) < temp_sad) { /* we have found a better match */  
                                 Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */  
475                                  Data->currentQMV->y = 2 * Data->currentMV->y;                                  Data->currentQMV->y = 2 * Data->currentMV->y;
476                          }                          }
                 }  
477    
                 if (Data->qpel && MotionFlags & XVID_ME_QUARTERPELREFINE8) {  
                                 Data->qpel_precision = 1;  
478                                  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,
479                                          pParam->width, pParam->height, Data->iFcode, 2, 0);                                          pParam->width, pParam->height, Data->iFcode, 2);
480                                  xvid_me_SubpelRefine(Data, CheckCandidate);                                  Data->qpel_precision = 1;
481    
482                                    xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate8, 0); /* qpel part */
483                  }                  }
484          }          }
485    
486          if (Data->rrv) {                  if (sbest_q <= Data->iMinSAD[0]) /* we have not found a better match */
487                          Data->currentMV->x = RRV_MV_SCALEDOWN(Data->currentMV->x);                          Data->currentQMV[0] = vbest_q;
488                          Data->currentMV->y = RRV_MV_SCALEDOWN(Data->currentMV->y);  
489          }          }
490    
491          if(Data->qpel) {          if(Data->qpel) {
# Line 743  Line 497 
497                  pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;                  pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;
498          }          }
499    
500            *(OldData->iMinSAD + 1 + block) = *Data->iMinSAD;
501            *(OldData->currentMV + 1 + block) = *Data->currentMV;
502            *(OldData->currentQMV + 1 + block) = *Data->currentQMV;
503    
504          pMB->mvs[block] = *Data->currentMV;          pMB->mvs[block] = *Data->currentMV;
505          pMB->sad8[block] = 4 * *Data->iMinSAD;          pMB->sad8[block] = 4 * *Data->iMinSAD;
506  }  }
# Line 772  Line 530 
530          CheckFunc * CheckCandidate;          CheckFunc * CheckCandidate;
531    
532          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,
533                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 1);
534    
535          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, pmv, Data->temp);
536    
537          Data->temp[5] = Data->temp[6] = 0; /* chroma-sad cache */          Data->chromaX = Data->chromaY = 0; /* chroma-sad cache */
538          i = Data->rrv ? 2 : 1;          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
539          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i;          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;
540          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;          Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;
541          Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;  
542            Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16;
543          Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16;
544          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16;
545          Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16;
546          Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
547          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;  
548    
549          Data->lambda16 = xvid_me_lambda_vec16[pMB->quant];          Data->lambda16 = xvid_me_lambda_vec16[pMB->quant];
550          Data->lambda8 = xvid_me_lambda_vec8[pMB->quant];          Data->lambda8 = xvid_me_lambda_vec8[pMB->quant];
551          Data->qpel_precision = 0;          Data->qpel_precision = 0;
552          *Data->dir = 0;          Data->dir = 0;
553    
554          memset(Data->currentMV, 0, 5*sizeof(VECTOR));          memset(Data->currentMV, 0, 5*sizeof(VECTOR));
555    
556          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);
557          else Data->predMV = pmv[0];          else Data->predMV = pmv[0];
558    
559          i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0, 0);          i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0);
560          Data->iMinSAD[0] = pMB->sad16 + ((Data->lambda16 * i * pMB->sad16)>>10);          Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i);
561          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);
562          Data->iMinSAD[2] = pMB->sad8[1];          Data->iMinSAD[2] = pMB->sad8[1];
563          Data->iMinSAD[3] = pMB->sad8[2];          Data->iMinSAD[3] = pMB->sad8[2];
564          Data->iMinSAD[4] = pMB->sad8[3];          Data->iMinSAD[4] = pMB->sad8[3];
# Line 814  Line 571 
571                  threshA = 512;                  threshA = 512;
572    
573          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
574                                          prevMBs + x + y * pParam->mb_width, Data->rrv);                                          prevMBs + x + y * pParam->mb_width);
575    
         if (!Data->rrv) {  
576                  if (inter4v) CheckCandidate = CheckCandidate16;                  if (inter4v) CheckCandidate = CheckCandidate16;
577                          else CheckCandidate = CheckCandidate16no4v; /* for extra speed */                          else CheckCandidate = CheckCandidate16no4v; /* for extra speed */
         } else CheckCandidate = CheckCandidate32;  
578    
579  /* 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())*/
580    
# Line 836  Line 591 
591          else {          else {
592    
593                  MainSearchFunc * MainSearchPtr;                  MainSearchFunc * MainSearchPtr;
594                  int mask = make_mask(pmv, i, *Data->dir); // all vectors pmv[0..i-1] have been checked                  int mask = make_mask(pmv, i, Data->dir); /* all vectors pmv[0..i-1] have been checked */
595    
596                  if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;                  if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;
597                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;
# Line 851  Line 606 
606                  if (MotionFlags & XVID_ME_EXTSEARCH16) {                  if (MotionFlags & XVID_ME_EXTSEARCH16) {
607                          int32_t bSAD;                          int32_t bSAD;
608                          VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];                          VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];
609                          if (Data->rrv) {                          if (Data->qpel) {
610                                  startMV.x = RRV_MV_SCALEUP(startMV.x);                                  startMV.x /= 2;
611                                  startMV.y = RRV_MV_SCALEUP(startMV.y);                                  startMV.y /= 2;
612                          }                          }
613                          if (!(MVequal(startMV, backupMV))) {                          if (!(MVequal(startMV, backupMV))) {
614                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
615    
616                                  CheckCandidate(startMV.x, startMV.y, Data, 255);                                  CheckCandidate(startMV.x, startMV.y, Data, 255);
617                                  MainSearchPtr(startMV.x, startMV.y, Data, 255, CheckCandidate);                                  xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate);
618                                  if (bSAD < Data->iMinSAD[0]) {                                  if (bSAD < Data->iMinSAD[0]) {
619                                          Data->currentMV[0] = backupMV;                                          Data->currentMV[0] = backupMV;
620                                          Data->iMinSAD[0] = bSAD; }                                          Data->iMinSAD[0] = bSAD;
621                                    }
622                          }                          }
623    
624                          backupMV = Data->currentMV[0];                          backupMV = Data->currentMV[0];
# Line 871  Line 627 
627                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
628    
629                                  CheckCandidate(startMV.x, startMV.y, Data, 255);                                  CheckCandidate(startMV.x, startMV.y, Data, 255);
630                                  MainSearchPtr(startMV.x, startMV.y, Data, 255, CheckCandidate);                                  xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate);
631                                  if (bSAD < Data->iMinSAD[0]) {                                  if (bSAD < Data->iMinSAD[0]) {
632                                          Data->currentMV[0] = backupMV;                                          Data->currentMV[0] = backupMV;
633                                          Data->iMinSAD[0] = bSAD;                                          Data->iMinSAD[0] = bSAD;
# Line 880  Line 636 
636                  }                  }
637          }          }
638    
639    
640            if(!Data->qpel) {
641                    /* halfpel mode */
642          if (MotionFlags & XVID_ME_HALFPELREFINE16)          if (MotionFlags & XVID_ME_HALFPELREFINE16)
643                          xvid_me_SubpelRefine(Data, CheckCandidate);                                  xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0);
644            } else {
645                    /* qpel mode */
646    
647          for(i = 0; i < 5; i++) {          for(i = 0; i < 5; i++) {
648                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */
649                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
650          }          }
651                    if(MotionFlags & XVID_ME_FASTREFINE16 && MotionFlags & XVID_ME_QUARTERPELREFINE16) {
652          if (Data->qpel) {                          /* fast */
653                            get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
654                                                    pParam->width, pParam->height, Data->iFcode, 2);
655                            FullRefine_Fast(Data, CheckCandidate, 0);
656                    } else {
657                            if(MotionFlags & (XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE16_RD)) {
658                                    /* full */
659                                    if (MotionFlags & XVID_ME_HALFPELREFINE16) {
660                                            xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0); /* hpel part */
661                                            for(i = 0; i < 5; i++) {
662                                                    Data->currentQMV[i].x = 2 * Data->currentMV[i].x;
663                                                    Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
664                                            }
665                                    }
666                  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,
667                                  pParam->width, pParam->height, Data->iFcode, 2, 0);                                                          pParam->width, pParam->height, Data->iFcode, 2);
668                  Data->qpel_precision = 1;                  Data->qpel_precision = 1;
669                  if (MotionFlags & XVID_ME_QUARTERPELREFINE16) {                                  if(MotionFlags & XVID_ME_QUARTERPELREFINE16)
670                          if(MotionFlags & XVID_ME_FASTREFINE16)                                          xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate, 0); /* qpel part */
671                                  SubpelRefine_Fast(Data, CheckCandidate);                          }
                         else  
                                 xvid_me_SubpelRefine(Data, CheckCandidate);  
672                  }                  }
673          }          }
674    
675          if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30)          if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30 * ((MotionFlags & XVID_ME_FASTREFINE16) ? 8 : 1))
676                  inter4v = 0;                  inter4v = 0;
677    
678          if (inter4v) {          if (inter4v) {
# Line 913  Line 685 
685                  Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);                  Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
686    
687                  if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_RD))) {                  if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_RD))) {
688                          /* chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, it will not be used */                          /* chroma is only used for comparison to INTER. if the comparison will be done in RD domain, it will not be used */
689                          int sumx = 0, sumy = 0;                          int sumx = 0, sumy = 0;
690    
691                          if (Data->qpel)                          if (Data->qpel)
# Line 958  Line 730 
730          if (Flags & XVID_ME_QUARTERPELREFINE8_RD)          if (Flags & XVID_ME_QUARTERPELREFINE8_RD)
731                  Flags &= ~XVID_ME_QUARTERPELREFINE8;                  Flags &= ~XVID_ME_QUARTERPELREFINE8;
732    
733            if (Flags & XVID_ME_QUARTERPELREFINE16_RD)
734                    Flags &= ~XVID_ME_QUARTERPELREFINE16;
735    
736          if (!(VolFlags & XVID_VOL_QUARTERPEL))          if (!(VolFlags & XVID_VOL_QUARTERPEL))
737                  Flags &= ~(XVID_ME_QUARTERPELREFINE16+XVID_ME_QUARTERPELREFINE8+XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD);                  Flags &= ~(XVID_ME_QUARTERPELREFINE16+XVID_ME_QUARTERPELREFINE8+XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD);
738    
739          if (!(VopFlags & XVID_VOP_HALFPEL))          if (!(VopFlags & XVID_VOP_HALFPEL))
740                  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);
741    
742          if ((VopFlags & XVID_VOP_GREYSCALE) || (VopFlags & XVID_VOP_REDUCED))          if (VopFlags & XVID_VOP_GREYSCALE)
743                  Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP);                  Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP);
744    
745            if (Flags & XVID_ME_FASTREFINE8)
746                    Flags &= ~XVID_ME_HALFPELREFINE8_RD;
747    
748            if (Flags & XVID_ME_FASTREFINE16)
749                    Flags &= ~XVID_ME_HALFPELREFINE16_RD;
750    
751          return Flags;          return Flags;
752  }  }
753    
754    static __inline void
755    motionStatsPVOP(int * const MVmax, int * const mvCount, int * const mvSum,
756                                    const MACROBLOCK * const pMB, const int qpel)
757    {
758            const VECTOR * const mv = qpel ? pMB->qmvs : pMB->mvs;
759            int i;
760            int max = *MVmax;
761    
762            switch (pMB->mode) {
763            case MODE_INTER4V:
764                    *mvCount += 3;
765                    for(i = 3; i; i--) {
766                            if (mv[i].x > max) max = mv[i].x;
767                            else if (-mv[i].x - 1 > max) max = -mv[i].x - 1;
768                            *mvSum += mv[i].x * mv[i].x;
769                            if (mv[i].y > max) max = mv[i].y;
770                            else if (-mv[i].y - 1 > max) max = -mv[i].y - 1;
771                            *mvSum += mv[i].y * mv[i].y;
772                    }
773            case MODE_INTER:
774                    (*mvCount)++;
775                    *mvSum += mv[0].x * mv[0].x;
776                    *mvSum += mv[0].y * mv[0].y;
777                    if (mv[0].x > max) max = mv[0].x;
778                    else if (-mv[0].x - 1 > max) max = -mv[0].x - 1;
779                    if (mv[0].y > max) max = mv[0].y;
780                    else if (-mv[0].y - 1 > max) max = -mv[0].y - 1;
781                    *MVmax = max;
782            default:
783                    break;
784            }
785    }
786    
787  bool  bool
788  MotionEstimation(MBParam * const pParam,  MotionEstimation(MBParam * const pParam,
789                                   FRAMEINFO * const current,                                   FRAMEINFO * const current,
# Line 984  Line 798 
798          const IMAGE *const pCurrent = &current->image;          const IMAGE *const pCurrent = &current->image;
799          const IMAGE *const pRef = &reference->image;          const IMAGE *const pRef = &reference->image;
800    
801          uint32_t mb_width = pParam->mb_width;          const uint32_t mb_width = pParam->mb_width;
802          uint32_t mb_height = pParam->mb_height;          const uint32_t mb_height = pParam->mb_height;
803          const uint32_t iEdgedWidth = pParam->edged_width;          const uint32_t iEdgedWidth = pParam->edged_width;
804          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);
805          int stat_thresh = 0;          int stat_thresh = 0;
806            int MVmax = 0, mvSum = 0, mvCount = 0;
807    
808          uint32_t x, y;          uint32_t x, y;
         uint32_t iIntra = 0;  
809          int32_t sad00;          int32_t sad00;
810          int skip_thresh = INITIAL_SKIP_THRESH * \          int skip_thresh = INITIAL_SKIP_THRESH * \
                 (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \  
811                  (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);                  (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);
812    
813          /* some pre-initialized thingies for SearchP */          /* some pre-initialized thingies for SearchP */
         int32_t temp[8]; uint32_t dir;  
         VECTOR currentMV[5];  
         VECTOR currentQMV[5];  
         VECTOR currentMV2[5];  
         VECTOR currentQMV2[5];  
         int32_t iMinSAD[5];  
         int32_t iMinSAD2[5];  
814          DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);
815          SearchData Data;          SearchData Data;
816          memset(&Data, 0, sizeof(SearchData));          memset(&Data, 0, sizeof(SearchData));
817          Data.iEdgedWidth = iEdgedWidth;          Data.iEdgedWidth = iEdgedWidth;
         Data.currentMV = currentMV;  
         Data.currentQMV = currentQMV;  
         Data.currentMV2 = currentMV2;  
         Data.currentQMV2 = currentQMV2;  
         Data.iMinSAD = iMinSAD;  
         Data.iMinSAD2 = iMinSAD2;  
         Data.temp = temp;  
         Data.dir = &dir;  
818          Data.iFcode = current->fcode;          Data.iFcode = current->fcode;
819          Data.rounding = pParam->m_rounding_type;          Data.rounding = pParam->m_rounding_type;
820          Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);          Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);
821          Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP;          Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP;
         Data.rrv = (current->vop_flags & XVID_VOP_REDUCED) ? 1:0;  
822          Data.dctSpace = dct_space;          Data.dctSpace = dct_space;
823          Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);          Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
824            Data.mpeg_quant_matrices = pParam->mpeg_quant_matrices;
         if ((current->vop_flags & XVID_VOP_REDUCED)) {  
                 mb_width = (pParam->width + 31) / 32;  
                 mb_height = (pParam->height + 31) / 32;  
                 Data.qpel = 0;  
         }  
825    
826          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) */
827          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();
# Line 1039  Line 831 
831                          MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];                          MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];
832                          MACROBLOCK *prevMB = &reference->mbs[x + y * pParam->mb_width];                          MACROBLOCK *prevMB = &reference->mbs[x + y * pParam->mb_width];
833    
834                          if (!Data.rrv) pMB->sad16 =                          pMB->sad16 =
835                                  sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16,                                  sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16,
836                                                          pRef->y + (x + y * iEdgedWidth) * 16,                                                          pRef->y + (x + y * iEdgedWidth) * 16,
837                                                          pParam->edged_width, pMB->sad8 );                                                          pParam->edged_width, pMB->sad8 );
838    
839                          else pMB->sad16 =                          sad00 = 4*MAX(MAX(pMB->sad8[0], pMB->sad8[1]), MAX(pMB->sad8[2], pMB->sad8[3]));
                                 sad32v_c(pCurrent->y + (x + y * iEdgedWidth) * 32,  
                                                         pRef->y + (x + y * iEdgedWidth) * 32,  
                                                         pParam->edged_width, pMB->sad8 );  
840    
841                          if (Data.chroma) {                          if (Data.chroma) {
842                                  Data.temp[7] = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8,                                  Data.chromaSAD = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8,
843                                                                          pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2)                                                                          pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2)
844                                                                  + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8,                                                                  + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8,
845                                                                          pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);                                                                          pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);
846                                  pMB->sad16 += Data.temp[7];                                  pMB->sad16 += Data.chromaSAD;
847                                    sad00 += Data.chromaSAD;
848                          }                          }
849    
                         sad00 = pMB->sad16;  
   
850                          /* initial skip decision */                          /* initial skip decision */
                         /* no early skip for GMC (global vector = skip vector is unknown!)  */  
851                          if (current->coding_type != S_VOP)      { /* no fast SKIP for S(GMC)-VOPs */                          if (current->coding_type != S_VOP)      { /* no fast SKIP for S(GMC)-VOPs */
852                                  if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)                                  if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)
853                                          if (Data.chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {                                          if (Data.chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant)) {
854                                                  ZeroMacroblockP(pMB, sad00);                                                  ZeroMacroblockP(pMB, sad00);
855                                                  pMB->mode = MODE_NOT_CODED;                                                  pMB->mode = MODE_NOT_CODED;
856                                                  continue;                                                  continue;
# Line 1071  Line 858 
858                          }                          }
859    
860                          if(MotionFlags & XVID_ME_DETECT_STATIC_MOTION) {                          if(MotionFlags & XVID_ME_DETECT_STATIC_MOTION) {
861                                    VECTOR *cmpMV;
862                                    VECTOR staticMV = { 0, 0 };
863    
864                                    if (current->coding_type == S_VOP)
865                                            cmpMV = &pMB->amv;
866                                    else
867                                            cmpMV = &staticMV;
868    
869                                  if(x > 0 && y > 0 && x < pParam->mb_width) {                                  if(x > 0 && y > 0 && x < pParam->mb_width) {
870                                          if(MVequal((&pMBs[(x-1) + y * pParam->mb_width])->mvs[0], zeroMV) &&                                          if(MVequal((&pMBs[(x-1) + y * pParam->mb_width])->mvs[0], *cmpMV) &&
871                                             MVequal((&pMBs[x + (y-1) * pParam->mb_width])->mvs[0], zeroMV) &&                                             MVequal((&pMBs[x + (y-1) * pParam->mb_width])->mvs[0], *cmpMV) &&
872                                         MVequal((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mvs[0], zeroMV) &&                                         MVequal((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mvs[0], *cmpMV) &&
873                                         MVequal(prevMB->mvs[0], zeroMV)) {                                         MVequal(prevMB->mvs[0], *cmpMV)) {
874                                                  stat_thresh = MAX((&pMBs[(x-1) + y * pParam->mb_width])->sad16,                                                  stat_thresh = MAX((&pMBs[(x-1) + y * pParam->mb_width])->sad16,
875                                                                            MAX((&pMBs[x + (y-1) * pParam->mb_width])->sad16,                                                                            MAX((&pMBs[x + (y-1) * pParam->mb_width])->sad16,
876                                                                            MAX((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,                                                                            MAX((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,
877                                                                            prevMB->sad16)));                                                                            prevMB->sad16)));
                                         }  
878                                  } else {                                  } else {
879                                          stat_thresh = MIN((&pMBs[(x-1) + y * pParam->mb_width])->sad16,                                          stat_thresh = MIN((&pMBs[(x-1) + y * pParam->mb_width])->sad16,
880                                                                            MIN((&pMBs[x + (y-1) * pParam->mb_width])->sad16,                                                                            MIN((&pMBs[x + (y-1) * pParam->mb_width])->sad16,
# Line 1088  Line 882 
882                                                                            prevMB->sad16)));                                                                            prevMB->sad16)));
883                                  }                                  }
884                          }                          }
885                            }
886    
887                             /* favorize (0,0) or global vector for cartoons */
888                            if (current->vop_flags & XVID_VOP_CARTOON) {
889                                    if (current->coding_type == S_VOP) {
890                                            int32_t iSAD = sad16(pCurrent->y + (x + y * iEdgedWidth) * 16,
891                                            pGMC->y + 16*y*iEdgedWidth + 16*x, iEdgedWidth, 65536);
892    
893                           /* favorize (0,0) vector for cartoons */                                          if (Data.chroma) {
894                          if ((current->vop_flags & XVID_VOP_CARTOON) &&                                                  iSAD += sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8, pGMC->u + 8*y*(iEdgedWidth/2) + 8*x, iEdgedWidth/2);
895                                  ((sad00 < pMB->quant * 4 * skip_thresh) || (sad00 < stat_thresh))) {                                                  iSAD += sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8, pGMC->v + 8*y*(iEdgedWidth/2) + 8*x, iEdgedWidth/2);
896                                            }
897    
898                                            if (iSAD <= stat_thresh) {              /* mode decision GMC */
899                                                    pMB->mode = MODE_INTER;
900                                                    pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = iSAD;
901                                                    pMB->mcsel = 1;
902                                                    if (Data.qpel) {
903                                                            pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
904                                                            pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2;
905                                                            pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2;
906                                                    } else
907                                                            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
908    
909                                                    continue;
910                                            }
911                                    }
912                                    else if (sad00 < stat_thresh) {
913                                  ZeroMacroblockP(pMB, sad00);                                  ZeroMacroblockP(pMB, sad00);
914                                            pMB->cbp = 0x3f;
915                                  continue;                                  continue;
916                          }                          }
917                            }
918    
919                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
920                                          y, MotionFlags, current->vop_flags,                                          y, MotionFlags, current->vop_flags,
# Line 1112  Line 932 
932                          else                          else
933                                  ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam,                                  ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam,
934                                                                  MotionFlags, current->vop_flags, current->vol_flags,                                                                  MotionFlags, current->vop_flags, current->vol_flags,
935                                                                  pCurrent, pRef, pGMC, current->coding_type);                                                                  pCurrent, pRef, pGMC, current->coding_type, sad00);
936    
937    
938                          if (pMB->mode == MODE_INTRA)                          motionStatsPVOP(&MVmax, &mvCount, &mvSum, pMB, Data.qpel);
                                 if (++iIntra > iLimit) return 1;  
                 }  
939          }          }
         return 0;  
940  }  }
941    
942            current->fcode = getMinFcode(MVmax);
943            current->sStat.iMvSum = mvSum;
944            current->sStat.iMvCount = mvCount;
945    
946            return 0;
947    }

Legend:
Removed from v.1.1.2.1  
changed lines
  Added in v.1.16

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