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