5 #include "packager/media/base/bit_reader.h"
16 num_remaining_bits_in_curr_byte_(0) {
17 DCHECK(data_ != NULL && bytes_left_ > 0);
22 BitReader::~BitReader() {}
27 if (num_bits > num_remaining_bits_in_curr_byte_) {
28 num_bits -= num_remaining_bits_in_curr_byte_;
29 num_remaining_bits_in_curr_byte_ = 0;
31 size_t num_bytes = num_bits / 8;
33 if (bytes_left_ < num_bytes) {
37 bytes_left_ -= num_bytes;
43 if (num_remaining_bits_in_curr_byte_ == 0)
44 return (num_bits == 0);
51 return ReadBitsInternal(num_bits, ¬_needed);
56 if (num_remaining_bits_in_curr_byte_ == 8)
59 num_remaining_bits_in_curr_byte_ = 0;
66 if (num_remaining_bits_in_curr_byte_ != 8)
69 data_ += num_bytes - 1;
70 if (num_bytes > bytes_left_ + 1)
72 bytes_left_ -= num_bytes - 1;
73 num_remaining_bits_in_curr_byte_ = 0;
78 bool BitReader::ReadBitsInternal(
size_t num_bits, uint64_t* out) {
79 DCHECK_LE(num_bits, 64u);
83 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) {
84 size_t bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits);
86 *out <<= bits_to_take;
87 *out += curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take);
88 num_bits -= bits_to_take;
89 num_remaining_bits_in_curr_byte_ -= bits_to_take;
90 curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1;
92 if (num_remaining_bits_in_curr_byte_ == 0)
99 void BitReader::UpdateCurrByte() {
100 DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0u);
102 if (bytes_left_ == 0)
109 num_remaining_bits_in_curr_byte_ = 8;