Newer
Older
xIBCSearchMVCandUpdate(sad, 0, y, sadBestCand, cMVCand);
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
tempSadBest = sadBestCand[0];
if (sadBestCand[0] <= 3)
{
bestX = cMVCand[0].getHor();
bestY = cMVCand[0].getVer();
sadBest = sadBestCand[0];
rcMv.set(bestX, bestY);
ruiCost = sadBest;
goto end;
}
}
const int boundX = std::max(srchRngHorLeft, -cuPelX);
for (int x = 0 - roiWidth - puPelOffsetX; x >= boundX; --x)
{
if (!PU::isBlockVectorValid(pu, cuPelX, cuPelY, roiWidth, roiHeight, picWidth, picHeight, 0, 0, x, 0, lcuWidth))
{
continue;
}
sad = m_pcRdCost->getBvCostMultiplePreds(x, 0, pu.cs->sps->getSpsNext().getImvMode() == IMV_4PEL);
m_cDistParam.cur.buf = piRefSrch + x;
sad += m_cDistParam.distFunc(m_cDistParam);
xIBCSearchMVCandUpdate(sad, x, 0, sadBestCand, cMVCand);
tempSadBest = sadBestCand[0];
if (sadBestCand[0] <= 3)
{
bestX = cMVCand[0].getHor();
bestY = cMVCand[0].getVer();
sadBest = sadBestCand[0];
rcMv.set(bestX, bestY);
ruiCost = sadBest;
goto end;
}
}
bestX = cMVCand[0].getHor();
bestY = cMVCand[0].getVer();
sadBest = sadBestCand[0];
if ((!bestX && !bestY) || (sadBest - m_pcRdCost->getBvCostMultiplePreds(bestX, bestY, pu.cs->sps->getSpsNext().getImvMode() == IMV_4PEL) <= 32))
{
//chroma refine
bestCandIdx = xIBCSearchMVChromaRefine(pu, roiWidth, roiHeight, cuPelX, cuPelY, sadBestCand, cMVCand);
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
bestX = cMVCand[bestCandIdx].getHor();
bestY = cMVCand[bestCandIdx].getVer();
sadBest = sadBestCand[bestCandIdx];
rcMv.set(bestX, bestY);
ruiCost = sadBest;
goto end;
}
if (pu.lwidth() < 16 && pu.lheight() < 16)
{
for (int y = std::max(srchRngVerTop, -cuPelY); y <= srchRngVerBottom; y += 2)
{
if ((y == 0) || ((int)(cuPelY + y + roiHeight) >= picHeight))
continue;
for (int x = std::max(srchRngHorLeft, -cuPelX); x <= srchRngHorRight; x++)
{
if ((x == 0) || ((int)(cuPelX + x + roiWidth) >= picWidth))
continue;
if (!PU::isBlockVectorValid(pu, cuPelX, cuPelY, roiWidth, roiHeight, picWidth, picHeight, 0, 0, x, y, lcuWidth))
{
continue;
}
sad = m_pcRdCost->getBvCostMultiplePreds(x, y, pu.cs->sps->getSpsNext().getImvMode() == IMV_4PEL);
m_cDistParam.cur.buf = piRefSrch + cStruct.iRefStride * y + x;
sad += m_cDistParam.distFunc(m_cDistParam);
xIBCSearchMVCandUpdate(sad, x, y, sadBestCand, cMVCand);
}
}
bestX = cMVCand[0].getHor();
bestY = cMVCand[0].getVer();
sadBest = sadBestCand[0];
if (sadBest - m_pcRdCost->getBvCostMultiplePreds(bestX, bestY, pu.cs->sps->getSpsNext().getImvMode() == IMV_4PEL) <= 16)
{
//chroma refine
bestCandIdx = xIBCSearchMVChromaRefine(pu, roiWidth, roiHeight, cuPelX, cuPelY, sadBestCand, cMVCand);
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
bestX = cMVCand[bestCandIdx].getHor();
bestY = cMVCand[bestCandIdx].getVer();
sadBest = sadBestCand[bestCandIdx];
rcMv.set(bestX, bestY);
ruiCost = sadBest;
goto end;
}
for (int y = (std::max(srchRngVerTop, -cuPelY) + 1); y <= srchRngVerBottom; y += 2)
{
if ((y == 0) || ((int)(cuPelY + y + roiHeight) >= picHeight))
continue;
for (int x = std::max(srchRngHorLeft, -cuPelX); x <= srchRngHorRight; x += 2)
{
if ((x == 0) || ((int)(cuPelX + x + roiWidth) >= picWidth))
continue;
if (!PU::isBlockVectorValid(pu, cuPelX, cuPelY, roiWidth, roiHeight, picWidth, picHeight, 0, 0, x, y, lcuWidth))
{
continue;
}
sad = m_pcRdCost->getBvCostMultiplePreds(x, y, pu.cs->sps->getSpsNext().getImvMode() == IMV_4PEL);
m_cDistParam.cur.buf = piRefSrch + cStruct.iRefStride * y + x;
sad += m_cDistParam.distFunc(m_cDistParam);
xIBCSearchMVCandUpdate(sad, x, y, sadBestCand, cMVCand);
if (sadBestCand[0] <= 5)
{
//chroma refine & return
bestCandIdx = xIBCSearchMVChromaRefine(pu, roiWidth, roiHeight, cuPelX, cuPelY, sadBestCand, cMVCand);
bestX = cMVCand[bestCandIdx].getHor();
bestY = cMVCand[bestCandIdx].getVer();
sadBest = sadBestCand[bestCandIdx];
rcMv.set(bestX, bestY);
ruiCost = sadBest;
goto end;
}
}
}
bestX = cMVCand[0].getHor();
bestY = cMVCand[0].getVer();
sadBest = sadBestCand[0];
if ((sadBest >= tempSadBest) || ((sadBest - m_pcRdCost->getBvCostMultiplePreds(bestX, bestY, pu.cs->sps->getSpsNext().getImvMode() == IMV_4PEL)) <= 32))
{
//chroma refine
bestCandIdx = xIBCSearchMVChromaRefine(pu, roiWidth, roiHeight, cuPelX, cuPelY, sadBestCand, cMVCand);
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
bestX = cMVCand[bestCandIdx].getHor();
bestY = cMVCand[bestCandIdx].getVer();
sadBest = sadBestCand[bestCandIdx];
rcMv.set(bestX, bestY);
ruiCost = sadBest;
goto end;
}
tempSadBest = sadBestCand[0];
for (int y = (std::max(srchRngVerTop, -cuPelY) + 1); y <= srchRngVerBottom; y += 2)
{
if ((y == 0) || ((int)(cuPelY + y + roiHeight) >= picHeight))
continue;
for (int x = (std::max(srchRngHorLeft, -cuPelX) + 1); x <= srchRngHorRight; x += 2)
{
if ((x == 0) || ((int)(cuPelX + x + roiWidth) >= picWidth))
continue;
if (!PU::isBlockVectorValid(pu, cuPelX, cuPelY, roiWidth, roiHeight, picWidth, picHeight, 0, 0, x, y, lcuWidth))
{
continue;
}
sad = m_pcRdCost->getBvCostMultiplePreds(x, y, pu.cs->sps->getSpsNext().getImvMode() == IMV_4PEL);
m_cDistParam.cur.buf = piRefSrch + cStruct.iRefStride * y + x;
sad += m_cDistParam.distFunc(m_cDistParam);
xIBCSearchMVCandUpdate(sad, x, y, sadBestCand, cMVCand);
if (sadBestCand[0] <= 5)
{
//chroma refine & return
bestCandIdx = xIBCSearchMVChromaRefine(pu, roiWidth, roiHeight, cuPelX, cuPelY, sadBestCand, cMVCand);
bestX = cMVCand[bestCandIdx].getHor();
bestY = cMVCand[bestCandIdx].getVer();
sadBest = sadBestCand[bestCandIdx];
rcMv.set(bestX, bestY);
ruiCost = sadBest;
goto end;
}
}
}
}
}
bestCandIdx = xIBCSearchMVChromaRefine(pu, roiWidth, roiHeight, cuPelX, cuPelY, sadBestCand, cMVCand);
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
bestX = cMVCand[bestCandIdx].getHor();
bestY = cMVCand[bestCandIdx].getVer();
sadBest = sadBestCand[bestCandIdx];
rcMv.set(bestX, bestY);
ruiCost = sadBest;
end:
if (roiWidth + roiHeight > 8)
{
m_numBVs = xMergeCandLists(m_acBVs, m_numBVs, cMVCand, CHROMA_REFINEMENT_CANDIDATES);
if (roiWidth + roiHeight == 32)
{
m_numBV16s = m_numBVs;
}
}
return;
}
// based on xMotionEstimation
void InterSearch::xIBCEstimation(PredictionUnit& pu, PelUnitBuf& origBuf,
Mv *pcMvPred,
Mv &rcMv,
Distortion &ruiCost, const int localSearchRangeX, const int localSearchRangeY
)
{
bool buffered = false;
if (m_pcEncCfg->getIBCFastMethod() & IBC_FAST_METHOD_BUFFERBV)
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
{
ruiCost = MAX_UINT;
const int iPicWidth = pu.cs->slice->getSPS()->getPicWidthInLumaSamples();
const int iPicHeight = pu.cs->slice->getSPS()->getPicHeightInLumaSamples();
const int cuPelX = pu.Y().x;
const int cuPelY = pu.Y().y;
int iRoiWidth = pu.lwidth();
int iRoiHeight = pu.lheight();
std::unordered_map<Mv, Distortion>& history = m_ctuRecord[pu.lumaPos()][pu.lumaSize()].bvRecord;
const unsigned int lcuWidth = pu.cs->slice->getSPS()->getMaxCUWidth();
for (std::unordered_map<Mv, Distortion>::iterator p = history.begin(); p != history.end(); p++)
{
const Mv& bv = p->first;
int xBv = bv.hor;
int yBv = bv.ver;
if (PU::isBlockVectorValid(pu, cuPelX, cuPelY, iRoiWidth, iRoiHeight, iPicWidth, iPicHeight, 0, 0, xBv, yBv, lcuWidth))
{
if (p->second < ruiCost)
{
rcMv = bv;
ruiCost = p->second;
buffered = true;
}
else if (p->second == ruiCost)
{
// stabilise the search through the unordered list
if (bv.hor < rcMv.getHor()
|| (bv.hor == rcMv.getHor() && bv.ver < rcMv.getVer()))
{
// update the vector.
rcMv = bv;
}
}
}
}
}
if (!buffered)
{
Mv cMvSrchRngLT;
Mv cMvSrchRngRB;
//cMvSrchRngLT.highPrec = false;
//cMvSrchRngRB.highPrec = false;
PelUnitBuf* pBuf = &origBuf;
// Search key pattern initialization
CPelBuf tmpPattern = pBuf->Y();
CPelBuf* pcPatternKey = &tmpPattern;
#if JVET_M0427_INLOOP_RESHAPER
if ((pu.cs->slice->getReshapeInfo().getUseSliceReshaper() && m_pcReshape->getCTUFlag()))
{
const CompArea &area = pu.blocks[COMPONENT_Y];
CompArea tmpArea(COMPONENT_Y, area.chromaFormat, Position(0, 0), area.size());
PelBuf tmpOrgLuma = m_tmpStorageLCU.getBuf(tmpArea);
tmpOrgLuma.copyFrom(tmpPattern);
tmpOrgLuma.rspSignal(m_pcReshape->getFwdLUT());
pcPatternKey = (CPelBuf*)&tmpOrgLuma;
}
#endif
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
m_lumaClpRng = pu.cs->slice->clpRng(COMPONENT_Y);
Picture* refPic = pu.cu->slice->getPic();
const CPelBuf refBuf = refPic->getRecoBuf(pu.blocks[COMPONENT_Y]);
IntTZSearchStruct cStruct;
cStruct.pcPatternKey = pcPatternKey;
cStruct.iRefStride = refBuf.stride;
cStruct.piRefY = refBuf.buf;
cStruct.imvShift = pu.cu->imv << 1;
cStruct.subShiftMode = 0; // used by intra pattern search function
// assume that intra BV is integer-pel precision
xSetIntraSearchRange(pu, pu.lwidth(), pu.lheight(), localSearchRangeX, localSearchRangeY, cMvSrchRngLT, cMvSrchRngRB);
// disable weighted prediction
setWpScalingDistParam(-1, REF_PIC_LIST_X, pu.cs->slice);
m_pcRdCost->getMotionCost(0, pu.cu->transQuantBypass);
m_pcRdCost->setPredictors(pcMvPred);
m_pcRdCost->setCostScale(0);
// Do integer search
xIntraPatternSearch(pu, cStruct, rcMv, ruiCost, &cMvSrchRngLT, &cMvSrchRngRB, pcMvPred);
}
}
// based on xSetSearchRange
void InterSearch::xSetIntraSearchRange(PredictionUnit& pu, int iRoiWidth, int iRoiHeight, const int localSearchRangeX, const int localSearchRangeY, Mv& rcMvSrchRngLT, Mv& rcMvSrchRngRB)
{
const SPS &sps = *pu.cs->sps;
int srLeft, srRight, srTop, srBottom;
const int cuPelX = pu.Y().x;
const int cuPelY = pu.Y().y;
const int iPicWidth = pu.cs->slice->getSPS()->getPicWidthInLumaSamples();
const int iPicHeight = pu.cs->slice->getSPS()->getPicHeightInLumaSamples();
srLeft = -std::min(cuPelX, localSearchRangeX);
srTop = -std::min(cuPelY, localSearchRangeY);
srRight = std::min(iPicWidth - cuPelX - iRoiWidth, localSearchRangeX);
srBottom = std::min(iPicHeight - cuPelY - iRoiHeight, localSearchRangeY);
rcMvSrchRngLT.setHor(srLeft);
rcMvSrchRngLT.setVer(srTop);
rcMvSrchRngRB.setHor(srRight);
rcMvSrchRngRB.setVer(srBottom);
rcMvSrchRngLT <<= 2;
rcMvSrchRngRB <<= 2;
xClipMv(rcMvSrchRngLT, pu.cu->lumaPos(),
xClipMv(rcMvSrchRngRB, pu.cu->lumaPos(),
rcMvSrchRngLT >>= 2;
rcMvSrchRngRB >>= 2;
}
bool InterSearch::predIBCSearch(CodingUnit& cu, Partitioner& partitioner, const int localSearchRangeX, const int localSearchRangeY, IbcHashMap& ibcHashMap)
// check only no greater than IBC_MAX_CAND_SIZE
if (cu.Y().width > IBC_MAX_CAND_SIZE || cu.Y().height > IBC_MAX_CAND_SIZE)
return false;
Mv cMvSrchRngLT;
Mv cMvSrchRngRB;
Mv cMv;
Mv cMvPred;
for (auto &pu : CU::traversePUs(cu))
{
m_maxCompIDToPred = MAX_NUM_COMPONENT;
CHECK(pu.cu != &cu, "PU is contained in another CU");
//////////////////////////////////////////////////////////
pu.cu->imv = 2;
AMVPInfo amvpInfo4Pel;
PU::fillMvpCand(pu, REF_PIC_LIST_0, pu.refIdx[REF_PIC_LIST_0], amvpInfo4Pel);
pu.cu->imv = 0;// (Int)cu.cs->sps->getSpsNext().getUseIMV(); // set as IMV=0 initially
Mv cMv, cMvPred[2];
AMVPInfo amvpInfo;
PU::fillMvpCand(pu, REF_PIC_LIST_0, pu.refIdx[REF_PIC_LIST_0], amvpInfo);
cMvPred[0].set(amvpInfo.mvCand[0].getHor() >> (2), amvpInfo.mvCand[0].getVer() >> (2)); // store in full pel accuracy, shift before use in search
cMvPred[1].set(amvpInfo.mvCand[1].getHor() >> (2), amvpInfo.mvCand[1].getVer() >> (2));
int iBvpNum = 2;
int bvpIdxBest = 0;
cMv.setZero();
Distortion cost = 0;
xxIBCHashSearch(pu, cMvPred, iBvpNum, cMv, bvpIdxBest, ibcHashMap);
}
if (cMv.getHor() == 0 && cMv.getVer() == 0)
{
// if hash search does not work or is not enabled
PelUnitBuf origBuf = pu.cs->getOrgBuf(pu);
xIBCEstimation(pu, origBuf, cMvPred, cMv, cost, localSearchRangeX, localSearchRangeY);
}
if (cMv.getHor() == 0 && cMv.getVer() == 0)
{
return false;
}
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
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
/////////////////////////////////////////////////////////
unsigned int bitsBVPBest, bitsBVPTemp;
bitsBVPBest = MAX_INT;
m_pcRdCost->setCostScale(0);
for (int bvpIdxTemp = 0; bvpIdxTemp<iBvpNum; bvpIdxTemp++)
{
m_pcRdCost->setPredictor(cMvPred[bvpIdxTemp]);
bitsBVPTemp = m_pcRdCost->getBitsOfVectorWithPredictor(cMv.getHor(), cMv.getVer(), 0);
if (bitsBVPTemp < bitsBVPBest)
{
bitsBVPBest = bitsBVPTemp;
bvpIdxBest = bvpIdxTemp;
if (cu.cs->sps->getSpsNext().getImvMode() && cMv != cMvPred[bvpIdxTemp])
pu.cu->imv = 1; // set as full-pel
else
pu.cu->imv = 0; // set as fractional-pel
}
unsigned int bitsBVPQP = MAX_UINT;
Mv mvPredQuadPel;
if ((cMv.getHor() % 4 == 0) && (cMv.getVer() % 4 == 0) && (pu.cs->sps->getSpsNext().getImvMode() == IMV_4PEL))
{
mvPredQuadPel = amvpInfo4Pel.mvCand[bvpIdxTemp];// cMvPred[bvpIdxTemp];
mvPredQuadPel >>= (4);
m_pcRdCost->setPredictor(mvPredQuadPel);
bitsBVPQP = m_pcRdCost->getBitsOfVectorWithPredictor(cMv.getHor() >> 2, cMv.getVer() >> 2, 0);
}
mvPredQuadPel <<= (2);
if (bitsBVPQP < bitsBVPBest && cMv != mvPredQuadPel)
{
bitsBVPBest = bitsBVPQP;
bvpIdxBest = bvpIdxTemp;
if (cu.cs->sps->getSpsNext().getImvMode())
pu.cu->imv = 2; // set as quad-pel
}
}
pu.bv = cMv;
cMv <<= (2);
pu.mv[REF_PIC_LIST_0] = cMv; // store in fractional pel accuracy
pu.mvpIdx[REF_PIC_LIST_0] = bvpIdxBest;
if(pu.cu->imv == 2 && cMv != amvpInfo4Pel.mvCand[bvpIdxBest])
pu.mvd[REF_PIC_LIST_0] = cMv - amvpInfo4Pel.mvCand[bvpIdxBest];
else
pu.mvd[REF_PIC_LIST_0] = cMv - amvpInfo.mvCand[bvpIdxBest];
if (pu.mvd[REF_PIC_LIST_0] == Mv(0, 0))
pu.cu->imv = 0;
if (pu.cu->imv == 2)
assert((cMv.getHor() % 16 == 0) && (cMv.getVer() % 16 == 0));
if (cu.cs->sps->getSpsNext().getUseIMV())
assert(pu.cu->imv>0 || pu.mvd[REF_PIC_LIST_0] == Mv());
if (!cu.cs->sps->getSpsNext().getUseIMV())
pu.mvd[REF_PIC_LIST_0] >>= (2);
pu.refIdx[REF_PIC_LIST_0] = pu.cs->slice->getNumRefIdx(REF_PIC_LIST_0) - 1;
pu.mv[REF_PIC_LIST_0].changePrecision(MV_PRECISION_QUARTER, MV_PRECISION_INTERNAL);
m_ctuRecord[cu.lumaPos()][cu.lumaSize()].bvRecord[pu.bv] = cost;
}
return true;
}
void InterSearch::xxIBCHashSearch(PredictionUnit& pu, Mv* mvPred, int numMvPred, Mv &mv, int& idxMvPred, IbcHashMap& ibcHashMap)
{
mv.setZero();
m_pcRdCost->setCostScale(0);
std::vector<Position> candPos;
if (ibcHashMap.ibcHashMatch(pu.Y(), candPos, *pu.cs, m_pcEncCfg->getIBCHashSearchMaxCand(), m_pcEncCfg->getIBCHashSearchRange4SmallBlk()))
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
{
unsigned int minCost = MAX_UINT;
const unsigned int lcuWidth = pu.cs->slice->getSPS()->getMaxCUWidth();
const int cuPelX = pu.Y().x;
const int cuPelY = pu.Y().y;
const int picWidth = pu.cs->slice->getSPS()->getPicWidthInLumaSamples();
const int picHeight = pu.cs->slice->getSPS()->getPicHeightInLumaSamples();
int roiWidth = pu.lwidth();
int roiHeight = pu.lheight();
for (std::vector<Position>::iterator pos = candPos.begin(); pos != candPos.end(); pos++)
{
Position bottomRight = pos->offset(pu.Y().width - 1, pu.Y().height - 1);
if (pu.cs->isDecomp(*pos, pu.cs->chType) && pu.cs->isDecomp(bottomRight, pu.cs->chType))
{
Position tmp = *pos - pu.Y().pos();
Mv candMv;
candMv.set(tmp.x, tmp.y);
if (!PU::isBlockVectorValid(pu, cuPelX, cuPelY, roiWidth, roiHeight, picWidth, picHeight, 0, 0, candMv.getHor(), candMv.getVer(), lcuWidth))
{
continue;
}
for (int n = 0; n < numMvPred; n++)
{
m_pcRdCost->setPredictor(mvPred[n]);
unsigned int cost = m_pcRdCost->getBitsOfVectorWithPredictor(candMv.getHor(), candMv.getVer(), 0);
if (cost < minCost)
{
mv = candMv;
idxMvPred = n;
minCost = cost;
}
int costQuadPel = MAX_UINT;
if ((candMv.getHor() % 4 == 0) && (candMv.getVer() % 4 == 0) && (pu.cs->sps->getSpsNext().getImvMode() == IMV_4PEL))
{
Mv mvPredQuadPel;
int imvShift = 2;
int offset = 1 << (imvShift - 1);
mvPredQuadPel.set(((mvPred[n].hor + offset) >> 2), ((mvPred[n].ver + offset) >> 2));
m_pcRdCost->setPredictor(mvPredQuadPel);
costQuadPel = m_pcRdCost->getBitsOfVectorWithPredictor(candMv.getHor() >> 2, candMv.getVer() >> 2, 0);
}
if (costQuadPel < minCost)
{
mv = candMv;
idxMvPred = n;
minCost = costQuadPel;
}
}
}
}
}
}

