Coverage Report

Created: 2026-06-15 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/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
0
{
71
0
  m_Bitstream = bitstream;
72
0
}
73
74
75
void BinDecoder::uninit()
76
0
{
77
0
  m_Bitstream = 0;
78
0
}
79
80
81
void BinDecoder::start()
82
0
{
83
0
  CHECK( m_Bitstream->getNumBitsUntilByteAligned(), "Bitstream is not byte aligned." );
84
0
  m_Range       = 510;
85
0
  m_Value       = ( uint32_t( m_Bitstream->readByte() ) << 8 ) + m_Bitstream->readByte();
86
0
  m_bitsNeeded  = -8;
87
0
}
88
89
90
void BinDecoder::finish()
91
0
{
92
0
  unsigned lastByte = m_Bitstream->peekPreviousByte();
93
0
  CHECK( ( ( lastByte << ( 8 + m_bitsNeeded ) ) & 0xff ) != 0x80,
94
0
        "No proper stop/alignment pattern at end of CABAC stream." );
95
0
}
96
97
98
void BinDecoder::reset( int qp, int initId )
99
0
{
100
0
  m_Ctx.init( qp, initId );
101
0
  start();
102
0
}
103
104
105
unsigned BinDecoder::decodeBinEP()
106
0
{
107
0
  unsigned value = m_Value << 1;
108
0
  if (++m_bitsNeeded >= 0) {
109
0
    value += m_Bitstream->readByte();
110
0
    m_bitsNeeded = -8;
111
0
  }
112
113
0
  unsigned bin = 0;
114
0
  unsigned SR = m_Range << 7;
115
0
  if (value >= SR) {
116
0
    value -= SR;
117
0
    bin = 1;
118
0
  }
119
0
  m_Value = value;
120
0
  DTRACE(g_trace_ctx, D_CABAC,
121
0
         "%d"
122
0
         "  "
123
0
         "%d"
124
0
         "  EP=%d \n",
125
0
         DTRACE_GET_COUNTER(g_trace_ctx, D_CABAC), m_Range, bin);
126
0
  return bin;
127
0
}
128
129
130
unsigned BinDecoder::decodeBinsEP( unsigned numBins )
131
0
{
132
#if ENABLE_TRACING
133
  int numBinsOrig = numBins;
134
#endif
135
136
0
  if( m_Range == 256 )
137
0
  {
138
0
    return decodeAlignedBinsEP( numBins );
139
0
  }
140
0
  unsigned remBins = numBins;
141
0
  unsigned bins    = 0;
142
0
  unsigned value   = m_Value;
143
0
  unsigned range = m_Range;
144
0
  int bitsNeeded = m_bitsNeeded;
145
0
  while( remBins > 8 )
146
0
  {
147
0
    value       = ( value << 8 ) + ( m_Bitstream->readByte() << ( 8 + bitsNeeded ) );
148
0
    unsigned SR =   range << 15;
149
0
    for( int i = 0; i < 8; i++ )
150
0
    {
151
0
      bins += bins;
152
0
      SR  >>= 1;
153
0
      if( value >= SR )
154
0
      {
155
0
        bins    ++;
156
0
        value -= SR;
157
0
      }
158
0
    }
159
0
    remBins -= 8;
160
0
  }
161
0
  bitsNeeded   += (int)remBins;
162
0
  value       <<= remBins;
163
0
  if( bitsNeeded >= 0 )
164
0
  {
165
0
    value      += m_Bitstream->readByte() << bitsNeeded;
166
0
    bitsNeeded -= 8;
167
0
  }
168
0
  unsigned SR = range << ( remBins + 7 );
169
0
  for ( unsigned i = 0; i < remBins; i++ )
170
0
  {
171
0
    bins += bins;
172
0
    SR  >>= 1;
173
0
    if( value >= SR )
174
0
    {
175
0
      bins    ++;
176
0
      value -= SR;
177
0
    }
178
0
  }
179
0
  m_Value = value;
180
0
  m_Range = range;
181
0
  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
0
  return bins;
189
0
}
190
191
unsigned BinDecoder::decodeRemAbsEP(unsigned goRicePar, unsigned cutoff, int maxLog2TrDynamicRange)
192
0
{
193
0
  unsigned prefix = 0;
194
0
  {
195
0
    const unsigned  maxPrefix = 32 - maxLog2TrDynamicRange;
196
0
    unsigned        codeWord = 0;
197
0
    do
198
0
    {
199
0
      prefix++;
200
0
      codeWord = decodeBinEP();
201
0
    } while (codeWord && prefix < maxPrefix);
202
0
    prefix -= 1 - (int)codeWord;
203
0
  }
204
205
0
  unsigned length = goRicePar, offset;
206
0
  if (prefix < cutoff)
207
0
  {
208
0
    offset = prefix << goRicePar;
209
0
  }
210
0
  else
211
0
  {
212
0
    offset = (((1u << (prefix - cutoff)) + cutoff - 1) << goRicePar);
213
0
    {
214
0
      length += (prefix == (32u - maxLog2TrDynamicRange) ? maxLog2TrDynamicRange - goRicePar : prefix - cutoff);
215
0
    }
216
0
  }
217
0
  return offset + decodeBinsEP(length);
218
0
}
219
220
unsigned BinDecoder::decodeBinTrm()
221
0
{
222
0
  m_Range    -= 2;
223
0
  unsigned SR = m_Range << 7;
224
  
225
0
  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
0
  if( m_Value >= SR )
228
0
  {
229
0
    return 1;
230
0
  }
231
0
  else
232
0
  {
233
0
    if( m_Range < 256 )
234
0
    {
235
0
      m_Range += m_Range;
236
0
      m_Value += m_Value;
237
0
      if( ++m_bitsNeeded == 0 )
238
0
      {
239
0
        m_Value      += m_Bitstream->readByte();
240
0
        m_bitsNeeded  = -8;
241
0
      }
242
0
    }
243
0
    return 0;
244
0
  }
245
0
}
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
0
{
262
#if ENABLE_TRACING
263
  int numBinsOrig = numBins;
264
#endif
265
0
  unsigned remBins = numBins;
266
0
  unsigned bins    = 0;
267
0
  while( remBins > 0 )
268
0
  {
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
0
    unsigned binsToRead = std::min<unsigned>( remBins, 8 ); //read bytes if able to take advantage of the system's byte-read function
278
0
    unsigned binMask    = ( 1 << binsToRead ) - 1;
279
0
    unsigned newBins    = ( m_Value >> (15 - binsToRead) ) & binMask;
280
0
    bins                = ( bins    << binsToRead) | newBins;
281
0
    m_Value             = ( m_Value << binsToRead) & 0x7FFF;
282
0
    remBins            -= binsToRead;
283
0
    m_bitsNeeded       += (int)binsToRead;
284
0
    if( m_bitsNeeded >= 0 )
285
0
    {
286
0
      m_Value          |= m_Bitstream->readByte() << m_bitsNeeded;
287
0
      m_bitsNeeded     -= 8;
288
0
    }
289
0
  }
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
0
  return bins;
297
0
}
298
299
300
unsigned BinDecoder::decodeBin( unsigned ctxId )
301
0
{
302
0
  BinProbModel& rcProbModel = m_Ctx[ctxId];
303
304
0
  unsigned bin, LPS;
305
0
  uint32_t range      = m_Range;
306
0
  uint32_t value      = m_Value;
307
0
  int32_t  bitsNeeded = m_bitsNeeded;
308
309
0
  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
0
  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
0
  range      -= LPS;
316
0
  uint32_t SR = range << 7;
317
318
0
  int b = ~( ( int( value ) - int( SR ) ) >> 31 );
319
0
  int a = ~b & ( ( int( range ) - 256 ) >> 31 );
320
  //int b = -( Value >= SR );
321
  
322
0
  int numBits  = ( a & rcProbModel.getRenormBitsRange( range ) ) | ( b & rcProbModel.getRenormBitsLPS( LPS ) );
323
  
324
0
  value       -= b & SR;
325
0
  value      <<= numBits;
326
  
327
0
  range       &=  ~b;
328
0
  range       |= ( b & LPS );
329
0
  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
0
  bin         ^= b;
337
0
  bin         &= 1;
338
  
339
0
  bitsNeeded  += numBits & ( a | b );
340
  
341
0
  const int c = ~(bitsNeeded >> 31);
342
0
  value      += uint32_t( m_Bitstream->readByteFlag( c ) ) << ( bitsNeeded & 31 );
343
0
  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
0
  m_Range      = range;
378
0
  m_Value      = value;
379
0
  m_bitsNeeded = bitsNeeded;
380
381
0
  rcProbModel.update( bin );
382
0
  DTRACE_WITHOUT_COUNT( g_trace_ctx, D_CABAC, "  -  " "%d" "\n", bin );
383
0
  return  bin;
384
0
}
385
386
}