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 #if defined(CPU_ARM) || defined(CPU_COLDFIRE)
33 /* Some standard functions and variables needed by Tremor */
35 static size_t read_handler(void *ptr
, size_t size
, size_t nmemb
, void *datasource
)
38 return ci
->read_filebuf(ptr
, nmemb
*size
);
41 static int initial_seek_handler(void *datasource
, ogg_int64_t offset
, int whence
)
49 static int seek_handler(void *datasource
, ogg_int64_t offset
, int whence
)
53 if (whence
== SEEK_CUR
) {
55 } else if (whence
== SEEK_END
) {
56 offset
+= ci
->filesize
;
59 if (ci
->seek_buffer(offset
)) {
66 static int close_handler(void *datasource
)
72 static long tell_handler(void *datasource
)
78 /* This sets the DSP parameters based on the current logical bitstream
79 * (sampling rate, number of channels, etc). It also tries to guess
80 * reasonable buffer parameters based on the current quality setting.
82 static bool vorbis_set_codec_parameters(OggVorbis_File
*vf
)
89 //ci->splash(HZ*2, "Vorbis Error");
93 ci
->configure(DSP_SWITCH_FREQUENCY
, ci
->id3
->frequency
);
94 codec_set_replaygain(ci
->id3
);
96 if (vi
->channels
== 2) {
97 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_NONINTERLEAVED
);
98 } else if (vi
->channels
== 1) {
99 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_MONO
);
105 /* this is the codec entry point */
106 enum codec_status
codec_main(void)
108 ov_callbacks callbacks
;
115 int previous_section
;
117 ogg_int64_t vf_offsets
[2];
118 ogg_int64_t vf_dataoffsets
;
119 ogg_uint32_t vf_serialnos
;
120 ogg_int64_t vf_pcmlengths
[2];
122 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 24);
123 /* Note: These are sane defaults for these values. Perhaps
124 * they should be set differently based on quality setting
127 #if defined(CPU_ARM) || defined(CPU_COLDFIRE)
128 if (setjmp(rb_jump_buf
) != 0)
130 /* malloc failed; skip to next track */
136 /* We need to flush reserver memory every track load. */
144 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
147 /* Create a decoder instance */
148 callbacks
.read_func
= read_handler
;
149 callbacks
.seek_func
= initial_seek_handler
;
150 callbacks
.tell_func
= tell_handler
;
151 callbacks
.close_func
= close_handler
;
153 /* Open a non-seekable stream */
154 error
= ov_open_callbacks(ci
, &vf
, NULL
, 0, callbacks
);
156 /* If the non-seekable open was successful, we need to supply the missing
157 * data to make it seekable. This is a hack, but it's reasonable since we
158 * don't want to run the whole file through the buffer before we start
159 * playing. Using Tremor's seekable open routine would cause us to do
160 * this, so we pretend not to be seekable at first. Then we fill in the
161 * missing fields of vf with 1) information in ci->id3, and 2) info
162 * obtained by Tremor in the above ov_open call.
164 * Note that this assumes there is only ONE logical Vorbis bitstream in our
165 * physical Ogg bitstream. This is verified in metadata.c, well before we
169 vf
.offsets
= vf_offsets
;
170 vf
.dataoffsets
= &vf_dataoffsets
;
171 vf
.serialnos
= &vf_serialnos
;
172 vf
.pcmlengths
= vf_pcmlengths
;
175 vf
.offsets
[1] = ci
->id3
->filesize
;
176 vf
.dataoffsets
[0] = vf
.offset
;
177 vf
.pcmlengths
[0] = 0;
178 vf
.pcmlengths
[1] = ci
->id3
->samples
;
179 vf
.serialnos
[0] = vf
.current_serialno
;
180 vf
.callbacks
.seek_func
= seek_handler
;
182 vf
.end
= ci
->id3
->filesize
;
183 vf
.ready_state
= OPENED
;
186 //ci->logf("ov_open: %d", error);
191 if (ci
->id3
->offset
) {
192 ci
->advance_buffer(ci
->id3
->offset
);
193 ov_raw_seek(&vf
, ci
->id3
->offset
);
194 ci
->set_elapsed(ov_time_tell(&vf
));
195 ci
->set_offset(ov_raw_tell(&vf
));
198 previous_section
= -1;
202 if (ci
->stop_codec
|| ci
->new_track
)
206 if (ov_time_seek(&vf
, ci
->seek_time
- 1)) {
207 //ci->logf("ov_time_seek failed");
212 /* Read host-endian signed 24-bit PCM samples */
213 n
= ov_read_fixed(&vf
, &pcm
, 1024, ¤t_section
);
215 /* Change DSP and buffer settings for this bitstream */
216 if (current_section
!= previous_section
) {
217 if (!vorbis_set_codec_parameters(&vf
)) {
221 previous_section
= current_section
;
228 DEBUGF("Error decoding frame\n");
230 ci
->pcmbuf_insert(pcm
[0], pcm
[1], n
);
231 ci
->set_offset(ov_raw_tell(&vf
));
232 ci
->set_elapsed(ov_time_tell(&vf
));
238 if (ci
->request_next_track()) {
239 /* Clean things up for the next track */
240 vf
.dataoffsets
= NULL
;
243 vf
.pcmlengths
= NULL
;