/src/vvdec/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) 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.h |
44 | | \brief class for handling bitstream (header) |
45 | | */ |
46 | | |
47 | | #pragma once |
48 | | |
49 | | #include <stdint.h> |
50 | | #include <vector> |
51 | | #include <memory> |
52 | | #include "CommonDef.h" |
53 | | |
54 | | #if defined(TARGET_SIMD_X86) && SIMD_ENABLE |
55 | | # include "CommonDefX86.h" // needed for simde_bswap64, but don't just include simde-common.h, because it breaks other files |
56 | | #else |
57 | | # include "simde/simde-common.h" |
58 | | #endif |
59 | | |
60 | | namespace vvdec |
61 | | { |
62 | | |
63 | | // ==================================================================================================================== |
64 | | // Class definition |
65 | | // ==================================================================================================================== |
66 | | |
67 | | /** |
68 | | * Model of an input bitstream that extracts bits from a predefined |
69 | | * bytestream. |
70 | | */ |
71 | | class InputBitstream |
72 | | { |
73 | | private: |
74 | | AlignedByteVec m_fifo; /// FIFO for storage of complete bytes |
75 | | std::vector<uint32_t> m_emulationPreventionByteLocation; |
76 | | |
77 | | uint32_t m_fifo_idx = 0; /// Read index into m_fifo |
78 | | |
79 | | uint32_t m_num_held_bits = 0; |
80 | | uint64_t m_held_bits = 0; |
81 | | |
82 | | bool m_zeroByteAdded = false; |
83 | | |
84 | | #if ENABLE_TRACING |
85 | | uint32_t m_numBitsRead = 0; |
86 | | #endif |
87 | | |
88 | | public: |
89 | | /** |
90 | | * Create a new bitstream reader object that reads from buf. |
91 | | */ |
92 | 0 | InputBitstream() = default; |
93 | 0 | ~InputBitstream() = default; |
94 | | CLASS_COPY_MOVE_DEFAULT( InputBitstream ) |
95 | | |
96 | | void resetToStart(); |
97 | | |
98 | 0 | void inputZeroByte() { if( !m_zeroByteAdded ) m_fifo.push_back(0); }; |
99 | | |
100 | | // interface for decoding |
101 | | uint32_t peekBits( uint32_t uiNumberOfBits ); |
102 | | uint32_t read ( uint32_t uiNumberOfBits ); |
103 | | inline uint8_t readByte() |
104 | 0 | { |
105 | | #if ENABLE_TRACING |
106 | | m_numBitsRead += 8; |
107 | | #endif |
108 | |
|
109 | 0 | if( m_num_held_bits ) |
110 | 0 | { |
111 | 0 | CHECKD( m_num_held_bits & 7, "held bits should be byte-aligned" ); |
112 | 0 | } |
113 | 0 | else |
114 | 0 | { |
115 | 0 | load_next_bits( 8 ); |
116 | 0 | } |
117 | | |
118 | 0 | uint32_t uiBits = 0xff & ( m_held_bits >> ( m_num_held_bits - 8 ) ); |
119 | 0 | m_num_held_bits -= 8; |
120 | 0 | return uiBits; |
121 | 0 | } |
122 | | |
123 | | // In the function initBitstream, add a Byte at the end of m_fifo |
124 | | // In the past, readByteFlag judged whether to read data according to the flag, so it would not cross the boundary |
125 | | // Now, in order to speed up, readByteFlag will definitely read data, so it may cross the boundary |
126 | | inline uint8_t readByteFlag( uint8_t flag ) |
127 | 0 | { |
128 | | #if ENABLE_TRACING |
129 | | m_numBitsRead += (flag & 1) * 8; |
130 | | #endif |
131 | |
|
132 | 0 | if( m_num_held_bits ) |
133 | 0 | { |
134 | 0 | CHECKD( m_num_held_bits & 7, "held bits should be byte-aligned" ); |
135 | 0 | } |
136 | 0 | else |
137 | 0 | { |
138 | 0 | load_next_bits( 8 ); |
139 | 0 | } |
140 | | |
141 | 0 | uint32_t uiBits = flag & ( m_held_bits >> ( m_num_held_bits - 8 ) ); |
142 | 0 | m_num_held_bits -= 8 * ( flag & 1 ); |
143 | 0 | return uiBits; |
144 | 0 | } |
145 | | |
146 | | inline uint8_t peekPreviousByte() |
147 | 0 | { |
148 | 0 | CHECKD( m_num_held_bits & 7, "held bits should be byte-aligned" ); |
149 | | |
150 | | // We don't know if m_heldBits actually contains the previous byte, because readByteFlag() |
151 | | // sometimes refills m_heldBits, but doesn't actually consume any of the loaded bits. So |
152 | | // we always need to look at the actual data buffer. |
153 | 0 | const unsigned held_bytes = m_num_held_bits >> 3; |
154 | 0 | CHECK( m_fifo_idx - held_bytes - 1 >= m_fifo.size(), "Exceeded FIFO size" ); |
155 | 0 | return m_fifo[m_fifo_idx - held_bytes - 1]; |
156 | 0 | } |
157 | | |
158 | | uint32_t readOutTrailingBits(); |
159 | 0 | inline uint8_t getHeldBits() { return m_held_bits; } |
160 | | inline uint32_t getByteLocation() |
161 | 0 | { |
162 | 0 | CHECKD( m_num_held_bits & 7, "held bits should be byte-aligned" ); |
163 | 0 | return m_fifo_idx - m_num_held_bits / 8; |
164 | 0 | } |
165 | | |
166 | 0 | inline uint8_t getNumBitsUntilByteAligned() const { return m_num_held_bits & ( 0x7 ); } |
167 | 0 | inline uint32_t getNumBitsLeft() const { return ( m_fifo_idx < m_fifo.size() ? 8 * ( (uint32_t) m_fifo.size() - m_fifo_idx ) : 0 ) + m_num_held_bits; } |
168 | | #if ENABLE_TRACING |
169 | | inline uint32_t getNumBitsRead() const { return m_numBitsRead; } |
170 | | #endif |
171 | | uint32_t readByteAlignment(); |
172 | | std::unique_ptr<InputBitstream> extractSubstream( uint32_t uiNumBits ); // Read the nominated number of bits, and return as a bitstream. |
173 | | |
174 | 0 | void pushEmulationPreventionByteLocation( uint32_t pos ) { m_emulationPreventionByteLocation.push_back( pos ); } |
175 | 0 | uint32_t numEmulationPreventionBytesRead() { return (uint32_t) m_emulationPreventionByteLocation.size(); } |
176 | 0 | uint32_t getEmulationPreventionByteLocation( uint32_t idx ) { return m_emulationPreventionByteLocation[idx]; } |
177 | 0 | const std::vector<uint32_t>& getEmulationPreventionByteLocation() const { return m_emulationPreventionByteLocation; } |
178 | 0 | void setEmulationPreventionByteLocation( const std::vector<uint32_t>& vec ) { m_emulationPreventionByteLocation = vec; } |
179 | 0 | void clearEmulationPreventionByteLocation() { m_emulationPreventionByteLocation.clear(); } |
180 | | |
181 | 0 | const AlignedByteVec& getFifo() const { return m_fifo; } |
182 | 0 | AlignedByteVec& getFifo() { return m_fifo; } |
183 | 0 | void clearFifo() { m_fifo.clear(); m_zeroByteAdded = false; } |
184 | | |
185 | | private: |
186 | | inline void load_next_bits( int requiredBits ) |
187 | 0 | { |
188 | 0 | uint32_t num_bytes_to_load = 8; |
189 | 0 | if UNLIKELY( m_fifo_idx + num_bytes_to_load > m_fifo.size() // end of bitstream |
190 | | || ( m_fifo_idx & 0x7 ) != 0 ) // unaligned read position (m_fifo should be aligned) |
191 | 0 | { |
192 | 0 | const int required_bytes = ( requiredBits + 7 ) >> 3; |
193 | 0 | CHECK( m_fifo_idx + required_bytes > m_fifo.size(), "Exceeded FIFO size" ); |
194 | |
|
195 | 0 | num_bytes_to_load = (uint32_t)m_fifo.size() - m_fifo_idx; |
196 | |
|
197 | 0 | m_held_bits = 0; |
198 | 0 | switch( num_bytes_to_load ) |
199 | 0 | { |
200 | 0 | default: num_bytes_to_load = 8; // in the unaligned case num_bytes_to_load could be >8 |
201 | 0 | case 8: m_held_bits = static_cast<uint64_t>( m_fifo[m_fifo_idx++] ) << ( 7 * 8 ); |
202 | 0 | case 7: m_held_bits |= static_cast<uint64_t>( m_fifo[m_fifo_idx++] ) << ( 6 * 8 ); |
203 | 0 | case 6: m_held_bits |= static_cast<uint64_t>( m_fifo[m_fifo_idx++] ) << ( 5 * 8 ); |
204 | 0 | case 5: m_held_bits |= static_cast<uint64_t>( m_fifo[m_fifo_idx++] ) << ( 4 * 8 ); |
205 | 0 | case 4: m_held_bits |= static_cast<uint64_t>( m_fifo[m_fifo_idx++] ) << ( 3 * 8 ); |
206 | 0 | case 3: m_held_bits |= static_cast<uint64_t>( m_fifo[m_fifo_idx++] ) << ( 2 * 8 ); |
207 | 0 | case 2: m_held_bits |= static_cast<uint64_t>( m_fifo[m_fifo_idx++] ) << ( 1 * 8 ); |
208 | 0 | case 1: m_held_bits |= static_cast<uint64_t>( m_fifo[m_fifo_idx++] ); |
209 | 0 | } |
210 | 0 | } |
211 | 0 | else |
212 | 0 | { |
213 | 0 | CHECKD( reinterpret_cast<intptr_t>( &m_fifo[m_fifo_idx] ) & 0x7, "bistream read pos unaligned" ); |
214 | 0 | m_held_bits = simde_bswap64( *reinterpret_cast<uint64_t*>( &m_fifo[m_fifo_idx] ) ); |
215 | 0 | m_fifo_idx += num_bytes_to_load; |
216 | 0 | } |
217 | | |
218 | 0 | m_num_held_bits = num_bytes_to_load * 8; |
219 | 0 | } |
220 | | }; |
221 | | |
222 | | } |