Make some local global variables static.
[kugel-rb.git] / apps / codecs.c
blob8e9e55a5d48bab2b625eaafaf3f438c20fff2b7a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
23 #include <stdbool.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <timefuncs.h>
28 #include <ctype.h>
29 #include "debug.h"
30 #include "button.h"
31 #include "dir.h"
32 #include "file.h"
33 #include "kernel.h"
34 #include "sprintf.h"
35 #include "screens.h"
36 #include "misc.h"
37 #include "mas.h"
38 #include "codecs.h"
39 #include "lang.h"
40 #include "keyboard.h"
41 #include "mpeg.h"
42 #include "buffer.h"
43 #include "buffering.h"
44 #include "mp3_playback.h"
45 #include "backlight.h"
46 #include "ata.h"
47 #include "talk.h"
48 #include "mp3data.h"
49 #include "powermgmt.h"
50 #include "system.h"
51 #include "sound.h"
52 #include "splash.h"
53 #include "general.h"
55 #define LOGF_ENABLE
56 #include "logf.h"
58 #ifdef SIMULATOR
59 #if CONFIG_CODEC == SWCODEC
60 unsigned char codecbuf[CODEC_SIZE];
61 #endif
62 void *sim_codec_load_ram(char* codecptr, int size, void **pd);
63 void sim_codec_close(void *pd);
64 #else
65 #define sim_codec_close(x)
66 extern unsigned char codecbuf[];
67 #endif
69 size_t codec_size;
71 extern void* plugin_get_audio_buffer(size_t *buffer_size);
73 struct codec_api ci = {
75 0, /* filesize */
76 0, /* curpos */
77 NULL, /* id3 */
78 NULL, /* taginfo_ready */
79 false, /* stop_codec */
80 0, /* new_track */
81 0, /* seek_time */
82 NULL, /* struct dsp_config *dsp */
83 NULL, /* codec_get_buffer */
84 NULL, /* pcmbuf_insert */
85 NULL, /* set_elapsed */
86 NULL, /* read_filebuf */
87 NULL, /* request_buffer */
88 NULL, /* advance_buffer */
89 NULL, /* advance_buffer_loc */
90 NULL, /* seek_buffer */
91 NULL, /* seek_complete */
92 NULL, /* request_next_track */
93 NULL, /* discard_codec */
94 NULL, /* set_offset */
95 NULL, /* configure */
97 /* kernel/ system */
98 PREFIX(sleep),
99 yield,
101 #if NUM_CORES > 1
102 create_thread,
103 thread_thaw,
104 thread_wait,
105 semaphore_init,
106 semaphore_wait,
107 semaphore_release,
108 event_init,
109 event_wait,
110 event_set_state,
111 #endif
113 #ifdef CACHE_FUNCTIONS_AS_CALL
114 flush_icache,
115 invalidate_icache,
116 #endif
118 /* strings and memory */
119 strcpy,
120 strncpy,
121 strlen,
122 strcmp,
123 strcat,
124 memset,
125 memcpy,
126 memmove,
127 memcmp,
128 memchr,
129 strcasestr,
130 #if defined(DEBUG) || defined(SIMULATOR)
131 debugf,
132 #endif
133 #ifdef ROCKBOX_HAS_LOGF
134 logf,
135 #endif
137 (qsort_func)qsort,
138 &global_settings,
140 #ifdef RB_PROFILE
141 profile_thread,
142 profstop,
143 __cyg_profile_func_enter,
144 __cyg_profile_func_exit,
145 #endif
147 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
148 false, /* stop_encoder */
149 0, /* enc_codec_loaded */
150 enc_get_inputs,
151 enc_set_parameters,
152 enc_get_chunk,
153 enc_finish_chunk,
154 enc_get_pcm_data,
155 enc_unget_pcm_data,
157 /* file */
158 (open_func)PREFIX(open),
159 close,
160 (read_func)read,
161 PREFIX(lseek),
162 (write_func)write,
163 round_value_to_list32,
165 #endif
167 /* new stuff at the end, sort into place next time
168 the API gets incompatible */
172 void codec_get_full_path(char *path, const char *codec_root_fn)
174 snprintf(path, MAX_PATH-1, CODECS_DIR "/%s." CODEC_EXTENSION,
175 codec_root_fn);
178 static int codec_load_ram(int size, struct codec_api *api)
180 struct codec_header *hdr;
181 int status;
182 #ifndef SIMULATOR
183 hdr = (struct codec_header *)codecbuf;
185 if (size <= (signed)sizeof(struct codec_header)
186 || (hdr->magic != CODEC_MAGIC
187 #ifdef HAVE_RECORDING
188 && hdr->magic != CODEC_ENC_MAGIC
189 #endif
191 || hdr->target_id != TARGET_ID
192 || hdr->load_addr != codecbuf
193 || hdr->end_addr > codecbuf + CODEC_SIZE)
195 logf("codec header error");
196 return CODEC_ERROR;
199 codec_size = hdr->end_addr - codecbuf;
201 #else /* SIMULATOR */
202 void *pd;
204 hdr = sim_codec_load_ram(codecbuf, size, &pd);
206 if (pd == NULL)
207 return CODEC_ERROR;
209 if (hdr == NULL
210 || (hdr->magic != CODEC_MAGIC
211 #ifdef HAVE_RECORDING
212 && hdr->magic != CODEC_ENC_MAGIC
213 #endif
215 || hdr->target_id != TARGET_ID) {
216 sim_codec_close(pd);
217 return CODEC_ERROR;
220 codec_size = codecbuf - codecbuf;
222 #endif /* SIMULATOR */
223 if (hdr->api_version > CODEC_API_VERSION
224 || hdr->api_version < CODEC_MIN_API_VERSION) {
225 sim_codec_close(pd);
226 return CODEC_ERROR;
229 invalidate_icache();
230 status = hdr->entry_point(api);
232 sim_codec_close(pd);
234 return status;
237 int codec_load_buf(unsigned int hid, struct codec_api *api)
239 int rc;
240 rc = bufread(hid, CODEC_SIZE, codecbuf);
241 if (rc < 0) {
242 logf("error loading codec");
243 return CODEC_ERROR;
245 api->discard_codec();
246 return codec_load_ram(rc, api);
249 int codec_load_file(const char *plugin, struct codec_api *api)
251 char path[MAX_PATH];
252 int fd;
253 int rc;
255 codec_get_full_path(path, plugin);
257 fd = open(path, O_RDONLY);
258 if (fd < 0) {
259 logf("Codec load error:%d", fd);
260 splashf(HZ*2, "Couldn't load codec: %s", path);
261 return fd;
264 rc = read(fd, &codecbuf[0], CODEC_SIZE);
265 close(fd);
266 if (rc <= 0) {
267 logf("Codec read error");
268 return CODEC_ERROR;
271 return codec_load_ram((size_t)rc, api);