diff options
author | Sean Bartell <wingedtachikoma@gmail.com> | 2011-06-25 21:32:25 -0400 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2012-04-25 22:13:20 +0200 |
commit | f40bfc9267b13b54e6379dfe7539447662879d24 (patch) | |
tree | 9b20069d5e62809ff434061ad730096836f916f2 /lib/rbcodec/codecs/vorbis.c | |
parent | a0009907de7a0107d49040d8a180f140e2eff299 (diff) | |
download | rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.tar.gz rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.zip |
Add codecs to librbcodec.
Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97
Reviewed-on: http://gerrit.rockbox.org/137
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
Diffstat (limited to 'lib/rbcodec/codecs/vorbis.c')
-rw-r--r-- | lib/rbcodec/codecs/vorbis.c | 263 |
1 files changed, 263 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/vorbis.c b/lib/rbcodec/codecs/vorbis.c new file mode 100644 index 0000000000..944dd6d60c --- /dev/null +++ b/lib/rbcodec/codecs/vorbis.c | |||
@@ -0,0 +1,263 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 Björn Stenberg | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "codeclib.h" | ||
23 | #include "libtremor/ivorbisfile.h" | ||
24 | #include "libtremor/ogg.h" | ||
25 | #ifdef SIMULATOR | ||
26 | #include <tlsf.h> | ||
27 | #endif | ||
28 | |||
29 | CODEC_HEADER | ||
30 | |||
31 | #if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS) | ||
32 | #include <setjmp.h> | ||
33 | jmp_buf rb_jump_buf; | ||
34 | #endif | ||
35 | |||
36 | /* Some standard functions and variables needed by Tremor */ | ||
37 | |||
38 | static size_t read_handler(void *ptr, size_t size, size_t nmemb, void *datasource) | ||
39 | { | ||
40 | (void)datasource; | ||
41 | return ci->read_filebuf(ptr, nmemb*size); | ||
42 | } | ||
43 | |||
44 | static int initial_seek_handler(void *datasource, ogg_int64_t offset, int whence) | ||
45 | { | ||
46 | (void)datasource; | ||
47 | (void)offset; | ||
48 | (void)whence; | ||
49 | return -1; | ||
50 | } | ||
51 | |||
52 | static int seek_handler(void *datasource, ogg_int64_t offset, int whence) | ||
53 | { | ||
54 | (void)datasource; | ||
55 | |||
56 | if (whence == SEEK_CUR) { | ||
57 | offset += ci->curpos; | ||
58 | } else if (whence == SEEK_END) { | ||
59 | offset += ci->filesize; | ||
60 | } | ||
61 | |||
62 | if (ci->seek_buffer(offset)) { | ||
63 | return 0; | ||
64 | } | ||
65 | |||
66 | return -1; | ||
67 | } | ||
68 | |||
69 | static int close_handler(void *datasource) | ||
70 | { | ||
71 | (void)datasource; | ||
72 | return 0; | ||
73 | } | ||
74 | |||
75 | static long tell_handler(void *datasource) | ||
76 | { | ||
77 | (void)datasource; | ||
78 | return ci->curpos; | ||
79 | } | ||
80 | |||
81 | /* This sets the DSP parameters based on the current logical bitstream | ||
82 | * (sampling rate, number of channels, etc). | ||
83 | */ | ||
84 | static bool vorbis_set_codec_parameters(OggVorbis_File *vf) | ||
85 | { | ||
86 | vorbis_info *vi; | ||
87 | |||
88 | vi = ov_info(vf, -1); | ||
89 | |||
90 | if (vi == NULL) { | ||
91 | return false; | ||
92 | } | ||
93 | |||
94 | ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); | ||
95 | codec_set_replaygain(ci->id3); | ||
96 | |||
97 | if (vi->channels == 2) { | ||
98 | ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED); | ||
99 | } else if (vi->channels == 1) { | ||
100 | ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); | ||
101 | } | ||
102 | |||
103 | return true; | ||
104 | } | ||
105 | |||
106 | /* this is the codec entry point */ | ||
107 | enum codec_status codec_main(enum codec_entry_call_reason reason) | ||
108 | { | ||
109 | if (reason == CODEC_LOAD) { | ||
110 | if (codec_init()) | ||
111 | return CODEC_ERROR; | ||
112 | ci->configure(DSP_SET_SAMPLE_DEPTH, 24); | ||
113 | } | ||
114 | |||
115 | return CODEC_OK; | ||
116 | } | ||
117 | |||
118 | /* this is called for each file to process */ | ||
119 | enum codec_status codec_run(void) | ||
120 | { | ||
121 | ov_callbacks callbacks; | ||
122 | OggVorbis_File vf; | ||
123 | ogg_int32_t **pcm; | ||
124 | |||
125 | int error = CODEC_ERROR; | ||
126 | long n; | ||
127 | int current_section; | ||
128 | int previous_section; | ||
129 | int eof; | ||
130 | ogg_int64_t vf_offsets[2]; | ||
131 | ogg_int64_t vf_dataoffsets; | ||
132 | ogg_uint32_t vf_serialnos; | ||
133 | ogg_int64_t vf_pcmlengths[2]; | ||
134 | intptr_t param; | ||
135 | |||
136 | #if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS) | ||
137 | if (setjmp(rb_jump_buf) != 0) { | ||
138 | /* malloc failed; finish with this track */ | ||
139 | goto done; | ||
140 | } | ||
141 | #endif | ||
142 | ogg_malloc_init(); | ||
143 | |||
144 | /* Create a decoder instance */ | ||
145 | callbacks.read_func = read_handler; | ||
146 | callbacks.seek_func = initial_seek_handler; | ||
147 | callbacks.tell_func = tell_handler; | ||
148 | callbacks.close_func = close_handler; | ||
149 | |||
150 | ci->seek_buffer(0); | ||
151 | |||
152 | /* Open a non-seekable stream */ | ||
153 | error = ov_open_callbacks(ci, &vf, NULL, 0, callbacks); | ||
154 | |||
155 | /* If the non-seekable open was successful, we need to supply the missing | ||
156 | * data to make it seekable. This is a hack, but it's reasonable since we | ||
157 | * don't want to run the whole file through the buffer before we start | ||
158 | * playing. Using Tremor's seekable open routine would cause us to do | ||
159 | * this, so we pretend not to be seekable at first. Then we fill in the | ||
160 | * missing fields of vf with 1) information in ci->id3, and 2) info | ||
161 | * obtained by Tremor in the above ov_open call. | ||
162 | * | ||
163 | * Note that this assumes there is only ONE logical Vorbis bitstream in our | ||
164 | * physical Ogg bitstream. This is verified in metadata.c, well before we | ||
165 | * get here. | ||
166 | */ | ||
167 | if (!error) { | ||
168 | ogg_free(vf.offsets); | ||
169 | ogg_free(vf.dataoffsets); | ||
170 | ogg_free(vf.serialnos); | ||
171 | |||
172 | vf.offsets = vf_offsets; | ||
173 | vf.dataoffsets = &vf_dataoffsets; | ||
174 | vf.serialnos = &vf_serialnos; | ||
175 | vf.pcmlengths = vf_pcmlengths; | ||
176 | |||
177 | vf.offsets[0] = 0; | ||
178 | vf.offsets[1] = ci->id3->filesize; | ||
179 | vf.dataoffsets[0] = vf.offset; | ||
180 | vf.pcmlengths[0] = 0; | ||
181 | vf.pcmlengths[1] = ci->id3->samples; | ||
182 | vf.serialnos[0] = vf.current_serialno; | ||
183 | vf.callbacks.seek_func = seek_handler; | ||
184 | vf.seekable = 1; | ||
185 | vf.end = ci->id3->filesize; | ||
186 | vf.ready_state = OPENED; | ||
187 | vf.links = 1; | ||
188 | } else { | ||
189 | DEBUGF("Vorbis: ov_open failed: %d\n", error); | ||
190 | goto done; | ||
191 | } | ||
192 | |||
193 | if (ci->id3->offset) { | ||
194 | ci->seek_buffer(ci->id3->offset); | ||
195 | ov_raw_seek(&vf, ci->id3->offset); | ||
196 | ci->set_elapsed(ov_time_tell(&vf)); | ||
197 | ci->set_offset(ov_raw_tell(&vf)); | ||
198 | } | ||
199 | else { | ||
200 | ci->set_elapsed(0); | ||
201 | } | ||
202 | |||
203 | previous_section = -1; | ||
204 | eof = 0; | ||
205 | while (!eof) { | ||
206 | enum codec_command_action action = ci->get_command(¶m); | ||
207 | |||
208 | if (action == CODEC_ACTION_HALT) | ||
209 | break; | ||
210 | |||
211 | if (action == CODEC_ACTION_SEEK_TIME) { | ||
212 | if (ov_time_seek(&vf, param)) { | ||
213 | //ci->logf("ov_time_seek failed"); | ||
214 | } | ||
215 | |||
216 | ci->set_elapsed(ov_time_tell(&vf)); | ||
217 | ci->seek_complete(); | ||
218 | } | ||
219 | |||
220 | /* Read host-endian signed 24-bit PCM samples */ | ||
221 | n = ov_read_fixed(&vf, &pcm, 1024, ¤t_section); | ||
222 | |||
223 | /* Change DSP and buffer settings for this bitstream */ | ||
224 | if (current_section != previous_section) { | ||
225 | if (!vorbis_set_codec_parameters(&vf)) { | ||
226 | goto done; | ||
227 | } else { | ||
228 | previous_section = current_section; | ||
229 | } | ||
230 | } | ||
231 | |||
232 | if (n == 0) { | ||
233 | eof = 1; | ||
234 | } else if (n < 0) { | ||
235 | DEBUGF("Vorbis: Error decoding frame\n"); | ||
236 | } else { | ||
237 | ci->pcmbuf_insert(pcm[0], pcm[1], n); | ||
238 | ci->set_offset(ov_raw_tell(&vf)); | ||
239 | ci->set_elapsed(ov_time_tell(&vf)); | ||
240 | } | ||
241 | } | ||
242 | |||
243 | error = CODEC_OK; | ||
244 | done: | ||
245 | #if 0 /* defined(SIMULATOR) */ | ||
246 | { | ||
247 | size_t bufsize; | ||
248 | void* buf = ci->codec_get_buffer(&bufsize); | ||
249 | |||
250 | DEBUGF("Vorbis: Memory max: %zu\n", get_max_size(buf)); | ||
251 | } | ||
252 | #endif | ||
253 | ogg_malloc_destroy(); | ||
254 | |||
255 | /* Clean things up for the next track */ | ||
256 | vf.dataoffsets = NULL; | ||
257 | vf.offsets = NULL; | ||
258 | vf.serialnos = NULL; | ||
259 | vf.pcmlengths = NULL; | ||
260 | ov_clear(&vf); | ||
261 | |||
262 | return error; | ||
263 | } | ||