1 is the correct value here. Not that it makes any difference... :)
[kugel-rb.git] / apps / codecs.c
blobc0b4aadeb42246eb624b1cb847c4a8d1e4f94bd2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
21 #include <stdbool.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <atoi.h>
25 #include <timefuncs.h>
26 #include <ctype.h>
27 #include "debug.h"
28 #include "button.h"
29 #include "dir.h"
30 #include "file.h"
31 #include "kernel.h"
32 #include "sprintf.h"
33 #include "logf.h"
34 #include "screens.h"
35 #include "misc.h"
36 #include "mas.h"
37 #include "codecs.h"
38 #include "lang.h"
39 #include "keyboard.h"
40 #include "mpeg.h"
41 #include "buffer.h"
42 #include "mp3_playback.h"
43 #include "backlight.h"
44 #include "ata.h"
45 #include "talk.h"
46 #include "mp3data.h"
47 #include "powermgmt.h"
48 #include "system.h"
49 #include "sound.h"
50 #include "database.h"
51 #include "splash.h"
53 #ifdef SIMULATOR
54 #if CONFIG_CODEC == SWCODEC
55 unsigned char codecbuf[CODEC_SIZE];
56 #endif
57 void *sim_codec_load_ram(char* codecptr, int size,
58 void* ptr2, int bufwrap, void **pd);
59 void sim_codec_close(void *pd);
60 #else
61 #define sim_codec_close(x)
62 extern unsigned char codecbuf[];
63 #endif
65 extern void* plugin_get_audio_buffer(int *buffer_size);
67 struct codec_api ci_voice;
69 struct codec_api ci = {
71 0, /* filesize */
72 0, /* curpos */
73 NULL, /* id3 */
74 NULL, /* taginfo_ready */
75 false, /* stop_codec */
76 0, /* new_track */
77 0, /* seek_time */
78 NULL, /* get_codec_memory */
79 NULL, /* pcmbuf_insert */
80 NULL, /* pcmbuf_insert_split */
81 NULL, /* set_elapsed */
82 NULL, /* read_filebuf */
83 NULL, /* request_buffer */
84 NULL, /* advance_buffer */
85 NULL, /* advance_buffer_loc */
86 NULL, /* seek_buffer */
87 NULL, /* seek_complete */
88 NULL, /* mp3_get_filepos */
89 NULL, /* request_next_track */
90 NULL, /* discard_codec */
91 NULL, /* set_offset */
92 NULL, /* configure */
94 gui_syncsplash,
96 /* file */
97 (open_func)PREFIX(open),
98 close,
99 (read_func)read,
100 PREFIX(lseek),
101 (creat_func)PREFIX(creat),
102 (write_func)write,
103 PREFIX(remove),
104 PREFIX(rename),
105 PREFIX(ftruncate),
106 fdprintf,
107 read_line,
108 settings_parseline,
109 #ifndef SIMULATOR
110 ata_sleep,
111 #endif
113 /* dir */
114 PREFIX(opendir),
115 PREFIX(closedir),
116 PREFIX(readdir),
117 PREFIX(mkdir),
119 /* kernel/ system */
120 PREFIX(sleep),
121 yield,
122 &current_tick,
123 default_event_handler,
124 default_event_handler_ex,
125 create_thread,
126 remove_thread,
127 reset_poweroff_timer,
128 #ifndef SIMULATOR
129 system_memory_guard,
130 &cpu_frequency,
131 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
132 cpu_boost,
133 #endif
134 #endif
136 /* strings and memory */
137 snprintf,
138 strcpy,
139 strncpy,
140 strlen,
141 strrchr,
142 strcmp,
143 strcasecmp,
144 strncasecmp,
145 memset,
146 memcpy,
147 memmove,
148 _ctype_,
149 atoi,
150 strchr,
151 strcat,
152 memcmp,
153 strcasestr,
154 memchr,
156 /* sound */
157 sound_set,
158 #ifndef SIMULATOR
159 mp3_play_data,
160 mp3_play_pause,
161 mp3_play_stop,
162 mp3_is_playing,
163 #endif
165 /* playback control */
166 PREFIX(audio_play),
167 audio_stop,
168 audio_pause,
169 audio_resume,
170 audio_next,
171 audio_prev,
172 audio_ff_rewind,
173 audio_next_track,
174 playlist_amount,
175 audio_status,
176 audio_has_changed_track,
177 audio_current_track,
178 audio_flush_and_reload_tracks,
179 audio_get_file_pos,
181 /* misc */
182 srand,
183 rand,
184 (qsort_func)qsort,
185 kbd_input,
186 get_time,
187 set_time,
188 plugin_get_audio_buffer,
190 #if defined(DEBUG) || defined(SIMULATOR)
191 debugf,
192 #endif
193 #ifdef ROCKBOX_HAS_LOGF
194 logf,
195 #endif
196 &global_settings,
197 mp3info,
198 count_mp3_frames,
199 create_xing_header,
200 find_next_frame,
201 battery_level,
202 battery_level_safe,
204 #ifdef RB_PROFILE
205 profile_thread,
206 profstop,
207 profile_func_enter,
208 profile_func_exit,
209 #endif
211 /* new stuff at the end, sort into place next time
212 the API gets incompatible */
216 int codec_load_ram(char* codecptr, int size, void* ptr2, int bufwrap,
217 struct codec_api *api)
219 struct codec_header *hdr;
220 int status;
221 #ifndef SIMULATOR
222 int copy_n;
224 if ((char *)&codecbuf[0] != codecptr) {
225 /* zero out codec buffer to ensure a properly zeroed bss area */
226 memset(codecbuf, 0, CODEC_SIZE);
228 size = MIN(size, CODEC_SIZE);
229 copy_n = MIN(size, bufwrap);
230 memcpy(codecbuf, codecptr, copy_n);
231 if (size - copy_n > 0) {
232 memcpy(&codecbuf[copy_n], ptr2, size - copy_n);
234 api->discard_codec();
236 hdr = (struct codec_header *)codecbuf;
238 if (size <= (signed)sizeof(struct codec_header)
239 || hdr->magic != CODEC_MAGIC
240 || hdr->target_id != TARGET_ID
241 || hdr->load_addr != codecbuf
242 || hdr->end_addr > codecbuf + CODEC_SIZE)
244 logf("codec header error");
245 return CODEC_ERROR;
247 #else /* SIMULATOR */
248 void *pd;
250 hdr = sim_codec_load_ram(codecptr, size, ptr2, bufwrap, &pd);
251 api->discard_codec();
253 if (pd == NULL)
254 return CODEC_ERROR;
256 if (hdr == NULL
257 || hdr->magic != CODEC_MAGIC
258 || hdr->target_id != TARGET_ID) {
259 sim_codec_close(pd);
260 return CODEC_ERROR;
262 #endif /* SIMULATOR */
263 if (hdr->api_version > CODEC_API_VERSION
264 || hdr->api_version < CODEC_MIN_API_VERSION) {
265 sim_codec_close(pd);
266 return CODEC_ERROR;
269 invalidate_icache();
270 status = hdr->entry_point(api);
272 sim_codec_close(pd);
274 return status;
277 int codec_load_file(const char *plugin, struct codec_api *api)
279 char msgbuf[80];
280 int fd;
281 int rc;
283 /* zero out codec buffer to ensure a properly zeroed bss area */
284 memset(codecbuf, 0, CODEC_SIZE);
286 fd = open(plugin, O_RDONLY);
287 if (fd < 0) {
288 snprintf(msgbuf, sizeof(msgbuf)-1, "Couldn't load codec: %s", plugin);
289 logf("Codec load error:%d", fd);
290 gui_syncsplash(HZ*2, true, msgbuf);
291 return fd;
294 rc = read(fd, &codecbuf[0], CODEC_SIZE);
295 close(fd);
296 if (rc <= 0) {
297 logf("Codec read error");
298 return CODEC_ERROR;
301 return codec_load_ram(codecbuf, (size_t)rc, NULL, 0, api);