📄 motion_est.c
字号:
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_xi, const int start_yi, 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; int start_x = start_xi, start_y = start_yi;/* 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 */ else { bDirection = iDirection; start_x = currMV->x; start_y = currMV->y; } } } while (1); /*forever */ } return iMinSAD;}int32_tFull8_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_tHalfpel16_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_tPMVfastSearch16(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, /* start is searched first, so it should contain the most */ const int start_y, /* likely motion vector for this block */ const int center_x, /* center is from where length of MVs is measured */ 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; const uint8_t *cur = pCur->y + x * 16 + y * 16 * iEdgedWidth; int32_t iDiamondSize; int32_t min_dx; int32_t max_dx; int32_t min_dy; int32_t max_dy; int32_t iFound; VECTOR newMV; VECTOR backupMV; /* just for PMVFAST */ VECTOR pmv[4]; int32_t psad[4]; MainSearch16FuncPtr MainSearchPtr; const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount; int32_t threshA, threshB; int32_t bPredEq; int32_t iMinSAD, iSAD;/* Get maximum range */ get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight, iFcode);/* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */ if (!(MotionFlags & PMV_HALFPEL16)) { min_dx = EVEN(min_dx); max_dx = EVEN(max_dx); min_dy = EVEN(min_dy); max_dy = EVEN(max_dy); } /* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */ bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad); if ((x == 0) && (y == 0)) { threshA = 512; threshB = 1024; } else { threshA = psad[0]; threshB = threshA + 256; if (threshA < 512) threshA = 512; if (threshA > 1024) threshA = 1024; if (threshB > 1792) threshB = 1792; } 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. */ currMV->x = start_x; currMV->y = start_y; if (!(MotionFlags & PMV_HALFPEL16)) { /* This should NOT be necessary! */ currMV->x = EVEN(currMV->x); currMV->y = EVEN(currMV->y); } if (currMV->x > max_dx) { currMV->x = max_dx; } if (currMV->x < min_dx) { currMV->x = min_dx; } if (currMV->y > max_dy) { currMV->y = max_dy; } if (currMV->y < min_dy) { currMV->y = min_dy; } 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); if ((iMinSAD < 256) || ((MVequal(*currMV, prevMB->mvs[0])) && ((int32_t) iMinSAD < prevMB->sad16))) { if (iMinSAD < (int)(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; } } if (MotionFlags & PMV_QUICKSTOP16) goto PMVfast16_Terminate_without_Refine; if (MotionFlags & PMV_EARLYSTOP16) goto PMVfast16_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[0]))) iFound = 2;/* Step 3 (lazy eval): If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search. Otherwise select large Diamond Search. */ if ((!MVzero(pmv[0])) || (threshB < 1536) || (bPredEq)) iDiamondSize = 1; /* halfpel! */ else iDiamondSize = 2; /* halfpel! */ if (!(MotionFlags & PMV_HALFPELDIAMOND16)) iDiamondSize *= 2;/* 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. *//* (0,0) is always possible */ if (!MVzero(pmv[0])) CHECK_MV16_ZERO;/* previous frame MV is always possible */ if (!MVzero(prevMB->mvs[0])) if (!MVequal(prevMB->mvs[0], pmv[0])) CHECK_MV16_CANDIDATE(prevMB->mvs[0].x, prevMB->mvs[0].y);/* left neighbour, if allowed */ if (!MVzero(pmv[1])) if (!MVequal(pmv[1], prevMB->mvs[0])) if (!MVequal(pmv[1], pmv[0])) { if (!(MotionFlags & PMV_HALFPEL16)) { pmv[1].x = EVEN(pmv[1].x); pmv[1].y = EVEN(pmv[1].y); } CHECK_MV16_CANDIDATE(pmv[1].x, pmv[1].y); }/* top neighbour, if allowed */ if (!MVzero(pmv[2])) 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -