前言
該系列文章記錄自己閱讀HM代碼的理解,防止自己以後遺忘,同時可以交流學習,可能會有錯誤,歡迎批評指正。
幀內預測
HEVC的幀內預測(intra)有以下特點:
1. 使用四叉樹的編碼塊劃分框架。
2. 使用細緻的預測角度劃分方式,共33個方向(如下圖所示),加上planar模式,DC模式,一共35個方向模式。
3. 使用planar模式預測平滑區域。
4. 在特定情況下對塊邊緣進行濾波。
5. 對參考邊緣進行自適應濾波。
還有其它一些特定,涉及到之後再加上。本文函數主要涉及特性2和特性4.
源代碼及註釋
//Function for deriving the angular Intra predictions
/** Function for deriving the simplified angular intra predictions.
* \param bitDepth bit depth
* \param pSrc pointer to reconstructed sample array,指向當前塊的(0,0)位置
* \param srcStride the stride of the reconstructed sample array,相當於圖像的寬
* \param pTrueDst reference to pointer for the prediction sample array
* \param dstStrideTrue the stride of the prediction sample array
* \param uiWidth the width of the block
* \param uiHeight the height of the block
* \param channelType type of pel array (luma/chroma)
* \param format chroma format
* \param dirMode the intra prediction mode index
* \param blkAboveAvailable boolean indication if the block above is available
* \param blkLeftAvailable boolean indication if the block to the left is available
* \param bEnableEdgeFilters indication whether to enable edge filters:針對垂直和水平模式使用,boundray smoothing
*
* This function derives the prediction samples for the angular mode based on the prediction direction indicated by
* the prediction mode index. The prediction direction is given by the displacement of the bottom row of the block and
* the reference row above the block in the case of vertical prediction or displacement of the rightmost column
* of the block and reference column left from the block in the case of the horizontal prediction. The displacement
* is signalled at 1/32 pixel accuracy. When projection of the predicted pixel falls inbetween reference samples,
* the predicted value for the pixel is linearly interpolated from the reference samples. All reference samples are taken
* from the extended main reference.
*/
//NOTE: Bit-Limit - 25-bit source
Void TComPrediction::xPredIntraAng( Int bitDepth,
const Pel* pSrc, Int srcStride,
Pel* pTrueDst, Int dstStrideTrue,
UInt uiWidth, UInt uiHeight, ChannelType channelType,
UInt dirMode, const Bool bEnableEdgeFilters
)
{
Int width=Int(uiWidth);
Int height=Int(uiHeight);
// Map the mode index to main prediction direction and angle
assert( dirMode != PLANAR_IDX ); //no planar
const Bool modeDC = dirMode==DC_IDX;
// Do the DC prediction
if (modeDC)
{
const Pel dcval = predIntraGetPredValDC(pSrc, srcStride, width, height);
for (Int y=height;y>0;y--, pTrueDst+=dstStrideTrue)
{
for (Int x=0; x<width;) // width is always a multiple of 4.
{
pTrueDst[x++] = dcval;
}
}
}
else // Do angular predictions
{
const Bool bIsModeVer = (dirMode >= 18);//2-17, 18-35
const Int intraPredAngleMode = (bIsModeVer) ? (Int)dirMode - VER_IDX : -((Int)dirMode - HOR_IDX);
const Int absAngMode = abs(intraPredAngleMode);
const Int signAng = intraPredAngleMode < 0 ? -1 : 1;
const Bool edgeFilter = bEnableEdgeFilters && isLuma(channelType) && (width <= MAXIMUM_INTRA_FILTERED_WIDTH) && (height <= MAXIMUM_INTRA_FILTERED_HEIGHT);
// Set bitshifts and scale the angle parameter to block size
static const Int angTable[9] = {0, 2, 5, 9, 13, 17, 21, 26, 32};
static const Int invAngTable[9] = {0, 4096, 1638, 910, 630, 482, 390, 315, 256}; // (256 * 32) / Angle
Int invAngle = invAngTable[absAngMode];
Int absAng = angTable[absAngMode];
Int intraPredAngle = signAng * absAng;
Pel* refMain;
Pel* refSide;
Pel refAbove[2*MAX_CU_SIZE+1];
Pel refLeft[2*MAX_CU_SIZE+1];
// Initialize the Main and Left reference array.
if (intraPredAngle < 0)
{
const Int refMainOffsetPreScale = (bIsModeVer ? height : width ) - 1;
const Int refMainOffset = height - 1;
for (Int x=0;x<width+1;x++)
{
refAbove[x+refMainOffset] = pSrc[x-srcStride-1];
}
for (Int y=0;y<height+1;y++)
{
refLeft[y+refMainOffset] = pSrc[(y-1)*srcStride-1];
}
refMain = (bIsModeVer ? refAbove : refLeft) + refMainOffset;
refSide = (bIsModeVer ? refLeft : refAbove) + refMainOffset;
// Extend the Main reference to the left.
Int invAngleSum = 128; // rounding for (shift by 8)
for (Int k=-1; k>(refMainOffsetPreScale+1)*intraPredAngle>>5; k--)
{
invAngleSum += invAngle;
refMain[k] = refSide[invAngleSum>>8];
}
}
else
{
for (Int x=0;x<2*width+1;x++)
{
refAbove[x] = pSrc[x-srcStride-1];
}
for (Int y=0;y<2*height+1;y++)
{
refLeft[y] = pSrc[(y-1)*srcStride-1];
}
refMain = bIsModeVer ? refAbove : refLeft ;
refSide = bIsModeVer ? refLeft : refAbove;
}
// swap width/height if we are doing a horizontal mode:
Pel tempArray[MAX_CU_SIZE*MAX_CU_SIZE];
const Int dstStride = bIsModeVer ? dstStrideTrue : MAX_CU_SIZE;
Pel *pDst = bIsModeVer ? pTrueDst : tempArray;
if (!bIsModeVer)
{
std::swap(width, height);
}
if (intraPredAngle == 0) // pure vertical or pure horizontal
{
for (Int y=0;y<height;y++)
{
for (Int x=0;x<width;x++)
{
pDst[y*dstStride+x] = refMain[x+1];
}
}
if (edgeFilter)//boundary smoothing
{
for (Int y=0;y<height;y++)
{
pDst[y*dstStride] = Clip3 (0, ((1 << bitDepth) - 1), pDst[y*dstStride] + (( refSide[y+1] - refSide[0] ) >> 1) );//Boundary Smoothing
}
}
}
else
{
Pel *pDsty=pDst;
for (Int y=0, deltaPos=intraPredAngle; y<height; y++, deltaPos+=intraPredAngle, pDsty+=dstStride)
{
const Int deltaInt = deltaPos >> 5;
const Int deltaFract = deltaPos & (32 - 1);
if (deltaFract)
{
// Do linear filtering
const Pel *pRM=refMain+deltaInt+1;
Int lastRefMainPel=*pRM++;
for (Int x=0;x<width;pRM++,x++)
{
Int thisRefMainPel=*pRM;
pDsty[x+0] = (Pel) ( ((32-deltaFract)*lastRefMainPel + deltaFract*thisRefMainPel +16) >> 5 );//1/32 pixel accuracy interpolation
lastRefMainPel=thisRefMainPel;
}
}
else
{
// Just copy the integer samples
for (Int x=0;x<width; x++)
{
pDsty[x] = refMain[x+deltaInt+1];
}
}
}
}
// Flip the block if this is the horizontal mode
if (!bIsModeVer)
{
for (Int y=0; y<height; y++)
{
for (Int x=0; x<width; x++)
{
pTrueDst[x*dstStrideTrue] = pDst[x];
}
pTrueDst++;
pDst+=dstStride;
}
}
}
}
代碼解讀
這裏以垂直偏左方向預測爲例(預測的方向模式在18-25):代碼中主要的數據結構的含義如上圖。
代碼主要實現了兩部分功能:
1. 先計算refMain和refSide,將refMain擴展
2. 插值計算當前塊的每一行
補:對於水平豎直預測,且預測塊不大於16x16,進行邊緣濾波
reference
- Jani Lainema, Frank Bossen, Woo-Jin Han, Junghye Min, and Kemal Ugur. Intra Coding of the HEVC Standard. CSVT.