Newer
Older
{
return true;
}
cnt ++;
if (cnt == maxNumMergeCandMin1)
{
break;
}
}
}
return false;
}
#endif
void PU::getInterMergeCandidates( const PredictionUnit &pu, MergeCtx& mrgCtx,
#if JVET_L0054_MMVD
const int& mrgCandIdx )

Karsten Suehring
committed
{
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;
Yuling Hsiao
committed
#if !JVET_L0090_PAIR_AVG
// this variable is unused if remove HEVC combined candidates

Karsten Suehring
committed
bool isCandInter[MRG_MAX_NUM_CANDS];
Yuling Hsiao
committed
#endif

Karsten Suehring
committed
for (uint32_t ui = 0; ui < maxNumMergeCand; ++ui)
{
Yuling Hsiao
committed
#if !JVET_L0090_PAIR_AVG

Karsten Suehring
committed
isCandInter[ui] = false;
#if JVET_L0646_GBI
mrgCtx.GBiIdx[ui] = GBI_DEFAULT;
#endif

Karsten Suehring
committed
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;
#if JVET_L0293_CPR && JVET_L0054_MMVD
int mrgCandIdxCPR = mrgCandIdx;
#endif
#if JVET_L0293_CPR && !JVET_L0369_SUBBLOCK_MERGE
int cntCPR = 0;
#endif

Karsten Suehring
committed
const Position posLT = pu.Y().topLeft();
const Position posRT = pu.Y().topRight();
const Position posLB = pu.Y().bottomLeft();
MotionInfo miAbove, miLeft, miAboveLeft, miAboveRight, miBelowLeft;
//left
const PredictionUnit* puLeft = cs.getPURestricted( posLB.offset( -1, 0 ), pu, pu.chType );
const bool isAvailableA1 = puLeft && isDiffMER( pu, *puLeft ) && pu.cu != puLeft->cu && CU::isInter( *puLeft->cu );
if( isAvailableA1 )
{
miLeft = puLeft->getMotionInfo( posLB.offset(-1, 0) );
Yuling Hsiao
committed
#if !JVET_L0090_PAIR_AVG

Karsten Suehring
committed
isCandInter[cnt] = true;
Yuling Hsiao
committed
#endif

Karsten Suehring
committed
// get Inter Dir
mrgCtx.interDirNeighbours[cnt] = miLeft.interDir;
#if JVET_L0646_GBI
mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? puLeft->cu->GBiIdx : GBI_DEFAULT;
#endif

Karsten Suehring
committed
// get Mv from Left
#if JVET_L0293_CPR
if (puLeft->cu->cpr)
{
mrgCtx.mrgTypeNeighbours[cnt] = MRG_TYPE_CPR;
#if !JVET_L0369_SUBBLOCK_MERGE
cntCPR++;
#endif
#if JVET_L0293_CPR && JVET_L0054_MMVD
if (mmvdList != 0 && mrgCandIdx != -1)
mrgCandIdxCPR++;
#endif
}
#endif

Karsten Suehring
committed
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]);
}
#if JVET_L0293_CPR && JVET_L0054_MMVD
if (mrgCandIdxCPR == cnt && canFastExit)
#else

Karsten Suehring
committed
if( mrgCandIdx == cnt && canFastExit )

Karsten Suehring
committed
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
{
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 ) )
{
Yuling Hsiao
committed
#if !JVET_L0090_PAIR_AVG

Karsten Suehring
committed
isCandInter[cnt] = true;
Yuling Hsiao
committed
#endif

Karsten Suehring
committed
// get Inter Dir
mrgCtx.interDirNeighbours[cnt] = miAbove.interDir;
// get Mv from Above
#if JVET_L0646_GBI
mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? puAbove->cu->GBiIdx : GBI_DEFAULT;
#endif
#if JVET_L0293_CPR
if (puAbove->cu->cpr)
{
mrgCtx.mrgTypeNeighbours[cnt] = MRG_TYPE_CPR;
#if !JVET_L0369_SUBBLOCK_MERGE
cntCPR++;
#endif
#if JVET_L0293_CPR && JVET_L0054_MMVD
if (mmvdList != 0 && mrgCandIdx != -1)
mrgCandIdxCPR++;
#endif
}
#endif

Christian Helmrich
committed
mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miAbove.mv[0], miAbove.refIdx[0] );

