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

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

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

revision 1.44.2.26, Sat Nov 23 15:09:31 2002 UTC revision 1.53, Sun Nov 17 00:32:06 2002 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *      XVID MPEG-4 VIDEO CODEC   *      XVID MPEG-4 VIDEO CODEC
4   *      motion estimation   *  - Motion Estimation module -
5   *   *
6   *      This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002 Christoph Lampert <gruel@web.de>
7   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *               2002 Michael Militzer <michael@xvid.org>
  *      to use this software module in hardware or software products are  
  *      advised that its use may infringe existing patents or copyrights, and  
  *      any such use would be at such party's own risk.  The original  
  *      developer of this software module and his/her company, and subsequent  
  *      editors and their companies, will have no liability for use of this  
  *      software or modifications or derivatives thereof.  
8   *   *
9   *      This program is free software; you can redistribute it and/or modify   *  This file is part of XviD, a free MPEG-4 video encoder/decoder
10   *      it under the terms of the GNU General Public License as published by   *
11     *  XviD is free software; you can redistribute it and/or modify it
12     *  under the terms of the GNU General Public License as published by
13   *      the Free Software Foundation; either version 2 of the License, or   *      the Free Software Foundation; either version 2 of the License, or
14   *      (at your option) any later version.   *      (at your option) any later version.
15   *   *
# Line 24  Line 20 
20   *   *
21   *      You should have received a copy of the GNU General Public License   *      You should have received a copy of the GNU General Public License
22   *      along with this program; if not, write to the Free Software   *      along with this program; if not, write to the Free Software
23   *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
24     *
25     *  Under section 8 of the GNU General Public License, the copyright
26     *  holders of XVID explicitly forbid distribution in the following
27     *  countries:
28     *
29     *    - Japan
30     *    - United States of America
31     *
32     *  Linking XviD statically or dynamically with other modules is making a
33     *  combined work based on XviD.  Thus, the terms and conditions of the
34     *  GNU General Public License cover the whole combination.
35     *
36     *  As a special exception, the copyright holders of XviD give you
37     *  permission to link XviD with independent modules that communicate with
38     *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the
39     *  license terms of these independent modules, and to copy and distribute
40     *  the resulting combined work under terms of your choice, provided that
41     *  every copy of the combined work is accompanied by a complete copy of
42     *  the source code of XviD (the version of XviD used to produce the
43     *  combined work), being distributed under the terms of the GNU General
44     *  Public License plus this exception.  An independent module is a module
45     *  which is not derived from or based on XviD.
46     *
47     *  Note that people who make modified versions of XviD are not obligated
48     *  to grant this special exception for their modified versions; it is
49     *  their choice whether to do so.  The GNU General Public License gives
50     *  permission to release a modified version without this exception; this
51     *  exception also makes it possible to release a modified version which
52     *  carries forward this exception.
53     *
54     * $Id$
55   *   *
56   *************************************************************************/   *************************************************************************/
57    
# Line 37  Line 64 
64  #include "../prediction/mbprediction.h"  #include "../prediction/mbprediction.h"
65  #include "../global.h"  #include "../global.h"
66  #include "../utils/timer.h"  #include "../utils/timer.h"
 #include "../image/interpolate8x8.h"  
 #include "motion_est.h"  
67  #include "motion.h"  #include "motion.h"
68  #include "sad.h"  #include "sad.h"
 #include "../utils/emms.h"  
   
 #define INITIAL_SKIP_THRESH     (10)  
 #define FINAL_SKIP_THRESH       (50)  
 #define MAX_SAD00_FOR_SKIP      (20)  
 #define MAX_CHROMA_SAD_FOR_SKIP (22)  
 #define SKIP_THRESH_B (15)  
69    
 #define CHECK_CANDIDATE(X,Y,D) { \  
 (*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); }  
70    
 #define iDiamondSize 2  
71    
72  static __inline int  static int32_t lambda_vec16[32] =       /* rounded values for lambda param for weight of motion bits as in modified H.26L */
73  d_mv_bits(int x, int y, const uint32_t iFcode)  { 0, (int) (1.00235 + 0.5), (int) (1.15582 + 0.5), (int) (1.31976 + 0.5),
74  {                  (int) (1.49591 + 0.5), (int) (1.68601 + 0.5),
75          int xb, yb;          (int) (1.89187 + 0.5), (int) (2.11542 + 0.5), (int) (2.35878 + 0.5),
76                    (int) (2.62429 + 0.5), (int) (2.91455 + 0.5),
77            (int) (3.23253 + 0.5), (int) (3.58158 + 0.5), (int) (3.96555 + 0.5),
78                    (int) (4.38887 + 0.5), (int) (4.85673 + 0.5),
79            (int) (5.37519 + 0.5), (int) (5.95144 + 0.5), (int) (6.59408 + 0.5),
80                    (int) (7.31349 + 0.5), (int) (8.12242 + 0.5),
81            (int) (9.03669 + 0.5), (int) (10.0763 + 0.5), (int) (11.2669 + 0.5),
82                    (int) (12.6426 + 0.5), (int) (14.2493 + 0.5),
83            (int) (16.1512 + 0.5), (int) (18.442 + 0.5), (int) (21.2656 + 0.5),
84                    (int) (24.8580 + 0.5), (int) (29.6436 + 0.5),
85            (int) (36.4949 + 0.5)
86    };
87    
88    static int32_t *lambda_vec8 = lambda_vec16;     /* same table for INTER and INTER4V for now */
89    
         if (x == 0) xb = 1;  
         else {  
                 if (x < 0) x = -x;  
                 x += (1 << (iFcode - 1)) - 1;  
                 x >>= (iFcode - 1);  
                 if (x > 32) x = 32;  
                 xb = mvtab[x] + iFcode;  
         }  
90    
         if (y == 0) yb = 1;  
         else {  
                 if (y < 0) y = -y;  
                 y += (1 << (iFcode - 1)) - 1;  
                 y >>= (iFcode - 1);  
                 if (y > 32) y = 32;  
                 yb = mvtab[y] + iFcode;  
         }  
         return xb + yb;  
 }  
   
 static int32_t  
 ChromaSAD(int dx, int dy, const SearchData * const data)  
 {  
         int sad;  
         dx = (dx >> 1) + roundtab_79[dx & 0x3];  
         dy = (dy >> 1) + roundtab_79[dy & 0x3];  
   
         switch (((dx & 1) << 1) + (dy & 1))     { // ((dx%2)?2:0)+((dy%2)?1:0)  
                 case 0:  
                         sad = sad8(data->CurU, data->RefCU + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2);  
                         sad += sad8(data->CurV, data->RefCV + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2);  
                         break;  
                 case 1:  
                         dx = dx / 2; dy = (dy - 1) / 2;  
                         sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2);  
                         sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2);  
                         break;  
                 case 2:  
                         dx = (dx - 1) / 2; dy = dy / 2;  
                         sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2);  
                         sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2);  
                         break;  
                 default:  
                         dx = (dx - 1) / 2; dy = (dy - 1) / 2;  
                         interpolate8x8_halfpel_hv(data->RefQ,  
                                                                          data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2,  
                                                                          data->rounding);  
                         sad = sad8(data->CurU, data->RefQ, data->iEdgedWidth/2);  
                         interpolate8x8_halfpel_hv(data->RefQ,  
                                                                          data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2,  
                                                                          data->rounding);  
                         sad += sad8(data->CurV, data->RefQ, data->iEdgedWidth/2);  
                         break;  
         }  
         return sad;  
 }  
91    
92  static __inline const uint8_t *  // mv.length table
93  GetReference(const int x, const int y, const int dir, const SearchData * const data)  static const uint32_t mvtab[33] = {
94            1, 2, 3, 4, 6, 7, 7, 7,
95            9, 9, 9, 10, 10, 10, 10, 10,
96            10, 10, 10, 10, 10, 10, 10, 10,
97            10, 11, 11, 11, 11, 11, 11, 12, 12
98    };
99    
100    
101    static __inline uint32_t
102    mv_bits(int32_t component,
103                    const uint32_t iFcode)
104  {  {
105  //      dir : 0 = forward, 1 = backward          if (component == 0)
106          switch ( (dir << 2) | ((x&1)<<1) | (y&1) ) {                  return 1;
                 case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth);  
                 case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth);  
                 case 2 : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth);  
                 case 3 : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth);  
                 case 4 : return data->bRef + x/2 + (y/2)*(data->iEdgedWidth);  
                 case 5 : return data->bRefV + x/2 + ((y-1)/2)*(data->iEdgedWidth);  
                 case 6 : return data->bRefH + (x-1)/2 + (y/2)*(data->iEdgedWidth);  
                 default : return data->bRefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth);  
107    
108          }          if (component < 0)
109  }                  component = -component;
110    
111  static uint8_t *          if (iFcode == 1) {
112  Interpolate8x8qpel(const int x, const int y, const int block, const int dir, const SearchData * const data)                  if (component > 32)
113  {                          component = 32;
 // create or find a qpel-precision reference picture; return pointer to it  
         uint8_t * Reference = (uint8_t *)data->RefQ + 16*dir;  
         const int32_t iEdgedWidth = data->iEdgedWidth;  
         const uint32_t rounding = data->rounding;  
         const int halfpel_x = x/2;  
         const int halfpel_y = y/2;  
         const uint8_t *ref1, *ref2, *ref3, *ref4;  
114    
115          ref1 = GetReference(halfpel_x, halfpel_y, dir, data); // this reference is used in all cases                  return mvtab[component] + 1;
116          ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;          }
         switch( ((x&1)<<1) + (y&1) ) {  
         case 0: // pure halfpel position  
                 Reference = (uint8_t *) GetReference(halfpel_x, halfpel_y, dir, data);  
                 Reference += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 break;  
117    
118          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          component += (1 << (iFcode - 1)) - 1;
119                  ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);          component >>= (iFcode - 1);
                 ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);  
                 break;  
120    
121          case 2: // x qpel, y halfpel - left or right during qpel refinement          if (component > 32)
122                  ref2 = GetReference(x - halfpel_x, halfpel_y, dir, data);                  component = 32;
                 ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);  
                 break;  
123    
124          default: // x and y in qpel resolution - the "corners" (top left/right and          return mvtab[component] + 1 + iFcode - 1;
                          // bottom left/right) during qpel refinement  
                 ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);  
                 ref3 = GetReference(x - halfpel_x, halfpel_y, dir, data);  
                 ref4 = GetReference(x - halfpel_x, y - halfpel_y, dir, data);  
                 ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);  
                 break;  
125          }          }
         return Reference;  
 }  
   
 static uint8_t *  
 Interpolate16x16qpel(const int x, const int y, const int dir, const SearchData * const data)  
 {  
 // create or find a qpel-precision reference picture; return pointer to it  
         uint8_t * Reference = (uint8_t *)data->RefQ + 16*dir;  
         const int32_t iEdgedWidth = data->iEdgedWidth;  
         const uint32_t rounding = data->rounding;  
         const int halfpel_x = x/2;  
         const int halfpel_y = y/2;  
         const uint8_t *ref1, *ref2, *ref3, *ref4;  
126    
         ref1 = GetReference(halfpel_x, halfpel_y, dir, data); // this reference is used in all cases  
         switch( ((x&1)<<1) + (y&1) ) {  
         case 0: // pure halfpel position  
                 return (uint8_t *) GetReference(halfpel_x, halfpel_y, dir, data);  
         case 1: // x halfpel, y qpel - top or bottom during qpel refinement  
                 ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);  
                 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);  
                 interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);  
                 interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8);  
                 interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);  
                 break;  
   
         case 2: // x qpel, y halfpel - left or right during qpel refinement  
                 ref2 = GetReference(x - halfpel_x, halfpel_y, dir, data);  
                 interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);  
                 interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);  
                 interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8);  
                 interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);  
                 break;  
127    
128          default: // x and y in qpel resolution - the "corners" (top left/right and  static __inline uint32_t
129                           // bottom left/right) during qpel refinement  calc_delta_16(const int32_t dx,
130                  ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data);                            const int32_t dy,
131                  ref3 = GetReference(x - halfpel_x, halfpel_y, dir, data);                            const uint32_t iFcode,
132                  ref4 = GetReference(x - halfpel_x, y - halfpel_y, dir, data);                            const uint32_t iQuant)
133                  interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);  {
134                  interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);          return NEIGH_TEND_16X16 * lambda_vec16[iQuant] * (mv_bits(dx, iFcode) +
135                  interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);                                                                                                            mv_bits(dy, iFcode));
                 interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);  
                 break;  
         }  
         return Reference;  
136  }  }
137    
138  /* CHECK_CANDIATE FUNCTIONS START */  static __inline uint32_t
139    calc_delta_8(const int32_t dx,
140                             const int32_t dy,
141                             const uint32_t iFcode,
142                             const uint32_t iQuant)
143    {
144            return NEIGH_TEND_8X8 * lambda_vec8[iQuant] * (mv_bits(dx, iFcode) +
145                                                                                                       mv_bits(dy, iFcode));
146    }
147    
148  static void  bool
149  CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  MotionEstimation(MBParam * const pParam,
150                                     FRAMEINFO * const current,
151                                     FRAMEINFO * const reference,
152                                     const IMAGE * const pRefH,
153                                     const IMAGE * const pRefV,
154                                     const IMAGE * const pRefHV,
155                                     const uint32_t iLimit)
156  {  {
157          int t, xc, yc;          const uint32_t iWcount = pParam->mb_width;
158          const uint8_t * Reference;          const uint32_t iHcount = pParam->mb_height;
159          VECTOR * current;          MACROBLOCK *const pMBs = current->mbs;
160            MACROBLOCK *const prevMBs = reference->mbs;
161            const IMAGE *const pCurrent = &current->image;
162            const IMAGE *const pRef = &reference->image;
163    
164          if (( x > data->max_dx) || ( x < data->min_dx)          static const VECTOR zeroMV = { 0, 0 };
165                  || ( y > data->max_dy) || (y < data->min_dy)) return;          VECTOR predMV;
166    
167          if (data->qpel_precision) { // x and y are in 1/4 precision          uint32_t x, y;
168                  Reference = Interpolate16x16qpel(x, y, 0, data);          uint32_t iIntra = 0;
169                  t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);          VECTOR pmv;
                 xc = x/2; yc = y/2; //for chroma sad  
                 current = data->currentQMV;  
         } else {  
                 switch ( ((x&1)<<1) + (y&1) ) {  
                         case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;  
                         case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
                         case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;  
                         default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
                 }  
                 if (data->qpel) t = d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode);  
                 else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);  
                 current = data->currentMV;  
                 xc = x; yc = y;  
         }  
170    
171          data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1);          if (sadInit)
172                    (*sadInit) ();
173    
174          data->temp[0] += (data->lambda16 * t * data->temp[0])/1000;          for (y = 0; y < iHcount; y++)   {
175          data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100;                  for (x = 0; x < iWcount; x ++)  {
176    
177          if (data->chroma) data->temp[0] += ChromaSAD(xc, yc, data);                          MACROBLOCK *const pMB = &pMBs[x + y * iWcount];
178    
179          if (data->temp[0] < data->iMinSAD[0]) {                          predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
                 data->iMinSAD[0] = data->temp[0];  
                 current[0].x = x; current[0].y = y;  
                 *dir = Direction; }  
180    
181          if (data->temp[1] < data->iMinSAD[1]) {                          pMB->sad16 =
182                  data->iMinSAD[1] = data->temp[1]; current[1].x = x; current[1].y= y; }                                  SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent,
183          if (data->temp[2] < data->iMinSAD[2]) {                                                   x, y, predMV.x, predMV.y, predMV.x, predMV.y,
184                  data->iMinSAD[2] = data->temp[2]; current[2].x = x; current[2].y = y; }                                                   current->motion_flags, current->quant,
185          if (data->temp[3] < data->iMinSAD[3]) {                                                   current->fcode, pParam, pMBs, prevMBs, &pMB->mv16,
186                  data->iMinSAD[3] = data->temp[3]; current[3].x = x; current[3].y = y; }                                                   &pMB->pmvs[0]);
         if (data->temp[4] < data->iMinSAD[4]) {  
                 data->iMinSAD[4] = data->temp[4]; current[4].x = x; current[4].y = y; }  
187    
188  }                          if (0 < (pMB->sad16 - MV16_INTER_BIAS)) {
189                                    int32_t deviation;
190    
191  static void                                  deviation =
192  CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)                                          dev16(pCurrent->y + x * 16 + y * 16 * pParam->edged_width,
193  {                                                    pParam->edged_width);
         int32_t sad;  
         const uint8_t * Reference;  
         int t;  
         VECTOR * current;  
194    
195          if (( x > data->max_dx) || ( x < data->min_dx)                                  if (deviation < (pMB->sad16 - MV16_INTER_BIAS)) {
196                  || ( y > data->max_dy) || (y < data->min_dy)) return;                                          pMB->mode = MODE_INTRA;
197                                            pMB->mv16 = pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] =
198                                                    pMB->mvs[3] = zeroMV;
199                                            pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] =
200                                                    pMB->sad8[3] = 0;
201    
202          if (data->qpel_precision) { // x and y are in 1/4 precision                                          iIntra++;
203                  Reference = Interpolate16x16qpel(x, y, 0, data);                                          if (iIntra >= iLimit)
204                  t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);                                                  return 1;
205                  current = data->currentQMV;  
206          } else {                                          continue;
                 switch ( ((x&1)<<1) + (y&1) ) {  
                         case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break;  
                         case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
                         case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break;  
                         default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break;  
207                  }                  }
                 if (data->qpel) t = d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode);  
                 else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);  
                 current = data->currentMV;  
208          }          }
209    
210          sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);                          pmv = pMB->pmvs[0];
211          sad += (data->lambda16 * t * sad)/1000;                          if (current->global_flags & XVID_INTER4V)
212                                    if ((!(current->global_flags & XVID_LUMIMASKING) ||
213                                             pMB->dquant == NO_CHANGE)) {
214                                            int32_t sad8 = IMV16X16 * current->quant;
215    
216                                            if (sad8 < pMB->sad16) {
217                                                    sad8 += pMB->sad8[0] =
218                                                            SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
219                                                                            pCurrent, 2 * x, 2 * y,
220                                                                            pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y,
221                                                                            current->motion_flags,
222                                                                            current->quant, current->fcode, pParam,
223                                                                            pMBs, prevMBs, &pMB->mvs[0],
224                                                                            &pMB->pmvs[0]);
225                                            }
226                                            if (sad8 < pMB->sad16) {
227    
228                                                    predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 1);
229                                                    sad8 += pMB->sad8[1] =
230                                                            SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
231                                                                            pCurrent, 2 * x + 1, 2 * y,
232                                                                            pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y,
233                                                                            current->motion_flags,
234                                                                            current->quant, current->fcode, pParam,
235                                                                            pMBs, prevMBs, &pMB->mvs[1],
236                                                                            &pMB->pmvs[1]);
237                                            }
238                                            if (sad8 < pMB->sad16) {
239                                                    predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 2);
240                                                    sad8 += pMB->sad8[2] =
241                                                            SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
242                                                                            pCurrent, 2 * x, 2 * y + 1,
243                                                                            pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y,
244                                                                            current->motion_flags,
245                                                                            current->quant, current->fcode, pParam,
246                                                                            pMBs, prevMBs, &pMB->mvs[2],
247                                                                            &pMB->pmvs[2]);
248                                            }
249                                            if (sad8 < pMB->sad16) {
250                                                    predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 3);
251                                                    sad8 += pMB->sad8[3] =
252                                                            SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
253                                                                            pCurrent, 2 * x + 1, 2 * y + 1,
254                                                                            pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y,
255                                                                            current->motion_flags,
256                                                                            current->quant, current->fcode, pParam,
257                                                                            pMBs, prevMBs,
258                                                                            &pMB->mvs[3],
259                                                                            &pMB->pmvs[3]);
260                                            }
261    
262                                            /* decide: MODE_INTER or MODE_INTER4V
263                                               mpeg4:   if (sad8 < pMB->sad16 - nb/2+1) use_inter4v
264                                             */
265    
266          if (sad < *(data->iMinSAD)) {                                          if (sad8 < pMB->sad16) {
267                  *(data->iMinSAD) = sad;                                                  pMB->mode = MODE_INTER4V;
268                  current->x = x; current->y = y;                                                  pMB->sad8[0] *= 4;
269                  *dir = Direction; }                                                  pMB->sad8[1] *= 4;
270                                                    pMB->sad8[2] *= 4;
271                                                    pMB->sad8[3] *= 4;
272                                                    continue;
273  }  }
274    
275  static void                                  }
 CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  
 {  
 // maximum speed - for P/B/I decision  
         int32_t sad;  
   
         if (( x > data->max_dx) || ( x < data->min_dx)  
                 || ( y > data->max_dy) || (y < data->min_dy)) return;  
276    
277          sad = sad16(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth),                          pMB->mode = MODE_INTER;
278                                          data->iEdgedWidth, 256*4096);                          pMB->pmvs[0] = pmv;     /* pMB->pmvs[1] = pMB->pmvs[2] = pMB->pmvs[3]  are not needed for INTER */
279                            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mv16;
280                            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] =
281                                    pMB->sad16;
282                            }
283                            }
284    
285          if (sad < *(data->iMinSAD)) {          return 0;
                 *(data->iMinSAD) = sad;  
                 data->currentMV[0].x = x; data->currentMV[0].y = y;  
                 *dir = Direction; }  
