enable downloading of the manual. It will get saved to the players root folder for...
[Rockbox.git] / apps / codecs.c
blobba9d7392b188ff2f18332b76c859fd5802b43743
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(size_t *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 /* kernel/ system */
95 PREFIX(sleep),
96 yield,
98 /* strings and memory */
99 strcpy,
100 strncpy,
101 strlen,
102 strcmp,
103 strcat,
104 memset,
105 memcpy,
106 memmove,
107 memcmp,
108 memchr,
110 #if defined(DEBUG) || defined(SIMULATOR)
111 debugf,
112 #endif
113 #ifdef ROCKBOX_HAS_LOGF
114 logf,
115 #endif
117 (qsort_func)qsort,
118 &global_settings,
120 #ifdef RB_PROFILE
121 profile_thread,
122 profstop,
123 profile_func_enter,
124 profile_func_exit,
125 #endif
127 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
128 false, /* stop_encoder */
129 0, /* enc_codec_loaded */
130 enc_get_inputs,
131 enc_set_parameters,
132 enc_get_chunk,
133 enc_finish_chunk,
134 enc_pcm_buf_near_empty,
135 enc_get_pcm_data,
136 enc_unget_pcm_data,
138 /* file */
139 (open_func)PREFIX(open),
140 close,
141 (read_func)read,
142 PREFIX(lseek),
143 (write_func)write,
145 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
146 #ifdef CPU_BOOST_LOGGING
147 cpu_boost_,
148 #else
149 cpu_boost,
150 #endif
151 #endif
153 round_value_to_list32,
155 #endif
157 /* new stuff at the end, sort into place next time
158 the API gets incompatible */
162 void codec_get_full_path(char *path, const char *codec_root_fn)
164 snprintf(path, MAX_PATH-1, CODECS_DIR "/%s." CODEC_EXTENSION,
165 codec_root_fn);
168 int codec_load_ram(char* codecptr, int size, void* ptr2, int bufwrap,
169 struct codec_api *api)
171 struct codec_header *hdr;
172 int status;
173 #ifndef SIMULATOR
174 int copy_n;
176 if ((char *)&codecbuf[0] != codecptr) {
177 size = MIN(size, CODEC_SIZE);
178 copy_n = MIN(size, bufwrap);
179 memcpy(codecbuf, codecptr, copy_n);
180 if (size - copy_n > 0) {
181 memcpy(&codecbuf[copy_n], ptr2, size - copy_n);
183 api->discard_codec();
185 hdr = (struct codec_header *)codecbuf;
187 if (size <= (signed)sizeof(struct codec_header)
188 || (hdr->magic != CODEC_MAGIC
189 #ifdef HAVE_RECORDING
190 && hdr->magic != CODEC_ENC_MAGIC
191 #endif
193 || hdr->target_id != TARGET_ID
194 || hdr->load_addr != codecbuf
195 || hdr->end_addr > codecbuf + CODEC_SIZE)
197 logf("codec header error");
198 return CODEC_ERROR;
200 #else /* SIMULATOR */
201 void *pd;
203 hdr = sim_codec_load_ram(codecptr, size, ptr2, bufwrap, &pd);
204 api->discard_codec();
206 if (pd == NULL)
207 return CODEC_ERROR;
209 if (hdr == NULL
210 || hdr->magic != CODEC_MAGIC
211 || hdr->target_id != TARGET_ID) {
212 sim_codec_close(pd);
213 return CODEC_ERROR;
215 #endif /* SIMULATOR */
216 if (hdr->api_version > CODEC_API_VERSION
217 || hdr->api_version < CODEC_MIN_API_VERSION) {
218 sim_codec_close(pd);
219 return CODEC_ERROR;
222 invalidate_icache();
223 status = hdr->entry_point(api);
225 sim_codec_close(pd);
227 return status;
230 int codec_load_file(const char *plugin, struct codec_api *api)
232 char msgbuf[80];
233 char path[MAX_PATH];
234 int fd;
235 int rc;
237 codec_get_full_path(path, plugin);
239 fd = open(path, O_RDONLY);
240 if (fd < 0) {
241 snprintf(msgbuf, sizeof(msgbuf)-1, "Couldn't load codec: %s", path);
242 logf("Codec load error:%d", fd);
243 gui_syncsplash(HZ*2, msgbuf);
244 return fd;
247 rc = read(fd, &codecbuf[0], CODEC_SIZE);
248 close(fd);
249 if (rc <= 0) {
250 logf("Codec read error");
251 return CODEC_ERROR;
254 return codec_load_ram(codecbuf, (size_t)rc, NULL, 0, api);