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/IntraPrediction.h
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
/** \file     IntraPrediction.h
43
    \brief    prediction class (header)
44
*/
45
46
#pragma once
47
48
#include "Unit.h"
49
#include "Picture.h"
50
#include "MatrixIntraPrediction.h"
51
52
//! \ingroup CommonLib
53
//! \{
54
55
namespace vvenc {
56
57
#if ENABLE_SIMD_OPT_INTRAPRED && defined( TARGET_SIMD_X86 )
58
using namespace x86_simd;
59
#endif
60
#if ENABLE_SIMD_OPT_INTRAPRED && defined( TARGET_SIMD_ARM )
61
using namespace arm_simd;
62
#endif
63
64
// ====================================================================================================================
65
// Class definition
66
// ====================================================================================================================
67
68
/// prediction class
69
enum PredBuf
70
{
71
  PRED_BUF_UNFILTERED = 0,
72
  PRED_BUF_FILTERED   = 1,
73
  NUM_PRED_BUF        = 2
74
};
75
76
static const uint32_t MAX_INTRA_FILTER_DEPTHS=8;
77
78
class IntraPrediction
79
{
80
private:
81
  Pel         m_refBuffer[MAX_NUM_COMP][NUM_PRED_BUF][(MAX_CU_SIZE * 2 + 1 + MAX_REF_LINE_IDX) * 2];
82
  // xPredIntraAng's transposed prediction of horizontal modes; a member, since as a 32 KB local it made every call
83
  // probe its stack pages (___chkstk_ms on Windows)
84
  Pel         m_tempArray[MAX_CU_SIZE * MAX_CU_SIZE];
85
  uint32_t    m_refBufferStride[MAX_NUM_COMP];
86
  int         m_numIntraNeighbor;
87
  bool        m_neighborFlags[4 * MAX_NUM_PART_IDXS_IN_CTU_WIDTH + 1];
88
  Area        m_lastArea;
89
  ChannelType m_lastCh;
90
91
  static const uint8_t m_aucIntraFilter[MAX_INTRA_FILTER_DEPTHS];
92
93
  struct IntraPredParam //parameters of Intra Prediction
94
  {
95
    bool refFilterFlag;
96
    bool applyPDPC;
97
    bool isModeVer;
98
    int  multiRefIndex;
99
    int  intraPredAngle;
100
    int  absInvAngle;
101
    bool interpolationFlag;
102
    int  angularScale;
103
104
    IntraPredParam()
105
8.14k
      : refFilterFlag     (false)
106
8.14k
      , applyPDPC         (false)
107
8.14k
      , isModeVer         (false)
108
8.14k
      , multiRefIndex     (-1)
109
8.14k
      , intraPredAngle    (std::numeric_limits<int>::max())
110
8.14k
      , absInvAngle       (std::numeric_limits<int>::max())
111
8.14k
      , interpolationFlag (false)
112
8.14k
      , angularScale      (-1)
113
8.14k
    {
114
8.14k
    }
115
  };
116
117
  IntraPredParam        m_ipaParam;
118
  static void xDerivePredIntraParams( IntraPredParam& param, const ChannelType chType, const Size& blockSize, const Size& puSize, const int dirMode,
119
                                      const int multiRefIdx, const bool useISP, const bool isMIP, const bool bdpcm );
120
  Pel*                  m_pMdlmTemp; // for MDLM mode
121
  MatrixIntraPrediction m_matrixIntraPred;
122
123
  // The above and left reference rows of one reference buffer as xPredIntraAng reads them: each after a front margin,
124
  // into which negative angles write their projection of the side row, and replicated past its end as far as positive
125
  // angles read. Built once per filling of the reference buffers (m_refGeneration, bumped by initIntraPatternChType*)
126
  // instead of copied for every mode the encoder tests.
127
  static const int EXT_REF_MARGIN = MAX_CU_SIZE;
128
  static const int EXT_REF_SIZE   = EXT_REF_MARGIN + 2 * MAX_CU_SIZE + 3 + 33 * MAX_REF_LINE_IDX;
129
  struct ExtRefRows
130
  {
131
    Pel      above[EXT_REF_SIZE] = {};   // initialised once: the windows of IntraPredAngleLumaHor and IntraAnglePDPC_SIMD
132
    Pel      left [EXT_REF_SIZE] = {};   // load up to 3 / 7 samples past the ones they use, maybe past the ones built
133
    uint64_t generation    = 0;   // m_refGeneration when built (0: never)
134
    int      multiRefIdx   = -1;
135
    int      width         = 0;
136
    int      height        = 0;
137
    int      topRefLength  = 0;
138
    int      leftRefLength = 0;
139
  };
140
  ExtRefRows            m_extRef[MAX_NUM_COMP][NUM_PRED_BUF];
141
  uint64_t              m_refGeneration = 1;
142
  ExtRefRows&           xGetExtRefRows( const ComponentID compID, const CPelBuf& pSrc, const int width, const int height, const int multiRefIdx );
143
144
protected:
145
  ChromaFormat          m_currChromaFormat;
146
147
  int                   m_topRefLength;
148
  int                   m_leftRefLength;
149
  void setReferenceArrayLengths(const CompArea& area);
150
151
private:
152
816k
  static bool isIntegerSlope      ( const int absAng) { return (0 == (absAng & 0x1F)); }
153
  static int getWideAngle         ( int width, int height, int predMode );
154
155
  // prediction
156
  void xPredIntraDc               ( PelBuf& pDst, const CPelBuf& pSrc );
157
  void xPredIntraAng              ( PelBuf& pDst, const CPelBuf& pSrc, const ComponentID compID, const ClpRng& clpRng);
158
  Pel  xGetPredValDc              ( const CPelBuf& pSrc, const Size& dstSize );
159
  void xPredIntraBDPCM            ( PelBuf& pDst, const CPelBuf& pSrc, const uint32_t dirMode, const ClpRng& clpRng );
160
161
  void xFillReferenceSamples      ( const CPelBuf& recoBuf,      Pel* refBufUnfiltered, const CompArea& area, const CodingUnit &cu );
162
  void xFilterReferenceSamples    ( const Pel* refBufUnfiltered, Pel* refBufFiltered, const CompArea& area, const SPS &sps, int multiRefIdx, int predStride = 0 );
163
  void xGetLMParameters(const CodingUnit& cu, const ComponentID compID, const CompArea& chromaArea, int& a, int& b, int& iShift);
164
165
#if ENABLE_SIMD_OPT_INTRAPRED && defined( TARGET_SIMD_X86 )
166
  void initIntraPredictionX86();
167
  template <X86_VEXT vext>
168
  void _initIntraPredictionX86();
169
#endif
170
171
#if ENABLE_SIMD_OPT_INTRAPRED && defined( TARGET_SIMD_ARM )
172
  void initIntraPredictionARM();
173
  template <ARM_VEXT vext>
174
  void _initIntraPredictionARM();
175
#endif
176
177
public:
178
  IntraPrediction( bool enableOpt = true );
179
  virtual ~IntraPrediction();
180
181
  void init                   ( ChromaFormat chromaFormatIDC, const unsigned bitDepthY);
182
  void reset                  ();
183
  void destroy                ();
184
185
  void initPredIntraParams    ( const CodingUnit& cu,  const CompArea compArea, const SPS& sps );
186
187
  // initPredIntraParams for plain luma blocks (no ISP, MIP, reference line or BDPCM), where the parameters depend only on
188
  // the block size and the mode: getLumaParamRow gives a block's row once (nullptr: not such a block, initPredIntraParams
189
  // has to be used), setLumaModeParams then sets the parameters of one mode from its entry. For every size
190
  // 4..MAX_CU_SIZE and mode, derived at first use with xDerivePredIntraParams (initPredIntraParams' own derivation);
191
  // where initPredIntraParams leaves a field unchanged (the angles of planar and DC, angularScale of non-positive
192
  // angles) the table holds 0 (angles) or -1 (scale); none of these is read for those modes.
193
  struct LumaModeParam
194
  {
195
    int16_t intraPredAngle;
196
    int16_t absInvAngle;
197
    int8_t  angularScale;
198
    uint8_t flags;   // 1: isModeVer, 2: applyPDPC, 4: refFilterFlag, 8: interpolationFlag
199
    int16_t unused;
200
  };
201
  const LumaModeParam* getLumaParamRow( const CodingUnit& cu ) const;
202
  void setLumaModeParams      ( const LumaModeParam& p )
203
664k
  {
204
664k
    m_ipaParam.isModeVer         = ( p.flags & 1 ) != 0;
205
664k
    m_ipaParam.applyPDPC         = ( p.flags & 2 ) != 0;
206
664k
    m_ipaParam.refFilterFlag     = ( p.flags & 4 ) != 0;
207
664k
    m_ipaParam.interpolationFlag = ( p.flags & 8 ) != 0;
208
664k
    m_ipaParam.multiRefIndex     = 0;
209
664k
    m_ipaParam.intraPredAngle    = p.intraPredAngle;
210
664k
    m_ipaParam.absInvAngle       = p.absInvAngle;
211
664k
    m_ipaParam.angularScale      = p.angularScale;
212
664k
  }
213
214
  // Angular Intra
215
  void predIntraAng           ( const ComponentID compId, PelBuf& piPred, const CodingUnit& cu);
216
1.88M
  Pel* getPredictorPtr        ( const ComponentID compId ) { return m_refBuffer[compId][m_ipaParam.refFilterFlag ? PRED_BUF_FILTERED : PRED_BUF_UNFILTERED]; }
217
218
  // Cross-component Chroma
219
  void predIntraChromaLM      ( const ComponentID compID, PelBuf& piPred, const CodingUnit& cu, const CompArea& chromaArea, int intraDir);
220
  void loadLMLumaRecPels      ( const CodingUnit& cu, const CompArea& chromaArea );
221
  /// set parameters from CU data for accessing intra data
222
  void initIntraPatternChType ( const CodingUnit &cu, const CompArea& area, const bool forceRefFilterFlag = false); // use forceRefFilterFlag to get both filtered and unfiltered buffers
223
  void keepIntraPatternChType ( const CodingUnit &cu, const CompArea& area );   // for another mode of the block initIntraPatternChType was last called for
224
  void initIntraPatternChTypeISP( const CodingUnit& cu, const CompArea& area, PelBuf& piReco, const bool forceRefFilterFlag = false );
225
226
  // Matrix-based intra prediction
227
  void initIntraMip           ( const CodingUnit& cu);
228
  void predIntraMip           ( PelBuf &piPred, const CodingUnit& cu);
229
230
  int getNumIntraCiip         ( const CodingUnit& cu )
231
0
  {
232
0
    const Position posBL = cu.Y().bottomLeft();
233
0
    const Position posTR = cu.Y().topRight();
234
0
    const CodingUnit *neigh0 = cu.cs->getCURestricted(posBL.offset(-1, 0), cu, CH_L);
235
0
    const CodingUnit *neigh1 = cu.cs->getCURestricted(posTR.offset(0, -1), cu, CH_L);
236
237
0
    int numIntra = 0;
238
0
    numIntra += (neigh0 && (neigh0->predMode == MODE_INTRA))?1:0;
239
0
    numIntra += (neigh1 && (neigh1->predMode == MODE_INTRA))?1:0;
240
241
0
    return numIntra;
242
0
  }
243
244
  void ( *IntraPredAngleLuma )    ( Pel* pDstBuf, const ptrdiff_t dstStride, Pel* refMain, int width, int height, int deltaPos, int intraPredAngle, const TFilterCoeff* ff, const bool useCubicFilter, const ClpRng& clpRng );
245
  void ( *IntraPredAngleChroma )  ( Pel* pDst, const ptrdiff_t dstStride, Pel* pBorder, int width, int height, int deltaPos, int intraPredAngle );
246
  void ( *IntraAnglePDPC )        ( Pel* pDsty, const int dstStride, Pel* refSide, const int width, const int height, int scale, int invAngle );
247
  // horizontal luma modes with a fractional |angle| <= 29 straight into the block (width >= 8, height >= 4: the block's),
248
  // IntraPredAngleLuma and IntraAnglePDPC of the transposed block, transposed; nullptr: xPredIntraAng predicts into a
249
  // temporary block and transposes it
250
  void ( *IntraPredAngleLumaHor ) ( Pel* dst, const ptrdiff_t dstStride, const Pel* refMain, const Pel* refSide, const int width, const int height,
251
                                    const int intraPredAngle, const int multiRefIdx, const int scale, const int invAngle, const bool useCubicFilter,
252
                                    const bool applyPDPC, const ClpRng& clpRng );
253
  void ( *IntraHorVerPDPC )       ( Pel* pDsty, const int dstStride, Pel* refSide, const int width, const int height, int scale, const Pel* refMain, const ClpRng& clpRng );
254
  void ( *IntraPredSampleFilter ) ( PelBuf& piPred, const CPelBuf& pSrc );
255
  void ( *xPredIntraPlanar )      ( PelBuf& pDst, const CPelBuf& pSrc );
256
  // CCLM: 4:2:0 luma downsampling with the 6-tap filter (no vertical collocation), height chroma rows from src rows
257
  // 2 * y and 2 * y + 1; without a left neighbour the samples left of the block are replaced by the first column
258
  void ( *LMLumaDownsample420 )   ( const Pel* src, const ptrdiff_t srcStride, Pel* dst, const ptrdiff_t dstStride, const int width, const int height, const bool leftAvailable );
259
  // [1 2 1] smoothing of the reference samples 1 ... length - 1
260
  void ( *IntraFilterRefSamples ) ( const Pel* src, Pel* dst, const int length );
261
};
262
263
} // namespace vvenc
264
265
//! \}
266