Newer
Older

Karsten Suehring
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* 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-2018, ITU/ISO/IEC
* 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 )
{
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; i++ )
{
for( uint32_t j = 0; j < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS; j++ )
{
m_filteredBlock[i][j][c] = nullptr;
}
m_filteredBlockTmp[i][c] = nullptr;
}
}
}
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; i++ )
{
for( uint32_t j = 0; j < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS; j++ )
{
xFree( m_filteredBlock[i][j][c] );
m_filteredBlock[i][j][c] = nullptr;
}
xFree( m_filteredBlockTmp[i][c] );
m_filteredBlockTmp[i][c] = nullptr;
}
}
}
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 + 16;
int extHeight = MAX_CU_SIZE + 1;
for( uint32_t i = 0; i < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS; i++ )
{
m_filteredBlockTmp[i][c] = ( Pel* ) xMalloc( Pel, ( extWidth + 4 ) * ( extHeight + 7 + 4 ) );
for( uint32_t j = 0; j < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS; j++ )
{
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 );
}
}

Karsten Suehring
committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
m_iRefListIdx = -1;
}
#if !JVET_J0090_MEMORY_BANDWITH_MEASURE
m_if.initInterpolationFilter( true );
#endif
}
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." );
const CMotionBuf &mb = pu.getMotionBuf();
if ( (pu.cu->affineType == AFFINEMODEL_4PARAM && (mb.at( 0, 0 ).mv[0] == mb.at( 0, 0 ).mv[1]) && (mb.at( mb.width - 1, 0 ).mv[0] == mb.at( mb.width - 1, 0 ).mv[1]))
|| (pu.cu->affineType == AFFINEMODEL_6PARAM && (mb.at( 0, 0 ).mv[0] == mb.at( 0, 0 ).mv[1]) && (mb.at( mb.width - 1, 0 ).mv[0] == mb.at( mb.width - 1, 0 ).mv[1]) && (mb.at( 0, mb.height - 1 ).mv[0] == mb.at( 0, mb.height - 1 ).mv[1])) )
{
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
{
const CMotionBuf &mb = pu.getMotionBuf();
if ( (pu.cu->affineType == AFFINEMODEL_4PARAM && (mb.at( 0, 0 ).mv[0] == mb.at( 0, 0 ).mv[1]) && (mb.at( mb.width - 1, 0 ).mv[0] == mb.at( mb.width - 1, 0 ).mv[1]))
|| (pu.cu->affineType == AFFINEMODEL_6PARAM && (mb.at( 0, 0 ).mv[0] == mb.at( 0, 0 ).mv[1]) && (mb.at( mb.width - 1, 0 ).mv[0] == mb.at( mb.width - 1, 0 ).mv[1]) && (mb.at( 0, mb.height - 1 ).mv[0] == mb.at( 0, mb.height - 1 ).mv[1])) )
{
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;
{
const Slice& slice = *pu.cs->slice;
numPartLine = std::max(puSize.width >> slice.getSubPuMvpSubblkLog2Size(), 1u);
numPartCol = std::max(puSize.height >> slice.getSubPuMvpSubblkLog2Size(), 1u);
puHeight = numPartCol == 1 ? puSize.height : 1 << slice.getSubPuMvpSubblkLog2Size();
puWidth = numPartLine == 1 ? puSize.width : 1 << slice.getSubPuMvpSubblkLog2Size();
}
PredictionUnit subPu;
subPu.cs = pu.cs;
subPu.cu = pu.cu;
subPu.mergeType = MRG_TYPE_DEFAULT_N;
// 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);
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));
motionCompensation(subPu, subPredBuf, eRefPicList);
secDim = later - secStep;
}
}
}
void InterPrediction::xPredInterUni(const PredictionUnit& pu, const RefPicList& eRefPicList, PelUnitBuf& pcYuvPred, const bool& bi
)

