Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvdec/source/Lib/CommonLib/UnitTools.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
The copyright in this software is being made available under the Clear BSD
3
License, included below. No patent rights, trademark rights and/or 
4
other Intellectual Property Rights other than the copyrights concerning 
5
the Software are granted under this license.
6
7
The Clear BSD License
8
9
Copyright (c) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC Authors.
10
All rights reserved.
11
12
Redistribution and use in source and binary forms, with or without modification,
13
are permitted (subject to the limitations in the disclaimer below) provided that
14
the following conditions are met:
15
16
     * Redistributions of source code must retain the above copyright notice,
17
     this list of conditions and the following disclaimer.
18
19
     * Redistributions in binary form must reproduce the above copyright
20
     notice, this list of conditions and the following disclaimer in the
21
     documentation and/or other materials provided with the distribution.
22
23
     * Neither the name of the copyright holder nor the names of its
24
     contributors may be used to endorse or promote products derived from this
25
     software without specific prior written permission.
26
27
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
28
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
36
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
POSSIBILITY OF SUCH DAMAGE.
39
40
41
------------------------------------------------------------------------------------------- */
42
43
/** \file     UnitTool.cpp
44
 *  \brief    defines operations for basic units
45
 */
46
47
#include "UnitTools.h"
48
49
#include "dtrace_next.h"
50
51
#include "Unit.h"
52
#include "Slice.h"
53
#include "Picture.h"
54
55
#include <utility>
56
#include <algorithm>
57
58
namespace vvdec
59
{
60
61
static bool isDualITree( const Slice &slice )
62
0
{
63
0
#if GDR_ADJ
64
0
  return slice.isIntra() && slice.getSPS()->getUseDualITree();
65
#else
66
  return slice.isIRAP() && slice.getSPS()->getUseDualITree();
67
#endif
68
0
}
69
70
71
bool CU::isDualITree( const CodingUnit &cu )
72
0
{
73
0
#if GDR_ADJ
74
0
  return cu.slice->isIntra() && cu.sps->getUseDualITree();
75
#else
76
  return cu.slice->isIRAP() && cu.sps->getUseDualITree();
77
#endif
78
0
}
79
80
UnitArea getArea( const Slice &slice, const UnitArea &area, const ChannelType chType, const TreeType treeType )
81
0
{
82
0
  return isDualITree( slice ) || treeType != TREE_D ? area.singleChan( chType ) : area;
83
0
}
84
85
// CU tools
86
87
bool CU::getRprScaling( const SPS* sps, const PPS* curPPS, const PPS* refPPS, int& xScale, int& yScale )
88
0
{
89
  // 64 bit, because the scaling window offsets are only bounded by +/- 15 * the picture size
90
0
  const Window& curScalingWindow = curPPS->getScalingWindow();
91
0
  int64_t curPicWidth = curPPS->getPicWidthInLumaSamples() - (int64_t) (curScalingWindow.getWindowLeftOffset() + curScalingWindow.getWindowRightOffset()) * SPS::getWinUnitX(sps->getChromaFormatIdc());
92
0
  int64_t curPicHeight = curPPS->getPicHeightInLumaSamples() - (int64_t) (curScalingWindow.getWindowTopOffset() + curScalingWindow.getWindowBottomOffset()) * SPS::getWinUnitY(sps->getChromaFormatIdc());
93
0
  const Window& refScalingWindow = refPPS->getScalingWindow();
94
0
  int64_t refPicWidth = refPPS->getPicWidthInLumaSamples() - (int64_t) (refScalingWindow.getWindowLeftOffset() + refScalingWindow.getWindowRightOffset()) * SPS::getWinUnitX(sps->getChromaFormatIdc());
95
0
  int64_t refPicHeight = refPPS->getPicHeightInLumaSamples() - (int64_t) (refScalingWindow.getWindowTopOffset() + refScalingWindow.getWindowBottomOffset()) * SPS::getWinUnitY(sps->getChromaFormatIdc());
96
97
0
  CHECK( curPicWidth <= 0 || curPicHeight <= 0 || refPicWidth <= 0 || refPicHeight <= 0, "scaling window larger than the picture" );
98
99
0
  int curSeqMaxPicWidthY = sps->getMaxPicWidthInLumaSamples();                  // pic_width_max_in_luma_samples
100
0
  int curSeqMaxPicHeightY = sps->getMaxPicHeightInLumaSamples();                // pic_height_max_in_luma_samples
101
0
  int curPicWidthY = curPPS->getPicWidthInLumaSamples();                        // pic_width_in_luma_samples
102
0
  int curPicHeightY = curPPS->getPicHeightInLumaSamples();                      // pic_height_in_luma_samples
103
0
  int max8MinCbSizeY = std::max((int)8, (1<<sps->getLog2MinCodingBlockSize())); // Max(8, MinCbSizeY)
104
105
0
  CHECK((curPicWidth * curSeqMaxPicWidthY) < refPicWidth * (curPicWidthY - max8MinCbSizeY), "(curPicWidth * curSeqMaxPicWidthY) should be greater than or equal to refPicWidth * (curPicWidthY - max8MinCbSizeY))");
106
0
  CHECK((curPicHeight * curSeqMaxPicHeightY) < refPicHeight * (curPicHeightY - max8MinCbSizeY), "(curPicHeight * curSeqMaxPicHeightY) should be greater than or equal to refPicHeight * (curPicHeightY - max8MinCbSizeY))");
107
108
0
  CHECK(curPicWidth * 2 < refPicWidth, "curPicWidth * 2 shall be greater than or equal to refPicWidth");
109
0
  CHECK(curPicHeight * 2 < refPicHeight, "curPicHeight * 2 shall be greater than or equal to refPicHeight");
110
0
  CHECK(curPicWidth > refPicWidth * 8, "curPicWidth shall be less than or equal to refPicWidth * 8");
111
0
  CHECK(curPicHeight > refPicHeight * 8, "curPicHeight shall be less than or equal to refPicHeight * 8");
112
113
  // only now that the ratios are known to be sane, the scale factors are guaranteed to fit into 32 bit
114
0
  xScale = (int) ( ( ( refPicWidth << SCALE_RATIO_BITS ) + ( curPicWidth >> 1 ) ) / curPicWidth );
115
0
  yScale = (int) ( ( ( refPicHeight << SCALE_RATIO_BITS ) + ( curPicHeight >> 1 ) ) / curPicHeight );
116
117
0
  return false; // return whatever, because it's not used... to be changed
118
0
}
119
120
void CU::checkConformanceILRP(Slice *slice)
121
0
{
122
0
  const int numRefList = (slice->getSliceType() == B_SLICE) ? (2) : (1);
123
124
0
  int currentSubPicIdx = NOT_VALID;
125
126
  // derive sub-picture index for the current slice
127
0
  for( int subPicIdx = 0; subPicIdx < slice->getPic()->cs->sps->getNumSubPics(); subPicIdx++ )
128
0
  {
129
0
    if( slice->getPic()->cs->pps->getSubPic( subPicIdx ).getSubPicID() == slice->getSliceSubPicId() )
130
0
    {
131
0
      currentSubPicIdx = subPicIdx;
132
0
      break;
133
0
    }
134
0
  }
135
136
0
  CHECK( currentSubPicIdx == NOT_VALID, "Sub-picture was not found" );
137
138
0
  if( !slice->getPic()->cs->sps->getSubPicTreatedAsPicFlag( currentSubPicIdx ) )
139
0
  {
140
0
    return;
141
0
  }
142
143
  //constraint 1: The picture referred to by each active entry in RefPicList[ 0 ] or RefPicList[ 1 ] has the same subpicture layout as the current picture
144
0
  bool isAllRefSameSubpicLayout = true;
145
0
  for (int refList = 0; refList < numRefList; refList++) // loop over l0 and l1
146
0
  {
147
0
    RefPicList  eRefPicList = (refList ? REF_PIC_LIST_1 : REF_PIC_LIST_0);
148
0
    for (int refIdx = 0; refIdx < slice->getNumRefIdx(eRefPicList); refIdx++)
149
0
    {
150
0
      const Picture* refPic = slice->getRefPic( eRefPicList, refIdx );
151
152
0
      if( refPic->subPictures.size() != slice->getPic()->cs->pps->getNumSubPics() )
153
0
      {
154
0
        isAllRefSameSubpicLayout = false;
155
0
        refList = numRefList;
156
0
        break;
157
0
      }
158
0
      else
159
0
      {
160
0
        for( int i = 0; i < refPic->subPictures.size(); i++ )
161
0
        {
162
0
          const SubPic& refSubPic = refPic->subPictures[i];
163
0
          const SubPic& curSubPic = slice->getPic()->cs->pps->getSubPic( i );
164
165
0
          if( refSubPic.getSubPicWidthInCTUs() != curSubPic.getSubPicWidthInCTUs()
166
0
            || refSubPic.getSubPicHeightInCTUs() != curSubPic.getSubPicHeightInCTUs()
167
0
            || refSubPic.getSubPicCtuTopLeftX() != curSubPic.getSubPicCtuTopLeftX()
168
0
            || refSubPic.getSubPicCtuTopLeftY() != curSubPic.getSubPicCtuTopLeftY()
169
0
            || ( refPic->layerId != slice->getPic()->layerId && refSubPic.getSubPicID() != curSubPic.getSubPicID() )
170
0
            || refSubPic.getTreatedAsPicFlag() != curSubPic.getTreatedAsPicFlag())
171
0
          {
172
0
            isAllRefSameSubpicLayout = false;
173
0
            refIdx = slice->getNumRefIdx(eRefPicList);
174
0
            refList = numRefList;
175
0
            break;
176
0
          }
177
0
        }
178
179
        // A picture with different sub-picture ID of the collocated sub-picture cannot be used as an active reference picture in the same layer
180
0
        if( refPic->layerId == slice->getPic()->layerId )
181
0
        {
182
0
          isAllRefSameSubpicLayout = isAllRefSameSubpicLayout && refPic->subPictures[currentSubPicIdx].getSubPicID() == slice->getSliceSubPicId();
183
0
        }
184
0
      }
185
0
    }
186
0
  }
187
188
  //constraint 2: The picture referred to by each active entry in RefPicList[ 0 ] or RefPicList[ 1 ] is an ILRP for which the value of sps_num_subpics_minus1 is equal to 0
189
0
  if (!isAllRefSameSubpicLayout)
190
0
  {
191
0
    for (int refList = 0; refList < numRefList; refList++) // loop over l0 and l1
192
0
    {
193
0
      RefPicList  eRefPicList = (refList ? REF_PIC_LIST_1 : REF_PIC_LIST_0);
194
0
      for (int refIdx = 0; refIdx < slice->getNumRefIdx(eRefPicList); refIdx++)
195
0
      {
196
0
        const Picture* refPic = slice->getRefPic( eRefPicList, refIdx );
197
0
        CHECK( refPic->layerId == slice->getPic()->layerId || refPic->subPictures.size() > 1, "The inter-layer reference shall contain a single subpicture or have same subpicture layout with the current picture" );
198
0
      }
199
0
    }
200
0
  }
201
202
0
  return;
203
0
}
204
205
bool CU::isSameSlice(const CodingUnit& cu, const CodingUnit& cu2)
206
0
{
207
0
  return cu.slice->getIndependentSliceIdx() == cu2.slice->getIndependentSliceIdx();
208
0
}
209
210
bool CU::isSameTile(const CodingUnit& cu, const CodingUnit& cu2)
211
0
{
212
0
  return cu.tileIdx == cu2.tileIdx;
213
0
}
214
215
bool CU::isSameSliceAndTile(const CodingUnit& cu, const CodingUnit& cu2)
216
0
{
217
0
  return isSameSlice(cu, cu2) && isSameTile(cu, cu2);
218
0
}
219
220
bool CU::isSameSubPic(const CodingUnit& cu, const CodingUnit& cu2)
221
0
{
222
0
  return (cu.pps->getSubPicFromCU(cu).getSubPicIdx() == cu2.pps->getSubPicFromCU(cu2).getSubPicIdx()) ;
223
0
}
224
225
bool CU::isSameCtu(const CodingUnit& cu, const CodingUnit& cu2)
226
0
{
227
0
  uint32_t ctuSizeBit = getLog2(cu.sps->getMaxCUWidth());
228
229
0
  Position pos1Ctu(cu.lumaPos().x  >> ctuSizeBit, cu.lumaPos().y  >> ctuSizeBit);
230
0
  Position pos2Ctu(cu2.lumaPos().x >> ctuSizeBit, cu2.lumaPos().y >> ctuSizeBit);
231
232
0
  return pos1Ctu.x == pos2Ctu.x && pos1Ctu.y == pos2Ctu.y;
233
0
}
234
235
bool CU::isAvailable( const CodingUnit& cu, const CodingUnit& cu2, const bool bEnforceSliceRestriction, const bool bEnforceTileRestriction, const bool bEnforceSubPicRestriction )
236
0
{
237
0
  return ( !bEnforceSliceRestriction || CU::isSameSlice( cu, cu2 ) )
238
0
         && ( !bEnforceTileRestriction || CU::isSameTile( cu, cu2 ) )
239
0
         && ( !bEnforceSubPicRestriction || CU::isSameSubPic( cu, cu2 ) );
240
0
}
241
242
uint32_t CU::getCtuAddr( const CodingUnit &cu )
243
0
{
244
0
  return getCtuAddr( cu.blocks[cu.chType()].lumaPos( cu.chromaFormat ), *cu.cs->pcv );
245
0
}
246
247
int CU::predictQP( const CodingUnit& cu, const int prevQP )
248
0
{
249
0
  const ChannelType      chType  = cu.chType();
250
0
  const CodingStructure& cs      = *cu.cs;
251
0
  const CodingUnit*      cuAbove = cs.getCU( cu.blocks[chType].pos().offset( 0, -1 ), chType );
252
0
  const CodingUnit*      cuLeft  = cs.getCU( cu.blocks[chType].pos().offset( -1, 0 ), chType );
253
254
0
  uint32_t  ctuRsAddr       = getCtuAddr( cu );
255
0
  uint32_t  ctuXPosInCtus   = ctuRsAddr % cs.pcv->widthInCtus;
256
0
  uint32_t  tileColIdx      = cu.pps->ctuToTileCol( ctuXPosInCtus );
257
0
  uint32_t  tileXPosInCtus  = cu.pps->getTileColumnBd( tileColIdx );
258
0
  if( ctuXPosInCtus == tileXPosInCtus &&
259
0
      !( cu.blocks[chType].x & ( cs.pcv->maxCUWidthMask  >> getChannelTypeScaleX( chType, cu.chromaFormat ) ) ) &&
260
0
      !( cu.blocks[chType].y & ( cs.pcv->maxCUHeightMask >> getChannelTypeScaleY( chType, cu.chromaFormat ) ) ) &&
261
0
       cuAbove != nullptr && CU::isSameSliceAndTile( *cuAbove, cu ) )
262
0
  {
263
0
    return cuAbove->qp;
264
0
  }
265
0
  else
266
0
  {
267
0
    const int a = ( cu.blocks[chType].y & ( cs.pcv->maxCUHeightMask >> getChannelTypeScaleY( chType, cu.chromaFormat ) ) ) ? cuAbove->qp : prevQP;
268
0
    const int b = ( cu.blocks[chType].x & ( cs.pcv->maxCUWidthMask  >> getChannelTypeScaleX( chType, cu.chromaFormat ) ) ) ? cuLeft->qp  : prevQP;
269
270
0
    return ( a + b + 1 ) >> 1;
271
0
  }
272
0
}
273
274
bool CU::divideTuInRows( const CodingUnit &cu )
275
0
{
276
0
  CHECK( cu.ispMode() != HOR_INTRA_SUBPARTITIONS && cu.ispMode() != VER_INTRA_SUBPARTITIONS, "Intra Subpartitions type not recognized!" );
277
0
  return cu.ispMode() == HOR_INTRA_SUBPARTITIONS ? true : false;
278
0
}
279
280
PartSplit CU::getISPType( const CodingUnit &cu, const ComponentID compID )
281
0
{
282
0
  if( cu.ispMode() && isLuma( compID ) )
283
0
  {
284
0
    const bool tuIsDividedInRows = CU::divideTuInRows( cu );
285
286
0
    return tuIsDividedInRows ? TU_1D_HORZ_SPLIT : TU_1D_VERT_SPLIT;
287
0
  }
288
0
  return TU_NO_ISP;
289
0
}
290
291
ISPType CU::canUseISPSplit( const CodingUnit &cu, const ComponentID compID )
292
0
{
293
0
  const int width     = cu.blocks[compID].width;
294
0
  const int height    = cu.blocks[compID].height;
295
0
  const int maxTrSize = cu.sps->getMaxTbSize();
296
297
0
  return CU::canUseISPSplit( width, height, maxTrSize );
298
0
}
299
300
bool CU::canUseLfnstWithISP( const CompArea& cuArea, const ISPType ispSplitType )
301
0
{
302
0
  if( ispSplitType == NOT_INTRA_SUBPARTITIONS )
303
0
  {
304
0
    return false;
305
0
  }
306
307
0
  const Size tuSize = ( ispSplitType == HOR_INTRA_SUBPARTITIONS )
308
0
    ? Size( cuArea.width, CU::getISPSplitDim( cuArea.width, cuArea.height, TU_1D_HORZ_SPLIT ) )
309
0
    : Size( CU::getISPSplitDim( cuArea.width, cuArea.height, TU_1D_VERT_SPLIT ), cuArea.height );
310
311
0
  if( !( tuSize.width >= MIN_TB_SIZEY && tuSize.height >= MIN_TB_SIZEY ) )
312
0
  {
313
0
    return false;
314
0
  }
315
0
  return true;
316
0
}
317
318
bool CU::canUseLfnstWithISP( const CodingUnit& cu, const ChannelType chType )
319
0
{
320
0
  CHECK( !isLuma( chType ), "Wrong ISP mode!" );
321
0
  return CU::canUseLfnstWithISP( cu.blocks[chType == CHANNEL_TYPE_LUMA ? 0 : 1], (ISPType)cu.ispMode() );
322
0
}
323
324
ISPType CU::canUseISPSplit( const int width, const int height, const int maxTrSize )
325
0
{
326
0
  const uint32_t minTuSizeForISP  = MIN_TB_SIZEY;
327
0
  bool  notEnoughSamplesToSplit   = ( getLog2( width ) + getLog2( height ) <= ( getLog2( minTuSizeForISP ) << 1 ) );
328
0
  bool  cuSizeLargerThanMaxTrSize = width  > maxTrSize || height > maxTrSize;
329
330
0
  int   widthCanBeUsed  = ( !cuSizeLargerThanMaxTrSize && !notEnoughSamplesToSplit ) ? 4 : 2;
331
0
  int   heightCanBeUsed = ( !cuSizeLargerThanMaxTrSize && !notEnoughSamplesToSplit ) ? 0 : 2;
332
333
0
  return ISPType( widthCanBeUsed >> heightCanBeUsed );
334
335
  //  widthCanBeUsed &&  heightCanBeUsed -> 4
336
  // !widthCanBeUsed &&  heightCanBeUsed -> 2
337
  //  widthCanBeUsed && !heightCanBeUsed -> 1
338
  // !widthCanBeUsed && !heightCanBeUsed -> 0
339
0
}
340
341
uint32_t CU::getISPSplitDim( const int width, const int height, const PartSplit ispType )
342
0
{
343
0
  bool divideTuInRows = ispType == TU_1D_HORZ_SPLIT;
344
0
  uint32_t splitDimensionSize, nonSplitDimensionSize, partitionSize, divShift = 2;
345
346
0
  if( divideTuInRows )
347
0
  {
348
0
    splitDimensionSize    = height;
349
0
    nonSplitDimensionSize = width;
350
0
  }
351
0
  else
352
0
  {
353
0
    splitDimensionSize    = width;
354
0
    nonSplitDimensionSize = height;
355
0
  }
356
357
0
  const int minNumberOfSamplesPerCu = 1 << ( ( getLog2(MIN_TB_SIZEY) << 1 ) );
358
0
  const int factorToMinSamples = nonSplitDimensionSize < minNumberOfSamplesPerCu ? minNumberOfSamplesPerCu >> getLog2(nonSplitDimensionSize) : 1;
359
0
  partitionSize = ( splitDimensionSize >> divShift ) < factorToMinSamples ? factorToMinSamples : ( splitDimensionSize >> divShift );
360
361
0
  CHECK( getLog2(partitionSize) + getLog2(nonSplitDimensionSize) < getLog2(minNumberOfSamplesPerCu), "A partition has less than the minimum amount of samples!" );
362
0
  return partitionSize;
363
0
}
364
365
// PU tools
366
367
int PU::getIntraMPMs( const CodingUnit &cu, unsigned* mpm, const ChannelType &channelType /*= CHANNEL_TYPE_LUMA*/ )
368
0
{
369
0
  const int numMPMs = NUM_MOST_PROBABLE_MODES;
370
0
  {
371
0
    CHECK(channelType != CHANNEL_TYPE_LUMA, "Not harmonized yet");
372
0
    int numCand      = -1;
373
0
    int leftIntraDir = PLANAR_IDX, aboveIntraDir = PLANAR_IDX;
374
375
0
    const CompArea &area = cu.block(getFirstComponentOfChannel(channelType));
376
0
    const Position posRT = area.topRight();
377
0
    const Position posLB = area.bottomLeft();
378
379
    // Get intra direction of left PU
380
0
    const CodingUnit *cuLeft = cu.cs->getCURestricted(posLB.offset(-1, 0), cu, channelType, cu.left);
381
0
    if( cuLeft && CU::isIntra( *cuLeft ) )
382
0
    {
383
0
      leftIntraDir = PU::getIntraDirLuma( *cuLeft );
384
0
    }
385
386
    // Get intra direction of above PU
387
0
    const CodingUnit *cuAbove = cu.cs->getCURestricted(posRT.offset(0, -1), cu, channelType, cu.above);
388
0
    if( cuAbove && CU::isIntra( *cuAbove ) && CU::isSameCtu( cu, *cuAbove ) )
389
0
    {
390
0
      aboveIntraDir = PU::getIntraDirLuma( *cuAbove );
391
0
    }
392
393
0
    CHECK(2 >= numMPMs, "Invalid number of most probable modes");
394
395
0
    const int offset = (int)NUM_LUMA_MODE - 6;
396
0
    const int mod = offset + 3;
397
398
0
    {
399
0
      mpm[0] = PLANAR_IDX;
400
0
      mpm[1] = DC_IDX;
401
0
      mpm[2] = VER_IDX;
402
0
      mpm[3] = HOR_IDX;
403
0
      mpm[4] = VER_IDX - 4;
404
0
      mpm[5] = VER_IDX + 4;
405
406
0
      if (leftIntraDir == aboveIntraDir)
407
0
      {
408
0
        numCand = 1;
409
0
        if (leftIntraDir > DC_IDX)
410
0
        {
411
0
          mpm[0] = PLANAR_IDX;
412
0
          mpm[1] = leftIntraDir;
413
0
          mpm[2] = ((leftIntraDir + offset) % mod) + 2;
414
0
          mpm[3] = ((leftIntraDir - 1) % mod) + 2;
415
0
          mpm[4] = ((leftIntraDir + offset - 1) % mod) + 2;
416
0
          mpm[5] = ( leftIntraDir               % mod) + 2;
417
0
        }
418
0
      }
419
0
      else //L!=A
420
0
      {
421
0
        numCand = 2;
422
0
        int  maxCandModeIdx = mpm[0] > mpm[1] ? 0 : 1;
423
424
0
        if ((leftIntraDir > DC_IDX) && (aboveIntraDir > DC_IDX))
425
0
        {
426
0
          mpm[0] = PLANAR_IDX;
427
0
          mpm[1] = leftIntraDir;
428
0
          mpm[2] = aboveIntraDir;
429
0
          maxCandModeIdx = mpm[1] > mpm[2] ? 1 : 2;
430
0
          int minCandModeIdx = mpm[1] > mpm[2] ? 2 : 1;
431
0
          if (mpm[maxCandModeIdx] - mpm[minCandModeIdx] == 1)
432
0
          {
433
0
            mpm[3] = ((mpm[minCandModeIdx] + offset)     % mod) + 2;
434
0
            mpm[4] = ((mpm[maxCandModeIdx] - 1)          % mod) + 2;
435
0
            mpm[5] = ((mpm[minCandModeIdx] + offset - 1) % mod) + 2;
436
0
          }
437
0
          else if (mpm[maxCandModeIdx] - mpm[minCandModeIdx] >= 62)
438
0
          {
439
0
            mpm[3] = ((mpm[minCandModeIdx] - 1)      % mod) + 2;
440
0
            mpm[4] = ((mpm[maxCandModeIdx] + offset) % mod) + 2;
441
0
            mpm[5] = ( mpm[minCandModeIdx]           % mod) + 2;
442
0
          }
443
0
          else if (mpm[maxCandModeIdx] - mpm[minCandModeIdx] == 2)
444
0
          {
445
0
            mpm[3] = ((mpm[minCandModeIdx] - 1)      % mod) + 2;
446
0
            mpm[4] = ((mpm[minCandModeIdx] + offset) % mod) + 2;
447
0
            mpm[5] = ((mpm[maxCandModeIdx] - 1)      % mod) + 2;
448
0
          }
449
0
          else
450
0
          {
451
0
            mpm[3] = ((mpm[minCandModeIdx] + offset) % mod) + 2;
452
0
            mpm[4] = ((mpm[minCandModeIdx] - 1)      % mod) + 2;
453
0
            mpm[5] = ((mpm[maxCandModeIdx] + offset) % mod) + 2;
454
0
          }
455
0
        }
456
0
        else if (leftIntraDir + aboveIntraDir >= 2)
457
0
        {
458
0
          mpm[0] = PLANAR_IDX;
459
0
          mpm[1] = (leftIntraDir < aboveIntraDir) ? aboveIntraDir : leftIntraDir;
460
0
          maxCandModeIdx = 1;
461
0
          mpm[2] = ((mpm[maxCandModeIdx] + offset)     % mod) + 2;
462
0
          mpm[3] = ((mpm[maxCandModeIdx] - 1)          % mod) + 2;
463
0
          mpm[4] = ((mpm[maxCandModeIdx] + offset - 1) % mod) + 2;
464
0
          mpm[5] = ( mpm[maxCandModeIdx]               % mod) + 2;
465
0
        }
466
0
      }
467
0
    }
468
0
    for (int i = 0; i < numMPMs; i++)
469
0
    {
470
0
      CHECK(mpm[i] >= NUM_LUMA_MODE, "Invalid MPM");
471
0
    }
472
0
    CHECK(numCand == 0, "No candidates found");
473
0
    return numCand;
474
0
  }
475
0
}
476
477
bool CU::isMIP( const CodingUnit &cu, const ChannelType &chType )
478
0
{
479
0
  if( chType == CHANNEL_TYPE_LUMA )
480
0
  {
481
    // Default case if chType is omitted.
482
0
    return cu.mipFlag();
483
0
  }
484
0
  else
485
0
  {
486
0
    return PU::isDMChromaMIP(cu) && (cu.intraDir[CHANNEL_TYPE_CHROMA] == DM_CHROMA_IDX);
487
0
  }
488
0
}
489
490
bool PU::isDMChromaMIP(const CodingUnit &cu)
491
0
{
492
0
  return !CU::isSepTree( cu ) && (cu.chromaFormat == CHROMA_444) && getCoLocatedLumaPU(cu).mipFlag();
493
0
}
494
495
int PU::getMipSizeId(const CodingUnit &cu)
496
0
{
497
0
  if( ( cu.lwidth() == 4 ) && ( cu.lheight() == 4 ) )
498
0
  {
499
0
    return 0; // MIP with 16x4 matrix
500
0
  }
501
0
  else if( cu.lwidth() <= 8 && cu.lheight() <= 8 )
502
0
  {
503
0
    return 1; // MIP with 16x8 matrix
504
0
  }
505
0
  else
506
0
  {
507
0
    return 2; // MIP with 64x8 matrix
508
0
  }
509
0
}
510
511
512
uint32_t PU::getIntraDirLuma( const CodingUnit &cu )
513
0
{
514
0
  if( CU::isMIP( cu ) )
515
0
  {
516
0
    return PLANAR_IDX;
517
0
  }
518
0
  else
519
0
  {
520
0
    return cu.intraDir[CHANNEL_TYPE_LUMA];
521
0
  }
522
0
}
523
524
525
void PU::getIntraChromaCandModes( const CodingUnit &cu, unsigned modeList[NUM_CHROMA_MODE] )
526
0
{
527
0
  modeList[0] = PLANAR_IDX;
528
0
  modeList[1] = VER_IDX;
529
0
  modeList[2] = HOR_IDX;
530
0
  modeList[3] = DC_IDX;
531
0
  modeList[4] = LM_CHROMA_IDX;
532
0
  modeList[5] = MDLM_L_IDX;
533
0
  modeList[6] = MDLM_T_IDX;
534
0
  modeList[7] = DM_CHROMA_IDX;
535
536
  // If Direct Mode is MIP, mode cannot be already in the list.
537
0
  if( isDMChromaMIP(cu) )
538
0
  {
539
0
    return;
540
0
  }
541
542
0
  const uint32_t lumaMode = getCoLocatedIntraLumaMode( cu );
543
0
  for( int i = 0; i < 4; i++ )
544
0
  {
545
0
    if( lumaMode == modeList[i] )
546
0
    {
547
0
      modeList[i] = VDIA_IDX;
548
0
      break;
549
0
    }
550
0
  }
551
0
}
552
553
554
bool PU::isLMCMode(unsigned mode)
555
0
{
556
0
  return mode >= LM_CHROMA_IDX && mode <= MDLM_T_IDX;
557
0
}
558
559
int PU::getLMSymbolList( const CodingUnit &cu, int *pModeList )
560
0
{
561
0
  pModeList[0] = LM_CHROMA_IDX;
562
0
  pModeList[1] = MDLM_L_IDX;
563
0
  pModeList[2] = MDLM_T_IDX;
564
565
0
  return 3;
566
0
}
567
568
uint32_t PU::getFinalIntraMode( const CodingUnit &cu, const ChannelType &chType )
569
0
{
570
0
  uint32_t uiIntraMode = cu.intraDir[chType];
571
572
0
  if( uiIntraMode == DM_CHROMA_IDX && !isLuma( chType ) )
573
0
  {
574
0
    uiIntraMode = getCoLocatedIntraLumaMode( cu );
575
0
  }
576
0
  if( cu.chromaFormat == CHROMA_422 && !isLuma( chType ) && uiIntraMode < NUM_LUMA_MODE ) // map directional, planar and dc
577
0
  {
578
0
    uiIntraMode = g_chroma422IntraAngleMappingTable[uiIntraMode];
579
0
  }
580
0
  return uiIntraMode;
581
0
}
582
583
const CodingUnit &PU::getCoLocatedLumaPU( const CodingUnit &cu )
584
0
{
585
0
  Position              topLeftPos = cu.blocks[cu.chType()].lumaPos ( cu.chromaFormat );
586
0
  Position refPos     = topLeftPos.offset( cu.blocks[cu.chType()].lumaSize( cu.chromaFormat ).width >> 1, cu.blocks[cu.chType()].lumaSize( cu.chromaFormat ).height >> 1 );
587
588
0
  const CodingUnit &lumaPU = CU::isSepTree( cu ) ? *cu.cs->getCU( refPos, CHANNEL_TYPE_LUMA ) : cu;
589
590
0
  return lumaPU;
591
0
}
592
593
uint32_t PU::getCoLocatedIntraLumaMode( const CodingUnit &cu )
594
0
{
595
0
  return PU::getIntraDirLuma( PU::getCoLocatedLumaPU(cu) );
596
0
}
597
598
int PU::getWideAngIntraMode( const TransformUnit &tu, const uint32_t dirMode, const ComponentID compID )
599
0
{
600
0
  if( dirMode < 2 )
601
0
  {
602
0
    return ( int ) dirMode;
603
0
  }
604
0
  const CompArea&  area         = tu.cu->ispMode() && isLuma(compID) ? tu.cu->blocks[compID] : tu.blocks[ compID ];
605
0
  int              width        = int( area.width );
606
0
  int              height       = int( area.height );
607
0
  static const int modeShift[ ] = { 0, 6, 10, 12, 14, 15 };
608
0
  int              deltaSize    = abs( getLog2( width ) - getLog2( height ) );
609
0
  int              predMode     = dirMode;
610
611
0
  if( width > height && dirMode < 2 + modeShift[ deltaSize ] )
612
0
  {
613
0
    predMode += ( VDIA_IDX - 1 );
614
0
  }
615
0
  else if( height > width && predMode > VDIA_IDX - modeShift[ deltaSize ] )
616
0
  {
617
0
    predMode -= ( VDIA_IDX + 1 );
618
0
  }
619
620
0
  return predMode;
621
0
}
622
623
bool PU::xCheckSimilarMotion(const int mergeCandIndex, const int prevCnt, const MergeCtx& mergeCandList, bool hasPruned[MRG_MAX_NUM_CANDS])
624
0
{
625
0
  for (uint32_t ui = 0; ui < prevCnt; ui++)
626
0
  {
627
0
    if (hasPruned[ui])
628
0
    {
629
0
      continue;
630
0
    }
631
0
    if (mergeCandList.interDirNeighbours[ui] == mergeCandList.interDirNeighbours[mergeCandIndex])
632
0
    {
633
0
      if (mergeCandList.interDirNeighbours[ui] == 3)
634
0
      {
635
0
        int offset0 = (ui * 2);
636
0
        int offset1 = (mergeCandIndex * 2);
637
0
        if (mergeCandList.mvFieldNeighbours[offset0    ].mfRefIdx == mergeCandList.mvFieldNeighbours[offset1    ].mfRefIdx &&
638
0
            mergeCandList.mvFieldNeighbours[offset0 + 1].mfRefIdx == mergeCandList.mvFieldNeighbours[offset1 + 1].mfRefIdx &&
639
0
            mergeCandList.mvFieldNeighbours[offset0    ].mv     == mergeCandList.mvFieldNeighbours[offset1    ].mv     &&
640
0
            mergeCandList.mvFieldNeighbours[offset0 + 1].mv     == mergeCandList.mvFieldNeighbours[offset1 + 1].mv
641
0
          )
642
0
        {
643
0
          hasPruned[ui] = true;
644
0
          return true;
645
0
        }
646
0
      }
647
0
      else
648
0
      {
649
0
        int offset0 = (ui             * 2) + mergeCandList.interDirNeighbours[ui] - 1;
650
0
        int offset1 = (mergeCandIndex * 2) + mergeCandList.interDirNeighbours[ui] - 1;
651
0
        if (mergeCandList.mvFieldNeighbours[offset0].mfRefIdx == mergeCandList.mvFieldNeighbours[offset1].mfRefIdx &&
652
0
            mergeCandList.mvFieldNeighbours[offset0].mv       == mergeCandList.mvFieldNeighbours[offset1].mv
653
0
          )
654
0
        {
655
0
          hasPruned[ui] = true;
656
0
          return true;
657
0
        }
658
0
      }
659
0
    }
660
0
  }
661
662
0
  return false;
663
0
}
664
665
666
bool PU::addMergeHMVPCand(const CodingStructure &cs, MergeCtx& mrgCtx, MotionHist& hist, bool canFastExit, const int& mrgCandIdx, const uint32_t maxNumMergeCandMin1, int &cnt, const int prevCnt, bool isAvailableSubPu, unsigned subPuMvpPos, bool ibcFlag, bool isGt4x4, bool isInterB)
667
0
{
668
0
  bool hasPruned[MRG_MAX_NUM_CANDS];
669
0
  memset(hasPruned, 0, MRG_MAX_NUM_CANDS * sizeof(bool));
670
0
  if (isAvailableSubPu)
671
0
  {
672
0
    hasPruned[subPuMvpPos] = true;
673
0
  }
674
0
  auto &lut = ibcFlag ? hist.motionLutIbc : hist.motionLut;
675
0
  int num_avai_candInLUT = (int) lut.size();
676
677
0
  for (int mrgIdx = 1; mrgIdx <= num_avai_candInLUT; mrgIdx++)
678
0
  {
679
0
    const HPMVInfo &miNeighbor = lut[(num_avai_candInLUT - mrgIdx)];
680
0
    mrgCtx.interDirNeighbours[cnt] = miNeighbor.interDir();
681
0
    CHECK(mrgCtx.interDirNeighbours[cnt] == 0, "miNeighbor.interDir() shouldn't be zero");
682
0
    mrgCtx.mvFieldNeighbours[cnt << 1].setMvField(miNeighbor.mv[0], miNeighbor.mhRefIdx[0]);
683
0
    mrgCtx.useAltHpelIf[cnt] = !ibcFlag && miNeighbor.useAltHpelIf;
684
0
    if (isInterB)
685
0
    {
686
0
      mrgCtx.mvFieldNeighbours[(cnt << 1) + 1].setMvField(miNeighbor.mv[1], miNeighbor.mhRefIdx[1]);
687
0
    }
688
0
    if (mrgIdx > 2 || ((mrgIdx > 1 || !isGt4x4) && ibcFlag) || !xCheckSimilarMotion(cnt, prevCnt, mrgCtx, hasPruned))
689
0
    {
690
0
      mrgCtx.BcwIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? miNeighbor.BcwIdx : BCW_DEFAULT;
691
0
      if (mrgCandIdx == cnt && canFastExit)
692
0
      {
693
0
        return true;
694
0
      }
695
0
      cnt ++;
696
0
      if (cnt  == maxNumMergeCandMin1)
697
0
      {
698
0
        break;
699
0
      }
700
0
    }
701
0
  }
702
0
  if (cnt < maxNumMergeCandMin1)
703
0
  {
704
0
    mrgCtx.useAltHpelIf[cnt] = false;
705
0
  }
706
0
  return false;
707
0
}
708
709
void PU::getIBCMergeCandidates(const CodingUnit &cu, MergeCtx& mrgCtx, MotionHist& hist, const int& mrgCandIdx)
710
0
{
711
0
  const CodingStructure &cs = *cu.cs;
712
0
  const Slice &slice = *cu.slice;
713
0
  const uint32_t maxNumMergeCand = slice.getPicHeader()->getMaxNumIBCMergeCand();
714
0
  const bool canFastExit = true; // TODO: remove this
715
716
0
  for (uint32_t ui = 0; ui < maxNumMergeCand; ++ui)
717
0
  {
718
0
    mrgCtx.BcwIdx[ui] = BCW_DEFAULT;
719
0
    mrgCtx.interDirNeighbours[ui] = 0;
720
0
    mrgCtx.mrgTypeNeighbours[ui] = MRG_TYPE_IBC;
721
0
    mrgCtx.mvFieldNeighbours[ui * 2].mfRefIdx = MF_NOT_VALID;
722
0
    mrgCtx.mvFieldNeighbours[ui * 2 + 1].mfRefIdx = MF_NOT_VALID;
723
0
    mrgCtx.useAltHpelIf[ui] = false;
724
0
  }
725
726
0
  mrgCtx.numValidMergeCand = maxNumMergeCand;
727
  // compute the location of the current PU
728
729
0
  int cnt = 0;
730
731
0
  const Position posRT = cu.Y().topRight();
732
0
  const Position posLB = cu.Y().bottomLeft();
733
734
0
  MotionInfo miAbove, miLeft, miAboveLeft, miAboveRight, miBelowLeft;
735
736
  //left
737
0
  const CodingUnit* cuLeft = cs.getCURestricted(posLB.offset(-1, 0), cu, CH_L, cu.left);
738
0
  const bool isAvailableA1 = cuLeft && CU::isIBC(*cuLeft);
739
0
  bool isGt4x4 = cu.lwidth() * cu.lheight() > 16;
740
0
  if (isGt4x4 && isAvailableA1)
741
0
  {
742
0
    miLeft             = cuLeft->getMotionInfo(posLB.offset(-1, 0));
743
0
    miLeft.miRefIdx[0] = MI_NOT_VALID + 1;
744
745
    // get Inter Dir
746
0
    mrgCtx.interDirNeighbours[cnt] = miLeft.interDir();
747
    // get Mv from Left
748
0
    mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miLeft.mv[0], 0 );
749
0
    if (mrgCandIdx == cnt && canFastExit)
750
0
    {
751
0
      return;
752
0
    }
753
0
    cnt++;
754
0
  }
755
756
  // early termination
757
0
  if (cnt == maxNumMergeCand)
758
0
  {
759
0
    return;
760
0
  }
761
762
763
  // above
764
0
  const CodingUnit *cuAbove = cs.getCURestricted(posRT.offset(0, -1), cu, CH_L, cu.above);
765
0
  bool isAvailableB1 = cuAbove && CU::isIBC(*cuAbove);
766
0
  if (isGt4x4 && isAvailableB1)
767
0
  {
768
0
    miAbove             = cuAbove->getMotionInfo(posRT.offset(0, -1));
769
0
    miAbove.miRefIdx[0] = MI_NOT_VALID + 1;
770
771
0
    if (!isAvailableA1 || cuAbove->slice->getIndependentSliceIdx() != cuLeft->slice->getIndependentSliceIdx() || miAbove != miLeft)
772
0
    {
773
      // get Inter Dir
774
0
      mrgCtx.interDirNeighbours[cnt] = miAbove.interDir();
775
      // get Mv from Above
776
0
      mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miAbove.mv[0], 0 );
777
0
      if (mrgCandIdx == cnt && canFastExit)
778
0
      {
779
0
        return;
780
0
      }
781
782
0
      cnt++;
783
0
    }
784
0
  }
