When looking for album art in /.rockbox/albumart/, use album artist rather than artis...
[kugel-rb.git] / apps / codecs.c
blob062487d6c80f744e639f790872d90287c6b4d3f8
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 "buffering.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 #define LOGF_ENABLE
55 #include "logf.h"
57 #ifdef SIMULATOR
58 #if CONFIG_CODEC == SWCODEC
59 unsigned char codecbuf[CODEC_SIZE];
60 #endif
61 void *sim_codec_load_ram(char* codecptr, int size, 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 = {
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, /* struct dsp_config *dsp */
80 NULL, /* get_codec_memory */
81 NULL, /* pcmbuf_insert */
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, /* request_next_track */
90 NULL, /* discard_codec */
91 NULL, /* set_offset */
92 NULL, /* configure */
94 /* kernel/ system */
95 PREFIX(sleep),
96 yield,
98 #if NUM_CORES > 1
99 create_thread,
100 thread_thaw,
101 thread_wait,
102 semaphore_init,
103 semaphore_wait,
104 semaphore_release,
105 event_init,
106 event_wait,
107 event_set_state,
108 #endif
110 #ifdef CACHE_FUNCTIONS_AS_CALL
111 flush_icache,
112 invalidate_icache,
113 #endif
115 /* strings and memory */
116 strcpy,
117 strncpy,
118 strlen,
119 strcmp,
120 strcat,
121 memset,
122 memcpy,
123 memmove,
124 memcmp,
125 memchr,
127 #if defined(DEBUG) || defined(SIMULATOR)
128 debugf,
129 #endif
130 #ifdef ROCKBOX_HAS_LOGF
131 logf,
132 #endif
134 (qsort_func)qsort,
135 &global_settings,
137 #ifdef RB_PROFILE
138 profile_thread,
139 profstop,
140 profile_func_enter,
141 profile_func_exit,
142 #endif
144 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
145 false, /* stop_encoder */
146 0, /* enc_codec_loaded */
147 enc_get_inputs,
148 enc_set_parameters,
149 enc_get_chunk,
150 enc_finish_chunk,
151 enc_get_pcm_data,
152 enc_unget_pcm_data,
154 /* file */
155 (open_func)PREFIX(open),
156 close,
157 (read_func)read,
158 PREFIX(lseek),
159 (write_func)write,
160 round_value_to_list32,
162 #endif
164 /* new stuff at the end, sort into place next time
165 the API gets incompatible */
169 void codec_get_full_path(char *path, const char *codec_root_fn)
171 snprintf(path, MAX_PATH-1, CODECS_DIR "/%s." CODEC_EXTENSION,
172 codec_root_fn);
175 static int codec_load_ram(int size, struct codec_api *api)
177 struct codec_header *hdr;
178 int status;
179 #ifndef SIMULATOR
180 hdr = (struct codec_header *)codecbuf;
182 if (size <= (signed)sizeof(struct codec_header)
183 || (hdr->magic != CODEC_MAGIC
184 #ifdef HAVE_RECORDING
185 && hdr->magic != CODEC_ENC_MAGIC
186 #endif
188 || hdr->target_id != TARGET_ID
189 || hdr->load_addr != codecbuf
190 || hdr->end_addr > codecbuf + CODEC_SIZE)
192 logf("codec header error");
193 return CODEC_ERROR;
195 #else /* SIMULATOR */
196 void *pd;
198 hdr = sim_codec_load_ram(codecbuf, size, &pd);
200 if (pd == NULL)
201 return CODEC_ERROR;
203 if (hdr == NULL
204 || (hdr->magic != CODEC_MAGIC
205 #ifdef HAVE_RECORDING
206 && hdr->magic != CODEC_ENC_MAGIC
207 #endif
209 || hdr->target_id != TARGET_ID) {
210 sim_codec_close(pd);
211 return CODEC_ERROR;
213 #endif /* SIMULATOR */
214 if (hdr->api_version > CODEC_API_VERSION
215 || hdr->api_version < CODEC_MIN_API_VERSION) {
216 sim_codec_close(pd);
217 return CODEC_ERROR;
220 invalidate_icache();
221 status = hdr->entry_point(api);
223 sim_codec_close(pd);
225 return status;
228 int codec_load_buf(unsigned int hid, struct codec_api *api) {
229 int rc;
230 rc = bufread(hid, CODEC_SIZE, codecbuf);
231 if (rc < 0) {
232 logf("error loading codec");
233 return CODEC_ERROR;
235 api->discard_codec();
236 return codec_load_ram(rc, api);
239 int codec_load_file(const char *plugin, struct codec_api *api)
241 char msgbuf[80];
242 char path[MAX_PATH];
243 int fd;
244 int rc;
246 codec_get_full_path(path, plugin);
248 fd = open(path, O_RDONLY);
249 if (fd < 0) {
250 snprintf(msgbuf, sizeof(msgbuf)-1, "Couldn't load codec: %s", path);
251 logf("Codec load error:%d", fd);
252 gui_syncsplash(HZ*2, msgbuf);
253 return fd;
256 rc = read(fd, &codecbuf[0], CODEC_SIZE);
257 close(fd);
258 if (rc <= 0) {
259 logf("Codec read error");
260 return CODEC_ERROR;
263 return codec_load_ram((size_t)rc, api);