Karsten Suehring
committed
if( slice.isInterB() )
{
mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miAbove.mv[1], miAbove.refIdx[1] );
}
#if JVET_L0293_CPR && JVET_L0054_MMVD
if (mrgCandIdxCPR == cnt && canFastExit)
#else

Karsten Suehring
committed
if( mrgCandIdx == cnt && canFastExit )

Karsten Suehring
committed
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
{
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
{
Yuling Hsiao
committed
#if !JVET_L0090_PAIR_AVG

Karsten Suehring
committed
isCandInter[cnt] = true;
Yuling Hsiao
committed
#endif

Karsten Suehring
committed
// get Inter Dir
mrgCtx.interDirNeighbours[cnt] = miAboveRight.interDir;
// get Mv from Above-right
#if JVET_L0646_GBI
mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? puAboveRight->cu->GBiIdx : GBI_DEFAULT;
#endif
#if JVET_L0293_CPR
if (puAboveRight->cu->cpr)
{
mrgCtx.mrgTypeNeighbours[cnt] = MRG_TYPE_CPR;
#if !JVET_L0369_SUBBLOCK_MERGE
cntCPR++;
#endif
#if JVET_L0293_CPR && JVET_L0054_MMVD
if (mmvdList != 0 && mrgCandIdx != -1)
mrgCandIdxCPR++;
#endif
}
#endif

Christian Helmrich
committed
mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miAboveRight.mv[0], miAboveRight.refIdx[0] );

Karsten Suehring
committed
if( slice.isInterB() )
{
mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miAboveRight.mv[1], miAboveRight.refIdx[1] );
}
#if JVET_L0293_CPR && JVET_L0054_MMVD
if (mrgCandIdxCPR == cnt && canFastExit)
#else

Karsten Suehring
committed
if( mrgCandIdx == cnt && canFastExit )

Karsten Suehring
committed
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
{
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 ) ) && ( !isAvailableB0 || ( miBelowLeft != miAboveRight ) ) )
#else
if( !isAvailableA1 || ( miBelowLeft != miLeft ) )
#endif
{
Yuling Hsiao
committed
#if !JVET_L0090_PAIR_AVG

Karsten Suehring
committed
isCandInter[cnt] = true;
Yuling Hsiao
committed
#endif

Karsten Suehring
committed
// get Inter Dir
mrgCtx.interDirNeighbours[cnt] = miBelowLeft.interDir;
#if JVET_L0646_GBI
mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? puLeftBottom->cu->GBiIdx : GBI_DEFAULT;
#endif

Karsten Suehring
committed
// get Mv from Bottom-Left
#if JVET_L0293_CPR
if (puLeftBottom->cu->cpr)
{
mrgCtx.mrgTypeNeighbours[cnt] = MRG_TYPE_CPR;
#if !JVET_L0369_SUBBLOCK_MERGE
cntCPR++;
#endif
#if JVET_L0293_CPR && JVET_L0054_MMVD
if (mmvdList != 0 && mrgCandIdx != -1)
mrgCandIdxCPR++;
#endif
}
#endif

Karsten Suehring
committed
mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miBelowLeft.mv[0], miBelowLeft.refIdx[0] );
if( slice.isInterB() )
{
mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miBelowLeft.mv[1], miBelowLeft.refIdx[1] );
}
#if JVET_L0293_CPR && JVET_L0054_MMVD
if (mrgCandIdxCPR == cnt && canFastExit)
#else

Karsten Suehring
committed
if( mrgCandIdx == cnt && canFastExit )

Karsten Suehring
committed
{
return;
}
cnt++;
}
}
// early termination
if( cnt == maxNumMergeCand )
{
return;
}
#if !JVET_L0369_SUBBLOCK_MERGE
#if JVET_L0293_CPR
bool enableSubPuMvp = slice.getSPS()->getSpsNext().getUseSubPuMvp() && !(slice.getPOC() == slice.getRefPic(REF_PIC_LIST_0, 0)->getPOC() && slice.isIRAP());
#else

