| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/common/gpu/media/h264_bit_reader.h" | |
| 6 #include "base/logging.h" | |
| 7 | |
| 8 namespace content { | |
| 9 | |
| 10 H264BitReader::H264BitReader() | |
| 11 : data_(NULL), bytes_left_(0), curr_byte_(0), | |
| 12 num_remaining_bits_in_curr_byte_(0), prev_two_bytes_(0), | |
| 13 emulation_prevention_bytes_(0) { | |
| 14 } | |
| 15 | |
| 16 H264BitReader::~H264BitReader() {} | |
| 17 | |
| 18 bool H264BitReader::Initialize(const uint8* data, off_t size) { | |
| 19 DCHECK(data); | |
| 20 | |
| 21 if (size < 1) | |
| 22 return false; | |
| 23 | |
| 24 data_ = data; | |
| 25 bytes_left_ = size; | |
| 26 num_remaining_bits_in_curr_byte_ = 0; | |
| 27 // Initially set to 0xffff to accept all initial two-byte sequences. | |
| 28 prev_two_bytes_ = 0xffff; | |
| 29 emulation_prevention_bytes_ = 0; | |
| 30 | |
| 31 return true; | |
| 32 } | |
| 33 | |
| 34 bool H264BitReader::UpdateCurrByte() { | |
| 35 if (bytes_left_ < 1) | |
| 36 return false; | |
| 37 | |
| 38 // Emulation prevention three-byte detection. | |
| 39 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03). | |
| 40 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) { | |
| 41 // Detected 0x000003, skip last byte. | |
| 42 ++data_; | |
| 43 --bytes_left_; | |
| 44 ++emulation_prevention_bytes_; | |
| 45 // Need another full three bytes before we can detect the sequence again. | |
| 46 prev_two_bytes_ = 0xffff; | |
| 47 | |
| 48 if (bytes_left_ < 1) | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 // Load a new byte and advance pointers. | |
| 53 curr_byte_ = *data_++ & 0xff; | |
| 54 --bytes_left_; | |
| 55 num_remaining_bits_in_curr_byte_ = 8; | |
| 56 | |
| 57 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_; | |
| 58 | |
| 59 return true; | |
| 60 } | |
| 61 | |
| 62 // Read |num_bits| (1 to 31 inclusive) from the stream and return them | |
| 63 // in |out|, with first bit in the stream as MSB in |out| at position | |
| 64 // (|num_bits| - 1). | |
| 65 bool H264BitReader::ReadBits(int num_bits, int *out) { | |
| 66 int bits_left = num_bits; | |
| 67 *out = 0; | |
| 68 DCHECK(num_bits <= 31); | |
| 69 | |
| 70 while (num_remaining_bits_in_curr_byte_ < bits_left) { | |
| 71 // Take all that's left in current byte, shift to make space for the rest. | |
| 72 *out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_)); | |
| 73 bits_left -= num_remaining_bits_in_curr_byte_; | |
| 74 | |
| 75 if (!UpdateCurrByte()) | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left)); | |
| 80 *out &= ((1 << num_bits) - 1); | |
| 81 num_remaining_bits_in_curr_byte_ -= bits_left; | |
| 82 | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 off_t H264BitReader::NumBitsLeft() { | |
| 87 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8); | |
| 88 } | |
| 89 | |
| 90 bool H264BitReader::HasMoreRBSPData() { | |
| 91 // Make sure we have more bits, if we are at 0 bits in current byte | |
| 92 // and updating current byte fails, we don't have more data anyway. | |
| 93 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte()) | |
| 94 return false; | |
| 95 | |
| 96 // On last byte? | |
| 97 if (bytes_left_) | |
| 98 return true; | |
| 99 | |
| 100 // Last byte, look for stop bit; | |
| 101 // We have more RBSP data if the last non-zero bit we find is not the | |
| 102 // first available bit. | |
| 103 return (curr_byte_ & | |
| 104 ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0; | |
| 105 } | |
| 106 | |
| 107 size_t H264BitReader::NumEmulationPreventionBytesRead() | |
| 108 { | |
| 109 return emulation_prevention_bytes_; | |
| 110 } | |
| 111 | |
| 112 } // namespace content | |
| OLD | NEW |