286  }  }
287    
288    
289  static void  #define CHECK_MV16_ZERO {\
290  CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)    if ( (0 <= max_dx) && (0 >= min_dx) \
291        && (0 <= max_dy) && (0 >= min_dy) ) \
292      { \
293        iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0, 0 , iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); \
294        iSAD += calc_delta_16(-center_x, -center_y, (uint8_t)iFcode, iQuant);\
295        if (iSAD < iMinSAD) \
296        {  iMinSAD=iSAD; currMV->x=0; currMV->y=0; }  }     \
297    }
298    
299    #define NOCHECK_MV16_CANDIDATE(X,Y) { \
300        iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
301        iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\
302        if (iSAD < iMinSAD) \
303        {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \
304    }
305    
306    #define CHECK_MV16_CANDIDATE(X,Y) { \
307      if ( ((X) <= max_dx) && ((X) >= min_dx) \
308        && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
309      { \
310        iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
311        iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\
312        if (iSAD < iMinSAD) \
313        {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \
314    }
315    
316    #define CHECK_MV16_CANDIDATE_DIR(X,Y,D) { \
317      if ( ((X) <= max_dx) && ((X) >= min_dx) \
318        && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
319      { \
320        iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
321        iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\
322        if (iSAD < iMinSAD) \
323        {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \
324    }
325    
326    #define CHECK_MV16_CANDIDATE_FOUND(X,Y,D) { \
327      if ( ((X) <= max_dx) && ((X) >= min_dx) \
328        && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
329      { \
330        iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
331        iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\
332        if (iSAD < iMinSAD) \
333        {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \
334    }
335    
336    
337    #define CHECK_MV8_ZERO {\
338      iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, 0, 0 , iEdgedWidth), iEdgedWidth); \
339      iSAD += calc_delta_8(-center_x, -center_y, (uint8_t)iFcode, iQuant);\
340      if (iSAD < iMinSAD) \
341      { iMinSAD=iSAD; currMV->x=0; currMV->y=0; } \
342    }
343    
344    #define NOCHECK_MV8_CANDIDATE(X,Y) \
345      { \
346        iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
347        iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\
348        if (iSAD < iMinSAD) \
349        {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \
350    }
351    
352    #define CHECK_MV8_CANDIDATE(X,Y) { \
353      if ( ((X) <= max_dx) && ((X) >= min_dx) \
354        && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
355      { \
356        iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
357        iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\
358        if (iSAD < iMinSAD) \
359        {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \
360    }
361    
362    #define CHECK_MV8_CANDIDATE_DIR(X,Y,D) { \
363      if ( ((X) <= max_dx) && ((X) >= min_dx) \
364        && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
365      { \
366        iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
367        iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\
368        if (iSAD < iMinSAD) \
369        {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \
370    }
371    
372    #define CHECK_MV8_CANDIDATE_FOUND(X,Y,D) { \
373      if ( ((X) <= max_dx) && ((X) >= min_dx) \
374        && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
375      { \
376        iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
377        iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\
378        if (iSAD < iMinSAD) \
379        {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \
380    }
381    
382    #if 0
383    /* too slow and not fully functional at the moment */
384    int32_t ZeroSearch16(
385                                            const uint8_t * const pRef,
386                                            const uint8_t * const pRefH,
387                                            const uint8_t * const pRefV,
388                                            const uint8_t * const pRefHV,
389                                            const IMAGE * const pCur,
390                                            const int x, const int y,
391                                            const uint32_t MotionFlags,
392                                            const uint32_t iQuant,
393                                            const uint32_t iFcode,
394                                            MBParam * const pParam,
395                                            const MACROBLOCK * const pMBs,
396                                            const MACROBLOCK * const prevMBs,
397                                            VECTOR * const currMV,
398                                            VECTOR * const currPMV)
399  {  {
400          int32_t sad;          const int32_t iEdgedWidth = pParam->edged_width;
401          int xb, yb, t;          const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth;
402          const uint8_t *ReferenceF, *ReferenceB;          int32_t iSAD;
403          VECTOR *current;          VECTOR pred;
404    
         if (( xf > data->max_dx) || ( xf < data->min_dx)  
                 || ( yf > data->max_dy) || (yf < data->min_dy)) return;  
405    
406          if (data->qpel_precision) {          pred = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
                 ReferenceF = Interpolate16x16qpel(xf, yf, 0, data);  
                 xb = data->currentQMV[1].x; yb = data->currentQMV[1].y;  
                 current = data->currentQMV;  
                 ReferenceB = Interpolate16x16qpel(xb, yb, 1, data);  
                 t = d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode)  
                                  + d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode);  
         } else {  
                 ReferenceF = Interpolate16x16qpel(2*xf, 2*yf, 0, data);  
                 xb = data->currentMV[1].x; yb = data->currentMV[1].y;  
                 ReferenceB = Interpolate16x16qpel(2*xb, 2*yb, 1, data);  
                 current = data->currentMV;  
                 if (data->qpel)  
                         t = d_mv_bits(2*xf - data->predMV.x, 2*yf - data->predMV.y, data->iFcode)  
                                          + d_mv_bits(2*xb - data->bpredMV.x, 2*yb - data->bpredMV.y, data->iFcode);  
                 else  
                         t = d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode)  
                                          + d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode);  
         }  
407    
408          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);          iSAD = sad16( cur,
409          sad += (data->lambda16 * t * sad)/1000;                  get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0,0, iEdgedWidth),
410                    iEdgedWidth, MV_MAX_ERROR);
411            if (iSAD <= iQuant * 96)
412                    iSAD -= MV16_00_BIAS;
413    
414          if (sad < *(data->iMinSAD)) {          currMV->x = 0;
415                  *(data->iMinSAD) = sad;          currMV->y = 0;
416                  current->x = xf; current->y = yf;          currPMV->x = -pred.x;
417                  *dir = Direction; }          currPMV->y = -pred.y;
 }  
418    
419  static void          return iSAD;
 CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  
 {  
         int32_t sad = 0;  
         int k;  
         const uint8_t *ReferenceF;  
         const uint8_t *ReferenceB;  
         VECTOR mvs, b_mvs;  
420    
421          if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;  }
422    #endif /* 0 */
423    
424          for (k = 0; k < 4; k++) {  int32_t
425                  mvs.x = data->directmvF[k].x + x;  Diamond16_MainSearch(const uint8_t * const pRef,
426                  b_mvs.x = ((x == 0) ?                                           const uint8_t * const pRefH,
427                          data->directmvB[k].x                                           const uint8_t * const pRefV,
428                          : mvs.x - data->referencemv[k].x);                                           const uint8_t * const pRefHV,
429                                             const uint8_t * const cur,
430                                             const int x,
431                                             const int y,
432                                             const int start_x,
433                                             const int start_y,
434                                             int iMinSAD,
435                                             VECTOR * const currMV,
436                                             const int center_x,
437                                             const int center_y,
438                                             const int32_t min_dx,
439                                             const int32_t max_dx,
440                                             const int32_t min_dy,
441                                             const int32_t max_dy,
442                                             const int32_t iEdgedWidth,
443                                             const int32_t iDiamondSize,
444                                             const int32_t iFcode,
445                                             const int32_t iQuant,
446                                             int iFound)
447    {
448    /* Do a diamond search around given starting point, return SAD of best */
449    
450            int32_t iDirection = 0;
451            int32_t iDirectionBackup;
452            int32_t iSAD;
453            VECTOR backupMV;
454    
455                  mvs.y = data->directmvF[k].y + y;          backupMV.x = start_x;
456                  b_mvs.y = ((y == 0) ?          backupMV.y = start_y;
                         data->directmvB[k].y  
                         : mvs.y - data->referencemv[k].y);  
457    
458                  if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )  /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
                         || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )  
                         || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )  
                         || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;  
459    
460                  if (!data->qpel) {          CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
461                          mvs.x *= 2; mvs.y *= 2;          CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
462                          b_mvs.x *= 2; b_mvs.y *= 2; //we move to qpel precision anyway          CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
463                  }          CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
                 ReferenceF = Interpolate8x8qpel(mvs.x, mvs.y, k, 0, data);  
                 ReferenceB = Interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data);  
464    
465                  sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth),          if (iDirection) {
466                                                  ReferenceF, ReferenceB,                  while (!iFound) {
467                                                  data->iEdgedWidth);                          iFound = 1;
468                  if (sad > *(data->iMinSAD)) return;                          backupMV = *currMV;
469                            iDirectionBackup = iDirection;
470    
471                            if (iDirectionBackup != 2)
472                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
473                                                                                       backupMV.y, 1);
474                            if (iDirectionBackup != 1)
475                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
476                                                                                       backupMV.y, 2);
477                            if (iDirectionBackup != 4)
478                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x,
479                                                                                       backupMV.y - iDiamondSize, 3);
480                            if (iDirectionBackup != 3)
481                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x,
482                                                                                       backupMV.y + iDiamondSize, 4);
483          }          }
484            } else {
485          sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;                  currMV->x = start_x;
486                    currMV->y = start_y;
487          if (sad < *(data->iMinSAD)) {          }
488                  *(data->iMinSAD) = sad;          return iMinSAD;
                 data->currentMV->x = x; data->currentMV->y = y;  
                 *dir = Direction; }  
489  }  }
490    
491  static void  int32_t
492  CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  Square16_MainSearch(const uint8_t * const pRef,
493                                            const uint8_t * const pRefH,
494                                            const uint8_t * const pRefV,
495                                            const uint8_t * const pRefHV,
496                                            const uint8_t * const cur,
497                                            const int x,
498                                            const int y,
499                                            const int start_x,
500                                            const int start_y,
501                                            int iMinSAD,
502                                            VECTOR * const currMV,
503                                            const int center_x,
504                                            const int center_y,
505                                            const int32_t min_dx,
506                                            const int32_t max_dx,
507                                            const int32_t min_dy,
508                                            const int32_t max_dy,
509                                            const int32_t iEdgedWidth,
510                                            const int32_t iDiamondSize,
511                                            const int32_t iFcode,
512                                            const int32_t iQuant,
513                                            int iFound)
514  {  {
515          int32_t sad;  /* Do a square search around given starting point, return SAD of best */
         const uint8_t *ReferenceF;  
         const uint8_t *ReferenceB;  
         VECTOR mvs, b_mvs;  
   
         if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;  
516    
517          mvs.x = data->directmvF[0].x + x;          int32_t iDirection = 0;
518          b_mvs.x = ((x == 0) ?          int32_t iSAD;
519                  data->directmvB[0].x          VECTOR backupMV;
                 : mvs.x - data->referencemv[0].x);  
520    
521          mvs.y = data->directmvF[0].y + y;          backupMV.x = start_x;
522          b_mvs.y = ((y == 0) ?          backupMV.y = start_y;
                 data->directmvB[0].y  
                 : mvs.y - data->referencemv[0].y);  
523    
524          if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )  /* It's one search with full square pattern, and new parts for all following diamonds */
                 || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )  
                 || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )  
                 || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;  
525    
526          if (!data->qpel) {  /*   new direction are extra, so 1-4 is normal diamond
527                          mvs.x *= 2; mvs.y *= 2;        537
528                          b_mvs.x *= 2; b_mvs.y *= 2; //we move to qpel precision anyway        1*2
529                  }        648
530          ReferenceF = Interpolate16x16qpel(mvs.x, mvs.y, 0, data);  */
531          ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data);  
532            CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
533            CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
534            CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
535            CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
536    
537            CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
538                                                             backupMV.y - iDiamondSize, 5);
539            CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,
540                                                             backupMV.y + iDiamondSize, 6);
541            CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
542                                                             backupMV.y - iDiamondSize, 7);
543            CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,
544                                                             backupMV.y + iDiamondSize, 8);
545    
         sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);  
         sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000;  
546    
547          if (sad < *(data->iMinSAD)) {          if (iDirection) {
548                  *(data->iMinSAD) = sad;                  while (!iFound) {
549                  data->currentMV->x = x; data->currentMV->y = y;                          iFound = 1;
550                  *dir = Direction; }                          backupMV = *currMV;
 }  
551    
552  static void                          switch (iDirection) {
553  CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)                          case 1:
554  {                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
555          int32_t sad; int t;                                                                                     backupMV.y, 1);
556          const uint8_t * Reference;                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
557                                                                                     backupMV.y - iDiamondSize, 5);
558                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
559                                                                                     backupMV.y - iDiamondSize, 7);
560                                    break;
561                            case 2:
562                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
563                                                                                     2);
564                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
565                                                                                     backupMV.y + iDiamondSize, 6);
566                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
567                                                                                     backupMV.y + iDiamondSize, 8);
568                                    break;
569    
570          if (( x > data->max_dx) || ( x < data->min_dx)                          case 3:
571                  || ( y > data->max_dy) || (y < data->min_dy)) return;                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
572                                                                                     4);
573                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
574                                                                                     backupMV.y - iDiamondSize, 7);
575                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
576                                                                                     backupMV.y + iDiamondSize, 8);
577                                    break;
578    
579          if (data->qpel) Reference = Interpolate16x16qpel(x, y, 0, data);                          case 4:
580          else Reference = Interpolate16x16qpel(2*x, 2*y, 0, data);                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
581                                                                                     3);
582                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
583                                                                                     backupMV.y - iDiamondSize, 5);
584                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
585                                                                                     backupMV.y + iDiamondSize, 6);
586                                    break;
587    
588                            case 5:
589                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
590                                                                                     1);
591                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
592                                                                                     3);
593                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
594                                                                                     backupMV.y - iDiamondSize, 5);
595                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
596                                                                                     backupMV.y + iDiamondSize, 6);
597                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
598                                                                                     backupMV.y - iDiamondSize, 7);
599                                    break;
600    
601                            case 6:
602                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
603                                                                                     2);
604                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
605                                                                                     3);
606    
607                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
608                                                                                     backupMV.y - iDiamondSize, 5);
609                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
610                                                                                     backupMV.y + iDiamondSize, 6);
611                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
612                                                                                     backupMV.y + iDiamondSize, 8);
613    
614          sad = sad8(data->Cur, Reference, data->iEdgedWidth);                                  break;
         if (data->qpel) t = d_mv_bits(2 * x - data->predMV.x, 2 * y - data->predMV.y, data->iFcode);  
         else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode);  
615    
616          sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100;                          case 7:
617                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
618                                                                                       backupMV.y, 1);
619                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
620                                                                                     4);
621                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
622                                                                                     backupMV.y - iDiamondSize, 5);
623                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
624                                                                                     backupMV.y - iDiamondSize, 7);
625                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
626                                                                                     backupMV.y + iDiamondSize, 8);
627                                    break;
628    
629          if (sad < *(data->iMinSAD)) {                          case 8:
630                  *(data->iMinSAD) = sad;                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
631                  data->currentMV->x = x; data->currentMV->y = y;                                                                                   2);
632                  *dir = Direction; }                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
633                                                                                     4);
634                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
635                                                                                     backupMV.y + iDiamondSize, 6);
636                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
637                                                                                     backupMV.y - iDiamondSize, 7);
638                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
639                                                                                     backupMV.y + iDiamondSize, 8);
640                                    break;
641                            default:
642                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
643                                                                                     1);
644                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
645                                                                                     2);
646                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
647                                                                                     3);
648                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
649                                                                                     4);
650    
651                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
652                                                                                     backupMV.y - iDiamondSize, 5);
653                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
654                                                                                     backupMV.y + iDiamondSize, 6);
655                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
656                                                                                     backupMV.y - iDiamondSize, 7);
657                                    CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
658                                                                                     backupMV.y + iDiamondSize, 8);
659                                    break;
660                            }
661  }  }
662            } else {
663                    currMV->x = start_x;
664                    currMV->y = start_y;
665            }
666            return iMinSAD;
667    }
668    
669    
670    int32_t
671    Full16_MainSearch(const uint8_t * const pRef,
672                                      const uint8_t * const pRefH,
673                                      const uint8_t * const pRefV,
674                                      const uint8_t * const pRefHV,
675                                      const uint8_t * const cur,
676                                      const int x,
677                                      const int y,
678                                      const int start_x,
679                                      const int start_y,
680                                      int iMinSAD,
681                                      VECTOR * const currMV,
682                                      const int center_x,
683                                      const int center_y,
684                                      const int32_t min_dx,
685                                      const int32_t max_dx,
686                                      const int32_t min_dy,
687                                      const int32_t max_dy,
688                                      const int32_t iEdgedWidth,
689                                      const int32_t iDiamondSize,
690                                      const int32_t iFcode,
691                                      const int32_t iQuant,
692                                      int iFound)
693    {
694            int32_t iSAD;
695            int32_t dx, dy;
696            VECTOR backupMV;
697    
698            backupMV.x = start_x;
699            backupMV.y = start_y;
700    
701  /* CHECK_CANDIATE FUNCTIONS END */          for (dx = min_dx; dx <= max_dx; dx += iDiamondSize)
702                    for (dy = min_dy; dy <= max_dy; dy += iDiamondSize)
703                            NOCHECK_MV16_CANDIDATE(dx, dy);
704    
705  /* MAINSEARCH FUNCTIONS START */          return iMinSAD;
706    }
707    
708  static void  int32_t
709  AdvDiamondSearch(int x, int y, const SearchData * const data, int bDirection)  AdvDiamond16_MainSearch(const uint8_t * const pRef,
710                                                    const uint8_t * const pRefH,
711                                                    const uint8_t * const pRefV,
712                                                    const uint8_t * const pRefHV,
713                                                    const uint8_t * const cur,
714                                                    const int x,
715                                                    const int y,
716                                                    const int start_xi,
717                                                    const int start_yi,
718                                                    int iMinSAD,
719                                                    VECTOR * const currMV,
720                                                    const int center_x,
721                                                    const int center_y,
722                                                    const int32_t min_dx,
723                                                    const int32_t max_dx,
724                                                    const int32_t min_dy,
725                                                    const int32_t max_dy,
726                                                    const int32_t iEdgedWidth,
727                                                    const int32_t iDiamondSize,
728                                                    const int32_t iFcode,
729                                                    const int32_t iQuant,
730                                                    int iDirection)
731  {  {
732    
733            int32_t iSAD;
734            int start_x = start_xi, start_y = start_yi;
735    
736  /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */  /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
737    
738                  int iDirection;          if (iDirection) {
739                    CHECK_MV16_CANDIDATE(start_x - iDiamondSize, start_y);
740                    CHECK_MV16_CANDIDATE(start_x + iDiamondSize, start_y);
741                    CHECK_MV16_CANDIDATE(start_x, start_y - iDiamondSize);
742                    CHECK_MV16_CANDIDATE(start_x, start_y + iDiamondSize);
743            } else {
744                    int bDirection = 1 + 2 + 4 + 8;
745    
746                  do {                  do {
747                          iDirection = 0;                          iDirection = 0;
748                          if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);                          if (bDirection & 1)     //we only want to check left if we came from the right (our last motion was to the left, up-left or down-left)
749                          if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);                                  CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
750                          if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4);  
751                          if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8);                          if (bDirection & 2)
752                                    CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
753    
754                            if (bDirection & 4)
755                                    CHECK_MV16_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
756    
757                            if (bDirection & 8)
758                                    CHECK_MV16_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
759    
760                          /* now we're doing diagonal checks near our candidate */                          /* now we're doing diagonal checks near our candidate */
761    
762                          if (iDirection) {               //checking if anything found                          if (iDirection)         //checking if anything found
763                            {
764                                  bDirection = iDirection;                                  bDirection = iDirection;
765                                  iDirection = 0;                                  iDirection = 0;
766                                  x = data->currentMV->x; y = data->currentMV->y;                                  start_x = currMV->x;
767                                  if (bDirection & 3) {   //our candidate is left or right                                  start_y = currMV->y;
768                                          CHECK_CANDIDATE(x, y + iDiamondSize, 8);                                  if (bDirection & 3)     //our candidate is left or right
769                                          CHECK_CANDIDATE(x, y - iDiamondSize, 4);                                  {
770                                  } else {                        // what remains here is up or down                                          CHECK_MV16_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
771                                          CHECK_CANDIDATE(x + iDiamondSize, y, 2);                                          CHECK_MV16_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
772                                          CHECK_CANDIDATE(x - iDiamondSize, y, 1); }                                  } else                  // what remains here is up or down
773                                    {
774                                            CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
775                                            CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
776                                    }
777    
778                                  if (iDirection) {                                  if (iDirection) {
779                                          bDirection += iDirection;                                          bDirection += iDirection;
780                                          x = data->currentMV->x; y = data->currentMV->y; }                                          start_x = currMV->x;
781                          } else {                                //about to quit, eh? not so fast....                                          start_y = currMV->y;
782                                    }
783                            } else                          //about to quit, eh? not so fast....
784                            {
785                                  switch (bDirection) {                                  switch (bDirection) {
786                                  case 2:                                  case 2:
787                                          CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
788                                          CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);                                                                                           start_y - iDiamondSize, 2 + 4);
789                                            CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
790                                                                                             start_y + iDiamondSize, 2 + 8);
791                                          break;                                          break;
792                                  case 1:                                  case 1:
793                                          CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);  
794                                          CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
795                                                                                             start_y - iDiamondSize, 1 + 4);
796                                            CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
797                                                                                             start_y + iDiamondSize, 1 + 8);
798                                          break;                                          break;
799                                  case 2 + 4:                                  case 2 + 4:
800                                          CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
801                                          CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);                                                                                           start_y - iDiamondSize, 1 + 4);
802                                          CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
803                                                                                             start_y - iDiamondSize, 2 + 4);
804                                            CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
805                                                                                             start_y + iDiamondSize, 2 + 8);
806                                          break;                                          break;
807                                  case 4:                                  case 4:
808                                          CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
809                                          CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);                                                                                           start_y - iDiamondSize, 2 + 4);
810                                            CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
811                                                                                             start_y - iDiamondSize, 1 + 4);
812                                          break;                                          break;
813                                  case 8:                                  case 8:
814                                          CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
815                                          CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);                                                                                           start_y + iDiamondSize, 2 + 8);
816                                            CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
817                                                                                             start_y + iDiamondSize, 1 + 8);
818                                          break;                                          break;
819                                  case 1 + 4:                                  case 1 + 4:
820                                          CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
821                                          CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);                                                                                           start_y + iDiamondSize, 1 + 8);
822                                          CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
823                                                                                             start_y - iDiamondSize, 1 + 4);
824                                            CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
825                                                                                             start_y - iDiamondSize, 2 + 4);
826                                          break;                                          break;
827                                  case 2 + 8:                                  case 2 + 8:
828                                          CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
829                                          CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);                                                                                           start_y - iDiamondSize, 1 + 4);
830                                          CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
831                                                                                             start_y + iDiamondSize, 1 + 8);
832                                            CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
833                                                                                             start_y + iDiamondSize, 2 + 8);
834                                          break;                                          break;
835                                  case 1 + 8:                                  case 1 + 8:
836                                          CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
837                                          CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);                                                                                           start_y - iDiamondSize, 2 + 4);
838                                          CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
839                                                                                             start_y + iDiamondSize, 2 + 8);
840                                            CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
841                                                                                             start_y + iDiamondSize, 1 + 8);
842                                          break;                                          break;
843                                  default:                //1+2+4+8 == we didn't find anything at all                                  default:                //1+2+4+8 == we didn't find anything at all
844                                          CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
845                                          CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);                                                                                           start_y - iDiamondSize, 1 + 4);
846                                          CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
847                                          CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);                                                                                           start_y + iDiamondSize, 1 + 8);
848                                            CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
849                                                                                             start_y - iDiamondSize, 2 + 4);
850                                            CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
851                                                                                             start_y + iDiamondSize, 2 + 8);
852                                          break;                                          break;
853                                  }                                  }
854                                  if (!iDirection) break;         //ok, the end. really                                  if (!iDirection)
855                                            break;          //ok, the end. really
856                                    else {
857                                  bDirection = iDirection;                                  bDirection = iDirection;
858                                  x = data->currentMV->x; y = data->currentMV->y;                                          start_x = currMV->x;
859                                            start_y = currMV->y;
860                                    }
861                          }                          }
862                  }                  }
863                  while (1);                              //forever                  while (1);                              //forever
864  }  }
865    
866  static void          return iMinSAD;
 SquareSearch(int x, int y, const SearchData * const data, int bDirection)  
 {  
         int iDirection;  
   
         do {  
                 iDirection = 0;  
                 if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1+16+64);  
                 if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2+32+128);  
                 if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4+16+32);  
                 if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8+64+128);  
                 if (bDirection & 16) CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1+4+16+32+64);  
                 if (bDirection & 32) CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2+4+16+32+128);  
                 if (bDirection & 64) CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1+8+16+64+128);  
                 if (bDirection & 128) CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2+8+32+64+128);  
   
                 bDirection = iDirection;  
                 x = data->currentMV->x; y = data->currentMV->y;  
         } while (iDirection);  
