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.
5 #ifndef MEDIA_FILTERS_H264_TO_ANNEX_B_BITSTREAM_CONVERTER_H_
6 #define MEDIA_FILTERS_H264_TO_ANNEX_B_BITSTREAM_CONVERTER_H_
10 #include "base/basictypes.h"
11 #include "media/base/media_export.h"
16 struct AVCDecoderConfigurationRecord
;
19 // H264ToAnnexBBitstreamConverter is a class to convert H.264 bitstream from
20 // MP4 format (as specified in ISO/IEC 14496-15) into H.264 bytestream
21 // (as specified in ISO/IEC 14496-10 Annex B).
22 class MEDIA_EXPORT H264ToAnnexBBitstreamConverter
{
24 H264ToAnnexBBitstreamConverter();
25 ~H264ToAnnexBBitstreamConverter();
27 // Parses the global AVCDecoderConfigurationRecord from the file format's
28 // headers. Converter will remember the field length from the configuration
29 // headers after this.
32 // configuration_record
33 // Pointer to buffer containing AVCDecoderConfigurationRecord.
34 // configuration_record_size
35 // Size of the buffer in bytes.
37 // Pointer to place the parsed AVCDecoderConfigurationRecord data into.
40 // Returns true if |configuration_record| was successfully parsed. False
41 // is returned if a parsing error occurred.
42 // |avc_config| only contains valid data when true is returned.
43 bool ParseConfiguration(
44 const uint8
* configuration_record
,
45 int configuration_record_size
,
46 mp4::AVCDecoderConfigurationRecord
* avc_config
);
48 // Returns the buffer size needed to store the parameter sets in |avc_config|
51 const mp4::AVCDecoderConfigurationRecord
& avc_config
) const;
53 // Calculates needed buffer size for the bitstream converted into bytestream.
54 // Lightweight implementation that does not do the actual conversion.
58 // Pointer to buffer containing NAL units in MP4 format.
60 // Size of the buffer in bytes.
62 // The AVCDecoderConfigurationRecord that contains the parameter sets that
63 // will be inserted into the output. NULL if no parameter sets need to be
67 // Required buffer size for the output NAL unit buffer when converted
68 // to bytestream format, or 0 if could not determine the size of
69 // the output buffer from the data in |input| and |avc_config|.
70 uint32
CalculateNeededOutputBufferSize(
73 const mp4::AVCDecoderConfigurationRecord
* avc_config
) const;
75 // ConvertAVCDecoderConfigToByteStream converts the
76 // AVCDecoderConfigurationRecord from the MP4 headers to bytestream format.
77 // Client is responsible for making sure the output buffer is large enough
78 // to hold the output data. Client can precalculate the needed output buffer
79 // size by using GetConfigSize().
83 // The AVCDecoderConfigurationRecord that contains the parameter sets that
84 // will be written to |output|.
86 // Pointer to buffer where the output should be written to.
88 // Pointer to the size of the output buffer. Will contain the number of
89 // bytes written to output after successful call.
92 // true if successful conversion|
93 // false if conversion not successful (|output_size| will hold the amount
95 bool ConvertAVCDecoderConfigToByteStream(
96 const mp4::AVCDecoderConfigurationRecord
& avc_config
,
100 // ConvertNalUnitStreamToByteStream converts the NAL unit from MP4 format
101 // to bytestream format. Client is responsible for making sure the output
102 // buffer is large enough to hold the output data. Client can precalculate the
103 // needed output buffer size by using CalculateNeededOutputBufferSize.
107 // Pointer to buffer containing NAL units in MP4 format.
109 // Size of the buffer in bytes.
111 // The AVCDecoderConfigurationRecord that contains the parameter sets to
112 // insert into the output. NULL if no parameter sets need to be inserted.
114 // Pointer to buffer where the output should be written to.
116 // Pointer to the size of the output buffer. Will contain the number of
117 // bytes written to output after successful call.
120 // true if successful conversion
121 // false if conversion not successful (output_size will hold the amount
122 // of converted data)
123 bool ConvertNalUnitStreamToByteStream(
126 const mp4::AVCDecoderConfigurationRecord
* avc_config
,
128 uint32
* output_size
);
131 // Writes Annex B start code and |param_set| to |*out|.
132 // |*out| - Is the memory location to write the parameter set.
133 // |*out_size| - Number of bytes available for the parameter set.
134 // Returns true if the start code and param set were successfully
135 // written. On a successful write, |*out| is updated to point to the first
136 // byte after the data that was written. |*out_size| is updated to reflect
137 // the new number of bytes left in |*out|.
138 bool WriteParamSet(const std::vector
<uint8
>& param_set
,
140 uint32
* out_size
) const;
142 // Flag for indicating whether global parameter sets have been processed.
143 bool configuration_processed_
;
144 // Flag for indicating whether next NAL unit starts new access unit.
145 bool first_nal_unit_in_access_unit_
;
146 // Variable to hold interleaving field's length in bytes.
147 uint8 nal_unit_length_field_width_
;
149 DISALLOW_COPY_AND_ASSIGN(H264ToAnnexBBitstreamConverter
);
154 #endif // MEDIA_FILTERS_H264_TO_ANNEX_B_BITSTREAM_CONVERTER_H_