Get a good start on cleaning up powermgmt.c
[kugel-rb.git] / apps / codecs.c
blob3e52dfb392b05934e0e26307b2152074b066dd59
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 <stdlib.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 "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, void **pd);
61 void sim_codec_close(void *pd);
62 #else
63 #define sim_codec_close(x)
64 extern unsigned char codecbuf[];
65 #endif
67 extern void* plugin_get_audio_buffer(size_t *buffer_size);
69 struct codec_api ci = {
71 0, /* filesize */
72 0, /* curpos */
73 NULL, /* id3 */
74 NULL, /* taginfo_ready */
75 false, /* stop_codec */
76 0, /* new_track */
77 0, /* seek_time */
78 NULL, /* struct dsp_config *dsp */
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, /* request_next_track */
89 NULL, /* discard_codec */
90 NULL, /* set_offset */
91 NULL, /* configure */
93 /* kernel/ system */
94 PREFIX(sleep),
95 yield,
97 #if NUM_CORES > 1
98 create_thread,
99 thread_thaw,
100 thread_wait,
101 semaphore_init,
102 semaphore_wait,
103 semaphore_release,
104 event_init,
105 event_wait,
106 event_set_state,
107 #endif
109 #ifdef CACHE_FUNCTIONS_AS_CALL
110 flush_icache,
111 invalidate_icache,
112 #endif
114 /* strings and memory */
115 strcpy,
116 strncpy,
117 strlen,
118 strcmp,
119 strcat,
120 memset,
121 memcpy,
122 memmove,
123 memcmp,
124 memchr,
126 #if defined(DEBUG) || defined(SIMULATOR)
127 debugf,
128 #endif
129 #ifdef ROCKBOX_HAS_LOGF
130 logf,
131 #endif
133 (qsort_func)qsort,
134 &global_settings,
136 #ifdef RB_PROFILE
137 profile_thread,
138 profstop,
139 profile_func_enter,
140 profile_func_exit,
141 #endif
143 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
144 false, /* stop_encoder */
145 0, /* enc_codec_loaded */
146 enc_get_inputs,
147 enc_set_parameters,
148 enc_get_chunk,
149 enc_finish_chunk,
150 enc_get_pcm_data,
151 enc_unget_pcm_data,
153 /* file */
154 (open_func)PREFIX(open),
155 close,
156 (read_func)read,
157 PREFIX(lseek),
158 (write_func)write,
159 round_value_to_list32,
161 #endif
163 /* new stuff at the end, sort into place next time
164 the API gets incompatible */
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 static int codec_load_ram(int size, struct codec_api *api)
176 struct codec_header *hdr;
177 int status;
178 #ifndef SIMULATOR
179 hdr = (struct codec_header *)codecbuf;
181 if (size <= (signed)sizeof(struct codec_header)
182 || (hdr->magic != CODEC_MAGIC
183 #ifdef HAVE_RECORDING
184 && hdr->magic != CODEC_ENC_MAGIC
185 #endif
187 || hdr->target_id != TARGET_ID
188 || hdr->load_addr != codecbuf
189 || hdr->end_addr > codecbuf + CODEC_SIZE)
191 logf("codec header error");
192 return CODEC_ERROR;
194 #else /* SIMULATOR */
195 void *pd;
197 hdr = sim_codec_load_ram(codecbuf, size, &pd);
199 if (pd == NULL)
200 return CODEC_ERROR;
202 if (hdr == NULL
203 || (hdr->magic != CODEC_MAGIC
204 #ifdef HAVE_RECORDING
205 && hdr->magic != CODEC_ENC_MAGIC
206 #endif
208 || hdr->target_id != TARGET_ID) {
209 sim_codec_close(pd);
210 return CODEC_ERROR;
212 #endif /* SIMULATOR */
213 if (hdr->api_version > CODEC_API_VERSION
214 || hdr->api_version < CODEC_MIN_API_VERSION) {
215 sim_codec_close(pd);
216 return CODEC_ERROR;
219 invalidate_icache();
220 status = hdr->entry_point(api);
222 sim_codec_close(pd);
224 return status;
227 int codec_load_buf(unsigned int hid, struct codec_api *api) {
228 int rc;
229 rc = bufread(hid, CODEC_SIZE, codecbuf);
230 if (rc < 0) {
231 logf("error loading codec");
232 return CODEC_ERROR;
234 api->discard_codec();
235 return codec_load_ram(rc, api);
238 int codec_load_file(const char *plugin, struct codec_api *api)
240 char msgbuf[80];
241 char path[MAX_PATH];
242 int fd;
243 int rc;
245 codec_get_full_path(path, plugin);
247 fd = open(path, O_RDONLY);
248 if (fd < 0) {
249 snprintf(msgbuf, sizeof(msgbuf)-1, "Couldn't load codec: %s", path);
250 logf("Codec load error:%d", fd);
251 gui_syncsplash(HZ*2, msgbuf);
252 return fd;
255 rc = read(fd, &codecbuf[0], CODEC_SIZE);
256 close(fd);
257 if (rc <= 0) {
258 logf("Codec read error");
259 return CODEC_ERROR;
262 return codec_load_ram((size_t)rc, api);