Imported Debian version 0.1.3.1
[deb_fdk-aac.git] / libSYS / src / genericStds.cpp
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 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 4
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
130 void 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
142 void 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
157 int FDKgetchar(void) { return getchar(); }
158
159 INT 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
169 INT 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
181 void FDKprintf( const char* szFmt, ...) { /* stub! */; }
182 void FDKprintfErr( const char* szFmt, ...) { /* stub! */; }
183 INT FDKfprintf(FILE *stream, const char *format, ...) { /*stub ! */; }
184 INT FDKsprintf(char *str, const char *format, ...) { /* stub! */; }
185
186 #endif
187
188 /************************************************************************************************/
189
190
191 const char *FDKstrchr(const char *s, INT c) { return strchr(s, c); }
192 const char *FDKstrstr(const char *haystack, const char *needle) { return strstr(haystack, needle); }
193 #ifndef FUNCTION_FDKstrcpy
194 char *FDKstrcpy(char *dest, const char *src) { return strcpy(dest, src); }
195 #endif
196 char *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
203 void *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
214 void *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
225 void FDKfree (void *ptr)
226 {
227 /* FDKprintf("f, heapSize: %d\n", heapSizeCurr); */
228 free((INT*)ptr);
229 }
230 #endif
231
232 #ifndef FUNCTION_FDKaalloc
233 void *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
249 void 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 *--------------------------------------------------------------------------*/
269 void *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
284 fallback:
285 return FDKcalloc(dim, size);
286 }
287 #endif /* FUNCTION_FDKcalloc_L */
288
289 #ifndef FUNCTION_FDKfree_L
290 void FDKfree_L (void *p)
291 {
292
293 FDKfree(p);
294 }
295 #endif /* FUNCTION_FDKfree_L */
296
297 #ifndef FUNCTION_FDKaalloc_L
298 void *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
314 void 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 *---------------------------------------------------------------------------------------*/
330 void FDKmemcpy(void *dst, const void *src, const UINT size)
331 {
332
333 /* do the copy */
334 memcpy(dst, src, size);
335 }
336
337 void FDKmemmove(void *dst, const void *src, const UINT size) { memmove(dst, src, size); }
338 void FDKmemset(void *memPtr, const INT value, const UINT size) { memset(memPtr, value, size); }
339 void FDKmemclear(void *memPtr, const UINT size) { FDKmemset(memPtr,0,size); }
340 UINT FDKstrlen(const char *s) { return (UINT)strlen(s); }
341
342 /* Compare function wrappers */
343 INT FDKmemcmp(const void *s1, const void *s2, const UINT size) { return memcmp(s1, s2, size); }
344 INT FDKstrcmp(const char *s1, const char *s2) { return strcmp(s1, s2); }
345 INT 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
350 INT FDKabs(INT j) { return abs(j); }
351 double FDKfabs(double x) { return fabs(x); }
352 double FDKpow(double x, double y) { return pow(x,y); }
353 double FDKsqrt(double x) { return sqrt(x); }
354 double FDKatan(double x) { return atan(x); }
355 double FDKlog(double x) { return log(x); }
356 double FDKsin(double x) { return sin(x); }
357 double FDKcos(double x) { return cos(x); }
358 double FDKexp(double x) { return exp(x); }
359 double FDKatan2(double y, double x) { return atan2(y, x); }
360 double FDKacos(double x) { return acos(x); }
361 double FDKtan(double x) { return tan(x); }
362 double FDKfloor(double x) { return floor(x); }
363 double FDKceil(double x) { return ceil(x); }
364
365 INT FDKatoi(const char *nptr) { return atoi(nptr); }
366 long FDKatol(const char *nptr) { return atol(nptr); }
367 float FDKatof(const char *nptr) { return (float)atof(nptr); }
368
369
370 /* ==================== FILE I/O ====================== */
371
372 #if !defined(FUNCTION_FDKfopen)
373 FDKFILE *FDKfopen(const char *filename, const char *mode) { return fopen(filename, mode); }
374 #endif
375 #if !defined(FUNCTION_FDKfclose)
376 INT FDKfclose(FDKFILE *fp) { return fclose((FILE*)fp);}
377 #endif
378 #if !defined(FUNCTION_FDKfseek)
379 INT FDKfseek(FDKFILE *fp, LONG OFFSET, int WHENCE) { return fseek((FILE*)fp, OFFSET, WHENCE);}
380 #endif
381 #if !defined(FUNCTION_FDKftell)
382 INT FDKftell(FDKFILE *fp) { return ftell((FILE*)fp); }
383 #endif
384 #if !defined(FUNCTION_FDKfflush)
385 INT FDKfflush(FDKFILE *fp) { return fflush((FILE*)fp); }
386 #endif
387 const INT FDKSEEK_SET = SEEK_SET;
388 const INT FDKSEEK_CUR = SEEK_CUR;
389 const INT FDKSEEK_END = SEEK_END;
390
391 #if !defined(FUNCTION_FDKfwrite)
392 UINT FDKfwrite(void *ptrf, INT size, UINT nmemb, FDKFILE *fp) { return fwrite(ptrf, size, nmemb, (FILE*)fp); }
393 #endif
394 #if !defined(FUNCTION_FDKfread)
395 UINT FDKfread(void *dst, INT size, UINT nmemb, FDKFILE *fp) { return fread(dst, size, nmemb, (FILE*)fp); }
396 #endif
397 #if !defined(FUNCTION_FDKfgets)
398 char* FDKfgets(void *dst, INT size, FDKFILE *fp) { return fgets((char *)dst, size, (FILE*)fp); }
399 #endif
400 #if !defined(FUNCTION_FDKrewind)
401 void FDKrewind(FDKFILE *fp) { FDKfseek((FILE*)fp,0,FDKSEEK_SET); }
402 #endif
403
404
405 UINT 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
427 UINT 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
471 INT 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
483 void 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