blob: 5aa1f132344ee9c821b82f187ba57a653271a3b8 [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
14#include "webrtc/base/common.h"
15
16namespace rtc {
17
18// A class, similar to ByteBuffer, that can parse bit-sized data out of a set of
Noah Richards86153c22015-04-28 15:13:44 -070019// bytes. Has a similar API to ByteBuffer, plus methods for reading bit-sized
20// and exponential golomb encoded data. For a writable version, use
21// BitBufferWriter. Unlike ByteBuffer, this class doesn't make a copy of the
22// source bytes, so it can be used on read-only data.
Noah Richardsbbf7c862015-04-21 16:30:13 -070023// Sizes/counts specify bits/bytes, for clarity.
24// Byte order is assumed big-endian/network.
25class BitBuffer {
26 public:
27 BitBuffer(const uint8* bytes, size_t byte_count);
28
Noah Richards86153c22015-04-28 15:13:44 -070029 // Gets the current offset, in bytes/bits, from the start of the buffer. The
30 // bit offset is the offset into the current byte, in the range [0,7].
31 void GetCurrentOffset(size_t* out_byte_offset, size_t* out_bit_offset);
32
Noah Richardsbbf7c862015-04-21 16:30:13 -070033 // The remaining bits in the byte buffer.
34 uint64 RemainingBitCount() const;
35
36 // Reads byte-sized values from the buffer. Returns false if there isn't
37 // enough data left for the specified type.
38 bool ReadUInt8(uint8* val);
39 bool ReadUInt16(uint16* val);
40 bool ReadUInt32(uint32* val);
41
42 // Reads bit-sized values from the buffer. Returns false if there isn't enough
Noah Richards86153c22015-04-28 15:13:44 -070043 // data left for the specified bit count..
Noah Richardsbbf7c862015-04-21 16:30:13 -070044 bool ReadBits(uint32* val, size_t bit_count);
45
46 // Peeks bit-sized values from the buffer. Returns false if there isn't enough
Noah Richards86153c22015-04-28 15:13:44 -070047 // data left for the specified number of bits. Doesn't move the current
48 // offset.
Noah Richardsbbf7c862015-04-21 16:30:13 -070049 bool PeekBits(uint32* val, size_t bit_count);
50
Noah Richards86153c22015-04-28 15:13:44 -070051 // Reads the exponential golomb encoded value at the current offset.
Noah Richardsbbf7c862015-04-21 16:30:13 -070052 // Exponential golomb values are encoded as:
53 // 1) x = source val + 1
54 // 2) In binary, write [countbits(x) - 1] 0s, then x
55 // To decode, we count the number of leading 0 bits, read that many + 1 bits,
56 // and increment the result by 1.
57 // Returns false if there isn't enough data left for the specified type, or if
58 // the value wouldn't fit in a uint32.
59 bool ReadExponentialGolomb(uint32* val);
60
61 // Moves current position |byte_count| bytes forward. Returns false if
62 // there aren't enough bytes left in the buffer.
63 bool ConsumeBytes(size_t byte_count);
64 // Moves current position |bit_count| bits forward. Returns false if
65 // there aren't enough bits left in the buffer.
66 bool ConsumeBits(size_t bit_count);
67
Noah Richards86153c22015-04-28 15:13:44 -070068 // Sets the current offset to the provied byte/bit offsets. The bit
69 // offset is from the given byte, in the range [0,7].
70 bool Seek(size_t byte_offset, size_t bit_offset);
71
72 protected:
Noah Richardsbbf7c862015-04-21 16:30:13 -070073 const uint8* const bytes_;
74 // The total size of |bytes_|.
75 size_t byte_count_;
76 // The current offset, in bytes, from the start of |bytes_|.
77 size_t byte_offset_;
78 // The current offset, in bits, into the current byte.
79 size_t bit_offset_;
80
81 DISALLOW_COPY_AND_ASSIGN(BitBuffer);
82};
83
Noah Richards86153c22015-04-28 15:13:44 -070084// A BitBuffer API for write operations. Supports symmetric write APIs to the
85// reading APIs of BitBuffer. Note that the read/write offset is shared with the
86// BitBuffer API, so both reading and writing will consume bytes/bits.
87class BitBufferWriter : public BitBuffer {
88 public:
89 // Constructs a bit buffer for the writable buffer of |bytes|.
90 BitBufferWriter(uint8* bytes, size_t byte_count);
91
92 // Writes byte-sized values from the buffer. Returns false if there isn't
93 // enough data left for the specified type.
94 bool WriteUInt8(uint8 val);
95 bool WriteUInt16(uint16 val);
96 bool WriteUInt32(uint32 val);
97
98 // Writes bit-sized values to the buffer. Returns false if there isn't enough
99 // room left for the specified number of bits.
100 bool WriteBits(uint64 val, size_t bit_count);
101
102 // Writes the exponential golomb encoded version of the supplied value.
103 // Returns false if there isn't enough room left for the value.
104 bool WriteExponentialGolomb(uint32 val);
105
106 private:
107 // The buffer, as a writable array.
108 uint8* const writable_bytes_;
109
110 DISALLOW_COPY_AND_ASSIGN(BitBufferWriter);
111};
112
Noah Richardsbbf7c862015-04-21 16:30:13 -0700113} // namespace rtc
114
115#endif // WEBRTC_BASE_BITBUFFER_H_