Karsten Suehring
committed
{
const SPS &sps = *pu.cs->sps;
int iRefIdx = pu.refIdx[eRefPicList];
Mv mv[3];
if( pu.cu->affine )
{
CHECK( iRefIdx < 0, "iRefIdx incorrect." );
const CMotionBuf &mb = pu.getMotionBuf();
mv[0] = mb.at( 0, 0 ).mv[eRefPicList];
mv[1] = mb.at( mb.width - 1, 0 ).mv[eRefPicList];
mv[2] = mb.at( 0, mb.height - 1 ).mv[eRefPicList];
}
else
{
mv[0] = pu.mv[eRefPicList];
}
if ( !pu.cu->affine )
clipMv(mv[0], pu.cu->lumaPos(), sps);
#if JVET_L0265_AFF_MINIMUM4X4
const int MVBUFFER_SIZE = MAX_CU_SIZE / MIN_PU_SIZE;
Mv storedMv[MVBUFFER_SIZE*MVBUFFER_SIZE];
#endif

Karsten Suehring
committed
for( uint32_t comp = COMPONENT_Y; comp < pcYuvPred.bufs.size() && comp <= m_maxCompIDToPred; comp++ )
{
const ComponentID compID = ComponentID( comp );
if ( pu.cu->affine )
{
xPredAffineBlk( compID, pu, pu.cu->slice->getRefPic( eRefPicList, iRefIdx ), mv, pcYuvPred, bi, pu.cu->slice->clpRng( compID )
#if JVET_L0265_AFF_MINIMUM4X4
,storedMv
#endif
);

Karsten Suehring
committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
}
else
{
xPredInterBlk( compID, pu, pu.cu->slice->getRefPic( eRefPicList, iRefIdx ), mv[0], pcYuvPred, bi, pu.cu->slice->clpRng( compID )
);
}
}
}
void InterPrediction::xPredInterBi(PredictionUnit& pu, PelUnitBuf &pcYuvPred)
{
const PPS &pps = *pu.cs->pps;
const Slice &slice = *pu.cs->slice;
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)
{
xPredInterUni ( pu, eRefPicList, pcMbBuf, true
);
}
else
{
if( ( (pps.getUseWP() && slice.getSliceType() == P_SLICE) || (pps.getWPBiPred() && slice.getSliceType() == B_SLICE) ) )
{
xPredInterUni ( pu, eRefPicList, pcMbBuf, true );
}
else
{
xPredInterUni ( pu, eRefPicList, pcMbBuf, false );
}
}
}
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
{
xWeightedAverage( pu, srcPred0, srcPred1, pcYuvPred, slice.getSPS()->getBitDepths(), slice.clpRngs() );
}
}
void InterPrediction::xPredInterBlk ( const ComponentID& compID, const PredictionUnit& pu, const Picture* refPic, const Mv& _mv, PelUnitBuf& dstPic, const bool& bi, const ClpRng& clpRng
)
{
JVET_J0090_SET_REF_PICTURE( refPic, compID );
const ChromaFormat chFmt = pu.chromaFormat;
const bool rndRes = !bi;
int iAddPrecShift = 0;

Karsten Suehring
committed
if (_mv.highPrec)
{
CHECKD(!pu.cs->sps->getSpsNext().getUseHighPrecMv(), "Found a high-precision motion vector, but the high-precision MV extension is disabled!");

Karsten Suehring
committed
iAddPrecShift = VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE;

Karsten Suehring
committed
}

Karsten Suehring
committed
int shiftHor = 2 + iAddPrecShift + ::getComponentScaleX(compID, chFmt);
int shiftVer = 2 + iAddPrecShift + ::getComponentScaleY(compID, chFmt);
int xFrac = _mv.hor & ((1 << shiftHor) - 1);
int yFrac = _mv.ver & ((1 << shiftVer) - 1);
xFrac <<= VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE - iAddPrecShift;
yFrac <<= VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE - iAddPrecShift;

Karsten Suehring
committed
CHECKD(!pu.cs->sps->getSpsNext().getUseHighPrecMv() && ((xFrac & 3) != 0), "Invalid fraction");
CHECKD(!pu.cs->sps->getSpsNext().getUseHighPrecMv() && ((yFrac & 3) != 0), "Invalid fraction");

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 );
refBuf = refPic->getRecoBuf( CompArea( compID, chFmt, offset, pu.blocks[compID].size() ) );
}
if( yFrac == 0 )
{
m_if.filterHor(compID, (Pel*) refBuf.buf, refBuf.stride, dstBuf.buf, dstBuf.stride, width, height, xFrac, rndRes, chFmt, clpRng);
}
else if( xFrac == 0 )
{
m_if.filterVer(compID, (Pel*) refBuf.buf, refBuf.stride, dstBuf.buf, dstBuf.stride, width, height, yFrac, true, rndRes, chFmt, clpRng);
}
else
{
PelBuf tmpBuf = PelBuf(m_filteredBlockTmp[0][compID], pu.blocks[compID]);

Karsten Suehring
committed
int vFilterSize = isLuma(compID) ? NTAPS_LUMA : NTAPS_CHROMA;
m_if.filterHor(compID, (Pel*) refBuf.buf - ((vFilterSize >> 1) - 1) * refBuf.stride, refBuf.stride, tmpBuf.buf, tmpBuf.stride, width, height + vFilterSize - 1, xFrac, false, chFmt, clpRng);
JVET_J0090_SET_CACHE_ENABLE( false );
m_if.filterVer(compID, (Pel*) tmpBuf.buf + ((vFilterSize >> 1) - 1) * tmpBuf.stride, tmpBuf.stride, dstBuf.buf, dstBuf.stride, width, height, yFrac, false, rndRes, chFmt, clpRng);
JVET_J0090_SET_CACHE_ENABLE( true );
}
}
void InterPrediction::xPredAffineBlk( const ComponentID& compID, const PredictionUnit& pu, const Picture* refPic, const Mv* _mv, PelUnitBuf& dstPic, const bool& bi, const ClpRng& clpRng
#if JVET_L0265_AFF_MINIMUM4X4
, Mv* storedMv
#endif
)

