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
* 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 InterSearch.h
\brief inter search class (header)
*/
#ifndef __INTERSEARCH__
#define __INTERSEARCH__
// Include files
#include "CABACWriter.h"
#include "EncCfg.h"
#include "CommonLib/MotionInfo.h"
#include "CommonLib/InterPrediction.h"
#include "CommonLib/CrossCompPrediction.h"
#include "CommonLib/TrQuant.h"
#include "CommonLib/Unit.h"
#include "CommonLib/UnitPartitioner.h"
#include "CommonLib/RdCost.h"
#include "CommonLib/AffineGradientSearch.h"

Karsten Suehring
committed
//! \ingroup EncoderLib
//! \{
// ====================================================================================================================
// Class definition
// ====================================================================================================================
static const uint32_t MAX_NUM_REF_LIST_ADAPT_SR = 2;
static const uint32_t MAX_IDX_ADAPT_SR = 33;
static const uint32_t NUM_MV_PREDICTORS = 3;
struct BlkRecord
{
std::unordered_map<Mv, Distortion> bvRecord;
};

Karsten Suehring
committed
class EncModeCtrl;
struct AffineMVInfo
{
Mv affMVs[2][33][3];
int x, y, w, h;
};
#if JVET_M0246_AFFINE_AMVR
typedef struct
{
Mv acMvAffine4Para[2][2];
Mv acMvAffine6Para[2][3];
int16_t affine4ParaRefIdx[2];
int16_t affine6ParaRefIdx[2];
Distortion hevcCost[3];
Distortion affineCost[3];
bool affine4ParaAvail;
bool affine6ParaAvail;
} EncAffineMotion;
#endif

