Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvenc/source/Lib/EncoderLib/IntraSearch.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     IntraSearch.h
43
    \brief    intra search class (header)
44
*/
45
46
#pragma once
47
48
#include "CABACWriter.h"
49
#include "CommonLib/IntraPrediction.h"
50
#include "CommonLib/TrQuant.h"
51
#include "CommonLib/Unit.h"
52
#include "CommonLib/RdCost.h"
53
#include "CommonLib/CommonDef.h"
54
55
//! \ingroup EncoderLib
56
//! \{
57
58
namespace vvenc {
59
60
// ====================================================================================================================
61
// Class definition
62
// ====================================================================================================================
63
class EncPicture;
64
class EncCu;
65
66
/// encoder search class
67
class IntraSearch : public IntraPrediction
68
{
69
public:
70
  // A luma residual as a trial of the search counted it: its bits, the CUCtx flags its residual_coding set and the
71
  // estimator's contexts after the trial's count (see takeLumaResiOfSearch).
72
  struct LumaResi
73
  {
74
    bool     known = false;
75
    uint64_t bits  = 0;
76
    CUCtx    flags;
77
    Ctx      ctx;
78
79
    void swap( LumaResi& other )
80
37.6k
    {
81
37.6k
      std::swap( known, other.known );
82
37.6k
      std::swap( bits,  other.bits );
83
37.6k
      std::swap( flags, other.flags );
84
37.6k
      ctx.swap( other.ctx );   // no copy of the contexts
85
37.6k
    }
86
  };
87
88
private:
89
90
  struct ModeInfo
91
  {
92
    bool     mipFlg;    // CU::mipFlag
93
    bool     mipTrFlg;  // CU::mipTransposeFlag
94
    int8_t   mRefId;    // CU::multiRefIdx
95
    uint8_t  ispMod;    // CU::ispMode
96
    uint8_t  modeId;    // CU::intraDir[CH_L]
97
98
1.48M
    ModeInfo() : mipFlg( false ), mipTrFlg( false ), mRefId( 0 ), ispMod( NOT_INTRA_SUBPARTITIONS ), modeId( 0 ) {}
99
2.85M
    ModeInfo( const bool mipf, const bool miptf, const int8_t mrid, const uint8_t ispm, const uint8_t mode ) : mipFlg( mipf ), mipTrFlg( miptf ), mRefId( mrid ), ispMod( ispm ), modeId( mode ) {}
100
350k
    bool operator==( const ModeInfo cmp ) const { return ( 0 == ::memcmp( this, &cmp, sizeof( ModeInfo ) ) ); }
101
  };
102
103
  CodingStructure*    m_pTempCS;
104
  CodingStructure*    m_pBestCS;
105
  CodingStructure**   m_pSaveCS;
106
  bool                m_saveCuCostInSCIPU;
107
  uint8_t             m_numCuInSCIPU;
108
  Area                m_cuAreaInSCIPU[NUM_INTER_CU_INFO_SAVE];
109
  double              m_cuCostInSCIPU[NUM_INTER_CU_INFO_SAVE];
110
  CompStorage         m_orgResiCb[5], m_orgResiCr[5]; // 0:std, 1-3:jointCbCr, 4:crossComp
111
112
  // The chroma pre-selection's Cb and Cr predictions of the modes it checks (all but LM, planar and DM; up to 6, since
113
  // the candidate list has mode 66 in place of planar when DM is planar). The RD check of such a mode copies them
114
  // instead of predicting again when its block is the CU's whole chroma block, whose reference samples the
115
  // pre-selection used. The predictions lie back to back in m_preselPreds, sized for the largest chroma transform
116
  // block (a larger CU's chroma is split into several TBs): kept compact, the few KB a CU's blocks take stay in L1.
117
  // Valid during estIntraPredChromaQT only.
118
  static constexpr int NUM_PRESEL_CHROMA_PREDS = NUM_CHROMA_MODE - 2;
119
  std::vector<Pel>    m_preselPreds;
120
  int                 m_preselPredMode[NUM_PRESEL_CHROMA_PREDS];
121
  int                 m_numPreselPreds = 0;
122
  SizeType            m_preselPredMaxArea = 0;
123
124
  std::vector<ModeInfo>
125
                      m_parentCandList;
126
127
  // The MPM list of the CU that estIntraPredLumaQT is searching, derived once when the search starts: it depends
128
  // only on the CU's area and its neighbours, which do not change during the search. Valid only during the search,
129
  // since the same area can have other neighbours in another partitioning.
130
  unsigned            m_searchMpmLst[NUM_MOST_PROBABLE_MODES];
131
  Area                m_searchMpmArea;
132
  bool                m_searchMpmValid = false;
133
134
  // the last luma residual_coding of xEncCoeffQT, and how many the current estimate made (one per luma TU)
135
  uint64_t            m_lumaResiCallBits = 0;
136
  CUCtx               m_lumaResiCallFlags;
137
  int                 m_lumaResiCalls    = 0;
138
  LumaResi            m_trialBestResi;   // of xIntraCodingLumaQT's best transform so far
139
  LumaResi            m_modeBestResi;    // of xEstIntraPredLumaQT's best mode so far: csBest's
140
141
protected:
142
  // interface to option
143
  const VVEncCfg* m_pcEncCfg;
144
145
  // interface to classes
146
  TrQuant*        m_pcTrQuant;
147
  RdCost*         m_pcRdCost;
148
  // RD computation
149
  CABACWriter*    m_CABACEstimator;
150
  CtxCache*       m_CtxCache;
151
152
  SortedPelUnitBufs<SORTED_BUFS> *m_SortedPelUnitBufs;
153
public:
154
  IntraSearch();
155
  ~IntraSearch();
156
157
  struct ISPTestedModesInfo
158
  {
159
    int                                         numTotalParts[2];
160
    int                                         bestModeSoFar;
161
    ISPType                                     bestSplitSoFar;
162
    double                                      bestCost[2];
163
    bool                                        splitIsFinished[2];
164
    int                                         subTuCounter;
165
    PartSplit                                   IspType;
166
    bool                                        relatedCuIsValid;
167
    bool                                        intraWasTested;
168
    int                                         bestIntraMode;
169
    bool                                        isIntra;
170
    int                                         bestBefore[3];
171
    // set everything to default values
172
    void clear()
173
98.3k
    {
174
294k
      for (int splitIdx = 0; splitIdx < NUM_INTRA_SUBPARTITIONS_MODES - 1; splitIdx++)
175
196k
      {
176
196k
        numTotalParts[splitIdx] = 0;
177
196k
        splitIsFinished[splitIdx] = false;
178
196k
        bestCost[splitIdx] = MAX_DOUBLE;
179
196k
      }
180
98.3k
      bestModeSoFar = -1;
181
98.3k
      bestSplitSoFar = NOT_INTRA_SUBPARTITIONS;
182
98.3k
      subTuCounter = -1;
183
98.3k
      IspType = TU_NO_ISP;
184
98.3k
    }
185
    void init(const int numTotalPartsHor, const int numTotalPartsVer, bool n)
186
188k
    {
187
188k
      if (n)
188
98.3k
      {
189
98.3k
        intraWasTested = false;
190
98.3k
        relatedCuIsValid = false;
191
98.3k
        bestIntraMode = 0;
192
98.3k
        isIntra   = false;
193
98.3k
        std::memset(bestBefore,0, sizeof(bestBefore));
194
98.3k
        clear();
195
98.3k
      }
196
90.1k
      else
197
90.1k
      {
198
90.1k
        const int horSplit = HOR_INTRA_SUBPARTITIONS - 1, verSplit = VER_INTRA_SUBPARTITIONS - 1;
199
90.1k
        numTotalParts[horSplit] = numTotalPartsHor;
200
90.1k
        numTotalParts[verSplit] = numTotalPartsVer;
201
90.1k
        splitIsFinished[horSplit] = (numTotalParts[horSplit] == 0);
202
90.1k
        splitIsFinished[verSplit] = (numTotalParts[verSplit] == 0);
203
90.1k
        subTuCounter = -1;
204
90.1k
        IspType = TU_NO_ISP;
205
90.1k
      }
206
188k
    }
207
  };
208
209
  ISPTestedModesInfo m_ispTestedModes[ NUM_LFNST_NUM_PER_SET ];
210
211
  void init                       ( const VVEncCfg &encCfg, TrQuant *pTrQuant, RdCost *pRdCost, SortedPelUnitBufs<SORTED_BUFS> *pSortedPelUnitBufs, XUCache &unitCache);
212
  void setCtuEncRsrc              ( CABACWriter* cabacEstimator, CtxCache* ctxCache );
213
  void destroy                    ();
214
215
5.72k
  CodingStructure  **getSaveCSBuf () { return m_pSaveCS; }
216
217
156k
  bool getSaveCuCostInSCIPU       ()               { return m_saveCuCostInSCIPU; }
218
427k
  void setSaveCuCostInSCIPU       ( bool b )       { m_saveCuCostInSCIPU = b;  }
219
213k
  void setNumCuInSCIPU            ( uint8_t i )    { m_numCuInSCIPU = i; }
220
  void saveCuAreaCostInSCIPU      ( Area area, double cost );
221
  void initCuAreaCostInSCIPU      ();
222
223
  bool estIntraPredLumaQT         ( CodingUnit &cu, Partitioner &pm, double bestCost = MAX_DOUBLE);
224
  void estIntraPredChromaQT       ( CodingUnit& cu, Partitioner& partitioner, const double maxCostAllowed );
225
  const LumaResi* takeLumaResiOfSearch( const CodingUnit& cu );
226
227
private:
228
  bool      xEstIntraPredLumaQT       ( CodingUnit &cu, Partitioner &pm, double bestCost );
229
  double    xFindInterCUCost          ( CodingUnit &cu );
230
  void      xPreCheckMTS              ( TransformUnit &tu, TrModeList *trModes, const int maxCand, PelUnitBuf *pPred, const ComponentID& compID = COMP_Y);
231
  void      xEstimateLumaRdModeList   ( int& numModesForFullRD,
232
                                        static_vector<ModeInfo, FAST_UDI_MAX_RDMODE_NUM>& RdModeList,
233
                                        static_vector<ModeInfo, FAST_UDI_MAX_RDMODE_NUM>& HadModeList,
234
                                        static_vector<double, FAST_UDI_MAX_RDMODE_NUM>& CandCostList,
235
                                        static_vector<double, FAST_UDI_MAX_RDMODE_NUM>& CandHadList, CodingUnit& cu, bool testMip);
236
237
  // -------------------------------------------------------------------------------------------------------------------
238
  // Intra search
239
  // -------------------------------------------------------------------------------------------------------------------
240
  uint64_t  xFracModeBitsIntraLuma    ( const CodingUnit& cu, const unsigned* mpmLst );
241
242
  void      xEncIntraHeader           ( CodingStructure &cs, Partitioner& pm, const bool luma );
243
  void      xEncSubdivCbfQT           ( CodingStructure &cs, Partitioner& pm, const bool luma );
244
  uint64_t  xGetIntraFracBitsQT       ( CodingStructure &cs, Partitioner &pm, const bool luma, CUCtx *cuCtx = nullptr );
245
246
  uint64_t  xGetIntraFracBitsQTChroma ( const TransformUnit& tu, const ComponentID compID, CUCtx *cuCtx );
247
  // the chroma pre-selection's kept prediction of slot's mode for comp (Cb or Cr), in a block of the size of area
248
  PelBuf    preselPred                ( int slot, ComponentID comp, const CompArea& area )
249
581k
  {
250
581k
    return PelBuf( m_preselPreds.data() + ( 2 * slot + ( comp == COMP_Cr ) ) * area.area(), area );
251
581k
  }
252
253
  // partitioner's current TU: an ISP sub-TU and a CU's only TU need no walk from the position
254
  TransformUnit& xCurrTU( CodingStructure& cs, Partitioner& partitioner ) const;
255
  void     xEncCoeffQT                ( CodingStructure& cs, Partitioner& pm, const ComponentID compID, CUCtx* cuCtx = nullptr, const int subTuIdx = -1, const PartSplit ispType = TU_NO_ISP );
256
257
  void      xIntraCodingTUBlock       ( TransformUnit &tu, const ComponentID compID, const bool checkCrossCPrediction, Distortion &ruiDist, uint32_t *numSig = nullptr, PelUnitBuf *pPred = nullptr, const bool loadTr = false, const QpParam* lumaQP = nullptr, const bool predKnown = false, const LfnstPrimary lfnstPrimary = LFNST_PRIMARY_NEW);
258
  ChromaCbfs xIntraChromaCodingQT     ( CodingStructure& cs, Partitioner& pm, const bool keepBestCtx );
259
  void     xIntraCodingLumaQT         ( CodingStructure& cs, Partitioner& pm, PelUnitBuf* pPred, const double bestCostSoFar, int numMode, bool disableMTS, const Ctx& ctxStart );
260
  void     xKeepLumaResiOfTrial       ( const CodingUnit& cu, const TransformUnit& tu );
261
  double   xTestISP                   ( CodingStructure& cs, Partitioner& pm, double bestCostSoFar, PartSplit ispType, bool& splitcbf, uint64_t& singleFracBits, Distortion& singleDistLuma, CUCtx& cuCtx);
262
  int      xSpeedUpISP                ( int speed, bool& testISP, int mode, int& noISP, int& endISP, CodingUnit& cu, static_vector<ModeInfo, FAST_UDI_MAX_RDMODE_NUM>& RdModeList,const ModeInfo& bestPUMode, int bestISP, int bestLfnstIdx);
263
  void     xSpeedUpIntra              ( double bestcost, int& EndMode, int& speedIntra, CodingUnit& cu);
264
265
  template<typename T, size_t N, int M>
266
  void      xReduceHadCandList        ( static_vector<T, N>& candModeList, static_vector<double, N>& candCostList, SortedPelUnitBufs<M>& sortedPelBuffer, int& numModesForFullRD, const double thresholdHadCost, const double* mipHadCost, const CodingUnit& cu, const bool fastMip);
267
268
};// END CLASS DEFINITION EncSearch
269
270
} // namespace vvenc
271
272
//! \}
273