blob: b2baaa90958acb6414d356557789ffd8d0ffc022 [file] [log] [blame]
Noah Richards5ac3e5d2015-04-21 23:30:131/*
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 Richards1dd804e2015-05-12 19:20:4714#include <stdint.h> // For integer types.
15#include <stddef.h> // For size_t.
16
17#include "webrtc/base/constructormagic.h"
Noah Richards5ac3e5d2015-04-21 23:30:1318
19namespace rtc {
20
21// A class, similar to ByteBuffer, that can parse bit-sized data out of a set of
Noah Richards115e06c2015-04-28 22:13:4422// 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 Richards5ac3e5d2015-04-21 23:30:1326// Sizes/counts specify bits/bytes, for clarity.
27// Byte order is assumed big-endian/network.
28class BitBuffer {
29 public:
Noah Richards1dd804e2015-05-12 19:20:4730 BitBuffer(const uint8_t* bytes, size_t byte_count);
Noah Richards5ac3e5d2015-04-21 23:30:1331
Noah Richards115e06c2015-04-28 22:13:4432 // 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 Richards5ac3e5d2015-04-21 23:30:1336 // The remaining bits in the byte buffer.
Noah Richards1dd804e2015-05-12 19:20:4737 uint64_t RemainingBitCount() const;
Noah Richards5ac3e5d2015-04-21 23:30:1338
39 // Reads byte-sized values from the buffer. Returns false if there isn't
40 // enough data left for the specified type.
Noah Richards1dd804e2015-05-12 19:20:4741 bool ReadUInt8(uint8_t* val);
42 bool ReadUInt16(uint16_t* val);
43 bool ReadUInt32(uint32_t* val);
Noah Richards5ac3e5d2015-04-21 23:30:1344
45 // Reads bit-sized values from the buffer. Returns false if there isn't enough
Noah Richards115e06c2015-04-28 22:13:4446 // data left for the specified bit count..
Noah Richards1dd804e2015-05-12 19:20:4747 bool ReadBits(uint32_t* val, size_t bit_count);
Noah Richards5ac3e5d2015-04-21 23:30:1348
49 // Peeks bit-sized values from the buffer. Returns false if there isn't enough
Noah Richards115e06c2015-04-28 22:13:4450 // data left for the specified number of bits. Doesn't move the current
51 // offset.
Noah Richards1dd804e2015-05-12 19:20:4752 bool PeekBits(uint32_t* val, size_t bit_count);
Noah Richards5ac3e5d2015-04-21 23:30:1353
Noah Richards115e06c2015-04-28 22:13:4454 // Reads the exponential golomb encoded value at the current offset.
Noah Richards5ac3e5d2015-04-21 23:30:1355 // 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 Richards1dd804e2015-05-12 19:20:4761 // the value wouldn't fit in a uint32_t.
62 bool ReadExponentialGolomb(uint32_t* val);
Peter Boström4920b2c2015-09-24 13:06:5063 // Reads signed exponential golomb values at the current offset. Signed
64 // exponential golomb values are just the unsigned values mapped to the
65 // sequence 0, 1, -1, 2, -2, etc. in order.
66 bool ReadSignedExponentialGolomb(int32_t* val);
Noah Richards5ac3e5d2015-04-21 23:30:1367
68 // Moves current position |byte_count| bytes forward. Returns false if
69 // there aren't enough bytes left in the buffer.
70 bool ConsumeBytes(size_t byte_count);
71 // Moves current position |bit_count| bits forward. Returns false if
72 // there aren't enough bits left in the buffer.
73 bool ConsumeBits(size_t bit_count);
74
Noah Richards115e06c2015-04-28 22:13:4475 // Sets the current offset to the provied byte/bit offsets. The bit
76 // offset is from the given byte, in the range [0,7].
77 bool Seek(size_t byte_offset, size_t bit_offset);
78
79 protected:
Noah Richards1dd804e2015-05-12 19:20:4780 const uint8_t* const bytes_;
Noah Richards5ac3e5d2015-04-21 23:30:1381 // The total size of |bytes_|.
82 size_t byte_count_;
83 // The current offset, in bytes, from the start of |bytes_|.
84 size_t byte_offset_;
85 // The current offset, in bits, into the current byte.
86 size_t bit_offset_;
87
henrikg9199c0e2015-09-16 12:37:4488 RTC_DISALLOW_COPY_AND_ASSIGN(BitBuffer);
Noah Richards5ac3e5d2015-04-21 23:30:1389};
90
Noah Richards115e06c2015-04-28 22:13:4491// A BitBuffer API for write operations. Supports symmetric write APIs to the
92// reading APIs of BitBuffer. Note that the read/write offset is shared with the
93// BitBuffer API, so both reading and writing will consume bytes/bits.
94class BitBufferWriter : public BitBuffer {
95 public:
96 // Constructs a bit buffer for the writable buffer of |bytes|.
Noah Richards1dd804e2015-05-12 19:20:4797 BitBufferWriter(uint8_t* bytes, size_t byte_count);
Noah Richards115e06c2015-04-28 22:13:4498
99 // Writes byte-sized values from the buffer. Returns false if there isn't
100 // enough data left for the specified type.
Noah Richards1dd804e2015-05-12 19:20:47101 bool WriteUInt8(uint8_t val);
102 bool WriteUInt16(uint16_t val);
103 bool WriteUInt32(uint32_t val);
Noah Richards115e06c2015-04-28 22:13:44104
105 // Writes bit-sized values to the buffer. Returns false if there isn't enough
106 // room left for the specified number of bits.
Noah Richards1dd804e2015-05-12 19:20:47107 bool WriteBits(uint64_t val, size_t bit_count);
Noah Richards115e06c2015-04-28 22:13:44108
109 // Writes the exponential golomb encoded version of the supplied value.
110 // Returns false if there isn't enough room left for the value.
Noah Richards1dd804e2015-05-12 19:20:47111 bool WriteExponentialGolomb(uint32_t val);
sprangcc31c7d2016-06-02 09:43:32112 // Writes the signed exponential golomb version of the supplied value.
113 // Signed exponential golomb values are just the unsigned values mapped to the
114 // sequence 0, 1, -1, 2, -2, etc. in order.
115 bool WriteSignedExponentialGolomb(int32_t val);
Noah Richards115e06c2015-04-28 22:13:44116
117 private:
118 // The buffer, as a writable array.
Noah Richards1dd804e2015-05-12 19:20:47119 uint8_t* const writable_bytes_;
Noah Richards115e06c2015-04-28 22:13:44120
henrikg9199c0e2015-09-16 12:37:44121 RTC_DISALLOW_COPY_AND_ASSIGN(BitBufferWriter);
Noah Richards115e06c2015-04-28 22:13:44122};
123
Noah Richards5ac3e5d2015-04-21 23:30:13124} // namespace rtc
125
126#endif // WEBRTC_BASE_BITBUFFER_H_