x265-1.7版本-common/slice.h注释

注:问号以及未注释部分 会在x265-1.8版本内更新

 

/*****************************************************************************
 * Copyright (C) 2015 x265 project
 *
 * Authors: Steve Borho <steve@borho.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at license @ x265.com.
 *****************************************************************************/

#ifndef X265_SLICE_H
#define X265_SLICE_H

#include "common.h"

namespace x265 {
// private namespace

class Frame;
class PicList;
class MotionReference;

enum SliceType
{
    B_SLICE,
    P_SLICE,
    I_SLICE
};

struct RPS//RPS(reference picture set) 在slice中被引用
{
    int  numberOfPictures; //参考帧列表中的参考数目
    int  numberOfNegativePictures;//前向参考个数
    int  numberOfPositivePictures;//后向参考帧个数

    int  poc[MAX_NUM_REF_PICS];//存储当前帧前面已经编码可参考的参考帧  初始化为0  (按照编码顺序排序 倒序)
    int  deltaPOC[MAX_NUM_REF_PICS];//参考帧poc减去存储当前帧poc的值 按照 远 近 (当前帧) 近 远的次序排序
    bool bUsed[MAX_NUM_REF_PICS];//记录参考帧列表对应位置帧是否可用 (如果当前帧是关键帧,则全为不可用)
    //如编码次序:0 4 2 1 3 8 6 5 7 12 10 9 11
    //则编码11时, poc存储为: 10 12 6 8
    //deltapoc 存储为: -5 -3 -1 12 (排序为 6 8 10 12)

    RPS()//初始化
        : numberOfPictures(0)
        , numberOfNegativePictures(0)
        , numberOfPositivePictures(0)
    {
        memset(deltaPOC, 0, sizeof(deltaPOC));
        memset(poc, 0, sizeof(poc));
        memset(bUsed, 0, sizeof(bUsed));
    }
    /** 函数功能           : 将RPS列表中的deltaPOC和bused 按照 远 近 (当前帧) 近 远的次序排序
    /*  调用范围           : 只在DPB::computeRPS函数中被调用
    *   返回值             : null
    **/
    void sortDeltaPOC();
};

namespace Profile {
    enum Name
    {
        NONE = 0,
        MAIN = 1,
        MAIN10 = 2,
        MAINSTILLPICTURE = 3,
        MAINREXT = 4,
        HIGHTHROUGHPUTREXT = 5
    };
}

namespace Level {
    enum Tier
    {
        MAIN = 0,
        HIGH = 1,
    };

    enum Name
    {
        NONE = 0,
        LEVEL1 = 30,
        LEVEL2 = 60,
        LEVEL2_1 = 63,
        LEVEL3 = 90,
        LEVEL3_1 = 93,
        LEVEL4 = 120,
        LEVEL4_1 = 123,
        LEVEL5 = 150,
        LEVEL5_1 = 153,
        LEVEL5_2 = 156,
        LEVEL6 = 180,
        LEVEL6_1 = 183,
        LEVEL6_2 = 186,
        LEVEL8_5 = 255,
    };
}

struct ProfileTierLevel
{
    bool     tierFlag;
    bool     progressiveSourceFlag;
    bool     interlacedSourceFlag;
    bool     nonPackedConstraintFlag;
    bool     frameOnlyConstraintFlag;
    bool     profileCompatibilityFlag[32];
    bool     intraConstraintFlag;
    bool     lowerBitRateConstraintFlag;
    int      profileIdc;
    int      levelIdc;
    uint32_t minCrForLevel;
    uint32_t maxLumaSrForLevel;
    uint32_t bitDepthConstraint;
    int      chromaFormatConstraint;
};

struct HRDInfo
{
    uint32_t bitRateScale;
    uint32_t cpbSizeScale;
    uint32_t initialCpbRemovalDelayLength;
    uint32_t cpbRemovalDelayLength;
    uint32_t dpbOutputDelayLength;
    uint32_t bitRateValue;
    uint32_t cpbSizeValue;
    bool     cbrFlag;

