Greek language update by Ioannis Koutoulakis - needs an increase of the language...
[Rockbox.git] / apps / codecs.c
blobf33957eba2470e55ebd99cecc98337e274718d01
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 "database.h"
52 #include "splash.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, /* pcmbuf_insert_split */
82 NULL, /* set_elapsed */
83 NULL, /* read_filebuf */
84 NULL, /* request_buffer */
85 NULL, /* advance_buffer */
86 NULL, /* advance_buffer_loc */
87 NULL, /* seek_buffer */
88 NULL, /* seek_complete */
89 NULL, /* mp3_get_filepos */
90 NULL, /* request_next_track */
91 NULL, /* discard_codec */
92 NULL, /* set_offset */
93 NULL, /* configure */
95 gui_syncsplash,
97 /* file */
98 (open_func)PREFIX(open),
99 close,
100 (read_func)read,
101 PREFIX(lseek),
102 (creat_func)PREFIX(creat),
103 (write_func)write,
104 PREFIX(remove),
105 PREFIX(rename),
106 PREFIX(ftruncate),
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_on_core,
127 remove_thread,
128 reset_poweroff_timer,
129 #ifndef SIMULATOR
130 system_memory_guard,
131 &cpu_frequency,
132 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
133 cpu_boost,
134 #endif
135 #endif
137 /* strings and memory */
138 snprintf,
139 strcpy,
140 strncpy,
141 strlen,
142 strrchr,
143 strcmp,
144 strcasecmp,
145 strncasecmp,
146 memset,
147 memcpy,
148 memmove,
149 _ctype_,
150 atoi,
151 strchr,
152 strcat,
153 memcmp,
154 strcasestr,
155 memchr,
157 /* sound */
158 sound_set,
159 #ifndef SIMULATOR
160 mp3_play_data,
161 mp3_play_pause,
162 mp3_play_stop,
163 mp3_is_playing,
164 #endif
166 /* playback control */
167 PREFIX(audio_play),
168 audio_stop,
169 audio_pause,
170 audio_resume,
171 audio_next,
172 audio_prev,
173 audio_ff_rewind,
174 audio_next_track,
175 playlist_amount,
176 audio_status,
177 audio_has_changed_track,
178 audio_current_track,
179 audio_flush_and_reload_tracks,
180 audio_get_file_pos,
182 /* misc */
183 srand,
184 rand,
185 (qsort_func)qsort,
186 kbd_input,
187 get_time,
188 set_time,
189 plugin_get_audio_buffer,
191 #if defined(DEBUG) || defined(SIMULATOR)
192 debugf,
193 #endif
194 #ifdef ROCKBOX_HAS_LOGF
195 logf,
196 #endif
197 &global_settings,
198 mp3info,
199 count_mp3_frames,
200 create_xing_header,
201 find_next_frame,
202 battery_level,
203 battery_level_safe,
205 #ifdef RB_PROFILE
206 profile_thread,
207 profstop,
208 profile_func_enter,
209 profile_func_exit,
210 #endif
212 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
213 false,
214 enc_get_inputs,
215 enc_set_parameters,
216 enc_alloc_chunk,
217 enc_free_chunk,
218 enc_wavbuf_near_empty,
219 enc_get_wav_data,
220 &enc_set_header_callback,
221 #endif
223 /* new stuff at the end, sort into place next time
224 the API gets incompatible */
228 void codec_get_full_path(char *path, const char *codec_fn)
230 /* Create full codec path */
231 snprintf(path, MAX_PATH-1, CODECS_DIR "/%s", codec_fn);
234 int codec_load_ram(char* codecptr, int size, void* ptr2, int bufwrap,
235 struct codec_api *api)
237 struct codec_header *hdr;
238 int status;
239 #ifndef SIMULATOR
240 int copy_n;
242 if ((char *)&codecbuf[0] != codecptr) {
243 /* zero out codec buffer to ensure a properly zeroed bss area */
244 memset(codecbuf, 0, CODEC_SIZE);
246 size = MIN(size, CODEC_SIZE);
247 copy_n = MIN(size, bufwrap);
248 memcpy(codecbuf, codecptr, copy_n);
249 if (size - copy_n > 0) {
250 memcpy(&codecbuf[copy_n], ptr2, size - copy_n);
252 api->discard_codec();
254 hdr = (struct codec_header *)codecbuf;
256 if (size <= (signed)sizeof(struct codec_header)
257 || hdr->magic != CODEC_MAGIC
258 || hdr->target_id != TARGET_ID
259 || hdr->load_addr != codecbuf
260 || hdr->end_addr > codecbuf + CODEC_SIZE)
262 logf("codec header error");
263 return CODEC_ERROR;
265 #else /* SIMULATOR */
266 void *pd;
268 hdr = sim_codec_load_ram(codecptr, size, ptr2, bufwrap, &pd);
269 api->discard_codec();
271 if (pd == NULL)
272 return CODEC_ERROR;
274 if (hdr == NULL
275 || hdr->magic != CODEC_MAGIC
276 || hdr->target_id != TARGET_ID) {
277 sim_codec_close(pd);
278 return CODEC_ERROR;
280 #endif /* SIMULATOR */
281 if (hdr->api_version > CODEC_API_VERSION
282 || hdr->api_version < CODEC_MIN_API_VERSION) {
283 sim_codec_close(pd);
284 return CODEC_ERROR;
287 invalidate_icache();
288 status = hdr->entry_point(api);
290 sim_codec_close(pd);
292 return status;
295 int codec_load_file(const char *plugin, struct codec_api *api)
297 char msgbuf[80];
298 char path[MAX_PATH];
299 int fd;
300 int rc;
302 codec_get_full_path(path, plugin);
304 /* zero out codec buffer to ensure a properly zeroed bss area */
305 memset(codecbuf, 0, CODEC_SIZE);
307 fd = open(path, O_RDONLY);
308 if (fd < 0) {
309 snprintf(msgbuf, sizeof(msgbuf)-1, "Couldn't load codec: %s", path);
310 logf("Codec load error:%d", fd);
311 gui_syncsplash(HZ*2, true, msgbuf);
312 return fd;
315 rc = read(fd, &codecbuf[0], CODEC_SIZE);
316 close(fd);
317 if (rc <= 0) {
318 logf("Codec read error");
319 return CODEC_ERROR;
322 return codec_load_ram(codecbuf, (size_t)rc, NULL, 0, api);