Fix the wavplay icon
[Rockbox.git] / apps / codecs.h
blob5dfadcc29d44d13eb6933db9b8baec9949b9677d
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 "font.h"
40 #include "system.h"
41 #include "id3.h"
42 #include "mpeg.h"
43 #include "audio.h"
44 #include "mp3_playback.h"
45 #ifdef RB_PROFILE
46 #include "profile.h"
47 #endif
48 #if (CONFIG_CODEC == SWCODEC)
49 #if !defined(SIMULATOR) && defined(HAVE_RECORDING)
50 #include "pcm_record.h"
51 #endif
52 #include "dsp.h"
53 #include "playback.h"
54 #endif
55 #include "settings.h"
56 #include "thread.h"
57 #include "playlist.h"
58 #include "sound.h"
60 #ifdef CODEC
62 #if defined(DEBUG) || defined(SIMULATOR)
63 #undef DEBUGF
64 #define DEBUGF ci->debugf
65 #undef LDEBUGF
66 #define LDEBUGF ci->debugf
67 #else
68 #define DEBUGF(...)
69 #define LDEBUGF(...)
70 #endif
72 #ifdef ROCKBOX_HAS_LOGF
73 #undef LOGF
74 #define LOGF ci->logf
75 #else
76 #define LOGF(...)
77 #endif
79 #endif
81 #ifdef SIMULATOR
82 #define PREFIX(_x_) sim_ ## _x_
83 #else
84 #define PREFIX(_x_) _x_
85 #endif
87 /* magic for normal codecs */
88 #define CODEC_MAGIC 0x52434F44 /* RCOD */
89 /* magic for encoder codecs */
90 #define CODEC_ENC_MAGIC 0x52454E43 /* RENC */
92 /* increase this every time the api struct changes */
93 #define CODEC_API_VERSION 17
95 /* update this to latest version if a change to the api struct breaks
96 backwards compatibility (and please take the opportunity to sort in any
97 new function which are "waiting" at the end of the function table) */
98 #define CODEC_MIN_API_VERSION 17
100 /* codec return codes */
101 enum codec_status {
102 CODEC_OK = 0,
103 CODEC_USB_CONNECTED,
104 CODEC_ERROR = -1,
107 /* NOTE: To support backwards compatibility, only add new functions at
108 the end of the structure. Every time you add a new function,
109 remember to increase CODEC_API_VERSION. If you make changes to the
110 existing APIs then also update CODEC_MIN_API_VERSION to current
111 version
113 struct codec_api {
115 off_t filesize; /* Total file length */
116 off_t curpos; /* Current buffer position */
118 /* For gapless mp3 */
119 struct mp3entry *id3; /* TAG metadata pointer */
120 bool *taginfo_ready; /* Is metadata read */
122 /* Codec should periodically check if stop_codec is set to true.
123 In case it is, codec must return immediately */
124 bool stop_codec;
125 /* Codec should periodically check if new_track is non zero.
126 When it is, the codec should request a new track. */
127 int new_track;
128 /* If seek_time != 0, codec should seek to that song position (in ms)
129 if codec supports seeking. */
130 long seek_time;
132 /* Returns buffer to malloc array. Only codeclib should need this. */
133 void* (*get_codec_memory)(size_t *size);
134 /* Insert PCM data into audio buffer for playback. Playback will start
135 automatically. */
136 bool (*pcmbuf_insert)(const void *ch1, const void *ch2, int count);
137 /* Set song position in WPS (value in ms). */
138 void (*set_elapsed)(unsigned int value);
140 /* Read next <size> amount bytes from file buffer to <ptr>.
141 Will return number of bytes read or 0 if end of file. */
142 size_t (*read_filebuf)(void *ptr, size_t size);
143 /* Request pointer to file buffer which can be used to read
144 <realsize> amount of data. <reqsize> tells the buffer system
145 how much data it should try to allocate. If <realsize> is 0,
146 end of file is reached. */
147 void* (*request_buffer)(size_t *realsize, size_t reqsize);
148 /* Advance file buffer position by <amount> amount of bytes. */
149 void (*advance_buffer)(size_t amount);
150 /* Advance file buffer to a pointer location inside file buffer. */
151 void (*advance_buffer_loc)(void *ptr);
152 /* Seek file buffer to position <newpos> beginning of file. */
153 bool (*seek_buffer)(size_t newpos);
154 /* Codec should call this function when it has done the seeking. */
155 void (*seek_complete)(void);
156 /* Calculate mp3 seek position from given time data in ms. */
157 off_t (*mp3_get_filepos)(int newtime);
158 /* Request file change from file buffer. Returns true is next
159 track is available and changed. If return value is false,
160 codec should exit immediately with PLUGIN_OK status. */
161 bool (*request_next_track)(void);
162 /* Free the buffer area of the current codec after its loaded */
163 void (*discard_codec)(void);
165 void (*set_offset)(size_t value);
166 /* Configure different codec buffer parameters. */
167 void (*configure)(int setting, intptr_t value);
169 void (*splash)(int ticks, const char *fmt, ...) ATTRIBUTE_PRINTF(2, 3);
171 /* file */
172 int (*PREFIX(open))(const char* pathname, int flags);
173 int (*close)(int fd);
174 ssize_t (*read)(int fd, void* buf, size_t count);
175 off_t (*PREFIX(lseek))(int fd, off_t offset, int whence);
176 int (*PREFIX(creat))(const char *pathname);
177 ssize_t (*write)(int fd, const void* buf, size_t count);
178 int (*PREFIX(remove))(const char* pathname);
179 int (*PREFIX(rename))(const char* path, const char* newname);
180 int (*PREFIX(ftruncate))(int fd, off_t length);
181 int (*PREFIX(fsync))(int fd);
183 int (*fdprintf)(int fd, const char *fmt, ...) ATTRIBUTE_PRINTF(2, 3);
184 int (*read_line)(int fd, char* buffer, int buffer_size);
185 bool (*settings_parseline)(char* line, char** name, char** value);
186 #ifndef SIMULATOR
187 void (*ata_sleep)(void);
188 #endif
190 /* dir */
191 DIR* (*PREFIX(opendir))(const char* name);
192 int (*PREFIX(closedir))(DIR* dir);
193 struct dirent* (*PREFIX(readdir))(DIR* dir);
194 int (*PREFIX(mkdir))(const char *name);
196 /* kernel/ system */
197 void (*PREFIX(sleep))(int ticks);
198 void (*yield)(void);
199 long* current_tick;
200 long (*default_event_handler)(long event);
201 long (*default_event_handler_ex)(long event, void (*callback)(void *), void *parameter);
202 struct thread_entry* (*create_thread)(void (*function)(void),
203 void* stack, int stack_size, const char *name
204 IF_PRIO(, int priority)
205 IF_COP(, unsigned int core, bool fallback));
206 void (*remove_thread)(struct thread_entry *thread);
207 void (*reset_poweroff_timer)(void);
208 #ifndef SIMULATOR
209 int (*system_memory_guard)(int newmode);
210 long *cpu_frequency;
211 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
212 #ifdef CPU_BOOST_LOGGING
213 void (*cpu_boost_)(bool on_off,char*location,int line);
214 #else
215 void (*cpu_boost)(bool on_off);
216 #endif
217 #endif
218 #endif
220 /* strings and memory */
221 int (*snprintf)(char *buf, size_t size, const char *fmt, ...)
222 ATTRIBUTE_PRINTF(3, 4);
223 char* (*strcpy)(char *dst, const char *src);
224 char* (*strncpy)(char *dst, const char *src, size_t length);
225 size_t (*strlen)(const char *str);
226 char * (*strrchr)(const char *s, int c);
227 int (*strcmp)(const char *, const char *);
228 int (*strcasecmp)(const char *, const char *);
229 int (*strncasecmp)(const char *s1, const char *s2, size_t n);
230 void* (*memset)(void *dst, int c, size_t length);
231 void* (*memcpy)(void *out, const void *in, size_t n);
232 void* (*memmove)(void *out, const void *in, size_t n);
233 const char *_ctype_;
234 int (*atoi)(const char *str);
235 char *(*strchr)(const char *s, int c);
236 char *(*strcat)(char *s1, const char *s2);
237 int (*memcmp)(const void *s1, const void *s2, size_t n);
238 char *(*strcasestr) (const char* phaystack, const char* pneedle);
239 void *(*memchr)(const void *s1, int c, size_t n);
241 /* sound */
242 void (*sound_set)(int setting, int value);
243 #ifndef SIMULATOR
244 void (*mp3_play_data)(const unsigned char* start,
245 int size, void (*get_more)(unsigned char** start, int* size));
246 void (*mp3_play_pause)(bool play);
247 void (*mp3_play_stop)(void);
248 bool (*mp3_is_playing)(void);
249 #endif /* !SIMULATOR */
251 /* playback control */
252 void (*PREFIX(audio_play))(long offset);
253 void (*audio_stop)(void);
254 void (*audio_pause)(void);
255 void (*audio_resume)(void);
256 void (*audio_next)(void);
257 void (*audio_prev)(void);
258 void (*audio_ff_rewind)(long newtime);
259 struct mp3entry* (*audio_next_track)(void);
260 int (*playlist_amount)(void);
261 int (*audio_status)(void);
262 bool (*audio_has_changed_track)(void);
263 struct mp3entry* (*audio_current_track)(void);
264 void (*audio_flush_and_reload_tracks)(void);
265 int (*audio_get_file_pos)(void);
267 /* misc */
268 void (*srand)(unsigned int seed);
269 int (*rand)(void);
270 void (*qsort)(void *base, size_t nmemb, size_t size,
271 int(*compar)(const void *, const void *));
272 int (*kbd_input)(char* buffer, int buflen);
273 struct tm* (*get_time)(void);
274 int (*set_time)(const struct tm *tm);
275 void* (*plugin_get_audio_buffer)(int* buffer_size);
276 int (*round_value_to_list32)(unsigned long value,
277 const unsigned long list[],
278 int count,
279 bool signd);
281 #if defined(DEBUG) || defined(SIMULATOR)
282 void (*debugf)(const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2);
283 #endif
284 #ifdef ROCKBOX_HAS_LOGF
285 void (*logf)(const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2);
286 #endif
287 struct user_settings* global_settings;
288 bool (*mp3info)(struct mp3entry *entry, const char *filename, bool v1first);
289 int (*count_mp3_frames)(int fd, int startpos, int filesize,
290 void (*progressfunc)(int));
291 int (*create_xing_header)(int fd, long startpos, long filesize,
292 unsigned char *buf, unsigned long num_frames,
293 unsigned long rec_time, unsigned long header_template,
294 void (*progressfunc)(int), bool generate_toc);
295 unsigned long (*find_next_frame)(int fd, long *offset,
296 long max_offset, unsigned long last_header);
297 int (*battery_level)(void);
298 bool (*battery_level_safe)(void);
300 #ifdef RB_PROFILE
301 void (*profile_thread)(void);
302 void (*profstop)(void);
303 void (*profile_func_enter)(void *this_fn, void *call_site);
304 void (*profile_func_exit)(void *this_fn, void *call_site);
305 #endif
307 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
308 volatile bool stop_encoder;
309 volatile int enc_codec_loaded; /* <0=error, 0=pending, >0=ok */
310 void (*enc_get_inputs)(struct enc_inputs *inputs);
311 void (*enc_set_parameters)(struct enc_parameters *params);
312 struct enc_chunk_hdr * (*enc_get_chunk)(void);
313 void (*enc_finish_chunk)(void);
314 int (*enc_pcm_buf_near_empty)(void);
315 unsigned char * (*enc_get_pcm_data)(size_t size);
316 size_t (*enc_unget_pcm_data)(size_t size);
317 #endif
319 /* new stuff at the end, sort into place next time
320 the API gets incompatible */
324 /* codec header */
325 struct codec_header {
326 unsigned long magic; /* RCOD or RENC */
327 unsigned short target_id;
328 unsigned short api_version;
329 unsigned char *load_addr;
330 unsigned char *end_addr;
331 enum codec_status(*entry_point)(struct codec_api*);
334 #ifdef CODEC
335 #ifndef SIMULATOR
336 /* plugin_* is correct, codecs use the plugin linker script */
337 extern unsigned char plugin_start_addr[];
338 extern unsigned char plugin_end_addr[];
339 /* decoders */
340 #define CODEC_HEADER \
341 const struct codec_header __header \
342 __attribute__ ((section (".header")))= { \
343 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
344 plugin_start_addr, plugin_end_addr, codec_start };
345 /* encoders */
346 #define CODEC_ENC_HEADER \
347 const struct codec_header __header \
348 __attribute__ ((section (".header")))= { \
349 CODEC_ENC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
350 plugin_start_addr, plugin_end_addr, codec_start };
352 #else /* def SIMULATOR */
353 /* decoders */
354 #define CODEC_HEADER \
355 const struct codec_header __header = { \
356 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
357 NULL, NULL, codec_start };
358 /* encoders */
359 #define CODEC_ENC_HEADER \
360 const struct codec_header __header = { \
361 CODEC_ENC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
362 NULL, NULL, codec_start };
363 #endif /* SIMULATOR */
364 #endif /* CODEC */
366 /* create full codec path from root filenames in audio_formats[]
367 assumes buffer size is MAX_PATH */
368 void codec_get_full_path(char *path, const char *codec_root_fn);
370 /* defined by the codec loader (codec.c) */
371 int codec_load_ram(char* codecptr, int size, void* ptr2, int bufwrap,
372 struct codec_api *api);
373 int codec_load_file(const char* codec, struct codec_api *api);
375 /* defined by the codec */
376 enum codec_status codec_start(struct codec_api* rockbox);
378 #endif