Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 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 WEBRTC_BASE_BITBUFFER_H_ |
| 12 | #define WEBRTC_BASE_BITBUFFER_H_ |
| 13 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 14 | #include <stdint.h> // For integer types. |
| 15 | #include <stddef.h> // For size_t. |
| 16 | |
| 17 | #include "webrtc/base/constructormagic.h" |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 18 | |
| 19 | namespace rtc { |
| 20 | |
| 21 | // A class, similar to ByteBuffer, that can parse bit-sized data out of a set of |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 22 | // bytes. Has a similar API to ByteBuffer, plus methods for reading bit-sized |
| 23 | // and exponential golomb encoded data. For a writable version, use |
| 24 | // BitBufferWriter. Unlike ByteBuffer, this class doesn't make a copy of the |
| 25 | // source bytes, so it can be used on read-only data. |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 26 | // Sizes/counts specify bits/bytes, for clarity. |
| 27 | // Byte order is assumed big-endian/network. |
| 28 | class BitBuffer { |
| 29 | public: |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 30 | BitBuffer(const uint8_t* bytes, size_t byte_count); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 31 | |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 32 | // Gets the current offset, in bytes/bits, from the start of the buffer. The |
| 33 | // bit offset is the offset into the current byte, in the range [0,7]. |
| 34 | void GetCurrentOffset(size_t* out_byte_offset, size_t* out_bit_offset); |
| 35 | |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 36 | // The remaining bits in the byte buffer. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 37 | uint64_t RemainingBitCount() const; |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 38 | |
| 39 | // Reads byte-sized values from the buffer. Returns false if there isn't |
| 40 | // enough data left for the specified type. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 41 | bool ReadUInt8(uint8_t* val); |
| 42 | bool ReadUInt16(uint16_t* val); |
| 43 | bool ReadUInt32(uint32_t* val); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 44 | |
| 45 | // Reads bit-sized values from the buffer. Returns false if there isn't enough |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 46 | // data left for the specified bit count.. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 47 | bool ReadBits(uint32_t* val, size_t bit_count); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 48 | |
| 49 | // Peeks bit-sized values from the buffer. Returns false if there isn't enough |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 50 | // data left for the specified number of bits. Doesn't move the current |
| 51 | // offset. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 52 | bool PeekBits(uint32_t* val, size_t bit_count); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 53 | |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 54 | // Reads the exponential golomb encoded value at the current offset. |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 55 | // Exponential golomb values are encoded as: |
| 56 | // 1) x = source val + 1 |
| 57 | // 2) In binary, write [countbits(x) - 1] 0s, then x |
| 58 | // To decode, we count the number of leading 0 bits, read that many + 1 bits, |
| 59 | // and increment the result by 1. |
| 60 | // Returns false if there isn't enough data left for the specified type, or if |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 61 | // the value wouldn't fit in a uint32_t. |
| 62 | bool ReadExponentialGolomb(uint32_t* val); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 63 | |
| 64 | // Moves current position |byte_count| bytes forward. Returns false if |
| 65 | // there aren't enough bytes left in the buffer. |
| 66 | bool ConsumeBytes(size_t byte_count); |
| 67 | // Moves current position |bit_count| bits forward. Returns false if |
| 68 | // there aren't enough bits left in the buffer. |
| 69 | bool ConsumeBits(size_t bit_count); |
| 70 | |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 71 | // Sets the current offset to the provied byte/bit offsets. The bit |
| 72 | // offset is from the given byte, in the range [0,7]. |
| 73 | bool Seek(size_t byte_offset, size_t bit_offset); |
| 74 | |
| 75 | protected: |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 76 | const uint8_t* const bytes_; |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 77 | // The total size of |bytes_|. |
| 78 | size_t byte_count_; |
| 79 | // The current offset, in bytes, from the start of |bytes_|. |
| 80 | size_t byte_offset_; |
| 81 | // The current offset, in bits, into the current byte. |
| 82 | size_t bit_offset_; |
| 83 | |
| 84 | DISALLOW_COPY_AND_ASSIGN(BitBuffer); |
| 85 | }; |
| 86 | |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 87 | // A BitBuffer API for write operations. Supports symmetric write APIs to the |
| 88 | // reading APIs of BitBuffer. Note that the read/write offset is shared with the |
| 89 | // BitBuffer API, so both reading and writing will consume bytes/bits. |
| 90 | class BitBufferWriter : public BitBuffer { |
| 91 | public: |
| 92 | // Constructs a bit buffer for the writable buffer of |bytes|. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 93 | BitBufferWriter(uint8_t* bytes, size_t byte_count); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 94 | |
| 95 | // Writes byte-sized values from the buffer. Returns false if there isn't |
| 96 | // enough data left for the specified type. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 97 | bool WriteUInt8(uint8_t val); |
| 98 | bool WriteUInt16(uint16_t val); |
| 99 | bool WriteUInt32(uint32_t val); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 100 | |
| 101 | // Writes bit-sized values to the buffer. Returns false if there isn't enough |
| 102 | // room left for the specified number of bits. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 103 | bool WriteBits(uint64_t val, size_t bit_count); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 104 | |
| 105 | // Writes the exponential golomb encoded version of the supplied value. |
| 106 | // Returns false if there isn't enough room left for the value. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 107 | bool WriteExponentialGolomb(uint32_t val); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 108 | |
| 109 | private: |
| 110 | // The buffer, as a writable array. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 111 | uint8_t* const writable_bytes_; |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 112 | |
| 113 | DISALLOW_COPY_AND_ASSIGN(BitBufferWriter); |
| 114 | }; |
| 115 | |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 116 | } // namespace rtc |
| 117 | |
| 118 | #endif // WEBRTC_BASE_BITBUFFER_H_ |