blob: a73ef5d869b70c4f1e72e8ad2afa741d3f0a01dc [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 lowest (right-most) `bit_count` bits in `byte`.
Noah Richards9b9f1c42015-05-12 12:20:47 -070022uint8_t LowestBits(uint8_t byte, size_t bit_count) {
kwibergaf476c72016-11-28 15:21:39 -080023 RTC_DCHECK_LE(bit_count, 8);
Noah Richards86153c22015-04-28 15:13:44 -070024 return byte & ((1 << bit_count) - 1);
Noah Richardsbbf7c862015-04-21 16:30:13 -070025}
26
Artem Titov96e3b992021-07-26 16:03:14 +020027// Returns the highest (left-most) `bit_count` bits in `byte`, shifted to the
Noah Richardsbbf7c862015-04-21 16:30:13 -070028// lowest bits (to the right).
Noah Richards9b9f1c42015-05-12 12:20:47 -070029uint8_t HighestBits(uint8_t byte, size_t bit_count) {
kwibergaf476c72016-11-28 15:21:39 -080030 RTC_DCHECK_LE(bit_count, 8);
Noah Richards9b9f1c42015-05-12 12:20:47 -070031 uint8_t shift = 8 - static_cast<uint8_t>(bit_count);
32 uint8_t mask = 0xFF << shift;
Noah Richardsbbf7c862015-04-21 16:30:13 -070033 return (byte & mask) >> shift;
34}
35
Artem Titov96e3b992021-07-26 16:03:14 +020036// Returns the highest byte of `val` in a uint8_t.
Noah Richards9b9f1c42015-05-12 12:20:47 -070037uint8_t HighestByte(uint64_t val) {
38 return static_cast<uint8_t>(val >> 56);
Noah Richards86153c22015-04-28 15:13:44 -070039}
40
Artem Titov96e3b992021-07-26 16:03:14 +020041// Returns the result of writing partial data from `source`, of
42// `source_bit_count` size in the highest bits, to `target` at
43// `target_bit_offset` from the highest bit.
Noah Richards9b9f1c42015-05-12 12:20:47 -070044uint8_t WritePartialByte(uint8_t source,
45 size_t source_bit_count,
46 uint8_t target,
47 size_t target_bit_offset) {
henrikg91d6ede2015-09-17 00:24:34 -070048 RTC_DCHECK(target_bit_offset < 8);
49 RTC_DCHECK(source_bit_count < 9);
50 RTC_DCHECK(source_bit_count <= (8 - target_bit_offset));
Noah Richards86153c22015-04-28 15:13:44 -070051 // Generate a mask for just the bits we're going to overwrite, so:
Noah Richards9b9f1c42015-05-12 12:20:47 -070052 uint8_t mask =
Noah Richards86153c22015-04-28 15:13:44 -070053 // The number of bits we want, in the most significant bits...
Noah Richards9b9f1c42015-05-12 12:20:47 -070054 static_cast<uint8_t>(0xFF << (8 - source_bit_count))
Noah Richards86153c22015-04-28 15:13:44 -070055 // ...shifted over to the target offset from the most signficant bit.
56 >> target_bit_offset;
57
58 // We want the target, with the bits we'll overwrite masked off, or'ed with
59 // the bits from the source we want.
60 return (target & ~mask) | (source >> target_bit_offset);
61}
62
Noah Richardsbbf7c862015-04-21 16:30:13 -070063} // namespace
64
65namespace rtc {
66
Noah Richards9b9f1c42015-05-12 12:20:47 -070067BitBuffer::BitBuffer(const uint8_t* bytes, size_t byte_count)
Noah Richardsbbf7c862015-04-21 16:30:13 -070068 : bytes_(bytes), byte_count_(byte_count), byte_offset_(), bit_offset_() {
henrikg91d6ede2015-09-17 00:24:34 -070069 RTC_DCHECK(static_cast<uint64_t>(byte_count_) <=
70 std::numeric_limits<uint32_t>::max());
Noah Richardsbbf7c862015-04-21 16:30:13 -070071}
72
Noah Richards9b9f1c42015-05-12 12:20:47 -070073uint64_t BitBuffer::RemainingBitCount() const {
74 return (static_cast<uint64_t>(byte_count_) - byte_offset_) * 8 - bit_offset_;
Noah Richardsbbf7c862015-04-21 16:30:13 -070075}
76
Björn Tereliusa77e16c2021-05-17 17:20:53 +020077bool BitBuffer::ReadUInt8(uint8_t& val) {
Noah Richards9b9f1c42015-05-12 12:20:47 -070078 uint32_t bit_val;
Björn Tereliusa77e16c2021-05-17 17:20:53 +020079 if (!ReadBits(sizeof(uint8_t) * 8, bit_val)) {
Noah Richardsbbf7c862015-04-21 16:30:13 -070080 return false;
81 }
henrikg91d6ede2015-09-17 00:24:34 -070082 RTC_DCHECK(bit_val <= std::numeric_limits<uint8_t>::max());
Björn Tereliusa77e16c2021-05-17 17:20:53 +020083 val = static_cast<uint8_t>(bit_val);
Noah Richardsbbf7c862015-04-21 16:30:13 -070084 return true;
85}
86
Björn Tereliusa77e16c2021-05-17 17:20:53 +020087bool BitBuffer::ReadUInt16(uint16_t& val) {
Noah Richards9b9f1c42015-05-12 12:20:47 -070088 uint32_t bit_val;
Björn Tereliusa77e16c2021-05-17 17:20:53 +020089 if (!ReadBits(sizeof(uint16_t) * 8, bit_val)) {
Noah Richardsbbf7c862015-04-21 16:30:13 -070090 return false;
91 }
henrikg91d6ede2015-09-17 00:24:34 -070092 RTC_DCHECK(bit_val <= std::numeric_limits<uint16_t>::max());
Björn Tereliusa77e16c2021-05-17 17:20:53 +020093 val = static_cast<uint16_t>(bit_val);
Noah Richardsbbf7c862015-04-21 16:30:13 -070094 return true;
95}
96
Björn Tereliusa77e16c2021-05-17 17:20:53 +020097bool BitBuffer::ReadUInt32(uint32_t& val) {
98 return ReadBits(sizeof(uint32_t) * 8, val);
Noah Richardsbbf7c862015-04-21 16:30:13 -070099}
100
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200101bool BitBuffer::PeekBits(size_t bit_count, uint32_t& val) {
Niels Möller827cf3c2018-09-27 11:02:08 +0200102 // TODO(nisse): Could allow bit_count == 0 and always return success. But
103 // current code reads one byte beyond end of buffer in the case that
104 // RemainingBitCount() == 0 and bit_count == 0.
105 RTC_DCHECK(bit_count > 0);
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200106 if (bit_count > RemainingBitCount() || bit_count > 32) {
Noah Richardsbbf7c862015-04-21 16:30:13 -0700107 return false;
108 }
Noah Richards9b9f1c42015-05-12 12:20:47 -0700109 const uint8_t* bytes = bytes_ + byte_offset_;
Noah Richardsbbf7c862015-04-21 16:30:13 -0700110 size_t remaining_bits_in_current_byte = 8 - bit_offset_;
Noah Richards9b9f1c42015-05-12 12:20:47 -0700111 uint32_t bits = LowestBits(*bytes++, remaining_bits_in_current_byte);
Noah Richardsbbf7c862015-04-21 16:30:13 -0700112 // If we're reading fewer bits than what's left in the current byte, just
113 // return the portion of this byte that we need.
114 if (bit_count < remaining_bits_in_current_byte) {
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200115 val = HighestBits(bits, bit_offset_ + bit_count);
Noah Richardsbbf7c862015-04-21 16:30:13 -0700116 return true;
117 }
118 // Otherwise, subtract what we've read from the bit count and read as many
119 // full bytes as we can into bits.
120 bit_count -= remaining_bits_in_current_byte;
121 while (bit_count >= 8) {
122 bits = (bits << 8) | *bytes++;
123 bit_count -= 8;
124 }
125 // Whatever we have left is smaller than a byte, so grab just the bits we need
126 // and shift them into the lowest bits.
127 if (bit_count > 0) {
128 bits <<= bit_count;
129 bits |= HighestBits(*bytes, bit_count);
130 }
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200131 val = bits;
Noah Richardsbbf7c862015-04-21 16:30:13 -0700132 return true;
133}
134
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200135bool BitBuffer::PeekBits(size_t bit_count, uint64_t& val) {
Björn Terelius39284362021-05-11 15:30:12 +0200136 // TODO(nisse): Could allow bit_count == 0 and always return success. But
137 // current code reads one byte beyond end of buffer in the case that
138 // RemainingBitCount() == 0 and bit_count == 0.
139 RTC_DCHECK(bit_count > 0);
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200140 if (bit_count > RemainingBitCount() || bit_count > 64) {
Björn Terelius39284362021-05-11 15:30:12 +0200141 return false;
142 }
143 const uint8_t* bytes = bytes_ + byte_offset_;
144 size_t remaining_bits_in_current_byte = 8 - bit_offset_;
145 uint64_t bits = LowestBits(*bytes++, remaining_bits_in_current_byte);
146 // If we're reading fewer bits than what's left in the current byte, just
147 // return the portion of this byte that we need.
148 if (bit_count < remaining_bits_in_current_byte) {
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200149 val = HighestBits(bits, bit_offset_ + bit_count);
Björn Terelius39284362021-05-11 15:30:12 +0200150 return true;
151 }
152 // Otherwise, subtract what we've read from the bit count and read as many
153 // full bytes as we can into bits.
154 bit_count -= remaining_bits_in_current_byte;
155 while (bit_count >= 8) {
156 bits = (bits << 8) | *bytes++;
157 bit_count -= 8;
158 }
159 // Whatever we have left is smaller than a byte, so grab just the bits we need
160 // and shift them into the lowest bits.
161 if (bit_count > 0) {
162 bits <<= bit_count;
163 bits |= HighestBits(*bytes, bit_count);
164 }
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200165 val = bits;
Björn Terelius39284362021-05-11 15:30:12 +0200166 return true;
167}
168
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200169bool BitBuffer::ReadBits(size_t bit_count, uint32_t& val) {
170 return PeekBits(bit_count, val) && ConsumeBits(bit_count);
Noah Richardsbbf7c862015-04-21 16:30:13 -0700171}
172
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200173bool BitBuffer::ReadBits(size_t bit_count, uint64_t& val) {
174 return PeekBits(bit_count, val) && ConsumeBits(bit_count);
Björn Terelius39284362021-05-11 15:30:12 +0200175}
176
Noah Richardsbbf7c862015-04-21 16:30:13 -0700177bool BitBuffer::ConsumeBytes(size_t byte_count) {
178 return ConsumeBits(byte_count * 8);
179}
180
181bool BitBuffer::ConsumeBits(size_t bit_count) {
182 if (bit_count > RemainingBitCount()) {
183 return false;
184 }
185
186 byte_offset_ += (bit_offset_ + bit_count) / 8;
187 bit_offset_ = (bit_offset_ + bit_count) % 8;
188 return true;
189}
190
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200191bool BitBuffer::ReadNonSymmetric(uint32_t num_values, uint32_t& val) {
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200192 RTC_DCHECK_GT(num_values, 0);
193 RTC_DCHECK_LE(num_values, uint32_t{1} << 31);
Danil Chapovalova9e1b492020-07-08 11:24:19 +0200194 if (num_values == 1) {
195 // When there is only one possible value, it requires zero bits to store it.
196 // But ReadBits doesn't support reading zero bits.
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200197 val = 0;
Danil Chapovalova9e1b492020-07-08 11:24:19 +0200198 return true;
199 }
Danil Chapovalov09fb7872021-08-20 12:46:14 +0200200 size_t count_bits = absl::bit_width(num_values);
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200201 uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values;
202
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200203 if (!ReadBits(count_bits - 1, val)) {
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200204 return false;
205 }
206
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200207 if (val < num_min_bits_values) {
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200208 return true;
209 }
210
211 uint32_t extra_bit;
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200212 if (!ReadBits(/*bit_count=*/1, extra_bit)) {
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200213 return false;
214 }
215
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200216 val = (val << 1) + extra_bit - num_min_bits_values;
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200217 return true;
218}
219
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200220bool BitBuffer::ReadExponentialGolomb(uint32_t& val) {
Noah Richardsbbf7c862015-04-21 16:30:13 -0700221 // Store off the current byte/bit offset, in case we want to restore them due
222 // to a failed parse.
223 size_t original_byte_offset = byte_offset_;
224 size_t original_bit_offset = bit_offset_;
225
226 // Count the number of leading 0 bits by peeking/consuming them one at a time.
227 size_t zero_bit_count = 0;
Noah Richards9b9f1c42015-05-12 12:20:47 -0700228 uint32_t peeked_bit;
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200229 while (PeekBits(1, peeked_bit) && peeked_bit == 0) {
Noah Richardsbbf7c862015-04-21 16:30:13 -0700230 zero_bit_count++;
231 ConsumeBits(1);
232 }
233
234 // We should either be at the end of the stream, or the next bit should be 1.
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200235 RTC_DCHECK(!PeekBits(1, peeked_bit) || peeked_bit == 1);
Noah Richardsbbf7c862015-04-21 16:30:13 -0700236
237 // The bit count of the value is the number of zeros + 1. Make sure that many
Noah Richards9b9f1c42015-05-12 12:20:47 -0700238 // bits fits in a uint32_t and that we have enough bits left for it, and then
Noah Richardsbbf7c862015-04-21 16:30:13 -0700239 // read the value.
240 size_t value_bit_count = zero_bit_count + 1;
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200241 if (value_bit_count > 32 || !ReadBits(value_bit_count, val)) {
henrikg91d6ede2015-09-17 00:24:34 -0700242 RTC_CHECK(Seek(original_byte_offset, original_bit_offset));
Noah Richardsbbf7c862015-04-21 16:30:13 -0700243 return false;
244 }
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200245 val -= 1;
Noah Richardsbbf7c862015-04-21 16:30:13 -0700246 return true;
247}
248
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200249bool BitBuffer::ReadSignedExponentialGolomb(int32_t& val) {
Peter Boström8c266e62015-09-24 15:06:50 +0200250 uint32_t unsigned_val;
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200251 if (!ReadExponentialGolomb(unsigned_val)) {
Peter Boström8c266e62015-09-24 15:06:50 +0200252 return false;
253 }
254 if ((unsigned_val & 1) == 0) {
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200255 val = -static_cast<int32_t>(unsigned_val / 2);
Peter Boström8c266e62015-09-24 15:06:50 +0200256 } else {
Björn Tereliusa77e16c2021-05-17 17:20:53 +0200257 val = (unsigned_val + 1) / 2;
Peter Boström8c266e62015-09-24 15:06:50 +0200258 }
259 return true;
260}
261
Yves Gerey665174f2018-06-19 15:03:05 +0200262void BitBuffer::GetCurrentOffset(size_t* out_byte_offset,
263 size_t* out_bit_offset) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800264 RTC_CHECK(out_byte_offset != nullptr);
265 RTC_CHECK(out_bit_offset != nullptr);
Noah Richards86153c22015-04-28 15:13:44 -0700266 *out_byte_offset = byte_offset_;
267 *out_bit_offset = bit_offset_;
268}
269
270bool BitBuffer::Seek(size_t byte_offset, size_t bit_offset) {
271 if (byte_offset > byte_count_ || bit_offset > 7 ||
272 (byte_offset == byte_count_ && bit_offset > 0)) {
273 return false;
274 }
275 byte_offset_ = byte_offset;
276 bit_offset_ = bit_offset;
277 return true;
278}
279
Noah Richards9b9f1c42015-05-12 12:20:47 -0700280BitBufferWriter::BitBufferWriter(uint8_t* bytes, size_t byte_count)
Yves Gerey665174f2018-06-19 15:03:05 +0200281 : BitBuffer(bytes, byte_count), writable_bytes_(bytes) {}
Noah Richards86153c22015-04-28 15:13:44 -0700282
Noah Richards9b9f1c42015-05-12 12:20:47 -0700283bool BitBufferWriter::WriteUInt8(uint8_t val) {
284 return WriteBits(val, sizeof(uint8_t) * 8);
Noah Richards86153c22015-04-28 15:13:44 -0700285}
286
Noah Richards9b9f1c42015-05-12 12:20:47 -0700287bool BitBufferWriter::WriteUInt16(uint16_t val) {
288 return WriteBits(val, sizeof(uint16_t) * 8);
Noah Richards86153c22015-04-28 15:13:44 -0700289}
290
Noah Richards9b9f1c42015-05-12 12:20:47 -0700291bool BitBufferWriter::WriteUInt32(uint32_t val) {
292 return WriteBits(val, sizeof(uint32_t) * 8);
Noah Richards86153c22015-04-28 15:13:44 -0700293}
294
Noah Richards9b9f1c42015-05-12 12:20:47 -0700295bool BitBufferWriter::WriteBits(uint64_t val, size_t bit_count) {
Noah Richards86153c22015-04-28 15:13:44 -0700296 if (bit_count > RemainingBitCount()) {
297 return false;
298 }
299 size_t total_bits = bit_count;
300
301 // For simplicity, push the bits we want to read from val to the highest bits.
Noah Richards9b9f1c42015-05-12 12:20:47 -0700302 val <<= (sizeof(uint64_t) * 8 - bit_count);
Noah Richards86153c22015-04-28 15:13:44 -0700303
Noah Richards9b9f1c42015-05-12 12:20:47 -0700304 uint8_t* bytes = writable_bytes_ + byte_offset_;
Noah Richards86153c22015-04-28 15:13:44 -0700305
306 // The first byte is relatively special; the bit offset to write to may put us
307 // in the middle of the byte, and the total bit count to write may require we
308 // save the bits at the end of the byte.
309 size_t remaining_bits_in_current_byte = 8 - bit_offset_;
310 size_t bits_in_first_byte =
311 std::min(bit_count, remaining_bits_in_current_byte);
Yves Gerey665174f2018-06-19 15:03:05 +0200312 *bytes = WritePartialByte(HighestByte(val), bits_in_first_byte, *bytes,
313 bit_offset_);
Noah Richards86153c22015-04-28 15:13:44 -0700314 if (bit_count <= remaining_bits_in_current_byte) {
315 // Nothing left to write, so quit early.
316 return ConsumeBits(total_bits);
317 }
318
319 // Subtract what we've written from the bit count, shift it off the value, and
320 // write the remaining full bytes.
321 val <<= bits_in_first_byte;
322 bytes++;
323 bit_count -= bits_in_first_byte;
324 while (bit_count >= 8) {
325 *bytes++ = HighestByte(val);
326 val <<= 8;
327 bit_count -= 8;
328 }
329
330 // Last byte may also be partial, so write the remaining bits from the top of
331 // val.
332 if (bit_count > 0) {
333 *bytes = WritePartialByte(HighestByte(val), bit_count, *bytes, 0);
334 }
335
336 // All done! Consume the bits we've written.
337 return ConsumeBits(total_bits);
338}
339
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200340bool BitBufferWriter::WriteNonSymmetric(uint32_t val, uint32_t num_values) {
341 RTC_DCHECK_LT(val, num_values);
342 RTC_DCHECK_LE(num_values, uint32_t{1} << 31);
Danil Chapovalova9e1b492020-07-08 11:24:19 +0200343 if (num_values == 1) {
344 // When there is only one possible value, it requires zero bits to store it.
345 // But WriteBits doesn't support writing zero bits.
346 return true;
347 }
Danil Chapovalov09fb7872021-08-20 12:46:14 +0200348 size_t count_bits = absl::bit_width(num_values);
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200349 uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values;
350
351 return val < num_min_bits_values
352 ? WriteBits(val, count_bits - 1)
353 : WriteBits(val + num_min_bits_values, count_bits);
354}
355
356size_t BitBufferWriter::SizeNonSymmetricBits(uint32_t val,
357 uint32_t num_values) {
358 RTC_DCHECK_LT(val, num_values);
359 RTC_DCHECK_LE(num_values, uint32_t{1} << 31);
Danil Chapovalov09fb7872021-08-20 12:46:14 +0200360 size_t count_bits = absl::bit_width(num_values);
Danil Chapovalova2b30d82019-07-03 14:41:50 +0200361 uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values;
362
363 return val < num_min_bits_values ? (count_bits - 1) : count_bits;
364}
365
Noah Richards9b9f1c42015-05-12 12:20:47 -0700366bool BitBufferWriter::WriteExponentialGolomb(uint32_t val) {
367 // We don't support reading UINT32_MAX, because it doesn't fit in a uint32_t
Noah Richards86153c22015-04-28 15:13:44 -0700368 // when encoded, so don't support writing it either.
Noah Richards9b9f1c42015-05-12 12:20:47 -0700369 if (val == std::numeric_limits<uint32_t>::max()) {
Noah Richards86153c22015-04-28 15:13:44 -0700370 return false;
371 }
Noah Richards9b9f1c42015-05-12 12:20:47 -0700372 uint64_t val_to_encode = static_cast<uint64_t>(val) + 1;
Noah Richards86153c22015-04-28 15:13:44 -0700373
Danil Chapovalov09fb7872021-08-20 12:46:14 +0200374 // We need to write bit_width(val+1) 0s and then val+1. Since val (as a
Noah Richards9b9f1c42015-05-12 12:20:47 -0700375 // uint64_t) has leading zeros, we can just write the total golomb encoded
376 // size worth of bits, knowing the value will appear last.
Danil Chapovalov09fb7872021-08-20 12:46:14 +0200377 return WriteBits(val_to_encode, absl::bit_width(val_to_encode) * 2 - 1);
Noah Richards86153c22015-04-28 15:13:44 -0700378}
379
sprang52033d62016-06-02 02:43:32 -0700380bool BitBufferWriter::WriteSignedExponentialGolomb(int32_t val) {
381 if (val == 0) {
382 return WriteExponentialGolomb(0);
383 } else if (val > 0) {
384 uint32_t signed_val = val;
385 return WriteExponentialGolomb((signed_val * 2) - 1);
386 } else {
387 if (val == std::numeric_limits<int32_t>::min())
388 return false; // Not supported, would cause overflow.
389 uint32_t signed_val = -val;
390 return WriteExponentialGolomb(signed_val * 2);
391 }
392}
393
Noah Richardsbbf7c862015-04-21 16:30:13 -0700394} // namespace rtc