MpcdecDecoderPlugin.cxx 6.93 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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
#include <cmath>
34 35
#include <iterator>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	val = sample * float_scale;
#endif

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

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

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

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

148 149 150
	return t;
}

151 152 153 154 155 156 157 158 159
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;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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