blob: 388218e69824d30283ac2460c04d4a23363fd546 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_BIT_BUFFER_H_
12#define RTC_BASE_BIT_BUFFER_H_
Noah Richardsbbf7c862015-04-21 16:30:13 -070013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <stddef.h> // For size_t.
Yves Gerey665174f2018-06-19 15:03:05 +020015#include <stdint.h> // For integer types.
Noah Richards9b9f1c42015-05-12 12:20:47 -070016
Björn Tereliusa77e16c2021-05-17 17:20:53 +020017#include "absl/base/attributes.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/constructor_magic.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019
20namespace rtc {
21
22// A class, similar to ByteBuffer, that can parse bit-sized data out of a set of
23// bytes. Has a similar API to ByteBuffer, plus methods for reading bit-sized
24// and exponential golomb encoded data. For a writable version, use
25// BitBufferWriter. Unlike ByteBuffer, this class doesn't make a copy of the
26// source bytes, so it can be used on read-only data.
27// Sizes/counts specify bits/bytes, for clarity.
28// Byte order is assumed big-endian/network.
29class BitBuffer {
30 public:
31 BitBuffer(const uint8_t* bytes, size_t byte_count);
32
33 // Gets the current offset, in bytes/bits, from the start of the buffer. The
34 // bit offset is the offset into the current byte, in the range [0,7].
35 void GetCurrentOffset(size_t* out_byte_offset, size_t* out_bit_offset);
36
37 // The remaining bits in the byte buffer.
38 uint64_t RemainingBitCount() const;
39
40 // Reads byte-sized values from the buffer. Returns false if there isn't
41 // enough data left for the specified type.
Björn Tereliusa77e16c2021-05-17 17:20:53 +020042 bool ReadUInt8(uint8_t& val);
43 bool ReadUInt16(uint16_t& val);
44 bool ReadUInt32(uint32_t& val);
45 ABSL_DEPRECATED("") bool ReadUInt8(uint8_t* val) {
46 return val ? ReadUInt8(*val) : false;
47 }
48 ABSL_DEPRECATED("") bool ReadUInt16(uint16_t* val) {
49 return val ? ReadUInt16(*val) : false;
50 }
51 ABSL_DEPRECATED("") bool ReadUInt32(uint32_t* val) {
52 return val ? ReadUInt32(*val) : false;
53 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054
55 // Reads bit-sized values from the buffer. Returns false if there isn't enough
Elad Alon21c66382018-09-12 15:59:30 +020056 // data left for the specified bit count.
Björn Tereliusa77e16c2021-05-17 17:20:53 +020057 bool ReadBits(size_t bit_count, uint32_t& val);
58 bool ReadBits(size_t bit_count, uint64_t& val);
59 ABSL_DEPRECATED("") bool ReadBits(uint32_t* val, size_t bit_count) {
60 return val ? ReadBits(bit_count, *val) : false;
61 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062
63 // Peeks bit-sized values from the buffer. Returns false if there isn't enough
64 // data left for the specified number of bits. Doesn't move the current
65 // offset.
Björn Tereliusa77e16c2021-05-17 17:20:53 +020066 bool PeekBits(size_t bit_count, uint32_t& val);
67 bool PeekBits(size_t bit_count, uint64_t& val);
68 ABSL_DEPRECATED("") bool PeekBits(uint32_t* val, size_t bit_count) {
69 return val ? PeekBits(bit_count, *val) : false;
70 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020071
Danil Chapovalova2b30d82019-07-03 14:41:50 +020072 // Reads value in range [0, num_values - 1].
73 // This encoding is similar to ReadBits(val, Ceil(Log2(num_values)),
74 // but reduces wastage incurred when encoding non-power of two value ranges
75 // Non symmetric values are encoded as:
76 // 1) n = countbits(num_values)
77 // 2) k = (1 << n) - num_values
78 // Value v in range [0, k - 1] is encoded in (n-1) bits.
79 // Value v in range [k, num_values - 1] is encoded as (v+k) in n bits.
80 // https://aomediacodec.github.io/av1-spec/#nsn
81 // Returns false if there isn't enough data left.
Björn Tereliusa77e16c2021-05-17 17:20:53 +020082 bool ReadNonSymmetric(uint32_t num_values, uint32_t& val);
83 ABSL_DEPRECATED("")
84 bool ReadNonSymmetric(uint32_t* val, uint32_t num_values) {
85 return val ? ReadNonSymmetric(num_values, *val) : false;
86 }
Danil Chapovalova2b30d82019-07-03 14:41:50 +020087
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020088 // Reads the exponential golomb encoded value at the current offset.
89 // Exponential golomb values are encoded as:
90 // 1) x = source val + 1
91 // 2) In binary, write [countbits(x) - 1] 0s, then x
92 // To decode, we count the number of leading 0 bits, read that many + 1 bits,
93 // and increment the result by 1.
94 // Returns false if there isn't enough data left for the specified type, or if
95 // the value wouldn't fit in a uint32_t.
Björn Tereliusa77e16c2021-05-17 17:20:53 +020096 bool ReadExponentialGolomb(uint32_t& val);
97 ABSL_DEPRECATED("") bool ReadExponentialGolomb(uint32_t* val) {
98 return val ? ReadExponentialGolomb(*val) : false;
99 }
100
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101 // Reads signed exponential golomb values at the current offset. Signed
102 // exponential golomb values are just the unsigned values mapped to the
103 // sequence 0, 1, -1, 2, -2, etc. in order.
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200104 bool ReadSignedExponentialGolomb(int32_t& val);
105 ABSL_DEPRECATED("") bool ReadSignedExponentialGolomb(int32_t* val) {
106 return val ? ReadSignedExponentialGolomb(*val) : false;
107 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108
109 // Moves current position |byte_count| bytes forward. Returns false if
110 // there aren't enough bytes left in the buffer.
111 bool ConsumeBytes(size_t byte_count);
112 // Moves current position |bit_count| bits forward. Returns false if
113 // there aren't enough bits left in the buffer.
114 bool ConsumeBits(size_t bit_count);
115
116 // Sets the current offset to the provied byte/bit offsets. The bit
117 // offset is from the given byte, in the range [0,7].
118 bool Seek(size_t byte_offset, size_t bit_offset);
119
120 protected:
121 const uint8_t* const bytes_;
122 // The total size of |bytes_|.
123 size_t byte_count_;
124 // The current offset, in bytes, from the start of |bytes_|.
125 size_t byte_offset_;
126 // The current offset, in bits, into the current byte.
127 size_t bit_offset_;
128
129 RTC_DISALLOW_COPY_AND_ASSIGN(BitBuffer);
130};
131
132// A BitBuffer API for write operations. Supports symmetric write APIs to the
133// reading APIs of BitBuffer. Note that the read/write offset is shared with the
134// BitBuffer API, so both reading and writing will consume bytes/bits.
135class BitBufferWriter : public BitBuffer {
136 public:
137 // Constructs a bit buffer for the writable buffer of |bytes|.
138 BitBufferWriter(uint8_t* bytes, size_t byte_count);
139
140 // Writes byte-sized values from the buffer. Returns false if there isn't
141 // enough data left for the specified type.
142 bool WriteUInt8(uint8_t val);
143 bool WriteUInt16(uint16_t val);
144 bool WriteUInt32(uint32_t val);
145
146 // Writes bit-sized values to the buffer. Returns false if there isn't enough
147 // room left for the specified number of bits.
148 bool WriteBits(uint64_t val, size_t bit_count);
149
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200150 // Writes value in range [0, num_values - 1]
151 // See ReadNonSymmetric documentation for the format,
152 // Call SizeNonSymmetricBits to get number of bits needed to store the value.
153 // Returns false if there isn't enough room left for the value.
154 bool WriteNonSymmetric(uint32_t val, uint32_t num_values);
155 // Returns number of bits required to store |val| with NonSymmetric encoding.
156 static size_t SizeNonSymmetricBits(uint32_t val, uint32_t num_values);
157
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200158 // Writes the exponential golomb encoded version of the supplied value.
159 // Returns false if there isn't enough room left for the value.
160 bool WriteExponentialGolomb(uint32_t val);
161 // Writes the signed exponential golomb version of the supplied value.
162 // Signed exponential golomb values are just the unsigned values mapped to the
163 // sequence 0, 1, -1, 2, -2, etc. in order.
164 bool WriteSignedExponentialGolomb(int32_t val);
165
166 private:
167 // The buffer, as a writable array.
168 uint8_t* const writable_bytes_;
169
170 RTC_DISALLOW_COPY_AND_ASSIGN(BitBufferWriter);
171};
172
173} // namespace rtc
Noah Richardsbbf7c862015-04-21 16:30:13 -0700174
Steve Anton10542f22019-01-11 09:11:00 -0800175#endif // RTC_BASE_BIT_BUFFER_H_