Coverage Report

Created: 2026-04-01 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vvenc/source/Lib/CommonLib/Contexts.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     Contexts.h
43
 *  \brief    Classes providing probability descriptions and contexts (header)
44
 */
45
46
#pragma once
47
48
#include "CommonDef.h"
49
#include "Slice.h"
50
51
#include <vector>
52
53
//! \ingroup CommonLib
54
//! \{
55
56
namespace vvenc {
57
58
static constexpr int     PROB_BITS   = 15;   // Nominal number of bits to represent probabilities
59
static constexpr int     PROB_BITS_0 = 10;   // Number of bits to represent 1st estimate
60
static constexpr int     PROB_BITS_1 = 14;   // Number of bits to represent 2nd estimate
61
static constexpr int     MASK_0      = ~(~0u << PROB_BITS_0) << (PROB_BITS - PROB_BITS_0);
62
static constexpr int     MASK_1      = ~(~0u << PROB_BITS_1) << (PROB_BITS - PROB_BITS_1);
63
static constexpr uint8_t DWS         = 8;   // 0x47 Default window sizes
64
65
struct BinFracBits
66
{
67
  uint32_t intBits[2];
68
};
69
70
71
class ProbModelTables
72
{
73
protected:
74
  static const BinFracBits m_binFracBits[256];
75
  static const uint8_t      m_RenormTable_32  [ 32];          // Std         MP   MPI
76
};
77
78
79
class BinProbModelBase : public ProbModelTables
80
{
81
public:
82
0
  static uint32_t estFracBitsEP ()                    { return  (       1 << SCALE_BITS ); }
83
0
  static uint32_t estFracBitsEP ( unsigned numBins )  { return  ( numBins << SCALE_BITS ); }
84
};
85
86
87
class BinProbModel : public BinProbModelBase
88
{
89
public:
90
  BinProbModel()
91
0
  {
92
0
    uint16_t half = 1 << (PROB_BITS - 1);
93
0
    m_state[0]    = half;
94
0
    m_state[1]    = half;
95
0
    m_rate        = DWS;
96
0
  }
97
98
public:
99
  void            init              ( int qp, int initId );
100
  void update(unsigned bin)
101
0
  {
102
0
    int rate0 = m_rate >> 4;
103
0
    int rate1 = m_rate & 15;
104
105
0
    m_state[0] -= ( m_state[0] >> rate0 ) & MASK_0;
106
0
    m_state[1] -= ( m_state[1] >> rate1 ) & MASK_1;
107
108
0
    m_state[0] += ( ( -static_cast< int >( bin ) & 0x7fffu ) >> rate0 ) & MASK_0;
109
0
    m_state[1] += ( ( -static_cast< int >( bin ) & 0x7fffu ) >> rate1 ) & MASK_1;
110
0
  }
111
  void setLog2WindowSize(uint8_t log2WindowSize)
112
0
  {
113
0
    int rate0 = 2 + ((log2WindowSize >> 2) & 3);
114
0
    int rate1 = 3 + rate0 + (log2WindowSize&  3);
115
0
    m_rate    = 16 * rate0 + rate1;
116
0
    CHECK(rate1 > 9, "Second window size is too large!");
117
0
  }
118
  void estFracBitsUpdate(unsigned bin, uint64_t &b)
119
0
  {
120
0
    b += estFracBits(bin);
121
0
    update(bin);
122
0
  }
123
0
  uint32_t        estFracBits(unsigned bin) const { return getFracBitsArray().intBits[bin]; }
124
0
  static uint32_t estFracBitsTrm(unsigned bin) { return (bin ? 0x3bfbb : 0x0010c); }
125
0
  const BinFracBits &getFracBitsArray() const { return m_binFracBits[state()]; }
126
public:
127
0
  uint8_t state() const { return (m_state[0] + m_state[1]) >> 8; }
128
0
  uint8_t mps() const { return state() >> 7; }
129
  uint8_t getLPS(unsigned range) const
130
0
  {
131
0
    uint16_t q = state();
132
0
    if (q & 0x80)
133
0
      q = q ^ 0xff;
134
0
    return ((q >> 2) * (range >> 5) >> 1) + 4;
135
0
  }
136
0
  static uint8_t  getRenormBitsLPS  ( unsigned LPS )                    { return    m_RenormTable_32  [LPS>>3]; }
137
  //static uint8_t  getRenormBitsRange( unsigned range )                  { return    1; }
138
0
  uint16_t getState() const { return m_state[0] + m_state[1]; }
139
  void     setState(uint16_t pState)
140
0
  {
141
0
    m_state[0] = (pState >> 1) & MASK_0;
142
0
    m_state[1] = (pState >> 1) & MASK_1;
143
0
  }
144
public:
145
  uint64_t estFracExcessBits(const BinProbModel &r) const
146
0
  {
147
0
    int n = 2 * state() + 1;
148
0
    return ((512 - n) * r.estFracBits(0) + n * r.estFracBits(1) + 256) >> 9;
149
0
  }
150
private:
151
  uint16_t m_state[2];
152
  uint8_t  m_rate;
153
};
154
155
156
class CtxSet
157
{
158
public:
159
20.9k
  CtxSet( uint16_t offset, uint16_t size ) : Offset( offset ), Size( size ) {}
160
1.28k
  CtxSet( const CtxSet& ctxSet ) : Offset( ctxSet.Offset ), Size( ctxSet.Size ) {}
161
  CtxSet( std::initializer_list<CtxSet> ctxSets );
162
public:
163
  uint16_t  operator()  ()  const
164
0
  {
165
0
    return Offset;
166
0
  }
167
  uint16_t  operator()  ( uint16_t inc )  const
168
0
  {
169
0
    CHECKD( inc >= Size, "Specified context increment (" << inc << ") exceed range of context set [0;" << Size - 1 << "]." );
170
0
    return Offset + inc;
171
0
  }
172
  bool operator== ( const CtxSet& ctxSet ) const
173
0
  {
174
0
    return ( Offset == ctxSet.Offset && Size == ctxSet.Size );
175
0
  }
176
  bool operator!= ( const CtxSet& ctxSet ) const
177
0
  {
178
0
    return ( Offset != ctxSet.Offset || Size != ctxSet.Size );
179
0
  }
180
0
  uint16_t size() const { return Size; }
181
public:
182
  uint16_t  Offset;
183
  uint16_t  Size;
184
};
185
186
187
class ContextSetCfg
188
{
189
public:
190
  // context sets: specify offset and size
191
  static const CtxSet   SplitFlag;
192
  static const CtxSet   SplitQtFlag;
193
  static const CtxSet   SplitHvFlag;
194
  static const CtxSet   Split12Flag;
195
  static const CtxSet   ModeConsFlag;
196
  static const CtxSet   SkipFlag;
197
198
  // keep these together to
199
  static const CtxSet   MergeFlag;
200
  static const CtxSet   RegularMergeFlag;
201
  static const CtxSet   MergeIdx;
202
  static const CtxSet   MmvdFlag;
203
  static const CtxSet   MmvdMergeIdx;
204
  static const CtxSet   MmvdStepMvpIdx;
205
  static const CtxSet   SubblockMergeFlag;
206
  static const CtxSet   AffMergeIdx;
207
208
  static const CtxSet   PredMode;
209
  static const CtxSet   CclmModeFlag;  
210
  static const CtxSet   CclmModeIdx;
211
  static const CtxSet   IntraChromaPredMode;
212
213
  // keep these for single cpy
214
  static const CtxSet   IntraLumaMpmFlag;
215
  static const CtxSet   IntraLumaPlanarFlag;
216
  static const CtxSet   MultiRefLineIdx;
217
  static const CtxSet   MipFlag;
218
  static const CtxSet   ISPMode;
219
220
  static const CtxSet   DeltaQP;
221
  static const CtxSet   InterDir;
222
  static const CtxSet   RefPic;
223
  static const CtxSet   AffineFlag;
224
  static const CtxSet   AffineType;
225
  static const CtxSet   Mvd;
226
  static const CtxSet   BDPCMMode;
227
  static const CtxSet   QtRootCbf;
228
  static const CtxSet   ACTFlag;
229
  static const CtxSet   QtCbf           [3];    // [ channel ]
230
  static const CtxSet   SigCoeffGroup   [2];    // [ ChannelType ]
231
  static const CtxSet   LastX           [2];    // [ ChannelType ]
232
  static const CtxSet   LastY           [2];    // [ ChannelType ]
233
  static const CtxSet   SigFlag         [6];    // [ ChannelType + State ]
234
  static const CtxSet   ParFlag         [2];    // [ ChannelType ]
235
  static const CtxSet   GtxFlag         [4];    // [ ChannelType + x ]
236
  static const CtxSet   TsSigCoeffGroup;
237
  static const CtxSet   TsSigFlag;
238
  static const CtxSet   TsParFlag;
239
  static const CtxSet   TsGtxFlag;
240
  static const CtxSet   TsLrg1Flag;
241
  static const CtxSet   TsResidualSign;
242
  static const CtxSet   MVPIdx;
243
  static const CtxSet   SaoMergeFlag;
244
  static const CtxSet   SaoTypeIdx;
245
  static const CtxSet   TransformSkipFlag;
246
  static const CtxSet   MTSIdx;
247
  static const CtxSet   LFNSTIdx;
248
  static const CtxSet   PLTFlag;
249
  static const CtxSet   SbtFlag;
250
  static const CtxSet   SbtQuadFlag;
251
  static const CtxSet   SbtHorFlag;
252
  static const CtxSet   SbtPosFlag;
253
  static const CtxSet   ChromaQpAdjFlag;
254
  static const CtxSet   ChromaQpAdjIdc;
255
  static const CtxSet   ImvFlag;
256
  static const CtxSet   BcwIdx;
257
  static const CtxSet   ctbAlfFlag;
258
  static const CtxSet   ctbAlfAlternative;
259
  static const CtxSet   AlfUseTemporalFilt;
260
  static const CtxSet   CcAlfFilterControlFlag;
261
  static const CtxSet   CiipFlag;
262
  static const CtxSet   SmvdFlag;
263
  static const CtxSet   IBCFlag;
264
  static const CtxSet   JointCbCrFlag;
265
  static const unsigned NumberOfContexts;
266
267
  // combined sets for less complex copying
268
  // NOTE: The contained CtxSet's should directly follow each other in the initalization list;
269
  //       otherwise, you will copy more elements than you want !!!
270
  static const CtxSet   Sao;
271
  static const CtxSet   Alf;
272
public:
273
  static const std::vector<uint8_t>&  getInitTable( unsigned initId );
274
private:
275
  static std::vector<std::vector<uint8_t> > sm_InitTables;
276
  static CtxSet addCtxSet( std::initializer_list<std::initializer_list<uint8_t> > initSet2d );
277
};
278
279
class CtxStore
280
{
281
public:
282
  CtxStore();
283
  CtxStore( bool dummy );
284
  CtxStore( const CtxStore& ctxStore );
285
public:
286
0
  void copyFrom   ( const CtxStore& src )                        { checkInit(); ::memcpy( m_Ctx,               src.m_Ctx,               sizeof( BinProbModel ) * ContextSetCfg::NumberOfContexts ); }
287
0
  void copyFrom   ( const CtxStore& src, const CtxSet& ctxSet )  { checkInit(); ::memcpy( m_Ctx+ctxSet.Offset, src.m_Ctx+ctxSet.Offset, sizeof( BinProbModel ) * ctxSet.Size ); }
288
  void init       ( int qp, int initId );
289
  void setWinSizes( const std::vector<uint8_t>&   log2WindowSizes );
290
  void loadPStates( const std::vector<uint16_t>&  probStates );
291
  void savePStates( std::vector<uint16_t>&        probStates )  const;
292
293
0
  const BinProbModel& operator[]      ( unsigned  ctxId  )  const { return m_Ctx[ctxId]; }
294
0
  BinProbModel&       operator[]      ( unsigned  ctxId  )        { return m_Ctx[ctxId]; }
295
  uint32_t            estFracBits     ( unsigned  bin,
296
0
                                        unsigned  ctxId  )  const { return m_Ctx[ctxId].estFracBits(bin); }
297
298
0
  const BinFracBits  &getFracBitsArray( unsigned  ctxId  )  const { return m_Ctx[ctxId].getFracBitsArray(); }
299
300
private:
301
0
  inline void checkInit() { if( m_Ctx ) return; m_CtxBuffer.resize( ContextSetCfg::NumberOfContexts ); m_Ctx = m_CtxBuffer.data(); }
302
private:
303
  std::vector<BinProbModel> m_CtxBuffer;
304
  BinProbModel*             m_Ctx;
305
};
306
307
typedef CtxStore FracBitsAccess;
308
309
310
class Ctx;
311
class SubCtx
312
{
313
  friend class Ctx;
314
public:
315
0
  SubCtx( const CtxSet& ctxSet, const Ctx& ctx ) : m_CtxSet( ctxSet          ), m_Ctx( ctx          ) {}
316
0
  SubCtx( const SubCtx& subCtx )                 : m_CtxSet( subCtx.m_CtxSet ), m_Ctx( subCtx.m_Ctx ) {}
317
  const SubCtx& operator= ( const SubCtx& ) = delete;
318
private:
319
  const CtxSet  m_CtxSet;
320
  const Ctx&    m_Ctx;
321
};
322
323
class Ctx : public ContextSetCfg
324
{
325
public:
326
  Ctx();
327
  Ctx( const BinProbModel*    dummy );
328
  Ctx( const Ctx&                 ctx   );
329
330
public:
331
  const Ctx& operator= ( const Ctx& ctx )
332
0
  {
333
0
    m_CtxStore  .copyFrom( ctx.m_CtxStore);
334
0
    return *this;
335
0
  }
336
337
  SubCtx operator= ( SubCtx&& subCtx )
338
0
  {
339
0
    m_CtxStore  .copyFrom( subCtx.m_Ctx.m_CtxStore,   subCtx.m_CtxSet );
340
0
    return std::move(subCtx);
341
0
  }
342
343
  void  init ( int qp, int initId )
344
0
  {
345
0
    m_CtxStore  .init( qp, initId );
346
0
  }
347
348
  void  loadPStates( const std::vector<uint16_t>& probStates )
349
0
  {
350
0
    m_CtxStore  .loadPStates( probStates );
351
0
  }
352
353
  void  savePStates( std::vector<uint16_t>& probStates ) const
354
0
  {
355
0
    m_CtxStore  .savePStates( probStates );
356
0
  }
357
358
  void  initCtxAndWinSize( unsigned ctxId, const Ctx& ctx, const uint8_t winSize )
359
0
  {
360
0
    m_CtxStore  [ctxId] = ctx.m_CtxStore      [ctxId];
361
0
    m_CtxStore  [ctxId] . setLog2WindowSize   (winSize);
362
0
  }
363
364
public:
365
0
  const Ctx&            getCtx       ()     const { return *this; }
366
0
  Ctx&                  getCtx       ()           { return *this; }
367
368
0
  explicit operator   const CtxStore&()     const { return m_CtxStore; }
369
0
  explicit operator         CtxStore&()           { return m_CtxStore; }
370
371
0
  const FracBitsAccess& getFracBitsAcess()  const { return m_CtxStore; }
372
373
private:
374
  CtxStore                      m_CtxStore;
375
};
376
377
378
379
typedef dynamic_cache<Ctx> CtxCache;
380
381
class TempCtx
382
{
383
  TempCtx( const TempCtx& ) = delete;
384
  const TempCtx& operator=( const TempCtx& ) = delete;
385
public:
386
0
  TempCtx ( CtxCache* cache )                     : m_ctx( *cache->get() ), m_cache( cache ) {}
387
0
  TempCtx ( CtxCache* cache, const Ctx& ctx    )  : m_ctx( *cache->get() ), m_cache( cache ) { m_ctx = ctx; }
388
0
  TempCtx ( CtxCache* cache, SubCtx&&   subCtx )  : m_ctx( *cache->get() ), m_cache( cache ) { m_ctx = std::forward<SubCtx>(subCtx); }
389
0
  ~TempCtx()                                      { m_cache->cache( &m_ctx ); }
390
0
  const Ctx& operator=( const Ctx& ctx )          { return ( m_ctx = ctx ); }
391
0
  SubCtx     operator=( SubCtx&&   subCtx )       { return m_ctx = std::forward<SubCtx>( subCtx ); }
392
0
  operator const Ctx& ()           const          { return m_ctx; }
393
0
  operator       Ctx& ()                          { return m_ctx; }
394
private:
395
  Ctx&      m_ctx;
396
  CtxCache* m_cache;
397
};
398
399
} // namespace vvenc
400
401
//! \}
402