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#ifndef MEDIA_FILTERS_H264_TO_ANNEX_B_BITSTREAM_CONVERTER_H_
6#define MEDIA_FILTERS_H264_TO_ANNEX_B_BITSTREAM_CONVERTER_H_
7
8#include "base/basictypes.h"
9#include "media/base/media_export.h"
10
11namespace media {
12
13// H264ToAnnexBBitstreamConverter is a class to convert H.264 bitstream from
14// MP4 format (as specified in ISO/IEC 14496-15) into H.264 bytestream
15// (as specified in ISO/IEC 14496-10 Annex B).
16class MEDIA_EXPORT H264ToAnnexBBitstreamConverter {
17 public:
18  H264ToAnnexBBitstreamConverter();
19  ~H264ToAnnexBBitstreamConverter();
20
21  // Parses the global AVCDecoderConfigurationRecord from the file format's
22  // headers. Converter will remember the field length from the configuration
23  // headers after this.
24  //
25  // Parameters
26  //   configuration_record
27  //     Pointer to buffer containing AVCDecoderConfigurationRecord.
28  //   configuration_record_size
29  //     Size of the buffer in bytes.
30  //
31  // Returns
32  //   Required buffer size for AVCDecoderConfigurationRecord when converted
33  //   to bytestream format, or 0 if could not determine the configuration
34  //   from the input buffer.
35  uint32 ParseConfigurationAndCalculateSize(const uint8* configuration_record,
36                                            uint32 configuration_record_size);
37
38  // Calculates needed buffer size for the bitstream converted into bytestream.
39  // Lightweight implementation that does not do the actual conversion.
40  //
41  // Parameters
42  //   configuration_record
43  //     Pointer to buffer containing AVCDecoderConfigurationRecord.
44  //   configuration_record_size
45  //     Size of the buffer in bytes.
46  //
47  // Returns
48  //   Required buffer size for the input NAL unit buffer when converted
49  //   to bytestream format, or 0 if could not determine the configuration
50  //   from the input buffer.
51  uint32 CalculateNeededOutputBufferSize(const uint8* input,
52                                         uint32 input_size) const;
53
54  // ConvertAVCDecoderConfigToByteStream converts the
55  // AVCDecoderConfigurationRecord from the MP4 headers to bytestream format.
56  // Client is responsible for making sure the output buffer is large enough
57  // to hold the output data. Client can precalculate the needed output buffer
58  // size by using ParseConfigurationAndCalculateSize.
59  //
60  // In case of failed conversion object H264BitstreamConverter may have written
61  // some bytes to buffer pointed by pinput but user should ignore those bytes.
62  // None of the outputs should be considered valid.
63  //
64  // Parameters
65  //   pinput
66  //     Pointer to buffer containing AVCDecoderConfigurationRecord.
67  //   input_size
68  //     Size of the buffer in bytes.
69  //   poutput
70  //     Pointer to buffer where the output should be written to.
71  //   poutput_size (i/o)
72  //     Pointer to the size of the output buffer. Will contain the number of
73  //     bytes written to output after successful call.
74  //
75  // Returns
76  //    true  if successful conversion
77  //    false if conversion not successful (poutput_size will hold the amount
78  //          of converted data)
79  bool ConvertAVCDecoderConfigToByteStream(const uint8* input,
80                                           uint32 input_size,
81                                           uint8* output,
82                                           uint32* output_size);
83
84  // ConvertNalUnitStreamToByteStream converts the NAL unit from MP4 format
85  // to bytestream format. Client is responsible for making sure the output
86  // buffer is large enough to hold the output data. Client can precalculate the
87  // needed output buffer size by using CalculateNeededOutputBufferSize.
88  //
89  // In case of failed conversion object H264BitstreamConverter may have written
90  // some bytes to buffer pointed by pinput but user should ignore those bytes.
91  // None of the outputs should be considered valid.
92  //
93  // Parameters
94  //   pinput
95  //     Pointer to buffer containing AVCDecoderConfigurationRecord.
96  //   input_size
97  //     Size of the buffer in bytes.
98  //   poutput
99  //     Pointer to buffer where the output should be written to.
100  //   poutput_size (i/o)
101  //     Pointer to the size of the output buffer. Will contain the number of
102  //     bytes written to output after successful call.
103  //
104  // Returns
105  //    true  if successful conversion
106  //    false if conversion not successful (poutput_size will hold the amount
107  //          of converted data)
108  bool ConvertNalUnitStreamToByteStream(const uint8* input, uint32 input_size,
109                                        uint8* output, uint32* output_size);
110
111 private:
112  // Flag for indicating whether global parameter sets have been processed.
113  bool configuration_processed_;
114  // Flag for indicating whether next NAL unit starts new access unit.
115  bool first_nal_unit_in_access_unit_;
116  // Variable to hold interleaving field's length in bytes.
117  uint8 nal_unit_length_field_width_;
118
119  DISALLOW_COPY_AND_ASSIGN(H264ToAnnexBBitstreamConverter);
120};
121
122}  // namespace media
123
124#endif  // MEDIA_FILTERS_H264_TO_ANNEX_B_BITSTREAM_CONVERTER_H_
125
126