2013-09-24 01:35:40 +00:00
|
|
|
// Copyright (c) 2012 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.
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/bit_reader.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2015-03-21 02:18:38 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-09-24 01:35:40 +00:00
|
|
|
namespace media {
|
|
|
|
|
2016-11-09 02:11:13 +00:00
|
|
|
BitReader::BitReader(const uint8_t* data, size_t size)
|
2015-11-18 19:51:15 +00:00
|
|
|
: data_(data),
|
|
|
|
initial_size_(size),
|
|
|
|
bytes_left_(size),
|
|
|
|
num_remaining_bits_in_curr_byte_(0) {
|
2013-09-24 01:35:40 +00:00
|
|
|
DCHECK(data_ != NULL && bytes_left_ > 0);
|
|
|
|
|
|
|
|
UpdateCurrByte();
|
|
|
|
}
|
|
|
|
|
|
|
|
BitReader::~BitReader() {}
|
|
|
|
|
2016-11-09 02:11:13 +00:00
|
|
|
bool BitReader::SkipBits(size_t num_bits) {
|
2013-09-24 01:35:40 +00:00
|
|
|
// Skip any bits in the current byte waiting to be processed, then
|
|
|
|
// process full bytes until less than 8 bits remaining.
|
2015-01-27 23:31:24 +00:00
|
|
|
if (num_bits > num_remaining_bits_in_curr_byte_) {
|
2013-09-24 01:35:40 +00:00
|
|
|
num_bits -= num_remaining_bits_in_curr_byte_;
|
|
|
|
num_remaining_bits_in_curr_byte_ = 0;
|
2015-01-27 23:31:24 +00:00
|
|
|
|
2016-11-09 02:11:13 +00:00
|
|
|
size_t num_bytes = num_bits / 8;
|
2015-01-27 23:31:24 +00:00
|
|
|
num_bits %= 8;
|
|
|
|
if (bytes_left_ < num_bytes) {
|
|
|
|
bytes_left_ = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bytes_left_ -= num_bytes;
|
|
|
|
data_ += num_bytes;
|
2013-09-24 01:35:40 +00:00
|
|
|
UpdateCurrByte();
|
|
|
|
|
|
|
|
// If there is no more data remaining, only return true if we
|
|
|
|
// skipped all that were requested.
|
|
|
|
if (num_remaining_bits_in_curr_byte_ == 0)
|
|
|
|
return (num_bits == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less than 8 bits remaining to skip. Use ReadBitsInternal to verify
|
|
|
|
// that the remaining bits we need exist, and adjust them as necessary
|
|
|
|
// for subsequent operations.
|
2014-09-30 21:52:21 +00:00
|
|
|
uint64_t not_needed;
|
2013-09-24 01:35:40 +00:00
|
|
|
return ReadBitsInternal(num_bits, ¬_needed);
|
|
|
|
}
|
|
|
|
|
2016-11-09 02:11:13 +00:00
|
|
|
bool BitReader::SkipBytes(size_t num_bytes) {
|
2015-11-20 19:28:45 +00:00
|
|
|
if (num_remaining_bits_in_curr_byte_ != 8)
|
|
|
|
return false;
|
|
|
|
if (num_bytes == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
data_ += num_bytes - 1; // One additional byte in curr_byte_.
|
|
|
|
if (num_bytes > bytes_left_ + 1)
|
|
|
|
return false;
|
|
|
|
bytes_left_ -= num_bytes - 1;
|
|
|
|
num_remaining_bits_in_curr_byte_ = 0;
|
|
|
|
UpdateCurrByte();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-09 02:11:13 +00:00
|
|
|
bool BitReader::ReadBitsInternal(size_t num_bits, uint64_t* out) {
|
|
|
|
DCHECK_LE(num_bits, 64u);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
*out = 0;
|
|
|
|
|
|
|
|
while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) {
|
2016-11-09 02:11:13 +00:00
|
|
|
size_t bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
*out <<= bits_to_take;
|
|
|
|
*out += curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take);
|
|
|
|
num_bits -= bits_to_take;
|
|
|
|
num_remaining_bits_in_curr_byte_ -= bits_to_take;
|
|
|
|
curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1;
|
|
|
|
|
|
|
|
if (num_remaining_bits_in_curr_byte_ == 0)
|
|
|
|
UpdateCurrByte();
|
|
|
|
}
|
|
|
|
|
|
|
|
return num_bits == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitReader::UpdateCurrByte() {
|
2016-11-09 02:11:13 +00:00
|
|
|
DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0u);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
if (bytes_left_ == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Load a new byte and advance pointers.
|
|
|
|
curr_byte_ = *data_;
|
|
|
|
++data_;
|
|
|
|
--bytes_left_;
|
|
|
|
num_remaining_bits_in_curr_byte_ = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|