    HRDInfo()
        : bitRateScale(0)
        , cpbSizeScale(0)
        , initialCpbRemovalDelayLength(1)
        , cpbRemovalDelayLength(1)
        , dpbOutputDelayLength(1)
        , cbrFlag(false)
    {
    }
};

struct TimingInfo
{
    uint32_t numUnitsInTick;
    uint32_t timeScale;
};

struct VPS
{
    uint32_t         maxTempSubLayers;
    uint32_t         numReorderPics;//需要重排序的帧数 值:(param.bBPyramid && param.bframes > 1) ? 2 : !!param.bframes;
    uint32_t         maxDecPicBuffering;//解码需要的buffer大小: 值 X265_MIN(MAX_NUM_REF, X265_MAX(vps.numReorderPics + 2, (uint32_t)param.maxNumReferences) + vps.numReorderPics);
    uint32_t         maxLatencyIncrease;
    HRDInfo          hrdParameters;
    ProfileTierLevel ptl;
};

struct Window
{
    bool bEnabled;
    int  leftOffset;
    int  rightOffset;
    int  topOffset;
    int  bottomOffset;

    Window()
    {
        bEnabled = false;
    }
};

struct VUI
{
    bool       aspectRatioInfoPresentFlag;
    int        aspectRatioIdc;
    int        sarWidth;
    int        sarHeight;

    bool       overscanInfoPresentFlag;
    bool       overscanAppropriateFlag;

    bool       videoSignalTypePresentFlag;
    int        videoFormat;
    bool       videoFullRangeFlag;

    bool       colourDescriptionPresentFlag;
    int        colourPrimaries;
    int        transferCharacteristics;
    int        matrixCoefficients;

    bool       chromaLocInfoPresentFlag;
    int        chromaSampleLocTypeTopField;
    int        chromaSampleLocTypeBottomField;

    Window     defaultDisplayWindow;

    bool       frameFieldInfoPresentFlag;
    bool       fieldSeqFlag;

    bool       hrdParametersPresentFlag;
    HRDInfo    hrdParameters;

    TimingInfo timingInfo;
};

struct SPS
{
    int      chromaFormatIdc;        // use param
    uint32_t picWidthInLumaSamples;  // use param
    uint32_t picHeightInLumaSamples; // use param

    uint32_t numCuInWidth;  //有多少CTU列
    uint32_t numCuInHeight; //有多少CTU行
    uint32_t numCUsInFrame; //一帧CTU个数
    uint32_t numPartitions;
    uint32_t numPartInCUSize;

    int      log2MinCodingBlockSize;
    int      log2DiffMaxMinCodingBlockSize;

    uint32_t quadtreeTULog2MaxSize;
    uint32_t quadtreeTULog2MinSize;//最小TU的值 (一般默认为4x4,值为2)

    uint32_t quadtreeTUMaxDepthInter; // use param
    uint32_t quadtreeTUMaxDepthIntra; // use param

    bool     bUseSAO; // use param
    bool     bUseAMP; // use param
    uint32_t maxAMPDepth;

    uint32_t maxTempSubLayers;   // max number of Temporal Sub layers
    uint32_t maxDecPicBuffering; // 继承VPS值 解码需要的buffer大小: 值 X265_MIN(MAX_NUM_REF, X265_MAX(vps.numReorderPics + 2, (uint32_t)param.maxNumReferences) + vps.numReorderPics); these are dups of VPS values
    uint32_t maxLatencyIncrease;
    int      numReorderPics;

    bool     bUseStrongIntraSmoothing; // use param
    bool     bTemporalMVPEnabled;

    Window   conformanceWindow;
    VUI      vuiParameters;
};

struct PPS
{
    uint32_t maxCuDQPDepth;

    int      chromaQpOffset[2];      // use param

