VVC学习之五:VTM帧内预测之色度预测——estIntraPredChromaQT()

文章目录

    • 色度预测过程回顾
    • estIntraPredChromaQT()代码学习

色度预测过程回顾

VTM中色度预测一共测试8种模式,其顺序为为PLANARVERHORDCLM_CHROMAMDLM_LMDLM_TDM_CHROMA,其中LM_CHROMAMDLM_LMDLM_T分别表示CCLMLM_LLM_A跨分量预测模式,DM_CHROMA为从亮度分量获取到的模式。其中如果DM_CHROMA和前四个默认预测模式相同,则将对应默认模式替换为VDIA模式。

在classic CCLM之外,模型参数还有另外两种计算方式,即CCLM还有两位两种模式,称之为LM_ALM_L模式。

  1. LM_A模式仅使用已重建亮度像素的上方元素作为参考像素计算线性模型参数,为了获取足够参考像素,上方亮度参考像素扩展为W+H个
  2. LM_L模式仅使用已重建像素左侧元素计算线性模型参数,同样,左侧参考像素扩展为H+W个

填充色度预测列表通过函数getIntraChromaCandModes()完成,其具体实现如下:

void PU::getIntraChromaCandModes( const PredictionUnit &pu, unsigned modeList[NUM_CHROMA_MODE] )
{
  {
    modeList[  0 ] = PLANAR_IDX;
    modeList[  1 ] = VER_IDX;
    modeList[  2 ] = HOR_IDX;
    modeList[  3 ] = DC_IDX;
    modeList[4] = LM_CHROMA_IDX;
    modeList[5] = MDLM_L_IDX;
    modeList[6] = MDLM_T_IDX;
    modeList[7] = DM_CHROMA_IDX;

    Position topLeftPos = pu.blocks[pu.chType].lumaPos();
    Position refPos = topLeftPos.offset( pu.blocks[pu.chType].lumaSize().width >> 1, pu.blocks[pu.chType].lumaSize().height >> 1 );
    const PredictionUnit *lumaPU = CS::isDualITree( *pu.cs ) ? pu.cs->picture->cs->getPU( refPos, CHANNEL_TYPE_LUMA ) : &pu;
    const uint32_t lumaMode = lumaPU->intraDir[CHANNEL_TYPE_LUMA];
    for( int i = 0; i < 4; i++ )
    {
      if( lumaMode == modeList[i] )
      {
        modeList[i] = VDIA_IDX;
        break;
      }
    }
  }
}

色度预测的主要流程如下:

  1. 初始化 chroma 预测模式候选列表
  2. 基于SATD粗略计算列表中各模式代价,并排序,去除两个SATD代价最大的候选模式。
  3. 对剩余候选模式进行RQT计算full Rdcost。
  4. 确定最优色度预测模式,更新picture buffer重建信号。

estIntraPredChromaQT()代码学习

