5 #include "packager/base/logging.h"
6 #include "packager/media/filters/h26x_bit_reader.h"
8 namespace edash_packager {
11 H26xBitReader::H26xBitReader()
15 num_remaining_bits_in_curr_byte_(0),
17 emulation_prevention_bytes_(0) {}
19 H26xBitReader::~H26xBitReader() {}
21 bool H26xBitReader::Initialize(
const uint8_t* data, off_t size) {
29 num_remaining_bits_in_curr_byte_ = 0;
31 prev_two_bytes_ = 0xffff;
32 emulation_prevention_bytes_ = 0;
37 bool H26xBitReader::UpdateCurrByte() {
43 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) {
47 ++emulation_prevention_bytes_;
49 prev_two_bytes_ = 0xffff;
56 curr_byte_ = *data_++ & 0xff;
58 num_remaining_bits_in_curr_byte_ = 8;
60 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_;
68 bool H26xBitReader::ReadBits(
int num_bits,
int* out) {
69 int bits_left = num_bits;
71 DCHECK(num_bits <= 31);
73 while (num_remaining_bits_in_curr_byte_ < bits_left) {
75 *out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_));
76 bits_left -= num_remaining_bits_in_curr_byte_;
78 if (!UpdateCurrByte())
82 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left));
83 *out &= ((1 << num_bits) - 1);
84 num_remaining_bits_in_curr_byte_ -= bits_left;
89 bool H26xBitReader::ReadUE(
int* val) {
96 if (!ReadBits(1, &bit))
105 *val = (1 << num_bits) - 1;
108 if (!ReadBits(num_bits, &rest))
116 bool H26xBitReader::ReadSE(
int* val) {
131 off_t H26xBitReader::NumBitsLeft() {
132 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
135 bool H26xBitReader::HasMoreRBSPData() {
138 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
149 ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0;
152 size_t H26xBitReader::NumEmulationPreventionBytesRead() {
153 return emulation_prevention_bytes_;