Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvenc/source/Lib/CommonLib/InterPrediction.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) 2019-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVenC 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
44
/** \file     Prediction.cpp
45
    \brief    prediction class
46
*/
47
48
#include "InterPrediction.h"
49
#include "Unit.h"
50
#include "UnitTools.h"
51
#include "dtrace_next.h"
52
#include "dtrace_buffer.h"
53
#include "CommonLib/TimeProfiler.h"
54
55
#include <memory.h>
56
#include <algorithm>
57
58
//! \ingroup CommonLib
59
//! \{
60
61
namespace vvenc {
62
63
void addBDOFAvgCore(const Pel* src0, const ptrdiff_t  src0Stride, const Pel* src1, const ptrdiff_t  src1Stride, Pel* dst, const ptrdiff_t dstStride, const Pel* gradX0, const Pel* gradX1, const Pel* gradY0, const Pel*gradY1, const ptrdiff_t  gradStride, int width, int height, int tmpx, int tmpy, unsigned shift, int offset, const ClpRng& clpRng)
64
0
{
65
0
  int b = 0;
66
67
0
  for (int y = 0; y < height; y++)
68
0
  {
69
0
    for (int x = 0; x < width; x += 4)
70
0
    {
71
0
      b = tmpx * (gradX0[x] - gradX1[x]) + tmpy * (gradY0[x] - gradY1[x]);
72
0
      dst[x] = ClipPel((int16_t)rightShiftU((src0[x] + src1[x] + b + offset), shift), clpRng);
73
74
0
      b = tmpx * (gradX0[x + 1] - gradX1[x + 1]) + tmpy * (gradY0[x + 1] - gradY1[x + 1]);
75
0
      dst[x + 1] = ClipPel((int16_t)rightShiftU((src0[x + 1] + src1[x + 1] + b + offset), shift), clpRng);
76
77
0
      b = tmpx * (gradX0[x + 2] - gradX1[x + 2]) + tmpy * (gradY0[x + 2] - gradY1[x + 2]);
78
0
      dst[x + 2] = ClipPel((int16_t)rightShiftU((src0[x + 2] + src1[x + 2] + b + offset), shift), clpRng);
79
80
0
      b = tmpx * (gradX0[x + 3] - gradX1[x + 3]) + tmpy * (gradY0[x + 3] - gradY1[x + 3]);
81
0
      dst[x + 3] = ClipPel((int16_t)rightShiftU((src0[x + 3] + src1[x + 3] + b + offset), shift), clpRng);
82
0
    }
83
0
    dst += dstStride;       src0 += src0Stride;     src1 += src1Stride;
84
0
    gradX0 += gradStride; gradX1 += gradStride; gradY0 += gradStride; gradY1 += gradStride;
85
0
  }
86
0
}
87
88
void applyPROFCore(Pel* dst, int dstStride, const Pel* src, int srcStride, int width, int height, const Pel* gradX, const Pel* gradY, int gradStride, const int* dMvX, const int* dMvY, int dMvStride, const bool& bi, int shiftNum, Pel offset, const ClpRng& clpRng)
89
0
{
90
0
  int idx = 0;
91
0
  const int dILimit = 1 << std::max<int>(clpRng.bd + 1, 13);
92
0
  for (int h = 0; h < height; h++)
93
0
  {
94
0
    for (int w = 0; w < width; w++)
95
0
    {
96
0
      int32_t dI = dMvX[idx] * gradX[w] + dMvY[idx] * gradY[w];
97
0
      dI = Clip3(-dILimit, dILimit - 1, dI);
98
0
      dst[w] = src[w] + dI;
99
0
      if (!bi)
100
0
      {
101
0
        dst[w] = (dst[w] + offset) >> shiftNum;
102
0
        dst[w] = ClipPel(dst[w], clpRng);
103
0
      }
104
0
      idx++;
105
0
    }
106
0
    gradX += gradStride;
107
0
    gradY += gradStride;
108
0
    dst += dstStride;
109
0
    src += srcStride;
110
0
  }
111
0
}
112
113
template<bool PAD = true>
114
void gradFilterCore(const Pel* pSrc, int srcStride, int width, int height, int gradStride, Pel* gradX, Pel* gradY, const int bitDepth)
115
0
{
116
0
  const Pel* srcTmp = pSrc + srcStride + 1;
117
0
  Pel* gradXTmp = gradX + gradStride + 1;
118
0
  Pel* gradYTmp = gradY + gradStride + 1;
119
0
  int  shift1 = 6;
120
121
0
  for (int y = 0; y < (height - 2 * BDOF_EXTEND_SIZE); y++)
122
0
  {
123
0
    for (int x = 0; x < (width - 2 * BDOF_EXTEND_SIZE); x++)
124
0
    {
125
0
      gradYTmp[x] = ( srcTmp[x + srcStride] >> shift1 ) - ( srcTmp[x - srcStride] >> shift1 );
126
0
      gradXTmp[x] = ( srcTmp[x + 1] >> shift1 ) - ( srcTmp[x - 1] >> shift1 );
127
0
    }
128
0
    gradXTmp += gradStride;
129
0
    gradYTmp += gradStride;
130
0
    srcTmp += srcStride;
131
0
  }
132
133
0
  if (PAD)
134
0
  {
135
0
    gradXTmp = gradX + gradStride + 1;
136
0
    gradYTmp = gradY + gradStride + 1;
137
0
    for (int y = 0; y < (height - 2 * BDOF_EXTEND_SIZE); y++)
138
0
    {
139
0
      gradXTmp[-1] = gradXTmp[0];
140
0
      gradXTmp[width - 2 * BDOF_EXTEND_SIZE] = gradXTmp[width - 2 * BDOF_EXTEND_SIZE - 1];
141
0
      gradXTmp += gradStride;
142
143
0
      gradYTmp[-1] = gradYTmp[0];
144
0
      gradYTmp[width - 2 * BDOF_EXTEND_SIZE] = gradYTmp[width - 2 * BDOF_EXTEND_SIZE - 1];
145
0
      gradYTmp += gradStride;
146
0
    }
147
148
0
    gradXTmp = gradX + gradStride;
149
0
    gradYTmp = gradY + gradStride;
150
0
    ::memcpy(gradXTmp - gradStride, gradXTmp, sizeof(Pel)*(width));
151
0
    ::memcpy(gradXTmp + (height - 2 * BDOF_EXTEND_SIZE)*gradStride, gradXTmp + (height - 2 * BDOF_EXTEND_SIZE - 1)*gradStride, sizeof(Pel)*(width));
152
0
    ::memcpy(gradYTmp - gradStride, gradYTmp, sizeof(Pel)*(width));
153
0
    ::memcpy(gradYTmp + (height - 2 * BDOF_EXTEND_SIZE)*gradStride, gradYTmp + (height - 2 * BDOF_EXTEND_SIZE - 1)*gradStride, sizeof(Pel)*(width));
154
0
  }
155
0
}
Unexecuted instantiation: void vvenc::gradFilterCore<true>(short const*, int, int, int, int, short*, short*, int)
Unexecuted instantiation: void vvenc::gradFilterCore<false>(short const*, int, int, int, int, short*, short*, int)
156
157
void calcBDOFSumsCore( const Pel* srcY0Tmp, const Pel* srcY1Tmp, const Pel* gradX0, const Pel* gradX1,
158
                       const Pel* gradY0, const Pel* gradY1, int xu, int yu, const ptrdiff_t src0Stride, const ptrdiff_t src1Stride,
159
                       const ptrdiff_t widthG, const int bitDepth, int* sumAbsGX, int* sumAbsGY, int* sumDIX, int* sumDIY,
160
                       int* sumSignGY_GX )
161
0
{
162
0
  int shift4 = 4;
163
0
  int shift5 = 1;
164
165
0
  for (int y = 0; y < 6; y++)
166
0
  {
167
0
    for (int x = 0; x < 6; x++)
168
0
    {
169
0
      int tmpGX = (gradX0[x] + gradX1[x]) >> shift5;
170
0
      int tmpGY = (gradY0[x] + gradY1[x]) >> shift5;
171
0
      int tmpDI = (int)((srcY1Tmp[x] >> shift4) - (srcY0Tmp[x] >> shift4));
172
0
      *sumAbsGX += (tmpGX < 0 ? -tmpGX : tmpGX);
173
0
      *sumAbsGY += (tmpGY < 0 ? -tmpGY : tmpGY);
174
0
      *sumDIX += (tmpGX < 0 ? -tmpDI : (tmpGX == 0 ? 0 : tmpDI));
175
0
      *sumDIY += (tmpGY < 0 ? -tmpDI : (tmpGY == 0 ? 0 : tmpDI));
176
0
      *sumSignGY_GX += (tmpGY < 0 ? -tmpGX : (tmpGY == 0 ? 0 : tmpGX));
177
178
0
    }
179
0
    srcY1Tmp += src1Stride;
180
0
    srcY0Tmp += src0Stride;
181
0
    gradX0 += widthG;
182
0
    gradX1 += widthG;
183
0
    gradY0 += widthG;
184
0
    gradY1 += widthG;
185
0
  }
186
0
}
187
188
189
template<int padSize>
190
void paddingCore(Pel *ptr, int stride, int width, int height)
191
0
{
192
  /*left and right padding*/
193
0
  Pel *ptrTemp1 = ptr;
194
0
  Pel *ptrTemp2 = ptr + (width - 1);
195
0
  ptrdiff_t offset = 0;
196
0
  for (int i = 0; i < height; i++)
197
0
  {
198
0
    offset = stride * i;
199
0
    for (int j = 1; j <= padSize; j++)
200
0
    {
201
0
      *(ptrTemp1 - j + offset) = *(ptrTemp1 + offset);
202
0
      *(ptrTemp2 + j + offset) = *(ptrTemp2 + offset);
203
0
    }
204
0
  }
205
  /*Top and Bottom padding*/
206
0
  int numBytes = (width + padSize + padSize) * sizeof(Pel);
207
0
  ptrTemp1 = (ptr - padSize);
208
0
  ptrTemp2 = (ptr + (stride * (height - 1)) - padSize);
209
0
  for (int i = 1; i <= padSize; i++)
210
0
  {
211
0
    memcpy(ptrTemp1 - (i * stride), (ptrTemp1), numBytes);
212
0
    memcpy(ptrTemp2 + (i * stride), (ptrTemp2), numBytes);
213
0
  }
214
0
}
Unexecuted instantiation: void vvenc::paddingCore<1>(short*, int, int, int)
Unexecuted instantiation: void vvenc::paddingCore<2>(short*, int, int, int)
215
216
void padDmvrCore( const Pel* src, const int srcStride, Pel* dst, const int dstStride, int width, int height, int padSize )
217
0
{
218
0
  g_pelBufOP.copyBuffer( ( const char* ) src, srcStride * sizeof( Pel ), ( char* ) dst, dstStride * sizeof( Pel ), width * sizeof( Pel ), height );
219
0
  if( padSize == 1 )
220
0
    paddingCore<1>( dst, dstStride, width, height );
221
0
  else
222
0
    paddingCore<2>( dst, dstStride, width, height );
223
0
}
224
225
// ====================================================================================================================
226
// Constructor / destructor / initialize
227
// ====================================================================================================================
228
229
InterPrediction::InterPrediction()
230
5.72k
  : m_currChromaFormat( NUM_CHROMA_FORMAT )
231
5.72k
  , m_subPuMC(false)
232
5.72k
  , m_IBCBufferWidth(0)
233
5.72k
{
234
5.72k
}
235
236
InterPrediction::~InterPrediction()
237
5.72k
{
238
5.72k
  destroy();
239
5.72k
}
240
241
void InterPrediction::destroy()
242
5.72k
{
243
17.1k
  for( uint32_t i = 0; i < NUM_REF_PIC_LIST_01; i++ )
244
11.4k
  {
245
11.4k
    m_yuvPred[i].destroy();
246
11.4k
  }
247
5.72k
  m_geoPartBuf[0].destroy();
248
5.72k
  m_geoPartBuf[1].destroy();
249
5.72k
  m_IBCBuffer.destroy();
250
5.72k
}
251
252
void InterPrediction::init( RdCost* pcRdCost, ChromaFormat chFormat, const int ctuSize, const int ifpLines )
253
5.72k
{
254
  // if it has been initialised before, but the chroma format has changed, release the memory and start again.
255
5.72k
  if( m_yuvPred[L0].getOrigin( COMP_Y ) != nullptr && m_currChromaFormat != chFormat )
256
0
  {
257
0
    destroy();
258
0
    DMVR::destroy();
259
0
    InterPredInterpolation::destroy();
260
0
  }
261
262
5.72k
  m_currChromaFormat = chFormat;
263
264
5.72k
  if( m_yuvPred[L0].getOrigin( COMP_Y ) == nullptr )
265
5.72k
  {
266
17.1k
    for( uint32_t i = 0; i < NUM_REF_PIC_LIST_01; i++ )
267
11.4k
    {
268
11.4k
      m_yuvPred[i].create( chFormat, Area{ 0, 0, (int)MAX_CU_SIZE, (int)MAX_CU_SIZE }, 0, 0, 32 );
269
11.4k
    }
270
271
5.72k
    InterPredInterpolation::init();
272
5.72k
    DMVR::init( pcRdCost, chFormat );
273
5.72k
    m_geoPartBuf[0].create(UnitArea(chFormat, Area(0, 0, MAX_CU_SIZE, MAX_CU_SIZE)));
274
5.72k
    m_geoPartBuf[1].create(UnitArea(chFormat, Area(0, 0, MAX_CU_SIZE, MAX_CU_SIZE)));
275
5.72k
  }
276
5.72k
  if (m_IBCBufferWidth != g_IBCBufferSize / ctuSize)
277
5.72k
  {
278
5.72k
    m_IBCBuffer.destroy();
279
5.72k
  }
280
5.72k
  if (m_IBCBuffer.bufs.empty())
281
5.72k
  {
282
5.72k
    m_IBCBufferWidth = g_IBCBufferSize / ctuSize;
283
5.72k
    m_IBCBuffer.create(UnitArea(chFormat, Area(0, 0, m_IBCBufferWidth, ctuSize)));
284
5.72k
  }
285
5.72k
  InterPredInterpolation::m_ifpLines = ifpLines;
286
5.72k
}
287
288
// ====================================================================================================================
289
// Public member functions
290
// ====================================================================================================================
291
292
bool InterPrediction::xCheckIdenticalMotion( const CodingUnit& cu ) const
293
0
{
294
0
  const Slice &slice = *cu.cs->slice;
295
296
0
  if( slice.isInterB() && !cu.cs->pps->weightedBiPred )
297
0
  {
298
0
    if( cu.refIdx[0] >= 0 && cu.refIdx[1] >= 0 )
299
0
    {
300
0
      int RefPOCL0 = slice.getRefPic( REF_PIC_LIST_0, cu.refIdx[0] )->getPOC();
301
0
      int RefPOCL1 = slice.getRefPic( REF_PIC_LIST_1, cu.refIdx[1] )->getPOC();
302
303
0
      if( RefPOCL0 == RefPOCL1 )
304
0
      {
305
0
        if( !cu.affine )
306
0
        {
307
0
          if( cu.mv[0][0] == cu.mv[1][0] )
308
0
          {
309
0
            return true;
310
0
          }
311
0
        }
312
0
        else
313
0
        {
314
0
          if( cu.mv[0][0] == cu.mv[1][0] && cu.mv[0][1] == cu.mv[1][1] && ( cu.affineType == AFFINEMODEL_4PARAM || cu.mv[0][2] == cu.mv[1][2] ) )
315
0
          {
316
0
            return true;
317
0
          }
318
0
        }
319
0
      }
320
0
    }
321
0
  }
322
323
0
  return false;
324
0
}
325
326
void InterPrediction::xSubPuBDOF( const CodingUnit& cu, PelUnitBuf& predBuf, const RefPicList& refPicList /*= REF_PIC_LIST_X*/)
327
0
{
328
0
  Position puPos = cu.lumaPos();
329
0
  Size puSize = cu.lumaSize();
330
331
0
  CodingUnit subCu = cu;  // th we do not need all that stuff 
332
0
  subCu.cs             = cu.cs;
333
0
  subCu.mergeType      = cu.mergeType;
334
0
  subCu.mmvdMergeFlag  = cu.mmvdMergeFlag;
335
0
  subCu.mcControl      = cu.mcControl;
336
0
  subCu.mergeFlag      = cu.mergeFlag;
337
0
  subCu.ciip           = cu.ciip;
338
0
  subCu.mvRefine       = cu.mvRefine;
339
0
  subCu.refIdx[0]      = cu.refIdx[0];
340
0
  subCu.refIdx[1]      = cu.refIdx[1];
341
342
0
  const int  yEnd      = puPos.y + puSize.height;
343
0
  const int  xEnd      = puPos.x + puSize.width;
344
0
  const int  dy        = std::min((int)MAX_BDOF_APPLICATION_REGION, (int)puSize.height);
345
0
  const int  dx        = std::min((int)MAX_BDOF_APPLICATION_REGION, (int)puSize.width);
346
347
  // BDOF is only applied to CUs with one motion for the whole block, so every sub-block uses the
348
  // motion of the CU. Taking it from the CU rather than from the motion buffer lets the merge search
349
  // skip writing the buffer for every candidate it evaluates.
350
0
  MotionInfo cuMotion;
351
0
  for( int i = 0; i < NUM_REF_PIC_LIST_01; i++ )
352
0
  {
353
0
    cuMotion.mv[i]       = cu.mv[i][0];
354
0
    cuMotion.miRefIdx[i] = cu.refIdx[i];
355
0
  }
356
357
0
  for (int y = puPos.y; y < yEnd; y += dy)
358
0
  {
359
0
    for (int x = puPos.x; x < xEnd; x += dx)
360
0
    {
361
0
      subCu.UnitArea::operator=(UnitArea(cu.chromaFormat, Area(x, y, dx, dy)));
362
0
      subCu = cuMotion;
363
0
      PelUnitBuf subPredBuf = predBuf.subBuf(UnitAreaRelative(cu, subCu));
364
365
0
      motionCompensation(subCu, subPredBuf, refPicList);
366
0
    }
367
0
  }
368
0
}
369
void InterPrediction::xPredInterUni( const CodingUnit &cu, const RefPicList &refPicList, PelUnitBuf &pcYuvPred, const bool bi, const bool bdofApplied )
370
26.8k
{
371
26.8k
  int iRefIdx = cu.refIdx[refPicList];
372
26.8k
  Mv mv[3];
373
26.8k
  bool isIBC = false;
374
26.8k
  CHECK(!CU::isIBC(cu) && cu.lwidth() == 4 && cu.lheight() == 4, "invalid 4x4 inter blocks");
375
26.8k
  if (CU::isIBC(cu))
376
26.8k
  {
377
26.8k
    isIBC = true;
378
26.8k
  }
379
26.8k
  if (cu.affine)
380
0
  {
381
0
    CHECK(iRefIdx < 0, "iRefIdx incorrect.");
382
383
0
    mv[0] = cu.mv[refPicList][0];
384
0
    mv[1] = cu.mv[refPicList][1];
385
0
    mv[2] = cu.mv[refPicList][2];
386
0
  }
387
26.8k
  else
388
26.8k
  {
389
26.8k
    mv[0] = cu.mv[refPicList][0];
390
26.8k
    if (!isIBC )
391
0
      clipMv(mv[0], cu.lumaPos(), cu.lumaSize(), *cu.cs->pcv);
392
26.8k
  }
393
394
107k
  for( uint32_t comp = COMP_Y; comp < pcYuvPred.bufs.size(); comp++ )
395
80.5k
  {
396
80.5k
    const ComponentID compID = ComponentID( comp );
397
398
80.5k
    bool luma   = !cu.mccNoLuma  ();
399
80.5k
    bool chroma = !cu.mccNoChroma();
400
401
80.5k
    if( compID == COMP_Y && !luma )
402
0
      continue;
403
80.5k
    if( compID != COMP_Y && !chroma )
404
53.7k
      continue;
405
406
26.8k
    if( cu.affine )
407
0
    {
408
0
      xPredAffineBlk( compID, cu, cu.slice->getRefPic( refPicList, iRefIdx ), mv, pcYuvPred, bi, cu.slice->clpRngs[ compID ], refPicList );
409
0
    }
410
26.8k
    else
411
26.8k
    {
412
26.8k
      if( isIBC )
413
26.8k
      {
414
26.8k
        xPredInterBlk( compID, cu, cu.slice->pic, mv[ 0 ], pcYuvPred, bi, cu.slice->clpRngs[ compID ], bdofApplied, isIBC );
415
26.8k
      }
416
0
      else
417
0
      {
418
0
        xPredInterBlk( compID, cu, cu.slice->getRefPic( refPicList, iRefIdx ), mv[ 0 ], pcYuvPred, bi, cu.slice->clpRngs[ compID ], bdofApplied, isIBC, refPicList );
419
0
      }
420
26.8k
    }
421
26.8k
  }
422
26.8k
}
423
424
void InterPrediction::xPredInterBi( const CodingUnit& cu, PelUnitBuf& yuvPred, const bool bdofApplied, PelUnitBuf *yuvPredTmp )
425
0
{
426
0
  CHECK( !cu.affine && cu.refIdx[0] >= 0 && cu.refIdx[1] >= 0 && ( cu.lwidth() + cu.lheight() == 12 ), "invalid 4x8/8x4 bi-predicted blocks" );
427
428
0
  PelUnitBuf puBuf[NUM_REF_PIC_LIST_01];
429
0
  for (uint32_t refList = 0; refList < NUM_REF_PIC_LIST_01; refList++)
430
0
  {
431
0
    if( cu.refIdx[refList] < 0)
432
0
    {
433
0
      continue;
434
0
    }
435
436
0
    RefPicList refPicList = (refList ? REF_PIC_LIST_1 : REF_PIC_LIST_0);
437
438
0
    CHECK(CU::isIBC(cu) && refPicList != REF_PIC_LIST_0, "Invalid interdir for ibc mode");
439
0
    CHECK(CU::isIBC(cu) && cu.refIdx[refList] != MAX_NUM_REF, "Invalid reference index for ibc mode");
440
0
    CHECK((CU::isInter(cu) && cu.refIdx[refList] >= cu.cs->slice->numRefIdx[ refPicList ]), "Invalid reference index");
441
442
0
    if( cu.refIdx[0] < 0 || cu.refIdx[1] < 0 )
443
0
    {
444
      // uni-prediction: the filters already write the final samples (clipped, or unclipped high precision for GEO's
445
      // later blend), so predict straight into yuvPred instead of into m_yuvPred plus a copy
446
0
      xPredInterUni( cu, refPicList, yuvPred, cu.geo, bdofApplied );
447
0
      return;
448
0
    }
449
450
0
    puBuf[refList] = m_yuvPred[refList].getCompactBuf( cu );
451
0
    xPredInterUni ( cu, refPicList, puBuf[refList], true, bdofApplied );
452
0
  }
453
454
0
  xWeightedAverage( cu, puBuf[0], puBuf[1], yuvPred, bdofApplied, yuvPredTmp );
455
0
}
456
457
void InterPrediction::motionCompensationIBC( CodingUnit& cu, PelUnitBuf& predBuf )
458
26.8k
{
459
  // dual tree handling for IBC as the only ref
460
26.8k
  xPredInterUni( cu, REF_PIC_LIST_0, predBuf, false, false );
461
26.8k
}
462
463
bool InterPrediction::motionCompensation( CodingUnit& cu, PelUnitBuf& predBuf, const RefPicList& refPicList, PelUnitBuf* predBufDfltWght )
464
0
{
465
0
  bool ret = false;
466
0
  if( refPicList != REF_PIC_LIST_X )
467
0
  {
468
0
    xPredInterUni( cu, refPicList, predBuf, false, false );
469
0
  }
470
0
  else
471
0
  {
472
0
    CHECK( !cu.affine && cu.refIdx[0] >= 0 && cu.refIdx[1] >= 0 && ( cu.lwidth() + cu.lheight() == 12 ), "Invalid 4x8/8x4 bi-predicted blocks" );
473
474
0
    bool bdofApplied = false;
475
0
    if( cu.cs->sps->BDOF && ( !cu.cs->picHeader->disBdofFlag ) )
476
0
    {
477
0
      if( cu.affine || m_subPuMC || cu.ciip )
478
0
      {
479
0
        bdofApplied = false;
480
0
      }
481
0
      else
482
0
      {
483
0
        if( CU::isBiPredFromDifferentDirEqDistPoc( cu )
484
0
            &&    cu.Y().minDim() >= 8
485
0
            &&    cu.Y().area() >= 128
486
0
            &&   !cu.smvdMode
487
0
            && !( cu.cs->sps->BCW && cu.BcwIdx != BCW_DEFAULT )
488
0
            && !( cu.mccNoBdof() && cu.mmvdMergeFlag ) )
489
0
        {
490
0
          bdofApplied = true;
491
0
        }
492
0
      }
493
0
    }
494
495
0
    bool dmvrApplied = cu.mvRefine && CU::checkDMVRCondition( cu );
496
0
    if( cu.lumaSize().maxDim() > MAX_BDOF_APPLICATION_REGION && cu.mergeType != MRG_TYPE_SUBPU_ATMVP && bdofApplied && !dmvrApplied )
497
0
    {
498
0
      xSubPuBDOF( cu, predBuf, refPicList );
499
0
    }
500
0
    else if (cu.mergeType != MRG_TYPE_DEFAULT_N && cu.mergeType != MRG_TYPE_IBC)
501
0
    {
502
0
      xSubPuMC(cu, predBuf, refPicList);
503
0
    }
504
0
    else if( xCheckIdenticalMotion( cu ) )
505
0
    {
506
0
      xPredInterUni( cu, REF_PIC_LIST_0, predBuf, false, false );
507
508
0
      if( predBufDfltWght )
509
0
      {
510
0
        predBufDfltWght->copyFrom( predBuf );
511
0
      }
512
0
    }
513
0
    else if( dmvrApplied )
514
0
    {
515
0
      xProcessDMVR( cu, predBuf, cu.slice->clpRngs, bdofApplied );
516
0
    }
517
0
    else
518
0
    {
519
0
      xPredInterBi( cu, predBuf, bdofApplied, predBufDfltWght );
520
0
    }
521
522
0
    DTRACE( g_trace_ctx, D_MOT_COMP, "BDOF=%d, DMVR=%d\n", bdofApplied, dmvrApplied );
523
0
    ret = bdofApplied || dmvrApplied;
524
0
  }
525
526
0
  DTRACE( g_trace_ctx, D_MOT_COMP, "MV=%d,%d\n", cu.mv[0][0].hor, cu.mv[0][0].ver );
527
0
  DTRACE( g_trace_ctx, D_MOT_COMP, "MV=%d,%d\n", cu.mv[1][0].hor, cu.mv[1][0].ver );
528
0
  DTRACE_PEL_BUF( D_MOT_COMP, predBuf.Y(), cu, cu.predMode, COMP_Y );
529
0
  if( cu.chromaFormat != VVENC_CHROMA_400 )
530
0
  {
531
0
    DTRACE_PEL_BUF( D_MOT_COMP, predBuf.Cb(), cu, cu.predMode, COMP_Cb );
532
0
    DTRACE_PEL_BUF( D_MOT_COMP, predBuf.Cr(), cu, cu.predMode, COMP_Cr );
533
0
  }
534
535
0
  return ret;
536
0
}
537
538
void InterPrediction::xSubPuMC(CodingUnit& cu, PelUnitBuf& predBuf, const RefPicList& eRefPicList /*= REF_PIC_LIST_X*/)
539
0
{
540
0
  Position puPos  = cu.lumaPos();
541
0
  Size     puSize = cu.lumaSize();
542
543
0
  int numPartLine = std::max( puSize.width  >> ATMVP_SUB_BLOCK_SIZE, 1u );
544
0
  int numPartCol  = std::max( puSize.height >> ATMVP_SUB_BLOCK_SIZE, 1u );
545
0
  int puHeight    = numPartCol == 1 ? puSize.height : 1 << ATMVP_SUB_BLOCK_SIZE;
546
0
  int puWidth     = numPartLine == 1 ? puSize.width : 1 << ATMVP_SUB_BLOCK_SIZE;
547
548
0
  CodingUnit subCu = cu;
549
0
  subCu.cs = cu.cs;
550
0
  subCu.mergeType = MRG_TYPE_DEFAULT_N;
551
552
0
  bool isAffine = cu.affine;
553
0
  subCu.affine = false;
554
555
  // join sub-pus containing the same motion
556
0
  bool verMC    = puSize.height > puSize.width;
557
0
  int  fstStart = (!verMC ? puPos.y : puPos.x);
558
0
  int  secStart = (!verMC ? puPos.x : puPos.y);
559
0
  int  fstEnd   = (!verMC ? puPos.y + puSize.height : puPos.x + puSize.width);
560
0
  int  secEnd   = (!verMC ? puPos.x + puSize.width : puPos.y + puSize.height);
561
0
  int  fstStep  = (!verMC ? puHeight : puWidth);
562
0
  int  secStep  = (!verMC ? puWidth : puHeight);
563
564
0
  cu.refIdx[0] = 0;
565
0
  cu.refIdx[1] = cu.cs->slice->sliceType == VVENC_B_SLICE ? 0 : -1;
566
0
  bool scaled = false;//!CU::isRefPicSameSize(cu);
567
568
0
  m_subPuMC = true;
569
570
0
  for (int fstDim = fstStart; fstDim < fstEnd; fstDim += fstStep)
571
0
  {
572
0
    for (int secDim = secStart; secDim < secEnd; secDim += secStep)
573
0
    {
574
0
      int x = !verMC ? secDim : fstDim;
575
0
      int y = !verMC ? fstDim : secDim;
576
0
      const MotionInfo &curMi = cu.getMotionInfo(Position{ x, y });
577
578
0
      int length = secStep;
579
0
      int later = secDim + secStep;
580
581
0
      while (later < secEnd)
582
0
      {
583
0
        const MotionInfo &laterMi = !verMC ? cu.getMotionInfo(Position{ later, fstDim }) : cu.getMotionInfo(Position{ fstDim, later });
584
0
        if (!scaled && laterMi == curMi)
585
0
        {
586
0
          length += secStep;
587
0
        }
588
0
        else
589
0
        {
590
0
          break;
591
0
        }
592
0
        later += secStep;
593
0
      }
594
0
      int dx = !verMC ? length : puWidth;
595
0
      int dy = !verMC ? puHeight : length;
596
597
0
      subCu.UnitArea::operator=(UnitArea(cu.chromaFormat, Area(x, y, dx, dy)));
598
0
      subCu = curMi;
599
0
      PelUnitBuf subPredBuf = predBuf.subBuf(UnitAreaRelative(cu, subCu));
600
0
      subCu.mcControl = (cu.mcControl >> 1) << 1;
601
0
      subCu.mvRefine = false;
602
0
      motionCompensation(subCu, subPredBuf, eRefPicList);
603
0
      secDim = later - secStep;
604
0
    }
605
0
  }
606
0
  m_subPuMC = false;
607
608
0
  cu.affine = isAffine;
609
0
}
610
611
static inline int xRightShiftMSB( int numer, int denom )
612
0
{
613
0
  return numer >> floorLog2( denom );
614
0
}
615
616
void xFpBiDirOptFlowCore( const Pel* srcY0, const Pel* srcY1, const Pel* gradX0, const Pel* gradX1, const Pel* gradY0,
617
                          const Pel* gradY1, const int width, const int height, Pel* dstY, const ptrdiff_t dstStride,
618
                          const int shiftNum, const int offset, const int limit, const ClpRng& clpRng,
619
                          const int bitDepth )
620
0
{
621
0
  int xUnit = width >> 2;
622
0
  int yUnit = height >> 2;
623
0
  ptrdiff_t widthG = width + 2 * BDOF_EXTEND_SIZE;
624
625
0
  ptrdiff_t offsetPos = widthG * BDOF_EXTEND_SIZE + BDOF_EXTEND_SIZE;
626
0
  ptrdiff_t stridePredMC = widthG + 2;
627
628
0
  const ptrdiff_t  src0Stride = stridePredMC;
629
0
  const ptrdiff_t  src1Stride = stridePredMC;
630
631
0
  const Pel* srcY0Temp = srcY0;
632
0
  const Pel* srcY1Temp = srcY1;
633
634
0
  for( int yu = 0; yu < yUnit; yu++ )
635
0
  {
636
0
    for( int xu = 0; xu < xUnit; xu++ )
637
0
    {
638
0
      int tmpx = 0, tmpy = 0;
639
0
      int sumAbsGX = 0, sumAbsGY = 0, sumDIX = 0, sumDIY = 0;
640
0
      int sumSignGY_GX = 0;
641
642
0
      const Pel* pGradX0Tmp = gradX0 + ( xu << 2 ) + ( yu << 2 ) * widthG;
643
0
      const Pel* pGradX1Tmp = gradX1 + ( xu << 2 ) + ( yu << 2 ) * widthG;
644
0
      const Pel* pGradY0Tmp = gradY0 + ( xu << 2 ) + ( yu << 2 ) * widthG;
645
0
      const Pel* pGradY1Tmp = gradY1 + ( xu << 2 ) + ( yu << 2 ) * widthG;
646
0
      const Pel* SrcY1Tmp = srcY1 + ( xu << 2 ) + ( yu << 2 ) * src1Stride;
647
0
      const Pel* SrcY0Tmp = srcY0 + ( xu << 2 ) + ( yu << 2 ) * src0Stride;
648
649
0
      calcBDOFSumsCore( SrcY0Tmp, SrcY1Tmp, pGradX0Tmp, pGradX1Tmp, pGradY0Tmp, pGradY1Tmp, xu, yu, src0Stride,
650
0
                        src1Stride, widthG, bitDepth, &sumAbsGX, &sumAbsGY, &sumDIX, &sumDIY, &sumSignGY_GX );
651
0
      tmpx = ( sumAbsGX == 0 ? 0 : xRightShiftMSB( 4 * sumDIX, sumAbsGX ) );
652
0
      tmpx = Clip3( -limit, limit, tmpx );
653
654
0
      const int tmpData = sumSignGY_GX * tmpx >> 1;
655
0
      tmpy = ( sumAbsGY == 0 ? 0 : xRightShiftMSB( ( 4 * sumDIY - tmpData ), sumAbsGY ) );
656
0
      tmpy = Clip3( -limit, limit, tmpy );
657
658
0
      srcY0Temp = srcY0 + ( stridePredMC + 1 ) + ( ( yu * src0Stride + xu ) << 2 );
659
0
      srcY1Temp = srcY1 + ( stridePredMC + 1 ) + ( ( yu * src0Stride + xu ) << 2 );
660
0
      pGradX0Tmp = gradX0 + offsetPos + ( ( yu * widthG + xu ) << 2 );
661
0
      pGradX1Tmp = gradX1 + offsetPos + ( ( yu * widthG + xu ) << 2 );
662
0
      pGradY0Tmp = gradY0 + offsetPos + ( ( yu * widthG + xu ) << 2 );
663
0
      pGradY1Tmp = gradY1 + offsetPos + ( ( yu * widthG + xu ) << 2 );
664
665
0
      Pel* dstY0 = dstY + ( ( yu * dstStride + xu ) << 2 );
666
0
      addBDOFAvgCore( srcY0Temp, src0Stride, srcY1Temp, src1Stride, dstY0, dstStride, pGradX0Tmp, pGradX1Tmp,
667
0
                      pGradY0Tmp, pGradY1Tmp, widthG, ( 1 << 2 ), ( 1 << 2 ), tmpx, tmpy, shiftNum, offset, clpRng );
668
0
    } // xu
669
0
  } // yu
670
0
}
671
672
InterPredInterpolation::InterPredInterpolation()
673
5.72k
  : m_storedMv(nullptr)
674
5.72k
  , m_skipPROF(false)
675
5.72k
  , m_encOnly(false)
676
5.72k
  , m_isBi(false)
677
5.72k
  , m_ifpLines(0)
678
5.72k
{
679
680
5.72k
}
681
682
InterPredInterpolation::~InterPredInterpolation()
683
5.72k
{
684
5.72k
  destroy();
685
5.72k
}
686
687
void InterPredInterpolation::destroy()
688
5.72k
{
689
22.9k
  for( uint32_t c = 0; c < MAX_NUM_COMP; c++ )
690
17.1k
  {
691
85.9k
    for( uint32_t i = 0; i < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS_SIGNAL; i++ )
692
68.7k
    {
693
343k
      for( uint32_t j = 0; j < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS_SIGNAL; j++ )
694
274k
      {
695
274k
        xFree( m_filteredBlock[i][j][c] );
696
274k
        m_filteredBlock[i][j][c] = nullptr;
697
274k
      }
698
699
68.7k
      xFree( m_filteredBlockTmp[i][c] );
700
68.7k
      m_filteredBlockTmp[i][c] = nullptr;
701
68.7k
    }
702
17.1k
  }
703
5.72k
  xFree(m_gradX0);   m_gradX0 = nullptr;
704
5.72k
  xFree(m_gradY0);   m_gradY0 = nullptr;
705
5.72k
  xFree(m_gradX1);   m_gradX1 = nullptr;
706
5.72k
  xFree(m_gradY1);   m_gradY1 = nullptr;
707
708
5.72k
  if (m_storedMv != nullptr)
709
5.72k
  {
710
5.72k
    delete[] m_storedMv;
711
5.72k
    m_storedMv = nullptr;
712
5.72k
  }
713
5.72k
}
714
715
void InterPredInterpolation::init( bool enableOpt )
716
5.72k
{
717
22.9k
  for( uint32_t c = 0; c < MAX_NUM_COMP; c++ )
718
17.1k
  {
719
17.1k
    int extWidth = MAX_CU_SIZE + (2 * BDOF_EXTEND_SIZE + 2) + 16;
720
17.1k
    int extHeight = MAX_CU_SIZE + (2 * BDOF_EXTEND_SIZE + 2) + 1;
721
17.1k
    extWidth = extWidth > (MAX_CU_SIZE + (2 * DMVR_NUM_ITERATION) + 16) ? extWidth : MAX_CU_SIZE + (2 * DMVR_NUM_ITERATION) + 16;
722
17.1k
    extHeight = extHeight > (MAX_CU_SIZE + (2 * DMVR_NUM_ITERATION) + 1) ? extHeight : MAX_CU_SIZE + (2 * DMVR_NUM_ITERATION) + 1;
723
85.9k
    for( uint32_t i = 0; i < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS_SIGNAL; i++ )
724
68.7k
    {
725
68.7k
      m_filteredBlockTmp[i][c] = ( Pel* ) xMalloc( Pel, ( extWidth + 4 ) * ( extHeight + 7 + 4 ) );
726
68.7k
      VALGRIND_MEMCLEAR( m_filteredBlockTmp[i][c], sizeof( Pel ) * (extWidth + 4) * (extHeight + 7 + 4) );
727
728
343k
      for( uint32_t j = 0; j < LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS_SIGNAL; j++ )
729
274k
      {
730
        // Only the fractional luma motion search reads the quarter-sample filtered blocks, so the
731
        // chroma ones are not allocated. That saves 32 blocks of about 39 KB per encoder thread.
732
274k
        if( c != COMP_Y )
733
183k
        {
734
183k
          m_filteredBlock[i][j][c] = nullptr;
735
183k
          continue;
736
183k
        }
737
91.6k
        m_filteredBlock[i][j][c] = ( Pel* ) xMalloc( Pel, extWidth * extHeight );
738
91.6k
        VALGRIND_MEMCLEAR( m_filteredBlock[i][j][c], sizeof( Pel ) * extWidth * extHeight );
739
91.6k
      }
740
68.7k
    }
741
17.1k
  }
742
743
5.72k
  m_gradX0 = (Pel*)xMalloc(Pel, BDOF_TEMP_BUFFER_SIZE);
744
5.72k
  m_gradY0 = (Pel*)xMalloc(Pel, BDOF_TEMP_BUFFER_SIZE);
745
5.72k
  m_gradX1 = (Pel*)xMalloc(Pel, BDOF_TEMP_BUFFER_SIZE);
746
5.72k
  m_gradY1 = (Pel*)xMalloc(Pel, BDOF_TEMP_BUFFER_SIZE);
747
748
5.72k
  VALGRIND_MEMCLEAR( m_gradX0, sizeof( Pel ) * BDOF_TEMP_BUFFER_SIZE );
749
5.72k
  VALGRIND_MEMCLEAR( m_gradY0, sizeof( Pel ) * BDOF_TEMP_BUFFER_SIZE );
750
5.72k
  VALGRIND_MEMCLEAR( m_gradX1, sizeof( Pel ) * BDOF_TEMP_BUFFER_SIZE );
751
5.72k
  VALGRIND_MEMCLEAR( m_gradY1, sizeof( Pel ) * BDOF_TEMP_BUFFER_SIZE );
752
753
5.72k
  m_if.initInterpolationFilter( true );
754
755
5.72k
  xFpBiDirOptFlow   = xFpBiDirOptFlowCore;
756
5.72k
  xFpBDOFGradFilter = gradFilterCore;
757
5.72k
  xFpProfGradFilter = gradFilterCore<false>;
758
5.72k
  xFpApplyPROF      = applyPROFCore;
759
5.72k
  for (auto& byBi : xFpAffineLumaCU)
760
11.4k
  {
761
11.4k
    byBi[0] = byBi[1] = nullptr;
762
11.4k
  }
763
5.72k
  xFpPadDmvr        = padDmvrCore;
764
765
5.72k
  if( enableOpt )
766
5.72k
  {
767
#if ENABLE_SIMD_OPT_BDOF && defined( TARGET_SIMD_X86 )
768
    initInterPredictionX86();
769
#endif
770
#if ENABLE_SIMD_OPT_BDOF && defined( TARGET_SIMD_ARM )
771
    initInterPredictionARM();
772
#endif
773
5.72k
  }
774
775
5.72k
  if (m_storedMv == nullptr)
776
5.72k
  {
777
5.72k
    const int MVBUFFER_SIZE = MAX_CU_SIZE / MIN_PU_SIZE;
778
5.72k
    m_storedMv = new Mv[MVBUFFER_SIZE*MVBUFFER_SIZE];
779
#if ENABLE_VALGRIND_CODE
780
    for( int i = 0; i < MVBUFFER_SIZE * MVBUFFER_SIZE; i++ )
781
    {
782
      m_storedMv[i].setZero();
783
    }
784
#endif
785
5.72k
  }
786
5.72k
}
787
788
void InterPredInterpolation::xPredInterBlk( const ComponentID compID, const CodingUnit &cu,
789
                                            const Picture *refPic, const Mv &_mv, PelUnitBuf &dstPic,
790
                                            const bool bi, const ClpRng &clpRng, const bool bdofApplied, const bool isIBC, const RefPicList refPicList,
791
                                            const SizeType dmvrWidth, const SizeType dmvrHeight,
792
                                            const bool bilinearMC, const Pel *srcPadBuf, const int32_t srcPadStride )
793
26.8k
{
794
26.8k
  const ChromaFormat  chFmt = cu.chromaFormat;
795
26.8k
  const bool          rndRes = !bi;
796
797
26.8k
  int shiftHor = MV_FRACTIONAL_BITS_INTERNAL + getComponentScaleX(compID, chFmt);
798
26.8k
  int shiftVer = MV_FRACTIONAL_BITS_INTERNAL + getComponentScaleY(compID, chFmt);
799
800
26.8k
  Mv    mv(_mv);
801
802
26.8k
  CHECKD( m_ifpLines && !srcPadBuf && cu.cs->picture != refPic && !CU::isMvInRangeFPP( cu[compID].y, cu[compID].height, mv.ver, m_ifpLines, *cu.cs->pcv, getComponentScaleY(compID, chFmt) ), "xPredInterBlk: CTU line-wise FPP MV restriction failed!\n" );
803
804
26.8k
  int xFrac = mv.hor & ((1 << shiftHor) - 1);
805
26.8k
  int yFrac = mv.ver & ((1 << shiftVer) - 1);
806
26.8k
  if (isIBC)
807
26.8k
  {
808
26.8k
    xFrac = yFrac = 0;
809
26.8k
  }
810
811
26.8k
  PelBuf& dstBuf  = dstPic.bufs[compID];
812
26.8k
  unsigned width  = dstBuf.width;
813
26.8k
  unsigned height = dstBuf.height;
814
815
26.8k
  const Pel* refBufPtr;
816
26.8k
  int        refBufStride;
817
818
26.8k
  if( srcPadBuf )
819
0
  {
820
0
    refBufPtr    = srcPadBuf;
821
0
    refBufStride = srcPadStride;
822
0
  }
823
26.8k
  else
824
26.8k
  {
825
26.8k
    Position offset = cu.blocks[compID].pos().offset( mv.hor >> shiftHor, mv.ver >> shiftVer );
826
26.8k
    refBufPtr       = refPic->getRecoBufPtr   ( compID );
827
26.8k
    refBufStride    = refPic->getRecoBufStride( compID );
828
26.8k
    refBufPtr      += offset.x;
829
26.8k
    refBufPtr      += offset.y * refBufStride;
830
26.8k
  }
831
832
26.8k
  if( dmvrWidth )
833
0
  {
834
0
    width  = dmvrWidth;
835
0
    height = dmvrHeight;
836
0
  }
837
  // backup data
838
26.8k
  const int backupWidth = width;
839
26.8k
  const int backupHeight = height;
840
26.8k
  Pel* backupDstBufPtr = dstBuf.buf;
841
26.8k
  int backupDstBufStride = dstBuf.stride;
842
26.8k
  if( bdofApplied && compID == COMP_Y )
843
0
  {
844
0
    width = width + 2 * BDOF_EXTEND_SIZE + 2;
845
0
    height = height + 2 * BDOF_EXTEND_SIZE + 2;
846
847
    // change MC output
848
0
    CHECK( refPicList >= NUM_REF_PIC_LIST_01, "Wrong refpiclist" );
849
0
    dstBuf.stride = width;
850
0
    dstBuf.buf = m_filteredBlockTmp[2 + refPicList][compID] + 2 * dstBuf.stride + 2;
851
0
  }
852
26.8k
  bool useAltHpelIf = cu.imv == IMV_HPEL;
853
854
26.8k
  if( bilinearMC )
855
0
  {
856
0
    m_if.filterN2_2D( compID, refBufPtr, refBufStride, dstBuf.buf, dstBuf.stride, width, height, xFrac, yFrac, clpRng );
857
0
  }
858
26.8k
  else if( yFrac == 0 )
859
26.8k
  {
860
26.8k
    m_if.filterHor(compID, refBufPtr, refBufStride, dstBuf.buf, dstBuf.stride, backupWidth, backupHeight, xFrac, rndRes, chFmt, clpRng, useAltHpelIf, 0);
861
26.8k
  }
862
0
  else if( xFrac == 0 )
863
0
  {
864
0
    m_if.filterVer(compID, refBufPtr, refBufStride, dstBuf.buf, dstBuf.stride, backupWidth, backupHeight, yFrac, true, rndRes, chFmt, clpRng, useAltHpelIf, 0);
865
0
  }
866
0
  else if( backupWidth == 4 && backupHeight == 4 )
867
0
  {
868
0
    m_if.filter4x4( compID, refBufPtr, refBufStride ,(Pel*)dstBuf.buf, dstBuf.stride, 4, 4, xFrac, yFrac, rndRes, chFmt, clpRng, useAltHpelIf );
869
0
  }
870
0
  else if( backupWidth == 16 )
871
0
  {
872
0
    m_if.filter16xH( compID, refBufPtr, refBufStride, dstBuf.buf, dstBuf.stride, 16, backupHeight, xFrac, yFrac, rndRes, chFmt, clpRng, useAltHpelIf );
873
0
  }
874
0
  else if( backupWidth == 8 )
875
0
  {
876
0
    m_if.filter8xH( compID, refBufPtr, refBufStride, dstBuf.buf, dstBuf.stride, 8, backupHeight, xFrac, yFrac, rndRes, chFmt, clpRng, useAltHpelIf );
877
0
  }
878
0
  else
879
0
  {
880
0
    const int vFilterSize = isLuma( compID ) ? NTAPS_LUMA : NTAPS_CHROMA;
881
882
0
    PelBuf tmpBuf( m_filteredBlockTmp[0][compID], dmvrWidth ? dmvrWidth : dstBuf.stride, dmvrWidth ? Size( dmvrWidth, dmvrHeight ) : cu.blocks[compID].size() );
883
884
0
    m_if.filterHor(compID, refBufPtr -  ((vFilterSize >> 1) - 1) * refBufStride,  refBufStride,  tmpBuf.buf, tmpBuf.stride, backupWidth, backupHeight + vFilterSize - 1, xFrac, false, chFmt, clpRng, useAltHpelIf, 0);
885
0
    m_if.filterVer(compID, tmpBuf.buf + ((vFilterSize >> 1) - 1) * tmpBuf.stride, tmpBuf.stride, dstBuf.buf, dstBuf.stride, backupWidth, backupHeight, yFrac, false, rndRes, chFmt, clpRng, useAltHpelIf, 0);
886
0
  }
887
888
26.8k
  if (bdofApplied && compID == COMP_Y)
889
0
  {
890
0
    const unsigned shift = std::max<int>(2, (IF_INTERNAL_PREC - clpRng.bd));
891
0
    int xOffset = (xFrac < 8) ? 1 : 0;
892
0
    int yOffset = (yFrac < 8) ? 1 : 0;
893
0
    const Pel* refPel = refBufPtr - yOffset * refBufStride - xOffset;
894
0
    Pel* dstPel = m_filteredBlockTmp[2 + refPicList][compID] + dstBuf.stride + 1;
895
0
    for (int w = 0; w < (width - 2 * BDOF_EXTEND_SIZE); w++)
896
0
    {
897
0
      Pel val = leftShiftU(refPel[w], shift);
898
0
      dstPel[w] = val - (Pel)IF_INTERNAL_OFFS;
899
0
    }
900
901
0
    refPel = refBufPtr + (1 - yOffset)*refBufStride - xOffset;
902
0
    dstPel = m_filteredBlockTmp[2 + refPicList][compID] + 2 * dstBuf.stride + 1;
903
0
    for (int h = 0; h < (height - 2 * BDOF_EXTEND_SIZE - 2); h++)
904
0
    {
905
0
      Pel val = leftShiftU(refPel[0], shift);
906
0
      dstPel[0] = val - (Pel)IF_INTERNAL_OFFS;
907
908
0
      val = leftShiftU(refPel[width - 3], shift);
909
0
      dstPel[width - 3] = val - (Pel)IF_INTERNAL_OFFS;
910
911
0
      refPel += refBufStride;
912
0
      dstPel += dstBuf.stride;
913
0
    }
914
915
0
    refPel = refBufPtr + (height - 2 * BDOF_EXTEND_SIZE - 2 + 1 - yOffset)*refBufStride - xOffset;
916
0
    dstPel = m_filteredBlockTmp[2 + refPicList][compID] + (height - 2 * BDOF_EXTEND_SIZE)*dstBuf.stride + 1;
917
0
    for (int w = 0; w < (width - 2 * BDOF_EXTEND_SIZE); w++)
918
0
    {
919
0
      Pel val = leftShiftU(refPel[w], shift);
920
0
      dstPel[w] = val - (Pel)IF_INTERNAL_OFFS;
921
0
    }
922
923
    // restore data
924
0
    width         = backupWidth;
925
0
    height        = backupHeight;
926
0
    dstBuf.buf    = backupDstBufPtr;
927
0
    dstBuf.stride = backupDstBufStride;
928
0
  }
929
26.8k
}
930
931
void InterPredInterpolation::xApplyBDOF( PelBuf& yuvDst, const ClpRng& clpRng )
932
0
{
933
0
  const int     bitDepth  = clpRng.bd;
934
935
0
  const int     height    = yuvDst.height;
936
0
  const int     width     = yuvDst.width;
937
0
  int           heightG   = height + 2 * BDOF_EXTEND_SIZE;
938
0
  int           widthG    = width + 2 * BDOF_EXTEND_SIZE;
939
940
0
  Pel*          gradX0 = m_gradX0;
941
0
  Pel*          gradX1 = m_gradX1;
942
0
  Pel*          gradY0 = m_gradY0;
943
0
  Pel*          gradY1 = m_gradY1;
944
945
0
  int           stridePredMC = widthG + 2;
946
0
  const Pel*    srcY0 = m_filteredBlockTmp[2][COMP_Y] + stridePredMC + 1;
947
0
  const Pel*    srcY1 = m_filteredBlockTmp[3][COMP_Y] + stridePredMC + 1;
948
949
0
  Pel*          dstY = yuvDst.buf;
950
0
  const int dstStride = yuvDst.stride;
951
952
0
  const unsigned shiftNum = IF_INTERNAL_PREC + 1 - bitDepth;
953
0
  const int   offset = (1 << (shiftNum - 1)) + 2 * IF_INTERNAL_OFFS;
954
0
  const int   limit = (1 << 4) - 1;
955
956
0
  if( width == 16 && height <= MAX_BDOF_APPLICATION_REGION && xFpBDOF16 )
957
0
  {
958
    // gradients, sums and average in one pass, without the gradient planes and the padding below
959
0
    xFpBDOF16( srcY0 + stridePredMC + 1, srcY1 + stridePredMC + 1, stridePredMC, height, dstY, dstStride, shiftNum, offset, limit, clpRng );
960
0
    return;
961
0
  }
962
963
0
  for (int refList = 0; refList < NUM_REF_PIC_LIST_01; refList++)
964
0
  {
965
0
    Pel* dstTempPtr = m_filteredBlockTmp[2 + refList][COMP_Y] + stridePredMC + 1;
966
0
    Pel* gradY = (refList == 0) ? m_gradY0 : m_gradY1;
967
0
    Pel* gradX = (refList == 0) ? m_gradX0 : m_gradX1;
968
969
0
    xFpBDOFGradFilter(dstTempPtr, stridePredMC, widthG, heightG, widthG, gradX, gradY, bitDepth );
970
0
    Pel* padStr = m_filteredBlockTmp[2 + refList][COMP_Y] + 2 * stridePredMC + 2;
971
0
    for (int y = 0; y< height; y++)
972
0
    {
973
0
      padStr[-1] = padStr[0];
974
0
      padStr[width] = padStr[width - 1];
975
0
      padStr += stridePredMC;
976
0
    }
977
978
0
    padStr = m_filteredBlockTmp[2 + refList][COMP_Y] + 2 * stridePredMC + 1;
979
0
    ::memcpy(padStr - stridePredMC, padStr, sizeof(Pel)*(widthG));
980
0
    ::memcpy(padStr + height*stridePredMC, padStr + (height - 1)*stridePredMC, sizeof(Pel)*(widthG));
981
0
  }
982
983
0
  xFpBiDirOptFlow( srcY0, srcY1, gradX0, gradX1, gradY0, gradY1, width, height, dstY, dstStride, shiftNum, offset,
984
0
                   limit, clpRng, bitDepth );
985
0
}
986
987
void InterPredInterpolation::xWeightedAverage( const CodingUnit& cu, const PelUnitBuf& pcYuvSrc0, const PelUnitBuf& pcYuvSrc1, PelUnitBuf& pcYuvDst, const bool bdofApplied, PelUnitBuf *yuvPredTmp )
988
0
{
989
0
  const bool lumaOnly     = cu.mccNoChroma();
990
0
  const bool chromaOnly   = cu.mccNoLuma  ();
991
992
0
  CHECK( chromaOnly && lumaOnly, "should not happen" );
993
994
  // uni-prediction is written straight into the destination (xPredInterBi)
995
0
  CHECKD( cu.refIdx[0] < 0 || cu.refIdx[1] < 0, "xWeightedAverage needs bi-prediction" );
996
997
0
  const ClpRngs& clpRngs  = cu.slice->clpRngs;
998
999
0
  if( cu.BcwIdx != BCW_DEFAULT && ( yuvPredTmp || !cu.ciip ) )
1000
0
  {
1001
0
    CHECK( bdofApplied, "BCW is disallowed with BIO" );
1002
1003
0
    pcYuvDst.addWeightedAvg( pcYuvSrc0, pcYuvSrc1, clpRngs, cu.BcwIdx, chromaOnly, lumaOnly );
1004
1005
0
    if( yuvPredTmp )
1006
0
    {
1007
0
      yuvPredTmp->addAvg( pcYuvSrc0, pcYuvSrc1, clpRngs, chromaOnly, lumaOnly );
1008
0
    }
1009
0
    return;
1010
0
  }
1011
1012
0
  if( bdofApplied && !chromaOnly )
1013
0
  {
1014
0
    xApplyBDOF( pcYuvDst.Y(), clpRngs[COMP_Y] );
1015
0
  }
1016
0
  if( !bdofApplied && ( lumaOnly || chromaOnly ) )
1017
0
  {
1018
0
    pcYuvDst.addAvg( pcYuvSrc0, pcYuvSrc1, clpRngs, chromaOnly, lumaOnly );
1019
0
  }
1020
0
  else if( !lumaOnly )   // with BDOF the luma is done above, and only chroma is left to average
1021
0
  {
1022
0
    pcYuvDst.addAvg( pcYuvSrc0, pcYuvSrc1, clpRngs, bdofApplied );
1023
0
  }
1024
0
}
1025
1026
void InterPrediction::motionCompensationGeo( CodingUnit &cu, PelUnitBuf &predBuf, const MergeCtx &geoMrgCtx )
1027
0
{
1028
0
  const ClpRngs &clpRngs      = cu.slice->clpRngs;
1029
0
  const UnitArea localUnitArea( cu.chromaFormat, Area( 0, 0, cu.lwidth(), cu.lheight() ) );
1030
1031
0
  PelUnitBuf     tmpGeoBuf0   = m_geoPartBuf[0].getBuf( localUnitArea );
1032
0
  PelUnitBuf     tmpGeoBuf1   = m_geoPartBuf[1].getBuf( localUnitArea );
1033
1034
0
  geoMrgCtx.setMergeInfo( cu, cu.geoMergeIdx[0] );
1035
0
  CU::spanMotionInfo    ( cu );
1036
0
  motionCompensation    ( cu, tmpGeoBuf0, REF_PIC_LIST_X );   // TODO: check 4:0:0 interaction with weighted prediction.
1037
1038
0
  geoMrgCtx.setMergeInfo( cu, cu.geoMergeIdx[1] );
1039
0
  CU::spanMotionInfo    ( cu );
1040
0
  motionCompensation    ( cu, tmpGeoBuf1, REF_PIC_LIST_X );   // TODO: check 4:0:0 interaction with weighted prediction.
1041
1042
0
  weightedGeoBlk( clpRngs, cu, cu.geoSplitDir, isChromaEnabled( cu.chromaFormat ) ? MAX_NUM_CH : CH_L, predBuf, tmpGeoBuf0, tmpGeoBuf1 );
1043
0
}
1044
1045
void InterPredInterpolation::weightedGeoBlk(const ClpRngs &clpRngs, CodingUnit& cu, const uint8_t splitDir,
1046
                                            int32_t channel, PelUnitBuf &predDst, PelUnitBuf &predSrc0, PelUnitBuf &predSrc1)
1047
0
{
1048
0
  if( channel != CH_C )
1049
0
  {
1050
0
    m_if.weightedGeoBlk( clpRngs, cu, cu.lumaSize().width, cu.lumaSize().height, COMP_Y, splitDir, predDst, predSrc0, predSrc1 );
1051
0
  }
1052
1053
0
  if( channel != CH_L && isChromaEnabled( cu.chromaFormat ) )
1054
0
  {
1055
0
    m_if.weightedGeoBlk( clpRngs, cu, cu.chromaSize().width, cu.chromaSize().height, COMP_Cb, splitDir, predDst, predSrc0, predSrc1 );
1056
0
    m_if.weightedGeoBlk( clpRngs, cu, cu.chromaSize().width, cu.chromaSize().height, COMP_Cr, splitDir, predDst, predSrc0, predSrc1 );
1057
0
  }
1058
0
}
1059
1060
5.72k
DMVR::DMVR() : m_pcRdCost( nullptr )
1061
5.72k
{
1062
5.72k
}
1063
1064
DMVR::~DMVR()
1065
5.72k
{
1066
5.72k
  destroy();
1067
5.72k
}
1068
1069
void DMVR::destroy()
1070
5.72k
{
1071
17.1k
  for( int i = 0; i < NUM_REF_PIC_LIST_01; i++ )
1072
11.4k
  {
1073
11.4k
    m_yuvPred[i].destroy();
1074
11.4k
    m_yuvPad[i].destroy();
1075
11.4k
    m_yuvTmp[i].destroy();
1076
11.4k
  }
1077
5.72k
  m_pcRdCost = nullptr;
1078
5.72k
}
1079
1080
void DMVR::init( RdCost* pcRdCost, const ChromaFormat chFormat )
1081
5.72k
{
1082
5.72k
  if( m_pcRdCost == nullptr )
1083
5.72k
  {
1084
5.72k
    m_pcRdCost = pcRdCost;
1085
1086
5.72k
    Area predArea = Area( 0, 0, DMVR_SUBCU_SIZE, DMVR_SUBCU_SIZE );
1087
5.72k
    Area refArea  = Area( 0, 0, MAX_CU_SIZE, MAX_CU_SIZE );
1088
17.1k
    for( int i = 0; i < NUM_REF_PIC_LIST_01; i++ )
1089
11.4k
    {
1090
11.4k
      m_yuvPred[i].create( chFormat, predArea );
1091
11.4k
      m_yuvTmp[i].create( CHROMA_400, refArea, 0, DMVR_NUM_ITERATION );
1092
11.4k
      m_yuvPad[i].create( chFormat, predArea, 0, DMVR_NUM_ITERATION + (NTAPS_LUMA>>1), 32 );
1093
      // the buffer m_yuvPad[i].bufs[0].buf is aligned to 32
1094
      // the actual begin of the written to buffer is m_yuvPad[i].bufs[0].buf - 3 * stride - 3 = m_yuvPad[i].bufs[0].buf - 99,
1095
      // which is not aligned with int. Since the margin on the left side is 1 sample too big, moving the buffer within the
1096
      // allocated memory 1 to the left doesn't cause problems
1097
11.4k
      m_yuvPad[i].bufs[0].buf--;
1098
11.4k
    }
1099
5.72k
  }
1100
5.72k
}
1101
1102
void DMVR::xCopyAndPad( const CodingUnit& cu, PelUnitBuf& pcPad, RefPicList refId, bool forLuma)
1103
0
{
1104
0
  int width, height;
1105
0
  Mv cMv;
1106
1107
0
  const Picture* refPic = cu.slice->getRefPic(refId, cu.refIdx[refId]);
1108
1109
0
  static constexpr int mvShift = MV_FRACTIONAL_BITS_INTERNAL;
1110
1111
0
  const int start = forLuma ? 0 : 1;
1112
0
  const int end   = forLuma ? 1 : MAX_NUM_COMP;
1113
1114
0
  for (int compID = start; compID < end; compID++)
1115
0
  {
1116
0
    int filtersize = compID == COMP_Y ? NTAPS_LUMA : NTAPS_CHROMA;
1117
0
    cMv            = cu.mv[refId][0];
1118
0
    width          = pcPad.bufs[compID].width;
1119
0
    height         = pcPad.bufs[compID].height;
1120
1121
0
    int mvshiftTemp = mvShift + getComponentScaleX((ComponentID)compID, cu.chromaFormat);
1122
1123
0
    width  += filtersize - 1;
1124
0
    height += filtersize - 1;
1125
0
    cMv    += Mv(-(((filtersize >> 1) - 1) << mvshiftTemp), -(((filtersize >> 1) - 1) << mvshiftTemp));
1126
1127
0
    clipMv(cMv, cu.lumaPos(), cu.lumaSize(), *cu.cs->pcv);
1128
1129
    /* Pre-fetch similar to HEVC*/
1130
0
    {
1131
0
      CPelBuf refBuf      = refPic->getRecoBuf(ComponentID(compID));
1132
0
      Position Rec_offset = cu.blocks[compID].pos().offset(cMv.hor >> mvshiftTemp, cMv.ver >> mvshiftTemp);
1133
0
      const Pel* refBufPtr = refBuf.bufAt(Rec_offset);
1134
1135
0
      PelBuf& dstBuf = pcPad.bufs[compID];
1136
1137
0
      const int leftTopFilterExt = ((filtersize >> 1) - 1);
1138
0
      const int padOffset        = leftTopFilterExt * dstBuf.stride + leftTopFilterExt;
1139
0
      const int padSize          = (DMVR_NUM_ITERATION) >> getComponentScaleX((ComponentID)compID, cu.chromaFormat);
1140
1141
0
      xFpPadDmvr( refBufPtr, refBuf.stride, dstBuf.buf - padOffset, dstBuf.stride, width, height, padSize );
1142
0
    }
1143
0
  }
1144
0
}
1145
1146
inline int32_t div_for_maxq7(int64_t N, int64_t D)
1147
0
{
1148
0
  int32_t sign, q;
1149
0
  sign = 0;
1150
0
  if (N < 0)
1151
0
  {
1152
0
    sign = 1;
1153
0
    N = -N;
1154
0
  }
1155
1156
0
  q = 0;
1157
0
  D = (D << 3);
1158
0
  if (N >= D)
1159
0
  {
1160
0
    N -= D;
1161
0
    q++;
1162
0
  }
1163
0
  q = (q << 1);
1164
1165
0
  D = (D >> 1);
1166
0
  if (N >= D)
1167
0
  {
1168
0
    N -= D;
1169
0
    q++;
1170
0
  }
1171
0
  q = (q << 1);
1172
1173
0
  if (N >= (D >> 1))
1174
0
    q++;
1175
1176
0
  if (sign)
1177
0
    return (-q);
1178
0
  return(q);
1179
0
}
1180
1181
void xSubPelErrorSrfc(uint64_t *sadBuffer, int32_t *deltaMv)
1182
0
{
1183
0
  for( int hv = 0; hv < 2; hv++)
1184
0
  {
1185
0
    const int32_t mvSubPelLvl = 4;/*1: half pel, 2: Qpel, 3:1/8, 4: 1/16*/
1186
0
    int64_t numerator   = (int64_t)((sadBuffer[hv+1] - sadBuffer[hv+3]) << mvSubPelLvl);
1187
0
    int64_t denominator = (int64_t)((sadBuffer[hv+1] + sadBuffer[hv+3] - (sadBuffer[0] << 1)));
1188
1189
0
    if (0 != denominator)
1190
0
    {
1191
0
      if ((sadBuffer[hv+1] != sadBuffer[0]) && (sadBuffer[hv+3] != sadBuffer[0]))
1192
0
      {
1193
0
        deltaMv[hv] = div_for_maxq7(numerator, denominator);
1194
0
      }
1195
0
      else
1196
0
      {
1197
0
        deltaMv[hv] = (sadBuffer[hv+1] == sadBuffer[0]) ? -8 : 8;
1198
0
      }
1199
0
    }
1200
0
  }
1201
0
}
1202
1203
void DMVR::xFinalPaddedMCForDMVR( const CodingUnit& cu, PelUnitBuf* dstBuf, const PelUnitBuf *refBuf, const bool bioApplied, const Mv mergeMv[NUM_REF_PIC_LIST_01], const Mv& refMv )
1204
0
{
1205
0
  int mvShift = MV_FRACTIONAL_BITS_INTERNAL;
1206
0
  Mv mv[2];
1207
0
  mv[L0] = mergeMv[L0] + refMv; mv[L0].clipToStorageBitDepth();
1208
0
  mv[L1] = mergeMv[L1] - refMv; mv[L1].clipToStorageBitDepth();
1209
1210
0
  for (int k = 0; k < NUM_REF_PIC_LIST_01; k++)
1211
0
  {
1212
0
    RefPicList refId = (RefPicList)k;
1213
0
    const Mv& cMv = mv[refId];
1214
0
    Mv cMvClipped( cMv );
1215
0
    clipMv(cMvClipped, cu.lumaPos(), cu.lumaSize(), *cu.cs->pcv);
1216
0
    const Picture* refPic = cu.slice->getRefPic(refId, cu.refIdx[refId]);
1217
0
    const Mv& startMv = mergeMv[refId];
1218
0
    for (int compID = 0; compID < getNumberValidComponents(cu.chromaFormat); compID++)
1219
0
    {
1220
0
      if( compID == COMP_Y ? cu.mccNoLuma() : cu.mccNoChroma() )
1221
0
      {
1222
0
        continue;
1223
0
      }
1224
1225
0
      int mvshiftTemp = mvShift + getComponentScaleX((ComponentID)compID, cu.chromaFormat);
1226
0
      int deltaIntMvX = (cMv.hor >> mvshiftTemp) - (startMv.hor >> mvshiftTemp);
1227
0
      int deltaIntMvY = (cMv.ver >> mvshiftTemp) - (startMv.ver >> mvshiftTemp);
1228
1229
0
      CHECK((abs(deltaIntMvX) > DMVR_NUM_ITERATION) || (abs(deltaIntMvY) > DMVR_NUM_ITERATION), "not expected DMVR movement");
1230
1231
0
      if (deltaIntMvX || deltaIntMvY)
1232
0
      {
1233
0
        const PelBuf& srcBuf = refBuf[refId].bufs[compID];
1234
0
        int offset = (deltaIntMvY)*srcBuf.stride + (deltaIntMvX);
1235
1236
0
        xPredInterBlk( ( ComponentID ) compID, cu, nullptr, cMvClipped, dstBuf[refId], true, cu.cs->slice->clpRngs[compID], bioApplied, false, refId, 0, 0, 0, srcBuf.buf + offset, srcBuf.stride );
1237
0
      }
1238
0
      else
1239
0
      {
1240
0
        xPredInterBlk( ( ComponentID ) compID, cu, refPic,  cMvClipped, dstBuf[refId], true, cu.cs->slice->clpRngs[compID], bioApplied, false, refId );
1241
0
      }
1242
0
    }
1243
0
  }
1244
0
}
1245
1246
static void xDMVRSubPixelErrorSurface( int16_t *totalDeltaMV, int16_t *deltaMV, uint64_t *pSADsArray )
1247
0
{
1248
0
  int sadStride = (((2 * DMVR_NUM_ITERATION) + 1));
1249
0
  uint64_t sadbuffer[5];
1250
0
  if( ( abs( totalDeltaMV[ 0 ] ) != ( 2 << MV_FRACTIONAL_BITS_INTERNAL ) )
1251
0
   && ( abs( totalDeltaMV[ 1 ] ) != ( 2 << MV_FRACTIONAL_BITS_INTERNAL ) ) )
1252
0
  {
1253
0
    int32_t tempDeltaMv[2] = { 0,0 };
1254
0
    sadbuffer[0] = pSADsArray[0];
1255
0
    sadbuffer[1] = pSADsArray[-1];
1256
0
    sadbuffer[2] = pSADsArray[-sadStride];
1257
0
    sadbuffer[3] = pSADsArray[1];
1258
0
    sadbuffer[4] = pSADsArray[sadStride];
1259
0
    xSubPelErrorSrfc(sadbuffer, tempDeltaMv);
1260
0
    totalDeltaMV[0] += tempDeltaMv[0];
1261
0
    totalDeltaMV[1] += tempDeltaMv[1];
1262
0
  }
1263
0
}
1264
1265
void DMVR::xProcessDMVR( const CodingUnit& cu, PelUnitBuf& pcYuvDst, const ClpRngs &clpRngs, const bool bioApplied )
1266
0
{
1267
0
  PROFILER_SCOPE_AND_STAGE_EXT( 1, _TPROF, P_INTER_MRG_DMVR, cu.cs, CH_L );
1268
  /*Always High Precision*/
1269
0
  const int csx      = getChannelTypeScaleX( CH_C, cu.chromaFormat );
1270
0
  const int csy      = getChannelTypeScaleY( CH_C, cu.chromaFormat );
1271
0
  const int mvShift  = MV_FRACTIONAL_BITS_INTERNAL;
1272
0
  const int mvShiftC = mvShift + csx;
1273
1274
  /*use merge MV as starting MV*/
1275
0
  const Mv mergeMv[] = { cu.mv[REF_PIC_LIST_0][0], cu.mv[REF_PIC_LIST_1][0] };
1276
1277
1278
0
  const int dy = std::min<int>(cu.lumaSize().height, DMVR_SUBCU_SIZE);
1279
0
  const int dx = std::min<int>(cu.lumaSize().width,  DMVR_SUBCU_SIZE);
1280
1281
0
  const Position& puPos = cu.lumaPos();
1282
1283
0
  bool bioAppliedType[MAX_NUM_SUBCU_DMVR] = { false, };
1284
1285
  // Do refinement search. A chroma-only call comes after a luma call for this CU, whose refinement the caller restored,
1286
  // and BDOF only touches luma.
1287
0
  if( !cu.mccNoLuma() )
1288
0
  {
1289
0
    const int bilinearBufStride = (cu.Y().width + (2 * DMVR_NUM_ITERATION));
1290
0
    const int padSize = DMVR_NUM_ITERATION << 1;
1291
0
    const int dstOffset = -( DMVR_NUM_ITERATION * bilinearBufStride + DMVR_NUM_ITERATION );
1292
1293
    /*use merge MV as starting MV*/
1294
0
    Mv mergeMVL0 = cu.mv[L0][0];
1295
0
    Mv mergeMVL1 = cu.mv[L1][0];
1296
1297
    /*Clip the starting MVs*/
1298
0
    clipMv(mergeMVL0, cu.lumaPos(), cu.lumaSize(), *cu.cs->pcv);
1299
0
    clipMv(mergeMVL1, cu.lumaPos(), cu.lumaSize(), *cu.cs->pcv);
1300
1301
    /*L0 MC for refinement*/
1302
0
    {
1303
0
      const Picture* refPic = cu.slice->getRefPic(L0, cu.refIdx[L0]);
1304
1305
0
      PelUnitBuf yuvTmp = PelUnitBuf(cu.chromaFormat, PelBuf(m_yuvTmp[L0].getBuf(COMP_Y).buf + dstOffset, bilinearBufStride, cu.lwidth() + padSize, cu.lheight() + padSize));
1306
1307
0
      mergeMVL0.hor -= (DMVR_NUM_ITERATION << MV_FRACTIONAL_BITS_INTERNAL);
1308
0
      mergeMVL0.ver -= (DMVR_NUM_ITERATION << MV_FRACTIONAL_BITS_INTERNAL);
1309
1310
0
      xPredInterBlk(COMP_Y, cu, refPic, mergeMVL0, yuvTmp, true, clpRngs[COMP_Y], false, false, L0, cu.lwidth() + padSize, cu.lheight() + padSize, true);
1311
0
    }
1312
1313
    /*L1 MC for refinement*/
1314
0
    {
1315
0
      const Picture* refPic = cu.slice->getRefPic(L1, cu.refIdx[L1]);
1316
1317
0
      PelUnitBuf yuvTmp = PelUnitBuf(cu.chromaFormat, PelBuf(m_yuvTmp[L1].getBuf(COMP_Y).buf + dstOffset, bilinearBufStride, cu.lwidth() + padSize, cu.lheight() + padSize));
1318
1319
0
      mergeMVL1.hor -= (DMVR_NUM_ITERATION << MV_FRACTIONAL_BITS_INTERNAL);
1320
0
      mergeMVL1.ver -= (DMVR_NUM_ITERATION << MV_FRACTIONAL_BITS_INTERNAL);
1321
1322
0
      xPredInterBlk(COMP_Y, cu, refPic, mergeMVL1, yuvTmp, true, clpRngs[COMP_Y], false, false, L1, cu.lwidth() + padSize, cu.lheight() + padSize, true);
1323
0
    }
1324
1325
    // point mc buffer to center point to avoid multiplication to reach each iteration to the beginning
1326
0
    const Pel* biLinearPredL0 = m_yuvTmp[0].getBuf( COMP_Y ).buf;
1327
0
    const Pel* biLinearPredL1 = m_yuvTmp[1].getBuf( COMP_Y ).buf;
1328
0
    const int bioEnabledThres = 2 * dy * dx;
1329
0
    const int bd = cu.cs->slice->clpRngs[COMP_Y].bd;
1330
1331
0
    DistParam distParam = m_pcRdCost->setDistParam( nullptr, nullptr, bilinearBufStride, bilinearBufStride, bd, COMP_Y, dx, dy, 1, true );
1332
1333
0
    int num = 0;
1334
0
    int yStart = 0;
1335
0
    uint64_t sadArray[((2 * DMVR_NUM_ITERATION) + 1) * ((2 * DMVR_NUM_ITERATION) + 1)];
1336
1337
0
    for( int y = puPos.y; y < ( puPos.y + cu.lumaSize().height ); y = y + dy, yStart = yStart + dy )
1338
0
    {
1339
0
      for( int x = puPos.x, xStart = 0; x < ( puPos.x + cu.lumaSize().width ); x = x + dx, xStart = xStart + dx )
1340
0
      {
1341
0
        uint64_t minCost        = MAX_UINT64;
1342
1343
        // set all entries to MAX_UNIT64
1344
0
        uint64_t *pSADsArray = &sadArray[( ( ( 2 * DMVR_NUM_ITERATION ) + 1 ) * ( ( 2 * DMVR_NUM_ITERATION ) + 1 ) ) >> 1];
1345
1346
0
        const Pel* addrL0Centre = biLinearPredL0 + yStart * bilinearBufStride + xStart;
1347
0
        const Pel* addrL1Centre = biLinearPredL1 + yStart * bilinearBufStride + xStart;
1348
1349
0
        const Pel* addrL0 = addrL0Centre;
1350
0
        const Pel* addrL1 = addrL1Centre;
1351
1352
0
        distParam.org.buf = addrL0;
1353
0
        distParam.cur.buf = addrL1;
1354
0
        minCost  = distParam.distFunc( distParam ) >> 1;
1355
0
        minCost -= ( minCost >> 2 );
1356
1357
0
        if( minCost < ( dx * dy ) )
1358
0
        {
1359
0
          cu.mvdL0SubPu[num] = Mv( 0, 0 );
1360
0
        }
1361
0
        else
1362
0
        {
1363
0
          int16_t totalDeltaMV[2] = { 0, 0 };
1364
0
          int16_t deltaMV[2]      = { 0, 0 };
1365
1366
0
          pSADsArray[0] = minCost;
1367
0
          pSADsArray    = sadArray;
1368
1369
0
          for( int ver = -2; ver <= 2; ver++ )
1370
0
          {
1371
0
            const int initHor = -2;
1372
0
            const ptrdiff_t offset = initHor + ver * bilinearBufStride;
1373
              
1374
0
            distParam.org.buf = addrL0 + offset;
1375
0
            distParam.cur.buf = addrL1 - offset;
1376
              
1377
0
            distParam.dmvrSadX5( distParam, pSADsArray, ver != 0 );
1378
1379
0
            for( int hor = -2; hor <= 2; hor++, pSADsArray++ )
1380
0
            {
1381
0
              Distortion cost = *pSADsArray;
1382
1383
0
              if( cost < minCost )
1384
0
              {
1385
0
                minCost    = cost;
1386
0
                deltaMV[0] = hor;
1387
0
                deltaMV[1] = ver;
1388
0
              }
1389
0
            }
1390
0
          }
1391
1392
0
          pSADsArray = &sadArray[( ( ( 2 * DMVR_NUM_ITERATION ) + 1 ) * ( ( 2 * DMVR_NUM_ITERATION ) + 1 ) ) >> 1];
1393
1394
0
          totalDeltaMV[0] += deltaMV[0];
1395
0
          totalDeltaMV[1] += deltaMV[1];
1396
0
          pSADsArray      += ( ( deltaMV[1] * ( ( ( 2 * DMVR_NUM_ITERATION ) + 1 ) ) ) + deltaMV[0] );
1397
0
          totalDeltaMV[0]  = totalDeltaMV[0] * ( 1 << mvShift );
1398
0
          totalDeltaMV[1]  = totalDeltaMV[1] * ( 1 << mvShift );
1399
1400
0
          xDMVRSubPixelErrorSurface( totalDeltaMV, deltaMV, pSADsArray );
1401
1402
0
          cu.mvdL0SubPu[num] = Mv( totalDeltaMV[0], totalDeltaMV[1] );
1403
0
        }
1404
1405
0
        bioAppliedType[num] = ( minCost < bioEnabledThres ) ? false : bioApplied;
1406
1407
0
        num++;
1408
0
      }
1409
0
    }
1410
0
  }
1411
1412
  // Final MC
1413
0
  CodingUnit subCu = cu;
1414
0
  subCu.UnitArea::operator=(UnitArea(cu.chromaFormat, Area(puPos.x, puPos.y, dx, dy)));
1415
0
  PelUnitBuf subPredBuf = pcYuvDst.subBuf(UnitAreaRelative(cu, subCu));
1416
1417
0
  PelUnitBuf predBuf[NUM_REF_PIC_LIST_01];
1418
0
  predBuf[L0] = m_yuvPred[L0].getCompactBuf( subCu );
1419
0
  predBuf[L1] = m_yuvPred[L1].getCompactBuf( subCu );
1420
  /* For padding */
1421
0
  PelUnitBuf padBuf[NUM_REF_PIC_LIST_01];
1422
0
  padBuf[L0] = m_yuvPad[L0].getBufPart(subCu);
1423
0
  padBuf[L1] = m_yuvPad[L1].getBufPart(subCu);
1424
1425
0
  int x = 0, y = 0;
1426
0
  int xStart = 0, yStart = 0;
1427
0
  int num = 0;
1428
0
  const int scaleX = getComponentScaleX(COMP_Cb, cu.chromaFormat);
1429
0
  const int scaleY = getComponentScaleY(COMP_Cb, cu.chromaFormat);
1430
1431
0
  const ptrdiff_t dstStride[MAX_NUM_COMP] = { pcYuvDst.bufs[COMP_Y].stride, cu.chromaFormat != CHROMA_400 ? pcYuvDst.bufs[COMP_Cb].stride : 0, cu.chromaFormat != CHROMA_400 ? pcYuvDst.bufs[COMP_Cr].stride : 0 };
1432
0
  for( y = puPos.y; y < ( puPos.y + cu.lumaSize().height ); y = y + dy, yStart = yStart + dy )
1433
0
  {
1434
0
    for( x = puPos.x, xStart = 0; x < ( puPos.x + cu.lumaSize().width ); x = x + dx, xStart = xStart + dx )
1435
0
    {
1436
0
      subCu.Y().x = x;
1437
0
      subCu.Y().y = y;
1438
1439
0
      if( cu.chromaFormat != CHROMA_400 )
1440
0
      {
1441
0
        subCu.Cb().x = subCu.Cr().x = x >> csx;
1442
0
        subCu.Cb().y = subCu.Cr().y = y >> csy;
1443
0
      }
1444
1445
0
      Mv mv0 = mergeMv[REF_PIC_LIST_0] + cu.mvdL0SubPu[num]; mv0.clipToStorageBitDepth();
1446
0
      Mv mv1 = mergeMv[REF_PIC_LIST_1] - cu.mvdL0SubPu[num]; mv1.clipToStorageBitDepth();
1447
1448
0
      bool padBufL0  = (mv0.hor >> mvShift)  != (mergeMv[0].hor >> mvShift)  || (mv0.ver >> mvShift)  != (mergeMv[0].ver >> mvShift);
1449
0
      bool padBufL0C = (mv0.hor >> mvShiftC) != (mergeMv[0].hor >> mvShiftC) || (mv0.ver >> mvShiftC) != (mergeMv[0].ver >> mvShiftC);
1450
        
1451
0
      bool padBufL1  = (mv1.hor >> mvShift)  != (mergeMv[1].hor >> mvShift)  || (mv1.ver >> mvShift)  != (mergeMv[1].ver >> mvShift);
1452
0
      bool padBufL1C = (mv1.hor >> mvShiftC) != (mergeMv[1].hor >> mvShiftC) || (mv1.ver >> mvShiftC) != (mergeMv[1].ver >> mvShiftC);
1453
1454
0
      padBufL0  &= !cu.mccNoLuma();
1455
0
      padBufL1  &= !cu.mccNoLuma();
1456
0
      padBufL0C &= cu.chromaFormat != CHROMA_400 && !cu.mccNoChroma();
1457
0
      padBufL1C &= cu.chromaFormat != CHROMA_400 && !cu.mccNoChroma();
1458
1459
0
      if (padBufL0)  xCopyAndPad(subCu, padBuf[L0], L0, true);
1460
0
      if (padBufL0C) xCopyAndPad(subCu, padBuf[L0], L0, false);
1461
0
      if (padBufL1)  xCopyAndPad(subCu, padBuf[L1], L1, true);
1462
0
      if (padBufL1C) xCopyAndPad(subCu, padBuf[L1], L1, false);
1463
1464
0
      xFinalPaddedMCForDMVR( subCu, predBuf, padBuf, bioAppliedType[num], mergeMv, cu.mvdL0SubPu[num] );
1465
1466
0
      subPredBuf.bufs[COMP_Y].buf  = pcYuvDst.bufs[COMP_Y].buf + xStart + yStart * dstStride[COMP_Y];
1467
0
      if( cu.chromaFormat != CHROMA_400 )
1468
0
      {
1469
0
        subPredBuf.bufs[COMP_Cb].buf = pcYuvDst.bufs[COMP_Cb].buf + (xStart >> scaleX) + ((yStart >> scaleY) * dstStride[COMP_Cb]);
1470
0
        subPredBuf.bufs[COMP_Cr].buf = pcYuvDst.bufs[COMP_Cr].buf + (xStart >> scaleX) + ((yStart >> scaleY) * dstStride[COMP_Cr]);
1471
0
      }
1472
1473
0
      xWeightedAverage( subCu, predBuf[L0], predBuf[L1], subPredBuf, bioAppliedType[num] );
1474
1475
0
      num++;
1476
0
    }
1477
0
  }
1478
0
}
1479
1480
bool InterPredInterpolation::isSubblockVectorSpreadOverLimit(int a, int b, int c, int d, int predType)
1481
0
{
1482
0
  int s4 = (4 << 11);
1483
0
  int filterTap = 6;
1484
1485
0
  if (predType == 3)
1486
0
  {
1487
0
    int refBlkWidth = std::max(std::max(0, 4 * a + s4), std::max(4 * c, 4 * a + 4 * c + s4)) - std::min(std::min(0, 4 * a + s4), std::min(4 * c, 4 * a + 4 * c + s4));
1488
0
    int refBlkHeight = std::max(std::max(0, 4 * b), std::max(4 * d + s4, 4 * b + 4 * d + s4)) - std::min(std::min(0, 4 * b), std::min(4 * d + s4, 4 * b + 4 * d + s4));
1489
0
    refBlkWidth = (refBlkWidth >> 11) + filterTap + 3;
1490
0
    refBlkHeight = (refBlkHeight >> 11) + filterTap + 3;
1491
1492
0
    if (refBlkWidth * refBlkHeight > (filterTap + 9) * (filterTap + 9))
1493
0
    {
1494
0
      return true;
1495
0
    }
1496
0
  }
1497
0
  else
1498
0
  {
1499
0
    int refBlkWidth = std::max(0, 4 * a + s4) - std::min(0, 4 * a + s4);
1500
0
    int refBlkHeight = std::max(0, 4 * b) - std::min(0, 4 * b);
1501
0
    refBlkWidth = (refBlkWidth >> 11) + filterTap + 3;
1502
0
    refBlkHeight = (refBlkHeight >> 11) + filterTap + 3;
1503
0
    if (refBlkWidth * refBlkHeight > (filterTap + 9) * (filterTap + 5))
1504
0
    {
1505
0
      return true;
1506
0
    }
1507
1508
0
    refBlkWidth = std::max(0, 4 * c) - std::min(0, 4 * c);
1509
0
    refBlkHeight = std::max(0, 4 * d + s4) - std::min(0, 4 * d + s4);
1510
0
    refBlkWidth = (refBlkWidth >> 11) + filterTap + 3;
1511
0
    refBlkHeight = (refBlkHeight >> 11) + filterTap + 3;
1512
0
    if (refBlkWidth * refBlkHeight > (filterTap + 5) * (filterTap + 9))
1513
0
    {
1514
0
      return true;
1515
0
    }
1516
0
  }
1517
0
  return false;
1518
0
}
1519
1520
void InterPredInterpolation::xPredAffineBlk(const ComponentID compID, const CodingUnit& cu, const Picture* refPic, const Mv* _mv, PelUnitBuf& dstPic, const bool bi, const ClpRng& clpRng, const RefPicList refPicList)
1521
0
{
1522
0
  const ChromaFormat chFmt = cu.chromaFormat;
1523
0
  int iScaleX = getComponentScaleX(compID, chFmt);
1524
0
  int iScaleY = getComponentScaleY(compID, chFmt);
1525
1526
0
  Mv mvLT = _mv[0];
1527
0
  Mv mvRT = _mv[1];
1528
0
  Mv mvLB = _mv[2];
1529
1530
  // get affine sub-block width and height
1531
0
  const int width = cu.Y().width;
1532
0
  const int height = cu.Y().height;
1533
0
  int blockWidth = AFFINE_MIN_BLOCK_SIZE;
1534
0
  int blockHeight = AFFINE_MIN_BLOCK_SIZE;
1535
1536
0
  CHECK(blockWidth  > (width >> iScaleX), "Sub Block width  > Block width");
1537
0
  CHECK(blockHeight > (height >> iScaleY), "Sub Block height > Block height");
1538
0
  const int MVBUFFER_SIZE = MAX_CU_SIZE / MIN_PU_SIZE;
1539
1540
0
  const int cxWidth = width >> iScaleX;
1541
0
  const int cxHeight = height >> iScaleY;
1542
0
  const int iHalfBW = blockWidth >> 1;
1543
0
  const int iHalfBH = blockHeight >> 1;
1544
1545
0
  const int iBit = MAX_CU_DEPTH;
1546
0
  int iDMvHorX = 0;
1547
0
  int iDMvHorY = 0;
1548
0
  int iDMvVerX = 0;
1549
0
  int iDMvVerY = 0;
1550
1551
0
  iDMvHorX = (mvRT - mvLT).hor * (1 <<(iBit - Log2(cxWidth)));
1552
0
  iDMvHorY = (mvRT - mvLT).ver * (1 <<(iBit - Log2(cxWidth)));
1553
0
  if (cu.affineType == AFFINEMODEL_6PARAM)
1554
0
  {
1555
0
    iDMvVerX = (mvLB - mvLT).hor * (1 <<(iBit - Log2(cxHeight)));
1556
0
    iDMvVerY = (mvLB - mvLT).ver * (1 <<(iBit - Log2(cxHeight)));
1557
0
  }
1558
0
  else
1559
0
  {
1560
0
    iDMvVerX = -iDMvHorY;
1561
0
    iDMvVerY = iDMvHorX;
1562
0
  }
1563
1564
0
  int iMvScaleHor = mvLT.hor * (1 << iBit);
1565
0
  int iMvScaleVer = mvLT.ver * (1 << iBit);
1566
0
  const PPS &pps = *cu.cs->pps;
1567
0
  const SPS &sps = *cu.cs->sps;
1568
0
  const int iMvShift = 4;
1569
0
  const int iOffset = 8;
1570
0
  const int iHorMax = (pps.picWidthInLumaSamples + iOffset - cu.Y().x - 1) << iMvShift;
1571
0
  const int iHorMin = (-(int)cu.cs->pcv->maxCUSize - iOffset - (int)cu.Y().x + 1) * (1 << iMvShift);
1572
0
  const int iVerMax = (pps.picHeightInLumaSamples + iOffset - cu.Y().y - 1) << iMvShift;
1573
0
  const int iVerMin = (-(int)cu.cs->pcv->maxCUSize - iOffset - (int)cu.Y().y + 1) * (1 << iMvShift);
1574
  
1575
0
  const int shift = iBit - 4 + MV_FRACTIONAL_BITS_INTERNAL;
1576
0
  const bool subblkMVSpreadOverLimit = isSubblockVectorSpreadOverLimit(iDMvHorX, iDMvHorY, iDMvVerX, iDMvVerY, cu.interDir);
1577
0
  const bool cpmvEqual = (cu.affineType == AFFINEMODEL_6PARAM && _mv[0] == _mv[1] && _mv[0] == _mv[2]) || (cu.affineType == AFFINEMODEL_4PARAM && _mv[0] == _mv[1]);
1578
1579
0
  bool enablePROF = sps.PROF && (!m_skipPROF) && (compID == COMP_Y);
1580
0
  enablePROF &= (!cu.cs->picHeader->disProfFlag);
1581
0
  enablePROF &= !cpmvEqual;
1582
0
  enablePROF &= !subblkMVSpreadOverLimit;
1583
0
  const int profThres = 1 << (iBit + (m_isBi ? 1 : 0));
1584
0
  enablePROF &= !m_encOnly || cu.slice->checkLDC || iDMvHorX > profThres || iDMvHorY > profThres || iDMvVerX > profThres || iDMvVerY > profThres || iDMvHorX < -profThres || iDMvHorY < -profThres || iDMvVerX < -profThres || iDMvVerY < -profThres;
1585
0
  enablePROF &= pps.picWidthInLumaSamples == refPic->cs->pps->picWidthInLumaSamples && pps.picHeightInLumaSamples == refPic->cs->pps->picHeightInLumaSamples;
1586
1587
0
  bool isLast = enablePROF ? false : !bi;
1588
1589
0
  const int cuExtW = AFFINE_MIN_BLOCK_SIZE + PROF_BORDER_EXT_W * 2;
1590
0
  const int cuExtH = AFFINE_MIN_BLOCK_SIZE + PROF_BORDER_EXT_H * 2;
1591
1592
0
  PelBuf gradXExt(m_gradBuf[0], cuExtW, cuExtH);
1593
0
  PelBuf gradYExt(m_gradBuf[1], cuExtW, cuExtH);
1594
1595
0
  int dstExtW = (((blockWidth + PROF_BORDER_EXT_W * 2 + 7) >> 3) << 3);
1596
0
  int dstExtH = (blockHeight + PROF_BORDER_EXT_H * 2);
1597
1598
0
  PelBuf dstExtBuf(m_filteredBlockTmp[1][compID], dstExtW, dstExtH);
1599
1600
0
  PelBuf& dstBuf = dstPic.bufs[compID];
1601
1602
0
  int *dMvScaleHor = m_dMvBuf[refPicList];
1603
0
  int *dMvScaleVer = m_dMvBuf[refPicList] + 16;
1604
0
  alignas(16) int16_t dMvScaleXY[2 * AFFINE_MIN_BLOCK_SIZE * AFFINE_MIN_BLOCK_SIZE];
1605
1606
0
  if (enablePROF)
1607
0
  {
1608
0
    int* dMvH = dMvScaleHor;
1609
0
    int* dMvV = dMvScaleVer;
1610
0
    int quadHorX = 4 * iDMvHorX ;
1611
0
    int quadHorY = 4 * iDMvHorY ;
1612
0
    int quadVerX = 4 * iDMvVerX ;
1613
0
    int quadVerY = 4 * iDMvVerY ;
1614
1615
0
    dMvH[0] = ((iDMvHorX + iDMvVerX) * 2) - ((quadHorX + quadVerX)  * 2);
1616
0
    dMvV[0] = ((iDMvHorY + iDMvVerY) * 2) - ((quadHorY + quadVerY)  * 2);
1617
1618
0
    for (int w = 1; w < blockWidth; w++)
1619
0
    {
1620
0
      dMvH[w] = dMvH[w - 1] + quadHorX;
1621
0
      dMvV[w] = dMvV[w - 1] + quadHorY;
1622
0
    }
1623
1624
0
    dMvH += blockWidth;
1625
0
    dMvV += blockWidth;
1626
0
    for (int h = 1; h < blockHeight; h++)
1627
0
    {
1628
0
      for (int w = 0; w < blockWidth; w++)
1629
0
      {
1630
0
        dMvH[w] = dMvH[w - blockWidth] + quadVerX;
1631
0
        dMvV[w] = dMvV[w - blockWidth] + quadVerY;
1632
0
      }
1633
0
      dMvH += blockWidth;
1634
0
      dMvV += blockWidth;
1635
0
    }
1636
1637
0
    const int mvShift  = 8;
1638
0
    const int dmvLimit = ( 1 << 5 ) - 1;
1639
1640
0
    if (!g_pelBufOP.roundIntVector)
1641
0
    {
1642
0
      for (int idx = 0; idx < blockWidth * blockHeight; idx++)
1643
0
      {
1644
0
        roundAffineMv(dMvScaleHor[idx], dMvScaleVer[idx], mvShift);
1645
0
        dMvScaleHor[idx] = Clip3(-dmvLimit, dmvLimit, dMvScaleHor[idx]);
1646
0
        dMvScaleVer[idx] = Clip3(-dmvLimit, dmvLimit, dMvScaleVer[idx]);
1647
0
      }
1648
0
    }
1649
0
    else
1650
0
    {
1651
0
      int sz = blockWidth * blockHeight;
1652
0
      g_pelBufOP.roundIntVector(dMvScaleHor, sz, mvShift, dmvLimit);
1653
0
      g_pelBufOP.roundIntVector(dMvScaleVer, sz, mvShift, dmvLimit);
1654
0
    }
1655
1656
    // The dMv offsets are the same for every sub-block of the CU, so the per-CU luma function gets them interleaved as
1657
    // int16 once here. They are clipped to +-dmvLimit above, so the narrowing is exact, as applyPROF_SSE's saturating
1658
    // packs.
1659
0
    if (xFpAffineLumaCU[1][bi])
1660
0
    {
1661
0
      for (int idx = 0; idx < blockWidth * blockHeight; idx++)
1662
0
      {
1663
0
        dMvScaleXY[2 * idx]     = (int16_t) dMvScaleHor[idx];
1664
0
        dMvScaleXY[2 * idx + 1] = (int16_t) dMvScaleVer[idx];
1665
0
      }
1666
0
    }
1667
0
  }
1668
1669
0
  int scaleXLuma = getComponentScaleX(COMP_Y, chFmt);
1670
0
  int scaleYLuma = getComponentScaleY(COMP_Y, chFmt);
1671
0
  if( cu.mccNoLuma() && ( compID == COMP_Cb ) && cu.chromaFormat != CHROMA_444 )
1672
0
  {
1673
0
    CHECK(compID == COMP_Y, "Chroma only subblock MV calculation should not apply to Luma");
1674
0
    int lumaBlockWidth = AFFINE_MIN_BLOCK_SIZE;
1675
0
    int lumaBlockHeight = AFFINE_MIN_BLOCK_SIZE;
1676
1677
0
    CHECK(lumaBlockWidth > (width >> scaleXLuma), "Sub Block width  > Block width");
1678
0
    CHECK(lumaBlockHeight > (height >> scaleYLuma), "Sub Block height > Block height");
1679
1680
0
    const int cxWidthLuma = width >> scaleXLuma;
1681
0
    const int cxHeightLuma = height >> scaleYLuma;
1682
0
    const int halfBWLuma = lumaBlockWidth >> 1;
1683
0
    const int halfBHLuma = lumaBlockHeight >> 1;
1684
1685
0
    int dMvHorXLuma, dMvHorYLuma, dMvVerXLuma, dMvVerYLuma;
1686
0
    dMvHorXLuma = (mvRT - mvLT).hor * (1 << (iBit - floorLog2(cxWidthLuma)));
1687
0
    dMvHorYLuma = (mvRT - mvLT).ver * (1 <<  (iBit - floorLog2(cxWidthLuma)));
1688
0
    if (cu.affineType == AFFINEMODEL_6PARAM)
1689
0
    {
1690
0
      dMvVerXLuma = (mvLB - mvLT).hor * (1 << (iBit - floorLog2(cxHeightLuma)));
1691
0
      dMvVerYLuma = (mvLB - mvLT).ver * (1 << (iBit - floorLog2(cxHeightLuma)));
1692
0
    }
1693
0
    else
1694
0
    {
1695
0
      dMvVerXLuma = -dMvHorYLuma;
1696
0
      dMvVerYLuma = dMvHorXLuma;
1697
0
    }
1698
1699
0
    const bool subblkMVSpreadOverLimitLuma = isSubblockVectorSpreadOverLimit(dMvHorXLuma, dMvHorYLuma, dMvVerXLuma, dMvVerYLuma, cu.interDir);
1700
1701
    // get luma MV block by block
1702
0
    for (int h = 0; h < cxHeightLuma; h += lumaBlockHeight)
1703
0
    {
1704
0
      for (int w = 0; w < cxWidthLuma; w += lumaBlockWidth)
1705
0
      {
1706
0
        int mvScaleTmpHor, mvScaleTmpVer;
1707
0
        if (!subblkMVSpreadOverLimitLuma)
1708
0
        {
1709
0
          mvScaleTmpHor = iMvScaleHor + dMvHorXLuma * (halfBWLuma + w) + dMvVerXLuma * (halfBHLuma + h);
1710
0
          mvScaleTmpVer = iMvScaleVer + dMvHorYLuma * (halfBWLuma + w) + dMvVerYLuma * (halfBHLuma + h);
1711
0
        }
1712
0
        else
1713
0
        {
1714
0
          mvScaleTmpHor = iMvScaleHor + dMvHorXLuma * (cxWidthLuma >> 1) + dMvVerXLuma * (cxHeightLuma >> 1);
1715
0
          mvScaleTmpVer = iMvScaleVer + dMvHorYLuma * (cxWidthLuma >> 1) + dMvVerYLuma * (cxHeightLuma >> 1);
1716
0
        }
1717
1718
0
        roundAffineMv(mvScaleTmpHor, mvScaleTmpVer, shift);
1719
0
        Mv tmpMv(mvScaleTmpHor, mvScaleTmpVer);
1720
0
        tmpMv.clipToStorageBitDepth();
1721
0
        mvScaleTmpHor = tmpMv.hor;
1722
0
        mvScaleTmpVer = tmpMv.ver;
1723
1724
0
        m_storedMv[h / AFFINE_MIN_BLOCK_SIZE * MVBUFFER_SIZE + w / AFFINE_MIN_BLOCK_SIZE].set(mvScaleTmpHor, mvScaleTmpVer);
1725
0
      }
1726
0
    }
1727
0
  }
1728
  // get prediction block by block
1729
0
  const CPelBuf refBuf     = refPic->getRecoBuf(compID);
1730
1731
0
  const int puX = cu.blocks[compID].x;
1732
0
  const int puY = cu.blocks[compID].y;
1733
1734
  // Equal control-point MVs (no affine motion) or a spread over the limit (every subblock takes the MV of the block's
1735
  // centre) give all subblocks one MV: then the luma block is predicted in one filter call (PROF is off for both).
1736
0
  if( compID == COMP_Y && ( cpmvEqual || subblkMVSpreadOverLimit ) )
1737
0
  {
1738
    // the subblocks' MV as the loop below computes it
1739
0
    const int mvPosX = subblkMVSpreadOverLimit ? cxWidth >> 1  : iHalfBW;
1740
0
    const int mvPosY = subblkMVSpreadOverLimit ? cxHeight >> 1 : iHalfBH;
1741
0
    int mvHor = iMvScaleHor + iDMvHorX * mvPosX + iDMvVerX * mvPosY;
1742
0
    int mvVer = iMvScaleVer + iDMvHorY * mvPosX + iDMvVerY * mvPosY;
1743
0
    roundAffineMv( mvHor, mvVer, shift );
1744
0
    Mv mv( mvHor, mvVer );
1745
0
    mv.clipToStorageBitDepth();
1746
0
    for( int h = 0; h < cxHeight; h += blockHeight )
1747
0
    {
1748
0
      for( int w = 0; w < cxWidth; w += blockWidth )
1749
0
      {
1750
0
        m_storedMv[h / AFFINE_MIN_BLOCK_SIZE * MVBUFFER_SIZE + w / AFFINE_MIN_BLOCK_SIZE] = mv;   // chroma reads them
1751
0
      }
1752
0
    }
1753
0
    mvHor = std::min<int>( iHorMax, std::max<int>( iHorMin, mv.hor ) );
1754
0
    mvVer = std::min<int>( iVerMax, std::max<int>( iVerMin, mv.ver ) );
1755
0
    CHECKD( m_ifpLines && !CU::isMvInRangeFPP( puY, cxHeight, mvVer, m_ifpLines, *pps.pcv, iScaleY ), "xPredAffineBlk: FPP MV restriction failed!\n" );
1756
1757
    // luma MVs in 1/16 sample
1758
0
    const Pel* ref = refBuf.buf + puX + ( mvHor >> 4 ) + ( puY + ( mvVer >> 4 ) ) * refBuf.stride;
1759
0
    m_if.filterAffineLumaBlock( ref, refBuf.stride, dstBuf.buf, dstBuf.stride, m_filteredBlockTmp[0][COMP_Y], cxWidth,
1760
0
                                cxHeight, mvHor & 15, mvVer & 15, isLast, chFmt, clpRng );
1761
0
    return;
1762
0
  }
1763
1764
0
  if (compID == COMP_Y && xFpAffineLumaCU[enablePROF][bi])
1765
0
  {
1766
    // the whole luma loop below in one SIMD call, which saves two calls and their argument set-up per sub-block (the
1767
    // uniform-MV CUs above, also every CU whose MV spread is over the limit, never get here)
1768
0
    AffineLumaCU luma;
1769
0
    luma.ref             = refBuf.bufAt(puX, puY);
1770
0
    luma.refStride       = refBuf.stride;
1771
0
    luma.dst             = dstBuf.buf;
1772
0
    luma.dstStride       = dstBuf.stride;
1773
0
    luma.width           = cxWidth;
1774
0
    luma.height          = cxHeight;
1775
0
    luma.mvScaleHor      = iMvScaleHor;
1776
0
    luma.mvScaleVer      = iMvScaleVer;
1777
0
    luma.dMvHorX         = iDMvHorX;
1778
0
    luma.dMvHorY         = iDMvHorY;
1779
0
    luma.dMvVerX         = iDMvVerX;
1780
0
    luma.dMvVerY         = iDMvVerY;
1781
0
    luma.roundShift      = shift;
1782
0
    luma.horMin          = iHorMin;
1783
0
    luma.horMax          = iHorMax;
1784
0
    luma.verMin          = iVerMin;
1785
0
    luma.verMax          = iVerMax;
1786
0
    luma.storedMv        = m_storedMv;
1787
0
    luma.storedMvStride  = MVBUFFER_SIZE;
1788
0
    luma.dMvXY           = dMvScaleXY;
1789
0
    luma.shiftNum        = std::max<int>(2, (IF_INTERNAL_PREC - clpRng.bd));
1790
0
    luma.offset          = (1 << (luma.shiftNum - 1)) + IF_INTERNAL_OFFS;
1791
0
    luma.clpRng          = clpRng;
1792
0
    xFpAffineLumaCU[enablePROF][bi](luma);
1793
0
#if defined( _DEBUG )
1794
    // the FPP MV restriction check of the sub-block loop below, on the MVs the CU call stored
1795
0
    for (int h = 0; h < cxHeight; h += blockHeight)
1796
0
    {
1797
0
      for (int w = 0; w < cxWidth; w += blockWidth)
1798
0
      {
1799
0
        const int mvVer = std::min<int>(iVerMax, std::max<int>(iVerMin, m_storedMv[h / AFFINE_MIN_BLOCK_SIZE * MVBUFFER_SIZE + w / AFFINE_MIN_BLOCK_SIZE].ver));
1800
0
        CHECKD( m_ifpLines && !CU::isMvInRangeFPP( puY + h, blockHeight, mvVer, m_ifpLines, *pps.pcv, iScaleY ), "xPredAffineBlk: FPP MV restriction failed!\n" );
1801
0
      }
1802
0
    }
1803
0
#endif
1804
0
    return;
1805
0
  }
1806
1807
0
  for (int h = 0; h < cxHeight; h += blockHeight)
1808
0
  {
1809
0
    for (int w = 0; w < cxWidth; w += blockWidth)
1810
0
    {
1811
0
      int iMvScaleTmpHor, iMvScaleTmpVer;
1812
0
      if (compID == COMP_Y || cu.chromaFormat == CHROMA_444)
1813
0
      {
1814
0
        if (!subblkMVSpreadOverLimit)
1815
0
        {
1816
0
          iMvScaleTmpHor = iMvScaleHor + iDMvHorX * (iHalfBW + w) + iDMvVerX * (iHalfBH + h);
1817
0
          iMvScaleTmpVer = iMvScaleVer + iDMvHorY * (iHalfBW + w) + iDMvVerY * (iHalfBH + h);
1818
0
        }
1819
0
        else
1820
0
        {
1821
0
          iMvScaleTmpHor = iMvScaleHor + iDMvHorX * (cxWidth >> 1) + iDMvVerX * (cxHeight >> 1);
1822
0
          iMvScaleTmpVer = iMvScaleVer + iDMvHorY * (cxWidth >> 1) + iDMvVerY * (cxHeight >> 1);
1823
0
        }
1824
1825
0
        roundAffineMv(iMvScaleTmpHor, iMvScaleTmpVer, shift);
1826
0
        Mv tmpMv(iMvScaleTmpHor, iMvScaleTmpVer);
1827
0
        tmpMv.clipToStorageBitDepth();
1828
0
        iMvScaleTmpHor = tmpMv.hor;
1829
0
        iMvScaleTmpVer = tmpMv.ver;
1830
1831
        // clip and scale
1832
0
        m_storedMv[h / AFFINE_MIN_BLOCK_SIZE * MVBUFFER_SIZE + w / AFFINE_MIN_BLOCK_SIZE].set(iMvScaleTmpHor, iMvScaleTmpVer);
1833
        //   if( scalingRatio == SCALE_1X ) 
1834
0
        {
1835
0
          iMvScaleTmpHor = std::min<int>(iHorMax, std::max<int>(iHorMin, iMvScaleTmpHor));
1836
0
          iMvScaleTmpVer = std::min<int>(iVerMax, std::max<int>(iVerMin, iMvScaleTmpVer));
1837
0
        }
1838
0
      }
1839
0
      else
1840
0
      {
1841
0
        Mv curMv = m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE) * MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE)] +
1842
0
          m_storedMv[((h << iScaleY) / AFFINE_MIN_BLOCK_SIZE + iScaleY)* MVBUFFER_SIZE + ((w << iScaleX) / AFFINE_MIN_BLOCK_SIZE + iScaleX)];
1843
0
        roundAffineMv(curMv.hor, curMv.ver, 1);
1844
1845
0
        curMv.hor = std::min<int>(iHorMax, std::max<int>(iHorMin, curMv.hor));
1846
0
        curMv.ver = std::min<int>(iVerMax, std::max<int>(iVerMin, curMv.ver));
1847
1848
0
        iMvScaleTmpHor = curMv.hor;
1849
0
        iMvScaleTmpVer = curMv.ver;
1850
0
      }
1851
1852
0
      CHECKD( m_ifpLines && !CU::isMvInRangeFPP( puY + h, blockHeight, iMvScaleTmpVer, m_ifpLines, *pps.pcv, iScaleY ), "xPredAffineBlk: FPP MV restriction failed!\n" );
1853
      // get the MV in high precision
1854
0
      int xFrac, yFrac, xInt, yInt;
1855
1856
0
      if (!iScaleX)
1857
0
      {
1858
0
        xInt = iMvScaleTmpHor >> 4;
1859
0
        xFrac = iMvScaleTmpHor & 15;
1860
0
      }
1861
0
      else
1862
0
      {
1863
0
        xInt = iMvScaleTmpHor >> 5;
1864
0
        xFrac = iMvScaleTmpHor & 31;
1865
0
      }
1866
0
      if (!iScaleY)
1867
0
      {
1868
0
        yInt = iMvScaleTmpVer >> 4;
1869
0
        yFrac = iMvScaleTmpVer & 15;
1870
0
      }
1871
0
      else
1872
0
      {
1873
0
        yInt = iMvScaleTmpVer >> 5;
1874
0
        yFrac = iMvScaleTmpVer & 31;
1875
0
      }
1876
1877
0
      const Pel* ref = refBuf.buf;
1878
0
      ref           +=   puX + xInt + w;
1879
0
      ref           += ( puY + yInt + h ) * refBuf.stride;
1880
0
      Pel* dst       = dstBuf.buf + w + h * dstBuf.stride;
1881
1882
0
      int refStride = refBuf.stride;
1883
0
      int dstStride = dstBuf.stride;
1884
1885
0
      int bw = blockWidth;
1886
0
      int bh = blockHeight;
1887
1888
0
      if( enablePROF )
1889
0
      {
1890
0
        dst = dstExtBuf.bufAt(PROF_BORDER_EXT_W, PROF_BORDER_EXT_H);
1891
0
        dstStride = dstExtBuf.stride;
1892
0
      }
1893
1894
0
      if( xFrac && yFrac )
1895
0
      {
1896
0
        m_if.filter4x4( compID, ref, refStride, dst, dstStride, 4, 4, xFrac, yFrac, isLast, chFmt, clpRng );
1897
0
      }
1898
0
      else if( !yFrac )
1899
0
      {
1900
0
        m_if.filterHor( compID, ref, refStride, dst, dstStride, bw, bh, xFrac, isLast, chFmt, clpRng );
1901
0
      }
1902
0
      else if( xFrac == 0 )
1903
0
      {
1904
0
        m_if.filterVer( compID, ref, refStride, dst, dstStride, bw, bh, yFrac, true, isLast, chFmt, clpRng );
1905
0
      }
1906
      
1907
0
      if (enablePROF)
1908
0
      {
1909
0
        const unsigned shift = std::max<int>(2, (IF_INTERNAL_PREC - clpRng.bd));
1910
0
        const int xOffset = xFrac >> 3;
1911
0
        const int yOffset = yFrac >> 3;
1912
1913
0
        const int refOffset = (blockHeight + 1) * refStride;
1914
0
        const int dstOffset = (blockHeight + 1)* dstStride;
1915
1916
0
        const Pel* refPel = ref - (1 - yOffset) * refStride + xOffset - 1;
1917
0
        Pel* dstPel = dst - dstStride - 1;
1918
0
        for (int pw = 0; pw < blockWidth + 2; pw++)
1919
0
        {
1920
0
          dstPel[pw] = leftShiftU(refPel[pw], shift) - (Pel)IF_INTERNAL_OFFS;
1921
0
          dstPel[pw + dstOffset] = leftShiftU(refPel[pw + refOffset], shift) - (Pel)IF_INTERNAL_OFFS;
1922
0
        }
1923
1924
0
        refPel = ref + yOffset * refBuf.stride + xOffset;
1925
0
        dstPel = dst;
1926
0
        for (int ph = 0; ph < blockHeight; ph++, refPel += refStride, dstPel += dstStride)
1927
0
        {
1928
0
          dstPel[-1] = leftShiftU(refPel[-1], shift) - (Pel)IF_INTERNAL_OFFS;
1929
0
          dstPel[blockWidth] = leftShiftU(refPel[blockWidth], shift) - (Pel)IF_INTERNAL_OFFS;
1930
0
        }
1931
1932
0
        PelBuf gradXBuf = gradXExt.subBuf(0, 0, blockWidth + 2, blockHeight + 2);
1933
0
        PelBuf gradYBuf = gradYExt.subBuf(0, 0, blockWidth + 2, blockHeight + 2);
1934
1935
0
        xFpProfGradFilter(dstExtBuf.buf, dstExtBuf.stride, blockWidth + 2, blockHeight + 2, gradXBuf.stride, gradXBuf.buf, gradYBuf.buf, clpRng.bd);
1936
1937
0
        const int shiftNum = std::max<int>(2, (IF_INTERNAL_PREC - clpRng.bd));
1938
0
        const Pel offset = (1 << (shiftNum - 1)) + IF_INTERNAL_OFFS;
1939
0
        Pel* src = dstExtBuf.bufAt(PROF_BORDER_EXT_W, PROF_BORDER_EXT_H);
1940
0
        Pel* gX = gradXBuf.bufAt(PROF_BORDER_EXT_W, PROF_BORDER_EXT_H);
1941
0
        Pel* gY = gradYBuf.bufAt(PROF_BORDER_EXT_W, PROF_BORDER_EXT_H);
1942
1943
0
        Pel*  dstY = dstBuf.bufAt(w, h);
1944
1945
0
        xFpApplyPROF(dstY, dstBuf.stride, src, dstExtBuf.stride, blockWidth, blockHeight, gX, gY, gradXBuf.stride, dMvScaleHor, dMvScaleVer, blockWidth, bi, shiftNum, offset, clpRng);
1946
0
      }
1947
0
    }
