Fix backlight timeout in 'keep backlight running' plugins (related to yesterday's...
[Rockbox.git] / apps / codecs.c
blob9969b6f59722a47b5b141d806821c0e8918b8275
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, /* 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 */
160 #ifdef CACHE_FUNCTIONS_AS_CALL
161 flush_icache,
162 invalidate_icache,
163 #endif
165 NULL, /* struct dsp_config *dsp */
167 #if NUM_CORES > 1
168 create_thread,
169 thread_thaw,
170 thread_wait,
171 semaphore_init,
172 semaphore_wait,
173 semaphore_release,
174 event_init,
175 event_wait,
176 event_set_state,
177 #endif
180 void codec_get_full_path(char *path, const char *codec_root_fn)
182 snprintf(path, MAX_PATH-1, CODECS_DIR "/%s." CODEC_EXTENSION,
183 codec_root_fn);
186 static int codec_load_ram(int size, struct codec_api *api)
188 struct codec_header *hdr;
189 int status;
190 #ifndef SIMULATOR
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(codecbuf, size, &pd);
211 if (pd == NULL)
212 return CODEC_ERROR;
214 if (hdr == NULL
215 || (hdr->magic != CODEC_MAGIC
216 #ifdef HAVE_RECORDING
217 && hdr->magic != CODEC_ENC_MAGIC
218 #endif
220 || hdr->target_id != TARGET_ID) {
221 sim_codec_close(pd);
222 return CODEC_ERROR;
224 #endif /* SIMULATOR */
225 if (hdr->api_version > CODEC_API_VERSION
226 || hdr->api_version < CODEC_MIN_API_VERSION) {
227 sim_codec_close(pd);
228 return CODEC_ERROR;
231 invalidate_icache();
232 status = hdr->entry_point(api);
234 sim_codec_close(pd);
236 return status;
239 int codec_load_buf(unsigned int hid, struct codec_api *api) {
240 int rc;
241 rc = bufread(hid, CODEC_SIZE, codecbuf);
242 if (rc < 0) {
243 logf("error loading codec");
244 return CODEC_ERROR;
246 api->discard_codec();
247 return codec_load_ram(rc, api);
250 int codec_load_file(const char *plugin, struct codec_api *api)
252 char msgbuf[80];
253 char path[MAX_PATH];
254 int fd;
255 int rc;
257 codec_get_full_path(path, plugin);
259 fd = open(path, O_RDONLY);
260 if (fd < 0) {
261 snprintf(msgbuf, sizeof(msgbuf)-1, "Couldn't load codec: %s", path);
262 logf("Codec load error:%d", fd);
263 gui_syncsplash(HZ*2, msgbuf);
264 return fd;
267 rc = read(fd, &codecbuf[0], CODEC_SIZE);
268 close(fd);
269 if (rc <= 0) {
270 logf("Codec read error");
271 return CODEC_ERROR;
274 return codec_load_ram((size_t)rc, api);