Karsten Suehring
committed

Karsten Suehring
committed
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
//! search of the best candidate for inter prediction
void InterSearch::predInterSearch(CodingUnit& cu, Partitioner& partitioner)
{
CodingStructure& cs = *cu.cs;
AMVPInfo amvp[2];
Mv cMvSrchRngLT;
Mv cMvSrchRngRB;
Mv cMvZero;
Mv cMv[2];
Mv cMvBi[2];
Mv cMvTemp[2][33];
Mv cMvHevcTemp[2][33];
int iNumPredDir = cs.slice->isInterP() ? 1 : 2;
Mv cMvPred[2][33];
Mv cMvPredBi[2][33];
int aaiMvpIdxBi[2][33];
int aaiMvpIdx[2][33];
int aaiMvpNum[2][33];
AMVPInfo aacAMVPInfo[2][33];
int iRefIdx[2]={0,0}; //If un-initialized, may cause SEGV in bi-directional prediction iterative stage.
int iRefIdxBi[2];
uint32_t uiMbBits[3] = {1, 1, 0};
uint32_t uiLastMode = 0;
uint32_t uiLastModeTemp = 0;
int iRefStart, iRefEnd;
#if JVET_M0444_SMVD
int symMode = 0;
#endif

Karsten Suehring
committed
int bestBiPRefIdxL1 = 0;
int bestBiPMvpL1 = 0;
Distortion biPDistTemp = std::numeric_limits<Distortion>::max();
uint8_t gbiIdx = (cu.cs->slice->isInterB() ? cu.GBiIdx : GBI_DEFAULT);
bool enforceGBiPred = false;

Karsten Suehring
committed
MergeCtx mergeCtx;
// Loop over Prediction Units
CHECK(!cu.firstPU, "CU does not contain any PUs");
uint32_t puIdx = 0;
auto &pu = *cu.firstPU;
#if JVET_M0246_AFFINE_AMVR
bool checkAffine = pu.cu->imv == 0 || pu.cu->slice->getSPS()->getAffineAmvrEnabledFlag();
bool checkNonAffine = pu.cu->imv == 0 || ( pu.cu->slice->getSPS()->getSpsNext().getUseIMV() &&
pu.cu->imv <= pu.cu->slice->getSPS()->getSpsNext().getImvMode() );
CodingUnit *bestCU = pu.cu->cs->bestCS != nullptr ? pu.cu->cs->bestCS->getCU( CHANNEL_TYPE_LUMA ) : nullptr;
#if JVET_M0444_SMVD
bool trySmvd = ( bestCU != nullptr && pu.cu->imv == 2 && checkAffine ) ? ( !bestCU->firstPU->mergeFlag && !bestCU->affine ) : true;
#endif
if ( pu.cu->imv && bestCU != nullptr && checkAffine )
{
checkAffine = !( bestCU->firstPU->mergeFlag || !bestCU->affine );
}
if ( pu.cu->imv == 2 && checkNonAffine && pu.cu->slice->getSPS()->getAffineAmvrEnabledFlag() )
{
checkNonAffine = m_affineMotion.hevcCost[1] < m_affineMotion.hevcCost[0] * 1.06f;
}
#endif

Karsten Suehring
committed
{
// motion estimation only evaluates luma component
m_maxCompIDToPred = MAX_NUM_COMPONENT;
// m_maxCompIDToPred = COMPONENT_Y;
CHECK(pu.cu != &cu, "PU is contained in another CU");
if (cu.cs->sps->getSBTMVPEnabledFlag())

Karsten Suehring
committed
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
{
Size bufSize = g_miScaling.scale(pu.lumaSize());
mergeCtx.subPuMvpMiBuf = MotionBuf(m_SubPuMiBuf, bufSize);
}
PU::spanMotionInfo( pu );
Distortion uiHevcCost = std::numeric_limits<Distortion>::max();
Distortion uiAffineCost = std::numeric_limits<Distortion>::max();
Distortion uiCost[2] = { std::numeric_limits<Distortion>::max(), std::numeric_limits<Distortion>::max() };
Distortion uiCostBi = std::numeric_limits<Distortion>::max();
Distortion uiCostTemp;
uint32_t uiBits[3];
uint32_t uiBitsTemp;
Distortion bestBiPDist = std::numeric_limits<Distortion>::max();
Distortion uiCostTempL0[MAX_NUM_REF];
for (int iNumRef=0; iNumRef < MAX_NUM_REF; iNumRef++)
{
uiCostTempL0[iNumRef] = std::numeric_limits<Distortion>::max();
}
uint32_t uiBitsTempL0[MAX_NUM_REF];
Mv mvValidList1;
int refIdxValidList1 = 0;
uint32_t bitsValidList1 = MAX_UINT;
Distortion costValidList1 = std::numeric_limits<Distortion>::max();
PelUnitBuf origBuf = pu.cs->getOrgBuf( pu );
xGetBlkBits( cs.slice->isInterP(), puIdx, uiLastMode, uiMbBits );

Karsten Suehring
committed
m_pcRdCost->selectMotionLambda( cu.transQuantBypass );
unsigned imvShift = pu.cu->imv << 1;
#if JVET_M0246_AFFINE_AMVR
if ( checkNonAffine )
{
#endif

Karsten Suehring
committed
// Uni-directional prediction
for ( int iRefList = 0; iRefList < iNumPredDir; iRefList++ )
{
RefPicList eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
if (cs.slice->getSPS()->getSpsNext().getIBCMode() && eRefPicList == REF_PIC_LIST_0)
{
refPicNumber--;
}
for (int iRefIdxTemp = 0; iRefIdxTemp < refPicNumber; iRefIdxTemp++)

Karsten Suehring
committed
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
{
uiBitsTemp = uiMbBits[iRefList];
if ( cs.slice->getNumRefIdx(eRefPicList) > 1 )
{
uiBitsTemp += iRefIdxTemp+1;
if ( iRefIdxTemp == cs.slice->getNumRefIdx(eRefPicList)-1 )
{
uiBitsTemp--;
}
}
xEstimateMvPredAMVP( pu, origBuf, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp], amvp[eRefPicList], false, &biPDistTemp);
aaiMvpIdx[iRefList][iRefIdxTemp] = pu.mvpIdx[eRefPicList];
aaiMvpNum[iRefList][iRefIdxTemp] = pu.mvpNum[eRefPicList];
if(cs.slice->getMvdL1ZeroFlag() && iRefList==1 && biPDistTemp < bestBiPDist)
{
bestBiPDist = biPDistTemp;
bestBiPMvpL1 = aaiMvpIdx[iRefList][iRefIdxTemp];
bestBiPRefIdxL1 = iRefIdxTemp;
}
uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdx[iRefList][iRefIdxTemp]][AMVP_MAX_NUM_CANDS];
if ( m_pcEncCfg->getFastMEForGenBLowDelayEnabled() && iRefList == 1 ) // list 1
{
if ( cs.slice->getList1IdxToList0Idx( iRefIdxTemp ) >= 0 )
{
cMvTemp[1][iRefIdxTemp] = cMvTemp[0][cs.slice->getList1IdxToList0Idx( iRefIdxTemp )];
uiCostTemp = uiCostTempL0[cs.slice->getList1IdxToList0Idx( iRefIdxTemp )];
/*first subtract the bit-rate part of the cost of the other list*/
uiCostTemp -= m_pcRdCost->getCost( uiBitsTempL0[cs.slice->getList1IdxToList0Idx( iRefIdxTemp )] );
/*correct the bit-rate part of the current ref*/
m_pcRdCost->setPredictor ( cMvPred[iRefList][iRefIdxTemp] );
uiBitsTemp += m_pcRdCost->getBitsOfVectorWithPredictor( cMvTemp[1][iRefIdxTemp].getHor(), cMvTemp[1][iRefIdxTemp].getVer(), imvShift );
/*calculate the correct cost*/
uiCostTemp += m_pcRdCost->getCost( uiBitsTemp );
}
else
{
xMotionEstimation( pu, origBuf, eRefPicList, cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], aaiMvpIdx[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp, amvp[eRefPicList] );
}
}
else
{
xMotionEstimation( pu, origBuf, eRefPicList, cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], aaiMvpIdx[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp, amvp[eRefPicList] );
}
if( cu.cs->sps->getSpsNext().getUseGBi() && cu.GBiIdx == GBI_DEFAULT && cu.cs->slice->isInterB() )
{
const bool checkIdentical = true;
m_uniMotions.setReadMode(checkIdentical, (uint32_t)iRefList, (uint32_t)iRefIdxTemp);
m_uniMotions.copyFrom(cMvTemp[iRefList][iRefIdxTemp], uiCostTemp - m_pcRdCost->getCost(uiBitsTemp), (uint32_t)iRefList, (uint32_t)iRefIdxTemp);

Karsten Suehring
committed
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
xCopyAMVPInfo( &amvp[eRefPicList], &aacAMVPInfo[iRefList][iRefIdxTemp]); // must always be done ( also when AMVP_MODE = AM_NONE )
xCheckBestMVP( eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPred[iRefList][iRefIdxTemp], aaiMvpIdx[iRefList][iRefIdxTemp], amvp[eRefPicList], uiBitsTemp, uiCostTemp, pu.cu->imv );
if ( iRefList == 0 )
{
uiCostTempL0[iRefIdxTemp] = uiCostTemp;
uiBitsTempL0[iRefIdxTemp] = uiBitsTemp;
}
if ( uiCostTemp < uiCost[iRefList] )
{
uiCost[iRefList] = uiCostTemp;
uiBits[iRefList] = uiBitsTemp; // storing for bi-prediction
// set motion
cMv [iRefList] = cMvTemp[iRefList][iRefIdxTemp];
iRefIdx[iRefList] = iRefIdxTemp;
}
if ( iRefList == 1 && uiCostTemp < costValidList1 && cs.slice->getList1IdxToList0Idx( iRefIdxTemp ) < 0 )
{
costValidList1 = uiCostTemp;
bitsValidList1 = uiBitsTemp;
// set motion
mvValidList1 = cMvTemp[iRefList][iRefIdxTemp];
refIdxValidList1 = iRefIdxTemp;
}
}
}
if (cu.Y().width > 8 && cu.Y().height > 8 && cu.slice->getSPS()->getSpsNext().getUseAffine()
#if JVET_M0246_AFFINE_AMVR
&& checkAffine
#else

Karsten Suehring
committed
&& cu.imv == 0
&& (gbiIdx == GBI_DEFAULT || m_affineModeSelected || !m_pcEncCfg->getUseGBiFast())

Karsten Suehring
committed
)
{
::memcpy( cMvHevcTemp, cMvTemp, sizeof( cMvTemp ) );
}
// Bi-predictive Motion estimation
if( ( cs.slice->isInterB() ) && ( PU::isBipredRestriction( pu ) == false )
&& (cu.slice->getCheckLDC() || gbiIdx == GBI_DEFAULT || !m_affineModeSelected || !m_pcEncCfg->getUseGBiFast())
)

Karsten Suehring
committed
{
cMvBi[0] = cMv[0];
cMvBi[1] = cMv[1];
iRefIdxBi[0] = iRefIdx[0];
iRefIdxBi[1] = iRefIdx[1];
::memcpy( cMvPredBi, cMvPred, sizeof( cMvPred ) );
::memcpy( aaiMvpIdxBi, aaiMvpIdx, sizeof( aaiMvpIdx ) );
uint32_t uiMotBits[2];
if(cs.slice->getMvdL1ZeroFlag())
{
xCopyAMVPInfo(&aacAMVPInfo[1][bestBiPRefIdxL1], &amvp[REF_PIC_LIST_1]);
aaiMvpIdxBi[1][bestBiPRefIdxL1] = bestBiPMvpL1;
cMvPredBi [1][bestBiPRefIdxL1] = amvp[REF_PIC_LIST_1].mvCand[bestBiPMvpL1];
cMvBi [1] = cMvPredBi[1][bestBiPRefIdxL1];
iRefIdxBi[1] = bestBiPRefIdxL1;
pu.mv [REF_PIC_LIST_1] = cMvBi[1];
pu.mv[REF_PIC_LIST_1].changePrecision(MV_PRECISION_QUARTER, MV_PRECISION_INTERNAL);

Karsten Suehring
committed
1811
1812
1813
1814
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
pu.refIdx[REF_PIC_LIST_1] = iRefIdxBi[1];
pu.mvpIdx[REF_PIC_LIST_1] = bestBiPMvpL1;
PelUnitBuf predBufTmp = m_tmpPredStorage[REF_PIC_LIST_1].getBuf( UnitAreaRelative(cu, pu) );
motionCompensation( pu, predBufTmp, REF_PIC_LIST_1 );
uiMotBits[0] = uiBits[0] - uiMbBits[0];
uiMotBits[1] = uiMbBits[1];
if ( cs.slice->getNumRefIdx(REF_PIC_LIST_1) > 1 )
{
uiMotBits[1] += bestBiPRefIdxL1 + 1;
if ( bestBiPRefIdxL1 == cs.slice->getNumRefIdx(REF_PIC_LIST_1)-1 )
{
uiMotBits[1]--;
}
}
uiMotBits[1] += m_auiMVPIdxCost[aaiMvpIdxBi[1][bestBiPRefIdxL1]][AMVP_MAX_NUM_CANDS];
uiBits[2] = uiMbBits[2] + uiMotBits[0] + uiMotBits[1];
cMvTemp[1][bestBiPRefIdxL1] = cMvBi[1];
}
else
{
uiMotBits[0] = uiBits[0] - uiMbBits[0];
uiMotBits[1] = uiBits[1] - uiMbBits[1];
uiBits[2] = uiMbBits[2] + uiMotBits[0] + uiMotBits[1];
}
// 4-times iteration (default)
int iNumIter = 4;
// fast encoder setting: only one iteration
if ( m_pcEncCfg->getFastInterSearchMode()==FASTINTERSEARCH_MODE1 || m_pcEncCfg->getFastInterSearchMode()==FASTINTERSEARCH_MODE2 || cs.slice->getMvdL1ZeroFlag() )
{
iNumIter = 1;
}
enforceGBiPred = (gbiIdx != GBI_DEFAULT);

Karsten Suehring
committed
for ( int iIter = 0; iIter < iNumIter; iIter++ )
{
int iRefList = iIter % 2;
if ( m_pcEncCfg->getFastInterSearchMode()==FASTINTERSEARCH_MODE1 || m_pcEncCfg->getFastInterSearchMode()==FASTINTERSEARCH_MODE2 )
{
if( uiCost[0] <= uiCost[1] )
{
iRefList = 1;
}
else
{
iRefList = 0;
}
if( gbiIdx != GBI_DEFAULT )
{
iRefList = ( abs( getGbiWeight(gbiIdx, REF_PIC_LIST_0 ) ) > abs( getGbiWeight(gbiIdx, REF_PIC_LIST_1 ) ) ? 1 : 0 );
}

Karsten Suehring
committed
}
else if ( iIter == 0 )
{
iRefList = 0;
}
if ( iIter == 0 && !cs.slice->getMvdL1ZeroFlag())
{
pu.mv [1 - iRefList] = cMv [1 - iRefList];
pu.mv[1 - iRefList].changePrecision(MV_PRECISION_QUARTER, MV_PRECISION_INTERNAL);

Karsten Suehring
committed
pu.refIdx[1 - iRefList] = iRefIdx[1 - iRefList];
PelUnitBuf predBufTmp = m_tmpPredStorage[1 - iRefList].getBuf( UnitAreaRelative(cu, pu) );
motionCompensation( pu, predBufTmp, RefPicList(1 - iRefList) );
}
RefPicList eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
if(cs.slice->getMvdL1ZeroFlag())
{
iRefList = 0;
eRefPicList = REF_PIC_LIST_0;
}
bool bChanged = false;
iRefStart = 0;
iRefEnd = cs.slice->getNumRefIdx(eRefPicList)-1;
if (cs.slice->getSPS()->getSpsNext().getIBCMode() && eRefPicList == REF_PIC_LIST_0)

Christian Helmrich
committed
for (int iRefIdxTemp = iRefStart; iRefIdxTemp <= iRefEnd; iRefIdxTemp++)

Karsten Suehring
committed
{
if( m_pcEncCfg->getUseGBiFast() && (gbiIdx != GBI_DEFAULT)
&& (pu.cu->slice->getRefPic(eRefPicList, iRefIdxTemp)->getPOC() == pu.cu->slice->getRefPic(RefPicList(1 - iRefList), pu.refIdx[1 - iRefList])->getPOC())
&& (!pu.cu->imv && pu.cu->slice->getTLayer()>1))
{
continue;
}

Karsten Suehring
committed
uiBitsTemp = uiMbBits[2] + uiMotBits[1-iRefList];
uiBitsTemp += ((cs.slice->getSPS()->getSpsNext().getUseGBi() == true) ? getWeightIdxBits(gbiIdx) : 0);

Karsten Suehring
committed
if ( cs.slice->getNumRefIdx(eRefPicList) > 1 )
{
uiBitsTemp += iRefIdxTemp+1;
if ( iRefIdxTemp == cs.slice->getNumRefIdx(eRefPicList)-1 )
{
uiBitsTemp--;
}
}
uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdxBi[iRefList][iRefIdxTemp]][AMVP_MAX_NUM_CANDS];
#if JVET_M0444_SMVD
if ( cs.slice->getBiDirPred() )
{
uiBitsTemp += 1; // add one bit for symmetrical MVD mode
}
#endif

Karsten Suehring
committed
// call ME
xCopyAMVPInfo(&aacAMVPInfo[iRefList][iRefIdxTemp], &amvp[eRefPicList] );
xMotionEstimation ( pu, origBuf, eRefPicList, cMvPredBi[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], aaiMvpIdxBi[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp, amvp[eRefPicList], true );
xCheckBestMVP( eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPredBi[iRefList][iRefIdxTemp], aaiMvpIdxBi[iRefList][iRefIdxTemp], amvp[eRefPicList], uiBitsTemp, uiCostTemp, pu.cu->imv);
if ( uiCostTemp < uiCostBi )
{
bChanged = true;
cMvBi[iRefList] = cMvTemp[iRefList][iRefIdxTemp];
iRefIdxBi[iRefList] = iRefIdxTemp;
uiCostBi = uiCostTemp;
uiMotBits[iRefList] = uiBitsTemp - uiMbBits[2] - uiMotBits[1-iRefList];
uiMotBits[iRefList] -= ((cs.slice->getSPS()->getSpsNext().getUseGBi() == true) ? getWeightIdxBits(gbiIdx) : 0);

Karsten Suehring
committed
uiBits[2] = uiBitsTemp;
if(iNumIter!=1)
{
// Set motion
pu.mv [eRefPicList] = cMvBi [iRefList];
pu.mv[eRefPicList].changePrecision(MV_PRECISION_QUARTER, MV_PRECISION_INTERNAL);

Karsten Suehring
committed
pu.refIdx[eRefPicList] = iRefIdxBi[iRefList];
PelUnitBuf predBufTmp = m_tmpPredStorage[iRefList].getBuf( UnitAreaRelative(cu, pu) );
motionCompensation( pu, predBufTmp, eRefPicList );
}
}
} // for loop-iRefIdxTemp
if ( !bChanged )
{
if ((uiCostBi <= uiCost[0] && uiCostBi <= uiCost[1]) || enforceGBiPred)

Karsten Suehring
committed
{
xCopyAMVPInfo(&aacAMVPInfo[0][iRefIdxBi[0]], &amvp[REF_PIC_LIST_0]);
xCheckBestMVP( REF_PIC_LIST_0, cMvBi[0], cMvPredBi[0][iRefIdxBi[0]], aaiMvpIdxBi[0][iRefIdxBi[0]], amvp[eRefPicList], uiBits[2], uiCostBi, pu.cu->imv);
if(!cs.slice->getMvdL1ZeroFlag())
{
xCopyAMVPInfo(&aacAMVPInfo[1][iRefIdxBi[1]], &amvp[REF_PIC_LIST_1]);
xCheckBestMVP( REF_PIC_LIST_1, cMvBi[1], cMvPredBi[1][iRefIdxBi[1]], aaiMvpIdxBi[1][iRefIdxBi[1]], amvp[eRefPicList], uiBits[2], uiCostBi, pu.cu->imv);
}
}
break;
}
} // for loop-iter
cu.refIdxBi[0] = iRefIdxBi[0];
cu.refIdxBi[1] = iRefIdxBi[1];
#if JVET_M0246_AFFINE_AMVR
if ( cs.slice->getBiDirPred() && trySmvd )
#else
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
{
Distortion symCost;
Mv cMvPredSym[2];
int mvpIdxSym[2];
int curRefList = REF_PIC_LIST_0;
int tarRefList = 1 - curRefList;
RefPicList eCurRefList = (curRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0);
int refIdxCur = cs.slice->getSymRefIdx( curRefList );
int refIdxTar = cs.slice->getSymRefIdx( tarRefList );
MvField cCurMvField, cTarMvField;
Distortion costStart = std::numeric_limits<Distortion>::max();
for ( int i = 0; i < aacAMVPInfo[curRefList][refIdxCur].numCand; i++ )
{
for ( int j = 0; j < aacAMVPInfo[tarRefList][refIdxTar].numCand; j++ )
{
cCurMvField.setMvField( aacAMVPInfo[curRefList][refIdxCur].mvCand[i], refIdxCur );
cTarMvField.setMvField( aacAMVPInfo[tarRefList][refIdxTar].mvCand[j], refIdxTar );
Distortion cost = xGetSymmetricCost( pu, origBuf, eCurRefList, cCurMvField, cTarMvField, gbiIdx );
if ( cost < costStart )
{