Newer
Older

Karsten Suehring
committed
/* 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.
*
* Copyright (c) 2010-2019, ITU/ISO/IEC

Karsten Suehring
committed
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
* 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 Prediction.cpp
\brief prediction class
*/
#include "InterPrediction.h"
#include "Buffer.h"
#include "UnitTools.h"
#include <memory.h>
#include <algorithm>
//! \ingroup CommonLib
//! \{
// ====================================================================================================================
// Constructor / destructor / initialize
// ====================================================================================================================
InterPrediction::InterPrediction()
:
m_currChromaFormat( NUM_CHROMA_FORMAT )
, m_maxCompIDToPred ( MAX_NUM_COMPONENT )
, m_pcRdCost ( nullptr )
, m_gradX0(nullptr)
, m_gradY0(nullptr)
, m_gradX1(nullptr)
, m_gradY1(nullptr)

Karsten Suehring
committed
{
for( uint32_t ch = 0; ch < MAX_NUM_COMPONENT; ch++ )
{
for( uint32_t refList = 0; refList < NUM_REF_PIC_LIST_01; refList++ )
{
m_acYuvPred[refList][ch] = nullptr;
}
}
for( uint32_t c = 0; c < MAX_NUM_COMPONENT; c++ )
{
for( uint32_t i = 0; i < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS_SIGNAL; i++ )

Karsten Suehring
committed
{
for( uint32_t j = 0; j < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS_SIGNAL; j++ )

Karsten Suehring
committed
{
m_filteredBlock[i][j][c] = nullptr;
}
m_filteredBlockTmp[i][c] = nullptr;
}
}
#if JVET_M0147_DMVR
m_cYuvPredTempDMVRL1 = nullptr;
m_cYuvPredTempDMVRL0 = nullptr;
for (uint32_t ch = 0; ch < MAX_NUM_COMPONENT; ch++)
{
m_cRefSamplesDMVRL0[ch] = nullptr;
m_cRefSamplesDMVRL1[ch] = nullptr;
}
#endif

Karsten Suehring
committed
}
InterPrediction::~InterPrediction()
{
destroy();
}
void InterPrediction::destroy()
{
for( uint32_t i = 0; i < NUM_REF_PIC_LIST_01; i++ )
{
for( uint32_t c = 0; c < MAX_NUM_COMPONENT; c++ )
{
xFree( m_acYuvPred[i][c] );
m_acYuvPred[i][c] = nullptr;
}
}
for( uint32_t c = 0; c < MAX_NUM_COMPONENT; c++ )
{
for( uint32_t i = 0; i < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS_SIGNAL; i++ )

Karsten Suehring
committed
{
for( uint32_t j = 0; j < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS_SIGNAL; j++ )

Karsten Suehring
committed
{
xFree( m_filteredBlock[i][j][c] );
m_filteredBlock[i][j][c] = nullptr;
}
xFree( m_filteredBlockTmp[i][c] );
m_filteredBlockTmp[i][c] = nullptr;
}
}
if (m_storedMv != nullptr)
{
delete[]m_storedMv;
xFree(m_gradX0); m_gradX0 = nullptr;
xFree(m_gradY0); m_gradY0 = nullptr;
xFree(m_gradX1); m_gradX1 = nullptr;
xFree(m_gradY1); m_gradY1 = nullptr;
#if JVET_M0147_DMVR
xFree(m_cYuvPredTempDMVRL0);
m_cYuvPredTempDMVRL0 = nullptr;
xFree(m_cYuvPredTempDMVRL1);
m_cYuvPredTempDMVRL1 = nullptr;
for (uint32_t ch = 0; ch < MAX_NUM_COMPONENT; ch++)
{
xFree(m_cRefSamplesDMVRL0[ch]);
m_cRefSamplesDMVRL0[ch] = nullptr;
xFree(m_cRefSamplesDMVRL1[ch]);
m_cRefSamplesDMVRL1[ch] = nullptr;
}
#endif

Karsten Suehring
committed
}
void InterPrediction::init( RdCost* pcRdCost, ChromaFormat chromaFormatIDC )
{
m_pcRdCost = pcRdCost;
// if it has been initialised before, but the chroma format has changed, release the memory and start again.
if( m_acYuvPred[REF_PIC_LIST_0][COMPONENT_Y] != nullptr && m_currChromaFormat != chromaFormatIDC )
{
destroy();
}
m_currChromaFormat = chromaFormatIDC;
if( m_acYuvPred[REF_PIC_LIST_0][COMPONENT_Y] == nullptr ) // check if first is null (in which case, nothing initialised yet)
{
for( uint32_t c = 0; c < MAX_NUM_COMPONENT; c++ )
{
int extWidth = MAX_CU_SIZE + (2 * BIO_EXTEND_SIZE + 2) + 16;
int extHeight = MAX_CU_SIZE + (2 * BIO_EXTEND_SIZE + 2) + 1;
#if JVET_M0147_DMVR
extWidth = extWidth > (MAX_CU_SIZE + (DMVR_NUM_ITERATION << 1) + 16) ? extWidth : MAX_CU_SIZE + (DMVR_NUM_ITERATION << 1) + 16;
extHeight = extHeight > (MAX_CU_SIZE + (DMVR_NUM_ITERATION << 1) + 1) ? extHeight : MAX_CU_SIZE + (DMVR_NUM_ITERATION << 1) + 1;
#endif
for( uint32_t i = 0; i < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS_SIGNAL; i++ )

Karsten Suehring
committed
{
m_filteredBlockTmp[i][c] = ( Pel* ) xMalloc( Pel, ( extWidth + 4 ) * ( extHeight + 7 + 4 ) );
for( uint32_t j = 0; j < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS_SIGNAL; j++ )

Karsten Suehring
committed
{
m_filteredBlock[i][j][c] = ( Pel* ) xMalloc( Pel, extWidth * extHeight );
}
}
// new structure
for( uint32_t i = 0; i < NUM_REF_PIC_LIST_01; i++ )
{
m_acYuvPred[i][c] = ( Pel* ) xMalloc( Pel, MAX_CU_SIZE * MAX_CU_SIZE );
}
}
m_triangleBuf.create(UnitArea(chromaFormatIDC, Area(0, 0, MAX_CU_SIZE, MAX_CU_SIZE)));

Karsten Suehring
committed
m_iRefListIdx = -1;
m_gradX0 = (Pel*)xMalloc(Pel, BIO_TEMP_BUFFER_SIZE);
m_gradY0 = (Pel*)xMalloc(Pel, BIO_TEMP_BUFFER_SIZE);
m_gradX1 = (Pel*)xMalloc(Pel, BIO_TEMP_BUFFER_SIZE);
m_gradY1 = (Pel*)xMalloc(Pel, BIO_TEMP_BUFFER_SIZE);

Karsten Suehring
committed
}
#if JVET_M0147_DMVR
m_cYuvPredTempDMVRL0 = (Pel*)xMalloc(Pel, (MAX_CU_SIZE + (DMVR_NUM_ITERATION << 1)) * (MAX_CU_SIZE + (DMVR_NUM_ITERATION << 1)));
m_cYuvPredTempDMVRL1 = (Pel*)xMalloc(Pel, (MAX_CU_SIZE + (DMVR_NUM_ITERATION << 1)) * (MAX_CU_SIZE + (DMVR_NUM_ITERATION << 1)));
for (uint32_t ch = 0; ch < MAX_NUM_COMPONENT; ch++)
{
m_cRefSamplesDMVRL0[ch] = (Pel*)xMalloc(Pel, (MAX_CU_SIZE + (DMVR_NUM_ITERATION << 1) + NTAPS_LUMA) * (MAX_CU_SIZE + (DMVR_NUM_ITERATION << 1) + NTAPS_LUMA));
m_cRefSamplesDMVRL1[ch] = (Pel*)xMalloc(Pel, (MAX_CU_SIZE + (DMVR_NUM_ITERATION << 1) + NTAPS_LUMA) * (MAX_CU_SIZE + (DMVR_NUM_ITERATION << 1) + NTAPS_LUMA));
}
#endif

Karsten Suehring
committed
#if !JVET_J0090_MEMORY_BANDWITH_MEASURE
m_if.initInterpolationFilter( true );
#endif
if (m_storedMv == nullptr)
{
const int MVBUFFER_SIZE = MAX_CU_SIZE / MIN_PU_SIZE;
m_storedMv = new Mv[MVBUFFER_SIZE*MVBUFFER_SIZE];
}

Karsten Suehring
committed
}
bool checkIdenticalMotion( const PredictionUnit &pu, bool checkAffine )
{
const Slice &slice = *pu.cs->slice;
if( slice.isInterB() && !pu.cs->pps->getWPBiPred() )
{
if( pu.refIdx[0] >= 0 && pu.refIdx[1] >= 0 )
{
int RefPOCL0 = slice.getRefPic( REF_PIC_LIST_0, pu.refIdx[0] )->getPOC();
int RefPOCL1 = slice.getRefPic( REF_PIC_LIST_1, pu.refIdx[1] )->getPOC();
if( RefPOCL0 == RefPOCL1 )
{
if( !pu.cu->affine )
{
if( pu.mv[0] == pu.mv[1] )
{
return true;
}
}
else
{
CHECK( !checkAffine, "In this case, checkAffine should be on." );
if ( (pu.cu->affineType == AFFINEMODEL_4PARAM && (pu.mvAffi[0][0] == pu.mvAffi[1][0]) && (pu.mvAffi[0][1] == pu.mvAffi[1][1]))
|| (pu.cu->affineType == AFFINEMODEL_6PARAM && (pu.mvAffi[0][0] == pu.mvAffi[1][0]) && (pu.mvAffi[0][1] == pu.mvAffi[1][1]) && (pu.mvAffi[0][2] == pu.mvAffi[1][2])) )

Karsten Suehring
committed
248
249
250
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
{
return true;
}
}
}
}
}
return false;
}
// ====================================================================================================================
// Public member functions
// ====================================================================================================================
bool InterPrediction::xCheckIdenticalMotion( const PredictionUnit &pu )
{
const Slice &slice = *pu.cs->slice;
if( slice.isInterB() && !pu.cs->pps->getWPBiPred() )
{
if( pu.refIdx[0] >= 0 && pu.refIdx[1] >= 0 )
{
int RefPOCL0 = slice.getRefPic( REF_PIC_LIST_0, pu.refIdx[0] )->getPOC();
int RefPOCL1 = slice.getRefPic( REF_PIC_LIST_1, pu.refIdx[1] )->getPOC();
if( RefPOCL0 == RefPOCL1 )
{
if( !pu.cu->affine )
{
if( pu.mv[0] == pu.mv[1] )
{
return true;
}
}
else
{
if ( (pu.cu->affineType == AFFINEMODEL_4PARAM && (pu.mvAffi[0][0] == pu.mvAffi[1][0]) && (pu.mvAffi[0][1] == pu.mvAffi[1][1]))
|| (pu.cu->affineType == AFFINEMODEL_6PARAM && (pu.mvAffi[0][0] == pu.mvAffi[1][0]) && (pu.mvAffi[0][1] == pu.mvAffi[1][1]) && (pu.mvAffi[0][2] == pu.mvAffi[1][2])) )

Karsten Suehring
committed
{
return true;
}
}
}
}
}
return false;
}
void InterPrediction::xSubPuMC( PredictionUnit& pu, PelUnitBuf& predBuf, const RefPicList &eRefPicList /*= REF_PIC_LIST_X*/ )
{
// compute the location of the current PU
Position puPos = pu.lumaPos();
Size puSize = pu.lumaSize();
int numPartLine, numPartCol, puHeight, puWidth;
{
numPartLine = std::max(puSize.width >> ATMVP_SUB_BLOCK_SIZE, 1u);
numPartCol = std::max(puSize.height >> ATMVP_SUB_BLOCK_SIZE, 1u);
puHeight = numPartCol == 1 ? puSize.height : 1 << ATMVP_SUB_BLOCK_SIZE;
puWidth = numPartLine == 1 ? puSize.width : 1 << ATMVP_SUB_BLOCK_SIZE;

Karsten Suehring
committed
}
PredictionUnit subPu;
subPu.cs = pu.cs;
subPu.cu = pu.cu;
subPu.mergeType = MRG_TYPE_DEFAULT_N;
bool isAffine = pu.cu->affine;
subPu.cu->affine = false;

Karsten Suehring
committed
// join sub-pus containing the same motion
bool verMC = puSize.height > puSize.width;
int fstStart = (!verMC ? puPos.y : puPos.x);
int secStart = (!verMC ? puPos.x : puPos.y);
int fstEnd = (!verMC ? puPos.y + puSize.height : puPos.x + puSize.width);
int secEnd = (!verMC ? puPos.x + puSize.width : puPos.y + puSize.height);
int fstStep = (!verMC ? puHeight : puWidth);
int secStep = (!verMC ? puWidth : puHeight);

Karsten Suehring
committed
333
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
for (int fstDim = fstStart; fstDim < fstEnd; fstDim += fstStep)
{
for (int secDim = secStart; secDim < secEnd; secDim += secStep)
{
int x = !verMC ? secDim : fstDim;
int y = !verMC ? fstDim : secDim;
const MotionInfo &curMi = pu.getMotionInfo(Position{ x, y });
int length = secStep;
int later = secDim + secStep;
while (later < secEnd)
{
const MotionInfo &laterMi = !verMC ? pu.getMotionInfo(Position{ later, fstDim }) : pu.getMotionInfo(Position{ fstDim, later });
if (laterMi == curMi)
{
length += secStep;
}
else
{
break;
}
later += secStep;
}
int dx = !verMC ? length : puWidth;
int dy = !verMC ? puHeight : length;
subPu.UnitArea::operator=(UnitArea(pu.chromaFormat, Area(x, y, dx, dy)));
subPu = curMi;
PelUnitBuf subPredBuf = predBuf.subBuf(UnitAreaRelative(pu, subPu));
LI JINGYA
committed
#if JVET_M0823_MMVD_ENCOPT
subPu.mmvdEncOptMode = 0;
#endif
#if JVET_M0147_DMVR
subPu.mvRefine = false;
LI JINGYA
committed
#endif

Karsten Suehring
committed
motionCompensation(subPu, subPredBuf, eRefPicList);
secDim = later - secStep;
}
}
pu.cu->affine = isAffine;

Karsten Suehring
committed
}
void InterPrediction::xChromaMC(PredictionUnit &pu, PelUnitBuf& pcYuvPred)
{
// separated tree, chroma
const CompArea lumaArea = CompArea(COMPONENT_Y, pu.chromaFormat, pu.Cb().lumaPos(), recalcSize(pu.chromaFormat, CHANNEL_TYPE_CHROMA, CHANNEL_TYPE_LUMA, pu.Cb().size()));
PredictionUnit subPu;
subPu.cs = pu.cs;
subPu.cu = pu.cu;
Picture * refPic = pu.cu->slice->getPic();
for (int y = lumaArea.y; y < lumaArea.y + lumaArea.height; y += MIN_PU_SIZE)
{
for (int x = lumaArea.x; x < lumaArea.x + lumaArea.width; x += MIN_PU_SIZE)
{
const MotionInfo &curMi = pu.cs->picture->cs->getMotionInfo(Position{ x, y });
subPu.UnitArea::operator=(UnitArea(pu.chromaFormat, Area(x, y, MIN_PU_SIZE, MIN_PU_SIZE)));
PelUnitBuf subPredBuf = pcYuvPred.subBuf(UnitAreaRelative(pu, subPu));
xPredInterBlk(COMPONENT_Cb, subPu, refPic, curMi.mv[0], subPredBuf, false, pu.cu->slice->clpRng(COMPONENT_Cb)
, false
, true);
xPredInterBlk(COMPONENT_Cr, subPu, refPic, curMi.mv[0], subPredBuf, false, pu.cu->slice->clpRng(COMPONENT_Cr)
, false
, true);
void InterPrediction::xPredInterUni(const PredictionUnit& pu, const RefPicList& eRefPicList, PelUnitBuf& pcYuvPred, const bool& bi
, const bool& bioApplied
, const bool luma, const bool chroma

Karsten Suehring
committed
{
const SPS &sps = *pu.cs->sps;
int iRefIdx = pu.refIdx[eRefPicList];
Mv mv[3];
if (pu.cs->slice->getRefPic(eRefPicList, iRefIdx)->getPOC() == pu.cs->slice->getPOC())
{

Karsten Suehring
committed
if( pu.cu->affine )
{
CHECK( iRefIdx < 0, "iRefIdx incorrect." );
mv[0] = pu.mvAffi[eRefPicList][0];
mv[1] = pu.mvAffi[eRefPicList][1];
mv[2] = pu.mvAffi[eRefPicList][2];

Karsten Suehring
committed
}
else
{
mv[0] = pu.mv[eRefPicList];
}
if ( !pu.cu->affine )
clipMv(mv[0], pu.cu->lumaPos(),
pu.cu->lumaSize(),
sps);

Karsten Suehring
committed
LI JINGYA
committed
#if JVET_M0823_MMVD_ENCOPT
int numOfPass = (pu.mmvdMergeFlag && pu.mmvdEncOptMode) ? 1 : (std::min((int)pcYuvPred.bufs.size(), m_maxCompIDToPred + 1));
for (uint32_t comp = COMPONENT_Y; comp < numOfPass; comp++)
#else

Karsten Suehring
committed
for( uint32_t comp = COMPONENT_Y; comp < pcYuvPred.bufs.size() && comp <= m_maxCompIDToPred; comp++ )
LI JINGYA
committed
#endif

Karsten Suehring
committed
{
const ComponentID compID = ComponentID( comp );
if (compID == COMPONENT_Y && !luma)
continue;
if (compID != COMPONENT_Y && !chroma)
continue;

Karsten Suehring
committed
if ( pu.cu->affine )
{
CHECK( bioApplied, "BIO is not allowed with affine" );

Karsten Suehring
committed
xPredAffineBlk( compID, pu, pu.cu->slice->getRefPic( eRefPicList, iRefIdx ), mv, pcYuvPred, bi, pu.cu->slice->clpRng( compID ) );
}
else
{
xPredInterBlk( compID, pu, pu.cu->slice->getRefPic( eRefPicList, iRefIdx ), mv[0], pcYuvPred, bi, pu.cu->slice->clpRng( compID )

Karsten Suehring
committed
}
}
}
void InterPrediction::xPredInterBi(PredictionUnit& pu, PelUnitBuf &pcYuvPred)
{
const PPS &pps = *pu.cs->pps;
const Slice &slice = *pu.cs->slice;
bool bioApplied = false;
if (pu.cs->sps->getBDOFEnabledFlag())
{
if (pu.cu->affine || m_subPuMC)
{
bioApplied = false;
const bool biocheck0 = !(pps.getWPBiPred() && slice.getSliceType() == B_SLICE);
const bool biocheck1 = !(pps.getUseWP() && slice.getSliceType() == P_SLICE);
if (biocheck0
&& biocheck1
&& PU::isBiPredFromDifferentDir(pu)
&& !(pu.Y().height == 4 || (pu.Y().width == 4 && pu.Y().height == 8))
)
{
bioApplied = true;
#if JVET_M0444_SMVD
if (bioApplied && pu.cu->smvdMode)
{
bioApplied = false;
}
#endif
if (pu.cu->cs->sps->getSpsNext().getUseGBi() && bioApplied && pu.cu->GBiIdx != GBI_DEFAULT)
bioApplied = false;
LI JINGYA
committed
#if JVET_M0823_MMVD_ENCOPT
if (pu.mmvdEncOptMode == 2 && pu.mmvdMergeFlag) {
bioApplied = false;
}
#endif
#if JVET_M0147_DMVR
bool bDMVRApplied = false;
bDMVRApplied = (pu.mvRefine) && PU::checkDMVRCondition(pu);
LI JINGYA
committed
#endif

Karsten Suehring
committed
for (uint32_t refList = 0; refList < NUM_REF_PIC_LIST_01; refList++)
{
if( pu.refIdx[refList] < 0)
{
continue;
}
RefPicList eRefPicList = (refList ? REF_PIC_LIST_1 : REF_PIC_LIST_0);
CHECK( pu.refIdx[refList] >= slice.getNumRefIdx( eRefPicList ), "Invalid reference index" );
m_iRefListIdx = refList;
PelUnitBuf pcMbBuf = ( pu.chromaFormat == CHROMA_400 ?
PelUnitBuf(pu.chromaFormat, PelBuf(m_acYuvPred[refList][0], pcYuvPred.Y())) :
PelUnitBuf(pu.chromaFormat, PelBuf(m_acYuvPred[refList][0], pcYuvPred.Y()), PelBuf(m_acYuvPred[refList][1], pcYuvPred.Cb()), PelBuf(m_acYuvPred[refList][2], pcYuvPred.Cr())) );
if (pu.refIdx[0] >= 0 && pu.refIdx[1] >= 0)
{
#if JVET_M0147_DMVR
if (bDMVRApplied)
continue; // mc will happen in processDMVR
#endif

Karsten Suehring
committed
xPredInterUni ( pu, eRefPicList, pcMbBuf, true
, bioApplied
, true, true
);

Karsten Suehring
committed
}
else
{
if( ( (pps.getUseWP() && slice.getSliceType() == P_SLICE) || (pps.getWPBiPred() && slice.getSliceType() == B_SLICE) ) )
{
xPredInterUni ( pu, eRefPicList, pcMbBuf, true
, bioApplied
, true, true
);

Karsten Suehring
committed
}
else
{
xPredInterUni( pu, eRefPicList, pcMbBuf, pu.cu->triangle
, bioApplied
, true, true
);

Karsten Suehring
committed
}
}
}
#if JVET_M0147_DMVR
if (bDMVRApplied)
{
xProcessDMVR(pu, pcYuvPred, slice.clpRngs(), bioApplied);
}
#endif

Karsten Suehring
committed
CPelUnitBuf srcPred0 = ( pu.chromaFormat == CHROMA_400 ?
CPelUnitBuf(pu.chromaFormat, PelBuf(m_acYuvPred[0][0], pcYuvPred.Y())) :
CPelUnitBuf(pu.chromaFormat, PelBuf(m_acYuvPred[0][0], pcYuvPred.Y()), PelBuf(m_acYuvPred[0][1], pcYuvPred.Cb()), PelBuf(m_acYuvPred[0][2], pcYuvPred.Cr())) );
CPelUnitBuf srcPred1 = ( pu.chromaFormat == CHROMA_400 ?
CPelUnitBuf(pu.chromaFormat, PelBuf(m_acYuvPred[1][0], pcYuvPred.Y())) :
CPelUnitBuf(pu.chromaFormat, PelBuf(m_acYuvPred[1][0], pcYuvPred.Y()), PelBuf(m_acYuvPred[1][1], pcYuvPred.Cb()), PelBuf(m_acYuvPred[1][2], pcYuvPred.Cr())) );
if( pps.getWPBiPred() && slice.getSliceType() == B_SLICE )
{
xWeightedPredictionBi( pu, srcPred0, srcPred1, pcYuvPred, m_maxCompIDToPred );
}
else if( pps.getUseWP() && slice.getSliceType() == P_SLICE )
{
xWeightedPredictionUni( pu, srcPred0, REF_PIC_LIST_0, pcYuvPred, -1, m_maxCompIDToPred );
}
else
{
#if JVET_M0147_DMVR
if (bDMVRApplied == false)
{
#endif
xWeightedAverage( pu, srcPred0, srcPred1, pcYuvPred, slice.getSPS()->getBitDepths(), slice.clpRngs(), bioApplied );
#if JVET_M0147_DMVR
}
#endif

Karsten Suehring
committed
}
}
#if JVET_M0147_DMVR

Karsten Suehring
committed
void InterPrediction::xPredInterBlk ( const ComponentID& compID, const PredictionUnit& pu, const Picture* refPic, const Mv& _mv, PelUnitBuf& dstPic, const bool& bi, const ClpRng& clpRng
, const bool& bioApplied
, SizeType dmvrWidth
, SizeType dmvrHeight
, bool bilinearMC
, Pel *srcPadBuf
, int32_t srcPadStride
#else
void InterPrediction::xPredInterBlk ( const ComponentID& compID, const PredictionUnit& pu, const Picture* refPic, const Mv& _mv, PelUnitBuf& dstPic, const bool& bi, const ClpRng& clpRng
, const bool& bioApplied
, bool isIBC
)
#endif

Karsten Suehring
committed
{
JVET_J0090_SET_REF_PICTURE( refPic, compID );
const ChromaFormat chFmt = pu.chromaFormat;
const bool rndRes = !bi;
int shiftHor = MV_FRACTIONAL_BITS_INTERNAL + ::getComponentScaleX(compID, chFmt);
int shiftVer = MV_FRACTIONAL_BITS_INTERNAL + ::getComponentScaleY(compID, chFmt);

Karsten Suehring
committed
int xFrac = _mv.hor & ((1 << shiftHor) - 1);
int yFrac = _mv.ver & ((1 << shiftVer) - 1);
JVET_J0090_SET_CACHE_ENABLE( false );

Karsten Suehring
committed
PelBuf &dstBuf = dstPic.bufs[compID];
unsigned width = dstBuf.width;
unsigned height = dstBuf.height;
CPelBuf refBuf;
{
Position offset = pu.blocks[compID].pos().offset( _mv.getHor() >> shiftHor, _mv.getVer() >> shiftVer );
#if JVET_M0147_DMVR
if (dmvrWidth)
{
refBuf = refPic->getRecoBuf(CompArea(compID, chFmt, offset, Size(dmvrWidth, dmvrHeight)));
}
else
#endif

Karsten Suehring
committed
refBuf = refPic->getRecoBuf( CompArea( compID, chFmt, offset, pu.blocks[compID].size() ) );
}
#if JVET_M0147_DMVR
if (NULL != srcPadBuf)
{
refBuf.buf = srcPadBuf;
refBuf.stride = srcPadStride;
}
if (dmvrWidth)
{
width = dmvrWidth;
height = dmvrHeight;
}
#endif
// backup data
int backupWidth = width;
int backupHeight = height;
Pel *backupDstBufPtr = dstBuf.buf;
int backupDstBufStride = dstBuf.stride;
if (bioApplied && compID == COMPONENT_Y)
width = width + 2 * BIO_EXTEND_SIZE + 2;
height = height + 2 * BIO_EXTEND_SIZE + 2;
// change MC output
dstBuf.stride = width;
dstBuf.buf = m_filteredBlockTmp[2 + m_iRefListIdx][compID] + 2 * dstBuf.stride + 2;
}

Karsten Suehring
committed
if( yFrac == 0 )
{
#if JVET_M0147_DMVR
m_if.filterHor(compID, (Pel*)refBuf.buf, refBuf.stride, dstBuf.buf, dstBuf.stride, backupWidth, backupHeight, xFrac, rndRes, chFmt, clpRng, bilinearMC, bilinearMC);
#else
m_if.filterHor(compID, (Pel*)refBuf.buf, refBuf.stride, dstBuf.buf, dstBuf.stride, backupWidth, backupHeight, xFrac, rndRes, chFmt, clpRng);

Karsten Suehring
committed
}
else if( xFrac == 0 )
{
#if JVET_M0147_DMVR
m_if.filterVer(compID, (Pel*)refBuf.buf, refBuf.stride, dstBuf.buf, dstBuf.stride, backupWidth, backupHeight, yFrac, true, rndRes, chFmt, clpRng, bilinearMC, bilinearMC);
#else
m_if.filterVer(compID, (Pel*)refBuf.buf, refBuf.stride, dstBuf.buf, dstBuf.stride, backupWidth, backupHeight, yFrac, true, rndRes, chFmt, clpRng);

Karsten Suehring
committed
}
else
{
#if JVET_M0147_DMVR
PelBuf tmpBuf = dmvrWidth ? PelBuf(m_filteredBlockTmp[0][compID], Size(dmvrWidth, dmvrHeight)) : PelBuf(m_filteredBlockTmp[0][compID], pu.blocks[compID]);
if (dmvrWidth == 0)
tmpBuf.stride = dstBuf.stride;
#else
PelBuf tmpBuf = PelBuf(m_filteredBlockTmp[0][compID], pu.blocks[compID]);
tmpBuf.stride = dstBuf.stride;

Karsten Suehring
committed
int vFilterSize = isLuma(compID) ? NTAPS_LUMA : NTAPS_CHROMA;
#if JVET_M0147_DMVR
if (bilinearMC)
{
vFilterSize = NTAPS_BILINEAR;
}
#endif
#if JVET_M0147_DMVR
m_if.filterHor(compID, (Pel*)refBuf.buf - ((vFilterSize >> 1) - 1) * refBuf.stride, refBuf.stride, tmpBuf.buf, tmpBuf.stride, backupWidth, backupHeight + vFilterSize - 1, xFrac, false, chFmt, clpRng, bilinearMC, bilinearMC);
#else
m_if.filterHor(compID, (Pel*)refBuf.buf - ((vFilterSize >> 1) - 1) * refBuf.stride, refBuf.stride, tmpBuf.buf, tmpBuf.stride, backupWidth, backupHeight + vFilterSize - 1, xFrac, false, chFmt, clpRng);

Karsten Suehring
committed
JVET_J0090_SET_CACHE_ENABLE( false );
#if JVET_M0147_DMVR
m_if.filterVer(compID, (Pel*)tmpBuf.buf + ((vFilterSize >> 1) - 1) * tmpBuf.stride, tmpBuf.stride, dstBuf.buf, dstBuf.stride, backupWidth, backupHeight, yFrac, false, rndRes, chFmt, clpRng, bilinearMC, bilinearMC);
#else
m_if.filterVer(compID, (Pel*)tmpBuf.buf + ((vFilterSize >> 1) - 1) * tmpBuf.stride, tmpBuf.stride, dstBuf.buf, dstBuf.stride, backupWidth, backupHeight, yFrac, false, rndRes, chFmt, clpRng);

Karsten Suehring
committed
}
JVET_J0090_SET_CACHE_ENABLE( true );
if (bioApplied && compID == COMPONENT_Y)
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
#if JVET_M0487_INT_EXTEND
const int shift = std::max<int>(2, (IF_INTERNAL_PREC - clpRng.bd));
const Pel* refPel = refBuf.buf - refBuf.stride - 1;
Pel* dstPel = m_filteredBlockTmp[2 + m_iRefListIdx][compID] + dstBuf.stride + 1;
for (int w = 0; w < (width - 2 * BIO_EXTEND_SIZE); w++)
{
Pel val = leftShift_round(refPel[w], shift);
dstPel[w] = val - (Pel)IF_INTERNAL_OFFS;
}
refPel = refBuf.buf - 1;
dstPel = m_filteredBlockTmp[2 + m_iRefListIdx][compID] + 2 * dstBuf.stride + 1;
for (int h = 0; h < (height - 2 * BIO_EXTEND_SIZE - 2); h++)
{
Pel val = leftShift_round(refPel[0], shift);
dstPel[0] = val - (Pel)IF_INTERNAL_OFFS;
val = leftShift_round(refPel[width - 3], shift);
dstPel[width - 3] = val - (Pel)IF_INTERNAL_OFFS;
refPel += refBuf.stride;
dstPel += dstBuf.stride;
}
refPel = refBuf.buf + (height - 2 * BIO_EXTEND_SIZE - 2)*refBuf.stride - 1;
dstPel = m_filteredBlockTmp[2 + m_iRefListIdx][compID] + (height - 2 * BIO_EXTEND_SIZE)*dstBuf.stride + 1;
for (int w = 0; w < (width - 2 * BIO_EXTEND_SIZE); w++)
{
Pel val = leftShift_round(refPel[w], shift);
dstPel[w] = val - (Pel)IF_INTERNAL_OFFS;
}
#else
refBuf.buf = refBuf.buf - refBuf.stride - 1;
#if JVET_M0147_DMVR
if (srcPadBuf)
{
refBuf.buf = srcPadBuf - srcPadStride - 1;
refBuf.stride = srcPadStride;
}
#endif
dstBuf.buf = m_filteredBlockTmp[2 + m_iRefListIdx][compID] + dstBuf.stride + 1;
bioSampleExtendBilinearFilter(refBuf.buf, refBuf.stride, dstBuf.buf, dstBuf.stride, width - 2, height - 2, 1, xFrac, yFrac, rndRes, chFmt, clpRng);
// restore data
width = backupWidth;
height = backupHeight;
dstBuf.buf = backupDstBufPtr;
dstBuf.stride = backupDstBufStride;
}

Karsten Suehring
committed
}
void InterPrediction::xPredAffineBlk( const ComponentID& compID, const PredictionUnit& pu, const Picture* refPic, const Mv* _mv, PelUnitBuf& dstPic, const bool& bi, const ClpRng& clpRng )
{
if ( (pu.cu->affineType == AFFINEMODEL_6PARAM && _mv[0] == _mv[1] && _mv[0] == _mv[2])
|| (pu.cu->affineType == AFFINEMODEL_4PARAM && _mv[0] == _mv[1])
)
{
Mv mvTemp = _mv[0];
clipMv( mvTemp, pu.cu->lumaPos(),
pu.cu->lumaSize(),
*pu.cs->sps );
xPredInterBlk( compID, pu, refPic, mvTemp, dstPic, bi, clpRng
, false
, false
);

Karsten Suehring
committed
return;
}
JVET_J0090_SET_REF_PICTURE( refPic, compID );
const ChromaFormat chFmt = pu.chromaFormat;
int iScaleX = ::getComponentScaleX( compID, chFmt );
int iScaleY = ::getComponentScaleY( compID, chFmt );
Mv mvLT =_mv[0];
Mv mvRT =_mv[1];
Mv mvLB =_mv[2];
// get affine sub-block width and height
const int width = pu.Y().width;
const int height = pu.Y().height;
int blockWidth = AFFINE_MIN_BLOCK_SIZE;
int blockHeight = AFFINE_MIN_BLOCK_SIZE;
blockWidth >>= iScaleX;
blockHeight >>= iScaleY;
blockWidth = std::max(blockWidth, AFFINE_MIN_BLOCK_SIZE);
blockHeight = std::max(blockHeight, AFFINE_MIN_BLOCK_SIZE);
CHECK(blockWidth > (width >> iScaleX ), "Sub Block width > Block width");
CHECK(blockHeight > (height >> iScaleX), "Sub Block height > Block height");
const int MVBUFFER_SIZE = MAX_CU_SIZE / MIN_PU_SIZE;

Karsten Suehring
committed
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
const int cxWidth = width >> iScaleX;
const int cxHeight = height >> iScaleY;
const int iHalfBW = blockWidth >> 1;
const int iHalfBH = blockHeight >> 1;
const int iBit = MAX_CU_DEPTH;
int iDMvHorX, iDMvHorY, iDMvVerX, iDMvVerY;
iDMvHorX = (mvRT - mvLT).getHor() << (iBit - g_aucLog2[cxWidth]);
iDMvHorY = (mvRT - mvLT).getVer() << (iBit - g_aucLog2[cxWidth]);
if ( pu.cu->affineType == AFFINEMODEL_6PARAM )
{
iDMvVerX = (mvLB - mvLT).getHor() << (iBit - g_aucLog2[cxHeight]);
iDMvVerY = (mvLB - mvLT).getVer() << (iBit - g_aucLog2[cxHeight]);
}
else
{
iDMvVerX = -iDMvHorY;
iDMvVerY = iDMvHorX;
}
int iMvScaleHor = mvLT.getHor() << iBit;
int iMvScaleVer = mvLT.getVer() << iBit;
const SPS &sps = *pu.cs->sps;
const int iMvShift = 4;
const int iOffset = 8;
const int iHorMax = ( sps.getPicWidthInLumaSamples() + iOffset - pu.Y().x - 1 ) << iMvShift;
const int iHorMin = ( -(int)pu.cs->pcv->maxCUWidth - iOffset - (int)pu.Y().x + 1 ) << iMvShift;
const int iVerMax = ( sps.getPicHeightInLumaSamples() + iOffset - pu.Y().y - 1 ) << iMvShift;
const int iVerMin = ( -(int)pu.cs->pcv->maxCUHeight - iOffset - (int)pu.Y().y + 1 ) << iMvShift;
PelBuf tmpBuf = PelBuf(m_filteredBlockTmp[0][compID], pu.blocks[compID]);
const int vFilterSize = isLuma(compID) ? NTAPS_LUMA : NTAPS_CHROMA;
const int shift = iBit - 4 + MV_FRACTIONAL_BITS_INTERNAL;

Karsten Suehring
committed
// get prediction block by block
for ( int h = 0; h < cxHeight; h += blockHeight )
{
for ( int w = 0; w < cxWidth; w += blockWidth )
{
int iMvScaleTmpHor, iMvScaleTmpVer;
if(compID == COMPONENT_Y)
{
iMvScaleTmpHor = iMvScaleHor + iDMvHorX * (iHalfBW + w) + iDMvVerX * (iHalfBH + h);
iMvScaleTmpVer = iMvScaleVer + iDMvHorY * (iHalfBW + w) + iDMvVerY * (iHalfBH + h);
roundAffineMv(iMvScaleTmpHor, iMvScaleTmpVer, shift);
#if JVET_M0145_AFFINE_MV_CLIP
Mv tmpMv(iMvScaleTmpHor, iMvScaleTmpVer);
tmpMv.clipToStorageBitDepth();
iMvScaleTmpHor = tmpMv.getHor();
iMvScaleTmpVer = tmpMv.getVer();
#endif
if (sps.getWrapAroundEnabledFlag())
{
m_storedMv[h / AFFINE_MIN_BLOCK_SIZE * MVBUFFER_SIZE + w / AFFINE_MIN_BLOCK_SIZE].set(iMvScaleTmpHor, iMvScaleTmpVer);
Mv tmpMv(iMvScaleTmpHor, iMvScaleTmpVer);
clipMv(tmpMv, Position(pu.Y().x + w, pu.Y().y + h), Size(blockWidth, blockHeight), sps);
iMvScaleTmpHor = tmpMv.getHor();
iMvScaleTmpVer = tmpMv.getVer();
}
else
{
#if JVET_M0265_MV_ROUNDING_CLEANUP
m_storedMv[h / AFFINE_MIN_BLOCK_SIZE * MVBUFFER_SIZE + w / AFFINE_MIN_BLOCK_SIZE].set(iMvScaleTmpHor, iMvScaleTmpVer);
#endif
iMvScaleTmpHor = std::min<int>(iHorMax, std::max<int>(iHorMin, iMvScaleTmpHor));
iMvScaleTmpVer = std::min<int>(iVerMax, std::max<int>(iVerMin, iMvScaleTmpVer));
m_storedMv[h / AFFINE_MIN_BLOCK_SIZE * MVBUFFER_SIZE + w / AFFINE_MIN_BLOCK_SIZE].set(iMvScaleTmpHor, iMvScaleTmpVer);
Anish Tamse
committed
#if JVET_M0192_AFF_CHROMA_SIMPL
Mv curMv = m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE) * MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE)] +
m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE + 1)* MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE + 1)];
roundAffineMv(curMv.hor, curMv.ver, 1);
#else
Mv curMv = m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE) * MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE)] +
m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE + 1)* MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE)] +
m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE)* MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE + 1)] +
m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE + 1)* MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE + 1)];
roundAffineMv(curMv.hor, curMv.ver, 2);
Anish Tamse
committed
#endif
#else
#if JVET_M0192_AFF_CHROMA_SIMPL
Mv curMv = m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE) * MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE)] +
m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE + 1)* MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE + 1)];
roundAffineMv(curMv.hor, curMv.ver, 1);
Mv curMv = (m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE) * MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE)] +
m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE + 1)* MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE)] +
m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE)* MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE + 1)] +
m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE + 1)* MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE + 1)] +
Mv(2, 2));
curMv.set(curMv.getHor() >> 2, curMv.getVer() >> 2);
Anish Tamse
committed
#endif
if (sps.getWrapAroundEnabledFlag())
{
clipMv(curMv, Position(pu.Y().x + (w << iScaleX), pu.Y().y + (h << iScaleY)), Size(blockWidth << iScaleX, blockHeight << iScaleY), sps);
}
#if JVET_M0265_MV_ROUNDING_CLEANUP
else
{
curMv.hor = std::min<int>(iHorMax, std::max<int>(iHorMin, curMv.hor));
curMv.ver = std::min<int>(iVerMax, std::max<int>(iVerMin, curMv.ver));
}
#endif
iMvScaleTmpHor = curMv.hor;
iMvScaleTmpVer = curMv.ver;
}

