Get the NSF internal playlists working again. Fix a few wrongdoings in the metadata...
[kugel-rb.git] / apps / codecs.c
blob25ace4969b246073f614226b0d7123408832772b
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 "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 #if (CONFIG_PLATFORM & (PLATFORM_SDL|PLATFORM_MAEMO|PLATFORM_PANDORA))
58 #define PREFIX(_x_) sim_ ## _x_
59 #else
60 #define PREFIX(_x_) _x_
61 #endif
63 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
64 #if CONFIG_CODEC == SWCODEC
65 unsigned char codecbuf[CODEC_SIZE];
66 #endif
67 #endif
69 size_t codec_size;
71 extern void* plugin_get_audio_buffer(size_t *buffer_size);
73 #if (CONFIG_PLATFORM & PLATFORM_NATIVE) && defined(HAVE_RECORDING)
74 #undef open
75 static int open(const char* pathname, int flags, ...)
77 return file_open(pathname, flags);
79 #endif
80 struct codec_api ci = {
82 0, /* filesize */
83 0, /* curpos */
84 NULL, /* id3 */
85 ERR_HANDLE_NOT_FOUND, /* audio_hid */
86 NULL, /* struct dsp_config *dsp */
87 NULL, /* codec_get_buffer */
88 NULL, /* pcmbuf_insert */
89 NULL, /* set_elapsed */
90 NULL, /* read_filebuf */
91 NULL, /* request_buffer */
92 NULL, /* advance_buffer */
93 NULL, /* seek_buffer */
94 NULL, /* seek_complete */
95 NULL, /* set_offset */
96 NULL, /* configure */
97 NULL, /* get_command */
99 /* kernel/ system */
100 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
101 __div0,
102 #endif
103 sleep,
104 yield,
106 #if NUM_CORES > 1
107 create_thread,
108 thread_thaw,
109 thread_wait,
110 semaphore_init,
111 semaphore_wait,
112 semaphore_release,
113 #endif
115 cpucache_flush,
116 cpucache_invalidate,
118 /* strings and memory */
119 strcpy,
120 strlen,
121 strcmp,
122 strcat,
123 memset,
124 memcpy,
125 memmove,
126 memcmp,
127 memchr,
128 strcasestr,
129 #if defined(DEBUG) || defined(SIMULATOR)
130 debugf,
131 #endif
132 #ifdef ROCKBOX_HAS_LOGF
133 logf,
134 #endif
136 (qsort_func)qsort,
137 &global_settings,
139 #ifdef RB_PROFILE
140 profile_thread,
141 profstop,
142 __cyg_profile_func_enter,
143 __cyg_profile_func_exit,
144 #endif
146 #ifdef HAVE_RECORDING
147 enc_get_inputs,
148 enc_set_parameters,
149 enc_get_chunk,
150 enc_finish_chunk,
151 enc_get_pcm_data,
152 enc_unget_pcm_data,
154 /* file */
155 (open_func)PREFIX(open),
156 PREFIX(close),
157 (read_func)PREFIX(read),
158 PREFIX(lseek),
159 (write_func)PREFIX(write),
160 round_value_to_list32,
162 #endif /* HAVE_RECORDING */
164 /* new stuff at the end, sort into place next time
165 the API gets incompatible */
168 void codec_get_full_path(char *path, const char *codec_root_fn)
170 snprintf(path, MAX_PATH-1, "%s/%s." CODEC_EXTENSION,
171 CODECS_DIR, codec_root_fn);
174 /** codec loading and call interface **/
175 static void *curr_handle = NULL;
176 static struct codec_header *c_hdr = NULL;
178 static int codec_load_ram(struct codec_api *api)
180 struct lc_header *hdr;
182 c_hdr = lc_get_header(curr_handle);
183 hdr = c_hdr ? &c_hdr->lc_hdr : NULL;
185 if (hdr == NULL
186 || (hdr->magic != CODEC_MAGIC
187 #ifdef HAVE_RECORDING
188 && hdr->magic != CODEC_ENC_MAGIC
189 #endif
191 || hdr->target_id != TARGET_ID
192 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
193 || hdr->load_addr != codecbuf
194 || hdr->end_addr > codecbuf + CODEC_SIZE
195 #endif
198 logf("codec header error");
199 lc_close(curr_handle);
200 curr_handle = NULL;
201 return CODEC_ERROR;
204 if (hdr->api_version > CODEC_API_VERSION
205 || hdr->api_version < CODEC_MIN_API_VERSION) {
206 logf("codec api version error");
207 lc_close(curr_handle);
208 curr_handle = NULL;
209 return CODEC_ERROR;
212 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
213 codec_size = hdr->end_addr - codecbuf;
214 #else
215 codec_size = 0;
216 #endif
218 *(c_hdr->api) = api;
220 logf("Codec: calling entrypoint");
221 return c_hdr->entry_point(CODEC_LOAD);
224 int codec_load_buf(int hid, struct codec_api *api)
226 int rc = bufread(hid, CODEC_SIZE, codecbuf);
228 if (rc < 0) {
229 logf("Codec: cannot read buf handle");
230 return CODEC_ERROR;
233 curr_handle = lc_open_from_mem(codecbuf, rc);
235 if (curr_handle == NULL) {
236 logf("Codec: load error");
237 return CODEC_ERROR;
240 return codec_load_ram(api);
243 int codec_load_file(const char *plugin, struct codec_api *api)
245 char path[MAX_PATH];
247 codec_get_full_path(path, plugin);
249 curr_handle = lc_open(path, codecbuf, CODEC_SIZE);
251 if (curr_handle == NULL) {
252 logf("Codec: cannot read file");
253 return CODEC_ERROR;
256 return codec_load_ram(api);
259 int codec_run_proc(void)
261 if (curr_handle == NULL) {
262 logf("Codec: no codec to run");
263 return CODEC_ERROR;
266 logf("Codec: entering run state");
267 return c_hdr->run_proc();
270 int codec_close(void)
272 int status = CODEC_OK;
274 if (curr_handle != NULL) {
275 logf("Codec: cleaning up");
276 status = c_hdr->entry_point(CODEC_UNLOAD);
277 lc_close(curr_handle);
278 curr_handle = NULL;
281 return status;