MpcdecDecoderPlugin.cxx 7.08 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "CheckAudioFormat.hxx"
24
#include "pcm/Traits.hxx"
25
#include "tag/Handler.hxx"
26
#include "util/Domain.hxx"
27
#include "util/Macros.hxx"
28
#include "util/Clamp.hxx"
29
#include "util/ScopeExit.hxx"
30
#include "Log.hxx"
31

32 33
#include <mpc/mpcdec.h>

34
#include <exception>
35

36
#include <math.h>
37

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	val = sample * float_scale;
#endif

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

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

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

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

150 151 152
	return t;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

240
		MpcdecSampleTraits::value_type chunk[ARRAY_SIZE(sample_buffer)];
241
		mpc_to_mpd_buffer(chunk, sample_buffer, ret);
242

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

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

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

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

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

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

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

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

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

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

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

290
const struct DecoderPlugin mpcdec_decoder_plugin = {
291 292 293 294 295 296 297 298 299 300
	"mpcdec",
	nullptr,
	nullptr,
	mpcdec_decode,
	nullptr,
	nullptr,
	mpcdec_scan_stream,
	nullptr,
	mpcdec_suffixes,
	nullptr,
301
};