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_parser.h" | 5 #include "content/common/gpu/media/h264_parser.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 | 10 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 } | 43 } |
44 | 44 |
45 H264SliceHeader::H264SliceHeader() { | 45 H264SliceHeader::H264SliceHeader() { |
46 memset(this, 0, sizeof(*this)); | 46 memset(this, 0, sizeof(*this)); |
47 } | 47 } |
48 | 48 |
49 H264SEIMessage::H264SEIMessage() { | 49 H264SEIMessage::H264SEIMessage() { |
50 memset(this, 0, sizeof(*this)); | 50 memset(this, 0, sizeof(*this)); |
51 } | 51 } |
52 | 52 |
53 H264Parser::H264BitReader::H264BitReader() | 53 H264Parser::H264BitReader::H264BitReader() { |
54 : data_(NULL), | |
55 bytes_left_(0), | |
56 curr_byte_(0), | |
57 num_remaining_bits_in_curr_byte_(0), | |
58 prev_two_bytes_(0) { | |
59 } | 54 } |
60 | 55 |
61 H264Parser::H264BitReader::~H264BitReader() {} | 56 H264Parser::H264BitReader::~H264BitReader() {} |
62 | 57 |
63 bool H264Parser::H264BitReader::Initialize(const uint8* data, off_t size) { | 58 void H264Parser::H264BitReader::UpdateCurrByte() { |
64 DCHECK(data); | 59 DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0); |
65 | 60 |
66 if (size < 1) | 61 if (bytes_left_ >= 1) { |
67 return false; | 62 // Emulation prevention three-byte detection. |
63 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03). | |
64 if (*data_ == 0x03 && Tell() >= 16 && data_[-1] == 0 && data_[-2] == 0) { | |
65 // Detected 0x000003, skip last byte. | |
66 ++data_; | |
67 --bytes_left_; | |
68 position_ += 8; | |
69 } | |
70 } | |
68 | 71 |
69 data_ = data; | 72 if (bytes_left_ >= 1) { |
70 bytes_left_ = size; | 73 // Load a new byte and advance pointers. |
71 num_remaining_bits_in_curr_byte_ = 0; | 74 curr_byte_ = *data_; |
72 // Initially set to 0xffff to accept all initial two-byte sequences. | |
73 prev_two_bytes_ = 0xffff; | |
74 | |
75 return true; | |
76 } | |
77 | |
78 bool H264Parser::H264BitReader::UpdateCurrByte() { | |
79 if (bytes_left_ < 1) | |
80 return false; | |
81 | |
82 // Emulation prevention three-byte detection. | |
83 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03). | |
84 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) { | |
85 // Detected 0x000003, skip last byte. | |
86 ++data_; | 75 ++data_; |
87 --bytes_left_; | 76 --bytes_left_; |
88 // Need another full three bytes before we can detect the sequence again. | 77 num_remaining_bits_in_curr_byte_ = 8; |
89 prev_two_bytes_ = 0xffff; | |
90 | |
91 if (bytes_left_ < 1) | |
92 return false; | |
93 } | 78 } |
94 | 79 |
95 // Load a new byte and advance pointers. | 80 // Check if this is the end of RBSP data. |
96 curr_byte_ = *data_++ & 0xff; | 81 if (bytes_left_ == 0) { |
97 --bytes_left_; | 82 while (num_remaining_bits_in_curr_byte_ != 0 && curr_byte_ % 2 == 0) { |
acolwell GONE FROM CHROMIUM
2012/06/28 17:31:25
nit: Since we are dealing with bits I'd prefer !(c
| |
98 num_remaining_bits_in_curr_byte_ = 8; | 83 --num_remaining_bits_in_curr_byte_; |
99 | 84 curr_byte_ /= 2; |
100 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_; | 85 } |
101 | |
102 return true; | |
103 } | |
104 | |
105 // Read |num_bits| (1 to 31 inclusive) from the stream and return them | |
106 // in |out|, with first bit in the stream as MSB in |out| at position | |
107 // (|num_bits| - 1). | |
108 bool H264Parser::H264BitReader::ReadBits(int num_bits, int *out) { | |
109 int bits_left = num_bits; | |
110 *out = 0; | |
111 DCHECK(num_bits <= 31); | |
112 | |
113 while (num_remaining_bits_in_curr_byte_ < bits_left) { | |
114 // Take all that's left in current byte, shift to make space for the rest. | |
115 *out = (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_)); | |
116 bits_left -= num_remaining_bits_in_curr_byte_; | |
117 | |
118 if (!UpdateCurrByte()) | |
119 return false; | |
120 } | 86 } |
121 | |
122 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left)); | |
123 *out &= ((1 << num_bits) - 1); | |
124 num_remaining_bits_in_curr_byte_ -= bits_left; | |
125 | |
126 return true; | |
127 } | |
128 | |
129 off_t H264Parser::H264BitReader::NumBitsLeft() { | |
130 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8); | |
131 } | |
132 | |
133 bool H264Parser::H264BitReader::HasMoreRBSPData() { | |
134 // Make sure we have more bits, if we are at 0 bits in current byte | |
135 // and updating current byte fails, we don't have more data anyway. | |
136 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte()) | |
137 return false; | |
138 | |
139 // On last byte? | |
140 if (bytes_left_) | |
141 return true; | |
142 | |
143 // Last byte, look for stop bit; | |
144 // We have more RBSP data if the last non-zero bit we find is not the | |
145 // first available bit. | |
146 return (curr_byte_ & | |
147 ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0; | |
148 } | 87 } |
149 | 88 |
150 #define READ_BITS_OR_RETURN(num_bits, out) \ | 89 #define READ_BITS_OR_RETURN(num_bits, out) \ |
151 do { \ | 90 do { \ |
152 int _out; \ | 91 int _out; \ |
153 if (!br_.ReadBits(num_bits, &_out)) { \ | 92 if (!br_.ReadBits(num_bits, &_out)) { \ |
154 DVLOG(1) << "Error in stream: unexpected EOS while trying to read " #out; \ | 93 DVLOG(1) << "Error in stream: unexpected EOS while trying to read " #out; \ |
155 return kInvalidStream; \ | 94 return kInvalidStream; \ |
156 } \ | 95 } \ |
157 *out = _out; \ | 96 *out = _out; \ |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
351 | 290 |
352 if (!LocateNALU(stream_, bytes_left_, &off_to_nalu_start, &nalu->size)) { | 291 if (!LocateNALU(stream_, bytes_left_, &off_to_nalu_start, &nalu->size)) { |
353 DVLOG(4) << "Could not find next NALU, bytes left in stream: " | 292 DVLOG(4) << "Could not find next NALU, bytes left in stream: " |
354 << bytes_left_; | 293 << bytes_left_; |
355 return kEOStream; | 294 return kEOStream; |
356 } | 295 } |
357 | 296 |
358 nalu->data = stream_ + off_to_nalu_start; | 297 nalu->data = stream_ + off_to_nalu_start; |
359 | 298 |
360 // Initialize bit reader at the start of found NALU. | 299 // Initialize bit reader at the start of found NALU. |
361 if (!br_.Initialize(nalu->data, nalu->size)) | 300 br_.Initialize(nalu->data, nalu->size); |
301 | |
302 if (!br_.HasMoreData()) | |
362 return kEOStream; | 303 return kEOStream; |
363 | 304 |
364 DVLOG(4) << "Looking for NALU, Stream bytes left: " << bytes_left_ | 305 DVLOG(4) << "Looking for NALU, Stream bytes left: " << bytes_left_ |
365 << " off to next nalu: " << off_to_nalu_start; | 306 << " off to next nalu: " << off_to_nalu_start; |
366 | 307 |
367 // Move parser state to after this NALU, so next time AdvanceToNextNALU | 308 // Move parser state to after this NALU, so next time AdvanceToNextNALU |
368 // is called, we will effectively be skipping it; | 309 // is called, we will effectively be skipping it; |
369 // other parsing functions will use the position saved | 310 // other parsing functions will use the position saved |
370 // in bit reader for parsing, so we don't have to remember it here. | 311 // in bit reader for parsing, so we don't have to remember it here. |
371 stream_ += off_to_nalu_start + nalu->size; | 312 stream_ += off_to_nalu_start + nalu->size; |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
805 READ_SE_OR_RETURN(&pps->pic_init_qs_minus26); | 746 READ_SE_OR_RETURN(&pps->pic_init_qs_minus26); |
806 IN_RANGE_OR_RETURN(pps->pic_init_qs_minus26, -26, 25); | 747 IN_RANGE_OR_RETURN(pps->pic_init_qs_minus26, -26, 25); |
807 | 748 |
808 READ_SE_OR_RETURN(&pps->chroma_qp_index_offset); | 749 READ_SE_OR_RETURN(&pps->chroma_qp_index_offset); |
809 IN_RANGE_OR_RETURN(pps->chroma_qp_index_offset, -12, 12); | 750 IN_RANGE_OR_RETURN(pps->chroma_qp_index_offset, -12, 12); |
810 | 751 |
811 READ_BOOL_OR_RETURN(&pps->deblocking_filter_control_present_flag); | 752 READ_BOOL_OR_RETURN(&pps->deblocking_filter_control_present_flag); |
812 READ_BOOL_OR_RETURN(&pps->constrained_intra_pred_flag); | 753 READ_BOOL_OR_RETURN(&pps->constrained_intra_pred_flag); |
813 READ_BOOL_OR_RETURN(&pps->redundant_pic_cnt_present_flag); | 754 READ_BOOL_OR_RETURN(&pps->redundant_pic_cnt_present_flag); |
814 | 755 |
815 if (br_.HasMoreRBSPData()) { | 756 if (br_.HasMoreData()) { |
816 READ_BOOL_OR_RETURN(&pps->transform_8x8_mode_flag); | 757 READ_BOOL_OR_RETURN(&pps->transform_8x8_mode_flag); |
817 READ_BOOL_OR_RETURN(&pps->pic_scaling_matrix_present_flag); | 758 READ_BOOL_OR_RETURN(&pps->pic_scaling_matrix_present_flag); |
818 | 759 |
819 if (pps->pic_scaling_matrix_present_flag) { | 760 if (pps->pic_scaling_matrix_present_flag) { |
820 DVLOG(4) << "Picture scaling matrix present"; | 761 DVLOG(4) << "Picture scaling matrix present"; |
821 res = ParsePPSScalingLists(*sps, pps.get()); | 762 res = ParsePPSScalingLists(*sps, pps.get()); |
822 if (res != kOk) | 763 if (res != kOk) |
823 return res; | 764 return res; |
824 } | 765 } |
825 | 766 |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1202 | 1143 |
1203 default: | 1144 default: |
1204 DVLOG(4) << "Unsupported SEI message"; | 1145 DVLOG(4) << "Unsupported SEI message"; |
1205 break; | 1146 break; |
1206 } | 1147 } |
1207 | 1148 |
1208 return kOk; | 1149 return kOk; |
1209 } | 1150 } |
1210 | 1151 |
1211 } // namespace content | 1152 } // namespace content |
OLD | NEW |