1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5 � Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6 All rights reserved.
7
8 1. INTRODUCTION
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.
12
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.
17
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.
24
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.
28
29 2. COPYRIGHT LICENSE
30
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:
33
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.
36
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.
41
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44
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.
47
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."
52
53 3. NO PATENT LICENSE
54
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.
58
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61
62 4. DISCLAIMER
63
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.
72
73 5. CONTACT INFORMATION
74
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83
84 /***************************** MPEG-4 AAC Decoder **************************
85
86 Author(s): Josef Hoepfl
87 Description: temporal noise shaping tool
88
89 ******************************************************************************/
90
91 #include "aacdec_tns.h"
92 #include "aac_rom.h"
93 #include "FDK_bitstream.h"
94 #include "channelinfo.h"
95
96
97
98 /*!
99 \brief Reset tns data
100
101 The function resets the tns data
102
103 \return none
104 */
CTns_Reset(CTnsData * pTnsData)105 void CTns_Reset(CTnsData *pTnsData)
106 {
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;
112 }
113
CTns_ReadDataPresentFlag(HANDLE_FDK_BITSTREAM bs,CTnsData * pTnsData)114 void CTns_ReadDataPresentFlag(HANDLE_FDK_BITSTREAM bs, /*!< pointer to bitstream */
115 CTnsData *pTnsData) /*!< pointer to aac decoder channel info */
116 {
117 pTnsData->DataPresent = (UCHAR) FDKreadBits(bs,1);
118 }
119
120 /*!
121 \brief Read tns data from bitstream
122
123 The function reads the elements for tns from
124 the bitstream.
125
126 \return none
127 */
CTns_Read(HANDLE_FDK_BITSTREAM bs,CTnsData * pTnsData,const CIcsInfo * pIcsInfo,const UINT flags)128 AAC_DECODER_ERROR CTns_Read(HANDLE_FDK_BITSTREAM bs,
129 CTnsData *pTnsData,
130 const CIcsInfo *pIcsInfo,
131 const UINT flags)
132 {
133 UCHAR n_filt,order;
134 UCHAR length,coef_res,coef_compress;
135 UCHAR window;
136 UCHAR wins_per_frame = GetWindowsPerFrame(pIcsInfo);
137 UCHAR isLongFlag = IsLongBlock(pIcsInfo);
138 AAC_DECODER_ERROR ErrorStatus = AAC_DEC_OK;
139
140 if (!pTnsData->DataPresent) {
141 return ErrorStatus;
142 }
143
144 for (window = 0; window < wins_per_frame; window++)
145 {
146 pTnsData->NumberOfFilters[window] = n_filt = (UCHAR) FDKreadBits(bs, isLongFlag ? 2 : 1);
147
148 if (pTnsData->NumberOfFilters[window] > TNS_MAXIMUM_FILTERS){
149 pTnsData->NumberOfFilters[window] = n_filt = TNS_MAXIMUM_FILTERS;
150 }
151
152 if (n_filt)
153 {
154 int index;
155 UCHAR nextstopband;
156
157 coef_res = (UCHAR) FDKreadBits(bs,1);
158
159 nextstopband = GetScaleFactorBandsTotal(pIcsInfo);
160
161 for (index=0; index < n_filt; index++)
162 {
163 CFilter *filter = &pTnsData->Filter[window][index];
164
165 length = (UCHAR)FDKreadBits(bs, isLongFlag ? 6 : 4);
166
167 if (length > nextstopband){
168 length = nextstopband;
169 }
170
171 filter->StartBand = nextstopband - length;
172 filter->StopBand = nextstopband;
173 nextstopband = filter->StartBand;
174
175 {
176 filter->Order = order = (UCHAR) FDKreadBits(bs, isLongFlag ? 5 : 3);
177 }
178
179 if (filter->Order > TNS_MAXIMUM_ORDER){
180 filter->Order = order = TNS_MAXIMUM_ORDER;
181 }
182
183 if (order)
184 {
185 UCHAR coef,s_mask;
186 UCHAR i;
187 SCHAR n_mask;
188 static const UCHAR sgn_mask[] = { 0x2, 0x4, 0x8 };
189 static const SCHAR neg_mask[] = { ~0x3, ~0x7, ~0xF };
190
191 filter->Direction = FDKreadBits(bs,1) ? -1 : 1;
192
193 coef_compress = (UCHAR) FDKreadBits(bs,1);
194
195 filter->Resolution = coef_res + 3;
196
197 s_mask = sgn_mask[coef_res + 1 - coef_compress];
198 n_mask = neg_mask[coef_res + 1 - coef_compress];
199
200 for (i=0; i < order; i++)
201 {
202 coef = (UCHAR) FDKreadBits(bs,filter->Resolution - coef_compress);
203 filter->Coeff[i] = (coef & s_mask) ? (coef | n_mask) : coef;
204 }
205 }
206 }
207 }
208 }
209
210 pTnsData->Active = 1;
211
212 return ErrorStatus;
213 }
214
215
CTns_Filter(FIXP_DBL * spec,int size,int inc,FIXP_TCC coeff[],int order)216 static void CTns_Filter (FIXP_DBL *spec, int size, int inc, FIXP_TCC coeff [], int order)
217 {
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)
220 //
221 // - The state variables of the filter are initialized to zero every time
222 //
223 // - The output data is written over the input data ("in-place operation")
224 //
225 // - An input vector of "size" samples is processed and the index increment
226 // to the next data sample is given by "inc"
227
228 int i,j,N;
229 FIXP_DBL *pSpec;
230 FIXP_DBL maxVal=FL2FXCONST_DBL(0.0);
231 INT s;
232
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));
236
237 for (i=0; i<size; i++) {
238 maxVal = fixMax(maxVal,fixp_abs(spec[i]));
239 }
240
241 if ( maxVal > FL2FXCONST_DBL(0.03125*0.70710678118) )
242 s = fixMax(CntLeadingZeros(maxVal)-6,0);
243 else
244 s = fixMax(CntLeadingZeros(maxVal)-5,0);
245
246 s = fixMin(s,2);
247 s = s-1;
248
249 if (inc == -1)
250 pSpec = &spec[size - 1];
251 else
252 pSpec = &spec[0];
253
254 FIXP_TCC *pCoeff;
255
256 #define FIRST_PART_FLTR \
257 FIXP_DBL x, *pState = state; \
258 pCoeff = coeff; \
259 \
260 if (s < 0) \
261 x = (pSpec [0]>>1) + fMultDiv2 (*pCoeff++, pState [0]) ; \
262 else \
263 x = (pSpec [0]<<s) + fMultDiv2 (*pCoeff++, pState [0]) ;
264
265 #define INNER_FLTR_INLINE \
266 x = fMultAddDiv2 (x, *pCoeff, pState [1]); \
267 pState [0] = pState [1] - (fMultDiv2 (*pCoeff++, x) <<2) ; \
268 pState++;
269
270 #define LAST_PART_FLTR \
271 if (s < 0) \
272 *pSpec = x << 1; \
273 else \
274 *pSpec = x >> s; \
275 *pState =(-x) << 1; \
276 pSpec += inc ;
277
278
279 if (order>8)
280 {
281 N = (order-1)&7;
282
283 for (i = size ; i != 0 ; i--)
284 {
285 FIRST_PART_FLTR
286
287 for (j = N; j > 0 ; j--) { INNER_FLTR_INLINE }
288
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
291
292 LAST_PART_FLTR
293 }
294
295 } else if (order>4) {
296
297 N = (order-1)&3;
298
299 for (i = size ; i != 0 ; i--)
300 {
301 FIRST_PART_FLTR
302 for (j = N; j > 0 ; j--) { INNER_FLTR_INLINE }
303
304 INNER_FLTR_INLINE INNER_FLTR_INLINE INNER_FLTR_INLINE INNER_FLTR_INLINE
305
306 LAST_PART_FLTR
307 }
308
309 } else {
310
311 N = order-1;
312
313 for (i = size ; i != 0 ; i--)
314 {
315 FIRST_PART_FLTR
316
317 for (j = N; j > 0 ; j--) { INNER_FLTR_INLINE }
318
319 LAST_PART_FLTR
320 }
321 }
322
323 C_ALLOC_SCRATCH_END(state, FIXP_DBL, TNS_MAXIMUM_ORDER);
324 }
325
326 /*!
327 \brief Apply tns to spectral lines
328
329 The function applies the tns to the spectrum,
330
331 \return none
332 */
CTns_Apply(CTnsData * RESTRICT pTnsData,const CIcsInfo * pIcsInfo,SPECTRAL_PTR pSpectralCoefficient,const SamplingRateInfo * pSamplingRateInfo,const INT granuleLength)333 void CTns_Apply (
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
339 )
340 {
341 int window,index,start,stop,size;
342
343
344 if (pTnsData->Active)
345 {
346 C_AALLOC_SCRATCH_START(coeff, FIXP_TCC, TNS_MAXIMUM_ORDER);
347
348 for (window=0; window < GetWindowsPerFrame(pIcsInfo); window++)
349 {
350 FIXP_DBL *pSpectrum = SPEC(pSpectralCoefficient, window, granuleLength);
351
352 for (index=0; index < pTnsData->NumberOfFilters[window]; index++)
353 {
354 CFilter *RESTRICT filter = &pTnsData->Filter[window][index];
355
356 if (filter->Order > 0)
357 {
358 FIXP_TCC *pCoeff;
359 int tns_max_bands;
360
361 pCoeff = &coeff[filter->Order-1];
362 if (filter->Resolution == 3)
363 {
364 int i;
365 for (i=0; i < filter->Order; i++)
366 *pCoeff-- = FDKaacDec_tnsCoeff3[filter->Coeff[i]+4];
367 }
368 else
369 {
370 int i;
371 for (i=0; i < filter->Order; i++)
372 *pCoeff-- = FDKaacDec_tnsCoeff4[filter->Coeff[i]+8];
373 }
374
375 switch (granuleLength) {
376 case 480:
377 tns_max_bands = tns_max_bands_tbl_480[pSamplingRateInfo->samplingRateIndex];
378 break;
379 case 512:
380 tns_max_bands = tns_max_bands_tbl_512[pSamplingRateInfo->samplingRateIndex];
381 break;
382 default:
383 tns_max_bands = GetMaximumTnsBands(pIcsInfo, pSamplingRateInfo->samplingRateIndex);
384 break;
385 }
386
387 start = fixMin( fixMin(filter->StartBand, tns_max_bands),
388 GetScaleFactorBandsTransmitted(pIcsInfo) );
389
390 start = GetScaleFactorBandOffsets(pIcsInfo, pSamplingRateInfo)[start];
391
392 stop = fixMin( fixMin(filter->StopBand, tns_max_bands),
393 GetScaleFactorBandsTransmitted(pIcsInfo) );
394
395 stop = GetScaleFactorBandOffsets(pIcsInfo, pSamplingRateInfo)[stop];
396
397 size = stop - start;
398
399 if (size > 0) {
400 CTns_Filter(&pSpectrum[start],
401 size,
402 filter->Direction,
403 coeff,
404 filter->Order );
405 }
406 }
407 }
408 }
409 C_AALLOC_SCRATCH_END(coeff, FIXP_TCC, TNS_MAXIMUM_ORDER);
410 }
411
412 }
413