2005-06-11 12:40:27 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Thom Johansen
|
|
|
|
*
|
|
|
|
* 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-11 12:40:27 +00:00
|
|
|
#include <codecs/libmusepack/musepack.h>
|
|
|
|
|
2006-01-18 00:05:14 +00:00
|
|
|
CODEC_HEADER
|
|
|
|
|
2006-08-31 18:18:57 +00:00
|
|
|
mpc_decoder decoder 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. */
|
|
|
|
mpc_int32_t read_impl(void *data, void *ptr, mpc_int32_t size)
|
2005-06-11 12:40:27 +00:00
|
|
|
{
|
2005-10-10 19:56:40 +00:00
|
|
|
struct codec_api *ci = (struct codec_api *)data;
|
2005-06-11 12:40:27 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-01-30 01:04:43 +00:00
|
|
|
mpc_bool_t seek_impl(void *data, mpc_int32_t offset)
|
2005-06-11 12:40:27 +00:00
|
|
|
{
|
2005-10-10 19:56:40 +00:00
|
|
|
struct codec_api *ci = (struct codec_api *)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
|
|
|
}
|
|
|
|
|
2005-06-22 22:02:11 +00:00
|
|
|
mpc_int32_t tell_impl(void *data)
|
2005-06-11 12:40:27 +00:00
|
|
|
{
|
2005-10-10 19:56:40 +00:00
|
|
|
struct codec_api *ci = (struct codec_api *)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
|
|
|
}
|
|
|
|
|
2005-06-22 22:02:11 +00:00
|
|
|
mpc_int32_t get_size_impl(void *data)
|
2005-06-11 12:40:27 +00:00
|
|
|
{
|
2005-10-10 19:56:40 +00:00
|
|
|
struct codec_api *ci = (struct codec_api *)data;
|
2005-06-22 22:02:11 +00:00
|
|
|
|
|
|
|
return ci->filesize;
|
2005-06-11 12:40:27 +00:00
|
|
|
}
|
|
|
|
|
2006-01-30 01:04:43 +00:00
|
|
|
mpc_bool_t canseek_impl(void *data)
|
2005-06-11 12:40:27 +00:00
|
|
|
{
|
|
|
|
(void)data;
|
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
|
|
|
}
|
|
|
|
|
2006-11-09 21:59:27 +00:00
|
|
|
MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH]
|
2007-08-30 11:41:05 +00:00
|
|
|
IBSS_ATTR_MPC_SAMPLE_BUF;
|
|
|
|
mpc_uint32_t seek_table[10000];
|
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;
|
2005-06-22 22:02:11 +00:00
|
|
|
unsigned long frequency;
|
2005-10-18 06:52:58 +00:00
|
|
|
unsigned status;
|
2005-06-22 22:02:11 +00:00
|
|
|
mpc_reader reader;
|
2005-10-10 19:56:40 +00:00
|
|
|
mpc_streaminfo info;
|
2006-04-25 20:00:53 +00:00
|
|
|
int retval = CODEC_OK;
|
2005-10-10 19:56:40 +00:00
|
|
|
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
|
2005-10-10 19:56:40 +00:00
|
|
|
|
2005-06-22 22:02:11 +00:00
|
|
|
/* Create a decoder instance */
|
|
|
|
reader.read = read_impl;
|
|
|
|
reader.seek = seek_impl;
|
|
|
|
reader.tell = tell_impl;
|
|
|
|
reader.get_size = get_size_impl;
|
|
|
|
reader.canseek = canseek_impl;
|
|
|
|
reader.data = ci;
|
|
|
|
|
2006-08-31 18:18:57 +00:00
|
|
|
/* Ensure that SeekTable is clear since decoder is reused */
|
|
|
|
decoder.SeekTable = NULL;
|
|
|
|
|
|
|
|
mpc_decoder_set_seek_table(&decoder, seek_table, sizeof(seek_table));
|
|
|
|
|
2005-10-10 19:56:40 +00:00
|
|
|
next_track:
|
2006-11-26 18:31:41 +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);
|
|
|
|
|
2007-04-06 21:48:17 +00:00
|
|
|
samplesdone = ci->id3->offset;
|
|
|
|
|
2005-06-22 22:02:11 +00:00
|
|
|
/* read file's streaminfo data */
|
|
|
|
mpc_streaminfo_init(&info);
|
2006-01-18 20:22:03 +00:00
|
|
|
if (mpc_streaminfo_read(&info, &reader) != ERROR_CODE_OK) {
|
|
|
|
retval = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-01-18 20:22:03 +00:00
|
|
|
}
|
2006-08-31 18:18:57 +00:00
|
|
|
frequency = info.sample_freq / 1000;
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SWITCH_FREQUENCY, info.sample_freq);
|
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 */
|
|
|
|
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);
|
2006-01-18 20:22:03 +00:00
|
|
|
else {
|
|
|
|
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);
|
2005-06-22 22:02:11 +00:00
|
|
|
/* instantiate a decoder with our file reader */
|
|
|
|
mpc_decoder_setup(&decoder, &reader);
|
2006-01-18 20:22:03 +00:00
|
|
|
if (!mpc_decoder_initialize(&decoder, &info)) {
|
|
|
|
retval = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-01-18 20:22:03 +00:00
|
|
|
}
|
2007-04-06 21:48:17 +00:00
|
|
|
|
|
|
|
/* Resume to saved sample offset. */
|
|
|
|
if(samplesdone > 0) {
|
|
|
|
/* hack to improve seek time if filebuf goes empty */
|
|
|
|
if (mpc_decoder_seek_sample(&decoder, samplesdone)) {
|
|
|
|
ci->set_elapsed(samplesdone/frequency);
|
|
|
|
} else {
|
|
|
|
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
|
|
|
#if 1
|
|
|
|
/* Complete seek handler. */
|
2005-10-10 19:56:40 +00:00
|
|
|
if (ci->seek_time) {
|
2006-08-31 18:18:57 +00:00
|
|
|
/* hack to improve seek time if filebuf goes empty */
|
|
|
|
mpc_int64_t new_offset = (ci->seek_time - 1)*frequency;
|
2005-11-06 19:12:27 +00:00
|
|
|
if (mpc_decoder_seek_sample(&decoder, new_offset)) {
|
|
|
|
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-10-10 19:56:40 +00:00
|
|
|
}
|
2005-11-06 19:12:27 +00:00
|
|
|
#else
|
2006-08-31 18:18:57 +00:00
|
|
|
/* Seek to start of track handler. */
|
2006-04-25 20:05:01 +00:00
|
|
|
if (ci->seek_time) {
|
|
|
|
if (ci->seek_time == 1 && mpc_decoder_seek_sample(&decoder, 0)) {
|
2005-11-06 19:12:27 +00:00
|
|
|
samplesdone = 0;
|
|
|
|
ci->set_elapsed(0);
|
|
|
|
}
|
|
|
|
ci->seek_complete();
|
|
|
|
}
|
|
|
|
#endif
|
2006-04-15 02:03:11 +00:00
|
|
|
if (ci->stop_codec || ci->new_track)
|
2005-06-22 22:02:11 +00:00
|
|
|
break;
|
|
|
|
|
2005-11-06 23:05:49 +00:00
|
|
|
status = mpc_decoder_decode(&decoder, sample_buffer, NULL, NULL);
|
2005-10-10 19:56:40 +00:00
|
|
|
ci->yield();
|
2006-04-25 19:52:08 +00:00
|
|
|
if (status == 0) /* end of file reached */
|
|
|
|
goto done;
|
2005-10-10 19:56:40 +00:00
|
|
|
if (status == (unsigned)(-1)) { /* decode error */
|
2006-01-18 20:22:03 +00:00
|
|
|
retval = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2005-10-10 19:56:40 +00:00
|
|
|
} else {
|
2007-02-07 00:51:50 +00:00
|
|
|
ci->pcmbuf_insert(sample_buffer,
|
|
|
|
sample_buffer + MPC_FRAME_LENGTH,
|
|
|
|
status);
|
2005-10-10 19:56:40 +00:00
|
|
|
samplesdone += status;
|
2006-08-31 18:18:57 +00:00
|
|
|
ci->set_elapsed(samplesdone/frequency);
|
2007-04-06 21:48:17 +00:00
|
|
|
ci->set_offset(samplesdone);
|
2005-06-11 12:40:27 +00:00
|
|
|
}
|
2005-11-06 19:12:27 +00:00
|
|
|
} while (status != 0);
|
2006-04-22 14:40:13 +00:00
|
|
|
retval = CODEC_OK;
|
|
|
|
|
|
|
|
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:
|
2006-08-31 18:18:57 +00:00
|
|
|
mpc_decoder_destroy(&decoder);
|
2006-01-18 20:22:03 +00:00
|
|
|
return retval;
|
2005-06-11 12:40:27 +00:00
|
|
|
}
|
|
|
|
|