[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.24, Sat Dec 18 16:02:00 2010 UTC
# Line 4  Line 4 
4   *  - Motion Estimation for P- and S- VOPs  -   *  - Motion Estimation for P- and S- VOPs  -
5   *   *
6   *  Copyright(C) 2002 Christoph Lampert <gruel@web.de>   *  Copyright(C) 2002 Christoph Lampert <gruel@web.de>
7   *               2002 Michael Militzer <michael@xvid.org>   *               2002-2010 Michael Militzer <michael@xvid.org>
8   *               2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net>   *               2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net>
9   *   *
10   *  This program is free software ; you can redistribute it and/or modify   *  This program is free software ; you can redistribute it and/or modify
# Line 39  Line 39 
39  #include "motion.h"  #include "motion.h"
40  #include "sad.h"  #include "sad.h"
41  #include "motion_inlines.h"  #include "motion_inlines.h"
42    #include "motion_smp.h"
43    
44    
45  static const int xvid_me_lambda_vec8[32] =  static const int xvid_me_lambda_vec8[32] =
46          {     0    ,(int)(1.00235 * NEIGH_TEND_8X8 + 0.5),          {     0    ,(int)(1.0 * NEIGH_TEND_8X8 + 0.5),
47          (int)(1.15582 + NEIGH_TEND_8X8 + 0.5), (int)(1.31976*NEIGH_TEND_8X8 + 0.5),          (int)(2.0*NEIGH_TEND_8X8 + 0.5), (int)(3.0*NEIGH_TEND_8X8 + 0.5),
48          (int)(1.49591*NEIGH_TEND_8X8 + 0.5), (int)(1.68601*NEIGH_TEND_8X8 + 0.5),          (int)(4.0*NEIGH_TEND_8X8 + 0.5), (int)(5.0*NEIGH_TEND_8X8 + 0.5),
49          (int)(1.89187*NEIGH_TEND_8X8 + 0.5), (int)(2.11542*NEIGH_TEND_8X8 + 0.5),          (int)(6.0*NEIGH_TEND_8X8 + 0.5), (int)(7.0*NEIGH_TEND_8X8 + 0.5),
50          (int)(2.35878*NEIGH_TEND_8X8 + 0.5), (int)(2.62429*NEIGH_TEND_8X8 + 0.5),          (int)(8.0*NEIGH_TEND_8X8 + 0.5), (int)(9.0*NEIGH_TEND_8X8 + 0.5),
51          (int)(2.91455*NEIGH_TEND_8X8 + 0.5), (int)(3.23253*NEIGH_TEND_8X8 + 0.5),          (int)(10.0*NEIGH_TEND_8X8 + 0.5), (int)(11.0*NEIGH_TEND_8X8 + 0.5),
52          (int)(3.58158*NEIGH_TEND_8X8 + 0.5), (int)(3.96555*NEIGH_TEND_8X8 + 0.5),          (int)(12.0*NEIGH_TEND_8X8 + 0.5), (int)(13.0*NEIGH_TEND_8X8 + 0.5),
53          (int)(4.38887*NEIGH_TEND_8X8 + 0.5), (int)(4.85673*NEIGH_TEND_8X8 + 0.5),          (int)(14.0*NEIGH_TEND_8X8 + 0.5), (int)(15.0*NEIGH_TEND_8X8 + 0.5),
54          (int)(5.37519*NEIGH_TEND_8X8 + 0.5), (int)(5.95144*NEIGH_TEND_8X8 + 0.5),          (int)(16.0*NEIGH_TEND_8X8 + 0.5), (int)(17.0*NEIGH_TEND_8X8 + 0.5),
55          (int)(6.59408*NEIGH_TEND_8X8 + 0.5), (int)(7.31349*NEIGH_TEND_8X8 + 0.5),          (int)(18.0*NEIGH_TEND_8X8 + 0.5), (int)(19.0*NEIGH_TEND_8X8 + 0.5),
56          (int)(8.12242*NEIGH_TEND_8X8 + 0.5), (int)(9.03669*NEIGH_TEND_8X8 + 0.5),          (int)(20.0*NEIGH_TEND_8X8 + 0.5), (int)(21.0*NEIGH_TEND_8X8 + 0.5),
57          (int)(10.0763*NEIGH_TEND_8X8 + 0.5), (int)(11.2669*NEIGH_TEND_8X8 + 0.5),          (int)(22.0*NEIGH_TEND_8X8 + 0.5), (int)(23.0*NEIGH_TEND_8X8 + 0.5),
58          (int)(12.6426*NEIGH_TEND_8X8 + 0.5), (int)(14.2493*NEIGH_TEND_8X8 + 0.5),          (int)(24.0*NEIGH_TEND_8X8 + 0.5), (int)(25.0*NEIGH_TEND_8X8 + 0.5),
59          (int)(16.1512*NEIGH_TEND_8X8 + 0.5), (int)(18.442*NEIGH_TEND_8X8 + 0.5),          (int)(26.0*NEIGH_TEND_8X8 + 0.5), (int)(27.0*NEIGH_TEND_8X8 + 0.5),
60          (int)(21.2656*NEIGH_TEND_8X8 + 0.5), (int)(24.8580*NEIGH_TEND_8X8 + 0.5),          (int)(28.0*NEIGH_TEND_8X8 + 0.5), (int)(29.0*NEIGH_TEND_8X8 + 0.5),
61          (int)(29.6436*NEIGH_TEND_8X8 + 0.5), (int)(36.4949*NEIGH_TEND_8X8 + 0.5)          (int)(30.0*NEIGH_TEND_8X8 + 0.5), (int)(31.0*NEIGH_TEND_8X8 + 0.5)
62  };  };
63    
64  static void  static void
65  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)
66  {  {
         int xc, yc;  
67          const uint8_t * Reference;          const uint8_t * Reference;
68            int32_t sad, xc, yc; uint32_t t;
69          VECTOR * current;          VECTOR * current;
         int32_t sad; uint32_t t;  
70    
71          if ( (x > data->max_dx) || (x < data->min_dx)          if ( (x > data->max_dx) || (x < data->min_dx)
72                  || (y > data->max_dy) || (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
73    
74          if (!data->qpel_precision) {          if (data->qpel_precision) { /* x and y are in 1/4 precision */
75                    Reference = xvid_me_interpolate16x16qpel(x, y, 0, data);
76                    current = data->currentQMV;
77                    xc = x/2; yc = y/2;
78            } else {
79                  Reference = GetReference(x, y, data);                  Reference = GetReference(x, y, data);
80                  current = data->currentMV;                  current = data->currentMV;
81                  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;  
82          }          }
83    
84          sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp);          sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp);
85          t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);          t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision);
86    
87          sad += (data->lambda16 * t * sad)>>10;          sad += (data->lambda16 * t);
88          data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10;          data->temp[0] += (data->lambda8 * t);
89    
90          if (data->chroma && sad < data->iMinSAD[0])          if (data->chroma) {
91                    if (sad >= data->iMinSAD[0]) goto no16;
92                  sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],                  sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],
93                                                                  (yc >> 1) + roundtab_79[yc & 0x3], data);                                                                  (yc >> 1) + roundtab_79[yc & 0x3], data);
94            }
95    
96          if (sad < data->iMinSAD[0]) {          if (sad < data->iMinSAD[0]) {
97                  data->iMinSAD[0] = sad;                  data->iMinSAD[0] = sad;
98                  current[0].x = x; current[0].y = y;                  current[0].x = x; current[0].y = y;
99                  *data->dir = Direction;                  data->dir = Direction;
         }  
   
         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; }  
 }  
   
 static void  
 CheckCandidate16_subpel(const int x, const int y, const 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;  
100          }          }
101    
102          sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp);  no16:
         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);  
   
