Skip to content
Snippets Groups Projects
EncModeCtrl.cpp 57.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • /* The copyright in this software is being made available under the BSD
     * License, included below. This software may be subject to other third party
     * and contributor rights, including patent rights, and no such rights are
     * granted under this license.
     *
    
     * Copyright (c) 2010-2019, ITU/ISO/IEC
    
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions are met:
     *
     *  * Redistributions of source code must retain the above copyright notice,
     *    this list of conditions and the following disclaimer.
     *  * Redistributions in binary form must reproduce the above copyright notice,
     *    this list of conditions and the following disclaimer in the documentation
     *    and/or other materials provided with the distribution.
     *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
     *    be used to endorse or promote products derived from this software without
     *    specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
     * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     * THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /** \file     EncModeCtrl.cpp
        \brief    Encoder controller for trying out specific modes
    */
    
    #include "EncModeCtrl.h"
    
    #include "AQp.h"
    #include "RateCtrl.h"
    
    #include "CommonLib/RdCost.h"
    #include "CommonLib/CodingStructure.h"
    #include "CommonLib/Picture.h"
    #include "CommonLib/UnitTools.h"
    
    #include "CommonLib/dtrace_next.h"
    
    #include <cmath>
    
    void EncModeCtrl::init( EncCfg *pCfg, RateCtrl *pRateCtrl, RdCost* pRdCost )
    {
      m_pcEncCfg      = pCfg;
      m_pcRateCtrl    = pRateCtrl;
      m_pcRdCost      = pRdCost;
      m_fastDeltaQP   = false;
    #if SHARP_LUMA_DELTA_QP
      m_lumaQPOffset  = 0;
    
      initLumaDeltaQpLUT();
    #endif
    }
    
    bool EncModeCtrl::tryModeMaster( const EncTestMode& encTestmode, const CodingStructure &cs, Partitioner& partitioner )
    {
    #if ENABLE_SPLIT_PARALLELISM
      if( m_ComprCUCtxList.back().isLevelSplitParallel )
      {
        if( !parallelJobSelector( encTestmode, cs, partitioner ) )
        {
          return false;
        }
      }
    #endif
      return tryMode( encTestmode, cs, partitioner );
    }
    
    void EncModeCtrl::setEarlySkipDetected()
    {
      m_ComprCUCtxList.back().earlySkip = true;
    }
    
    void EncModeCtrl::xExtractFeatures( const EncTestMode encTestmode, CodingStructure& cs )
    {
      CHECK( cs.features.size() < NUM_ENC_FEATURES, "Features vector is not initialized" );
    
      cs.features[ENC_FT_DISTORTION     ] = double( cs.dist              );
      cs.features[ENC_FT_FRAC_BITS      ] = double( cs.fracBits          );
      cs.features[ENC_FT_RD_COST        ] = double( cs.cost              );
      cs.features[ENC_FT_ENC_MODE_TYPE  ] = double( encTestmode.type     );
      cs.features[ENC_FT_ENC_MODE_OPTS  ] = double( encTestmode.opts     );
    }
    
    bool EncModeCtrl::nextMode( const CodingStructure &cs, Partitioner &partitioner )
    {
      m_ComprCUCtxList.back().lastTestMode = m_ComprCUCtxList.back().testModes.back();
    
      m_ComprCUCtxList.back().testModes.pop_back();
    
      while( !m_ComprCUCtxList.back().testModes.empty() && !tryModeMaster( currTestMode(), cs, partitioner ) )
      {
        m_ComprCUCtxList.back().testModes.pop_back();
      }
    
      return !m_ComprCUCtxList.back().testModes.empty();
    }
    
    EncTestMode EncModeCtrl::currTestMode() const
    {
      return m_ComprCUCtxList.back().testModes.back();
    }
    
    EncTestMode EncModeCtrl::lastTestMode() const
    {
      return m_ComprCUCtxList.back().lastTestMode;
    }
    
    bool EncModeCtrl::anyMode() const
    {
      return !m_ComprCUCtxList.back().testModes.empty();
    }
    
    void EncModeCtrl::setBest( CodingStructure& cs )
    {
      if( cs.cost != MAX_DOUBLE && !cs.cus.empty() )
      {
        m_ComprCUCtxList.back().bestCS = &cs;
        m_ComprCUCtxList.back().bestCU = cs.cus[0];
        m_ComprCUCtxList.back().bestTU = cs.cus[0]->firstTU;
        m_ComprCUCtxList.back().lastTestMode = getCSEncMode( cs );
      }
    }
    
    void EncModeCtrl::xGetMinMaxQP( int& minQP, int& maxQP, const CodingStructure& cs, const Partitioner &partitioner, const int baseQP, const SPS& sps, const PPS& pps, const bool splitMode )
    {
      if( m_pcEncCfg->getUseRateCtrl() )
      {
        minQP = m_pcRateCtrl->getRCQP();
        maxQP = m_pcRateCtrl->getRCQP();
        return;
      }
    
      const uint32_t currDepth = partitioner.currDepth;
    
      if( !splitMode )
      {
        if( currDepth <= pps.getMaxCuDQPDepth() )
        {
          int deltaQP = m_pcEncCfg->getMaxDeltaQP();
          minQP = Clip3( -sps.getQpBDOffset( CHANNEL_TYPE_LUMA ), MAX_QP, baseQP - deltaQP );
          maxQP = Clip3( -sps.getQpBDOffset( CHANNEL_TYPE_LUMA ), MAX_QP, baseQP + deltaQP );
        }
    
    #if ENABLE_QPA_SUB_CTU
        else if (pps.getUseDQP() && pps.getMaxCuDQPDepth() > 0 && (!CS::isDualITree (cs) || isLuma (partitioner.chType)))
        {
          minQP = baseQP;
          maxQP = baseQP;
        }
    #endif
    
        else
        {
          minQP = cs.currQP[partitioner.chType];
          maxQP = cs.currQP[partitioner.chType];
        }
      }
      else
      {
        if( currDepth == pps.getMaxCuDQPDepth() )
        {
          int deltaQP = m_pcEncCfg->getMaxDeltaQP();
          minQP = Clip3( -sps.getQpBDOffset( CHANNEL_TYPE_LUMA ), MAX_QP, baseQP - deltaQP );
          maxQP = Clip3( -sps.getQpBDOffset( CHANNEL_TYPE_LUMA ), MAX_QP, baseQP + deltaQP );
        }
    
        else if (currDepth < pps.getMaxCuDQPDepth()
    #if ENABLE_QPA_SUB_CTU
             || (pps.getUseDQP() && pps.getMaxCuDQPDepth() > 0 && (!CS::isDualITree (cs) || isLuma (partitioner.chType)))
    #endif
                )
    
        {
          minQP = baseQP;
          maxQP = baseQP;
        }
        else
        {
          minQP = cs.currQP[partitioner.chType];
          maxQP = cs.currQP[partitioner.chType];
        }
    
    
      if (m_pcEncCfg->getLumaLevelToDeltaQPMapping().isEnabled() && (!CS::isDualITree (cs) || isLuma (partitioner.chType)))
      {
        minQP = Clip3 (-sps.getQpBDOffset (CHANNEL_TYPE_LUMA), MAX_QP, baseQP - m_lumaQPOffset);
        maxQP = minQP;
    
    }
    
    
    int EncModeCtrl::xComputeDQP( const CodingStructure &cs, const Partitioner &partitioner )
    {
      Picture* picture    = cs.picture;
      unsigned uiAQDepth  = std::min( partitioner.currDepth, ( uint32_t ) picture->aqlayer.size() - 1 );
      AQpLayer* pcAQLayer = picture->aqlayer[uiAQDepth];
    
      double dMaxQScale   = pow( 2.0, m_pcEncCfg->getQPAdaptationRange() / 6.0 );
      double dAvgAct      = pcAQLayer->getAvgActivity();
      double dCUAct       = pcAQLayer->getActivity( cs.area.Y().topLeft() );
      double dNormAct     = ( dMaxQScale*dCUAct + dAvgAct ) / ( dCUAct + dMaxQScale*dAvgAct );
      double dQpOffset    = log( dNormAct ) / log( 2.0 ) * 6.0;
      int    iQpOffset    = int( floor( dQpOffset + 0.49999 ) );
      return iQpOffset;
    }
    
    
    #if SHARP_LUMA_DELTA_QP
    void EncModeCtrl::initLumaDeltaQpLUT()
    {
      const LumaLevelToDeltaQPMapping &mapping = m_pcEncCfg->getLumaLevelToDeltaQPMapping();
    
      if( !mapping.isEnabled() )
      {
        return;
      }
    
      // map the sparse LumaLevelToDeltaQPMapping.mapping to a fully populated linear table.
    
      int         lastDeltaQPValue = 0;
      std::size_t nextSparseIndex = 0;
      for( int index = 0; index < LUMA_LEVEL_TO_DQP_LUT_MAXSIZE; index++ )
      {
        while( nextSparseIndex < mapping.mapping.size() && index >= mapping.mapping[nextSparseIndex].first )
        {
          lastDeltaQPValue = mapping.mapping[nextSparseIndex].second;
          nextSparseIndex++;
        }
        m_lumaLevelToDeltaQPLUT[index] = lastDeltaQPValue;
      }
    }
    
    int EncModeCtrl::calculateLumaDQP( const CPelBuf& rcOrg )
    {
      double avg = 0;
    
      // Get QP offset derived from Luma level
    #if !WCG_EXT
      if( m_pcEncCfg->getLumaLevelToDeltaQPMapping().mode == LUMALVL_TO_DQP_AVG_METHOD )
    #else
      CHECK( m_pcEncCfg->getLumaLevelToDeltaQPMapping().mode != LUMALVL_TO_DQP_AVG_METHOD, "invalid delta qp mode" );
    #endif
      {
    
        // Use average luma value
        avg = (double) rcOrg.mean();
    
      }
    #if !WCG_EXT
      else
      {
        // Use maximum luma value
        int maxVal = 0;
        for( uint32_t y = 0; y < rcOrg.height; y++ )
        {
          for( uint32_t x = 0; x < rcOrg.width; x++ )
          {
            const Pel& v = rcOrg.at( x, y );
            if( v > maxVal )
            {
              maxVal = v;
            }
          }
        }
        // use a percentage of the maxVal
        avg = ( double ) maxVal * m_pcEncCfg->getLumaLevelToDeltaQPMapping().maxMethodWeight;
      }
    #endif
      int lumaIdx = Clip3<int>( 0, int( LUMA_LEVEL_TO_DQP_LUT_MAXSIZE ) - 1, int( avg + 0.5 ) );
      int QP = m_lumaLevelToDeltaQPLUT[lumaIdx];
      return QP;
    }
    #endif
    
    #if ENABLE_SPLIT_PARALLELISM
    void EncModeCtrl::copyState( const EncModeCtrl& other, const UnitArea& area )
    {
      m_slice          = other.m_slice;
      m_fastDeltaQP    = other.m_fastDeltaQP;
      m_lumaQPOffset   = other.m_lumaQPOffset;
      m_runNextInParallel
                       = other.m_runNextInParallel;
      m_ComprCUCtxList = other.m_ComprCUCtxList;
    }
    
    #endif
    void CacheBlkInfoCtrl::create()
    {
      const unsigned numPos = MAX_CU_SIZE >> MIN_CU_LOG2;
    
      m_numWidths  = gp_sizeIdxInfo->numWidths();
      m_numHeights = gp_sizeIdxInfo->numHeights();
    
      for( unsigned x = 0; x < numPos; x++ )
      {
        for( unsigned y = 0; y < numPos; y++ )
        {
          m_codedCUInfo[x][y] = new CodedCUInfo**[m_numWidths];
    
          for( int wIdx = 0; wIdx < gp_sizeIdxInfo->numWidths(); wIdx++ )
          {
            if( gp_sizeIdxInfo->isCuSize( gp_sizeIdxInfo->sizeFrom( wIdx ) ) && x + ( gp_sizeIdxInfo->sizeFrom( wIdx ) >> MIN_CU_LOG2 ) <= ( MAX_CU_SIZE >> MIN_CU_LOG2 ) )
            {
              m_codedCUInfo[x][y][wIdx] = new CodedCUInfo*[gp_sizeIdxInfo->numHeights()];
    
              for( int hIdx = 0; hIdx < gp_sizeIdxInfo->numHeights(); hIdx++ )
              {
                if( gp_sizeIdxInfo->isCuSize( gp_sizeIdxInfo->sizeFrom( hIdx ) ) && y + ( gp_sizeIdxInfo->sizeFrom( hIdx ) >> MIN_CU_LOG2 ) <= ( MAX_CU_SIZE >> MIN_CU_LOG2 ) )
                {
                  m_codedCUInfo[x][y][wIdx][hIdx] = new CodedCUInfo;
                }
                else
                {
                  m_codedCUInfo[x][y][wIdx][hIdx] = nullptr;
                }
              }
            }
            else
            {
              m_codedCUInfo[x][y][wIdx] = nullptr;
            }
          }
        }
      }
    }
    
    void CacheBlkInfoCtrl::destroy()
    {
      const unsigned numPos = MAX_CU_SIZE >> MIN_CU_LOG2;
    
      for( unsigned x = 0; x < numPos; x++ )
      {
        for( unsigned y = 0; y < numPos; y++ )
        {
          for( int wIdx = 0; wIdx < gp_sizeIdxInfo->numWidths(); wIdx++ )
          {
            if( m_codedCUInfo[x][y][wIdx] )
            {
              for( int hIdx = 0; hIdx < gp_sizeIdxInfo->numHeights(); hIdx++ )
              {
                if( m_codedCUInfo[x][y][wIdx][hIdx] )
                {
                  delete m_codedCUInfo[x][y][wIdx][hIdx];
                }
              }
    
              delete[] m_codedCUInfo[x][y][wIdx];
            }
          }
    
          delete[] m_codedCUInfo[x][y];
        }
      }
    }
    
    void CacheBlkInfoCtrl::init( const Slice &slice )
    {
      const unsigned numPos = MAX_CU_SIZE >> MIN_CU_LOG2;
    
      for( unsigned x = 0; x < numPos; x++ )
      {
        for( unsigned y = 0; y < numPos; y++ )
        {
          for( int wIdx = 0; wIdx < gp_sizeIdxInfo->numWidths(); wIdx++ )
          {
            if( m_codedCUInfo[x][y][wIdx] )
            {
              for( int hIdx = 0; hIdx < gp_sizeIdxInfo->numHeights(); hIdx++ )
              {
                if( m_codedCUInfo[x][y][wIdx][hIdx] )
                {
                  memset( m_codedCUInfo[x][y][wIdx][hIdx], 0, sizeof( CodedCUInfo ) );
                }
              }
            }
          }
        }
      }
    
      m_slice_chblk = &slice;
    #if ENABLE_SPLIT_PARALLELISM
    
      m_currTemporalId = 0;
    #endif
    }
    #if ENABLE_SPLIT_PARALLELISM
    
    void CacheBlkInfoCtrl::touch( const UnitArea& area )
    {
      CodedCUInfo& cuInfo = getBlkInfo( area );
      cuInfo.temporalId = m_currTemporalId;
    }
    
    void CacheBlkInfoCtrl::copyState( const CacheBlkInfoCtrl &other, const UnitArea& area )
    {
      m_slice_chblk = other.m_slice_chblk;
    
      m_currTemporalId = other.m_currTemporalId;
    
      if( m_slice_chblk->isIntra() ) return;
    
      const int cuSizeMask = m_slice_chblk->getSPS()->getMaxCUWidth() - 1;
    
      const int minPosX = ( area.lx() & cuSizeMask ) >> MIN_CU_LOG2;
      const int minPosY = ( area.ly() & cuSizeMask ) >> MIN_CU_LOG2;
      const int maxPosX = ( area.Y().bottomRight().x & cuSizeMask ) >> MIN_CU_LOG2;
      const int maxPosY = ( area.Y().bottomRight().y & cuSizeMask ) >> MIN_CU_LOG2;
    
      for( unsigned x = minPosX; x <= maxPosX; x++ )
      {
        for( unsigned y = minPosY; y <= maxPosY; y++ )
        {
          for( int wIdx = 0; wIdx < gp_sizeIdxInfo->numWidths(); wIdx++ )
          {
            const int width = gp_sizeIdxInfo->sizeFrom( wIdx );
    
            if( m_codedCUInfo[x][y][wIdx] && width <= area.lwidth() && x + ( width >> MIN_CU_LOG2 ) <= ( maxPosX + 1 ) )
            {
              for( int hIdx = 0; hIdx < gp_sizeIdxInfo->numHeights(); hIdx++ )
              {
                const int height = gp_sizeIdxInfo->sizeFrom( hIdx );
    
                if( gp_sizeIdxInfo->isCuSize( height ) && height <= area.lheight() && y + ( height >> MIN_CU_LOG2 ) <= ( maxPosY + 1 ) )
                {
                  if( other.m_codedCUInfo[x][y][wIdx][hIdx]->temporalId > m_codedCUInfo[x][y][wIdx][hIdx]->temporalId )
                  {
                    *m_codedCUInfo[x][y][wIdx][hIdx] = *other.m_codedCUInfo[x][y][wIdx][hIdx];
                    m_codedCUInfo[x][y][wIdx][hIdx]->temporalId = m_currTemporalId;
                  }
                }
                else if( y + ( height >> MIN_CU_LOG2 ) > maxPosY + 1 )
                {
                  break;;
                }
              }
            }
            else if( x + ( width >> MIN_CU_LOG2 ) > maxPosX + 1 )
            {
              break;
            }
          }
        }
      }
    }
    #endif
    
    CodedCUInfo& CacheBlkInfoCtrl::getBlkInfo( const UnitArea& area )
    {
      unsigned idx1, idx2, idx3, idx4;
      getAreaIdx( area.Y(), *m_slice_chblk->getPPS()->pcv, idx1, idx2, idx3, idx4 );
    
      return *m_codedCUInfo[idx1][idx2][idx3][idx4];
    }
    
    bool CacheBlkInfoCtrl::isSkip( const UnitArea& area )
    {
      unsigned idx1, idx2, idx3, idx4;
      getAreaIdx( area.Y(), *m_slice_chblk->getPPS()->pcv, idx1, idx2, idx3, idx4 );
    
      return m_codedCUInfo[idx1][idx2][idx3][idx4]->isSkip;
    }
    
    
    bool CacheBlkInfoCtrl::isMMVDSkip(const UnitArea& area)
    {
      unsigned idx1, idx2, idx3, idx4;
      getAreaIdx(area.Y(), *m_slice_chblk->getPPS()->pcv, idx1, idx2, idx3, idx4);
    
      return m_codedCUInfo[idx1][idx2][idx3][idx4]->isMMVDSkip;
    }
    
    
    void CacheBlkInfoCtrl::setMv( const UnitArea& area, const RefPicList refPicList, const int iRefIdx, const Mv& rMv )
    {
      if( iRefIdx >= MAX_STORED_CU_INFO_REFS ) return;
    
      unsigned idx1, idx2, idx3, idx4;
      getAreaIdx( area.Y(), *m_slice_chblk->getPPS()->pcv, idx1, idx2, idx3, idx4 );
    
      m_codedCUInfo[idx1][idx2][idx3][idx4]->saveMv [refPicList][iRefIdx] = rMv;
      m_codedCUInfo[idx1][idx2][idx3][idx4]->validMv[refPicList][iRefIdx] = true;
    #if ENABLE_SPLIT_PARALLELISM
    
      touch( area );
    #endif
    }
    
    bool CacheBlkInfoCtrl::getMv( const UnitArea& area, const RefPicList refPicList, const int iRefIdx, Mv& rMv ) const
    {
      unsigned idx1, idx2, idx3, idx4;
      getAreaIdx( area.Y(), *m_slice_chblk->getPPS()->pcv, idx1, idx2, idx3, idx4 );
    
      if( iRefIdx >= MAX_STORED_CU_INFO_REFS )
      {
        rMv = m_codedCUInfo[idx1][idx2][idx3][idx4]->saveMv[refPicList][0];
        return false;
      }
    
      rMv = m_codedCUInfo[idx1][idx2][idx3][idx4]->saveMv[refPicList][iRefIdx];
      return m_codedCUInfo[idx1][idx2][idx3][idx4]->validMv[refPicList][iRefIdx];
    }
    
    
    bool CacheBlkInfoCtrl::getInter(const UnitArea& area)
    {
      unsigned idx1, idx2, idx3, idx4;
      getAreaIdx(area.Y(), *m_slice_chblk->getPPS()->pcv, idx1, idx2, idx3, idx4);
    
      return m_codedCUInfo[idx1][idx2][idx3][idx4]->isInter;
    }
    void CacheBlkInfoCtrl::setGbiIdx(const UnitArea& area, uint8_t gBiIdx)
    {
      unsigned idx1, idx2, idx3, idx4;
      getAreaIdx(area.Y(), *m_slice_chblk->getPPS()->pcv, idx1, idx2, idx3, idx4);
    
      m_codedCUInfo[idx1][idx2][idx3][idx4]->GBiIdx = gBiIdx;
    }
    uint8_t CacheBlkInfoCtrl::getGbiIdx(const UnitArea& area)
    {
      unsigned idx1, idx2, idx3, idx4;
      getAreaIdx(area.Y(), *m_slice_chblk->getPPS()->pcv, idx1, idx2, idx3, idx4);
    
      return m_codedCUInfo[idx1][idx2][idx3][idx4]->GBiIdx;
    }
    
    static bool isTheSameNbHood( const CodingUnit &cu, const CodingStructure& cs, const Partitioner &partitioner
    #if JVET_M0170_MRG_SHARELIST
                                , const PredictionUnit &pu, int picW, int picH
    #endif
                               )
    
    {
      if( cu.chType != partitioner.chType )
      {
        return false;
      }
    
      const PartitioningStack &ps = partitioner.getPartStack();
    
      int i = 1;
    
      for( ; i < ps.size(); i++ )
      {
        if( ps[i].split != CU::getSplitAtDepth( cu, i - 1 ) )
        {
          break;
        }
      }
    
      const UnitArea &cmnAnc = ps[i - 1].parts[ps[i - 1].idx];
    
      const UnitArea cuArea  = CS::getArea( cs, cu, partitioner.chType );
    
    #if JVET_M0170_MRG_SHARELIST
      bool sharedListReuseMode = true;
      if(
          pu.mergeFlag == true &&
          cu.affine == false &&    
          cu.predMode == MODE_INTER
        )
      {
        sharedListReuseMode = false;
    
        if ((cu.lumaSize().width*cu.lumaSize().height) >= MRG_SHARELIST_SHARSIZE)
        {
          sharedListReuseMode = true;
        }
    
        if (((cmnAnc.lumaSize().width)*(cmnAnc.lumaSize().height) <= MRG_SHARELIST_SHARSIZE))
        {
          sharedListReuseMode = true;
        }
      }
      else
      {
        sharedListReuseMode = true;
      }
    //#endif
    #endif
    
        if( i < cuArea.blocks.size() && cuArea.blocks[i].valid() && cuArea.blocks[i].pos() != cmnAnc.blocks[i].pos() )
    
    #if JVET_M0170_MRG_SHARELIST
      if(!sharedListReuseMode)
      {
        return false;
      }
    #endif
    
    
    
      return true;
    }
    
    void BestEncInfoCache::create( const ChromaFormat chFmt )
    {
      const unsigned numPos = MAX_CU_SIZE >> MIN_CU_LOG2;
    
      m_numWidths  = gp_sizeIdxInfo->numWidths();
      m_numHeights = gp_sizeIdxInfo->numHeights();
    
      for( unsigned x = 0; x < numPos; x++ )
      {
        for( unsigned y = 0; y < numPos; y++ )
        {
          m_bestEncInfo[x][y] = new BestEncodingInfo**[m_numWidths];
    
          for( int wIdx = 0; wIdx < gp_sizeIdxInfo->numWidths(); wIdx++ )
          {
            if( gp_sizeIdxInfo->isCuSize( gp_sizeIdxInfo->sizeFrom( wIdx ) ) && x + ( gp_sizeIdxInfo->sizeFrom( wIdx ) >> MIN_CU_LOG2 ) <= ( MAX_CU_SIZE >> MIN_CU_LOG2 ) )
            {
              m_bestEncInfo[x][y][wIdx] = new BestEncodingInfo*[gp_sizeIdxInfo->numHeights()];
    
              for( int hIdx = 0; hIdx < gp_sizeIdxInfo->numHeights(); hIdx++ )
              {
                if( gp_sizeIdxInfo->isCuSize( gp_sizeIdxInfo->sizeFrom( hIdx ) ) && y + ( gp_sizeIdxInfo->sizeFrom( hIdx ) >> MIN_CU_LOG2 ) <= ( MAX_CU_SIZE >> MIN_CU_LOG2 ) )
                {
                  m_bestEncInfo[x][y][wIdx][hIdx] = new BestEncodingInfo;
    
                  int w = gp_sizeIdxInfo->sizeFrom( wIdx );
                  int h = gp_sizeIdxInfo->sizeFrom( hIdx );
    
                  const UnitArea area( chFmt, Area( 0, 0, w, h ) );
    
                  m_bestEncInfo[x][y][wIdx][hIdx]->cu.UnitArea::operator=( area );
                  m_bestEncInfo[x][y][wIdx][hIdx]->pu.UnitArea::operator=( area );
                  m_bestEncInfo[x][y][wIdx][hIdx]->tu.UnitArea::operator=( area );
    
                  m_bestEncInfo[x][y][wIdx][hIdx]->poc      = -1;
                  m_bestEncInfo[x][y][wIdx][hIdx]->testMode = EncTestMode();
                }
                else
                {
                  m_bestEncInfo[x][y][wIdx][hIdx] = nullptr;
                }
              }
            }
            else
            {
              m_bestEncInfo[x][y][wIdx] = nullptr;
            }
          }
        }
      }
    }
    
    void BestEncInfoCache::destroy()
    {
      const unsigned numPos = MAX_CU_SIZE >> MIN_CU_LOG2;
    
      for( unsigned x = 0; x < numPos; x++ )
      {
        for( unsigned y = 0; y < numPos; y++ )
        {
          for( int wIdx = 0; wIdx < gp_sizeIdxInfo->numWidths(); wIdx++ )
          {
            if( m_bestEncInfo[x][y][wIdx] )
            {
              for( int hIdx = 0; hIdx < gp_sizeIdxInfo->numHeights(); hIdx++ )
              {
                if( m_bestEncInfo[x][y][wIdx][hIdx] )
                {
                  delete m_bestEncInfo[x][y][wIdx][hIdx];
                }
              }
    
              delete[] m_bestEncInfo[x][y][wIdx];
            }
          }
    
          delete[] m_bestEncInfo[x][y];
        }
      }
    
      delete[] m_pCoeff;
      delete[] m_pPcmBuf;
    }
    
    void BestEncInfoCache::init( const Slice &slice )
    {
      bool isInitialized = m_slice_bencinf;
    
      m_slice_bencinf = &slice;
    
      if( isInitialized ) return;
      
      const unsigned numPos = MAX_CU_SIZE >> MIN_CU_LOG2;
    
      m_numWidths  = gp_sizeIdxInfo->numWidths();
      m_numHeights = gp_sizeIdxInfo->numHeights();
    
      size_t numCoeff = 0;
      
      for( unsigned x = 0; x < numPos; x++ )
      {
        for( unsigned y = 0; y < numPos; y++ )
        {
          for( int wIdx = 0; wIdx < gp_sizeIdxInfo->numWidths(); wIdx++ )
          {
            if( m_bestEncInfo[x][y][wIdx] ) for( int hIdx = 0; hIdx < gp_sizeIdxInfo->numHeights(); hIdx++ )
            {
              if( m_bestEncInfo[x][y][wIdx][hIdx] )
              {
                for( const CompArea& blk : m_bestEncInfo[x][y][wIdx][hIdx]->cu.blocks )
                {
                  numCoeff += blk.area();
                }
              }
            }
          }
        }
      }
    
      m_pCoeff  = new TCoeff[numCoeff];
      m_pPcmBuf = new Pel   [numCoeff];
    
      TCoeff *coeffPtr = m_pCoeff;
      Pel    *pcmPtr   = m_pPcmBuf;
    
      m_dummyCS.pcv = m_slice_bencinf->getPPS()->pcv;
    
      for( unsigned x = 0; x < numPos; x++ )
      {
        for( unsigned y = 0; y < numPos; y++ )
        {
          for( int wIdx = 0; wIdx < gp_sizeIdxInfo->numWidths(); wIdx++ )
          {
            if( m_bestEncInfo[x][y][wIdx] ) for( int hIdx = 0; hIdx < gp_sizeIdxInfo->numHeights(); hIdx++ )
            {
              if( m_bestEncInfo[x][y][wIdx][hIdx] )
              {
                TCoeff *coeff[MAX_NUM_TBLOCKS] = { 0, };
                Pel    *pcmbf[MAX_NUM_TBLOCKS] = { 0, };
    
                const UnitArea &area = m_bestEncInfo[x][y][wIdx][hIdx]->tu;
    
                for( int i = 0; i < area.blocks.size(); i++ )
                {
                  coeff[i] = coeffPtr; coeffPtr += area.blocks[i].area();
                  pcmbf[i] =   pcmPtr;   pcmPtr += area.blocks[i].area();
                }
    
                m_bestEncInfo[x][y][wIdx][hIdx]->tu.cs = &m_dummyCS;
                m_bestEncInfo[x][y][wIdx][hIdx]->tu.init( coeff, pcmbf );
              }
            }
          }
        }
      }
    }
    
    bool BestEncInfoCache::setFromCs( const CodingStructure& cs, const Partitioner& partitioner )
    {
      if( cs.cus.size() != 1 || cs.tus.size() != 1 || cs.pus.size() != 1 )
      {
        return false;
      }
    
      unsigned idx1, idx2, idx3, idx4;
      getAreaIdx( cs.area.Y(), *m_slice_bencinf->getPPS()->pcv, idx1, idx2, idx3, idx4 );
    
      BestEncodingInfo& encInfo = *m_bestEncInfo[idx1][idx2][idx3][idx4];
    
      encInfo.poc            =  cs.picture->poc;
      encInfo.cu.repositionTo( *cs.cus.front() );
      encInfo.pu.repositionTo( *cs.pus.front() );
      encInfo.tu.repositionTo( *cs.tus.front() );
      encInfo.cu             = *cs.cus.front();
      encInfo.pu             = *cs.pus.front();
      for( auto &blk : cs.tus.front()->blocks )
      {
        if( blk.valid() ) encInfo.tu.copyComponentFrom( *cs.tus.front(), blk.compID );
      }
      encInfo.testMode       = getCSEncMode( cs );
    
      return true;
    }
    
    
    bool BestEncInfoCache::isValid( const CodingStructure& cs, const Partitioner& partitioner, int qp )
    
    {
      unsigned idx1, idx2, idx3, idx4;
      getAreaIdx( cs.area.Y(), *m_slice_bencinf->getPPS()->pcv, idx1, idx2, idx3, idx4 );
    
      BestEncodingInfo& encInfo = *m_bestEncInfo[idx1][idx2][idx3][idx4];
    
    
      if( encInfo.cu.qp != qp )
        return false;
    
      if( cs.picture->poc != encInfo.poc || CS::getArea( cs, cs.area, partitioner.chType ) != CS::getArea( cs, encInfo.cu, partitioner.chType ) || !isTheSameNbHood( encInfo.cu, cs, partitioner
    #if JVET_M0170_MRG_SHARELIST
        , encInfo.pu, (cs.picture->Y().width), (cs.picture->Y().height)
    #endif
    ) 
    
    Yu Han's avatar
    Yu Han committed
        || encInfo.cu.ibc
    
      {
        return false;
      }
      else
      {
        return true;
      }
    }
    
    bool BestEncInfoCache::setCsFrom( CodingStructure& cs, EncTestMode& testMode, const Partitioner& partitioner ) const
    {
      unsigned idx1, idx2, idx3, idx4;
      getAreaIdx( cs.area.Y(), *m_slice_bencinf->getPPS()->pcv, idx1, idx2, idx3, idx4 );
    
      BestEncodingInfo& encInfo = *m_bestEncInfo[idx1][idx2][idx3][idx4];
    
    
      if( cs.picture->poc != encInfo.poc || CS::getArea( cs, cs.area, partitioner.chType ) != CS::getArea( cs, encInfo.cu, partitioner.chType ) || !isTheSameNbHood( encInfo.cu, cs, partitioner 
    #if JVET_M0170_MRG_SHARELIST
        , encInfo.pu, (cs.picture->Y().width), (cs.picture->Y().height)
    #endif
    ) )
    
      {
        return false;
      }
    
      CodingUnit     &cu = cs.addCU( CS::getArea( cs, cs.area, partitioner.chType ), partitioner.chType );
      PredictionUnit &pu = cs.addPU( CS::getArea( cs, cs.area, partitioner.chType ), partitioner.chType );
      TransformUnit  &tu = cs.addTU( CS::getArea( cs, cs.area, partitioner.chType ), partitioner.chType );
    
      cu          .repositionTo( encInfo.cu );
      pu          .repositionTo( encInfo.pu );
      tu          .repositionTo( encInfo.tu );
    
      cu          = encInfo.cu;
      pu          = encInfo.pu;
      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() );
    }
    
    void EncModeCtrlMTnoRQT::destroy()
    {
      CacheBlkInfoCtrl::destroy();
      BestEncInfoCache::destroy();
    }
    
    #endif
    void EncModeCtrlMTnoRQT::initCTUEncoding( const Slice &slice )
    {
      CacheBlkInfoCtrl::init( slice );
    #if REUSE_CU_RESULTS
      BestEncInfoCache::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);
    
    
    #if ENABLE_QPA_SUB_CTU
    static Position getMaxLumaDQPDepthPos (const CodingStructure &cs, const Partitioner &partitioner)
    {
      if (partitioner.currDepth <= cs.pps->getMaxCuDQPDepth())
      {
        return partitioner.currArea().lumaPos();
      }
      const PartLevel splitAtMaxDepth = partitioner.getPartStack().at (cs.pps->getMaxCuDQPDepth());
      // the parent node of qtDepth + mttDepth == maxDqpDepth
      return splitAtMaxDepth.parts[splitAtMaxDepth.idx].lumaPos();
    }
    #endif
    
    
    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 (!CS::isDualITree (cs) || isLuma (partitioner.chType))
    
          baseQP = Clip3(-cs.sps->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, baseQP + xComputeDQP (cs, partitioner));
    
    #if ENABLE_QPA_SUB_CTU
        else if (m_pcEncCfg->getUsePerceptQPA() && !m_pcEncCfg->getUseRateCtrl() && cs.pps->getUseDQP() && cs.pps->getMaxCuDQPDepth() > 0)
        {
          const PreCalcValues &pcv = *cs.pcv;
    
          if ((partitioner.currArea().lwidth() < pcv.maxCUWidth) && (partitioner.currArea().lheight() < pcv.maxCUHeight) && cs.picture)
          {
            const Position    &pos = getMaxLumaDQPDepthPos (cs, partitioner);
            const unsigned mtsLog2 = (unsigned)g_aucLog2[std::min (cs.sps->getMaxTrSize(), pcv.maxCUWidth)];
            const unsigned  stride = pcv.maxCUWidth >> mtsLog2;
    
            baseQP = cs.picture->m_subCtuQP[((pos.x & pcv.maxCUWidthMask) >> mtsLog2) + stride * ((pos.y & pcv.maxCUHeightMask) >> mtsLog2)];
          }