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: PNS parameters depending on bitrate and bandwidth
88
89******************************************************************************/
90
91#include "pnsparam.h"
92#include "psy_configuration.h"
93
94typedef struct {
95  SHORT     startFreq;
96  /* Parameters for detection */
97  FIXP_SGL  refPower;
98  FIXP_SGL  refTonality;
99  SHORT     tnsGainThreshold;     /* scaled by TNS_PREDGAIN_SCALE (=1000) */
100  SHORT     tnsPNSGainThreshold;  /* scaled by TNS_PREDGAIN_SCALE (=1000) */
101  FIXP_SGL  gapFillThr;
102  SHORT     minSfbWidth;
103  USHORT    detectionAlgorithmFlags;
104} PNS_INFO_TAB;
105
106
107typedef struct {
108  ULONG brFrom;
109  ULONG brTo;
110  UCHAR S22050;
111  UCHAR S24000;
112  UCHAR S32000;
113  UCHAR S44100;
114  UCHAR S48000;
115} AUTO_PNS_TAB;
116
117static const AUTO_PNS_TAB levelTable_mono[]= {
118  {0,      11999, 1, 1, 1, 1, 1,},
119  {12000,  19999, 1, 1, 1, 1, 1,},
120  {20000,  28999, 2, 1, 1, 1, 1,},
121  {29000,  40999, 4, 4, 4, 2, 2,},
122  {41000,  55999, 9, 9, 7, 7, 7,},
123  {56000,  79999, 0, 0, 0, 9, 9,},
124  {80000,  99999, 0, 0, 0, 0, 0,},
125  {100000,999999, 0, 0, 0, 0, 0,},
126};
127
128static const AUTO_PNS_TAB levelTable_stereo[]= {
129  {0,      11999, 1, 1, 1, 1, 1,},
130  {12000,  19999, 3, 1, 1, 1, 1,},
131  {20000,  28999, 3, 3, 3, 2, 2,},
132  {29000,  40999, 7, 6, 6, 5, 5,},
133  {41000,  55999, 9, 9, 7, 7, 7,},
134  {56000,  79999, 0, 0, 0, 0, 0,},
135  {80000,  99999, 0, 0, 0, 0, 0,},
136  {100000,999999, 0, 0, 0, 0, 0,},
137};
138
139
140static const PNS_INFO_TAB pnsInfoTab[] = {
141/*0   pns off */
142/*1*/ { 4000, FL2FXCONST_SGL(0.04), FL2FXCONST_SGL(0.06), 1150, 1200, FL2FXCONST_SGL(0.02), 8,
143        USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS /*| JUST_LONG_WINDOW*/ },
144/*2*/ { 4000, FL2FXCONST_SGL(0.04), FL2FXCONST_SGL(0.07), 1130, 1300, FL2FXCONST_SGL(0.05), 8,
145        USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS /*| JUST_LONG_WINDOW*/ },
146/*3*/ { 4100, FL2FXCONST_SGL(0.04), FL2FXCONST_SGL(0.07), 1100, 1400, FL2FXCONST_SGL(0.10), 8,
147        USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS /*| JUST_LONG_WINDOW*/ },
148/*4*/ { 4100, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.10), 1100, 1400, FL2FXCONST_SGL(0.15), 8,
149        USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS /*| JUST_LONG_WINDOW*/ },
150/*5*/ { 4300, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.10), 1100, 1400, FL2FXCONST_SGL(0.15), 8,
151        USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
152/*6*/ { 5000, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.10), 1100, 1400, FL2FXCONST_SGL(0.25), 8,
153        USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
154/*7*/ { 5500, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.12), 1100, 1400, FL2FXCONST_SGL(0.35), 8,
155        USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
156/*8*/ { 6000, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.12), 1080, 1400, FL2FXCONST_SGL(0.40), 8,
157        USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
158/*9*/ { 6000, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.14), 1070, 1400, FL2FXCONST_SGL(0.45), 8,
159        USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
160};
161
162static const AUTO_PNS_TAB levelTable_lowComplexity[]= {
163  {0,      27999, 0, 0, 0, 0, 0,},
164  {28000,  31999, 2, 2, 2, 2, 2,},
165  {32000,  47999, 3, 3, 3, 3, 3,},
166  {48000,  48000, 4, 4, 4, 4, 4,},
167  {48001, 999999, 0, 0, 0, 0, 0,},
168};
169
170/* conversion of old LC tuning tables to new (LD enc) structure (only entries which are actually used were converted) */
171static const PNS_INFO_TAB pnsInfoTab_lowComplexity[] = {
172/*0   pns off */
173      /* DEFAULT parameter set */
174/*1*/ { 4100, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.16), 1100, 1400, FL2FXCONST_SGL(0.5), 16,
175        USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
176/*2*/ { 4100, FL2FXCONST_SGL(0.05), FL2FXCONST_SGL(0.10), 1410, 1400, FL2FXCONST_SGL(0.5), 16,
177        USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
178/*3*/ { 4100, FL2FXCONST_SGL(0.05), FL2FXCONST_SGL(0.10), 1100, 1400, FL2FXCONST_SGL(0.5), 16,
179        USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
180      /* LOWSUBST -> PNS is used less often than with DEFAULT parameter set (for br: 48000 - 79999) */
181/*4*/ { 4100, FL2FXCONST_SGL(0.20), FL2FXCONST_SGL(0.10), 1410, 1400, FL2FXCONST_SGL(0.5), 16,
182        USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
183};
184
185/****************************************************************************
186  function to look up used pns level
187****************************************************************************/
188int FDKaacEnc_lookUpPnsUse (int bitRate, int sampleRate, int numChan, const int isLC) {
189
190  int hUsePns=0, size, i;
191  const AUTO_PNS_TAB *levelTable;
192
193  if (isLC) {
194    levelTable = &levelTable_lowComplexity[0];
195    size = sizeof(levelTable_lowComplexity);
196  } else
197  { /* (E)LD */
198    levelTable = (numChan > 1) ? &levelTable_stereo[0] : &levelTable_mono[0];
199    size = (numChan > 1) ? sizeof(levelTable_stereo) : sizeof(levelTable_mono);
200  }
201
202  for(i = 0; i < (int) (size/sizeof(AUTO_PNS_TAB)); i++) {
203    if(((ULONG)bitRate >= levelTable[i].brFrom) &&
204       ((ULONG)bitRate <= levelTable[i].brTo) )
205      break;
206  }
207
208  /* sanity check */
209  if ((int)(sizeof(pnsInfoTab)/sizeof(PNS_INFO_TAB)) < i ) {
210    return (PNS_TABLE_ERROR);
211  }
212
213  switch (sampleRate) {
214  case 22050: hUsePns = levelTable[i].S22050; break;
215  case 24000: hUsePns = levelTable[i].S24000; break;
216  case 32000: hUsePns = levelTable[i].S32000; break;
217  case 44100: hUsePns = levelTable[i].S44100; break;
218  case 48000: hUsePns = levelTable[i].S48000; break;
219  default:
220    if (isLC) {
221      hUsePns = levelTable[i].S48000;
222    }
223    break;
224  }
225
226  return (hUsePns);
227}
228
229
230/*****************************************************************************
231
232    functionname: FDKaacEnc_GetPnsParam
233    description:  Gets PNS parameters depending on bitrate and bandwidth
234    returns:      error status
235    input:        Noiseparams struct, bitrate, sampling rate,
236                  number of sfb's, pointer to sfb offset
237    output:       PNS parameters
238
239*****************************************************************************/
240AAC_ENCODER_ERROR FDKaacEnc_GetPnsParam(NOISEPARAMS *np,
241                                        INT         bitRate,
242                                        INT         sampleRate,
243                                        INT         sfbCnt,
244                                        const INT   *sfbOffset,
245                                        INT         *usePns,
246                                        INT         numChan,
247                                        const int   isLC)
248
249{
250    int i, hUsePns;
251    const PNS_INFO_TAB *pnsInfo;
252
253    if (isLC) {
254      np->detectionAlgorithmFlags = IS_LOW_COMLEXITY;
255      pnsInfo = pnsInfoTab_lowComplexity;
256    }
257    else
258    {
259      np->detectionAlgorithmFlags = 0;
260      pnsInfo = pnsInfoTab;
261    }
262
263    if (*usePns<=0)
264        return AAC_ENC_OK;
265
266    /* new pns params */
267    hUsePns = FDKaacEnc_lookUpPnsUse (bitRate, sampleRate, numChan, isLC);
268    if (hUsePns == 0) {
269      *usePns = 0;
270        return AAC_ENC_OK;
271        }
272    if (hUsePns == PNS_TABLE_ERROR)
273      return AAC_ENC_PNS_TABLE_ERROR;
274
275    /* select correct row of tuning table */
276    pnsInfo += hUsePns-1;
277
278    np->startSfb = FDKaacEnc_FreqToBandWithRounding( pnsInfo->startFreq,
279        sampleRate,
280        sfbCnt,
281        sfbOffset );
282
283    np->detectionAlgorithmFlags |= pnsInfo->detectionAlgorithmFlags;
284
285    np->refPower             = FX_SGL2FX_DBL(pnsInfo->refPower);
286    np->refTonality          = FX_SGL2FX_DBL(pnsInfo->refTonality);
287    np->tnsGainThreshold     = pnsInfo->tnsGainThreshold;
288    np->tnsPNSGainThreshold  = pnsInfo->tnsPNSGainThreshold;
289    np->minSfbWidth          = pnsInfo->minSfbWidth;
290
291    np->gapFillThr           = (FIXP_SGL)pnsInfo->gapFillThr;
292
293    /* assuming a constant dB/Hz slope in the signal's PSD curve,
294    the detection threshold needs to be corrected for the width of the band */
295    for ( i = 0; i < (sfbCnt-1); i++)
296    {
297        INT qtmp, sfbWidth;
298        FIXP_DBL tmp;
299
300        sfbWidth = sfbOffset[i+1]-sfbOffset[i];
301
302        tmp = fPow(np->refPower, 0, sfbWidth, DFRACT_BITS-1-5, &qtmp);
303        np->powDistPSDcurve[i] = (FIXP_SGL)((LONG)(scaleValue(tmp, qtmp) >> 16));
304    }
305    np->powDistPSDcurve[sfbCnt] = np->powDistPSDcurve[sfbCnt-1];
306
307  return AAC_ENC_OK;
308}
309