Add a get_offset API call to make codec_advance_buffer_loc_callback work.
[Rockbox.git] / apps / codecs.c
blobe41e04772070c5f2548b15b6633da15aa37987b0
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 "screens.h"
34 #include "misc.h"
35 #include "mas.h"
36 #include "codecs.h"
37 #include "lang.h"
38 #include "keyboard.h"
39 #include "mpeg.h"
40 #include "buffer.h"
41 #include "mp3_playback.h"
42 #include "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 "splash.h"
51 #include "general.h"
53 #define LOGF_ENABLE
54 #include "logf.h"
56 #ifdef SIMULATOR
57 #if CONFIG_CODEC == SWCODEC
58 unsigned char codecbuf[CODEC_SIZE];
59 #endif
60 void *sim_codec_load_ram(char* codecptr, int size,
61 void* ptr2, int bufwrap, void **pd);
62 void sim_codec_close(void *pd);
63 #else
64 #define sim_codec_close(x)
65 extern unsigned char codecbuf[];
66 #endif
68 extern void* plugin_get_audio_buffer(size_t *buffer_size);
70 struct codec_api ci_voice;
72 struct codec_api ci = {
74 0, /* filesize */
75 0, /* curpos */
76 NULL, /* id3 */
77 NULL, /* taginfo_ready */
78 false, /* stop_codec */
79 0, /* new_track */
80 0, /* seek_time */
81 NULL, /* get_codec_memory */
82 NULL, /* pcmbuf_insert */
83 NULL, /* set_elapsed */
84 NULL, /* read_filebuf */
85 NULL, /* request_buffer */
86 NULL, /* advance_buffer */
87 NULL, /* advance_buffer_loc */
88 NULL, /* seek_buffer */
89 NULL, /* seek_complete */
90 NULL, /* mp3_get_filepos */
91 NULL, /* request_next_track */
92 NULL, /* discard_codec */
93 NULL, /* set_offset */
94 NULL, /* configure */
96 /* kernel/ system */
97 PREFIX(sleep),
98 yield,
100 /* strings and memory */
101 strcpy,
102 strncpy,
103 strlen,
104 strcmp,
105 strcat,
106 memset,
107 memcpy,
108 memmove,
109 memcmp,
110 memchr,
112 #if defined(DEBUG) || defined(SIMULATOR)
113 debugf,
114 #endif
115 #ifdef ROCKBOX_HAS_LOGF
116 logf,
117 #endif
119 (qsort_func)qsort,
120 &global_settings,
122 #ifdef RB_PROFILE
123 profile_thread,
124 profstop,
125 profile_func_enter,
126 profile_func_exit,
127 #endif
129 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
130 false, /* stop_encoder */
131 0, /* enc_codec_loaded */
132 enc_get_inputs,
133 enc_set_parameters,
134 enc_get_chunk,
135 enc_finish_chunk,
136 enc_pcm_buf_near_empty,
137 enc_get_pcm_data,
138 enc_unget_pcm_data,
140 /* file */
141 (open_func)PREFIX(open),
142 close,
143 (read_func)read,
144 PREFIX(lseek),
145 (write_func)write,
147 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
148 #ifdef CPU_BOOST_LOGGING
149 cpu_boost_,
150 #else
151 cpu_boost,
152 #endif
153 #endif
155 round_value_to_list32,
157 #endif
159 /* new stuff at the end, sort into place next time
160 the API gets incompatible */
162 #ifdef CACHE_FUNCTIONS_AS_CALL
163 flush_icache,
164 invalidate_icache,
165 #endif
168 void codec_get_full_path(char *path, const char *codec_root_fn)
170 snprintf(path, MAX_PATH-1, CODECS_DIR "/%s." CODEC_EXTENSION,
171 codec_root_fn);
174 int codec_load_ram(char* codecptr, int size, void* ptr2, int bufwrap,
175 struct codec_api *api)
177 struct codec_header *hdr;
178 int status;
179 #ifndef SIMULATOR
180 int copy_n;
182 if ((char *)&codecbuf[0] != codecptr) {
183 size = MIN(size, CODEC_SIZE);
184 copy_n = MIN(size, bufwrap);
185 memcpy(codecbuf, codecptr, copy_n);
186 if (size - copy_n > 0) {
187 memcpy(&codecbuf[copy_n], ptr2, size - copy_n);
189 api->discard_codec();
191 hdr = (struct codec_header *)codecbuf;
193 if (size <= (signed)sizeof(struct codec_header)
194 || (hdr->magic != CODEC_MAGIC
195 #ifdef HAVE_RECORDING
196 && hdr->magic != CODEC_ENC_MAGIC
197 #endif
199 || hdr->target_id != TARGET_ID
200 || hdr->load_addr != codecbuf
201 || hdr->end_addr > codecbuf + CODEC_SIZE)
203 logf("codec header error");
204 return CODEC_ERROR;
206 #else /* SIMULATOR */
207 void *pd;
209 hdr = sim_codec_load_ram(codecptr, size, ptr2, bufwrap, &pd);
210 api->discard_codec();
212 if (pd == NULL)
213 return CODEC_ERROR;
215 if (hdr == NULL
216 || hdr->magic != CODEC_MAGIC
217 || hdr->target_id != TARGET_ID) {
218 sim_codec_close(pd);
219 return CODEC_ERROR;
221 #endif /* SIMULATOR */
222 if (hdr->api_version > CODEC_API_VERSION
223 || hdr->api_version < CODEC_MIN_API_VERSION) {
224 sim_codec_close(pd);
225 return CODEC_ERROR;
228 invalidate_icache();
229 status = hdr->entry_point(api);
231 sim_codec_close(pd);
233 return status;
236 int codec_load_file(const char *plugin, struct codec_api *api)
238 char msgbuf[80];
239 char path[MAX_PATH];
240 int fd;
241 int rc;
243 codec_get_full_path(path, plugin);
245 fd = open(path, O_RDONLY);
246 if (fd < 0) {
247 snprintf(msgbuf, sizeof(msgbuf)-1, "Couldn't load codec: %s", path);
248 logf("Codec load error:%d", fd);
249 gui_syncsplash(HZ*2, msgbuf);
250 return fd;
253 rc = read(fd, &codecbuf[0], CODEC_SIZE);
254 close(fd);
255 if (rc <= 0) {
256 logf("Codec read error");
257 return CODEC_ERROR;
260 return codec_load_ram(codecbuf, (size_t)rc, NULL, 0, api);