2005-02-01 03:00:25 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2005-02-01 03:00:25 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
typedef struct _MpcCallbackData {
|
2008-10-26 19:54:57 +01:00
|
|
|
struct input_stream *inStream;
|
2008-08-26 08:27:07 +02:00
|
|
|
struct decoder *decoder;
|
2005-02-01 03:00:25 +01:00
|
|
|
} MpcCallbackData;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static mpc_int32_t mpc_read_cb(void *vdata, void *ptr, mpc_int32_t size)
|
|
|
|
{
|
|
|
|
MpcCallbackData *data = (MpcCallbackData *) vdata;
|
|
|
|
|
2008-08-26 08:27:14 +02:00
|
|
|
return decoder_read(data->decoder, data->inStream, ptr, size);
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static mpc_bool_t mpc_seek_cb(void *vdata, mpc_int32_t offset)
|
|
|
|
{
|
|
|
|
MpcCallbackData *data = (MpcCallbackData *) vdata;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2008-10-30 08:42:18 +01:00
|
|
|
return input_stream_seek(data->inStream, offset, SEEK_SET);
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static mpc_int32_t mpc_tell_cb(void *vdata)
|
|
|
|
{
|
|
|
|
MpcCallbackData *data = (MpcCallbackData *) vdata;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
|
|
|
return (long)(data->inStream->offset);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static mpc_bool_t mpc_canseek_cb(void *vdata)
|
|
|
|
{
|
|
|
|
MpcCallbackData *data = (MpcCallbackData *) vdata;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
|
|
|
return data->inStream->seekable;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static mpc_int32_t mpc_getsize_cb(void *vdata)
|
|
|
|
{
|
|
|
|
MpcCallbackData *data = (MpcCallbackData *) vdata;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
|
|
|
return data->inStream->size;
|
|
|
|
}
|
|
|
|
|
2006-07-15 15:42:57 +02:00
|
|
|
/* this _looks_ performance-critical, don't de-inline -- eric */
|
2008-10-30 08:45:00 +01:00
|
|
|
static inline int32_t convertSample(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)
|
|
|
|
*dest++ = convertSample(*src++);
|
|
|
|
}
|
|
|
|
|
2008-11-11 17:13:44 +01:00
|
|
|
static void
|
2008-10-26 19:54:57 +01:00
|
|
|
mpc_decode(struct decoder *mpd_decoder, struct input_stream *inStream)
|
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
|
|
|
|
|
|
|
MpcCallbackData 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)];
|
2005-02-01 03:00:25 +01:00
|
|
|
long bitRate = 0;
|
2005-02-01 04:20:16 +01:00
|
|
|
unsigned long samplePos = 0;
|
|
|
|
mpc_uint32_t vbrUpdateAcc;
|
|
|
|
mpc_uint32_t vbrUpdateBits;
|
2008-01-26 13:46:21 +01:00
|
|
|
float total_time;
|
2008-11-11 15:55:34 +01:00
|
|
|
struct replay_gain_info *replayGainInfo = NULL;
|
2008-11-12 07:07:12 +01:00
|
|
|
enum decoder_command cmd = DECODE_COMMAND_NONE;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
data.inStream = inStream;
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
replayGainInfo = replay_gain_info_new();
|
2008-11-11 16:24:27 +01:00
|
|
|
replayGainInfo->tuples[REPLAY_GAIN_ALBUM].gain = info.gain_album * 0.01;
|
|
|
|
replayGainInfo->tuples[REPLAY_GAIN_ALBUM].peak = info.peak_album / 32767.0;
|
|
|
|
replayGainInfo->tuples[REPLAY_GAIN_TRACK].gain = info.gain_title * 0.01;
|
|
|
|
replayGainInfo->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,
|
2008-11-02 17:01:51 +01:00
|
|
|
inStream->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) {
|
2008-08-26 08:27:07 +02:00
|
|
|
samplePos = decoder_seek_where(mpd_decoder) *
|
2008-10-10 14:40:54 +02:00
|
|
|
audio_format.sample_rate;
|
2008-11-12 07:06:47 +01:00
|
|
|
if (mpc_decoder_seek_sample(&decoder, samplePos))
|
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
|
|
|
|
2005-08-07 12:05:57 +02:00
|
|
|
vbrUpdateAcc = 0;
|
|
|
|
vbrUpdateBits = 0;
|
2005-02-01 06:26:37 +01:00
|
|
|
ret = mpc_decoder_decode(&decoder, sample_buffer,
|
2006-07-20 18:02:40 +02:00
|
|
|
&vbrUpdateAcc, &vbrUpdateBits);
|
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;
|
|
|
|
|
2005-02-01 04:20:16 +01:00
|
|
|
samplePos += 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
|
|
|
|
2008-10-10 14:40:54 +02:00
|
|
|
total_time = ((float)samplePos) / audio_format.sample_rate;
|
2008-11-12 07:03:44 +01:00
|
|
|
bitRate = vbrUpdateBits * audio_format.sample_rate
|
|
|
|
/ 1152 / 1000;
|
2005-02-01 04:20:16 +01:00
|
|
|
|
2008-11-12 07:07:12 +01:00
|
|
|
cmd = decoder_data(mpd_decoder, inStream,
|
|
|
|
chunk, ret * sizeof(chunk[0]),
|
|
|
|
total_time,
|
|
|
|
bitRate, replayGainInfo);
|
|
|
|
} while (cmd != DECODE_COMMAND_STOP);
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_info_free(replayGainInfo);
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2008-10-31 15:56:43 +01:00
|
|
|
static float mpcGetTime(const char *file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-26 19:54:57 +01:00
|
|
|
struct input_stream inStream;
|
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;
|
|
|
|
MpcCallbackData data;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
data.inStream = &inStream;
|
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);
|
|
|
|
|
2008-11-12 07:02:29 +01:00
|
|
|
if (!input_stream_open(&inStream, 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) {
|
2008-10-26 20:34:47 +01:00
|
|
|
input_stream_close(&inStream);
|
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
|
|
|
|
2008-10-26 20:34:47 +01:00
|
|
|
input_stream_close(&inStream);
|
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
|
|
|
}
|
|
|
|
|
2008-10-31 15:56:43 +01:00
|
|
|
static struct tag *mpcTagDup(const char *file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-08-29 09:38:11 +02:00
|
|
|
struct tag *ret = NULL;
|
2008-01-26 13:46:21 +01:00
|
|
|
float total_time = mpcGetTime(file);
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
if (total_time < 0) {
|
2008-11-12 07:02:29 +01:00
|
|
|
g_debug("mpcTagDup: Failed to get Songlength of file: %s\n",
|
|
|
|
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
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
ret = tag_ape_load(file);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!ret)
|
2008-08-29 09:38:21 +02:00
|
|
|
ret = tag_id3_load(file);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!ret)
|
2008-08-29 09:38:21 +02:00
|
|
|
ret = tag_new();
|
2008-01-26 13:46:21 +01:00
|
|
|
ret->time = total_time;
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return ret;
|
2005-02-01 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2008-11-01 14:55:23 +01:00
|
|
|
static const char *const mpcSuffixes[] = { "mpc", NULL };
|
2005-02-01 03:00:25 +01:00
|
|
|
|
2008-11-01 14:54:09 +01:00
|
|
|
const struct decoder_plugin mpcPlugin = {
|
2008-09-29 15:55:17 +02:00
|
|
|
.name = "mpc",
|
|
|
|
.stream_decode = mpc_decode,
|
|
|
|
.tag_dup = mpcTagDup,
|
|
|
|
.suffixes = mpcSuffixes,
|
2005-02-01 03:00:25 +01:00
|
|
|
};
|