25 #ifndef COMMON_BITSTREAM_H 26 #define COMMON_BITSTREAM_H 30 #include <boost/noncopyable.hpp> 46 virtual size_t pos()
const = 0;
49 virtual size_t size()
const = 0;
52 virtual bool eos()
const = 0;
58 virtual void skip(
size_t n) = 0;
84 template<
int valueBits,
bool isLE,
bool isMSB2LSB>
100 return _stream->readUint32LE();
102 return _stream->readUint64LE();
107 return _stream->readUint16BE();
109 return _stream->readUint32BE();
111 return _stream->readUint64BE();
120 if ((
size() -
pos()) < valueBits)
121 throw Exception(
"BitStream::readValue(): End of bit stream reached");
127 _value <<= 64 - valueBits;
137 if ((valueBits != 8) && (valueBits != 16) && (valueBits != 32) && (valueBits != 64))
138 throw Exception(
"BitStream: Invalid memory layout %d, %d, %d", valueBits, isLE, isMSB2LSB);
145 if ((valueBits != 8) && (valueBits != 16) && (valueBits != 32) && (valueBits != 64))
146 throw Exception(
"BitStream: Invalid memory layout %d, %d, %d", valueBits, isLE, isMSB2LSB);
161 b = ((
_value & 0x8000000000000000ULL) == 0) ? 0 : 1;
163 b = ((
_value & 1) == 0) ? 0 : 1;
183 throw Exception(
"Too many bits requested to be read");
192 for (
uint32 i = 0; i < n; i++)
204 throw Exception(
"Too many bits requested to be read");
209 x = (x & ~(1 << n)) | (
getBit() << n);
231 size_t p = (
_inValue == 0) ?
_stream->pos() : ((
_stream->pos() - 1) & ~((
size_t) ((valueBits >> 3) - 1)));
237 return (
_stream->size() & ~((size_t) ((valueBits >> 3) - 1))) * 8;
281 #endif // COMMON_BITSTREAM_H BitStreamImpl< 32, true, true > BitStream32LEMSB
32-bit little-endian data, MSB to LSB.
BitStreamImpl< 16, true, false > BitStream16LELSB
16-bit little-endian data, LSB to MSB.
virtual size_t pos() const =0
Return the stream position in bits.
BitStreamImpl< 64, true, true > BitStream64LEMSB
64-bit little-endian data, MSB to LSB.
void rewind()
Rewind the bit stream back to the start.
BitStreamImpl< 16, false, false > BitStream16BELSB
16-bit big-endian data, LSB to MSB.
uint32 getBit()
Read a bit from the bit stream.
uint32 getBits(size_t n)
Read a multi-bit value from the bit stream.
BitStreamImpl< 16, false, true > BitStream16BEMSB
16-bit big-endian data, MSB to LSB.
BitStreamImpl< 16, true, true > BitStream16LEMSB
16-bit little-endian data, MSB to LSB.
size_t pos() const
Return the stream position in bits.
virtual void rewind()=0
Rewind the bit stream back to the start.
BitStreamImpl< 32, true, false > BitStream32LELSB
32-bit little-endian data, LSB to MSB.
void readValue()
Read the next data value.
BitStreamImpl< 32, false, false > BitStream32BELSB
32-bit big-endian data, LSB to MSB.
Basic exceptions to throw.
uint8 _inValue
Position within the current value.
BitStreamImpl< 8, false, false > BitStream8LSB
8-bit data, LSB to MSB.
virtual bool eos() const =0
Has the end of the stream been reached?
uint64 readData()
Read a data value.
virtual uint32 getBit()=0
Read a bit from the bit stream.
BitStreamImpl(SeekableReadStream *stream, bool disposeAfterUse=false)
Create a bit stream using this input data stream and optionally delete it on destruction.
Low-level type definitions to handle fixed width types portably.
BitStreamImpl(SeekableReadStream &stream)
Create a bit stream using this input data stream.
uint64 _value
Current value.
Basic reading stream interfaces.
A disposable plain pointer, allowing pointer-y access and normal deletion.
void skip(size_t n)
Skip the specified amount of bits.
A smart pointer with a deletion flag.
size_t size() const
Return the stream size in bits.
virtual void addBit(uint32 &x, size_t n)=0
Add a bit to the n-bit value x, making it an (n+1)-bit value.
void addBit(uint32 &x, size_t n)
Add a bit to the n-bit value x, making it an (n+1)-bit value.
virtual void skip(size_t n)=0
Skip the specified amount of bits.
bool eos() const
Has the end of the stream been reached?
DisposablePtr< SeekableReadStream > _stream
The input stream.
virtual uint32 getBits(size_t n)=0
Read a multi-bit value from the bit stream.
BitStreamImpl< 32, false, true > BitStream32BEMSB
32-bit big-endian data, MSB to LSB.
A template implementing a bit stream for different data memory layouts.
BitStreamImpl< 64, false, false > BitStream64BELSB
64-bit big-endian data, LSB to MSB.
BitStreamImpl< 64, false, true > BitStream64BEMSB
64-bit big-endian data, MSB to LSB.
Interface for a seekable & readable data stream.
virtual size_t size() const =0
Return the stream size in bits.
BitStreamImpl< 64, true, false > BitStream64LELSB
64-bit little-endian data, LSB to MSB.
BitStreamImpl< 8, false, true > BitStream8MSB
8-bit data, MSB to LSB.