UnitTools.cpp 167 KB
Newer Older
1 2 3 4 5
/* The copyright in this software is being made available under the BSD
 * License, included below. This software may be subject to other third party
 * and contributor rights, including patent rights, and no such rights are
 * granted under this license.
 *
6
 * Copyright (c) 2010-2019, ITU/ISO/IEC
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
 *    be used to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

/** \file     UnitTool.cpp
 *  \brief    defines operations for basic units
 */

#include "UnitTools.h"

#include "dtrace_next.h"

#include "Unit.h"
#include "Slice.h"
#include "Picture.h"

#include <utility>
#include <algorithm>

// CS tools


uint64_t CS::getEstBits(const CodingStructure &cs)
{
  return cs.fracBits >> SCALE_BITS;
}



bool CS::isDualITree( const CodingStructure &cs )
{
61
  return cs.slice->isIRAP() && !cs.pcv->ISingleTree;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
}

UnitArea CS::getArea( const CodingStructure &cs, const UnitArea &area, const ChannelType chType )
{
  return isDualITree( cs ) ? area.singleChan( chType ) : area;
}

// CU tools

bool CU::isIntra(const CodingUnit &cu)
{
  return cu.predMode == MODE_INTRA;
}

bool CU::isInter(const CodingUnit &cu)
{
  return cu.predMode == MODE_INTER;
}

Yu Han's avatar
Yu Han committed
81
#if JVET_M0483_IBC
Yu Han's avatar
Yu Han committed
82 83 84 85 86 87
bool CU::isIBC(const CodingUnit &cu)
{
  return cu.predMode == MODE_IBC;
}
#endif

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
bool CU::isRDPCMEnabled(const CodingUnit& cu)
{
  return cu.cs->sps->getSpsRangeExtension().getRdpcmEnabledFlag(cu.predMode == MODE_INTRA ? RDPCM_SIGNAL_IMPLICIT : RDPCM_SIGNAL_EXPLICIT);
}

bool CU::isLosslessCoded(const CodingUnit &cu)
{
  return cu.cs->pps->getTransquantBypassEnabledFlag() && cu.transQuantBypass;
}

bool CU::isSameSlice(const CodingUnit& cu, const CodingUnit& cu2)
{
  return cu.slice->getIndependentSliceIdx() == cu2.slice->getIndependentSliceIdx();
}

#if HEVC_TILES_WPP
bool CU::isSameTile(const CodingUnit& cu, const CodingUnit& cu2)
{
  return cu.tileIdx == cu2.tileIdx;
}

bool CU::isSameSliceAndTile(const CodingUnit& cu, const CodingUnit& cu2)
{
  return ( cu.slice->getIndependentSliceIdx() == cu2.slice->getIndependentSliceIdx() ) && ( cu.tileIdx == cu2.tileIdx );
}
#endif

bool CU::isSameCtu(const CodingUnit& cu, const CodingUnit& cu2)
{
  uint32_t ctuSizeBit = g_aucLog2[cu.cs->sps->getMaxCUWidth()];

  Position pos1Ctu(cu.lumaPos().x  >> ctuSizeBit, cu.lumaPos().y  >> ctuSizeBit);
  Position pos2Ctu(cu2.lumaPos().x >> ctuSizeBit, cu2.lumaPos().y >> ctuSizeBit);

  return pos1Ctu.x == pos2Ctu.x && pos1Ctu.y == pos2Ctu.y;
}

uint32_t CU::getIntraSizeIdx(const CodingUnit &cu)
{
  uint8_t uiWidth = cu.lumaSize().width;

  uint32_t  uiCnt   = 0;

  while (uiWidth)
  {
    uiCnt++;
    uiWidth >>= 1;
  }

  uiCnt -= 2;

  return uiCnt > 6 ? 6 : uiCnt;
}

bool CU::isLastSubCUOfCtu( const CodingUnit &cu )
{
  const SPS &sps      = *cu.cs->sps;
  const Area cuAreaY = CS::isDualITree( *cu.cs ) ? Area( recalcPosition( cu.chromaFormat, cu.chType, CHANNEL_TYPE_LUMA, cu.blocks[cu.chType].pos() ), recalcSize( cu.chromaFormat, cu.chType, CHANNEL_TYPE_LUMA, cu.blocks[cu.chType].size() ) ) : ( const Area& ) cu.Y();

  return ( ( ( ( cuAreaY.x + cuAreaY.width  ) & cu.cs->pcv->maxCUWidthMask  ) == 0 || cuAreaY.x + cuAreaY.width  == sps.getPicWidthInLumaSamples()  ) &&
           ( ( ( cuAreaY.y + cuAreaY.height ) & cu.cs->pcv->maxCUHeightMask ) == 0 || cuAreaY.y + cuAreaY.height == sps.getPicHeightInLumaSamples() ) );
}

uint32_t CU::getCtuAddr( const CodingUnit &cu )
{
  return getCtuAddr( cu.blocks[cu.chType].lumaPos(), *cu.cs->pcv );
}

int CU::predictQP( const CodingUnit& cu, const int prevQP )
{
  const CodingStructure &cs = *cu.cs;

#if ENABLE_WPP_PARALLELISM
  if( cs.sps->getSpsNext().getUseNextDQP() )
  {
    // Inter-CTU 2D "planar"   c(orner)  a(bove)
    // predictor arrangement:  b(efore)  p(rediction)

    // restrict the lookup, as it might cross CTU/slice/tile boundaries
    const CodingUnit *cuA = cs.getCURestricted( cu.blocks[cu.chType].pos().offset(  0, -1 ), cu, cu.chType );
    const CodingUnit *cuB = cs.getCURestricted( cu.blocks[cu.chType].pos().offset( -1,  0 ), cu, cu.chType );
    const CodingUnit *cuC = cs.getCURestricted( cu.blocks[cu.chType].pos().offset( -1, -1 ), cu, cu.chType );

    const int a = cuA ? cuA->qp : cs.slice->getSliceQpBase();
    const int b = cuB ? cuB->qp : cs.slice->getSliceQpBase();
    const int c = cuC ? cuC->qp : cs.slice->getSliceQpBase();

    return Clip3( ( a < b ? a : b ), ( a > b ? a : b ), a + b - c ); // derived from Martucci's Median Adaptive Prediction, 1990
  }

#endif
  // only predict within the same CTU, use HEVC's above+left prediction
  const int a = ( cu.blocks[cu.chType].y & ( cs.pcv->maxCUHeightMask >> getChannelTypeScaleY( cu.chType, cu.chromaFormat ) ) ) ? ( cs.getCU( cu.blocks[cu.chType].pos().offset( 0, -1 ), cu.chType ) )->qp : prevQP;
  const int b = ( cu.blocks[cu.chType].x & ( cs.pcv->maxCUWidthMask  >> getChannelTypeScaleX( cu.chType, cu.chromaFormat ) ) ) ? ( cs.getCU( cu.blocks[cu.chType].pos().offset( -1, 0 ), cu.chType ) )->qp : prevQP;

  return ( a + b + 1 ) >> 1;
}

