Chromium Code Reviews| Index: media/base/bit_reader_base.cc |
| diff --git a/media/base/bit_reader_base.cc b/media/base/bit_reader_base.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..852a85af821c3c30723d351e24f4c69043ea1759 |
| --- /dev/null |
| +++ b/media/base/bit_reader_base.cc |
| @@ -0,0 +1,161 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/base/bit_reader_base.h" |
| + |
| +#include <base/sys_byteorder.h> |
| + |
| +namespace media { |
| + |
| +BitReaderBase::BitReaderBase() |
| + : nbits_(0), |
| + reg_(0), |
| + nbits_next_(0), |
| + reg_next_(0) { |
| +} |
| + |
| +BitReaderBase::~BitReaderBase() { |
| +} |
| + |
| +bool BitReaderBase::ReadFlag(bool* flag) { |
| + if (nbits_ == 0) { |
| + if (!Refill(1)) |
|
damienv1
2013/12/19 03:29:12
Could be only one condition.
damienv1
2013/12/19 21:46:34
Done.
|
| + return false; |
| + } |
| + |
| + *flag = (static_cast<int64>(reg_) < 0); |
| + reg_ <<= 1; |
| + nbits_--; |
| + return true; |
| + |
| +} |
| + |
| +bool BitReaderBase::ReadUE(uint32* out) { |
| + // Get the number of leading zeros. |
| + int zero_count = -1; |
| + bool is_one; |
| + do { |
| + if (nbits_ == 0 && !Refill(1)) |
| + return false; |
| + is_one = (static_cast<int64>(reg_) < 0); |
| + reg_ <<= 1; |
| + nbits_--; |
|
damienv1
2013/12/19 03:46:51
zero_count++;
damienv1
2013/12/19 21:46:34
Done.
|
| + } while(!is_one); |
| + |
| + // If zero_count is greater than 31, the calculated value will overflow. |
| + if (zero_count > 31) |
| + return false; |
| + |
| + // Read the actual value. |
| + uint32 base = (1 << zero_count) - 1; |
| + uint32 offset; |
| + if (!ReadBits(zero_count, &offset)) |
| + return false; |
| + *out = base + offset; |
| + |
| + return true; |
| +} |
| + |
| +bool BitReaderBase::SkipBits(int num_bits) { |
| + DCHECK_GE(num_bits, 0); |
| + DVLOG_IF(0, num_bits > 100) |
| + << "BitReader::SkipBits inefficient for large skips"; |
| + |
| + uint64 dummy; |
| + while (num_bits >= 32) { |
| + if (!ReadBitsInternal(32, &dummy)) |
| + return false; |
| + num_bits -= 32; |
| + } |
| + |
| + if (num_bits > 0) |
| + return ReadBitsInternal(num_bits, &dummy); |
| + |
| + return true; |
| +} |
| + |
| +int BitReaderBase::bits_available_in_registers() const { |
| + return nbits_ + nbits_next_; |
| +} |
| + |
| +bool BitReaderBase::ReadBitsInternal(int num_bits, uint64* out) { |
| + DCHECK_GE(num_bits, 0); |
| + |
| + if (num_bits == 0) { |
| + *out = 0; |
| + return true; |
| + } |
| + |
| + if (num_bits > nbits_) { |
| + if (!Refill(num_bits)) { |
|
damienv1
2013/12/19 03:29:12
Could be only one condition.
damienv1
2013/12/19 21:46:34
Done.
|
| + ResetRegisters(); |
| + return false; |
| + } |
| + } |
| + |
| + *out = reg_ >> (sizeof(RegType) * CHAR_BIT - num_bits); |
| + reg_ <<= num_bits; |
| + nbits_ -= num_bits; |
| + return true; |
| +} |
| + |
| +bool BitReaderBase::Refill(int min_nbits) { |
| + DCHECK_LE(min_nbits, static_cast<int>(sizeof(RegType) * CHAR_BIT)); |
| + |
| + // Transfer from the next to the current register. |
| + RefillCurrentRegister(); |
| + if (min_nbits <= nbits_) |
| + return true; |
| + DCHECK_EQ(nbits_next_, 0); |
| + |
| + // Strict minimum number of bytes to refill. |
| + int min_nbytes = (min_nbits - nbits_ + CHAR_BIT - 1) / CHAR_BIT; |
| + |
| + // Max number of bytes to refill. |
| + int max_nbytes = 8; |
|
damienv1
2013/12/19 02:01:25
Should be sizeof(RegType)
damienv1
2013/12/19 21:46:34
Done.
|
| + |
| + // Refill. |
| + const uint8* byte_stream_window; |
| + int window_size = GetBytes(min_nbytes, max_nbytes, &byte_stream_window); |
| + if (window_size < min_nbytes) |
| + return false; |
| + |
| + reg_next_ = 0; |
| + memcpy(®_next_, byte_stream_window, window_size); |
| + reg_next_ = base::ByteSwap(reg_next_); |
|
damienv1
2013/12/19 03:29:12
Should be NetToHost64 !
damienv1
2013/12/19 21:46:34
Done.
|
| + nbits_next_ = window_size * CHAR_BIT; |
| + |
| + // Transfer from the next to the current register. |
| + RefillCurrentRegister(); |
| + DCHECK_LE(min_nbits, nbits_); |
| + |
| + return true; |
| +} |
| + |
| +void BitReaderBase::RefillCurrentRegister() { |
| + if (nbits_ == 64 || nbits_next_ == 0) |
|
damienv1
2013/12/19 02:01:25
Should be sizeof(RegType) * CHAR_BIT
damienv1
2013/12/19 21:46:34
Done.
|
| + return; |
| + |
| + reg_ |= (reg_next_ >> nbits_); |
| + |
| + int free_nbits = static_cast<int>(sizeof(RegType) * CHAR_BIT) - nbits_; |
| + if (free_nbits >= nbits_next_) { |
| + nbits_ += nbits_next_; |
| + reg_next_ = 0; |
| + nbits_next_ = 0; |
| + } else { |
| + nbits_ += free_nbits; |
| + reg_next_ <<= free_nbits; |
| + nbits_next_ -= free_nbits; |
| + } |
| +} |
| + |
| +void BitReaderBase::ResetRegisters() { |
| + reg_ = 0; |
| + nbits_ = 0; |
| + reg_next_ = 0; |
| + nbits_next_ = 0; |
| +} |
| + |
| +} // namespace media |