2005-06-22 19:41:30 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-05-05 10:32:46 +00:00
|
|
|
* Copyright (C) 2002 Björn Stenberg
|
2005-06-22 19:41:30 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2005-06-22 19:41:30 +00:00
|
|
|
*
|
|
|
|
* 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"
|
2008-11-05 13:30:58 +00:00
|
|
|
#include "libtremor/ivorbisfile.h"
|
|
|
|
#include "libtremor/ogg.h"
|
2009-08-29 12:23:40 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
#include "lib/tlsf/src/tlsf.h"
|
|
|
|
#endif
|
2005-06-22 19:41:30 +00:00
|
|
|
|
2006-01-18 00:05:14 +00:00
|
|
|
CODEC_HEADER
|
|
|
|
|
2009-08-29 12:23:40 +00:00
|
|
|
#if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS)
|
2009-03-08 12:48:58 +00:00
|
|
|
#include <setjmp.h>
|
|
|
|
jmp_buf rb_jump_buf;
|
|
|
|
#endif
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
/* Some standard functions and variables needed by Tremor */
|
|
|
|
|
2008-08-12 22:01:55 +00:00
|
|
|
static size_t read_handler(void *ptr, size_t size, size_t nmemb, void *datasource)
|
2005-06-22 19:41:30 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2008-08-12 22:01:55 +00:00
|
|
|
static int initial_seek_handler(void *datasource, ogg_int64_t offset, int whence)
|
2005-10-10 20:19:09 +00:00
|
|
|
{
|
2005-06-22 19:41:30 +00:00
|
|
|
(void)datasource;
|
|
|
|
(void)offset;
|
|
|
|
(void)whence;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-12 22:01:55 +00:00
|
|
|
static int seek_handler(void *datasource, ogg_int64_t offset, int whence)
|
2005-07-05 08:43:36 +00:00
|
|
|
{
|
|
|
|
(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;
|
|
|
|
}
|
|
|
|
|
2008-08-12 22:01:55 +00:00
|
|
|
static int close_handler(void *datasource)
|
2005-06-22 19:41:30 +00:00
|
|
|
{
|
|
|
|
(void)datasource;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-12 22:01:55 +00:00
|
|
|
static long tell_handler(void *datasource)
|
2005-06-22 19:41:30 +00:00
|
|
|
{
|
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
|
2009-08-29 12:23:40 +00:00
|
|
|
* (sampling rate, number of channels, etc).
|
2005-07-11 06:47:35 +00:00
|
|
|
*/
|
2008-08-12 22:01:55 +00:00
|
|
|
static bool vorbis_set_codec_parameters(OggVorbis_File *vf)
|
2005-07-11 06:47:35 +00:00
|
|
|
{
|
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) {
|
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 */
|
2011-04-27 03:08:23 +00:00
|
|
|
enum codec_status codec_main(enum codec_entry_call_reason reason)
|
|
|
|
{
|
|
|
|
if (reason == CODEC_LOAD) {
|
|
|
|
if (codec_init())
|
|
|
|
return CODEC_ERROR;
|
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CODEC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is called for each file to process */
|
|
|
|
enum codec_status codec_run(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
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
int error = CODEC_ERROR;
|
2005-06-22 19:41:30 +00:00
|
|
|
long n;
|
|
|
|
int current_section;
|
2009-03-08 12:48:58 +00:00
|
|
|
int previous_section;
|
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];
|
2011-04-27 03:08:23 +00:00
|
|
|
intptr_t param;
|
2009-08-29 12:23:40 +00:00
|
|
|
|
|
|
|
#if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS)
|
|
|
|
if (setjmp(rb_jump_buf) != 0) {
|
2011-04-27 03:08:23 +00:00
|
|
|
/* malloc failed; finish with this track */
|
2009-03-08 12:48:58 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
#endif
|
2010-08-23 20:09:58 +00:00
|
|
|
ogg_malloc_init();
|
|
|
|
|
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
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->seek_buffer(0);
|
|
|
|
|
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) {
|
2010-02-07 18:38:47 +00:00
|
|
|
ogg_free(vf.offsets);
|
|
|
|
ogg_free(vf.dataoffsets);
|
|
|
|
ogg_free(vf.serialnos);
|
2009-08-29 12:23:40 +00:00
|
|
|
|
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 {
|
2010-12-15 21:28:52 +00:00
|
|
|
DEBUGF("Vorbis: ov_open failed: %d\n", 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) {
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->seek_buffer(ci->id3->offset);
|
2006-11-26 18:31:41 +00:00
|
|
|
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
|
|
|
}
|
2011-08-28 07:45:35 +00:00
|
|
|
else {
|
|
|
|
ci->set_elapsed(0);
|
|
|
|
}
|
2005-07-05 08:43:36 +00:00
|
|
|
|
2009-03-08 12:48:58 +00:00
|
|
|
previous_section = -1;
|
2005-10-10 20:19:09 +00:00
|
|
|
eof = 0;
|
2005-06-22 19:41:30 +00:00
|
|
|
while (!eof) {
|
2011-04-27 03:08:23 +00:00
|
|
|
enum codec_command_action action = ci->get_command(¶m);
|
|
|
|
|
|
|
|
if (action == CODEC_ACTION_HALT)
|
2005-10-10 20:19:09 +00:00
|
|
|
break;
|
2005-07-05 08:43:36 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_SEEK_TIME) {
|
|
|
|
if (ov_time_seek(&vf, param)) {
|
2006-11-26 18:31:41 +00:00
|
|
|
//ci->logf("ov_time_seek failed");
|
2005-07-05 08:43:36 +00:00
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
|
|
|
|
ci->set_elapsed(ov_time_tell(&vf));
|
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-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) {
|
2009-08-29 12:23:40 +00:00
|
|
|
DEBUGF("Vorbis: Error decoding frame\n");
|
2005-06-22 19:41:30 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
2011-02-20 15:27:10 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
error = CODEC_OK;
|
2006-04-22 14:40:13 +00:00
|
|
|
done:
|
2009-08-29 12:23:40 +00:00
|
|
|
#if 0 /* defined(SIMULATOR) */
|
|
|
|
{
|
|
|
|
size_t bufsize;
|
|
|
|
void* buf = ci->codec_get_buffer(&bufsize);
|
|
|
|
|
2011-10-08 10:18:04 +00:00
|
|
|
DEBUGF("Vorbis: Memory max: %zu\n", get_max_size(buf));
|
2009-08-29 12:23:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
2010-08-23 20:09:58 +00:00
|
|
|
ogg_malloc_destroy();
|
2009-08-29 12:23:40 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Clean things up for the next track */
|
|
|
|
vf.dataoffsets = NULL;
|
|
|
|
vf.offsets = NULL;
|
|
|
|
vf.serialnos = NULL;
|
|
|
|
vf.pcmlengths = NULL;
|
|
|
|
ov_clear(&vf);
|
|
|
|
|
2006-01-18 20:22:03 +00:00
|
|
|
return error;
|
2005-06-22 19:41:30 +00:00
|
|
|
}
|