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