MpcdecDecoderPlugin.cxx 6.74 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
static void
141
mpcdec_decode(DecoderClient &client, InputStream &is)
142
{
143
	mpc_decoder_data data(is, &client);
144

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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