Small '#ifdef HAVE_TAGCACHE' fix
[Rockbox.git] / apps / codecs.h
blobceba2bf85204002b62ae22edaa12ecf97864d310
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 16
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 16
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, bool center, const unsigned char *fmt, ...);
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, ...);
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 char* (*strcpy)(char *dst, const char *src);
223 char* (*strncpy)(char *dst, const char *src, size_t length);
224 size_t (*strlen)(const char *str);
225 char * (*strrchr)(const char *s, int c);
226 int (*strcmp)(const char *, const char *);
227 int (*strcasecmp)(const char *, const char *);
228 int (*strncasecmp)(const char *s1, const char *s2, size_t n);
229 void* (*memset)(void *dst, int c, size_t length);
230 void* (*memcpy)(void *out, const void *in, size_t n);
231 void* (*memmove)(void *out, const void *in, size_t n);
232 const char *_ctype_;
233 int (*atoi)(const char *str);
234 char *(*strchr)(const char *s, int c);
235 char *(*strcat)(char *s1, const char *s2);
236 int (*memcmp)(const void *s1, const void *s2, size_t n);
237 char *(*strcasestr) (const char* phaystack, const char* pneedle);
238 void *(*memchr)(const void *s1, int c, size_t n);
240 /* sound */
241 void (*sound_set)(int setting, int value);
242 #ifndef SIMULATOR
243 void (*mp3_play_data)(const unsigned char* start,
244 int size, void (*get_more)(unsigned char** start, int* size));
245 void (*mp3_play_pause)(bool play);
246 void (*mp3_play_stop)(void);
247 bool (*mp3_is_playing)(void);
248 #endif /* !SIMULATOR */
250 /* playback control */
251 void (*PREFIX(audio_play))(long offset);
252 void (*audio_stop)(void);
253 void (*audio_pause)(void);
254 void (*audio_resume)(void);
255 void (*audio_next)(void);
256 void (*audio_prev)(void);
257 void (*audio_ff_rewind)(long newtime);
258 struct mp3entry* (*audio_next_track)(void);
259 int (*playlist_amount)(void);
260 int (*audio_status)(void);
261 bool (*audio_has_changed_track)(void);
262 struct mp3entry* (*audio_current_track)(void);
263 void (*audio_flush_and_reload_tracks)(void);
264 int (*audio_get_file_pos)(void);
266 /* misc */
267 void (*srand)(unsigned int seed);
268 int (*rand)(void);
269 void (*qsort)(void *base, size_t nmemb, size_t size,
270 int(*compar)(const void *, const void *));
271 int (*kbd_input)(char* buffer, int buflen);
272 struct tm* (*get_time)(void);
273 int (*set_time)(const struct tm *tm);
274 void* (*plugin_get_audio_buffer)(int* buffer_size);
275 int (*round_value_to_list32)(unsigned long value,
276 const unsigned long list[],
277 int count,
278 bool signd);
280 #if defined(DEBUG) || defined(SIMULATOR)
281 void (*debugf)(const char *fmt, ...);
282 #endif
283 #ifdef ROCKBOX_HAS_LOGF
284 void (*logf)(const char *fmt, ...);
285 #endif
286 struct user_settings* global_settings;
287 bool (*mp3info)(struct mp3entry *entry, const char *filename, bool v1first);
288 int (*count_mp3_frames)(int fd, int startpos, int filesize,
289 void (*progressfunc)(int));
290 int (*create_xing_header)(int fd, long startpos, long filesize,
291 unsigned char *buf, unsigned long num_frames,
292 unsigned long rec_time, unsigned long header_template,
293 void (*progressfunc)(int), bool generate_toc);
294 unsigned long (*find_next_frame)(int fd, long *offset,
295 long max_offset, unsigned long last_header);
296 int (*battery_level)(void);
297 bool (*battery_level_safe)(void);
299 #ifdef RB_PROFILE
300 void (*profile_thread)(void);
301 void (*profstop)(void);
302 void (*profile_func_enter)(void *this_fn, void *call_site);
303 void (*profile_func_exit)(void *this_fn, void *call_site);
304 #endif
306 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
307 volatile bool stop_encoder;
308 volatile int enc_codec_loaded; /* <0=error, 0=pending, >0=ok */
309 void (*enc_get_inputs)(struct enc_inputs *inputs);
310 void (*enc_set_parameters)(struct enc_parameters *params);
311 struct enc_chunk_hdr * (*enc_get_chunk)(void);
312 void (*enc_finish_chunk)(void);
313 int (*enc_pcm_buf_near_empty)(void);
314 unsigned char * (*enc_get_pcm_data)(size_t size);
315 size_t (*enc_unget_pcm_data)(size_t size);
316 #endif
318 /* new stuff at the end, sort into place next time
319 the API gets incompatible */
323 /* codec header */
324 struct codec_header {
325 unsigned long magic; /* RCOD or RENC */
326 unsigned short target_id;
327 unsigned short api_version;
328 unsigned char *load_addr;
329 unsigned char *end_addr;
330 enum codec_status(*entry_point)(struct codec_api*);
333 #ifdef CODEC
334 #ifndef SIMULATOR
335 /* plugin_* is correct, codecs use the plugin linker script */
336 extern unsigned char plugin_start_addr[];
337 extern unsigned char plugin_end_addr[];
338 /* decoders */
339 #define CODEC_HEADER \
340 const struct codec_header __header \
341 __attribute__ ((section (".header")))= { \
342 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
343 plugin_start_addr, plugin_end_addr, codec_start };
344 /* encoders */
345 #define CODEC_ENC_HEADER \
346 const struct codec_header __header \
347 __attribute__ ((section (".header")))= { \
348 CODEC_ENC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
349 plugin_start_addr, plugin_end_addr, codec_start };
351 #else /* def SIMULATOR */
352 /* decoders */
353 #define CODEC_HEADER \
354 const struct codec_header __header = { \
355 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
356 NULL, NULL, codec_start };
357 /* encoders */
358 #define CODEC_ENC_HEADER \
359 const struct codec_header __header = { \
360 CODEC_ENC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
361 NULL, NULL, codec_start };
362 #endif /* SIMULATOR */
363 #endif /* CODEC */
365 /* create full codec path from root filenames in audio_formats[]
366 assumes buffer size is MAX_PATH */
367 void codec_get_full_path(char *path, const char *codec_root_fn);
369 /* defined by the codec loader (codec.c) */
370 int codec_load_ram(char* codecptr, int size, void* ptr2, int bufwrap,
371 struct codec_api *api);
372 int codec_load_file(const char* codec, struct codec_api *api);
374 /* defined by the codec */
375 enum codec_status codec_start(struct codec_api* rockbox);
377 #endif