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/EncGOP.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     EncGOP.h
43
    \brief    GOP encoder class (header)
44
*/
45
46
#pragma once
47
48
#include "EncSampleAdaptiveOffset.h"
49
#include "VLCWriter.h"
50
#include "SEIwrite.h"
51
#include "SEIEncoder.h"
52
#include "Analyze.h"
53
#include "EncPicture.h"
54
#include "CommonLib/Picture.h"
55
#include "CommonLib/CommonDef.h"
56
#include "CommonLib/Nal.h"
57
#include "EncHRD.h"
58
#include "EncStage.h"
59
#include "SEIFilmGrainAnalyzer.h"
60
61
#include <vector>
62
#include <list>
63
#include <stdlib.h>
64
#include <atomic>
65
66
67
#include "Utilities/NoMallocThreadPool.h"
68
#include <mutex>
69
#include <condition_variable>
70
71
//! \ingroup EncoderLib
72
//! \{
73
74
namespace vvenc {
75
76
// ====================================================================================================================
77
78
class InputByteStream;
79
class DecLib;
80
class EncHRD;
81
class MsgLog;
82
class GOPCfg;
83
84
// ====================================================================================================================
85
86
class EncGOP;
87
struct FinishTaskParam {
88
  EncGOP*     gopEncoder;
89
  EncPicture* picEncoder;
90
  Picture*    pic;
91
0
  FinishTaskParam()                                          : gopEncoder( nullptr ), picEncoder( nullptr ), pic( nullptr ) {}
92
1.15k
  FinishTaskParam( EncGOP* _g, EncPicture* _e, Picture* _p ) : gopEncoder( _g ),      picEncoder( _e ),      pic( _p )      {}
93
};
94
95
// ====================================================================================================================
96
struct RateCapParam {
97
  unsigned accumActualBits = 0;
98
  unsigned accumTargetBits = 0;
99
  unsigned accumGopCounter = 0;
100
  double   nonRateCapEstim = 0.0;
101
  int      gopAdaptedQPAdj = 0;
102
103
  void reset()
104
1.15k
  {
105
1.15k
    accumActualBits = 0;
106
1.15k
    accumTargetBits = 0;
107
1.15k
    accumGopCounter = 0;
108
1.15k
  }
109
};
110
111
// ====================================================================================================================
112
// film grain parameters estimated by the film grain analysis, as signalled in the FGC SEI
113
struct FilmGrainParam {
114
  int               log2ScaleFactor = 0;
115
  SeiFgc::CompModel compModel[ MAX_NUM_COMP ];
116
117
1.15k
  FilmGrainParam() = default;
118
  explicit FilmGrainParam( FGAnalyzer& fgAnalyzer )
119
0
    : log2ScaleFactor( fgAnalyzer.getLog2scaleFactor() )
120
0
  {
121
0
    for( int compIdx = 0; compIdx < MAX_NUM_COMP; compIdx++ )
122
0
    {
123
0
      compModel[ compIdx ] = fgAnalyzer.getCompModel( compIdx );
124
0
    }
125
0
  }
126
};
127
128
class EncGOP : public EncStage
129
{
130
private:
131
  MsgLog&                   msg;
132
133
  Analyze                   m_AnalyzeAll;
134
  Analyze                   m_AnalyzeI;
135
  Analyze                   m_AnalyzeP;
136
  Analyze                   m_AnalyzeB;
137
138
  std::function<void( void*, vvencYUVBuffer* )> m_recYuvBufFunc;
139
  void*                                         m_recYuvBufCtx;
140
141
  NoMallocThreadPool*       m_threadPool;
142
  std::mutex                m_gopEncMutex;
143
  std::condition_variable   m_gopEncCond;
144
  XUCache                   m_shrdUnitCache;
145
  std::mutex                m_unitCacheMutex;
146
147
  const VVEncCfg*           m_pcEncCfg;
148
  const GOPCfg*             m_gopCfg;
149
  RateCtrl*                 m_pcRateCtrl;
150
  HLSWriter                 m_HLSWriter;
151
  SEIWriter                 m_seiWriter;
152
  SEIEncoder                m_seiEncoder;
153
  BlkStat                   m_BlkStat;
154
155
  ParameterSetMap<SPS>      m_spsMap;
156
  ParameterSetMap<PPS>      m_ppsMap;
157
  EncHRD                    m_EncHRD;
158
  VPS                       m_VPS;
159
  DCI                       m_DCI;
160
161
  bool                      m_isPreAnalysis;
162
  bool                      m_bFirstWrite;
163
  bool                      m_bRefreshPending;
164
  int                       m_lastCodingNum;
165
  int                       m_numPicsCoded;
166
  int                       m_numPicsInMissing;
167
  int                       m_numPicsOutOffset;
168
  uint64_t                  m_lastCts;
169
  int                       m_pocRecOut;
170
  int                       m_ticksPerFrameMul4;
171
  int                       m_lastIDR;
172
  int                       m_lastRasPoc;
173
  int                       m_pocCRA;
174
  int                       m_associatedIRAPPOC;
175
  vvencNalUnitType          m_associatedIRAPType;
176
  RateCapParam              m_rcap;
177
178
  ThreadRsrcPool            m_threadRsrc;          ///< CU encoders of the worker threads, shared by all picture encoders
179
  std::list<EncPicture*>    m_freePicEncoderList;
180
  std::list<Picture*>       m_gopEncListInput;
181
  std::list<Picture*>       m_gopEncListOutput;
182
  std::list<Picture*>       m_procList;
183
  std::list<Picture*>       m_rcUpdateList;
184
  std::list<Picture*>       m_rcInputReorderList;  // used in RC in IFP lines synchro mode
185
  std::deque<PicApsGlobal*> m_globalApsList;
186
  std::vector<int>          m_globalCtuQpVector;
187
  bool                      m_forceSCC;
188
189
  FGAnalyzer                m_fgAnalyzer;
190
  std::deque<std::pair<int, FilmGrainParam>> m_fgaResults;  ///< analysis results by coding number, waiting for the analyzed picture's access unit
191
  FilmGrainParam            m_fgcParam;                     ///< film grain parameters signalled in the access units being written
192
193
public:
194
  EncGOP( MsgLog& msglog );
195
  virtual ~EncGOP();
196
197
  void setRecYUVBufferCallback( void* ctx, std::function<void( void*, vvencYUVBuffer* )> func );
198
199
  void init               ( const VVEncCfg& encCfg, const GOPCfg* gopCfg, RateCtrl& rateCtrl, NoMallocThreadPool* threadPool, bool isPreAnalysis );
200
  void printOutSummary    ( const bool printMSEBasedSNR, const bool printSequenceMSE, const bool printHexPsnr );
201
  void getParameterSets   ( AccessUnitList& accessUnit );
202
203
protected:
204
  virtual void initPicture    ( Picture* pic );
205
  virtual void processPictures( const PicList& picList, AccessUnitList& auList, PicList& doneList, PicList& freeList );
206
  virtual void waitForFreeEncoders();
207
208
private:
209
  void xUpdateRasInit                 ( Slice* slice );
210
  void xProcessPictures               ( AccessUnitList& auList, PicList& doneList );
211
  void xEncodePicture                 ( Picture* pic, EncPicture* picEncoder );
212
  void xOutputRecYuv                  ( const PicList& picList );
213
  void xReleasePictures               ( const PicList& picList, PicList& freeList );
214
215
  void xInitVPS                       ( VPS &vps ) const;
216
  void xInitDCI                       ( DCI &dci, const SPS &sps, const int dciId ) const;
217
  void xInitConstraintInfo            ( ConstraintInfo &ci ) const;
218
  void xInitSPS                       ( SPS &sps ) const;
219
  void xInitPPS                       ( PPS &pps, const SPS &sps ) const;
220
  void xInitPPSforTiles               ( PPS &pps, const SPS &sps ) const;
221
  void xInitRPL                       ( SPS &sps ) const;
222
  void xInitHrdParameters             ( SPS &sps );
223
224
  vvencNalUnitType xGetNalUnitType    ( const GOPEntry* _gopEntry ) const;
225
  bool xIsSliceTemporalSwitchingPoint ( const Slice* slice, const PicList& picList ) const;
226
227
  void xSetupPicAps                   ( Picture* pic );
228
  void xAnalyzeFilmGrain              ( Picture& pic );
229
  void xInitPicsInCodingOrder         ( const PicList& picList );
230
  void xGetProcessingLists            ( std::list<Picture*>& procList, std::list<Picture*>& rcUpdateList, const bool lockStepMode );
231
  void xUpdateVAStartOfLastGop        ( Picture& keyPic ) const;
232
  void xInitGopQpCascade              ( Picture& keyPic, PicList::const_iterator picItr, const PicList& picList );
233
  void xInitFirstSlice                ( Picture& pic, const PicList& picList, bool isEncodeLtRef );
234
  void xInitSliceTMVPFlag             ( PicHeader* picHeader, const Slice* slice );
235
  void xUpdateRPRtmvp                 ( PicHeader* picHeader, Slice* slice );
236
  void xInitSliceMvdL1Zero            ( PicHeader* picHeader, const Slice* slice );
237
  void xSelectReferencePictureList    ( Slice* slice ) const;
238
  void xSyncAlfAps                    ( Picture& pic );
239
240
  void xUpdateRcIfp                   ();
241
  void xWritePicture                  ( Picture& pic, AccessUnitList& au, bool isEncodeLtRef );
242
  int  xWriteParameterSets            ( Picture& pic, AccessUnitList& accessUnit, HLSWriter& hlsWriter );
243
  int  xWritePictureSlices            ( Picture& pic, AccessUnitList& accessUnit, HLSWriter& hlsWriter );
244
  void xWriteLeadingSEIs              ( const Picture& pic, AccessUnitList& accessUnit );
245
  void xWriteTrailingSEIs             ( const Picture& pic, AccessUnitList& accessUnit, std::string& digestStr );
246
  int  xWriteVPS                      ( AccessUnitList &accessUnit, const VPS *vps, HLSWriter& hlsWriter );
247
  int  xWriteDCI                      ( AccessUnitList &accessUnit, const DCI *dci, HLSWriter& hlsWriter );
248
  int  xWriteSPS                      ( AccessUnitList &accessUnit, const SPS *sps, HLSWriter& hlsWriter );
249
  int  xWritePPS                      ( AccessUnitList &accessUnit, const PPS *pps, const SPS *sps, HLSWriter& hlsWriter );
250
  int  xWriteAPS                      ( AccessUnitList &accessUnit, const APS *aps, HLSWriter& hlsWriter, vvencNalUnitType eNalUnitType );
251
  void xWriteAccessUnitDelimiter      ( AccessUnitList &accessUnit, Slice* slice, bool IrapOrGdr, HLSWriter& hlsWriter );
252
  void xWriteSEI                      ( vvencNalUnitType naluType, SEIMessages& seiMessages, AccessUnitList &accessUnit, AccessUnitList::iterator &auPos, int temporalId, const SPS *sps );
253
  void xWriteSEISeparately            ( vvencNalUnitType naluType, SEIMessages& seiMessages, AccessUnitList &accessUnit, AccessUnitList::iterator &auPos, int temporalId, const SPS *sps );
254
  void xAttachSliceDataToNalUnit      ( OutputNALUnit& rNalu, const OutputBitstream* pcBitstreamRedirect );
255
  void xCabacZeroWordPadding          ( const Picture& pic, const Slice* slice, uint32_t binCountsInNalUnits, uint32_t numBytesInVclNalUnits, std::ostringstream &nalUnitData );
256
257
  void xUpdateRateCap();
258
  void xUpdateRateCapBits             ( const Picture* pic, const uint32_t uibits );
259
  void xAddPSNRStats( const Picture* pic, CPelUnitBuf cPicD, AccessUnitList&, bool printFrameMSE, double* PSNR_Y, bool isEncodeLtRef );
260
  uint64_t xFindDistortionPlane       ( const CPelBuf& pic0, const CPelBuf& pic1, uint32_t rshift ) const;
261
  void xPrintPictureInfo              ( const Picture& pic, AccessUnitList& accessUnit, const std::string& digestStr, bool printFrameMSE, bool isEncodeLtRef );
262
1.15k
  inline bool xEncodersFinished       () { return ( int ) m_freePicEncoderList.size() >= std::max(1, m_pcEncCfg->m_maxParallelFrames); }
263
  inline bool xLockStepPicsFinished   ()
264
0
  {
265
0
    std::lock_guard<std::mutex> lock( m_gopEncMutex );
266
0
    return ( int ) m_freePicEncoderList.size() >= std::max(1, m_pcEncCfg->m_maxParallelFrames); 
267
0
  }
268
  void xForceScc                      ( Picture& pic );
269
270
};// END CLASS DEFINITION EncGOP
271
272
} // namespace vvenc
273
274
//! \}
275