24 #ifndef COMMON_BITSTREAM_H 25 #define COMMON_BITSTREAM_H 27 #include "common/scummsys.h" 28 #include "common/textconsole.h" 29 #include "common/stream.h" 30 #include "common/types.h" 31 #include "common/util.h" 54 template<
class STREAM,
typename CONTAINER,
int valueBits,
bool isLE,
bool MSB2LSB>
58 DisposeAfterUse::Flag _disposeAfterUse;
60 CONTAINER _bitContainer;
66 FORCEINLINE uint32 readData() {
69 return _stream->readByte();
71 return _stream->readUint16LE();
73 return _stream->readUint32LE();
76 return _stream->readByte();
78 return _stream->readUint16BE();
80 return _stream->readUint32BE();
88 FORCEINLINE
void fillContainer(
size_t min) {
89 while (_bitsLeft < min) {
92 if (_pos + _bitsLeft + valueBits <= _size) {
105 _bitContainer |= data << ((
sizeof(_bitContainer) * 8) - valueBits - _bitsLeft);
107 _bitContainer |= data << _bitsLeft;
109 _bitsLeft += valueBits;
114 FORCEINLINE
static uint32 getNBits(CONTAINER value,
size_t n) {
118 const size_t toShift = (
sizeof(value) * 8) - n;
121 return value >> toShift;
123 return (value << toShift) >> toShift;
127 FORCEINLINE
void skipBits(
size_t n) {
128 assert(n <= _bitsLeft);
142 BitStreamImpl(STREAM *stream, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::NO) :
143 _stream(stream), _disposeAfterUse(disposeAfterUse), _bitContainer(0), _bitsLeft(0), _pos(0) {
145 if ((valueBits != 8) && (valueBits != 16) && (valueBits != 32))
146 error(
"BitStreamImpl: Invalid memory layout %d, %d, %d", valueBits,
int(isLE),
int(MSB2LSB));
148 _size = (_stream->size() & ~((uint32) ((valueBits >> 3) - 1))) * 8;
153 _stream(&stream), _disposeAfterUse(
DisposeAfterUse::NO), _bitContainer(0), _bitsLeft(0), _pos(0) {
155 if ((valueBits != 8) && (valueBits != 16) && (valueBits != 32))
156 error(
"BitStreamImpl: Invalid memory layout %d, %d, %d", valueBits,
int(isLE),
int(MSB2LSB));
158 _size = (_stream->size() & ~((uint32) ((valueBits >> 3) - 1))) * 8;
162 if (_disposeAfterUse == DisposeAfterUse::YES)
170 return getNBits(_bitContainer, 1);
190 error(
"BitStreamImpl::peekBits(): Too many bits requested to be peeked");
193 return getNBits(_bitContainer, n);
209 error(
"BitStreamImpl::getBits(): Too many bits requested to be read");
211 const uint32 b = peekBits<n>();
225 error(
"BitStreamImpl::peekBits(): Too many bits requested to be peeked");
228 return getNBits(_bitContainer, n);
243 error(
"BitStreamImpl::getBits(): Too many bits requested to be read");
265 error(
"BitStreamImpl::addBit(): Too many bits requested to be read");
270 x = (x & ~(1 << n)) | (
getBit() << n);
284 if (n >= _bitsLeft) {
301 uint32 bitsAfterBoundary = _pos % valueBits;
302 if (bitsAfterBoundary) {
303 skip(valueBits - bitsAfterBoundary);
318 return _stream->eos() || (_pos >= _size);
321 static bool isMSB2LSB() {
339 const byte *
const _ptrOrig;
343 DisposeAfterUse::Flag _disposeMemory;
347 BitStreamMemoryStream(
const byte *dataPtr, uint32 dataSize, DisposeAfterUse::Flag disposeMemory = DisposeAfterUse::NO) :
352 _disposeMemory(disposeMemory),
357 free(const_cast<byte *>(_ptrOrig));
372 uint32
size()
const {
376 bool seek(uint32 offset) {
377 assert(offset <= _size);
381 _ptr = _ptrOrig + _pos;
395 uint16 readUint16LE() {
396 if (_pos + 2 > _size) {
406 uint16 val = READ_LE_UINT16(_ptr);
414 uint16 readUint16BE() {
415 if (_pos + 2 > _size) {
419 return (*_ptr++) << 8;
425 uint16 val = READ_LE_UINT16(_ptr);
433 uint32 readUint32LE() {
434 if (_pos + 4 > _size) {
435 uint32 val = readByte();
436 val |= (uint32)readByte() << 8;
437 val |= (uint32)readByte() << 16;
438 val |= (uint32)readByte() << 24;
443 uint32 val = READ_LE_UINT32(_ptr);
451 uint32 readUint32BE() {
452 if (_pos + 4 > _size) {
453 uint32 val = (uint32)readByte() << 24;
454 val |= (uint32)readByte() << 16;
455 val |= (uint32)readByte() << 8;
456 val |= (uint32)readByte();
461 uint32 val = READ_BE_UINT32(_ptr);
530 #endif // COMMON_BITSTREAM_H BitStreamImpl< SeekableReadStream, uint64, 32, true, false > BitStream32LELSB
Definition: bitstream.h:493
BitStreamImpl< BitStreamMemoryStream, uint64, 32, false, true > BitStreamMemory32BEMSB
Definition: bitstream.h:520
void align()
Definition: bitstream.h:300
void skip(uint32 n)
Definition: bitstream.h:283
uint peekBit()
Definition: bitstream.h:167
uint32 getBits()
Definition: bitstream.h:207
BitStreamImpl< BitStreamMemoryStream, uint64, 32, false, false > BitStreamMemory32BELSB
Definition: bitstream.h:522
BitStreamImpl< SeekableReadStream, uint64, 32, false, false > BitStream32BELSB
Definition: bitstream.h:497
BitStreamImpl< BitStreamMemoryStream, uint64, 16, true, false > BitStreamMemory16LELSB
Definition: bitstream.h:509
uint32 getBits(size_t n)
Definition: bitstream.h:241
BitStreamImpl(STREAM &stream)
Definition: bitstream.h:152
BitStreamImpl< SeekableReadStream, uint64, 16, true, false > BitStream16LELSB
Definition: bitstream.h:484
BitStreamImpl< BitStreamMemoryStream, uint64, 8, false, true > BitStreamMemory8MSB
Definition: bitstream.h:502
BitStreamImpl< SeekableReadStream, uint64, 32, true, true > BitStream32LEMSB
Definition: bitstream.h:491
BitStreamImpl< SeekableReadStream, uint64, 32, false, true > BitStream32BEMSB
Definition: bitstream.h:495
Definition: bitstream.h:55
Definition: algorithm.h:29
BitStreamImpl< SeekableReadStream, uint64, 8, false, false > BitStream8LSB
Definition: bitstream.h:479
uint32 size() const
Definition: bitstream.h:313
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
BitStreamImpl(STREAM *stream, DisposeAfterUse::Flag disposeAfterUse=DisposeAfterUse::NO)
Definition: bitstream.h:142
BitStreamImpl< BitStreamMemoryStream, uint64, 16, false, true > BitStreamMemory16BEMSB
Definition: bitstream.h:511
BitStreamImpl< SeekableReadStream, uint64, 8, false, true > BitStream8MSB
Definition: bitstream.h:477
uint32 pos() const
Definition: bitstream.h:308
BitStreamImpl< SeekableReadStream, uint64, 16, true, true > BitStream16LEMSB
Definition: bitstream.h:482
BitStreamImpl< BitStreamMemoryStream, uint64, 32, true, true > BitStreamMemory32LEMSB
Definition: bitstream.h:516
uint getBit()
Definition: bitstream.h:174
void rewind()
Definition: bitstream.h:274
void addBit(uint32 &x, uint32 n)
Definition: bitstream.h:263
BitStreamImpl< BitStreamMemoryStream, uint64, 16, false, false > BitStreamMemory16BELSB
Definition: bitstream.h:513
Definition: bitstream.h:337
uint32 peekBits()
Definition: bitstream.h:188
BitStreamImpl< SeekableReadStream, uint64, 16, false, true > BitStream16BEMSB
Definition: bitstream.h:486
BitStreamImpl< BitStreamMemoryStream, uint64, 8, false, false > BitStreamMemory8LSB
Definition: bitstream.h:504
BitStreamImpl< SeekableReadStream, uint64, 16, false, false > BitStream16BELSB
Definition: bitstream.h:488
BitStreamMemoryStream(const byte *dataPtr, uint32 dataSize, DisposeAfterUse::Flag disposeMemory=DisposeAfterUse::NO)
Definition: bitstream.h:347
uint32 peekBits(size_t n)
Definition: bitstream.h:223
BitStreamImpl< BitStreamMemoryStream, uint64, 16, true, true > BitStreamMemory16LEMSB
Definition: bitstream.h:507
BitStreamImpl< BitStreamMemoryStream, uint64, 32, true, false > BitStreamMemory32LELSB
Definition: bitstream.h:518