blob: 7dc7428fe9108630cfe951504cf182d882c7f70c [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#include "rtc_base/bit_buffer.h"
Noah Richardsbbf7c862015-04-21 16:30:13 -070012
Noah Richards86153c22015-04-28 15:13:44 -070013#include <algorithm>
Noah Richardsbbf7c862015-04-21 16:30:13 -070014#include <limits>
15
Danil Chapovalov09fb7872021-08-20 12:46:14 +020016#include "absl/numeric/bits.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
Noah Richardsbbf7c862015-04-21 16:30:13 -070018
19namespace {
20
Artem Titov96e3b992021-07-26 16:03:14 +020021// Returns the highest byte of `val` in a uint8_t.
Noah Richards9b9f1c42015-05-12 12:20:47 -070022uint8_t HighestByte(uint64_t val) {
23 return static_cast<uint8_t>(val >> 56);
Noah Richards86153c22015-04-28 15:13:44 -070024}
25
Artem Titov96e3b992021-07-26 16:03:14 +020026// Returns the result of writing partial data from `source`, of
27// `source_bit_count` size in the highest bits, to `target` at
28// `target_bit_offset` from the highest bit.
Noah Richards9b9f1c42015-05-12 12:20:47 -070029uint8_t WritePartialByte(uint8_t source,
30 size_t source_bit_count,
31 uint8_t target,
32 size_t target_bit_offset) {
henrikg91d6ede2015-09-17 00:24:34 -070033 RTC_DCHECK(target_bit_offset < 8);
34 RTC_DCHECK(source_bit_count < 9);
35 RTC_DCHECK(source_bit_count <= (8 - target_bit_offset));
Noah Richards86153c22015-04-28 15:13:44 -070036 // Generate a mask for just the bits we're going to overwrite, so:
Noah Richards9b9f1c42015-05-12 12:20:47 -070037 uint8_t mask =
Noah Richards86153c22015-04-28 15:13:44 -070038 // The number of bits we want, in the most significant bits...
Noah Richards9b9f1c42015-05-12 12:20:47 -070039 static_cast<uint8_t>(0xFF << (8 - source_bit_count))
Noah Richards86153c22015-04-28 15:13:44 -070040 // ...shifted over to the target offset from the most signficant bit.
41 >> target_bit_offset;
42
43 // We want the target, with the bits we'll overwrite masked off, or'ed with
44 // the bits from the source we want.
45 return (target & ~mask) | (source >> target_bit_offset);
46}
47
Noah Richardsbbf7c862015-04-21 16:30:13 -070048} // namespace
49
50namespace rtc {
51
Danil Chapovalov48f95252021-09-16 13:20:48 +020052BitBufferWriter::BitBufferWriter(uint8_t* bytes, size_t byte_count)
53 : writable_bytes_(bytes),
54 byte_count_(byte_count),
55 byte_offset_(),
56 bit_offset_() {
henrikg91d6ede2015-09-17 00:24:34 -070057 RTC_DCHECK(static_cast<uint64_t>(byte_count_) <=
58 std::numeric_limits<uint32_t>::max());
Noah Richardsbbf7c862015-04-21 16:30:13 -070059}
60
Danil Chapovalov48f95252021-09-16 13:20:48 +020061uint64_t BitBufferWriter::RemainingBitCount() const {
Noah Richards9b9f1c42015-05-12 12:20:47 -070062 return (static_cast<uint64_t>(byte_count_) - byte_offset_) * 8 - bit_offset_;
Noah Richardsbbf7c862015-04-21 16:30:13 -070063}
64
Danil Chapovalov48f95252021-09-16 13:20:48 +020065bool BitBufferWriter::ConsumeBytes(size_t byte_count) {
Noah Richardsbbf7c862015-04-21 16:30:13 -070066 return ConsumeBits(byte_count * 8);
67}
68
Danil Chapovalov48f95252021-09-16 13:20:48 +020069bool BitBufferWriter::ConsumeBits(size_t bit_count) {
Noah Richardsbbf7c862015-04-21 16:30:13 -070070 if (bit_count > RemainingBitCount()) {
71 return false;
72 }
73
74 byte_offset_ += (bit_offset_ + bit_count) / 8;
75 bit_offset_ = (bit_offset_ + bit_count) % 8;
76 return true;
77}
78
Danil Chapovalov48f95252021-09-16 13:20:48 +020079void BitBufferWriter::GetCurrentOffset(size_t* out_byte_offset,
80 size_t* out_bit_offset) {
deadbeef37f5ecf2017-02-27 14:06:41 -080081 RTC_CHECK(out_byte_offset != nullptr);
82 RTC_CHECK(out_bit_offset != nullptr);
Noah Richards86153c22015-04-28 15:13:44 -070083 *out_byte_offset = byte_offset_;
84 *out_bit_offset = bit_offset_;
85}
86
Danil Chapovalov48f95252021-09-16 13:20:48 +020087bool BitBufferWriter::Seek(size_t byte_offset, size_t bit_offset) {
Noah Richards86153c22015-04-28 15:13:44 -070088 if (byte_offset > byte_count_ || bit_offset > 7 ||
89 (byte_offset == byte_count_ && bit_offset > 0)) {
90 return false;
91 }
92 byte_offset_ = byte_offset;
93 bit_offset_ = bit_offset;
94 return true;
95}
96
Noah Richards9b9f1c42015-05-12 12:20:47 -070097bool BitBufferWriter::WriteUInt8(uint8_t val) {
98 return WriteBits(val, sizeof(uint8_t) * 8);
Noah Richards86153c22015-04-28 15:13:44 -070099}
100
Noah Richards9b9f1c42015-05-12 12:20:47 -0700101bool BitBufferWriter::WriteUInt16(uint16_t val) {
102 return WriteBits(val, sizeof(uint16_t) * 8);
Noah Richards86153c22015-04-28 15:13:44 -0700103}
104
Noah Richards9b9f1c42015-05-12 12:20:47 -0700105bool BitBufferWriter::WriteUInt32(uint32_t val) {
106 return WriteBits(val, sizeof(uint32_t) * 8);
Noah Richards86153c22015-04-28 15:13:44 -0700107}
108
Noah Richards9b9f1c42015-05-12 12:20:47 -0700109bool BitBufferWriter::WriteBits(uint64_t val, size_t bit_count) {
Noah Richards86153c22015-04-28 15:13:44 -0700110 if (bit_count > RemainingBitCount()) {
111 return false;
112 }
113 size_t total_bits = bit_count;
114
115 // For simplicity, push the bits we want to read from val to the highest bits.
Noah Richards9b9f1c42015-05-12 12:20:47 -0700116 val <<= (sizeof(uint64_t) * 8 - bit_count);
Noah Richards86153c22015-04-28 15:13:44 -0700117
Noah Richards9b9f1c42015-05-12 12:20:47 -0700118 uint8_t* bytes = writable_bytes_ + byte_offset_;
Noah Richards86153c22015-04-28 15:13:44 -0700119
120 // The first byte is relatively special; the bit offset to write to may put us
121 // in the middle of the byte, and the total bit count to write may require we
122 // save the bits at the end of the byte.
123 size_t remaining_bits_in_current_byte = 8 - bit_offset_;
124 size_t bits_in_first_byte =
125 std::min(bit_count, remaining_bits_in_current_byte);
Yves Gerey665174f2018-06-19 15:03:05 +0200126 *bytes = WritePartialByte(HighestByte(val), bits_in_first_byte, *bytes,
127 bit_offset_);
Noah Richards86153c22015-04-28 15:13:44 -0700128 if (bit_count <= remaining_bits_in_current_byte) {
129 // Nothing left to write, so quit early.
130 return ConsumeBits(total_bits);
131 }
132
133 // Subtract what we've written from the bit count, shift it off the value, and
134 // write the remaining full bytes.
135 val <<= bits_in_first_byte;
136 bytes++;
137 bit_count -= bits_in_first_byte;
138 while (bit_count >= 8) {
139 *bytes++ = HighestByte(val);
140 val <<= 8;
141 bit_count -= 8;
142 }
143
144 // Last byte may also be partial, so write the remaining bits from the top of
145 // val.
146 if (bit_count > 0) {
147 *bytes = WritePartialByte(HighestByte(val), bit_count, *bytes, 0);
148 }
149
150 // All done! Consume the bits we've written.
151 return ConsumeBits(total_bits);
152}
153
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200154bool BitBufferWriter::WriteNonSymmetric(uint32_t val, uint32_t num_values) {
155 RTC_DCHECK_LT(val, num_values);
156 RTC_DCHECK_LE(num_values, uint32_t{1} << 31);
Danil Chapovalova9e1b492020-07-08 11:24:19 +0200157 if (num_values == 1) {
158 // When there is only one possible value, it requires zero bits to store it.
159 // But WriteBits doesn't support writing zero bits.
160 return true;
161 }
Danil Chapovalov09fb7872021-08-20 12:46:14 +0200162 size_t count_bits = absl::bit_width(num_values);
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200163 uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values;
164
165 return val < num_min_bits_values
166 ? WriteBits(val, count_bits - 1)
167 : WriteBits(val + num_min_bits_values, count_bits);
168}
169
170size_t BitBufferWriter::SizeNonSymmetricBits(uint32_t val,
171 uint32_t num_values) {
172 RTC_DCHECK_LT(val, num_values);
173 RTC_DCHECK_LE(num_values, uint32_t{1} << 31);
Danil Chapovalov09fb7872021-08-20 12:46:14 +0200174 size_t count_bits = absl::bit_width(num_values);
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200175 uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values;
176
177 return val < num_min_bits_values ? (count_bits - 1) : count_bits;
178}
179
Noah Richards9b9f1c42015-05-12 12:20:47 -0700180bool BitBufferWriter::WriteExponentialGolomb(uint32_t val) {
181 // We don't support reading UINT32_MAX, because it doesn't fit in a uint32_t
Noah Richards86153c22015-04-28 15:13:44 -0700182 // when encoded, so don't support writing it either.
Noah Richards9b9f1c42015-05-12 12:20:47 -0700183 if (val == std::numeric_limits<uint32_t>::max()) {
Noah Richards86153c22015-04-28 15:13:44 -0700184 return false;
185 }
Noah Richards9b9f1c42015-05-12 12:20:47 -0700186 uint64_t val_to_encode = static_cast<uint64_t>(val) + 1;
Noah Richards86153c22015-04-28 15:13:44 -0700187
Danil Chapovalov09fb7872021-08-20 12:46:14 +0200188 // We need to write bit_width(val+1) 0s and then val+1. Since val (as a
Noah Richards9b9f1c42015-05-12 12:20:47 -0700189 // uint64_t) has leading zeros, we can just write the total golomb encoded
190 // size worth of bits, knowing the value will appear last.
Danil Chapovalov09fb7872021-08-20 12:46:14 +0200191 return WriteBits(val_to_encode, absl::bit_width(val_to_encode) * 2 - 1);
Noah Richards86153c22015-04-28 15:13:44 -0700192}
193
sprang52033d62016-06-02 02:43:32 -0700194bool BitBufferWriter::WriteSignedExponentialGolomb(int32_t val) {
195 if (val == 0) {
196 return WriteExponentialGolomb(0);
197 } else if (val > 0) {
198 uint32_t signed_val = val;
199 return WriteExponentialGolomb((signed_val * 2) - 1);
200 } else {
201 if (val == std::numeric_limits<int32_t>::min())
202 return false; // Not supported, would cause overflow.
203 uint32_t signed_val = -val;
204 return WriteExponentialGolomb(signed_val * 2);
205 }
206}
207
Noah Richardsbbf7c862015-04-21 16:30:13 -0700208} // namespace rtc