Skip to content
Snippets Groups Projects
EncModeCtrl.cpp 63.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • #if REUSE_CU_RESULTS_WITH_MULTIPLE_TUS
      CHECKD( !( encInfo.numTus > 0 ), "Empty tus array" );
      for( int i = 0; i < encInfo.numTus; i++ )
      {
        TransformUnit  &tu = cs.addTU( encInfo.tus[i], partitioner.chType );
    
        for( auto &blk : tu.blocks )
        {
          if( blk.valid() ) tu.copyComponentFrom( encInfo.tus[i], blk.compID );
        }
      }
    #else
    
      for( auto &blk : tu.blocks )
      {
        if( blk.valid() ) tu.copyComponentFrom( encInfo.tu, blk.compID );
      }
    
    
      testMode    = encInfo.testMode;
    
      return true;
    }
    
    #endif
    
    static bool interHadActive( const ComprCUCtx& ctx )
    {
      return ctx.interHad != 0;
    }
    
    //////////////////////////////////////////////////////////////////////////
    // EncModeCtrlQTBT
    //////////////////////////////////////////////////////////////////////////
    
    EncModeCtrlMTnoRQT::EncModeCtrlMTnoRQT()
    {
    #if !REUSE_CU_RESULTS
      CacheBlkInfoCtrl::create();
    #endif
    }
    
    EncModeCtrlMTnoRQT::~EncModeCtrlMTnoRQT()
    {
    #if !REUSE_CU_RESULTS
      CacheBlkInfoCtrl::destroy();
    #endif
    }
    
    #if REUSE_CU_RESULTS
    void EncModeCtrlMTnoRQT::create( const EncCfg& cfg )
    {
      CacheBlkInfoCtrl::create();
      BestEncInfoCache::create( cfg.getChromaFormatIdc() );
    
    #if JVET_M0140_SBT
      SaveLoadEncInfoSbt::create();
    #endif
    
    }
    
    void EncModeCtrlMTnoRQT::destroy()
    {
      CacheBlkInfoCtrl::destroy();
      BestEncInfoCache::destroy();
    
    #if JVET_M0140_SBT
      SaveLoadEncInfoSbt::destroy();
    #endif
    
    }
    
    #endif
    void EncModeCtrlMTnoRQT::initCTUEncoding( const Slice &slice )
    {
      CacheBlkInfoCtrl::init( slice );
    #if REUSE_CU_RESULTS
      BestEncInfoCache::init( slice );
    #endif
    
    #if JVET_M0140_SBT
      SaveLoadEncInfoSbt::init( slice );
    #endif
    
    
      CHECK( !m_ComprCUCtxList.empty(), "Mode list is not empty at the beginning of a CTU" );
    
      m_slice             = &slice;
    #if ENABLE_SPLIT_PARALLELISM
      m_runNextInParallel      = false;
    #endif
    
      if( m_pcEncCfg->getUseE0023FastEnc() )
      {
    
        if (m_pcEncCfg->getUseCompositeRef())
          m_skipThreshold = ( ( slice.getMinPictureDistance() <= PICTURE_DISTANCE_TH * 2 ) ? FAST_SKIP_DEPTH : SKIP_DEPTH );
        else
          m_skipThreshold = ((slice.getMinPictureDistance() <= PICTURE_DISTANCE_TH) ? FAST_SKIP_DEPTH : SKIP_DEPTH);
    
    
      }
      else
      {
        m_skipThreshold = SKIP_DEPTH;
      }
    }
    
    
    void EncModeCtrlMTnoRQT::initCULevel( Partitioner &partitioner, const CodingStructure& cs )
    {
      // Min/max depth
      unsigned minDepth = 0;
    
      unsigned maxDepth = g_aucLog2[cs.sps->getCTUSize()] - g_aucLog2[cs.sps->getMinQTSize( m_slice->getSliceType(), partitioner.chType )];
    
      if( m_pcEncCfg->getUseFastLCTU() )
      {
        if( auto adPartitioner = dynamic_cast<AdaptiveDepthPartitioner*>( &partitioner ) )
        {
          // LARGE CTU
          adPartitioner->setMaxMinDepth( minDepth, maxDepth, cs );
        }
      }
    
      m_ComprCUCtxList.push_back( ComprCUCtx( cs, minDepth, maxDepth, NUM_EXTRA_FEATURES ) );
    
    #if ENABLE_SPLIT_PARALLELISM
      if( m_runNextInParallel )
      {
        for( auto &level : m_ComprCUCtxList )
        {
          CHECK( level.isLevelSplitParallel, "Tring to parallelize a level within parallel execution!" );
        }
        CHECK( cs.picture->scheduler.getSplitJobId() == 0, "Trying to run a parallel level although jobId is 0!" );
        m_runNextInParallel                          = false;
        m_ComprCUCtxList.back().isLevelSplitParallel = true;
      }
    
    #endif
      const CodingUnit* cuLeft  = cs.getCU( cs.area.blocks[partitioner.chType].pos().offset( -1, 0 ), partitioner.chType );
      const CodingUnit* cuAbove = cs.getCU( cs.area.blocks[partitioner.chType].pos().offset( 0, -1 ), partitioner.chType );
    
      const bool qtBeforeBt = ( (  cuLeft  &&  cuAbove  && cuLeft ->qtDepth > partitioner.currQtDepth && cuAbove->qtDepth > partitioner.currQtDepth )
                             || (  cuLeft  && !cuAbove  && cuLeft ->qtDepth > partitioner.currQtDepth )
                             || ( !cuLeft  &&  cuAbove  && cuAbove->qtDepth > partitioner.currQtDepth )
                             || ( !cuAbove && !cuLeft   && cs.area.lwidth() >= ( 32 << cs.slice->getDepth() ) ) )
                             && ( cs.area.lwidth() > ( cs.pcv->getMinQtSize( *cs.slice, partitioner.chType ) << 1 ) );
    
      // set features
      ComprCUCtx &cuECtx  = m_ComprCUCtxList.back();
      cuECtx.set( BEST_NON_SPLIT_COST,  MAX_DOUBLE );
      cuECtx.set( BEST_VERT_SPLIT_COST, MAX_DOUBLE );
      cuECtx.set( BEST_HORZ_SPLIT_COST, MAX_DOUBLE );
      cuECtx.set( BEST_TRIH_SPLIT_COST, MAX_DOUBLE );
      cuECtx.set( BEST_TRIV_SPLIT_COST, MAX_DOUBLE );
      cuECtx.set( DO_TRIH_SPLIT,        cs.sps->getSpsNext().getMTTMode() & 1 );
      cuECtx.set( DO_TRIV_SPLIT,        cs.sps->getSpsNext().getMTTMode() & 1 );
      cuECtx.set( BEST_IMV_COST,        MAX_DOUBLE * .5 );
      cuECtx.set( BEST_NO_IMV_COST,     MAX_DOUBLE * .5 );
      cuECtx.set( QT_BEFORE_BT,         qtBeforeBt );
      cuECtx.set( DID_QUAD_SPLIT,       false );
      cuECtx.set( IS_BEST_NOSPLIT_SKIP, false );
      cuECtx.set( MAX_QT_SUB_DEPTH,     0 );
    
      // QP
      int baseQP = cs.baseQP;
      if( m_pcEncCfg->getUseAdaptiveQP() )
      {
    
        if (!CS::isDualITree(cs) || isLuma(partitioner.chType))
    
        {
          baseQP = Clip3(-cs.sps->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, baseQP + xComputeDQP(cs, partitioner));
        }
    
      int minQP = baseQP;
      int maxQP = baseQP;
    
    #if SHARP_LUMA_DELTA_QP
      if( m_pcEncCfg->getLumaLevelToDeltaQPMapping().isEnabled() )
      {
        if( partitioner.currDepth <= cs.pps->getMaxCuDQPDepth() )
        {
          CompArea clipedArea = clipArea( cs.area.Y(), cs.picture->Y() );
          // keep using the same m_QP_LUMA_OFFSET in the same CTU
          m_lumaQPOffset = calculateLumaDQP( cs.getOrgBuf( clipedArea ) );
        }
      }
    #endif
    
      xGetMinMaxQP( minQP, maxQP, cs, partitioner, baseQP, *cs.sps, *cs.pps, true );
    
    Yu Han's avatar
    Yu Han committed
      bool checkIbc = true;
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      if (cs.chType == CHANNEL_TYPE_CHROMA)
      {
    
    Yu Han's avatar
    Yu Han committed
        IbcLumaCoverage ibcLumaCoverage = cs.getIbcLumaCoverage(cs.area.Cb());
        switch (ibcLumaCoverage)
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
        {
    
    Yu Han's avatar
    Yu Han committed
        case IBC_LUMA_COVERAGE_FULL:
          // check IBC 
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
          break;
    
    Yu Han's avatar
    Yu Han committed
        case IBC_LUMA_COVERAGE_PARTIAL:
          // do not check IBC
          checkIbc = false;
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
          break;
    
    Yu Han's avatar
    Yu Han committed
        case IBC_LUMA_COVERAGE_NONE:
          // do not check IBC
          checkIbc = false;
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
          break;
        default:
    
    Yu Han's avatar
    Yu Han committed
          THROW("Unknown IBC luma coverage type");
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
        }
      }
    
      // Add coding modes here
      // NOTE: Working back to front, as a stack, which is more efficient with the container
      // NOTE: First added modes will be processed at the end.
    
      //////////////////////////////////////////////////////////////////////////
      // Add unit split modes
    
      if( !cuECtx.get<bool>( QT_BEFORE_BT ) )
      {
        for( int qp = maxQP; qp >= minQP; qp-- )
        {
    
    Karsten Suehring's avatar
    Karsten Suehring committed
          m_ComprCUCtxList.back().testModes.push_back( { ETM_SPLIT_QT, ETO_STANDARD, qp, false } );
    
        }
      }
    
      if( partitioner.canSplit( CU_TRIV_SPLIT, cs ) )
      {
        // add split modes
        for( int qp = maxQP; qp >= minQP; qp-- )
        {
    
    Karsten Suehring's avatar
    Karsten Suehring committed
          m_ComprCUCtxList.back().testModes.push_back( { ETM_SPLIT_TT_V, ETO_STANDARD, qp, false } );
    
        }
      }
    
      if( partitioner.canSplit( CU_TRIH_SPLIT, cs ) )
      {
        // add split modes
        for( int qp = maxQP; qp >= minQP; qp-- )
        {
    
    Karsten Suehring's avatar
    Karsten Suehring committed
          m_ComprCUCtxList.back().testModes.push_back( { ETM_SPLIT_TT_H, ETO_STANDARD, qp, false } );
    
        }
      }
    
      if( partitioner.canSplit( CU_VERT_SPLIT, cs ) )
      {
        // add split modes
        for( int qp = maxQP; qp >= minQP; qp-- )
        {
    
    Karsten Suehring's avatar
    Karsten Suehring committed
          m_ComprCUCtxList.back().testModes.push_back( { ETM_SPLIT_BT_V, ETO_STANDARD, qp, false } );
    
        }
        m_ComprCUCtxList.back().set( DID_VERT_SPLIT, true );
      }
      else
      {
        m_ComprCUCtxList.back().set( DID_VERT_SPLIT, false );
      }
    
      if( partitioner.canSplit( CU_HORZ_SPLIT, cs ) )
      {
        // add split modes
        for( int qp = maxQP; qp >= minQP; qp-- )
        {
    
    Karsten Suehring's avatar
    Karsten Suehring committed
          m_ComprCUCtxList.back().testModes.push_back( { ETM_SPLIT_BT_H, ETO_STANDARD, qp, false } );
    
        }
        m_ComprCUCtxList.back().set( DID_HORZ_SPLIT, true );
      }
      else
      {
        m_ComprCUCtxList.back().set( DID_HORZ_SPLIT, false );
      }
    
      if( cuECtx.get<bool>( QT_BEFORE_BT ) )
      {
        for( int qp = maxQP; qp >= minQP; qp-- )
        {
    
    Karsten Suehring's avatar
    Karsten Suehring committed
          m_ComprCUCtxList.back().testModes.push_back( { ETM_SPLIT_QT, ETO_STANDARD, qp, false } );
    
        }
      }
    
      m_ComprCUCtxList.back().testModes.push_back( { ETM_POST_DONT_SPLIT } );
    
      xGetMinMaxQP( minQP, maxQP, cs, partitioner, baseQP, *cs.sps, *cs.pps, false );
    
      bool useLossless = false;
      int  lowestQP = minQP;
      if( cs.pps->getTransquantBypassEnabledFlag() )
      {
        useLossless = true; // mark that the first iteration is to cost TQB mode.
        minQP = minQP - 1;  // increase loop variable range by 1, to allow testing of TQB mode along with other QPs
    
        if( m_pcEncCfg->getCUTransquantBypassFlagForceValue() )
        {
          maxQP = minQP;
        }
      }
    
      //////////////////////////////////////////////////////////////////////////
      // Add unit coding modes: Intra, InterME, InterMerge ...
    
      for( int qpLoop = maxQP; qpLoop >= minQP; qpLoop-- )
      {
        const int  qp       = std::max( qpLoop, lowestQP );
        const bool lossless = useLossless && qpLoop == minQP;
    
    #if REUSE_CU_RESULTS
    
        const bool isReusingCu = isValid( cs, partitioner, qp );
        cuECtx.set( IS_REUSING_CU, isReusingCu );
        if( isReusingCu )
        {
    
          m_ComprCUCtxList.back().testModes.push_back( {ETM_RECO_CACHED, ETO_STANDARD, qp, lossless} );
    
    Karsten Suehring's avatar
    Karsten Suehring committed
        m_ComprCUCtxList.back().testModes.push_back( { ETM_IPCM,  ETO_STANDARD, qp, lossless } );
        m_ComprCUCtxList.back().testModes.push_back( { ETM_INTRA, ETO_STANDARD, qp, lossless } );
    
    Yu Han's avatar
    Yu Han committed
        // add ibc mode to intra path
    
    Yu Han's avatar
    Yu Han committed
    #if JVET_M0483_IBC
        if (cs.sps->getIBCFlag() && checkIbc)
    #else
    
    Yu Han's avatar
    Yu Han committed
        if (cs.sps->getSpsNext().getIBCMode() && checkIbc )
    
    Yu Han's avatar
    Yu Han committed
    #endif
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
        {
    
    Yu Han's avatar
    Yu Han committed
          m_ComprCUCtxList.back().testModes.push_back({ ETM_IBC,         ETO_STANDARD,  qp, lossless });
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
          if (cs.chType == CHANNEL_TYPE_LUMA)
          {
    
    Yu Han's avatar
    Yu Han committed
            m_ComprCUCtxList.back().testModes.push_back({ ETM_IBC_MERGE,   ETO_STANDARD,  qp, lossless });
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
          }
        }
    
      if( !m_slice->isIRAP() )
    
      {
        for( int qpLoop = maxQP; qpLoop >= minQP; qpLoop-- )
        {
          const int  qp       = std::max( qpLoop, lowestQP );
          const bool lossless = useLossless && qpLoop == minQP;
    
    #if JVET_M0246_AFFINE_AMVR
          if( m_pcEncCfg->getIMV() || m_pcEncCfg->getUseAffineAmvr() )
    #else
    
    #if JVET_M0246_AFFINE_AMVR
            if( m_pcEncCfg->getIMV() == IMV_4PEL || m_pcEncCfg->getUseAffineAmvr() )
    #else
    
            {
              int imv = m_pcEncCfg->getIMV4PelFast() ? 3 : 2;
    
    Karsten Suehring's avatar
    Karsten Suehring committed
              m_ComprCUCtxList.back().testModes.push_back( { ETM_INTER_ME, EncTestModeOpts( imv << ETO_IMV_SHIFT ), qp, lossless } );
    
    Karsten Suehring's avatar
    Karsten Suehring committed
            m_ComprCUCtxList.back().testModes.push_back( { ETM_INTER_ME, EncTestModeOpts( 1 << ETO_IMV_SHIFT ), qp, lossless } );
    
          }
          // add inter modes
          if( m_pcEncCfg->getUseEarlySkipDetection() )
          {
    
            if( cs.sps->getSpsNext().getUseTriangle() && cs.slice->isInterB() )
            {
    
    Karsten Suehring's avatar
    Karsten Suehring committed
              m_ComprCUCtxList.back().testModes.push_back( { ETM_MERGE_TRIANGLE, ETO_STANDARD, qp, lossless } );
    
    Karsten Suehring's avatar
    Karsten Suehring committed
            m_ComprCUCtxList.back().testModes.push_back( { ETM_MERGE_SKIP,  ETO_STANDARD, qp, lossless } );
    
            if ( cs.sps->getSpsNext().getUseAffine() || cs.sps->getSBTMVPEnabledFlag() )
    
    Karsten Suehring's avatar
    Karsten Suehring committed
              m_ComprCUCtxList.back().testModes.push_back( { ETM_AFFINE,    ETO_STANDARD, qp, lossless } );
    
    Karsten Suehring's avatar
    Karsten Suehring committed
            m_ComprCUCtxList.back().testModes.push_back( { ETM_INTER_ME,    ETO_STANDARD, qp, lossless } );
    
    Karsten Suehring's avatar
    Karsten Suehring committed
            m_ComprCUCtxList.back().testModes.push_back( { ETM_INTER_ME,    ETO_STANDARD, qp, lossless } );
    
            if( cs.sps->getSpsNext().getUseTriangle() && cs.slice->isInterB() )
            {
    
    Karsten Suehring's avatar
    Karsten Suehring committed
              m_ComprCUCtxList.back().testModes.push_back( { ETM_MERGE_TRIANGLE, ETO_STANDARD, qp, lossless } );
    
    Karsten Suehring's avatar
    Karsten Suehring committed
            m_ComprCUCtxList.back().testModes.push_back( { ETM_MERGE_SKIP,  ETO_STANDARD, qp, lossless } );
    
            if ( cs.sps->getSpsNext().getUseAffine() || cs.sps->getSBTMVPEnabledFlag() )
    
    Karsten Suehring's avatar
    Karsten Suehring committed
              m_ComprCUCtxList.back().testModes.push_back( { ETM_AFFINE,    ETO_STANDARD, qp, lossless } );
    
    #if JVET_M0253_HASH_ME
          if (m_pcEncCfg->getUseHashME())
          {
            if ((cs.area.lwidth() == cs.area.lheight() && cs.area.lwidth() <= 64 && cs.area.lwidth() >= 4) || (cs.area.lwidth() == 4 && cs.area.lheight() == 8) || (cs.area.lwidth() == 8 && cs.area.lheight() == 4))
            {
              m_ComprCUCtxList.back().testModes.push_back({ ETM_HASH_INTER, ETO_STANDARD, qp, lossless });
            }
          }
    #endif
    
        }
      }
    
      // ensure to skip unprobable modes
      if( !tryModeMaster( m_ComprCUCtxList.back().testModes.back(), cs, partitioner ) )
      {
        nextMode( cs, partitioner );
      }
    
      m_ComprCUCtxList.back().lastTestMode = EncTestMode();
    }
    
    void EncModeCtrlMTnoRQT::finishCULevel( Partitioner &partitioner )
    {
      m_ComprCUCtxList.pop_back();
    }
    
    
    bool EncModeCtrlMTnoRQT::tryMode( const EncTestMode& encTestmode, const CodingStructure &cs, Partitioner& partitioner )
    {
      ComprCUCtx& cuECtx = m_ComprCUCtxList.back();
    
      // Fast checks, partitioning depended
    
    #if JVET_M0253_HASH_ME
      if (cuECtx.isHashPerfectMatch && encTestmode.type != ETM_MERGE_SKIP && encTestmode.type != ETM_AFFINE && encTestmode.type != ETM_MERGE_TRIANGLE)
      {
        return false;
      }
    #endif
    
    
      // if early skip detected, skip all modes checking but the splits
    
    Karsten Suehring's avatar
    Karsten Suehring committed
      if( cuECtx.earlySkip && m_pcEncCfg->getUseEarlySkipDetection() && !isModeSplit( encTestmode ) && !( isModeInter( encTestmode ) ) )
    
      {
        return false;
      }
    
      const PartSplit implicitSplit = partitioner.getImplicitSplit( cs );
      const bool isBoundary         = implicitSplit != CU_DONT_SPLIT;
    
      if( isBoundary && encTestmode.type != ETM_SPLIT_QT )
      {
        return getPartSplit( encTestmode ) == implicitSplit;
      }
      else if( isBoundary && encTestmode.type == ETM_SPLIT_QT )
      {
        return partitioner.canSplit( CU_QUAD_SPLIT, cs );
      }
    
    #if REUSE_CU_RESULTS
      if( cuECtx.get<bool>( IS_REUSING_CU ) )
      {
        if( encTestmode.type == ETM_RECO_CACHED )
        {
          return true;
        }
    
        if( isModeNoSplit( encTestmode ) )
        {
          return false;
        }
      }
    
    #endif
      const Slice&           slice       = *m_slice;
      const SPS&             sps         = *slice.getSPS();
      const uint32_t             numComp     = getNumberValidComponents( slice.getSPS()->getChromaFormatIdc() );
      const uint32_t             width       = partitioner.currArea().lumaSize().width;
      const CodingStructure *bestCS      = cuECtx.bestCS;
      const CodingUnit      *bestCU      = cuECtx.bestCU;
      const EncTestMode      bestMode    = bestCS ? getCSEncMode( *bestCS ) : EncTestMode();
    
      CodedCUInfo    &relatedCU          = getBlkInfo( partitioner.currArea() );
    
      if( cuECtx.minDepth > partitioner.currQtDepth && partitioner.canSplit( CU_QUAD_SPLIT, cs ) )
      {
        // enforce QT
        return encTestmode.type == ETM_SPLIT_QT;
      }
      else if( encTestmode.type == ETM_SPLIT_QT && cuECtx.maxDepth <= partitioner.currQtDepth )
      {
        // don't check this QT depth
        return false;
      }
    
      if( bestCS && bestCS->cus.size() == 1 )
      {
        // update the best non-split cost
        cuECtx.set( BEST_NON_SPLIT_COST, bestCS->cost );
      }
    
      if( encTestmode.type == ETM_INTRA )
      {
        if( getFastDeltaQp() )
        {
          if( cs.area.lumaSize().width > cs.pcv->fastDeltaQPCuMaxSize )
          {
            return false; // only check necessary 2Nx2N Intra in fast delta-QP mode
          }
        }
    
        if( m_pcEncCfg->getUseFastLCTU() && partitioner.currArea().lumaSize().area() > 4096 )
        {
          return false;
        }
    
        if (CS::isDualITree(cs) && (partitioner.currArea().lumaSize().width > 64 || partitioner.currArea().lumaSize().height > 64))
        {
          return false;
        }
    
    
    Yu Han's avatar
    Yu Han committed
    #if JVET_M0483_IBC
    
    Yu Han's avatar
    Yu Han committed
        if (m_pcEncCfg->getUsePbIntraFast() && (!cs.slice->isIntra() || cs.slice->getSPS()->getIBCFlag()) && !interHadActive(cuECtx) && cuECtx.bestCU && !CU::isIntra(*cuECtx.bestCU))
    
    Yu Han's avatar
    Yu Han committed
    #else
    
        if( m_pcEncCfg->getUsePbIntraFast() && !cs.slice->isIntra() && !interHadActive( cuECtx ) && cuECtx.bestCU && CU::isInter( *cuECtx.bestCU ) )
    
    Yu Han's avatar
    Yu Han committed
    #endif
    
    Yu Han's avatar
    Yu Han committed
    #if JVET_M0483_IBC
        if (cs.sps->getIBCFlag() && !cuECtx.bestTU)
    #else
    
    Yu Han's avatar
    Yu Han committed
        if (cs.sps->getSpsNext().getIBCMode() && !cuECtx.bestTU)
    
    Yu Han's avatar
    Yu Han committed
    #endif
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
          return true;
    
        CHECK( !slice.isIntra() && !cuECtx.bestTU, "No possible non-intra encoding for a P- or B-slice found" );
    
    
        if( !( slice.isIRAP() || bestMode.type == ETM_INTRA || 
    
    Yu Han's avatar
    Yu Han committed
    #if JVET_M0483_IBC
    
    Yu Han's avatar
    Yu Han committed
          ((!m_pcEncCfg->getDisableIntraPUsInInterSlices()) && (!relatedCU.isInter || !relatedCU.isIBC) && (
    #else    
          ( ( !m_pcEncCfg->getDisableIntraPUsInInterSlices() ) && !relatedCU.isInter && (
    #endif
    
                                             ( cuECtx.bestTU->cbf[0] != 0 ) ||
               ( ( numComp > COMPONENT_Cb ) && cuECtx.bestTU->cbf[1] != 0 ) ||
               ( ( numComp > COMPONENT_Cr ) && cuECtx.bestTU->cbf[2] != 0 )  // avoid very complex intra if it is unlikely
             ) ) ) )
        {
          return false;
        }
    
    Yu Han's avatar
    Yu Han committed
        if ((m_pcEncCfg->getIBCFastMethod() & IBC_FAST_METHOD_NOINTRA_IBCCBF0)
          && (bestMode.type == ETM_IBC || bestMode.type == ETM_IBC_MERGE)
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
          && (!cuECtx.bestCU->Y().valid() || cuECtx.bestTU->cbf[0] == 0)
          && (!cuECtx.bestCU->Cb().valid() || cuECtx.bestTU->cbf[1] == 0)
          && (!cuECtx.bestCU->Cr().valid() || cuECtx.bestTU->cbf[2] == 0))
        {
          return false;
        }
    
        if( lastTestMode().type != ETM_INTRA && cuECtx.bestCS && cuECtx.bestCU && interHadActive( cuECtx ) )
        {
          // Get SATD threshold from best Inter-CU
    
          if( !cs.slice->isIRAP() && m_pcEncCfg->getUsePbIntraFast() )
    
    Yu Han's avatar
    Yu Han committed
    #if JVET_M0483_IBC
    
    Yu Han's avatar
    Yu Han committed
            if (bestCU && !CU::isIntra(*bestCU))
    #else
    
    Yu Han's avatar
    Yu Han committed
    #endif
    
            {
              DistParam distParam;
              const bool useHad = !bestCU->transQuantBypass;
              m_pcRdCost->setDistParam( distParam, cs.getOrgBuf( COMPONENT_Y ), cuECtx.bestCS->getPredBuf( COMPONENT_Y ), cs.sps->getBitDepth( CHANNEL_TYPE_LUMA ), COMPONENT_Y, useHad );
              cuECtx.interHad = distParam.distFunc( distParam );
            }
          }
        }
    
        return true;
      }
      else if( encTestmode.type == ETM_IPCM )
      {
        if( getFastDeltaQp() )
        {
          const SPS &sps = *cs.sps;
          const uint32_t fastDeltaQPCuMaxPCMSize = Clip3( ( uint32_t ) 1 << sps.getPCMLog2MinSize(), ( uint32_t ) 1 << sps.getPCMLog2MaxSize(), 32u );
    
          if( cs.area.lumaSize().width > fastDeltaQPCuMaxPCMSize )
          {
            return false;   // only check necessary PCM in fast deltaqp mode
          }
        }
    
        // PCM MODES
    
        return sps.getPCMEnabledFlag() && width <= ( 1 << sps.getPCMLog2MaxSize() ) && width >= ( 1 << sps.getPCMLog2MinSize() );
    
    Yu Han's avatar
    Yu Han committed
      else if (encTestmode.type == ETM_IBC || encTestmode.type == ETM_IBC_MERGE)
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      {
    
    Yu Han's avatar
    Yu Han committed
        // IBC MODES
    
    Yu Han's avatar
    Yu Han committed
    #if JVET_M0483_IBC
        return sps.getIBCFlag() && width <= IBC_MAX_CAND_SIZE && partitioner.currArea().lumaSize().height <= IBC_MAX_CAND_SIZE;
    #else
    
    Yu Han's avatar
    Yu Han committed
        return sps.getSpsNext().getIBCMode() && width <= IBC_MAX_CAND_SIZE && partitioner.currArea().lumaSize().height <= IBC_MAX_CAND_SIZE;
    
    Yu Han's avatar
    Yu Han committed
    #endif
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      }
    
      else if( isModeInter( encTestmode ) )
      {
        // INTER MODES (ME + MERGE/SKIP)
        CHECK( slice.isIntra(), "Inter-mode should not be in the I-Slice mode list!" );
    
        if( getFastDeltaQp() )
        {
          if( encTestmode.type == ETM_MERGE_SKIP )
          {
            return false;
          }
    
    Karsten Suehring's avatar
    Karsten Suehring committed
          if( cs.area.lumaSize().width > cs.pcv->fastDeltaQPCuMaxSize )
    
          {
            return false; // only check necessary 2Nx2N Inter in fast deltaqp mode
          }
        }
    
        // --- Check if we can quit current mode using SAVE/LOAD coding history
    
        if( encTestmode.type == ETM_INTER_ME )
        {
          if( encTestmode.opts == ETO_STANDARD )
          {
            // NOTE: ETO_STANDARD is always done when early SKIP mode detection is enabled
            if( !m_pcEncCfg->getUseEarlySkipDetection() )
            {
              if( relatedCU.isSkip || relatedCU.isIntra )
              {
                return false;
              }
            }
          }
          else if ((encTestmode.opts & ETO_IMV) != 0)
          {
            int imvOpt = (encTestmode.opts & ETO_IMV) >> ETO_IMV_SHIFT;
    
            if (imvOpt == 3 && cuECtx.get<double>(BEST_NO_IMV_COST) * 1.06 < cuECtx.get<double>(BEST_IMV_COST))
            {
    
    #if JVET_M0246_AFFINE_AMVR
              if ( !m_pcEncCfg->getUseAffineAmvr() )
    #endif
    
              return false;
            }
          }
        }
    
        if ( encTestmode.type == ETM_AFFINE && relatedCU.isIntra )
        {
          return false;
        }
    
        if( encTestmode.type == ETM_MERGE_TRIANGLE && ( partitioner.currArea().lumaSize().area() < TRIANGLE_MIN_SIZE || relatedCU.isIntra ) )
        { 
          return false;
        }
    
        return true;
      }
      else if( isModeSplit( encTestmode ) )
      {
        //////////////////////////////////////////////////////////////////////////
        // skip-history rule - don't split further if at least for three past levels
        //                     in the split tree it was found that skip is the best mode
        //////////////////////////////////////////////////////////////////////////
        int skipScore = 0;
    
    
    Yu Han's avatar
    Yu Han committed
    #if JVET_M0483_IBC 
    
    Yu Han's avatar
    Yu Han committed
        if ((!slice.isIntra() || slice.getSPS()->getIBCFlag()) && cuECtx.get<bool>(IS_BEST_NOSPLIT_SKIP))
    
    Yu Han's avatar
    Yu Han committed
    #else
    
        if( !slice.isIntra() && cuECtx.get<bool>( IS_BEST_NOSPLIT_SKIP ) )
    
    Yu Han's avatar
    Yu Han committed
    #endif
    
        {
          for( int i = 2; i < m_ComprCUCtxList.size(); i++ )
          {
            if( ( m_ComprCUCtxList.end() - i )->get<bool>( IS_BEST_NOSPLIT_SKIP ) )
            {
              skipScore += 1;
            }
            else
            {
              break;
            }
          }
        }
    
        const PartSplit split = getPartSplit( encTestmode );
        if( !partitioner.canSplit( split, cs ) || skipScore >= 2 )
        {
          if( split == CU_HORZ_SPLIT ) cuECtx.set( DID_HORZ_SPLIT, false );
          if( split == CU_VERT_SPLIT ) cuECtx.set( DID_VERT_SPLIT, false );
          if( split == CU_QUAD_SPLIT ) cuECtx.set( DID_QUAD_SPLIT, false );
    
          return false;
        }
    
        if( m_pcEncCfg->getUseContentBasedFastQtbt() )
        {
          const CompArea& currArea = partitioner.currArea().Y();
          int cuHeight  = currArea.height;
          int cuWidth   = currArea.width;
    
          const bool condIntraInter = m_pcEncCfg->getIntraPeriod() == 1 ? ( partitioner.currBtDepth == 0 ) : ( cuHeight > 32 && cuWidth > 32 );
    
          if( cuWidth == cuHeight && condIntraInter && getPartSplit( encTestmode ) != CU_QUAD_SPLIT )
          {
            const CPelBuf bufCurrArea = cs.getOrgBuf( partitioner.currArea().block( COMPONENT_Y ) );
    
            double horVal = 0;
            double verVal = 0;
            double dupVal = 0;
            double dowVal = 0;
    
            const double th = m_pcEncCfg->getIntraPeriod() == 1 ? 1.2 : 1.0;
    
            unsigned j, k;
    
            for( j = 0; j < cuWidth - 1; j++ )
            {
              for( k = 0; k < cuHeight - 1; k++ )
              {
                horVal += abs( bufCurrArea.at( j + 1, k     ) - bufCurrArea.at( j, k ) );
                verVal += abs( bufCurrArea.at( j    , k + 1 ) - bufCurrArea.at( j, k ) );
                dowVal += abs( bufCurrArea.at( j + 1, k )     - bufCurrArea.at( j, k + 1 ) );
                dupVal += abs( bufCurrArea.at( j + 1, k + 1 ) - bufCurrArea.at( j, k ) );
              }
            }
            if( horVal > th * verVal && sqrt( 2 ) * horVal > th * dowVal && sqrt( 2 ) * horVal > th * dupVal && ( getPartSplit( encTestmode ) == CU_HORZ_SPLIT || getPartSplit( encTestmode ) == CU_TRIH_SPLIT ) )
            {
              return false;
            }
            if( th * dupVal < sqrt( 2 ) * verVal && th * dowVal < sqrt( 2 ) * verVal && th * horVal < verVal && ( getPartSplit( encTestmode ) == CU_VERT_SPLIT || getPartSplit( encTestmode ) == CU_TRIV_SPLIT ) )
            {
              return false;
            }
          }
    
          if( m_pcEncCfg->getIntraPeriod() == 1 && cuWidth <= 32 && cuHeight <= 32 && bestCS && bestCS->tus.size() == 1 && bestCU && bestCU->depth == partitioner.currDepth && partitioner.currBtDepth > 1 && isLuma( partitioner.chType ) )
          {
            if( !bestCU->rootCbf )
            {
              return false;
            }
          }
        }
    
        if( bestCU && bestCU->skip && bestCU->mtDepth >= m_skipThreshold && !isModeSplit( cuECtx.lastTestMode ) )
        {
          return false;
        }
    
        int featureToSet = -1;
    
        switch( getPartSplit( encTestmode ) )
        {
          case CU_QUAD_SPLIT:
            {
    #if ENABLE_SPLIT_PARALLELISM
              if( !cuECtx.isLevelSplitParallel )
    #endif
              if( !cuECtx.get<bool>( QT_BEFORE_BT ) && bestCU )
              {
                unsigned maxBTD        = cs.pcv->getMaxBtDepth( slice, partitioner.chType );
                const CodingUnit *cuBR = bestCS->cus.back();
    
                unsigned height        = partitioner.currArea().lumaSize().height;
    
    Yu Han's avatar
    Yu Han committed
    #if JVET_M0483_IBC
    
    Yu Han's avatar
    Yu Han committed
                if (bestCU && ((bestCU->btDepth == 0 && maxBTD >= ((slice.isIntra() && !slice.getSPS()->getIBCFlag()) ? 3 : 2))
                  || (bestCU->btDepth == 1 && cuBR && cuBR->btDepth == 1 && maxBTD >= ((slice.isIntra() && !slice.getSPS()->getIBCFlag()) ? 4 : 3)))
    
    Yu Han's avatar
    Yu Han committed
                  && (width <= MAX_TU_SIZE_FOR_PROFILE && height <= MAX_TU_SIZE_FOR_PROFILE)
                  && cuECtx.get<bool>(DID_HORZ_SPLIT) && cuECtx.get<bool>(DID_VERT_SPLIT))
                {
                  return false;
                }
    #else
    
                if( bestCU && ( ( bestCU->btDepth == 0 &&                               maxBTD >= ( slice.isIntra() ? 3 : 2 ) )
                             || ( bestCU->btDepth == 1 && cuBR && cuBR->btDepth == 1 && maxBTD >= ( slice.isIntra() ? 4 : 3 ) ) )
    
                           && ( width <= MAX_TU_SIZE_FOR_PROFILE && height <= MAX_TU_SIZE_FOR_PROFILE )
    
                           && cuECtx.get<bool>( DID_HORZ_SPLIT ) && cuECtx.get<bool>( DID_VERT_SPLIT ) )
                {
                  return false;
                }
    
    Yu Han's avatar
    Yu Han committed
    #endif
    
              }
              if( m_pcEncCfg->getUseEarlyCU() && bestCS->cost != MAX_DOUBLE && bestCU && bestCU->skip )
              {
                return false;
              }
              if( getFastDeltaQp() && width <= slice.getPPS()->pcv->fastDeltaQPCuMaxSize )
              {
                return false;
              }
            }
            break;
          case CU_HORZ_SPLIT:
            featureToSet = DID_HORZ_SPLIT;
            break;
          case CU_VERT_SPLIT:
            featureToSet = DID_VERT_SPLIT;
            break;
          case CU_TRIH_SPLIT:
            if( cuECtx.get<bool>( DID_HORZ_SPLIT ) && bestCU && bestCU->btDepth == partitioner.currBtDepth && !bestCU->rootCbf )
            {
              return false;
            }
    
            if( !cuECtx.get<bool>( DO_TRIH_SPLIT ) )
            {
              return false;
            }
            break;
          case CU_TRIV_SPLIT:
            if( cuECtx.get<bool>( DID_VERT_SPLIT ) && bestCU && bestCU->btDepth == partitioner.currBtDepth && !bestCU->rootCbf )
            {
              return false;
            }
    
            if( !cuECtx.get<bool>( DO_TRIV_SPLIT ) )
            {
              return false;
            }
            break;
          default:
            THROW( "Only CU split modes are governed by the EncModeCtrl" );
            return false;
            break;
        }
    
        switch( split )
        {
          case CU_HORZ_SPLIT:
          case CU_TRIH_SPLIT:
            if( cuECtx.get<bool>( QT_BEFORE_BT ) && cuECtx.get<bool>( DID_QUAD_SPLIT ) )
            {
              if( cuECtx.get<int>( MAX_QT_SUB_DEPTH ) > partitioner.currQtDepth + 1 )
              {
                if( featureToSet >= 0 ) cuECtx.set( featureToSet, false );
                return false;
              }
            }
            break;
          case CU_VERT_SPLIT:
          case CU_TRIV_SPLIT:
            if( cuECtx.get<bool>( QT_BEFORE_BT ) && cuECtx.get<bool>( DID_QUAD_SPLIT ) )
            {
              if( cuECtx.get<int>( MAX_QT_SUB_DEPTH ) > partitioner.currQtDepth + 1 )
              {
                if( featureToSet >= 0 ) cuECtx.set( featureToSet, false );
                return false;
              }
            }
            break;
          default:
            break;
        }
    
        if( split == CU_QUAD_SPLIT ) cuECtx.set( DID_QUAD_SPLIT, true );
        return true;
      }
      else
      {
        CHECK( encTestmode.type != ETM_POST_DONT_SPLIT, "Unknown mode" );
    
        if( !bestCS || ( bestCS && isModeSplit( bestMode ) ) )
        {
          return false;
        }
        else
        {
    #if REUSE_CU_RESULTS
          setFromCs( *bestCS, partitioner );
    
    #endif
          // assume the non-split modes are done and set the marks for the best found mode
          if( bestCS && bestCU )
          {
            if( CU::isInter( *bestCU ) )
            {
              relatedCU.isInter   = true;
    #if HM_CODED_CU_INFO
              relatedCU.isSkip   |= bestCU->skip;
    
              relatedCU.isMMVDSkip |= bestCU->mmvdSkip;
    
    #endif
              relatedCU.GBiIdx    = bestCU->GBiIdx;
    
    Yu Han's avatar
    Yu Han committed
    #if JVET_M0483_IBC
    
    Yu Han's avatar
    Yu Han committed
            else if (CU::isIBC(*bestCU))
            {
              relatedCU.isIBC = true;
    #if HM_CODED_CU_INFO
              relatedCU.isSkip |= bestCU->skip;
    #endif
            }
    #endif
    
            else if( CU::isIntra( *bestCU ) )
            {
              relatedCU.isIntra   = true;
            }
    #if ENABLE_SPLIT_PARALLELISM
            touch( partitioner.currArea() );
    #endif
            cuECtx.set( IS_BEST_NOSPLIT_SKIP, bestCU->skip );
          }
        }
    
        return false;
      }
    }
    
    bool EncModeCtrlMTnoRQT::useModeResult( const EncTestMode& encTestmode, CodingStructure*& tempCS, Partitioner& partitioner )
    {
      xExtractFeatures( encTestmode, *tempCS );
    
      ComprCUCtx& cuECtx = m_ComprCUCtxList.back();
    
    
      if(      encTestmode.type == ETM_SPLIT_BT_H )
      {
        cuECtx.set( BEST_HORZ_SPLIT_COST, tempCS->cost );
      }
      else if( encTestmode.type == ETM_SPLIT_BT_V )
      {
        cuECtx.set( BEST_VERT_SPLIT_COST, tempCS->cost );
      }
      else if( encTestmode.type == ETM_SPLIT_TT_H )
      {
        cuECtx.set( BEST_TRIH_SPLIT_COST, tempCS->cost );
      }
      else if( encTestmode.type == ETM_SPLIT_TT_V )
      {
        cuECtx.set( BEST_TRIV_SPLIT_COST, tempCS->cost );
      }
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #if !JVET_M0464_UNI_MTS
    
    Karsten Suehring's avatar
    Karsten Suehring committed
      else if( encTestmode.type == ETM_INTRA )
    
      {
        const CodingUnit cu = *tempCS->getCU( partitioner.chType );
    
        if( !cu.emtFlag )
        {
          cuECtx.bestEmtSize2Nx2N1stPass = tempCS->cost;
        }
    
    #if JVET_M0102_INTRA_SUBPARTITIONS
        if (!cu.ispMode)
        {
          cuECtx.bestCostEmtFirstPassNoIsp = tempCS->cost;
        }
    #endif
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #endif
    
    
      if( m_pcEncCfg->getIMV4PelFast() && m_pcEncCfg->getIMV() && encTestmode.type == ETM_INTER_ME )
      {
        int imvMode = ( encTestmode.opts & ETO_IMV ) >> ETO_IMV_SHIFT;
    
        if( imvMode == 1 )
        {
          if( tempCS->cost < cuECtx.get<double>( BEST_IMV_COST ) )
          {
            cuECtx.set( BEST_IMV_COST, tempCS->cost );
          }
        }
        else if( imvMode == 0 )
        {
          if( tempCS->cost < cuECtx.get<double>( BEST_NO_IMV_COST ) )
          {
            cuECtx.set( BEST_NO_IMV_COST, tempCS->cost );
          }
        }
      }
    
      if( encTestmode.type == ETM_SPLIT_QT )
      {
        int maxQtD = 0;
        for( const auto& cu : tempCS->cus )
        {
          maxQtD = std::max<int>( maxQtD, cu->qtDepth );
        }
        cuECtx.set( MAX_QT_SUB_DEPTH, maxQtD );
      }
    
      if( ( tempCS->sps->getSpsNext().getMTTMode() & 1 ) == 1 )
      {
        int maxMtD = tempCS->pcv->getMaxBtDepth( *tempCS->slice, partitioner.chType ) + partitioner.currImplicitBtDepth;
    
        if( encTestmode.type == ETM_SPLIT_BT_H )
        {
          if( tempCS->cus.size() > 2 )
          {
            int h_2   = tempCS->area.blocks[partitioner.chType].height / 2;
            int cu1_h = tempCS->cus.front()->blocks[partitioner.chType].height;
            int cu2_h = tempCS->cus.back() ->blocks[partitioner.chType].height;
    
            cuECtx.set( DO_TRIH_SPLIT, cu1_h < h_2 || cu2_h < h_2 || partitioner.currMtDepth + 1 == maxMtD );
          }
        }
        else if( encTestmode.type == ETM_SPLIT_BT_V )
        {
          if( tempCS->cus.size() > 2 )
          {
            int w_2   = tempCS->area.blocks[partitioner.chType].width / 2;
            int cu1_w = tempCS->cus.front()->blocks[partitioner.chType].width;
            int cu2_w = tempCS->cus.back() ->blocks[partitioner.chType].width;
    
            cuECtx.set( DO_TRIV_SPLIT, cu1_w < w_2 || cu2_w < w_2 || partitioner.currMtDepth + 1 == maxMtD );
          }
        }
      }
    
      // for now just a simple decision based on RD-cost or choose tempCS if bestCS is not yet coded
      if( !cuECtx.bestCS || tempCS->features[ENC_FT_RD_COST] < cuECtx.bestCS->features[ENC_FT_RD_COST] )
      {
        cuECtx.bestCS = tempCS;
        cuECtx.bestCU = tempCS->cus[0];
        cuECtx.bestTU = cuECtx.bestCU->firstTU;
    
        if( isModeInter( encTestmode ) )
        {
          //Here we take the best cost of both inter modes. We are assuming only the inter modes (and all of them) have come before the intra modes!!!
          cuECtx.bestInterCost = cuECtx.bestCS->cost;
        }
    
        return true;
      }