867  }  }
868    
869  static void  int32_t
870  DiamondSearch(int x, int y, const SearchData * const data, int bDirection)  AdvDiamond8_MainSearch(const uint8_t * const pRef,
871                                               const uint8_t * const pRefH,
872                                               const uint8_t * const pRefV,
873                                               const uint8_t * const pRefHV,
874                                               const uint8_t * const cur,
875                                               const int x,
876                                               const int y,
877                                               const int start_xi,
878                                               const int start_yi,
879                                               int iMinSAD,
880                                               VECTOR * const currMV,
881                                               const int center_x,
882                                               const int center_y,
883                                               const int32_t min_dx,
884                                               const int32_t max_dx,
885                                               const int32_t min_dy,
886                                               const int32_t max_dy,
887                                               const int32_t iEdgedWidth,
888                                               const int32_t iDiamondSize,
889                                               const int32_t iFcode,
890                                               const int32_t iQuant,
891                                               int iDirection)
892  {  {
893    
894            int32_t iSAD;
895            int start_x = start_xi, start_y = start_yi;
896    
897  /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */  /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
898    
899                  int iDirection;          if (iDirection) {
900                    CHECK_MV8_CANDIDATE(start_x - iDiamondSize, start_y);
901                    CHECK_MV8_CANDIDATE(start_x + iDiamondSize, start_y);
902                    CHECK_MV8_CANDIDATE(start_x, start_y - iDiamondSize);
903                    CHECK_MV8_CANDIDATE(start_x, start_y + iDiamondSize);
904            } else {
905                    int bDirection = 1 + 2 + 4 + 8;
906    
907                  do {                  do {
908                          iDirection = 0;                          iDirection = 0;
909                          if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);                          if (bDirection & 1)     //we only want to check left if we came from the right (our last motion was to the left, up-left or down-left)
910                          if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);                                  CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
911                          if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4);  
912                          if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8);                          if (bDirection & 2)
913                                    CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
914    
915                            if (bDirection & 4)
916                                    CHECK_MV8_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
917    
918                            if (bDirection & 8)
919                                    CHECK_MV8_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
920    
921                          /* now we're doing diagonal checks near our candidate */                          /* now we're doing diagonal checks near our candidate */
922    
923                          if (iDirection) {               //checking if anything found                          if (iDirection)         //checking if anything found
924                            {
925                                  bDirection = iDirection;                                  bDirection = iDirection;
926                                  iDirection = 0;                                  iDirection = 0;
927                                  x = data->currentMV->x; y = data->currentMV->y;                                  start_x = currMV->x;
928                                  if (bDirection & 3) {   //our candidate is left or right                                  start_y = currMV->y;
929                                          CHECK_CANDIDATE(x, y + iDiamondSize, 8);                                  if (bDirection & 3)     //our candidate is left or right
930                                          CHECK_CANDIDATE(x, y - iDiamondSize, 4);                                  {
931                                  } else {                        // what remains here is up or down                                          CHECK_MV8_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
932                                          CHECK_CANDIDATE(x + iDiamondSize, y, 2);                                          CHECK_MV8_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
933                                          CHECK_CANDIDATE(x - iDiamondSize, y, 1); }                                  } else                  // what remains here is up or down
934                                    {
935                                            CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
936                                            CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
937                                    }
938    
939                                    if (iDirection) {
940                                  bDirection += iDirection;                                  bDirection += iDirection;
941                                  x = data->currentMV->x; y = data->currentMV->y;                                          start_x = currMV->x;
942                                            start_y = currMV->y;
943                                    }
944                            } else                          //about to quit, eh? not so fast....
945                            {
946                                    switch (bDirection) {
947                                    case 2:
948                                            CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
949                                                                                            start_y - iDiamondSize, 2 + 4);
950                                            CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
951                                                                                            start_y + iDiamondSize, 2 + 8);
952                                            break;
953                                    case 1:
954                                            CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
955                                                                                            start_y - iDiamondSize, 1 + 4);
956                                            CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
957                                                                                            start_y + iDiamondSize, 1 + 8);
958                                            break;
959                                    case 2 + 4:
960                                            CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
961                                                                                            start_y - iDiamondSize, 1 + 4);
962                                            CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
963                                                                                            start_y - iDiamondSize, 2 + 4);
964                                            CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
965                                                                                            start_y + iDiamondSize, 2 + 8);
966                                            break;
967                                    case 4:
968                                            CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
969                                                                                            start_y - iDiamondSize, 2 + 4);
970                                            CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
971                                                                                            start_y - iDiamondSize, 1 + 4);
972                                            break;
973                                    case 8:
974                                            CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
975                                                                                            start_y + iDiamondSize, 2 + 8);
976                                            CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
977                                                                                            start_y + iDiamondSize, 1 + 8);
978                                            break;
979                                    case 1 + 4:
980                                            CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
981                                                                                            start_y + iDiamondSize, 1 + 8);
982                                            CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
983                                                                                            start_y - iDiamondSize, 1 + 4);
984                                            CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
985                                                                                            start_y - iDiamondSize, 2 + 4);
986                                            break;
987                                    case 2 + 8:
988                                            CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
989                                                                                            start_y - iDiamondSize, 1 + 4);
990                                            CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
991                                                                                            start_y + iDiamondSize, 1 + 8);
992                                            CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
993                                                                                            start_y + iDiamondSize, 2 + 8);
994                                            break;
995                                    case 1 + 8:
996                                            CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
997                                                                                            start_y - iDiamondSize, 2 + 4);
998                                            CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
999                                                                                            start_y + iDiamondSize, 2 + 8);
1000                                            CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1001                                                                                            start_y + iDiamondSize, 1 + 8);
1002                                            break;
1003                                    default:                //1+2+4+8 == we didn't find anything at all
1004                                            CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1005                                                                                            start_y - iDiamondSize, 1 + 4);
1006                                            CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1007                                                                                            start_y + iDiamondSize, 1 + 8);
1008                                            CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1009                                                                                            start_y - iDiamondSize, 2 + 4);
1010                                            CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1011                                                                                            start_y + iDiamondSize, 2 + 8);
1012                                            break;
1013                                    }
1014                                    if (!(iDirection))
1015                                            break;          //ok, the end. really
1016                                    else {
1017                                            bDirection = iDirection;
1018                                            start_x = currMV->x;
1019                                            start_y = currMV->y;
1020                          }                          }
1021                  }                  }
1022                  while (iDirection);                  }
1023                    while (1);                              //forever
1024            }
1025            return iMinSAD;
1026  }  }
1027    
 /* MAINSEARCH FUNCTIONS END */  
   
 /* HALFPELREFINE COULD BE A MAINSEARCH FUNCTION, BUT THERE IS NO NEED FOR IT */  
1028    
1029  static void  int32_t
1030  SubpelRefine(const SearchData * const data)  Full8_MainSearch(const uint8_t * const pRef,
1031                                     const uint8_t * const pRefH,
1032                                     const uint8_t * const pRefV,
1033                                     const uint8_t * const pRefHV,
1034                                     const uint8_t * const cur,
1035                                     const int x,
1036                                     const int y,
1037                               const int start_x,
1038                               const int start_y,
1039                               int iMinSAD,
1040                               VECTOR * const currMV,
1041                               const int center_x,
1042                               const int center_y,
1043                                     const int32_t min_dx,
1044                                     const int32_t max_dx,
1045                                     const int32_t min_dy,
1046                                     const int32_t max_dy,
1047                                     const int32_t iEdgedWidth,
1048                                     const int32_t iDiamondSize,
1049                                     const int32_t iFcode,
1050                                     const int32_t iQuant,
1051                                     int iFound)
1052  {  {
1053  /* Do a half-pel or q-pel refinement */          int32_t iSAD;
1054            int32_t dx, dy;
1055          VECTOR backupMV;          VECTOR backupMV;
         int iDirection; //not needed  
1056    
1057          if (data->qpel_precision)          backupMV.x = start_x;
1058                  backupMV = *(data->currentQMV);          backupMV.y = start_y;
1059          else backupMV = *(data->currentMV);  
1060            for (dx = min_dx; dx <= max_dx; dx += iDiamondSize)
1061                    for (dy = min_dy; dy <= max_dy; dy += iDiamondSize)
1062                            NOCHECK_MV8_CANDIDATE(dx, dy);
1063    
1064          CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0);          return iMinSAD;
1065          CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0);  }
         CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0);  
         CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0);  
1066    
1067          CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0);  Halfpel8_RefineFuncPtr Halfpel8_Refine;
         CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0);  
1068    
1069          CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0);  int32_t
1070          CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0);  Halfpel16_Refine(const uint8_t * const pRef,
1071                                     const uint8_t * const pRefH,
1072                                     const uint8_t * const pRefV,
1073                                     const uint8_t * const pRefHV,
1074                                     const uint8_t * const cur,
1075                                     const int x,
1076                                     const int y,
1077                                     VECTOR * const currMV,
1078                                     int32_t iMinSAD,
1079                               const int center_x,
1080                               const int center_y,
1081                                     const int32_t min_dx,
1082                                     const int32_t max_dx,
1083                                     const int32_t min_dy,
1084                                     const int32_t max_dy,
1085                                     const int32_t iFcode,
1086                                     const int32_t iQuant,
1087                                     const int32_t iEdgedWidth)
1088    {
1089    /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */
1090    
1091            int32_t iSAD;
1092            VECTOR backupMV = *currMV;
1093    
1094            CHECK_MV16_CANDIDATE(backupMV.x - 1, backupMV.y - 1);
1095            CHECK_MV16_CANDIDATE(backupMV.x, backupMV.y - 1);
1096            CHECK_MV16_CANDIDATE(backupMV.x + 1, backupMV.y - 1);
1097            CHECK_MV16_CANDIDATE(backupMV.x - 1, backupMV.y);
1098            CHECK_MV16_CANDIDATE(backupMV.x + 1, backupMV.y);
1099            CHECK_MV16_CANDIDATE(backupMV.x - 1, backupMV.y + 1);
1100            CHECK_MV16_CANDIDATE(backupMV.x, backupMV.y + 1);
1101            CHECK_MV16_CANDIDATE(backupMV.x + 1, backupMV.y + 1);
1102    
1103            return iMinSAD;
1104  }  }
1105    
1106  static __inline int  #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16)
 SkipDecisionP(const IMAGE * current, const IMAGE * reference,  
                                                         const int x, const int y,  
                                                         const uint32_t iEdgedWidth, const uint32_t iQuant)  
