2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-07-31 00:43:21 +02:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2005-02-01 03:00:25 +01: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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-02-01 03:00:25 +01:00
|
|
|
*/
|
|
|
|
|
2009-01-08 21:37:02 +01:00
|
|
|
#include "config.h"
|
2013-07-28 13:04:12 +02:00
|
|
|
#include "MpcdecDecoderPlugin.hxx"
|
2013-07-28 13:18:48 +02:00
|
|
|
#include "DecoderAPI.hxx"
|
2013-09-05 00:06:31 +02:00
|
|
|
#include "InputStream.hxx"
|
2013-07-29 07:50:08 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/TagHandler.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
2013-10-15 22:04:17 +02:00
|
|
|
#include "util/Macros.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-03-27 19:51:59 +01:00
|
|
|
#include <mpc/mpcdec.h>
|
|
|
|
|
2009-11-10 19:01:38 +01:00
|
|
|
#include <assert.h>
|
2008-11-24 10:33:08 +01:00
|
|
|
#include <unistd.h>
|
2013-07-31 00:43:21 +02:00
|
|
|
#include <math.h>
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
struct mpc_decoder_data {
|
|
|
|
struct input_stream *is;
|
2008-08-26 08:27:07 +02:00
|
|
|
struct decoder *decoder;
|
2009-03-27 19:37:08 +01:00
|
|
|
};
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain mpcdec_domain("mpcdec");
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
static mpc_int32_t
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_read_cb(mpc_reader *reader, void *ptr, mpc_int32_t size)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-31 00:43:21 +02:00
|
|
|
struct mpc_decoder_data *data =
|
|
|
|
(struct mpc_decoder_data *)reader->data;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
return decoder_read(data->decoder, data->is, ptr, size);
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
static mpc_bool_t
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_seek_cb(mpc_reader *reader, mpc_int32_t offset)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-31 00:43:21 +02:00
|
|
|
struct mpc_decoder_data *data =
|
|
|
|
(struct mpc_decoder_data *)reader->data;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
return data->is->LockSeek(offset, SEEK_SET, IgnoreError());
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
static mpc_int32_t
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_tell_cb(mpc_reader *reader)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-31 00:43:21 +02:00
|
|
|
struct mpc_decoder_data *data =
|
|
|
|
(struct mpc_decoder_data *)reader->data;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
return (long)data->is->GetOffset();
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
static mpc_bool_t
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_canseek_cb(mpc_reader *reader)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-31 00:43:21 +02:00
|
|
|
struct mpc_decoder_data *data =
|
|
|
|
(struct mpc_decoder_data *)reader->data;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
return data->is->IsSeekable();
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
static mpc_int32_t
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_getsize_cb(mpc_reader *reader)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-31 00:43:21 +02:00
|
|
|
struct mpc_decoder_data *data =
|
|
|
|
(struct mpc_decoder_data *)reader->data;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
return data->is->GetSize();
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2006-07-15 15:42:57 +02:00
|
|
|
/* this _looks_ performance-critical, don't de-inline -- eric */
|
2009-03-27 19:37:08 +01:00
|
|
|
static inline int32_t
|
|
|
|
mpc_to_mpd_sample(MPC_SAMPLE_FORMAT sample)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2005-02-01 04:20:16 +01:00
|
|
|
/* only doing 16-bit audio for now */
|
2008-09-29 15:49:29 +02:00
|
|
|
int32_t val;
|
2005-02-01 04:20:16 +01:00
|
|
|
|
2008-10-30 08:44:51 +01:00
|
|
|
enum {
|
2008-10-30 08:45:00 +01:00
|
|
|
bits = 24,
|
2008-10-30 08:44:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const int clip_min = -1 << (bits - 1);
|
|
|
|
const int clip_max = (1 << (bits - 1)) - 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2005-02-01 04:20:16 +01:00
|
|
|
#ifdef MPC_FIXED_POINT
|
2008-10-30 08:44:51 +01:00
|
|
|
const int shift = bits - MPC_FIXED_POINT_SCALE_SHIFT;
|
2005-02-01 04:20:16 +01:00
|
|
|
|
2008-10-30 08:44:28 +01:00
|
|
|
if (shift < 0)
|
2010-03-17 17:54:21 +01:00
|
|
|
val = sample >> -shift;
|
2008-10-30 08:44:28 +01:00
|
|
|
else
|
|
|
|
val = sample << shift;
|
2005-02-01 04:20:16 +01:00
|
|
|
#else
|
2008-10-30 08:44:51 +01:00
|
|
|
const int float_scale = 1 << (bits - 1);
|
2005-02-01 04:20:16 +01:00
|
|
|
|
|
|
|
val = sample * float_scale;
|
|
|
|
#endif
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (val < clip_min)
|
|
|
|
val = clip_min;
|
|
|
|
else if (val > clip_max)
|
|
|
|
val = clip_max;
|
2005-02-01 04:20:16 +01:00
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2008-11-12 07:06:47 +01:00
|
|
|
static void
|
|
|
|
mpc_to_mpd_buffer(int32_t *dest, const MPC_SAMPLE_FORMAT *src,
|
|
|
|
unsigned num_samples)
|
|
|
|
{
|
|
|
|
while (num_samples-- > 0)
|
2009-03-27 19:37:08 +01:00
|
|
|
*dest++ = mpc_to_mpd_sample(*src++);
|
2008-11-12 07:06:47 +01:00
|
|
|
}
|
|
|
|
|
2008-11-11 17:13:44 +01:00
|
|
|
static void
|
2009-03-27 19:58:50 +01:00
|
|
|
mpcdec_decode(struct decoder *mpd_decoder, struct input_stream *is)
|
2005-02-01 03:00:25 +01:00
|
|
|
{
|
2005-02-01 04:20:16 +01:00
|
|
|
MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2013-07-31 00:43:21 +02:00
|
|
|
struct mpc_decoder_data data;
|
2009-03-27 19:37:08 +01:00
|
|
|
data.is = is;
|
2008-08-26 08:27:07 +02:00
|
|
|
data.decoder = mpd_decoder;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_reader reader;
|
2005-02-01 03:00:25 +01:00
|
|
|
reader.read = mpc_read_cb;
|
|
|
|
reader.seek = mpc_seek_cb;
|
|
|
|
reader.tell = mpc_tell_cb;
|
|
|
|
reader.get_size = mpc_getsize_cb;
|
|
|
|
reader.canseek = mpc_canseek_cb;
|
|
|
|
reader.data = &data;
|
|
|
|
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_demux *demux = mpc_demux_init(&reader);
|
2013-07-28 13:04:12 +02:00
|
|
|
if (demux == nullptr) {
|
2013-09-27 12:11:37 +02:00
|
|
|
if (decoder_get_command(mpd_decoder) != DecoderCommand::STOP)
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(mpcdec_domain,
|
|
|
|
"Not a valid musepack stream");
|
2009-03-27 19:51:59 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_streaminfo info;
|
2009-03-27 19:51:59 +01:00
|
|
|
mpc_demux_get_info(demux, &info);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat audio_format;
|
|
|
|
if (!audio_format_init_checked(audio_format, info.sample_freq,
|
|
|
|
SampleFormat::S24_P32,
|
2013-08-10 18:02:44 +02:00
|
|
|
info.channels, error)) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error);
|
2009-03-27 19:51:59 +01:00
|
|
|
mpc_demux_exit(demux);
|
2008-11-21 20:27:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-14 20:36:31 +01:00
|
|
|
struct replay_gain_info replay_gain_info;
|
|
|
|
replay_gain_info_init(&replay_gain_info);
|
2010-03-28 19:31:47 +02:00
|
|
|
replay_gain_info.tuples[REPLAY_GAIN_ALBUM].gain = MPC_OLD_GAIN_REF - (info.gain_album / 256.);
|
|
|
|
replay_gain_info.tuples[REPLAY_GAIN_ALBUM].peak = pow(10, info.peak_album / 256. / 20) / 32767;
|
|
|
|
replay_gain_info.tuples[REPLAY_GAIN_TRACK].gain = MPC_OLD_GAIN_REF - (info.gain_title / 256.);
|
|
|
|
replay_gain_info.tuples[REPLAY_GAIN_TRACK].peak = pow(10, info.peak_title / 256. / 20) / 32767;
|
2005-02-02 05:05:19 +01:00
|
|
|
|
2010-02-14 20:36:31 +01:00
|
|
|
decoder_replay_gain(mpd_decoder, &replay_gain_info);
|
2005-02-02 05:05:19 +01:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
decoder_initialized(mpd_decoder, audio_format,
|
2013-09-05 00:06:31 +02:00
|
|
|
is->IsSeekable(),
|
2008-08-26 08:27:05 +02:00
|
|
|
mpc_streaminfo_get_length(&info));
|
2005-02-01 06:26:37 +01:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand cmd = DecoderCommand::NONE;
|
2008-11-12 07:07:12 +01:00
|
|
|
do {
|
2013-09-27 12:11:37 +02:00
|
|
|
if (cmd == DecoderCommand::SEEK) {
|
2009-12-25 19:47:33 +01:00
|
|
|
mpc_int64_t where = decoder_seek_where(mpd_decoder) *
|
2008-10-10 14:40:54 +02:00
|
|
|
audio_format.sample_rate;
|
2009-12-25 19:47:33 +01:00
|
|
|
bool success;
|
2009-03-27 19:51:59 +01:00
|
|
|
|
2009-12-25 19:47:33 +01:00
|
|
|
success = mpc_demux_seek_sample(demux, where)
|
2009-03-27 19:51:59 +01:00
|
|
|
== MPC_STATUS_OK;
|
|
|
|
if (success)
|
2008-08-26 08:27:07 +02:00
|
|
|
decoder_command_finished(mpd_decoder);
|
2008-11-12 07:06:47 +01:00
|
|
|
else
|
2008-08-26 08:27:07 +02:00
|
|
|
decoder_seek_error(mpd_decoder);
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
2005-02-01 14:22:58 +01:00
|
|
|
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_uint32_t vbr_update_bits = 0;
|
2011-08-23 17:58:56 +02:00
|
|
|
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_frame_info frame;
|
2009-03-27 19:51:59 +01:00
|
|
|
frame.buffer = (MPC_SAMPLE_FORMAT *)sample_buffer;
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_status status = mpc_demux_decode(demux, &frame);
|
2009-03-27 19:51:59 +01:00
|
|
|
if (status != MPC_STATUS_OK) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(mpcdec_domain,
|
|
|
|
"Failed to decode sample");
|
2009-03-27 19:51:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame.bits == -1)
|
|
|
|
break;
|
|
|
|
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_uint32_t ret = frame.samples;
|
2008-11-12 07:07:40 +01:00
|
|
|
ret *= info.channels;
|
2005-02-01 04:20:16 +01:00
|
|
|
|
2013-10-15 22:04:17 +02:00
|
|
|
int32_t chunk[ARRAY_SIZE(sample_buffer)];
|
2008-11-12 07:06:47 +01:00
|
|
|
mpc_to_mpd_buffer(chunk, sample_buffer, ret);
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2013-07-31 00:43:21 +02:00
|
|
|
long bit_rate = vbr_update_bits * audio_format.sample_rate
|
2008-11-12 07:03:44 +01:00
|
|
|
/ 1152 / 1000;
|
2005-02-01 04:20:16 +01:00
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
cmd = decoder_data(mpd_decoder, is,
|
2008-11-12 07:07:12 +01:00
|
|
|
chunk, ret * sizeof(chunk[0]),
|
2010-01-03 22:44:23 +01:00
|
|
|
bit_rate);
|
2013-09-27 12:11:37 +02:00
|
|
|
} while (cmd != DecoderCommand::STOP);
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-03-27 19:51:59 +01:00
|
|
|
mpc_demux_exit(demux);
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
static float
|
2009-12-31 18:20:08 +01:00
|
|
|
mpcdec_get_file_duration(struct input_stream *is)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-03-27 19:37:08 +01:00
|
|
|
struct mpc_decoder_data data;
|
2009-12-31 18:20:08 +01:00
|
|
|
data.is = is;
|
2013-07-28 13:04:12 +02:00
|
|
|
data.decoder = nullptr;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_reader reader;
|
2005-03-07 01:51:21 +01:00
|
|
|
reader.read = mpc_read_cb;
|
|
|
|
reader.seek = mpc_seek_cb;
|
|
|
|
reader.tell = mpc_tell_cb;
|
|
|
|
reader.get_size = mpc_getsize_cb;
|
|
|
|
reader.canseek = mpc_canseek_cb;
|
|
|
|
reader.data = &data;
|
|
|
|
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_demux *demux = mpc_demux_init(&reader);
|
2013-07-28 13:04:12 +02:00
|
|
|
if (demux == nullptr)
|
2009-03-27 19:51:59 +01:00
|
|
|
return -1;
|
|
|
|
|
2013-07-31 00:43:21 +02:00
|
|
|
mpc_streaminfo info;
|
2009-03-27 19:51:59 +01:00
|
|
|
mpc_demux_get_info(demux, &info);
|
|
|
|
mpc_demux_exit(demux);
|
2005-03-07 01:51:21 +01:00
|
|
|
|
2013-07-31 00:43:21 +02:00
|
|
|
return mpc_streaminfo_get_length(&info);
|
2005-03-07 01:51:21 +01:00
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
|
|
|
mpcdec_scan_stream(struct input_stream *is,
|
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-12-31 18:20:08 +01:00
|
|
|
float total_time = mpcdec_get_file_duration(is);
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-12-31 18:20:08 +01:00
|
|
|
if (total_time < 0)
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_duration(handler, handler_ctx, total_time);
|
|
|
|
return true;
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2013-07-28 13:04:12 +02:00
|
|
|
static const char *const mpcdec_suffixes[] = { "mpc", nullptr };
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-02-17 08:48:20 +01:00
|
|
|
const struct decoder_plugin mpcdec_decoder_plugin = {
|
2013-07-28 13:04:12 +02:00
|
|
|
"mpcdec",
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
mpcdec_decode,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
mpcdec_scan_stream,
|
|
|
|
nullptr,
|
|
|
|
mpcdec_suffixes,
|
|
|
|
nullptr,
|
2005-02-01 03:00:25 +01:00
|
|
|
};
|