| 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 | #include "absl/strings/string_view.h" |
| 18 | #include "api/units/data_size.h" |
| 19 |
| 20 | namespace rtc { |
| 21 |
| 22 | // A BitBuffer API for write operations. Supports symmetric write APIs to the |
| 23 | // reading APIs of BitstreamReader. |
| 24 | // Sizes/counts specify bits/bytes, for clarity. |
| 25 | // Byte order is assumed big-endian/network. |
| 26 | class BitBufferWriter { |
| 27 | public: |
| 28 | static constexpr webrtc::DataSize kMaxLeb128Length = |
| 29 | webrtc::DataSize::Bytes(10); |
| 30 |
| 31 | // Constructs a bit buffer for the writable buffer of `bytes`. |
| 32 | BitBufferWriter(uint8_t* bytes, size_t byte_count); |
| 33 |
| 34 | BitBufferWriter(const BitBufferWriter&) = delete; |
| 35 | BitBufferWriter& operator=(const BitBufferWriter&) = delete; |
| 36 |
| 37 | // Gets the current offset, in bytes/bits, from the start of the buffer. The |
| 38 | // bit offset is the offset into the current byte, in the range [0,7]. |
| 39 | void GetCurrentOffset(size_t* out_byte_offset, size_t* out_bit_offset); |
| 40 |
| 41 | // The remaining bits in the byte buffer. |
| 42 | uint64_t RemainingBitCount() const; |
| 43 |
| 44 | // Moves current position `byte_count` bytes forward. Returns false if |
| 45 | // there aren't enough bytes left in the buffer. |
| 46 | bool ConsumeBytes(size_t byte_count); |
| 47 | // Moves current position `bit_count` bits forward. Returns false if |
| 48 | // there aren't enough bits left in the buffer. |
| 49 | bool ConsumeBits(size_t bit_count); |
| 50 |
| 51 | // Sets the current offset to the provied byte/bit offsets. The bit |
| 52 | // offset is from the given byte, in the range [0,7]. |
| 53 | bool Seek(size_t byte_offset, size_t bit_offset); |
| 54 |
| 55 | // Writes byte-sized values from the buffer. Returns false if there isn't |
| 56 | // enough data left for the specified type. |
| 57 | bool WriteUInt8(uint8_t val); |
| 58 | bool WriteUInt16(uint16_t val); |
| 59 | bool WriteUInt32(uint32_t val); |
| 60 |
| 61 | // Writes bit-sized values to the buffer. Returns false if there isn't enough |
| 62 | // room left for the specified number of bits. |
| 63 | bool WriteBits(uint64_t val, size_t bit_count); |
| 64 |
| 65 | // Writes value in range [0, num_values - 1] |
| 66 | // See ReadNonSymmetric documentation for the format, |
| 67 | // Call SizeNonSymmetricBits to get number of bits needed to store the value. |
| 68 | // Returns false if there isn't enough room left for the value. |
| 69 | bool WriteNonSymmetric(uint32_t val, uint32_t num_values); |
| 70 | // Returns number of bits required to store `val` with NonSymmetric encoding. |
| 71 | static size_t SizeNonSymmetricBits(uint32_t val, uint32_t num_values); |
| 72 |
| 73 | // Writes the exponential golomb encoded version of the supplied value. |
| 74 | // Returns false if there isn't enough room left for the value. |
| 75 | bool WriteExponentialGolomb(uint32_t val); |
| 76 | // Writes the signed exponential golomb version of the supplied value. |
| 77 | // Signed exponential golomb values are just the unsigned values mapped to the |
| 78 | // sequence 0, 1, -1, 2, -2, etc. in order. |
| 79 | bool WriteSignedExponentialGolomb(int32_t val); |
| 80 |
| 81 | // Writes the Leb128 encoded value. |
| 82 | bool WriteLeb128(uint64_t val); |
| 83 |
| 84 | // Writes the string as bytes of data. |
| 85 | bool WriteString(absl::string_view data); |
| 86 |
| 87 | private: |
| 88 | // The buffer, as a writable array. |
| 89 | uint8_t* const writable_bytes_; |
| 90 | // The total size of `bytes_`. |
| 91 | const size_t byte_count_; |
| 92 | // The current offset, in bytes, from the start of `bytes_`. |
| 93 | size_t byte_offset_; |
| 94 | // The current offset, in bits, into the current byte. |
| 95 | size_t bit_offset_; |
| 96 | }; |
| 97 |
| 98 | } // namespace rtc |
| 99 |
| 100 | #endif // RTC_BASE_BIT_BUFFER_H_ |
| 101 |