186
bool CU::isQGStart( const CodingUnit& cu, Partitioner& partitioner )
187
{
188 189 190 191 192 193 194 195 196 197 198 199
  int maxDqpDepth = cu.slice->getPPS()->getMaxCuDQPDepth();
  if( partitioner.currDepth >= maxDqpDepth )
  {
    PartLevel splitAtMaxDepth = partitioner.getPartStack().at( maxDqpDepth ); 
    // the parent node of qtDepth + mttDepth == maxDqpDepth
    if( splitAtMaxDepth.parts[splitAtMaxDepth.idx].blocks[partitioner.chType].pos() == cu.blocks[partitioner.chType].pos() )
      return true;
    else
      return false;
  }
  else
    return true;
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
}

uint32_t CU::getNumPUs( const CodingUnit& cu )
{
  uint32_t cnt = 0;
  PredictionUnit *pu = cu.firstPU;

  do
  {
    cnt++;
  } while( ( pu != cu.lastPU ) && ( pu = pu->next ) );

  return cnt;
}

void CU::addPUs( CodingUnit& cu )
{
  cu.cs->addPU( CS::getArea( *cu.cs, cu, cu.chType ), cu.chType );
}


PartSplit CU::getSplitAtDepth( const CodingUnit& cu, const unsigned depth )
{
  if( depth >= cu.depth ) return CU_DONT_SPLIT;

  const PartSplit cuSplitType = PartSplit( ( cu.splitSeries >> ( depth * SPLIT_DMULT ) ) & SPLIT_MASK );

  if     ( cuSplitType == CU_QUAD_SPLIT    ) return CU_QUAD_SPLIT;

  else if( cuSplitType == CU_HORZ_SPLIT    ) return CU_HORZ_SPLIT;

  else if( cuSplitType == CU_VERT_SPLIT    ) return CU_VERT_SPLIT;

  else if( cuSplitType == CU_TRIH_SPLIT    ) return CU_TRIH_SPLIT;
  else if( cuSplitType == CU_TRIV_SPLIT    ) return CU_TRIV_SPLIT;
  else   { THROW( "Unknown split mode"    ); return CU_QUAD_SPLIT; }
}

bool CU::hasNonTsCodedBlock( const CodingUnit& cu )
{
  bool hasAnyNonTSCoded = false;

  for( auto &currTU : traverseTUs( cu ) )
  {
    for( uint32_t i = 0; i < ::getNumberValidTBlocks( *cu.cs->pcv ); i++ )
    {
Tung Nguyen's avatar
Tung Nguyen committed
246 247 248
#if JVET_M0464_UNI_MTS
      hasAnyNonTSCoded |= ( currTU.blocks[i].valid() && ( isLuma(ComponentID(i)) ? currTU.mtsIdx != 1 : true ) && TU::getCbf( currTU, ComponentID( i ) ) );
#else
249
      hasAnyNonTSCoded |= ( currTU.blocks[i].valid() && !currTU.transformSkip[i] && TU::getCbf( currTU, ComponentID( i ) ) );
Tung Nguyen's avatar
Tung Nguyen committed
250
#endif
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    }
  }

  return hasAnyNonTSCoded;
}

uint32_t CU::getNumNonZeroCoeffNonTs( const CodingUnit& cu )
{
  uint32_t count = 0;
  for( auto &currTU : traverseTUs( cu ) )
  {
    count += TU::getNumNonZeroCoeffsNonTS( currTU );
  }

  return count;
}




PUTraverser CU::traversePUs( CodingUnit& cu )
{
  return PUTraverser( cu.firstPU, cu.lastPU->next );
}

TUTraverser CU::traverseTUs( CodingUnit& cu )
{
  return TUTraverser( cu.firstTU, cu.lastTU->next );
}

cPUTraverser CU::traversePUs( const CodingUnit& cu )
{
  return cPUTraverser( cu.firstPU, cu.lastPU->next );
}

cTUTraverser CU::traverseTUs( const CodingUnit& cu )
{
  return cTUTraverser( cu.firstTU, cu.lastTU->next );
}

// PU tools

