Introduce "power" thread for RaaA
[maemo-rb.git] / apps / codecs.h
blob028e3614ff7e75c19d495293dff749cb731e49cc
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 #include <stdbool.h>
31 #include <stdlib.h>
32 #include "strlcpy.h"
33 #include "config.h"
34 #include "system.h"
35 #include "metadata.h"
36 #include "audio.h"
37 #ifdef RB_PROFILE
38 #include "profile.h"
39 #include "thread.h"
40 #endif
41 #if (CONFIG_CODEC == SWCODEC)
42 #ifdef HAVE_RECORDING
43 #include "pcm_record.h"
44 #endif
45 #include "dsp.h"
46 #endif
47 #include "settings.h"
49 #include "gcc_extensions.h"
50 #include "load_code.h"
52 #ifdef CODEC
53 #if defined(DEBUG) || defined(SIMULATOR)
54 #undef DEBUGF
55 #define DEBUGF ci->debugf
56 #undef LDEBUGF
57 #define LDEBUGF ci->debugf
58 #else
59 #define DEBUGF(...)
60 #define LDEBUGF(...)
61 #endif
63 #ifdef ROCKBOX_HAS_LOGF
64 #undef LOGF
65 #define LOGF ci->logf
66 #else
67 #define LOGF(...)
68 #endif
70 #endif
72 /* magic for normal codecs */
73 #define CODEC_MAGIC 0x52434F44 /* RCOD */
74 /* magic for encoder codecs */
75 #define CODEC_ENC_MAGIC 0x52454E43 /* RENC */
77 /* increase this every time the api struct changes */
78 #define CODEC_API_VERSION 39
80 /* update this to latest version if a change to the api struct breaks
81 backwards compatibility (and please take the opportunity to sort in any
82 new function which are "waiting" at the end of the function table) */
83 #define CODEC_MIN_API_VERSION 39
85 /* codec return codes */
86 enum codec_status {
87 CODEC_OK = 0,
88 CODEC_ERROR = -1,
91 /* NOTE: To support backwards compatibility, only add new functions at
92 the end of the structure. Every time you add a new function,
93 remember to increase CODEC_API_VERSION. If you make changes to the
94 existing APIs then also update CODEC_MIN_API_VERSION to current
95 version
97 struct codec_api {
99 off_t filesize; /* Total file length */
100 off_t curpos; /* Current buffer position */
102 /* For gapless mp3 */
103 struct mp3entry *id3; /* TAG metadata pointer */
104 bool *taginfo_ready; /* Is metadata read */
106 /* Codec should periodically check if stop_codec is set to true.
107 In case it is, codec must return immediately */
108 volatile bool stop_codec;
109 /* Codec should periodically check if new_track is non zero.
110 When it is, the codec should request a new track. */
111 volatile int new_track;
112 /* If seek_time != 0, codec should seek to that song position (in ms)
113 if codec supports seeking. */
114 volatile long seek_time;
116 /* The dsp instance to be used for audio output */
117 struct dsp_config *dsp;
119 /* Returns buffer to malloc array. Only codeclib should need this. */
120 void* (*codec_get_buffer)(size_t *size);
121 /* Insert PCM data into audio buffer for playback. Playback will start
122 automatically. */
123 void (*pcmbuf_insert)(const void *ch1, const void *ch2, int count);
124 /* Set song position in WPS (value in ms). */
125 void (*set_elapsed)(unsigned long value);
127 /* Read next <size> amount bytes from file buffer to <ptr>.
128 Will return number of bytes read or 0 if end of file. */
129 size_t (*read_filebuf)(void *ptr, size_t size);
130 /* Request pointer to file buffer which can be used to read
131 <realsize> amount of data. <reqsize> tells the buffer system
132 how much data it should try to allocate. If <realsize> is 0,
133 end of file is reached. */
134 void* (*request_buffer)(size_t *realsize, size_t reqsize);
135 /* Advance file buffer position by <amount> amount of bytes. */
136 void (*advance_buffer)(size_t amount);
137 /* Advance file buffer to a pointer location inside file buffer. */
138 void (*advance_buffer_loc)(void *ptr);
139 /* Seek file buffer to position <newpos> beginning of file. */
140 bool (*seek_buffer)(size_t newpos);
141 /* Codec should call this function when it has done the seeking. */
142 void (*seek_complete)(void);
143 /* Request file change from file buffer. Returns true is next
144 track is available and changed. If return value is false,
145 codec should exit immediately with PLUGIN_OK status. */
146 bool (*request_next_track)(void);
148 void (*set_offset)(size_t value);
149 /* Configure different codec buffer parameters. */
150 void (*configure)(int setting, intptr_t value);
152 /* kernel/ system */
153 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
154 void (*__div0)(void);
155 #endif
156 unsigned (*sleep)(unsigned ticks);
157 void (*yield)(void);
159 #if NUM_CORES > 1
160 unsigned int
161 (*create_thread)(void (*function)(void), void* stack,
162 size_t stack_size, unsigned flags, const char *name
163 IF_PRIO(, int priority)
164 IF_COP(, unsigned int core));
166 void (*thread_thaw)(unsigned int thread_id);
167 void (*thread_wait)(unsigned int thread_id);
168 void (*semaphore_init)(struct semaphore *s, int max, int start);
169 int (*semaphore_wait)(struct semaphore *s, int timeout);
170 void (*semaphore_release)(struct semaphore *s);
171 #endif /* NUM_CORES */
173 void (*cpucache_flush)(void);
174 void (*cpucache_invalidate)(void);
176 /* strings and memory */
177 char* (*strcpy)(char *dst, const char *src);
178 size_t (*strlen)(const char *str);
179 int (*strcmp)(const char *, const char *);
180 char *(*strcat)(char *s1, const char *s2);
181 void* (*memset)(void *dst, int c, size_t length);
182 void* (*memcpy)(void *out, const void *in, size_t n);
183 void* (*memmove)(void *out, const void *in, size_t n);
184 int (*memcmp)(const void *s1, const void *s2, size_t n);
185 void *(*memchr)(const void *s1, int c, size_t n);
186 char *(*strcasestr) (const char* phaystack, const char* pneedle);
188 #if defined(DEBUG) || defined(SIMULATOR)
189 void (*debugf)(const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2);
190 #endif
191 #ifdef ROCKBOX_HAS_LOGF
192 void (*logf)(const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2);
193 #endif
195 /* Tremor requires qsort */
196 void (*qsort)(void *base, size_t nmemb, size_t size,
197 int(*compar)(const void *, const void *));
199 /* The ADX codec accesses global_settings to test for REPEAT_ONE mode */
200 struct user_settings* global_settings;
202 #ifdef RB_PROFILE
203 void (*profile_thread)(void);
204 void (*profstop)(void);
205 void (*profile_func_enter)(void *this_fn, void *call_site);
206 void (*profile_func_exit)(void *this_fn, void *call_site);
207 #endif
209 #ifdef HAVE_RECORDING
210 void (*enc_get_inputs)(struct enc_inputs *inputs);
211 void (*enc_set_parameters)(struct enc_parameters *params);
212 struct enc_chunk_hdr * (*enc_get_chunk)(void);
213 void (*enc_finish_chunk)(void);
214 unsigned char * (*enc_get_pcm_data)(size_t size);
215 size_t (*enc_unget_pcm_data)(size_t size);
217 /* file */
218 int (*open)(const char* pathname, int flags, ...);
219 int (*close)(int fd);
220 ssize_t (*read)(int fd, void* buf, size_t count);
221 off_t (*lseek)(int fd, off_t offset, int whence);
222 ssize_t (*write)(int fd, const void* buf, size_t count);
223 int (*round_value_to_list32)(unsigned long value,
224 const unsigned long list[],
225 int count,
226 bool signd);
227 #endif
229 /* new stuff at the end, sort into place next time
230 the API gets incompatible */
233 /* codec header */
234 struct codec_header {
235 struct lc_header lc_hdr; /* must be first */
236 enum codec_status(*entry_point)(void);
237 struct codec_api **api;
240 extern unsigned char codecbuf[];
241 extern size_t codec_size;
243 #ifdef CODEC
244 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
245 /* plugin_* is correct, codecs use the plugin linker script */
246 extern unsigned char plugin_start_addr[];
247 extern unsigned char plugin_end_addr[];
248 /* decoders */
249 #define CODEC_HEADER \
250 const struct codec_header __header \
251 __attribute__ ((section (".header")))= { \
252 { CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
253 plugin_start_addr, plugin_end_addr }, codec_start, &ci };
254 /* encoders */
255 #define CODEC_ENC_HEADER \
256 const struct codec_header __header \
257 __attribute__ ((section (".header")))= { \
258 { CODEC_ENC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
259 plugin_start_addr, plugin_end_addr }, codec_start, &ci };
261 #else /* def SIMULATOR */
262 /* decoders */
263 #define CODEC_HEADER \
264 const struct codec_header __header \
265 __attribute__((visibility("default"))) = { \
266 { CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, NULL, NULL }, \
267 codec_start, &ci };
268 /* encoders */
269 #define CODEC_ENC_HEADER \
270 const struct codec_header __header = { \
271 { CODEC_ENC_MAGIC, TARGET_ID, CODEC_API_VERSION, NULL, NULL }, \
272 codec_start, &ci };
273 #endif /* SIMULATOR */
274 #endif /* CODEC */
276 /* create full codec path from root filenames in audio_formats[]
277 assumes buffer size is MAX_PATH */
278 void codec_get_full_path(char *path, const char *codec_root_fn);
280 /* defined by the codec loader (codec.c) */
281 void * codec_load_buf(int hid, struct codec_api *api);
282 void * codec_load_file(const char* codec, struct codec_api *api);
283 int codec_begin(void *handle);
284 void codec_close(void *handle);
286 /* defined by the codec */
287 enum codec_status codec_start(void);
288 enum codec_status codec_main(void);
290 #endif