Karsten Suehring
committed
bool enableSubPuMvp = slice.getSPS()->getSpsNext().getUseSubPuMvp();

Karsten Suehring
committed
bool isAvailableSubPu = false;
unsigned subPuMvpPos = 0;
if( enableSubPuMvp )
{
CHECK( mrgCtx.subPuMvpMiBuf .area() == 0 || !mrgCtx.subPuMvpMiBuf .buf, "Buffer not initialized" );
mrgCtx.subPuMvpMiBuf .fill( MotionInfo() );
}
if( enableSubPuMvp && slice.getEnableTMVPFlag() )
{
#if JVET_L0293_CPR && JVET_L0054_MMVD
bool bMrgIdxMatchATMVPCan = (mrgCandIdxCPR == cnt);
#else

Karsten Suehring
committed
bool bMrgIdxMatchATMVPCan = ( mrgCandIdx == cnt );

Karsten Suehring
committed
bool tmpLICFlag = false;
isAvailableSubPu = cs.sps->getSpsNext().getUseATMVP() &&
getInterMergeSubPuMvpCand( pu, mrgCtx, tmpLICFlag, cnt
#if JVET_L0054_MMVD
, mmvdList

Karsten Suehring
committed
if( isAvailableSubPu )
{
Yuling Hsiao
committed
#if !JVET_L0090_PAIR_AVG

Karsten Suehring
committed
isCandInter[cnt] = true;
Yuling Hsiao
committed
#endif

Karsten Suehring
committed
mrgCtx.mrgTypeNeighbours[cnt] = MRG_TYPE_SUBPU_ATMVP;
if( bMrgIdxMatchATMVPCan )
{
return;
}
subPuMvpPos = cnt;
cnt++;
if( cnt == maxNumMergeCand )
{
return;
}
}
}

Karsten Suehring
committed
// above left
#if JVET_L0369_SUBBLOCK_MERGE
if ( cnt < 4 )
#else

Karsten Suehring
committed
if( cnt < ( enableSubPuMvp ? 6 : 4 ) )

Karsten Suehring
committed
{
const PredictionUnit *puAboveLeft = cs.getPURestricted( posLT.offset( -1, -1 ), pu, pu.chType );
bool isAvailableB2 = puAboveLeft && isDiffMER( pu, *puAboveLeft ) && CU::isInter( *puAboveLeft->cu );
if( isAvailableB2 )
{
miAboveLeft = puAboveLeft->getMotionInfo( posLT.offset( -1, -1 ) );
#if 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
{
Yuling Hsiao
committed
#if !JVET_L0090_PAIR_AVG

Karsten Suehring
committed
isCandInter[cnt] = true;
Yuling Hsiao
committed
#endif

Karsten Suehring
committed
// get Inter Dir
mrgCtx.interDirNeighbours[cnt] = miAboveLeft.interDir;
#if JVET_L0646_GBI
mrgCtx.GBiIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? puAboveLeft->cu->GBiIdx : GBI_DEFAULT;
#endif

Karsten Suehring
committed
// get Mv from Above-Left
#if JVET_L0293_CPR
if (puAboveLeft->cu->cpr)
{
mrgCtx.mrgTypeNeighbours[cnt] = MRG_TYPE_CPR;
#if !JVET_L0369_SUBBLOCK_MERGE
cntCPR++;
#endif
#if JVET_L0293_CPR && JVET_L0054_MMVD
if (mmvdList != 0 && mrgCandIdx != -1)
mrgCandIdxCPR++;
#endif
}
#endif

Karsten Suehring
committed
mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miAboveLeft.mv[0], miAboveLeft.refIdx[0] );
if( slice.isInterB() )
{
mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miAboveLeft.mv[1], miAboveLeft.refIdx[1] );
}
#if JVET_L0293_CPR && JVET_L0054_MMVD
if (mrgCandIdxCPR == cnt && canFastExit)
#else

Karsten Suehring
committed
if( mrgCandIdx == cnt && canFastExit )

Karsten Suehring
committed
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
{
return;
}
cnt++;
}
}
}
// early termination
if (cnt == maxNumMergeCand)
{
return;
}
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;
Position posC1 = pu.Y().center();
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 iRefIdx = 0;
int dir = 0;
unsigned uiArrayAddr = cnt;
bool bExistMV = ( C0Avail && getColocatedMVP(pu, REF_PIC_LIST_0, posC0, cColMv, iRefIdx ) )
|| getColocatedMVP(pu, REF_PIC_LIST_0, posC1, cColMv, iRefIdx );
if (bExistMV)
{
dir |= 1;
mrgCtx.mvFieldNeighbours[2 * uiArrayAddr].setMvField(cColMv, iRefIdx);
}
if (slice.isInterB())
{
bExistMV = ( C0Avail && getColocatedMVP(pu, REF_PIC_LIST_1, posC0, cColMv, iRefIdx ) )
|| getColocatedMVP(pu, REF_PIC_LIST_1, posC1, cColMv, iRefIdx );
if (bExistMV)
{
dir |= 2;
mrgCtx.mvFieldNeighbours[2 * uiArrayAddr + 1].setMvField(cColMv, iRefIdx);
}
}
if( dir != 0 )
{
#if JVET_L0369_SUBBLOCK_MERGE
bool addTMvp = true;
#else

Karsten Suehring
committed
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
bool addTMvp = !( cs.sps->getSpsNext().getUseSubPuMvp() && isAvailableSubPu );
if( !addTMvp )
{
if ( dir != mrgCtx.interDirNeighbours[subPuMvpPos] )
{
addTMvp = true;
}
else
{
for( unsigned refList = 0; refList < NUM_REF_PIC_LIST_01; refList++ )
{
if( dir & ( 1 << refList ) )
{
if( mrgCtx.mvFieldNeighbours[( cnt << 1 ) + refList] != mrgCtx.mvFieldNeighbours[(subPuMvpPos << 1) + refList] )
{
addTMvp = true;
break;
}
}
}
}
}

Karsten Suehring
committed
#if HM_JEM_MERGE_CANDS
#if JVET_L0369_SUBBLOCK_MERGE
int iSpanCand = cnt;
#else

Karsten Suehring
committed
int iSpanCand = isAvailableSubPu ? cnt - 1 : cnt;

Karsten Suehring
committed
for( int i = 0; i < iSpanCand; i++ )
{
if( mrgCtx.interDirNeighbours[ i ] == dir &&
mrgCtx.mvFieldNeighbours [ i << 1 ] == mrgCtx.mvFieldNeighbours[ uiArrayAddr << 1 ] &&
mrgCtx.mvFieldNeighbours [( i << 1 ) + 1] == mrgCtx.mvFieldNeighbours[( uiArrayAddr << 1 ) + 1] )
{
addTMvp = false;
}
}
#endif
if( addTMvp )
{
mrgCtx.interDirNeighbours[uiArrayAddr] = dir;
Yuling Hsiao
committed
#if !JVET_L0090_PAIR_AVG

Karsten Suehring
committed
isCandInter [uiArrayAddr] = true;
Yuling Hsiao
committed
#endif
mrgCtx.GBiIdx[uiArrayAddr] = GBI_DEFAULT;
#if JVET_L0293_CPR && JVET_L0054_MMVD
if (mrgCandIdxCPR == cnt && canFastExit)
#else

Karsten Suehring
committed
if( mrgCandIdx == cnt && canFastExit )

Karsten Suehring
committed
{
return;
}
cnt++;
}
}
}
// early termination
if (cnt == maxNumMergeCand)
{
return;
}
#if JVET_L0266_HMVP
int maxNumMergeCandMin1 = maxNumMergeCand - 1;
if (cnt != maxNumMergeCandMin1)
{
#if JVET_L0369_SUBBLOCK_MERGE
bool isAvailableSubPu = false;
unsigned subPuMvpPos = 0;
#endif
bool bFound = addMergeHMVPCand(slice, mrgCtx, canFastExit
#if JVET_L0293_CPR && JVET_L0054_MMVD
, (mmvdList != 0 && mrgCandIdx != -1) ? (const int) mrgCandIdxCPR : mrgCandIdx
#else
, mrgCandIdx
#endif
, maxNumMergeCandMin1, cnt, cnt, isAvailableSubPu, subPuMvpPos
#if JVET_L0293_CPR && JVET_L0054_MMVD
, mmvdList
#endif
);
#else
bool bFound = addMergeHMVPCand(slice, mrgCtx, isCandInter, canFastExit
#if JVET_L0293_CPR
, (mmvdList != 0 && mrgCandIdx != -1) ? (const int)mrgCandIdxCPR : mrgCandIdx
, mrgCandIdx
#endif
, maxNumMergeCandMin1, cnt, cnt, isAvailableSubPu, subPuMvpPos
#if JVET_L0293_CPR && JVET_L0054_MMVD
, mmvdList
#endif
);
#endif
if (bFound)
{
return;
}
}
#endif
Yuling Hsiao
committed
#if JVET_L0090_PAIR_AVG
// pairwise-average candidates
{
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];
mrgCtx.mvFieldNeighbours[cnt * 2].setMvField( Mv( 0, 0 ), NOT_VALID );
mrgCtx.mvFieldNeighbours[cnt * 2 + 1].setMvField( Mv( 0, 0 ), NOT_VALID );
// calculate average MV for L0 and L1 seperately
unsigned char interDir = 0;
#if JVET_L0293_CPR
// skip when one is BV and one is MV
if (mrgCtx.mrgTypeNeighbours[i] != mrgCtx.mrgTypeNeighbours[j] && pu.cs->sps->getSpsNext().getCPRMode())
{
continue;
}
#endif
Yuling Hsiao
committed
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
for( int refListId = 0; refListId < (slice.isInterB() ? 2 : 1); refListId++ )
{
const short refIdxI = mrgCtx.mvFieldNeighbours[i * 2 + refListId].refIdx;
const short refIdxJ = mrgCtx.mvFieldNeighbours[j * 2 + refListId].refIdx;
// both MVs are invalid, skip
if( (refIdxI == NOT_VALID) && (refIdxJ == NOT_VALID) )
{
continue;
}
interDir += 1 << refListId;
// both MVs are valid, average these two MVs
if( (refIdxI != NOT_VALID) && (refIdxJ != NOT_VALID) )
{
const Mv& MvI = mrgCtx.mvFieldNeighbours[i * 2 + refListId].mv;
const Mv& MvJ = mrgCtx.mvFieldNeighbours[j * 2 + refListId].mv;
// average two MVs
Mv avgMv = MvI;
avgMv += MvJ;
avgMv.setHor( avgMv.getHor() / 2 );
avgMv.setVer( avgMv.getVer() / 2 );
#if JVET_L0293_CPR
if (mrgCtx.mrgTypeNeighbours[i] == MRG_TYPE_CPR && mrgCtx.mrgTypeNeighbours[j] == MRG_TYPE_CPR && pu.cs->sps->getSpsNext().getCPRMode())
{
mrgCtx.mrgTypeNeighbours[cnt] = MRG_TYPE_CPR;
avgMv.setHor((avgMv.getHor() / 16) << 4);
avgMv.setVer((avgMv.getVer() / 16) << 4);
}
#endif
Yuling Hsiao
committed
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
mrgCtx.mvFieldNeighbours[cnt * 2 + refListId].setMvField( avgMv, refIdxI );
}
// only one MV is valid, take the only one MV
else if( refIdxI != NOT_VALID )
{
Mv singleMv = mrgCtx.mvFieldNeighbours[i * 2 + refListId].mv;
mrgCtx.mvFieldNeighbours[cnt * 2 + refListId].setMvField( singleMv, refIdxI );
}
else if( refIdxJ != NOT_VALID )
{
Mv singleMv = mrgCtx.mvFieldNeighbours[j * 2 + refListId].mv;
mrgCtx.mvFieldNeighbours[cnt * 2 + refListId].setMvField( singleMv, refIdxJ );
}
}
mrgCtx.interDirNeighbours[cnt] = interDir;
if( interDir > 0 )
{
cnt++;
}
}
// early termination
if( cnt == maxNumMergeCand )
{
return;
}
}
#endif

