Skip to content
Snippets Groups Projects
EncCu.cpp 147 KiB
Newer Older
  • Learn to ignore specific revisions
  •           bitsRefineStep--;
            }
    
            bitsCand = bitsBaseIdx + bitsRefineStep + bitsDirection;
            bitsCand++; // for mmvd_flag
    
            mergeCtx.setMmvdMergeCandiInfo(pu, mmvdMergeCand);
    
            PU::spanMotionInfo(pu, mergeCtx);
    
            distParam.cur = singleMergeTempBuffer->Y();
    
            pu.mmvdEncOptMode = (refineStep > 2 ? 2 : 1);
    
            CHECK(!pu.mmvdMergeFlag, "MMVD merge should be set");
            // Don't do chroma MC here
            m_pcInterSearch->motionCompensation(pu, *singleMergeTempBuffer, REF_PIC_LIST_X, true, false);
    
            Distortion uiSad = distParam.distFunc(distParam);
    
    
            double cost = (double)uiSad + (double)bitsCand * sqrtLambdaForFirstPass;
            insertPos = -1;
    
            updateDoubleCandList(mergeCand, cost, RdModeList, candCostList, RdModeList2, (uint32_t)NUM_LUMA_MODE, uiNumMrgSATDCand, &insertPos);
    
            if (insertPos != -1)
            {
              for (int i = int(RdModeList.size()) - 1; i > insertPos; i--)
              {
                swap(acMergeTempBuffer[i - 1], acMergeTempBuffer[i]);
              }
              swap(singleMergeTempBuffer, acMergeTempBuffer[insertPos]);
            }
          }
    
    Frank Bossen's avatar
    Frank Bossen committed
    #endif
    
          // Try to limit number of candidates using SATD-costs
          for( uint32_t i = 1; i < uiNumMrgSATDCand; i++ )
          {
            if( candCostList[i] > MRG_FAST_RATIO * candCostList[0] )
            {
              uiNumMrgSATDCand = i;
              break;
            }
          }
    
    
          setMergeBestSATDCost( candCostList[0] );
    
    
            pu.mhIntraFlag = true;
    
            for (uint32_t mergeCnt = 0; mergeCnt < uiNumMrgSATDCand; mergeCnt++)
            {
              if (RdModeList[mergeCnt] >= (MRG_MAX_NUM_CANDS + MMVD_ADD_NUM))
              {
                pu.intraDir[0] = RdModeList2[mergeCnt];
                pu.intraDir[1] = DM_CHROMA_IDX;
                uint32_t bufIdx = (pu.intraDir[0] > 1) ? (pu.intraDir[0] == HOR_IDX ? 2 : 3) : pu.intraDir[0];
                bool isUseFilter = IntraPrediction::useFilteredIntraRefSamples(COMPONENT_Cb, pu, true, pu);
                m_pcIntraSearch->initIntraPatternChType(*pu.cu, pu.Cb(), isUseFilter);
                m_pcIntraSearch->predIntraAng(COMPONENT_Cb, pu.cs->getPredBuf(pu).Cb(), pu, isUseFilter);
                m_pcIntraSearch->switchBuffer(pu, COMPONENT_Cb, pu.cs->getPredBuf(pu).Cb(), m_pcIntraSearch->getPredictorPtr2(COMPONENT_Cb, bufIdx));
                isUseFilter = IntraPrediction::useFilteredIntraRefSamples(COMPONENT_Cr, pu, true, pu);
                m_pcIntraSearch->initIntraPatternChType(*pu.cu, pu.Cr(), isUseFilter);
                m_pcIntraSearch->predIntraAng(COMPONENT_Cr, pu.cs->getPredBuf(pu).Cr(), pu, isUseFilter);
                m_pcIntraSearch->switchBuffer(pu, COMPONENT_Cr, pu.cs->getPredBuf(pu).Cr(), m_pcIntraSearch->getPredictorPtr2(COMPONENT_Cr, bufIdx));
              }
            }
    
            pu.mhIntraFlag = false;
    
          tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
        }
        else
        {
    
          if (bestIsMMVDSkip)
          {
            uiNumMrgSATDCand = mergeCtx.numValidMergeCand + MMVD_ADD_NUM;
          }
          else
          {
            uiNumMrgSATDCand = mergeCtx.numValidMergeCand;
          }
    
    Nan Hu's avatar
    Nan Hu committed
      m_bestModeUpdated = tempCS->useDbCost = bestCS->useDbCost = false;
    
      uint32_t iteration;
      uint32_t iterationBegin = m_modeCtrl->getIsHashPerfectMatch() ? 1 : 0;
      if (encTestMode.lossless)
      {
        iteration = 1;
        iterationBegin = 0;
      }
      else
      {
        iteration = 2;
      }
      for (uint32_t uiNoResidualPass = iterationBegin; uiNoResidualPass < iteration; ++uiNoResidualPass)
    
      {
        for( uint32_t uiMrgHADIdx = 0; uiMrgHADIdx < uiNumMrgSATDCand; uiMrgHADIdx++ )
        {
          uint32_t uiMergeCand = RdModeList[uiMrgHADIdx];
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
    
    
          if (uiNoResidualPass != 0 && uiMergeCand >= (MRG_MAX_NUM_CANDS + MMVD_ADD_NUM)) // intrainter does not support skip mode
          {
            uiMergeCand -= (MRG_MAX_NUM_CANDS + MMVD_ADD_NUM); // for skip, map back to normal merge candidate idx and try RDO
            if (isTestSkipMerge[uiMergeCand])
            {
              continue;
            }
          }
    
    
          if (((uiNoResidualPass != 0) && candHasNoResidual[uiMrgHADIdx])
    
           || ( (uiNoResidualPass == 0) && bestIsSkip ) )
          {
            continue;
          }
    
          // first get merge candidates
          CodingUnit &cu      = tempCS->addCU( tempCS->area, partitioner.chType );
    
          partitioner.setCUData( cu );
          cu.slice            = tempCS->slice;
          cu.tileIdx          = tempCS->picture->tileMap->getTileIdxMap( tempCS->area.lumaPos() );
          cu.skip             = false;
    
        //cu.affine
          cu.predMode         = MODE_INTER;
        //cu.LICFlag
          cu.transQuantBypass = encTestMode.lossless;
          cu.chromaQpAdj      = cu.transQuantBypass ? 0 : m_cuChromaQpOffsetIdxPlus1;
          cu.qp               = encTestMode.qp;
          PredictionUnit &pu  = tempCS->addPU( cu, partitioner.chType );
    
    
          if (uiNoResidualPass == 0 && uiMergeCand >= (MRG_MAX_NUM_CANDS + MMVD_ADD_NUM))
          {
            uiMergeCand -= (MRG_MAX_NUM_CANDS + MMVD_ADD_NUM);
            cu.mmvdSkip = false;
            mergeCtx.setMergeInfo(pu, uiMergeCand);
    
            pu.mhIntraFlag = true;
    
            pu.intraDir[0] = RdModeList2[uiMrgHADIdx];
            CHECK(pu.intraDir[0]<0 || pu.intraDir[0]>(NUM_LUMA_MODE - 1), "out of intra mode");
            pu.intraDir[1] = DM_CHROMA_IDX;
          }
    
          else if (uiMergeCand >= mergeCtx.numValidMergeCand && uiMergeCand < MRG_MAX_NUM_CANDS + MMVD_ADD_NUM)
    
          {
            cu.mmvdSkip = true;
            mergeCtx.setMmvdMergeCandiInfo(pu, uiMergeCand - mergeCtx.numValidMergeCand);
          }
          else
          {
            cu.mmvdSkip = false;
            mergeCtx.setMergeInfo(pu, uiMergeCand);
          }
    
          if( m_pcEncCfg->getMCTSEncConstraint() )
    
            bool isDMVR = PU::checkDMVRCondition( pu );
            if( ( isDMVR && MCTSHelper::isRefBlockAtRestrictedTileBoundary( pu ) ) || ( !isDMVR && !( MCTSHelper::checkMvBufferForMCTSConstraint( pu ) ) ) )
            {
              // Do not use this mode
              tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
              continue;
            }
    
            {
              int dx, dy, i, j, num = 0;
              dy = std::min<int>(pu.lumaSize().height, DMVR_SUBCU_HEIGHT);
              dx = std::min<int>(pu.lumaSize().width, DMVR_SUBCU_WIDTH);
              if (PU::checkDMVRCondition(pu))
              {
                for (i = 0; i < (pu.lumaSize().height); i += dy)
                {
                  for (j = 0; j < (pu.lumaSize().width); j += dx)
                  {
                    pu.mvdL0SubPu[num] = refinedMvdL0[num][uiMergeCand];
                    num++;
                  }
                }
              }
            }
    
            if (pu.mhIntraFlag)
    
            {
              uint32_t bufIdx = (pu.intraDir[0] > 1) ? (pu.intraDir[0] == HOR_IDX ? 2 : 3) : pu.intraDir[0];
              PelBuf tmpBuf = tempCS->getPredBuf(pu).Y();
              tmpBuf.copyFrom(acMergeBuffer[uiMergeCand].Y());
    
    Taoran Lu's avatar
    Taoran Lu committed
              if (pu.cs->slice->getReshapeInfo().getUseSliceReshaper() && m_pcReshape->getCTUFlag())
              {
                tmpBuf.rspSignal(m_pcReshape->getFwdLUT());
              }
    
              m_pcIntraSearch->geneWeightedPred(COMPONENT_Y, tmpBuf, pu, m_pcIntraSearch->getPredictorPtr2(COMPONENT_Y, bufIdx));
              tmpBuf = tempCS->getPredBuf(pu).Cb();
              tmpBuf.copyFrom(acMergeBuffer[uiMergeCand].Cb());
              m_pcIntraSearch->geneWeightedPred(COMPONENT_Cb, tmpBuf, pu, m_pcIntraSearch->getPredictorPtr2(COMPONENT_Cb, bufIdx));
              tmpBuf = tempCS->getPredBuf(pu).Cr();
              tmpBuf.copyFrom(acMergeBuffer[uiMergeCand].Cr());
              m_pcIntraSearch->geneWeightedPred(COMPONENT_Cr, tmpBuf, pu, m_pcIntraSearch->getPredictorPtr2(COMPONENT_Cr, bufIdx));
            }
            else
            {
    
              if (uiMergeCand >= mergeCtx.numValidMergeCand && uiMergeCand < MRG_MAX_NUM_CANDS + MMVD_ADD_NUM) {
                pu.mmvdEncOptMode = 0;
                m_pcInterSearch->motionCompensation(pu);
              }
              else
    
              if (uiNoResidualPass != 0 && uiMergeCand < mergeCtx.numValidMergeCand && RdModeList[uiMrgHADIdx] >= (MRG_MAX_NUM_CANDS + MMVD_ADD_NUM))
              {
                tempCS->getPredBuf().copyFrom(acMergeBuffer[uiMergeCand]);
              }
              else
              {
                tempCS->getPredBuf().copyFrom(*acMergeTempBuffer[uiMrgHADIdx]);
              }
            }
    
          if (!cu.mmvdSkip && !pu.mhIntraFlag && uiNoResidualPass != 0)
    
          {
            CHECK(uiMergeCand >= mergeCtx.numValidMergeCand, "out of normal merge");
            isTestSkipMerge[uiMergeCand] = true;
          }
    
    
          xEncodeInterResidual( tempCS, bestCS, partitioner, encTestMode, uiNoResidualPass, uiNoResidualPass == 0 ? &candHasNoResidual[uiMrgHADIdx] : NULL );
    
          if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip && !pu.mhIntraFlag)
    
    Valeri George's avatar
    Valeri George committed
            bestIsSkip = !bestCS->cus.empty() && bestCS->getCU( partitioner.chType )->rootCbf == 0;
    
          }
          tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
        }// end loop uiMrgHADIdx
    
        if( uiNoResidualPass == 0 && m_pcEncCfg->getUseEarlySkipDetection() )
        {
          const CodingUnit     &bestCU = *bestCS->getCU( partitioner.chType );
          const PredictionUnit &bestPU = *bestCS->getPU( partitioner.chType );
    
          if( bestCU.rootCbf == 0 )
          {
            if( bestPU.mergeFlag )
            {
              m_modeCtrl->setEarlySkipDetected();
            }
            else if( m_pcEncCfg->getMotionEstimationSearchMethod() != MESEARCH_SELECTIVE )
            {
              int absolute_MV = 0;
    
              for( uint32_t uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
              {
                if( slice.getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
                {
                  absolute_MV += bestPU.mvd[uiRefListIdx].getAbsHor() + bestPU.mvd[uiRefListIdx].getAbsVer();
                }
              }
    
              if( absolute_MV == 0 )
              {
                m_modeCtrl->setEarlySkipDetected();
              }
            }
          }
        }
      }
    
    Nan Hu's avatar
    Nan Hu committed
      if ( m_bestModeUpdated && bestCS->cost != MAX_DOUBLE )
    
    Nan Hu's avatar
    Nan Hu committed
      {
    
    Nan Hu's avatar
    Nan Hu committed
        xCalDebCost( *bestCS, partitioner );
    
    Nan Hu's avatar
    Nan Hu committed
      }
    
    void EncCu::xCheckRDCostMergeTriangle2Nx2N( CodingStructure *&tempCS, CodingStructure *&bestCS, Partitioner &partitioner, const EncTestMode& encTestMode )
    {
      const Slice &slice = *tempCS->slice;
      const SPS &sps = *tempCS->sps;
    
      CHECK( slice.getSliceType() != B_SLICE, "Triangle mode is only applied to B-slices" );
    
      tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
    
    rlliao's avatar
    rlliao committed
      bool trianglecandHasNoResidual[TRIANGLE_MAX_NUM_CANDS];
      for( int mergeCand = 0; mergeCand < TRIANGLE_MAX_NUM_CANDS; mergeCand++ )
    
    rlliao's avatar
    rlliao committed
        trianglecandHasNoResidual[mergeCand] = false;
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      bool bestIsSkip;
      CodingUnit* cuTemp = bestCS->getCU(partitioner.chType);
      if (cuTemp)
        bestIsSkip = m_pcEncCfg->getUseFastDecisionForMerge() ? bestCS->getCU(partitioner.chType)->rootCbf == 0 : false;
      else
        bestIsSkip = false;
    
    rlliao's avatar
    rlliao committed
      uint8_t                                         numTriangleCandidate   = TRIANGLE_MAX_NUM_CANDS;
      uint8_t                                         triangleNumMrgSATDCand = TRIANGLE_MAX_NUM_SATD_CANDS;
      PelUnitBuf                                      triangleBuffer[TRIANGLE_MAX_NUM_UNI_CANDS];
      PelUnitBuf                                      triangleWeightedBuffer[TRIANGLE_MAX_NUM_CANDS];
      static_vector<uint8_t, TRIANGLE_MAX_NUM_CANDS> triangleRdModeList;
      static_vector<double,  TRIANGLE_MAX_NUM_CANDS> tianglecandCostList;
    
    
      if( auto blkCache = dynamic_cast< CacheBlkInfoCtrl* >( m_modeCtrl ) )
      {
        bestIsSkip |= blkCache->isSkip( tempCS->area );
      }
    
      DistParam distParam;
    
    rlliao's avatar
    rlliao committed
      const bool useHadamard = !encTestMode.lossless;
      m_pcRdCost->setDistParam( distParam, tempCS->getOrgBuf().Y(), m_acMergeBuffer[0].Y(), sps.getBitDepth( CHANNEL_TYPE_LUMA ), COMPONENT_Y, useHadamard );
    
    
      const UnitArea localUnitArea( tempCS->area.chromaFormat, Area( 0, 0, tempCS->area.Y().width, tempCS->area.Y().height) );
    
      const double sqrtLambdaForFirstPass = m_pcRdCost->getMotionLambda(encTestMode.lossless);
    
    
    rlliao's avatar
    rlliao committed
      MergeCtx triangleMrgCtx;
    
      {
        CodingUnit cu( tempCS->area );
        cu.cs       = tempCS;
        cu.predMode = MODE_INTER;
        cu.slice    = tempCS->slice;
    
    Karsten Suehring's avatar
    Karsten Suehring committed
        cu.tileIdx          = tempCS->picture->tileMap->getTileIdxMap( tempCS->area.lumaPos() );
    
        cu.triangle = true;
        cu.mmvdSkip = false;
        cu.GBiIdx   = GBI_DEFAULT;
    
        PredictionUnit pu( tempCS->area );
        pu.cu = &cu;
        pu.cs = tempCS;
    
    
    
    rlliao's avatar
    rlliao committed
        PU::getTriangleMergeCandidates( pu, triangleMrgCtx );
        for( uint8_t mergeCand = 0; mergeCand < TRIANGLE_MAX_NUM_UNI_CANDS; mergeCand++ )
    
    rlliao's avatar
    rlliao committed
          triangleBuffer[mergeCand] = m_acMergeBuffer[mergeCand].getBuf(localUnitArea);
          triangleMrgCtx.setMergeInfo( pu, mergeCand );
          PU::spanMotionInfo( pu, triangleMrgCtx );
    
    Valeri George's avatar
    Valeri George committed
          if( m_pcEncCfg->getMCTSEncConstraint() && ( !( MCTSHelper::checkMvBufferForMCTSConstraint( pu ) ) ) )
          {
            // Do not use this mode
            tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
            return;
          }
    
    rlliao's avatar
    rlliao committed
          m_pcInterSearch->motionCompensation( pu, triangleBuffer[mergeCand] );
    
    rlliao's avatar
    rlliao committed
      bool tempBufSet = bestIsSkip ? false : true;
      triangleNumMrgSATDCand = bestIsSkip ? TRIANGLE_MAX_NUM_CANDS : TRIANGLE_MAX_NUM_SATD_CANDS;
    
      if( bestIsSkip )
      {
        for( uint8_t i = 0; i < TRIANGLE_MAX_NUM_CANDS; i++ )
        {
    
    rlliao's avatar
    rlliao committed
          triangleRdModeList.push_back(i);
    
        }
      }
      else
      {
        CodingUnit &cu      = tempCS->addCU( tempCS->area, partitioner.chType );
    
        partitioner.setCUData( cu );
        cu.slice            = tempCS->slice;
    
    Karsten Suehring's avatar
    Karsten Suehring committed
        cu.tileIdx          = tempCS->picture->tileMap->getTileIdxMap( tempCS->area.lumaPos() );
    
        cu.skip             = false;
        cu.predMode         = MODE_INTER;
        cu.transQuantBypass = encTestMode.lossless;
        cu.chromaQpAdj      = cu.transQuantBypass ? 0 : m_cuChromaQpOffsetIdxPlus1;
        cu.qp               = encTestMode.qp;
        cu.triangle         = true;
        cu.mmvdSkip         = false;
        cu.GBiIdx           = GBI_DEFAULT;
    
        PredictionUnit &pu  = tempCS->addPU( cu, partitioner.chType );
    
    rlliao's avatar
    rlliao committed
        if( abs(g_aucLog2[cu.lwidth()] - g_aucLog2[cu.lheight()]) >= 2 )
    
    rlliao's avatar
    rlliao committed
          numTriangleCandidate = 30;
    
    rlliao's avatar
    rlliao committed
          numTriangleCandidate = TRIANGLE_MAX_NUM_CANDS;
    
    rlliao's avatar
    rlliao committed
        for( uint8_t mergeCand = 0; mergeCand < numTriangleCandidate; mergeCand++ )
    
          bool    splitDir = m_triangleModeTest[mergeCand].m_splitDir;
          uint8_t candIdx0 = m_triangleModeTest[mergeCand].m_candIdx0;
          uint8_t candIdx1 = m_triangleModeTest[mergeCand].m_candIdx1;
    
          pu.triangleSplitDir = splitDir;
          pu.triangleMergeIdx0 = candIdx0;
          pu.triangleMergeIdx1 = candIdx1;
    
    rlliao's avatar
    rlliao committed
          triangleWeightedBuffer[mergeCand] = m_acTriangleWeightedBuffer[mergeCand].getBuf( localUnitArea );
          triangleBuffer[candIdx0] = m_acMergeBuffer[candIdx0].getBuf( localUnitArea );
          triangleBuffer[candIdx1] = m_acMergeBuffer[candIdx1].getBuf( localUnitArea );
    
          m_pcInterSearch->weightedTriangleBlk( pu, splitDir, CHANNEL_TYPE_LUMA, triangleWeightedBuffer[mergeCand], triangleBuffer[candIdx0], triangleBuffer[candIdx1] );
    
    rlliao's avatar
    rlliao committed
          distParam.cur = triangleWeightedBuffer[mergeCand].Y();
    
    
          Distortion uiSad = distParam.distFunc( distParam );
    
    
          uint32_t uiBitsCand = m_triangleIdxBins[splitDir][candIdx0][candIdx1];
    
    
          double cost = (double)uiSad + (double)uiBitsCand * sqrtLambdaForFirstPass;
    
    
          static_vector<int, TRIANGLE_MAX_NUM_CANDS> * nullList = nullptr;
          updateCandList( mergeCand, cost, triangleRdModeList, tianglecandCostList
            , *nullList, -1
            , triangleNumMrgSATDCand );
    
        // limit number of candidates using SATD-costs
    
    rlliao's avatar
    rlliao committed
        for( uint8_t i = 0; i < triangleNumMrgSATDCand; i++ )
    
    rlliao's avatar
    rlliao committed
          if( tianglecandCostList[i] > MRG_FAST_RATIO * tianglecandCostList[0] || tianglecandCostList[i] > getMergeBestSATDCost() )
    
    rlliao's avatar
    rlliao committed
            triangleNumMrgSATDCand = i;
    
            break;
          }
        }
    
        // perform chroma weighting process
    
    rlliao's avatar
    rlliao committed
        for( uint8_t i = 0; i < triangleNumMrgSATDCand; i++ )
    
    rlliao's avatar
    rlliao committed
          uint8_t  mergeCand = triangleRdModeList[i];
    
          bool     splitDir  = m_triangleModeTest[mergeCand].m_splitDir;
          uint8_t  candIdx0  = m_triangleModeTest[mergeCand].m_candIdx0;
          uint8_t  candIdx1  = m_triangleModeTest[mergeCand].m_candIdx1;
    
          pu.triangleSplitDir = splitDir;
          pu.triangleMergeIdx0 = candIdx0;
          pu.triangleMergeIdx1 = candIdx1;
    
          m_pcInterSearch->weightedTriangleBlk( pu, splitDir, CHANNEL_TYPE_CHROMA, triangleWeightedBuffer[mergeCand], triangleBuffer[candIdx0], triangleBuffer[candIdx1] );
    
        }
    
        tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
      }
    
    Nan Hu's avatar
    Nan Hu committed
      m_bestModeUpdated = tempCS->useDbCost = bestCS->useDbCost = false;
    
        uint8_t iteration;
        uint8_t iterationBegin = m_modeCtrl->getIsHashPerfectMatch() ? 1 : 0;
        if (encTestMode.lossless)
        {
          iteration = 1;
          iterationBegin = 0;
        }
        else
        {
          iteration = 2;
        }
        for (uint8_t noResidualPass = iterationBegin; noResidualPass < iteration; ++noResidualPass)
    
    rlliao's avatar
    rlliao committed
          for( uint8_t mrgHADIdx = 0; mrgHADIdx < triangleNumMrgSATDCand; mrgHADIdx++ )
    
    rlliao's avatar
    rlliao committed
            uint8_t mergeCand = triangleRdModeList[mrgHADIdx];
    
    rlliao's avatar
    rlliao committed
            if ( ( (noResidualPass != 0) && trianglecandHasNoResidual[mergeCand] )
              || ( (noResidualPass == 0) && bestIsSkip ) )
    
            bool    splitDir = m_triangleModeTest[mergeCand].m_splitDir;
            uint8_t candIdx0 = m_triangleModeTest[mergeCand].m_candIdx0;
            uint8_t candIdx1 = m_triangleModeTest[mergeCand].m_candIdx1;
    
    
            CodingUnit &cu = tempCS->addCU(tempCS->area, partitioner.chType);
    
            partitioner.setCUData(cu);
            cu.slice = tempCS->slice;
    
    Karsten Suehring's avatar
    Karsten Suehring committed
            cu.tileIdx          = tempCS->picture->tileMap->getTileIdxMap( tempCS->area.lumaPos() );
    
            cu.skip = false;
            cu.predMode = MODE_INTER;
            cu.transQuantBypass = encTestMode.lossless;
            cu.chromaQpAdj = cu.transQuantBypass ? 0 : m_cuChromaQpOffsetIdxPlus1;
            cu.qp = encTestMode.qp;
            cu.triangle = true;
            cu.mmvdSkip = false;
            cu.GBiIdx   = GBI_DEFAULT;
            PredictionUnit &pu = tempCS->addPU(cu, partitioner.chType);
    
    
            pu.triangleSplitDir = splitDir;
            pu.triangleMergeIdx0 = candIdx0;
            pu.triangleMergeIdx1 = candIdx1;
    
            PU::spanTriangleMotionInfo(pu, triangleMrgCtx, splitDir, candIdx0, candIdx1 );
    
    Valeri George's avatar
    Valeri George committed
            if( m_pcEncCfg->getMCTSEncConstraint() && ( !( MCTSHelper::checkMvBufferForMCTSConstraint( *cu.firstPU ) ) ) )
            {
              // Do not use this mode
              tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
              return;
            }
    
    rlliao's avatar
    rlliao committed
            if( tempBufSet )
    
    rlliao's avatar
    rlliao committed
              tempCS->getPredBuf().copyFrom( triangleWeightedBuffer[mergeCand] );
    
    rlliao's avatar
    rlliao committed
              triangleBuffer[candIdx0] = m_acMergeBuffer[candIdx0].getBuf( localUnitArea );
              triangleBuffer[candIdx1] = m_acMergeBuffer[candIdx1].getBuf( localUnitArea );
    
              PelUnitBuf predBuf         = tempCS->getPredBuf();
    
              m_pcInterSearch->weightedTriangleBlk( pu, splitDir, MAX_NUM_CHANNEL_TYPE, predBuf, triangleBuffer[candIdx0], triangleBuffer[candIdx1] );
    
            xEncodeInterResidual( tempCS, bestCS, partitioner, encTestMode, noResidualPass, ( noResidualPass == 0 ? &trianglecandHasNoResidual[mergeCand] : NULL ) );
    
    
            if (m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip)
            {
              bestIsSkip = bestCS->getCU(partitioner.chType)->rootCbf == 0;
            }
            tempCS->initStructData(encTestMode.qp, encTestMode.lossless);
    
    rlliao's avatar
    rlliao committed
          }// end loop mrgHADIdx
    
    Nan Hu's avatar
    Nan Hu committed
      if ( m_bestModeUpdated && bestCS->cost != MAX_DOUBLE )
    
    Nan Hu's avatar
    Nan Hu committed
      {
    
    Nan Hu's avatar
    Nan Hu committed
        xCalDebCost( *bestCS, partitioner );
    
    Nan Hu's avatar
    Nan Hu committed
      }
    
    void EncCu::xCheckRDCostAffineMerge2Nx2N( CodingStructure *&tempCS, CodingStructure *&bestCS, Partitioner &partitioner, const EncTestMode& encTestMode )
    {
      if( m_modeCtrl->getFastDeltaQp() )
      {
        return;
      }
    
      if ( bestCS->area.lumaSize().width < 8 || bestCS->area.lumaSize().height < 8 )
      {
        return;
      }
    
    Nan Hu's avatar
    Nan Hu committed
      m_bestModeUpdated = tempCS->useDbCost = bestCS->useDbCost = false;
    
      const Slice &slice = *tempCS->slice;
    
      CHECK( slice.getSliceType() == I_SLICE, "Affine Merge modes not available for I-slices" );
    
      tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
    
      AffineMergeCtx affineMergeCtx;
      const SPS &sps = *tempCS->sps;
    
      MergeCtx mrgCtx;
    
      if ( sps.getSBTMVPEnabledFlag() )
    
      {
        Size bufSize = g_miScaling.scale( tempCS->area.lumaSize() );
        mrgCtx.subPuMvpMiBuf = MotionBuf( m_SubPuMiBuf, bufSize );
        affineMergeCtx.mrgCtx = &mrgCtx;
      }
    
      {
        // first get merge candidates
        CodingUnit cu( tempCS->area );
        cu.cs = tempCS;
        cu.predMode = MODE_INTER;
        cu.slice = tempCS->slice;
        cu.tileIdx = tempCS->picture->tileMap->getTileIdxMap( tempCS->area.lumaPos() );
    
    Huanbang Chen's avatar
    Huanbang Chen committed
        cu.mmvdSkip = false;
    
    
        PredictionUnit pu( tempCS->area );
        pu.cu = &cu;
        pu.cs = tempCS;
    
        PU::getAffineMergeCand( pu, affineMergeCtx );
    
        if ( affineMergeCtx.numValidMergeCand <= 0 )
        {
          return;
        }
      }
    
      bool candHasNoResidual[AFFINE_MRG_MAX_NUM_CANDS];
      for ( uint32_t ui = 0; ui < affineMergeCtx.numValidMergeCand; ui++ )
      {
        candHasNoResidual[ui] = false;
      }
    
      bool                                        bestIsSkip = false;
      uint32_t                                    uiNumMrgSATDCand = affineMergeCtx.numValidMergeCand;
      PelUnitBuf                                  acMergeBuffer[AFFINE_MRG_MAX_NUM_CANDS];
      static_vector<uint32_t, AFFINE_MRG_MAX_NUM_CANDS>  RdModeList;
      bool                                        mrgTempBufSet = false;
    
      for ( uint32_t i = 0; i < AFFINE_MRG_MAX_NUM_CANDS; i++ )
      {
        RdModeList.push_back( i );
      }
    
      if ( m_pcEncCfg->getUseFastMerge() )
      {
        uiNumMrgSATDCand = std::min( NUM_AFF_MRG_SATD_CAND, affineMergeCtx.numValidMergeCand );
        bestIsSkip = false;
    
        if ( auto blkCache = dynamic_cast<CacheBlkInfoCtrl*>(m_modeCtrl) )
        {
          bestIsSkip = blkCache->isSkip( tempCS->area );
        }
    
        static_vector<double, AFFINE_MRG_MAX_NUM_CANDS> candCostList;
    
        // 1. Pass: get SATD-cost for selected candidates and reduce their count
        if ( !bestIsSkip )
        {
          RdModeList.clear();
          mrgTempBufSet = true;
          const double sqrtLambdaForFirstPass = m_pcRdCost->getMotionLambda( encTestMode.lossless );
    
          CodingUnit &cu = tempCS->addCU( tempCS->area, partitioner.chType );
    
          partitioner.setCUData( cu );
          cu.slice = tempCS->slice;
          cu.tileIdx = tempCS->picture->tileMap->getTileIdxMap( tempCS->area.lumaPos() );
          cu.skip = false;
          cu.affine = true;
          cu.predMode = MODE_INTER;
          cu.transQuantBypass = encTestMode.lossless;
          cu.chromaQpAdj = cu.transQuantBypass ? 0 : m_cuChromaQpOffsetIdxPlus1;
          cu.qp = encTestMode.qp;
    
          PredictionUnit &pu = tempCS->addPU( cu, partitioner.chType );
    
          DistParam distParam;
          const bool bUseHadamard = !encTestMode.lossless;
          m_pcRdCost->setDistParam( distParam, tempCS->getOrgBuf().Y(), m_acMergeBuffer[0].Y(), sps.getBitDepth( CHANNEL_TYPE_LUMA ), COMPONENT_Y, bUseHadamard );
    
          const UnitArea localUnitArea( tempCS->area.chromaFormat, Area( 0, 0, tempCS->area.Y().width, tempCS->area.Y().height ) );
    
          for ( uint32_t uiMergeCand = 0; uiMergeCand < affineMergeCtx.numValidMergeCand; uiMergeCand++ )
          {
            acMergeBuffer[uiMergeCand] = m_acMergeBuffer[uiMergeCand].getBuf( localUnitArea );
    
            // set merge information
            pu.interDir = affineMergeCtx.interDirNeighbours[uiMergeCand];
            pu.mergeFlag = true;
            pu.mergeIdx = uiMergeCand;
            cu.affineType = affineMergeCtx.affineType[uiMergeCand];
            cu.GBiIdx = affineMergeCtx.GBiIdx[uiMergeCand];
    
            pu.mergeType = affineMergeCtx.mergeType[uiMergeCand];
            if ( pu.mergeType == MRG_TYPE_SUBPU_ATMVP )
            {
              pu.refIdx[0] = affineMergeCtx.mvFieldNeighbours[(uiMergeCand << 1) + 0][0].refIdx;
              pu.refIdx[1] = affineMergeCtx.mvFieldNeighbours[(uiMergeCand << 1) + 1][0].refIdx;
              PU::spanMotionInfo( pu, mrgCtx );
            }
            else
            {
              PU::setAllAffineMvField( pu, affineMergeCtx.mvFieldNeighbours[(uiMergeCand << 1) + 0], REF_PIC_LIST_0 );
              PU::setAllAffineMvField( pu, affineMergeCtx.mvFieldNeighbours[(uiMergeCand << 1) + 1], REF_PIC_LIST_1 );
    
              PU::spanMotionInfo( pu );
            }
    
            distParam.cur = acMergeBuffer[uiMergeCand].Y();
    
            m_pcInterSearch->motionCompensation( pu, acMergeBuffer[uiMergeCand] );
    
            Distortion uiSad = distParam.distFunc( distParam );
            uint32_t   uiBitsCand = uiMergeCand + 1;
            if ( uiMergeCand == tempCS->slice->getMaxNumAffineMergeCand() - 1 )
            {
              uiBitsCand--;
            }
            double cost = (double)uiSad + (double)uiBitsCand * sqrtLambdaForFirstPass;
    
            static_vector<int, AFFINE_MRG_MAX_NUM_CANDS> emptyList;
    
            updateCandList( uiMergeCand, cost, RdModeList, candCostList
    
              , emptyList, -1
    
              , uiNumMrgSATDCand );
    
    
            CHECK( std::min( uiMergeCand + 1, uiNumMrgSATDCand ) != RdModeList.size(), "" );
          }
    
          // Try to limit number of candidates using SATD-costs
          for ( uint32_t i = 1; i < uiNumMrgSATDCand; i++ )
          {
            if ( candCostList[i] > MRG_FAST_RATIO * candCostList[0] )
            {
              uiNumMrgSATDCand = i;
              break;
            }
          }
    
          tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
        }
        else
        {
          uiNumMrgSATDCand = affineMergeCtx.numValidMergeCand;
        }
      }
    
    
      uint32_t iteration;
      uint32_t iterationBegin = m_modeCtrl->getIsHashPerfectMatch() ? 1 : 0;
      if (encTestMode.lossless)
      {
        iteration = 1;
        iterationBegin = 0;
      }
      else
      {
        iteration = 2;
      }
      for (uint32_t uiNoResidualPass = iterationBegin; uiNoResidualPass < iteration; ++uiNoResidualPass)
    
      {
        for ( uint32_t uiMrgHADIdx = 0; uiMrgHADIdx < uiNumMrgSATDCand; uiMrgHADIdx++ )
        {
          uint32_t uiMergeCand = RdModeList[uiMrgHADIdx];
    
          if ( ((uiNoResidualPass != 0) && candHasNoResidual[uiMergeCand])
            || ((uiNoResidualPass == 0) && bestIsSkip) )
          {
            continue;
          }
    
          // first get merge candidates
          CodingUnit &cu = tempCS->addCU( tempCS->area, partitioner.chType );
    
          partitioner.setCUData( cu );
          cu.slice = tempCS->slice;
          cu.tileIdx = tempCS->picture->tileMap->getTileIdxMap( tempCS->area.lumaPos() );
          cu.skip = false;
          cu.affine = true;
          cu.predMode = MODE_INTER;
          cu.transQuantBypass = encTestMode.lossless;
          cu.chromaQpAdj = cu.transQuantBypass ? 0 : m_cuChromaQpOffsetIdxPlus1;
          cu.qp = encTestMode.qp;
          PredictionUnit &pu = tempCS->addPU( cu, partitioner.chType );
    
          // set merge information
          pu.mergeFlag = true;
          pu.mergeIdx = uiMergeCand;
          pu.interDir = affineMergeCtx.interDirNeighbours[uiMergeCand];
          cu.affineType = affineMergeCtx.affineType[uiMergeCand];
          cu.GBiIdx = affineMergeCtx.GBiIdx[uiMergeCand];
    
          pu.mergeType = affineMergeCtx.mergeType[uiMergeCand];
          if ( pu.mergeType == MRG_TYPE_SUBPU_ATMVP )
          {
            pu.refIdx[0] = affineMergeCtx.mvFieldNeighbours[(uiMergeCand << 1) + 0][0].refIdx;
            pu.refIdx[1] = affineMergeCtx.mvFieldNeighbours[(uiMergeCand << 1) + 1][0].refIdx;
            PU::spanMotionInfo( pu, mrgCtx );
          }
          else
          {
            PU::setAllAffineMvField( pu, affineMergeCtx.mvFieldNeighbours[(uiMergeCand << 1) + 0], REF_PIC_LIST_0 );
            PU::setAllAffineMvField( pu, affineMergeCtx.mvFieldNeighbours[(uiMergeCand << 1) + 1], REF_PIC_LIST_1 );
    
            PU::spanMotionInfo( pu );
          }
    
    
    Valeri George's avatar
    Valeri George committed
          if( m_pcEncCfg->getMCTSEncConstraint() && ( !( MCTSHelper::checkMvBufferForMCTSConstraint( *cu.firstPU ) ) ) )
          {
            // Do not use this mode
            tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
            return;
          }
    
          if ( mrgTempBufSet )
          {
            tempCS->getPredBuf().copyFrom( acMergeBuffer[uiMergeCand] );
          }
          else
          {
            m_pcInterSearch->motionCompensation( pu );
          }
    
    
          xEncodeInterResidual( tempCS, bestCS, partitioner, encTestMode, uiNoResidualPass, ( uiNoResidualPass == 0 ? &candHasNoResidual[uiMergeCand] : NULL ) );
    
    
          if ( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
          {
            bestIsSkip = bestCS->getCU( partitioner.chType )->rootCbf == 0;
          }
          tempCS->initStructData( encTestMode.qp, encTestMode.lossless );
        }// end loop uiMrgHADIdx
    
        if ( uiNoResidualPass == 0 && m_pcEncCfg->getUseEarlySkipDetection() )
        {
          const CodingUnit     &bestCU = *bestCS->getCU( partitioner.chType );
          const PredictionUnit &bestPU = *bestCS->getPU( partitioner.chType );
    
          if ( bestCU.rootCbf == 0 )
          {
            if ( bestPU.mergeFlag )
            {
              m_modeCtrl->setEarlySkipDetected();
            }
            else if ( m_pcEncCfg->getMotionEstimationSearchMethod() != MESEARCH_SELECTIVE )
            {
              int absolute_MV = 0;
    
              for ( uint32_t uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
              {
                if ( slice.getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
                {
                  absolute_MV += bestPU.mvd[uiRefListIdx].getAbsHor() + bestPU.mvd[uiRefListIdx].getAbsVer();
                }
              }
    
              if ( absolute_MV == 0 )
              {
                m_modeCtrl->setEarlySkipDetected();
              }
            }
          }
        }
      }
    
    Nan Hu's avatar
    Nan Hu committed
      if ( m_bestModeUpdated && bestCS->cost != MAX_DOUBLE )
      {
        xCalDebCost( *bestCS, partitioner );
      }
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
    //////////////////////////////////////////////////////////////////////////////////////////////
    
    Yu Han's avatar
    Yu Han committed
    // ibc merge/skip mode check
    void EncCu::xCheckRDCostIBCModeMerge2Nx2N(CodingStructure *&tempCS, CodingStructure *&bestCS, Partitioner &partitioner, const EncTestMode& encTestMode)
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
    {
    
    Yu Han's avatar
    Yu Han committed
      assert(tempCS->chType != CHANNEL_TYPE_CHROMA); // chroma IBC is derived
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
    
    
    Yu Han's avatar
    Yu Han committed
      if (tempCS->area.lwidth() > IBC_MAX_CAND_SIZE || tempCS->area.lheight() > IBC_MAX_CAND_SIZE) // currently only check 32x32 and below block for ibc merge/skip
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      {
        return;
      }
      const SPS &sps = *tempCS->sps;
    
      tempCS->initStructData(encTestMode.qp, encTestMode.lossless);
      MergeCtx mergeCtx;
    
    
    
      if (sps.getSBTMVPEnabledFlag())
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      {
        Size bufSize = g_miScaling.scale(tempCS->area.lumaSize());
        mergeCtx.subPuMvpMiBuf = MotionBuf(m_SubPuMiBuf, bufSize);
      }
    
      {
        // first get merge candidates
        CodingUnit cu(tempCS->area);
        cu.cs = tempCS;
    
    Yu Han's avatar
    Yu Han committed
        cu.predMode = MODE_IBC;
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
        cu.slice = tempCS->slice;
        cu.tileIdx = tempCS->picture->tileMap->getTileIdxMap(tempCS->area.lumaPos());
        PredictionUnit pu(tempCS->area);
        pu.cu = &cu;
        pu.cs = tempCS;
        cu.mmvdSkip = false;
        pu.mmvdMergeFlag = false;
        cu.triangle = false;
    
        pu.shareParentPos = tempCS->sharedBndPos;
        pu.shareParentSize = tempCS->sharedBndSize;
    
    Yu Han's avatar
    Yu Han committed
        PU::getIBCMergeCandidates(pu, mergeCtx);
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      }
    
      int candHasNoResidual[MRG_MAX_NUM_CANDS];
      for (unsigned int ui = 0; ui < mergeCtx.numValidMergeCand; ui++)
      {
        candHasNoResidual[ui] = 0;
      }
    
      bool                                        bestIsSkip = false;
      unsigned                                    numMrgSATDCand = mergeCtx.numValidMergeCand;
      static_vector<unsigned, MRG_MAX_NUM_CANDS>  RdModeList(MRG_MAX_NUM_CANDS);
      for (unsigned i = 0; i < MRG_MAX_NUM_CANDS; i++)
      {
        RdModeList[i] = i;
      }
    
      //{
        static_vector<double, MRG_MAX_NUM_CANDS>  candCostList(MRG_MAX_NUM_CANDS, MAX_DOUBLE);
        // 1. Pass: get SATD-cost for selected candidates and reduce their count
        {
          const double sqrtLambdaForFirstPass = m_pcRdCost->getMotionLambda(encTestMode.lossless);
    
          CodingUnit &cu = tempCS->addCU(CS::getArea(*tempCS, tempCS->area, (const ChannelType)partitioner.chType), (const ChannelType)partitioner.chType);
    
          partitioner.setCUData(cu);
          cu.slice = tempCS->slice;
          cu.tileIdx = tempCS->picture->tileMap->getTileIdxMap(tempCS->area.lumaPos());
          cu.skip = false;
    
    Yu Han's avatar
    Yu Han committed
          cu.predMode = MODE_IBC;
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
          cu.transQuantBypass = encTestMode.lossless;
          cu.chromaQpAdj = cu.transQuantBypass ? 0 : m_cuChromaQpOffsetIdxPlus1;
          cu.qp = encTestMode.qp;
          cu.mmvdSkip = false;
          cu.triangle = false;
          DistParam distParam;
          const bool bUseHadamard = !encTestMode.lossless;
          PredictionUnit &pu = tempCS->addPU(cu, partitioner.chType); //tempCS->addPU(cu);
          pu.mmvdMergeFlag = false;
          Picture* refPic = pu.cu->slice->getPic();
          const CPelBuf refBuf = refPic->getRecoBuf(pu.blocks[COMPONENT_Y]);
          const Pel*        piRefSrch = refBuf.buf;
    
    Taoran Lu's avatar
    Taoran Lu committed
          if (tempCS->slice->getReshapeInfo().getUseSliceReshaper() && m_pcReshape->getCTUFlag())
          {
            const CompArea &area = cu.blocks[COMPONENT_Y];
            CompArea    tmpArea(COMPONENT_Y, area.chromaFormat, Position(0, 0), area.size());
            PelBuf tmpLuma = m_tmpStorageLCU->getBuf(tmpArea);
            tmpLuma.copyFrom(tempCS->getOrgBuf().Y());
            tmpLuma.rspSignal(m_pcReshape->getFwdLUT());
            m_pcRdCost->setDistParam(distParam, tmpLuma, refBuf, sps.getBitDepth(CHANNEL_TYPE_LUMA), COMPONENT_Y, bUseHadamard);
          }
          else
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
          m_pcRdCost->setDistParam(distParam, tempCS->getOrgBuf().Y(), refBuf, sps.getBitDepth(CHANNEL_TYPE_LUMA), COMPONENT_Y, bUseHadamard);
          int refStride = refBuf.stride;
          const UnitArea localUnitArea(tempCS->area.chromaFormat, Area(0, 0, tempCS->area.Y().width, tempCS->area.Y().height));
          int numValidBv = mergeCtx.numValidMergeCand;
          for (unsigned int mergeCand = 0; mergeCand < mergeCtx.numValidMergeCand; mergeCand++)
          {
            mergeCtx.setMergeInfo(pu, mergeCand); // set bv info in merge mode
            const int cuPelX = pu.Y().x;
            const int cuPelY = pu.Y().y;
            int roiWidth = pu.lwidth();
            int roiHeight = pu.lheight();
            const int picWidth = pu.cs->slice->getSPS()->getPicWidthInLumaSamples();
            const int picHeight = pu.cs->slice->getSPS()->getPicHeightInLumaSamples();
            const unsigned int  lcuWidth = pu.cs->slice->getSPS()->getMaxCUWidth();
            int xPred = pu.bv.getHor();
            int yPred = pu.bv.getVer();
    
            if (!PU::isBlockVectorValid(pu, cuPelX, cuPelY, roiWidth, roiHeight, picWidth, picHeight, 0, 0, xPred, yPred, lcuWidth)) // not valid bv derived
            {
              numValidBv--;
              continue;
            }
            PU::spanMotionInfo(pu, mergeCtx);
    
            distParam.cur.buf = piRefSrch + refStride * yPred + xPred;
    
            Distortion sad = distParam.distFunc(distParam);
            unsigned int bitsCand = mergeCand + 1;
            if (mergeCand == tempCS->slice->getMaxNumMergeCand() - 1)
            {
              bitsCand--;
            }
            double cost = (double)sad + (double)bitsCand * sqrtLambdaForFirstPass;
            static_vector<int, MRG_MAX_NUM_CANDS> * nullList = nullptr;
    
            updateCandList(mergeCand, cost, RdModeList, candCostList
              , *nullList, -1
             , numMrgSATDCand);
          }
    
          // Try to limit number of candidates using SATD-costs
          if (numValidBv)
          {
            numMrgSATDCand = numValidBv;
            for (unsigned int i = 1; i < numValidBv; i++)
            {
              if (candCostList[i] > MRG_FAST_RATIO*candCostList[0])
              {
                numMrgSATDCand = i;
                break;
              }
            }
          }
          else
          {
            tempCS->dist = 0;
            tempCS->fracBits = 0;
            tempCS->cost = MAX_DOUBLE;
    
    Nan Hu's avatar
    Nan Hu committed
            tempCS->costDbOffset = 0;
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
            tempCS->initStructData(encTestMode.qp, encTestMode.lossless);
            return;
          }
    
          tempCS->initStructData(encTestMode.qp, encTestMode.lossless);
        }
      //}
    
    
      const unsigned int iteration = encTestMode.lossless ? 1 : 2;
    
    Nan Hu's avatar
    Nan Hu committed
      m_bestModeUpdated = tempCS->useDbCost = bestCS->useDbCost = false;
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      // 2. Pass: check candidates using full RD test
      for (unsigned int numResidualPass = 0; numResidualPass < iteration; numResidualPass++)
      {
        for (unsigned int mrgHADIdx = 0; mrgHADIdx < numMrgSATDCand; mrgHADIdx++)
        {
          unsigned int mergeCand = RdModeList[mrgHADIdx];
          if (!(numResidualPass == 1 && candHasNoResidual[mergeCand] == 1))
          {
            if (!(bestIsSkip && (numResidualPass == 0)))
            {
    
    Tung Nguyen's avatar
    Tung Nguyen committed
              {
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
    
                // first get merge candidates
                CodingUnit &cu = tempCS->addCU(CS::getArea(*tempCS, tempCS->area, (const ChannelType)partitioner.chType), (const ChannelType)partitioner.chType);
    
                partitioner.setCUData(cu);
                cu.slice = tempCS->slice;
                cu.tileIdx = tempCS->picture->tileMap->getTileIdxMap(tempCS->area.lumaPos());
                cu.skip = false;
    
    Yu Han's avatar
    Yu Han committed
                cu.predMode = MODE_IBC;
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
                cu.transQuantBypass = encTestMode.lossless;
                cu.chromaQpAdj = cu.transQuantBypass ? 0 : m_cuChromaQpOffsetIdxPlus1;
                cu.qp = encTestMode.qp;
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed