1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero 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.
14 **
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.
18 **
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
21 **
22 ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23 ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24 **
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27 **
28 ** $Id: common.h,v 1.77 2009/02/05 00:51:03 menno Exp $
29 **/
30 
31 #ifndef __COMMON_H__
32 #define __COMMON_H__
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #ifdef HAVE_CONFIG_H
39 #  include "config.h"
40 #endif
41 
42 #include "neaacdec.h"
43 
44 #if 1
45 #define INLINE __inline
46 #else
47 #define INLINE inline
48 #endif
49 
50 #if 0 //defined(_WIN32) && !defined(_WIN32_WCE)
51 #define ALIGN __declspec(align(16))
52 #else
53 #define ALIGN
54 #endif
55 
56 #ifndef max
57 #define max(a, b) (((a) > (b)) ? (a) : (b))
58 #endif
59 #ifndef min
60 #define min(a, b) (((a) < (b)) ? (a) : (b))
61 #endif
62 
63 /* COMPILE TIME DEFINITIONS */
64 
65 /* use double precision */
66 /* #define USE_DOUBLE_PRECISION */
67 /* use fixed point reals */
68 //#define FIXED_POINT
69 //#define BIG_IQ_TABLE
70 
71 /* Use if target platform has address generators with autoincrement */
72 //#define PREFER_POINTERS
73 
74 #ifdef _WIN32_WCE
75 #define FIXED_POINT
76 #endif
77 
78 #ifdef __BFIN__
79 #define FIXED_POINT
80 #endif
81 
82 #define ERROR_RESILIENCE
83 
84 
85 /* Allow decoding of MAIN profile AAC */
86 #define MAIN_DEC
87 /* Allow decoding of SSR profile AAC */
88 //#define SSR_DEC
89 /* Allow decoding of LTP profile AAC */
90 #define LTP_DEC
91 /* Allow decoding of LD profile AAC */
92 #define LD_DEC
93 /* Allow decoding of Digital Radio Mondiale (DRM) */
94 //#define DRM
95 //#define DRM_PS
96 
97 /* LD can't do without LTP */
98 #ifdef LD_DEC
99 #ifndef ERROR_RESILIENCE
100 #define ERROR_RESILIENCE
101 #endif
102 #ifndef LTP_DEC
103 #define LTP_DEC
104 #endif
105 #endif
106 
107 #define ALLOW_SMALL_FRAMELENGTH
108 
109 
110 // Define LC_ONLY_DECODER if you want a pure AAC LC decoder (independant of SBR_DEC and PS_DEC)
111 //#define LC_ONLY_DECODER
112 #ifdef LC_ONLY_DECODER
113   #undef LD_DEC
114   #undef LTP_DEC
115   #undef MAIN_DEC
116   #undef SSR_DEC
117   #undef DRM
118   #undef ALLOW_SMALL_FRAMELENGTH
119   #undef ERROR_RESILIENCE
120 #endif
121 
122 #define SBR_DEC
123 //#define SBR_LOW_POWER
124 #define PS_DEC
125 
126 #ifdef SBR_LOW_POWER
127 #undef PS_DEC
128 #endif
129 
130 /* FIXED POINT: No MAIN decoding */
131 #ifdef FIXED_POINT
132 # ifdef MAIN_DEC
133 #  undef MAIN_DEC
134 # endif
135 #endif // FIXED_POINT
136 
137 #ifdef DRM
138 # ifndef ALLOW_SMALL_FRAMELENGTH
139 #  define ALLOW_SMALL_FRAMELENGTH
140 # endif
141 # undef LD_DEC
142 # undef LTP_DEC
143 # undef MAIN_DEC
144 # undef SSR_DEC
145 #endif
146 
147 
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
155 
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
165 
166 
167 /* END COMPILE TIME DEFINITIONS */
168 
169 #if defined(_WIN32) && !defined(__MINGW32__)
170 
171 #include <stdlib.h>
172 
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 signed __int64 int64_t;
178 typedef signed __int32 int32_t;
179 typedef signed __int16 int16_t;
180 typedef signed __int8  int8_t;
181 typedef float float32_t;
182 
183 
184 #else
185 
186 #include <stdio.h>
187 #if HAVE_SYS_TYPES_H
188 # include <sys/types.h>
189 #endif
190 #if HAVE_SYS_STAT_H
191 # include <sys/stat.h>
192 #endif
193 #if STDC_HEADERS
194 # include <stdlib.h>
195 # include <stddef.h>
196 #else
197 # if HAVE_STDLIB_H
198 #  include <stdlib.h>
199 # endif
200 #endif
201 #if HAVE_STRING_H
202 # if !STDC_HEADERS && HAVE_MEMORY_H
203 #  include <memory.h>
204 # endif
205 # include <string.h>
206 #endif
207 #if HAVE_STRINGS_H
208 # include <strings.h>
209 #endif
210 #if HAVE_INTTYPES_H
211 # include <inttypes.h>
212 #else
213 # if HAVE_STDINT_H
214 #  include <stdint.h>
215 # else
216 /* we need these... */
217 #ifndef __TCS__
218 typedef unsigned long long uint64_t;
219 typedef signed long long int64_t;
220 #else
221 typedef unsigned long uint64_t;
222 typedef signed long int64_t;
223 #endif
224 typedef unsigned long uint32_t;
225 typedef unsigned short uint16_t;
226 typedef unsigned char uint8_t;
227 typedef signed long int32_t;
228 typedef signed short int16_t;
229 typedef signed char int8_t;
230 # endif
231 #endif
232 #if HAVE_UNISTD_H
233 //# include <unistd.h>
234 #endif
235 
236 #ifndef HAVE_FLOAT32_T
237 typedef float float32_t;
238 #endif
239 
240 #if STDC_HEADERS
241 # include <string.h>
242 #else
243 # if !HAVE_STRCHR
244 #  define strchr index
245 #  define strrchr rindex
246 # endif
247 char *strchr(), *strrchr();
248 # if !HAVE_MEMCPY
249 #  define memcpy(d, s, n) bcopy((s), (d), (n))
250 #  define memmove(d, s, n) bcopy((s), (d), (n))
251 # endif
252 #endif
253 
254 #endif
255 
256 #ifdef WORDS_BIGENDIAN
257 #define ARCH_IS_BIG_ENDIAN
258 #endif
259 
260 /* FIXED_POINT doesn't work with MAIN and SSR yet */
261 #ifdef FIXED_POINT
262   #undef MAIN_DEC
263   #undef SSR_DEC
264 #endif
265 
266 
267 #if defined(FIXED_POINT)
268 
269   #include "fixed.h"
270 
271 #elif defined(USE_DOUBLE_PRECISION)
272 
273   typedef double real_t;
274 
275   #include <math.h>
276 
277   #define MUL_R(A,B) ((A)*(B))
278   #define MUL_C(A,B) ((A)*(B))
279   #define MUL_F(A,B) ((A)*(B))
280 
281   /* Complex multiplication */
ComplexMult(real_t * y1,real_t * y2,real_t x1,real_t x2,real_t c1,real_t c2)282   static INLINE void ComplexMult(real_t *y1, real_t *y2,
283       real_t x1, real_t x2, real_t c1, real_t c2)
284   {
285       *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
286       *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
287   }
288 
289   #define REAL_CONST(A) ((real_t)(A))
290   #define COEF_CONST(A) ((real_t)(A))
291   #define Q2_CONST(A) ((real_t)(A))
292   #define FRAC_CONST(A) ((real_t)(A)) /* pure fractional part */
293 
294 #else /* Normal floating point operation */
295 
296   typedef float real_t;
297 
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 
302   #define REAL_CONST(A) ((real_t)(A))
303   #define COEF_CONST(A) ((real_t)(A))
304   #define Q2_CONST(A) ((real_t)(A))
305   #define FRAC_CONST(A) ((real_t)(A)) /* pure fractional part */
306 
307   /* Complex multiplication */
ComplexMult(real_t * y1,real_t * y2,real_t x1,real_t x2,real_t c1,real_t c2)308   static INLINE void ComplexMult(real_t *y1, real_t *y2,
309       real_t x1, real_t x2, real_t c1, real_t c2)
310   {
311       *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
312       *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
313   }
314 
315 
316   #if defined(_WIN32) && !defined(__MINGW32__)
317     #define HAS_LRINTF
lrintf(float f)318     static INLINE int lrintf(float f)
319     {
320         int i;
321         __asm
322         {
323             fld   f
324             fistp i
325         }
326         return i;
327     }
328   #elif (defined(__i386__) && defined(__GNUC__) && \
329 	!defined(__CYGWIN__) && !defined(__MINGW32__))
330     #ifndef HAVE_LRINTF
331     #define HAS_LRINTF
332     // from http://www.stereopsis.com/FPU.html
lrintf(float f)333     static INLINE int lrintf(float f)
334     {
335         int i;
336         __asm__ __volatile__ (
337             "flds %1        \n\t"
338             "fistpl %0      \n\t"
339             : "=m" (i)
340             : "m" (f));
341         return i;
342     }
343     #endif /* HAVE_LRINTF */
344   #endif
345 
346 
347   #ifdef __ICL /* only Intel C compiler has fmath ??? */
348 
349     #include <mathf.h>
350 
351     #define sin sinf
352     #define cos cosf
353     #define log logf
354     #define floor floorf
355     #define ceil ceilf
356     #define sqrt sqrtf
357 
358   #else
359 
360 #ifdef HAVE_LRINTF
361 #  define HAS_LRINTF
362 #  define _ISOC9X_SOURCE 1
363 #  define _ISOC99_SOURCE 1
364 #  define __USE_ISOC9X   1
365 #  define __USE_ISOC99   1
366 #endif
367 
368     #include <math.h>
369 
370 #ifdef HAVE_SINF
371 #  define sin sinf
372 #error
373 #endif
374 #ifdef HAVE_COSF
375 #  define cos cosf
376 #endif
377 #ifdef HAVE_LOGF
378 #  define log logf
379 #endif
380 #ifdef HAVE_EXPF
381 #  define exp expf
382 #endif
383 #ifdef HAVE_FLOORF
384 #  define floor floorf
385 #endif
386 #ifdef HAVE_CEILF
387 #  define ceil ceilf
388 #endif
389 #ifdef HAVE_SQRTF
390 #  define sqrt sqrtf
391 #endif
392 
393   #endif
394 
395 #endif
396 
397 #ifndef HAS_LRINTF
398 /* standard cast */
399 #define lrintf(f) ((int32_t)(f))
400 #endif
401 
402 typedef real_t complex_t[2];
403 #define RE(A) A[0]
404 #define IM(A) A[1]
405 
406 
407 /* common functions */
408 uint8_t cpu_has_sse(void);
409 uint32_t ne_rng(uint32_t *__r1, uint32_t *__r2);
410 uint32_t wl_min_lzc(uint32_t x);
411 #ifdef FIXED_POINT
412 #define LOG2_MIN_INF REAL_CONST(-10000)
413 int32_t log2_int(uint32_t val);
414 int32_t log2_fix(uint32_t val);
415 int32_t pow2_int(real_t val);
416 real_t pow2_fix(real_t val);
417 #endif
418 uint8_t get_sr_index(const uint32_t samplerate);
419 uint8_t max_pred_sfb(const uint8_t sr_index);
420 uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type,
421                     const uint8_t is_short);
422 uint32_t get_sample_rate(const uint8_t sr_index);
423 int8_t can_decode_ot(const uint8_t object_type);
424 
425 void *faad_malloc(size_t size);
426 void faad_free(void *b);
427 
428 //#define PROFILE
429 #ifdef PROFILE
faad_get_ts()430 static int64_t faad_get_ts()
431 {
432     __asm
433     {
434         rdtsc
435     }
436 }
437 #endif
438 
439 #ifndef M_PI
440 #define M_PI 3.14159265358979323846
441 #endif
442 #ifndef M_PI_2 /* PI/2 */
443 #define M_PI_2 1.57079632679489661923
444 #endif
445 
446 
447 #ifdef __cplusplus
448 }
449 #endif
450 #endif
451