2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2008-08-26 08:27:04 +02:00
|
|
|
#include "../decoder_api.h"
|
2009-01-08 21:37:02 +01:00
|
|
|
#include "config.h"
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2005-07-30 12:28:43 +02:00
|
|
|
#include <mpcdec/mpcdec.h>
|
2008-11-12 07:02:29 +01:00
|
|
|
#include <glib.h>
|
2008-11-24 10:33:08 +01:00
|
|
|
#include <unistd.h>
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-02-17 08:51:34 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "mpcdec"
|
|
|
|
|
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
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
static mpc_int32_t
|
|
|
|
mpc_read_cb(void *vdata, void *ptr, mpc_int32_t size)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-03-27 19:37:08 +01:00
|
|
|
struct mpc_decoder_data *data = (struct mpc_decoder_data *) vdata;
|
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
|
|
|
|
mpc_seek_cb(void *vdata, mpc_int32_t offset)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-03-27 19:37:08 +01:00
|
|
|
struct mpc_decoder_data *data = (struct mpc_decoder_data *) vdata;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
return input_stream_seek(data->is, offset, SEEK_SET);
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
static mpc_int32_t
|
|
|
|
mpc_tell_cb(void *vdata)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-03-27 19:37:08 +01:00
|
|
|
struct mpc_decoder_data *data = (struct mpc_decoder_data *) vdata;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
return (long)(data->is->offset);
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
static mpc_bool_t
|
|
|
|
mpc_canseek_cb(void *vdata)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-03-27 19:37:08 +01:00
|
|
|
struct mpc_decoder_data *data = (struct mpc_decoder_data *) vdata;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
return data->is->seekable;
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
static mpc_int32_t
|
|
|
|
mpc_getsize_cb(void *vdata)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-03-27 19:37:08 +01:00
|
|
|
struct mpc_decoder_data *data = (struct mpc_decoder_data *) vdata;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
return data->is->size;
|
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)
|
|
|
|
val = sample << -shift;
|
|
|
|
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:37:08 +01:00
|
|
|
mpc_decode(struct decoder *mpd_decoder, struct input_stream *is)
|
2005-02-01 03:00:25 +01:00
|
|
|
{
|
|
|
|
mpc_decoder decoder;
|
|
|
|
mpc_reader reader;
|
|
|
|
mpc_streaminfo info;
|
2008-09-07 19:19:55 +02:00
|
|
|
struct audio_format audio_format;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
struct mpc_decoder_data data;
|
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
|
|
|
|
2008-11-12 07:14:18 +01:00
|
|
|
mpc_uint32_t ret;
|
2008-11-12 07:03:44 +01:00
|
|
|
int32_t chunk[G_N_ELEMENTS(sample_buffer)];
|
2009-03-27 19:37:08 +01:00
|
|
|
long bit_rate = 0;
|
|
|
|
unsigned long sample_pos = 0;
|
|
|
|
mpc_uint32_t vbr_update_acc;
|
|
|
|
mpc_uint32_t vbr_update_bits;
|
2008-01-26 13:46:21 +01:00
|
|
|
float total_time;
|
2009-03-27 19:37:08 +01:00
|
|
|
struct replay_gain_info *replay_gain_info = NULL;
|
2008-11-12 07:07:12 +01:00
|
|
|
enum decoder_command cmd = DECODE_COMMAND_NONE;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
mpc_streaminfo_init(&info);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if ((ret = mpc_streaminfo_read(&info, &reader)) != ERROR_CODE_OK) {
|
2008-11-11 17:13:44 +01:00
|
|
|
if (decoder_get_command(mpd_decoder) != DECODE_COMMAND_STOP)
|
2008-11-12 07:02:29 +01:00
|
|
|
g_warning("Not a valid musepack stream\n");
|
2008-11-11 17:13:44 +01:00
|
|
|
return;
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mpc_decoder_setup(&decoder, &reader);
|
2005-02-01 14:22:58 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!mpc_decoder_initialize(&decoder, &info)) {
|
2008-11-11 17:13:44 +01:00
|
|
|
if (decoder_get_command(mpd_decoder) != DECODE_COMMAND_STOP)
|
2008-11-12 07:02:29 +01:00
|
|
|
g_warning("Not a valid musepack stream\n");
|
2008-11-11 17:13:44 +01:00
|
|
|
return;
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-10-30 08:45:00 +01:00
|
|
|
audio_format.bits = 24;
|
2008-08-26 08:27:05 +02:00
|
|
|
audio_format.channels = info.channels;
|
2008-10-10 14:40:54 +02:00
|
|
|
audio_format.sample_rate = info.sample_freq;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2008-11-21 20:27:30 +01:00
|
|
|
if (!audio_format_valid(&audio_format)) {
|
|
|
|
g_warning("Invalid audio format: %u:%u:%u\n",
|
|
|
|
audio_format.sample_rate,
|
|
|
|
audio_format.bits,
|
|
|
|
audio_format.channels);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
replay_gain_info = replay_gain_info_new();
|
|
|
|
replay_gain_info->tuples[REPLAY_GAIN_ALBUM].gain = info.gain_album * 0.01;
|
|
|
|
replay_gain_info->tuples[REPLAY_GAIN_ALBUM].peak = info.peak_album / 32767.0;
|
|
|
|
replay_gain_info->tuples[REPLAY_GAIN_TRACK].gain = info.gain_title * 0.01;
|
|
|
|
replay_gain_info->tuples[REPLAY_GAIN_TRACK].peak = info.peak_title / 32767.0;
|
2005-02-02 05:05:19 +01:00
|
|
|
|
2008-08-26 08:27:05 +02:00
|
|
|
decoder_initialized(mpd_decoder, &audio_format,
|
2009-03-27 19:37:08 +01:00
|
|
|
is->seekable,
|
2008-08-26 08:27:05 +02:00
|
|
|
mpc_streaminfo_get_length(&info));
|
2005-02-01 06:26:37 +01:00
|
|
|
|
2008-11-12 07:07:12 +01:00
|
|
|
do {
|
|
|
|
if (cmd == DECODE_COMMAND_SEEK) {
|
2009-03-27 19:37:08 +01:00
|
|
|
sample_pos = decoder_seek_where(mpd_decoder) *
|
2008-10-10 14:40:54 +02:00
|
|
|
audio_format.sample_rate;
|
2009-03-27 19:37:08 +01:00
|
|
|
if (mpc_decoder_seek_sample(&decoder, sample_pos))
|
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
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
vbr_update_acc = 0;
|
|
|
|
vbr_update_bits = 0;
|
2005-02-01 06:26:37 +01:00
|
|
|
ret = mpc_decoder_decode(&decoder, sample_buffer,
|
2009-03-27 19:37:08 +01:00
|
|
|
&vbr_update_acc, &vbr_update_bits);
|
2008-11-12 07:14:18 +01:00
|
|
|
if (ret == 0 || ret == (mpc_uint32_t)-1)
|
2005-02-01 03:00:25 +01:00
|
|
|
break;
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
sample_pos += ret;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2008-11-12 07:07:40 +01:00
|
|
|
ret *= info.channels;
|
2005-02-01 04:20:16 +01:00
|
|
|
|
2008-11-12 07:06:47 +01:00
|
|
|
mpc_to_mpd_buffer(chunk, sample_buffer, ret);
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
total_time = ((float)sample_pos) / audio_format.sample_rate;
|
|
|
|
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]),
|
|
|
|
total_time,
|
2009-03-27 19:37:08 +01:00
|
|
|
bit_rate, replay_gain_info);
|
2008-11-12 07:07:12 +01:00
|
|
|
} while (cmd != DECODE_COMMAND_STOP);
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
replay_gain_info_free(replay_gain_info);
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
static float
|
|
|
|
mpcdec_get_file_duration(const char *file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-03-27 19:37:08 +01:00
|
|
|
struct input_stream is;
|
2008-01-26 13:46:21 +01:00
|
|
|
float total_time = -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2005-03-07 01:51:21 +01:00
|
|
|
mpc_reader reader;
|
|
|
|
mpc_streaminfo info;
|
2009-03-27 19:37:08 +01:00
|
|
|
struct mpc_decoder_data data;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
data.is = &is;
|
2008-08-26 08:27:07 +02:00
|
|
|
data.decoder = NULL;
|
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;
|
|
|
|
|
|
|
|
mpc_streaminfo_init(&info);
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
if (!input_stream_open(&is, file))
|
2005-09-08 23:08:02 +02:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (mpc_streaminfo_read(&info, &reader) != ERROR_CODE_OK) {
|
2009-03-27 19:37:08 +01:00
|
|
|
input_stream_close(&is);
|
2005-03-07 01:51:21 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
total_time = mpc_streaminfo_get_length(&info);
|
2005-03-07 01:51:21 +01:00
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
input_stream_close(&is);
|
2005-03-07 01:51:21 +01:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
return total_time;
|
2005-03-07 01:51:21 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
static struct tag *
|
|
|
|
mpcdec_tag_dup(const char *file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-03-27 19:37:08 +01:00
|
|
|
float total_time = mpcdec_get_file_duration(file);
|
2009-01-17 13:23:42 +01:00
|
|
|
struct tag *tag;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
if (total_time < 0) {
|
2009-02-17 08:51:34 +01:00
|
|
|
g_debug("Failed to get duration of file: %s", file);
|
2006-07-20 18:02:40 +02:00
|
|
|
return NULL;
|
2005-09-08 23:08:02 +02:00
|
|
|
}
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-01-17 13:23:42 +01:00
|
|
|
tag = tag_new();
|
|
|
|
tag->time = total_time;
|
|
|
|
return tag;
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 19:37:08 +01:00
|
|
|
static const char *const mpcdec_suffixes[] = { "mpc", NULL };
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2009-02-17 08:48:20 +01:00
|
|
|
const struct decoder_plugin mpcdec_decoder_plugin = {
|
2008-09-29 15:55:17 +02:00
|
|
|
.name = "mpc",
|
|
|
|
.stream_decode = mpc_decode,
|
2009-03-27 19:37:08 +01:00
|
|
|
.tag_dup = mpcdec_tag_dup,
|
|
|
|
.suffixes = mpcdec_suffixes,
|
2005-02-01 03:00:25 +01:00
|
|
|
};
|