Coverage Report

Created: 2026-06-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vvdec/source/Lib/CommonLib/BitStream.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     BitStream.cpp
44
    \brief    class for handling bitstream
45
*/
46
47
#include "BitStream.h"
48
49
#include <stdint.h>
50
#include <vector>
51
#include <string.h>
52
#include <memory.h>
53
54
55
namespace vvdec
56
{
57
58
void InputBitstream::resetToStart()
59
6.76k
{
60
6.76k
  m_fifo_idx      = 0;
61
6.76k
  m_num_held_bits = 0;
62
6.76k
  m_held_bits     = 0;
63
#if ENABLE_TRACING
64
  m_numBitsRead   = 0;
65
#endif
66
6.76k
}
67
68
/**
69
 * read uiNumberOfBits from bitstream without updating the bitstream
70
 * state, storing the result in ruiBits.
71
 *
72
 * If reading uiNumberOfBits would overrun the bitstream buffer,
73
 * the bitstream is effectively padded with sufficient zero-bits to
74
 * avoid the overrun.
75
 */
76
uint32_t InputBitstream::peekBits( uint32_t uiNumberOfBits )
77
193
{
78
193
  auto saved_fifo_idx      = m_fifo_idx;
79
193
  auto saved_num_held_bits = m_num_held_bits;
80
193
  auto saved_held_bits     = m_held_bits;
81
#if ENABLE_TRACING
82
  auto saved_numBitsRead   = m_numBitsRead;
83
#endif
84
85
193
  uint32_t num_bits_to_read = std::min(uiNumberOfBits, getNumBitsLeft());
86
193
  uint32_t uiBits           = read( num_bits_to_read );
87
193
  uiBits <<= (uiNumberOfBits - num_bits_to_read);
88
89
193
  m_fifo_idx      = saved_fifo_idx;
90
193
  m_num_held_bits = saved_num_held_bits;
91
193
  m_held_bits     = saved_held_bits;
92
#if ENABLE_TRACING
93
  m_numBitsRead   = saved_numBitsRead;
94
#endif
95
193
  return uiBits;
96
193
}
97
98
99
uint32_t InputBitstream::read (uint32_t uiNumberOfBits)
100
1.84M
{
101
#if ENABLE_TRACING
102
  m_numBitsRead += uiNumberOfBits;
103
#endif
104
105
1.84M
  constexpr static uint64_t ONES = ~static_cast<uint64_t>( 0 );   // need to ensure 64 bits for the mask, because shift by 32 is UB for uin32_t
106
107
  /* NB, bits are extracted from the MSB of each byte. */
108
1.84M
  uint32_t retval = 0;
109
1.84M
  if (uiNumberOfBits <= m_num_held_bits)
110
1.72M
  {
111
    /* n=1, len(H)=7:   -VHH HHHH, shift_down=6, mask=0xfe
112
     * n=3, len(H)=7:   -VVV HHHH, shift_down=4, mask=0xf8
113
     */
114
1.72M
    retval = static_cast<uint32_t>( m_held_bits >> ( m_num_held_bits - uiNumberOfBits ) );
115
1.72M
    retval &= ~( ONES << uiNumberOfBits );
116
1.72M
    m_num_held_bits -= uiNumberOfBits;
117
118
1.72M
    return retval;
119
1.72M
  }
120
121
123k
  CHECK( uiNumberOfBits > 32, "Too many bits read" );
122
123
123k
  if( m_num_held_bits )
124
6.59k
  {
125
    /* all num_held_bits will go into retval
126
     *   => need to mask leftover bits from previous extractions
127
     *   => align retval with top of extracted word */
128
    /* n=5, len(H)=3: ---- -VVV, mask=0x07, shift_up=5-3=2,
129
     * n=9, len(H)=3: ---- -VVV, mask=0x07, shift_up=9-3=6 */
130
6.59k
    uiNumberOfBits -= m_num_held_bits;
131
6.59k
    retval = static_cast<uint32_t>( m_held_bits ) & ~( ONES << m_num_held_bits );   // we can cast to 32 bits, because the held bits are the rightmost bits
132
6.59k
    retval <<= uiNumberOfBits;
133
6.59k
  }
134
135
  /* number of whole bytes that need to be loaded to form retval */
136
  /* n=32, len(H)=0, load 4bytes, shift_down=0
137
   * n=32, len(H)=1, load 4bytes, shift_down=1
138
   * n=31, len(H)=1, load 4bytes, shift_down=1+1
139
   * n=8,  len(H)=0, load 1byte,  shift_down=0
140
   * n=8,  len(H)=3, load 1byte,  shift_down=3
141
   * n=5,  len(H)=1, load 1byte,  shift_down=1+3
142
   */
143
123k
  load_next_bits( uiNumberOfBits );
144
145
  /* resolve remainder bits */
146
123k
  m_num_held_bits -= uiNumberOfBits;
147
148
  /* copy required part of m_held_bits into retval */
149
123k
  retval |= static_cast<uint32_t>( m_held_bits >> m_num_held_bits );
150
151
123k
  return retval;
152
123k
}
153
154
uint32_t InputBitstream::readOutTrailingBits ()
155
3
{
156
3
  uint32_t count = 0;
157
3
  while( ( getNumBitsLeft() > 0 ) && ( getNumBitsUntilByteAligned() != 0 ) )
158
0
  {
159
0
    count++;
160
0
    read( 1 );
161
0
  }
162
3
  return count;
163
3
}
164
165
/**
166
 Extract substream from the current bitstream.
167
168
 \param  uiNumBits    number of bits to transfer
169
 */
170
std::unique_ptr<InputBitstream> InputBitstream::extractSubstream( uint32_t uiNumBits )
171
715
{
172
715
  std::unique_ptr<InputBitstream> substream( new InputBitstream );
173
174
715
  AlignedByteVec& buf = substream->getFifo();
175
715
  buf.reserve( ( uiNumBits + 7 ) / 8 + 1 );    // +1 because a zero byte might be added later
176
177
715
  const uint32_t uiNumBytes = uiNumBits / 8;   // don't round up here, because the remaing bits will be copied later
178
715
  if( m_num_held_bits == 0 )
179
1
  {
180
1
    CHECK( m_fifo_idx + uiNumBytes > m_fifo.size(), "Exceeded FIFO size" );
181
182
1
    std::size_t currentOutputBufferSize = buf.size();
183
1
    buf.resize( currentOutputBufferSize + uiNumBytes );
184
185
1
    const uint32_t uiNumBytesToReadFromFifo = std::min<uint32_t>( uiNumBytes, (uint32_t) m_fifo.size() - m_fifo_idx );
186
1
    memcpy( &( buf[currentOutputBufferSize] ), &( m_fifo[m_fifo_idx] ), uiNumBytesToReadFromFifo );
187
1
    m_fifo_idx += uiNumBytesToReadFromFifo;
188
189
1
    if( uiNumBytesToReadFromFifo != uiNumBytes )
190
0
    {
191
0
      memset( &( buf[currentOutputBufferSize + uiNumBytesToReadFromFifo] ), 0, uiNumBytes - uiNumBytesToReadFromFifo );
192
0
    }
193
1
  }
194
714
  else
195
714
  {
196
798k
    for( uint32_t ui = 0; ui < uiNumBytes; ui++ )
197
797k
    {
198
797k
      uint32_t uiByte = read( 8 );
199
797k
      buf.push_back( uiByte );
200
797k
    }
201
714
  }
202
203
715
  if( uiNumBits & 0x7 )
204
0
  {
205
0
    uint32_t uiByte = read( uiNumBits & 0x7 );
206
0
    uiByte <<= 8 - ( uiNumBits & 0x7 );
207
0
    buf.push_back( uiByte );
208
0
  }
209
715
  return substream;
210
715
}
211
212
uint32_t InputBitstream::readByteAlignment()
213
718
{
214
718
  uint32_t code = read( 1 );
215
718
  CHECK( code != 1, "Code is not '1'" );
216
217
716
  uint32_t numBits = getNumBitsUntilByteAligned();
218
716
  if(numBits)
219
709
  {
220
709
    CHECK( numBits > getNumBitsLeft(), "More bits available than left" );
221
709
    code = read( numBits );
222
709
    CHECK( code != 0, "Code not '0'" );
223
707
  }
224
714
  return numBits+1;
225
716
}
226
227
}