Coverage Report

Created: 2026-10-17 06:56

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
private:
70
71
  struct ModeInfo
72
  {
73
    bool     mipFlg;    // CU::mipFlag
74
    bool     mipTrFlg;  // CU::mipTransposeFlag
75
    int8_t   mRefId;    // CU::multiRefIdx
76
    uint8_t  ispMod;    // CU::ispMode
77
    uint8_t  modeId;    // CU::intraDir[CH_L]
78
79
1.17M
    ModeInfo() : mipFlg( false ), mipTrFlg( false ), mRefId( 0 ), ispMod( NOT_INTRA_SUBPARTITIONS ), modeId( 0 ) {}
80
2.26M
    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 ) {}
81
277k
    bool operator==( const ModeInfo cmp ) const { return ( 0 == ::memcmp( this, &cmp, sizeof( ModeInfo ) ) ); }
82
  };
83
84
  CodingStructure*    m_pTempCS;
85
  CodingStructure*    m_pBestCS;
86
  CodingStructure**   m_pSaveCS;
87
  bool                m_saveCuCostInSCIPU;
88
  uint8_t             m_numCuInSCIPU;
89
  Area                m_cuAreaInSCIPU[NUM_INTER_CU_INFO_SAVE];
90
  double              m_cuCostInSCIPU[NUM_INTER_CU_INFO_SAVE];
91
  CompStorage         m_orgResiCb[5], m_orgResiCr[5]; // 0:std, 1-3:jointCbCr, 4:crossComp
92
  std::vector<ModeInfo>
93
                      m_parentCandList;
94
95
  // The MPM list of the CU that estIntraPredLumaQT is searching, derived once when the search starts: it depends
96
  // only on the CU's area and its neighbours, which do not change during the search. Valid only during the search,
97
  // since the same area can have other neighbours in another partitioning.
98
  unsigned            m_searchMpmLst[NUM_MOST_PROBABLE_MODES];
99
  Area                m_searchMpmArea;
100
  bool                m_searchMpmValid = false;
101
102
protected:
103
  // interface to option
104
  const VVEncCfg* m_pcEncCfg;
105
106
  // interface to classes
107
  TrQuant*        m_pcTrQuant;
108
  RdCost*         m_pcRdCost;
109
  // RD computation
110
  CABACWriter*    m_CABACEstimator;
111
  CtxCache*       m_CtxCache;
112
113
  SortedPelUnitBufs<SORTED_BUFS> *m_SortedPelUnitBufs;
114
public:
115
  IntraSearch();
116
  ~IntraSearch();
117
118
  struct ISPTestedModesInfo
119
  {
120
    int                                         numTotalParts[2];
121
    int                                         bestModeSoFar;
122
    ISPType                                     bestSplitSoFar;
123
    double                                      bestCost[2];
124
    bool                                        splitIsFinished[2];
125
    int                                         subTuCounter;
126
    PartSplit                                   IspType;
127
    bool                                        relatedCuIsValid;
128
    bool                                        intraWasTested;
129
    int                                         bestIntraMode;
130
    bool                                        isIntra;
131
    int                                         bestBefore[3];
132
    // set everything to default values
133
    void clear()
134
77.7k
    {
135
233k
      for (int splitIdx = 0; splitIdx < NUM_INTRA_SUBPARTITIONS_MODES - 1; splitIdx++)
136
155k
      {
137
155k
        numTotalParts[splitIdx] = 0;
138
155k
        splitIsFinished[splitIdx] = false;
139
155k
        bestCost[splitIdx] = MAX_DOUBLE;
140
155k
      }
141
77.7k
      bestModeSoFar = -1;
142
77.7k
      bestSplitSoFar = NOT_INTRA_SUBPARTITIONS;
143
77.7k
      subTuCounter = -1;
144
77.7k
      IspType = TU_NO_ISP;
145
77.7k
    }
146
    void init(const int numTotalPartsHor, const int numTotalPartsVer, bool n)
147
148k
    {
148
148k
      if (n)
149
77.7k
      {
150
77.7k
        intraWasTested = false;
151
77.7k
        relatedCuIsValid = false;
152
77.7k
        bestIntraMode = 0;
153
77.7k
        isIntra   = false;
154
77.7k
        std::memset(bestBefore,0, sizeof(bestBefore));
155
77.7k
        clear();
156
77.7k
      }
157
71.1k
      else
158
71.1k
      {
159
71.1k
        const int horSplit = HOR_INTRA_SUBPARTITIONS - 1, verSplit = VER_INTRA_SUBPARTITIONS - 1;
160
71.1k
        numTotalParts[horSplit] = numTotalPartsHor;
161
71.1k
        numTotalParts[verSplit] = numTotalPartsVer;
162
71.1k
        splitIsFinished[horSplit] = (numTotalParts[horSplit] == 0);
163
71.1k
        splitIsFinished[verSplit] = (numTotalParts[verSplit] == 0);
164
71.1k
        subTuCounter = -1;
165
71.1k
        IspType = TU_NO_ISP;
166
71.1k
      }
167
148k
    }
168
  };
