Extend lc_open() to also being able to load overlay plugins.
[maemo-rb.git] / apps / codecs.c
blob3fa05be59a33df674e11721b39837118a6747a60
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_SDL)
59 #define PREFIX(_x_) sim_ ## _x_
60 #else
61 #define PREFIX(_x_) _x_
62 #endif
64 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
65 #if CONFIG_CODEC == SWCODEC
66 unsigned char codecbuf[CODEC_SIZE];
67 #endif
68 #endif
70 size_t codec_size;
72 extern void* plugin_get_audio_buffer(size_t *buffer_size);
74 #if (CONFIG_PLATFORM & PLATFORM_NATIVE) && defined(HAVE_RECORDING)
75 #undef open
76 static int open(const char* pathname, int flags, ...)
78 return file_open(pathname, flags);
80 #endif
81 struct codec_api ci = {
83 0, /* filesize */
84 0, /* curpos */
85 NULL, /* id3 */
86 NULL, /* taginfo_ready */
87 false, /* stop_codec */
88 0, /* new_track */
89 0, /* seek_time */
90 NULL, /* struct dsp_config *dsp */
91 NULL, /* codec_get_buffer */
92 NULL, /* pcmbuf_insert */
93 NULL, /* set_elapsed */
94 NULL, /* read_filebuf */
95 NULL, /* request_buffer */
96 NULL, /* advance_buffer */
97 NULL, /* advance_buffer_loc */
98 NULL, /* seek_buffer */
99 NULL, /* seek_complete */
100 NULL, /* request_next_track */
101 NULL, /* discard_codec */
102 NULL, /* set_offset */
103 NULL, /* configure */
105 /* kernel/ system */
106 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
107 __div0,
108 #endif
109 sleep,
110 yield,
112 #if NUM_CORES > 1
113 create_thread,
114 thread_thaw,
115 thread_wait,
116 semaphore_init,
117 semaphore_wait,
118 semaphore_release,
119 #endif
121 cpucache_flush,
122 cpucache_invalidate,
124 /* strings and memory */
125 strcpy,
126 strlen,
127 strcmp,
128 strcat,
129 memset,
130 memcpy,
131 memmove,
132 memcmp,
133 memchr,
134 strcasestr,
135 #if defined(DEBUG) || defined(SIMULATOR)
136 debugf,
137 #endif
138 #ifdef ROCKBOX_HAS_LOGF
139 logf,
140 #endif
142 (qsort_func)qsort,
143 &global_settings,
145 #ifdef RB_PROFILE
146 profile_thread,
147 profstop,
148 __cyg_profile_func_enter,
149 __cyg_profile_func_exit,
150 #endif
152 #ifdef HAVE_RECORDING
153 false, /* stop_encoder */
154 0, /* enc_codec_loaded */
155 enc_get_inputs,
156 enc_set_parameters,
157 enc_get_chunk,
158 enc_finish_chunk,
159 enc_get_pcm_data,
160 enc_unget_pcm_data,
162 /* file */
163 (open_func)PREFIX(open),
164 PREFIX(close),
165 (read_func)PREFIX(read),
166 PREFIX(lseek),
167 (write_func)PREFIX(write),
168 round_value_to_list32,
170 #endif /* HAVE_RECORDING */
172 /* new stuff at the end, sort into place next time
173 the API gets incompatible */
176 void codec_get_full_path(char *path, const char *codec_root_fn)
178 snprintf(path, MAX_PATH-1, "%s/%s." CODEC_EXTENSION,
179 CODECS_DIR, codec_root_fn);
182 static int codec_load_ram(void *handle, struct codec_api *api)
184 struct codec_header *c_hdr = lc_get_header(handle);
185 struct lc_header *hdr = c_hdr ? &c_hdr->lc_hdr : NULL;
186 int status;
188 if (hdr == NULL
189 || (hdr->magic != CODEC_MAGIC
190 #ifdef HAVE_RECORDING
191 && hdr->magic != CODEC_ENC_MAGIC
192 #endif
194 || hdr->target_id != TARGET_ID
195 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
196 || hdr->load_addr != codecbuf
197 || hdr->end_addr > codecbuf + CODEC_SIZE
198 #endif
201 logf("codec header error");
202 lc_close(handle);
203 return CODEC_ERROR;
206 if (hdr->api_version > CODEC_API_VERSION
207 || hdr->api_version < CODEC_MIN_API_VERSION) {
208 logf("codec api version error");
209 lc_close(handle);
210 return CODEC_ERROR;
213 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
214 codec_size = hdr->end_addr - codecbuf;
215 #else
216 codec_size = 0;
217 #endif
219 *(c_hdr->api) = api;
220 status = c_hdr->entry_point();
222 lc_close(handle);
224 return status;
227 int codec_load_buf(unsigned int hid, struct codec_api *api)
229 int rc;
230 void *handle;
231 rc = bufread(hid, CODEC_SIZE, codecbuf);
232 if (rc < 0) {
233 logf("error loading codec");
234 return CODEC_ERROR;
236 handle = lc_open_from_mem(codecbuf, rc);
237 if (handle == NULL)
239 logf("error loading codec");
240 return CODEC_ERROR;
243 api->discard_codec();
244 return codec_load_ram(handle, api);
247 int codec_load_file(const char *plugin, struct codec_api *api)
249 char path[MAX_PATH];
250 void *handle;
252 codec_get_full_path(path, plugin);
254 handle = lc_open(path, codecbuf, CODEC_SIZE);
256 if (handle == NULL) {
257 logf("Codec load error");
258 splashf(HZ*2, "Couldn't load codec: %s", path);
259 return CODEC_ERROR;
262 return codec_load_ram(handle, api);