Revert change 13001, since it causes the metadata to be re-read for partially buffere...
[kugel-rb.git] / apps / codecs.c
blob1de83588c19d8ede6d3ad80f543846e97f0ba5dc
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 "playback.h"
44 #include "backlight.h"
45 #include "ata.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 #ifdef SIMULATOR
55 #if CONFIG_CODEC == SWCODEC
56 unsigned char codecbuf[CODEC_SIZE];
57 #endif
58 void *sim_codec_load_ram(char* codecptr, int size,
59 void* ptr2, int bufwrap, void **pd);
60 void sim_codec_close(void *pd);
61 #else
62 #define sim_codec_close(x)
63 extern unsigned char codecbuf[];
64 #endif
66 extern void* plugin_get_audio_buffer(int *buffer_size);
68 struct codec_api ci_voice;
70 struct codec_api ci = {
72 0, /* filesize */
73 0, /* curpos */
74 NULL, /* id3 */
75 NULL, /* taginfo_ready */
76 false, /* stop_codec */
77 0, /* new_track */
78 0, /* seek_time */
79 NULL, /* get_codec_memory */
80 NULL, /* pcmbuf_insert */
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 PREFIX(fsync),
107 fdprintf,
108 read_line,
109 settings_parseline,
110 #ifndef SIMULATOR
111 ata_sleep,
112 #endif
114 /* dir */
115 PREFIX(opendir),
116 PREFIX(closedir),
117 PREFIX(readdir),
118 PREFIX(mkdir),
120 /* kernel/ system */
121 PREFIX(sleep),
122 yield,
123 &current_tick,
124 default_event_handler,
125 default_event_handler_ex,
126 create_thread,
127 remove_thread,
128 reset_poweroff_timer,
129 #ifndef SIMULATOR
130 system_memory_guard,
131 &cpu_frequency,
132 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
133 #ifdef CPU_BOOST_LOGGING
134 cpu_boost_,
135 #else
136 cpu_boost,
137 #endif
138 #endif
139 #endif
141 /* strings and memory */
142 snprintf,
143 strcpy,
144 strncpy,
145 strlen,
146 strrchr,
147 strcmp,
148 strcasecmp,
149 strncasecmp,
150 memset,
151 memcpy,
152 memmove,
153 _ctype_,
154 atoi,
155 strchr,
156 strcat,
157 memcmp,
158 strcasestr,
159 memchr,
161 /* sound */
162 sound_set,
163 #ifndef SIMULATOR
164 mp3_play_data,
165 mp3_play_pause,
166 mp3_play_stop,
167 mp3_is_playing,
168 #endif
170 /* playback control */
171 PREFIX(audio_play),
172 audio_stop,
173 audio_pause,
174 audio_resume,
175 audio_next,
176 audio_prev,
177 audio_ff_rewind,
178 audio_next_track,
179 playlist_amount,
180 audio_status,
181 audio_has_changed_track,
182 audio_current_track,
183 audio_flush_and_reload_tracks,
184 audio_get_file_pos,
186 /* misc */
187 srand,
188 rand,
189 (qsort_func)qsort,
190 kbd_input,
191 get_time,
192 set_time,
193 plugin_get_audio_buffer,
194 round_value_to_list32,
196 #if defined(DEBUG) || defined(SIMULATOR)
197 debugf,
198 #endif
199 #ifdef ROCKBOX_HAS_LOGF
200 logf,
201 #endif
202 &global_settings,
203 mp3info,
204 count_mp3_frames,
205 create_xing_header,
206 find_next_frame,
207 battery_level,
208 battery_level_safe,
210 #ifdef RB_PROFILE
211 profile_thread,
212 profstop,
213 profile_func_enter,
214 profile_func_exit,
215 #endif
217 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
218 false, /* stop_encoder */
219 0, /* enc_codec_loaded */
220 enc_get_inputs,
221 enc_set_parameters,
222 enc_get_chunk,
223 enc_finish_chunk,
224 enc_pcm_buf_near_empty,
225 enc_get_pcm_data,
226 enc_unget_pcm_data,
227 #endif
229 /* new stuff at the end, sort into place next time
230 the API gets incompatible */
234 void codec_get_full_path(char *path, const char *codec_root_fn)
236 snprintf(path, MAX_PATH-1, CODECS_DIR "/%s." CODEC_EXTENSION,
237 codec_root_fn);
240 int codec_load_ram(char* codecptr, int size, void* ptr2, int bufwrap,
241 struct codec_api *api)
243 struct codec_header *hdr;
244 int status;
245 #ifndef SIMULATOR
246 int copy_n;
248 if ((char *)&codecbuf[0] != codecptr) {
249 size = MIN(size, CODEC_SIZE);
250 copy_n = MIN(size, bufwrap);
251 memcpy(codecbuf, codecptr, copy_n);
252 if (size - copy_n > 0) {
253 memcpy(&codecbuf[copy_n], ptr2, size - copy_n);
255 api->discard_codec();
257 hdr = (struct codec_header *)codecbuf;
259 if (size <= (signed)sizeof(struct codec_header)
260 || (hdr->magic != CODEC_MAGIC
261 #ifdef HAVE_RECORDING
262 && hdr->magic != CODEC_ENC_MAGIC
263 #endif
265 || hdr->target_id != TARGET_ID
266 || hdr->load_addr != codecbuf
267 || hdr->end_addr > codecbuf + CODEC_SIZE)
269 logf("codec header error");
270 return CODEC_ERROR;
272 #else /* SIMULATOR */
273 void *pd;
275 hdr = sim_codec_load_ram(codecptr, size, ptr2, bufwrap, &pd);
276 api->discard_codec();
278 if (pd == NULL)
279 return CODEC_ERROR;
281 if (hdr == NULL
282 || hdr->magic != CODEC_MAGIC
283 || hdr->target_id != TARGET_ID) {
284 sim_codec_close(pd);
285 return CODEC_ERROR;
287 #endif /* SIMULATOR */
288 if (hdr->api_version > CODEC_API_VERSION
289 || hdr->api_version < CODEC_MIN_API_VERSION) {
290 sim_codec_close(pd);
291 return CODEC_ERROR;
294 invalidate_icache();
295 status = hdr->entry_point(api);
297 sim_codec_close(pd);
299 return status;
302 int codec_load_file(const char *plugin, struct codec_api *api)
304 char msgbuf[80];
305 char path[MAX_PATH];
306 int fd;
307 int rc;
309 codec_get_full_path(path, plugin);
311 fd = open(path, O_RDONLY);
312 if (fd < 0) {
313 snprintf(msgbuf, sizeof(msgbuf)-1, "Couldn't load codec: %s", path);
314 logf("Codec load error:%d", fd);
315 gui_syncsplash(HZ*2, msgbuf);
316 return fd;
319 rc = read(fd, &codecbuf[0], CODEC_SIZE);
320 close(fd);
321 if (rc <= 0) {
322 logf("Codec read error");
323 return CODEC_ERROR;
326 return codec_load_ram(codecbuf, (size_t)rc, NULL, 0, api);