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 <vector> 9 10 #include "base/basictypes.h" 11 #include "media/base/media_export.h" 12 13 namespace media { 14 15 namespace mp4 { 16 struct AVCDecoderConfigurationRecord; 17 } 18 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 { 23 public: 24 H264ToAnnexBBitstreamConverter(); 25 ~H264ToAnnexBBitstreamConverter(); 26 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. 30 // 31 // Parameters 32 // configuration_record 33 // Pointer to buffer containing AVCDecoderConfigurationRecord. 34 // configuration_record_size 35 // Size of the buffer in bytes. 36 // avc_config 37 // Pointer to place the parsed AVCDecoderConfigurationRecord data into. 38 // 39 // Returns 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); 47 48 // Returns the buffer size needed to store the parameter sets in |avc_config| 49 // in Annex B form. 50 uint32 GetConfigSize( 51 const mp4::AVCDecoderConfigurationRecord& avc_config) const; 52 53 // Calculates needed buffer size for the bitstream converted into bytestream. 54 // Lightweight implementation that does not do the actual conversion. 55 // 56 // Parameters 57 // input 58 // Pointer to buffer containing NAL units in MP4 format. 59 // input_size 60 // Size of the buffer in bytes. 61 // avc_config 62 // The AVCDecoderConfigurationRecord that contains the parameter sets that 63 // will be inserted into the output. NULL if no parameter sets need to be 64 // inserted. 65 // 66 // Returns 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( 71 const uint8* input, 72 uint32 input_size, 73 const mp4::AVCDecoderConfigurationRecord* avc_config) const; 74 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(). 80 // 81 // Parameters 82 // avc_config 83 // The AVCDecoderConfigurationRecord that contains the parameter sets that 84 // will be written to |output|. 85 // output 86 // Pointer to buffer where the output should be written to. 87 // output_size (i/o) 88 // Pointer to the size of the output buffer. Will contain the number of 89 // bytes written to output after successful call. 90 // 91 // Returns 92 // true if successful conversion| 93 // false if conversion not successful (|output_size| will hold the amount 94 // of converted data) 95 bool ConvertAVCDecoderConfigToByteStream( 96 const mp4::AVCDecoderConfigurationRecord& avc_config, 97 uint8* output, 98 uint32* output_size); 99 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. 104 // 105 // Parameters 106 // input 107 // Pointer to buffer containing NAL units in MP4 format. 108 // input_size 109 // Size of the buffer in bytes. 110 // avc_config 111 // The AVCDecoderConfigurationRecord that contains the parameter sets to 112 // insert into the output. NULL if no parameter sets need to be inserted. 113 // output 114 // Pointer to buffer where the output should be written to. 115 // output_size (i/o) 116 // Pointer to the size of the output buffer. Will contain the number of 117 // bytes written to output after successful call. 118 // 119 // Returns 120 // true if successful conversion 121 // false if conversion not successful (output_size will hold the amount 122 // of converted data) 123 bool ConvertNalUnitStreamToByteStream( 124 const uint8* input, 125 uint32 input_size, 126 const mp4::AVCDecoderConfigurationRecord* avc_config, 127 uint8* output, 128 uint32* output_size); 129 130 private: 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, 139 uint8** out, 140 uint32* out_size) const; 141 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_; 148 149 DISALLOW_COPY_AND_ASSIGN(H264ToAnnexBBitstreamConverter); 150 }; 151 152 } // namespace media 153 154 #endif // MEDIA_FILTERS_H264_TO_ANNEX_B_BITSTREAM_CONVERTER_H_ 155