Skip to content
Snippets Groups Projects
EncCu.cpp 147 KiB
Newer Older
  • Learn to ignore specific revisions
  •       //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( 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;
        }
    
    
      if( histBestSbt == MAX_UCHAR && doPreAnalyzeResi && numRDOTried > 1 )
      {
        slsSbt->saveBestSbt( cu->cs->area, (uint32_t)( curPuSse >> slShift ), currBestSbt, currBestTrs );
      }
      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;
          }
        }
      }
    
    }
    
    
    void EncCu::xEncodeDontSplit( CodingStructure &cs, Partitioner &partitioner )
    {
      m_CABACEstimator->resetBits();
    
    
    Adam Wieckowski's avatar
    Adam Wieckowski committed
      m_CABACEstimator->split_cu_mode( CU_DONT_SPLIT, cs, partitioner );
    
    
      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 )
    {
      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();
    
        cu.shareParentPos = tempCS->sharedBndPos;
        cu.shareParentSize = tempCS->sharedBndSize;
    
        partitioner.setCUData( cu );
    
        if( CU::isIntra( cu ) )
        {
          xReconIntraQT( cu );
        }
        else
        {
          xDeriveCUMV( cu );
          xReconInter( cu );
        }
    
    
    Nan Hu's avatar
    Nan Hu committed
        Distortion finalDistortion = 0;
    
    Nan Hu's avatar
    Nan Hu committed
        tempCS->useDbCost = m_pcEncCfg->getUseEncDbOpt();
    
    Nan Hu's avatar
    Nan Hu committed
        if ( m_pcEncCfg->getUseEncDbOpt() )
        {
          xCalDebCost( *tempCS, partitioner, true );
          finalDistortion = tempCS->dist;
        }
        else
        {
    
    Nan Hu's avatar
    Nan Hu committed
        const SPS &sps = *tempCS->sps;
    
        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 (m_pcEncCfg->getLumaLevelToDeltaQPMapping().isEnabled() || (
            m_pcEncCfg->getReshaper() && (tempCS->slice->getReshapeInfo().getUseSliceReshaper() && m_pcReshape->getCTUFlag())))
    
          {
            const CPelBuf orgLuma = tempCS->getOrgBuf(tempCS->area.blocks[COMPONENT_Y]);
    
    Taoran Lu's avatar
    Taoran Lu committed
            if (compID == COMPONENT_Y && !(m_pcEncCfg->getLumaLevelToDeltaQPMapping().isEnabled()))
    
    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
    
            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 );
        }
    
    Nan Hu's avatar
    Nan Hu committed
        }
    
    
        m_CABACEstimator->getCtx() = m_CurrCtx->start;
        m_CABACEstimator->resetBits();
    
        CUCtx cuCtx;
        cuCtx.isDQPCoded = true;
        cuCtx.isChromaQpAdjCoded = true;
        m_CABACEstimator->coding_unit( cu, partitioner, cuCtx );
    
    
    Nan Hu's avatar
    Nan Hu committed
    
    
        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