注:问号以及未注释部分 会在x265-1.9版本内更新
/*****************************************************************************
* 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_NS {
// 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 {
/*H.265/HEVC标准中提出了三种档次,分别是Main、Main 10和Main Still Picture
**/
enum Name //档次名称
{
NONE = 0, //不应用
MAIN = 1, //支持每像素8比特的位深、4:2:0的采样格式,是最常见的档次
MAIN10 = 2, //10比特位深,有助于提高视频质量。主要应用于消费电子领域。
MAINSTILLPICTURE = 3,//支持单个静止图像 不支持帧间预测
MAINREXT = 4, //全I帧 其它档次
HIGHTHROUGHPUTREXT = 5 //不应用
};
}
namespace Level {
/*
H.265/HEVC 标准中定义了两个层和13级
x265:level 大于等于 Level::LEVEL5时,CTB必须大于等于32
x265:param.maxNumReferences + vps.numReorderPics; 全部参考帧个数不能大于8
**/
enum Tier //层
{
/*
两个层分别是 Main Tier和High Tier
4和4以上的8个Level支持High Tier
Tier按其最高比特率来处理应用问题, Main tier适用于大多数应用,High Tier用于高需求应用。 符合某一Tier/Level 的解码器能够解码当前以及比当前Tier/Level低的所有码流
**/
MAIN = 0,
HIGH = 1,
};
enum Name//级
{
/*同一个Level实际上就是一套对编码比特流的一系列编码参数的限制。
H.265/HEVC 的13个等级支持从QCIF到8K多种分辨率的图像。
图像宽高受到该级别定义参数MaxLumaPS的限制—图像的宽和高均小于等于8倍的MaxLumaPS再开方。此外,Level 还约束了每幅图像中垂直和水平方向Tile的最大数量,以及每秒最大的Tile数量。
**/
NONE = 0,//不用
LEVEL1 = 30, //以下为13个等级
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//用途:存储? 只存在于VPS中
{
bool tierFlag; //当前层次等级 MAIN HIGH
bool progressiveSourceFlag;
bool interlacedSourceFlag;
bool nonPackedConstraintFlag;
bool frameOnlyConstraintFlag;
/*
MAIN : MAIN、 MAIN10 置为true 其它为false
MAIN10并且8位 : MAIN、 MAIN10 置为true 其它为false
MAINSTILLPICTURE: MAIN、 MAIN10 MAINSTILLPICTURE 置为true 其它为false
MAIN10 : MAIN10 置为true 其它为false
MAINREXT : MAINREXT 置为true 其它为false
**/
bool profileCompatibilityFlag[32];//当general_profile_space 值等于0时,该语法元素取值为1,表示当前CVS编码过程使用的档次的ID为j 如采用MAIN 则profileCompatibilityFlag[MAIN]=1 其它为0 coded video sequence (CVS)
bool intraConstraintFlag;//是否全I帧
bool onePictureOnlyConstraintFlag;//当前需要编码的是否只有一帧
bool lowerBitRateConstraintFlag;//用途????置为true 暂不应用
int profileIdc; //当前编码所处的档次 MAIN MAIN10 MAINSTILLPICTURE等
int levelIdc; //当前level等级 如Level::LEVEL1等
uint32_t minCrForLevel;//当前的最小压缩比 CR 表示 CompressionRatio
uint32_t maxLumaSrForLevel;//该等级每帧最大亮度像素个数
uint32_t bitDepthConstraint;//像素位宽 8 10 12
int chromaFormatConstraint;//图像格式 默认为420
};
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;//最大时域层个数 param.bEnableTemporalSubLayers ? 2 : 1
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;//CTU中一边有多少4x4块 默认为16(64有16个4)
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; //当前intra参考像素是否需要强滤波 use param
bool bTemporalMVPEnabled;
Window conformanceWindow;
VUI vuiParameters;
};
struct PPS
{
uint32_t maxCuDQPDepth;
int chromaQpOffset[2]; //分别存储cb、cr量化参数相对于亮度量化参数的偏移 默认为0// 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;//存储经过rateControlStart估计后的当前帧的量化参数
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