| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/base/bitbuffer.h" | 11 #include "webrtc/base/bitbuffer.h" |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <limits> | 14 #include <limits> |
| 15 | 15 |
| 16 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // Returns the lowest (right-most) |bit_count| bits in |byte|. | 20 // Returns the lowest (right-most) |bit_count| bits in |byte|. |
| 21 uint8_t LowestBits(uint8_t byte, size_t bit_count) { | 21 uint8_t LowestBits(uint8_t byte, size_t bit_count) { |
| 22 DCHECK_LE(bit_count, 8u); | 22 RTC_DCHECK_LE(bit_count, 8u); |
| 23 return byte & ((1 << bit_count) - 1); | 23 return byte & ((1 << bit_count) - 1); |
| 24 } | 24 } |
| 25 | 25 |
| 26 // Returns the highest (left-most) |bit_count| bits in |byte|, shifted to the | 26 // Returns the highest (left-most) |bit_count| bits in |byte|, shifted to the |
| 27 // lowest bits (to the right). | 27 // lowest bits (to the right). |
| 28 uint8_t HighestBits(uint8_t byte, size_t bit_count) { | 28 uint8_t HighestBits(uint8_t byte, size_t bit_count) { |
| 29 DCHECK_LE(bit_count, 8u); | 29 RTC_DCHECK_LE(bit_count, 8u); |
| 30 uint8_t shift = 8 - static_cast<uint8_t>(bit_count); | 30 uint8_t shift = 8 - static_cast<uint8_t>(bit_count); |
| 31 uint8_t mask = 0xFF << shift; | 31 uint8_t mask = 0xFF << shift; |
| 32 return (byte & mask) >> shift; | 32 return (byte & mask) >> shift; |
| 33 } | 33 } |
| 34 | 34 |
| 35 // Returns the highest byte of |val| in a uint8_t. | 35 // Returns the highest byte of |val| in a uint8_t. |
| 36 uint8_t HighestByte(uint64_t val) { | 36 uint8_t HighestByte(uint64_t val) { |
| 37 return static_cast<uint8_t>(val >> 56); | 37 return static_cast<uint8_t>(val >> 56); |
| 38 } | 38 } |
| 39 | 39 |
| 40 // Returns the result of writing partial data from |source|, of | 40 // Returns the result of writing partial data from |source|, of |
| 41 // |source_bit_count| size in the highest bits, to |target| at | 41 // |source_bit_count| size in the highest bits, to |target| at |
| 42 // |target_bit_offset| from the highest bit. | 42 // |target_bit_offset| from the highest bit. |
| 43 uint8_t WritePartialByte(uint8_t source, | 43 uint8_t WritePartialByte(uint8_t source, |
| 44 size_t source_bit_count, | 44 size_t source_bit_count, |
| 45 uint8_t target, | 45 uint8_t target, |
| 46 size_t target_bit_offset) { | 46 size_t target_bit_offset) { |
| 47 DCHECK(target_bit_offset < 8); | 47 RTC_DCHECK(target_bit_offset < 8); |
| 48 DCHECK(source_bit_count < 9); | 48 RTC_DCHECK(source_bit_count < 9); |
| 49 DCHECK(source_bit_count <= (8 - target_bit_offset)); | 49 RTC_DCHECK(source_bit_count <= (8 - target_bit_offset)); |
| 50 // Generate a mask for just the bits we're going to overwrite, so: | 50 // Generate a mask for just the bits we're going to overwrite, so: |
| 51 uint8_t mask = | 51 uint8_t mask = |
| 52 // The number of bits we want, in the most significant bits... | 52 // The number of bits we want, in the most significant bits... |
| 53 static_cast<uint8_t>(0xFF << (8 - source_bit_count)) | 53 static_cast<uint8_t>(0xFF << (8 - source_bit_count)) |
| 54 // ...shifted over to the target offset from the most signficant bit. | 54 // ...shifted over to the target offset from the most signficant bit. |
| 55 >> target_bit_offset; | 55 >> target_bit_offset; |
| 56 | 56 |
| 57 // We want the target, with the bits we'll overwrite masked off, or'ed with | 57 // We want the target, with the bits we'll overwrite masked off, or'ed with |
| 58 // the bits from the source we want. | 58 // the bits from the source we want. |
| 59 return (target & ~mask) | (source >> target_bit_offset); | 59 return (target & ~mask) | (source >> target_bit_offset); |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Counts the number of bits used in the binary representation of val. | 62 // Counts the number of bits used in the binary representation of val. |
| 63 size_t CountBits(uint64_t val) { | 63 size_t CountBits(uint64_t val) { |
| 64 size_t bit_count = 0; | 64 size_t bit_count = 0; |
| 65 while (val != 0) { | 65 while (val != 0) { |
| 66 bit_count++; | 66 bit_count++; |
| 67 val >>= 1; | 67 val >>= 1; |
| 68 } | 68 } |
| 69 return bit_count; | 69 return bit_count; |
| 70 } | 70 } |
| 71 | 71 |
| 72 } // namespace | 72 } // namespace |
| 73 | 73 |
| 74 namespace rtc { | 74 namespace rtc { |
| 75 | 75 |
| 76 BitBuffer::BitBuffer(const uint8_t* bytes, size_t byte_count) | 76 BitBuffer::BitBuffer(const uint8_t* bytes, size_t byte_count) |
| 77 : bytes_(bytes), byte_count_(byte_count), byte_offset_(), bit_offset_() { | 77 : bytes_(bytes), byte_count_(byte_count), byte_offset_(), bit_offset_() { |
| 78 DCHECK(static_cast<uint64_t>(byte_count_) <= | 78 RTC_DCHECK(static_cast<uint64_t>(byte_count_) <= |
| 79 std::numeric_limits<uint32_t>::max()); | 79 std::numeric_limits<uint32_t>::max()); |
| 80 } | 80 } |
| 81 | 81 |
| 82 uint64_t BitBuffer::RemainingBitCount() const { | 82 uint64_t BitBuffer::RemainingBitCount() const { |
| 83 return (static_cast<uint64_t>(byte_count_) - byte_offset_) * 8 - bit_offset_; | 83 return (static_cast<uint64_t>(byte_count_) - byte_offset_) * 8 - bit_offset_; |
| 84 } | 84 } |
| 85 | 85 |
| 86 bool BitBuffer::ReadUInt8(uint8_t* val) { | 86 bool BitBuffer::ReadUInt8(uint8_t* val) { |
| 87 uint32_t bit_val; | 87 uint32_t bit_val; |
| 88 if (!ReadBits(&bit_val, sizeof(uint8_t) * 8)) { | 88 if (!ReadBits(&bit_val, sizeof(uint8_t) * 8)) { |
| 89 return false; | 89 return false; |
| 90 } | 90 } |
| 91 DCHECK(bit_val <= std::numeric_limits<uint8_t>::max()); | 91 RTC_DCHECK(bit_val <= std::numeric_limits<uint8_t>::max()); |
| 92 *val = static_cast<uint8_t>(bit_val); | 92 *val = static_cast<uint8_t>(bit_val); |
| 93 return true; | 93 return true; |
| 94 } | 94 } |
| 95 | 95 |
| 96 bool BitBuffer::ReadUInt16(uint16_t* val) { | 96 bool BitBuffer::ReadUInt16(uint16_t* val) { |
| 97 uint32_t bit_val; | 97 uint32_t bit_val; |
| 98 if (!ReadBits(&bit_val, sizeof(uint16_t) * 8)) { | 98 if (!ReadBits(&bit_val, sizeof(uint16_t) * 8)) { |
| 99 return false; | 99 return false; |
| 100 } | 100 } |
| 101 DCHECK(bit_val <= std::numeric_limits<uint16_t>::max()); | 101 RTC_DCHECK(bit_val <= std::numeric_limits<uint16_t>::max()); |
| 102 *val = static_cast<uint16_t>(bit_val); | 102 *val = static_cast<uint16_t>(bit_val); |
| 103 return true; | 103 return true; |
| 104 } | 104 } |
| 105 | 105 |
| 106 bool BitBuffer::ReadUInt32(uint32_t* val) { | 106 bool BitBuffer::ReadUInt32(uint32_t* val) { |
| 107 return ReadBits(val, sizeof(uint32_t) * 8); | 107 return ReadBits(val, sizeof(uint32_t) * 8); |
| 108 } | 108 } |
| 109 | 109 |
| 110 bool BitBuffer::PeekBits(uint32_t* val, size_t bit_count) { | 110 bool BitBuffer::PeekBits(uint32_t* val, size_t bit_count) { |
| 111 if (!val || bit_count > RemainingBitCount() || bit_count > 32) { | 111 if (!val || bit_count > RemainingBitCount() || bit_count > 32) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 | 166 |
| 167 // Count the number of leading 0 bits by peeking/consuming them one at a time. | 167 // Count the number of leading 0 bits by peeking/consuming them one at a time. |
| 168 size_t zero_bit_count = 0; | 168 size_t zero_bit_count = 0; |
| 169 uint32_t peeked_bit; | 169 uint32_t peeked_bit; |
| 170 while (PeekBits(&peeked_bit, 1) && peeked_bit == 0) { | 170 while (PeekBits(&peeked_bit, 1) && peeked_bit == 0) { |
| 171 zero_bit_count++; | 171 zero_bit_count++; |
| 172 ConsumeBits(1); | 172 ConsumeBits(1); |
| 173 } | 173 } |
| 174 | 174 |
| 175 // We should either be at the end of the stream, or the next bit should be 1. | 175 // We should either be at the end of the stream, or the next bit should be 1. |
| 176 DCHECK(!PeekBits(&peeked_bit, 1) || peeked_bit == 1); | 176 RTC_DCHECK(!PeekBits(&peeked_bit, 1) || peeked_bit == 1); |
| 177 | 177 |
| 178 // The bit count of the value is the number of zeros + 1. Make sure that many | 178 // The bit count of the value is the number of zeros + 1. Make sure that many |
| 179 // bits fits in a uint32_t and that we have enough bits left for it, and then | 179 // bits fits in a uint32_t and that we have enough bits left for it, and then |
| 180 // read the value. | 180 // read the value. |
| 181 size_t value_bit_count = zero_bit_count + 1; | 181 size_t value_bit_count = zero_bit_count + 1; |
| 182 if (value_bit_count > 32 || !ReadBits(val, value_bit_count)) { | 182 if (value_bit_count > 32 || !ReadBits(val, value_bit_count)) { |
| 183 CHECK(Seek(original_byte_offset, original_bit_offset)); | 183 RTC_CHECK(Seek(original_byte_offset, original_bit_offset)); |
| 184 return false; | 184 return false; |
| 185 } | 185 } |
| 186 *val -= 1; | 186 *val -= 1; |
| 187 return true; | 187 return true; |
| 188 } | 188 } |
| 189 | 189 |
| 190 void BitBuffer::GetCurrentOffset( | 190 void BitBuffer::GetCurrentOffset( |
| 191 size_t* out_byte_offset, size_t* out_bit_offset) { | 191 size_t* out_byte_offset, size_t* out_bit_offset) { |
| 192 CHECK(out_byte_offset != NULL); | 192 RTC_CHECK(out_byte_offset != NULL); |
| 193 CHECK(out_bit_offset != NULL); | 193 RTC_CHECK(out_bit_offset != NULL); |
| 194 *out_byte_offset = byte_offset_; | 194 *out_byte_offset = byte_offset_; |
| 195 *out_bit_offset = bit_offset_; | 195 *out_bit_offset = bit_offset_; |
| 196 } | 196 } |
| 197 | 197 |
| 198 bool BitBuffer::Seek(size_t byte_offset, size_t bit_offset) { | 198 bool BitBuffer::Seek(size_t byte_offset, size_t bit_offset) { |
| 199 if (byte_offset > byte_count_ || bit_offset > 7 || | 199 if (byte_offset > byte_count_ || bit_offset > 7 || |
| 200 (byte_offset == byte_count_ && bit_offset > 0)) { | 200 (byte_offset == byte_count_ && bit_offset > 0)) { |
| 201 return false; | 201 return false; |
| 202 } | 202 } |
| 203 byte_offset_ = byte_offset; | 203 byte_offset_ = byte_offset; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 } | 274 } |
| 275 uint64_t val_to_encode = static_cast<uint64_t>(val) + 1; | 275 uint64_t val_to_encode = static_cast<uint64_t>(val) + 1; |
| 276 | 276 |
| 277 // We need to write CountBits(val+1) 0s and then val+1. Since val (as a | 277 // We need to write CountBits(val+1) 0s and then val+1. Since val (as a |
| 278 // uint64_t) has leading zeros, we can just write the total golomb encoded | 278 // uint64_t) has leading zeros, we can just write the total golomb encoded |
| 279 // size worth of bits, knowing the value will appear last. | 279 // size worth of bits, knowing the value will appear last. |
| 280 return WriteBits(val_to_encode, CountBits(val_to_encode) * 2 - 1); | 280 return WriteBits(val_to_encode, CountBits(val_to_encode) * 2 - 1); |
| 281 } | 281 } |
| 282 | 282 |
| 283 } // namespace rtc | 283 } // namespace rtc |
| OLD | NEW |