2005-06-11 12:40:27 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Thom Johansen
|
|
|
|
*
|
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-11 12:40:27 +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"
|
2010-03-07 19:34:44 +00:00
|
|
|
#include <codecs/libmusepack/mpcdec.h>
|
|
|
|
#include <codecs/libmusepack/internal.h>
|
2005-06-11 12:40:27 +00:00
|
|
|
|
2006-01-18 00:05:14 +00:00
|
|
|
CODEC_HEADER
|
|
|
|
|
2010-03-07 19:34:44 +00:00
|
|
|
MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH] IBSS_ATTR;
|
2005-06-11 12:40:27 +00:00
|
|
|
|
2005-06-22 22:02:11 +00:00
|
|
|
/* Our implementations of the mpc_reader callback functions. */
|
2010-03-07 19:34:44 +00:00
|
|
|
static mpc_int32_t read_impl(mpc_reader *reader, void *ptr, mpc_int32_t size)
|
2005-06-11 12:40:27 +00:00
|
|
|
{
|
2010-03-07 19:34:44 +00:00
|
|
|
struct codec_api *ci = (struct codec_api *)(reader->data);
|
2005-06-22 22:02:11 +00:00
|
|
|
return ((mpc_int32_t)(ci->read_filebuf(ptr, size)));
|
2005-06-11 12:40:27 +00:00
|
|
|
}
|
|
|
|
|
2010-03-07 19:34:44 +00:00
|
|
|
static mpc_bool_t seek_impl(mpc_reader *reader, mpc_int32_t offset)
|
2005-06-11 12:40:27 +00:00
|
|
|
{
|
2010-03-07 19:34:44 +00:00
|
|
|
struct codec_api *ci = (struct codec_api *)(reader->data);
|
2005-06-11 12:40:27 +00:00
|
|
|
|
2005-06-22 22:02:11 +00:00
|
|
|
/* WARNING: assumes we don't need to skip too far into the past,
|
|
|
|
this might not be supported by the buffering layer yet */
|
|
|
|
return ci->seek_buffer(offset);
|
2005-06-11 12:40:27 +00:00
|
|
|
}
|
|
|
|
|
2010-03-07 19:34:44 +00:00
|
|
|
static mpc_int32_t tell_impl(mpc_reader *reader)
|
2005-06-11 12:40:27 +00:00
|
|
|
{
|
2010-03-07 19:34:44 +00:00
|
|
|
struct codec_api *ci = (struct codec_api *)(reader->data);
|
2005-06-11 12:40:27 +00:00
|
|
|
|
2005-06-22 22:02:11 +00:00
|
|
|
return ci->curpos;
|
2005-06-11 12:40:27 +00:00
|
|
|
}
|
|
|
|
|
2010-03-07 19:34:44 +00:00
|
|
|
static mpc_int32_t get_size_impl(mpc_reader *reader)
|
2005-06-11 12:40:27 +00:00
|
|
|
{
|
2010-03-07 19:34:44 +00:00
|
|
|
struct codec_api *ci = (struct codec_api *)(reader->data);
|
2005-06-22 22:02:11 +00:00
|
|
|
|
|
|
|
return ci->filesize;
|
2005-06-11 12:40:27 +00:00
|
|
|
}
|
|
|
|
|
2010-03-07 19:34:44 +00:00
|
|
|
static mpc_bool_t canseek_impl(mpc_reader *reader)
|
2005-06-11 12:40:27 +00:00
|
|
|
{
|
2010-03-07 19:34:44 +00:00
|
|
|
(void)reader;
|
2005-06-22 22:02:11 +00:00
|
|
|
|
|
|
|
/* doesn't much matter, libmusepack ignores this anyway */
|
|
|
|
return true;
|
2005-06-11 12:40:27 +00:00
|
|
|
}
|
|
|
|
|
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-11 12:40:27 +00:00
|
|
|
{
|
2005-10-10 19:56:40 +00:00
|
|
|
mpc_int64_t samplesdone;
|
2010-03-14 13:32:16 +00:00
|
|
|
uint32_t frequency; /* 0.1 kHz accuracy */
|
|
|
|
uint32_t elapsed_time; /* milliseconds */
|
|
|
|
uint32_t byterate; /* bytes per second */
|
2010-03-07 19:34:44 +00:00
|
|
|
mpc_status status;
|
2005-06-22 22:02:11 +00:00
|
|
|
mpc_reader reader;
|
2005-10-10 19:56:40 +00:00
|
|
|
mpc_streaminfo info;
|
2010-03-07 19:34:44 +00:00
|
|
|
mpc_frame_info frame;
|
|
|
|
mpc_demux *demux = NULL;
|
2006-04-25 20:00:53 +00:00
|
|
|
int retval = CODEC_OK;
|
2005-10-10 19:56:40 +00:00
|
|
|
|
2010-03-07 19:34:44 +00:00
|
|
|
frame.buffer = sample_buffer;
|
|
|
|
|
2008-06-10 06:11:50 +00:00
|
|
|
/* musepack's sample representation is 18.14
|
|
|
|
* DSP_SET_SAMPLE_DEPTH = 14 (FRACT) + 16 (NATIVE) - 1 (SIGN) = 29 */
|
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
|
2005-10-10 19:56:40 +00:00
|
|
|
|
2005-06-22 22:02:11 +00:00
|
|
|
/* Create a decoder instance */
|
2010-03-07 19:34:44 +00:00
|
|
|
reader.read = read_impl;
|
|
|
|
reader.seek = seek_impl;
|
|
|
|
reader.tell = tell_impl;
|
2005-06-22 22:02:11 +00:00
|
|
|
reader.get_size = get_size_impl;
|
2010-03-07 19:34:44 +00:00
|
|
|
reader.canseek = canseek_impl;
|
|
|
|
reader.data = ci;
|
2005-06-22 22:02:11 +00:00
|
|
|
|
2005-10-10 19:56:40 +00:00
|
|
|
next_track:
|
2010-03-07 19:34:44 +00:00
|
|
|
if (codec_init())
|
|
|
|
{
|
2006-01-18 20:22:03 +00:00
|
|
|
retval = CODEC_ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
2005-10-10 19:56:40 +00:00
|
|
|
|
2008-02-24 19:12:15 +00:00
|
|
|
while (!*ci->taginfo_ready && !ci->stop_codec)
|
|
|
|
ci->sleep(1);
|
|
|
|
|
2010-03-07 19:34:44 +00:00
|
|
|
/* initialize demux/decoder */
|
|
|
|
demux = mpc_demux_init(&reader);
|
|
|
|
if (NULL == demux)
|
|
|
|
{
|
2006-01-18 20:22:03 +00:00
|
|
|
retval = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-01-18 20:22:03 +00:00
|
|
|
}
|
2010-03-07 19:34:44 +00:00
|
|
|
/* read file's streaminfo data */
|
|
|
|
mpc_demux_get_info(demux, &info);
|
|
|
|
|
2010-03-14 13:32:16 +00:00
|
|
|
byterate = (mpc_uint32_t)(info.average_bitrate) / 8;
|
2009-04-20 19:16:48 +00:00
|
|
|
frequency = info.sample_freq / 100; /* 0.1 kHz accuracy */
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SWITCH_FREQUENCY, info.sample_freq);
|
2010-03-14 13:32:16 +00:00
|
|
|
|
|
|
|
/* Remark: rockbox offset is the file offset in bytes. So, estimate the
|
|
|
|
* sample seek position from the file offset, the sampling frequency and
|
|
|
|
* the bitrate. As the saved position is exactly calculated the reverse way
|
|
|
|
* there is no loss of information except rounding. */
|
|
|
|
samplesdone = 100 * ((mpc_uint64_t)(ci->id3->offset * frequency) / byterate);
|
2005-10-10 19:56:40 +00:00
|
|
|
|
|
|
|
/* set playback engine up for correct number of channels */
|
|
|
|
/* NOTE: current musepack format only allows for stereo files
|
|
|
|
but code is here to handle other configurations anyway */
|
2010-03-07 19:34:44 +00:00
|
|
|
if (info.channels == 2)
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
|
2005-10-10 19:56:40 +00:00
|
|
|
else if (info.channels == 1)
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
|
2010-03-07 19:34:44 +00:00
|
|
|
else
|
|
|
|
{
|
2006-01-18 20:22:03 +00:00
|
|
|
retval = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-01-18 20:22:03 +00:00
|
|
|
}
|
2005-10-10 19:56:40 +00:00
|
|
|
|
2005-11-04 21:35:08 +00:00
|
|
|
codec_set_replaygain(ci->id3);
|
2010-03-07 19:34:44 +00:00
|
|
|
|
2007-04-06 21:48:17 +00:00
|
|
|
/* Resume to saved sample offset. */
|
2009-04-20 19:16:48 +00:00
|
|
|
if (samplesdone > 0) {
|
2007-04-06 21:48:17 +00:00
|
|
|
/* hack to improve seek time if filebuf goes empty */
|
2010-03-07 19:34:44 +00:00
|
|
|
if (mpc_demux_seek_sample(demux, samplesdone) == MPC_STATUS_OK)
|
|
|
|
{
|
2009-04-20 19:16:48 +00:00
|
|
|
elapsed_time = (samplesdone*10)/frequency;
|
|
|
|
ci->set_elapsed(elapsed_time);
|
2010-03-07 19:34:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-04-06 21:48:17 +00:00
|
|
|
samplesdone = 0;
|
|
|
|
}
|
|
|
|
/* reset chunksize */
|
|
|
|
}
|
2005-06-22 22:02:11 +00:00
|
|
|
|
|
|
|
/* This is the decoding loop. */
|
2005-10-18 06:52:58 +00:00
|
|
|
do {
|
2006-08-31 18:18:57 +00:00
|
|
|
/* Complete seek handler. */
|
2010-03-07 19:34:44 +00:00
|
|
|
if (ci->seek_time)
|
|
|
|
{
|
2006-08-31 18:18:57 +00:00
|
|
|
/* hack to improve seek time if filebuf goes empty */
|
2009-04-20 19:16:48 +00:00
|
|
|
mpc_int64_t new_offset = ((ci->seek_time - 1)/10)*frequency;
|
2010-03-07 19:34:44 +00:00
|
|
|
if (mpc_demux_seek_sample(demux, new_offset) == MPC_STATUS_OK)
|
|
|
|
{
|
2005-11-06 19:12:27 +00:00
|
|
|
samplesdone = new_offset;
|
2005-10-10 19:56:40 +00:00
|
|
|
ci->set_elapsed(ci->seek_time);
|
|
|
|
}
|
|
|
|
ci->seek_complete();
|
2006-08-31 18:18:57 +00:00
|
|
|
/* reset chunksize */
|
2005-11-06 19:12:27 +00:00
|
|
|
}
|
2006-04-15 02:03:11 +00:00
|
|
|
if (ci->stop_codec || ci->new_track)
|
2005-06-22 22:02:11 +00:00
|
|
|
break;
|
|
|
|
|
2010-03-07 19:34:44 +00:00
|
|
|
status = mpc_demux_decode(demux, &frame);
|
2005-10-10 19:56:40 +00:00
|
|
|
ci->yield();
|
2010-03-07 19:34:44 +00:00
|
|
|
if (frame.bits == -1) /* decoding stopped */
|
|
|
|
{
|
|
|
|
retval = (status == MPC_STATUS_OK) ? CODEC_OK : CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2010-03-07 19:34:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ci->pcmbuf_insert(frame.buffer,
|
|
|
|
frame.buffer + MPC_FRAME_LENGTH,
|
|
|
|
frame.samples);
|
|
|
|
samplesdone += frame.samples;
|
2009-04-20 19:16:48 +00:00
|
|
|
elapsed_time = (samplesdone*10)/frequency;
|
|
|
|
ci->set_elapsed(elapsed_time);
|
2010-03-14 13:32:16 +00:00
|
|
|
/* Remark: rockbox offset is the file offset in bytes. So estimate
|
|
|
|
* this offset from the samples, sampling frequency and bitrate */
|
|
|
|
ci->set_offset( (samplesdone * byterate)/(frequency*100) );
|
2005-06-11 12:40:27 +00:00
|
|
|
}
|
2010-03-07 19:34:44 +00:00
|
|
|
} while (true);
|
2006-04-22 14:40:13 +00:00
|
|
|
|
|
|
|
done:
|
2005-06-22 22:02:11 +00:00
|
|
|
if (ci->request_next_track())
|
|
|
|
goto next_track;
|
2006-01-18 20:22:03 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
return retval;
|
2005-06-11 12:40:27 +00:00
|
|
|
}
|
|
|
|
|