1107    
1108    
1109    
1110    int32_t
1111    PMVfastSearch16(const uint8_t * const pRef,
1112                                    const uint8_t * const pRefH,
1113                                    const uint8_t * const pRefV,
1114                                    const uint8_t * const pRefHV,
1115                                    const IMAGE * const pCur,
1116                                    const int x,
1117                                    const int y,
1118                                    const int start_x,      /* start is searched first, so it should contain the most */
1119                                    const int start_y,  /* likely motion vector for this block */
1120                                    const int center_x,     /* center is from where length of MVs is measured */
1121                                    const int center_y,
1122                                    const uint32_t MotionFlags,
1123                                    const uint32_t iQuant,
1124                                    const uint32_t iFcode,
1125                                    const MBParam * const pParam,
1126                                    const MACROBLOCK * const pMBs,
1127                                    const MACROBLOCK * const prevMBs,
1128                                    VECTOR * const currMV,
1129                                    VECTOR * const currPMV)
1130  {  {
1131  /*      keep repeating checks for all b-frames before this P frame,          const uint32_t iWcount = pParam->mb_width;
1132          to make sure that SKIP is possible (todo)          const int32_t iWidth = pParam->width;
1133          how: if skip is not possible set sad00 to a very high value */          const int32_t iHeight = pParam->height;
1134            const int32_t iEdgedWidth = pParam->edged_width;
         uint32_t sadC = sad8(current->u + x*8 + y*(iEdgedWidth/2)*8,  
                                         reference->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2);  
         if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;  
         sadC += sad8(current->v + (x + y*(iEdgedWidth/2))*8,  
                                         reference->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2);  
         if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;  
1135    
1136          return 1;          const uint8_t *cur = pCur->y + x * 16 + y * 16 * iEdgedWidth;
1137    
1138            int32_t iDiamondSize;
1139    
1140            int32_t min_dx;
1141            int32_t max_dx;
1142            int32_t min_dy;
1143            int32_t max_dy;
1144    
1145            int32_t iFound;
1146    
1147            VECTOR newMV;
1148            VECTOR backupMV;                        /* just for PMVFAST */
1149    
1150            VECTOR pmv[4];
1151            int32_t psad[4];
1152    
1153            MainSearch16FuncPtr MainSearchPtr;
1154    
1155            const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;
1156    
1157            int32_t threshA, threshB;
1158            int32_t bPredEq;
1159            int32_t iMinSAD, iSAD;
1160    
1161    /* Get maximum range */
1162            get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight,
1163                              iFcode);
1164    
1165    /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */
1166    
1167            if (!(MotionFlags & PMV_HALFPEL16)) {
1168                    min_dx = EVEN(min_dx);
1169                    max_dx = EVEN(max_dx);
1170                    min_dy = EVEN(min_dy);
1171                    max_dy = EVEN(max_dy);
1172  }  }
1173    
1174  static __inline void          /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
1175  SkipMacroblockP(MACROBLOCK *pMB, const int32_t sad)          bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad);
1176  {  
1177          pMB->mode = MODE_NOT_CODED;          if ((x == 0) && (y == 0)) {
1178          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;                  threshA = 512;
1179          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;                  threshB = 1024;
1180            } else {
1181                    threshA = psad[0];
1182                    threshB = threshA + 256;
1183                    if (threshA < 512)
1184                            threshA = 512;
1185                    if (threshA > 1024)
1186                            threshA = 1024;
1187                    if (threshB > 1792)
1188                            threshB = 1792;
1189            }
1190    
1191            iFound = 0;
1192    
1193          pMB->qmvs[0].x = pMB->qmvs[1].x = pMB->qmvs[2].x = pMB->qmvs[3].x = 0;  /* Step 4: Calculate SAD around the Median prediction.
1194          pMB->qmvs[0].y = pMB->qmvs[1].y = pMB->qmvs[2].y = pMB->qmvs[3].y = 0;     MinSAD=SAD
1195       If Motion Vector equal to Previous frame motion vector
1196       and MinSAD<PrevFrmSAD goto Step 10.
1197       If SAD<=256 goto Step 10.
1198    */
1199    
1200          pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;          currMV->x = start_x;
1201            currMV->y = start_y;
1202    
1203            if (!(MotionFlags & PMV_HALFPEL16)) {   /* This should NOT be necessary! */
1204                    currMV->x = EVEN(currMV->x);
1205                    currMV->y = EVEN(currMV->y);
1206  }  }
1207    
1208  bool          if (currMV->x > max_dx) {
1209  MotionEstimation(MBParam * const pParam,                  currMV->x = max_dx;
1210                                   FRAMEINFO * const current,          }
1211                                   FRAMEINFO * const reference,          if (currMV->x < min_dx) {
1212                                   const IMAGE * const pRefH,                  currMV->x = min_dx;
1213                                   const IMAGE * const pRefV,          }
1214                                   const IMAGE * const pRefHV,          if (currMV->y > max_dy) {
1215                                   const uint32_t iLimit)                  currMV->y = max_dy;
1216            }
1217            if (currMV->y < min_dy) {
1218                    currMV->y = min_dy;
1219            }
1220    
1221            iMinSAD =
1222                    sad16(cur,
1223                              get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV,
1224                                                     iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);
1225            iMinSAD +=
1226                    calc_delta_16(currMV->x - center_x, currMV->y - center_y,
1227                                              (uint8_t) iFcode, iQuant);
1228    
1229            if ((iMinSAD < 256) ||
1230                    ((MVequal(*currMV, prevMB->mvs[0])) &&
1231                     ((int32_t) iMinSAD < prevMB->sad16))) {
1232                    if (iMinSAD < (int)(2 * iQuant))        // high chances for SKIP-mode
1233  {  {
1234          MACROBLOCK *const pMBs = current->mbs;                          if (!MVzero(*currMV)) {
1235          const IMAGE *const pCurrent = &current->image;                                  iMinSAD += MV16_00_BIAS;
1236          const IMAGE *const pRef = &reference->image;                                  CHECK_MV16_ZERO;        // (0,0) saves space for letterboxed pictures
1237                                    iMinSAD -= MV16_00_BIAS;
1238                            }
1239                    }
1240    
1241          const VECTOR zeroMV = { 0, 0 };                  if (MotionFlags & PMV_QUICKSTOP16)
1242                            goto PMVfast16_Terminate_without_Refine;
1243                    if (MotionFlags & PMV_EARLYSTOP16)
1244                            goto PMVfast16_Terminate_with_Refine;
1245            }
1246    
         uint32_t x, y;  
         uint32_t iIntra = 0;  
         int32_t InterBias, quant = current->quant, sad00;  
         uint8_t *qimage;  
1247    
1248          // some pre-initialized thingies for SearchP  /* Step 2 (lazy eval): Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion
1249          int32_t temp[5];     vector of the median.
1250          VECTOR currentMV[5];     If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2
1251          VECTOR currentQMV[5];  */
1252          int32_t iMinSAD[5];  
1253          SearchData Data;          if ((bPredEq) && (MVequal(pmv[0], prevMB->mvs[0])))
1254          Data.iEdgedWidth = pParam->edged_width;                  iFound = 2;
1255          Data.currentMV = currentMV;  
1256          Data.currentQMV = currentQMV;  /* Step 3 (lazy eval): If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search.
1257          Data.iMinSAD = iMinSAD;     Otherwise select large Diamond Search.
1258          Data.temp = temp;  */
1259          Data.iFcode = current->fcode;  
1260          Data.rounding = pParam->m_rounding_type;          if ((!MVzero(pmv[0])) || (threshB < 1536) || (bPredEq))
1261          Data.qpel = pParam->m_quarterpel;                  iDiamondSize = 1;               // halfpel!
1262          Data.chroma = current->global_flags & XVID_ME_COLOUR;          else
1263                    iDiamondSize = 2;               // halfpel!
1264          if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)  
1265                  return 1; // allocate some mem for qpel interpolated blocks          if (!(MotionFlags & PMV_HALFPELDIAMOND16))
1266                                    // somehow this is dirty since I think we shouldn't use malloc outside                  iDiamondSize *= 2;
1267                                    // encoder_create() - so please fix me!  
1268          Data.RefQ = qimage;  /*
1269          if (sadInit) (*sadInit) ();     Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block.
1270       Also calculate (0,0) but do not subtract offset.
1271          for (y = 0; y < pParam->mb_height; y++) {     Let MinSAD be the smallest SAD up to this point.
1272                  for (x = 0; x < pParam->mb_width; x++)  {     If MV is (0,0) subtract offset.
1273                          MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];  */
1274    
1275                          pMB->sad16  // (0,0) is always possible
1276                                  = sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16,  
1277                                                          pRef->y + (x + y * pParam->edged_width) * 16,          if (!MVzero(pmv[0]))
1278                                                          pParam->edged_width, pMB->sad8 );                  CHECK_MV16_ZERO;
1279    
1280                          if (Data.chroma) {  // previous frame MV is always possible
1281                                  pMB->sad16 += sad8(pCurrent->u + x*8 + y*(pParam->edged_width/2)*8,  
1282                                                                  pRef->u + x*8 + y*(pParam->edged_width/2)*8, pParam->edged_width/2);          if (!MVzero(prevMB->mvs[0]))
1283                    if (!MVequal(prevMB->mvs[0], pmv[0]))
1284                                  pMB->sad16 += sad8(pCurrent->v + (x + y*(pParam->edged_width/2))*8,                          CHECK_MV16_CANDIDATE(prevMB->mvs[0].x, prevMB->mvs[0].y);
1285                                                                  pRef->v + (x + y*(pParam->edged_width/2))*8, pParam->edged_width/2);  
1286                          }  // left neighbour, if allowed
1287    
1288                          sad00 = pMB->sad16; //if no gmc; else sad00 = (..)          if (!MVzero(pmv[1]))
1289                    if (!MVequal(pmv[1], prevMB->mvs[0]))
1290                          if (!(current->global_flags & XVID_LUMIMASKING)) {                          if (!MVequal(pmv[1], pmv[0])) {
1291                                  pMB->dquant = NO_CHANGE;                                  if (!(MotionFlags & PMV_HALFPEL16)) {
1292                                  pMB->quant = current->quant;                                          pmv[1].x = EVEN(pmv[1].x);
1293                          } else {                                          pmv[1].y = EVEN(pmv[1].y);
                                 if (pMB->dquant != NO_CHANGE) {  
                                         quant += DQtab[pMB->dquant];  
                                         if (quant > 31) quant = 31;  
                                         else if (quant < 1) quant = 1;  
                                 }  
                                 pMB->quant = quant;  
1294                          }                          }
1295    
1296  //initial skip decision                                  CHECK_MV16_CANDIDATE(pmv[1].x, pmv[1].y);
 /* no early skip for GMC (global vector = skip vector is unknown!)  */  
                         if (current->coding_type == P_VOP)      { /* no fast SKIP for S(GMC)-VOPs */  
                                 if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH)  
                                         if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {  
                                                 SkipMacroblockP(pMB, sad00);  
                                                 continue;  
1297                                          }                                          }
1298    // top neighbour, if allowed
1299            if (!MVzero(pmv[2]))
1300                    if (!MVequal(pmv[2], prevMB->mvs[0]))
1301                            if (!MVequal(pmv[2], pmv[0]))
1302                                    if (!MVequal(pmv[2], pmv[1])) {
1303                                            if (!(MotionFlags & PMV_HALFPEL16)) {
1304                                                    pmv[2].x = EVEN(pmv[2].x);
1305                                                    pmv[2].y = EVEN(pmv[2].y);
1306                          }                          }
1307                                            CHECK_MV16_CANDIDATE(pmv[2].x, pmv[2].y);
1308    
1309                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,  // top right neighbour, if allowed
1310                                                  y, current->motion_flags, pMB->quant,                                          if (!MVzero(pmv[3]))
1311                                                  &Data, pParam, pMBs, reference->mbs,                                                  if (!MVequal(pmv[3], prevMB->mvs[0]))
1312                                                  current->global_flags & XVID_INTER4V, pMB);                                                          if (!MVequal(pmv[3], pmv[0]))
1313                                                                    if (!MVequal(pmv[3], pmv[1]))
1314  /* final skip decision, a.k.a. "the vector you found, really that good?" */                                                                          if (!MVequal(pmv[3], pmv[2])) {
1315                          if (current->coding_type == P_VOP)      {                                                                                  if (!(MotionFlags & PMV_HALFPEL16)) {
1316                                  if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP)                                                                                          pmv[3].x = EVEN(pmv[3].x);
1317                                  && ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH) )                                                                                          pmv[3].y = EVEN(pmv[3].y);
1318                                          if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) {                                                                                  }
1319                                                  SkipMacroblockP(pMB, sad00);                                                                                  CHECK_MV16_CANDIDATE(pmv[3].x,
1320                                                  continue;                                                                                                                           pmv[3].y);
1321                                                                            }
1322                                    }
1323    
1324            if ((MVzero(*currMV)) &&
1325                    (!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96) */ )
1326                    iMinSAD -= MV16_00_BIAS;
1327    
1328    
1329    /* Step 6: If MinSAD <= thresa goto Step 10.
1330       If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10.
1331    */
1332    
1333            if ((iMinSAD <= threshA) ||
1334                    (MVequal(*currMV, prevMB->mvs[0]) &&
1335                     ((int32_t) iMinSAD < prevMB->sad16))) {
1336                    if (MotionFlags & PMV_QUICKSTOP16)
1337                            goto PMVfast16_Terminate_without_Refine;
1338                    if (MotionFlags & PMV_EARLYSTOP16)
1339                            goto PMVfast16_Terminate_with_Refine;
1340            }
1341    
1342    
1343    /************ (Diamond Search)  **************/
1344    /*
1345       Step 7: Perform Diamond search, with either the small or large diamond.
1346       If Found=2 only examine one Diamond pattern, and afterwards goto step 10
1347       Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond.
1348       If center then goto step 10.
1349       Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center.
1350       Refine by using small diamond and goto step 10.
1351    */
1352    
1353            if (MotionFlags & PMV_USESQUARES16)
1354                    MainSearchPtr = Square16_MainSearch;
1355            else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
1356                    MainSearchPtr = AdvDiamond16_MainSearch;
1357            else
1358                    MainSearchPtr = Diamond16_MainSearch;
1359    
1360            backupMV = *currMV;                     /* save best prediction, actually only for EXTSEARCH */
1361    
1362    
1363    /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
1364            iSAD =
1365                    (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
1366                                                      currMV->x, currMV->y, iMinSAD, &newMV, center_x, center_y,
1367                                                      min_dx, max_dx,
1368                                                      min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
1369                                                      iQuant, iFound);
1370    
1371            if (iSAD < iMinSAD) {
1372                    *currMV = newMV;
1373                    iMinSAD = iSAD;
1374            }
1375    
1376            if (MotionFlags & PMV_EXTSEARCH16) {
1377    /* extended: search (up to) two more times: orignal prediction and (0,0) */
1378    
1379                    if (!(MVequal(pmv[0], backupMV))) {
1380                            iSAD =
1381                                    (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
1382                                                                      center_x, center_y, iMinSAD, &newMV, center_x, center_y,
1383                                                                      min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
1384                                                                      iDiamondSize, iFcode, iQuant, iFound);
1385    
1386                            if (iSAD < iMinSAD) {
1387                                    *currMV = newMV;
1388                                    iMinSAD = iSAD;
1389                            }
1390                    }
1391    
1392                    if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
1393                            iSAD =
1394                                    (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
1395                                                                      iMinSAD, &newMV, center_x, center_y,
1396                                                                      min_dx, max_dx, min_dy, max_dy,
1397                                                                      iEdgedWidth, iDiamondSize, iFcode,
1398                                                                      iQuant, iFound);
1399    
1400                            if (iSAD < iMinSAD) {
1401                                    *currMV = newMV;
1402                                    iMinSAD = iSAD;
1403                            }
1404                    }
1405            }
1406    
1407    /*
1408       Step 10:  The motion vector is chosen according to the block corresponding to MinSAD.
1409    */
1410    
1411      PMVfast16_Terminate_with_Refine:
1412            if (MotionFlags & PMV_HALFPELREFINE16)  // perform final half-pel step
1413                    iMinSAD =
1414                            Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
1415                                                             iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
1416                                                             iFcode, iQuant, iEdgedWidth);
1417    
1418      PMVfast16_Terminate_without_Refine:
1419            currPMV->x = currMV->x - center_x;
1420            currPMV->y = currMV->y - center_y;
1421            return iMinSAD;
1422    }
1423    
1424    
1425    
1426    
1427    
1428    
1429    int32_t
1430    Diamond8_MainSearch(const uint8_t * const pRef,
1431                                            const uint8_t * const pRefH,
1432                                            const uint8_t * const pRefV,
1433                                            const uint8_t * const pRefHV,
1434                                            const uint8_t * const cur,
1435                                            const int x,
1436                                            const int y,
1437                                            const int32_t start_x,
1438                                            const int32_t start_y,
1439                                            int32_t iMinSAD,
1440                                            VECTOR * const currMV,
1441                                            const int center_x,
1442                                            const int center_y,
1443                                            const int32_t min_dx,
1444                                            const int32_t max_dx,
1445                                            const int32_t min_dy,
1446                                            const int32_t max_dy,
1447                                            const int32_t iEdgedWidth,
1448                                            const int32_t iDiamondSize,
1449                                            const int32_t iFcode,
1450                                            const int32_t iQuant,
1451                                            int iFound)
1452    {
1453    /* Do a diamond search around given starting point, return SAD of best */
1454    
1455            int32_t iDirection = 0;
1456            int32_t iDirectionBackup;
1457            int32_t iSAD;
1458            VECTOR backupMV;
1459    
1460            backupMV.x = start_x;
1461            backupMV.y = start_y;
1462    
1463    /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
1464    
1465            CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
1466            CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
1467            CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
1468            CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
1469    
1470            if (iDirection) {
1471                    while (!iFound) {
1472                            iFound = 1;
1473                            backupMV = *currMV;     // since iDirection!=0, this is well defined!
1474                            iDirectionBackup = iDirection;
1475    
1476                            if (iDirectionBackup != 2)
1477                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1478                                                                                      backupMV.y, 1);
1479                            if (iDirectionBackup != 1)
1480                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1481                                                                                      backupMV.y, 2);
1482                            if (iDirectionBackup != 4)
1483                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x,
1484                                                                                      backupMV.y - iDiamondSize, 3);
1485                            if (iDirectionBackup != 3)
1486                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x,
1487                                                                                      backupMV.y + iDiamondSize, 4);
1488                    }
1489            } else {
1490                    currMV->x = start_x;
1491                    currMV->y = start_y;
1492                                          }                                          }
1493            return iMinSAD;
1494                          }                          }
1495    
 /* finally, intra decision */  
1496    
                         InterBias = MV16_INTER_BIAS;  
                         if (pMB->quant > 8)  InterBias += 100 * (pMB->quant - 8); // to make high quants work  
                         if (y != 0)  
                                 if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;  
                         if (x != 0)  
                                 if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;  
1497    
                         if (Data.chroma) InterBias += 50; // to compensate bigger SAD  
1498    
1499                          if (InterBias < pMB->sad16)  {  int32_t
1500                                  const int32_t deviation =  Square8_MainSearch(const uint8_t * const pRef,
1501                                          dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,                                     const uint8_t * const pRefH,
1502                                                    pParam->edged_width);                                     const uint8_t * const pRefV,
1503                                       const uint8_t * const pRefHV,
1504                                       const uint8_t * const cur,
1505                                       const int x,
1506                                       const int y,
1507                                       const int32_t start_x,
1508                                       const int32_t start_y,
1509                                       int32_t iMinSAD,
1510                                       VECTOR * const currMV,
1511                                       const int center_x,
1512                                       const int center_y,
1513                                       const int32_t min_dx,
1514                                       const int32_t max_dx,
1515                                       const int32_t min_dy,
1516                                       const int32_t max_dy,
1517                                       const int32_t iEdgedWidth,
1518                                       const int32_t iDiamondSize,
1519                                       const int32_t iFcode,
1520                                       const int32_t iQuant,
1521                                       int iFound)
1522    {
1523    /* Do a square search around given starting point, return SAD of best */
1524    
1525            int32_t iDirection = 0;
1526            int32_t iSAD;
1527            VECTOR backupMV;
1528    
1529            backupMV.x = start_x;
1530            backupMV.y = start_y;
1531    
1532    /* It's one search with full square pattern, and new parts for all following diamonds */
1533    
1534    /*   new direction are extra, so 1-4 is normal diamond
1535          537
1536          1*2
1537          648
1538    */
1539    
1540            CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
1541            CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
1542            CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
1543            CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
1544    
1545            CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize,
1546                                                             backupMV.y - iDiamondSize, 5);
1547            CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize,
1548                                                             backupMV.y + iDiamondSize, 6);
1549            CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize,
1550                                                             backupMV.y - iDiamondSize, 7);
1551            CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize,
1552                                                             backupMV.y + iDiamondSize, 8);
1553    
1554    
1555            if (iDirection) {
1556                    while (!iFound) {
1557                            iFound = 1;
1558                            backupMV = *currMV;
1559    
1560                                  if (deviation < (pMB->sad16 - InterBias)) {                          switch (iDirection) {
1561                                          if (++iIntra >= iLimit) { free(qimage); return 1; }                          case 1:
1562                                          pMB->mode = MODE_INTRA;                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1563                                          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] =                                                                                     backupMV.y, 1);
1564                                                          pMB->mvs[3] = zeroMV;                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1565                                          pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] =                                                                                   backupMV.y - iDiamondSize, 5);
1566                                                          pMB->qmvs[3] = zeroMV;                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1567                                          pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] =                                                                                   backupMV.y - iDiamondSize, 7);
1568                                                  pMB->sad8[3] = 0;                                  break;
1569                                  }                          case 2:
1570                          }                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1571                  }                                                                                   2);
1572          }                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1573          free(qimage);                                                                                   backupMV.y + iDiamondSize, 6);
1574                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1575                                                                                     backupMV.y + iDiamondSize, 8);
1576                                    break;
1577    
1578          if (current->coding_type == S_VOP)      /* first GMC step only for S(GMC)-VOPs */                          case 3:
1579                  current->GMC_MV = GlobalMotionEst( pMBs, pParam, current->fcode );                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1580          else                                                                                   4);
1581                  current->GMC_MV = zeroMV;                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1582                                                                                     backupMV.y - iDiamondSize, 7);
1583                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1584                                                                                     backupMV.y + iDiamondSize, 8);
1585                                    break;
1586    
1587          return 0;                          case 4:
1588  }                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1589                                                                                     3);
1590                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1591                                                                                     backupMV.y - iDiamondSize, 5);
1592                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1593                                                                                     backupMV.y + iDiamondSize, 6);
1594                                    break;
1595    
1596                            case 5:
1597                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
1598                                                                                     1);
1599                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1600                                                                                     3);
1601                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1602                                                                                     backupMV.y - iDiamondSize, 5);
1603                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1604                                                                                     backupMV.y + iDiamondSize, 6);
1605                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1606                                                                                     backupMV.y - iDiamondSize, 7);
1607                                    break;
1608    
1609                            case 6:
1610                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1611                                                                                     2);
1612                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1613                                                                                     3);
1614    
1615                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1616                                                                                     backupMV.y - iDiamondSize, 5);
1617                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1618                                                                                     backupMV.y + iDiamondSize, 6);
1619                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1620                                                                                     backupMV.y + iDiamondSize, 8);
1621    
1622                                    break;
1623    
1624  #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16)                          case 7:
1625                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1626                                                                                       backupMV.y, 1);
1627                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1628                                                                                     4);
1629                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1630                                                                                     backupMV.y - iDiamondSize, 5);
1631                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1632                                                                                     backupMV.y - iDiamondSize, 7);
1633                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1634                                                                                     backupMV.y + iDiamondSize, 8);
1635                                    break;
1636    
1637  static __inline int                          case 8:
1638  make_mask(const VECTOR * const pmv, const int i)                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1639  {                                                                                   2);
1640          int mask = 255, j;                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1641          for (j = 0; j < i; j++) {                                                                                   4);
1642                  if (MVequal(pmv[i], pmv[j])) return 0; // same vector has been checked already                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1643                  if (pmv[i].x == pmv[j].x) {                                                                                   backupMV.y + iDiamondSize, 6);
1644                          if (pmv[i].y == pmv[j].y + iDiamondSize) { mask &= ~4; continue; }                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1645                          if (pmv[i].y == pmv[j].y - iDiamondSize) { mask &= ~8; continue; }                                                                                   backupMV.y - iDiamondSize, 7);
1646                  } else                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1647                          if (pmv[i].y == pmv[j].y) {                                                                                   backupMV.y + iDiamondSize, 8);
1648                                  if (pmv[i].x == pmv[j].x + iDiamondSize) { mask &= ~1; continue; }                                  break;
1649                                  if (pmv[i].x == pmv[j].x - iDiamondSize) { mask &= ~2; continue; }                          default:
1650                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
1651                                                                                     1);
1652                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1653                                                                                     2);
1654                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1655                                                                                     3);
1656                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1657                                                                                     4);
1658    
1659                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1660                                                                                     backupMV.y - iDiamondSize, 5);
1661                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1662                                                                                     backupMV.y + iDiamondSize, 6);
1663                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1664                                                                                     backupMV.y - iDiamondSize, 7);
1665                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1666                                                                                     backupMV.y + iDiamondSize, 8);
1667                                    break;
1668                          }                          }
1669          }          }
1670          return mask;          } else {
1671                    currMV->x = start_x;
1672                    currMV->y = start_y;
1673            }
1674            return iMinSAD;
1675  }  }
1676    
 static __inline void  
 PreparePredictionsP(VECTOR * const pmv, int x, int y, const int iWcount,  
                         const int iHcount, const MACROBLOCK * const prevMB)  
 {  
   
 //this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself  
1677    
         if ( (y != 0) && (x != (iWcount-1)) ) {         // [5] top-right neighbour  
                 pmv[5].x = EVEN(pmv[3].x);  
                 pmv[5].y = EVEN(pmv[3].y);  
         } else pmv[5].x = pmv[5].y = 0;  
1678    
         if (x != 0) { pmv[3].x = EVEN(pmv[1].x); pmv[3].y = EVEN(pmv[1].y); }// pmv[3] is left neighbour  
         else pmv[3].x = pmv[3].y = 0;  
1679    
         if (y != 0) { pmv[4].x = EVEN(pmv[2].x); pmv[4].y = EVEN(pmv[2].y); }// [4] top neighbour  
     else pmv[4].x = pmv[4].y = 0;  
1680    
1681          // [1] median prediction  int32_t
1682          pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y);  Halfpel8_Refine_c(const uint8_t * const pRef,
1683                                      const uint8_t * const pRefH,
1684                                      const uint8_t * const pRefV,
1685                                      const uint8_t * const pRefHV,
1686                                      const uint8_t * const cur,
1687                                      const int x,
1688                                      const int y,
1689                                      VECTOR * const currMV,
1690                                      int32_t iMinSAD,
1691                                      const int center_x,
1692                                      const int center_y,
1693                                      const int32_t min_dx,
1694                                      const int32_t max_dx,
1695                                      const int32_t min_dy,
1696                                      const int32_t max_dy,
1697                                      const int32_t iFcode,
1698                                      const int32_t iQuant,
1699                                      const int32_t iEdgedWidth)
1700    {
1701    /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */
1702    
1703            int32_t iSAD;
1704            VECTOR backupMV = *currMV;
1705    
1706            CHECK_MV8_CANDIDATE(backupMV.x - 1, backupMV.y - 1);
1707            CHECK_MV8_CANDIDATE(backupMV.x, backupMV.y - 1);
1708            CHECK_MV8_CANDIDATE(backupMV.x + 1, backupMV.y - 1);
1709            CHECK_MV8_CANDIDATE(backupMV.x - 1, backupMV.y);
1710            CHECK_MV8_CANDIDATE(backupMV.x + 1, backupMV.y);
1711            CHECK_MV8_CANDIDATE(backupMV.x - 1, backupMV.y + 1);
1712            CHECK_MV8_CANDIDATE(backupMV.x, backupMV.y + 1);
1713            CHECK_MV8_CANDIDATE(backupMV.x + 1, backupMV.y + 1);
1714    
1715          pmv[0].x = pmv[0].y = 0; // [0] is zero; not used in the loop (checked before) but needed here for make_mask          return iMinSAD;
1716    }
1717    
         pmv[2].x = EVEN(prevMB->mvs[0].x); // [2] is last frame  
         pmv[2].y = EVEN(prevMB->mvs[0].y);  
