PcmDecoderPlugin.cxx 5.17 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"
Max Kellermann's avatar
Max Kellermann committed
24
#include "input/InputStream.hxx"
25
#include "system/ByteOrder.hxx"
26
#include "util/Domain.hxx"
27
#include "util/ByteReverse.hxx"
28
#include "util/StaticFifoBuffer.hxx"
29 30
#include "util/NumberParser.hxx"
#include "util/MimeType.hxx"
31
#include "Log.hxx"
32

33 34
#include <stdexcept>

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

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

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

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

	buffer.Append(nbytes);
	return true;
}

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

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

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

77 78 79 80
	const bool reverse_endian = (l16 && IsLittleEndian()) ||
		(mime != nullptr &&
		 strcmp(mime, "audio/x-mpd-cdda-pcm-reverse") == 0);

81 82 83
	if (is_float)
		audio_format.format = SampleFormat::FLOAT;

84
	if (mime != nullptr) {
85 86 87 88 89 90 91 92 93
		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) {
94
				FormatWarning(pcm_decoder_domain,
95 96 97 98 99
					      "Failed to parse sample rate: %s",
					      s);
				return;
			}

100 101 102 103
			try {
				CheckSampleRate(value);
			} catch (const std::runtime_error &e) {
				LogError(e);
104 105 106 107 108 109 110 111 112 113 114 115
				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) {
116
				FormatWarning(pcm_decoder_domain,
117 118 119 120 121
					      "Failed to parse sample rate: %s",
					      s);
				return;
			}

122 123 124 125
			try {
				CheckChannelCount(value);
			} catch (const std::runtime_error &e) {
				LogError(e);
126 127 128 129 130 131 132 133
				return;
			}

			audio_format.channels = value;
		}
	}

	if (audio_format.sample_rate == 0) {
134
		FormatWarning(pcm_decoder_domain,
135 136 137 138 139
			      "Missing 'rate' parameter: %s",
			      mime);
		return;
	}

140
	const auto frame_size = audio_format.GetFrameSize();
141

142 143 144 145
	const auto total_time = is.KnownSize()
		? SignedSongTime::FromScale<uint64_t>(is.GetSize() / frame_size,
						      audio_format.sample_rate)
		: SignedSongTime::Negative();
146

147
	client.Ready(audio_format, is.IsSeekable(), total_time);
148

149 150
	StaticFifoBuffer<uint8_t, 4096> buffer;

151
	DecoderCommand cmd;
152
	do {
153
		if (!FillBuffer(client, is, buffer))
154 155
			break;

156 157
		auto r = buffer.Read();
		/* round down to the nearest frame size, because we
158 159
		   must not pass partial frames to
		   DecoderClient::SubmitData() */
160 161 162
		r.size -= r.size % frame_size;
		buffer.Consume(r.size);

163 164
		if (reverse_endian)
			/* make sure we deliver samples in host byte order */
165 166 167
			reverse_bytes_16((uint16_t *)r.data,
					 (uint16_t *)r.data,
					 (uint16_t *)(r.data + r.size));
168

169
		cmd = !r.IsEmpty()
170
			? client.SubmitData(is, r.data, r.size, 0)
171
			: client.GetCommand();
172
		if (cmd == DecoderCommand::SEEK) {
173
			uint64_t frame = client.GetSeekFrame();
174
			offset_type offset = frame * frame_size;
175

176 177
			try {
				is.LockSeek(offset);
178
				buffer.Clear();
179
				client.CommandFinished();
180 181
			} catch (const std::runtime_error &e) {
				LogError(e);
182
				client.SeekError();
183 184
			}

185
			cmd = DecoderCommand::NONE;
186
		}
187
	} while (cmd == DecoderCommand::NONE);
188 189 190
}

static const char *const pcm_mime_types[] = {
191 192 193
	/* RFC 2586 */
	"audio/L16",

194 195 196
	/* MPD-specific: float32 native-endian */
	"audio/x-mpd-float",

197 198
	/* for streams obtained by the cdio_paranoia input plugin */
	"audio/x-mpd-cdda-pcm",
199 200 201 202

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

203
	nullptr
204 205
};

206
const struct DecoderPlugin pcm_decoder_plugin = {
207 208 209 210 211 212 213 214 215 216
	"pcm",
	nullptr,
	nullptr,
	pcm_stream_decode,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	pcm_mime_types,
217
};