    bool     bUseWeightPred;         // 当前是否应用P帧加权预测use param
    bool     bUseWeightedBiPred;     // 当前是否应用B帧加权预测use param
    bool     bUseDQP;
    bool     bConstrainedIntraPred;  // use param

    bool     bTransquantBypassEnabled;  // Indicates presence of cu_transquant_bypass_flag in CUs.
    bool     bTransformSkipEnabled;     // 是否应用transformSkip 在4x4才应用 use param
    bool     bEntropyCodingSyncEnabled; // use param
    bool     bSignHideEnabled;          // use param

    bool     bDeblockingFilterControlPresent;
    bool     bPicDisableDeblockingFilter;
    int      deblockingFilterBetaOffsetDiv2;
    int      deblockingFilterTcOffsetDiv2;
};

struct WeightParam  //存储加权帧的加权信息  用于slice类
{
    //加权为 w*ref + offset  ref为参考帧所有像素值
    //其中w为: inputWeight/(1<<log2WeightDenom)
    //其中offset为:inputOffset
    //权重系数w一般在 √(原始帧方差/参考帧方差) 周围搜索
    // Explicit weighted prediction parameters parsed in slice header,
    bool     bPresentFlag;    //是否加权
    uint32_t log2WeightDenom; //权重系数左移位个数(为了保证精度)
    int      inputWeight;     //权重系数    整个的权重系数等于:(1<<(7-log2WeightDenom)) + inputWeight  目前的权重系数为guessScale * (1<<denom)+0.5f =  (1<<(7-log2WeightDenom)) + inputWeight      其中guessScale :√(原始帧方差/参考帧方差)
    int      inputOffset;     //offset信息 整帧所有像素偏移值

    /* makes a non-h265 weight (i.e. fix7), into an h265 weight */
    /** 函数功能             :设置相应数据
    /*  调用范围             :只在在LookaheadTLD::weightsAnalyse和weightAnalyse函数中被调用
    * \参数 w                :LookaheadTLD::weightsAnalyse: (int)(guessScale * 128 + 0.5f) weightAnalyse:guessScale * (1<<denom)+0.5f   denom 一般等于7
    * \参数 o                :LookaheadTLD::weightsAnalyse:  0   weightAnalyse:0
    * \参数 denom            :LookaheadTLD::weightsAnalyse:  7   weightAnalyse:一般为7
    * \参数 bNormalize       :LookaheadTLD::weightsAnalyse:  trueweightAnalyse:list0为true  list1 为false
    * \返回                  :NULL */
    void setFromWeightAndOffset(int w, int o, int denom, bool bNormalize)
    {
        inputOffset = o;
        log2WeightDenom = denom;
        inputWeight = w;
        while (bNormalize && log2WeightDenom > 0 && (inputWeight > 127))
        {
            log2WeightDenom--;
            inputWeight >>= 1;
        }

        inputWeight = X265_MIN(inputWeight, 127);
    }
};

//设置WeightParam类数据
#define SET_WEIGHT(w, b, s, d, o) \
{ \
    (w).inputWeight = (s); \
    (w).log2WeightDenom = (d); \
    (w).inputOffset = (o); \
    (w).bPresentFlag = (b); \
}

class Slice
{
public:

    const SPS*  m_sps; //指向encoder的SPS
    const PPS*  m_pps; //指向encoder的PPS
    WeightParam m_weightPredTable[2][MAX_NUM_REF][3]; // 参考帧加权状态信息 (每个list的第一帧分析加权与否,其它不加权) [list][refIdx][0:Y, 1:U, 2:V]
    MotionReference (*m_mref)[MAX_NUM_REF + 1]; //指向FrameEncoder中对应的参考帧信息
    RPS         m_rps;

    NalUnitType m_nalUnitType;
    SliceType   m_sliceType; //在DPB::prepareEncode函数确定slice类型:B_SLICE,P_SLICE,I_SLICE
    int         m_sliceQp;
    int         m_poc;   //当前POC
    
