Coverage Report

Created: 2026-04-01 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vvenc/source/Lib/CommonLib/BitStream.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) 2019-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVenC 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
/** \file     BitStream.h
43
    \brief    class for handling bitstream (header)
44
*/
45
46
#pragma once
47
48
#include "CommonDef.h"
49
#include <stdint.h>
50
#include <vector>
51
52
//! \ingroup CommonLib
53
//! \{
54
55
namespace vvenc {
56
57
// ====================================================================================================================
58
// Class definition
59
// ====================================================================================================================
60
/**
61
 * Model of a writable bitstream that accumulates bits to produce a
62
 * bytestream.
63
 */
64
class OutputBitstream
65
{
66
  /**
67
   * FIFO for storage of bytes.  Use:
68
   *  - fifo.push_back(x) to append words
69
   *  - fifo.clear() to empty the FIFO
70
   *  - &fifo.front() to get a pointer to the data array.
71
   *    NB, this pointer is only valid until the next push_back()/clear()
72
   */
73
  std::vector<uint8_t> m_fifo;
74
75
  uint32_t m_num_held_bits; /// number of bits not flushed to bytestream.
76
  uint8_t  m_held_bits; /// the bits held and not flushed to bytestream.
77
                             /// this value is always msb-aligned, bigendian.
78
public:
79
  // create / destroy
80
  OutputBitstream();
81
  ~OutputBitstream();
82
83
  // interface for encoding
84
  /**
85
   * append uiNumberOfBits least significant bits of uiBits to
86
   * the current bitstream
87
   */
88
  void        write           ( uint32_t uiBits, uint32_t uiNumberOfBits );
89
90
  /** insert one bits until the bitstream is byte-aligned */
91
  void        writeAlignOne   ();
92
93
  /** insert zero bits until the bitstream is byte-aligned */
94
  void        writeAlignZero  ();
95
96
  // utility functions
97
98
  /**
99
   * Return a pointer to the start of the byte-stream buffer.
100
   * Pointer is valid until the next write/flush/reset call.
101
   * NB, data is arranged such that subsequent bytes in the
102
   * bytestream are stored in ascending addresses.
103
   */
104
  uint8_t* getByteStream() const;
105
106
  /**
107
   * Return the number of valid bytes available from  getByteStream()
108
   */
109
  uint32_t getByteStreamLength();
110
111
  /**
112
   * Reset all internal state.
113
   */
114
  void clear();
115
116
  /**
117
   * returns the number of bits that need to be written to
118
   * achieve byte alignment.
119
   */
120
0
  int getNumBitsUntilByteAligned() const { return (8 - m_num_held_bits) & 0x7; }
121
122
  /**
123
   * Return the number of bits that have been written since the last clear()
124
   */
125
0
  uint32_t getNumberOfWrittenBits() const { return uint32_t(m_fifo.size()) * 8 + m_num_held_bits; }
126
127
  void insertAt(const OutputBitstream& src, uint32_t pos);
128
129
  /**
130
   * Return a reference to the internal fifo
131
   */
132
0
  std::vector<uint8_t>& getFIFO() { return m_fifo; }
133
134
0
  uint8_t getHeldBits() const { return m_held_bits; }
135
136
  //OutputBitstream& operator= (const OutputBitstream& src);
137
  /** Return a reference to the internal fifo */
138
0
  const std::vector<uint8_t>& getFIFO() const { return m_fifo; }
139
140
  void addSubstream( const OutputBitstream* pcSubstream );
141
  void writeByteAlignment();
142
143
  //! returns the number of start code emulations contained in the current buffer
144
  int countStartCodeEmulations();
145
};
146
147
/**
148
 * Model of an input bitstream that extracts bits from a predefined
149
 * bytestream.
150
 */
151
class InputBitstream
152
{
153
protected:
154
  std::vector<uint8_t> m_fifo; /// FIFO for storage of complete bytes
155
  std::vector<uint32_t>    m_emulationPreventionByteLocation;
156
157
  uint32_t m_fifo_idx; /// Read index into m_fifo
158
159
  uint32_t m_num_held_bits;
160
  uint8_t m_held_bits;
161
  uint32_t  m_numBitsRead;
162
163
public:
164
  /**
165
   * Create a new bitstream reader object that reads from buf.
166
   */
167
  InputBitstream();
168
0
  virtual ~InputBitstream() { }
169
  InputBitstream(const InputBitstream &src);
170
171
  void resetToStart();
172
173
  // interface for decoding
174
  void        pseudoRead      ( uint32_t uiNumberOfBits, uint32_t& ruiBits );
175
  void        read            ( uint32_t uiNumberOfBits, uint32_t& ruiBits );
176
  void        readByte        ( uint32_t &ruiBits )
177
0
  {
178
0
    CHECK( m_fifo_idx >= m_fifo.size(), "FIFO exceeded" );
179
0
    ruiBits = m_fifo[m_fifo_idx++];
180
0
#if ENABLE_TRACING
181
0
    m_numBitsRead += 8;
182
0
#endif
183
0
  }
184
185
  void        peekPreviousByte( uint32_t &byte )
186
0
  {
187
0
    CHECK( m_fifo_idx == 0, "FIFO empty" );
188
0
    byte = m_fifo[m_fifo_idx - 1];
189
0
  }
190
191
  uint32_t readOutTrailingBits ();
192
0
  uint8_t  getHeldBits() const { return m_held_bits; }
193
  OutputBitstream& operator= (const OutputBitstream& src);
194
0
  uint32_t getByteLocation              ( )                     { return m_fifo_idx                    ; }
195
196
  // Peek at bits in word-storage. Used in determining if we have completed reading of current bitstream and therefore slice in LCEC.
197
0
  uint32_t peekBits (uint32_t uiBits) { uint32_t tmp; pseudoRead(uiBits, tmp); return tmp; }
198
199
  // utility functions
200
0
  uint32_t read(uint32_t numberOfBits)      { uint32_t tmp; read(numberOfBits, tmp); return tmp; }
201
0
  uint32_t readByte()                   { uint32_t tmp; readByte( tmp ); return tmp; }
202
0
  uint32_t getNumBitsUntilByteAligned() { return m_num_held_bits & (0x7); }
203
0
  uint32_t getNumBitsLeft()             { return 8*((uint32_t)m_fifo.size() - m_fifo_idx) + m_num_held_bits; }
204
  InputBitstream *extractSubstream( uint32_t uiNumBits ); // Read the nominated number of bits, and return as a bitstream.
205
0
  uint32_t getNumBitsRead()            { return m_numBitsRead; }
206
  uint32_t readByteAlignment();
207
208
0
  void     pushEmulationPreventionByteLocation ( uint32_t pos )                         { m_emulationPreventionByteLocation.push_back( pos ); }
209
0
  uint32_t numEmulationPreventionBytesRead     ()                                   { return (uint32_t) m_emulationPreventionByteLocation.size();    }
210
0
  const std::vector<uint32_t> &getEmulationPreventionByteLocation  () const              { return m_emulationPreventionByteLocation;           }
211
0
  uint32_t getEmulationPreventionByteLocation  ( uint32_t idx )                         { return m_emulationPreventionByteLocation[ idx ];    }
212
0
  void     clearEmulationPreventionByteLocation()                                   { m_emulationPreventionByteLocation.clear();          }
213
0
  void     setEmulationPreventionByteLocation  ( const std::vector<uint32_t> &vec )     { m_emulationPreventionByteLocation = vec;            }
214
215
0
  const std::vector<uint8_t> &getFifo() const { return m_fifo; }
216
0
        std::vector<uint8_t> &getFifo()       { return m_fifo; }
217
};
218
219
} // namespace vvenc
220
221
//! \}
222