blob: 045d3692491b2c8f55d9a43ecb257fd7a266831a [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
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/constructor_magic.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018
19namespace rtc {
20
21// A class, similar to ByteBuffer, that can parse bit-sized data out of a set of
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.
26// Sizes/counts specify bits/bytes, for clarity.
27// Byte order is assumed big-endian/network.
28class BitBuffer {
29 public:
30 BitBuffer(const uint8_t* bytes, size_t byte_count);
31
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
36 // The remaining bits in the byte buffer.
37 uint64_t RemainingBitCount() const;
38
39 // Reads byte-sized values from the buffer. Returns false if there isn't
40 // enough data left for the specified type.
41 bool ReadUInt8(uint8_t* val);
42 bool ReadUInt16(uint16_t* val);
43 bool ReadUInt32(uint32_t* val);
44
45 // Reads bit-sized values from the buffer. Returns false if there isn't enough
Elad Alon21c66382018-09-12 15:59:30 +020046 // data left for the specified bit count.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020047 bool ReadBits(uint32_t* val, size_t bit_count);
Björn Terelius39284362021-05-11 15:30:12 +020048 bool ReadBits(uint64_t* val, size_t bit_count);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049
50 // Peeks bit-sized values from the buffer. Returns false if there isn't enough
51 // data left for the specified number of bits. Doesn't move the current
52 // offset.
53 bool PeekBits(uint32_t* val, size_t bit_count);
Björn Terelius39284362021-05-11 15:30:12 +020054 bool PeekBits(uint64_t* val, size_t bit_count);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055
Danil Chapovalova2b30d82019-07-03 14:41:50 +020056 // Reads value in range [0, num_values - 1].
57 // This encoding is similar to ReadBits(val, Ceil(Log2(num_values)),
58 // but reduces wastage incurred when encoding non-power of two value ranges
59 // Non symmetric values are encoded as:
60 // 1) n = countbits(num_values)
61 // 2) k = (1 << n) - num_values
62 // Value v in range [0, k - 1] is encoded in (n-1) bits.
63 // Value v in range [k, num_values - 1] is encoded as (v+k) in n bits.
64 // https://aomediacodec.github.io/av1-spec/#nsn
65 // Returns false if there isn't enough data left.
66 bool ReadNonSymmetric(uint32_t* val, uint32_t num_values);
67
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068 // Reads the exponential golomb encoded value at the current offset.
69 // Exponential golomb values are encoded as:
70 // 1) x = source val + 1
71 // 2) In binary, write [countbits(x) - 1] 0s, then x
72 // To decode, we count the number of leading 0 bits, read that many + 1 bits,
73 // and increment the result by 1.
74 // Returns false if there isn't enough data left for the specified type, or if
75 // the value wouldn't fit in a uint32_t.
76 bool ReadExponentialGolomb(uint32_t* val);
77 // Reads signed exponential golomb values at the current offset. Signed
78 // exponential golomb values are just the unsigned values mapped to the
79 // sequence 0, 1, -1, 2, -2, etc. in order.
80 bool ReadSignedExponentialGolomb(int32_t* val);
81
82 // Moves current position |byte_count| bytes forward. Returns false if
83 // there aren't enough bytes left in the buffer.
84 bool ConsumeBytes(size_t byte_count);
85 // Moves current position |bit_count| bits forward. Returns false if
86 // there aren't enough bits left in the buffer.
87 bool ConsumeBits(size_t bit_count);
88
89 // Sets the current offset to the provied byte/bit offsets. The bit
90 // offset is from the given byte, in the range [0,7].
91 bool Seek(size_t byte_offset, size_t bit_offset);
92
93 protected:
94 const uint8_t* const bytes_;
95 // The total size of |bytes_|.
96 size_t byte_count_;
97 // The current offset, in bytes, from the start of |bytes_|.
98 size_t byte_offset_;
99 // The current offset, in bits, into the current byte.
100 size_t bit_offset_;
101
102 RTC_DISALLOW_COPY_AND_ASSIGN(BitBuffer);
103};
104
105// A BitBuffer API for write operations. Supports symmetric write APIs to the
106// reading APIs of BitBuffer. Note that the read/write offset is shared with the
107// BitBuffer API, so both reading and writing will consume bytes/bits.
108class BitBufferWriter : public BitBuffer {
109 public:
110 // Constructs a bit buffer for the writable buffer of |bytes|.
111 BitBufferWriter(uint8_t* bytes, size_t byte_count);
112
113 // Writes byte-sized values from the buffer. Returns false if there isn't
114 // enough data left for the specified type.
115 bool WriteUInt8(uint8_t val);
116 bool WriteUInt16(uint16_t val);
117 bool WriteUInt32(uint32_t val);
118
119 // Writes bit-sized values to the buffer. Returns false if there isn't enough
120 // room left for the specified number of bits.
121 bool WriteBits(uint64_t val, size_t bit_count);
122
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200123 // Writes value in range [0, num_values - 1]
124 // See ReadNonSymmetric documentation for the format,
125 // Call SizeNonSymmetricBits to get number of bits needed to store the value.
126 // Returns false if there isn't enough room left for the value.
127 bool WriteNonSymmetric(uint32_t val, uint32_t num_values);
128 // Returns number of bits required to store |val| with NonSymmetric encoding.
129 static size_t SizeNonSymmetricBits(uint32_t val, uint32_t num_values);
130
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200131 // Writes the exponential golomb encoded version of the supplied value.
132 // Returns false if there isn't enough room left for the value.
133 bool WriteExponentialGolomb(uint32_t val);
134 // Writes the signed exponential golomb version of the supplied value.
135 // Signed exponential golomb values are just the unsigned values mapped to the
136 // sequence 0, 1, -1, 2, -2, etc. in order.
137 bool WriteSignedExponentialGolomb(int32_t val);
138
139 private:
140 // The buffer, as a writable array.
141 uint8_t* const writable_bytes_;
142
143 RTC_DISALLOW_COPY_AND_ASSIGN(BitBufferWriter);
144};
145
146} // namespace rtc
Noah Richardsbbf7c862015-04-21 16:30:13 -0700147
Steve Anton10542f22019-01-11 09:11:00 -0800148#endif // RTC_BASE_BIT_BUFFER_H_