MpcdecDecoderPlugin.cxx 6.67 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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/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 48 49
static constexpr SampleFormat mpcdec_sample_format = SampleFormat::S24_P32;
typedef SampleTraits<mpcdec_sample_format> MpcdecSampleTraits;

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
{
53 54
	struct mpc_decoder_data *data =
		(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
{
62 63
	struct mpc_decoder_data *data =
		(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
{
76 77
	struct mpc_decoder_data *data =
		(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
{
85 86
	struct mpc_decoder_data *data =
		(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
{
94 95
	struct mpc_decoder_data *data =
		(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 132
mpc_to_mpd_buffer(MpcdecSampleTraits::pointer_type dest,
		  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
static void
140
mpcdec_decode(DecoderClient &client, InputStream &is)
141
{
142
	mpc_decoder_data data(is, &client);
143

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

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

160 161
	AtScopeExit(demux) { mpc_demux_exit(demux); };

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

165 166 167
	auto audio_format = CheckAudioFormat(info.sample_freq,
					     mpcdec_sample_format,
					     info.channels);
168

169
	ReplayGainInfo rgi;
170
	rgi.Clear();
171 172 173 174
	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;
175

176
	client.SubmitReplayGain(&rgi);
Warren Dukes's avatar
Warren Dukes committed
177

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

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

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

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

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

208 209 210 211 212 213 214 215 216
		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;
		}

217
		mpc_uint32_t ret = frame.samples;
218
		ret *= info.channels;
219

220
		MpcdecSampleTraits::value_type chunk[std::size(sample_buffer)];
221
		mpc_to_mpd_buffer(chunk, sample_buffer, ret);
222

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

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

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

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

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

249 250
	AtScopeExit(demux) { mpc_demux_exit(demux); };

251
	mpc_streaminfo info;
252
	mpc_demux_get_info(demux, &info);
253

254
	return SongTime::FromS(mpc_streaminfo_get_length(&info));
255 256
}

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

264
	handler.OnDuration(SongTime(duration));
265
	return true;
266 267
}

268
static const char *const mpcdec_suffixes[] = { "mpc", nullptr };
269

270 271 272
constexpr DecoderPlugin mpcdec_decoder_plugin =
	DecoderPlugin("mpcdec", mpcdec_decode, mpcdec_scan_stream)
	.WithSuffixes(mpcdec_suffixes);