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

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

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

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

Legend:
Removed from v.344  
changed lines
  Added in v.644

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