This source file includes following definitions.
- emulation_prevention_bytes_
- Initialize
- UpdateCurrByte
- ReadBits
- NumBitsLeft
- HasMoreRBSPData
- NumEmulationPreventionBytesRead
#include "base/logging.h"
#include "media/filters/h264_bit_reader.h"
namespace media {
H264BitReader::H264BitReader()
: data_(NULL),
bytes_left_(0),
curr_byte_(0),
num_remaining_bits_in_curr_byte_(0),
prev_two_bytes_(0),
emulation_prevention_bytes_(0) {}
H264BitReader::~H264BitReader() {}
bool H264BitReader::Initialize(const uint8* data, off_t size) {
DCHECK(data);
if (size < 1)
return false;
data_ = data;
bytes_left_ = size;
num_remaining_bits_in_curr_byte_ = 0;
prev_two_bytes_ = 0xffff;
emulation_prevention_bytes_ = 0;
return true;
}
bool H264BitReader::UpdateCurrByte() {
if (bytes_left_ < 1)
return false;
if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) {
++data_;
--bytes_left_;
++emulation_prevention_bytes_;
prev_two_bytes_ = 0xffff;
if (bytes_left_ < 1)
return false;
}
curr_byte_ = *data_++ & 0xff;
--bytes_left_;
num_remaining_bits_in_curr_byte_ = 8;
prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_;
return true;
}
bool H264BitReader::ReadBits(int num_bits, int* out) {
int bits_left = num_bits;
*out = 0;
DCHECK(num_bits <= 31);
while (num_remaining_bits_in_curr_byte_ < bits_left) {
*out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_));
bits_left -= num_remaining_bits_in_curr_byte_;
if (!UpdateCurrByte())
return false;
}
*out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left));
*out &= ((1 << num_bits) - 1);
num_remaining_bits_in_curr_byte_ -= bits_left;
return true;
}
off_t H264BitReader::NumBitsLeft() {
return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
}
bool H264BitReader::HasMoreRBSPData() {
if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
return false;
if (bytes_left_)
return true;
return (curr_byte_ &
((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0;
}
size_t H264BitReader::NumEmulationPreventionBytesRead() {
return emulation_prevention_bytes_;
}
}