1 /*****************************************************************
2 |
3 |    AP4 - dac4 Atoms
4 |
5 |    Copyright 2002-2018 Axiomatic Systems, LLC
6 |
7 |
8 |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
9 |
10 |    Unless you have obtained Bento4 under a difference license,
11 |    this version of Bento4 is Bento4|GPL.
12 |    Bento4|GPL is free software; you can redistribute it and/or modify
13 |    it under the terms of the GNU General Public License as published by
14 |    the Free Software Foundation; either version 2, or (at your option)
15 |    any later version.
16 |
17 |    Bento4|GPL is distributed in the hope that it will be useful,
18 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
19 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 |    GNU General Public License for more details.
21 |
22 |    You should have received a copy of the GNU General Public License
23 |    along with Bento4|GPL; see the file COPYING.  If not, write to the
24 |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 |    02111-1307, USA.
26 |
27 ****************************************************************/
28 
29 #ifndef _AP4_DEC4_ATOM_H_
30 #define _AP4_DEC4_ATOM_H_
31 
32 /*----------------------------------------------------------------------
33 |   includes
34 +---------------------------------------------------------------------*/
35 #include "Ap4Atom.h"
36 #include "Ap4Utils.h"
37 #include "Ap4Ac4Utils.h"
38 
39 /*----------------------------------------------------------------------
40 |   AP4_Dac4Atom
41 +---------------------------------------------------------------------*/
42 class AP4_Dac4Atom : public AP4_Atom
43 {
44 public:
45     AP4_IMPLEMENT_DYNAMIC_CAST_D(AP4_Dac4Atom, AP4_Atom)
46 
47     // inner classes
48     struct Ac4Dsi {
49         struct Ac4BitrateDsi{
50             AP4_UI08 bit_rate_mode;
51             AP4_UI32 bit_rate;
52             AP4_UI32 bit_rate_precision;
53 
54             // methods
55             AP4_Result WriteBitrateDsi(AP4_BitWriter &bits);
56         };
57         struct Ac4AlternativeInfo{
58             AP4_UI16 name_len;
59             AP4_UI08 presentation_name[256]; // restrict to 256 char
60             AP4_UI08 n_targets;
61             AP4_UI08 target_md_compat[32];
62             AP4_UI08 target_device_category[32];
63 
64             // methods
65             AP4_Result WriteAlternativeInfo(AP4_BitWriter &bits);
66         };
67         struct SubStream{
68             AP4_UI08 b_4_back_channels_present;
69             AP4_UI08 b_centre_present;
70             AP4_UI08 top_channels_present;
71             AP4_UI08 b_lfe;
72             AP4_UI08 dsi_sf_multiplier;
73             AP4_UI08 b_substream_bitrate_indicator;
74             AP4_UI08 substream_bitrate_indicator;
75             AP4_UI08 ch_mode; // auxiliary information, used to calcuate pres_ch_mode
76             AP4_UI32 dsi_substream_channel_mask;
77             AP4_UI08 b_ajoc;
78             AP4_UI08 b_static_dmx;
79             AP4_UI08 n_dmx_objects_minus1;
80             AP4_UI08 n_umx_objects_minus1;
81             AP4_UI08 b_substream_contains_bed_objects;
82             AP4_UI08 b_substream_contains_dynamic_objects;
83             AP4_UI08 b_substream_contains_ISF_objects;
84 
85             // methods
86             AP4_Result ParseSubstreamInfoChan(AP4_BitReader &bits,
87                                               unsigned int  presentation_version,
88                                               unsigned char defalut_presentation_flag,
89                                               unsigned int  fs_idx,
90                                               unsigned int  &speaker_index_mask,
91                                               unsigned int  frame_rate_factor,
92                                               unsigned int  b_substreams_present,
93                                               unsigned char &dolby_atmos_indicator);
94 
95             AP4_Result ParseSubStreamInfoAjoc(AP4_BitReader &bits,
96                                               unsigned int  &channel_count,
97                                               unsigned char defalut_presentation_flag,
98                                               unsigned int  fs_idx,
99                                               unsigned int  frame_rate_factor,
100                                               unsigned int  b_substreams_present);
101 
102             AP4_Result ParseSubstreamInfoObj(AP4_BitReader &bits,
103                                              unsigned int  &channel_count,
104                                              unsigned char defalut_presentation_flag,
105                                              unsigned int  fs_idx,
106                                              unsigned int  frame_rate_factor,
107                                              unsigned int  b_substreams_present);
108             AP4_Result WriteSubstreamDsi    (AP4_BitWriter &bits, unsigned char b_channel_coded);
109             AP4_Result GetChModeCore        (unsigned char b_channel_coded);
110         private:
111             AP4_Result ParseChMode          (AP4_BitReader &bits, int presentationVersion, unsigned char &dolby_atmos_indicator);
112             AP4_Result ParseDsiSfMutiplier  (AP4_BitReader &bits, unsigned int fs_idx  );
113             AP4_Result BedDynObjAssignment  (AP4_BitReader &bits, unsigned int nSignals, bool is_upmix);
114             AP4_Result ParseSubstreamIdxInfo(AP4_BitReader &bits, unsigned int b_substreams_present);
115 
116             AP4_Result ParseBitrateIndicator(AP4_BitReader &bits);
117             AP4_Result ParseOamdCommonData  (AP4_BitReader &bits);
118             AP4_Result Trim                 (AP4_BitReader &bits);
119             AP4_Result BedRendeInfo         (AP4_BitReader &bits);
120             AP4_UI32   ObjNumFromIsfConfig  (unsigned char isf_config);
121             AP4_UI32   BedNumFromAssignCode (unsigned char assign_code);
122             AP4_UI32   BedNumFromNonStdMask (unsigned int  non_std_mask);
123             AP4_UI32   BedNumFromStdMask    (unsigned int  std_mask);
124         };
125         struct SubStreamGroupV1 {
126             union {
127                 struct
128                 {
129                     AP4_UI08 channel_mode;
130                     AP4_UI08 dsi_sf_multiplier;
131                     AP4_UI08 b_substream_bitrate_indicator;
132                     AP4_UI08 substream_bitrate_indicator;
133                     AP4_UI08 add_ch_base;
134                     AP4_UI08 b_content_type;
135                     AP4_UI08 content_classifier;
136                     AP4_UI08 b_language_indicator;
137                     AP4_UI08 n_language_tag_bytes;
138                     AP4_UI08 language_tag_bytes[64];
139                 }v0;
140                 struct
141                 {
142                     AP4_UI08 b_substreams_present;
143                     AP4_UI08 b_hsf_ext;
144                     AP4_UI08 b_channel_coded;
145                     AP4_UI08 n_substreams;
146                     SubStream* substreams;
147                     AP4_UI08 b_content_type;
148                     AP4_UI08 content_classifier;
149                     AP4_UI08 b_language_indicator;
150                     AP4_UI08 n_language_tag_bytes;
151                     AP4_UI08 language_tag_bytes[64]; // n_language_tag_bytes is 6 bits
152                     AP4_UI08 dolby_atmos_indicator;
153                 }v1;
154             }d;
155             AP4_Result ParseSubstreamGroupInfo(AP4_BitReader &bits,
156                                                unsigned int  bitstream_version,
157                                                unsigned int  presentation_version,
158                                                unsigned char defalut_presentation_flag,
159                                                unsigned int  frame_rate_factor,
160                                                unsigned int  fs_idx,
161                                                unsigned int  &channel_count,
162                                                unsigned int  &speaker_index_mask,
163                                                unsigned int  &b_obj_or_Ajoc);
164             AP4_Result WriteSubstreamGroupDsi(AP4_BitWriter &bits);
165         private:
166             AP4_Result ParseOamdSubstreamInfo  (AP4_BitReader &bits);
167             AP4_Result ParseHsfExtSubstreamInfo(AP4_BitReader &bits);
168             AP4_Result ParseContentType        (AP4_BitReader &bits);
169             AP4_Result WriteContentType        (AP4_BitWriter &bits);
170         };
171         struct PresentationV1 {
172             AP4_UI08 presentation_version;
173             union {
174                 struct {
175                     AP4_UI08 presentation_config;
176                     AP4_UI08 mdcompat;
177                     AP4_UI08 presentation_id;
178                     AP4_UI08 dsi_frame_rate_multiply_info;
179                     AP4_UI08 presentation_emdf_version;
180                     AP4_UI16 presentation_key_id;
181                     AP4_UI32 presentation_channel_mask;
182                     // more fields omitted, v0 is deprecated.
183                 } v0;
184                 struct {
185                     AP4_UI08 presentation_config_v1;
186                     AP4_UI08 mdcompat;
187                     AP4_UI08 b_presentation_id;
188                     AP4_UI08 presentation_id;
189                     AP4_UI08 dsi_frame_rate_multiply_info;
190                     AP4_UI08 dsi_frame_rate_fraction_info;
191                     AP4_UI08 presentation_emdf_version;
192                     AP4_UI16 presentation_key_id;
193                     AP4_UI08 b_presentation_channel_coded;
194                     AP4_UI08 dsi_presentation_ch_mode;
195                     AP4_UI08 pres_b_4_back_channels_present;
196                     AP4_UI08 pres_top_channel_pairs;
197                     AP4_UI32 presentation_channel_mask_v1;
198                     AP4_UI08 b_presentation_core_differs;
199                     AP4_UI08 b_presentation_core_channel_coded;
200                     AP4_UI08 dsi_presentation_channel_mode_core;
201                     AP4_UI08 b_presentation_filter;
202                     AP4_UI08 b_enable_presentation;
203                     AP4_UI08 n_filter_bytes;
204 
205                     AP4_UI08 b_multi_pid;
206                     AP4_UI08 n_substream_groups;
207                     SubStreamGroupV1* substream_groups;
208                     AP4_UI32* substream_group_indexs; // auxiliary information, not exist in DSI
209                     AP4_UI08 n_skip_bytes;
210 
211                     AP4_UI08 b_pre_virtualized;
212                     AP4_UI08 b_add_emdf_substreams;
213                     AP4_UI08 n_add_emdf_substreams;
214                     AP4_UI08 substream_emdf_version[128]; // n_add_emdf_substreams - 7 bits
215                     AP4_UI16 substream_key_id[128];
216 
217                     AP4_UI08 b_presentation_bitrate_info;
218                     Ac4BitrateDsi ac4_bitrate_dsi;
219                     AP4_UI08 b_alternative;
220                     Ac4AlternativeInfo alternative_info;
221                     AP4_UI08 de_indicator;
222                     AP4_UI08 dolby_atmos_indicator;
223                     AP4_UI08 b_extended_presentation_id;
224                     AP4_UI16 extended_presentation_id;
225                 } v1;
226             } d;
227             AP4_Result ParsePresentationV1Info(AP4_BitReader &bits,
228                                                unsigned int  bitstream_version,
229                                                unsigned int  frame_rate_idx,
230                                                unsigned int  pres_idx,
231                                                unsigned int  &max_group_index,
232                                                unsigned int  **first_pres_sg_index,
233                                                unsigned int  &first_pres_sg_num);
234             AP4_Result WritePresentationV1Dsi(AP4_BitWriter &bits);
235         private:
236             AP4_Result ParsePresentationVersion      (AP4_BitReader &bits, unsigned int bitstream_version);
237             AP4_Result ParsePresentationConfigExtInfo(AP4_BitReader &bits, unsigned int bitstream_version);
238             AP4_UI32   ParseAc4SgiSpecifier          (AP4_BitReader &bits, unsigned int bitstream_version) ;
239             AP4_Result ParseDSIFrameRateMultiplyInfo (AP4_BitReader &bits, unsigned int frame_rate_idx);
240             AP4_Result ParseDSIFrameRateFractionsInfo(AP4_BitReader &bits, unsigned int frame_rate_idx);
241             AP4_Result ParseEmdInfo                  (AP4_BitReader &bits, AP4_Ac4EmdfInfo &emdf_info);
242             AP4_Result ParsePresentationSubstreamInfo(AP4_BitReader &bits);
243             AP4_Result GetPresentationChMode();
244             AP4_Result GetPresentationChannelMask();
245             AP4_Result GetPresB4BackChannelsPresent();
246             AP4_Result GetPresTopChannelPairs();
247             AP4_Result GetBPresentationCoreDiffers();
248         };
249 
250         AP4_UI08 ac4_dsi_version;
251         union {
252             struct {
253                 AP4_UI08 bitstream_version;
254                 AP4_UI08 fs_index;
255                 AP4_UI32 fs;
256                 AP4_UI08 frame_rate_index;
257                 AP4_UI16 n_presentations;
258                 // more fields from `ac4_dsi` are not included here, v0 is deprecated.
259             } v0;
260             struct {
261                 AP4_UI08 bitstream_version;
262                 AP4_UI08 fs_index;
263                 AP4_UI32 fs;
264                 AP4_UI08 frame_rate_index;
265                 AP4_UI08 b_program_id;
266                 AP4_UI16 short_program_id;
267                 AP4_UI08 b_uuid;
268                 AP4_UI08 program_uuid[16];
269                 Ac4BitrateDsi ac4_bitrate_dsi;
270                 AP4_UI16        n_presentations;
271                 PresentationV1* presentations;
272             } v1;
273         } d;
274     };
275 
276     // class methods
277     static AP4_Dac4Atom* Create(AP4_Size size, AP4_ByteStream& stream);
278 
279     // constructors
280     AP4_Dac4Atom(AP4_UI32 size, const Ac4Dsi* ac4Dsi);  // DSI vaiable initialize m_RawBytes (m_Dsi -> m_RawBytes)
281 
282     // destructor
283     ~AP4_Dac4Atom();
284 
285     // methods
286     virtual AP4_Result    InspectFields(AP4_AtomInspector& inspector);
287     virtual AP4_Result    WriteFields(AP4_ByteStream& stream);
Clone()288     virtual AP4_Atom*     Clone() { return new AP4_Dac4Atom(m_Size32, m_RawBytes.GetData()); }
CloneConst()289     virtual AP4_Dac4Atom* CloneConst() const { return new AP4_Dac4Atom(m_Size32, m_RawBytes.GetData()); }
290 
291     // accessors
GetRawBytes()292     const AP4_DataBuffer& GetRawBytes() const { return m_RawBytes;   }
GetDsi()293     const Ac4Dsi&         GetDsi()      const { return m_Dsi;        }
294 
295     // helpers
296     void GetCodecString(AP4_String& codec);
297 
298 private:
299     // methods
300     AP4_Dac4Atom(const AP4_Dac4Atom& other);
301     AP4_Dac4Atom(AP4_UI32 size, const AP4_UI08* payload); // box data initialize m_Dsi (m_RawBytes -> m_Dsi)
302 
303     // members
304     AP4_DataBuffer m_RawBytes;
305     Ac4Dsi         m_Dsi;
306 };
307 
308 #endif // _AP4_DAC4_ATOM_H_
309