2010-12-22 09:08:12 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2010-12-22 09:08:12 +01:00
|
|
|
* 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"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "PcmDecoderPlugin.hxx"
|
|
|
|
#include "../DecoderAPI.hxx"
|
2016-06-10 22:45:22 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/InputStream.hxx"
|
2016-07-01 21:34:56 +02:00
|
|
|
#include "system/ByteOrder.hxx"
|
2016-11-10 11:45:17 +01:00
|
|
|
#include "util/Domain.hxx"
|
2013-10-16 21:04:52 +02:00
|
|
|
#include "util/ByteReverse.hxx"
|
2016-11-17 21:58:27 +01:00
|
|
|
#include "util/StaticFifoBuffer.hxx"
|
2016-06-10 22:45:22 +02:00
|
|
|
#include "util/NumberParser.hxx"
|
|
|
|
#include "util/MimeType.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2013-07-28 13:10:05 +02:00
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2016-11-17 21:58:27 +01:00
|
|
|
#include <assert.h>
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.h>
|
2010-12-22 09:08:12 +01:00
|
|
|
|
2016-11-10 11:45:17 +01:00
|
|
|
static constexpr Domain pcm_decoder_domain("pcm_decoder");
|
|
|
|
|
2016-11-17 21:58:27 +01:00
|
|
|
template<typename B>
|
|
|
|
static bool
|
2016-11-18 07:13:35 +01:00
|
|
|
FillBuffer(DecoderClient &client, InputStream &is, B &buffer)
|
2016-11-17 21:58:27 +01:00
|
|
|
{
|
|
|
|
buffer.Shift();
|
|
|
|
auto w = buffer.Write();
|
|
|
|
assert(!w.IsEmpty());
|
|
|
|
|
2016-11-18 07:13:35 +01:00
|
|
|
size_t nbytes = decoder_read(client, is, w.data, w.size);
|
2016-11-17 21:58:27 +01:00
|
|
|
if (nbytes == 0 && is.LockIsEOF())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buffer.Append(nbytes);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-22 09:08:12 +01:00
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
pcm_stream_decode(DecoderClient &client, InputStream &is)
|
2010-12-22 09:08:12 +01:00
|
|
|
{
|
2016-06-10 22:45:22 +02:00
|
|
|
AudioFormat audio_format = {
|
2013-07-28 13:10:05 +02:00
|
|
|
44100,
|
2013-08-03 21:00:50 +02:00
|
|
|
SampleFormat::S16,
|
2013-07-28 13:10:05 +02:00
|
|
|
2,
|
2010-12-22 09:08:12 +01:00
|
|
|
};
|
2011-10-10 09:40:46 +02:00
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
const char *const mime = is.GetMimeType();
|
2011-10-10 09:40:46 +02:00
|
|
|
|
2016-06-10 22:45:22 +02:00
|
|
|
const bool l16 = mime != nullptr &&
|
|
|
|
GetMimeTypeBase(mime) == "audio/L16";
|
2016-06-10 22:59:45 +02:00
|
|
|
const bool is_float = mime != nullptr &&
|
|
|
|
GetMimeTypeBase(mime) == "audio/x-mpd-float";
|
|
|
|
if (l16 || is_float) {
|
2016-06-10 22:45:22 +02:00
|
|
|
audio_format.sample_rate = 0;
|
|
|
|
audio_format.channels = 1;
|
|
|
|
}
|
|
|
|
|
2016-07-01 21:34:56 +02:00
|
|
|
const bool reverse_endian = (l16 && IsLittleEndian()) ||
|
|
|
|
(mime != nullptr &&
|
|
|
|
strcmp(mime, "audio/x-mpd-cdda-pcm-reverse") == 0);
|
|
|
|
|
2016-06-10 22:59:45 +02:00
|
|
|
if (is_float)
|
|
|
|
audio_format.format = SampleFormat::FLOAT;
|
|
|
|
|
2016-06-10 22:45:22 +02:00
|
|
|
{
|
|
|
|
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) {
|
2016-11-10 11:45:17 +01:00
|
|
|
FormatWarning(pcm_decoder_domain,
|
2016-06-10 22:45:22 +02:00
|
|
|
"Failed to parse sample rate: %s",
|
|
|
|
s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-10 11:45:17 +01:00
|
|
|
try {
|
|
|
|
CheckSampleRate(value);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
LogError(e);
|
2016-06-10 22:45:22 +02:00
|
|
|
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) {
|
2016-11-10 11:45:17 +01:00
|
|
|
FormatWarning(pcm_decoder_domain,
|
2016-06-10 22:45:22 +02:00
|
|
|
"Failed to parse sample rate: %s",
|
|
|
|
s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-10 11:45:17 +01:00
|
|
|
try {
|
|
|
|
CheckChannelCount(value);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
LogError(e);
|
2016-06-10 22:45:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
audio_format.channels = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audio_format.sample_rate == 0) {
|
2016-11-10 11:45:17 +01:00
|
|
|
FormatWarning(pcm_decoder_domain,
|
2016-06-10 22:45:22 +02:00
|
|
|
"Missing 'rate' parameter: %s",
|
|
|
|
mime);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:05:02 +02:00
|
|
|
const auto frame_size = audio_format.GetFrameSize();
|
2010-12-22 09:08:12 +01:00
|
|
|
|
2014-08-29 20:52:39 +02:00
|
|
|
const auto total_time = is.KnownSize()
|
|
|
|
? SignedSongTime::FromScale<uint64_t>(is.GetSize() / frame_size,
|
|
|
|
audio_format.sample_rate)
|
|
|
|
: SignedSongTime::Negative();
|
2010-12-22 09:08:12 +01:00
|
|
|
|
2016-11-18 07:59:01 +01:00
|
|
|
client.Ready(audio_format, is.IsSeekable(), total_time);
|
2010-12-22 09:08:12 +01:00
|
|
|
|
2016-11-17 21:58:27 +01:00
|
|
|
StaticFifoBuffer<uint8_t, 4096> buffer;
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand cmd;
|
2010-12-22 09:08:12 +01:00
|
|
|
do {
|
2016-11-18 07:13:35 +01:00
|
|
|
if (!FillBuffer(client, is, buffer))
|
2011-03-20 17:26:28 +01:00
|
|
|
break;
|
|
|
|
|
2016-11-17 21:58:27 +01:00
|
|
|
auto r = buffer.Read();
|
|
|
|
/* round down to the nearest frame size, because we
|
2016-11-18 08:27:30 +01:00
|
|
|
must not pass partial frames to
|
|
|
|
DecoderClient::SubmitData() */
|
2016-11-17 21:58:27 +01:00
|
|
|
r.size -= r.size % frame_size;
|
|
|
|
buffer.Consume(r.size);
|
|
|
|
|
2012-03-21 19:07:15 +01:00
|
|
|
if (reverse_endian)
|
|
|
|
/* make sure we deliver samples in host byte order */
|
2016-11-17 21:58:27 +01:00
|
|
|
reverse_bytes_16((uint16_t *)r.data,
|
|
|
|
(uint16_t *)r.data,
|
|
|
|
(uint16_t *)(r.data + r.size));
|
2012-03-21 19:07:15 +01:00
|
|
|
|
2016-11-17 21:58:27 +01:00
|
|
|
cmd = !r.IsEmpty()
|
2016-11-18 08:27:30 +01:00
|
|
|
? client.SubmitData(is, r.data, r.size, 0)
|
2016-11-18 08:15:07 +01:00
|
|
|
: client.GetCommand();
|
2013-09-27 12:11:37 +02:00
|
|
|
if (cmd == DecoderCommand::SEEK) {
|
2016-11-18 08:15:07 +01:00
|
|
|
uint64_t frame = client.GetSeekFrame();
|
2014-08-26 22:05:02 +02:00
|
|
|
offset_type offset = frame * frame_size;
|
2013-08-10 18:02:44 +02:00
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
try {
|
|
|
|
is.LockSeek(offset);
|
2016-11-17 21:58:27 +01:00
|
|
|
buffer.Clear();
|
2016-11-18 08:15:07 +01:00
|
|
|
client.CommandFinished();
|
2016-09-09 18:47:42 +02:00
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
LogError(e);
|
2016-11-18 08:15:07 +01:00
|
|
|
client.SeekError();
|
2010-12-22 09:08:12 +01:00
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
cmd = DecoderCommand::NONE;
|
2010-12-22 09:08:12 +01:00
|
|
|
}
|
2013-09-27 12:11:37 +02:00
|
|
|
} while (cmd == DecoderCommand::NONE);
|
2010-12-22 09:08:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const pcm_mime_types[] = {
|
2016-06-10 22:45:22 +02:00
|
|
|
/* RFC 2586 */
|
|
|
|
"audio/L16",
|
|
|
|
|
2016-06-10 22:59:45 +02:00
|
|
|
/* MPD-specific: float32 native-endian */
|
|
|
|
"audio/x-mpd-float",
|
|
|
|
|
2010-12-22 09:08:12 +01:00
|
|
|
/* for streams obtained by the cdio_paranoia input plugin */
|
|
|
|
"audio/x-mpd-cdda-pcm",
|
2011-10-10 09:40:46 +02:00
|
|
|
|
|
|
|
/* same as above, but with reverse byte order */
|
|
|
|
"audio/x-mpd-cdda-pcm-reverse",
|
|
|
|
|
2013-07-28 13:10:05 +02:00
|
|
|
nullptr
|
2010-12-22 09:08:12 +01:00
|
|
|
};
|
|
|
|
|
2013-10-21 20:31:34 +02:00
|
|
|
const struct DecoderPlugin pcm_decoder_plugin = {
|
2013-07-28 13:10:05 +02:00
|
|
|
"pcm",
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
pcm_stream_decode,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
pcm_mime_types,
|
2010-12-22 09:08:12 +01:00
|
|
|
};
|