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 ----------------------------------------------------------------------------------------------------------- */
88 #include "genericStds.h"
90 /* smoothFilter[4] = {0.05857864376269f, 0.2f, 0.34142135623731f, 0.4f}; */
91 static const FIXP_DBL smoothFilter
[4] = { 0x077f813d, 0x19999995, 0x2bb3b1f5, 0x33333335 };
93 /* static const INT smoothFilterLength = 4; */
95 static const FIXP_DBL QuantOffset
= (INT
)0xfc000000; /* ld64(0.25) */
98 #define min(a,b) ( a < b ? a:b)
102 #define max(a,b) ( a > b ? a:b)
105 #define NOISE_FLOOR_OFFSET_SCALING (4)
109 /**************************************************************************/
111 \brief The function applies smoothing to the noise levels.
118 /**************************************************************************/
120 smoothingOfNoiseLevels(FIXP_DBL
*NoiseLevels
, /*!< pointer to noise-floor levels.*/
121 INT nEnvelopes
, /*!< Number of noise floor envelopes.*/
122 INT noNoiseBands
, /*!< Number of noise bands for every noise floor envelope. */
123 FIXP_DBL prevNoiseLevels
[NF_SMOOTHING_LENGTH
][MAX_NUM_NOISE_VALUES
],/*!< Previous noise floor envelopes. */
124 const FIXP_DBL
*smoothFilter
, /*!< filter used for smoothing the noise floor levels. */
125 INT transientFlag
) /*!< flag indicating if a transient is present*/
131 for(env
= 0; env
< nEnvelopes
; env
++){
133 for (i
= 0; i
< NF_SMOOTHING_LENGTH
; i
++){
134 FDKmemcpy(prevNoiseLevels
[i
],NoiseLevels
+env
*noNoiseBands
,noNoiseBands
*sizeof(FIXP_DBL
));
138 for (i
= 1; i
< NF_SMOOTHING_LENGTH
; i
++){
139 FDKmemcpy(prevNoiseLevels
[i
- 1],prevNoiseLevels
[i
],noNoiseBands
*sizeof(FIXP_DBL
));
141 FDKmemcpy(prevNoiseLevels
[NF_SMOOTHING_LENGTH
- 1],NoiseLevels
+env
*noNoiseBands
,noNoiseBands
*sizeof(FIXP_DBL
));
144 for (band
= 0; band
< noNoiseBands
; band
++){
145 accu
= FL2FXCONST_DBL(0.0f
);
146 for (i
= 0; i
< NF_SMOOTHING_LENGTH
; i
++){
147 accu
+= fMultDiv2(smoothFilter
[i
], prevNoiseLevels
[i
][band
]);
149 FDK_ASSERT( (band
+ env
*noNoiseBands
) < MAX_NUM_NOISE_VALUES
);
150 NoiseLevels
[band
+ env
*noNoiseBands
] = accu
<<1;
155 /**************************************************************************/
157 \brief Does the noise floor level estiamtion.
159 The noiseLevel samples are scaled by the factor 0.25
164 /**************************************************************************/
166 qmfBasedNoiseFloorDetection(FIXP_DBL
*noiseLevel
, /*!< Pointer to vector to store the noise levels in.*/
167 FIXP_DBL
** quotaMatrixOrig
, /*!< Matrix holding the quota values of the original. */
168 SCHAR
*indexVector
, /*!< Index vector to obtain the patched data. */
169 INT startIndex
, /*!< Start index. */
170 INT stopIndex
, /*!< Stop index. */
171 INT startChannel
, /*!< Start channel of the current noise floor band.*/
172 INT stopChannel
, /*!< Stop channel of the current noise floor band. */
173 FIXP_DBL ana_max_level
, /*!< Maximum level of the adaptive noise.*/
174 FIXP_DBL noiseFloorOffset
, /*!< Noise floor offset. */
175 INT missingHarmonicFlag
, /*!< Flag indicating if a strong tonal component is missing.*/
176 FIXP_DBL weightFac
, /*!< Weightening factor for the difference between orig and sbr. */
177 INVF_MODE diffThres
, /*!< Threshold value to control the inverse filtering decision.*/
178 INVF_MODE inverseFilteringLevel
) /*!< Inverse filtering level of the current band.*/
181 FIXP_DBL meanOrig
=FL2FXCONST_DBL(0.0f
), meanSbr
=FL2FXCONST_DBL(0.0f
), diff
;
182 FIXP_DBL invIndex
= GetInvInt(stopIndex
-startIndex
);
183 FIXP_DBL invChannel
= GetInvInt(stopChannel
-startChannel
);
187 Calculate the mean value, over the current time segment, for the original, the HFR
188 and the difference, over all channels in the current frequency range.
191 if(missingHarmonicFlag
== 1){
192 for(l
= startChannel
; l
< stopChannel
;l
++){
194 accu
= FL2FXCONST_DBL(0.0f
);
195 for(k
= startIndex
; k
< stopIndex
; k
++){
196 accu
+= fMultDiv2(quotaMatrixOrig
[k
][l
], invIndex
);
198 meanOrig
= fixMax(meanOrig
,(accu
<<1));
201 accu
= FL2FXCONST_DBL(0.0f
);
202 for(k
= startIndex
; k
< stopIndex
; k
++){
203 accu
+= fMultDiv2(quotaMatrixOrig
[k
][indexVector
[l
]], invIndex
);
205 meanSbr
= fixMax(meanSbr
,(accu
<<1));
210 for(l
= startChannel
; l
< stopChannel
;l
++){
212 accu
= FL2FXCONST_DBL(0.0f
);
213 for(k
= startIndex
; k
< stopIndex
; k
++){
214 accu
+= fMultDiv2(quotaMatrixOrig
[k
][l
], invIndex
);
216 meanOrig
+= fMult((accu
<<1), invChannel
);
219 accu
= FL2FXCONST_DBL(0.0f
);
220 for(k
= startIndex
; k
< stopIndex
; k
++){
221 accu
+= fMultDiv2(quotaMatrixOrig
[k
][indexVector
[l
]], invIndex
);
223 meanSbr
+= fMult((accu
<<1), invChannel
);
227 /* Small fix to avoid noise during silent passages.*/
228 if( meanOrig
<= FL2FXCONST_DBL(0.000976562f
*RELAXATION_FLOAT
) &&
229 meanSbr
<= FL2FXCONST_DBL(0.000976562f
*RELAXATION_FLOAT
) )
231 meanOrig
= FL2FXCONST_DBL(101.5936673f
*RELAXATION_FLOAT
);
232 meanSbr
= FL2FXCONST_DBL(101.5936673f
*RELAXATION_FLOAT
);
235 meanOrig
= fixMax(meanOrig
,RELAXATION
);
236 meanSbr
= fixMax(meanSbr
,RELAXATION
);
238 if (missingHarmonicFlag
== 1 ||
239 inverseFilteringLevel
== INVF_MID_LEVEL
||
240 inverseFilteringLevel
== INVF_LOW_LEVEL
||
241 inverseFilteringLevel
== INVF_OFF
||
242 inverseFilteringLevel
<= diffThres
)
247 accu
= fDivNorm(meanSbr
, meanOrig
, &scale
);
249 diff
= fixMax( RELAXATION
,
250 fMult(RELAXATION_FRACT
,fMult(weightFac
,accu
)) >>( RELAXATION_SHIFT
-scale
) ) ;
254 * noise Level is now a positive value, i.e.
255 * the more harmonic the signal is the higher noise level,
256 * this makes no sense so we change the sign.
257 *********************************************************/
258 accu
= fDivNorm(diff
, meanOrig
, &scale
);
261 if ( (scale
>0) && (accu
> ((FIXP_DBL
)MAXVAL_DBL
)>>scale
) ) {
262 *noiseLevel
= (FIXP_DBL
)MAXVAL_DBL
;
265 *noiseLevel
= scaleValue(accu
, scale
);
269 * Add a noise floor offset to compensate for bias in the detector
270 *****************************************************************/
271 if(!missingHarmonicFlag
)
272 *noiseLevel
= fMult(*noiseLevel
, noiseFloorOffset
)<<(NOISE_FLOOR_OFFSET_SCALING
);
275 * check to see that we don't exceed the maximum allowed level
276 **************************************************************/
277 *noiseLevel
= fixMin(*noiseLevel
, ana_max_level
); /* ana_max_level is scaled with factor 0.25 */
280 /**************************************************************************/
282 \brief Does the noise floor level estiamtion.
283 The function calls the Noisefloor estimation function
284 for the time segments decided based upon the transient
285 information. The block is always divided into one or two segments.
291 /**************************************************************************/
293 FDKsbrEnc_sbrNoiseFloorEstimateQmf(HANDLE_SBR_NOISE_FLOOR_ESTIMATE h_sbrNoiseFloorEstimate
, /*!< Handle to SBR_NOISE_FLOOR_ESTIMATE struct */
294 const SBR_FRAME_INFO
*frame_info
, /*!< Time frequency grid of the current frame. */
295 FIXP_DBL
*noiseLevels
, /*!< Pointer to vector to store the noise levels in.*/
296 FIXP_DBL
**quotaMatrixOrig
, /*!< Matrix holding the quota values of the original. */
297 SCHAR
*indexVector
, /*!< Index vector to obtain the patched data. */
298 INT missingHarmonicsFlag
, /*!< Flag indicating if a strong tonal component will be missing. */
299 INT startIndex
, /*!< Start index. */
300 int numberOfEstimatesPerFrame
, /*!< The number of tonality estimates per frame. */
301 int transientFrame
, /*!< A flag indicating if a transient is present. */
302 INVF_MODE
* pInvFiltLevels
, /*!< Pointer to the vector holding the inverse filtering levels. */
308 INT nNoiseEnvelopes
, startPos
[2], stopPos
[2], env
, band
;
310 INT noNoiseBands
= h_sbrNoiseFloorEstimate
->noNoiseBands
;
311 INT
*freqBandTable
= h_sbrNoiseFloorEstimate
->freqBandTableQmf
;
313 nNoiseEnvelopes
= frame_info
->nNoiseEnvelopes
;
315 if (sbrSyntaxFlags
& SBR_SYNTAX_LOW_DELAY
) {
317 startPos
[0] = startIndex
;
318 stopPos
[0] = startIndex
+ min(numberOfEstimatesPerFrame
,2);
320 if(nNoiseEnvelopes
== 1){
321 startPos
[0] = startIndex
;
322 stopPos
[0] = startIndex
+ 2;
325 startPos
[0] = startIndex
;
326 stopPos
[0] = startIndex
+ 1;
327 startPos
[1] = startIndex
+ 1;
328 stopPos
[1] = startIndex
+ 2;
332 * Estimate the noise floor.
333 **************************************/
334 for(env
= 0; env
< nNoiseEnvelopes
; env
++){
335 for(band
= 0; band
< noNoiseBands
; band
++){
336 FDK_ASSERT( (band
+ env
*noNoiseBands
) < MAX_NUM_NOISE_VALUES
);
337 qmfBasedNoiseFloorDetection(&noiseLevels
[band
+ env
*noNoiseBands
],
343 freqBandTable
[band
+1],
344 h_sbrNoiseFloorEstimate
->ana_max_level
,
345 h_sbrNoiseFloorEstimate
->noiseFloorOffset
[band
],
346 missingHarmonicsFlag
,
347 h_sbrNoiseFloorEstimate
->weightFac
,
348 h_sbrNoiseFloorEstimate
->diffThres
,
349 pInvFiltLevels
[band
]);
355 * Smoothing of the values.
356 **************************/
357 smoothingOfNoiseLevels(noiseLevels
,
359 h_sbrNoiseFloorEstimate
->noNoiseBands
,
360 h_sbrNoiseFloorEstimate
->prevNoiseLevels
,
361 h_sbrNoiseFloorEstimate
->smoothFilter
,
366 for(env
= 0; env
< nNoiseEnvelopes
; env
++){
367 for(band
= 0; band
< noNoiseBands
; band
++){
368 FDK_ASSERT( (band
+ env
*noNoiseBands
) < MAX_NUM_NOISE_VALUES
);
369 noiseLevels
[band
+ env
*noNoiseBands
] =
370 (FIXP_DBL
)NOISE_FLOOR_OFFSET_64
- (FIXP_DBL
)CalcLdData(noiseLevels
[band
+ env
*noNoiseBands
]+(FIXP_DBL
)1) + QuantOffset
;
375 /**************************************************************************/
380 \return errorCode, noError if successful
383 /**************************************************************************/
385 downSampleLoRes(INT
*v_result
, /*!< */
386 INT num_result
, /*!< */
387 const UCHAR
*freqBandTableRef
,/*!< */
392 INT org_length
,result_length
;
393 INT v_index
[MAX_FREQ_COEFFS
/2];
397 result_length
=num_result
;
399 v_index
[0]=0; /* Always use left border */
401 while(org_length
> 0) /* Create downsample vector */
404 step
=org_length
/result_length
; /* floor; */
405 org_length
=org_length
- step
;
407 v_index
[i
]=v_index
[i
-1]+step
;
410 if(i
!= num_result
) /* Should never happen */
411 return (1);/* error downsampling */
413 for(j
=0;j
<=i
;j
++) /* Use downsample vector to index LoResolution vector. */
415 v_result
[j
]=freqBandTableRef
[v_index
[j
]];
421 /**************************************************************************/
423 \brief Initialize an instance of the noise floor level estimation module.
426 \return errorCode, noError if successful
429 /**************************************************************************/
431 FDKsbrEnc_InitSbrNoiseFloorEstimate (HANDLE_SBR_NOISE_FLOOR_ESTIMATE h_sbrNoiseFloorEstimate
, /*!< Handle to SBR_NOISE_FLOOR_ESTIMATE struct */
432 INT ana_max_level
, /*!< Maximum level of the adaptive noise. */
433 const UCHAR
*freqBandTable
, /*!< Frequany band table. */
434 INT nSfb
, /*!< Number of frequency bands. */
435 INT noiseBands
, /*!< Number of noise bands per octave. */
436 INT noiseFloorOffset
, /*!< Noise floor offset. */
437 INT timeSlots
, /*!< Number of time slots in a frame. */
438 UINT useSpeechConfig
/*!< Flag: adapt tuning parameters according to speech */
444 FDKmemclear(h_sbrNoiseFloorEstimate
,sizeof(SBR_NOISE_FLOOR_ESTIMATE
));
446 h_sbrNoiseFloorEstimate
->smoothFilter
= smoothFilter
;
447 if (useSpeechConfig
) {
448 h_sbrNoiseFloorEstimate
->weightFac
= (FIXP_DBL
)MAXVAL_DBL
;
449 h_sbrNoiseFloorEstimate
->diffThres
= INVF_LOW_LEVEL
;
452 h_sbrNoiseFloorEstimate
->weightFac
= FL2FXCONST_DBL(0.25f
);
453 h_sbrNoiseFloorEstimate
->diffThres
= INVF_MID_LEVEL
;
456 h_sbrNoiseFloorEstimate
->timeSlots
= timeSlots
;
457 h_sbrNoiseFloorEstimate
->noiseBands
= noiseBands
;
459 /* h_sbrNoiseFloorEstimate->ana_max_level is scaled by 0.25 */
460 switch(ana_max_level
)
463 h_sbrNoiseFloorEstimate
->ana_max_level
= (FIXP_DBL
)MAXVAL_DBL
;
466 h_sbrNoiseFloorEstimate
->ana_max_level
= FL2FXCONST_DBL(0.5);
469 h_sbrNoiseFloorEstimate
->ana_max_level
= FL2FXCONST_DBL(0.125);
472 /* Should not enter here */
473 h_sbrNoiseFloorEstimate
->ana_max_level
= (FIXP_DBL
)MAXVAL_DBL
;
478 calculate number of noise bands and allocate
480 if(FDKsbrEnc_resetSbrNoiseFloorEstimate(h_sbrNoiseFloorEstimate
,freqBandTable
,nSfb
))
483 if(noiseFloorOffset
== 0) {
484 tmp
= ((FIXP_DBL
)MAXVAL_DBL
)>>NOISE_FLOOR_OFFSET_SCALING
;
487 /* noiseFloorOffset has to be smaller than 12, because
488 the result of the calculation below must be smaller than 1:
489 (2^(noiseFloorOffset/3))*2^4<1 */
490 FDK_ASSERT(noiseFloorOffset
<12);
492 /* Assumes the noise floor offset in tuning table are in q31 */
493 /* Change the qformat here when non-zero values would be filled */
494 exp
= fDivNorm((FIXP_DBL
)noiseFloorOffset
, 3, &qexp
);
495 tmp
= fPow(2, DFRACT_BITS
-1, exp
, qexp
, &qtmp
);
496 tmp
= scaleValue(tmp
, qtmp
-NOISE_FLOOR_OFFSET_SCALING
);
499 for(i
=0;i
<h_sbrNoiseFloorEstimate
->noNoiseBands
;i
++) {
500 h_sbrNoiseFloorEstimate
->noiseFloorOffset
[i
] = tmp
;
506 /**************************************************************************/
508 \brief Resets the current instance of the noise floor estiamtion
512 \return errorCode, noError if successful
515 /**************************************************************************/
517 FDKsbrEnc_resetSbrNoiseFloorEstimate (HANDLE_SBR_NOISE_FLOOR_ESTIMATE h_sbrNoiseFloorEstimate
, /*!< Handle to SBR_NOISE_FLOOR_ESTIMATE struct */
518 const UCHAR
*freqBandTable
, /*!< Frequany band table. */
519 INT nSfb
) /*!< Number of bands in the frequency band table. */
524 * Calculate number of noise bands
525 ***********************************/
526 k2
=freqBandTable
[nSfb
];
528 if(h_sbrNoiseFloorEstimate
->noiseBands
== 0){
529 h_sbrNoiseFloorEstimate
->noNoiseBands
= 1;
533 * Calculate number of noise bands 1,2 or 3 bands/octave
534 ********************************************************/
535 FIXP_DBL tmp
, ratio
, lg2
;
536 INT ratio_e
, qlg2
, nNoiseBands
;
538 ratio
= fDivNorm(k2
, kx
, &ratio_e
);
539 lg2
= fLog2(ratio
, ratio_e
, &qlg2
);
540 tmp
= fMult((FIXP_DBL
)(h_sbrNoiseFloorEstimate
->noiseBands
<<24), lg2
);
541 tmp
= scaleValue(tmp
, qlg2
-23);
543 nNoiseBands
= (INT
)((tmp
+ (FIXP_DBL
)1) >> 1);
546 if (nNoiseBands
> MAX_NUM_NOISE_COEFFS
) {
547 nNoiseBands
= MAX_NUM_NOISE_COEFFS
;
550 if( nNoiseBands
== 0 ) {
554 h_sbrNoiseFloorEstimate
->noNoiseBands
= nNoiseBands
;
559 return(downSampleLoRes(h_sbrNoiseFloorEstimate
->freqBandTableQmf
,
560 h_sbrNoiseFloorEstimate
->noNoiseBands
,
561 freqBandTable
,nSfb
));
564 /**************************************************************************/
566 \brief Deletes the current instancce of the noise floor level
573 /**************************************************************************/
575 FDKsbrEnc_deleteSbrNoiseFloorEstimate (HANDLE_SBR_NOISE_FLOOR_ESTIMATE h_sbrNoiseFloorEstimate
) /*!< Handle to SBR_NOISE_FLOOR_ESTIMATE struct */
578 if (h_sbrNoiseFloorEstimate
) {