103          if (data->temp[0] < data->iMinSAD[1]) {          if (data->temp[0] < data->iMinSAD[1]) {
104                  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; }
105          if (data->temp[1] < data->iMinSAD[2]) {          if (data->temp[1] < data->iMinSAD[2]) {
# Line 147  Line 108 
108                  data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; }                  data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; }
109          if (data->temp[3] < data->iMinSAD[4]) {          if (data->temp[3] < data->iMinSAD[4]) {
110                  data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; }                  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;  
         }  
111  }  }
112    
113  static void  static void
114  CheckCandidate8(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)
115  {  {
116          int32_t sad; uint32_t t;          int32_t sad; uint32_t t;
117          const uint8_t * Reference;          const uint8_t * Reference;
# Line 184  Line 129 
129          }          }
130    
131          sad = sad8(data->Cur, Reference, data->iEdgedWidth);          sad = sad8(data->Cur, Reference, data->iEdgedWidth);
132          t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);          t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision);
133    
134          sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10;          sad += (data->lambda8 * t);
135    
136          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
137                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
138                  current->x = x; current->y = y;                  current->x = x; current->y = y;
139                  *data->dir = Direction;                  data->dir = Direction;
         }  
 }  
   
 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;  
140          }          }
   
         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  }  }
142    
143  int  int
144  xvid_me_SkipDecisionP(const IMAGE * current, const IMAGE * reference,  xvid_me_SkipDecisionP(const IMAGE * current, const IMAGE * reference,
145                                                          const int x, const int y,                                                          const int x, const int y,
146                                                          const uint32_t stride, const uint32_t iQuant, int rrv)                                                          const uint32_t stride, const uint32_t iQuant)
147  {  {
148          int offset = (x + y*stride)*8;          int offset = (x + y*stride)*8;
         if(!rrv) {  
149                  uint32_t sadC = sad8(current->u + offset,                  uint32_t sadC = sad8(current->u + offset,
150                                                  reference->u + offset, stride);                                                  reference->u + offset, stride);
151                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
# Line 344  Line 153 
153                                                  reference->v + offset, stride);                                                  reference->v + offset, stride);
154                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
155                  return 1;                  return 1;
   
         } else {  
                 uint32_t sadC = sad16(current->u + 2*offset,  
                                                 reference->u + 2*offset, stride, 256*4096);  
                 if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;  
                 sadC += sad16(current->v + 2*offset,  
                                                 reference->v + 2*offset, stride, 256*4096);  
                 if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;  
                 return 1;  
         }  
156  }  }
157    
158          /*          /*
# Line 369  Line 168 
168           *  [3]: topright neighbour's SAD           *  [3]: topright neighbour's SAD
169           */           */
170    
171  static __inline int  static __inline void
172  get_pmvdata2(const MACROBLOCK * const mbs,  get_pmvdata2(const MACROBLOCK * const mbs,
173                  const int mb_width,                  const int mb_width,
174                  const int bound,                  const int bound,
175                  const int x,                  const int x,
176                  const int y,                  const int y,
                 const int block,  
177                  VECTOR * const pmv,                  VECTOR * const pmv,
178                  int32_t * const psad)                  int32_t * const psad)
179  {  {
# Line 385  Line 183 
183          int lpos, tpos, rpos;          int lpos, tpos, rpos;
184          int num_cand = 0, last_cand = 1;          int num_cand = 0, last_cand = 1;
185    
         switch (block) {  
         case 0:  
186                  lx = x - 1;     ly = y;         lz = 1;                  lx = x - 1;     ly = y;         lz = 1;
187                  tx = x;         ty = y - 1;     tz = 2;                  tx = x;         ty = y - 1;     tz = 2;
188                  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;  
         }  
189    
190          lpos = lx + ly * mb_width;          lpos = lx + ly * mb_width;
191          rpos = rx + ry * mb_width;          rpos = rx + ry * mb_width;
# Line 442  Line 222 
222          }          }
223    
224          /* original pmvdata() compatibility hack */          /* original pmvdata() compatibility hack */
225          if (x == 0 && y == 0 && block == 0) {          if (x == 0 && y == 0) {
226                  pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV;                  pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV;
227                  psad[0] = 0;                  psad[0] = 0;
228                  psad[1] = psad[2] = psad[3] = MV_MAX_ERROR;                  psad[1] = psad[2] = psad[3] = MV_MAX_ERROR;
229                  return 0;                  return;
230          }          }
231    
232          /* 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 */
233          if (num_cand == 1) {          if (num_cand == 1) {
234                  pmv[0] = pmv[last_cand];                  pmv[0] = pmv[last_cand];
235                  psad[0] = psad[last_cand];                  psad[0] = psad[last_cand];
236  #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);  
237          }          }
238    
239          if ((MVequal(pmv[1], pmv[2])) && (MVequal(pmv[1], pmv[3]))) {          if ((MVequal(pmv[1], pmv[2])) && (MVequal(pmv[1], pmv[3]))) {
240                  pmv[0] = pmv[1];                  pmv[0] = pmv[1];
241                  psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);                  psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);
242                  return 1;                  return;
243          }          }
244    
245          /* set median, minimum */          /* set median, minimum */
# Line 478  Line 253 
253    
254          psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);          psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);
255    
         return 0;  
