blob: 7202675a44c7348c79deb9b7ccebdc81000b19d4 [file] [log] [blame]
Noah Richardsbbf7c862015-04-21 16:30:13 -07001/*
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 Richards9b9f1c42015-05-12 12:20:47 -070014#include <stdint.h> // For integer types.
15#include <stddef.h> // For size_t.
16
17#include "webrtc/base/constructormagic.h"
Noah Richardsbbf7c862015-04-21 16:30:13 -070018
19namespace rtc {
20
21// A class, similar to ByteBuffer, that can parse bit-sized data out of a set of
Noah Richards86153c22015-04-28 15:13:44 -070022// 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 Richardsbbf7c862015-04-21 16:30:13 -070026// Sizes/counts specify bits/bytes, for clarity.
27// Byte order is assumed big-endian/network.
28class BitBuffer {
29 public:
Noah Richards9b9f1c42015-05-12 12:20:47 -070030 BitBuffer(const uint8_t* bytes, size_t byte_count);
Noah Richardsbbf7c862015-04-21 16:30:13 -070031
Noah Richards86153c22015-04-28 15:13:44 -070032 // 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 Richardsbbf7c862015-04-21 16:30:13 -070036 // The remaining bits in the byte buffer.
Noah Richards9b9f1c42015-05-12 12:20:47 -070037 uint64_t RemainingBitCount() const;
Noah Richardsbbf7c862015-04-21 16:30:13 -070038
39 // Reads byte-sized values from the buffer. Returns false if there isn't
40 // enough data left for the specified type.
Noah Richards9b9f1c42015-05-12 12:20:47 -070041 bool ReadUInt8(uint8_t* val);
42 bool ReadUInt16(uint16_t* val);
43 bool ReadUInt32(uint32_t* val);
Noah Richardsbbf7c862015-04-21 16:30:13 -070044
45 // Reads bit-sized values from the buffer. Returns false if there isn't enough
Noah Richards86153c22015-04-28 15:13:44 -070046 // data left for the specified bit count..
Noah Richards9b9f1c42015-05-12 12:20:47 -070047 bool ReadBits(uint32_t* val, size_t bit_count);
Noah Richardsbbf7c862015-04-21 16:30:13 -070048
49 // Peeks bit-sized values from the buffer. Returns false if there isn't enough
Noah Richards86153c22015-04-28 15:13:44 -070050 // data left for the specified number of bits. Doesn't move the current
51 // offset.
Noah Richards9b9f1c42015-05-12 12:20:47 -070052 bool PeekBits(uint32_t* val, size_t bit_count);
Noah Richardsbbf7c862015-04-21 16:30:13 -070053
Noah Richards86153c22015-04-28 15:13:44 -070054 // Reads the exponential golomb encoded value at the current offset.
Noah Richardsbbf7c862015-04-21 16:30:13 -070055 // 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 Richards9b9f1c42015-05-12 12:20:47 -070061 // the value wouldn't fit in a uint32_t.
62 bool ReadExponentialGolomb(uint32_t* val);
Noah Richardsbbf7c862015-04-21 16:30:13 -070063
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 Richards86153c22015-04-28 15:13:44 -070071 // 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 Richards9b9f1c42015-05-12 12:20:47 -070076 const uint8_t* const bytes_;
Noah Richardsbbf7c862015-04-21 16:30:13 -070077 // 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
henrikg3c089d72015-09-16 05:37:44 -070084 RTC_DISALLOW_COPY_AND_ASSIGN(BitBuffer);
Noah Richardsbbf7c862015-04-21 16:30:13 -070085};
86
Noah Richards86153c22015-04-28 15:13:44 -070087// 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.
90class BitBufferWriter : public BitBuffer {
91 public:
92 // Constructs a bit buffer for the writable buffer of |bytes|.
Noah Richards9b9f1c42015-05-12 12:20:47 -070093 BitBufferWriter(uint8_t* bytes, size_t byte_count);
Noah Richards86153c22015-04-28 15:13:44 -070094
95 // Writes byte-sized values from the buffer. Returns false if there isn't
96 // enough data left for the specified type.
Noah Richards9b9f1c42015-05-12 12:20:47 -070097 bool WriteUInt8(uint8_t val);
98 bool WriteUInt16(uint16_t val);
99 bool WriteUInt32(uint32_t val);
Noah Richards86153c22015-04-28 15:13:44 -0700100
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 Richards9b9f1c42015-05-12 12:20:47 -0700103 bool WriteBits(uint64_t val, size_t bit_count);
Noah Richards86153c22015-04-28 15:13:44 -0700104
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 Richards9b9f1c42015-05-12 12:20:47 -0700107 bool WriteExponentialGolomb(uint32_t val);
Noah Richards86153c22015-04-28 15:13:44 -0700108
109 private:
110 // The buffer, as a writable array.
Noah Richards9b9f1c42015-05-12 12:20:47 -0700111 uint8_t* const writable_bytes_;
Noah Richards86153c22015-04-28 15:13:44 -0700112
henrikg3c089d72015-09-16 05:37:44 -0700113 RTC_DISALLOW_COPY_AND_ASSIGN(BitBufferWriter);
Noah Richards86153c22015-04-28 15:13:44 -0700114};
115
Noah Richardsbbf7c862015-04-21 16:30:13 -0700116} // namespace rtc
117
118#endif // WEBRTC_BASE_BITBUFFER_H_