1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include "libtremor/ivorbisfile.h"
24 #include "libtremor/ogg.h"
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
)
33 return ci
->read_filebuf(ptr
, nmemb
*size
);
36 static int initial_seek_handler(void *datasource
, ogg_int64_t offset
, int whence
)
44 static int seek_handler(void *datasource
, ogg_int64_t offset
, int whence
)
48 if (whence
== SEEK_CUR
) {
50 } else if (whence
== SEEK_END
) {
51 offset
+= ci
->filesize
;
54 if (ci
->seek_buffer(offset
)) {
61 static int close_handler(void *datasource
)
67 static long tell_handler(void *datasource
)
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
)
84 //ci->splash(HZ*2, "Vorbis Error");
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
);
100 /* this is the codec entry point */
101 enum codec_status
codec_main(void)
103 ov_callbacks callbacks
;
110 int previous_section
= -1;
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. */
130 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
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
155 vf
.offsets
= vf_offsets
;
156 vf
.dataoffsets
= &vf_dataoffsets
;
157 vf
.serialnos
= &vf_serialnos
;
158 vf
.pcmlengths
= vf_pcmlengths
;
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
;
168 vf
.end
= ci
->id3
->filesize
;
169 vf
.ready_state
= OPENED
;
172 //ci->logf("ov_open: %d", error);
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
));
187 if (ci
->stop_codec
|| ci
->new_track
)
191 if (ov_time_seek(&vf
, ci
->seek_time
- 1)) {
192 //ci->logf("ov_time_seek failed");
197 /* Read host-endian signed 24-bit PCM samples */
198 n
= ov_read_fixed(&vf
, &pcm
, 1024, ¤t_section
);
200 /* Change DSP and buffer settings for this bitstream */
201 if (current_section
!= previous_section
) {
202 if (!vorbis_set_codec_parameters(&vf
)) {
206 previous_section
= current_section
;
213 DEBUGF("Error decoding frame\n");
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
));
223 if (ci
->request_next_track()) {
224 /* Clean things up for the next track */
225 vf
.dataoffsets
= NULL
;
228 vf
.pcmlengths
= NULL
;
230 previous_section
= -1;