1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5 � Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6 All rights reserved.
7
8 1. INTRODUCTION
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28
29 2. COPYRIGHT LICENSE
30
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52
53 3. NO PATENT LICENSE
54
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61
62 4. DISCLAIMER
63
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72
73 5. CONTACT INFORMATION
74
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83
84 /***************************** MPEG-4 AAC Decoder **************************
85
86 Author(s): Daniel Homm
87 Description:
88
89 ******************************************************************************/
90
91 #include "tpdec_latm.h"
92
93
94 #include "FDK_bitstream.h"
95
96
97 #define TPDEC_TRACKINDEX(p,l) (2*(p) + (l))
98
99 static
CLatmDemux_GetValue(HANDLE_FDK_BITSTREAM bs)100 UINT CLatmDemux_GetValue(HANDLE_FDK_BITSTREAM bs)
101 {
102 UCHAR bytesForValue = 0, tmp = 0;
103 int value = 0;
104
105 bytesForValue = (UCHAR) FDKreadBits(bs,2);
106
107 for (UINT i=0; i<=bytesForValue; i++) {
108 value <<= 8;
109 tmp = (UCHAR) FDKreadBits(bs,8);
110 value += tmp;
111 }
112
113 return value;
114 }
115
116
117 static
CLatmDemux_ReadAudioMuxElement(HANDLE_FDK_BITSTREAM bs,CLatmDemux * pLatmDemux,int m_muxConfigPresent,CSTpCallBacks * pTpDecCallbacks,CSAudioSpecificConfig * pAsc)118 TRANSPORTDEC_ERROR CLatmDemux_ReadAudioMuxElement(
119 HANDLE_FDK_BITSTREAM bs,
120 CLatmDemux *pLatmDemux,
121 int m_muxConfigPresent,
122 CSTpCallBacks *pTpDecCallbacks,
123 CSAudioSpecificConfig *pAsc
124 )
125 {
126 TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
127
128 if (m_muxConfigPresent) {
129 pLatmDemux->m_useSameStreamMux = FDKreadBits(bs,1);
130
131 if (!pLatmDemux->m_useSameStreamMux) {
132 if ((ErrorStatus = CLatmDemux_ReadStreamMuxConfig(bs, pLatmDemux, pTpDecCallbacks, pAsc))) {
133 return (ErrorStatus);
134 }
135 }
136 }
137
138 if (pLatmDemux->m_AudioMuxVersionA == 0) {
139 /* Do only once per call, because parsing and decoding is done in-line. */
140 if ((ErrorStatus = CLatmDemux_ReadPayloadLengthInfo(bs,pLatmDemux))) {
141 return (ErrorStatus);
142 }
143 } else {
144 /* audioMuxVersionA > 0 is reserved for future extensions */
145 ErrorStatus = TRANSPORTDEC_UNSUPPORTED_FORMAT;
146 }
147
148 return (ErrorStatus);
149 }
150
CLatmDemux_Read(HANDLE_FDK_BITSTREAM bs,CLatmDemux * pLatmDemux,TRANSPORT_TYPE tt,CSTpCallBacks * pTpDecCallbacks,CSAudioSpecificConfig * pAsc,const INT ignoreBufferFullness)151 TRANSPORTDEC_ERROR CLatmDemux_Read(
152 HANDLE_FDK_BITSTREAM bs,
153 CLatmDemux *pLatmDemux,
154 TRANSPORT_TYPE tt,
155 CSTpCallBacks *pTpDecCallbacks,
156 CSAudioSpecificConfig *pAsc,
157 const INT ignoreBufferFullness
158 )
159 {
160 UINT cntBits;
161 UINT cmpBufferFullness;
162 UINT audioMuxLengthBytesLast = 0;
163 TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
164
165 cntBits = FDKgetValidBits(bs);
166
167 if ((INT)cntBits < MIN_LATM_HEADERLENGTH) {
168 return TRANSPORTDEC_NOT_ENOUGH_BITS;
169 }
170
171 if ((ErrorStatus = CLatmDemux_ReadAudioMuxElement(bs, pLatmDemux, (tt != TT_MP4_LATM_MCP0), pTpDecCallbacks, pAsc)))
172 return (ErrorStatus);
173
174 if (!ignoreBufferFullness)
175 {
176 cmpBufferFullness = 24+audioMuxLengthBytesLast*8
177 + pLatmDemux->m_linfo[0][0].m_bufferFullness* pAsc[TPDEC_TRACKINDEX(0,0)].m_channelConfiguration*32;
178
179 /* evaluate buffer fullness */
180
181 if (pLatmDemux->m_linfo[0][0].m_bufferFullness != 0xFF)
182 {
183 if (!pLatmDemux->BufferFullnessAchieved)
184 {
185 if (cntBits < cmpBufferFullness)
186 {
187 /* condition for start of decoding is not fulfilled */
188
189 /* the current frame will not be decoded */
190 return TRANSPORTDEC_NOT_ENOUGH_BITS;
191 }
192 else
193 {
194 pLatmDemux->BufferFullnessAchieved = 1;
195 }
196 }
197 }
198 }
199
200 return (ErrorStatus);
201 }
202
203
CLatmDemux_ReadStreamMuxConfig(HANDLE_FDK_BITSTREAM bs,CLatmDemux * pLatmDemux,CSTpCallBacks * pTpDecCallbacks,CSAudioSpecificConfig * pAsc)204 TRANSPORTDEC_ERROR CLatmDemux_ReadStreamMuxConfig(
205 HANDLE_FDK_BITSTREAM bs,
206 CLatmDemux *pLatmDemux,
207 CSTpCallBacks *pTpDecCallbacks,
208 CSAudioSpecificConfig *pAsc
209 )
210 {
211 LATM_LAYER_INFO *p_linfo = NULL;
212 TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
213
214 pLatmDemux->m_AudioMuxVersion = FDKreadBits(bs,1);
215
216 if (pLatmDemux->m_AudioMuxVersion == 0) {
217 pLatmDemux->m_AudioMuxVersionA = 0;
218 } else {
219 pLatmDemux->m_AudioMuxVersionA = FDKreadBits(bs,1);
220 }
221
222 if (pLatmDemux->m_AudioMuxVersionA == 0) {
223 if (pLatmDemux->m_AudioMuxVersion == 1) {
224 pLatmDemux->m_taraBufferFullness = CLatmDemux_GetValue(bs);
225 }
226 pLatmDemux->m_allStreamsSameTimeFraming = FDKreadBits(bs,1);
227 pLatmDemux->m_noSubFrames = FDKreadBits(bs,6) + 1;
228 pLatmDemux->m_numProgram = FDKreadBits(bs,4) + 1;
229
230 if (pLatmDemux->m_numProgram > 1) {
231 return TRANSPORTDEC_UNSUPPORTED_FORMAT;
232 }
233
234 int idCnt = 0;
235 for (UINT prog = 0; prog < pLatmDemux->m_numProgram; prog++) {
236 pLatmDemux->m_numLayer = FDKreadBits(bs,3) + 1;
237 if (pLatmDemux->m_numLayer > 2) {
238 return TRANSPORTDEC_UNSUPPORTED_FORMAT;
239 }
240
241 for (UINT lay = 0; lay < pLatmDemux->m_numLayer; lay++) {
242 p_linfo = &pLatmDemux->m_linfo[prog][lay];
243
244 p_linfo->m_streamID = idCnt++;
245 p_linfo->m_frameLengthInBits = 0;
246
247 if( (prog == 0) && (lay == 0) ) {
248 pLatmDemux->m_useSameConfig = 0;
249 } else {
250 pLatmDemux->m_useSameConfig = FDKreadBits(bs,1);
251 }
252
253 if (pLatmDemux->m_useSameConfig) {
254 if (lay > 1) {
255 FDKmemcpy(&pAsc[TPDEC_TRACKINDEX(prog,lay)], &pAsc[TPDEC_TRACKINDEX(prog,lay-1)], sizeof(CSAudioSpecificConfig));
256 } else {
257 return TRANSPORTDEC_PARSE_ERROR;
258 }
259 } else {
260 if (pLatmDemux->m_AudioMuxVersion == 1)
261 {
262 FDK_BITSTREAM tmpBs;
263 UINT ascStartPos, ascLen=0;
264
265 ascLen = CLatmDemux_GetValue(bs);
266 ascStartPos = FDKgetValidBits(bs);
267 tmpBs = *bs;
268 FDKsyncCache(&tmpBs);
269 tmpBs.hBitBuf.ValidBits = ascLen;
270
271 /* Read ASC */
272 if ((ErrorStatus = AudioSpecificConfig_Parse(&pAsc[TPDEC_TRACKINDEX(prog,lay)], &tmpBs, 1, pTpDecCallbacks))) {
273 return (ErrorStatus);
274 }
275
276 /* The field p_linfo->m_ascLen could be wrong, so check if */
277 if ( 0 > (INT)FDKgetValidBits(&tmpBs)) {
278 return TRANSPORTDEC_PARSE_ERROR;
279 }
280 FDKpushFor(bs, ascLen); /* position bitstream after ASC */
281 }
282 else {
283 /* Read ASC */
284 if ((ErrorStatus = AudioSpecificConfig_Parse(&pAsc[TPDEC_TRACKINDEX(prog,lay)], bs, 0, pTpDecCallbacks))) {
285 return (ErrorStatus);
286 }
287 }
288 {
289 int cbError;
290
291 cbError = pTpDecCallbacks->cbUpdateConfig(pTpDecCallbacks->cbUpdateConfigData, &pAsc[TPDEC_TRACKINDEX(prog,lay)]);
292 if (cbError != 0) {
293 return TRANSPORTDEC_UNKOWN_ERROR;
294 }
295 }
296 }
297
298 p_linfo->m_frameLengthType = FDKreadBits(bs,3);
299 switch( p_linfo->m_frameLengthType ) {
300 case 0:
301 p_linfo->m_bufferFullness = FDKreadBits(bs,8);
302
303 if (!pLatmDemux->m_allStreamsSameTimeFraming) {
304 if ((lay > 0) && (pAsc[TPDEC_TRACKINDEX(prog,lay)].m_aot == AOT_AAC_SCAL || pAsc[TPDEC_TRACKINDEX(prog,lay)].m_aot == AOT_ER_AAC_SCAL)) {
305 return TRANSPORTDEC_UNSUPPORTED_FORMAT;
306 }
307 }
308 break;
309 case 1:
310 /* frameLength = FDKreadBits(bs,9); */
311 case 3:
312 case 4:
313 case 5:
314 /* CELP */
315 case 6:
316 case 7:
317 /* HVXC */
318 default:
319 return TRANSPORTDEC_PARSE_ERROR; //_LATM_INVALIDFRAMELENGTHTYPE;
320
321 } /* switch framelengthtype*/
322
323 } /* layer loop */
324 } /* prog loop */
325
326 pLatmDemux->m_otherDataPresent = FDKreadBits(bs,1);
327 pLatmDemux->m_otherDataLength = 0;
328
329 if (pLatmDemux->m_otherDataPresent) {
330 int otherDataLenEsc = 0;
331 do {
332 pLatmDemux->m_otherDataLength <<= 8; // *= 256
333 otherDataLenEsc = FDKreadBits(bs,1);
334 pLatmDemux->m_otherDataLength += FDKreadBits(bs,8);
335 } while (otherDataLenEsc);
336 }
337
338 pLatmDemux->m_crcCheckPresent = FDKreadBits(bs,1);
339 pLatmDemux->m_crcCheckSum = 0;
340
341 if (pLatmDemux->m_crcCheckPresent) {
342 pLatmDemux->m_crcCheckSum = FDKreadBits(bs,8);
343 }
344
345 }
346 else {
347 /* audioMuxVersionA > 0 is reserved for future extensions */
348 ErrorStatus = TRANSPORTDEC_UNSUPPORTED_FORMAT;
349 }
350 return (ErrorStatus);
351 }
352
CLatmDemux_ReadPayloadLengthInfo(HANDLE_FDK_BITSTREAM bs,CLatmDemux * pLatmDemux)353 TRANSPORTDEC_ERROR CLatmDemux_ReadPayloadLengthInfo(HANDLE_FDK_BITSTREAM bs, CLatmDemux *pLatmDemux)
354 {
355 TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
356 int totalPayloadBits = 0;
357
358 if( pLatmDemux->m_allStreamsSameTimeFraming == 1 ) {
359 for (UINT prog=0; prog<pLatmDemux->m_numProgram; prog++ ) {
360 for (UINT lay=0; lay<pLatmDemux->m_numLayer; lay++ ) {
361 LATM_LAYER_INFO *p_linfo = &pLatmDemux->m_linfo[prog][lay];
362
363 switch (p_linfo->m_frameLengthType ) {
364 case 0:
365 p_linfo->m_frameLengthInBits = CLatmDemux_ReadAuChunkLengthInfo(bs);
366 totalPayloadBits += p_linfo->m_frameLengthInBits;
367 break;
368 case 3:
369 case 5:
370 case 7:
371 default:
372 return TRANSPORTDEC_PARSE_ERROR; //AAC_DEC_LATM_INVALIDFRAMELENGTHTYPE;
373 }
374 }
375 }
376 }
377 else {
378 ErrorStatus = TRANSPORTDEC_PARSE_ERROR; //AAC_DEC_LATM_TIMEFRAMING;
379 }
380 if (pLatmDemux->m_audioMuxLengthBytes > 0 && totalPayloadBits > pLatmDemux->m_audioMuxLengthBytes*8) {
381 return TRANSPORTDEC_PARSE_ERROR;
382 }
383 return (ErrorStatus);
384 }
385
CLatmDemux_ReadAuChunkLengthInfo(HANDLE_FDK_BITSTREAM bs)386 int CLatmDemux_ReadAuChunkLengthInfo(HANDLE_FDK_BITSTREAM bs)
387 {
388 UCHAR endFlag;
389 int len = 0;
390
391 do {
392 UCHAR tmp = (UCHAR) FDKreadBits(bs,8);
393 endFlag = (tmp < 255);
394
395 len += tmp;
396
397 } while( endFlag == 0 );
398
399 len <<= 3; /* convert from bytes to bits */
400
401 return len;
402 }
403
CLatmDemux_GetFrameLengthInBits(CLatmDemux * pLatmDemux)404 int CLatmDemux_GetFrameLengthInBits(CLatmDemux *pLatmDemux)
405 {
406 return pLatmDemux->m_linfo[0][0].m_frameLengthInBits;
407 }
408
CLatmDemux_GetOtherDataPresentFlag(CLatmDemux * pLatmDemux)409 int CLatmDemux_GetOtherDataPresentFlag(CLatmDemux *pLatmDemux)
410 {
411 return pLatmDemux->m_otherDataPresent ? 1 : 0;
412 }
413
CLatmDemux_GetOtherDataLength(CLatmDemux * pLatmDemux)414 int CLatmDemux_GetOtherDataLength(CLatmDemux *pLatmDemux)
415 {
416 return pLatmDemux->m_otherDataLength;
417 }
418
CLatmDemux_GetNrOfSubFrames(CLatmDemux * pLatmDemux)419 UINT CLatmDemux_GetNrOfSubFrames(CLatmDemux *pLatmDemux)
420 {
421 return pLatmDemux->m_noSubFrames;
422 }
423
424