Coverage Report

Created: 2026-04-01 07:49

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