[M120 Migration][hbbtv] Audio tracks count notification
[platform/framework/web/chromium-efl.git] / media / filters / h265_to_annex_b_bitstream_converter.h
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.
4
5 #ifndef MEDIA_FILTERS_H265_TO_ANNEX_B_BITSTREAM_CONVERTER_H_
6 #define MEDIA_FILTERS_H265_TO_ANNEX_B_BITSTREAM_CONVERTER_H_
7
8 #include <stdint.h>
9
10 #include <vector>
11
12 #include "media/base/media_export.h"
13
14 namespace media {
15
16 namespace mp4 {
17 struct HEVCDecoderConfigurationRecord;
18 }
19
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 {
24  public:
25   H265ToAnnexBBitstreamConverter();
26
27   H265ToAnnexBBitstreamConverter(const H265ToAnnexBBitstreamConverter&) =
28       delete;
29   H265ToAnnexBBitstreamConverter& operator=(
30       const H265ToAnnexBBitstreamConverter&) = delete;
31
32   ~H265ToAnnexBBitstreamConverter();
33
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.
37   //
38   // Parameters
39   //   configuration_record
40   //     Pointer to buffer containing HEVCDecoderConfigurationRecord.
41   //   configuration_record_size
42   //     Size of the buffer in bytes.
43   //   hevc_config
44   //     Pointer to place the parsed HEVCDecoderConfigurationRecord data into.
45   //
46   // Returns
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);
53
54   // Returns the buffer size needed to store the parameter sets in |hevc_config|
55   // in Annex B form.
56   uint32_t GetConfigSize(
57       const mp4::HEVCDecoderConfigurationRecord& hevc_config) const;
58
59   // Calculates needed buffer size for the bitstream converted into bytestream.
60   // Lightweight implementation that does not do the actual conversion.
61   //
62   // Parameters
63   //   input
64   //     Pointer to buffer containing NAL units in MP4 format.
65   //   input_size
66   //     Size of the buffer in bytes.
67   //   hevc_config
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.
71   //
72   // Returns
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(
77       const uint8_t* input,
78       uint32_t input_size,
79       const mp4::HEVCDecoderConfigurationRecord* hevc_config) const;
80
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().
86   //
87   // Parameters
88   //   hevc_config
89   //     The HEVCDecoderConfigurationRecord that contains the parameter sets
90   //     that will be written to |output|.
91   //   output
92   //     Pointer to buffer where the output should be written to.
93   //   output_size (i/o)
94   //     Pointer to the size of the output buffer. Will contain the number of
95   //     bytes written to output after successful call.
96   //
97   // Returns
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,
103       uint8_t* output,
104       uint32_t* output_size);
105
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.
110   //
111   // Parameters
112   //   input
113   //     Pointer to buffer containing NAL units in MP4 format.
114   //   input_size
115   //     Size of the buffer in bytes.
116   //   hevc_config
117   //     The HEVCDecoderConfigurationRecord that contains the parameter sets to
118   //     insert into the output. NULL if no parameter sets need to be inserted.
119   //   output
120   //     Pointer to buffer where the output should be written to.
121   //   output_size (i/o)
122   //     Pointer to the size of the output buffer. Will contain the number of
123   //     bytes written to output after successful call.
124   //
125   // Returns
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,
131       uint32_t input_size,
132       const mp4::HEVCDecoderConfigurationRecord* hevc_config,
133       uint8_t* output,
134       uint32_t* output_size);
135
136  private:
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,
145                      uint8_t** out,
146                      uint32_t* out_size) const;
147
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;
152 };
153
154 }  // namespace media
155
156 #endif  // MEDIA_FILTERS_H265_TO_ANNEX_B_BITSTREAM_CONVERTER_H_