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

📄 mode.cpp

📁 au1200 linux2.6.11 硬件解码mae驱动和maiplayer播放器源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************This software module was originally developed by 	Ming-Chieh Lee (mingcl@microsoft.com), Microsoft Corporation	Wei-ge Chen (wchen@microsoft.com), Microsoft Corporation	Bruce Lin (blin@microsoft.com), Microsoft Corporation	Chuang Gu (chuanggu@microsoft.com), Microsoft Corporation	Simon Winder (swinder@microsoft.com), Microsoft Corporation	(date: March, 1996)and edited by	Yoshinori Suzuki (Hitachi, Ltd.)    Sehoon Son (shson@unitel.co.kr) Samsung AITin the course of development of the MPEG-4 Video (ISO/IEC 14496-2). This software module is an implementation of a part of one or more MPEG-4 Video tools as specified by the MPEG-4 Video. ISO/IEC gives users of the MPEG-4 Video free license to this software module or modifications thereof for use in hardware or software products claiming conformance to the MPEG-4 Video. Those intending to use this software module in hardware or software products are advised that its use may infringe existing patents. The original developer of this software module and his/her company, the subsequent editors and their companies, and ISO/IEC have no liability for use of this software module or modifications thereof in an implementation. Copyright is not released for non MPEG-4 Video conforming products. Microsoft retains full right to use the code for his/her own purpose, assign or donate the code to a third party and to inhibit third parties from using the code for non <MPEG standard> conforming products. This copyright notice must be included in all copies or derivative works. Copyright (c) 1996, 1997.Module Name:	mode.hAbstract:	basic coding modes for VOP and MBRevision History:	Dec. 12, 1997:	Interlaced tools added by NextLevel Systems        May. 9   1998:  add boundary by Hyundai Electronics                                  Cheol-Soo Park (cspark@super5.hyundai.co.kr)        May. 9   1998:  add field based MC padding by Hyundai Electronics                                  Cheol-Soo Park (cspark@super5.hyundai.co.kr)        Feb. 23  1999:  GMC added by Yoshinori Suzuki (Hitachi, Ltd.)	May 9, 1999:	tm5 rate control by DemoGraFX, duhoff@mediaone.net*************************************************************************/#include <string.h>#include <iostream.h>#include <math.h>#include "typeapi.h"#include "mode.hpp"#include "warp.hpp"#ifdef __MFC_#ifdef _DEBUG#undef THIS_FILEstatic char BASED_CODE THIS_FILE[] = __FILE__;#endif#define new DEBUG_NEW				   #endif // __MFC_// only works for 4:2:0#define MAX_NUM_MV 4//UInt uiNumBlks = 10;  // variable with MAC//#define uiNumBlks 10CMBMode::~CMBMode (){ 	delete [] m_rgbCodedBlockPattern; 	delete [] m_rgfltMinErrors;// MAC (SB) 26-Nov-99  delete [] m_pCODAlpha;      delete [] m_pbACPredictionAlpha;  delete [] m_preddir;//~MAC}CMBMode::CMBMode (Int iAuxCompCount): m_iAuxCompCount(iAuxCompCount){  UInt i;  m_uiNumBlks = 6+iAuxCompCount*4;    for (i = 0; i < 11; i++) {    m_rgTranspStatus [i] = NONE;    m_rgNumNonTranspPixels [i] = 0;  }  /*BBM// Added for Boundary by Hyundai(1998-5-9)  for (i = 0; i < uiNumBlks + 1; i++)  m_rgTranspStatusBBM[i] = NONE;  memset (m_bMerged, FALSE, 7 * sizeof(Bool));  // End of Hyundai(1998-5-9)*/    // Added for field based MC padding by Hyundai(1998-5-9)  for (i = 0; i < 5; i++)    m_rgFieldTranspStatus [i] = ALL;  memset (m_rgbFieldPadded, 0, 5 * sizeof(Bool));  // End of Hyundai(1998-5-9)    m_rgbCodedBlockPattern = new Bool [m_uiNumBlks];  memset (m_rgbCodedBlockPattern, FALSE, m_uiNumBlks * sizeof (Bool));  m_rgfltMinErrors = new Float [m_uiNumBlks];  memset (m_rgfltMinErrors, 0, m_uiNumBlks * sizeof (Float));  m_preddir = new IntraPredDirection [m_uiNumBlks];  m_bPadded = FALSE;	m_bSkip = FALSE;// GMC	m_bMCSEL = FALSE;// ~GMC	m_dctMd = INTRA;	m_shpmd = UNKNOWN;	m_shpssmd = UNDEFINED;				//OBSS_SAIT_991015	m_mbType = FORWARD;	m_intStepDelta = 0;	m_bhas4MVForward = FALSE; 	m_bhas4MVBackward = FALSE;	m_bFieldMV = FALSE;	m_bForwardTop = FALSE;	  	m_bForwardBottom = FALSE;	m_bBackwardTop = FALSE;		m_bBackwardBottom = FALSE;	m_bFieldDCT = FALSE;	m_bPerspectiveForward = FALSE;	m_bPerspectiveBackward = FALSE;	m_stepSize = 0;	m_bACPrediction = FALSE;	m_bInterShapeCoding = FALSE;	m_bColocatedMBSkip = FALSE;// GMC	m_bColocatedMBMCSEL = FALSE;// ~GMC	m_iVideoPacketNumber = 0;		m_vctDirectDeltaMV = CVector (0, 0);  m_pCODAlpha = new CODAlpha [m_iAuxCompCount];   // MAC (SB) 26-Nov-99  m_pbACPredictionAlpha = new Bool [m_iAuxCompCount];}CMBMode::CMBMode (const CMBMode& md): m_iAuxCompCount(md.m_iAuxCompCount),m_uiNumBlks(md.m_uiNumBlks){	memcpy (m_rgTranspStatus, md.m_rgTranspStatus, (11)  * sizeof (TransparentStatus));	memcpy (m_rgNumNonTranspPixels, md.m_rgNumNonTranspPixels, (11)  * sizeof (UInt));  m_preddir = new IntraPredDirection [m_uiNumBlks];  	memcpy (m_preddir,md.m_preddir,m_uiNumBlks * sizeof(IntraPredDirection));	/*BBM// Added for Boundary by Hyundai(1998-5-9)        memcpy (m_rgTranspStatusBBM, md.m_rgTranspStatusBBM, (uiNumBlks + 1)  * sizeof (TransparentStatus));        memcpy (m_bMerged, md.m_bMerged, 7 * sizeof (Bool));	// End of Hyundai(1998-5-9)*/	// Added for field based MC padding by Hyundai(1998-5-9)        memcpy (m_rgFieldTranspStatus, md.m_rgFieldTranspStatus, 5  * sizeof (TransparentStatus));		memcpy (m_rgbFieldPadded, md.m_rgbFieldPadded, 5 * sizeof (Bool));	// End of Hyundai(1998-5-9)	m_bPadded = md.m_bPadded;	m_bSkip = md.m_bSkip;// GMC	m_bMCSEL = md.m_bMCSEL;// ~GMC	m_mbType = md.m_mbType;	m_dctMd = md.m_dctMd;	m_shpmd = md.m_shpmd ;	m_shpssmd = md.m_shpssmd ;			//OBSS_SAIT_991015	m_intStepDelta = md.m_intStepDelta;	m_bhas4MVForward = md.m_bhas4MVForward; 	m_bhas4MVBackward = md.m_bhas4MVBackward; 	m_bPerspectiveForward = md.m_bPerspectiveForward;	m_bPerspectiveBackward = md.m_bPerspectiveBackward;	m_stepSize = md.m_stepSize;	m_bACPrediction = md.m_bACPrediction;	m_bInterShapeCoding = md.m_bInterShapeCoding;	m_bCodeDcAsAc = md.m_bCodeDcAsAc;	m_bColocatedMBSkip = md.m_bColocatedMBSkip;// GMC	m_bColocatedMBMCSEL = md.m_bColocatedMBMCSEL;// ~GMC	m_iVideoPacketNumber = md.m_iVideoPacketNumber;	m_rgbCodedBlockPattern = new Bool [m_uiNumBlks];	memcpy (m_rgbCodedBlockPattern, md.m_rgbCodedBlockPattern, m_uiNumBlks * sizeof (Bool));	m_rgfltMinErrors = new Float [m_uiNumBlks];	memcpy (m_rgfltMinErrors, md.m_rgfltMinErrors, m_uiNumBlks * sizeof (Float));// MAC  m_stepSizeAlpha       = md.m_stepSizeAlpha;  m_bCodeDcAsAcAlpha    = md.m_bCodeDcAsAcAlpha;  m_pbACPredictionAlpha = new Bool [m_iAuxCompCount];  m_pCODAlpha           = new CODAlpha [m_iAuxCompCount];  for(Int iAuxComp=0;iAuxComp<m_iAuxCompCount;iAuxComp++) {    m_pCODAlpha[iAuxComp]           = md.m_pCODAlpha[iAuxComp];    m_pbACPredictionAlpha[iAuxComp] = md.m_pbACPredictionAlpha[iAuxComp];  }//~MAC}Void CMBMode::operator = (const CMBMode& md){//  assert( m_uiNumBlks==md.m_uiNumBlks );  if (m_uiNumBlks!=md.m_uiNumBlks) {    delete [] m_rgbCodedBlockPattern;     delete [] m_rgfltMinErrors;    delete [] m_preddir;    m_uiNumBlks = 6+md.m_iAuxCompCount*4;    m_rgbCodedBlockPattern = new Bool [m_uiNumBlks];    m_rgfltMinErrors = new Float [m_uiNumBlks];    m_preddir = new IntraPredDirection [m_uiNumBlks];  }	memcpy (m_rgTranspStatus, md.m_rgTranspStatus, (11) * sizeof (TransparentStatus));	memcpy (m_rgNumNonTranspPixels, md.m_rgNumNonTranspPixels, (11) * sizeof (UInt));	memcpy (m_preddir,md.m_preddir,m_uiNumBlks * sizeof(IntraPredDirection));	/*BBM// Added for Boundary by Hyundai(1998-5-9)	memcpy (m_rgTranspStatusBBM, md.m_rgTranspStatusBBM, (uiNumBlks + 1)  * sizeof (TransparentStatus));        memcpy (m_bMerged, md.m_bMerged, 7 * sizeof (Bool));	// End of Hyundai(1998-5-9)*/	// Added for field based MC padding by Hyundai(1998-5-9)  memcpy (m_rgFieldTranspStatus, md.m_rgFieldTranspStatus, 5  * sizeof (TransparentStatus));  memcpy (m_rgbFieldPadded, md.m_rgbFieldPadded, 5 * sizeof (Bool));	// End of Hyundai(1998-5-9)	m_bPadded = md.m_bPadded;	m_bSkip = md.m_bSkip;// GMC	m_bMCSEL = md.m_bMCSEL;// ~GMC	m_mbType = md.m_mbType;	m_dctMd = md.m_dctMd;	m_shpmd = md.m_shpmd ;	m_intStepDelta = md.m_intStepDelta;	m_bhas4MVForward = md.m_bhas4MVForward; 	m_bhas4MVBackward = md.m_bhas4MVBackward; 	m_bPerspectiveForward = md.m_bPerspectiveForward;	m_bPerspectiveBackward = md.m_bPerspectiveBackward;	m_stepSize = md.m_stepSize;	m_bACPrediction = md.m_bACPrediction;	m_bInterShapeCoding = md.m_bInterShapeCoding;	m_bCodeDcAsAc = md.m_bCodeDcAsAc;	m_bColocatedMBSkip = md.m_bColocatedMBSkip;// GMC	m_bColocatedMBMCSEL = md.m_bColocatedMBMCSEL;// ~GMC	m_iVideoPacketNumber = md.m_iVideoPacketNumber;	memcpy (m_rgbCodedBlockPattern, md.m_rgbCodedBlockPattern, m_uiNumBlks * sizeof (Bool));	memcpy (m_rgfltMinErrors, md.m_rgfltMinErrors, m_uiNumBlks * sizeof (Float));// MAC  m_stepSizeAlpha      = md.m_stepSizeAlpha;  m_bCodeDcAsAcAlpha   = md.m_bCodeDcAsAcAlpha;  if (m_iAuxCompCount!=md.m_iAuxCompCount) {    delete [] m_pCODAlpha;    delete [] m_pbACPredictionAlpha;    m_iAuxCompCount = md.m_iAuxCompCount;    m_pCODAlpha = new CODAlpha [m_iAuxCompCount];    m_pbACPredictionAlpha = new Bool [m_iAuxCompCount];  }  for(Int iAuxComp=0;iAuxComp<m_iAuxCompCount;iAuxComp++) {    m_pCODAlpha[iAuxComp] = md.m_pCODAlpha[iAuxComp];    m_pbACPredictionAlpha[iAuxComp] = md.m_pbACPredictionAlpha[iAuxComp];  }//~MAC}Void CMBMode::setCodedBlockPattern (const Bool* rgbblockNum){ 	for (UInt i = 0; i < m_uiNumBlks; i++)		m_rgbCodedBlockPattern[i] = rgbblockNum [i];}Void CMBMode::setMinError (const Float* pfltminError){	for (UInt i = 0; i < m_uiNumBlks; i++)		m_rgfltMinErrors[i] = pfltminError [i];}CDirectModeData::~CDirectModeData (){	destroyMem ();}CDirectModeData::CDirectModeData (){	m_ppmbmd = NULL;	m_prgmv = NULL;}Bool CDirectModeData::inBound (UInt iMbIdx) const{	if (iMbIdx >= m_uiNumMB)		return FALSE;	else		return TRUE;}Bool CDirectModeData::inBound (UInt idX, UInt idY) const{	if (idX >= m_uiNumMBX || idY >= m_uiNumMBY || idX * idY >= m_uiNumMB)		return FALSE;	else		return TRUE;}Void CDirectModeData::reassign (UInt numMBX, UInt numMBY, Int iAuxCompCount){	destroyMem ();	m_uiNumMBX = numMBX;	m_uiNumMBY = numMBY;	m_uiNumMB = m_uiNumMBX * m_uiNumMBY;	m_ppmbmd = new CMBMode* [m_uiNumMB];	m_prgmv = new CMotionVector* [m_uiNumMB];	for (UInt i = 0; i < m_uiNumMB; i++) {		m_ppmbmd [i] = new CMBMode(iAuxCompCount);		m_prgmv [i] = new CMotionVector [5];	}}Void CDirectModeData::assign (UInt imb, const CMBMode& mbmd, const CMotionVector* rgmv){	assert (inBound (imb));	*(m_ppmbmd [imb]) = mbmd;	memcpy (m_prgmv [imb], rgmv, 5 * sizeof (CMotionVector));}Void CDirectModeData::destroyMem (){	if (m_ppmbmd != NULL) {		for (UInt i = 0; i < m_uiNumMB; i++)			delete m_ppmbmd [i];		delete [] m_ppmbmd;	}	if (m_prgmv != NULL) {		for (UInt i = 0; i < m_uiNumMB; i++)			delete [] m_prgmv [i];		delete [] m_prgmv;	}}Void CRCMode::reset (UInt uiFirstFrame, UInt uiLastFrame, UInt uiTemporalRate, Int iPCount, Int *piPVopQP,					   UInt uiBufferSize, Double mad, UInt uiBitsFirstFrame, Double dFrameHz){	if(m_FirstTime)		m_iPVopQP = *piPVopQP;	else		*piPVopQP = m_iPVopQP;	m_FirstTime = FALSE;	// rate control segment consists of initial I-vop followed by iPCount p-vops	// pcount = 0, means no pvops, but we dont try to do rate control in that case	if(iPCount * uiTemporalRate >= (uiLastFrame - uiFirstFrame + 1))	{		// no I-vops		m_NrFlag = TRUE;		m_Ts = (uiLastFrame - uiFirstFrame + 1) / dFrameHz; // seconds for the sequence	}	else	{		m_NrFlag = FALSE;		m_Ts = uiTemporalRate * (iPCount + 1) / dFrameHz; // seconds for the segment [IPP...PP]	}	m_Rs = uiBufferSize; // buffersize is # bits per second	m_Ns = uiTemporalRate; // distance between encoded frames	m_X1 = m_Rs * m_Ns / 2.0;				// 1st order coefficient transient	m_X2 = 0.0;								// 2nd order coeeficient transient	if(m_NrFlag)		m_Nr = (uiLastFrame - uiFirstFrame + 1) / uiTemporalRate - 1;  // number of pframes after current I	else		m_Nr = iPCount;	m_Nc = 0;	m_Hp = 500; // guess of header bits	m_Hc = 500; // guess of header bits	m_Rf = uiBitsFirstFrame;	m_Rc = uiBitsFirstFrame;	m_S = uiBitsFirstFrame;	m_Qc = 15;	m_Qp = 15;							// a simple solution (assumption)	m_Rr = (UInt) (m_Ts * m_Rs) - m_Rf;	// total number of bits available for this segment	m_Rp = m_Rr / m_Nr;					// average bits to be removed from the buffer	m_Bs = m_Rs / 2;					// assumed buffer size	m_B = m_Rs / 4;						// current buffer level	containing the bits for first frame	//m_B += uiBitsFirstFrame - 4 * m_Rp;		// assume I-vop takes 4 * p-vop bits//	if(m_B<0)	//	m_B = 0;	for (UInt i = 0; i < RC_MAX_SLIDING_WINDOW; i++) {// if 0, don't use that frame (rejected outliers)		m_rgQp[i] = 0;		m_rgRp[i] = 0.0;	}	m_Ec = mad; 	m_skipNextFrame = FALSE;}UInt CRCMode::updateQuanStepsize (UInt uiCurrQP){	// check that we have already been called (ie at lease 1 p-vop was done already)	if(m_Nc<1)		return uiCurrQP;	// Target bit calculation 	m_T = (UInt) max (m_Rs / 30.0, m_Rr / m_Nr * (1.0 - RC_PAST_PERCENT) + m_S * RC_PAST_PERCENT); // a minimum of Rs/30 is assigned to each frame	m_T = (UInt) (m_T * (m_B + 2.0 * (m_Bs - m_B)) / (2.0 * m_B + (m_Bs - m_B))); // increase if less than half, decrease if more than half, don't change if half	if ((m_B + m_T) >	(1.0 - RC_SAFETY_MARGIN) * m_Bs)		m_T = (UInt) max (m_Rs / 30.0, (1.0 - RC_SAFETY_MARGIN) * m_Bs - m_B); // to avoid buffer overflow	else if ((m_B - m_Rp + m_T) < RC_SAFETY_MARGIN * m_Bs)		m_T = m_Rp - m_B + (UInt) (RC_SAFETY_MARGIN * m_Bs);			  // to avoid buffer underflow	m_T = min (m_T, (UInt)m_Rr);		// Quantization level calculation	m_T = max (m_Rp / 3 + m_Hp, m_T);	Double dtmp = m_Ec * m_X1 * m_Ec * m_X1 + 4 * m_X2 * m_Ec * (m_T - m_Hp);	if ((m_X2 == 0.0) || (dtmp < 0) || ((sqrt (dtmp) - m_X1 * m_Ec) <= 0.0))	// fall back 1st order mode		m_Qc = (UInt) (m_X1 * m_Ec / (Double) (m_T - m_Hp));	else				// 2nd order mode		m_Qc = (UInt) ((2 * m_X2 * m_Ec) / (sqrt (dtmp) - m_X1 * m_Ec));	m_Qc = (UInt) min (ceil(m_Qp * (1.0 + RC_MAX_Q_INCREASE)), (Double) m_Qc);	// control variation	m_Qc = min (m_Qc, RC_MAX_QUANT);						// clipping	m_Qc = (UInt) max (ceil(m_Qp * (1.0 - RC_MAX_Q_INCREASE)), (Double) m_Qc);	// control variation	m_Qc = max (m_Qc, RC_MIN_QUANT);						// clipping		return m_Qc;}Bool CRCMode::skipThisFrame (){	printf("<%d %d %d>\n", m_B, m_Bs, m_Rp);	if (m_Nr>0 && m_B > (Int) ((RC_SKIP_MARGIN / 100.0) * m_Bs)) {	// buffer full!		m_skipNextFrame = TRUE;						// set the status		if(m_NrFlag)			m_Nr--;		// do not decrement if I-vops will be coded later because we still have the same number of P		// left to code in an IPPPPIPPPP cycle, even if we skip		m_B -= m_Rp;								// decrease current buffer level	} else		m_skipNextFrame = FALSE;

⌨️ 快捷键说明

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