int PU::getIntraMPMs( const PredictionUnit &pu, unsigned* mpm, const ChannelType &channelType /*= CHANNEL_TYPE_LUMA*/ )
{
295
  const int numMPMs = NUM_MOST_PROBABLE_MODES;
296
  const int extendRefLine = (channelType == CHANNEL_TYPE_LUMA) ? pu.multiRefIdx : 0;
297 298
  {
    int numCand      = -1;
ling's avatar
ling committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    int leftIntraDir = PLANAR_IDX, aboveIntraDir = PLANAR_IDX;

    const CompArea &area = pu.block(getFirstComponentOfChannel(channelType));
    const Position posRT = area.topRight();
    const Position posLB = area.bottomLeft();

    // Get intra direction of left PU
    const PredictionUnit *puLeft = pu.cs->getPURestricted(posLB.offset(-1, 0), pu, channelType);
    if (puLeft && CU::isIntra(*puLeft->cu))
    {
      leftIntraDir = puLeft->intraDir[channelType];
    }

    // Get intra direction of above PU
    const PredictionUnit *puAbove = pu.cs->getPURestricted(posRT.offset(0, -1), pu, channelType);
    if (puAbove && CU::isIntra(*puAbove->cu) && CU::isSameCtu(*pu.cu, *puAbove->cu))
    {
      aboveIntraDir = puAbove->intraDir[channelType];
    }

    CHECK(2 >= numMPMs, "Invalid number of most probable modes");

ling's avatar
ling committed
321
    const int offset = (int)NUM_LUMA_MODE - 6;
ling's avatar
ling committed
322 323
    const int mod = offset + 3;

324
    if (extendRefLine)
ling's avatar
ling committed
325
    {
326 327 328
      int modeIdx = 0;
      int angularMode[2] = { 0, 0 };

ling's avatar
ling committed
329 330
      if (leftIntraDir > DC_IDX)
      {
331 332
        angularMode[modeIdx++] = leftIntraDir;
      }
333
      if (aboveIntraDir > DC_IDX && aboveIntraDir != leftIntraDir)
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
      {
        angularMode[modeIdx++] = aboveIntraDir;
      }
      if (modeIdx == 0)
      {
        mpm[0] = VER_IDX;
        mpm[1] = HOR_IDX;
        mpm[2] = 2;
        mpm[3] = DIA_IDX;
        mpm[4] = VDIA_IDX;
        mpm[5] = 26;
      }
      else if (modeIdx == 1)
      {
        mpm[0] = angularMode[0];
        mpm[1] = ((angularMode[0] + offset) % mod) + 2;
        mpm[2] = ((angularMode[0] - 1) % mod) + 2;
        mpm[3] = ((angularMode[0] + offset - 1) % mod) + 2;
        mpm[4] = (angularMode[0] % mod) + 2;
        mpm[5] = ((angularMode[0] + offset - 2) % mod) + 2;
      }
      else
      {
        mpm[0] = angularMode[0];
        mpm[1] = angularMode[1];
        int maxCandModeIdx = mpm[0] > mpm[1] ? 0 : 1;
        int minCandModeIdx = 1 - maxCandModeIdx;
        if (mpm[maxCandModeIdx] - mpm[minCandModeIdx] == 1)
        {
          mpm[2] = ((angularMode[minCandModeIdx] + offset) % mod) + 2;
          mpm[3] = ((angularMode[maxCandModeIdx] - 1) % mod) + 2;
          mpm[4] = ((angularMode[minCandModeIdx] + offset - 1) % mod) + 2;
          mpm[5] = ( angularMode[maxCandModeIdx] % mod) + 2;
        }
        else if (mpm[maxCandModeIdx] - mpm[minCandModeIdx] >= 62)
        {
          mpm[2] = ((angularMode[minCandModeIdx] - 1) % mod) + 2;
          mpm[3] = ((angularMode[maxCandModeIdx] + offset) % mod) + 2;
          mpm[4] = ((angularMode[minCandModeIdx]) % mod) + 2;
          mpm[5] = ((angularMode[maxCandModeIdx] + offset - 1) % mod) + 2;
        }
        else if (mpm[maxCandModeIdx] - mpm[minCandModeIdx] == 2)
        {
          mpm[2] = ((angularMode[minCandModeIdx] - 1) % mod) + 2;
          mpm[3] = ((angularMode[minCandModeIdx] + offset) % mod) + 2;
          mpm[4] = ((angularMode[maxCandModeIdx] - 1) % mod) + 2;
          mpm[5] = ((angularMode[minCandModeIdx] + offset - 1) % mod) + 2;
        }
        else
        {
          mpm[2] = ((angularMode[minCandModeIdx] + offset) % mod) + 2;
          mpm[3] = ((angularMode[minCandModeIdx] - 1) % mod) + 2;
          mpm[4] = ((angularMode[maxCandModeIdx] + offset) % mod) + 2;
          mpm[5] = ((angularMode[maxCandModeIdx] - 1) % mod) + 2;
        }
ling's avatar
ling committed
389 390
      }
    }
391
    else
ling's avatar
ling committed
392 393
    {
      mpm[0] = leftIntraDir;
394 395 396 397 398
      mpm[1] = (mpm[0] == PLANAR_IDX) ? DC_IDX : PLANAR_IDX;
      mpm[2] = VER_IDX;
      mpm[3] = HOR_IDX;
      mpm[4] = VER_IDX - 4;
      mpm[5] = VER_IDX + 4;
ling's avatar
ling committed
399

400
      if (leftIntraDir == aboveIntraDir)
ling's avatar
ling committed
401
      {
402 403
        numCand = 1;
        if (leftIntraDir > DC_IDX)
ling's avatar
ling committed
404
        {
405 406 407 408 409 410
          mpm[0] = leftIntraDir;
          mpm[1] = PLANAR_IDX;
          mpm[2] = DC_IDX;
          mpm[3] = ((leftIntraDir + offset) % mod) + 2;
          mpm[4] = ((leftIntraDir - 1) % mod) + 2;
          mpm[5] = ((leftIntraDir + offset - 1) % mod) + 2;
ling's avatar
ling committed
411 412
        }
      }
413
      else //L!=A
ling's avatar
ling committed
414
      {
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
        numCand = 2;
        mpm[0] = leftIntraDir;
        mpm[1] = aboveIntraDir;
        bool maxCandModeIdx = mpm[0] > mpm[1] ? 0 : 1;

        if ((leftIntraDir > DC_IDX) && (aboveIntraDir > DC_IDX))
        {
          mpm[2] = PLANAR_IDX;
          mpm[3] = DC_IDX;
          if ((mpm[maxCandModeIdx] - mpm[!maxCandModeIdx] < 63) && (mpm[maxCandModeIdx] - mpm[!maxCandModeIdx] > 1))
          {
            mpm[4] = ((mpm[maxCandModeIdx] + offset) % mod) + 2;
            mpm[5] = ((mpm[maxCandModeIdx] - 1) % mod) + 2;
          }
          else
          {
            mpm[4] = ((mpm[maxCandModeIdx] + offset - 1) % mod) + 2;
            mpm[5] = ((mpm[maxCandModeIdx]) % mod) + 2;
          }
        }
        else if (leftIntraDir + aboveIntraDir >= 2)
        {
          mpm[2] = (mpm[!maxCandModeIdx] == PLANAR_IDX) ? DC_IDX : PLANAR_IDX;
          mpm[3] = ((mpm[maxCandModeIdx] + offset) % mod) + 2;
          mpm[4] = ((mpm[maxCandModeIdx] - 1) % mod) + 2;
          mpm[5] = ((mpm[maxCandModeIdx] + offset - 1) % mod) + 2;
        }
ling's avatar
ling committed
442 443
      }
    }
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    for (int i = 0; i < numMPMs; i++)
    {
      CHECK(mpm[i] >= NUM_LUMA_MODE, "Invalid MPM");
    }
    CHECK(numCand == 0, "No candidates found");
    return numCand;
  }
}


