1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software 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
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 /*************************** Fraunhofer IIS FDK Tools **********************
85
86 Author(s): M. Gayer
87 Description: Fixed point specific mathematical functions
88
89 ******************************************************************************/
90
91 #ifndef __fixpoint_math_H
92 #define __fixpoint_math_H
93
94
95 #include "common_fix.h"
96
97 #if !defined(FUNCTION_fIsLessThan)
98 /**
99 * \brief Compares two fixpoint values incl. scaling.
100 * \param a_m mantissa of the first input value.
101 * \param a_e exponent of the first input value.
102 * \param b_m mantissa of the second input value.
103 * \param b_e exponent of the second input value.
104 * \return non-zero if (a_m*2^a_e) < (b_m*2^b_e), 0 otherwise
105 */
fIsLessThan(FIXP_DBL a_m,INT a_e,FIXP_DBL b_m,INT b_e)106 FDK_INLINE INT fIsLessThan(FIXP_DBL a_m, INT a_e, FIXP_DBL b_m, INT b_e)
107 {
108 if (a_e > b_e) {
109 return (b_m >> fMin(a_e-b_e, DFRACT_BITS-1) > a_m);
110 } else {
111 return (a_m >> fMin(b_e-a_e, DFRACT_BITS-1) < b_m);
112 }
113 }
114
fIsLessThan(FIXP_SGL a_m,INT a_e,FIXP_SGL b_m,INT b_e)115 FDK_INLINE INT fIsLessThan(FIXP_SGL a_m, INT a_e, FIXP_SGL b_m, INT b_e)
116 {
117 if (a_e > b_e) {
118 return (b_m >> fMin(a_e-b_e, FRACT_BITS-1) > a_m);
119 } else {
120 return (a_m >> fMin(b_e-a_e, FRACT_BITS-1) < b_m);
121 }
122 }
123 #endif
124
125
126
127 #define LD_DATA_SCALING (64.0f)
128 #define LD_DATA_SHIFT 6 /* pow(2, LD_DATA_SHIFT) = LD_DATA_SCALING */
129
130 /**
131 * \brief deprecated. Use fLog2() instead.
132 */
133 FIXP_DBL CalcLdData(FIXP_DBL op);
134
135 void LdDataVector(FIXP_DBL *srcVector, FIXP_DBL *destVector, INT number);
136
137 FIXP_DBL CalcInvLdData(FIXP_DBL op);
138
139
140 void InitLdInt();
141 FIXP_DBL CalcLdInt(INT i);
142
143 extern const USHORT sqrt_tab[49];
144
sqrtFixp_lookup(FIXP_DBL x)145 inline FIXP_DBL sqrtFixp_lookup(FIXP_DBL x)
146 {
147 UINT y = (INT)x;
148 UCHAR is_zero=(y==0);
149 INT zeros=fixnormz_D(y) & 0x1e;
150 y<<=zeros;
151 UINT idx=(y>>26)-16;
152 USHORT frac=(y>>10)&0xffff;
153 USHORT nfrac=0xffff^frac;
154 UINT t=nfrac*sqrt_tab[idx]+frac*sqrt_tab[idx+1];
155 t=t>>(zeros>>1);
156 return(is_zero ? 0 : t);
157 }
158
sqrtFixp_lookup(FIXP_DBL x,INT * x_e)159 inline FIXP_DBL sqrtFixp_lookup(FIXP_DBL x, INT *x_e)
160 {
161 UINT y = (INT)x;
162 INT e;
163
164 if (x == (FIXP_DBL)0) {
165 return x;
166 }
167
168 /* Normalize */
169 e=fixnormz_D(y);
170 y<<=e;
171 e = *x_e - e + 2;
172
173 /* Correct odd exponent. */
174 if (e & 1) {
175 y >>= 1;
176 e ++;
177 }
178 /* Get square root */
179 UINT idx=(y>>26)-16;
180 USHORT frac=(y>>10)&0xffff;
181 USHORT nfrac=0xffff^frac;
182 UINT t=nfrac*sqrt_tab[idx]+frac*sqrt_tab[idx+1];
183
184 /* Write back exponent */
185 *x_e = e >> 1;
186 return (FIXP_DBL)(LONG)(t>>1);
187 }
188
189
190
191 FIXP_DBL sqrtFixp(FIXP_DBL op);
192
193 void InitInvSqrtTab();
194
195 FIXP_DBL invSqrtNorm2(FIXP_DBL op, INT *shift);
196
197 /*****************************************************************************
198
199 functionname: invFixp
200 description: delivers 1/(op)
201
202 *****************************************************************************/
invFixp(FIXP_DBL op)203 inline FIXP_DBL invFixp(FIXP_DBL op)
204 {
205 INT tmp_exp ;
206 FIXP_DBL tmp_inv = invSqrtNorm2(op, &tmp_exp) ;
207 FDK_ASSERT((31-(2*tmp_exp+1))>=0) ;
208 return ( fPow2Div2( (FIXP_DBL)tmp_inv ) >> (31-(2*tmp_exp+1)) ) ;
209 }
210
211
212
213 #if defined(__mips__) && (__GNUC__==2)
214
215 #define FUNCTION_schur_div
schur_div(FIXP_DBL num,FIXP_DBL denum,INT count)216 inline FIXP_DBL schur_div(FIXP_DBL num,FIXP_DBL denum, INT count)
217 {
218 INT result, tmp ;
219 __asm__ ("srl %1, %2, 15\n"
220 "div %3, %1\n" : "=lo" (result)
221 : "%d" (tmp), "d" (denum) , "d" (num)
222 : "hi" ) ;
223 return result<<16 ;
224 }
225
226 /*###########################################################################################*/
227 #elif defined(__mips__) && (__GNUC__==3)
228
229 #define FUNCTION_schur_div
schur_div(FIXP_DBL num,FIXP_DBL denum,INT count)230 inline FIXP_DBL schur_div(FIXP_DBL num,FIXP_DBL denum, INT count)
231 {
232 INT result, tmp;
233
234 __asm__ ("srl %[tmp], %[denum], 15\n"
235 "div %[result], %[num], %[tmp]\n"
236 : [tmp] "+r" (tmp), [result]"=r"(result)
237 : [denum]"r"(denum), [num]"r"(num)
238 : "hi", "lo");
239 return result << (DFRACT_BITS-16);
240 }
241
242 /*###########################################################################################*/
243 #elif defined(SIMULATE_MIPS_DIV)
244
245 #define FUNCTION_schur_div
schur_div(FIXP_DBL num,FIXP_DBL denum,INT count)246 inline FIXP_DBL schur_div(FIXP_DBL num, FIXP_DBL denum, INT count)
247 {
248 FDK_ASSERT (count<=DFRACT_BITS-1);
249 FDK_ASSERT (num>=(FIXP_DBL)0);
250 FDK_ASSERT (denum>(FIXP_DBL)0);
251 FDK_ASSERT (num <= denum);
252
253 INT tmp = denum >> (count-1);
254 INT result = 0;
255
256 while (num > tmp)
257 {
258 num -= tmp;
259 result++;
260 }
261
262 return result << (DFRACT_BITS-count);
263 }
264
265 /*###########################################################################################*/
266 #endif /* target architecture selector */
267
268 #if !defined(FUNCTION_schur_div)
269 /**
270 * \brief Divide two FIXP_DBL values with given precision.
271 * \param num dividend
272 * \param denum divisor
273 * \param count amount of significant bits of the result (starting to the MSB)
274 * \return num/divisor
275 */
276 FIXP_DBL schur_div(FIXP_DBL num,FIXP_DBL denum, INT count);
277 #endif
278
279
280
281 FIXP_DBL mul_dbl_sgl_rnd (const FIXP_DBL op1,
282 const FIXP_SGL op2);
283
284 /**
285 * \brief multiply two values with normalization, thus max precision.
286 * Author: Robert Weidner
287 *
288 * \param f1 first factor
289 * \param f2 secod factor
290 * \param result_e pointer to an INT where the exponent of the result is stored into
291 * \return mantissa of the product f1*f2
292 */
293 FIXP_DBL fMultNorm(
294 FIXP_DBL f1,
295 FIXP_DBL f2,
296 INT *result_e
297 );
298
fMultNorm(FIXP_DBL f1,FIXP_DBL f2)299 inline FIXP_DBL fMultNorm(FIXP_DBL f1, FIXP_DBL f2)
300 {
301 FIXP_DBL m;
302 INT e;
303
304 m = fMultNorm(f1, f2, &e);
305
306 m = scaleValueSaturate(m, e);
307
308 return m;
309 }
310
311 /**
312 * \brief Divide 2 FIXP_DBL values with normalization of input values.
313 * \param num numerator
314 * \param denum denomintator
315 * \return num/denum with exponent = 0
316 */
317 FIXP_DBL fDivNorm(FIXP_DBL num, FIXP_DBL denom, INT *result_e);
318
319 /**
320 * \brief Divide 2 FIXP_DBL values with normalization of input values.
321 * \param num numerator
322 * \param denum denomintator
323 * \param result_e pointer to an INT where the exponent of the result is stored into
324 * \return num/denum with exponent = *result_e
325 */
326 FIXP_DBL fDivNorm(FIXP_DBL num, FIXP_DBL denom);
327
328 /**
329 * \brief Divide 2 FIXP_DBL values with normalization of input values.
330 * \param num numerator
331 * \param denum denomintator
332 * \return num/denum with exponent = 0
333 */
334 FIXP_DBL fDivNormHighPrec(FIXP_DBL L_num, FIXP_DBL L_denum, INT *result_e);
335
336 /**
337 * \brief Calculate log(argument)/log(2) (logarithm with base 2). deprecated. Use fLog2() instead.
338 * \param arg mantissa of the argument
339 * \param arg_e exponent of the argument
340 * \param result_e pointer to an INT to store the exponent of the result
341 * \return the mantissa of the result.
342 * \param
343 */
344 FIXP_DBL CalcLog2(FIXP_DBL arg, INT arg_e, INT *result_e);
345
346 /**
347 * \brief return 2 ^ (exp * 2^exp_e)
348 * \param exp_m mantissa of the exponent to 2.0f
349 * \param exp_e exponent of the exponent to 2.0f
350 * \param result_e pointer to a INT where the exponent of the result will be stored into
351 * \return mantissa of the result
352 */
353 FIXP_DBL f2Pow(const FIXP_DBL exp_m, const INT exp_e, INT *result_e);
354
355 /**
356 * \brief return 2 ^ (exp_m * 2^exp_e). This version returns only the mantissa with implicit exponent of zero.
357 * \param exp_m mantissa of the exponent to 2.0f
358 * \param exp_e exponent of the exponent to 2.0f
359 * \return mantissa of the result
360 */
361 FIXP_DBL f2Pow(const FIXP_DBL exp_m, const INT exp_e);
362
363 /**
364 * \brief return x ^ (exp * 2^exp_e), where log2(x) = baseLd_m * 2^(baseLd_e). This saves
365 * the need to compute log2() of constant values (when x is a constant).
366 * \param ldx_m mantissa of log2() of x.
367 * \param ldx_e exponent of log2() of x.
368 * \param exp_m mantissa of the exponent to 2.0f
369 * \param exp_e exponent of the exponent to 2.0f
370 * \param result_e pointer to a INT where the exponent of the result will be stored into
371 * \return mantissa of the result
372 */
373 FIXP_DBL fLdPow(
374 FIXP_DBL baseLd_m,
375 INT baseLd_e,
376 FIXP_DBL exp_m, INT exp_e,
377 INT *result_e
378 );
379
380 /**
381 * \brief return x ^ (exp * 2^exp_e), where log2(x) = baseLd_m * 2^(baseLd_e). This saves
382 * the need to compute log2() of constant values (when x is a constant). This version
383 * does not return an exponent, which is implicitly 0.
384 * \param ldx_m mantissa of log2() of x.
385 * \param ldx_e exponent of log2() of x.
386 * \param exp_m mantissa of the exponent to 2.0f
387 * \param exp_e exponent of the exponent to 2.0f
388 * \return mantissa of the result
389 */
390 FIXP_DBL fLdPow(
391 FIXP_DBL baseLd_m, INT baseLd_e,
392 FIXP_DBL exp_m, INT exp_e
393 );
394
395 /**
396 * \brief return (base * 2^base_e) ^ (exp * 2^exp_e). Use fLdPow() instead whenever possible.
397 * \param base_m mantissa of the base.
398 * \param base_e exponent of the base.
399 * \param exp_m mantissa of power to be calculated of the base.
400 * \param exp_e exponent of power to be calculated of the base.
401 * \param result_e pointer to a INT where the exponent of the result will be stored into.
402 * \return mantissa of the result.
403 */
404 FIXP_DBL fPow(FIXP_DBL base_m, INT base_e, FIXP_DBL exp_m, INT exp_e, INT *result_e);
405
406 /**
407 * \brief return (base * 2^base_e) ^ N
408 * \param base mantissa of the base
409 * \param base_e exponent of the base
410 * \param power to be calculated of the base
411 * \param result_e pointer to a INT where the exponent of the result will be stored into
412 * \return mantissa of the result
413 */
414 FIXP_DBL fPowInt(FIXP_DBL base_m, INT base_e, INT N, INT *result_e);
415
416 /**
417 * \brief calculate logarithm of base 2 of x_m * 2^(x_e)
418 * \param x_m mantissa of the input value.
419 * \param x_e exponent of the input value.
420 * \param pointer to an INT where the exponent of the result is returned into.
421 * \return mantissa of the result.
422 */
423 FIXP_DBL fLog2(FIXP_DBL x_m, INT x_e, INT *result_e);
424
425 /**
426 * \brief calculate logarithm of base 2 of x_m * 2^(x_e)
427 * \param x_m mantissa of the input value.
428 * \param x_e exponent of the input value.
429 * \return mantissa of the result with implicit exponent of LD_DATA_SHIFT.
430 */
431 FIXP_DBL fLog2(FIXP_DBL x_m, INT x_e);
432
433 /**
434 * \brief Add with saturation of the result.
435 * \param a first summand
436 * \param b second summand
437 * \return saturated sum of a and b.
438 */
fAddSaturate(const FIXP_SGL a,const FIXP_SGL b)439 inline FIXP_SGL fAddSaturate(const FIXP_SGL a, const FIXP_SGL b)
440 {
441 LONG sum;
442
443 sum = (LONG)(SHORT)a + (LONG)(SHORT)b;
444 sum = fMax(fMin((INT)sum, (INT)MAXVAL_SGL), (INT)MINVAL_SGL);
445 return (FIXP_SGL)(SHORT)sum;
446 }
447
448 /**
449 * \brief Add with saturation of the result.
450 * \param a first summand
451 * \param b second summand
452 * \return saturated sum of a and b.
453 */
fAddSaturate(const FIXP_DBL a,const FIXP_DBL b)454 inline FIXP_DBL fAddSaturate(const FIXP_DBL a, const FIXP_DBL b)
455 {
456 LONG sum;
457
458 sum = (LONG)(a>>1) + (LONG)(b>>1);
459 sum = fMax(fMin((INT)sum, (INT)(MAXVAL_DBL>>1)), (INT)(MINVAL_DBL>>1));
460 return (FIXP_DBL)(LONG)(sum<<1);
461 }
462
463 //#define TEST_ROUNDING
464
465
466
467
468 /*****************************************************************************
469
470 array for 1/n, n=1..80
471
472 ****************************************************************************/
473
474 extern const FIXP_DBL invCount[80];
475
476 LNK_SECTION_INITCODE
InitInvInt(void)477 inline void InitInvInt(void) {}
478
479
480 /**
481 * \brief Calculate the value of 1/i where i is a integer value. It supports
482 * input values from 0 upto 79.
483 * \param intValue Integer input value.
484 * \param FIXP_DBL representation of 1/intValue
485 */
GetInvInt(int intValue)486 inline FIXP_DBL GetInvInt(int intValue)
487 {
488 FDK_ASSERT((intValue >= 0) && (intValue < 80));
489 if (intValue > 79)
490 return invCount[79];
491 else if (intValue < 0)
492 return invCount[0];
493 else
494 return invCount[intValue];
495 }
496
497
498 #endif
499
500