Karsten Suehring
committed
uint32_t uiArrayAddr = cnt;
Yuling Hsiao
committed
#if !JVET_L0090_PAIR_AVG
#if JVET_L0266_HMVP
uint32_t uiCutoff = std::min( uiArrayAddr, 3u );
#else

Karsten Suehring
committed
uint32_t uiCutoff = std::min( uiArrayAddr, 4u );

Karsten Suehring
committed
if (slice.isInterB())
{
static const uint32_t NUM_PRIORITY_LIST = 12;
static const uint32_t uiPriorityList0[NUM_PRIORITY_LIST] = { 0 , 1, 0, 2, 1, 2, 0, 3, 1, 3, 2, 3 };
static const uint32_t uiPriorityList1[NUM_PRIORITY_LIST] = { 1 , 0, 2, 0, 2, 1, 3, 0, 3, 1, 3, 2 };
for (int idx = 0; idx < uiCutoff * (uiCutoff - 1) && uiArrayAddr != maxNumMergeCand; idx++)
{
CHECK( idx >= NUM_PRIORITY_LIST, "Invalid priority list number" );
int i = uiPriorityList0[idx];
int j = uiPriorityList1[idx];
if (isCandInter[i] && isCandInter[j] && (mrgCtx.interDirNeighbours[i] & 0x1) && (mrgCtx.interDirNeighbours[j] & 0x2))
{
isCandInter[uiArrayAddr] = true;
mrgCtx.interDirNeighbours[uiArrayAddr] = 3;
#if JVET_L0646_GBI
mrgCtx.GBiIdx[uiArrayAddr] = ((mrgCtx.interDirNeighbours[uiArrayAddr] == 3)) ? CU::deriveGbiIdx(mrgCtx.GBiIdx[i], mrgCtx.GBiIdx[j]) : GBI_DEFAULT;
#endif

Karsten Suehring
committed
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
// get Mv from cand[i] and cand[j]
mrgCtx.mvFieldNeighbours[ uiArrayAddr << 1 ].setMvField(mrgCtx.mvFieldNeighbours[ i << 1 ].mv, mrgCtx.mvFieldNeighbours[ i << 1 ].refIdx);
mrgCtx.mvFieldNeighbours[(uiArrayAddr << 1) + 1].setMvField(mrgCtx.mvFieldNeighbours[(j << 1) + 1].mv, mrgCtx.mvFieldNeighbours[(j << 1) + 1].refIdx);
int iRefPOCL0 = slice.getRefPOC(REF_PIC_LIST_0, mrgCtx.mvFieldNeighbours[(uiArrayAddr << 1) ].refIdx);
int iRefPOCL1 = slice.getRefPOC(REF_PIC_LIST_1, mrgCtx.mvFieldNeighbours[(uiArrayAddr << 1) + 1].refIdx);
if( iRefPOCL0 == iRefPOCL1 && mrgCtx.mvFieldNeighbours[( uiArrayAddr << 1 )].mv == mrgCtx.mvFieldNeighbours[( uiArrayAddr << 1 ) + 1].mv )
{
isCandInter[uiArrayAddr] = false;
}
else
{
uiArrayAddr++;
}
}
}
}
// early termination
if (uiArrayAddr == maxNumMergeCand)
{
return;
}
Yuling Hsiao
committed
#endif

