--- trunk/xvidcore/src/motion/motion_est.c 2002/03/08 02:46:11 3 +++ trunk/xvidcore/src/motion/motion_est.c 2002/03/28 20:57:25 78 @@ -4,24 +4,24 @@ * * 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 + * 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% + * 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) + * 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 + * 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 + * 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. * @@ -67,42 +67,43 @@ #define MIN(X, Y) ((X)<(Y)?(X):(Y)) #define MAX(X, Y) ((X)>(Y)?(X):(Y)) -#define ABS(X) (((X)>0)?(X):-(X)) -#define SIGN(X) (((X)>0)?1:-1) +#define ABS(X) (((X)>0)?(X):-(X)) +#define SIGN(X) (((X)>0)?1:-1) int32_t PMVfastSearch8( - const uint8_t * const pRef, - const uint8_t * const pRefH, - const uint8_t * const pRefV, - const uint8_t * const pRefHV, - const IMAGE * const pCur, - const int x, const int y, - const int start_x, int start_y, - const uint32_t iQuality, - MBParam * const pParam, - MACROBLOCK * const pMBs, - VECTOR * const currMV, - VECTOR * const currPMV); + 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, int start_y, + const uint32_t iQuality, + MBParam * const pParam, + MACROBLOCK * const pMBs, + VECTOR * const currMV, + VECTOR * const currPMV); int32_t PMVfastSearch16( - const uint8_t * const pRef, - const uint8_t * const pRefH, - const uint8_t * const pRefV, - const uint8_t * const pRefHV, - const IMAGE * const pCur, - const int x, const int y, - const uint32_t iQuality, - MBParam * const pParam, - MACROBLOCK * const pMBs, - VECTOR * const currMV, - VECTOR * const currPMV); + 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 iQuality, + MBParam * const pParam, + MACROBLOCK * const pMBs, + VECTOR * const currMV, + VECTOR * const currPMV); -/* diamond search stuff - keep the the sequence in circular order (so optimization works) -*/ +/* + * diamond search stuff + * keep the the sequence in circular order (so optimization works) + */ typedef struct { @@ -178,16 +179,17 @@ */ static void __inline get_range( - int32_t * const min_dx, int32_t * const max_dx, - int32_t * const min_dy, int32_t * const max_dy, - const uint32_t x, const uint32_t y, - const uint32_t block_sz, // block dimension, 8 or 16 - const uint32_t width, const uint32_t height, - const uint32_t fcode) + int32_t * const min_dx, int32_t * const max_dx, + int32_t * const min_dy, int32_t * const max_dy, + const uint32_t x, const uint32_t y, + const uint32_t block_sz, // block dimension, 8 or 16 + const uint32_t width, const uint32_t height, + const uint32_t fcode) { + const int search_range = 32 << (fcode - 1); - const int high = search_range - 1; - const int low = -search_range; + const int high = search_range - 1; + const int low = -search_range; // convert full-pixel measurements to half pixel const int hp_width = 2 * width; @@ -196,59 +198,65 @@ const int hp_x = 2 * (x) * block_sz; // we need _right end_ of block, not x-coordinate const int hp_y = 2 * (y) * block_sz; // same for _bottom end_ - *max_dx = MIN(high, hp_width - hp_x); - *max_dy = MIN(high, hp_height - hp_y); - *min_dx = MAX(low, -(hp_edge + hp_x)); - *min_dy = MAX(low, -(hp_edge + hp_y)); + *max_dx = MIN(high, hp_width - hp_x); + *max_dy = MIN(high, hp_height - hp_y); + *min_dx = MAX(low, -(hp_edge + hp_x)); + *min_dy = MAX(low, -(hp_edge + hp_y)); + } -/* getref: calculate reference image pointer -the decision to use interpolation h/v/hv or the normal image is -based on dx & dy. -*/ +/* + * getref: calculate reference image pointer + * the decision to use interpolation h/v/hv or the normal image is + * based on dx & dy. + */ static __inline const uint8_t * get_ref( - const uint8_t * const refn, - const uint8_t * const refh, - const uint8_t * const refv, - const uint8_t * const refhv, - const uint32_t x, const uint32_t y, - const uint32_t block, // block dimension, 8 or 16 - const int32_t dx, const int32_t dy, - const uint32_t stride) + const uint8_t * const refn, + const uint8_t * const refh, + const uint8_t * const refv, + const uint8_t * const refhv, + const uint32_t x, const uint32_t y, + const uint32_t block, // block dimension, 8 or 16 + const int32_t dx, const int32_t dy, + const uint32_t stride) { + switch ( ((dx&1)<<1) + (dy&1) ) // ((dx%2)?2:0)+((dy%2)?1:0) - { - case 0 : return refn + (x*block+dx/2) + (y*block+dy/2)*stride; - case 1 : return refv + (x*block+dx/2) + (y*block+(dy-1)/2)*stride; - case 2 : return refh + (x*block+(dx-1)/2) + (y*block+dy/2)*stride; + { + case 0 : return refn + (x*block+dx/2) + (y*block+dy/2)*stride; + case 1 : return refv + (x*block+dx/2) + (y*block+(dy-1)/2)*stride; + case 2 : return refh + (x*block+(dx-1)/2) + (y*block+dy/2)*stride; default : - case 3 : return refhv + (x*block+(dx-1)/2) + (y*block+(dy-1)/2)*stride; + case 3 : return refhv + (x*block+(dx-1)/2) + (y*block+(dy-1)/2)*stride; } + } /* This is somehow a copy of get_ref, but with MV instead of X,Y */ static __inline const uint8_t * get_ref_mv( - const uint8_t * const refn, - const uint8_t * const refh, - const uint8_t * const refv, - const uint8_t * const refhv, - const uint32_t x, const uint32_t y, - const uint32_t block, // block dimension, 8 or 16 - const VECTOR* mv, // measured in half-pel! - const uint32_t stride) + const uint8_t * const refn, + const uint8_t * const refh, + const uint8_t * const refv, + const uint8_t * const refhv, + const uint32_t x, const uint32_t y, + const uint32_t block, // block dimension, 8 or 16 + const VECTOR* mv, // measured in half-pel! + const uint32_t stride) { + switch ( (((mv->x)&1)<<1) + ((mv->y)&1) ) - { - case 0 : return refn + (x*block+(mv->x)/2) + (y*block+(mv->y)/2)*stride; - case 1 : return refv + (x*block+(mv->x)/2) + (y*block+((mv->y)-1)/2)*stride; - case 2 : return refh + (x*block+((mv->x)-1)/2) + (y*block+(mv->y)/2)*stride; + { + case 0 : return refn + (x*block+(mv->x)/2) + (y*block+(mv->y)/2)*stride; + case 1 : return refv + (x*block+(mv->x)/2) + (y*block+((mv->y)-1)/2)*stride; + case 2 : return refh + (x*block+((mv->x)-1)/2) + (y*block+(mv->y)/2)*stride; default : - case 3 : return refhv + (x*block+((mv->x)-1)/2) + (y*block+((mv->y)-1)/2)*stride; + case 3 : return refhv + (x*block+((mv->x)-1)/2) + (y*block+((mv->y)-1)/2)*stride; } + } #ifndef SEARCH16 @@ -260,89 +268,101 @@ #endif bool MotionEstimation( - MACROBLOCK * const pMBs, - MBParam * const pParam, - const IMAGE * const pRef, - const IMAGE * const pRefH, - const IMAGE * const pRefV, - const IMAGE * const pRefHV, - IMAGE * const pCurrent, - const uint32_t iLimit) + MACROBLOCK * const pMBs, + MBParam * const pParam, + const IMAGE * const pRef, + const IMAGE * const pRefH, + const IMAGE * const pRefV, + const IMAGE * const pRefHV, + IMAGE * const pCurrent, + const uint32_t iLimit) { - const uint32_t iWcount = pParam->mb_width; - const uint32_t iHcount = pParam->mb_height; + const uint32_t iWcount = pParam->mb_width; + const uint32_t iHcount = pParam->mb_height; uint32_t i, j, iIntra = 0; - VECTOR mv16; - VECTOR pmv16; + VECTOR mv16; + VECTOR pmv16; - int32_t sad8 = 0; - int32_t sad16; - int32_t deviation; + int32_t sad8 = 0; + int32_t sad16; + int32_t deviation; // note: i==horizontal, j==vertical - for (i = 0; i < iHcount; i++) + for (i = 0; i < iHcount; i++) for (j = 0; j < iWcount; j++) { MACROBLOCK *pMB = &pMBs[j + i * iWcount]; sad16 = SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, - j, i, pParam->motion_flags, - pParam, pMBs, &mv16, &pmv16); + j, i, pParam->motion_flags, + pParam, pMBs, &mv16, &pmv16); pMB->sad16=sad16; - /* decide: MODE_INTER or MODE_INTRA - if (dev_intra < sad_inter - 2 * nb) use_intra - */ + /* decide: MODE_INTER or MODE_INTRA + if (dev_intra < sad_inter - 2 * nb) use_intra + */ - deviation = dev16(pCurrent->y + j*16 + i*16*pParam->edged_width, pParam->edged_width); + deviation = dev16(pCurrent->y + j*16 + i*16*pParam->edged_width, pParam->edged_width); - if (deviation < (sad16 - INTER_BIAS)) - { - pMB->mode = MODE_INTRA; - pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; - pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; - - iIntra++; - if(iIntra >= iLimit) - return 1; + if (deviation < (sad16 - INTER_BIAS)) + { + pMB->mode = MODE_INTRA; + pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; + pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; + + iIntra++; + if(iIntra >= iLimit) + return 1; - continue; - } + continue; + } - if (pParam->global_flags & XVID_INTER4V) - { - pMB->sad8[0] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, - 2 * j, 2 * i, mv16.x, mv16.y, pParam->motion_flags, - pParam, pMBs, &pMB->mvs[0], &pMB->pmvs[0]); - - pMB->sad8[1] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, - 2 * j + 1, 2 * i, mv16.x, mv16.y, pParam->motion_flags, - pParam, pMBs, &pMB->mvs[1], &pMB->pmvs[1]); - - pMB->sad8[2] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, - 2 * j, 2 * i + 1, mv16.x, mv16.y, pParam->motion_flags, - pParam, pMBs, &pMB->mvs[2], &pMB->pmvs[2]); + if (pParam->global_flags & XVID_INTER4V) + { + pMB->sad8[0] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, + 2 * j, 2 * i, mv16.x, mv16.y, pParam->motion_flags, + pParam, pMBs, &pMB->mvs[0], &pMB->pmvs[0]); + + pMB->sad8[1] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, + 2 * j + 1, 2 * i, mv16.x, mv16.y, pParam->motion_flags, + pParam, pMBs, &pMB->mvs[1], &pMB->pmvs[1]); + + pMB->sad8[2] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, + 2 * j, 2 * i + 1, mv16.x, mv16.y, pParam->motion_flags, + pParam, pMBs, &pMB->mvs[2], &pMB->pmvs[2]); - pMB->sad8[3] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, - 2 * j + 1, 2 * i + 1, mv16.x, mv16.y, pParam->motion_flags, - pParam, pMBs, &pMB->mvs[3], &pMB->pmvs[3]); + pMB->sad8[3] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, + 2 * j + 1, 2 * i + 1, mv16.x, mv16.y, pParam->motion_flags, + pParam, pMBs, &pMB->mvs[3], &pMB->pmvs[3]); - sad8 = pMB->sad8[0] + pMB->sad8[1] + pMB->sad8[2] + pMB->sad8[3]; - } + sad8 = pMB->sad8[0] + pMB->sad8[1] + pMB->sad8[2] + pMB->sad8[3]; + } - /* decide: MODE_INTER or MODE_INTER4V - mpeg4: if (sad8 < sad16 - nb/2+1) use_inter4v - */ - - if (pMB->dquant == NO_CHANGE) { - if (((pParam->global_flags & XVID_INTER4V)==0) || - (sad16 < (sad8 + (int32_t)(IMV16X16 * pParam->quant)))) { + /* decide: MODE_INTER or MODE_INTER4V + mpeg4: if (sad8 < sad16 - nb/2+1) use_inter4v + */ + + if (pMB->dquant == NO_CHANGE) { + if (((pParam->global_flags & XVID_INTER4V)==0) || + (sad16 < (sad8 + (int32_t)(IMV16X16 * pParam->quant)))) { + sad8 = sad16; + pMB->mode = MODE_INTER; + pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = mv16.x; + pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = mv16.y; + pMB->pmvs[0].x = pmv16.x; + pMB->pmvs[0].y = pmv16.y; + } + else + pMB->mode = MODE_INTER4V; + } + else + { sad8 = sad16; pMB->mode = MODE_INTER; pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = mv16.x; @@ -350,19 +370,7 @@ pMB->pmvs[0].x = pmv16.x; pMB->pmvs[0].y = pmv16.y; } - else - pMB->mode = MODE_INTER4V; - } - else - { - sad8 = sad16; - pMB->mode = MODE_INTER; - pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = mv16.x; - pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = mv16.y; - pMB->pmvs[0].x = pmv16.x; - pMB->pmvs[0].y = pmv16.y; } - } return 0; } @@ -494,23 +502,23 @@ */ int32_t PMVfastSearch16_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, - int32_t startx, int32_t starty, - int32_t iMinSAD, - VECTOR * const currMV, - const VECTOR * const pmv, - const int32_t min_dx, const int32_t max_dx, - const int32_t min_dy, const int32_t max_dy, - const int32_t iEdgedWidth, - const int32_t iDiamondSize, - const int32_t iFcode, - const int32_t iQuant, - int iFound) + const uint8_t * const pRef, + const uint8_t * const pRefH, + const uint8_t * const pRefV, + const uint8_t * const pRefHV, + const uint8_t * const cur, + const int x, const int y, + int32_t startx, int32_t starty, + int32_t iMinSAD, + VECTOR * const currMV, + const VECTOR * const pmv, + const int32_t min_dx, const int32_t max_dx, + const int32_t min_dy, const int32_t max_dy, + const int32_t iEdgedWidth, + const int32_t iDiamondSize, + const int32_t iFcode, + const int32_t iQuant, + int iFound) { /* Do a diamond search around given starting point, return SAD of best */ @@ -543,28 +551,28 @@ CHECK_MV16_CANDIDATE_FOUND(backupMV.x,backupMV.y+iDiamondSize,4); } else - { - currMV->x = startx; - currMV->y = starty; - } + { + currMV->x = startx; + currMV->y = starty; + } return iMinSAD; } int32_t PMVfastSearch16_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 VECTOR * const pmv, - const int32_t min_dx, const int32_t max_dx, - const int32_t min_dy, const int32_t max_dy, - const int32_t iFcode, - const int32_t iQuant, - const int32_t iEdgedWidth) + 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 VECTOR * const pmv, + const int32_t min_dx, const int32_t max_dx, + const int32_t min_dy, const int32_t max_dy, + const int32_t iFcode, + const int32_t iQuant, + const int32_t iEdgedWidth) { /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */ @@ -586,17 +594,17 @@ #define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16) int32_t PMVfastSearch16( - const uint8_t * const pRef, - const uint8_t * const pRefH, - const uint8_t * const pRefV, - const uint8_t * const pRefHV, - const IMAGE * const pCur, - const int x, const int y, - const uint32_t MotionFlags, - MBParam * const pParam, - MACROBLOCK * const pMBs, - VECTOR * const currMV, - VECTOR * const currPMV) + const uint8_t * const pRef, + const uint8_t * const pRefH, + const uint8_t * const pRefV, + const uint8_t * const pRefHV, + const IMAGE * const pCur, + const int x, const int y, + const uint32_t MotionFlags, + MBParam * const pParam, + MACROBLOCK * const pMBs, + VECTOR * const currMV, + VECTOR * const currPMV) { const uint32_t iWcount = pParam->mb_width; const int32_t iFcode = pParam->fixed_code; @@ -630,15 +638,15 @@ /* Get maximum range */ get_range(&min_dx, &max_dx, &min_dy, &max_dy, - x, y, 16, iWidth, iHeight, iFcode); + x, y, 16, iWidth, iHeight, iFcode); /* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */ if (!(MotionFlags & PMV_HALFPEL16 )) { min_dx = EVEN(min_dx); - max_dx = EVEN(max_dx); - min_dy = EVEN(min_dy); - max_dy = EVEN(max_dy); + max_dx = EVEN(max_dx); + min_dy = EVEN(min_dy); + max_dy = EVEN(max_dy); } /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */ @@ -662,15 +670,15 @@ iFound=0; /* Step 2: Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion - vector of the median. - If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2 + vector of the median. + If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2 */ if ((bPredEq) && (MVequal(pmv[0],pMB->mvs[0]) ) ) iFound=2; /* Step 3: If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search. - Otherwise select large Diamond Search. + Otherwise select large Diamond Search. */ if ( (pmv[0].x != 0) || (pmv[0].y != 0) || (threshB<1536) || (bPredEq) ) @@ -682,10 +690,10 @@ iDiamondSize*=2; /* Step 4: Calculate SAD around the Median prediction. - MinSAD=SAD - If Motion Vector equal to Previous frame motion vector - and MinSADx > max_dx) - { - currMV->x=max_dx; - } + { + currMV->x=max_dx; + } if (currMV->x < min_dx) - { - currMV->x=min_dx; - } + { + currMV->x=min_dx; + } if (currMV->y > max_dy) - { - currMV->y=max_dy; - } + { + currMV->y=max_dy; + } if (currMV->y < min_dy) - { - currMV->y=min_dy; - } + { + currMV->y=min_dy; + } iMinSAD = sad16( cur, - get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, iEdgedWidth), - iEdgedWidth, MV_MAX_ERROR); + get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, iEdgedWidth), + iEdgedWidth, MV_MAX_ERROR); iMinSAD += calc_delta_16(currMV->x-pmv[0].x, currMV->y-pmv[0].y, (uint8_t)iFcode) * iQuant; if ( (iMinSAD < 256 ) || ( (MVequal(*currMV,pMB->mvs[0])) && (iMinSAD < pMB->sad16) ) ) - { + { - if (MotionFlags & PMV_QUICKSTOP16) - goto step10b; - if (MotionFlags & PMV_EARLYSTOP16) - goto step10; - } + if (MotionFlags & PMV_QUICKSTOP16) + goto step10b; + if (MotionFlags & PMV_EARLYSTOP16) + goto step10; + } /* -Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block. - Also calculate (0,0) but do not subtract offset. - Let MinSAD be the smallest SAD up to this point. - If MV is (0,0) subtract offset. ******** WHAT'S THIS 'OFFSET' ??? *********** + Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block. + Also calculate (0,0) but do not subtract offset. + Let MinSAD be the smallest SAD up to this point. + If MV is (0,0) subtract offset. ******** WHAT'S THIS 'OFFSET' ??? *********** */ // (0,0) is always possible @@ -748,7 +756,7 @@ { if (!(MotionFlags & PMV_HALFPEL16 )) { pmv[1].x = EVEN(pmv[1].x); - pmv[1].y = EVEN(pmv[1].y); + pmv[1].y = EVEN(pmv[1].y); } CHECK_MV16_CANDIDATE(pmv[1].x,pmv[1].y); } @@ -758,7 +766,7 @@ { if (!(MotionFlags & PMV_HALFPEL16 )) { pmv[2].x = EVEN(pmv[2].x); - pmv[2].y = EVEN(pmv[2].y); + pmv[2].y = EVEN(pmv[2].y); } CHECK_MV16_CANDIDATE(pmv[2].x,pmv[2].y); @@ -767,7 +775,7 @@ { if (!(MotionFlags & PMV_HALFPEL16 )) { pmv[3].x = EVEN(pmv[3].x); - pmv[3].y = EVEN(pmv[3].y); + pmv[3].y = EVEN(pmv[3].y); } CHECK_MV16_CANDIDATE(pmv[3].x,pmv[3].y); } @@ -778,31 +786,31 @@ */ if ( (iMinSAD <= threshA) || ( MVequal(*currMV,pMB->mvs[0]) && (iMinSAD < pMB->sad16) ) ) - { - if (MotionFlags & PMV_QUICKSTOP16) - goto step10b; - if (MotionFlags & PMV_EARLYSTOP16) - goto step10; - } + { + if (MotionFlags & PMV_QUICKSTOP16) + goto step10b; + if (MotionFlags & PMV_EARLYSTOP16) + goto step10; + } /************ (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. + 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 = PMVfastSearch16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur, - x, y, - currMV->x, currMV->y, iMinSAD, &newMV, - pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); + x, y, + currMV->x, currMV->y, iMinSAD, &newMV, + pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); if (iSAD < iMinSAD) { @@ -816,43 +824,43 @@ if (!(MVequal(pmv[0],backupMV)) ) { iSAD = PMVfastSearch16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur, - x, y, - pmv[0].x, pmv[0].y, iMinSAD, &newMV, - pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); + x, y, + pmv[0].x, pmv[0].y, iMinSAD, &newMV, + pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); - if (iSAD < iMinSAD) - { - *currMV = newMV; - iMinSAD = iSAD; - } + if (iSAD < iMinSAD) + { + *currMV = newMV; + iMinSAD = iSAD; + } } if ( (!(MVzero(pmv[0]))) && (!(MVzero(backupMV))) ) { iSAD = PMVfastSearch16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur, - x, y, - 0, 0, iMinSAD, &newMV, - pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); + x, y, + 0, 0, iMinSAD, &newMV, + pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); - if (iSAD < iMinSAD) - { - *currMV = newMV; - iMinSAD = iSAD; - } + if (iSAD < iMinSAD) + { + *currMV = newMV; + iMinSAD = iSAD; + } } } /* - Step 10: The motion vector is chosen according to the block corresponding to MinSAD. + Step 10: The motion vector is chosen according to the block corresponding to MinSAD. */ -step10: + step10: if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step iMinSAD = PMVfastSearch16_Refine( pRef, pRefH, pRefV, pRefHV, cur, - x, y, - currMV, iMinSAD, - pmv, min_dx, max_dx, min_dy, max_dy, iFcode, iQuant, iEdgedWidth); + x, y, + currMV, iMinSAD, + pmv, min_dx, max_dx, min_dy, max_dy, iFcode, iQuant, iEdgedWidth); -step10b: + step10b: currPMV->x = currMV->x - pmv[0].x; currPMV->y = currMV->y - pmv[0].y; return iMinSAD; @@ -864,23 +872,23 @@ int32_t PMVfastSearch8_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, - int32_t startx, int32_t starty, - int32_t iMinSAD, - VECTOR * const currMV, - const VECTOR * const pmv, - const int32_t min_dx, const int32_t max_dx, - const int32_t min_dy, const int32_t max_dy, - const int32_t iEdgedWidth, - const int32_t iDiamondSize, - const int32_t iFcode, - const int32_t iQuant, - int iFound) + const uint8_t * const pRef, + const uint8_t * const pRefH, + const uint8_t * const pRefV, + const uint8_t * const pRefHV, + const uint8_t * const cur, + const int x, const int y, + int32_t startx, int32_t starty, + int32_t iMinSAD, + VECTOR * const currMV, + const VECTOR * const pmv, + const int32_t min_dx, const int32_t max_dx, + const int32_t min_dy, const int32_t max_dy, + const int32_t iEdgedWidth, + const int32_t iDiamondSize, + const int32_t iFcode, + const int32_t iQuant, + int iFound) { /* Do a diamond search around given starting point, return SAD of best */ @@ -913,28 +921,28 @@ CHECK_MV8_CANDIDATE_FOUND(backupMV.x,backupMV.y+iDiamondSize,4); } else - { - currMV->x = startx; - currMV->y = starty; - } + { + currMV->x = startx; + currMV->y = starty; + } return iMinSAD; } int32_t PMVfastSearch8_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 VECTOR * const pmv, - const int32_t min_dx, const int32_t max_dx, - const int32_t min_dy, const int32_t max_dy, - const int32_t iFcode, - const int32_t iQuant, - const int32_t iEdgedWidth) + 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 VECTOR * const pmv, + const int32_t min_dx, const int32_t max_dx, + const int32_t min_dy, const int32_t max_dy, + const int32_t iFcode, + const int32_t iQuant, + const int32_t iEdgedWidth) { /* Do a half-pel refinement (or rather a "smallest possible amount" refinement) */ @@ -957,18 +965,18 @@ #define PMV_HALFPEL8 (PMV_HALFPELDIAMOND8|PMV_HALFPELREFINE8) int32_t PMVfastSearch8( - const uint8_t * const pRef, - const uint8_t * const pRefH, - const uint8_t * const pRefV, - const uint8_t * const pRefHV, - const IMAGE * const pCur, - const int x, const int y, - const int start_x, int start_y, - const uint32_t MotionFlags, - MBParam * const pParam, - MACROBLOCK * const pMBs, - VECTOR * const currMV, - VECTOR * const currPMV) + 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, int start_y, + const uint32_t MotionFlags, + MBParam * const pParam, + MACROBLOCK * const pMBs, + VECTOR * const currMV, + VECTOR * const currPMV) { const uint32_t iWcount = pParam->mb_width; @@ -1001,16 +1009,16 @@ int32_t iSubBlock = ((y&1)<<1) + (x&1); /* Get maximum range */ - get_range(&min_dx, &max_dx, &min_dy, &max_dy, - x, y, 8, iWidth, iHeight, iFcode); + get_range(&min_dx, &max_dx, &min_dy, &max_dy, + x, y, 8, iWidth, iHeight, iFcode); /* we work with abs. MVs, not relative to prediction, so range is relative to 0,0 */ if (!(MotionFlags & PMV_HALFPELDIAMOND8 )) { min_dx = EVEN(min_dx); - max_dx = EVEN(max_dx); - min_dy = EVEN(min_dy); - max_dy = EVEN(max_dy); + max_dx = EVEN(max_dx); + min_dy = EVEN(min_dy); + max_dy = EVEN(max_dy); } /* because we might use IF (dx>max_dx) THEN dx=max_dx; */ @@ -1034,15 +1042,15 @@ iFound=0; /* Step 2: Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion - vector of the median. - If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2 + vector of the median. + If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2 */ if ((bPredEq) && (MVequal(pmv[0],pMB->mvs[iSubBlock]) ) ) iFound=2; /* Step 3: If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search. - Otherwise select large Diamond Search. + Otherwise select large Diamond Search. */ if ( (pmv[0].x != 0) || (pmv[0].y != 0) || (threshB<1536/4) || (bPredEq) ) @@ -1054,10 +1062,10 @@ iDiamondSize*=2; /* Step 4: Calculate SAD around the Median prediction. - MinSAD=SAD - If Motion Vector equal to Previous frame motion vector - and MinSADy=start_y; iMinSAD = sad8( cur, - get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, iEdgedWidth), - iEdgedWidth); + get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 8, currMV, iEdgedWidth), + iEdgedWidth); iMinSAD += calc_delta_8(currMV->x - pmv[0].x, currMV->y - pmv[0].y, (uint8_t)iFcode) * iQuant; if ( (iMinSAD < 256/4 ) || ( (MVequal(*currMV,pMB->mvs[iSubBlock])) && (iMinSAD < pMB->sad8[iSubBlock]) ) ) - { - if (MotionFlags & PMV_QUICKSTOP8) - goto step10_8b; - if (MotionFlags & PMV_EARLYSTOP8) - goto step10_8; - } + { + if (MotionFlags & PMV_QUICKSTOP8) + goto step10_8b; + if (MotionFlags & PMV_EARLYSTOP8) + goto step10_8; + } /* -Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block. - Also calculate (0,0) but do not subtract offset. - Let MinSAD be the smallest SAD up to this point. - If MV is (0,0) subtract offset. ******** WHAT'S THIS 'OFFSET' ??? *********** + Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block. + Also calculate (0,0) but do not subtract offset. + Let MinSAD be the smallest SAD up to this point. + If MV is (0,0) subtract offset. ******** WHAT'S THIS 'OFFSET' ??? *********** */ // the prediction might be even better than mv16 @@ -1100,7 +1108,7 @@ { if (!(MotionFlags & PMV_HALFPEL8 )) { pmv[1].x = EVEN(pmv[1].x); - pmv[1].y = EVEN(pmv[1].y); + pmv[1].y = EVEN(pmv[1].y); } CHECK_MV8_CANDIDATE(pmv[1].x,pmv[1].y); } @@ -1110,17 +1118,17 @@ { if (!(MotionFlags & PMV_HALFPEL8 )) { pmv[2].x = EVEN(pmv[2].x); - pmv[2].y = EVEN(pmv[2].y); + pmv[2].y = EVEN(pmv[2].y); } CHECK_MV8_CANDIDATE(pmv[2].x,pmv[2].y); // top right neighbour, if allowed if (psad[3] != MV_MAX_ERROR) { - if (!(MotionFlags & PMV_HALFPEL8 )) - { pmv[3].x = EVEN(pmv[3].x); + 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); } } @@ -1130,30 +1138,30 @@ */ if ( (iMinSAD <= threshA) || ( MVequal(*currMV,pMB->mvs[iSubBlock]) && (iMinSAD < pMB->sad8[iSubBlock]) ) ) - { - if (MotionFlags & PMV_QUICKSTOP8) - goto step10_8b; - if (MotionFlags & PMV_EARLYSTOP8) - goto step10_8; - } + { + if (MotionFlags & PMV_QUICKSTOP8) + goto step10_8b; + if (MotionFlags & PMV_EARLYSTOP8) + goto step10_8; + } /************ (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. + 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 = PMVfastSearch8_MainSearch(pRef, pRefH, pRefV, pRefHV, cur, - x, y, - currMV->x, currMV->y, iMinSAD, &newMV, - pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); + x, y, + currMV->x, currMV->y, iMinSAD, &newMV, + pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); if (iSAD < iMinSAD) { @@ -1167,43 +1175,43 @@ if (!(MVequal(pmv[0],backupMV)) ) { iSAD = PMVfastSearch16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur, - x, y, - pmv[0].x, pmv[0].y, iMinSAD, &newMV, - pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); + x, y, + pmv[0].x, pmv[0].y, iMinSAD, &newMV, + pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); - if (iSAD < iMinSAD) - { - *currMV = newMV; - iMinSAD = iSAD; - } + if (iSAD < iMinSAD) + { + *currMV = newMV; + iMinSAD = iSAD; + } } if ( (!(MVzero(pmv[0]))) && (!(MVzero(backupMV))) ) { iSAD = PMVfastSearch16_MainSearch(pRef, pRefH, pRefV, pRefHV, cur, - x, y, - 0, 0, iMinSAD, &newMV, - pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); + x, y, + 0, 0, iMinSAD, &newMV, + pmv, min_dx, max_dx, min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, iQuant, iFound); - if (iSAD < iMinSAD) - { - *currMV = newMV; - iMinSAD = iSAD; - } + if (iSAD < iMinSAD) + { + *currMV = newMV; + iMinSAD = iSAD; + } } } /* Step 10: The motion vector is chosen according to the block corresponding to MinSAD. - By performing an optional local half-pixel search, we can refine this result even further. + By performing an optional local half-pixel search, we can refine this result even further. */ -step10_8: + step10_8: if (MotionFlags & PMV_HALFPELREFINE8) // perform final half-pel step iMinSAD = PMVfastSearch8_Refine( pRef, pRefH, pRefV, pRefHV, cur, - x, y, - currMV, iMinSAD, - pmv, min_dx, max_dx, min_dy, max_dy, iFcode, iQuant, iEdgedWidth); + x, y, + currMV, iMinSAD, + pmv, min_dx, max_dx, min_dy, max_dy, iFcode, iQuant, iEdgedWidth); -step10_8b: + step10_8b: currPMV->x = currMV->x - pmv[0].x; currPMV->y = currMV->y - pmv[0].y;