Rework of libfaad in several areas. Allow removal of malloc with a new define FAAD_ST...
[kugel-rb.git] / apps / codecs / libfaad / common.h
blob08f4c7ab538d691fc089be5339e5adf2f8c1411a
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 #if (CONFIG_CPU == MCF5250)
55 /* Enough IRAM but performance suffers with ICODE_ATTR. */
56 #define IBSS_ATTR_FAAD_LARGE_IRAM IBSS_ATTR
57 #define ICODE_ATTR_FAAD_LARGE_IRAM
58 #define ICONST_ATTR_FAAD_LARGE_IRAM ICONST_ATTR
60 #elif (CONFIG_CPU == PP5022) || (CONFIG_CPU == PP5024)
61 /* Enough IRAM to move additional data and code to it. */
62 #define IBSS_ATTR_FAAD_LARGE_IRAM IBSS_ATTR
63 #define ICODE_ATTR_FAAD_LARGE_IRAM ICODE_ATTR
64 #define ICONST_ATTR_FAAD_LARGE_IRAM ICONST_ATTR
66 #elif defined(CPU_S5L870X)
67 /* Very large IRAM. Move even more data to it. */
68 #define IBSS_ATTR_FAAD_LARGE_IRAM IBSS_ATTR
69 #define ICODE_ATTR_FAAD_LARGE_IRAM ICODE_ATTR
70 #define ICONST_ATTR_FAAD_LARGE_IRAM ICONST_ATTR
71 #define FAAD_HAVE_XLR_IN_IRAM
73 #else
74 /* Not enough IRAM available. */
75 #define IBSS_ATTR_FAAD_LARGE_IRAM
76 #define ICODE_ATTR_FAAD_LARGE_IRAM
77 #define ICONST_ATTR_FAAD_LARGE_IRAM
78 #endif
80 /* Used to allocate several SBR + PS arrays and variables statically. */
81 //#define FAAD_STATIC_ALLOC
83 #define INLINE __inline
84 #if 0 //defined(_WIN32) && !defined(_WIN32_WCE)
85 #define ALIGN __declspec(align(16))
86 #else
87 #define ALIGN
88 #endif
90 #ifndef max
91 #define max(a, b) (((a) > (b)) ? (a) : (b))
92 #endif
93 #ifndef min
94 #define min(a, b) (((a) < (b)) ? (a) : (b))
95 #endif
97 /* COMPILE TIME DEFINITIONS */
99 /* use double precision */
100 /* #define USE_DOUBLE_PRECISION */
101 /* use fixed point reals */
102 #define FIXED_POINT
103 #define BIG_IQ_TABLE /* BIG_IQ_TABLE results in faster requantization */
105 /* Use if target platform has address generators with autoincrement */
106 //#define PREFER_POINTERS
108 #define ERROR_RESILIENCE
111 /* Allow decoding of MAIN profile AAC */
112 #define MAIN_DEC
113 #if MEMORYSIZE > 2
114 /* Allow decoding of SSR profile AAC */
115 #define SSR_DEC
116 #endif
117 /* Allow decoding of LTP profile AAC */
118 #define LTP_DEC
119 /* Allow decoding of LD profile AAC */
120 #define LD_DEC
121 /* Allow decoding of scalable profiles */
122 //#define SCALABLE_DEC
123 /* Allow decoding of Digital Radio Mondiale (DRM) */
124 //#define DRM
125 //#define DRM_PS
127 /* LD can't do without LTP */
128 #ifdef LD_DEC
129 #ifndef ERROR_RESILIENCE
130 #define ERROR_RESILIENCE
131 #endif
132 #ifndef LTP_DEC
133 #define LTP_DEC
134 #endif
135 #endif
137 #define ALLOW_SMALL_FRAMELENGTH
140 // Define LC_ONLY_DECODER if you want a pure AAC LC decoder (independant of SBR_DEC and PS_DEC)
141 #define LC_ONLY_DECODER
142 #ifdef LC_ONLY_DECODER
143 #undef LD_DEC
144 #undef LTP_DEC
145 #undef MAIN_DEC
146 #undef SSR_DEC
147 #undef DRM
148 #undef ALLOW_SMALL_FRAMELENGTH
149 #undef ERROR_RESILIENCE
150 #endif
152 #if MEMORYSIZE > 2
153 #define SBR_DEC
154 //#define SBR_LOW_POWER
155 #define PS_DEC
156 #endif
158 /* FIXED POINT: No MAIN decoding */
159 #ifdef FIXED_POINT
160 # ifdef MAIN_DEC
161 # undef MAIN_DEC
162 # endif
163 #endif // FIXED_POINT
165 #ifdef DRM
166 # ifndef SCALABLE_DEC
167 # define SCALABLE_DEC
168 # endif
169 #endif
172 #ifdef FIXED_POINT
173 #define DIV_R(A, B) (((int64_t)A << REAL_BITS)/B)
174 #define DIV_C(A, B) (((int64_t)A << COEF_BITS)/B)
175 #define DIV_Q(A, B) (((int64_t)A << Q2_BITS )/B)
176 #else
177 #define DIV_R(A, B) ((A)/(B))
178 #define DIV_C(A, B) ((A)/(B))
179 #define DIV_Q(A, B) ((A)/(B))
180 #endif
182 #ifndef SBR_LOW_POWER
183 #define qmf_t complex_t
184 #define QMF_RE(A) RE(A)
185 #define QMF_IM(A) IM(A)
186 #else
187 #define qmf_t real_t
188 #define QMF_RE(A) (A)
189 #define QMF_IM(A)
190 #endif
193 /* END COMPILE TIME DEFINITIONS */
195 #if defined(_WIN32) && !defined(__MINGW32__)
197 #include <stdlib.h>
199 typedef unsigned __int64 uint64_t;
200 typedef unsigned __int32 uint32_t;
201 typedef unsigned __int16 uint16_t;
202 typedef unsigned __int8 uint8_t;
203 typedef __int64 int64_t;
204 typedef __int32 int32_t;
205 typedef __int16 int16_t;
206 typedef __int8 int8_t;
207 typedef float float32_t;
210 #else
212 #include <stdio.h>
213 #ifdef HAVE_SYS_TYPES_H
214 # include "inttypes.h"
215 #endif
216 #ifdef HAVE_SYS_STAT_H
217 # include <sys/stat.h>
218 #endif
219 #ifdef STDC_HEADERS
220 # include <stdlib.h>
221 # include <stddef.h>
222 #else
223 # ifdef HAVE_STDLIB_H
224 # include <stdlib.h>
225 # endif
226 #endif
227 #ifdef HAVE_STRING_H
228 # if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
229 # include <memory.h>
230 # endif
231 //# include <string.h>
232 #endif
233 #ifdef HAVE_STRINGS_H
234 //# include <strings.h>
235 #endif
236 #ifdef HAVE_INTTYPES_H
237 # include <inttypes.h>
238 #else
239 # ifdef HAVE_STDINT_H
240 # include <stdint.h>
241 # else
242 /* we need these... */
243 typedef unsigned long long uint64_t;
244 typedef unsigned long uint32_t;
245 typedef unsigned short uint16_t;
246 typedef unsigned char uint8_t;
247 typedef long long int64_t;
248 typedef long int32_t;
249 typedef short int16_t;
250 typedef char int8_t;
251 # endif
252 #endif
253 #ifdef HAVE_UNISTD_H
254 # include <unistd.h>
255 #endif
257 #ifndef HAVE_FLOAT32_T
258 typedef float float32_t;
259 #endif
261 #if STDC_HEADERS
262 //# include <string.h>
263 #else
264 # if !HAVE_STRCHR
265 # define strchr index
266 # define strrchr rindex
267 # endif
268 char *strchr(), *strrchr();
269 # if !HAVE_MEMCPY
270 # define memcpy(d, s, n) bcopy((s), (d), (n))
271 # define memmove(d, s, n) bcopy((s), (d), (n))
272 # endif
273 #endif
275 #endif
277 #ifdef WORDS_BIGENDIAN
278 #define ARCH_IS_BIG_ENDIAN
279 #endif
281 /* FIXED_POINT doesn't work with MAIN and SSR yet */
282 #ifdef FIXED_POINT
283 #undef MAIN_DEC
284 #undef SSR_DEC
285 #endif
288 #if defined(FIXED_POINT)
290 #include "fixed.h"
292 #elif defined(USE_DOUBLE_PRECISION)
294 typedef double real_t;
296 #include <math.h>
298 #define MUL_R(A,B) ((A)*(B))
299 #define MUL_C(A,B) ((A)*(B))
300 #define MUL_F(A,B) ((A)*(B))
301 #define MUL_Q2(A,B) ((A)*(B))
303 /* Complex multiplication */
304 static INLINE void ComplexMult(real_t *y1, real_t *y2,
305 real_t x1, real_t x2, real_t c1, real_t c2)
307 *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
308 *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
311 #define REAL_CONST(A) ((real_t)(A))
312 #define COEF_CONST(A) ((real_t)(A))
313 #define Q2_CONST(A) ((real_t)(A))
314 #define FRAC_CONST(A) ((real_t)(A)) /* pure fractional part */
316 #else /* Normal floating point operation */
318 typedef float real_t;
320 #define MUL_R(A,B) ((A)*(B))
321 #define MUL_C(A,B) ((A)*(B))
322 #define MUL_F(A,B) ((A)*(B))
323 #define MUL_Q2(A,B) ((A)*(B))
325 #define REAL_CONST(A) ((real_t)(A))
326 #define COEF_CONST(A) ((real_t)(A))
327 #define Q2_CONST(A) ((real_t)(A))
328 #define FRAC_CONST(A) ((real_t)(A)) /* pure fractional part */
330 /* Complex multiplication */
331 static INLINE void ComplexMult(real_t *y1, real_t *y2,
332 real_t x1, real_t x2, real_t c1, real_t c2)
334 *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
335 *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
338 /* rockbox: must be commented to build for non-FP
339 #if defined(_WIN32) && !defined(__MINGW32__)
340 #define HAS_LRINTF
341 static INLINE int lrintf(float f)
343 int i;
344 __asm
346 fld f
347 fistp i
349 return i;
351 #elif (defined(__i386__) && defined(__GNUC__))
352 #define HAS_LRINTF
353 // from http://www.stereopsis.com/FPU.html
354 static INLINE int lrintf(float f)
356 int i;
357 __asm__ __volatile__ (
358 "flds %1 \n\t"
359 "fistpl %0 \n\t"
360 : "=m" (i)
361 : "m" (f));
362 return i;
364 #endif
367 #ifdef __ICL /* only Intel C compiler has fmath ??? */
369 #include <mathf.h>
371 #define sin sinf
372 #define cos cosf
373 #define log logf
374 #define floor floorf
375 #define ceil ceilf
376 #define sqrt sqrtf
378 #else
380 #ifdef HAVE_LRINTF
381 # define HAS_LRINTF
382 # define _ISOC9X_SOURCE 1
383 # define _ISOC99_SOURCE 1
384 # define __USE_ISOC9X 1
385 # define __USE_ISOC99 1
386 #endif
388 #include <math.h>
390 #ifdef HAVE_SINF
391 # define sin sinf
392 #error
393 #endif
394 #ifdef HAVE_COSF
395 # define cos cosf
396 #endif
397 #ifdef HAVE_LOGF
398 # define log logf
399 #endif
400 #ifdef HAVE_EXPF
401 # define exp expf
402 #endif
403 #ifdef HAVE_FLOORF
404 # define floor floorf
405 #endif
406 #ifdef HAVE_CEILF
407 # define ceil ceilf
408 #endif
409 #ifdef HAVE_SQRTF
410 # define sqrt sqrtf
411 #endif
413 #endif
415 #endif
417 #ifndef HAS_LRINTF
418 /* standard cast */
419 #define lrintf(f) ((int32_t)(f))
420 #endif
422 typedef real_t complex_t[2];
423 #define RE(A) A[0]
424 #define IM(A) A[1]
427 /* common functions */
428 uint8_t cpu_has_sse(void);
429 uint32_t random_int(void);
430 #define wl_min_lzc(x) bs_generic(x, BS_LOG2|BS_0_0)
431 #ifdef FIXED_POINT
432 #define LOG2_MIN_INF REAL_CONST(-10000)
433 int32_t log2_int(uint32_t val);
434 int32_t log2_fix(uint32_t val);
435 uint32_t pow2_int(real_t val);
436 uint32_t pow2_fix(real_t val);
437 #endif
438 uint8_t get_sr_index(const uint32_t samplerate);
439 uint8_t max_pred_sfb(const uint8_t sr_index);
440 uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type,
441 const uint8_t is_short);
442 uint32_t get_sample_rate(const uint8_t sr_index);
443 int8_t can_decode_ot(const uint8_t object_type);
445 #ifndef FAAD_STATIC_ALLOC
446 /* Those should not be defined or used anymore */
447 #define faad_malloc(A) malloc(A)
448 #define faad_free(A) free(A)
449 #endif
451 //#define PROFILE
452 #ifdef PROFILE
453 static int64_t faad_get_ts()
455 __asm
457 rdtsc
460 #endif
462 #ifndef M_PI
463 #define M_PI 3.14159265358979323846
464 #endif
465 #ifndef M_PI_2 /* PI/2 */
466 #define M_PI_2 1.57079632679489661923
467 #endif
470 #ifdef __cplusplus
472 #endif
473 #endif