void PU::getIntraChromaCandModes( const PredictionUnit &pu, unsigned modeList[NUM_CHROMA_MODE] )
{
  {
    modeList[  0 ] = PLANAR_IDX;
    modeList[  1 ] = VER_IDX;
    modeList[  2 ] = HOR_IDX;
    modeList[  3 ] = DC_IDX;
    modeList[4] = LM_CHROMA_IDX;
Xiang Ma's avatar
Xiang Ma committed
462 463 464
    modeList[5] = MDLM_L_IDX;
    modeList[6] = MDLM_T_IDX;
    modeList[7] = DM_CHROMA_IDX;
465

Li's avatar
Li committed
466
    Position topLeftPos = pu.blocks[pu.chType].lumaPos();
Li's avatar
Li committed
467
    Position refPos = topLeftPos.offset( pu.blocks[pu.chType].lumaSize().width >> 1, pu.blocks[pu.chType].lumaSize().height >> 1 );
Li's avatar
Li committed
468
    const PredictionUnit *lumaPU = CS::isDualITree( *pu.cs ) ? pu.cs->picture->cs->getPU( refPos, CHANNEL_TYPE_LUMA ) : &pu;
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    const uint32_t lumaMode = lumaPU->intraDir[CHANNEL_TYPE_LUMA];
    for( int i = 0; i < 4; i++ )
    {
      if( lumaMode == modeList[i] )
      {
        modeList[i] = VDIA_IDX;
        break;
      }
    }
  }
}


bool PU::isLMCMode(unsigned mode)
{
Xiang Ma's avatar
Xiang Ma committed
484
  return (mode >= LM_CHROMA_IDX && mode <= MDLM_T_IDX);
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
}
bool PU::isLMCModeEnabled(const PredictionUnit &pu, unsigned mode)
{
  if ( pu.cs->sps->getSpsNext().getUseLMChroma() )
  {
    return true;
  }
  return false;
}

int PU::getLMSymbolList(const PredictionUnit &pu, int *pModeList)
{
  int iIdx = 0;

  pModeList[ iIdx++ ] = LM_CHROMA_IDX;
    pModeList[ iIdx++ ] = -1;
Xiang Ma's avatar
Xiang Ma committed
501 502
  pModeList[iIdx++] = MDLM_L_IDX;
  pModeList[iIdx++] = MDLM_T_IDX;
503 504 505 506 507 508 509 510 511 512
  return iIdx;
}



bool PU::isChromaIntraModeCrossCheckMode( const PredictionUnit &pu )
{
  return pu.intraDir[CHANNEL_TYPE_CHROMA] == DM_CHROMA_IDX;
}

513 514
int PU::getMHIntraMPMs(const PredictionUnit &pu, unsigned* mpm, const ChannelType &channelType /*= CHANNEL_TYPE_LUMA*/, const bool isChromaMDMS /*= false*/, const unsigned startIdx /*= 0*/)
{
515
  const int numMPMs = 3; // Multi-hypothesis intra uses only 3 MPM
516 517 518 519 520 521 522 523 524 525
  {
    int numCand = -1;
    uint32_t leftIntraDir = DC_IDX, aboveIntraDir = DC_IDX;

    const CompArea& area = pu.block(getFirstComponentOfChannel(channelType));
    const Position& pos = area.pos();

    // Get intra direction of left PU
    const PredictionUnit *puLeft = pu.cs->getPURestricted(pos.offset(-1, 0), pu, channelType);

526
    if (puLeft && (CU::isIntra(*puLeft->cu) || puLeft->mhIntraFlag))
527 528 529 530 531 532 533 534 535 536 537 538
    {
      leftIntraDir = puLeft->intraDir[channelType];

      if (isChroma(channelType) && leftIntraDir == DM_CHROMA_IDX)
      {
        leftIntraDir = puLeft->intraDir[0];
      }
    }

    // Get intra direction of above PU
    const PredictionUnit* puAbove = pu.cs->getPURestricted(pos.offset(0, -1), pu, channelType);

539
    if (puAbove && (CU::isIntra(*puAbove->cu) || puAbove->mhIntraFlag) && CU::isSameCtu(*pu.cu, *puAbove->cu))
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
    {
      aboveIntraDir = puAbove->intraDir[channelType];

      if (isChroma(channelType) && aboveIntraDir == DM_CHROMA_IDX)
      {
        aboveIntraDir = puAbove->intraDir[0];
      }
    }

    CHECK(2 >= numMPMs, "Invalid number of most probable modes");

    uint32_t leftIntraDir2 = leftIntraDir;
    uint32_t aboveIntraDir2 = aboveIntraDir;

    leftIntraDir2 = (leftIntraDir2 > DC_IDX) ? ((leftIntraDir2 <= DIA_IDX) ? HOR_IDX : VER_IDX) : leftIntraDir2;
    aboveIntraDir2 = (aboveIntraDir2 > DC_IDX) ? ((aboveIntraDir2 <= DIA_IDX) ? HOR_IDX : VER_IDX) : aboveIntraDir2;

    if (leftIntraDir2 == aboveIntraDir2)
    {
      numCand = 1;

      if (leftIntraDir2 > DC_IDX) // angular modes
      {
        mpm[0] = leftIntraDir2;
        mpm[1] = PLANAR_IDX;
        mpm[2] = DC_IDX;
      }
      else //non-angular
      {
        mpm[0] = PLANAR_IDX;
        mpm[1] = DC_IDX;
        mpm[2] = VER_IDX;
      }
    }
    else
    {
      numCand = 2;

      mpm[0] = leftIntraDir2;
      mpm[1] = aboveIntraDir2;

      if (leftIntraDir2 && aboveIntraDir2) //both modes are non-planar
      {
        mpm[2] = PLANAR_IDX;
      }
      else
      {
        mpm[2] = (leftIntraDir2 + aboveIntraDir2) < 2 ? VER_IDX : DC_IDX;
      }
    }
    int narrowCase = getNarrowShape(pu.lwidth(), pu.lheight());
    if (narrowCase > 0)
    {
      bool isMPM[NUM_LUMA_MODE];
      for (int idx = 0; idx < NUM_LUMA_MODE; idx++)
      {
        isMPM[idx] = false;
      }
      for (int idx = 0; idx < numMPMs; idx++)
      {
        isMPM[mpm[idx]] = true;
      }
      if (narrowCase == 1 && isMPM[HOR_IDX])
      {
        for (int idx = 0; idx < numMPMs; idx++)
        {
          if (mpm[idx] == HOR_IDX)
          {
            if (!isMPM[PLANAR_IDX])
              mpm[idx] = PLANAR_IDX;
            else if (!isMPM[DC_IDX])
              mpm[idx] = DC_IDX;
            else if (!isMPM[VER_IDX])
              mpm[idx] = VER_IDX;
            break;
          }
        }
      }
      if (narrowCase == 2 && isMPM[VER_IDX])
      {
        for (int idx = 0; idx < numMPMs; idx++)
        {
          if (mpm[idx] == VER_IDX)
          {
            if (!isMPM[PLANAR_IDX])
              mpm[idx] = PLANAR_IDX;
            else if (!isMPM[DC_IDX])
              mpm[idx] = DC_IDX;
            else if (!isMPM[HOR_IDX])
              mpm[idx] = HOR_IDX;
            break;
          }
        }
      }
    }
    CHECK(numCand == 0, "No candidates found");
    CHECK(mpm[0] == mpm[1] || mpm[0] == mpm[2] || mpm[2] == mpm[1], "redundant MPM");
    return numCand;
  }
}
int PU::getNarrowShape(const int width, const int height)
{
  int longSide = (width > height) ? width : height;
  int shortSide = (width > height) ? height : width;
  if (longSide > (2 * shortSide))
  {
    if (longSide == width)
      return 1;
    else
      return 2;
  }
  else
  {
    return 0;
  }
}