256  }  }
257    
258    
# Line 494  Line 268 
268                                  const IMAGE * const pCurrent,                                  const IMAGE * const pCurrent,
269                                  const IMAGE * const pRef,                                  const IMAGE * const pRef,
270                                  const IMAGE * const vGMC,                                  const IMAGE * const vGMC,
271                                  const int coding_type)                                  const int coding_type,
272                                    const int skip_sad)
273  {  {
274          int mode = MODE_INTER;          int mode = MODE_INTER;
275          int mcsel = 0;          int mcsel = 0;
# Line 520  Line 295 
295          }          }
296    
297          /* final skip decision, a.k.a. "the vector you found, really that good?" */          /* final skip decision, a.k.a. "the vector you found, really that good?" */
298          if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP))          if (skip_possible && (skip_sad < (int)iQuant * MAX_SAD00_FOR_SKIP))
299                  if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH)                  if ( (100*skip_sad)/(pMB->sad16+1) < FINAL_SKIP_THRESH)
300                          if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) {                          if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant)) {
301                                  mode = MODE_NOT_CODED;                                  mode = MODE_NOT_CODED;
302                                  sad = 0;                                  sad = 0;
303                          }                          }
# Line 547  Line 322 
322    
323          /* intra decision */          /* intra decision */
324    
325          if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */          if (iQuant > 10) InterBias += 60 * (iQuant - 10); /* to make high quants work */
326          if (y != 0)          if (y != 0)
327                  if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;                  if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;
328          if (x != 0)          if (x != 0)
329                  if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;                  if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
330    
331          if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */          if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */
         if (Data->rrv) InterBias *= 4;  
332    
333          if (InterBias < sad) {          if (InterBias < sad) {
334                  int32_t deviation;                  int32_t deviation = dev16(Data->Cur, Data->iEdgedWidth);
                 if (!Data->rrv)  
                         deviation = dev16(Data->Cur, Data->iEdgedWidth);  
                 else  
                         deviation = dev16(Data->Cur, Data->iEdgedWidth) + /* dev32() */  
                                                 dev16(Data->Cur+16, Data->iEdgedWidth) +  
                                                 dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) +  
                                                 dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth);  
   
335                  if (deviation < (sad - InterBias)) mode = MODE_INTRA;                  if (deviation < (sad - InterBias)) mode = MODE_INTRA;
336          }          }
337    
338          pMB->cbp = 63;          pMB->cbp = 63;
339          pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;          pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
340    
         if (Data->rrv) {  
                         Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);  
                         Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);  
         }  
   
341          if (mode == MODE_INTER && mcsel == 0) {          if (mode == MODE_INTER && mcsel == 0) {
342                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
343    
# Line 590  Line 351 
351                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
352                  }                  }
353    
354          } else if (mode == MODE_INTER ) { // but mcsel == 1          } else if (mode == MODE_INTER ) { /* but mcsel == 1 */
355    
356                  pMB->mcsel = 1;                  pMB->mcsel = 1;
357                  if (Data->qpel) {                  if (Data->qpel) {
# Line 610  Line 371 
371    
372  static __inline void  static __inline void
373  PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,  PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount,
374                          int iHcount, const MACROBLOCK * const prevMB, int rrv)                          int iHcount, const MACROBLOCK * const prevMB)
375  {  {
         /* this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself */  
         if (rrv) { iWcount /= 2; iHcount /= 2; }  
376    
377          if ( (y != 0) && (x < (iWcount-1)) ) {          /* [5] top-right neighbour */          if ( (y != 0) && (x < (iWcount-1)) ) {          /* [5] top-right neighbour */
378                  pmv[5].x = EVEN(pmv[3].x);                  pmv[5].x = EVEN(pmv[3].x);
# Line 638  Line 397 
397                  pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); /* [6] right-down neighbour in last frame */                  pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); /* [6] right-down neighbour in last frame */
398                  pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);                  pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);
399          } else pmv[6].x = pmv[6].y = 0;          } else pmv[6].x = pmv[6].y = 0;
   
         if (rrv) {  
                 int i;  
                 for (i = 0; i < 7; i++) {  
                         pmv[i].x = RRV_MV_SCALEUP(pmv[i].x);  
                         pmv[i].y = RRV_MV_SCALEUP(pmv[i].y);  
                 }  
         }  
400  }  }
401    
402  static void  static void
403  Search8(const SearchData * const OldData,  Search8(SearchData * const OldData,
404                  const int x, const int y,                  const int x, const int y,
405                  const uint32_t MotionFlags,                  const uint32_t MotionFlags,
406                  const MBParam * const pParam,                  const MBParam * const pParam,
407                  MACROBLOCK * const pMB,                  MACROBLOCK * const pMB,
408                  const MACROBLOCK * const pMBs,                  const MACROBLOCK * const pMBs,
409                  const int block,                  const int block,
410                  SearchData * const Data)                  SearchData * const Data,
411                    const int bound)
412  {  {
413          int i = 0;          int i = 0;
414          CheckFunc * CheckCandidate;          VECTOR vbest_q; int32_t sbest_q;
415          Data->iMinSAD = OldData->iMinSAD + 1 + block;          *Data->iMinSAD = *(OldData->iMinSAD + 1 + block);
416          Data->currentMV = OldData->currentMV + 1 + block;          *Data->currentMV = *(OldData->currentMV + 1 + block);
417          Data->currentQMV = OldData->currentQMV + 1 + block;          *Data->currentQMV = *(OldData->currentQMV + 1 + block);
418    
419          if(Data->qpel) {          if(Data->qpel) {
420                  Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block);                  Data->predMV = get_qpmv2(pMBs, pParam->mb_width, bound, x/2, y/2, block);
421                  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,
422                                                                                  Data->predMV, Data->iFcode, 0, 0);                                                                                  Data->predMV, Data->iFcode, 0);
423          } else {          } else {
424                  Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block);                  Data->predMV = get_pmv2(pMBs, pParam->mb_width, bound, x/2, y/2, block);
425                  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,
426                                                                                  Data->predMV, Data->iFcode, 0, Data->rrv);                                                                                  Data->predMV, Data->iFcode, 0);
427          }          }
428    
429          *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;          *(Data->iMinSAD) += (Data->lambda8 * i);
430    
431          if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {          if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {
432    
433                  if (Data->rrv) i = 16; else i = 8;                  vbest_q = Data->currentQMV[0];
434                    sbest_q = Data->iMinSAD[0];
435    
436    
437                  Data->RefP[0] = OldData->RefP[0] + i * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->RefP[0] = OldData->RefP[0] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
438                  Data->RefP[1] = OldData->RefP[1] + i * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->RefP[1] = OldData->RefP[1] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
439                  Data->RefP[2] = OldData->RefP[2] + i * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->RefP[2] = OldData->RefP[2] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
440                  Data->RefP[3] = OldData->RefP[3] + i * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->RefP[3] = OldData->RefP[3] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
441    
442                  Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->Cur = OldData->Cur + 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
443                  Data->qpel_precision = 0;                  Data->qpel_precision = 0;
444    
445                  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,
446                                          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;  
447    
448                  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 */  
449    
450                          MainSearchFunc *MainSearchPtr;                          MainSearchFunc *MainSearchPtr;
451                          if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = xvid_me_SquareSearch;                          if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = xvid_me_SquareSearch;
452                                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = xvid_me_AdvDiamondSearch;                                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = xvid_me_AdvDiamondSearch;
453                                          else MainSearchPtr = xvid_me_DiamondSearch;                                          else MainSearchPtr = xvid_me_DiamondSearch;
454    
455                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidate);                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidate8);
456                    }
457    
458                          if(*(Data->iMinSAD) < temp_sad) {                  if(!Data->qpel) {
459                                          Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */                          /* halfpel mode */
460                            if (MotionFlags & XVID_ME_HALFPELREFINE8)
461                                    /* perform halfpel refine of current best vector */
462                                    xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate8, 0);
463                    } else {
464                            /* qpel mode */
465                            Data->currentQMV->x = 2*Data->currentMV->x;
466                                          Data->currentQMV->y = 2 * Data->currentMV->y;                                          Data->currentQMV->y = 2 * Data->currentMV->y;
                         }  
                 }  