1718    
1719          if ((x != iWcount-1) && (y != iHcount-1)) {  #define PMV_HALFPEL8 (PMV_HALFPELDIAMOND8|PMV_HALFPELREFINE8)
                 pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); //[6] right-down neighbour in last frame  
                 pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y);  
         } else pmv[6].x = pmv[6].y = 0;  
 }  
1720    
1721  static void  int32_t
1722  SearchP(const IMAGE * const pRef,  PMVfastSearch8(const uint8_t * const pRef,
1723                  const uint8_t * const pRefH,                  const uint8_t * const pRefH,
1724                  const uint8_t * const pRefV,                  const uint8_t * const pRefV,
1725                  const uint8_t * const pRefHV,                  const uint8_t * const pRefHV,
1726                  const IMAGE * const pCur,                  const IMAGE * const pCur,
1727                  const int x,                  const int x,
1728                  const int y,                  const int y,
1729                               const int start_x,
1730                               const int start_y,
1731                                    const int center_x,
1732                                    const int center_y,
1733                  const uint32_t MotionFlags,                  const uint32_t MotionFlags,
1734                  const uint32_t iQuant,                  const uint32_t iQuant,
1735                  SearchData * const Data,                             const uint32_t iFcode,
1736                  const MBParam * const pParam,                  const MBParam * const pParam,
1737                  const MACROBLOCK * const pMBs,                  const MACROBLOCK * const pMBs,
1738                  const MACROBLOCK * const prevMBs,                  const MACROBLOCK * const prevMBs,
1739                  int inter4v,                             VECTOR * const currMV,
1740                  MACROBLOCK * const pMB)                             VECTOR * const currPMV)
1741  {  {
1742            const uint32_t iWcount = pParam->mb_width;
1743            const int32_t iWidth = pParam->width;
1744            const int32_t iHeight = pParam->height;
1745            const int32_t iEdgedWidth = pParam->edged_width;
1746    
1747          int i, iDirection = 255, mask, threshA;          const uint8_t *cur = pCur->y + x * 8 + y * 8 * iEdgedWidth;
         VECTOR pmv[7];  
   
         get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);  //has to be changed to get_pmv(2)()  
         get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,  
                                 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);  
   
         Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;  
         Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;  
         Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;  
   
         Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;  
         Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;  
   
         Data->lambda16 = lambda_vec16[iQuant];  
         Data->lambda8 = lambda_vec8[iQuant];  
         Data->qpel_precision = 0;  
   
         if (!(MotionFlags & PMV_HALFPEL16)) {  
                 Data->min_dx = EVEN(Data->min_dx);  
                 Data->max_dx = EVEN(Data->max_dx);  
                 Data->min_dy = EVEN(Data->min_dy);  
                 Data->max_dy = EVEN(Data->max_dy); }  
   
         if (pMB->dquant != NO_CHANGE) inter4v = 0;  
   
         for(i = 0;  i < 5; i++)  
                 Data->currentMV[i].x = Data->currentMV[i].y = 0;  
   
         if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);  
         else Data->predMV = pmv[0];  
   
         i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode);  
         Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000;  
         Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100;  
         Data->iMinSAD[2] = pMB->sad8[1];  
         Data->iMinSAD[3] = pMB->sad8[2];  
         Data->iMinSAD[4] = pMB->sad8[3];  
1748    
1749          if ((x == 0) && (y == 0)) threshA = 512;          int32_t iDiamondSize;
         else {  
                 threshA = Data->temp[0]; // that's when we keep this SAD atm  
                 if (threshA < 512) threshA = 512;  
                 if (threshA > 1024) threshA = 1024; }  
1750    
1751          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,          int32_t min_dx;
1752                                          prevMBs + x + y * pParam->mb_width);          int32_t max_dx;
1753            int32_t min_dy;
1754            int32_t max_dy;
1755    
1756            VECTOR pmv[4];
1757            int32_t psad[4];
1758            VECTOR newMV;
1759            VECTOR backupMV;
1760            VECTOR startMV;
1761    
1762          if (inter4v || Data->chroma) CheckCandidate = CheckCandidate16;  //  const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount;
1763          else CheckCandidate = CheckCandidate16no4v; //for extra speed          const MACROBLOCK *const prevMB = prevMBs + (x >> 1) + (y >> 1) * iWcount;
1764    
1765  /* main loop. checking all predictions */           int32_t threshA, threshB;
1766            int32_t iFound, bPredEq;
1767            int32_t iMinSAD, iSAD;
1768    
1769          for (i = 1; i < 7; i++) {          int32_t iSubBlock = (y & 1) + (y & 1) + (x & 1);
                 if (!(mask = make_mask(pmv, i)) ) continue;  
                 (*CheckCandidate)(pmv[i].x, pmv[i].y, mask, &iDirection, Data);  
                 if (Data->iMinSAD[0] <= threshA) break;  
         }  
1770    
1771          if ((Data->iMinSAD[0] <= threshA) ||          MainSearch8FuncPtr MainSearchPtr;
                         (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&  
                         (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) {  
                 inter4v = 0;  
         } else {  
1772    
1773                  MainSearchFunc * MainSearchPtr;          /* Init variables */
1774                  if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;          startMV.x = start_x;
1775                  else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;          startMV.y = start_y;
                         else MainSearchPtr = DiamondSearch;  
   
                 (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection);  
   
 /* extended search, diamond starting in 0,0 and in prediction.  
         note that this search is/might be done in halfpel positions,  
         which makes it more different than the diamond above */  
1776    
1777                  if (MotionFlags & PMV_EXTSEARCH16) {          /* Get maximum range */
1778                          int32_t bSAD;          get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 8, iWidth, iHeight,
1779                          VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];                            iFcode);
                         if (!(MotionFlags & PMV_HALFPELREFINE16)) // who's gonna use extsearch and no halfpel?  
                                 startMV.x = EVEN(startMV.x); startMV.y = EVEN(startMV.y);  
                         if (!(MVequal(startMV, backupMV))) {  
                                 bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;  
1780    
1781                                  (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);          if (!(MotionFlags & PMV_HALFPELDIAMOND8)) {
1782                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);                  min_dx = EVEN(min_dx);
1783                                  if (bSAD < Data->iMinSAD[0]) {                  max_dx = EVEN(max_dx);
1784                                          Data->currentMV[0] = backupMV;                  min_dy = EVEN(min_dy);
1785                                          Data->iMinSAD[0] = bSAD; }                  max_dy = EVEN(max_dy);
1786                          }                          }
1787    
1788                          backupMV = Data->currentMV[0];          /* because we might use IF (dx>max_dx) THEN dx=max_dx; */
1789                          if (MotionFlags & PMV_HALFPELREFINE16) startMV.x = startMV.y = 1;          bPredEq = get_pmvdata2(pMBs, iWcount, 0, (x >> 1), (y >> 1), iSubBlock, pmv, psad);
                         else startMV.x = startMV.y = 0;  
                         if (!(MVequal(startMV, backupMV))) {  
                                 bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;  
1790    
1791                                  (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);          if ((x == 0) && (y == 0)) {
1792                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);                  threshA = 512 / 4;
1793                                  if (bSAD < Data->iMinSAD[0]) {                  threshB = 1024 / 4;
                                         Data->currentMV[0] = backupMV;  
                                         Data->iMinSAD[0] = bSAD; }  
                         }  
                 }  
         }  
1794    
1795          if (MotionFlags & PMV_HALFPELREFINE16) SubpelRefine(Data);          } else {
1796                    threshA = psad[0] / 4;  /* good estimate? */
1797                    threshB = threshA + 256 / 4;
1798                    if (threshA < 512 / 4)
1799                            threshA = 512 / 4;
1800                    if (threshA > 1024 / 4)
1801                            threshA = 1024 / 4;
1802                    if (threshB > 1792 / 4)
1803                            threshB = 1792 / 4;
1804            }
1805    
1806            iFound = 0;
1807    
1808    /* Step 4: Calculate SAD around the Median prediction.
1809       MinSAD=SAD
1810       If Motion Vector equal to Previous frame motion vector
1811       and MinSAD<PrevFrmSAD goto Step 10.
1812       If SAD<=256 goto Step 10.
1813    */
1814    
         for(i = 0; i < 5; i++) {  
                 Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors  
                 Data->currentQMV[i].y = 2 * Data->currentMV[i].y;  
         }  
1815    
1816          if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {  // Prepare for main loop
1817    
1818                  Data->qpel_precision = 1;    if (MotionFlags & PMV_USESQUARES8)
1819                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,        MainSearchPtr = Square8_MainSearch;
1820                                  pParam->width, pParam->height, Data->iFcode, 0);    else
1821    
1822                  SubpelRefine(Data);          if (MotionFlags & PMV_ADVANCEDDIAMOND8)
1823          }                  MainSearchPtr = AdvDiamond8_MainSearch;
1824            else
1825                    MainSearchPtr = Diamond8_MainSearch;
1826    
         if (Data->iMinSAD[0] < (int32_t)iQuant * 30 ) inter4v = 0;  
         if (inter4v) {  
                 SearchData Data8;  
                 Data8.iFcode = Data->iFcode;  
                 Data8.lambda8 = Data->lambda8;  
                 Data8.iEdgedWidth = Data->iEdgedWidth;  
                 Data8.RefQ = Data->RefQ;  
                 Data8.qpel = Data->qpel;  
                 Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);  
                 Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);  
                 Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);  
                 Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);  
1827    
1828                  if (Data->chroma) {          *currMV = startMV;
                         int sumx, sumy, dx, dy;  
1829    
1830                          if(pParam->m_quarterpel) {          iMinSAD =
1831                                  sumx= pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2;                  sad8(cur,
1832                                  sumy = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2;                           get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV,
1833                          } else {                                                  iEdgedWidth), iEdgedWidth);
1834                                  sumx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;          iMinSAD +=
1835                                  sumy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  calc_delta_8(currMV->x - center_x, currMV->y - center_y,
1836                          }                                           (uint8_t) iFcode, iQuant);
1837                          dx = (sumx >> 3) + roundtab_76[sumx & 0xf];  
1838                          dy = (sumy >> 3) + roundtab_76[sumy & 0xf];          if ((iMinSAD < 256 / 4) || ((MVequal(*currMV, prevMB->mvs[iSubBlock]))
1839                                                                    && ((int32_t) iMinSAD <
1840                                                                            prevMB->sad8[iSubBlock]))) {
1841                    if (MotionFlags & PMV_QUICKSTOP16)
1842                            goto PMVfast8_Terminate_without_Refine;
1843                    if (MotionFlags & PMV_EARLYSTOP16)
1844                            goto PMVfast8_Terminate_with_Refine;
1845            }
1846    
1847    /* Step 2 (lazy eval): Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion
1848       vector of the median.
1849       If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2
1850    */
1851    
1852            if ((bPredEq) && (MVequal(pmv[0], prevMB->mvs[iSubBlock])))
1853                    iFound = 2;
1854    
1855    /* Step 3 (lazy eval): If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search.
1856       Otherwise select large Diamond Search.
1857    */
1858    
1859                          Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);          if ((!MVzero(pmv[0])) || (threshB < 1536 / 4) || (bPredEq))
1860                  }                  iDiamondSize = 1;               // 1 halfpel!
1861          }          else
1862                    iDiamondSize = 2;               // 2 halfpel = 1 full pixel!
1863    
1864          if (!(inter4v) ||          if (!(MotionFlags & PMV_HALFPELDIAMOND8))
1865                  (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +                  iDiamondSize *= 2;
                         Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {  
 // INTER MODE  
                 pMB->mode = MODE_INTER;  
                 pMB->mvs[0] = pMB->mvs[1]  
                         = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];  
1866    
                 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =  
                         pMB->sad8[2] = pMB->sad8[3] =  Data->iMinSAD[0];  
1867    
1868                  if(pParam->m_quarterpel) {  /*
1869                          pMB->qmvs[0] = pMB->qmvs[1]     Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block.
1870                                  = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];     Also calculate (0,0) but do not subtract offset.
1871                          pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;     Let MinSAD be the smallest SAD up to this point.
1872                          pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;     If MV is (0,0) subtract offset.
1873                  } else {  */
1874                          pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;  
1875                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;  // the median prediction might be even better than mv16
1876                  }  
1877          } else {          if (!MVequal(pmv[0], startMV))
1878  // INTER4V MODE; all other things are already set in Search8                  CHECK_MV8_CANDIDATE(center_x, center_y);
1879                  pMB->mode = MODE_INTER4V;  
1880                  pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] +  // (0,0) if needed
1881                          Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * iQuant;          if (!MVzero(pmv[0]))
1882                    if (!MVzero(startMV))
1883                            CHECK_MV8_ZERO;
1884    
1885    // previous frame MV if needed
1886            if (!MVzero(prevMB->mvs[iSubBlock]))
1887                    if (!MVequal(prevMB->mvs[iSubBlock], startMV))
1888                            if (!MVequal(prevMB->mvs[iSubBlock], pmv[0]))
1889                                    CHECK_MV8_CANDIDATE(prevMB->mvs[iSubBlock].x,
1890                                                                            prevMB->mvs[iSubBlock].y);
1891    
1892            if ((iMinSAD <= threshA) ||
1893                    (MVequal(*currMV, prevMB->mvs[iSubBlock]) &&
1894                     ((int32_t) iMinSAD < prevMB->sad8[iSubBlock]))) {
1895                    if (MotionFlags & PMV_QUICKSTOP16)
1896                            goto PMVfast8_Terminate_without_Refine;
1897                    if (MotionFlags & PMV_EARLYSTOP16)
1898                            goto PMVfast8_Terminate_with_Refine;
1899            }
1900    
1901    // left neighbour, if allowed and needed
1902            if (!MVzero(pmv[1]))
1903                    if (!MVequal(pmv[1], startMV))
1904                            if (!MVequal(pmv[1], prevMB->mvs[iSubBlock]))
1905                                    if (!MVequal(pmv[1], pmv[0])) {
1906                                            if (!(MotionFlags & PMV_HALFPEL8)) {
1907                                                    pmv[1].x = EVEN(pmv[1].x);
1908                                                    pmv[1].y = EVEN(pmv[1].y);
1909                                            }
1910                                            CHECK_MV8_CANDIDATE(pmv[1].x, pmv[1].y);
1911                                    }
1912    // top neighbour, if allowed and needed
1913            if (!MVzero(pmv[2]))
1914                    if (!MVequal(pmv[2], startMV))
1915                            if (!MVequal(pmv[2], prevMB->mvs[iSubBlock]))
1916                                    if (!MVequal(pmv[2], pmv[0]))
1917                                            if (!MVequal(pmv[2], pmv[1])) {
1918                                                    if (!(MotionFlags & PMV_HALFPEL8)) {
1919                                                            pmv[2].x = EVEN(pmv[2].x);
1920                                                            pmv[2].y = EVEN(pmv[2].y);
1921                                                    }
1922                                                    CHECK_MV8_CANDIDATE(pmv[2].x, pmv[2].y);
1923    
1924    // top right neighbour, if allowed and needed
1925                                                    if (!MVzero(pmv[3]))
1926                                                            if (!MVequal(pmv[3], startMV))
1927                                                                    if (!MVequal(pmv[3], prevMB->mvs[iSubBlock]))
1928                                                                            if (!MVequal(pmv[3], pmv[0]))
1929                                                                                    if (!MVequal(pmv[3], pmv[1]))
1930                                                                                            if (!MVequal(pmv[3], pmv[2])) {
1931                                                                                                    if (!
1932                                                                                                            (MotionFlags &
1933                                                                                                             PMV_HALFPEL8)) {
1934                                                                                                            pmv[3].x = EVEN(pmv[3].x);
1935                                                                                                            pmv[3].y = EVEN(pmv[3].y);
1936                                                                                                    }
1937                                                                                                    CHECK_MV8_CANDIDATE(pmv[3].x,
1938                                                                                                                                            pmv[3].y);
1939                                                                                            }
1940                                            }
1941    
1942            if ((MVzero(*currMV)) &&
1943                    (!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96) */ )
1944                    iMinSAD -= MV8_00_BIAS;
1945    
1946    
1947    /* Step 6: If MinSAD <= thresa goto Step 10.
1948       If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10.
1949    */
1950    
1951            if ((iMinSAD <= threshA) ||
1952                    (MVequal(*currMV, prevMB->mvs[iSubBlock]) &&
1953                     ((int32_t) iMinSAD < prevMB->sad8[iSubBlock]))) {
1954                    if (MotionFlags & PMV_QUICKSTOP16)
1955                            goto PMVfast8_Terminate_without_Refine;
1956                    if (MotionFlags & PMV_EARLYSTOP16)
1957                            goto PMVfast8_Terminate_with_Refine;
1958            }
1959    
1960    /************ (Diamond Search)  **************/
1961    /*
1962       Step 7: Perform Diamond search, with either the small or large diamond.
1963       If Found=2 only examine one Diamond pattern, and afterwards goto step 10
1964       Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond.
1965       If center then goto step 10.
1966       Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center.
1967       Refine by using small diamond and goto step 10.
1968    */
1969    
1970            backupMV = *currMV;                     /* save best prediction, actually only for EXTSEARCH */
1971    
1972    /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
1973            iSAD =
1974                    (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
1975                                                      currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
1976                                                      min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
1977                                                      iQuant, iFound);
1978    
1979            if (iSAD < iMinSAD) {
1980                    *currMV = newMV;
1981                    iMinSAD = iSAD;
1982          }          }
 }  
   
 static void  
 Search8(const SearchData * const OldData,  
                 const int x, const int y,  
                 const uint32_t MotionFlags,  
                 const MBParam * const pParam,  
                 MACROBLOCK * const pMB,  
                 const MACROBLOCK * const pMBs,  
                 const int block,  
                 SearchData * const Data)  
 {  
         Data->iMinSAD = OldData->iMinSAD + 1 + block;  
         Data->currentMV = OldData->currentMV + 1 + block;  
         Data->currentQMV = OldData->currentQMV + 1 + block;  
   
         if(pParam->m_quarterpel) {  
                 Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);  
                 if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *  
                                                                         d_mv_bits(      Data->currentQMV->x - Data->predMV.x,  
                                                                                                 Data->currentQMV->y - Data->predMV.y,  
                                                                                                 Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;  
         } else {  
                 Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block);  
                 if (block != 0) *(Data->iMinSAD) += (Data->lambda8 *  
                                                                         d_mv_bits(      Data->currentMV->x - Data->predMV.x,  
                                                                                                 Data->currentMV->y - Data->predMV.y,  
                                                                                                 Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100;  
         }  
   
         if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) {  
   
                 Data->Ref = OldData->Ref + 8 * ((block&1) + pParam->edged_width*(block>>1));  
                 Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1));  
                 Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1));  
                 Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1));  
   
                 Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1));  
                 Data->qpel_precision = 0;  
   
                 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,  
                                 pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel);  
                 CheckCandidate = CheckCandidate8;  
