HEVC代码追踪(十一。三):运动估计/补偿之xMotionEstimation

//!< 运动估计(基本思想就是用TZSearch算法先进行整像素搜索,确定一个局部的最佳值,然后以这个最佳点为中心再进行精度更高的分像素搜索。)
Void TEncSearch::xMotionEstimation( TComDataCU* pcCU,
	                                TComYuv* pcYuvOrg,
									Int iPartIdx,
									RefPicList eRefPicList,
									TComMv* pcMvPred,
									Int iRefIdxPred,
									TComMv& rcMv,
									UInt& ruiBits,
									UInt& ruiCost,
									Bool bBi  )
{
  UInt          uiPartAddr;
  Int           iRoiWidth;
  Int           iRoiHeight;

  TComMv        cMvHalf, cMvQter;
  TComMv        cMvSrchRngLT;
  TComMv        cMvSrchRngRB;

  TComYuv*      pcYuv = pcYuvOrg;

  m_iSearchRange = m_aaiAdaptSR[eRefPicList][iRefIdxPred];//!< 根据参考帧列表类型、参考帧序号自适应设置搜索范围  

  Int           iSrchRng      = ( bBi ? m_bipredSearchRange : m_iSearchRange ); //!< 根据是否是双向预测设置搜索范围
  TComPattern*  pcPatternKey  = pcCU->getPattern        ();//!< 用于获取neighbor的信息

  Double        fWeight       = 1.0;

  pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iRoiWidth, iRoiHeight );//!< 获取PU的地址,宽度和高度  

  if ( bBi )
  {
    TComYuv*  pcYuvOther = &m_acYuvPred[1-(Int)eRefPicList];
    pcYuv                = &m_cYuvPredTemp;

    pcYuvOrg->copyPartToPartYuv( pcYuv, uiPartAddr, iRoiWidth, iRoiHeight );

    pcYuv->removeHighFreq( pcYuvOther, uiPartAddr, iRoiWidth, iRoiHeight );

    fWeight = 0.5;
  }

  //  Search key pattern initialization
  pcPatternKey->initPattern( pcYuv->getLumaAddr( uiPartAddr ),
                            pcYuv->getCbAddr  ( uiPartAddr ),
                            pcYuv->getCrAddr  ( uiPartAddr ),
                            iRoiWidth,
                            iRoiHeight,
                            pcYuv->getStride(),
                            0, 0 );//!< 设置待搜索的PU的相关参数,首地址,宽度,高度,跨度等
 //!< 获取参考图像首地址和跨度(已编码的)
  Pel*        piRefY      = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxPred )->getPicYuvRec()->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() + uiPartAddr );
  Int         iRefStride  = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxPred )->getPicYuvRec()->getStride();

  /***************************************************************

	cout<<"iRoiWidth = "<<iRoiWidth<<endl;
	cout<<"iRoiHeight = "<<iRoiHeight<<endl;
	cout<<"iRefStride = "<<iRefStride<<endl;

  	for (int y = 0; y < iRoiHeight; y++)
	{
		for(int x=0;x<iRoiWidth;x++)
		{

			if(x%16==0) cout<<endl;
			cout<<*(piRefY+x)<<" ";

		}
		cout<<endl<<"****************************************************************"<<"y="<<(y+1)<<endl;
		piRefY += iRefStride;
	}

	/***************************************************************/

  TComMv      cMvPred = *pcMvPred;
  //!< 设置运动估计的搜索范围,LeftTop & RightBottom
  if ( bBi )  xSetSearchRange   ( pcCU, rcMv   , iSrchRng, cMvSrchRngLT, cMvSrchRngRB );
  else        xSetSearchRange   ( pcCU, cMvPred, iSrchRng, cMvSrchRngLT, cMvSrchRngRB );

  m_pcRdCost->getMotionCost ( 1, 0 );

  m_pcRdCost->setPredictor  ( *pcMvPred );
  m_pcRdCost->setCostScale  ( 2 );

  setWpScalingDistParam( pcCU, iRefIdxPred, eRefPicList );//!< 设置跟weighted prediction相关的参数
  //  Do integer search
  if ( !m_iFastSearch || bBi )
  {
    xPatternSearch      ( pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost );
  }
  else
  {
    rcMv = *pcMvPred;
    xPatternSearchFast  ( pcCU, pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost );
  }

  m_pcRdCost->getMotionCost( 1, 0 );
  m_pcRdCost->setCostScale ( 1 );
  //!< 分像素搜索
  xPatternSearchFracDIF( pcCU, pcPatternKey, piRefY, iRefStride, &rcMv, cMvHalf, cMvQter, ruiCost,bBi );

  m_pcRdCost->setCostScale( 0 );
  rcMv <<= 2;//!< 整像素
  rcMv += (cMvHalf <<= 1);//!< 1/2 像素
  rcMv +=  cMvQter;//!< 1/4 像素
  //!< 故rcMv最终以1/4像素为单位
  UInt uiMvBits = m_pcRdCost->getBits( rcMv.getHor(), rcMv.getVer() );

  ruiBits      += uiMvBits;
  ruiCost       = (UInt)( floor( fWeight * ( (Double)ruiCost - (Double)m_pcRdCost->getCost( uiMvBits ) ) ) + (Double)m_pcRdCost->getCost( ruiBits ) );
}
时间: 2024-08-31 00:17:19

