HEVC Test Model (HM)  HM-16.3
TComBitStream.h
Go to the documentation of this file.
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 
38 #ifndef __TCOMBITSTREAM__
39 #define __TCOMBITSTREAM__
40 
41 #if _MSC_VER > 1000
42 #pragma once
43 #endif // _MSC_VER > 1000
44 
45 #include <stdint.h>
46 #include <vector>
47 #include <stdio.h>
48 #include "CommonDef.h"
49 
52 
53 // ====================================================================================================================
54 // Class definition
55 // ====================================================================================================================
56 
58 class TComBitIf
59 {
60 public:
61  virtual Void writeAlignOne () {};
62  virtual Void writeAlignZero () {};
63  virtual Void write ( UInt uiBits, UInt uiNumberOfBits ) = 0;
64  virtual Void resetBits () = 0;
65  virtual UInt getNumberOfWrittenBits() const = 0;
66  virtual ~TComBitIf() {}
67 };
68 
74 {
82  std::vector<uint8_t> m_fifo;
83 
86 public:
88  // create / destroy
91 
92  // interface for encoding
97  Void write ( UInt uiBits, UInt uiNumberOfBits );
98 
100  Void writeAlignOne ();
101 
103  Void writeAlignZero ();
104 
106  Void resetBits() { assert(0); }
107 
108  // utility functions
109 
116  Char* getByteStream() const;
117 
122 
126  Void clear();
127 
132  Int getNumBitsUntilByteAligned() { return (8 - m_num_held_bits) & 0x7; }
133 
137  UInt getNumberOfWrittenBits() const { return UInt(m_fifo.size()) * 8 + m_num_held_bits; }
138 
139  Void insertAt(const TComOutputBitstream& src, UInt pos);
140 
144  std::vector<uint8_t>& getFIFO() { return m_fifo; }
145 
147 
148  //TComOutputBitstream& operator= (const TComOutputBitstream& src);
150  const std::vector<uint8_t>& getFIFO() const { return m_fifo; }
151 
152  Void addSubstream ( TComOutputBitstream* pcSubstream );
154 
157 };
158 
164 {
165  std::vector<uint8_t> *m_fifo;
167 
168 protected:
170 
174 
175 public:
181  TComInputBitstream(std::vector<uint8_t>* buf);
183 
184  // interface for decoding
185  Void pseudoRead ( UInt uiNumberOfBits, UInt& ruiBits );
186  Void read ( UInt uiNumberOfBits, UInt& ruiBits );
187  Void readByte ( UInt &ruiBits )
188  {
189  assert(m_fifo_idx < m_fifo->size());
190  ruiBits = (*m_fifo)[m_fifo_idx++];
191  }
192 
194  {
195  assert(m_fifo_idx > 0);
196  byte = (*m_fifo)[m_fifo_idx - 1];
197  }
198 
202  UInt getByteLocation ( ) { return m_fifo_idx ; }
203 
204  // Peek at bits in word-storage. Used in determining if we have completed reading of current bitstream and therefore slice in LCEC.
205  UInt peekBits (UInt uiBits) { UInt tmp; pseudoRead(uiBits, tmp); return tmp; }
206 
207  // utility functions
208  UInt read(UInt numberOfBits) { UInt tmp; read(numberOfBits, tmp); return tmp; }
209  UInt readByte() { UInt tmp; readByte( tmp ); return tmp; }
210  UInt getNumBitsUntilByteAligned() { return m_num_held_bits & (0x7); }
211  UInt getNumBitsLeft() { return 8*((UInt)m_fifo->size() - m_fifo_idx) + m_num_held_bits; }
212  TComInputBitstream *extractSubstream( UInt uiNumBits ); // Read the nominated number of bits, and return as a bitstream.
213  Void deleteFifo(); // Delete internal fifo of bitstream.
216 
217  Void pushEmulationPreventionByteLocation ( UInt pos ) { m_emulationPreventionByteLocation.push_back( pos ); }
218  UInt numEmulationPreventionBytesRead () { return (UInt) m_emulationPreventionByteLocation.size(); }
220  UInt getEmulationPreventionByteLocation ( UInt idx ) { return m_emulationPreventionByteLocation[ idx ]; }
221  Void clearEmulationPreventionByteLocation() { m_emulationPreventionByteLocation.clear(); }
222  Void setEmulationPreventionByteLocation ( std::vector<UInt> vec ) { m_emulationPreventionByteLocation = vec; }
223 
224  const std::vector<uint8_t> *getFifo() const { return m_fifo; }
225 };
226 
228 
229 #endif
TComInputBitstream(std::vector< uint8_t > *buf)
Void insertAt(const TComOutputBitstream &src, UInt pos)
UInt getNumBitsUntilByteAligned()
UInt getEmulationPreventionByteLocation(UInt idx)
virtual Void resetBits()=0
virtual Void write(UInt uiBits, UInt uiNumberOfBits)=0
Defines constants, macros and tool parameters.
void Void
Definition: TypeDef.h:285
TComInputBitstream * extractSubstream(UInt uiNumBits)
pure virtual class for basic bit handling
Definition: TComBitStream.h:58
char Char
Definition: TypeDef.h:291
unsigned int UInt
Definition: TypeDef.h:297
const std::vector< uint8_t > * getFifo() const
Void setEmulationPreventionByteLocation(std::vector< UInt > vec)
TComOutputBitstream & operator=(const TComOutputBitstream &src)
virtual ~TComBitIf()
Definition: TComBitStream.h:66
std::vector< uint8_t > * m_fifo
Void pushEmulationPreventionByteLocation(UInt pos)
UInt numEmulationPreventionBytesRead()
Int countStartCodeEmulations()
returns the number of start code emulations contained in the current buffer
std::vector< uint8_t > & getFIFO()
const std::vector< uint8_t > & getFIFO() const
Void peekPreviousByte(UInt &byte)
UChar m_held_bits
number of bits not flushed to bytestream.
Definition: TComBitStream.h:85
UInt getNumberOfWrittenBits() const
Char * getByteStream() const
std::vector< uint8_t > m_fifo
Definition: TComBitStream.h:82
unsigned char UChar
Definition: TypeDef.h:293
Void pseudoRead(UInt uiNumberOfBits, UInt &ruiBits)
UInt read(UInt numberOfBits)
std::vector< UInt > getEmulationPreventionByteLocation()
std::vector< UInt > m_emulationPreventionByteLocation
FIFO for storage of complete bytes.
Void read(UInt uiNumberOfBits, UInt &ruiBits)
Void readByte(UInt &ruiBits)
virtual Void writeAlignZero()
Definition: TComBitStream.h:62
TComOutputBitstream()
this value is always msb-aligned, bigendian.
UInt m_num_held_bits
Read index into m_fifo.
int Int
Definition: TypeDef.h:296
Void write(UInt uiBits, UInt uiNumberOfBits)
UInt peekBits(UInt uiBits)
virtual Void writeAlignOne()
Definition: TComBitStream.h:61
Void addSubstream(TComOutputBitstream *pcSubstream)
Int getNumBitsUntilByteAligned()
virtual UInt getNumberOfWrittenBits() const =0
Void clearEmulationPreventionByteLocation()