Karsten Suehring
committed
int iNumRefIdx = slice.isInterB() ? std::min(slice.getNumRefIdx(REF_PIC_LIST_0), slice.getNumRefIdx(REF_PIC_LIST_1)) : slice.getNumRefIdx(REF_PIC_LIST_0);
int r = 0;
int refcnt = 0;
while (uiArrayAddr < maxNumMergeCand)
{
Yuling Hsiao
committed
#if !JVET_L0090_PAIR_AVG

Karsten Suehring
committed
isCandInter [uiArrayAddr ] = true;
Yuling Hsiao
committed
#endif

Karsten Suehring
committed
mrgCtx.interDirNeighbours [uiArrayAddr ] = 1;
#if JVET_L0646_GBI
mrgCtx.GBiIdx [uiArrayAddr ] = GBI_DEFAULT;
#endif

Karsten Suehring
committed
mrgCtx.mvFieldNeighbours [uiArrayAddr << 1].setMvField(Mv(0, 0), r);
if (slice.isInterB())
{
mrgCtx.interDirNeighbours [ uiArrayAddr ] = 3;
mrgCtx.mvFieldNeighbours [(uiArrayAddr << 1) + 1].setMvField(Mv(0, 0), r);
}
#if JVET_L0293_CPR
if ( mrgCtx.interDirNeighbours[uiArrayAddr] == 1 && pu.cs->slice->getRefPic(REF_PIC_LIST_0, mrgCtx.mvFieldNeighbours[uiArrayAddr << 1].refIdx)->getPOC() == pu.cs->slice->getPOC())
{
mrgCtx.mrgTypeNeighbours[uiArrayAddr] = MRG_TYPE_CPR;
}
#endif

Karsten Suehring
committed
uiArrayAddr++;
if (refcnt == iNumRefIdx - 1)
{
r = 0;
}
else
{
++r;
++refcnt;
}
}
mrgCtx.numValidMergeCand = uiArrayAddr;
}
#if JVET_L0293_CPR
// for cpr pu validation
bool PU::isBlockVectorValid(PredictionUnit& pu, int xPos, int yPos, int width, int height, int picWidth, int picHeight, int xStartInCU, int yStartInCU, int xBv, int yBv, int ctuSize)
{
const int ctuSizeLog2 = g_aucLog2[ctuSize];
int refRightX = xPos + xBv + width - 1;
int refBottomY = yPos + yBv + height - 1;

Karsten Suehring
committed
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
int refLeftX = xPos + xBv;
int refTopY = yPos + yBv;
if ((xPos + xBv) < 0)
{
return false;
}
if (refRightX >= picWidth)
{
return false;
}
if ((yPos + yBv) < 0)
{
return false;
}
if (refBottomY >= picHeight)
{
return false;
}
if ((xBv + width) > 0 && (yBv + height) > 0)
{
return false;
}
// cannot be in the above CTU row
if (refTopY >> ctuSizeLog2 < yPos >> ctuSizeLog2)
return false;
// cannot be in the below CTU row
if (refBottomY >> ctuSizeLog2 > yPos >> ctuSizeLog2)
{
return false;
}
// in the same CTU line
if ((refRightX >> ctuSizeLog2 <= xPos >> ctuSizeLog2) && (refLeftX >> ctuSizeLog2 >= (xPos >> ctuSizeLog2)))
{
// in the same CTU, check if the reference block is already coded
const Position refPosLT = pu.Y().topLeft().offset(xBv, yBv);
const Position refPosBR = pu.Y().bottomRight().offset(xBv, yBv);
const ChannelType chType = toChannelType(COMPONENT_Y);
{
if (!pu.cs->isDecomp(refPosBR, chType))
return false;
if (!pu.cs->isDecomp(refPosLT, chType))
return false;
}
return true;
}
else
return false;
}// for cpr pu validation
#endif

