⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 motest.cpp

📁 此源码是在VC平台下,实现MPEG4编解码的源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	}

#if 1				// #if 0 to temporarily disable direct mode (debug only)
	// Check direct mode
	// new changes
/*	Int iSADDirect;
	if (bColocatedMBExist) {
		iSADDirect = (pmbmdRef->m_bFieldMV) ?
			directSADField(x, y, pmbmd, pmbmdRef, pmvRef, ppxlcRef0MBY, ppxlcRef1MBY) :
			directSAD     (x, y, pmbmd, pmbmdRef, pmvRef);
		if ((iSADDirect - BbiasFavorDirect) < iSAD) {
			pmbmd->m_mbType = DIRECT;
			pmbmd->m_bFieldMV = pmbmdRef->m_bFieldMV;
			iSADnobias = iSADDirect;
		}
	}  */
#endif
#ifdef __TRACE_AND_STATS_
/*	static char *cDirectType[] = {	"FrmDirectSAD", "FldDirectSAD" };
	static char *cWinner[] = { "FrmDirect\n", "FrmAve\n", "FrmBak\n", "FrmFwd\n", "FldDirect\n", "FldAve\n", "FldBak\n", "FldFwd\n" };
	if (bColocatedMBExist)
		m_pbitstrmOut->trace (iSADDirect, cDirectType[pmbmdRef->m_bFieldMV]);
	m_pbitstrmOut->trace (cWinner[(Int)pmbmd->m_mbType + 4*pmbmd->m_bFieldMV]); */
#if 0				// Put all MV candidates in trace file
	Int iMV[10];
	iMV[0] = pmvForward[0].m_vctTrueHalfPel.x; iMV[1] = pmvForward[0].m_vctTrueHalfPel.y;
	iMV[2] = pmvForward[1].m_vctTrueHalfPel.x; iMV[3] = pmvForward[1].m_vctTrueHalfPel.y;
	iMV[4] = pmvForward[2].m_vctTrueHalfPel.x; iMV[5] = pmvForward[2].m_vctTrueHalfPel.y;
	iMV[6] = pmvForward[3].m_vctTrueHalfPel.x; iMV[7] = pmvForward[3].m_vctTrueHalfPel.y;
	iMV[8] = pmvForward[4].m_vctTrueHalfPel.x; iMV[9] = pmvForward[4].m_vctTrueHalfPel.y;
	m_pbitstrmOut->trace (iMV, 10, "FwdMV");
	iMV[0] = pmvBackward[0].m_vctTrueHalfPel.x; iMV[1] = pmvBackward[0].m_vctTrueHalfPel.y;
	iMV[2] = pmvBackward[1].m_vctTrueHalfPel.x; iMV[3] = pmvBackward[1].m_vctTrueHalfPel.y;
	iMV[4] = pmvBackward[2].m_vctTrueHalfPel.x; iMV[5] = pmvBackward[2].m_vctTrueHalfPel.y;
	iMV[6] = pmvBackward[3].m_vctTrueHalfPel.x; iMV[7] = pmvBackward[3].m_vctTrueHalfPel.y;
	iMV[8] = pmvBackward[4].m_vctTrueHalfPel.x; iMV[9] = pmvBackward[4].m_vctTrueHalfPel.y;
	m_pbitstrmOut->trace (iMV, 10, "BakMV");
#endif
#endif // __TRACE_AND_STATS_

	pmbmd->m_bhas4MVForward  = FALSE;
	pmbmd->m_bhas4MVBackward = FALSE;
	return iSADnobias;
}

//end of new changes
//end of INTERLACED

