1
2/* -----------------------------------------------------------------------------------------------------------
3Software 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
9The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16of the MPEG specifications.
17
18Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20individually for the purpose of encoding or decoding bit streams in products that are compliant with
21the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27applications information and documentation.
28
292.    COPYRIGHT LICENSE
30
31Redistribution and use in source and binary forms, with or without modification, are permitted without
32payment of copyright license fees provided that you satisfy the following conditions:
33
34You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35your modifications thereto in source code form.
36
37You must retain the complete text of this software license in the documentation and/or other materials
38provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40modifications thereto to recipients of copies in binary form.
41
42The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43prior written permission.
44
45You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46software or your modifications thereto.
47
48Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49and 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
533.    NO PATENT LICENSE
54
55NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57respect to this software.
58
59You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60by appropriate patent licenses.
61
624.    DISCLAIMER
63
64This 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
66of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69or business interruption, however caused and on any theory of liability, whether in contract, strict
70liability, or tort (including negligence), arising in any way out of the use of this software, even if
71advised of the possibility of such damage.
72
735.    CONTACT INFORMATION
74
75Fraunhofer Institute for Integrated Circuits IIS
76Attention: Audio and Multimedia Departments - FDK AAC LL
77Am Wolfsmantel 33
7891058 Erlangen, Germany
79
80www.iis.fraunhofer.de/amm
81amm-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 */
104FIXP_DBL CalcLdData(FIXP_DBL op);
105
106void LdDataVector(FIXP_DBL *srcVector, FIXP_DBL *destVector, INT number);
107
108FIXP_DBL CalcInvLdData(FIXP_DBL op);
109
110
111void     InitLdInt();
112FIXP_DBL CalcLdInt(INT i);
113
114extern const USHORT sqrt_tab[49];
115
116inline 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
130inline 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
162FIXP_DBL sqrtFixp(FIXP_DBL op);
163
164void InitInvSqrtTab();
165
166FIXP_DBL invSqrtNorm2(FIXP_DBL op, INT *shift);
167
168/*****************************************************************************
169
170    functionname: invFixp
171    description:  delivers 1/(op)
172
173*****************************************************************************/
174inline 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
187inline 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
201inline 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
217inline 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 */
247FIXP_DBL schur_div(FIXP_DBL num,FIXP_DBL denum, INT count);
248#endif
249
250
251
252FIXP_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 */
264FIXP_DBL fMultNorm(
265        FIXP_DBL f1,
266        FIXP_DBL f2,
267        INT *result_e
268        );
269
270inline 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 */
288FIXP_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 */
297FIXP_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 */
305FIXP_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 */
315FIXP_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 */
324FIXP_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 */
332FIXP_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 */
344FIXP_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 */
361FIXP_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 */
375FIXP_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 */
385FIXP_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 */
394FIXP_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 */
402FIXP_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 */
410inline 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 */
425inline 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
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 */
457inline 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