blob: 30091c53332608121cae2b1269f23c7d9c7fd19f [file] [log] [blame]
Linus Nielsen Feltzing1c497e62005-06-05 23:05:10 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 Dave Chapman
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Linus Nielsen Feltzing1c497e62005-06-05 23:05:10 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
Nils Wallméniusb9d7f982009-12-05 16:47:43 +000022#ifndef __CODECLIB_H__
23#define __CODECLIB_H__
24
Thomas Martitz50a6ca32010-05-06 21:04:40 +000025#include <inttypes.h>
26#include <string.h>
Daniel Stenberg1dd672f2005-06-22 19:41:30 +000027#include "config.h"
28#include "codecs.h"
Dave Hooper42774d32010-02-17 00:49:53 +000029#include "mdct.h"
30#include "fft.h"
Daniel Stenberg1dd672f2005-06-22 19:41:30 +000031
Tomasz Malesinski80da8b12006-11-26 18:31:41 +000032extern struct codec_api *ci;
Björn Stenbergc6b3d382008-11-20 11:27:31 +000033extern size_t mem_ptr;
34extern size_t bufsize;
Nils Wallménius24f551b2008-11-06 18:49:11 +000035extern unsigned char* mp3buf; /* The actual MP3 buffer from Rockbox */
36extern unsigned char* mallocbuf; /* The free space after the codec in the codec buffer */
37extern unsigned char* filebuf; /* The rest of the MP3 buffer */
Thom Johansenc91e0bb2005-10-13 11:32:52 +000038
39/* Standard library functions that are used by the codecs follow here */
40
Björn Stenbergc6b3d382008-11-20 11:27:31 +000041/* 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)
Thomas Martitz50a6ca32010-05-06 21:04:40 +000048#undef alloca
Björn Stenbergc6b3d382008-11-20 11:27:31 +000049#define alloca(x) __builtin_alloca(x)
50
Linus Nielsen Feltzing1c497e62005-06-05 23:05:10 +000051void* codec_malloc(size_t size);
52void* codec_calloc(size_t nmemb, size_t size);
Linus Nielsen Feltzing1c497e62005-06-05 23:05:10 +000053void* codec_realloc(void* ptr, size_t size);
54void codec_free(void* ptr);
Magnus Holmgrenf5ec0fa2005-09-18 12:44:27 +000055
Linus Nielsen Feltzing1c497e62005-06-05 23:05:10 +000056void *memcpy(void *dest, const void *src, size_t n);
57void *memset(void *s, int c, size_t n);
58int memcmp(const void *s1, const void *s2, size_t n);
Dave Chapmanc3f901b2005-10-31 20:41:54 +000059void *memmove(void *s1, const void *s2, size_t n);
Thom Johansenc91e0bb2005-10-13 11:32:52 +000060
61size_t strlen(const char *s);
62char *strcpy(char *dest, const char *src);
63char *strcat(char *dest, const char *src);
Thomas Martitz50a6ca32010-05-06 21:04:40 +000064
65/* on some platforms strcmp() seems to be a tricky define which
66 * breaks if we write down strcmp's prototype */
67#undef strcmp
68int strcmp(const char *s1, const char *s2);
Thom Johansenc91e0bb2005-10-13 11:32:52 +000069
70void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
71
Michael Giacomelli46f85c42008-09-04 18:02:10 +000072/*MDCT library functions*/
Dave Hooper42774d32010-02-17 00:49:53 +000073/* -1- Tremor mdct */
Dave Hooper0b5338a2010-02-21 21:17:53 +000074extern void mdct_backward(int n, int32_t *in, int32_t *out);
Dave Hooper42774d32010-02-17 00:49:53 +000075/* -2- ffmpeg fft-based mdct */
76extern void ff_imdct_half(unsigned int nbits, int32_t *output, const int32_t *input);
77extern void ff_imdct_calc(unsigned int nbits, int32_t *output, const int32_t *input);
78/*ffmpeg fft (can be used without mdct)*/
79extern void ff_fft_calc_c(int nbits, FFTComplex *z);
Michael Giacomelli46f85c42008-09-04 18:02:10 +000080
Andrew Mahone85aad9b2009-12-09 02:24:45 +000081#if !defined(CPU_ARM) || ARM_ARCH < 5
82/* From libavutil/common.h */
83extern const uint8_t bs_log2_tab[256] ICONST_ATTR;
84extern const uint8_t bs_clz_tab[256] ICONST_ATTR;
85#endif
86
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 */
91
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. */
94static inline unsigned int bs_generic(unsigned int v, int mode)
95{
96#if defined(CPU_ARM) && ARM_ARCH >= 5
97 unsigned int r = __builtin_clz(v);
98 if (mode & BS_CLZ)
99 {
100 if (mode & BS_0_0)
101 r &= 31;
102 } else {
103 r = 31 - r;
Andree Buschmann5cebdcd2010-01-03 11:12:31 +0000104 /* If mode is constant, this is a single conditional instruction */
Andrew Mahone85aad9b2009-12-09 02:24:45 +0000105 if (mode & BS_0_0 && (signed)r < 0)
106 r += 1;
107 }
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)
116 {
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;
124 }
125 if (!(mode & BS_SHORT) && n >= 0x10000) {
126 n >>= 16;
127 r += inc;
128 }
129 if (n > 0xff) {
130 n >>= 8;
131 r += inc / 2;
132 }
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)
141 );
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;
150}
151
Nils Wallméniusb9d7f982009-12-05 16:47:43 +0000152/* TODO figure out if we really need to care about calculating
153 av_log2(0) */
Andrew Mahone85aad9b2009-12-09 02:24:45 +0000154#define av_log2(v) bs_generic(v, BS_0_0)
Nils Wallméniusb9d7f982009-12-05 16:47:43 +0000155
Thom Johansenc91e0bb2005-10-13 11:32:52 +0000156/* Various codec helper functions */
Linus Nielsen Feltzing1c497e62005-06-05 23:05:10 +0000157
Tomasz Malesinski80da8b12006-11-26 18:31:41 +0000158int codec_init(void);
Michael Sevakisc537d592011-04-27 03:08:23 +0000159void codec_set_replaygain(const struct mp3entry *id3);
Thom Johansenc91e0bb2005-10-13 11:32:52 +0000160
Brandon Low05dccc32006-01-18 20:54:13 +0000161#ifdef RB_PROFILE
162void __cyg_profile_func_enter(void *this_fn, void *call_site)
163 NO_PROF_ATTR ICODE_ATTR;
164void __cyg_profile_func_exit(void *this_fn, void *call_site)
165 NO_PROF_ATTR ICODE_ATTR;
166#endif
Nils Wallméniusb9d7f982009-12-05 16:47:43 +0000167
168#endif /* __CODECLIB_H__ */