Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / codecs / lib / codeclib.h
blobb7685ebbcbf1b0995797d52c2af93023babd22c0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #ifndef __CODECLIB_H__
23 #define __CODECLIB_H__
25 #include <inttypes.h>
26 #include <string.h>
27 #include "config.h"
28 #include "codecs.h"
29 #include "mdct.h"
30 #include "fft.h"
32 extern struct codec_api *ci;
33 extern size_t mem_ptr;
34 extern size_t bufsize;
35 extern unsigned char* mp3buf; /* The actual MP3 buffer from Rockbox */
36 extern unsigned char* mallocbuf; /* The free space after the codec in the codec buffer */
37 extern unsigned char* filebuf; /* The rest of the MP3 buffer */
39 /* Standard library functions that are used by the codecs follow here */
41 /* Get these functions 'out of the way' of the standard functions. Not doing
42 * so confuses the cygwin linker, and maybe others. These functions need to
43 * be implemented elsewhere */
44 #define malloc(x) codec_malloc(x)
45 #define calloc(x,y) codec_calloc(x,y)
46 #define realloc(x,y) codec_realloc(x,y)
47 #define free(x) codec_free(x)
48 #undef alloca
49 #define alloca(x) __builtin_alloca(x)
51 void* codec_malloc(size_t size);
52 void* codec_calloc(size_t nmemb, size_t size);
53 void* codec_realloc(void* ptr, size_t size);
54 void codec_free(void* ptr);
56 void *memcpy(void *dest, const void *src, size_t n);
57 void *memset(void *s, int c, size_t n);
58 int memcmp(const void *s1, const void *s2, size_t n);
59 void *memmove(void *s1, const void *s2, size_t n);
61 size_t strlen(const char *s);
62 char *strcpy(char *dest, const char *src);
63 char *strcat(char *dest, const char *src);
65 /* on some platforms strcmp() seems to be a tricky define which
66 * breaks if we write down strcmp's prototype */
67 #undef strcmp
68 int strcmp(const char *s1, const char *s2);
70 void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
72 /*MDCT library functions*/
73 /* -1- Tremor mdct */
74 extern void mdct_backward(int n, int32_t *in, int32_t *out);
75 /* -2- ffmpeg fft-based mdct */
76 extern void ff_imdct_half(unsigned int nbits, int32_t *output, const int32_t *input);
77 extern void ff_imdct_calc(unsigned int nbits, int32_t *output, const int32_t *input);
78 /*ffmpeg fft (can be used without mdct)*/
79 extern void ff_fft_calc_c(int nbits, FFTComplex *z);
81 #if !defined(CPU_ARM) || ARM_ARCH < 5
82 /* From libavutil/common.h */
83 extern const uint8_t bs_log2_tab[256] ICONST_ATTR;
84 extern const uint8_t bs_clz_tab[256] ICONST_ATTR;
85 #endif
87 #define BS_LOG2 0 /* default personality, equivalent floor(log2(x)) */
88 #define BS_CLZ 1 /* alternate personality, Count Leading Zeros */
89 #define BS_SHORT 2 /* input guaranteed not to exceed 16 bits */
90 #define BS_0_0 4 /* guarantee mapping of 0 input to 0 output */
92 /* Generic bit-scanning function, used to wrap platform CLZ instruction or
93 scan-and-lookup code, and to provide control over output for 0 inputs. */
94 static inline unsigned int bs_generic(unsigned int v, int mode)
96 #if defined(CPU_ARM) && ARM_ARCH >= 5
97 unsigned int r = __builtin_clz(v);
98 if (mode & BS_CLZ)
100 if (mode & BS_0_0)
101 r &= 31;
102 } else {
103 r = 31 - r;
104 /* If mode is constant, this is a single conditional instruction */
105 if (mode & BS_0_0 && (signed)r < 0)
106 r += 1;
108 #else
109 const uint8_t *bs_tab;
110 unsigned int r;
111 unsigned int n = v;
112 int inc;
113 /* Set up table, increment, and initial result value based on
114 personality. */
115 if (mode & BS_CLZ)
117 bs_tab = bs_clz_tab;
118 r = 24;
119 inc = -16;
120 } else {
121 bs_tab = bs_log2_tab;
122 r = 0;
123 inc = 16;
125 if (!(mode & BS_SHORT) && n >= 0x10000) {
126 n >>= 16;
127 r += inc;
129 if (n > 0xff) {
130 n >>= 8;
131 r += inc / 2;
133 #ifdef CPU_COLDFIRE
134 /* The high 24 bits of n are guaranteed empty after the above, so a
135 superfluous ext.b instruction can be saved by loading the LUT value over
136 n with asm */
137 asm volatile (
138 "move.b (%1,%0.l),%0"
139 : "+d" (n)
140 : "a" (bs_tab)
142 #else
143 n = bs_tab[n];
144 #endif
145 r += n;
146 if (mode & BS_CLZ && mode & BS_0_0 && v == 0)
147 r = 0;
148 #endif
149 return r;
152 /* TODO figure out if we really need to care about calculating
153 av_log2(0) */
154 #define av_log2(v) bs_generic(v, BS_0_0)
156 /* Various codec helper functions */
158 int codec_init(void);
159 void codec_set_replaygain(struct mp3entry* id3);
161 #ifdef RB_PROFILE
162 void __cyg_profile_func_enter(void *this_fn, void *call_site)
163 NO_PROF_ATTR ICODE_ATTR;
164 void __cyg_profile_func_exit(void *this_fn, void *call_site)
165 NO_PROF_ATTR ICODE_ATTR;
166 #endif
168 #endif /* __CODECLIB_H__ */