Skip to content
Snippets Groups Projects
UnitTools.cpp 145 KiB
Newer Older
  • Learn to ignore specific revisions
  • Yu Han's avatar
    Yu Han committed
        mrgCtx.mvFieldNeighbours[ui * 2 + 1].refIdx = NOT_VALID;
    
    Yu Han's avatar
    Yu Han committed
      }
    
      mrgCtx.numValidMergeCand = maxNumMergeCand;
      // compute the location of the current PU
    
      int cnt = 0;
    
    
      const Position posRT = pu.shareParentPos.offset(pu.shareParentSize.width - 1, 0);
      const Position posLB = pu.shareParentPos.offset(0, pu.shareParentSize.height - 1);
    
    Yu Han's avatar
    Yu Han committed
    
      MotionInfo miAbove, miLeft, miAboveLeft, miAboveRight, miBelowLeft;
    
      //left
      const PredictionUnit* puLeft = cs.getPURestricted(posLB.offset(-1, 0), pu, pu.chType);
      const bool isAvailableA1 = puLeft && isDiffMER(pu, *puLeft) && pu.cu != puLeft->cu && CU::isIBC(*puLeft->cu);
      if (isAvailableA1)
      {
        miLeft = puLeft->getMotionInfo(posLB.offset(-1, 0));
    
        // get Inter Dir
        mrgCtx.interDirNeighbours[cnt] = miLeft.interDir;
        // get Mv from Left
        mrgCtx.mvFieldNeighbours[cnt << 1].setMvField(miLeft.mv[0], miLeft.refIdx[0]);
        if (mrgCandIdx == cnt && canFastExit)
        {
          return;
        }
        cnt++;
      }
    
      // early termination
      if (cnt == maxNumMergeCand)
      {
        return;
      }
    
    
      // above
      const PredictionUnit *puAbove = cs.getPURestricted(posRT.offset(0, -1), pu, pu.chType);
      bool isAvailableB1 = puAbove && isDiffMER(pu, *puAbove) && pu.cu != puAbove->cu && CU::isIBC(*puAbove->cu);
      if (isAvailableB1)
      {
        miAbove = puAbove->getMotionInfo(posRT.offset(0, -1));
    
        if (!isAvailableA1 || (miAbove != miLeft))
        {
          // get Inter Dir
          mrgCtx.interDirNeighbours[cnt] = miAbove.interDir;
          // get Mv from Above
          mrgCtx.mvFieldNeighbours[cnt << 1].setMvField(miAbove.mv[0], miAbove.refIdx[0]);
          if (mrgCandIdx == cnt && canFastExit)
          {
            return;
          }
    
          cnt++;
        }
      }
    
      // early termination
      if (cnt == maxNumMergeCand)
      {
        return;
      }
    
    
    Yu Han's avatar
    Yu Han committed
      int spatialCandPos = cnt;
    
    
    Yu Han's avatar
    Yu Han committed
      int maxNumMergeCandMin1 = maxNumMergeCand;
    
    Yu Han's avatar
    Yu Han committed
      if (cnt != maxNumMergeCandMin1)
      {
        bool isAvailableSubPu = false;
        unsigned subPuMvpPos = 0;
    
    
        bool  isShared = ((pu.Y().lumaSize().width != pu.shareParentSize.width) || (pu.Y().lumaSize().height != pu.shareParentSize.height));
    
    
    Yu Han's avatar
    Yu Han committed
    #if JVET_L0090_PAIR_AVG
    
        bool bFound = addMergeHMVPCand(cs, mrgCtx, canFastExit
    
    Yu Han's avatar
    Yu Han committed
          , mrgCandIdx
    
    Yu Han's avatar
    Yu Han committed
          , maxNumMergeCandMin1, cnt
          , spatialCandPos
          , isAvailableSubPu, subPuMvpPos
    
    Yu Han's avatar
    Yu Han committed
          , true
    
    Yu Han's avatar
    Yu Han committed
        );
    #else
        bool bFound = addMergeHMVPCand(slice, mrgCtx, isCandInter, canFastExit
          , mrgCandIdx
          , maxNumMergeCandMin1, cnt, cnt, isAvailableSubPu, subPuMvpPos
        );
    #endif
        if (bFound)
        {
          return;
        }
      }
    
    
    
    Yu Han's avatar
    Yu Han committed
        while (cnt < maxNumMergeCand)
        {
          mrgCtx.mvFieldNeighbours[cnt * 2].setMvField(Mv(0, 0), MAX_NUM_REF);
          mrgCtx.interDirNeighbours[cnt] = 1;
          cnt++;
          if (mrgCandIdx == cnt && canFastExit)
          {
            return;
          }
        }
    
    
    Yu Han's avatar
    Yu Han committed
      mrgCtx.numValidMergeCand = cnt;
    
    }
    
    
    void PU::getInterMergeCandidates( const PredictionUnit &pu, MergeCtx& mrgCtx,
    
                                     int mmvdList,
                                     const int& mrgCandIdx )
    
    {
      const CodingStructure &cs  = *pu.cs;
      const Slice &slice         = *pu.cs->slice;
      const uint32_t maxNumMergeCand = slice.getMaxNumMergeCand();
      const bool canFastExit     = pu.cs->pps->getLog2ParallelMergeLevelMinus2() == 0;
    
    
    #if !JVET_L0090_PAIR_AVG
      // this variable is unused if remove HEVC combined candidates
    
    Yu-Chi Su's avatar
    Yu-Chi Su committed
    #endif
    
        mrgCtx.GBiIdx[ui] = GBI_DEFAULT;
    
        mrgCtx.interDirNeighbours[ui] = 0;
        mrgCtx.mrgTypeNeighbours [ui] = MRG_TYPE_DEFAULT_N;
        mrgCtx.mvFieldNeighbours[(ui << 1)    ].refIdx = NOT_VALID;
        mrgCtx.mvFieldNeighbours[(ui << 1) + 1].refIdx = NOT_VALID;
      }
    
      mrgCtx.numValidMergeCand = maxNumMergeCand;
      // compute the location of the current PU
    
      int cnt = 0;
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
    
    
      const Position posLT = pu.Y().topLeft();
      const Position posRT = pu.Y().topRight();
      const Position posLB = pu.Y().bottomLeft();
    
      MotionInfo miAbove, miLeft, miAboveLeft, miAboveRight, miBelowLeft;
    
      //left
      const PredictionUnit* puLeft = cs.getPURestricted( posLB.offset( -1, 0 ), pu, pu.chType );
    
      const bool isAvailableA1 = puLeft && isDiffMER( pu, *puLeft ) && pu.cu != puLeft->cu && CU::isInter( *puLeft->cu );
    
      if( isAvailableA1 )
      {
        miLeft = puLeft->getMotionInfo( posLB.offset(-1, 0) );
    
    
    
        // get Inter Dir
        mrgCtx.interDirNeighbours[cnt] = miLeft.interDir;
    
        mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? puLeft->cu->GBiIdx : GBI_DEFAULT;
    
        // get Mv from Left
        mrgCtx.mvFieldNeighbours[cnt << 1].setMvField(miLeft.mv[0], miLeft.refIdx[0]);
    
        if (slice.isInterB())
        {
          mrgCtx.mvFieldNeighbours[(cnt << 1) + 1].setMvField(miLeft.mv[1], miLeft.refIdx[1]);
        }
    
    Yu Han's avatar
    Yu Han committed
        if (mrgCandIdx == cnt && canFastExit)
    
        {
          return;
        }
    
        cnt++;
      }
    
      // early termination
      if (cnt == maxNumMergeCand)
      {
        return;
      }
    
    
      // above
      const PredictionUnit *puAbove = cs.getPURestricted( posRT.offset( 0, -1 ), pu, pu.chType );
    
      bool isAvailableB1 = puAbove && isDiffMER( pu, *puAbove ) && pu.cu != puAbove->cu && CU::isInter( *puAbove->cu );
    
      if( isAvailableB1 )
      {
        miAbove = puAbove->getMotionInfo( posRT.offset( 0, -1 ) );
    
        if( !isAvailableA1 || ( miAbove != miLeft ) )
        {
    
    
          // get Inter Dir
          mrgCtx.interDirNeighbours[cnt] = miAbove.interDir;
    
          mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? puAbove->cu->GBiIdx : GBI_DEFAULT;
    
          mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miAbove.mv[0], miAbove.refIdx[0] );
    
    
          if( slice.isInterB() )
          {
            mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miAbove.mv[1], miAbove.refIdx[1] );
          }
    
    Yu Han's avatar
    Yu Han committed
          if (mrgCandIdx == cnt && canFastExit)
    
    Yu Han's avatar
    Yu Han committed
      int spatialCandPos = cnt;
    
    
      // above right
      const PredictionUnit *puAboveRight = cs.getPURestricted( posRT.offset( 1, -1 ), pu, pu.chType );
    
      bool isAvailableB0 = puAboveRight && isDiffMER( pu, *puAboveRight ) && CU::isInter( *puAboveRight->cu );
    
      if( isAvailableB0 )
      {
        miAboveRight = puAboveRight->getMotionInfo( posRT.offset( 1, -1 ) );
    
        if( !isAvailableB1 || ( miAbove != miAboveRight ) )
        {
    
    
          // get Inter Dir
          mrgCtx.interDirNeighbours[cnt] = miAboveRight.interDir;
    
          mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? puAboveRight->cu->GBiIdx : GBI_DEFAULT;
    
          mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miAboveRight.mv[0], miAboveRight.refIdx[0] );
    
    
          if( slice.isInterB() )
          {
            mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miAboveRight.mv[1], miAboveRight.refIdx[1] );
          }
    
    
    Yu Han's avatar
    Yu Han committed
          if (mrgCandIdx == cnt && canFastExit)
    
          {
            return;
          }
    
          cnt++;
        }
      }
      // early termination
      if( cnt == maxNumMergeCand )
      {
        return;
      }
    
      //left bottom
      const PredictionUnit *puLeftBottom = cs.getPURestricted( posLB.offset( -1, 1 ), pu, pu.chType );
    
      bool isAvailableA0 = puLeftBottom && isDiffMER( pu, *puLeftBottom ) && CU::isInter( *puLeftBottom->cu );
    
      if( isAvailableA0 )
      {
        miBelowLeft = puLeftBottom->getMotionInfo( posLB.offset( -1, 1 ) );
    
        if( !isAvailableA1 || ( miBelowLeft != miLeft ) )
        {
    
    
          // get Inter Dir
          mrgCtx.interDirNeighbours[cnt] = miBelowLeft.interDir;
    
          mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? puLeftBottom->cu->GBiIdx : GBI_DEFAULT;
    
          // get Mv from Bottom-Left
          mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miBelowLeft.mv[0], miBelowLeft.refIdx[0] );
    
          if( slice.isInterB() )
          {
            mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miBelowLeft.mv[1], miBelowLeft.refIdx[1] );
          }
    
    
    Yu Han's avatar
    Yu Han committed
          if (mrgCandIdx == cnt && canFastExit)
    
      {
        const PredictionUnit *puAboveLeft = cs.getPURestricted( posLT.offset( -1, -1 ), pu, pu.chType );
    
        bool isAvailableB2 = puAboveLeft && isDiffMER( pu, *puAboveLeft ) && CU::isInter( *puAboveLeft->cu );
    
        if( isAvailableB2 )
        {
          miAboveLeft = puAboveLeft->getMotionInfo( posLT.offset( -1, -1 ) );
    
          if( ( !isAvailableA1 || ( miLeft != miAboveLeft ) ) && ( !isAvailableB1 || ( miAbove != miAboveLeft ) ) )
          {
    
    
            // get Inter Dir
            mrgCtx.interDirNeighbours[cnt] = miAboveLeft.interDir;
    
            mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? puAboveLeft->cu->GBiIdx : GBI_DEFAULT;
    
            // get Mv from Above-Left
            mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miAboveLeft.mv[0], miAboveLeft.refIdx[0] );
    
            if( slice.isInterB() )
            {
              mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miAboveLeft.mv[1], miAboveLeft.refIdx[1] );
            }
    
    
    Yu Han's avatar
    Yu Han committed
            if (mrgCandIdx == cnt && canFastExit)
    
    wsh's avatar
    wsh committed
      if (slice.getEnableTMVPFlag() && (pu.lumaSize().width + pu.lumaSize().height > 12))
    
      {
        //>> MTK colocated-RightBottom
        // offset the pos to be sure to "point" to the same position the uiAbsPartIdx would've pointed to
    
        Position posRB = pu.Y().bottomRight().offset( -3, -3 );
    
        bool C0Avail = false;
        if (((posRB.x + pcv.minCUWidth) < pcv.lumaWidth) && ((posRB.y + pcv.minCUHeight) < pcv.lumaHeight))
        {
    
          int posYInCtu = posRB.y & pcv.maxCUHeightMask;
          if (posYInCtu + 4 < pcv.maxCUHeight)
    
            posC0 = posRB.offset(4, 4);
            C0Avail = true;
    
          }
        }
    
        Mv        cColMv;
        int       iRefIdx     = 0;
        int       dir         = 0;
        unsigned  uiArrayAddr = cnt;
        bool      bExistMV    = ( C0Avail && getColocatedMVP(pu, REF_PIC_LIST_0, posC0, cColMv, iRefIdx ) )
    
                                  || getColocatedMVP( pu, REF_PIC_LIST_0, posC1, cColMv, iRefIdx );
    
        if (bExistMV)
        {
          dir     |= 1;
          mrgCtx.mvFieldNeighbours[2 * uiArrayAddr].setMvField(cColMv, iRefIdx);
        }
    
        if (slice.isInterB())
        {
          bExistMV = ( C0Avail && getColocatedMVP(pu, REF_PIC_LIST_1, posC0, cColMv, iRefIdx ) )
    
                       || getColocatedMVP( pu, REF_PIC_LIST_1, posC1, cColMv, iRefIdx );
    
          if (bExistMV)
          {
            dir     |= 2;
            mrgCtx.mvFieldNeighbours[2 * uiArrayAddr + 1].setMvField(cColMv, iRefIdx);
          }
        }
    
        if( dir != 0 )
        {
    
          if( addTMvp )
          {
            mrgCtx.interDirNeighbours[uiArrayAddr] = dir;
    
            mrgCtx.GBiIdx[uiArrayAddr] = GBI_DEFAULT;
    
    Yu Han's avatar
    Yu Han committed
            if (mrgCandIdx == cnt && canFastExit)
    
      int maxNumMergeCandMin1 = maxNumMergeCand - 1;
      if (cnt != maxNumMergeCandMin1)
      {
    
        bool isAvailableSubPu = false;
        unsigned subPuMvpPos = 0;
    
    #if JVET_L0090_PAIR_AVG
    
        bool bFound = addMergeHMVPCand(cs, mrgCtx, canFastExit
    
    Yu Han's avatar
    Yu Han committed
          , mrgCandIdx
    
    Yu Han's avatar
    Yu Han committed
          , maxNumMergeCandMin1, cnt
          , spatialCandPos
          , isAvailableSubPu, subPuMvpPos
    
    Yu Han's avatar
    Yu Han committed
          , CU::isIBC(*pu.cu)
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
        );
    #else
        bool bFound = addMergeHMVPCand(slice, mrgCtx, isCandInter, canFastExit
    
    Yu Han's avatar
    Yu Han committed
          , (mmvdList != 0 && mrgCandIdx != -1) ? (const int)mrgCandIdxIBC : mrgCandIdx
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
          , maxNumMergeCandMin1, cnt, cnt, isAvailableSubPu, subPuMvpPos
          , mmvdList
        );
    
    Yu Han's avatar
    Yu Han committed
        if (cnt > 1 && cnt < maxNumMergeCand)
    
        {
    
          mrgCtx.mvFieldNeighbours[cnt * 2].setMvField( Mv( 0, 0 ), NOT_VALID );
          mrgCtx.mvFieldNeighbours[cnt * 2 + 1].setMvField( Mv( 0, 0 ), NOT_VALID );
          // calculate average MV for L0 and L1 seperately
          unsigned char interDir = 0;
    
          for( int refListId = 0; refListId < (slice.isInterB() ? 2 : 1); refListId++ )
          {
    
            const short refIdxI = mrgCtx.mvFieldNeighbours[0 * 2 + refListId].refIdx;
            const short refIdxJ = mrgCtx.mvFieldNeighbours[1 * 2 + refListId].refIdx;
    
    
            // both MVs are invalid, skip
            if( (refIdxI == NOT_VALID) && (refIdxJ == NOT_VALID) )
            {
              continue;
            }
    
            interDir += 1 << refListId;
            // both MVs are valid, average these two MVs
            if( (refIdxI != NOT_VALID) && (refIdxJ != NOT_VALID) )
            {
    
              const Mv& MvI = mrgCtx.mvFieldNeighbours[0 * 2 + refListId].mv;
              const Mv& MvJ = mrgCtx.mvFieldNeighbours[1 * 2 + refListId].mv;
    
              roundAffineMv(avgMv.hor, avgMv.ver, 1);
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
    
    
              mrgCtx.mvFieldNeighbours[cnt * 2 + refListId].setMvField( avgMv, refIdxI );
            }
            // only one MV is valid, take the only one MV
            else if( refIdxI != NOT_VALID )
            {
    
              Mv singleMv = mrgCtx.mvFieldNeighbours[0 * 2 + refListId].mv;
    
              mrgCtx.mvFieldNeighbours[cnt * 2 + refListId].setMvField( singleMv, refIdxI );
            }
            else if( refIdxJ != NOT_VALID )
            {
    
              Mv singleMv = mrgCtx.mvFieldNeighbours[1 * 2 + refListId].mv;
    
              mrgCtx.mvFieldNeighbours[cnt * 2 + refListId].setMvField( singleMv, refIdxJ );
            }
          }
    
          mrgCtx.interDirNeighbours[cnt] = interDir;
          if( interDir > 0 )
          {
            cnt++;
          }
        }
    
        // early termination
        if( cnt == maxNumMergeCand )
        {
          return;
        }
      }
    #endif
    
    
      uint32_t uiCutoff    = std::min( uiArrayAddr, 3u );
    
      if (slice.isInterB())
      {
        static const uint32_t NUM_PRIORITY_LIST = 12;
        static const uint32_t uiPriorityList0[NUM_PRIORITY_LIST] = { 0 , 1, 0, 2, 1, 2, 0, 3, 1, 3, 2, 3 };
        static const uint32_t uiPriorityList1[NUM_PRIORITY_LIST] = { 1 , 0, 2, 0, 2, 1, 3, 0, 3, 1, 3, 2 };
    
        for (int idx = 0; idx < uiCutoff * (uiCutoff - 1) && uiArrayAddr != maxNumMergeCand; idx++)
        {
          CHECK( idx >= NUM_PRIORITY_LIST, "Invalid priority list number" );
          int i = uiPriorityList0[idx];
          int j = uiPriorityList1[idx];
          if (isCandInter[i] && isCandInter[j] && (mrgCtx.interDirNeighbours[i] & 0x1) && (mrgCtx.interDirNeighbours[j] & 0x2))
          {
            isCandInter[uiArrayAddr] = true;
            mrgCtx.interDirNeighbours[uiArrayAddr] = 3;
    
            mrgCtx.GBiIdx[uiArrayAddr] = ((mrgCtx.interDirNeighbours[uiArrayAddr] == 3)) ? CU::deriveGbiIdx(mrgCtx.GBiIdx[i], mrgCtx.GBiIdx[j]) : GBI_DEFAULT;
    
    
            // get Mv from cand[i] and cand[j]
            mrgCtx.mvFieldNeighbours[ uiArrayAddr << 1     ].setMvField(mrgCtx.mvFieldNeighbours[ i << 1     ].mv, mrgCtx.mvFieldNeighbours[ i << 1     ].refIdx);
            mrgCtx.mvFieldNeighbours[(uiArrayAddr << 1) + 1].setMvField(mrgCtx.mvFieldNeighbours[(j << 1) + 1].mv, mrgCtx.mvFieldNeighbours[(j << 1) + 1].refIdx);
    
            int iRefPOCL0 = slice.getRefPOC(REF_PIC_LIST_0, mrgCtx.mvFieldNeighbours[(uiArrayAddr << 1)    ].refIdx);
            int iRefPOCL1 = slice.getRefPOC(REF_PIC_LIST_1, mrgCtx.mvFieldNeighbours[(uiArrayAddr << 1) + 1].refIdx);
    
            if( iRefPOCL0 == iRefPOCL1 && mrgCtx.mvFieldNeighbours[( uiArrayAddr << 1 )].mv == mrgCtx.mvFieldNeighbours[( uiArrayAddr << 1 ) + 1].mv )
            {
              isCandInter[uiArrayAddr] = false;
            }
            else
            {
              uiArrayAddr++;
            }
          }
        }
      }
    
      // early termination
      if (uiArrayAddr == maxNumMergeCand)
      {
        return;
      }
    
    
      int iNumRefIdx = slice.isInterB() ? std::min(slice.getNumRefIdx(REF_PIC_LIST_0), slice.getNumRefIdx(REF_PIC_LIST_1)) : slice.getNumRefIdx(REF_PIC_LIST_0);
    
      int r = 0;
      int refcnt = 0;
      while (uiArrayAddr < maxNumMergeCand)
      {
    
        mrgCtx.interDirNeighbours [uiArrayAddr     ] = 1;
    
        mrgCtx.GBiIdx             [uiArrayAddr     ] = GBI_DEFAULT;
    
        mrgCtx.mvFieldNeighbours  [uiArrayAddr << 1].setMvField(Mv(0, 0), r);
    
        if (slice.isInterB())
        {
          mrgCtx.interDirNeighbours [ uiArrayAddr          ] = 3;
          mrgCtx.mvFieldNeighbours  [(uiArrayAddr << 1) + 1].setMvField(Mv(0, 0), r);
        }
    
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
        if ( mrgCtx.interDirNeighbours[uiArrayAddr] == 1 && pu.cs->slice->getRefPic(REF_PIC_LIST_0, mrgCtx.mvFieldNeighbours[uiArrayAddr << 1].refIdx)->getPOC() == pu.cs->slice->getPOC())
        {
    
    Yu Han's avatar
    Yu Han committed
          mrgCtx.mrgTypeNeighbours[uiArrayAddr] = MRG_TYPE_IBC;
    
        uiArrayAddr++;
    
        if (refcnt == iNumRefIdx - 1)
        {
          r = 0;
        }
        else
        {
          ++r;
          ++refcnt;
        }
      }
      mrgCtx.numValidMergeCand = uiArrayAddr;
    }
    
    bool PU::checkDMVRCondition(const PredictionUnit& pu)
    {
    
      WPScalingParam *wp0;
      WPScalingParam *wp1;
      int refIdx0 = pu.refIdx[REF_PIC_LIST_0];
      int refIdx1 = pu.refIdx[REF_PIC_LIST_1];
      pu.cu->slice->getWpScaling(REF_PIC_LIST_0, refIdx0, wp0);
      pu.cu->slice->getWpScaling(REF_PIC_LIST_1, refIdx1, wp1);
    #if JVET_O1140_SLICE_DISABLE_BDOF_DMVR_FLAG
      if (pu.cs->sps->getUseDMVR() && (!pu.cs->slice->getDisBdofDmvrFlag()))
    #else
    
      if (pu.cs->sps->getUseDMVR())
    
      {
        return pu.mergeFlag
          && pu.mergeType == MRG_TYPE_DEFAULT_N
    
    #if JVET_O0108_DIS_DMVR_BDOF_CIIP
          && !pu.mhIntraFlag
    #endif
    
          && !pu.cu->affine
          && !pu.mmvdMergeFlag
          && !pu.cu->mmvdSkip
          && PU::isBiPredFromDifferentDirEqDistPoc(pu)
          && (pu.lheight() >= 8)
    
          && (pu.lwidth() >= 8)
          && ((pu.lheight() * pu.lwidth()) >= 128)
    
          && (pu.cu->GBiIdx == GBI_DEFAULT)
          && ((!wp0[COMPONENT_Y].bPresentFlag) && (!wp1[COMPONENT_Y].bPresentFlag))
    
    Yu Han's avatar
    Yu Han committed
    // for ibc pu validation
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
    bool PU::isBlockVectorValid(PredictionUnit& pu, int xPos, int yPos, int width, int height, int picWidth, int picHeight, int xStartInCU, int yStartInCU, int xBv, int yBv, int ctuSize)
    {
      const int ctuSizeLog2 = g_aucLog2[ctuSize];
    
      int refRightX = xPos + xBv + width - 1;
      int refBottomY = yPos + yBv + height - 1;
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      int refLeftX = xPos + xBv;
      int refTopY = yPos + yBv;
    
      if ((xPos + xBv) < 0)
      {
        return false;
      }
      if (refRightX >= picWidth)
      {
        return false;
      }
    
      if ((yPos + yBv) < 0)
      {
        return false;
      }
      if (refBottomY >= picHeight)
      {
        return false;
      }
      if ((xBv + width) > 0 && (yBv + height) > 0)
      {
        return false;
      }
    
      // cannot be in the above CTU row
      if (refTopY >> ctuSizeLog2 < yPos >> ctuSizeLog2)
        return false;
    
      // cannot be in the below CTU row
      if (refBottomY >> ctuSizeLog2 > yPos >> ctuSizeLog2)
      {
        return false;
      }
    
      // in the same CTU line
    
      int numLeftCTUs = (1 << ((7 - ctuSizeLog2) << 1)) - ((ctuSizeLog2 < 7) ? 1 : 0);
      if ((refRightX >> ctuSizeLog2 <= xPos >> ctuSizeLog2) && (refLeftX >> ctuSizeLog2 >= (xPos >> ctuSizeLog2) - numLeftCTUs))
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      {
    
    
        // in the same CTU, or left CTU
        // if part of ref block is in the left CTU, some area can be referred from the not-yet updated local CTU buffer
    
        if (((refLeftX >> ctuSizeLog2) == ((xPos >> ctuSizeLog2) - 1)) && (ctuSizeLog2 == 7))
    
        {
          // ref block's collocated block in current CTU
          const Position refPosCol = pu.Y().topLeft().offset(xBv + ctuSize, yBv);
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
          int offset64x = (refPosCol.x >> (ctuSizeLog2 - 1)) << (ctuSizeLog2 - 1);
          int offset64y = (refPosCol.y >> (ctuSizeLog2 - 1)) << (ctuSizeLog2 - 1);
          const Position refPosCol64x64 = {offset64x, offset64y};
    
          if (pu.cs->isDecomp(refPosCol64x64, toChannelType(COMPONENT_Y)))
            return false;
    
          if (refPosCol64x64 == pu.Y().topLeft())
            return false;
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      }
      else
        return false;
    
    
      // in the same CTU, or valid area from left CTU. Check if the reference block is already coded
      const Position refPosLT = pu.Y().topLeft().offset(xBv, yBv);
      const Position refPosBR = pu.Y().bottomRight().offset(xBv, yBv);
      const ChannelType      chType = toChannelType(COMPONENT_Y);
      if (!pu.cs->isDecomp(refPosBR, chType))
        return false;
      if (!pu.cs->isDecomp(refPosLT, chType))
        return false;
      return true;
    
    
    Yu Han's avatar
    Yu Han committed
    }// for ibc pu validation
    
    
    static int xGetDistScaleFactor(const int &iCurrPOC, const int &iCurrRefPOC, const int &iColPOC, const int &iColRefPOC)
    {
      int iDiffPocD = iColPOC - iColRefPOC;
      int iDiffPocB = iCurrPOC - iCurrRefPOC;
    
      if (iDiffPocD == iDiffPocB)
      {
        return 4096;
      }
      else
      {
        int iTDB = Clip3(-128, 127, iDiffPocB);
        int iTDD = Clip3(-128, 127, iDiffPocD);
        int iX = (0x4000 + abs(iTDD / 2)) / iTDD;
        int iScale = Clip3(-4096, 4095, (iTDB * iX + 32) >> 6);
        return iScale;
      }
    }
    
    
    int convertMvFixedToFloat(int32_t val)
    {
      int sign  = val >> 31;
      int scale = floorLog2((val ^ sign) | MV_MANTISSA_UPPER_LIMIT) - (MV_MANTISSA_BITCOUNT - 1);
    
      int exponent;
      int mantissa;
      if (scale >= 0)
      {
        int round = (1 << scale) >> 1;
        int n     = (val + round) >> scale;
        exponent  = scale + ((n ^ sign) >> (MV_MANTISSA_BITCOUNT - 1));
        mantissa  = (n & MV_MANTISSA_UPPER_LIMIT) | (sign << (MV_MANTISSA_BITCOUNT - 1));
      }
      else
      {
        exponent = 0;
        mantissa = val;
      }
    
      return exponent | (mantissa << MV_EXPONENT_BITCOUNT);
    }
    
    int convertMvFloatToFixed(int val)
    {
      int exponent = val & MV_EXPONENT_MASK;
      int mantissa = val >> MV_EXPONENT_BITCOUNT;
      return exponent == 0 ? mantissa : (mantissa ^ MV_MANTISSA_LIMIT) << (exponent - 1);
    }
    
    int roundMvComp(int x)
    {
      return convertMvFloatToFixed(convertMvFixedToFloat(x));
    }
    
    
    int PU::getDistScaleFactor(const int &currPOC, const int &currRefPOC, const int &colPOC, const int &colRefPOC)
    {
      return xGetDistScaleFactor(currPOC, currRefPOC, colPOC, colRefPOC);
    }
    
    void PU::getInterMMVDMergeCandidates(const PredictionUnit &pu, MergeCtx& mrgCtx, const int& mrgCandIdx)
    {
      int refIdxList0, refIdxList1;
      int k;
      int currBaseNum = 0;
      const uint16_t maxNumMergeCand = mrgCtx.numValidMergeCand;
    
      for (k = 0; k < maxNumMergeCand; k++)
      {
        if (mrgCtx.mrgTypeNeighbours[k] == MRG_TYPE_DEFAULT_N)
        {
          refIdxList0 = mrgCtx.mvFieldNeighbours[(k << 1)].refIdx;
          refIdxList1 = mrgCtx.mvFieldNeighbours[(k << 1) + 1].refIdx;
    
          if ((refIdxList0 >= 0) && (refIdxList1 >= 0))
          {
            mrgCtx.mmvdBaseMv[currBaseNum][0] = mrgCtx.mvFieldNeighbours[(k << 1)];
            mrgCtx.mmvdBaseMv[currBaseNum][1] = mrgCtx.mvFieldNeighbours[(k << 1) + 1];
          }
          else if (refIdxList0 >= 0)
          {
            mrgCtx.mmvdBaseMv[currBaseNum][0] = mrgCtx.mvFieldNeighbours[(k << 1)];
            mrgCtx.mmvdBaseMv[currBaseNum][1] = MvField(Mv(0, 0), -1);
          }
          else if (refIdxList1 >= 0)
          {
            mrgCtx.mmvdBaseMv[currBaseNum][0] = MvField(Mv(0, 0), -1);
            mrgCtx.mmvdBaseMv[currBaseNum][1] = mrgCtx.mvFieldNeighbours[(k << 1) + 1];
          }
    
          currBaseNum++;
    
          if (currBaseNum == MMVD_BASE_MV_NUM)
            break;
        }
      }
    }
    
    bool PU::getColocatedMVP(const PredictionUnit &pu, const RefPicList &eRefPicList, const Position &_pos, Mv& rcMv, const int &refIdx )
    {
      // don't perform MV compression when generally disabled or subPuMvp is used
    
      const unsigned scale = 4 * std::max<int>(1, 4 * AMVP_DECIMATION_FACTOR / 4);
    
      const unsigned mask  = ~( scale - 1 );
    
      const Position pos = Position{ PosType( _pos.x & mask ), PosType( _pos.y & mask ) };
    
      const Slice &slice = *pu.cs->slice;
    
      // use coldir.
      const Picture* const pColPic = slice.getRefPic(RefPicList(slice.isInterB() ? 1 - slice.getColFromL0Flag() : 0), slice.getColRefIdx());
    
      if( !pColPic )
      {
        return false;
      }
    
      RefPicList eColRefPicList = slice.getCheckLDC() ? eRefPicList : RefPicList(slice.getColFromL0Flag());
    
      const MotionInfo& mi = pColPic->cs->getMotionInfo( pos );
    
      if( !mi.isInter )
      {
        return false;
      }
    
    Yu Han's avatar
    Yu Han committed
      if (mi.isIBCmot)
      {
        return false;
      }
      if (CU::isIBC(*pu.cu))
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      {
        return false;
      }
    
      int iColRefIdx = mi.refIdx[eColRefPicList];
    
      if (iColRefIdx < 0)
      {
        eColRefPicList = RefPicList(1 - eColRefPicList);
        iColRefIdx = mi.refIdx[eColRefPicList];
    
        if (iColRefIdx < 0)
        {
          return false;
        }
      }
    
      const Slice *pColSlice = nullptr;
    
      for( const auto s : pColPic->slices )
      {
        if( s->getIndependentSliceIdx() == mi.sliceIdx )
        {
          pColSlice = s;
          break;
        }
      }
    
      CHECK( pColSlice == nullptr, "Slice segment not found" );
    
      const Slice &colSlice = *pColSlice;
    
      const bool bIsCurrRefLongTerm = slice.getRefPic(eRefPicList, refIdx)->longTerm;
      const bool bIsColRefLongTerm  = colSlice.getIsUsedAsLongTerm(eColRefPicList, iColRefIdx);
    
      if (bIsCurrRefLongTerm != bIsColRefLongTerm)
      {
        return false;
      }
    
    
      // Scale the vector.
      Mv cColMv = mi.mv[eColRefPicList];
    
      cColMv.setHor(roundMvComp(cColMv.getHor()));
      cColMv.setVer(roundMvComp(cColMv.getVer()));
    
    
      if (bIsCurrRefLongTerm /*|| bIsColRefLongTerm*/)
      {
        rcMv = cColMv;
      }
      else
      {
        const int currPOC    = slice.getPOC();
        const int colPOC     = colSlice.getPOC();
        const int colRefPOC  = colSlice.getRefPOC(eColRefPicList, iColRefIdx);
        const int currRefPOC = slice.getRefPic(eRefPicList, refIdx)->getPOC();
        const int distscale  = xGetDistScaleFactor(currPOC, currRefPOC, colPOC, colRefPOC);
    
        if (distscale == 4096)
        {
          rcMv = cColMv;
        }
        else
        {
          rcMv = cColMv.scaleMv(distscale);
        }
      }
    
      return true;
    }
    
    bool PU::isDiffMER(const PredictionUnit &pu1, const PredictionUnit &pu2)
    {
      const unsigned xN = pu1.lumaPos().x;
      const unsigned yN = pu1.lumaPos().y;
      const unsigned xP = pu2.lumaPos().x;
      const unsigned yP = pu2.lumaPos().y;
    
      unsigned plevel = pu1.cs->pps->getLog2ParallelMergeLevelMinus2() + 2;
    
      if ((xN >> plevel) != (xP >> plevel))
      {
        return true;
      }
    
      if ((yN >> plevel) != (yP >> plevel))
      {
        return true;
      }
    
      return false;
    }
    
    
    bool PU::isAddNeighborMv(const Mv& currMv, Mv* neighborMvs, int numNeighborMv)
    {
      bool existed = false;
      for (uint32_t cand = 0; cand < numNeighborMv && !existed; cand++)
      {
        if (currMv == neighborMvs[cand])
        {
          existed = true;
        }
      }
    
      if (!existed)
      {
        return true;
      }
      else
      {
        return false;
      }
    }
    
    void PU::getIbcMVPsEncOnly(PredictionUnit &pu, Mv* mvPred, int& nbPred)
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
    {
    
      const PreCalcValues   &pcv = *pu.cs->pcv;
      const int  cuWidth = pu.blocks[COMPONENT_Y].width;
      const int  cuHeight = pu.blocks[COMPONENT_Y].height;
      const int  log2UnitWidth = g_aucLog2[pcv.minCUWidth];
      const int  log2UnitHeight = g_aucLog2[pcv.minCUHeight];
      const int  totalAboveUnits = (cuWidth >> log2UnitWidth) + 1;
      const int  totalLeftUnits = (cuHeight >> log2UnitHeight) + 1;
    
      nbPred = 0;
      Position posLT = pu.Y().topLeft();
    
      // above-left
    
      const PredictionUnit *aboveLeftPU = pu.cs->getPURestricted(posLT.offset(-1, -1), pu, CHANNEL_TYPE_LUMA);
    
      if (aboveLeftPU && CU::isIBC(*aboveLeftPU->cu))
      {
        if (isAddNeighborMv(aboveLeftPU->bv, mvPred, nbPred))
        {
          mvPred[nbPred++] = aboveLeftPU->bv;
        }
      }
    
      // above neighbors
      for (uint32_t dx = 0; dx < totalAboveUnits && nbPred < IBC_NUM_CANDIDATES; dx++)
      {
    
        const PredictionUnit* tmpPU = pu.cs->getPURestricted(posLT.offset((dx << log2UnitWidth), -1), pu, CHANNEL_TYPE_LUMA);
    
        if (tmpPU && CU::isIBC(*tmpPU->cu))
        {
          if (isAddNeighborMv(tmpPU->bv, mvPred, nbPred))
          {
            mvPred[nbPred++] = tmpPU->bv;
          }
        }
      }
    
      // left neighbors
      for (uint32_t dy = 0; dy < totalLeftUnits && nbPred < IBC_NUM_CANDIDATES; dy++)
      {
    
        const PredictionUnit* tmpPU = pu.cs->getPURestricted(posLT.offset(-1, (dy << log2UnitHeight)), pu, CHANNEL_TYPE_LUMA);
    
        if (tmpPU && CU::isIBC(*tmpPU->cu))
        {
          if (isAddNeighborMv(tmpPU->bv, mvPred, nbPred))
          {
            mvPred[nbPred++] = tmpPU->bv;
          }