VVC/JEM代码学习4:xPredIntraAng

(个人理解)

      该函数的主要作用是计算角度模式(包括DC模式)的预测值。在进行预测值计算前,还要对参考像素进行滤波。

      帧内角度模式的基本原理是通过当前像素燕某个预测方向在参考像素集上投影,“投中”的那个参考像素就是当前像素的预测值。在上面参考像素的获得过程中,已经可以看到,某些预测像素,某些预测角度,很多情况下并不能投中某个参考像素,原因可能是该方向上没有参考像素,也可能是投中在两个像素之间。而且对某个像素的预测有时即需要尝试投影上参考像素集,也需要尝试投影左方参考像素集,这样计算的复杂度就会很高。因此,HEVC设计了将上方和左方两个参考像素集转换为一个一维的参考像素集。以角度18--34(HEVC)例,组成一维参考像素集的方法就是在上方参考像素集的基础上沿着x轴负方向左方延伸,将原来在预测块左方的参考像素值按照一定的规则投射到延伸的参考像素集上。

Void TComPrediction::xPredIntraAng(       Int bitDepth,

                                    const Pel* pSrc,     Int srcStride,
                                          Pel* pTrueDst, Int dstStrideTrue,
                                          UInt uiWidth, UInt uiHeight,
#if JVET_D0033_ADAPTIVE_CLIPPING
                                          ComponentID compID,
#else
                                          ChannelType channelType,
#endif
                                          UInt dirMode, const Bool bEnableEdgeFilters
#if VCEG_AZ07_INTRA_4TAP_FILTER
                                          , Bool enable4TapFilter
#endif
#if COM16_C983_RSAF_PREVENT_OVERSMOOTHING
                                          , Bool enableRSAF
#endif
                                          )
{
  Int width=Int(uiWidth);
  Int height=Int(uiHeight);


#if JVET_D0033_ADAPTIVE_CLIPPING
    const ChannelType channelType=toChannelType(compID);
#endif
  // 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)//如果是DC模式;
  {
    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,如果是角度预测;
  {
#if VCEG_AZ07_INTRA_65ANG_MODES
    const Bool       bIsModeVer         = (dirMode >= DIA_IDX);//在67种模式中,大于等于34是垂直模式;
#else
    const Bool       bIsModeVer         = (dirMode >= 18);
#endif//intraPredAngleMode表示该模式的编号与垂直模式50或者水平模式18的差;
    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
#if VCEG_AZ07_INTRA_65ANG_MODES
    static const Int angTable[17]    = {0,    1,    2,    3,    5,    7,    9,   11,   13,   15,   17,   19,   21,   23,   26,   29,   32};
    //设置invAngTable的nudity是为了消除帧内角度预测模式在计算预测值时麻烦的除法运算;
static const Int invAngTable[17] = {0, 8192, 4096, 2731, 1638, 1170,  910,  745,  630,  546,  482,  431,  390,  356,  315,  282,  256}; // (256 * 32) / Angle
#else
    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
#endif
    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)//对应的是11--25(原始模式)的这些模式;
    {
      const Int refMainOffsetPreScale = (bIsModeVer ? height : width ) - 1;
#if !JVET_C0024_QTBT
      const Int refMainOffset         = height - 1;
#endif
      for (Int x=0;x<width+1;x++)
      {
#if JVET_C0024_QTBT
        refAbove[x+height-1] = pSrc[x-srcStride-1];//height-1到width+height-1,得到上边的参考像素;
#else
        refAbove[x+refMainOffset] = pSrc[x-srcStride-1];
#endif
      }
      for (Int y=0;y<height+1;y++)
      {
#if JVET_C0024_QTBT
        refLeft[y+width-1] = pSrc[(y-1)*srcStride-1];//width-1到height+width-1,得到左列的参考像素;
#else
        refLeft[y+refMainOffset] = pSrc[(y-1)*srcStride-1];
#endif
      }
#if JVET_C0024_QTBT
  //如果是垂直模式,则refmain指向refAbove的第一个元素,否则指向refLeft的第一个元素;
      refMain = (bIsModeVer ? refAbove + height : refLeft + width)  - 1;
  //如果是垂直模式,则refSide指向refLeft的第一个元素,否则指向refAbove的第一个元素;
      refSide = (bIsModeVer ? refLeft + width  : refAbove + height) - 1;
#else
      refMain = (bIsModeVer ? refAbove : refLeft)  + refMainOffset;
      refSide = (bIsModeVer ? refLeft  : refAbove) + refMainOffset;
#endif


      // 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//对应的是2--9和27--34(HEVC)这些模式;
    {
#if JVET_C0024_QTBT
      for (Int x=0;x<width+height+1;x++)
#else
      for (Int x=0;x<2*width+1;x++)
#endif
      {
        refAbove[x] = pSrc[x-srcStride-1];//0到width+height,上行的参考值;
#if JVET_C0024_QTBT
        refLeft[x] = pSrc[(x-1)*srcStride-1];//0到width+height,左列的参考值;
#endif
      }
#if !JVET_C0024_QTBT
      for (Int y=0;y<2*height+1;y++)
      {
        refLeft[y] = pSrc[(y-1)*srcStride-1];
      }
#endif
  //如果是垂直模式,则refmain指向refAbove的第一个元素,否则指向refLeft的第一个元素;
      refMain = bIsModeVer ? refAbove : refLeft ;
  //如果是垂直模式,则refSide指向refLeft的第一个元素,否则指向refAbove的第一个元素;
      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)//如果不是垂直模式,则把width和height交换一下;
    {
      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)//做边缘滤波;
      {
        for (Int y=0;y<height;y++)
        {
#if JVET_D0033_ADAPTIVE_CLIPPING//对当前块的第一列进行边缘滤波;
            pDst[y*dstStride] = ClipA (pDst[y*dstStride] + (( refSide[y+1] - refSide[0] ) >> 1) ,compID);
#else
          pDst[y*dstStride] = Clip3 (0, ((1 << bitDepth) - 1), pDst[y*dstStride] + (( refSide[y+1] - refSide[0] ) >> 1) );
#endif
        }
      }
    }
    else//不是纯水平,也不是纯垂直模式时;
    {
      Pel *pDsty=pDst;


      for (Int y=0, deltaPos=intraPredAngle; y<height; y++, deltaPos+=intraPredAngle, pDsty+=dstStride)
      {
        const Int deltaInt   = deltaPos >> 5;//对应书上的iIdx;
        const Int deltaFract = deltaPos & (32 - 1);//对应draft里的iFact,书上的w;


        if (deltaFract)//iFact不等于0时;
        {
          // Do linear filtering
#if VCEG_AZ07_INTRA_4TAP_FILTER
          if (enable4TapFilter)//使用4抽头滤波;
          {
            Int p[4], x, refMainIndex;
#if !JVET_D0033_ADAPTIVE_CLIPPING
            const Pel nMin = 0, nMax = (1 << bitDepth) - 1;
#endif
#if COM16_C983_RSAF_PREVENT_OVERSMOOTHING
            Int *f =  ((channelType==CHANNEL_TYPE_LUMA) && enableRSAF) ? g_aiIntraCubicFilter[deltaFract] : ( (width<=8) ? g_aiIntraCubicFilter[deltaFract] : g_aiIntraGaussFilter[deltaFract] );
#else       //块的宽度小于8的采用三次插值滤波,否则用高斯滤波;
            Int *f = (width<=8) ? g_aiIntraCubicFilter[deltaFract] : g_aiIntraGaussFilter[deltaFract];
#endif


            
            for (x=0;x<width;x++)
            {
              refMainIndex = x+deltaInt+1;


              p[1] = refMain[refMainIndex];
              p[2] = refMain[refMainIndex+1];


              p[0] = x==0 ? p[1] : refMain[refMainIndex-1];
              p[3] = x==(width-1) ? p[2] : refMain[refMainIndex+2];
  //进行插值滤波,f[0],f[1],f[2],f[3]是滤波系数;
              pDst[y*dstStride+x] =  (Pel)( ( f[0]*p[0] + f[1]*p[1] + f[2]*p[2] + f[3]*p[3] + 128 ) >> 8 );


#if COM16_C983_RSAF_PREVENT_OVERSMOOTHING
              if (enableRSAF || width<=8)
#else
              if( width<=8 ) // for blocks larger than 8x8, Gaussian interpolation filter with positive coefficients is used, no Clipping is necessary
#endif
              {
#if JVET_D0033_ADAPTIVE_CLIPPING
                  pDst[y*dstStride+x] =  ClipA( pDst[y*dstStride+x],compID );
#else
                pDst[y*dstStride+x] =  Clip3( nMin, nMax, pDst[y*dstStride+x] );
#endif
              }
            }
          }
          else//不使用4抽头滤波;
          {
#endif
          const Pel *pRM=refMain+deltaInt+1;
          Int lastRefMainPel=*pRM++;
          for (Int x=0;x<width;pRM++,x++)
          {
            Int thisRefMainPel=*pRM;
//按照书上(朱秀昌)P123的公式计算预测值;
            pDsty[x+0] = (Pel) ( ((32-deltaFract)*lastRefMainPel + deltaFract*thisRefMainPel +16) >> 5 );
            lastRefMainPel=thisRefMainPel;
          }
#if VCEG_AZ07_INTRA_4TAP_FILTER
          }
#endif
        }//iFact等于0;
        else
        {
          // Just copy the integer samples
          for (Int x=0;x<width; x++)
          {
            pDsty[x] = refMain[x+deltaInt+1];//直接将整数位置的参考像素值赋给对预测值;
          }
        }
      }
#if VCEG_AZ07_INTRA_65ANG_MODES
      if ( edgeFilter && absAng<=1 )
      {
        for (Int y=0;y<height;y++)
        {
#if JVET_D0033_ADAPTIVE_CLIPPING
            pDst[y*dstStride] = ClipA(pDst[y*dstStride] + (( refSide[y+1] - refSide[0] ) >> 2) ,compID);
#else
          pDst[y*dstStride] = Clip3(0, (1<<bitDepth)-1, pDst[y*dstStride] + (( refSide[y+1] - refSide[0] ) >> 2) );
#endif
        }
      }
#endif
    }


    // 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;
      }
    }
  }
  ///角度模式结束//
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值