Implement rebuffering for seeking outside of buffered data boudaries
[Rockbox.git] / apps / codecs.h
bloba59a791d42acdf7057b14b03b83cea91c54a4835
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 <stdlib.h>
34 #include "config.h"
35 #include "kernel.h"
36 #include "system.h"
37 #include "id3.h"
38 #include "audio.h"
39 #ifdef RB_PROFILE
40 #include "profile.h"
41 #include "thread.h"
42 #endif
43 #if (CONFIG_CODEC == SWCODEC)
44 #if !defined(SIMULATOR) && defined(HAVE_RECORDING)
45 #include "pcm_record.h"
46 #endif
47 #include "dsp.h"
48 #endif
49 #include "settings.h"
51 #ifdef CODEC
52 #if defined(DEBUG) || defined(SIMULATOR)
53 #undef DEBUGF
54 #define DEBUGF ci->debugf
55 #undef LDEBUGF
56 #define LDEBUGF ci->debugf
57 #else
58 #define DEBUGF(...)
59 #define LDEBUGF(...)
60 #endif
62 #ifdef ROCKBOX_HAS_LOGF
63 #undef LOGF
64 #define LOGF ci->logf
65 #else
66 #define LOGF(...)
67 #endif
69 #endif
71 #ifdef SIMULATOR
72 #define PREFIX(_x_) sim_ ## _x_
73 #else
74 #define PREFIX(_x_) _x_
75 #endif
77 /* magic for normal codecs */
78 #define CODEC_MAGIC 0x52434F44 /* RCOD */
79 /* magic for encoder codecs */
80 #define CODEC_ENC_MAGIC 0x52454E43 /* RENC */
82 /* increase this every time the api struct changes */
83 #define CODEC_API_VERSION 19
85 /* update this to latest version if a change to the api struct breaks
86 backwards compatibility (and please take the opportunity to sort in any
87 new function which are "waiting" at the end of the function table) */
88 #define CODEC_MIN_API_VERSION 18
90 /* codec return codes */
91 enum codec_status {
92 CODEC_OK = 0,
93 CODEC_USB_CONNECTED,
94 CODEC_ERROR = -1,
97 /* NOTE: To support backwards compatibility, only add new functions at
98 the end of the structure. Every time you add a new function,
99 remember to increase CODEC_API_VERSION. If you make changes to the
100 existing APIs then also update CODEC_MIN_API_VERSION to current
101 version
103 struct codec_api {
105 off_t filesize; /* Total file length */
106 off_t curpos; /* Current buffer position */
108 /* For gapless mp3 */
109 struct mp3entry *id3; /* TAG metadata pointer */
110 bool *taginfo_ready; /* Is metadata read */
112 /* Codec should periodically check if stop_codec is set to true.
113 In case it is, codec must return immediately */
114 bool stop_codec;
115 /* Codec should periodically check if new_track is non zero.
116 When it is, the codec should request a new track. */
117 int new_track;
118 /* If seek_time != 0, codec should seek to that song position (in ms)
119 if codec supports seeking. */
120 long seek_time;
122 /* Returns buffer to malloc array. Only codeclib should need this. */
123 void* (*get_codec_memory)(size_t *size);
124 /* Insert PCM data into audio buffer for playback. Playback will start
125 automatically. */
126 bool (*pcmbuf_insert)(const void *ch1, const void *ch2, int count);
127 /* Set song position in WPS (value in ms). */
128 void (*set_elapsed)(unsigned int value);
130 /* Read next <size> amount bytes from file buffer to <ptr>.
131 Will return number of bytes read or 0 if end of file. */
132 size_t (*read_filebuf)(void *ptr, size_t size);
133 /* Request pointer to file buffer which can be used to read
134 <realsize> amount of data. <reqsize> tells the buffer system
135 how much data it should try to allocate. If <realsize> is 0,
136 end of file is reached. */
137 void* (*request_buffer)(size_t *realsize, size_t reqsize);
138 /* Advance file buffer position by <amount> amount of bytes. */
139 void (*advance_buffer)(size_t amount);
140 /* Advance file buffer to a pointer location inside file buffer. */
141 void (*advance_buffer_loc)(void *ptr);
142 /* Seek file buffer to position <newpos> beginning of file. */
143 bool (*seek_buffer)(size_t newpos);
144 /* Codec should call this function when it has done the seeking. */
145 void (*seek_complete)(void);
146 /* Calculate mp3 seek position from given time data in ms. */
147 off_t (*mp3_get_filepos)(int newtime);
148 /* Request file change from file buffer. Returns true is next
149 track is available and changed. If return value is false,
150 codec should exit immediately with PLUGIN_OK status. */
151 bool (*request_next_track)(void);
152 /* Free the buffer area of the current codec after its loaded */
153 void (*discard_codec)(void);
155 void (*set_offset)(size_t value);
156 /* Configure different codec buffer parameters. */
157 void (*configure)(int setting, intptr_t value);
159 /* kernel/ system */
160 void (*PREFIX(sleep))(int ticks);
161 void (*yield)(void);
163 /* strings and memory */
164 char* (*strcpy)(char *dst, const char *src);
165 char* (*strncpy)(char *dst, const char *src, size_t length);
166 size_t (*strlen)(const char *str);
167 int (*strcmp)(const char *, const char *);
168 char *(*strcat)(char *s1, const char *s2);
169 void* (*memset)(void *dst, int c, size_t length);
170 void* (*memcpy)(void *out, const void *in, size_t n);
171 void* (*memmove)(void *out, const void *in, size_t n);
172 int (*memcmp)(const void *s1, const void *s2, size_t n);
173 void *(*memchr)(const void *s1, int c, size_t n);
175 #if defined(DEBUG) || defined(SIMULATOR)
176 void (*debugf)(const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2);
177 #endif
178 #ifdef ROCKBOX_HAS_LOGF
179 void (*logf)(const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2);
180 #endif
182 /* Tremor requires qsort */
183 void (*qsort)(void *base, size_t nmemb, size_t size,
184 int(*compar)(const void *, const void *));
186 /* The ADX codec accesses global_settings to test for REPEAT_ONE mode */
187 struct user_settings* global_settings;
189 #ifdef RB_PROFILE
190 void (*profile_thread)(void);
191 void (*profstop)(void);
192 void (*profile_func_enter)(void *this_fn, void *call_site);
193 void (*profile_func_exit)(void *this_fn, void *call_site);
194 #endif
196 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
197 volatile bool stop_encoder;
198 volatile int enc_codec_loaded; /* <0=error, 0=pending, >0=ok */
199 void (*enc_get_inputs)(struct enc_inputs *inputs);
200 void (*enc_set_parameters)(struct enc_parameters *params);
201 struct enc_chunk_hdr * (*enc_get_chunk)(void);
202 void (*enc_finish_chunk)(void);
203 int (*enc_pcm_buf_near_empty)(void);
204 unsigned char * (*enc_get_pcm_data)(size_t size);
205 size_t (*enc_unget_pcm_data)(size_t size);
207 /* file */
208 int (*PREFIX(open))(const char* pathname, int flags);
209 int (*close)(int fd);
210 ssize_t (*read)(int fd, void* buf, size_t count);
211 off_t (*PREFIX(lseek))(int fd, off_t offset, int whence);
212 ssize_t (*write)(int fd, const void* buf, size_t count);
214 /* Encoder codecs adjust CPU boost themselves */
215 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
216 #ifdef CPU_BOOST_LOGGING
217 void (*cpu_boost_)(bool on_off,char*location,int line);
218 #else
219 void (*cpu_boost)(bool on_off);
220 #endif
221 #endif
223 int (*round_value_to_list32)(unsigned long value,
224 const unsigned long list[],
225 int count,
226 bool signd);
228 #endif
230 /* new stuff at the end, sort into place next time
231 the API gets incompatible */
233 #ifdef CACHE_FUNCTIONS_AS_CALL
234 void (*flush_icache)(void);
235 void (*invalidate_icache)(void);
236 #endif
239 /* codec header */
240 struct codec_header {
241 unsigned long magic; /* RCOD or RENC */
242 unsigned short target_id;
243 unsigned short api_version;
244 unsigned char *load_addr;
245 unsigned char *end_addr;
246 enum codec_status(*entry_point)(struct codec_api*);
249 #ifdef CODEC
250 #ifndef SIMULATOR
251 /* plugin_* is correct, codecs use the plugin linker script */
252 extern unsigned char plugin_start_addr[];
253 extern unsigned char plugin_end_addr[];
254 /* decoders */
255 #define CODEC_HEADER \
256 const struct codec_header __header \
257 __attribute__ ((section (".header")))= { \
258 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
259 plugin_start_addr, plugin_end_addr, codec_start };
260 /* encoders */
261 #define CODEC_ENC_HEADER \
262 const struct codec_header __header \
263 __attribute__ ((section (".header")))= { \
264 CODEC_ENC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
265 plugin_start_addr, plugin_end_addr, codec_start };
267 #else /* def SIMULATOR */
268 /* decoders */
269 #define CODEC_HEADER \
270 const struct codec_header __header \
271 __attribute__((visibility("default"))) = { \
272 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
273 NULL, NULL, codec_start };
274 /* encoders */
275 #define CODEC_ENC_HEADER \
276 const struct codec_header __header = { \
277 CODEC_ENC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
278 NULL, NULL, codec_start };
279 #endif /* SIMULATOR */
280 #endif /* CODEC */
282 /* create full codec path from root filenames in audio_formats[]
283 assumes buffer size is MAX_PATH */
284 void codec_get_full_path(char *path, const char *codec_root_fn);
286 /* defined by the codec loader (codec.c) */
287 int codec_load_ram(char* codecptr, int size, void* ptr2, int bufwrap,
288 struct codec_api *api);
289 int codec_load_file(const char* codec, struct codec_api *api);
291 /* defined by the codec */
292 enum codec_status codec_start(struct codec_api* rockbox);
294 #ifndef CACHE_FUNCTION_WRAPPERS
296 #ifdef CACHE_FUNCTIONS_AS_CALL
297 #define CACHE_FUNCTION_WRAPPERS(api) \
298 void flush_icache(void) \
300 (api)->flush_icache(); \
302 void invalidate_icache(void) \
304 (api)->invalidate_icache(); \
306 #else
307 #define CACHE_FUNCTION_WRAPPERS(api)
308 #endif /* CACHE_FUNCTIONS_AS_CALL */
310 #endif /* CACHE_FUNCTION_WRAPPERS */
312 #endif