- add sources.
[platform/framework/web/crosswalk.git] / src / media / filters / h264_to_annex_b_bitstream_converter.cc
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/filters/h264_to_annex_b_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 H264ToAnnexBBitstreamConverter::H264ToAnnexBBitstreamConverter()
29     : configuration_processed_(false),
30       first_nal_unit_in_access_unit_(true),
31       nal_unit_length_field_width_(0) {
32 }
33
34 H264ToAnnexBBitstreamConverter::~H264ToAnnexBBitstreamConverter() {}
35
36 uint32 H264ToAnnexBBitstreamConverter::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 H264ToAnnexBBitstreamConverter::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     if (data_left < nal_unit_length_field_width_) {
117       return 0;  // Error: not enough data for correct conversion.
118     }
119
120     // Read the next NAL unit length from the input buffer
121     uint8 size_of_len_field;
122     uint32 nal_unit_length;
123     for (nal_unit_length = 0, size_of_len_field = nal_unit_length_field_width_;
124          size_of_len_field > 0;
125          input++, size_of_len_field--, data_left--) {
126       nal_unit_length <<= 8;
127       nal_unit_length |= *input;
128     }
129
130     if (nal_unit_length == 0) {
131       break;  // Signifies that no more data left in the buffer
132     } else if (nal_unit_length > data_left) {
133       return 0;  // Error: Not enough data for correct conversion
134     }
135     data_left -= nal_unit_length;
136
137     // five least significant bits of first NAL unit byte signify nal_unit_type
138     int nal_unit_type = *input & 0x1F;
139     if (first_nal_in_this_access_unit ||
140         IsAccessUnitBoundaryNal(nal_unit_type)) {
141       output_size += 1;  // Extra zero_byte for these nal units
142       first_nal_in_this_access_unit = false;
143     }
144     // Start code prefix
145     output_size += sizeof(kStartCodePrefix);
146     // Actual NAL unit size
147     output_size += nal_unit_length;
148     input += nal_unit_length;
149     // No need for trailing zero bits
150   }
151   return output_size;
152 }
153
154 bool H264ToAnnexBBitstreamConverter::ConvertAVCDecoderConfigToByteStream(
155     const uint8* input,
156     uint32 input_size,
157     uint8* output,
158     uint32* output_size) {
159   uint8* outscan = output;
160   // FFmpeg's AVCodecContext's extradata field contains the Decoder Specific
161   // Information from MP4 headers that contain the H.264 SPS and PPS members.
162   // ISO 14496-15 Chapter 5.2.4 AVCDecoderConfigurationRecord.
163   const uint8* decoder_configuration = input;
164   uint32 decoderconfiguration_size = input_size;
165   uint32 out_size = 0;
166
167   if (decoder_configuration == NULL || decoderconfiguration_size == 0) {
168     return 0;  // Error: input invalid
169   }
170
171   // We can skip the four first bytes as they're only profile information.
172   decoder_configuration += 4;
173   // Fifth byte's two LSBs contain the interleaving field's size minus one
174   uint8 size_of_len_field = (*decoder_configuration & 0x3) + 1;
175   if (size_of_len_field != 1 && size_of_len_field != 2 &&
176       size_of_len_field != 4) {
177     return 0;  // Error: invalid input, NAL unit field len is not correct
178   }
179   decoder_configuration++;
180   // Sixth byte's five LSBs contain the number of SPSs
181   uint8 sps_count = *decoder_configuration & 0x1F;
182   decoder_configuration++;
183   // Then we have N * SPS's with two byte length field and actual SPS
184   while (sps_count-- > 0) {
185     uint16 sps_len = decoder_configuration[0] << 8 |
186                      decoder_configuration[1];
187     decoder_configuration += 2;
188     if (out_size + 1 + sizeof(kStartCodePrefix) + sps_len >
189         *output_size) {
190       *output_size = 0;
191       return 0;  // too small output buffer;
192     }
193     // write the SPS to output, always with zero byte + start code prefix
194     *outscan = 0;  // zero byte
195     outscan += 1;
196     memcpy(outscan, kStartCodePrefix, sizeof(kStartCodePrefix));
197     outscan += sizeof(kStartCodePrefix);
198     memcpy(outscan, decoder_configuration, sps_len);
199     decoder_configuration += sps_len;
200     outscan += sps_len;
201     out_size += 1 + sizeof(kStartCodePrefix) + sps_len;
202   }
203   // Then we have the numner of pps in one byte
204   uint8 pps_count = *decoder_configuration;
205   decoder_configuration++;
206   // And finally, we have N * PPS with two byte length field and actual PPS
207   while (pps_count-- > 0) {
208     uint16 pps_len = decoder_configuration[0] << 8 | decoder_configuration[1];
209     decoder_configuration += 2;
210     if (out_size + 1 + sizeof(kStartCodePrefix) + pps_len >
211         *output_size) {
212       *output_size = 0;
213       return 0;  // too small output buffer;
214     }
215     // write the SPS to output, always with zero byte + start code prefix
216     *outscan = 0;  // zero byte
217     outscan += 1;
218     memcpy(outscan, kStartCodePrefix, sizeof(kStartCodePrefix));
219     outscan += sizeof(kStartCodePrefix);
220     memcpy(outscan, decoder_configuration, pps_len);
221     decoder_configuration += pps_len;
222     outscan += pps_len;
223     out_size += 1 + sizeof(kStartCodePrefix) + pps_len;
224   }
225   // We're done processing the AVCDecoderConfigurationRecord, store the needed
226   // information
227   nal_unit_length_field_width_ = size_of_len_field;
228   configuration_processed_ = true;
229   *output_size = out_size;
230   return true;
231 }
232
233 bool H264ToAnnexBBitstreamConverter::ConvertNalUnitStreamToByteStream(
234     const uint8* input, uint32 input_size,
235     uint8* output, uint32* output_size) {
236   const uint8* inscan = input;  // We read the input from here progressively
237   uint8* outscan = output;  // We write the output to here progressively
238   uint32 data_left = input_size;
239
240   if (inscan == NULL || input_size == 0 ||
241       outscan == NULL || *output_size == 0) {
242     *output_size = 0;
243     return false;  // Error: invalid input
244   }
245
246   // NAL unit width should be known at this point
247   CHECK(nal_unit_length_field_width_ == 1 ||
248         nal_unit_length_field_width_ == 2 ||
249         nal_unit_length_field_width_ == 4);
250
251   // Do the actual conversion for the actual input packet
252   while (data_left > 0) {
253     uint8 i;
254     uint32 nal_unit_length;
255
256     // Read the next NAL unit length from the input buffer by scanning
257     // the input stream with the specific length field width
258     for (nal_unit_length = 0, i = nal_unit_length_field_width_;
259          i > 0 && data_left > 0;
260          inscan++, i--, data_left--) {
261       nal_unit_length <<= 8;
262       nal_unit_length |= *inscan;
263     }
264
265     if (nal_unit_length == 0) {
266       break;  // Successful conversion, end of buffer
267     } else if (nal_unit_length > data_left) {
268       *output_size = 0;
269       return false;  // Error: not enough data for correct conversion
270     }
271
272     uint32 start_code_len;
273     first_nal_unit_in_access_unit_ ?
274         start_code_len = sizeof(kStartCodePrefix) + 1 :
275         start_code_len = sizeof(kStartCodePrefix);
276     if (static_cast<uint32>(outscan - output) +
277         start_code_len + nal_unit_length > *output_size) {
278       *output_size = 0;
279       return false;  // Error: too small output buffer
280     }
281
282     // Five least significant bits of first NAL unit byte signify
283     // nal_unit_type.
284     int nal_unit_type = *inscan & 0x1F;
285
286     // Check if this packet marks access unit boundary by checking the
287     // packet type.
288     if (IsAccessUnitBoundaryNal(nal_unit_type)) {
289       first_nal_unit_in_access_unit_ = true;
290     }
291
292     // Write extra zero-byte before start code prefix if this packet
293     // signals next access unit.
294     if (first_nal_unit_in_access_unit_) {
295       *outscan = 0;
296       outscan++;
297       first_nal_unit_in_access_unit_ = false;
298     }
299
300     // No need to write leading zero bits.
301     // Write start-code prefix.
302     memcpy(outscan, kStartCodePrefix, sizeof(kStartCodePrefix));
303     outscan += sizeof(kStartCodePrefix);
304     // Then write the actual NAL unit from the input buffer.
305     memcpy(outscan, inscan, nal_unit_length);
306     inscan += nal_unit_length;
307     data_left -= nal_unit_length;
308     outscan += nal_unit_length;
309     // No need for trailing zero bits.
310   }
311   // Successful conversion, output the freshly allocated bitstream buffer.
312   *output_size = static_cast<uint32>(outscan - output);
313   return true;
314 }
315
316 }  // namespace media