Skip to content
Snippets Groups Projects
UnitTools.cpp 145 KiB
Newer Older
  • Learn to ignore specific revisions
  • rlliao's avatar
    rlliao committed
    bool PU::isUniqueTriangleCandidates( const PredictionUnit &pu, MergeCtx& triangleMrgCtx )
    
    rlliao's avatar
    rlliao committed
      int newCand = triangleMrgCtx.numValidMergeCand;
    
      for( int32_t i = 0; i < newCand; i++ )
      {
    
    rlliao's avatar
    rlliao committed
        int32_t predFlagCur  = triangleMrgCtx.interDirNeighbours[i] == 1 ? 0 : 1;
        int32_t predFlagNew  = triangleMrgCtx.interDirNeighbours[newCand] == 1 ? 0 : 1;
        int32_t refPicPocCur = pu.cs->slice->getRefPOC( (RefPicList)predFlagCur, triangleMrgCtx.mvFieldNeighbours[(i << 1) + predFlagCur].refIdx );
        int32_t refPicPocNew = pu.cs->slice->getRefPOC( (RefPicList)predFlagNew, triangleMrgCtx.mvFieldNeighbours[(newCand << 1) + predFlagNew].refIdx);
        if( refPicPocCur == refPicPocNew && triangleMrgCtx.mvFieldNeighbours[(i << 1) + predFlagCur].mv == triangleMrgCtx.mvFieldNeighbours[(newCand << 1) + predFlagNew].mv )
    
    rlliao's avatar
    rlliao committed
    bool PU::getTriangleWeights( const PredictionUnit& pu, MergeCtx &triangleMrgCtx, const uint8_t candIdx0, const uint8_t candIdx1 )
    
    rlliao's avatar
    rlliao committed
      RefPicList refPicListCand0 = triangleMrgCtx.interDirNeighbours[candIdx0] == 1 ? REF_PIC_LIST_0 : REF_PIC_LIST_1;
      RefPicList refPicListCand1 = triangleMrgCtx.interDirNeighbours[candIdx1] == 1 ? REF_PIC_LIST_0 : REF_PIC_LIST_1;
      int32_t refPicPoc0 = pu.cs->slice->getRefPOC( refPicListCand0, triangleMrgCtx.mvFieldNeighbours[ (candIdx0 << 1) + refPicListCand0 ].refIdx );
      int32_t refPicPoc1 = pu.cs->slice->getRefPOC( refPicListCand1, triangleMrgCtx.mvFieldNeighbours[ (candIdx1 << 1) + refPicListCand1 ].refIdx );
    
    rlliao's avatar
    rlliao committed
      if( refPicPoc0 != refPicPoc1 )
    
      {
        // different reference picture
        return true;
      }
      
      // same reference picture, but mv difference is larger than 16 pel
      int32_t threshold = 16 << 4;
    
    rlliao's avatar
    rlliao committed
      Mv diffMv = triangleMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + refPicListCand0].mv - triangleMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + refPicListCand1].mv;
    
    rlliao's avatar
    rlliao committed
      if( diffMv.getAbsHor() > threshold || diffMv.getAbsVer() > threshold  )
    
    rlliao's avatar
    rlliao committed
    void PU::spanTriangleMotionInfo( PredictionUnit &pu, MergeCtx &triangleMrgCtx, const uint8_t mergeIdx, const bool splitDir, const uint8_t candIdx0, const uint8_t candIdx1 )
    
    rlliao's avatar
    rlliao committed
      pu.mergeIdx  = mergeIdx;
    
      MotionBuf mb = pu.getMotionBuf();
    
    
    rlliao's avatar
    rlliao committed
      MotionInfo biMv;
      biMv.isInter  = true;
    
    rlliao's avatar
    rlliao committed
      if( triangleMrgCtx.interDirNeighbours[candIdx0] == 1 && triangleMrgCtx.interDirNeighbours[candIdx1] == 2 )
    
    rlliao's avatar
    rlliao committed
        biMv.interDir  = 3;
        biMv.mv[0]     = triangleMrgCtx.mvFieldNeighbours[ candIdx0 << 1     ].mv;
        biMv.mv[1]     = triangleMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].mv;
        biMv.refIdx[0] = triangleMrgCtx.mvFieldNeighbours[ candIdx0 << 1     ].refIdx;
        biMv.refIdx[1] = triangleMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].refIdx;
    
    rlliao's avatar
    rlliao committed
      else if( triangleMrgCtx.interDirNeighbours[candIdx0] == 2 && triangleMrgCtx.interDirNeighbours[candIdx1] == 1 )
    
    rlliao's avatar
    rlliao committed
        biMv.interDir  = 3;
        biMv.mv[0]     = triangleMrgCtx.mvFieldNeighbours[ candIdx1 << 1     ].mv;
        biMv.mv[1]     = triangleMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].mv;
        biMv.refIdx[0] = triangleMrgCtx.mvFieldNeighbours[ candIdx1 << 1     ].refIdx;
        biMv.refIdx[1] = triangleMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].refIdx;
    
    rlliao's avatar
    rlliao committed
      else if( triangleMrgCtx.interDirNeighbours[candIdx0] == 1 && triangleMrgCtx.interDirNeighbours[candIdx1] == 1 )
    
    rlliao's avatar
    rlliao committed
        int32_t refIdx = mappingRefPic( pu, pu.cs->slice->getRefPOC( REF_PIC_LIST_0, triangleMrgCtx.mvFieldNeighbours[candIdx1 << 1].refIdx ), REF_PIC_LIST_1 );
    
    rlliao's avatar
    rlliao committed
          biMv.interDir  = 3;
          biMv.mv[0]     = triangleMrgCtx.mvFieldNeighbours[candIdx0 << 1].mv;
          biMv.mv[1]     = triangleMrgCtx.mvFieldNeighbours[candIdx1 << 1].mv;
          biMv.refIdx[0] = triangleMrgCtx.mvFieldNeighbours[candIdx0 << 1].refIdx;
          biMv.refIdx[1] = refIdx;
    
    rlliao's avatar
    rlliao committed
          refIdx = mappingRefPic( pu, pu.cs->slice->getRefPOC( REF_PIC_LIST_0, triangleMrgCtx.mvFieldNeighbours[candIdx0 << 1].refIdx), REF_PIC_LIST_1 );
          biMv.interDir  = ( refIdx != -1 ) ? 3 : 1;
          biMv.mv[0]     = ( refIdx != -1 ) ? triangleMrgCtx.mvFieldNeighbours[candIdx1 << 1].mv : triangleMrgCtx.mvFieldNeighbours[candIdx0 << 1].mv;
          biMv.mv[1]     = ( refIdx != -1 ) ? triangleMrgCtx.mvFieldNeighbours[candIdx0 << 1].mv : Mv(0, 0);
          biMv.refIdx[0] = ( refIdx != -1 ) ? triangleMrgCtx.mvFieldNeighbours[candIdx1 << 1].refIdx : triangleMrgCtx.mvFieldNeighbours[candIdx0 << 1].refIdx;
          biMv.refIdx[1] = ( refIdx != -1 ) ? refIdx : -1;
    
    rlliao's avatar
    rlliao committed
      else if( triangleMrgCtx.interDirNeighbours[candIdx0] == 2 && triangleMrgCtx.interDirNeighbours[candIdx1] == 2 )
    
    rlliao's avatar
    rlliao committed
        int32_t refIdx = mappingRefPic( pu, pu.cs->slice->getRefPOC( REF_PIC_LIST_1, triangleMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].refIdx ), REF_PIC_LIST_0 );
    
    rlliao's avatar
    rlliao committed
          biMv.interDir  = 3;
          biMv.mv[0]     = triangleMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].mv;
          biMv.mv[1]     = triangleMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].mv;
          biMv.refIdx[0] = refIdx;
          biMv.refIdx[1] = triangleMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].refIdx;
    
    rlliao's avatar
    rlliao committed
          refIdx = mappingRefPic( pu, pu.cs->slice->getRefPOC( REF_PIC_LIST_1, triangleMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].refIdx ), REF_PIC_LIST_0 );
          biMv.interDir  = ( refIdx != -1 ) ? 3 : 2;
          biMv.mv[0]     = ( refIdx != -1 ) ? triangleMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].mv : Mv(0, 0);
          biMv.mv[1]     = ( refIdx != -1 ) ? triangleMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].mv : triangleMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].mv;
          biMv.refIdx[0] = ( refIdx != -1 ) ? refIdx : -1; 
          biMv.refIdx[1] = ( refIdx != -1 ) ? triangleMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].refIdx : triangleMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].refIdx;
    
    rlliao's avatar
    rlliao committed
      int32_t idxW  = (int32_t)(g_aucLog2[pu.lwidth() ] - MIN_CU_LOG2);
      int32_t idxH  = (int32_t)(g_aucLog2[pu.lheight()] - MIN_CU_LOG2);
    
      for( int32_t y = 0; y < mb.height; y++ )
      {
        for( int32_t x = 0; x < mb.width; x++ )
        {
    
    rlliao's avatar
    rlliao committed
          if( g_triangleMvStorage[splitDir][idxH][idxW][y][x] == 2 )
    
          {
            mb.at( x, y ).isInter   = true;
    
    rlliao's avatar
    rlliao committed
            mb.at( x, y ).interDir  = biMv.interDir;
            mb.at( x, y ).refIdx[0] = biMv.refIdx[0];
            mb.at( x, y ).refIdx[1] = biMv.refIdx[1];
            mb.at( x, y ).mv    [0] = biMv.mv    [0];
            mb.at( x, y ).mv    [1] = biMv.mv    [1];
    
    rlliao's avatar
    rlliao committed
          else if( g_triangleMvStorage[splitDir][idxH][idxW][y][x] == 0 )
    
          {
            mb.at( x, y ).isInter   = true;
    
    rlliao's avatar
    rlliao committed
            mb.at( x, y ).interDir  = triangleMrgCtx.interDirNeighbours[candIdx0];
            mb.at( x, y ).refIdx[0] = triangleMrgCtx.mvFieldNeighbours[ candIdx0 << 1     ].refIdx;
            mb.at( x, y ).refIdx[1] = triangleMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].refIdx;
            mb.at( x, y ).mv    [0] = triangleMrgCtx.mvFieldNeighbours[ candIdx0 << 1     ].mv;
            mb.at( x, y ).mv    [1] = triangleMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].mv;
    
    rlliao's avatar
    rlliao committed
            mb.at( x, y ).interDir  = triangleMrgCtx.interDirNeighbours[candIdx1];
            mb.at( x, y ).refIdx[0] = triangleMrgCtx.mvFieldNeighbours[ candIdx1 << 1     ].refIdx;
            mb.at( x, y ).refIdx[1] = triangleMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].refIdx;
            mb.at( x, y ).mv    [0] = triangleMrgCtx.mvFieldNeighbours[ candIdx1 << 1     ].mv;
            mb.at( x, y ).mv    [1] = triangleMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].mv;
    
          }
        }
      }
    }
    
    int32_t PU::mappingRefPic( const PredictionUnit &pu, int32_t refPicPoc, bool targetRefPicList )
    {
      int32_t numRefIdx = pu.cs->slice->getNumRefIdx( (RefPicList)targetRefPicList );
    
      for( int32_t i = 0; i < numRefIdx; i++ )
      {
        if( pu.cs->slice->getRefPOC( (RefPicList)targetRefPicList, i ) == refPicPoc )
        {
          return i;
        }
      }
      return -1;
    }
    
    
    void CU::resetMVDandMV2Int( CodingUnit& cu, InterPrediction *interPred )
    {
      for( auto &pu : CU::traversePUs( cu ) )
      {
        MergeCtx mrgCtx;
    
        if( !pu.mergeFlag )
        {
          if( pu.interDir != 2 /* PRED_L1 */ )
          {
            Mv mv        = pu.mv[0];
            Mv mvPred;
            AMVPInfo amvpInfo;
            PU::fillMvpCand(pu, REF_PIC_LIST_0, pu.refIdx[0], amvpInfo);
            pu.mvpNum[0] = amvpInfo.numCand;
    
            mvPred       = amvpInfo.mvCand[pu.mvpIdx[0]];
    
            mv.roundToAmvrSignalPrecision(MV_PRECISION_QUARTER, cu.imv);
    
            pu.mv[0]     = mv;
            Mv mvDiff    = mv - mvPred;
            pu.mvd[0]    = mvDiff;
          }
          if( pu.interDir != 1 /* PRED_L0 */ )
          {
            Mv mv        = pu.mv[1];
            Mv mvPred;
            AMVPInfo amvpInfo;
            PU::fillMvpCand(pu, REF_PIC_LIST_1, pu.refIdx[1], amvpInfo);
            pu.mvpNum[1] = amvpInfo.numCand;
    
            mvPred       = amvpInfo.mvCand[pu.mvpIdx[1]];
    
            mv.roundToAmvrSignalPrecision(MV_PRECISION_QUARTER, cu.imv);
    
            Mv mvDiff    = mv - mvPred;
    
            if( pu.cu->cs->slice->getMvdL1ZeroFlag() && pu.interDir == 3 /* PRED_BI */ )
            {
              pu.mvd[1] = Mv();
              mv = mvPred;
            }
            else
            {
              pu.mvd[1] = mvDiff;
            }
            pu.mv[1] = mv;
          }
    
        }
        else
        {
    
            PU::getInterMergeCandidates ( pu, mrgCtx 
              , 0
            );
    
    #if !JVET_M0068_M0171_MMVD_CLEANUP
    
    
            mrgCtx.setMergeInfo( pu, pu.mergeIdx );
        }
    
        PU::spanMotionInfo( pu, mrgCtx );
      }
    }
    
    bool CU::hasSubCUNonZeroMVd( const CodingUnit& cu )
    {
      bool bNonZeroMvd = false;
    
      for( const auto &pu : CU::traversePUs( cu ) )
      {
        if( ( !pu.mergeFlag ) && ( !cu.skip ) )
        {
          if( pu.interDir != 2 /* PRED_L1 */ )
          {
            bNonZeroMvd |= pu.mvd[REF_PIC_LIST_0].getHor() != 0;
            bNonZeroMvd |= pu.mvd[REF_PIC_LIST_0].getVer() != 0;
          }
          if( pu.interDir != 1 /* PRED_L0 */ )
          {
            if( !pu.cu->cs->slice->getMvdL1ZeroFlag() || pu.interDir != 3 /* PRED_BI */ )
            {
              bNonZeroMvd |= pu.mvd[REF_PIC_LIST_1].getHor() != 0;
              bNonZeroMvd |= pu.mvd[REF_PIC_LIST_1].getVer() != 0;
            }
          }
        }
      }
    
      return bNonZeroMvd;
    }
    
    int CU::getMaxNeighboriMVCandNum( const CodingStructure& cs, const Position& pos )
    {
      const int  numDefault     = 0;
      int        maxImvNumCand  = 0;
    
      // Get BCBP of left PU
    #if HEVC_TILES_WPP
      const CodingUnit *cuLeft  = cs.getCURestricted( pos.offset( -1, 0 ), cs.slice->getIndependentSliceIdx(), cs.picture->tileMap->getTileIdxMap( pos ), CH_L );
    #else
      const CodingUnit *cuLeft  = cs.getCURestricted( pos.offset( -1, 0 ), cs.slice->getIndependentSliceIdx(), CH_L );
    #endif
      maxImvNumCand = ( cuLeft ) ? cuLeft->imvNumCand : numDefault;
    
      // Get BCBP of above PU
    #if HEVC_TILES_WPP
      const CodingUnit *cuAbove = cs.getCURestricted( pos.offset( 0, -1 ), cs.slice->getIndependentSliceIdx(), cs.picture->tileMap->getTileIdxMap( pos ), CH_L );
    #else
      const CodingUnit *cuAbove = cs.getCURestricted( pos.offset( 0, -1 ), cs.slice->getIndependentSliceIdx(), CH_L );
    #endif
      maxImvNumCand = std::max( maxImvNumCand, ( cuAbove ) ? cuAbove->imvNumCand : numDefault );
    
      return maxImvNumCand;
    }
    
    
    bool CU::isGBiIdxCoded( const CodingUnit &cu )
    {
      if( cu.cs->sps->getSpsNext().getUseGBi() == false )
      {
        CHECK(cu.GBiIdx != GBI_DEFAULT, "Error: cu.GBiIdx != GBI_DEFAULT");
        return false;
      }
    
      if( cu.predMode == MODE_INTRA || cu.cs->slice->isInterP() )
      {
        return false;
      }
    
      if( cu.lwidth() * cu.lheight() < GBI_SIZE_CONSTRAINT )
      {
        return false;
      }
    
      if( cu.firstPU->interDir == 3 && !cu.firstPU->mergeFlag )
      {
        return true;
      }
    
      return false;
    }
    
    uint8_t CU::getValidGbiIdx( const CodingUnit &cu )
    {
      if( cu.firstPU->interDir == 3 && !cu.firstPU->mergeFlag )
      {
        return cu.GBiIdx;
      }
      else if( cu.firstPU->interDir == 3 && cu.firstPU->mergeFlag && cu.firstPU->mergeType == MRG_TYPE_DEFAULT_N )
      {
        // This is intended to do nothing here.
      }
      else if( cu.firstPU->mergeFlag && cu.firstPU->mergeType == MRG_TYPE_SUBPU_ATMVP )
      {
        CHECK(cu.GBiIdx != GBI_DEFAULT, " cu.GBiIdx != GBI_DEFAULT ");
      }
      else
      {
        CHECK(cu.GBiIdx != GBI_DEFAULT, " cu.GBiIdx != GBI_DEFAULT ");
      }
    
      return GBI_DEFAULT;
    }
    
    void CU::setGbiIdx( CodingUnit &cu, uint8_t uh )
    {
      int8_t uhCnt = 0;
    
      if( cu.firstPU->interDir == 3 && !cu.firstPU->mergeFlag )
      {
        cu.GBiIdx = uh;
        ++uhCnt;
      }
      else if( cu.firstPU->interDir == 3 && cu.firstPU->mergeFlag && cu.firstPU->mergeType == MRG_TYPE_DEFAULT_N )
      {
        // This is intended to do nothing here.
      }
      else if( cu.firstPU->mergeFlag && cu.firstPU->mergeType == MRG_TYPE_SUBPU_ATMVP )
      {
        cu.GBiIdx = GBI_DEFAULT;
      }
      else
      {
        cu.GBiIdx = GBI_DEFAULT;
      }
    
      CHECK(uhCnt <= 0, " uhCnt <= 0 ");
    }
    
    uint8_t CU::deriveGbiIdx( uint8_t gbiLO, uint8_t gbiL1 )
    {
      if( gbiLO == gbiL1 )
      {
        return gbiLO;
      }
      const int8_t w0 = getGbiWeight(gbiLO, REF_PIC_LIST_0);
      const int8_t w1 = getGbiWeight(gbiL1, REF_PIC_LIST_1);
      const int8_t th = g_GbiWeightBase >> 1;
      const int8_t off = 1;
    
      if( w0 == w1 || (w0 < (th - off) && w1 < (th - off)) || (w0 >(th + off) && w1 >(th + off)) )
      {
        return GBI_DEFAULT;
      }
      else
      {
        if( w0 > w1 )
        {
          return ( w0 >= th ? gbiLO : gbiL1 );
        }
        else
        {
          return ( w1 >= th ? gbiL1 : gbiLO );
        }
      }
    }
    
    
    // TU tools
    
    #if HEVC_USE_4x4_DSTVII
    bool TU::useDST(const TransformUnit &tu, const ComponentID &compID)
    {
      return isLuma(compID) && tu.cu->predMode == MODE_INTRA;
    }
    
    #endif
    
    bool TU::isNonTransformedResidualRotated(const TransformUnit &tu, const ComponentID &compID)
    {
      return tu.cs->sps->getSpsRangeExtension().getTransformSkipRotationEnabledFlag() && tu.blocks[compID].width == 4 && tu.cu->predMode == MODE_INTRA;
    }
    
    bool TU::getCbf( const TransformUnit &tu, const ComponentID &compID )
    {
      return getCbfAtDepth( tu, compID, tu.depth );
    }
    
    bool TU::getCbfAtDepth(const TransformUnit &tu, const ComponentID &compID, const unsigned &depth)
    {
      return ((tu.cbf[compID] >> depth) & 1) == 1;
    }
    
    void TU::setCbfAtDepth(TransformUnit &tu, const ComponentID &compID, const unsigned &depth, const bool &cbf)
    {
      // first clear the CBF at the depth
      tu.cbf[compID] &= ~(1  << depth);
      // then set the CBF
      tu.cbf[compID] |= ((cbf ? 1 : 0) << depth);
    }
    
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #if JVET_M0464_UNI_MTS
    bool TU::isTSAllowed(const TransformUnit &tu, const ComponentID compID)
    {
      bool    tsAllowed = compID == COMPONENT_Y;
      const int maxSize = tu.cs->pps->getPpsRangeExtension().getLog2MaxTransformSkipBlockSize();
    
      tsAllowed &= tu.cs->pps->getUseTransformSkip();
      tsAllowed &= !tu.cu->transQuantBypass;
    
      SizeType transformSkipMaxSize = 1 << maxSize;
      tsAllowed &= tu.lwidth() <= transformSkipMaxSize && tu.lheight() <= transformSkipMaxSize;
    
      return tsAllowed;
    }
    
    bool TU::isMTSAllowed(const TransformUnit &tu, const ComponentID compID)
    {
      bool   mtsAllowed = compID == COMPONENT_Y;
      const int maxSize = CU::isIntra( *tu.cu ) ? MTS_INTRA_MAX_CU_SIZE : MTS_INTER_MAX_CU_SIZE;
    
      mtsAllowed &= CU::isIntra( *tu.cu ) ? tu.cs->sps->getSpsNext().getUseIntraMTS() : tu.cs->sps->getSpsNext().getUseInterMTS();
      mtsAllowed &= ( tu.lwidth() <= maxSize && tu.lheight() <= maxSize );
      return mtsAllowed;
    }
    #else
    
    bool TU::hasTransformSkipFlag(const CodingStructure& cs, const CompArea& area)
    {
      uint32_t transformSkipLog2MaxSize = cs.pps->getPpsRangeExtension().getLog2MaxTransformSkipBlockSize();
    
    
    Karsten Suehring's avatar
    Karsten Suehring committed
      SizeType transformSkipMaxSize = 1 << transformSkipLog2MaxSize;
      return area.width <= transformSkipMaxSize && area.height <= transformSkipMaxSize;
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #endif
    
    
    uint32_t TU::getGolombRiceStatisticsIndex(const TransformUnit &tu, const ComponentID &compID)
    {
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #if JVET_M0464_UNI_MTS
      const bool transformSkip    = tu.mtsIdx==1;
    #else
    
      const bool transformSkip    = tu.transformSkip[compID];
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #endif
    
      const bool transquantBypass = tu.cu->transQuantBypass;
    
      //--------
    
      const uint32_t channelTypeOffset = isChroma(compID) ? 2 : 0;
      const uint32_t nonTransformedOffset = (transformSkip || transquantBypass) ? 1 : 0;
    
      //--------
    
      const uint32_t selectedIndex = channelTypeOffset + nonTransformedOffset;
      CHECK( selectedIndex >= RExt__GOLOMB_RICE_ADAPTATION_STATISTICS_SETS, "Invalid golomb rice adaptation statistics set" );
    
      return selectedIndex;
    }
    
    #if HEVC_USE_MDCS
    uint32_t TU::getCoefScanIdx(const TransformUnit &tu, const ComponentID &compID)
    {
      //------------------------------------------------
    
      //this mechanism is available for intra only
    
      if( !CU::isIntra( *tu.cu ) )
      {
        return SCAN_DIAG;
      }
    
      //------------------------------------------------
    
      //check that MDCS can be used for this TU
    
    
      const CompArea &area      = tu.blocks[compID];
      const SPS &sps            = *tu.cs->sps;
      const ChromaFormat format = sps.getChromaFormatIdc();
    
    
      const uint32_t maximumWidth  = MDCS_MAXIMUM_WIDTH  >> getComponentScaleX(compID, format);
      const uint32_t maximumHeight = MDCS_MAXIMUM_HEIGHT >> getComponentScaleY(compID, format);
    
      if ((area.width > maximumWidth) || (area.height > maximumHeight))
      {
        return SCAN_DIAG;
      }
    
      //------------------------------------------------
    
      //otherwise, select the appropriate mode
    
      const PredictionUnit &pu = *tu.cs->getPU( area.pos(), toChannelType( compID ) );
    
      uint32_t uiDirMode = PU::getFinalIntraMode(pu, toChannelType(compID));
    
      //------------------
    
           if (abs((int) uiDirMode - VER_IDX) <= MDCS_ANGLE_LIMIT)
      {
        return SCAN_HOR;
      }
      else if (abs((int) uiDirMode - HOR_IDX) <= MDCS_ANGLE_LIMIT)
      {
        return SCAN_VER;
      }
      else
      {
        return SCAN_DIAG;
      }
    }
    
    #endif
    bool TU::hasCrossCompPredInfo( const TransformUnit &tu, const ComponentID &compID )
    {
      return ( isChroma(compID) && tu.cs->pps->getPpsRangeExtension().getCrossComponentPredictionEnabledFlag() && TU::getCbf( tu, COMPONENT_Y ) &&
             ( CU::isInter(*tu.cu) || PU::isChromaIntraModeCrossCheckMode( *tu.cs->getPU( tu.blocks[compID].pos(), toChannelType( compID ) ) ) ) );
    }
    
    uint32_t TU::getNumNonZeroCoeffsNonTS( const TransformUnit& tu, const bool bLuma, const bool bChroma )
    {
      uint32_t count = 0;
      for( uint32_t i = 0; i < ::getNumberValidTBlocks( *tu.cs->pcv ); i++ )
      {
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #if JVET_M0464_UNI_MTS
        if( tu.blocks[i].valid() && ( isLuma(ComponentID(i)) ? tu.mtsIdx !=1 : true ) && TU::getCbf( tu, ComponentID( i ) ) )
    #else
    
        if( tu.blocks[i].valid() && !tu.transformSkip[i] && TU::getCbf( tu, ComponentID( i ) ) )
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #endif
    
        {
          if( isLuma  ( tu.blocks[i].compID ) && !bLuma   ) continue;
          if( isChroma( tu.blocks[i].compID ) && !bChroma ) continue;
    
          uint32_t area = tu.blocks[i].area();
          const TCoeff* coeff = tu.getCoeffs( ComponentID( i ) ).buf;
          for( uint32_t j = 0; j < area; j++ )
          {
            count += coeff[j] != 0;
          }
        }
      }
      return count;
    }
    
    bool TU::needsSqrt2Scale( const Size& size )
    {
      return (((g_aucLog2[size.width] + g_aucLog2[size.height]) & 1) == 1);
    }
    
    #if HM_QTBT_AS_IN_JEM_QUANT
    
    bool TU::needsBlockSizeTrafoScale( const Size& size )
    {
      return needsSqrt2Scale( size ) || isNonLog2BlockSize( size );
    }
    #else
    bool TU::needsQP3Offset(const TransformUnit &tu, const ComponentID &compID)
    {
    
    Karsten Suehring's avatar
    Karsten Suehring committed
      if( !tu.transformSkip[compID] )
    
      {
        return ( ( ( g_aucLog2[tu.blocks[compID].width] + g_aucLog2[tu.blocks[compID].height] ) & 1 ) == 1 );
      }
      return false;
    }
    #endif
    
    
    
    
    
    // other tools
    
    uint32_t getCtuAddr( const Position& pos, const PreCalcValues& pcv )
    {
      return ( pos.x >> pcv.maxCUWidthLog2 ) + ( pos.y >> pcv.maxCUHeightLog2 ) * pcv.widthInCtus;
    }