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 #include <stdint.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 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 #else
184
185 #include <stdio.h>
186 #if HAVE_SYS_TYPES_H
187 # include <sys/types.h>
188 #endif
189 #if HAVE_SYS_STAT_H
190 # include <sys/stat.h>
191 #endif
192 #if STDC_HEADERS
193 # include <stdlib.h>
194 # include <stddef.h>
195 #else
196 # if HAVE_STDLIB_H
197 # include <stdlib.h>
198 # endif
199 #endif
200 #if HAVE_STRING_H
201 # if !STDC_HEADERS && HAVE_MEMORY_H
202 # include <memory.h>
203 # endif
204 # include <string.h>
205 #endif
206 #if HAVE_STRINGS_H
207 # include <strings.h>
208 #endif
209 #if HAVE_INTTYPES_H
210 # include <inttypes.h>
211 #else
212 # if HAVE_STDINT_H
213 # include <stdint.h>
214 # else
215 /* we need these... */
216 #ifndef __TCS__
217 typedef unsigned long long uint64_t;
218 typedef signed long long int64_t;
219 #else
220 typedef unsigned long uint64_t;
221 typedef signed long int64_t;
222 #endif
223 typedef unsigned long uint32_t;
224 typedef unsigned short uint16_t;
225 typedef unsigned char uint8_t;
226 typedef signed long int32_t;
227 typedef signed short int16_t;
228 typedef signed char int8_t;
229 # endif
230 #endif
231 #if HAVE_UNISTD_H
232 //# include <unistd.h>
233 #endif
234
235 #ifndef HAVE_FLOAT32_T
236 typedef float float32_t;
237 #endif
238
239 #if STDC_HEADERS
240 # include <string.h>
241 #else
242 # if !HAVE_STRCHR
243 # define strchr index
244 # define strrchr rindex
245 # endif
246 char *strchr(), *strrchr();
247 # if !HAVE_MEMCPY
248 # define memcpy(d, s, n) bcopy((s), (d), (n))
249 # define memmove(d, s, n) bcopy((s), (d), (n))
250 # endif
251 #endif
252
253 #endif
254
255 #ifdef WORDS_BIGENDIAN
256 #define ARCH_IS_BIG_ENDIAN
257 #endif
258
259 /* FIXED_POINT doesn't work with MAIN and SSR yet */
260 #ifdef FIXED_POINT
261 #undef MAIN_DEC
262 #undef SSR_DEC
263 #endif
264
265
266 #if defined(FIXED_POINT)
267
268 #include "fixed.h"
269
270 #elif defined(USE_DOUBLE_PRECISION)
271
272 typedef double real_t;
273
274 #include <math.h>
275
276 #define MUL_R(A,B) ((A)*(B))
277 #define MUL_C(A,B) ((A)*(B))
278 #define MUL_F(A,B) ((A)*(B))
279
280 /* Complex multiplication */
ComplexMult(real_t * y1,real_t * y2,real_t x1,real_t x2,real_t c1,real_t c2)281 static INLINE void ComplexMult(real_t *y1, real_t *y2,
282 real_t x1, real_t x2, real_t c1, real_t c2)
283 {
284 *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
285 *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
286 }
287
288 #define REAL_CONST(A) ((real_t)(A))
289 #define COEF_CONST(A) ((real_t)(A))
290 #define Q2_CONST(A) ((real_t)(A))
291 #define FRAC_CONST(A) ((real_t)(A)) /* pure fractional part */
292
293 #else /* Normal floating point operation */
294
295 typedef float real_t;
296
297 #define MUL_R(A,B) ((A)*(B))
298 #define MUL_C(A,B) ((A)*(B))
299 #define MUL_F(A,B) ((A)*(B))
300
301 #define REAL_CONST(A) ((real_t)(A))
302 #define COEF_CONST(A) ((real_t)(A))
303 #define Q2_CONST(A) ((real_t)(A))
304 #define FRAC_CONST(A) ((real_t)(A)) /* pure fractional part */
305
306 /* Complex multiplication */
ComplexMult(real_t * y1,real_t * y2,real_t x1,real_t x2,real_t c1,real_t c2)307 static INLINE void ComplexMult(real_t *y1, real_t *y2,
308 real_t x1, real_t x2, real_t c1, real_t c2)
309 {
310 *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
311 *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
312 }
313
314
315 #if defined(_WIN32) && !defined(__MINGW32__)
316 //#define HAS_LRINTF
317 //static INLINE int lrintf(float f)
318 //{
319 // int i;
320 // __asm
321 // {
322 // fld f
323 // fistp i
324 // }
325 // return i;
326 //}
327 #elif (defined(__i386__) && defined(__GNUC__) && \
328 !defined(__CYGWIN__) && !defined(__MINGW32__))
329 #ifndef HAVE_LRINTF
330 #define HAS_LRINTF
331 // from http://www.stereopsis.com/FPU.html
lrintf(float f)332 static INLINE int lrintf(float f)
333 {
334 int i;
335 __asm__ __volatile__ (
336 "flds %1 \n\t"
337 "fistpl %0 \n\t"
338 : "=m" (i)
339 : "m" (f));
340 return i;
341 }
342 #endif /* HAVE_LRINTF */
343 #endif
344
345
346 #ifdef __ICL /* only Intel C compiler has fmath ??? */
347
348 #include <mathf.h>
349
350 #define sin sinf
351 #define cos cosf
352 #define log logf
353 #define floor floorf
354 #define ceil ceilf
355 #define sqrt sqrtf
356
357 #else
358
359 #ifdef HAVE_LRINTF
360 # define HAS_LRINTF
361 # define _ISOC9X_SOURCE 1
362 # define _ISOC99_SOURCE 1
363 # define __USE_ISOC9X 1
364 # define __USE_ISOC99 1
365 #endif
366
367 #include <math.h>
368
369 #ifdef HAVE_SINF
370 # define sin sinf
371 #error
372 #endif
373 #ifdef HAVE_COSF
374 # define cos cosf
375 #endif
376 #ifdef HAVE_LOGF
377 # define log logf
378 #endif
379 #ifdef HAVE_EXPF
380 # define exp expf
381 #endif
382 #ifdef HAVE_FLOORF
383 # define floor floorf
384 #endif
385 #ifdef HAVE_CEILF
386 # define ceil ceilf
387 #endif
388 #ifdef HAVE_SQRTF
389 # define sqrt sqrtf
390 #endif
391
392 #endif
393
394 #endif
395
396 #ifndef HAS_LRINTF
397 /* standard cast */
398 #define lrintf(f) ((int32_t)(f))
399 #endif
400
401 typedef real_t complex_t[2];
402 #define RE(A) A[0]
403 #define IM(A) A[1]
404
405
406 /* common functions */
407 uint8_t cpu_has_sse(void);
408 uint32_t ne_rng(uint32_t *__r1, uint32_t *__r2);
409 uint32_t wl_min_lzc(uint32_t x);
410 #ifdef FIXED_POINT
411 #define LOG2_MIN_INF REAL_CONST(-10000)
412 int32_t log2_int(uint32_t val);
413 int32_t log2_fix(uint32_t val);
414 int32_t pow2_int(real_t val);
415 real_t pow2_fix(real_t val);
416 #endif
417 uint8_t get_sr_index(const uint32_t samplerate);
418 uint8_t max_pred_sfb(const uint8_t sr_index);
419 uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type,
420 const uint8_t is_short);
421 uint32_t get_sample_rate(const uint8_t sr_index);
422 int8_t can_decode_ot(const uint8_t object_type);
423
424 void *faad_malloc(size_t size);
425 void faad_free(void *b);
426
427 //#define PROFILE
428 #ifdef PROFILE
faad_get_ts()429 static int64_t faad_get_ts()
430 {
431 __asm
432 {
433 rdtsc
434 }
435 }
436 #endif
437
438 #ifndef M_PI
439 #define M_PI 3.14159265358979323846
440 #endif
441 #ifndef M_PI_2 /* PI/2 */
442 #define M_PI_2 1.57079632679489661923
443 #endif
444
445
446 #ifdef __cplusplus
447 }
448 #endif
449 #endif
450