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 /***************************  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 
98 #define LD_DATA_SCALING (64.0f)
99 #define LD_DATA_SHIFT   6   /* pow(2, LD_DATA_SHIFT) = LD_DATA_SCALING */
100 
101 /**
102  * \brief deprecated. Use fLog2() instead.
103  */
104 FIXP_DBL CalcLdData(FIXP_DBL op);
105 
106 void LdDataVector(FIXP_DBL *srcVector, FIXP_DBL *destVector, INT number);
107 
108 FIXP_DBL CalcInvLdData(FIXP_DBL op);
109 
110 
111 void     InitLdInt();
112 FIXP_DBL CalcLdInt(INT i);
113 
114 extern const USHORT sqrt_tab[49];
115 
sqrtFixp_lookup(FIXP_DBL x)116 inline FIXP_DBL sqrtFixp_lookup(FIXP_DBL x)
117 {
118   UINT y = (INT)x;
119   UCHAR is_zero=(y==0);
120   INT zeros=fixnormz_D(y) & 0x1e;
121   y<<=zeros;
122   UINT idx=(y>>26)-16;
123   USHORT frac=(y>>10)&0xffff;
124   USHORT nfrac=0xffff^frac;
125   UINT t=nfrac*sqrt_tab[idx]+frac*sqrt_tab[idx+1];
126   t=t>>(zeros>>1);
127   return(is_zero ? 0 : t);
128 }
129 
sqrtFixp_lookup(FIXP_DBL x,INT * x_e)130 inline FIXP_DBL sqrtFixp_lookup(FIXP_DBL x, INT *x_e)
131 {
132   UINT y = (INT)x;
133   INT e;
134 
135   if (x == (FIXP_DBL)0) {
136     return x;
137   }
138 
139   /* Normalize */
140   e=fixnormz_D(y);
141   y<<=e;
142   e  = *x_e - e + 2;
143 
144   /* Correct odd exponent. */
145   if (e & 1) {
146     y >>= 1;
147     e ++;
148   }
149   /* Get square root */
150   UINT idx=(y>>26)-16;
151   USHORT frac=(y>>10)&0xffff;
152   USHORT nfrac=0xffff^frac;
153   UINT t=nfrac*sqrt_tab[idx]+frac*sqrt_tab[idx+1];
154 
155   /* Write back exponent */
156   *x_e = e >> 1;
157   return (FIXP_DBL)(LONG)(t>>1);
158 }
159 
160 
161 
162 FIXP_DBL sqrtFixp(FIXP_DBL op);
163 
164 void InitInvSqrtTab();
165 
166 FIXP_DBL invSqrtNorm2(FIXP_DBL op, INT *shift);
167 
168 /*****************************************************************************
169 
170     functionname: invFixp
171     description:  delivers 1/(op)
172 
173 *****************************************************************************/
invFixp(FIXP_DBL op)174 inline FIXP_DBL invFixp(FIXP_DBL op)
175 {
176     INT tmp_exp ;
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)) ) ;
180 }
181 
182 
183 
184 #if defined(__mips__) && (__GNUC__==2)
185 
186 #define FUNCTION_schur_div
schur_div(FIXP_DBL num,FIXP_DBL denum,INT count)187 inline FIXP_DBL schur_div(FIXP_DBL num,FIXP_DBL denum, INT count)
188 {
189   INT result, tmp ;
190    __asm__ ("srl %1, %2, 15\n"
191             "div %3, %1\n" : "=lo" (result)
192                            : "%d" (tmp), "d" (denum) ,  "d" (num)
193                            : "hi" ) ;
194   return result<<16 ;
195 }
196 
197 /*###########################################################################################*/
198 #elif defined(__mips__) && (__GNUC__==3)
199 
200 #define FUNCTION_schur_div
schur_div(FIXP_DBL num,FIXP_DBL denum,INT count)201 inline FIXP_DBL schur_div(FIXP_DBL num,FIXP_DBL denum, INT count)
202 {
203   INT result, tmp;
204 
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)
209             : "hi", "lo");
210   return result << (DFRACT_BITS-16);
211 }
212 
213 /*###########################################################################################*/
214 #elif defined(SIMULATE_MIPS_DIV)
215 
216 #define FUNCTION_schur_div
schur_div(FIXP_DBL num,FIXP_DBL denum,INT count)217 inline FIXP_DBL schur_div(FIXP_DBL num, FIXP_DBL denum, INT count)
218 {
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);
223 
224     INT tmp = denum >> (count-1);
225     INT result = 0;
226 
227     while (num > tmp)
228     {
229         num -= tmp;
230         result++;
231     }
232 
233     return result << (DFRACT_BITS-count);
234 }
235 
236 /*###########################################################################################*/
237 #endif /* target architecture selector */
238 
239 #if !defined(FUNCTION_schur_div)
240 /**
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
246  */
247 FIXP_DBL schur_div(FIXP_DBL num,FIXP_DBL denum, INT count);
248 #endif
249 
250 
251 
252 FIXP_DBL mul_dbl_sgl_rnd (const FIXP_DBL op1,
253                           const FIXP_SGL op2);
254 
255 /**
256  * \brief multiply two values with normalization, thus max precision.
257  * Author: Robert Weidner
258  *
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
263  */
264 FIXP_DBL fMultNorm(
265         FIXP_DBL f1,
266         FIXP_DBL f2,
267         INT *result_e
268         );
269 
fMultNorm(FIXP_DBL f1,FIXP_DBL f2)270 inline FIXP_DBL fMultNorm(FIXP_DBL f1, FIXP_DBL f2)
271 {
272   FIXP_DBL m;
273   INT e;
274 
275   m = fMultNorm(f1, f2, &e);
276 
277   m = scaleValueSaturate(m, e);
278 
279   return m;
280 }
281 
282 /**
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
287  */
288 FIXP_DBL fDivNorm(FIXP_DBL num, FIXP_DBL denom, INT *result_e);
289 
290 /**
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
296  */
297 FIXP_DBL fDivNorm(FIXP_DBL num, FIXP_DBL denom);
298 
299 /**
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
304  */
305 FIXP_DBL fDivNormHighPrec(FIXP_DBL L_num, FIXP_DBL L_denum, INT *result_e);
306 
307 /**
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.
313  * \param
314  */
315 FIXP_DBL CalcLog2(FIXP_DBL arg, INT arg_e, INT *result_e);
316 
317 /**
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
323  */
324 FIXP_DBL f2Pow(const FIXP_DBL exp_m, const INT exp_e, INT *result_e);
325 
326 /**
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
331  */
332 FIXP_DBL f2Pow(const FIXP_DBL exp_m, const INT exp_e);
333 
334 /**
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
343  */
344 FIXP_DBL fLdPow(
345         FIXP_DBL baseLd_m,
346         INT baseLd_e,
347         FIXP_DBL exp_m, INT exp_e,
348         INT *result_e
349         );
350 
351 /**
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
360  */
361 FIXP_DBL fLdPow(
362         FIXP_DBL baseLd_m, INT baseLd_e,
363         FIXP_DBL exp_m, INT exp_e
364         );
365 
366 /**
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.
374  */
375 FIXP_DBL fPow(FIXP_DBL base_m, INT base_e, FIXP_DBL exp_m, INT exp_e, INT *result_e);
376 
377 /**
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
384  */
385 FIXP_DBL fPowInt(FIXP_DBL base_m, INT base_e, INT N, INT *result_e);
386 
387 /**
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.
393  */
394 FIXP_DBL fLog2(FIXP_DBL x_m, INT x_e, INT *result_e);
395 
396 /**
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.
401  */
402 FIXP_DBL fLog2(FIXP_DBL x_m, INT x_e);
403 
404 /**
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.
409  */
fAddSaturate(const FIXP_SGL a,const FIXP_SGL b)410 inline FIXP_SGL fAddSaturate(const FIXP_SGL a, const FIXP_SGL b)
411 {
412   LONG sum;
413 
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;
417 }
418 
419 /**
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.
424  */
fAddSaturate(const FIXP_DBL a,const FIXP_DBL b)425 inline FIXP_DBL fAddSaturate(const FIXP_DBL a, const FIXP_DBL b)
426 {
427   LONG sum;
428 
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);
432 }
433 
434 //#define TEST_ROUNDING
435 
436 
437 
438 
439 /*****************************************************************************
440 
441  array for 1/n, n=1..50
442 
443 ****************************************************************************/
444 
445   extern const FIXP_DBL invCount[50];
446 
447   LNK_SECTION_INITCODE
InitInvInt(void)448   inline void InitInvInt(void) {}
449 
450 
451 /**
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
456  */
GetInvInt(int intValue)457 inline FIXP_DBL GetInvInt(int intValue)
458 {
459   FDK_ASSERT((intValue > 0) && (intValue < 50));
460   FDK_ASSERT(intValue<50);
461 	return invCount[intValue];
462 }
463 
464 
465 #endif
466 
467