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);
55 if (num_remaining_bits_in_curr_byte_ != 8)
60 data_ += num_bytes - 1;
61 if (num_bytes > bytes_left_ + 1)
63 bytes_left_ -= num_bytes - 1;
64 num_remaining_bits_in_curr_byte_ = 0;
69 bool BitReader::ReadBitsInternal(
size_t num_bits, uint64_t* out) {
70 DCHECK_LE(num_bits, 64u);
74 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) {
75 size_t bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits);
77 *out <<= bits_to_take;
78 *out += curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take);
79 num_bits -= bits_to_take;
80 num_remaining_bits_in_curr_byte_ -= bits_to_take;
81 curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1;
83 if (num_remaining_bits_in_curr_byte_ == 0)
90 void BitReader::UpdateCurrByte() {
91 DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0u);
100 num_remaining_bits_in_curr_byte_ = 8;
All the methods that are virtual are virtual for mocking.