Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/bit_buffer.h" |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 12 | |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 13 | #include <algorithm> |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 14 | #include <limits> |
| 15 | |
Danil Chapovalov | 09fb787 | 2021-08-20 12:46:14 +0200 | [diff] [blame^] | 16 | #include "absl/numeric/bits.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 18 | |
| 19 | namespace { |
| 20 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 21 | // Returns the lowest (right-most) `bit_count` bits in `byte`. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 22 | uint8_t LowestBits(uint8_t byte, size_t bit_count) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 23 | RTC_DCHECK_LE(bit_count, 8); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 24 | return byte & ((1 << bit_count) - 1); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 27 | // Returns the highest (left-most) `bit_count` bits in `byte`, shifted to the |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 28 | // lowest bits (to the right). |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 29 | uint8_t HighestBits(uint8_t byte, size_t bit_count) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 30 | RTC_DCHECK_LE(bit_count, 8); |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 31 | uint8_t shift = 8 - static_cast<uint8_t>(bit_count); |
| 32 | uint8_t mask = 0xFF << shift; |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 33 | return (byte & mask) >> shift; |
| 34 | } |
| 35 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 36 | // Returns the highest byte of `val` in a uint8_t. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 37 | uint8_t HighestByte(uint64_t val) { |
| 38 | return static_cast<uint8_t>(val >> 56); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 41 | // 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 Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 44 | uint8_t WritePartialByte(uint8_t source, |
| 45 | size_t source_bit_count, |
| 46 | uint8_t target, |
| 47 | size_t target_bit_offset) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 48 | RTC_DCHECK(target_bit_offset < 8); |
| 49 | RTC_DCHECK(source_bit_count < 9); |
| 50 | RTC_DCHECK(source_bit_count <= (8 - target_bit_offset)); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 51 | // Generate a mask for just the bits we're going to overwrite, so: |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 52 | uint8_t mask = |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 53 | // The number of bits we want, in the most significant bits... |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 54 | static_cast<uint8_t>(0xFF << (8 - source_bit_count)) |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 55 | // ...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 Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 63 | } // namespace |
| 64 | |
| 65 | namespace rtc { |
| 66 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 67 | BitBuffer::BitBuffer(const uint8_t* bytes, size_t byte_count) |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 68 | : bytes_(bytes), byte_count_(byte_count), byte_offset_(), bit_offset_() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 69 | RTC_DCHECK(static_cast<uint64_t>(byte_count_) <= |
| 70 | std::numeric_limits<uint32_t>::max()); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 73 | uint64_t BitBuffer::RemainingBitCount() const { |
| 74 | return (static_cast<uint64_t>(byte_count_) - byte_offset_) * 8 - bit_offset_; |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 77 | bool BitBuffer::ReadUInt8(uint8_t& val) { |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 78 | uint32_t bit_val; |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 79 | if (!ReadBits(sizeof(uint8_t) * 8, bit_val)) { |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 80 | return false; |
| 81 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 82 | RTC_DCHECK(bit_val <= std::numeric_limits<uint8_t>::max()); |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 83 | val = static_cast<uint8_t>(bit_val); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 87 | bool BitBuffer::ReadUInt16(uint16_t& val) { |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 88 | uint32_t bit_val; |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 89 | if (!ReadBits(sizeof(uint16_t) * 8, bit_val)) { |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 92 | RTC_DCHECK(bit_val <= std::numeric_limits<uint16_t>::max()); |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 93 | val = static_cast<uint16_t>(bit_val); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 94 | return true; |
| 95 | } |
| 96 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 97 | bool BitBuffer::ReadUInt32(uint32_t& val) { |
| 98 | return ReadBits(sizeof(uint32_t) * 8, val); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 101 | bool BitBuffer::PeekBits(size_t bit_count, uint32_t& val) { |
Niels Möller | 827cf3c | 2018-09-27 11:02:08 +0200 | [diff] [blame] | 102 | // 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 Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 106 | if (bit_count > RemainingBitCount() || bit_count > 32) { |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 107 | return false; |
| 108 | } |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 109 | const uint8_t* bytes = bytes_ + byte_offset_; |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 110 | size_t remaining_bits_in_current_byte = 8 - bit_offset_; |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 111 | uint32_t bits = LowestBits(*bytes++, remaining_bits_in_current_byte); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 112 | // 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 Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 115 | val = HighestBits(bits, bit_offset_ + bit_count); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 116 | 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 Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 131 | val = bits; |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 135 | bool BitBuffer::PeekBits(size_t bit_count, uint64_t& val) { |
Björn Terelius | 3928436 | 2021-05-11 15:30:12 +0200 | [diff] [blame] | 136 | // 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 Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 140 | if (bit_count > RemainingBitCount() || bit_count > 64) { |
Björn Terelius | 3928436 | 2021-05-11 15:30:12 +0200 | [diff] [blame] | 141 | 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 Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 149 | val = HighestBits(bits, bit_offset_ + bit_count); |
Björn Terelius | 3928436 | 2021-05-11 15:30:12 +0200 | [diff] [blame] | 150 | 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 Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 165 | val = bits; |
Björn Terelius | 3928436 | 2021-05-11 15:30:12 +0200 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 169 | bool BitBuffer::ReadBits(size_t bit_count, uint32_t& val) { |
| 170 | return PeekBits(bit_count, val) && ConsumeBits(bit_count); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 173 | bool BitBuffer::ReadBits(size_t bit_count, uint64_t& val) { |
| 174 | return PeekBits(bit_count, val) && ConsumeBits(bit_count); |
Björn Terelius | 3928436 | 2021-05-11 15:30:12 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 177 | bool BitBuffer::ConsumeBytes(size_t byte_count) { |
| 178 | return ConsumeBits(byte_count * 8); |
| 179 | } |
| 180 | |
| 181 | bool 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 Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 191 | bool BitBuffer::ReadNonSymmetric(uint32_t num_values, uint32_t& val) { |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 192 | RTC_DCHECK_GT(num_values, 0); |
| 193 | RTC_DCHECK_LE(num_values, uint32_t{1} << 31); |
Danil Chapovalov | a9e1b49 | 2020-07-08 11:24:19 +0200 | [diff] [blame] | 194 | 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 Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 197 | val = 0; |
Danil Chapovalov | a9e1b49 | 2020-07-08 11:24:19 +0200 | [diff] [blame] | 198 | return true; |
| 199 | } |
Danil Chapovalov | 09fb787 | 2021-08-20 12:46:14 +0200 | [diff] [blame^] | 200 | size_t count_bits = absl::bit_width(num_values); |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 201 | uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values; |
| 202 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 203 | if (!ReadBits(count_bits - 1, val)) { |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 207 | if (val < num_min_bits_values) { |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 208 | return true; |
| 209 | } |
| 210 | |
| 211 | uint32_t extra_bit; |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 212 | if (!ReadBits(/*bit_count=*/1, extra_bit)) { |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 216 | val = (val << 1) + extra_bit - num_min_bits_values; |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 220 | bool BitBuffer::ReadExponentialGolomb(uint32_t& val) { |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 221 | // 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 Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 228 | uint32_t peeked_bit; |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 229 | while (PeekBits(1, peeked_bit) && peeked_bit == 0) { |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 230 | 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 Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 235 | RTC_DCHECK(!PeekBits(1, peeked_bit) || peeked_bit == 1); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 236 | |
| 237 | // The bit count of the value is the number of zeros + 1. Make sure that many |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 238 | // bits fits in a uint32_t and that we have enough bits left for it, and then |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 239 | // read the value. |
| 240 | size_t value_bit_count = zero_bit_count + 1; |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 241 | if (value_bit_count > 32 || !ReadBits(value_bit_count, val)) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 242 | RTC_CHECK(Seek(original_byte_offset, original_bit_offset)); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 243 | return false; |
| 244 | } |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 245 | val -= 1; |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 246 | return true; |
| 247 | } |
| 248 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 249 | bool BitBuffer::ReadSignedExponentialGolomb(int32_t& val) { |
Peter Boström | 8c266e6 | 2015-09-24 15:06:50 +0200 | [diff] [blame] | 250 | uint32_t unsigned_val; |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 251 | if (!ReadExponentialGolomb(unsigned_val)) { |
Peter Boström | 8c266e6 | 2015-09-24 15:06:50 +0200 | [diff] [blame] | 252 | return false; |
| 253 | } |
| 254 | if ((unsigned_val & 1) == 0) { |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 255 | val = -static_cast<int32_t>(unsigned_val / 2); |
Peter Boström | 8c266e6 | 2015-09-24 15:06:50 +0200 | [diff] [blame] | 256 | } else { |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 257 | val = (unsigned_val + 1) / 2; |
Peter Boström | 8c266e6 | 2015-09-24 15:06:50 +0200 | [diff] [blame] | 258 | } |
| 259 | return true; |
| 260 | } |
| 261 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 262 | void BitBuffer::GetCurrentOffset(size_t* out_byte_offset, |
| 263 | size_t* out_bit_offset) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 264 | RTC_CHECK(out_byte_offset != nullptr); |
| 265 | RTC_CHECK(out_bit_offset != nullptr); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 266 | *out_byte_offset = byte_offset_; |
| 267 | *out_bit_offset = bit_offset_; |
| 268 | } |
| 269 | |
| 270 | bool 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 Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 280 | BitBufferWriter::BitBufferWriter(uint8_t* bytes, size_t byte_count) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 281 | : BitBuffer(bytes, byte_count), writable_bytes_(bytes) {} |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 282 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 283 | bool BitBufferWriter::WriteUInt8(uint8_t val) { |
| 284 | return WriteBits(val, sizeof(uint8_t) * 8); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 287 | bool BitBufferWriter::WriteUInt16(uint16_t val) { |
| 288 | return WriteBits(val, sizeof(uint16_t) * 8); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 291 | bool BitBufferWriter::WriteUInt32(uint32_t val) { |
| 292 | return WriteBits(val, sizeof(uint32_t) * 8); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 295 | bool BitBufferWriter::WriteBits(uint64_t val, size_t bit_count) { |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 296 | 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 Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 302 | val <<= (sizeof(uint64_t) * 8 - bit_count); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 303 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 304 | uint8_t* bytes = writable_bytes_ + byte_offset_; |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 305 | |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 312 | *bytes = WritePartialByte(HighestByte(val), bits_in_first_byte, *bytes, |
| 313 | bit_offset_); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 314 | 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 Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 340 | bool 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 Chapovalov | a9e1b49 | 2020-07-08 11:24:19 +0200 | [diff] [blame] | 343 | 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 Chapovalov | 09fb787 | 2021-08-20 12:46:14 +0200 | [diff] [blame^] | 348 | size_t count_bits = absl::bit_width(num_values); |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 349 | 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 | |
| 356 | size_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 Chapovalov | 09fb787 | 2021-08-20 12:46:14 +0200 | [diff] [blame^] | 360 | size_t count_bits = absl::bit_width(num_values); |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 361 | 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 Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 366 | bool BitBufferWriter::WriteExponentialGolomb(uint32_t val) { |
| 367 | // We don't support reading UINT32_MAX, because it doesn't fit in a uint32_t |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 368 | // when encoded, so don't support writing it either. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 369 | if (val == std::numeric_limits<uint32_t>::max()) { |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 370 | return false; |
| 371 | } |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 372 | uint64_t val_to_encode = static_cast<uint64_t>(val) + 1; |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 373 | |
Danil Chapovalov | 09fb787 | 2021-08-20 12:46:14 +0200 | [diff] [blame^] | 374 | // We need to write bit_width(val+1) 0s and then val+1. Since val (as a |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 375 | // 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 Chapovalov | 09fb787 | 2021-08-20 12:46:14 +0200 | [diff] [blame^] | 377 | return WriteBits(val_to_encode, absl::bit_width(val_to_encode) * 2 - 1); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 378 | } |
| 379 | |
sprang | 52033d6 | 2016-06-02 02:43:32 -0700 | [diff] [blame] | 380 | bool 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 Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 394 | } // namespace rtc |