Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / codecs / libfaad / common.h
blob01164e374624177059b4df17e348e53833f9f6a7
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id$
26 **/
28 #ifndef __COMMON_H__
29 #define __COMMON_H__
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
35 #include "faad_config.h"
36 #include "codeclib.h"
38 extern struct codec_api* ci;
40 #if defined(DEBUG) || defined(SIMULATOR)
41 #undef DEBUGF
42 #define DEBUGF ci->debugf
43 #else
44 #define DEBUGF(...)
45 #endif
47 #ifdef ROCKBOX_HAS_LOGF
48 #undef LOGF
49 #define LOGF ci->logf
50 #else
51 #define LOGF(...)
52 #endif
54 #define INLINE __inline
55 #if 0 //defined(_WIN32) && !defined(_WIN32_WCE)
56 #define ALIGN __declspec(align(16))
57 #else
58 #define ALIGN
59 #endif
61 #ifndef max
62 #define max(a, b) (((a) > (b)) ? (a) : (b))
63 #endif
64 #ifndef min
65 #define min(a, b) (((a) < (b)) ? (a) : (b))
66 #endif
68 /* COMPILE TIME DEFINITIONS */
70 /* use double precision */
71 /* #define USE_DOUBLE_PRECISION */
72 /* use fixed point reals */
73 #define FIXED_POINT
74 //#define BIG_IQ_TABLE
76 /* Use if target platform has address generators with autoincrement */
77 //#define PREFER_POINTERS
79 #ifdef _WIN32_WCE
80 #define FIXED_POINT
81 #endif
84 #define ERROR_RESILIENCE
87 /* Allow decoding of MAIN profile AAC */
88 #define MAIN_DEC
89 #if MEMORYSIZE > 2
90 /* Allow decoding of SSR profile AAC */
91 #define SSR_DEC
92 #endif
93 /* Allow decoding of LTP profile AAC */
94 #define LTP_DEC
95 /* Allow decoding of LD profile AAC */
96 #define LD_DEC
97 /* Allow decoding of scalable profiles */
98 //#define SCALABLE_DEC
99 /* Allow decoding of Digital Radio Mondiale (DRM) */
100 //#define DRM
101 //#define DRM_PS
103 /* LD can't do without LTP */
104 #ifdef LD_DEC
105 #ifndef ERROR_RESILIENCE
106 #define ERROR_RESILIENCE
107 #endif
108 #ifndef LTP_DEC
109 #define LTP_DEC
110 #endif
111 #endif
113 #define ALLOW_SMALL_FRAMELENGTH
116 // Define LC_ONLY_DECODER if you want a pure AAC LC decoder (independant of SBR_DEC and PS_DEC)
117 #define LC_ONLY_DECODER
118 #ifdef LC_ONLY_DECODER
119 #undef LD_DEC
120 #undef LTP_DEC
121 #undef MAIN_DEC
122 #undef SSR_DEC
123 #undef DRM
124 #undef ALLOW_SMALL_FRAMELENGTH
125 #undef ERROR_RESILIENCE
126 #endif
128 #if MEMORYSIZE > 2
129 #define SBR_DEC
130 //#define SBR_LOW_POWER
131 #define PS_DEC
132 #endif
134 /* FIXED POINT: No MAIN decoding */
135 #ifdef FIXED_POINT
136 # ifdef MAIN_DEC
137 # undef MAIN_DEC
138 # endif
139 #endif // FIXED_POINT
141 #ifdef DRM
142 # ifndef SCALABLE_DEC
143 # define SCALABLE_DEC
144 # endif
145 #endif
148 #ifdef FIXED_POINT
149 #define DIV_R(A, B) (((int64_t)A << REAL_BITS)/B)
150 #define DIV_C(A, B) (((int64_t)A << COEF_BITS)/B)
151 #else
152 #define DIV_R(A, B) ((A)/(B))
153 #define DIV_C(A, B) ((A)/(B))
154 #endif
156 #ifndef SBR_LOW_POWER
157 #define qmf_t complex_t
158 #define QMF_RE(A) RE(A)
159 #define QMF_IM(A) IM(A)
160 #else
161 #define qmf_t real_t
162 #define QMF_RE(A) (A)
163 #define QMF_IM(A)
164 #endif
167 /* END COMPILE TIME DEFINITIONS */
169 #if defined(_WIN32) && !defined(__MINGW32__)
171 #include <stdlib.h>
173 typedef unsigned __int64 uint64_t;
174 typedef unsigned __int32 uint32_t;
175 typedef unsigned __int16 uint16_t;
176 typedef unsigned __int8 uint8_t;
177 typedef __int64 int64_t;
178 typedef __int32 int32_t;
179 typedef __int16 int16_t;
180 typedef __int8 int8_t;
181 typedef float float32_t;
184 #else
186 #include <stdio.h>
187 #ifdef HAVE_SYS_TYPES_H
188 # include "inttypes.h"
189 #endif
190 #ifdef HAVE_SYS_STAT_H
191 # include <sys/stat.h>
192 #endif
193 #ifdef STDC_HEADERS
194 # include <stdlib.h>
195 # include <stddef.h>
196 #else
197 # ifdef HAVE_STDLIB_H
198 # include <stdlib.h>
199 # endif
200 #endif
201 #ifdef HAVE_STRING_H
202 # if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
203 # include <memory.h>
204 # endif
205 //# include <string.h>
206 #endif
207 #ifdef HAVE_STRINGS_H
208 //# include <strings.h>
209 #endif
210 #ifdef HAVE_INTTYPES_H
211 # include <inttypes.h>
212 #else
213 # ifdef HAVE_STDINT_H
214 # include <stdint.h>
215 # else
216 /* we need these... */
217 typedef unsigned long long uint64_t;
218 typedef unsigned long uint32_t;
219 typedef unsigned short uint16_t;
220 typedef unsigned char uint8_t;
221 typedef long long int64_t;
222 typedef long int32_t;
223 typedef short int16_t;
224 typedef char int8_t;
225 # endif
226 #endif
227 #ifdef HAVE_UNISTD_H
228 # include <unistd.h>
229 #endif
231 #ifndef HAVE_FLOAT32_T
232 typedef float float32_t;
233 #endif
235 #if STDC_HEADERS
236 //# include <string.h>
237 #else
238 # if !HAVE_STRCHR
239 # define strchr index
240 # define strrchr rindex
241 # endif
242 char *strchr(), *strrchr();
243 # if !HAVE_MEMCPY
244 # define memcpy(d, s, n) bcopy((s), (d), (n))
245 # define memmove(d, s, n) bcopy((s), (d), (n))
246 # endif
247 #endif
249 #endif
251 #ifdef WORDS_BIGENDIAN
252 #define ARCH_IS_BIG_ENDIAN
253 #endif
255 /* FIXED_POINT doesn't work with MAIN and SSR yet */
256 #ifdef FIXED_POINT
257 #undef MAIN_DEC
258 #undef SSR_DEC
259 #endif
262 #if defined(FIXED_POINT)
264 #include "fixed.h"
266 #elif defined(USE_DOUBLE_PRECISION)
268 typedef double real_t;
270 #include <math.h>
272 #define MUL_R(A,B) ((A)*(B))
273 #define MUL_C(A,B) ((A)*(B))
274 #define MUL_F(A,B) ((A)*(B))
276 /* Complex multiplication */
277 static INLINE void ComplexMult(real_t *y1, real_t *y2,
278 real_t x1, real_t x2, real_t c1, real_t c2)
280 *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
281 *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
284 #define REAL_CONST(A) ((real_t)(A))
285 #define COEF_CONST(A) ((real_t)(A))
286 #define Q2_CONST(A) ((real_t)(A))
287 #define FRAC_CONST(A) ((real_t)(A)) /* pure fractional part */
289 #else /* Normal floating point operation */
291 typedef float real_t;
293 #define MUL_R(A,B) ((A)*(B))
294 #define MUL_C(A,B) ((A)*(B))
295 #define MUL_F(A,B) ((A)*(B))
297 #define REAL_CONST(A) ((real_t)(A))
298 #define COEF_CONST(A) ((real_t)(A))
299 #define Q2_CONST(A) ((real_t)(A))
300 #define FRAC_CONST(A) ((real_t)(A)) /* pure fractional part */
302 /* Complex multiplication */
303 static INLINE void ComplexMult(real_t *y1, real_t *y2,
304 real_t x1, real_t x2, real_t c1, real_t c2)
306 *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
307 *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
311 #if defined(_WIN32) && !defined(__MINGW32__)
312 #define HAS_LRINTF
313 static INLINE int lrintf(float f)
315 int i;
316 __asm
318 fld f
319 fistp i
321 return i;
323 #elif (defined(__i386__) && defined(__GNUC__))
324 #define HAS_LRINTF
325 // from http://www.stereopsis.com/FPU.html
326 static INLINE int lrintf(float f)
328 int i;
329 __asm__ __volatile__ (
330 "flds %1 \n\t"
331 "fistpl %0 \n\t"
332 : "=m" (i)
333 : "m" (f));
334 return i;
336 #endif
339 #ifdef __ICL /* only Intel C compiler has fmath ??? */
341 #include <mathf.h>
343 #define sin sinf
344 #define cos cosf
345 #define log logf
346 #define floor floorf
347 #define ceil ceilf
348 #define sqrt sqrtf
350 #else
352 #ifdef HAVE_LRINTF
353 # define HAS_LRINTF
354 # define _ISOC9X_SOURCE 1
355 # define _ISOC99_SOURCE 1
356 # define __USE_ISOC9X 1
357 # define __USE_ISOC99 1
358 #endif
360 #include <math.h>
362 #ifdef HAVE_SINF
363 # define sin sinf
364 #error
365 #endif
366 #ifdef HAVE_COSF
367 # define cos cosf
368 #endif
369 #ifdef HAVE_LOGF
370 # define log logf
371 #endif
372 #ifdef HAVE_EXPF
373 # define exp expf
374 #endif
375 #ifdef HAVE_FLOORF
376 # define floor floorf
377 #endif
378 #ifdef HAVE_CEILF
379 # define ceil ceilf
380 #endif
381 #ifdef HAVE_SQRTF
382 # define sqrt sqrtf
383 #endif
385 #endif
387 #endif
389 #ifndef HAS_LRINTF
390 /* standard cast */
391 #define lrintf(f) ((int32_t)(f))
392 #endif
394 typedef real_t complex_t[2];
395 #define RE(A) A[0]
396 #define IM(A) A[1]
399 /* common functions */
400 uint8_t cpu_has_sse(void);
401 uint32_t random_int(void);
402 #define wl_min_lzc(x) bs_generic(x, BS_LOG2|BS_0_0)
403 #ifdef FIXED_POINT
404 #define LOG2_MIN_INF REAL_CONST(-10000)
405 int32_t log2_int(uint32_t val);
406 int32_t log2_fix(uint32_t val);
407 uint32_t pow2_int(real_t val);
408 uint32_t pow2_fix(real_t val);
409 #endif
410 uint8_t get_sr_index(const uint32_t samplerate);
411 uint8_t max_pred_sfb(const uint8_t sr_index);
412 uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type,
413 const uint8_t is_short);
414 uint32_t get_sample_rate(const uint8_t sr_index);
415 int8_t can_decode_ot(const uint8_t object_type);
417 void *faad_malloc(size_t size);
418 void faad_free(void *b);
420 //#define PROFILE
421 #ifdef PROFILE
422 static int64_t faad_get_ts()
424 __asm
426 rdtsc
429 #endif
431 #ifndef M_PI
432 #define M_PI 3.14159265358979323846
433 #endif
434 #ifndef M_PI_2 /* PI/2 */
435 #define M_PI_2 1.57079632679489661923
436 #endif
439 #ifdef __cplusplus
441 #endif
442 #endif