657 658 659 660 661 662
uint32_t PU::getFinalIntraMode( const PredictionUnit &pu, const ChannelType &chType )
{
  uint32_t uiIntraMode = pu.intraDir[chType];

  if( uiIntraMode == DM_CHROMA_IDX && !isLuma( chType ) )
  {
Li's avatar
Li committed
663
    Position topLeftPos = pu.blocks[pu.chType].lumaPos();
Li's avatar
Li committed
664
    Position refPos = topLeftPos.offset( pu.blocks[pu.chType].lumaSize().width >> 1, pu.blocks[pu.chType].lumaSize().height >> 1 );
Li's avatar
Li committed
665
    const PredictionUnit &lumaPU = CS::isDualITree( *pu.cs ) ? *pu.cs->picture->cs->getPU( refPos, CHANNEL_TYPE_LUMA ) : *pu.cs->getPU( topLeftPos, CHANNEL_TYPE_LUMA );
666

667 668 669 670 671 672 673 674 675
    uiIntraMode = lumaPU.intraDir[0];
  }
  if( pu.chromaFormat == CHROMA_422 && !isLuma( chType ) )
  {
    uiIntraMode = g_chroma422IntraAngleMappingTable[uiIntraMode];
  }
  return uiIntraMode;
}

676 677 678 679 680 681 682 683
bool PU::xCheckSimilarMotion(const int mergeCandIndex, const int prevCnt, const MergeCtx mergeCandList, bool hasPruned[MRG_MAX_NUM_CANDS])
{
  for (uint32_t ui = 0; ui < prevCnt; ui++)
  {
    if (hasPruned[ui])
    {
      continue;
    }
684
    if (mergeCandList.interDirNeighbours[ui] == mergeCandList.interDirNeighbours[mergeCandIndex])
685 686 687
    {
      if (mergeCandList.interDirNeighbours[ui] == 3)
      {
Li's avatar
Li committed
688 689
        int offset0 = (ui * 2);
        int offset1 = (mergeCandIndex * 2);
690 691 692 693 694 695 696 697 698 699 700 701
        if (mergeCandList.mvFieldNeighbours[offset0].refIdx == mergeCandList.mvFieldNeighbours[offset1].refIdx &&
            mergeCandList.mvFieldNeighbours[offset0 + 1].refIdx == mergeCandList.mvFieldNeighbours[offset1 + 1].refIdx &&
            mergeCandList.mvFieldNeighbours[offset0].mv == mergeCandList.mvFieldNeighbours[offset1].mv &&
            mergeCandList.mvFieldNeighbours[offset0 + 1].mv == mergeCandList.mvFieldNeighbours[offset1 + 1].mv
          )
        {
          hasPruned[ui] = true;
          return true;
        }
      }
      else
      {
Li's avatar
Li committed
702 703
        int offset0 = (ui * 2) + mergeCandList.interDirNeighbours[ui] - 1;
        int offset1 = (mergeCandIndex * 2) + mergeCandList.interDirNeighbours[ui] - 1;
704
        if (mergeCandList.mvFieldNeighbours[offset0].refIdx == mergeCandList.mvFieldNeighbours[offset1].refIdx &&
705
            mergeCandList.mvFieldNeighbours[offset0].mv == mergeCandList.mvFieldNeighbours[offset1].mv
706 707 708 709 710 711 712 713
          )
        {
          hasPruned[ui] = true;
          return true;
        }
      }
    }
  }
714

715 716
  return false;
}
717

718
#if JVET_L0090_PAIR_AVG
Xiaozhong Xu's avatar
Xiaozhong Xu committed
719 720

bool PU::addMergeHMVPCand(const Slice &slice, MergeCtx& mrgCtx, bool canFastExit, const int& mrgCandIdx, const uint32_t maxNumMergeCandMin1, int &cnt, const int prevCnt, bool isAvailableSubPu, unsigned subPuMvpPos
Yu Han's avatar
Yu Han committed
721
#if JVET_M0483_IBC==0
Xiaozhong Xu's avatar
Xiaozhong Xu committed
722
  , int mmvdList
Yu Han's avatar
Yu Han committed
723
#endif
Yu Han's avatar
Yu Han committed
724
#if JVET_M0483_IBC
Yu Han's avatar
Yu Han committed
725
  , bool ibcFlag
Yu Han's avatar
Yu Han committed
726
#endif
727 728 729
#if JVET_M0170_MRG_SHARELIST
  , bool isShared
#endif
Xiaozhong Xu's avatar
Xiaozhong Xu committed
730
)
731
#else
Xiaozhong Xu's avatar
Xiaozhong Xu committed
732 733 734 735

