Introduce a small api for loading code (codecs,plugins) from disk/memory.
[maemo-rb.git] / apps / codecs.c
blobb072c65f407d32910691e6c2e0a21952256b913c
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 "load_code.h"
32 #include "debug.h"
33 #include "button.h"
34 #include "dir.h"
35 #include "file.h"
36 #include "kernel.h"
37 #include "screens.h"
38 #include "misc.h"
39 #include "mas.h"
40 #include "codecs.h"
41 #include "lang.h"
42 #include "keyboard.h"
43 #include "buffering.h"
44 #include "mp3_playback.h"
45 #include "backlight.h"
46 #include "storage.h"
47 #include "talk.h"
48 #include "mp3data.h"
49 #include "powermgmt.h"
50 #include "system.h"
51 #include "sound.h"
52 #include "splash.h"
53 #include "general.h"
55 #define LOGF_ENABLE
56 #include "logf.h"
58 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
60 #define PREFIX(_x_) sim_ ## _x_
61 #if CONFIG_CODEC == SWCODEC
62 unsigned char codecbuf[CODEC_SIZE];
63 #endif
64 void *sim_codec_load_ram(char* codecptr, int size, void **pd);
65 void sim_codec_close(void *pd);
67 #else /* !PLATFORM_HOSTED */
69 #define PREFIX
70 #define sim_codec_close(x)
72 #endif
74 size_t codec_size;
76 extern void* plugin_get_audio_buffer(size_t *buffer_size);
78 #if (CONFIG_PLATFORM & PLATFORM_NATIVE) && defined(HAVE_RECORDING)
79 #undef open
80 static int open(const char* pathname, int flags, ...)
82 return file_open(pathname, flags);
84 #endif
85 struct codec_api ci = {
87 0, /* filesize */
88 0, /* curpos */
89 NULL, /* id3 */
90 NULL, /* taginfo_ready */
91 false, /* stop_codec */
92 0, /* new_track */
93 0, /* seek_time */
94 NULL, /* struct dsp_config *dsp */
95 NULL, /* codec_get_buffer */
96 NULL, /* pcmbuf_insert */
97 NULL, /* set_elapsed */
98 NULL, /* read_filebuf */
99 NULL, /* request_buffer */
100 NULL, /* advance_buffer */
101 NULL, /* advance_buffer_loc */
102 NULL, /* seek_buffer */
103 NULL, /* seek_complete */
104 NULL, /* request_next_track */
105 NULL, /* discard_codec */
106 NULL, /* set_offset */
107 NULL, /* configure */
109 /* kernel/ system */
110 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
111 __div0,
112 #endif
113 PREFIX(sleep),
114 yield,
116 #if NUM_CORES > 1
117 create_thread,
118 thread_thaw,
119 thread_wait,
120 semaphore_init,
121 semaphore_wait,
122 semaphore_release,
123 #endif
125 #if NUM_CORES > 1
126 cpucache_flush,
127 cpucache_invalidate,
128 #endif
130 /* strings and memory */
131 strcpy,
132 strlen,
133 strcmp,
134 strcat,
135 memset,
136 memcpy,
137 memmove,
138 memcmp,
139 memchr,
140 strcasestr,
141 #if defined(DEBUG) || defined(SIMULATOR)
142 debugf,
143 #endif
144 #ifdef ROCKBOX_HAS_LOGF
145 logf,
146 #endif
148 (qsort_func)qsort,
149 &global_settings,
151 #ifdef RB_PROFILE
152 profile_thread,
153 profstop,
154 __cyg_profile_func_enter,
155 __cyg_profile_func_exit,
156 #endif
158 #ifdef HAVE_RECORDING
159 false, /* stop_encoder */
160 0, /* enc_codec_loaded */
161 enc_get_inputs,
162 enc_set_parameters,
163 enc_get_chunk,
164 enc_finish_chunk,
165 enc_get_pcm_data,
166 enc_unget_pcm_data,
168 /* file */
169 (open_func)PREFIX(open),
170 PREFIX(close),
171 (read_func)PREFIX(read),
172 PREFIX(lseek),
173 (write_func)PREFIX(write),
174 round_value_to_list32,
176 #endif /* HAVE_RECORDING */
178 /* new stuff at the end, sort into place next time
179 the API gets incompatible */
182 void codec_get_full_path(char *path, const char *codec_root_fn)
184 snprintf(path, MAX_PATH-1, "%s/%s." CODEC_EXTENSION,
185 CODECS_DIR, codec_root_fn);
188 static int codec_load_ram(void *handle, struct codec_api *api)
190 struct codec_header *hdr = lc_get_header(handle);
191 int status;
193 if (hdr == NULL
194 || (hdr->magic != CODEC_MAGIC
195 #ifdef HAVE_RECORDING
196 && hdr->magic != CODEC_ENC_MAGIC
197 #endif
199 || hdr->target_id != TARGET_ID
200 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
201 || hdr->load_addr != codecbuf
202 || hdr->end_addr > codecbuf + CODEC_SIZE
203 #endif
206 logf("codec header error");
207 lc_close(handle);
208 return CODEC_ERROR;
211 if (hdr->api_version > CODEC_API_VERSION
212 || hdr->api_version < CODEC_MIN_API_VERSION) {
213 logf("codec api version error");
214 lc_close(handle);
215 return CODEC_ERROR;
218 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
219 codec_size = hdr->end_addr - codecbuf;
220 #else
221 codec_size = 0;
222 #endif
224 *(hdr->api) = api;
225 status = hdr->entry_point();
227 lc_close(handle);
229 return status;
232 int codec_load_buf(unsigned int hid, struct codec_api *api)
234 int rc;
235 void *handle;
236 rc = bufread(hid, CODEC_SIZE, codecbuf);
237 if (rc < 0) {
238 logf("error loading codec");
239 return CODEC_ERROR;
241 handle = lc_open_from_mem(codecbuf, rc);
242 if (handle == NULL)
244 logf("error loading codec");
245 return CODEC_ERROR;
248 api->discard_codec();
249 return codec_load_ram(handle, api);
252 int codec_load_file(const char *plugin, struct codec_api *api)
254 char path[MAX_PATH];
255 void *handle;
257 codec_get_full_path(path, plugin);
259 handle = lc_open(path, codecbuf, CODEC_SIZE);
261 if (handle == NULL) {
262 logf("Codec load error");
263 splashf(HZ*2, "Couldn't load codec: %s", path);
264 return CODEC_ERROR;
267 return codec_load_ram(handle, api);