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-07-25 22:24:53 +00:00
|
|
|
static 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-06-21 18:45:34 +00:00
|
|
|
(void)reader;
|
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)
|
2010-06-23 06:25:18 +00:00
|
|
|
{
|
2010-06-21 18:45:34 +00:00
|
|
|
(void)reader;
|
2005-06-22 22:02:11 +00:00
|
|
|
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-06-21 18:45:34 +00:00
|
|
|
(void)reader;
|
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
|
|
|
(void)reader;
|
2010-06-21 18:45:34 +00:00
|
|
|
return ci->filesize;
|
2005-06-11 12:40:27 +00:00
|
|
|
}
|
|
|
|
|
2010-06-23 06:25:18 +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;
|
2011-02-20 15:27:10 +00:00
|
|
|
int retval;
|
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;
|
|
|
|
|
2011-02-20 15:27:10 +00:00
|
|
|
next_track:
|
|
|
|
retval = CODEC_OK;
|
|
|
|
|
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
|
|
|
|
2011-02-20 15:27:10 +00:00
|
|
|
if (codec_wait_taginfo() != 0)
|
|
|
|
goto done;
|
2008-02-24 19:12:15 +00:00
|
|
|
|
2010-06-23 06:25:18 +00:00
|
|
|
/* Initialize demux/decoder. */
|
2010-03-07 19:34:44 +00:00
|
|
|
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-06-23 06:25:18 +00:00
|
|
|
/* Read file's streaminfo data. */
|
2010-03-07 19:34:44 +00:00
|
|
|
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
|
|
|
|
2010-06-23 06:25:18 +00:00
|
|
|
/* Set up digital signal processing for correct number of channels */
|
2005-10-10 19:56:40 +00:00
|
|
|
/* 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. */
|
2010-06-23 06:25:18 +00:00
|
|
|
if (samplesdone > 0)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
}
|
2005-06-22 22:02:11 +00:00
|
|
|
|
|
|
|
/* This is the decoding loop. */
|
2010-06-23 06:25:18 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Complete seek handler. */
|
2010-03-07 19:34:44 +00:00
|
|
|
if (ci->seek_time)
|
|
|
|
{
|
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();
|
2005-11-06 19:12:27 +00:00
|
|
|
}
|
2010-06-23 06:25:18 +00:00
|
|
|
|
|
|
|
/* Stop or skip occured, exit decoding loop. */
|
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-06-23 06:25:18 +00:00
|
|
|
/* Decode one frame. */
|
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-06-23 06:25:18 +00:00
|
|
|
if (frame.bits == -1)
|
2010-03-07 19:34:44 +00:00
|
|
|
{
|
2010-06-23 06:25:18 +00:00
|
|
|
/* Decoding error, exit decoding loop. */
|
2010-03-07 19:34:44 +00:00
|
|
|
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
|
|
|
|
{
|
2010-06-23 06:25:18 +00:00
|
|
|
/* Decoding passed, insert samples to PCM buffer. */
|
2010-03-07 19:34:44 +00:00
|
|
|
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
|
|
|
}
|