RTL language enhancements by Tomers Shalev and I.
[kugel-rb.git] / apps / codecs.c
blob8976871abdf0d381ed28d9c203af52df5f8ec902
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 struct codec_api ci = {
78 0, /* filesize */
79 0, /* curpos */
80 NULL, /* id3 */
81 NULL, /* taginfo_ready */
82 false, /* stop_codec */
83 0, /* new_track */
84 0, /* seek_time */
85 NULL, /* struct dsp_config *dsp */
86 NULL, /* codec_get_buffer */
87 NULL, /* pcmbuf_insert */
88 NULL, /* set_elapsed */
89 NULL, /* read_filebuf */
90 NULL, /* request_buffer */
91 NULL, /* advance_buffer */
92 NULL, /* advance_buffer_loc */
93 NULL, /* seek_buffer */
94 NULL, /* seek_complete */
95 NULL, /* request_next_track */
96 NULL, /* discard_codec */
97 NULL, /* set_offset */
98 NULL, /* configure */
100 /* kernel/ system */
101 #ifdef CPU_ARM
102 __div0,
103 #endif
104 PREFIX(sleep),
105 yield,
107 #if NUM_CORES > 1
108 create_thread,
109 thread_thaw,
110 thread_wait,
111 semaphore_init,
112 semaphore_wait,
113 semaphore_release,
114 #endif
116 #if NUM_CORES > 1
117 cpucache_flush,
118 cpucache_invalidate,
119 #endif
121 /* strings and memory */
122 strcpy,
123 strlen,
124 strcmp,
125 strcat,
126 memset,
127 memcpy,
128 memmove,
129 memcmp,
130 memchr,
131 strcasestr,
132 #if defined(DEBUG) || defined(SIMULATOR)
133 debugf,
134 #endif
135 #ifdef ROCKBOX_HAS_LOGF
136 logf,
137 #endif
139 (qsort_func)qsort,
140 &global_settings,
142 #ifdef RB_PROFILE
143 profile_thread,
144 profstop,
145 __cyg_profile_func_enter,
146 __cyg_profile_func_exit,
147 #endif
149 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
150 false, /* stop_encoder */
151 0, /* enc_codec_loaded */
152 enc_get_inputs,
153 enc_set_parameters,
154 enc_get_chunk,
155 enc_finish_chunk,
156 enc_get_pcm_data,
157 enc_unget_pcm_data,
159 /* file */
160 (open_func)PREFIX(open),
161 close,
162 (read_func)read,
163 PREFIX(lseek),
164 (write_func)write,
165 round_value_to_list32,
167 #endif
169 /* new stuff at the end, sort into place next time
170 the API gets incompatible */
173 void codec_get_full_path(char *path, const char *codec_root_fn)
175 snprintf(path, MAX_PATH-1, CODECS_DIR "/%s." CODEC_EXTENSION,
176 codec_root_fn);
179 static int codec_load_ram(int size, struct codec_api *api)
181 struct codec_header *hdr;
182 int status;
183 #ifndef SIMULATOR
184 hdr = (struct codec_header *)codecbuf;
186 if (size <= (signed)sizeof(struct codec_header)
187 || (hdr->magic != CODEC_MAGIC
188 #ifdef HAVE_RECORDING
189 && hdr->magic != CODEC_ENC_MAGIC
190 #endif
192 || hdr->target_id != TARGET_ID
193 || hdr->load_addr != codecbuf
194 || hdr->end_addr > codecbuf + CODEC_SIZE)
196 logf("codec header error");
197 return CODEC_ERROR;
200 codec_size = hdr->end_addr - codecbuf;
202 #else /* SIMULATOR */
203 void *pd;
205 hdr = sim_codec_load_ram(codecbuf, size, &pd);
207 if (pd == NULL)
208 return CODEC_ERROR;
210 if (hdr == NULL
211 || (hdr->magic != CODEC_MAGIC
212 #ifdef HAVE_RECORDING
213 && hdr->magic != CODEC_ENC_MAGIC
214 #endif
216 || hdr->target_id != TARGET_ID) {
217 sim_codec_close(pd);
218 return CODEC_ERROR;
221 codec_size = codecbuf - codecbuf;
223 #endif /* SIMULATOR */
224 if (hdr->api_version > CODEC_API_VERSION
225 || hdr->api_version < CODEC_MIN_API_VERSION) {
226 sim_codec_close(pd);
227 return CODEC_ERROR;
230 *(hdr->api) = api;
231 cpucache_invalidate();
232 status = hdr->entry_point();
234 sim_codec_close(pd);
236 return status;
239 int codec_load_buf(unsigned int hid, struct codec_api *api)
241 int rc;
242 rc = bufread(hid, CODEC_SIZE, codecbuf);
243 if (rc < 0) {
244 logf("error loading codec");
245 return CODEC_ERROR;
247 api->discard_codec();
248 return codec_load_ram(rc, api);
251 int codec_load_file(const char *plugin, struct codec_api *api)
253 char path[MAX_PATH];
254 int fd;
255 int rc;
257 codec_get_full_path(path, plugin);
259 fd = open(path, O_RDONLY);
260 if (fd < 0) {
261 logf("Codec load error:%d", fd);
262 splashf(HZ*2, "Couldn't load codec: %s", path);
263 return fd;
266 rc = read(fd, &codecbuf[0], CODEC_SIZE);
267 close(fd);
268 if (rc <= 0) {
269 logf("Codec read error");
270 return CODEC_ERROR;
273 return codec_load_ram((size_t)rc, api);