1 
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4 
5 � Copyright  1995 - 2013 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6   All rights reserved.
7 
8  1.    INTRODUCTION
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17 
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24 
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28 
29 2.    COPYRIGHT LICENSE
30 
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33 
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36 
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41 
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44 
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47 
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52 
53 3.    NO PATENT LICENSE
54 
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58 
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61 
62 4.    DISCLAIMER
63 
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72 
73 5.    CONTACT INFORMATION
74 
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79 
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83 
84 /**************************  Fraunhofer IIS FDK SysLib  **********************
85 
86    Author(s):
87 
88 ******************************************************************************/
89 
90 /** \file   genericStds.h
91     \brief  Generic Run-Time Support function wrappers and heap allocation monitoring.
92  */
93 
94 #if !defined(__GENERICSTDS_H__)
95 #define __GENERICSTDS_H__
96 
97 #include "machine_type.h"
98 
99 
100 /* Always increase verbosity of memory allocation in case of a debug built. DEBUG is defined globally in that case. */
101 #if defined(DEBUG) || defined(FDK_DEBUG)
102 //#define MEMORY_MEASUREMENT
103 #endif
104 
105 #ifndef M_PI
106   #define M_PI   3.14159265358979323846  /*! Pi. Only used in example projects. */
107 #endif
108 
109 
110 /* #define _CRT_SECURE_NO_DEPRECATE */
111 
112 
113 /**
114  * Identifiers for various memory locations. They are used along with memory allocation
115  * functions like FDKcalloc_L() to specify the requested memory's location.
116  */
117 typedef enum {
118   /* Internal */
119   SECT_DATA_L1 = 0x2000,
120   SECT_DATA_L2,
121   SECT_DATA_L1_A,
122   SECT_DATA_L1_B,
123   SECT_CONSTDATA_L1,
124 
125   /* External */
126   SECT_DATA_EXTERN = 0x4000,
127   SECT_CONSTDATA_EXTERN
128 
129 } MEMORY_SECTION;
130 
131 
132 /**
133  * The H_ prefix indicates header file version, the C_* prefix indicates the corresponding
134  * object version.
135  *
136  * Declaring memory areas requires to specify a unique name and a data type. Use the H_ macro
137  * for this purpose inside a header file.
138  *
139  * For defining a memory area your require additionally one or two sizes, depending if the
140  * memory should be organized into one or two dimensions.
141  *
142  * The macros containing the keyword AALLOC instead of ALLOC also do take care of returning
143  * aligned memory addresses (beyond the natural alignment of its type). The preprocesor macro
144  * ::ALIGNMENT_DEFAULT indicates the aligment to be used (this is hardware specific).
145  *
146  * The _L suffix indicates that the memory will be located in a specific section. This is
147  * useful to allocate critical memory section into fast internal SRAM for example.
148  *
149  */
150 
151 #define H_ALLOC_MEM(name,type)          type * Get ## name(int n=0); void Free ## name(type** p); \
152                                         UINT GetRequiredMem ## name(void);
153 
154 /** See #H_ALLOC_MEM for description. */
155 #define H_ALLOC_MEM_OVERLAY(name,type)  type * Get ## name(int n=0); void Free ## name(type** p); \
156                                         UINT GetRequiredMem ## name(void);
157 
158 
159   /** See #H_ALLOC_MEM for description. */
160   #define C_ALLOC_MEM(name,type,num) \
161     type * Get ## name(int n) { FDK_ASSERT((n) == 0); return ((type*)FDKcalloc(num, sizeof(type))); } \
162     void Free ## name(type** p) { if (p != NULL) { FDKfree(*p); *p=NULL; } } \
163     UINT GetRequiredMem ## name(void) { return ALGN_SIZE_EXTRES((num) * sizeof(type)); }
164 
165   /** See #H_ALLOC_MEM for description. */
166   #define C_ALLOC_MEM_STATIC(name,type,num) \
167     static type * Get ## name(int n) { FDK_ASSERT((n) == 0); return ((type*)FDKcalloc(num, sizeof(type))); } \
168     static void Free ## name(type** p) { if (p != NULL) { FDKfree(*p); *p=NULL; } } \
169     static UINT GetRequiredMem ## name(void) { return ALGN_SIZE_EXTRES((num) * sizeof(type)); }
170 
171   /** See #H_ALLOC_MEM for description. */
172   #define C_ALLOC_MEM2(name,type,n1,n2) \
173     type * Get ## name (int n) { FDK_ASSERT((n) < (n2)); return ((type*)FDKcalloc(n1, sizeof(type))); } \
174     void Free ## name(type** p) { if (p != NULL) { FDKfree(*p); *p=NULL; } } \
175     UINT GetRequiredMem ## name(void) { return ALGN_SIZE_EXTRES((n1) * sizeof(type)) * (n2); }
176 
177   /** See #H_ALLOC_MEM for description. */
178   #define C_AALLOC_MEM(name,type,num) \
179     type * Get ## name(int n) { FDK_ASSERT((n) == 0); return ((type*)FDKaalloc((num)*sizeof(type), ALIGNMENT_DEFAULT)); } \
180     void Free ## name(type** p) { if (p != NULL) { FDKafree(*p); *p=NULL; } } \
181     UINT GetRequiredMem ## name(void) { return ALGN_SIZE_EXTRES((num) * sizeof(type) + ALIGNMENT_DEFAULT + sizeof(void *)); }
182 
183   /** See #H_ALLOC_MEM for description. */
184   #define C_AALLOC_MEM2(name,type,n1,n2) \
185     type * Get ## name (int n) { FDK_ASSERT((n) < (n2)); return ((type*)FDKaalloc((n1)*sizeof(type), ALIGNMENT_DEFAULT)); } \
186     void Free ## name(type** p) { if (p != NULL) { FDKafree(*p); *p=NULL; } } \
187     UINT GetRequiredMem ## name(void) { return ALGN_SIZE_EXTRES((n1) * sizeof(type) + ALIGNMENT_DEFAULT + sizeof(void *)) * (n2); }
188 
189   /** See #H_ALLOC_MEM for description. */
190   #define C_ALLOC_MEM_L(name,type,num,s) \
191     type * Get ## name(int n) { FDK_ASSERT((n) == 0); return ((type*)FDKcalloc_L(num, sizeof(type), s)); } \
192     void Free ## name(type** p) { if (p != NULL) { FDKfree_L(*p); *p=NULL; } } \
193     UINT GetRequiredMem ## name(void) { return ALGN_SIZE_EXTRES((num) * sizeof(type)); }
194 
195   /** See #H_ALLOC_MEM for description. */
196   #define C_ALLOC_MEM2_L(name,type,n1,n2,s) \
197     type * Get ## name (int n) { FDK_ASSERT((n) < (n2)); return (type*)FDKcalloc_L(n1, sizeof(type), s); } \
198     void Free ## name(type** p) { if (p != NULL) { FDKfree_L(*p); *p=NULL; } } \
199     UINT GetRequiredMem ## name(void) { return ALGN_SIZE_EXTRES((n1) * sizeof(type)) * (n2); }
200 
201   /** See #H_ALLOC_MEM for description. */
202   #define C_AALLOC_MEM_L(name,type,num,s) \
203     type * Get ## name(int n) { FDK_ASSERT((n) == 0); return ((type*)FDKaalloc_L((num)*sizeof(type), ALIGNMENT_DEFAULT, s)); } \
204     void Free ## name(type** p) { if (p != NULL) { FDKafree_L(*p); *p=NULL; } } \
205     UINT GetRequiredMem ## name(void) { return ALGN_SIZE_EXTRES((num) * sizeof(type) + ALIGNMENT_DEFAULT + sizeof(void *)); }
206 
207   /** See #H_ALLOC_MEM for description. */
208   #define C_AALLOC_MEM2_L(name,type,n1,n2,s) \
209     type * Get ## name (int n) { FDK_ASSERT((n) < (n2)); return ((type*)FDKaalloc_L((n1)*sizeof(type), ALIGNMENT_DEFAULT, s)); } \
210     void Free ## name(type** p) { if (p != NULL) { FDKafree_L(*p); *p=NULL; } } \
211     UINT GetRequiredMem ## name(void) { return ALGN_SIZE_EXTRES((n1) * sizeof(type) + ALIGNMENT_DEFAULT + sizeof(void *)) * (n2); }
212 
213 /** See #H_ALLOC_MEM_OVERLAY for description. */
214 
215 
216   #define C_ALLOC_MEM_OVERLAY(name,type,num,sect,tag) C_AALLOC_MEM_L(name,type,num,sect)
217 
218 
219    #define C_AALLOC_SCRATCH_START(name,type,n) \
220      type _ ## name[(n)+(ALIGNMENT_DEFAULT+sizeof(type)-1)]; \
221      type * name = (type*)ALIGN_PTR(_ ## name); \
222 
223    #define C_ALLOC_SCRATCH_START(name,type,n) \
224      type name[n];
225 
226    #define C_AALLOC_SCRATCH_END(name,type,n)
227    #define C_ALLOC_SCRATCH_END(name,type,n)
228 
229 
230 /*--------------------------------------------
231  * Runtime support declarations
232  *---------------------------------------------*/
233 #ifdef __cplusplus
234 extern "C" {
235 #endif
236 
237 /** printf() using stdout. If ::ARCH_WA_FLUSH_CONSOLE defined, a flush is  done additionally after printf(). */
238 void FDKprintf    ( const char* szFmt, ...);
239 
240 /** printf() using stderr. If ::ARCH_WA_FLUSH_CONSOLE defined, a flush is done additionally after printf(). */
241 void FDKprintfErr ( const char* szFmt, ...);
242 
243 /** Wrapper for <stdio.h>'s getchar(). */
244 int FDKgetchar(void);
245 
246 INT  FDKfprintf(void  *stream,  const  char *format, ...);
247 INT  FDKsprintf(char *str, const char *format, ...);
248 
249 
250 
251 const char *FDKstrchr(const char *s, INT c);
252 const char *FDKstrstr(const char *haystack, const char *needle);
253 char *FDKstrcpy(char *dest, const char *src);
254 char *FDKstrncpy(char *dest, const char *src, const UINT n);
255 
256 #define FDK_MAX_OVERLAYS 8   /**< Maximum number of memory overlays. */
257 
258 
259 void *FDKcalloc (const UINT n, const UINT size);
260 void *FDKmalloc (const UINT size);
261 void  FDKfree   (void *ptr);
262 
263 /**
264  *  Allocate and clear an aligned memory area. Use FDKafree() instead of FDKfree() for these memory areas.
265  *
266  * \param size       Size of requested memory in bytes.
267  * \param alignment  Alignment of requested memory in bytes.
268  * \return           Pointer to allocated memory.
269  */
270 void *FDKaalloc (const UINT size, const UINT alignment);
271 
272 /**
273  *  Free an aligned memory area.
274  *
275  * \param ptr  Pointer to be freed.
276  * \return     void
277  */
278 void FDKafree (void *ptr);
279 
280 
281 /**
282  *  Allocate memory in a specific memory section.
283  *  Requests can be made for internal or external memory. If internal memory is
284  *  requested, FDKcalloc_L() first tries to use L1 memory, which sizes are defined
285  *  by ::DATA_L1_A_SIZE and ::DATA_L1_B_SIZE. If no L1 memory is available, then
286  *  FDKcalloc_L() tries to use L2 memory. If that fails as well, the requested
287  *  memory is allocated at an extern location using the fallback FDKcalloc().
288  *
289  * \param n     See MSDN documentation on calloc().
290  * \param size  See MSDN documentation on calloc().
291  * \param s     Memory section.
292  * \return      See MSDN documentation on calloc().
293  */
294 void *FDKcalloc_L(const UINT n, const UINT size, MEMORY_SECTION s);
295 
296 /**
297  *  Allocate aligned memory in a specific memory section.
298  *  See FDKcalloc_L() description for details - same applies here.
299  */
300 void *FDKaalloc_L(const UINT size, const UINT alignment, MEMORY_SECTION s);
301 
302 /**
303  *  Free memory that was allocated in a specific memory section.
304  */
305 void  FDKfree_L(void *ptr);
306 
307 /**
308  *  Free aligned memory that was allocated in a specific memory section.
309  */
310 void  FDKafree_L(void *ptr);
311 
312 
313 /**
314  * Copy memory. Source and destination memory must not overlap.
315  * Either use implementation from a Standard Library, or, if no Standard Library
316  * is available, a generic implementation.
317  * The define ::USE_BUILTIN_MEM_FUNCTIONS in genericStds.cpp controls what to use.
318  * The function arguments correspond to the standard memcpy(). Please see MSDN
319  * documentation for details on how to use it.
320  */
321 void FDKmemcpy(void *dst, const void *src, const UINT size);
322 
323 /**
324  * Copy memory. Source and destination memory are allowed to overlap.
325  * Either use implementation from a Standard Library, or, if no Standard Library
326  * is available, a generic implementation.
327  * The define ::USE_BUILTIN_MEM_FUNCTIONS in genericStds.cpp controls what to use.
328  * The function arguments correspond to the standard memmove(). Please see MSDN
329  * documentation for details on how to use it.
330 */
331 void FDKmemmove(void *dst, const void *src, const UINT size);
332 
333 /**
334  * Clear memory.
335  * Either use implementation from a Standard Library, or, if no Standard Library
336  * is available, a generic implementation.
337  * The define ::USE_BUILTIN_MEM_FUNCTIONS in genericStds.cpp controls what to use.
338  * The function arguments correspond to the standard memclear(). Please see MSDN
339  * documentation for details on how to use it.
340 */
341 void FDKmemclear(void *memPtr, const UINT size);
342 
343 /**
344  * Fill memory with values.
345  * The function arguments correspond to the standard memset(). Please see MSDN
346  * documentation for details on how to use it.
347  */
348 void FDKmemset(void *memPtr, const INT value, const UINT size);
349 
350 /* Compare function wrappers */
351 INT   FDKmemcmp(const void *s1, const void *s2, const UINT size);
352 INT   FDKstrcmp(const char *s1, const char *s2);
353 INT   FDKstrncmp(const char *s1, const char *s2, const UINT size);
354 
355 UINT  FDKstrlen(const char *s);
356 
357 #define FDKmax(a,b) ( (a) > (b) ? (a):(b))
358 #define FDKmin(a,b) ( (a) < (b) ? (a):(b))
359 
360 #define FDK_INT_MAX ((INT)0x7FFFFFFF)
361 #define FDK_INT_MIN ((INT)0x80000000)
362 
363 /* Math function wrappers. Only intended for compatibility, not to be highly optimized. */
364 /* Used for debugging, dev code .. */
365 
366 INT FDKabs(INT j);
367 double FDKfabs(double x);
368 double FDKpow(double x, double y);
369 double FDKsqrt(double x);
370 double FDKatan(double x);
371 double FDKlog(double x);
372 double FDKsin(double x);
373 double FDKcos(double x);
374 double FDKexp(double x);
375 #define FDKlog2(a) (FDKlog(a)*1.442695041) /* log(2.0) = 1.442695041 */
376 #define FDKlog10(a) (FDKlog(a)*0.434294482) /* 1.0/log(10.0) = 0.434294482 */
377 double FDKatan2(double y, double x);
378 double FDKacos(double x);
379 double FDKtan(double x);
380 double FDKfloor(double x);
381 double FDKceil(double x);
382 INT   FDKatoi(const char *nptr);
383 long  FDKatol(const char *nptr);
384 float FDKatof(const char *nptr);
385 /* LONG LONG FDKatoll(const char *nptr); */
386 /* LONG LONG FDKatoq(const char *nptr); */
387 
388 
389 
390 /* FILE I/O */
391 
392 /*!
393  *  Check platform for endianess.
394  *
395  * \return  1 if platform is little endian, non-1 if platform is big endian.
396  */
397 #ifdef __cplusplus
398 inline
399 #else
400 static
401 #endif
IS_LITTLE_ENDIAN(void)402 int IS_LITTLE_ENDIAN(void) {
403   int __dummy = 1;
404   return ( *( (UCHAR*)(&(__dummy) ) ) );
405 }
406 
407 /*!
408  *  Convert input value to little endian format.
409  *
410  * \param val  Value to be converted. It may be in both big or little endian.
411  * \return     Value in little endian format.
412  */
413 #define TO_LITTLE_ENDIAN(val) \
414   ( (IS_LITTLE_ENDIAN()) ? \
415      (val) \
416    : ( (((val) & 0xff) << 24) || (((val) & 0xff00)<< 8) || (((val) & 0xff0000)>>8) || (((val) & 0xff000000) >> 24) ) )
417 
418 
419 /*!
420  * \fn     FDKFILE *FDKfopen(const char *filename, const char *mode);
421  *         Standard fopen() wrapper.
422  * \fn     INT FDKfclose(FDKFILE *FP);
423  *         Standard fclose() wrapper.
424  * \fn     INT FDKfseek(FDKFILE *FP, LONG OFFSET, int WHENCE);
425  *         Standard fseek() wrapper.
426  * \fn     INT FDKftell(FDKFILE *FP);
427  *         Standard ftell() wrapper.
428  * \fn     INT FDKfflush(FDKFILE *fp);
429  *         Standard fflush() wrapper.
430  * \fn     UINT FDKfwrite(void *ptrf, INT size, UINT nmemb, FDKFILE *fp);
431  *         Standard fwrite() wrapper.
432  * \fn     UINT FDKfread(void *dst, INT size, UINT nmemb, FDKFILE *fp);
433  *         Standard fread() wrapper.
434  */
435 typedef void FDKFILE;
436 extern const INT FDKSEEK_SET, FDKSEEK_CUR, FDKSEEK_END;
437 
438 FDKFILE *FDKfopen(const char *filename, const char *mode);
439 INT FDKfclose(FDKFILE *FP);
440 INT FDKfseek(FDKFILE *FP, LONG OFFSET, int WHENCE);
441 INT FDKftell(FDKFILE *FP);
442 INT FDKfflush(FDKFILE *fp);
443 UINT FDKfwrite(void *ptrf, INT size, UINT nmemb, FDKFILE *fp);
444 UINT FDKfread(void *dst, INT size, UINT nmemb, FDKFILE *fp);
445 char* FDKfgets(void *dst, INT size, FDKFILE *fp);
446 void FDKrewind(FDKFILE *fp);
447 INT FDKfeof(FDKFILE *fp);
448 
449 /**
450  * \brief        Write each member in little endian order. Convert automatically to host endianess.
451  * \param ptrf   Pointer to memory where to read data from.
452  * \param size   Size of each item to be written.
453  * \param nmemb  Number of items to be written.
454  * \param fp     File pointer of type FDKFILE.
455  * \return       Number of items read on success and fread() error on failure.
456  */
457 UINT FDKfwrite_EL(void *ptrf, INT size, UINT nmemb, FDKFILE *fp);
458 
459 /**
460  * \brief        Read variable of size "size" as little endian. Convert automatically to host endianess.
461  *                4-byte alignment is enforced for 24 bit data, at 32 bit full scale.
462  * \param dst    Pointer to memory where to store data into.
463  * \param size   Size of each item to be read.
464  * \param nmemb  Number of items to be read.
465  * \param fp     File pointer of type FDKFILE.
466  * \return       Number of items read on success and fread() error on failure.
467  */
468 UINT FDKfread_EL(void *dst, INT size, UINT nmemb, FDKFILE *fp);
469 
470 
471 /**
472  * \brief  Print FDK software disclaimer.
473  */
474 void FDKprintDisclaimer(void);
475 
476 #ifdef __cplusplus
477 }
478 #endif
479 
480 #endif /* __GENERICSTDS_H__ */
481