1 // Copyright 2022 The Chromium Authors
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_H265_TO_ANNEX_B_BITSTREAM_CONVERTER_H_
6 #define MEDIA_FILTERS_H265_TO_ANNEX_B_BITSTREAM_CONVERTER_H_
12 #include "media/base/media_export.h"
17 struct HEVCDecoderConfigurationRecord;
20 // H265ToAnnexBBitstreamConverter is a class to convert H.265 bitstream from
21 // MP4 format (as specified in ISO/IEC 14496-15) into H.265 bytestream
22 // (as specified in ISO/IEC 14496-10 Annex B).
23 class MEDIA_EXPORT H265ToAnnexBBitstreamConverter {
25 H265ToAnnexBBitstreamConverter();
27 H265ToAnnexBBitstreamConverter(const H265ToAnnexBBitstreamConverter&) =
29 H265ToAnnexBBitstreamConverter& operator=(
30 const H265ToAnnexBBitstreamConverter&) = delete;
32 ~H265ToAnnexBBitstreamConverter();
34 // Parses the global HEVCDecoderConfigurationRecord from the file format's
35 // headers. Converter will remember the field length from the configuration
36 // headers after this.
39 // configuration_record
40 // Pointer to buffer containing HEVCDecoderConfigurationRecord.
41 // configuration_record_size
42 // Size of the buffer in bytes.
44 // Pointer to place the parsed HEVCDecoderConfigurationRecord data into.
47 // Returns true if |configuration_record| was successfully parsed. False
48 // is returned if a parsing error occurred.
49 // |hevc_config| only contains valid data when true is returned.
50 bool ParseConfiguration(const uint8_t* configuration_record,
51 int configuration_record_size,
52 mp4::HEVCDecoderConfigurationRecord* hevc_config);
54 // Returns the buffer size needed to store the parameter sets in |hevc_config|
56 uint32_t GetConfigSize(
57 const mp4::HEVCDecoderConfigurationRecord& hevc_config) const;
59 // Calculates needed buffer size for the bitstream converted into bytestream.
60 // Lightweight implementation that does not do the actual conversion.
64 // Pointer to buffer containing NAL units in MP4 format.
66 // Size of the buffer in bytes.
68 // The HEVCDecoderConfigurationRecord that contains the parameter sets
69 // that will be inserted into the output. nullptr if no parameter sets
70 // need to be inserted.
73 // Required buffer size for the output NAL unit buffer when converted
74 // to bytestream format, or 0 if could not determine the size of
75 // the output buffer from the data in |input| and |hevc_config|.
76 uint32_t CalculateNeededOutputBufferSize(
79 const mp4::HEVCDecoderConfigurationRecord* hevc_config) const;
81 // ConvertHEVCDecoderConfigToByteStream converts the
82 // HEVCDecoderConfigurationRecord from the MP4 headers to bytestream format.
83 // Client is responsible for making sure the output buffer is large enough
84 // to hold the output data. Client can precalculate the needed output buffer
85 // size by using GetConfigSize().
89 // The HEVCDecoderConfigurationRecord that contains the parameter sets
90 // that will be written to |output|.
92 // Pointer to buffer where the output should be written to.
94 // Pointer to the size of the output buffer. Will contain the number of
95 // bytes written to output after successful call.
98 // true if successful conversion|
99 // false if conversion not successful (|output_size| will hold the amount
100 // of converted data)
101 bool ConvertHEVCDecoderConfigToByteStream(
102 const mp4::HEVCDecoderConfigurationRecord& hevc_config,
104 uint32_t* output_size);
106 // ConvertNalUnitStreamToByteStream converts the NAL unit from MP4 format
107 // to bytestream format. Client is responsible for making sure the output
108 // buffer is large enough to hold the output data. Client can precalculate the
109 // needed output buffer size by using CalculateNeededOutputBufferSize.
113 // Pointer to buffer containing NAL units in MP4 format.
115 // Size of the buffer in bytes.
117 // The HEVCDecoderConfigurationRecord that contains the parameter sets to
118 // insert into the output. NULL if no parameter sets need to be inserted.
120 // Pointer to buffer where the output should be written to.
122 // Pointer to the size of the output buffer. Will contain the number of
123 // bytes written to output after successful call.
126 // true if successful conversion
127 // false if conversion not successful (output_size will hold the amount
128 // of converted data)
129 bool ConvertNalUnitStreamToByteStream(
130 const uint8_t* input,
132 const mp4::HEVCDecoderConfigurationRecord* hevc_config,
134 uint32_t* output_size);
137 // Writes Annex B start code and |param_set| to |*out|.
138 // |*out| - Is the memory location to write the parameter set.
139 // |*out_size| - Number of bytes available for the parameter set.
140 // Returns true if the start code and param set were successfully
141 // written. On a successful write, |*out| is updated to point to the first
142 // byte after the data that was written. |*out_size| is updated to reflect
143 // the new number of bytes left in |*out|.
144 bool WriteParamSet(const std::vector<uint8_t>& param_set,
146 uint32_t* out_size) const;
148 // Flag for indicating whether global parameter sets have been processed.
149 bool configuration_processed_ = false;
150 // Flag for indicating whether next NAL unit starts new access unit.
151 bool first_nal_unit_in_access_unit_ = false;
156 #endif // MEDIA_FILTERS_H265_TO_ANNEX_B_BITSTREAM_CONVERTER_H_