genericStds.cpp revision 6792cf6361ff339e336287affb0bfe54bf6957a9
1 2/* ----------------------------------------------------------------------------------------------------------- 3Software 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 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 Description: - Generic memory, stdio, string, etc. function wrappers or 88 builtins. 89 - OS dependant function wrappers. 90 91******************************************************************************/ 92 93#define _CRT_SECURE_NO_WARNINGS 94 95#include "genericStds.h" 96 97#include <math.h> 98 99/* library info */ 100#define SYS_LIB_VL0 1 101#define SYS_LIB_VL1 3 102#define SYS_LIB_VL2 6 103#define SYS_LIB_TITLE "System Integration Library" 104#define SYS_LIB_BUILD_DATE __DATE__ 105#define SYS_LIB_BUILD_TIME __TIME__ 106 107 #include <stdlib.h> 108 #include <stdio.h> 109 #include <string.h> 110 #include <stdarg.h> 111 112 113/*************************************************************** 114 * memory allocation monitoring variables 115 ***************************************************************/ 116 117 118/* Include OS/System specific implementations. */ 119#if defined(__linux__) /* cppp replaced: elif */ 120 #include "linux/genericStds_linux.cpp" 121#endif 122 123 124#if !(defined(USE_BUILTIN_STRING_FUNCTIONS) || defined(__SYMBIAN32__)) 125#include <string.h> 126#include <stdlib.h> 127#include <stdio.h> 128 129#ifndef FUNCTION_FDKprintf 130void FDKprintf( const char* szFmt, ...) { 131 va_list ap; 132 va_start(ap, szFmt); 133 vprintf(szFmt, ap); 134 va_end(ap); 135#ifdef ARCH_WA_FLUSH_CONSOLE 136 fflush(stdout); 137#endif 138} 139#endif 140 141#ifndef FUNCTION_FDKprintfErr 142void FDKprintfErr( const char* szFmt, ...) { 143 va_list ap; 144 va_start(ap, szFmt); 145#if defined(ARCH_WA_SLOWCON) 146 vprintf(szFmt, ap); 147#else 148 vfprintf(stderr, szFmt, ap); 149#endif 150 va_end(ap); 151#ifdef ARCH_WA_FLUSH_CONSOLE 152 fflush(stderr); 153#endif 154} 155#endif 156 157int FDKgetchar(void) { return getchar(); } 158 159INT FDKfprintf(FDKFILE *stream, const char *format, ...) { 160 INT chars = 0; 161 va_list ap; 162 va_start(ap, format); 163 chars += vfprintf((FILE*)stream, format, ap); 164 va_end(ap); 165 return chars; 166} 167 168#ifndef FUNCTION_FDKsprintf 169INT FDKsprintf(char *str, const char *format, ...) { 170 INT chars = 0; 171 va_list ap; 172 va_start(ap, format); 173 chars += vsprintf(str, format, ap); 174 va_end(ap); 175 return chars; 176} 177#endif 178 179#else 180 181void FDKprintf( const char* szFmt, ...) { /* stub! */; } 182void FDKprintfErr( const char* szFmt, ...) { /* stub! */; } 183INT FDKfprintf(FILE *stream, const char *format, ...) { /*stub ! */; } 184INT FDKsprintf(char *str, const char *format, ...) { /* stub! */; } 185 186#endif 187 188/************************************************************************************************/ 189 190 191const char *FDKstrchr(const char *s, INT c) { return strchr(s, c); } 192const char *FDKstrstr(const char *haystack, const char *needle) { return strstr(haystack, needle); } 193#ifndef FUNCTION_FDKstrcpy 194char *FDKstrcpy(char *dest, const char *src) { return strcpy(dest, src); } 195#endif 196char *FDKstrncpy(char *dest, const char *src, UINT n) { return strncpy(dest, src, n); } 197 198/************************************************************************* 199 * DYNAMIC MEMORY management (heap) 200 *************************************************************************/ 201 202#ifndef FUNCTION_FDKcalloc 203void *FDKcalloc (const UINT n, const UINT size) 204{ 205 void* ptr; 206 207 ptr = calloc(n, size); 208 209 return ptr; 210} 211#endif 212 213#ifndef FUNCTION_FDKmalloc 214void *FDKmalloc (const UINT size) 215{ 216 void* ptr; 217 218 ptr = malloc(size); 219 220 return ptr; 221} 222#endif 223 224#ifndef FUNCTION_FDKfree 225void FDKfree (void *ptr) 226{ 227 /* FDKprintf("f, heapSize: %d\n", heapSizeCurr); */ 228 free((INT*)ptr); 229} 230#endif 231 232#ifndef FUNCTION_FDKaalloc 233void *FDKaalloc(const UINT size, const UINT alignment) 234{ 235 void *addr, *result=NULL; 236 addr = FDKcalloc(1, size + alignment + sizeof(void*)); /* Malloc and clear memory. */ 237 238 if (addr!=NULL) 239 { 240 result = ALIGN_PTR((unsigned char*)addr + sizeof(void*)); /* Get aligned memory base address. */ 241 *(((void**)result) - 1) = addr; /* Save malloc'ed memory pointer. */ 242 } 243 244 return result; /* Return aligned address. */ 245} 246#endif 247 248#ifndef FUNCTION_FDKafree 249void FDKafree (void *ptr) 250{ 251 void *addr; 252 addr = *(((void**)ptr)-1); /* Get pointer to malloc'ed memory. */ 253 FDKfree(addr); /* Free malloc'ed memory area. */ 254} 255#endif 256 257 258#ifndef FUNCTION_FDKcalloc_L 259 260/*--------------------------------------------------------------------------* 261 * DATA MEMORY L1/L2 (fallback) 262 *--------------------------------------------------------------------------*/ 263 264 265 266/*--------------------------------------------------------------------------* 267 * FDKcalloc_L 268 *--------------------------------------------------------------------------*/ 269void *FDKcalloc_L(const UINT dim, const UINT size, MEMORY_SECTION s) 270{ 271 int a_size; 272 273 if (s == SECT_DATA_EXTERN) 274 goto fallback; 275 276 a_size = ((dim*size+3)&0xfffffffc); /* force 4 byte alignment (1111 .... 1111 1100) */ 277 278 279 280 281 282 //printf("Warning, out of internal memory\n"); 283 284fallback: 285 return FDKcalloc(dim, size); 286} 287#endif /* FUNCTION_FDKcalloc_L */ 288 289#ifndef FUNCTION_FDKfree_L 290void FDKfree_L (void *p) 291{ 292 293 FDKfree(p); 294} 295#endif /* FUNCTION_FDKfree_L */ 296 297#ifndef FUNCTION_FDKaalloc_L 298void *FDKaalloc_L(const UINT size, const UINT alignment, MEMORY_SECTION s) 299{ 300 void *addr, *result=NULL; 301 addr = FDKcalloc_L(1, size + alignment + sizeof(void*), s); /* Malloc and clear memory. */ 302 303 if (addr!=NULL) 304 { 305 result = ALIGN_PTR((unsigned char *)addr + sizeof(void*)); /* Get aligned memory base address. */ 306 *(((void**)result) - 1) = addr; /* Save malloc'ed memory pointer. */ 307 } 308 309 return result; /* Return aligned address. */ 310} 311#endif 312 313#ifndef FUNCTION_FDKafree_L 314void FDKafree_L (void *ptr) 315{ 316 void *addr; 317 318 addr = *(((void**)ptr)-1); /* Get pointer to malloc'ed memory. */ 319 FDKfree_L(addr); /* Free malloc'ed memory area. */ 320} 321#endif 322 323 324 325/*--------------------------------------------------------------------------------------- 326 * FUNCTION: FDKmemcpy 327 * DESCRIPTION: - copies memory from "src" to "dst" with length "size" bytes 328 * - compiled with FDK_DEBUG will give you warnings 329 *---------------------------------------------------------------------------------------*/ 330void FDKmemcpy(void *dst, const void *src, const UINT size) 331{ 332 333 /* do the copy */ 334 memcpy(dst, src, size); 335} 336 337void FDKmemmove(void *dst, const void *src, const UINT size) { memmove(dst, src, size); } 338void FDKmemset(void *memPtr, const INT value, const UINT size) { memset(memPtr, value, size); } 339void FDKmemclear(void *memPtr, const UINT size) { FDKmemset(memPtr,0,size); } 340UINT FDKstrlen(const char *s) { return (UINT)strlen(s); } 341 342/* Compare function wrappers */ 343INT FDKmemcmp(const void *s1, const void *s2, const UINT size) { return memcmp(s1, s2, size); } 344INT FDKstrcmp(const char *s1, const char *s2) { return strcmp(s1, s2); } 345INT FDKstrncmp(const char *s1, const char *s2, const UINT size) { return strncmp(s1, s2, size); } 346 347 348/* Math function wrappers. Only intended for compatibility, not to be highly optimized. */ 349 350INT FDKabs(INT j) { return abs(j); } 351double FDKfabs(double x) { return fabs(x); } 352double FDKpow(double x, double y) { return pow(x,y); } 353double FDKsqrt(double x) { return sqrt(x); } 354double FDKatan(double x) { return atan(x); } 355double FDKlog(double x) { return log(x); } 356double FDKsin(double x) { return sin(x); } 357double FDKcos(double x) { return cos(x); } 358double FDKexp(double x) { return exp(x); } 359double FDKatan2(double y, double x) { return atan2(y, x); } 360double FDKacos(double x) { return acos(x); } 361double FDKtan(double x) { return tan(x); } 362double FDKfloor(double x) { return floor(x); } 363double FDKceil(double x) { return ceil(x); } 364 365INT FDKatoi(const char *nptr) { return atoi(nptr); } 366long FDKatol(const char *nptr) { return atol(nptr); } 367float FDKatof(const char *nptr) { return (float)atof(nptr); } 368 369 370/* ==================== FILE I/O ====================== */ 371 372#if !defined(FUNCTION_FDKfopen) 373FDKFILE *FDKfopen(const char *filename, const char *mode) { return fopen(filename, mode); } 374#endif 375#if !defined(FUNCTION_FDKfclose) 376INT FDKfclose(FDKFILE *fp) { return fclose((FILE*)fp);} 377#endif 378#if !defined(FUNCTION_FDKfseek) 379INT FDKfseek(FDKFILE *fp, LONG OFFSET, int WHENCE) { return fseek((FILE*)fp, OFFSET, WHENCE);} 380#endif 381#if !defined(FUNCTION_FDKftell) 382INT FDKftell(FDKFILE *fp) { return ftell((FILE*)fp); } 383#endif 384#if !defined(FUNCTION_FDKfflush) 385INT FDKfflush(FDKFILE *fp) { return fflush((FILE*)fp); } 386#endif 387const INT FDKSEEK_SET = SEEK_SET; 388const INT FDKSEEK_CUR = SEEK_CUR; 389const INT FDKSEEK_END = SEEK_END; 390 391#if !defined(FUNCTION_FDKfwrite) 392UINT FDKfwrite(void *ptrf, INT size, UINT nmemb, FDKFILE *fp) { return fwrite(ptrf, size, nmemb, (FILE*)fp); } 393#endif 394#if !defined(FUNCTION_FDKfread) 395UINT FDKfread(void *dst, INT size, UINT nmemb, FDKFILE *fp) { return fread(dst, size, nmemb, (FILE*)fp); } 396#endif 397#if !defined(FUNCTION_FDKfgets) 398char* FDKfgets(void *dst, INT size, FDKFILE *fp) { return fgets((char *)dst, size, (FILE*)fp); } 399#endif 400#if !defined(FUNCTION_FDKrewind) 401void FDKrewind(FDKFILE *fp) { FDKfseek((FILE*)fp,0,FDKSEEK_SET); } 402#endif 403 404 405UINT FDKfwrite_EL(void *ptrf, INT size, UINT nmemb, FDKFILE *fp) { 406 407 if (IS_LITTLE_ENDIAN()) { 408 FDKfwrite(ptrf, size, nmemb, fp); 409 } else { 410 UINT n; 411 INT s; 412 413 UCHAR *ptr = (UCHAR*) ptrf; 414 415 for (n=0; n<nmemb; n++) { 416 for (s=size-1; s>=0; s--) { 417 //FDKprintf("char = %c\n", (char)*(ptr+s)); 418 FDKfwrite(ptr + s, 1, 1, fp); 419 } 420 ptr = ptr + size; 421 } 422 } 423 return nmemb; 424} 425 426 427UINT FDKfread_EL(void *dst, INT size, UINT nmemb, FDKFILE *fp) { 428 UINT n, s0, s1, err; 429 UCHAR tmp, *ptr; 430 UCHAR tmp24[3]; 431 432 /* Enforce alignment of 24 bit data. */ 433 if (size == 3) { 434 ptr = (UCHAR*)dst; 435 err = 0; 436 for (n=0; n<nmemb; n++) { 437 if ((err = FDKfread(tmp24, 1, 3, fp)) != 3) { 438 return err; 439 } 440 *ptr++ = tmp24[0]; 441 *ptr++ = tmp24[1]; 442 *ptr++ = tmp24[2]; 443 /* Sign extension */ 444 if (tmp24[2] & 0x80) { 445 *ptr++ = 0xff; 446 } else { 447 *ptr++ = 0; 448 } 449 } 450 err = nmemb; 451 size = sizeof(LONG); 452 } else { 453 if ((err = FDKfread(dst, size, nmemb, fp)) != nmemb) { 454 return err; 455 } 456 } 457 if (!IS_LITTLE_ENDIAN() && size > 1) { 458 ptr = (UCHAR*)dst; 459 for (n=0; n<nmemb; n++) { 460 for (s0=0, s1=size-1; s0 < s1; s0++, s1--) { 461 tmp = ptr[s0]; 462 ptr[s0] = ptr[s1]; 463 ptr[s1] = tmp; 464 } 465 ptr += size; 466 } 467 } 468 return err; 469} 470 471INT FDKfeof(FDKFILE *fp) { return feof((FILE*)fp); } 472 473/* Global initialization/cleanup */ 474 475#if defined(_DEBUG) && defined(_WIN32) && !defined(_WIN32_WCE) 476 #define _CRTDBG_MAP_ALLOC 477 #include <crtdbg.h> 478#endif 479 480 481 482 483void FDKprintDisclaimer(void) 484{ 485 FDKprintf( 486 "This program is protected by copyright law and international treaties.\n" \ 487 "Any reproduction or distribution of this program, or any portion\n" \ 488 "of it, may result in severe civil and criminal penalties, and will be\n" \ 489 "prosecuted to the maximum extent possible under law.\n\n"); 490} 491 492