Karsten Suehring
committed
static int xGetDistScaleFactor(const int &iCurrPOC, const int &iCurrRefPOC, const int &iColPOC, const int &iColRefPOC)
{
int iDiffPocD = iColPOC - iColRefPOC;
int iDiffPocB = iCurrPOC - iCurrRefPOC;
if (iDiffPocD == iDiffPocB)
{
return 4096;
}
else
{
int iTDB = Clip3(-128, 127, iDiffPocB);
int iTDD = Clip3(-128, 127, iDiffPocD);
int iX = (0x4000 + abs(iTDD / 2)) / iTDD;
int iScale = Clip3(-4096, 4095, (iTDB * iX + 32) >> 6);
return iScale;
}
}
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
#if JVET_L0054_MMVD
int PU::getDistScaleFactor(const int &currPOC, const int &currRefPOC, const int &colPOC, const int &colRefPOC)
{
return xGetDistScaleFactor(currPOC, currRefPOC, colPOC, colRefPOC);
}
void PU::getInterMMVDMergeCandidates(const PredictionUnit &pu, MergeCtx& mrgCtx, const int& mrgCandIdx)
{
int refIdxList0, refIdxList1;
int k;
int currBaseNum = 0;
const uint16_t maxNumMergeCand = mrgCtx.numValidMergeCand;
for (k = 0; k < maxNumMergeCand; k++)
{
if (mrgCtx.mrgTypeNeighbours[k] == MRG_TYPE_DEFAULT_N)
{
refIdxList0 = mrgCtx.mvFieldNeighbours[(k << 1)].refIdx;
refIdxList1 = mrgCtx.mvFieldNeighbours[(k << 1) + 1].refIdx;
if ((refIdxList0 >= 0) && (refIdxList1 >= 0))
{
mrgCtx.mmvdBaseMv[currBaseNum][0] = mrgCtx.mvFieldNeighbours[(k << 1)];
mrgCtx.mmvdBaseMv[currBaseNum][1] = mrgCtx.mvFieldNeighbours[(k << 1) + 1];
}
else if (refIdxList0 >= 0)
{
mrgCtx.mmvdBaseMv[currBaseNum][0] = mrgCtx.mvFieldNeighbours[(k << 1)];
mrgCtx.mmvdBaseMv[currBaseNum][1] = MvField(Mv(0, 0), -1);
}
else if (refIdxList1 >= 0)
{
mrgCtx.mmvdBaseMv[currBaseNum][0] = MvField(Mv(0, 0), -1);
mrgCtx.mmvdBaseMv[currBaseNum][1] = mrgCtx.mvFieldNeighbours[(k << 1) + 1];
}

Karsten Suehring
committed
currBaseNum++;
if (currBaseNum == MMVD_BASE_MV_NUM)
break;
}
}
if (currBaseNum < MMVD_BASE_MV_NUM)
{
for (k = currBaseNum; k < MMVD_BASE_MV_NUM; k++)
{
mrgCtx.mmvdBaseMv[k][0] = MvField(Mv(0, 0), 0);
mrgCtx.mmvdBaseMv[k][0] = MvField(Mv(0, 0), 0);
}
}
}
#endif