bool PU::addMergeHMVPCand(const Slice &slice, MergeCtx& mrgCtx, bool isCandInter[MRG_MAX_NUM_CANDS], bool canFastExit, const int& mrgCandIdx, const uint32_t maxNumMergeCandMin1, int &cnt, const int prevCnt, bool isAvailableSubPu, unsigned subPuMvpPos
  , int mmvdList
)
736 737
#endif
{
Yu Han's avatar
Yu Han committed
738
#if JVET_M0483_IBC==0
Yu Han's avatar
Yu Han committed
739
  int mrgCandIdxIBC = mrgCandIdx;
Yu Han's avatar
Yu Han committed
740
#endif
741 742
  MotionInfo miNeighbor;
  bool hasPruned[MRG_MAX_NUM_CANDS];
Li's avatar
Li committed
743
  memset(hasPruned, 0, MRG_MAX_NUM_CANDS * sizeof(bool));
744 745 746 747
  if (isAvailableSubPu)
  {
    hasPruned[subPuMvpPos] = true;
  }
748
#if JVET_M0170_MRG_SHARELIST
Yu Han's avatar
Yu Han committed
749
#if JVET_M0483_IBC
Yu Han's avatar
Yu Han committed
750 751
  int num_avai_candInLUT = ibcFlag ? slice.getAvailableLUTIBCMrgNum() : (isShared ? slice.getAvailableLUTBkupMrgNum() : slice.getAvailableLUTMrgNum());
  int offset = ibcFlag ? MAX_NUM_HMVP_CANDS : 0;
Yu Han's avatar
Yu Han committed
752 753 754 755
#else
  int num_avai_candInLUT = (isShared ? slice.getAvailableLUTBkupMrgNum() : slice.getAvailableLUTMrgNum());
#endif
#else
Yu Han's avatar
Yu Han committed
756
#if JVET_M0483_IBC
Yu Han's avatar
Yu Han committed
757 758
  int num_avai_candInLUT = ibcFlag ? slice.getAvailableLUTIBCMrgNum() : slice.getAvailableLUTMrgNum();
  int offset = ibcFlag ? MAX_NUM_HMVP_CANDS : 0;
759
#else
760
  int num_avai_candInLUT = slice.getAvailableLUTMrgNum();
Yu Han's avatar
Yu Han committed
761
#endif
762 763
#endif

764 765
  for (int mrgIdx = 1; mrgIdx <= num_avai_candInLUT; mrgIdx++)
  {
766
#if JVET_M0170_MRG_SHARELIST
Yu Han's avatar
Yu Han committed
767
#if JVET_M0483_IBC
Yu Han's avatar
Yu Han committed
768
    miNeighbor = ibcFlag ? slice.getMotionInfoFromLUTs(num_avai_candInLUT - mrgIdx + offset)
Yu Han's avatar
Yu Han committed
769
                          : (isShared ? slice.getMotionInfoFromLUTBkup(num_avai_candInLUT - mrgIdx) : slice.getMotionInfoFromLUTs(num_avai_candInLUT - mrgIdx));
Yu Han's avatar
Yu Han committed
770 771 772 773
#else
    miNeighbor = isShared ? slice.getMotionInfoFromLUTBkup(num_avai_candInLUT - mrgIdx) : slice.getMotionInfoFromLUTs(num_avai_candInLUT - mrgIdx);
#endif
#else
Yu Han's avatar
Yu Han committed
774
#if JVET_M0483_IBC
Yu Han's avatar
Yu Han committed
775
    miNeighbor = slice.getMotionInfoFromLUTs(num_avai_candInLUT - mrgIdx + offset);
776
#else
777
    miNeighbor = slice.getMotionInfoFromLUTs(num_avai_candInLUT - mrgIdx);
Yu Han's avatar
Yu Han committed
778
#endif
779
#endif
780
    mrgCtx.interDirNeighbours[cnt] = miNeighbor.interDir;
781 782 783
#if JVET_M0264_HMVP_WITH_GBIIDX
    mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? miNeighbor.GBiIdx : GBI_DEFAULT;
#endif
784 785 786 787 788 789 790 791 792 793
    mrgCtx.mvFieldNeighbours[cnt << 1].setMvField(miNeighbor.mv[0], miNeighbor.refIdx[0]);
    if (slice.isInterB())
    {
      mrgCtx.mvFieldNeighbours[(cnt << 1) + 1].setMvField(miNeighbor.mv[1], miNeighbor.refIdx[1]);
    }
    if (!xCheckSimilarMotion(cnt, prevCnt, mrgCtx, hasPruned))
    {
#if !JVET_L0090_PAIR_AVG
      isCandInter[cnt] = true;
#endif
Yu Han's avatar
Yu Han committed
794
#if JVET_M0483_IBC==0
Xiaozhong Xu's avatar
Xiaozhong Xu committed
795 796
      if (miNeighbor.interDir == 1 && slice.getRefPic(REF_PIC_LIST_0, miNeighbor.refIdx[0])->getPOC() == slice.getPOC())
      {
Yu Han's avatar
Yu Han committed
797
        mrgCtx.mrgTypeNeighbours[cnt] = MRG_TYPE_IBC;
Xiaozhong Xu's avatar
Xiaozhong Xu committed
798
        if(mmvdList != 0 && mrgCandIdx != -1)
Yu Han's avatar
Yu Han committed
799
          mrgCandIdxIBC++;
Xiaozhong Xu's avatar
Xiaozhong Xu committed
800
      }
Yu Han's avatar
Yu Han committed
801
      if (mrgCandIdxIBC == cnt && canFastExit)
Yu Han's avatar
Yu Han committed
802 803 804
#else
      if (mrgCandIdx == cnt && canFastExit)
#endif
805 806 807 808 809 810 811 812 813 814 815 816
      {
        return true;
      }
      cnt ++;
      if (cnt  == maxNumMergeCandMin1)
      {
        break;
      }
    }
  }
  return false;
}
817

