xref: /aosp_15_r20/external/aac/libAACenc/src/noisedet.cpp (revision e54365361535b070c2db7374cec45c159c7d0e7a)
1*e5436536SAndroid Build Coastguard Worker /* -----------------------------------------------------------------------------
2*e5436536SAndroid Build Coastguard Worker Software License for The Fraunhofer FDK AAC Codec Library for Android
3*e5436536SAndroid Build Coastguard Worker 
4*e5436536SAndroid Build Coastguard Worker © Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten
5*e5436536SAndroid Build Coastguard Worker Forschung e.V. All rights reserved.
6*e5436536SAndroid Build Coastguard Worker 
7*e5436536SAndroid Build Coastguard Worker  1.    INTRODUCTION
8*e5436536SAndroid Build Coastguard Worker The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9*e5436536SAndroid Build Coastguard Worker that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10*e5436536SAndroid Build Coastguard Worker scheme for digital audio. This FDK AAC Codec software is intended to be used on
11*e5436536SAndroid Build Coastguard Worker a wide variety of Android devices.
12*e5436536SAndroid Build Coastguard Worker 
13*e5436536SAndroid Build Coastguard Worker AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14*e5436536SAndroid Build Coastguard Worker general perceptual audio codecs. AAC-ELD is considered the best-performing
15*e5436536SAndroid Build Coastguard Worker full-bandwidth communications codec by independent studies and is widely
16*e5436536SAndroid Build Coastguard Worker deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17*e5436536SAndroid Build Coastguard Worker specifications.
18*e5436536SAndroid Build Coastguard Worker 
19*e5436536SAndroid Build Coastguard Worker Patent licenses for necessary patent claims for the FDK AAC Codec (including
20*e5436536SAndroid Build Coastguard Worker those of Fraunhofer) may be obtained through Via Licensing
21*e5436536SAndroid Build Coastguard Worker (www.vialicensing.com) or through the respective patent owners individually for
22*e5436536SAndroid Build Coastguard Worker the purpose of encoding or decoding bit streams in products that are compliant
23*e5436536SAndroid Build Coastguard Worker with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24*e5436536SAndroid Build Coastguard Worker Android devices already license these patent claims through Via Licensing or
25*e5436536SAndroid Build Coastguard Worker directly from the patent owners, and therefore FDK AAC Codec software may
26*e5436536SAndroid Build Coastguard Worker already be covered under those patent licenses when it is used for those
27*e5436536SAndroid Build Coastguard Worker licensed purposes only.
28*e5436536SAndroid Build Coastguard Worker 
29*e5436536SAndroid Build Coastguard Worker Commercially-licensed AAC software libraries, including floating-point versions
30*e5436536SAndroid Build Coastguard Worker with enhanced sound quality, are also available from Fraunhofer. Users are
31*e5436536SAndroid Build Coastguard Worker encouraged to check the Fraunhofer website for additional applications
32*e5436536SAndroid Build Coastguard Worker information and documentation.
33*e5436536SAndroid Build Coastguard Worker 
34*e5436536SAndroid Build Coastguard Worker 2.    COPYRIGHT LICENSE
35*e5436536SAndroid Build Coastguard Worker 
36*e5436536SAndroid Build Coastguard Worker Redistribution and use in source and binary forms, with or without modification,
37*e5436536SAndroid Build Coastguard Worker are permitted without payment of copyright license fees provided that you
38*e5436536SAndroid Build Coastguard Worker satisfy the following conditions:
39*e5436536SAndroid Build Coastguard Worker 
40*e5436536SAndroid Build Coastguard Worker You must retain the complete text of this software license in redistributions of
41*e5436536SAndroid Build Coastguard Worker the FDK AAC Codec or your modifications thereto in source code form.
42*e5436536SAndroid Build Coastguard Worker 
43*e5436536SAndroid Build Coastguard Worker You must retain the complete text of this software license in the documentation
44*e5436536SAndroid Build Coastguard Worker and/or other materials provided with redistributions of the FDK AAC Codec or
45*e5436536SAndroid Build Coastguard Worker your modifications thereto in binary form. You must make available free of
46*e5436536SAndroid Build Coastguard Worker charge copies of the complete source code of the FDK AAC Codec and your
47*e5436536SAndroid Build Coastguard Worker modifications thereto to recipients of copies in binary form.
48*e5436536SAndroid Build Coastguard Worker 
49*e5436536SAndroid Build Coastguard Worker The name of Fraunhofer may not be used to endorse or promote products derived
50*e5436536SAndroid Build Coastguard Worker from this library without prior written permission.
51*e5436536SAndroid Build Coastguard Worker 
52*e5436536SAndroid Build Coastguard Worker You may not charge copyright license fees for anyone to use, copy or distribute
53*e5436536SAndroid Build Coastguard Worker the FDK AAC Codec software or your modifications thereto.
54*e5436536SAndroid Build Coastguard Worker 
55*e5436536SAndroid Build Coastguard Worker Your modified versions of the FDK AAC Codec must carry prominent notices stating
56*e5436536SAndroid Build Coastguard Worker that you changed the software and the date of any change. For modified versions
57*e5436536SAndroid Build Coastguard Worker of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58*e5436536SAndroid Build Coastguard Worker must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59*e5436536SAndroid Build Coastguard Worker AAC Codec Library for Android."
60*e5436536SAndroid Build Coastguard Worker 
61*e5436536SAndroid Build Coastguard Worker 3.    NO PATENT LICENSE
62*e5436536SAndroid Build Coastguard Worker 
63*e5436536SAndroid Build Coastguard Worker NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64*e5436536SAndroid Build Coastguard Worker limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65*e5436536SAndroid Build Coastguard Worker Fraunhofer provides no warranty of patent non-infringement with respect to this
66*e5436536SAndroid Build Coastguard Worker software.
67*e5436536SAndroid Build Coastguard Worker 
68*e5436536SAndroid Build Coastguard Worker You may use this FDK AAC Codec software or modifications thereto only for
69*e5436536SAndroid Build Coastguard Worker purposes that are authorized by appropriate patent licenses.
70*e5436536SAndroid Build Coastguard Worker 
71*e5436536SAndroid Build Coastguard Worker 4.    DISCLAIMER
72*e5436536SAndroid Build Coastguard Worker 
73*e5436536SAndroid Build Coastguard Worker This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74*e5436536SAndroid Build Coastguard Worker holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75*e5436536SAndroid Build Coastguard Worker including but not limited to the implied warranties of merchantability and
76*e5436536SAndroid Build Coastguard Worker fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77*e5436536SAndroid Build Coastguard Worker CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78*e5436536SAndroid Build Coastguard Worker or consequential damages, including but not limited to procurement of substitute
79*e5436536SAndroid Build Coastguard Worker goods or services; loss of use, data, or profits, or business interruption,
80*e5436536SAndroid Build Coastguard Worker however caused and on any theory of liability, whether in contract, strict
81*e5436536SAndroid Build Coastguard Worker liability, or tort (including negligence), arising in any way out of the use of
82*e5436536SAndroid Build Coastguard Worker this software, even if advised of the possibility of such damage.
83*e5436536SAndroid Build Coastguard Worker 
84*e5436536SAndroid Build Coastguard Worker 5.    CONTACT INFORMATION
85*e5436536SAndroid Build Coastguard Worker 
86*e5436536SAndroid Build Coastguard Worker Fraunhofer Institute for Integrated Circuits IIS
87*e5436536SAndroid Build Coastguard Worker Attention: Audio and Multimedia Departments - FDK AAC LL
88*e5436536SAndroid Build Coastguard Worker Am Wolfsmantel 33
89*e5436536SAndroid Build Coastguard Worker 91058 Erlangen, Germany
90*e5436536SAndroid Build Coastguard Worker 
91*e5436536SAndroid Build Coastguard Worker www.iis.fraunhofer.de/amm
92*e5436536SAndroid Build Coastguard Worker [email protected]
93*e5436536SAndroid Build Coastguard Worker ----------------------------------------------------------------------------- */
94*e5436536SAndroid Build Coastguard Worker 
95*e5436536SAndroid Build Coastguard Worker /**************************** AAC encoder library ******************************
96*e5436536SAndroid Build Coastguard Worker 
97*e5436536SAndroid Build Coastguard Worker    Author(s):   M. Lohwasser
98*e5436536SAndroid Build Coastguard Worker 
99*e5436536SAndroid Build Coastguard Worker    Description: noisedet.c
100*e5436536SAndroid Build Coastguard Worker                 Routines for Noise Detection
101*e5436536SAndroid Build Coastguard Worker 
102*e5436536SAndroid Build Coastguard Worker *******************************************************************************/
103*e5436536SAndroid Build Coastguard Worker 
104*e5436536SAndroid Build Coastguard Worker #include "noisedet.h"
105*e5436536SAndroid Build Coastguard Worker 
106*e5436536SAndroid Build Coastguard Worker #include "aacenc_pns.h"
107*e5436536SAndroid Build Coastguard Worker #include "pnsparam.h"
108*e5436536SAndroid Build Coastguard Worker 
109*e5436536SAndroid Build Coastguard Worker /*****************************************************************************
110*e5436536SAndroid Build Coastguard Worker 
111*e5436536SAndroid Build Coastguard Worker     functionname: FDKaacEnc_fuzzyIsSmaller
112*e5436536SAndroid Build Coastguard Worker     description:  Fuzzy value calculation for "testVal is smaller than refVal"
113*e5436536SAndroid Build Coastguard Worker     returns:      fuzzy value
114*e5436536SAndroid Build Coastguard Worker     input:        test and ref Value,
115*e5436536SAndroid Build Coastguard Worker                   low and high Lim
116*e5436536SAndroid Build Coastguard Worker     output:       return fuzzy value
117*e5436536SAndroid Build Coastguard Worker 
118*e5436536SAndroid Build Coastguard Worker *****************************************************************************/
FDKaacEnc_fuzzyIsSmaller(FIXP_DBL testVal,FIXP_DBL refVal,FIXP_DBL loLim,FIXP_DBL hiLim)119*e5436536SAndroid Build Coastguard Worker static FIXP_SGL FDKaacEnc_fuzzyIsSmaller(FIXP_DBL testVal, FIXP_DBL refVal,
120*e5436536SAndroid Build Coastguard Worker                                          FIXP_DBL loLim, FIXP_DBL hiLim) {
121*e5436536SAndroid Build Coastguard Worker   if (refVal <= FL2FXCONST_DBL(0.0))
122*e5436536SAndroid Build Coastguard Worker     return (FL2FXCONST_SGL(0.0f));
123*e5436536SAndroid Build Coastguard Worker   else if (testVal >= fMult((hiLim >> 1) + (loLim >> 1), refVal))
124*e5436536SAndroid Build Coastguard Worker     return (FL2FXCONST_SGL(0.0f));
125*e5436536SAndroid Build Coastguard Worker   else
126*e5436536SAndroid Build Coastguard Worker     return ((FIXP_SGL)MAXVAL_SGL);
127*e5436536SAndroid Build Coastguard Worker }
128*e5436536SAndroid Build Coastguard Worker 
129*e5436536SAndroid Build Coastguard Worker /*****************************************************************************
130*e5436536SAndroid Build Coastguard Worker 
131*e5436536SAndroid Build Coastguard Worker     functionname: FDKaacEnc_noiseDetect
132*e5436536SAndroid Build Coastguard Worker     description:  detect tonal sfb's; two tests
133*e5436536SAndroid Build Coastguard Worker                   Powerdistribution:
134*e5436536SAndroid Build Coastguard Worker                     sfb splittet in four regions,
135*e5436536SAndroid Build Coastguard Worker                     compare the energy in all sections
136*e5436536SAndroid Build Coastguard Worker                   PsychTonality:
137*e5436536SAndroid Build Coastguard Worker                     compare tonality from chaosmeasure with reftonality
138*e5436536SAndroid Build Coastguard Worker     returns:
139*e5436536SAndroid Build Coastguard Worker     input:        spectrum of one large mdct
140*e5436536SAndroid Build Coastguard Worker                   number of sfb's
141*e5436536SAndroid Build Coastguard Worker                   pointer to offset of sfb's
142*e5436536SAndroid Build Coastguard Worker                   pointer to noiseFuzzyMeasure (modified)
143*e5436536SAndroid Build Coastguard Worker                   noiseparams struct
144*e5436536SAndroid Build Coastguard Worker                   pointer to sfb energies
145*e5436536SAndroid Build Coastguard Worker                   pointer to tonality calculated in chaosmeasure
146*e5436536SAndroid Build Coastguard Worker     output:       noiseFuzzy Measure
147*e5436536SAndroid Build Coastguard Worker 
148*e5436536SAndroid Build Coastguard Worker *****************************************************************************/
149*e5436536SAndroid Build Coastguard Worker 
FDKaacEnc_noiseDetect(FIXP_DBL * RESTRICT mdctSpectrum,INT * RESTRICT sfbMaxScaleSpec,INT sfbActive,const INT * RESTRICT sfbOffset,FIXP_SGL * RESTRICT noiseFuzzyMeasure,NOISEPARAMS * np,FIXP_SGL * RESTRICT sfbtonality)150*e5436536SAndroid Build Coastguard Worker void FDKaacEnc_noiseDetect(FIXP_DBL *RESTRICT mdctSpectrum,
151*e5436536SAndroid Build Coastguard Worker                            INT *RESTRICT sfbMaxScaleSpec, INT sfbActive,
152*e5436536SAndroid Build Coastguard Worker                            const INT *RESTRICT sfbOffset,
153*e5436536SAndroid Build Coastguard Worker                            FIXP_SGL *RESTRICT noiseFuzzyMeasure,
154*e5436536SAndroid Build Coastguard Worker                            NOISEPARAMS *np, FIXP_SGL *RESTRICT sfbtonality)
155*e5436536SAndroid Build Coastguard Worker 
156*e5436536SAndroid Build Coastguard Worker {
157*e5436536SAndroid Build Coastguard Worker   int i, k, sfb, sfbWidth;
158*e5436536SAndroid Build Coastguard Worker   FIXP_SGL fuzzy, fuzzyTotal;
159*e5436536SAndroid Build Coastguard Worker   FIXP_DBL refVal, testVal;
160*e5436536SAndroid Build Coastguard Worker 
161*e5436536SAndroid Build Coastguard Worker   /***** Start detection phase *****/
162*e5436536SAndroid Build Coastguard Worker   /* Start noise detection for each band based on a number of checks */
163*e5436536SAndroid Build Coastguard Worker   for (sfb = 0; sfb < sfbActive; sfb++) {
164*e5436536SAndroid Build Coastguard Worker     fuzzyTotal = (FIXP_SGL)MAXVAL_SGL;
165*e5436536SAndroid Build Coastguard Worker     sfbWidth = sfbOffset[sfb + 1] - sfbOffset[sfb];
166*e5436536SAndroid Build Coastguard Worker 
167*e5436536SAndroid Build Coastguard Worker     /* Reset output for lower bands or too small bands */
168*e5436536SAndroid Build Coastguard Worker     if (sfb < np->startSfb || sfbWidth < np->minSfbWidth) {
169*e5436536SAndroid Build Coastguard Worker       noiseFuzzyMeasure[sfb] = FL2FXCONST_SGL(0.0f);
170*e5436536SAndroid Build Coastguard Worker       continue;
171*e5436536SAndroid Build Coastguard Worker     }
172*e5436536SAndroid Build Coastguard Worker 
173*e5436536SAndroid Build Coastguard Worker     if ((np->detectionAlgorithmFlags & USE_POWER_DISTRIBUTION) &&
174*e5436536SAndroid Build Coastguard Worker         (fuzzyTotal > FL2FXCONST_SGL(0.5f))) {
175*e5436536SAndroid Build Coastguard Worker       FIXP_DBL fhelp1, fhelp2, fhelp3, fhelp4, maxVal, minVal;
176*e5436536SAndroid Build Coastguard Worker       INT leadingBits = fixMax(
177*e5436536SAndroid Build Coastguard Worker           0, (sfbMaxScaleSpec[sfb] -
178*e5436536SAndroid Build Coastguard Worker               3)); /* max sfbWidth = 96/4 ; 2^5=32 => 5/2 = 3 (spc*spc) */
179*e5436536SAndroid Build Coastguard Worker 
180*e5436536SAndroid Build Coastguard Worker       /*  check power distribution in four regions */
181*e5436536SAndroid Build Coastguard Worker       fhelp1 = fhelp2 = fhelp3 = fhelp4 = FL2FXCONST_DBL(0.0f);
182*e5436536SAndroid Build Coastguard Worker       k = sfbWidth >> 2; /* Width of a quarter band */
183*e5436536SAndroid Build Coastguard Worker 
184*e5436536SAndroid Build Coastguard Worker       for (i = sfbOffset[sfb]; i < sfbOffset[sfb] + k; i++) {
185*e5436536SAndroid Build Coastguard Worker         fhelp1 = fPow2AddDiv2(fhelp1, mdctSpectrum[i] << leadingBits);
186*e5436536SAndroid Build Coastguard Worker         fhelp2 = fPow2AddDiv2(fhelp2, mdctSpectrum[i + k] << leadingBits);
187*e5436536SAndroid Build Coastguard Worker         fhelp3 = fPow2AddDiv2(fhelp3, mdctSpectrum[i + 2 * k] << leadingBits);
188*e5436536SAndroid Build Coastguard Worker         fhelp4 = fPow2AddDiv2(fhelp4, mdctSpectrum[i + 3 * k] << leadingBits);
189*e5436536SAndroid Build Coastguard Worker       }
190*e5436536SAndroid Build Coastguard Worker 
191*e5436536SAndroid Build Coastguard Worker       /* get max into fhelp: */
192*e5436536SAndroid Build Coastguard Worker       maxVal = fixMax(fhelp1, fhelp2);
193*e5436536SAndroid Build Coastguard Worker       maxVal = fixMax(maxVal, fhelp3);
194*e5436536SAndroid Build Coastguard Worker       maxVal = fixMax(maxVal, fhelp4);
195*e5436536SAndroid Build Coastguard Worker 
196*e5436536SAndroid Build Coastguard Worker       /* get min into fhelp1: */
197*e5436536SAndroid Build Coastguard Worker       minVal = fixMin(fhelp1, fhelp2);
198*e5436536SAndroid Build Coastguard Worker       minVal = fixMin(minVal, fhelp3);
199*e5436536SAndroid Build Coastguard Worker       minVal = fixMin(minVal, fhelp4);
200*e5436536SAndroid Build Coastguard Worker 
201*e5436536SAndroid Build Coastguard Worker       /* Normalize min and max Val */
202*e5436536SAndroid Build Coastguard Worker       leadingBits = CountLeadingBits(maxVal);
203*e5436536SAndroid Build Coastguard Worker       testVal = maxVal << leadingBits;
204*e5436536SAndroid Build Coastguard Worker       refVal = minVal << leadingBits;
205*e5436536SAndroid Build Coastguard Worker 
206*e5436536SAndroid Build Coastguard Worker       /* calculate fuzzy value for power distribution */
207*e5436536SAndroid Build Coastguard Worker       testVal = fMultDiv2(testVal, np->powDistPSDcurve[sfb]);
208*e5436536SAndroid Build Coastguard Worker 
209*e5436536SAndroid Build Coastguard Worker       fuzzy = FDKaacEnc_fuzzyIsSmaller(
210*e5436536SAndroid Build Coastguard Worker           testVal,                /* 1/2 * maxValue * PSDcurve */
211*e5436536SAndroid Build Coastguard Worker           refVal,                 /* 1   * minValue            */
212*e5436536SAndroid Build Coastguard Worker           FL2FXCONST_DBL(0.495),  /* 1/2 * loLim  (0.99f/2)    */
213*e5436536SAndroid Build Coastguard Worker           FL2FXCONST_DBL(0.505)); /* 1/2 * hiLim  (1.01f/2)    */
214*e5436536SAndroid Build Coastguard Worker 
215*e5436536SAndroid Build Coastguard Worker       fuzzyTotal = fixMin(fuzzyTotal, fuzzy);
216*e5436536SAndroid Build Coastguard Worker     }
217*e5436536SAndroid Build Coastguard Worker 
218*e5436536SAndroid Build Coastguard Worker     if ((np->detectionAlgorithmFlags & USE_PSYCH_TONALITY) &&
219*e5436536SAndroid Build Coastguard Worker         (fuzzyTotal > FL2FXCONST_SGL(0.5f))) {
220*e5436536SAndroid Build Coastguard Worker       /* Detection with tonality-value of psych. acoustic (here: 1 is tonal!)*/
221*e5436536SAndroid Build Coastguard Worker 
222*e5436536SAndroid Build Coastguard Worker       testVal = FX_SGL2FX_DBL(sfbtonality[sfb]) >> 1; /* 1/2 * sfbTonality */
223*e5436536SAndroid Build Coastguard Worker       refVal = np->refTonality;
224*e5436536SAndroid Build Coastguard Worker 
225*e5436536SAndroid Build Coastguard Worker       fuzzy = FDKaacEnc_fuzzyIsSmaller(
226*e5436536SAndroid Build Coastguard Worker           testVal, refVal, FL2FXCONST_DBL(0.45f), /* 1/2 * loLim  (0.9f/2) */
227*e5436536SAndroid Build Coastguard Worker           FL2FXCONST_DBL(0.55f)); /* 1/2 * hiLim  (1.1f/2)     */
228*e5436536SAndroid Build Coastguard Worker 
229*e5436536SAndroid Build Coastguard Worker       fuzzyTotal = fixMin(fuzzyTotal, fuzzy);
230*e5436536SAndroid Build Coastguard Worker     }
231*e5436536SAndroid Build Coastguard Worker 
232*e5436536SAndroid Build Coastguard Worker     /* Output of final result */
233*e5436536SAndroid Build Coastguard Worker     noiseFuzzyMeasure[sfb] = fuzzyTotal;
234*e5436536SAndroid Build Coastguard Worker   }
235*e5436536SAndroid Build Coastguard Worker }
236