HAVE_ADJUSTABLE_CPU_FREQ isn't defined for simulators, so we don't have to check...
[Rockbox.git] / apps / codecs.h
blob50ab4cda0e2ab97d2df0b9ea9a76cf17efa9265d
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 6
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 6
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 long seek_time;
127 /* Returns buffer to malloc array. Only codeclib should need this. */
128 void* (*get_codec_memory)(size_t *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 size_t (*read_filebuf)(void *ptr, size_t 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)(size_t *realsize, size_t reqsize);
144 /* Advance file buffer position by <amount> amount of bytes. */
145 void (*advance_buffer)(size_t 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)(size_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)(size_t 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 void* (*memmove)(void *out, const void *in, size_t n);
218 const char *_ctype_;
219 int (*atoi)(const char *str);
220 char *(*strchr)(const char *s, int c);
221 char *(*strcat)(char *s1, const char *s2);
222 int (*memcmp)(const void *s1, const void *s2, size_t n);
223 char *(*strcasestr) (const char* phaystack, const char* pneedle);
224 void *(*memchr)(const void *s1, int c, size_t n);
226 /* sound */
227 void (*sound_set)(int setting, int value);
228 #ifndef SIMULATOR
229 void (*mp3_play_data)(const unsigned char* start, int size, void (*get_more)(unsigned char** start, int* size));
230 void (*mp3_play_pause)(bool play);
231 void (*mp3_play_stop)(void);
232 bool (*mp3_is_playing)(void);
233 #endif /* !SIMULATOR */
235 /* playback control */
236 void (*PREFIX(audio_play))(long offset);
237 void (*audio_stop)(void);
238 void (*audio_pause)(void);
239 void (*audio_resume)(void);
240 void (*audio_next)(void);
241 void (*audio_prev)(void);
242 void (*audio_ff_rewind)(long newtime);
243 struct mp3entry* (*audio_next_track)(void);
244 int (*playlist_amount)(void);
245 int (*audio_status)(void);
246 bool (*audio_has_changed_track)(void);
247 struct mp3entry* (*audio_current_track)(void);
248 void (*audio_flush_and_reload_tracks)(void);
249 int (*audio_get_file_pos)(void);
251 /* misc */
252 void (*srand)(unsigned int seed);
253 int (*rand)(void);
254 void (*qsort)(void *base, size_t nmemb, size_t size,
255 int(*compar)(const void *, const void *));
256 int (*kbd_input)(char* buffer, int buflen);
257 struct tm* (*get_time)(void);
258 int (*set_time)(const struct tm *tm);
259 void* (*plugin_get_audio_buffer)(int* buffer_size);
261 #if defined(DEBUG) || defined(SIMULATOR)
262 void (*debugf)(const char *fmt, ...);
263 #endif
264 #ifdef ROCKBOX_HAS_LOGF
265 void (*logf)(const char *fmt, ...);
266 #endif
267 struct user_settings* global_settings;
268 bool (*mp3info)(struct mp3entry *entry, const char *filename, bool v1first);
269 int (*count_mp3_frames)(int fd, int startpos, int filesize,
270 void (*progressfunc)(int));
271 int (*create_xing_header)(int fd, long startpos, long filesize,
272 unsigned char *buf, unsigned long num_frames,
273 unsigned long rec_time, unsigned long header_template,
274 void (*progressfunc)(int), bool generate_toc);
275 unsigned long (*find_next_frame)(int fd, long *offset,
276 long max_offset, unsigned long last_header);
277 int (*battery_level)(void);
278 bool (*battery_level_safe)(void);
280 #ifdef RB_PROFILE
281 void (*profile_thread)(void);
282 void (*profstop)(void);
283 void (*profile_func_enter)(void *this_fn, void *call_site);
284 void (*profile_func_exit)(void *this_fn, void *call_site);
285 #endif
287 /* new stuff at the end, sort into place next time
288 the API gets incompatible */
292 /* codec header */
293 struct codec_header {
294 unsigned long magic;
295 unsigned short target_id;
296 unsigned short api_version;
297 unsigned char *load_addr;
298 unsigned char *end_addr;
299 enum codec_status(*entry_point)(struct codec_api*);
301 #ifdef CODEC
302 #ifndef SIMULATOR
303 /* plugin_* is correct, codecs use the plugin linker script */
304 extern unsigned char plugin_start_addr[];
305 extern unsigned char plugin_end_addr[];
306 #define CODEC_HEADER \
307 const struct codec_header __header \
308 __attribute__ ((section (".header")))= { \
309 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
310 plugin_start_addr, plugin_end_addr, codec_start };
311 #else /* SIMULATOR */
312 #define CODEC_HEADER \
313 const struct codec_header __header = { \
314 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
315 NULL, NULL, codec_start };
316 #endif
317 #endif
319 /* defined by the codec loader (codec.c) */
320 int codec_load_ram(char* codecptr, int size, void* ptr2, int bufwrap,
321 struct codec_api *api);
322 int codec_load_file(const char* codec, struct codec_api *api);
324 /* defined by the codec */
325 enum codec_status codec_start(struct codec_api* rockbox);
327 #endif