[svn] / branches / dev-api-3 / xvidcore / src / motion / motion_est.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/motion/motion_est.c

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

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

Legend:
Removed from v.118  
changed lines
  Added in v.702

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