new plugin: FS#10559 - lrcplayer: a plugin to view .lrc file.
[kugel-rb.git] / apps / codecs.c
blob46d92235606641d6d8de6eae9a0ac57b2ad1fc81
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 #include "config.h"
23 #include <stdbool.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <timefuncs.h>
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include "string-extra.h"
31 #include "debug.h"
32 #include "button.h"
33 #include "dir.h"
34 #include "file.h"
35 #include "kernel.h"
36 #include "screens.h"
37 #include "misc.h"
38 #include "mas.h"
39 #include "codecs.h"
40 #include "lang.h"
41 #include "keyboard.h"
42 #include "buffering.h"
43 #include "mp3_playback.h"
44 #include "backlight.h"
45 #include "storage.h"
46 #include "talk.h"
47 #include "mp3data.h"
48 #include "powermgmt.h"
49 #include "system.h"
50 #include "sound.h"
51 #include "splash.h"
52 #include "general.h"
54 #define LOGF_ENABLE
55 #include "logf.h"
57 #ifdef SIMULATOR
58 #define PREFIX(_x_) sim_ ## _x_
59 #else
60 #define PREFIX
61 #endif
63 #ifdef SIMULATOR
64 #if CONFIG_CODEC == SWCODEC
65 unsigned char codecbuf[CODEC_SIZE];
66 #endif
67 void *sim_codec_load_ram(char* codecptr, int size, void **pd);
68 void sim_codec_close(void *pd);
69 #else
70 #define sim_codec_close(x)
71 #endif
73 size_t codec_size;
75 extern void* plugin_get_audio_buffer(size_t *buffer_size);
77 #undef open
78 static int open(const char* pathname, int flags, ...)
80 #ifdef SIMULATOR
81 int fd;
82 if (flags & O_CREAT)
84 va_list ap;
85 va_start(ap, flags);
86 fd = sim_open(pathname, flags, va_arg(ap, unsigned int));
87 va_end(ap);
89 else
90 fd = sim_open(pathname, flags);
92 return fd;
93 #else
94 return file_open(pathname, flags);
95 #endif
97 struct codec_api ci = {
99 0, /* filesize */
100 0, /* curpos */
101 NULL, /* id3 */
102 NULL, /* taginfo_ready */
103 false, /* stop_codec */
104 0, /* new_track */
105 0, /* seek_time */
106 NULL, /* struct dsp_config *dsp */
107 NULL, /* codec_get_buffer */
108 NULL, /* pcmbuf_insert */
109 NULL, /* set_elapsed */
110 NULL, /* read_filebuf */
111 NULL, /* request_buffer */
112 NULL, /* advance_buffer */
113 NULL, /* advance_buffer_loc */
114 NULL, /* seek_buffer */
115 NULL, /* seek_complete */
116 NULL, /* request_next_track */
117 NULL, /* discard_codec */
118 NULL, /* set_offset */
119 NULL, /* configure */
121 /* kernel/ system */
122 #ifdef CPU_ARM
123 __div0,
124 #endif
125 PREFIX(sleep),
126 yield,
128 #if NUM_CORES > 1
129 create_thread,
130 thread_thaw,
131 thread_wait,
132 semaphore_init,
133 semaphore_wait,
134 semaphore_release,
135 #endif
137 #if NUM_CORES > 1
138 cpucache_flush,
139 cpucache_invalidate,
140 #endif
142 /* strings and memory */
143 strcpy,
144 strlen,
145 strcmp,
146 strcat,
147 memset,
148 memcpy,
149 memmove,
150 memcmp,
151 memchr,
152 strcasestr,
153 #if defined(DEBUG) || defined(SIMULATOR)
154 debugf,
155 #endif
156 #ifdef ROCKBOX_HAS_LOGF
157 logf,
158 #endif
160 (qsort_func)qsort,
161 &global_settings,
163 #ifdef RB_PROFILE
164 profile_thread,
165 profstop,
166 __cyg_profile_func_enter,
167 __cyg_profile_func_exit,
168 #endif
170 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
171 false, /* stop_encoder */
172 0, /* enc_codec_loaded */
173 enc_get_inputs,
174 enc_set_parameters,
175 enc_get_chunk,
176 enc_finish_chunk,
177 enc_get_pcm_data,
178 enc_unget_pcm_data,
180 /* file */
181 (open_func)PREFIX(open),
182 close,
183 (read_func)read,
184 PREFIX(lseek),
185 (write_func)write,
186 round_value_to_list32,
188 #endif
190 /* new stuff at the end, sort into place next time
191 the API gets incompatible */
194 void codec_get_full_path(char *path, const char *codec_root_fn)
196 snprintf(path, MAX_PATH-1, CODECS_DIR "/%s." CODEC_EXTENSION,
197 codec_root_fn);
200 static int codec_load_ram(int size, struct codec_api *api)
202 struct codec_header *hdr;
203 int status;
204 #ifndef SIMULATOR
205 hdr = (struct codec_header *)codecbuf;
207 if (size <= (signed)sizeof(struct codec_header)
208 || (hdr->magic != CODEC_MAGIC
209 #ifdef HAVE_RECORDING
210 && hdr->magic != CODEC_ENC_MAGIC
211 #endif
213 || hdr->target_id != TARGET_ID
214 || hdr->load_addr != codecbuf
215 || hdr->end_addr > codecbuf + CODEC_SIZE)
217 logf("codec header error");
218 return CODEC_ERROR;
221 codec_size = hdr->end_addr - codecbuf;
223 #else /* SIMULATOR */
224 void *pd;
226 hdr = sim_codec_load_ram(codecbuf, size, &pd);
228 if (pd == NULL)
229 return CODEC_ERROR;
231 if (hdr == NULL
232 || (hdr->magic != CODEC_MAGIC
233 #ifdef HAVE_RECORDING
234 && hdr->magic != CODEC_ENC_MAGIC
235 #endif
237 || hdr->target_id != TARGET_ID) {
238 sim_codec_close(pd);
239 return CODEC_ERROR;
242 codec_size = codecbuf - codecbuf;
244 #endif /* SIMULATOR */
245 if (hdr->api_version > CODEC_API_VERSION
246 || hdr->api_version < CODEC_MIN_API_VERSION) {
247 sim_codec_close(pd);
248 return CODEC_ERROR;
251 *(hdr->api) = api;
252 cpucache_invalidate();
253 status = hdr->entry_point();
255 sim_codec_close(pd);
257 return status;
260 int codec_load_buf(unsigned int hid, struct codec_api *api)
262 int rc;
263 rc = bufread(hid, CODEC_SIZE, codecbuf);
264 if (rc < 0) {
265 logf("error loading codec");
266 return CODEC_ERROR;
268 api->discard_codec();
269 return codec_load_ram(rc, api);
272 int codec_load_file(const char *plugin, struct codec_api *api)
274 char path[MAX_PATH];
275 int fd;
276 int rc;
278 codec_get_full_path(path, plugin);
280 fd = open(path, O_RDONLY);
281 if (fd < 0) {
282 logf("Codec load error:%d", fd);
283 splashf(HZ*2, "Couldn't load codec: %s", path);
284 return fd;
287 rc = read(fd, &codecbuf[0], CODEC_SIZE);
288 close(fd);
289 if (rc <= 0) {
290 logf("Codec read error");
291 return CODEC_ERROR;
294 return codec_load_ram((size_t)rc, api);