void IntraSearch::estIntraPredChromaQT( CodingUnit &cu, Partitioner &partitioner, const double maxCostAllowed )
{
  const ChromaFormat format   = cu.chromaFormat;
  const uint32_t    numberValidComponents = getNumberValidComponents(format);
  CodingStructure &cs = *cu.cs;
  const TempCtx ctxStart  ( m_CtxCache, m_CABACEstimator->getCtx() );

  cs.setDecomp( cs.area.Cb(), false ); // 设置重建标志false

#if JVET_M0102_INTRA_SUBPARTITIONS
  double    bestCostSoFar = maxCostAllowed;
  bool      lumaUsesISP   = !CS::isDualITree( *cu.cs ) && cu.ispMode; // 帧间亮度是否为ISP模式
  PartSplit ispType       = lumaUsesISP ? CU::getISPType( cu, COMPONENT_Y ) : TU_NO_ISP; // 亮度ISP类型
  CHECK( cu.ispMode && bestCostSoFar < 0, "bestCostSoFar must be positive!" );
#endif

  auto &pu = *cu.firstPU;

  {
    uint32_t       uiBestMode = 0; // 记录最优模式
    Distortion uiBestDist = 0; 	   // 最优模式对应distortion
    double     dBestCost = MAX_DOUBLE; // 最优模式rdCost

    //----- init mode list ----
    {
      uint32_t  uiMinMode = 0;
      uint32_t  uiMaxMode = NUM_CHROMA_MODE;

      //----- check chroma modes -----
      uint32_t chromaCandModes[ NUM_CHROMA_MODE ];
      PU::getIntraChromaCandModes( pu, chromaCandModes ); // 构建chroma模式列表

      // create a temporary CS 
      CodingStructure &saveCS = *m_pSaveCS[0];
      saveCS.pcv      = cs.pcv;
      saveCS.picture  = cs.picture;
      saveCS.area.repositionTo( cs.area );
      saveCS.clearTUs();

#if JVET_M0102_INTRA_SUBPARTITIONS
      if( !CS::isDualITree( cs ) && cu.ispMode )
      {
        saveCS.clearCUs();
        saveCS.clearPUs();
      }
#endif

      if( CS::isDualITree( cs ) )
      {
        if( partitioner.canSplit( TU_MAX_TR_SPLIT, cs ) )
        {
          partitioner.splitCurrArea( TU_MAX_TR_SPLIT, cs );

          do
          {
            cs.addTU( CS::getArea( cs, partitioner.currArea(), partitioner.chType ), partitioner.chType ).depth = partitioner.currTrDepth;
          } while( partitioner.nextPart( cs ) );

          partitioner.exitCurrSplit();
        }
        else
        cs.addTU( CS::getArea( cs, partitioner.currArea(), partitioner.chType ), partitioner.chType ); // add tu
      }

      std::vector<TransformUnit*> orgTUs;

#if JVET_M0102_INTRA_SUBPARTITIONS
      if( lumaUsesISP ) // 如果亮度是ISP,记录当前cu,pu信息
      {
        CodingUnit& auxCU = saveCS.addCU( cu, partitioner.chType );
        auxCU.ispMode = cu.ispMode;
        saveCS.sps = cu.cs->sps;
        saveCS.addPU( *cu.firstPU, partitioner.chType );
      }
#endif


      // create a store for the TUs
      for( const auto &ptu : cs.tus )
      {
        // for split TUs in HEVC, add the TUs without Chroma parts for correct setting of Cbfs
#if JVET_M0102_INTRA_SUBPARTITIONS
        if( lumaUsesISP || pu.contains( *ptu, CHANNEL_TYPE_CHROMA ) )
#else
        if( pu.contains( *ptu, CHANNEL_TYPE_CHROMA ) )
#endif
        {
          saveCS.addTU( *ptu, partitioner.chType );
          orgTUs.push_back( ptu );
        }
      }
#if JVET_M0102_INTRA_SUBPARTITIONS
      if( lumaUsesISP )
      {
        saveCS.clearCUs();
      }
#endif
      // SATD pre-selecting.
      int satdModeList[NUM_CHROMA_MODE]; // chroma基于satd模式列表
      int64_t satdSortedCost[NUM_CHROMA_MODE]; // 对应satd cost
      for (int i = 0; i < NUM_CHROMA_MODE; i++)
      {
        // 默认跳过基于SATD预测的模式,直接进行fullRD代价计算
        satdSortedCost[i] = 0; // for the mode not pre-select by SATD, do RDO by default, so set the initial value 0.
        satdModeList[i] = 0;
      }
      bool modeIsEnable[NUM_INTRA_MODE + 1]; // use intra mode idx to check whether enable
      for (int i = 0; i < NUM_INTRA_MODE + 1; i++)
      {
        modeIsEnable[i] = 1;
      }

      DistParam distParam;
      const bool useHadamard = true;
      // 这里将pu模式设置为LM_L模式,为了在下面对luma重建信号进行下采样
      pu.intraDir[1] = MDLM_L_IDX; // temporary assigned, just to indicate this is a MDLM mode. for luma down-sampling operation.

      initIntraPatternChType(cu, pu.Cb()); // 获取Cb分量参考像素
      initIntraPatternChType(cu, pu.Cr()); // 获取Cr分量参考像素
      xGetLumaRecPixels(pu, pu.Cb()); 	   // CCLM预测模式亮度重建参考像素获取

// *************** 基于SATD模式粗选择 ************* //
      for (int idx = uiMinMode; idx <= uiMaxMode - 1; idx++) // 遍历所有候选
      {
        int mode = chromaCandModes[idx];
        satdModeList[idx] = mode;
        if (PU::isLMCMode(mode) && !PU::isLMCModeEnabled(pu, mode))
        {
          continue;
        }
        if ((mode == LM_CHROMA_IDX) || (mode == PLANAR_IDX) || (mode == DM_CHROMA_IDX)) // only pre-check regular modes and MDLM modes, not including DM ,Planar, and LM
        {
          continue;
        }
        pu.intraDir[1] = mode; // temporary assigned, for SATD checking.

        int64_t sad = 0;
        CodingStructure& cs = *(pu.cs);

        CompArea areaCb = pu.Cb();
        PelBuf orgCb = cs.getOrgBuf(areaCb);
        PelBuf predCb = cs.getPredBuf(areaCb);

        m_pcRdCost->setDistParam(distParam, orgCb, predCb, pu.cs->sps->getBitDepth(CHANNEL_TYPE_CHROMA), COMPONENT_Cb, useHadamard);
        distParam.applyWeight = false;

        if (PU::isLMCMode(mode)) // CCLM,LM_A,LM_L模式,进行跨分量预测
        {
          predIntraChromaLM(COMPONENT_Cb, predCb, pu, areaCb, mode);
        }
        else
        {
          predIntraAng(COMPONENT_Cb, predCb, pu, false);
        }

        sad += distParam.distFunc(distParam);

        CompArea areaCr = pu.Cr();
        PelBuf orgCr = cs.getOrgBuf(areaCr);
        PelBuf predCr = cs.getPredBuf(areaCr);

        m_pcRdCost->setDistParam(distParam, orgCr, predCr, pu.cs->sps->getBitDepth(CHANNEL_TYPE_CHROMA), COMPONENT_Cr, useHadamard);
        distParam.applyWeight = false;

        if (PU::isLMCMode(mode))
        {
          predIntraChromaLM(COMPONENT_Cr, predCr, pu, areaCr, mode);
        }
        else
        {
          predIntraAng(COMPONENT_Cr, predCr, pu, false);
        }
        sad += distParam.distFunc(distParam); // chroma 代价为Cb和Cr的和
        satdSortedCost[idx] = sad;
      }
      // sort the mode based on the cost from small to large.
      // 基于satd cost 进行模式排序,冒泡
      int tempIdx = 0;
      int64_t tempCost = 0;
      for (int i = uiMinMode; i <= uiMaxMode - 1; i++)
      {
        for (int j = i + 1; j <= uiMaxMode - 1; j++)
        {
          if (satdSortedCost[j] < satdSortedCost[i])
          {
            tempIdx = satdModeList[i];
            satdModeList[i] = satdModeList[j];
            satdModeList[j] = tempIdx;

            tempCost = satdSortedCost[i];
            satdSortedCost[i] = satdSortedCost[j];
            satdSortedCost[j] = tempCost;

          }
        }
      }
      int reducedModeNumber = 2; // reduce the number of chroma modes
      for (int i = 0; i < reducedModeNumber; i++) // 剔除两个SATD cost最大的模式
      {
        modeIsEnable[satdModeList[uiMaxMode - 1 - i]] = 0; // disable the last reducedModeNumber modes
      }

      // save the dist 记录当前cost
      Distortion baseDist = cs.dist;

// *************** 进行RQT计算RDcost ************* //
      for (uint32_t uiMode = uiMinMode; uiMode < uiMaxMode; uiMode++)
      {
        const int chromaIntraMode = chromaCandModes[uiMode];
        if( PU::isLMCMode( chromaIntraMode ) && ! PU::isLMCModeEnabled( pu, chromaIntraMode ) )
        {
          continue;
        }
        if (!modeIsEnable[chromaIntraMode] && PU::isLMCModeEnabled(pu, chromaIntraMode)) // when CCLM is disable, then MDLM is disable. not use satd checking
        {
          continue;
        }
        cs.setDecomp( pu.Cb(), false );
        cs.dist = baseDist;
        //----- restore context models -----
        m_CABACEstimator->getCtx() = ctxStart; //重置上下文

        //----- chroma coding -----
        pu.intraDir[1] = chromaIntraMode;

#if JVET_M0102_INTRA_SUBPARTITIONS
        xRecurIntraChromaCodingQT( cs, partitioner, bestCostSoFar, ispType );  // RQT计算变换量化,反变换反量化,计算重建之后distortion
        if( lumaUsesISP && cs.dist == MAX_UINT )
        {
          continue;
        }
#else
        xRecurIntraChromaCodingQT( cs, partitioner );
#endif

        if (cs.pps->getUseTransformSkip())
        {
          m_CABACEstimator->getCtx() = ctxStart;
        }

#if JVET_M0102_INTRA_SUBPARTITIONS
        uint64_t fracBits   = xGetIntraFracBitsQT( cs, partitioner, false, true, -1, ispType );
#else
        uint64_t fracBits   = xGetIntraFracBitsQT( cs, partitioner, false, true );
#endif
        Distortion uiDist = cs.dist;
        double    dCost   = m_pcRdCost->calcRdCost( fracBits, uiDist - baseDist ); // distortion = currentCost - storeCost

        //----- compare -----
        if( dCost < dBestCost ) // 更新best信息,记录预测、残差、重建信号
        {
#if JVET_M0102_INTRA_SUBPARTITIONS
          if( lumaUsesISP && dCost < bestCostSoFar )
          {
            bestCostSoFar = dCost;
          }
#endif
          for( uint32_t i = getFirstComponentOfChannel( CHANNEL_TYPE_CHROMA ); i < numberValidComponents; i++ )
          {
            const CompArea &area = pu.blocks[i];

            saveCS.getRecoBuf     ( area ).copyFrom( cs.getRecoBuf   ( area ) );
#if KEEP_PRED_AND_RESI_SIGNALS
            saveCS.getPredBuf     ( area ).copyFrom( cs.getPredBuf   ( area ) );
            saveCS.getResiBuf     ( area ).copyFrom( cs.getResiBuf   ( area ) );
#endif
#if JVET_M0427_INLOOP_RESHAPER
            saveCS.getPredBuf     ( area ).copyFrom( cs.getPredBuf   (area ) );
            cs.picture->getPredBuf( area ).copyFrom( cs.getPredBuf   (area ) );
#endif
            cs.picture->getRecoBuf( area ).copyFrom( cs.getRecoBuf( area ) );

            for( uint32_t j = 0; j < saveCS.tus.size(); j++ )
            {
              saveCS.tus[j]->copyComponentFrom( *orgTUs[j], area.compID );
            }
          }

          dBestCost  = dCost;
          uiBestDist = uiDist;
          uiBestMode = chromaIntraMode;
        }
      }

	  // 将最优信号,更新在picture buffer
      for( uint32_t i = getFirstComponentOfChannel( CHANNEL_TYPE_CHROMA ); i < numberValidComponents; i++ )
      {
        const CompArea &area = pu.blocks[i];

        cs.getRecoBuf         ( area ).copyFrom( saveCS.getRecoBuf( area ) );
#if KEEP_PRED_AND_RESI_SIGNALS
        cs.getPredBuf         ( area ).copyFrom( saveCS.getPredBuf( area ) );
        cs.getResiBuf         ( area ).copyFrom( saveCS.getResiBuf( area ) );
#endif
#if JVET_M0427_INLOOP_RESHAPER
        cs.getPredBuf         ( area ).copyFrom( saveCS.getPredBuf( area ) );
        cs.picture->getPredBuf( area ).copyFrom( cs.getPredBuf    ( area ) );
#endif

        cs.picture->getRecoBuf( area ).copyFrom( cs.    getRecoBuf( area ) );

        for( uint32_t j = 0; j < saveCS.tus.size(); j++ )
        {
          orgTUs[ j ]->copyComponentFrom( *saveCS.tus[ j ], area.compID );
        }
      }
    }

    pu.intraDir[1] = uiBestMode;
    cs.dist        = uiBestDist;
  }

  //----- restore context models ----- 更新上下文模型
  m_CABACEstimator->getCtx() = ctxStart;
#if JVET_M0102_INTRA_SUBPARTITIONS
  if( lumaUsesISP && bestCostSoFar >= maxCostAllowed )
  {
    cu.ispMode = 0;
  }
#endif
}

你可能感兴趣的:(VVC/H.266学习日记,帧内预测,色度,cclm)