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