467    
468                            if(MotionFlags & XVID_ME_FASTREFINE8) {
469                                    /* fast */
470                                    get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3,
471                                            pParam->width, pParam->height, Data->iFcode, 2);
472                                    FullRefine_Fast(Data, CheckCandidate8, 0);
473                            } else if(MotionFlags & XVID_ME_QUARTERPELREFINE8) {
474                                    /* full */
475                  if (MotionFlags & XVID_ME_HALFPELREFINE8) {                  if (MotionFlags & XVID_ME_HALFPELREFINE8) {
476                          int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */                                          xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate8, 0); /* hpel part */
477                                            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 */  
478                                  Data->currentQMV->y = 2 * Data->currentMV->y;                                  Data->currentQMV->y = 2 * Data->currentMV->y;
479                          }                          }
                 }  
480    
                 if (Data->qpel && MotionFlags & XVID_ME_QUARTERPELREFINE8) {  
                                 Data->qpel_precision = 1;  
481                                  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,
482                                          pParam->width, pParam->height, Data->iFcode, 2, 0);                                          pParam->width, pParam->height, Data->iFcode, 2);
483                                  xvid_me_SubpelRefine(Data, CheckCandidate);                                  Data->qpel_precision = 1;
484    
485                                    xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate8, 0); /* qpel part */
486                  }                  }
487          }          }
488    
489          if (Data->rrv) {                  if (sbest_q <= Data->iMinSAD[0]) /* we have not found a better match */
490                          Data->currentMV->x = RRV_MV_SCALEDOWN(Data->currentMV->x);                          Data->currentQMV[0] = vbest_q;
491                          Data->currentMV->y = RRV_MV_SCALEDOWN(Data->currentMV->y);  
492          }          }
493    
494          if(Data->qpel) {          if(Data->qpel) {
# Line 743  Line 500 
500                  pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;                  pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;
501          }          }
502    
503            *(OldData->iMinSAD + 1 + block) = *Data->iMinSAD;
504            *(OldData->currentMV + 1 + block) = *Data->currentMV;
505            *(OldData->currentQMV + 1 + block) = *Data->currentQMV;
506    
507          pMB->mvs[block] = *Data->currentMV;          pMB->mvs[block] = *Data->currentMV;
508          pMB->sad8[block] = 4 * *Data->iMinSAD;          pMB->sad8[block] = 4 * *Data->iMinSAD;
509  }  }
# Line 763  Line 524 
524                  const MBParam * const pParam,                  const MBParam * const pParam,
525                  const MACROBLOCK * const pMBs,                  const MACROBLOCK * const pMBs,
526                  const MACROBLOCK * const prevMBs,                  const MACROBLOCK * const prevMBs,
527                  MACROBLOCK * const pMB)                  MACROBLOCK * const pMB,
528                    const int bound)
529  {  {
530    
531          int i, threshA;          int i, threshA;
# Line 772  Line 534 
534          CheckFunc * CheckCandidate;          CheckFunc * CheckCandidate;
535    
536          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,
537                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 1);
538    
539          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);          get_pmvdata2(pMBs, pParam->mb_width, bound, x, y, pmv, Data->temp);
540    
541          Data->temp[5] = Data->temp[6] = 0; /* chroma-sad cache */          Data->chromaX = Data->chromaY = 0; /* chroma-sad cache */
542          i = Data->rrv ? 2 : 1;          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;
543          Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i;          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;
544          Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i;          Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;
545          Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i;  
546            Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16;
547          Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16;
548          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16;
549          Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16;
550          Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16*i;          Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;
551          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;  
552    
553          Data->lambda16 = xvid_me_lambda_vec16[pMB->quant];          Data->lambda16 = xvid_me_lambda_vec16[pMB->quant];
554          Data->lambda8 = xvid_me_lambda_vec8[pMB->quant];          Data->lambda8 = xvid_me_lambda_vec8[pMB->quant];
555          Data->qpel_precision = 0;          Data->qpel_precision = 0;
556          *Data->dir = 0;          Data->dir = 0;
557    
558          memset(Data->currentMV, 0, 5*sizeof(VECTOR));          memset(Data->currentMV, 0, 5*sizeof(VECTOR));
559    
560          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, bound, x, y, 0);
561          else Data->predMV = pmv[0];          else Data->predMV = pmv[0];
562    
563          i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0, 0);          i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0);
564          Data->iMinSAD[0] = pMB->sad16 + ((Data->lambda16 * i * pMB->sad16)>>10);          Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i);
565          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);
566          Data->iMinSAD[2] = pMB->sad8[1];          Data->iMinSAD[2] = pMB->sad8[1];
567          Data->iMinSAD[3] = pMB->sad8[2];          Data->iMinSAD[3] = pMB->sad8[2];
568          Data->iMinSAD[4] = pMB->sad8[3];          Data->iMinSAD[4] = pMB->sad8[3];
# Line 814  Line 575 
575                  threshA = 512;                  threshA = 512;
576    
577          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
578                                          prevMBs + x + y * pParam->mb_width, Data->rrv);                                          prevMBs + x + y * pParam->mb_width);
579    
         if (!Data->rrv) {  
580                  if (inter4v) CheckCandidate = CheckCandidate16;                  if (inter4v) CheckCandidate = CheckCandidate16;
581                          else CheckCandidate = CheckCandidate16no4v; /* for extra speed */                          else CheckCandidate = CheckCandidate16no4v; /* for extra speed */
         } else CheckCandidate = CheckCandidate32;  
