genericStds.h 19 KB

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