5 #include "packager/media/base/bit_reader.h"
9 namespace edash_packager {
13 : data_(data), bytes_left_(size), num_remaining_bits_in_curr_byte_(0) {
14 DCHECK(data_ != NULL && bytes_left_ > 0);
19 BitReader::~BitReader() {}
22 DCHECK_GE(num_bits, 0);
26 if (num_bits > num_remaining_bits_in_curr_byte_) {
27 num_bits -= num_remaining_bits_in_curr_byte_;
28 num_remaining_bits_in_curr_byte_ = 0;
30 int num_bytes = num_bits / 8;
32 if (bytes_left_ < num_bytes) {
36 bytes_left_ -= num_bytes;
42 if (num_remaining_bits_in_curr_byte_ == 0)
43 return (num_bits == 0);
50 return ReadBitsInternal(num_bits, ¬_needed);
54 return 8 * bytes_left_ + num_remaining_bits_in_curr_byte_;
57 bool BitReader::ReadBitsInternal(
int num_bits, uint64_t* out) {
58 DCHECK_LE(num_bits, 64);
62 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) {
63 int bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits);
65 *out <<= bits_to_take;
66 *out += curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take);
67 num_bits -= bits_to_take;
68 num_remaining_bits_in_curr_byte_ -= bits_to_take;
69 curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1;
71 if (num_remaining_bits_in_curr_byte_ == 0)
78 void BitReader::UpdateCurrByte() {
79 DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0);
88 num_remaining_bits_in_curr_byte_ = 8;