Karsten Suehring
committed
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
// get the MV in high precision
int xFrac, yFrac, xInt, yInt;
if (!iScaleX)
{
xInt = iMvScaleTmpHor >> 4;
xFrac = iMvScaleTmpHor & 15;
}
else
{
xInt = iMvScaleTmpHor >> 5;
xFrac = iMvScaleTmpHor & 31;
}
if (!iScaleY)
{
yInt = iMvScaleTmpVer >> 4;
yFrac = iMvScaleTmpVer & 15;
}
else
{
yInt = iMvScaleTmpVer >> 5;
yFrac = iMvScaleTmpVer & 31;
}
const CPelBuf refBuf = refPic->getRecoBuf( CompArea( compID, chFmt, pu.blocks[compID].offset(xInt + w, yInt + h), pu.blocks[compID] ) );
PelBuf &dstBuf = dstPic.bufs[compID];
if ( yFrac == 0 )
{
m_if.filterHor( compID, (Pel*) refBuf.buf, refBuf.stride, dstBuf.buf + w + h * dstBuf.stride, dstBuf.stride, blockWidth, blockHeight, xFrac, !bi, chFmt, clpRng );
}
else if ( xFrac == 0 )
{
m_if.filterVer( compID, (Pel*) refBuf.buf, refBuf.stride, dstBuf.buf + w + h * dstBuf.stride, dstBuf.stride, blockWidth, blockHeight, yFrac, true, !bi, chFmt, clpRng );
}
else
{
m_if.filterHor( compID, (Pel*) refBuf.buf - ((vFilterSize>>1) -1)*refBuf.stride, refBuf.stride, tmpBuf.buf, tmpBuf.stride, blockWidth, blockHeight+vFilterSize-1, xFrac, false, chFmt, clpRng);
JVET_J0090_SET_CACHE_ENABLE( false );
m_if.filterVer( compID, tmpBuf.buf + ((vFilterSize>>1) -1)*tmpBuf.stride, tmpBuf.stride, dstBuf.buf + w + h * dstBuf.stride, dstBuf.stride, blockWidth, blockHeight, yFrac, false, !bi, chFmt, clpRng);
JVET_J0090_SET_CACHE_ENABLE( true );
}
}
}
}
int getMSB( unsigned x )
{
int msb = 0, bits = ( sizeof(int) << 3 ), y = 1;
while( x > 1u )
{
bits >>= 1;
y = x >> bits;
if( y )
{
x = y;
msb += bits;
}
}
msb += y;
return msb;
}
void InterPrediction::applyBiOptFlow(const PredictionUnit &pu, const CPelUnitBuf &yuvSrc0, const CPelUnitBuf &yuvSrc1, const int &refIdx0, const int &refIdx1, PelUnitBuf &yuvDst, const BitDepths &clipBitDepths)
const int height = yuvDst.Y().height;
const int width = yuvDst.Y().width;
int heightG = height + 2 * BIO_EXTEND_SIZE;
int widthG = width + 2 * BIO_EXTEND_SIZE;
int offsetPos = widthG*BIO_EXTEND_SIZE + BIO_EXTEND_SIZE;