1983    
1984                  if (MotionFlags & PMV_EXTSEARCH8) {                  if (MotionFlags & PMV_EXTSEARCH8) {
1985                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD  /* extended: search (up to) two more times: orignal prediction and (0,0) */
   
                         MainSearchFunc *MainSearchPtr;  
                         if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch;  
                                 else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;  
                                         else MainSearchPtr = DiamondSearch;  
   
                         (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);  
   
                         if(*(Data->iMinSAD) < temp_sad) {  
                                         Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector  
                                         Data->currentQMV->y = 2 * Data->currentMV->y;  
                         }  
                 }  
   
                 if (MotionFlags & PMV_HALFPELREFINE8) {  
                         int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD  
1986    
1987                          SubpelRefine(Data); // perform halfpel refine of current best vector                  if (!(MVequal(pmv[0], backupMV))) {
1988                            iSAD =
1989                                    (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
1990                                                                      pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
1991                                                                      min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
1992                                                                      iDiamondSize, iFcode, iQuant, iFound);
1993    
1994                          if(*(Data->iMinSAD) < temp_sad) { // we have found a better match                          if (iSAD < iMinSAD) {
1995                                  Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector                                  *currMV = newMV;
1996                                  Data->currentQMV->y = 2 * Data->currentMV->y;                                  iMinSAD = iSAD;
1997                          }                          }
1998                  }                  }
1999    
2000                  if(pParam->m_quarterpel) {                  if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2001                          if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) &&                          iSAD =
2002                                  (MotionFlags & PMV_QUARTERPELREFINE8)) {                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2003                          Data->qpel_precision = 1;                                                                    iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
2004                          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,                                                                    max_dy, iEdgedWidth, iDiamondSize, iFcode,
2005                                  pParam->width, pParam->height, OldData->iFcode, 0);                                                                    iQuant, iFound);
                         SubpelRefine(Data);  
                         }  
                 }  
         }  
2006    
2007          if(pParam->m_quarterpel) {                          if (iSAD < iMinSAD) {
2008                  pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x;                                  *currMV = newMV;
2009                  pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y;                                  iMinSAD = iSAD;
                 pMB->qmvs[block] = *(Data->currentQMV);  
         }  
         else {  
                 pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x;  
                 pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y;  
2010          }          }
   
         pMB->mvs[block] = *(Data->currentMV);  
         pMB->sad8[block] =  4 * (*Data->iMinSAD);  
2011  }  }
   
 /* B-frames code starts here */  
   
 static __inline VECTOR  
 ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode)  
 {  
 /* the stupidiest function ever */  
         if (mode == MODE_FORWARD) return pMB->mvs[0];  
         else return pMB->b_mvs[0];  
2012  }  }
2013    
2014  static void __inline  /* Step 10: The motion vector is chosen according to the block corresponding to MinSAD.
2015  PreparePredictionsBF(VECTOR * const pmv, const int x, const int y,     By performing an optional local half-pixel search, we can refine this result even further.
2016                                                          const uint32_t iWcount,  */
                                                         const MACROBLOCK * const pMB,  
                                                         const uint32_t mode_curr)  
 {  
   
         // [0] is prediction  
         pmv[0].x = EVEN(pmv[0].x); pmv[0].y = EVEN(pmv[0].y);  
   
         pmv[1].x = pmv[1].y = 0; // [1] is zero  
   
         pmv[2] = ChoosePred(pMB, mode_curr);  
         pmv[2].x = EVEN(pmv[2].x); pmv[2].y = EVEN(pmv[2].y);  
   
         if ((y != 0)&&(x != (int)(iWcount+1))) {                        // [3] top-right neighbour  
                 pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr);  
                 pmv[3].x = EVEN(pmv[3].x); pmv[3].y = EVEN(pmv[3].y);  
         } else pmv[3].x = pmv[3].y = 0;  
2017    
2018          if (y != 0) {    PMVfast8_Terminate_with_Refine:
2019                  pmv[4] = ChoosePred(pMB-iWcount, mode_curr);          if (MotionFlags & PMV_HALFPELREFINE8)   // perform final half-pel step
2020                  pmv[4].x = EVEN(pmv[4].x); pmv[4].y = EVEN(pmv[4].y);                  iMinSAD =
2021          } else pmv[4].x = pmv[4].y = 0;                          Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2022                                                            iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
2023                                                            iFcode, iQuant, iEdgedWidth);
2024    
         if (x != 0) {  
                 pmv[5] = ChoosePred(pMB-1, mode_curr);  
                 pmv[5].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);  
         } else pmv[5].x = pmv[5].y = 0;  
2025    
2026          if ((x != 0)&&(y != 0)) {    PMVfast8_Terminate_without_Refine:
2027                  pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr);          currPMV->x = currMV->x - center_x;
2028                  pmv[6].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y);          currPMV->y = currMV->y - center_y;
         } else pmv[6].x = pmv[6].y = 0;  
2029    
2030  // more?          return iMinSAD;
2031  }  }
2032    
2033    int32_t
2034  /* search backward or forward, for b-frames */  EPZSSearch16(const uint8_t * const pRef,
 static void  
 SearchBF(       const uint8_t * const pRef,  
2035                          const uint8_t * const pRefH,                          const uint8_t * const pRefH,
2036                          const uint8_t * const pRefV,                          const uint8_t * const pRefV,
2037                          const uint8_t * const pRefHV,                          const uint8_t * const pRefHV,
2038                          const IMAGE * const pCur,                          const IMAGE * const pCur,
2039                          const int x, const int y,                           const int x,
2040                             const int y,
2041                            const int start_x,
2042                            const int start_y,
2043                            const int center_x,
2044                            const int center_y,
2045                          const uint32_t MotionFlags,                          const uint32_t MotionFlags,
2046                             const uint32_t iQuant,
2047                          const uint32_t iFcode,                          const uint32_t iFcode,
2048                          const MBParam * const pParam,                          const MBParam * const pParam,
2049                          MACROBLOCK * const pMB,                           const MACROBLOCK * const pMBs,
2050                          const VECTOR * const predMV,                           const MACROBLOCK * const prevMBs,
2051                          int32_t * const best_sad,                           VECTOR * const currMV,
2052                          const int32_t mode_current,                           VECTOR * const currPMV)
                         SearchData * const Data)  
2053  {  {
2054            const uint32_t iWcount = pParam->mb_width;
2055            const uint32_t iHcount = pParam->mb_height;
2056    
2057            const int32_t iWidth = pParam->width;
2058            const int32_t iHeight = pParam->height;
2059          const int32_t iEdgedWidth = pParam->edged_width;          const int32_t iEdgedWidth = pParam->edged_width;
2060    
2061          int i, iDirection, mask;          const uint8_t *cur = pCur->y + x * 16 + y * 16 * iEdgedWidth;
         VECTOR pmv[7];  
         MainSearchFunc *MainSearchPtr;  
         *Data->iMinSAD = MV_MAX_ERROR;  
         Data->iFcode = iFcode;  
         Data->qpel_precision = 0;  
   
         Data->Ref = pRef + (x + y * iEdgedWidth) * 16;  
         Data->RefH = pRefH + (x + y * iEdgedWidth) * 16;  
         Data->RefV = pRefV + (x + y * iEdgedWidth) * 16;  
         Data->RefHV = pRefHV + (x + y * iEdgedWidth) * 16;  
   
         Data->predMV = *predMV;  
   
         get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,  
                                 pParam->width, pParam->height, iFcode, pParam->m_quarterpel);  
   
         pmv[0] = Data->predMV;  
         if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; }  
         PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current);  
   
         Data->currentMV->x = Data->currentMV->y = 0;  
         CheckCandidate = CheckCandidate16no4v;  
   
 // main loop. checking all predictions  
         for (i = 0; i < 8; i++) {  
                 if (!(mask = make_mask(pmv, i)) ) continue;  
                 CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);  
         }  
2062    
2063          if (MotionFlags & PMV_USESQUARES16)          int32_t min_dx;
2064                  MainSearchPtr = SquareSearch;          int32_t max_dx;
2065          else if (MotionFlags & PMV_ADVANCEDDIAMOND16)          int32_t min_dy;
2066                  MainSearchPtr = AdvDiamondSearch;          int32_t max_dy;
                 else MainSearchPtr = DiamondSearch;  
2067    
2068          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);          VECTOR newMV;
2069            VECTOR backupMV;
2070    
2071          SubpelRefine(Data);          VECTOR pmv[4];
2072            int32_t psad[8];
2073    
2074          if (Data->qpel) {          static MACROBLOCK *oldMBs = NULL;
                 Data->currentQMV->x = 2*Data->currentMV->x;  
                 Data->currentQMV->y = 2*Data->currentMV->y;  
                 Data->qpel_precision = 1;  
                 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,  
                                         pParam->width, pParam->height, iFcode, 0);  
                 SubpelRefine(Data);  
         }  
   
 // three bits are needed to code backward mode. four for forward  
 // we treat the bits just like they were vector's  
         if (mode_current == MODE_FORWARD) *Data->iMinSAD +=  4 * Data->lambda16;  
         else *Data->iMinSAD +=  3 * Data->lambda16;  
   
         if (*Data->iMinSAD < *best_sad) {  
                 *best_sad = *Data->iMinSAD;  
                 pMB->mode = mode_current;  
                 if (Data->qpel) {  
                         pMB->pmvs[0].x = Data->currentQMV->x - predMV->x;  
                         pMB->pmvs[0].y = Data->currentQMV->y - predMV->y;  
                         if (mode_current == MODE_FORWARD)  
                                 pMB->qmvs[0] = *Data->currentQMV;  
                         else  
                                 pMB->b_qmvs[0] = *Data->currentQMV;  
                 } else {  
                         pMB->pmvs[0].x = Data->currentMV->x - predMV->x;  
                         pMB->pmvs[0].y = Data->currentMV->y - predMV->y;  
                 }  
                 if (mode_current == MODE_FORWARD)  
                         pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV;  
                 else  
                         pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search  
2075    
2076          }  //  const MACROBLOCK * const pMB = pMBs + x + y * iWcount;
2077            const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;
2078            MACROBLOCK *oldMB = NULL;
2079    
2080  }           int32_t thresh2;
2081            int32_t bPredEq;
2082            int32_t iMinSAD, iSAD = 9999;
2083    
2084  static int32_t          MainSearch16FuncPtr MainSearchPtr;
2085  SearchDirect(const IMAGE * const f_Ref,  
2086                                  const uint8_t * const f_RefH,          if (oldMBs == NULL) {
2087                                  const uint8_t * const f_RefV,                  oldMBs = (MACROBLOCK *) calloc(iWcount * iHcount, sizeof(MACROBLOCK));
2088                                  const uint8_t * const f_RefHV,  //      fprintf(stderr,"allocated %d bytes for oldMBs\n",iWcount*iHcount*sizeof(MACROBLOCK));
                                 const IMAGE * const b_Ref,  
                                 const uint8_t * const b_RefH,  
                                 const uint8_t * const b_RefV,  
                                 const uint8_t * const b_RefHV,  
                                 const IMAGE * const pCur,  
                                 const int x, const int y,  
                                 const uint32_t MotionFlags,  
                                 const int32_t TRB, const int32_t TRD,  
                                 const MBParam * const pParam,  
                                 MACROBLOCK * const pMB,  
                                 const MACROBLOCK * const b_mb,  
                                 int32_t * const best_sad,  
                                 SearchData * const Data)  
   
 {  
         int32_t skip_sad;  
         int k;  
   
         MainSearchFunc *MainSearchPtr;  
   
         *Data->iMinSAD = 256*4096;  
   
         Data->Ref = f_Ref->y + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefH = f_RefH + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefV = f_RefV + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefHV = f_RefHV + (x + Data->iEdgedWidth*y) * 16;  
         Data->bRef = b_Ref->y + (x + Data->iEdgedWidth*y) * 16;  
         Data->bRefH = b_RefH + (x + Data->iEdgedWidth*y) * 16;  
         Data->bRefV = b_RefV + (x + Data->iEdgedWidth*y) * 16;  
         Data->bRefHV = b_RefHV + (x + Data->iEdgedWidth*y) * 16;  
   
         Data->max_dx = 2 * pParam->width - 2 * (x) * 16;  
         Data->max_dy = 2 * pParam->height - 2 * (y) * 16;  
         Data->min_dx = -(2 * 16 + 2 * (x) * 16);  
         Data->min_dy = -(2 * 16 + 2 * (y) * 16);  
         if (Data->qpel) { //we measure in qpixels  
                 Data->max_dx *= 2;  
                 Data->max_dy *= 2;  
                 Data->min_dx *= 2;  
                 Data->min_dy *= 2;  
                 Data->referencemv = b_mb->qmvs;  
         } else Data->referencemv = b_mb->mvs;  
         Data->qpel_precision = 0; // it's a trick. it's 1 not 0, but we need 0 here  
   
         for (k = 0; k < 4; k++) {  
                 pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD);  
                 pMB->b_mvs[k].x = Data->directmvB[k].x = ((TRB - TRD) * Data->referencemv[k].x) / TRD;  
                 pMB->mvs[k].y = Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD);  
                 pMB->b_mvs[k].y = Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD;  
   
                 if ( ( pMB->b_mvs[k].x > Data->max_dx ) || ( pMB->b_mvs[k].x < Data->min_dx )  
                         || ( pMB->b_mvs[k].y > Data->max_dy ) || ( pMB->b_mvs[k].y < Data->min_dy )) {  
   
                         *best_sad = 256*4096; // in that case, we won't use direct mode  
                         pMB->mode = MODE_DIRECT; // just to make sure it doesn't say "MODE_DIRECT_NONE_MV"  
                         pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0;  
                         return 0;  
                 }  
                 if (b_mb->mode != MODE_INTER4V) {  
                         pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mvs[0];  
                         pMB->b_mvs[1] = pMB->b_mvs[2] = pMB->b_mvs[3] = pMB->b_mvs[0];  
                         Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0];  
                         Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0];  
                         break;  
                 }  
2089          }          }
2090            oldMB = oldMBs + x + y * iWcount;
2091    
2092    /* Get maximum range */
2093            get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight,
2094                              iFcode);
2095    
2096          if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect;          if (!(MotionFlags & PMV_HALFPEL16)) {
2097          else CheckCandidate = CheckCandidateDirectno4v;                  min_dx = EVEN(min_dx);
2098                    max_dx = EVEN(max_dx);
2099                    min_dy = EVEN(min_dy);
2100                    max_dy = EVEN(max_dy);
2101            }
2102            /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
2103            bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad);
2104    
2105    /* Step 4: Calculate SAD around the Median prediction.
2106            MinSAD=SAD
2107            If Motion Vector equal to Previous frame motion vector
2108                    and MinSAD<PrevFrmSAD goto Step 10.
2109            If SAD<=256 goto Step 10.
2110    */
2111    
2112    // Prepare for main loop
2113    
2114          (*CheckCandidate)(0, 0, 255, &k, Data);          currMV->x = start_x;
2115            currMV->y = start_y;
2116    
2117            if (!(MotionFlags & PMV_HALFPEL16)) {
2118                    currMV->x = EVEN(currMV->x);
2119                    currMV->y = EVEN(currMV->y);
2120            }
2121    
2122  // skip decision          if (currMV->x > max_dx)
2123          if (*Data->iMinSAD < pMB->quant * SKIP_THRESH_B) {                  currMV->x = max_dx;
2124                  //possible skip - checking chroma. everything copied from MC          if (currMV->x < min_dx)
2125                  //this is not full chroma compensation, only it's fullpel approximation. should work though                  currMV->x = min_dx;
2126                  int sum, dx, dy, b_dx, b_dy;          if (currMV->y > max_dy)
2127                    currMV->y = max_dy;
2128            if (currMV->y < min_dy)
2129                    currMV->y = min_dy;
2130    
2131    /***************** This is predictor SET A: only median prediction ******************/
2132    
2133            iMinSAD =
2134                    sad16(cur,
2135                              get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV,
2136                                                     iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);
2137            iMinSAD +=
2138                    calc_delta_16(currMV->x - center_x, currMV->y - center_y,
2139                                              (uint8_t) iFcode, iQuant);
2140    
2141    // thresh1 is fixed to 256
2142            if ((iMinSAD < 256) ||
2143                    ((MVequal(*currMV, prevMB->mvs[0])) &&
2144                     ((int32_t) iMinSAD < prevMB->sad16))) {
2145                    if (MotionFlags & PMV_QUICKSTOP16)
2146                            goto EPZS16_Terminate_without_Refine;
2147                    if (MotionFlags & PMV_EARLYSTOP16)
2148                            goto EPZS16_Terminate_with_Refine;
2149            }
2150    
2151    /************** This is predictor SET B: (0,0), prev.frame MV, neighbours **************/
2152    
2153                  if (Data->qpel) {  // previous frame MV
2154                          sum = pMB->mvs[0].y/2 + pMB->mvs[1].y/2 + pMB->mvs[2].y/2 + pMB->mvs[3].y/2;          CHECK_MV16_CANDIDATE(prevMB->mvs[0].x, prevMB->mvs[0].y);
                         dy = (sum >> 3) + roundtab_76[sum & 0xf];  
                         sum = pMB->mvs[0].x/2 + pMB->mvs[1].x/2 + pMB->mvs[2].x/2 + pMB->mvs[3].x/2;  
                         dx = (sum >> 3) + roundtab_76[sum & 0xf];  
2155    
2156                          sum = pMB->b_mvs[0].y/2 + pMB->b_mvs[1].y/2 + pMB->b_mvs[2].y/2 + pMB->b_mvs[3].y/2;  // set threshhold based on Min of Prediction and SAD of collocated block
2157                          b_dy = (sum >> 3) + roundtab_76[sum & 0xf];  // CHECK_MV16 always uses iSAD for the SAD of last vector to check, so now iSAD is what we want
                         sum = pMB->b_mvs[0].x/2 + pMB->b_mvs[1].x/2 + pMB->b_mvs[2].x/2 + pMB->b_mvs[3].x/2;  
                         b_dx = (sum >> 3) + roundtab_76[sum & 0xf];  
2158    
2159            if ((x == 0) && (y == 0)) {
2160                    thresh2 = 512;
2161                  } else {                  } else {
2162                          sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  /* T_k = 1.2 * MIN(SAD_top,SAD_left,SAD_topleft,SAD_coll) +128;   [Tourapis, 2002] */
                         dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));  
                         sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
                         dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));  
2163    
2164                          sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;                  thresh2 = MIN(psad[0], iSAD) * 6 / 5 + 128;
                         b_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));  
                         sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;  
                         b_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));  
2165                  }                  }
                 sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2),  
                                         f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,  
                                         b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,  
                                         Data->iEdgedWidth/2);  
                 sum += sad8bi(pCur->v + 8*x + 8*y*(Data->iEdgedWidth/2),  
                                         f_Ref->v + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2,  
                                         b_Ref->v + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2,  
                                         Data->iEdgedWidth/2);  
