1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3 
4 © Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten
5 Forschung e.V. All rights reserved.
6 
7  1.    INTRODUCTION
8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
11 a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14 general perceptual audio codecs. AAC-ELD is considered the best-performing
15 full-bandwidth communications codec by independent studies and is widely
16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17 specifications.
18 
19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
20 those of Fraunhofer) may be obtained through Via Licensing
21 (www.vialicensing.com) or through the respective patent owners individually for
22 the purpose of encoding or decoding bit streams in products that are compliant
23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24 Android devices already license these patent claims through Via Licensing or
25 directly from the patent owners, and therefore FDK AAC Codec software may
26 already be covered under those patent licenses when it is used for those
27 licensed purposes only.
28 
29 Commercially-licensed AAC software libraries, including floating-point versions
30 with enhanced sound quality, are also available from Fraunhofer. Users are
31 encouraged to check the Fraunhofer website for additional applications
32 information and documentation.
33 
34 2.    COPYRIGHT LICENSE
35 
36 Redistribution and use in source and binary forms, with or without modification,
37 are permitted without payment of copyright license fees provided that you
38 satisfy the following conditions:
39 
40 You must retain the complete text of this software license in redistributions of
41 the FDK AAC Codec or your modifications thereto in source code form.
42 
43 You must retain the complete text of this software license in the documentation
44 and/or other materials provided with redistributions of the FDK AAC Codec or
45 your modifications thereto in binary form. You must make available free of
46 charge copies of the complete source code of the FDK AAC Codec and your
47 modifications thereto to recipients of copies in binary form.
48 
49 The name of Fraunhofer may not be used to endorse or promote products derived
50 from this library without prior written permission.
51 
52 You may not charge copyright license fees for anyone to use, copy or distribute
53 the FDK AAC Codec software or your modifications thereto.
54 
55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
56 that you changed the software and the date of any change. For modified versions
57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59 AAC Codec Library for Android."
60 
61 3.    NO PATENT LICENSE
62 
63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65 Fraunhofer provides no warranty of patent non-infringement with respect to this
66 software.
67 
68 You may use this FDK AAC Codec software or modifications thereto only for
69 purposes that are authorized by appropriate patent licenses.
70 
71 4.    DISCLAIMER
72 
73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75 including but not limited to the implied warranties of merchantability and
76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78 or consequential damages, including but not limited to procurement of substitute
79 goods or services; loss of use, data, or profits, or business interruption,
80 however caused and on any theory of liability, whether in contract, strict
81 liability, or tort (including negligence), arising in any way out of the use of
82 this software, even if advised of the possibility of such damage.
83 
84 5.    CONTACT INFORMATION
85 
86 Fraunhofer Institute for Integrated Circuits IIS
87 Attention: Audio and Multimedia Departments - FDK AAC LL
88 Am Wolfsmantel 33
89 91058 Erlangen, Germany
90 
91 www.iis.fraunhofer.de/amm
92 amm-info@iis.fraunhofer.de
93 ----------------------------------------------------------------------------- */
94 
95 /**************************** AAC encoder library ******************************
96 
97    Author(s):   M.Lohwasser
98 
99    Description: PNS parameters depending on bitrate and bandwidth
100 
101 *******************************************************************************/
102 
103 #include "pnsparam.h"
104 
105 #include "psy_configuration.h"
106 
107 typedef struct {
108   SHORT startFreq;
109   /* Parameters for detection */
110   FIXP_SGL refPower;
111   FIXP_SGL refTonality;
112   SHORT tnsGainThreshold;    /* scaled by TNS_PREDGAIN_SCALE (=1000) */
113   SHORT tnsPNSGainThreshold; /* scaled by TNS_PREDGAIN_SCALE (=1000) */
114   FIXP_SGL gapFillThr;
115   SHORT minSfbWidth;
116   USHORT detectionAlgorithmFlags;
117 } PNS_INFO_TAB;
118 
119 typedef struct {
120   ULONG brFrom;
121   ULONG brTo;
122   UCHAR S16000;
123   UCHAR S22050;
124   UCHAR S24000;
125   UCHAR S32000;
126   UCHAR S44100;
127   UCHAR S48000;
128 } AUTO_PNS_TAB;
129 
130 static const AUTO_PNS_TAB levelTable_mono[] = {
131     {
132         0,
133         11999,
134         0,
135         1,
136         1,
137         1,
138         1,
139         1,
140     },
141     {
142         12000,
143         19999,
144         0,
145         1,
146         1,
147         1,
148         1,
149         1,
150     },
151     {
152         20000,
153         28999,
154         0,
155         2,
156         1,
157         1,
158         1,
159         1,
160     },
161     {
162         29000,
163         40999,
164         0,
165         4,
166         4,
167         4,
168         2,
169         2,
170     },
171     {
172         41000,
173         55999,
174         0,
175         9,
176         9,
177         7,
178         7,
179         7,
180     },
181     {
182         56000,
183         61999,
184         0,
185         0,
186         0,
187         0,
188         9,
189         9,
190     },
191     {
192         62000,
193         75999,
194         0,
195         0,
196         0,
197         0,
198         0,
199         0,
200     },
201     {
202         76000,
203         92999,
204         0,
205         0,
206         0,
207         0,
208         0,
209         0,
210     },
211     {
212         93000,
213         999999,
214         0,
215         0,
216         0,
217         0,
218         0,
219         0,
220     },
221 };
222 
223 static const AUTO_PNS_TAB levelTable_stereo[] = {
224     {
225         0,
226         11999,
227         0,
228         1,
229         1,
230         1,
231         1,
232         1,
233     },
234     {
235         12000,
236         19999,
237         0,
238         3,
239         1,
240         1,
241         1,
242         1,
243     },
244     {
245         20000,
246         28999,
247         0,
248         3,
249         3,
250         3,
251         2,
252         2,
253     },
254     {
255         29000,
256         40999,
257         0,
258         7,
259         6,
260         6,
261         5,
262         5,
263     },
264     {
265         41000,
266         55999,
267         0,
268         9,
269         9,
270         7,
271         7,
272         7,
273     },
274     {
275         56000,
276         79999,
277         0,
278         0,
279         0,
280         0,
281         0,
282         0,
283     },
284     {
285         80000,
286         99999,
287         0,
288         0,
289         0,
290         0,
291         0,
292         0,
293     },
294     {
295         100000,
296         999999,
297         0,
298         0,
299         0,
300         0,
301         0,
302         0,
303     },
304 };
305 
306 static const PNS_INFO_TAB pnsInfoTab[] = {
307     /*0   pns off */
308     /*1*/ {4000, FL2FXCONST_SGL(0.04), FL2FXCONST_SGL(0.06), 1150, 1200,
309            FL2FXCONST_SGL(0.02), 8,
310            USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR |
311                USE_TNS_PNS /*| JUST_LONG_WINDOW*/},
312     /*2*/
313     {4000, FL2FXCONST_SGL(0.04), FL2FXCONST_SGL(0.07), 1130, 1300,
314      FL2FXCONST_SGL(0.05), 8,
315      USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR |
316          USE_TNS_PNS /*| JUST_LONG_WINDOW*/},
317     /*3*/
318     {4100, FL2FXCONST_SGL(0.04), FL2FXCONST_SGL(0.07), 1100, 1400,
319      FL2FXCONST_SGL(0.10), 8,
320      USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR |
321          USE_TNS_PNS /*| JUST_LONG_WINDOW*/},
322     /*4*/
323     {4100, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.10), 1100, 1400,
324      FL2FXCONST_SGL(0.15), 8,
325      USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR |
326          USE_TNS_PNS /*| JUST_LONG_WINDOW*/},
327     /*5*/
328     {4300, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.10), 1100, 1400,
329      FL2FXCONST_SGL(0.15), 8,
330      USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR |
331          USE_TNS_PNS | JUST_LONG_WINDOW},
332     /*6*/
333     {5000, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.10), 1100, 1400,
334      FL2FXCONST_SGL(0.25), 8,
335      USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR |
336          USE_TNS_PNS | JUST_LONG_WINDOW},
337     /*7*/
338     {5500, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.12), 1100, 1400,
339      FL2FXCONST_SGL(0.35), 8,
340      USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR |
341          USE_TNS_PNS | JUST_LONG_WINDOW},
342     /*8*/
343     {6000, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.12), 1080, 1400,
344      FL2FXCONST_SGL(0.40), 8,
345      USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR |
346          USE_TNS_PNS | JUST_LONG_WINDOW},
347     /*9*/
348     {6000, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.14), 1070, 1400,
349      FL2FXCONST_SGL(0.45), 8,
350      USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR |
351          USE_TNS_PNS | JUST_LONG_WINDOW},
352 };
353 
354 static const AUTO_PNS_TAB levelTable_lowComplexity[] = {
355     {
356         0,
357         27999,
358         0,
359         0,
360         0,
361         0,
362         0,
363         0,
364     },
365     {
366         28000,
367         31999,
368         0,
369         2,
370         2,
371         2,
372         2,
373         2,
374     },
375     {
376         32000,
377         47999,
378         0,
379         3,
380         3,
381         3,
382         3,
383         3,
384     },
385     {
386         48000,
387         48000,
388         0,
389         4,
390         4,
391         4,
392         4,
393         4,
394     },
395     {
396         48001,
397         999999,
398         0,
399         0,
400         0,
401         0,
402         0,
403         0,
404     },
405 };
406 /* conversion of old LC tuning tables to new (LD enc) structure (only entries
407  * which are actually used were converted) */
408 static const PNS_INFO_TAB pnsInfoTab_lowComplexity[] = {
409     /*0   pns off */
410     /* DEFAULT parameter set */
411     /*1*/ {4100, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.16), 1100, 1400,
412            FL2FXCONST_SGL(0.5), 16,
413            USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR |
414                USE_TNS_PNS | JUST_LONG_WINDOW},
415     /*2*/
416     {4100, FL2FXCONST_SGL(0.05), FL2FXCONST_SGL(0.10), 1410, 1400,
417      FL2FXCONST_SGL(0.5), 16,
418      USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR |
419          USE_TNS_PNS | JUST_LONG_WINDOW},
420     /*3*/
421     {4100, FL2FXCONST_SGL(0.05), FL2FXCONST_SGL(0.10), 1100, 1400,
422      FL2FXCONST_SGL(0.5), 16,
423      USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR |
424          USE_TNS_PNS | JUST_LONG_WINDOW},
425     /* LOWSUBST -> PNS is used less often than with DEFAULT parameter set (for
426        br: 48000 - 79999) */
427     /*4*/
428     {4100, FL2FXCONST_SGL(0.20), FL2FXCONST_SGL(0.10), 1410, 1400,
429      FL2FXCONST_SGL(0.5), 16,
430      USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR |
431          USE_TNS_PNS | JUST_LONG_WINDOW},
432 };
433 
434 /****************************************************************************
435   function to look up used pns level
436 ****************************************************************************/
FDKaacEnc_lookUpPnsUse(int bitRate,int sampleRate,int numChan,const int isLC)437 int FDKaacEnc_lookUpPnsUse(int bitRate, int sampleRate, int numChan,
438                            const int isLC) {
439   int hUsePns = 0, size, i;
440   const AUTO_PNS_TAB *levelTable;
441 
442   if (isLC) {
443     levelTable = &levelTable_lowComplexity[0];
444     size = sizeof(levelTable_lowComplexity);
445   } else { /* (E)LD */
446     levelTable = (numChan > 1) ? &levelTable_stereo[0] : &levelTable_mono[0];
447     size = (numChan > 1) ? sizeof(levelTable_stereo) : sizeof(levelTable_mono);
448   }
449 
450   for (i = 0; i < (int)(size / sizeof(AUTO_PNS_TAB)); i++) {
451     if (((ULONG)bitRate >= levelTable[i].brFrom) &&
452         ((ULONG)bitRate <= levelTable[i].brTo))
453       break;
454   }
455 
456   /* sanity check */
457   if ((int)(sizeof(pnsInfoTab) / sizeof(PNS_INFO_TAB)) < i) {
458     return (PNS_TABLE_ERROR);
459   }
460 
461   switch (sampleRate) {
462     case 16000:
463       hUsePns = levelTable[i].S16000;
464       break;
465     case 22050:
466       hUsePns = levelTable[i].S22050;
467       break;
468     case 24000:
469       hUsePns = levelTable[i].S24000;
470       break;
471     case 32000:
472       hUsePns = levelTable[i].S32000;
473       break;
474     case 44100:
475       hUsePns = levelTable[i].S44100;
476       break;
477     case 48000:
478       hUsePns = levelTable[i].S48000;
479       break;
480     default:
481       if (isLC) {
482         hUsePns = levelTable[i].S48000;
483       }
484       break;
485   }
486 
487   return (hUsePns);
488 }
489 
490 /*****************************************************************************
491 
492     functionname: FDKaacEnc_GetPnsParam
493     description:  Gets PNS parameters depending on bitrate and bandwidth or
494                     bitsPerLine
495     returns:      error status
496     input:        Noiseparams struct, bitrate, sampling rate,
497                   number of sfb's, pointer to sfb offset
498     output:       PNS parameters
499 
500 *****************************************************************************/
FDKaacEnc_GetPnsParam(NOISEPARAMS * np,INT bitRate,INT sampleRate,INT sfbCnt,const INT * sfbOffset,INT * usePns,INT numChan,const INT isLC)501 AAC_ENCODER_ERROR FDKaacEnc_GetPnsParam(NOISEPARAMS *np, INT bitRate,
502                                         INT sampleRate, INT sfbCnt,
503                                         const INT *sfbOffset, INT *usePns,
504                                         INT numChan, const INT isLC) {
505   int i, hUsePns;
506   const PNS_INFO_TAB *pnsInfo;
507 
508   if (*usePns <= 0) return AAC_ENC_OK;
509 
510   if (isLC) {
511     np->detectionAlgorithmFlags = IS_LOW_COMPLEXITY;
512 
513     pnsInfo = pnsInfoTab_lowComplexity;
514 
515     /* new pns params */
516     hUsePns = FDKaacEnc_lookUpPnsUse(bitRate, sampleRate, numChan, isLC);
517     if (hUsePns == 0) {
518       *usePns = 0;
519       return AAC_ENC_OK;
520     }
521 
522     if (hUsePns == PNS_TABLE_ERROR) {
523       return AAC_ENC_PNS_TABLE_ERROR;
524     }
525 
526     /* select correct row of tuning table */
527     pnsInfo += hUsePns - 1;
528 
529   } else {
530     np->detectionAlgorithmFlags = 0;
531     pnsInfo = pnsInfoTab;
532 
533     /* new pns params */
534     hUsePns = FDKaacEnc_lookUpPnsUse(bitRate, sampleRate, numChan, isLC);
535     if (hUsePns == 0) {
536       *usePns = 0;
537       return AAC_ENC_OK;
538     }
539     if (hUsePns == PNS_TABLE_ERROR) return AAC_ENC_PNS_TABLE_ERROR;
540 
541     /* select correct row of tuning table */
542     pnsInfo += hUsePns - 1;
543   }
544 
545   np->startSfb = FDKaacEnc_FreqToBandWidthRounding(
546       pnsInfo->startFreq, sampleRate, sfbCnt, sfbOffset);
547 
548   np->detectionAlgorithmFlags |= pnsInfo->detectionAlgorithmFlags;
549 
550   np->refPower = FX_SGL2FX_DBL(pnsInfo->refPower);
551   np->refTonality = FX_SGL2FX_DBL(pnsInfo->refTonality);
552   np->tnsGainThreshold = pnsInfo->tnsGainThreshold;
553   np->tnsPNSGainThreshold = pnsInfo->tnsPNSGainThreshold;
554   np->minSfbWidth = pnsInfo->minSfbWidth;
555 
556   np->gapFillThr =
557       pnsInfo->gapFillThr; /* for LC it is always FL2FXCONST_SGL(0.5) */
558 
559   /* assuming a constant dB/Hz slope in the signal's PSD curve,
560     the detection threshold needs to be corrected for the width of the band */
561 
562   for (i = 0; i < (sfbCnt - 1); i++) {
563     INT qtmp, sfbWidth;
564     FIXP_DBL tmp;
565 
566     sfbWidth = sfbOffset[i + 1] - sfbOffset[i];
567 
568     tmp = fPow(np->refPower, 0, sfbWidth, DFRACT_BITS - 1 - 5, &qtmp);
569     np->powDistPSDcurve[i] = (FIXP_SGL)((LONG)(scaleValue(tmp, qtmp) >> 16));
570   }
571   np->powDistPSDcurve[sfbCnt] = np->powDistPSDcurve[sfbCnt - 1];
572 
573   return AAC_ENC_OK;
574 }
575