785
786
  // early termination
787
0
  if (cnt == maxNumMergeCand)
788
0
  {
789
0
    return;
790
0
  }
791
792
0
  int spatialCandPos = cnt;
793
794
795
0
  int maxNumMergeCandMin1 = maxNumMergeCand;
796
0
  if( cnt != maxNumMergeCandMin1 )
797
0
  {
798
0
    bool isAvailableSubPu = false;
799
0
    unsigned subPuMvpPos = 0;
800
801
0
    bool bFound = addMergeHMVPCand( cs, mrgCtx, hist, canFastExit, mrgCandIdx, maxNumMergeCandMin1, cnt, spatialCandPos, isAvailableSubPu, subPuMvpPos, true, isGt4x4, cu.slice->isInterB() );
802
0
    if( bFound )
803
0
    {
804
0
      return;
805
0
    }
806
0
  }
807
808
0
  while( cnt < maxNumMergeCand )
809
0
  {
810
0
    mrgCtx.mvFieldNeighbours [cnt * 2].setMvField( Mv( 0, 0 ), NOT_VALID );
811
0
    mrgCtx.interDirNeighbours[cnt] = 1;
812
0
    cnt++;
813
0
    if( mrgCandIdx == cnt && canFastExit )
814
0
    {
815
0
      return;
816
0
    }
817
0
  }
818
819
0
  mrgCtx.numValidMergeCand = cnt;
820
0
}
821
822
void PU::getInterMergeCandidates( const CodingUnit &cu, MergeCtx& mrgCtx, MotionHist& hist, const int& mrgCandIdx )
823
0
{
824
0
  const unsigned plevel      = cu.sps->getLog2ParallelMergeLevelMinus2() + 2;
825
0
  const CodingStructure &cs  = *cu.cs;
826
0
  const Slice &slice         = *cu.slice;
827
0
  const uint32_t maxNumMergeCand = cu.sps->getMaxNumMergeCand();// slice.getPicHeader()->getMaxNumMergeCand();
828
0
  const bool canFastExit = true; // TODO: remove this
829
830
0
  for (uint32_t ui = 0; ui < maxNumMergeCand; ++ui)
831
0
  {
832
0
    mrgCtx.BcwIdx[ui] = BCW_DEFAULT;
833
0
    mrgCtx.interDirNeighbours[ui] = 0;
834
0
    mrgCtx.mrgTypeNeighbours [ui] = MRG_TYPE_DEFAULT_N;
835
0
    mrgCtx.mvFieldNeighbours[(ui << 1)    ].mfRefIdx = MF_NOT_VALID;
836
0
    mrgCtx.mvFieldNeighbours[(ui << 1) + 1].mfRefIdx = MF_NOT_VALID;
837
0
    mrgCtx.useAltHpelIf[ui] = false;
838
0
  }
839
840
0
  mrgCtx.numValidMergeCand = maxNumMergeCand;
841
  // compute the location of the current PU
842
843
0
  int cnt = 0;
844
845
0
  const Position posLT = cu.Y().topLeft();
846
0
  const Position posRT = cu.Y().topRight();
847
0
  const Position posLB = cu.Y().bottomLeft();
848
0
  MotionInfo miAbove, miLeft, miAboveLeft, miAboveRight, miBelowLeft;
849
850
  // above
851
0
  const CodingUnit *cuAbove = cs.getCURestricted( posRT.offset( 0, -1 ), cu, CH_L, cu.above );
852
853
0
  bool isAvailableB1 = cuAbove && CU::isInter( *cuAbove ) && isDiffMER( cu.lumaPos(), posRT.offset( 0, -1 ), plevel );
854
855
0
  if( isAvailableB1 )
856
0
  {
857
0
    miAbove = cuAbove->getMotionInfo( posRT.offset( 0, -1 ) );
858
859
    // get Inter Dir
860
0
    mrgCtx.interDirNeighbours[cnt] = miAbove.interDir();
861
0
    mrgCtx.useAltHpelIf[cnt] = cuAbove->imv() == IMV_HPEL;
862
    // get Mv from Above
863
0
    mrgCtx.BcwIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? cuAbove->BcwIdx() : BCW_DEFAULT;
864
0
    mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miAbove.mv[0], miAbove.miRefIdx[0] );
865
866
0
    if( slice.isInterB() )
867
0
    {
868
0
      mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miAbove.mv[1], miAbove.miRefIdx[1] );
869
0
    }
870
0
    if (mrgCandIdx == cnt && canFastExit)
871
0
    {
872
0
      return;
873
0
    }
874
875
0
    cnt++;
876
0
  }
877
878
  // early termination
879
0
  if (cnt == maxNumMergeCand)
880
0
  {
881
0
    return;
882
0
  }
883
884
  //left
885
0
  const CodingUnit* cuLeft = cs.getCURestricted( posLB.offset( -1, 0 ), cu, CH_L, cu.left );
886
887
0
  const bool isAvailableA1 = cuLeft && CU::isInter( *cuLeft ) && isDiffMER( cu.lumaPos(), posLB.offset( -1, 0 ), plevel );
888
889
0
  if( isAvailableA1 )
890
0
  {
891
0
    miLeft = cuLeft->getMotionInfo( posLB.offset(-1, 0) );
892
893
0
    if (!isAvailableB1 || cuAbove->slice->getIndependentSliceIdx() != cuLeft->slice->getIndependentSliceIdx() || miAbove != miLeft)
894
0
    {
895
      // get Inter Dir
896
0
      mrgCtx.interDirNeighbours[cnt] = miLeft.interDir();
897
0
      mrgCtx.useAltHpelIf[cnt] = cuLeft->imv() == IMV_HPEL;
898
0
      mrgCtx.BcwIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? cuLeft->BcwIdx() : BCW_DEFAULT;
899
      // get Mv from Left
900
0
      mrgCtx.mvFieldNeighbours[cnt << 1].setMvField(miLeft.mv[0], miLeft.miRefIdx[0]);
901
902
0
      if (slice.isInterB())
903
0
      {
904
0
        mrgCtx.mvFieldNeighbours[(cnt << 1) + 1].setMvField(miLeft.mv[1], miLeft.miRefIdx[1]);
905
0
      }
906
0
      if (mrgCandIdx == cnt && canFastExit)
907
0
      {
908
0
        return;
909
0
      }
910
911
0
    cnt++;
912
0
  }
913
0
}
914
915
    // early termination
916
0
    if( cnt == maxNumMergeCand )
917
0
    {
918
0
      return;
919
0
    }
920
921
0
  int spatialCandPos = cnt;
922
923
  // above right
924
0
  const CodingUnit *cuAboveRight = cs.getCURestricted( posRT.offset( 1, -1 ), cu, CH_L, cuAbove );
925
926
0
  bool isAvailableB0 = cuAboveRight && CU::isInter( *cuAboveRight ) && isDiffMER( cu.lumaPos(), posRT.offset(1, -1), plevel);
927
928
0
  if( isAvailableB0 )
929
0
  {
930
0
    miAboveRight = cuAboveRight->getMotionInfo( posRT.offset( 1, -1 ) );
931
932
0
    if( !isAvailableB1 || cuAbove->slice->getIndependentSliceIdx() != cuAboveRight->slice->getIndependentSliceIdx() || miAbove != miAboveRight )
933
0
    {
934
935
      // get Inter Dir
936
0
      mrgCtx.interDirNeighbours[cnt] = miAboveRight.interDir();
937
0
      mrgCtx.useAltHpelIf[cnt] = cuAboveRight->imv() == IMV_HPEL;
938
      // get Mv from Above-right
939
0
      mrgCtx.BcwIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? cuAboveRight->BcwIdx() : BCW_DEFAULT;
940
0
      mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miAboveRight.mv[0], miAboveRight.miRefIdx[0] );
941
942
0
      if( slice.isInterB() )
943
0
      {
944
0
        mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miAboveRight.mv[1], miAboveRight.miRefIdx[1] );
945
0
      }
946
947
0
      if (mrgCandIdx == cnt && canFastExit)
948
0
      {
949
0
        return;
950
0
      }
951
952
0
      cnt++;
953
0
    }
954
955
    // early termination
956
0
    if( cnt == maxNumMergeCand )
957
0
    {
958
0
      return;
959
0
    }
960
0
  }
961
962
  //left bottom
963
0
  const CodingUnit *cuLeftBottom = cs.getCURestricted( posLB.offset( -1, 1 ), cu, CH_L, cuLeft );
964
965
0
  bool isAvailableA0 = cuLeftBottom && CU::isInter( *cuLeftBottom ) && isDiffMER( cu.lumaPos(), posLB.offset(-1, 1), plevel);
966
967
0
  if( isAvailableA0 )
968
0
  {
969
0
    miBelowLeft = cuLeftBottom->getMotionInfo( posLB.offset( -1, 1 ) );
970
971
0
    if( !isAvailableA1 || cuLeftBottom->slice->getIndependentSliceIdx() != cuLeft->slice->getIndependentSliceIdx() || miBelowLeft != miLeft )
972
0
    {
973
974
      // get Inter Dir
975
0
      mrgCtx.interDirNeighbours[cnt] = miBelowLeft.interDir();
976
0
      mrgCtx.useAltHpelIf[cnt] = cuLeftBottom->imv() == IMV_HPEL;
977
0
      mrgCtx.BcwIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? cuLeftBottom->BcwIdx() : BCW_DEFAULT;
978
      // get Mv from Bottom-Left
979
0
      mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miBelowLeft.mv[0], miBelowLeft.miRefIdx[0] );
980
981
0
      if( slice.isInterB() )
982
0
      {
983
0
        mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miBelowLeft.mv[1], miBelowLeft.miRefIdx[1] );
984
0
      }