Yu Han's avatar
Yu Han committed
818
#if JVET_M0483_IBC
Yu Han's avatar
Yu Han committed
819 820 821 822 823 824 825 826 827 828 829 830
void PU::getIBCMergeCandidates(const PredictionUnit &pu, MergeCtx& mrgCtx, 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;

  for (uint32_t ui = 0; ui < maxNumMergeCand; ++ui)
  {
    mrgCtx.GBiIdx[ui] = GBI_DEFAULT;
    mrgCtx.interDirNeighbours[ui] = 0;
    mrgCtx.mrgTypeNeighbours[ui] = MRG_TYPE_IBC;
Yu Han's avatar
Yu Han committed
831 832
    mrgCtx.mvFieldNeighbours[ui * 2].refIdx = NOT_VALID;
    mrgCtx.mvFieldNeighbours[ui * 2 + 1].refIdx = NOT_VALID;
Yu Han's avatar
Yu Han committed
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
  }

  mrgCtx.numValidMergeCand = maxNumMergeCand;
  // compute the location of the current PU

  int cnt = 0;

  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::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;
  }

  // above right
  const PredictionUnit *puAboveRight = cs.getPURestricted(posRT.offset(1, -1), pu, pu.chType);
  bool isAvailableB0 = puAboveRight && isDiffMER(pu, *puAboveRight) && CU::isIBC(*puAboveRight->cu);
  if (isAvailableB0)
  {
    miAboveRight = puAboveRight->getMotionInfo(posRT.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[cnt] = miAboveRight.interDir;
      // get Mv from Above-right
      mrgCtx.mvFieldNeighbours[cnt << 1].setMvField(miAboveRight.mv[0], miAboveRight.refIdx[0]);

      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::isIBC(*puLeftBottom->cu);
  if (isAvailableA0)
  {
    miBelowLeft = puLeftBottom->getMotionInfo(posLB.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[cnt] = miBelowLeft.interDir;
      mrgCtx.mvFieldNeighbours[cnt << 1].setMvField(miBelowLeft.mv[0], miBelowLeft.refIdx[0]);
      if (mrgCandIdx == cnt && canFastExit)
      {
        return;
      }

      cnt++;
    }
  }
  // early termination
  if (cnt == maxNumMergeCand)
  {
    return;
  }

  // above left
  if (cnt < 4)
  {
    const PredictionUnit *puAboveLeft = cs.getPURestricted(posLT.offset(-1, -1), pu, pu.chType);
    bool isAvailableB2 = puAboveLeft && isDiffMER(pu, *puAboveLeft) && CU::isIBC(*puAboveLeft->cu);
    if (isAvailableB2)
    {
      miAboveLeft = puAboveLeft->getMotionInfo(posLT.offset(-1, -1));

#if HM_JEM_MERGE_CANDS
      if ((!isAvailableA1 || (miLeft != miAboveLeft)) && (!isAvailableB1 || (miAbove != miAboveLeft)) && (!isAvailableA0 || (miBelowLeft != miAboveLeft)) && (!isAvailableB0 || (miAboveRight != miAboveLeft)))
#else
      if ((!isAvailableA1 || (miLeft != miAboveLeft)) && (!isAvailableB1 || (miAbove != miAboveLeft)))
#endif
      {
        // get Inter Dir
        mrgCtx.interDirNeighbours[cnt] = miAboveLeft.interDir;
        mrgCtx.mvFieldNeighbours[cnt << 1].setMvField(miAboveLeft.mv[0], miAboveLeft.refIdx[0]);
        if (mrgCandIdx == cnt && canFastExit)
        {
          return;
        }

        cnt++;
      }
    }
  }
  // early termination
  if (cnt == maxNumMergeCand)
  {
    return;
  }

  int maxNumMergeCandMin1 = maxNumMergeCand - 1;
  if (cnt != maxNumMergeCandMin1)
  {
    bool isAvailableSubPu = false;
    unsigned subPuMvpPos = 0;

#if JVET_L0090_PAIR_AVG
    bool bFound = addMergeHMVPCand(slice, mrgCtx, canFastExit
      , mrgCandIdx
      , maxNumMergeCandMin1, cnt, cnt, isAvailableSubPu, subPuMvpPos
Yu Han's avatar
Yu Han committed
1004
#if JVET_M0483_IBC
Yu Han's avatar
Yu Han committed
1005 1006 1007
      , true
#endif
#if JVET_M0170_MRG_SHARELIST
Yu Han's avatar
Yu Han committed
1008
      , false
Yu Han's avatar
Yu Han committed
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
#endif
    );
#else
    bool bFound = addMergeHMVPCand(slice, mrgCtx, isCandInter, canFastExit
      , mrgCandIdx
      , maxNumMergeCandMin1, cnt, cnt, isAvailableSubPu, subPuMvpPos
    );
#endif
    if (bFound)
    {
      return;
    }
  }

#if JVET_L0090_PAIR_AVG
  // pairwise-average candidates
Yu Han's avatar
Yu Han committed
1025
#if JVET_M0193_PAIR_AVG_REDUCTION==0
Yu Han's avatar
Yu Han committed
1026 1027 1028 1029 1030 1031 1032 1033 1034
    const int cutoff = std::min(cnt, 4);
    const int end = cutoff * (cutoff - 1) / 2;
    constexpr int PRIORITY_LIST0[] = { 0, 0, 1, 0, 1, 2 };
    constexpr int PRIORITY_LIST1[] = { 1, 2, 2, 3, 3, 3 };

    for (int idx = 0; idx < end && cnt != maxNumMergeCand; idx++)
    {
      const int i = PRIORITY_LIST0[idx];
      const int j = PRIORITY_LIST1[idx];
Yu Han's avatar
Yu Han committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
#else
    if (cnt>1 && cnt <maxNumMergeCand)
    {
#endif
       mrgCtx.mvFieldNeighbours[cnt * 2    ].setMvField(Mv(0, 0), NOT_VALID);
       mrgCtx.mvFieldNeighbours[cnt * 2 + 1].setMvField(Mv(0, 0), NOT_VALID);
  
#if JVET_M0193_PAIR_AVG_REDUCTION
       const Mv& MvI = mrgCtx.mvFieldNeighbours[0 * 2].mv;
       const Mv& MvJ = mrgCtx.mvFieldNeighbours[1 * 2].mv;
#else
       const Mv& MvI = mrgCtx.mvFieldNeighbours[i * 2 ].mv;
       const Mv& MvJ = mrgCtx.mvFieldNeighbours[j * 2 ].mv;
#endif
       // average two MVs
       Mv avgMv = MvI;
Yu Han's avatar
Yu Han committed
1051

Yu Han's avatar
Yu Han committed
1052 1053
       avgMv += MvJ;
       mrgCtx.mrgTypeNeighbours[cnt] = MRG_TYPE_IBC;
Yu Han's avatar
Yu Han committed
1054
#if JVET_M0265_MV_ROUNDING_CLEANUP
Yu Han's avatar
Yu Han committed
1055
       roundAffineMv(avgMv.hor, avgMv.ver, 1);
Yu Han's avatar
Yu Han committed
1056
#else
Yu Han's avatar
Yu Han committed
1057 1058
       avgMv.setHor(avgMv.getHor() / 2);
       avgMv.setVer(avgMv.getVer() / 2);
Yu Han's avatar
Yu Han committed
1059
#endif
Yu Han's avatar
Yu Han committed
1060 1061 1062
      mrgCtx.mvFieldNeighbours[cnt * 2 ].setMvField(avgMv, MAX_NUM_REF);
      mrgCtx.interDirNeighbours[cnt] = 1;
      cnt++;
Yu Han's avatar
Yu Han committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    }

    // early termination
    if (cnt == maxNumMergeCand)
    {
      return;
    }
#endif

  mrgCtx.numValidMergeCand = cnt;

}
#endif

1077
void PU::getInterMergeCandidates( const PredictionUnit &pu, MergeCtx& mrgCtx,
1078 1079
                                 int mmvdList,
                                 const int& mrgCandIdx )
1080 1081 1082 1083 1084 1085
{
  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;

1086 1087
#if !JVET_L0090_PAIR_AVG
  // this variable is unused if remove HEVC combined candidates
1088
  bool isCandInter[MRG_MAX_NUM_CANDS];
1089
#endif
1090 1091 1092

  for (uint32_t ui = 0; ui < maxNumMergeCand; ++ui)
  {
1093
#if !JVET_L0090_PAIR_AVG
1094
    isCandInter[ui] = false;
Yu-Chi Su's avatar
Yu-Chi Su committed
1095
#endif
1096
    mrgCtx.GBiIdx[ui] = GBI_DEFAULT;
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
    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
1107

Yu Han's avatar
Yu Han committed
1108
#if JVET_M0483_IBC==0
Yu Han's avatar
Yu Han committed
1109
  int mrgCandIdxIBC = mrgCandIdx;
Yu Han's avatar
Yu Han committed
1110
#endif
Xiaozhong Xu's avatar
Xiaozhong Xu committed
1111

1112 1113 1114 1115 1116
#if JVET_M0170_MRG_SHARELIST
  const Position posLT = pu.shareParentPos;
  const Position posRT = pu.shareParentPos.offset(pu.shareParentSize.width - 1, 0);
  const Position posLB = pu.shareParentPos.offset(0, pu.shareParentSize.height - 1);
#else
1117 1118 1119
  const Position posLT = pu.Y().topLeft();
  const Position posRT = pu.Y().topRight();
  const Position posLB = pu.Y().bottomLeft();
1120
#endif
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
  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) );

1132
#if !JVET_L0090_PAIR_AVG
1133
    isCandInter[cnt] = true;
1134
#endif
1135 1136 1137

    // get Inter Dir
    mrgCtx.interDirNeighbours[cnt] = miLeft.interDir;
1138
    mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? puLeft->cu->GBiIdx : GBI_DEFAULT;
1139
    // get Mv from Left
Yu Han's avatar
Yu Han committed
1140
#if JVET_M0483_IBC==0
Yu Han's avatar
Yu Han committed
1141
    if (puLeft->cu->ibc)
Xiaozhong Xu's avatar
Xiaozhong Xu committed
1142
    {
Yu Han's avatar
Yu Han committed
1143
      mrgCtx.mrgTypeNeighbours[cnt] = MRG_TYPE_IBC;
Xiaozhong Xu's avatar
Xiaozhong Xu committed
1144
      if (mmvdList != 0 && mrgCandIdx != -1)
Yu Han's avatar
Yu Han committed
1145
        mrgCandIdxIBC++;
Xiaozhong Xu's avatar
Xiaozhong Xu committed
1146
    }
Yu Han's avatar
Yu Han committed
1147
#endif
1148 1149 1150 1151 1152 1153
    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
1154
#if JVET_M0483_IBC==0
Yu Han's avatar
Yu Han committed
1155
    if (mrgCandIdxIBC == cnt && canFastExit)
Yu Han's avatar
Yu Han committed
1156 1157 1158
#else
    if (mrgCandIdx == cnt && canFastExit)
#endif
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
    {
      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 ) )
    {
1184
#if !JVET_L0090_PAIR_AVG
1185
      isCandInter[cnt] = true;
1186
#endif
1187 1188 1189

      // get Inter Dir
      mrgCtx.interDirNeighbours[cnt] = miAbove.interDir;
1190
      // get Mv from Above
1191
      mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? puAbove->cu->GBiIdx : GBI_DEFAULT;
Yu Han's avatar
Yu Han committed
1192
#if JVET_M0483_IBC==0
Yu Han's avatar
Yu Han committed
1193
      if (puAbove->cu->ibc)
Xiaozhong Xu's avatar
Xiaozhong Xu committed
1194
      {
Yu Han's avatar
Yu Han committed
1195
        mrgCtx.mrgTypeNeighbours[cnt] = MRG_TYPE_IBC;
Xiaozhong Xu's avatar
Xiaozhong Xu committed
1196
        if (mmvdList != 0 && mrgCandIdx != -1)
Yu Han's avatar
Yu Han committed
1197
          mrgCandIdxIBC++;
Xiaozhong Xu's avatar
Xiaozhong Xu committed
1198
      }
Yu Han's avatar
Yu Han committed
1199
#endif
1200
      mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miAbove.mv[0], miAbove.refIdx[0] );
1201 1202 1203 1204 1205

      if( slice.isInterB() )
      {
        mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miAbove.mv[1], miAbove.refIdx[1] );
      }
