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

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

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

revision 232, Fri Jun 21 16:12:48 2002 UTC revision 444, Sat Sep 7 13:41:13 2002 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *  Modifications:   *  XVID MPEG-4 VIDEO CODEC
4     *  - Motion Estimation module -
5   *   *
6   *      01.05.2002      updated MotionEstimationBVOP   *  Copyright(C) 2002 Christoph Lampert <gruel@web.de>
7   *      25.04.2002 partial prevMB conversion   *  Copyright(C) 2002 Michael Militzer <michael@xvid.org>
8   *  22.04.2002 remove some compile warning by chenm001 <chenm001@163.com>   *  Copyright(C) 2002 Edouard Gomez <ed.gomez@wanadoo.fr>
  *  14.04.2002 added MotionEstimationBVOP()  
  *  02.04.2002 add EPZS(^2) as ME algorithm, use PMV_USESQUARES to choose between  
  *             EPZS and EPZS^2  
  *  08.02.2002 split up PMVfast into three routines: PMVFast, PMVFast_MainLoop  
  *             PMVFast_Refine to support multiple searches with different start points  
  *  07.01.2002 uv-block-based interpolation  
  *  06.01.2002 INTER/INTRA-decision is now done before any SEARCH8 (speedup)  
  *             changed INTER_BIAS to 150 (as suggested by suxen_drol)  
  *             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.  
9   *   *
10   *  Michael Militzer <isibaar@videocoding.de>   *  This program is an implementation of a part of one or more MPEG-4
11     *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
12     *  to use this software module in hardware or software products are
13     *  advised that its use may infringe existing patents or copyrights, and
14     *  any such use would be at such party's own risk.  The original
15     *  developer of this software module and his/her company, and subsequent
16     *  editors and their companies, will have no liability for use of this
17     *  software or modifications or derivatives thereof.
18   *   *
19   **************************************************************************/   *  This program is free software; you can redistribute it and/or modify
20     *  it under the terms of the GNU General Public License as published by
21     *  the Free Software Foundation; either version 2 of the License, or
22     *  (at your option) any later version.
23     *
24     *  This program is distributed in the hope that it will be useful,
25     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27     *  GNU General Public License for more details.
28     *
29     *  You should have received a copy of the GNU General Public License
30     *  along with this program; if not, write to the Free Software
31     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
32     *
33     *************************************************************************/
34    
35  #include <assert.h>  #include <assert.h>
36  #include <stdio.h>  #include <stdio.h>
# Line 47  Line 44 
44  #include "motion.h"  #include "motion.h"
45  #include "sad.h"  #include "sad.h"
46    
 // very large value  
 #define MV_MAX_ERROR    (4096 * 256)  
   
 // stop search if sdelta < THRESHOLD  
 #define MV16_THRESHOLD  192  
 #define MV8_THRESHOLD   56  
   
 #define NEIGH_MOVE_THRESH 0  
 // how much a block's MV must differ from his neighbour  
 // to be search for INTER4V. The more, the faster...  
   
 /* sad16(0,0) bias; mpeg4 spec suggests nb/2+1 */  
 /* nb  = vop pixels * 2^(bpp-8) */  
 #define MV16_00_BIAS    (128+1)  
 #define MV8_00_BIAS     (0)  
   
 /* INTER bias for INTER/INTRA decision; mpeg4 spec suggests 2*nb */  
 #define MV16_INTER_BIAS 512  
   
 /* Parameters which control inter/inter4v decision */  
 #define IMV16X16                        5  
   
 /* vector map (vlc delta size) smoother parameters */  
 #define NEIGH_TEND_16X16        2  
 #define NEIGH_TEND_8X8          2  
   
 // fast ((A)/2)*2  
 #define EVEN(A)         (((A)<0?(A)+1:(A)) & ~1)  
   
 #define MVzero(A) ( ((A).x)==(0) && ((A).y)==(0) )  
 #define MVequal(A,B) ( ((A).x)==((B).x) && ((A).y)==((B).y) )  
   
 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 uint32_t iQuant,  
                                                 const uint32_t iFcode,  
                                                 const MBParam * const pParam,  
                                                 const MACROBLOCK * const pMBs,  
                                                 const MACROBLOCK * const prevMBs,  
                                                 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 uint32_t iQuant,  
                                          const uint32_t iFcode,  
                                          const MBParam * const pParam,  
                                          const MACROBLOCK * const pMBs,  
                                          const MACROBLOCK * const prevMBs,  
                                          VECTOR * const currMV,  
                                          VECTOR * const currPMV);  
   
   
 int32_t PMVfastSearch8(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 int start_x,  
                                            const int start_y,  
                                            const uint32_t MotionFlags,  
                                            const uint32_t iQuant,  
                                            const uint32_t iFcode,  
                                            const MBParam * const pParam,  
                                            const MACROBLOCK * const pMBs,  
                                            const MACROBLOCK * const prevMBs,  
                                            VECTOR * const currMV,  
                                            VECTOR * const currPMV);  
   
 int32_t EPZSSearch8(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 int start_x,  
                                         const int start_y,  
                                         const uint32_t MotionFlags,  
                                         const uint32_t iQuant,  
                                         const uint32_t iFcode,  
                                         const MBParam * const pParam,  
                                         const MACROBLOCK * const pMBs,  
                                         const MACROBLOCK * const prevMBs,  
                                         VECTOR * const currMV,  
                                         VECTOR * const currPMV);  
   
47    
 typedef int32_t(MainSearch16Func) (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);  
   
 typedef MainSearch16Func *MainSearch16FuncPtr;  
   
   
 typedef int32_t(MainSearch8Func) (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);  
   
 typedef MainSearch8Func *MainSearch8FuncPtr;  
48    
49  static int32_t lambda_vec16[32] =       /* rounded values for lambda param for weight of motion bits as in modified H.26L */  static int32_t lambda_vec16[32] =       /* rounded values for lambda param for weight of motion bits as in modified H.26L */
50  { 0, (int) (1.00235 + 0.5), (int) (1.15582 + 0.5), (int) (1.31976 + 0.5),  { 0, (int) (1.00235 + 0.5), (int) (1.15582 + 0.5), (int) (1.31976 + 0.5),
# Line 274  Line 122 
122                                                                                                     mv_bits(dy, iFcode));                                                                                                     mv_bits(dy, iFcode));
123  }  }
124    
   
   
   
   
 #ifndef SEARCH16  
 #define SEARCH16        PMVfastSearch16  
 //#define SEARCH16  FullSearch16  
 //#define SEARCH16  EPZSSearch16  
 #endif  
   
 #ifndef SEARCH8  
 #define SEARCH8         PMVfastSearch8  
 //#define SEARCH8   EPZSSearch8  
 #endif  
   
