Fix too-soon frequency unboost when pcm buffer crosses watermark from the low side...
[kugel-rb.git] / apps / codecs.c
blob565adcf203d2d15214782116b9fee8b2cefdaa24
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 "storage.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 #define PREFIX(_x_) sim_ ## _x_
60 #else
61 #define PREFIX
62 #endif
64 #ifdef SIMULATOR
65 #if CONFIG_CODEC == SWCODEC
66 unsigned char codecbuf[CODEC_SIZE];
67 #endif
68 void *sim_codec_load_ram(char* codecptr, int size, void **pd);
69 void sim_codec_close(void *pd);
70 #else
71 #define sim_codec_close(x)
72 extern unsigned char codecbuf[];
73 #endif
75 size_t codec_size;
77 extern void* plugin_get_audio_buffer(size_t *buffer_size);
79 struct codec_api ci = {
81 0, /* filesize */
82 0, /* curpos */
83 NULL, /* id3 */
84 NULL, /* taginfo_ready */
85 false, /* stop_codec */
86 0, /* new_track */
87 0, /* seek_time */
88 NULL, /* struct dsp_config *dsp */
89 NULL, /* codec_get_buffer */
90 NULL, /* pcmbuf_insert */
91 NULL, /* set_elapsed */
92 NULL, /* read_filebuf */
93 NULL, /* request_buffer */
94 NULL, /* advance_buffer */
95 NULL, /* advance_buffer_loc */
96 NULL, /* seek_buffer */
97 NULL, /* seek_complete */
98 NULL, /* request_next_track */
99 NULL, /* discard_codec */
100 NULL, /* set_offset */
101 NULL, /* configure */
103 /* kernel/ system */
104 #ifdef CPU_ARM
105 __div0,
106 #endif
107 PREFIX(sleep),
108 yield,
110 #if NUM_CORES > 1
111 create_thread,
112 thread_thaw,
113 thread_wait,
114 semaphore_init,
115 semaphore_wait,
116 semaphore_release,
117 #endif
119 #if NUM_CORES > 1
120 cpucache_flush,
121 cpucache_invalidate,
122 #endif
124 /* strings and memory */
125 strcpy,
126 strncpy,
127 strlen,
128 strcmp,
129 strcat,
130 memset,
131 memcpy,
132 memmove,
133 memcmp,
134 memchr,
135 strcasestr,
136 #if defined(DEBUG) || defined(SIMULATOR)
137 debugf,
138 #endif
139 #ifdef ROCKBOX_HAS_LOGF
140 logf,
141 #endif
143 (qsort_func)qsort,
144 &global_settings,
146 #ifdef RB_PROFILE
147 profile_thread,
148 profstop,
149 __cyg_profile_func_enter,
150 __cyg_profile_func_exit,
151 #endif
153 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
154 false, /* stop_encoder */
155 0, /* enc_codec_loaded */
156 enc_get_inputs,
157 enc_set_parameters,
158 enc_get_chunk,
159 enc_finish_chunk,
160 enc_get_pcm_data,
161 enc_unget_pcm_data,
163 /* file */
164 (open_func)PREFIX(open),
165 close,
166 (read_func)read,
167 PREFIX(lseek),
168 (write_func)write,
169 round_value_to_list32,
171 #endif
173 /* new stuff at the end, sort into place next time
174 the API gets incompatible */
177 void codec_get_full_path(char *path, const char *codec_root_fn)
179 snprintf(path, MAX_PATH-1, CODECS_DIR "/%s." CODEC_EXTENSION,
180 codec_root_fn);
183 static int codec_load_ram(int size, struct codec_api *api)
185 struct codec_header *hdr;
186 int status;
187 #ifndef SIMULATOR
188 hdr = (struct codec_header *)codecbuf;
190 if (size <= (signed)sizeof(struct codec_header)
191 || (hdr->magic != CODEC_MAGIC
192 #ifdef HAVE_RECORDING
193 && hdr->magic != CODEC_ENC_MAGIC
194 #endif
196 || hdr->target_id != TARGET_ID
197 || hdr->load_addr != codecbuf
198 || hdr->end_addr > codecbuf + CODEC_SIZE)
200 logf("codec header error");
201 return CODEC_ERROR;
204 codec_size = hdr->end_addr - codecbuf;
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;
225 codec_size = codecbuf - codecbuf;
227 #endif /* SIMULATOR */
228 if (hdr->api_version > CODEC_API_VERSION
229 || hdr->api_version < CODEC_MIN_API_VERSION) {
230 sim_codec_close(pd);
231 return CODEC_ERROR;
234 *(hdr->api) = api;
235 cpucache_invalidate();
236 status = hdr->entry_point();
238 sim_codec_close(pd);
240 return status;
243 int codec_load_buf(unsigned int hid, struct codec_api *api)
245 int rc;
246 rc = bufread(hid, CODEC_SIZE, codecbuf);
247 if (rc < 0) {
248 logf("error loading codec");
249 return CODEC_ERROR;
251 api->discard_codec();
252 return codec_load_ram(rc, api);
255 int codec_load_file(const char *plugin, struct codec_api *api)
257 char path[MAX_PATH];
258 int fd;
259 int rc;
261 codec_get_full_path(path, plugin);
263 fd = open(path, O_RDONLY);
264 if (fd < 0) {
265 logf("Codec load error:%d", fd);
266 splashf(HZ*2, "Couldn't load codec: %s", path);
267 return fd;
270 rc = read(fd, &codecbuf[0], CODEC_SIZE);
271 close(fd);
272 if (rc <= 0) {
273 logf("Codec read error");
274 return CODEC_ERROR;
277 return codec_load_ram((size_t)rc, api);