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: temporal noise shaping tool
89 ******************************************************************************/
91 #include "aacdec_tns.h"
93 #include "FDK_bitstream.h"
94 #include "channelinfo.h"
101 The function resets the tns data
105 void CTns_Reset(CTnsData
*pTnsData
)
107 /* Note: the following FDKmemclear should not be required. */
108 FDKmemclear(pTnsData
->Filter
, TNS_MAX_WINDOWS
*TNS_MAXIMUM_FILTERS
*sizeof(CFilter
));
109 FDKmemclear(pTnsData
->NumberOfFilters
, TNS_MAX_WINDOWS
*sizeof(UCHAR
));
110 pTnsData
->DataPresent
= 0;
111 pTnsData
->Active
= 0;
114 void CTns_ReadDataPresentFlag(HANDLE_FDK_BITSTREAM bs
, /*!< pointer to bitstream */
115 CTnsData
*pTnsData
) /*!< pointer to aac decoder channel info */
117 pTnsData
->DataPresent
= (UCHAR
) FDKreadBits(bs
,1);
121 \brief Read tns data from bitstream
123 The function reads the elements for tns from
128 AAC_DECODER_ERROR
CTns_Read(HANDLE_FDK_BITSTREAM bs
,
130 const CIcsInfo
*pIcsInfo
,
134 UCHAR length
,coef_res
,coef_compress
;
136 UCHAR wins_per_frame
= GetWindowsPerFrame(pIcsInfo
);
137 UCHAR isLongFlag
= IsLongBlock(pIcsInfo
);
138 AAC_DECODER_ERROR ErrorStatus
= AAC_DEC_OK
;
140 if (!pTnsData
->DataPresent
) {
144 for (window
= 0; window
< wins_per_frame
; window
++)
146 pTnsData
->NumberOfFilters
[window
] = n_filt
= (UCHAR
) FDKreadBits(bs
, isLongFlag
? 2 : 1);
148 if (pTnsData
->NumberOfFilters
[window
] > TNS_MAXIMUM_FILTERS
){
149 pTnsData
->NumberOfFilters
[window
] = n_filt
= TNS_MAXIMUM_FILTERS
;
157 coef_res
= (UCHAR
) FDKreadBits(bs
,1);
159 nextstopband
= GetScaleFactorBandsTotal(pIcsInfo
);
161 for (index
=0; index
< n_filt
; index
++)
163 CFilter
*filter
= &pTnsData
->Filter
[window
][index
];
165 length
= (UCHAR
)FDKreadBits(bs
, isLongFlag
? 6 : 4);
167 if (length
> nextstopband
){
168 length
= nextstopband
;
171 filter
->StartBand
= nextstopband
- length
;
172 filter
->StopBand
= nextstopband
;
173 nextstopband
= filter
->StartBand
;
176 filter
->Order
= order
= (UCHAR
) FDKreadBits(bs
, isLongFlag
? 5 : 3);
179 if (filter
->Order
> TNS_MAXIMUM_ORDER
){
180 filter
->Order
= order
= TNS_MAXIMUM_ORDER
;
188 static const UCHAR sgn_mask
[] = { 0x2, 0x4, 0x8 };
189 static const SCHAR neg_mask
[] = { ~0x3, ~0x7, ~0xF };
191 filter
->Direction
= FDKreadBits(bs
,1) ? -1 : 1;
193 coef_compress
= (UCHAR
) FDKreadBits(bs
,1);
195 filter
->Resolution
= coef_res
+ 3;
197 s_mask
= sgn_mask
[coef_res
+ 1 - coef_compress
];
198 n_mask
= neg_mask
[coef_res
+ 1 - coef_compress
];
200 for (i
=0; i
< order
; i
++)
202 coef
= (UCHAR
) FDKreadBits(bs
,filter
->Resolution
- coef_compress
);
203 filter
->Coeff
[i
] = (coef
& s_mask
) ? (coef
| n_mask
) : coef
;
210 pTnsData
->Active
= 1;
216 static void CTns_Filter (FIXP_DBL
*spec
, int size
, int inc
, FIXP_TCC coeff
[], int order
)
218 // - Simple all-pole filter of order "order" defined by
219 // y(n) = x(n) - a(2)*y(n-1) - ... - a(order+1)*y(n-order)
221 // - The state variables of the filter are initialized to zero every time
223 // - The output data is written over the input data ("in-place operation")
225 // - An input vector of "size" samples is processed and the index increment
226 // to the next data sample is given by "inc"
230 FIXP_DBL maxVal
=FL2FXCONST_DBL(0.0);
233 FDK_ASSERT(order
<= TNS_MAXIMUM_ORDER
);
234 C_ALLOC_SCRATCH_START(state
, FIXP_DBL
, TNS_MAXIMUM_ORDER
);
235 FDKmemclear(state
, order
*sizeof(FIXP_DBL
));
237 for (i
=0; i
<size
; i
++) {
238 maxVal
= fixMax(maxVal
,fixp_abs(spec
[i
]));
241 if ( maxVal
> FL2FXCONST_DBL(0.03125*0.70710678118) )
242 s
= fixMax(CntLeadingZeros(maxVal
)-6,0);
244 s
= fixMax(CntLeadingZeros(maxVal
)-5,0);
250 pSpec
= &spec
[size
- 1];
256 #define FIRST_PART_FLTR \
257 FIXP_DBL x, *pState = state; \
261 x = (pSpec [0]>>1) + fMultDiv2 (*pCoeff++, pState [0]) ; \
263 x = (pSpec [0]<<s) + fMultDiv2 (*pCoeff++, pState [0]) ;
265 #define INNER_FLTR_INLINE \
266 x = fMultAddDiv2 (x, *pCoeff, pState [1]); \
267 pState [0] = pState [1] - (fMultDiv2 (*pCoeff++, x) <<2) ; \
270 #define LAST_PART_FLTR \
275 *pState =(-x) << 1; \
283 for (i
= size
; i
!= 0 ; i
--)
287 for (j
= N
; j
> 0 ; j
--) { INNER_FLTR_INLINE
}
289 INNER_FLTR_INLINE INNER_FLTR_INLINE INNER_FLTR_INLINE INNER_FLTR_INLINE
290 INNER_FLTR_INLINE INNER_FLTR_INLINE INNER_FLTR_INLINE INNER_FLTR_INLINE
295 } else if (order
>4) {
299 for (i
= size
; i
!= 0 ; i
--)
302 for (j
= N
; j
> 0 ; j
--) { INNER_FLTR_INLINE
}
304 INNER_FLTR_INLINE INNER_FLTR_INLINE INNER_FLTR_INLINE INNER_FLTR_INLINE
313 for (i
= size
; i
!= 0 ; i
--)
317 for (j
= N
; j
> 0 ; j
--) { INNER_FLTR_INLINE
}
323 C_ALLOC_SCRATCH_END(state
, FIXP_DBL
, TNS_MAXIMUM_ORDER
);
327 \brief Apply tns to spectral lines
329 The function applies the tns to the spectrum,
334 CTnsData
*RESTRICT pTnsData
, /*!< pointer to aac decoder info */
335 const CIcsInfo
*pIcsInfo
,
336 SPECTRAL_PTR pSpectralCoefficient
,
337 const SamplingRateInfo
*pSamplingRateInfo
,
338 const INT granuleLength
341 int window
,index
,start
,stop
,size
;
344 if (pTnsData
->Active
)
346 C_AALLOC_SCRATCH_START(coeff
, FIXP_TCC
, TNS_MAXIMUM_ORDER
);
348 for (window
=0; window
< GetWindowsPerFrame(pIcsInfo
); window
++)
350 FIXP_DBL
*pSpectrum
= SPEC(pSpectralCoefficient
, window
, granuleLength
);
352 for (index
=0; index
< pTnsData
->NumberOfFilters
[window
]; index
++)
354 CFilter
*RESTRICT filter
= &pTnsData
->Filter
[window
][index
];
356 if (filter
->Order
> 0)
361 pCoeff
= &coeff
[filter
->Order
-1];
362 if (filter
->Resolution
== 3)
365 for (i
=0; i
< filter
->Order
; i
++)
366 *pCoeff
-- = FDKaacDec_tnsCoeff3
[filter
->Coeff
[i
]+4];
371 for (i
=0; i
< filter
->Order
; i
++)
372 *pCoeff
-- = FDKaacDec_tnsCoeff4
[filter
->Coeff
[i
]+8];
375 switch (granuleLength
) {
377 tns_max_bands
= tns_max_bands_tbl_480
[pSamplingRateInfo
->samplingRateIndex
];
380 tns_max_bands
= tns_max_bands_tbl_512
[pSamplingRateInfo
->samplingRateIndex
];
383 tns_max_bands
= GetMaximumTnsBands(pIcsInfo
, pSamplingRateInfo
->samplingRateIndex
);
387 start
= fixMin( fixMin(filter
->StartBand
, tns_max_bands
),
388 GetScaleFactorBandsTransmitted(pIcsInfo
) );
390 start
= GetScaleFactorBandOffsets(pIcsInfo
, pSamplingRateInfo
)[start
];
392 stop
= fixMin( fixMin(filter
->StopBand
, tns_max_bands
),
393 GetScaleFactorBandsTransmitted(pIcsInfo
) );
395 stop
= GetScaleFactorBandOffsets(pIcsInfo
, pSamplingRateInfo
)[stop
];
400 CTns_Filter(&pSpectrum
[start
],
409 C_AALLOC_SCRATCH_END(coeff
, FIXP_TCC
, TNS_MAXIMUM_ORDER
);