rk27xx: move debug-target.h content to .c
[maemo-rb.git] / apps / codecs.c
blobfafe4ac7a3f9103418f5f29c1c4615fc545fbfea
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 <stdarg.h>
30 #include "string-extra.h"
31 #include "load_code.h"
32 #include "debug.h"
33 #include "button.h"
34 #include "dir.h"
35 #include "file.h"
36 #include "kernel.h"
37 #include "screens.h"
38 #include "misc.h"
39 #include "codecs.h"
40 #include "lang.h"
41 #include "keyboard.h"
42 #include "buffering.h"
43 #include "mp3_playback.h"
44 #include "backlight.h"
45 #include "storage.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"
53 #include "rbpaths.h"
55 #define LOGF_ENABLE
56 #include "logf.h"
58 #if (CONFIG_PLATFORM & (PLATFORM_SDL|PLATFORM_MAEMO|PLATFORM_PANDORA))
59 #define PREFIX(_x_) sim_ ## _x_
60 #else
61 #define PREFIX(_x_) _x_
62 #endif
64 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
65 /* For PLATFORM_HOSTED this buffer must be define here. */
66 static unsigned char codecbuf[CODEC_SIZE];
67 #else
68 /* For PLATFORM_NATIVE this buffer is defined in *.lds files. */
69 extern unsigned char codecbuf[];
70 #endif
72 static size_t codec_size;
74 extern void* plugin_get_audio_buffer(size_t *buffer_size);
76 #if (CONFIG_PLATFORM & PLATFORM_NATIVE) && defined(HAVE_RECORDING)
77 #undef open
78 static int open(const char* pathname, int flags, ...)
80 return file_open(pathname, flags);
82 #endif
83 struct codec_api ci = {
85 0, /* filesize */
86 0, /* curpos */
87 NULL, /* id3 */
88 ERR_HANDLE_NOT_FOUND, /* audio_hid */
89 NULL, /* struct dsp_config *dsp */
90 NULL, /* codec_get_buffer */
91 NULL, /* pcmbuf_insert */
92 NULL, /* set_elapsed */
93 NULL, /* read_filebuf */
94 NULL, /* request_buffer */
95 NULL, /* advance_buffer */
96 NULL, /* seek_buffer */
97 NULL, /* seek_complete */
98 NULL, /* set_offset */
99 NULL, /* configure */
100 NULL, /* get_command */
101 NULL, /* loop_track */
103 /* kernel/ system */
104 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
105 __div0,
106 #endif
107 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 commit_dcache,
120 commit_discard_dcache,
122 /* strings and memory */
123 strcpy,
124 strlen,
125 strcmp,
126 strcat,
127 memset,
128 memcpy,
129 memmove,
130 memcmp,
131 memchr,
132 #if defined(DEBUG) || defined(SIMULATOR)
133 debugf,
134 #endif
135 #ifdef ROCKBOX_HAS_LOGF
136 logf,
137 #endif
139 (qsort_func)qsort,
141 #ifdef RB_PROFILE
142 profile_thread,
143 profstop,
144 __cyg_profile_func_enter,
145 __cyg_profile_func_exit,
146 #endif
148 #ifdef HAVE_RECORDING
149 enc_get_inputs,
150 enc_set_parameters,
151 enc_get_chunk,
152 enc_finish_chunk,
153 enc_get_pcm_data,
154 enc_unget_pcm_data,
156 /* file */
157 (open_func)PREFIX(open),
158 PREFIX(close),
159 (read_func)PREFIX(read),
160 PREFIX(lseek),
161 (write_func)PREFIX(write),
162 round_value_to_list32,
164 #endif /* HAVE_RECORDING */
166 /* new stuff at the end, sort into place next time
167 the API gets incompatible */
169 commit_discard_idcache,
172 void codec_get_full_path(char *path, const char *codec_root_fn)
174 snprintf(path, MAX_PATH-1, CODECS_DIR "/" CODEC_PREFIX "%s."
175 CODEC_EXTENSION, codec_root_fn);
178 /* Returns pointer to and size of free codec RAM. Aligns to CACHEALIGN_SIZE. */
179 void *codec_get_buffer_callback(size_t *size)
181 void *buf = &codecbuf[codec_size];
182 ssize_t s = CODEC_SIZE - codec_size;
184 if (s <= 0)
185 return NULL;
187 *size = s;
188 ALIGN_BUFFER(buf, *size, CACHEALIGN_SIZE);
190 return buf;
193 /** codec loading and call interface **/
194 static void *curr_handle = NULL;
195 static struct codec_header *c_hdr = NULL;
197 static int codec_load_ram(struct codec_api *api)
199 struct lc_header *hdr;
201 c_hdr = lc_get_header(curr_handle);
202 hdr = c_hdr ? &c_hdr->lc_hdr : NULL;
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 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
212 || hdr->load_addr != codecbuf
213 || hdr->end_addr > codecbuf + CODEC_SIZE
214 #endif
217 logf("codec header error");
218 lc_close(curr_handle);
219 curr_handle = NULL;
220 return CODEC_ERROR;
223 if (hdr->api_version > CODEC_API_VERSION
224 || hdr->api_version < CODEC_MIN_API_VERSION) {
225 logf("codec api version error");
226 lc_close(curr_handle);
227 curr_handle = NULL;
228 return CODEC_ERROR;
231 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
232 codec_size = hdr->end_addr - codecbuf;
233 #else
234 codec_size = 0;
235 #endif
237 *(c_hdr->api) = api;
239 logf("Codec: calling entrypoint");
240 return c_hdr->entry_point(CODEC_LOAD);
243 int codec_load_buf(int hid, struct codec_api *api)
245 int rc = bufread(hid, CODEC_SIZE, codecbuf);
247 if (rc < 0) {
248 logf("Codec: cannot read buf handle");
249 return CODEC_ERROR;
252 curr_handle = lc_open_from_mem(codecbuf, rc);
254 if (curr_handle == NULL) {
255 logf("Codec: load error");
256 return CODEC_ERROR;
259 return codec_load_ram(api);
262 int codec_load_file(const char *plugin, struct codec_api *api)
264 char path[MAX_PATH];
266 codec_get_full_path(path, plugin);
268 curr_handle = lc_open(path, codecbuf, CODEC_SIZE);
270 if (curr_handle == NULL) {
271 logf("Codec: cannot read file");
272 return CODEC_ERROR;
275 return codec_load_ram(api);
278 int codec_run_proc(void)
280 if (curr_handle == NULL) {
281 logf("Codec: no codec to run");
282 return CODEC_ERROR;
285 logf("Codec: entering run state");
286 return c_hdr->run_proc();
289 int codec_close(void)
291 int status = CODEC_OK;
293 if (curr_handle != NULL) {
294 logf("Codec: cleaning up");
295 status = c_hdr->entry_point(CODEC_UNLOAD);
296 lc_close(curr_handle);
297 curr_handle = NULL;
300 return status;