Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvdec/source/Lib/CommonLib/IntraPrediction.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
The copyright in this software is being made available under the Clear BSD
3
License, included below. No patent rights, trademark rights and/or 
4
other Intellectual Property Rights other than the copyrights concerning 
5
the Software are granted under this license.
6
7
The Clear BSD License
8
9
Copyright (c) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC Authors.
10
All rights reserved.
11
12
Redistribution and use in source and binary forms, with or without modification,
13
are permitted (subject to the limitations in the disclaimer below) provided that
14
the following conditions are met:
15
16
     * Redistributions of source code must retain the above copyright notice,
17
     this list of conditions and the following disclaimer.
18
19
     * Redistributions in binary form must reproduce the above copyright
20
     notice, this list of conditions and the following disclaimer in the
21
     documentation and/or other materials provided with the distribution.
22
23
     * Neither the name of the copyright holder nor the names of its
24
     contributors may be used to endorse or promote products derived from this
25
     software without specific prior written permission.
26
27
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
28
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
36
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
POSSIBILITY OF SUCH DAMAGE.
39
40
41
------------------------------------------------------------------------------------------- */
42
43
/** \file     Prediction.cpp
44
    \brief    prediction class
45
*/
46
47
#define DONT_UNDEF_SIZE_AWARE_PER_EL_OP
48
49
#include "IntraPrediction.h"
50
51
#include "Unit.h"
52
#include "UnitTools.h"
53
54
#include "Buffer.h"
55
56
#include "dtrace_next.h"
57
#include "Rom.h"
58
59
#include <memory.h>
60
#include <array>
61
62
#include "CommonLib/InterpolationFilter.h"
63
#include "CommonLib/TimeProfiler.h"
64
65
namespace vvdec
66
{
67
68
// ====================================================================================================================
69
// Tables
70
// ====================================================================================================================
71
72
const uint8_t IntraPrediction::m_aucIntraFilter[MAX_NUM_CHANNEL_TYPE][MAX_INTRA_FILTER_DEPTHS] =
73
{
74
  { // Luma
75
    24, //   1xn
76
    24, //   2xn
77
    24, //   4xn
78
    14, //   8xn
79
    2,  //  16xn
80
    0,  //  32xn
81
    0,  //  64xn
82
    0,  // 128xn
83
  },
84
  { // Chroma
85
    40, //   1xn
86
    40, //   2xn
87
    40, //   4xn
88
    28, //   8xn
89
    4,  //  16xn
90
    0,  //  32xn
91
    0,  //  64xn
92
    0,  // 128xn
93
  }
94
};
95
96
const TFilterCoeff g_intraGaussFilter[32][4] = {
97
  { 16, 32, 16,  0 },
98
  { 16, 32, 16,  0 },
99
  { 15, 31, 17,  1 },
100
  { 15, 31, 17,  1 },
101
  { 14, 30, 18,  2 },
102
  { 14, 30, 18,  2 },
103
  { 13, 29, 19,  3 },
104
  { 13, 29, 19,  3 },
105
  { 12, 28, 20,  4 },
106
  { 12, 28, 20,  4 },
107
  { 11, 27, 21,  5 },
108
  { 11, 27, 21,  5 },
109
  { 10, 26, 22,  6 },
110
  { 10, 26, 22,  6 },
111
  {  9, 25, 23,  7 },
112
  {  9, 25, 23,  7 },
113
  {  8, 24, 24,  8 },
114
  {  8, 24, 24,  8 },
115
  {  7, 23, 25,  9 },
116
  {  7, 23, 25,  9 },
117
  {  6, 22, 26, 10 },
118
  {  6, 22, 26, 10 },
119
  {  5, 21, 27, 11 },
120
  {  5, 21, 27, 11 },
121
  {  4, 20, 28, 12 },
122
  {  4, 20, 28, 12 },
123
  {  3, 19, 29, 13 },
124
  {  3, 19, 29, 13 },
125
  {  2, 18, 30, 14 },
126
  {  2, 18, 30, 14 },
127
  {  1, 17, 31, 15 },
128
  {  1, 17, 31, 15 },
129
};
130
131
// Pure vertical prediction with PDPC. A horizontal mode is predicted transposed with the same function.
132
// refMain and refSide point at the reference sample before the first one of the block.
133
void IntraPredHorVerPdpcCore( Pel* pDst, const ptrdiff_t dstStride, const Pel* refMain, const Pel* refSide, const int width, const int height, const int scale, const Pel topLeft, const ClpRng& clpRng )
134
0
{
135
0
  const int lev[4] = { std::min( 3, width ), std::min( 6, width ), std::min( 12, width ), std::min( 24, width ) };
136
137
0
  for( int y = 0; y < height; y++ )
138
0
  {
139
0
    const Pel  left =  refSide[y + 1];
140
0
          Pel *line = &pDst[y * dstStride];
141
0
    for( int x = 0; x < lev[scale]; x++ )
142
0
    {
143
0
      int wL = 32 >> std::min( 31, ( ( x << 1 ) >> scale ) );
144
0
      *line++ = ClipPel( ( wL * ( left - topLeft ) + refMain[x + 1] * ( 1 << 6 ) + 32 ) >> 6, clpRng );
145
0
    }
146
0
    memcpy( line, refMain + lev[scale] + 1, ( width - lev[scale] ) * sizeof( Pel ) );
147
0
  }
148
0
}
149
150
// PDPC of the angular modes with a positive angle: the first samples of each row move towards the side reference
151
// sample on their prediction direction, with a weight that decreases with the distance from the side.
152
void IntraPredAngPdpcCore( Pel* pDst, const ptrdiff_t dstStride, const Pel* refSide, const int width, const int height, const int invAngle, const int angularScale )
153
0
{
154
0
  for( int y = 0; y < height; y++, pDst += dstStride )
155
0
  {
156
0
    int invAngleSum = 256;
157
158
0
    for( int x = 0; x < std::min( 3 << angularScale, width ); x++ )
159
0
    {
160
0
      invAngleSum += invAngle;
161
162
0
      int wL   = 32 >> ( 2 * x >> angularScale );
163
0
      Pel left = refSide[y + ( invAngleSum >> 9 ) + 1];
164
0
      pDst[x]  = pDst[x] + ( ( wL * ( left - pDst[x] ) + 32 ) >> 6 );
165
0
    }
166
0
  }
167
0
}
168
169
void GetLumaRecPixel420Core (const int width,const int height, const Pel* pRecSrc0,const ptrdiff_t iRecStride,Pel* pDst0,const ptrdiff_t iDstStride)
170
0
{
171
0
  for( int y = 0; y < height; y++ )
172
0
    {
173
0
      for( int x = 0; x < width; x ++ )
174
0
      {
175
0
        pDst0[x + 0 ] = (   pRecSrc0[( (x + 0 ) << 1 )    ] * 2
176
0
                          + pRecSrc0[( (x + 0 ) << 1 ) + 1] * 1
177
0
                          + pRecSrc0[( (x + 0 ) << 1 ) - 1] * 1
178
0
                          + pRecSrc0[( (x + 0 ) << 1 ) + iRecStride] * 2
179
0
                          + pRecSrc0[( (x + 0 ) << 1 ) + 1 + iRecStride] * 1
180
0
                          + pRecSrc0[( (x + 0 ) << 1 ) - 1 + iRecStride] * 1
181
0
                          + 4 ) >> 3;
182
0
      }
183
0
      pDst0 += iDstStride;
184
0
      pRecSrc0 += (iRecStride<<1);
185
0
    }
186
0
}
187
188
// 4:2:0 luma downsampling for CCLM when the chroma samples are collocated with the even luma samples
189
// (sps_chroma_vertical_collocated_flag): a 5-tap cross around each even luma sample. The left and the above neighbours
190
// of the block must be available.
191
void GetLumaRecPixel420CollocatedCore( const int width, const int height, const Pel* pRecSrc0, const ptrdiff_t iRecStride, Pel* pDst0, const ptrdiff_t iDstStride )
192
0
{
193
0
  for( int y = 0; y < height; y++ )
194
0
  {
195
0
    for( int x = 0; x < width; x++ )
196
0
    {
197
0
      const Pel* center = pRecSrc0 + 2 * x;
198
0
      pDst0[x] = ( center[-iRecStride] + center[-1] + 4 * center[0] + center[1] + center[iRecStride] + 4 ) >> 3;
199
0
    }
200
0
    pDst0    += iDstStride;
201
0
    pRecSrc0 += 2 * iRecStride;
202
0
  }
203
0
}
204
205
// The 4:2:0 CCLM template column left of the block, one sample per two luma rows, written down the column of pDst:
206
// the 6-tap filter around luma column -2, or with collocated chroma the 5-tap cross around it. pRecSrc0 points to the
207
// top left luma sample of the block. The rows above the block must be available for the collocated filter.
208
void GetLumaRecPixel420LeftCore( const int height, const Pel* pRecSrc0, const ptrdiff_t iRecStride, Pel* pDst, const ptrdiff_t iDstStride, const bool collocated )
209
0
{
210
0
  const Pel* center = pRecSrc0 - 2;
211
0
  for( int y = 0; y < height; y++, center += 2 * iRecStride, pDst += iDstStride )
212
0
  {
213
0
    if( collocated )
214
0
    {
215
0
      pDst[0] = ( center[-iRecStride] + center[-1] + 4 * center[0] + center[1] + center[iRecStride] + 4 ) >> 3;
216
0
    }
217
0
    else
218
0
    {
219
0
      pDst[0] = ( 2 * center[0] + center[-1] + center[1] + 2 * center[iRecStride] + center[iRecStride - 1] + center[iRecStride + 1] + 4 ) >> 3;
220
0
    }
221
0
  }
222
0
}
223
224
/** Function for deriving planar intra prediction. This function derives the prediction samples for planar mode (intra coding).
225
 */
226
227
//NOTE: Bit-Limit - 24-bit source
228
void xPredIntraPlanarCore( const CPelBuf &pSrc, PelBuf &pDst, const SPS& sps )
229
0
{
230
  // with some optimizations gcc gives spurious "-Wmaybe-uninitialized" warnings here
231
  GCC_WARNING_DISABLE_maybe_uninitialized
232
233
0
  const uint32_t width  = pDst.width;
234
0
  const uint32_t height = pDst.height;
235
0
  const uint32_t log2W  = getLog2( width );
236
0
  const uint32_t log2H  = getLog2( height );
237
0
  int leftColumn[MAX_CU_SIZE + 1], topRow[MAX_CU_SIZE + 1], bottomRow[MAX_CU_SIZE], rightColumn[MAX_CU_SIZE];
238
0
  const uint32_t offset = 1 << (log2W + log2H);
239
  // Get left and above reference column and row
240
0
  for( int k = 0; k < width + 1; k++ )
241
0
  {
242
0
    topRow[k] = pSrc.at( k + 1, 0 );
243
0
  }
244
245
0
  for( int k = 0; k < height + 1; k++ )
246
0
  {
247
0
    leftColumn[k] = pSrc.at( 0, k + 1 );
248
0
  }
249
250
  // Prepare intermediate variables used in interpolation
251
0
  int bottomLeft = leftColumn[height];
252
0
  int topRight = topRow[width];
253
254
0
  for( int k = 0; k < width; k++ )
255
0
  {
256
0
    bottomRow[k] = bottomLeft - topRow[k];
257
0
    topRow[k]    = topRow[k] << log2H;
258
0
  }
259
260
0
  for( int k = 0; k < height; k++ )
261
0
  {
262
0
    rightColumn[k] = topRight - leftColumn[k];
263
0
    leftColumn[k]  = leftColumn[k] << log2W;
264
0
  }
265
266
0
  const uint32_t finalShift = 1 + log2W + log2H;
267
0
  const ptrdiff_t stride     = pDst.stride;
268
0
  Pel*       pred       = pDst.buf;
269
0
  for( int y = 0; y < height; y++, pred += stride )
270
0
  {
271
0
    int horPred = leftColumn[y];
272
273
0
    for( int x = 0; x < width; x++ )
274
0
    {
275
0
      horPred += rightColumn[y];
276
0
      topRow[x] += bottomRow[x];
277
278
0
      int vertPred = topRow[x];
279
0
      pred[x]      = ( ( horPred << log2H ) + ( vertPred << log2W ) + offset ) >> finalShift;
280
281
0
    }
282
0
  }
283
0
  GCC_WARNING_RESET
284
0
}
285
286
void  IntraPredSampleFilterCore(Pel *ptrSrc,const ptrdiff_t  srcStride,PelBuf &piPred,const uint32_t uiDirMode,const ClpRng& clpRng)
287
0
{
288
0
  const CPelBuf srcBuf  ( ptrSrc, ( SizeType ) srcStride, ( SizeType ) srcStride );
289
0
  const int     iWidth  = piPred.width;
290
0
  const int     iHeight = piPred.height;
291
0
  PelBuf        dstBuf  = piPred;
292
293
0
  const int scale = ((getLog2(iWidth) - 2 + getLog2(iHeight) - 2 + 2) >> 2);
294
0
  CHECK(scale < 0 || scale > 31, "PDPC: scale < 0 || scale > 31");
295
296
0
#if 1
297
0
  if( uiDirMode == PLANAR_IDX || uiDirMode == DC_IDX )
298
0
  {
299
0
    for( int y = 0; y < iHeight; y++ )
300
0
    {
301
0
      const int wT   = 32 >> std::min(31, ((y << 1) >> scale));
302
//      const Pel left = srcBuf.at(y + 1, 1);
303
0
      const Pel left = srcBuf.at(0, y + 1 );
304
0
      for (int x = 0; x < iWidth; x++)
305
0
      {
306
0
        const int wL    = 32 >> std::min(31, ((x << 1) >> scale));
307
0
        const Pel top   = srcBuf.at(x + 1, 0);
308
0
        const Pel val   = dstBuf.at(x, y);
309
0
        dstBuf.at(x, y) = val + ((wL * (left - val) + wT * (top - val) + 32) >> 6);
310
0
      }
311
0
    }
312
0
  }
313
#else
314
  const int lev[4]={std::min(3,iWidth),std::min(6,iWidth),std::min(12,iWidth),std::min(24,iWidth)};
315
  if (uiDirMode == PLANAR_IDX)
316
  {
317
    for (int y = 0; y < iHeight; y++)
318
    {
319
      int wT = 32 >> std::min(31, ((y << 1) >> scale));
320
      const Pel left = srcBuf.at(0, y + 1);
321
      if (wT)
322
      {
323
        for (int x = 0; x < iWidth; x++)
324
        {
325
          const Pel top = srcBuf.at(x + 1, 0);
326
          int wL = 32 >> std::min(31, ((x << 1) >> scale));
327
          dstBuf.at(x, y) = ClipPel((wL * left + wT * top + (64 - wL - wT) * dstBuf.at(x, y) + 32) >> 6, clpRng);
328
329
        }
330
      }
331
      else
332
      {
333
        for (int x = 0; x < lev[scale]; x++)   // bis wL 0 ist, das ist bei x lev[scale]
334
        {
335
          int wL = 32 >> std::min(31, ((x << 1) >> scale));
336
          dstBuf.at(x, y) = ClipPel((wL * left + (64 - wL) * dstBuf.at(x, y) + 32) >> 6, clpRng);
337
        }
338
      }
339
    }
340
  }
341
  else if (uiDirMode == DC_IDX)
342
  {
343
    const Pel topLeft = srcBuf.at(0, 0);
344
    for (int y = 0; y < iHeight; y++)
345
    {
346
      int wT = 32 >> std::min(31, ((y << 1) >> scale));
347
      const Pel left = srcBuf.at(0, y + 1);
348
      if (wT)
349
      {
350
351
        for (int x = 0; x < iWidth; x++)
352
        {
353
          const Pel top = srcBuf.at(x + 1, 0);
354
          int wL = 32 >> std::min(31, ((x << 1) >> scale));
355
          int wTL = (wL >> 4) + (wT >> 4);
356
          dstBuf.at(x, y) = ClipPel((wL * left + wT * top - wTL * topLeft + (64 - wL - wT + wTL) * dstBuf.at(x, y) + 32) >> 6, clpRng);
357
          }
358
      }
359
      else
360
      {
361
        for (int x = 0; x < lev[scale]; x++)
362
        {
363
          const Pel top = srcBuf.at(x + 1, 0);
364
          int wL = 32 >> std::min(31, ((x << 1) >> scale));
365
          int wTL = (wL >> 4) + (wT >> 4);
366
          dstBuf.at(x, y) = ClipPel((wL * left + wT * top - wTL * topLeft + (64 - wL - wT + wTL) * dstBuf.at(x, y) + 32) >> 6, clpRng);
367
        }
368
369
      }
370
    }
371
  }
372
#endif
373
0
}
374
375
template<typename T>
376
void IntraPredAngleCore(T* pDstBuf,const ptrdiff_t dstStride,T* refMain,int width,int height,int deltaPos,int intraPredAngle,const TFilterCoeff *ff,const bool useCubicFilter,const ClpRng& clpRng)
377
0
{
378
0
    for (int y = 0; y<height; y++ )
379
0
    {
380
0
      const int deltaInt   = deltaPos >> 5;
381
0
      const int deltaFract = deltaPos & ( 32 - 1 );
382
383
0
      Pel p[4];
384
385
0
      int refMainIndex = deltaInt + 1;
386
387
0
      const TFilterCoeff *f = &ff[deltaFract << 2];
388
389
0
      for( int x = 0; x < width; x++, refMainIndex++ )
390
0
      {
391
0
        p[0] = refMain[refMainIndex - 1];
392
0
        p[1] = refMain[refMainIndex    ];
393
0
        p[2] = refMain[refMainIndex + 1];
394
0
        p[3] = refMain[refMainIndex + 2];
395
396
0
        pDstBuf[y*dstStride + x] = static_cast<Pel>((static_cast<int>(f[0] * p[0]) + static_cast<int>(f[1] * p[1]) + static_cast<int>(f[2] * p[2]) + static_cast<int>(f[3] * p[3]) + 32) >> 6);
397
398
0
        if( useCubicFilter ) // only cubic filter has negative coefficients and requires clipping
399
0
        {
400
0
          pDstBuf[y*dstStride + x] = ClipPel( pDstBuf[y*dstStride + x], clpRng );
401
0
        }
402
0
      }
403
0
      deltaPos += intraPredAngle;
404
0
    }
405
0
}
406
407
template<typename T>
408
void IntraPredAngleChroma(T* pDstBuf,const ptrdiff_t dstStride,int16_t* pBorder,int width,int height,int deltaPos,int intraPredAngle)
409
0
{
410
0
  for (int y = 0; y<height; y++)
411
0
  {
412
0
    const int deltaInt   = deltaPos >> 5;
413
0
    const int deltaFract = deltaPos & (32 - 1);
414
415
    // Do linear filtering
416
0
    const Pel *pRM = pBorder + deltaInt + 1;
417
0
    int lastRefMainPel = *pRM++;
418
419
0
    for( int x = 0; x < width; pRM++, x++ )
420
0
    {
421
0
      int thisRefMainPel = *pRM;
422
0
      pDstBuf[x + 0] = ( Pel ) ( ( ( 32 - deltaFract )*lastRefMainPel + deltaFract*thisRefMainPel + 16 ) >> 5 );
423
0
      lastRefMainPel = thisRefMainPel;
424
0
    }
425
0
    deltaPos += intraPredAngle;
426
0
    pDstBuf += dstStride;
427
0
  }
428
429
0
}
430
431
// Blends the merge prediction in dst with the intra prediction for CIIP. The weights sum to 4.
432
void CiipBlendCore( Pel* dst, const ptrdiff_t dstStride, const Pel* intraPred, const ptrdiff_t intraStride, const int width, const int height, const int wMerge, const int wIntra )
433
0
{
434
0
  for( int y = 0; y < height; y++, dst += dstStride, intraPred += intraStride )
435
0
  {
436
0
    for( int x = 0; x < width; x++ )
437
0
    {
438
0
      dst[x] = ( wMerge * dst[x] + wIntra * intraPred[x] + 2 ) >> 2;
439
0
    }
440
0
  }
441
0
}
442
443
// ====================================================================================================================
444
// Constructor / destructor / initialize
445
// ====================================================================================================================
446
447
37.8k
IntraPrediction::IntraPrediction() : m_currChromaFormat( NUM_CHROMA_FORMAT )
448
37.8k
{
449
37.8k
  IntraPredAngleCore4 = IntraPredAngleCore;
450
37.8k
  IntraPredAngleCore8 = IntraPredAngleCore;
451
37.8k
  IntraPredAngleChroma4 = IntraPredAngleChroma;
452
37.8k
  IntraPredAngleChroma8 = IntraPredAngleChroma;
453
37.8k
  IntraPredAngPdpc = IntraPredAngPdpcCore;
454
455
37.8k
  IntraPredSampleFilter8 = IntraPredSampleFilterCore;
456
37.8k
  IntraPredSampleFilter16 = IntraPredSampleFilterCore;
457
458
37.8k
  MipMatrixMul   = m_matrixIntraPred.m_predictorMip.MatrixMul;
459
37.8k
  MipUpsampleHor = m_matrixIntraPred.m_predictorMip.UpsampleHor;
460
461
37.8k
  xPredIntraPlanar = xPredIntraPlanarCore;
462
37.8k
  CiipBlend = CiipBlendCore;
463
37.8k
  IntraPredHorVerPdpc = IntraPredHorVerPdpcCore;
464
465
37.8k
  GetLumaRecPixel420           = GetLumaRecPixel420Core;
466
37.8k
  GetLumaRecPixel420Collocated = GetLumaRecPixel420CollocatedCore;
467
37.8k
  GetLumaRecPixel420Left       = GetLumaRecPixel420LeftCore;
468
37.8k
}
469
470
IntraPrediction::~IntraPrediction()
471
37.8k
{
472
37.8k
  destroy();
473
37.8k
}
474
475
void IntraPrediction::destroy()
476
37.8k
{
477
37.8k
}
478
479
void IntraPrediction::init(ChromaFormat chromaFormatIDC, const unsigned bitDepthY)
480
0
{
481
  // if it has been initialised before, but the chroma format has changed, release the memory and start again.
482
0
  if (m_currChromaFormat != chromaFormatIDC)
483
0
  {
484
0
    destroy();
485
0
  }
486
487
0
  m_currChromaFormat = chromaFormatIDC;
488
489
0
  std::fill_n( m_neighborSize, 3, 0 );
490
0
  m_lastCUidx = -1;
491
492
#if ENABLE_SIMD_OPT_INTRAPRED && defined( TARGET_SIMD_X86 )
493
  initIntraPredictionX86();
494
#endif
495
#if ENABLE_SIMD_OPT_INTRAPRED && defined( TARGET_SIMD_ARM )
496
  initIntraPredictionARM();
497
#endif
498
0
}
499
500
// ====================================================================================================================
501
// Public member functions
502
// ====================================================================================================================
503
504
// Function for calculating DC value of the reference samples used in Intra prediction
505
//NOTE: Bit-Limit - 25-bit source
506
Pel IntraPrediction::xGetPredValDc( const CPelBuf &pSrc, const Size &dstSize, const int mrlIdx )
507
0
{
508
0
  CHECK( dstSize.width == 0 || dstSize.height == 0, "Empty area provided" );
509
510
0
  int idx, sum = 0;
511
0
  Pel dcVal;
512
0
  const int  width     = dstSize.width;
513
0
  const int  height    = dstSize.height;
514
0
  const auto denom     = (width == height) ? (width << 1) : std::max(width,height);
515
0
  const auto divShift  = getLog2(denom);
516
0
  const auto divOffset = (denom >> 1);
517
518
0
  if( width >= height )
519
0
  {
520
0
    for( idx = 0; idx < width; idx++ )
521
0
    {
522
0
      sum += pSrc.at( mrlIdx + 1 + idx, 0 );
523
0
    }
524
0
  }
525
0
  if( width <= height )
526
0
  {
527
0
    for( idx = 0; idx < height; idx++ )
528
0
    {
529
0
      sum += pSrc.at( 0, mrlIdx + 1 + idx );
530
0
    }
531
0
  }
532
533
0
  dcVal = (sum + divOffset) >> divShift;
534
0
  return dcVal;
535
0
}
536
537
int IntraPrediction::getWideAngle( int width, int height, int predMode )
538
0
{
539
0
  if ( predMode > DC_IDX && predMode <= VDIA_IDX )
540
0
  {
541
0
    int modeShift[] = { 0, 6, 10, 12, 14, 15 };
542
0
    int deltaSize = abs(getLog2(width) - getLog2(height));
543
0
    if (width > height && predMode < 2 + modeShift[deltaSize])
544
0
    {
545
0
      predMode += (VDIA_IDX - 1);
546
0
    }
547
0
    else if (height > width && predMode > VDIA_IDX - modeShift[deltaSize])
548
0
    {
549
0
      predMode -= (VDIA_IDX - 1);
550
0
    }
551
0
  }
552
0
  return predMode;
553
0
}
554
555
void IntraPrediction::setReferenceArrayLengths( const CompArea &area )
556
0
{
557
  // set Top and Left reference samples length
558
0
  const int  width    = area.width;
559
0
  const int  height   = area.height;
560
561
0
  m_leftRefLength     = (height << 1);
562
0
  m_topRefLength      = (width << 1);
563
564
0
}
565
566
567
568
void IntraPrediction::predIntraAng( const ComponentID compID, PelBuf &piPred, const CodingUnit &cu, const bool useFilteredPredSamples )
569
0
{
570
0
  const ChannelType    channelType  = toChannelType( compID );
571
0
  const int            iWidth       = piPred.width;
572
0
  const int            iHeight      = piPred.height;
573
0
  const Size           cuSize       = Size( cu.blocks[compID].width, cu.blocks[compID].height );
574
0
  CHECK( CU::isMIP(cu, toChannelType(compID)), "We should not get here for MIP." );
575
0
  const uint32_t       uiDirMode    = isLuma( compID ) && cu.bdpcmMode() ? BDPCM_IDX : !isLuma(compID) && cu.bdpcmModeChroma() ? BDPCM_IDX : PU::getFinalIntraMode(cu, channelType);
576
577
0
  CHECKD( iWidth == 2, "Width of 2 is not supported" );
578
579
0
  const int     multiRefIdx = ( compID == COMPONENT_Y ) ? cu.multiRefIdx() : 0;
580
0
  const bool    useISP      = cu.ispMode() && isLuma( compID );
581
0
  const int     srcStride   = m_topRefLength  + 1 + multiRefIdx;
582
0
  const int     srcHStride  = m_leftRefLength + 1 + multiRefIdx;
583
0
  const ClpRng& clpRng      ( cu.slice->clpRng( compID ) );
584
0
        bool    doPDPC      = ( iWidth >= MIN_TB_SIZEY && iHeight >= MIN_TB_SIZEY ) && multiRefIdx == 0;
585
586
0
  const PelBuf& srcBuf = cu.ispMode() && isLuma(compID) ? getISPBuffer( useFilteredPredSamples ) : PelBuf(getPredictorPtr(compID, useFilteredPredSamples), srcStride, srcHStride);
587
588
0
  switch (uiDirMode)
589
0
  {
590
0
    case(PLANAR_IDX): xPredIntraPlanar(srcBuf, piPred, *cu.sps); break;
591
0
    case(DC_IDX):     xPredIntraDc    (srcBuf, piPred, channelType, false, multiRefIdx); break;
592
0
    case(BDPCM_IDX):  xPredIntraBDPCM(srcBuf, piPred, isLuma(compID) ? cu.bdpcmMode() : cu.bdpcmModeChroma(), clpRng); break;
593
0
    case(2):
594
0
    case(DIA_IDX):
595
0
    case(VDIA_IDX):
596
0
      if (getWideAngle(useISP ? cuSize.width : iWidth, useISP ? cuSize.height : iHeight, uiDirMode) == static_cast<int>(uiDirMode)) // check if uiDirMode is not wide-angle
597
0
      {
598
0
        xPredIntraAng(srcBuf, piPred, channelType, uiDirMode, clpRng, *cu.sps, multiRefIdx, useFilteredPredSamples, doPDPC, useISP, cuSize );
599
0
        break;
600
0
      }
601
0
    default:          xPredIntraAng(srcBuf, piPred, channelType, uiDirMode, clpRng, *cu.sps, multiRefIdx, useFilteredPredSamples, doPDPC, useISP, cuSize); break;
602
0
  }
603
604
0
  if( doPDPC && (uiDirMode == PLANAR_IDX || uiDirMode == DC_IDX ) )
605
0
  {
606
0
    if (iWidth>8)
607
0
      IntraPredSampleFilter16(srcBuf.buf,srcBuf.stride,piPred,uiDirMode,clpRng);
608
0
    else
609
0
      IntraPredSampleFilter8(srcBuf.buf,srcBuf.stride,piPred,uiDirMode,clpRng);
610
0
  }
611
0
}
612
613
void IntraPrediction::predIntraChromaLM( const ComponentID compID, PelBuf& piPred, const CodingUnit& cu, const CompArea& chromaArea, int intraDir )
614
0
{
615
0
  int  iLumaStride = 0;
616
0
  PelBuf Temp;
617
0
  if( (intraDir == MDLM_L_IDX) || (intraDir == MDLM_T_IDX) )
618
0
  {
619
0
    iLumaStride = 2 * MAX_TU_SIZE_FOR_PROFILE + 1;
620
0
    Temp = PelBuf( m_piYuvExt[1] + iLumaStride + 1, iLumaStride, Size( chromaArea ) );
621
0
  }
622
0
  else
623
0
  {
624
0
    iLumaStride = MAX_TU_SIZE_FOR_PROFILE + 1;
625
0
    Temp = PelBuf( m_piYuvExt[1] + iLumaStride + 1, iLumaStride, Size( chromaArea ) );
626
0
  }
627
0
  int a, b, iShift;
628
0
  xGetLMParameters( cu, compID, chromaArea, a, b, iShift );
629
630
  ////// final prediction
631
0
  piPred.copyFrom( Temp );
632
0
  piPred.linearTransform( a, iShift, b, true, cu.slice->clpRng( compID ) );
633
0
}
634
635
void IntraPrediction::xPredIntraDc( const CPelBuf &pSrc, PelBuf &pDst, const ChannelType channelType, const bool enableBoundaryFilter, const int mrlIdx )
636
0
{
637
0
  const Pel dcval = xGetPredValDc( pSrc, pDst, mrlIdx );
638
0
  pDst.fill( dcval );
639
0
}
640
641
// Function for deriving the angular Intra predictions
642
void IntraPredAngleCore(Pel *pDstBuf,const int dstStride,Pel* refMain,int width,int height,int deltaPos,int intraPredAngle,const TFilterCoeff *ff,const bool useCubicFilter,const ClpRng& clpRng)
643
0
{
644
0
  for (int y = 0; y<height; y++ )
645
0
  {
646
0
    const int deltaInt   = deltaPos >> 5;
647
0
    const int deltaFract = deltaPos & ( 32 - 1 );
648
649
0
    Pel p[4];
650
651
0
    int refMainIndex = deltaInt + 1;
652
653
0
    const TFilterCoeff *f = &ff[deltaFract << 2];
654
655
0
    for( int x = 0; x < width; x++, refMainIndex++ )
656
0
    {
657
0
      p[0] = refMain[refMainIndex - 1];
658
0
      p[1] = refMain[refMainIndex    ];
659
0
      p[2] = refMain[refMainIndex + 1];
660
0
      p[3] = refMain[refMainIndex + 2];
661
662
0
      pDstBuf[y*dstStride + x] = static_cast<Pel>((static_cast<int>(f[0] * p[0]) + static_cast<int>(f[1] * p[1]) + static_cast<int>(f[2] * p[2]) + static_cast<int>(f[3] * p[3]) + 32) >> 6);
663
664
0
      if( useCubicFilter ) // only cubic filter has negative coefficients and requires clipping
665
0
      {
666
0
        pDstBuf[y*dstStride + x] = ClipPel( pDstBuf[y*dstStride + x], clpRng );
667
0
      }
668
0
    }
669
0
    deltaPos += intraPredAngle;
670
0
  }
671
0
}
672
673
674
/** Function for deriving the simplified angular intra predictions.
675
 *
676
 * This function derives the prediction samples for the angular mode based on the prediction direction indicated by
677
 * the prediction mode index. The prediction direction is given by the displacement of the bottom row of the block and
678
 * the reference row above the block in the case of vertical prediction or displacement of the rightmost column
679
 * of the block and reference column left from the block in the case of the horizontal prediction. The displacement
680
 * is signalled at 1/32 pixel accuracy. When projection of the predicted pixel falls inbetween reference samples,
681
 * the predicted value for the pixel is linearly interpolated from the reference samples. All reference samples are taken
682
 * from the extended main reference.
683
 */
684
//NOTE: Bit-Limit - 25-bit source
685
686
void IntraPrediction::xPredIntraAng( const CPelBuf &pSrc, PelBuf &pDst, const ChannelType channelType, const uint32_t dirMode, const ClpRng& clpRng, const SPS& sps,
687
                                           int      multiRefIdx,
688
                                     const bool     useFilteredPredSamples ,
689
                                           bool     &doPDPC,
690
                                     const bool     useISP,
691
                                     const Size     cuSize
692
                                    )
693
0
{
694
0
  int width =int(pDst.width);
695
0
  int height=int(pDst.height);
696
697
0
  CHECK( !( dirMode > DC_IDX && dirMode < NUM_LUMA_MODE ), "Invalid intra dir" );
698
0
  int              predMode           = useISP ? getWideAngle( cuSize.width, cuSize.height, dirMode ) : getWideAngle( width, height, dirMode );
699
0
  const bool       bIsModeVer         = predMode >= DIA_IDX;
700
0
  const int        intraPredAngleMode = (bIsModeVer) ? predMode - VER_IDX : -(predMode - HOR_IDX);
701
0
  const int        absAngMode         = abs(intraPredAngleMode);
702
0
  const int        signAng            = intraPredAngleMode < 0 ? -1 : 1;
703
704
  // Set bitshifts and scale the angle parameter to block size
705
0
  static const int angTable[32]    = { 0,    1,    2,    3,    4,    6,     8,   10,   12,   14,   16,   18,   20,   23,   26,   29,   32,   35,   39,  45,  51,  57,  64,  73,  86, 102, 128, 171, 256, 341, 512, 1024 };
706
0
  static const int invAngTable[32] = {
707
0
    0,   16384, 8192, 5461, 4096, 2731, 2048, 1638, 1365, 1170, 1024, 910, 819, 712, 630, 565,
708
0
    512, 468,   420,  364,  321,  287,  256,  224,  191,  161,  128,  96,  64,  48,  32,  16
709
0
  };   // (512 * 32) / Angle
710
0
  int invAngle                    = invAngTable[absAngMode];
711
0
  int absAng                      = angTable   [absAngMode];
712
0
  int intraPredAngle              = signAng * absAng;
713
714
0
  Pel* refMain;
715
0
  Pel* refSide;
716
717
0
  Pel  refAbove[2 * MAX_CU_SIZE + 3 + 33 * MAX_REF_LINE_IDX];
718
0
  Pel  refLeft [2 * MAX_CU_SIZE + 3 + 33 * MAX_REF_LINE_IDX];
719
720
  // Initialize the Main and Left reference array.
721
0
  if (intraPredAngle < 0)
722
0
  {
723
0
    for (int x = 0; x <= width + 1 + multiRefIdx; x++)
724
0
    {
725
0
      refAbove[x + height] = pSrc.at(x, 0);
726
0
    }
727
0
    for (int y = 0; y <= height + 1 + multiRefIdx; y++)
728
0
    {
729
0
      refLeft[y + width] = pSrc.at(0, y);
730
0
    }
731
0
    refMain = bIsModeVer ? refAbove + height : refLeft + width;
732
0
    refSide = bIsModeVer ? refLeft  + width : refAbove + height;
733
734
    // Extend the Main reference to the left.
735
0
    int sizeSide = bIsModeVer ? height : width;
736
0
    for (int k = -sizeSide; k <= -1; k++)
737
0
    {
738
0
      refMain[k] = refSide[std::min((-k * invAngle + 256) >> 9, sizeSide)];
739
0
    }
740
0
  }
741
0
  else
742
0
  {
743
    //for (int x = 0; x <= m_topRefLength + multiRefIdx; x++)
744
    //{
745
    //  refAbove[x] = pSrc.at(x, 0);
746
    //}
747
0
    memcpy( refAbove, pSrc.buf, ( m_topRefLength + multiRefIdx + 1 ) * sizeof( Pel ) );
748
0
    for (int y = 0; y <= m_leftRefLength + multiRefIdx; y++)
749
0
    {
750
0
      refLeft[y] = pSrc.at(0, y);
751
0
    }
752
753
0
    refMain = bIsModeVer ? refAbove : refLeft;
754
0
    refSide = bIsModeVer ? refLeft : refAbove;
755
756
    // Extend main reference to right using replication
757
0
    const int log2Ratio = getLog2(width) - getLog2(height);
758
0
    const int s         = std::max<int>(0, bIsModeVer ? log2Ratio : -log2Ratio);
759
0
    const int maxIndex  = (multiRefIdx << s) + 2;
760
0
    const int refLength = bIsModeVer ? m_topRefLength : m_leftRefLength;
761
0
    const Pel val       = refMain[refLength + multiRefIdx];
762
0
    for (int z = 1; z <= maxIndex; z++)
763
0
    {
764
0
      refMain[refLength + multiRefIdx + z] = val;
765
0
    }
766
0
  }
767
768
  // swap width/height if we are doing a horizontal mode:
769
0
  Pel tempArray[MAX_TB_SIZEY*MAX_TB_SIZEY];
770
0
  const ptrdiff_t dstStride = bIsModeVer ? pDst.stride : MAX_TB_SIZEY;
771
0
  Pel *pDstBuf = bIsModeVer ? pDst.buf : tempArray;
772
0
  if (!bIsModeVer)
773
0
  {
774
0
    std::swap(width, height);
775
0
  }
776
777
  // compensate for line offset in reference line buffers
778
0
  refMain += multiRefIdx;
779
0
  refSide += multiRefIdx;
780
781
0
  if( intraPredAngle == 0 )  // pure vertical or pure horizontal
782
0
  {
783
0
    if( doPDPC )
784
0
    {
785
0
      const int scale = ( ( getLog2( width ) - 2 + getLog2( height ) - 2 + 2 ) >> 2 );
786
0
      CHECK( scale < 0 || scale > 3, "PDPC: scale < 0 || scale > 3" );
787
788
0
      IntraPredHorVerPdpc( pDstBuf, dstStride, refMain, refSide, width, height, scale, pSrc.at( 0, 0 ), clpRng );
789
0
    }
790
0
    else
791
0
    {
792
0
      for( int y = 0; y < height; y++ )
793
0
      {
794
0
        memcpy( pDstBuf + y * dstStride, refMain + 1, width * sizeof( Pel ) );
795
0
      }
796
797
0
    }
798
0
  }
799
0
  else
800
0
  {
801
0
    Pel *pDsty=pDstBuf;
802
803
0
    if( !(0 == (absAng & 0x1F)) )
804
0
    {
805
0
      if( isLuma(channelType) )
806
0
      {
807
0
        int deltaPos = intraPredAngle * (1 + multiRefIdx);
808
0
        bool interpolationFlag = false, filterFlag = false;
809
0
        const int diff = std::min<int>( abs( predMode - HOR_IDX ), abs( predMode - VER_IDX ) );
810
0
        const int log2Size = ((getLog2(width) + getLog2(height)) >> 1);
811
0
        CHECKD( log2Size >= MAX_INTRA_FILTER_DEPTHS, "Size not supported" );
812
0
        filterFlag = (diff > m_aucIntraFilter[channelType][log2Size]);
813
814
0
        if( filterFlag )
815
0
        {
816
0
          const bool isRefFilter = 0 == ( absAng & 0x1F );
817
0
          interpolationFlag = !isRefFilter;
818
0
        }
819
0
        const bool useCubicFilter = useISP ? true : ( !interpolationFlag || multiRefIdx > 0 );
820
0
        const TFilterCoeff *f              = (useCubicFilter) ? InterpolationFilter::getChromaFilterTable(0) : g_intraGaussFilter[0];
821
0
        if( ( width & 7 ) == 0 )
822
0
        {
823
0
          IntraPredAngleCore8(pDstBuf,dstStride,refMain,width,height,deltaPos,intraPredAngle,f,useCubicFilter,clpRng);
824
825
0
        }
826
0
        else if( ( width & 3 ) == 0 )
827
0
        {
828
0
          IntraPredAngleCore4(pDstBuf,dstStride,refMain,width,height,deltaPos,intraPredAngle,f,useCubicFilter,clpRng);
829
0
        }
830
0
        else
831
0
        {
832
0
          CHECK( !useISP, "should not happen" );
833
834
0
          for (int y = 0; y<height; y++ )
835
0
          {
836
0
            const int deltaInt   = deltaPos >> 5;
837
0
            const int deltaFract = deltaPos & ( 32 - 1 );
838
839
0
            Pel p[4];
840
841
0
            int refMainIndex = deltaInt + 1;
842
843
0
            const TFilterCoeff *ff = &f[deltaFract << 2];
844
845
0
            for( int x = 0; x < width; x++, refMainIndex++ )
846
0
            {
847
0
              p[0] = refMain[refMainIndex - 1];
848
0
              p[1] = refMain[refMainIndex    ];
849
0
              p[2] = refMain[refMainIndex + 1];
850
0
              p[3] = refMain[refMainIndex + 2];
851
852
0
              pDstBuf[y*dstStride + x] = static_cast<Pel>((static_cast<int>(ff[0] * p[0]) + static_cast<int>(ff[1] * p[1]) + static_cast<int>(ff[2] * p[2]) + static_cast<int>(ff[3] * p[3]) + 32) >> 6);
853
854
0
              if( useCubicFilter ) // only cubic filter has negative coefficients and requires clipping
855
0
              {
856
0
                pDstBuf[y*dstStride + x] = ClipPel( pDstBuf[y*dstStride + x], clpRng );
857
0
              }
858
0
            }
859
0
            deltaPos += intraPredAngle;
860
0
          }
861
0
        }
862
863
0
      }
864
0
      else
865
0
      {
866
0
        int deltaPos = intraPredAngle * (1 + multiRefIdx);
867
0
        if ( width >=8 )
868
0
        {
869
0
          IntraPredAngleChroma8(pDstBuf,dstStride,refMain,width,height,deltaPos,intraPredAngle);
870
0
        }
871
0
        else if( width == 4 )
872
0
        {
873
0
          IntraPredAngleChroma4(pDstBuf,dstStride,refMain,width,height,deltaPos,intraPredAngle);
874
0
        }
875
0
        else
876
0
        {
877
0
          IntraPredAngleChroma(pDstBuf,dstStride,refMain,width,height,deltaPos,intraPredAngle);
878
0
        }
879
0
      }
880
881
0
    }
882
0
    else
883
0
    {
884
0
      for (int y = 0, deltaPos = intraPredAngle * (1 + multiRefIdx); y<height; y++, deltaPos += intraPredAngle, pDsty += dstStride)
885
0
      {
886
0
        const int deltaInt   = deltaPos >> 5;
887
        // Just copy the integer samples
888
0
        memcpy(pDsty,refMain  + deltaInt + 1,width*sizeof(Pel));
889
0
      }
890
0
    }
891
892
0
    int angularScale = 0;
893
0
    if( intraPredAngle < 0 )
894
0
    {
895
0
      doPDPC = false;
896
0
    }
897
0
    else if( intraPredAngle > 0 )
898
0
    {
899
0
      const int sideSize = predMode >= DIA_IDX ? pDst.height : pDst.width;
900
0
      const int maxScale = 2;
901
902
0
      angularScale = std::min(maxScale, getLog2(sideSize) - (getLog2(3 * invAngle - 2) - 8));
903
0
      doPDPC &= angularScale >= 0;
904
0
    }
905
906
0
    if( doPDPC )
907
0
    {
908
0
      IntraPredAngPdpc( pDstBuf, dstStride, refSide, width, height, invAngle, angularScale );
909
0
    }
910
0
  }
911
912
  // Flip the block if this is the horizontal mode
913
0
  if( !bIsModeVer )
914
0
  {
915
0
    pDst.transposedFrom( CPelBuf( pDstBuf, dstStride, width, height ) );
916
0
  }
917
0
}
918
919
void IntraPrediction::xPredIntraBDPCM(const CPelBuf &pSrc, PelBuf &pDst, const uint32_t dirMode, const ClpRng& clpRng )
920
0
{
921
0
  const int wdt = pDst.width;
922
0
  const int hgt = pDst.height;
923
924
0
  const ptrdiff_t strideP = pDst.stride;
925
0
  const ptrdiff_t strideS = pSrc.stride;
926
927
0
  CHECK( !( dirMode == 1 || dirMode == 2 ), "Incorrect BDPCM mode parameter." );
928
929
0
  Pel* pred = &pDst.buf[0];
930
0
  if( dirMode == 1 )
931
0
  {
932
0
    Pel  val;
933
0
    for( int y = 0; y < hgt; y++ )
934
0
    {
935
0
      val = pSrc.buf[(y + 1) * strideS];
936
0
      for( int x = 0; x < wdt; x++ )
937
0
      {
938
0
        pred[x] = val;
939
0
      }
940
0
      pred += strideP;
941
0
    }
942
0
  }
943
0
  else
944
0
  {
945
0
    for( int y = 0; y < hgt; y++ )
946
0
    {
947
0
      for( int x = 0; x < wdt; x++ )
948
0
      {
949
0
        pred[x] = pSrc.buf[x + 1];
950
0
      }
951
0
      pred += strideP;
952
0
    }
953
0
  }
954
0
}
955
956
void IntraPrediction::predBlendIntraCiip( PelUnitBuf &predUnit, const CodingUnit &cu )
957
0
{
958
0
  int maxCompID = 1;
959
960
0
  if( isChromaEnabled( cu.chromaFormat ) && cu.chromaSize().width > 2 )
961
0
  {
962
0
    maxCompID = MAX_NUM_COMPONENT;
963
0
  }
964
965
0
  for( int currCompID = 0; currCompID < maxCompID; currCompID++ )
966
0
  {
967
0
    PelBuf&              pred   = predUnit.bufs[ currCompID ];
968
0
    const int            width  = pred.width;
969
0
    const int            height = pred.height;
970
0
    const ptrdiff_t      srcStride = width;
971
0
    const ptrdiff_t      dstStride = pred.stride;
972
0
    Pel*                 dstBuf    = pred.buf;
973
0
    const bool           isUseFilter = currCompID == 0 && IntraPrediction::useFilteredIntraRefSamples( COMPONENT_Y, cu, cu );
974
0
    Pel*                 srcBuf    = m_piYuvExt[!isUseFilter];
975
0
    PelBuf               srcAreaBuf( srcBuf, srcStride, width, height );
976
977
0
    {
978
0
      PROFILER_SCOPE_AND_STAGE_EXT( 1, g_timeProfiler, P_INTRAPRED, *cu.cs, compID );
979
980
0
      initIntraPatternChType( cu.firstTU, cu.blocks[currCompID], isUseFilter );
981
0
      predIntraAng( ComponentID( currCompID ), srcAreaBuf, cu, isUseFilter );
982
0
    }
983
984
0
    CHECKD( width == 2, "Width of 2 is not supported" );
985
986
0
    const Position posBL = cu.Y().bottomLeft();
987
0
    const Position posTR = cu.Y().topRight();
988
989
0
    const CodingUnit* cuLeft  = cu.cs->getCURestricted( posBL.offset( -1, 0 ), cu, CHANNEL_TYPE_LUMA, cu.left );
990
0
    const CodingUnit* cuAbove = cu.cs->getCURestricted( posTR.offset( 0, -1 ), cu, CHANNEL_TYPE_LUMA, cu.above );
991
992
0
    const bool isNeigh0Intra = cuLeft  && ( CU::isIntra( *cuLeft ) );
993
0
    const bool isNeigh1Intra = cuAbove && ( CU::isIntra( *cuAbove ) );
994
995
0
    const int wIntra = 3 - !isNeigh0Intra - !isNeigh1Intra;
996
0
    const int wMerge = 3 - !!isNeigh0Intra - !!isNeigh1Intra;
997
998
0
    CiipBlend( dstBuf, dstStride, srcBuf, srcStride, width, height, wMerge, wIntra );
999
0
  }
1000
0
}
1001
1002
inline int isAboveAvailable(const TransformUnit &tu, const ChannelType &chType, const Position &posLT,
1003
                            const uint32_t uiNumUnitsInPU, const uint32_t unitWidthLog2);
1004
inline int isLeftAvailable(const TransformUnit &tu, const ChannelType &chType, const Position &posLT,
1005
                           const uint32_t uiNumUnitsInPU, const uint32_t unitHeightLog2);
1006
1007
void IntraPrediction::initIntraPatternChType(const TransformUnit &tu, const CompArea &area, const bool bFilterRefSamples)
1008
0
{
1009
0
  CHECK( area.width == 2, "Width of 2 is not supported" );
1010
0
  const CodingStructure& cs   = *tu.cu->cs;
1011
1012
0
  Pel *refBufUnfiltered   = m_piYuvExt[PRED_BUF_UNFILTERED];
1013
0
  Pel *refBufFiltered     = m_piYuvExt[PRED_BUF_FILTERED];
1014
1015
0
  setReferenceArrayLengths( area );
1016
1017
  // ----- Step 1: unfiltered reference samples -----
1018
0
  xFillReferenceSamples( cs.picture->getRecoBuf( area ), refBufUnfiltered, area, tu );
1019
  // ----- Step 2: filtered reference samples -----
1020
0
  if( bFilterRefSamples )
1021
0
  {
1022
0
    xFilterReferenceSamples( refBufUnfiltered, refBufFiltered, area, *cs.sps , tu.cu->multiRefIdx() );
1023
0
  }
1024
0
}
1025
1026
void IntraPrediction::initIntraPatternChTypeISP(const CodingUnit& cu, const CompArea& area, PelBuf& recBuf)
1027
0
{
1028
0
  const CodingStructure& cs = *cu.cs;
1029
1030
0
  const Position &posLT = area.pos();
1031
0
  bool isLeftAvail  = nullptr != cs.getCURestricted( posLT.offset( -1, 0 ), cu, CH_L, posLT.x == cu.lx() ? cu.left : &cu );
1032
0
  bool isAboveAvail = nullptr != cs.getCURestricted( posLT.offset( 0, -1 ), cu, CH_L, posLT.y == cu.ly() ? cu.left : &cu );
1033
1034
  // ----- Step 1: unfiltered reference samples -----
1035
0
  if( cu.blocks[area.compID()].x == area.x && cu.blocks[area.compID()].y == area.y )
1036
0
  {
1037
0
    Pel* refBufUnfiltered = m_piYuvExt[PRED_BUF_UNFILTERED];
1038
    // With the first subpartition all the CU reference samples are fetched at once in a single call to xFillReferenceSamples
1039
0
    if( cu.ispMode() == HOR_INTRA_SUBPARTITIONS )
1040
0
    {
1041
0
      m_leftRefLength = cu.Y().height << 1;
1042
0
      m_topRefLength  = cu.Y().width + area.width;
1043
0
    }
1044
0
    else //if (cu.ispMode() == VER_INTRA_SUBPARTITIONS)
1045
0
    {
1046
0
      m_leftRefLength = cu.Y().height + area.height;
1047
0
      m_topRefLength  = cu.Y().width << 1;
1048
0
    }
1049
1050
0
    const int srcStride = m_topRefLength + 1;
1051
0
    const int srcHStride = m_leftRefLength + 1;
1052
1053
0
    m_pelBufISP[0] = m_pelBufISPBase[0] = PelBuf(m_piYuvExt[PRED_BUF_UNFILTERED], srcStride, srcHStride);
1054
0
    m_pelBufISP[1] = m_pelBufISPBase[1] = PelBuf(m_piYuvExt[PRED_BUF_FILTERED], srcStride, srcHStride);
1055
1056
0
    xFillReferenceSamples( cs.picture->getRecoBuf( cu.Y() ), refBufUnfiltered, cu.Y(), isLuma( area.compID() ) ? cu.firstTU : *cu.lastTU );
1057
1058
    // After having retrieved all the CU reference samples, the number of reference samples is now adjusted for the current subpartition
1059
0
    m_topRefLength = cu.blocks[area.compID()].width + area.width;
1060
0
    m_leftRefLength = cu.blocks[area.compID()].height + area.height;
1061
0
  }
1062
0
  else
1063
0
  {
1064
    //Now we only need to fetch the newly available reconstructed samples from the previously coded TU
1065
0
    Position tuPos = area;
1066
0
    tuPos.relativeTo(cu.Y());
1067
0
    m_pelBufISP[0] = m_pelBufISPBase[0].subBuf(tuPos, area.size());
1068
0
    m_pelBufISP[1] = m_pelBufISPBase[1].subBuf(tuPos, area.size());
1069
1070
0
    PelBuf& dstBuf = m_pelBufISP[0];
1071
1072
0
    m_topRefLength = cu.blocks[area.compID()].width + area.width;
1073
0
    m_leftRefLength = cu.blocks[area.compID()].height + area.height;
1074
1075
0
    const int predSizeHor = m_topRefLength;
1076
0
    const int predSizeVer = m_leftRefLength;
1077
0
    if (cu.ispMode() == HOR_INTRA_SUBPARTITIONS)
1078
0
    {
1079
0
      Pel* src = recBuf.bufAt(0, -1);
1080
0
      Pel* dst = dstBuf.bufAt(1, 0);
1081
0
      for (int i = 0; i < area.width; i++)
1082
0
      {
1083
0
        dst[i] = src[i];
1084
0
      }
1085
0
      Pel sample = src[area.width - 1];
1086
0
      dst += area.width;
1087
0
      for (int i = 0; i < predSizeHor - area.width; i++)
1088
0
      {
1089
0
        dst[i] = sample;
1090
0
      }
1091
0
      if (!isLeftAvail) //if left is not avaible, then it is necessary to fetch these samples for each subpartition
1092
0
      {
1093
0
        Pel* dst = dstBuf.bufAt(0, 0);
1094
0
        Pel  sample = src[0];
1095
0
        for (int i = 0; i < predSizeVer + 1; i++)
1096
0
        {
1097
0
          *dst = sample;
1098
0
          dst += dstBuf.stride;
1099
0
        }
1100
0
      }
1101
0
    }
1102
0
    else
1103
0
    {
1104
0
      Pel* src = recBuf.bufAt(-1, 0);
1105
0
      Pel* dst = dstBuf.bufAt(0, 1);
1106
0
      for (int i = 0; i < area.height; i++)
1107
0
      {
1108
0
        *dst = *src;
1109
0
        src += recBuf.stride;
1110
0
        dst += dstBuf.stride;
1111
0
      }
1112
0
      Pel sample = src[-recBuf.stride];
1113
0
      for (int i = 0; i < predSizeVer - area.height; i++)
1114
0
      {
1115
0
        *dst = sample;
1116
0
        dst += dstBuf.stride;
1117
0
      }
1118
1119
0
      if (!isAboveAvail) //if above is not avaible, then it is necessary to fetch these samples for each subpartition
1120
0
      {
1121
0
        Pel* dst = dstBuf.bufAt(0, 0);
1122
0
        Pel  sample = recBuf.at(-1, 0);
1123
0
        for (int i = 0; i < predSizeHor + 1; i++)
1124
0
        {
1125
0
          dst[i] = sample;
1126
0
        }
1127
0
      }
1128
0
    }
1129
0
  }
1130
0
}
1131
1132
void IntraPrediction::xFillReferenceSamples( const CPelBuf &recoBuf, Pel* refBufUnfiltered, const CompArea &area, const TransformUnit &tu )
1133
0
{
1134
0
  const ChannelType      chType = toChannelType( area.compID() );
1135
0
  const CodingUnit      &cu     = *tu.cu;
1136
0
  const CodingStructure &cs     = *cu.cs;
1137
0
  const SPS             &sps    = *cs.sps;
1138
0
  const PreCalcValues   &pcv    = *cs.pcv;
1139
1140
0
  const int multiRefIdx         = (area.compID() == COMPONENT_Y) ? cu.multiRefIdx() : 0;
1141
1142
0
  const int  tuWidth            = area.width;
1143
0
  const int  tuHeight           = area.height;
1144
0
  const int  predSize           = m_topRefLength;
1145
0
  const int  predHSize          = m_leftRefLength;
1146
0
  const int  predStride         = predSize + 1 + multiRefIdx;
1147
1148
0
  const int  csx                = getChannelTypeScaleX( chType, pcv.chrFormat );
1149
0
  const int  csy                = getChannelTypeScaleY( chType, pcv.chrFormat );
1150
1151
  // the unit sizes are powers of two, so shifts replace the divisions by them
1152
0
  const int  unitWidthLog2      = pcv.minCUWidthLog2  - csx;
1153
0
  const int  unitHeightLog2     = pcv.minCUHeightLog2 - csy;
1154
0
  const int  unitWidth          = 1 << unitWidthLog2;
1155
0
  const int  unitHeight         = 1 << unitHeightLog2;
1156
1157
0
  const int  totalAboveUnits    = (predSize + (unitWidth - 1)) >> unitWidthLog2;
1158
0
  const int  totalLeftUnits     = (predHSize + (unitHeight - 1)) >> unitHeightLog2;
1159
0
  const int  totalUnits         = totalAboveUnits + totalLeftUnits + 1; //+1 for top-left
1160
0
  const int  numAboveUnits      = tuWidth >> unitWidthLog2;
1161
0
  const int  numLeftUnits       = tuHeight >> unitHeightLog2;
1162
0
  const int  numAboveRightUnits = totalAboveUnits - numAboveUnits;
1163
0
  const int  numLeftBelowUnits  = totalLeftUnits - numLeftUnits;
1164
1165
0
  CHECK( numAboveUnits <= 0 || numLeftUnits <= 0 || numAboveRightUnits <= 0 || numLeftBelowUnits <= 0, "Size not supported" );
1166
1167
  // ----- Step 1: analyze neighborhood -----
1168
0
  if( m_lastCUidx == tu.cu->idx && area.compID() != getFirstComponentOfChannel( tu.cu->chType() ) )
1169
0
  {
1170
0
  }
1171
0
  else
1172
0
  {
1173
0
    const Position posLT = area.pos();
1174
  
1175
0
    const bool sameCTUx  = !!( posLT.x & ( pcv.maxCUWidthMask  >> csx ) );
1176
0
    const bool sameCTUy  = !!( posLT.y & ( pcv.maxCUHeightMask >> csy ) );
1177
0
    const bool sameCTU   = sameCTUx && sameCTUy;
1178
1179
0
    m_neighborSize[0]    = sameCTU ? 1 : !!cu.cs->getCURestricted( posLT.offset( -1, -1 ), cu, chType, cu.left ? cu.left : cu.above );
1180
1181
0
    if( cu.above || area.y > tu.cu->blocks[chType].y )
1182
0
    {
1183
0
      m_neighborSize[1] = numAboveUnits;
1184
1185
0
      Position posAR{ posLT.x + ( PosType ) area.width, posLT.y };
1186
0
      m_neighborSize[1] += isAboveAvailable( tu, chType, posAR, numAboveRightUnits, unitWidthLog2 );
1187
0
    }
1188
0
    else
1189
0
      m_neighborSize[1] = 0;
1190
1191
0
    if( cu.left || area.x > tu.cu->blocks[chType].x )
1192
0
    {
1193
0
      m_neighborSize[2] = numLeftUnits;
1194
1195
0
      Position posLB{ posLT.x, posLT.y + ( PosType ) area.height };
1196
0
      m_neighborSize[2] += isLeftAvailable( tu, chType, posLB, numLeftBelowUnits, unitHeightLog2 );
1197
0
    }
1198
0
    else
1199
0
      m_neighborSize[2] = 0;
1200
1201
0
    m_lastCUidx = tu.cu->idx;
1202
0
  }
1203
1204
0
  int numIntraNeighbor = m_neighborSize[0] + m_neighborSize[1] + m_neighborSize[2];
1205
1206
  // ----- Step 2: fill reference samples (depending on neighborhood) -----
1207
0
  const Pel*  srcBuf    = recoBuf.buf;
1208
0
  const ptrdiff_t srcStride = recoBuf.stride;
1209
0
        Pel*  ptrDst    = refBufUnfiltered;
1210
0
  const Pel*  ptrSrc;
1211
0
  const Pel   valueDC   = 1 << (sps.getBitDepth() - 1);
1212
1213
0
  if( numIntraNeighbor == 0 )
1214
0
  {
1215
    // Fill border with DC value
1216
0
    for (int j = 0; j <= predSize + multiRefIdx; j++) { ptrDst[j] = valueDC; }
1217
0
    for (int i = 1; i <= predHSize + multiRefIdx; i++) { ptrDst[i*predStride] = valueDC; }
1218
0
  }
1219
0
  else if( numIntraNeighbor == totalUnits )
1220
0
  {
1221
    // Fill top-left border and top and top right with rec. samples
1222
0
    ptrSrc = srcBuf - (1 + multiRefIdx) * srcStride - (1 + multiRefIdx);
1223
0
    for (int j = 0; j <= predSize + multiRefIdx; j++) { ptrDst[j] = ptrSrc[j]; }
1224
0
    ptrSrc = srcBuf - multiRefIdx * srcStride - (1 + multiRefIdx);
1225
0
    for (int i = 1; i <= predHSize + multiRefIdx; i++) { ptrDst[i*predStride] = *(ptrSrc); ptrSrc += srcStride; }
1226
0
  }
1227
0
  else // reference samples are partially available
1228
0
  {
1229
    // Fill top-left sample(s) if available
1230
0
    if ( m_neighborSize[2] > 0) {  // left is available
1231
      // Fill left & below-left samples if available (downwards)
1232
0
      ptrSrc = srcBuf - (1 + multiRefIdx);
1233
0
      ptrDst = refBufUnfiltered + (1 + multiRefIdx) * predStride;
1234
0
      int tmpSize = m_neighborSize[2] * unitHeight;
1235
0
      tmpSize = std::min(tmpSize, predHSize);
1236
0
      for (int i = 0; i < tmpSize; i++) {
1237
0
        ptrDst[i * predStride] = ptrSrc[i * srcStride];
1238
0
      }
1239
1240
      // pad
1241
0
      Pel tmpPixel = ptrDst[(tmpSize - 1) * predStride];
1242
0
      for (int i = tmpSize; i < predHSize; i++) {
1243
0
        ptrDst[i * predStride] = tmpPixel;
1244
0
      }
1245
1246
      // Fill top-left sample(s) if available
1247
0
      if ( m_neighborSize[0]) {
1248
0
        ptrSrc = srcBuf - (1 + multiRefIdx) * srcStride - (1 + multiRefIdx);
1249
0
        ptrDst = refBufUnfiltered;
1250
0
        memcpy(ptrDst, ptrSrc, sizeof(Pel) * (multiRefIdx + 1));
1251
0
        for (int i = 1; i <= multiRefIdx; i++) {
1252
0
          ptrDst[i * predStride] = ptrSrc[i * srcStride];
1253
0
        }
1254
0
      } else {                                // pad
1255
0
        ptrSrc = srcBuf - (1 + multiRefIdx);  // left pixel
1256
0
        ptrDst = refBufUnfiltered;
1257
0
        tmpPixel = ptrSrc[0];
1258
0
        ptrDst[0] = tmpPixel;
1259
0
        for (int i = 1; i <= multiRefIdx; i++) {
1260
0
          ptrDst[i] = tmpPixel;
1261
0
          ptrDst[i * predStride] = tmpPixel;
1262
0
        }
1263
0
      }
1264
1265
      // Fill above & above-right samples if available (left-to-right)
1266
0
      if ( m_neighborSize[1]) {
1267
0
        ptrSrc = srcBuf - srcStride * (1 + multiRefIdx);
1268
0
        ptrDst = refBufUnfiltered + 1 + multiRefIdx;
1269
0
        tmpSize = m_neighborSize[1] * unitWidth;
1270
0
        tmpSize = std::min(tmpSize, predSize);
1271
0
        memcpy(ptrDst, ptrSrc, tmpSize * sizeof(Pel));
1272
        // pad
1273
0
        Pel tmpPixel = ptrDst[tmpSize - 1];
1274
0
        for (int i = tmpSize; i < predSize; i++) {
1275
0
          ptrDst[i] = tmpPixel;
1276
0
        }
1277
0
      } else {  // all not available, pad
1278
0
        ptrSrc = srcBuf - srcStride * (1 + multiRefIdx);
1279
0
        ptrDst = refBufUnfiltered + 1 + multiRefIdx;
1280
0
        Pel tmpPixel = ptrDst[-1];
1281
0
        std::fill_n(ptrDst, predSize, tmpPixel);
1282
0
      }
1283
0
    } else {  // left is not available, top must be available
1284
      // Fill above & above-right samples (left-to-right)
1285
0
      ptrSrc = srcBuf - srcStride * (1 + multiRefIdx);
1286
0
      ptrDst = refBufUnfiltered + 1 + multiRefIdx;
1287
0
      int tmpSize = m_neighborSize[1] * unitWidth;
1288
0
      tmpSize = std::min(tmpSize, predSize);
1289
0
      memcpy(ptrDst, ptrSrc, tmpSize * sizeof(Pel));
1290
      // pad
1291
0
      Pel tmpPixel = ptrDst[tmpSize - 1];
1292
0
      for (int i = tmpSize; i < predSize; i++) {
1293
0
        ptrDst[i] = tmpPixel;
1294
0
      }
1295
1296
0
      tmpPixel = ptrSrc[0];
1297
      // pad top-left sample(s)
1298
0
      ptrDst = refBufUnfiltered;
1299
0
      ptrDst[0] = tmpPixel;
1300
0
      for (int i = 1; i <= multiRefIdx; i++) {
1301
0
        ptrDst[i] = tmpPixel;
1302
0
        ptrDst[i * predStride] = tmpPixel;
1303
0
      }
1304
1305
      // pad left sample(s)
1306
0
      ptrDst = refBufUnfiltered + (1 + multiRefIdx) * predStride;
1307
0
      for (int i = 0; i < predHSize; i++) {
1308
0
        ptrDst[i * predStride] = tmpPixel;
1309
0
      }
1310
0
    }
1311
0
  }
1312
0
}
1313
1314
void IntraPrediction::xFilterReferenceSamples( const Pel* refBufUnfiltered, Pel* refBufFiltered, const CompArea &area, const SPS &sps, int multiRefIdx, ptrdiff_t stride ) const
1315
0
{
1316
0
  if (area.compID() != COMPONENT_Y)
1317
0
  {
1318
0
    multiRefIdx = 0;
1319
0
  }
1320
0
  const int       predSize   = m_topRefLength  + multiRefIdx;
1321
0
  const int       predHSize  = m_leftRefLength + multiRefIdx;
1322
0
  const ptrdiff_t predStride = stride == 0 ? predSize + 1 : stride;
1323
1324
1325
1326
  // Regular reference sample filter
1327
0
  const Pel *piSrcPtr  = refBufUnfiltered + (predStride * predHSize); // bottom left
1328
0
        Pel *piDestPtr = refBufFiltered   + (predStride * predHSize); // bottom left
1329
1330
  // bottom left (not filtered)
1331
0
  *piDestPtr = *piSrcPtr;
1332
0
  piDestPtr -= predStride;
1333
0
  piSrcPtr  -= predStride;
1334
  //left column (bottom to top)
1335
0
  for( int i = 1; i < predHSize; i++, piDestPtr -= predStride, piSrcPtr -= predStride)
1336
0
  {
1337
0
    *piDestPtr = (piSrcPtr[predStride] + 2 * piSrcPtr[0] + piSrcPtr[-predStride] + 2) >> 2;
1338
0
  }
1339
  //top-left
1340
0
  *piDestPtr = (piSrcPtr[predStride] + 2 * piSrcPtr[0] + piSrcPtr[1] + 2) >> 2;
1341
0
  piDestPtr++;
1342
0
  piSrcPtr++;
1343
  //top row (left-to-right)
1344
0
  for( uint32_t i=1; i < predSize; i++, piDestPtr++, piSrcPtr++ )
1345
0
  {
1346
0
    *piDestPtr = (piSrcPtr[1] + 2 * piSrcPtr[0] + piSrcPtr[-1] + 2) >> 2;
1347
0
  }
1348
  // top right (not filtered)
1349
0
  *piDestPtr=*piSrcPtr;
1350
0
}
1351
1352
static bool getUseFilterRef( const int predMode )
1353
0
{
1354
0
  static const int angTable[32]    = { 0,    1,    2,    3,    4,    6,     8,   10,   12,   14,   16,   18,   20,   23,   26,   29,   32,   35,   39,  45,  51,  57,  64,  73,  86, 102, 128, 171, 256, 341, 512, 1024 };
1355
1356
0
  const int     intraPredAngleMode = ( predMode >= DIA_IDX ) ? predMode - VER_IDX : -( predMode - HOR_IDX );
1357
1358
0
  const int     absAngMode         = abs(intraPredAngleMode);
1359
0
  const int     absAng             = angTable   [absAngMode];
1360
1361
0
  return 0 == ( absAng & 0x1F );
1362
0
}
1363
1364
bool IntraPrediction::useFilteredIntraRefSamples( const ComponentID &compID, const CodingUnit &cu, const UnitArea &tuArea )
1365
0
{
1366
  //const SPS         &sps    = *cu.sps;
1367
0
  const ChannelType  chType = toChannelType( compID );
1368
1369
  // high level conditions
1370
  //if( sps.getSpsRangeExtension().getIntraSmoothingDisabledFlag() )  { return false; }
1371
  //if( !isLuma( chType ) )                                           { return false; }
1372
  //if( cu.ispMode() && isLuma(compID) )                              { return false; }
1373
  //if( CU::isMIP( cu, chType ) )                                     { return false; }
1374
0
  if( cu.multiRefIdx() )                                            { return false; }
1375
0
  if( cu.bdpcmMode() )                                              { return false; }
1376
1377
  // pred. mode related conditions
1378
0
  const int dirMode = PU::getFinalIntraMode( cu, chType );
1379
0
  if (dirMode == DC_IDX)                                            { return false; }
1380
0
  if (dirMode == PLANAR_IDX)
1381
0
  {
1382
0
    return tuArea.blocks[compID].area() > 32 ? true : false;
1383
0
  }
1384
1385
0
  const int predMode = getWideAngle(tuArea.blocks[compID].width, tuArea.blocks[compID].height, dirMode);
1386
0
  const int diff     = std::min<int>( abs( predMode - HOR_IDX ), abs( predMode - VER_IDX ) );
1387
0
  const int log2Size = ( ( getLog2( tuArea.blocks[compID].width ) + getLog2( tuArea.blocks[compID].height ) ) >> 1 );
1388
0
  CHECKD( log2Size >= MAX_INTRA_FILTER_DEPTHS, "Size not supported" );
1389
0
  return diff > m_aucIntraFilter[ chType ][ log2Size ] && getUseFilterRef( predMode );
1390
0
}
1391
1392
static inline TransformUnit const* getTU( const CodingUnit& cu, const Position& pos, const ChannelType chType )
1393
0
{
1394
0
  const TransformUnit* ptu = &cu.firstTU;
1395
1396
0
  if( !ptu->next ) return ptu;
1397
1398
0
  while( !( ptu->blocks[chType].x + ptu->blocks[chType].width > pos.x && ptu->blocks[chType].y + ptu->blocks[chType].height > pos.y ) )
1399
0
  {
1400
0
    ptu = ptu->next;
1401
0
  }
1402
1403
0
  return ptu;
1404
0
}
1405
1406
int isAboveAvailable(const TransformUnit &tu, const ChannelType &chType, const Position &posLT,
1407
                     const uint32_t uiNumUnitsInPU, const uint32_t unitWidthLog2)
1408
0
{
1409
0
  const CodingUnit      &cu = *tu.cu;
1410
0
  const CodingStructure &cs = *cu.cs;
1411
1412
0
  int maxDx = uiNumUnitsInPU << unitWidthLog2;
1413
0
  Position refPos = posLT.offset(0, -1);
1414
0
  const TransformUnit *pcTUAbove = nullptr;
1415
0
  const int currTUIdx = tu.idx;
1416
0
  int dx = 0;
1417
1418
0
  while( dx < maxDx )
1419
0
  {
1420
0
    const CodingUnit* cuAbove = cs.getCURestricted( refPos, cu, chType, pcTUAbove ? nullptr : cu.above );
1421
1422
0
    if( !cuAbove ) break;
1423
0
    pcTUAbove = getTU( *cuAbove, refPos, chType );
1424
0
    if( cuAbove->ctuData == cu.ctuData && pcTUAbove->idx >= currTUIdx ) break;
1425
1426
0
    int diff  = ( int ) pcTUAbove->blocks[chType].width - refPos.x + pcTUAbove->blocks[chType].x;
1427
0
    dx       += diff;
1428
0
    refPos.x += diff;
1429
0
  }
1430
1431
0
  int neighborSize = dx >> unitWidthLog2;
1432
0
  neighborSize = std::min<int>( neighborSize, uiNumUnitsInPU );
1433
0
  return neighborSize;
1434
0
}
1435
1436
int isLeftAvailable(const TransformUnit &tu, const ChannelType &chType, const Position &posLT,
1437
                    const uint32_t uiNumUnitsInPU, const uint32_t unitHeightLog2)
1438
0
{
1439
0
  const CodingUnit      &cu = *tu.cu;
1440
0
  const CodingStructure &cs = *cu.cs;
1441
1442
0
  int maxDy = uiNumUnitsInPU << unitHeightLog2;
1443
0
  Position refPos = posLT.offset(-1, 0);
1444
0
  const TransformUnit *pcTULeft = nullptr;
1445
0
  int currTUIdx = tu.idx;
1446
0
  int dy = 0;
1447
1448
0
  while( dy < maxDy )
1449
0
  {
1450
0
    const CodingUnit* cuLeft = cs.getCURestricted( refPos, cu, chType, pcTULeft ? nullptr : cu.left );
1451
1452
0
    if( !cuLeft ) break;
1453
0
    pcTULeft = getTU( *cuLeft, refPos, chType );
1454
0
    if( cuLeft->ctuData == cu.ctuData && pcTULeft->idx >= currTUIdx ) break;
1455
1456
0
    int diff  = ( int ) pcTULeft->blocks[chType].height - refPos.y + pcTULeft->blocks[chType].y;
1457
0
    dy       += diff;
1458
0
    refPos.y += diff;
1459
0
  }
1460
1461
0
  int neighborSize = dy >> unitHeightLog2;
1462
0
  neighborSize = std::min<int>( neighborSize, uiNumUnitsInPU );
1463
0
  return neighborSize;
1464
0
}
1465
// LumaRecPixels
1466
NO_THREAD_SANITIZE void IntraPrediction::xGetLumaRecPixels(const CodingUnit &cu, CompArea chromaArea)
1467
0
{
1468
0
  int iDstStride = 0;
1469
0
  Pel* pDst0 = 0;
1470
0
  int curChromaMode = cu.intraDir[1];
1471
0
  if ((curChromaMode == MDLM_L_IDX) || (curChromaMode == MDLM_T_IDX))
1472
0
  {
1473
0
    iDstStride = 2 * MAX_TU_SIZE_FOR_PROFILE + 1;
1474
0
    pDst0      = m_piYuvExt[1] + iDstStride + 1;
1475
0
  }
1476
0
  else
1477
0
  {
1478
0
    iDstStride = MAX_TU_SIZE_FOR_PROFILE + 1;
1479
0
    pDst0      = m_piYuvExt[1] + iDstStride + 1; //MMLM_SAMPLE_NEIGHBOR_LINES;
1480
0
  }
1481
  //assert 420 chroma subsampling
1482
0
  CompArea lumaArea = CompArea( COMPONENT_Y, chromaArea.lumaPos( cu.chromaFormat),
1483
0
                                recalcSize( cu.chromaFormat, CHANNEL_TYPE_CHROMA, CHANNEL_TYPE_LUMA, chromaArea.size() ) );//needed for correct pos/size (4x4 Tus)
1484
1485
1486
0
  CHECK( lumaArea.width  == chromaArea.width  && CHROMA_444 != cu.chromaFormat, "" );
1487
0
  CHECK( lumaArea.height == chromaArea.height && CHROMA_444 != cu.chromaFormat && CHROMA_422 != cu.chromaFormat, "" );
1488
1489
0
  const SizeType uiCWidth = chromaArea.width;
1490
0
  const SizeType uiCHeight = chromaArea.height;
1491
1492
0
  CPelBuf Src = cu.cs->picture->getRecoBuf( lumaArea );
1493
0
  Pel const* pRecSrc0   = Src.bufAt( 0, 0 );
1494
0
  ptrdiff_t iRecStride  = Src.stride;
1495
0
  int logSubWidthC  = getChannelTypeScaleX(CHANNEL_TYPE_CHROMA, cu.chromaFormat);
1496
0
  int logSubHeightC = getChannelTypeScaleY(CHANNEL_TYPE_CHROMA, cu.chromaFormat);
1497
1498
1499
0
  ptrdiff_t iRecStride2 = iRecStride << logSubHeightC;    // TODO: really Height here? not Width?
1500
0
  const int mult        =          1 << logSubWidthC ;
1501
1502
0
  const CompArea& area = isChroma( cu.chType() ) ? chromaArea : lumaArea;
1503
1504
0
  const uint32_t uiTuWidth  = area.width;
1505
0
  const uint32_t uiTuHeight = area.height;
1506
1507
0
  int iBaseUnitSize = ( 1 << MIN_CU_LOG2 );
1508
1509
0
  const int  iUnitWidth       = iBaseUnitSize >> getComponentScaleX( area.compID(), cu.chromaFormat );
1510
0
  const int  iUnitHeight      = iBaseUnitSize >> getComponentScaleY( area.compID(), cu.chromaFormat );
1511
0
  const int  iTUWidthInUnits  = uiTuWidth  / iUnitWidth;
1512
0
  const int  iTUHeightInUnits = uiTuHeight / iUnitHeight;
1513
0
  const int  iAboveUnits      = iTUWidthInUnits;
1514
0
  const int  iLeftUnits       = iTUHeightInUnits;
1515
0
  const int  chromaUnitWidth  = iBaseUnitSize >> getComponentScaleX(COMPONENT_Cb, cu.chromaFormat);
1516
0
  const int  chromaUnitHeight = iBaseUnitSize >> getComponentScaleY(COMPONENT_Cb, cu.chromaFormat);
1517
0
  const int  topTemplateSampNum  = 2 * uiCWidth; // for MDLM, the number of template samples is 2W or 2H.
1518
0
  const int  leftTemplateSampNum = 2 * uiCHeight;
1519
0
  CHECKD( !( m_topRefLength >= topTemplateSampNum ), "Error!" );
1520
0
  CHECKD( !( m_leftRefLength >= leftTemplateSampNum ), "Error!" );
1521
0
  int totalAboveUnits = (curChromaMode == MDLM_T_IDX) ? (topTemplateSampNum + (chromaUnitWidth - 1)) / chromaUnitWidth : iAboveUnits;
1522
0
  int totalLeftUnits = (curChromaMode == MDLM_L_IDX) ? (leftTemplateSampNum + (chromaUnitHeight - 1)) / chromaUnitHeight : iLeftUnits;
1523
1524
0
  const int  availlableLeftUnit = ( cu.left || chromaArea.x > cu.blocks[CH_C].x ) ? totalLeftUnits : 0;
1525
0
  const bool bLeftAvaillable    = availlableLeftUnit >= iTUHeightInUnits;
1526
  
1527
0
  const int  availlableAboveUnit = ( cu.above || chromaArea.y > cu.blocks[CH_C].y ) ? totalAboveUnits : 0;
1528
0
  const bool bAboveAvaillable    = availlableAboveUnit >= iTUWidthInUnits;
1529
1530
0
  Pel*       pDst  = nullptr;
1531
0
  Pel const* piSrc = nullptr;
1532
1533
0
  bool isFirstRowOfCtu            = ( lumaArea.y & ( cu.sps->getCTUSize() - 1) ) == 0;
1534
0
  const ptrdiff_t strOffset       = ( CHROMA_444 == cu.chromaFormat ) ? 0 : iRecStride;
1535
1536
0
  int c0_3tap = 2, c1_3tap = 1, c2_3tap = 1,                                        offset_3tap = 2, shift_3tap = 2; //sum = 4
1537
0
  int c0_5tap = 1, c1_5tap = 4, c2_5tap = 1, c3_5tap = 1, c4_5tap = 1,              offset_5tap = 4, shift_5tap = 3; //sum = 8
1538
0
  int c0_6tap = 2, c1_6tap = 1, c2_6tap = 1, c3_6tap = 2, c4_6tap = 1, c5_6tap = 1, offset_6tap = 4, shift_6tap = 3; //sum = 8
1539
1540
0
  switch (cu.chromaFormat)
1541
0
  {
1542
0
    case CHROMA_422: //overwrite filter coefficient values for 422
1543
0
      c0_3tap = 2, c1_3tap = 1, c2_3tap = 1,                                        offset_3tap = 2, shift_3tap = 2; //sum = 4
1544
0
      c0_5tap = 0, c1_5tap = 2, c2_5tap = 1, c3_5tap = 1, c4_5tap = 0,              offset_5tap = 2, shift_5tap = 2; //sum = 4
1545
0
      c0_6tap = 2, c1_6tap = 1, c2_6tap = 1, c3_6tap = 0, c4_6tap = 0, c5_6tap = 0, offset_6tap = 2, shift_6tap = 2; //sum = 4
1546
0
      break;
1547
1548
0
    case CHROMA_444:  //overwrite filter coefficient values for 444
1549
0
      c0_3tap = 1, c1_3tap = 0, c2_3tap = 0,                                        offset_3tap = 0, shift_3tap = 0; //sum = 1
1550
0
      c0_5tap = 0, c1_5tap = 1, c2_5tap = 0, c3_5tap = 0, c4_5tap = 0,              offset_5tap = 0, shift_5tap = 0; //sum = 1
1551
0
      c0_6tap = 1, c1_6tap = 0, c2_6tap = 0, c3_6tap = 0, c4_6tap = 0, c5_6tap = 0, offset_6tap = 0, shift_6tap = 0; //sum = 1
1552
0
      break;
1553
1554
0
    default:
1555
0
      break;
1556
0
  }
1557
1558
0
  const bool collocated = cu.sps->getCclmCollocatedChromaFlag();
1559
1560
0
  if( bAboveAvaillable && cu.chromaFormat == CHROMA_420 && !isFirstRowOfCtu )
1561
0
  {
1562
    // The template row above the block is downsampled like one more row of the block, by the same kernels. Without the
1563
    // left neighbour the first sample is computed separately, so that the kernels do not read left of the block.
1564
0
    pDst  = pDst0    - iDstStride;
1565
0
    piSrc = pRecSrc0 - iRecStride2;
1566
0
    const int avaiAboveSizes = availlableAboveUnit * chromaUnitWidth;
1567
0
    const int firstX         = bLeftAvaillable ? 0 : 1;
1568
0
    if( collocated )
1569
0
    {
1570
0
      GetLumaRecPixel420Collocated( avaiAboveSizes - firstX, 1, piSrc + 2 * firstX, iRecStride, pDst + firstX, iDstStride );
1571
0
    }
1572
0
    else
1573
0
    {
1574
      // GetLumaRecPixel420 takes widths that are a multiple of 8 or at most 4
1575
0
      int x = firstX;
1576
0
      for( const int step : { 8, 4, 2, 1 } )
1577
0
      {
1578
0
        const int width = ( avaiAboveSizes - x ) & ~( step - 1 );
1579
0
        if( width )
1580
0
        {
1581
0
          GetLumaRecPixel420( width, 1, piSrc + 2 * x, iRecStride, pDst + x, iDstStride );
1582
0
          x += width;
1583
0
        }
1584
0
      }
1585
0
    }
1586
1587
0
    if( !bLeftAvaillable )
1588
0
    {
1589
0
      pDst[0] = collocated ? ( piSrc[-strOffset] * c0_5tap + piSrc[0] * c1_5tap + piSrc[0] * c2_5tap + piSrc[1] * c3_5tap + piSrc[strOffset] * c4_5tap + offset_5tap ) >> shift_5tap
1590
0
                           : ( ( piSrc[0]             * c0_6tap + piSrc[0]             * c1_6tap + piSrc[1]             * c2_6tap )
1591
0
                             + ( piSrc[0 + strOffset] * c3_6tap + piSrc[0 + strOffset] * c4_6tap + piSrc[1 + strOffset] * c5_6tap )
1592
0
                             + offset_6tap ) >> shift_6tap;
1593
0
    }
1594
0
  }
1595
0
  else if( bAboveAvaillable )
1596
0
  {
1597
0
    pDst  = pDst0    - iDstStride;
1598
0
    int avaiAboveSizes = availlableAboveUnit * chromaUnitWidth;
1599
0
    for (int i = 0; i < avaiAboveSizes; i++)
1600
0
    {
1601
0
      if (isFirstRowOfCtu)
1602
0
      {
1603
0
        piSrc = pRecSrc0 - iRecStride;
1604
1605
0
        if ((i == 0 && !bLeftAvaillable) || (i == uiCWidth + avaiAboveSizes - 1 + logSubWidthC))
1606
0
        {
1607
0
          pDst[i] = (piSrc[mult * i] * c0_3tap + piSrc[mult * i] * c1_3tap + piSrc[mult * i + 1] * c2_3tap + offset_3tap) >> shift_3tap;
1608
0
        }
1609
0
        else
1610
0
        {
1611
0
          pDst[i] = (piSrc[mult * i] * c0_3tap + piSrc[mult * i - 1] * c1_3tap + piSrc[mult * i + 1] * c2_3tap + offset_3tap) >> shift_3tap;
1612
0
        }
1613
0
      }
1614
0
      else if( cu.sps->getCclmCollocatedChromaFlag() )
1615
0
      {
1616
0
        piSrc = pRecSrc0 - iRecStride2;
1617
1618
0
        if ((i == 0 && !bLeftAvaillable) || (i == uiCWidth + avaiAboveSizes - 1 + logSubWidthC))
1619
0
        {
1620
0
          pDst[i] = (piSrc[mult * i - strOffset] * c0_5tap
1621
0
                  +  piSrc[mult * i]             * c1_5tap + piSrc[mult * i] * c2_5tap + piSrc[mult * i + 1] * c3_5tap
1622
0
                  +  piSrc[mult * i + strOffset] * c4_5tap
1623
0
                  +  offset_5tap) >> shift_5tap;
1624
0
        }
1625
0
        else
1626
0
        {
1627
0
          pDst[i] = (piSrc[mult * i - strOffset] * c0_5tap
1628
0
                  +  piSrc[mult * i]             * c1_5tap + piSrc[mult * i - 1] * c2_5tap + piSrc[mult * i + 1] * c3_5tap
1629
0
                  +  piSrc[mult * i + strOffset] * c4_5tap
1630
0
                  +  offset_5tap) >> shift_5tap;
1631
0
        }
1632
0
      }
1633
0
      else
1634
0
      {
1635
0
        piSrc = pRecSrc0 - iRecStride2;
1636
1637
0
        if ((i == 0 && !bLeftAvaillable) || (i == uiCWidth + avaiAboveSizes - 1 + logSubWidthC))
1638
0
        {
1639
0
          pDst[i] = ((piSrc[mult * i]            * c0_6tap + piSrc[mult * i]             * c1_6tap + piSrc[mult * i + 1]             * c2_6tap)
1640
0
                  + (piSrc[mult * i + strOffset] * c3_6tap + piSrc[mult * i + strOffset] * c4_6tap + piSrc[mult * i + 1 + strOffset] * c5_6tap)
1641
0
                  + offset_6tap) >> shift_6tap;
1642
0
        }
1643
0
        else
1644
0
        {
1645
0
          pDst[i] = ((piSrc[mult * i]            * c0_6tap + piSrc[mult * i - 1]             * c1_6tap + piSrc[mult * i + 1]             * c2_6tap)
1646
0
                  + (piSrc[mult * i + strOffset] * c3_6tap + piSrc[mult * i - 1 + strOffset] * c4_6tap + piSrc[mult * i + 1 + strOffset] * c5_6tap)
1647
0
                  + offset_6tap) >> shift_6tap;
1648
0
        }
1649
0
      }
1650
0
    }
1651
0
  }
1652
1653
0
  if( bLeftAvaillable && cu.chromaFormat == CHROMA_420 )
1654
0
  {
1655
    // The template column left of the block. Without the above neighbour the collocated filter computes the first sample
1656
    // separately, so that the kernel does not read above the block.
1657
0
    pDst  = pDst0    - 1;
1658
0
    piSrc = pRecSrc0 - 2 - logSubWidthC;
1659
1660
0
    const int firstY = collocated && !bAboveAvaillable ? 1 : 0;
1661
0
    GetLumaRecPixel420Left( availlableLeftUnit * chromaUnitHeight - firstY, pRecSrc0 + firstY * iRecStride2, iRecStride, pDst + firstY * iDstStride, iDstStride, collocated );
1662
1663
0
    if( firstY )
1664
0
    {
1665
0
      pDst[0] = ( piSrc[1            ] * c0_5tap
1666
0
                + piSrc[1            ] * c1_5tap + piSrc[0] * c2_5tap + piSrc[2] * c3_5tap
1667
0
                + piSrc[1 + strOffset] * c4_5tap
1668
0
                + offset_5tap ) >> shift_5tap;
1669
0
    }
1670
0
  }
1671
0
  else if( bLeftAvaillable )
1672
0
  {
1673
0
    pDst  = pDst0    - 1;
1674
1675
0
    piSrc = pRecSrc0 - 2 - logSubWidthC;
1676
1677
0
    int availlableLeftSizes = availlableLeftUnit * chromaUnitHeight;
1678
0
    for (int j = 0; j < availlableLeftSizes; j++)
1679
0
    {
1680
0
      if( cu.sps->getCclmCollocatedChromaFlag() )
1681
0
      {
1682
0
        if ((j == 0 && !bAboveAvaillable) || (j == uiCHeight + availlableLeftSizes - 1 + logSubWidthC))
1683
0
        {
1684
0
          pDst[0] = ( piSrc[1            ] * c0_5tap
1685
0
                    + piSrc[1            ] * c1_5tap + piSrc[0] * c2_5tap + piSrc[2] * c3_5tap
1686
0
                    + piSrc[1 + strOffset] * c4_5tap
1687
0
                    + offset_5tap ) >> shift_5tap;
1688
0
        }
1689
0
        else
1690
0
        {
1691
0
          pDst[0] = ( piSrc[1 - strOffset] * c0_5tap
1692
0
                    + piSrc[1            ] * c1_5tap + piSrc[0] * c2_5tap + piSrc[2] * c3_5tap
1693
0
                    + piSrc[1 + strOffset] * c4_5tap
1694
0
                    + offset_5tap ) >> shift_5tap;
1695
0
        }
1696
0
      }
1697
0
      else
1698
0
      {
1699
0
        pDst[0] = ((piSrc[1]             * c0_6tap + piSrc[0]         * c1_6tap + piSrc[2]             * c2_6tap)
1700
0
                +  (piSrc[1 + strOffset] * c3_6tap + piSrc[strOffset] * c4_6tap + piSrc[2 + strOffset] * c5_6tap)
1701
0
                +   offset_6tap) >> shift_6tap;
1702
0
      }
1703
1704
0
      piSrc += iRecStride2;
1705
0
      pDst  += iDstStride;
1706
0
    }
1707
0
  }
1708
1709
0
  if( cu.sps->getCclmCollocatedChromaFlag() )
1710
0
  {
1711
    // The 4:2:0 kernel reads the luma samples left of and above the block, so blocks at an unavailable edge keep the
1712
    // generic loop below.
1713
0
    if( cu.chromaFormat == CHROMA_420 && bLeftAvaillable && bAboveAvaillable )
1714
0
    {
1715
0
      GetLumaRecPixel420Collocated( uiCWidth, uiCHeight, pRecSrc0, iRecStride, pDst0, iDstStride );
1716
0
      return;
1717
0
    }
1718
1719
    // TODO: unroll loop
1720
0
    for( int j = 0; j < uiCHeight; j++ )
1721
0
    {
1722
0
      for( int i = 0; i < uiCWidth; i++ )
1723
0
      {
1724
0
        if( i == 0 && !bLeftAvaillable )
1725
0
        {
1726
0
          if( j == 0 && !bAboveAvaillable )
1727
0
          {
1728
0
            pDst0[i] = (pRecSrc0[mult * i] * c0_5tap
1729
0
                     +  pRecSrc0[mult * i] * c1_5tap + pRecSrc0[mult * i] * c2_5tap + pRecSrc0[mult * i + 1] * c3_5tap
1730
0
                     +  pRecSrc0[mult * i + strOffset] * c4_5tap
1731
0
                     +  offset_5tap) >> shift_5tap;
1732
0
          }
1733
0
          else
1734
0
          {
1735
0
            pDst0[i] = (pRecSrc0[mult * i - strOffset] * c0_5tap
1736
0
                     +  pRecSrc0[mult * i] * c1_5tap + pRecSrc0[mult * i] * c2_5tap + pRecSrc0[mult * i + 1] * c3_5tap
1737
0
                     +  pRecSrc0[mult * i + strOffset] * c4_5tap
1738
0
                     +  offset_5tap) >> shift_5tap;
1739
0
          }
1740
0
        }
1741
0
        else if( j == 0 && !bAboveAvaillable )
1742
0
        {
1743
0
          pDst0[i] = (pRecSrc0[mult * i] * c0_5tap
1744
0
                   +  pRecSrc0[mult * i] * c1_5tap + pRecSrc0[mult * i - 1] * c2_5tap + pRecSrc0[mult * i + 1] * c3_5tap
1745
0
                   +  pRecSrc0[mult * i + strOffset] * c4_5tap
1746
0
                   +  offset_5tap) >> shift_5tap;
1747
0
        }
1748
0
        else
1749
0
        {
1750
0
          pDst0[i] = (pRecSrc0[mult * i - strOffset] * c0_5tap
1751
0
                   +  pRecSrc0[mult * i]             * c1_5tap + pRecSrc0[mult * i - 1] * c2_5tap + pRecSrc0[mult * i + 1] * c3_5tap
1752
0
                   +  pRecSrc0[mult * i + strOffset] * c4_5tap
1753
0
                   +  offset_5tap) >> shift_5tap;
1754
0
        }
1755
0
      }
1756
0
      pDst0    += iDstStride;
1757
0
      pRecSrc0 += iRecStride2;
1758
0
    }
1759
0
    return;
1760
0
  }
1761
1762
1763
1764
1765
0
#define GET_LUMA_REC_PIX_INC   \
1766
0
  pDst0 += iDstStride;         \
1767
0
  pRecSrc0 += iRecStride2
1768
1769
0
#define GET_LUMA_REC_PIX_OP2(ADDR)                                                    \
1770
0
  pDst0[ADDR] = (   pRecSrc0[( (ADDR) << logSubWidthC )    ]              * c0_6tap   \
1771
0
                  + pRecSrc0[( (ADDR) << logSubWidthC ) + 1]              * c1_6tap   \
1772
0
                  + pRecSrc0[( (ADDR) << logSubWidthC ) - 1]              * c2_6tap   \
1773
0
                  + pRecSrc0[( (ADDR) << logSubWidthC )     + iRecStride] * c3_6tap   \
1774
0
                  + pRecSrc0[( (ADDR) << logSubWidthC ) + 1 + iRecStride] * c4_6tap   \
1775
0
                  + pRecSrc0[( (ADDR) << logSubWidthC ) - 1 + iRecStride] * c5_6tap   \
1776
0
                  + offset_6tap ) >> shift_6tap
1777
1778
0
#define GET_LUMA_REC_PIX_OP1(ADDR)                          \
1779
0
  if( !(ADDR) )                                             \
1780
0
  {                                                         \
1781
0
    pDst0[0] = (   pRecSrc0[0    ]              * c0_6tap   \
1782
0
                 + pRecSrc0[0 + 1]              * c1_6tap   \
1783
0
                 + pRecSrc0[0]                  * c2_6tap   \
1784
0
                 + pRecSrc0[0     + iRecStride] * c3_6tap   \
1785
0
                 + pRecSrc0[0 + 1 + iRecStride] * c4_6tap   \
1786
0
                 + pRecSrc0[0     + iRecStride] * c5_6tap   \
1787
0
                 + offset_6tap ) >> shift_6tap;             \
1788
0
  }                                                         \
1789
0
  else                                                      \
1790
0
  {                                                         \
1791
0
    GET_LUMA_REC_PIX_OP2(ADDR);                             \
1792
0
  }
1793
1794
0
  int width  = uiCWidth;
1795
0
  int height = uiCHeight;
1796
1797
0
  if( bLeftAvaillable )
1798
0
  {
1799
0
    if( cu.chromaFormat == CHROMA_420 )
1800
0
    {
1801
0
      GetLumaRecPixel420( width, height, pRecSrc0, iRecStride, pDst0, iDstStride );
1802
      //      SIZE_AWARE_PER_EL_OP( GET_LUMA_REC_PIX_OP2, GET_LUMA_REC_PIX_INC );
1803
0
    }
1804
0
    else  //TODO add SIMD for 422,444
1805
0
    {
1806
0
      SIZE_AWARE_PER_EL_OP( GET_LUMA_REC_PIX_OP2, GET_LUMA_REC_PIX_INC );
1807
0
    }
1808
0
  }
1809
0
  else
1810
0
  {
1811
0
    SIZE_AWARE_PER_EL_OP( GET_LUMA_REC_PIX_OP1, GET_LUMA_REC_PIX_INC );
1812
0
  }
1813
0
}
1814
1815
#undef GET_LUMA_REC_PIX_INC
1816
#undef GET_LUMA_REC_PIX_OP1
1817
#undef GET_LUMA_REC_PIX_OP2
1818
#undef SIZE_AWARE_PER_EL_OP
1819
1820
void IntraPrediction::xGetLMParameters(const CodingUnit &cu, const ComponentID compID,
1821
                                              const CompArea &chromaArea,
1822
                                              int &a, int &b, int &iShift)
1823
0
{
1824
0
  CHECK(compID == COMPONENT_Y, "");
1825
1826
0
  const SizeType cWidth  = chromaArea.width;
1827
0
  const SizeType cHeight = chromaArea.height;
1828
1829
0
  const Position posLT = chromaArea;
1830
1831
0
  const CodingStructure &cs = *cu.cs;
1832
1833
0
  const SPS &        sps           = *cs.sps;
1834
0
  const uint32_t     tuWidth     = chromaArea.width;
1835
0
  const uint32_t     tuHeight    = chromaArea.height;
1836
0
  const ChromaFormat nChromaFormat = sps.getChromaFormatIdc();
1837
1838
0
  const int baseUnitSize   = 1 << MIN_CU_LOG2;
1839
0
  const int unitWidthLog2  = MIN_CU_LOG2 - getComponentScaleX(chromaArea.compID(), nChromaFormat);
1840
0
  const int unitHeightLog2 = MIN_CU_LOG2 - getComponentScaleX(chromaArea.compID(), nChromaFormat);
1841
0
  const int unitWidth      = baseUnitSize >> getComponentScaleX(chromaArea.compID(), nChromaFormat);
1842
0
  const int unitHeight     = baseUnitSize >> getComponentScaleX(chromaArea.compID(), nChromaFormat);
1843
1844
0
  const int tuWidthInUnits  = tuWidth / unitWidth;
1845
0
  const int tuHeightInUnits = tuHeight / unitHeight;
1846
0
  const int aboveUnits      = tuWidthInUnits;
1847
0
  const int leftUnits       = tuHeightInUnits;
1848
0
  int topTemplateSampNum = 2 * cWidth; // for MDLM, the template sample number is 2W or 2H;
1849
0
  int leftTemplateSampNum = 2 * cHeight;
1850
0
  CHECKD( !(m_topRefLength >= topTemplateSampNum),   "Error!" );
1851
0
  CHECKD( !(m_leftRefLength >= leftTemplateSampNum), "Error!" );
1852
0
  int totalAboveUnits = (topTemplateSampNum + (unitWidth - 1)) / unitWidth;
1853
0
  int totalLeftUnits = (leftTemplateSampNum + (unitHeight - 1)) / unitHeight;
1854
0
  int aboveRightUnits = totalAboveUnits - aboveUnits;
1855
0
  int leftBelowUnits = totalLeftUnits - leftUnits;
1856
1857
0
  int curChromaMode = cu.intraDir[1];
1858
0
  bool aboveAvailable = 0, leftAvailable = 0;
1859
1860
0
  const TransformUnit& tu = *getTU( cu, chromaArea.pos(), CH_C );
1861
1862
0
  Pel *srcColor0, *curChroma0;
1863
0
  int  srcStride, curStride;
1864
1865
0
  PelBuf temp;
1866
0
  if ((curChromaMode == MDLM_L_IDX) || (curChromaMode == MDLM_T_IDX))
1867
0
  {
1868
0
    srcStride = 2 * MAX_TU_SIZE_FOR_PROFILE + 1;
1869
0
    temp      = PelBuf(m_piYuvExt[1] + srcStride + 1, srcStride, Size(chromaArea));
1870
0
  }
1871
0
  else
1872
0
  {
1873
0
    srcStride = MAX_TU_SIZE_FOR_PROFILE + 1;
1874
0
    temp      = PelBuf(m_piYuvExt[1] + srcStride + 1, srcStride, Size(chromaArea));
1875
0
  }
1876
0
  srcColor0 = temp.bufAt(0, 0);
1877
0
  curChroma0 = getPredictorPtr(compID);
1878
1879
0
  curStride = m_topRefLength + 1;
1880
1881
0
  curChroma0 += curStride + 1;
1882
1883
0
  unsigned internalBitDepth = sps.getBitDepth();
1884
1885
0
  int minLuma[2] = {  MAX_INT, 0 };
1886
0
  int maxLuma[2] = { -MAX_INT, 0 };
1887
1888
0
  Pel *src = srcColor0 - srcStride;
1889
0
  Pel *cur = curChroma0 - curStride;
1890
0
  int actualTopTemplateSampNum = 0;
1891
0
  int actualLeftTemplateSampNum = 0;
1892
0
  if( curChromaMode == MDLM_T_IDX )
1893
0
  {
1894
0
    int avaiAboveUnits = 0;
1895
1896
0
    if( tu.cu->above || chromaArea.y > tu.cu->blocks[CH_C].y )
1897
0
    {
1898
0
      avaiAboveUnits  = aboveUnits;
1899
0
      aboveRightUnits = aboveRightUnits > ( cHeight / unitWidth ) ? cHeight / unitWidth : aboveRightUnits;
1900
0
      avaiAboveUnits += isAboveAvailable( tu, CHANNEL_TYPE_CHROMA, { posLT.x + ( PosType ) cWidth, posLT.y }, aboveRightUnits, unitWidthLog2 );
1901
0
    }
1902
1903
0
    aboveAvailable           = avaiAboveUnits >= tuWidthInUnits;
1904
0
    actualTopTemplateSampNum = unitWidth * avaiAboveUnits;
1905
0
  }
1906
0
  else if( curChromaMode == MDLM_L_IDX )
1907
0
  {
1908
0
    int avaiLeftUnits = 0;
1909
    
1910
0
    if( tu.cu->left || chromaArea.x > tu.cu->blocks[CH_C].x )
1911
0
    {
1912
0
      avaiLeftUnits  = leftUnits;
1913
0
      leftBelowUnits = leftBelowUnits > ( cWidth / unitHeight ) ? cWidth / unitHeight : leftBelowUnits;
1914
0
      avaiLeftUnits += isLeftAvailable( tu, CHANNEL_TYPE_CHROMA, { posLT.x, posLT.y + ( PosType ) cHeight }, leftBelowUnits, unitHeightLog2 );
1915
0
    }
1916
    
1917
0
    leftAvailable             = avaiLeftUnits >= tuHeightInUnits;
1918
0
    actualLeftTemplateSampNum = unitHeight * avaiLeftUnits;
1919
0
  }
1920
0
  else if( curChromaMode == LM_CHROMA_IDX )
1921
0
  {
1922
0
    aboveAvailable = tu.cu->above || chromaArea.y > tu.cu->blocks[CH_C].y;
1923
0
    leftAvailable  = tu.cu->left || chromaArea.x > tu.cu->blocks[CH_C].x;
1924
0
    actualTopTemplateSampNum =  cWidth;
1925
0
    actualLeftTemplateSampNum = cHeight;
1926
0
  }
1927
0
  int startPos[2]; //0:Above, 1: Left
1928
0
  int pickStep[2];
1929
1930
0
  int aboveIs4 = leftAvailable  ? 0 : 1;
1931
0
  int leftIs4 =  aboveAvailable ? 0 : 1;
1932
1933
0
  startPos[0] = actualTopTemplateSampNum >> (2 + aboveIs4);
1934
0
  pickStep[0] = std::max(1, actualTopTemplateSampNum >> (1 + aboveIs4));
1935
1936
0
  startPos[1] = actualLeftTemplateSampNum >> (2 + leftIs4);
1937
0
  pickStep[1] = std::max(1, actualLeftTemplateSampNum >> (1 + leftIs4));
1938
1939
0
  Pel selectLumaPix[4] = { 0, 0, 0, 0 };
1940
0
  Pel selectChromaPix[4] = { 0, 0, 0, 0 };
1941
1942
0
  int cntT, cntL;
1943
0
  cntT = cntL = 0;
1944
0
  int cnt = 0;
1945
0
  if (aboveAvailable)
1946
0
  {
1947
0
    cntT = std::min(actualTopTemplateSampNum, (1 + aboveIs4) << 1);
1948
0
    src = srcColor0 - srcStride;
1949
0
    cur = curChroma0 - curStride;
1950
0
    for (int pos = startPos[0]; cnt < cntT; pos += pickStep[0], cnt++)
1951
0
    {
1952
0
      selectLumaPix[cnt] = src[pos];
1953
0
      selectChromaPix[cnt] = cur[pos];
1954
0
    }
1955
0
  }
1956
1957
0
  if (leftAvailable)
1958
0
  {
1959
0
    cntL = std::min(actualLeftTemplateSampNum, ( 1 + leftIs4 ) << 1 );
1960
0
    src = srcColor0 - 1;
1961
0
    cur = curChroma0 - 1;
1962
0
    for (int pos = startPos[1], cnt = 0; cnt < cntL; pos += pickStep[1], cnt++)
1963
0
    {
1964
0
      selectLumaPix[cnt + cntT] = src[pos * srcStride];
1965
0
      selectChromaPix[cnt+ cntT] = cur[pos * curStride];
1966
0
    }
1967
0
  }
1968
0
  cnt = cntL + cntT;
1969
1970
0
  if (cnt == 2)
1971
0
  {
1972
0
    selectLumaPix[3] = selectLumaPix[0]; selectChromaPix[3] = selectChromaPix[0];
1973
0
    selectLumaPix[2] = selectLumaPix[1]; selectChromaPix[2] = selectChromaPix[1];
1974
0
    selectLumaPix[0] = selectLumaPix[1]; selectChromaPix[0] = selectChromaPix[1];
1975
0
    selectLumaPix[1] = selectLumaPix[3]; selectChromaPix[1] = selectChromaPix[3];
1976
0
  }
1977
1978
0
  int minGrpIdx[2] = { 0, 2 };
1979
0
  int maxGrpIdx[2] = { 1, 3 };
1980
0
  int *tmpMinGrp = minGrpIdx;
1981
0
  int *tmpMaxGrp = maxGrpIdx;
1982
0
  if (selectLumaPix[tmpMinGrp[0]] > selectLumaPix[tmpMinGrp[1]]) std::swap(tmpMinGrp[0], tmpMinGrp[1]);
1983
0
  if (selectLumaPix[tmpMaxGrp[0]] > selectLumaPix[tmpMaxGrp[1]]) std::swap(tmpMaxGrp[0], tmpMaxGrp[1]);
1984
0
  if (selectLumaPix[tmpMinGrp[0]] > selectLumaPix[tmpMaxGrp[1]]) std::swap(tmpMinGrp, tmpMaxGrp);       // TODO: really? not std::swap(tmpMinGrp[0], tmpMaxGrp[1]); ?
1985
0
  if (selectLumaPix[tmpMinGrp[1]] > selectLumaPix[tmpMaxGrp[0]]) std::swap(tmpMinGrp[1], tmpMaxGrp[0]);
1986
1987
0
  minLuma[0] = (selectLumaPix  [tmpMinGrp[0]] + selectLumaPix  [tmpMinGrp[1]] + 1) >> 1;
1988
0
  minLuma[1] = (selectChromaPix[tmpMinGrp[0]] + selectChromaPix[tmpMinGrp[1]] + 1) >> 1;
1989
0
  maxLuma[0] = (selectLumaPix  [tmpMaxGrp[0]] + selectLumaPix  [tmpMaxGrp[1]] + 1) >> 1;
1990
0
  maxLuma[1] = (selectChromaPix[tmpMaxGrp[0]] + selectChromaPix[tmpMaxGrp[1]] + 1) >> 1;
1991
1992
0
  if (leftAvailable || aboveAvailable)
1993
0
  {
1994
0
    int diff = maxLuma[0] - minLuma[0];
1995
0
    if (diff > 0)
1996
0
    {
1997
0
      int diffC = maxLuma[1] - minLuma[1];
1998
0
      int x = getLog2( diff );
1999
0
      static const uint8_t DivSigTable[1 << 4] = {
2000
        // 4bit significands - 8 ( MSB is omitted )
2001
0
        0,  7,  6,  5,  5,  4,  4,  3,  3,  2,  2,  1,  1,  1,  1,  0
2002
0
      };
2003
0
      int normDiff = (diff << 4 >> x) & 15;
2004
0
      int v = DivSigTable[normDiff] | 8;
2005
0
      x += normDiff != 0;
2006
2007
0
      int y = diffC == 0 ? 0 : getLog2( abs( diffC ) ) + 1;
2008
0
      int add = 1 << y >> 1;
2009
0
      a = (diffC * v + add) >> y;
2010
0
      iShift = 3 + x - y;
2011
0
      if ( iShift < 1 ) {
2012
0
        iShift = 1;
2013
0
        a = ( (a == 0)? 0: (a < 0)? -15 : 15 );   // a=Sign(a)*15
2014
0
      }
2015
0
      b = minLuma[1] - ((a * minLuma[0]) >> iShift);
2016
0
    }
2017
0
    else
2018
0
    {
2019
0
      a = 0;
2020
0
      b = minLuma[1];
2021
0
      iShift = 0;
2022
0
    }
2023
0
  }
2024
0
  else
2025
0
  {
2026
0
    a = 0;
2027
2028
0
    b = 1 << (internalBitDepth - 1);
2029
2030
0
    iShift = 0;
2031
0
  }
2032
0
}
2033
2034
void IntraPrediction::initIntraMip( const CodingUnit &cu, const CompArea &area )
2035
0
{
2036
0
  CHECK( area.width > MIP_MAX_WIDTH || area.height > MIP_MAX_HEIGHT, "Error: block size not supported for MIP" );
2037
2038
  // prepare input (boundary) data for prediction
2039
//  CHECK( m_ipaParam.refFilterFlag, "ERROR: unfiltered refs expected for MIP" );
2040
0
  Pel *ptrSrc = getPredictorPtr( area.compID() );
2041
0
  const int srcStride  = m_topRefLength  + 1; //TODO: check this if correct
2042
0
  const int srcHStride = m_leftRefLength + 1;
2043
2044
0
  m_matrixIntraPred.prepareInputForPred( CPelBuf( ptrSrc, srcStride, srcHStride ), area, cu.sps->getBitDepth(), area.compID() );
2045
0
}
2046
2047
void IntraPrediction::predIntraMip( const ComponentID compId, PelBuf &piPred, const CodingUnit &cu )
2048
0
{
2049
0
  CHECK( piPred.width > MIP_MAX_WIDTH || piPred.height > MIP_MAX_HEIGHT, "Error: block size not supported for MIP" );
2050
0
  CHECK( piPred.width != (1 << getLog2(piPred.width)) || piPred.height != (1 << getLog2(piPred.height)), "Error: expecting blocks of size 2^M x 2^N" );
2051
2052
  // generate mode-specific prediction
2053
0
  uint32_t modeIdx       = MAX_NUM_MIP_MODE;
2054
0
  bool     transposeFlag = false;
2055
0
  if( compId == COMPONENT_Y )
2056
0
  {
2057
0
    modeIdx       = cu.intraDir[CHANNEL_TYPE_LUMA];
2058
0
    transposeFlag = cu.mipTransposedFlag();
2059
0
  }
2060
0
  else
2061
0
  {
2062
0
    const CodingUnit &coLocatedLumaPU = PU::getCoLocatedLumaPU(cu);
2063
2064
0
    CHECK(cu.intraDir[CHANNEL_TYPE_CHROMA] != DM_CHROMA_IDX, "Error: MIP is only supported for chroma with DM_CHROMA.");
2065
0
    CHECK(!coLocatedLumaPU.mipFlag(), "Error: Co-located luma CU should use MIP.");
2066
2067
0
    modeIdx       = coLocatedLumaPU.intraDir[CHANNEL_TYPE_LUMA];
2068
0
    transposeFlag = coLocatedLumaPU.mipTransposedFlag();
2069
0
  }
2070
2071
0
  CHECK(modeIdx >= getNumModesMip(piPred), "Error: Wrong MIP mode index");
2072
2073
0
  const int bitDepth = cu.sps->getBitDepth();
2074
0
  m_matrixIntraPred.predBlock( piPred, modeIdx, piPred, transposeFlag, bitDepth, compId, m_piYuvExt[0] );
2075
0
}
2076
2077
}