985
986
0
      if (mrgCandIdx == cnt && canFastExit)
987
0
      {
988
0
        return;
989
0
      }
990
991
0
      cnt++;
992
0
    }
993
994
    // early termination
995
0
    if( cnt == maxNumMergeCand )
996
0
    {
997
0
      return;
998
0
    }
999
0
  }
1000
1001
  // above left
1002
0
  if ( cnt < 4 )
1003
0
  {
1004
0
    const CodingUnit *cuAboveLeft = cs.getCURestricted( posLT.offset( -1, -1 ), cu, CH_L, cu.left ? cu.left : cu.above );
1005
1006
0
    bool isAvailableB2 = cuAboveLeft && CU::isInter( *cuAboveLeft ) && isDiffMER( cu.lumaPos(), posLT.offset(-1, -1), plevel );
1007
1008
0
    if( isAvailableB2 )
1009
0
    {
1010
0
      miAboveLeft = cuAboveLeft->getMotionInfo( posLT.offset( -1, -1 ) );
1011
1012
0
      if( ( !isAvailableA1 || cuLeft->slice->getIndependentSliceIdx() != cuAboveLeft->slice->getIndependentSliceIdx() || miLeft != miAboveLeft ) && ( !isAvailableB1 || cuAbove->slice->getIndependentSliceIdx() != cuAboveLeft->slice->getIndependentSliceIdx() || miAbove != miAboveLeft ) )
1013
0
      {
1014
1015
        // get Inter Dir
1016
0
        mrgCtx.interDirNeighbours[cnt] = miAboveLeft.interDir();
1017
0
        mrgCtx.useAltHpelIf[cnt] = cuAboveLeft->imv() == IMV_HPEL;
1018
0
        mrgCtx.BcwIdx[cnt] = (mrgCtx.interDirNeighbours[cnt] == 3) ? cuAboveLeft->BcwIdx() : BCW_DEFAULT;
1019
        // get Mv from Above-Left
1020
0
        mrgCtx.mvFieldNeighbours[cnt << 1].setMvField( miAboveLeft.mv[0], miAboveLeft.miRefIdx[0] );
1021
1022
0
        if( slice.isInterB() )
1023
0
        {
1024
0
          mrgCtx.mvFieldNeighbours[( cnt << 1 ) + 1].setMvField( miAboveLeft.mv[1], miAboveLeft.miRefIdx[1] );
1025
0
        }
1026
1027
0
        if (mrgCandIdx == cnt && canFastExit)
1028
0
        {
1029
0
          return;
1030
0
        }
1031
1032
0
        cnt++;
1033
0
      }
1034
0
    }
1035
1036
    // early termination
1037
0
    if( cnt == maxNumMergeCand )
1038
0
    {
1039
0
      return;
1040
0
    }
1041
0
  }
1042
1043
0
  if( slice.getPicHeader()->getEnableTMVPFlag() && (cu.lumaSize().width + cu.lumaSize().height > 12) )
1044
0
  {
1045
    //>> MTK colocated-RightBottom
1046
    // offset the pos to be sure to "point" to the same position the uiAbsPartIdx would've pointed to
1047
1048
//    Position posRB = cu.shareParentArea.topLeft().offset(cu.shareParentArea.width-3, cu.shareParentArea.height - 3);
1049
0
    Position posRB = cu.Y().bottomRight().offset( -3, -3 );
1050
0
    const PreCalcValues& pcv = *cs.pcv;
1051
1052
0
    Position posC0;
1053
0
    Position posC1 = cu.Y().center();
1054
0
    bool C0Avail = false;
1055
1056
0
    bool boundaryCond = ((posRB.x + pcv.minCUWidth) < pcv.lumaWidth) && ((posRB.y + pcv.minCUHeight) < pcv.lumaHeight);
1057
0
    const SubPic& curSubPic = cu.pps->getSubPicFromPos( cu.lumaPos() );
1058
0
    if (curSubPic.getTreatedAsPicFlag())
1059
0
    {
1060
0
      boundaryCond = ((posRB.x + pcv.minCUWidth) <= curSubPic.getSubPicRight() &&
1061
0
                      (posRB.y + pcv.minCUHeight) <= curSubPic.getSubPicBottom());
1062
0
    }
1063
0
    if (boundaryCond)
1064
0
    {
1065
0
      {
1066
0
        Position posInCtu( posRB.x & pcv.maxCUWidthMask, posRB.y & pcv.maxCUHeightMask );
1067
1068
0
        if( ( posInCtu.x + 4 < pcv.maxCUWidth ) &&           // is not at the last column of CTU
1069
0
            ( posInCtu.y + 4 < pcv.maxCUHeight ) )           // is not at the last row    of CTU
1070
0
        {
1071
0
          posC0 = posRB.offset( 4, 4 );
1072
0
          C0Avail = true;
1073
0
        }
1074
0
        else if( posInCtu.x + 4 < pcv.maxCUWidth )           // is not at the last column of CTU But is last row of CTU
1075
0
        {
1076
0
          posC0 = posRB.offset( 4, 4 );
1077
          // in the reference the CTU address is not set - thus probably resulting in no using this C0 possibility
1078
0
        }
1079
0
        else if( posInCtu.y + 4 < pcv.maxCUHeight )          // is not at the last row of CTU But is last column of CTU
1080
0
        {
1081
0
          posC0 = posRB.offset( 4, 4 );
1082
0
          C0Avail = true;
1083
0
        }
1084
0
        else //is the right bottom corner of CTU
1085
0
        {
1086
0
          posC0 = posRB.offset( 4, 4 );
1087
          // same as for last column but not last row
1088
0
        }
1089
0
      }
1090
0
    }
1091
1092
0
    Mv        cColMv;
1093
0
    int       iRefIdx     = 0;
1094
0
    int       dir         = 0;
1095
0
    unsigned  uiArrayAddr = cnt;
1096
0
    bool      bExistMV    = ( C0Avail && getColocatedMVP( cu, REF_PIC_LIST_0, posC0, cColMv, iRefIdx, false ) )
1097
0
                                      || getColocatedMVP( cu, REF_PIC_LIST_0, posC1, cColMv, iRefIdx, false );
1098
0
    if (bExistMV)
1099
0
    {
1100
0
      dir     |= 1;
1101
0
      mrgCtx.mvFieldNeighbours[2 * uiArrayAddr].setMvField(cColMv, iRefIdx);
1102
0
    }
1103
1104
0
    if (slice.isInterB())
1105
0
    {
1106
0
      bExistMV = ( C0Avail && getColocatedMVP( cu, REF_PIC_LIST_1, posC0, cColMv, iRefIdx, false ) )
1107
0
                           || getColocatedMVP( cu, REF_PIC_LIST_1, posC1, cColMv, iRefIdx, false );
1108
0
      if (bExistMV)
1109
0
      {
1110
0
        dir     |= 2;
1111
0
        mrgCtx.mvFieldNeighbours[2 * uiArrayAddr + 1].setMvField(cColMv, iRefIdx);
1112
0
      }
1113
0
    }
1114
1115
0
    if( dir != 0 )
1116
0
    {
1117
0
      bool addTMvp = true;
1118
0
      if( addTMvp )
1119
0
      {
1120
0
        mrgCtx.interDirNeighbours[uiArrayAddr] = dir;
1121
0
        mrgCtx.BcwIdx[uiArrayAddr] = BCW_DEFAULT;
1122
0
        mrgCtx.useAltHpelIf[uiArrayAddr] = false;
1123
0
        if (mrgCandIdx == cnt && canFastExit)
1124
0
        {
1125
0
          return;
1126
0
        }
1127
1128
0
        cnt++;
1129
0
      }
1130
0
    }
1131
1132
    // early termination
1133
0
    if( cnt == maxNumMergeCand )
1134
0
    {
1135
0
      return;
1136
0
    }
1137
0
  }
1138
1139
0
  int maxNumMergeCandMin1 = maxNumMergeCand - 1;
1140
0
  if( cnt != maxNumMergeCandMin1 )
1141
0
  {
1142
0
    bool isAvailableSubPu = false;
1143
0
    unsigned subPuMvpPos = 0;
1144
0
    bool bFound = addMergeHMVPCand( cs, mrgCtx, hist, canFastExit, mrgCandIdx, maxNumMergeCandMin1, cnt, spatialCandPos, isAvailableSubPu, subPuMvpPos, CU::isIBC( cu ), true, cu.slice->isInterB() );
1145
1146
0
    if( bFound )
1147
0
    {
1148
0
      return;
1149
0
    }
1150
0
  }
1151
1152
  // pairwise-average candidates
1153
0
  {
1154
0
    if (cnt > 1 && cnt < maxNumMergeCand)
1155
0
    {
1156
1157
0
      mrgCtx.mvFieldNeighbours[cnt * 2    ].setMvField( Mv( 0, 0 ), MF_NOT_VALID );
1158
0
      mrgCtx.mvFieldNeighbours[cnt * 2 + 1].setMvField( Mv( 0, 0 ), MF_NOT_VALID );
1159
      // calculate average MV for L0 and L1 seperately
1160
0
      unsigned char interDir = 0;
1161
1162
1163
0
      mrgCtx.useAltHpelIf[cnt] = (mrgCtx.useAltHpelIf[0] == mrgCtx.useAltHpelIf[1]) ? mrgCtx.useAltHpelIf[0] : false;
1164
0
      for( int refListId = 0; refListId < (slice.isInterB() ? 2 : 1); refListId++ )
1165
0
      {
1166
0
        const short refIdxI = mrgCtx.mvFieldNeighbours[0 * 2 + refListId].mfRefIdx;
1167
0
        const short refIdxJ = mrgCtx.mvFieldNeighbours[1 * 2 + refListId].mfRefIdx;
1168
1169
        // both MVs are invalid, skip
1170
0
        if( (refIdxI == NOT_VALID) && (refIdxJ == NOT_VALID) )
1171
0
        {
1172
0
          continue;
1173
0
        }
1174
1175
0
        interDir += 1 << refListId;
1176
        // both MVs are valid, average these two MVs
1177
0
        if( (refIdxI != NOT_VALID) && (refIdxJ != NOT_VALID) )
1178
0
        {
1179
0
          const Mv& MvI = mrgCtx.mvFieldNeighbours[0 * 2 + refListId].mv;
1180
0
          const Mv& MvJ = mrgCtx.mvFieldNeighbours[1 * 2 + refListId].mv;
1181
1182
          // average two MVs
1183
0
          Mv avgMv = MvI;
1184
0
          avgMv += MvJ;
1185
0
          roundAffineMv(avgMv.hor, avgMv.ver, 1);
1186
1187
1188
1189
0
          mrgCtx.mvFieldNeighbours[cnt * 2 + refListId].setMvField( avgMv, refIdxI );
1190
0
        }
1191
        // only one MV is valid, take the only one MV
1192
0
        else if( refIdxI != NOT_VALID )
1193
0
        {
1194
0
          Mv singleMv = mrgCtx.mvFieldNeighbours[0 * 2 + refListId].mv;
1195
0
          mrgCtx.mvFieldNeighbours[cnt * 2 + refListId].setMvField( singleMv, refIdxI );
1196
0
        }
1197
0
        else if( refIdxJ != NOT_VALID )
1198
0
        {
1199
0
          Mv singleMv = mrgCtx.mvFieldNeighbours[1 * 2 + refListId].mv;
1200
0
          mrgCtx.mvFieldNeighbours[cnt * 2 + refListId].setMvField( singleMv, refIdxJ );
1201
0
        }
1202
0
      }
1203
1204
0
      mrgCtx.interDirNeighbours[cnt] = interDir;
1205
0
      if( interDir > 0 )
1206
0
      {
1207
0
        cnt++;
1208
0
      }
1209
0
    }
1210
1211
    // early termination
1212
0
    if( cnt == maxNumMergeCand )
1213
0
    {
1214
0
      return;
1215
0
    }
1216
0
  }
1217
1218
0
  int iArrayAddr = cnt;
1219
1220
0
  int iNumRefIdx = slice.isInterB() ? std::min(slice.getNumRefIdx(REF_PIC_LIST_0), slice.getNumRefIdx(REF_PIC_LIST_1)) : slice.getNumRefIdx(REF_PIC_LIST_0);
1221
1222
0
  int r = 0;
1223
0
  int refcnt = 0;
1224
  // second condition needed for gcc-10 overflow checking. Required for now. TODO: fix properly
1225
0
  while (iArrayAddr < maxNumMergeCand && iArrayAddr < MRG_MAX_NUM_CANDS)
1226
0
  {
1227
0
    mrgCtx.interDirNeighbours [iArrayAddr     ] = 1;
1228
0
    mrgCtx.BcwIdx             [iArrayAddr     ] = BCW_DEFAULT;
1229
0
    mrgCtx.mvFieldNeighbours  [iArrayAddr << 1] . setMvField(Mv(0, 0), r);
1230
0
    mrgCtx.useAltHpelIf       [iArrayAddr     ] = false;
1231
1232
0
    if (slice.isInterB())
1233
0
    {
1234
0
      mrgCtx.interDirNeighbours [ iArrayAddr          ] = 3;
1235
0
      mrgCtx.mvFieldNeighbours  [(iArrayAddr << 1) + 1].setMvField(Mv(0, 0), r);
1236
0
    }
1237
1238
0
    if ( mrgCtx.interDirNeighbours[iArrayAddr] == 1 && cu.slice->getRefPOC(REF_PIC_LIST_0, mrgCtx.mvFieldNeighbours[iArrayAddr << 1].mfRefIdx) == cu.slice->getPOC())
1239
0
    {
1240
0
      mrgCtx.mrgTypeNeighbours[iArrayAddr] = MRG_TYPE_IBC;
1241
0
    }
1242
1243
0
    iArrayAddr++;
1244
1245
0
    if (refcnt == iNumRefIdx - 1)
1246
0
    {
1247
0
      r = 0;
1248
0
    }
1249
0
    else
1250
0
    {
1251
0
      ++r;
1252
0
      ++refcnt;
1253
0
    }
1254
0
  }
1255
0
  mrgCtx.numValidMergeCand = iArrayAddr;
1256
0
}
1257
1258
bool PU::checkDMVRCondition( const CodingUnit& cu )
1259
0
{
1260
0
  const WPScalingParam* wp0 = nullptr;
1261
0
  const WPScalingParam* wp1 = nullptr;
1262
0
  int refIdx0 = cu.refIdx[REF_PIC_LIST_0];
1263
0
  int refIdx1 = cu.refIdx[REF_PIC_LIST_1];
1264
0
  cu.slice->getWpScaling( REF_PIC_LIST_0, refIdx0, wp0 );
1265
0
  cu.slice->getWpScaling( REF_PIC_LIST_1, refIdx1, wp1 );
1266
1267
0
  if( cu.sps->getUseDMVR() && !cu.cs->picHeader->getDisDmvrFlag() )
1268
0
  {
1269
0
    return cu.mergeFlag()
1270
0
        && cu.mergeType() == MRG_TYPE_DEFAULT_N
1271
0
        && !cu.ciipFlag()
1272
0
        && !cu.affineFlag()
1273
0
        && !cu.mmvdFlag()
1274
0
        && PU::isBiPredFromDifferentDirEqDistPoc( cu )
1275
0
        && cu.lheight()  >=   8
1276
0
        && cu.lwidth()   >=   8
1277
0
        && cu.Y().area() >= 128
1278
0
        && cu.BcwIdx() == BCW_DEFAULT
1279
0
        && !wp0[COMPONENT_Y].bPresentFlag
1280
0
        && !wp1[COMPONENT_Y].bPresentFlag
1281
0
        && !wp0[COMPONENT_Cb].bPresentFlag
1282
0
        && !wp0[COMPONENT_Cr].bPresentFlag
1283
0
        && !wp1[COMPONENT_Cb].bPresentFlag
1284
0
        && !wp1[COMPONENT_Cr].bPresentFlag
1285
0
        && PU::isRefPicSameSize( cu )
1286
0
        ;
1287
0
  }
1288
0
  else
1289
0
  {
1290
0
    return false;
1291
0
  }
1292
0
}
1293
1294
static int xGetDistScaleFactor(const int &iCurrPOC, const int &iCurrRefPOC, const int &iColPOC, const int &iColRefPOC)
1295
0
{
1296
0
  const int iDiffPocD = iColPOC  - iColRefPOC;
1297
0
  const int iDiffPocB = iCurrPOC - iCurrRefPOC;
1298
1299
0
  if (iDiffPocD == iDiffPocB)
1300
0
  {
1301
0
    return 4096;
1302
0
  }
1303
0
  else
1304
0
  {
1305
0
    const int iTDB    = Clip3( -128, 127, iDiffPocB );
1306
0
    const int iTDD    = Clip3( -128, 127, iDiffPocD );
1307
0
    const int iX      = ( 0x4000 + abs( iTDD / 2 ) ) / iTDD;
1308
0
    const int iScale  = Clip3( -4096, 4095, ( iTDB * iX + 32 ) >> 6 );
1309
0
    return iScale;
1310
0
  }
1311
0
}
1312
1313
static int convertMvFixedToFloat(int32_t val)
1314
0
{
1315
0
  int sign  = val >> 31;
1316
0
  int scale = getLog2((val ^ sign) | MV_MANTISSA_UPPER_LIMIT) - (MV_MANTISSA_BITCOUNT - 1);
1317
1318
0
  int exponent;
1319
0
  int mantissa;
1320
0
  if (scale >= 0)
1321
0
  {
1322
0
    int round = (1 << scale) >> 1;
1323
0
    int n     = (val + round) >> scale;
1324
0
    exponent  = scale + ((n ^ sign) >> (MV_MANTISSA_BITCOUNT - 1));
1325
0
    mantissa  = (n & MV_MANTISSA_UPPER_LIMIT) | (sign *(1<< (MV_MANTISSA_BITCOUNT - 1)));
1326
0
  }
1327
0
  else
1328
0
  {
1329
0
    exponent = 0;
1330
0
    mantissa = val;
1331
0
  }
1332
1333
0
  return exponent | (mantissa *(1<< MV_EXPONENT_BITCOUNT));
1334
0
}
1335
1336
static inline int convertMvFloatToFixed(int val)
1337
0
{
1338
0
  int exponent = val & MV_EXPONENT_MASK;
1339
0
  int mantissa = val >> MV_EXPONENT_BITCOUNT;
1340
0
  return exponent == 0 ? mantissa : (mantissa ^ MV_MANTISSA_LIMIT) *(1<< (exponent - 1));
1341
0
}
1342
1343
static inline int roundMvComp(int x)
1344
0
{
1345
0
  return convertMvFloatToFixed(convertMvFixedToFloat(x));
1346
0
}
1347
1348
int PU::getDistScaleFactor(const int &currPOC, const int &currRefPOC, const int &colPOC, const int &colRefPOC)
1349
0
{
1350
0
  return xGetDistScaleFactor(currPOC, currRefPOC, colPOC, colRefPOC);
1351
0
}
1352
1353
void PU::getInterMMVDMergeCandidates(const CodingUnit &cu, MergeCtx& mrgCtx, const int& mrgCandIdx)
1354
0
{
1355
0
  int refIdxList0, refIdxList1;
1356
0
  int k;
1357
0
  int currBaseNum = 0;
1358
0
  const uint16_t maxNumMergeCand = mrgCtx.numValidMergeCand;
1359
1360
0
  for (k = 0; k < maxNumMergeCand; k++)
1361
0
  {
1362
0
    if (mrgCtx.mrgTypeNeighbours[k] == MRG_TYPE_DEFAULT_N)
1363
0
    {
1364
0
      refIdxList0 = mrgCtx.mvFieldNeighbours[(k << 1)].mfRefIdx;
1365
0
      refIdxList1 = mrgCtx.mvFieldNeighbours[(k << 1) + 1].mfRefIdx;
1366
1367
0
      if ((refIdxList0 >= 0) && (refIdxList1 >= 0))
1368
0
      {
1369
0
        mrgCtx.mmvdBaseMv[currBaseNum][0] = mrgCtx.mvFieldNeighbours[(k << 1)];
1370
0
        mrgCtx.mmvdBaseMv[currBaseNum][1] = mrgCtx.mvFieldNeighbours[(k << 1) + 1];
1371
0
      }
1372
0
      else if (refIdxList0 >= 0)
1373
0
      {
1374
0
        mrgCtx.mmvdBaseMv[currBaseNum][0] = mrgCtx.mvFieldNeighbours[(k << 1)];
1375
0
        mrgCtx.mmvdBaseMv[currBaseNum][1] = MvField(Mv(0, 0), -1);
1376
0
      }
1377
0
      else if (refIdxList1 >= 0)
1378
0
      {
1379
0
        mrgCtx.mmvdBaseMv[currBaseNum][0] = MvField(Mv(0, 0), -1);
1380
0
        mrgCtx.mmvdBaseMv[currBaseNum][1] = mrgCtx.mvFieldNeighbours[(k << 1) + 1];
1381
0
      }
1382
0
      mrgCtx.mmvdUseAltHpelIf[currBaseNum] = mrgCtx.useAltHpelIf[k];
1383
1384
0
      currBaseNum++;
1385
1386
0
      if (currBaseNum == MMVD_BASE_MV_NUM)
1387
0
        break;
1388
0
    }
1389
0
  }
1390
0
}
1391
bool PU::getColocatedMVP(const CodingUnit &cu, const RefPicList &eRefPicList, const Position &pos, Mv& rcMv, const int refIdx, bool sbFlag )
1392
0
{
1393
0
  if( CU::isIBC( cu ) )
1394
0
  {
1395
0
    return false;
1396
0
  }
1397
1398
0
  const Slice &slice = *cu.slice;
1399
1400
  // use coldir.
1401
0
  const Picture* const pColPic = slice.getRefPic(RefPicList(slice.isInterB() ? 1 - slice.getColFromL0Flag() : 0), slice.getColRefIdx());
1402
1403
0
  if( !pColPic )
1404
0
  {
1405
0
    return false;
1406
0
  }
1407
1408
  // Check the position of colocated block is within a subpicture
1409
0
  const SubPic& curSubPic = cu.pps->getSubPicFromPos( cu.lumaPos() );
1410
0
  if( curSubPic.getTreatedAsPicFlag() )
1411
0
  {
1412
0
    if (!curSubPic.isContainingPos(pos))
1413
0
      return false;
1414
0
  }
1415
0
  RefPicList eColRefPicList = slice.getCheckLDC() ? eRefPicList : RefPicList(slice.getColFromL0Flag());
1416
1417
0
  const Slice* pColSlice;
1418
0
  const ColocatedMotionInfo&
1419
0
               mi = pColPic->cs->getColInfo( pos, pColSlice );
1420
1421
0
  if( !mi.isInter() )
1422
0
  {
1423
0
    return false;
1424
0
  }
1425
1426
0
  int iColRefIdx = mi.coRefIdx[eColRefPicList];
1427
1428
0
  if( sbFlag && !slice.getCheckLDC() )
1429
0
  {
1430
0
    eColRefPicList = eRefPicList;
1431
0
    iColRefIdx     = mi.coRefIdx[eColRefPicList];
1432
0
    if (iColRefIdx < 0)
1433
0
    {
1434
0
      return false;
1435
0
    }
1436
0
  }
1437
0
  else
1438
0
  {
1439
0
    if( iColRefIdx < 0 )
1440
0
    {
1441
0
      eColRefPicList = RefPicList( 1 - eColRefPicList );
1442
0
      iColRefIdx     = mi.coRefIdx[eColRefPicList];
1443
1444
0
      if( iColRefIdx < 0 )
1445
0
      {
1446
0
        return false;
1447
0
      }
1448
0
    }
1449
0
  }
1450
1451
0
  CHECK( pColSlice == nullptr, "Slice segment not found" );
1452
1453
0
  const Slice &colSlice = *pColSlice;
1454
1455
0
  const bool bIsCurrRefLongTerm = slice.getIsUsedAsLongTerm(eRefPicList, refIdx);
1456
0
  const bool bIsColRefLongTerm  = colSlice.getIsUsedAsLongTerm(eColRefPicList, iColRefIdx);
1457
1458
0
  if (bIsCurrRefLongTerm != bIsColRefLongTerm)
1459
0
  {
1460
0
    return false;
1461
0
  }
1462
1463
1464
  // Scale the vector.
1465
0
  Mv cColMv = mi.mv[eColRefPicList];
1466
0
  cColMv.setHor(roundMvComp(cColMv.getHor()));
1467
0
  cColMv.setVer(roundMvComp(cColMv.getVer()));
1468
1469
0
  if (bIsCurrRefLongTerm /*|| bIsColRefLongTerm*/)
1470
0
  {
1471
0
    rcMv = cColMv;
1472
0
    rcMv.clipToStorageBitDepth();
1473
0
  }
1474
0
  else
1475
0
  {
1476
0
    const int currPOC    = slice.getPOC();
1477
0
    const int colPOC     = colSlice.getPOC();
1478
0
    const int colRefPOC  = colSlice.getRefPOC(eColRefPicList, iColRefIdx);
1479
0
    const int currRefPOC = slice.getRefPOC(eRefPicList, refIdx);
1480
0
    const int distscale  = xGetDistScaleFactor(currPOC, currRefPOC, colPOC, colRefPOC);
1481
1482
0
    if (distscale == 4096)
1483
0
    {
1484
0
      rcMv = cColMv;
1485
0
      rcMv.clipToStorageBitDepth();
1486
0
    }
1487
0
    else
1488
0
    {
1489
0
      rcMv = cColMv.scaleMv(distscale);
1490
0
    }
1491
0
  }
1492
1493
0
  return true;
1494
0
}
1495
1496
bool PU::isDiffMER( const Position& pos1, const Position& pos2, const unsigned plevel )
1497
0
{
1498
0
  const unsigned xN = pos1.x;
1499
0
  const unsigned yN = pos1.y;
1500
0
  const unsigned xP = pos2.x;
1501
0
  const unsigned yP = pos2.y;
1502
1503
0
  if( ( xN >> plevel ) != ( xP >> plevel ) )
1504
0
  {
1505
0
    return true;
1506
0
  }
1507
1508
0
  if( ( yN >> plevel ) != ( yP >> plevel ) )
1509
0
  {
1510
0
    return true;
1511
0
  }
1512
0
  return false;
1513
0
}
1514
1515
/**
1516
 * Constructs a list of candidates for IBC AMVP (See specification, section "Derivation process for motion vector predictor candidates")
1517
 */
