1
2/* -----------------------------------------------------------------------------------------------------------
3Software 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
9The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16of the MPEG specifications.
17
18Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20individually for the purpose of encoding or decoding bit streams in products that are compliant with
21the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27applications information and documentation.
28
292.    COPYRIGHT LICENSE
30
31Redistribution and use in source and binary forms, with or without modification, are permitted without
32payment of copyright license fees provided that you satisfy the following conditions:
33
34You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35your modifications thereto in source code form.
36
37You must retain the complete text of this software license in the documentation and/or other materials
38provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40modifications thereto to recipients of copies in binary form.
41
42The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43prior written permission.
44
45You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46software or your modifications thereto.
47
48Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49and 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
533.    NO PATENT LICENSE
54
55NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57respect to this software.
58
59You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60by appropriate patent licenses.
61
624.    DISCLAIMER
63
64This 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
66of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69or business interruption, however caused and on any theory of liability, whether in contract, strict
70liability, or tort (including negligence), arising in any way out of the use of this software, even if
71advised of the possibility of such damage.
72
735.    CONTACT INFORMATION
74
75Fraunhofer Institute for Integrated Circuits IIS
76Attention: Audio and Multimedia Departments - FDK AAC LL
77Am Wolfsmantel 33
7891058 Erlangen, Germany
79
80www.iis.fraunhofer.de/amm
81amm-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*****************************************************************************/
108static 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
143void 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}
229