125  bool  bool
126  MotionEstimation(MBParam * const pParam,  MotionEstimation(MBParam * const pParam,
127                                   FRAMEINFO * const current,                                   FRAMEINFO * const current,
# Line 305  Line 138 
138          const IMAGE *const pCurrent = &current->image;          const IMAGE *const pCurrent = &current->image;
139          const IMAGE *const pRef = &reference->image;          const IMAGE *const pRef = &reference->image;
140    
141          const VECTOR zeroMV = { 0, 0 };          static const VECTOR zeroMV = { 0, 0 };
142            VECTOR predMV;
143    
144          int32_t x, y;          int32_t x, y;
145          int32_t iIntra = 0;          int32_t iIntra = 0;
# Line 314  Line 148 
148          if (sadInit)          if (sadInit)
149                  (*sadInit) ();                  (*sadInit) ();
150    
151          for (y = 0; y < iHcount; y++)          for (y = 0; y < iHcount; y++)   {
152                  for (x = 0; x < iWcount; x++) {                  for (x = 0; x < iWcount; x++) {
153    
154                          MACROBLOCK *const pMB = &pMBs[x + y * iWcount];                          MACROBLOCK *const pMB = &pMBs[x + y * iWcount];
155    
156                            if (pMB->mode == MODE_NOT_CODED)
157                                    continue;
158    
159                            predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
160    
161                          pMB->sad16 =                          pMB->sad16 =
162                                  SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,                                  SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent,
163                                                   y, current->motion_flags, current->quant,                                                   x, y, predMV.x, predMV.y, predMV.x, predMV.y,
164                                                     current->motion_flags, current->quant,
165                                                   current->fcode, pParam, pMBs, prevMBs, &pMB->mv16,                                                   current->fcode, pParam, pMBs, prevMBs, &pMB->mv16,
166                                                   &pMB->pmvs[0]);                                                   &pMB->pmvs[0]);
167    
# Line 352  Line 193 
193                                           pMB->dquant == NO_CHANGE)) {                                           pMB->dquant == NO_CHANGE)) {
194                                          int32_t sad8 = IMV16X16 * current->quant;                                          int32_t sad8 = IMV16X16 * current->quant;
195    
196                                          if (sad8 < pMB->sad16)                                          if (sad8 < pMB->sad16) {
   
197                                                  sad8 += pMB->sad8[0] =                                                  sad8 += pMB->sad8[0] =
198                                                          SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,                                                          SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
199                                                                          pCurrent, 2 * x, 2 * y, pMB->mv16.x,                                                                          pCurrent, 2 * x, 2 * y,
200                                                                          pMB->mv16.y, current->motion_flags,                                                                          pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y,
201                                                                            current->motion_flags,
202                                                                          current->quant, current->fcode, pParam,                                                                          current->quant, current->fcode, pParam,
203                                                                          pMBs, prevMBs, &pMB->mvs[0],                                                                          pMBs, prevMBs, &pMB->mvs[0],
204                                                                          &pMB->pmvs[0]);                                                                          &pMB->pmvs[0]);
205                                            }
206                                            if (sad8 < pMB->sad16) {
207    
208                                          if (sad8 < pMB->sad16)                                                  predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 1);
209                                                  sad8 += pMB->sad8[1] =                                                  sad8 += pMB->sad8[1] =
210                                                          SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,                                                          SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
211                                                                          pCurrent, 2 * x + 1, 2 * y, pMB->mv16.x,                                                                          pCurrent, 2 * x + 1, 2 * y,
212                                                                          pMB->mv16.y, current->motion_flags,                                                                          pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y,
213                                                                            current->motion_flags,
214                                                                          current->quant, current->fcode, pParam,                                                                          current->quant, current->fcode, pParam,
215                                                                          pMBs, prevMBs, &pMB->mvs[1],                                                                          pMBs, prevMBs, &pMB->mvs[1],
216                                                                          &pMB->pmvs[1]);                                                                          &pMB->pmvs[1]);
217                                            }
218                                          if (sad8 < pMB->sad16)                                          if (sad8 < pMB->sad16) {
219                                                    predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 2);
220                                                  sad8 += pMB->sad8[2] =                                                  sad8 += pMB->sad8[2] =
221                                                          SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,                                                          SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
222                                                                          pCurrent, 2 * x, 2 * y + 1, pMB->mv16.x,                                                                          pCurrent, 2 * x, 2 * y + 1,
223                                                                          pMB->mv16.y, current->motion_flags,                                                                          pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y,
224                                                                            current->motion_flags,
225                                                                          current->quant, current->fcode, pParam,                                                                          current->quant, current->fcode, pParam,
226                                                                          pMBs, prevMBs, &pMB->mvs[2],                                                                          pMBs, prevMBs, &pMB->mvs[2],
227                                                                          &pMB->pmvs[2]);                                                                          &pMB->pmvs[2]);
228                                            }
229                                          if (sad8 < pMB->sad16)                                          if (sad8 < pMB->sad16) {
230                                                    predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 3);
231                                                  sad8 += pMB->sad8[3] =                                                  sad8 += pMB->sad8[3] =
232                                                          SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,                                                          SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y,
233                                                                          pCurrent, 2 * x + 1, 2 * y + 1,                                                                          pCurrent, 2 * x + 1, 2 * y + 1,
234                                                                          pMB->mv16.x, pMB->mv16.y,                                                                          pMB->mv16.x, pMB->mv16.y, predMV.x, predMV.y,
235                                                                          current->motion_flags, current->quant,                                                                          current->motion_flags,
236                                                                          current->fcode, pParam, pMBs, prevMBs,                                                                          current->quant, current->fcode, pParam,
237                                                                          &pMB->mvs[3], &pMB->pmvs[3]);                                                                          pMBs, prevMBs,
238                                                                            &pMB->mvs[3],
239                                                                            &pMB->pmvs[3]);
240                                            }
241    
242                                          /* decide: MODE_INTER or MODE_INTER4V                                          /* decide: MODE_INTER or MODE_INTER4V
243                                             mpeg4:   if (sad8 < pMB->sad16 - nb/2+1) use_inter4v                                             mpeg4:   if (sad8 < pMB->sad16 - nb/2+1) use_inter4v
# Line 409  Line 259 
259                          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mv16;                          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mv16;
260                          pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] =                          pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] =
261                                  pMB->sad16;                                  pMB->sad16;
   
262                  }                  }
263                            }
264    
265          return 0;          return 0;
266  }  }
267    
268    
269  #define CHECK_MV16_ZERO {\  #define CHECK_MV16_ZERO {\
270    if ( (0 <= max_dx) && (0 >= min_dx) \    if ( (0 <= max_dx) && (0 >= min_dx) \
271      && (0 <= max_dy) && (0 >= min_dy) ) \      && (0 <= max_dy) && (0 >= min_dy) ) \
272    { \    { \
273      iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0, 0 , iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); \      iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0, 0 , iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); \
274      iSAD += calc_delta_16(-pmv[0].x, -pmv[0].y, (uint8_t)iFcode, iQuant);\      iSAD += calc_delta_16(-center_x, -center_y, (uint8_t)iFcode, iQuant);\
275      if (iSAD < iMinSAD) \      if (iSAD < iMinSAD) \
276      {  iMinSAD=iSAD; currMV->x=0; currMV->y=0; }  }     \      {  iMinSAD=iSAD; currMV->x=0; currMV->y=0; }  }     \
277  }  }
278    
279  #define NOCHECK_MV16_CANDIDATE(X,Y) { \  #define NOCHECK_MV16_CANDIDATE(X,Y) { \
280      iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \      iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
281      iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\      iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\
282      if (iSAD < iMinSAD) \      if (iSAD < iMinSAD) \
283      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \
284  }  }
# Line 436  Line 288 
288      && ((Y) <= max_dy) && ((Y) >= min_dy) ) \      && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
289    { \    { \
290      iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \      iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
291      iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\      iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\
292      if (iSAD < iMinSAD) \      if (iSAD < iMinSAD) \
293      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \
294  }  }
# Line 446  Line 298 
298      && ((Y) <= max_dy) && ((Y) >= min_dy) ) \      && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
299    { \    { \
300      iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \      iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
301      iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\      iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\
302      if (iSAD < iMinSAD) \      if (iSAD < iMinSAD) \
303      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \
304  }  }
# Line 456  Line 308 
308      && ((Y) <= max_dy) && ((Y) >= min_dy) ) \      && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
309    { \    { \
310      iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \      iSAD = sad16( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, X, Y, iEdgedWidth),iEdgedWidth, iMinSAD); \
311      iSAD += calc_delta_16((X) - pmv[0].x, (Y) - pmv[0].y, (uint8_t)iFcode, iQuant);\      iSAD += calc_delta_16((X) - center_x, (Y) - center_y, (uint8_t)iFcode, iQuant);\
312      if (iSAD < iMinSAD) \      if (iSAD < iMinSAD) \
313      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \
314  }  }
# Line 464  Line 316 
316    
317  #define CHECK_MV8_ZERO {\  #define CHECK_MV8_ZERO {\
318    iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, 0, 0 , iEdgedWidth), iEdgedWidth); \    iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, 0, 0 , iEdgedWidth), iEdgedWidth); \
319    iSAD += calc_delta_8(-pmv[0].x, -pmv[0].y, (uint8_t)iFcode, iQuant);\    iSAD += calc_delta_8(-center_x, -center_y, (uint8_t)iFcode, iQuant);\
320    if (iSAD < iMinSAD) \    if (iSAD < iMinSAD) \
321    { iMinSAD=iSAD; currMV->x=0; currMV->y=0; } \    { iMinSAD=iSAD; currMV->x=0; currMV->y=0; } \
322  }  }
# Line 472  Line 324 
324  #define NOCHECK_MV8_CANDIDATE(X,Y) \  #define NOCHECK_MV8_CANDIDATE(X,Y) \
325    { \    { \
326      iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \      iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
327      iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\      iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\
328      if (iSAD < iMinSAD) \      if (iSAD < iMinSAD) \
329      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } \
330  }  }
# Line 482  Line 334 
334      && ((Y) <= max_dy) && ((Y) >= min_dy) ) \      && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
335    { \    { \
336      iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \      iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
337      iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\      iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\
338      if (iSAD < iMinSAD) \      if (iSAD < iMinSAD) \
339      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); } } \
340  }  }
# Line 492  Line 344 
344      && ((Y) <= max_dy) && ((Y) >= min_dy) ) \      && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
345    { \    { \
346      iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \      iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
347      iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\      iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\
348      if (iSAD < iMinSAD) \      if (iSAD < iMinSAD) \
349      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); } } \
350  }  }
# Line 502  Line 354 
354      && ((Y) <= max_dy) && ((Y) >= min_dy) ) \      && ((Y) <= max_dy) && ((Y) >= min_dy) ) \
355    { \    { \
356      iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \      iSAD = sad8( cur, get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 8, (X), (Y), iEdgedWidth),iEdgedWidth); \
357      iSAD += calc_delta_8((X)-pmv[0].x, (Y)-pmv[0].y, (uint8_t)iFcode, iQuant);\      iSAD += calc_delta_8((X)-center_x, (Y)-center_y, (uint8_t)iFcode, iQuant);\
358      if (iSAD < iMinSAD) \      if (iSAD < iMinSAD) \
359      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \      {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iDirection=(D); iFound=0; } } \
360  }  }
# Line 528  Line 380 
380          const int32_t iEdgedWidth = pParam->edged_width;          const int32_t iEdgedWidth = pParam->edged_width;
381          const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth;          const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth;
382          int32_t iSAD;          int32_t iSAD;
383          int32_t pred_x,pred_y;          VECTOR pred;
384    
385          get_pmv(pMBs, x, y, pParam->mb_width, 0, &pred_x, &pred_y);  
386            pred = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
387    
388          iSAD = sad16( cur,          iSAD = sad16( cur,
389                  get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0,0, iEdgedWidth),                  get_ref(pRef, pRefH, pRefV, pRefHV, x, y, 16, 0,0, iEdgedWidth),
# Line 540  Line 393 
393    
394          currMV->x = 0;          currMV->x = 0;
395          currMV->y = 0;          currMV->y = 0;
396          currPMV->x = -pred_x;          currPMV->x = -pred.x;
397          currPMV->y = -pred_y;          currPMV->y = -pred.y;
398    
399          return iSAD;          return iSAD;
400    
# Line 556  Line 409 
409                                           const uint8_t * const cur,                                           const uint8_t * const cur,
410                                           const int x,                                           const int x,
411                                           const int y,                                           const int y,
412                                           int32_t startx,                                     const int start_x,
413                                           int32_t starty,                                     const int start_y,
414                                           int32_t iMinSAD,                                     int iMinSAD,
415                                           VECTOR * const currMV,                                           VECTOR * const currMV,
416                                           const VECTOR * const pmv,                                     const int center_x,
417                                       const int center_y,
418                                           const int32_t min_dx,                                           const int32_t min_dx,
419                                           const int32_t max_dx,                                           const int32_t max_dx,
420                                           const int32_t min_dy,                                           const int32_t min_dy,
# Line 574  Line 428 
428  /* Do a diamond search around given starting point, return SAD of best */  /* Do a diamond search around given starting point, return SAD of best */
429    
430          int32_t iDirection = 0;          int32_t iDirection = 0;
431            int32_t iDirectionBackup;
432          int32_t iSAD;          int32_t iSAD;
433          VECTOR backupMV;          VECTOR backupMV;
434    
435          backupMV.x = startx;          backupMV.x = start_x;
436          backupMV.y = starty;          backupMV.y = start_y;
437    
438  /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */  /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
439    
# Line 587  Line 442 
442          CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);          CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
443          CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);          CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
444    
445          if (iDirection)          if (iDirection) {
446                  while (!iFound) {                  while (!iFound) {
447                          iFound = 1;                          iFound = 1;
448                          backupMV = *currMV;                          backupMV = *currMV;
449                            iDirectionBackup = iDirection;
450    
451                          if (iDirection != 2)                          if (iDirectionBackup != 2)
452                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
453                                                                                     backupMV.y, 1);                                                                                     backupMV.y, 1);
454                          if (iDirection != 1)                          if (iDirectionBackup != 1)
455                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
456                                                                                     backupMV.y, 2);                                                                                     backupMV.y, 2);
457                          if (iDirection != 4)                          if (iDirectionBackup != 4)
458                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x,
459                                                                                     backupMV.y - iDiamondSize, 3);                                                                                     backupMV.y - iDiamondSize, 3);
460                          if (iDirection != 3)                          if (iDirectionBackup != 3)
461                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x,
462                                                                                     backupMV.y + iDiamondSize, 4);                                                                                     backupMV.y + iDiamondSize, 4);
463                    }
464          } else {          } else {
465                  currMV->x = startx;                  currMV->x = start_x;
466                  currMV->y = starty;                  currMV->y = start_y;
467          }          }
468          return iMinSAD;          return iMinSAD;
469  }  }
# Line 619  Line 476 
476                                          const uint8_t * const cur,                                          const uint8_t * const cur,
477                                          const int x,                                          const int x,
478                                          const int y,                                          const int y,
479                                          int32_t startx,                                     const int start_x,
480                                          int32_t starty,                                     const int start_y,
481                                          int32_t iMinSAD,                                     int iMinSAD,
482                                          VECTOR * const currMV,                                          VECTOR * const currMV,
483                                          const VECTOR * const pmv,                                     const int center_x,
484                                       const int center_y,
485                                          const int32_t min_dx,                                          const int32_t min_dx,
486                                          const int32_t max_dx,                                          const int32_t max_dx,
487                                          const int32_t min_dy,                                          const int32_t min_dy,
# Line 640  Line 498 
498          int32_t iSAD;          int32_t iSAD;
499          VECTOR backupMV;          VECTOR backupMV;
500    
501          backupMV.x = startx;          backupMV.x = start_x;
502          backupMV.y = starty;          backupMV.y = start_y;
503    
504  /* It's one search with full square pattern, and new parts for all following diamonds */  /* It's one search with full square pattern, and new parts for all following diamonds */
505    
# Line 666  Line 524 
524                                                           backupMV.y + iDiamondSize, 8);                                                           backupMV.y + iDiamondSize, 8);
525    
526    
527          if (iDirection)          if (iDirection) {
528                  while (!iFound) {                  while (!iFound) {
529                          iFound = 1;                          iFound = 1;
530                          backupMV = *currMV;                          backupMV = *currMV;
# Line 675  Line 533 
533                          case 1:                          case 1:
534                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
535                                                                                     backupMV.y, 1);                                                                                     backupMV.y, 1);
536                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
537                                                                                   backupMV.y - iDiamondSize, 5);                                                                                   backupMV.y - iDiamondSize, 5);
538                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
539                                                                                   backupMV.y - iDiamondSize, 7);                                                                                   backupMV.y - iDiamondSize, 7);
540                                  break;                                  break;
541                          case 2:                          case 2:
542                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
543                                                                                   2);                                                                                   2);
544                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
545                                                                                   backupMV.y + iDiamondSize, 6);                                                                                   backupMV.y + iDiamondSize, 6);
546                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
547                                                                                   backupMV.y + iDiamondSize, 8);                                                                                   backupMV.y + iDiamondSize, 8);
548                                  break;                                  break;
549    
550                          case 3:                          case 3:
551                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
552                                                                                   4);                                                                                   4);
553                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
554                                                                                   backupMV.y - iDiamondSize, 7);                                                                                   backupMV.y - iDiamondSize, 7);
555                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
556                                                                                   backupMV.y + iDiamondSize, 8);                                                                                   backupMV.y + iDiamondSize, 8);
557                                  break;                                  break;
558    
559                          case 4:                          case 4:
560                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
561                                                                                   3);                                                                                   3);
562                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
563                                                                                   backupMV.y - iDiamondSize, 5);                                                                                   backupMV.y - iDiamondSize, 5);
564                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
565                                                                                   backupMV.y + iDiamondSize, 6);                                                                                   backupMV.y + iDiamondSize, 6);
566                                  break;                                  break;
567    
568                          case 5:                          case 5:
569                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
570                                                                                   1);                                                                                   1);
571                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
572                                                                                   3);                                                                                   3);
573                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
574                                                                                   backupMV.y - iDiamondSize, 5);                                                                                   backupMV.y - iDiamondSize, 5);
575                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
576                                                                                   backupMV.y + iDiamondSize, 6);                                                                                   backupMV.y + iDiamondSize, 6);
577                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
578                                                                                   backupMV.y - iDiamondSize, 7);                                                                                   backupMV.y - iDiamondSize, 7);
579                                  break;                                  break;
580    
581                          case 6:                          case 6:
582                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
583                                                                                   2);                                                                                   2);
584                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
585                                                                                   3);                                                                                   3);
586    
587                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
588                                                                                   backupMV.y - iDiamondSize, 5);                                                                                   backupMV.y - iDiamondSize, 5);
589                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
590                                                                                   backupMV.y + iDiamondSize, 6);                                                                                   backupMV.y + iDiamondSize, 6);
591                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
592                                                                                   backupMV.y + iDiamondSize, 8);                                                                                   backupMV.y + iDiamondSize, 8);
593    
594                                  break;                                  break;
# Line 738  Line 596 
596                          case 7:                          case 7:
597                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
598                                                                                     backupMV.y, 1);                                                                                     backupMV.y, 1);
599                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
600                                                                                   4);                                                                                   4);
601                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
602                                                                                   backupMV.y - iDiamondSize, 5);                                                                                   backupMV.y - iDiamondSize, 5);
603                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
604                                                                                   backupMV.y - iDiamondSize, 7);                                                                                   backupMV.y - iDiamondSize, 7);
605                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
606                                                                                   backupMV.y + iDiamondSize, 8);                                                                                   backupMV.y + iDiamondSize, 8);
607                                  break;                                  break;
608    
609                          case 8:                          case 8:
610                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
611                                                                                   2);                                                                                   2);
612                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
613                                                                                   4);                                                                                   4);
614                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
615                                                                                   backupMV.y + iDiamondSize, 6);                                                                                   backupMV.y + iDiamondSize, 6);
616                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
617                                                                                   backupMV.y - iDiamondSize, 7);                                                                                   backupMV.y - iDiamondSize, 7);
618                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
619                                                                                   backupMV.y + iDiamondSize, 8);                                                                                   backupMV.y + iDiamondSize, 8);
620                                  break;                                  break;
621                          default:                          default:
622                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
623                                                                                   1);                                                                                   1);
624                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
625                                                                                   2);                                                                                   2);
626                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
627                                                                                   3);                                                                                   3);
628                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
629                                                                                   4);                                                                                   4);
630    
631                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
632                                                                                   backupMV.y - iDiamondSize, 5);                                                                                   backupMV.y - iDiamondSize, 5);
633                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x - iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
634                                                                                   backupMV.y + iDiamondSize, 6);                                                                                   backupMV.y + iDiamondSize, 6);
635                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
636                                                                                   backupMV.y - iDiamondSize, 7);                                                                                   backupMV.y - iDiamondSize, 7);
637                                  CHECK_MV16_CANDIDATE_DIR(backupMV.x + iDiamondSize,                                  CHECK_MV16_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
638                                                                                   backupMV.y + iDiamondSize, 8);                                                                                   backupMV.y + iDiamondSize, 8);
639                                  break;                                  break;
640                          }                          }
641                    }
642          } else {          } else {
643                  currMV->x = startx;                  currMV->x = start_x;
644                  currMV->y = starty;                  currMV->y = start_y;
645          }          }
646          return iMinSAD;          return iMinSAD;
647  }  }
# Line 796  Line 655 
655                                    const uint8_t * const cur,                                    const uint8_t * const cur,
656                                    const int x,                                    const int x,
657                                    const int y,                                    const int y,
658                                    int32_t startx,                                     const int start_x,
659                                    int32_t starty,                                     const int start_y,
660                                    int32_t iMinSAD,                                     int iMinSAD,
661                                    VECTOR * const currMV,                                    VECTOR * const currMV,
662                                    const VECTOR * const pmv,                                     const int center_x,
663                                       const int center_y,
664                                    const int32_t min_dx,                                    const int32_t min_dx,
665                                    const int32_t max_dx,                                    const int32_t max_dx,
666                                    const int32_t min_dy,                                    const int32_t min_dy,
# Line 815  Line 675 
675          int32_t dx, dy;          int32_t dx, dy;
676          VECTOR backupMV;          VECTOR backupMV;
677    
678          backupMV.x = startx;          backupMV.x = start_x;
679          backupMV.y = starty;          backupMV.y = start_y;
680    
681          for (dx = min_dx; dx <= max_dx; dx += iDiamondSize)          for (dx = min_dx; dx <= max_dx; dx += iDiamondSize)
682                  for (dy = min_dy; dy <= max_dy; dy += iDiamondSize)                  for (dy = min_dy; dy <= max_dy; dy += iDiamondSize)
# Line 833  Line 693 
693                                                  const uint8_t * const cur,                                                  const uint8_t * const cur,
694                                                  const int x,                                                  const int x,
695                                                  const int y,                                                  const int y,
696                                                  int32_t startx,                                             int start_x,
697                                                  int32_t starty,                                             int start_y,
698                                                  int32_t iMinSAD,                                             int iMinSAD,
699                                                  VECTOR * const currMV,                                                  VECTOR * const currMV,
700                                                  const VECTOR * const pmv,                                             const int center_x,
701                                               const int center_y,
702                                                  const int32_t min_dx,                                                  const int32_t min_dx,
703                                                  const int32_t max_dx,                                                  const int32_t max_dx,
704                                                  const int32_t min_dy,                                                  const int32_t min_dy,
# Line 854  Line 715 
715  /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */  /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
716    
717          if (iDirection) {          if (iDirection) {
718                  CHECK_MV16_CANDIDATE(startx - iDiamondSize, starty);                  CHECK_MV16_CANDIDATE(start_x - iDiamondSize, start_y);
719                  CHECK_MV16_CANDIDATE(startx + iDiamondSize, starty);                  CHECK_MV16_CANDIDATE(start_x + iDiamondSize, start_y);
720                  CHECK_MV16_CANDIDATE(startx, starty - iDiamondSize);                  CHECK_MV16_CANDIDATE(start_x, start_y - iDiamondSize);
721                  CHECK_MV16_CANDIDATE(startx, starty + iDiamondSize);                  CHECK_MV16_CANDIDATE(start_x, start_y + iDiamondSize);
722          } else {          } else {
723                  int bDirection = 1 + 2 + 4 + 8;                  int bDirection = 1 + 2 + 4 + 8;
724    
725                  do {                  do {
726                          iDirection = 0;                          iDirection = 0;
727                          if (bDirection & 1)     //we only want to check left if we came from the right (our last motion was to the left, up-left or down-left)                          if (bDirection & 1)     //we only want to check left if we came from the right (our last motion was to the left, up-left or down-left)
728                                  CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, starty, 1);                                  CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
729    
730                          if (bDirection & 2)                          if (bDirection & 2)
731                                  CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, starty, 2);                                  CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
732    
733                          if (bDirection & 4)                          if (bDirection & 4)
734                                  CHECK_MV16_CANDIDATE_DIR(startx, starty - iDiamondSize, 4);                                  CHECK_MV16_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
735    
736                          if (bDirection & 8)                          if (bDirection & 8)
737                                  CHECK_MV16_CANDIDATE_DIR(startx, starty + iDiamondSize, 8);                                  CHECK_MV16_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
738    
739                          /* now we're doing diagonal checks near our candidate */                          /* now we're doing diagonal checks near our candidate */
740    
# Line 881  Line 742 
742                          {                          {
743                                  bDirection = iDirection;                                  bDirection = iDirection;
744                                  iDirection = 0;                                  iDirection = 0;
745                                  startx = currMV->x;                                  start_x = currMV->x;
746                                  starty = currMV->y;                                  start_y = currMV->y;
747                                  if (bDirection & 3)     //our candidate is left or right                                  if (bDirection & 3)     //our candidate is left or right
748                                  {                                  {
749                                          CHECK_MV16_CANDIDATE_DIR(startx, starty + iDiamondSize, 8);                                          CHECK_MV16_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
750                                          CHECK_MV16_CANDIDATE_DIR(startx, starty - iDiamondSize, 4);                                          CHECK_MV16_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
751                                  } else                  // what remains here is up or down                                  } else                  // what remains here is up or down
752                                  {                                  {
753                                          CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize, starty, 2);                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
754                                          CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize, starty, 1);                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
755                                  }                                  }
756    
757                                  if (iDirection) {                                  if (iDirection) {
758                                          bDirection += iDirection;                                          bDirection += iDirection;
759                                          startx = currMV->x;                                          start_x = currMV->x;
760                                          starty = currMV->y;                                          start_y = currMV->y;
761                                  }                                  }
762                          } else                          //about to quit, eh? not so fast....                          } else                          //about to quit, eh? not so fast....
763                          {                          {
764                                  switch (bDirection) {                                  switch (bDirection) {
765                                  case 2:                                  case 2:
766                                          CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
767                                                                                           starty - iDiamondSize, 2 + 4);                                                                                           start_y - iDiamondSize, 2 + 4);
768                                          CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
769                                                                                           starty + iDiamondSize, 2 + 8);                                                                                           start_y + iDiamondSize, 2 + 8);
770                                          break;                                          break;
771                                  case 1:                                  case 1:
772                                          CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,  
773                                                                                           starty - iDiamondSize, 1 + 4);                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
774                                          CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,                                                                                           start_y - iDiamondSize, 1 + 4);
775                                                                                           starty + iDiamondSize, 1 + 8);                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
776                                                                                             start_y + iDiamondSize, 1 + 8);
777                                          break;                                          break;
778                                  case 2 + 4:                                  case 2 + 4:
779                                          CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
780                                                                                           starty - iDiamondSize, 1 + 4);                                                                                           start_y - iDiamondSize, 1 + 4);
781                                          CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
782                                                                                           starty - iDiamondSize, 2 + 4);                                                                                           start_y - iDiamondSize, 2 + 4);
783                                          CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
784                                                                                           starty + iDiamondSize, 2 + 8);                                                                                           start_y + iDiamondSize, 2 + 8);
785                                          break;                                          break;
786                                  case 4:                                  case 4:
787                                          CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
788                                                                                           starty - iDiamondSize, 2 + 4);                                                                                           start_y - iDiamondSize, 2 + 4);
789                                          CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
790                                                                                           starty - iDiamondSize, 1 + 4);                                                                                           start_y - iDiamondSize, 1 + 4);
791                                          break;                                          break;
792                                  case 8:                                  case 8:
793                                          CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
794                                                                                           starty + iDiamondSize, 2 + 8);                                                                                           start_y + iDiamondSize, 2 + 8);
795                                          CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
796                                                                                           starty + iDiamondSize, 1 + 8);                                                                                           start_y + iDiamondSize, 1 + 8);
797                                          break;                                          break;
798                                  case 1 + 4:                                  case 1 + 4:
799                                          CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
800                                                                                           starty + iDiamondSize, 1 + 8);                                                                                           start_y + iDiamondSize, 1 + 8);
801                                          CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
802                                                                                           starty - iDiamondSize, 1 + 4);                                                                                           start_y - iDiamondSize, 1 + 4);
803                                          CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
804                                                                                           starty - iDiamondSize, 2 + 4);                                                                                           start_y - iDiamondSize, 2 + 4);
805                                          break;                                          break;
806                                  case 2 + 8:                                  case 2 + 8:
807                                          CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
808                                                                                           starty - iDiamondSize, 1 + 4);                                                                                           start_y - iDiamondSize, 1 + 4);
809                                          CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
810                                                                                           starty + iDiamondSize, 1 + 8);                                                                                           start_y + iDiamondSize, 1 + 8);
811                                          CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
812                                                                                           starty + iDiamondSize, 2 + 8);                                                                                           start_y + iDiamondSize, 2 + 8);
813                                          break;                                          break;
814                                  case 1 + 8:                                  case 1 + 8:
815                                          CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
816                                                                                           starty - iDiamondSize, 2 + 4);                                                                                           start_y - iDiamondSize, 2 + 4);
817                                          CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
818                                                                                           starty + iDiamondSize, 2 + 8);                                                                                           start_y + iDiamondSize, 2 + 8);
819                                          CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
820                                                                                           starty + iDiamondSize, 1 + 8);                                                                                           start_y + iDiamondSize, 1 + 8);
821                                          break;                                          break;
822                                  default:                //1+2+4+8 == we didn't find anything at all                                  default:                //1+2+4+8 == we didn't find anything at all
823                                          CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
824                                                                                           starty - iDiamondSize, 1 + 4);                                                                                           start_y - iDiamondSize, 1 + 4);
825                                          CHECK_MV16_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x - iDiamondSize,
826                                                                                           starty + iDiamondSize, 1 + 8);                                                                                           start_y + iDiamondSize, 1 + 8);
827                                          CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
828                                                                                           starty - iDiamondSize, 2 + 4);                                                                                           start_y - iDiamondSize, 2 + 4);
829                                          CHECK_MV16_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV16_CANDIDATE_DIR(start_x + iDiamondSize,
830                                                                                           starty + iDiamondSize, 2 + 8);                                                                                           start_y + iDiamondSize, 2 + 8);
831                                          break;                                          break;
832                                  }                                  }
833                                  if (!iDirection)                                  if (!iDirection)
834                                          break;          //ok, the end. really                                          break;          //ok, the end. really
835                                  else {                                  else {
836                                          bDirection = iDirection;                                          bDirection = iDirection;
837                                          startx = currMV->x;                                          start_x = currMV->x;
838                                          starty = currMV->y;                                          start_y = currMV->y;
839                                  }                                  }
840                          }                          }
841                  }                  }
# Line 982  Line 844 
844          return iMinSAD;          return iMinSAD;
845  }  }
846    
847    #define CHECK_MV16_F_INTERPOL(X,Y) { \
848      if ( ((X) <= f_max_dx) && ((X) >= f_min_dx) \
849        && ((Y) <= f_max_dy) && ((Y) >= f_min_dy) ) \
850      { \
851        iSAD = sad16bi( cur, \
852                            get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, X, Y, iEdgedWidth),       \
853                            get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, b_currMV->x, b_currMV->y, iEdgedWidth),   \
854                            iEdgedWidth); \
855        iSAD += calc_delta_16((X) - f_center_x, (Y) - f_center_y, (uint8_t)f_iFcode, iQuant);\
856        iSAD += calc_delta_16(b_currMV->x - b_center_x, b_currMV->y - b_center_y, (uint8_t)b_iFcode, iQuant);\
857        if (iSAD < iMinSAD) \
858        {  iMinSAD=iSAD; f_currMV->x=(X); f_currMV->y=(Y); } } \
859    }
860    
861    #define CHECK_MV16_F_INTERPOL_FOUND(X,Y) { \
862      if ( ((X) <= f_max_dx) && ((X) >= f_min_dx) \
863        && ((Y) <= f_max_dy) && ((Y) >= f_min_dy) ) \
864      { \
865        iSAD = sad16bi( cur, \
866                            get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, X, Y, iEdgedWidth),       \
867                            get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, b_currMV->x, b_currMV->y, iEdgedWidth),   \
868                            iEdgedWidth); \
869        iSAD += calc_delta_16((X) - f_center_x, (Y) - f_center_y, (uint8_t)f_iFcode, iQuant);\
870        iSAD += calc_delta_16(b_currMV->x - b_center_x, b_currMV->y - b_center_y, (uint8_t)b_iFcode, iQuant);\
871        if (iSAD < iMinSAD) \
872        {  iMinSAD=iSAD; f_currMV->x=(X); f_currMV->y=(Y); iFound=0;} } \
873    }
874    
875    #define CHECK_MV16_B_INTERPOL(X,Y) { \
876      if ( ((X) <= b_max_dx) && ((X) >= b_min_dx) \
877        && ((Y) <= b_max_dy) && ((Y) >= b_min_dy) ) \
878      { \
879        iSAD = sad16bi( cur, \
880                            get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, f_currMV->x, f_currMV->y, iEdgedWidth),   \
881                            get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, X, Y, iEdgedWidth),       \
882                            iEdgedWidth); \
883        iSAD += calc_delta_16(f_currMV->x - f_center_x, f_currMV->y - f_center_y, (uint8_t)f_iFcode, iQuant);\
884        iSAD += calc_delta_16((X) - b_center_x, (Y) - b_center_y, (uint8_t)b_iFcode, iQuant);\
885        if (iSAD < iMinSAD) \
886        {  iMinSAD=iSAD; b_currMV->x=(X); b_currMV->y=(Y); } } \
887    }
888    
889    #define CHECK_MV16_B_INTERPOL_FOUND(X,Y) { \
890      if ( ((X) <= b_max_dx) && ((X) >= b_min_dx) \
891        && ((Y) <= b_max_dy) && ((Y) >= b_min_dy) ) \
892      { \
893        iSAD = sad16bi( cur, \
894                            get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, x, y, 16, f_currMV->x, f_currMV->y, iEdgedWidth),   \
895                            get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, x, y, 16, X, Y, iEdgedWidth),       \
896                            iEdgedWidth); \
897        iSAD += calc_delta_16(f_currMV->x - f_center_x, f_currMV->y - f_center_y, (uint8_t)f_iFcode, iQuant);\
898        iSAD += calc_delta_16((X) - b_center_x, (Y) - b_center_y, (uint8_t)b_iFcode, iQuant);\
899        if (iSAD < iMinSAD) \
900        {  iMinSAD=iSAD; b_currMV->x=(X); b_currMV->y=(Y); iFound=0;} } \
901    }
902    
903    int32_t
904    Diamond16_InterpolMainSearch(
905                                            const uint8_t * const f_pRef,
906                                             const uint8_t * const f_pRefH,
907                                             const uint8_t * const f_pRefV,
908                                             const uint8_t * const f_pRefHV,
909    
910                                             const uint8_t * const cur,
911    
912                                            const uint8_t * const b_pRef,
913                                             const uint8_t * const b_pRefH,
914                                             const uint8_t * const b_pRefV,
915                                             const uint8_t * const b_pRefHV,
916    
917                                             const int x,
918                                             const int y,
919    
920                                       const int f_start_x,
921                                       const int f_start_y,
922                                       const int b_start_x,
923                                       const int b_start_y,
924    
925                                       int iMinSAD,
926                                       VECTOR * const f_currMV,
927                                       VECTOR * const b_currMV,
928    
929                                       const int f_center_x,
930                                       const int f_center_y,
931                                       const int b_center_x,
932                                       const int b_center_y,
933    
934                                        const int32_t f_min_dx,
935                                            const int32_t f_max_dx,
936                                            const int32_t f_min_dy,
937                                            const int32_t f_max_dy,
938    
939                                        const int32_t b_min_dx,
940                                            const int32_t b_max_dx,
941                                            const int32_t b_min_dy,
942                                            const int32_t b_max_dy,
943    
944                                            const int32_t iEdgedWidth,
945                                            const int32_t iDiamondSize,
946    
947                                            const int32_t f_iFcode,
948                                            const int32_t b_iFcode,
949    
950                                            const int32_t iQuant,
951                                            int iFound)
952    {
953    /* Do a diamond search around given starting point, return SAD of best */
954    
955            int32_t iSAD;
956    
957            VECTOR f_backupMV;
958            VECTOR b_backupMV;
959    
960            f_currMV->x = f_start_x;
961            f_currMV->y = f_start_y;
962            b_currMV->x = b_start_x;
963            b_currMV->y = b_start_y;
964    
965            do
966            {
967                    iFound = 1;
968    
969                    f_backupMV = *f_currMV;
970    
971                    CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x - iDiamondSize, f_backupMV.y);
972                    CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x + iDiamondSize, f_backupMV.y);
973                    CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x, f_backupMV.y - iDiamondSize);
974                    CHECK_MV16_F_INTERPOL_FOUND(f_backupMV.x, f_backupMV.y + iDiamondSize);
975    
976                    b_backupMV = *b_currMV;
977    
978                    CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x - iDiamondSize, b_backupMV.y);
979                    CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x + iDiamondSize, b_backupMV.y);
980                    CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x, b_backupMV.y - iDiamondSize);
981                    CHECK_MV16_B_INTERPOL_FOUND(b_backupMV.x, b_backupMV.y + iDiamondSize);
982    
983            } while (!iFound);
984    
985            return iMinSAD;
986    }
987    
988    /* Sorry, these MACROS really got too large... I'll turn them into function soon! */
989    
990    #define CHECK_MV16_DIRECT_FOUND(X,Y) \
991            if ( (X)>=(-32) && (X)<=(31) && ((Y)>=-32) && ((Y)<=31) ) \
992            { int k;\
993            VECTOR mvs,b_mvs;       \
994            iSAD = 0;\
995            for (k = 0; k < 4; k++) {       \
996                                            mvs.x = (int32_t) ((TRB * directmv[k].x) / TRD + (X));          \
997                        b_mvs.x = (int32_t) (((X) == 0)                                                     \
998                                                                                    ? ((TRB - TRD) * directmv[k].x) / TRD   \
999                                                : mvs.x - directmv[k].x);                           \
1000                                                                                                                                                                    \
1001                        mvs.y = (int32_t) ((TRB * directmv[k].y) / TRD + (Y));              \
1002                            b_mvs.y = (int32_t) (((Y) == 0)                                                         \
1003                                                                                    ? ((TRB - TRD) * directmv[k].y) / TRD   \
1004                                                : mvs.y - directmv[k].y);                           \
1005                                                                                                                                                                    \
1006      if ( (mvs.x <= max_dx) && (mvs.x >= min_dx) \
1007        && (mvs.y <= max_dy) && (mvs.y >= min_dy)  \
1008            && (b_mvs.x <= max_dx) && (b_mvs.x >= min_dx)  \
1009        && (b_mvs.y <= max_dy) && (b_mvs.y >= min_dy) ) { \
1010                iSAD += sad8bi( cur + 8*(k&1) + 8*(k>>1)*iEdgedWidth,                                                                                                       \
1011                            get_ref(f_pRef, f_pRefH, f_pRefV, f_pRefHV, 2*x+(k&1), 2*y+(k>>1), 8, \
1012                                            mvs.x, mvs.y, iEdgedWidth),                                                             \
1013                            get_ref(b_pRef, b_pRefH, b_pRefV, b_pRefHV, 2*x+(k&1), 2*y+(k>>1), 8, \
1014                                            b_mvs.x, b_mvs.y, iEdgedWidth),                                                         \
1015                            iEdgedWidth); \
1016                    }       \
1017            else    \
1018                    iSAD = 65535;   \
1019            } \
1020            iSAD += calc_delta_16((X),(Y), 1, iQuant);\
1021            if (iSAD < iMinSAD) \
1022                {  iMinSAD=iSAD; currMV->x=(X); currMV->y=(Y); iFound=0; } \
1023    }
1024    
1025    
1026    
1027    int32_t
1028    Diamond16_DirectMainSearch(
1029                                            const uint8_t * const f_pRef,
1030                                            const uint8_t * const f_pRefH,
1031                                            const uint8_t * const f_pRefV,
1032                                            const uint8_t * const f_pRefHV,
1033    
1034                                            const uint8_t * const cur,
1035    
1036                                            const uint8_t * const b_pRef,
1037                                            const uint8_t * const b_pRefH,
1038                                            const uint8_t * const b_pRefV,
1039                                            const uint8_t * const b_pRefHV,
1040    
1041                                            const int x,
1042                                            const int y,
1043    
1044                                            const int TRB,
1045                                            const int TRD,
1046    
1047                                        const int start_x,
1048                                        const int start_y,
1049    
1050                                        int iMinSAD,
1051                                        VECTOR * const currMV,
1052                                            const VECTOR * const directmv,
1053    
1054                                        const int32_t min_dx,
1055                                            const int32_t max_dx,
1056                                            const int32_t min_dy,
1057                                            const int32_t max_dy,
1058    
1059                                            const int32_t iEdgedWidth,
1060                                            const int32_t iDiamondSize,
1061    
1062                                            const int32_t iQuant,
1063                                            int iFound)
1064    {
1065    /* Do a diamond search around given starting point, return SAD of best */
1066    
1067            int32_t iSAD;
1068    
1069            VECTOR backupMV;
1070    
1071            currMV->x = start_x;
1072            currMV->y = start_y;
1073    
1074    /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
1075    
1076            do
1077            {
1078                    iFound = 1;
1079    
1080                    backupMV = *currMV;
1081    
1082                    CHECK_MV16_DIRECT_FOUND(backupMV.x - iDiamondSize, backupMV.y);
1083                    CHECK_MV16_DIRECT_FOUND(backupMV.x + iDiamondSize, backupMV.y);
1084                    CHECK_MV16_DIRECT_FOUND(backupMV.x, backupMV.y - iDiamondSize);
1085                    CHECK_MV16_DIRECT_FOUND(backupMV.x, backupMV.y + iDiamondSize);
1086    
1087            } while (!iFound);
1088    
1089            return iMinSAD;
1090    }
1091    
1092    
1093  int32_t  int32_t
1094  AdvDiamond8_MainSearch(const uint8_t * const pRef,  AdvDiamond8_MainSearch(const uint8_t * const pRef,
1095                                             const uint8_t * const pRefH,                                             const uint8_t * const pRefH,
# Line 990  Line 1098 
1098                                             const uint8_t * const cur,                                             const uint8_t * const cur,
1099                                             const int x,                                             const int x,
1100                                             const int y,                                             const int y,
1101                                             int32_t startx,                                             int start_x,
1102                                             int32_t starty,                                             int start_y,
1103                                             int32_t iMinSAD,                                             int iMinSAD,
1104                                             VECTOR * const currMV,                                             VECTOR * const currMV,
1105                                             const VECTOR * const pmv,                                             const int center_x,
1106                                               const int center_y,
1107                                             const int32_t min_dx,                                             const int32_t min_dx,
1108                                             const int32_t max_dx,                                             const int32_t max_dx,
1109                                             const int32_t min_dy,                                             const int32_t min_dy,
# Line 1011  Line 1120 
1120  /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */  /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
1121    
1122          if (iDirection) {          if (iDirection) {
1123                  CHECK_MV8_CANDIDATE(startx - iDiamondSize, starty);                  CHECK_MV8_CANDIDATE(start_x - iDiamondSize, start_y);
1124                  CHECK_MV8_CANDIDATE(startx + iDiamondSize, starty);                  CHECK_MV8_CANDIDATE(start_x + iDiamondSize, start_y);
1125                  CHECK_MV8_CANDIDATE(startx, starty - iDiamondSize);                  CHECK_MV8_CANDIDATE(start_x, start_y - iDiamondSize);
1126                  CHECK_MV8_CANDIDATE(startx, starty + iDiamondSize);                  CHECK_MV8_CANDIDATE(start_x, start_y + iDiamondSize);
1127          } else {          } else {
1128                  int bDirection = 1 + 2 + 4 + 8;                  int bDirection = 1 + 2 + 4 + 8;
1129    
1130                  do {                  do {
1131                          iDirection = 0;                          iDirection = 0;
1132                          if (bDirection & 1)     //we only want to check left if we came from the right (our last motion was to the left, up-left or down-left)                          if (bDirection & 1)     //we only want to check left if we came from the right (our last motion was to the left, up-left or down-left)
1133                                  CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, starty, 1);                                  CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
1134    
1135                          if (bDirection & 2)                          if (bDirection & 2)
1136                                  CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, starty, 2);                                  CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
1137    
1138                          if (bDirection & 4)                          if (bDirection & 4)
1139                                  CHECK_MV8_CANDIDATE_DIR(startx, starty - iDiamondSize, 4);                                  CHECK_MV8_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
1140    
1141                          if (bDirection & 8)                          if (bDirection & 8)
1142                                  CHECK_MV8_CANDIDATE_DIR(startx, starty + iDiamondSize, 8);                                  CHECK_MV8_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
1143    
1144                          /* now we're doing diagonal checks near our candidate */                          /* now we're doing diagonal checks near our candidate */
1145    
# Line 1038  Line 1147 
1147                          {                          {
1148                                  bDirection = iDirection;                                  bDirection = iDirection;
1149                                  iDirection = 0;                                  iDirection = 0;
1150                                  startx = currMV->x;                                  start_x = currMV->x;
1151                                  starty = currMV->y;                                  start_y = currMV->y;
1152                                  if (bDirection & 3)     //our candidate is left or right                                  if (bDirection & 3)     //our candidate is left or right
1153                                  {                                  {
1154                                          CHECK_MV8_CANDIDATE_DIR(startx, starty + iDiamondSize, 8);                                          CHECK_MV8_CANDIDATE_DIR(start_x, start_y + iDiamondSize, 8);
1155                                          CHECK_MV8_CANDIDATE_DIR(startx, starty - iDiamondSize, 4);                                          CHECK_MV8_CANDIDATE_DIR(start_x, start_y - iDiamondSize, 4);
1156                                  } else                  // what remains here is up or down                                  } else                  // what remains here is up or down
1157                                  {                                  {
1158                                          CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize, starty, 2);                                          CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize, start_y, 2);
1159                                          CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize, starty, 1);                                          CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize, start_y, 1);
1160                                  }                                  }
1161    
1162                                  if (iDirection) {                                  if (iDirection) {
1163                                          bDirection += iDirection;                                          bDirection += iDirection;
1164                                          startx = currMV->x;                                          start_x = currMV->x;
1165                                          starty = currMV->y;                                          start_y = currMV->y;
1166                                  }                                  }
1167                          } else                          //about to quit, eh? not so fast....                          } else                          //about to quit, eh? not so fast....
1168                          {                          {
1169                                  switch (bDirection) {                                  switch (bDirection) {
1170                                  case 2:                                  case 2:
1171                                          CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1172                                                                                          starty - iDiamondSize, 2 + 4);                                                                                          start_y - iDiamondSize, 2 + 4);
1173                                          CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1174                                                                                          starty + iDiamondSize, 2 + 8);                                                                                          start_y + iDiamondSize, 2 + 8);
1175                                          break;                                          break;
1176                                  case 1:                                  case 1:
1177                                          CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1178                                                                                          starty - iDiamondSize, 1 + 4);                                                                                          start_y - iDiamondSize, 1 + 4);
1179                                          CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1180                                                                                          starty + iDiamondSize, 1 + 8);                                                                                          start_y + iDiamondSize, 1 + 8);
1181                                          break;                                          break;
1182                                  case 2 + 4:                                  case 2 + 4:
1183                                          CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1184                                                                                          starty - iDiamondSize, 1 + 4);                                                                                          start_y - iDiamondSize, 1 + 4);
1185                                          CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1186                                                                                          starty - iDiamondSize, 2 + 4);                                                                                          start_y - iDiamondSize, 2 + 4);
1187                                          CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1188                                                                                          starty + iDiamondSize, 2 + 8);                                                                                          start_y + iDiamondSize, 2 + 8);
1189                                          break;                                          break;
1190                                  case 4:                                  case 4:
1191                                          CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1192                                                                                          starty - iDiamondSize, 2 + 4);                                                                                          start_y - iDiamondSize, 2 + 4);
1193                                          CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1194                                                                                          starty - iDiamondSize, 1 + 4);                                                                                          start_y - iDiamondSize, 1 + 4);
1195                                          break;                                          break;
1196                                  case 8:                                  case 8:
1197                                          CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1198                                                                                          starty + iDiamondSize, 2 + 8);                                                                                          start_y + iDiamondSize, 2 + 8);
1199                                          CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1200                                                                                          starty + iDiamondSize, 1 + 8);                                                                                          start_y + iDiamondSize, 1 + 8);
1201                                          break;                                          break;
1202                                  case 1 + 4:                                  case 1 + 4:
1203                                          CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1204                                                                                          starty + iDiamondSize, 1 + 8);                                                                                          start_y + iDiamondSize, 1 + 8);
1205                                          CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1206                                                                                          starty - iDiamondSize, 1 + 4);                                                                                          start_y - iDiamondSize, 1 + 4);
1207                                          CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1208                                                                                          starty - iDiamondSize, 2 + 4);                                                                                          start_y - iDiamondSize, 2 + 4);
1209                                          break;                                          break;
1210                                  case 2 + 8:                                  case 2 + 8:
1211                                          CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1212                                                                                          starty - iDiamondSize, 1 + 4);                                                                                          start_y - iDiamondSize, 1 + 4);
1213                                          CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1214                                                                                          starty + iDiamondSize, 1 + 8);                                                                                          start_y + iDiamondSize, 1 + 8);
1215                                          CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1216                                                                                          starty + iDiamondSize, 2 + 8);                                                                                          start_y + iDiamondSize, 2 + 8);
1217                                          break;                                          break;
1218                                  case 1 + 8:                                  case 1 + 8:
1219                                          CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1220                                                                                          starty - iDiamondSize, 2 + 4);                                                                                          start_y - iDiamondSize, 2 + 4);
1221                                          CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1222                                                                                          starty + iDiamondSize, 2 + 8);                                                                                          start_y + iDiamondSize, 2 + 8);
1223                                          CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1224                                                                                          starty + iDiamondSize, 1 + 8);                                                                                          start_y + iDiamondSize, 1 + 8);
1225                                          break;                                          break;
1226                                  default:                //1+2+4+8 == we didn't find anything at all                                  default:                //1+2+4+8 == we didn't find anything at all
1227                                          CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1228                                                                                          starty - iDiamondSize, 1 + 4);                                                                                          start_y - iDiamondSize, 1 + 4);
1229                                          CHECK_MV8_CANDIDATE_DIR(startx - iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x - iDiamondSize,
1230                                                                                          starty + iDiamondSize, 1 + 8);                                                                                          start_y + iDiamondSize, 1 + 8);
1231                                          CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1232                                                                                          starty - iDiamondSize, 2 + 4);                                                                                          start_y - iDiamondSize, 2 + 4);
1233                                          CHECK_MV8_CANDIDATE_DIR(startx + iDiamondSize,                                          CHECK_MV8_CANDIDATE_DIR(start_x + iDiamondSize,
1234                                                                                          starty + iDiamondSize, 2 + 8);                                                                                          start_y + iDiamondSize, 2 + 8);
1235                                          break;                                          break;
1236                                  }                                  }
1237                                  if (!(iDirection))                                  if (!(iDirection))
1238                                          break;          //ok, the end. really                                          break;          //ok, the end. really
1239                                  else {                                  else {
1240                                          bDirection = iDirection;                                          bDirection = iDirection;
1241                                          startx = currMV->x;                                          start_x = currMV->x;
1242                                          starty = currMV->y;                                          start_y = currMV->y;
1243                                  }                                  }
1244                          }                          }
1245                  }                  }
# Line 1148  Line 1257 
1257                                   const uint8_t * const cur,                                   const uint8_t * const cur,
1258                                   const int x,                                   const int x,
1259                                   const int y,                                   const int y,
1260                                   int32_t startx,                             const int start_x,
1261                                   int32_t starty,                             const int start_y,
1262                                   int32_t iMinSAD,                             int iMinSAD,
1263                                   VECTOR * const currMV,                                   VECTOR * const currMV,
1264                                   const VECTOR * const pmv,                             const int center_x,
1265                               const int center_y,
1266                                   const int32_t min_dx,                                   const int32_t min_dx,
1267                                   const int32_t max_dx,                                   const int32_t max_dx,
1268                                   const int32_t min_dy,                                   const int32_t min_dy,
# Line 1167  Line 1277 
1277          int32_t dx, dy;          int32_t dx, dy;
1278          VECTOR backupMV;          VECTOR backupMV;
1279    
1280          backupMV.x = startx;          backupMV.x = start_x;
1281          backupMV.y = starty;          backupMV.y = start_y;
1282    
1283          for (dx = min_dx; dx <= max_dx; dx += iDiamondSize)          for (dx = min_dx; dx <= max_dx; dx += iDiamondSize)
1284                  for (dy = min_dy; dy <= max_dy; dy += iDiamondSize)                  for (dy = min_dy; dy <= max_dy; dy += iDiamondSize)
# Line 1177  Line 1287 
1287          return iMinSAD;          return iMinSAD;
1288  }  }
1289    
1290    Halfpel8_RefineFuncPtr Halfpel8_Refine;
1291    
1292  int32_t  int32_t
1293  Halfpel16_Refine(const uint8_t * const pRef,  Halfpel16_Refine(const uint8_t * const pRef,
# Line 1189  Line 1299 
1299                                   const int y,                                   const int y,
1300                                   VECTOR * const currMV,                                   VECTOR * const currMV,
1301                                   int32_t iMinSAD,                                   int32_t iMinSAD,
1302                                   const VECTOR * const pmv,                             const int center_x,
1303                               const int center_y,
1304                                   const int32_t min_dx,                                   const int32_t min_dx,
1305                                   const int32_t max_dx,                                   const int32_t max_dx,
1306                                   const int32_t min_dy,                                   const int32_t min_dy,
# Line 1218  Line 1329 
1329  #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16)  #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16)
1330    
1331    
1332    
1333  int32_t  int32_t
1334  PMVfastSearch16(const uint8_t * const pRef,  PMVfastSearch16(const uint8_t * const pRef,
1335                                  const uint8_t * const pRefH,                                  const uint8_t * const pRefH,
# Line 1226  Line 1338 
1338                                  const IMAGE * const pCur,                                  const IMAGE * const pCur,
1339                                  const int x,                                  const int x,
1340                                  const int y,                                  const int y,
1341                                    const int start_x,      /* start is searched first, so it should contain the most */
1342                                    const int start_y,  /* likely motion vector for this block */
1343                                    const int center_x,     /* center is from where length of MVs is measured */
1344                                    const int center_y,
1345                                  const uint32_t MotionFlags,                                  const uint32_t MotionFlags,
1346                                  const uint32_t iQuant,                                  const uint32_t iQuant,
1347                                  const uint32_t iFcode,                                  const uint32_t iFcode,
# Line 1259  Line 1375 
1375    
1376          MainSearch16FuncPtr MainSearchPtr;          MainSearch16FuncPtr MainSearchPtr;
1377    
 //  const MACROBLOCK * const pMB = pMBs + x + y * iWcount;  
1378          const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;          const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;
1379    
1380          static int32_t threshA, threshB;          int32_t threshA, threshB;
1381          int32_t bPredEq;          int32_t bPredEq;
1382          int32_t iMinSAD, iSAD;          int32_t iMinSAD, iSAD;
1383    
# Line 1280  Line 1395 
1395          }          }
1396    
1397          /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */          /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
1398          bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad);          bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad);
1399    
1400          if ((x == 0) && (y == 0)) {          if ((x == 0) && (y == 0)) {
1401                  threshA = 512;                  threshA = 512;
1402                  threshB = 1024;                  threshB = 1024;
   
1403          } else {          } else {
1404                  threshA = psad[0];                  threshA = psad[0];
1405                  threshB = threshA + 256;                  threshB = threshA + 256;
# Line 1306  Line 1420 
1420     If SAD<=256 goto Step 10.     If SAD<=256 goto Step 10.
1421  */  */
1422    
1423          *currMV = pmv[0];                       /* current best := prediction */          currMV->x = start_x;
1424            currMV->y = start_y;
1425    
1426          if (!(MotionFlags & PMV_HALFPEL16)) {   /* This should NOT be necessary! */          if (!(MotionFlags & PMV_HALFPEL16)) {   /* This should NOT be necessary! */
1427                  currMV->x = EVEN(currMV->x);                  currMV->x = EVEN(currMV->x);
1428                  currMV->y = EVEN(currMV->y);                  currMV->y = EVEN(currMV->y);
# Line 1330  Line 1446 
1446                            get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV,                            get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV,
1447                                                   iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);                                                   iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);
1448          iMinSAD +=          iMinSAD +=
1449                  calc_delta_16(currMV->x - pmv[0].x, currMV->y - pmv[0].y,                  calc_delta_16(currMV->x - center_x, currMV->y - center_y,
1450                                            (uint8_t) iFcode, iQuant);                                            (uint8_t) iFcode, iQuant);
1451    
1452          if ((iMinSAD < 256) ||          if ((iMinSAD < 256) ||
1453                  ((MVequal(*currMV, prevMB->mvs[0])) &&                  ((MVequal(*currMV, prevMB->mvs[0])) &&
1454                   ((uint32_t) iMinSAD < prevMB->sad16))) {                   ((int32_t) iMinSAD < prevMB->sad16))) {
1455                  if (iMinSAD < 2 * iQuant)       // high chances for SKIP-mode                  if (iMinSAD < 2 * iQuant)       // high chances for SKIP-mode
1456                  {                  {
1457                          if (!MVzero(*currMV)) {                          if (!MVzero(*currMV)) {
# Line 1439  Line 1555 
1555    
1556          if ((iMinSAD <= threshA) ||          if ((iMinSAD <= threshA) ||
1557                  (MVequal(*currMV, prevMB->mvs[0]) &&                  (MVequal(*currMV, prevMB->mvs[0]) &&
1558                   ((uint32_t) iMinSAD < prevMB->sad16))) {                   ((int32_t) iMinSAD < prevMB->sad16))) {
1559                  if (MotionFlags & PMV_QUICKSTOP16)                  if (MotionFlags & PMV_QUICKSTOP16)
1560                          goto PMVfast16_Terminate_without_Refine;                          goto PMVfast16_Terminate_without_Refine;
1561                  if (MotionFlags & PMV_EARLYSTOP16)                  if (MotionFlags & PMV_EARLYSTOP16)
# Line 1466  Line 1582 
1582    
1583          backupMV = *currMV;                     /* save best prediction, actually only for EXTSEARCH */          backupMV = *currMV;                     /* save best prediction, actually only for EXTSEARCH */
1584    
1585    
1586  /* default: use best prediction as starting point for one call of PMVfast_MainSearch */  /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
1587          iSAD =          iSAD =
1588                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
1589                                                    currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx,                                                    currMV->x, currMV->y, iMinSAD, &newMV, center_x, center_y,
1590                                                      min_dx, max_dx,
1591                                                    min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,                                                    min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
1592                                                    iQuant, iFound);                                                    iQuant, iFound);
1593    
# Line 1484  Line 1602 
1602                  if (!(MVequal(pmv[0], backupMV))) {                  if (!(MVequal(pmv[0], backupMV))) {
1603                          iSAD =                          iSAD =
1604                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
1605                                                                    pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv,                                                                    center_x, center_y, iMinSAD, &newMV, center_x, center_y,
1606                                                                    min_dx, max_dx, min_dy, max_dy, iEdgedWidth,                                                                    min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
1607                                                                    iDiamondSize, iFcode, iQuant, iFound);                                                                    iDiamondSize, iFcode, iQuant, iFound);
1608    
# Line 1497  Line 1615 
1615                  if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {                  if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
1616                          iSAD =                          iSAD =
1617                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
1618                                                                    iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy,                                                                    iMinSAD, &newMV, center_x, center_y,
1619                                                                    max_dy, iEdgedWidth, iDiamondSize, iFcode,                                                                    min_dx, max_dx, min_dy, max_dy,
1620                                                                      iEdgedWidth, iDiamondSize, iFcode,
1621                                                                    iQuant, iFound);                                                                    iQuant, iFound);
1622    
1623                          if (iSAD < iMinSAD) {                          if (iSAD < iMinSAD) {
# Line 1516  Line 1635 
1635          if (MotionFlags & PMV_HALFPELREFINE16)  // perform final half-pel step          if (MotionFlags & PMV_HALFPELREFINE16)  // perform final half-pel step
1636                  iMinSAD =                  iMinSAD =
1637                          Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,                          Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
1638                                                           iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy,                                                           iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
1639                                                           iFcode, iQuant, iEdgedWidth);                                                           iFcode, iQuant, iEdgedWidth);
1640    
1641    PMVfast16_Terminate_without_Refine:    PMVfast16_Terminate_without_Refine:
1642          currPMV->x = currMV->x - pmv[0].x;          currPMV->x = currMV->x - center_x;
1643          currPMV->y = currMV->y - pmv[0].y;          currPMV->y = currMV->y - center_y;
1644          return iMinSAD;          return iMinSAD;
1645  }  }
1646    
# Line 1538  Line 1657 
1657                                          const uint8_t * const cur,                                          const uint8_t * const cur,
1658                                          const int x,                                          const int x,
1659                                          const int y,                                          const int y,
1660                                          int32_t startx,                                          int32_t start_x,
1661                                          int32_t starty,                                          int32_t start_y,
1662                                          int32_t iMinSAD,                                          int32_t iMinSAD,
1663                                          VECTOR * const currMV,                                          VECTOR * const currMV,
1664                                          const VECTOR * const pmv,                                     const int center_x,
1665                                       const int center_y,
1666                                          const int32_t min_dx,                                          const int32_t min_dx,
1667                                          const int32_t max_dx,                                          const int32_t max_dx,
1668                                          const int32_t min_dy,                                          const int32_t min_dy,
# Line 1556  Line 1676 
1676  /* Do a diamond search around given starting point, return SAD of best */  /* Do a diamond search around given starting point, return SAD of best */
1677    
1678          int32_t iDirection = 0;          int32_t iDirection = 0;
1679            int32_t iDirectionBackup;
1680          int32_t iSAD;          int32_t iSAD;
1681          VECTOR backupMV;          VECTOR backupMV;
1682    
1683          backupMV.x = startx;          backupMV.x = start_x;
1684          backupMV.y = starty;          backupMV.y = start_y;
1685    
1686  /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */  /* It's one search with full Diamond pattern, and only 3 of 4 for all following diamonds */
1687    
# Line 1569  Line 1690 
1690          CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);          CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
1691          CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);          CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
1692    
1693          if (iDirection)          if (iDirection) {
1694                  while (!iFound) {                  while (!iFound) {
1695                          iFound = 1;                          iFound = 1;
1696                          backupMV = *currMV;     // since iDirection!=0, this is well defined!                          backupMV = *currMV;     // since iDirection!=0, this is well defined!
1697                            iDirectionBackup = iDirection;
1698    
1699                          if (iDirection != 2)                          if (iDirectionBackup != 2)
1700                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1701                                                                                    backupMV.y, 1);                                                                                    backupMV.y, 1);
1702                          if (iDirection != 1)                          if (iDirectionBackup != 1)
1703                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1704                                                                                      backupMV.y, 2);
1705                            if (iDirectionBackup != 4)
1706                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x,
1707                                                                                      backupMV.y - iDiamondSize, 3);
1708                            if (iDirectionBackup != 3)
1709                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x,
1710                                                                                      backupMV.y + iDiamondSize, 4);
1711                    }
1712            } else {
1713                    currMV->x = start_x;
1714                    currMV->y = start_y;
1715            }
1716            return iMinSAD;
1717    }
1718    
1719    
1720    
1721    
1722    int32_t
1723    Square8_MainSearch(const uint8_t * const pRef,
1724                                            const uint8_t * const pRefH,
1725                                            const uint8_t * const pRefV,
1726                                            const uint8_t * const pRefHV,
1727                                            const uint8_t * const cur,
1728                                            const int x,
1729                                            const int y,
1730                                            int32_t start_x,
1731                                            int32_t start_y,
1732                                            int32_t iMinSAD,
1733                                            VECTOR * const currMV,
1734                                       const int center_x,
1735                                       const int center_y,
1736                                            const int32_t min_dx,
1737                                            const int32_t max_dx,
1738                                            const int32_t min_dy,
1739                                            const int32_t max_dy,
1740                                            const int32_t iEdgedWidth,
1741                                            const int32_t iDiamondSize,
1742                                            const int32_t iFcode,
1743                                            const int32_t iQuant,
1744                                            int iFound)
1745    {
1746    /* Do a square search around given starting point, return SAD of best */
1747    
1748            int32_t iDirection = 0;
1749            int32_t iSAD;
1750            VECTOR backupMV;
1751    
1752            backupMV.x = start_x;
1753            backupMV.y = start_y;
1754    
1755    /* It's one search with full square pattern, and new parts for all following diamonds */
1756    
1757    /*   new direction are extra, so 1-4 is normal diamond
1758          537
1759          1*2
1760          648
1761    */
1762    
1763            CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize, backupMV.y, 1);
1764            CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize, backupMV.y, 2);
1765            CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y - iDiamondSize, 3);
1766            CHECK_MV8_CANDIDATE_DIR(backupMV.x, backupMV.y + iDiamondSize, 4);
1767    
1768            CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize,
1769                                                             backupMV.y - iDiamondSize, 5);
1770            CHECK_MV8_CANDIDATE_DIR(backupMV.x - iDiamondSize,
1771                                                             backupMV.y + iDiamondSize, 6);
1772            CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize,
1773                                                             backupMV.y - iDiamondSize, 7);
1774            CHECK_MV8_CANDIDATE_DIR(backupMV.x + iDiamondSize,
1775                                                             backupMV.y + iDiamondSize, 8);
1776    
1777    
1778            if (iDirection) {
1779                    while (!iFound) {
1780                            iFound = 1;
1781                            backupMV = *currMV;
1782    
1783                            switch (iDirection) {
1784                            case 1:
1785                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1786                                                                                       backupMV.y, 1);
1787                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1788                                                                                     backupMV.y - iDiamondSize, 5);
1789                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1790                                                                                     backupMV.y - iDiamondSize, 7);
1791                                    break;
1792                            case 2:
1793                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1794                                                                                     2);
1795                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1796                                                                                     backupMV.y + iDiamondSize, 6);
1797                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1798                                                                                     backupMV.y + iDiamondSize, 8);
1799                                    break;
1800    
1801                            case 3:
1802                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1803                                                                                     4);
1804                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1805                                                                                     backupMV.y - iDiamondSize, 7);
1806                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1807                                                                                     backupMV.y + iDiamondSize, 8);
1808                                    break;
1809    
1810                            case 4:
1811                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1812                                                                                     3);
1813                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1814                                                                                     backupMV.y - iDiamondSize, 5);
1815                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1816                                                                                     backupMV.y + iDiamondSize, 6);
1817                                    break;
1818    
1819                            case 5:
1820                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
1821                                                                                     1);
1822                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1823                                                                                     3);
1824                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1825                                                                                     backupMV.y - iDiamondSize, 5);
1826                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1827                                                                                     backupMV.y + iDiamondSize, 6);
1828                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1829                                                                                     backupMV.y - iDiamondSize, 7);
1830                                    break;
1831    
1832                            case 6:
1833                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1834                                                                                     2);
1835                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1836                                                                                     3);
1837    
1838                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1839                                                                                     backupMV.y - iDiamondSize, 5);
1840                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1841                                                                                     backupMV.y + iDiamondSize, 6);
1842                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1843                                                                                     backupMV.y + iDiamondSize, 8);
1844    
1845                                    break;
1846    
1847                            case 7:
1848                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1849                                                                                       backupMV.y, 1);
1850                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1851                                                                                     4);
1852                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1853                                                                                     backupMV.y - iDiamondSize, 5);
1854                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1855                                                                                     backupMV.y - iDiamondSize, 7);
1856                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1857                                                                                     backupMV.y + iDiamondSize, 8);
1858                                    break;
1859    
1860                            case 8:
1861                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1862                                                                                     2);
1863                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1864                                                                                     4);
1865                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1866                                                                                     backupMV.y + iDiamondSize, 6);
1867                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1868                                                                                     backupMV.y - iDiamondSize, 7);
1869                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1870                                                                                     backupMV.y + iDiamondSize, 8);
1871                                    break;
1872                            default:
1873                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize, backupMV.y,
1874                                                                                     1);
1875                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize, backupMV.y,
1876                                                                                     2);
1877                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y - iDiamondSize,
1878                                                                                     3);
1879                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x, backupMV.y + iDiamondSize,
1880                                                                                     4);
1881    
1882                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1883                                                                                     backupMV.y - iDiamondSize, 5);
1884                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x - iDiamondSize,
1885                                                                                     backupMV.y + iDiamondSize, 6);
1886                                    CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1887                                                                                     backupMV.y - iDiamondSize, 7);
1888                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x + iDiamondSize,
1889                                                                                    backupMV.y, 2);                                                                                   backupMV.y + iDiamondSize, 8);
1890                          if (iDirection != 4)                                  break;
1891                                  CHECK_MV8_CANDIDATE_FOUND(backupMV.x,                          }
1892                                                                                    backupMV.y - iDiamondSize, 3);                  }
                         if (iDirection != 3)  
                                 CHECK_MV8_CANDIDATE_FOUND(backupMV.x,  
                                                                                   backupMV.y + iDiamondSize, 4);  