1518
void PU::fillIBCMvpCand( CodingUnit &cu, AMVPInfo &amvpInfo, MotionHist& hist )
1519
0
{
1520
1521
0
  AMVPInfo *pInfo = &amvpInfo;
1522
1523
0
  pInfo->numCand = 0;
1524
1525
0
  MergeCtx mergeCtx;
1526
0
  PU::getIBCMergeCandidates(cu, mergeCtx, hist, AMVP_MAX_NUM_CANDS - 1);
1527
0
  int candIdx = 0;
1528
0
  while (pInfo->numCand < AMVP_MAX_NUM_CANDS)
1529
0
  {
1530
0
    pInfo->mvCand[pInfo->numCand] = mergeCtx.mvFieldNeighbours[(candIdx << 1) + 0].mv;;
1531
0
    pInfo->numCand++;
1532
0
    candIdx++;
1533
0
  }
1534
1535
0
  for (Mv &mv : pInfo->mvCand)
1536
0
  {
1537
0
    mv.roundToPrecision( MV_PRECISION_INTERNAL, cu.imv() == 2 ? MV_PRECISION_4PEL : MV_PRECISION_INT );
1538
0
  }
1539
0
}
1540
1541
/** Constructs a list of candidates for AMVP (See specification, section "Derivation process for motion vector predictor candidates")
1542
* \param uiPartIdx
1543
* \param uiPartAddr
1544
* \param eRefPicList
1545
* \param iRefIdx
1546
* \param pInfo
1547
*/
1548
void PU::fillMvpCand(CodingUnit &cu, const RefPicList &eRefPicList, const int &refIdx, AMVPInfo &amvpInfo, MotionHist& hist)
1549
0
{
1550
0
  const CodingStructure &cs = *cu.cs;
1551
0
  AMVPInfo *pInfo = &amvpInfo;
1552
1553
0
  pInfo->numCand = 0;
1554
1555
0
  if (refIdx < 0)
1556
0
  {
1557
0
    return;
1558
0
  }
1559
1560
  //-- Get Spatial MV
1561
0
  const Position posLT = cu.Y().topLeft();
1562
0
  const Position posRT = cu.Y().topRight();
1563
0
  const Position posLB = cu.Y().bottomLeft();
1564
1565
0
  bool bAdded = addMVPCandUnscaled( cu, eRefPicList, refIdx, posLB, MD_BELOW_LEFT, *pInfo );
1566
1567
0
  if( !bAdded )
1568
0
  {
1569
0
    bAdded = addMVPCandUnscaled( cu, eRefPicList, refIdx, posLB, MD_LEFT, *pInfo );
1570
0
  }
1571
1572
  // Above predictor search
1573
0
  {
1574
0
    bool bAdded = addMVPCandUnscaled( cu, eRefPicList, refIdx, posRT, MD_ABOVE_RIGHT, *pInfo );
1575
1576
0
    if( !bAdded )
1577
0
    {
1578
0
      bAdded = addMVPCandUnscaled( cu, eRefPicList, refIdx, posRT, MD_ABOVE, *pInfo );
1579
1580
0
      if( !bAdded )
1581
0
      {
1582
0
        addMVPCandUnscaled( cu, eRefPicList, refIdx, posLT, MD_ABOVE_LEFT, *pInfo );
1583
0
      }
1584
0
    }
1585
0
  }
1586
1587
1588
0
  for( int i = 0; i < pInfo->numCand; i++ )
1589
0
  {
1590
0
    pInfo->mvCand[i].roundToAmvrSignalPrecision( MV_PRECISION_INTERNAL, cu.imv() );
1591
0
  }
1592
1593
0
  if( pInfo->numCand == 2 )
1594
0
  {
1595
0
    if( pInfo->mvCand[0] == pInfo->mvCand[1] )
1596
0
    {
1597
0
      pInfo->numCand = 1;
1598
0
    }
1599
0
  }
1600
1601
0
  if( cs.picHeader->getEnableTMVPFlag() && pInfo->numCand < AMVP_MAX_NUM_CANDS && ( cu.lumaSize().width + cu.lumaSize().height > 12 ) )
1602
0
  {
1603
    // Get Temporal Motion Predictor
1604
0
    const int refIdx_Col = refIdx;
1605
1606
0
    Position posRB = cu.Y().bottomRight().offset(-3, -3);
1607
1608
0
    const PreCalcValues& pcv = *cs.pcv;
1609
1610
0
    Position posC0;
1611
0
    bool C0Avail = false;
1612
0
    Position posC1 = cu.Y().center();
1613
0
    Mv cColMv;
1614
1615
0
    bool boundaryCond = ((posRB.x + pcv.minCUWidth) < pcv.lumaWidth) && ((posRB.y + pcv.minCUHeight) < pcv.lumaHeight);
1616
0
    const SubPic& curSubPic = cu.pps->getSubPicFromPos( cu.lumaPos() );
1617
0
    if( curSubPic.getTreatedAsPicFlag() )
1618
0
    {
1619
0
      boundaryCond = ((posRB.x + pcv.minCUWidth) <= curSubPic.getSubPicRight() &&
1620
0
                      (posRB.y + pcv.minCUHeight) <= curSubPic.getSubPicBottom());
1621
0
    }
1622
0
    if( boundaryCond )
1623
0
    {
1624
0
      Position posInCtu( posRB.x & pcv.maxCUWidthMask, posRB.y & pcv.maxCUHeightMask );
1625
1626
0
      if ((posInCtu.x + 4 < pcv.maxCUWidth) &&           // is not at the last column of CTU
1627
0
          (posInCtu.y + 4 < pcv.maxCUHeight))             // is not at the last row    of CTU
1628
0
      {
1629
0
        posC0 = posRB.offset(4, 4);
1630
0
        C0Avail = true;
1631
0
      }
1632
0
      else if (posInCtu.x + 4 < pcv.maxCUWidth)           // is not at the last column of CTU But is last row of CTU
1633
0
      {
1634
        // in the reference the CTU address is not set - thus probably resulting in no using this C0 possibility
1635
0
        posC0 = posRB.offset(4, 4);
1636
0
      }
1637
0
      else if (posInCtu.y + 4 < pcv.maxCUHeight)          // is not at the last row of CTU But is last column of CTU
1638
0
      {
1639
0
        posC0 = posRB.offset(4, 4);
1640
0
        C0Avail = true;
1641
0
      }
1642
0
      else //is the right bottom corner of CTU
1643
0
      {
1644
        // same as for last column but not last row
1645
0
        posC0 = posRB.offset(4, 4);
1646
0
      }
1647
0
    }
1648
1649
0
    if ( ( C0Avail && getColocatedMVP( cu, eRefPicList, posC0, cColMv, refIdx_Col, false ) ) || getColocatedMVP( cu, eRefPicList, posC1, cColMv, refIdx_Col, false ) )
1650
0
    {
1651
0
      cColMv.roundToAmvrSignalPrecision( MV_PRECISION_INTERNAL, cu.imv() );
1652
0
      pInfo->mvCand[pInfo->numCand++] = cColMv;
1653
0
    }
1654
0
  }
1655
0
  if (pInfo->numCand < AMVP_MAX_NUM_CANDS)
1656
0
  {
1657
0
    const int        currRefPOC = cu.slice->getRefPOC(eRefPicList, refIdx);
1658
0
    const RefPicList eRefPicList2nd = (eRefPicList == REF_PIC_LIST_0) ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1659
0
    addAMVPHMVPCand( cu, hist, eRefPicList, eRefPicList2nd, currRefPOC, *pInfo, cu.imv() );
1660
0
  }
1661
0
  if (pInfo->numCand > AMVP_MAX_NUM_CANDS)
1662
0
  {
1663
0
    pInfo->numCand = AMVP_MAX_NUM_CANDS;
1664
0
  }
1665
1666
0
  while (pInfo->numCand < AMVP_MAX_NUM_CANDS)
1667
0
  {
1668
0
    pInfo->mvCand[pInfo->numCand] = Mv( 0, 0 );
1669
0
    pInfo->numCand++;
1670
0
  }
1671
0
  for (Mv &mv : pInfo->mvCand)
1672
0
  {
1673
0
    mv.roundToAmvrSignalPrecision( MV_PRECISION_INTERNAL, cu.imv() );
1674
0
  }
1675
0
}
1676
1677
bool PU::addAffineMVPCandUnscaled( const CodingUnit &cu, const RefPicList &refPicList, const int &refIdx, const Position &pos, const MvpDir &dir, AffineAMVPInfo &affiAMVPInfo )
1678
0
{
1679
0
  const CodingStructure &cs = *cu.cs;
1680
0
  const CodingUnit  *neibCU = nullptr;
1681
0
  const CodingUnit  *guess  = cu.left;
1682
0
  Position neibPos;
1683
1684
0
  switch ( dir )
1685
0
  {
1686
0
  case MD_LEFT:
1687
0
    neibPos = pos.offset( -1, 0 );
1688
0
    break;
1689
0
  case MD_ABOVE:
1690
0
    guess = cu.above;
1691
0
    neibPos = pos.offset( 0, -1 );
1692
0
    break;
1693
0
  case MD_ABOVE_RIGHT:
1694
0
    guess = cu.above;
1695
0
    neibPos = pos.offset( 1, -1 );
1696
0
    break;
1697
0
  case MD_BELOW_LEFT:
1698
0
    neibPos = pos.offset( -1, 1 );
1699
0
    break;
1700
0
  case MD_ABOVE_LEFT:
1701
0
    guess = guess ? guess : cu.above;
1702
0
    neibPos = pos.offset( -1, -1 );
1703
0
    break;
1704
0
  default:
1705
0
    break;
1706
0
  }
1707
1708
0
  neibCU = cs.getCURestricted( neibPos, cu, CH_L, guess );
1709
1710
0
  if ( !neibCU || !CU::isInter( *neibCU ) || !neibCU->affineFlag() || neibCU->mergeType() != MRG_TYPE_DEFAULT_N )
1711
0
  {
1712
0
    return false;
1713
0
  }
1714
1715
0
  Mv outputAffineMv[3];
1716
0
  const MotionInfo& neibMi = neibCU->getMotionInfo( neibPos );
1717
1718
0
  const int        currRefPOC = cu.slice->getRefPOC( refPicList, refIdx );
1719
0
  const RefPicList refPicList2nd = (refPicList == REF_PIC_LIST_0) ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1720
1721
0
  for ( int predictorSource = 0; predictorSource < 2; predictorSource++ ) // examine the indicated reference picture list, then if not available, examine the other list.
1722
0
  {
1723
0
    const RefPicList eRefPicListIndex = (predictorSource == 0) ? refPicList : refPicList2nd;
1724
0
    const int        neibRefIdx = neibMi.miRefIdx[eRefPicListIndex];
1725
1726
0
    if( ( ( neibCU->interDir() & ( eRefPicListIndex + 1 ) ) == 0 ) || cu.slice->getRefPOC( eRefPicListIndex, neibRefIdx ) != currRefPOC )
1727
0
    {
1728
0
      continue;
1729
0
    }
1730
1731
0
    xInheritedAffineMv( cu, cu.affineType() == AFFINEMODEL_6PARAM, neibCU, eRefPicListIndex, outputAffineMv );
1732
1733
0
    if( cu.imv() == 0 )
1734
0
    {
1735
0
      outputAffineMv[0].roundToPrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
1736
0
      outputAffineMv[1].roundToPrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
1737
0
    }
1738
0
    else if( cu.imv() == 2 )
1739
0
    {
1740
0
      outputAffineMv[0].roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_INT );
1741
0
      outputAffineMv[1].roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_INT );
1742
0
    }
1743
1744
0
    affiAMVPInfo.mvCandLT[affiAMVPInfo.numCand] = outputAffineMv[0];
1745
0
    affiAMVPInfo.mvCandRT[affiAMVPInfo.numCand] = outputAffineMv[1];
1746
1747
0
    if( cu.affineType() == AFFINEMODEL_6PARAM )
1748
0
    {
1749
0
      if( cu.imv() == 0 )
1750
0
      {
1751
0
        outputAffineMv[2].roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER );
1752
0
      }
1753
0
      else if( cu.imv() == 2 )
1754
0
      {
1755
0
        outputAffineMv[2].roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_INT );
1756
0
      }
1757
0
      affiAMVPInfo.mvCandLB[affiAMVPInfo.numCand] = outputAffineMv[2];
1758
0
    }
1759
0
    affiAMVPInfo.numCand++;
1760
0
    return true;
1761
0
  }
1762
1763
0
  return false;
1764
0
}
1765
1766
void PU::xInheritedAffineMv( const CodingUnit &cu, bool is6param, const CodingUnit* puNeighbour, RefPicList eRefPicList, Mv rcMv[3] )
1767
0
{
1768
0
  int posNeiX = puNeighbour->Y().pos().x;
1769
0
  int posNeiY = puNeighbour->Y().pos().y;
1770
0
  int posCurX = cu.Y().pos().x;
1771
0
  int posCurY = cu.Y().pos().y;
1772
1773
0
  int neiW = puNeighbour->Y().width;
1774
0
  int curW = cu.Y().width;
1775
0
  int neiH = puNeighbour->Y().height;
1776
0
  int curH = cu.Y().height;
1777
1778
0
  Mv mvLT, mvRT, mvLB;
1779
0
  mvLT = puNeighbour->mv[eRefPicList][0];
1780
0
  mvRT = puNeighbour->mv[eRefPicList][1];
1781
0
  mvLB = puNeighbour->mv[eRefPicList][2];
1782
1783
0
  bool isTopCtuBoundary = false;
1784
0
  if ( (posNeiY + neiH) % cu.sps->getCTUSize() == 0 && (posNeiY + neiH) == posCurY )
1785
0
  {
1786
    // use bottom-left and bottom-right sub-block MVs for inheritance
1787
0
    const Position posRB = puNeighbour->Y().bottomRight();
1788
0
    const Position posLB = puNeighbour->Y().bottomLeft();
1789
0
    mvLT = puNeighbour->getMotionInfo( posLB ).mv[eRefPicList];
1790
0
    mvRT = puNeighbour->getMotionInfo( posRB ).mv[eRefPicList];
1791
0
    posNeiY += neiH;
1792
0
    isTopCtuBoundary = true;
1793
0
  }
1794
1795
0
  int shift = MAX_CU_DEPTH;
1796
0
  int iDMvHorX, iDMvHorY, iDMvVerX, iDMvVerY;
1797
1798
0
  iDMvHorX = (mvRT - mvLT).getHor() *(1<< (shift - getLog2(neiW)));
1799
0
  iDMvHorY = (mvRT - mvLT).getVer() *(1<< (shift - getLog2(neiW)));
1800
0
  if ( puNeighbour->affineType() == AFFINEMODEL_6PARAM && !isTopCtuBoundary )
1801
0
  {
1802
0
    iDMvVerX = (mvLB - mvLT).getHor() *(1<< (shift - getLog2(neiH)));
1803
0
    iDMvVerY = (mvLB - mvLT).getVer() *(1<< (shift - getLog2(neiH)));
1804
0
  }
1805
0
  else
1806
0
  {
1807
0
    iDMvVerX = -iDMvHorY;
1808
0
    iDMvVerY = iDMvHorX;
1809
0
  }
1810
1811
0
  int iMvScaleHor = mvLT.getHor() *(1<< shift);
1812
0
  int iMvScaleVer = mvLT.getVer() *(1<< shift);
1813
0
  int horTmp, verTmp;
1814
1815
  // v0
1816
0
  horTmp = iMvScaleHor + iDMvHorX * (posCurX - posNeiX) + iDMvVerX * (posCurY - posNeiY);
1817
0
  verTmp = iMvScaleVer + iDMvHorY * (posCurX - posNeiX) + iDMvVerY * (posCurY - posNeiY);
1818
0
  roundAffineMv( horTmp, verTmp, shift );
1819
0
  rcMv[0].hor = horTmp;
1820
0
  rcMv[0].ver = verTmp;
1821
0
  rcMv[0].clipToStorageBitDepth();
1822
1823
  // v1
1824
0
  horTmp = iMvScaleHor + iDMvHorX * (posCurX + curW - posNeiX) + iDMvVerX * (posCurY - posNeiY);
1825
0
  verTmp = iMvScaleVer + iDMvHorY * (posCurX + curW - posNeiX) + iDMvVerY * (posCurY - posNeiY);
1826
0
  roundAffineMv( horTmp, verTmp, shift );
1827
0
  rcMv[1].hor = horTmp;
1828
0
  rcMv[1].ver = verTmp;
1829
0
  rcMv[1].clipToStorageBitDepth();
1830
1831
  // v2
1832
0
  if ( is6param )
1833
0
  {
1834
0
    horTmp = iMvScaleHor + iDMvHorX * (posCurX - posNeiX) + iDMvVerX * (posCurY + curH - posNeiY);
1835
0
    verTmp = iMvScaleVer + iDMvHorY * (posCurX - posNeiX) + iDMvVerY * (posCurY + curH - posNeiY);
1836
0
    roundAffineMv( horTmp, verTmp, shift );
1837
0
    rcMv[2].hor = horTmp;
1838
0
    rcMv[2].ver = verTmp;
1839
0
    rcMv[2].clipToStorageBitDepth();
1840
0
  }
1841
0
}
1842
1843
1844
void PU::fillAffineMvpCand(CodingUnit &cu, const RefPicList &eRefPicList, const int &refIdx, AffineAMVPInfo &affiAMVPInfo)
1845
0
{
1846
0
  affiAMVPInfo.numCand = 0;
1847
1848
0
  if (refIdx < 0)
1849
0
  {
1850
0
    return;
1851
0
  }
1852
1853
1854
  // insert inherited affine candidates
1855
0
  Mv outputAffineMv[3];
1856
0
  Position posLT = cu.Y().topLeft();
1857
0
  Position posRT = cu.Y().topRight();
1858
0
  Position posLB = cu.Y().bottomLeft();
1859
1860
  // check left neighbor
1861
0
  if( !addAffineMVPCandUnscaled( cu, eRefPicList, refIdx, posLB, MD_BELOW_LEFT, affiAMVPInfo ) )
1862
0
  {
1863
0
    addAffineMVPCandUnscaled( cu, eRefPicList, refIdx, posLB, MD_LEFT, affiAMVPInfo );
1864
0
  }
1865
1866
  // check above neighbor
1867
0
  if( !addAffineMVPCandUnscaled( cu, eRefPicList, refIdx, posRT, MD_ABOVE_RIGHT, affiAMVPInfo ) )
1868
0
  {
1869
0
    if( !addAffineMVPCandUnscaled( cu, eRefPicList, refIdx, posRT, MD_ABOVE, affiAMVPInfo ) )
1870
0
    {
1871
0
      addAffineMVPCandUnscaled( cu, eRefPicList, refIdx, posLT, MD_ABOVE_LEFT, affiAMVPInfo );
1872
0
    }
1873
0
  }
1874
1875
0
  if( affiAMVPInfo.numCand >= AMVP_MAX_NUM_CANDS )
1876
0
  {
1877
0
    for( int i = 0; i < affiAMVPInfo.numCand; i++ )
1878
0
    {
1879
0
      if( cu.imv() != 1 )
1880
0
      {
1881
0
        affiAMVPInfo.mvCandLT[i].changePrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
1882
0
        affiAMVPInfo.mvCandRT[i].changePrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
1883
0
        affiAMVPInfo.mvCandLB[i].changePrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
1884
0
      }
1885
0
    }
1886
0
    return;
1887
0
  }
1888
1889
  // insert constructed affine candidates
1890
0
  int cornerMVPattern = 0;
1891
1892
  //-------------------  V0 (START) -------------------//
1893
0
  AMVPInfo amvpInfo0;
1894
0
  amvpInfo0.numCand = 0;
1895
1896
  // A->C: Above Left, Above, Left
1897
0
  addMVPCandUnscaled( cu, eRefPicList, refIdx, posLT, MD_ABOVE_LEFT, amvpInfo0 );
1898
0
  if ( amvpInfo0.numCand < 1 )
1899
0
  {
1900
0
    addMVPCandUnscaled( cu, eRefPicList, refIdx, posLT, MD_ABOVE, amvpInfo0 );
1901
0
  }
1902
0
  if ( amvpInfo0.numCand < 1 )
1903
0
  {
1904
0
    addMVPCandUnscaled( cu, eRefPicList, refIdx, posLT, MD_LEFT, amvpInfo0 );
1905
0
  }
1906
0
  cornerMVPattern = cornerMVPattern | amvpInfo0.numCand;
1907
1908
  //-------------------  V1 (START) -------------------//
1909
0
  AMVPInfo amvpInfo1;
1910
0
  amvpInfo1.numCand = 0;
1911
1912
  // D->E: Above, Above Right
1913
0
  addMVPCandUnscaled( cu, eRefPicList, refIdx, posRT, MD_ABOVE, amvpInfo1 );
1914
0
  if ( amvpInfo1.numCand < 1 )
1915
0
  {
1916
0
    addMVPCandUnscaled( cu, eRefPicList, refIdx, posRT, MD_ABOVE_RIGHT, amvpInfo1 );
1917
0
  }
1918
0
  cornerMVPattern = cornerMVPattern | (amvpInfo1.numCand << 1);
1919
1920
  //-------------------  V2 (START) -------------------//
1921
0
  AMVPInfo amvpInfo2;
1922
0
  amvpInfo2.numCand = 0;
1923
1924
  // F->G: Left, Below Left
1925
0
  addMVPCandUnscaled( cu, eRefPicList, refIdx, posLB, MD_LEFT, amvpInfo2 );
1926
0
  if( amvpInfo2.numCand < 1 )
1927
0
  {
1928
0
    addMVPCandUnscaled( cu, eRefPicList, refIdx, posLB, MD_BELOW_LEFT, amvpInfo2 );
1929
0
  }
1930
0
  cornerMVPattern = cornerMVPattern | ( amvpInfo2.numCand << 2 );
1931
1932
0
  outputAffineMv[0] = amvpInfo0.mvCand[0];
1933
0
  outputAffineMv[1] = amvpInfo1.mvCand[0];
1934
0
  outputAffineMv[2] = amvpInfo2.mvCand[0];
1935
1936
0
  if( cu.imv() == 0 )
1937
0
  {
1938
0
    outputAffineMv[0].roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER );
1939
0
    outputAffineMv[1].roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER );
1940
0
    outputAffineMv[2].roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER );
1941
0
  }
1942
0
  else if( cu.imv() == 2 )
1943
0
  {
1944
0
    outputAffineMv[0].roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_INT );
1945
0
    outputAffineMv[1].roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_INT );
1946
0
    outputAffineMv[2].roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_INT );
1947
0
  }
1948
1949
0
  if( cornerMVPattern == 7 || ( cornerMVPattern == 3 && cu.affineType() == AFFINEMODEL_4PARAM ) )
1950
0
  {
1951
0
    affiAMVPInfo.mvCandLT[affiAMVPInfo.numCand] = outputAffineMv[0];
1952
0
    affiAMVPInfo.mvCandRT[affiAMVPInfo.numCand] = outputAffineMv[1];
1953
0
    affiAMVPInfo.mvCandLB[affiAMVPInfo.numCand] = outputAffineMv[2];
1954
0
    affiAMVPInfo.numCand++;
1955
0
  }
1956
1957
1958
0
  if ( affiAMVPInfo.numCand < 2 )
