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
89 ******************************************************************************/
92 #include "aacdecoder.h"
94 #include "aacdec_tns.h"
95 #include "FDK_bitstream.h"
96 #include "FDK_tools_rom.h"
102 #include "aacdec_hcr.h"
106 void MapMidSideMaskToPnsCorrelation (CAacDecoderChannelInfo
*pAacDecoderChannelInfo
[2])
110 for (group
= 0 ; group
< pAacDecoderChannelInfo
[L
]->icsInfo
.WindowGroups
; group
++) {
111 UCHAR groupMask
= 1 << group
;
113 for (UCHAR band
= 0 ; band
< pAacDecoderChannelInfo
[L
]->icsInfo
.MaxSfBands
; band
++) {
114 if (pAacDecoderChannelInfo
[L
]->pComData
->jointStereoData
.MsUsed
[band
] & groupMask
) { /* channels are correlated */
115 CPns_SetCorrelation(&pAacDecoderChannelInfo
[L
]->data
.aac
.PnsData
, group
, band
, 0);
117 if (CPns_IsPnsUsed(&pAacDecoderChannelInfo
[L
]->data
.aac
.PnsData
, group
, band
) &&
118 CPns_IsPnsUsed(&pAacDecoderChannelInfo
[R
]->data
.aac
.PnsData
, group
, band
))
119 pAacDecoderChannelInfo
[L
]->pComData
->jointStereoData
.MsUsed
[band
] ^= groupMask
; /* clear the groupMask-bit */
126 \brief Decode channel pair element
128 The function decodes a channel pair element.
132 void CChannelElement_Decode( CAacDecoderChannelInfo
*pAacDecoderChannelInfo
[2], /*!< pointer to aac decoder channel info */
133 CAacDecoderStaticChannelInfo
*pAacDecoderStaticChannelInfo
[2],
134 SamplingRateInfo
*pSamplingRateInfo
,
138 int ch
, maybe_jstereo
= 0;
140 maybe_jstereo
= (el_channels
> 1);
142 for (ch
= 0; ch
< el_channels
; ch
++) {
143 if ( pAacDecoderChannelInfo
[ch
]->renderMode
== AACDEC_RENDER_IMDCT
144 || pAacDecoderChannelInfo
[ch
]->renderMode
== AACDEC_RENDER_ELDFB
)
146 CBlock_InverseQuantizeSpectralData(pAacDecoderChannelInfo
[ch
], pSamplingRateInfo
);
154 if (pAacDecoderChannelInfo
[L
]->pDynData
->RawDataInfo
.CommonWindow
) {
155 int maxSfBandsL
= GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo
[L
]->icsInfo
);
156 int maxSfBandsR
= GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo
[R
]->icsInfo
);
157 if (pAacDecoderChannelInfo
[L
]->data
.aac
.PnsData
.PnsActive
|| pAacDecoderChannelInfo
[R
]->data
.aac
.PnsData
.PnsActive
) {
158 MapMidSideMaskToPnsCorrelation(pAacDecoderChannelInfo
);
161 CJointStereo_ApplyMS(pAacDecoderChannelInfo
,
162 GetScaleFactorBandOffsets(&pAacDecoderChannelInfo
[L
]->icsInfo
, pSamplingRateInfo
),
163 GetWindowGroupLengthTable(&pAacDecoderChannelInfo
[L
]->icsInfo
),
164 GetWindowGroups(&pAacDecoderChannelInfo
[L
]->icsInfo
),
169 /* apply intensity stereo */ /* modifies pAacDecoderChannelInfo[]->aSpecSfb */
170 CJointStereo_ApplyIS(pAacDecoderChannelInfo
,
171 GetScaleFactorBandOffsets(&pAacDecoderChannelInfo
[L
]->icsInfo
, pSamplingRateInfo
),
172 GetWindowGroupLengthTable(&pAacDecoderChannelInfo
[L
]->icsInfo
),
173 GetWindowGroups(&pAacDecoderChannelInfo
[L
]->icsInfo
),
174 GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo
[L
]->icsInfo
),
175 pAacDecoderChannelInfo
[L
]->pDynData
->RawDataInfo
.CommonWindow
? 1 : 0);
179 for (ch
= 0; ch
< el_channels
; ch
++)
182 /* write pAacDecoderChannelInfo[ch]->specScale */
183 CBlock_ScaleSpectralData(pAacDecoderChannelInfo
[ch
], pSamplingRateInfo
);
185 ApplyTools (pAacDecoderChannelInfo
, pSamplingRateInfo
, flags
, ch
);
191 pAacDecoderChannelInfo
,
192 pAacDecoderStaticChannelInfo
,
198 void CChannel_CodebookTableInit(CAacDecoderChannelInfo
*pAacDecoderChannelInfo
)
200 int b
, w
, maxBands
, maxWindows
;
201 int maxSfb
= GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo
->icsInfo
);
202 UCHAR
*pCodeBook
= pAacDecoderChannelInfo
->pDynData
->aCodeBook
;
204 if ( IsLongBlock(&pAacDecoderChannelInfo
->icsInfo
) ) {
212 for (w
= 0; w
<maxWindows
; w
++) {
213 for (b
= 0; b
< maxSfb
; b
++) {
214 pCodeBook
[b
] = ESCBOOK
;
216 for (; b
<maxBands
; b
++) {
217 pCodeBook
[b
] = ZERO_HCB
;
219 pCodeBook
+= maxBands
;
225 * Arbitrary order bitstream parser
228 AAC_DECODER_ERROR
CChannelElement_Read(HANDLE_FDK_BITSTREAM hBs
,
229 CAacDecoderChannelInfo
*pAacDecoderChannelInfo
[],
230 CAacDecoderStaticChannelInfo
*pAacDecoderStaticChannelInfo
[],
231 const AUDIO_OBJECT_TYPE aot
,
232 const SamplingRateInfo
*pSamplingRateInfo
,
234 const UINT frame_length
,
235 const UCHAR numberOfChannels
,
236 const SCHAR epConfig
,
237 HANDLE_TRANSPORTDEC pTpDec
240 AAC_DECODER_ERROR error
= AAC_DEC_OK
;
241 const element_list_t
*list
;
242 int i
, ch
, decision_bit
;
243 int crcReg1
= -1, crcReg2
= -1;
245 FDK_ASSERT( (numberOfChannels
== 1) || (numberOfChannels
== 2) );
247 /* Get channel element sequence table */
248 list
= getBitstreamElementList(aot
, epConfig
, numberOfChannels
, 0);
250 error
= AAC_DEC_UNSUPPORTED_FORMAT
;
254 CTns_Reset(&pAacDecoderChannelInfo
[0]->pDynData
->TnsData
);
255 if (numberOfChannels
== 2) {
256 CTns_Reset(&pAacDecoderChannelInfo
[1]->pDynData
->TnsData
);
259 if (flags
& (AC_ELD
|AC_SCALABLE
)) {
260 pAacDecoderChannelInfo
[0]->pDynData
->RawDataInfo
.CommonWindow
= 1;
261 if (numberOfChannels
== 2) {
262 pAacDecoderChannelInfo
[1]->pDynData
->RawDataInfo
.CommonWindow
= pAacDecoderChannelInfo
[0]->pDynData
->RawDataInfo
.CommonWindow
;
264 if (numberOfChannels
== 2) {
265 pAacDecoderChannelInfo
[1]->pDynData
->RawDataInfo
.CommonWindow
= pAacDecoderChannelInfo
[0]->pDynData
->RawDataInfo
.CommonWindow
;
269 /* Iterate through sequence table */
274 switch (list
->id
[i
]) {
275 case element_instance_tag
:
276 pAacDecoderChannelInfo
[0]->ElementInstanceTag
= FDKreadBits(hBs
, 4);
277 if (numberOfChannels
== 2) {
278 pAacDecoderChannelInfo
[1]->ElementInstanceTag
= pAacDecoderChannelInfo
[0]->ElementInstanceTag
;
282 decision_bit
= pAacDecoderChannelInfo
[ch
]->pDynData
->RawDataInfo
.CommonWindow
= FDKreadBits(hBs
, 1);
283 if (numberOfChannels
== 2) {
284 pAacDecoderChannelInfo
[1]->pDynData
->RawDataInfo
.CommonWindow
= pAacDecoderChannelInfo
[0]->pDynData
->RawDataInfo
.CommonWindow
;
288 /* Read individual channel info */
289 error
= IcsRead( hBs
,
290 &pAacDecoderChannelInfo
[ch
]->icsInfo
,
294 if (numberOfChannels
== 2 && pAacDecoderChannelInfo
[0]->pDynData
->RawDataInfo
.CommonWindow
) {
295 pAacDecoderChannelInfo
[1]->icsInfo
= pAacDecoderChannelInfo
[0]->icsInfo
;
300 case ltp_data_present
:
301 if (FDKreadBits(hBs
, 1) != 0) {
302 error
= AAC_DEC_UNSUPPORTED_PREDICTION
;
307 if ( CJointStereo_Read(
309 &pAacDecoderChannelInfo
[0]->pComData
->jointStereoData
,
310 GetWindowGroups(&pAacDecoderChannelInfo
[0]->icsInfo
),
311 GetScaleMaxFactorBandsTransmitted(&pAacDecoderChannelInfo
[0]->icsInfo
,
312 &pAacDecoderChannelInfo
[1]->icsInfo
),
315 error
= AAC_DEC_PARSE_ERROR
;
320 pAacDecoderChannelInfo
[ch
]->pDynData
->RawDataInfo
.GlobalGain
= (UCHAR
) FDKreadBits(hBs
,8);
324 error
= CBlock_ReadSectionData( hBs
,
325 pAacDecoderChannelInfo
[ch
],
331 case scale_factor_data
:
332 if (flags
& AC_ER_RVLC
) {
333 /* read RVLC data from bitstream (error sens. cat. 1) */
334 CRvlc_Read(pAacDecoderChannelInfo
[ch
], hBs
);
338 error
= CBlock_ReadScaleFactorData(pAacDecoderChannelInfo
[ch
], hBs
, flags
);
343 if ( CPulseData_Read( hBs
,
344 &pAacDecoderChannelInfo
[ch
]->pDynData
->specificTo
.aac
.PulseData
,
345 pSamplingRateInfo
->ScaleFactorBands_Long
, /* pulse data is only allowed to be present in long blocks! */
346 (void*)&pAacDecoderChannelInfo
[ch
]->icsInfo
,
350 error
= AAC_DEC_DECODE_FRAME_ERROR
;
353 case tns_data_present
:
354 CTns_ReadDataPresentFlag(hBs
, &pAacDecoderChannelInfo
[ch
]->pDynData
->TnsData
);
357 /* tns_data_present is checked inside CTns_Read(). */
358 error
= CTns_Read(hBs
, &pAacDecoderChannelInfo
[ch
]->pDynData
->TnsData
, &pAacDecoderChannelInfo
[ch
]->icsInfo
, flags
);
361 case gain_control_data
:
364 case gain_control_data_present
:
365 if (FDKreadBits(hBs
, 1)) {
366 error
= AAC_DEC_UNSUPPORTED_GAIN_CONTROL_DATA
;
371 if (flags
& AC_ER_RVLC
) {
373 pAacDecoderChannelInfo
[ch
],
374 pAacDecoderStaticChannelInfo
[ch
],
381 if (flags
& AC_ER_HCR
) {
382 CHcr_Read(hBs
, pAacDecoderChannelInfo
[ch
] );
387 error
= CBlock_ReadSpectralData( hBs
,
388 pAacDecoderChannelInfo
[ch
],
391 if (flags
& AC_ELD
) {
392 pAacDecoderChannelInfo
[ch
]->renderMode
= AACDEC_RENDER_ELDFB
;
394 pAacDecoderChannelInfo
[ch
]->renderMode
= AACDEC_RENDER_IMDCT
;
400 case adtscrc_start_reg1
:
401 if (pTpDec
!= NULL
) {
402 crcReg1
= transportDec_CrcStartReg(pTpDec
, 192);
405 case adtscrc_start_reg2
:
406 if (pTpDec
!= NULL
) {
407 crcReg2
= transportDec_CrcStartReg(pTpDec
, 128);
410 case adtscrc_end_reg1
:
412 if (pTpDec
!= NULL
) {
413 transportDec_CrcEndReg(pTpDec
, crcReg1
);
416 case adtscrc_end_reg2
:
417 if (pTpDec
!= NULL
) {
418 transportDec_CrcEndReg(pTpDec
, crcReg2
);
421 case drmcrc_start_reg
:
422 if (pTpDec
!= NULL
) {
423 crcReg1
= transportDec_CrcStartReg(pTpDec
, 0);
429 ch
= (ch
+ 1) % numberOfChannels
;
432 list
= list
->next
[decision_bit
];
437 error
= AAC_DEC_UNSUPPORTED_FORMAT
;
441 if (error
!= AAC_DEC_OK
) {
447 } while (list
->id
[i
] != end_of_sequence
);