| Index: media/base/bit_reader_core.cc
|
| diff --git a/media/base/bit_reader_core.cc b/media/base/bit_reader_core.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..99d2fe100515650b938657e9cc5412f35147ab73
|
| --- /dev/null
|
| +++ b/media/base/bit_reader_core.cc
|
| @@ -0,0 +1,193 @@
|
| +// 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_core.h"
|
| +
|
| +#include <base/sys_byteorder.h>
|
| +
|
| +namespace media {
|
| +
|
| +BitReaderCore::ByteStreamProvider::ByteStreamProvider() {
|
| +}
|
| +
|
| +BitReaderCore::ByteStreamProvider::~ByteStreamProvider() {
|
| +}
|
| +
|
| +BitReaderCore::BitReaderCore(ByteStreamProvider* byte_stream_provider)
|
| + : byte_stream_provider_(byte_stream_provider),
|
| + bit_count_(0),
|
| + nbits_(0),
|
| + reg_(0),
|
| + nbits_next_(0),
|
| + reg_next_(0) {
|
| +}
|
| +
|
| +BitReaderCore::~BitReaderCore() {
|
| +}
|
| +
|
| +bool BitReaderCore::ReadFlag(bool* flag) {
|
| + if (nbits_ == 0 && !Refill(1))
|
| + return false;
|
| +
|
| + *flag = (static_cast<SignedRegType>(reg_) < 0);
|
| + reg_ <<= 1;
|
| + nbits_--;
|
| + bit_count_++;
|
| + return true;
|
| +}
|
| +
|
| +bool BitReaderCore::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<SignedRegType>(reg_) < 0);
|
| + reg_ <<= 1;
|
| + nbits_--;
|
| + zero_count++;
|
| + } while(!is_one);
|
| + bit_count_ += (zero_count + 1);
|
| +
|
| + // 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 BitReaderCore::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;
|
| + }
|
| + return ReadBitsInternal(num_bits, &dummy);
|
| +}
|
| +
|
| +int BitReaderCore::GetBitCount() const {
|
| + return bit_count_;
|
| +}
|
| +
|
| +int BitReaderCore::bits_available() const {
|
| + return nbits_ + nbits_next_;
|
| +}
|
| +
|
| +bool BitReaderCore::HasMoreRBSPData() {
|
| + // Try to have at least 9 bits (strictly more than 1 byte).
|
| + if (nbits_ < 9)
|
| + Refill(9);
|
| +
|
| + // Not on last byte.
|
| + if (nbits_ > 8)
|
| + return true;
|
| +
|
| + // Last byte, look for stop bit;
|
| + // We have more RBSP data if the last non-zero bit we find is not the
|
| + // first available bit.
|
| + return (reg_ << 1) != 0;
|
| +}
|
| +
|
| +bool BitReaderCore::ReadBitsInternal(int num_bits, uint64* out) {
|
| + DCHECK_GE(num_bits, 0);
|
| +
|
| + if (num_bits == 0) {
|
| + *out = 0;
|
| + return true;
|
| + }
|
| +
|
| + if (num_bits > nbits_ && !Refill(num_bits)) {
|
| + // Any subsequent ReadBits should fail:
|
| + // empty the current bit register for that purposer.
|
| + nbits_ = 0;
|
| + reg_ = 0;
|
| + return false;
|
| + }
|
| +
|
| + bit_count_ += num_bits;
|
| +
|
| + if (num_bits == sizeof(RegType) * CHAR_BIT) {
|
| + // Special case needed since for example for a 64 bit integer "a"
|
| + // "a << 64" is not defined by the C/C++ standard.
|
| + *out = reg_;
|
| + reg_ = 0;
|
| + nbits_ = 0;
|
| + return true;
|
| + }
|
| +
|
| + *out = reg_ >> (sizeof(RegType) * CHAR_BIT - num_bits);
|
| + reg_ <<= num_bits;
|
| + nbits_ -= num_bits;
|
| + return true;
|
| +}
|
| +
|
| +bool BitReaderCore::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);
|
| + DCHECK_EQ(reg_next_, 0u);
|
| +
|
| + // 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 = sizeof(RegType);
|
| +
|
| + // Refill.
|
| + const uint8* byte_stream_window;
|
| + int window_size =
|
| + byte_stream_provider_->GetBytes(
|
| + min_nbytes, max_nbytes, &byte_stream_window);
|
| + if (window_size == 0)
|
| + return false;
|
| +
|
| + reg_next_ = 0;
|
| + memcpy(®_next_, byte_stream_window, window_size);
|
| + reg_next_ = base::NetToHost64(reg_next_);
|
| + nbits_next_ = window_size * CHAR_BIT;
|
| +
|
| + // Transfer from the next to the current register.
|
| + RefillCurrentRegister();
|
| +
|
| + return (nbits_ >= min_nbits);
|
| +}
|
| +
|
| +void BitReaderCore::RefillCurrentRegister() {
|
| + // No refill possible if the destination register is full
|
| + // or the source register is empty.
|
| + if (nbits_ == sizeof(RegType) * CHAR_BIT || nbits_next_ == 0)
|
| + 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;
|
| + }
|
| +}
|
| +
|
| +} // namespace media
|
|
|