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. Lohwasser, M. Gayer
87   Description: Flexible fixpoint library configuration
88
89******************************************************************************/
90
91#ifndef _COMMON_FIX_H
92#define _COMMON_FIX_H
93
94#include "FDK_archdef.h"
95#include "machine_type.h"
96
97/* ***** Start of former fix.h ****** */
98
99/* Configure fractional or integer arithmetic */
100  #define FIX_FRACT 0 /* Define this to "1" to use fractional arithmetic simulation in class fract instead of integer arithmetic */
101                      /* 1 for debug with extra runtime overflow checking.                                                      */
102
103/* Define bit sizes of integer fixpoint fractional data types */
104#define FRACT_BITS      16 /* single precision */
105#define DFRACT_BITS     32 /* double precision */
106#define ACCU_BITS       40 /* double precision plus overflow */
107
108/* Fixpoint equivalent type fot PCM audio time domain data. */
109#if defined(SAMPLE_BITS)
110#if (SAMPLE_BITS == DFRACT_BITS)
111  #define FIXP_PCM      FIXP_DBL
112  #define FX_PCM2FX_DBL(x) ((FIXP_DBL)(x))
113  #define FX_DBL2FX_PCM(x) ((INT_PCM)(x))
114#elif (SAMPLE_BITS == FRACT_BITS)
115  #define FIXP_PCM      FIXP_SGL
116  #define FX_PCM2FX_DBL(x) FX_SGL2FX_DBL((FIXP_SGL)(x))
117  #define FX_DBL2FX_PCM(x) FX_DBL2FX_SGL(x)
118#else
119  #error SAMPLE_BITS different from FRACT_BITS or DFRACT_BITS not implemented!
120#endif
121#endif
122
123/* ****** End of former fix.h ****** */
124
125#define SGL_MASK            ((1UL<<FRACT_BITS)-1)   /* 16bit: (2^16)-1 = 0xFFFF */
126
127#define MAX_SHIFT_SGL (FRACT_BITS-1)   /* maximum possible shift for FIXP_SGL values */
128#define MAX_SHIFT_DBL (DFRACT_BITS-1)  /* maximum possible shift for FIXP_DBL values */
129
130/* Scale factor from/to float/fixpoint values. DO NOT USE THESE VALUES AS SATURATION LIMITS !! */
131#define FRACT_FIX_SCALE     ((INT64(1)<<(FRACT_BITS-1)))
132#define DFRACT_FIX_SCALE    ((INT64(1)<<(DFRACT_BITS-1)))
133
134/* Max and Min values for saturation purposes. DO NOT USE THESE VALUES AS SCALE VALUES !! */
135#define  MAXVAL_SGL     ((signed)0x00007FFF)    /* this has to be synchronized to FRACT_BITS */
136#define  MINVAL_SGL     ((signed)0xFFFF8000)    /* this has to be synchronized to FRACT_BITS */
137#define  MAXVAL_DBL     ((signed)0x7FFFFFFF)    /* this has to be synchronized to DFRACT_BITS */
138#define  MINVAL_DBL     ((signed)0x80000000)    /* this has to be synchronized to DFRACT_BITS */
139
140
141#define FX_DBL2FXCONST_SGL(val) ( ( ((((val) >> (DFRACT_BITS-FRACT_BITS-1)) + 1) > (((LONG)1<<FRACT_BITS)-1)) && ((LONG)(val) > 0) ) ? \
142   (FIXP_SGL)(SHORT)(((LONG)1<<(FRACT_BITS-1))-1):(FIXP_SGL)(SHORT)((((val) >> (DFRACT_BITS-FRACT_BITS-1)) + 1) >> 1) )
143
144
145
146#define shouldBeUnion union     /* unions are possible */
147
148    typedef SHORT       FIXP_SGL;
149    typedef LONG        FIXP_DBL;
150
151/* macros for compile-time conversion of constant float values to fixedpoint */
152#define FL2FXCONST_SPC FL2FXCONST_DBL
153
154#define MINVAL_DBL_CONST MINVAL_DBL
155#define MINVAL_SGL_CONST MINVAL_SGL
156
157#define FL2FXCONST_SGL(val)                                                                                                     \
158(FIXP_SGL)( ( (val) >= 0) ?                                                                                                               \
159((( (double)(val) * (FRACT_FIX_SCALE) + 0.5 ) >= (double)(MAXVAL_SGL) ) ? (SHORT)(MAXVAL_SGL) : (SHORT)( (double)(val) * (double)(FRACT_FIX_SCALE) + 0.5)) :   \
160((( (double)(val) * (FRACT_FIX_SCALE) - 0.5) <=  (double)(MINVAL_SGL_CONST) ) ? (SHORT)(MINVAL_SGL_CONST) : (SHORT)( (double)(val) * (double)(FRACT_FIX_SCALE) - 0.5)) )
161
162#define FL2FXCONST_DBL(val)                                                                                                     \
163(FIXP_DBL)( ( (val) >= 0) ?                                                                                                               \
164((( (double)(val) * (DFRACT_FIX_SCALE) + 0.5 ) >= (double)(MAXVAL_DBL) ) ? (LONG)(MAXVAL_DBL) : (LONG)( (double)(val) * (double)(DFRACT_FIX_SCALE) + 0.5)) : \
165((( (double)(val) * (DFRACT_FIX_SCALE) - 0.5) <=  (double)(MINVAL_DBL_CONST) ) ? (LONG)(MINVAL_DBL_CONST) : (LONG)( (double)(val) * (double)(DFRACT_FIX_SCALE) - 0.5)) )
166
167/* macros for runtime conversion of float values to integer fixedpoint. NO OVERFLOW CHECK!!! */
168#define FL2FX_SPC FL2FX_DBL
169#define FL2FX_SGL(val) ( (val)>0.0f ? (SHORT)( (val)*(float)(FRACT_FIX_SCALE)+0.5f ) : (SHORT)( (val)*(float)(FRACT_FIX_SCALE)-0.5f ) )
170#define FL2FX_DBL(val) ( (val)>0.0f ? (LONG)( (val)*(float)(DFRACT_FIX_SCALE)+0.5f ) : (LONG)( (val)*(float)(DFRACT_FIX_SCALE)-0.5f ) )
171
172/* macros for runtime conversion of fixedpoint values to other fixedpoint. NO ROUNDING!!! */
173#define FX_ACC2FX_SGL(val) ((FIXP_SGL)((val)>>(ACCU_BITS-FRACT_BITS)))
174#define FX_ACC2FX_DBL(val) ((FIXP_DBL)((val)>>(ACCU_BITS-DFRACT_BITS)))
175#define FX_SGL2FX_ACC(val) ((FIXP_ACC)((LONG)(val)<<(ACCU_BITS-FRACT_BITS)))
176#define FX_SGL2FX_DBL(val) ((FIXP_DBL)((LONG)(val)<<(DFRACT_BITS-FRACT_BITS)))
177#define FX_DBL2FX_SGL(val) ((FIXP_SGL)((val)>>(DFRACT_BITS-FRACT_BITS)))
178
179/* ############################################################# */
180
181/* macros for runtime conversion of integer fixedpoint values to float. */
182/* This is just for temporary use and should not be required in a final version! */
183
184/* #define FX_DBL2FL(val)  ((float)(pow(2.,-31.)*(float)val)) */  /* version #1 */
185#define FX_DBL2FL(val)  ((float)((double)(val)/(double)DFRACT_FIX_SCALE))   /* version #2 - identical to class dfract cast from dfract to float */
186
187/* ############################################################# */
188#include "fixmul.h"
189
190FDK_INLINE LONG fMult(SHORT a, SHORT b)         { return fixmul_SS(a, b); }
191FDK_INLINE LONG fMult(SHORT a, LONG b)          { return fixmul_SD(a, b); }
192FDK_INLINE LONG fMult(LONG a, SHORT b)          { return fixmul_DS(a, b); }
193FDK_INLINE LONG fMult(LONG a, LONG b)           { return fixmul_DD(a, b); }
194FDK_INLINE LONG fPow2(LONG a)                   { return fixpow2_D(a);    }
195FDK_INLINE LONG fPow2(SHORT a)                  { return fixpow2_S(a);    }
196
197FDK_INLINE INT  fMultI(LONG a, SHORT b)         { return ( (INT)(((1<<(FRACT_BITS-2)) +
198                                                          fixmuldiv2_DD(a,((INT)b<<FRACT_BITS)))>>(FRACT_BITS-1)) ); }
199
200FDK_INLINE INT  fMultIfloor(LONG a, INT b)      { return ( (INT)((1 +
201                                                          fixmuldiv2_DD(a,(b<<FRACT_BITS))) >> (FRACT_BITS-1)) ); }
202
203FDK_INLINE INT  fMultIceil(LONG a, INT b)       { return ( (INT)(((INT)0x7fff +
204                                                          fixmuldiv2_DD(a,(b<<FRACT_BITS))) >> (FRACT_BITS-1)) ); }
205
206FDK_INLINE LONG fMultDiv2(SHORT a, SHORT b)     { return fixmuldiv2_SS(a, b); }
207FDK_INLINE LONG fMultDiv2(SHORT a, LONG b)      { return fixmuldiv2_SD(a, b); }
208FDK_INLINE LONG fMultDiv2(LONG a, SHORT b)      { return fixmuldiv2_DS(a, b); }
209FDK_INLINE LONG fMultDiv2(LONG a, LONG b)       { return fixmuldiv2_DD(a, b); }
210FDK_INLINE LONG fPow2Div2(LONG a)               { return fixpow2div2_D(a);    }
211FDK_INLINE LONG fPow2Div2(SHORT a)              { return fixpow2div2_S(a);    }
212
213FDK_INLINE LONG fMultDiv2BitExact(LONG a, LONG b)   { return fixmuldiv2BitExact_DD(a, b); }
214FDK_INLINE LONG fMultDiv2BitExact(SHORT a, LONG  b) { return fixmuldiv2BitExact_SD(a, b); }
215FDK_INLINE LONG fMultDiv2BitExact(LONG  a, SHORT b) { return fixmuldiv2BitExact_DS(a, b); }
216FDK_INLINE LONG fMultBitExact(LONG a, LONG b)       { return fixmulBitExact_DD(a, b); }
217FDK_INLINE LONG fMultBitExact(SHORT a, LONG  b)     { return fixmulBitExact_SD(a, b); }
218FDK_INLINE LONG fMultBitExact(LONG  a, SHORT b)     { return fixmulBitExact_DS(a, b); }
219
220/* ******************************************************************************** */
221#include "abs.h"
222
223FDK_INLINE FIXP_DBL fAbs(FIXP_DBL x)
224                { return fixabs_D(x); }
225FDK_INLINE FIXP_SGL fAbs(FIXP_SGL x)
226                { return fixabs_S(x); }
227
228/* workaround for TI C6x compiler but not for TI ARM9E compiler */
229#if (!defined(__TI_COMPILER_VERSION__) || defined(__TI_TMS470_V5__)) && !defined(__LP64__)
230FDK_INLINE INT  fAbs(INT x)
231                { return fixabs_I(x); }
232#endif
233
234/* ******************************************************************************** */
235
236#include "clz.h"
237
238FDK_INLINE INT fNormz(FIXP_DBL x)
239               { return fixnormz_D(x); }
240FDK_INLINE INT fNormz(FIXP_SGL x)
241               { return fixnormz_S(x); }
242FDK_INLINE INT fNorm(FIXP_DBL x)
243               { return fixnorm_D(x); }
244FDK_INLINE INT fNorm(FIXP_SGL x)
245               { return fixnorm_S(x); }
246
247
248/* ******************************************************************************** */
249/* ******************************************************************************** */
250/* ******************************************************************************** */
251
252#include "clz.h"
253#define fixp_abs(x) fAbs(x)
254#define fixMin(a,b) fMin(a,b)
255#define fixMax(a,b) fMax(a,b)
256#define CntLeadingZeros(x)  fixnormz_D(x)
257#define CountLeadingBits(x) fixnorm_D(x)
258
259#include "fixmadd.h"
260
261/* y = (x+0.5*a*b) */
262FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
263                { return fixmadddiv2_DD(x, a, b); }
264FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
265                { return fixmadddiv2_SD(x, a, b); }
266FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
267                { return fixmadddiv2_DS(x, a, b); }
268FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b)
269                { return fixmadddiv2_SS(x, a, b); }
270
271FDK_INLINE FIXP_DBL fPow2AddDiv2(FIXP_DBL x, FIXP_DBL a)
272                { return fixpadddiv2_D(x, a); }
273FDK_INLINE FIXP_DBL fPow2AddDiv2(FIXP_DBL x, FIXP_SGL a)
274                { return fixpadddiv2_S(x, a); }
275
276
277/* y = 2*(x+0.5*a*b) = (2x+a*b) */
278FDK_INLINE FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
279                { return fixmadd_DD(x, a, b); }
280inline FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
281                { return fixmadd_SD(x, a, b); }
282inline FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
283                { return fixmadd_DS(x, a, b); }
284inline FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b)
285                { return fixmadd_SS(x, a, b); }
286
287inline FIXP_DBL fPow2Add(FIXP_DBL x, FIXP_DBL a)
288                { return fixpadd_D(x, a); }
289inline FIXP_DBL fPow2Add(FIXP_DBL x, FIXP_SGL a)
290                { return fixpadd_S(x, a); }
291
292
293/* y = (x-0.5*a*b) */
294inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
295                { return fixmsubdiv2_DD(x, a, b); }
296inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
297                { return fixmsubdiv2_SD(x, a, b); }
298inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
299                { return fixmsubdiv2_DS(x, a, b); }
300inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b)
301                { return fixmsubdiv2_SS(x, a, b); }
302
303/* y = 2*(x-0.5*a*b) = (2*x-a*b) */
304FDK_INLINE FIXP_DBL fMultSub(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
305                { return fixmsub_DD(x, a, b); }
306inline FIXP_DBL fMultSub(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
307                { return fixmsub_SD(x, a, b); }
308inline FIXP_DBL fMultSub(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
309                { return fixmsub_DS(x, a, b); }
310inline FIXP_DBL fMultSub(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b)
311                { return fixmsub_SS(x, a, b); }
312
313FDK_INLINE FIXP_DBL fMultAddDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
314                { return fixmadddiv2BitExact_DD(x, a, b); }
315FDK_INLINE FIXP_DBL fMultAddDiv2BitExact(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
316                { return fixmadddiv2BitExact_SD(x, a, b); }
317FDK_INLINE FIXP_DBL fMultAddDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
318                { return fixmadddiv2BitExact_DS(x, a, b); }
319FDK_INLINE FIXP_DBL fMultSubDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
320                { return fixmsubdiv2BitExact_DD(x, a, b); }
321FDK_INLINE FIXP_DBL fMultSubDiv2BitExact(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
322                { return fixmsubdiv2BitExact_SD(x, a, b); }
323FDK_INLINE FIXP_DBL fMultSubDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
324                { return fixmsubdiv2BitExact_DS(x, a, b); }
325
326#include "fixminmax.h"
327
328FDK_INLINE FIXP_DBL fMin(FIXP_DBL a, FIXP_DBL b)
329                { return fixmin_D(a,b); }
330FDK_INLINE FIXP_DBL fMax(FIXP_DBL a, FIXP_DBL b)
331                { return fixmax_D(a,b); }
332
333FDK_INLINE FIXP_SGL fMin(FIXP_SGL a, FIXP_SGL b)
334                { return fixmin_S(a,b); }
335FDK_INLINE FIXP_SGL fMax(FIXP_SGL a, FIXP_SGL b)
336                { return fixmax_S(a,b); }
337
338/* workaround for TI C6x compiler but not for TI ARM9E */
339#if ((!defined(__TI_COMPILER_VERSION__) || defined(__TI_TMS470_V5__)) && !defined(__LP64__)) || (FIX_FRACT == 1)
340FDK_INLINE INT fMax(INT a, INT b)
341                { return fixmax_I(a,b); }
342FDK_INLINE INT fMin(INT a, INT b)
343                { return fixmin_I(a,b); }
344#endif
345
346inline UINT fMax(UINT a, UINT b)
347                { return fixmax_UI(a,b); }
348inline UINT fMin(UINT a, UINT b)
349                { return fixmin_UI(a,b); }
350
351/* Complex data types */
352typedef shouldBeUnion {
353  /* vector representation for arithmetic */
354  struct {
355    FIXP_SGL re;
356    FIXP_SGL im;
357  } v;
358  /* word representation for memory move */
359  LONG w;
360} FIXP_SPK;
361
362typedef shouldBeUnion {
363  /* vector representation for arithmetic */
364  struct {
365    FIXP_DBL re;
366    FIXP_DBL im;
367  } v;
368  /* word representation for memory move */
369  INT64 w;
370} FIXP_DPK;
371
372#include "fixmul.h"
373#include "fixmadd.h"
374#include "cplx_mul.h"
375#include "scale.h"
376#include "fixpoint_math.h"
377
378#endif
379