Index: content/common/gpu/media/h264_bit_reader.cc |
diff --git a/content/common/gpu/media/h264_bit_reader.cc b/content/common/gpu/media/h264_bit_reader.cc |
index 64e12bee203ecfdcd34322faabbde9362d31c5f2..f7d992069615b9a3520ef92aeeeb412d83384bf2 100644 |
--- a/content/common/gpu/media/h264_bit_reader.cc |
+++ b/content/common/gpu/media/h264_bit_reader.cc |
@@ -3,25 +3,27 @@ |
// found in the LICENSE file. |
#include "content/common/gpu/media/h264_bit_reader.h" |
+ |
+#include <string.h> |
+ |
#include "base/logging.h" |
namespace content { |
-H264BitReader::H264BitReader() |
- : data_(NULL), bytes_left_(0), curr_byte_(0), |
- num_remaining_bits_in_curr_byte_(0), prev_two_bytes_(0) { |
-} |
+H264BitReader::H264BitReader() {} |
H264BitReader::~H264BitReader() {} |
bool H264BitReader::Initialize(const uint8* data, off_t size) { |
DCHECK(data); |
- |
- if (size < 1) |
- return false; |
+ DCHECK_GE(size, 0); |
data_ = data; |
bytes_left_ = size; |
+ |
+ if (!RemoveTrailingZeroAndStopBit()) |
+ return false; |
+ |
num_remaining_bits_in_curr_byte_ = 0; |
// Initially set to 0xffff to accept all initial two-byte sequences. |
prev_two_bytes_ = 0xffff; |
@@ -49,20 +51,23 @@ bool H264BitReader::UpdateCurrByte() { |
// Load a new byte and advance pointers. |
curr_byte_ = *data_++ & 0xff; |
--bytes_left_; |
- num_remaining_bits_in_curr_byte_ = 8; |
+ |
+ if (bytes_left_ == 0) { |
+ curr_byte_ >>= 8 - data_bits_in_last_byte_; |
+ num_remaining_bits_in_curr_byte_ = data_bits_in_last_byte_; |
+ } else { |
+ num_remaining_bits_in_curr_byte_ = 8; |
+ } |
prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_; |
return true; |
} |
-// Read |num_bits| (1 to 31 inclusive) from the stream and return them |
-// in |out|, with first bit in the stream as MSB in |out| at position |
-// (|num_bits| - 1). |
bool H264BitReader::ReadBits(int num_bits, int *out) { |
int bits_left = num_bits; |
*out = 0; |
- DCHECK(num_bits <= 31); |
+ DCHECK_LE(num_bits, 31); |
while (num_remaining_bits_in_curr_byte_ < bits_left) { |
// Take all that's left in current byte, shift to make space for the rest. |
@@ -80,25 +85,51 @@ bool H264BitReader::ReadBits(int num_bits, int *out) { |
return true; |
} |
-off_t H264BitReader::NumBitsLeft() { |
- return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8); |
+off_t H264BitReader::NumBitsLeft() const { |
+ off_t bits_left = num_remaining_bits_in_curr_byte_ + |
+ (bytes_left_ + trailing_zero_bytes_) * 8; |
+ if (bytes_left_ == 0) |
+ bits_left += 8 - data_bits_in_last_byte_; |
+ return bits_left; |
} |
-bool H264BitReader::HasMoreRBSPData() { |
- // Make sure we have more bits, if we are at 0 bits in current byte |
- // and updating current byte fails, we don't have more data anyway. |
- if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte()) |
- return false; |
+bool H264BitReader::RemoveTrailingZeroAndStopBit() { |
+ // First remove all trailing zero bytes. |
+ trailing_zero_bytes_ = 0; |
+ |
+ while (bytes_left_ != 0) { |
+ if (data_[bytes_left_ - 1] == 0) { |
+ --bytes_left_; |
+ ++trailing_zero_bytes_; |
+ continue; |
+ } |
+ if (bytes_left_ >= 3 && data_[bytes_left_ - 1] == 0x03 && |
+ data_[bytes_left_ - 2] == 0x00 && data_[bytes_left_ - 3] == 0x00) { |
+ bytes_left_ -= 3; |
+ trailing_zero_bytes_ += 3; |
+ continue; |
+ } |
+ |
+ break; |
+ } |
+ |
+ if (bytes_left_ == 0) |
+ return false; |
+ |
+ // Then remove the trailing zero bits of the last byte and the stop bit. |
+ uint8 last_byte = data_[bytes_left_ - 1]; |
- // On last byte? |
- if (bytes_left_) |
- return true; |
+ if (last_byte == 0x80) { |
+ --bytes_left_; |
+ data_bits_in_last_byte_ = 8; |
+ ++trailing_zero_bytes_; |
+ |
+ return bytes_left_ != 0; |
+ } |
- // Last byte, look for stop bit; |
- // We have more RBSP data if the last non-zero bit we find is not the |
- // first available bit. |
- return (curr_byte_ & |
- ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0; |
+ data_bits_in_last_byte_ = 8 - ffs(last_byte); |
+ |
+ return true; |
} |
} // namespace content |