Remove .a files before running ar, to avoid problems with renamed files remaining...
[kugel-rb.git] / apps / codecs.h
blob4194524e6da1c16b6ad4f8185c0d7c5ba14a528a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
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 ****************************************************************************/
21 #ifndef _CODECS_H_
22 #define _CODECS_H_
24 /* instruct simulator code to not redefine any symbols when compiling codecs.
25 (the CODEC macro is defined in apps/codecs/Makefile) */
26 #ifdef CODEC
27 #define NO_REDEFINES_PLEASE
28 #endif
30 #ifndef MEM
31 #define MEM 2
32 #endif
34 #include <stdbool.h>
35 #include <stdlib.h>
36 #include "config.h"
37 #include "kernel.h"
38 #include "system.h"
39 #include "metadata.h"
40 #include "audio.h"
41 #ifdef RB_PROFILE
42 #include "profile.h"
43 #include "thread.h"
44 #endif
45 #if (CONFIG_CODEC == SWCODEC)
46 #if !defined(SIMULATOR) && defined(HAVE_RECORDING)
47 #include "pcm_record.h"
48 #endif
49 #include "dsp.h"
50 #endif
51 #include "settings.h"
53 #ifdef CODEC
54 #if defined(DEBUG) || defined(SIMULATOR)
55 #undef DEBUGF
56 #define DEBUGF ci->debugf
57 #undef LDEBUGF
58 #define LDEBUGF ci->debugf
59 #else
60 #define DEBUGF(...)
61 #define LDEBUGF(...)
62 #endif
64 #ifdef ROCKBOX_HAS_LOGF
65 #undef LOGF
66 #define LOGF ci->logf
67 #else
68 #define LOGF(...)
69 #endif
71 #endif
73 #ifdef SIMULATOR
74 #define PREFIX(_x_) sim_ ## _x_
75 #else
76 #define PREFIX(_x_) _x_
77 #endif
79 /* magic for normal codecs */
80 #define CODEC_MAGIC 0x52434F44 /* RCOD */
81 /* magic for encoder codecs */
82 #define CODEC_ENC_MAGIC 0x52454E43 /* RENC */
84 /* increase this every time the api struct changes */
85 #define CODEC_API_VERSION 27
87 /* update this to latest version if a change to the api struct breaks
88 backwards compatibility (and please take the opportunity to sort in any
89 new function which are "waiting" at the end of the function table) */
90 #define CODEC_MIN_API_VERSION 27
92 /* codec return codes */
93 enum codec_status {
94 CODEC_OK = 0,
95 CODEC_USB_CONNECTED,
96 CODEC_ERROR = -1,
99 /* NOTE: To support backwards compatibility, only add new functions at
100 the end of the structure. Every time you add a new function,
101 remember to increase CODEC_API_VERSION. If you make changes to the
102 existing APIs then also update CODEC_MIN_API_VERSION to current
103 version
105 struct codec_api {
107 off_t filesize; /* Total file length */
108 off_t curpos; /* Current buffer position */
110 /* For gapless mp3 */
111 struct mp3entry *id3; /* TAG metadata pointer */
112 bool *taginfo_ready; /* Is metadata read */
114 /* Codec should periodically check if stop_codec is set to true.
115 In case it is, codec must return immediately */
116 bool stop_codec;
117 /* Codec should periodically check if new_track is non zero.
118 When it is, the codec should request a new track. */
119 int new_track;
120 /* If seek_time != 0, codec should seek to that song position (in ms)
121 if codec supports seeking. */
122 long seek_time;
124 /* The dsp instance to be used for audio output */
125 struct dsp_config *dsp;
127 /* Returns buffer to malloc array. Only codeclib should need this. */
128 void* (*codec_get_buffer)(size_t *size);
129 /* Insert PCM data into audio buffer for playback. Playback will start
130 automatically. */
131 bool (*pcmbuf_insert)(const void *ch1, const void *ch2, int count);
132 /* Set song position in WPS (value in ms). */
133 void (*set_elapsed)(unsigned int value);
135 /* Read next <size> amount bytes from file buffer to <ptr>.
136 Will return number of bytes read or 0 if end of file. */
137 size_t (*read_filebuf)(void *ptr, size_t size);
138 /* Request pointer to file buffer which can be used to read
139 <realsize> amount of data. <reqsize> tells the buffer system
140 how much data it should try to allocate. If <realsize> is 0,
141 end of file is reached. */
142 void* (*request_buffer)(size_t *realsize, size_t reqsize);
143 /* Advance file buffer position by <amount> amount of bytes. */
144 void (*advance_buffer)(size_t amount);
145 /* Advance file buffer to a pointer location inside file buffer. */
146 void (*advance_buffer_loc)(void *ptr);
147 /* Seek file buffer to position <newpos> beginning of file. */
148 bool (*seek_buffer)(size_t newpos);
149 /* Codec should call this function when it has done the seeking. */
150 void (*seek_complete)(void);
151 /* Request file change from file buffer. Returns true is next
152 track is available and changed. If return value is false,
153 codec should exit immediately with PLUGIN_OK status. */
154 bool (*request_next_track)(void);
155 /* Free the buffer area of the current codec after its loaded */
156 void (*discard_codec)(void);
158 void (*set_offset)(size_t value);
159 /* Configure different codec buffer parameters. */
160 void (*configure)(int setting, intptr_t value);
162 /* kernel/ system */
163 void (*PREFIX(sleep))(int ticks);
164 void (*yield)(void);
166 #if NUM_CORES > 1
167 struct thread_entry *
168 (*create_thread)(void (*function)(void), void* stack,
169 size_t stack_size, unsigned flags, const char *name
170 IF_PRIO(, int priority)
171 IF_COP(, unsigned int core));
173 void (*thread_thaw)(struct thread_entry *thread);
174 void (*thread_wait)(struct thread_entry *thread);
175 void (*semaphore_init)(struct semaphore *s, int max, int start);
176 void (*semaphore_wait)(struct semaphore *s);
177 void (*semaphore_release)(struct semaphore *s);
178 #endif /* NUM_CORES */
180 #ifdef CACHE_FUNCTIONS_AS_CALL
181 void (*flush_icache)(void);
182 void (*invalidate_icache)(void);
183 #endif
185 /* strings and memory */
186 char* (*strcpy)(char *dst, const char *src);
187 char* (*strncpy)(char *dst, const char *src, size_t length);
188 size_t (*strlen)(const char *str);
189 int (*strcmp)(const char *, const char *);
190 char *(*strcat)(char *s1, const char *s2);
191 void* (*memset)(void *dst, int c, size_t length);
192 void* (*memcpy)(void *out, const void *in, size_t n);
193 void* (*memmove)(void *out, const void *in, size_t n);
194 int (*memcmp)(const void *s1, const void *s2, size_t n);
195 void *(*memchr)(const void *s1, int c, size_t n);
196 char *(*strcasestr) (const char* phaystack, const char* pneedle);
198 #if defined(DEBUG) || defined(SIMULATOR)
199 void (*debugf)(const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2);
200 #endif
201 #ifdef ROCKBOX_HAS_LOGF
202 void (*logf)(const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2);
203 #endif
205 /* Tremor requires qsort */
206 void (*qsort)(void *base, size_t nmemb, size_t size,
207 int(*compar)(const void *, const void *));
209 /* The ADX codec accesses global_settings to test for REPEAT_ONE mode */
210 struct user_settings* global_settings;
212 #ifdef RB_PROFILE
213 void (*profile_thread)(void);
214 void (*profstop)(void);
215 void (*profile_func_enter)(void *this_fn, void *call_site);
216 void (*profile_func_exit)(void *this_fn, void *call_site);
217 #endif
219 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
220 volatile bool stop_encoder;
221 volatile int enc_codec_loaded; /* <0=error, 0=pending, >0=ok */
222 void (*enc_get_inputs)(struct enc_inputs *inputs);
223 void (*enc_set_parameters)(struct enc_parameters *params);
224 struct enc_chunk_hdr * (*enc_get_chunk)(void);
225 void (*enc_finish_chunk)(void);
226 unsigned char * (*enc_get_pcm_data)(size_t size);
227 size_t (*enc_unget_pcm_data)(size_t size);
229 /* file */
230 int (*PREFIX(open))(const char* pathname, int flags);
231 int (*close)(int fd);
232 ssize_t (*read)(int fd, void* buf, size_t count);
233 off_t (*PREFIX(lseek))(int fd, off_t offset, int whence);
234 ssize_t (*write)(int fd, const void* buf, size_t count);
235 int (*round_value_to_list32)(unsigned long value,
236 const unsigned long list[],
237 int count,
238 bool signd);
239 #endif
241 /* new stuff at the end, sort into place next time
242 the API gets incompatible */
246 /* codec header */
247 struct codec_header {
248 unsigned long magic; /* RCOD or RENC */
249 unsigned short target_id;
250 unsigned short api_version;
251 unsigned char *load_addr;
252 unsigned char *end_addr;
253 enum codec_status(*entry_point)(struct codec_api*);
256 extern unsigned char codecbuf[];
257 extern size_t codec_size;
259 #ifdef CODEC
260 #ifndef SIMULATOR
261 /* plugin_* is correct, codecs use the plugin linker script */
262 extern unsigned char plugin_start_addr[];
263 extern unsigned char plugin_end_addr[];
264 /* decoders */
265 #define CODEC_HEADER \
266 const struct codec_header __header \
267 __attribute__ ((section (".header")))= { \
268 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
269 plugin_start_addr, plugin_end_addr, codec_start };
270 /* encoders */
271 #define CODEC_ENC_HEADER \
272 const struct codec_header __header \
273 __attribute__ ((section (".header")))= { \
274 CODEC_ENC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
275 plugin_start_addr, plugin_end_addr, codec_start };
277 #else /* def SIMULATOR */
278 /* decoders */
279 #define CODEC_HEADER \
280 const struct codec_header __header \
281 __attribute__((visibility("default"))) = { \
282 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
283 NULL, NULL, codec_start };
284 /* encoders */
285 #define CODEC_ENC_HEADER \
286 const struct codec_header __header = { \
287 CODEC_ENC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
288 NULL, NULL, codec_start };
289 #endif /* SIMULATOR */
290 #endif /* CODEC */
292 /* create full codec path from root filenames in audio_formats[]
293 assumes buffer size is MAX_PATH */
294 void codec_get_full_path(char *path, const char *codec_root_fn);
296 /* defined by the codec loader (codec.c) */
297 int codec_load_buf(unsigned int hid, struct codec_api *api);
298 int codec_load_file(const char* codec, struct codec_api *api);
300 /* defined by the codec */
301 enum codec_status codec_start(struct codec_api* rockbox);
302 enum codec_status codec_main(void);
304 #ifndef CACHE_FUNCTION_WRAPPERS
306 #ifdef CACHE_FUNCTIONS_AS_CALL
307 #define CACHE_FUNCTION_WRAPPERS(api) \
308 void flush_icache(void) \
310 (api)->flush_icache(); \
312 void invalidate_icache(void) \
314 (api)->invalidate_icache(); \
316 #else
317 #define CACHE_FUNCTION_WRAPPERS(api)
318 #endif /* CACHE_FUNCTIONS_AS_CALL */
320 #endif /* CACHE_FUNCTION_WRAPPERS */
322 #endif