New makefile solution: A single invocation of 'make' to build the entire tree. Fully...
[kugel-rb.git] / apps / codecs / libfaad / common.h
bloba9c67a545950f93be585261ec1affe06c5de6ed4
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 /* Allow decoding of SSR profile AAC */
90 #define SSR_DEC
91 /* Allow decoding of LTP profile AAC */
92 #define LTP_DEC
93 /* Allow decoding of LD profile AAC */
94 #define LD_DEC
95 /* Allow decoding of scalable profiles */
96 //#define SCALABLE_DEC
97 /* Allow decoding of Digital Radio Mondiale (DRM) */
98 //#define DRM
99 //#define DRM_PS
101 /* LD can't do without LTP */
102 #ifdef LD_DEC
103 #ifndef ERROR_RESILIENCE
104 #define ERROR_RESILIENCE
105 #endif
106 #ifndef LTP_DEC
107 #define LTP_DEC
108 #endif
109 #endif
111 #define ALLOW_SMALL_FRAMELENGTH
114 // Define LC_ONLY_DECODER if you want a pure AAC LC decoder (independant of SBR_DEC and PS_DEC)
115 #define LC_ONLY_DECODER
116 #ifdef LC_ONLY_DECODER
117 #undef LD_DEC
118 #undef LTP_DEC
119 #undef MAIN_DEC
120 #undef SSR_DEC
121 #undef DRM
122 #undef ALLOW_SMALL_FRAMELENGTH
123 #undef ERROR_RESILIENCE
124 #endif
126 #define SBR_DEC
127 //#define SBR_LOW_POWER
128 #define PS_DEC
130 /* FIXED POINT: No MAIN decoding */
131 #ifdef FIXED_POINT
132 # ifdef MAIN_DEC
133 # undef MAIN_DEC
134 # endif
135 #endif // FIXED_POINT
137 #ifdef DRM
138 # ifndef SCALABLE_DEC
139 # define SCALABLE_DEC
140 # endif
141 #endif
144 #ifdef FIXED_POINT
145 #define DIV_R(A, B) (((int64_t)A << REAL_BITS)/B)
146 #define DIV_C(A, B) (((int64_t)A << COEF_BITS)/B)
147 #else
148 #define DIV_R(A, B) ((A)/(B))
149 #define DIV_C(A, B) ((A)/(B))
150 #endif
152 #ifndef SBR_LOW_POWER
153 #define qmf_t complex_t
154 #define QMF_RE(A) RE(A)
155 #define QMF_IM(A) IM(A)
156 #else
157 #define qmf_t real_t
158 #define QMF_RE(A) (A)
159 #define QMF_IM(A)
160 #endif
163 /* END COMPILE TIME DEFINITIONS */
165 #if defined(_WIN32) && !defined(__MINGW32__)
167 #include <stdlib.h>
169 typedef unsigned __int64 uint64_t;
170 typedef unsigned __int32 uint32_t;
171 typedef unsigned __int16 uint16_t;
172 typedef unsigned __int8 uint8_t;
173 typedef __int64 int64_t;
174 typedef __int32 int32_t;
175 typedef __int16 int16_t;
176 typedef __int8 int8_t;
177 typedef float float32_t;
180 #else
182 #include <stdio.h>
183 #ifdef HAVE_SYS_TYPES_H
184 # include <sys/types.h>
185 #endif
186 #ifdef HAVE_SYS_STAT_H
187 # include <sys/stat.h>
188 #endif
189 #ifdef STDC_HEADERS
190 # include <stdlib.h>
191 # include <stddef.h>
192 #else
193 # ifdef HAVE_STDLIB_H
194 # include <stdlib.h>
195 # endif
196 #endif
197 #ifdef HAVE_STRING_H
198 # if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
199 # include <memory.h>
200 # endif
201 //# include <string.h>
202 #endif
203 #ifdef HAVE_STRINGS_H
204 //# include <strings.h>
205 #endif
206 #ifdef HAVE_INTTYPES_H
207 # include <inttypes.h>
208 #else
209 # ifdef HAVE_STDINT_H
210 # include <stdint.h>
211 # else
212 /* we need these... */
213 typedef unsigned long long uint64_t;
214 typedef unsigned long uint32_t;
215 typedef unsigned short uint16_t;
216 typedef unsigned char uint8_t;
217 typedef long long int64_t;
218 typedef long int32_t;
219 typedef short int16_t;
220 typedef char int8_t;
221 # endif
222 #endif
223 #ifdef HAVE_UNISTD_H
224 # include <unistd.h>
225 #endif
227 #ifndef HAVE_FLOAT32_T
228 typedef float float32_t;
229 #endif
231 #if STDC_HEADERS
232 //# include <string.h>
233 #else
234 # if !HAVE_STRCHR
235 # define strchr index
236 # define strrchr rindex
237 # endif
238 char *strchr(), *strrchr();
239 # if !HAVE_MEMCPY
240 # define memcpy(d, s, n) bcopy((s), (d), (n))
241 # define memmove(d, s, n) bcopy((s), (d), (n))
242 # endif
243 #endif
245 #endif
247 #ifdef WORDS_BIGENDIAN
248 #define ARCH_IS_BIG_ENDIAN
249 #endif
251 /* FIXED_POINT doesn't work with MAIN and SSR yet */
252 #ifdef FIXED_POINT
253 #undef MAIN_DEC
254 #undef SSR_DEC
255 #endif
258 #if defined(FIXED_POINT)
260 #include "fixed.h"
262 #elif defined(USE_DOUBLE_PRECISION)
264 typedef double real_t;
266 #include <math.h>
268 #define MUL_R(A,B) ((A)*(B))
269 #define MUL_C(A,B) ((A)*(B))
270 #define MUL_F(A,B) ((A)*(B))
272 /* Complex multiplication */
273 static INLINE void ComplexMult(real_t *y1, real_t *y2,
274 real_t x1, real_t x2, real_t c1, real_t c2)
276 *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
277 *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
280 #define REAL_CONST(A) ((real_t)(A))
281 #define COEF_CONST(A) ((real_t)(A))
282 #define Q2_CONST(A) ((real_t)(A))
283 #define FRAC_CONST(A) ((real_t)(A)) /* pure fractional part */
285 #else /* Normal floating point operation */
287 typedef float real_t;
289 #define MUL_R(A,B) ((A)*(B))
290 #define MUL_C(A,B) ((A)*(B))
291 #define MUL_F(A,B) ((A)*(B))
293 #define REAL_CONST(A) ((real_t)(A))
294 #define COEF_CONST(A) ((real_t)(A))
295 #define Q2_CONST(A) ((real_t)(A))
296 #define FRAC_CONST(A) ((real_t)(A)) /* pure fractional part */
298 /* Complex multiplication */
299 static INLINE void ComplexMult(real_t *y1, real_t *y2,
300 real_t x1, real_t x2, real_t c1, real_t c2)
302 *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
303 *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
307 #if defined(_WIN32) && !defined(__MINGW32__)
308 #define HAS_LRINTF
309 static INLINE int lrintf(float f)
311 int i;
312 __asm
314 fld f
315 fistp i
317 return i;
319 #elif (defined(__i386__) && defined(__GNUC__))
320 #define HAS_LRINTF
321 // from http://www.stereopsis.com/FPU.html
322 static INLINE int lrintf(float f)
324 int i;
325 __asm__ __volatile__ (
326 "flds %1 \n\t"
327 "fistpl %0 \n\t"
328 : "=m" (i)
329 : "m" (f));
330 return i;
332 #endif
335 #ifdef __ICL /* only Intel C compiler has fmath ??? */
337 #include <mathf.h>
339 #define sin sinf
340 #define cos cosf
341 #define log logf
342 #define floor floorf
343 #define ceil ceilf
344 #define sqrt sqrtf
346 #else
348 #ifdef HAVE_LRINTF
349 # define HAS_LRINTF
350 # define _ISOC9X_SOURCE 1
351 # define _ISOC99_SOURCE 1
352 # define __USE_ISOC9X 1
353 # define __USE_ISOC99 1
354 #endif
356 #include <math.h>
358 #ifdef HAVE_SINF
359 # define sin sinf
360 #error
361 #endif
362 #ifdef HAVE_COSF
363 # define cos cosf
364 #endif
365 #ifdef HAVE_LOGF
366 # define log logf
367 #endif
368 #ifdef HAVE_EXPF
369 # define exp expf
370 #endif
371 #ifdef HAVE_FLOORF
372 # define floor floorf
373 #endif
374 #ifdef HAVE_CEILF
375 # define ceil ceilf
376 #endif
377 #ifdef HAVE_SQRTF
378 # define sqrt sqrtf
379 #endif
381 #endif
383 #endif
385 #ifndef HAS_LRINTF
386 /* standard cast */
387 #define lrintf(f) ((int32_t)(f))
388 #endif
390 typedef real_t complex_t[2];
391 #define RE(A) A[0]
392 #define IM(A) A[1]
395 /* common functions */
396 uint8_t cpu_has_sse(void);
397 uint32_t random_int(void);
398 uint32_t ones32(uint32_t x);
399 uint32_t floor_log2(uint32_t x);
400 uint32_t wl_min_lzc(uint32_t x);
401 #ifdef FIXED_POINT
402 #define LOG2_MIN_INF REAL_CONST(-10000)
403 int32_t log2_int(uint32_t val);
404 int32_t log2_fix(uint32_t val);
405 int32_t pow2_int(real_t val);
406 real_t pow2_fix(real_t val);
407 #endif
408 uint8_t get_sr_index(const uint32_t samplerate);
409 uint8_t max_pred_sfb(const uint8_t sr_index);
410 uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type,
411 const uint8_t is_short);
412 uint32_t get_sample_rate(const uint8_t sr_index);
413 int8_t can_decode_ot(const uint8_t object_type);
415 void *faad_malloc(size_t size);
416 void faad_free(void *b);
418 //#define PROFILE
419 #ifdef PROFILE
420 static int64_t faad_get_ts()
422 __asm
424 rdtsc
427 #endif
429 #ifndef M_PI
430 #define M_PI 3.14159265358979323846
431 #endif
432 #ifndef M_PI_2 /* PI/2 */
433 #define M_PI_2 1.57079632679489661923
434 #endif
437 #ifdef __cplusplus
439 #endif
440 #endif