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 "media/base/h264_bitstream_converter.h" | |
6 | |
7 #include "base/logging.h" | |
8 | |
9 namespace media { | |
10 | |
11 static const uint8 kStartCodePrefix[3] = {0, 0, 1}; | |
12 | |
13 // Helper function which determines whether NAL unit of given type marks | |
14 // access unit boundary. | |
15 static bool IsAccessUnitBoundaryNal(int nal_unit_type) { | |
16 // Check if this packet marks access unit boundary by checking the | |
17 // packet type. | |
18 if (nal_unit_type == 6 || // Supplemental enhancement information | |
19 nal_unit_type == 7 || // Picture parameter set | |
20 nal_unit_type == 8 || // Sequence parameter set | |
21 nal_unit_type == 9 || // Access unit delimiter | |
22 (nal_unit_type >= 14 && nal_unit_type <= 18)) { // Reserved types | |
23 return true; | |
24 } | |
25 return false; | |
26 } | |
27 | |
28 H264BitstreamConverter::H264BitstreamConverter() | |
29 : configuration_processed_(false), | |
30 first_nal_unit_in_access_unit_(true), | |
31 nal_unit_length_field_width_(0) { | |
32 } | |
33 | |
34 H264BitstreamConverter::~H264BitstreamConverter() {} | |
35 | |
36 uint32 H264BitstreamConverter::ParseConfigurationAndCalculateSize( | |
37 const uint8* configuration_record, | |
38 uint32 configuration_record_size) { | |
39 // FFmpeg's AVCodecContext's extradata field contains the Decoder Specific | |
40 // Information from MP4 headers that contain the H.264 SPS and PPS members. | |
41 // ISO 14496-15 Chapter 5.2.4 AVCDecoderConfigurationRecord. | |
42 // AVCConfigurationRecord must be at least 7 bytes long. | |
43 if (configuration_record == NULL || configuration_record_size < 7) { | |
44 return 0; // Error: invalid input | |
45 } | |
46 const uint8* decoder_configuration = configuration_record; | |
47 uint32 parameter_set_size_bytes = 0; | |
48 | |
49 // We can skip the four first bytes as they're only profile information | |
50 decoder_configuration += 4; | |
51 // Fifth byte's two LSBs contain the interleaving field's size minus one | |
52 uint8 size_of_len_field = (*decoder_configuration & 0x3) + 1; | |
53 if (size_of_len_field != 1 && size_of_len_field != 2 && | |
54 size_of_len_field != 4) { | |
55 return 0; // Error: invalid input, NAL unit field len is not correct | |
56 } | |
57 decoder_configuration++; | |
58 // Sixth byte's five LSBs contain the number of SPSs | |
59 uint8 sps_count = *decoder_configuration & 0x1F; | |
60 decoder_configuration++; | |
61 // Then we have N * SPS's with two byte length field and actual SPS | |
62 while (sps_count-- > 0) { | |
63 if ((decoder_configuration - configuration_record) + 2 > | |
64 static_cast<int32>(configuration_record_size)) { | |
65 return 0; // Error: ran out of data | |
66 } | |
67 uint16 sps_len = decoder_configuration[0] << 8 | decoder_configuration[1]; | |
68 decoder_configuration += 2; | |
69 // write the SPS to output, always with zero byte + start code prefix | |
70 parameter_set_size_bytes += 1 + sizeof(kStartCodePrefix); | |
71 decoder_configuration += sps_len; | |
72 parameter_set_size_bytes += sps_len; | |
73 } | |
74 // Then we have the numner of pps in one byte | |
75 uint8 pps_count = *decoder_configuration; | |
76 decoder_configuration++; | |
77 // And finally, we have N * PPS with two byte length field and actual PPS | |
78 while (pps_count-- > 0) { | |
79 if ((decoder_configuration - configuration_record) + 2 > | |
80 static_cast<int32>(configuration_record_size)) { | |
81 return 0; // Error: ran out of data | |
82 } | |
83 uint16 pps_len = decoder_configuration[0] << 8 | decoder_configuration[1]; | |
84 decoder_configuration += 2; | |
85 // write the SPS to output, always with zero byte + start code prefix | |
86 parameter_set_size_bytes += 1 + sizeof(kStartCodePrefix); | |
87 decoder_configuration += pps_len; | |
88 parameter_set_size_bytes += pps_len; | |
89 } | |
90 // We're done processing the AVCDecoderConfigurationRecord, | |
91 // store the needed information for parsing actual payload | |
92 nal_unit_length_field_width_ = size_of_len_field; | |
93 configuration_processed_ = true; | |
94 return parameter_set_size_bytes; | |
95 } | |
96 | |
97 uint32 H264BitstreamConverter::CalculateNeededOutputBufferSize( | |
98 const uint8* input, | |
99 uint32 input_size) const { | |
100 uint32 output_size = 0; | |
101 uint32 data_left = input_size; | |
102 bool first_nal_in_this_access_unit = first_nal_unit_in_access_unit_; | |
103 | |
104 if (input == NULL || input_size == 0) { | |
105 return 0; // Error: invalid input data | |
106 } | |
107 if (!configuration_processed_) { | |
108 return 0; // Error: configuration not handled, we don't know nal unit width | |
109 } | |
110 CHECK(nal_unit_length_field_width_ == 1 || | |
111 nal_unit_length_field_width_ == 2 || | |
112 nal_unit_length_field_width_ == 4); | |
113 | |
114 // Then add the needed size for the actual packet | |
115 while (data_left > 0) { | |
116 // Read the next NAL unit length from the input buffer | |
117 uint8 size_of_len_field; | |
118 uint32 nal_unit_length; | |
119 for (nal_unit_length = 0, size_of_len_field = nal_unit_length_field_width_; | |
120 size_of_len_field > 0; | |
121 input++, size_of_len_field--, data_left--) { | |
122 nal_unit_length <<= 8; | |
123 nal_unit_length |= *input; | |
124 } | |
125 | |
126 if (nal_unit_length == 0) { | |
127 break; // Signifies that no more data left in the buffer | |
128 } else if (nal_unit_length > data_left) { | |
129 return 0; // Error: Not enough data for correct conversion | |
130 } | |
131 data_left -= nal_unit_length; | |
132 | |
133 // five least significant bits of first NAL unit byte signify nal_unit_type | |
134 int nal_unit_type = *input & 0x1F; | |
135 if (first_nal_in_this_access_unit || | |
136 IsAccessUnitBoundaryNal(nal_unit_type)) { | |
137 output_size += 1; // Extra zero_byte for these nal units | |
138 first_nal_in_this_access_unit = false; | |
139 } | |
140 // Start code prefix | |
141 output_size += sizeof(kStartCodePrefix); | |
142 // Actual NAL unit size | |
143 output_size += nal_unit_length; | |
144 input += nal_unit_length; | |
145 // No need for trailing zero bits | |
146 } | |
147 return output_size; | |
148 } | |
149 | |
150 bool H264BitstreamConverter::ConvertAVCDecoderConfigurationRecordToByteStream( | |
151 const uint8* input, | |
152 uint32 input_size, | |
153 uint8* output, | |
154 uint32* output_size) { | |
155 uint8* outscan = output; | |
156 // FFmpeg's AVCodecContext's extradata field contains the Decoder Specific | |
157 // Information from MP4 headers that contain the H.264 SPS and PPS members. | |
158 // ISO 14496-15 Chapter 5.2.4 AVCDecoderConfigurationRecord. | |
159 const uint8* decoder_configuration = input; | |
160 uint32 decoderconfiguration_size = input_size; | |
161 uint32 out_size = 0; | |
162 | |
163 if (decoder_configuration == NULL || decoderconfiguration_size == 0) { | |
164 return 0; // Error: input invalid | |
165 } | |
166 | |
167 // We can skip the four first bytes as they're only profile information. | |
168 decoder_configuration += 4; | |
169 // Fifth byte's two LSBs contain the interleaving field's size minus one | |
170 uint8 size_of_len_field = (*decoder_configuration & 0x3) + 1; | |
171 if (size_of_len_field != 1 && size_of_len_field != 2 && | |
172 size_of_len_field != 4) { | |
173 return 0; // Error: invalid input, NAL unit field len is not correct | |
174 } | |
175 decoder_configuration++; | |
176 // Sixth byte's five LSBs contain the number of SPSs | |
177 uint8 sps_count = *decoder_configuration & 0x1F; | |
178 decoder_configuration++; | |
179 // Then we have N * SPS's with two byte length field and actual SPS | |
180 while (sps_count-- > 0) { | |
181 uint16 sps_len = decoder_configuration[0] << 8 | | |
182 decoder_configuration[1]; | |
183 decoder_configuration += 2; | |
184 if (out_size + 1 + sizeof(kStartCodePrefix) + sps_len > | |
185 *output_size) { | |
186 *output_size = 0; | |
187 return 0; // too small output buffer; | |
188 } | |
189 // write the SPS to output, always with zero byte + start code prefix | |
190 *outscan = 0; // zero byte | |
191 outscan += 1; | |
192 memcpy(outscan, kStartCodePrefix, sizeof(kStartCodePrefix)); | |
193 outscan += sizeof(kStartCodePrefix); | |
194 memcpy(outscan, decoder_configuration, sps_len); | |
195 decoder_configuration += sps_len; | |
196 outscan += sps_len; | |
197 out_size += 1 + sizeof(kStartCodePrefix) + sps_len; | |
198 } | |
199 // Then we have the numner of pps in one byte | |
200 uint8 pps_count = *decoder_configuration; | |
201 decoder_configuration++; | |
202 // And finally, we have N * PPS with two byte length field and actual PPS | |
203 while (pps_count-- > 0) { | |
204 uint16 pps_len = decoder_configuration[0] << 8 | decoder_configuration[1]; | |
205 decoder_configuration += 2; | |
206 if (out_size + 1 + sizeof(kStartCodePrefix) + pps_len > | |
207 *output_size) { | |
208 *output_size = 0; | |
209 return 0; // too small output buffer; | |
210 } | |
211 // write the SPS to output, always with zero byte + start code prefix | |
212 *outscan = 0; // zero byte | |
213 outscan += 1; | |
214 memcpy(outscan, kStartCodePrefix, sizeof(kStartCodePrefix)); | |
215 outscan += sizeof(kStartCodePrefix); | |
216 memcpy(outscan, decoder_configuration, pps_len); | |
217 decoder_configuration += pps_len; | |
218 outscan += pps_len; | |
219 out_size += 1 + sizeof(kStartCodePrefix) + pps_len; | |
220 } | |
221 // We're done processing the AVCDecoderConfigurationRecord, store the needed | |
222 // information | |
223 nal_unit_length_field_width_ = size_of_len_field; | |
224 configuration_processed_ = true; | |
225 *output_size = out_size; | |
226 return true; | |
227 } | |
228 | |
229 bool H264BitstreamConverter::ConvertNalUnitStreamToByteStream( | |
230 const uint8* input, uint32 input_size, | |
231 uint8* output, uint32* output_size) { | |
232 const uint8* inscan = input; // We read the input from here progressively | |
233 uint8* outscan = output; // We write the output to here progressively | |
234 uint32 data_left = input_size; | |
235 | |
236 if (inscan == NULL || input_size == 0 || | |
237 outscan == NULL || *output_size == 0) { | |
238 *output_size = 0; | |
239 return false; // Error: invalid input | |
240 } | |
241 | |
242 // NAL unit width should be known at this point | |
243 CHECK(nal_unit_length_field_width_ == 1 || | |
244 nal_unit_length_field_width_ == 2 || | |
245 nal_unit_length_field_width_ == 4); | |
246 | |
247 // Do the actual conversion for the actual input packet | |
248 while (data_left > 0) { | |
249 uint8 i; | |
250 uint32 nal_unit_length; | |
251 | |
252 // Read the next NAL unit length from the input buffer by scanning | |
253 // the input stream with the specific length field width | |
254 for (nal_unit_length = 0, i = nal_unit_length_field_width_; | |
255 i > 0 && data_left > 0; | |
256 inscan++, i--, data_left--) { | |
257 nal_unit_length <<= 8; | |
258 nal_unit_length |= *inscan; | |
259 } | |
260 | |
261 if (nal_unit_length == 0) { | |
262 break; // Successful conversion, end of buffer | |
263 } else if (nal_unit_length > data_left) { | |
264 *output_size = 0; | |
265 return false; // Error: not enough data for correct conversion | |
266 } | |
267 | |
268 uint32 start_code_len; | |
269 first_nal_unit_in_access_unit_ ? | |
270 start_code_len = sizeof(kStartCodePrefix) + 1 : | |
271 start_code_len = sizeof(kStartCodePrefix); | |
272 if (static_cast<uint32>(outscan - output) + | |
273 start_code_len + nal_unit_length > *output_size) { | |
274 *output_size = 0; | |
275 return false; // Error: too small output buffer | |
276 } | |
277 | |
278 // Five least significant bits of first NAL unit byte signify | |
279 // nal_unit_type. | |
280 int nal_unit_type = *inscan & 0x1F; | |
281 | |
282 // Check if this packet marks access unit boundary by checking the | |
283 // packet type. | |
284 if (IsAccessUnitBoundaryNal(nal_unit_type)) { | |
285 first_nal_unit_in_access_unit_ = true; | |
286 } | |
287 | |
288 // Write extra zero-byte before start code prefix if this packet | |
289 // signals next access unit. | |
290 if (first_nal_unit_in_access_unit_) { | |
291 *outscan = 0; | |
292 outscan++; | |
293 first_nal_unit_in_access_unit_ = false; | |
294 } | |
295 | |
296 // No need to write leading zero bits. | |
297 // Write start-code prefix. | |
298 memcpy(outscan, kStartCodePrefix, sizeof(kStartCodePrefix)); | |
299 outscan += sizeof(kStartCodePrefix); | |
300 // Then write the actual NAL unit from the input buffer. | |
301 memcpy(outscan, inscan, nal_unit_length); | |
302 inscan += nal_unit_length; | |
303 data_left -= nal_unit_length; | |
304 outscan += nal_unit_length; | |
305 // No need for trailing zero bits. | |
306 } | |
307 // Successful conversion, output the freshly allocated bitstream buffer. | |
308 *output_size = static_cast<uint32>(outscan - output); | |
309 return true; | |
310 } | |
311 | |
312 } // namespace media | |
OLD | NEW |