Simplify touchscreen scrollbar handling code
[Rockbox.git] / apps / codecs.c
blobaa3f81d2e653726ffe4698677c88ca41f784f592
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 extern void* plugin_get_audio_buffer(size_t *buffer_size);
71 struct codec_api ci = {
73 0, /* filesize */
74 0, /* curpos */
75 NULL, /* id3 */
76 NULL, /* taginfo_ready */
77 false, /* stop_codec */
78 0, /* new_track */
79 0, /* seek_time */
80 NULL, /* struct dsp_config *dsp */
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, /* request_next_track */
91 NULL, /* discard_codec */
92 NULL, /* set_offset */
93 NULL, /* configure */
95 /* kernel/ system */
96 PREFIX(sleep),
97 yield,
99 #if NUM_CORES > 1
100 create_thread,
101 thread_thaw,
102 thread_wait,
103 semaphore_init,
104 semaphore_wait,
105 semaphore_release,
106 event_init,
107 event_wait,
108 event_set_state,
109 #endif
111 #ifdef CACHE_FUNCTIONS_AS_CALL
112 flush_icache,
113 invalidate_icache,
114 #endif
116 /* strings and memory */
117 strcpy,
118 strncpy,
119 strlen,
120 strcmp,
121 strcat,
122 memset,
123 memcpy,
124 memmove,
125 memcmp,
126 memchr,
128 #if defined(DEBUG) || defined(SIMULATOR)
129 debugf,
130 #endif
131 #ifdef ROCKBOX_HAS_LOGF
132 logf,
133 #endif
135 (qsort_func)qsort,
136 &global_settings,
138 #ifdef RB_PROFILE
139 profile_thread,
140 profstop,
141 profile_func_enter,
142 profile_func_exit,
143 #endif
145 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
146 false, /* stop_encoder */
147 0, /* enc_codec_loaded */
148 enc_get_inputs,
149 enc_set_parameters,
150 enc_get_chunk,
151 enc_finish_chunk,
152 enc_get_pcm_data,
153 enc_unget_pcm_data,
155 /* file */
156 (open_func)PREFIX(open),
157 close,
158 (read_func)read,
159 PREFIX(lseek),
160 (write_func)write,
161 round_value_to_list32,
163 #endif
165 /* new stuff at the end, sort into place next time
166 the API gets incompatible */
170 void codec_get_full_path(char *path, const char *codec_root_fn)
172 snprintf(path, MAX_PATH-1, CODECS_DIR "/%s." CODEC_EXTENSION,
173 codec_root_fn);
176 static int codec_load_ram(int size, struct codec_api *api)
178 struct codec_header *hdr;
179 int status;
180 #ifndef SIMULATOR
181 hdr = (struct codec_header *)codecbuf;
183 if (size <= (signed)sizeof(struct codec_header)
184 || (hdr->magic != CODEC_MAGIC
185 #ifdef HAVE_RECORDING
186 && hdr->magic != CODEC_ENC_MAGIC
187 #endif
189 || hdr->target_id != TARGET_ID
190 || hdr->load_addr != codecbuf
191 || hdr->end_addr > codecbuf + CODEC_SIZE)
193 logf("codec header error");
194 return CODEC_ERROR;
196 #else /* SIMULATOR */
197 void *pd;
199 hdr = sim_codec_load_ram(codecbuf, size, &pd);
201 if (pd == NULL)
202 return CODEC_ERROR;
204 if (hdr == NULL
205 || (hdr->magic != CODEC_MAGIC
206 #ifdef HAVE_RECORDING
207 && hdr->magic != CODEC_ENC_MAGIC
208 #endif
210 || hdr->target_id != TARGET_ID) {
211 sim_codec_close(pd);
212 return CODEC_ERROR;
214 #endif /* SIMULATOR */
215 if (hdr->api_version > CODEC_API_VERSION
216 || hdr->api_version < CODEC_MIN_API_VERSION) {
217 sim_codec_close(pd);
218 return CODEC_ERROR;
221 invalidate_icache();
222 status = hdr->entry_point(api);
224 sim_codec_close(pd);
226 return status;
229 int codec_load_buf(unsigned int hid, struct codec_api *api) {
230 int rc;
231 rc = bufread(hid, CODEC_SIZE, codecbuf);
232 if (rc < 0) {
233 logf("error loading codec");
234 return CODEC_ERROR;
236 api->discard_codec();
237 return codec_load_ram(rc, api);
240 int codec_load_file(const char *plugin, struct codec_api *api)
242 char msgbuf[80];
243 char path[MAX_PATH];
244 int fd;
245 int rc;
247 codec_get_full_path(path, plugin);
249 fd = open(path, O_RDONLY);
250 if (fd < 0) {
251 snprintf(msgbuf, sizeof(msgbuf)-1, "Couldn't load codec: %s", path);
252 logf("Codec load error:%d", fd);
253 gui_syncsplash(HZ*2, msgbuf);
254 return fd;
257 rc = read(fd, &codecbuf[0], CODEC_SIZE);
258 close(fd);
259 if (rc <= 0) {
260 logf("Codec read error");
261 return CODEC_ERROR;
264 return codec_load_ram((size_t)rc, api);