Add a slot to set the progress bar value and a member to hide it.
[Rockbox.git] / apps / codecs / vorbis.c
blobad53ecd0b63140776b241b7f16c232600b3b537c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "codeclib.h"
21 #include "Tremor/ivorbisfile.h"
22 #include "Tremor/ogg.h"
24 CODEC_HEADER
26 /* Some standard functions and variables needed by Tremor */
28 size_t read_handler(void *ptr, size_t size, size_t nmemb, void *datasource)
30 (void)datasource;
31 return ci->read_filebuf(ptr, nmemb*size);
34 int initial_seek_handler(void *datasource, ogg_int64_t offset, int whence)
36 (void)datasource;
37 (void)offset;
38 (void)whence;
39 return -1;
42 int seek_handler(void *datasource, ogg_int64_t offset, int whence)
44 (void)datasource;
46 if (whence == SEEK_CUR) {
47 offset += ci->curpos;
48 } else if (whence == SEEK_END) {
49 offset += ci->filesize;
52 if (ci->seek_buffer(offset)) {
53 return 0;
56 return -1;
59 int close_handler(void *datasource)
61 (void)datasource;
62 return 0;
65 long tell_handler(void *datasource)
67 (void)datasource;
68 return ci->curpos;
71 /* This sets the DSP parameters based on the current logical bitstream
72 * (sampling rate, number of channels, etc). It also tries to guess
73 * reasonable buffer parameters based on the current quality setting.
75 bool vorbis_set_codec_parameters(OggVorbis_File *vf)
77 vorbis_info *vi;
79 vi = ov_info(vf, -1);
81 if (vi == NULL) {
82 //ci->splash(HZ*2, "Vorbis Error");
83 return false;
86 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
87 codec_set_replaygain(ci->id3);
89 if (vi->channels == 2) {
90 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
91 } else if (vi->channels == 1) {
92 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
95 return true;
98 /* this is the codec entry point */
99 enum codec_status codec_main(void)
101 ov_callbacks callbacks;
102 OggVorbis_File vf;
103 ogg_int32_t **pcm;
105 int error;
106 long n;
107 int current_section;
108 int previous_section = -1;
109 int eof;
110 ogg_int64_t vf_offsets[2];
111 ogg_int64_t vf_dataoffsets;
112 ogg_uint32_t vf_serialnos;
113 ogg_int64_t vf_pcmlengths[2];
115 ci->configure(DSP_SET_SAMPLE_DEPTH, 24);
116 /* Note: These are sane defaults for these values. Perhaps
117 * they should be set differently based on quality setting
120 /* We need to flush reserver memory every track load. */
121 next_track:
122 if (codec_init()) {
123 error = CODEC_ERROR;
124 goto exit;
126 ogg_malloc_init();
128 while (!*ci->taginfo_ready && !ci->stop_codec)
129 ci->sleep(1);
131 /* Create a decoder instance */
132 callbacks.read_func = read_handler;
133 callbacks.seek_func = initial_seek_handler;
134 callbacks.tell_func = tell_handler;
135 callbacks.close_func = close_handler;
137 /* Open a non-seekable stream */
138 error = ov_open_callbacks(ci, &vf, NULL, 0, callbacks);
140 /* If the non-seekable open was successful, we need to supply the missing
141 * data to make it seekable. This is a hack, but it's reasonable since we
142 * don't want to run the whole file through the buffer before we start
143 * playing. Using Tremor's seekable open routine would cause us to do
144 * this, so we pretend not to be seekable at first. Then we fill in the
145 * missing fields of vf with 1) information in ci->id3, and 2) info
146 * obtained by Tremor in the above ov_open call.
148 * Note that this assumes there is only ONE logical Vorbis bitstream in our
149 * physical Ogg bitstream. This is verified in metadata.c, well before we
150 * get here.
152 if (!error) {
153 vf.offsets = vf_offsets;
154 vf.dataoffsets = &vf_dataoffsets;
155 vf.serialnos = &vf_serialnos;
156 vf.pcmlengths = vf_pcmlengths;
158 vf.offsets[0] = 0;
159 vf.offsets[1] = ci->id3->filesize;
160 vf.dataoffsets[0] = vf.offset;
161 vf.pcmlengths[0] = 0;
162 vf.pcmlengths[1] = ci->id3->samples;
163 vf.serialnos[0] = vf.current_serialno;
164 vf.callbacks.seek_func = seek_handler;
165 vf.seekable = 1;
166 vf.end = ci->id3->filesize;
167 vf.ready_state = OPENED;
168 vf.links = 1;
169 } else {
170 //ci->logf("ov_open: %d", error);
171 error = CODEC_ERROR;
172 goto done;
175 if (ci->id3->offset) {
176 ci->advance_buffer(ci->id3->offset);
177 ov_raw_seek(&vf, ci->id3->offset);
178 ci->set_elapsed(ov_time_tell(&vf));
179 ci->set_offset(ov_raw_tell(&vf));
182 eof = 0;
183 while (!eof) {
184 ci->yield();
185 if (ci->stop_codec || ci->new_track)
186 break;
188 if (ci->seek_time) {
189 if (ov_time_seek(&vf, ci->seek_time - 1)) {
190 //ci->logf("ov_time_seek failed");
192 ci->seek_complete();
195 /* Read host-endian signed 24-bit PCM samples */
196 n = ov_read_fixed(&vf, &pcm, 1024, &current_section);
198 /* Change DSP and buffer settings for this bitstream */
199 if (current_section != previous_section) {
200 if (!vorbis_set_codec_parameters(&vf)) {
201 error = CODEC_ERROR;
202 goto done;
203 } else {
204 previous_section = current_section;
208 if (n == 0) {
209 eof = 1;
210 } else if (n < 0) {
211 DEBUGF("Error decoding frame\n");
212 } else {
213 ci->pcmbuf_insert(pcm[0], pcm[1], n);
214 ci->set_offset(ov_raw_tell(&vf));
215 ci->set_elapsed(ov_time_tell(&vf));
218 error = CODEC_OK;
220 done:
221 if (ci->request_next_track()) {
222 /* Clean things up for the next track */
223 vf.dataoffsets = NULL;
224 vf.offsets = NULL;
225 vf.serialnos = NULL;
226 vf.pcmlengths = NULL;
227 ov_clear(&vf);
228 previous_section = -1;
229 goto next_track;
232 exit:
233 return error;