Karsten Suehring
committed
/// encoder search class
class InterSearch : public InterPrediction, CrossComponentPrediction, AffineGradientSearch
{
private:
EncModeCtrl *m_modeCtrl;
PelStorage m_tmpPredStorage [NUM_REF_PIC_LIST_01];
PelStorage m_tmpStorageLCU;
PelStorage m_tmpAffiStorage;
Pel* m_tmpAffiError;
int* m_tmpAffiDeri[2];
CodingStructure ****m_pSplitCS;
CodingStructure ****m_pFullCS;
CodingStructure **m_pSaveCS;
ClpRng m_lumaClpRng;
uint32_t m_estWeightIdxBits[GBI_NUM];
GBiMotionParam m_uniMotions;
std::unordered_map< Position, std::unordered_map< Size, BlkRecord> > m_ctuRecord;
AffineMVInfo *m_affMVList;
int m_affMVListIdx;
int m_affMVListSize;
int m_affMVListMaxSize;
Distortion m_hevcCost;
#if JVET_M0246_AFFINE_AMVR
EncAffineMotion m_affineMotion;
#endif

Karsten Suehring
committed
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
149
protected:
// interface to option
EncCfg* m_pcEncCfg;
// interface to classes
TrQuant* m_pcTrQuant;
// ME parameters
int m_iSearchRange;
int m_bipredSearchRange; // Search range for bi-prediction
MESearchMethod m_motionEstimationSearchMethod;
int m_aaiAdaptSR [MAX_NUM_REF_LIST_ADAPT_SR][MAX_IDX_ADAPT_SR];
// RD computation
CABACWriter* m_CABACEstimator;
CtxCache* m_CtxCache;
DistParam m_cDistParam;
// Misc.
Pel *m_pTempPel;
// AMVP cost computation
uint32_t m_auiMVPIdxCost [AMVP_MAX_NUM_CANDS+1][AMVP_MAX_NUM_CANDS+1]; //th array bounds
Mv m_integerMv2Nx2N [NUM_REF_PIC_LIST_01][MAX_NUM_REF];
bool m_isInitialized;

Karsten Suehring
committed
public:
InterSearch();
virtual ~InterSearch();
void init ( EncCfg* pcEncCfg,
TrQuant* pcTrQuant,
int iSearchRange,
int bipredSearchRange,
MESearchMethod motionEstimationSearchMethod,
const uint32_t maxCUWidth,
const uint32_t maxCUHeight,
const uint32_t maxTotalCUDepth,
RdCost* pcRdCost,
CABACWriter* CABACEstimator,
CtxCache* ctxCache
);
void destroy ();
void setTempBuffers (CodingStructure ****pSlitCS, CodingStructure ****pFullCS, CodingStructure **pSaveCS );

Karsten Suehring
committed
#if ENABLE_SPLIT_PARALLELISM
void copyState ( const InterSearch& other );
#endif
void setAffineModeSelected ( bool flag) { m_affineModeSelected = flag; }
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
void resetAffineMVList() { m_affMVListIdx = 0; m_affMVListSize = 0; }
void savePrevAffMVInfo(int idx, AffineMVInfo &tmpMVInfo, bool& isSaved)
{
if (m_affMVListSize > idx)
{
tmpMVInfo = m_affMVList[(m_affMVListIdx - 1 - idx + m_affMVListMaxSize) % m_affMVListMaxSize];
isSaved = true;
}
else
isSaved = false;
}
void addAffMVInfo(AffineMVInfo &tmpMVInfo)
{
int j = 0;
AffineMVInfo *prevInfo = nullptr;
for (; j < m_affMVListSize; j++)
{
prevInfo = m_affMVList + ((m_affMVListIdx - j - 1 + m_affMVListMaxSize) % (m_affMVListMaxSize));
if ((tmpMVInfo.x == prevInfo->x) && (tmpMVInfo.y == prevInfo->y) && (tmpMVInfo.w == prevInfo->w) && (tmpMVInfo.h == prevInfo->h))
{
break;
}
}
if (j < m_affMVListSize)
*prevInfo = tmpMVInfo;
else
{
m_affMVList[m_affMVListIdx] = tmpMVInfo;
m_affMVListIdx = (m_affMVListIdx + 1) % m_affMVListMaxSize;
m_affMVListSize = std::min(m_affMVListSize + 1, m_affMVListMaxSize);
}
}
#if JVET_M0246_AFFINE_AMVR
void resetSavedAffineMotion();
void storeAffineMotion( Mv acAffineMv[2][3], int16_t affineRefIdx[2], EAffineModel affineType, int gbiIdx );
#endif

Karsten Suehring
committed
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
protected:
/// sub-function for motion vector refinement used in fractional-pel accuracy
Distortion xPatternRefinement ( const CPelBuf* pcPatternKey, Mv baseRefMv, int iFrac, Mv& rcMvFrac, bool bAllowUseOfHadamard );
typedef struct
{
int left;
int right;
int top;
int bottom;
}SearchRange;
typedef struct
{
SearchRange searchRange;
const CPelBuf* pcPatternKey;
const Pel* piRefY;
int iRefStride;
int iBestX;
int iBestY;
uint32_t uiBestRound;
uint32_t uiBestDistance;
Distortion uiBestSad;
uint8_t ucPointNr;
int subShiftMode;
unsigned imvShift;
bool inCtuSearch;
bool zeroMV;

Karsten Suehring
committed
} IntTZSearchStruct;
// sub-functions for ME
inline void xTZSearchHelp ( IntTZSearchStruct& rcStruct, const int iSearchX, const int iSearchY, const uint8_t ucPointNr, const uint32_t uiDistance );
inline void xTZ2PointSearch ( IntTZSearchStruct& rcStruct );
inline void xTZ8PointSquareSearch ( IntTZSearchStruct& rcStruct, const int iStartX, const int iStartY, const int iDist );
inline void xTZ8PointDiamondSearch( IntTZSearchStruct& rcStruct, const int iStartX, const int iStartY, const int iDist, const bool bCheckCornersAtDist1 );
Distortion xGetInterPredictionError( PredictionUnit& pu, PelUnitBuf& origBuf, const RefPicList &eRefPicList = REF_PIC_LIST_X );
public:
/// encoder estimation - inter prediction (non-skip)
void setModeCtrl( EncModeCtrl *modeCtrl ) { m_modeCtrl = modeCtrl;}
void predInterSearch(CodingUnit& cu, Partitioner& partitioner );
/// set ME search range
void setAdaptiveSearchRange ( int iDir, int iRefIdx, int iSearchRange) { CHECK(iDir >= MAX_NUM_REF_LIST_ADAPT_SR || iRefIdx>=int(MAX_IDX_ADAPT_SR), "Invalid index"); m_aaiAdaptSR[iDir][iRefIdx] = iSearchRange; }
bool predIBCSearch ( CodingUnit& cu, Partitioner& partitioner, const int localSearchRangeX, const int localSearchRangeY, IbcHashMap& ibcHashMap);
void xIntraPatternSearch ( PredictionUnit& pu, IntTZSearchStruct& cStruct, Mv& rcMv, Distortion& ruiCost, Mv* cMvSrchRngLT, Mv* cMvSrchRngRB, Mv* pcMvPred);
void xSetIntraSearchRange ( PredictionUnit& pu, int iRoiWidth, int iRoiHeight, const int localSearchRangeX, const int localSearchRangeY, Mv& rcMvSrchRngLT, Mv& rcMvSrchRngRB);
void xIBCEstimation ( PredictionUnit& pu, PelUnitBuf& origBuf, Mv *pcMvPred, Mv &rcMv, Distortion &ruiCost, const int localSearchRangeX, const int localSearchRangeY);
void xIBCSearchMVCandUpdate ( Distortion uiSad, int x, int y, Distortion* uiSadBestCand, Mv* cMVCand);
int xIBCSearchMVChromaRefine( PredictionUnit& pu, int iRoiWidth, int iRoiHeight, int cuPelX, int cuPelY, Distortion* uiSadBestCand, Mv* cMVCand);

Karsten Suehring
committed
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
protected:
// -------------------------------------------------------------------------------------------------------------------
// Inter search (AMP)
// -------------------------------------------------------------------------------------------------------------------
void xEstimateMvPredAMVP ( PredictionUnit& pu,
PelUnitBuf& origBuf,
RefPicList eRefPicList,
int iRefIdx,
Mv& rcMvPred,
AMVPInfo& amvpInfo,
bool bFilled = false,
Distortion* puiDistBiP = NULL
);
void xCheckBestMVP ( RefPicList eRefPicList,
Mv cMv,
Mv& rcMvPred,
int& riMVPIdx,
AMVPInfo& amvpInfo,
uint32_t& ruiBits,
Distortion& ruiCost
,
const uint8_t imv
);
Distortion xGetTemplateCost ( const PredictionUnit& pu,
PelUnitBuf& origBuf,
PelUnitBuf& predBuf,
Mv cMvCand,
int iMVPIdx,
int iMVPNum,
RefPicList eRefPicList,
int iRefIdx
);
#if JVET_M0246_AFFINE_AMVR
uint32_t xCalcAffineMVBits ( PredictionUnit& pu, Mv mvCand[3], Mv mvPred[3], bool mvHighPrec = false );
#endif

Karsten Suehring
committed
void xCopyAMVPInfo ( AMVPInfo* pSrc, AMVPInfo* pDst );
uint32_t xGetMvpIdxBits ( int iIdx, int iNum );
void xGetBlkBits ( bool bPSlice, int iPartIdx, uint32_t uiLastMode, uint32_t uiBlkBit[3]);

Karsten Suehring
committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// -------------------------------------------------------------------------------------------------------------------
// motion estimation
// -------------------------------------------------------------------------------------------------------------------
void xMotionEstimation ( PredictionUnit& pu,
PelUnitBuf& origBuf,
RefPicList eRefPicList,
Mv& rcMvPred,
int iRefIdxPred,
Mv& rcMv,
int& riMVPIdx,
uint32_t& ruiBits,
Distortion& ruiCost,
const AMVPInfo& amvpInfo,
bool bBi = false
);
void xTZSearch ( const PredictionUnit& pu,
IntTZSearchStruct& cStruct,
Mv& rcMv,
Distortion& ruiSAD,
const Mv* const pIntegerMv2Nx2NPred,
const bool bExtendedSettings,
const bool bFastSettings = false
);
void xTZSearchSelective ( const PredictionUnit& pu,
IntTZSearchStruct& cStruct,
Mv& rcMv,
Distortion& ruiSAD,
const Mv* const pIntegerMv2Nx2NPred
);
void xSetSearchRange ( const PredictionUnit& pu,
const Mv& cMvPred,
const int iSrchRng,
SearchRange& sr
, IntTZSearchStruct & cStruct

Karsten Suehring
committed
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
);
void xPatternSearchFast ( const PredictionUnit& pu,
IntTZSearchStruct& cStruct,
Mv& rcMv,
Distortion& ruiSAD,
const Mv* const pIntegerMv2Nx2NPred
);
void xPatternSearch ( IntTZSearchStruct& cStruct,
Mv& rcMv,
Distortion& ruiSAD
);
void xPatternSearchIntRefine ( PredictionUnit& pu,
IntTZSearchStruct& cStruct,
Mv& rcMv,
Mv& rcMvPred,
int& riMVPIdx,
uint32_t& ruiBits,
Distortion& ruiCost,
const AMVPInfo& amvpInfo,
double fWeight
);
void xPatternSearchFracDIF ( const PredictionUnit& pu,
RefPicList eRefPicList,
int iRefIdx,
IntTZSearchStruct& cStruct,
const Mv& rcMvInt,
Mv& rcMvHalf,
Mv& rcMvQter,
Distortion& ruiCost
);
void xPredAffineInterSearch ( PredictionUnit& pu,
PelUnitBuf& origBuf,
int puIdx,
uint32_t& lastMode,
Distortion& affineCost,
Mv hevcMv[2][33]
, Mv mvAffine4Para[2][33][3]
, int refIdx4Para[2]
, uint8_t gbiIdx = GBI_DEFAULT
, bool enforceGBiPred = false
, uint32_t gbiIdxBits = 0

Karsten Suehring
committed
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
);
void xAffineMotionEstimation ( PredictionUnit& pu,
PelUnitBuf& origBuf,
RefPicList eRefPicList,
Mv acMvPred[3],
int iRefIdxPred,
Mv acMv[3],
uint32_t& ruiBits,
Distortion& ruiCost,
bool bBi = false
);
void xEstimateAffineAMVP ( PredictionUnit& pu,
AffineAMVPInfo& affineAMVPInfo,
PelUnitBuf& origBuf,
RefPicList eRefPicList,
int iRefIdx,
Mv acMvPred[3],
Distortion* puiDistBiP
);
Distortion xGetAffineTemplateCost( PredictionUnit& pu, PelUnitBuf& origBuf, PelUnitBuf& predBuf, Mv acMvCand[3], int iMVPIdx, int iMVPNum, RefPicList eRefPicList, int iRefIdx );
void xCopyAffineAMVPInfo ( AffineAMVPInfo& src, AffineAMVPInfo& dst );
void xCheckBestAffineMVP ( PredictionUnit &pu, AffineAMVPInfo &affineAMVPInfo, RefPicList eRefPicList, Mv acMv[3], Mv acMvPred[3], int& riMVPIdx, uint32_t& ruiBits, Distortion& ruiCost );
#if JVET_M0444_SMVD
Distortion xGetSymmetricCost( PredictionUnit& pu, PelUnitBuf& origBuf, RefPicList eCurRefPicList, const MvField& cCurMvField, MvField& cTarMvField , int gbiIdx );
Distortion xSymmeticRefineMvSearch( PredictionUnit& pu, PelUnitBuf& origBuf, Mv& rcMvCurPred, Mv& rcMvTarPred
, RefPicList eRefPicList, MvField& rCurMvField, MvField& rTarMvField, Distortion uiMinCost, int searchPattern, int nSearchStepShift, uint32_t uiMaxSearchRounds , int gbiIdx );
void xSymmetricMotionEstimation( PredictionUnit& pu, PelUnitBuf& origBuf, Mv& rcMvCurPred, Mv& rcMvTarPred, RefPicList eRefPicList, MvField& rCurMvField, MvField& rTarMvField, Distortion& ruiCost, int gbiIdx );
#endif
bool xReadBufferedAffineUniMv ( PredictionUnit& pu, RefPicList eRefPicList, int32_t iRefIdx, Mv acMvPred[3], Mv acMv[3], uint32_t& ruiBits, Distortion& ruiCost);
double xGetMEDistortionWeight ( uint8_t gbiIdx, RefPicList eRefPicList);
bool xReadBufferedUniMv ( PredictionUnit& pu, RefPicList eRefPicList, int32_t iRefIdx, Mv& pcMvPred, Mv& rcMv, uint32_t& ruiBits, Distortion& ruiCost);
void xClipMv ( Mv& rcMv, const struct Position& pos, const struct Size& size, const class SPS& sps );
void resetBufferedUniMotions () { m_uniMotions.reset(); }
uint32_t getWeightIdxBits ( uint8_t gbiIdx ) { return m_estWeightIdxBits[gbiIdx]; }
#if JVET_M0444_SMVD
void symmvdCheckBestMvp(
PredictionUnit& pu,
PelUnitBuf& origBuf,
Mv curMv,
RefPicList curRefList,
AMVPInfo amvpInfo[2][33],
int32_t gbiIdx,
Mv cMvPredSym[2],
int32_t mvpIdxSym[2],
Distortion& bestCost,
bool skip = false
);
#endif

Karsten Suehring
committed
void xExtDIFUpSamplingH ( CPelBuf* pcPattern );
void xExtDIFUpSamplingQ ( CPelBuf* pcPatternKey, Mv halfPelRef );
// -------------------------------------------------------------------------------------------------------------------
// compute symbol bits
// -------------------------------------------------------------------------------------------------------------------
void setWpScalingDistParam ( int iRefIdx, RefPicList eRefPicListCur, Slice *slice );
void xxIBCHashSearch(PredictionUnit& pu, Mv* mvPred, int numMvPred, Mv &mv, int& idxMvPred, IbcHashMap& ibcHashMap);

Karsten Suehring
committed
public:
void encodeResAndCalcRdInterCU (CodingStructure &cs, Partitioner &partitioner, const bool &skipResidual

Karsten Suehring
committed
void xEncodeInterResidualQT (CodingStructure &cs, Partitioner &partitioner, const ComponentID &compID);
void xEstimateInterResidualQT (CodingStructure &cs, Partitioner &partitioner, Distortion *puiZeroDist = NULL

Karsten Suehring
committed
uint64_t xGetSymbolFracBitsInter (CodingStructure &cs, Partitioner &partitioner);
};// END CLASS DEFINITION EncSearch
//! \}
#endif // __ENCSEARCH__