Karsten Suehring
committed
{
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.cs->sps );
xPredInterBlk( compID, pu, refPic, mvTemp, dstPic, bi, clpRng );
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];

Karsten Suehring
committed
mvLT.setHighPrec();
mvRT.setHighPrec();
mvLB.setHighPrec();

Karsten Suehring
committed
// 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;
#if JVET_L0265_AFF_MINIMUM4X4
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
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 + VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE + 2;
// get prediction block by block
for ( int h = 0; h < cxHeight; h += blockHeight )
{
for ( int w = 0; w < cxWidth; w += blockWidth )
{
{
iMvScaleTmpHor = iMvScaleHor + iDMvHorX * (iHalfBW + w) + iDMvVerX * (iHalfBH + h);
iMvScaleTmpVer = iMvScaleVer + iDMvHorY * (iHalfBW + w) + iDMvVerY * (iHalfBH + h);
roundAffineMv(iMvScaleTmpHor, iMvScaleTmpVer, shift);
// clip and scale
iMvScaleTmpHor = std::min<int>(iHorMax, std::max<int>(iHorMin, iMvScaleTmpHor));
iMvScaleTmpVer = std::min<int>(iVerMax, std::max<int>(iVerMin, iMvScaleTmpVer));
if (storedMv != nullptr)
{
storedMv[h / AFFINE_MIN_BLOCK_SIZE * MVBUFFER_SIZE + w / AFFINE_MIN_BLOCK_SIZE].set(iMvScaleTmpHor, iMvScaleTmpVer);
}
else if(compID != COMPONENT_Y && storedMv != nullptr)
Mv curMv = (storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE) * MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE)] +
storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE + 1)* MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE)] +
storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE)* MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE + 1)] +
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);
iMvScaleTmpHor = curMv.hor;
iMvScaleTmpVer = curMv.ver;
}
else
{
CHECK(1, "Impossible condition in affine motion compensation");
}

