2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
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.
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.
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.
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.
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:
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.
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.
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
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.
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."
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.
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
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.
73 5. CONTACT INFORMATION
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
78 91058 Erlangen, Germany
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
84 /*************************** Fraunhofer IIS FDK Tools **********************
87 Description: Fixed point specific mathematical functions
89 ******************************************************************************/
91 #ifndef __fixpoint_math_H
92 #define __fixpoint_math_H
95 #include "common_fix.h"
98 #define LD_DATA_SCALING (64.0f)
99 #define LD_DATA_SHIFT 6 /* pow(2, LD_DATA_SHIFT) = LD_DATA_SCALING */
102 * \brief deprecated. Use fLog2() instead.
104 FIXP_DBL
CalcLdData(FIXP_DBL op
);
106 void LdDataVector(FIXP_DBL
*srcVector
, FIXP_DBL
*destVector
, INT number
);
108 FIXP_DBL
CalcInvLdData(FIXP_DBL op
);
112 FIXP_DBL
CalcLdInt(INT i
);
114 extern const USHORT sqrt_tab
[49];
116 inline FIXP_DBL
sqrtFixp_lookup(FIXP_DBL x
)
119 UCHAR is_zero
=(y
==0);
120 INT zeros
=fixnormz_D(y
) & 0x1e;
123 USHORT frac
=(y
>>10)&0xffff;
124 USHORT nfrac
=0xffff^frac
;
125 UINT t
=nfrac
*sqrt_tab
[idx
]+frac
*sqrt_tab
[idx
+1];
127 return(is_zero
? 0 : t
);
130 inline FIXP_DBL
sqrtFixp_lookup(FIXP_DBL x
, INT
*x_e
)
135 if (x
== (FIXP_DBL
)0) {
144 /* Correct odd exponent. */
149 /* Get square root */
151 USHORT frac
=(y
>>10)&0xffff;
152 USHORT nfrac
=0xffff^frac
;
153 UINT t
=nfrac
*sqrt_tab
[idx
]+frac
*sqrt_tab
[idx
+1];
155 /* Write back exponent */
157 return (FIXP_DBL
)(LONG
)(t
>>1);
162 FIXP_DBL
sqrtFixp(FIXP_DBL op
);
164 void InitInvSqrtTab();
166 FIXP_DBL
invSqrtNorm2(FIXP_DBL op
, INT
*shift
);
168 /*****************************************************************************
170 functionname: invFixp
171 description: delivers 1/(op)
173 *****************************************************************************/
174 inline FIXP_DBL
invFixp(FIXP_DBL op
)
177 FIXP_DBL tmp_inv
= invSqrtNorm2(op
, &tmp_exp
) ;
178 FDK_ASSERT((31-(2*tmp_exp
+1))>=0) ;
179 return ( fPow2Div2( (FIXP_DBL
)tmp_inv
) >> (31-(2*tmp_exp
+1)) ) ;
184 #if defined(__mips__) && (__GNUC__==2)
186 #define FUNCTION_schur_div
187 inline FIXP_DBL
schur_div(FIXP_DBL num
,FIXP_DBL denum
, INT count
)
190 __asm__ ("srl %1, %2, 15\n"
191 "div %3, %1\n" : "=lo" (result
)
192 : "%d" (tmp
), "d" (denum
) , "d" (num
)
197 /*###########################################################################################*/
198 #elif defined(__mips__) && (__GNUC__==3)
200 #define FUNCTION_schur_div
201 inline FIXP_DBL
schur_div(FIXP_DBL num
,FIXP_DBL denum
, INT count
)
205 __asm__ ("srl %[tmp], %[denum], 15\n"
206 "div %[result], %[num], %[tmp]\n"
207 : [tmp
] "+r" (tmp
), [result
]"=r"(result
)
208 : [denum
]"r"(denum
), [num
]"r"(num
)
210 return result
<< (DFRACT_BITS
-16);
213 /*###########################################################################################*/
214 #elif defined(SIMULATE_MIPS_DIV)
216 #define FUNCTION_schur_div
217 inline FIXP_DBL
schur_div(FIXP_DBL num
, FIXP_DBL denum
, INT count
)
219 FDK_ASSERT (count
<=DFRACT_BITS
-1);
220 FDK_ASSERT (num
>=(FIXP_DBL
)0);
221 FDK_ASSERT (denum
>(FIXP_DBL
)0);
222 FDK_ASSERT (num
<= denum
);
224 INT tmp
= denum
>> (count
-1);
233 return result
<< (DFRACT_BITS
-count
);
236 /*###########################################################################################*/
237 #endif /* target architecture selector */
239 #if !defined(FUNCTION_schur_div)
241 * \brief Divide two FIXP_DBL values with given precision.
242 * \param num dividend
243 * \param denum divisor
244 * \param count amount of significant bits of the result (starting to the MSB)
245 * \return num/divisor
247 FIXP_DBL
schur_div(FIXP_DBL num
,FIXP_DBL denum
, INT count
);
252 FIXP_DBL
mul_dbl_sgl_rnd (const FIXP_DBL op1
,
256 * \brief multiply two values with normalization, thus max precision.
257 * Author: Robert Weidner
259 * \param f1 first factor
260 * \param f2 secod factor
261 * \param result_e pointer to an INT where the exponent of the result is stored into
262 * \return mantissa of the product f1*f2
270 inline FIXP_DBL
fMultNorm(FIXP_DBL f1
, FIXP_DBL f2
)
275 m
= fMultNorm(f1
, f2
, &e
);
277 m
= scaleValueSaturate(m
, e
);
283 * \brief Divide 2 FIXP_DBL values with normalization of input values.
284 * \param num numerator
285 * \param denum denomintator
286 * \return num/denum with exponent = 0
288 FIXP_DBL
fDivNorm(FIXP_DBL num
, FIXP_DBL denom
, INT
*result_e
);
291 * \brief Divide 2 FIXP_DBL values with normalization of input values.
292 * \param num numerator
293 * \param denum denomintator
294 * \param result_e pointer to an INT where the exponent of the result is stored into
295 * \return num/denum with exponent = *result_e
297 FIXP_DBL
fDivNorm(FIXP_DBL num
, FIXP_DBL denom
);
300 * \brief Divide 2 FIXP_DBL values with normalization of input values.
301 * \param num numerator
302 * \param denum denomintator
303 * \return num/denum with exponent = 0
305 FIXP_DBL
fDivNormHighPrec(FIXP_DBL L_num
, FIXP_DBL L_denum
, INT
*result_e
);
308 * \brief Calculate log(argument)/log(2) (logarithm with base 2). deprecated. Use fLog2() instead.
309 * \param arg mantissa of the argument
310 * \param arg_e exponent of the argument
311 * \param result_e pointer to an INT to store the exponent of the result
312 * \return the mantissa of the result.
315 FIXP_DBL
CalcLog2(FIXP_DBL arg
, INT arg_e
, INT
*result_e
);
318 * \brief return 2 ^ (exp * 2^exp_e)
319 * \param exp_m mantissa of the exponent to 2.0f
320 * \param exp_e exponent of the exponent to 2.0f
321 * \param result_e pointer to a INT where the exponent of the result will be stored into
322 * \return mantissa of the result
324 FIXP_DBL
f2Pow(const FIXP_DBL exp_m
, const INT exp_e
, INT
*result_e
);
327 * \brief return 2 ^ (exp_m * 2^exp_e). This version returns only the mantissa with implicit exponent of zero.
328 * \param exp_m mantissa of the exponent to 2.0f
329 * \param exp_e exponent of the exponent to 2.0f
330 * \return mantissa of the result
332 FIXP_DBL
f2Pow(const FIXP_DBL exp_m
, const INT exp_e
);
335 * \brief return x ^ (exp * 2^exp_e), where log2(x) = baseLd_m * 2^(baseLd_e). This saves
336 * the need to compute log2() of constant values (when x is a constant).
337 * \param ldx_m mantissa of log2() of x.
338 * \param ldx_e exponent of log2() of x.
339 * \param exp_m mantissa of the exponent to 2.0f
340 * \param exp_e exponent of the exponent to 2.0f
341 * \param result_e pointer to a INT where the exponent of the result will be stored into
342 * \return mantissa of the result
347 FIXP_DBL exp_m
, INT exp_e
,
352 * \brief return x ^ (exp * 2^exp_e), where log2(x) = baseLd_m * 2^(baseLd_e). This saves
353 * the need to compute log2() of constant values (when x is a constant). This version
354 * does not return an exponent, which is implicitly 0.
355 * \param ldx_m mantissa of log2() of x.
356 * \param ldx_e exponent of log2() of x.
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
362 FIXP_DBL baseLd_m
, INT baseLd_e
,
363 FIXP_DBL exp_m
, INT exp_e
367 * \brief return (base * 2^base_e) ^ (exp * 2^exp_e). Use fLdPow() instead whenever possible.
368 * \param base_m mantissa of the base.
369 * \param base_e exponent of the base.
370 * \param exp_m mantissa of power to be calculated of the base.
371 * \param exp_e exponent of power to be calculated of the base.
372 * \param result_e pointer to a INT where the exponent of the result will be stored into.
373 * \return mantissa of the result.
375 FIXP_DBL
fPow(FIXP_DBL base_m
, INT base_e
, FIXP_DBL exp_m
, INT exp_e
, INT
*result_e
);
378 * \brief return (base * 2^base_e) ^ N
379 * \param base mantissa of the base
380 * \param base_e exponent of the base
381 * \param power to be calculated of the base
382 * \param result_e pointer to a INT where the exponent of the result will be stored into
383 * \return mantissa of the result
385 FIXP_DBL
fPowInt(FIXP_DBL base_m
, INT base_e
, INT N
, INT
*result_e
);
388 * \brief calculate logarithm of base 2 of x_m * 2^(x_e)
389 * \param x_m mantissa of the input value.
390 * \param x_e exponent of the input value.
391 * \param pointer to an INT where the exponent of the result is returned into.
392 * \return mantissa of the result.
394 FIXP_DBL
fLog2(FIXP_DBL x_m
, INT x_e
, INT
*result_e
);
397 * \brief calculate logarithm of base 2 of x_m * 2^(x_e)
398 * \param x_m mantissa of the input value.
399 * \param x_e exponent of the input value.
400 * \return mantissa of the result with implicit exponent of LD_DATA_SHIFT.
402 FIXP_DBL
fLog2(FIXP_DBL x_m
, INT x_e
);
405 * \brief Add with saturation of the result.
406 * \param a first summand
407 * \param b second summand
408 * \return saturated sum of a and b.
410 inline FIXP_SGL
fAddSaturate(const FIXP_SGL a
, const FIXP_SGL b
)
414 sum
= (LONG
)(SHORT
)a
+ (LONG
)(SHORT
)b
;
415 sum
= fMax(fMin((INT
)sum
, (INT
)MAXVAL_SGL
), (INT
)MINVAL_SGL
);
416 return (FIXP_SGL
)(SHORT
)sum
;
420 * \brief Add with saturation of the result.
421 * \param a first summand
422 * \param b second summand
423 * \return saturated sum of a and b.
425 inline FIXP_DBL
fAddSaturate(const FIXP_DBL a
, const FIXP_DBL b
)
429 sum
= (LONG
)(a
>>1) + (LONG
)(b
>>1);
430 sum
= fMax(fMin((INT
)sum
, (INT
)(MAXVAL_DBL
>>1)), (INT
)(MINVAL_DBL
>>1));
431 return (FIXP_DBL
)(LONG
)(sum
<<1);
434 //#define TEST_ROUNDING
439 /*****************************************************************************
441 array for 1/n, n=1..50
443 ****************************************************************************/
445 extern const FIXP_DBL invCount
[50];
448 inline void InitInvInt(void) {}
452 * \brief Calculate the value of 1/i where i is a integer value. It supports
453 * input values from 1 upto 50.
454 * \param intValue Integer input value.
455 * \param FIXP_DBL representation of 1/intValue
457 inline FIXP_DBL
GetInvInt(int intValue
)
459 FDK_ASSERT((intValue
> 0) && (intValue
< 50));
460 FDK_ASSERT(intValue
<50);
461 return invCount
[intValue
];