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() {}
25 DCHECK_GE(num_bits, 0);
29 if (num_bits > num_remaining_bits_in_curr_byte_) {
30 num_bits -= num_remaining_bits_in_curr_byte_;
31 num_remaining_bits_in_curr_byte_ = 0;
33 int num_bytes = num_bits / 8;
35 if (bytes_left_ < num_bytes) {
39 bytes_left_ -= num_bytes;
45 if (num_remaining_bits_in_curr_byte_ == 0)
46 return (num_bits == 0);
53 return ReadBitsInternal(num_bits, ¬_needed);
57 if (num_remaining_bits_in_curr_byte_ != 8)
62 data_ += num_bytes - 1;
63 if (num_bytes > bytes_left_ + 1)
65 bytes_left_ -= num_bytes - 1;
66 num_remaining_bits_in_curr_byte_ = 0;
71 bool BitReader::ReadBitsInternal(
int num_bits, uint64_t* out) {
72 DCHECK_LE(num_bits, 64);
76 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) {
77 int bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits);
79 *out <<= bits_to_take;
80 *out += curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take);
81 num_bits -= bits_to_take;
82 num_remaining_bits_in_curr_byte_ -= bits_to_take;
83 curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1;
85 if (num_remaining_bits_in_curr_byte_ == 0)
92 void BitReader::UpdateCurrByte() {
93 DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0);
102 num_remaining_bits_in_curr_byte_ = 8;