Don't use the same completion_event for both directions. This could cause problems...
[kugel-rb.git] / apps / codecs.c
blob45b0fa7790bca9fff11cb46eec2042f569547fc0
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 "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 struct codec_api ci = {
79 0, /* filesize */
80 0, /* curpos */
81 NULL, /* id3 */
82 NULL, /* taginfo_ready */
83 false, /* stop_codec */
84 0, /* new_track */
85 0, /* seek_time */
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, /* advance_buffer_loc */
94 NULL, /* seek_buffer */
95 NULL, /* seek_complete */
96 NULL, /* request_next_track */
97 NULL, /* discard_codec */
98 NULL, /* set_offset */
99 NULL, /* configure */
101 /* kernel/ system */
102 #ifdef CPU_ARM
103 __div0,
104 #endif
105 PREFIX(sleep),
106 yield,
108 #if NUM_CORES > 1
109 create_thread,
110 thread_thaw,
111 thread_wait,
112 semaphore_init,
113 semaphore_wait,
114 semaphore_release,
115 #endif
117 #if NUM_CORES > 1
118 cpucache_flush,
119 cpucache_invalidate,
120 #endif
122 /* strings and memory */
123 strcpy,
124 strlen,
125 strcmp,
126 strcat,
127 memset,
128 memcpy,
129 memmove,
130 memcmp,
131 memchr,
132 strcasestr,
133 #if defined(DEBUG) || defined(SIMULATOR)
134 debugf,
135 #endif
136 #ifdef ROCKBOX_HAS_LOGF
137 logf,
138 #endif
140 (qsort_func)qsort,
141 &global_settings,
143 #ifdef RB_PROFILE
144 profile_thread,
145 profstop,
146 __cyg_profile_func_enter,
147 __cyg_profile_func_exit,
148 #endif
150 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
151 false, /* stop_encoder */
152 0, /* enc_codec_loaded */
153 enc_get_inputs,
154 enc_set_parameters,
155 enc_get_chunk,
156 enc_finish_chunk,
157 enc_get_pcm_data,
158 enc_unget_pcm_data,
160 /* file */
161 (open_func)PREFIX(open),
162 close,
163 (read_func)read,
164 PREFIX(lseek),
165 (write_func)write,
166 round_value_to_list32,
168 #endif
170 /* new stuff at the end, sort into place next time
171 the API gets incompatible */
174 void codec_get_full_path(char *path, const char *codec_root_fn)
176 snprintf(path, MAX_PATH-1, CODECS_DIR "/%s." CODEC_EXTENSION,
177 codec_root_fn);
180 static int codec_load_ram(int size, struct codec_api *api)
182 struct codec_header *hdr;
183 int status;
184 #ifndef SIMULATOR
185 hdr = (struct codec_header *)codecbuf;
187 if (size <= (signed)sizeof(struct codec_header)
188 || (hdr->magic != CODEC_MAGIC
189 #ifdef HAVE_RECORDING
190 && hdr->magic != CODEC_ENC_MAGIC
191 #endif
193 || hdr->target_id != TARGET_ID
194 || hdr->load_addr != codecbuf
195 || hdr->end_addr > codecbuf + CODEC_SIZE)
197 logf("codec header error");
198 return CODEC_ERROR;
201 codec_size = hdr->end_addr - codecbuf;
203 #else /* SIMULATOR */
204 void *pd;
206 hdr = sim_codec_load_ram(codecbuf, size, &pd);
208 if (pd == NULL)
209 return CODEC_ERROR;
211 if (hdr == NULL
212 || (hdr->magic != CODEC_MAGIC
213 #ifdef HAVE_RECORDING
214 && hdr->magic != CODEC_ENC_MAGIC
215 #endif
217 || hdr->target_id != TARGET_ID) {
218 sim_codec_close(pd);
219 return CODEC_ERROR;
222 codec_size = codecbuf - codecbuf;
224 #endif /* SIMULATOR */
225 if (hdr->api_version > CODEC_API_VERSION
226 || hdr->api_version < CODEC_MIN_API_VERSION) {
227 sim_codec_close(pd);
228 return CODEC_ERROR;
231 *(hdr->api) = api;
232 cpucache_invalidate();
233 status = hdr->entry_point();
235 sim_codec_close(pd);
237 return status;
240 int codec_load_buf(unsigned int hid, struct codec_api *api)
242 int rc;
243 rc = bufread(hid, CODEC_SIZE, codecbuf);
244 if (rc < 0) {
245 logf("error loading codec");
246 return CODEC_ERROR;
248 api->discard_codec();
249 return codec_load_ram(rc, api);
252 int codec_load_file(const char *plugin, struct codec_api *api)
254 char path[MAX_PATH];
255 int fd;
256 int rc;
258 codec_get_full_path(path, plugin);
260 fd = open(path, O_RDONLY);
261 if (fd < 0) {
262 logf("Codec load error:%d", fd);
263 splashf(HZ*2, "Couldn't load codec: %s", path);
264 return fd;
267 rc = read(fd, &codecbuf[0], CODEC_SIZE);
268 close(fd);
269 if (rc <= 0) {
270 logf("Codec read error");
271 return CODEC_ERROR;
274 return codec_load_ram((size_t)rc, api);