Make open() posix compliant api-wise. A few calls (those with O_CREAT) need the addit...
[kugel-rb.git] / apps / codecs.c
blobb20aa7f9696584ba713a63178b8983886c21fb4a
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 "buffering.h"
42 #include "mp3_playback.h"
43 #include "backlight.h"
44 #include "storage.h"
45 #include "talk.h"
46 #include "mp3data.h"
47 #include "powermgmt.h"
48 #include "system.h"
49 #include "sound.h"
50 #include "splash.h"
51 #include "general.h"
53 #define LOGF_ENABLE
54 #include "logf.h"
56 #ifdef SIMULATOR
57 #define PREFIX(_x_) sim_ ## _x_
58 #else
59 #define PREFIX
60 #endif
62 #ifdef SIMULATOR
63 #if CONFIG_CODEC == SWCODEC
64 unsigned char codecbuf[CODEC_SIZE];
65 #endif
66 void *sim_codec_load_ram(char* codecptr, int size, void **pd);
67 void sim_codec_close(void *pd);
68 #else
69 #define sim_codec_close(x)
70 #endif
72 size_t codec_size;
74 extern void* plugin_get_audio_buffer(size_t *buffer_size);
76 #undef open
77 static int open(const char* pathname, int flags, ...)
79 #ifdef SIMULATOR
80 int fd;
81 if (flags & O_CREAT)
83 va_list ap;
84 va_start(ap, flags);
85 fd = sim_open(pathname, flags, va_arg(ap, mode_t));
86 va_end(ap);
88 else
89 fd = sim_open(pathname, flags);
91 return fd;
92 #else
93 return file_open(pathname, flags);
94 #endif
96 struct codec_api ci = {
98 0, /* filesize */
99 0, /* curpos */
100 NULL, /* id3 */
101 NULL, /* taginfo_ready */
102 false, /* stop_codec */
103 0, /* new_track */
104 0, /* seek_time */
105 NULL, /* struct dsp_config *dsp */
106 NULL, /* codec_get_buffer */
107 NULL, /* pcmbuf_insert */
108 NULL, /* set_elapsed */
109 NULL, /* read_filebuf */
110 NULL, /* request_buffer */
111 NULL, /* advance_buffer */
112 NULL, /* advance_buffer_loc */
113 NULL, /* seek_buffer */
114 NULL, /* seek_complete */
115 NULL, /* request_next_track */
116 NULL, /* discard_codec */
117 NULL, /* set_offset */
118 NULL, /* configure */
120 /* kernel/ system */
121 #ifdef CPU_ARM
122 __div0,
123 #endif
124 PREFIX(sleep),
125 yield,
127 #if NUM_CORES > 1
128 create_thread,
129 thread_thaw,
130 thread_wait,
131 semaphore_init,
132 semaphore_wait,
133 semaphore_release,
134 #endif
136 #if NUM_CORES > 1
137 cpucache_flush,
138 cpucache_invalidate,
139 #endif
141 /* strings and memory */
142 strcpy,
143 strlen,
144 strcmp,
145 strcat,
146 memset,
147 memcpy,
148 memmove,
149 memcmp,
150 memchr,
151 strcasestr,
152 #if defined(DEBUG) || defined(SIMULATOR)
153 debugf,
154 #endif
155 #ifdef ROCKBOX_HAS_LOGF
156 logf,
157 #endif
159 (qsort_func)qsort,
160 &global_settings,
162 #ifdef RB_PROFILE
163 profile_thread,
164 profstop,
165 __cyg_profile_func_enter,
166 __cyg_profile_func_exit,
167 #endif
169 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
170 false, /* stop_encoder */
171 0, /* enc_codec_loaded */
172 enc_get_inputs,
173 enc_set_parameters,
174 enc_get_chunk,
175 enc_finish_chunk,
176 enc_get_pcm_data,
177 enc_unget_pcm_data,
179 /* file */
180 (open_func)PREFIX(open),
181 close,
182 (read_func)read,
183 PREFIX(lseek),
184 (write_func)write,
185 round_value_to_list32,
187 #endif
189 /* new stuff at the end, sort into place next time
190 the API gets incompatible */
193 void codec_get_full_path(char *path, const char *codec_root_fn)
195 snprintf(path, MAX_PATH-1, CODECS_DIR "/%s." CODEC_EXTENSION,
196 codec_root_fn);
199 static int codec_load_ram(int size, struct codec_api *api)
201 struct codec_header *hdr;
202 int status;
203 #ifndef SIMULATOR
204 hdr = (struct codec_header *)codecbuf;
206 if (size <= (signed)sizeof(struct codec_header)
207 || (hdr->magic != CODEC_MAGIC
208 #ifdef HAVE_RECORDING
209 && hdr->magic != CODEC_ENC_MAGIC
210 #endif
212 || hdr->target_id != TARGET_ID
213 || hdr->load_addr != codecbuf
214 || hdr->end_addr > codecbuf + CODEC_SIZE)
216 logf("codec header error");
217 return CODEC_ERROR;
220 codec_size = hdr->end_addr - codecbuf;
222 #else /* SIMULATOR */
223 void *pd;
225 hdr = sim_codec_load_ram(codecbuf, size, &pd);
227 if (pd == NULL)
228 return CODEC_ERROR;
230 if (hdr == NULL
231 || (hdr->magic != CODEC_MAGIC
232 #ifdef HAVE_RECORDING
233 && hdr->magic != CODEC_ENC_MAGIC
234 #endif
236 || hdr->target_id != TARGET_ID) {
237 sim_codec_close(pd);
238 return CODEC_ERROR;
241 codec_size = codecbuf - codecbuf;
243 #endif /* SIMULATOR */
244 if (hdr->api_version > CODEC_API_VERSION
245 || hdr->api_version < CODEC_MIN_API_VERSION) {
246 sim_codec_close(pd);
247 return CODEC_ERROR;
250 *(hdr->api) = api;
251 cpucache_invalidate();
252 status = hdr->entry_point();
254 sim_codec_close(pd);
256 return status;
259 int codec_load_buf(unsigned int hid, struct codec_api *api)
261 int rc;
262 rc = bufread(hid, CODEC_SIZE, codecbuf);
263 if (rc < 0) {
264 logf("error loading codec");
265 return CODEC_ERROR;
267 api->discard_codec();
268 return codec_load_ram(rc, api);
271 int codec_load_file(const char *plugin, struct codec_api *api)
273 char path[MAX_PATH];
274 int fd;
275 int rc;
277 codec_get_full_path(path, plugin);
279 fd = open(path, O_RDONLY);
280 if (fd < 0) {
281 logf("Codec load error:%d", fd);
282 splashf(HZ*2, "Couldn't load codec: %s", path);
283 return fd;
286 rc = read(fd, &codecbuf[0], CODEC_SIZE);
287 close(fd);
288 if (rc <= 0) {
289 logf("Codec read error");
290 return CODEC_ERROR;
293 return codec_load_ram((size_t)rc, api);