Coverage Report

Created: 2026-04-01 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vvdec/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) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC 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
43
/** \file     Contexts.h
44
 *  \brief    Classes providing probability descriptions and contexts (header)
45
 */
46
47
#pragma once
48
49
#include "CommonDef.h"
50
51
#include <array>
52
#include <vector>
53
54
namespace vvdec
55
{
56
57
static constexpr int     PROB_BITS   = 15;   // Nominal number of bits to represent probabilities
58
static constexpr int     PROB_BITS_0 = 10;   // Number of bits to represent 1st estimate
59
static constexpr int     PROB_BITS_1 = 14;   // Number of bits to represent 2nd estimate
60
static constexpr int     MASK_0      = ~(~0u << PROB_BITS_0) << (PROB_BITS - PROB_BITS_0);
61
static constexpr int     MASK_1      = ~(~0u << PROB_BITS_1) << (PROB_BITS - PROB_BITS_1);
62
static constexpr uint8_t DWS         = 8;   // 0x47 Default window sizes
63
static constexpr uint8_t CNU         = 35;
64
65
class ProbModelTables
66
{
67
protected:
68
  static const uint8_t      m_RenormTable_32  [ 32];          // Std         MP   MPI
69
};
70
71
class BinProbModel : public ProbModelTables
72
{
73
public:
74
  BinProbModel()
75
0
  {
76
0
    uint16_t half = 1 << (PROB_BITS - 1);
77
0
    m_state[0]    = half;
78
0
    m_state[1]    = half;
79
0
    m_rate0 = 0;
80
0
    m_rate1 = DWS;
81
0
    m_delta0[0] = ((0xFFFFU) >> (16 - m_rate0));
82
0
    m_delta0[1] = ((0xFFFFU) >> (16 - PROB_BITS));
83
0
    m_delta1[0] = ((0xFFFFU) >> (16 - m_rate1));
84
0
    m_delta1[1] = ((0xFFFFU) >> (16 - PROB_BITS));
85
0
  }
86
0
  ~BinProbModel() {}
87
public:
88
  void            init              ( int qp, int initId );
89
  void update(unsigned bin)
90
0
  {
91
//    m_state[0] -= (m_state[0] >> m_rate0) & MASK_0;
92
//    m_state[1] -= (m_state[1] >> m_rate1) & MASK_1;
93
//    m_state[0] += ( ( bin ? 0x7fffu : 0x0u ) >> m_rate0 ) & MASK_0;
94
//    m_state[1] += ( ( bin ? 0x7fffu : 0x0u ) >> m_rate1 ) & MASK_1;
95
0
    int delta0 = m_delta0[bin] - m_state[0];
96
0
    int delta1 = m_delta1[bin] - m_state[1];
97
0
    delta0 = delta0 >> m_rate0;
98
0
    delta1 = delta1 >> m_rate1;
99
0
    m_state[0] += delta0 *(1<< 5);
100
0
    m_state[1] += delta1 *2;
101
0
  }
102
  void setLog2WindowSize(uint8_t log2WindowSize)
103
0
  {
104
0
    m_rate0 = 2 + ((log2WindowSize >> 2) & 3);
105
0
    m_rate1 = 3 + m_rate0 + (log2WindowSize & 3);
106
0
    CHECK(m_rate1 > 9, "Second window size is too large!");
107
0
    m_rate0 += 5;
108
0
    m_rate1 += 1;
109
0
    m_delta0[0] = ((0xFFFFU) >> (16 - m_rate0));
110
0
    m_delta1[0] = ((0xFFFFU) >> (16 - m_rate1));
111
0
  }
112
public:
113
0
  uint8_t state() const { return (m_state[0] + m_state[1]) >> 8; }
114
0
  uint8_t mps() const { return state() >> 7; }
115
  uint8_t lps( unsigned range ) const
116
0
  {
117
0
    uint16_t q = state();
118
0
    if (q & 0x80)
119
0
      q = q ^ 0xff;
120
0
    return ((q >> 2) * (range >> 5) >> 1) + 4;
121
0
  }
122
  void lpsmps( unsigned range, unsigned& lps, unsigned& bin ) const
123
0
  {
124
0
    const uint16_t q = (m_state[0] + m_state[1]) >> 8;
125
0
    bin = q >> 7;
126
    
127
0
    lps = ((((q ^ (-static_cast<int>(bin) & 0xff)) >> 2) * (range >> 5)) >> 1) + 4;
128
    
129
    //if( q & 0x80 )
130
    //{
131
    //  CHECK( lps != ( ( ( ( q ^ 0xff ) >> 2 ) * ( range >> 5 ) >> 1 ) + 4 ), "" );
132
    //  lps = ( ( ( q ^ 0xff ) >> 2 ) * ( range >> 5 ) >> 1 ) + 4;
133
    //}
134
    //else
135
    //{
136
    //  CHECK( lps != ( ( ( q >> 2 ) * ( range >> 5 ) >> 1 ) + 4 ), "" );
137
    //  lps = ( ( q >> 2 ) * ( range >> 5 ) >> 1 ) + 4;
138
    //}
139
0
  }
140
0
  static uint8_t  getRenormBitsLPS  ( unsigned LPS )                    { return    m_RenormTable_32  [LPS>>3]; }
141
0
  static uint8_t  getRenormBitsRange( unsigned range )                  { return    1; }
142
private:
143
  uint16_t m_state[2];
144
  uint16_t m_rate0;
145
  uint16_t m_rate1;
146
  uint16_t m_delta0[2];
147
  uint16_t m_delta1[2];
148
};
149
150
151
class CtxSet
152
{
153
public:
154
20.9k
  CtxSet( uint16_t offset, uint16_t size ) : Offset( offset ), Size( size ) {}
155
  CtxSet( std::initializer_list<CtxSet> ctxSets );
156
157
  uint16_t operator()() const
158
0
  {
159
0
    return Offset;
160
0
  }
161
  uint16_t operator()( uint16_t inc ) const
162
0
  {
163
0
    CHECKD( inc >= Size, "Specified context increment (" << inc << ") exceed range of context set [0;" << Size - 1 << "]." );
164
165
0
    return Offset + inc;
166
0
  }
167
168
private:
169
  uint16_t Offset;
170
  uint16_t Size;
171
};
172
173
174
class ContextSetCfg
175
{
176
public:
177
  // context sets: specify offset and size
178
  static const CtxSet   SplitFlag;
179
  static const CtxSet   SplitQtFlag;
180
  static const CtxSet   SplitHvFlag;
181
  static const CtxSet   Split12Flag;
182
  static const CtxSet   ModeConsFlag;
183
  static const CtxSet   SkipFlag;
184
  static const CtxSet   MergeFlag;
185
  static const CtxSet   RegularMergeFlag;
186
  static const CtxSet   MergeIdx;
187
  static const CtxSet   PredMode;
188
  static const CtxSet   MultiRefLineIdx;
189
  static const CtxSet   IntraLumaPlanarFlag;
190
  static const CtxSet   CclmModeFlag;
191
  static const CtxSet   CclmModeIdx;
192
  static const CtxSet   IPredMode       [2];    // [ ChannelType ]
193
  static const CtxSet   MipFlag;
194
  static const CtxSet   DeltaQP;
195
  static const CtxSet   InterDir;
196
  static const CtxSet   RefPic;
197
  static const CtxSet   MmvdFlag;
198
  static const CtxSet   MmvdMergeIdx;
199
  static const CtxSet   MmvdStepMvpIdx;
200
  static const CtxSet   SubblockMergeFlag;
201
  static const CtxSet   AffineFlag;
202
  static const CtxSet   AffineType;
203
  static const CtxSet   AffMergeIdx;
204
  static const CtxSet   Mvd;
205
  static const CtxSet   BDPCMMode;
206
  static const CtxSet   QtRootCbf;
207
  static const CtxSet   ACTFlag;
208
  static const CtxSet   QtCbf           [3];    // [ channel ]
209
  static const CtxSet   SigCoeffGroup   [2];    // [ ChannelType ]
210
  static const CtxSet   LastX           [2];    // [ ChannelType ]
211
  static const CtxSet   LastY           [2];    // [ ChannelType ]
212
  static const CtxSet   SigFlag         [6];    // [ ChannelType + State ]
213
  static const CtxSet   ParFlag         [2];    // [ ChannelType ]
214
  static const CtxSet   GtxFlag         [4];    // [ ChannelType + x ]
215
  static const CtxSet   TsSigCoeffGroup;
216
  static const CtxSet   TsSigFlag;
217
  static const CtxSet   TsParFlag;
218
  static const CtxSet   TsGtxFlag;
219
  static const CtxSet   TsLrg1Flag;
220
  static const CtxSet   TsResidualSign;
221
  static const CtxSet   MVPIdx;
222
  static const CtxSet   SaoMergeFlag;
223
  static const CtxSet   SaoTypeIdx;
224
  static const CtxSet   MTSIndex;
225
  static const CtxSet   LFNSTIdx;
226
  static const CtxSet   RdpcmFlag;
227
  static const CtxSet   RdpcmDir;
228
  static const CtxSet   SbtFlag;
229
  static const CtxSet   SbtQuadFlag;
230
  static const CtxSet   SbtHorFlag;
231
  static const CtxSet   SbtPosFlag;
232
  static const CtxSet   ChromaQpAdjFlag;
233
  static const CtxSet   ChromaQpAdjIdc;
234
  static const CtxSet   ImvFlag;
235
  static const CtxSet   CcAlfFilterControlFlag;
236
  static const CtxSet   BcwIdx;
237
  static const CtxSet   ctbAlfFlag;
238
  static const CtxSet   ctbAlfAlternative;
239
  static const CtxSet   AlfUseTemporalFilt;
240
  static const CtxSet   CiipFlag;
241
  static const CtxSet   SmvdFlag;
242
  static const CtxSet   IBCFlag;
243
  static const CtxSet   ISPMode;
244
  static const CtxSet   JointCbCrFlag;
245
  static const unsigned NumberOfContexts;
246
247
  // combined sets for less complex copying
248
  // NOTE: The contained CtxSet's should directly follow each other in the initalization list;
249
  //       otherwise, you will copy more elements than you want !!!
250
  static const CtxSet   Sao;
251
  static const CtxSet   Alf;
252
253
public:
254
  static const std::vector<uint8_t>&  getInitTable( unsigned initId );
255
256
private:
257
  static std::array<std::vector<uint8_t>, NUMBER_OF_SLICE_TYPES + 1> sm_InitTables;
258
  static CtxSet addCtxSet( std::initializer_list<std::initializer_list<uint8_t> > initSet2d );
259
};
260
261
class Ctx : public ContextSetCfg
262
{
263
public:
264
0
  Ctx() = default;
265
266
  void init( int qp, int initId );
267
268
0
  const BinProbModel& operator[]( unsigned ctxId ) const { return m_CtxBuffer[ctxId]; }
269
0
        BinProbModel& operator[]( unsigned ctxId )       { return m_CtxBuffer[ctxId]; }
270
271
private:
272
  static_vector<BinProbModel, 372> m_CtxBuffer{ ContextSetCfg::NumberOfContexts };
273
};
274
275
}