Patch #3060 from Andrew Scott - iPod mini button driver
[Rockbox.git] / apps / codecs.h
blobcf0324b49650abb6c3ce09a86634e9818d84461c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #ifndef _CODECS_H_
20 #define _CODECS_H_
22 /* instruct simulator code to not redefine any symbols when compiling codecs.
23 (the CODEC macro is defined in apps/codecs/Makefile) */
24 #ifdef CODEC
25 #define NO_REDEFINES_PLEASE
26 #endif
28 #ifndef MEM
29 #define MEM 2
30 #endif
32 #include <stdbool.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/types.h>
36 #include "config.h"
37 #include "dir.h"
38 #include "kernel.h"
39 #include "button.h"
40 #include "font.h"
41 #include "system.h"
42 #include "id3.h"
43 #include "mpeg.h"
44 #include "audio.h"
45 #include "mp3_playback.h"
46 #ifdef RB_PROFILE
47 #include "profile.h"
48 #endif
49 #if (CONFIG_CODEC == SWCODEC)
50 #include "dsp.h"
51 #include "playback.h"
52 #endif
53 #include "settings.h"
54 #include "thread.h"
55 #include "playlist.h"
56 #include "sound.h"
58 #ifdef CODEC
60 #if defined(DEBUG) || defined(SIMULATOR)
61 #undef DEBUGF
62 #define DEBUGF rb->debugf
63 #undef LDEBUGF
64 #define LDEBUGF rb->debugf
65 #else
66 #define DEBUGF(...)
67 #define LDEBUGF(...)
68 #endif
70 #ifdef ROCKBOX_HAS_LOGF
71 #undef LOGF
72 #define LOGF rb->logf
73 #else
74 #define LOGF(...)
75 #endif
77 #endif
79 #ifdef SIMULATOR
80 #define PREFIX(_x_) sim_ ## _x_
81 #else
82 #define PREFIX(_x_) _x_
83 #endif
85 #define CODEC_MAGIC 0x52434F44 /* RCOD */
87 /* increase this every time the api struct changes */
88 #define CODEC_API_VERSION 4
90 /* update this to latest version if a change to the api struct breaks
91 backwards compatibility (and please take the opportunity to sort in any
92 new function which are "waiting" at the end of the function table) */
93 #define CODEC_MIN_API_VERSION 3
95 /* codec return codes */
96 enum codec_status {
97 CODEC_OK = 0,
98 CODEC_USB_CONNECTED,
99 CODEC_ERROR = -1,
102 /* NOTE: To support backwards compatibility, only add new functions at
103 the end of the structure. Every time you add a new function,
104 remember to increase CODEC_API_VERSION. If you make changes to the
105 existing APIs then also update CODEC_MIN_API_VERSION to current
106 version
108 struct codec_api {
110 off_t filesize; /* Total file length */
111 off_t curpos; /* Current buffer position */
113 /* For gapless mp3 */
114 struct mp3entry *id3; /* TAG metadata pointer */
115 bool *taginfo_ready; /* Is metadata read */
117 /* Codec should periodically check if stop_codec is set to true.
118 In case it's, codec must return with PLUGIN_OK status immediately. */
119 bool stop_codec;
120 /* Codec should periodically check if reload_codec is set to true.
121 In case it's, codec should reload itself without exiting. */
122 bool reload_codec;
123 /* If seek_time != 0, codec should seek to that song position (in ms)
124 if codec supports seeking. */
125 int seek_time;
127 /* Returns buffer to malloc array. Only codeclib should need this. */
128 void* (*get_codec_memory)(long *size);
129 /* Insert PCM data into audio buffer for playback. Playback will start
130 automatically. */
131 bool (*pcmbuf_insert)(const char *data, size_t length);
132 bool (*pcmbuf_insert_split)(const void *ch1, const void *ch2, size_t length);
133 /* Set song position in WPS (value in ms). */
134 void (*set_elapsed)(unsigned int value);
136 /* Read next <size> amount bytes from file buffer to <ptr>.
137 Will return number of bytes read or 0 if end of file. */
138 long (*read_filebuf)(void *ptr, long size);
139 /* Request pointer to file buffer which can be used to read
140 <realsize> amount of data. <reqsize> tells the buffer system
141 how much data it should try to allocate. If <realsize> is 0,
142 end of file is reached. */
143 void* (*request_buffer)(long *realsize, long reqsize);
144 /* Advance file buffer position by <amount> amount of bytes. */
145 void (*advance_buffer)(long amount);
146 /* Advance file buffer to a pointer location inside file buffer. */
147 void (*advance_buffer_loc)(void *ptr);
148 /* Seek file buffer to position <newpos> beginning of file. */
149 bool (*seek_buffer)(off_t newpos);
150 /* Codec should call this function when it has done the seeking. */
151 void (*seek_complete)(void);
152 /* Calculate mp3 seek position from given time data in ms. */
153 off_t (*mp3_get_filepos)(int newtime);
154 /* Request file change from file buffer. Returns true is next
155 track is available and changed. If return value is false,
156 codec should exit immediately with PLUGIN_OK status. */
157 bool (*request_next_track)(void);
159 void (*set_offset)(unsigned int value);
160 /* Configure different codec buffer parameters. */
161 void (*configure)(int setting, void *value);
163 void (*splash)(int ticks, bool center, const unsigned char *fmt, ...);
165 /* file */
166 int (*PREFIX(open))(const char* pathname, int flags);
167 int (*close)(int fd);
168 ssize_t (*read)(int fd, void* buf, size_t count);
169 off_t (*PREFIX(lseek))(int fd, off_t offset, int whence);
170 int (*PREFIX(creat))(const char *pathname, mode_t mode);
171 ssize_t (*write)(int fd, const void* buf, size_t count);
172 int (*PREFIX(remove))(const char* pathname);
173 int (*PREFIX(rename))(const char* path, const char* newname);
174 int (*PREFIX(ftruncate))(int fd, off_t length);
176 int (*fdprintf)(int fd, const char *fmt, ...);
177 int (*read_line)(int fd, char* buffer, int buffer_size);
178 bool (*settings_parseline)(char* line, char** name, char** value);
179 #ifndef SIMULATOR
180 void (*ata_sleep)(void);
181 #endif
183 /* dir */
184 DIR* (*PREFIX(opendir))(const char* name);
185 int (*PREFIX(closedir))(DIR* dir);
186 struct dirent* (*PREFIX(readdir))(DIR* dir);
187 int (*PREFIX(mkdir))(const char *name, int mode);
189 /* kernel/ system */
190 void (*PREFIX(sleep))(int ticks);
191 void (*yield)(void);
192 long* current_tick;
193 long (*default_event_handler)(long event);
194 long (*default_event_handler_ex)(long event, void (*callback)(void *), void *parameter);
195 int (*create_thread)(void (*function)(void), void* stack, int stack_size, const char *name);
196 void (*remove_thread)(int threadnum);
197 void (*reset_poweroff_timer)(void);
198 #ifndef SIMULATOR
199 int (*system_memory_guard)(int newmode);
200 long *cpu_frequency;
201 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
202 void (*cpu_boost)(bool on_off);
203 #endif
204 #endif
206 /* strings and memory */
207 int (*snprintf)(char *buf, size_t size, const char *fmt, ...);
208 char* (*strcpy)(char *dst, const char *src);
209 char* (*strncpy)(char *dst, const char *src, size_t length);
210 size_t (*strlen)(const char *str);
211 char * (*strrchr)(const char *s, int c);
212 int (*strcmp)(const char *, const char *);
213 int (*strcasecmp)(const char *, const char *);
214 int (*strncasecmp)(const char *s1, const char *s2, size_t n);
215 void* (*memset)(void *dst, int c, size_t length);
216 void* (*memcpy)(void *out, const void *in, size_t n);
217 const char *_ctype_;
218 int (*atoi)(const char *str);
219 char *(*strchr)(const char *s, int c);
220 char *(*strcat)(char *s1, const char *s2);
221 int (*memcmp)(const void *s1, const void *s2, size_t n);
222 char *(*strcasestr) (const char* phaystack, const char* pneedle);
223 void *(*memchr)(const void *s1, int c, size_t n);
225 /* sound */
226 void (*sound_set)(int setting, int value);
227 #ifndef SIMULATOR
228 void (*mp3_play_data)(const unsigned char* start, int size, void (*get_more)(unsigned char** start, int* size));
229 void (*mp3_play_pause)(bool play);
230 void (*mp3_play_stop)(void);
231 bool (*mp3_is_playing)(void);
232 #endif /* !SIMULATOR */
234 /* playback control */
235 void (*PREFIX(audio_play))(int offset);
236 void (*audio_stop)(void);
237 void (*audio_pause)(void);
238 void (*audio_resume)(void);
239 void (*audio_next)(void);
240 void (*audio_prev)(void);
241 void (*audio_ff_rewind)(int newtime);
242 struct mp3entry* (*audio_next_track)(void);
243 int (*playlist_amount)(void);
244 int (*audio_status)(void);
245 bool (*audio_has_changed_track)(void);
246 struct mp3entry* (*audio_current_track)(void);
247 void (*audio_flush_and_reload_tracks)(void);
248 int (*audio_get_file_pos)(void);
250 /* tag database */
251 struct tagdb_header *tagdbheader;
252 int *tagdb_fd;
253 int *tagdb_initialized;
254 int (*tagdb_init) (void);
256 /* misc */
257 void (*srand)(unsigned int seed);
258 int (*rand)(void);
259 void (*qsort)(void *base, size_t nmemb, size_t size,
260 int(*compar)(const void *, const void *));
261 int (*kbd_input)(char* buffer, int buflen);
262 struct tm* (*get_time)(void);
263 int (*set_time)(const struct tm *tm);
264 void* (*plugin_get_audio_buffer)(int* buffer_size);
266 #if defined(DEBUG) || defined(SIMULATOR)
267 void (*debugf)(const char *fmt, ...);
268 #endif
269 #ifdef ROCKBOX_HAS_LOGF
270 void (*logf)(const char *fmt, ...);
271 #endif
272 struct user_settings* global_settings;
273 bool (*mp3info)(struct mp3entry *entry, const char *filename, bool v1first);
274 int (*count_mp3_frames)(int fd, int startpos, int filesize,
275 void (*progressfunc)(int));
276 int (*create_xing_header)(int fd, long startpos, long filesize,
277 unsigned char *buf, unsigned long num_frames,
278 unsigned long rec_time, unsigned long header_template,
279 void (*progressfunc)(int), bool generate_toc);
280 unsigned long (*find_next_frame)(int fd, long *offset,
281 long max_offset, unsigned long last_header);
282 int (*battery_level)(void);
283 bool (*battery_level_safe)(void);
285 #ifdef RB_PROFILE
286 void (*profile_thread)(void);
287 void (*profstop)(void);
288 void (*profile_func_enter)(void *this_fn, void *call_site);
289 void (*profile_func_exit)(void *this_fn, void *call_site);
290 #endif
292 /* new stuff at the end, sort into place next time
293 the API gets incompatible */
295 void* (*memmove)(void *out, const void *in, size_t n);
298 /* codec header */
299 struct codec_header {
300 unsigned long magic;
301 unsigned short target_id;
302 unsigned short api_version;
303 unsigned char *load_addr;
304 unsigned char *end_addr;
305 enum codec_status(*entry_point)(struct codec_api*);
307 #ifdef CODEC
308 #ifndef SIMULATOR
309 /* plugin_* is correct, codecs use the plugin linker script */
310 extern unsigned char plugin_start_addr[];
311 extern unsigned char plugin_end_addr[];
312 #define CODEC_HEADER \
313 const struct codec_header __header \
314 __attribute__ ((section (".header")))= { \
315 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
316 plugin_start_addr, plugin_end_addr, codec_start };
317 #else /* SIMULATOR */
318 #define CODEC_HEADER \
319 const struct codec_header __header = { \
320 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
321 NULL, NULL, codec_start };
322 #endif
323 #endif
325 /* defined by the codec loader (codec.c) */
326 int codec_load_ram(char* codecptr, int size, void* ptr2, int bufwrap,
327 struct codec_api *api);
328 int codec_load_file(const char* codec, struct codec_api *api);
330 /* defined by the codec */
331 enum codec_status codec_start(struct codec_api* rockbox);
333 #endif