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 ----------------------------------------------------------------------------------------------------------- */
86 \brief Frequency scale calculation
89 #include "sbrdec_freq_sca.h"
91 #include "transcendent.h"
95 #include "genericStds.h" /* need log() for debug-code only */
98 #define MAX_SECOND_REGION 50
101 static int numberOfBands(FIXP_SGL bpo_div16
, int start
, int stop
, int warpFlag
);
102 static void CalcBands(UCHAR
* diff
, UCHAR start
, UCHAR stop
, UCHAR num_bands
);
103 static SBR_ERROR
modifyBands(UCHAR max_band
, UCHAR
* diff
, UCHAR length
);
104 static void cumSum(UCHAR start_value
, UCHAR
* diff
, UCHAR length
, UCHAR
*start_adress
);
109 \brief Retrieve QMF-band where the SBR range starts
111 Convert startFreq which was read from the bitstream into a
114 \return Number of start band
117 getStartBand(UINT fs
, /*!< Output sampling frequency */
118 UCHAR startFreq
, /*!< Index to table of possible start bands */
119 UINT headerDataFlags
) /*!< Info to SBR mode */
129 band
= FDK_sbrDecoder_sbr_start_freq_88
[startFreq
];
132 band
= FDK_sbrDecoder_sbr_start_freq_64
[startFreq
];
135 band
= FDK_sbrDecoder_sbr_start_freq_48
[startFreq
];
138 band
= FDK_sbrDecoder_sbr_start_freq_44
[startFreq
];
141 band
= FDK_sbrDecoder_sbr_start_freq_32
[startFreq
];
144 band
= FDK_sbrDecoder_sbr_start_freq_24
[startFreq
];
147 band
= FDK_sbrDecoder_sbr_start_freq_22
[startFreq
];
150 band
= FDK_sbrDecoder_sbr_start_freq_16
[startFreq
];
161 \brief Retrieve QMF-band where the SBR range starts
163 Convert startFreq which was read from the bitstream into a
166 \return Number of start band
169 getStopBand(UINT fs
, /*!< Output sampling frequency */
170 UCHAR stopFreq
, /*!< Index to table of possible start bands */
171 UINT headerDataFlags
, /*!< Info to SBR mode */
172 UCHAR k0
) /*!< Start freq index */
178 UCHAR diff_tot
[MAX_OCTAVE
+ MAX_SECOND_REGION
];
179 UCHAR
*diff0
= diff_tot
;
180 UCHAR
*diff1
= diff_tot
+MAX_OCTAVE
;
183 stopMin
= (((2*6000*2*(64)) / fs
) + 1) >> 1;
187 stopMin
= (((2*8000*2*(64)) / fs
) + 1) >> 1;
190 stopMin
= (((2*10000*2*(64)) / fs
) + 1) >> 1;
195 Choose a stop band between k1 and 64 depending on stopFreq (0..13),
196 based on a logarithmic scale.
197 The vectors diff0 and diff1 are used temporarily here.
199 CalcBands( diff0
, stopMin
, 64, 13);
200 shellsort( diff0
, 13);
201 cumSum(stopMin
, diff0
, 13, diff1
);
202 k2
= diff1
[stopFreq
];
204 else if (stopFreq
==14)
209 /* Limit to Nyquist */
215 /* 1 <= difference <= 48; 1 <= fs <= 96000 */
216 if ( ((k2
- k0
) > MAX_FREQ_COEFFS
) || (k2
<= k0
) ) {
220 if (headerDataFlags
& (SBRDEC_SYNTAX_USAC
|SBRDEC_SYNTAX_RSVD50
)) {
221 /* 1 <= difference <= 35; 42000 <= fs <= 96000 */
222 if ( (fs
>= 42000) && ( (k2
- k0
) > MAX_FREQ_COEFFS_FS44100
) ) {
225 /* 1 <= difference <= 32; 46009 <= fs <= 96000 */
226 if ( (fs
>= 46009) && ( (k2
- k0
) > MAX_FREQ_COEFFS_FS48000
) ) {
231 /* 1 <= difference <= 35; fs == 44100 */
232 if ( (fs
== 44100) && ( (k2
- k0
) > MAX_FREQ_COEFFS_FS44100
) ) {
235 /* 1 <= difference <= 32; 48000 <= fs <= 96000 */
236 if ( (fs
>= 48000) && ( (k2
- k0
) > MAX_FREQ_COEFFS_FS48000
) ) {
246 \brief Generates master frequency tables
248 Frequency tables are calculated according to the selected domain
249 (linear/logarithmic) and granularity.
250 IEC 14496-3 4.6.18.3.2.1
252 \return errorCode, 0 if successful
255 sbrdecUpdateFreqScale(UCHAR
* v_k_master
, /*!< Master table to be created */
256 UCHAR
*numMaster
, /*!< Number of entries in master table */
257 UINT fs
, /*!< SBR working sampling rate */
258 HANDLE_SBR_HEADER_DATA hHeaderData
, /*!< Control data from bitstream */
261 FIXP_SGL bpo_div16
; /* bands_per_octave divided by 16 */
264 /* Internal variables */
266 UCHAR num_bands0
= 0;
267 UCHAR num_bands1
= 0;
268 UCHAR diff_tot
[MAX_OCTAVE
+ MAX_SECOND_REGION
];
269 UCHAR
*diff0
= diff_tot
;
270 UCHAR
*diff1
= diff_tot
+MAX_OCTAVE
;
278 k0
= getStartBand(fs
, hHeaderData
->bs_data
.startFreq
, flags
);
280 return SBRDEC_UNSUPPORTED_CONFIG
;
286 k2
= getStopBand(fs
, hHeaderData
->bs_data
.stopFreq
, flags
, k0
);
288 return SBRDEC_UNSUPPORTED_CONFIG
;
291 if(hHeaderData
->bs_data
.freqScale
>0) { /* Bark */
294 if(hHeaderData
->bs_data
.freqScale
==1) {
295 bpo_div16
= FL2FXCONST_SGL(12.0f
/16.0f
);
297 else if(hHeaderData
->bs_data
.freqScale
==2) {
298 bpo_div16
= FL2FXCONST_SGL(10.0f
/16.0f
);
301 bpo_div16
= FL2FXCONST_SGL(8.0f
/16.0f
);
305 if( 1000 * k2
> 2245 * k0
) { /* Two or more regions */
308 num_bands0
= numberOfBands(bpo_div16
, k0
, k1
, 0);
309 num_bands1
= numberOfBands(bpo_div16
, k1
, k2
, hHeaderData
->bs_data
.alterScale
);
310 if ( num_bands0
< 1) {
311 return SBRDEC_UNSUPPORTED_CONFIG
;
313 if ( num_bands1
< 1 ) {
314 return SBRDEC_UNSUPPORTED_CONFIG
;
317 CalcBands(diff0
, k0
, k1
, num_bands0
);
318 shellsort( diff0
, num_bands0
);
322 return SBRDEC_UNSUPPORTED_CONFIG
;
325 cumSum(k0
, diff0
, num_bands0
, v_k_master
);
327 CalcBands(diff1
, k1
, k2
, num_bands1
);
328 shellsort( diff1
, num_bands1
);
329 if(diff0
[num_bands0
-1] > diff1
[0]) {
332 err
= modifyBands(diff0
[num_bands0
-1],diff1
, num_bands1
);
334 return SBRDEC_UNSUPPORTED_CONFIG
;
338 cumSum(k1
, diff1
, num_bands1
, &v_k_master
[num_bands0
]);
339 *numMaster
= num_bands0
+ num_bands1
; /* Output nr of bands */
342 else { /* Only one region */
345 num_bands0
= numberOfBands(bpo_div16
, k0
, k1
, 0);
346 if ( num_bands0
< 1) {
347 return SBRDEC_UNSUPPORTED_CONFIG
;
349 CalcBands(diff0
, k0
, k1
, num_bands0
);
350 shellsort(diff0
, num_bands0
);
354 return SBRDEC_UNSUPPORTED_CONFIG
;
357 cumSum(k0
, diff0
, num_bands0
, v_k_master
);
358 *numMaster
= num_bands0
; /* Output nr of bands */
362 else { /* Linear mode */
363 if (hHeaderData
->bs_data
.alterScale
==0) {
365 /* FLOOR to get to few number of bands (next lower even number) */
366 num_bands0
= (k2
- k0
) & 254;
369 num_bands0
= ( ((k2
- k0
) >> 1) + 1 ) & 254; /* ROUND to the closest fit */
372 if (num_bands0
< 1) {
373 return SBRDEC_UNSUPPORTED_CONFIG
;
374 /* We must return already here because 'i' can become negative below. */
377 k2_achived
= k0
+ num_bands0
*dk
;
378 k2_diff
= k2
- k2_achived
;
380 for(i
=0;i
<num_bands0
;i
++)
383 /* If linear scale wasn't achieved */
384 /* and we got too wide SBR area */
390 /* If linear scale wasn't achieved */
391 /* and we got too small SBR area */
397 /* Adjust diff vector to get sepc. SBR range */
398 while (k2_diff
!= 0) {
399 diff_tot
[i
] = diff_tot
[i
] - incr
;
401 k2_diff
= k2_diff
+ incr
;
404 cumSum(k0
, diff_tot
, num_bands0
, v_k_master
);/* cumsum */
405 *numMaster
= num_bands0
; /* Output nr of bands */
408 if (*numMaster
< 1) {
409 return SBRDEC_UNSUPPORTED_CONFIG
;
414 Print out the calculated table
422 \brief Calculate frequency ratio of one SBR band
424 All SBR bands should span a constant frequency range in the logarithmic
425 domain. This function calculates the ratio of any SBR band's upper and lower
428 \return num_band-th root of k_start/k_stop
430 static FIXP_SGL
calcFactorPerBand(int k_start
, int k_stop
, int num_bands
)
432 /* Scaled bandfactor and step 1 bit right to avoid overflow
433 * use double data type */
434 FIXP_DBL bandfactor
= FL2FXCONST_DBL(0.25f
); /* Start value */
435 FIXP_DBL step
= FL2FXCONST_DBL(0.125f
); /* Initial increment for factor */
439 /* Because saturation can't be done in INT IIS,
440 * changed start and stop data type from FIXP_SGL to FIXP_DBL */
441 FIXP_DBL start
= k_start
<< (DFRACT_BITS
-8);
442 FIXP_DBL stop
= k_stop
<< (DFRACT_BITS
-8);
448 while ( step
> FL2FXCONST_DBL(0.0f
)) {
452 /* Calculate temp^num_bands: */
453 for (j
=0; j
<num_bands
; j
++)
454 //temp = fMult(temp,bandfactor);
455 temp
= fMultDiv2(temp
,bandfactor
)<<2;
457 if (temp
<start
) { /* Factor too strong, make it weaker */
459 /* Halfen step. Right shift is not done as fract because otherwise the
460 lowest bit cannot be cleared due to rounding */
461 step
= (FIXP_DBL
)((LONG
)step
>> 1);
463 bandfactor
= bandfactor
+ step
;
465 else { /* Factor is too weak: make it stronger */
467 step
= (FIXP_DBL
)((LONG
)step
>> 1);
469 bandfactor
= bandfactor
- step
;
473 step
= FL2FXCONST_DBL(0.0f
);
476 return FX_DBL2FX_SGL(bandfactor
<<1);
481 \brief Calculate number of SBR bands between start and stop band
483 Given the number of bands per octave, this function calculates how many
484 bands fit in the given frequency range.
485 When the warpFlag is set, the 'band density' is decreased by a factor
488 \return number of bands
491 numberOfBands(FIXP_SGL bpo_div16
, /*!< Input: number of bands per octave divided by 16 */
492 int start
, /*!< First QMF band of SBR frequency range */
493 int stop
, /*!< Last QMF band of SBR frequency range + 1 */
494 int warpFlag
) /*!< Stretching flag */
496 FIXP_SGL num_bands_div128
;
499 num_bands_div128
= FX_DBL2FX_SGL(fMult(FDK_getNumOctavesDiv8(start
,stop
),bpo_div16
));
502 /* Apply the warp factor of 1.3 to get wider bands. We use a value
503 of 32768/25200 instead of the exact value to avoid critical cases
506 num_bands_div128
= FX_DBL2FX_SGL(fMult(num_bands_div128
, FL2FXCONST_SGL(25200.0/32768.0)));
509 /* add scaled 1 for rounding to even numbers: */
510 num_bands_div128
= num_bands_div128
+ FL2FXCONST_SGL( 1.0f
/128.0f
);
511 /* scale back to right aligned integer and double the value: */
512 num_bands
= 2 * ((LONG
)num_bands_div128
>> (FRACT_BITS
- 7));
519 \brief Calculate width of SBR bands
521 Given the desired number of bands within the SBR frequency range,
522 this function calculates the width of each SBR band in QMF channels.
523 The bands get wider from start to stop (bark scale).
526 CalcBands(UCHAR
* diff
, /*!< Vector of widths to be calculated */
527 UCHAR start
, /*!< Lower end of subband range */
528 UCHAR stop
, /*!< Upper end of subband range */
529 UCHAR num_bands
) /*!< Desired number of bands */
534 FIXP_SGL exact
, temp
;
535 FIXP_SGL bandfactor
= calcFactorPerBand(start
, stop
, num_bands
);
537 previous
= stop
; /* Start with highest QMF channel */
538 exact
= (FIXP_SGL
)(stop
<< (FRACT_BITS
-8)); /* Shift left to gain some accuracy */
540 for(i
=num_bands
-1; i
>=0; i
--) {
541 /* Calculate border of next lower sbr band */
542 exact
= FX_DBL2FX_SGL(fMult(exact
,bandfactor
));
544 /* Add scaled 0.5 for rounding:
545 We use a value 128/256 instead of 0.5 to avoid some critical cases of rounding. */
546 temp
= exact
+ FL2FXCONST_SGL(128.0/32768.0);
548 /* scale back to right alinged integer: */
549 current
= (LONG
)temp
>> (FRACT_BITS
-8);
551 /* Save width of band i */
552 diff
[i
] = previous
- current
;
559 \brief Calculate cumulated sum vector from delta vector
562 cumSum(UCHAR start_value
, UCHAR
* diff
, UCHAR length
, UCHAR
*start_adress
)
565 start_adress
[0]=start_value
;
566 for(i
=1; i
<=length
; i
++)
567 start_adress
[i
] = start_adress
[i
-1] + diff
[i
-1];
572 \brief Adapt width of frequency bands in the second region
574 If SBR spans more than 2 octaves, the upper part of a bark-frequency-scale
575 is calculated separately. This function tries to avoid that the second region
576 starts with a band smaller than the highest band of the first region.
579 modifyBands(UCHAR max_band_previous
, UCHAR
* diff
, UCHAR length
)
581 int change
= max_band_previous
- diff
[0];
583 /* Limit the change so that the last band cannot get narrower than the first one */
584 if ( change
> (diff
[length
-1]-diff
[0])>>1 )
585 change
= (diff
[length
-1]-diff
[0])>>1;
588 diff
[length
-1] -= change
;
589 shellsort(diff
, length
);
596 \brief Update high resolution frequency band table
599 sbrdecUpdateHiRes(UCHAR
* h_hires
,
607 *num_hires
= num_bands
-xover_band
;
609 for(i
=xover_band
; i
<=num_bands
; i
++) {
610 h_hires
[i
-xover_band
] = v_k_master
[i
];
616 \brief Build low resolution table out of high resolution table
619 sbrdecUpdateLoRes(UCHAR
* h_lores
,
626 if( (num_hires
& 1) == 0) {
627 /* If even number of hires bands */
628 *num_lores
= num_hires
>> 1;
629 /* Use every second lores=hires[0,2,4...] */
630 for(i
=0; i
<=*num_lores
; i
++)
631 h_lores
[i
] = h_hires
[i
*2];
634 /* Odd number of hires, which means xover is odd */
635 *num_lores
= (num_hires
+1) >> 1;
636 /* Use lores=hires[0,1,3,5 ...] */
637 h_lores
[0] = h_hires
[0];
638 for(i
=1; i
<=*num_lores
; i
++) {
639 h_lores
[i
] = h_hires
[i
*2-1];
646 \brief Derive a low-resolution frequency-table from the master frequency table
649 sbrdecDownSampleLoRes(UCHAR
*v_result
,
651 UCHAR
*freqBandTableRef
,
656 int org_length
,result_length
;
657 int v_index
[MAX_FREQ_COEFFS
>>1];
660 org_length
= num_Ref
;
661 result_length
= num_result
;
663 v_index
[0] = 0; /* Always use left border */
665 while(org_length
> 0) {
666 /* Create downsample vector */
668 step
= org_length
/ result_length
;
669 org_length
= org_length
- step
;
671 v_index
[i
] = v_index
[i
-1] + step
;
675 /* Use downsample vector to index LoResolution vector */
676 v_result
[j
]=freqBandTableRef
[v_index
[j
]];
683 \brief Sorting routine
685 void shellsort(UCHAR
*in
, UCHAR n
)
697 for (i
= inc
; i
< n
; i
++) {
700 while ((w
=in
[j
-inc
]) > v
) {
715 \brief Reset frequency band tables
716 \return errorCode, 0 if successful
719 resetFreqBandTables(HANDLE_SBR_HEADER_DATA hHeaderData
, const UINT flags
)
721 SBR_ERROR err
= SBRDEC_OK
;
724 UCHAR nBandsLo
, nBandsHi
;
725 HANDLE_FREQ_BAND_DATA hFreq
= &hHeaderData
->freqBandData
;
727 /* Calculate master frequency function */
728 err
= sbrdecUpdateFreqScale(hFreq
->v_k_master
,
730 hHeaderData
->sbrProcSmplRate
,
734 if ( err
|| (hHeaderData
->bs_info
.xover_band
> hFreq
->numMaster
) ) {
735 return SBRDEC_UNSUPPORTED_CONFIG
;
738 /* Derive Hiresolution from master frequency function */
739 sbrdecUpdateHiRes(hFreq
->freqBandTable
[1], &nBandsHi
, hFreq
->v_k_master
, hFreq
->numMaster
, hHeaderData
->bs_info
.xover_band
);
740 /* Derive Loresolution from Hiresolution */
741 sbrdecUpdateLoRes(hFreq
->freqBandTable
[0], &nBandsLo
, hFreq
->freqBandTable
[1], nBandsHi
);
744 hFreq
->nSfb
[0] = nBandsLo
;
745 hFreq
->nSfb
[1] = nBandsHi
;
747 /* Check index to freqBandTable[0] */
748 if ( !(nBandsLo
> 0) || (nBandsLo
> (MAX_FREQ_COEFFS
>>1)) ) {
749 return SBRDEC_UNSUPPORTED_CONFIG
;
752 lsb
= hFreq
->freqBandTable
[0][0];
753 usb
= hFreq
->freqBandTable
[0][nBandsLo
];
755 /* Additional check for lsb */
756 if ( (lsb
> (32)) || (lsb
>= usb
) ) {
757 return SBRDEC_UNSUPPORTED_CONFIG
;
761 /* Calculate number of noise bands */
763 k2
= hFreq
->freqBandTable
[1][nBandsHi
];
764 kx
= hFreq
->freqBandTable
[1][0];
766 if (hHeaderData
->bs_data
.noise_bands
== 0)
770 else /* Calculate no of noise bands 1,2 or 3 bands/octave */
772 /* Fetch number of octaves divided by 32 */
773 intTemp
= (LONG
)FDK_getNumOctavesDiv8(kx
,k2
) >> 2;
775 /* Integer-Multiplication with number of bands: */
776 intTemp
= intTemp
* hHeaderData
->bs_data
.noise_bands
;
778 /* Add scaled 0.5 for rounding: */
779 intTemp
= intTemp
+ (LONG
)FL2FXCONST_SGL(0.5f
/32.0f
);
781 /* Convert to right-aligned integer: */
782 intTemp
= intTemp
>> (FRACT_BITS
- 1 /*sign*/ - 5 /* rescale */);
784 /* Compare with float calculation */
785 FDK_ASSERT( intTemp
== (int)((hHeaderData
->bs_data
.noise_bands
* FDKlog( (float)k2
/kx
) / (float)(FDKlog(2.0)))+0.5) );
790 hFreq
->nNfb
= intTemp
;
793 hFreq
->nInvfBands
= hFreq
->nNfb
;
795 if( hFreq
->nNfb
> MAX_NOISE_COEFFS
) {
796 return SBRDEC_UNSUPPORTED_CONFIG
;
799 /* Get noise bands */
800 sbrdecDownSampleLoRes(hFreq
->freqBandTableNoise
,
802 hFreq
->freqBandTable
[0],
808 hFreq
->lowSubband
= lsb
;
809 hFreq
->highSubband
= usb
;