• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*********************** MPEG surround decoder library *************************
96 
97    Author(s):   Matthias Hildenbrand
98 
99    Description: USAC MPS212 Transient Steering Decorrelator (TSD)
100 
101 *******************************************************************************/
102 
103 #include "sac_tsd.h"
104 
105 #define TSD_START_BAND (7)
106 #define SIZE_S (4)
107 #define SIZE_C (5)
108 
109 /*** Tables ***/
110 RAM_ALIGN
111 LNK_SECTION_CONSTDATA
112 static const UCHAR nBitsTsdCW_32slots[32] = {
113     5,  9,  13, 16, 18, 20, 22, 24, 25, 26, 27, 28, 29, 29, 30, 30,
114     30, 29, 29, 28, 27, 26, 25, 24, 22, 20, 18, 16, 13, 9,  5,  0};
115 
116 RAM_ALIGN
117 LNK_SECTION_CONSTDATA
118 static const UCHAR nBitsTsdCW_64slots[64] = {
119     6,  11, 16, 20, 23, 27, 30, 33, 35, 38, 40, 42, 44, 46, 48, 49,
120     51, 52, 53, 55, 56, 57, 58, 58, 59, 60, 60, 60, 61, 61, 61, 61,
121     61, 61, 61, 60, 60, 60, 59, 58, 58, 57, 56, 55, 53, 52, 51, 49,
122     48, 46, 44, 42, 40, 38, 35, 33, 30, 27, 23, 20, 16, 11, 6,  0};
123 
124 RAM_ALIGN
125 LNK_SECTION_CONSTDATA
126 static const FIXP_STP phiTsd[8] = {
127     STCP(0x7fffffff, 0x00000000), STCP(0x5a82799a, 0x5a82799a),
128     STCP(0x00000000, 0x7fffffff), STCP(0xa57d8666, 0x5a82799a),
129     STCP(0x80000000, 0x00000000), STCP(0xa57d8666, 0xa57d8666),
130     STCP(0x00000000, 0x80000000), STCP(0x5a82799a, 0xa57d8666),
131 };
132 
133 /*** Static Functions ***/
longmult1(USHORT a[],USHORT b,USHORT d[],int len)134 static void longmult1(USHORT a[], USHORT b, USHORT d[], int len) {
135   int k;
136   ULONG tmp;
137   ULONG b0 = (ULONG)b;
138 
139   tmp = ((ULONG)a[0]) * b0;
140   d[0] = (USHORT)tmp;
141 
142   for (k = 1; k < len; k++) {
143     tmp = (tmp >> 16) + ((ULONG)a[k]) * b0;
144     d[k] = (USHORT)tmp;
145   }
146 }
147 
longdiv(USHORT b[],USHORT a,USHORT d[],USHORT * pr,int len)148 static void longdiv(USHORT b[], USHORT a, USHORT d[], USHORT *pr, int len) {
149   ULONG r;
150   ULONG tmp;
151   int k;
152 
153   FDK_ASSERT(a != 0);
154 
155   r = 0;
156 
157   for (k = len - 1; k >= 0; k--) {
158     tmp = ((ULONG)b[k]) + (r << 16);
159 
160     if (tmp) {
161       d[k] = (USHORT)(tmp / a);
162       r = tmp - d[k] * a;
163     } else {
164       d[k] = 0;
165     }
166   }
167   *pr = (USHORT)r;
168 }
169 
longsub(USHORT a[],USHORT b[],int lena,int lenb)170 static void longsub(USHORT a[], USHORT b[], int lena, int lenb) {
171   int h;
172   LONG carry = 0;
173 
174   FDK_ASSERT(lena >= lenb);
175   for (h = 0; h < lenb; h++) {
176     carry += ((LONG)a[h]) - ((LONG)b[h]);
177     a[h] = (USHORT)carry;
178     carry = carry >> 16;
179   }
180 
181   for (; h < lena; h++) {
182     carry = ((LONG)a[h]) + carry;
183     a[h] = (USHORT)carry;
184     carry = carry >> 16;
185   }
186 
187   FDK_ASSERT(carry ==
188              0); /* carry != 0 indicates subtraction underflow, e.g. b > a */
189   return;
190 }
191 
longcompare(USHORT a[],USHORT b[],int len)192 static int longcompare(USHORT a[], USHORT b[], int len) {
193   int i;
194 
195   for (i = len - 1; i > 0; i--) {
196     if (a[i] != b[i]) break;
197   }
198   return (a[i] >= b[i]) ? 1 : 0;
199 }
200 
isTrSlot(const TSD_DATA * pTsdData,const int ts)201 FDK_INLINE int isTrSlot(const TSD_DATA *pTsdData, const int ts) {
202   return (pTsdData->bsTsdTrPhaseData[ts] >= 0);
203 }
204 
205 /*** Public Functions ***/
TsdRead(HANDLE_FDK_BITSTREAM hBs,const int numSlots,TSD_DATA * pTsdData)206 int TsdRead(HANDLE_FDK_BITSTREAM hBs, const int numSlots, TSD_DATA *pTsdData) {
207   int nBitsTrSlots = 0;
208   int bsTsdNumTrSlots;
209   const UCHAR *nBitsTsdCW_tab = NULL;
210 
211   switch (numSlots) {
212     case 32:
213       nBitsTrSlots = 4;
214       nBitsTsdCW_tab = nBitsTsdCW_32slots;
215       break;
216     case 64:
217       nBitsTrSlots = 5;
218       nBitsTsdCW_tab = nBitsTsdCW_64slots;
219       break;
220     default:
221       return 1;
222   }
223 
224   /*** Read TempShapeData for bsTempShapeConfig == 3 ***/
225   pTsdData->bsTsdEnable = FDKreadBit(hBs);
226   if (!pTsdData->bsTsdEnable) {
227     return 0;
228   }
229 
230   /*** Parse/Decode TsdData() ***/
231   pTsdData->numSlots = numSlots;
232 
233   bsTsdNumTrSlots = FDKreadBits(hBs, nBitsTrSlots);
234 
235   /* Decode transient slot positions */
236   {
237     int nBitsTsdCW = (int)nBitsTsdCW_tab[bsTsdNumTrSlots];
238     SCHAR *phaseData = pTsdData->bsTsdTrPhaseData;
239     int p = bsTsdNumTrSlots + 1;
240     int k, h;
241     USHORT s[SIZE_S] = {0};
242     USHORT c[SIZE_C] = {0};
243     USHORT r[1];
244 
245     /* Init with TsdSepData[k] = 0 */
246     for (k = 0; k < numSlots; k++) {
247       phaseData[k] = -1; /* means TsdSepData[] = 0 */
248     }
249 
250     for (h = (SIZE_S - 1); h >= 0; h--) {
251       if (nBitsTsdCW > h * 16) {
252         s[h] = (USHORT)FDKreadBits(hBs, nBitsTsdCW - h * 16);
253         nBitsTsdCW = h * 16;
254       }
255     }
256 
257     /* c = prod_{h=1}^{p} (k-p+h)/h */
258     k = numSlots - 1;
259     c[0] = k - p + 1;
260     for (h = 2; h <= p; h++) {
261       longmult1(c, (k - p + h), c, 5); /* c *= k - p + h; */
262       longdiv(c, h, c, r, 5);          /* c /= h; */
263       FDK_ASSERT(*r == 0);
264     }
265 
266     /* go through all slots */
267     for (; k >= 0; k--) {
268       if (p > k) {
269         for (; k >= 0; k--) {
270           phaseData[k] = 1; /* means TsdSepData[] = 1 */
271         }
272         break;
273       }
274       if (longcompare(s, c, 4)) { /* (s >= c) */
275         longsub(s, c, 4, 4);      /* s -= c; */
276         phaseData[k] = 1;         /* means TsdSepData[] = 1 */
277         if (p == 1) {
278           break;
279         }
280         /* Update c for next iteration: c_new = c_old * p / k */
281         longmult1(c, p, c, 5);
282         p--;
283       } else {
284         /* Update c for next iteration: c_new = c_old * (k-p) / k */
285         longmult1(c, (k - p), c, 5);
286       }
287       longdiv(c, k, c, r, 5);
288       FDK_ASSERT(*r == 0);
289     }
290 
291     /* Read phase data */
292     for (k = 0; k < numSlots; k++) {
293       if (phaseData[k] == 1) {
294         phaseData[k] = FDKreadBits(hBs, 3);
295       }
296     }
297   }
298 
299   return 0;
300 }
301 
TsdGenerateNonTr(const int numHybridBands,const TSD_DATA * pTsdData,const int ts,FIXP_DBL * pVdirectReal,FIXP_DBL * pVdirectImag,FIXP_DBL * pVnonTrReal,FIXP_DBL * pVnonTrImag,FIXP_DBL ** ppDecorrInReal,FIXP_DBL ** ppDecorrInImag)302 void TsdGenerateNonTr(const int numHybridBands, const TSD_DATA *pTsdData,
303                       const int ts, FIXP_DBL *pVdirectReal,
304                       FIXP_DBL *pVdirectImag, FIXP_DBL *pVnonTrReal,
305                       FIXP_DBL *pVnonTrImag, FIXP_DBL **ppDecorrInReal,
306                       FIXP_DBL **ppDecorrInImag) {
307   int k = 0;
308 
309   if (!isTrSlot(pTsdData, ts)) {
310     /* Let allpass based decorrelator read from direct input. */
311     *ppDecorrInReal = pVdirectReal;
312     *ppDecorrInImag = pVdirectImag;
313     return;
314   }
315 
316   /* Generate nonTr input signal for allpass based decorrelator */
317   for (; k < TSD_START_BAND; k++) {
318     pVnonTrReal[k] = pVdirectReal[k];
319     pVnonTrImag[k] = pVdirectImag[k];
320   }
321   for (; k < numHybridBands; k++) {
322     pVnonTrReal[k] = (FIXP_DBL)0;
323     pVnonTrImag[k] = (FIXP_DBL)0;
324   }
325   *ppDecorrInReal = pVnonTrReal;
326   *ppDecorrInImag = pVnonTrImag;
327 }
328 
TsdApply(const int numHybridBands,const TSD_DATA * pTsdData,int * pTsdTs,const FIXP_DBL * pVdirectReal,const FIXP_DBL * pVdirectImag,FIXP_DBL * pDnonTrReal,FIXP_DBL * pDnonTrImag)329 void TsdApply(const int numHybridBands, const TSD_DATA *pTsdData, int *pTsdTs,
330               const FIXP_DBL *pVdirectReal, const FIXP_DBL *pVdirectImag,
331               FIXP_DBL *pDnonTrReal, FIXP_DBL *pDnonTrImag) {
332   const int ts = *pTsdTs;
333 
334   if (isTrSlot(pTsdData, ts)) {
335     int k;
336     const FIXP_STP *phi = &phiTsd[pTsdData->bsTsdTrPhaseData[ts]];
337     FDK_ASSERT((pTsdData->bsTsdTrPhaseData[ts] >= 0) &&
338                (pTsdData->bsTsdTrPhaseData[ts] < 8));
339 
340     /* d = d_nonTr + v_direct * exp(j * bsTsdTrPhaseData[ts]/4 * pi ) */
341     for (k = TSD_START_BAND; k < numHybridBands; k++) {
342       FIXP_DBL tempReal, tempImag;
343       cplxMult(&tempReal, &tempImag, pVdirectReal[k], pVdirectImag[k], *phi);
344       pDnonTrReal[k] += tempReal;
345       pDnonTrImag[k] += tempImag;
346     }
347   }
348 
349   /* The modulo MAX_TSD_TIME_SLOTS operation is to avoid illegal memory accesses
350    * in case of errors. */
351   *pTsdTs = (ts + 1) & (MAX_TSD_TIME_SLOTS - 1);
352   return;
353 }
354