Yu Han's avatar
Yu Han committed
1206
#if JVET_M0483_IBC==0
Yu Han's avatar
Yu Han committed
1207
      if (mrgCandIdxIBC == cnt && canFastExit)
Yu Han's avatar
Yu Han committed
1208 1209 1210
#else
      if (mrgCandIdx == cnt && canFastExit)
#endif
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
      {
        return;
      }

      cnt++;
    }
  }

  // early termination
  if( cnt == maxNumMergeCand )
  {
    return;
  }

  // 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 HM_JEM_MERGE_CANDS
    if( ( !isAvailableB1 || ( miAbove != miAboveRight ) ) && ( !isAvailableA1 || ( miLeft != miAboveRight ) ) )
#else
    if( !isAvailableB1 || ( miAbove != miAboveRight ) )
#endif
    {
1240
#if !JVET_L0090_PAIR_AVG
1241
      isCandInter[cnt] = true;
1242
#endif
1243 1244 1245

      // get Inter Dir
      mrgCtx.interDirNeighbours[cnt] = miAboveRight.interDir;
1246
      // get Mv from Above-right
1247
      mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? puAboveRight->cu->GBiIdx : GBI_DEFAULT;
Yu Han's avatar
Yu Han committed
1248
#if JVET_M0483_IBC==0
Yu Han's avatar
Yu Han committed
1249
      if (puAboveRight->cu->ibc)
Xiaozhong Xu's avatar
Xiaozhong Xu committed
1250
      {
Yu Han's avatar
Yu Han committed
1251
        mrgCtx.mrgTypeNeighbours[cnt] = MRG_TYPE_IBC;
Xiaozhong Xu's avatar
Xiaozhong Xu committed
1252
        if (mmvdList != 0 && mrgCandIdx != -1)
Yu Han's avatar
Yu Han committed
1253
          mrgCandIdxIBC++;
Xiaozhong Xu's avatar
Xiaozhong Xu committed
1254
      }
Yu Han's avatar
Yu Han committed
1255
#endif
1256
      mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miAboveRight.mv[0], miAboveRight.refIdx[0] );
1257 1258 1259 1260 1261 1262

      if( slice.isInterB() )
      {
        mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miAboveRight.mv[1], miAboveRight.refIdx[1] );
      }

Yu Han's avatar
Yu Han committed
1263
#if JVET_M0483_IBC==0
Yu Han's avatar
Yu Han committed
1264
      if (mrgCandIdxIBC == cnt && canFastExit)
Yu Han's avatar
Yu Han committed
1265 1266 1267
#else
      if (mrgCandIdx == cnt && canFastExit)
#endif
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
      {
        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 HM_JEM_MERGE_CANDS
    if( ( !isAvailableA1 || ( miBelowLeft != miLeft ) ) && ( !isAvailableB1 || ( miBelowLeft != miAbove ) )