2166    
2167                  if (sum < MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) {  // MV=(0,0) is often a good choice
2168                          pMB->mode = MODE_DIRECT_NONE_MV;  
2169                          return *Data->iMinSAD;          CHECK_MV16_ZERO;
2170    
2171    
2172    // left neighbour, if allowed
2173            if (x != 0) {
2174                    if (!(MotionFlags & PMV_HALFPEL16)) {
2175                            pmv[1].x = EVEN(pmv[1].x);
2176                            pmv[1].y = EVEN(pmv[1].y);
2177                  }                  }
2178                    CHECK_MV16_CANDIDATE(pmv[1].x, pmv[1].y);
2179          }          }
2180    // top neighbour, if allowed
2181            if (y != 0) {
2182                    if (!(MotionFlags & PMV_HALFPEL16)) {
2183                            pmv[2].x = EVEN(pmv[2].x);
2184                            pmv[2].y = EVEN(pmv[2].y);
2185                    }
2186                    CHECK_MV16_CANDIDATE(pmv[2].x, pmv[2].y);
2187    
2188          skip_sad = *Data->iMinSAD;  // top right neighbour, if allowed
2189                    if ((uint32_t) x != (iWcount - 1)) {
2190  //  DIRECT MODE DELTA VECTOR SEARCH.                          if (!(MotionFlags & PMV_HALFPEL16)) {
2191  //      This has to be made more effective, but at the moment I'm happy it's running at all                                  pmv[3].x = EVEN(pmv[3].x);
2192                                    pmv[3].y = EVEN(pmv[3].y);
2193                            }
2194                            CHECK_MV16_CANDIDATE(pmv[3].x, pmv[3].y);
2195                    }
2196            }
2197    
2198          if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;  /* Terminate if MinSAD <= T_2
2199                  else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;     Terminate if MV[t] == MV[t-1] and MinSAD[t] <= MinSAD[t-1]
2200                          else MainSearchPtr = DiamondSearch;  */
2201    
2202          (*MainSearchPtr)(0, 0, Data, 255);          if ((iMinSAD <= thresh2)
2203                    || (MVequal(*currMV, prevMB->mvs[0]) &&
2204                            ((int32_t) iMinSAD <= prevMB->sad16))) {
2205                    if (MotionFlags & PMV_QUICKSTOP16)
2206                            goto EPZS16_Terminate_without_Refine;
2207                    if (MotionFlags & PMV_EARLYSTOP16)
2208                            goto EPZS16_Terminate_with_Refine;
2209            }
2210    
2211          SubpelRefine(Data);  /***** predictor SET C: acceleration MV (new!), neighbours in prev. frame(new!) ****/
2212    
2213          *Data->iMinSAD +=  1 * Data->lambda16; // one bit is needed to code direct mode          backupMV = prevMB->mvs[0];      // collocated MV
2214          *best_sad = *Data->iMinSAD;          backupMV.x += (prevMB->mvs[0].x - oldMB->mvs[0].x);     // acceleration X
2215            backupMV.y += (prevMB->mvs[0].y - oldMB->mvs[0].y);     // acceleration Y
2216    
2217          if (b_mb->mode == MODE_INTER4V)          CHECK_MV16_CANDIDATE(backupMV.x, backupMV.y);
                 pMB->mode = MODE_DIRECT;  
         else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation  
2218    
2219          pMB->pmvs[3] = *Data->currentMV;  // left neighbour
2220            if (x != 0)
2221                    CHECK_MV16_CANDIDATE((prevMB - 1)->mvs[0].x, (prevMB - 1)->mvs[0].y);
2222    
2223          for (k = 0; k < 4; k++) {  // top neighbour
2224                  pMB->mvs[k].x = Data->directmvF[k].x + Data->currentMV->x;          if (y != 0)
2225                  pMB->b_mvs[k].x = (     (Data->currentMV->x == 0)                  CHECK_MV16_CANDIDATE((prevMB - iWcount)->mvs[0].x,
2226                                                          ? Data->directmvB[k].x                                                           (prevMB - iWcount)->mvs[0].y);
                                                         :pMB->mvs[k].x - Data->referencemv[k].x);  
                 pMB->mvs[k].y = (Data->directmvF[k].y + Data->currentMV->y);  
                 pMB->b_mvs[k].y = ((Data->currentMV->y == 0)  
                                                         ? Data->directmvB[k].y  
                                                         : pMB->mvs[k].y - Data->referencemv[k].y);  
                 if (Data->qpel) {  
                         pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2;  
                         pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2;  
                         pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2;  
                         pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2;  
                 }  
2227    
2228                  if (b_mb->mode != MODE_INTER4V) {  // right neighbour, if allowed (this value is not written yet, so take it from   pMB->mvs
                         pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0];  
                         pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0];  
                         pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0];  
                         pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0];  
                         break;  
                 }  
         }  
         return skip_sad;  
 }  
2229    
2230            if ((uint32_t) x != iWcount - 1)
2231                    CHECK_MV16_CANDIDATE((prevMB + 1)->mvs[0].x, (prevMB + 1)->mvs[0].y);
2232    
2233  static __inline void  // bottom neighbour, dito
2234  SearchInterpolate(const uint8_t * const f_Ref,          if ((uint32_t) y != iHcount - 1)
2235                                  const uint8_t * const f_RefH,                  CHECK_MV16_CANDIDATE((prevMB + iWcount)->mvs[0].x,
2236                                  const uint8_t * const f_RefV,                                                           (prevMB + iWcount)->mvs[0].y);
                                 const uint8_t * const f_RefHV,  
                                 const uint8_t * const b_Ref,  
                                 const uint8_t * const b_RefH,  
                                 const uint8_t * const b_RefV,  
                                 const uint8_t * const b_RefHV,  
                                 const IMAGE * const pCur,  
                                 const int x, const int y,  
                                 const uint32_t fcode,  
                                 const uint32_t bcode,  
                                 const uint32_t MotionFlags,  
                                 const MBParam * const pParam,  
                                 const VECTOR * const f_predMV,  
                                 const VECTOR * const b_predMV,  
                                 MACROBLOCK * const pMB,  
                                 int32_t * const best_sad,  
                                 SearchData * const fData)  
2237    
2238  {  /* Terminate if MinSAD <= T_3 (here T_3 = T_2)  */
2239            if (iMinSAD <= thresh2) {
2240                    if (MotionFlags & PMV_QUICKSTOP16)
2241                            goto EPZS16_Terminate_without_Refine;
2242                    if (MotionFlags & PMV_EARLYSTOP16)
2243                            goto EPZS16_Terminate_with_Refine;
2244            }
2245    
2246          const int32_t iEdgedWidth = pParam->edged_width;  /************ (if Diamond Search)  **************/
         int iDirection, i, j;  
         SearchData bData;  
2247    
2248          *(bData.iMinSAD = fData->iMinSAD) = 4096*256;          backupMV = *currMV;                     /* save best prediction, actually only for EXTSEARCH */
         bData.Cur = fData->Cur;  
         fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth;  
         bData.currentMV = fData->currentMV + 1; bData.currentQMV = fData->currentQMV + 1;  
         bData.lambda16 = fData->lambda16;  
         fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;  
   
         bData.bRef = fData->Ref = f_Ref + (x + y * iEdgedWidth) * 16;  
         bData.bRefH = fData->RefH = f_RefH + (x + y * iEdgedWidth) * 16;  
         bData.bRefV = fData->RefV = f_RefV + (x + y * iEdgedWidth) * 16;  
         bData.bRefHV = fData->RefHV = f_RefHV + (x + y * iEdgedWidth) * 16;  
         bData.Ref = fData->bRef = b_Ref + (x + y * iEdgedWidth) * 16;  
         bData.RefH = fData->bRefH = b_RefH + (x + y * iEdgedWidth) * 16;  
         bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16;  
         bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16;  
         bData.RefQ = fData->RefQ;  
         fData->qpel_precision = bData.qpel_precision = 0;  
         bData.rounding = 0;  
   
         bData.bpredMV = fData->predMV = *f_predMV;  
         fData->bpredMV = bData.predMV = *b_predMV;  
   
         fData->currentMV[0] = fData->currentMV[2];  
         get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, pParam->m_quarterpel);  
         get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, pParam->m_quarterpel);  
   
         if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx;  
         if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dy;  
         if (fData->currentMV[0].y > fData->max_dy) fData->currentMV[0].y = fData->max_dx;  
         if (fData->currentMV[0].y > fData->min_dy) fData->currentMV[0].y = fData->min_dy;  
   
         if (fData->currentMV[1].x > bData.max_dx) fData->currentMV[1].x = bData.max_dx;  
         if (fData->currentMV[1].x < bData.min_dx) fData->currentMV[1].x = bData.min_dy;  
         if (fData->currentMV[1].y > bData.max_dy) fData->currentMV[1].y = bData.max_dx;  
         if (fData->currentMV[1].y > bData.min_dy) fData->currentMV[1].y = bData.min_dy;  
2249    
2250          CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData);          if (MotionFlags & PMV_USESQUARES16)
2251                    MainSearchPtr = Square16_MainSearch;
2252            else
2253             if (MotionFlags & PMV_ADVANCEDDIAMOND16)
2254                    MainSearchPtr = AdvDiamond16_MainSearch;
2255            else
2256                    MainSearchPtr = Diamond16_MainSearch;
2257    
2258  //diamond. I wish we could use normal mainsearch functions (square, advdiamond)  /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
2259    
2260          do {          iSAD =
2261                  iDirection = 255;                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
2262                  // forward MV moves                                                    currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
2263                  i = fData->currentMV[0].x; j = fData->currentMV[0].y;                                                    min_dy, max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);
2264    
2265                  CheckCandidateInt(i + 1, j, 0, &iDirection, fData);          if (iSAD < iMinSAD) {
2266                  CheckCandidateInt(i, j + 1, 0, &iDirection, fData);                  *currMV = newMV;
2267                  CheckCandidateInt(i - 1, j, 0, &iDirection, fData);                  iMinSAD = iSAD;
                 CheckCandidateInt(i, j - 1, 0, &iDirection, fData);  
   
                 // backward MV moves  
                 i = fData->currentMV[1].x; j = fData->currentMV[1].y;  
                 fData->currentMV[2] = fData->currentMV[0];  
                 CheckCandidateInt(i + 1, j, 0, &iDirection, &bData);  
                 CheckCandidateInt(i, j + 1, 0, &iDirection, &bData);  
                 CheckCandidateInt(i - 1, j, 0, &iDirection, &bData);  
                 CheckCandidateInt(i, j - 1, 0, &iDirection, &bData);  
   
         } while (!(iDirection));  
   
         *fData->iMinSAD +=  2 * fData->lambda16; // two bits are needed to code interpolate mode.  
   
         if (fData->qpel) {  
                 fData->qpel_precision = bData.qpel_precision = 1;  
                 get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 0);  
                 get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, 0);  
                 fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x;  
                 fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y;  
                 fData->currentQMV[1].x = 2 * fData->currentMV[1].x;  
                 fData->currentQMV[1].y = 2 * fData->currentMV[1].y;  
                 SubpelRefine(fData);  
                 fData->currentQMV[2] = fData->currentQMV[0];  
                 SubpelRefine(&bData);  
         }  
   
         if (*fData->iMinSAD < *best_sad) {  
                 *best_sad = *fData->iMinSAD;  
                 pMB->mvs[0] = fData->currentMV[0];  
                 pMB->b_mvs[0] = fData->currentMV[1];  
                 pMB->mode = MODE_INTERPOLATE;  
                 if (fData->qpel) {  
                         pMB->qmvs[0] = fData->currentQMV[0];  
                         pMB->b_qmvs[0] = fData->currentQMV[1];  
                         pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x;  
                         pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y;  
                         pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x;  
                         pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y;  
                 } else {  
                         pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x;  
                         pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y;  
                         pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x;  
                         pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y;  
                 }  
         }  
2268  }  }
2269    
 void  
 MotionEstimationBVOP(MBParam * const pParam,  
                                          FRAMEINFO * const frame,  
                                          const int32_t time_bp,  
                                          const int32_t time_pp,  
                                          // forward (past) reference  
                                          const MACROBLOCK * const f_mbs,  
                                          const IMAGE * const f_ref,  
                                          const IMAGE * const f_refH,  
                                          const IMAGE * const f_refV,  
                                          const IMAGE * const f_refHV,  
                                          // backward (future) reference  
                                          const FRAMEINFO * const b_reference,  
                                          const IMAGE * const b_ref,  
                                          const IMAGE * const b_refH,  
                                          const IMAGE * const b_refV,  
                                          const IMAGE * const b_refHV)  
 {  
         uint32_t i, j;  
         int32_t best_sad, skip_sad;  
         int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0;  
         static const VECTOR zeroMV={0,0};  
         const MACROBLOCK * const b_mbs = b_reference->mbs;  
2270    
2271          VECTOR f_predMV, b_predMV;      /* there is no prediction for direct mode*/          if (MotionFlags & PMV_EXTSEARCH16) {
2272    /* extended mode: search (up to) two more times: orignal prediction and (0,0) */
2273    
2274          const int32_t TRB = time_pp - time_bp;                  if (!(MVequal(pmv[0], backupMV))) {
2275          const int32_t TRD = time_pp;                          iSAD =
2276          uint8_t * qimage;                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
2277                                                                      pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
2278  // some pre-inintialized data for the rest of the search                                                                    min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
2279                                                                      2, iFcode, iQuant, 0);
         SearchData Data;  
         int32_t iMinSAD;  
         VECTOR currentMV[3];  
         VECTOR currentQMV[3];  
         Data.iEdgedWidth = pParam->edged_width;  
         Data.currentMV = currentMV; Data.currentQMV = currentQMV;  
         Data.iMinSAD = &iMinSAD;  
         Data.lambda16 = lambda_vec16[frame->quant];  
         Data.qpel = pParam->m_quarterpel;  
         Data.rounding = 0;  
   
         if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)  
                 return; // allocate some mem for qpel interpolated blocks  
                                   // somehow this is dirty since I think we shouldn't use malloc outside  
                                   // encoder_create() - so please fix me!  
         Data.RefQ = qimage;  
   
         // note: i==horizontal, j==vertical  
         for (j = 0; j < pParam->mb_height; j++) {  
   
                 f_predMV = b_predMV = zeroMV;   /* prediction is reset at left boundary */  
   
                 for (i = 0; i < pParam->mb_width; i++) {  
                         MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;  
                         const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;  
   
 /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */  
                         if (b_reference->coding_type != S_VOP)  
                                 if (b_mb->mode == MODE_NOT_CODED) {  
                                         pMB->mode = MODE_NOT_CODED;  
                                         continue;  
2280                                  }                                  }
2281    
2282                          Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;                  if (iSAD < iMinSAD) {
2283                          pMB->quant = frame->quant;                          *currMV = newMV;
2284                            iMinSAD = iSAD;
 /* direct search comes first, because it (1) checks for SKIP-mode  
         and (2) sets very good predictions for forward and backward search */  
                         skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y,  
                                                                         b_ref, b_refH->y, b_refV->y, b_refHV->y,  
                                                                         &frame->image,  
                                                                         i, j,  
                                                                         frame->motion_flags,  
                                                                         TRB, TRD,  
                                                                         pParam,  
                                                                         pMB, b_mb,  
                                                                         &best_sad,  
                                                                         &Data);  
   
                         if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }  
   
                         // forward search  
                         SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,  
                                                 &frame->image, i, j,  
                                                 frame->motion_flags,  
                                                 frame->fcode, pParam,  
                                                 pMB, &f_predMV, &best_sad,  
                                                 MODE_FORWARD, &Data);  
   
                         // backward search  
                         SearchBF(b_ref->y, b_refH->y, b_refV->y, b_refHV->y,  
                                                 &frame->image, i, j,  
                                                 frame->motion_flags,  
                                                 frame->bcode, pParam,  
                                                 pMB, &b_predMV, &best_sad,  
                                                 MODE_BACKWARD, &Data);  
   
                         // interpolate search comes last, because it uses data from forward and backward as prediction  
   
                         SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,  
                                                 b_ref->y, b_refH->y, b_refV->y, b_refHV->y,  
                                                 &frame->image,  
                                                 i, j,  
                                                 frame->fcode, frame->bcode,  
                                                 frame->motion_flags,  
                                                 pParam,  
                                                 &f_predMV, &b_predMV,  
                                                 pMB, &best_sad,  
                                                 &Data);  
   
                         switch (pMB->mode) {  
                                 case MODE_FORWARD:  
                                         f_count++;  
                                         if (pParam->m_quarterpel) f_predMV = pMB->qmvs[0];  
                                         else f_predMV = pMB->mvs[0];  
                                         break;  
                                 case MODE_BACKWARD:  
                                         b_count++;  
                                         if (pParam->m_quarterpel) b_predMV = pMB->b_qmvs[0];  
                                         else b_predMV = pMB->b_mvs[0];  
                                         break;  
                                 case MODE_INTERPOLATE:  
                                         i_count++;  
                                         if (pParam->m_quarterpel) {  
                                                 f_predMV = pMB->qmvs[0];  
                                                 b_predMV = pMB->b_qmvs[0];  
                                         } else {  
                                                 f_predMV = pMB->mvs[0];  
                                                 b_predMV = pMB->b_mvs[0];  
2285                                          }                                          }
2286                                          break;  
2287                                  case MODE_DIRECT:                  if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2288                                  case MODE_DIRECT_NO4V:                          iSAD =
2289                                          d_count++;                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2290                                          break;                                                                    iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
2291                                  default:                                                                    max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);
2292                                          break;  
2293                            if (iSAD < iMinSAD) {
2294                                    *currMV = newMV;
2295                                    iMinSAD = iSAD;
2296                          }                          }
2297                  }                  }
2298          }          }
2299          free(qimage);  
2300    /***************        Choose best MV found     **************/
2301    
2302      EPZS16_Terminate_with_Refine:
2303            if (MotionFlags & PMV_HALFPELREFINE16)  // perform final half-pel step
2304                    iMinSAD =
2305                            Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2306                                                             iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
2307                                                             iFcode, iQuant, iEdgedWidth);
2308    
2309      EPZS16_Terminate_without_Refine:
2310    
2311            *oldMB = *prevMB;
2312    
2313            currPMV->x = currMV->x - center_x;
2314            currPMV->y = currMV->y - center_y;
2315            return iMinSAD;
2316  }  }
2317    
 /* Hinted ME starts here */  
2318    
2319  static void  int32_t
2320  SearchPhinted ( const IMAGE * const pRef,  EPZSSearch8(const uint8_t * const pRef,
2321                                  const uint8_t * const pRefH,                                  const uint8_t * const pRefH,
2322                                  const uint8_t * const pRefV,                                  const uint8_t * const pRefV,
2323                                  const uint8_t * const pRefHV,                                  const uint8_t * const pRefHV,
2324                                  const IMAGE * const pCur,                                  const IMAGE * const pCur,
2325                                  const int x,                                  const int x,
2326                                  const int y,                                  const int y,
2327                            const int start_x,
2328                            const int start_y,
2329                            const int center_x,
2330                            const int center_y,
2331                                  const uint32_t MotionFlags,                                  const uint32_t MotionFlags,
2332                                  const uint32_t iQuant,                                  const uint32_t iQuant,
2333                            const uint32_t iFcode,
2334                                  const MBParam * const pParam,                                  const MBParam * const pParam,
2335                                  const MACROBLOCK * const pMBs,                                  const MACROBLOCK * const pMBs,
2336                                  int inter4v,                          const MACROBLOCK * const prevMBs,
2337                                  MACROBLOCK * const pMB,                          VECTOR * const currMV,
2338                                  SearchData * const Data)                          VECTOR * const currPMV)
2339  {  {
2340    /* Please not that EPZS might not be a good choice for 8x8-block motion search ! */
         int i, t;  
         MainSearchFunc * MainSearchPtr;  
   
         get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,  
                                 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);  
   
         Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16;  
         Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8;  
         Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8;  
   
         Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16;  
         Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8;  
         Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8;  
         Data->qpel_precision = 0;  
2341    
2342          if (!(MotionFlags & PMV_HALFPEL16)) {          const uint32_t iWcount = pParam->mb_width;
2343                  Data->min_dx = EVEN(Data->min_dx);          const int32_t iWidth = pParam->width;
2344                  Data->max_dx = EVEN(Data->max_dx);          const int32_t iHeight = pParam->height;
2345                  Data->min_dy = EVEN(Data->min_dy);          const int32_t iEdgedWidth = pParam->edged_width;
                 Data->max_dy = EVEN(Data->max_dy);  
         }  
         if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);  
         else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);  
   
         for(i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;  
   
         if (pMB->dquant != NO_CHANGE) inter4v = 0;  
   
         if (inter4v || Data->chroma) CheckCandidate = CheckCandidate16;  
         else CheckCandidate = CheckCandidate16no4v;  
   
         pMB->mvs[0].x = EVEN(pMB->mvs[0].x);  
         pMB->mvs[0].y = EVEN(pMB->mvs[0].y);  
         if (pMB->mvs[0].x > Data->max_dx) pMB->mvs[0].x = Data->max_dx; // this is in case iFcode changed  
         if (pMB->mvs[0].x < Data->min_dx) pMB->mvs[0].x = Data->min_dx;  
         if (pMB->mvs[0].y > Data->max_dy) pMB->mvs[0].y = Data->max_dy;  
         if (pMB->mvs[0].y < Data->min_dy) pMB->mvs[0].y = Data->min_dy;  
   
         (*CheckCandidate)(pMB->mvs[0].x, pMB->mvs[0].y, 0, &t, Data);  
   
         if (pMB->mode == MODE_INTER4V)  
                 for (i = 1; i < 4; i++) { // all four vectors will be used as four predictions for 16x16 search  
                         pMB->mvs[i].x = EVEN(pMB->mvs[i].x);  
                         pMB->mvs[i].y = EVEN(pMB->mvs[i].y);  
                         if (!(make_mask(pMB->mvs, i)))  
                                 (*CheckCandidate)(pMB->mvs[i].x, pMB->mvs[i].y, 0, &t, Data);  
                 }  
2346    
2347          if (MotionFlags & PMV_USESQUARES16)          const uint8_t *cur = pCur->y + x * 8 + y * 8 * iEdgedWidth;
                 MainSearchPtr = SquareSearch;  
         else if (MotionFlags & PMV_ADVANCEDDIAMOND16)  
                 MainSearchPtr = AdvDiamondSearch;  
                 else MainSearchPtr = DiamondSearch;  
2348    
2349          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);          int32_t iDiamondSize = 1;
2350    
2351          if (MotionFlags & PMV_HALFPELREFINE16) SubpelRefine(Data);          int32_t min_dx;
2352            int32_t max_dx;
2353            int32_t min_dy;
2354            int32_t max_dy;
2355    
2356          for(i = 0; i < 5; i++) {          VECTOR newMV;
2357                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors          VECTOR backupMV;
                 Data->currentQMV[i].y = 2 * Data->currentMV[i].y;  
         }  
   
         if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) {  
                 get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,  
                                 pParam->width, pParam->height, Data->iFcode, 0);  
                 Data->qpel_precision = 1;  
                 SubpelRefine(Data);  
         }  
   
         if (inter4v) {  
                 SearchData Data8;  
                 Data8.iFcode = Data->iFcode;  
                 Data8.lambda8 = Data->lambda8;  
                 Data8.iEdgedWidth = Data->iEdgedWidth;  
                 Data8.RefQ = Data->RefQ;  
                 Data8.qpel = Data->qpel;  
                 Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8);  
                 Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8);  
                 Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);  
                 Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);  
   
                 if (Data->chroma) {  
                         int sumx, sumy, dx, dy;  
   
                         if(pParam->m_quarterpel) {  
                                 sumx= pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2;  
                                 sumy = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2;  
                         } else {  
                                 sumx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
                                 sumy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
                         }  
                         dx = (sumx >> 3) + roundtab_76[sumx & 0xf];  
                         dy = (sumy >> 3) + roundtab_76[sumy & 0xf];  
2358    
2359                          Data->iMinSAD[1] += ChromaSAD(dx, dy, Data);          VECTOR pmv[4];
2360                  }          int32_t psad[8];
         }  
