Skip to content
Snippets Groups Projects
UnitTools.cpp 179 KiB
Newer Older
  • Learn to ignore specific revisions
  •   //-------------------  V0 (START) -------------------//
      AMVPInfo amvpInfo0;
      amvpInfo0.numCand = 0;
    
      // A->C: Above Left, Above, Left
    
    Huanbang Chen's avatar
    Huanbang Chen committed
      addMVPCandUnscaled( pu, eRefPicList, refIdx, posLT, MD_ABOVE_LEFT, amvpInfo0 );
    
    Huanbang Chen's avatar
    Huanbang Chen committed
        addMVPCandUnscaled( pu, eRefPicList, refIdx, posLT, MD_ABOVE, amvpInfo0 );
    
    Huanbang Chen's avatar
    Huanbang Chen committed
        addMVPCandUnscaled( pu, eRefPicList, refIdx, posLT, MD_LEFT, amvpInfo0 );
    
      }
      cornerMVPattern = cornerMVPattern | amvpInfo0.numCand;
    
      //-------------------  V1 (START) -------------------//
      AMVPInfo amvpInfo1;
      amvpInfo1.numCand = 0;
    
      // D->E: Above, Above Right
    
    Huanbang Chen's avatar
    Huanbang Chen committed
      addMVPCandUnscaled( pu, eRefPicList, refIdx, posRT, MD_ABOVE, amvpInfo1 );
    
    Huanbang Chen's avatar
    Huanbang Chen committed
        addMVPCandUnscaled( pu, eRefPicList, refIdx, posRT, MD_ABOVE_RIGHT, amvpInfo1 );
    
      }
      cornerMVPattern = cornerMVPattern | (amvpInfo1.numCand << 1);
    
      //-------------------  V2 (START) -------------------//
      AMVPInfo amvpInfo2;
      amvpInfo2.numCand = 0;
    
      // F->G: Left, Below Left
    
    Huanbang Chen's avatar
    Huanbang Chen committed
      addMVPCandUnscaled( pu, eRefPicList, refIdx, posLB, MD_LEFT, amvpInfo2 );
    
    Huanbang Chen's avatar
    Huanbang Chen committed
        addMVPCandUnscaled( pu, eRefPicList, refIdx, posLB, MD_BELOW_LEFT, amvpInfo2 );
    
      }
      cornerMVPattern = cornerMVPattern | (amvpInfo2.numCand << 2);
    
      outputAffineMv[0] = amvpInfo0.mvCand[0];
      outputAffineMv[1] = amvpInfo1.mvCand[0];
      outputAffineMv[2] = amvpInfo2.mvCand[0];
    
    
      if ( pu.cu->imv == 0 )
      {
        outputAffineMv[0].roundToPrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
        outputAffineMv[1].roundToPrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
        outputAffineMv[2].roundToPrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
      }
      else if ( pu.cu->imv == 2 )
      {
        outputAffineMv[0].roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_INT );
        outputAffineMv[1].roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_INT );
        outputAffineMv[2].roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_INT );
      }
    
      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++;
      }
    
    
      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();
    
          bool C1Avail =  ( posC1.x  < pcv.lumaWidth ) && ( posC1.y < pcv.lumaHeight ) ;
    
          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 JVET_N0266_SMALL_BLOCKS
          if ( ( C0Avail && getColocatedMVP( pu, eRefPicList, posC0, cColMv, refIdxCol ) ) || getColocatedMVP( pu, eRefPicList, posC1, cColMv, refIdxCol ) )
    #else
    
          if ( (C0Avail && getColocatedMVP( pu, eRefPicList, posC0, cColMv, refIdxCol )) || (C1Avail && getColocatedMVP( pu, eRefPicList, posC1, cColMv, refIdxCol ) ) )
    
            if ( pu.cu->imv == 0 )
            {
              cColMv.roundToPrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
            }
            else if ( pu.cu->imv == 2 )
            {
              cColMv.roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_INT );
            }
    
            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();
            affiAMVPInfo.numCand++;
          }
        }
      }
    
    
      for (int i = 0; i < affiAMVPInfo.numCand; i++)
      {
    
          affiAMVPInfo.mvCandLT[i].changePrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
          affiAMVPInfo.mvCandRT[i].changePrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
    
          affiAMVPInfo.mvCandLB[i].changePrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
        }
    
    Yu Han's avatar
    Yu Han committed
    bool PU::addIBCMVPCand(const PredictionUnit &pu, const Position &pos, const MvpDir &eDir, AMVPInfo &info)
    {
      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::isIBC(*neibPU->cu)==false)
      {
        return false;
      }
    
      const MotionInfo& neibMi = neibPU->getMotionInfo(neibPos);
      info.mvCand[info.numCand++] = neibMi.mv[REF_PIC_LIST_0];
      return true;
    }
    
    
    Huanbang Chen's avatar
    Huanbang Chen committed
    bool PU::addMVPCandUnscaled( const PredictionUnit &pu, const RefPicList &eRefPicList, const int &iRefIdx, const Position &pos, const MvpDir &eDir, AMVPInfo &info )
    
    {
            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 ) )
        {
    
    Huanbang Chen's avatar
    Huanbang Chen committed
          info.mvCand[info.numCand++] = neibMi.mv[eRefPicListIndex];
          return true;
    
        }
      }
    
      return false;
    }
    
    /**
    * \param pInfo
    * \param eRefPicList
    * \param iRefIdx
    * \param uiPartUnitIdx
    * \param eDir
    * \returns bool
    */
    
    Huanbang Chen's avatar
    Huanbang Chen committed
    bool PU::addMVPCandWithScaling( const PredictionUnit &pu, const RefPicList &eRefPicList, const int &iRefIdx, const Position &pos, const MvpDir &eDir, AMVPInfo &info )
    
    {
            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 );
    
    
    Yu Han's avatar
    Yu Han committed
      if (neibPU == NULL || !CU::isInter(*neibPU->cu) || !CU::isInter(*pu.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 )
              {
                cMv = cMv.scaleMv( scale );
              }
            }
    
    
    Huanbang Chen's avatar
    Huanbang Chen committed
            info.mvCand[info.numCand++] = cMv;
            return true;
    
    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;
    
      auto &lut = CU::isIBC(*pu.cu) ? pu.cs->motionLut.lutIbc : pu.cs->motionLut.lut;
      int num_avai_candInLUT = (int) lut.size();
    
      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 = lut[mrgIdx - 1];
    
        for (int predictorSource = 0; predictorSource < 2; predictorSource++)
    
        {
          const RefPicList eRefPicListIndex = (predictorSource == 0) ? eRefPicList : eRefPicList2nd;
          const int        neibRefIdx = neibMi.refIdx[eRefPicListIndex];
    
    
    Yu Han's avatar
    Yu Han committed
          if (neibRefIdx >= 0 && (CU::isIBC(*pu.cu) || (currRefPOC == slice.getRefPOC(eRefPicListIndex, neibRefIdx))))
    
          {
            Mv pmv = neibMi.mv[eRefPicListIndex];
    
            pmv.roundToAmvrSignalPrecision(MV_PRECISION_INTERNAL, pu.cu->imv);
    
    
            info.mvCand[info.numCand++] = pmv;
            if (info.numCand >= AMVP_MAX_NUM_CANDS)
            {
              return;
            }
    
    bool PU::isBipredRestriction(const PredictionUnit &pu)
    {
    
      if(pu.cu->lumaSize().width == 4 && pu.cu->lumaSize().height ==4 )
      {
        return true;
      }
    
    #if JVET_N0266_SMALL_BLOCKS
      /* disable bi-prediction for 4x8/8x4 */
      if ( pu.cu->lumaSize().width + pu.cu->lumaSize().height == 12 )
      {
        return true;
      }
    #endif
    
    #if JVET_N0481_BCW_CONSTRUCTED_AFFINE 
    void PU::getAffineControlPointCand(const PredictionUnit &pu, MotionInfo mi[4], int8_t neighGbi[4], bool isAvailable[4], int verIdx[4], int modelIdx, int verNum, AffineMergeCtx& affMrgType)
    #else
    
    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;
    
    #if  JVET_N0481_BCW_CONSTRUCTED_AFFINE
      int8_t gbiIdx = GBI_DEFAULT;
    #endif
    
      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])
    
            {
              dir |= (l + 1);
              refIdx[l] = mi[idx0].refIdx[l];
            }
          }
        }
    
    #if JVET_N0481_BCW_CONSTRUCTED_AFFINE
        if (dir == 3)
        {
          if (neighGbi[idx0] == neighGbi[idx1])
          {
            gbiIdx = neighGbi[idx0];
          }
        }
    
    #endif
    
      }
      else if ( verNum == 3 )
      {
        int idx0 = verIdx[0], idx1 = verIdx[1], idx2 = verIdx[2];
    
        if ( !isAvailable[idx0] || !isAvailable[idx1] || !isAvailable[idx2] )
    
        {
          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])
    
            {
              dir |= (l + 1);
              refIdx[l] = mi[idx0].refIdx[l];
            }
          }
        }
    
    #if JVET_N0481_BCW_CONSTRUCTED_AFFINE
        int gbiClass[5] = { -1,0,0,0,1 };
        if (dir == 3)
        {
          if (neighGbi[idx0] == neighGbi[idx1] && gbiClass[neighGbi[idx0]] == gbiClass[neighGbi[idx2]])
          {
            gbiIdx = neighGbi[idx0];
          }
          else if (neighGbi[idx0] == neighGbi[idx2] && gbiClass[neighGbi[idx0]] == gbiClass[neighGbi[idx1]])
          {
            gbiIdx = neighGbi[idx0];
    
          }
          else if (neighGbi[idx1] == neighGbi[idx2] && gbiClass[neighGbi[idx0]] == gbiClass[neighGbi[idx1]])
          {
            gbiIdx = neighGbi[idx1];
          }
          else
          {
            gbiIdx = GBI_DEFAULT;
          }
    
        }
    
    #endif
    
      }
    
      if ( dir == 0 )
      {
        return;
      }
    
    
      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;
    
    #if JVET_N0334_MVCLIPPING
            cMv[l][2].clipToStorageBitDepth();
    #endif
    
            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;
    
    #if JVET_N0334_MVCLIPPING
            cMv[l][1].clipToStorageBitDepth();
    #endif
    
            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;
    
    #if JVET_N0334_MVCLIPPING
            cMv[l][0].clipToStorageBitDepth();
    #endif
    
            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 );
    
    #if JVET_N0334_MVCLIPPING
            cMv[l][1].clipToStorageBitDepth();
    #endif
    
            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;
    
    #if JVET_N0481_BCW_CONSTRUCTED_AFFINE
      affMrgType.GBiIdx[affMrgType.numValidMergeCand] = gbiIdx;
    #endif
    
      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
        && puLeftBottom->mergeType == MRG_TYPE_DEFAULT_N
        )
      {
        npu[num++] = puLeftBottom;
        return num;
      }
    
      const PredictionUnit* puLeft = pu.cs->getPURestricted( posLB.offset( -1, 0 ), pu, pu.chType );
      if ( puLeft && puLeft->cu->affine
        && puLeft->mergeType == MRG_TYPE_DEFAULT_N
        )
      {
        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
        && puAboveRight->mergeType == MRG_TYPE_DEFAULT_N
        )
      {
        npu[num++] = puAboveRight;
        return num;
      }
    
      const PredictionUnit* puAbove = pu.cs->getPURestricted( posRT.offset( 0, -1 ), pu, pu.chType );
      if ( puAbove && puAbove->cu->affine
        && puAbove->mergeType == MRG_TYPE_DEFAULT_N
        )
      {
        npu[num++] = puAbove;
        return num;
      }
    
      const PredictionUnit *puAboveLeft = pu.cs->getPURestricted( posLT.offset( -1, -1 ), pu, pu.chType );
      if ( puAboveLeft && puAboveLeft->cu->affine
        && puAboveLeft->mergeType == MRG_TYPE_DEFAULT_N
        )
      {
        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;
        affMrgCtx.mergeType[i] = MRG_TYPE_DEFAULT_N;
        affMrgCtx.GBiIdx[i] = GBI_DEFAULT;
      }
    
      affMrgCtx.numValidMergeCand = 0;
      affMrgCtx.maxNumMergeCand = maxNumAffineMergeCand;
    
    
      bool enableSubPuMvp = slice.getSPS()->getSBTMVPEnabledFlag() && !(slice.getPOC() == slice.getRefPic(REF_PIC_LIST_0, 0)->getPOC() && slice.isIRAP());
    
      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 posCurLB = pu.Y().bottomLeft();
        MotionInfo miLeft;
    
    
        //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++;
        }
    
        mrgCtx.numValidMergeCand = pos;
    
        isAvailableSubPu = getInterMergeSubPuMvpCand( pu, mrgCtx, tmpLICFlag, pos
          , 0
        );
        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;
          }
        }
      }
    
    
      if ( slice.getSPS()->getUseAffine() )
    
      {
        ///> 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);
          affMrgCtx.GBiIdx[affMrgCtx.numValidMergeCand] = puNeigh->cu->GBiIdx;
    
          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 };
    
    #if JVET_N0481_BCW_CONSTRUCTED_AFFINE 
          int8_t neighGbi[4] = { GBI_DEFAULT };
    #endif
    
          // 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 );
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
    
            if ( puNeigh && CU::isInter( *puNeigh->cu )
              )
    
              mi[0] = puNeigh->getMotionInfo( pos );
    
    #if JVET_N0481_BCW_CONSTRUCTED_AFFINE 
              neighGbi[0] = puNeigh->cu->GBiIdx;
    #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 )
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
              )
    
              mi[1] = puNeigh->getMotionInfo( pos );
    
    #if JVET_N0481_BCW_CONSTRUCTED_AFFINE 
              neighGbi[1] = puNeigh->cu->GBiIdx;
    #endif
    
              break;
            }
          }
    
          // control point: LB A1->A0
          const Position posLB[2] = { pu.Y().bottomLeft().offset( -1, 0 ), pu.Y().bottomLeft().offset( -1, 1 ) };
          for ( int i = 0; i < 2; i++ )
          {
            const Position pos = posLB[i];
            const PredictionUnit* puNeigh = cs.getPURestricted( pos, pu, pu.chType );
    
            if ( puNeigh && CU::isInter( *puNeigh->cu )
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
              )
    
              mi[2] = puNeigh->getMotionInfo( pos );
    
    #if JVET_N0481_BCW_CONSTRUCTED_AFFINE 
              neighGbi[2] = puNeigh->cu->GBiIdx;
    #endif
    
              break;
            }
          }
    
          // control point: RB
          if ( slice.getEnableTMVPFlag() )
          {
            //>> 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 );
    
            const PreCalcValues& pcv = *cs.pcv;
            Position posC0;
            bool C0Avail = false;
    
            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
              {
                posC0 = posRB.offset( 4, 4 );
                // in the reference the CTU address is not set - thus probably resulting in no using this C0 possibility
              }
              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
              {
                posC0 = posRB.offset( 4, 4 );
                // same as for last column but not last row
              }
            }
    
            Mv        cColMv;
            int       refIdx = 0;
            bool      bExistMV = C0Avail && getColocatedMVP( pu, REF_PIC_LIST_0, posC0, cColMv, refIdx );
            if ( bExistMV )
            {
              mi[3].mv[0] = cColMv;
              mi[3].refIdx[0] = refIdx;
              mi[3].interDir = 1;
    
            }
    
            if ( slice.isInterB() )
            {
              bExistMV = C0Avail && getColocatedMVP( pu, REF_PIC_LIST_1, posC0, cColMv, refIdx );
              if ( bExistMV )
              {
                mi[3].mv[1] = cColMv;
                mi[3].refIdx[1] = refIdx;
                mi[3].interDir |= 2;
    
              }
            }
          }
    
          //-------------------  insert model  -------------------//
          int order[6] = { 0, 1, 2, 3, 4, 5 };
          int modelNum = 6;
          int model[6][4] = {
            { 0, 1, 2 },          // 0:  LT, RT, LB
            { 0, 1, 3 },          // 1:  LT, RT, RB
            { 0, 2, 3 },          // 2:  LT, LB, RB
            { 1, 2, 3 },          // 3:  RT, LB, RB
            { 0, 1 },             // 4:  LT, RT
            { 0, 2 },             // 5:  LT, LB
          };
    
          int verNum[6] = { 3, 3, 3, 3, 2, 2 };
    
          int startIdx = pu.cs->sps->getUseAffineType() ? 0 : 4;
    
          for ( int idx = startIdx; idx < modelNum; idx++ )
          {
            int modelIdx = order[idx];
    
    #if JVET_N0481_BCW_CONSTRUCTED_AFFINE
            getAffineControlPointCand(pu, mi, neighGbi, isAvailable, model[modelIdx], modelIdx, verNum[modelIdx], affMrgCtx);
    #else
    
            getAffineControlPointCand( pu, mi, isAvailable, model[modelIdx], modelIdx, verNum[modelIdx], affMrgCtx );
    
            if ( affMrgCtx.numValidMergeCand != 0 && affMrgCtx.numValidMergeCand - 1 == mrgCandIdx )
            {
              return;
            }
    
            // early termination
            if ( affMrgCtx.numValidMergeCand == maxNumAffineMergeCand )
            {
              return;
            }
          }
        }
        ///> End: Constructed affine candidates
      }
    
      ///> zero padding
      int cnt = affMrgCtx.numValidMergeCand;
      while ( cnt < maxNumAffineMergeCand )
      {
        for ( int mvNum = 0; mvNum < 3; mvNum++ )
        {
          affMrgCtx.mvFieldNeighbours[(cnt << 1) + 0][mvNum].setMvField( Mv( 0, 0 ), 0 );
        }
        affMrgCtx.interDirNeighbours[cnt] = 1;
    
        if ( slice.isInterB() )
        {
          for ( int mvNum = 0; mvNum < 3; mvNum++ )
          {
            affMrgCtx.mvFieldNeighbours[(cnt << 1) + 1][mvNum].setMvField( Mv( 0, 0 ), 0 );
          }
          affMrgCtx.interDirNeighbours[cnt] = 3;
        }
        affMrgCtx.affineType[cnt] = AFFINEMODEL_4PARAM;
    
    
    roberta's avatar
    roberta committed
        if ( cnt == mrgCandIdx )
    
    roberta's avatar
    roberta committed
        cnt++;
        affMrgCtx.numValidMergeCand++;
    
    
    void PU::setAllAffineMvField( PredictionUnit &pu, MvField *mvField, RefPicList eRefList )
    {
      // Set Mv
      Mv mv[3];
      for ( int i = 0; i < 3; i++ )
      {
        mv[i] = mvField[i].mv;
      }
      setAllAffineMv( pu, mv[0], mv[1], mv[2], eRefList );
    
      // Set RefIdx
      CHECK( mvField[0].refIdx != mvField[1].refIdx || mvField[0].refIdx != mvField[2].refIdx, "Affine mv corners don't have the same refIdx." );
      pu.refIdx[eRefList] = mvField[0].refIdx;
    }
    
    
    #if JVET_N0334_MVCLIPPING
    void PU::setAllAffineMv(PredictionUnit& pu, Mv affLT, Mv affRT, Mv affLB, RefPicList eRefList, bool setHighPrec, bool clipCPMVs)
    #else
    
    void PU::setAllAffineMv( PredictionUnit& pu, Mv affLT, Mv affRT, Mv affLB, RefPicList eRefList, bool setHighPrec)