Imported Debian version 0.1.3.1
[deb_fdk-aac.git] / libFDK / include / common_fix.h
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. 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
190 FDK_INLINE LONG fMult(SHORT a, SHORT b) { return fixmul_SS(a, b); }
191 FDK_INLINE LONG fMult(SHORT a, LONG b) { return fixmul_SD(a, b); }
192 FDK_INLINE LONG fMult(LONG a, SHORT b) { return fixmul_DS(a, b); }
193 FDK_INLINE LONG fMult(LONG a, LONG b) { return fixmul_DD(a, b); }
194 FDK_INLINE LONG fPow2(LONG a) { return fixpow2_D(a); }
195 FDK_INLINE LONG fPow2(SHORT a) { return fixpow2_S(a); }
196
197 FDK_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
200 FDK_INLINE INT fMultIfloor(LONG a, INT b) { return ( (INT)((1 +
201 fixmuldiv2_DD(a,(b<<FRACT_BITS))) >> (FRACT_BITS-1)) ); }
202
203 FDK_INLINE INT fMultIceil(LONG a, INT b) { return ( (INT)(((INT)0x7fff +
204 fixmuldiv2_DD(a,(b<<FRACT_BITS))) >> (FRACT_BITS-1)) ); }
205
206 FDK_INLINE LONG fMultDiv2(SHORT a, SHORT b) { return fixmuldiv2_SS(a, b); }
207 FDK_INLINE LONG fMultDiv2(SHORT a, LONG b) { return fixmuldiv2_SD(a, b); }
208 FDK_INLINE LONG fMultDiv2(LONG a, SHORT b) { return fixmuldiv2_DS(a, b); }
209 FDK_INLINE LONG fMultDiv2(LONG a, LONG b) { return fixmuldiv2_DD(a, b); }
210 FDK_INLINE LONG fPow2Div2(LONG a) { return fixpow2div2_D(a); }
211 FDK_INLINE LONG fPow2Div2(SHORT a) { return fixpow2div2_S(a); }
212
213 FDK_INLINE LONG fMultDiv2BitExact(LONG a, LONG b) { return fixmuldiv2BitExact_DD(a, b); }
214 FDK_INLINE LONG fMultDiv2BitExact(SHORT a, LONG b) { return fixmuldiv2BitExact_SD(a, b); }
215 FDK_INLINE LONG fMultDiv2BitExact(LONG a, SHORT b) { return fixmuldiv2BitExact_DS(a, b); }
216 FDK_INLINE LONG fMultBitExact(LONG a, LONG b) { return fixmulBitExact_DD(a, b); }
217 FDK_INLINE LONG fMultBitExact(SHORT a, LONG b) { return fixmulBitExact_SD(a, b); }
218 FDK_INLINE LONG fMultBitExact(LONG a, SHORT b) { return fixmulBitExact_DS(a, b); }
219
220 /* ******************************************************************************** */
221 #include "abs.h"
222
223 FDK_INLINE FIXP_DBL fAbs(FIXP_DBL x)
224 { return fixabs_D(x); }
225 FDK_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(__x86_64__)
230 FDK_INLINE INT fAbs(INT x)
231 { return fixabs_I(x); }
232 #endif
233
234 /* ******************************************************************************** */
235
236 #include "clz.h"
237
238 FDK_INLINE INT fNormz(FIXP_DBL x)
239 { return fixnormz_D(x); }
240 FDK_INLINE INT fNormz(FIXP_SGL x)
241 { return fixnormz_S(x); }
242 FDK_INLINE INT fNorm(FIXP_DBL x)
243 { return fixnorm_D(x); }
244 FDK_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) */
262 FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
263 { return fixmadddiv2_DD(x, a, b); }
264 FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
265 { return fixmadddiv2_SD(x, a, b); }
266 FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
267 { return fixmadddiv2_DS(x, a, b); }
268 FDK_INLINE FIXP_DBL fMultAddDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b)
269 { return fixmadddiv2_SS(x, a, b); }
270
271 FDK_INLINE FIXP_DBL fPow2AddDiv2(FIXP_DBL x, FIXP_DBL a)
272 { return fixpadddiv2_D(x, a); }
273 FDK_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) */
278 FDK_INLINE FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
279 { return fixmadd_DD(x, a, b); }
280 inline FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
281 { return fixmadd_SD(x, a, b); }
282 inline FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
283 { return fixmadd_DS(x, a, b); }
284 inline FIXP_DBL fMultAdd(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b)
285 { return fixmadd_SS(x, a, b); }
286
287 inline FIXP_DBL fPow2Add(FIXP_DBL x, FIXP_DBL a)
288 { return fixpadd_D(x, a); }
289 inline FIXP_DBL fPow2Add(FIXP_DBL x, FIXP_SGL a)
290 { return fixpadd_S(x, a); }
291
292
293 /* y = (x-0.5*a*b) */
294 inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
295 { return fixmsubdiv2_DD(x, a, b); }
296 inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
297 { return fixmsubdiv2_SD(x, a, b); }
298 inline FIXP_DBL fMultSubDiv2(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
299 { return fixmsubdiv2_DS(x, a, b); }
300 inline 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) */
304 FDK_INLINE FIXP_DBL fMultSub(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
305 { return fixmsub_DD(x, a, b); }
306 inline FIXP_DBL fMultSub(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
307 { return fixmsub_SD(x, a, b); }
308 inline FIXP_DBL fMultSub(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
309 { return fixmsub_DS(x, a, b); }
310 inline FIXP_DBL fMultSub(FIXP_DBL x, FIXP_SGL a, FIXP_SGL b)
311 { return fixmsub_SS(x, a, b); }
312
313 FDK_INLINE FIXP_DBL fMultAddDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
314 { return fixmadddiv2BitExact_DD(x, a, b); }
315 FDK_INLINE FIXP_DBL fMultAddDiv2BitExact(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
316 { return fixmadddiv2BitExact_SD(x, a, b); }
317 FDK_INLINE FIXP_DBL fMultAddDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_SGL b)
318 { return fixmadddiv2BitExact_DS(x, a, b); }
319 FDK_INLINE FIXP_DBL fMultSubDiv2BitExact(FIXP_DBL x, FIXP_DBL a, FIXP_DBL b)
320 { return fixmsubdiv2BitExact_DD(x, a, b); }
321 FDK_INLINE FIXP_DBL fMultSubDiv2BitExact(FIXP_DBL x, FIXP_SGL a, FIXP_DBL b)
322 { return fixmsubdiv2BitExact_SD(x, a, b); }
323 FDK_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
328 FDK_INLINE FIXP_DBL fMin(FIXP_DBL a, FIXP_DBL b)
329 { return fixmin_D(a,b); }
330 FDK_INLINE FIXP_DBL fMax(FIXP_DBL a, FIXP_DBL b)
331 { return fixmax_D(a,b); }
332
333 FDK_INLINE FIXP_SGL fMin(FIXP_SGL a, FIXP_SGL b)
334 { return fixmin_S(a,b); }
335 FDK_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(__x86_64__)) || (FIX_FRACT == 1)
340 FDK_INLINE INT fMax(INT a, INT b)
341 { return fixmax_I(a,b); }
342 FDK_INLINE INT fMin(INT a, INT b)
343 { return fixmin_I(a,b); }
344 #endif
345
346 inline UINT fMax(UINT a, UINT b)
347 { return fixmax_UI(a,b); }
348 inline UINT fMin(UINT a, UINT b)
349 { return fixmin_UI(a,b); }
350
351 /* Complex data types */
352 typedef 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
362 typedef 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