1893          } else {          } else {
1894                  currMV->x = startx;                  currMV->x = start_x;
1895                  currMV->y = starty;                  currMV->y = start_y;
1896          }          }
1897          return iMinSAD;          return iMinSAD;
1898  }  }
1899    
1900    
1901    
1902    
1903    
1904  int32_t  int32_t
1905  Halfpel8_Refine(const uint8_t * const pRef,  Halfpel8_Refine_c(const uint8_t * const pRef,
1906                                  const uint8_t * const pRefH,                                  const uint8_t * const pRefH,
1907                                  const uint8_t * const pRefV,                                  const uint8_t * const pRefV,
1908                                  const uint8_t * const pRefHV,                                  const uint8_t * const pRefHV,
# Line 1603  Line 1911 
1911                                  const int y,                                  const int y,
1912                                  VECTOR * const currMV,                                  VECTOR * const currMV,
1913                                  int32_t iMinSAD,                                  int32_t iMinSAD,
1914                                  const VECTOR * const pmv,                             const int center_x,
1915                               const int center_y,
1916                                  const int32_t min_dx,                                  const int32_t min_dx,
1917                                  const int32_t max_dx,                                  const int32_t max_dx,
1918                                  const int32_t min_dy,                                  const int32_t min_dy,
# Line 1642  Line 1951 
1951                             const int y,                             const int y,
1952                             const int start_x,                             const int start_x,
1953                             const int start_y,                             const int start_y,
1954                                    const int center_x,
1955                                    const int center_y,
1956                             const uint32_t MotionFlags,                             const uint32_t MotionFlags,
1957                             const uint32_t iQuant,                             const uint32_t iQuant,
1958                             const uint32_t iFcode,                             const uint32_t iFcode,
# Line 1674  Line 1985 
1985  //  const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount;  //  const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount;
1986          const MACROBLOCK *const prevMB = prevMBs + (x >> 1) + (y >> 1) * iWcount;          const MACROBLOCK *const prevMB = prevMBs + (x >> 1) + (y >> 1) * iWcount;
1987    
1988          static int32_t threshA, threshB;           int32_t threshA, threshB;
1989          int32_t iFound, bPredEq;          int32_t iFound, bPredEq;
1990          int32_t iMinSAD, iSAD;          int32_t iMinSAD, iSAD;
1991    
# Line 1698  Line 2009 
2009          }          }
2010    
2011          /* because we might use IF (dx>max_dx) THEN dx=max_dx; */          /* because we might use IF (dx>max_dx) THEN dx=max_dx; */
2012          bPredEq =          bPredEq = get_pmvdata2(pMBs, iWcount, 0, (x >> 1), (y >> 1), iSubBlock, pmv, psad);
                 get_pmvdata(pMBs, (x >> 1), (y >> 1), iWcount, iSubBlock, pmv, psad);  
