fillMvpCand在xEstimateMvPredAMVP函数中被调用,用来构建AMVP列表,将AMVP候选存储于amvpInfo。
AMVP列表的构建,添加顺序为:
1.左下、左pu的无缩放信息
2.左下、左pu的缩放信息
3.右上、上、左上pu无缩放信息
4.如果左下和左的pu都无效,那么添加右上、上、左上pu的缩放信息
5.添加TMVP
6.添加HMVP
7.补0
其中,调用addMVPCandUnscaled函数添加相应位置处的pu运动信息,调用addMVPCandWithScaling函数则用来添加相应位置处pu运动信息经过scale之后的运动信息,scale方式就是和tmvp中一样的按照poc距离缩放。
fillMvpCand函数与VTM1.0中代码的区别在于:HMVP的添加,最后部分关于Imv的mvp舍入部分,也不同。其余左、上、时域mvp的添加都相同。其中调用的addMVPCandUnscaled函数等都和VTM1.0没有任何变化。
void PU::fillMvpCand(PredictionUnit &pu, const RefPicList &eRefPicList, const int &refIdx, AMVPInfo &amvpInfo)
{
CodingStructure &cs = *pu.cs;
AMVPInfo *pInfo = &amvpInfo; //AMVP列表
pInfo->numCand = 0; //AMVP候选的个数
if (refIdx < 0)
{
return;
}
//-- Get Spatial MV
Position posLT = pu.Y().topLeft();
Position posRT = pu.Y().topRight();
Position posLB = pu.Y().bottomLeft(); //pu的左上、右上、左下的pos
bool isScaledFlagLX = false; /// variable name from specification; true when the PUs below left or left are available (availableA0 || availableA1).
{ //左下的pu信息
const PredictionUnit* tmpPU = cs.getPURestricted( posLB.offset( -1, 1 ), pu, pu.chType ); // getPUBelowLeft(idx, partIdxLB);
isScaledFlagLX = tmpPU != NULL && CU::isInter( *tmpPU->cu );
if( !isScaledFlagLX ) //左下pu不存在时,获取左侧pu信息
{
tmpPU = cs.getPURestricted( posLB.offset( -1, 0 ), pu, pu.chType );
isScaledFlagLX = tmpPU != NULL && CU::isInter( *tmpPU->cu );
}
}
// Left predictor search
if( isScaledFlagLX ) //左下、左侧pu其中之一存在,isScaledFlagLX为true
{
bool bAdded = addMVPCandUnscaled( pu, eRefPicList, refIdx, posLB, MD_BELOW_LEFT, *pInfo );
if( !bAdded )
{
bAdded = addMVPCandUnscaled( pu, eRefPicList, refIdx, posLB, MD_LEFT, *pInfo ); //AMVP列表添加左下的pu信息(无缩放)
if( !bAdded )
{ //左侧pu无缩放不能添加,AMVP列表添加左左下的pu信息(缩放)
bAdded = addMVPCandWithScaling( pu, eRefPicList, refIdx, posLB, MD_BELOW_LEFT, *pInfo );//若左下pu无缩放不能添加,AMVP列表添加左侧的pu信息(无缩放)
if( !bAdded )
{ //左下pu信息缩放不能添加,AMVP列表添加左侧的pu信息(缩放)
addMVPCandWithScaling( pu, eRefPicList, refIdx, posLB, MD_LEFT, *pInfo );
}
}
}
}
// Above predictor search
{ //AMVP列表添加右上的pu信息(无缩放)
bool bAdded = addMVPCandUnscaled( pu, eRefPicList, refIdx, posRT, MD_ABOVE_RIGHT, *pInfo );
if( !bAdded )
{ //右上pu无缩放不能添加,AMVP列表添加上侧的pu信息(无缩放)
bAdded = addMVPCandUnscaled( pu, eRefPicList, refIdx, posRT, MD_ABOVE, *pInfo );
if( !bAdded )
{ //上侧pu无缩放不能添加,AMVP列表添加左上的pu信息(无缩放)
addMVPCandUnscaled( pu, eRefPicList, refIdx, posLT, MD_ABOVE_LEFT, *pInfo );
}
}
}
if( !isScaledFlagLX ) //如果左侧和左下的pu都不可用
{ //AMVP列表添加右上的pu信息(缩放)
bool bAdded = addMVPCandWithScaling( pu, eRefPicList, refIdx, posRT, MD_ABOVE_RIGHT, *pInfo );
if( !bAdded )
{ //右上pu缩放信息不能添加,AMVP列表添加上侧的pu信息(缩放)
bAdded = addMVPCandWithScaling( pu, eRefPicList, refIdx, posRT, MD_ABOVE, *pInfo );
if( !bAdded )
{ //上侧pu缩放信息不能添加,AMVP列表添加左上的pu信息(缩放)
addMVPCandWithScaling( pu, eRefPicList, refIdx, posLT, MD_ABOVE_LEFT, *pInfo );
}
}
}
if( pu.cu->imv != 0)
{
unsigned imvShift = pu.cu->imv << 1;
#if REMOVE_MV_ADAPT_PREC
imvShift += VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE;
#endif
for( int i = 0; i < pInfo->numCand; i++ )
{
roundMV( pInfo->mvCand[i], imvShift );
}
}
if( pInfo->numCand == 2 )
{
if( pInfo->mvCand[0] == pInfo->mvCand[1] )
{
pInfo->numCand = 1; //AMVP列表冗余裁剪
}
}
if( cs.slice->getEnableTMVPFlag() ) //AMVP列表中添加TMVP的运动信息
{
// Get Temporal Motion Predictor
const int refIdx_Col = refIdx;
Position posRB = pu.Y().bottomRight().offset(-3, -3);
const PreCalcValues& pcv = *cs.pcv;
Position posC0;
bool C0Avail = false;
Position posC1 = pu.Y().center(); //C1为中心位置,C0右下位置
Mv cColMv;
if( ( ( posRB.x + pcv.minCUWidth ) < pcv.lumaWidth ) && ( ( posRB.y + pcv.minCUHeight ) < pcv.lumaHeight ) )
{
Position posInCtu( posRB.x & pcv.maxCUWidthMask, posRB.y & pcv.maxCUHeightMask );
if ((posInCtu.x + 4 < pcv.maxCUWidth) && // is not at the last column of CTU
(posInCtu.y + 4 < pcv.maxCUHeight)) // is not at the last row of CTU
{
posC0 = posRB.offset(4, 4);
C0Avail = true;
}
else if (posInCtu.x + 4 < pcv.maxCUWidth) // is not at the last column of CTU But is last row of CTU
{
// in the reference the CTU address is not set - thus probably resulting in no using this C0 possibility
posC0 = posRB.offset(4, 4);
}
else if (posInCtu.y + 4 < pcv.maxCUHeight) // is not at the last row of CTU But is last column of CTU
{
posC0 = posRB.offset(4, 4);
C0Avail = true;
}
else //is the right bottom corner of CTU
{
// same as for last column but not last row
posC0 = posRB.offset(4, 4);
}
}
//getColocatedMVP获取时域MVP
if ((C0Avail && getColocatedMVP(pu, eRefPicList, posC0, cColMv, refIdx_Col)) || getColocatedMVP(pu, eRefPicList, posC1, cColMv, refIdx_Col))
{
#if JVET_L0266_HMVP
if (pu.cu->imv != 0)
{
unsigned imvShift = pu.cu->imv << 1;
#if REMOVE_MV_ADAPT_PREC
imvShift += VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE;
#endif
roundMV(cColMv, imvShift);
}
int i = 0;
for (i = 0; i < pInfo->numCand; i++)
{
if (cColMv == pInfo->mvCand[i])
{
break;
}
}
if (i == pInfo->numCand)
{
pInfo->mvCand[pInfo->numCand++] = cColMv; //获得到的TMVP添加到AMVP列表中
}
#else
pInfo->mvCand[pInfo->numCand++] = cColMv;
#endif
}
}
#if JVET_L0266_HMVP
if (pInfo->numCand < AMVP_MAX_NUM_CANDS) //HMVP
{
const int currRefPOC = cs.slice->getRefPic(eRefPicList, refIdx)->getPOC();
const RefPicList eRefPicList2nd = (eRefPicList == REF_PIC_LIST_0) ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
addAMVPHMVPCand(pu, eRefPicList, eRefPicList2nd, currRefPOC, *pInfo, pu.cu->imv); //添加HMVP的候选信息
}
#endif
if (pInfo->numCand > AMVP_MAX_NUM_CANDS)
{
pInfo->numCand = AMVP_MAX_NUM_CANDS;
}
while (pInfo->numCand < AMVP_MAX_NUM_CANDS) //补0
{
#if !REMOVE_MV_ADAPT_PREC
const bool prec = pInfo->mvCand[pInfo->numCand].highPrec;
pInfo->mvCand[pInfo->numCand] = Mv( 0, 0, prec );
#else
pInfo->mvCand[pInfo->numCand] = Mv( 0, 0 );
#endif
pInfo->numCand++;
}
#if !REMOVE_MV_ADAPT_PREC
if (pu.cs->sps->getSpsNext().getUseHighPrecMv())
{
#endif
for (Mv &mv : pInfo->mvCand)
{
#if REMOVE_MV_ADAPT_PREC
const int nShift = VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE;
const int nOffset = 1 << (nShift - 1);
mv.hor = mv.hor >= 0 ? (mv.hor + nOffset) >> nShift : -((-mv.hor + nOffset) >> nShift);
mv.ver = mv.ver >= 0 ? (mv.ver + nOffset) >> nShift : -((-mv.ver + nOffset) >> nShift);
#else
if (mv.highPrec) mv.setLowPrec();
#endif
}
#if !REMOVE_MV_ADAPT_PREC
}
#endif
if (pu.cu->imv != 0)
{
unsigned imvShift = pu.cu->imv << 1;
for (int i = 0; i < pInfo->numCand; i++)
{
roundMV(pInfo->mvCand[i], imvShift);
}
}
#if !REMOVE_MV_ADAPT_PREC
if (pu.cs->sps->getSpsNext().getUseHighPrecMv())
{
for (Mv &mv : pInfo->mvCand)
{
if (mv.highPrec) mv.setLowPrec();
}
}
#endif
}