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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 17 | |
| 18 | namespace { |
| 19 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame^] | 20 | // Returns the lowest (right-most) `bit_count` bits in `byte`. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 21 | uint8_t LowestBits(uint8_t byte, size_t bit_count) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 22 | RTC_DCHECK_LE(bit_count, 8); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 23 | return byte & ((1 << bit_count) - 1); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame^] | 26 | // 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] | 27 | // lowest bits (to the right). |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 28 | uint8_t HighestBits(uint8_t byte, size_t bit_count) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 29 | RTC_DCHECK_LE(bit_count, 8); |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 30 | uint8_t shift = 8 - static_cast<uint8_t>(bit_count); |
| 31 | uint8_t mask = 0xFF << shift; |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 32 | return (byte & mask) >> shift; |
| 33 | } |
| 34 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame^] | 35 | // Returns the highest byte of `val` in a uint8_t. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 36 | uint8_t HighestByte(uint64_t val) { |
| 37 | return static_cast<uint8_t>(val >> 56); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame^] | 40 | // Returns the result of writing partial data from `source`, of |
| 41 | // `source_bit_count` size in the highest bits, to `target` at |
| 42 | // `target_bit_offset` from the highest bit. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 43 | uint8_t WritePartialByte(uint8_t source, |
| 44 | size_t source_bit_count, |
| 45 | uint8_t target, |
| 46 | size_t target_bit_offset) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 47 | RTC_DCHECK(target_bit_offset < 8); |
| 48 | RTC_DCHECK(source_bit_count < 9); |
| 49 | RTC_DCHECK(source_bit_count <= (8 - target_bit_offset)); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 50 | // 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] | 51 | uint8_t mask = |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 52 | // The number of bits we want, in the most significant bits... |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 53 | static_cast<uint8_t>(0xFF << (8 - source_bit_count)) |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 54 | // ...shifted over to the target offset from the most signficant bit. |
| 55 | >> target_bit_offset; |
| 56 | |
| 57 | // We want the target, with the bits we'll overwrite masked off, or'ed with |
| 58 | // the bits from the source we want. |
| 59 | return (target & ~mask) | (source >> target_bit_offset); |
| 60 | } |
| 61 | |
| 62 | // Counts the number of bits used in the binary representation of val. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 63 | size_t CountBits(uint64_t val) { |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 64 | size_t bit_count = 0; |
| 65 | while (val != 0) { |
| 66 | bit_count++; |
| 67 | val >>= 1; |
| 68 | } |
| 69 | return bit_count; |
| 70 | } |
| 71 | |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 72 | } // namespace |
| 73 | |
| 74 | namespace rtc { |
| 75 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 76 | BitBuffer::BitBuffer(const uint8_t* bytes, size_t byte_count) |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 77 | : bytes_(bytes), byte_count_(byte_count), byte_offset_(), bit_offset_() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 78 | RTC_DCHECK(static_cast<uint64_t>(byte_count_) <= |
| 79 | std::numeric_limits<uint32_t>::max()); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 82 | uint64_t BitBuffer::RemainingBitCount() const { |
| 83 | return (static_cast<uint64_t>(byte_count_) - byte_offset_) * 8 - bit_offset_; |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 86 | bool BitBuffer::ReadUInt8(uint8_t& val) { |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 87 | uint32_t bit_val; |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 88 | if (!ReadBits(sizeof(uint8_t) * 8, bit_val)) { |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 89 | return false; |
| 90 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 91 | RTC_DCHECK(bit_val <= std::numeric_limits<uint8_t>::max()); |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 92 | val = static_cast<uint8_t>(bit_val); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 96 | bool BitBuffer::ReadUInt16(uint16_t& val) { |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 97 | uint32_t bit_val; |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 98 | if (!ReadBits(sizeof(uint16_t) * 8, bit_val)) { |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 99 | return false; |
| 100 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 101 | RTC_DCHECK(bit_val <= std::numeric_limits<uint16_t>::max()); |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 102 | val = static_cast<uint16_t>(bit_val); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 106 | bool BitBuffer::ReadUInt32(uint32_t& val) { |
| 107 | return ReadBits(sizeof(uint32_t) * 8, val); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 110 | bool BitBuffer::PeekBits(size_t bit_count, uint32_t& val) { |
Niels Möller | 827cf3c | 2018-09-27 11:02:08 +0200 | [diff] [blame] | 111 | // TODO(nisse): Could allow bit_count == 0 and always return success. But |
| 112 | // current code reads one byte beyond end of buffer in the case that |
| 113 | // RemainingBitCount() == 0 and bit_count == 0. |
| 114 | RTC_DCHECK(bit_count > 0); |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 115 | if (bit_count > RemainingBitCount() || bit_count > 32) { |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 118 | const uint8_t* bytes = bytes_ + byte_offset_; |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 119 | size_t remaining_bits_in_current_byte = 8 - bit_offset_; |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 120 | uint32_t bits = LowestBits(*bytes++, remaining_bits_in_current_byte); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 121 | // If we're reading fewer bits than what's left in the current byte, just |
| 122 | // return the portion of this byte that we need. |
| 123 | if (bit_count < remaining_bits_in_current_byte) { |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 124 | val = HighestBits(bits, bit_offset_ + bit_count); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | // Otherwise, subtract what we've read from the bit count and read as many |
| 128 | // full bytes as we can into bits. |
| 129 | bit_count -= remaining_bits_in_current_byte; |
| 130 | while (bit_count >= 8) { |
| 131 | bits = (bits << 8) | *bytes++; |
| 132 | bit_count -= 8; |
| 133 | } |
| 134 | // Whatever we have left is smaller than a byte, so grab just the bits we need |
| 135 | // and shift them into the lowest bits. |
| 136 | if (bit_count > 0) { |
| 137 | bits <<= bit_count; |
| 138 | bits |= HighestBits(*bytes, bit_count); |
| 139 | } |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 140 | val = bits; |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 144 | bool BitBuffer::PeekBits(size_t bit_count, uint64_t& val) { |
Björn Terelius | 3928436 | 2021-05-11 15:30:12 +0200 | [diff] [blame] | 145 | // TODO(nisse): Could allow bit_count == 0 and always return success. But |
| 146 | // current code reads one byte beyond end of buffer in the case that |
| 147 | // RemainingBitCount() == 0 and bit_count == 0. |
| 148 | RTC_DCHECK(bit_count > 0); |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 149 | if (bit_count > RemainingBitCount() || bit_count > 64) { |
Björn Terelius | 3928436 | 2021-05-11 15:30:12 +0200 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | const uint8_t* bytes = bytes_ + byte_offset_; |
| 153 | size_t remaining_bits_in_current_byte = 8 - bit_offset_; |
| 154 | uint64_t bits = LowestBits(*bytes++, remaining_bits_in_current_byte); |
| 155 | // If we're reading fewer bits than what's left in the current byte, just |
| 156 | // return the portion of this byte that we need. |
| 157 | if (bit_count < remaining_bits_in_current_byte) { |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 158 | val = HighestBits(bits, bit_offset_ + bit_count); |
Björn Terelius | 3928436 | 2021-05-11 15:30:12 +0200 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | // Otherwise, subtract what we've read from the bit count and read as many |
| 162 | // full bytes as we can into bits. |
| 163 | bit_count -= remaining_bits_in_current_byte; |
| 164 | while (bit_count >= 8) { |
| 165 | bits = (bits << 8) | *bytes++; |
| 166 | bit_count -= 8; |
| 167 | } |
| 168 | // Whatever we have left is smaller than a byte, so grab just the bits we need |
| 169 | // and shift them into the lowest bits. |
| 170 | if (bit_count > 0) { |
| 171 | bits <<= bit_count; |
| 172 | bits |= HighestBits(*bytes, bit_count); |
| 173 | } |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 174 | val = bits; |
Björn Terelius | 3928436 | 2021-05-11 15:30:12 +0200 | [diff] [blame] | 175 | return true; |
| 176 | } |
| 177 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 178 | bool BitBuffer::ReadBits(size_t bit_count, uint32_t& val) { |
| 179 | return PeekBits(bit_count, val) && ConsumeBits(bit_count); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 182 | bool BitBuffer::ReadBits(size_t bit_count, uint64_t& val) { |
| 183 | return PeekBits(bit_count, val) && ConsumeBits(bit_count); |
Björn Terelius | 3928436 | 2021-05-11 15:30:12 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 186 | bool BitBuffer::ConsumeBytes(size_t byte_count) { |
| 187 | return ConsumeBits(byte_count * 8); |
| 188 | } |
| 189 | |
| 190 | bool BitBuffer::ConsumeBits(size_t bit_count) { |
| 191 | if (bit_count > RemainingBitCount()) { |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | byte_offset_ += (bit_offset_ + bit_count) / 8; |
| 196 | bit_offset_ = (bit_offset_ + bit_count) % 8; |
| 197 | return true; |
| 198 | } |
| 199 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 200 | bool BitBuffer::ReadNonSymmetric(uint32_t num_values, uint32_t& val) { |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 201 | RTC_DCHECK_GT(num_values, 0); |
| 202 | RTC_DCHECK_LE(num_values, uint32_t{1} << 31); |
Danil Chapovalov | a9e1b49 | 2020-07-08 11:24:19 +0200 | [diff] [blame] | 203 | if (num_values == 1) { |
| 204 | // When there is only one possible value, it requires zero bits to store it. |
| 205 | // But ReadBits doesn't support reading zero bits. |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 206 | val = 0; |
Danil Chapovalov | a9e1b49 | 2020-07-08 11:24:19 +0200 | [diff] [blame] | 207 | return true; |
| 208 | } |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 209 | size_t count_bits = CountBits(num_values); |
| 210 | uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values; |
| 211 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 212 | if (!ReadBits(count_bits - 1, val)) { |
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 | if (val < num_min_bits_values) { |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
| 220 | uint32_t extra_bit; |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 221 | if (!ReadBits(/*bit_count=*/1, extra_bit)) { |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 222 | return false; |
| 223 | } |
| 224 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 225 | val = (val << 1) + extra_bit - num_min_bits_values; |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 229 | bool BitBuffer::ReadExponentialGolomb(uint32_t& val) { |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 230 | // Store off the current byte/bit offset, in case we want to restore them due |
| 231 | // to a failed parse. |
| 232 | size_t original_byte_offset = byte_offset_; |
| 233 | size_t original_bit_offset = bit_offset_; |
| 234 | |
| 235 | // Count the number of leading 0 bits by peeking/consuming them one at a time. |
| 236 | size_t zero_bit_count = 0; |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 237 | uint32_t peeked_bit; |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 238 | while (PeekBits(1, peeked_bit) && peeked_bit == 0) { |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 239 | zero_bit_count++; |
| 240 | ConsumeBits(1); |
| 241 | } |
| 242 | |
| 243 | // 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] | 244 | RTC_DCHECK(!PeekBits(1, peeked_bit) || peeked_bit == 1); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 245 | |
| 246 | // 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] | 247 | // 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] | 248 | // read the value. |
| 249 | size_t value_bit_count = zero_bit_count + 1; |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 250 | if (value_bit_count > 32 || !ReadBits(value_bit_count, val)) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 251 | RTC_CHECK(Seek(original_byte_offset, original_bit_offset)); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 252 | return false; |
| 253 | } |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 254 | val -= 1; |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 255 | return true; |
| 256 | } |
| 257 | |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 258 | bool BitBuffer::ReadSignedExponentialGolomb(int32_t& val) { |
Peter Boström | 8c266e6 | 2015-09-24 15:06:50 +0200 | [diff] [blame] | 259 | uint32_t unsigned_val; |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 260 | if (!ReadExponentialGolomb(unsigned_val)) { |
Peter Boström | 8c266e6 | 2015-09-24 15:06:50 +0200 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | if ((unsigned_val & 1) == 0) { |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 264 | val = -static_cast<int32_t>(unsigned_val / 2); |
Peter Boström | 8c266e6 | 2015-09-24 15:06:50 +0200 | [diff] [blame] | 265 | } else { |
Björn Terelius | a77e16c | 2021-05-17 17:20:53 +0200 | [diff] [blame] | 266 | val = (unsigned_val + 1) / 2; |
Peter Boström | 8c266e6 | 2015-09-24 15:06:50 +0200 | [diff] [blame] | 267 | } |
| 268 | return true; |
| 269 | } |
| 270 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 271 | void BitBuffer::GetCurrentOffset(size_t* out_byte_offset, |
| 272 | size_t* out_bit_offset) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 273 | RTC_CHECK(out_byte_offset != nullptr); |
| 274 | RTC_CHECK(out_bit_offset != nullptr); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 275 | *out_byte_offset = byte_offset_; |
| 276 | *out_bit_offset = bit_offset_; |
| 277 | } |
| 278 | |
| 279 | bool BitBuffer::Seek(size_t byte_offset, size_t bit_offset) { |
| 280 | if (byte_offset > byte_count_ || bit_offset > 7 || |
| 281 | (byte_offset == byte_count_ && bit_offset > 0)) { |
| 282 | return false; |
| 283 | } |
| 284 | byte_offset_ = byte_offset; |
| 285 | bit_offset_ = bit_offset; |
| 286 | return true; |
| 287 | } |
| 288 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 289 | BitBufferWriter::BitBufferWriter(uint8_t* bytes, size_t byte_count) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 290 | : BitBuffer(bytes, byte_count), writable_bytes_(bytes) {} |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 291 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 292 | bool BitBufferWriter::WriteUInt8(uint8_t val) { |
| 293 | return WriteBits(val, sizeof(uint8_t) * 8); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 296 | bool BitBufferWriter::WriteUInt16(uint16_t val) { |
| 297 | return WriteBits(val, sizeof(uint16_t) * 8); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 300 | bool BitBufferWriter::WriteUInt32(uint32_t val) { |
| 301 | return WriteBits(val, sizeof(uint32_t) * 8); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 304 | bool BitBufferWriter::WriteBits(uint64_t val, size_t bit_count) { |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 305 | if (bit_count > RemainingBitCount()) { |
| 306 | return false; |
| 307 | } |
| 308 | size_t total_bits = bit_count; |
| 309 | |
| 310 | // 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] | 311 | val <<= (sizeof(uint64_t) * 8 - bit_count); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 312 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 313 | uint8_t* bytes = writable_bytes_ + byte_offset_; |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 314 | |
| 315 | // The first byte is relatively special; the bit offset to write to may put us |
| 316 | // in the middle of the byte, and the total bit count to write may require we |
| 317 | // save the bits at the end of the byte. |
| 318 | size_t remaining_bits_in_current_byte = 8 - bit_offset_; |
| 319 | size_t bits_in_first_byte = |
| 320 | std::min(bit_count, remaining_bits_in_current_byte); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 321 | *bytes = WritePartialByte(HighestByte(val), bits_in_first_byte, *bytes, |
| 322 | bit_offset_); |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 323 | if (bit_count <= remaining_bits_in_current_byte) { |
| 324 | // Nothing left to write, so quit early. |
| 325 | return ConsumeBits(total_bits); |
| 326 | } |
| 327 | |
| 328 | // Subtract what we've written from the bit count, shift it off the value, and |
| 329 | // write the remaining full bytes. |
| 330 | val <<= bits_in_first_byte; |
| 331 | bytes++; |
| 332 | bit_count -= bits_in_first_byte; |
| 333 | while (bit_count >= 8) { |
| 334 | *bytes++ = HighestByte(val); |
| 335 | val <<= 8; |
| 336 | bit_count -= 8; |
| 337 | } |
| 338 | |
| 339 | // Last byte may also be partial, so write the remaining bits from the top of |
| 340 | // val. |
| 341 | if (bit_count > 0) { |
| 342 | *bytes = WritePartialByte(HighestByte(val), bit_count, *bytes, 0); |
| 343 | } |
| 344 | |
| 345 | // All done! Consume the bits we've written. |
| 346 | return ConsumeBits(total_bits); |
| 347 | } |
| 348 | |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 349 | bool BitBufferWriter::WriteNonSymmetric(uint32_t val, uint32_t num_values) { |
| 350 | RTC_DCHECK_LT(val, num_values); |
| 351 | RTC_DCHECK_LE(num_values, uint32_t{1} << 31); |
Danil Chapovalov | a9e1b49 | 2020-07-08 11:24:19 +0200 | [diff] [blame] | 352 | if (num_values == 1) { |
| 353 | // When there is only one possible value, it requires zero bits to store it. |
| 354 | // But WriteBits doesn't support writing zero bits. |
| 355 | return true; |
| 356 | } |
Danil Chapovalov | a2b30d8 | 2019-07-03 14:41:50 +0200 | [diff] [blame] | 357 | size_t count_bits = CountBits(num_values); |
| 358 | uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values; |
| 359 | |
| 360 | return val < num_min_bits_values |
| 361 | ? WriteBits(val, count_bits - 1) |
| 362 | : WriteBits(val + num_min_bits_values, count_bits); |
| 363 | } |
| 364 | |
| 365 | size_t BitBufferWriter::SizeNonSymmetricBits(uint32_t val, |
| 366 | uint32_t num_values) { |
| 367 | RTC_DCHECK_LT(val, num_values); |
| 368 | RTC_DCHECK_LE(num_values, uint32_t{1} << 31); |
| 369 | size_t count_bits = CountBits(num_values); |
| 370 | uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values; |
| 371 | |
| 372 | return val < num_min_bits_values ? (count_bits - 1) : count_bits; |
| 373 | } |
| 374 | |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 375 | bool BitBufferWriter::WriteExponentialGolomb(uint32_t val) { |
| 376 | // 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] | 377 | // when encoded, so don't support writing it either. |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 378 | if (val == std::numeric_limits<uint32_t>::max()) { |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 379 | return false; |
| 380 | } |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 381 | uint64_t val_to_encode = static_cast<uint64_t>(val) + 1; |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 382 | |
| 383 | // We need to write CountBits(val+1) 0s and then val+1. Since val (as a |
Noah Richards | 9b9f1c4 | 2015-05-12 12:20:47 -0700 | [diff] [blame] | 384 | // uint64_t) has leading zeros, we can just write the total golomb encoded |
| 385 | // size worth of bits, knowing the value will appear last. |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame] | 386 | return WriteBits(val_to_encode, CountBits(val_to_encode) * 2 - 1); |
| 387 | } |
| 388 | |
sprang | 52033d6 | 2016-06-02 02:43:32 -0700 | [diff] [blame] | 389 | bool BitBufferWriter::WriteSignedExponentialGolomb(int32_t val) { |
| 390 | if (val == 0) { |
| 391 | return WriteExponentialGolomb(0); |
| 392 | } else if (val > 0) { |
| 393 | uint32_t signed_val = val; |
| 394 | return WriteExponentialGolomb((signed_val * 2) - 1); |
| 395 | } else { |
| 396 | if (val == std::numeric_limits<int32_t>::min()) |
| 397 | return false; // Not supported, would cause overflow. |
| 398 | uint32_t signed_val = -val; |
| 399 | return WriteExponentialGolomb(signed_val * 2); |
| 400 | } |
| 401 | } |
| 402 | |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 403 | } // namespace rtc |