Flyspray: FS#10326
[kugel-rb.git] / apps / codecs / vorbis.c
blobf14aeead84207f7e901ede8a18ae179e2b547068
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 ****************************************************************************/
22 #include "codeclib.h"
23 #include "libtremor/ivorbisfile.h"
24 #include "libtremor/ogg.h"
26 CODEC_HEADER
28 #if defined(CPU_ARM) || defined(CPU_COLDFIRE)
29 #include <setjmp.h>
30 jmp_buf rb_jump_buf;
31 #endif
33 /* Some standard functions and variables needed by Tremor */
35 static size_t read_handler(void *ptr, size_t size, size_t nmemb, void *datasource)
37 (void)datasource;
38 return ci->read_filebuf(ptr, nmemb*size);
41 static int initial_seek_handler(void *datasource, ogg_int64_t offset, int whence)
43 (void)datasource;
44 (void)offset;
45 (void)whence;
46 return -1;
49 static int seek_handler(void *datasource, ogg_int64_t offset, int whence)
51 (void)datasource;
53 if (whence == SEEK_CUR) {
54 offset += ci->curpos;
55 } else if (whence == SEEK_END) {
56 offset += ci->filesize;
59 if (ci->seek_buffer(offset)) {
60 return 0;
63 return -1;
66 static int close_handler(void *datasource)
68 (void)datasource;
69 return 0;
72 static long tell_handler(void *datasource)
74 (void)datasource;
75 return ci->curpos;
78 /* This sets the DSP parameters based on the current logical bitstream
79 * (sampling rate, number of channels, etc). It also tries to guess
80 * reasonable buffer parameters based on the current quality setting.
82 static bool vorbis_set_codec_parameters(OggVorbis_File *vf)
84 vorbis_info *vi;
86 vi = ov_info(vf, -1);
88 if (vi == NULL) {
89 //ci->splash(HZ*2, "Vorbis Error");
90 return false;
93 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
94 codec_set_replaygain(ci->id3);
96 if (vi->channels == 2) {
97 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
98 } else if (vi->channels == 1) {
99 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
102 return true;
105 /* this is the codec entry point */
106 enum codec_status codec_main(void)
108 ov_callbacks callbacks;
109 OggVorbis_File vf;
110 ogg_int32_t **pcm;
112 int error;
113 long n;
114 int current_section;
115 int previous_section;
116 int eof;
117 ogg_int64_t vf_offsets[2];
118 ogg_int64_t vf_dataoffsets;
119 ogg_uint32_t vf_serialnos;
120 ogg_int64_t vf_pcmlengths[2];
122 ci->configure(DSP_SET_SAMPLE_DEPTH, 24);
123 /* Note: These are sane defaults for these values. Perhaps
124 * they should be set differently based on quality setting
127 #if defined(CPU_ARM) || defined(CPU_COLDFIRE)
128 if (setjmp(rb_jump_buf) != 0)
130 /* malloc failed; skip to next track */
131 error = CODEC_ERROR;
132 goto done;
134 #endif
136 /* We need to flush reserver memory every track load. */
137 next_track:
138 if (codec_init()) {
139 error = CODEC_ERROR;
140 goto exit;
142 ogg_malloc_init();
144 while (!*ci->taginfo_ready && !ci->stop_codec)
145 ci->sleep(1);
147 /* Create a decoder instance */
148 callbacks.read_func = read_handler;
149 callbacks.seek_func = initial_seek_handler;
150 callbacks.tell_func = tell_handler;
151 callbacks.close_func = close_handler;
153 /* Open a non-seekable stream */
154 error = ov_open_callbacks(ci, &vf, NULL, 0, callbacks);
156 /* If the non-seekable open was successful, we need to supply the missing
157 * data to make it seekable. This is a hack, but it's reasonable since we
158 * don't want to run the whole file through the buffer before we start
159 * playing. Using Tremor's seekable open routine would cause us to do
160 * this, so we pretend not to be seekable at first. Then we fill in the
161 * missing fields of vf with 1) information in ci->id3, and 2) info
162 * obtained by Tremor in the above ov_open call.
164 * Note that this assumes there is only ONE logical Vorbis bitstream in our
165 * physical Ogg bitstream. This is verified in metadata.c, well before we
166 * get here.
168 if (!error) {
169 vf.offsets = vf_offsets;
170 vf.dataoffsets = &vf_dataoffsets;
171 vf.serialnos = &vf_serialnos;
172 vf.pcmlengths = vf_pcmlengths;
174 vf.offsets[0] = 0;
175 vf.offsets[1] = ci->id3->filesize;
176 vf.dataoffsets[0] = vf.offset;
177 vf.pcmlengths[0] = 0;
178 vf.pcmlengths[1] = ci->id3->samples;
179 vf.serialnos[0] = vf.current_serialno;
180 vf.callbacks.seek_func = seek_handler;
181 vf.seekable = 1;
182 vf.end = ci->id3->filesize;
183 vf.ready_state = OPENED;
184 vf.links = 1;
185 } else {
186 //ci->logf("ov_open: %d", error);
187 error = CODEC_ERROR;
188 goto done;
191 if (ci->id3->offset) {
192 ci->advance_buffer(ci->id3->offset);
193 ov_raw_seek(&vf, ci->id3->offset);
194 ci->set_elapsed(ov_time_tell(&vf));
195 ci->set_offset(ov_raw_tell(&vf));
198 previous_section = -1;
199 eof = 0;
200 while (!eof) {
201 ci->yield();
202 if (ci->stop_codec || ci->new_track)
203 break;
205 if (ci->seek_time) {
206 if (ov_time_seek(&vf, ci->seek_time - 1)) {
207 //ci->logf("ov_time_seek failed");
209 ci->seek_complete();
212 /* Read host-endian signed 24-bit PCM samples */
213 n = ov_read_fixed(&vf, &pcm, 1024, &current_section);
215 /* Change DSP and buffer settings for this bitstream */
216 if (current_section != previous_section) {
217 if (!vorbis_set_codec_parameters(&vf)) {
218 error = CODEC_ERROR;
219 goto done;
220 } else {
221 previous_section = current_section;
225 if (n == 0) {
226 eof = 1;
227 } else if (n < 0) {
228 DEBUGF("Error decoding frame\n");
229 } else {
230 ci->pcmbuf_insert(pcm[0], pcm[1], n);
231 ci->set_offset(ov_raw_tell(&vf));
232 ci->set_elapsed(ov_time_tell(&vf));
235 error = CODEC_OK;
237 done:
238 if (ci->request_next_track()) {
239 /* Clean things up for the next track */
240 vf.dataoffsets = NULL;
241 vf.offsets = NULL;
242 vf.serialnos = NULL;
243 vf.pcmlengths = NULL;
244 ov_clear(&vf);
245 goto next_track;
248 exit:
249 return error;