1959
0
  {
1960
    // check corner MVs
1961
0
    for ( int i = 2; i >= 0 && affiAMVPInfo.numCand < AMVP_MAX_NUM_CANDS; i-- )
1962
0
    {
1963
0
      if ( cornerMVPattern & (1 << i) ) // MV i exist
1964
0
      {
1965
0
        affiAMVPInfo.mvCandLT[affiAMVPInfo.numCand] = outputAffineMv[i];
1966
0
        affiAMVPInfo.mvCandRT[affiAMVPInfo.numCand] = outputAffineMv[i];
1967
0
        affiAMVPInfo.mvCandLB[affiAMVPInfo.numCand] = outputAffineMv[i];
1968
0
        affiAMVPInfo.numCand++;
1969
0
      }
1970
0
    }
1971
1972
    // Get Temporal Motion Predictor
1973
0
    if ( affiAMVPInfo.numCand < 2 && cu.cs->picHeader->getEnableTMVPFlag() )
1974
0
    {
1975
0
      const int refIdxCol = refIdx;
1976
1977
0
      Position posRB = cu.Y().bottomRight().offset( -3, -3 );
1978
1979
0
      const PreCalcValues& pcv = *cu.cs->pcv;
1980
1981
0
      Position posC0;
1982
0
      bool C0Avail = false;
1983
0
      Position posC1 = cu.Y().center();
1984
0
      Mv cColMv;
1985
0
      bool boundaryCond = ((posRB.x + pcv.minCUWidth) < pcv.lumaWidth) && ((posRB.y + pcv.minCUHeight) < pcv.lumaHeight);
1986
0
      const SubPic& curSubPic = cu.pps->getSubPicFromPos( cu.lumaPos() );
1987
0
      if( curSubPic.getTreatedAsPicFlag() )
1988
0
      {
1989
0
        boundaryCond = ((posRB.x + pcv.minCUWidth) <= curSubPic.getSubPicRight() &&
1990
0
          (posRB.y + pcv.minCUHeight) <= curSubPic.getSubPicBottom());
1991
0
      }
1992
0
      if( boundaryCond )
1993
0
      {
1994
0
        Position posInCtu( posRB.x & pcv.maxCUWidthMask, posRB.y & pcv.maxCUHeightMask );
1995
1996
0
        if ( (posInCtu.x + 4 < pcv.maxCUWidth) &&           // is not at the last column of CTU
1997
0
          (posInCtu.y + 4 < pcv.maxCUHeight) )             // is not at the last row    of CTU
1998
0
        {
1999
0
          posC0 = posRB.offset( 4, 4 );
2000
0
          C0Avail = true;
2001
0
        }
2002
0
        else if ( posInCtu.x + 4 < pcv.maxCUWidth )           // is not at the last column of CTU But is last row of CTU
2003
0
        {
2004
          // in the reference the CTU address is not set - thus probably resulting in no using this C0 possibility
2005
0
          posC0 = posRB.offset( 4, 4 );
2006
0
        }
2007
0
        else if ( posInCtu.y + 4 < pcv.maxCUHeight )          // is not at the last row of CTU But is last column of CTU
2008
0
        {
2009
0
          posC0 = posRB.offset( 4, 4 );
2010
0
          C0Avail = true;
2011
0
        }
2012
0
        else //is the right bottom corner of CTU
2013
0
        {
2014
          // same as for last column but not last row
2015
0
          posC0 = posRB.offset( 4, 4 );
2016
0
        }
2017
0
      }
2018
2019
0
      if( ( C0Avail && getColocatedMVP( cu, eRefPicList, posC0, cColMv, refIdxCol, false ) ) || getColocatedMVP( cu, eRefPicList, posC1, cColMv, refIdxCol, false ) )
2020
0
      {
2021
0
        if( cu.imv() == 0 )
2022
0
        {
2023
0
          cColMv.roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER );
2024
0
        }
2025
0
        else if( cu.imv() == 2 )
2026
0
        {
2027
0
          cColMv.roundToPrecision( MV_PRECISION_INTERNAL, MV_PRECISION_INT );
2028
0
        }
2029
0
        affiAMVPInfo.mvCandLT[affiAMVPInfo.numCand] = cColMv;
2030
0
        affiAMVPInfo.mvCandRT[affiAMVPInfo.numCand] = cColMv;
2031
0
        affiAMVPInfo.mvCandLB[affiAMVPInfo.numCand] = cColMv;
2032
0
        affiAMVPInfo.numCand++;
2033
0
      }
2034
0
      }
2035
2036
0
    if( affiAMVPInfo.numCand < 2 )
2037
0
    {
2038
      // add zero MV
2039
0
      for( int i = affiAMVPInfo.numCand; i < AMVP_MAX_NUM_CANDS; i++ )
2040
0
      {
2041
0
        affiAMVPInfo.mvCandLT[affiAMVPInfo.numCand].setZero();
2042
0
        affiAMVPInfo.mvCandRT[affiAMVPInfo.numCand].setZero();
2043
0
        affiAMVPInfo.mvCandLB[affiAMVPInfo.numCand].setZero();
2044
0
        affiAMVPInfo.numCand++;
2045
0
      }
2046
0
    }
2047
0
    }
2048
2049
0
  for( int i = 0; i < affiAMVPInfo.numCand; i++ )
2050
0
  {
2051
0
    if( cu.imv() != 1 )
2052
0
    {
2053
0
      affiAMVPInfo.mvCandLT[i].changePrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
2054
0
      affiAMVPInfo.mvCandRT[i].changePrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
2055
0
      affiAMVPInfo.mvCandLB[i].changePrecision(MV_PRECISION_INTERNAL, MV_PRECISION_QUARTER);
2056
0
    }
2057
0
  }
