MpcdecDecoderPlugin.cxx 6.76 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "MpcdecDecoderPlugin.hxx"
22
#include "../DecoderAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "input/InputStream.hxx"
24
#include "CheckAudioFormat.hxx"
25
#include "pcm/Traits.hxx"
26
#include "tag/Handler.hxx"
27
#include "util/Domain.hxx"
28
#include "util/Macros.hxx"
29
#include "util/Clamp.hxx"
30
#include "util/ScopeExit.hxx"
31
#include "Log.hxx"
32

33 34
#include <mpc/mpcdec.h>

35
#include <exception>
36

37
#include <math.h>
38

Max Kellermann's avatar
Max Kellermann committed
39
struct mpc_decoder_data {
40
	InputStream &is;
41
	DecoderClient *client;
42

43 44
	mpc_decoder_data(InputStream &_is, DecoderClient *_client)
		:is(_is), client(_client) {}
Max Kellermann's avatar
Max Kellermann committed
45
};
46

47 48
static constexpr Domain mpcdec_domain("mpcdec");

49 50 51
static constexpr SampleFormat mpcdec_sample_format = SampleFormat::S24_P32;
typedef SampleTraits<mpcdec_sample_format> MpcdecSampleTraits;

Max Kellermann's avatar
Max Kellermann committed
52
static mpc_int32_t
53
mpc_read_cb(mpc_reader *reader, void *ptr, mpc_int32_t size)
Avuton Olrich's avatar
Avuton Olrich committed
54
{
55 56
	struct mpc_decoder_data *data =
		(struct mpc_decoder_data *)reader->data;
Avuton Olrich's avatar
Avuton Olrich committed
57

58
	return decoder_read(data->client, data->is, ptr, size);
59 60
}

Max Kellermann's avatar
Max Kellermann committed
61
static mpc_bool_t
62
mpc_seek_cb(mpc_reader *reader, mpc_int32_t offset)
Avuton Olrich's avatar
Avuton Olrich committed
63
{
64 65
	struct mpc_decoder_data *data =
		(struct mpc_decoder_data *)reader->data;
66

67 68 69
	try {
		data->is.LockSeek(offset);
		return true;
70
	} catch (...) {
71 72
		return false;
	}
73 74
}

Max Kellermann's avatar
Max Kellermann committed
75
static mpc_int32_t
76
mpc_tell_cb(mpc_reader *reader)
Avuton Olrich's avatar
Avuton Olrich committed
77
{
78 79
	struct mpc_decoder_data *data =
		(struct mpc_decoder_data *)reader->data;
80

81
	return (long)data->is.GetOffset();
82 83
}

Max Kellermann's avatar
Max Kellermann committed
84
static mpc_bool_t
85
mpc_canseek_cb(mpc_reader *reader)
Avuton Olrich's avatar
Avuton Olrich committed
86
{
87 88
	struct mpc_decoder_data *data =
		(struct mpc_decoder_data *)reader->data;
89

90
	return data->is.IsSeekable();
91 92
}

Max Kellermann's avatar
Max Kellermann committed
93
static mpc_int32_t
94
mpc_getsize_cb(mpc_reader *reader)
Avuton Olrich's avatar
Avuton Olrich committed
95
{
96 97
	struct mpc_decoder_data *data =
		(struct mpc_decoder_data *)reader->data;
98

99 100 101
	if (!data->is.KnownSize())
		return -1;

102
	return data->is.GetSize();
103 104
}

105
/* this _looks_ performance-critical, don't de-inline -- eric */
106
static inline MpcdecSampleTraits::value_type
Max Kellermann's avatar
Max Kellermann committed
107
mpc_to_mpd_sample(MPC_SAMPLE_FORMAT sample)
Avuton Olrich's avatar
Avuton Olrich committed
108
{
109
	/* only doing 16-bit audio for now */
110
	MpcdecSampleTraits::value_type val;
111

112 113 114
	constexpr int bits = MpcdecSampleTraits::BITS;
	constexpr auto clip_min = MpcdecSampleTraits::MIN;
	constexpr auto clip_max = MpcdecSampleTraits::MAX;
Avuton Olrich's avatar
Avuton Olrich committed
115

116
#ifdef MPC_FIXED_POINT
117
	const int shift = bits - MPC_FIXED_POINT_SCALE_SHIFT;
118

119
	if (shift < 0)
120
		val = sample >> -shift;
121 122
	else
		val = sample << shift;
123
#else
124
	const int float_scale = 1 << (bits - 1);
125 126 127 128

	val = sample * float_scale;
#endif

129
	return Clamp(val, clip_min, clip_max);
130 131
}

132
static void
133 134
mpc_to_mpd_buffer(MpcdecSampleTraits::pointer_type dest,
		  const MPC_SAMPLE_FORMAT *src,
135 136 137
		  unsigned num_samples)
{
	while (num_samples-- > 0)
Max Kellermann's avatar
Max Kellermann committed
138
		*dest++ = mpc_to_mpd_sample(*src++);
139 140
}

