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): Josef Hoepfl
87 Description: ADTS interface
89 ******************************************************************************/
91 #include "tpdec_adts.h"
94 #include "FDK_bitstream.h"
98 void adtsRead_CrcInit(HANDLE_ADTS pAdts
) /*!< pointer to adts crc info stucture */
100 FDKcrcInit(&pAdts
->crcInfo
, 0x8005, 0xFFFF, 16);
103 int adtsRead_CrcStartReg(
104 HANDLE_ADTS pAdts
, /*!< pointer to adts stucture */
105 HANDLE_FDK_BITSTREAM hBs
, /*!< handle to current bit buffer structure */
106 int mBits
/*!< number of bits in crc region */
109 if (pAdts
->bs
.protection_absent
) {
113 return ( FDKcrcStartReg(&pAdts
->crcInfo
, hBs
, mBits
) );
117 void adtsRead_CrcEndReg(
118 HANDLE_ADTS pAdts
, /*!< pointer to adts crc info stucture */
119 HANDLE_FDK_BITSTREAM hBs
, /*!< handle to current bit buffer structure */
120 int reg
/*!< crc region */
123 if (pAdts
->bs
.protection_absent
== 0)
125 FDKcrcEndReg(&pAdts
->crcInfo
, hBs
, reg
);
129 TRANSPORTDEC_ERROR
adtsRead_CrcCheck( HANDLE_ADTS pAdts
)
131 TRANSPORTDEC_ERROR ErrorStatus
= TRANSPORTDEC_OK
;
134 if (pAdts
->bs
.protection_absent
)
135 return TRANSPORTDEC_OK
;
137 crc
= FDKcrcGetCRC(&pAdts
->crcInfo
);
138 if (crc
!= pAdts
->crcReadValue
)
140 return (TRANSPORTDEC_CRC_ERROR
);
143 return (ErrorStatus
);
148 #define Adts_Length_SyncWord 12
149 #define Adts_Length_Id 1
150 #define Adts_Length_Layer 2
151 #define Adts_Length_ProtectionAbsent 1
152 #define Adts_Length_Profile 2
153 #define Adts_Length_SamplingFrequencyIndex 4
154 #define Adts_Length_PrivateBit 1
155 #define Adts_Length_ChannelConfiguration 3
156 #define Adts_Length_OriginalCopy 1
157 #define Adts_Length_Home 1
158 #define Adts_Length_CopyrightIdentificationBit 1
159 #define Adts_Length_CopyrightIdentificationStart 1
160 #define Adts_Length_FrameLength 13
161 #define Adts_Length_BufferFullness 11
162 #define Adts_Length_NumberOfRawDataBlocksInFrame 2
163 #define Adts_Length_CrcCheck 16
165 TRANSPORTDEC_ERROR
adtsRead_DecodeHeader(
167 CSAudioSpecificConfig
*pAsc
,
168 HANDLE_FDK_BITSTREAM hBs
,
169 const INT ignoreBufferFullness
175 INT cmp_buffer_fullness
;
176 int i
, adtsHeaderLength
;
181 CProgramConfig oldPce
;
182 /* Store the old PCE temporarily. Maybe we'll need it later if we
183 have channelConfig=0 and no PCE in this frame. */
184 FDKmemcpy(&oldPce
, &pAsc
->m_progrConfigElement
, sizeof(CProgramConfig
));
187 valBits
= FDKgetValidBits(hBs
);
189 /* adts_fixed_header */
190 bs
.mpeg_id
= FDKreadBits(hBs
, Adts_Length_Id
);
191 bs
.layer
= FDKreadBits(hBs
, Adts_Length_Layer
);
192 bs
.protection_absent
= FDKreadBits(hBs
, Adts_Length_ProtectionAbsent
);
193 bs
.profile
= FDKreadBits(hBs
, Adts_Length_Profile
);
194 bs
.sample_freq_index
= FDKreadBits(hBs
, Adts_Length_SamplingFrequencyIndex
);
195 bs
.private_bit
= FDKreadBits(hBs
, Adts_Length_PrivateBit
);
196 bs
.channel_config
= FDKreadBits(hBs
, Adts_Length_ChannelConfiguration
);
197 bs
.original
= FDKreadBits(hBs
, Adts_Length_OriginalCopy
);
198 bs
.home
= FDKreadBits(hBs
, Adts_Length_Home
);
200 /* adts_variable_header */
201 bs
.copyright_id
= FDKreadBits(hBs
, Adts_Length_CopyrightIdentificationBit
);
202 bs
.copyright_start
= FDKreadBits(hBs
, Adts_Length_CopyrightIdentificationStart
);
203 bs
.frame_length
= FDKreadBits(hBs
, Adts_Length_FrameLength
);
204 bs
.adts_fullness
= FDKreadBits(hBs
, Adts_Length_BufferFullness
);
205 bs
.num_raw_blocks
= FDKreadBits(hBs
, Adts_Length_NumberOfRawDataBlocksInFrame
);
208 adtsHeaderLength
= ADTS_HEADERLENGTH
;
210 if (!bs
.protection_absent
) {
211 FDKcrcReset(&pAdts
->crcInfo
);
212 FDKpushBack(hBs
, 56); /* complete fixed and variable header! */
213 crcReg
= FDKcrcStartReg(&pAdts
->crcInfo
, hBs
, 0);
217 if (! bs
.protection_absent
&& bs
.num_raw_blocks
>0) {
218 for (i
=0; i
<bs
.num_raw_blocks
; i
++) {
219 pAdts
->rawDataBlockDist
[i
] = (USHORT
)FDKreadBits(hBs
, 16);
220 adtsHeaderLength
+= 16;
222 /* Change raw data blocks to delta values */
223 pAdts
->rawDataBlockDist
[bs
.num_raw_blocks
] = bs
.frame_length
- 7 - bs
.num_raw_blocks
*2 - 2 ;
224 for (i
=bs
.num_raw_blocks
; i
>0; i
--) {
225 pAdts
->rawDataBlockDist
[i
] -= pAdts
->rawDataBlockDist
[i
-1];
229 /* adts_error_check */
230 if (!bs
.protection_absent
)
234 FDKcrcEndReg(&pAdts
->crcInfo
, hBs
, crcReg
);
235 crc_check
= FDKreadBits(hBs
, Adts_Length_CrcCheck
);
236 adtsHeaderLength
+= Adts_Length_CrcCheck
;
238 pAdts
->crcReadValue
= crc_check
;
239 /* Check header CRC in case of multiple raw data blocks */
240 if (bs
.num_raw_blocks
> 0) {
241 if (pAdts
->crcReadValue
!= FDKcrcGetCRC(&pAdts
->crcInfo
)) {
242 return TRANSPORTDEC_CRC_ERROR
;
244 /* Reset CRC for the upcoming raw_data_block() */
245 FDKcrcReset(&pAdts
->crcInfo
);
250 /* check if valid header */
252 (bs
.layer
!= 0) || // we only support MPEG ADTS
253 (bs
.sample_freq_index
>= 13) // we only support 96kHz - 7350kHz
255 FDKpushFor(hBs
, bs
.frame_length
* 8); // try again one frame later
256 return TRANSPORTDEC_UNSUPPORTED_FORMAT
;
259 /* special treatment of id-bit */
260 if ( (bs
.mpeg_id
== 0) && (pAdts
->decoderCanDoMpeg4
== 0) )
262 /* MPEG-2 decoder cannot play MPEG-4 bitstreams */
265 FDKpushFor(hBs
, bs
.frame_length
* 8); // try again one frame later
266 return TRANSPORTDEC_UNSUPPORTED_FORMAT
;
269 if (!ignoreBufferFullness
)
271 cmp_buffer_fullness
= bs
.frame_length
*8 + bs
.adts_fullness
*32*getNumberOfEffectiveChannels(bs
.channel_config
);
274 /* Evaluate buffer fullness */
275 if (bs
.adts_fullness
!= 0x7FF)
277 if (pAdts
->BufferFullnesStartFlag
)
279 if ( valBits
< cmp_buffer_fullness
)
281 /* Condition for start of decoding is not fulfilled */
283 /* The current frame will not be decoded */
284 FDKpushBack(hBs
, adtsHeaderLength
);
286 if ( (cmp_buffer_fullness
+adtsHeaderLength
) > ((TRANSPORTDEC_INBUF_SIZE
<<3)-7) ) {
287 return TRANSPORTDEC_SYNC_ERROR
;
289 return TRANSPORTDEC_NOT_ENOUGH_BITS
;
294 pAdts
->BufferFullnesStartFlag
= 0;
301 /* Get info from ADTS header */
302 AudioSpecificConfig_Init(pAsc
);
303 pAsc
->m_aot
= (AUDIO_OBJECT_TYPE
)(bs
.profile
+ 1);
304 pAsc
->m_samplingFrequencyIndex
= bs
.sample_freq_index
;
305 pAsc
->m_samplingFrequency
= SamplingRateTable
[bs
.sample_freq_index
];
306 pAsc
->m_channelConfiguration
= bs
.channel_config
;
307 pAsc
->m_samplesPerFrame
= 1024;
310 if (bs
.channel_config
== 0)
313 UINT alignAnchor
= FDKgetValidBits(hBs
);
315 if (FDKreadBits(hBs
,3) == ID_PCE
) {
316 /* Got luck! Parse the PCE */
318 crcReg
= adtsRead_CrcStartReg(pAdts
, hBs
, 0);
320 CProgramConfig_Read(&pAsc
->m_progrConfigElement
, hBs
, alignAnchor
);
322 adtsRead_CrcEndReg(pAdts
, hBs
, crcReg
);
323 pceBits
= alignAnchor
- FDKgetValidBits(hBs
);
324 /* store the number of PCE bits */
325 bs
.num_pce_bits
= pceBits
;
328 /* No PCE in this frame! Push back the ID tag bits. */
331 /* Encoders do not have to write a PCE in each frame.
332 So if we already have a valid PCE we have to use it. */
334 && (bs
.sample_freq_index
== pAdts
->bs
.sample_freq_index
) /* we could compare the complete fixed header (bytes) here! */
335 && (bs
.channel_config
== pAdts
->bs
.channel_config
) /* == 0 */
336 && (bs
.mpeg_id
== pAdts
->bs
.mpeg_id
) )
337 { /* Restore previous PCE which is still valid */
338 FDKmemcpy(&pAsc
->m_progrConfigElement
, &oldPce
, sizeof(CProgramConfig
));
340 else if (bs
.mpeg_id
== 0) {
341 /* If not it seems that we have a implicit channel configuration.
342 This mode is not allowed in the context of ISO/IEC 14496-3.
343 Skip this frame and try the next one. */
344 FDKpushFor(hBs
, (bs
.frame_length
<<3) - adtsHeaderLength
- 3);
345 return TRANSPORTDEC_UNSUPPORTED_FORMAT
;
348 ISO/IEC 13818-7 implicit channel mapping is allowed.
349 So just open the box of chocolates to see what we got.
353 #endif /* TP_PCE_ENABLE */
355 /* Copy bit stream data struct to persistent memory now, once we passed all sanity checks above. */
356 FDKmemcpy(&pAdts
->bs
, &bs
, sizeof(STRUCT_ADTS_BS
));
358 return TRANSPORTDEC_OK
;
361 int adtsRead_GetRawDataBlockLength(
368 if (pAdts
->bs
.num_raw_blocks
== 0) {
369 length
= (pAdts
->bs
.frame_length
- 7) << 3; /* aac_frame_length subtracted by the header size (7 bytes). */
370 if (pAdts
->bs
.protection_absent
== 0)
371 length
-= 16; /* substract 16 bit CRC */
373 if (pAdts
->bs
.protection_absent
) {
374 length
= -1; /* raw data block length is unknown */
376 if (blockNum
< 0 || blockNum
> 3) {
379 length
= (pAdts
->rawDataBlockDist
[blockNum
] << 3) - 16;
382 if (blockNum
== 0 && length
> 0) {
383 length
-= pAdts
->bs
.num_pce_bits
;