Karsten Suehring
committed
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
bool PU::getColocatedMVP(const PredictionUnit &pu, const RefPicList &eRefPicList, const Position &_pos, Mv& rcMv, const int &refIdx )
{
// don't perform MV compression when generally disabled or subPuMvp is used
const unsigned scale = ( pu.cs->pcv->noMotComp ? 1 : 4 * std::max<int>(1, 4 * AMVP_DECIMATION_FACTOR / 4) );
const unsigned mask = ~( scale - 1 );
const Position pos = Position{ PosType( _pos.x & mask ), PosType( _pos.y & mask ) };
const Slice &slice = *pu.cs->slice;
// use coldir.
const Picture* const pColPic = slice.getRefPic(RefPicList(slice.isInterB() ? 1 - slice.getColFromL0Flag() : 0), slice.getColRefIdx());
if( !pColPic )
{
return false;
}
RefPicList eColRefPicList = slice.getCheckLDC() ? eRefPicList : RefPicList(slice.getColFromL0Flag());
const MotionInfo& mi = pColPic->cs->getMotionInfo( pos );
if( !mi.isInter )
{
return false;
}
#if JVET_L0293_CPR
if (eRefPicList == REF_PIC_LIST_0 && pu.cs->slice->getRefPic(eRefPicList, refIdx)->getPOC() == pu.cs->slice->getPOC())
{
return false;
}
#endif

Karsten Suehring
committed
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
int iColRefIdx = mi.refIdx[eColRefPicList];
if (iColRefIdx < 0)
{
eColRefPicList = RefPicList(1 - eColRefPicList);
iColRefIdx = mi.refIdx[eColRefPicList];
if (iColRefIdx < 0)
{
return false;
}
}
const Slice *pColSlice = nullptr;
for( const auto s : pColPic->slices )
{
if( s->getIndependentSliceIdx() == mi.sliceIdx )
{
pColSlice = s;
break;
}
}
CHECK( pColSlice == nullptr, "Slice segment not found" );