Imported Debian version 0.1.3.1
[deb_fdk-aac.git] / libSBRdec / src / transcendent.h
1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software 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
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28
29 2. COPYRIGHT LICENSE
30
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and 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
53 3. NO PATENT LICENSE
54
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61
62 4. DISCLAIMER
63
64 This 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
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72
73 5. CONTACT INFORMATION
74
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83
84 /*!
85 \file
86 \brief FDK Fixed Point Arithmetic Library Interface
87 */
88
89 #ifndef __TRANSCENDENT_H
90 #define __TRANSCENDENT_H
91
92 #include "sbrdecoder.h"
93 #include "sbr_rom.h"
94
95 /************************************************************************/
96 /*!
97 \brief Get number of octaves between frequencies a and b
98
99 The Result is scaled with 1/8.
100 The valid range for a and b is 1 to LOG_DUALIS_TABLE_SIZE.
101
102 \return ld(a/b) / 8
103 */
104 /************************************************************************/
105 static inline FIXP_SGL FDK_getNumOctavesDiv8(INT a, /*!< lower band */
106 INT b) /*!< upper band */
107 {
108 return ( (SHORT)((LONG)(CalcLdInt(b) - CalcLdInt(a))>>(FRACT_BITS-3)) );
109 }
110
111
112 /************************************************************************/
113 /*!
114 \brief Add two values given by mantissa and exponent.
115
116 Mantissas are in fract format with values between 0 and 1. <br>
117 The base for exponents is 2. Example: \f$ a = a\_m * 2^{a\_e} \f$<br>
118 */
119 /************************************************************************/
120 inline void FDK_add_MantExp(FIXP_SGL a_m, /*!< Mantissa of 1st operand a */
121 SCHAR a_e, /*!< Exponent of 1st operand a */
122 FIXP_SGL b_m, /*!< Mantissa of 2nd operand b */
123 SCHAR b_e, /*!< Exponent of 2nd operand b */
124 FIXP_SGL *ptrSum_m, /*!< Mantissa of result */
125 SCHAR *ptrSum_e) /*!< Exponent of result */
126 {
127 FIXP_DBL accu;
128 int shift;
129 int shiftAbs;
130
131 FIXP_DBL shiftedMantissa;
132 FIXP_DBL otherMantissa;
133
134 /* Equalize exponents of the summands.
135 For the smaller summand, the exponent is adapted and
136 for compensation, the mantissa is shifted right. */
137
138 shift = (int)(a_e - b_e);
139
140 shiftAbs = (shift>0)? shift : -shift;
141 shiftAbs = (shiftAbs < DFRACT_BITS-1)? shiftAbs : DFRACT_BITS-1;
142 shiftedMantissa = (shift>0)? (FX_SGL2FX_DBL(b_m) >> shiftAbs) : (FX_SGL2FX_DBL(a_m) >> shiftAbs);
143 otherMantissa = (shift>0)? FX_SGL2FX_DBL(a_m) : FX_SGL2FX_DBL(b_m);
144 *ptrSum_e = (shift>0)? a_e : b_e;
145
146 accu = (shiftedMantissa >> 1) + (otherMantissa >> 1);
147 /* shift by 1 bit to avoid overflow */
148
149 if ( (accu >= (FL2FXCONST_DBL(0.5f) - (FIXP_DBL)1)) || (accu <= FL2FXCONST_DBL(-0.5f)) )
150 *ptrSum_e += 1;
151 else
152 accu = (shiftedMantissa + otherMantissa);
153
154 *ptrSum_m = FX_DBL2FX_SGL(accu);
155
156 }
157
158 inline void FDK_add_MantExp(FIXP_DBL a, /*!< Mantissa of 1st operand a */
159 SCHAR a_e, /*!< Exponent of 1st operand a */
160 FIXP_DBL b, /*!< Mantissa of 2nd operand b */
161 SCHAR b_e, /*!< Exponent of 2nd operand b */
162 FIXP_DBL *ptrSum, /*!< Mantissa of result */
163 SCHAR *ptrSum_e) /*!< Exponent of result */
164 {
165 FIXP_DBL accu;
166 int shift;
167 int shiftAbs;
168
169 FIXP_DBL shiftedMantissa;
170 FIXP_DBL otherMantissa;
171
172 /* Equalize exponents of the summands.
173 For the smaller summand, the exponent is adapted and
174 for compensation, the mantissa is shifted right. */
175
176 shift = (int)(a_e - b_e);
177
178 shiftAbs = (shift>0)? shift : -shift;
179 shiftAbs = (shiftAbs < DFRACT_BITS-1)? shiftAbs : DFRACT_BITS-1;
180 shiftedMantissa = (shift>0)? (b >> shiftAbs) : (a >> shiftAbs);
181 otherMantissa = (shift>0)? a : b;
182 *ptrSum_e = (shift>0)? a_e : b_e;
183
184 accu = (shiftedMantissa >> 1) + (otherMantissa >> 1);
185 /* shift by 1 bit to avoid overflow */
186
187 if ( (accu >= (FL2FXCONST_DBL(0.5f) - (FIXP_DBL)1)) || (accu <= FL2FXCONST_DBL(-0.5f)) )
188 *ptrSum_e += 1;
189 else
190 accu = (shiftedMantissa + otherMantissa);
191
192 *ptrSum = accu;
193
194 }
195
196 /************************************************************************/
197 /*!
198 \brief Divide two values given by mantissa and exponent.
199
200 Mantissas are in fract format with values between 0 and 1. <br>
201 The base for exponents is 2. Example: \f$ a = a\_m * 2^{a\_e} \f$<br>
202
203 For performance reasons, the division is based on a table lookup
204 which limits accuracy.
205 */
206 /************************************************************************/
207 static inline void FDK_divide_MantExp(FIXP_SGL a_m, /*!< Mantissa of dividend a */
208 SCHAR a_e, /*!< Exponent of dividend a */
209 FIXP_SGL b_m, /*!< Mantissa of divisor b */
210 SCHAR b_e, /*!< Exponent of divisor b */
211 FIXP_SGL *ptrResult_m, /*!< Mantissa of quotient a/b */
212 SCHAR *ptrResult_e) /*!< Exponent of quotient a/b */
213
214 {
215 int preShift, postShift, index, shift;
216 FIXP_DBL ratio_m;
217 FIXP_SGL bInv_m = FL2FXCONST_SGL(0.0f);
218
219 preShift = CntLeadingZeros(FX_SGL2FX_DBL(b_m));
220
221 /*
222 Shift b into the range from 0..INV_TABLE_SIZE-1,
223
224 E.g. 10 bits must be skipped for INV_TABLE_BITS 8:
225 - leave 8 bits as index for table
226 - skip sign bit,
227 - skip first bit of mantissa, because this is always the same (>0.5)
228
229 We are dealing with energies, so we need not care
230 about negative numbers
231 */
232
233 /*
234 The first interval has half width so the lowest bit of the index is
235 needed for a doubled resolution.
236 */
237 shift = (FRACT_BITS - 2 - INV_TABLE_BITS - preShift);
238
239 index = (shift<0)? (LONG)b_m << (-shift) : (LONG)b_m >> shift;
240
241
242 /* The index has INV_TABLE_BITS +1 valid bits here. Clear the other bits. */
243 index &= (1 << (INV_TABLE_BITS+1)) - 1;
244
245 /* Remove offset of half an interval */
246 index--;
247
248 /* Now the lowest bit is shifted out */
249 index = index >> 1;
250
251 /* Fetch inversed mantissa from table: */
252 bInv_m = (index<0)? bInv_m : FDK_sbrDecoder_invTable[index];
253
254 /* Multiply a with the inverse of b: */
255 ratio_m = (index<0)? FX_SGL2FX_DBL(a_m >> 1) : fMultDiv2(bInv_m,a_m);
256
257 postShift = CntLeadingZeros(ratio_m)-1;
258
259 *ptrResult_m = FX_DBL2FX_SGL(ratio_m << postShift);
260 *ptrResult_e = a_e - b_e + 1 + preShift - postShift;
261 }
262
263 static inline void FDK_divide_MantExp(FIXP_DBL a_m, /*!< Mantissa of dividend a */
264 SCHAR a_e, /*!< Exponent of dividend a */
265 FIXP_DBL b_m, /*!< Mantissa of divisor b */
266 SCHAR b_e, /*!< Exponent of divisor b */
267 FIXP_DBL *ptrResult_m, /*!< Mantissa of quotient a/b */
268 SCHAR *ptrResult_e) /*!< Exponent of quotient a/b */
269
270 {
271 int preShift, postShift, index, shift;
272 FIXP_DBL ratio_m;
273 FIXP_SGL bInv_m = FL2FXCONST_SGL(0.0f);
274
275 preShift = CntLeadingZeros(b_m);
276
277 /*
278 Shift b into the range from 0..INV_TABLE_SIZE-1,
279
280 E.g. 10 bits must be skipped for INV_TABLE_BITS 8:
281 - leave 8 bits as index for table
282 - skip sign bit,
283 - skip first bit of mantissa, because this is always the same (>0.5)
284
285 We are dealing with energies, so we need not care
286 about negative numbers
287 */
288
289 /*
290 The first interval has half width so the lowest bit of the index is
291 needed for a doubled resolution.
292 */
293 shift = (DFRACT_BITS - 2 - INV_TABLE_BITS - preShift);
294
295 index = (shift<0)? (LONG)b_m << (-shift) : (LONG)b_m >> shift;
296
297
298 /* The index has INV_TABLE_BITS +1 valid bits here. Clear the other bits. */
299 index &= (1 << (INV_TABLE_BITS+1)) - 1;
300
301 /* Remove offset of half an interval */
302 index--;
303
304 /* Now the lowest bit is shifted out */
305 index = index >> 1;
306
307 /* Fetch inversed mantissa from table: */
308 bInv_m = (index<0)? bInv_m : FDK_sbrDecoder_invTable[index];
309
310 /* Multiply a with the inverse of b: */
311 ratio_m = (index<0)? (a_m >> 1) : fMultDiv2(bInv_m,a_m);
312
313 postShift = CntLeadingZeros(ratio_m)-1;
314
315 *ptrResult_m = ratio_m << postShift;
316 *ptrResult_e = a_e - b_e + 1 + preShift - postShift;
317 }
318
319 /*!
320 \brief Calculate the squareroot of a number given by mantissa and exponent
321
322 Mantissa is in fract format with values between 0 and 1. <br>
323 The base for the exponent is 2. Example: \f$ a = a\_m * 2^{a\_e} \f$<br>
324 The operand is addressed via pointers and will be overwritten with the result.
325
326 For performance reasons, the square root is based on a table lookup
327 which limits accuracy.
328 */
329 static inline void FDK_sqrt_MantExp(FIXP_DBL *mantissa, /*!< Pointer to mantissa */
330 SCHAR *exponent,
331 const SCHAR *destScale)
332 {
333 FIXP_DBL input_m = *mantissa;
334 int input_e = (int) *exponent;
335 FIXP_DBL result = FL2FXCONST_DBL(0.0f);
336 int result_e = -FRACT_BITS;
337
338 /* Call lookup square root, which does internally normalization. */
339 result = sqrtFixp_lookup(input_m, &input_e);
340 result_e = input_e;
341
342 /* Write result */
343 if (exponent==destScale) {
344 *mantissa = result;
345 *exponent = result_e;
346 } else {
347 int shift = result_e - *destScale;
348 *mantissa = (shift>=0) ? result << (INT)fixMin(DFRACT_BITS-1,shift)
349 : result >> (INT)fixMin(DFRACT_BITS-1,-shift);
350 *exponent = *destScale;
351 }
352 }
353
354
355 #endif