Int CVideoObjectEncoder::blkmatch16 (
	CMotionVector* pmv, 
	CoordI iXRef, CoordI iYRef,
	CoordI iXCurr, CoordI iYCurr,
	Int iMinSAD,
	const PixelC* ppxlcRefMBY,
	const CU8Image* puciRefQZoomY,
	Int iSearchRange
)
{
	Int mbDiff; // start with big
	Int iXDest = iXRef, iYDest = iYRef;
	CoordI x = iXRef, y = iYRef, ix, iy;
	Int uiPosInLoop, iLoop;

	if (m_iMVFileUsage != 1) {
	    // Spiral Search for the rest
	    for (iLoop = 1; iLoop <= iSearchRange; iLoop++) {
		    x++;
		    y++;
		    ppxlcRefMBY += m_iFrameWidthY + 1;
		    for (uiPosInLoop = 0; uiPosInLoop < (iLoop << 3); uiPosInLoop++) { // inside each spiral loop 
				if (
				    x >= m_rctRefVOPY0.left && y >= m_rctRefVOPY0.top && 
				    x <= m_rctRefVOPY0.right - MB_SIZE && y <= m_rctRefVOPY0.bottom - MB_SIZE
			    ) {
				    const PixelC* ppxlcTmpC = m_ppxlcCurrMBY;
				    const PixelC* ppxlcRefMB = ppxlcRefMBY;
				    mbDiff = 0;
				    for (iy = 0; iy < MB_SIZE; iy++) {
					    for (ix = 0; ix < MB_SIZE; ix++)
						    mbDiff += abs (ppxlcTmpC [ix] - ppxlcRefMB [ix]);
					    if (mbDiff >= iMinSAD)
						    goto NEXT_POSITION; // skip the current position
					    ppxlcRefMB += m_iFrameWidthY;
					    ppxlcTmpC += MB_SIZE;
				    }
				    iMinSAD = mbDiff;
				    iXDest = x;
				    iYDest = y;
			    }
NEXT_POSITION:	
			    if (uiPosInLoop < (iLoop << 1)) {
				    ppxlcRefMBY--;
				    x--;
			    }
			    else if (uiPosInLoop < (iLoop << 2)) {
				    ppxlcRefMBY -= m_iFrameWidthY;
				    y--;
			    }
			    else if (uiPosInLoop < (iLoop * 6)) {
				    ppxlcRefMBY++;					  
				    x++;
			    }
			    else {
				    ppxlcRefMBY += m_iFrameWidthY;
				    y++;
			    }
		    }
	    }
	    *pmv = CMotionVector (iXDest - iXCurr, iYDest - iYCurr);
	} else {
		iXDest = iXRef + pmv->iMVX;
		iYDest = iYRef + pmv->iMVY;
	}
/* NBIT: change to a bigger number
	iMinSAD = 256*256;		// Reset iMinSAD because reference VOP changed from original to reconstructed (Bob Eifrig)
*/
	iMinSAD = 4096*256;
/* NBIT: change
	Int iFavorZero = pmv->isZero() ? -FAVORZERO : 0;
*/
	Int iFavorZero = pmv->isZero() ? FAVORZERO : 0;
	Int nBits = m_volmd.nBits;
	if (nBits > 8) {
		iFavorZero <<= (nBits-8);
	} else if (nBits < 8) {
		iFavorZero >>= (8-nBits);
	}
	iFavorZero = -iFavorZero;

	// half pel search
	I8 destHalfX = 0, destHalfY = 0;
	Int lowX = (iXDest == m_rctRefVOPY0.left) ? 0 : -1;
	Int lowY = (iYDest == m_rctRefVOPY0.top) ? 0 : -1;
	Int highX = (iXDest == m_rctRefVOPY0.right - MB_SIZE + 1) ? 0 : 1;
	Int highY = (iYDest == m_rctRefVOPY0.bottom - MB_SIZE + 1) ? 0 : 1;
	const PixelC* ppxlcRefZoom = puciRefQZoomY->pixels ((iXDest << 1) + lowX, (iYDest << 1) + lowY);
	for (Int incY = lowY; incY <= highY; incY++) {
		for (Int incX = lowX; incX <= highX; incX++) {
			const PixelC* ppxlcRefZoomMB = ppxlcRefZoom;
			const PixelC* ppxlcTmpC = m_ppxlcCurrMBY;
			mbDiff = (incX || incY) ? 0 : iFavorZero;
			for (iy = 0; iy < MB_SIZE; iy++) {
				for (ix = 0; ix < MB_SIZE; ix++)
					mbDiff += abs (ppxlcTmpC [ix] - ppxlcRefZoomMB [2 * ix]);
				if (mbDiff > iMinSAD)			// Try to find shortest MV in case of equal SADs
					goto NEXT_HALF_POSITION;
				ppxlcRefZoomMB += m_iFrameWidthZoomY * 2;
				ppxlcTmpC += MB_SIZE;
			}
			if ((iMinSAD > mbDiff) ||
				((abs(pmv->m_vctTrueHalfPel.x + destHalfX) + abs(pmv->m_vctTrueHalfPel.y + destHalfY)) >
				 (abs(pmv->m_vctTrueHalfPel.x + incX)      + abs(pmv->m_vctTrueHalfPel.y + incY)))) {
				iMinSAD = mbDiff;
				destHalfX = incX;
				destHalfY = incY;
			}
NEXT_HALF_POSITION:
			ppxlcRefZoom++;
		}
		ppxlcRefZoom += m_iFrameWidthZoomY - (highX - lowX + 1);
	}
	pmv -> iHalfX = destHalfX;
	pmv -> iHalfY = destHalfY;
	return iMinSAD;
}

