1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
22 /* instruct simulator code to not redefine any symbols when compiling codecs.
23 (the CODEC macro is defined in apps/codecs/Makefile) */
25 #define NO_REDEFINES_PLEASE
35 #include <sys/types.h>
44 #include "mp3_playback.h"
48 #if (CONFIG_CODEC == SWCODEC)
49 #if !defined(SIMULATOR)
50 #include "pcm_record.h"
62 #if defined(DEBUG) || defined(SIMULATOR)
64 #define DEBUGF rb->debugf
66 #define LDEBUGF rb->debugf
72 #ifdef ROCKBOX_HAS_LOGF
82 #define PREFIX(_x_) sim_ ## _x_
84 #define PREFIX(_x_) _x_
87 #define CODEC_MAGIC 0x52434F44 /* RCOD */
89 /* increase this every time the api struct changes */
90 #define CODEC_API_VERSION 9
92 /* update this to latest version if a change to the api struct breaks
93 backwards compatibility (and please take the opportunity to sort in any
94 new function which are "waiting" at the end of the function table) */
95 #define CODEC_MIN_API_VERSION 8
97 /* codec return codes */
104 /* NOTE: To support backwards compatibility, only add new functions at
105 the end of the structure. Every time you add a new function,
106 remember to increase CODEC_API_VERSION. If you make changes to the
107 existing APIs then also update CODEC_MIN_API_VERSION to current
112 off_t filesize
; /* Total file length */
113 off_t curpos
; /* Current buffer position */
115 /* For gapless mp3 */
116 struct mp3entry
*id3
; /* TAG metadata pointer */
117 bool *taginfo_ready
; /* Is metadata read */
119 /* Codec should periodically check if stop_codec is set to true.
120 In case it is, codec must return immediately */
122 /* Codec should periodically check if new_track is non zero.
123 When it is, the codec should request a new track. */
125 /* If seek_time != 0, codec should seek to that song position (in ms)
126 if codec supports seeking. */
129 /* Returns buffer to malloc array. Only codeclib should need this. */
130 void* (*get_codec_memory
)(size_t *size
);
131 /* Insert PCM data into audio buffer for playback. Playback will start
133 bool (*pcmbuf_insert
)(const char *data
, size_t length
);
134 bool (*pcmbuf_insert_split
)(const void *ch1
, const void *ch2
, size_t length
);
135 /* Set song position in WPS (value in ms). */
136 void (*set_elapsed
)(unsigned int value
);
138 /* Read next <size> amount bytes from file buffer to <ptr>.
139 Will return number of bytes read or 0 if end of file. */
140 size_t (*read_filebuf
)(void *ptr
, size_t size
);
141 /* Request pointer to file buffer which can be used to read
142 <realsize> amount of data. <reqsize> tells the buffer system
143 how much data it should try to allocate. If <realsize> is 0,
144 end of file is reached. */
145 void* (*request_buffer
)(size_t *realsize
, size_t reqsize
);
146 /* Advance file buffer position by <amount> amount of bytes. */
147 void (*advance_buffer
)(size_t amount
);
148 /* Advance file buffer to a pointer location inside file buffer. */
149 void (*advance_buffer_loc
)(void *ptr
);
150 /* Seek file buffer to position <newpos> beginning of file. */
151 bool (*seek_buffer
)(size_t newpos
);
152 /* Codec should call this function when it has done the seeking. */
153 void (*seek_complete
)(void);
154 /* Calculate mp3 seek position from given time data in ms. */
155 off_t (*mp3_get_filepos
)(int newtime
);
156 /* Request file change from file buffer. Returns true is next
157 track is available and changed. If return value is false,
158 codec should exit immediately with PLUGIN_OK status. */
159 bool (*request_next_track
)(void);
160 /* Free the buffer area of the current codec after its loaded */
161 void (*discard_codec
)(void);
163 void (*set_offset
)(size_t value
);
164 /* Configure different codec buffer parameters. */
165 void (*configure
)(int setting
, void *value
);
167 void (*splash
)(int ticks
, bool center
, const unsigned char *fmt
, ...);
170 int (*PREFIX(open
))(const char* pathname
, int flags
);
171 int (*close
)(int fd
);
172 ssize_t (*read
)(int fd
, void* buf
, size_t count
);
173 off_t (*PREFIX(lseek
))(int fd
, off_t offset
, int whence
);
174 int (*PREFIX(creat
))(const char *pathname
, mode_t mode
);
175 ssize_t (*write
)(int fd
, const void* buf
, size_t count
);
176 int (*PREFIX(remove
))(const char* pathname
);
177 int (*PREFIX(rename
))(const char* path
, const char* newname
);
178 int (*PREFIX(ftruncate
))(int fd
, off_t length
);
180 int (*fdprintf
)(int fd
, const char *fmt
, ...);
181 int (*read_line
)(int fd
, char* buffer
, int buffer_size
);
182 bool (*settings_parseline
)(char* line
, char** name
, char** value
);
184 void (*ata_sleep
)(void);
188 DIR* (*PREFIX(opendir
))(const char* name
);
189 int (*PREFIX(closedir
))(DIR* dir
);
190 struct dirent
* (*PREFIX(readdir
))(DIR* dir
);
191 int (*PREFIX(mkdir
))(const char *name
, int mode
);
194 void (*PREFIX(sleep
))(int ticks
);
197 long (*default_event_handler
)(long event
);
198 long (*default_event_handler_ex
)(long event
, void (*callback
)(void *), void *parameter
);
199 struct thread_entry
* (*create_thread
)(unsigned int core
, void (*function
)(void),
200 void* stack
, int stack_size
, const char *name
201 IF_PRIO(, int priority
));
202 void (*remove_thread
)(struct thread_entry
*thread
);
203 void (*reset_poweroff_timer
)(void);
205 int (*system_memory_guard
)(int newmode
);
207 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
208 void (*cpu_boost
)(bool on_off
);
212 /* strings and memory */
213 int (*snprintf
)(char *buf
, size_t size
, const char *fmt
, ...);
214 char* (*strcpy
)(char *dst
, const char *src
);
215 char* (*strncpy
)(char *dst
, const char *src
, size_t length
);
216 size_t (*strlen
)(const char *str
);
217 char * (*strrchr
)(const char *s
, int c
);
218 int (*strcmp
)(const char *, const char *);
219 int (*strcasecmp
)(const char *, const char *);
220 int (*strncasecmp
)(const char *s1
, const char *s2
, size_t n
);
221 void* (*memset
)(void *dst
, int c
, size_t length
);
222 void* (*memcpy
)(void *out
, const void *in
, size_t n
);
223 void* (*memmove
)(void *out
, const void *in
, size_t n
);
225 int (*atoi
)(const char *str
);
226 char *(*strchr
)(const char *s
, int c
);
227 char *(*strcat
)(char *s1
, const char *s2
);
228 int (*memcmp
)(const void *s1
, const void *s2
, size_t n
);
229 char *(*strcasestr
) (const char* phaystack
, const char* pneedle
);
230 void *(*memchr
)(const void *s1
, int c
, size_t n
);
233 void (*sound_set
)(int setting
, int value
);
235 void (*mp3_play_data
)(const unsigned char* start
, int size
, void (*get_more
)(unsigned char** start
, int* size
));
236 void (*mp3_play_pause
)(bool play
);
237 void (*mp3_play_stop
)(void);
238 bool (*mp3_is_playing
)(void);
239 #endif /* !SIMULATOR */
241 /* playback control */
242 void (*PREFIX(audio_play
))(long offset
);
243 void (*audio_stop
)(void);
244 void (*audio_pause
)(void);
245 void (*audio_resume
)(void);
246 void (*audio_next
)(void);
247 void (*audio_prev
)(void);
248 void (*audio_ff_rewind
)(long newtime
);
249 struct mp3entry
* (*audio_next_track
)(void);
250 int (*playlist_amount
)(void);
251 int (*audio_status
)(void);
252 bool (*audio_has_changed_track
)(void);
253 struct mp3entry
* (*audio_current_track
)(void);
254 void (*audio_flush_and_reload_tracks
)(void);
255 int (*audio_get_file_pos
)(void);
258 void (*srand
)(unsigned int seed
);
260 void (*qsort
)(void *base
, size_t nmemb
, size_t size
,
261 int(*compar
)(const void *, const void *));
262 int (*kbd_input
)(char* buffer
, int buflen
);
263 struct tm
* (*get_time
)(void);
264 int (*set_time
)(const struct tm
*tm
);
265 void* (*plugin_get_audio_buffer
)(int* buffer_size
);
267 #if defined(DEBUG) || defined(SIMULATOR)
268 void (*debugf
)(const char *fmt
, ...);
270 #ifdef ROCKBOX_HAS_LOGF
271 void (*logf
)(const char *fmt
, ...);
273 struct user_settings
* global_settings
;
274 bool (*mp3info
)(struct mp3entry
*entry
, const char *filename
, bool v1first
);
275 int (*count_mp3_frames
)(int fd
, int startpos
, int filesize
,
276 void (*progressfunc
)(int));
277 int (*create_xing_header
)(int fd
, long startpos
, long filesize
,
278 unsigned char *buf
, unsigned long num_frames
,
279 unsigned long rec_time
, unsigned long header_template
,
280 void (*progressfunc
)(int), bool generate_toc
);
281 unsigned long (*find_next_frame
)(int fd
, long *offset
,
282 long max_offset
, unsigned long last_header
);
283 int (*battery_level
)(void);
284 bool (*battery_level_safe
)(void);
287 void (*profile_thread
)(void);
288 void (*profstop
)(void);
289 void (*profile_func_enter
)(void *this_fn
, void *call_site
);
290 void (*profile_func_exit
)(void *this_fn
, void *call_site
);
293 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
294 bool enc_codec_loaded
;
295 void (*enc_get_inputs
)(int *buffer_size
,
296 int *channels
, int *quality
);
297 void (*enc_set_parameters
)(int chunk_size
, int num_chunks
,
298 int samp_per_chunk
, char *head_ptr
, int head_size
,
300 unsigned int* (*enc_alloc_chunk
)(void);
301 void (*enc_free_chunk
)(void);
302 int (*enc_wavbuf_near_empty
)(void);
303 char* (*enc_get_wav_data
)(int size
);
304 void (**enc_set_header_callback
)(void *head_buffer
,
305 int head_size
, int num_samples
, bool is_file_header
);
308 /* new stuff at the end, sort into place next time
309 the API gets incompatible */
314 struct codec_header
{
316 unsigned short target_id
;
317 unsigned short api_version
;
318 unsigned char *load_addr
;
319 unsigned char *end_addr
;
320 enum codec_status(*entry_point
)(struct codec_api
*);
324 /* plugin_* is correct, codecs use the plugin linker script */
325 extern unsigned char plugin_start_addr
[];
326 extern unsigned char plugin_end_addr
[];
327 #define CODEC_HEADER \
328 const struct codec_header __header \
329 __attribute__ ((section (".header")))= { \
330 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
331 plugin_start_addr, plugin_end_addr, codec_start };
332 #else /* SIMULATOR */
333 #define CODEC_HEADER \
334 const struct codec_header __header = { \
335 CODEC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
336 NULL, NULL, codec_start };
340 /* create full codec path from filenames in audio_formats[]
341 assumes buffer size is MAX_PATH */
342 void codec_get_full_path(char *path
, const char *codec_fn
);
344 /* defined by the codec loader (codec.c) */
345 int codec_load_ram(char* codecptr
, int size
, void* ptr2
, int bufwrap
,
346 struct codec_api
*api
);
347 int codec_load_file(const char* codec
, struct codec_api
*api
);
349 /* defined by the codec */
350 enum codec_status
codec_start(struct codec_api
* rockbox
);