2361    
2362          if (!(inter4v) ||          const int32_t iSubBlock = ((y & 1) << 1) + (x & 1);
                 (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] +  
                                                         Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {  
 // INTER MODE  
                 pMB->mode = MODE_INTER;  
                 pMB->mvs[0] = pMB->mvs[1]  
                         = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];  
2363    
2364                  pMB->qmvs[0] = pMB->qmvs[1]  //  const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount;
2365                          = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0];          const MACROBLOCK *const prevMB = prevMBs + (x >> 1) + (y >> 1) * iWcount;
2366    
2367                  pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] =          int32_t bPredEq;
2368                          pMB->sad8[2] = pMB->sad8[3] =  Data->iMinSAD[0];          int32_t iMinSAD, iSAD = 9999;
2369    
2370                  if(pParam->m_quarterpel) {          MainSearch8FuncPtr MainSearchPtr;
                         pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x;  
                         pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y;  
                 } else {  
                         pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;  
                         pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;  
                 }  
         } else {  
 // INTER4V MODE; all other things are already set in Search8  
                 pMB->mode = MODE_INTER4V;  
                 pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3]  
                                                 + Data->iMinSAD[4] + IMV16X16 * iQuant;  
         }  
2371    
2372  }  /* Get maximum range */
2373            get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 8, iWidth, iHeight,
2374                              iFcode);
2375    
2376  void  /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */
 MotionEstimationHinted( MBParam * const pParam,  
                                                 FRAMEINFO * const current,  
                                                 FRAMEINFO * const reference,  
                                                 const IMAGE * const pRefH,  
                                                 const IMAGE * const pRefV,  
                                                 const IMAGE * const pRefHV)  
 {  
         MACROBLOCK *const pMBs = current->mbs;  
         const IMAGE *const pCurrent = &current->image;  
         const IMAGE *const pRef = &reference->image;  
2377    
2378          uint32_t x, y;          if (!(MotionFlags & PMV_HALFPEL8)) {
2379          uint8_t * qimage;                  min_dx = EVEN(min_dx);
2380          int32_t temp[5], quant = current->quant;                  max_dx = EVEN(max_dx);
2381          int32_t iMinSAD[5];                  min_dy = EVEN(min_dy);
2382          VECTOR currentMV[5], currentQMV[5];                  max_dy = EVEN(max_dy);
         SearchData Data;  
         Data.iEdgedWidth = pParam->edged_width;  
         Data.currentMV = currentMV;  
         Data.currentQMV = currentQMV;  
         Data.iMinSAD = iMinSAD;  
         Data.temp = temp;  
         Data.iFcode = current->fcode;  
         Data.rounding = pParam->m_rounding_type;  
         Data.qpel = pParam->m_quarterpel;  
         Data.chroma = current->global_flags & XVID_ME_COLOUR;  
   
         if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL)  
                 return; // allocate some mem for qpel interpolated blocks  
                                   // somehow this is dirty since I think we shouldn't use malloc outside  
                                   // encoder_create() - so please fix me!  
   
         Data.RefQ = qimage;  
   
         if (sadInit) (*sadInit) ();  
   
         for (y = 0; y < pParam->mb_height; y++) {  
                 for (x = 0; x < pParam->mb_width; x++)  {  
   
                         MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];  
   
 //intra mode is copied from the first pass. At least for the time being  
                         if  ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue;  
   
                         if (!(current->global_flags & XVID_LUMIMASKING)) {  
                                 pMB->dquant = NO_CHANGE;  
                                 pMB->quant = current->quant; }  
                         else {  
                                 if (pMB->dquant != NO_CHANGE) {  
                                         quant += DQtab[pMB->dquant];  
                                         if (quant > 31) quant = 31;  
                                         else if (quant < 1) quant = 1;  
                                 }  
                                 pMB->quant = quant;  
2383                          }                          }
2384            /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
2385            bPredEq = get_pmvdata2(pMBs, iWcount, 0, x >> 1, y >> 1, iSubBlock, pmv, psad);
2386    
                         SearchPhinted(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,  
                                                         y, current->motion_flags, pMB->quant,  
                                                         pParam, pMBs, current->global_flags & XVID_INTER4V, pMB,  
                                                         &Data);  
2387    
2388                  }  /* Step 4: Calculate SAD around the Median prediction.
2389          }          MinSAD=SAD
2390          free(qimage);          If Motion Vector equal to Previous frame motion vector
2391  }                  and MinSAD<PrevFrmSAD goto Step 10.
2392            If SAD<=256 goto Step 10.
2393    */
2394    
2395  static __inline int  // Prepare for main loop
 MEanalyzeMB (   const uint8_t * const pRef,  
                                 const uint8_t * const pCur,  
                                 const int x,  
                                 const int y,  
                                 const MBParam * const pParam,  
                                 const MACROBLOCK * const pMBs,  
                                 MACROBLOCK * const pMB,  
                                 SearchData * const Data)  
 {  
2396    
         int i = 255, mask;  
         VECTOR pmv[3];  
         *(Data->iMinSAD) = MV_MAX_ERROR;  
2397    
2398          //median is only used as prediction. it doesn't have to be real          if (!(MotionFlags & PMV_HALFPEL8)) {
2399          if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0;                  currMV->x = EVEN(currMV->x);
2400          else                  currMV->y = EVEN(currMV->y);
                 if (x == 1) //left macroblock does not have any vector now  
                         Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median  
                 else if (y == 1) // top macroblock don't have it's vector  
                         Data->predMV = (pMB - 1)->mvs[0]; // left instead of median  
                         else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median  
   
         get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,  
                                 pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel);  
   
         Data->Cur = pCur + (x + y * pParam->edged_width) * 16;  
         Data->Ref = pRef + (x + y * pParam->edged_width) * 16;  
   
         pmv[1].x = EVEN(pMB->mvs[0].x);  
         pmv[1].y = EVEN(pMB->mvs[0].y);  
         pmv[2].x = EVEN(Data->predMV.x);  
         pmv[2].y = EVEN(Data->predMV.y);  
         pmv[0].x = pmv[0].y = 0;  
   
         (*CheckCandidate)(0, 0, 255, &i, Data);  
   
 //early skip for 0,0  
         if (*Data->iMinSAD < MAX_SAD00_FOR_SKIP * 4) {  
                 pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];  
                 pMB->mode = MODE_NOT_CODED;  
                 return 0;  
2401          }          }
2402    
2403          if (!(mask = make_mask(pmv, 1)))          if (currMV->x > max_dx)
2404                  (*CheckCandidate)(pmv[1].x, pmv[1].y, mask, &i, Data);                  currMV->x = max_dx;
2405          if (!(mask = make_mask(pmv, 2)))          if (currMV->x < min_dx)
2406                  (*CheckCandidate)(pmv[2].x, pmv[2].y, mask, &i, Data);                  currMV->x = min_dx;
2407            if (currMV->y > max_dy)
2408          if (*Data->iMinSAD > MAX_SAD00_FOR_SKIP * 4) // diamond only if needed                  currMV->y = max_dy;
2409                  DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);          if (currMV->y < min_dy)
2410                    currMV->y = min_dy;
         pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];  
         pMB->mode = MODE_INTER;  
         return *(Data->iMinSAD);  
 }  
2411    
2412  #define INTRA_THRESH    1350  /***************** This is predictor SET A: only median prediction ******************/
 #define INTER_THRESH    1200  
2413    
2414    
2415  int          iMinSAD =
2416  MEanalysis(     const IMAGE * const pRef,                  sad8(cur,
2417                          FRAMEINFO * const Current,                           get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV,
2418                          MBParam * const pParam,                                                  iEdgedWidth), iEdgedWidth);
2419                          int maxIntra, //maximum number if non-I frames          iMinSAD +=
2420                          int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame                  calc_delta_8(currMV->x - center_x, currMV->y - center_y,
2421                          int bCount) // number if B frames in a row                                           (uint8_t) iFcode, iQuant);
 {  
         uint32_t x, y, intra = 0;  
         int sSAD = 0;  
         MACROBLOCK * const pMBs = Current->mbs;  
         const IMAGE * const pCurrent = &Current->image;  
         int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH;  
   
         VECTOR currentMV;  
         int32_t iMinSAD;  
         SearchData Data;  
         Data.iEdgedWidth = pParam->edged_width;  
         Data.currentMV = &currentMV;  
         Data.iMinSAD = &iMinSAD;  
         Data.iFcode = Current->fcode;  
         CheckCandidate = CheckCandidate16no4vI;  
2422    
         if (intraCount < 10) // we're right after an I frame  
                 IntraThresh += 4 * (intraCount - 10) * (intraCount - 10);  
         else  
                 if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec  
                         IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra;  
2423    
2424    // thresh1 is fixed to 256
2425            if (iMinSAD < 256 / 4) {
2426                    if (MotionFlags & PMV_QUICKSTOP8)
2427                            goto EPZS8_Terminate_without_Refine;
2428                    if (MotionFlags & PMV_EARLYSTOP8)
2429                            goto EPZS8_Terminate_with_Refine;
2430            }
2431    
2432          InterThresh += 400 * (1 - bCount);  /************** This is predictor SET B: (0,0), prev.frame MV, neighbours **************/
         if (InterThresh < 200) InterThresh = 200;  
2433    
         if (sadInit) (*sadInit) ();  
2434    
2435          for (y = 1; y < pParam->mb_height-1; y++) {  // MV=(0,0) is often a good choice
2436                  for (x = 1; x < pParam->mb_width-1; x++) {          CHECK_MV8_ZERO;
                         int sad, dev;  
                         MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width];  
2437    
2438                          sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y,  // previous frame MV
2439                                                                  pParam, pMBs, pMB, &Data);          CHECK_MV8_CANDIDATE(prevMB->mvs[iSubBlock].x, prevMB->mvs[iSubBlock].y);
2440    
2441                          if (sad > IntraThresh) {  // left neighbour, if allowed
2442                                  dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16,          if (psad[1] != MV_MAX_ERROR) {
2443                                                            pParam->edged_width);                  if (!(MotionFlags & PMV_HALFPEL8)) {
2444                                  if (dev + IntraThresh < sad) {                          pmv[1].x = EVEN(pmv[1].x);
2445                                          pMB->mode = MODE_INTRA;                          pmv[1].y = EVEN(pmv[1].y);
                                         if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2;  // I frame  
2446                                  }                                  }
2447                    CHECK_MV8_CANDIDATE(pmv[1].x, pmv[1].y);
2448                          }                          }
2449                          sSAD += sad;  // top neighbour, if allowed
2450            if (psad[2] != MV_MAX_ERROR) {
2451                    if (!(MotionFlags & PMV_HALFPEL8)) {
2452                            pmv[2].x = EVEN(pmv[2].x);
2453                            pmv[2].y = EVEN(pmv[2].y);
2454                  }                  }
2455          }                  CHECK_MV8_CANDIDATE(pmv[2].x, pmv[2].y);
         sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2);  
         if (sSAD > InterThresh ) return 1; //P frame  
         emms();  
         return 0; // B frame  
2456    
2457    // top right neighbour, if allowed
2458                    if (psad[3] != MV_MAX_ERROR) {
2459                            if (!(MotionFlags & PMV_HALFPEL8)) {
2460                                    pmv[3].x = EVEN(pmv[3].x);
2461                                    pmv[3].y = EVEN(pmv[3].y);
2462                            }
2463                            CHECK_MV8_CANDIDATE(pmv[3].x, pmv[3].y);
2464                    }
2465  }  }
2466    
2467  int  /*  // this bias is zero anyway, at the moment!
 FindFcode(      const MBParam * const pParam,  
                         const FRAMEINFO * const current)  
 {  
         uint32_t x, y;  
         int max = 0, min = 0, i;  
2468    
2469          for (y = 0; y < pParam->mb_height; y++) {          if ( (MVzero(*currMV)) && (!MVzero(pmv[0])) ) // && (iMinSAD <= iQuant * 96)
2470                  for (x = 0; x < pParam->mb_width; x++) {                  iMinSAD -= MV8_00_BIAS;
2471    
2472                          MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];  */
                         for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4:1); i++) {  
                                 if (pMB->mvs[i].x > max) max = pMB->mvs[i].x;  
                                 if (pMB->mvs[i].y > max) max = pMB->mvs[i].y;  
2473    
2474                                  if (pMB->mvs[i].x < min) min = pMB->mvs[i].x;  /* Terminate if MinSAD <= T_2
2475                                  if (pMB->mvs[i].y < min) min = pMB->mvs[i].y;     Terminate if MV[t] == MV[t-1] and MinSAD[t] <= MinSAD[t-1]
2476                          }  */
2477                  }  
2478            if (iMinSAD < 512 / 4) {        /* T_2 == 512/4 hardcoded */
2479                    if (MotionFlags & PMV_QUICKSTOP8)
2480                            goto EPZS8_Terminate_without_Refine;
2481                    if (MotionFlags & PMV_EARLYSTOP8)
2482                            goto EPZS8_Terminate_with_Refine;
2483          }          }
2484    
2485          min = -min;  /************ (Diamond Search)  **************/
         max += 1;  
         if (min > max) max = min;  
         if (pParam->m_quarterpel) max *= 2;  
2486    
2487          for (i = 1; (max > 32 << (i - 1)); i++);          backupMV = *currMV;                     /* save best prediction, actually only for EXTSEARCH */
         return i;  
 }  
2488    
2489  static void          if (!(MotionFlags & PMV_HALFPELDIAMOND8))
2490  CheckGMC(int x, int y, const int dir, int * iDirection,                  iDiamondSize *= 2;
                 const MACROBLOCK * const pMBs, uint32_t * bestcount, VECTOR * GMC,  
                 const MBParam * const pParam)  
 {  
         uint32_t mx, my, a, count = 0;  
2491    
2492          for (my = 1; my < pParam->mb_height-1; my++)  /* default: use best prediction as starting point for one call of EPZS_MainSearch */
                 for (mx = 1; mx < pParam->mb_width-1; mx++) {  
                         VECTOR mv;  
                         const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];  
                         if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) continue;  
                         mv = pMB->mvs[0];  
                         a = ABS(mv.x - x) + ABS(mv.y - y);  
                         if (a < 6) count += 6 - a;  
                 }  
2493    
2494          if (count > *bestcount) {  // there is no EPZS^2 for inter4v at the moment
                 *bestcount = count;  
                 *iDirection = dir;  
                 GMC->x = x; GMC->y = y;  
         }  
 }  
2495    
2496      if (MotionFlags & PMV_USESQUARES8)
2497          MainSearchPtr = Square8_MainSearch;
2498      else
2499    
2500  static VECTOR          if (MotionFlags & PMV_ADVANCEDDIAMOND8)
2501  GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode)                  MainSearchPtr = AdvDiamond8_MainSearch;
2502  {          else
2503                    MainSearchPtr = Diamond8_MainSearch;
2504    
2505          uint32_t count, bestcount = 0;          iSAD =
2506          int x, y;                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
2507          VECTOR gmc = {0,0};                                                    currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
2508          int step, min_x, max_x, min_y, max_y;                                                    min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
2509          uint32_t mx, my;                                                    iQuant, 0);
         int iDirection, bDirection;  
2510    
         min_x = min_y = -32<<iFcode;  
         max_x = max_y = 32<<iFcode;  
2511    
2512  //step1: let's find a rough camera panning          if (iSAD < iMinSAD) {
2513          for (step = 32; step >= 2; step /= 2) {                  *currMV = newMV;
2514                  bestcount = 0;                  iMinSAD = iSAD;
2515                  for (y = min_y; y <= max_y; y += step)          }
                         for (x = min_x ; x <= max_x; x += step) {  
                                 count = 0;  
                                 //for all macroblocks  
                                 for (my = 1; my < pParam->mb_height-1; my++)  
                                         for (mx = 1; mx < pParam->mb_width-1; mx++) {  
                                                 const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];  
                                                 VECTOR mv;  
2516    
2517                                                  if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED)          if (MotionFlags & PMV_EXTSEARCH8) {
2518                                                          continue;  /* extended mode: search (up to) two more times: orignal prediction and (0,0) */
2519    
2520                    if (!(MVequal(pmv[0], backupMV))) {
2521                            iSAD =
2522                                    (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
2523                                                                      pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
2524                                                                      min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
2525                                                                      iDiamondSize, iFcode, iQuant, 0);
2526    
2527                                                  mv = pMB->mvs[0];                          if (iSAD < iMinSAD) {
2528                                                  if ( ABS(mv.x - x) <= step && ABS(mv.y - y) <= step )   /* GMC translation is always halfpel-res */                                  *currMV = newMV;
2529                                                          count++;                                  iMinSAD = iSAD;
2530                                          }                                          }
                                 if (count >= bestcount) { bestcount = count; gmc.x = x; gmc.y = y; }  
2531                          }                          }
                 min_x = gmc.x - step;  
                 max_x = gmc.x + step;  
                 min_y = gmc.y - step;  
                 max_y = gmc.y + step;  
2532    
2533          }                  if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2534                            iSAD =
2535                                    (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2536                                                                      iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
2537                                                                      max_dy, iEdgedWidth, iDiamondSize, iFcode,
2538                                                                      iQuant, 0);
2539    
2540          if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10)                          if (iSAD < iMinSAD) {
2541                  gmc.x = gmc.y = 0; //no camara pan, no GMC                                  *currMV = newMV;
2542                                    iMinSAD = iSAD;
2543                            }
2544                    }
2545            }
2546    
2547  // step2: let's refine camera panning using gradiend-descent approach.  /***************        Choose best MV found     **************/
 // TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond)  
         bestcount = 0;  
         CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam);  
         do {  
                 x = gmc.x; y = gmc.y;  
                 bDirection = iDirection; iDirection = 0;  
                 if (bDirection & 1) CheckGMC(x - 1, y, 1+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);  
                 if (bDirection & 2) CheckGMC(x + 1, y, 2+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);  
                 if (bDirection & 4) CheckGMC(x, y - 1, 1+2+4, &iDirection, pMBs, &bestcount, &gmc, pParam);  
                 if (bDirection & 8) CheckGMC(x, y + 1, 1+2+8, &iDirection, pMBs, &bestcount, &gmc, pParam);  
2548    
2549          } while (iDirection);    EPZS8_Terminate_with_Refine:
2550            if (MotionFlags & PMV_HALFPELREFINE8)   // perform final half-pel step
2551                    iMinSAD =
2552                            Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2553                                                            iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
2554                                                            iFcode, iQuant, iEdgedWidth);
2555    
2556          if (pParam->m_quarterpel) {    EPZS8_Terminate_without_Refine:
                 gmc.x *= 2;  
                 gmc.y *= 2;     /* we store the halfpel value as pseudo-qpel to make comparison easier */  
         }  
2557    
2558          return gmc;          currPMV->x = currMV->x - center_x;
2559            currPMV->y = currMV->y - center_y;
2560            return iMinSAD;
2561  }  }

Legend:
Removed from v.1.44.2.26  
changed lines
  Added in v.1.53

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