582    
583  /* 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())*/
584    
# Line 836  Line 595 
595          else {          else {
596    
597                  MainSearchFunc * MainSearchPtr;                  MainSearchFunc * MainSearchPtr;
598                  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 */
599    
600                  if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;                  if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;
601                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;
# Line 851  Line 610 
610                  if (MotionFlags & XVID_ME_EXTSEARCH16) {                  if (MotionFlags & XVID_ME_EXTSEARCH16) {
611                          int32_t bSAD;                          int32_t bSAD;
612                          VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];                          VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];
613                          if (Data->rrv) {                          if (Data->qpel) {
614                                  startMV.x = RRV_MV_SCALEUP(startMV.x);                                  startMV.x /= 2;
615                                  startMV.y = RRV_MV_SCALEUP(startMV.y);                                  startMV.y /= 2;
616                          }                          }
617                          if (!(MVequal(startMV, backupMV))) {                          if (!(MVequal(startMV, backupMV))) {
618                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
619    
620                                  CheckCandidate(startMV.x, startMV.y, Data, 255);                                  CheckCandidate(startMV.x, startMV.y, Data, 255);
621                                  MainSearchPtr(startMV.x, startMV.y, Data, 255, CheckCandidate);                                  xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate);
622                                  if (bSAD < Data->iMinSAD[0]) {                                  if (bSAD < Data->iMinSAD[0]) {
623                                          Data->currentMV[0] = backupMV;                                          Data->currentMV[0] = backupMV;
624                                          Data->iMinSAD[0] = bSAD; }                                          Data->iMinSAD[0] = bSAD;
625                                    }
626                          }                          }
627    
628                          backupMV = Data->currentMV[0];                          backupMV = Data->currentMV[0];
# Line 871  Line 631 
631                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
632    
633                                  CheckCandidate(startMV.x, startMV.y, Data, 255);                                  CheckCandidate(startMV.x, startMV.y, Data, 255);
634                                  MainSearchPtr(startMV.x, startMV.y, Data, 255, CheckCandidate);                                  xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate);
635                                  if (bSAD < Data->iMinSAD[0]) {                                  if (bSAD < Data->iMinSAD[0]) {
636                                          Data->currentMV[0] = backupMV;                                          Data->currentMV[0] = backupMV;
637                                          Data->iMinSAD[0] = bSAD;                                          Data->iMinSAD[0] = bSAD;
# Line 880  Line 640 
640                  }                  }
641          }          }
642    
643    
644            if(!Data->qpel) {
645                    /* halfpel mode */
646          if (MotionFlags & XVID_ME_HALFPELREFINE16)          if (MotionFlags & XVID_ME_HALFPELREFINE16)
647                          xvid_me_SubpelRefine(Data, CheckCandidate);                                  xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0);
648            } else {
649                    /* qpel mode */
650    
651          for(i = 0; i < 5; i++) {          for(i = 0; i < 5; i++) {
652                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */
653                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
654          }          }
655                    if(MotionFlags & XVID_ME_FASTREFINE16 && MotionFlags & XVID_ME_QUARTERPELREFINE16) {
656          if (Data->qpel) {                          /* fast */
657                  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,
658                                  pParam->width, pParam->height, Data->iFcode, 2, 0);                                                  pParam->width, pParam->height, Data->iFcode, 2);
659                            FullRefine_Fast(Data, CheckCandidate, 0);
660                    } else {
661                            if(MotionFlags & (XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE16_RD)) {
662                                    /* full */
663                                    if (MotionFlags & XVID_ME_HALFPELREFINE16) {
664                                            xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0); /* hpel part */
665                                            for(i = 0; i < 5; i++) {
666                                                    Data->currentQMV[i].x = 2 * Data->currentMV[i].x;
667                                                    Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
668                                            }
669                                    }
670                                    get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
671                                                            pParam->width, pParam->height, Data->iFcode, 2);
672                  Data->qpel_precision = 1;                  Data->qpel_precision = 1;
673                  if (MotionFlags & XVID_ME_QUARTERPELREFINE16) {                                  if(MotionFlags & XVID_ME_QUARTERPELREFINE16)
674                          if(MotionFlags & XVID_ME_FASTREFINE16)                                          xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate, 0); /* qpel part */
675                                  SubpelRefine_Fast(Data, CheckCandidate);                          }
                         else  
                                 xvid_me_SubpelRefine(Data, CheckCandidate);  
