| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #ifndef RTC_BASE_BIT_BUFFER_H_ |
| 12 | #define RTC_BASE_BIT_BUFFER_H_ |
| 13 |
| 14 | #include <stddef.h> // For size_t. |
| 15 | #include <stdint.h> // For integer types. |
| 16 |
| 17 | namespace rtc { |
| 18 |
| 19 | // A BitBuffer API for write operations. Supports symmetric write APIs to the |
| 20 | // reading APIs of BitstreamReader. |
| 21 | // Sizes/counts specify bits/bytes, for clarity. |
| 22 | // Byte order is assumed big-endian/network. |
| 23 | class BitBufferWriter { |
| 24 | public: |
| 25 | // Constructs a bit buffer for the writable buffer of `bytes`. |
| 26 | BitBufferWriter(uint8_t* bytes, size_t byte_count); |
| 27 |
| 28 | BitBufferWriter(const BitBufferWriter&) = delete; |
| 29 | BitBufferWriter& operator=(const BitBufferWriter&) = delete; |
| 30 |
| 31 | // Gets the current offset, in bytes/bits, from the start of the buffer. The |
| 32 | // bit offset is the offset into the current byte, in the range [0,7]. |
| 33 | void GetCurrentOffset(size_t* out_byte_offset, size_t* out_bit_offset); |
| 34 |
| 35 | // The remaining bits in the byte buffer. |
| 36 | uint64_t RemainingBitCount() const; |
| 37 |
| 38 | // Reads byte-sized values from the buffer. Returns false if there isn't |
| 39 | // enough data left for the specified type. |
| 40 | bool ReadUInt8(uint8_t& val); |
| 41 | bool ReadUInt16(uint16_t& val); |
| 42 | bool ReadUInt32(uint32_t& val); |
| 43 | bool ReadUInt8(uint8_t* val) { |
| 44 | return val ? ReadUInt8(*val) : false; |
| 45 | } |
| 46 | bool ReadUInt16(uint16_t* val) { |
| 47 | return val ? ReadUInt16(*val) : false; |
| 48 | } |
| 49 | bool ReadUInt32(uint32_t* val) { |
| 50 | return val ? ReadUInt32(*val) : false; |
| 51 | } |
| 52 |
| 53 | // Reads bit-sized values from the buffer. Returns false if there isn't enough |
| 54 | // data left for the specified bit count. |
| 55 | bool ReadBits(size_t bit_count, uint32_t& val); |
| 56 | bool ReadBits(size_t bit_count, uint64_t& val); |
| 57 | bool ReadBits(uint32_t* val, size_t bit_count) { |
| 58 | return val ? ReadBits(bit_count, *val) : false; |
| 59 | } |
| 60 |
| 61 | // Peeks bit-sized values from the buffer. Returns false if there isn't enough |
| 62 | // data left for the specified number of bits. Doesn't move the current |
| 63 | // offset. |
| 64 | bool PeekBits(size_t bit_count, uint32_t& val); |
| 65 | bool PeekBits(size_t bit_count, uint64_t& val); |
| 66 | bool PeekBits(uint32_t* val, size_t bit_count) { |
| 67 | return val ? PeekBits(bit_count, *val) : false; |
| 68 | } |
| 69 |
| 70 | // Reads value in range [0, num_values - 1]. |
| 71 | // This encoding is similar to ReadBits(val, Ceil(Log2(num_values)), |
| 72 | // but reduces wastage incurred when encoding non-power of two value ranges |
| 73 | // Non symmetric values are encoded as: |
| 74 | // 1) n = countbits(num_values) |
| 75 | // 2) k = (1 << n) - num_values |
| 76 | // Value v in range [0, k - 1] is encoded in (n-1) bits. |
| 77 | // Value v in range [k, num_values - 1] is encoded as (v+k) in n bits. |
| 78 | // https://aomediacodec.github.io/av1-spec/#nsn |
| 79 | // Returns false if there isn't enough data left. |
| 80 | bool ReadNonSymmetric(uint32_t num_values, uint32_t& val); |
| 81 | bool ReadNonSymmetric(uint32_t* val, uint32_t num_values) { |
| 82 | return val ? ReadNonSymmetric(num_values, *val) : false; |
| 83 | } |
| 84 |
| 85 | // Reads the exponential golomb encoded value at the current offset. |
| 86 | // Exponential golomb values are encoded as: |
| 87 | // 1) x = source val + 1 |
| 88 | // 2) In binary, write [countbits(x) - 1] 0s, then x |
| 89 | // To decode, we count the number of leading 0 bits, read that many + 1 bits, |
| 90 | // and increment the result by 1. |
| 91 | // Returns false if there isn't enough data left for the specified type, or if |
| 92 | // the value wouldn't fit in a uint32_t. |
| 93 | bool ReadExponentialGolomb(uint32_t& val); |
| 94 | bool ReadExponentialGolomb(uint32_t* val) { |
| 95 | return val ? ReadExponentialGolomb(*val) : false; |
| 96 | } |
| 97 |
| 98 | // Reads signed exponential golomb values at the current offset. Signed |
| 99 | // exponential golomb values are just the unsigned values mapped to the |
| 100 | // sequence 0, 1, -1, 2, -2, etc. in order. |
| 101 | bool ReadSignedExponentialGolomb(int32_t& val); |
| 102 | bool ReadSignedExponentialGolomb(int32_t* val) { |
| 103 | return val ? ReadSignedExponentialGolomb(*val) : false; |
| 104 | } |
| 105 |
| 106 | // Moves current position `byte_count` bytes forward. Returns false if |
| 107 | // there aren't enough bytes left in the buffer. |
| 108 | bool ConsumeBytes(size_t byte_count); |
| 109 | // Moves current position `bit_count` bits forward. Returns false if |
| 110 | // there aren't enough bits left in the buffer. |
| 111 | bool ConsumeBits(size_t bit_count); |
| 112 |
| 113 | // Sets the current offset to the provied byte/bit offsets. The bit |
| 114 | // offset is from the given byte, in the range [0,7]. |
| 115 | bool Seek(size_t byte_offset, size_t bit_offset); |
| 116 |
| 117 | // Writes byte-sized values from the buffer. Returns false if there isn't |
| 118 | // enough data left for the specified type. |
| 119 | bool WriteUInt8(uint8_t val); |
| 120 | bool WriteUInt16(uint16_t val); |
| 121 | bool WriteUInt32(uint32_t val); |
| 122 |
| 123 | // Writes bit-sized values to the buffer. Returns false if there isn't enough |
| 124 | // room left for the specified number of bits. |
| 125 | bool WriteBits(uint64_t val, size_t bit_count); |
| 126 |
| 127 | // Writes value in range [0, num_values - 1] |
| 128 | // See ReadNonSymmetric documentation for the format, |
| 129 | // Call SizeNonSymmetricBits to get number of bits needed to store the value. |
| 130 | // Returns false if there isn't enough room left for the value. |
| 131 | bool WriteNonSymmetric(uint32_t val, uint32_t num_values); |
| 132 | // Returns number of bits required to store `val` with NonSymmetric encoding. |
| 133 | static size_t SizeNonSymmetricBits(uint32_t val, uint32_t num_values); |
| 134 |
| 135 | // Writes the exponential golomb encoded version of the supplied value. |
| 136 | // Returns false if there isn't enough room left for the value. |
| 137 | bool WriteExponentialGolomb(uint32_t val); |
| 138 | // Writes the signed exponential golomb version of the supplied value. |
| 139 | // Signed exponential golomb values are just the unsigned values mapped to the |
| 140 | // sequence 0, 1, -1, 2, -2, etc. in order. |
| 141 | bool WriteSignedExponentialGolomb(int32_t val); |
| 142 |
| 143 | private: |
| 144 | // The buffer, as a writable array. |
| 145 | uint8_t* const writable_bytes_; |
| 146 | // The total size of `bytes_`. |
| 147 | const size_t byte_count_; |
| 148 | // The current offset, in bytes, from the start of `bytes_`. |
| 149 | size_t byte_offset_; |
| 150 | // The current offset, in bits, into the current byte. |
| 151 | size_t bit_offset_; |
| 152 | }; |
| 153 |
| 154 | } // namespace rtc |
| 155 |
| 156 | #endif // RTC_BASE_BIT_BUFFER_H_ |
| 157 |