5 #include "packager/base/logging.h"
6 #include "packager/media/codecs/h26x_bit_reader.h"
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::SkipBits(
int num_bits) {
90 int bits_left = num_bits;
91 while (num_remaining_bits_in_curr_byte_ < bits_left) {
92 bits_left -= num_remaining_bits_in_curr_byte_;
93 if (!UpdateCurrByte())
97 num_remaining_bits_in_curr_byte_ -= bits_left;
101 bool H26xBitReader::ReadUE(
int* val) {
108 if (!ReadBits(1, &bit))
117 *val = (1 << num_bits) - 1;
120 if (!ReadBits(num_bits, &rest))
128 bool H26xBitReader::ReadSE(
int* val) {
143 off_t H26xBitReader::NumBitsLeft() {
144 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
147 bool H26xBitReader::HasMoreRBSPData() {
150 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
161 ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0;
164 size_t H26xBitReader::NumEmulationPreventionBytesRead() {
165 return emulation_prevention_bytes_;