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.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
#include "kernel.h"
|
|
|
|
|
#include "codecs.h"
|
|
|
|
|
|
|
|
|
|
#include "Tremor/ivorbisfile.h"
|
2005-07-05 08:43:36 +00:00
|
|
|
|
#include "Tremor/ogg.h"
|
2005-06-22 19:41:30 +00:00
|
|
|
|
#include "playback.h"
|
2005-06-26 19:41:29 +00:00
|
|
|
|
#include "dsp.h"
|
2005-06-22 19:41:30 +00:00
|
|
|
|
#include "lib/codeclib.h"
|
|
|
|
|
|
2005-07-11 06:47:35 +00:00
|
|
|
|
#define TEST_RESUME
|
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
|
static struct codec_api* rb;
|
|
|
|
|
|
|
|
|
|
/* Some standard functions and variables needed by Tremor */
|
|
|
|
|
|
|
|
|
|
int errno;
|
|
|
|
|
|
|
|
|
|
size_t strlen(const char *s)
|
|
|
|
|
{
|
|
|
|
|
return(rb->strlen(s));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *strcpy(char *dest, const char *src)
|
|
|
|
|
{
|
|
|
|
|
return(rb->strcpy(dest,src));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *strcat(char *dest, const char *src)
|
|
|
|
|
{
|
|
|
|
|
return(rb->strcat(dest,src));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t read_handler(void *ptr, size_t size, size_t nmemb, void *datasource)
|
|
|
|
|
{
|
2005-06-22 19:55:09 +00:00
|
|
|
|
(void)datasource;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
return rb->read_filebuf(ptr, nmemb*size);
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-05 08:43:36 +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;
|
|
|
|
|
|
|
|
|
|
if ( whence == SEEK_CUR ) {
|
|
|
|
|
offset += rb->curpos;
|
|
|
|
|
} else if ( whence == SEEK_END ) {
|
|
|
|
|
offset += rb->filesize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rb->seek_buffer(offset)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
return rb->curpos;
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
vorbis_info* vi;
|
|
|
|
|
|
|
|
|
|
vi=ov_info(vf,-1);
|
|
|
|
|
|
|
|
|
|
if (vi==NULL) {
|
|
|
|
|
//rb->splash(HZ*2, true, "Vorbis Error");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rb->configure(DSP_SET_FREQUENCY, (int *)rb->id3->frequency);
|
2005-07-24 15:32:28 +00:00
|
|
|
|
codec_set_replaygain(rb->id3);
|
2005-07-11 06:47:35 +00:00
|
|
|
|
|
|
|
|
|
if (vi->channels == 2) {
|
2005-07-24 15:32:28 +00:00
|
|
|
|
rb->configure(DSP_SET_STEREO_MODE, (int *)STEREO_NONINTERLEAVED);
|
2005-07-11 06:47:35 +00:00
|
|
|
|
} else if (vi->channels == 1) {
|
|
|
|
|
rb->configure(DSP_SET_STEREO_MODE, (int *)STEREO_MONO);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
|
#ifdef USE_IRAM
|
|
|
|
|
extern char iramcopy[];
|
|
|
|
|
extern char iramstart[];
|
|
|
|
|
extern char iramend[];
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* this is the codec entry point */
|
2005-06-22 19:55:09 +00:00
|
|
|
|
enum codec_status codec_start(struct codec_api* api)
|
2005-06-22 19:41:30 +00:00
|
|
|
|
{
|
|
|
|
|
ov_callbacks callbacks;
|
|
|
|
|
OggVorbis_File vf;
|
2005-07-24 15:32:28 +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
|
|
|
|
|
|
|
|
|
TEST_CODEC_API(api);
|
|
|
|
|
|
|
|
|
|
/* if you are using a global api pointer, don't forget to copy it!
|
2005-07-05 08:43:36 +00:00
|
|
|
|
otherwise you will get lovely "I04: IllInstr" errors... :-) */
|
2005-06-22 19:41:30 +00:00
|
|
|
|
rb = api;
|
|
|
|
|
|
2005-07-05 08:43:36 +00:00
|
|
|
|
#ifdef USE_IRAM
|
2005-06-22 19:41:30 +00:00
|
|
|
|
rb->memcpy(iramstart, iramcopy, iramend-iramstart);
|
2005-07-05 08:43:36 +00:00
|
|
|
|
#endif
|
2005-07-11 06:47:35 +00:00
|
|
|
|
|
2005-07-24 15:32:28 +00:00
|
|
|
|
rb->configure(CODEC_DSP_ENABLE, (bool *)true);
|
|
|
|
|
rb->configure(DSP_DITHER, (bool *)false);
|
|
|
|
|
rb->configure(DSP_SET_SAMPLE_DEPTH, (long *) (24));
|
|
|
|
|
rb->configure(DSP_SET_CLIP_MAX, (long *) ((1 << 24) - 1));
|
|
|
|
|
rb->configure(DSP_SET_CLIP_MIN, (long *) -((1 << 24) - 1));
|
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
|
|
|
|
|
*/
|
|
|
|
|
rb->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*2));
|
|
|
|
|
|
|
|
|
|
/* The chunk size below is magic. If set any lower, resume
|
|
|
|
|
* doesn't work properly (ov_raw_seek() does the wrong thing).
|
|
|
|
|
*/
|
|
|
|
|
rb->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*256));
|
|
|
|
|
|
2005-06-26 19:41:29 +00:00
|
|
|
|
|
|
|
|
|
/* We need to flush reserver memory every track load. */
|
2005-06-22 19:41:30 +00:00
|
|
|
|
next_track:
|
|
|
|
|
if (codec_init(rb)) {
|
|
|
|
|
return CODEC_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-10 20:37:36 +00:00
|
|
|
|
while (!*rb->taginfo_ready && !rb->stop_codec)
|
2005-07-11 18:47:47 +00:00
|
|
|
|
rb->sleep(1);
|
2005-07-11 06:47:35 +00:00
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
|
/* Create a decoder instance */
|
|
|
|
|
callbacks.read_func=read_handler;
|
2005-07-05 08:43:36 +00:00
|
|
|
|
callbacks.seek_func=initial_seek_handler;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
callbacks.tell_func=tell_handler;
|
|
|
|
|
callbacks.close_func=close_handler;
|
|
|
|
|
|
2005-07-05 08:43:36 +00:00
|
|
|
|
/* Open a non-seekable stream */
|
2005-06-22 19:41:30 +00:00
|
|
|
|
error=ov_open_callbacks(rb,&vf,NULL,0,callbacks);
|
|
|
|
|
|
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
|
|
|
|
|
* missing fields of vf with 1) information in rb->id3, and 2) info
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
vf.offsets[1] = rb->id3->filesize;
|
|
|
|
|
vf.dataoffsets[0] = vf.offset;
|
|
|
|
|
vf.pcmlengths[0] = 0;
|
|
|
|
|
vf.pcmlengths[1] = rb->id3->samples;
|
|
|
|
|
vf.serialnos[0] = vf.current_serialno;
|
|
|
|
|
vf.callbacks.seek_func=seek_handler;
|
|
|
|
|
vf.seekable = 1;
|
|
|
|
|
vf.end = rb->id3->filesize;
|
|
|
|
|
vf.ready_state = OPENED;
|
|
|
|
|
vf.links = 1;
|
2005-07-05 08:43:36 +00:00
|
|
|
|
} else {
|
2005-07-11 06:47:35 +00:00
|
|
|
|
//rb->logf("ov_open: %d", error);
|
|
|
|
|
return CODEC_ERROR;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
}
|
2005-07-05 08:43:36 +00:00
|
|
|
|
|
2005-07-11 06:47:35 +00:00
|
|
|
|
if ( rb->id3->offset ) {
|
|
|
|
|
rb->advance_buffer(rb->id3->offset);
|
|
|
|
|
ov_raw_seek(&vf,rb->id3->offset);
|
|
|
|
|
rb->set_elapsed(ov_time_tell(&vf));
|
2005-07-12 16:45:38 +00:00
|
|
|
|
rb->set_offset(ov_raw_tell(&vf));
|
2005-07-05 08:43:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
|
eof=0;
|
|
|
|
|
while (!eof) {
|
2005-07-14 03:58:22 +00:00
|
|
|
|
rb->yield();
|
2005-07-05 08:43:36 +00:00
|
|
|
|
if (rb->stop_codec || rb->reload_codec)
|
|
|
|
|
break ;
|
|
|
|
|
|
|
|
|
|
if (rb->seek_time) {
|
2005-07-11 06:47:35 +00:00
|
|
|
|
|
2005-07-05 08:43:36 +00:00
|
|
|
|
if (ov_time_seek(&vf, rb->seek_time)) {
|
2005-07-11 06:47:35 +00:00
|
|
|
|
//rb->logf("ov_time_seek failed");
|
2005-07-05 08:43:36 +00:00
|
|
|
|
}
|
|
|
|
|
rb->seek_time = 0;
|
|
|
|
|
}
|
2005-07-24 15:32:28 +00:00
|
|
|
|
|
|
|
|
|
/* Read host-endian signed 24-bit PCM samples */
|
|
|
|
|
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 */
|
|
|
|
|
if ( current_section != previous_section ) {
|
|
|
|
|
if (!vorbis_set_codec_parameters(&vf)) {
|
|
|
|
|
return CODEC_ERROR;
|
|
|
|
|
} else {
|
|
|
|
|
previous_section = current_section;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-22 19:41:30 +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 {
|
2005-07-24 15:32:28 +00:00
|
|
|
|
while (!rb->pcmbuf_insert_split(pcm[0], pcm[1],
|
|
|
|
|
n * sizeof(ogg_int32_t))) {
|
2005-07-11 18:47:47 +00:00
|
|
|
|
rb->sleep(1);
|
2005-07-24 15:32:28 +00:00
|
|
|
|
}
|
2005-07-14 03:58:22 +00:00
|
|
|
|
|
|
|
|
|
rb->set_offset(ov_raw_tell(&vf));
|
|
|
|
|
rb->set_elapsed(ov_time_tell(&vf));
|
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-05 08:43:36 +00:00
|
|
|
|
if (rb->request_next_track()) {
|
2005-07-11 06:47:35 +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-07-16 00:10:42 +00:00
|
|
|
|
previous_section = -1;
|
2005-07-05 08:43:36 +00:00
|
|
|
|
goto next_track;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
|
return CODEC_OK;
|
|
|
|
|
}
|
2005-07-11 06:47:35 +00:00
|
|
|
|
|