1 /*****************************************************************
2 |
3 |    AP4 - AAC Info
4 |
5 |    Copyright 2002-2009 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 /*----------------------------------------------------------------------
30 |   includes
31 +---------------------------------------------------------------------*/
32 #include "Ap4BitStream.h"
33 #include "Ap4Mp4AudioInfo.h"
34 #include "Ap4DataBuffer.h"
35 #include "Ap4SampleDescription.h"
36 
37 /*----------------------------------------------------------------------
38 |   constants
39 +---------------------------------------------------------------------*/
40 const unsigned int AP4_AAC_MAX_SAMPLING_FREQUENCY_INDEX = 12;
41 static const unsigned int AP4_AacSamplingFreqTable[13] =
42 {
43 	96000, 88200, 64000, 48000,
44     44100, 32000, 24000, 22050,
45     16000, 12000, 11025, 8000,
46     7350
47 };
48 
49 /*----------------------------------------------------------------------
50 |   AP4_Mp4AudioDsiParser
51 +---------------------------------------------------------------------*/
52 class AP4_Mp4AudioDsiParser
53 {
54 public:
AP4_Mp4AudioDsiParser(const AP4_UI08 * data,AP4_Size data_size)55     AP4_Mp4AudioDsiParser(const AP4_UI08* data, AP4_Size data_size) :
56         m_Data(data, data_size),
57         m_Position(0) {}
58 
BitsLeft()59     AP4_Size BitsLeft() { return 8*m_Data.GetDataSize()-m_Position; }
ReadBits(unsigned int n)60     AP4_UI32 ReadBits(unsigned int n) {
61         AP4_UI32 result = 0;
62         const AP4_UI08* data = m_Data.GetData();
63         while (n) {
64             unsigned int bits_avail = 8-(m_Position%8);
65             unsigned int chunk_size = bits_avail >= n ? n : bits_avail;
66             unsigned int chunk_bits = (((unsigned int)(data[m_Position/8]))>>(bits_avail-chunk_size))&((1<<chunk_size)-1);
67             result = (result << chunk_size) | chunk_bits;
68             n -= chunk_size;
69             m_Position += chunk_size;
70         }
71 
72         return result;
73     }
74 
75 private:
76     AP4_DataBuffer m_Data;
77     unsigned int   m_Position;
78 };
79 
80 /*----------------------------------------------------------------------
81 |   AP4_Mp4AudioDecoderConfig::AP4_Mp4AudioDecoderConfig
82 +---------------------------------------------------------------------*/
AP4_Mp4AudioDecoderConfig()83 AP4_Mp4AudioDecoderConfig::AP4_Mp4AudioDecoderConfig()
84 {
85     Reset();
86 }
87 
88 /*----------------------------------------------------------------------
89 |   AP4_Mp4AudioDecoderConfig::Reset
90 +---------------------------------------------------------------------*/
91 void
Reset()92 AP4_Mp4AudioDecoderConfig::Reset()
93 {
94     m_ObjectType             = 0;
95     m_SamplingFrequencyIndex = 0;
96     m_SamplingFrequency      = 0;
97     m_ChannelCount           = 0;
98     m_ChannelConfiguration   = CHANNEL_CONFIG_NONE;
99     m_FrameLengthFlag        = false;
100     m_DependsOnCoreCoder     = false;
101     m_CoreCoderDelay         = 0;
102     m_Extension.m_SbrPresent = false;
103     m_Extension.m_PsPresent  = false;
104     m_Extension.m_ObjectType = 0;
105     m_Extension.m_SamplingFrequencyIndex = 0;
106     m_Extension.m_SamplingFrequency      = 0;
107 }
108 
109 /*----------------------------------------------------------------------
110 |   AP4_Mp4AudioDecoderConfig::ParseAudioObjectType
111 +---------------------------------------------------------------------*/
112 AP4_Result
ParseAudioObjectType(AP4_Mp4AudioDsiParser & parser,AP4_UI08 & object_type)113 AP4_Mp4AudioDecoderConfig::ParseAudioObjectType(AP4_Mp4AudioDsiParser& parser, AP4_UI08& object_type)
114 {
115     if (parser.BitsLeft() < 5) return AP4_ERROR_INVALID_FORMAT;
116     object_type = (AP4_UI08)parser.ReadBits(5);
117 	if ((int)object_type == 31) {
118         if (parser.BitsLeft() < 6) return AP4_ERROR_INVALID_FORMAT;
119 		object_type = (AP4_UI08)(32 + parser.ReadBits(6));
120 	}
121 	return AP4_SUCCESS;
122 }
123 
124 /*----------------------------------------------------------------------
125 |   AP4_Mp4AudioDecoderConfig::ParseExtension
126 +---------------------------------------------------------------------*/
127 AP4_Result
ParseExtension(AP4_Mp4AudioDsiParser & parser)128 AP4_Mp4AudioDecoderConfig::ParseExtension(AP4_Mp4AudioDsiParser& parser)
129 {
130     if (parser.BitsLeft() < 16) return AP4_ERROR_INVALID_FORMAT;
131     unsigned int sync_extension_type = parser.ReadBits(11);
132     if (sync_extension_type == 0x2b7) {
133         AP4_Result result = ParseAudioObjectType(parser, m_Extension.m_ObjectType);
134         if (AP4_FAILED(result)) return result;
135         if (m_Extension.m_ObjectType == AP4_MPEG4_AUDIO_OBJECT_TYPE_SBR) {
136             m_Extension.m_SbrPresent = (parser.ReadBits(1) == 1);
137             if (m_Extension.m_SbrPresent) {
138                 result = ParseSamplingFrequency(parser,
139                                                 m_Extension.m_SamplingFrequencyIndex,
140                                                 m_Extension.m_SamplingFrequency);
141                 if (AP4_FAILED(result)) return result;
142                 if (parser.BitsLeft() >= 12) {
143                     sync_extension_type = parser.ReadBits(11);
144                     if (sync_extension_type == 0x548) {
145                         m_Extension.m_PsPresent = (parser.ReadBits(1) == 1);
146                     }
147                 }
148             }
149         } else if (m_Extension.m_ObjectType == AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_BSAC) {
150             m_Extension.m_SbrPresent = (parser.ReadBits(1) == 1);
151             if (m_Extension.m_SbrPresent) {
152                 result = ParseSamplingFrequency(parser,
153                                                 m_Extension.m_SamplingFrequencyIndex,
154                                                 m_Extension.m_SamplingFrequency);
155                 if (AP4_FAILED(result)) return result;
156             }
157             parser.ReadBits(4); // extensionChannelConfiguration
158         }
159     }
160 	return AP4_SUCCESS;
161 }
162 
163 /*----------------------------------------------------------------------
164 |   AP4_Mp4AudioDecoderConfig::ParseGASpecificInfo
165 +---------------------------------------------------------------------*/
166 AP4_Result
ParseGASpecificInfo(AP4_Mp4AudioDsiParser & parser)167 AP4_Mp4AudioDecoderConfig::ParseGASpecificInfo(AP4_Mp4AudioDsiParser& parser)
168 {
169     if (parser.BitsLeft() < 2) return AP4_ERROR_INVALID_FORMAT;
170 	m_FrameLengthFlag = (parser.ReadBits(1) == 1);
171 	m_DependsOnCoreCoder = (parser.ReadBits(1) == 1);
172 	if (m_DependsOnCoreCoder) {
173         if (parser.BitsLeft() < 14) return AP4_ERROR_INVALID_FORMAT;
174 		m_CoreCoderDelay = parser.ReadBits(14);
175     } else {
176         m_CoreCoderDelay = 0;
177     }
178     if (parser.BitsLeft() < 1) return AP4_ERROR_INVALID_FORMAT;
179 	unsigned int extensionFlag = parser.ReadBits(1);
180 	if (m_ChannelConfiguration == CHANNEL_CONFIG_NONE) {
181 		/*program_config_element (); */
182         return AP4_ERROR_NOT_SUPPORTED;
183 	}
184     if (m_ObjectType == AP4_MPEG4_AUDIO_OBJECT_TYPE_AAC_SCALABLE ||
185         m_ObjectType == AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_AAC_SCALABLE) {
186         if (parser.BitsLeft() < 3) return AP4_ERROR_INVALID_FORMAT;
187         parser.ReadBits(3); // layerNr
188     }
189     if (extensionFlag) {
190         if (m_ObjectType == AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_BSAC) {
191             if (parser.BitsLeft() < 16) return AP4_ERROR_INVALID_FORMAT;
192             parser.ReadBits(16); // numOfSubFrame (5); layer_length (11)
193         }
194         if (m_ObjectType == AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_AAC_LC       ||
195             m_ObjectType == AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_AAC_SCALABLE ||
196             m_ObjectType == AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_AAC_LD) {
197             if (parser.BitsLeft() < 3) return AP4_ERROR_INVALID_FORMAT;
198             parser.ReadBits(3); // aacSectionDataResilienceFlag (1)
199                                 // aacScalefactorDataResilienceFlag (1)
200                                 // aacSpectralDataResilienceFlag (1)
201         }
202         if (parser.BitsLeft() < 1) return AP4_ERROR_INVALID_FORMAT;
203         unsigned int extensionFlag3 = parser.ReadBits(1);
204         if (extensionFlag3) {
205             return AP4_ERROR_NOT_SUPPORTED;
206         }
207     }
208 
209     return AP4_SUCCESS;
210 }
211 
212 /*----------------------------------------------------------------------
213 |   AP4_Mp4AudioDecoderConfig::ParseSamplingFrequency
214 +---------------------------------------------------------------------*/
215 AP4_Result
ParseSamplingFrequency(AP4_Mp4AudioDsiParser & parser,unsigned int & sampling_frequency_index,unsigned int & sampling_frequency)216 AP4_Mp4AudioDecoderConfig::ParseSamplingFrequency(AP4_Mp4AudioDsiParser& parser,
217                                                   unsigned int&          sampling_frequency_index,
218                                                   unsigned int&          sampling_frequency)
219 {
220     if (parser.BitsLeft() < 4) {
221         return AP4_ERROR_INVALID_FORMAT;
222     }
223 
224     sampling_frequency_index = parser.ReadBits(4);
225     if (sampling_frequency_index == 0xF) {
226         if (parser.BitsLeft() < 24) {
227             return AP4_ERROR_INVALID_FORMAT;
228         }
229         sampling_frequency = parser.ReadBits(24);
230     } else if (sampling_frequency_index <= AP4_AAC_MAX_SAMPLING_FREQUENCY_INDEX) {
231         sampling_frequency = AP4_AacSamplingFreqTable[sampling_frequency_index];
232     } else {
233         sampling_frequency = 0;
234         return AP4_ERROR_INVALID_FORMAT;
235     }
236 
237     return AP4_SUCCESS;
238 }
239 
240 /*----------------------------------------------------------------------
241 |   AP4_Mp4AudioDecoderConfig::Parse
242 +---------------------------------------------------------------------*/
243 AP4_Result
Parse(const unsigned char * data,AP4_Size data_size)244 AP4_Mp4AudioDecoderConfig::Parse(const unsigned char* data,
245                                  AP4_Size             data_size)
246 {
247     AP4_Result            result;
248     AP4_Mp4AudioDsiParser bits(data, data_size);
249 
250     // default config
251     Reset();
252 
253     // parse the audio object type
254 	result = ParseAudioObjectType(bits, m_ObjectType);
255     if (AP4_FAILED(result)) return result;
256 
257     // parse the sampling frequency
258     result = ParseSamplingFrequency(bits,
259                                     m_SamplingFrequencyIndex,
260                                     m_SamplingFrequency);
261     if (AP4_FAILED(result)) return result;
262 
263     if (bits.BitsLeft() < 4) {
264         return AP4_ERROR_INVALID_FORMAT;
265     }
266 	m_ChannelConfiguration = (ChannelConfiguration)bits.ReadBits(4);
267     m_ChannelCount = (unsigned int)m_ChannelConfiguration;
268     if (m_ChannelCount == 7) {
269         m_ChannelCount = 8;
270     } else if (m_ChannelCount > 7) {
271         m_ChannelCount = 0;
272     }
273 
274 	if (m_ObjectType == AP4_MPEG4_AUDIO_OBJECT_TYPE_SBR ||
275         m_ObjectType == AP4_MPEG4_AUDIO_OBJECT_TYPE_PS) {
276 		m_Extension.m_ObjectType = AP4_MPEG4_AUDIO_OBJECT_TYPE_SBR;
277 		m_Extension.m_SbrPresent = true;
278         m_Extension.m_PsPresent  = m_ObjectType == AP4_MPEG4_AUDIO_OBJECT_TYPE_PS;
279         result = ParseSamplingFrequency(bits,
280                                         m_Extension.m_SamplingFrequencyIndex,
281                                         m_Extension.m_SamplingFrequency);
282         if (AP4_FAILED(result)) return result;
283 		result = ParseAudioObjectType(bits, m_ObjectType);
284         if (AP4_FAILED(result)) return result;
285         if (m_ObjectType == AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_BSAC) {
286             if (bits.BitsLeft() < 4) return AP4_ERROR_INVALID_FORMAT;
287             bits.ReadBits(4); // extensionChannelConfiguration (4)
288         }
289 	} else {
290         m_Extension.m_ObjectType             = 0;
291         m_Extension.m_SamplingFrequency      = 0;
292         m_Extension.m_SamplingFrequencyIndex = 0;
293         m_Extension.m_SbrPresent             = false;
294         m_Extension.m_PsPresent              = false;
295     }
296 
297 	switch (m_ObjectType) {
298         case AP4_MPEG4_AUDIO_OBJECT_TYPE_AAC_MAIN:
299         case AP4_MPEG4_AUDIO_OBJECT_TYPE_AAC_LC:
300         case AP4_MPEG4_AUDIO_OBJECT_TYPE_AAC_SSR:
301         case AP4_MPEG4_AUDIO_OBJECT_TYPE_AAC_LTP:
302         case AP4_MPEG4_AUDIO_OBJECT_TYPE_AAC_SCALABLE:
303         case AP4_MPEG4_AUDIO_OBJECT_TYPE_TWINVQ:
304         case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_AAC_LC:
305         case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_AAC_LTP:
306         case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_AAC_SCALABLE:
307         case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_AAC_LD:
308         case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_TWINVQ:
309         case AP4_MPEG4_AUDIO_OBJECT_TYPE_ER_BSAC:
310             result = ParseGASpecificInfo(bits);
311             if (result == AP4_SUCCESS) {
312                 if (m_Extension.m_ObjectType !=  AP4_MPEG4_AUDIO_OBJECT_TYPE_SBR &&
313                     bits.BitsLeft() >= 16) {
314                     result = ParseExtension(bits);
315                 }
316             }
317             if (result == AP4_ERROR_NOT_SUPPORTED) {
318                 // not a fatal error
319                 result = AP4_SUCCESS;
320             }
321             if (result != AP4_SUCCESS) return result;
322             break;
323 
324         default:
325             return AP4_ERROR_NOT_SUPPORTED;
326     }
327 
328     return AP4_SUCCESS;
329 }
330 
331 
332 
333 
334