New makefile solution: A single invocation of 'make' to build the entire tree. Fully...
[kugel-rb.git] / apps / codecs / vorbis.c
blob74d582ebb35daeb3c1d269f660d52424b41084cd
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 /* Some standard functions and variables needed by Tremor */
30 static size_t read_handler(void *ptr, size_t size, size_t nmemb, void *datasource)
32 (void)datasource;
33 return ci->read_filebuf(ptr, nmemb*size);
36 static int initial_seek_handler(void *datasource, ogg_int64_t offset, int whence)
38 (void)datasource;
39 (void)offset;
40 (void)whence;
41 return -1;
44 static int seek_handler(void *datasource, ogg_int64_t offset, int whence)
46 (void)datasource;
48 if (whence == SEEK_CUR) {
49 offset += ci->curpos;
50 } else if (whence == SEEK_END) {
51 offset += ci->filesize;
54 if (ci->seek_buffer(offset)) {
55 return 0;
58 return -1;
61 static int close_handler(void *datasource)
63 (void)datasource;
64 return 0;
67 static long tell_handler(void *datasource)
69 (void)datasource;
70 return ci->curpos;
73 /* This sets the DSP parameters based on the current logical bitstream
74 * (sampling rate, number of channels, etc). It also tries to guess
75 * reasonable buffer parameters based on the current quality setting.
77 static bool vorbis_set_codec_parameters(OggVorbis_File *vf)
79 vorbis_info *vi;
81 vi = ov_info(vf, -1);
83 if (vi == NULL) {
84 //ci->splash(HZ*2, "Vorbis Error");
85 return false;
88 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
89 codec_set_replaygain(ci->id3);
91 if (vi->channels == 2) {
92 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
93 } else if (vi->channels == 1) {
94 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
97 return true;
100 /* this is the codec entry point */
101 enum codec_status codec_main(void)
103 ov_callbacks callbacks;
104 OggVorbis_File vf;
105 ogg_int32_t **pcm;
107 int error;
108 long n;
109 int current_section;
110 int previous_section = -1;
111 int eof;
112 ogg_int64_t vf_offsets[2];
113 ogg_int64_t vf_dataoffsets;
114 ogg_uint32_t vf_serialnos;
115 ogg_int64_t vf_pcmlengths[2];
117 ci->configure(DSP_SET_SAMPLE_DEPTH, 24);
118 /* Note: These are sane defaults for these values. Perhaps
119 * they should be set differently based on quality setting
122 /* We need to flush reserver memory every track load. */
123 next_track:
124 if (codec_init()) {
125 error = CODEC_ERROR;
126 goto exit;
128 ogg_malloc_init();
130 while (!*ci->taginfo_ready && !ci->stop_codec)
131 ci->sleep(1);
133 /* Create a decoder instance */
134 callbacks.read_func = read_handler;
135 callbacks.seek_func = initial_seek_handler;
136 callbacks.tell_func = tell_handler;
137 callbacks.close_func = close_handler;
139 /* Open a non-seekable stream */
140 error = ov_open_callbacks(ci, &vf, NULL, 0, callbacks);
142 /* If the non-seekable open was successful, we need to supply the missing
143 * data to make it seekable. This is a hack, but it's reasonable since we
144 * don't want to run the whole file through the buffer before we start
145 * playing. Using Tremor's seekable open routine would cause us to do
146 * this, so we pretend not to be seekable at first. Then we fill in the
147 * missing fields of vf with 1) information in ci->id3, and 2) info
148 * obtained by Tremor in the above ov_open call.
150 * Note that this assumes there is only ONE logical Vorbis bitstream in our
151 * physical Ogg bitstream. This is verified in metadata.c, well before we
152 * get here.
154 if (!error) {
155 vf.offsets = vf_offsets;
156 vf.dataoffsets = &vf_dataoffsets;
157 vf.serialnos = &vf_serialnos;
158 vf.pcmlengths = vf_pcmlengths;
160 vf.offsets[0] = 0;
161 vf.offsets[1] = ci->id3->filesize;
162 vf.dataoffsets[0] = vf.offset;
163 vf.pcmlengths[0] = 0;
164 vf.pcmlengths[1] = ci->id3->samples;
165 vf.serialnos[0] = vf.current_serialno;
166 vf.callbacks.seek_func = seek_handler;
167 vf.seekable = 1;
168 vf.end = ci->id3->filesize;
169 vf.ready_state = OPENED;
170 vf.links = 1;
171 } else {
172 //ci->logf("ov_open: %d", error);
173 error = CODEC_ERROR;
174 goto done;
177 if (ci->id3->offset) {
178 ci->advance_buffer(ci->id3->offset);
179 ov_raw_seek(&vf, ci->id3->offset);
180 ci->set_elapsed(ov_time_tell(&vf));
181 ci->set_offset(ov_raw_tell(&vf));
184 eof = 0;
185 while (!eof) {
186 ci->yield();
187 if (ci->stop_codec || ci->new_track)
188 break;
190 if (ci->seek_time) {
191 if (ov_time_seek(&vf, ci->seek_time - 1)) {
192 //ci->logf("ov_time_seek failed");
194 ci->seek_complete();
197 /* Read host-endian signed 24-bit PCM samples */
198 n = ov_read_fixed(&vf, &pcm, 1024, &current_section);
200 /* Change DSP and buffer settings for this bitstream */
201 if (current_section != previous_section) {
202 if (!vorbis_set_codec_parameters(&vf)) {
203 error = CODEC_ERROR;
204 goto done;
205 } else {
206 previous_section = current_section;
210 if (n == 0) {
211 eof = 1;
212 } else if (n < 0) {
213 DEBUGF("Error decoding frame\n");
214 } else {
215 ci->pcmbuf_insert(pcm[0], pcm[1], n);
216 ci->set_offset(ov_raw_tell(&vf));
217 ci->set_elapsed(ov_time_tell(&vf));
220 error = CODEC_OK;
222 done:
223 if (ci->request_next_track()) {
224 /* Clean things up for the next track */
225 vf.dataoffsets = NULL;
226 vf.offsets = NULL;
227 vf.serialnos = NULL;
228 vf.pcmlengths = NULL;
229 ov_clear(&vf);
230 previous_section = -1;
231 goto next_track;
234 exit:
235 return error;