141
static void
142
mpcdec_decode(DecoderClient &client, InputStream &is)
143
{
144
	mpc_decoder_data data(is, &client);
145

146
	mpc_reader reader;
147 148 149 150 151 152 153
	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;

154
	mpc_demux *demux = mpc_demux_init(&reader);
155
	if (demux == nullptr) {
156
		if (client.GetCommand() != DecoderCommand::STOP)
157 158
			LogWarning(mpcdec_domain,
				   "Not a valid musepack stream");
159 160 161
		return;
	}

162 163
	AtScopeExit(demux) { mpc_demux_exit(demux); };

164
	mpc_streaminfo info;
165
	mpc_demux_get_info(demux, &info);
Avuton Olrich's avatar
Avuton Olrich committed
166

167 168 169
	auto audio_format = CheckAudioFormat(info.sample_freq,
					     mpcdec_sample_format,
					     info.channels);
170

171
	ReplayGainInfo rgi;
172
	rgi.Clear();
173 174 175 176
	rgi.album.gain = MPC_OLD_GAIN_REF  - (info.gain_album  / 256.);
	rgi.album.peak = pow(10, info.peak_album / 256. / 20) / 32767;
	rgi.track.gain = MPC_OLD_GAIN_REF  - (info.gain_title  / 256.);
	rgi.track.peak = pow(10, info.peak_title / 256. / 20) / 32767;
177

178
	client.SubmitReplayGain(&rgi);
Warren Dukes's avatar
Warren Dukes committed
179

180 181
	client.Ready(audio_format, is.IsSeekable(),
		     SongTime::FromS(mpc_streaminfo_get_length(&info)));
182

183
	DecoderCommand cmd = DecoderCommand::NONE;
184
	do {
185
		if (cmd == DecoderCommand::SEEK) {
186
			mpc_int64_t where = client.GetSeekFrame();
187
			bool success;
188

189
			success = mpc_demux_seek_sample(demux, where)
190 191
				== MPC_STATUS_OK;
			if (success)
192
				client.CommandFinished();
193
			else
194
				client.SeekError();
195
		}
196

197
		MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
198
		mpc_frame_info frame;
199
		frame.buffer = (MPC_SAMPLE_FORMAT *)sample_buffer;
200
		mpc_status status = mpc_demux_decode(demux, &frame);
201
		if (status != MPC_STATUS_OK) {
202 203
			LogWarning(mpcdec_domain,
				   "Failed to decode sample");
204 205 206 207 208 209
			break;
		}

		if (frame.bits == -1)
			break;

210 211 212 213 214 215 216 217 218
		if (frame.samples <= 0) {
			/* empty frame - this has been observed to
			   happen spuriously after seeking; skip this
			   obscure frame, and hope libmpcdec
			   recovers */
			cmd = client.GetCommand();
			continue;
		}

219
		mpc_uint32_t ret = frame.samples;
220
		ret *= info.channels;
221

222
		MpcdecSampleTraits::value_type chunk[ARRAY_SIZE(sample_buffer)];
223
		mpc_to_mpd_buffer(chunk, sample_buffer, ret);
224

225 226
		long bit_rate = unsigned(frame.bits) * audio_format.sample_rate
			/ (1000 * frame.samples);
227

228 229 230
		cmd = client.SubmitData(is,
					chunk, ret * sizeof(chunk[0]),
					bit_rate);
231
	} while (cmd != DecoderCommand::STOP);
232 233
}

234
static SignedSongTime
235
mpcdec_get_file_duration(InputStream &is)
Avuton Olrich's avatar
Avuton Olrich committed
236
{
237
	mpc_decoder_data data(is, nullptr);
238

239
	mpc_reader reader;
240 241 242 243 244 245 246
	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;

247
	mpc_demux *demux = mpc_demux_init(&reader);
248
	if (demux == nullptr)
249
		return SignedSongTime::Negative();
250

251 252
	AtScopeExit(demux) { mpc_demux_exit(demux); };

253
	mpc_streaminfo info;
254
	mpc_demux_get_info(demux, &info);
255

256
	return SongTime::FromS(mpc_streaminfo_get_length(&info));
257 258
}

259
static bool
260
mpcdec_scan_stream(InputStream &is, TagHandler &handler) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
261
{
262 263
	const auto duration = mpcdec_get_file_duration(is);
	if (duration.IsNegative())
264
		return false;
265

266
	handler.OnDuration(SongTime(duration));
267
	return true;
268 269
}

270
static const char *const mpcdec_suffixes[] = { "mpc", nullptr };
271

272
const struct DecoderPlugin mpcdec_decoder_plugin = {
273 274 275 276 277 278 279 280 281 282
	"mpcdec",
	nullptr,
	nullptr,
	mpcdec_decode,
	nullptr,
	nullptr,
	mpcdec_scan_stream,
	nullptr,
	mpcdec_suffixes,
	nullptr,
283
};