1948
0
  }
1949
0
}
1950
1951
bool InterPredInterpolation::xIsAffineMvInRangeFPP( const CodingUnit &cu, const Mv* _mv, const int ifpLines, const int mvPrecShift )
1952
0
{
1953
0
  const PreCalcValues& pcv = *cu.cs->pcv;
1954
0
  if( cu.ly() >= ( ( pcv.heightInCtus - 1 - ifpLines ) << pcv.maxCUSizeLog2 ) )
1955
0
    return true;
1956
1957
0
  const ChromaFormat chFmt = cu.chromaFormat;
1958
0
  const int width       = cu.Y().width;
1959
0
  const int height      = cu.Y().height;
1960
0
  const int nBW  = AFFINE_MIN_BLOCK_SIZE;
1961
0
  const int nBH = AFFINE_MIN_BLOCK_SIZE;
1962
0
  const int iHalfBW     = nBW >> 1;
1963
0
  const int iHalfBH     = nBH >> 1;
1964
0
  const int iBit        = MAX_CU_DEPTH;
1965
0
  const int shift       = iBit - 4 + MV_FRACTIONAL_BITS_INTERNAL;
1966
0
  Mv mvLT = _mv[0];
1967
0
  Mv mvRT = _mv[1];
1968
0
  Mv mvLB = _mv[2];
1969
1970
0
  int iDMvHorX = 0;
1971
0
  int iDMvHorY = 0;
1972
0
  int iDMvVerX = 0;
1973
0
  int iDMvVerY = 0;
1974
1975
0
  const int iMvScaleVer = mvLT.ver * (1 << iBit);
1976
1977
0
  int iScaleX = getChannelTypeScaleX(CH_C, chFmt);
1978
0
  int iScaleY = getChannelTypeScaleY(CH_C, chFmt);
1979
0
  const int cxWidth  = width;
1980
0
  const int cxHeight = height;
1981
1982
0
  iDMvHorX = (mvRT - mvLT).hor * (1 << (iBit - Log2(cxWidth)));
1983
0
  iDMvHorY = (mvRT - mvLT).ver * (1 <<(iBit - Log2(cxWidth)));
1984
0
  if (cu.affineType == AFFINEMODEL_6PARAM)
1985
0
  {
1986
0
    iDMvVerX = (mvLB - mvLT).hor * (1 <<(iBit - Log2(cxHeight)));
1987
0
    iDMvVerY = (mvLB - mvLT).ver * (1 <<(iBit - Log2(cxHeight)));
1988
0
  }
1989
0
  else
1990
0
  {
1991
0
    iDMvVerX = -iDMvHorY;
1992
0
    iDMvVerY = iDMvHorX;
1993
0
  }
1994
0
  const bool subblkMVSpreadOverLimit = InterPrediction::isSubblockVectorSpreadOverLimit(iDMvHorX, iDMvHorY, iDMvVerX, iDMvVerY, cu.interDir);
1995
1996
0
  const int yRefMax     = ( ( ( cu.ly() >> pcv.maxCUSizeLog2 ) + ifpLines + 1 ) << pcv.maxCUSizeLog2 ) - 1;
1997
0
  const int dctifMarginVerBot = 4;
1998
1999
0
  auto roundMvVal = [&](int mvVal, int shift)
2000
0
  {
2001
0
    const int nOffset = 1 << (shift - 1);
2002
0
    mvVal = (mvVal + nOffset - (mvVal >= 0)) >> shift;
2003
0
    return mvVal;
2004
0
  };
2005
0
  auto calcAffineMv = [&](int w, int h)
2006
0
  {
2007
0
    int iMvScaleTmpVer;
2008
0
    if(!subblkMVSpreadOverLimit)
2009
0
    {
2010
0
      iMvScaleTmpVer = iMvScaleVer + iDMvHorY * (iHalfBW + w) + iDMvVerY * (iHalfBH + h);
2011
0
    }
2012
0
    else
2013
0
    {
2014
0
      iMvScaleTmpVer = iMvScaleVer + iDMvHorY * (cxWidth >> 1) + iDMvVerY * (cxHeight >> 1);
2015
0
    }
2016
0
    iMvScaleTmpVer = roundMvVal(iMvScaleTmpVer, shift);
2017
0
    return iMvScaleTmpVer;
2018
0
  };
2019
2020
0
  auto checkMvLineSync = [&](int yMv, int blkBot, const int scaleVer = 0 )
2021
0
  {
2022
0
    if( ( ( blkBot ) + (yMv >> ( mvPrecShift + scaleVer) ) > ( yRefMax >> scaleVer ) ) )
2023
0
      return false;
2024
0
    return true;
2025
0
  };
2026
2027
0
  const int filterMargin = dctifMarginVerBot - 1;
2028
0
  int x = cu.lx();
2029
0
  int y = cu.ly();
2030
2031
  // luma
2032
0
  Position off00 (0, 0);
2033
0
  Position blk00 (x + off00.x, y + off00.y);
2034
0
  int mvVer00 = calcAffineMv(off00.x, off00.y);
2035
0
  if( !checkMvLineSync( mvVer00, blk00.y + filterMargin + nBH ) ) 
2036
0
    return false;
2037
2038
0
  Position off01 (width - nBW, 0);
2039
0
  Position blk01 (x + off01.x, y + off01.y);
2040
0
  int mvVer01 = calcAffineMv(off01.x, off01.y);
2041
0
  if( !checkMvLineSync( mvVer01, blk00.y + filterMargin + nBH ) ) 
2042
0
    return false;
2043
2044
0
  Position off10 (0, height - nBH);
2045
0
  Position blk10 (x + off10.x, y + off10.y);
2046
0
  int mvVer10 = calcAffineMv(off10.x, off10.y);
2047
0
  if( !checkMvLineSync( mvVer10, blk10.y + filterMargin + nBH  ) ) 
2048
0
    return false;
2049
2050
0
  Position off11 (width - nBW, height - nBH);
2051
0
  Position blk11 (x + off11.x, y + off11.y);
2052
0
  int mvVer11 = calcAffineMv(off11.x, off11.y);
2053
0
  if( !checkMvLineSync( mvVer11, blk10.y + filterMargin + nBH  ) ) 
2054
0
    return false;
2055
2056
  // chroma
2057
0
  if( cu.lwidth() == 8 && cu.lheight() == 8 )
2058
0
  {
2059
0
    if(iScaleX || iScaleY)
2060
0
    {
2061
0
      if(iScaleY)
2062
0
      {
2063
0
        const int blkBot = ((blk00.y + filterMargin) >> iScaleY) + nBH;
2064
0
        if(!checkMvLineSync( roundMvVal( mvVer00 + mvVer11, 1 ), blkBot, iScaleY ))
2065
0
          return false;
2066
0
      }
2067
0
      else
2068
0
      {
2069
0
        if(!checkMvLineSync( roundMvVal( mvVer00 + mvVer01, 1 ), blk00.y + filterMargin + nBH, iScaleY ))
2070
0
          return false;
2071
0
        if(!checkMvLineSync( roundMvVal( mvVer10 + mvVer11, 1 ), blk10.y + filterMargin + nBH, iScaleY ))
2072
0
          return false;
2073
0
      }
2074
0
    }
2075
0
  }
2076
0
  else
2077
0
  {
2078
0
    if(iScaleX || iScaleY)
2079
0
    {
2080
0
      int blkBot = ((blk00.y + filterMargin) >> iScaleY) + nBH;
2081
0
      int mvVer00_ = calcAffineMv(off00.x + nBW, off00.y + (iScaleY ? nBH: 0) );
2082
0
      if(!checkMvLineSync( roundMvVal( mvVer00 + mvVer00_, 1 ), blkBot, iScaleY ))
2083
0
        return false;
2084
2085
0
      int _mvVer01 =           calcAffineMv( off01.x - nBW, off01.y       );
2086
0
      int mvVer01_ = iScaleY ? calcAffineMv( off01.x      , off01.y + nBH ): mvVer01;
2087
0
      if(!checkMvLineSync( roundMvVal( _mvVer01 + mvVer01_, 1 ), blkBot, iScaleY ))
2088
0
        return false;
2089
2090
0
      blkBot = ((blk10.y - (iScaleY ? nBH: 0) + filterMargin) >> iScaleY) + nBH;
2091
0
      int _mvVer10 = iScaleY ? calcAffineMv( off10.x      , off10.y - nBH ): mvVer10;
2092
0
      int mvVer10_ =           calcAffineMv( off10.x + nBW, off10.y       );
2093
0
      if(!checkMvLineSync( roundMvVal( _mvVer10 + mvVer10_, 1 ), blkBot, iScaleY ))
2094
0
        return false;
2095
2096
0
      int _mvVer11 = calcAffineMv( off11.x - nBW, off11.y - (iScaleY ? nBH: 0) );
2097
0
      if(!checkMvLineSync( roundMvVal( _mvVer11 + mvVer11, 1 ), blkBot, iScaleY ))
2098
0
        return false;
2099
0
    }
2100
0
  }
2101
2102
0
  return true;
2103
0
}
2104
2105
void InterPrediction::xFillIBCBuffer(CodingUnit& cu)
2106
0
{
2107
0
  for (auto& currPU : CU::traverseTUs(cu))
2108
0
  {
2109
0
    for (const CompArea& area : currPU.blocks)
2110
0
    {
2111
0
      if (!area.valid())
2112
0
      {
2113
0
        continue;
2114
0
      }
2115
0
      const unsigned int lcuWidth = cu.cs->slice->sps->CTUSize;
2116
0
      const int shiftSampleHor = getComponentScaleX(area.compID, cu.chromaFormat);
2117
0
      const int shiftSampleVer = getComponentScaleY(area.compID, cu.chromaFormat);
2118
0
      const int ctuSizeLog2Ver = floorLog2(lcuWidth) - shiftSampleVer;
2119
0
      const int pux = area.x & ((m_IBCBufferWidth >> shiftSampleHor) - 1);
2120
0
      const int puy = area.y & ((1 << ctuSizeLog2Ver) - 1);
2121
0
      const CompArea dstArea = CompArea(area.compID, cu.chromaFormat, Position(pux, puy), Size(area.width, area.height));
2122
0
      CPelBuf srcBuf = cu.cs->getRecoBuf(area);
2123
0
      PelBuf dstBuf = m_IBCBuffer.getBuf(dstArea);
2124
2125
0
      dstBuf.copyFrom(srcBuf);
2126
0
    }
2127
0
  }
2128
0
}
2129
2130
void InterPrediction::xIntraBlockCopyIBC(CodingUnit& cu, PelUnitBuf& predBuf, const ComponentID compID)
2131
0
{
2132
0
  const unsigned int lcuWidth = cu.cs->slice->sps->CTUSize;
2133
0
  const int shiftSampleHor = getComponentScaleX(compID, cu.chromaFormat);
2134
0
  const int shiftSampleVer = getComponentScaleY(compID, cu.chromaFormat);
2135
0
  const int ctuSizeLog2Ver = floorLog2(lcuWidth) - shiftSampleVer;
2136
0
  Mv bv = cu.mv[REF_PIC_LIST_0][0];
2137
0
  bv.changePrecision( MV_PRECISION_INTERNAL, MV_PRECISION_INT );
2138
0
  int refx, refy;
2139
0
  if (compID == COMP_Y)
2140
0
  {
2141
0
    refx = cu.Y().x + bv.hor;
2142
0
    refy = cu.Y().y + bv.ver;
2143
0
  }
2144
0
  else
2145
0
  {//Cb or Cr
2146
0
    refx = cu.Cb().x + (bv.hor >> shiftSampleHor);
2147
0
    refy = cu.Cb().y + (bv.ver >> shiftSampleVer);
2148
0
  }
2149
0
  refx &= ((m_IBCBufferWidth >> shiftSampleHor) - 1);
2150
0
  refy &= ((1 << ctuSizeLog2Ver) - 1);
2151
2152
0
  if (refx + predBuf.bufs[compID].width <= (m_IBCBufferWidth >> shiftSampleHor))
2153
0
  {
2154
0
    const CompArea srcArea = CompArea(compID, cu.chromaFormat, Position(refx, refy), Size(predBuf.bufs[compID].width, predBuf.bufs[compID].height));
2155
0
    const CPelBuf refBuf = m_IBCBuffer.getBuf(srcArea);
2156
0
    predBuf.bufs[compID].copyFrom(refBuf);
2157
0
  }
2158
0
  else
2159
0
  {//wrap around
2160
0
    int width = (m_IBCBufferWidth >> shiftSampleHor) - refx;
2161
0
    CompArea srcArea = CompArea(compID, cu.chromaFormat, Position(refx, refy), Size(width, predBuf.bufs[compID].height));
2162
0
    CPelBuf srcBuf = m_IBCBuffer.getBuf(srcArea);
2163
0
    PelBuf dstBuf = PelBuf(predBuf.bufs[compID].bufAt(Position(0, 0)), predBuf.bufs[compID].stride, Size(width, predBuf.bufs[compID].height));
2164
0
    dstBuf.copyFrom(srcBuf);
2165
2166
0
    width = refx + predBuf.bufs[compID].width - (m_IBCBufferWidth >> shiftSampleHor);
2167
0
    srcArea = CompArea(compID, cu.chromaFormat, Position(0, refy), Size(width, predBuf.bufs[compID].height));
2168
0
    srcBuf = m_IBCBuffer.getBuf(srcArea);
2169
0
    dstBuf = PelBuf(predBuf.bufs[compID].bufAt(Position((m_IBCBufferWidth >> shiftSampleHor) - refx, 0)), predBuf.bufs[compID].stride, Size(width, predBuf.bufs[compID].height));
2170
0
    dstBuf.copyFrom(srcBuf);
2171
0
  }
2172
0
}
2173
2174
void InterPrediction::resetIBCBuffer(const ChromaFormat chromaFormatIDC, const int ctuSize)
2175
0
{
2176
0
  const UnitArea area = UnitArea(chromaFormatIDC, Area(0, 0, m_IBCBufferWidth, ctuSize));
2177
0
  m_IBCBuffer.getBuf(area).fill(-1);
2178
0
}
2179
2180
void InterPrediction::resetVPDUforIBC(const ChromaFormat chromaFormatIDC, const int ctuSize, const int vSize, const int xPos, const int yPos)
2181
0
{
2182
0
  const UnitArea area = UnitArea(chromaFormatIDC, Area(xPos & (m_IBCBufferWidth - 1), yPos & (ctuSize - 1), vSize, vSize));
2183
0
  m_IBCBuffer.getBuf(area).fill(-1);
2184
0
}
2185
bool InterPrediction::isLumaBvValidIBC(const int ctuSize, const int xCb, const int yCb, const int width, const int height, const int xBv, const int yBv)
2186
0
{
2187
0
  if (((yCb + yBv) & (ctuSize - 1)) + height > ctuSize)
2188
0
  {
2189
0
    return false;
2190
0
  }
2191
0
  int refTLx = xCb + xBv;
2192
0
  int refTLy = (yCb + yBv) & (ctuSize - 1);
2193
0
  PelBuf buf = m_IBCBuffer.Y();
2194
0
  for (int x = 0; x < width; x += 4)
2195
0
  {
2196
0
    for (int y = 0; y < height; y += 4)
2197
0
    {
2198
0
      if (buf.at((x + refTLx) & (m_IBCBufferWidth - 1), y + refTLy) == -1) return false;
2199
0
      if (buf.at((x + 3 + refTLx) & (m_IBCBufferWidth - 1), y + refTLy) == -1) return false;
2200
0
      if (buf.at((x + refTLx) & (m_IBCBufferWidth - 1), y + 3 + refTLy) == -1) return false;
2201
0
      if (buf.at((x + 3 + refTLx) & (m_IBCBufferWidth - 1), y + 3 + refTLy) == -1) return false;
2202
0
    }
2203
0
  }
2204
0
  return true;
2205
0
}
2206
2207
} // namespace vvenc
2208
2209
//! \}
2210