2013    
2014          if ((x == 0) && (y == 0)) {          if ((x == 0) && (y == 0)) {
2015                  threshA = 512 / 4;                  threshA = 512 / 4;
2016                  threshB = 1024 / 4;                  threshB = 1024 / 4;
2017    
2018          } else {          } else {
2019                  threshA = psad[0] / 4;  /* good estimate */                  threshA = psad[0] / 4;  /* good estimate? */
2020                  threshB = threshA + 256 / 4;                  threshB = threshA + 256 / 4;
2021                  if (threshA < 512 / 4)                  if (threshA < 512 / 4)
2022                          threshA = 512 / 4;                          threshA = 512 / 4;
# Line 1728  Line 2038 
2038    
2039  // Prepare for main loop  // Prepare for main loop
2040    
2041  //  if (MotionFlags & PMV_USESQUARES8)    if (MotionFlags & PMV_USESQUARES8)
2042  //      MainSearchPtr = Square8_MainSearch;        MainSearchPtr = Square8_MainSearch;
2043  //  else    else
2044    
2045          if (MotionFlags & PMV_ADVANCEDDIAMOND8)          if (MotionFlags & PMV_ADVANCEDDIAMOND8)
2046                  MainSearchPtr = AdvDiamond8_MainSearch;                  MainSearchPtr = AdvDiamond8_MainSearch;
# Line 1745  Line 2055 
2055                           get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV,                           get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV,
2056                                                  iEdgedWidth), iEdgedWidth);                                                  iEdgedWidth), iEdgedWidth);
2057          iMinSAD +=          iMinSAD +=
2058                  calc_delta_8(currMV->x - pmv[0].x, currMV->y - pmv[0].y,                  calc_delta_8(currMV->x - center_x, currMV->y - center_y,
2059                                           (uint8_t) iFcode, iQuant);                                           (uint8_t) iFcode, iQuant);
2060    
2061          if ((iMinSAD < 256 / 4) || ((MVequal(*currMV, prevMB->mvs[iSubBlock]))          if ((iMinSAD < 256 / 4) || ((MVequal(*currMV, prevMB->mvs[iSubBlock]))
2062                                                                  && ((uint32_t) iMinSAD <                                                                  && ((int32_t) iMinSAD <
2063                                                                          prevMB->sad8[iSubBlock]))) {                                                                          prevMB->sad8[iSubBlock]))) {
2064                  if (MotionFlags & PMV_QUICKSTOP16)                  if (MotionFlags & PMV_QUICKSTOP16)
2065                          goto PMVfast8_Terminate_without_Refine;                          goto PMVfast8_Terminate_without_Refine;
# Line 1788  Line 2098 
2098  // the median prediction might be even better than mv16  // the median prediction might be even better than mv16
2099    
2100          if (!MVequal(pmv[0], startMV))          if (!MVequal(pmv[0], startMV))
2101                  CHECK_MV8_CANDIDATE(pmv[0].x, pmv[0].y);                  CHECK_MV8_CANDIDATE(center_x, center_y);
2102    
2103  // (0,0) if needed  // (0,0) if needed
2104          if (!MVzero(pmv[0]))          if (!MVzero(pmv[0]))
# Line 1804  Line 2114 
2114    
2115          if ((iMinSAD <= threshA) ||          if ((iMinSAD <= threshA) ||
2116                  (MVequal(*currMV, prevMB->mvs[iSubBlock]) &&                  (MVequal(*currMV, prevMB->mvs[iSubBlock]) &&
2117                   ((uint32_t) iMinSAD < prevMB->sad8[iSubBlock]))) {                   ((int32_t) iMinSAD < prevMB->sad8[iSubBlock]))) {
2118                  if (MotionFlags & PMV_QUICKSTOP16)                  if (MotionFlags & PMV_QUICKSTOP16)
2119                          goto PMVfast8_Terminate_without_Refine;                          goto PMVfast8_Terminate_without_Refine;
2120                  if (MotionFlags & PMV_EARLYSTOP16)                  if (MotionFlags & PMV_EARLYSTOP16)
# Line 1863  Line 2173 
2173    
2174          if ((iMinSAD <= threshA) ||          if ((iMinSAD <= threshA) ||
2175                  (MVequal(*currMV, prevMB->mvs[iSubBlock]) &&                  (MVequal(*currMV, prevMB->mvs[iSubBlock]) &&
2176                   ((uint32_t) iMinSAD < prevMB->sad8[iSubBlock]))) {                   ((int32_t) iMinSAD < prevMB->sad8[iSubBlock]))) {
2177                  if (MotionFlags & PMV_QUICKSTOP16)                  if (MotionFlags & PMV_QUICKSTOP16)
2178                          goto PMVfast8_Terminate_without_Refine;                          goto PMVfast8_Terminate_without_Refine;
2179                  if (MotionFlags & PMV_EARLYSTOP16)                  if (MotionFlags & PMV_EARLYSTOP16)
# Line 1885  Line 2195 
2195  /* default: use best prediction as starting point for one call of PMVfast_MainSearch */  /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
2196          iSAD =          iSAD =
2197                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
2198                                                    currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx,                                                    currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
2199                                                    min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,                                                    min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
2200                                                    iQuant, iFound);                                                    iQuant, iFound);
2201    
# Line 1900  Line 2210 
2210                  if (!(MVequal(pmv[0], backupMV))) {                  if (!(MVequal(pmv[0], backupMV))) {
2211                          iSAD =                          iSAD =
2212                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
2213                                                                    pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv,                                                                    pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
2214                                                                    min_dx, max_dx, min_dy, max_dy, iEdgedWidth,                                                                    min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
2215                                                                    iDiamondSize, iFcode, iQuant, iFound);                                                                    iDiamondSize, iFcode, iQuant, iFound);
2216    
# Line 1913  Line 2223 
2223                  if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {                  if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2224                          iSAD =                          iSAD =
2225                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2226                                                                    iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy,                                                                    iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
2227                                                                    max_dy, iEdgedWidth, iDiamondSize, iFcode,                                                                    max_dy, iEdgedWidth, iDiamondSize, iFcode,
2228                                                                    iQuant, iFound);                                                                    iQuant, iFound);
2229    
# Line 1932  Line 2242 
2242          if (MotionFlags & PMV_HALFPELREFINE8)   // perform final half-pel step          if (MotionFlags & PMV_HALFPELREFINE8)   // perform final half-pel step
2243                  iMinSAD =                  iMinSAD =
2244                          Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,                          Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2245                                                          iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy,                                                          iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
2246                                                          iFcode, iQuant, iEdgedWidth);                                                          iFcode, iQuant, iEdgedWidth);
2247    
2248    
2249    PMVfast8_Terminate_without_Refine:    PMVfast8_Terminate_without_Refine:
2250          currPMV->x = currMV->x - pmv[0].x;          currPMV->x = currMV->x - center_x;
2251          currPMV->y = currMV->y - pmv[0].y;          currPMV->y = currMV->y - center_y;
2252    
2253          return iMinSAD;          return iMinSAD;
2254  }  }
# Line 1951  Line 2261 
2261                           const IMAGE * const pCur,                           const IMAGE * const pCur,
2262                           const int x,                           const int x,
2263                           const int y,                           const int y,
2264                            const int start_x,
2265                            const int start_y,
2266                            const int center_x,
2267                            const int center_y,
2268                           const uint32_t MotionFlags,                           const uint32_t MotionFlags,
2269                           const uint32_t iQuant,                           const uint32_t iQuant,
2270                           const uint32_t iFcode,                           const uint32_t iFcode,
# Line 1986  Line 2300 
2300          const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;          const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;
2301          MACROBLOCK *oldMB = NULL;          MACROBLOCK *oldMB = NULL;
2302    
2303          static int32_t thresh2;           int32_t thresh2;
2304          int32_t bPredEq;          int32_t bPredEq;
2305          int32_t iMinSAD, iSAD = 9999;          int32_t iMinSAD, iSAD = 9999;
2306    
# Line 2009  Line 2323 
2323                  max_dy = EVEN(max_dy);                  max_dy = EVEN(max_dy);
2324          }          }
2325          /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */          /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
2326          bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad);          bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad);
2327    
2328  /* Step 4: Calculate SAD around the Median prediction.  /* Step 4: Calculate SAD around the Median prediction.
2329          MinSAD=SAD          MinSAD=SAD
# Line 2020  Line 2334 
2334    
2335  // Prepare for main loop  // Prepare for main loop
2336    
2337          *currMV = pmv[0];                       /* current best := median prediction */          currMV->x = start_x;
2338            currMV->y = start_y;
2339    
2340          if (!(MotionFlags & PMV_HALFPEL16)) {          if (!(MotionFlags & PMV_HALFPEL16)) {
2341                  currMV->x = EVEN(currMV->x);                  currMV->x = EVEN(currMV->x);
2342                  currMV->y = EVEN(currMV->y);                  currMV->y = EVEN(currMV->y);
# Line 2042  Line 2358 
2358                            get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV,                            get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV,
2359                                                   iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);                                                   iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);
2360          iMinSAD +=          iMinSAD +=
2361                  calc_delta_16(currMV->x - pmv[0].x, currMV->y - pmv[0].y,                  calc_delta_16(currMV->x - center_x, currMV->y - center_y,
2362                                            (uint8_t) iFcode, iQuant);                                            (uint8_t) iFcode, iQuant);
2363    
2364  // thresh1 is fixed to 256  // thresh1 is fixed to 256
2365          if ((iMinSAD < 256) ||          if ((iMinSAD < 256) ||
2366                  ((MVequal(*currMV, prevMB->mvs[0])) &&                  ((MVequal(*currMV, prevMB->mvs[0])) &&
2367                   ((uint32_t) iMinSAD < prevMB->sad16))) {                   ((int32_t) iMinSAD < prevMB->sad16))) {
2368                  if (MotionFlags & PMV_QUICKSTOP16)                  if (MotionFlags & PMV_QUICKSTOP16)
2369                          goto EPZS16_Terminate_without_Refine;                          goto EPZS16_Terminate_without_Refine;
2370                  if (MotionFlags & PMV_EARLYSTOP16)                  if (MotionFlags & PMV_EARLYSTOP16)
# Line 2108  Line 2424 
2424    
2425          if ((iMinSAD <= thresh2)          if ((iMinSAD <= thresh2)
2426                  || (MVequal(*currMV, prevMB->mvs[0]) &&                  || (MVequal(*currMV, prevMB->mvs[0]) &&
2427                          ((uint32_t) iMinSAD <= prevMB->sad16))) {                          ((int32_t) iMinSAD <= prevMB->sad16))) {
2428                  if (MotionFlags & PMV_QUICKSTOP16)                  if (MotionFlags & PMV_QUICKSTOP16)
2429                          goto EPZS16_Terminate_without_Refine;                          goto EPZS16_Terminate_without_Refine;
2430                  if (MotionFlags & PMV_EARLYSTOP16)                  if (MotionFlags & PMV_EARLYSTOP16)
# Line 2154  Line 2470 
2470    
2471          backupMV = *currMV;                     /* save best prediction, actually only for EXTSEARCH */          backupMV = *currMV;                     /* save best prediction, actually only for EXTSEARCH */
2472    
2473          if (MotionFlags & PMV_USESQUARES8)          if (MotionFlags & PMV_USESQUARES16)
2474                  MainSearchPtr = Square16_MainSearch;                  MainSearchPtr = Square16_MainSearch;
2475          else          else
2476           if (MotionFlags & PMV_ADVANCEDDIAMOND8)           if (MotionFlags & PMV_ADVANCEDDIAMOND16)
2477                  MainSearchPtr = AdvDiamond16_MainSearch;                  MainSearchPtr = AdvDiamond16_MainSearch;
2478          else          else
2479                  MainSearchPtr = Diamond16_MainSearch;                  MainSearchPtr = Diamond16_MainSearch;
# Line 2166  Line 2482 
2482    
2483          iSAD =          iSAD =
2484                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
2485                                                    currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx,                                                    currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
2486                                                    min_dy, max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);                                                    min_dy, max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);
2487    
2488          if (iSAD < iMinSAD) {          if (iSAD < iMinSAD) {
# Line 2181  Line 2497 
2497                  if (!(MVequal(pmv[0], backupMV))) {                  if (!(MVequal(pmv[0], backupMV))) {
2498                          iSAD =                          iSAD =
2499                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
2500                                                                    pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv,                                                                    pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
2501                                                                    min_dx, max_dx, min_dy, max_dy, iEdgedWidth,                                                                    min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
2502                                                                    2, iFcode, iQuant, 0);                                                                    2, iFcode, iQuant, 0);
2503                  }                  }
# Line 2194  Line 2510 
2510                  if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {                  if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2511                          iSAD =                          iSAD =
2512                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2513                                                                    iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy,                                                                    iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
2514                                                                    max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);                                                                    max_dy, iEdgedWidth, 2, iFcode, iQuant, 0);
2515    
2516                          if (iSAD < iMinSAD) {                          if (iSAD < iMinSAD) {
# Line 2210  Line 2526 
2526          if (MotionFlags & PMV_HALFPELREFINE16)  // perform final half-pel step          if (MotionFlags & PMV_HALFPELREFINE16)  // perform final half-pel step
2527                  iMinSAD =                  iMinSAD =
2528                          Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,                          Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2529                                                           iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy,                                                           iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
2530                                                           iFcode, iQuant, iEdgedWidth);                                                           iFcode, iQuant, iEdgedWidth);
2531    
2532    EPZS16_Terminate_without_Refine:    EPZS16_Terminate_without_Refine:
2533    
2534          *oldMB = *prevMB;          *oldMB = *prevMB;
2535    
2536          currPMV->x = currMV->x - pmv[0].x;          currPMV->x = currMV->x - center_x;
2537          currPMV->y = currMV->y - pmv[0].y;          currPMV->y = currMV->y - center_y;
2538          return iMinSAD;          return iMinSAD;
2539  }  }
2540    
# Line 2233  Line 2549 
2549                          const int y,                          const int y,
2550                          const int start_x,                          const int start_x,
2551                          const int start_y,                          const int start_y,
2552                            const int center_x,
2553                            const int center_y,
2554                          const uint32_t MotionFlags,                          const uint32_t MotionFlags,
2555                          const uint32_t iQuant,                          const uint32_t iQuant,
2556                          const uint32_t iFcode,                          const uint32_t iFcode,
# Line 2287  Line 2605 
2605                  max_dy = EVEN(max_dy);                  max_dy = EVEN(max_dy);
2606          }          }
2607          /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */          /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */
2608          bPredEq = get_pmvdata(pMBs, x >> 1, y >> 1, iWcount, iSubBlock, pmv, psad);          bPredEq = get_pmvdata2(pMBs, iWcount, 0, x >> 1, y >> 1, iSubBlock, pmv, psad);
2609    
2610    
2611  /* Step 4: Calculate SAD around the Median prediction.  /* Step 4: Calculate SAD around the Median prediction.
# Line 2322  Line 2640 
2640                           get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV,                           get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV,
2641                                                  iEdgedWidth), iEdgedWidth);                                                  iEdgedWidth), iEdgedWidth);
2642          iMinSAD +=          iMinSAD +=
2643                  calc_delta_8(currMV->x - pmv[0].x, currMV->y - pmv[0].y,                  calc_delta_8(currMV->x - center_x, currMV->y - center_y,
2644                                           (uint8_t) iFcode, iQuant);                                           (uint8_t) iFcode, iQuant);
2645    
2646    
# Line 2398  Line 2716 
2716    
2717  // there is no EPZS^2 for inter4v at the moment  // there is no EPZS^2 for inter4v at the moment
2718    
2719  //  if (MotionFlags & PMV_USESQUARES8)    if (MotionFlags & PMV_USESQUARES8)
2720  //      MainSearchPtr = Square8_MainSearch;        MainSearchPtr = Square8_MainSearch;
2721  //  else    else
2722    
2723          if (MotionFlags & PMV_ADVANCEDDIAMOND8)          if (MotionFlags & PMV_ADVANCEDDIAMOND8)
2724                  MainSearchPtr = AdvDiamond8_MainSearch;                  MainSearchPtr = AdvDiamond8_MainSearch;
# Line 2409  Line 2727 
2727    
2728          iSAD =          iSAD =
2729                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
2730                                                    currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx,                                                    currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
2731                                                    min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,                                                    min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
2732                                                    iQuant, 0);                                                    iQuant, 0);
2733    
# Line 2425  Line 2743 
2743                  if (!(MVequal(pmv[0], backupMV))) {                  if (!(MVequal(pmv[0], backupMV))) {
2744                          iSAD =                          iSAD =
2745                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
2746                                                                    pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv,                                                                    pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
2747                                                                    min_dx, max_dx, min_dy, max_dy, iEdgedWidth,                                                                    min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
2748                                                                    iDiamondSize, iFcode, iQuant, 0);                                                                    iDiamondSize, iFcode, iQuant, 0);
2749    
# Line 2438  Line 2756 
2756                  if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {                  if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
2757                          iSAD =                          iSAD =
2758                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,                                  (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
2759                                                                    iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy,                                                                    iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
2760                                                                    max_dy, iEdgedWidth, iDiamondSize, iFcode,                                                                    max_dy, iEdgedWidth, iDiamondSize, iFcode,
2761                                                                    iQuant, 0);                                                                    iQuant, 0);
2762    
# Line 2455  Line 2773 
2773          if (MotionFlags & PMV_HALFPELREFINE8)   // perform final half-pel step          if (MotionFlags & PMV_HALFPELREFINE8)   // perform final half-pel step
2774                  iMinSAD =                  iMinSAD =
2775                          Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,                          Halfpel8_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
2776                                                          iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy,                                                          iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
2777                                                          iFcode, iQuant, iEdgedWidth);                                                          iFcode, iQuant, iEdgedWidth);
2778    
2779    EPZS8_Terminate_without_Refine:    EPZS8_Terminate_without_Refine:
2780    
2781          currPMV->x = currMV->x - pmv[0].x;          currPMV->x = currMV->x - center_x;
2782          currPMV->y = currMV->y - pmv[0].y;          currPMV->y = currMV->y - center_y;
2783          return iMinSAD;          return iMinSAD;
2784  }  }
2785    
2786    
2787    
2788    int32_t
2789    PMVfastIntSearch16(const uint8_t * const pRef,
2790                                    const uint8_t * const pRefH,
2791                                    const uint8_t * const pRefV,
2792                                    const uint8_t * const pRefHV,
2793                                    const IMAGE * const pCur,
2794                                    const int x,
2795                                    const int y,
2796                                    const int start_x,              /* start should be most likely vector */
2797                                    const int start_y,
2798                                    const int center_x,             /* center is from where length of MVs is measured */
2799                                    const int center_y,
2800                                    const uint32_t MotionFlags,
2801                                    const uint32_t iQuant,
2802                                    const uint32_t iFcode,
2803                                    const MBParam * const pParam,
2804                                    const MACROBLOCK * const pMBs,
2805                                    const MACROBLOCK * const prevMBs,
2806                                    VECTOR * const currMV,
2807                                    VECTOR * const currPMV)
2808    {
2809            const uint32_t iWcount = pParam->mb_width;
2810            const int32_t iWidth = pParam->width;
2811            const int32_t iHeight = pParam->height;
2812            const int32_t iEdgedWidth = pParam->edged_width;
2813    
2814            const uint8_t *cur = pCur->y + x * 16 + y * 16 * iEdgedWidth;
2815            const VECTOR zeroMV = { 0, 0 };
2816    
2817            int32_t iDiamondSize;
2818    
2819            int32_t min_dx;
2820            int32_t max_dx;
2821            int32_t min_dy;
2822            int32_t max_dy;
2823    
2824            int32_t iFound;
2825    
2826            VECTOR newMV;
2827            VECTOR backupMV;
2828    
2829            VECTOR pmv[4];
2830            int32_t psad[4];
2831    
2832  /* ***********************************************************          MainSearch16FuncPtr MainSearchPtr;
2833          bvop motion estimation  
2834  // TODO: need to incorporate prediction here (eg. sad += calc_delta_16)          const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount;
2835  ***************************************************************/          MACROBLOCK *const pMB = pMBs + x + y * iWcount;
2836    
2837            int32_t threshA, threshB;
2838  void          int32_t bPredEq;
2839  MotionEstimationBVOP(MBParam * const pParam,          int32_t iMinSAD, iSAD;
2840                                           FRAMEINFO * const frame,  
2841                                           // forward (past) reference  
2842                                           const MACROBLOCK * const f_mbs,  /* Get maximum range */
2843                                           const IMAGE * const f_ref,          get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight,
2844                                           const IMAGE * const f_refH,                            iFcode);
2845                                           const IMAGE * const f_refV,  
2846                                           const IMAGE * const f_refHV,  /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */
2847                                           // backward (future) reference  
2848                                           const MACROBLOCK * const b_mbs,          if ((x == 0) && (y == 0)) {
2849                                           const IMAGE * const b_ref,                  threshA = 512;
2850                                           const IMAGE * const b_refH,                  threshB = 1024;
2851                                           const IMAGE * const b_refV,  
2852                                           const IMAGE * const b_refHV)                  bPredEq = 0;
2853  {                  psad[0] = psad[1] = psad[2] = psad[3] = 0;
2854          const uint32_t mb_width = pParam->mb_width;                  *currMV = pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV;
2855          const uint32_t mb_height = pParam->mb_height;  
2856          const int32_t edged_width = pParam->edged_width;          } else {
2857    
2858          uint32_t i, j;                  bPredEq = get_ipmvdata(pMBs, iWcount, 0, x, y, 0, pmv, psad);
2859    
2860          int32_t f_sad16;                  threshA = psad[0];
2861          int32_t b_sad16;                  threshB = threshA + 256;
2862          int32_t i_sad16;                  if (threshA < 512)
2863          int32_t d_sad16;                          threshA = 512;
2864          int32_t best_sad;                  if (threshA > 1024)
2865                            threshA = 1024;
2866          VECTOR pmv_dontcare;                  if (threshB > 1792)
2867                            threshB = 1792;
2868          // note: i==horizontal, j==vertical  
2869          for (j = 0; j < mb_height; j++) {                  *currMV = pmv[0];                       /* current best := prediction */
2870                  for (i = 0; i < mb_width; i++) {          }
2871                          MACROBLOCK *mb = &frame->mbs[i + j * mb_width];  
2872                          const MACROBLOCK *f_mb = &f_mbs[i + j * mb_width];          iFound = 0;
2873                          const MACROBLOCK *b_mb = &b_mbs[i + j * mb_width];  
2874    /* Step 4: Calculate SAD around the Median prediction.
2875                          if (b_mb->mode == MODE_INTER && b_mb->cbp == 0 &&     MinSAD=SAD
2876                                  b_mb->mvs[0].x == 0 && b_mb->mvs[0].y == 0) {     If Motion Vector equal to Previous frame motion vector
2877                                  mb->mode = MODE_NOT_CODED;     and MinSAD<PrevFrmSAD goto Step 10.
2878                                  mb->mvs[0].x = 0;     If SAD<=256 goto Step 10.
2879                                  mb->mvs[0].y = 0;  */
2880                                  mb->b_mvs[0].x = 0;  
2881                                  mb->b_mvs[0].y = 0;          if (currMV->x > max_dx) {
2882                                  continue;                  currMV->x = EVEN(max_dx);
2883            }
2884            if (currMV->x < min_dx) {
2885                    currMV->x = EVEN(min_dx);
2886            }
2887            if (currMV->y > max_dy) {
2888                    currMV->y = EVEN(max_dy);
2889            }
2890            if (currMV->y < min_dy) {
2891                    currMV->y = EVEN(min_dy);
2892            }
2893    
2894            iMinSAD =
2895                    sad16(cur,
2896                              get_iref_mv(pRef, x, y, 16, currMV,
2897                                                     iEdgedWidth), iEdgedWidth, MV_MAX_ERROR);
2898            iMinSAD +=
2899                    calc_delta_16(currMV->x - center_x, currMV->y - center_y,
2900                                              (uint8_t) iFcode, iQuant);
2901    
2902            if ((iMinSAD < 256) ||
2903                    ((MVequal(*currMV, prevMB->i_mvs[0])) &&
2904                     ((int32_t) iMinSAD < prevMB->i_sad16))) {
2905                    if (iMinSAD < 2 * iQuant)       // high chances for SKIP-mode
2906                    {
2907                            if (!MVzero(*currMV)) {
2908                                    iMinSAD += MV16_00_BIAS;
2909                                    CHECK_MV16_ZERO;        // (0,0) saves space for letterboxed pictures
2910                                    iMinSAD -= MV16_00_BIAS;
2911                          }                          }
2912                    }
2913    
2914                    if (MotionFlags & PMV_EARLYSTOP16)
2915                            goto PMVfastInt16_Terminate_with_Refine;
2916            }
2917    
2918    
2919    /* Step 2 (lazy eval): Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion
2920       vector of the median.
2921       If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2
2922    */
2923    
2924            if ((bPredEq) && (MVequal(pmv[0], prevMB->i_mvs[0])))
2925                    iFound = 2;
2926    
2927                          // forward search  /* Step 3 (lazy eval): If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search.
2928                          f_sad16 =     Otherwise select large Diamond Search.
2929                                  SEARCH16(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,  */
                                                  &frame->image, i, j, frame->motion_flags,  
                                                  frame->quant, frame->fcode, pParam, f_mbs,  
                                                  f_mbs /* todo */ ,  
                                                  &mb->mvs[0], &pmv_dontcare);   // ignore pmv  
   
                         // backward search  
                         b_sad16 = SEARCH16(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, &frame->image, i, j, frame->motion_flags, frame->quant, frame->bcode, pParam, b_mbs, b_mbs,      /* todo */  
                                                            &mb->b_mvs[0], &pmv_dontcare);       // ignore pmv  
2930    
2931                          // interpolate search (simple, but effective)          if ((!MVzero(pmv[0])) || (threshB < 1536) || (bPredEq))
2932                          i_sad16 = 65535;                  iDiamondSize = 2;               // halfpel units!
2933            else
2934                    iDiamondSize = 4;               // halfpel units!
2935    
2936                          /*                          /*
2937                          x/y range somewhat buggy     Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block.
2938                          i_sad16 =     Also calculate (0,0) but do not subtract offset.
2939                                  sad16bi_c(frame->image.y + i * 16 + j * 16 * edged_width,     Let MinSAD be the smallest SAD up to this point.
2940                                                    get_ref(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,     If MV is (0,0) subtract offset.
2941                                                                    i, j, 16, mb->mvs[0].x, mb->mvs[0].y,  */
2942                                                                    edged_width), get_ref(b_ref->y, b_refH->y,  
2943                                                                                                                  b_refV->y, b_refHV->y,  // (0,0) is often a good choice
2944                                                                                                                  i, j, 16,  
2945                                                                                                                  mb->b_mvs[0].x,          if (!MVzero(pmv[0]))
2946                                                                                                                  mb->b_mvs[0].x,                  CHECK_MV16_ZERO;
2947                                                                                                                  edged_width),  
2948                                                    edged_width);  // previous frame MV is always possible
2949                          */  
2950            if (!MVzero(prevMB->i_mvs[0]))
2951                          // TODO: direct search                  if (!MVequal(prevMB->i_mvs[0], pmv[0]))
2952                          // predictor + range of [-32,32]                          CHECK_MV16_CANDIDATE(prevMB->i_mvs[0].x, prevMB->i_mvs[0].y);
2953                          d_sad16 = 65535;  
2954    // left neighbour, if allowed
2955    
2956                          if (f_sad16 < b_sad16) {          if (!MVzero(pmv[1]))
2957                                  best_sad = f_sad16;                  if (!MVequal(pmv[1], prevMB->i_mvs[0]))
2958                                  mb->mode = MODE_FORWARD;                          if (!MVequal(pmv[1], pmv[0]))
2959                          } else {                                  CHECK_MV16_CANDIDATE(pmv[1].x, pmv[1].y);
2960                                  best_sad = b_sad16;  
2961                                  mb->mode = MODE_BACKWARD;  // top neighbour, if allowed
2962            if (!MVzero(pmv[2]))
2963                    if (!MVequal(pmv[2], prevMB->i_mvs[0]))
2964                            if (!MVequal(pmv[2], pmv[0]))
2965                                    if (!MVequal(pmv[2], pmv[1]))
2966                                            CHECK_MV16_CANDIDATE(pmv[2].x, pmv[2].y);
2967    
2968    // top right neighbour, if allowed
2969                                            if (!MVzero(pmv[3]))
2970                                                    if (!MVequal(pmv[3], prevMB->i_mvs[0]))
2971                                                            if (!MVequal(pmv[3], pmv[0]))
2972                                                                    if (!MVequal(pmv[3], pmv[1]))
2973                                                                            if (!MVequal(pmv[3], pmv[2]))
2974                                                                                    CHECK_MV16_CANDIDATE(pmv[3].x,
2975                                                                                                                             pmv[3].y);
2976    
2977            if ((MVzero(*currMV)) &&
2978                    (!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96) */ )
2979                    iMinSAD -= MV16_00_BIAS;
2980    
2981    
2982    /* Step 6: If MinSAD <= thresa goto Step 10.
2983       If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10.
2984    */
2985    
2986            if ((iMinSAD <= threshA) ||
2987                    (MVequal(*currMV, prevMB->i_mvs[0]) &&
2988                     ((int32_t) iMinSAD < prevMB->i_sad16))) {
2989    
2990                    if (MotionFlags & PMV_EARLYSTOP16)
2991                            goto PMVfastInt16_Terminate_with_Refine;
2992                          }                          }
2993    
2994                          if (i_sad16 < best_sad) {  
2995                                  best_sad = i_sad16;  /************ (Diamond Search)  **************/
2996                                  mb->mode = MODE_INTERPOLATE;  /*
2997       Step 7: Perform Diamond search, with either the small or large diamond.
2998       If Found=2 only examine one Diamond pattern, and afterwards goto step 10
2999       Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond.
3000       If center then goto step 10.
3001       Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center.
3002       Refine by using small diamond and goto step 10.
3003    */
3004    
3005            if (MotionFlags & PMV_USESQUARES16)
3006                    MainSearchPtr = Square16_MainSearch;
3007            else if (MotionFlags & PMV_ADVANCEDDIAMOND16)
3008                    MainSearchPtr = AdvDiamond16_MainSearch;
3009            else
3010                    MainSearchPtr = Diamond16_MainSearch;
3011    
3012            backupMV = *currMV;                     /* save best prediction, actually only for EXTSEARCH */
3013    
3014    
3015    /* default: use best prediction as starting point for one call of PMVfast_MainSearch */
3016            iSAD =
3017                    (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x,
3018                                                      currMV->y, iMinSAD, &newMV, center_x, center_y, min_dx, max_dx,
3019                                                      min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode,
3020                                                      iQuant, iFound);
3021    
3022            if (iSAD < iMinSAD) {
3023                    *currMV = newMV;
3024                    iMinSAD = iSAD;
3025                          }                          }
3026    
3027                          if (d_sad16 < best_sad) {          if (MotionFlags & PMV_EXTSEARCH16) {
3028                                  best_sad = d_sad16;  /* extended: search (up to) two more times: orignal prediction and (0,0) */
3029                                  mb->mode = MODE_DIRECT;  
3030                    if (!(MVequal(pmv[0], backupMV))) {
3031                            iSAD =
3032                                    (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y,
3033                                                                      pmv[0].x, pmv[0].y, iMinSAD, &newMV, center_x, center_y,
3034                                                                      min_dx, max_dx, min_dy, max_dy, iEdgedWidth,
3035                                                                      iDiamondSize, iFcode, iQuant, iFound);
3036    
3037                            if (iSAD < iMinSAD) {
3038                                    *currMV = newMV;
3039                                    iMinSAD = iSAD;
3040                          }                          }
3041                    }
3042    
3043                    if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) {
3044                            iSAD =
3045                                    (*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0,
3046                                                                      iMinSAD, &newMV, center_x, center_y, min_dx, max_dx, min_dy,
3047                                                                      max_dy, iEdgedWidth, iDiamondSize, iFcode,
3048                                                                      iQuant, iFound);
3049    
3050                            if (iSAD < iMinSAD) {
3051                                    *currMV = newMV;
3052                                    iMinSAD = iSAD;
3053                  }                  }
3054          }          }
3055  }  }
3056    
3057    /*
3058       Step 10:  The motion vector is chosen according to the block corresponding to MinSAD.
3059    */
3060    
3061    PMVfastInt16_Terminate_with_Refine:
3062    
3063            pMB->i_mvs[0] = pMB->i_mvs[1] = pMB->i_mvs[2] = pMB->i_mvs[3] = pMB->i_mv16 = *currMV;
3064            pMB->i_sad8[0] = pMB->i_sad8[1] = pMB->i_sad8[2] = pMB->i_sad8[3] = pMB->i_sad16 = iMinSAD;
3065    
3066            if (MotionFlags & PMV_HALFPELREFINE16)  // perform final half-pel step
3067                    iMinSAD =
3068                            Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV,
3069                                                             iMinSAD, center_x, center_y, min_dx, max_dx, min_dy, max_dy,
3070                                                             iFcode, iQuant, iEdgedWidth);
3071    
3072            pmv[0] = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);          // get _REAL_ prediction (halfpel possible)
3073    
3074    PMVfastInt16_Terminate_without_Refine:
3075            currPMV->x = currMV->x - center_x;
3076            currPMV->y = currMV->y - center_y;
3077            return iMinSAD;
3078    }
3079    

Legend:
Removed from v.232  
changed lines
  Added in v.444

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