今天正式转入BMS代码的学习,最近在干解码端的东西,就从解码这边开始看了。先来看decompressSlice
函数。在之前的 HEVC代码学习38:decompressSlice函数 中已经讲过HM中的两个decompressSlice
函数,BMS中整合为一个decompressSlice
函数,变化不大。
DecSlice::decompressSlice
该函数比较简单,是解码一帧图像的上层入口函数,在DecLib::xDecodeSlice
中被调用。主要功能是完成编码结构CodingStructure
、熵解码器等信息的初始化,对遍历每个CTU调用DecCu::decompressCtu
解码一帧图像。
对比和HM、JEM的函数调用结构可以看出,BMS做了很多简化。
在宏定义中可以看到有两类:HEVC开头和JEM开头的,这些是原HM和JEM中的代码,现在HEVC开头的基本都是关闭的,从宏定义可以很清楚的看出变化。
流程如下:
1.初始化编码结构、熵编码器等。
2.遍历全部CTU调用DecCu::decompressCtu
进行解码,更新上细纹模型。
3.如果是最后一个CTU,不分析剩余比特;否则分析剩余比特。
代码分析:
Void DecSlice::decompressSlice(Slice* slice, InputBitstream* bitstream)
{
//-- For time output for each slice
slice->startProcessingTimer();
const SPS* sps = slice->getSPS(); //SPS
Picture* pic = slice->getPic(); //当期图像
#if HEVC_TILES_WPP
const TileMap& tileMap = *pic->tileMap;
#endif
#if JEM_TOOLS
CABACReader& cabacReader = *m_CABACDecoder->getCABACReader(sps->getSpsNext().getCABACEngineMode()); //加载CABAC
#if JEM_TOOLS
m_CABACDataStore->updateBufferState(slice, false); //更新slice
#else
m_CABACDataStore->updateBufferState(slice);
#endif
#else
CABACReader& cabacReader = *m_CABACDecoder->getCABACReader(0);
#endif
// setup coding structure 加载编码结构
CodingStructure& cs = *pic->cs; //获取当前CU
cs.slice = slice; //slice
cs.sps = sps; //sps
cs.pps = slice->getPPS(); //pps
#if HEVC_VPS
cs.vps = slice->getVPS(); //vps,VVC中好像不用了
#endif
cs.pcv = slice->getPPS()->pcv; //pcv,不清楚这个量
cs.chromaQpAdj = 0; //色度QP自适应
cs.picture->resizeSAO(cs.pcv->sizeInCtus, 0);
const unsigned numSubstreams = slice->getNumberOfSubstreamSizes() + 1;
// init each couple {EntropyDecoder, Substream}
// Table of extracted substreams.
std::vector<InputBitstream*> ppcSubstreams(numSubstreams); //输入的bit流
for (unsigned idx = 0; idx < numSubstreams; idx++) //分成子bit流
{
ppcSubstreams[idx] = bitstream->extractSubstream(idx + 1 < numSubstreams ? (slice->getSubstreamSize(idx) << 3) : bitstream->getNumBitsLeft());
}
#if HEVC_DEPENDENT_SLICES
const int startCtuTsAddr = slice->getSliceSegmentCurStartCtuTsAddr();
#else
const int startCtuTsAddr = slice->getSliceCurStartCtuTsAddr(); //起始CTU地址
#endif
#if HEVC_TILES_WPP
const int startCtuRsAddr = tileMap.getCtuTsToRsAddrMap(startCtuTsAddr);
#else
#if HEVC_DEPENDENT_SLICES
const int startCtuRsAddr = startCtuTsAddr;
#endif
#endif
const unsigned numCtusInFrame = cs.pcv->sizeInCtus; //一帧CTU数量
const unsigned widthInCtus = cs.pcv->widthInCtus; //一行CTU数量
#if HEVC_DEPENDENT_SLICES
const bool depSliceSegmentsEnabled = cs.pps->getDependentSliceSegmentsEnabledFlag();
#endif
#if HEVC_TILES_WPP
const bool wavefrontsEnabled = cs.pps->getEntropyCodingSyncEnabledFlag();
#endif
cabacReader.initBitstream( ppcSubstreams[0] ); //初始化bit流
#if JEM_TOOLS
cabacReader.initCtxModels( *slice, m_CABACDataStore ); //初始化上个文模型
#else
cabacReader.initCtxModels( *slice );
#endif
// Quantization parameter 设置QP
#if HEVC_DEPENDENT_SLICES
if(!slice->getDependentSliceSegmentFlag())
{
#endif
pic->m_prevQP[0] = pic->m_prevQP[1] = slice->getSliceQp();
#if HEVC_DEPENDENT_SLICES
}
#endif
CHECK( pic->m_prevQP[0] == std::numeric_limits<Int>::max(), "Invalid previous QP" );
DTRACE( g_trace_ctx, D_HEADER, "=========== POC: %d ===========\n", slice->getPOC() );
// The first CTU of the slice is the first coded substream, but the global substream number, as calculated by getSubstreamForCtuAddr may be higher.
// This calculates the common offset for all substreams in this slice.
#if HEVC_DEPENDENT_SLICES
const unsigned subStreamOffset = tileMap.getSubstreamForCtuAddr( startCtuRsAddr, true, slice );
#elif HEVC_TILES_WPP
const unsigned subStreamOffset = 0;
#endif
#if HEVC_DEPENDENT_SLICES
if( depSliceSegmentsEnabled )
{
// modify initial contexts with previous slice segment if this is a dependent slice.
const unsigned startTileIdx = tileMap.getTileIdxMap(startCtuRsAddr);
const Tile& currentTile = tileMap.tiles[startTileIdx];
const unsigned firstCtuRsAddrOfTile = currentTile.getFirstCtuRsAddr();
if( slice->getDependentSliceSegmentFlag() && startCtuRsAddr != firstCtuRsAddrOfTile )
{
if( currentTile.getTileWidthInCtus() >= 2 || !wavefrontsEnabled )
{
cabacReader.getCtx() = m_lastSliceSegmentEndContextState;
}
}
}
#endif
// for every CTU in the slice segment...
bool isLastCtuOfSliceSegment = false; //是否是最后一个CTU、
//遍历全部CTU进行解码
for( unsigned ctuTsAddr = startCtuTsAddr; !isLastCtuOfSliceSegment && ctuTsAddr < numCtusInFrame; ctuTsAddr++ )
{
#if HEVC_TILES_WPP
const unsigned ctuRsAddr = tileMap.getCtuTsToRsAddrMap(ctuTsAddr);
const Tile& currentTile = tileMap.tiles[ tileMap.getTileIdxMap(ctuRsAddr) ];
const unsigned firstCtuRsAddrOfTile = currentTile.getFirstCtuRsAddr();
const unsigned tileXPosInCtus = firstCtuRsAddrOfTile % widthInCtus;
const unsigned tileYPosInCtus = firstCtuRsAddrOfTile / widthInCtus;
#else
const unsigned ctuRsAddr = ctuTsAddr; //CTU索引
#endif
const unsigned ctuXPosInCtus = ctuRsAddr % widthInCtus; //x坐标
const unsigned ctuYPosInCtus = ctuRsAddr / widthInCtus; //y坐标
#if HEVC_TILES_WPP
const unsigned subStrmId = tileMap.getSubstreamForCtuAddr( ctuRsAddr, true, slice ) - subStreamOffset;
#else
const unsigned subStrmId = 0;
#endif
const unsigned maxCUSize = sps->getMaxCUWidth(); //CU最大尺寸
#if JEM_TOOLS
const CIPFSpec cipf = getCIPFSpec( slice, ctuXPosInCtus, ctuYPosInCtus );
#endif
Position pos( ctuXPosInCtus*maxCUSize, ctuYPosInCtus*maxCUSize) ; //二维坐标(x,y)
UnitArea ctuArea(cs.area.chromaFormat, Area( pos.x, pos.y, maxCUSize, maxCUSize ) );
DTRACE_UPDATE( g_trace_ctx, std::make_pair( "ctu", ctuRsAddr ) );
cabacReader.initBitstream( ppcSubstreams[subStrmId] ); //初始化bit流
#if HEVC_TILES_WPP
// set up CABAC contexts' state for this CTU
if( ctuRsAddr == firstCtuRsAddrOfTile )
{
if( ctuTsAddr != startCtuTsAddr ) // if it is the first CTU, then the entropy coder has already been reset
{
#if JEM_TOOLS
cabacReader.initCtxModels( *slice, m_CABACDataStore );
#else
cabacReader.initCtxModels( *slice );
#endif
}
pic->m_prevQP[0] = pic->m_prevQP[1] = slice->getSliceQp();
}
else if( ctuXPosInCtus == tileXPosInCtus && wavefrontsEnabled )
{
// Synchronize cabac probabilities with upper-right CTU if it's available and at the start of a line.
if( ctuTsAddr != startCtuTsAddr ) // if it is the first CTU, then the entropy coder has already been reset
{
#if JEM_TOOLS
cabacReader.initCtxModels( *slice, m_CABACDataStore );
#else
cabacReader.initCtxModels( *slice );
#endif
}
if( cs.getCURestricted( pos.offset(maxCUSize, -1), slice->getIndependentSliceIdx(), tileMap.getTileIdxMap( pos ), CH_L ) )
{
// Top-right is available, so use it.
cabacReader.getCtx() = m_entropyCodingSyncContextState;
}
pic->m_prevQP[0] = pic->m_prevQP[1] = slice->getSliceQp();
}
#endif
#if JEM_TOOLS
// load ctx from previous frame
if( cipf.loadCtx )
{
m_CABACDataStore->loadCtxStates( slice, cabacReader.getCtx(), cipf.ctxId ); //加载上下文模型
}
#endif
#if JEM_TOOLS
if( ctuRsAddr == 0 )
{
cabacReader.alf( cs );
}
#endif
isLastCtuOfSliceSegment = cabacReader.coding_tree_unit(cs, ctuArea, pic->m_prevQP, ctuRsAddr); //是否是最后一个CTU
m_pcCuDecoder->decompressCtu( cs, ctuArea ); //decompressCtu
#if HEVC_TILES_WPP
if( ctuXPosInCtus == tileXPosInCtus+1 && wavefrontsEnabled )
{
m_entropyCodingSyncContextState = cabacReader.getCtx();
}
#endif
#if JEM_TOOLS
// store CABAC context to be used in next frames
if( cipf.storeCtx )
{
m_CABACDataStore->storeCtxStates( slice, cabacReader.getCtx(), cipf.ctxId ); //存储上下文模型
}
#endif
if( isLastCtuOfSliceSegment ) //最后一个CTU
{
#if DECODER_CHECK_SUBSTREAM_AND_SLICE_TRAILING_BYTES
cabacReader.remaining_bytes( false );
#endif
#if HEVC_DEPENDENT_SLICES
if( !slice->getDependentSliceSegmentFlag() )
{
#endif
slice->setSliceCurEndCtuTsAddr( ctuTsAddr+1 );
#if HEVC_DEPENDENT_SLICES
}
slice->setSliceSegmentCurEndCtuTsAddr( ctuTsAddr+1 );
#endif
}
#if HEVC_TILES_WPP
else if( ( ctuXPosInCtus + 1 == tileXPosInCtus + currentTile.getTileWidthInCtus () ) &&
( ctuYPosInCtus + 1 == tileYPosInCtus + currentTile.getTileHeightInCtus() || wavefrontsEnabled ) )
{
// The sub-stream/stream should be terminated after this CTU.
// (end of slice-segment, end of tile, end of wavefront-CTU-row)
unsigned binVal = cabacReader.terminating_bit();
CHECK( !binVal, "Expecting a terminating bit" );
#if DECODER_CHECK_SUBSTREAM_AND_SLICE_TRAILING_BYTES
cabacReader.remaining_bytes( true );
#endif
}
#endif
}
CHECK( !isLastCtuOfSliceSegment, "Last CTU of slice segment not signalled as such" );
#if HEVC_DEPENDENT_SLICES
if( depSliceSegmentsEnabled )
{
m_lastSliceSegmentEndContextState = cabacReader.getCtx(); //ctx end of dep.slice
}
#endif
// deallocate all created substreams, including internal buffers.
for( auto substr: ppcSubstreams )
{
delete substr;
}
slice->stopProcessingTimer();
}