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"
26 #include "lib/tlsf/src/tlsf.h"
31 #if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS)
36 /* Some standard functions and variables needed by Tremor */
38 static size_t read_handler(void *ptr
, size_t size
, size_t nmemb
, void *datasource
)
41 return ci
->read_filebuf(ptr
, nmemb
*size
);
44 static int initial_seek_handler(void *datasource
, ogg_int64_t offset
, int whence
)
52 static int seek_handler(void *datasource
, ogg_int64_t offset
, int whence
)
56 if (whence
== SEEK_CUR
) {
58 } else if (whence
== SEEK_END
) {
59 offset
+= ci
->filesize
;
62 if (ci
->seek_buffer(offset
)) {
69 static int close_handler(void *datasource
)
75 static long tell_handler(void *datasource
)
81 /* This sets the DSP parameters based on the current logical bitstream
82 * (sampling rate, number of channels, etc).
84 static bool vorbis_set_codec_parameters(OggVorbis_File
*vf
)
94 ci
->configure(DSP_SWITCH_FREQUENCY
, ci
->id3
->frequency
);
95 codec_set_replaygain(ci
->id3
);
97 if (vi
->channels
== 2) {
98 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_NONINTERLEAVED
);
99 } else if (vi
->channels
== 1) {
100 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_MONO
);
106 /* this is the codec entry point */
107 enum codec_status
codec_main(void)
109 ov_callbacks callbacks
;
116 int previous_section
;
118 ogg_int64_t vf_offsets
[2];
119 ogg_int64_t vf_dataoffsets
;
120 ogg_uint32_t vf_serialnos
;
121 ogg_int64_t vf_pcmlengths
[2];
123 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 24);
132 #if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS)
133 if (setjmp(rb_jump_buf
) != 0) {
134 /* malloc failed; skip to next track */
141 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
144 /* Create a decoder instance */
145 callbacks
.read_func
= read_handler
;
146 callbacks
.seek_func
= initial_seek_handler
;
147 callbacks
.tell_func
= tell_handler
;
148 callbacks
.close_func
= close_handler
;
150 /* Open a non-seekable stream */
151 error
= ov_open_callbacks(ci
, &vf
, NULL
, 0, callbacks
);
153 /* If the non-seekable open was successful, we need to supply the missing
154 * data to make it seekable. This is a hack, but it's reasonable since we
155 * don't want to run the whole file through the buffer before we start
156 * playing. Using Tremor's seekable open routine would cause us to do
157 * this, so we pretend not to be seekable at first. Then we fill in the
158 * missing fields of vf with 1) information in ci->id3, and 2) info
159 * obtained by Tremor in the above ov_open call.
161 * Note that this assumes there is only ONE logical Vorbis bitstream in our
162 * physical Ogg bitstream. This is verified in metadata.c, well before we
166 ogg_free(vf
.offsets
);
167 ogg_free(vf
.dataoffsets
);
168 ogg_free(vf
.serialnos
);
170 vf
.offsets
= vf_offsets
;
171 vf
.dataoffsets
= &vf_dataoffsets
;
172 vf
.serialnos
= &vf_serialnos
;
173 vf
.pcmlengths
= vf_pcmlengths
;
176 vf
.offsets
[1] = ci
->id3
->filesize
;
177 vf
.dataoffsets
[0] = vf
.offset
;
178 vf
.pcmlengths
[0] = 0;
179 vf
.pcmlengths
[1] = ci
->id3
->samples
;
180 vf
.serialnos
[0] = vf
.current_serialno
;
181 vf
.callbacks
.seek_func
= seek_handler
;
183 vf
.end
= ci
->id3
->filesize
;
184 vf
.ready_state
= OPENED
;
187 DEBUGF("Vorbis: ov_open failed: %d", error
);
192 if (ci
->id3
->offset
) {
193 ci
->advance_buffer(ci
->id3
->offset
);
194 ov_raw_seek(&vf
, ci
->id3
->offset
);
195 ci
->set_elapsed(ov_time_tell(&vf
));
196 ci
->set_offset(ov_raw_tell(&vf
));
199 previous_section
= -1;
203 if (ci
->stop_codec
|| ci
->new_track
)
207 if (ov_time_seek(&vf
, ci
->seek_time
- 1)) {
208 //ci->logf("ov_time_seek failed");
213 /* Read host-endian signed 24-bit PCM samples */
214 n
= ov_read_fixed(&vf
, &pcm
, 1024, ¤t_section
);
216 /* Change DSP and buffer settings for this bitstream */
217 if (current_section
!= previous_section
) {
218 if (!vorbis_set_codec_parameters(&vf
)) {
222 previous_section
= current_section
;
229 DEBUGF("Vorbis: Error decoding frame\n");
231 ci
->pcmbuf_insert(pcm
[0], pcm
[1], n
);
232 ci
->set_offset(ov_raw_tell(&vf
));
233 ci
->set_elapsed(ov_time_tell(&vf
));
239 #if 0 /* defined(SIMULATOR) */
242 void* buf
= ci
->codec_get_buffer(&bufsize
);
244 DEBUGF("Vorbis: Memory max: %u\n", get_max_size(buf
));
248 if (ci
->request_next_track()) {
249 /* Clean things up for the next track */
250 vf
.dataoffsets
= NULL
;
253 vf
.pcmlengths
= NULL
;
259 ogg_malloc_destroy();