2058
0
}
2059
2060
bool PU::addMVPCandUnscaled( const CodingUnit &cu, const RefPicList &eRefPicList, const int &iRefIdx, const Position &pos, const MvpDir &eDir, AMVPInfo &info )
2061
0
{
2062
0
        CodingStructure &cs = *cu.cs;
2063
0
  const CodingUnit *neibCU  = NULL;
2064
0
  const CodingUnit  *guess  = cu.left;
2065
0
        Position neibPos;
2066
2067
0
  switch (eDir)
2068
0
  {
2069
0
  case MD_LEFT:
2070
0
    neibPos = pos.offset( -1,  0 );
2071
0
    break;
2072
0
  case MD_ABOVE:
2073
0
    guess = cu.above;
2074
0
    neibPos = pos.offset(  0, -1 );
2075
0
    break;
2076
0
  case MD_ABOVE_RIGHT:
2077
0
    guess = cu.above;
2078
0
    neibPos = pos.offset(  1, -1 );
2079
0
    break;
2080
0
  case MD_BELOW_LEFT:
2081
0
    neibPos = pos.offset( -1,  1 );
2082
0
    break;
2083
0
  case MD_ABOVE_LEFT:
2084
0
    guess = cu.left ? cu.left : cu.above;
2085
0
    neibPos = pos.offset( -1, -1 );
2086
0
    break;
2087
0
  default:
2088
0
    break;
2089
0
  }
2090
2091
0
  neibCU = cs.getCURestricted( neibPos, cu, CH_L, guess );
2092
2093
0
  if( neibCU == NULL || !CU::isInter( *neibCU ) )
2094
0
  {
2095
0
    return false;
2096
0
  }
2097
2098
0
  const MotionInfo& neibMi        = neibCU->getMotionInfo( neibPos );
2099
2100
0
  const int        currRefPOC     = cu.slice->getRefPOC( eRefPicList, iRefIdx );
2101
0
  const RefPicList eRefPicList2nd = ( eRefPicList == REF_PIC_LIST_0 ) ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
2102
2103
0
  for( int predictorSource = 0; predictorSource < 2; predictorSource++ ) // examine the indicated reference picture list, then if not available, examine the other list.
2104
0
  {
2105
0
    const RefPicList eRefPicListIndex = ( predictorSource == 0 ) ? eRefPicList : eRefPicList2nd;
2106
0
    const int        neibRefIdx       = neibMi.miRefIdx[eRefPicListIndex];
2107
2108
0
    if( neibRefIdx >= 0 && currRefPOC == cu.slice->getRefPOC( eRefPicListIndex, neibRefIdx ) )
2109
0
    {
2110
0
      info.mvCand[info.numCand++] = neibMi.mv[eRefPicListIndex];
2111
0
      return true;
2112
0
    }
2113
0
  }
2114
2115
0
  return false;
2116
0
}
2117
2118
2119
void PU::addAMVPHMVPCand( const CodingUnit &cu, MotionHist& hist, const RefPicList eRefPicList, const RefPicList eRefPicList2nd, const int currRefPOC, AMVPInfo &info, uint8_t imv )
2120
0
{
2121
0
  const Slice &slice = *cu.slice;
2122
2123
0
  auto &lut = CU::isIBC( cu ) ? hist.motionLutIbc : hist.motionLut;
2124
0
  int num_avai_candInLUT = (int) lut.size();
2125
0
  int num_allowedCand = std::min(MAX_NUM_HMVP_AVMPCANDS, num_avai_candInLUT);
2126
2127
0
  for( int mrgIdx = 1; mrgIdx <= num_allowedCand; mrgIdx++ )
2128
0
  {
2129
0
    if( info.numCand >= AMVP_MAX_NUM_CANDS )
2130
0
    {
2131
0
      return;
2132
0
    }
2133
2134
0
    const HPMVInfo &neibMi = lut[mrgIdx - 1];
2135
2136
0
    for( int predictorSource = 0; predictorSource < 2; predictorSource++ )
2137
0
    {
2138
0
      const RefPicList eRefPicListIndex = predictorSource == 0 ? eRefPicList : eRefPicList2nd;
2139
0
      const int        neibRefIdx       = neibMi.mhRefIdx[eRefPicListIndex];
2140
2141
0
      if( neibRefIdx >= 0 && ( CU::isIBC( cu ) || currRefPOC == slice.getRefPOC( eRefPicListIndex, neibRefIdx ) ) )
2142
0
      {
2143
0
        Mv pmv = neibMi.mv[eRefPicListIndex];
2144
0
        pmv.roundToAmvrSignalPrecision( MV_PRECISION_INTERNAL, cu.imv() );
2145
2146
0
        info.mvCand[info.numCand++] = pmv;
2147
2148
0
        if( info.numCand >= AMVP_MAX_NUM_CANDS )
2149
0
        {
2150
0
          return;
2151
0
        }
2152
0
      }
2153
0
    }
2154
0
  }
2155
0
}
2156
2157
bool PU::isBipredRestriction( const CodingUnit &cu )
2158
0
{
2159
0
  const SizeType w = cu.lwidth(), h = cu.lheight();
2160
  /* disable bi-prediction for 4x8/8x4 */
2161
0
  return ( w + h <= 12 );
2162
0
}
2163
2164
void PU::getAffineControlPointCand( const CodingUnit &cu, MotionInfo mi[4], bool isAvailable[4], int verIdx[4], int8_t bcwIdx, int modelIdx, int verNum, AffineMergeCtx& affMrgType )
2165
0
{
2166
0
  int cuW = cu.Y().width;
2167
0
  int cuH = cu.Y().height;
2168
0
  int vx, vy;
2169
0
  int shift = MAX_CU_DEPTH;
2170
0
  int shiftHtoW = shift + getLog2(cuW) - getLog2(cuH);
2171
2172
  // motion info
2173
0
  Mv cMv[2][4];
2174
0
  int refIdx[2] = { -1, -1 };
2175
0
  int dir = 0;
2176
0
  AffineModel curType = (verNum == 2) ? AFFINEMODEL_4PARAM : AFFINEMODEL_6PARAM;
2177
2178
0
  if ( verNum == 2 )
2179
0
  {
2180
0
    int idx0 = verIdx[0], idx1 = verIdx[1];
2181
0
    if ( !isAvailable[idx0] || !isAvailable[idx1] )
2182
0
    {
2183
0
      return;
2184
0
    }
2185
2186
0
    for ( int l = 0; l < 2; l++ )
2187
0
    {
2188
0
      if ( isMotionValid( mi[idx0].miRefIdx[l], MI_NOT_VALID ) && isMotionValid( mi[idx1].miRefIdx[l], MI_NOT_VALID ) )
2189
0
      {
2190
        // check same refidx and different mv
2191
0
        if ( mi[idx0].miRefIdx[l] == mi[idx1].miRefIdx[l])
2192
0
        {
2193
0
          dir |= (l + 1);
2194
0
          refIdx[l] = mi[idx0].miRefIdx[l];
2195
0
        }
2196
0
      }
2197
0
    }
2198
2199
0
  }
2200
0
  else if ( verNum == 3 )
2201
0
  {
2202
0
    int idx0 = verIdx[0], idx1 = verIdx[1], idx2 = verIdx[2];
2203
0
    if ( !isAvailable[idx0] || !isAvailable[idx1] || !isAvailable[idx2] )
2204
0
    {
2205
0
      return;
2206
0
    }
2207
2208
0
    for ( int l = 0; l < 2; l++ )
2209
0
    {
2210
0
      if ( isMotionValid( mi[idx0].miRefIdx[l], MI_NOT_VALID ) && isMotionValid( mi[idx1].miRefIdx[l], MI_NOT_VALID ) && isMotionValid( mi[idx2].miRefIdx[l], MI_NOT_VALID ) )
2211
0
      {
2212
        // check same refidx and different mv
2213
0
        if ( mi[idx0].miRefIdx[l] == mi[idx1].miRefIdx[l] && mi[idx0].miRefIdx[l] == mi[idx2].miRefIdx[l])
2214
0
        {
2215
0
          dir |= (l + 1);
2216
0
          refIdx[l] = mi[idx0].miRefIdx[l];
2217
0
        }
2218
0
      }
2219
0
    }
2220
0
  }
2221
2222
0
  if ( dir == 0 )
2223
0
  {
2224
0
    return;
2225
0
  }
2226
2227
2228
0
  for ( int l = 0; l < 2; l++ )
2229
0
  {
2230
0
    if ( dir & (l + 1) )
2231
0
    {
2232
0
      for ( int i = 0; i < verNum; i++ )
2233
0
      {
2234
0
        cMv[l][verIdx[i]] = mi[verIdx[i]].mv[l];
2235
0
      }
2236
2237
      // convert to LT, RT[, [LB]]
2238
0
      switch ( modelIdx )
2239
0
      {
2240
0
      case 0: // 0 : LT, RT, LB
2241
0
        break;
2242
2243
0
      case 1: // 1 : LT, RT, RB
2244
0
        cMv[l][2].hor = cMv[l][3].hor + cMv[l][0].hor - cMv[l][1].hor;
2245
0
        cMv[l][2].ver = cMv[l][3].ver + cMv[l][0].ver - cMv[l][1].ver;
2246
0
        cMv[l][2].clipToStorageBitDepth();
2247
0
        break;
2248
2249
0
      case 2: // 2 : LT, LB, RB
2250
0
        cMv[l][1].hor = cMv[l][3].hor + cMv[l][0].hor - cMv[l][2].hor;
2251
0
        cMv[l][1].ver = cMv[l][3].ver + cMv[l][0].ver - cMv[l][2].ver;
2252
0
        cMv[l][1].clipToStorageBitDepth();
2253
0
        break;
2254
2255
0
      case 3: // 3 : RT, LB, RB
2256
0
        cMv[l][0].hor = cMv[l][1].hor + cMv[l][2].hor - cMv[l][3].hor;
2257
0
        cMv[l][0].ver = cMv[l][1].ver + cMv[l][2].ver - cMv[l][3].ver;
2258
0
        cMv[l][0].clipToStorageBitDepth();
2259
0
        break;
2260
2261
0
      case 4: // 4 : LT, RT
2262
0
        break;
2263
2264
0
      case 5: // 5 : LT, LB
2265
0
        vx = (cMv[l][0].hor *(1<< shift)) + ((cMv[l][2].ver - cMv[l][0].ver) *(1<< shiftHtoW));
2266
0
        vy = (cMv[l][0].ver *(1<< shift)) - ((cMv[l][2].hor - cMv[l][0].hor) *(1<< shiftHtoW));
2267
0
        roundAffineMv( vx, vy, shift );
2268
0
        cMv[l][1].set( vx, vy );
2269
0
        cMv[l][1].clipToStorageBitDepth();
2270
0
        break;
2271
2272
0
      default:
2273
0
        CHECK( 1, "Invalid model index!\n" );
2274
0
        break;
2275
0
      }
2276
0
    }
2277
0
    else
2278
0
    {
2279
0
      for ( int i = 0; i < 4; i++ )
2280
0
      {
2281
0
        cMv[l][i].hor = 0;
2282
0
        cMv[l][i].ver = 0;
2283
0
      }
2284
0
    }
2285
0
  }
2286
2287
0
  for ( int i = 0; i < 3; i++ )
2288
0
  {
2289
0
    affMrgType.mvFieldNeighbours[(affMrgType.numValidMergeCand << 1) + 0][i].mv       = cMv   [0][i];
2290
0
    affMrgType.mvFieldNeighbours[(affMrgType.numValidMergeCand << 1) + 0][i].mfRefIdx = refIdx[0];
2291
2292
0
    affMrgType.mvFieldNeighbours[(affMrgType.numValidMergeCand << 1) + 1][i].mv       = cMv   [1][i];
2293
0
    affMrgType.mvFieldNeighbours[(affMrgType.numValidMergeCand << 1) + 1][i].mfRefIdx = refIdx[1];
2294
0
  }
2295
2296
0
  affMrgType.interDirNeighbours[affMrgType.numValidMergeCand] = dir;
2297
0
  affMrgType.affineType        [affMrgType.numValidMergeCand] = curType;
2298
0
  affMrgType.BcwIdx            [affMrgType.numValidMergeCand] = dir == 3 ? bcwIdx : BCW_DEFAULT;
2299
0
  affMrgType.numValidMergeCand++;
2300
2301
2302
0
  return;
2303
0
}
2304
2305
int getAvailableAffineNeighboursForLeftPredictor( const CodingUnit &cu, const CodingUnit* npu[] )
2306
0
{
2307
0
  const Position posLB = cu.Y().bottomLeft();
2308
0
  const unsigned plevel = cu.sps->getLog2ParallelMergeLevelMinus2() + 2;
2309
0
  int num = 0;
2310
2311
0
  const CodingUnit *cuLeftBottom = cu.cs->getCURestricted( posLB.offset( -1, 1 ), cu, CH_L, cu.left );
2312
0
  if( cuLeftBottom && cuLeftBottom->affineFlag() && cuLeftBottom->mergeType() == MRG_TYPE_DEFAULT_N && PU::isDiffMER( cu.lumaPos(), posLB.offset( -1, 1 ), plevel ) )
2313
0
  {
2314
0
    npu[num++] = cuLeftBottom;
2315
0
    return num;
2316
0
  }
2317
2318
0
  const CodingUnit* cuLeft = cu.cs->getCURestricted( posLB.offset( -1, 0 ), cu, CH_L, cu.left );
2319
0
  if( cuLeft && cuLeft->affineFlag() && cuLeft->mergeType() == MRG_TYPE_DEFAULT_N && PU::isDiffMER( cu.lumaPos(), posLB.offset( -1, 0 ), plevel ) )
2320
0
  {
2321
0
    npu[num++] = cuLeft;
2322
0
    return num;
2323
0
  }
2324
2325
0
  return num;
2326
0
}
2327
2328
int getAvailableAffineNeighboursForAbovePredictor( const CodingUnit &cu, const CodingUnit* npu[], int numAffNeighLeft )
2329
0
{
2330
0
  const Position posLT = cu.Y().topLeft();
2331
0
  const Position posRT = cu.Y().topRight();
2332
0
  const unsigned plevel = cu.sps->getLog2ParallelMergeLevelMinus2() + 2;
2333
0
  int num = numAffNeighLeft;
2334
2335
0
  const CodingUnit* cuAboveRight = cu.cs->getCURestricted( posRT.offset( 1, -1 ), cu, CH_L, cu.above );
2336
0
  if( cuAboveRight && cuAboveRight->affineFlag() && cuAboveRight->mergeType() == MRG_TYPE_DEFAULT_N && PU::isDiffMER( cu.lumaPos(), posRT.offset( 1, -1 ), plevel ) )
2337
0
  {
2338
0
    npu[num++] = cuAboveRight;
2339
0
    return num;
2340
0
  }
2341
2342
0
  const CodingUnit* cuAbove = cu.cs->getCURestricted( posRT.offset( 0, -1 ), cu, CH_L, cu.above );
2343
0
  if( cuAbove && cuAbove->affineFlag() && cuAbove->mergeType() == MRG_TYPE_DEFAULT_N && PU::isDiffMER( cu.lumaPos(), posRT.offset( 0, -1 ), plevel ) )
2344
0
  {
2345
0
    npu[num++] = cuAbove;
2346
0
    return num;
2347
0
  }
2348
2349
0
  const CodingUnit *cuAboveLeft = cu.cs->getCURestricted( posLT.offset( -1, -1 ), cu, CH_L, cu.left ? cu.left : cu.above );
2350
0
  if( cuAboveLeft && cuAboveLeft->affineFlag() && cuAboveLeft->mergeType() == MRG_TYPE_DEFAULT_N && PU::isDiffMER( cu.lumaPos(), posLT.offset( -1, -1 ), plevel ) )
2351
0
  {
2352
0
    npu[num++] = cuAboveLeft;
2353
0
    return num;
2354
0
  }
2355
2356
0
  return num;
2357
0
}
2358
2359
void PU::getAffineMergeCand( const CodingUnit &cu, AffineMergeCtx& affMrgCtx, const int mrgCandIdx )
2360
0
{
2361
0
  const CodingStructure &cs = *cu.cs;
2362
0
  const Slice &slice        = *cu.slice;
2363
0
  const uint32_t maxNumAffineMergeCand = slice.getPicHeader()->getMaxNumAffineMergeCand();
2364
0
  const unsigned plevel = cu.sps->getLog2ParallelMergeLevelMinus2() + 2;
2365
2366
0
  for ( int i = 0; i < maxNumAffineMergeCand; i++ )
2367
0
  {
2368
0
    for ( int mvNum = 0; mvNum < 3; mvNum++ )
2369
0
    {
2370
0
      affMrgCtx.mvFieldNeighbours[(i << 1) + 0][mvNum].setMvField( Mv(), MF_NOT_VALID );
2371
0
      affMrgCtx.mvFieldNeighbours[(i << 1) + 1][mvNum].setMvField( Mv(), MF_NOT_VALID );
2372
0
    }
2373
0
    affMrgCtx.interDirNeighbours[i] = 0;
2374
0
    affMrgCtx.affineType[i] = AFFINEMODEL_4PARAM;
2375
0
    affMrgCtx.mergeType[i] = MRG_TYPE_DEFAULT_N;
2376
0
    affMrgCtx.BcwIdx[i] = BCW_DEFAULT;
2377
0
  }
2378
2379
0
  affMrgCtx.numValidMergeCand = 0;
2380
0
  affMrgCtx.maxNumMergeCand = maxNumAffineMergeCand;
2381
0
  bool enableSubPuMvp = slice.getSPS()->getSBTMVPEnabledFlag() && !(slice.getPOC() == slice.getRefPOC(REF_PIC_LIST_0, 0) && slice.isIRAP());
2382
0
  bool isAvailableSubPu = false;
2383
0
  if ( enableSubPuMvp && slice.getPicHeader()->getEnableTMVPFlag() )
2384
0
  {
2385
0
    CHECKD( affMrgCtx.subPuMvpMiBuf.area() == 0 || !affMrgCtx.subPuMvpMiBuf.buf, "Buffer not initialized" );
2386
2387
0
    int pos = 0;
2388
    // Get spatial MV
2389
0
    const Position posCurLB = cu.Y().bottomLeft();
2390
2391
    //left
2392
0
    const CodingUnit* cuLeft = cs.getCURestricted( posCurLB.offset( -1, 0 ), cu, CH_L, cu.left );
2393
0
    const bool isAvailableA1 = cuLeft && CU::isInter( *cuLeft ) && isDiffMER( cu.lumaPos(), posCurLB.offset( -1, 0 ), plevel );
2394
0
    if ( isAvailableA1 )
2395
0
    {
2396
0
      const MotionInfo& miLeft = cuLeft->getMotionInfo( posCurLB.offset( -1, 0 ) );
2397
      // get Inter Dir
2398
0
      affMrgCtx.interDirNeighbours[pos] = miLeft.interDir();
2399
2400
      // get Mv from Left
2401
0
      affMrgCtx.mvFieldNeighbours[pos << 1][0].setMvField(miLeft.mv[0], miLeft.miRefIdx[0]);
2402
2403
0
      if ( slice.isInterB() )
2404
0
      {
2405
0
        affMrgCtx.mvFieldNeighbours[(pos << 1) + 1][0].setMvField(miLeft.mv[1], miLeft.miRefIdx[1]);
2406
0
      }
2407
0
      pos++;
2408
0
    }
2409
2410
0
    isAvailableSubPu = getInterMergeSubPuMvpCand( cu, affMrgCtx, pos );
2411
2412
0
    if( isAvailableSubPu )
2413
0
    {
2414
0
      affMrgCtx.mergeType[affMrgCtx.numValidMergeCand] = MRG_TYPE_SUBPU_ATMVP;
2415
2416
0
      affMrgCtx.numValidMergeCand++;
2417
2418
0
      if( affMrgCtx.numValidMergeCand == mrgCandIdx + 1 )
2419
0
      {
2420
0
        return;
2421
0
      }
2422
2423
      // early termination
2424
0
      if( affMrgCtx.numValidMergeCand == maxNumAffineMergeCand )
2425
0
      {
2426
0
        return;
2427
0
      }
2428
0
    }
2429
0
  }
2430
2431
0
  if ( slice.getSPS()->getUseAffine() )
2432
0
  {
2433
    ///> Start: inherited affine candidates
2434
0
    const CodingUnit* npu[5];
2435
0
    int numAffNeighLeft = getAvailableAffineNeighboursForLeftPredictor( cu, npu );
2436
0
    int numAffNeigh     = getAvailableAffineNeighboursForAbovePredictor( cu, npu, numAffNeighLeft );
2437
0
    for( int idx = 0; idx < numAffNeigh; idx++ )
2438
0
    {
2439
      // derive Mv from Neigh affine PU
2440
0
      Mv cMv[2][3];
2441
0
      const CodingUnit* puNeigh = npu[idx];
2442
2443
0
      if( puNeigh->interDir() != 2 )
2444
0
      {
2445
0
        xInheritedAffineMv( cu, puNeigh->affineType() == AFFINEMODEL_6PARAM, puNeigh, REF_PIC_LIST_0, cMv[0] );
2446
0
      }
2447
2448
0
      if( slice.isInterB() )
2449
0
      {
2450
0
        if( puNeigh->interDir() != 1 )
2451
0
        {
2452
0
          xInheritedAffineMv( cu, puNeigh->affineType() == AFFINEMODEL_6PARAM, puNeigh, REF_PIC_LIST_1, cMv[1] );
2453
0
        }
2454
0
      }
2455
2456
0
      for( int mvNum = 0; mvNum < 3; mvNum++ )
2457
0
      {
2458
0
        affMrgCtx.mvFieldNeighbours[(affMrgCtx.numValidMergeCand << 1) + 0][mvNum].setMvField( cMv[0][mvNum], puNeigh->refIdx[0] );
2459
0
        affMrgCtx.mvFieldNeighbours[(affMrgCtx.numValidMergeCand << 1) + 1][mvNum].setMvField( cMv[1][mvNum], puNeigh->refIdx[1] );
2460
0
      }
2461
2462
0
      affMrgCtx.interDirNeighbours[affMrgCtx.numValidMergeCand] = puNeigh->interDir();
2463
0
      affMrgCtx.affineType[affMrgCtx.numValidMergeCand]         = puNeigh->affineType();
2464
0
      affMrgCtx.BcwIdx[affMrgCtx.numValidMergeCand]             = puNeigh->BcwIdx();
2465
2466
0
      if( affMrgCtx.numValidMergeCand == mrgCandIdx )
2467
0
      {
2468
0
        return;
2469
0
      }
2470
2471
      // early termination
2472
0
      affMrgCtx.numValidMergeCand++;
2473
0
      if( affMrgCtx.numValidMergeCand == maxNumAffineMergeCand )
2474
0
      {
2475
0
        return;
2476
0
      }
2477
0
    }
2478
    ///> End: inherited affine candidates
2479
2480
    ///> Start: Constructed affine candidates
2481
0
    {
2482
0
      MotionInfo mi[4];
2483
0
      bool isAvailable[4] = { false };
2484
0
      int8_t neighBcw[2] = { BCW_DEFAULT, BCW_DEFAULT };
2485
      // control point: LT B2->B3->A2
2486
0
      const Position posLT[3] = { cu.Y().topLeft().offset( -1, -1 ), cu.Y().topLeft().offset( 0, -1 ), cu.Y().topLeft().offset( -1, 0 ) };
2487
0
      const CodingUnit* guess[3] = { cu.left ? cu.left : cu.above, cu.above, cu.left };
2488
0
      for ( int i = 0; i < 3; i++ )
2489
0
      {
2490
0
        const Position pos = posLT[i];
2491
0
        const CodingUnit* cuNeigh = cs.getCURestricted( pos, cu, CH_L, guess[i] );
2492
2493
0
        if( cuNeigh && CU::isInter( *cuNeigh ) && PU::isDiffMER( cu.lumaPos(), pos, plevel ) )
2494
0
        {
2495
0
          isAvailable[0] = true;
2496
0
          mi[0] = cuNeigh->getMotionInfo( pos );
2497
0
          neighBcw[0] = cuNeigh->BcwIdx();
2498
0
          break;
2499
0
        }
2500
0
      }
2501
2502
      // control point: RT B1->B0
2503
0
      const Position posRT[2] = { cu.Y().topRight().offset( 0, -1 ), cu.Y().topRight().offset( 1, -1 ) };
2504
0
      for ( int i = 0; i < 2; i++ )
2505
0
      {
2506
0
        const Position pos = posRT[i];
2507
0
        const CodingUnit* cuNeigh = cs.getCURestricted( pos, cu, CH_L, cu.above );
2508
2509
0
        if( cuNeigh && CU::isInter( *cuNeigh ) && PU::isDiffMER( cu.lumaPos(), pos, plevel ) )
2510
0
        {
2511
0
          isAvailable[1] = true;
2512
0
          mi[1] = cuNeigh->getMotionInfo( pos );
2513
0
          neighBcw[1] = cuNeigh->BcwIdx();
2514
0
          break;
2515
0
        }
2516
0
      }
2517
2518
      // control point: LB A1->A0
2519
0
      const Position posLB[2] = { cu.Y().bottomLeft().offset( -1, 0 ), cu.Y().bottomLeft().offset( -1, 1 ) };
2520
0
      for ( int i = 0; i < 2; i++ )
2521
0
      {
2522
0
        const Position pos = posLB[i];
2523
0
        const CodingUnit* cuNeigh = cs.getCURestricted( pos, cu, CH_L, cu.left );
2524
2525
0
        if( cuNeigh && CU::isInter( *cuNeigh ) && PU::isDiffMER( cu.lumaPos(), pos, plevel ) )
2526
0
        {
2527
0
          isAvailable[2] = true;
2528
0
          mi[2] = cuNeigh->getMotionInfo( pos );
2529
0
          break;
2530
0
        }
2531
0
      }
2532
2533
      // control point: RB
2534
0
      if ( slice.getPicHeader()->getEnableTMVPFlag() )
2535
0
      {
2536
        //>> MTK colocated-RightBottom
2537
        // offset the pos to be sure to "point" to the same position the uiAbsPartIdx would've pointed to
2538
0
        Position posRB = cu.Y().bottomRight().offset( -3, -3 );
2539
2540
0
        const PreCalcValues& pcv = *cs.pcv;
2541
0
        Position posC0;
2542
0
        bool C0Avail = false;
2543
2544
0
        bool boundaryCond = ((posRB.x + pcv.minCUWidth) < pcv.lumaWidth) && ((posRB.y + pcv.minCUHeight) < pcv.lumaHeight);
2545
0
        const SubPic& curSubPic = cu.pps->getSubPicFromPos( cu.lumaPos() );
2546
0
        if( curSubPic.getTreatedAsPicFlag() )
2547
0
        {
2548
0
          boundaryCond = ((posRB.x + pcv.minCUWidth) <= curSubPic.getSubPicRight() &&
2549
0
            (posRB.y + pcv.minCUHeight) <= curSubPic.getSubPicBottom());
2550
0
        }
2551
0
        if( boundaryCond )
2552
0
        {
2553
0
          Position posInCtu( posRB.x & pcv.maxCUWidthMask, posRB.y & pcv.maxCUHeightMask );
2554
2555
0
          if ( (posInCtu.x + 4 < pcv.maxCUWidth) &&  // is not at the last column of CTU
2556
0
            (posInCtu.y + 4 < pcv.maxCUHeight) )     // is not at the last row    of CTU
2557
0
          {
2558
0
            posC0 = posRB.offset( 4, 4 );
2559
0
            C0Avail = true;
2560
0
          }
2561
0
          else if ( posInCtu.x + 4 < pcv.maxCUWidth ) // is not at the last column of CTU But is last row of CTU
2562
0
          {
2563
0
            posC0 = posRB.offset( 4, 4 );
2564
            // in the reference the CTU address is not set - thus probably resulting in no using this C0 possibility
2565
0
          }
2566
0
          else if ( posInCtu.y + 4 < pcv.maxCUHeight ) // is not at the last row of CTU But is last column of CTU
2567
0
          {
2568
0
            posC0 = posRB.offset( 4, 4 );
2569
0
            C0Avail = true;
2570
0
          }
2571
0
          else //is the right bottom corner of CTU
2572
0
          {
2573
0
            posC0 = posRB.offset( 4, 4 );
2574
            // same as for last column but not last row
2575
0
          }
2576
0
        }
2577
2578
0
        Mv        cColMv;
2579
0
        int       refIdx = 0;
2580
0
        bool      bExistMV = C0Avail && getColocatedMVP( cu, REF_PIC_LIST_0, posC0, cColMv, refIdx, false );
2581
0
        if ( bExistMV )
2582
0
        {
2583
0
          mi[3].mv[0] = cColMv;
2584
0
          mi[3].miRefIdx[0] = refIdx;
2585
0
          isAvailable[3] = true;
2586
0
        }
2587
2588
0
        if ( slice.isInterB() )
2589
0
        {
2590
0
          bExistMV = C0Avail && getColocatedMVP( cu, REF_PIC_LIST_1, posC0, cColMv, refIdx, false );
2591
0
          if ( bExistMV )
2592
0
          {
2593
0
            mi[3].mv[1] = cColMv;
2594
0
            mi[3].miRefIdx[1] = refIdx;
2595
0
            isAvailable[3] = true;
2596
0
          }
2597
0
        }
2598
0
      }
2599
2600
      //-------------------  insert model  -------------------//
2601
0
      int order[6] = { 0, 1, 2, 3, 4, 5 };
2602
0
      int modelNum = 6;
2603
0
      int model[6][4] = {
2604
0
        { 0, 1, 2 },          // 0:  LT, RT, LB
2605
0
        { 0, 1, 3 },          // 1:  LT, RT, RB
2606
0
        { 0, 2, 3 },          // 2:  LT, LB, RB
2607
0
        { 1, 2, 3 },          // 3:  RT, LB, RB
2608
0
        { 0, 1 },             // 4:  LT, RT
2609
0
        { 0, 2 },             // 5:  LT, LB
2610
0
      };
2611
2612
0
      int verNum[6] = { 3, 3, 3, 3, 2, 2 };
2613
0
      int startIdx = cu.sps->getUseAffineType() ? 0 : 4;
2614
0
      for ( int idx = startIdx; idx < modelNum; idx++ )
2615
0
      {
2616
0
        int modelIdx = order[idx];
2617
0
        getAffineControlPointCand( cu, mi, isAvailable, model[modelIdx], modelIdx == 3 ? neighBcw[1] : neighBcw[0], modelIdx, verNum[modelIdx], affMrgCtx );
2618
0
        if ( affMrgCtx.numValidMergeCand != 0 && affMrgCtx.numValidMergeCand - 1 == mrgCandIdx )
2619
0
        {
2620
0
          return;
2621
0
        }
2622
2623
        // early termination
2624
0
        if ( affMrgCtx.numValidMergeCand == maxNumAffineMergeCand )
2625
0
        {
2626
0
          return;
2627
0
        }
2628
0
      }
2629
0
    }
2630
    ///> End: Constructed affine candidates
2631
0
  }
2632
2633
  ///> zero padding
2634
0
  int cnt = affMrgCtx.numValidMergeCand;
2635
0
  while ( cnt < maxNumAffineMergeCand )
2636
0
  {
2637
0
    for ( int mvNum = 0; mvNum < 3; mvNum++ )
2638
0
    {
2639
0
      affMrgCtx.mvFieldNeighbours[(cnt << 1) + 0][mvNum].setMvField( Mv( 0, 0 ), 0 );
2640
0
    }
2641
0
    affMrgCtx.interDirNeighbours[cnt] = 1;
2642
2643
0
    if ( slice.isInterB() )
2644
0
    {
2645
0
      for ( int mvNum = 0; mvNum < 3; mvNum++ )
2646
0
      {
2647
0
        affMrgCtx.mvFieldNeighbours[(cnt << 1) + 1][mvNum].setMvField( Mv( 0, 0 ), 0 );
2648
0
      }
2649
0
      affMrgCtx.interDirNeighbours[cnt] = 3;
2650
0
    }
2651
0
    affMrgCtx.affineType[cnt] = AFFINEMODEL_4PARAM;
2652
0
    cnt++;
2653
2654
0
    if ( cnt == maxNumAffineMergeCand )
2655
0
    {
2656
0
      return;
2657
0
    }
2658
0
  }
2659
0
}
2660
2661
void PU::setAllAffineMvField( CodingUnit &cu, MvField *mvField, RefPicList eRefList )
2662
0
{
2663
  // Set RefIdx
2664
0
  CHECK( mvField[0].mfRefIdx != mvField[1].mfRefIdx || mvField[0].mfRefIdx != mvField[2].mfRefIdx, "Affine mv corners don't have the same refIdx." );
2665
0
  cu.refIdx[eRefList] = mvField[0].mfRefIdx;
2666
2667
0
  setAllAffineMv( cu, mvField[0].mv, mvField[1].mv, mvField[2].mv, eRefList );
2668
0
}
2669
2670
void PU::setAllAffineMv( CodingUnit& cu, Mv affLT, Mv affRT, Mv affLB, RefPicList eRefList, bool clipCPMVs )
2671
0
{
2672
0
        int width  = cu.lwidth();
2673
0
        int height = cu.lheight();
2674
0
  const int shift  = MAX_CU_DEPTH;
2675
2676
0
  if( clipCPMVs )
2677
0
  {
2678
0
    affLT.mvCliptoStorageBitDepth();
2679
0
    affRT.mvCliptoStorageBitDepth();
2680
0
    if( cu.affineType() == AFFINEMODEL_6PARAM )
2681
0
    {
2682
0
      affLB.mvCliptoStorageBitDepth();
2683
0
    }
2684
0
  }
2685
2686
0
  int deltaMvHorX, deltaMvHorY, deltaMvVerX, deltaMvVerY;
2687
2688
0
  deltaMvHorX = ( affRT - affLT ).getHor() *(1<< ( shift - getLog2( width )) );
2689
0
  deltaMvHorY = ( affRT - affLT ).getVer() *(1<< ( shift - getLog2( width )) );
2690
2691
0
  if ( cu.affineType() == AFFINEMODEL_6PARAM )
2692
0
  {
2693
0
    deltaMvVerX = ( affLB - affLT ).getHor() *(1<< ( shift - getLog2( height )) );
2694
0
    deltaMvVerY = ( affLB - affLT ).getVer() *(1<< ( shift - getLog2( height )) );
2695
0
  }
2696
0
  else
2697
0
  {
2698
0
    deltaMvVerX = -deltaMvHorY;
2699
0
    deltaMvVerY =  deltaMvHorX;
2700
0
  }
2701
2702
0
  const int mvScaleHor = affLT.getHor() *(1<< shift);
2703
0
  const int mvScaleVer = affLT.getVer() *(1<< shift);
2704
2705
0
  MotionBuf mb = cu.getMotionBuf();
2706
2707
0
  const bool subblkMVSpreadOverLimit = InterPrediction::isSubblockVectorSpreadOverLimit( deltaMvHorX, deltaMvHorY, deltaMvVerX, deltaMvVerY, cu.interDir() );
2708
2709
0
  static_assert( AFFINE_MIN_BLOCK_SIZE ==   4,                  "" );
2710
0
  static_assert( AFFINE_MIN_BLOCK_SIZE == ( 1 << MIN_CU_LOG2 ), "" );
2711
2712
0
  const int halfBH = 2;
2713
2714
  // fallback motion vector
2715
0
  Mv flbMv( 0, 0 );
2716
2717
0
  if( subblkMVSpreadOverLimit )
2718
0
  {
2719
0
    flbMv.hor = mvScaleHor + deltaMvHorX * ( width >> 1 ) + deltaMvVerX * ( height >> 1 );
2720
0
    flbMv.ver = mvScaleVer + deltaMvHorY * ( width >> 1 ) + deltaMvVerY * ( height >> 1 );
2721
2722
0
    roundAffineMv( flbMv.hor, flbMv.ver, shift );
2723
0
    flbMv.clipToStorageBitDepth();
2724
0
  }
2725
2726
0
  width  >>= MIN_CU_LOG2;
2727
0
  height >>= MIN_CU_LOG2;
2728
2729
#if ENABLE_SIMD_OPT && defined( TARGET_SIMD_X86 ) && 0 // TODO: disable inline intrinsics for now
2730
  if( !subblkMVSpreadOverLimit && read_x86_extension_flags() > x86_simd::SCALAR )
2731
  {
2732
    __m128i xvbase = _mm_setr_epi32( mvScaleHor, mvScaleVer, mvScaleHor, mvScaleVer );
2733
    __m128i xvdvxy = _mm_setr_epi32( deltaMvVerX, deltaMvVerY, deltaMvVerX, deltaMvVerY );
2734
    __m128i xhdhxy = _mm_setr_epi32( deltaMvHorX, deltaMvHorY, deltaMvHorX, deltaMvHorY );
2735
2736
    for( int h = 0; h < height; h++ )
2737
    {
2738
      __m128i
2739
        xvoff = _mm_set1_epi32( halfBH + ( h << MIN_CU_LOG2 ) );
2740
      xvoff = _mm_mullo_epi32( xvoff, xvdvxy );
2741
      xvoff = _mm_add_epi32( xvoff, xvbase );
2742
2743
      for( int w = 0; w < width; w += 2 )
2744
      {
2745
        MotionInfo* mi = &mb.at( w, h );
2746
2747
        __m128i
2748
          xhoff = _mm_set1_epi32( 2 + ( w << MIN_CU_LOG2 ) );
2749
        xhoff = _mm_add_epi32( xhoff, _mm_setr_epi32( 0, 0, 1 << MIN_CU_LOG2, 1 << MIN_CU_LOG2 ) );
2750
        xhoff = _mm_mullo_epi32( xhoff, xhdhxy );
2751
        xhoff = _mm_add_epi32( xhoff, xvoff );
2752
        __m128i
2753
          xmv = _mm_add_epi32( xhoff, _mm_set1_epi32( 1 << ( shift - 1 ) ) );
2754
        xmv = _mm_add_epi32( xmv, _mm_cmpgt_epi32( xhoff, _mm_set1_epi32( -1 ) ) );
2755
        xmv = _mm_srai_epi32( xmv, shift );
2756
        xmv = _mm_max_epi32( _mm_set1_epi32( -( 1 << 17 ) ), _mm_min_epi32( _mm_set1_epi32( ( 1 << 17 ) - 1 ), xmv ) );
2757
2758
        _mm_storeu_si64( ( __m128i* ) & mi[0].mv[eRefList], xmv );
2759
        _mm_storeu_si64( ( __m128i* ) & mi[1].mv[eRefList], _mm_unpackhi_epi64( xmv, _mm_setzero_si128() ) );
2760
      }
2761
    }
2762
  }
2763
  else
2764
#endif
2765
0
  {
2766
0
    for( int h = 0; h < height; h++ )
2767
0
    {
2768
0
      if( subblkMVSpreadOverLimit )
2769
0
      {
2770
0
        for( int w = 0; w < width; w++ )
2771
0
        {
2772
0
          MotionInfo &mi = mb.at( w, h );
2773
2774
0
          mi.mv[eRefList] = flbMv;
2775
0
        }
2776
0
      }
2777
0
      else
2778
0
      {
2779
0
        for( int w = 0; w < width; w++ )
2780
0
        {
2781
0
          MotionInfo &mi = mb.at( w, h );
2782
2783
0
          int mvHor = mvScaleHor + deltaMvHorX * ( 2 + ( w << MIN_CU_LOG2 ) ) + deltaMvVerX * ( halfBH + ( h << MIN_CU_LOG2 ) );
2784
0
          int mvVer = mvScaleVer + deltaMvHorY * ( 2 + ( w << MIN_CU_LOG2 ) ) + deltaMvVerY * ( halfBH + ( h << MIN_CU_LOG2 ) );
2785
2786
0
          roundAffineMv( mvHor, mvVer, shift );
2787
2788
0
          Mv rndMv( mvHor, mvVer );
2789
0
          rndMv.clipToStorageBitDepth();
2790
2791
0
          mi.mv[eRefList] = rndMv;
2792
0
        }
2793
0
      }
2794
0
    }
2795
0
  }
2796
2797
0
  cu.mv[eRefList][0] = affLT;
2798
0
  cu.mv[eRefList][1] = affRT;
2799
0
  cu.mv[eRefList][2] = affLB;
2800
0
}
2801
2802
void clipColPos(int& posX, int& posY, const CodingUnit& cu)
2803
0
{
2804
0
  Position puPos = cu.lumaPos();
2805
0
  int log2CtuSize = getLog2(cu.sps->getCTUSize());
2806
0
  int ctuX = ((puPos.x >> log2CtuSize) << log2CtuSize);
2807
0
  int ctuY = ((puPos.y >> log2CtuSize) << log2CtuSize);
2808
0
  int horMax;
2809
0
  const SubPic& curSubPic = cu.pps->getSubPicFromPos( puPos );
2810
0
  if( curSubPic.getTreatedAsPicFlag() )
2811
0
  {
2812
0
    horMax = std::min((int)curSubPic.getSubPicRight(), ctuX + (int)cu.sps->getCTUSize() + 3);
2813
0
  }
2814
0
  else
2815
0
  {
2816
0
    horMax = std::min((int)cu.pps->getPicWidthInLumaSamples() - 1, ctuX + (int)cu.sps->getCTUSize() + 3);
2817
0
  }
2818
0
  int horMin = std::max((int)0, ctuX);
2819
0
  int verMax = std::min( (int)cu.pps->getPicHeightInLumaSamples() - 1, ctuY + (int)cu.sps->getCTUSize() - 1 );
2820
0
  int verMin = std::max((int)0, ctuY);
2821
2822
0
  posX = std::min(horMax, std::max(horMin, posX));
2823
0
  posY = std::min(verMax, std::max(verMin, posY));
2824
0
}
2825
2826
bool PU::getInterMergeSubPuMvpCand(const CodingUnit &cu, AffineMergeCtx& mrgCtx, const int count )
2827
0
{
2828
0
  const Slice   &slice = *cu.slice;
2829
0
  const unsigned scale = 4 * std::max<int>(1, 4 * AMVP_DECIMATION_FACTOR / 4);
2830
0
  const unsigned mask  = ~(scale - 1);
2831
2832
0
  const Picture *pColPic = slice.getRefPic(RefPicList(slice.isInterB() ? 1 - slice.getColFromL0Flag() : 0), slice.getColRefIdx());
2833
0
  Mv cTMv;
2834
2835
0
  if( count )
2836
0
  {
2837
0
    if(                          ( mrgCtx.interDirNeighbours[0] & ( 1 << REF_PIC_LIST_0 ) ) && slice.getRefPic( REF_PIC_LIST_0, mrgCtx.mvFieldNeighbours[REF_PIC_LIST_0][0].mfRefIdx) == pColPic )
2838
0
    {
2839
0
      cTMv = mrgCtx.mvFieldNeighbours[REF_PIC_LIST_0][0].mv;
2840
0
    }
2841
0
    else if( slice.isInterB() && ( mrgCtx.interDirNeighbours[0] & ( 1 << REF_PIC_LIST_1 ) ) && slice.getRefPic( REF_PIC_LIST_1, mrgCtx.mvFieldNeighbours[REF_PIC_LIST_1][0].mfRefIdx) == pColPic )
2842
0
    {
2843
0
      cTMv = mrgCtx.mvFieldNeighbours[REF_PIC_LIST_1][0].mv;
2844
0
    }
2845
0
  }
2846
2847
  ///////////////////////////////////////////////////////////////////////
2848
  ////////          GET Initial Temporal Vector                  ////////
2849
  ///////////////////////////////////////////////////////////////////////
2850
0
  Mv cTempVector    = cTMv;
2851
2852
  // compute the location of the current PU
2853
0
  Position puPos  = cu.lumaPos();
2854
0
  Size     puSize = cu.lumaSize();
2855
0
  static constexpr int puHeight    = 1 << ATMVP_SUB_BLOCK_SIZE;
2856
0
  static constexpr int puWidth     = 1 << ATMVP_SUB_BLOCK_SIZE;
2857
2858
0
  Mv cColMv;
2859
  // use coldir.
2860
0
  bool    bBSlice = slice.isInterB();
2861
2862
0
  Position centerPos;
2863
2864
0
  bool found  = false;
2865
0
  cTempVector = cTMv;
2866
2867
0
  cTempVector.changePrecision(MV_PRECISION_INTERNAL, MV_PRECISION_INT);
2868
0
  int tempX = cTempVector.getHor();
2869
0
  int tempY = cTempVector.getVer();
2870
2871
0
  centerPos.x = puPos.x + (puSize.width  >> 1) + tempX;
2872
0
  centerPos.y = puPos.y + (puSize.height >> 1) + tempY;
2873
2874
0
  clipColPos( centerPos.x, centerPos.y, cu );
2875
2876
0
  centerPos.x &= mask;
2877
0
  centerPos.y &= mask;
2878
2879
  // derivation of center motion parameters from the collocated CU
2880
0
  const Slice* pColSlice;
2881
0
  const ColocatedMotionInfo&
2882
0
               mi = pColPic->cs->getColInfo( centerPos, pColSlice );
2883
2884
0
  if (mi.isInter())
2885
0
  {
2886
0
    mrgCtx.interDirNeighbours[count] = 0;
2887
2888
0
    for (unsigned currRefListId = 0; currRefListId < (bBSlice ? 2 : 1); currRefListId++)
2889
0
    {
2890
0
      RefPicList  currRefPicList = RefPicList(currRefListId);
2891
2892
0
      if( getColocatedMVP( cu, currRefPicList, centerPos, cColMv, 0, true ) )
2893
0
      {
2894
        // set as default, for further motion vector field spanning
2895
0
        mrgCtx.mvFieldNeighbours[(count << 1) + currRefListId][0].setMvField(cColMv, 0);
2896
0
        mrgCtx.interDirNeighbours[count] |= (1 << currRefListId);
2897
0
        mrgCtx.BcwIdx[count] = BCW_DEFAULT;
2898
0
        found = true;
2899
0
      }
2900
0
      else
2901
0
      {
2902
0
        mrgCtx.mvFieldNeighbours[(count << 1) + currRefListId][0].setMvField(Mv(), MF_NOT_VALID);
2903
0
        mrgCtx.interDirNeighbours[count] &= ~(1 << currRefListId);
2904
0
      }
2905
0
    }
2906
0
  }
2907
2908
0
  if( !found )
2909
0
  {
2910
0
    return false;
2911
0
  }
2912
0
  else if( cu.mergeIdx() > 0 )
2913
0
  {
2914
0
    return true;
2915
0
  }
2916
2917
0
  if( true )
2918
0
  {
2919
0
    int xOff = (puWidth >> 1) + tempX;
2920
0
    int yOff = (puHeight >> 1) + tempY;
2921
2922
0
    MotionBuf& mb = mrgCtx.subPuMvpMiBuf;
2923
2924
0
    const bool isBiPred = isBipredRestriction(cu);
2925
2926
0
    MotionInfo mi;
2927
2928
0
    MotionInfo* miPtr = mb.buf;
2929
2930
0
    for( int y = puPos.y; y < puPos.y + puSize.height; y += puHeight, miPtr += g_miScaling.scaleVer( puHeight ) * mb.stride )
2931
0
    {
2932
0
      MotionInfo* miLinePtr = miPtr;
2933
0
      for( int x = puPos.x; x < puPos.x + puSize.width; x += puWidth, miLinePtr += g_miScaling.scaleHor( puWidth ) )
2934
0
      {
2935
0
        mi.miRefIdx[0] = mi.miRefIdx[1] = MI_NOT_VALID;
2936
0
        memset( NO_WARNING_class_memaccess( mi.mv ), 0, sizeof( mi.mv ) );
2937
2938
0
        Position colPos{ x + xOff, y + yOff };
2939
2940
0
        clipColPos( colPos.x, colPos.y, cu );
2941
2942
0
        const Slice* pColSlice;
2943
0
        const ColocatedMotionInfo&
2944
0
                     colMi = pColPic->cs->getColInfo( colPos, pColSlice );
2945
0
        bool found = false;
2946
2947
0
        if (colMi.isInter())
2948
0
        {
2949
0
          for (unsigned currRefListId = 0; currRefListId < (!isBiPred && bBSlice ? 2 : 1); currRefListId++)
2950
0
          {
2951
0
            RefPicList currRefPicList = RefPicList(currRefListId);
2952
0
            if( getColocatedMVP( cu, currRefPicList, colPos, cColMv, 0, true ) )
2953
0
            {
2954
0
              mi.miRefIdx[currRefListId] = 0;
2955
0
              mi.mv[currRefListId] = cColMv;
2956
0
              found = true;
2957
0
            }
2958
0
          }
2959
0
        }
2960
        
2961
0
        if( !found )
2962
0
        {
2963
          // intra coded, in this case, no motion vector is available for list 0 or list 1, so use default
2964
0
          mi.mv[0] = mrgCtx.mvFieldNeighbours[(count << 1) + 0][0].mv;
2965
0
          mi.mv[1] = mrgCtx.mvFieldNeighbours[(count << 1) + 1][0].mv;
2966
0
          mi.miRefIdx[0] = mrgCtx.mvFieldNeighbours[(count << 1) + 0][0].mfRefIdx;
2967
0
          mi.miRefIdx[1] = mrgCtx.mvFieldNeighbours[(count << 1) + 1][0].mfRefIdx;
2968
0
        }
2969
2970
0
        if( isBiPred && mi.interDir() == 3 )
2971
0
        {
2972
0
          mi.mv[1]      = Mv();
2973
0
          mi.miRefIdx[1]  = MI_NOT_VALID;
2974
0
        }
2975
2976
0
        *  miLinePtr                   = mi;
2977
0
        *( miLinePtr             + 1 ) = mi;
2978
0
        *( miLinePtr + mb.stride     ) = mi;
2979
0
        *( miLinePtr + mb.stride + 1 ) = mi;
2980
0
      }
2981
0
    }
2982
0
  }
2983
0
  return true;
2984
0
}
2985
2986
void PU::spanMotionInfo( CodingUnit &cu )
2987
0
{
2988
0
  MotionBuf mb = cu.getMotionBuf();
2989
2990
0
  if( !cu.mergeFlag() || cu.mergeType() == MRG_TYPE_DEFAULT_N || cu.mergeType() == MRG_TYPE_IBC )
2991
0
  {
2992
0
    MotionInfo mi;
2993
2994
0
    bool isIbc = CU::isIBC( cu );
2995
2996
0
    for( int i = 0; i < NUM_REF_PIC_LIST_01; i++ )
2997
0
    {
2998
0
      mi.mv[i]       = cu.mv[i][0];
2999
0
      mi.miRefIdx[i] = isIbc ? MI_NOT_VALID : cu.refIdx[i];
3000
0
    }
3001
3002
0
    if( cu.affineFlag() )
3003
0
    {
3004
0
      for( int y = 0; y < mb.height; y++ )
3005
0
      {
3006
0
        for( int x = 0; x < mb.width; x++ )
3007
0
        {
3008
0
          MotionInfo &dest  = mb.at( x, y );
3009
3010
0
          for( int i = 0; i < NUM_REF_PIC_LIST_01; i++ )
3011
0
          {
3012
0
            if( isMotionInvalid( mi.miRefIdx[i], MI_NOT_VALID ) )
3013
0
            {
3014
0
              dest.mv[i] = Mv();
3015
0
            }
3016
0
            dest.miRefIdx[i] = mi.miRefIdx[i];
3017
0
          }
3018
0
        }
3019
0
      }
3020
0
    }
3021
0
    else
3022
0
    {
3023
0
      mb.fill( mi );
3024
0
    }
3025
0
  }
3026
0
  else
3027
0
  {
3028
    // cu.mergeType() == MRG_TYPE_SUBPU_ATMVP
3029
    // already done
3030
0
  }
3031
0
}
3032
3033
void PU::applyImv( CodingUnit& cu, MotionHist& hist )
3034
0
{
3035
0
  CHECKD( cu.mergeFlag(), "IMV should never be applied to merge!" );
3036
3037
0
  Mv mvd;
3038
3039
0
  if( cu.interDir() != 2 /* PRED_L1 */ )
3040
0
  {
3041
0
    mvd = cu.mv[0][0];
3042
0
    mvd.changePrecisionAmvr( cu.imv(), MV_PRECISION_INTERNAL );
3043
0
    unsigned mvp_idx = cu.mvpIdx[0];
3044
0
    AMVPInfo amvpInfo;
3045
3046
0
    if( CU::isIBC( cu ) )
3047
0
      PU::fillIBCMvpCand( cu, amvpInfo, hist );
3048
0
    else
3049
0
      PU::fillMvpCand( cu, REF_PIC_LIST_0, cu.refIdx[0], amvpInfo, hist );
3050
3051
0
    cu.mvpIdx[0]    = mvp_idx;
3052
0
    cu.mv    [0][0] = amvpInfo.mvCand[mvp_idx] + mvd;
3053
0
    cu.mv    [0][0] . mvCliptoStorageBitDepth();
3054
0
  }
3055
3056
0
  if( cu.interDir() != 1 /* PRED_L0 */ )
3057
0
  {
3058
0
    mvd = cu.mv[1][0];
3059
3060
0
    if( !( cu.cs->picHeader->getMvdL1ZeroFlag() && cu.interDir() == 3 ) && cu.imv() )/* PRED_BI */
3061
0
    {
3062
0
      mvd.changePrecisionAmvr( cu.imv(), MV_PRECISION_INTERNAL );
3063
0
    }
3064
3065
0
    unsigned mvp_idx = cu.mvpIdx[1];
3066
0
    AMVPInfo amvpInfo;
3067
0
    PU::fillMvpCand( cu, REF_PIC_LIST_1, cu.refIdx[1], amvpInfo, hist );
3068
0
    cu.mvpIdx[1]    = mvp_idx;
3069
0
    cu.mv    [1][0] = amvpInfo.mvCand[mvp_idx] + mvd;
3070
0
    cu.mv    [1][0] . mvCliptoStorageBitDepth();
3071
0
  }
3072
0
}
3073
3074
3075
bool PU::isBiPredFromDifferentDirEqDistPoc( const CodingUnit& cu )
3076
0
{
3077
0
  if( cu.refIdx[0] >= 0 && cu.refIdx[1] >= 0 )
3078
0
  {
3079
0
    if( cu.slice->getIsUsedAsLongTerm( REF_PIC_LIST_0, cu.refIdx[0] ) || cu.slice->getIsUsedAsLongTerm( REF_PIC_LIST_1, cu.refIdx[1] ) )
3080
0
    {
3081
0
      return false;
3082
0
    }
3083
0
    const int poc0 = cu.slice->getRefPOC( REF_PIC_LIST_0, cu.refIdx[0] );
3084
0
    const int poc1 = cu.slice->getRefPOC( REF_PIC_LIST_1, cu.refIdx[1] );
3085
0
    const int poc  = cu.slice->getPOC();
3086
3087
0
    return ( poc - poc0 ) == ( poc1 - poc );
3088
0
  }
3089
0
  return false;
3090
0
}
3091
3092
void PU::restrictBiPredMergeCandsOne( CodingUnit &cu )
3093
0
{
3094
0
  if( PU::isBipredRestriction( cu ) )
3095
0
  {
3096
0
    if( cu.interDir() == 3 )
3097
0
    {
3098
0
      cu.setInterDir(  1 );
3099
0
      cu.refIdx[1]  = -1;
3100
0
      cu.mv[1][0]   = Mv( 0, 0 );
3101
0
      cu.setBcwIdx  ( BCW_DEFAULT );
3102
0
    }
3103
0
  }
3104
0
}
3105
3106
void PU::getGeoMergeCandidates( const CodingUnit &cu, MergeCtx& geoMrgCtx, MotionHist& hist )
3107
0
{
3108
0
  MergeCtx tmpMergeCtx;
3109
3110
0
  const Slice &slice = *cu.slice;
3111
0
  const uint32_t maxNumMergeCand = slice.getSPS()->getMaxNumMergeCand();
3112
3113
0
  geoMrgCtx.numValidMergeCand = 0;
3114
3115
0
  for (int32_t i = 0; i < GEO_MAX_NUM_UNI_CANDS; i++)
3116
0
  {
3117
0
    geoMrgCtx.BcwIdx[i] = BCW_DEFAULT;
3118
0
    geoMrgCtx.interDirNeighbours[i] = 0;
3119
0
    geoMrgCtx.mrgTypeNeighbours[i] = MRG_TYPE_DEFAULT_N;
3120
0
    geoMrgCtx.mvFieldNeighbours[(i << 1)].mfRefIdx = MF_NOT_VALID;
3121
0
    geoMrgCtx.mvFieldNeighbours[(i << 1) + 1].mfRefIdx = MF_NOT_VALID;
3122
0
    geoMrgCtx.mvFieldNeighbours[(i << 1)].mv = Mv();
3123
0
    geoMrgCtx.mvFieldNeighbours[(i << 1) + 1].mv = Mv();
3124
0
    geoMrgCtx.useAltHpelIf[i] = false;
3125
0
  }
3126
3127
0
  PU::getInterMergeCandidates(cu, tmpMergeCtx, hist);
3128
3129
0
  for (int32_t i = 0; i < maxNumMergeCand; i++)
3130
0
  {
3131
0
    int parity = i & 1;
3132
0
    if( tmpMergeCtx.interDirNeighbours[i] & (0x01 + parity) )
3133
0
    {
3134
0
      geoMrgCtx.interDirNeighbours[geoMrgCtx.numValidMergeCand] = 1 + parity;
3135
0
      geoMrgCtx.mrgTypeNeighbours[geoMrgCtx.numValidMergeCand] = MRG_TYPE_DEFAULT_N;
3136
0
      geoMrgCtx.mvFieldNeighbours[(geoMrgCtx.numValidMergeCand << 1) + !parity].mv = Mv(0, 0);
3137
0
      geoMrgCtx.mvFieldNeighbours[(geoMrgCtx.numValidMergeCand << 1) + parity].mv = tmpMergeCtx.mvFieldNeighbours[(i << 1) + parity].mv;
3138
0
      geoMrgCtx.mvFieldNeighbours[(geoMrgCtx.numValidMergeCand << 1) + !parity].mfRefIdx = -1;
3139
0
      geoMrgCtx.mvFieldNeighbours[(geoMrgCtx.numValidMergeCand << 1) + parity].mfRefIdx = tmpMergeCtx.mvFieldNeighbours[(i << 1) + parity].mfRefIdx;
3140
0
      geoMrgCtx.numValidMergeCand++;
3141
0
      if (geoMrgCtx.numValidMergeCand == GEO_MAX_NUM_UNI_CANDS)
3142
0
      {
3143
0
        return;
3144
0
      }
3145
0
      continue;
3146
0
    }
3147
3148
0
    if (tmpMergeCtx.interDirNeighbours[i] & (0x02 - parity))
3149
0
    {
3150
0
      geoMrgCtx.interDirNeighbours[geoMrgCtx.numValidMergeCand] = 2 - parity;
3151
0
      geoMrgCtx.mrgTypeNeighbours[geoMrgCtx.numValidMergeCand] = MRG_TYPE_DEFAULT_N;
3152
0
      geoMrgCtx.mvFieldNeighbours[(geoMrgCtx.numValidMergeCand << 1) + !parity].mv = tmpMergeCtx.mvFieldNeighbours[(i << 1) + !parity].mv;
3153
0
      geoMrgCtx.mvFieldNeighbours[(geoMrgCtx.numValidMergeCand << 1) + parity].mv = Mv(0, 0);
3154
0
      geoMrgCtx.mvFieldNeighbours[(geoMrgCtx.numValidMergeCand << 1) + !parity].mfRefIdx = tmpMergeCtx.mvFieldNeighbours[(i << 1) + !parity].mfRefIdx;
3155
0
      geoMrgCtx.mvFieldNeighbours[(geoMrgCtx.numValidMergeCand << 1) + parity].mfRefIdx = -1;
3156
0
      geoMrgCtx.numValidMergeCand++;
3157
0
      if (geoMrgCtx.numValidMergeCand == GEO_MAX_NUM_UNI_CANDS)
3158
0
      {
3159
0
        return;
3160
0
      }
3161
0
    }
3162
0
  }
3163
0
}
3164
3165
void PU::spanGeoMotionInfo( CodingUnit &cu, MergeCtx &geoMrgCtx, const uint8_t splitDir, const uint8_t candIdx0, const uint8_t candIdx1)
3166
0
{
3167
0
  uint8_t off0 = geoMrgCtx.interDirNeighbours[candIdx0] == 1 ? 0 : 1;
3168
0
  uint8_t off1 = geoMrgCtx.interDirNeighbours[candIdx1] == 1 ? 0 : 1;
3169
3170
0
  cu.mv[0][1] = geoMrgCtx.mvFieldNeighbours[( candIdx0 << 1 ) + off0].mv;
3171
0
  cu.mv[1][1] = geoMrgCtx.mvFieldNeighbours[( candIdx1 << 1 ) + off1].mv;
3172
3173
0
  uint8_t val0 = geoMrgCtx.interDirNeighbours[candIdx0];
3174
0
  uint8_t val1 = geoMrgCtx.interDirNeighbours[candIdx1];
3175
0
  val0 <<= 4;
3176
0
  val1 <<= 4;
3177
0
  val0 += geoMrgCtx.mvFieldNeighbours[( candIdx0 << 1 ) + off0].mfRefIdx;
3178
0
  val1 += geoMrgCtx.mvFieldNeighbours[( candIdx1 << 1 ) + off1].mfRefIdx;
3179
3180
0
  cu.setInterDirrefIdxGeo0( val0 );
3181
0
  cu.setInterDirrefIdxGeo1( val1 );
3182
3183
0
  MotionBuf mb = cu.getMotionBuf();
3184
3185
0
  MotionInfo biMv;
3186
3187
0
  if( geoMrgCtx.interDirNeighbours[candIdx0] == 1 && geoMrgCtx.interDirNeighbours[candIdx1] == 2 )
3188
0
  {
3189
0
    biMv.mv[0]     = geoMrgCtx.mvFieldNeighbours[ candIdx0 << 1     ].mv;
3190
0
    biMv.mv[1]     = geoMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].mv;
3191
0
    biMv.miRefIdx[0] = geoMrgCtx.mvFieldNeighbours[ candIdx0 << 1     ].mfRefIdx;
3192
0
    biMv.miRefIdx[1] = geoMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].mfRefIdx;
