Coverage Report

Created: 2026-04-01 07:49

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