    int         m_lastIDR;

    bool        m_bCheckLDC;       // 目前x265: B帧 false 其它 ture 指的是low-delay mode??? TODO: is this necessary?
    bool        m_sLFaseFlag;      // 应用????????表示环路滤波是否可以越过当前slice的上边界和左边界 初始化为ture (只在prepareEncode计算过一次然后就写码流) loop filter boundary flag
    bool        m_colFromL0Flag;   // ?????用于时域MV预测的collocated图像来自参考图像列表List0还是List1 目前x265: B帧 false 其它 ture collocated picture from List0 or List1 flag
    uint32_t    m_colRefIdx;       // ?????用于时域MV预测的collocated图像的参考索引号 ,一直为0,没被修改过never modified
    
    //x265中没有应用长期参考帧 x265中List0 全为前向参考帧 List1全为后向参考帧
    //List0 存储方式:按照距离当前帧由近到远排序
    //List1 存储方式:按照距离当前帧由近到远排序
    int         m_numRefIdx[2];//分别为L0的最大帧数(配置的配置的参考帧最大数目与实际前向帧数取最小值)  L1 的最大帧数(2或者1 与 实际后向帧数取最小值) 分别为前向帧和后向帧
    Frame*      m_refPicList[2][MAX_NUM_REF + 1];//存储List帧指针(数据存放在dpb中的m_picList中)setRefPicList函数设定
    int         m_refPOCList[2][MAX_NUM_REF + 1];//存储List帧poc setRefPicList函数设定

    uint32_t    m_maxNumMergeCand; //Merge选择的候选个数,默认值为3 use param
    uint32_t    m_endCUAddr;//一帧中最后实际像素在帧中的4x4块标号+1

    Slice()
    {
        m_lastIDR = 0;
        m_sLFaseFlag = true;
        m_numRefIdx[0] = m_numRefIdx[1] = 0;
        for (int i = 0; i < MAX_NUM_REF; i++)
        {
            m_refPicList[0][i] = NULL;
            m_refPicList[1][i] = NULL;
            m_refPOCList[0][i] = 0;
            m_refPOCList[1][i] = 0;
        }

        disableWeights();
    }

    /** 函数功能           : 关闭当前帧的加权预测
    /*  调用范围           : 只在Slice()、FrameEncoder::compressFrame()、weightAnalyse函数中被调用
    *   返回值             : null
    **/
    void disableWeights();
    /** 函数功能       : 获取参考帧List 以及 POC
    /*  调用范围       : 只在DPB::prepareEncode函数中被调用
    * \参数 picList    : DPB m_picList
    *   返回值         : null
    **/
    void setRefPicList(PicList& picList);

    const Frame* getRefPic(int list, int refIdx) const { return refIdx >= 0 ? m_refPicList[list][refIdx] : NULL; }

    bool getRapPicFlag() const
    {
        return m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
            || m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA;
    }

    bool getIdrPicFlag() const
    {
        return m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL;
    }

    bool isIRAP() const   { return m_nalUnitType >= 16 && m_nalUnitType <= 23; }//判断是否关键帧:IRAP(Intra Random Access Point)三种IRAP IDR(Instantaneous Decoding Refresh) CRA(Clean Random Access) BLA(Broken Link Access)

    bool isIntra()  const { return m_sliceType == I_SLICE; }

    bool isInterB() const { return m_sliceType == B_SLICE; }

    bool isInterP() const { return m_sliceType == P_SLICE; }
    /** 函数功能       : 返回一帧中最后实际像素在帧中的4x4块标号+1
    /*  调用范围       : 只在Encoder::encode函数中被调用
    * \参数 endCUAddr  : (一帧CTU个数)*(在CTU中4x4的个数)
    *   返回值         : 返回一帧中最后实际像素在帧中的4x4块标号+1
    **/
    uint32_t realEndAddress(uint32_t endCUAddr) const;
};

}

#endif // ifndef X265_SLICE_H

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值