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.79 2015/01/26 17:48:53 knik 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 #ifdef DRM_SUPPORT
95 #define DRM
96 #define DRM_PS
97 #endif
98
99 /* LD can't do without LTP */
100 #ifdef LD_DEC
101 #ifndef ERROR_RESILIENCE
102 #define ERROR_RESILIENCE
103 #endif
104 #ifndef LTP_DEC
105 #define LTP_DEC
106 #endif
107 #endif
108
109 #define ALLOW_SMALL_FRAMELENGTH
110
111
112 // Define LC_ONLY_DECODER if you want a pure AAC LC decoder (independant of SBR_DEC and PS_DEC)
113 //#define LC_ONLY_DECODER
114 #ifdef LC_ONLY_DECODER
115 #undef LD_DEC
116 #undef LTP_DEC
117 #undef MAIN_DEC
118 #undef SSR_DEC
119 #undef DRM
120 #undef DRM_PS
121 #undef ALLOW_SMALL_FRAMELENGTH
122 #undef ERROR_RESILIENCE
123 #endif
124
125 #define SBR_DEC
126 //#define SBR_LOW_POWER
127 #define PS_DEC
128
129 #ifdef SBR_LOW_POWER
130 #undef PS_DEC
131 #endif
132
133 /* FIXED POINT: No MAIN decoding */
134 #ifdef FIXED_POINT
135 # ifdef MAIN_DEC
136 # undef MAIN_DEC
137 # endif
138 #endif // FIXED_POINT
139
140 #ifdef DRM
141 # ifndef ALLOW_SMALL_FRAMELENGTH
142 # define ALLOW_SMALL_FRAMELENGTH
143 # endif
144 # undef LD_DEC
145 # undef LTP_DEC
146 # undef MAIN_DEC
147 # undef SSR_DEC
148 #endif
149
150
151 #ifdef FIXED_POINT
152 #define DIV_R(A, B) (((int64_t)A << REAL_BITS)/B)
153 #define DIV_C(A, B) (((int64_t)A << COEF_BITS)/B)
154 #else
155 #define DIV_R(A, B) ((A)/(B))
156 #define DIV_C(A, B) ((A)/(B))
157 #endif
158
159 #ifndef SBR_LOW_POWER
160 #define qmf_t complex_t
161 #define QMF_RE(A) RE(A)
162 #define QMF_IM(A) IM(A)
163 #else
164 #define qmf_t real_t
165 #define QMF_RE(A) (A)
166 #define QMF_IM(A)
167 #endif
168
169
170 /* END COMPILE TIME DEFINITIONS */
171
172 #if defined(_WIN32) && !defined(__MINGW32__)
173
174 #include <stdlib.h>
175
176 typedef unsigned __int64 uint64_t;
177 typedef unsigned __int32 uint32_t;
178 typedef unsigned __int16 uint16_t;
179 typedef unsigned __int8 uint8_t;
180 typedef signed __int64 int64_t;
181 typedef signed __int32 int32_t;
182 typedef signed __int16 int16_t;
183 typedef signed __int8 int8_t;
184 typedef float float32_t;
185
186
187 #else
188
189 #include <stdio.h>
190 #if HAVE_SYS_TYPES_H
191 # include <sys/types.h>
192 #endif
193 #if HAVE_SYS_STAT_H
194 # include <sys/stat.h>
195 #endif
196 #if STDC_HEADERS
197 # include <stdlib.h>
198 # include <stddef.h>
199 #else
200 # if HAVE_STDLIB_H
201 # include <stdlib.h>
202 # endif
203 #endif
204 #if HAVE_STRING_H
205 # if !STDC_HEADERS && HAVE_MEMORY_H
206 # include <memory.h>
207 # endif
208 # include <string.h>
209 #endif
210 #if HAVE_STRINGS_H
211 # include <strings.h>
212 #endif
213 #if HAVE_INTTYPES_H
214 # include <inttypes.h>
215 #else
216 # if HAVE_STDINT_H
217 # include <stdint.h>
218 # else
219 /* we need these... */
220 #ifndef __TCS__
221 typedef unsigned long long uint64_t;
222 typedef signed long long int64_t;
223 #else
224 typedef unsigned long uint64_t;
225 typedef signed long int64_t;
226 #endif
227 typedef unsigned long uint32_t;
228 typedef unsigned short uint16_t;
229 typedef unsigned char uint8_t;
230 typedef signed long int32_t;
231 typedef signed short int16_t;
232 typedef signed char int8_t;
233 # endif
234 #endif
235 #if HAVE_UNISTD_H
236 //# include <unistd.h>
237 #endif
238
239 #ifndef HAVE_FLOAT32_T
240 typedef float float32_t;
241 #endif
242
243 #if STDC_HEADERS
244 # include <string.h>
245 #else
246 # if !HAVE_STRCHR
247 # define strchr index
248 # define strrchr rindex
249 # endif
250 char *strchr(), *strrchr();
251 # if !HAVE_MEMCPY
252 # define memcpy(d, s, n) bcopy((s), (d), (n))
253 # define memmove(d, s, n) bcopy((s), (d), (n))
254 # endif
255 #endif
256
257 #endif
258
259 #ifdef WORDS_BIGENDIAN
260 #define ARCH_IS_BIG_ENDIAN
261 #endif
262
263 /* FIXED_POINT doesn't work with MAIN and SSR yet */
264 #ifdef FIXED_POINT
265 #undef MAIN_DEC
266 #undef SSR_DEC
267 #endif
268
269
270 #if defined(FIXED_POINT)
271
272 #include "fixed.h"
273
274 #elif defined(USE_DOUBLE_PRECISION)
275
276 typedef double real_t;
277
278 #include <math.h>
279
280 #define MUL_R(A,B) ((A)*(B))
281 #define MUL_C(A,B) ((A)*(B))
282 #define MUL_F(A,B) ((A)*(B))
283
284 /* Complex multiplication */
ComplexMult(real_t * y1,real_t * y2,real_t x1,real_t x2,real_t c1,real_t c2)285 static INLINE void ComplexMult(real_t *y1, real_t *y2,
286 real_t x1, real_t x2, real_t c1, real_t c2)
287 {
288 *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
289 *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
290 }
291
292 #define REAL_CONST(A) ((real_t)(A))
293 #define COEF_CONST(A) ((real_t)(A))
294 #define Q2_CONST(A) ((real_t)(A))
295 #define FRAC_CONST(A) ((real_t)(A)) /* pure fractional part */
296
297 #else /* Normal floating point operation */
298
299 typedef float real_t;
300
301 #define MUL_R(A,B) ((A)*(B))
302 #define MUL_C(A,B) ((A)*(B))
303 #define MUL_F(A,B) ((A)*(B))
304
305 #define REAL_CONST(A) ((real_t)(A))
306 #define COEF_CONST(A) ((real_t)(A))
307 #define Q2_CONST(A) ((real_t)(A))
308 #define FRAC_CONST(A) ((real_t)(A)) /* pure fractional part */
309
310 /* Complex multiplication */
ComplexMult(real_t * y1,real_t * y2,real_t x1,real_t x2,real_t c1,real_t c2)311 static INLINE void ComplexMult(real_t *y1, real_t *y2,
312 real_t x1, real_t x2, real_t c1, real_t c2)
313 {
314 *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
315 *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
316 }
317
318
319 #if defined(_WIN32) && defined(_M_IX86) && !defined(__MINGW32__)
320 #ifndef HAVE_LRINTF
321 #define HAS_LRINTF
lrintf(float f)322 static INLINE int lrintf(float f)
323 {
324 int i;
325 __asm
326 {
327 fld f
328 fistp i
329 }
330 return i;
331 }
332 #endif /* HAVE_LRINTF */
333 #elif (defined(__i386__) && defined(__GNUC__) && \
334 !defined(__CYGWIN__) && !defined(__MINGW32__))
335 #ifndef HAVE_LRINTF
336 #define HAS_LRINTF
337 // from http://www.stereopsis.com/FPU.html
lrintf(float f)338 static INLINE int lrintf(float f)
339 {
340 int i;
341 __asm__ __volatile__ (
342 "flds %1 \n\t"
343 "fistpl %0 \n\t"
344 : "=m" (i)
345 : "m" (f));
346 return i;
347 }
348 #endif /* HAVE_LRINTF */
349 #endif
350
351
352 #ifdef __ICL /* only Intel C compiler has fmath ??? */
353
354 #include <mathf.h>
355
356 #define sin sinf
357 #define cos cosf
358 #define log logf
359 #define floor floorf
360 #define ceil ceilf
361 #define sqrt sqrtf
362
363 #else
364
365 #ifdef HAVE_LRINTF
366 # define HAS_LRINTF
367 # define _ISOC9X_SOURCE 1
368 # define _ISOC99_SOURCE 1
369 # define __USE_ISOC9X 1
370 # define __USE_ISOC99 1
371 #endif
372
373 #include <math.h>
374
375 #ifdef HAVE_SINF
376 # define sin sinf
377 #error
378 #endif
379 #ifdef HAVE_COSF
380 # define cos cosf
381 #endif
382 #ifdef HAVE_LOGF
383 # define log logf
384 #endif
385 #ifdef HAVE_EXPF
386 # define exp expf
387 #endif
388 #ifdef HAVE_FLOORF
389 # define floor floorf
390 #endif
391 #ifdef HAVE_CEILF
392 # define ceil ceilf
393 #endif
394 #ifdef HAVE_SQRTF
395 # define sqrt sqrtf
396 #endif
397
398 #endif
399
400 #endif
401
402 #ifndef HAS_LRINTF
403 /* standard cast */
404 #define lrintf(f) ((int32_t)(f))
405 #endif
406
407 typedef real_t complex_t[2];
408 #define RE(A) A[0]
409 #define IM(A) A[1]
410
411
412 /* common functions */
413 uint8_t cpu_has_sse(void);
414 uint32_t ne_rng(uint32_t *__r1, uint32_t *__r2);
415 uint32_t wl_min_lzc(uint32_t x);
416 #ifdef FIXED_POINT
417 #define LOG2_MIN_INF REAL_CONST(-10000)
418 int32_t log2_int(uint32_t val);
419 int32_t log2_fix(uint32_t val);
420 int32_t pow2_int(real_t val);
421 real_t pow2_fix(real_t val);
422 #endif
423 uint8_t get_sr_index(const uint32_t samplerate);
424 uint8_t max_pred_sfb(const uint8_t sr_index);
425 uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type,
426 const uint8_t is_short);
427 uint32_t get_sample_rate(const uint8_t sr_index);
428 int8_t can_decode_ot(const uint8_t object_type);
429
430 void *faad_malloc(size_t size);
431 void faad_free(void *b);
432
433 //#define PROFILE
434 #ifdef PROFILE
faad_get_ts()435 static int64_t faad_get_ts()
436 {
437 __asm
438 {
439 rdtsc
440 }
441 }
442 #endif
443
444 #ifndef M_PI
445 #define M_PI 3.14159265358979323846
446 #endif
447 #ifndef M_PI_2 /* PI/2 */
448 #define M_PI_2 1.57079632679489661923
449 #endif
450
451
452 #ifdef __cplusplus
453 }
454 #endif
455 #endif
456