676                  }                  }
677          }          }
678    
679          if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30)          if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30 * ((MotionFlags & XVID_ME_FASTREFINE16) ? 8 : 1))
680                  inter4v = 0;                  inter4v = 0;
681    
682          if (inter4v) {          if (inter4v) {
683                  SearchData Data8;                  SearchData Data8;
684                  memcpy(&Data8, Data, sizeof(SearchData)); /* quick copy of common data */                  memcpy(&Data8, Data, sizeof(SearchData)); /* quick copy of common data */
685    
686                  Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);                  Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8, bound);
687                  Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);                  Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8, bound);
688                  Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);                  Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8, bound);
689                  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, bound);
690    
691                  if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_RD))) {                  if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_RD))) {
692                          /* 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 */
693                          int sumx = 0, sumy = 0;                          int sumx = 0, sumy = 0;
694    
695                          if (Data->qpel)                          if (Data->qpel)
# Line 933  Line 709 
709          } else Data->iMinSAD[1] = 4096*256;          } else Data->iMinSAD[1] = 4096*256;
710  }  }
711    
712    static int
713    InitialSkipDecisionP(int sad00,
714                                             const MBParam * pParam,
715                                             const FRAMEINFO * current,
716                                             MACROBLOCK * pMB,
717                                             const MACROBLOCK * prevMB,
718                                             int x, int y,
719                                             const SearchData * Data,
720                                             const IMAGE * const pGMC,
721                                             const IMAGE * const pCurrent,
722                                             const IMAGE * const pRef,
723                                             const uint32_t MotionFlags,
724                                             const int bound)
725    {
726            const unsigned int iEdgedWidth = pParam->edged_width;
727    
728            int skip_thresh = INITIAL_SKIP_THRESH * \
729                    (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);
730            int stat_thresh = 0;
731    
732            /* initial skip decision */
733            if (current->coding_type != S_VOP)      { /* no fast SKIP for S(GMC)-VOPs */
734                    if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)
735                            if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant)) {
736                                    ZeroMacroblockP(pMB, sad00);
737                                    pMB->mode = MODE_NOT_CODED;
738                                    return 1;
739                            }
740            }
741    
742            if(MotionFlags & XVID_ME_DETECT_STATIC_MOTION) {
743                    VECTOR *cmpMV;
744                    VECTOR staticMV = { 0, 0 };
745                    const MACROBLOCK * pMBs = current->mbs;
746    
747                    if (current->coding_type == S_VOP)
748                            cmpMV = &pMB->amv;
749                    else
750                            cmpMV = &staticMV;
751    
752                    if(x > 0 && y > 0 && x < (int) pParam->mb_width) {
753                            if(MVequal((&pMBs[(x-1) + y * pParam->mb_width])->mvs[0], *cmpMV) &&
754                               MVequal((&pMBs[x + (y-1) * pParam->mb_width])->mvs[0], *cmpMV) &&
755                               MVequal((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mvs[0], *cmpMV) &&
756                               MVequal(prevMB->mvs[0], *cmpMV)) {
757                                    stat_thresh = MAX((&pMBs[(x-1) + y * pParam->mb_width])->sad16,
758                                                              MAX((&pMBs[x + (y-1) * pParam->mb_width])->sad16,
759                                                              MAX((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,
760                                                              prevMB->sad16)));
761                            } else {
762                                    stat_thresh = MIN((&pMBs[(x-1) + y * pParam->mb_width])->sad16,
763                                                              MIN((&pMBs[x + (y-1) * pParam->mb_width])->sad16,
764                                                              MIN((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,
765                                                              prevMB->sad16)));
766                            }
767                    }
768            }
769    
770             /* favorize (0,0) or global vector for cartoons */
771            if (current->vop_flags & XVID_VOP_CARTOON) {
772                    if (current->coding_type == S_VOP) {
773                            int32_t iSAD = sad16(pCurrent->y + (x + y * iEdgedWidth) * 16,
774                            pGMC->y + 16*y*iEdgedWidth + 16*x, iEdgedWidth, 65536);
775    
776                            if (Data->chroma) {
777                                    iSAD += sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8, pGMC->u + 8*y*(iEdgedWidth/2) + 8*x, iEdgedWidth/2);
778                                    iSAD += sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8, pGMC->v + 8*y*(iEdgedWidth/2) + 8*x, iEdgedWidth/2);
779                            }
780    
781                            if (iSAD <= stat_thresh) {              /* mode decision GMC */
782                                    pMB->mode = MODE_INTER;
783                                    pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = iSAD;
784                                    pMB->mcsel = 1;
785                                    if (Data->qpel) {
786                                            pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
787                                            pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2;
788                                            pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2;
789                                    } else
790                                            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
791    
792                                    return 1;
793                            }
794                    }
795                    else if (sad00 < stat_thresh) {
796                            VECTOR predMV;
797                            if (Data->qpel)
798                                    predMV = get_qpmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
799                            else
800                                    predMV = get_pmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
801    
802                            ZeroMacroblockP(pMB, sad00);
803                            pMB->cbp = 0x3f;
804                            pMB->pmvs[0].x = - predMV.x;
805                            pMB->pmvs[0].y = - predMV.y;
806                            return 1;
807                    }
808            }
809    
810            return 0;
811    }
812    
813  static __inline uint32_t  static __inline uint32_t
814  MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags)  MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags)
815  {  {
# Line 958  Line 835 
835          if (Flags & XVID_ME_QUARTERPELREFINE8_RD)          if (Flags & XVID_ME_QUARTERPELREFINE8_RD)
836                  Flags &= ~XVID_ME_QUARTERPELREFINE8;                  Flags &= ~XVID_ME_QUARTERPELREFINE8;
837    
838            if (Flags & XVID_ME_QUARTERPELREFINE16_RD)
839                    Flags &= ~XVID_ME_QUARTERPELREFINE16;
840    
841          if (!(VolFlags & XVID_VOL_QUARTERPEL))          if (!(VolFlags & XVID_VOL_QUARTERPEL))
842                  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);
843    
844          if (!(VopFlags & XVID_VOP_HALFPEL))          if (!(VopFlags & XVID_VOP_HALFPEL))
845                  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);
846    
847          if ((VopFlags & XVID_VOP_GREYSCALE) || (VopFlags & XVID_VOP_REDUCED))          if (VopFlags & XVID_VOP_GREYSCALE)
848                  Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP);                  Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP);
849    
850            if (Flags & XVID_ME_FASTREFINE8)
851                    Flags &= ~XVID_ME_HALFPELREFINE8_RD;
852    
853            if (Flags & XVID_ME_FASTREFINE16)
854                    Flags &= ~XVID_ME_HALFPELREFINE16_RD;
855    
856          return Flags;          return Flags;
857  }  }
858    
859  bool  static __inline void
860    motionStatsPVOP(int * const MVmax, int * const mvCount, int * const mvSum,
861                                    const MACROBLOCK * const pMB, const int qpel)
862    {
863            const VECTOR * const mv = qpel ? pMB->qmvs : pMB->mvs;
864            int i;
865            int max = *MVmax;
866    
867            switch (pMB->mode) {
868            case MODE_INTER4V:
869                    *mvCount += 3;
870                    for(i = 3; i; i--) {
871                            if (mv[i].x > max) max = mv[i].x;
872                            else if (-mv[i].x - 1 > max) max = -mv[i].x - 1;
873                            *mvSum += mv[i].x * mv[i].x;
874                            if (mv[i].y > max) max = mv[i].y;
875                            else if (-mv[i].y - 1 > max) max = -mv[i].y - 1;
876                            *mvSum += mv[i].y * mv[i].y;
877                    }
878            case MODE_INTER:
879                    (*mvCount)++;
880                    *mvSum += mv[0].x * mv[0].x;
881                    *mvSum += mv[0].y * mv[0].y;
882                    if (mv[0].x > max) max = mv[0].x;
883                    else if (-mv[0].x - 1 > max) max = -mv[0].x - 1;
884                    if (mv[0].y > max) max = mv[0].y;
885                    else if (-mv[0].y - 1 > max) max = -mv[0].y - 1;
886                    *MVmax = max;
887            default:
888                    break;
889            }
890    }
891    
892    void
893  MotionEstimation(MBParam * const pParam,  MotionEstimation(MBParam * const pParam,
894                                   FRAMEINFO * const current,                                   FRAMEINFO * const current,
895                                   FRAMEINFO * const reference,                                   FRAMEINFO * const reference,
# Line 978  Line 897 
897                                   const IMAGE * const pRefV,                                   const IMAGE * const pRefV,
898                                   const IMAGE * const pRefHV,                                   const IMAGE * const pRefHV,
899                                  const IMAGE * const pGMC,                                  const IMAGE * const pGMC,
900                                   const uint32_t iLimit)                                   const uint32_t iLimit,
901                                     const int num_slices)
902  {  {
903          MACROBLOCK *const pMBs = current->mbs;          MACROBLOCK *const pMBs = current->mbs;
904          const IMAGE *const pCurrent = &current->image;          const IMAGE *const pCurrent = &current->image;
905          const IMAGE *const pRef = &reference->image;          const IMAGE *const pRef = &reference->image;
906    
907          uint32_t mb_width = pParam->mb_width;          const uint32_t mb_width = pParam->mb_width;
908          uint32_t mb_height = pParam->mb_height;          const uint32_t mb_height = pParam->mb_height;
909          const uint32_t iEdgedWidth = pParam->edged_width;          const uint32_t iEdgedWidth = pParam->edged_width;
910          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);
911          int stat_thresh = 0;          int stat_thresh = 0;
912            int bound = 0;
913            int MVmax = 0, mvSum = 0, mvCount = 0;
914    
915          uint32_t x, y;          uint32_t x, y;
916          uint32_t iIntra = 0;          int sad00;
         int32_t sad00;  
917          int skip_thresh = INITIAL_SKIP_THRESH * \          int skip_thresh = INITIAL_SKIP_THRESH * \
                 (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \  
918                  (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);                  (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);
919            int block = 0;
920    
921          /* 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];  
922          DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);
923          SearchData Data;          SearchData Data;
924          memset(&Data, 0, sizeof(SearchData));          memset(&Data, 0, sizeof(SearchData));
925          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;  
926          Data.iFcode = current->fcode;          Data.iFcode = current->fcode;
927          Data.rounding = pParam->m_rounding_type;          Data.rounding = pParam->m_rounding_type;
928          Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);          Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);
929          Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP;          Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP;
         Data.rrv = (current->vop_flags & XVID_VOP_REDUCED) ? 1:0;  
930          Data.dctSpace = dct_space;          Data.dctSpace = dct_space;
931          Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);          Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
932            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;  
         }  
933    
934          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) */
935          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();
936    
937          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
938    
939                    bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
940    
941                  for (x = 0; x < mb_width; x++)  {                  for (x = 0; x < mb_width; x++)  {
942                          MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];                          MACROBLOCK *pMB = &pMBs[block];
943                          MACROBLOCK *prevMB = &reference->mbs[x + y * pParam->mb_width];                          MACROBLOCK *prevMB = &reference->mbs[block];
944                            int skip;
945                            block++;
946    
947                          if (!Data.rrv) pMB->sad16 =                          pMB->sad16 =
948                                  sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16,                                  sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16,
949                                                          pRef->y + (x + y * iEdgedWidth) * 16,                                                          pRef->y + (x + y * iEdgedWidth) * 16,
950                                                          pParam->edged_width, pMB->sad8 );                                                          pParam->edged_width, pMB->sad8 );
951    
952                          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 );  
953    
954                          if (Data.chroma) {                          if (Data.chroma) {
955                                  Data.temp[7] = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8,                                  Data.chromaSAD = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8,
956                                                                          pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2)                                                                          pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2)
957                                                                  + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8,                                                                  + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8,
958                                                                          pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);                                                                          pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);
959                                  pMB->sad16 += Data.temp[7];                                  pMB->sad16 += Data.chromaSAD;
960                                    sad00 += Data.chromaSAD;
961                          }                          }
962    
963                          sad00 = pMB->sad16;                          skip = InitialSkipDecisionP(sad00, pParam, current, pMB, prevMB, x, y, &Data, pGMC,
964                                                                                    pCurrent, pRef, MotionFlags, bound);
965                            if (skip) continue;
966    
967                          /* initial skip decision */                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
968                          /* no early skip for GMC (global vector = skip vector is unknown!)  */                                          y, MotionFlags, current->vop_flags,
969                          if (current->coding_type != S_VOP)      { /* no fast SKIP for S(GMC)-VOPs */                                          &Data, pParam, pMBs, reference->mbs, pMB, bound);
970                                  if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)  
971                                          if (Data.chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {                          if (current->vop_flags & XVID_VOP_MODEDECISION_RD)
972                                                  ZeroMacroblockP(pMB, sad00);                                  xvid_me_ModeDecision_RD(&Data, pMB, pMBs, x, y, pParam,
973                                                  pMB->mode = MODE_NOT_CODED;                                                                  MotionFlags, current->vop_flags, current->vol_flags,
974                                                  continue;                                                                  pCurrent, pRef, pGMC, current->coding_type, bound);
975    
976                            else if (current->vop_flags & XVID_VOP_FAST_MODEDECISION_RD)
977                                    xvid_me_ModeDecision_Fast(&Data, pMB, pMBs, x, y, pParam,
978                                                                    MotionFlags, current->vop_flags, current->vol_flags,
979                                                                    pCurrent, pRef, pGMC, current->coding_type, bound);
980                            else
981                                    ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam,
982                                                                    MotionFlags, current->vop_flags, current->vol_flags,
983                                                                    pCurrent, pRef, pGMC, current->coding_type, sad00);
984    
985    
986                            motionStatsPVOP(&MVmax, &mvCount, &mvSum, pMB, Data.qpel);
987                                          }                                          }
988                          }                          }
989    
990                          if(MotionFlags & XVID_ME_DETECT_STATIC_MOTION) {          current->fcode = getMinFcode(MVmax);
991                                  if(x > 0 && y > 0 && x < pParam->mb_width) {          current->sStat.iMvSum = mvSum;
992                                          if(MVequal((&pMBs[(x-1) + y * pParam->mb_width])->mvs[0], zeroMV) &&          current->sStat.iMvCount = mvCount;
                                            MVequal((&pMBs[x + (y-1) * pParam->mb_width])->mvs[0], zeroMV) &&  
                                        MVequal((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mvs[0], zeroMV) &&  
                                        MVequal(prevMB->mvs[0], zeroMV)) {  
                                                 stat_thresh = MAX((&pMBs[(x-1) + y * pParam->mb_width])->sad16,  
                                                                           MAX((&pMBs[x + (y-1) * pParam->mb_width])->sad16,  
                                                                           MAX((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,  
                                                                           prevMB->sad16)));  
993                                          }                                          }
994                                  } else {  
995                                          stat_thresh = MIN((&pMBs[(x-1) + y * pParam->mb_width])->sad16,  void
996                                                                            MIN((&pMBs[x + (y-1) * pParam->mb_width])->sad16,  MotionEstimateSMP(SMPData * h)
997                                                                            MIN((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16,  {
998                                                                            prevMB->sad16)));          Encoder *pEnc = (Encoder *) h->pEnc;
999    
1000            const MBParam * const pParam = &pEnc->mbParam;
1001            const FRAMEINFO * const current = pEnc->current;
1002            const FRAMEINFO * const reference = pEnc->reference;
1003            const IMAGE * const pRefH = &pEnc->vInterH;
1004            const IMAGE * const pRefV = &pEnc->vInterV;
1005            const IMAGE * const pRefHV = &pEnc->vInterHV;
1006            const IMAGE * const pGMC = &pEnc->vGMC;
1007            uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags,
1008                                                                                                    current->vop_flags,
1009                                                                                                    current->vol_flags);
1010    
1011            MACROBLOCK *const pMBs = current->mbs;
1012            const IMAGE *const pCurrent = &current->image;
1013            const IMAGE *const pRef = &reference->image;
1014    
1015            const int mb_width = pParam->mb_width;
1016            const int mb_height = pParam->mb_height;
1017            const uint32_t iEdgedWidth = pParam->edged_width;
1018            int stat_thresh = 0;
1019            int bound = 0;
1020            int num_slices = pEnc->num_slices;
1021            int y_step = h->y_step;
1022            int y_row = h->y_row;
1023            int start_y = h->start_y;
1024            int stop_y = h->stop_y;
1025            int MVmax = 0, mvSum = 0, mvCount = 0;
1026    
1027            int x, y;
1028            int sad00;
1029            int skip_thresh = INITIAL_SKIP_THRESH * \
1030                    (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);
1031            int block = (start_y+y_row)*mb_width;
1032            int * complete_count_self = h->complete_count_self;
1033            const volatile int * complete_count_above = h->complete_count_above;
1034            int max_mbs;
1035            int current_mb = 0;
1036    
1037            /* some pre-initialized thingies for SearchP */
1038            DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);
1039            SearchData Data;
1040            memset(&Data, 0, sizeof(SearchData));
1041            Data.iEdgedWidth = iEdgedWidth;
1042            Data.iFcode = current->fcode;
1043            Data.rounding = pParam->m_rounding_type;
1044            Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);
1045            Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP;
1046            Data.dctSpace = dct_space;
1047            Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
1048            Data.mpeg_quant_matrices = pParam->mpeg_quant_matrices;
1049    
1050            /* todo: sort out temp memory space */
1051            Data.RefQ = h->RefQ;
1052            if (sadInit) (*sadInit) ();
1053    
1054            max_mbs = 0;
1055    
1056            for (y = (start_y + y_row); y < stop_y; y += y_step)    {
1057                    bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1058    
1059                    if (y == start_y) max_mbs = mb_width; /* we can process all blocks of the first row */
1060    
1061                    for (x = 0; x < mb_width; x++)  {
1062    
1063                            MACROBLOCK *pMB, *prevMB;
1064                            int skip;
1065    
1066                            if (current_mb >= max_mbs) {
1067                                    /* we ME-ed all macroblocks we safely could. grab next portion */
1068                                    int above_count = *complete_count_above; /* sync point */
1069                                    if (above_count == mb_width) {
1070                                            /* full line above is ready */
1071                                            above_count = mb_width+1;
1072                                            if (y < (stop_y-y_step)) {
1073                                                    /* this is not last line, grab a portion of MBs from the next line too */
1074                                                    above_count += MAX(0, complete_count_above[1] - 1);
1075                                  }                                  }
1076                          }                          }
1077    
1078                           /* favorize (0,0) vector for cartoons */                                  max_mbs = current_mb + above_count - x - 1;
1079                          if ((current->vop_flags & XVID_VOP_CARTOON) &&  
1080                                  ((sad00 < pMB->quant * 4 * skip_thresh) || (sad00 < stat_thresh))) {                                  if (current_mb >= max_mbs) {
1081                                  ZeroMacroblockP(pMB, sad00);                                          /* current workload is zero */
1082                                            x--;
1083                                            sched_yield();
1084                                            continue;
1085                                    }
1086                            }
1087    
1088                            pMB = &pMBs[block];
1089                            prevMB = &reference->mbs[block];
1090    
1091                            pMB->sad16 =
1092                                    sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16,
1093                                                            pRef->y + (x + y * iEdgedWidth) * 16,
1094                                                            pParam->edged_width, pMB->sad8);
1095    
1096                            sad00 = 4*MAX(MAX(pMB->sad8[0], pMB->sad8[1]), MAX(pMB->sad8[2], pMB->sad8[3]));
1097    
1098                            if (Data.chroma) {
1099                                    Data.chromaSAD = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8,
1100                                                                            pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2)
1101                                                                    + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8,
1102                                                                            pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);
1103                                    pMB->sad16 += Data.chromaSAD;
1104                                    sad00 += Data.chromaSAD;
1105                            }
1106    
1107                            skip = InitialSkipDecisionP(sad00, pParam, current, pMB, prevMB, x, y, &Data, pGMC,
1108                                                                                    pCurrent, pRef, MotionFlags, bound);
1109    
1110                            if (skip) {
1111                                    current_mb++;
1112                                    block++;
1113                                    *complete_count_self = x+1;
1114                                  continue;                                  continue;
1115                          }                          }
1116    
1117                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
1118                                          y, MotionFlags, current->vop_flags,                                          y, MotionFlags, current->vop_flags,
1119                                          &Data, pParam, pMBs, reference->mbs, pMB);                                          &Data, pParam, pMBs, reference->mbs, pMB, bound);
1120    
1121                          if (current->vop_flags & XVID_VOP_MODEDECISION_RD)                          if (current->vop_flags & XVID_VOP_MODEDECISION_RD)
1122                                  xvid_me_ModeDecision_RD(&Data, pMB, pMBs, x, y, pParam,                                  xvid_me_ModeDecision_RD(&Data, pMB, pMBs, x, y, pParam,
1123                                                                  MotionFlags, current->vop_flags, current->vol_flags,                                                                  MotionFlags, current->vop_flags, current->vol_flags,
1124                                                                  pCurrent, pRef, pGMC, current->coding_type);                                                                  pCurrent, pRef, pGMC, current->coding_type, bound);
1125    
1126                          else if (current->vop_flags & XVID_VOP_FAST_MODEDECISION_RD)                          else if (current->vop_flags & XVID_VOP_FAST_MODEDECISION_RD)
1127                                  xvid_me_ModeDecision_Fast(&Data, pMB, pMBs, x, y, pParam,                                  xvid_me_ModeDecision_Fast(&Data, pMB, pMBs, x, y, pParam,
1128                                                                  MotionFlags, current->vop_flags, current->vol_flags,                                                                  MotionFlags, current->vop_flags, current->vol_flags,
1129                                                                  pCurrent, pRef, pGMC, current->coding_type);                                                                  pCurrent, pRef, pGMC, current->coding_type, bound);
1130                          else                          else
1131                                  ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam,                                  ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam,
1132                                                                  MotionFlags, current->vop_flags, current->vol_flags,                                                                  MotionFlags, current->vop_flags, current->vol_flags,
1133                                                                  pCurrent, pRef, pGMC, current->coding_type);                                                                  pCurrent, pRef, pGMC, current->coding_type, sad00);
1134    
1135                            *complete_count_self = x+1;
1136    
1137                            current_mb++;
1138                            block++;
1139    
1140                            motionStatsPVOP(&MVmax, &mvCount, &mvSum, pMB, Data.qpel);
1141    
                         if (pMB->mode == MODE_INTRA)  
                                 if (++iIntra > iLimit) return 1;  
1142                  }                  }
1143    
1144                    block += (y_step-1)*pParam->mb_width;
1145    
1146                    complete_count_self++;
1147                    complete_count_above++;
1148          }          }
1149          return 0;  
1150            h->minfcode = getMinFcode(MVmax);
1151    
1152            h->MVmax = MVmax;
1153            h->mvSum = mvSum;
1154            h->mvCount = mvCount;
1155  }  }
1156    
1157    

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

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