Coverage Report

Created: 2026-08-13 07:23

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