Oops.
[kugel-rb.git] / apps / codecs.c
blobc86aeda422b1e4acaceea0cf7519d12a86e445f6
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 "debug.h"
30 #include "button.h"
31 #include "dir.h"
32 #include "file.h"
33 #include "kernel.h"
34 #include "sprintf.h"
35 #include "screens.h"
36 #include "misc.h"
37 #include "mas.h"
38 #include "codecs.h"
39 #include "lang.h"
40 #include "keyboard.h"
41 #include "mpeg.h"
42 #include "buffer.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 #ifdef SIMULATOR
59 #define PREFIX(_x_) sim_ ## _x_
60 #else
61 #define PREFIX
62 #endif
64 #ifdef SIMULATOR
65 #if CONFIG_CODEC == SWCODEC
66 unsigned char codecbuf[CODEC_SIZE];
67 #endif
68 void *sim_codec_load_ram(char* codecptr, int size, void **pd);
69 void sim_codec_close(void *pd);
70 #else
71 #define sim_codec_close(x)
72 #endif
74 size_t codec_size;
76 extern void* plugin_get_audio_buffer(size_t *buffer_size);
78 struct codec_api ci = {
80 0, /* filesize */
81 0, /* curpos */
82 NULL, /* id3 */
83 NULL, /* taginfo_ready */
84 false, /* stop_codec */
85 0, /* new_track */
86 0, /* seek_time */
87 NULL, /* struct dsp_config *dsp */
88 NULL, /* codec_get_buffer */
89 NULL, /* pcmbuf_insert */
90 NULL, /* set_elapsed */
91 NULL, /* read_filebuf */
92 NULL, /* request_buffer */
93 NULL, /* advance_buffer */
94 NULL, /* advance_buffer_loc */
95 NULL, /* seek_buffer */
96 NULL, /* seek_complete */
97 NULL, /* request_next_track */
98 NULL, /* discard_codec */
99 NULL, /* set_offset */
100 NULL, /* configure */
102 /* kernel/ system */
103 #ifdef CPU_ARM
104 __div0,
105 #endif
106 PREFIX(sleep),
107 yield,
109 #if NUM_CORES > 1
110 create_thread,
111 thread_thaw,
112 thread_wait,
113 semaphore_init,
114 semaphore_wait,
115 semaphore_release,
116 #endif
118 #if NUM_CORES > 1
119 cpucache_flush,
120 cpucache_invalidate,
121 #endif
123 /* strings and memory */
124 strcpy,
125 strncpy,
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 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
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 close,
165 (read_func)read,
166 PREFIX(lseek),
167 (write_func)write,
168 round_value_to_list32,
170 #endif
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, CODECS_DIR "/%s." CODEC_EXTENSION,
179 codec_root_fn);
182 static int codec_load_ram(int size, struct codec_api *api)
184 struct codec_header *hdr;
185 int status;
186 #ifndef SIMULATOR
187 hdr = (struct codec_header *)codecbuf;
189 if (size <= (signed)sizeof(struct codec_header)
190 || (hdr->magic != CODEC_MAGIC
191 #ifdef HAVE_RECORDING
192 && hdr->magic != CODEC_ENC_MAGIC
193 #endif
195 || hdr->target_id != TARGET_ID
196 || hdr->load_addr != codecbuf
197 || hdr->end_addr > codecbuf + CODEC_SIZE)
199 logf("codec header error");
200 return CODEC_ERROR;
203 codec_size = hdr->end_addr - codecbuf;
205 #else /* SIMULATOR */
206 void *pd;
208 hdr = sim_codec_load_ram(codecbuf, size, &pd);
210 if (pd == NULL)
211 return CODEC_ERROR;
213 if (hdr == NULL
214 || (hdr->magic != CODEC_MAGIC
215 #ifdef HAVE_RECORDING
216 && hdr->magic != CODEC_ENC_MAGIC
217 #endif
219 || hdr->target_id != TARGET_ID) {
220 sim_codec_close(pd);
221 return CODEC_ERROR;
224 codec_size = codecbuf - codecbuf;
226 #endif /* SIMULATOR */
227 if (hdr->api_version > CODEC_API_VERSION
228 || hdr->api_version < CODEC_MIN_API_VERSION) {
229 sim_codec_close(pd);
230 return CODEC_ERROR;
233 *(hdr->api) = api;
234 cpucache_invalidate();
235 status = hdr->entry_point();
237 sim_codec_close(pd);
239 return status;
242 int codec_load_buf(unsigned int hid, struct codec_api *api)
244 int rc;
245 rc = bufread(hid, CODEC_SIZE, codecbuf);
246 if (rc < 0) {
247 logf("error loading codec");
248 return CODEC_ERROR;
250 api->discard_codec();
251 return codec_load_ram(rc, api);
254 int codec_load_file(const char *plugin, struct codec_api *api)
256 char path[MAX_PATH];
257 int fd;
258 int rc;
260 codec_get_full_path(path, plugin);
262 fd = open(path, O_RDONLY);
263 if (fd < 0) {
264 logf("Codec load error:%d", fd);
265 splashf(HZ*2, "Couldn't load codec: %s", path);
266 return fd;
269 rc = read(fd, &codecbuf[0], CODEC_SIZE);
270 close(fd);
271 if (rc <= 0) {
272 logf("Codec read error");
273 return CODEC_ERROR;
276 return codec_load_ram((size_t)rc, api);