Karsten Suehring
committed
int iMvScaleTmpHor = iMvScaleHor + iDMvHorX * (iHalfBW + w) + iDMvVerX * (iHalfBH + h);
int iMvScaleTmpVer = iMvScaleVer + iDMvHorY * (iHalfBW + w) + iDMvVerY * (iHalfBH + h);
roundAffineMv( iMvScaleTmpHor, iMvScaleTmpVer, shift );
// clip and scale
iMvScaleTmpHor = std::min<int>( iHorMax, std::max<int>( iHorMin, iMvScaleTmpHor ) );
iMvScaleTmpVer = std::min<int>( iVerMax, std::max<int>( iVerMin, iMvScaleTmpVer ) );

Karsten Suehring
committed
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
// 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::xWeightedAverage( const PredictionUnit& pu, const CPelUnitBuf& pcYuvSrc0, const CPelUnitBuf& pcYuvSrc1, PelUnitBuf& pcYuvDst, const BitDepths& clipBitDepths, const ClpRngs& clpRngs )
{
const int iRefIdx0 = pu.refIdx[0];
const int iRefIdx1 = pu.refIdx[1];
if( iRefIdx0 >= 0 && iRefIdx1 >= 0 )
{
#if JVET_L0646_GBI
if( pu.cu->GBiIdx != GBI_DEFAULT )
{
pcYuvDst.addWeightedAvg(pcYuvSrc0, pcYuvSrc1, clpRngs, pu.cu->GBiIdx);
return;
}
#endif

Karsten Suehring
committed
pcYuvDst.addAvg( pcYuvSrc0, pcYuvSrc1, clpRngs );
}
else if( iRefIdx0 >= 0 && iRefIdx1 < 0 )
{
pcYuvDst.copyClip( pcYuvSrc0, clpRngs );
}
else if( iRefIdx0 < 0 && iRefIdx1 >= 0 )
{
pcYuvDst.copyClip( pcYuvSrc1, clpRngs );
}
}
void InterPrediction::motionCompensation( PredictionUnit &pu, PelUnitBuf &predBuf, const RefPicList &eRefPicList
)

Karsten Suehring
committed
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
{
CodingStructure &cs = *pu.cs;
const PPS &pps = *cs.pps;
const SliceType sliceType = cs.slice->getSliceType();
if( eRefPicList != REF_PIC_LIST_X )
{
if( ( ( sliceType == P_SLICE && pps.getUseWP() ) || ( sliceType == B_SLICE && pps.getWPBiPred() ) ) )
{
xPredInterUni ( pu, eRefPicList, predBuf, true );
xWeightedPredictionUni( pu, predBuf, eRefPicList, predBuf, -1, m_maxCompIDToPred );
}
else
{
xPredInterUni( pu, eRefPicList, predBuf, false );
}
}
else
{
if( pu.mergeType != MRG_TYPE_DEFAULT_N )
{
xSubPuMC( pu, predBuf, eRefPicList );
}
else if( xCheckIdenticalMotion( pu ) )
{
xPredInterUni( pu, REF_PIC_LIST_0, predBuf, false );
}
else
{
xPredInterBi( pu, predBuf );
}
}
return;
}
void InterPrediction::motionCompensation( CodingUnit &cu, const RefPicList &eRefPicList
)

Karsten Suehring
committed
{
for( auto &pu : CU::traversePUs( cu ) )
{
PelUnitBuf predBuf = cu.cs->getPredBuf( pu );
motionCompensation( pu, predBuf, eRefPicList );
}
}
void InterPrediction::motionCompensation( PredictionUnit &pu, const RefPicList &eRefPicList /*= REF_PIC_LIST_X*/
)

Karsten Suehring
committed
{
PelUnitBuf predBuf = pu.cs->getPredBuf( pu );
motionCompensation( pu, predBuf, eRefPicList
);

Karsten Suehring
committed
}
#if JVET_J0090_MEMORY_BANDWITH_MEASURE
void InterPrediction::cacheAssign( CacheModel *cache )
{
m_cacheModel = cache;
m_if.cacheAssign( cache );
m_if.initInterpolationFilter( !cache->isCacheEnable() );
}
#endif
//! \}