Int CVideoObjectEncoder::blkmatch16WithShape (
	CMotionVector* pmv, 
	CoordI iXRef, CoordI iYRef,
	CoordI iXCurr, CoordI iYCurr,
	Int iMinSAD,
	const PixelC* ppxlcRefMBY,
	const CU8Image* puciRefQZoom,
    const CMBMode *pmbmd,
	Int iSearchRange,
	Int iDirection
)
{
	Int mbDiff; // start with big
	Int iXDest = iXRef, iYDest = iYRef;
	CoordI x = iXRef, y = iYRef, ix, iy;
	Int uiPosInLoop, iLoop;

	CRct CTmp=(iDirection) ? m_rctRefVOPY1:m_rctRefVOPY0;

	// Spiral Search for the rest
	if (m_iMVFileUsage != 1) {
	    for (iLoop = 1; iLoop <= iSearchRange; iLoop++) {
		    x++;
		    y++;
		    ppxlcRefMBY += m_iFrameWidthY + 1;
		    for (uiPosInLoop = 0; uiPosInLoop < (iLoop << 3); uiPosInLoop++) { // inside each spiral loop 
				if (
				    x >= CTmp.left && y >= CTmp.top && 
				    x <= CTmp.right - MB_SIZE && y <= CTmp.bottom - MB_SIZE
			    ) {
				    const PixelC* ppxlcTmpC = m_ppxlcCurrMBY;
				    const PixelC* ppxlcTmpCBY = m_ppxlcCurrMBBY;
				    const PixelC* ppxlcRefMB = ppxlcRefMBY;
				    mbDiff = 0;
				    for (iy = 0; iy < MB_SIZE; iy++) {
					    for (ix = 0; ix < MB_SIZE; ix++) {
						    if (ppxlcTmpCBY [ix] != transpValue)
							    mbDiff += abs (ppxlcTmpC [ix] - ppxlcRefMB [ix]);
					    }
					    if (mbDiff >= iMinSAD)
						    goto NEXT_POSITION; // skip the current position
					    ppxlcRefMB += m_iFrameWidthY;
					    ppxlcTmpC += MB_SIZE;
					    ppxlcTmpCBY += MB_SIZE;
				    }
				    iMinSAD = mbDiff;
				    iXDest = x;
				    iYDest = y;
			    }
NEXT_POSITION:	
			    if (uiPosInLoop < (iLoop << 1)) {
				    ppxlcRefMBY--;
				    x--;
			    }
			    else if (uiPosInLoop < (iLoop << 2)) {
				    ppxlcRefMBY -= m_iFrameWidthY;
				    y--;
			    }
			    else if (uiPosInLoop < (iLoop * 6)) {
				    ppxlcRefMBY++;					  
				    x++;
			    }
			    else {
				    ppxlcRefMBY += m_iFrameWidthY;
				    y++;
			    }
		    }
	    }
	    *pmv = CMotionVector (iXDest - iXCurr, iYDest - iYCurr);
	} 
	else {
		iXDest = iXRef + pmv->iMVX;
		iYDest = iYRef + pmv->iMVY;
	}
	// check for case when zero MV due to MB outside the extended BB.
	if(iXDest==iXCurr && iYDest==iYCurr
		&& (iXDest < CTmp.left || iYDest < CTmp.top	// 12.22.98 changes
			|| iXDest > CTmp.right - MB_SIZE		// 12.22.98 changes
			|| iYDest > CTmp.bottom - MB_SIZE))		// 12.22.98 changes
	{
		pmv -> iHalfX = 0;
		pmv -> iHalfY = 0;
		return iMinSAD;  
	}

/* NBIT: change to a bigger number
	iMinSAD = 256*256;		// Reset iMinSAD because reference VOP may change from original to reconstructed (Bob Eifrig)
*/
	iMinSAD = 4096*256;
	Int iFavorZero = pmv->isZero() ? -(pmbmd -> m_rgNumNonTranspPixels [0] >> 1) - 1 : 0;	
	// half pel search
	I8 destHalfX = 0, destHalfY = 0;
	Int lowY, highY, lowX, highX;
	lowX = (iXDest == CTmp.left) ? 0 : -1;	// 12.22.98 changes
	lowY = (iYDest == CTmp.top) ? 0 : -1;	// 12.22.98 changes
	highX = (iXDest == CTmp.right - MB_SIZE + 1) ? 0 : 1;	// 12.22.98 changes
	highY = (iYDest == CTmp.bottom - MB_SIZE + 1) ? 0 : 1;	// 12.22.98 changes
	const PixelC* ppxlcRefZoom = puciRefQZoom->pixels ((iXDest << 1) + lowX, (iYDest << 1) + lowY);
	for (Int incY = lowY; incY <= highY; incY++) {
		for (Int incX = lowX; incX <= highX; incX++) {
			const PixelC* ppxlcRefZoomMB = ppxlcRefZoom;
			const PixelC* ppxlcTmpC = m_ppxlcCurrMBY;
			const PixelC* ppxlcTmpCBY = m_ppxlcCurrMBBY;
			mbDiff = (incX || incY) ? 0 : iFavorZero;
			for (iy = 0; iy < MB_SIZE; iy++) {
				for (ix = 0; ix < MB_SIZE; ix++) {
					if (ppxlcTmpCBY [ix] != transpValue) 
						mbDiff += abs (ppxlcTmpC [ix] - ppxlcRefZoomMB [2 * ix]);
				}
				if (mbDiff > iMinSAD)			// Try to find shortest MV in case of equal SADs
					goto NEXT_HALF_POSITION;
				ppxlcRefZoomMB += m_iFrameWidthZoomY * 2;
				ppxlcTmpC += MB_SIZE;
				ppxlcTmpCBY += MB_SIZE;
			}
			if ((iMinSAD > mbDiff) ||
				((abs(pmv->m_vctTrueHalfPel.x + destHalfX) + abs(pmv->m_vctTrueHalfPel.y + destHalfY)) >
				 (abs(pmv->m_vctTrueHalfPel.x + incX)      + abs(pmv->m_vctTrueHalfPel.y + incY)))) {
				iMinSAD = mbDiff;
				destHalfX = incX;
				destHalfY = incY;
			}
NEXT_HALF_POSITION:
			ppxlcRefZoom++;
		}
		ppxlcRefZoom += m_iFrameWidthZoomY - (highX - lowX + 1);
	}
	pmv -> iHalfX = destHalfX;
	pmv -> iHalfY = destHalfY;
	return iMinSAD;
}