3193
0
  }
3194
0
  else if( geoMrgCtx.interDirNeighbours[candIdx0] == 2 && geoMrgCtx.interDirNeighbours[candIdx1] == 1 )
3195
0
  {
3196
0
    biMv.mv[0]     = geoMrgCtx.mvFieldNeighbours[ candIdx1 << 1     ].mv;
3197
0
    biMv.mv[1]     = geoMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].mv;
3198
0
    biMv.miRefIdx[0] = geoMrgCtx.mvFieldNeighbours[ candIdx1 << 1     ].mfRefIdx;
3199
0
    biMv.miRefIdx[1] = geoMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].mfRefIdx;
3200
0
  }
3201
0
  else if( geoMrgCtx.interDirNeighbours[candIdx0] == 1 && geoMrgCtx.interDirNeighbours[candIdx1] == 1 )
3202
0
  {
3203
0
    biMv.mv[0] = geoMrgCtx.mvFieldNeighbours[candIdx1 << 1].mv;
3204
0
    biMv.mv[1] = Mv(0, 0);
3205
0
    biMv.miRefIdx[0] = geoMrgCtx.mvFieldNeighbours[candIdx1 << 1].mfRefIdx;
3206
0
    biMv.miRefIdx[1] = MI_NOT_VALID;
3207
0
  }
3208
0
  else if( geoMrgCtx.interDirNeighbours[candIdx0] == 2 && geoMrgCtx.interDirNeighbours[candIdx1] == 2 )
3209
0
  {
3210
0
    biMv.mv[0] = Mv(0, 0);
3211
0
    biMv.mv[1] = geoMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].mv;
3212
0
    biMv.miRefIdx[0] = MI_NOT_VALID;
3213
0
    biMv.miRefIdx[1] = geoMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].mfRefIdx;
3214
0
  }
3215
3216
0
  int16_t angle = g_GeoParams[splitDir][0];
3217
0
  int tpmMask = 0;
3218
0
  int lookUpY = 0, motionIdx = 0;
3219
0
  bool isFlip = angle >= 13 && angle <= 27;
3220
0
  int distanceIdx = g_GeoParams[splitDir][1];
3221
0
  int distanceX = angle;
3222
0
  int distanceY = (distanceX + (GEO_NUM_ANGLES >> 2)) % GEO_NUM_ANGLES;
3223
0
  int offsetX = (-(int)cu.lwidth()) >> 1;
3224
0
  int offsetY = (-(int)cu.lheight()) >> 1;
3225
0
  if (distanceIdx > 0)
3226
0
  {
3227
0
    if (angle % 16 == 8 || (angle % 16 != 0 && cu.lheight() >= cu.lwidth()))
3228
0
      offsetY += angle < 16 ? ((distanceIdx * cu.lheight()) >> 3) : -(int)((distanceIdx * cu.lheight()) >> 3);
3229
0
    else
3230
0
      offsetX += angle < 16 ? ((distanceIdx * cu.lwidth()) >> 3) : -(int)((distanceIdx * cu.lwidth()) >> 3);
3231
0
  }
3232
0
  for (int y = 0; y < mb.height; y++)
3233
0
  {
3234
0
    lookUpY = (((4 * y + offsetY) *2) + 5) * g_Dis[distanceY];
3235
0
    for (int x = 0; x < mb.width; x++)
3236
0
    {
3237
0
      motionIdx = (((4 * x + offsetX) *2) + 5) * g_Dis[distanceX] + lookUpY;
3238
0
      tpmMask = abs(motionIdx) < 32 ? 2 : (motionIdx <= 0 ? (1 - isFlip) : isFlip);
3239
0
      if (tpmMask == 2)
3240
0
      {
3241
0
        mb.at(x, y) = biMv;
3242
0
      }
3243
0
      else if (tpmMask == 0)
3244
0
      {
3245
0
        mb.at(x, y).miRefIdx[0] = geoMrgCtx.mvFieldNeighbours[candIdx0 << 1].mfRefIdx;
3246
0
        mb.at(x, y).miRefIdx[1] = geoMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].mfRefIdx;
3247
0
        mb.at(x, y).mv[0] = geoMrgCtx.mvFieldNeighbours[candIdx0 << 1].mv;
3248
0
        mb.at(x, y).mv[1] = geoMrgCtx.mvFieldNeighbours[(candIdx0 << 1) + 1].mv;
3249
0
      }
3250
0
      else
3251
0
      {
3252
0
        mb.at(x, y).miRefIdx[0] = geoMrgCtx.mvFieldNeighbours[candIdx1 << 1].mfRefIdx;
3253
0
        mb.at(x, y).miRefIdx[1] = geoMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].mfRefIdx;
3254
0
        mb.at(x, y).mv[0] = geoMrgCtx.mvFieldNeighbours[candIdx1 << 1].mv;
3255
0
        mb.at(x, y).mv[1] = geoMrgCtx.mvFieldNeighbours[(candIdx1 << 1) + 1].mv;
3256
0
      }
3257
0
    }
3258
0
  }
