HAVE_ADJUSTABLE_CPU_FREQ isn't defined for simulators, so we don't have to check...
[Rockbox.git] / apps / codecs.c
blob95a07539b9b4460d0fee7d39c0c136798730a478
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 false, /* reload_codec */
77 0, /* seek_time */
78 NULL,
79 NULL,
80 NULL,
81 NULL,
82 NULL,
83 NULL,
84 NULL,
85 NULL,
86 NULL,
87 NULL,
88 NULL,
89 NULL,
90 NULL,
91 NULL,
93 gui_syncsplash,
95 /* file */
96 (open_func)PREFIX(open),
97 close,
98 (read_func)read,
99 PREFIX(lseek),
100 (creat_func)PREFIX(creat),
101 (write_func)write,
102 PREFIX(remove),
103 PREFIX(rename),
104 PREFIX(ftruncate),
105 fdprintf,
106 read_line,
107 settings_parseline,
108 #ifndef SIMULATOR
109 ata_sleep,
110 #endif
112 /* dir */
113 PREFIX(opendir),
114 PREFIX(closedir),
115 PREFIX(readdir),
116 PREFIX(mkdir),
118 /* kernel/ system */
119 PREFIX(sleep),
120 yield,
121 &current_tick,
122 default_event_handler,
123 default_event_handler_ex,
124 create_thread,
125 remove_thread,
126 reset_poweroff_timer,
127 #ifndef SIMULATOR
128 system_memory_guard,
129 &cpu_frequency,
130 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
131 cpu_boost,
132 #endif
133 #endif
135 /* strings and memory */
136 snprintf,
137 strcpy,
138 strncpy,
139 strlen,
140 strrchr,
141 strcmp,
142 strcasecmp,
143 strncasecmp,
144 memset,
145 memcpy,
146 memmove,
147 _ctype_,
148 atoi,
149 strchr,
150 strcat,
151 memcmp,
152 strcasestr,
153 memchr,
155 /* sound */
156 sound_set,
157 #ifndef SIMULATOR
158 mp3_play_data,
159 mp3_play_pause,
160 mp3_play_stop,
161 mp3_is_playing,
162 #endif
164 /* playback control */
165 PREFIX(audio_play),
166 audio_stop,
167 audio_pause,
168 audio_resume,
169 audio_next,
170 audio_prev,
171 audio_ff_rewind,
172 audio_next_track,
173 playlist_amount,
174 audio_status,
175 audio_has_changed_track,
176 audio_current_track,
177 audio_flush_and_reload_tracks,
178 audio_get_file_pos,
180 /* misc */
181 srand,
182 rand,
183 (qsort_func)qsort,
184 kbd_input,
185 get_time,
186 set_time,
187 plugin_get_audio_buffer,
189 #if defined(DEBUG) || defined(SIMULATOR)
190 debugf,
191 #endif
192 #ifdef ROCKBOX_HAS_LOGF
193 logf,
194 #endif
195 &global_settings,
196 mp3info,
197 count_mp3_frames,
198 create_xing_header,
199 find_next_frame,
200 battery_level,
201 battery_level_safe,
203 #ifdef RB_PROFILE
204 profile_thread,
205 profstop,
206 profile_func_enter,
207 profile_func_exit,
208 #endif
210 /* new stuff at the end, sort into place next time
211 the API gets incompatible */
215 int codec_load_ram(char* codecptr, int size, void* ptr2, int bufwrap,
216 struct codec_api *api)
218 struct codec_header *hdr;
219 int status;
220 #ifndef SIMULATOR
221 int copy_n;
223 if ((char *)&codecbuf[0] != codecptr) {
224 /* zero out codec buffer to ensure a properly zeroed bss area */
225 memset(codecbuf, 0, CODEC_SIZE);
227 size = MIN(size, CODEC_SIZE);
228 copy_n = MIN(size, bufwrap);
229 memcpy(codecbuf, codecptr, copy_n);
230 if (size - copy_n > 0) {
231 memcpy(&codecbuf[copy_n], ptr2, size - copy_n);
234 hdr = (struct codec_header *)codecbuf;
236 if (size <= (signed)sizeof(struct codec_header)
237 || hdr->magic != CODEC_MAGIC
238 || hdr->target_id != TARGET_ID
239 || hdr->load_addr != codecbuf
240 || hdr->end_addr > codecbuf + CODEC_SIZE)
242 logf("codec header error");
243 return CODEC_ERROR;
245 #else /* SIMULATOR */
246 void *pd;
248 hdr = sim_codec_load_ram(codecptr, size, ptr2, bufwrap, &pd);
249 if (pd == NULL)
250 return CODEC_ERROR;
252 if (hdr == NULL
253 || hdr->magic != CODEC_MAGIC
254 || hdr->target_id != TARGET_ID) {
255 sim_codec_close(pd);
256 return CODEC_ERROR;
258 #endif /* SIMULATOR */
259 if (hdr->api_version > CODEC_API_VERSION
260 || hdr->api_version < CODEC_MIN_API_VERSION) {
261 sim_codec_close(pd);
262 return CODEC_ERROR;
265 invalidate_icache();
266 status = hdr->entry_point(api);
268 sim_codec_close(pd);
270 return status;
273 int codec_load_file(const char *plugin, struct codec_api *api)
275 char msgbuf[80];
276 int fd;
277 int rc;
279 /* zero out codec buffer to ensure a properly zeroed bss area */
280 memset(codecbuf, 0, CODEC_SIZE);
282 fd = open(plugin, O_RDONLY);
283 if (fd < 0) {
284 snprintf(msgbuf, sizeof(msgbuf)-1, "Couldn't load codec: %s", plugin);
285 logf("Codec load error:%d", fd);
286 gui_syncsplash(HZ*2, true, msgbuf);
287 return fd;
290 rc = read(fd, &codecbuf[0], CODEC_SIZE);
291 close(fd);
292 if (rc <= 0) {
293 logf("Codec read error");
294 return CODEC_ERROR;
297 return codec_load_ram(codecbuf, (size_t)rc, NULL, 0, api);