1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
21 #include "Tremor/ivorbisfile.h"
22 #include "Tremor/ogg.h"
26 /* Some standard functions and variables needed by Tremor */
28 size_t read_handler(void *ptr
, size_t size
, size_t nmemb
, void *datasource
)
31 return ci
->read_filebuf(ptr
, nmemb
*size
);
34 int initial_seek_handler(void *datasource
, ogg_int64_t offset
, int whence
)
42 int seek_handler(void *datasource
, ogg_int64_t offset
, int whence
)
46 if (whence
== SEEK_CUR
) {
48 } else if (whence
== SEEK_END
) {
49 offset
+= ci
->filesize
;
52 if (ci
->seek_buffer(offset
)) {
59 int close_handler(void *datasource
)
65 long tell_handler(void *datasource
)
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
)
82 //ci->splash(HZ*2, "Vorbis Error");
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
);
98 /* this is the codec entry point */
99 enum codec_status
codec_main(void)
101 ov_callbacks callbacks
;
108 int previous_section
= -1;
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. */
128 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
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
153 vf
.offsets
= vf_offsets
;
154 vf
.dataoffsets
= &vf_dataoffsets
;
155 vf
.serialnos
= &vf_serialnos
;
156 vf
.pcmlengths
= vf_pcmlengths
;
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
;
166 vf
.end
= ci
->id3
->filesize
;
167 vf
.ready_state
= OPENED
;
170 //ci->logf("ov_open: %d", error);
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
));
185 if (ci
->stop_codec
|| ci
->new_track
)
189 if (ov_time_seek(&vf
, ci
->seek_time
- 1)) {
190 //ci->logf("ov_time_seek failed");
195 /* Read host-endian signed 24-bit PCM samples */
196 n
= ov_read_fixed(&vf
, &pcm
, 1024, ¤t_section
);
198 /* Change DSP and buffer settings for this bitstream */
199 if (current_section
!= previous_section
) {
200 if (!vorbis_set_codec_parameters(&vf
)) {
204 previous_section
= current_section
;
211 DEBUGF("Error decoding frame\n");
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
));
221 if (ci
->request_next_track()) {
222 /* Clean things up for the next track */
223 vf
.dataoffsets
= NULL
;
226 vf
.pcmlengths
= NULL
;
228 previous_section
= -1;