Skip to content
Snippets Groups Projects
UnitTools.cpp 128 KiB
Newer Older
  • Learn to ignore specific revisions
  •       }
    
          xInheritedAffineMv( pu, puNeighbour, eTestRefPicList, outputAffineMv );
    
          outputAffineMv[0].roundMV2SignalPrecision();
          outputAffineMv[1].roundMV2SignalPrecision();
          if ( pu.cu->affineType == AFFINEMODEL_6PARAM )
          {
            outputAffineMv[2].roundMV2SignalPrecision();
          }
    
          if ( affiAMVPInfo.numCand == 0
            || (pu.cu->affineType == AFFINEMODEL_4PARAM && (outputAffineMv[0] != affiAMVPInfo.mvCandLT[0] || outputAffineMv[1] != affiAMVPInfo.mvCandRT[0]))
            || (pu.cu->affineType == AFFINEMODEL_6PARAM && (outputAffineMv[0] != affiAMVPInfo.mvCandLT[0] || outputAffineMv[1] != affiAMVPInfo.mvCandRT[0] || outputAffineMv[2] != affiAMVPInfo.mvCandLB[0]))
            )
          {
            affiAMVPInfo.mvCandLT[affiAMVPInfo.numCand] = outputAffineMv[0];
            affiAMVPInfo.mvCandRT[affiAMVPInfo.numCand] = outputAffineMv[1];
            affiAMVPInfo.mvCandLB[affiAMVPInfo.numCand] = outputAffineMv[2];
            affiAMVPInfo.numCand++;
          }
        }
      }
    
    #if REMOVE_MV_ADAPT_PREC
        for (int i = 0; i < affiAMVPInfo.numCand; i++)
        {
          affiAMVPInfo.mvCandLT[i].hor = affiAMVPInfo.mvCandLT[i].hor >= 0 ? (affiAMVPInfo.mvCandLT[i].hor + nOffset) >> nShift : -((-affiAMVPInfo.mvCandLT[i].hor + nOffset) >> nShift);
          affiAMVPInfo.mvCandLT[i].ver = affiAMVPInfo.mvCandLT[i].ver >= 0 ? (affiAMVPInfo.mvCandLT[i].ver + nOffset) >> nShift : -((-affiAMVPInfo.mvCandLT[i].ver + nOffset) >> nShift);
          affiAMVPInfo.mvCandRT[i].hor = affiAMVPInfo.mvCandRT[i].hor >= 0 ? (affiAMVPInfo.mvCandRT[i].hor + nOffset) >> nShift : -((-affiAMVPInfo.mvCandRT[i].hor + nOffset) >> nShift);
          affiAMVPInfo.mvCandRT[i].ver = affiAMVPInfo.mvCandRT[i].ver >= 0 ? (affiAMVPInfo.mvCandRT[i].ver + nOffset) >> nShift : -((-affiAMVPInfo.mvCandRT[i].ver + nOffset) >> nShift);
          affiAMVPInfo.mvCandLB[i].hor = affiAMVPInfo.mvCandLB[i].hor >= 0 ? (affiAMVPInfo.mvCandLB[i].hor + nOffset) >> nShift : -((-affiAMVPInfo.mvCandLB[i].hor + nOffset) >> nShift);
          affiAMVPInfo.mvCandLB[i].ver = affiAMVPInfo.mvCandLB[i].ver >= 0 ? (affiAMVPInfo.mvCandLB[i].ver + nOffset) >> nShift : -((-affiAMVPInfo.mvCandLB[i].ver + nOffset) >> nShift);
        }
    #endif
    
        return;
      }
    
      // insert constructed affine candidates
      int cornerMVPattern = 0;
    
    #if !JVET_L0271_AFFINE_AMVP_SIMPLIFY
    
      Position posLT = pu.Y().topLeft();
      Position posRT = pu.Y().topRight();
      Position posLB = pu.Y().bottomLeft();
    
    
      //-------------------  V0 (START) -------------------//
      AMVPInfo amvpInfo0;
      amvpInfo0.numCand = 0;
    
      // A->C: Above Left, Above, Left
      addMVPCandUnscaled( pu, eRefPicList, refIdx, posLT, MD_ABOVE_LEFT, amvpInfo0, true );
      if ( amvpInfo0.numCand < 1 )
      {
        addMVPCandUnscaled( pu, eRefPicList, refIdx, posLT, MD_ABOVE, amvpInfo0, true );
      }
      if ( amvpInfo0.numCand < 1 )
      {
        addMVPCandUnscaled( pu, eRefPicList, refIdx, posLT, MD_LEFT, amvpInfo0, true );
      }
      cornerMVPattern = cornerMVPattern | amvpInfo0.numCand;
    
      //-------------------  V1 (START) -------------------//
      AMVPInfo amvpInfo1;
      amvpInfo1.numCand = 0;
    
      // D->E: Above, Above Right
      addMVPCandUnscaled( pu, eRefPicList, refIdx, posRT, MD_ABOVE, amvpInfo1, true );
      if ( amvpInfo1.numCand < 1 )
      {
        addMVPCandUnscaled( pu, eRefPicList, refIdx, posRT, MD_ABOVE_RIGHT, amvpInfo1, true );
      }
      cornerMVPattern = cornerMVPattern | (amvpInfo1.numCand << 1);
    
      //-------------------  V2 (START) -------------------//
      AMVPInfo amvpInfo2;
      amvpInfo2.numCand = 0;
    
      // F->G: Left, Below Left
      addMVPCandUnscaled( pu, eRefPicList, refIdx, posLB, MD_LEFT, amvpInfo2, true );
      if ( amvpInfo2.numCand < 1 )
      {
        addMVPCandUnscaled( pu, eRefPicList, refIdx, posLB, MD_BELOW_LEFT, amvpInfo2, true );
      }
      cornerMVPattern = cornerMVPattern | (amvpInfo2.numCand << 2);
    
      outputAffineMv[0] = amvpInfo0.mvCand[0];
      outputAffineMv[1] = amvpInfo1.mvCand[0];
      outputAffineMv[2] = amvpInfo2.mvCand[0];
    
    
    #if !REMOVE_MV_ADAPT_PREC
    
      outputAffineMv[0].setHighPrec();
      outputAffineMv[1].setHighPrec();
      outputAffineMv[2].setHighPrec();
    
    
      outputAffineMv[0].roundMV2SignalPrecision();
      outputAffineMv[1].roundMV2SignalPrecision();
      outputAffineMv[2].roundMV2SignalPrecision();
    
    
    #if JVET_L0271_AFFINE_AMVP_SIMPLIFY
      if ( cornerMVPattern == 7 || (cornerMVPattern == 3 && pu.cu->affineType == AFFINEMODEL_4PARAM) )
      {
        affiAMVPInfo.mvCandLT[affiAMVPInfo.numCand] = outputAffineMv[0];
        affiAMVPInfo.mvCandRT[affiAMVPInfo.numCand] = outputAffineMv[1];
        affiAMVPInfo.mvCandLB[affiAMVPInfo.numCand] = outputAffineMv[2];
        affiAMVPInfo.numCand++;
      }
    #else
    
      if ( cornerMVPattern == 7 || cornerMVPattern == 3 || cornerMVPattern == 5 )
      {
        if ( cornerMVPattern == 3 && pu.cu->affineType == AFFINEMODEL_6PARAM ) // V0 V1 are available, derived V2 for 6-para
        {
          int shift = MAX_CU_DEPTH;
          int vx2 = (outputAffineMv[0].getHor() << shift) - ((outputAffineMv[1].getVer() - outputAffineMv[0].getVer()) << (shift + g_aucLog2[curHeight] - g_aucLog2[curWidth]));
          int vy2 = (outputAffineMv[0].getVer() << shift) + ((outputAffineMv[1].getHor() - outputAffineMv[0].getHor()) << (shift + g_aucLog2[curHeight] - g_aucLog2[curWidth]));
          roundAffineMv( vx2, vy2, shift );
          outputAffineMv[2].set( vx2, vy2 );
          outputAffineMv[2].roundMV2SignalPrecision();
        }
    
        if ( cornerMVPattern == 5 ) // V0 V2 are available, derived V1
        {
          int shift = MAX_CU_DEPTH;
          int vx1 = (outputAffineMv[0].getHor() << shift) + ((outputAffineMv[2].getVer() - outputAffineMv[0].getVer()) << (shift + g_aucLog2[curWidth] - g_aucLog2[curHeight]));
          int vy1 = (outputAffineMv[0].getVer() << shift) - ((outputAffineMv[2].getHor() - outputAffineMv[0].getHor()) << (shift + g_aucLog2[curWidth] - g_aucLog2[curHeight]));
          roundAffineMv( vx1, vy1, shift );
          outputAffineMv[1].set( vx1, vy1 );
          outputAffineMv[1].roundMV2SignalPrecision();
        }
    
        if ( affiAMVPInfo.numCand == 0
          || (pu.cu->affineType == AFFINEMODEL_4PARAM && (outputAffineMv[0] != affiAMVPInfo.mvCandLT[0] || outputAffineMv[1] != affiAMVPInfo.mvCandRT[0]))
          || (pu.cu->affineType == AFFINEMODEL_6PARAM && (outputAffineMv[0] != affiAMVPInfo.mvCandLT[0] || outputAffineMv[1] != affiAMVPInfo.mvCandRT[0] || outputAffineMv[2] != affiAMVPInfo.mvCandLB[0]))
          )
        {
          affiAMVPInfo.mvCandLT[affiAMVPInfo.numCand] = outputAffineMv[0];
          affiAMVPInfo.mvCandRT[affiAMVPInfo.numCand] = outputAffineMv[1];
          affiAMVPInfo.mvCandLB[affiAMVPInfo.numCand] = outputAffineMv[2];
          affiAMVPInfo.numCand++;
        }
      }
    
    #endif
    
    
    #if JVET_L0271_AFFINE_AMVP_SIMPLIFY
      if ( affiAMVPInfo.numCand < 2 )
      {
        // check corner MVs
        for ( int i = 2; i >= 0 && affiAMVPInfo.numCand < AMVP_MAX_NUM_CANDS; i-- )
        {
          if ( cornerMVPattern & (1 << i) ) // MV i exist
          {
            affiAMVPInfo.mvCandLT[affiAMVPInfo.numCand] = outputAffineMv[i];
            affiAMVPInfo.mvCandRT[affiAMVPInfo.numCand] = outputAffineMv[i];
            affiAMVPInfo.mvCandLB[affiAMVPInfo.numCand] = outputAffineMv[i];
            affiAMVPInfo.numCand++;
          }
        }
    
        // Get Temporal Motion Predictor
        if ( affiAMVPInfo.numCand < 2 && pu.cs->slice->getEnableTMVPFlag() )
        {
    
          const int refIdxCol = refIdx;
    
    
          Position posRB = pu.Y().bottomRight().offset( -3, -3 );
    
          const PreCalcValues& pcv = *pu.cs->pcv;
    
          Position posC0;
          bool C0Avail = false;
          Position posC1 = pu.Y().center();
    
          Mv cColMv;
          if ( ((posRB.x + pcv.minCUWidth) < pcv.lumaWidth) && ((posRB.y + pcv.minCUHeight) < pcv.lumaHeight) )
          {
            Position posInCtu( posRB.x & pcv.maxCUWidthMask, posRB.y & pcv.maxCUHeightMask );
    
            if ( (posInCtu.x + 4 < pcv.maxCUWidth) &&           // is not at the last column of CTU
              (posInCtu.y + 4 < pcv.maxCUHeight) )             // is not at the last row    of CTU
            {
              posC0 = posRB.offset( 4, 4 );
              C0Avail = true;
            }
            else if ( posInCtu.x + 4 < pcv.maxCUWidth )           // is not at the last column of CTU But is last row of CTU
            {
              // in the reference the CTU address is not set - thus probably resulting in no using this C0 possibility
              posC0 = posRB.offset( 4, 4 );
            }
            else if ( posInCtu.y + 4 < pcv.maxCUHeight )          // is not at the last row of CTU But is last column of CTU
            {
              posC0 = posRB.offset( 4, 4 );
              C0Avail = true;
            }
            else //is the right bottom corner of CTU
            {
              // same as for last column but not last row
              posC0 = posRB.offset( 4, 4 );
            }
          }
    
    
          if ( (C0Avail && getColocatedMVP( pu, eRefPicList, posC0, cColMv, refIdxCol )) || getColocatedMVP( pu, eRefPicList, posC1, cColMv, refIdxCol ) )
    
          {
    #if !REMOVE_MV_ADAPT_PREC
            cColMv.setHighPrec();
    #endif
            cColMv.roundMV2SignalPrecision();
            affiAMVPInfo.mvCandLT[affiAMVPInfo.numCand] = cColMv;
            affiAMVPInfo.mvCandRT[affiAMVPInfo.numCand] = cColMv;
            affiAMVPInfo.mvCandLB[affiAMVPInfo.numCand] = cColMv;
            affiAMVPInfo.numCand++;
          }
        }
    
        if ( affiAMVPInfo.numCand < 2 )
        {
          // add zero MV
          for ( int i = affiAMVPInfo.numCand; i < AMVP_MAX_NUM_CANDS; i++ )
          {
            affiAMVPInfo.mvCandLT[affiAMVPInfo.numCand].setZero();
            affiAMVPInfo.mvCandRT[affiAMVPInfo.numCand].setZero();
            affiAMVPInfo.mvCandLB[affiAMVPInfo.numCand].setZero();
    #if !REMOVE_MV_ADAPT_PREC
            affiAMVPInfo.mvCandLT[affiAMVPInfo.numCand].setHighPrec();
            affiAMVPInfo.mvCandRT[affiAMVPInfo.numCand].setHighPrec();
            affiAMVPInfo.mvCandLB[affiAMVPInfo.numCand].setHighPrec();
    #endif
            affiAMVPInfo.numCand++;
          }
        }
      }
    #endif
    
    
    #if REMOVE_MV_ADAPT_PREC
      for (int i = 0; i < affiAMVPInfo.numCand; i++)
      {
        affiAMVPInfo.mvCandLT[i].hor = affiAMVPInfo.mvCandLT[i].hor >= 0 ? (affiAMVPInfo.mvCandLT[i].hor + nOffset) >> nShift : -((-affiAMVPInfo.mvCandLT[i].hor + nOffset) >> nShift);
        affiAMVPInfo.mvCandLT[i].ver = affiAMVPInfo.mvCandLT[i].ver >= 0 ? (affiAMVPInfo.mvCandLT[i].ver + nOffset) >> nShift : -((-affiAMVPInfo.mvCandLT[i].ver + nOffset) >> nShift);
        affiAMVPInfo.mvCandRT[i].hor = affiAMVPInfo.mvCandRT[i].hor >= 0 ? (affiAMVPInfo.mvCandRT[i].hor + nOffset) >> nShift : -((-affiAMVPInfo.mvCandRT[i].hor + nOffset) >> nShift);
        affiAMVPInfo.mvCandRT[i].ver = affiAMVPInfo.mvCandRT[i].ver >= 0 ? (affiAMVPInfo.mvCandRT[i].ver + nOffset) >> nShift : -((-affiAMVPInfo.mvCandRT[i].ver + nOffset) >> nShift);
        affiAMVPInfo.mvCandLB[i].hor = affiAMVPInfo.mvCandLB[i].hor >= 0 ? (affiAMVPInfo.mvCandLB[i].hor + nOffset) >> nShift : -((-affiAMVPInfo.mvCandLB[i].hor + nOffset) >> nShift);
        affiAMVPInfo.mvCandLB[i].ver = affiAMVPInfo.mvCandLB[i].ver >= 0 ? (affiAMVPInfo.mvCandLB[i].ver + nOffset) >> nShift : -((-affiAMVPInfo.mvCandLB[i].ver + nOffset) >> nShift);
      }
    #endif
    
      if ( affiAMVPInfo.numCand < 2 )
      {
        AMVPInfo amvpInfo;
        PU::fillMvpCand( pu, eRefPicList, refIdx, amvpInfo );
    
        int iAdd = amvpInfo.numCand - affiAMVPInfo.numCand;
        for ( int i = 0; i < iAdd; i++ )
        {
    
    #if !REMOVE_MV_ADAPT_PREC
    
          amvpInfo.mvCand[i].setHighPrec();
    #endif
          affiAMVPInfo.mvCandLT[affiAMVPInfo.numCand] = amvpInfo.mvCand[i];
          affiAMVPInfo.mvCandRT[affiAMVPInfo.numCand] = amvpInfo.mvCand[i];
          affiAMVPInfo.mvCandLB[affiAMVPInfo.numCand] = amvpInfo.mvCand[i];
          affiAMVPInfo.numCand++;
        }
      }
    
    }
    
    bool PU::addMVPCandUnscaled( const PredictionUnit &pu, const RefPicList &eRefPicList, const int &iRefIdx, const Position &pos, const MvpDir &eDir, AMVPInfo &info, bool affine )
    {
            CodingStructure &cs    = *pu.cs;
      const PredictionUnit *neibPU = NULL;
            Position neibPos;
    
      switch (eDir)
      {
      case MD_LEFT:
        neibPos = pos.offset( -1,  0 );
        break;
      case MD_ABOVE:
        neibPos = pos.offset(  0, -1 );
        break;
      case MD_ABOVE_RIGHT:
        neibPos = pos.offset(  1, -1 );
        break;
      case MD_BELOW_LEFT:
        neibPos = pos.offset( -1,  1 );
        break;
      case MD_ABOVE_LEFT:
        neibPos = pos.offset( -1, -1 );
        break;
      default:
        break;
      }
    
      neibPU = cs.getPURestricted( neibPos, pu, pu.chType );
    
      if( neibPU == NULL || !CU::isInter( *neibPU->cu ) )
      {
        return false;
      }
    
      const MotionInfo& neibMi        = neibPU->getMotionInfo( neibPos );
    
      const int        currRefPOC     = cs.slice->getRefPic( eRefPicList, iRefIdx )->getPOC();
      const RefPicList eRefPicList2nd = ( eRefPicList == REF_PIC_LIST_0 ) ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
    
      for( int predictorSource = 0; predictorSource < 2; predictorSource++ ) // examine the indicated reference picture list, then if not available, examine the other list.
      {
        const RefPicList eRefPicListIndex = ( predictorSource == 0 ) ? eRefPicList : eRefPicList2nd;
        const int        neibRefIdx       = neibMi.refIdx[eRefPicListIndex];
    
        if( neibRefIdx >= 0 && currRefPOC == cs.slice->getRefPOC( eRefPicListIndex, neibRefIdx ) )
        {
          if( affine )
          {
            int i = 0;
            for( i = 0; i < info.numCand; i++ )
            {
              if( info.mvCand[i] == neibMi.mv[eRefPicListIndex] )
              {
                break;
              }
            }
            if( i == info.numCand )
            {
              info.mvCand[info.numCand++] = neibMi.mv[eRefPicListIndex];
    
    #if !REMOVE_MV_ADAPT_PREC
    
              Mv cMvHigh = neibMi.mv[eRefPicListIndex];
              cMvHigh.setHighPrec();
    
    //          CHECK( !neibMi.mv[eRefPicListIndex].highPrec, "Unexpected low precision mv.");
              return true;
            }
          }
          else
          {
            info.mvCand[info.numCand++] = neibMi.mv[eRefPicListIndex];
            return true;
          }
        }
      }
    
    
      return false;
    }
    
    /**
    * \param pInfo
    * \param eRefPicList
    * \param iRefIdx
    * \param uiPartUnitIdx
    * \param eDir
    * \returns bool
    */
    bool PU::addMVPCandWithScaling( const PredictionUnit &pu, const RefPicList &eRefPicList, const int &iRefIdx, const Position &pos, const MvpDir &eDir, AMVPInfo &info, bool affine )
    {
            CodingStructure &cs    = *pu.cs;
      const Slice &slice           = *cs.slice;
      const PredictionUnit *neibPU = NULL;
            Position neibPos;
    
      switch( eDir )
      {
      case MD_LEFT:
        neibPos = pos.offset( -1,  0 );
        break;
      case MD_ABOVE:
        neibPos = pos.offset(  0, -1 );
        break;
      case MD_ABOVE_RIGHT:
        neibPos = pos.offset(  1, -1 );
        break;
      case MD_BELOW_LEFT:
        neibPos = pos.offset( -1,  1 );
        break;
      case MD_ABOVE_LEFT:
        neibPos = pos.offset( -1, -1 );
        break;
      default:
        break;
      }
    
      neibPU = cs.getPURestricted( neibPos, pu, pu.chType );
    
      if( neibPU == NULL || !CU::isInter( *neibPU->cu ) )
      {
        return false;
      }
    
      const MotionInfo& neibMi        = neibPU->getMotionInfo( neibPos );
    
      const RefPicList eRefPicList2nd = ( eRefPicList == REF_PIC_LIST_0 ) ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
    
      const int  currPOC            = slice.getPOC();
      const int  currRefPOC         = slice.getRefPic( eRefPicList, iRefIdx )->poc;
      const bool bIsCurrRefLongTerm = slice.getRefPic( eRefPicList, iRefIdx )->longTerm;
      const int  neibPOC            = currPOC;
    
      for( int predictorSource = 0; predictorSource < 2; predictorSource++ ) // examine the indicated reference picture list, then if not available, examine the other list.
      {
        const RefPicList eRefPicListIndex = (predictorSource == 0) ? eRefPicList : eRefPicList2nd;
        const int        neibRefIdx       = neibMi.refIdx[eRefPicListIndex];
        if( neibRefIdx >= 0 )
        {
          const bool bIsNeibRefLongTerm = slice.getRefPic(eRefPicListIndex, neibRefIdx)->longTerm;
    
          if (bIsCurrRefLongTerm == bIsNeibRefLongTerm)
          {
            Mv cMv = neibMi.mv[eRefPicListIndex];
    
            if( !( bIsCurrRefLongTerm /* || bIsNeibRefLongTerm*/) )
            {
              const int neibRefPOC = slice.getRefPOC( eRefPicListIndex, neibRefIdx );
              const int scale      = xGetDistScaleFactor( currPOC, currRefPOC, neibPOC, neibRefPOC );
    
              if( scale != 4096 )
              {
    
    #if !REMOVE_MV_ADAPT_PREC
    
                if( slice.getSPS()->getSpsNext().getUseHighPrecMv() )
                {
                  cMv.setHighPrec();
                }
    #endif
                cMv = cMv.scaleMv( scale );
              }
            }
    
            if( affine )
            {
              int i;
              for( i = 0; i < info.numCand; i++ )
              {
                if( info.mvCand[i] == cMv )
                {
                  break;
                }
              }
              if( i == info.numCand )
              {
                info.mvCand[info.numCand++] = cMv;
    //            CHECK( !cMv.highPrec, "Unexpected low precision mv.");
                return true;
              }
            }
            else
            {
              info.mvCand[info.numCand++] = cMv;
              return true;
            }
          }
        }
      }
    
    
      return false;
    }
    
    
    #if JVET_L0266_HMVP
    void PU::addAMVPHMVPCand(const PredictionUnit &pu, const RefPicList eRefPicList, const RefPicList eRefPicList2nd, const int currRefPOC, AMVPInfo &info, uint8_t imv)
    {
      const Slice &slice = *(*pu.cs).slice;
    
      MotionInfo neibMi;
      int i = 0;
      unsigned imvShift = imv << 1;
    
      int num_avai_candInLUT = slice.getAvailableLUTMrgNum();
      int num_allowedCand = std::min(MAX_NUM_HMVP_AVMPCANDS, num_avai_candInLUT);
    
      for (int mrgIdx = 1; mrgIdx <= num_allowedCand; mrgIdx++)
      {
        if (info.numCand >= AMVP_MAX_NUM_CANDS)
        {
          return;
        }
        neibMi = slice.getMotionInfoFromLUTs(num_avai_candInLUT - mrgIdx);
    
    
    Li's avatar
    Li committed
        for (int predictorSource = 0; predictorSource < 2; predictorSource++) 
    
        {
          const RefPicList eRefPicListIndex = (predictorSource == 0) ? eRefPicList : eRefPicList2nd;
          const int        neibRefIdx = neibMi.refIdx[eRefPicListIndex];
    
          if (neibRefIdx >= 0 && currRefPOC == slice.getRefPOC(eRefPicListIndex, neibRefIdx))
          {
            Mv pmv = neibMi.mv[eRefPicListIndex];
            if (imv != 0)
            {
              roundMV(pmv, imvShift);
            }
            for (i = 0; i < info.numCand; i++)
            {
              if (pmv == info.mvCand[i])
              {
                break;
              }
            }
            if (i == info.numCand)
            {
              info.mvCand[info.numCand++] = pmv;
              if (info.numCand >= AMVP_MAX_NUM_CANDS)
              {
                return;
              }
            }
          }
        }
      }
    }
    #endif
    
    bool PU::isBipredRestriction(const PredictionUnit &pu)
    {
      const SPSNext &spsNext = pu.cs->sps->getSpsNext();
    
    #if JVET_L0104_NO_4x4BI_INTER_CU
      if(pu.cu->lumaSize().width == 4 && pu.cu->lumaSize().height ==4 )
      {
        return true;
      }
    #endif
    
      if( !pu.cs->pcv->only2Nx2N && !spsNext.getUseSubPuMvp() && pu.cu->lumaSize().width == 8 && ( pu.lumaSize().width < 8 || pu.lumaSize().height < 8 ) )
      {
        return true;
      }
      return false;
    }
    
    
    void PU::getAffineControlPointCand( const PredictionUnit &pu, MotionInfo mi[4], bool isAvailable[4], int verIdx[4], int modelIdx, int verNum, AffineMergeCtx& affMrgType )
    
    {
      int cuW = pu.Y().width;
      int cuH = pu.Y().height;
      int vx, vy;
      int shift = MAX_CU_DEPTH;
      int shiftHtoW = shift + g_aucLog2[cuW] - g_aucLog2[cuH];
    
      // motion info
      Mv cMv[2][4];
      int refIdx[2] = { -1, -1 };
      int dir = 0;
      EAffineModel curType = (verNum == 2) ? AFFINEMODEL_4PARAM : AFFINEMODEL_6PARAM;
    
      if ( verNum == 2 )
      {
        int idx0 = verIdx[0], idx1 = verIdx[1];
    
        if ( !isAvailable[idx0] || !isAvailable[idx1] )
    
        {
          return;
        }
    
        for ( int l = 0; l < 2; l++ )
        {
          if ( mi[idx0].refIdx[l] >= 0 && mi[idx1].refIdx[l] >= 0 )
          {
            // check same refidx and different mv
            if ( mi[idx0].refIdx[l] == mi[idx1].refIdx[l] && mi[idx0].mv[l] != mi[idx1].mv[l] )
            {
              dir |= (l + 1);
              refIdx[l] = mi[idx0].refIdx[l];
            }
          }
        }
      }
      else if ( verNum == 3 )
      {
        int idx0 = verIdx[0], idx1 = verIdx[1], idx2 = verIdx[2];
    
        if ( !isAvailable[idx0] || !isAvailable[idx1] || !isAvailable[idx2] )
    
    2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951
        {
          return;
        }
    
        for ( int l = 0; l < 2; l++ )
        {
          if ( mi[idx0].refIdx[l] >= 0 && mi[idx1].refIdx[l] >= 0 && mi[idx2].refIdx[l] >= 0 )
          {
            // check same refidx and different mv
            if ( mi[idx0].refIdx[l] == mi[idx1].refIdx[l] && mi[idx0].refIdx[l] == mi[idx2].refIdx[l] && (mi[idx0].mv[l] != mi[idx1].mv[l] || mi[idx0].mv[l] != mi[idx2].mv[l]) )
            {
              dir |= (l + 1);
              refIdx[l] = mi[idx0].refIdx[l];
            }
          }
        }
      }
    
      if ( dir == 0 )
      {
        return;
      }
    
    #if !REMOVE_MV_ADAPT_PREC
      for ( int l = 0; l < 2; l++ )
      {
        for ( int i = 0; i < 4; i++ )
        {
          cMv[l][i].highPrec = true;
        }
      }
    #endif
    
      for ( int l = 0; l < 2; l++ )
      {
        if ( dir & (l + 1) )
        {
          for ( int i = 0; i < verNum; i++ )
          {
            cMv[l][verIdx[i]] = mi[verIdx[i]].mv[l];
          }
    
          // convert to LT, RT[, [LB]]
          switch ( modelIdx )
          {
          case 0: // 0 : LT, RT, LB
            break;
    
          case 1: // 1 : LT, RT, RB
            cMv[l][2].hor = cMv[l][3].hor + cMv[l][0].hor - cMv[l][1].hor;
            cMv[l][2].ver = cMv[l][3].ver + cMv[l][0].ver - cMv[l][1].ver;
            break;
    
          case 2: // 2 : LT, LB, RB
            cMv[l][1].hor = cMv[l][3].hor + cMv[l][0].hor - cMv[l][2].hor;
            cMv[l][1].ver = cMv[l][3].ver + cMv[l][0].ver - cMv[l][2].ver;
            break;
    
          case 3: // 3 : RT, LB, RB
            cMv[l][0].hor = cMv[l][1].hor + cMv[l][2].hor - cMv[l][3].hor;
            cMv[l][0].ver = cMv[l][1].ver + cMv[l][2].ver - cMv[l][3].ver;
            break;
    
          case 4: // 4 : LT, RT
            break;
    
          case 5: // 5 : LT, LB
            vx = (cMv[l][0].hor << shift) + ((cMv[l][2].ver - cMv[l][0].ver) << shiftHtoW);
            vy = (cMv[l][0].ver << shift) - ((cMv[l][2].hor - cMv[l][0].hor) << shiftHtoW);
            roundAffineMv( vx, vy, shift );
            cMv[l][1].set( vx, vy );
            break;
    
          default:
            CHECK( 1, "Invalid model index!\n" );
            break;
          }
        }
        else
        {
          for ( int i = 0; i < 4; i++ )
          {
            cMv[l][i].hor = 0;
            cMv[l][i].ver = 0;
          }
        }
      }
    
      for ( int i = 0; i < 3; i++ )
      {
        affMrgType.mvFieldNeighbours[(affMrgType.numValidMergeCand << 1) + 0][i].mv = cMv[0][i];
        affMrgType.mvFieldNeighbours[(affMrgType.numValidMergeCand << 1) + 0][i].refIdx = refIdx[0];
    
        affMrgType.mvFieldNeighbours[(affMrgType.numValidMergeCand << 1) + 1][i].mv = cMv[1][i];
        affMrgType.mvFieldNeighbours[(affMrgType.numValidMergeCand << 1) + 1][i].refIdx = refIdx[1];
      }
      affMrgType.interDirNeighbours[affMrgType.numValidMergeCand] = dir;
      affMrgType.affineType[affMrgType.numValidMergeCand] = curType;
      affMrgType.numValidMergeCand++;
    
    
      return;
    }
    
    const int getAvailableAffineNeighboursForLeftPredictor( const PredictionUnit &pu, const PredictionUnit* npu[] )
    {
      const Position posLB = pu.Y().bottomLeft();
      int num = 0;
    
      const PredictionUnit *puLeftBottom = pu.cs->getPURestricted( posLB.offset( -1, 1 ), pu, pu.chType );
      if ( puLeftBottom && puLeftBottom->cu->affine
    #if JVET_L0369_SUBBLOCK_MERGE
        && puLeftBottom->mergeType == MRG_TYPE_DEFAULT_N
    #endif
        )
      {
        npu[num++] = puLeftBottom;
        return num;
      }
    
      const PredictionUnit* puLeft = pu.cs->getPURestricted( posLB.offset( -1, 0 ), pu, pu.chType );
      if ( puLeft && puLeft->cu->affine
    #if JVET_L0369_SUBBLOCK_MERGE
        && puLeft->mergeType == MRG_TYPE_DEFAULT_N
    #endif
        )
      {
        npu[num++] = puLeft;
        return num;
      }
    
      return num;
    }
    
    const int getAvailableAffineNeighboursForAbovePredictor( const PredictionUnit &pu, const PredictionUnit* npu[], int numAffNeighLeft )
    {
      const Position posLT = pu.Y().topLeft();
      const Position posRT = pu.Y().topRight();
      int num = numAffNeighLeft;
    
      const PredictionUnit* puAboveRight = pu.cs->getPURestricted( posRT.offset( 1, -1 ), pu, pu.chType );
      if ( puAboveRight && puAboveRight->cu->affine
    #if JVET_L0369_SUBBLOCK_MERGE
        && puAboveRight->mergeType == MRG_TYPE_DEFAULT_N
    #endif
        )
      {
        npu[num++] = puAboveRight;
        return num;
      }
    
      const PredictionUnit* puAbove = pu.cs->getPURestricted( posRT.offset( 0, -1 ), pu, pu.chType );
      if ( puAbove && puAbove->cu->affine
    #if JVET_L0369_SUBBLOCK_MERGE
        && puAbove->mergeType == MRG_TYPE_DEFAULT_N
    #endif
        )
      {
        npu[num++] = puAbove;
        return num;
      }
    
      const PredictionUnit *puAboveLeft = pu.cs->getPURestricted( posLT.offset( -1, -1 ), pu, pu.chType );
      if ( puAboveLeft && puAboveLeft->cu->affine
    #if JVET_L0369_SUBBLOCK_MERGE
        && puAboveLeft->mergeType == MRG_TYPE_DEFAULT_N
    #endif
        )
      {
        npu[num++] = puAboveLeft;
        return num;
      }
    
      return num;
    }
    
    void PU::getAffineMergeCand( const PredictionUnit &pu, AffineMergeCtx& affMrgCtx, const int mrgCandIdx )
    {
      const CodingStructure &cs = *pu.cs;
      const Slice &slice = *pu.cs->slice;
      const uint32_t maxNumAffineMergeCand = slice.getMaxNumAffineMergeCand();
    
      for ( int i = 0; i < maxNumAffineMergeCand; i++ )
      {
        for ( int mvNum = 0; mvNum < 3; mvNum++ )
        {
          affMrgCtx.mvFieldNeighbours[(i << 1) + 0][mvNum].setMvField( Mv(), -1 );
          affMrgCtx.mvFieldNeighbours[(i << 1) + 1][mvNum].setMvField( Mv(), -1 );
        }
        affMrgCtx.interDirNeighbours[i] = 0;
        affMrgCtx.affineType[i] = AFFINEMODEL_4PARAM;
    #if JVET_L0369_SUBBLOCK_MERGE
        affMrgCtx.mergeType[i] = MRG_TYPE_DEFAULT_N;
    #endif
    #if JVET_L0646_GBI
        affMrgCtx.GBiIdx[i] = GBI_DEFAULT;
    #endif
      }
    
      affMrgCtx.numValidMergeCand = 0;
      affMrgCtx.maxNumMergeCand = maxNumAffineMergeCand;
    
    #if JVET_L0369_SUBBLOCK_MERGE ///> insert ATMVP candidate
      bool enableSubPuMvp = slice.getSPS()->getSpsNext().getUseSubPuMvp();
      bool isAvailableSubPu = false;
      if ( enableSubPuMvp && slice.getEnableTMVPFlag() )
      {
        MergeCtx mrgCtx = *affMrgCtx.mrgCtx;
        bool tmpLICFlag = false;
    
        CHECK( mrgCtx.subPuMvpMiBuf.area() == 0 || !mrgCtx.subPuMvpMiBuf.buf, "Buffer not initialized" );
        mrgCtx.subPuMvpMiBuf.fill( MotionInfo() );
    
        int pos = 0;
        // Get spatial MV
        const Position posCurRT = pu.Y().topRight();
        const Position posCurLB = pu.Y().bottomLeft();
        MotionInfo miAbove, miLeft, miAboveRight, miBelowLeft;
    
        //left
        const PredictionUnit* puLeft = cs.getPURestricted( posCurLB.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( posCurLB.offset( -1, 0 ) );
          // get Inter Dir
          mrgCtx.interDirNeighbours[pos] = miLeft.interDir;
    
          // get Mv from Left
          mrgCtx.mvFieldNeighbours[pos << 1].setMvField( miLeft.mv[0], miLeft.refIdx[0] );
    
          if ( slice.isInterB() )
          {
            mrgCtx.mvFieldNeighbours[(pos << 1) + 1].setMvField( miLeft.mv[1], miLeft.refIdx[1] );
          }
          pos++;
        }
    
        // above
        const PredictionUnit *puAbove = cs.getPURestricted( posCurRT.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( posCurRT.offset( 0, -1 ) );
    
          if ( !isAvailableA1 || (miAbove != miLeft) )
          {
            // get Inter Dir
            mrgCtx.interDirNeighbours[pos] = miAbove.interDir;
            // get Mv from Left
            mrgCtx.mvFieldNeighbours[pos << 1].setMvField( miAbove.mv[0], miAbove.refIdx[0] );
    
            if ( slice.isInterB() )
            {
              mrgCtx.mvFieldNeighbours[(pos << 1) + 1].setMvField( miAbove.mv[1], miAbove.refIdx[1] );
            }
    
            pos++;
          }
        }
    
        // above right
        const PredictionUnit *puAboveRight = cs.getPURestricted( posCurRT.offset( 1, -1 ), pu, pu.chType );
        bool isAvailableB0 = puAboveRight && isDiffMER( pu, *puAboveRight ) && CU::isInter( *puAboveRight->cu );
        if ( isAvailableB0 )
        {
          miAboveRight = puAboveRight->getMotionInfo( posCurRT.offset( 1, -1 ) );
    
    #if HM_JEM_MERGE_CANDS
          if ( (!isAvailableB1 || (miAbove != miAboveRight)) && (!isAvailableA1 || (miLeft != miAboveRight)) )
    #else
          if ( !isAvailableB1 || (miAbove != miAboveRight) )
    #endif
          {
            // get Inter Dir
            mrgCtx.interDirNeighbours[pos] = miAboveRight.interDir;
            // get Mv from Left
            mrgCtx.mvFieldNeighbours[pos << 1].setMvField( miAboveRight.mv[0], miAboveRight.refIdx[0] );
    
            if ( slice.isInterB() )
            {
              mrgCtx.mvFieldNeighbours[(pos << 1) + 1].setMvField( miAboveRight.mv[1], miAboveRight.refIdx[1] );
            }
    
            pos++;
          }
        }
    
        //left bottom
        const PredictionUnit *puLeftBottom = cs.getPURestricted( posCurLB.offset( -1, 1 ), pu, pu.chType );
        bool isAvailableA0 = puLeftBottom && isDiffMER( pu, *puLeftBottom ) && CU::isInter( *puLeftBottom->cu );
        if ( isAvailableA0 )
        {
          miBelowLeft = puLeftBottom->getMotionInfo( posCurLB.offset( -1, 1 ) );
    
    #if HM_JEM_MERGE_CANDS
          if ( (!isAvailableA1 || (miBelowLeft != miLeft)) && (!isAvailableB1 || (miBelowLeft != miAbove)) && (!isAvailableB0 || (miBelowLeft != miAboveRight)) )
    #else
          if ( !isAvailableA1 || (miBelowLeft != miLeft) )
    #endif
          {
            // get Inter Dir
            mrgCtx.interDirNeighbours[pos] = miBelowLeft.interDir;
            // get Mv from Bottom-Left
            mrgCtx.mvFieldNeighbours[pos << 1].setMvField( miBelowLeft.mv[0], miBelowLeft.refIdx[0] );
    
            if ( slice.isInterB() )
            {
              mrgCtx.mvFieldNeighbours[(pos << 1) + 1].setMvField( miBelowLeft.mv[1], miBelowLeft.refIdx[1] );
            }
            pos++;
          }
        }
        mrgCtx.numValidMergeCand = pos;
    
        isAvailableSubPu = getInterMergeSubPuMvpCand( pu, mrgCtx, tmpLICFlag, pos
    #if JVET_L0054_MMVD
          , 0
    #endif
        );
        if ( isAvailableSubPu )
        {
          for ( int mvNum = 0; mvNum < 3; mvNum++ )
          {
            affMrgCtx.mvFieldNeighbours[(affMrgCtx.numValidMergeCand << 1) + 0][mvNum].setMvField( mrgCtx.mvFieldNeighbours[(pos << 1) + 0].mv, mrgCtx.mvFieldNeighbours[(pos << 1) + 0].refIdx );
            affMrgCtx.mvFieldNeighbours[(affMrgCtx.numValidMergeCand << 1) + 1][mvNum].setMvField( mrgCtx.mvFieldNeighbours[(pos << 1) + 1].mv, mrgCtx.mvFieldNeighbours[(pos << 1) + 1].refIdx );
          }
          affMrgCtx.interDirNeighbours[affMrgCtx.numValidMergeCand] = mrgCtx.interDirNeighbours[pos];
    
          affMrgCtx.affineType[affMrgCtx.numValidMergeCand] = AFFINE_MODEL_NUM;
          affMrgCtx.mergeType[affMrgCtx.numValidMergeCand] = MRG_TYPE_SUBPU_ATMVP;
          if ( affMrgCtx.numValidMergeCand == mrgCandIdx )
          {
            return;
          }
    
          affMrgCtx.numValidMergeCand++;
    
          // early termination
          if ( affMrgCtx.numValidMergeCand == maxNumAffineMergeCand )
          {
            return;
          }
        }
      }
    #endif
    
    #if JVET_L0369_SUBBLOCK_MERGE
      if ( slice.getSPS()->getSpsNext().getUseAffine() )
      {
    #endif
        ///> Start: inherited affine candidates
        const PredictionUnit* npu[5];
        int numAffNeighLeft = getAvailableAffineNeighboursForLeftPredictor( pu, npu );
        int numAffNeigh = getAvailableAffineNeighboursForAbovePredictor( pu, npu, numAffNeighLeft );
        for ( int idx = 0; idx < numAffNeigh; idx++ )
        {
          // derive Mv from Neigh affine PU
          Mv cMv[2][3];
          const PredictionUnit* puNeigh = npu[idx];
          pu.cu->affineType = puNeigh->cu->affineType;
          if ( puNeigh->interDir != 2 )
          {
            xInheritedAffineMv( pu, puNeigh, REF_PIC_LIST_0, cMv[0] );
          }
          if ( slice.isInterB() )
          {
            if ( puNeigh->interDir != 1 )
            {
              xInheritedAffineMv( pu, puNeigh, REF_PIC_LIST_1, cMv[1] );
            }
          }
    
          for ( int mvNum = 0; mvNum < 3; mvNum++ )
          {
            affMrgCtx.mvFieldNeighbours[(affMrgCtx.numValidMergeCand << 1) + 0][mvNum].setMvField( cMv[0][mvNum], puNeigh->refIdx[0] );
            affMrgCtx.mvFieldNeighbours[(affMrgCtx.numValidMergeCand << 1) + 1][mvNum].setMvField( cMv[1][mvNum], puNeigh->refIdx[1] );
          }
          affMrgCtx.interDirNeighbours[affMrgCtx.numValidMergeCand] = puNeigh->interDir;
          affMrgCtx.affineType[affMrgCtx.numValidMergeCand] = (EAffineModel)(puNeigh->cu->affineType);
    #if JVET_L0646_GBI
          affMrgCtx.GBiIdx[affMrgCtx.numValidMergeCand] = puNeigh->cu->GBiIdx;
    #endif
    
          if ( affMrgCtx.numValidMergeCand == mrgCandIdx )
          {
            return;
          }
    
          // early termination
          affMrgCtx.numValidMergeCand++;
          if ( affMrgCtx.numValidMergeCand == maxNumAffineMergeCand )
          {
            return;
          }
        }
        ///> End: inherited affine candidates
    
        ///> Start: Constructed affine candidates
        {
          MotionInfo mi[4];
    
          bool isAvailable[4] = { false };
    
    
          // control point: LT B2->B3->A2
          const Position posLT[3] = { pu.Y().topLeft().offset( -1, -1 ), pu.Y().topLeft().offset( 0, -1 ), pu.Y().topLeft().offset( -1, 0 ) };
          for ( int i = 0; i < 3; i++ )
          {
            const Position pos = posLT[i];
            const PredictionUnit* puNeigh = cs.getPURestricted( pos, pu, pu.chType );
            if ( puNeigh && CU::isInter( *puNeigh->cu ) )
            {
    
              mi[0] = puNeigh->getMotionInfo( pos );
    #if !REMOVE_MV_ADAPT_PREC
              mi[0].mv[0].setHighPrec();
              mi[0].mv[1].setHighPrec();
    #endif
              break;
            }
          }
    
          // control point: RT B1->B0
          const Position posRT[2] = { pu.Y().topRight().offset( 0, -1 ), pu.Y().topRight().offset( 1, -1 ) };
          for ( int i = 0; i < 2; i++ )
          {
            const Position pos = posRT[i];
            const PredictionUnit* puNeigh = cs.getPURestricted( pos, pu, pu.chType );
            if ( puNeigh && CU::isInter( *puNeigh->cu ) )
            {
    
              mi[1] = puNeigh->getMotionInfo( pos );
    #if !REMOVE_MV_ADAPT_PREC