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 #include "base/logging.h" | 6 #include "base/logging.h" |
7 | 7 |
8 namespace content { | 8 namespace content { |
9 | 9 |
10 H264BitReader::H264BitReader() | 10 H264BitReader::H264BitReader() |
11 : data_(NULL), bytes_left_(0), curr_byte_(0), | 11 : data_(NULL), bytes_left_(0), curr_byte_(0), |
12 num_remaining_bits_in_curr_byte_(0), prev_two_bytes_(0) { | 12 num_remaining_bits_in_curr_byte_(0), prev_two_bytes_(0), |
13 trailing_zero_bytes_(0), data_bits_in_last_byte_(0) { | |
Ami GONE FROM CHROMIUM
2012/08/01 23:04:00
data_bits_in_last_byte_(0) violates the member com
xiaomings
2012/08/01 23:39:54
All removed.
On 2012/08/01 23:04:00, Ami Fischman
| |
13 } | 14 } |
14 | 15 |
15 H264BitReader::~H264BitReader() {} | 16 H264BitReader::~H264BitReader() {} |
16 | 17 |
17 bool H264BitReader::Initialize(const uint8* data, off_t size) { | 18 bool H264BitReader::Initialize(const uint8* data, off_t size) { |
18 DCHECK(data); | 19 DCHECK(data); |
19 | 20 DCHECK_GE(size, 0); |
20 if (size < 1) | |
21 return false; | |
22 | 21 |
23 data_ = data; | 22 data_ = data; |
24 bytes_left_ = size; | 23 bytes_left_ = size; |
24 | |
25 if (!RemoveTrailingZeroAndStopBit()) | |
26 return false; | |
27 | |
25 num_remaining_bits_in_curr_byte_ = 0; | 28 num_remaining_bits_in_curr_byte_ = 0; |
26 // Initially set to 0xffff to accept all initial two-byte sequences. | 29 // Initially set to 0xffff to accept all initial two-byte sequences. |
27 prev_two_bytes_ = 0xffff; | 30 prev_two_bytes_ = 0xffff; |
28 | 31 |
29 return true; | 32 return true; |
30 } | 33 } |
31 | 34 |
32 bool H264BitReader::UpdateCurrByte() { | 35 bool H264BitReader::UpdateCurrByte() { |
33 if (bytes_left_ < 1) | 36 if (bytes_left_ < 1) |
34 return false; | 37 return false; |
35 | 38 |
36 // Emulation prevention three-byte detection. | 39 // Emulation prevention three-byte detection. |
37 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03). | 40 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03). |
38 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) { | 41 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) { |
39 // Detected 0x000003, skip last byte. | 42 // Detected 0x000003, skip last byte. |
40 ++data_; | 43 ++data_; |
41 --bytes_left_; | 44 --bytes_left_; |
42 // Need another full three bytes before we can detect the sequence again. | 45 // Need another full three bytes before we can detect the sequence again. |
43 prev_two_bytes_ = 0xffff; | 46 prev_two_bytes_ = 0xffff; |
44 | 47 |
45 if (bytes_left_ < 1) | 48 if (bytes_left_ < 1) |
46 return false; | 49 return false; |
47 } | 50 } |
48 | 51 |
49 // Load a new byte and advance pointers. | 52 // Load a new byte and advance pointers. |
50 curr_byte_ = *data_++ & 0xff; | 53 curr_byte_ = *data_++ & 0xff; |
51 --bytes_left_; | 54 --bytes_left_; |
52 num_remaining_bits_in_curr_byte_ = 8; | 55 |
56 if (bytes_left_ == 0) { | |
57 curr_byte_ >>= 8 - data_bits_in_last_byte_; | |
58 num_remaining_bits_in_curr_byte_ = data_bits_in_last_byte_; | |
59 } else { | |
60 num_remaining_bits_in_curr_byte_ = 8; | |
61 } | |
53 | 62 |
54 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_; | 63 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_; |
55 | 64 |
56 return true; | 65 return true; |
57 } | 66 } |
58 | 67 |
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) { | 68 bool H264BitReader::ReadBits(int num_bits, int *out) { |
63 int bits_left = num_bits; | 69 int bits_left = num_bits; |
64 *out = 0; | 70 *out = 0; |
65 DCHECK(num_bits <= 31); | 71 DCHECK_LE(num_bits, 31); |
66 | 72 |
67 while (num_remaining_bits_in_curr_byte_ < bits_left) { | 73 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. | 74 // 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_)); | 75 *out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_)); |
Ami GONE FROM CHROMIUM
2012/08/01 23:04:00
Is this a serious bug fix?
(where's the test, why
xiaomings
2012/08/01 23:39:54
It fixed a bug that H264BitReader fails to read an
| |
70 bits_left -= num_remaining_bits_in_curr_byte_; | 76 bits_left -= num_remaining_bits_in_curr_byte_; |
71 | 77 |
72 if (!UpdateCurrByte()) | 78 if (!UpdateCurrByte()) |
73 return false; | 79 return false; |
74 } | 80 } |
75 | 81 |
76 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left)); | 82 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left)); |
77 *out &= ((1 << num_bits) - 1); | 83 *out &= ((1 << num_bits) - 1); |
78 num_remaining_bits_in_curr_byte_ -= bits_left; | 84 num_remaining_bits_in_curr_byte_ -= bits_left; |
79 | 85 |
80 return true; | 86 return true; |
81 } | 87 } |
82 | 88 |
83 off_t H264BitReader::NumBitsLeft() { | 89 off_t H264BitReader::NumBitsLeft() const { |
84 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8); | 90 off_t bits_left = num_remaining_bits_in_curr_byte_ + |
91 (bytes_left_ + trailing_zero_bytes_) * 8; | |
92 if (bytes_left_ == 0) | |
93 bits_left += 8 - data_bits_in_last_byte_; | |
94 return bits_left; | |
85 } | 95 } |
86 | 96 |
87 bool H264BitReader::HasMoreRBSPData() { | 97 bool H264BitReader::HasMoreRBSPData() const { |
88 // Make sure we have more bits, if we are at 0 bits in current byte | 98 return num_remaining_bits_in_curr_byte_ != 0 || bytes_left_ != 0; |
89 // and updating current byte fails, we don't have more data anyway. | 99 } |
90 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte()) | |
91 return false; | |
92 | 100 |
93 // On last byte? | 101 bool H264BitReader::RemoveTrailingZeroAndStopBit() { |
94 if (bytes_left_) | 102 // First remove all trailing zero bytes. |
95 return true; | 103 trailing_zero_bytes_ = 0; |
96 | 104 |
97 // Last byte, look for stop bit; | 105 while (bytes_left_ != 0) { |
98 // We have more RBSP data if the last non-zero bit we find is not the | 106 if (data_[bytes_left_ - 1] == 0) { |
99 // first available bit. | 107 --bytes_left_; |
100 return (curr_byte_ & | 108 ++trailing_zero_bytes_; |
101 ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0; | 109 continue; |
110 } | |
111 if (bytes_left_ >= 3 && data_[bytes_left_ - 1] == 0x03 && | |
112 data_[bytes_left_ - 2] == 0x00 && data_[bytes_left_ - 3] == 0x00) { | |
113 bytes_left_ -= 3; | |
114 trailing_zero_bytes_ += 3; | |
115 continue; | |
116 } | |
117 | |
118 break; | |
119 } | |
120 | |
121 if (bytes_left_ == 0) | |
122 return false; | |
123 | |
124 // Then remove the trailing zero bits of the last byte and the stop bit. | |
125 uint8 last_byte = data_[bytes_left_ - 1]; | |
126 | |
127 // The whole last byte will be ignored if it is 0x80. | |
Ami GONE FROM CHROMIUM
2012/08/01 23:04:00
l.127-140 smell to me. Why is 0x80 handled differ
xiaomings
2012/08/01 23:39:54
0x80 is special here, as if the last bytes is 0x80
| |
128 if (last_byte == 0x80) { | |
129 --bytes_left_; | |
130 data_bits_in_last_byte_ = 8; | |
131 ++trailing_zero_bytes_; | |
132 | |
133 return bytes_left_ != 0; | |
134 } | |
135 | |
136 data_bits_in_last_byte_ = 8; | |
137 | |
138 while ((last_byte & (1 << (8 - data_bits_in_last_byte_))) == 0) | |
139 --data_bits_in_last_byte_; | |
140 --data_bits_in_last_byte_; | |
141 | |
142 return true; | |
102 } | 143 } |
103 | 144 |
104 } // namespace content | 145 } // namespace content |
OLD | NEW |