5 #include "packager/base/logging.h"
6 #include "packager/media/filters/h264_bit_reader.h"
8 namespace edash_packager {
11 H264BitReader::H264BitReader()
15 num_remaining_bits_in_curr_byte_(0),
17 emulation_prevention_bytes_(0) {}
19 H264BitReader::~H264BitReader() {}
21 bool H264BitReader::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 H264BitReader::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 H264BitReader::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 off_t H264BitReader::NumBitsLeft() {
90 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
93 bool H264BitReader::HasMoreRBSPData() {
96 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
107 ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0;
110 size_t H264BitReader::NumEmulationPreventionBytesRead() {
111 return emulation_prevention_bytes_;