| 1 | #ifndef IOUTILITIES_BITREADER_H |
| 2 | #define IOUTILITIES_BITREADER_H |
| 3 | |
| 4 | #include "../global.h" |
| 5 | |
| 6 | #include <cstdint> |
| 7 | #include <ios> |
| 8 | #include <iostream> |
| 9 | #include <type_traits> |
| 10 | |
| 11 | namespace CppUtilities { |
| 12 | |
| 13 | class CPP_UTILITIES_EXPORT BitReader { |
| 14 | public: |
| 15 | BitReader(const char *buffer, std::size_t bufferSize); |
| 16 | BitReader(const char *buffer, const char *end); |
| 17 | |
| 18 | template <typename intType> intType readBits(std::uint8_t bitCount); |
| 19 | std::uint8_t readBit(); |
| 20 | template <typename intType> intType readUnsignedExpGolombCodedBits(); |
| 21 | template <typename intType> intType readSignedExpGolombCodedBits(); |
| 22 | template <typename intType> intType showBits(std::uint8_t bitCount); |
| 23 | void skipBits(std::size_t bitCount); |
| 24 | void align(); |
| 25 | std::size_t bitsAvailable(); |
| 26 | void reset(const char *buffer, std::size_t bufferSize); |
| 27 | void reset(const char *buffer, const char *end); |
| 28 | |
| 29 | private: |
| 30 | const std::uint8_t *m_buffer; |
| 31 | const std::uint8_t *m_end; |
| 32 | std::uint8_t m_bitsAvail; |
| 33 | }; |
| 34 | |
| 35 | /*! |
| 36 | * \brief Constructs a new BitReader. |
| 37 | * \remarks |
| 38 | * - Does not take ownership over the specified \a buffer. |
| 39 | * - bufferSize must be equal or greater than 1. |
| 40 | */ |
| 41 | inline BitReader::BitReader(const char *buffer, std::size_t bufferSize) |
| 42 | : BitReader(buffer, buffer + bufferSize) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | /*! |
| 47 | * \brief Constructs a new BitReader. |
| 48 | * \remarks |
| 49 | * - Does not take ownership over the specified \a buffer. |
| 50 | * - \a end must be greater than \a buffer. |
| 51 | */ |
| 52 | inline BitReader::BitReader(const char *buffer, const char *end) |
| 53 | : m_buffer(reinterpret_cast<const std::uint8_t *>(buffer)) |
| 54 | , m_end(reinterpret_cast<const std::uint8_t *>(end)) |
| 55 | , m_bitsAvail(8) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | /*! |
| 60 | * \brief Reads the specified number of bits from the buffer advancing the current position by \a bitCount bits. |
| 61 | * \param bitCount Specifies the number of bits read. |
| 62 | * \tparam intType Specifies the type of the returned value. |
| 63 | * \remarks Does not check whether intType is big enough to hold result. |
| 64 | * \throws Throws ios_base::failure if the end of the buffer is exceeded. |
| 65 | * The reader becomes invalid in that case. |
| 66 | */ |
| 67 | template <typename intType> intType BitReader::readBits(std::uint8_t bitCount) |
| 68 | { |
| 69 | intType val = 0; |
| 70 | for (std::uint8_t readAtOnce; bitCount; bitCount -= readAtOnce) { |
| 71 | if (!m_bitsAvail) { |
| 72 | if (++m_buffer >= m_end) { |
| 73 | throw std::ios_base::failure("end of buffer exceeded"); |
| 74 | } |
| 75 | m_bitsAvail = 8; |
| 76 | } |
| 77 | readAtOnce = std::min(bitCount, m_bitsAvail); |
| 78 | val = static_cast<intType>( |
| 79 | (val << readAtOnce) | static_cast<intType>(((*m_buffer) >> (m_bitsAvail -= readAtOnce)) & (0xFF >> (0x08 - readAtOnce)))); |
| 80 | } |
| 81 | return val; |
| 82 | } |
| 83 | |
| 84 | /*! |
| 85 | * \brief Reads the one bit from the buffer advancing the current position by one bit. |
| 86 | * \throws Throws ios_base::failure if the end of the buffer is exceeded. |
| 87 | * The reader becomes invalid in that case. |
| 88 | */ |
| 89 | inline std::uint8_t BitReader::readBit() |
| 90 | { |
| 91 | return readBits<std::uint8_t>(1) == 1; |
| 92 | } |
| 93 | |
| 94 | /*! |
| 95 | * \brief Reads "Exp-Golomb coded" bits (unsigned). |
| 96 | * \tparam intType Specifies the type of the returned value. |
| 97 | * \remarks Does not check whether intType is big enough to hold result. |
| 98 | * \throws Throws ios_base::failure if the end of the buffer is exceeded. |
| 99 | * The reader becomes invalid in that case. |
| 100 | * \sa https://en.wikipedia.org/wiki/Exponential-Golomb_coding |
| 101 | */ |
| 102 | template <typename intType> intType BitReader::readUnsignedExpGolombCodedBits() |
| 103 | { |
| 104 | std::uint8_t count = 0; |
| 105 | while (!readBit()) { |
| 106 | ++count; |
| 107 | } |
| 108 | return count ? static_cast<intType>(((1 << count) | readBits<intType>(count)) - 1) : 0; |
| 109 | } |
| 110 | |
| 111 | /*! |
| 112 | * \brief Reads "Exp-Golomb coded" bits (signed). |
| 113 | * \tparam intType Specifies the type of the returned value which should be signed (obviously). |
| 114 | * \remarks Does not check whether intType is big enough to hold result. |
| 115 | * \throws Throws ios_base::failure if the end of the buffer is exceeded. |
| 116 | * The reader becomes invalid in that case. |
| 117 | * \sa https://en.wikipedia.org/wiki/Exponential-Golomb_coding |
| 118 | */ |
| 119 | template <typename intType> intType BitReader::readSignedExpGolombCodedBits() |
| 120 | { |
| 121 | auto value = readUnsignedExpGolombCodedBits<typename std::make_unsigned<intType>::type>(); |
| 122 | return (value % 2) ? static_cast<intType>((value + 1) / 2) : (-static_cast<intType>(value / 2)); |
| 123 | } |
| 124 | |
| 125 | /*! |
| 126 | * \brief Reads the specified number of bits from the buffer without advancing the current position. |
| 127 | */ |
| 128 | template <typename intType> intType BitReader::showBits(std::uint8_t bitCount) |
| 129 | { |
| 130 | auto tmp = *this; |
| 131 | return tmp.readBits<intType>(bitCount); |
| 132 | } |
| 133 | |
| 134 | /*! |
| 135 | * \brief Returns the number of bits which are still available to read. |
| 136 | */ |
| 137 | inline std::size_t BitReader::bitsAvailable() |
| 138 | { |
| 139 | return m_buffer != m_end ? static_cast<std::size_t>(((m_end - m_buffer - 1) * 8) + m_bitsAvail) : static_cast<std::size_t>(0); |
| 140 | } |
| 141 | |
| 142 | /*! |
| 143 | * \brief Resets the reader. |
| 144 | * \remarks |
| 145 | * - Does not take ownership over the specified \a buffer. |
| 146 | * - bufferSize must be equal or greater than 1. |
| 147 | */ |
| 148 | inline void BitReader::reset(const char *buffer, std::size_t bufferSize) |
| 149 | { |
| 150 | m_buffer = reinterpret_cast<const std::uint8_t *>(buffer); |
| 151 | m_end = reinterpret_cast<const std::uint8_t *>(buffer + bufferSize); |
| 152 | m_bitsAvail = 8; |
| 153 | } |
| 154 | |
| 155 | /*! |
| 156 | * \brief Resets the reader. |
| 157 | * \remarks |
| 158 | * - Does not take ownership over the specified \a buffer. |
| 159 | * - \a end must be greater than \a buffer. |
| 160 | */ |
| 161 | inline void BitReader::reset(const char *buffer, const char *end) |
| 162 | { |
| 163 | m_buffer = reinterpret_cast<const std::uint8_t *>(buffer); |
| 164 | m_end = reinterpret_cast<const std::uint8_t *>(end); |
| 165 | m_bitsAvail = 8; |
| 166 | } |
| 167 | |
| 168 | /*! |
| 169 | * \brief Re-establishes alignment. |
| 170 | */ |
| 171 | inline void BitReader::align() |
| 172 | { |
| 173 | skipBits(m_bitsAvail); |
| 174 | } |
| 175 | |
| 176 | } // namespace CppUtilities |
| 177 | |
| 178 | #endif // IOUTILITIES_BITREADER_H |