/work/vvenc/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) 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 | | |
43 | | |
44 | | /** \file BitStream.cpp |
45 | | \brief class for handling bitstream |
46 | | */ |
47 | | |
48 | | #include "BitStream.h" |
49 | | #include "dtrace_next.h" |
50 | | |
51 | | #include <stdint.h> |
52 | | #include <vector> |
53 | | #include <string.h> |
54 | | #include <memory.h> |
55 | | |
56 | | //! \ingroup CommonLib |
57 | | //! \{ |
58 | | |
59 | | namespace vvenc { |
60 | | |
61 | | // ==================================================================================================================== |
62 | | // Constructor / destructor / create / destroy |
63 | | // ==================================================================================================================== |
64 | | |
65 | | OutputBitstream::OutputBitstream() |
66 | 11.4k | { |
67 | 11.4k | clear(); |
68 | 11.4k | } |
69 | | |
70 | | OutputBitstream::~OutputBitstream() |
71 | 11.4k | { |
72 | 11.4k | } |
73 | | |
74 | | |
75 | | InputBitstream::InputBitstream() |
76 | 0 | : m_fifo() |
77 | 0 | , m_emulationPreventionByteLocation() |
78 | 0 | , m_fifo_idx(0) |
79 | 0 | , m_num_held_bits(0) |
80 | 0 | , m_held_bits(0) |
81 | 0 | , m_numBitsRead(0) |
82 | 0 | { } |
83 | | |
84 | | InputBitstream::InputBitstream(const InputBitstream &src) |
85 | 0 | : m_fifo(src.m_fifo) |
86 | 0 | , m_emulationPreventionByteLocation(src.m_emulationPreventionByteLocation) |
87 | 0 | , m_fifo_idx(src.m_fifo_idx) |
88 | 0 | , m_num_held_bits(src.m_num_held_bits) |
89 | 0 | , m_held_bits(src.m_held_bits) |
90 | 0 | , m_numBitsRead(src.m_numBitsRead) |
91 | 0 | { } |
92 | | |
93 | | // ==================================================================================================================== |
94 | | // Public member functions |
95 | | // ==================================================================================================================== |
96 | | |
97 | | void InputBitstream::resetToStart() |
98 | 0 | { |
99 | 0 | m_fifo_idx=0; |
100 | 0 | m_num_held_bits=0; |
101 | 0 | m_held_bits=0; |
102 | 0 | m_numBitsRead=0; |
103 | 0 | } |
104 | | |
105 | | uint8_t* OutputBitstream::getByteStream() const |
106 | 4.29k | { |
107 | 4.29k | return (uint8_t*) &m_fifo.front(); |
108 | 4.29k | } |
109 | | |
110 | | uint32_t OutputBitstream::getByteStreamLength() |
111 | 4.29k | { |
112 | 4.29k | return uint32_t(m_fifo.size()); |
113 | 4.29k | } |
114 | | |
115 | | void OutputBitstream::clear() |
116 | 11.4k | { |
117 | 11.4k | m_fifo.clear(); |
118 | 11.4k | m_held_bits = 0; |
119 | 11.4k | m_num_held_bits = 0; |
120 | 11.4k | } |
121 | | |
122 | | void OutputBitstream::write ( uint32_t uiBits, uint32_t uiNumberOfBits ) |
123 | 1.05M | { |
124 | 1.05M | CHECK( uiNumberOfBits > 32, "Number of bits is exceeds '32'" ); |
125 | 1.05M | CHECK( uiNumberOfBits != 32 && (uiBits & (~0u << uiNumberOfBits)) != 0, "Unsupported parameters" ); |
126 | | |
127 | | /* any modulo 8 remainder of num_total_bits cannot be written this time, |
128 | | * and will be held until next time. */ |
129 | 1.05M | uint32_t num_total_bits = uiNumberOfBits + m_num_held_bits; |
130 | 1.05M | uint32_t next_num_held_bits = num_total_bits % 8; |
131 | | |
132 | | /* form a byte aligned word (write_bits), by concatenating any held bits |
133 | | * with the new bits, discarding the bits that will form the next_held_bits. |
134 | | * eg: H = held bits, V = n new bits /---- next_held_bits |
135 | | * len(H)=7, len(V)=1: ... ---- HHHH HHHV . 0000 0000, next_num_held_bits=0 |
136 | | * len(H)=7, len(V)=2: ... ---- HHHH HHHV . V000 0000, next_num_held_bits=1 |
137 | | * if total_bits < 8, the value of v_ is not used */ |
138 | 1.05M | uint8_t next_held_bits = uiBits << (8 - next_num_held_bits); |
139 | | |
140 | 1.05M | if (!(num_total_bits >> 3)) |
141 | 783k | { |
142 | | /* insufficient bits accumulated to write out, append new_held_bits to |
143 | | * current held_bits */ |
144 | | /* NB, this requires that v only contains 0 in bit positions {31..n} */ |
145 | 783k | m_held_bits |= next_held_bits; |
146 | 783k | m_num_held_bits = next_num_held_bits; |
147 | 783k | return; |
148 | 783k | } |
149 | | |
150 | | /* topword serves to justify held_bits to align with the msb of uiBits */ |
151 | 270k | uint32_t topword = (uiNumberOfBits - next_num_held_bits) & ~((1 << 3) -1); |
152 | 270k | uint32_t write_bits = (uint32_t)(((uint64_t)m_held_bits << topword) | (uiBits >> next_num_held_bits)); |
153 | 270k | switch (num_total_bits >> 3) |
154 | 270k | { |
155 | 2.86k | case 4: m_fifo.push_back(write_bits >> 24); |
156 | 2.86k | case 3: m_fifo.push_back(write_bits >> 16); |
157 | 2.90k | case 2: m_fifo.push_back(write_bits >> 8); |
158 | 270k | case 1: m_fifo.push_back(write_bits); |
159 | 270k | } |
160 | | |
161 | 270k | DTRACE( g_trace_ctx, D_BITSTREAM, " %5d: %0X\n", (int)(m_fifo.size() - 1), m_fifo.back() ); |
162 | 270k | m_held_bits = next_held_bits; |
163 | 270k | m_num_held_bits = next_num_held_bits; |
164 | 270k | } |
165 | | |
166 | | void OutputBitstream::writeAlignOne() |
167 | 0 | { |
168 | 0 | uint32_t num_bits = getNumBitsUntilByteAligned(); |
169 | 0 | write((1 << num_bits) - 1, num_bits); |
170 | 0 | return; |
171 | 0 | } |
172 | | |
173 | | void OutputBitstream::writeAlignZero() |
174 | 2.86k | { |
175 | 2.86k | if (0 == m_num_held_bits) |
176 | 182 | { |
177 | 182 | return; |
178 | 182 | } |
179 | 2.68k | m_fifo.push_back(m_held_bits); |
180 | 2.68k | m_held_bits = 0; |
181 | 2.68k | m_num_held_bits = 0; |
182 | 2.68k | } |
183 | | |
184 | | /** |
185 | | - add substream to the end of the current bitstream |
186 | | . |
187 | | \param pcSubstream substream to be added |
188 | | */ |
189 | | void OutputBitstream::addSubstream( const OutputBitstream* pcSubstream ) |
190 | 2.86k | { |
191 | 2.86k | uint32_t uiNumBits = pcSubstream->getNumberOfWrittenBits(); |
192 | | |
193 | 2.86k | const std::vector<uint8_t>& rbsp = pcSubstream->getFIFO(); |
194 | 2.86k | if( m_num_held_bits == 0 ) |
195 | 2.86k | { |
196 | | // byte-aligned, as after a slice header or a whole substream: the bytes go in unchanged, so append them at once |
197 | 2.86k | m_fifo.insert( m_fifo.end(), rbsp.begin(), rbsp.end() ); |
198 | 2.86k | } |
199 | 0 | else |
200 | 0 | { |
201 | 0 | for (std::vector<uint8_t>::const_iterator it = rbsp.begin(); it != rbsp.end();) |
202 | 0 | { |
203 | 0 | write(*it++, 8); |
204 | 0 | } |
205 | 0 | } |
206 | 2.86k | if (uiNumBits&0x7) |
207 | 0 | { |
208 | 0 | write(pcSubstream->getHeldBits()>>(8-(uiNumBits&0x7)), uiNumBits&0x7); |
209 | 0 | } |
210 | 2.86k | } |
211 | | |
212 | | void OutputBitstream::writeByteAlignment() |
213 | 2.86k | { |
214 | 2.86k | write( 1, 1); |
215 | 2.86k | writeAlignZero(); |
216 | 2.86k | } |
217 | | |
218 | | int OutputBitstream::countStartCodeEmulations() |
219 | 0 | { |
220 | 0 | uint32_t cnt = 0; |
221 | 0 | std::vector<uint8_t>& rbsp = getFIFO(); |
222 | 0 | for (std::vector<uint8_t>::iterator it = rbsp.begin(); it != rbsp.end();) |
223 | 0 | { |
224 | 0 | std::vector<uint8_t>::iterator found = it; |
225 | 0 | do |
226 | 0 | { |
227 | | // find the next emulated 00 00 {00,01,02,03} |
228 | | // NB, end()-1, prevents finding a trailing two byte sequence |
229 | 0 | found = search_n(found, rbsp.end()-1, 2, 0); |
230 | 0 | found++; |
231 | | // if not found, found == end, otherwise found = second zero byte |
232 | 0 | if (found == rbsp.end()) |
233 | 0 | { |
234 | 0 | break; |
235 | 0 | } |
236 | 0 | if (*(++found) <= 3) |
237 | 0 | { |
238 | 0 | break; |
239 | 0 | } |
240 | 0 | } while (true); |
241 | 0 | it = found; |
242 | 0 | if (found != rbsp.end()) |
243 | 0 | { |
244 | 0 | cnt++; |
245 | 0 | } |
246 | 0 | } |
247 | 0 | return cnt; |
248 | 0 | } |
249 | | |
250 | | /** |
251 | | * read uiNumberOfBits from bitstream without updating the bitstream |
252 | | * state, storing the result in ruiBits. |
253 | | * |
254 | | * If reading uiNumberOfBits would overrun the bitstream buffer, |
255 | | * the bitstream is effectively padded with sufficient zero-bits to |
256 | | * avoid the overrun. |
257 | | */ |
258 | | void InputBitstream::pseudoRead ( uint32_t uiNumberOfBits, uint32_t& ruiBits ) |
259 | 0 | { |
260 | 0 | uint32_t saved_num_held_bits = m_num_held_bits; |
261 | 0 | uint8_t saved_held_bits = m_held_bits; |
262 | 0 | uint32_t saved_fifo_idx = m_fifo_idx; |
263 | |
|
264 | 0 | uint32_t num_bits_to_read = std::min(uiNumberOfBits, getNumBitsLeft()); |
265 | 0 | read(num_bits_to_read, ruiBits); |
266 | 0 | ruiBits <<= (uiNumberOfBits - num_bits_to_read); |
267 | |
|
268 | 0 | m_fifo_idx = saved_fifo_idx; |
269 | 0 | m_held_bits = saved_held_bits; |
270 | 0 | m_num_held_bits = saved_num_held_bits; |
271 | 0 | } |
272 | | |
273 | | |
274 | | void InputBitstream::read (uint32_t uiNumberOfBits, uint32_t& ruiBits) |
275 | 0 | { |
276 | 0 | CHECK( uiNumberOfBits > 32, "Too many bits read" ); |
277 | |
|
278 | 0 | m_numBitsRead += uiNumberOfBits; |
279 | | |
280 | | /* NB, bits are extracted from the MSB of each byte. */ |
281 | 0 | uint32_t retval = 0; |
282 | 0 | if (uiNumberOfBits <= m_num_held_bits) |
283 | 0 | { |
284 | | /* n=1, len(H)=7: -VHH HHHH, shift_down=6, mask=0xfe |
285 | | * n=3, len(H)=7: -VVV HHHH, shift_down=4, mask=0xf8 |
286 | | */ |
287 | 0 | retval = m_held_bits >> (m_num_held_bits - uiNumberOfBits); |
288 | 0 | retval &= ~(0xff << uiNumberOfBits); |
289 | 0 | m_num_held_bits -= uiNumberOfBits; |
290 | 0 | ruiBits = retval; |
291 | 0 | return; |
292 | 0 | } |
293 | | |
294 | | /* all num_held_bits will go into retval |
295 | | * => need to mask leftover bits from previous extractions |
296 | | * => align retval with top of extracted word */ |
297 | | /* n=5, len(H)=3: ---- -VVV, mask=0x07, shift_up=5-3=2, |
298 | | * n=9, len(H)=3: ---- -VVV, mask=0x07, shift_up=9-3=6 */ |
299 | 0 | uiNumberOfBits -= m_num_held_bits; |
300 | 0 | retval = m_held_bits & ~(0xff << m_num_held_bits); |
301 | 0 | retval <<= uiNumberOfBits; |
302 | | |
303 | | /* number of whole bytes that need to be loaded to form retval */ |
304 | | /* n=32, len(H)=0, load 4bytes, shift_down=0 |
305 | | * n=32, len(H)=1, load 4bytes, shift_down=1 |
306 | | * n=31, len(H)=1, load 4bytes, shift_down=1+1 |
307 | | * n=8, len(H)=0, load 1byte, shift_down=0 |
308 | | * n=8, len(H)=3, load 1byte, shift_down=3 |
309 | | * n=5, len(H)=1, load 1byte, shift_down=1+3 |
310 | | */ |
311 | 0 | uint32_t aligned_word = 0; |
312 | 0 | uint32_t num_bytes_to_load = (uiNumberOfBits - 1) >> 3; |
313 | 0 | CHECK(m_fifo_idx + num_bytes_to_load >= m_fifo.size(), "Exceeded FIFO size"); |
314 | |
|
315 | 0 | switch (num_bytes_to_load) |
316 | 0 | { |
317 | 0 | case 3: aligned_word = m_fifo[m_fifo_idx++] << 24; |
318 | 0 | case 2: aligned_word |= m_fifo[m_fifo_idx++] << 16; |
319 | 0 | case 1: aligned_word |= m_fifo[m_fifo_idx++] << 8; |
320 | 0 | case 0: aligned_word |= m_fifo[m_fifo_idx++]; |
321 | 0 | } |
322 | | |
323 | | /* resolve remainder bits */ |
324 | 0 | uint32_t next_num_held_bits = (32 - uiNumberOfBits) % 8; |
325 | | |
326 | | /* copy required part of aligned_word into retval */ |
327 | 0 | retval |= aligned_word >> next_num_held_bits; |
328 | | |
329 | | /* store held bits */ |
330 | 0 | m_num_held_bits = next_num_held_bits; |
331 | 0 | m_held_bits = aligned_word; |
332 | |
|
333 | 0 | ruiBits = retval; |
334 | 0 | } |
335 | | |
336 | | /** |
337 | | * insert the contents of the bytealigned (and flushed) bitstream src |
338 | | * into this at byte position pos. |
339 | | */ |
340 | | void OutputBitstream::insertAt(const OutputBitstream& src, uint32_t pos) |
341 | 0 | { |
342 | 0 | CHECK(0 != src.getNumberOfWrittenBits() % 8, "Number of written bits is not a multiple of 8"); |
343 | |
|
344 | 0 | std::vector<uint8_t>::iterator at = m_fifo.begin() + pos; |
345 | 0 | m_fifo.insert(at, src.m_fifo.begin(), src.m_fifo.end()); |
346 | 0 | } |
347 | | |
348 | | uint32_t InputBitstream::readOutTrailingBits () |
349 | 0 | { |
350 | 0 | uint32_t count=0; |
351 | 0 | uint32_t uiBits = 0; |
352 | |
|
353 | 0 | while ( ( getNumBitsLeft() > 0 ) && (getNumBitsUntilByteAligned()!=0) ) |
354 | 0 | { |
355 | 0 | count++; |
356 | 0 | read ( 1, uiBits ); |
357 | 0 | } |
358 | 0 | return count; |
359 | 0 | } |
360 | | |
361 | | /** |
362 | | Extract substream from the current bitstream. |
363 | | |
364 | | \param uiNumBits number of bits to transfer |
365 | | */ |
366 | | InputBitstream *InputBitstream::extractSubstream( uint32_t uiNumBits ) |
367 | 0 | { |
368 | 0 | uint32_t uiNumBytes = uiNumBits/8; |
369 | 0 | InputBitstream *pResult = new InputBitstream; |
370 | |
|
371 | 0 | std::vector<uint8_t> &buf = pResult->getFifo(); |
372 | 0 | buf.reserve((uiNumBits+7)>>3); |
373 | |
|
374 | 0 | if (m_num_held_bits == 0) |
375 | 0 | { |
376 | 0 | std::size_t currentOutputBufferSize=buf.size(); |
377 | 0 | const uint32_t uiNumBytesToReadFromFifo = std::min<uint32_t>(uiNumBytes, (uint32_t)m_fifo.size() - m_fifo_idx); |
378 | 0 | buf.resize(currentOutputBufferSize+uiNumBytes); |
379 | 0 | memcpy(&(buf[currentOutputBufferSize]), &(m_fifo[m_fifo_idx]), uiNumBytesToReadFromFifo); m_fifo_idx+=uiNumBytesToReadFromFifo; |
380 | 0 | if (uiNumBytesToReadFromFifo != uiNumBytes) |
381 | 0 | { |
382 | 0 | memset(&(buf[currentOutputBufferSize+uiNumBytesToReadFromFifo]), 0, uiNumBytes - uiNumBytesToReadFromFifo); |
383 | 0 | } |
384 | 0 | } |
385 | 0 | else |
386 | 0 | { |
387 | 0 | for (uint32_t ui = 0; ui < uiNumBytes; ui++) |
388 | 0 | { |
389 | 0 | uint32_t uiByte; |
390 | 0 | read(8, uiByte); |
391 | 0 | buf.push_back(uiByte); |
392 | 0 | } |
393 | 0 | } |
394 | 0 | if (uiNumBits&0x7) |
395 | 0 | { |
396 | 0 | uint32_t uiByte = 0; |
397 | 0 | read(uiNumBits&0x7, uiByte); |
398 | 0 | uiByte <<= 8-(uiNumBits&0x7); |
399 | 0 | buf.push_back(uiByte); |
400 | 0 | } |
401 | 0 | return pResult; |
402 | 0 | } |
403 | | |
404 | | uint32_t InputBitstream::readByteAlignment() |
405 | 0 | { |
406 | 0 | uint32_t code = 0; |
407 | 0 | read( 1, code ); |
408 | 0 | CHECK(code != 1, "Code is not '1'"); |
409 | |
|
410 | 0 | uint32_t numBits = getNumBitsUntilByteAligned(); |
411 | 0 | if(numBits) |
412 | 0 | { |
413 | 0 | CHECK(numBits > getNumBitsLeft(), "More bits available than left"); |
414 | 0 | read( numBits, code ); |
415 | 0 | CHECK(code != 0, "Code not '0'"); |
416 | 0 | } |
417 | 0 | return numBits+1; |
418 | 0 | } |
419 | | |
420 | | } // namespace vvenc |
421 | | |
422 | | //! \} |
423 | | |