OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/gpu/media/h264_bit_reader.h" | 5 #include "content/common/gpu/media/h264_bit_reader.h" |
| 6 |
| 7 #include <string.h> |
| 8 |
6 #include "base/logging.h" | 9 #include "base/logging.h" |
7 | 10 |
8 namespace content { | 11 namespace content { |
9 | 12 |
10 H264BitReader::H264BitReader() | 13 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 | 14 |
15 H264BitReader::~H264BitReader() {} | 15 H264BitReader::~H264BitReader() {} |
16 | 16 |
17 bool H264BitReader::Initialize(const uint8* data, off_t size) { | 17 bool H264BitReader::Initialize(const uint8* data, off_t size) { |
18 DCHECK(data); | 18 DCHECK(data); |
19 | 19 DCHECK_GE(size, 0); |
20 if (size < 1) | |
21 return false; | |
22 | 20 |
23 data_ = data; | 21 data_ = data; |
24 bytes_left_ = size; | 22 bytes_left_ = size; |
| 23 |
| 24 if (!RemoveTrailingZeroAndStopBit()) |
| 25 return false; |
| 26 |
25 num_remaining_bits_in_curr_byte_ = 0; | 27 num_remaining_bits_in_curr_byte_ = 0; |
26 // Initially set to 0xffff to accept all initial two-byte sequences. | 28 // Initially set to 0xffff to accept all initial two-byte sequences. |
27 prev_two_bytes_ = 0xffff; | 29 prev_two_bytes_ = 0xffff; |
28 | 30 |
29 return true; | 31 return true; |
30 } | 32 } |
31 | 33 |
32 bool H264BitReader::UpdateCurrByte() { | 34 bool H264BitReader::UpdateCurrByte() { |
33 if (bytes_left_ < 1) | 35 if (bytes_left_ < 1) |
34 return false; | 36 return false; |
35 | 37 |
36 // Emulation prevention three-byte detection. | 38 // Emulation prevention three-byte detection. |
37 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03). | 39 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03). |
38 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) { | 40 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) { |
39 // Detected 0x000003, skip last byte. | 41 // Detected 0x000003, skip last byte. |
40 ++data_; | 42 ++data_; |
41 --bytes_left_; | 43 --bytes_left_; |
42 // Need another full three bytes before we can detect the sequence again. | 44 // Need another full three bytes before we can detect the sequence again. |
43 prev_two_bytes_ = 0xffff; | 45 prev_two_bytes_ = 0xffff; |
44 | 46 |
45 if (bytes_left_ < 1) | 47 if (bytes_left_ < 1) |
46 return false; | 48 return false; |
47 } | 49 } |
48 | 50 |
49 // Load a new byte and advance pointers. | 51 // Load a new byte and advance pointers. |
50 curr_byte_ = *data_++ & 0xff; | 52 curr_byte_ = *data_++ & 0xff; |
51 --bytes_left_; | 53 --bytes_left_; |
52 num_remaining_bits_in_curr_byte_ = 8; | 54 |
| 55 if (bytes_left_ == 0) { |
| 56 curr_byte_ >>= 8 - data_bits_in_last_byte_; |
| 57 num_remaining_bits_in_curr_byte_ = data_bits_in_last_byte_; |
| 58 } else { |
| 59 num_remaining_bits_in_curr_byte_ = 8; |
| 60 } |
53 | 61 |
54 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_; | 62 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_; |
55 | 63 |
56 return true; | 64 return true; |
57 } | 65 } |
58 | 66 |
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) { | 67 bool H264BitReader::ReadBits(int num_bits, int *out) { |
63 int bits_left = num_bits; | 68 int bits_left = num_bits; |
64 *out = 0; | 69 *out = 0; |
65 DCHECK(num_bits <= 31); | 70 DCHECK_LE(num_bits, 31); |
66 | 71 |
67 while (num_remaining_bits_in_curr_byte_ < bits_left) { | 72 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. | 73 // 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_)); | 74 *out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_)); |
70 bits_left -= num_remaining_bits_in_curr_byte_; | 75 bits_left -= num_remaining_bits_in_curr_byte_; |
71 | 76 |
72 if (!UpdateCurrByte()) | 77 if (!UpdateCurrByte()) |
73 return false; | 78 return false; |
74 } | 79 } |
75 | 80 |
76 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left)); | 81 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left)); |
77 *out &= ((1 << num_bits) - 1); | 82 *out &= ((1 << num_bits) - 1); |
78 num_remaining_bits_in_curr_byte_ -= bits_left; | 83 num_remaining_bits_in_curr_byte_ -= bits_left; |
79 | 84 |
80 return true; | 85 return true; |
81 } | 86 } |
82 | 87 |
83 off_t H264BitReader::NumBitsLeft() { | 88 off_t H264BitReader::NumBitsLeft() const { |
84 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8); | 89 off_t bits_left = num_remaining_bits_in_curr_byte_ + |
| 90 (bytes_left_ + trailing_zero_bytes_) * 8; |
| 91 if (bytes_left_ == 0) |
| 92 bits_left += 8 - data_bits_in_last_byte_; |
| 93 return bits_left; |
85 } | 94 } |
86 | 95 |
87 bool H264BitReader::HasMoreRBSPData() { | 96 bool H264BitReader::RemoveTrailingZeroAndStopBit() { |
88 // Make sure we have more bits, if we are at 0 bits in current byte | 97 // First remove all trailing zero bytes. |
89 // and updating current byte fails, we don't have more data anyway. | 98 trailing_zero_bytes_ = 0; |
90 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte()) | |
91 return false; | |
92 | 99 |
93 // On last byte? | 100 while (bytes_left_ != 0) { |
94 if (bytes_left_) | 101 if (data_[bytes_left_ - 1] == 0) { |
95 return true; | 102 --bytes_left_; |
| 103 ++trailing_zero_bytes_; |
| 104 continue; |
| 105 } |
| 106 if (bytes_left_ >= 3 && data_[bytes_left_ - 1] == 0x03 && |
| 107 data_[bytes_left_ - 2] == 0x00 && data_[bytes_left_ - 3] == 0x00) { |
| 108 bytes_left_ -= 3; |
| 109 trailing_zero_bytes_ += 3; |
| 110 continue; |
| 111 } |
96 | 112 |
97 // Last byte, look for stop bit; | 113 break; |
98 // We have more RBSP data if the last non-zero bit we find is not the | 114 } |
99 // first available bit. | 115 |
100 return (curr_byte_ & | 116 if (bytes_left_ == 0) |
101 ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0; | 117 return false; |
| 118 |
| 119 // Then remove the trailing zero bits of the last byte and the stop bit. |
| 120 uint8 last_byte = data_[bytes_left_ - 1]; |
| 121 |
| 122 if (last_byte == 0x80) { |
| 123 --bytes_left_; |
| 124 data_bits_in_last_byte_ = 8; |
| 125 ++trailing_zero_bytes_; |
| 126 |
| 127 return bytes_left_ != 0; |
| 128 } |
| 129 |
| 130 data_bits_in_last_byte_ = 8 - ffs(last_byte); |
| 131 |
| 132 return true; |
102 } | 133 } |
103 | 134 |
104 } // namespace content | 135 } // namespace content |
OLD | NEW |