1 /*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
2  *
3  *  Use of this source code is governed by a BSD-style license that can
4  *  be found in the License.html file in the root of the source tree.
5  */
6 
7 //---------------------------------------------------------------------------
8 // Pre-compilation
9 #include "MediaInfo/PreComp.h"
10 #ifdef __BORLANDC__
11     #pragma hdrstop
12 #endif
13 //---------------------------------------------------------------------------
14 
15 //---------------------------------------------------------------------------
16 #include "MediaInfo/Setup.h"
17 //---------------------------------------------------------------------------
18 
19 //---------------------------------------------------------------------------
20 #if defined(MEDIAINFO_AAC_YES)
21 //---------------------------------------------------------------------------
22 
23 //---------------------------------------------------------------------------
24 #include "MediaInfo/Audio/File_Aac.h"
25 #if MEDIAINFO_ADVANCED
26     #include "MediaInfo/MediaInfo_Config_MediaInfo.h"
27 #endif //MEDIAINFO_ADVANCED
28 #include <cmath>
29 using namespace std;
30 //---------------------------------------------------------------------------
31 
32 namespace MediaInfoLib
33 {
34 
35 //***************************************************************************
36 // Infos
37 //***************************************************************************
38 
39 //---------------------------------------------------------------------------
40 extern const size_t Aac_sampling_frequency_Size=13;
41 extern const size_t Aac_sampling_frequency_Size_Usac=31; // USAC expands Aac_sampling_frequency[]
42 extern const int32u Aac_sampling_frequency[Aac_sampling_frequency_Size_Usac]=
43 {96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
44  16000, 12000, 11025,  8000,  7350,     0,     0, 57600,
45  51200, 40000, 38400, 34150, 28800, 25600, 20000, 19200,
46  17075, 14400, 12800,  9600,     0,     0,     0};
47 
48 //---------------------------------------------------------------------------
49 static const char* Aac_Adts_ID[]=
50 {
51     "MPEG-4",
52     "MPEG-2",
53 };
54 
55 //---------------------------------------------------------------------------
Aac_Format(int8u ID)56 static const char* Aac_Format(int8u ID)
57 {
58     switch (ID)
59     {
60         case    1 :
61         case    2 :
62         case    3 :
63         case    4 : return "AAC";
64         case    5 : return "SBR";
65         case    6 : return "AAC scalable";
66         case    7 : return "TwinVQ";
67         case    8 : return "CELP";
68         case    9 : return "HVXC";
69         case   12 : return "TTSI";
70         case   13 : return "Main synthetic";
71         case   14 : return "Wavetable synthesis";
72         case   15 : return "General MIDI";
73         case   16 : return "Algorithmic Synthesis and Audio FX";
74         case   17 :
75         case   19 :
76         case   20 : return "ER AAC";
77         case   21 : return "ER TwinVQ";
78         case   22 : return "ER BSAC";
79         case   23 : return "ER AAC LD";
80         case   24 : return "ER CELP";
81         case   25 : return "ER HVXC";
82         case   26 : return "ER HILN";
83         case   27 : return "ER Parametric";
84         case   28 : return "SSC";
85         case   29 : return "ParametricStereo";
86         case   32 : return "Layer-1";
87         case   33 : return "Layer-2";
88         case   34 : return "Layer-3";
89         case   35 : return "DST";
90         case   36 : return "ALS";
91         case   37 :
92         case   38 : return "SLS";
93         case   39 : return "ER AAC ELD";
94         case   40 : return "SMR Simple";
95         case   41 : return "SMR Main";
96         case   42 : return "USAC";
97         default   : return "";
98     }
99 }
100 
101 //---------------------------------------------------------------------------
Aac_Format_Profile(int8u ID)102 const char* Aac_Format_Profile(int8u ID)
103 {
104     switch (ID)
105     {
106         case    1 : return "Main";
107         case    2 : return "LC";
108         case    3 : return "SSR";
109         case    4 : return "LTP";
110         case   17 : return "LC";
111         case   19 : return "LTP";
112         case   37 : return "non-core";
113         default   : return "";
114     }
115 }
116 
117 //---------------------------------------------------------------------------
Aac_audioObjectType(int8u audioObjectType)118 const char* Aac_audioObjectType(int8u audioObjectType)
119 {
120     switch (audioObjectType)
121     {
122         case    1 : return "AAC Main";
123         case    2 : return "AAC LC";
124         case    3 : return "AAC SSR";
125         case    4 : return "AAC LTP";
126         case    5 : return "SBR";
127         case    6 : return "AAC scalable";
128         case    7 : return "TwinVQ";
129         case    8 : return "CELP";
130         case    9 : return "HVXC";
131         case   12 : return "TTSI";
132         case   13 : return "Main synthetic";
133         case   14 : return "Wavetable synthesis";
134         case   15 : return "General MIDI";
135         case   16 : return "Algorithmic Synthesis and Audio FX";
136         case   17 : return "ER AAC LC";
137         case   19 : return "ER AAC LTP";
138         case   20 : return "ER AAC scalable";
139         case   21 : return "ER TwinVQ";
140         case   22 : return "ER BSAC";
141         case   23 : return "ER AAC LD";
142         case   24 : return "ER CELP";
143         case   25 : return "ER HVXC";
144         case   26 : return "ER HILN";
145         case   27 : return "ER Parametric";
146         case   28 : return "SSC";
147         case   29 : return "PS";
148         case   31 : return "(escape)";
149         case   32 : return "Layer-1";
150         case   33 : return "Layer-2";
151         case   34 : return "Layer-3";
152         case   35 : return "DST";
153         case   36 : return "ALS";
154         case   37 : return "SLS";
155         case   38 : return "SLS non-core";
156         case   39 : return "ER AAC ELD";
157         case   40 : return "SMR Simple";
158         case   41 : return "SMR Main";
159         case   42 : return "USAC";
160         default   : return "";
161     }
162 }
163 
164 //---------------------------------------------------------------------------
165 static const int8u Aac_Channels_Size_Usac=14;
166 static const int8u Aac_Channels_Size=21;
167 static const int8u Aac_Channels[Aac_Channels_Size]=
168 {
169     0,
170     1,
171     2,
172     3,
173     4,
174     5,
175     6,
176     8,
177     //AudioSpecificConfig
178     2,
179     3,
180     4,
181     7,
182     8,
183     24,
184     //MPEG-H 3D Audio
185     8,
186     12,
187     10,
188     12,
189     14,
190     12,
191     14,
192 };
Aac_Channels_Get(int8u ChannelLayout)193 extern int8u Aac_Channels_Get(int8u ChannelLayout)
194 {
195     if (ChannelLayout>=Aac_Channels_Size)
196         return 0; // Unknown
197 
198     return Aac_Channels[ChannelLayout];
199 }
Aac_Channels_GetString(int8u ChannelLayout)200 extern string Aac_Channels_GetString(int8u ChannelLayout)
201 {
202     if (!ChannelLayout)
203         return string();
204     if (ChannelLayout>=Aac_Channels_Size)
205         return "ChannelLayout "+Ztring::ToZtring(ChannelLayout).To_UTF8();
206 
207     return Ztring::ToZtring(Aac_Channels[ChannelLayout]).To_UTF8();
208 }
209 
210 //---------------------------------------------------------------------------
211 static const char* Aac_ChannelConfiguration[Aac_Channels_Size]=
212 {
213     "",
214     "Front: C",
215     "Front: L R",
216     "Front: L C R",
217     "Front: L C R, Back: C",
218     "Front: L C R, Side: L R",
219     "Front: L C R, Side: L R, LFE",
220     "Front: L C R, Side: L R, Back: L R, LFE",
221     //AudioSpecificConfig
222     "Dual mono",
223     "Front: L R, Back: C",
224     "Front: L C R, Back: L R",
225     "Front: L C R, Back: L C R, LFE",
226     "Front: L C R, Back: L L R R, LFE",
227     "",
228     //MPEG-H 3D Audio
229     "",
230     "",
231     "",
232     "",
233     "",
234     "",
235     "",
236 };
Aac_ChannelConfiguration_GetString(int8u ChannelLayout)237 extern string Aac_ChannelConfiguration_GetString(int8u ChannelLayout)
238 {
239     if (!ChannelLayout || ChannelLayout>=Aac_Channels_Size)
240         return string();
241 
242     return Aac_ChannelConfiguration[ChannelLayout];
243 }
244 
245 //---------------------------------------------------------------------------
246 static const char* Aac_ChannelConfiguration2[Aac_Channels_Size]=
247 {
248     "",
249     "1/0/0",
250     "2/0/0",
251     "3/0/0",
252     "3/0/1",
253     "3/2/0",
254     "3/2/0.1",
255     "3/4/0.1",
256     //AudioSpecificConfig
257     "1+1",
258     "2/0/1",
259     "3/2/0",
260     "3/2/1.1",
261     "3/2/2.1",
262     "",
263     //MPEG-H 3D Audio
264     "",
265     "",
266     "",
267     "",
268     "",
269     "",
270     "",
271 };
Aac_ChannelConfiguration2_GetString(int8u ChannelLayout)272 extern string Aac_ChannelConfiguration2_GetString(int8u ChannelLayout)
273 {
274     if (!ChannelLayout || ChannelLayout>=Aac_Channels_Size)
275         return string();
276 
277     return Aac_ChannelConfiguration2[ChannelLayout];
278 }
279 
280 //---------------------------------------------------------------------------
281 static const Aac_OutputChannel Aac_ChannelLayout[]= //Size of each line is provided by Aac_Channels[]
282 {
283     CH_M_000,
284     CH_M_L030, CH_M_R030,
285     CH_M_000, CH_M_L030, CH_M_R030,
286     CH_M_000, CH_M_L030, CH_M_R030, CH_M_180,
287     CH_M_000, CH_M_L030, CH_M_R030, CH_M_L110, CH_M_R110,
288     CH_M_000, CH_M_L030, CH_M_R030, CH_M_L110, CH_M_R110, CH_LFE,
289     CH_M_000, CH_M_L030, CH_M_R030, CH_M_L110, CH_M_R110, CH_M_L060, CH_M_R060, CH_LFE,
290     //AudioSpecificConfig
291     CH_M_000, CH_M_000,
292     CH_M_L030, CH_M_R030, CH_M_180,
293     CH_M_L030, CH_M_R030, CH_M_L110, CH_M_R110,
294     CH_M_000, CH_M_L030, CH_M_R030, CH_M_L110, CH_M_R110, CH_M_180, CH_LFE,
295     CH_M_000, CH_M_L030, CH_M_R030, CH_M_L110, CH_M_R110, CH_M_L135, CH_M_R135, CH_LFE,
296     CH_M_000, CH_M_L030, CH_M_R030, CH_M_L060, CH_M_R060, CH_M_L090, CH_M_R090, CH_M_L135, CH_M_R135, CH_M_180, CH_LFE, CH_LFE2, CH_U_000, CH_U_L030, CH_U_R030, CH_U_L090, CH_U_R090, CH_T_000, CH_U_L135, CH_U_R135, CH_U_180, CH_L_000, CH_L_L045, CH_L_R045,
297 };
298 
299 //---------------------------------------------------------------------------
300 static const Aac_OutputChannel Aac_ChannelLayout_MpegH[]= //Size of each line is provided by Aac_Channels[]
301 {
302     CH_M_000,
303     CH_M_L030, CH_M_R030,
304     CH_M_L030, CH_M_R030, CH_M_000,
305     CH_M_L030, CH_M_R030, CH_M_000, CH_M_180,
306     CH_M_L030, CH_M_R030, CH_M_000, CH_M_L110, CH_M_R110,
307     CH_M_L030, CH_M_R030, CH_M_000, CH_LFE, CH_M_L110, CH_M_R110,
308     CH_M_L030, CH_M_R030, CH_M_000, CH_LFE, CH_M_L110, CH_M_R110, CH_M_L060, CH_M_R060,
309     //AudioSpecificConfig
310     CH_M_000, CH_M_000,
311     CH_M_L030, CH_M_R030, CH_M_180,
312     CH_M_L030, CH_M_R030, CH_M_L110, CH_M_R110,
313     CH_M_L030, CH_M_R030, CH_M_000, CH_LFE, CH_M_L110, CH_M_R110, CH_M_180,
314     CH_M_L030, CH_M_R030, CH_M_000, CH_LFE, CH_M_L110, CH_M_R110, CH_M_L135, CH_M_R135,
315     CH_M_L060, CH_M_R060, CH_M_000, CH_LFE2, CH_M_L135, CH_M_R135, CH_M_L030, CH_M_R030, CH_M_180, CH_LFE3, CH_M_L090, CH_M_R090, CH_U_L045, CH_U_R045, CH_U_000, CH_T_000, CH_U_L135, CH_U_R135, CH_U_L090, CH_U_R090, CH_U_180, CH_L_000, CH_L_L045, CH_L_R045,
316     //MPEG-H
317     CH_M_L030, CH_M_R030, CH_M_000, CH_LFE, CH_M_L110, CH_M_R110, CH_U_L030, CH_U_R030,
318     CH_M_L030, CH_M_R030, CH_M_000, CH_LFE2, CH_M_L135, CH_M_R135, CH_LFE3, CH_M_L090, CH_M_R090, CH_U_L045, CH_U_R045, CH_U_180,
319     CH_M_L030, CH_M_R030, CH_M_000, CH_LFE, CH_M_L110, CH_M_R110, CH_U_L030, CH_U_R030, CH_U_L110, CH_U_R110,
320     CH_M_L030, CH_M_R030, CH_M_000, CH_LFE, CH_M_L110, CH_M_R110, CH_U_L030, CH_U_R030, CH_U_000, CH_U_L110, CH_U_R110, CH_T_000,
321     CH_M_L030, CH_M_R030, CH_M_000, CH_LFE, CH_M_L110, CH_M_R110, CH_M_L150, CH_M_R150, CH_U_L030, CH_U_R030, CH_U_000, CH_U_L110, CH_U_R110, CH_T_000,
322     CH_M_L030, CH_M_R030, CH_M_000, CH_LFE, CH_M_L135, CH_M_R135, CH_M_L090, CH_M_R090, CH_U_L030, CH_U_R030, CH_U_L135, CH_U_R135,
323     CH_M_L030, CH_M_R030, CH_M_000, CH_LFE, CH_M_L135, CH_M_R135, CH_M_L090, CH_M_R090, CH_U_L045, CH_U_R045, CH_U_L135, CH_U_R135, CH_M_LSCR, CH_M_RSCR,
324 };
325 
326 static const size_t Aac_OutputChannelPosition_Size=CH_MAX;
327 static const char* const Aac_OutputChannelPosition[Aac_OutputChannelPosition_Size]=
328 {
329     //USAC
330     "L",
331     "R",
332     "C",
333     "LFE",
334     "Ls",
335     "Rs",
336     "Lc",
337     "Rc",
338     "Lsr",
339     "Rsr",
340     "Cs",
341     "Lsd",
342     "Rsd",
343     "Lss",
344     "Rss",
345     "Lw",
346     "Rw",
347     "Lv",
348     "Rv",
349     "Cv",
350     "Lvr",
351     "Rvr",
352     "Cvr",
353     "Lvss",
354     "Rvss",
355     "Ts",
356     "LFE2",
357     "Lb",
358     "Rb",
359     "Cb",
360     "Lvs",
361     "Rvs",
362     //MPEG-H 3D Audio
363     "Lv", // +45 version of Lv (+30), merged
364     "Rv", // -45 version of Rv (-30), merged
365     "L", // +45 version of L (+30), merged
366     "R", // -45 version of R (-30), merged
367     "LFE3",
368     "Lscr",
369     "Rscr",
370     "Lsch",
371     "Rsch",
372     "Lsr", // +150 version of Lsr (+135), merged
373     "Rsr", // -150 version of Rsr (-135), merged
374 };
Aac_OutputChannelPosition_GetString(int8u OutputChannelPosition)375 extern string Aac_OutputChannelPosition_GetString(int8u OutputChannelPosition)
376 {
377     if (!OutputChannelPosition)
378         return string();
379     if (OutputChannelPosition>=Aac_OutputChannelPosition_Size)
380         return "OutputChannelPosition"+Ztring::ToZtring(OutputChannelPosition).To_UTF8();
381 
382     return Aac_OutputChannelPosition[OutputChannelPosition];
383 }
384 
385 //---------------------------------------------------------------------------
Aac_ChannelLayout_GetString(const Aac_OutputChannel * const OutputChannels,size_t OutputChannels_Size)386 extern string Aac_ChannelLayout_GetString(const Aac_OutputChannel* const OutputChannels, size_t OutputChannels_Size)
387 {
388     if (!OutputChannels)
389         return string();
390 
391     // Build the string
392     string Value;
393     for (int i=0; i< OutputChannels_Size; i++)
394     {
395         if (OutputChannels[i]<Aac_OutputChannelPosition_Size)
396             Value+=Aac_OutputChannelPosition[OutputChannels[i]];
397         else
398             Value+=Ztring::ToZtring(OutputChannels[i]).To_UTF8();
399         Value+=' ';
400     }
401     Value.resize(Value.size()-1);
402     return Value;
403 }
Aac_ChannelLayout_GetString(int8u ChannelLayout,bool IsMpegh3da=false)404 extern string Aac_ChannelLayout_GetString(int8u ChannelLayout, bool IsMpegh3da=false)
405 {
406     if (!ChannelLayout)
407         return string();
408     if (ChannelLayout>=(IsMpegh3da?Aac_Channels_Size:Aac_Channels_Size_Usac))
409         return "ChannelLayout"+Ztring::ToZtring(ChannelLayout).To_UTF8();
410 
411     // Compute start/end in Aac_ChannelLayout array
412     int Aac_ChannelLayout_Start=0;
413     for (int i=0; i<ChannelLayout; i++)
414         Aac_ChannelLayout_Start+=Aac_Channels[i];
415     int Aac_ChannelLayout_End=Aac_ChannelLayout_Start+Aac_Channels[ChannelLayout];
416 
417     // Build the string
418     return Aac_ChannelLayout_GetString((IsMpegh3da?Aac_ChannelLayout_MpegH:Aac_ChannelLayout)+Aac_ChannelLayout_Start, Aac_ChannelLayout_End-Aac_ChannelLayout_Start);
419 }
Aac_ChannelLayout_GetString(const vector<Aac_OutputChannel> & OutputChannels)420 extern string Aac_ChannelLayout_GetString(const vector<Aac_OutputChannel>& OutputChannels)
421 {
422     if (OutputChannels.empty())
423         return string();
424     return Aac_ChannelLayout_GetString(&*OutputChannels.begin(), OutputChannels.size());
425 }
426 
427 //---------------------------------------------------------------------------
428 extern const int8u Aac_ChannelMode_Max=4; // 0=Middle, 1=LFE, 2=Upper, 3=Bottom
429 extern const char Aac_ChannelMode[Aac_OutputChannelPosition_Size]=
430 {
431     //USAC
432     0,
433     0,
434     0,
435     1,
436     0,
437     0,
438     0,
439     0,
440     0,
441     0,
442     0,
443     0,
444     0,
445     0,
446     0,
447     0,
448     0,
449     2,
450     2,
451     2,
452     2,
453     2,
454     2,
455     2,
456     2,
457     2,
458     1,
459     3,
460     3,
461     3,
462     2,
463     2,
464     //MPEG-H 3D Audio
465     2,
466     2,
467     0,
468     0,
469     1,
470     0,
471     0,
472     0,
473     0,
474     0,
475     0,
476 };
477 
478 //---------------------------------------------------------------------------
Aac_ChannelMode_GetString(const Aac_OutputChannel * const OutputChannels,size_t OutputChannels_Size)479 extern string Aac_ChannelMode_GetString(const Aac_OutputChannel* const OutputChannels, size_t OutputChannels_Size)
480 {
481     if (!OutputChannels)
482         return string();
483 
484     // Count
485     int8u ChannelModes[Aac_ChannelMode_Max+1];
486     memset(ChannelModes, 0, Aac_ChannelMode_Max+1);
487     for (int i=0; i<OutputChannels_Size; i++)
488     {
489         if (OutputChannels[i]>Aac_OutputChannelPosition_Size)
490             ChannelModes[Aac_ChannelMode_Max]++;
491         else
492             ChannelModes[Aac_ChannelMode[OutputChannels[i]]]++;
493     }
494 
495     // Build the string
496     string Value;
497     if (OutputChannels_Size==24 && ChannelModes[0]==10 && ChannelModes[1]==2 && ChannelModes[2]==9 && ChannelModes[3]==3)
498     {
499         Value="22.2";
500     }
501     else
502     {
503         Value=Ztring::ToZtring(ChannelModes[0]).To_UTF8()+'.'+Ztring::ToZtring(ChannelModes[1]).To_UTF8();
504         if (ChannelModes[2] || ChannelModes[3])
505         {
506              Value+='.'+Ztring::ToZtring(ChannelModes[2]).To_UTF8();
507              if (ChannelModes[3])
508                 Value+='.'+Ztring::ToZtring(ChannelModes[3]).To_UTF8();
509         }
510         if (ChannelModes[Aac_ChannelMode_Max])
511             Value+='+'+Ztring::ToZtring(ChannelModes[Aac_ChannelMode_Max]).To_UTF8();
512     }
513     return Value;
514 }
Aac_ChannelMode_GetString(int8u ChannelLayout,bool IsMpegh3da=false)515 extern string Aac_ChannelMode_GetString(int8u ChannelLayout, bool IsMpegh3da=false)
516 {
517     if (!ChannelLayout)
518         return string();
519     if (ChannelLayout>=(IsMpegh3da?Aac_Channels_Size:Aac_Channels_Size_Usac))
520         return "ChannelLayout"+Ztring::ToZtring(ChannelLayout).To_UTF8();
521 
522     // Compute start/end in Aac_ChannelLayout array
523     int Aac_ChannelLayout_Start=0;
524     for (int i=0; i<ChannelLayout; i++)
525         Aac_ChannelLayout_Start+=Aac_Channels[i];
526     int Aac_ChannelLayout_End=Aac_ChannelLayout_Start+Aac_Channels[ChannelLayout];
527 
528     // Build the string
529     return Aac_ChannelMode_GetString((IsMpegh3da?Aac_ChannelLayout_MpegH:Aac_ChannelLayout)+Aac_ChannelLayout_Start, Aac_ChannelLayout_End-Aac_ChannelLayout_Start);
530 }
Aac_ChannelMode_GetString(const vector<Aac_OutputChannel> & OutputChannels)531 extern string Aac_ChannelMode_GetString(const vector<Aac_OutputChannel>& OutputChannels)
532 {
533     if (OutputChannels.empty())
534         return string();
535     return Aac_ChannelMode_GetString(&*OutputChannels.begin(), OutputChannels.size());
536 }
537 
538 //---------------------------------------------------------------------------
Aac_AudioSpecificConfig_sampling_frequency_index(const int64s sampling_frequency)539 int8u Aac_AudioSpecificConfig_sampling_frequency_index(const int64s sampling_frequency)
540 {
541     if (sampling_frequency>=92017) return 0;
542     if (sampling_frequency>=75132) return 1;
543     if (sampling_frequency>=55426) return 2;
544     if (sampling_frequency>=46009) return 3;
545     if (sampling_frequency>=37566) return 4;
546     if (sampling_frequency>=27713) return 5;
547     if (sampling_frequency>=23004) return 6;
548     if (sampling_frequency>=18783) return 7;
549     if (sampling_frequency>=13856) return 8;
550     if (sampling_frequency>=11502) return 9;
551     if (sampling_frequency>=9391) return 10;
552     return 11;
553 }
554 
555 //---------------------------------------------------------------------------
AudioSpecificConfig(size_t End)556 void File_Aac::AudioSpecificConfig (size_t End)
557 {
558     //Parsing
559     bool    sbrData=false, sbrPresentFlag=false, psData=false, psPresentFlag=false;
560     Element_Begin1("AudioSpecificConfig");
561     GetAudioObjectType(audioObjectType,                         "audioObjectType");
562     Infos["CodecID"].From_Number(audioObjectType);
563     Get_S1 (4, sampling_frequency_index,                        "samplingFrequencyIndex"); Param_Info1C(sampling_frequency_index<Aac_sampling_frequency_Size, Aac_sampling_frequency[sampling_frequency_index]);
564     if (sampling_frequency_index==0xF)
565     {
566         int32u samplingFrequency;
567         Get_S3 (24, samplingFrequency,                          "samplingFrequency");
568         Frequency_b=samplingFrequency;
569         sampling_frequency_index=Aac_AudioSpecificConfig_sampling_frequency_index(Frequency_b);
570     }
571     else if(sampling_frequency_index<Aac_sampling_frequency_Size)
572         Frequency_b=Aac_sampling_frequency[sampling_frequency_index];
573     else
574         Frequency_b=0;
575     Get_S1 (4, channelConfiguration,                            "channelConfiguration"); Param_Info1(Aac_ChannelConfiguration[channelConfiguration]);
576     if (audioObjectType==5 || audioObjectType==29)
577     {
578         extensionAudioObjectType=5;
579         sbrPresentFlag=true;
580         if (audioObjectType==29)
581             psPresentFlag=true;
582         Get_S1 (4, extension_sampling_frequency_index,          "extensionSamplingFrequencyIndex"); Param_Info1C(sampling_frequency_index<Aac_sampling_frequency_Size, Aac_sampling_frequency[extension_sampling_frequency_index]);
583         if (extension_sampling_frequency_index==0xF)
584         {
585             Get_S3 (24, extension_sampling_frequency,           "extensionSamplingFrequency");
586             sampling_frequency_index=Aac_AudioSpecificConfig_sampling_frequency_index(extension_sampling_frequency);
587         }
588         else
589             extension_sampling_frequency=Aac_sampling_frequency[extension_sampling_frequency_index];
590         GetAudioObjectType(audioObjectType,                     "audioObjectType");
591         if (audioObjectType==22) //BSAC
592             Skip_S1(4,                                          "extensionChannelConfiguration");
593     }
594     else
595         extensionAudioObjectType=0x00;
596 
597     switch (audioObjectType)
598     {
599         case 1:
600         case 2:
601         case 3:
602         case 4:
603         case 6:
604         case 7:
605         case 17:
606         case 19:
607         case 20:
608         case 21:
609         case 22:
610         case 23:
611             GASpecificConfig();
612             break;
613         case 8:
614             CelpSpecificConfig();
615             break;
616         case 9:
617             HvxcSpecificConfig();
618             break;
619         case 12:
620             TTSSpecificConfig();
621             break;
622         //~ case 13:
623         //~ case 14:
624         //~ case 15:
625         //~ case 16:
626             //~ StructuredAudioSpecificConfig();
627             //~ break;
628         case 24:
629             ErrorResilientCelpSpecificConfig();
630             break;
631         case 25:
632             ErrorResilientHvxcSpecificConfig();
633             break;
634         case 26:
635         case 27:
636             ParametricSpecificConfig();
637             break;
638         case 28:
639             SSCSpecificConfig();
640             break;
641         //~ case 30:
642             //~ Skip_S1(1,                                          "sacPayloadEmbedding");
643             //~ SpatialSpecificConfig(); //ISO/IEC 23003-1
644             //~ break;
645         case 32:
646         case 33:
647         case 34:
648             MPEG_1_2_SpecificConfig();
649             break;
650         case 35:
651             DSTSpecificConfig();
652             break;
653         case 36:
654             Skip_S1(5,                                          "fillBits");
655             ALSSpecificConfig();
656             break;
657         case 37:
658         case 38:
659             SLSSpecificConfig();
660             break;
661         case 39:
662             ELDSpecificConfig();
663             break;
664         //~ case 40:
665         //~ case 41:
666             //~ SymbolicMusicSpecificConfig(); //ISO/IEC 14496-23
667             //~ break;
668         case 42:
669             UsacConfig();
670             break;
671         default:
672             Element_Begin1("not implemented part");
673             Skip_BS(Data_BS_Remain()-((End==(size_t)-1)?0:End), "(Not implemented)");
674             Element_End0();
675             Frame_Count=(size_t)-1; //Forcing not to parse following data anymore
676     }
677 
678     switch (audioObjectType)
679     {
680         case 17:
681         case 19:
682         case 20:
683         case 21:
684         case 22:
685         case 23:
686         case 24:
687         case 25:
688         case 26:
689         case 27:
690         case 39:
691             int8u epConfig;
692             Get_S1(2,epConfig,                                  "epConfig");
693             if ( epConfig == 2 || epConfig == 3 )
694                 ErrorProtectionSpecificConfig();
695             if ( epConfig == 3 )
696             {
697                 bool directMapping;
698                 Get_SB(directMapping,                           "directMapping");
699                 if ( ! directMapping )
700                 {
701                     Element_Begin1("not implemented part");
702                     Skip_BS(Data_BS_Remain()-((End==(size_t)-1)?0:End), "(Not implemented)");
703                     Element_End0();
704                     if (Mode==Mode_LATM)
705                         File__Analyze::Accept();
706                     Frame_Count=(size_t)-1; //Forcing not to parse following data anymore
707                 }
708             }
709         default : ;
710     }
711 
712     if (extensionAudioObjectType!=5 && End!=(size_t)-1 && Data_BS_Remain()>=End+16)
713     {
714         int16u syncExtensionType;
715         Get_S2(11,syncExtensionType,                            "syncExtensionType");
716         if (syncExtensionType == 0x2b7)
717         {
718             sbrData=true;
719             GetAudioObjectType(extensionAudioObjectType,        "extensionAudioObjectType");
720             if (extensionAudioObjectType==5 )
721             {
722                 Get_SB(sbrPresentFlag,                          "sbrPresentFlag");
723                 if (sbrPresentFlag)
724                 {
725                     Get_S1 (4, extension_sampling_frequency_index, "extensionSamplingFrequencyIndex"); Param_Info1(Aac_sampling_frequency[extension_sampling_frequency_index]);
726                     if (extension_sampling_frequency_index==0xF)
727                     {
728                         Get_S3 (24, extension_sampling_frequency, "extensionSamplingFrequency");
729                         extension_sampling_frequency_index=Aac_AudioSpecificConfig_sampling_frequency_index(extension_sampling_frequency);
730                     }
731                     else
732                         extension_sampling_frequency=Aac_sampling_frequency[extension_sampling_frequency_index];
733                     if (Data_BS_Remain()>=End+12)
734                     {
735                         int16u syncExtensionType;
736                         Get_S2(11,syncExtensionType,            "syncExtensionType");
737                         if (syncExtensionType == 0x548)
738                         {
739                             psData=true;
740                             Get_SB (psPresentFlag,              "psPresentFlag");
741                         }
742                     }
743                 }
744             }
745             if ( extensionAudioObjectType == 29 )
746             {
747                 Get_SB(sbrPresentFlag,                          "sbrPresentFlag");
748                 if (sbrPresentFlag)
749                 {
750                     Get_S1 (4, extension_sampling_frequency_index, "extensionSamplingFrequencyIndex"); Param_Info1(Aac_sampling_frequency[extension_sampling_frequency_index]);
751                     if (extension_sampling_frequency_index==0xF)
752                     {
753                         Get_S3 (24, extension_sampling_frequency, "extensionSamplingFrequency");
754                         extension_sampling_frequency_index=Aac_AudioSpecificConfig_sampling_frequency_index(extension_sampling_frequency);
755                     }
756                     else
757                         extension_sampling_frequency=Aac_sampling_frequency[extension_sampling_frequency_index];
758                 }
759                 Skip_S1(4,                                      "extensionChannelConfiguration");
760             }
761         }
762     }
763     Element_End0();
764     if (Data_BS_Remain()>End)
765     {
766         int8u LastByte=0xFF;
767         if (Data_BS_Remain()-End<8)
768             Peek_S1((int8u)(Data_BS_Remain()-End), LastByte);
769         Skip_BS(Data_BS_Remain()-End,                           LastByte?"Unknown":"Padding");
770     }
771 
772     FILLING_BEGIN();
773         AudioSpecificConfig_OutOfBand (Frequency_b, audioObjectType, sbrData, psData, sbrPresentFlag, psPresentFlag);
774         if (Frame_Count==(size_t)-1)
775         {
776             if (Mode==File_Aac::Mode_ADIF || Mode==File_Aac::Mode_ADTS)
777                 File__Tags_Helper::Finish();
778             else
779                 File__Analyze::Finish();
780         }
781     FILLING_END()
782 }
783 
784 //---------------------------------------------------------------------------
AudioSpecificConfig_OutOfBand(int64s sampling_frequency_,int8u audioObjectType_,bool sbrData,bool psData,bool sbrPresentFlag,bool psPresentFlag)785 void File_Aac::AudioSpecificConfig_OutOfBand (int64s sampling_frequency_, int8u audioObjectType_, bool sbrData, bool psData, bool sbrPresentFlag, bool psPresentFlag)
786 {
787     if (!Frequency_b && sampling_frequency_) //Only if not yet set
788     {
789         Frequency_b=sampling_frequency_;
790         sampling_frequency_index=Aac_AudioSpecificConfig_sampling_frequency_index((int32u)Frequency_b);
791     }
792 
793     if (audioObjectType_==(int8u)-1)
794     {
795         if (audioObjectType==(int8u)-1)
796             return; //All data is not yet available
797         std::map<std::string, Ztring>::const_iterator i=Infos.find("Format_Settings_SBR");
798         if (i!=Infos.end())
799         {
800             sbrData=true;
801             sbrPresentFlag=i->second.find(__T("Yes"))!=string::npos;
802         }
803         else
804         {
805             sbrData=false;
806             sbrPresentFlag=false;
807         }
808         i = Infos.find("Format_Settings_PS");
809         if (i!=Infos.end())
810         {
811             psData=true;
812             psPresentFlag=i->second.find(__T("Yes"))!=string::npos;
813         }
814         else
815         {
816             psData=false;
817             psPresentFlag=false;
818         }
819     }
820     else
821         audioObjectType=audioObjectType_;
822 
823     if (Frequency_b)
824         Infos["SamplingRate"].From_Number(Frequency_b);
825     Infos["Format"].From_UTF8(Aac_Format(audioObjectType));
826     Infos["Format_Profile"].From_UTF8(Aac_Format_Profile(audioObjectType));
827     Infos["Codec"].From_UTF8(Aac_audioObjectType(audioObjectType));
828     if (channelConfiguration && channelConfiguration!=(int8u)-1)
829     {
830         Infos["Channel(s)"].From_UTF8(Aac_Channels_GetString(channelConfiguration));
831         Infos["ChannelPositions"].From_UTF8(Aac_ChannelConfiguration_GetString(channelConfiguration));
832         Infos["ChannelPositions/String2"].From_UTF8(Aac_ChannelConfiguration2_GetString(channelConfiguration));
833         Infos["ChannelLayout"].From_UTF8(Aac_ChannelLayout_GetString(channelConfiguration));
834     }
835 
836     if (sbrPresentFlag || !Infos["Format_Settings_SBR"].empty())
837     {
838         Infos["Format_Profile"]=__T("HE-AAC");
839         int32u SamplingRate=(extension_sampling_frequency_index==(int8u)-1)?(((int32u)Frequency_b)*2):extension_sampling_frequency;
840         if (SamplingRate)
841         {
842             const Ztring SamplingRate_Previous = Infos["SamplingRate"];
843             Infos["SamplingRate"].From_Number(SamplingRate, 10);
844             if (MediaInfoLib::Config.LegacyStreamDisplay_Get())
845             {
846                 Infos["Format_Profile"]+=__T(" / LC");
847                 Infos["SamplingRate"]+=__T(" / ")+SamplingRate_Previous;
848             }
849         }
850         Infos["Format_Settings"]=sbrData?__T("Explicit"):__T("NBC"); // "Not Backward Compatible"
851         Infos["Format_Settings_SBR"]=sbrData?__T("Yes (Explicit)"):__T("Yes (NBC)"); // "Not Backward Compatible"
852         Infos["Codec"]=Ztring().From_UTF8(Aac_audioObjectType(audioObjectType))+__T("-SBR");
853     }
854     else if (sbrData)
855         Infos["Format_Settings_SBR"]=__T("No (Explicit)");
856 
857     if (psPresentFlag || !Infos["Format_Settings_PS"].empty())
858         FillInfosHEAACv2(psData ? __T("Explicit") : __T("NBC")); // "Not Backward Compatible");
859     else if (psData)
860         Infos["Format_Settings_PS"]=__T("No (Explicit)");
861 
862     //Commercial names
863     if (Infos["Format"]==__T("USAC"))
864         Infos["Format_Commercial_IfAny"]=__T("xHE-AAC");
865 }
866 
867 //---------------------------------------------------------------------------
GetAudioObjectType(int8u & ObjectType,const char * Name)868 void File_Aac::GetAudioObjectType(int8u &ObjectType, const char* Name)
869 {
870     Element_Begin1(Name);
871     Get_S1(5, ObjectType,                                       "audioObjectType");
872     if (ObjectType==31)
873     {
874         Get_S1(6, ObjectType,                                   "audioObjectTypeExt");
875         ObjectType+=32;
876     }
877     Element_Info1(ObjectType); Element_Info1(Aac_Format(ObjectType)); Element_Info1(Aac_Format_Profile(ObjectType));
878     Element_End0();
879 }
880 
881 //***************************************************************************
882 // Elements - Multiplex layer
883 //***************************************************************************
884 
885 //---------------------------------------------------------------------------
AudioMuxElement()886 void File_Aac::AudioMuxElement()
887 {
888     Element_Begin1("AudioMuxElement");
889     if (muxConfigPresent)
890     {
891         bool useSameStreamMux;
892         Get_SB (useSameStreamMux,                               "useSameStreamMux");
893         if (!useSameStreamMux)
894             StreamMuxConfig();
895     }
896     if (sampling_frequency_index==(int8u)-1) //No previous config
897     {
898         CanFill=false;
899         Skip_BS(Data_BS_Remain(),                               "(Waiting for configuration)");
900         return;
901     }
902     if (audioMuxVersionA==0)
903     {
904         for (int8u i=0; i<=numSubFrames; i++)
905         {
906             PayloadLengthInfo();
907             PayloadMux();
908         }
909         if (otherDataLenBits)
910             Skip_BS(otherDataLenBits,                           "otherData");
911     }
912     else
913     {
914         Element_Begin1("(not implemented)");
915         Skip_BS(Data_BS_Remain(),                               "(not implemented)");
916         Element_End0();
917     }
918     if (Data_BS_Remain()%8)
919         Skip_S1(Data_BS_Remain()%8,                             "byte_alignment");
920     Element_End0();
921 }
922 
923 //---------------------------------------------------------------------------
StreamMuxConfig()924 void File_Aac::StreamMuxConfig()
925 {
926     Element_Begin1("StreamMuxConfig");
927 
928     bool audioMuxVersion;
929     Get_SB (audioMuxVersion,                                    "audioMuxVersion");
930     if (audioMuxVersion)
931         Get_SB (audioMuxVersionA,                               "audioMuxVersionA");
932     else
933         audioMuxVersionA=false;
934 
935     if (!audioMuxVersionA)
936     {
937         if (audioMuxVersion==1)
938         {
939             Element_Begin1("(not implemented)");
940             Skip_BS(Data_BS_Remain(),                               "(not implemented)");
941             Element_End0();
942             //taraBufferFullness=LatmGetValue();
943         }
944 
945         int8u streamCnt = 0;
946         bool useSameConfig;
947 
948         Get_SB (allStreamsSameTimeFraming,                      "allStreamsSameTimeFraming");
949         Get_S1 (6, numSubFrames,                                "numSubFrames");
950         Get_S1 (4, numProgram,                                  "numProgram");
951 
952         for (int8u prog=0; prog<=numProgram; prog++)
953         {
954             Get_S1(3,numLayer,                                  "numLayer");
955             for (int8u lay=0; lay<=numLayer; lay++)
956             {
957                 progSIndx[streamCnt]=prog;
958                 laySIndx[streamCnt]=lay;
959                 streamID[prog][lay]=streamCnt++;
960                 if (prog==0 && lay==0)
961                     useSameConfig=0;
962                 else
963                     Get_SB(useSameConfig,                       "useSameConfig");
964 
965                 if (!useSameConfig)
966                 {
967                     if (audioMuxVersion==0)
968                         AudioSpecificConfig();
969                     else
970                     {
971                         int ascLen=LatmGetValue();
972                         AudioSpecificConfig(Data_BS_Remain()-ascLen);
973                     }
974                 }
975 
976                 Get_S1(3,frameLengthType[streamID[prog][lay]],  "frameLengthType[streamID[prog][lay]]");
977                 switch(frameLengthType[streamID[prog][lay]])
978                 {
979                     case 0 :
980                             Skip_S1(8,                          "latmBufferFullness[streamID[prog][lay]]");
981                             if(!allStreamsSameTimeFraming)
982                             {
983                                 Element_Begin1("(not implemented)");
984                                 Skip_BS(Data_BS_Remain(),       "(not implemented)");
985                                 Element_End0();
986                             }
987                             //~ if ((!allStreamsSameTimeFraming) &&
988                             //~ (AudioObjectType[lay] == 6 || AudioObjectType[lay] == 20) &&
989                             //~ (AudioObjectType[lay-1] == 8 || AudioObjectType[lay-1] == 24))
990                             //~ {
991                                 //~ Skip_S1(6,                          "coreFrameOffset");
992                             //~ }
993                             break;
994                     case 1 :
995                             Get_S2(9,frameLength[streamID[prog][lay]],"frameLength[streamID[prog][lay]]");
996                             break;
997                     case 3 :
998                     case 4 :
999                     case 5 :
1000                             Skip_S1(6,                          "CELPframeLengthTableIndex[streamID[prog][lay]]");
1001                             break;
1002                     case 6 :
1003                     case 7 :
1004                             Skip_S1(1,                          "HVXCframeLengthTableIndex[streamID[prog][lay]]");
1005                             break;
1006                     default :
1007                             Element_Begin1("(not implemented)");
1008                             Skip_BS(Data_BS_Remain(),       "(not implemented)");
1009                             Element_End0();
1010                 }
1011             }
1012         }
1013         bool otherDataPresent, crcCheckPresent;
1014         Get_SB (otherDataPresent,                               "otherDataPresent");
1015         if (otherDataPresent)
1016         {
1017             if (audioMuxVersion==1)
1018                 otherDataLenBits=LatmGetValue();
1019             else
1020             {
1021                 otherDataLenBits=0;
1022                 bool otherDataLenEsc;
1023                 do
1024                 {
1025                     int8u otherDataLenTmp;
1026                     otherDataLenBits<<=8;
1027                     Get_SB(   otherDataLenEsc,                   "otherDataLenEsc");
1028                     Get_S1(8, otherDataLenTmp,                   "otherDataLenTmp");
1029                     otherDataLenBits+=otherDataLenTmp;
1030                 }
1031                 while (otherDataLenEsc);
1032             }
1033         }
1034         else
1035             otherDataLenBits=0;
1036         Get_SB(crcCheckPresent,                                 "crcCheckPresent");
1037         if(crcCheckPresent)
1038             Skip_S1(8,                                          "crcCheckSum");
1039     }
1040     else
1041     {
1042         Element_Begin1("(not implemented)");
1043         Skip_BS(Data_BS_Remain(),                               "(not implemented)");
1044         Element_End0();
1045     }
1046     Element_End0();
1047 
1048     FILLING_BEGIN();
1049         CanFill=true;
1050     FILLING_END();
1051 }
1052 
1053 //---------------------------------------------------------------------------
LatmGetValue()1054 int32u File_Aac::LatmGetValue()
1055 {
1056     Element_Begin1("LatmGetValue");
1057     int8u valueTmp, bytesForValue;
1058     Get_S1(2, bytesForValue,                                    "bytesForValue");
1059     int32u value=0;
1060     for (int8u i=0; i<=bytesForValue; i++)
1061     {
1062         value<<=8;
1063         Get_S1(8, valueTmp,                                     "valueTmp");
1064         value+=valueTmp;
1065     }
1066     Element_End0();
1067     return value;
1068 }
1069 
1070 //---------------------------------------------------------------------------
PayloadLengthInfo()1071 void File_Aac::PayloadLengthInfo()
1072 {
1073     Element_Begin1("PayloadLengthInfo");
1074     int8u tmp;
1075     if (allStreamsSameTimeFraming)
1076     {
1077         for (int8u prog=0; prog<=numProgram; prog++)
1078         {
1079             for (int8u lay=0; lay<=numLayer; lay++)
1080             {
1081                 if (frameLengthType[streamID[prog][lay]]==0)
1082                 {
1083                     MuxSlotLengthBytes[streamID[prog][lay]]=0;
1084                     do
1085                     {
1086                         Get_S1(8, tmp,                          "tmp");
1087                         MuxSlotLengthBytes[streamID[prog][lay]]+=tmp;
1088                     }
1089                     while(tmp==255);
1090                 }
1091                 else if (frameLengthType[streamID[prog][lay]]==3
1092                       || frameLengthType[streamID[prog][lay]]==5
1093                       || frameLengthType[streamID[prog][lay]]==7)
1094                 {
1095                     Skip_S1(2,                                  "MuxSlotLengthCoded[streamID[prog][lay]]");
1096                 }
1097             }
1098         }
1099     }
1100     else
1101     {
1102         int8u streamIndx;
1103         int8u prog, lay;
1104         Get_S1(4, numChunk,                                     "numChunk");
1105         for (int chunkCnt=0; chunkCnt<=numChunk; chunkCnt++)
1106         {
1107             Get_S1(4,streamIndx,                                "streamIndx");
1108             prog=progCIndx[chunkCnt]=progSIndx[streamIndx];
1109             lay=layCIndx[chunkCnt]=laySIndx[streamIndx];
1110             if (frameLengthType[streamID[prog][lay]]==0)
1111             {
1112                 MuxSlotLengthBytes[streamID[prog][lay]]=0;
1113                 do
1114                 {
1115                     Get_S1(8, tmp,                              "tmp");
1116                     MuxSlotLengthBytes[streamID[prog][lay]]+=tmp;
1117                 }
1118                 while(tmp==255);
1119                 Skip_SB(                                        "AuEndFlag[streamID[prog][lay]]");
1120             }
1121             else if (frameLengthType[streamID[prog][lay]]==3
1122                   || frameLengthType[streamID[prog][lay]]==5
1123                   || frameLengthType[streamID[prog][lay]]==7)
1124             {
1125                 Skip_S1(2,                                      "MuxSlotLengthCoded[streamID[prog][lay]]");
1126             }
1127         }
1128     }
1129     Element_End0();
1130 }
1131 
1132 //---------------------------------------------------------------------------
PayloadMux()1133 void File_Aac::PayloadMux()
1134 {
1135     Element_Begin1("PayloadMux");
1136     if (allStreamsSameTimeFraming)
1137     {
1138         for (int8u prog=0; prog<=numProgram; prog++)
1139             for (int8u lay=0; lay<=numLayer; lay++)
1140             {
1141                 switch(frameLengthType[streamID[prog][lay]])
1142                 {
1143                     case 0 :
1144                             if (CA_system_ID_MustSkipSlices)
1145                             {
1146                                 //Encryption management
1147                                 Skip_BS(8*MuxSlotLengthBytes[streamID[prog][lay]], "Encrypted payload[streamID[prog][lay]]");
1148                                 Frame_Count_Valid=0;
1149                             }
1150                             else
1151                                 raw_data_block();
1152                             break;
1153                     case 1 :
1154                             Skip_BS(8 * (frameLength[streamID[prog][lay]] + 20),"payload[streamID[prog][lay]]");
1155                             break;
1156                     default:
1157                             Element_Begin1("(not implemented)");
1158                             Skip_BS(Data_BS_Remain(),           "(not implemented)");
1159                             Element_End0();
1160                 }
1161             }
1162     }
1163     else
1164     {
1165         for (int8u chunkCnt=0; chunkCnt<=numChunk; chunkCnt++)
1166         {
1167             int8u prog=progCIndx[chunkCnt];
1168             int8u lay=layCIndx[chunkCnt];
1169             switch(frameLengthType[streamID[prog][lay]])
1170             {
1171                 case 0 :
1172                         raw_data_block(); //Skip_BS(MuxSlotLengthBytes[streamID[prog][lay]], "payload[streamID[prog][lay]]");
1173                         break;
1174                 case 1 :
1175                         Skip_BS(8*(frameLength[streamID[prog][lay]]+20), "payload[streamID[prog][lay]]");
1176                         break;
1177                 default:
1178                         Element_Begin1("not implemented");
1179                         Element_End0();
1180             }
1181         }
1182     }
1183     Element_End0();
1184 }
1185 
1186 //***************************************************************************
1187 // Elements - Error protection
1188 //***************************************************************************
1189 
1190 //---------------------------------------------------------------------------
ErrorProtectionSpecificConfig()1191 void File_Aac::ErrorProtectionSpecificConfig()
1192 {
1193     Element_Begin1("ErrorProtectionSpecificConfig");
1194     int8u number_of_predefined_set,number_of_concatenated_frame,interleave_type;
1195     Get_S1(8,number_of_predefined_set,                          "number_of_predefined_set");
1196     Get_S1(2,interleave_type,                                   "interleave_type");
1197     Skip_S1(3,                                                  "bit_stuffing");
1198     Get_S1(3,number_of_concatenated_frame,                      "number_of_concatenated_frame");
1199     for (int8u i = 0; i < number_of_predefined_set; i++ )
1200     {
1201         int8u number_of_class;
1202         Get_S1(6,number_of_class,                               "number_of_class[i]");
1203         for (int8u j = 0; j < number_of_class; j++)
1204         {
1205             bool length_escape,fec_type,rate_escape,crclen_escape;
1206             Get_SB(length_escape,                               "length_escape[i][j]");
1207             Get_SB(rate_escape,                                 "rate_escape[i][j]");
1208             Get_SB(crclen_escape,                               "crclen_escape[i][j]");
1209             if (number_of_concatenated_frame != 1)
1210                 Skip_SB(                                        "concatenate_flag[i][j]");
1211             Get_SB(fec_type,                                    "fec_type[i][j]");
1212             if(!fec_type)
1213                 Skip_SB(                                        "termination_switch[i][j]");
1214             if (interleave_type == 2)
1215                 Skip_S1(2,                                      "interleave_switch[i][j]");
1216             Skip_SB(                                            "class_optional");
1217             if (length_escape)
1218             {
1219                 /* ESC */
1220                 Skip_S1(4,                                      "number_of_bits_for_length[i][j]");
1221             }
1222             else
1223             {
1224                 Skip_S2(16,                                     "class_length[i][j]");
1225             }
1226             if (!rate_escape)
1227             { /* not ESC */
1228                 if(fec_type)
1229                 {
1230                     Skip_S1(7,                                  "class_rate[i][j]");
1231                 }
1232                 else
1233                 {
1234                     Skip_S1(5,                                  "class_rate[i][j]");
1235                 }
1236             }
1237             if (!crclen_escape)
1238             {
1239             /* not ESC */
1240             Skip_S1(5,                                          "class_crclen[i][j]");
1241             }
1242         }
1243         bool class_reordered_output;
1244         Get_SB(class_reordered_output,                          "class_reordered_output");
1245         if ( class_reordered_output )
1246         {
1247             for (int j = 0; j < number_of_class; j++ )
1248             {
1249                 Skip_S1(6,                                      "class_output_order[i][j]");
1250             }
1251         }
1252     }
1253     bool header_protection;
1254     Get_SB(header_protection,                                   "header_protection");
1255     if (header_protection)
1256     {
1257         Skip_S1(5,                                              "header_rate");
1258         Skip_S1(5,                                              "header_crclen");
1259     }
1260     Element_End0();
1261 }
1262 
1263 //***************************************************************************
1264 // Elements - MPEG-2 AAC Audio_Data_Interchange_Format, ADIF
1265 //***************************************************************************
1266 
1267 //---------------------------------------------------------------------------
adif_header()1268 void File_Aac::adif_header()
1269 {
1270     //Parsing
1271     int32u bitrate;
1272     int8u  num_program_config_elements;
1273     bool   bitstream_type;
1274     Skip_C4(                                                    "adif_id");
1275     BS_Begin();
1276     TEST_SB_SKIP(                                               "copyright_id_present");
1277         Skip_S4(32,                                             "copyright_id");
1278         Skip_S4(32,                                             "copyright_id");
1279         Skip_S4( 8,                                             "copyright_id");
1280     TEST_SB_END();
1281     Skip_SB(                                                    "original_copy");
1282     Skip_SB(                                                    "home");
1283     Get_SB (    bitstream_type,                                 "bitstream_type"); Param_Info1(bitstream_type?"VBR":"CBR");
1284     Get_S3 (23, bitrate,                                        "bitrate");
1285     Get_S1 ( 4, num_program_config_elements,                    "num_program_config_elements");
1286     if (!bitstream_type)
1287         Skip_S3(20,                                             "adif_buffer_fullness");
1288     for (int8u Pos=0; Pos<num_program_config_elements+1; Pos++)
1289         program_config_element();
1290     BS_End();
1291 
1292     FILLING_BEGIN();
1293         Fill(Stream_General, 0, General_Format, "ADIF", Unlimited, true, true);
1294         Fill(Stream_General, 0, General_HeaderSize, Element_Size);
1295         Fill(Stream_General, 0, General_OverallBitRate_Mode, bitstream_type?"VBR":"CBR");
1296 
1297         for (size_t StreamPos=0; StreamPos<Count_Get(Stream_Audio); StreamPos++)
1298             Fill(Stream_Audio, StreamPos, Audio_MuxingMode, "ADIF");
1299         if (num_program_config_elements==0) //Easy to fill only if 1 audio stream
1300         {
1301             Infos["BitRate_Mode"].From_UTF8(bitstream_type?"VBR":"CBR");
1302             if (bitrate>0)
1303                 Infos[bitstream_type?"BitRate_Maximum":"BitRate"].From_Number(bitrate);
1304         }
1305 
1306         //No more need data
1307         File__Tags_Helper::Finish("ADIF");
1308     FILLING_END();
1309 }
1310 
1311 //***************************************************************************
1312 // Elements - Audio_Data_Transport_Stream frame, ADTS
1313 //***************************************************************************
1314 
1315 //---------------------------------------------------------------------------
adts_frame()1316 void File_Aac::adts_frame()
1317 {
1318     //Parsing
1319     adts_fixed_header();
1320     adts_variable_header();
1321 
1322     //Encryption management
1323     if (CA_system_ID_MustSkipSlices)
1324     {
1325         //Is not decodable
1326         BS_End();
1327         Skip_XX(Element_Size-Element_Offset,                    "Encrypted data");
1328         Frame_Count_Valid=0;
1329         return;
1330     }
1331 
1332     if (num_raw_data_blocks==0)
1333     {
1334         if (!protection_absent)
1335         {
1336             Element_Begin1("adts_error_check");
1337                 Skip_S2(16,                                     "crc_check");
1338             Element_End0();
1339         }
1340         raw_data_block();
1341     }
1342     else
1343     {
1344         Element_Begin1("adts_header_error_check");
1345             if (!protection_absent)
1346                 for (int i=1; i<=num_raw_data_blocks; i++)
1347                     Skip_S2(16,                                 "raw_data_block_position(i)");
1348             Skip_S2(16,                                         "crc_check");
1349         Element_End0();
1350 
1351         for(int i=0; i<=num_raw_data_blocks; i++)
1352         {
1353             raw_data_block();
1354             if (!Data_BS_Remain())
1355                 break; // Maybe the content was not parsed
1356             if (!protection_absent)
1357             {
1358                 Element_Begin1("adts_raw_data_block_error_check");
1359                     Skip_BS(16,                                 "crc_check");
1360                 Element_End0();
1361             }
1362         }
1363     }
1364 }
1365 
1366 //---------------------------------------------------------------------------
adts_fixed_header()1367 void File_Aac::adts_fixed_header()
1368 {
1369     //Parsing
1370     bool    id;
1371     Element_Begin1("adts_fixed_header");
1372     Skip_BS(12,                                                 "syncword");
1373     Get_SB (    id,                                             "id"); Param_Info1(Aac_Adts_ID[id]);
1374     Skip_BS( 2,                                                 "layer");
1375     Get_SB (    protection_absent,                              "protection_absent");
1376     Get_S1 ( 2, audioObjectType,                                "profile_ObjectType"); audioObjectType++; Param_Info1(Aac_audioObjectType(audioObjectType));
1377     Get_S1 ( 4, sampling_frequency_index,                       "sampling_frequency_index");
1378     if(sampling_frequency_index<Aac_sampling_frequency_Size)
1379         Frequency_b=Aac_sampling_frequency[sampling_frequency_index];
1380     else
1381         Frequency_b=0;
1382     Param_Info2(Frequency_b, " Hz");
1383     Skip_SB(                                                    "private");
1384     Get_S1 ( 3, channelConfiguration,                           "channel_configuration");
1385     Skip_SB(                                                    "original");
1386     Skip_SB(                                                    "home");
1387     Element_End0();
1388 
1389     FILLING_BEGIN();
1390         if (Infos["Format"].empty())
1391         {
1392             Infos_General["Format"].From_UTF8("ADTS");
1393 
1394             Infos["Format"].From_UTF8("AAC");
1395             Infos["Format_Version"].From_UTF8(id?"Version 2":"Version 4");
1396             Infos["Format_Profile"].From_UTF8(Aac_Format_Profile(audioObjectType));
1397             Infos["CodecID"].From_Number(audioObjectType);
1398             Infos["Codec"].From_UTF8(Aac_audioObjectType(audioObjectType));
1399             if (Frequency_b)
1400                 Infos["SamplingRate"].From_Number(Frequency_b);
1401             Infos["Channel(s)"].From_UTF8(Aac_Channels_GetString(channelConfiguration));
1402             Infos["ChannelPositions"].From_UTF8(Aac_ChannelConfiguration_GetString(channelConfiguration));
1403             Infos["ChannelPositions/String2"].From_UTF8(Aac_ChannelConfiguration2_GetString(channelConfiguration));
1404             Infos["ChannelLayout"].From_UTF8(Aac_ChannelLayout_GetString(channelConfiguration));
1405             if (IsSub)
1406                 Infos["MuxingMode"].From_UTF8("ADTS");
1407         }
1408     FILLING_END();
1409 }
1410 
1411 //---------------------------------------------------------------------------
adts_variable_header()1412 void File_Aac::adts_variable_header()
1413 {
1414     //Parsing
1415     int16u  aac_frame_length, adts_buffer_fullness;
1416     Element_Begin1("adts_variable_header");
1417     Skip_SB(                                                    "copyright_id");
1418     Skip_SB(                                                    "copyright_id_start");
1419     Get_S2 (13, aac_frame_length,                               "aac_frame_length");
1420     Get_S2 (11, adts_buffer_fullness,                           "adts_buffer_fullness"); Param_Info1(adts_buffer_fullness==0x7FF?"VBR":"CBR");
1421     Get_S1 ( 2, num_raw_data_blocks,                            "num_raw_data_blocks");
1422     Element_End0();
1423 
1424     FILLING_BEGIN();
1425         if (adts_buffer_fullness==0x7FF)
1426             adts_buffer_fullness_Is7FF=true;
1427         #if MEDIAINFO_ADVANCED
1428             aac_frame_length_Total+=aac_frame_length;
1429         #endif //MEDIAINFO_ADVANCED
1430     FILLING_END();
1431 }
1432 
1433 //***************************************************************************
1434 // C++
1435 //***************************************************************************
1436 
1437 } //NameSpace
1438 
1439 #endif //MEDIAINFO_AAC_YES
1440 
1441