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/EncSlice.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     EncSlice.h
43
    \brief    slice encoder class (header)
44
*/
45
46
#pragma once
47
48
#include "EncCu.h"
49
#include "EncSampleAdaptiveOffset.h"
50
#include "CommonLib/CommonDef.h"
51
#include "CommonLib/Picture.h"
52
#include "InterSearch.h"
53
54
#include <atomic>
55
#include <functional>
56
57
//! \ingroup EncoderLib
58
//! \{
59
60
namespace vvenc {
61
62
struct SAOStatData;
63
class EncSampleAdaptiveOffset;
64
class EncAdaptiveLoopFilter;
65
class EncPicture;
66
class NoMallocThreadPool;
67
struct WaitCounter;
68
69
// ====================================================================================================================
70
// Class definition
71
// ====================================================================================================================
72
73
struct TileLineEncRsrc;
74
struct PerThreadRsrc;
75
struct CtuEncParam;
76
77
enum TaskType {
78
  CTU_ENCODE     = 0,
79
  LF_VER,
80
  LF_HOR,
81
  SAO_FILTER,
82
  ALF_GET_STATISTICS,
83
  ALF_DERIVE_FILTER,
84
  ALF_RECONSTRUCT,
85
  CCALF_GET_STATISTICS,
86
  CCALF_DERIVE_FILTER,
87
  CCALF_RECONSTRUCT,
88
  FINISH_SLICE,
89
  PROCESS_DONE
90
};
91
92
using ProcessCtuState = std::atomic<TaskType>;
93
94
/// CU encoders and scratch buffers for CTU tasks, one set per worker thread.
95
/// A worker runs one task at a time, so the slice encoders of all pictures that are encoded in
96
/// parallel can share these sets instead of owning one each. A set takes about 12 MB with 128x128
97
/// CTUs at the medium preset, and sharing saves one set per thread for every extra parallel frame.
98
class ThreadRsrcPool
99
{
100
public:
101
1.43k
  ThreadRsrcPool() = default;
102
  ~ThreadRsrcPool();
103
104
  ThreadRsrcPool( const ThreadRsrcPool& )            = delete;
105
  ThreadRsrcPool& operator=( const ThreadRsrcPool& ) = delete;
106
107
  void           init( const VVEncCfg& encCfg, const SPS& sps, const PPS& pps, std::vector<int>* const globalCtuQpVector, RateCtrl& rateCtrl, const int numSets );
108
  PerThreadRsrc& get ( const int threadIdx )
109
32.5k
  {
110
32.5k
    CHECK( threadIdx < 0 || threadIdx >= ( int ) m_sets.size(), "no thread resources for this worker thread" );
111
32.5k
    return *m_sets[threadIdx];
112
32.5k
  }
113
114
private:
115
  std::vector<PerThreadRsrc*> m_sets;
116
};
117
118
/// slice encoder class
119
class EncSlice
120
{
121
private:
122
  // encoder configuration
123
  const VVEncCfg*              m_pcEncCfg;                           ///< encoder configuration class
124
125
  ThreadRsrcPool*              m_threadRsrc;
126
  PicEncState                  m_picEncState;                        ///< state that worker threads apply to their CU encoders
127
  std::vector<int>*            m_globalCtuQpVector;
128
  std::vector<TileLineEncRsrc*>m_TileLineEncRsrc;
129
  NoMallocThreadPool*          m_threadPool;
130
  WaitCounter*                 m_ctuTasksDoneCounter;
131
  std::vector<ProcessCtuState> m_processStates;
132
  int                          m_ctuEncDelay;
133
  PelStorage                   m_ctuLineBottoms;                     ///< the encoded picture's unfiltered CTU line bottoms (Picture::m_ctuLineBottoms)
134
135
  LoopFilter*                  m_pLoopFilter;
136
  EncAdaptiveLoopFilter*       m_pALF;
137
  RateCtrl*                    m_pcRateCtrl;
138
  BinEncoder                   m_BinEncoder;
139
  CABACWriter                  m_CABACWriter;
140
141
  Ctx                          m_entropyCodingSyncContextState;      ///< context storage for state of contexts at the wavefront/WPP/entropy-coding-sync second CTU of tile-row used for writing
142
  std::vector<Ctx>             m_syncPicCtx;                         ///< context storage for state of contexts at the wavefront/WPP/entropy-coding-sync second CTU of tile-row used for estimation
143
  SliceType                    m_encCABACTableIdx;
144
  unsigned                     m_alfDeriveCtu;
145
  unsigned                     m_ccalfDeriveCtu;
146
147
  double                       m_saoDisabledRate[ MAX_NUM_COMP ][ VVENC_MAX_TLAYER ];
148
  bool                         m_saoEnabled[ MAX_NUM_COMP ];
149
  bool                         m_saoAllDisabled;
150
  std::vector<SAOBlkParam>     m_saoReconParams;
151
  std::vector<SAOStatData**>   m_saoStatData;
152
  SaoPreFilterEdges            m_saoPreFilterEdges;
153
  std::vector<CtuEncParam>     ctuEncParams;
154
  std::vector<int>             m_ctuAddrMap;
155
156
public:
157
  std::function<void( Picture& )> m_alfFiltersDerived;               ///< IFP: called when the ALF and CC-ALF filters (the APSs) are final
158
159
  EncSlice();
160
  virtual ~EncSlice();
161
162
  void    init                ( const VVEncCfg& encCfg,
163
                                const SPS& sps,
164
                                const PPS& pps,
165
                                std::vector<int>* const globalCtuQpVector,
166
                                LoopFilter& loopFilter,
167
                                EncAdaptiveLoopFilter& alf,
168
                                RateCtrl& rateCtrl,
169
                                NoMallocThreadPool* threadPool,
170
                                ThreadRsrcPool& threadRsrc,
171
                                WaitCounter* ctuTasksDoneCounter );
172
173
  void    initPic             ( Picture* pic );
174
175
  // compress and encode slice
176
  void    compressSlice       ( Picture* pic );      ///< analysis stage of slices
177
  void    encodeSliceData     ( Picture* pic );
178
  void    saoDisabledRate     ( CodingStructure& cs, SAOBlkParam* reconParams );
179
  void    finishCompressSlice ( Picture* pic, Slice& slice );
180
181
private:
182
  void    xInitSliceLambdaQP  ( Slice* slice );
183
  double  xCalculateLambda    ( const Slice* slice, const int depth, const double refQP, const double dQP, int& iQP );
184
  void    xProcessCtus        ( Picture* pic, const unsigned startCtuTsAddr, const unsigned boundingCtuTsAddr );
185
  bool    xCcAlfStatsUnused   ( const ProcessCtuState* processStates, const int ctuRsAddr ) const;
186
  template<bool checkReadyState=false>
187
  static bool xProcessCtuTask ( int taskIdx, void* taskParam );
188
189
  double  xGetQPForPicture    ( const Slice* slice );
190
  int     xGetQPForPicture    ( const Slice* slice, const int baseQP );
191
192
public:
193
  static int getCascadeQP     ( const VVEncCfg& encCfg, const GOPEntry& gopEntry, const bool isIntra, int qp, const int lumaQpBDOffset );
194
};
195
196
} // namespace vvenc
197
198
//! \}
199