Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvenc/source/Lib/CommonLib/Picture.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     Picture.h
43
 *  \brief    Description of a coded picture
44
 */
45
46
#pragma once
47
48
#include "CommonDef.h"
49
#include "Common.h"
50
#include "Unit.h"
51
#include "Slice.h"
52
#include "CodingStructure.h"
53
#include "BitStream.h"
54
55
#include <deque>
56
#include <chrono>
57
#include <atomic>
58
59
//! \ingroup CommonLib
60
//! \{
61
62
namespace vvenc {
63
64
class SEI;
65
class SEIDecodedPictureHash;
66
class EncRCPic;
67
class PicShared;
68
class MsgLog;
69
70
typedef std::list<SEI*> SEIMessages;
71
72
73
struct StopClock
74
{
75
4.29k
  StopClock() : m_startTime(), m_timer() {}
76
77
1.43k
  int  getTimerInSec() const { return (int)std::chrono::duration_cast<std::chrono::seconds>( m_timer ).count(); };
78
4.29k
  void resetTimer()          { m_timer = std::chrono::steady_clock::duration::zero(); }
79
5.72k
  void startTimer()          { m_startTime  = std::chrono::steady_clock::now(); }
80
5.72k
  void stopTimer()           { auto endTime = std::chrono::steady_clock::now(); m_timer += endTime - m_startTime; m_startTime = endTime; }
81
82
  std::chrono::steady_clock::time_point m_startTime;
83
  std::chrono::steady_clock::duration   m_timer;
84
};
85
86
87
struct PicVisAct
88
{
89
  PicVisAct()
90
11.4k
    : spatAct       { 0, 0 }
91
11.4k
    , prevTL0spatAct{ 0, 0 }
92
11.4k
    , visAct        ( 0 )
93
11.4k
    , visActTL0     ( 0 )
94
11.4k
  {
95
11.4k
  }
96
5.72k
  void reset() { *this = PicVisAct(); }
97
  uint16_t spatAct[MAX_NUM_CH];
98
  uint16_t prevTL0spatAct[MAX_NUM_CH];
99
  uint16_t visAct;
100
  uint16_t visActTL0;
101
};
102
103
struct Picture;
104
105
class BlkStat
106
{
107
public:
108
  BlkStat()
109
5.72k
    : m_bResetAMaxBT( true )
110
5.72k
  {
111
5.72k
  }
112
113
  ~BlkStat()
114
5.72k
  {
115
5.72k
  }
116
117
  void storeBlkSize ( const Picture& pic );
118
  void updateMaxBT  ( const Slice& slice, const BlkStat& blkStat );
119
  void setSliceMaxBT( Slice& slice );
120
121
protected:
122
  uint32_t m_uiBlkSize[NUM_AMAXBT_LAYER];
123
  uint32_t m_uiNumBlk[NUM_AMAXBT_LAYER];
124
  uint32_t m_uiPrevISlicePOC;
125
  bool     m_bResetAMaxBT;
126
};
127
128
struct PicApsGlobal{
129
  int      poc;
130
  unsigned tid;
131
  bool     initalized = false;
132
  int      refCnt = 0;
133
  ParameterSetMap<APS> apsMap;
134
0
  PicApsGlobal( int _p ) : poc(_p), tid(MAX_UINT), apsMap( MAX_NUM_APS * MAX_NUM_APS_TYPE ) {}
135
1.43k
  PicApsGlobal( int _p, unsigned _t ) : poc(_p), tid(_t), apsMap( MAX_NUM_APS * MAX_NUM_APS_TYPE ) {}
136
};
137
138
// Film grain analysis: the 16x16 DCT power of a picture component's flat original blocks with one 8-bit block mean, and of
139
// the same blocks of the reconstruction, summed over the picture (FGAnalyzer::lostGrainSpectra). The film grain SEI is
140
// scaled down to the grain power the coding lost (EncGOP::xScaleToLostGrain).
141
struct LostGrainSpectrum
142
{
143
  int    mean8  = 0;
144
  int    blocks = 0;
145
  double orig[ 256 ] = {};
146
  double reco[ 256 ] = {};
147
};
148
149
struct Picture : public UnitArea
150
{
151
  uint32_t margin;
152
  Picture();
153
154
  void create( ChromaFormat _chromaFormat, const Size& size, unsigned _maxCUSize, unsigned _margin, bool _decoder );
155
  void reset();
156
  void destroy( bool bPicHeader );
157
158
  void linkSharedBuffers( PelStorage* origBuf, PelStorage* filteredBuf, PelStorage* prevOrigBufs[ NUM_QPA_PREV_FRAMES ], PicShared* picShared );
159
  void releasePrevBuffers();
160
  void releaseSharedBuffers();
161
162
  // The reconstruction and the motion field are needed from the start of the picture's encoding
163
  // until the picture is released, not while it waits in the GOP queue.
164
  void createEncodingBuffers();
165
  void releaseEncodingBuffers();
166
167
  void extendPicBorder();
168
  void finalInit( const VPS& vps, const SPS& sps, const PPS& pps, PicHeader* picHeader, XUCache& unitCache, std::mutex* mutex, APS** alfAps );
169
  void setSccFlags( const VVEncCfg* encCfg );
170
171
2.86k
  int  getPOC()                                                     const { return poc; }
172
173
0
  const PelStorage& getOrigBuffer()                                       { return *m_sharedBufs[       PIC_ORIGINAL]; }
174
30.4k
         PelUnitBuf getOrigBuf()                                          { return getSharedBuf(        PIC_ORIGINAL); }
175
0
  const CPelUnitBuf getOrigBuf()                                    const { return getSharedBuf(        PIC_ORIGINAL); }
176
5.72k
  const CPelBuf     getOrigBuf(const ComponentID compID)            const { return getSharedBuf(compID, PIC_ORIGINAL); }
177
245k
  const CPelBuf     getOrigBuf(const CompArea& blk)                 const { return getSharedBuf(blk,    PIC_ORIGINAL); }
178
4.65k
  const CPelUnitBuf getOrigBuf(const UnitArea& unit)                const { return getSharedBuf(unit,   PIC_ORIGINAL); }
179
180
0
         PelBuf     getRecoBuf(const ComponentID compID)                  { return getPicBuf(compID, PIC_RECONSTRUCTION); }
181
0
  const CPelBuf     getRecoBuf(const ComponentID compID)            const { return getPicBuf(compID, PIC_RECONSTRUCTION); }
182
1.06M
         PelBuf     getRecoBuf(const CompArea& blk)                       { return getPicBuf(blk,    PIC_RECONSTRUCTION); }
183
171k
  const CPelBuf     getRecoBuf(const CompArea& blk)                 const { return getPicBuf(blk,    PIC_RECONSTRUCTION); }
184
148k
         PelUnitBuf getRecoBuf(const UnitArea& unit)                      { return getPicBuf(unit,   PIC_RECONSTRUCTION); }
185
0
  const CPelUnitBuf getRecoBuf(const UnitArea& unit)                const { return getPicBuf(unit,   PIC_RECONSTRUCTION); }
186
25.1k
         PelUnitBuf getRecoBuf()                                          { return getPicBuf(        PIC_RECONSTRUCTION); }
187
1.43k
  const CPelUnitBuf getRecoBuf()                                    const { return getPicBuf(        PIC_RECONSTRUCTION); }
188
26.8k
         int        getRecoBufStride(const ComponentID compID)      const { return m_picBufs[PIC_RECONSTRUCTION].bufs[compID].stride; }
189
26.8k
  const  Pel*       getRecoBufPtr   (const ComponentID compID)      const { return m_picBufs[PIC_RECONSTRUCTION].bufs[compID].buf; }
190
191
  // The rows above a CTU top as the CTU line above was encoded, before its loop filters. The readers of unfiltered samples
192
  // there (intra reference samples, CCLM, the deblocking estimate EDO) use it, so a line is deblocked without waiting for the
193
  // encoding of the line below. blk lies in the bottom CTU_LINE_BOTTOM_ROWS (luma) rows of one CTU line.
194
  const CPelBuf     getRecoAboveCtuTop(const CompArea& blk)         const;
195
  // Keeps the bottom rows of an encoded CTU for getRecoAboveCtuTop (called before its deblocking may start).
196
  void              keepCtuBottomRows (const UnitArea& ctuArea);
197
198
215k
        PelStorage& getFilteredOrigBuffer()                               { return *m_sharedBufs[       PIC_FILT_ORIGINAL]; }
199
0
         PelUnitBuf getFiltOrigBuf()                                      { return getSharedBuf(        PIC_FILT_ORIGINAL); }
200
0
  const CPelUnitBuf getFiltOrigBuf()                                const { return getSharedBuf(        PIC_FILT_ORIGINAL); }
201
0
  const CPelBuf     getFiltOrigBuf(const ComponentID compID)        const { return getSharedBuf(compID, PIC_FILT_ORIGINAL); }
202
0
  const CPelBuf     getFiltOrigBuf(const CompArea& blk)             const { return getSharedBuf(blk,    PIC_FILT_ORIGINAL); }
203
0
  const CPelUnitBuf getFiltOrigBuf(const UnitArea& unit)            const { return getSharedBuf(unit,   PIC_FILT_ORIGINAL); }
204
205
  const CPelBuf     getOrigBufPrev(const CompArea &blk, const PrevFrameType type) const;
206
  const CPelUnitBuf getOrigBufPrev(const PrevFrameType type) const;
207
  const CPelBuf     getOrigBufPrev(const ComponentID compID, const PrevFrameType type) const;
208
209
private:
210
25.1k
         PelUnitBuf getPicBuf(                          const PictureType type)         { return m_picBufs[ type ]; }
211
1.43k
  const CPelUnitBuf getPicBuf(                          const PictureType type)   const { return m_picBufs[ type ]; }
212
0
         PelBuf     getPicBuf(const ComponentID compID, const PictureType type)         { return m_picBufs[ type ].getBuf( compID ); }
213
0
  const CPelBuf     getPicBuf(const ComponentID compID, const PictureType type)   const { return m_picBufs[ type ].getBuf( compID ); }
214
1.50M
         PelBuf     getPicBuf(const CompArea& blk,      const PictureType type)         { return ( !blk.valid() ) ? PelBuf() : m_picBufs[ type ].getBuf( blk ); }
215
171k
  const CPelBuf     getPicBuf(const CompArea& blk,      const PictureType type)   const { return ( !blk.valid() ) ? PelBuf() : m_picBufs[ type ].getBuf( blk ); }
216
         PelUnitBuf getPicBuf(const UnitArea& unit,     const PictureType type);
217
  const CPelUnitBuf getPicBuf(const UnitArea& unit,     const PictureType type) const;
218
219
30.4k
         PelUnitBuf getSharedBuf(                          const PictureType type)         { return *m_sharedBufs[ type ]; }
220
0
  const CPelUnitBuf getSharedBuf(                          const PictureType type)   const { return *m_sharedBufs[ type ]; }
221
0
         PelBuf     getSharedBuf(const ComponentID compID, const PictureType type)         { return  m_sharedBufs[ type ]->getBuf( compID ); }
222
5.72k
  const CPelBuf     getSharedBuf(const ComponentID compID, const PictureType type)   const { return  m_sharedBufs[ type ]->getBuf( compID ); }
223
0
         PelBuf     getSharedBuf(const CompArea& blk,      const PictureType type)         { return ( !blk.valid() ) ? PelBuf() : m_sharedBufs[ type ]->getBuf( blk ); }
224
259k
  const CPelBuf     getSharedBuf(const CompArea& blk,      const PictureType type)   const { return ( !blk.valid() ) ? PelBuf() : m_sharedBufs[ type ]->getBuf( blk ); }
225
         PelUnitBuf getSharedBuf(const UnitArea& unit,     const PictureType type);
226
  const CPelUnitBuf getSharedBuf(const UnitArea& unit,     const PictureType type) const;
227
228
public:
229
  CodingStructure*              cs;
230
  const VPS*                    vps;
231
  const DCI*                    dci;
232
  ParameterSetMap<APS>          picApsMap;
233
  std::deque<Slice*>            slices;
234
  std::vector<const Slice*>     ctuSlice;
235
  SEIMessages                   SEIs;
236
  BlkStat                       picBlkStat;
237
  std::vector<OutputBitstream>  sliceDataStreams;
238
239
  bool                          isInitDone;
240
  std::atomic_bool              isReconstructed;    // usable as a reference: reconstruction, motion and ALF APSs final
241
  std::atomic_bool              isEncoded;          // finalizePicture done too: slice data written, picture encoder free
242
  bool                          isBorderExtended;
243
  bool                          isReferenced;
244
  bool                          isNeededForOutput;
245
  bool                          isFinished;
246
  bool                          isLongTerm;
247
  bool                          isFlush;
248
  bool                          isInProcessList;
249
  bool                          precedingDRAP; // preceding a DRAP picture in decoding order
250
251
  const GOPEntry*               gopEntry;
252
253
  int                           refCounter;
254
  int                           poc;
255
  unsigned                      TLayer;
256
  int                           layerId;
257
  bool                          isSubPicBorderSaved;
258
  int                           sliceDataNumBins;
259
  uint64_t                      cts;
260
  bool                          ctsValid;
261
  int64_t                       picsInMissing;      // summed up missing frames on input
262
  int64_t                       picOutOffset;       // signalization of pic offset to re-calc dts
263
  bool                          isPreAnalysis;
264
  int64_t                       encStartOrder;      // 1, 2, ...: the order in which the pictures got a picture encoder (per EncGOP)
265
266
  PicShared*                    m_picShared;
267
268
  PelStorage                    m_picBufs[ NUM_PIC_TYPES ];
269
  PelStorage*                   m_sharedBufs[ NUM_PIC_TYPES ];
270
  PelStorage*                   m_bufsOrigPrev[ NUM_QPA_PREV_FRAMES ];
271
272
  std::vector<double>           ctuQpaLambda;
273
  std::vector<int>              ctuAdaptedQP;
274
  int                           gopAdaptedQP; // QP offset of GOP (delta relative to base QP)
275
  bool                          force2ndOrder; // force 2nd-order high-pass in activity calc.
276
  bool                          isSceneCutGOP;
277
  bool                          isSceneCutCheckAdjQP;
278
  bool                          isMeanQPLimited;
279
  std::mutex                    wppMutex;
280
  int                           picInitialQP;
281
  double                        picInitialLambda;
282
  double                        picQPFraction; // fractional part of the picture QP with a fractional --qp, applied by QPA
283
  int16_t                       picMemorySTA;
284
  PicVisAct                     picVA;
285
  std::vector<LostGrainSpectrum> lostGrainSpectra[ MAX_NUM_COMP ];   // ascending mean8; from the picture's end to its SEI
286
  double                        psnr[MAX_NUM_COMP];
287
  double                        mse [MAX_NUM_COMP];
288
289
  StopClock                     encTime;
290
  bool                          isSccWeak;
291
  bool                          isSccStrong;
292
  bool                          useME;
293
  bool                          useMCTF;
294
  bool                          useTS;
295
  bool                          useBDPCM;
296
  bool                          useIBC;
297
  bool                          useSAO;
298
  bool                          useNumRefs;
299
  bool                          useSelectiveRdoq;
300
  int                           useFastMrg;
301
  int                           useQtbttSpeedUpMode;
302
  int                           actualHeadBits;
303
  int                           actualTotalBits;
304
  std::shared_ptr<EncRCPic>     encRCPic;
305
  PicApsGlobal*                 picApsGlobal;
306
  PicApsGlobal*                 refApsGlobal;
307
308
  std::vector<SAOBlkParam>      m_sao[ 2 ];
309
  std::vector<uint8_t>          m_alfCtuEnabled[ MAX_NUM_COMP ];
310
  std::vector<short>            m_alfCtbFilterIndex;
311
  std::vector<uint8_t>          m_alfCtuAlternative[ MAX_NUM_COMP ];
312
  std::vector<std::atomic<int>>*  m_tileColsDone = nullptr;
313
  // While the picture is encoded: the bottom CTU_LINE_BOTTOM_ROWS luma rows (and the chroma rows beside them) of every CTU
314
  // line, unfiltered, owned by the slice encoder. Null otherwise; getRecoAboveCtuTop then reads the reconstruction.
315
  PelStorage*                   m_ctuLineBottoms = nullptr;
316
  // the deepest of those readers, EDO, looks 8 luma rows above a CU
317
  static constexpr int          CTU_LINE_BOTTOM_ROWS = 8;
318
319
  void*                         userData;
320
321
public:
322
  Slice*          allocateNewSlice();
323
  Slice*          swapSliceObject( Slice* p, uint32_t i );
324
325
9.30k
  SAOBlkParam    *getSAO    (int id = 0)                     { return &m_sao[id][0]; };
326
2.86k
  void            resizeSAO (unsigned numEntries, int dstid) { m_sao[dstid].resize(numEntries); }
327
0
  void            copySAO   (const Picture& src, int dstid)  { std::copy(src.m_sao[0].begin(), src.m_sao[0].end(), m_sao[dstid].begin()); }
328
329
  void            resizeAlfCtuBuffers( int numEntries );
330
};
331
332
int calcAndPrintHashStatus(const CPelUnitBuf& pic, const SEIDecodedPictureHash* pictureHashSEI, const BitDepths &bitDepths, const vvencMsgLevel msgl, MsgLog& logger );
333
334
typedef std::list<Picture*> PicList;
335
336
} // namespace vvenc
337
338
//! \}
339