HEVC代码追踪(十一。三):运动估计/补偿之xMotionEstimation的相关文章

HEVC代码追踪(三):encode-&amp;gt;compressGOP

/** - Application has picture buffer list with size of GOP + 1 - Picture buffer list acts like as ring buffer - End of the list has the latest picture . \param flush cause encoder to encode a partial GOP \param pcPicYuvOrg original YUV picture \retva

HEVC代码追踪(十一):运动估计/补偿之理论知识

运动估计的英文名称是Motion Estimation,是视频编码和视频处理(例如去交织)中广泛使用的一种技术. 运动估计的基本思想是将图像序列的每一帧分成许多互不重叠的宏块,并认为宏块内所有像素的位移量都相同,然后对每个宏块到参考帧某一给定特定搜索范围内根据一定的匹配准则找出与当前块最相似的块,即匹配块,匹配块与当前块的相对位移即为运动矢量.视频压缩的时候,只需保存运动矢量和残差数据就可以完全恢复出当前块. 在帧间预测编码中,由于活动图像邻近帧中的景物存在着一定的相关性.因此,可将活动图像分成

HEVC代码追踪(十一。五):运动估计/补偿之xTZSearch

Void TEncSearch::xTZSearch( TComDataCU* pcCU, TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, TComMv* pcMvSrchRngLT, TComMv* pcMvSrchRngRB, TComMv& rcMv, UInt& ruiSAD ) {//!< 确定运动估计搜索范围的边界 Int iSrchRngHorLeft = pcMvSrchRngLT->getHor(

HEVC代码追踪(十一。九):运动估计/补偿之xTZ2PointSearch

__inline Void TEncSearch::xTZ2PointSearch( TComPattern* pcPatternKey, IntTZSearchStruct& rcStruct, TComMv* pcMvSrchRngLT, TComMv* pcMvSrchRngRB ) { Int iSrchRngHorLeft = pcMvSrchRngLT->getHor(); Int iSrchRngHorRight = pcMvSrchRngRB->getHor(); In

HEVC代码追踪(十一。二):运动估计/补偿之predInterSearch

/** search of the best candidate for inter prediction * \param pcCU * \param pcOrgYuv * \param rpcPredYuv * \param rpcResiYuv * \param rpcRecoYuv * \param bUseRes * \returns Void */ #if AMP_MRG Void TEncSearch::predInterSearch( TComDataCU* pcCU, TCom

HEVC代码追踪(十一。一):运动估计/补偿之xCheckRDCostInter

#if AMP_MRG Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG) #else Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize ) #endif

HEVC代码追踪(十一。四):运动估计/补偿之xPatternSearch和xPatternSearchFast

Void TEncSearch::xPatternSearch( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, TComMv* pcMvSrchRngLT, TComMv* pcMvSrchRngRB, TComMv& rcMv, UInt& ruiSAD ) { Int iSrchRngHorLeft = pcMvSrchRngLT->getHor(); Int iSrchRngHorRight = pcMvSrch

HEVC代码追踪(十一。八):运动估计/补偿之xTZ8PointSquareSearch

__inline Void TEncSearch::xTZ8PointSquareSearch( TComPattern* pcPatternKey, IntTZSearchStruct& rcStruct, TComMv* pcMvSrchRngLT, TComMv* pcMvSrchRngRB, const Int iStartX, const Int iStartY, const Int iDist ) { Int iSrchRngHorLeft = pcMvSrchRngLT->ge

HEVC代码追踪(十一。六):运动估计/补偿之xTZSearchHelp

/* 分析xTZSearch这个函数,xTZSearchHelp是当中最为重要的子函数之一.它实现最基本的功能:根据输入的搜索点坐标, 参考图像首地址,原始图像首地址,以及当前PU大小等相关信息,计算出SAD,并与之前保存的最佳值进行比较,更新到 目前为止的最佳值相关参数,如uiBestSad,搜索点坐标,搜索步长等.其他的函数如xTZ8PointSearch等搜索函数,最终 都是调用xTZSearchHelp进行误差匹配的. */ __inline Void TEncSearch::xTZSe