rbutilQt: Add Uninstallation for Rockbox and Bootloaders. Also add Talkfiles to the...
[Rockbox.git] / apps / codecs / vorbis.c
blob2ea0f746451d560012fef4ed4b9ce7c986e82810
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 /* The chunk size below is magic. If set any lower, resume
121 * doesn't work properly (ov_raw_seek() does the wrong thing).
123 ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, 1024*256);
125 /* We need to flush reserver memory every track load. */
126 next_track:
127 if (codec_init()) {
128 error = CODEC_ERROR;
129 goto exit;
131 ogg_malloc_init();
133 while (!*ci->taginfo_ready && !ci->stop_codec)
134 ci->sleep(1);
136 /* Create a decoder instance */
137 callbacks.read_func = read_handler;
138 callbacks.seek_func = initial_seek_handler;
139 callbacks.tell_func = tell_handler;
140 callbacks.close_func = close_handler;
142 /* Open a non-seekable stream */
143 error = ov_open_callbacks(ci, &vf, NULL, 0, callbacks);
145 /* If the non-seekable open was successful, we need to supply the missing
146 * data to make it seekable. This is a hack, but it's reasonable since we
147 * don't want to run the whole file through the buffer before we start
148 * playing. Using Tremor's seekable open routine would cause us to do
149 * this, so we pretend not to be seekable at first. Then we fill in the
150 * missing fields of vf with 1) information in ci->id3, and 2) info
151 * obtained by Tremor in the above ov_open call.
153 * Note that this assumes there is only ONE logical Vorbis bitstream in our
154 * physical Ogg bitstream. This is verified in metadata.c, well before we
155 * get here.
157 if (!error) {
158 vf.offsets = vf_offsets;
159 vf.dataoffsets = &vf_dataoffsets;
160 vf.serialnos = &vf_serialnos;
161 vf.pcmlengths = vf_pcmlengths;
163 vf.offsets[0] = 0;
164 vf.offsets[1] = ci->id3->filesize;
165 vf.dataoffsets[0] = vf.offset;
166 vf.pcmlengths[0] = 0;
167 vf.pcmlengths[1] = ci->id3->samples;
168 vf.serialnos[0] = vf.current_serialno;
169 vf.callbacks.seek_func = seek_handler;
170 vf.seekable = 1;
171 vf.end = ci->id3->filesize;
172 vf.ready_state = OPENED;
173 vf.links = 1;
174 } else {
175 //ci->logf("ov_open: %d", error);
176 error = CODEC_ERROR;
177 goto done;
180 if (ci->id3->offset) {
181 ci->advance_buffer(ci->id3->offset);
182 ov_raw_seek(&vf, ci->id3->offset);
183 ci->set_elapsed(ov_time_tell(&vf));
184 ci->set_offset(ov_raw_tell(&vf));
187 eof = 0;
188 while (!eof) {
189 ci->yield();
190 if (ci->stop_codec || ci->new_track)
191 break;
193 if (ci->seek_time) {
194 if (ov_time_seek(&vf, ci->seek_time - 1)) {
195 //ci->logf("ov_time_seek failed");
197 ci->seek_complete();
200 /* Read host-endian signed 24-bit PCM samples */
201 n = ov_read_fixed(&vf, &pcm, 1024, &current_section);
203 /* Change DSP and buffer settings for this bitstream */
204 if (current_section != previous_section) {
205 if (!vorbis_set_codec_parameters(&vf)) {
206 error = CODEC_ERROR;
207 goto done;
208 } else {
209 previous_section = current_section;
213 if (n == 0) {
214 eof = 1;
215 } else if (n < 0) {
216 DEBUGF("Error decoding frame\n");
217 } else {
218 ci->pcmbuf_insert(pcm[0], pcm[1], n);
219 ci->set_offset(ov_raw_tell(&vf));
220 ci->set_elapsed(ov_time_tell(&vf));
223 error = CODEC_OK;
225 done:
226 if (ci->request_next_track()) {
227 /* Clean things up for the next track */
228 vf.dataoffsets = NULL;
229 vf.offsets = NULL;
230 vf.serialnos = NULL;
231 vf.pcmlengths = NULL;
232 ov_clear(&vf);
233 previous_section = -1;
234 goto next_track;
237 exit:
238 return error;