率失真优化是HEVC中的重要内容,通过基本形式如下图的公式求率失真代价J,权衡码率R和失真D,为编码器选择最优编码参数,实现高效编码。
HM中,对于率失真相关的计算都是通过TComRdCost类指针m_pcRdCost调用其成员函数实现的,那就来看下TComRdCost类。
TComRdCost类中定义了失真D、码率R和λ有关的函数,其中计算RD代价的函数为:calcRdCost(详见http://blog.csdn.net/lin453701006/article/details/78145247)。失真计算函数会通过FpDistFunc m_afpDistortFunc[DF_TOTAL_FUNCTIONS]函数指针来调用。
/// RD cost computation class
//率失真
class TComRdCost
{
private:
// for distortion
FpDistFunc m_afpDistortFunc[DF_TOTAL_FUNCTIONS]; // [eDFunc] 失真函数指针
CostMode m_costMode; //失真模式
Double m_distortionWeight[MAX_NUM_COMPONENT]; // only chroma values are used. //色度失真权重
Double m_dLambda;
Double m_sqrtLambda;
#if RExt__HIGH_BIT_DEPTH_SUPPORT
Double m_dLambdaMotionSAD[2 /* 0=standard, 1=for transquant bypass when mixed-lossless cost evaluation enabled*/];
Double m_dLambdaMotionSSE[2 /* 0=standard, 1=for transquant bypass when mixed-lossless cost evaluation enabled*/];
#else
UInt m_uiLambdaMotionSAD[2 /* 0=standard, 1=for transquant bypass when mixed-lossless cost evaluation enabled*/];
UInt m_uiLambdaMotionSSE[2 /* 0=standard, 1=for transquant bypass when mixed-lossless cost evaluation enabled*/];
#endif
Double m_dFrameLambda;
// for motion cost
TComMv m_mvPredictor;
#if RExt__HIGH_BIT_DEPTH_SUPPORT
Double m_dCost;
#else
UInt m_uiCost;
#endif
Int m_iCostScale;
public:
TComRdCost();
virtual ~TComRdCost();
//计算RD代价
Double calcRdCost ( UInt uiBits, Distortion uiDistortion, Bool bFlag = false, DFunc eDFunc = DF_DEFAULT );
Double calcRdCost64( UInt64 uiBits, UInt64 uiDistortion, Bool bFlag = false, DFunc eDFunc = DF_DEFAULT );
//设置失真权重
Void setDistortionWeight ( const ComponentID compID, const Double distortionWeight ) { m_distortionWeight[compID] = distortionWeight; }
//设置lambda
Void setLambda ( Double dLambda, const BitDepths &bitDepths );
Void setFrameLambda ( Double dLambda ) { m_dFrameLambda = dLambda; }
//获取lambda平方根
Double getSqrtLambda () { return m_sqrtLambda; }
//获取lambda
Double getLambda() { return m_dLambda; }
//获取色度权重
Double getChromaWeight () { return ((m_distortionWeight[COMPONENT_Cb] + m_distortionWeight[COMPONENT_Cr]) / 2.0); }
//获取代价模式
Void setCostMode(CostMode m ) { m_costMode = m; }
// Distortion Functions
Void init();
//设置失真参数
Void setDistParam( UInt uiBlkWidth, UInt uiBlkHeight, DFunc eDFunc, DistParam& rcDistParam );
Void setDistParam( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, DistParam& rcDistParam ); //设置帧间运动估计的失真参数
Void setDistParam( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, Int iStep, DistParam& rcDistParam, Bool bHADME=false ); //设置帧间亚像素运动估计的失真参数
Void setDistParam( DistParam& rcDP, Int bitDepth, Pel* p1, Int iStride1, Pel* p2, Int iStride2, Int iWidth, Int iHeight, Bool bHadamard = false );
Distortion calcHAD(Int bitDepth, Pel* pi0, Int iStride0, Pel* pi1, Int iStride1, Int iWidth, Int iHeight ); //计算SATD
// for motion cost
static UInt xGetExpGolombNumberOfBits( Int iVal );
//运动估计代价
#if RExt__HIGH_BIT_DEPTH_SUPPORT
Void getMotionCost( Bool bSad, Int iAdd, Bool bIsTransquantBypass ) { m_dCost = (bSad ? m_dLambdaMotionSAD[(bIsTransquantBypass && m_costMode==COST_MIXED_LOSSLESS_LOSSY_CODING) ?1:0] + iAdd : m_dLambdaMotionSSE[(bIsTransquantBypass && m_costMode==COST_MIXED_LOSSLESS_LOSSY_CODING)?1:0] + iAdd); }
#else
Void getMotionCost( Bool bSad, Int iAdd, Bool bIsTransquantBypass ) { m_uiCost = (bSad ? m_uiLambdaMotionSAD[(bIsTransquantBypass && m_costMode==COST_MIXED_LOSSLESS_LOSSY_CODING) ?1:0] + iAdd : m_uiLambdaMotionSSE[(bIsTransquantBypass && m_costMode==COST_MIXED_LOSSLESS_LOSSY_CODING)?1:0] + iAdd); }
#endif
Void setPredictor( TComMv& rcMv )
{
m_mvPredictor = rcMv;
}
Void setCostScale( Int iCostScale ) { m_iCostScale = iCostScale; }
__inline Distortion getCost( Int x, Int y )
{
#if RExt__HIGH_BIT_DEPTH_SUPPORT
return Distortion((m_dCost * getBits(x, y)) / 65536.0);
#else
return m_uiCost * getBits(x, y) >> 16;
#endif
}
#if RExt__HIGH_BIT_DEPTH_SUPPORT
Distortion getCost( UInt b ) { return Distortion(( m_dCost * b ) / 65536.0); }
#else
Distortion getCost( UInt b ) { return ( m_uiCost * b ) >> 16; }
#endif
UInt getBits( Int x, Int y )
{
return xGetExpGolombNumberOfBits((x << m_iCostScale) - m_mvPredictor.getHor())
+ xGetExpGolombNumberOfBits((y << m_iCostScale) - m_mvPredictor.getVer());
}
//SSE、SAD、SATD失真计算函数
private:
static Distortion xGetSSE ( DistParam* pcDtParam );
static Distortion xGetSSE4 ( DistParam* pcDtParam );
static Distortion xGetSSE8 ( DistParam* pcDtParam );
static Distortion xGetSSE16 ( DistParam* pcDtParam );
static Distortion xGetSSE32 ( DistParam* pcDtParam );
static Distortion xGetSSE64 ( DistParam* pcDtParam );
static Distortion xGetSSE16N ( DistParam* pcDtParam );
static Distortion xGetSAD ( DistParam* pcDtParam );
static Distortion xGetSAD4 ( DistParam* pcDtParam );
static Distortion xGetSAD8 ( DistParam* pcDtParam );
static Distortion xGetSAD16 ( DistParam* pcDtParam );
static Distortion xGetSAD32 ( DistParam* pcDtParam );
static Distortion xGetSAD64 ( DistParam* pcDtParam );
static Distortion xGetSAD16N ( DistParam* pcDtParam );
static Distortion xGetSAD12 ( DistParam* pcDtParam );
static Distortion xGetSAD24 ( DistParam* pcDtParam );
static Distortion xGetSAD48 ( DistParam* pcDtParam );
static Distortion xGetHADs ( DistParam* pcDtParam );
static Distortion xCalcHADs2x2 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep );
static Distortion xCalcHADs4x4 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep );
static Distortion xCalcHADs8x8 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep );
public:
//计算失真
Distortion getDistPart(Int bitDepth, Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, const ComponentID compID, DFunc eDFunc = DF_SSE );
};// END CLASS DEFINITION TComRdCost