5 #include "packager/base/logging.h" 6 #include "packager/media/codecs/h26x_bit_reader.h" 13 bool CheckAnyBitsSet(
int byte,
int valid_bits) {
14 return (byte & ((1 << valid_bits) - 1)) != 0;
19 H26xBitReader::H26xBitReader()
23 num_remaining_bits_in_curr_byte_(0),
25 emulation_prevention_bytes_(0) {}
27 H26xBitReader::~H26xBitReader() {}
29 bool H26xBitReader::Initialize(
const uint8_t* data, off_t size) {
37 num_remaining_bits_in_curr_byte_ = 0;
39 prev_two_bytes_ = 0xffff;
40 emulation_prevention_bytes_ = 0;
45 bool H26xBitReader::UpdateCurrByte() {
51 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) {
55 ++emulation_prevention_bytes_;
57 prev_two_bytes_ = 0xffff;
64 curr_byte_ = *data_++ & 0xff;
66 num_remaining_bits_in_curr_byte_ = 8;
68 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_;
76 bool H26xBitReader::ReadBits(
int num_bits,
int* out) {
77 int bits_left = num_bits;
79 DCHECK(num_bits <= 31);
81 while (num_remaining_bits_in_curr_byte_ < bits_left) {
83 *out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_));
84 bits_left -= num_remaining_bits_in_curr_byte_;
86 if (!UpdateCurrByte())
90 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left));
91 *out &= ((1 << num_bits) - 1);
92 num_remaining_bits_in_curr_byte_ -= bits_left;
97 bool H26xBitReader::SkipBits(
int num_bits) {
98 int bits_left = num_bits;
99 while (num_remaining_bits_in_curr_byte_ < bits_left) {
100 bits_left -= num_remaining_bits_in_curr_byte_;
101 if (!UpdateCurrByte())
105 num_remaining_bits_in_curr_byte_ -= bits_left;
109 bool H26xBitReader::ReadUE(
int* val) {
116 if (!ReadBits(1, &bit))
125 *val = (1 << num_bits) - 1;
128 if (!ReadBits(num_bits, &rest))
136 bool H26xBitReader::ReadSE(
int* val) {
151 off_t H26xBitReader::NumBitsLeft() {
152 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
155 bool H26xBitReader::HasMoreRBSPData() {
158 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
165 if (CheckAnyBitsSet(curr_byte_, num_remaining_bits_in_curr_byte_ - 1))
172 for (off_t i = 0; i < bytes_left_; i++) {
181 size_t H26xBitReader::NumEmulationPreventionBytesRead() {
182 return emulation_prevention_bytes_;
All the methods that are virtual are virtual for mocking.