2005-06-22 19:41:30 +00:00
|
|
|
|
/***************************************************************************
|
|
|
|
|
* __________ __ ___.
|
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2002 Bj<EFBFBD>rn Stenberg
|
|
|
|
|
*
|
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
|
*
|
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
|
* KIND, either express or implied.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2005-10-13 11:32:52 +00:00
|
|
|
|
|
|
|
|
|
#include "codeclib.h"
|
2005-06-22 19:41:30 +00:00
|
|
|
|
#include "Tremor/ivorbisfile.h"
|
2005-07-05 08:43:36 +00:00
|
|
|
|
#include "Tremor/ogg.h"
|
2005-06-22 19:41:30 +00:00
|
|
|
|
|
2006-01-18 00:05:14 +00:00
|
|
|
|
CODEC_HEADER
|
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
|
/* Some standard functions and variables needed by Tremor */
|
|
|
|
|
|
|
|
|
|
size_t read_handler(void *ptr, size_t size, size_t nmemb, void *datasource)
|
|
|
|
|
{
|
2005-06-22 19:55:09 +00:00
|
|
|
|
(void)datasource;
|
2006-11-26 18:31:41 +00:00
|
|
|
|
return ci->read_filebuf(ptr, nmemb*size);
|
2005-06-22 19:41:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-10-10 20:19:09 +00:00
|
|
|
|
int initial_seek_handler(void *datasource, ogg_int64_t offset, int whence)
|
|
|
|
|
{
|
2005-06-22 19:41:30 +00:00
|
|
|
|
(void)datasource;
|
|
|
|
|
(void)offset;
|
|
|
|
|
(void)whence;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-05 08:43:36 +00:00
|
|
|
|
int seek_handler(void *datasource, ogg_int64_t offset, int whence)
|
|
|
|
|
{
|
|
|
|
|
(void)datasource;
|
|
|
|
|
|
2005-10-10 20:19:09 +00:00
|
|
|
|
if (whence == SEEK_CUR) {
|
2006-11-26 18:31:41 +00:00
|
|
|
|
offset += ci->curpos;
|
2005-10-10 20:19:09 +00:00
|
|
|
|
} else if (whence == SEEK_END) {
|
2006-11-26 18:31:41 +00:00
|
|
|
|
offset += ci->filesize;
|
2005-07-05 08:43:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-11-26 18:31:41 +00:00
|
|
|
|
if (ci->seek_buffer(offset)) {
|
2005-10-10 20:19:09 +00:00
|
|
|
|
return 0;
|
2005-07-05 08:43:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
|
int close_handler(void *datasource)
|
|
|
|
|
{
|
|
|
|
|
(void)datasource;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long tell_handler(void *datasource)
|
|
|
|
|
{
|
2005-06-22 19:55:09 +00:00
|
|
|
|
(void)datasource;
|
2006-11-26 18:31:41 +00:00
|
|
|
|
return ci->curpos;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-07-11 06:47:35 +00:00
|
|
|
|
/* This sets the DSP parameters based on the current logical bitstream
|
|
|
|
|
* (sampling rate, number of channels, etc). It also tries to guess
|
|
|
|
|
* reasonable buffer parameters based on the current quality setting.
|
|
|
|
|
*/
|
|
|
|
|
bool vorbis_set_codec_parameters(OggVorbis_File *vf)
|
|
|
|
|
{
|
2005-10-10 20:19:09 +00:00
|
|
|
|
vorbis_info *vi;
|
2005-07-11 06:47:35 +00:00
|
|
|
|
|
2005-10-10 20:19:09 +00:00
|
|
|
|
vi = ov_info(vf, -1);
|
2005-07-11 06:47:35 +00:00
|
|
|
|
|
2005-10-10 20:19:09 +00:00
|
|
|
|
if (vi == NULL) {
|
2006-11-26 18:31:41 +00:00
|
|
|
|
//ci->splash(HZ*2, true, "Vorbis Error");
|
2005-07-11 06:47:35 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-10 16:34:16 +00:00
|
|
|
|
ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
|
2006-11-26 18:31:41 +00:00
|
|
|
|
codec_set_replaygain(ci->id3);
|
2005-07-11 06:47:35 +00:00
|
|
|
|
|
|
|
|
|
if (vi->channels == 2) {
|
2007-02-10 16:34:16 +00:00
|
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
|
2005-07-11 06:47:35 +00:00
|
|
|
|
} else if (vi->channels == 1) {
|
2007-02-10 16:34:16 +00:00
|
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
|
2005-07-11 06:47:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
|
/* this is the codec entry point */
|
2006-11-26 18:31:41 +00:00
|
|
|
|
enum codec_status codec_main(void)
|
2005-06-22 19:41:30 +00:00
|
|
|
|
{
|
|
|
|
|
ov_callbacks callbacks;
|
|
|
|
|
OggVorbis_File vf;
|
2005-10-10 20:19:09 +00:00
|
|
|
|
ogg_int32_t **pcm;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
|
|
|
|
|
int error;
|
|
|
|
|
long n;
|
|
|
|
|
int current_section;
|
2005-07-11 06:47:35 +00:00
|
|
|
|
int previous_section = -1;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
int eof;
|
2005-07-05 08:43:36 +00:00
|
|
|
|
ogg_int64_t vf_offsets[2];
|
|
|
|
|
ogg_int64_t vf_dataoffsets;
|
|
|
|
|
ogg_uint32_t vf_serialnos;
|
|
|
|
|
ogg_int64_t vf_pcmlengths[2];
|
2005-06-22 19:41:30 +00:00
|
|
|
|
|
2007-02-10 16:34:16 +00:00
|
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 24);
|
2005-07-11 06:47:35 +00:00
|
|
|
|
/* Note: These are sane defaults for these values. Perhaps
|
|
|
|
|
* they should be set differently based on quality setting
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* The chunk size below is magic. If set any lower, resume
|
|
|
|
|
* doesn't work properly (ov_raw_seek() does the wrong thing).
|
|
|
|
|
*/
|
2007-02-10 16:34:16 +00:00
|
|
|
|
ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, 1024*256);
|
2005-07-11 06:47:35 +00:00
|
|
|
|
|
2005-06-26 19:41:29 +00:00
|
|
|
|
/* We need to flush reserver memory every track load. */
|
2005-10-10 20:19:09 +00:00
|
|
|
|
next_track:
|
2006-11-26 18:31:41 +00:00
|
|
|
|
if (codec_init()) {
|
2006-01-18 20:22:03 +00:00
|
|
|
|
error = CODEC_ERROR;
|
|
|
|
|
goto exit;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
}
|
2006-11-26 20:56:26 +00:00
|
|
|
|
ogg_malloc_init();
|
2005-06-22 19:41:30 +00:00
|
|
|
|
|
2006-11-26 18:31:41 +00:00
|
|
|
|
while (!*ci->taginfo_ready && !ci->stop_codec)
|
|
|
|
|
ci->sleep(1);
|
2005-07-11 06:47:35 +00:00
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
|
/* Create a decoder instance */
|
2005-10-10 20:19:09 +00:00
|
|
|
|
callbacks.read_func = read_handler;
|
|
|
|
|
callbacks.seek_func = initial_seek_handler;
|
|
|
|
|
callbacks.tell_func = tell_handler;
|
|
|
|
|
callbacks.close_func = close_handler;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
|
2005-07-05 08:43:36 +00:00
|
|
|
|
/* Open a non-seekable stream */
|
2006-11-26 18:31:41 +00:00
|
|
|
|
error = ov_open_callbacks(ci, &vf, NULL, 0, callbacks);
|
2005-06-22 19:41:30 +00:00
|
|
|
|
|
2005-07-05 08:43:36 +00:00
|
|
|
|
/* If the non-seekable open was successful, we need to supply the missing
|
|
|
|
|
* data to make it seekable. This is a hack, but it's reasonable since we
|
2005-07-11 06:47:35 +00:00
|
|
|
|
* don't want to run the whole file through the buffer before we start
|
2005-07-05 08:43:36 +00:00
|
|
|
|
* playing. Using Tremor's seekable open routine would cause us to do
|
|
|
|
|
* this, so we pretend not to be seekable at first. Then we fill in the
|
2006-11-26 18:31:41 +00:00
|
|
|
|
* missing fields of vf with 1) information in ci->id3, and 2) info
|
2005-07-05 08:43:36 +00:00
|
|
|
|
* obtained by Tremor in the above ov_open call.
|
|
|
|
|
*
|
|
|
|
|
* Note that this assumes there is only ONE logical Vorbis bitstream in our
|
|
|
|
|
* physical Ogg bitstream. This is verified in metadata.c, well before we
|
|
|
|
|
* get here.
|
|
|
|
|
*/
|
2005-10-10 20:19:09 +00:00
|
|
|
|
if (!error) {
|
2005-07-11 06:47:35 +00:00
|
|
|
|
vf.offsets = vf_offsets;
|
|
|
|
|
vf.dataoffsets = &vf_dataoffsets;
|
|
|
|
|
vf.serialnos = &vf_serialnos;
|
|
|
|
|
vf.pcmlengths = vf_pcmlengths;
|
|
|
|
|
|
|
|
|
|
vf.offsets[0] = 0;
|
2006-11-26 18:31:41 +00:00
|
|
|
|
vf.offsets[1] = ci->id3->filesize;
|
2005-07-11 06:47:35 +00:00
|
|
|
|
vf.dataoffsets[0] = vf.offset;
|
|
|
|
|
vf.pcmlengths[0] = 0;
|
2006-11-26 18:31:41 +00:00
|
|
|
|
vf.pcmlengths[1] = ci->id3->samples;
|
2005-07-11 06:47:35 +00:00
|
|
|
|
vf.serialnos[0] = vf.current_serialno;
|
2005-10-10 20:19:09 +00:00
|
|
|
|
vf.callbacks.seek_func = seek_handler;
|
2005-07-11 06:47:35 +00:00
|
|
|
|
vf.seekable = 1;
|
2006-11-26 18:31:41 +00:00
|
|
|
|
vf.end = ci->id3->filesize;
|
2005-07-11 06:47:35 +00:00
|
|
|
|
vf.ready_state = OPENED;
|
|
|
|
|
vf.links = 1;
|
2005-07-05 08:43:36 +00:00
|
|
|
|
} else {
|
2006-11-26 18:31:41 +00:00
|
|
|
|
//ci->logf("ov_open: %d", error);
|
2006-01-18 20:22:03 +00:00
|
|
|
|
error = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
|
goto done;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
}
|
2005-07-05 08:43:36 +00:00
|
|
|
|
|
2006-11-26 18:31:41 +00:00
|
|
|
|
if (ci->id3->offset) {
|
|
|
|
|
ci->advance_buffer(ci->id3->offset);
|
|
|
|
|
ov_raw_seek(&vf, ci->id3->offset);
|
|
|
|
|
ci->set_elapsed(ov_time_tell(&vf));
|
|
|
|
|
ci->set_offset(ov_raw_tell(&vf));
|
2005-07-05 08:43:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-10-10 20:19:09 +00:00
|
|
|
|
eof = 0;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
while (!eof) {
|
2006-11-26 18:31:41 +00:00
|
|
|
|
ci->yield();
|
|
|
|
|
if (ci->stop_codec || ci->new_track)
|
2005-10-10 20:19:09 +00:00
|
|
|
|
break;
|
2005-07-05 08:43:36 +00:00
|
|
|
|
|
2006-11-26 18:31:41 +00:00
|
|
|
|
if (ci->seek_time) {
|
|
|
|
|
if (ov_time_seek(&vf, ci->seek_time - 1)) {
|
|
|
|
|
//ci->logf("ov_time_seek failed");
|
2005-07-05 08:43:36 +00:00
|
|
|
|
}
|
2006-11-26 18:31:41 +00:00
|
|
|
|
ci->seek_complete();
|
2005-07-05 08:43:36 +00:00
|
|
|
|
}
|
2005-07-24 15:32:28 +00:00
|
|
|
|
|
|
|
|
|
/* Read host-endian signed 24-bit PCM samples */
|
2005-10-10 20:19:09 +00:00
|
|
|
|
n = ov_read_fixed(&vf, &pcm, 1024, ¤t_section);
|
2005-06-22 19:41:30 +00:00
|
|
|
|
|
2005-07-11 06:47:35 +00:00
|
|
|
|
/* Change DSP and buffer settings for this bitstream */
|
2005-10-10 20:19:09 +00:00
|
|
|
|
if (current_section != previous_section) {
|
2005-07-11 06:47:35 +00:00
|
|
|
|
if (!vorbis_set_codec_parameters(&vf)) {
|
2006-01-18 20:22:03 +00:00
|
|
|
|
error = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
|
goto done;
|
2005-07-11 06:47:35 +00:00
|
|
|
|
} else {
|
|
|
|
|
previous_section = current_section;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-10-10 20:19:09 +00:00
|
|
|
|
if (n == 0) {
|
|
|
|
|
eof = 1;
|
2005-07-05 08:43:36 +00:00
|
|
|
|
} else if (n < 0) {
|
2005-06-22 19:41:30 +00:00
|
|
|
|
DEBUGF("Error decoding frame\n");
|
|
|
|
|
} else {
|
2007-02-07 00:51:50 +00:00
|
|
|
|
ci->pcmbuf_insert(pcm[0], pcm[1], n);
|
2006-11-26 18:31:41 +00:00
|
|
|
|
ci->set_offset(ov_raw_tell(&vf));
|
|
|
|
|
ci->set_elapsed(ov_time_tell(&vf));
|
2005-06-22 19:41:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2006-04-22 14:40:13 +00:00
|
|
|
|
error = CODEC_OK;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
|
done:
|
2006-11-26 18:31:41 +00:00
|
|
|
|
if (ci->request_next_track()) {
|
2005-10-10 20:19:09 +00:00
|
|
|
|
/* Clean things up for the next track */
|
2005-07-05 08:43:36 +00:00
|
|
|
|
vf.dataoffsets = NULL;
|
|
|
|
|
vf.offsets = NULL;
|
|
|
|
|
vf.serialnos = NULL;
|
|
|
|
|
vf.pcmlengths = NULL;
|
2005-07-11 06:47:35 +00:00
|
|
|
|
ov_clear(&vf);
|
2005-10-10 20:19:09 +00:00
|
|
|
|
previous_section = -1;
|
2005-07-05 08:43:36 +00:00
|
|
|
|
goto next_track;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-18 20:22:03 +00:00
|
|
|
|
exit:
|
|
|
|
|
return error;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
}
|
2005-07-11 06:47:35 +00:00
|
|
|
|
|