1 | /* The copyright in this software is being made available under the BSD |
---|
2 | * License, included below. This software may be subject to other third party |
---|
3 | * and contributor rights, including patent rights, and no such rights are |
---|
4 | * granted under this license. |
---|
5 | * |
---|
6 | * Copyright (c) 2010-2015, ITU/ISO/IEC |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * |
---|
12 | * * Redistributions of source code must retain the above copyright notice, |
---|
13 | * this list of conditions and the following disclaimer. |
---|
14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
15 | * this list of conditions and the following disclaimer in the documentation |
---|
16 | * and/or other materials provided with the distribution. |
---|
17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
18 | * be used to endorse or promote products derived from this software without |
---|
19 | * specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | */ |
---|
33 | |
---|
34 | /** \file TComBitStream.cpp |
---|
35 | \brief class for handling bitstream |
---|
36 | */ |
---|
37 | |
---|
38 | #include <stdint.h> |
---|
39 | #include <vector> |
---|
40 | #include "TComBitStream.h" |
---|
41 | #include <string.h> |
---|
42 | #include <memory.h> |
---|
43 | #if NH_MV |
---|
44 | #include "TComRom.h" // This is only here, since ENC_DEC_TRACE is currently there. Consider removing when this has changed. |
---|
45 | #endif |
---|
46 | using namespace std; |
---|
47 | |
---|
48 | //! \ingroup TLibCommon |
---|
49 | //! \{ |
---|
50 | |
---|
51 | // ==================================================================================================================== |
---|
52 | // Constructor / destructor / create / destroy |
---|
53 | // ==================================================================================================================== |
---|
54 | |
---|
55 | TComOutputBitstream::TComOutputBitstream() |
---|
56 | { |
---|
57 | clear(); |
---|
58 | } |
---|
59 | |
---|
60 | TComOutputBitstream::~TComOutputBitstream() |
---|
61 | { |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | TComInputBitstream::TComInputBitstream() |
---|
66 | : m_fifo() |
---|
67 | , m_emulationPreventionByteLocation() |
---|
68 | , m_fifo_idx(0) |
---|
69 | , m_num_held_bits(0) |
---|
70 | , m_held_bits(0) |
---|
71 | , m_numBitsRead(0) |
---|
72 | { } |
---|
73 | |
---|
74 | TComInputBitstream::TComInputBitstream(const TComInputBitstream &src) |
---|
75 | : m_fifo(src.m_fifo) |
---|
76 | , m_emulationPreventionByteLocation(src.m_emulationPreventionByteLocation) |
---|
77 | , m_fifo_idx(src.m_fifo_idx) |
---|
78 | , m_num_held_bits(src.m_num_held_bits) |
---|
79 | , m_held_bits(src.m_held_bits) |
---|
80 | , m_numBitsRead(src.m_numBitsRead) |
---|
81 | { } |
---|
82 | |
---|
83 | // ==================================================================================================================== |
---|
84 | // Public member functions |
---|
85 | // ==================================================================================================================== |
---|
86 | |
---|
87 | Void TComInputBitstream::resetToStart() |
---|
88 | { |
---|
89 | m_fifo_idx=0; |
---|
90 | m_num_held_bits=0; |
---|
91 | m_held_bits=0; |
---|
92 | m_numBitsRead=0; |
---|
93 | } |
---|
94 | |
---|
95 | Char* TComOutputBitstream::getByteStream() const |
---|
96 | { |
---|
97 | return (Char*) &m_fifo.front(); |
---|
98 | } |
---|
99 | |
---|
100 | UInt TComOutputBitstream::getByteStreamLength() |
---|
101 | { |
---|
102 | return UInt(m_fifo.size()); |
---|
103 | } |
---|
104 | |
---|
105 | Void TComOutputBitstream::clear() |
---|
106 | { |
---|
107 | m_fifo.clear(); |
---|
108 | m_held_bits = 0; |
---|
109 | m_num_held_bits = 0; |
---|
110 | } |
---|
111 | |
---|
112 | Void TComOutputBitstream::write ( UInt uiBits, UInt uiNumberOfBits ) |
---|
113 | { |
---|
114 | assert( uiNumberOfBits <= 32 ); |
---|
115 | assert( uiNumberOfBits == 32 || (uiBits & (~0 << uiNumberOfBits)) == 0 ); |
---|
116 | |
---|
117 | /* any modulo 8 remainder of num_total_bits cannot be written this time, |
---|
118 | * and will be held until next time. */ |
---|
119 | UInt num_total_bits = uiNumberOfBits + m_num_held_bits; |
---|
120 | UInt next_num_held_bits = num_total_bits % 8; |
---|
121 | |
---|
122 | /* form a byte aligned word (write_bits), by concatenating any held bits |
---|
123 | * with the new bits, discarding the bits that will form the next_held_bits. |
---|
124 | * eg: H = held bits, V = n new bits /---- next_held_bits |
---|
125 | * len(H)=7, len(V)=1: ... ---- HHHH HHHV . 0000 0000, next_num_held_bits=0 |
---|
126 | * len(H)=7, len(V)=2: ... ---- HHHH HHHV . V000 0000, next_num_held_bits=1 |
---|
127 | * if total_bits < 8, the value of v_ is not used */ |
---|
128 | UChar next_held_bits = uiBits << (8 - next_num_held_bits); |
---|
129 | |
---|
130 | if (!(num_total_bits >> 3)) |
---|
131 | { |
---|
132 | /* insufficient bits accumulated to write out, append new_held_bits to |
---|
133 | * current held_bits */ |
---|
134 | /* NB, this requires that v only contains 0 in bit positions {31..n} */ |
---|
135 | m_held_bits |= next_held_bits; |
---|
136 | m_num_held_bits = next_num_held_bits; |
---|
137 | return; |
---|
138 | } |
---|
139 | |
---|
140 | /* topword serves to justify held_bits to align with the msb of uiBits */ |
---|
141 | UInt topword = (uiNumberOfBits - next_num_held_bits) & ~((1 << 3) -1); |
---|
142 | UInt write_bits = (m_held_bits << topword) | (uiBits >> next_num_held_bits); |
---|
143 | |
---|
144 | switch (num_total_bits >> 3) |
---|
145 | { |
---|
146 | case 4: m_fifo.push_back(write_bits >> 24); |
---|
147 | case 3: m_fifo.push_back(write_bits >> 16); |
---|
148 | case 2: m_fifo.push_back(write_bits >> 8); |
---|
149 | case 1: m_fifo.push_back(write_bits); |
---|
150 | } |
---|
151 | |
---|
152 | m_held_bits = next_held_bits; |
---|
153 | m_num_held_bits = next_num_held_bits; |
---|
154 | } |
---|
155 | |
---|
156 | Void TComOutputBitstream::writeAlignOne() |
---|
157 | { |
---|
158 | UInt num_bits = getNumBitsUntilByteAligned(); |
---|
159 | write((1 << num_bits) - 1, num_bits); |
---|
160 | return; |
---|
161 | } |
---|
162 | |
---|
163 | Void TComOutputBitstream::writeAlignZero() |
---|
164 | { |
---|
165 | if (0 == m_num_held_bits) |
---|
166 | { |
---|
167 | return; |
---|
168 | } |
---|
169 | m_fifo.push_back(m_held_bits); |
---|
170 | m_held_bits = 0; |
---|
171 | m_num_held_bits = 0; |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | - add substream to the end of the current bitstream |
---|
176 | . |
---|
177 | \param pcSubstream substream to be added |
---|
178 | */ |
---|
179 | Void TComOutputBitstream::addSubstream( TComOutputBitstream* pcSubstream ) |
---|
180 | { |
---|
181 | UInt uiNumBits = pcSubstream->getNumberOfWrittenBits(); |
---|
182 | |
---|
183 | const vector<uint8_t>& rbsp = pcSubstream->getFIFO(); |
---|
184 | for (vector<uint8_t>::const_iterator it = rbsp.begin(); it != rbsp.end();) |
---|
185 | { |
---|
186 | write(*it++, 8); |
---|
187 | } |
---|
188 | if (uiNumBits&0x7) |
---|
189 | { |
---|
190 | write(pcSubstream->getHeldBits()>>(8-(uiNumBits&0x7)), uiNumBits&0x7); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | Void TComOutputBitstream::writeByteAlignment() |
---|
195 | { |
---|
196 | write( 1, 1); |
---|
197 | writeAlignZero(); |
---|
198 | } |
---|
199 | |
---|
200 | Int TComOutputBitstream::countStartCodeEmulations() |
---|
201 | { |
---|
202 | UInt cnt = 0; |
---|
203 | vector<uint8_t>& rbsp = getFIFO(); |
---|
204 | for (vector<uint8_t>::iterator it = rbsp.begin(); it != rbsp.end();) |
---|
205 | { |
---|
206 | vector<uint8_t>::iterator found = it; |
---|
207 | do |
---|
208 | { |
---|
209 | // find the next emulated 00 00 {00,01,02,03} |
---|
210 | // NB, end()-1, prevents finding a trailing two byte sequence |
---|
211 | found = search_n(found, rbsp.end()-1, 2, 0); |
---|
212 | found++; |
---|
213 | // if not found, found == end, otherwise found = second zero byte |
---|
214 | if (found == rbsp.end()) |
---|
215 | { |
---|
216 | break; |
---|
217 | } |
---|
218 | if (*(++found) <= 3) |
---|
219 | { |
---|
220 | break; |
---|
221 | } |
---|
222 | } while (true); |
---|
223 | it = found; |
---|
224 | if (found != rbsp.end()) |
---|
225 | { |
---|
226 | cnt++; |
---|
227 | } |
---|
228 | } |
---|
229 | return cnt; |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * read uiNumberOfBits from bitstream without updating the bitstream |
---|
234 | * state, storing the result in ruiBits. |
---|
235 | * |
---|
236 | * If reading uiNumberOfBits would overrun the bitstream buffer, |
---|
237 | * the bitstream is effectively padded with sufficient zero-bits to |
---|
238 | * avoid the overrun. |
---|
239 | */ |
---|
240 | Void TComInputBitstream::pseudoRead ( UInt uiNumberOfBits, UInt& ruiBits ) |
---|
241 | { |
---|
242 | UInt saved_num_held_bits = m_num_held_bits; |
---|
243 | UChar saved_held_bits = m_held_bits; |
---|
244 | UInt saved_fifo_idx = m_fifo_idx; |
---|
245 | |
---|
246 | UInt num_bits_to_read = min(uiNumberOfBits, getNumBitsLeft()); |
---|
247 | read(num_bits_to_read, ruiBits); |
---|
248 | ruiBits <<= (uiNumberOfBits - num_bits_to_read); |
---|
249 | |
---|
250 | m_fifo_idx = saved_fifo_idx; |
---|
251 | m_held_bits = saved_held_bits; |
---|
252 | m_num_held_bits = saved_num_held_bits; |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | Void TComInputBitstream::read (UInt uiNumberOfBits, UInt& ruiBits) |
---|
257 | { |
---|
258 | assert( uiNumberOfBits <= 32 ); |
---|
259 | |
---|
260 | m_numBitsRead += uiNumberOfBits; |
---|
261 | |
---|
262 | #if ENC_DEC_TRACE && H_MV_ENC_DEC_TRAC |
---|
263 | if ( g_traceBitsRead ) |
---|
264 | { |
---|
265 | Bool oldJustDoIt = g_bJustDoIt; |
---|
266 | g_bJustDoIt = true; |
---|
267 | writeToTraceFile( "Bits: ", m_numBitsRead, true ); |
---|
268 | g_bJustDoIt = oldJustDoIt; |
---|
269 | } |
---|
270 | #endif |
---|
271 | |
---|
272 | /* NB, bits are extracted from the MSB of each byte. */ |
---|
273 | UInt retval = 0; |
---|
274 | if (uiNumberOfBits <= m_num_held_bits) |
---|
275 | { |
---|
276 | /* n=1, len(H)=7: -VHH HHHH, shift_down=6, mask=0xfe |
---|
277 | * n=3, len(H)=7: -VVV HHHH, shift_down=4, mask=0xf8 |
---|
278 | */ |
---|
279 | retval = m_held_bits >> (m_num_held_bits - uiNumberOfBits); |
---|
280 | retval &= ~(0xff << uiNumberOfBits); |
---|
281 | m_num_held_bits -= uiNumberOfBits; |
---|
282 | ruiBits = retval; |
---|
283 | return; |
---|
284 | } |
---|
285 | |
---|
286 | /* all num_held_bits will go into retval |
---|
287 | * => need to mask leftover bits from previous extractions |
---|
288 | * => align retval with top of extracted word */ |
---|
289 | /* n=5, len(H)=3: ---- -VVV, mask=0x07, shift_up=5-3=2, |
---|
290 | * n=9, len(H)=3: ---- -VVV, mask=0x07, shift_up=9-3=6 */ |
---|
291 | uiNumberOfBits -= m_num_held_bits; |
---|
292 | retval = m_held_bits & ~(0xff << m_num_held_bits); |
---|
293 | retval <<= uiNumberOfBits; |
---|
294 | |
---|
295 | /* number of whole bytes that need to be loaded to form retval */ |
---|
296 | /* n=32, len(H)=0, load 4bytes, shift_down=0 |
---|
297 | * n=32, len(H)=1, load 4bytes, shift_down=1 |
---|
298 | * n=31, len(H)=1, load 4bytes, shift_down=1+1 |
---|
299 | * n=8, len(H)=0, load 1byte, shift_down=0 |
---|
300 | * n=8, len(H)=3, load 1byte, shift_down=3 |
---|
301 | * n=5, len(H)=1, load 1byte, shift_down=1+3 |
---|
302 | */ |
---|
303 | UInt aligned_word = 0; |
---|
304 | UInt num_bytes_to_load = (uiNumberOfBits - 1) >> 3; |
---|
305 | assert(m_fifo_idx + num_bytes_to_load < m_fifo.size()); |
---|
306 | |
---|
307 | switch (num_bytes_to_load) |
---|
308 | { |
---|
309 | case 3: aligned_word = m_fifo[m_fifo_idx++] << 24; |
---|
310 | case 2: aligned_word |= m_fifo[m_fifo_idx++] << 16; |
---|
311 | case 1: aligned_word |= m_fifo[m_fifo_idx++] << 8; |
---|
312 | case 0: aligned_word |= m_fifo[m_fifo_idx++]; |
---|
313 | } |
---|
314 | |
---|
315 | /* resolve remainder bits */ |
---|
316 | UInt next_num_held_bits = (32 - uiNumberOfBits) % 8; |
---|
317 | |
---|
318 | /* copy required part of aligned_word into retval */ |
---|
319 | retval |= aligned_word >> next_num_held_bits; |
---|
320 | |
---|
321 | /* store held bits */ |
---|
322 | m_num_held_bits = next_num_held_bits; |
---|
323 | m_held_bits = aligned_word; |
---|
324 | |
---|
325 | ruiBits = retval; |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * insert the contents of the bytealigned (and flushed) bitstream src |
---|
330 | * into this at byte position pos. |
---|
331 | */ |
---|
332 | Void TComOutputBitstream::insertAt(const TComOutputBitstream& src, UInt pos) |
---|
333 | { |
---|
334 | UInt src_bits = src.getNumberOfWrittenBits(); |
---|
335 | assert(0 == src_bits % 8); |
---|
336 | |
---|
337 | vector<uint8_t>::iterator at = m_fifo.begin() + pos; |
---|
338 | m_fifo.insert(at, src.m_fifo.begin(), src.m_fifo.end()); |
---|
339 | } |
---|
340 | |
---|
341 | UInt TComInputBitstream::readOutTrailingBits () |
---|
342 | { |
---|
343 | UInt count=0; |
---|
344 | UInt uiBits = 0; |
---|
345 | |
---|
346 | while ( ( getNumBitsLeft() > 0 ) && (getNumBitsUntilByteAligned()!=0) ) |
---|
347 | { |
---|
348 | count++; |
---|
349 | read ( 1, uiBits ); |
---|
350 | } |
---|
351 | return count; |
---|
352 | } |
---|
353 | // |
---|
354 | //TComOutputBitstream& TComOutputBitstream::operator= (const TComOutputBitstream& src) |
---|
355 | //{ |
---|
356 | // vector<uint8_t>::iterator at = m_fifo.begin(); |
---|
357 | // m_fifo.insert(at, src.m_fifo.begin(), src.m_fifo.end()); |
---|
358 | // |
---|
359 | // m_num_held_bits = src.m_num_held_bits; |
---|
360 | // m_held_bits = src.m_held_bits; |
---|
361 | // |
---|
362 | // return *this; |
---|
363 | //} |
---|
364 | |
---|
365 | /** |
---|
366 | Extract substream from the current bitstream. |
---|
367 | |
---|
368 | \param uiNumBits number of bits to transfer |
---|
369 | */ |
---|
370 | TComInputBitstream *TComInputBitstream::extractSubstream( UInt uiNumBits ) |
---|
371 | { |
---|
372 | UInt uiNumBytes = uiNumBits/8; |
---|
373 | TComInputBitstream *pResult = new TComInputBitstream; |
---|
374 | |
---|
375 | std::vector<uint8_t> &buf = pResult->getFifo(); |
---|
376 | buf.reserve((uiNumBits+7)>>3); |
---|
377 | |
---|
378 | if (m_num_held_bits == 0) |
---|
379 | { |
---|
380 | std::size_t currentOutputBufferSize=buf.size(); |
---|
381 | const UInt uiNumBytesToReadFromFifo = std::min<UInt>(uiNumBytes, (UInt)m_fifo.size() - m_fifo_idx); |
---|
382 | buf.resize(currentOutputBufferSize+uiNumBytes); |
---|
383 | memcpy(&(buf[currentOutputBufferSize]), &(m_fifo[m_fifo_idx]), uiNumBytesToReadFromFifo); m_fifo_idx+=uiNumBytesToReadFromFifo; |
---|
384 | if (uiNumBytesToReadFromFifo != uiNumBytes) |
---|
385 | { |
---|
386 | memset(&(buf[currentOutputBufferSize+uiNumBytesToReadFromFifo]), 0, uiNumBytes - uiNumBytesToReadFromFifo); |
---|
387 | } |
---|
388 | } |
---|
389 | else |
---|
390 | { |
---|
391 | for (UInt ui = 0; ui < uiNumBytes; ui++) |
---|
392 | { |
---|
393 | UInt uiByte; |
---|
394 | read(8, uiByte); |
---|
395 | buf.push_back(uiByte); |
---|
396 | } |
---|
397 | } |
---|
398 | if (uiNumBits&0x7) |
---|
399 | { |
---|
400 | UInt uiByte = 0; |
---|
401 | read(uiNumBits&0x7, uiByte); |
---|
402 | uiByte <<= 8-(uiNumBits&0x7); |
---|
403 | buf.push_back(uiByte); |
---|
404 | } |
---|
405 | return pResult; |
---|
406 | } |
---|
407 | |
---|
408 | UInt TComInputBitstream::readByteAlignment() |
---|
409 | { |
---|
410 | UInt code = 0; |
---|
411 | read( 1, code ); |
---|
412 | assert(code == 1); |
---|
413 | |
---|
414 | UInt numBits = getNumBitsUntilByteAligned(); |
---|
415 | if(numBits) |
---|
416 | { |
---|
417 | assert(numBits <= getNumBitsLeft()); |
---|
418 | read( numBits, code ); |
---|
419 | assert(code == 0); |
---|
420 | } |
---|
421 | return numBits+1; |
---|
422 | } |
---|
423 | |
---|
424 | //! \} |
---|