Imported Debian version 0.1.3.1
[deb_fdk-aac.git] / libAACenc / src / noisedet.cpp
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 Audio Encoder **************************
85
86 Initial author: M. Lohwasser
87 contents/description: noisedet.c
88 Routines for Noise Detection
89
90 ******************************************************************************/
91
92 #include "noisedet.h"
93
94 #include "aacenc_pns.h"
95 #include "pnsparam.h"
96
97
98 /*****************************************************************************
99
100 functionname: FDKaacEnc_fuzzyIsSmaller
101 description: Fuzzy value calculation for "testVal is smaller than refVal"
102 returns: fuzzy value
103 input: test and ref Value,
104 low and high Lim
105 output: return fuzzy value
106
107 *****************************************************************************/
108 static FIXP_SGL FDKaacEnc_fuzzyIsSmaller( FIXP_DBL testVal,
109 FIXP_DBL refVal,
110 FIXP_DBL loLim,
111 FIXP_DBL hiLim )
112 {
113 if (refVal <= FL2FXCONST_DBL(0.0))
114 return( FL2FXCONST_SGL(0.0f) );
115 else if (testVal >= fMult((hiLim>>1)+(loLim>>1), refVal))
116 return( FL2FXCONST_SGL(0.0f) );
117 else return( (FIXP_SGL)MAXVAL_SGL );
118 }
119
120
121
122 /*****************************************************************************
123
124 functionname: FDKaacEnc_noiseDetect
125 description: detect tonal sfb's; two tests
126 Powerdistribution:
127 sfb splittet in four regions,
128 compare the energy in all sections
129 PsychTonality:
130 compare tonality from chaosmeasure with reftonality
131 returns:
132 input: spectrum of one large mdct
133 number of sfb's
134 pointer to offset of sfb's
135 pointer to noiseFuzzyMeasure (modified)
136 noiseparams struct
137 pointer to sfb energies
138 pointer to tonality calculated in chaosmeasure
139 output: noiseFuzzy Measure
140
141 *****************************************************************************/
142
143 void FDKaacEnc_noiseDetect(FIXP_DBL *RESTRICT mdctSpectrum,
144 INT *RESTRICT sfbMaxScaleSpec,
145 INT sfbActive,
146 const INT *RESTRICT sfbOffset,
147 FIXP_SGL *RESTRICT noiseFuzzyMeasure,
148 NOISEPARAMS *np,
149 FIXP_SGL *RESTRICT sfbtonality )
150
151 {
152 int i, k, sfb, sfbWidth;
153 FIXP_SGL fuzzy, fuzzyTotal;
154 FIXP_DBL refVal, testVal;
155
156 /***** Start detection phase *****/
157 /* Start noise detection for each band based on a number of checks */
158 for (sfb=0; sfb<sfbActive; sfb++) {
159
160 fuzzyTotal = (FIXP_SGL)MAXVAL_SGL;
161 sfbWidth = sfbOffset[sfb+1] - sfbOffset[sfb];
162
163 /* Reset output for lower bands or too small bands */
164 if (sfb < np->startSfb || sfbWidth < np->minSfbWidth) {
165 noiseFuzzyMeasure[sfb] = FL2FXCONST_SGL(0.0f);
166 continue;
167 }
168
169 if ( (np->detectionAlgorithmFlags & USE_POWER_DISTRIBUTION) && (fuzzyTotal > FL2FXCONST_SGL(0.5f)) ) {
170 FIXP_DBL fhelp1, fhelp2, fhelp3, fhelp4, maxVal, minVal;
171 INT leadingBits = fixMax(0,(sfbMaxScaleSpec[sfb] - 3)); /* max sfbWidth = 96/4 ; 2^5=32 => 5/2 = 3 (spc*spc) */
172
173 /* check power distribution in four regions */
174 fhelp1 = fhelp2 = fhelp3 = fhelp4 = FL2FXCONST_DBL(0.0f);
175 k = sfbWidth >>2; /* Width of a quarter band */
176
177 for (i=sfbOffset[sfb]; i<sfbOffset[sfb]+k; i++) {
178 fhelp1 = fPow2AddDiv2(fhelp1, mdctSpectrum[i]<<leadingBits);
179 fhelp2 = fPow2AddDiv2(fhelp2, mdctSpectrum[i+k]<<leadingBits);
180 fhelp3 = fPow2AddDiv2(fhelp3, mdctSpectrum[i+2*k]<<leadingBits);
181 fhelp4 = fPow2AddDiv2(fhelp4, mdctSpectrum[i+3*k]<<leadingBits);
182 }
183
184 /* get max into fhelp: */
185 maxVal = fixMax(fhelp1, fhelp2);
186 maxVal = fixMax(maxVal, fhelp3);
187 maxVal = fixMax(maxVal, fhelp4);
188
189 /* get min into fhelp1: */
190 minVal = fixMin(fhelp1, fhelp2);
191 minVal = fixMin(minVal, fhelp3);
192 minVal = fixMin(minVal, fhelp4);
193
194 /* Normalize min and max Val */
195 leadingBits = CountLeadingBits(maxVal);
196 testVal = maxVal << leadingBits;
197 refVal = minVal << leadingBits;
198
199 /* calculate fuzzy value for power distribution */
200 testVal = fMultDiv2(testVal, np->powDistPSDcurve[sfb]);
201
202 fuzzy = FDKaacEnc_fuzzyIsSmaller(testVal, /* 1/2 * maxValue * PSDcurve */
203 refVal, /* 1 * minValue */
204 FL2FXCONST_DBL(0.495), /* 1/2 * loLim (0.99f/2) */
205 FL2FXCONST_DBL(0.505)); /* 1/2 * hiLim (1.01f/2) */
206
207 fuzzyTotal = fixMin(fuzzyTotal, fuzzy);
208 }
209
210 if ( (np->detectionAlgorithmFlags & USE_PSYCH_TONALITY) && (fuzzyTotal > FL2FXCONST_SGL(0.5f)) ) {
211 /* Detection with tonality-value of psych. acoustic (here: 1 is tonal!)*/
212
213 testVal = FX_SGL2FX_DBL(sfbtonality[sfb])>>1; /* 1/2 * sfbTonality */
214 refVal = np->refTonality;
215
216 fuzzy = FDKaacEnc_fuzzyIsSmaller(testVal,
217 refVal,
218 FL2FXCONST_DBL(0.45f), /* 1/2 * loLim (0.9f/2) */
219 FL2FXCONST_DBL(0.55f)); /* 1/2 * hiLim (1.1f/2) */
220
221 fuzzyTotal = fixMin(fuzzyTotal, fuzzy);
222 }
223
224
225 /* Output of final result */
226 noiseFuzzyMeasure[sfb] = fuzzyTotal;
227 }
228 }