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 /* 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. */
133 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
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
158 vf
.offsets
= vf_offsets
;
159 vf
.dataoffsets
= &vf_dataoffsets
;
160 vf
.serialnos
= &vf_serialnos
;
161 vf
.pcmlengths
= vf_pcmlengths
;
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
;
171 vf
.end
= ci
->id3
->filesize
;
172 vf
.ready_state
= OPENED
;
175 //ci->logf("ov_open: %d", error);
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
));
190 if (ci
->stop_codec
|| ci
->new_track
)
194 if (ov_time_seek(&vf
, ci
->seek_time
- 1)) {
195 //ci->logf("ov_time_seek failed");
200 /* Read host-endian signed 24-bit PCM samples */
201 n
= ov_read_fixed(&vf
, &pcm
, 1024, ¤t_section
);
203 /* Change DSP and buffer settings for this bitstream */
204 if (current_section
!= previous_section
) {
205 if (!vorbis_set_codec_parameters(&vf
)) {
209 previous_section
= current_section
;
216 DEBUGF("Error decoding frame\n");
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
));
226 if (ci
->request_next_track()) {
227 /* Clean things up for the next track */
228 vf
.dataoffsets
= NULL
;
231 vf
.pcmlengths
= NULL
;
233 previous_section
= -1;