Coverage Report

Created: 2026-06-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vvdec/source/Lib/DecoderLib/BinDecoder.cpp
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     BinDecoder.cpp
44
 *  \brief    Low level binary symbol writer
45
 */
46
47
48
#include "BinDecoder.h"
49
#include "CommonLib/Rom.h"
50
#include "CommonLib/BitStream.h"
51
52
#include "CommonLib/dtrace_next.h"
53
54
#include <limits>
55
56
#define CNT_OFFSET 0
57
58
namespace vvdec
59
{
60
61
// BinDecoder uses the `signed >> 31` sign-extract idiom, which is implementation-defined
62
// for negative operands before C++20. Require two's-complement representation so the idiom
63
// behaves as intended (mandatory anyway in C++20).
64
static_assert( std::numeric_limits<int>::min() == -std::numeric_limits<int>::max() - 1,
65
               "two's complement signed integer representation required" );
66
static_assert( ( -1 >> 1 ) == -1,
67
               "right shift of negative number needs to be implemented as arithmetic shift." );
68
69
void BinDecoder::init( InputBitstream* bitstream )
70
6.14k
{
71
6.14k
  m_Bitstream = bitstream;
72
6.14k
}
73
74
75
void BinDecoder::uninit()
76
0
{
77
0
  m_Bitstream = 0;
78
0
}
79
80
81
void BinDecoder::start()
82
713
{
83
713
  CHECK( m_Bitstream->getNumBitsUntilByteAligned(), "Bitstream is not byte aligned." );
84
713
  m_Range       = 510;
85
713
  m_Value       = ( uint32_t( m_Bitstream->readByte() ) << 8 ) + m_Bitstream->readByte();
86
713
  m_bitsNeeded  = -8;
87
713
}
88
89
90
void BinDecoder::finish()
91
8
{
92
8
  unsigned lastByte = m_Bitstream->peekPreviousByte();
93
8
  CHECK( ( ( lastByte << ( 8 + m_bitsNeeded ) ) & 0xff ) != 0x80,
94
8
        "No proper stop/alignment pattern at end of CABAC stream." );
95
3
}
96
97
98
void BinDecoder::reset( int qp, int initId )
99
713
{
100
713
  m_Ctx.init( qp, initId );
101
713
  start();
102
713
}
103
104
105
unsigned BinDecoder::decodeBinEP()
106
573k
{
107
573k
  unsigned value = m_Value << 1;
108
573k
  if (++m_bitsNeeded >= 0) {
109
71.0k
    value += m_Bitstream->readByte();
110
71.0k
    m_bitsNeeded = -8;
111
71.0k
  }
112
113
573k
  unsigned bin = 0;
114
573k
  unsigned SR = m_Range << 7;
115
573k
  if (value >= SR) {
116
336k
    value -= SR;
117
336k
    bin = 1;
118
336k
  }
119
573k
  m_Value = value;
120
573k
  DTRACE(g_trace_ctx, D_CABAC,
121
573k
         "%d"
122
573k
         "  "
123
573k
         "%d"
124
573k
         "  EP=%d \n",
125
573k
         DTRACE_GET_COUNTER(g_trace_ctx, D_CABAC), m_Range, bin);
126
573k
  return bin;
127
573k
}
128
129
130
unsigned BinDecoder::decodeBinsEP( unsigned numBins )
131
399k
{
132
#if ENABLE_TRACING
133
  int numBinsOrig = numBins;
134
#endif
135
136
399k
  if( m_Range == 256 )
137
7.68k
  {
138
7.68k
    return decodeAlignedBinsEP( numBins );
139
7.68k
  }
140
391k
  unsigned remBins = numBins;
141
391k
  unsigned bins    = 0;
142
391k
  unsigned value   = m_Value;
143
391k
  unsigned range = m_Range;
144
391k
  int bitsNeeded = m_bitsNeeded;
145
438k
  while( remBins > 8 )
146
47.1k
  {
147
47.1k
    value       = ( value << 8 ) + ( m_Bitstream->readByte() << ( 8 + bitsNeeded ) );
148
47.1k
    unsigned SR =   range << 15;
149
424k
    for( int i = 0; i < 8; i++ )
150
377k
    {
151
377k
      bins += bins;
152
377k
      SR  >>= 1;
153
377k
      if( value >= SR )
154
187k
      {
155
187k
        bins    ++;
156
187k
        value -= SR;
157
187k
      }
158
377k
    }
159
47.1k
    remBins -= 8;
160
47.1k
  }
161
391k
  bitsNeeded   += (int)remBins;
162
391k
  value       <<= remBins;
163
391k
  if( bitsNeeded >= 0 )
164
108k
  {
165
108k
    value      += m_Bitstream->readByte() << bitsNeeded;
166
108k
    bitsNeeded -= 8;
167
108k
  }
168
391k
  unsigned SR = range << ( remBins + 7 );
169
1.27M
  for ( unsigned i = 0; i < remBins; i++ )
170
883k
  {
171
883k
    bins += bins;
172
883k
    SR  >>= 1;
173
883k
    if( value >= SR )
174
431k
    {
175
431k
      bins    ++;
176
431k
      value -= SR;
177
431k
    }
178
883k
  }
179
391k
  m_Value = value;
180
391k
  m_Range = range;
181
391k
  m_bitsNeeded = bitsNeeded;
182
#if ENABLE_TRACING
183
  for( int i = 0; i < numBinsOrig; i++ )
184
  {
185
    DTRACE( g_trace_ctx, D_CABAC, "%d" "  " "%d" "  EP=%d \n", DTRACE_GET_COUNTER( g_trace_ctx, D_CABAC ), m_Range, ( bins >> ( numBinsOrig - 1 - i ) ) & 1 );
186
  }
187
#endif
188
391k
  return bins;
189
399k
}
190
191
unsigned BinDecoder::decodeRemAbsEP(unsigned goRicePar, unsigned cutoff, int maxLog2TrDynamicRange)
192
145k
{
193
145k
  unsigned prefix = 0;
194
145k
  {
195
145k
    const unsigned  maxPrefix = 32 - maxLog2TrDynamicRange;
196
145k
    unsigned        codeWord = 0;
197
145k
    do
198
378k
    {
199
378k
      prefix++;
200
378k
      codeWord = decodeBinEP();
201
378k
    } while (codeWord && prefix < maxPrefix);
202
145k
    prefix -= 1 - (int)codeWord;
203
145k
  }
204
205
145k
  unsigned length = goRicePar, offset;
206
145k
  if (prefix < cutoff)
207
108k
  {
208
108k
    offset = prefix << goRicePar;
209
108k
  }
210
36.8k
  else
211
36.8k
  {
212
36.8k
    offset = (((1u << (prefix - cutoff)) + cutoff - 1) << goRicePar);
213
36.8k
    {
214
36.8k
      length += (prefix == (32u - maxLog2TrDynamicRange) ? maxLog2TrDynamicRange - goRicePar : prefix - cutoff);
215
36.8k
    }
216
36.8k
  }
217
145k
  return offset + decodeBinsEP(length);
218
145k
}
219
220
unsigned BinDecoder::decodeBinTrm()
221
64
{
222
64
  m_Range    -= 2;
223
64
  unsigned SR = m_Range << 7;
224
  
225
64
  DTRACE( g_trace_ctx, D_CABAC, "%d" " binTrm  range=" "%d" "  bin=%d\n" , DTRACE_GET_COUNTER( g_trace_ctx, D_CABAC ), m_Range, m_Value >= SR );
226
227
64
  if( m_Value >= SR )
228
8
  {
229
8
    return 1;
230
8
  }
231
56
  else
232
56
  {
233
56
    if( m_Range < 256 )
234
3
    {
235
3
      m_Range += m_Range;
236
3
      m_Value += m_Value;
237
3
      if( ++m_bitsNeeded == 0 )
238
1
      {
239
1
        m_Value      += m_Bitstream->readByte();
240
1
        m_bitsNeeded  = -8;
241
1
      }
242
3
    }
243
56
    return 0;
244
56
  }
245
64
}
246
247
248
void BinDecoder::align()
249
0
{
250
0
  m_Range = 256;
251
0
}
252
253
#if ENABLE_TRACING
254
unsigned int BinDecoder::getNumBitsRead() const
255
{
256
  return m_Bitstream->getNumBitsRead() + m_bitsNeeded;
257
}
258
#endif   // ENABLE_TRACING
259
260
unsigned BinDecoder::decodeAlignedBinsEP( unsigned numBins )
261
7.68k
{
262
#if ENABLE_TRACING
263
  int numBinsOrig = numBins;
264
#endif
265
7.68k
  unsigned remBins = numBins;
266
7.68k
  unsigned bins    = 0;
267
14.6k
  while( remBins > 0 )
268
6.98k
  {
269
    // The MSB of m_Value is known to be 0 because range is 256. Therefore:
270
    //   > The comparison against the symbol range of 128 is simply a test on the next-most-significant bit
271
    //   > "Subtracting" the symbol range if the decoded bin is 1 simply involves clearing that bit.
272
    //  As a result, the required bins are simply the <binsToRead> next-most-significant bits of m_Value
273
    //  (m_Value is stored MSB-aligned in a 16-bit buffer - hence the shift of 15)
274
    //
275
    //    m_Value = |0|V|V|V|V|V|V|V|V|B|B|B|B|B|B|B|        (V = usable bit, B = potential buffered bit (buffer refills when m_bitsNeeded >= 0))
276
    //
277
6.98k
    unsigned binsToRead = std::min<unsigned>( remBins, 8 ); //read bytes if able to take advantage of the system's byte-read function
278
6.98k
    unsigned binMask    = ( 1 << binsToRead ) - 1;
279
6.98k
    unsigned newBins    = ( m_Value >> (15 - binsToRead) ) & binMask;
280
6.98k
    bins                = ( bins    << binsToRead) | newBins;
281
6.98k
    m_Value             = ( m_Value << binsToRead) & 0x7FFF;
282
6.98k
    remBins            -= binsToRead;
283
6.98k
    m_bitsNeeded       += (int)binsToRead;
284
6.98k
    if( m_bitsNeeded >= 0 )
285
3.65k
    {
286
3.65k
      m_Value          |= m_Bitstream->readByte() << m_bitsNeeded;
287
3.65k
      m_bitsNeeded     -= 8;
288
3.65k
    }
289
6.98k
  }
290
#if ENABLE_TRACING
291
  for( int i = 0; i < numBinsOrig; i++ )
292
  {
293
    DTRACE( g_trace_ctx, D_CABAC, "%d" "  " "%d" "  " "EP=%d \n", DTRACE_GET_COUNTER( g_trace_ctx, D_CABAC ), m_Range, ( bins >> ( numBinsOrig - 1 - i ) ) & 1 );
294
  }
295
#endif
296
7.68k
  return bins;
297
7.68k
}
298
299
300
unsigned BinDecoder::decodeBin( unsigned ctxId )
301
6.73M
{
302
6.73M
  BinProbModel& rcProbModel = m_Ctx[ctxId];
303
304
6.73M
  unsigned bin, LPS;
305
6.73M
  uint32_t range      = m_Range;
306
6.73M
  uint32_t value      = m_Value;
307
6.73M
  int32_t  bitsNeeded = m_bitsNeeded;
308
309
6.73M
  rcProbModel.lpsmps( range, LPS, bin );
310
311
//  DTRACE( g_trace_ctx, D_CABAC, "%d" " xxx " "%d" "  " "[%d:%d]" "  " "%2d(MPS=%d)"  "  " , DTRACE_GET_COUNTER( g_trace_ctx, D_CABAC ), m_Range, m_Range-LPS, LPS, ( unsigned int )( rcProbModel.state() ), m_Value < ( ( m_Range - LPS ) << 7 ) );
312
6.73M
  DTRACE( g_trace_ctx, D_CABAC, "%d" " %d " "%d" "  " "[%d:%d]" "  " "%2d(MPS=%d)"  "  " , DTRACE_GET_COUNTER( g_trace_ctx, D_CABAC ), 666, range, range-LPS, LPS, ( unsigned int )( rcProbModel.state() ), value < ( ( range - LPS ) << 7 ) );
313
  //DTRACE( g_trace_ctx, D_CABAC, " %d " "%d" "  " "[%d:%d]" "  " "%2d(MPS=%d)"  "  ", DTRACE_GET_COUNTER( g_trace_ctx, D_CABAC ), m_Range, m_Range - LPS, LPS, (unsigned int)( rcProbModel.state() ), m_Value < ( ( m_Range - LPS ) << 7 ) );
314
315
6.73M
  range      -= LPS;
316
6.73M
  uint32_t SR = range << 7;
317
318
6.73M
  int b = ~( ( int( value ) - int( SR ) ) >> 31 );
319
6.73M
  int a = ~b & ( ( int( range ) - 256 ) >> 31 );
320
  //int b = -( Value >= SR );
321
  
322
6.73M
  int numBits  = ( a & rcProbModel.getRenormBitsRange( range ) ) | ( b & rcProbModel.getRenormBitsLPS( LPS ) );
323
  
324
6.73M
  value       -= b & SR;
325
6.73M
  value      <<= numBits;
326
  
327
6.73M
  range       &=  ~b;
328
6.73M
  range       |= ( b & LPS );
329
6.73M
  range      <<= numBits;
330
  
331
  // b    0 0 1 1
332
  // bin  0 1 0 1
333
  // res  0 1 1 0
334
335
  //bin          = ( ~b & bin ) | ( b & !bin );
336
6.73M
  bin         ^= b;
337
6.73M
  bin         &= 1;
338
  
339
6.73M
  bitsNeeded  += numBits & ( a | b );
340
  
341
6.73M
  const int c = ~(bitsNeeded >> 31);
342
6.73M
  value      += uint32_t( m_Bitstream->readByteFlag( c ) ) << ( bitsNeeded & 31 );
343
6.73M
  bitsNeeded -= c & 8;
344
  
345
  //if( Value < SR )
346
  //{
347
  //  // MPS path
348
  //  if( Range < 256 )
349
  //  {
350
  //    int numBits   = rcProbModel.getRenormBitsRange( Range );
351
  //    Range       <<= numBits;
352
  //    Value       <<= numBits;
353
  //    bitsNeeded   += numBits;
354
  //    if( bitsNeeded >= 0 )
355
  //    {
356
  //      Value      += m_Bitstream->readByte() << bitsNeeded;
357
  //      bitsNeeded -= 8;
358
  //    }
359
  //  }
360
  //}
361
  //else
362
  //{
363
  //  bin = !bin;
364
  //  // LPS path
365
  //  int numBits   = rcProbModel.getRenormBitsLPS( LPS );
366
  //  Value        -= SR;
367
  //  Value       <<= numBits;
368
  //  Range         = LPS     << numBits;
369
  //  bitsNeeded   += numBits;
370
  //  if( bitsNeeded >= 0 )
371
  //  {
372
  //    Value      += m_Bitstream->readByte() << bitsNeeded;
373
  //    bitsNeeded -= 8;
374
  //  }
375
  //}
376
377
6.73M
  m_Range      = range;
378
6.73M
  m_Value      = value;
379
6.73M
  m_bitsNeeded = bitsNeeded;
380
381
6.73M
  rcProbModel.update( bin );
382
6.73M
  DTRACE_WITHOUT_COUNT( g_trace_ctx, D_CABAC, "  -  " "%d" "\n", bin );
383
6.73M
  return  bin;
384
6.73M
}
385
386
}