Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 Dave Chapman |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 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. |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 16 | * |
| 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énius | b9d7f98 | 2009-12-05 16:47:43 +0000 | [diff] [blame] | 22 | #ifndef __CODECLIB_H__ |
| 23 | #define __CODECLIB_H__ |
| 24 | |
Thomas Martitz | 50a6ca3 | 2010-05-06 21:04:40 +0000 | [diff] [blame] | 25 | #include <inttypes.h> |
| 26 | #include <string.h> |
Daniel Stenberg | 1dd672f | 2005-06-22 19:41:30 +0000 | [diff] [blame] | 27 | #include "config.h" |
| 28 | #include "codecs.h" |
Dave Hooper | 42774d3 | 2010-02-17 00:49:53 +0000 | [diff] [blame] | 29 | #include "mdct.h" |
| 30 | #include "fft.h" |
Daniel Stenberg | 1dd672f | 2005-06-22 19:41:30 +0000 | [diff] [blame] | 31 | |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 32 | extern struct codec_api *ci; |
Björn Stenberg | c6b3d38 | 2008-11-20 11:27:31 +0000 | [diff] [blame] | 33 | extern size_t mem_ptr; |
| 34 | extern size_t bufsize; |
Nils Wallménius | 24f551b | 2008-11-06 18:49:11 +0000 | [diff] [blame] | 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 */ |
Thom Johansen | c91e0bb | 2005-10-13 11:32:52 +0000 | [diff] [blame] | 38 | |
| 39 | /* Standard library functions that are used by the codecs follow here */ |
| 40 | |
Björn Stenberg | c6b3d38 | 2008-11-20 11:27:31 +0000 | [diff] [blame] | 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) |
Thomas Martitz | 50a6ca3 | 2010-05-06 21:04:40 +0000 | [diff] [blame] | 48 | #undef alloca |
Björn Stenberg | c6b3d38 | 2008-11-20 11:27:31 +0000 | [diff] [blame] | 49 | #define alloca(x) __builtin_alloca(x) |
| 50 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 51 | void* codec_malloc(size_t size); |
| 52 | void* codec_calloc(size_t nmemb, size_t size); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 53 | void* codec_realloc(void* ptr, size_t size); |
| 54 | void codec_free(void* ptr); |
Magnus Holmgren | f5ec0fa | 2005-09-18 12:44:27 +0000 | [diff] [blame] | 55 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 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); |
Dave Chapman | c3f901b | 2005-10-31 20:41:54 +0000 | [diff] [blame] | 59 | void *memmove(void *s1, const void *s2, size_t n); |
Thom Johansen | c91e0bb | 2005-10-13 11:32:52 +0000 | [diff] [blame] | 60 | |
| 61 | size_t strlen(const char *s); |
| 62 | char *strcpy(char *dest, const char *src); |
| 63 | char *strcat(char *dest, const char *src); |
Thomas Martitz | 50a6ca3 | 2010-05-06 21:04:40 +0000 | [diff] [blame] | 64 | |
| 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); |
Thom Johansen | c91e0bb | 2005-10-13 11:32:52 +0000 | [diff] [blame] | 69 | |
| 70 | void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); |
| 71 | |
Michael Giacomelli | 46f85c4 | 2008-09-04 18:02:10 +0000 | [diff] [blame] | 72 | /*MDCT library functions*/ |
Dave Hooper | 42774d3 | 2010-02-17 00:49:53 +0000 | [diff] [blame] | 73 | /* -1- Tremor mdct */ |
Dave Hooper | 0b5338a | 2010-02-21 21:17:53 +0000 | [diff] [blame] | 74 | extern void mdct_backward(int n, int32_t *in, int32_t *out); |
Dave Hooper | 42774d3 | 2010-02-17 00:49:53 +0000 | [diff] [blame] | 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); |
Michael Giacomelli | 46f85c4 | 2008-09-04 18:02:10 +0000 | [diff] [blame] | 80 | |
Andrew Mahone | 85aad9b | 2009-12-09 02:24:45 +0000 | [diff] [blame] | 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 |
| 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. */ |
| 94 | static 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 Buschmann | 5cebdcd | 2010-01-03 11:12:31 +0000 | [diff] [blame] | 104 | /* If mode is constant, this is a single conditional instruction */ |
Andrew Mahone | 85aad9b | 2009-12-09 02:24:45 +0000 | [diff] [blame] | 105 | 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énius | b9d7f98 | 2009-12-05 16:47:43 +0000 | [diff] [blame] | 152 | /* TODO figure out if we really need to care about calculating |
| 153 | av_log2(0) */ |
Andrew Mahone | 85aad9b | 2009-12-09 02:24:45 +0000 | [diff] [blame] | 154 | #define av_log2(v) bs_generic(v, BS_0_0) |
Nils Wallménius | b9d7f98 | 2009-12-05 16:47:43 +0000 | [diff] [blame] | 155 | |
Thom Johansen | c91e0bb | 2005-10-13 11:32:52 +0000 | [diff] [blame] | 156 | /* Various codec helper functions */ |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 157 | |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 158 | int codec_init(void); |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame^] | 159 | void codec_set_replaygain(const struct mp3entry *id3); |
Thom Johansen | c91e0bb | 2005-10-13 11:32:52 +0000 | [diff] [blame] | 160 | |
Brandon Low | 05dccc3 | 2006-01-18 20:54:13 +0000 | [diff] [blame] | 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 |
Nils Wallménius | b9d7f98 | 2009-12-05 16:47:43 +0000 | [diff] [blame] | 167 | |
| 168 | #endif /* __CODECLIB_H__ */ |