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 ----------------------------------------------------------------------------------------------------------- */
90 /* PS dec privat functions */
91 SBR_ERROR
ResetPsDec(HANDLE_PS_DEC h_ps_d
);
92 void ResetPsDeCor (HANDLE_PS_DEC h_ps_d
);
94 /***************************************************************************/
96 \brief huffman decoding by codebook table
98 \return index of huffman codebook table
100 ****************************************************************************/
102 decode_huff_cw (Huffman h
, /*!< pointer to huffman codebook table */
103 HANDLE_FDK_BITSTREAM hBitBuf
, /*!< Handle to Bitbuffer */
104 int *length
) /*!< length of huffman codeword (or NULL) */
111 bit
= FDKreadBits (hBitBuf
, 1);
113 index
= h
[index
][bit
];
118 return( index
+64 ); /* Add offset */
121 /***************************************************************************/
123 \brief helper function - limiting of value to min/max values
125 \return limited value
127 ****************************************************************************/
142 /***************************************************************************/
144 \brief Decodes delta values in-place and updates
145 data buffers according to quantization classes.
147 When delta coded in frequency the first element is deltacode from zero.
148 aIndex buffer is decoded from delta values to actual values.
152 ****************************************************************************/
154 deltaDecodeArray(SCHAR enable
,
155 SCHAR
*aIndex
, /*!< ICC/IID parameters */
156 SCHAR
*aPrevFrameIndex
, /*!< ICC/IID parameters of previous frame */
158 UCHAR nrElements
, /*!< as conveyed in bitstream */
159 /*!< output array size: nrElements*stride */
160 UCHAR stride
, /*!< 1=dflt, 2=half freq. resolution */
168 if (DtDf
== 0) { /* Delta coded in freq */
169 aIndex
[0] = 0 + aIndex
[0];
170 aIndex
[0] = limitMinMax(aIndex
[0],minIdx
,maxIdx
);
171 for (i
= 1; i
< nrElements
; i
++) {
172 aIndex
[i
] = aIndex
[i
-1] + aIndex
[i
];
173 aIndex
[i
] = limitMinMax(aIndex
[i
],minIdx
,maxIdx
);
176 else { /* Delta time */
177 for (i
= 0; i
< nrElements
; i
++) {
178 aIndex
[i
] = aPrevFrameIndex
[i
*stride
] + aIndex
[i
];
179 aIndex
[i
] = limitMinMax(aIndex
[i
],minIdx
,maxIdx
);
183 else { /* No data is sent, set index to zero */
184 for (i
= 0; i
< nrElements
; i
++) {
189 for (i
=nrElements
*stride
-1; i
>0; i
--) {
190 aIndex
[i
] = aIndex
[i
>>1];
195 /***************************************************************************/
197 \brief Mapping of ICC/IID parameters to 20 stereo bands
201 ****************************************************************************/
202 static void map34IndexTo20 (SCHAR
*aIndex
, /*!< decoded ICC/IID parameters */
203 UCHAR noBins
) /*!< number of stereo bands */
205 aIndex
[0] = (2*aIndex
[0]+aIndex
[1])/3;
206 aIndex
[1] = (aIndex
[1]+2*aIndex
[2])/3;
207 aIndex
[2] = (2*aIndex
[3]+aIndex
[4])/3;
208 aIndex
[3] = (aIndex
[4]+2*aIndex
[5])/3;
209 aIndex
[4] = (aIndex
[6]+aIndex
[7])/2;
210 aIndex
[5] = (aIndex
[8]+aIndex
[9])/2;
211 aIndex
[6] = aIndex
[10];
212 aIndex
[7] = aIndex
[11];
213 aIndex
[8] = (aIndex
[12]+aIndex
[13])/2;
214 aIndex
[9] = (aIndex
[14]+aIndex
[15])/2;
215 aIndex
[10] = aIndex
[16];
216 /* For IPD/OPD it stops here */
218 if (noBins
== NO_HI_RES_BINS
)
220 aIndex
[11] = aIndex
[17];
221 aIndex
[12] = aIndex
[18];
222 aIndex
[13] = aIndex
[19];
223 aIndex
[14] = (aIndex
[20]+aIndex
[21])/2;
224 aIndex
[15] = (aIndex
[22]+aIndex
[23])/2;
225 aIndex
[16] = (aIndex
[24]+aIndex
[25])/2;
226 aIndex
[17] = (aIndex
[26]+aIndex
[27])/2;
227 aIndex
[18] = (aIndex
[28]+aIndex
[29]+aIndex
[30]+aIndex
[31])/4;
228 aIndex
[19] = (aIndex
[32]+aIndex
[33])/2;
232 /***************************************************************************/
234 \brief Decodes delta coded IID, ICC, IPD and OPD indices
236 \return PS processing flag. If set to 1
238 ****************************************************************************/
240 DecodePs( struct PS_DEC
*h_ps_d
, /*!< PS handle */
241 const UCHAR frameError
) /*!< Flag telling that frame had errors */
243 MPEG_PS_BS_DATA
*pBsData
;
245 int bPsHeaderValid
, bPsDataAvail
;
247 /* Shortcuts to avoid deferencing and keep the code readable */
248 pBsData
= &h_ps_d
->bsData
[h_ps_d
->processSlot
].mpeg
;
249 bPsHeaderValid
= pBsData
->bPsHeaderValid
;
250 bPsDataAvail
= (h_ps_d
->bPsDataAvail
[h_ps_d
->processSlot
] == ppt_mpeg
) ? 1 : 0;
252 /***************************************************************************************
253 * Decide whether to process or to conceal PS data or not. */
255 if ( ( h_ps_d
->psDecodedPrv
&& !frameError
&& !bPsDataAvail
)
256 || (!h_ps_d
->psDecodedPrv
&& (frameError
|| !bPsDataAvail
|| !bPsHeaderValid
)) ) {
257 /* Don't apply PS processing.
258 * Declare current PS header and bitstream data invalid. */
259 pBsData
->bPsHeaderValid
= 0;
260 h_ps_d
->bPsDataAvail
[h_ps_d
->processSlot
] = ppt_none
;
264 if (frameError
|| !bPsHeaderValid
)
265 { /* no new PS data available (e.g. frame loss) */
266 /* => keep latest data constant (i.e. FIX with noEnv=0) */
270 /***************************************************************************************
271 * Decode bitstream payload or prepare parameter for concealment:
273 for (env
=0; env
<pBsData
->noEnv
; env
++) {
274 SCHAR
*aPrevIidIndex
;
275 SCHAR
*aPrevIccIndex
;
277 UCHAR noIidSteps
= pBsData
->bFineIidQ
?NO_IID_STEPS_FINE
:NO_IID_STEPS
;
280 aPrevIidIndex
= h_ps_d
->specificTo
.mpeg
.aIidPrevFrameIndex
;
281 aPrevIccIndex
= h_ps_d
->specificTo
.mpeg
.aIccPrevFrameIndex
;
284 aPrevIidIndex
= pBsData
->aaIidIndex
[env
-1];
285 aPrevIccIndex
= pBsData
->aaIccIndex
[env
-1];
288 deltaDecodeArray(pBsData
->bEnableIid
,
289 pBsData
->aaIidIndex
[env
],
291 pBsData
->abIidDtFlag
[env
],
292 FDK_sbrDecoder_aNoIidBins
[pBsData
->freqResIid
],
293 (pBsData
->freqResIid
)?1:2,
297 deltaDecodeArray(pBsData
->bEnableIcc
,
298 pBsData
->aaIccIndex
[env
],
300 pBsData
->abIccDtFlag
[env
],
301 FDK_sbrDecoder_aNoIccBins
[pBsData
->freqResIcc
],
302 (pBsData
->freqResIcc
)?1:2,
305 } /* for (env=0; env<pBsData->noEnv; env++) */
307 /* handling of FIX noEnv=0 */
308 if (pBsData
->noEnv
==0) {
309 /* set noEnv=1, keep last parameters or force 0 if not enabled */
312 if (pBsData
->bEnableIid
) {
313 for (gr
= 0; gr
< NO_HI_RES_IID_BINS
; gr
++) {
314 pBsData
->aaIidIndex
[pBsData
->noEnv
-1][gr
] =
315 h_ps_d
->specificTo
.mpeg
.aIidPrevFrameIndex
[gr
];
319 for (gr
= 0; gr
< NO_HI_RES_IID_BINS
; gr
++) {
320 pBsData
->aaIidIndex
[pBsData
->noEnv
-1][gr
] = 0;
324 if (pBsData
->bEnableIcc
) {
325 for (gr
= 0; gr
< NO_HI_RES_ICC_BINS
; gr
++) {
326 pBsData
->aaIccIndex
[pBsData
->noEnv
-1][gr
] =
327 h_ps_d
->specificTo
.mpeg
.aIccPrevFrameIndex
[gr
];
331 for (gr
= 0; gr
< NO_HI_RES_ICC_BINS
; gr
++) {
332 pBsData
->aaIccIndex
[pBsData
->noEnv
-1][gr
] = 0;
337 /* Update previous frame index buffers */
338 for (gr
= 0; gr
< NO_HI_RES_IID_BINS
; gr
++) {
339 h_ps_d
->specificTo
.mpeg
.aIidPrevFrameIndex
[gr
] =
340 pBsData
->aaIidIndex
[pBsData
->noEnv
-1][gr
];
342 for (gr
= 0; gr
< NO_HI_RES_ICC_BINS
; gr
++) {
343 h_ps_d
->specificTo
.mpeg
.aIccPrevFrameIndex
[gr
] =
344 pBsData
->aaIccIndex
[pBsData
->noEnv
-1][gr
];
347 /* PS data from bitstream (if avail) was decoded now */
348 h_ps_d
->bPsDataAvail
[h_ps_d
->processSlot
] = ppt_none
;
350 /* handling of env borders for FIX & VAR */
351 if (pBsData
->bFrameClass
== 0) {
352 /* FIX_BORDERS NoEnv=0,1,2,4 */
353 pBsData
->aEnvStartStop
[0] = 0;
354 for (env
=1; env
<pBsData
->noEnv
; env
++) {
355 pBsData
->aEnvStartStop
[env
] =
356 (env
* h_ps_d
->noSubSamples
) / pBsData
->noEnv
;
358 pBsData
->aEnvStartStop
[pBsData
->noEnv
] = h_ps_d
->noSubSamples
;
359 /* 1024 (32 slots) env borders: 0, 8, 16, 24, 32 */
360 /* 960 (30 slots) env borders: 0, 7, 15, 22, 30 */
362 else { /* if (h_ps_d->bFrameClass == 0) */
363 /* VAR_BORDERS NoEnv=1,2,3,4 */
364 pBsData
->aEnvStartStop
[0] = 0;
366 /* handle case aEnvStartStop[noEnv]<noSubSample for VAR_BORDERS by
367 duplicating last PS parameters and incrementing noEnv */
368 if (pBsData
->aEnvStartStop
[pBsData
->noEnv
] < h_ps_d
->noSubSamples
) {
369 for (gr
= 0; gr
< NO_HI_RES_IID_BINS
; gr
++) {
370 pBsData
->aaIidIndex
[pBsData
->noEnv
][gr
] =
371 pBsData
->aaIidIndex
[pBsData
->noEnv
-1][gr
];
373 for (gr
= 0; gr
< NO_HI_RES_ICC_BINS
; gr
++) {
374 pBsData
->aaIccIndex
[pBsData
->noEnv
][gr
] =
375 pBsData
->aaIccIndex
[pBsData
->noEnv
-1][gr
];
378 pBsData
->aEnvStartStop
[pBsData
->noEnv
] = h_ps_d
->noSubSamples
;
381 /* enforce strictly monotonic increasing borders */
382 for (env
=1; env
<pBsData
->noEnv
; env
++) {
384 thr
= (UCHAR
)h_ps_d
->noSubSamples
- (pBsData
->noEnv
- env
);
385 if (pBsData
->aEnvStartStop
[env
] > thr
) {
386 pBsData
->aEnvStartStop
[env
] = thr
;
389 thr
= pBsData
->aEnvStartStop
[env
-1]+1;
390 if (pBsData
->aEnvStartStop
[env
] < thr
) {
391 pBsData
->aEnvStartStop
[env
] = thr
;
395 } /* if (h_ps_d->bFrameClass == 0) ... else */
397 /* copy data prior to possible 20<->34 in-place mapping */
398 for (env
=0; env
<pBsData
->noEnv
; env
++) {
400 for (i
=0; i
<NO_HI_RES_IID_BINS
; i
++) {
401 h_ps_d
->specificTo
.mpeg
.coef
.aaIidIndexMapped
[env
][i
] = pBsData
->aaIidIndex
[env
][i
];
403 for (i
=0; i
<NO_HI_RES_ICC_BINS
; i
++) {
404 h_ps_d
->specificTo
.mpeg
.coef
.aaIccIndexMapped
[env
][i
] = pBsData
->aaIccIndex
[env
][i
];
409 /* MPEG baseline PS */
410 /* Baseline version of PS always uses the hybrid filter structure with 20 stereo bands. */
411 /* If ICC/IID parameters for 34 stereo bands are decoded they have to be mapped to 20 */
413 /* Additionaly the IPD/OPD parameters won't be used. */
415 for (env
=0; env
<pBsData
->noEnv
; env
++) {
416 if (pBsData
->freqResIid
== 2)
417 map34IndexTo20 (h_ps_d
->specificTo
.mpeg
.coef
.aaIidIndexMapped
[env
], NO_HI_RES_IID_BINS
);
418 if (pBsData
->freqResIcc
== 2)
419 map34IndexTo20 (h_ps_d
->specificTo
.mpeg
.coef
.aaIccIndexMapped
[env
], NO_HI_RES_ICC_BINS
);
421 /* IPD/OPD is disabled in baseline version and thus was removed here */
428 /***************************************************************************/
431 \brief Reads parametric stereo data from bitstream
435 ****************************************************************************/
437 ReadPsData (HANDLE_PS_DEC h_ps_d
, /*!< handle to struct PS_DEC */
438 HANDLE_FDK_BITSTREAM hBitBuf
, /*!< handle to struct BIT_BUF */
439 int nBitsLeft
/*!< max number of bits available */
442 MPEG_PS_BS_DATA
*pBsData
;
447 Huffman CurrentTable
;
453 pBsData
= &h_ps_d
->bsData
[h_ps_d
->bsReadSlot
].mpeg
;
455 if (h_ps_d
->bsReadSlot
!= h_ps_d
->bsLastSlot
) {
456 /* Copy last header data */
457 FDKmemcpy(pBsData
, &h_ps_d
->bsData
[h_ps_d
->bsLastSlot
].mpeg
, sizeof(MPEG_PS_BS_DATA
));
461 startbits
= (INT
) FDKgetValidBits(hBitBuf
);
463 bEnableHeader
= (SCHAR
) FDKreadBits (hBitBuf
, 1);
467 pBsData
->bPsHeaderValid
= 1;
468 pBsData
->bEnableIid
= (UCHAR
) FDKreadBits (hBitBuf
, 1);
469 if (pBsData
->bEnableIid
) {
470 pBsData
->modeIid
= (UCHAR
) FDKreadBits (hBitBuf
, 3);
473 pBsData
->bEnableIcc
= (UCHAR
) FDKreadBits (hBitBuf
, 1);
474 if (pBsData
->bEnableIcc
) {
475 pBsData
->modeIcc
= (UCHAR
) FDKreadBits (hBitBuf
, 3);
478 pBsData
->bEnableExt
= (UCHAR
) FDKreadBits (hBitBuf
, 1);
481 pBsData
->bFrameClass
= (UCHAR
) FDKreadBits (hBitBuf
, 1);
482 if (pBsData
->bFrameClass
== 0) {
483 /* FIX_BORDERS NoEnv=0,1,2,4 */
484 pBsData
->noEnv
= FDK_sbrDecoder_aFixNoEnvDecode
[(UCHAR
) FDKreadBits (hBitBuf
, 2)];
485 /* all additional handling of env borders is now in DecodePs() */
488 /* VAR_BORDERS NoEnv=1,2,3,4 */
489 pBsData
->noEnv
= 1+(UCHAR
) FDKreadBits (hBitBuf
, 2);
490 for (env
=1; env
<pBsData
->noEnv
+1; env
++)
491 pBsData
->aEnvStartStop
[env
] = ((UCHAR
) FDKreadBits (hBitBuf
, 5)) + 1;
492 /* all additional handling of env borders is now in DecodePs() */
495 /* verify that IID & ICC modes (quant grid, freq res) are supported */
496 if ((pBsData
->modeIid
> 5) || (pBsData
->modeIcc
> 5)) {
497 /* no useful PS data could be read from bitstream */
498 h_ps_d
->bPsDataAvail
[h_ps_d
->bsReadSlot
] = ppt_none
;
499 /* discard all remaining bits */
500 nBitsLeft
-= startbits
- FDKgetValidBits(hBitBuf
);
506 FDKreadBits (hBitBuf
, i
);
509 return (startbits
- FDKgetValidBits(hBitBuf
));
512 if (pBsData
->modeIid
> 2){
513 pBsData
->freqResIid
= pBsData
->modeIid
-3;
514 pBsData
->bFineIidQ
= 1;
517 pBsData
->freqResIid
= pBsData
->modeIid
;
518 pBsData
->bFineIidQ
= 0;
521 if (pBsData
->modeIcc
> 2){
522 pBsData
->freqResIcc
= pBsData
->modeIcc
-3;
525 pBsData
->freqResIcc
= pBsData
->modeIcc
;
529 /* Extract IID data */
530 if (pBsData
->bEnableIid
) {
531 for (env
=0; env
<pBsData
->noEnv
; env
++) {
532 dtFlag
= (SCHAR
)FDKreadBits (hBitBuf
, 1);
535 if (pBsData
->bFineIidQ
)
536 CurrentTable
= (Huffman
)&aBookPsIidFineFreqDecode
;
538 CurrentTable
= (Huffman
)&aBookPsIidFreqDecode
;
542 if (pBsData
->bFineIidQ
)
543 CurrentTable
= (Huffman
)&aBookPsIidFineTimeDecode
;
545 CurrentTable
= (Huffman
)&aBookPsIidTimeDecode
;
548 for (gr
= 0; gr
< FDK_sbrDecoder_aNoIidBins
[pBsData
->freqResIid
]; gr
++)
549 pBsData
->aaIidIndex
[env
][gr
] = decode_huff_cw(CurrentTable
,hBitBuf
,NULL
);
550 pBsData
->abIidDtFlag
[env
] = dtFlag
;
554 /* Extract ICC data */
555 if (pBsData
->bEnableIcc
) {
556 for (env
=0; env
<pBsData
->noEnv
; env
++) {
557 dtFlag
= (SCHAR
)FDKreadBits (hBitBuf
, 1);
559 CurrentTable
= (Huffman
)&aBookPsIccFreqDecode
;
561 CurrentTable
= (Huffman
)&aBookPsIccTimeDecode
;
563 for (gr
= 0; gr
< FDK_sbrDecoder_aNoIccBins
[pBsData
->freqResIcc
]; gr
++)
564 pBsData
->aaIccIndex
[env
][gr
] = decode_huff_cw(CurrentTable
,hBitBuf
,NULL
);
565 pBsData
->abIccDtFlag
[env
] = dtFlag
;
569 if (pBsData
->bEnableExt
) {
572 Decoders that support only the baseline version of the PS tool are allowed
573 to ignore the IPD/OPD data, but according header data has to be parsed.
574 ISO/IEC 14496-3 Subpart 8 Annex 4
577 int cnt
= FDKreadBits(hBitBuf
, PS_EXTENSION_SIZE_BITS
);
578 if (cnt
== (1<<PS_EXTENSION_SIZE_BITS
)-1) {
579 cnt
+= FDKreadBits(hBitBuf
, PS_EXTENSION_ESC_COUNT_BITS
);
582 FDKreadBits(hBitBuf
, 8);
586 /* new PS data was read from bitstream */
587 h_ps_d
->bPsDataAvail
[h_ps_d
->bsReadSlot
] = ppt_mpeg
;
591 return (startbits
- FDKgetValidBits(hBitBuf
));