3259
0
}
3260
3261
// CU
3262
3263
bool CU::hasSubCUNonZeroMVd( const CodingUnit& cu )
3264
0
{
3265
0
  bool bNonZeroMvd = false;
3266
3267
0
  if( cu.interDir() != 2 /* PRED_L1 */ )
3268
0
  {
3269
0
    bNonZeroMvd |= cu.mv[REF_PIC_LIST_0][0].getHor() != 0;
3270
0
    bNonZeroMvd |= cu.mv[REF_PIC_LIST_0][0].getVer() != 0;
3271
0
  }
3272
0
  if( cu.interDir() != 1 /* PRED_L0 */ )
3273
0
  {
3274
0
    if( !cu.cs->picHeader->getMvdL1ZeroFlag() || cu.interDir() != 3 /* PRED_BI */ )
3275
0
    {
3276
0
      bNonZeroMvd |= cu.mv[REF_PIC_LIST_1][0].getHor() != 0;
3277
0
      bNonZeroMvd |= cu.mv[REF_PIC_LIST_1][0].getVer() != 0;
3278
0
    }
3279
0
  }
3280
3281
0
  return bNonZeroMvd;
3282
0
}
3283
3284
bool CU::hasSubCUNonZeroAffineMVd( const CodingUnit& cu )
3285
0
{
3286
0
  bool nonZeroAffineMvd = false;
3287
3288
0
  if( cu.interDir() != 2 /* PRED_L1 */ )
3289
0
  {
3290
0
    for( int i = 0; !nonZeroAffineMvd && i < ( cu.affineType() == AFFINEMODEL_6PARAM ? 3 : 2 ); i++ )
3291
0
    {
3292
0
      nonZeroAffineMvd |= cu.mv[REF_PIC_LIST_0][i].getHor() != 0;
3293
0
      nonZeroAffineMvd |= cu.mv[REF_PIC_LIST_0][i].getVer() != 0;
3294
0
    }
3295
0
  }
3296
3297
0
  if( !nonZeroAffineMvd && cu.interDir() != 1 /* PRED_L0 */ )
3298
0
  {
3299
0
    if ( !cu.cs->picHeader->getMvdL1ZeroFlag() || cu.interDir() != 3 /* PRED_BI */ )
3300
0
    {
3301
0
      for( int i = 0; !nonZeroAffineMvd && i < ( cu.affineType() == AFFINEMODEL_6PARAM ? 3 : 2 ); i++ )
3302
0
      {
3303
0
        nonZeroAffineMvd |= cu.mv[REF_PIC_LIST_1][i].getHor() != 0;
3304
0
        nonZeroAffineMvd |= cu.mv[REF_PIC_LIST_1][i].getVer() != 0;
3305
0
      }
3306
0
    }
3307
0
  }
3308
3309
0
  return nonZeroAffineMvd;
3310
0
}
3311
3312
uint8_t CU::getSbtIdx( const uint8_t sbtInfo )
3313
0
{
3314
0
  return ( sbtInfo >> 0 ) & 0xf;
3315
0
}
3316
3317
uint8_t CU::getSbtPos( const uint8_t sbtInfo )
3318
0
{
3319
0
  return ( sbtInfo >> 4 ) & 0x3;
3320
0
}
3321
3322
uint8_t CU::targetSbtAllowed( uint8_t sbtIdx, uint8_t sbtAllowed )
3323
0
{
3324
0
  return ( sbtAllowed >> sbtIdx ) & 0x1;
3325
0
}
3326
3327
0
uint8_t CU::getSbtIdx( const CodingUnit& cu )              { CHECKD( ( ( cu.sbtInfo() >> 0 ) & 0xf ) >= NUMBER_SBT_IDX, "wrong" ); return ( cu.sbtInfo() >> 0 ) & 0xf; }
3328
0
uint8_t CU::getSbtPos( const CodingUnit& cu )              { return ( cu.sbtInfo() >> 4 ) & 0x3; }
3329
0
void    CU::setSbtIdx(       CodingUnit& cu, uint8_t idx ) { CHECKD( idx >= NUMBER_SBT_IDX, "sbt_idx wrong" ); cu.setSbtInfo( ( idx << 0 ) + ( cu.sbtInfo() & 0xf0 ) ); }
3330
0
void    CU::setSbtPos(       CodingUnit& cu, uint8_t pos ) { CHECKD( pos >= 4, "sbt_pos wrong" ); cu.setSbtInfo( ( pos << 4 ) + ( cu.sbtInfo() & 0xcf ) ); }
3331
3332
uint8_t CU::checkAllowedSbt( const CodingUnit& cu )
3333
0
{
3334
  //check on prediction mode
3335
0
  if( !cu.slice->getSPS()->getUseSBT() || cu.predMode() != MODE_INTER || cu.ciipFlag() ) //intra or IBC or triangle
3336
0
  {
3337
0
    return 0;
3338
0
  }
3339
3340
0
  uint8_t sbtAllowed = 0;
3341
0
  int cuWidth  = cu.lwidth();
3342
0
  int cuHeight = cu.lheight();
3343
3344
  //parameter
3345
0
  const int maxSbtCUSize = cu.sps->getMaxTbSize();
3346
3347
  //check on size
3348
0
  if( cuWidth > maxSbtCUSize || cuHeight > maxSbtCUSize )
3349
0
  {
3350
0
    return 0;
3351
0
  }
3352
3353
0
  const int minSbtCUSize = 1 << ( MIN_CU_LOG2 + 1 );
3354
3355
0
  sbtAllowed |= ( cuWidth   >= minSbtCUSize )          << SBT_VER_HALF;
3356
0
  sbtAllowed |= ( cuHeight  >= minSbtCUSize )          << SBT_HOR_HALF;
3357
0
  sbtAllowed |= ( cuWidth   >= ( minSbtCUSize << 1 ) ) << SBT_VER_QUAD;
3358
0
  sbtAllowed |= ( cuHeight  >= ( minSbtCUSize << 1 ) ) << SBT_HOR_QUAD;
3359
3360
0
  return sbtAllowed;
3361
0
}
3362
3363
uint8_t CU::getSbtTuSplit( const CodingUnit& cu )
3364
0
{
3365
0
  uint8_t sbtTuSplitType = 0;
3366
3367
0
  switch( getSbtIdx( cu ) )
3368
0
  {
3369
0
  case SBT_VER_HALF: sbtTuSplitType = ( getSbtPos( cu ) == SBT_POS0 ? 0 : 1 ) + SBT_VER_HALF_POS0_SPLIT; break;
3370
0
  case SBT_HOR_HALF: sbtTuSplitType = ( getSbtPos( cu ) == SBT_POS0 ? 0 : 1 ) + SBT_HOR_HALF_POS0_SPLIT; break;
3371
0
  case SBT_VER_QUAD: sbtTuSplitType = ( getSbtPos( cu ) == SBT_POS0 ? 0 : 1 ) + SBT_VER_QUAD_POS0_SPLIT; break;
3372
0
  case SBT_HOR_QUAD: sbtTuSplitType = ( getSbtPos( cu ) == SBT_POS0 ? 0 : 1 ) + SBT_HOR_QUAD_POS0_SPLIT; break;
3373
0
  default: CHECK( true, "wrong split" );  break;
3374
0
  }
3375
3376
0
  CHECK( !( sbtTuSplitType <= SBT_HOR_QUAD_POS1_SPLIT && sbtTuSplitType >= SBT_VER_HALF_POS0_SPLIT ), "wrong split type" );
3377
0
  return sbtTuSplitType;
3378
0
}
3379
3380
static bool isMinWidthPredEnabledForBlkSize( const int w, const int h )
3381
0
{
3382
0
  return ( ( w == 8 && h > 4 ) || w == 4 );
3383
0
}
3384
3385
bool CU::isPredRegDiffFromTB( const CodingUnit &cu, const ComponentID compID )
3386
0
{
3387
0
  return isLuma( compID ) && cu.ispMode() == VER_INTRA_SUBPARTITIONS && isMinWidthPredEnabledForBlkSize( cu.blocks[compID].width, cu.blocks[compID].height );
3388
0
}
3389
3390
bool CU::isFirstTBInPredReg( const CodingUnit& cu, const ComponentID compID, const CompArea &area )
3391
0
{
3392
0
  return isLuma( compID ) && cu.ispMode() && ( ( area.topLeft().x - cu.Y().topLeft().x ) % PRED_REG_MIN_WIDTH == 0 );
3393
0
}
3394
3395
void CU::adjustPredArea(CompArea &area)
3396
0
{
3397
0
  area.width = std::max<int>( PRED_REG_MIN_WIDTH, area.width );
3398
0
}
3399
3400
PartSplit CU::getSplitAtDepth( const CodingUnit& cu, const unsigned depth )
3401
0
{
3402
0
  CHECK( depth >= 3, "Only works up to the split depth of '3'" );
3403
3404
0
  if( depth >= cu.depth ) return CU_DONT_SPLIT;
3405
3406
0
  const PartSplit cuSplitType = PartSplit( ( cu.splitSeries >> ( depth * SPLIT_DMULT ) ) & SPLIT_MASK );
3407
3408
0
  if     ( cuSplitType == CU_QUAD_SPLIT    ) return CU_QUAD_SPLIT;
3409
3410
0
  else if( cuSplitType == CU_HORZ_SPLIT    ) return CU_HORZ_SPLIT;
3411
3412
0
  else if( cuSplitType == CU_VERT_SPLIT    ) return CU_VERT_SPLIT;
3413
3414
0
  else if( cuSplitType == CU_TRIH_SPLIT    ) return CU_TRIH_SPLIT;
3415
0
  else if( cuSplitType == CU_TRIV_SPLIT    ) return CU_TRIV_SPLIT;
3416
3417
0
  THROW_RECOVERABLE( "Unknown split mode" );
3418
0
}
3419
3420
bool CU::checkCCLMAllowed( const CodingUnit& cu )
3421
0
{
3422
0
  bool allowCCLM = false;
3423
3424
0
  if( !CU::isDualITree( cu ) ) //single tree I slice or non-I slice (Note: judging chType is no longer equivalent to checking dual-tree I slice since the local dual-tree is introduced)
3425
0
  {
3426
0
    allowCCLM = true;
3427
0
  }
3428
0
  else if( cu.sps->getCTUSize() <= 32 ) //dual tree, CTUsize < 64
3429
0
  {
3430
0
    allowCCLM = true;
3431
0
  }
3432
0
  else //dual tree, CTU size 64 or 128
3433
0
  {
3434
0
    const int       depthFor64x64Node = cu.sps->getCTUSize() == 128 ? 1 : 0;
3435
0
    const PartSplit cuSplitTypeDepth1 = CU::getSplitAtDepth( cu, depthFor64x64Node );
3436
0
    const PartSplit cuSplitTypeDepth2 = CU::getSplitAtDepth( cu, depthFor64x64Node + 1 );
3437
3438
    //allow CCLM if 64x64 chroma tree node uses QT split or HBT+VBT split combination
3439
0
    if( cuSplitTypeDepth1 == CU_QUAD_SPLIT || ( cuSplitTypeDepth1 == CU_HORZ_SPLIT && cuSplitTypeDepth2 == CU_VERT_SPLIT ) )
3440
0
    {
3441
0
      allowCCLM = true;
3442
0
    }
3443
    //allow CCLM if 64x64 chroma tree node uses NS (No Split) and becomes a chroma CU containing 32x32 chroma blocks
3444
0
    else if( cuSplitTypeDepth1 == CU_DONT_SPLIT )
3445
0
    {
3446
0
      allowCCLM = true;
3447
0
    }
3448
    //allow CCLM if 64x32 chroma tree node uses NS and becomes a chroma CU containing 32x16 chroma blocks
3449
0
    else if( cuSplitTypeDepth1 == CU_HORZ_SPLIT && cuSplitTypeDepth2 == CU_DONT_SPLIT )
3450
0
    {
3451
0
      allowCCLM = true;
3452
0
    }
3453
3454
    //further check luma conditions
3455
0
    if( allowCCLM )
3456
0
    {
3457
      //disallow CCLM if luma 64x64 block uses BT or TT or NS with ISP
3458
0
      const Position lumaRefPos( cu.chromaPos().x << getComponentScaleX( COMPONENT_Cb, cu.chromaFormat ), cu.chromaPos().y << getComponentScaleY( COMPONENT_Cb, cu.chromaFormat ) );
3459
0
      const CodingUnit* colLumaCu = cu.cs->getCU( lumaRefPos, CHANNEL_TYPE_LUMA );
3460
3461
0
      if( colLumaCu->depth > depthFor64x64Node && colLumaCu->qtDepth == depthFor64x64Node ) //further split at 64x64 luma node
3462
0
      {
3463
0
        allowCCLM = false;
3464
0
      }
3465
0
      else if( colLumaCu->depth == depthFor64x64Node && colLumaCu->ispMode() ) //not split at 64x64 luma node and use ISP mode
3466
0
      {
3467
0
        allowCCLM = false;
3468
0
      }
3469
0
    }
3470
0
  }
3471
3472
0
  return allowCCLM;
3473
0
}
3474
3475
bool CU::isBcwIdxCoded( const CodingUnit &cu )
3476
0
{
3477
0
  if( cu.sps->getUseBcw() == false )
3478
0
  {
3479
0
    CHECK( cu.BcwIdx() != BCW_DEFAULT, "Error: cu.BcwIdx != BCW_DEFAULT" );
3480
0
    return false;
3481
0
  }
3482
3483
0
  if( cu.predMode() == MODE_IBC || cu.predMode() == MODE_INTRA || cu.slice->isInterP() || cu.interDir() != 3 )
3484
0
  {
3485
0
    return false;
3486
0
  }
3487
3488
0
  if( cu.lwidth() * cu.lheight() < BCW_SIZE_CONSTRAINT )
3489
0
  {
3490
0
    return false;
3491
0
  }
3492
3493
0
  const WPScalingParam* wp0 = nullptr;
3494
0
  const WPScalingParam* wp1 = nullptr;
3495
0
  int refIdx0 = cu.refIdx[REF_PIC_LIST_0];
3496
0
  int refIdx1 = cu.refIdx[REF_PIC_LIST_1];
3497
3498
0
  cu.slice->getWpScaling(REF_PIC_LIST_0, refIdx0, wp0);
3499
0
  cu.slice->getWpScaling(REF_PIC_LIST_1, refIdx1, wp1);
3500
3501
0
  if ((wp0[COMPONENT_Y].bPresentFlag || wp0[COMPONENT_Cb].bPresentFlag || wp0[COMPONENT_Cr].bPresentFlag
3502
0
    || wp1[COMPONENT_Y].bPresentFlag || wp1[COMPONENT_Cb].bPresentFlag || wp1[COMPONENT_Cr].bPresentFlag))
3503
0
  {
3504
0
    return false;
3505
0
  }
3506
0
  return true;
3507
0
}
3508
3509
void CU::setBcwIdx( CodingUnit &cu, uint8_t uh )
3510
0
{
3511
0
  int8_t uhCnt = 0;
3512
3513
0
  if( cu.interDir() == 3 && !cu.mergeFlag() )
3514
0
  {
3515
0
    cu.setBcwIdx( uh );
3516
0
    ++uhCnt;
3517
0
  }
3518
0
  else if( cu.interDir()== 3 && cu.mergeFlag() && cu.mergeType() == MRG_TYPE_DEFAULT_N )
3519
0
  {
3520
    // This is intended to do nothing here.
3521
0
  }
3522
0
  else if( cu.mergeFlag() && cu.mergeType() == MRG_TYPE_SUBPU_ATMVP )
3523
0
  {
3524
0
    cu.setBcwIdx( BCW_DEFAULT );
3525
0
  }
3526
0
  else
3527
0
  {
3528
0
    cu.setBcwIdx( BCW_DEFAULT );
3529
0
  }
3530
3531
0
  CHECK(uhCnt <= 0, " uhCnt <= 0 ");
3532
0
}
3533
3534
3535
bool CU::bdpcmAllowed( const CodingUnit& cu, const ComponentID compID )
3536
0
{
3537
0
  const SizeType transformSkipMaxSize = 1 << cu.sps->getLog2MaxTransformSkipBlockSize();
3538
0
  const Size&    blkSize              = cu.blocks[compID].size();
3539
3540
0
  bool bdpcmAllowed = cu.sps->getBDPCMEnabledFlag() &&
3541
0
                    ( isLuma( compID ) || !cu.colorTransform() ) &&
3542
0
                      blkSize.width <= transformSkipMaxSize &&
3543
0
                      blkSize.height <= transformSkipMaxSize;
3544
3545
0
  return bdpcmAllowed;
3546
0
}
3547
3548
bool CU::isMTSAllowed(const CodingUnit &cu, const ComponentID compID)
3549
0
{
3550
0
  SizeType tsMaxSize = 1 << cu.sps->getLog2MaxTransformSkipBlockSize();
3551
0
  const int maxSize  = CU::isIntra( cu ) ? MTS_INTRA_MAX_CU_SIZE : MTS_INTER_MAX_CU_SIZE;
3552
0
  const int cuWidth  = cu.lumaSize().width;
3553
0
  const int cuHeight = cu.lumaSize().height;
3554
0
  bool mtsAllowed    = cu.chType() == CHANNEL_TYPE_LUMA && compID == COMPONENT_Y;
3555
3556
0
  mtsAllowed &= CU::isIntra( cu ) ? cu.sps->getUseIntraMTS() : cu.sps->getUseInterMTS() && CU::isInter( cu );
3557
0
  mtsAllowed &= cuWidth <= maxSize && cuHeight <= maxSize;
3558
0
  mtsAllowed &= !cu.ispMode();
3559
0
  mtsAllowed &= !cu.sbtInfo();
3560
0
  mtsAllowed &= !(cu.bdpcmMode() && cuWidth <= tsMaxSize && cuHeight <= tsMaxSize);
3561
0
  return mtsAllowed;
3562
0
}
3563
3564
// TU tools
3565
3566
bool TU::getCbf( const TransformUnit &tu, const ComponentID &compID )
3567
0
{
3568
0
  return ( tu.cbf >> compID ) & 1;
3569
0
}
3570
3571
void TU::setCbf(TransformUnit &tu, const ComponentID &compID, const bool &cbf)
3572
0
{
3573
  // first clear the CBF at the depth
3574
0
  tu.cbf &= ~(1  << compID);
3575
  // then set the CBF
3576
0
  tu.cbf |= ((cbf ? 1 : 0) << compID);
3577
0
}
3578
3579
bool TU::isTSAllowed(const TransformUnit &tu, const ComponentID compID)
3580
0
{
3581
0
  const int maxSize = tu.cu->sps->getLog2MaxTransformSkipBlockSize();
3582
0
  bool tsAllowed = tu.cu->sps->getTransformSkipEnabledFlag();
3583
0
  tsAllowed &= ( !tu.cu->ispMode() || !isLuma( compID ) );
3584
0
  SizeType transformSkipMaxSize = 1 << maxSize;
3585
0
  tsAllowed &= !(tu.cu->bdpcmMode() && isLuma(compID));
3586
0
  tsAllowed &= !(tu.cu->bdpcmModeChroma() && isChroma(compID));
3587
0
  tsAllowed &= tu.blocks[compID].width <= transformSkipMaxSize && tu.blocks[compID].height <= transformSkipMaxSize;
3588
0
  tsAllowed &= !tu.cu->sbtInfo();
3589
3590
0
  return tsAllowed;
3591
0
}
3592
3593
3594
int TU::getICTMode( const TransformUnit& tu, bool sign )
3595
0
{
3596
0
  return g_ictModes[ sign ][ tu.jointCbCr ];
3597
0
}
3598
3599
3600
3601
bool TU::needsSqrt2Scale( const TransformUnit &tu, const ComponentID &compID )
3602
0
{
3603
0
  const Size &size            = tu.blocks[compID];
3604
0
  const bool  isTransformSkip = tu.mtsIdx( compID ) == 1;
3605
0
  return !isTransformSkip && ( ( getLog2( size.width ) + getLog2( size.height ) ) & 1 ) == 1;
3606
0
}
3607
3608
bool TU::needsBlockSizeTrafoScale( const TransformUnit &tu, const ComponentID &compID )
3609
0
{
3610
0
  return needsSqrt2Scale( tu, compID );
3611
0
}
3612
3613
const TransformUnit* TU::getPrevTU( const TransformUnit &tu, const ComponentID compID )
3614
0
{
3615
0
  const TransformUnit* prevTU = nullptr;
3616
3617
0
  for( auto &currTu : cTUTraverser( &tu.cu->firstTU, &tu ) )
3618
0
  {
3619
0
    if( &tu == &currTu ) break;
3620
0
    prevTU = &currTu;
3621
0
  }
3622
3623
0
  if( prevTU != nullptr && ( prevTU->cu != tu.cu || !prevTU->blocks[compID].valid() ) )
3624
0
  {
3625
0
    prevTU = nullptr;
3626
0
  }
3627
3628
0
  return prevTU;
3629
0
}
3630
3631
bool TU::getPrevTUCbf( const TransformUnit &currentTu, const ComponentID compID )
3632
0
{
3633
0
  const TransformUnit* prevTU = getPrevTU( currentTu, compID );
3634
0
  return ( prevTU != nullptr ) ? TU::getCbf( *prevTU, compID ) : false;
3635
0
}
3636
3637
3638
bool TU::checkTuNoResidual( TransformUnit &tu, unsigned idx )
3639
0
{
3640
0
  if( CU::getSbtIdx( tu.cu->sbtInfo() ) == SBT_OFF_DCT )
3641
0
  {
3642
0
    return false;
3643
0
  }
3644
3645
0
  if( ( CU::getSbtPos( tu.cu->sbtInfo() ) == SBT_POS0 && idx == 1 ) || ( CU::getSbtPos( tu.cu->sbtInfo() ) == SBT_POS1 && idx == 0 ) )
3646
0
  {
3647
0
    return true;
3648
0
  }
3649
3650
0
  return false;
3651
0
}
3652
3653
3654
int TU::getTbAreaAfterCoefZeroOut(const TransformUnit &tu, const ComponentID compID)
3655
0
{
3656
0
  int tbZeroOutWidth  = tu.blocks[compID].width;
3657
0
  int tbZeroOutHeight = tu.blocks[compID].height;
3658
3659
0
  if( compID == COMPONENT_Y && ( tu.mtsIdx( compID ) > MTS_SKIP || ( tu.cu->sps->getUseMTS() && tu.cu->sbtInfo() != 0 && tbZeroOutWidth <= 32 && tbZeroOutHeight <= 32 ) ) )
3660
0
  {
3661
0
    tbZeroOutWidth  = (tbZeroOutWidth  == 32) ? 16 : tbZeroOutWidth;
3662
0
    tbZeroOutHeight = (tbZeroOutHeight == 32) ? 16 : tbZeroOutHeight;
3663
0
  }
3664
3665
0
  tbZeroOutWidth  = std::min<int>( JVET_C0024_ZERO_OUT_TH, tbZeroOutWidth );
3666
0
  tbZeroOutHeight = std::min<int>( JVET_C0024_ZERO_OUT_TH, tbZeroOutHeight );
3667
0
  return tbZeroOutWidth * tbZeroOutHeight;
3668
0
}
3669
3670
3671
// other tools
3672
3673
uint32_t getCtuAddr( const Position& pos, const PreCalcValues& pcv )
3674
0
{
3675
0
  return ( pos.x >> pcv.maxCUWidthLog2 ) + ( pos.y >> pcv.maxCUHeightLog2 ) * pcv.widthInCtus;
3676
0
}
3677
3678
3679
UnitArea getLineArea(const CodingStructure & cs, unsigned line, bool clipToPic)
3680
0
{
3681
0
  const unsigned widthInCtus = cs.pcv->widthInCtus;
3682
0
  const unsigned maxCUWidth  = cs.pcv->maxCUWidth;
3683
0
  const unsigned maxCUHeight = cs.pcv->maxCUHeight;
3684
3685
0
  if( !clipToPic )
3686
0
  {
3687
0
    return UnitArea( cs.area.chromaFormat, Area( 0, line * maxCUHeight, maxCUWidth * widthInCtus, maxCUHeight ) );
3688
0
  }
3689
3690
0
  const unsigned width  = cs.pcv->lumaWidth;
3691
0
  const unsigned height = std::min( cs.pcv->lumaHeight - line * maxCUHeight, maxCUHeight );
3692
3693
0
  return UnitArea( cs.area.chromaFormat, Area( 0, line * maxCUHeight, width, height ) );
3694
0
}
3695
3696
UnitArea getCtuArea(const CodingStructure & cs, unsigned col, unsigned line, bool clipToPic)
3697
0
{
3698
0
  const unsigned maxCUWidth  = cs.pcv->maxCUWidth;
3699
0
  const unsigned maxCUHeight = cs.pcv->maxCUHeight;
3700
0
  const int      xPos        = col  * maxCUWidth;
3701
0
  const int      yPos        = line * maxCUHeight;
3702
3703
0
  if( !clipToPic )
3704
0
  {
3705
0
    return UnitArea( cs.area.chromaFormat, Area( xPos, yPos, maxCUWidth, maxCUHeight ) );
3706
0
  }
3707
3708
0
  CHECKD( (unsigned)xPos > cs.pcv->lumaWidth,  "Block start lies outside of the picture!" );
3709
0
  CHECKD( (unsigned)yPos > cs.pcv->lumaHeight, "Block start lies outside of the picture!" );
3710
3711
0
  const unsigned width  = std::min( cs.pcv->lumaWidth  - xPos, maxCUWidth );
3712
0
  const unsigned height = std::min( cs.pcv->lumaHeight - yPos, maxCUHeight );
3713
3714
0
  return UnitArea( cs.area.chromaFormat, Area( xPos, yPos, width, height ) );
3715
0
}
3716
3717
int getNumModesMip( const Size& block )
3718
0
{
3719
0
  switch( getMipSizeId(block) )
3720
0
  {
3721
0
    case 0: return 16;
3722
0
    case 1: return  8;
3723
0
    case 2: return  6;
3724
0
    default: THROW_FATAL( "Invalid mipSizeId" );
3725
0
  }
3726
0
}
3727
3728
3729
int getMipSizeId(const Size& block)
3730
0
{
3731
0
  if( block.width == 4 && block.height == 4 )
3732
0
  {
3733
0
    return 0;
3734
0
  }
3735
0
  else if( block.width == 4 || block.height == 4 || (block.width == 8 && block.height == 8) )
3736
0
  {
3737
0
    return 1;
3738
0
  }
3739
0
  else
3740
0
  {
3741
0
    return 2;
3742
0
  }
3743
3744
0
}
3745
3746
bool allowLfnstWithMip( const Size& block )
3747
0
{
3748
0
  return block.width >= 16 && block.height >= 16;
3749
0
}
3750
3751
bool PU::isRefPicSameSize( const CodingUnit& cu )
3752
0
{
3753
0
  bool samePicSize = true;
3754
0
  int curPicWidth  = cu.pps->getPicWidthInLumaSamples();
3755
0
  int curPicHeight = cu.pps->getPicHeightInLumaSamples();
3756
3757
0
  if( cu.refIdx[0] >= 0 )
3758
0
  {
3759
0
    int refPicWidth  = cu.slice->getRefPic( REF_PIC_LIST_0, cu.refIdx[0] )->cs->pps->getPicWidthInLumaSamples();
3760
0
    int refPicHeight = cu.slice->getRefPic( REF_PIC_LIST_0, cu.refIdx[0] )->cs->pps->getPicHeightInLumaSamples();
3761
3762
0
    samePicSize = refPicWidth == curPicWidth && refPicHeight == curPicHeight;
3763
0
  }
3764
3765
0
  if( cu.refIdx[1] >= 0 )
3766
0
  {
3767
0
    int refPicWidth  = cu.slice->getRefPic( REF_PIC_LIST_1, cu.refIdx[1] )->cs->pps->getPicWidthInLumaSamples();
3768
0
    int refPicHeight = cu.slice->getRefPic( REF_PIC_LIST_1, cu.refIdx[1] )->cs->pps->getPicHeightInLumaSamples();
3769
3770
0
    samePicSize = samePicSize && ( refPicWidth == curPicWidth && refPicHeight == curPicHeight );
3771
0
  }
3772
3773
0
  return samePicSize;
3774
0
}
3775
3776
bool isCrossedByVirtualBoundaries( const PicHeader* picHeader,
3777
                                   const Area&      area,
3778
                                   int&             numHorVirBndry,
3779
                                   int&             numVerVirBndry,
3780
                                   int              horVirBndryPos[],
3781
                                   int              verVirBndryPos[] )
3782
0
{
3783
0
  numHorVirBndry = 0;
3784
0
  numVerVirBndry = 0;
3785
0
  if( !picHeader->getVirtualBoundariesPresentFlag() )
3786
0
  {
3787
0
    return false;
3788
0
  }
3789
3790
0
  for( int i = 0; i < picHeader->getNumHorVirtualBoundaries(); i++ )
3791
0
  {
3792
0
    if( area.y <= picHeader->getVirtualBoundariesPosY( i ) && picHeader->getVirtualBoundariesPosY( i ) <= area.y + area.height )
3793
0
    {
3794
0
      horVirBndryPos[numHorVirBndry++] = picHeader->getVirtualBoundariesPosY( i );
3795
0
    }
3796
0
  }
3797
0
  for( int i = 0; i < picHeader->getNumVerVirtualBoundaries(); i++ )
3798
0
  {
3799
0
    if( area.x <= picHeader->getVirtualBoundariesPosX( i ) && picHeader->getVirtualBoundariesPosX( i ) <= area.x + area.width )
3800
0
    {
3801
0
      verVirBndryPos[numVerVirBndry++] = picHeader->getVirtualBoundariesPosX( i );
3802
0
    }
3803
0
  }
3804
3805
0
  return numHorVirBndry > 0 || numVerVirBndry > 0;
3806
0
}
3807
3808
}