169
170
  ISPTestedModesInfo m_ispTestedModes[ NUM_LFNST_NUM_PER_SET ];
171
172
  void init                       ( const VVEncCfg &encCfg, TrQuant *pTrQuant, RdCost *pRdCost, SortedPelUnitBufs<SORTED_BUFS> *pSortedPelUnitBufs, XUCache &unitCache);
173
  void setCtuEncRsrc              ( CABACWriter* cabacEstimator, CtxCache* ctxCache );
174
  void destroy                    ();
175
176
4.62k
  CodingStructure  **getSaveCSBuf () { return m_pSaveCS; }
177
178
123k
  bool getSaveCuCostInSCIPU       ()               { return m_saveCuCostInSCIPU; }
179
337k
  void setSaveCuCostInSCIPU       ( bool b )       { m_saveCuCostInSCIPU = b;  }
180
168k
  void setNumCuInSCIPU            ( uint8_t i )    { m_numCuInSCIPU = i; }
181
  void saveCuAreaCostInSCIPU      ( Area area, double cost );
182
  void initCuAreaCostInSCIPU      ();
183
184
  bool estIntraPredLumaQT         ( CodingUnit &cu, Partitioner &pm, double bestCost = MAX_DOUBLE);
185
  void estIntraPredChromaQT       ( CodingUnit& cu, Partitioner& partitioner, const double maxCostAllowed );
186
187
private:
188
  bool      xEstIntraPredLumaQT       ( CodingUnit &cu, Partitioner &pm, double bestCost );
189
  double    xFindInterCUCost          ( CodingUnit &cu );
190
  void      xPreCheckMTS              ( TransformUnit &tu, TrModeList *trModes, const int maxCand, PelUnitBuf *pPred, const ComponentID& compID = COMP_Y);
191
  void      xEstimateLumaRdModeList   ( int& numModesForFullRD,
192
                                        static_vector<ModeInfo, FAST_UDI_MAX_RDMODE_NUM>& RdModeList,
193
                                        static_vector<ModeInfo, FAST_UDI_MAX_RDMODE_NUM>& HadModeList,
194
                                        static_vector<double, FAST_UDI_MAX_RDMODE_NUM>& CandCostList,
195
                                        static_vector<double, FAST_UDI_MAX_RDMODE_NUM>& CandHadList, CodingUnit& cu, bool testMip);
196
197
  // -------------------------------------------------------------------------------------------------------------------
198
  // Intra search
199
  // -------------------------------------------------------------------------------------------------------------------
200
  uint64_t  xFracModeBitsIntraLuma    ( const CodingUnit& cu, const unsigned* mpmLst );
201
202
  void      xEncIntraHeader           ( CodingStructure &cs, Partitioner& pm, const bool luma );
203
  void      xEncSubdivCbfQT           ( CodingStructure &cs, Partitioner& pm, const bool luma );
204
  uint64_t  xGetIntraFracBitsQT       ( CodingStructure &cs, Partitioner &pm, const bool luma, CUCtx *cuCtx = nullptr );
205
206
  uint64_t  xGetIntraFracBitsQTChroma ( const TransformUnit& tu, const ComponentID compID, CUCtx *cuCtx );
207
208
  void     xEncCoeffQT                ( CodingStructure& cs, Partitioner& pm, const ComponentID compID, CUCtx* cuCtx = nullptr, const int subTuIdx = -1, const PartSplit ispType = TU_NO_ISP );
209
210
  void      xIntraCodingTUBlock       ( TransformUnit &tu, const ComponentID compID, const bool checkCrossCPrediction, Distortion &ruiDist, uint32_t *numSig = nullptr, PelUnitBuf *pPred = nullptr, const bool loadTr = false);
211
  ChromaCbfs xIntraChromaCodingQT     ( CodingStructure& cs, Partitioner& pm );
212
  void     xIntraCodingLumaQT         ( CodingStructure& cs, Partitioner& pm, PelUnitBuf* pPred, const double bestCostSoFar, int numMode, bool disableMTS);
213
  double   xTestISP                   ( CodingStructure& cs, Partitioner& pm, double bestCostSoFar, PartSplit ispType, bool& splitcbf, uint64_t& singleFracBits, Distortion& singleDistLuma, CUCtx& cuCtx);
214
  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);
215
  void     xSpeedUpIntra              ( double bestcost, int& EndMode, int& speedIntra, CodingUnit& cu);
216
217
  template<typename T, size_t N, int M>
218
  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);
219
220
};// END CLASS DEFINITION EncSearch
221
222
} // namespace vvenc
223
224
//! \}
225