MpcdecDecoderPlugin.cxx 6.93 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 "MpcdecDecoderPlugin.hxx"
21
#include "../DecoderAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "input/InputStream.hxx"
23
#include "pcm/CheckAudioFormat.hxx"
24
#include "pcm/Traits.hxx"
25
#include "tag/Handler.hxx"
26
#include "util/Domain.hxx"
27
#include "util/Clamp.hxx"
28
#include "util/ScopeExit.hxx"
29
#include "Log.hxx"
30

31 32
#include <mpc/mpcdec.h>

33 34
#include <iterator>

35
#include <math.h>
36

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

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

45 46
static constexpr Domain mpcdec_domain("mpcdec");

47
static constexpr SampleFormat mpcdec_sample_format = SampleFormat::S24_P32;
48
using MpcdecSampleTraits = SampleTraits<mpcdec_sample_format>;
49

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

56
	return decoder_read(data->client, data->is, ptr, size);
57 58
}

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

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

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

79
	return (long)data->is.GetOffset();
80 81
}

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

88
	return data->is.IsSeekable();
89 90
}

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

97 98 99
	if (!data->is.KnownSize())
		return -1;

100
	return data->is.GetSize();
101 102
}

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

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

114
#ifdef MPC_FIXED_POINT
115
	const int shift = bits - MPC_FIXED_POINT_SCALE_SHIFT;
116

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

	val = sample * float_scale;
#endif

127
	return Clamp(val, clip_min, clip_max);
128 129
}

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

139 140 141 142
static constexpr ReplayGainTuple
ImportMpcdecReplayGain(mpc_uint16_t gain, mpc_uint16_t peak) noexcept
{
	auto t = ReplayGainTuple::Undefined();
143 144 145 146 147 148

	if (gain != 0 && peak != 0) {
		t.gain = MPC_OLD_GAIN_REF - (gain  / 256.);
		t.peak = pow(10, peak / 256. / 20) / 32767;
	}

149 150 151
	return t;
}

152 153 154 155 156 157 158 159 160
static constexpr ReplayGainInfo
ImportMpcdecReplayGain(const mpc_streaminfo &info) noexcept
{
	auto rgi = ReplayGainInfo::Undefined();
	rgi.album = ImportMpcdecReplayGain(info.gain_album, info.peak_album);
	rgi.track = ImportMpcdecReplayGain(info.gain_title, info.peak_title);
	return rgi;
}

161
static void
162
mpcdec_decode(DecoderClient &client, InputStream &is)
163
{
164
	mpc_decoder_data data(is, &client);
165

166
	mpc_reader reader;
167 168 169 170 171 172 173
	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;

174
	mpc_demux *demux = mpc_demux_init(&reader);
175
	if (demux == nullptr) {
176
		if (client.GetCommand() != DecoderCommand::STOP)
177 178
			LogWarning(mpcdec_domain,
				   "Not a valid musepack stream");
179 180 181
		return;
	}

182 183
	AtScopeExit(demux) { mpc_demux_exit(demux); };

184
	mpc_streaminfo info;
185
	mpc_demux_get_info(demux, &info);
Avuton Olrich's avatar
Avuton Olrich committed
186

187 188 189
	auto audio_format = CheckAudioFormat(info.sample_freq,
					     mpcdec_sample_format,
					     info.channels);
190

191 192
	{
		const auto rgi = ImportMpcdecReplayGain(info);
193 194
		if (rgi.IsDefined())
			client.SubmitReplayGain(&rgi);
195
	}
Warren Dukes's avatar
Warren Dukes committed
196

197 198
	client.Ready(audio_format, is.IsSeekable(),
		     SongTime::FromS(mpc_streaminfo_get_length(&info)));
199

200
	DecoderCommand cmd = DecoderCommand::NONE;
201
	do {
202
		if (cmd == DecoderCommand::SEEK) {
203
			mpc_int64_t where = client.GetSeekFrame();
204
			bool success;
205

206
			success = mpc_demux_seek_sample(demux, where)
207 208
				== MPC_STATUS_OK;
			if (success)
209
				client.CommandFinished();
210
			else
211
				client.SeekError();
212
		}
213

214
		MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
215
		mpc_frame_info frame;
216
		frame.buffer = (MPC_SAMPLE_FORMAT *)sample_buffer;
217
		mpc_status status = mpc_demux_decode(demux, &frame);
218
		if (status != MPC_STATUS_OK) {
219 220
			LogWarning(mpcdec_domain,
				   "Failed to decode sample");
221 222 223 224 225 226
			break;
		}

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

227 228 229 230 231 232 233 234 235
		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;
		}

236
		mpc_uint32_t ret = frame.samples;
237
		ret *= info.channels;
238

239
		MpcdecSampleTraits::value_type chunk[std::size(sample_buffer)];
240
		mpc_to_mpd_buffer(chunk, sample_buffer, ret);
241

242 243
		long bit_rate = unsigned(frame.bits) * audio_format.sample_rate
			/ (1000 * frame.samples);
244

245 246 247
		cmd = client.SubmitData(is,
					chunk, ret * sizeof(chunk[0]),
					bit_rate);
248
	} while (cmd != DecoderCommand::STOP);
249 250
}

251
static SignedSongTime
252
mpcdec_get_file_duration(InputStream &is)
Avuton Olrich's avatar
Avuton Olrich committed
253
{
254
	mpc_decoder_data data(is, nullptr);
255

256
	mpc_reader reader;
257 258 259 260 261 262 263
	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;

264
	mpc_demux *demux = mpc_demux_init(&reader);
265
	if (demux == nullptr)
266
		return SignedSongTime::Negative();
267

268 269
	AtScopeExit(demux) { mpc_demux_exit(demux); };

270
	mpc_streaminfo info;
271
	mpc_demux_get_info(demux, &info);
272

273
	return SongTime::FromS(mpc_streaminfo_get_length(&info));
274 275
}

276
static bool
277
mpcdec_scan_stream(InputStream &is, TagHandler &handler) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
278
{
279 280
	const auto duration = mpcdec_get_file_duration(is);
	if (duration.IsNegative())
281
		return false;
282

283
	handler.OnDuration(SongTime(duration));
284
	return true;
285 286
}

287
static const char *const mpcdec_suffixes[] = { "mpc", nullptr };
288

289 290 291
constexpr DecoderPlugin mpcdec_decoder_plugin =
	DecoderPlugin("mpcdec", mpcdec_decode, mpcdec_scan_stream)
	.WithSuffixes(mpcdec_suffixes);