PcmDecoderPlugin.cxx 5.82 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * http://www.musicpd.org
 *
 * 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.
 *
 * 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.
 */

#include "config.h"
21 22
#include "PcmDecoderPlugin.hxx"
#include "../DecoderAPI.hxx"
23
#include "CheckAudioFormat.hxx"
24
#include "pcm/PcmPack.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "input/InputStream.hxx"
26
#include "system/ByteOrder.hxx"
27
#include "util/Domain.hxx"
28
#include "util/ByteReverse.hxx"
29
#include "util/StaticFifoBuffer.hxx"
30 31
#include "util/NumberParser.hxx"
#include "util/MimeType.hxx"
32
#include "Log.hxx"
33

34
#include <exception>
35

36
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
37
#include <string.h>
38

39 40
static constexpr Domain pcm_decoder_domain("pcm_decoder");

41 42
template<typename B>
static bool
43
FillBuffer(DecoderClient &client, InputStream &is, B &buffer)
44 45 46
{
	buffer.Shift();
	auto w = buffer.Write();
47
	if (w.empty())
48
		return true;
49

50
	size_t nbytes = decoder_read(client, is, w.data, w.size);
51 52 53 54 55 56 57
	if (nbytes == 0 && is.LockIsEOF())
		return false;

	buffer.Append(nbytes);
	return true;
}

58
static void
59
pcm_stream_decode(DecoderClient &client, InputStream &is)
60
{
61
	AudioFormat audio_format = {
62
		44100,
63
		SampleFormat::S16,
64
		2,
65
	};
66

67
	const char *const mime = is.GetMimeType();
68

69 70
	const bool l16 = mime != nullptr &&
		GetMimeTypeBase(mime) == "audio/L16";
71 72
	const bool l24 = mime != nullptr &&
		GetMimeTypeBase(mime) == "audio/L24";
73 74
	const bool is_float = mime != nullptr &&
		GetMimeTypeBase(mime) == "audio/x-mpd-float";
75
	if (l16 || l24 || is_float) {
76 77 78 79
		audio_format.sample_rate = 0;
		audio_format.channels = 1;
	}

80 81 82
	if (l24)
		audio_format.format = SampleFormat::S24_P32;

83 84 85 86
	const bool reverse_endian = (l16 && IsLittleEndian()) ||
		(mime != nullptr &&
		 strcmp(mime, "audio/x-mpd-cdda-pcm-reverse") == 0);

87 88 89
	if (is_float)
		audio_format.format = SampleFormat::FLOAT;

90
	if (mime != nullptr) {
91 92 93 94 95 96 97 98 99
		const auto mime_parameters = ParseMimeTypeParameters(mime);

		/* MIME type parameters according to RFC 2586 */
		auto i = mime_parameters.find("rate");
		if (i != mime_parameters.end()) {
			const char *s = i->second.c_str();
			char *endptr;
			unsigned value = ParseUnsigned(s, &endptr);
			if (endptr == s || *endptr != 0) {
100
				FormatWarning(pcm_decoder_domain,
101 102 103 104 105
					      "Failed to parse sample rate: %s",
					      s);
				return;
			}

106 107
			try {
				CheckSampleRate(value);
108 109
			} catch (...) {
				LogError(std::current_exception());
110 111 112 113 114 115 116 117 118 119 120 121
				return;
			}

			audio_format.sample_rate = value;
		}

		i = mime_parameters.find("channels");
		if (i != mime_parameters.end()) {
			const char *s = i->second.c_str();
			char *endptr;
			unsigned value = ParseUnsigned(s, &endptr);
			if (endptr == s || *endptr != 0) {
122
				FormatWarning(pcm_decoder_domain,
123 124 125 126 127
					      "Failed to parse sample rate: %s",
					      s);
				return;
			}

128 129
			try {
				CheckChannelCount(value);
130 131
			} catch (...) {
				LogError(std::current_exception());
132 133 134 135 136 137 138 139
				return;
			}

			audio_format.channels = value;
		}
	}

	if (audio_format.sample_rate == 0) {
140
		FormatWarning(pcm_decoder_domain,
141 142 143 144 145
			      "Missing 'rate' parameter: %s",
			      mime);
		return;
	}

146 147
	const auto out_frame_size = audio_format.GetFrameSize();
	const auto in_frame_size = out_frame_size;
148

149
	const auto total_time = is.KnownSize()
150
		? SignedSongTime::FromScale<uint64_t>(is.GetSize() / in_frame_size,
151 152
						      audio_format.sample_rate)
		: SignedSongTime::Negative();
153

154
	client.Ready(audio_format, is.IsSeekable(), total_time);
155

156 157
	StaticFifoBuffer<uint8_t, 4096> buffer;

158 159 160 161
	/* a buffer for pcm_unpack_24be() large enough to hold the
	   results for a full source buffer */
	int32_t unpack_buffer[buffer.GetCapacity() / 3];

162
	DecoderCommand cmd;
163
	do {
164
		if (!FillBuffer(client, is, buffer))
165 166
			break;

167 168
		auto r = buffer.Read();
		/* round down to the nearest frame size, because we
169 170
		   must not pass partial frames to
		   DecoderClient::SubmitData() */
171
		r.size -= r.size % in_frame_size;
172 173
		buffer.Consume(r.size);

174 175
		if (reverse_endian)
			/* make sure we deliver samples in host byte order */
176 177 178
			reverse_bytes_16((uint16_t *)r.data,
					 (uint16_t *)r.data,
					 (uint16_t *)(r.data + r.size));
179 180 181 182 183 184 185 186
		else if (l24) {
			/* convert big-endian packed 24 bit
			   (audio/L24) to native-endian 24 bit (in 32
			   bit integers) */
			pcm_unpack_24be(unpack_buffer, r.begin(), r.end());
			r.data = (uint8_t *)&unpack_buffer[0];
			r.size = (r.size / 3) * 4;
		}
187

188
		cmd = !r.empty()
189
			? client.SubmitData(is, r.data, r.size, 0)
190
			: client.GetCommand();
191
		if (cmd == DecoderCommand::SEEK) {
192
			uint64_t frame = client.GetSeekFrame();
193
			offset_type offset = frame * in_frame_size;
194

195 196
			try {
				is.LockSeek(offset);
197
				buffer.Clear();
198
				client.CommandFinished();
199 200
			} catch (...) {
				LogError(std::current_exception());
201
				client.SeekError();
202 203
			}

204
			cmd = DecoderCommand::NONE;
205
		}
206
	} while (cmd == DecoderCommand::NONE);
207 208 209
}

static const char *const pcm_mime_types[] = {
210 211 212
	/* RFC 2586 */
	"audio/L16",

213 214 215
	/* RFC 3190 */
	"audio/L24",

216 217 218
	/* MPD-specific: float32 native-endian */
	"audio/x-mpd-float",

219 220
	/* for streams obtained by the cdio_paranoia input plugin */
	"audio/x-mpd-cdda-pcm",
221 222 223 224

	/* same as above, but with reverse byte order */
	"audio/x-mpd-cdda-pcm-reverse",

225
	nullptr
226 227
};

228
const struct DecoderPlugin pcm_decoder_plugin = {
229 230 231 232 233 234 235 236 237 238
	"pcm",
	nullptr,
	nullptr,
	pcm_stream_decode,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	pcm_mime_types,
239
};