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
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* this is the codec entry point */
|
|
|
|
enum codec_status codec_main(enum codec_entry_call_reason reason)
|
|
|
|
{
|
|
|
|
if (reason == CODEC_LOAD) {
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CODEC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is called for each file to process */
|
|
|
|
enum codec_status codec_run(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-04-27 03:08:23 +00:00
|
|
|
intptr_t param;
|
2005-10-10 19:56:40 +00:00
|
|
|
|
2010-03-07 19:34:44 +00:00
|
|
|
frame.buffer = sample_buffer;
|
2011-04-27 03:08:23 +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
|
|
|
if (codec_init())
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2005-10-10 19:56:40 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Prep position */
|
|
|
|
ci->seek_buffer(0);
|
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)
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
|
|
|
|
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 */
|
2013-05-05 04:48:40 +00:00
|
|
|
ci->configure(DSP_SET_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. */
|
2013-07-14 11:59:39 +00:00
|
|
|
elapsed_time = ci->id3->elapsed;
|
2011-07-23 20:56:53 +00:00
|
|
|
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);
|
2011-04-27 03:08:23 +00:00
|
|
|
else
|
|
|
|
return CODEC_ERROR;
|
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
|
|
|
|
2013-07-14 11:59:39 +00:00
|
|
|
if (samplesdone > 0 || elapsed_time)
|
2010-06-23 06:25:18 +00:00
|
|
|
{
|
2013-07-14 11:59:39 +00:00
|
|
|
mpc_int64_t new_offset = samplesdone;
|
|
|
|
|
|
|
|
if (new_offset <= 0)
|
|
|
|
new_offset = (elapsed_time/10)*frequency; /* by time */
|
|
|
|
|
|
|
|
/* Resume to sample offset. */
|
|
|
|
if (mpc_demux_seek_sample(demux, new_offset) == MPC_STATUS_OK)
|
2010-03-07 19:34:44 +00:00
|
|
|
{
|
2013-07-14 11:59:39 +00:00
|
|
|
samplesdone = new_offset;
|
2010-03-07 19:34:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-04-06 21:48:17 +00:00
|
|
|
samplesdone = 0;
|
|
|
|
}
|
2013-07-14 11:59:39 +00:00
|
|
|
|
|
|
|
elapsed_time = (samplesdone*10)/frequency;
|
2007-04-06 21:48:17 +00:00
|
|
|
}
|
2005-06-22 22:02:11 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
ci->set_elapsed(elapsed_time);
|
|
|
|
|
2005-06-22 22:02:11 +00:00
|
|
|
/* This is the decoding loop. */
|
2010-06-23 06:25:18 +00:00
|
|
|
do
|
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
enum codec_command_action action = ci->get_command(¶m);
|
|
|
|
|
|
|
|
if (action == CODEC_ACTION_HALT)
|
|
|
|
return CODEC_OK;
|
|
|
|
|
2010-06-23 06:25:18 +00:00
|
|
|
/* Complete seek handler. */
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_SEEK_TIME)
|
2010-03-07 19:34:44 +00:00
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
mpc_int64_t new_offset = (param/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
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
|
|
|
|
elapsed_time = (samplesdone*10)/frequency;
|
|
|
|
ci->set_elapsed(elapsed_time);
|
2005-10-10 19:56:40 +00:00
|
|
|
ci->seek_complete();
|
2005-11-06 19:12:27 +00:00
|
|
|
}
|
2005-06-22 22:02:11 +00:00
|
|
|
|
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. */
|
2011-04-27 03:08:23 +00:00
|
|
|
return (status == MPC_STATUS_OK) ? CODEC_OK : CODEC_ERROR;
|
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);
|
2005-06-11 12:40:27 +00:00
|
|
|
}
|