Int CVideoObjectEncoder::blockmatch8 (
	const PixelC* ppxlcCodedBlkY, 
	CMotionVector* pmv8, 
	CoordI iXBlk, CoordI iYBlk,
	const CMotionVector* pmvPred,
	Int iSearchRange
)
{
	Int mbDiff,iMinSAD; // start with big
	CoordI ix,iy,iXDest,iYDest;

	if (m_iMVFileUsage != 1) {
	    CoordI x = iXBlk + pmvPred->iMVX, y = iYBlk + pmvPred->iMVY; // , ix, iy;
	    CoordI left = -1 * min (abs (pmvPred->iMVX + iSearchRange), ADD_DISP);
	    CoordI top = -1 * min (abs (pmvPred->iMVY + iSearchRange), ADD_DISP);
	    CoordI right = min (abs (pmvPred->iMVX - iSearchRange), ADD_DISP);
	    CoordI bottom = min (abs (pmvPred->iMVY - iSearchRange), ADD_DISP);

	    CoordI iOrigX = x, iOrigY = y;
	    // CoordI 
	    iXDest = x, iYDest = y;
	    Int uiPosInLoop, iLoop;
	    const PixelC* ppxlcRefBlk = (m_volmd.bOriginalForME ? m_pvopcRefOrig0 : m_pvopcRefQ0)->getPlane (Y_PLANE)->pixels (x, y);
	    iMinSAD = sad8x8At0 (ppxlcCodedBlkY, ppxlcRefBlk);
	    // Spiral Search for the rest
	    Int iix = 0, iiy = 0;
	    for (iLoop = 1; iLoop <= ADD_DISP; iLoop++) {
		    x++;
		    y++;
		    iix++; iiy++;
		    ppxlcRefBlk += m_iFrameWidthY + 1;
		    for (uiPosInLoop = 0; uiPosInLoop < (iLoop << 3); uiPosInLoop++) { // inside each spiral loop 
				if (
				    iix >= left && iix <= right &&
				    iiy >= top && iiy <= bottom &&
				    x >= m_rctRefVOPY0.left && y >= m_rctRefVOPY0.top && 
				    x <= m_rctRefVOPY0.right - MB_SIZE && y <= m_rctRefVOPY0.bottom - MB_SIZE
			    ) {
				    const PixelC* ppxlcTmpC = ppxlcCodedBlkY;
				    const PixelC* ppxlcRefMB = ppxlcRefBlk;
				    mbDiff = 0;
				    for (iy = 0; iy < BLOCK_SIZE; iy++) {
					    for (ix = 0; ix < BLOCK_SIZE; ix++)
						    mbDiff += abs (ppxlcTmpC [ix] - ppxlcRefMB [ix]);
					    if (mbDiff > iMinSAD)
						    goto NEXT_POSITION; // skip the current position
					    ppxlcRefMB += m_iFrameWidthY;
					    ppxlcTmpC += MB_SIZE;
				    }
				    iMinSAD = mbDiff;
				    iXDest = x;
				    iYDest = y;
			    }
NEXT_POSITION:	
			    if (uiPosInLoop < (iLoop << 1)) {
				    ppxlcRefBlk--;
				    x--;
				    iix--;
			    }
			    else if (uiPosInLoop < (iLoop << 2)) {
				    ppxlcRefBlk -= m_iFrameWidthY;
				    y--;
				    iiy--;
			    }
			    else if (uiPosInLoop < (iLoop * 6)) {
				    ppxlcRefBlk++;					  
				    x++;
				    iix++;
			    }
			    else {
				    ppxlcRefBlk += m_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -