Skip to content
Snippets Groups Projects
EncCu.cpp 160 KiB
Newer Older
  • Learn to ignore specific revisions
  •   bool testGbi;
      uint8_t gbiIdx;
    
    #if JVET_M0246_AFFINE_AMVR
      bool affineAmvrEanbledFlag = cu.slice->getSPS()->getAffineAmvrEnabledFlag();
    #endif
    
      if( pcCUInfo2Reuse != nullptr )
      {
        // reuse the motion info from pcCUInfo2Reuse
        CU::resetMVDandMV2Int( cu, m_pcInterSearch );
    
    
        CHECK(cu.GBiIdx < 0 || cu.GBiIdx >= GBI_NUM, "cu.GBiIdx < 0 || cu.GBiIdx >= GBI_NUM");
        gbiIdx = CU::getValidGbiIdx(cu);
        testGbi = (gbiIdx != GBI_DEFAULT);
    
    #if JVET_M0246_AFFINE_AMVR
        if ( !CU::hasSubCUNonZeroMVd( cu ) && !CU::hasSubCUNonZeroAffineMVd( cu ) )
    #else
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
          if (m_modeCtrl->useModeResult(encTestModeBase, tempCS, partitioner))
          {
            std::swap(tempCS, bestCS);
            // store temp best CI for next CU coding
            m_CurrCtx->best = m_CABACEstimator->getCtx();
          }
    
    #if JVET_M0246_AFFINE_AMVR
          if ( affineAmvrEanbledFlag )
          {
            tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
            continue;
          }
          else
          {
            return false;
          }
    #else
    
        cu.GBiIdx = g_GbiSearchOrder[gbiLoopIdx];
        gbiIdx = cu.GBiIdx;
        testGbi = (gbiIdx != GBI_DEFAULT);
    
    #if JVET_M0246_AFFINE_AMVR
        cu.firstPU->interDir = 10;
    #endif
    
    
        m_pcInterSearch->predInterSearch( cu, partitioner );
    
    #if JVET_M0246_AFFINE_AMVR
        if ( cu.firstPU->interDir <= 3 )
        {
          gbiIdx = CU::getValidGbiIdx(cu);
        }
        else
        {
          return false;
        }
    #else
    
        gbiIdx = CU::getValidGbiIdx(cu);
    
      if( testGbi && gbiIdx == GBI_DEFAULT ) // Enabled GBi but the search results is uni.
      {
        tempCS->initStructData(encTestMode.qp, encTestMode.lossless);
        continue;
      }
      CHECK(!(testGbi || (!testGbi && gbiIdx == GBI_DEFAULT)), " !( bTestGbi || (!bTestGbi && gbiIdx == GBI_DEFAULT ) )");
    
      bool isEqualUni = false;
      if( m_pcEncCfg->getUseGBiFast() )
      {
        if( cu.firstPU->interDir != 3 && testGbi == 0 )
        {
          isEqualUni = true;
        }
      }
    
    #if JVET_M0246_AFFINE_AMVR
      if ( !CU::hasSubCUNonZeroMVd( cu ) && !CU::hasSubCUNonZeroAffineMVd( cu ) )
    #else
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
        if (m_modeCtrl->useModeResult(encTestModeBase, tempCS, partitioner))
        {
          std::swap(tempCS, bestCS);
          // store temp best CI for next CU coding
          m_CurrCtx->best = m_CABACEstimator->getCtx();
        }
    
    #if JVET_M0246_AFFINE_AMVR
        if ( affineAmvrEanbledFlag )
        {
          tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
          continue;
        }
        else
        {
          return false;
        }
    #else
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #if JVET_M0464_UNI_MTS
      xEncodeInterResidual( tempCS, bestCS, partitioner, encTestModeBase, 0
                            , NULL
                            , 0
                            , &equGBiCost
    #else
    
      xEncodeInterResidual( tempCS, bestCS, partitioner, encTestModeBase, 0
        , NULL
        , true
        , 0
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #endif
    
      tempCS->initStructData(encTestMode.qp, encTestMode.lossless);
    
      double skipTH = MAX_DOUBLE;
      skipTH = (m_pcEncCfg->getUseGBiFast() ? 1.05 : MAX_DOUBLE);
      if( equGBiCost > curBestCost * skipTH )
      {
        break;
      }
    
      if( m_pcEncCfg->getUseGBiFast() )
      {
        if( isEqualUni == true && m_pcEncCfg->getIntraPeriod() == -1 )
        {
          break;
        }
      }
      if( g_GbiSearchOrder[gbiLoopIdx] == GBI_DEFAULT && xIsGBiSkip(cu) && m_pcEncCfg->getUseGBiFast() )
      {
        break;
      }
    
    #if JVET_M0246_AFFINE_AMVR
      validMode = true;
    #endif
    
     } // for( UChar gbiLoopIdx = 0; gbiLoopIdx < gbiLoopNum; gbiLoopIdx++ )
    
    
    #if JVET_M0246_AFFINE_AMVR
      return tempCS->slice->getSPS()->getAffineAmvrEnabledFlag() ? validMode : true;
    #else
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #if JVET_M0464_UNI_MTS
    void EncCu::xEncodeInterResidual(   CodingStructure *&tempCS
                                      , CodingStructure *&bestCS
                                      , Partitioner &partitioner
                                      , const EncTestMode& encTestMode
                                      , int residualPass
                                      , CodingStructure* imvCS
                                      , bool* bestHasNonResi
                                      , double* equGBiCost
    #else
    
    void EncCu::xEncodeInterResidual( CodingStructure *&tempCS, CodingStructure *&bestCS, Partitioner &partitioner, const EncTestMode& encTestMode, int residualPass
      , CodingStructure* imvCS
      , int emtMode
      , bool* bestHasNonResi
    
      , double* equGBiCost
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #endif
    
    {
      if( residualPass == 1 && encTestMode.lossless )
      {
        return;
      }
    
      CodingUnit*            cu        = tempCS->getCU( partitioner.chType );
      double   bestCostInternal        = MAX_DOUBLE;
    
    #if JVET_M0140_SBT
      double           bestCostBegin   = bestCS->cost;
      CodingUnit*      prevBestCU      = bestCS->getCU( partitioner.chType );
      uint8_t          prevBestSbt     = ( prevBestCU == nullptr ) ? 0 : prevBestCU->sbtInfo;
    #endif
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #if !JVET_M0464_UNI_MTS
    
    Karsten Suehring's avatar
    Karsten Suehring committed
      const int      maxSizeEMT        = EMT_INTER_MAX_CU_WITH_QTBT;
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #endif
    
      bool              swapped        = false; // avoid unwanted data copy
      bool             reloadCU        = false;
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #if !JVET_M0464_UNI_MTS
    
      const bool considerEmtSecondPass = emtMode && sps.getSpsNext().getUseInterEMT() && partitioner.currArea().lwidth() <= maxSizeEMT && partitioner.currArea().lheight() <= maxSizeEMT;
    
      int minEMTMode = 0;
      int maxEMTMode = (considerEmtSecondPass?1:0);
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #endif
    
    Xiang Li's avatar
    Xiang Li committed
      // Not allow very big |MVd| to avoid CABAC crash caused by too large MVd. Normally no impact on coding performance.
      const int maxMvd = 1 << 15;
      const PredictionUnit& pu = *cu->firstPU;
      if (!cu->affine)
      {
        if ((pu.refIdx[0] >= 0 && (pu.mvd[0].getAbsHor() >= maxMvd || pu.mvd[0].getAbsVer() >= maxMvd))
          || (pu.refIdx[1] >= 0 && (pu.mvd[1].getAbsHor() >= maxMvd || pu.mvd[1].getAbsVer() >= maxMvd)))
        {
          return;
        }
      }
    
      else
      {
        for (int refList = 0; refList < NUM_REF_PIC_LIST_01; refList++)
        {
          if (pu.refIdx[refList] >= 0)
          {
            for (int ctrlP = 1 + (cu->affineType == AFFINEMODEL_6PARAM); ctrlP >= 0; ctrlP--)
            {
              if (pu.mvdAffi[refList][ctrlP].getAbsHor() >= maxMvd || pu.mvdAffi[refList][ctrlP].getAbsVer() >= maxMvd)
              {
                return;
              }
            }
          }
        }
      }
    
    #if JVET_M0140_SBT
      const bool mtsAllowed = tempCS->sps->getSpsNext().getUseInterMTS() && partitioner.currArea().lwidth() <= MTS_INTER_MAX_CU_SIZE && partitioner.currArea().lheight() <= MTS_INTER_MAX_CU_SIZE;
      uint8_t sbtAllowed = cu->checkAllowedSbt();
      uint8_t numRDOTried = 0;
      Distortion sbtOffDist = 0;
      bool    sbtOffRootCbf = 0;
      double  sbtOffCost      = MAX_DOUBLE;
      double  currBestCost = MAX_DOUBLE;
      bool    doPreAnalyzeResi = ( sbtAllowed || mtsAllowed ) && residualPass == 0;
    
      m_pcInterSearch->initTuAnalyzer();
      if( doPreAnalyzeResi )
      {
        m_pcInterSearch->calcMinDistSbt( *tempCS, *cu, sbtAllowed );
      }
    
      auto    slsSbt = dynamic_cast<SaveLoadEncInfoSbt*>( m_modeCtrl );
      int     slShift = 4 + std::min( (int)gp_sizeIdxInfo->idxFrom( cu->lwidth() ) + (int)gp_sizeIdxInfo->idxFrom( cu->lheight() ), 9 );
      Distortion curPuSse = m_pcInterSearch->getEstDistSbt( NUMBER_SBT_MODE );
      uint8_t currBestSbt = 0;
      uint8_t currBestTrs = MAX_UCHAR;
      uint8_t histBestSbt = MAX_UCHAR;
      uint8_t histBestTrs = MAX_UCHAR;
      m_pcInterSearch->setHistBestTrs( MAX_UCHAR, MAX_UCHAR );
      if( doPreAnalyzeResi )
      {
        if( m_pcInterSearch->getSkipSbtAll() && !mtsAllowed ) //emt is off
        {
          histBestSbt = 0; //try DCT2
          m_pcInterSearch->setHistBestTrs( histBestSbt, histBestTrs );
        }
        else
        {
          assert( curPuSse != std::numeric_limits<uint64_t>::max() );
          uint16_t compositeSbtTrs = slsSbt->findBestSbt( cu->cs->area, (uint32_t)( curPuSse >> slShift ) );
          histBestSbt = ( compositeSbtTrs >> 0 ) & 0xff;
          histBestTrs = ( compositeSbtTrs >> 8 ) & 0xff;
          if( m_pcInterSearch->getSkipSbtAll() && CU::isSbtMode( histBestSbt ) ) //special case, skip SBT when loading SBT
          {
            histBestSbt = 0; //try DCT2
          }
          m_pcInterSearch->setHistBestTrs( histBestSbt, histBestTrs );
        }
      }
    #endif
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #if !JVET_M0464_UNI_MTS
    
      if( emtMode == 2 )
      {
        minEMTMode = maxEMTMode = (cu->emtFlag?1:0);
      }
    
      for( int curEmtMode = minEMTMode; curEmtMode <= maxEMTMode; curEmtMode++ )
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #endif
    
        if( reloadCU )
        {
          if( bestCost == bestCS->cost ) //The first EMT pass didn't become the bestCS, so we clear the TUs generated
          {
            tempCS->clearTUs();
          }
          else if( false == swapped )
          {
            tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
            tempCS->copyStructure( *bestCS, partitioner.chType );
            tempCS->getPredBuf().copyFrom( bestCS->getPredBuf() );
            bestCost = bestCS->cost;
            cu       = tempCS->getCU( partitioner.chType );
            swapped = true;
          }
          else
          {
            tempCS->clearTUs();
            bestCost = bestCS->cost;
            cu       = tempCS->getCU( partitioner.chType );
          }
    
          //we need to restart the distortion for the new tempCS, the bit count and the cost
          tempCS->dist     = 0;
          tempCS->fracBits = 0;
          tempCS->cost     = MAX_DOUBLE;
        }
    
        reloadCU    = true; // enable cu reloading
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #if !JVET_M0464_UNI_MTS
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #endif
    
    #if JVET_M0140_SBT
        cu->sbtInfo = 0;
    #endif
    
    #if JVET_M0140_SBT // skip DCT-2 and EMT if historical best transform mode is SBT
        if( skipResidual || histBestSbt == MAX_UCHAR || !CU::isSbtMode( histBestSbt ) )
        {
    #endif
    
        m_pcInterSearch->encodeResAndCalcRdInterCU( *tempCS, partitioner, skipResidual );
    
    #if JVET_M0140_SBT
        numRDOTried += mtsAllowed ? 2 : 1;
    #endif
    
        xEncodeDontSplit( *tempCS, partitioner );
    
        xCheckDQP( *tempCS, partitioner );
    
    
    #if !JVET_M0140_SBT //harmonize with GBI fast algorithm (move the code to the end of this function)
    
        if( ETM_INTER_ME == encTestMode.type )
        {
          if( equGBiCost != NULL )
          {
            if( tempCS->cost < (*equGBiCost) && cu->GBiIdx == GBI_DEFAULT )
            {
              (*equGBiCost) = tempCS->cost;
            }
          }
          else
          {
            CHECK(equGBiCost == NULL, "equGBiCost == NULL");
          }
          if( tempCS->slice->getCheckLDC() && !cu->imv && cu->GBiIdx != GBI_DEFAULT && tempCS->cost < m_bestGbiCost[1] )
          {
            if( tempCS->cost < m_bestGbiCost[0] )
            {
              m_bestGbiCost[1] = m_bestGbiCost[0];
              m_bestGbiCost[0] = tempCS->cost;
              m_bestGbiIdx[1] = m_bestGbiIdx[0];
              m_bestGbiIdx[0] = cu->GBiIdx;
            }
            else
            {
              m_bestGbiCost[1] = tempCS->cost;
              m_bestGbiIdx[1] = cu->GBiIdx;
            }
          }
        }
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #if !JVET_M0464_UNI_MTS
    
        double emtFirstPassCost = tempCS->cost;
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #endif
    
        if( imvCS && (tempCS->cost < imvCS->cost) )
        {
          if( imvCS->cost != MAX_DOUBLE )
          {
            imvCS->initStructData( encTestMode.qp, encTestMode.lossless );
          }
          imvCS->copyStructure( *tempCS, partitioner.chType );
        }
        if( NULL != bestHasNonResi && (bestCostInternal > tempCS->cost) )
        {
          bestCostInternal = tempCS->cost;
    
          if (!(tempCS->getPU(partitioner.chType)->mhIntraFlag))
    
          if (tempCS->getPU(partitioner.chType)->mhIntraFlag)
    
    #if JVET_M0140_SBT
        currBestCost = tempCS->cost;
        sbtOffCost = tempCS->cost;
        sbtOffDist = tempCS->dist;
        sbtOffRootCbf = cu->rootCbf;
        currBestSbt = CU::getSbtInfo( cu->firstTU->mtsIdx > 1 ? SBT_OFF_MTS : SBT_OFF_DCT, 0 );
        currBestTrs = cu->firstTU->mtsIdx;
        if( cu->lwidth() <= MAX_TU_SIZE_FOR_PROFILE && cu->lheight() <= MAX_TU_SIZE_FOR_PROFILE )
        {
          CHECK( tempCS->tus.size() != 1, "tu must be only one" );
        }
    #endif
    
    #if WCG_EXT
        DTRACE_MODE_COST( *tempCS, m_pcRdCost->getLambda( true ) );
    #else
        DTRACE_MODE_COST( *tempCS, m_pcRdCost->getLambda() );
    #endif
        xCheckBestMode( tempCS, bestCS, partitioner, encTestMode );
    
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #if !JVET_M0464_UNI_MTS
    
        //now we check whether the second pass should be skipped or not
        if( !curEmtMode && maxEMTMode )
        {
          const double thresholdToSkipEmtSecondPass = 1.1; // Skip checking EMT transforms
          const bool bCond1 = !cu->firstTU->cbf[COMPONENT_Y];
    
          const bool bCond3 = emtFirstPassCost > ( bestCost * thresholdToSkipEmtSecondPass );
    
    
          if( m_pcEncCfg->getFastInterEMT() && (bCond1 || bCond3 ) )
    
    #endif
    #if JVET_M0140_SBT // skip DCT-2 and EMT
        }
    #endif
    
    #if JVET_M0140_SBT //RDO for SBT
        uint8_t numSbtRdo = CU::numSbtModeRdo( sbtAllowed );
        //early termination if all SBT modes are not allowed
        //normative
        if( !sbtAllowed || skipResidual )
        {
          numSbtRdo = 0;
        }
        //fast algorithm
        if( ( histBestSbt != MAX_UCHAR && !CU::isSbtMode( histBestSbt ) ) || m_pcInterSearch->getSkipSbtAll() )
        {
          numSbtRdo = 0;
        }
        if( bestCost != MAX_DOUBLE && sbtOffCost != MAX_DOUBLE )
        {
          double th = 1.07;
          if( !( prevBestSbt == 0 || m_sbtCostSave[0] == MAX_DOUBLE ) )
          {
            assert( m_sbtCostSave[1] <= m_sbtCostSave[0] );
            th *= ( m_sbtCostSave[0] / m_sbtCostSave[1] );
          }
          if( sbtOffCost > bestCost * th )
          {
            numSbtRdo = 0;
          }
        }
        if( !sbtOffRootCbf && sbtOffCost != MAX_DOUBLE )
        {
          double th = Clip3( 0.05, 0.55, ( 27 - cu->qp ) * 0.02 + 0.35 );
          if( sbtOffCost < m_pcRdCost->calcRdCost( ( cu->lwidth() * cu->lheight() ) << SCALE_BITS, 0 ) * th )
          {
            numSbtRdo = 0;
          }
        }
    
        if( histBestSbt != MAX_UCHAR && numSbtRdo != 0 )
        {
          numSbtRdo = 1;
          m_pcInterSearch->initSbtRdoOrder( CU::getSbtMode( CU::getSbtIdx( histBestSbt ), CU::getSbtPos( histBestSbt ) ) );
        }
    
        for( int sbtModeIdx = 0; sbtModeIdx < numSbtRdo; sbtModeIdx++ )
        {
          uint8_t sbtMode = m_pcInterSearch->getSbtRdoOrder( sbtModeIdx );
          uint8_t sbtIdx = CU::getSbtIdxFromSbtMode( sbtMode );
          uint8_t sbtPos = CU::getSbtPosFromSbtMode( sbtMode );
    
          //fast algorithm (early skip, save & load)
          if( histBestSbt == MAX_UCHAR )
          {
            uint8_t skipCode = m_pcInterSearch->skipSbtByRDCost( cu->lwidth(), cu->lheight(), cu->mtDepth, sbtIdx, sbtPos, bestCS->cost, sbtOffDist, sbtOffCost, sbtOffRootCbf );
            if( skipCode != MAX_UCHAR )
            {
              continue;
            }
    
            if( sbtModeIdx > 0 )
            {
              uint8_t prevSbtMode = m_pcInterSearch->getSbtRdoOrder( sbtModeIdx - 1 );
              //make sure the prevSbtMode is the same size as the current SBT mode (otherwise the estimated dist may not be comparable)
              if( CU::isSameSbtSize( prevSbtMode, sbtMode ) )
              {
                Distortion currEstDist = m_pcInterSearch->getEstDistSbt( sbtMode );
                Distortion prevEstDist = m_pcInterSearch->getEstDistSbt( prevSbtMode );
                if( currEstDist > prevEstDist * 1.15 )
                {
                  continue;
                }
              }
            }
          }
    
          //init tempCS and TU
          if( bestCost == bestCS->cost ) //The first EMT pass didn't become the bestCS, so we clear the TUs generated
          {
            tempCS->clearTUs();
          }
          else if( false == swapped )
          {
            tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
            tempCS->copyStructure( *bestCS, partitioner.chType );
            tempCS->getPredBuf().copyFrom( bestCS->getPredBuf() );
            bestCost = bestCS->cost;
            cu = tempCS->getCU( partitioner.chType );
            swapped = true;
          }
          else
          {
            tempCS->clearTUs();
            bestCost = bestCS->cost;
            cu = tempCS->getCU( partitioner.chType );
          }
    
          //we need to restart the distortion for the new tempCS, the bit count and the cost
          tempCS->dist = 0;
          tempCS->fracBits = 0;
          tempCS->cost = MAX_DOUBLE;
          cu->skip = false;
    
          //set SBT info
          cu->setSbtIdx( sbtIdx );
          cu->setSbtPos( sbtPos );
    
          //try residual coding
          m_pcInterSearch->encodeResAndCalcRdInterCU( *tempCS, partitioner, skipResidual );
          numRDOTried++;
    
          xEncodeDontSplit( *tempCS, partitioner );
    
          xCheckDQP( *tempCS, partitioner );
    
          if( imvCS && ( tempCS->cost < imvCS->cost ) )
          {
            if( imvCS->cost != MAX_DOUBLE )
            {
              imvCS->initStructData( encTestMode.qp, encTestMode.lossless );
            }
            imvCS->copyStructure( *tempCS, partitioner.chType );
          }
    
          if( NULL != bestHasNonResi && ( bestCostInternal > tempCS->cost ) )
          {
            bestCostInternal = tempCS->cost;
            if( !( tempCS->getPU( partitioner.chType )->mhIntraFlag ) )
              *bestHasNonResi = !cu->rootCbf;
          }
    
          if( tempCS->cost < currBestCost )
          {
            currBestSbt = cu->sbtInfo;
            currBestTrs = tempCS->tus[cu->sbtInfo ? cu->getSbtPos() : 0]->mtsIdx;
            assert( currBestTrs == 0 || currBestTrs == 1 );
            currBestCost = tempCS->cost;
          }
    
    #if WCG_EXT
          DTRACE_MODE_COST( *tempCS, m_pcRdCost->getLambda( true ) );
    #else
          DTRACE_MODE_COST( *tempCS, m_pcRdCost->getLambda() );
    #endif
          xCheckBestMode( tempCS, bestCS, partitioner, encTestMode );
        }
    
        if( bestCostBegin != bestCS->cost )
        {
          m_sbtCostSave[0] = sbtOffCost;
          m_sbtCostSave[1] = currBestCost;
        }
    
    Tung Nguyen's avatar
    Tung Nguyen committed
    #endif
    
    
    #if JVET_M0140_SBT
      if( histBestSbt == MAX_UCHAR && doPreAnalyzeResi && numRDOTried > 1 )
      {
        slsSbt->saveBestSbt( cu->cs->area, (uint32_t)( curPuSse >> slShift ), currBestSbt, currBestTrs );
      }
    #endif
    #if JVET_M0140_SBT //harmonize with GBI fast algorithm (move the code here)
      tempCS->cost = currBestCost;
      if( ETM_INTER_ME == encTestMode.type )
      {
        if( equGBiCost != NULL )
        {
          if( tempCS->cost < ( *equGBiCost ) && cu->GBiIdx == GBI_DEFAULT )
          {
            ( *equGBiCost ) = tempCS->cost;
          }
        }
        else
        {
          CHECK( equGBiCost == NULL, "equGBiCost == NULL" );
        }
        if( tempCS->slice->getCheckLDC() && !cu->imv && cu->GBiIdx != GBI_DEFAULT && tempCS->cost < m_bestGbiCost[1] )
        {
          if( tempCS->cost < m_bestGbiCost[0] )
          {
            m_bestGbiCost[1] = m_bestGbiCost[0];
            m_bestGbiCost[0] = tempCS->cost;
            m_bestGbiIdx[1] = m_bestGbiIdx[0];
            m_bestGbiIdx[0] = cu->GBiIdx;
          }
          else
          {
            m_bestGbiCost[1] = tempCS->cost;
            m_bestGbiIdx[1] = cu->GBiIdx;
          }
        }
      }
    #endif
    
    }
    
    
    void EncCu::xEncodeDontSplit( CodingStructure &cs, Partitioner &partitioner )
    {
      m_CABACEstimator->resetBits();
    
    
    Adam Wieckowski's avatar
    Adam Wieckowski committed
    #if JVET_M0421_SPLIT_SIG
      m_CABACEstimator->split_cu_mode( CU_DONT_SPLIT, cs, partitioner );
    #else
    
      {
        if( partitioner.canSplit( CU_QUAD_SPLIT, cs ) )
        {
          m_CABACEstimator->split_cu_flag( false, cs, partitioner );
        }
        if( partitioner.canSplit( CU_MT_SPLIT, cs ) )
        {
          m_CABACEstimator->split_cu_mode_mt( CU_DONT_SPLIT, cs, partitioner );
        }
      }
    
    Adam Wieckowski's avatar
    Adam Wieckowski committed
    #endif
    
    
      cs.fracBits += m_CABACEstimator->getEstFracBits(); // split bits
      cs.cost      = m_pcRdCost->calcRdCost( cs.fracBits, cs.dist );
    
    }
    
    #if REUSE_CU_RESULTS
    void EncCu::xReuseCachedResult( CodingStructure *&tempCS, CodingStructure *&bestCS, Partitioner &partitioner )
    {
      const SPS &sps = *tempCS->sps;
    
      BestEncInfoCache* bestEncCache = dynamic_cast<BestEncInfoCache*>( m_modeCtrl );
      CHECK( !bestEncCache, "If this mode is chosen, mode controller has to implement the mode caching capabilities" );
      EncTestMode cachedMode;
    
      if( bestEncCache->setCsFrom( *tempCS, cachedMode, partitioner ) )
      {
        CodingUnit& cu = *tempCS->cus.front();
    
    #if JVET_M0170_MRG_SHARELIST
        cu.shareParentPos = tempCS->sharedBndPos;
        cu.shareParentSize = tempCS->sharedBndSize;
    #endif
    
        partitioner.setCUData( cu );
    
        if( CU::isIntra( cu ) )
        {
          xReconIntraQT( cu );
        }
        else
        {
          xDeriveCUMV( cu );
          xReconInter( cu );
        }
    
        Distortion finalDistortion = 0;
        const int  numValidComponents = getNumberValidComponents( tempCS->area.chromaFormat );
    
        for( int comp = 0; comp < numValidComponents; comp++ )
        {
          const ComponentID compID = ComponentID( comp );
    
          if( CS::isDualITree( *tempCS ) && toChannelType( compID ) != partitioner.chType )
          {
            continue;
          }
    
          CPelBuf reco = tempCS->getRecoBuf( compID );
          CPelBuf org  = tempCS->getOrgBuf ( compID );
    
    #if WCG_EXT
    
    Taoran Lu's avatar
    Taoran Lu committed
    #if JVET_M0427_INLOOP_RESHAPER
          if (m_pcEncCfg->getLumaLevelToDeltaQPMapping().isEnabled() || (
            m_pcEncCfg->getReshaper() && (tempCS->slice->getReshapeInfo().getUseSliceReshaper() && m_pcReshape->getCTUFlag())))
    #else
    
          if( m_pcEncCfg->getLumaLevelToDeltaQPMapping().isEnabled() )
    
    Taoran Lu's avatar
    Taoran Lu committed
    #endif
    
          {
            const CPelBuf orgLuma = tempCS->getOrgBuf(tempCS->area.blocks[COMPONENT_Y]);
    
    Taoran Lu's avatar
    Taoran Lu committed
    #if JVET_M0427_INLOOP_RESHAPER
    
            if (compID == COMPONENT_Y && (tempCS->slice->getReshapeInfo().getUseSliceReshaper() && m_pcReshape->getCTUFlag()))
    
    Taoran Lu's avatar
    Taoran Lu committed
            {
              const CompArea &area = cu.blocks[COMPONENT_Y];
              CompArea    tmpArea(COMPONENT_Y, area.chromaFormat, Position(0, 0), area.size());
              PelBuf tmpRecLuma = m_tmpStorageLCU->getBuf(tmpArea);
              tmpRecLuma.copyFrom(reco);
              tmpRecLuma.rspSignal(m_pcReshape->getInvLUT());
              finalDistortion += m_pcRdCost->getDistPart(org, tmpRecLuma, sps.getBitDepth(toChannelType(compID)), compID, DF_SSE_WTD, &orgLuma);
            }
            else
    #endif
    
            finalDistortion += m_pcRdCost->getDistPart( org, reco, sps.getBitDepth( toChannelType( compID ) ), compID, DF_SSE_WTD, &orgLuma );
          }
          else
    #endif
          finalDistortion += m_pcRdCost->getDistPart( org, reco, sps.getBitDepth( toChannelType( compID ) ), compID, DF_SSE );
        }
    
        m_CABACEstimator->getCtx() = m_CurrCtx->start;
        m_CABACEstimator->resetBits();
    
        CUCtx cuCtx;
        cuCtx.isDQPCoded = true;
        cuCtx.isChromaQpAdjCoded = true;
        m_CABACEstimator->coding_unit( cu, partitioner, cuCtx );
    
        tempCS->dist     = finalDistortion;
        tempCS->fracBits = m_CABACEstimator->getEstFracBits();
        tempCS->cost     = m_pcRdCost->calcRdCost( tempCS->fracBits, tempCS->dist );
    
        xEncodeDontSplit( *tempCS,         partitioner );
        xCheckDQP       ( *tempCS,         partitioner );
        xCheckBestMode  (  tempCS, bestCS, partitioner, cachedMode );
      }
      else
      {
        THROW( "Should never happen!" );
      }
    }
    
    #endif