2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
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.
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.
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.
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.
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:
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.
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.
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
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.
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."
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.
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
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.
73 5. CONTACT INFORMATION
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
78 91058 Erlangen, Germany
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
84 /***************************** MPEG-4 AAC Decoder **************************
86 Author(s): Daniel Homm
89 ******************************************************************************/
91 #include "tpdec_latm.h"
94 #include "FDK_bitstream.h"
97 #define TPDEC_TRACKINDEX(p,l) (2*(p) + (l))
100 UINT
CLatmDemux_GetValue(HANDLE_FDK_BITSTREAM bs
)
102 UCHAR bytesForValue
= 0, tmp
= 0;
105 bytesForValue
= (UCHAR
) FDKreadBits(bs
,2);
107 for (UINT i
=0; i
<=bytesForValue
; i
++) {
109 tmp
= (UCHAR
) FDKreadBits(bs
,8);
118 TRANSPORTDEC_ERROR
CLatmDemux_ReadAudioMuxElement(
119 HANDLE_FDK_BITSTREAM bs
,
120 CLatmDemux
*pLatmDemux
,
121 int m_muxConfigPresent
,
122 CSTpCallBacks
*pTpDecCallbacks
,
123 CSAudioSpecificConfig
*pAsc
,
127 TRANSPORTDEC_ERROR ErrorStatus
= TRANSPORTDEC_OK
;
129 if (m_muxConfigPresent
) {
130 pLatmDemux
->m_useSameStreamMux
= FDKreadBits(bs
,1);
132 if (!pLatmDemux
->m_useSameStreamMux
) {
133 if ((ErrorStatus
= CLatmDemux_ReadStreamMuxConfig(bs
, pLatmDemux
, pTpDecCallbacks
, pAsc
, pfConfigFound
))) {
134 return (ErrorStatus
);
139 /* If there was no configuration read, its not possible to parse PayloadLengthInfo below. */
140 if (! *pfConfigFound
) {
141 return TRANSPORTDEC_SYNC_ERROR
;
144 if (pLatmDemux
->m_AudioMuxVersionA
== 0) {
145 /* Do only once per call, because parsing and decoding is done in-line. */
146 if ((ErrorStatus
= CLatmDemux_ReadPayloadLengthInfo(bs
,pLatmDemux
))) {
147 return (ErrorStatus
);
150 /* audioMuxVersionA > 0 is reserved for future extensions */
151 ErrorStatus
= TRANSPORTDEC_UNSUPPORTED_FORMAT
;
154 return (ErrorStatus
);
157 TRANSPORTDEC_ERROR
CLatmDemux_Read(
158 HANDLE_FDK_BITSTREAM bs
,
159 CLatmDemux
*pLatmDemux
,
161 CSTpCallBacks
*pTpDecCallbacks
,
162 CSAudioSpecificConfig
*pAsc
,
164 const INT ignoreBufferFullness
168 UINT cmpBufferFullness
;
169 UINT audioMuxLengthBytesLast
= 0;
170 TRANSPORTDEC_ERROR ErrorStatus
= TRANSPORTDEC_OK
;
172 cntBits
= FDKgetValidBits(bs
);
174 if ((INT
)cntBits
< MIN_LATM_HEADERLENGTH
) {
175 return TRANSPORTDEC_NOT_ENOUGH_BITS
;
178 if ((ErrorStatus
= CLatmDemux_ReadAudioMuxElement(bs
, pLatmDemux
, (tt
!= TT_MP4_LATM_MCP0
), pTpDecCallbacks
, pAsc
, pfConfigFound
)))
179 return (ErrorStatus
);
181 if (!ignoreBufferFullness
)
183 cmpBufferFullness
= 24+audioMuxLengthBytesLast
*8
184 + pLatmDemux
->m_linfo
[0][0].m_bufferFullness
* pAsc
[TPDEC_TRACKINDEX(0,0)].m_channelConfiguration
*32;
186 /* evaluate buffer fullness */
188 if (pLatmDemux
->m_linfo
[0][0].m_bufferFullness
!= 0xFF)
190 if (!pLatmDemux
->BufferFullnessAchieved
)
192 if (cntBits
< cmpBufferFullness
)
194 /* condition for start of decoding is not fulfilled */
196 /* the current frame will not be decoded */
197 return TRANSPORTDEC_NOT_ENOUGH_BITS
;
201 pLatmDemux
->BufferFullnessAchieved
= 1;
207 return (ErrorStatus
);
211 TRANSPORTDEC_ERROR
CLatmDemux_ReadStreamMuxConfig(
212 HANDLE_FDK_BITSTREAM bs
,
213 CLatmDemux
*pLatmDemux
,
214 CSTpCallBacks
*pTpDecCallbacks
,
215 CSAudioSpecificConfig
*pAsc
,
219 LATM_LAYER_INFO
*p_linfo
= NULL
;
220 TRANSPORTDEC_ERROR ErrorStatus
= TRANSPORTDEC_OK
;
222 pLatmDemux
->m_AudioMuxVersion
= FDKreadBits(bs
,1);
224 if (pLatmDemux
->m_AudioMuxVersion
== 0) {
225 pLatmDemux
->m_AudioMuxVersionA
= 0;
227 pLatmDemux
->m_AudioMuxVersionA
= FDKreadBits(bs
,1);
230 if (pLatmDemux
->m_AudioMuxVersionA
== 0) {
231 if (pLatmDemux
->m_AudioMuxVersion
== 1) {
232 pLatmDemux
->m_taraBufferFullness
= CLatmDemux_GetValue(bs
);
234 pLatmDemux
->m_allStreamsSameTimeFraming
= FDKreadBits(bs
,1);
235 pLatmDemux
->m_noSubFrames
= FDKreadBits(bs
,6) + 1;
236 pLatmDemux
->m_numProgram
= FDKreadBits(bs
,4) + 1;
238 if (pLatmDemux
->m_numProgram
> 1) {
239 return TRANSPORTDEC_UNSUPPORTED_FORMAT
;
243 for (UINT prog
= 0; prog
< pLatmDemux
->m_numProgram
; prog
++) {
244 pLatmDemux
->m_numLayer
= FDKreadBits(bs
,3) + 1;
245 if (pLatmDemux
->m_numLayer
> 2) {
246 return TRANSPORTDEC_UNSUPPORTED_FORMAT
;
249 for (UINT lay
= 0; lay
< pLatmDemux
->m_numLayer
; lay
++) {
250 p_linfo
= &pLatmDemux
->m_linfo
[prog
][lay
];
252 p_linfo
->m_streamID
= idCnt
++;
253 p_linfo
->m_frameLengthInBits
= 0;
255 if( (prog
== 0) && (lay
== 0) ) {
256 pLatmDemux
->m_useSameConfig
= 0;
258 pLatmDemux
->m_useSameConfig
= FDKreadBits(bs
,1);
261 if (pLatmDemux
->m_useSameConfig
) {
263 FDKmemcpy(&pAsc
[TPDEC_TRACKINDEX(prog
,lay
)], &pAsc
[TPDEC_TRACKINDEX(prog
,lay
-1)], sizeof(CSAudioSpecificConfig
));
265 return TRANSPORTDEC_PARSE_ERROR
;
268 if (pLatmDemux
->m_AudioMuxVersion
== 1)
271 UINT ascStartPos
, ascLen
=0;
273 ascLen
= CLatmDemux_GetValue(bs
);
274 ascStartPos
= FDKgetValidBits(bs
);
276 FDKsyncCache(&tmpBs
);
277 tmpBs
.hBitBuf
.ValidBits
= ascLen
;
280 if ((ErrorStatus
= AudioSpecificConfig_Parse(&pAsc
[TPDEC_TRACKINDEX(prog
,lay
)], &tmpBs
, 1, pTpDecCallbacks
))) {
281 return (ErrorStatus
);
285 /* The field p_linfo->m_ascLen could be wrong, so check if */
286 if ( 0 > (INT
)FDKgetValidBits(&tmpBs
)) {
287 return TRANSPORTDEC_PARSE_ERROR
;
289 FDKpushFor(bs
, ascLen
); /* position bitstream after ASC */
293 if ((ErrorStatus
= AudioSpecificConfig_Parse(&pAsc
[TPDEC_TRACKINDEX(prog
,lay
)], bs
, 0, pTpDecCallbacks
))) {
294 return (ErrorStatus
);
300 cbError
= pTpDecCallbacks
->cbUpdateConfig(pTpDecCallbacks
->cbUpdateConfigData
, &pAsc
[TPDEC_TRACKINDEX(prog
,lay
)]);
302 return TRANSPORTDEC_UNKOWN_ERROR
;
308 p_linfo
->m_frameLengthType
= FDKreadBits(bs
,3);
309 switch( p_linfo
->m_frameLengthType
) {
311 p_linfo
->m_bufferFullness
= FDKreadBits(bs
,8);
313 if (!pLatmDemux
->m_allStreamsSameTimeFraming
) {
314 if ((lay
> 0) && (pAsc
[TPDEC_TRACKINDEX(prog
,lay
)].m_aot
== AOT_AAC_SCAL
|| pAsc
[TPDEC_TRACKINDEX(prog
,lay
)].m_aot
== AOT_ER_AAC_SCAL
)) {
315 return TRANSPORTDEC_UNSUPPORTED_FORMAT
;
320 /* frameLength = FDKreadBits(bs,9); */
329 return TRANSPORTDEC_PARSE_ERROR
; //_LATM_INVALIDFRAMELENGTHTYPE;
331 } /* switch framelengthtype*/
336 pLatmDemux
->m_otherDataPresent
= FDKreadBits(bs
,1);
337 pLatmDemux
->m_otherDataLength
= 0;
339 if (pLatmDemux
->m_otherDataPresent
) {
340 int otherDataLenEsc
= 0;
342 pLatmDemux
->m_otherDataLength
<<= 8; // *= 256
343 otherDataLenEsc
= FDKreadBits(bs
,1);
344 pLatmDemux
->m_otherDataLength
+= FDKreadBits(bs
,8);
345 } while (otherDataLenEsc
);
348 pLatmDemux
->m_crcCheckPresent
= FDKreadBits(bs
,1);
349 pLatmDemux
->m_crcCheckSum
= 0;
351 if (pLatmDemux
->m_crcCheckPresent
) {
352 pLatmDemux
->m_crcCheckSum
= FDKreadBits(bs
,8);
357 /* audioMuxVersionA > 0 is reserved for future extensions */
358 ErrorStatus
= TRANSPORTDEC_UNSUPPORTED_FORMAT
;
360 return (ErrorStatus
);
363 TRANSPORTDEC_ERROR
CLatmDemux_ReadPayloadLengthInfo(HANDLE_FDK_BITSTREAM bs
, CLatmDemux
*pLatmDemux
)
365 TRANSPORTDEC_ERROR ErrorStatus
= TRANSPORTDEC_OK
;
366 int totalPayloadBits
= 0;
368 if( pLatmDemux
->m_allStreamsSameTimeFraming
== 1 ) {
369 for (UINT prog
=0; prog
<pLatmDemux
->m_numProgram
; prog
++ ) {
370 for (UINT lay
=0; lay
<pLatmDemux
->m_numLayer
; lay
++ ) {
371 LATM_LAYER_INFO
*p_linfo
= &pLatmDemux
->m_linfo
[prog
][lay
];
373 switch (p_linfo
->m_frameLengthType
) {
375 p_linfo
->m_frameLengthInBits
= CLatmDemux_ReadAuChunkLengthInfo(bs
);
376 totalPayloadBits
+= p_linfo
->m_frameLengthInBits
;
382 return TRANSPORTDEC_PARSE_ERROR
; //AAC_DEC_LATM_INVALIDFRAMELENGTHTYPE;
388 ErrorStatus
= TRANSPORTDEC_PARSE_ERROR
; //AAC_DEC_LATM_TIMEFRAMING;
390 if (pLatmDemux
->m_audioMuxLengthBytes
> (UINT
)0 && totalPayloadBits
> (int)pLatmDemux
->m_audioMuxLengthBytes
*8) {
391 return TRANSPORTDEC_PARSE_ERROR
;
393 return (ErrorStatus
);
396 int CLatmDemux_ReadAuChunkLengthInfo(HANDLE_FDK_BITSTREAM bs
)
402 UCHAR tmp
= (UCHAR
) FDKreadBits(bs
,8);
403 endFlag
= (tmp
< 255);
407 } while( endFlag
== 0 );
409 len
<<= 3; /* convert from bytes to bits */
414 int CLatmDemux_GetFrameLengthInBits(CLatmDemux
*pLatmDemux
)
416 return pLatmDemux
->m_linfo
[0][0].m_frameLengthInBits
;
419 int CLatmDemux_GetOtherDataPresentFlag(CLatmDemux
*pLatmDemux
)
421 return pLatmDemux
->m_otherDataPresent
? 1 : 0;
424 int CLatmDemux_GetOtherDataLength(CLatmDemux
*pLatmDemux
)
426 return pLatmDemux
->m_otherDataLength
;
429 UINT
CLatmDemux_GetNrOfSubFrames(CLatmDemux
*pLatmDemux
)
431 return pLatmDemux
->m_noSubFrames
;