5 #include <packager/media/base/bit_reader.h>
9 #include <absl/log/check.h>
18 num_remaining_bits_in_curr_byte_(0) {
19 DCHECK(data_ != NULL && bytes_left_ > 0);
24 BitReader::~BitReader() {}
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 size_t 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);
58 if (num_remaining_bits_in_curr_byte_ == 8)
61 num_remaining_bits_in_curr_byte_ = 0;
68 if (num_remaining_bits_in_curr_byte_ != 8)
71 data_ += num_bytes - 1;
72 if (num_bytes > bytes_left_ + 1)
74 bytes_left_ -= num_bytes - 1;
75 num_remaining_bits_in_curr_byte_ = 0;
80 bool BitReader::ReadBitsInternal(
size_t num_bits, uint64_t* out) {
81 DCHECK_LE(num_bits, 64u);
85 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) {
86 size_t bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits);
88 *out <<= bits_to_take;
89 *out += curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take);
90 num_bits -= bits_to_take;
91 num_remaining_bits_in_curr_byte_ -= bits_to_take;
92 curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1;
94 if (num_remaining_bits_in_curr_byte_ == 0)
101 void BitReader::UpdateCurrByte() {
102 DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0u);
104 if (bytes_left_ == 0)
111 num_remaining_bits_in_curr_byte_ = 8;
All the methods that are virtual are virtual for mocking.