12 #include <type_traits>
35 using byte =
unsigned char;
37 size_t firstBitOffset = 0;
38 size_t currentBitPosition = 0;
39 const byte* buffer =
nullptr;
46 firstBitOffset(firstBitOffsetInBuffer),
47 currentBitPosition(firstBitOffsetInBuffer),
48 buffer(static_cast<const byte*>(inputBuffer))
56 if (newbufferSizeInBits == 0)
return {};
57 const size_t curBytesPos = currentBitPosition / CHAR_BIT;
58 const size_t bitPosInCurByte = currentBitPosition % CHAR_BIT;
59 assert(
tell() + newbufferSizeInBits <= size);
61 return {buffer + curBytesPos, newbufferSizeInBits, bitPosInCurByte};
65 size_t tell()
const {
return currentBitPosition - firstBitOffset; }
69 assert(newPosition >= 0_z && newPosition < size);
70 currentBitPosition = newPosition + firstBitOffset;
78 currentBitPosition += nbBits;
84 currentBitPosition = (currentBitPosition + 7_z) & (~0x7_z);
87 size_t bufferSizeInBytes()
const {
return (size + firstBitOffset + 7) / CHAR_BIT; }
98 const size_t currentBytesPos = currentBitPosition / CHAR_BIT;
99 const size_t bitPosInCurrentByte = currentBitPosition % CHAR_BIT;
100 const int mask = (1 << bitPosInCurrentByte);
101 currentBitPosition++;
102 return (buffer[currentBytesPos] & mask) == mask;
111 template<
typename RetType = u
int32_t>
114 static_assert(std::is_unsigned<RetType>::value,
"You must return an unsigned type !");
116 size_t curBytesPos = currentBitPosition / CHAR_BIT;
117 size_t bitPosInCurByte = currentBitPosition % CHAR_BIT;
118 currentBitPosition += nbBits;
120 size_t curDestBitPosition = 0;
121 while (curDestBitPosition < nbBits)
124 const size_t bitsToReadInCurByte =
125 std::min(CHAR_BIT - bitPosInCurByte, nbBits - curDestBitPosition);
126 const RetType mask = RetType((1U << bitsToReadInCurByte) - 1U);
128 const RetType inBits = (RetType(buffer[curBytesPos++]) >> bitPosInCurByte) & mask;
130 value |= inBits << curDestBitPosition;
131 curDestBitPosition += bitsToReadInCurByte;
139 uint8_t readUnsigned8OrLess(
const int nbBits)
141 const size_t curBytesPos = currentBitPosition / CHAR_BIT;
142 const int bitPosInCurByte = currentBitPosition % CHAR_BIT;
143 currentBitPosition += size_t(nbBits);
145 const uint16_t shortFromBuffer =
146 buffer[curBytesPos] |
147 ((bitPosInCurByte + nbBits > 8) ? uint16_t(buffer[curBytesPos + 1] << CHAR_BIT) : 0);
148 const unsigned mask = 0xFF >> (CHAR_BIT - nbBits);
149 const uint8_t value = uint8_t((shortFromBuffer >> bitPosInCurByte) & mask);
160 template<
unsigned NbBits,
typename std::enable_if<(NbBits > 0)>::type* =
nullptr>
163 return Utils::signExtend<int32_t, NbBits>(
readUnsigned(NbBits));
171 template<unsigned NbBits, typename std::enable_if<(NbBits == 0)>::type* =
nullptr>
174 currentBitPosition += NbBits;
This class reuses the parts of std::ios API but doesn't provide heavy stream functionnality (ie...
uint32_t readBit()
Reads a single bit from the stream (uint32_t version)
RetType readUnsigned(unsigned nbBits)
Reads an unsigned value of variable bit size.
bool readBool()
Reads a single bit from the stream.
BitStreamView(const void *inputBuffer, size_t sizeInBits, size_t firstBitOffsetInBuffer=0)
Creates a bitstream from raw memory.
size_t bitPositionInBuffer() const
Returns the current position in the buffer (ignoring the first bit position) in bits.
size_t bufferSizeInBits() const
Returns the total size of the current stream buffer in bits.
void setPosition(size_t newPosition)
Set the current position, in bits.
size_t tell() const
Returns the current position in the stream in bits.
size_t sizeInBits() const
Returns the total size of the current stream buffer in bytes.
int32_t readSigned()
Reads an signed value of variable bit size.
void skip(size_t nbBits)
Skips the next nbBits bits.
Provides access to a variable bitsize values stream.
uint32_t read0Bits()
Return 0u, used for member function tables as replacement for BitStream::readUnsigned<0> ...