2009-07-07 08:58:51 +02:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2009-07-07 08:58:51 +02: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.
|
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-07-28 12:42:06 +02:00
|
|
|
#include "SndfileDecoderPlugin.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "../DecoderAPI.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/InputStream.hxx"
|
2013-07-29 07:50:08 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Handler.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
|
|
|
#include "Log.hxx"
|
2009-07-07 08:58:51 +02:00
|
|
|
|
2017-12-19 10:56:23 +01:00
|
|
|
#include <exception>
|
2016-09-09 18:47:42 +02:00
|
|
|
|
2009-07-07 08:58:51 +02:00
|
|
|
#include <sndfile.h>
|
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain sndfile_domain("sndfile");
|
2009-07-07 08:58:51 +02:00
|
|
|
|
2014-07-11 21:53:03 +02:00
|
|
|
static bool
|
2015-01-21 22:13:44 +01:00
|
|
|
sndfile_init(gcc_unused const ConfigBlock &block)
|
2014-07-11 21:53:03 +02:00
|
|
|
{
|
|
|
|
LogDebug(sndfile_domain, sf_version_string());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-11 20:50:44 +02:00
|
|
|
struct SndfileInputStream {
|
2016-11-18 07:13:35 +01:00
|
|
|
DecoderClient *const client;
|
2014-07-11 20:50:44 +02:00
|
|
|
InputStream &is;
|
|
|
|
|
|
|
|
size_t Read(void *buffer, size_t size) {
|
2014-07-11 21:17:43 +02:00
|
|
|
/* libsndfile chokes on partial reads; therefore
|
|
|
|
always force full reads */
|
2016-11-18 07:13:35 +01:00
|
|
|
return decoder_read_full(client, is, buffer, size)
|
2014-07-11 21:17:43 +02:00
|
|
|
? size
|
|
|
|
: 0;
|
2014-07-11 20:50:44 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-07-07 08:58:51 +02:00
|
|
|
static sf_count_t
|
|
|
|
sndfile_vio_get_filelen(void *user_data)
|
|
|
|
{
|
2014-07-11 20:50:44 +02:00
|
|
|
SndfileInputStream &sis = *(SndfileInputStream *)user_data;
|
|
|
|
const InputStream &is = sis.is;
|
2009-07-07 08:58:51 +02:00
|
|
|
|
2014-08-19 20:57:25 +02:00
|
|
|
if (!is.KnownSize())
|
|
|
|
return -1;
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
return is.GetSize();
|
2009-07-07 08:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static sf_count_t
|
2014-05-22 10:10:16 +02:00
|
|
|
sndfile_vio_seek(sf_count_t _offset, int whence, void *user_data)
|
2009-07-07 08:58:51 +02:00
|
|
|
{
|
2014-07-11 20:50:44 +02:00
|
|
|
SndfileInputStream &sis = *(SndfileInputStream *)user_data;
|
|
|
|
InputStream &is = sis.is;
|
2009-07-07 08:58:51 +02:00
|
|
|
|
2014-08-19 22:29:52 +02:00
|
|
|
offset_type offset = _offset;
|
2014-05-22 10:10:16 +02:00
|
|
|
switch (whence) {
|
|
|
|
case SEEK_SET:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR:
|
|
|
|
offset += is.GetOffset();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
|
|
|
if (!is.KnownSize())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
offset += is.GetSize();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
try {
|
|
|
|
is.LockSeek(offset);
|
|
|
|
return is.GetOffset();
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception(), "Seek failed");
|
2009-07-07 08:58:51 +02:00
|
|
|
return -1;
|
2014-07-11 21:11:33 +02:00
|
|
|
}
|
2009-07-07 08:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static sf_count_t
|
|
|
|
sndfile_vio_read(void *ptr, sf_count_t count, void *user_data)
|
|
|
|
{
|
2014-07-11 20:50:44 +02:00
|
|
|
SndfileInputStream &sis = *(SndfileInputStream *)user_data;
|
2014-04-09 23:58:56 +02:00
|
|
|
|
2014-07-11 21:17:43 +02:00
|
|
|
return sis.Read(ptr, count);
|
2009-07-07 08:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static sf_count_t
|
2013-08-04 23:48:01 +02:00
|
|
|
sndfile_vio_write(gcc_unused const void *ptr,
|
|
|
|
gcc_unused sf_count_t count,
|
|
|
|
gcc_unused void *user_data)
|
2009-07-07 08:58:51 +02:00
|
|
|
{
|
|
|
|
/* no writing! */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sf_count_t
|
|
|
|
sndfile_vio_tell(void *user_data)
|
|
|
|
{
|
2014-07-11 20:50:44 +02:00
|
|
|
SndfileInputStream &sis = *(SndfileInputStream *)user_data;
|
|
|
|
const InputStream &is = sis.is;
|
2009-07-07 08:58:51 +02:00
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
return is.GetOffset();
|
2009-07-07 08:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-31 22:17:15 +01:00
|
|
|
* This SF_VIRTUAL_IO implementation wraps MPD's #InputStream to a
|
2009-07-07 08:58:51 +02:00
|
|
|
* libsndfile stream.
|
|
|
|
*/
|
|
|
|
static SF_VIRTUAL_IO vio = {
|
2013-07-28 12:42:06 +02:00
|
|
|
sndfile_vio_get_filelen,
|
|
|
|
sndfile_vio_seek,
|
|
|
|
sndfile_vio_read,
|
|
|
|
sndfile_vio_write,
|
|
|
|
sndfile_vio_tell,
|
2009-07-07 08:58:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a frame number to a timestamp (in seconds).
|
|
|
|
*/
|
2014-08-30 00:53:14 +02:00
|
|
|
static constexpr SongTime
|
|
|
|
sndfile_duration(const SF_INFO &info)
|
2009-07-07 08:58:51 +02:00
|
|
|
{
|
2014-08-30 00:53:14 +02:00
|
|
|
return SongTime::FromScale<uint64_t>(info.frames, info.samplerate);
|
2009-07-07 08:58:51 +02:00
|
|
|
}
|
|
|
|
|
2014-09-19 21:06:44 +02:00
|
|
|
gcc_pure
|
|
|
|
static SampleFormat
|
2017-05-08 14:44:49 +02:00
|
|
|
sndfile_sample_format(const SF_INFO &info) noexcept
|
2014-09-19 21:06:44 +02:00
|
|
|
{
|
2014-09-19 21:40:22 +02:00
|
|
|
switch (info.format & SF_FORMAT_SUBMASK) {
|
|
|
|
case SF_FORMAT_PCM_S8:
|
|
|
|
case SF_FORMAT_PCM_U8:
|
|
|
|
case SF_FORMAT_PCM_16:
|
|
|
|
return SampleFormat::S16;
|
|
|
|
|
|
|
|
case SF_FORMAT_FLOAT:
|
|
|
|
case SF_FORMAT_DOUBLE:
|
|
|
|
return SampleFormat::FLOAT;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return SampleFormat::S32;
|
|
|
|
}
|
2014-09-19 21:06:44 +02:00
|
|
|
}
|
|
|
|
|
2014-09-19 21:42:06 +02:00
|
|
|
static sf_count_t
|
2014-09-19 21:40:22 +02:00
|
|
|
sndfile_read_frames(SNDFILE *sf, SampleFormat format,
|
|
|
|
void *buffer, sf_count_t n_frames)
|
2014-09-19 21:42:06 +02:00
|
|
|
{
|
2014-09-19 21:40:22 +02:00
|
|
|
switch (format) {
|
|
|
|
case SampleFormat::S16:
|
|
|
|
return sf_readf_short(sf, (short *)buffer, n_frames);
|
|
|
|
|
|
|
|
case SampleFormat::S32:
|
|
|
|
return sf_readf_int(sf, (int *)buffer, n_frames);
|
|
|
|
|
|
|
|
case SampleFormat::FLOAT:
|
|
|
|
return sf_readf_float(sf, (float *)buffer, n_frames);
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
gcc_unreachable();
|
|
|
|
}
|
2014-09-19 21:42:06 +02:00
|
|
|
}
|
|
|
|
|
2009-07-07 08:58:51 +02:00
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
sndfile_stream_decode(DecoderClient &client, InputStream &is)
|
2009-07-07 08:58:51 +02:00
|
|
|
{
|
|
|
|
SF_INFO info;
|
|
|
|
|
|
|
|
info.format = 0;
|
|
|
|
|
2016-11-18 07:13:35 +01:00
|
|
|
SndfileInputStream sis{&client, is};
|
2014-07-11 21:51:07 +02:00
|
|
|
SNDFILE *const sf = sf_open_virtual(&vio, SFM_READ, &info, &sis);
|
2013-07-28 12:42:06 +02:00
|
|
|
if (sf == nullptr) {
|
2014-09-19 21:51:24 +02:00
|
|
|
FormatWarning(sndfile_domain, "sf_open_virtual() failed: %s",
|
|
|
|
sf_strerror(nullptr));
|
2009-07-07 08:58:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-10 11:45:17 +01:00
|
|
|
const auto audio_format =
|
|
|
|
CheckAudioFormat(info.samplerate,
|
|
|
|
sndfile_sample_format(info),
|
|
|
|
info.channels);
|
2009-07-07 08:58:51 +02:00
|
|
|
|
2016-11-18 07:59:01 +01:00
|
|
|
client.Ready(audio_format, info.seekable, sndfile_duration(info));
|
2009-07-07 08:58:51 +02:00
|
|
|
|
2014-09-19 21:42:06 +02:00
|
|
|
char buffer[16384];
|
2014-07-11 21:51:07 +02:00
|
|
|
|
|
|
|
const size_t frame_size = audio_format.GetFrameSize();
|
|
|
|
const sf_count_t read_frames = sizeof(buffer) / frame_size;
|
2009-07-07 08:58:51 +02:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand cmd;
|
2009-07-07 08:58:51 +02:00
|
|
|
do {
|
2014-09-19 21:42:06 +02:00
|
|
|
sf_count_t num_frames =
|
|
|
|
sndfile_read_frames(sf,
|
2014-09-19 21:40:22 +02:00
|
|
|
audio_format.format,
|
2014-09-19 21:42:06 +02:00
|
|
|
buffer, read_frames);
|
2009-07-07 08:58:51 +02:00
|
|
|
if (num_frames <= 0)
|
|
|
|
break;
|
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
cmd = client.SubmitData(is,
|
|
|
|
buffer, num_frames * frame_size,
|
|
|
|
0);
|
2013-09-27 12:11:37 +02:00
|
|
|
if (cmd == DecoderCommand::SEEK) {
|
2016-11-18 08:15:07 +01:00
|
|
|
sf_count_t c = client.GetSeekFrame();
|
2009-07-07 08:58:51 +02:00
|
|
|
c = sf_seek(sf, c, SEEK_SET);
|
|
|
|
if (c < 0)
|
2016-11-18 08:15:07 +01:00
|
|
|
client.SeekError();
|
2009-07-07 08:58:51 +02:00
|
|
|
else
|
2016-11-18 08:15:07 +01:00
|
|
|
client.CommandFinished();
|
2013-09-27 12:11:37 +02:00
|
|
|
cmd = DecoderCommand::NONE;
|
2009-07-07 08:58:51 +02:00
|
|
|
}
|
2013-09-27 12:11:37 +02:00
|
|
|
} while (cmd == DecoderCommand::NONE);
|
2009-07-07 08:58:51 +02:00
|
|
|
|
|
|
|
sf_close(sf);
|
|
|
|
}
|
|
|
|
|
2014-07-11 21:56:02 +02:00
|
|
|
static void
|
|
|
|
sndfile_handle_tag(SNDFILE *sf, int str, TagType tag,
|
2016-02-23 10:10:13 +01:00
|
|
|
const TagHandler &handler, void *handler_ctx)
|
2014-07-11 21:56:02 +02:00
|
|
|
{
|
|
|
|
const char *value = sf_get_string(sf, str);
|
|
|
|
if (value != nullptr)
|
|
|
|
tag_handler_invoke_tag(handler, handler_ctx, tag, value);
|
|
|
|
}
|
|
|
|
|
2014-07-11 21:57:41 +02:00
|
|
|
static constexpr struct {
|
|
|
|
int8_t str;
|
|
|
|
TagType tag;
|
|
|
|
} sndfile_tags[] = {
|
|
|
|
{ SF_STR_TITLE, TAG_TITLE },
|
|
|
|
{ SF_STR_ARTIST, TAG_ARTIST },
|
2014-07-11 22:01:11 +02:00
|
|
|
{ SF_STR_COMMENT, TAG_COMMENT },
|
2014-07-11 21:57:41 +02:00
|
|
|
{ SF_STR_DATE, TAG_DATE },
|
2014-07-11 22:01:11 +02:00
|
|
|
{ SF_STR_ALBUM, TAG_ALBUM },
|
|
|
|
{ SF_STR_TRACKNUMBER, TAG_TRACK },
|
|
|
|
{ SF_STR_GENRE, TAG_GENRE },
|
2014-07-11 21:57:41 +02:00
|
|
|
};
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
2014-07-11 22:09:35 +02:00
|
|
|
sndfile_scan_stream(InputStream &is,
|
2016-02-23 10:10:13 +01:00
|
|
|
const TagHandler &handler, void *handler_ctx)
|
2009-07-07 08:58:51 +02:00
|
|
|
{
|
|
|
|
SF_INFO info;
|
|
|
|
|
|
|
|
info.format = 0;
|
|
|
|
|
2014-07-11 22:09:35 +02:00
|
|
|
SndfileInputStream sis{nullptr, is};
|
|
|
|
SNDFILE *const sf = sf_open_virtual(&vio, SFM_READ, &info, &sis);
|
2013-07-28 12:42:06 +02:00
|
|
|
if (sf == nullptr)
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2009-07-07 08:58:51 +02:00
|
|
|
|
|
|
|
if (!audio_valid_sample_rate(info.samplerate)) {
|
|
|
|
sf_close(sf);
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(sndfile_domain,
|
2014-07-11 22:09:35 +02:00
|
|
|
"Invalid sample rate in %s", is.GetURI());
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2009-07-07 08:58:51 +02:00
|
|
|
}
|
|
|
|
|
2014-08-30 00:53:14 +02:00
|
|
|
tag_handler_invoke_duration(handler, handler_ctx,
|
|
|
|
sndfile_duration(info));
|
2009-07-07 08:58:51 +02:00
|
|
|
|
2014-07-11 21:57:41 +02:00
|
|
|
for (auto i : sndfile_tags)
|
|
|
|
sndfile_handle_tag(sf, i.str, i.tag, handler, handler_ctx);
|
2009-07-07 08:58:51 +02:00
|
|
|
|
|
|
|
sf_close(sf);
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
return true;
|
2009-07-07 08:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const sndfile_suffixes[] = {
|
|
|
|
"wav", "aiff", "aif", /* Microsoft / SGI / Apple */
|
|
|
|
"au", "snd", /* Sun / DEC / NeXT */
|
|
|
|
"paf", /* Paris Audio File */
|
|
|
|
"iff", "svx", /* Commodore Amiga IFF / SVX */
|
|
|
|
"sf", /* IRCAM */
|
|
|
|
"voc", /* Creative */
|
|
|
|
"w64", /* Soundforge */
|
|
|
|
"pvf", /* Portable Voice Format */
|
|
|
|
"xi", /* Fasttracker */
|
|
|
|
"htk", /* HMM Tool Kit */
|
|
|
|
"caf", /* Apple */
|
|
|
|
"sd2", /* Sound Designer II */
|
|
|
|
|
|
|
|
/* libsndfile also supports FLAC and Ogg Vorbis, but only by
|
|
|
|
linking with libFLAC and libvorbis - we can do better, we
|
|
|
|
have native plugins for these libraries */
|
|
|
|
|
2013-07-28 12:42:06 +02:00
|
|
|
nullptr
|
2009-07-07 08:58:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const sndfile_mime_types[] = {
|
|
|
|
"audio/x-wav",
|
|
|
|
"audio/x-aiff",
|
|
|
|
|
|
|
|
/* what are the MIME types of the other supported formats? */
|
|
|
|
|
2013-07-28 12:42:06 +02:00
|
|
|
nullptr
|
2009-07-07 08:58:51 +02:00
|
|
|
};
|
|
|
|
|
2013-10-21 20:31:34 +02:00
|
|
|
const struct DecoderPlugin sndfile_decoder_plugin = {
|
2013-07-28 12:42:06 +02:00
|
|
|
"sndfile",
|
2014-07-11 21:53:03 +02:00
|
|
|
sndfile_init,
|
2013-07-28 12:42:06 +02:00
|
|
|
nullptr,
|
|
|
|
sndfile_stream_decode,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2014-07-11 22:09:35 +02:00
|
|
|
sndfile_scan_stream,
|
2013-07-28 12:42:06 +02:00
|
|
|
nullptr,
|
|
|
|
sndfile_suffixes,
|
|
|
|
sndfile_mime_types,
|
2009-07-07 08:58:51 +02:00
|
|
|
};
|