本文主要是列出几个与变换有关的函数,以及涉及到的比较重要的变量,但不会去深入讨论函数的实现,日后有机会的话会更详细地讨论这几个函数,这里仅是列举出来,起到备忘的作用,也能供大家参考。
如何定位到这几个函数的,方法与前面的一篇博客HEVC学习(八) —— 以SAO为例浅析跟踪代码方法类似,有兴趣的朋友可以去看看,这里就不重复这个过程了。
在xCompressCU函数中,有这么几个函数值得我们注意的,xCheckRDCostInter、xCheckRDCostMerge2Nx2N、xCheckRDCostIntra。它们分别是实现帧间预测模式、Merge模式、帧内预测式的主函数。前两个函数的子函数xEstimateResidualQT(实际上通过调用函数encodeResAndCalcRdInterCU)、第三个函数的子函数xRecurIntraCodingQT(实际上通过调用xIntraCodingLumaBlk、xIntraCodingChromaBlk)均会调用函数transformNxN,该函数如下定义:
- Void TComTrQuant::transformNxN( TComDataCU* pcCU,
- Pel* pcResidual,
- UInt uiStride,
- TCoeff* rpcCoeff,
- #if ADAPTIVE_QP_SELECTION
- Int*& rpcArlCoeff,
- #endif
- UInt uiWidth,
- UInt uiHeight,
- UInt& uiAbsSum,
- TextType eTType,
- UInt uiAbsPartIdx,
- Bool useTransformSkip
- )
- {
- if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
- {
- uiAbsSum=0;
- for (UInt k = 0; k<uiHeight; k++)
- {
- for (UInt j = 0; j<uiWidth; j++)
- {
- rpcCoeff[k*uiWidth+j]= pcResidual[k*uiStride+j];
- uiAbsSum += abs(pcResidual[k*uiStride+j]);
- }
- }
- return;
- }
- UInt uiMode; //luma intra pred
- if(eTType == TEXT_LUMA && pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTRA )
- {
- uiMode = pcCU->getLumaIntraDir( uiAbsPartIdx );
- }
- else
- {
- uiMode = REG_DCT;
- }
- uiAbsSum = 0;
- assert( (pcCU->getSlice()->getSPS()->getMaxTrSize() >= uiWidth) );
- if(useTransformSkip)
- {
- xTransformSkip( pcResidual, uiStride, m_plTempCoeff, uiWidth, uiHeight );
- }
- else
- {
- xT( uiMode, pcResidual, uiStride, m_plTempCoeff, uiWidth, uiHeight );
- }
- xQuant( pcCU, m_plTempCoeff, rpcCoeff,
- #if ADAPTIVE_QP_SELECTION
- rpcArlCoeff,
- #endif
- uiWidth, uiHeight, uiAbsSum, eTType, uiAbsPartIdx );
- }
这个函数主要调用了xTransformSkip、xT、xQuant三个函数,分别实现对残差进行TS(Transform Skip)模式、普通变换模式以及量化的功能。对于第二个函数xT来说,它调用了xTrNxN,该函数的实现如下所示:
- /** MxN forward transform (2D)
- * \param block input data (residual)
- * \param coeff output data (transform coefficients)
- * \param iWidth input data (width of transform)
- * \param iHeight input data (height of transform)
- */
- void xTrMxN(short *block,short *coeff, int iWidth, int iHeight, UInt uiMode)
- {
- #if FULL_NBIT
- int shift_1st = g_aucConvertToBit[iWidth] + 1 + g_uiBitDepth - 8; // log2(iWidth) - 1 + g_uiBitDepth - 8
- #else
- int shift_1st = g_aucConvertToBit[iWidth] + 1 + g_uiBitIncrement; // log2(iWidth) - 1 + g_uiBitIncrement
- #endif
- int shift_2nd = g_aucConvertToBit[iHeight] + 8; // log2(iHeight) + 6
- short tmp[ 64 * 64 ];
- #if !REMOVE_NSQT
- if( iWidth == 16 && iHeight == 4)
- {
- partialButterfly16( block, tmp, shift_1st, iHeight );
- partialButterfly4( tmp, coeff, shift_2nd, iWidth );
- }
- else if( iWidth == 32 && iHeight == 8 )
- {
- partialButterfly32( block, tmp, shift_1st, iHeight );
- partialButterfly8( tmp, coeff, shift_2nd, iWidth );
- }
- else if( iWidth == 4 && iHeight == 16)
- {
- partialButterfly4( block, tmp, shift_1st, iHeight );
- partialButterfly16( tmp, coeff, shift_2nd, iWidth );
- }
- else if( iWidth == 8 && iHeight == 32 )
- {
- partialButterfly8( block, tmp, shift_1st, iHeight );
- partialButterfly32( tmp, coeff, shift_2nd, iWidth );
- }
- else
- #endif
- if( iWidth == 4 && iHeight == 4)
- {
- #if INTRA_TRANS_SIMP
- if (uiMode != REG_DCT)
- {
- fastForwardDst(block,tmp,shift_1st); // Forward DST BY FAST ALGORITHM, block input, tmp output
- fastForwardDst(tmp,coeff,shift_2nd); // Forward DST BY FAST ALGORITHM, tmp input, coeff output
- }
- else
- {
- partialButterfly4(block, tmp, shift_1st, iHeight);
- partialButterfly4(tmp, coeff, shift_2nd, iWidth);
- }
- #else
- if (uiMode != REG_DCT && (!uiMode || (uiMode>=2 && uiMode <= 25))) // Check for DCT or DST
- {
- fastForwardDst(block,tmp,shift_1st); // Forward DST BY FAST ALGORITHM, block input, tmp output
- }
- else
- {
- partialButterfly4(block, tmp, shift_1st, iHeight);
- }
- if (uiMode != REG_DCT && (!uiMode || (uiMode>=11 && uiMode <= 34))) // Check for DCT or DST
- {
- fastForwardDst(tmp,coeff,shift_2nd); // Forward DST BY FAST ALGORITHM, tmp input, coeff output
- }
- else
- {
- partialButterfly4(tmp, coeff, shift_2nd, iWidth);
- }
- #endif
- }
- else if( iWidth == 8 && iHeight == 8)
- {
- partialButterfly8( block, tmp, shift_1st, iHeight );
- partialButterfly8( tmp, coeff, shift_2nd, iWidth );
- }
- else if( iWidth == 16 && iHeight == 16)
- {
- partialButterfly16( block, tmp, shift_1st, iHeight );
- partialButterfly16( tmp, coeff, shift_2nd, iWidth );
- }
- else if( iWidth == 32 && iHeight == 32)
- {
- partialButterfly32( block, tmp, shift_1st, iHeight );
- partialButterfly32( tmp, coeff, shift_2nd, iWidth );
- }
- }
到了这里,相信大家光看名字也能看出这些代码是做什么事情的了,对帧内预测模式的4x4块进行DST变换,其余的根据块大小分别做蝶形快速变换(4x4,8x8,16x16,32x32),不同尺寸的变换矩阵在代码中如下表示:
- const short g_aiT4[4][4] =
- {
- { 64, 64, 64, 64},
- { 83, 36,-36,-83},
- { 64,-64,-64, 64},
- { 36,-83, 83,-36}
- };
- const short g_aiT8[8][8] =
- {
- { 64, 64, 64, 64, 64, 64, 64, 64},
- { 89, 75, 50, 18,-18,-50,-75,-89},
- { 83, 36,-36,-83,-83,-36, 36, 83},
- { 75,-18,-89,-50, 50, 89, 18,-75},
- { 64,-64,-64, 64, 64,-64,-64, 64},
- { 50,-89, 18, 75,-75,-18, 89,-50},
- { 36,-83, 83,-36,-36, 83,-83, 36},
- { 18,-50, 75,-89, 89,-75, 50,-18}
- };
- const short g_aiT16[16][16] =
- {
- { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64},
- { 90, 87, 80, 70, 57, 43, 25, 9, -9,-25,-43,-57,-70,-80,-87,-90},
- { 89, 75, 50, 18,-18,-50,-75,-89,-89,-75,-50,-18, 18, 50, 75, 89},
- { 87, 57, 9,-43,-80,-90,-70,-25, 25, 70, 90, 80, 43, -9,-57,-87},
- { 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83},
- { 80, 9,-70,-87,-25, 57, 90, 43,-43,-90,-57, 25, 87, 70, -9,-80},
- { 75,-18,-89,-50, 50, 89, 18,-75,-75, 18, 89, 50,-50,-89,-18, 75},
- { 70,-43,-87, 9, 90, 25,-80,-57, 57, 80,-25,-90, -9, 87, 43,-70},
- { 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64},
- { 57,-80,-25, 90, -9,-87, 43, 70,-70,-43, 87, 9,-90, 25, 80,-57},
- { 50,-89, 18, 75,-75,-18, 89,-50,-50, 89,-18,-75, 75, 18,-89, 50},
- { 43,-90, 57, 25,-87, 70, 9,-80, 80, -9,-70, 87,-25,-57, 90,-43},
- { 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36},
- { 25,-70, 90,-80, 43, 9,-57, 87,-87, 57, -9,-43, 80,-90, 70,-25},
- { 18,-50, 75,-89, 89,-75, 50,-18,-18, 50,-75, 89,-89, 75,-50, 18},
- { 9,-25, 43,-57, 70,-80, 87,-90, 90,-87, 80,-70, 57,-43, 25, -9}
- };
- const short g_aiT32[32][32] =
- {
- { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64},
- { 90, 90, 88, 85, 82, 78, 73, 67, 61, 54, 46, 38, 31, 22, 13, 4, -4,-13,-22,-31,-38,-46,-54,-61,-67,-73,-78,-82,-85,-88,-90,-90},
- { 90, 87, 80, 70, 57, 43, 25, 9, -9,-25,-43,-57,-70,-80,-87,-90,-90,-87,-80,-70,-57,-43,-25, -9, 9, 25, 43, 57, 70, 80, 87, 90},
- { 90, 82, 67, 46, 22, -4,-31,-54,-73,-85,-90,-88,-78,-61,-38,-13, 13, 38, 61, 78, 88, 90, 85, 73, 54, 31, 4,-22,-46,-67,-82,-90},
- { 89, 75, 50, 18,-18,-50,-75,-89,-89,-75,-50,-18, 18, 50, 75, 89, 89, 75, 50, 18,-18,-50,-75,-89,-89,-75,-50,-18, 18, 50, 75, 89},
- { 88, 67, 31,-13,-54,-82,-90,-78,-46, -4, 38, 73, 90, 85, 61, 22,-22,-61,-85,-90,-73,-38, 4, 46, 78, 90, 82, 54, 13,-31,-67,-88},
- { 87, 57, 9,-43,-80,-90,-70,-25, 25, 70, 90, 80, 43, -9,-57,-87,-87,-57, -9, 43, 80, 90, 70, 25,-25,-70,-90,-80,-43, 9, 57, 87},
- { 85, 46,-13,-67,-90,-73,-22, 38, 82, 88, 54, -4,-61,-90,-78,-31, 31, 78, 90, 61, 4,-54,-88,-82,-38, 22, 73, 90, 67, 13,-46,-85},
- { 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83},
- { 82, 22,-54,-90,-61, 13, 78, 85, 31,-46,-90,-67, 4, 73, 88, 38,-38,-88,-73, -4, 67, 90, 46,-31,-85,-78,-13, 61, 90, 54,-22,-82},
- { 80, 9,-70,-87,-25, 57, 90, 43,-43,-90,-57, 25, 87, 70, -9,-80,-80, -9, 70, 87, 25,-57,-90,-43, 43, 90, 57,-25,-87,-70, 9, 80},
- { 78, -4,-82,-73, 13, 85, 67,-22,-88,-61, 31, 90, 54,-38,-90,-46, 46, 90, 38,-54,-90,-31, 61, 88, 22,-67,-85,-13, 73, 82, 4,-78},
- { 75,-18,-89,-50, 50, 89, 18,-75,-75, 18, 89, 50,-50,-89,-18, 75, 75,-18,-89,-50, 50, 89, 18,-75,-75, 18, 89, 50,-50,-89,-18, 75},
- { 73,-31,-90,-22, 78, 67,-38,-90,-13, 82, 61,-46,-88, -4, 85, 54,-54,-85, 4, 88, 46,-61,-82, 13, 90, 38,-67,-78, 22, 90, 31,-73},
- { 70,-43,-87, 9, 90, 25,-80,-57, 57, 80,-25,-90, -9, 87, 43,-70,-70, 43, 87, -9,-90,-25, 80, 57,-57,-80, 25, 90, 9,-87,-43, 70},
- { 67,-54,-78, 38, 85,-22,-90, 4, 90, 13,-88,-31, 82, 46,-73,-61, 61, 73,-46,-82, 31, 88,-13,-90, -4, 90, 22,-85,-38, 78, 54,-67},
- { 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64},
- { 61,-73,-46, 82, 31,-88,-13, 90, -4,-90, 22, 85,-38,-78, 54, 67,-67,-54, 78, 38,-85,-22, 90, 4,-90, 13, 88,-31,-82, 46, 73,-61},
- { 57,-80,-25, 90, -9,-87, 43, 70,-70,-43, 87, 9,-90, 25, 80,-57,-57, 80, 25,-90, 9, 87,-43,-70, 70, 43,-87, -9, 90,-25,-80, 57},
- { 54,-85, -4, 88,-46,-61, 82, 13,-90, 38, 67,-78,-22, 90,-31,-73, 73, 31,-90, 22, 78,-67,-38, 90,-13,-82, 61, 46,-88, 4, 85,-54},
- { 50,-89, 18, 75,-75,-18, 89,-50,-50, 89,-18,-75, 75, 18,-89, 50, 50,-89, 18, 75,-75,-18, 89,-50,-50, 89,-18,-75, 75, 18,-89, 50},
- { 46,-90, 38, 54,-90, 31, 61,-88, 22, 67,-85, 13, 73,-82, 4, 78,-78, -4, 82,-73,-13, 85,-67,-22, 88,-61,-31, 90,-54,-38, 90,-46},
- { 43,-90, 57, 25,-87, 70, 9,-80, 80, -9,-70, 87,-25,-57, 90,-43,-43, 90,-57,-25, 87,-70, -9, 80,-80, 9, 70,-87, 25, 57,-90, 43},
- { 38,-88, 73, -4,-67, 90,-46,-31, 85,-78, 13, 61,-90, 54, 22,-82, 82,-22,-54, 90,-61,-13, 78,-85, 31, 46,-90, 67, 4,-73, 88,-38},
- { 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36},
- { 31,-78, 90,-61, 4, 54,-88, 82,-38,-22, 73,-90, 67,-13,-46, 85,-85, 46, 13,-67, 90,-73, 22, 38,-82, 88,-54, -4, 61,-90, 78,-31},
- { 25,-70, 90,-80, 43, 9,-57, 87,-87, 57, -9,-43, 80,-90, 70,-25,-25, 70,-90, 80,-43, -9, 57,-87, 87,-57, 9, 43,-80, 90,-70, 25},
- { 22,-61, 85,-90, 73,-38, -4, 46,-78, 90,-82, 54,-13,-31, 67,-88, 88,-67, 31, 13,-54, 82,-90, 78,-46, 4, 38,-73, 90,-85, 61,-22},
- { 18,-50, 75,-89, 89,-75, 50,-18,-18, 50,-75, 89,-89, 75,-50, 18, 18,-50, 75,-89, 89,-75, 50,-18,-18, 50,-75, 89,-89, 75,-50, 18},
- { 13,-38, 61,-78, 88,-90, 85,-73, 54,-31, 4, 22,-46, 67,-82, 90,-90, 82,-67, 46,-22, -4, 31,-54, 73,-85, 90,-88, 78,-61, 38,-13},
- { 9,-25, 43,-57, 70,-80, 87,-90, 90,-87, 80,-70, 57,-43, 25, -9, -9, 25,-43, 57,-70, 80,-87, 90,-90, 87,-80, 70,-57, 43,-25, 9},
- { 4,-13, 22,-31, 38,-46, 54,-61, 67,-73, 78,-82, 85,-88, 90,-90, 90,-90, 88,-85, 82,-78, 73,-67, 61,-54, 46,-38, 31,-22, 13, -4}
- };