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 | |
| 11 | #include "webrtc/base/bitbuffer.h" |
| 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 | |
| 16 | #include "webrtc/base/checks.h" |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | // Returns the lowest (right-most) |bit_count| bits in |byte|. |
| 21 | uint8 LowestBits(uint8 byte, size_t bit_count) { |
| 22 | DCHECK_LE(bit_count, 8u); |
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 | |
| 26 | // Returns the highest (left-most) |bit_count| bits in |byte|, shifted to the |
| 27 | // lowest bits (to the right). |
| 28 | uint8 HighestBits(uint8 byte, size_t bit_count) { |
| 29 | DCHECK_LE(bit_count, 8u); |
| 30 | uint8 shift = 8 - static_cast<uint8>(bit_count); |
| 31 | uint8 mask = 0xFF << shift; |
| 32 | return (byte & mask) >> shift; |
| 33 | } |
| 34 | |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame^] | 35 | // Returns the highest byte of |val| in a uint8. |
| 36 | uint8 HighestByte(uint64 val) { |
| 37 | return static_cast<uint8>(val >> 56); |
| 38 | } |
| 39 | |
| 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. |
| 43 | uint8 WritePartialByte(uint8 source, size_t source_bit_count, |
| 44 | uint8 target, size_t target_bit_offset) { |
| 45 | DCHECK(target_bit_offset < 8); |
| 46 | DCHECK(source_bit_count < 9); |
| 47 | DCHECK(source_bit_count <= (8 - target_bit_offset)); |
| 48 | // Generate a mask for just the bits we're going to overwrite, so: |
| 49 | uint8 mask = |
| 50 | // The number of bits we want, in the most significant bits... |
| 51 | static_cast<uint8>(0xFF << (8 - source_bit_count)) |
| 52 | // ...shifted over to the target offset from the most signficant bit. |
| 53 | >> target_bit_offset; |
| 54 | |
| 55 | // We want the target, with the bits we'll overwrite masked off, or'ed with |
| 56 | // the bits from the source we want. |
| 57 | return (target & ~mask) | (source >> target_bit_offset); |
| 58 | } |
| 59 | |
| 60 | // Counts the number of bits used in the binary representation of val. |
| 61 | size_t CountBits(uint64 val) { |
| 62 | size_t bit_count = 0; |
| 63 | while (val != 0) { |
| 64 | bit_count++; |
| 65 | val >>= 1; |
| 66 | } |
| 67 | return bit_count; |
| 68 | } |
| 69 | |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 70 | } // namespace |
| 71 | |
| 72 | namespace rtc { |
| 73 | |
| 74 | BitBuffer::BitBuffer(const uint8* bytes, size_t byte_count) |
| 75 | : bytes_(bytes), byte_count_(byte_count), byte_offset_(), bit_offset_() { |
| 76 | DCHECK(static_cast<uint64>(byte_count_) <= |
| 77 | std::numeric_limits<uint32>::max()); |
| 78 | } |
| 79 | |
| 80 | uint64 BitBuffer::RemainingBitCount() const { |
| 81 | return (static_cast<uint64>(byte_count_) - byte_offset_) * 8 - bit_offset_; |
| 82 | } |
| 83 | |
| 84 | bool BitBuffer::ReadUInt8(uint8* val) { |
| 85 | uint32 bit_val; |
| 86 | if (!ReadBits(&bit_val, sizeof(uint8) * 8)) { |
| 87 | return false; |
| 88 | } |
| 89 | DCHECK(bit_val <= std::numeric_limits<uint8>::max()); |
| 90 | *val = static_cast<uint8>(bit_val); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | bool BitBuffer::ReadUInt16(uint16* val) { |
| 95 | uint32 bit_val; |
| 96 | if (!ReadBits(&bit_val, sizeof(uint16) * 8)) { |
| 97 | return false; |
| 98 | } |
| 99 | DCHECK(bit_val <= std::numeric_limits<uint16>::max()); |
| 100 | *val = static_cast<uint16>(bit_val); |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | bool BitBuffer::ReadUInt32(uint32* val) { |
| 105 | return ReadBits(val, sizeof(uint32) * 8); |
| 106 | } |
| 107 | |
| 108 | bool BitBuffer::PeekBits(uint32* val, size_t bit_count) { |
| 109 | if (!val || bit_count > RemainingBitCount() || bit_count > 32) { |
| 110 | return false; |
| 111 | } |
| 112 | const uint8* bytes = bytes_ + byte_offset_; |
| 113 | size_t remaining_bits_in_current_byte = 8 - bit_offset_; |
| 114 | uint32 bits = LowestBits(*bytes++, remaining_bits_in_current_byte); |
| 115 | // If we're reading fewer bits than what's left in the current byte, just |
| 116 | // return the portion of this byte that we need. |
| 117 | if (bit_count < remaining_bits_in_current_byte) { |
| 118 | *val = HighestBits(bits, bit_offset_ + bit_count); |
| 119 | return true; |
| 120 | } |
| 121 | // Otherwise, subtract what we've read from the bit count and read as many |
| 122 | // full bytes as we can into bits. |
| 123 | bit_count -= remaining_bits_in_current_byte; |
| 124 | while (bit_count >= 8) { |
| 125 | bits = (bits << 8) | *bytes++; |
| 126 | bit_count -= 8; |
| 127 | } |
| 128 | // Whatever we have left is smaller than a byte, so grab just the bits we need |
| 129 | // and shift them into the lowest bits. |
| 130 | if (bit_count > 0) { |
| 131 | bits <<= bit_count; |
| 132 | bits |= HighestBits(*bytes, bit_count); |
| 133 | } |
| 134 | *val = bits; |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | bool BitBuffer::ReadBits(uint32* val, size_t bit_count) { |
| 139 | return PeekBits(val, bit_count) && ConsumeBits(bit_count); |
| 140 | } |
| 141 | |
| 142 | bool BitBuffer::ConsumeBytes(size_t byte_count) { |
| 143 | return ConsumeBits(byte_count * 8); |
| 144 | } |
| 145 | |
| 146 | bool BitBuffer::ConsumeBits(size_t bit_count) { |
| 147 | if (bit_count > RemainingBitCount()) { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | byte_offset_ += (bit_offset_ + bit_count) / 8; |
| 152 | bit_offset_ = (bit_offset_ + bit_count) % 8; |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | bool BitBuffer::ReadExponentialGolomb(uint32* val) { |
| 157 | if (!val) { |
| 158 | return false; |
| 159 | } |
| 160 | // Store off the current byte/bit offset, in case we want to restore them due |
| 161 | // to a failed parse. |
| 162 | size_t original_byte_offset = byte_offset_; |
| 163 | size_t original_bit_offset = bit_offset_; |
| 164 | |
| 165 | // Count the number of leading 0 bits by peeking/consuming them one at a time. |
| 166 | size_t zero_bit_count = 0; |
| 167 | uint32 peeked_bit; |
| 168 | while (PeekBits(&peeked_bit, 1) && peeked_bit == 0) { |
| 169 | zero_bit_count++; |
| 170 | ConsumeBits(1); |
| 171 | } |
| 172 | |
| 173 | // We should either be at the end of the stream, or the next bit should be 1. |
| 174 | DCHECK(!PeekBits(&peeked_bit, 1) || peeked_bit == 1); |
| 175 | |
| 176 | // The bit count of the value is the number of zeros + 1. Make sure that many |
| 177 | // bits fits in a uint32 and that we have enough bits left for it, and then |
| 178 | // read the value. |
| 179 | size_t value_bit_count = zero_bit_count + 1; |
| 180 | if (value_bit_count > 32 || !ReadBits(val, value_bit_count)) { |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame^] | 181 | CHECK(Seek(original_byte_offset, original_bit_offset)); |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | *val -= 1; |
| 185 | return true; |
| 186 | } |
| 187 | |
Noah Richards | 86153c2 | 2015-04-28 15:13:44 -0700 | [diff] [blame^] | 188 | void BitBuffer::GetCurrentOffset( |
| 189 | size_t* out_byte_offset, size_t* out_bit_offset) { |
| 190 | CHECK(out_byte_offset != NULL); |
| 191 | CHECK(out_bit_offset != NULL); |
| 192 | *out_byte_offset = byte_offset_; |
| 193 | *out_bit_offset = bit_offset_; |
| 194 | } |
| 195 | |
| 196 | bool BitBuffer::Seek(size_t byte_offset, size_t bit_offset) { |
| 197 | if (byte_offset > byte_count_ || bit_offset > 7 || |
| 198 | (byte_offset == byte_count_ && bit_offset > 0)) { |
| 199 | return false; |
| 200 | } |
| 201 | byte_offset_ = byte_offset; |
| 202 | bit_offset_ = bit_offset; |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | BitBufferWriter::BitBufferWriter(uint8* bytes, size_t byte_count) |
| 207 | : BitBuffer(bytes, byte_count), writable_bytes_(bytes) { |
| 208 | } |
| 209 | |
| 210 | bool BitBufferWriter::WriteUInt8(uint8 val) { |
| 211 | return WriteBits(val, sizeof(uint8) * 8); |
| 212 | } |
| 213 | |
| 214 | bool BitBufferWriter::WriteUInt16(uint16 val) { |
| 215 | return WriteBits(val, sizeof(uint16) * 8); |
| 216 | } |
| 217 | |
| 218 | bool BitBufferWriter::WriteUInt32(uint32 val) { |
| 219 | return WriteBits(val, sizeof(uint32) * 8); |
| 220 | } |
| 221 | |
| 222 | bool BitBufferWriter::WriteBits(uint64 val, size_t bit_count) { |
| 223 | if (bit_count > RemainingBitCount()) { |
| 224 | return false; |
| 225 | } |
| 226 | size_t total_bits = bit_count; |
| 227 | |
| 228 | // For simplicity, push the bits we want to read from val to the highest bits. |
| 229 | val <<= (sizeof(uint64) * 8 - bit_count); |
| 230 | |
| 231 | uint8* bytes = writable_bytes_ + byte_offset_; |
| 232 | |
| 233 | // The first byte is relatively special; the bit offset to write to may put us |
| 234 | // in the middle of the byte, and the total bit count to write may require we |
| 235 | // save the bits at the end of the byte. |
| 236 | size_t remaining_bits_in_current_byte = 8 - bit_offset_; |
| 237 | size_t bits_in_first_byte = |
| 238 | std::min(bit_count, remaining_bits_in_current_byte); |
| 239 | *bytes = WritePartialByte( |
| 240 | HighestByte(val), bits_in_first_byte, *bytes, bit_offset_); |
| 241 | if (bit_count <= remaining_bits_in_current_byte) { |
| 242 | // Nothing left to write, so quit early. |
| 243 | return ConsumeBits(total_bits); |
| 244 | } |
| 245 | |
| 246 | // Subtract what we've written from the bit count, shift it off the value, and |
| 247 | // write the remaining full bytes. |
| 248 | val <<= bits_in_first_byte; |
| 249 | bytes++; |
| 250 | bit_count -= bits_in_first_byte; |
| 251 | while (bit_count >= 8) { |
| 252 | *bytes++ = HighestByte(val); |
| 253 | val <<= 8; |
| 254 | bit_count -= 8; |
| 255 | } |
| 256 | |
| 257 | // Last byte may also be partial, so write the remaining bits from the top of |
| 258 | // val. |
| 259 | if (bit_count > 0) { |
| 260 | *bytes = WritePartialByte(HighestByte(val), bit_count, *bytes, 0); |
| 261 | } |
| 262 | |
| 263 | // All done! Consume the bits we've written. |
| 264 | return ConsumeBits(total_bits); |
| 265 | } |
| 266 | |
| 267 | bool BitBufferWriter::WriteExponentialGolomb(uint32 val) { |
| 268 | // We don't support reading UINT32_MAX, because it doesn't fit in a uint32 |
| 269 | // when encoded, so don't support writing it either. |
| 270 | if (val == std::numeric_limits<uint32>::max()) { |
| 271 | return false; |
| 272 | } |
| 273 | uint64 val_to_encode = static_cast<uint64>(val) + 1; |
| 274 | |
| 275 | // We need to write CountBits(val+1) 0s and then val+1. Since val (as a |
| 276 | // uint64) has leading zeros, we can just write the total golomb encoded size |
| 277 | // worth of bits, knowing the value will appear last. |
| 278 | return WriteBits(val_to_encode, CountBits(val_to_encode) * 2 - 1); |
| 279 | } |
| 280 | |
Noah Richards | bbf7c86 | 2015-04-21 16:30:13 -0700 | [diff] [blame] | 281 | } // namespace rtc |