2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-12-28 17:11:18 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-12-28 17:11:18 +01:00
|
|
|
*/
|
|
|
|
|
2013-07-28 12:56:35 +02:00
|
|
|
#include "ModplugDecoderPlugin.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "../DecoderAPI.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/InputStream.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Handler.hxx"
|
2013-10-21 18:26:14 +02:00
|
|
|
#include "util/WritableBuffer.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
2016-11-24 14:09:58 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2019-06-06 12:02:55 +02:00
|
|
|
#include "util/StringView.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2020-05-27 14:29:24 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* assume ModPlug is built as static library on Windows; without
|
|
|
|
this, linking to the static library would fail */
|
|
|
|
#define MODPLUG_STATIC
|
|
|
|
#endif
|
2013-09-26 17:14:25 +02:00
|
|
|
|
|
|
|
#include <libmodplug/modplug.h>
|
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain modplug_domain("modplug");
|
2009-01-24 19:15:53 +01:00
|
|
|
|
2013-07-28 12:56:35 +02:00
|
|
|
static constexpr size_t MODPLUG_FRAME_SIZE = 4096;
|
|
|
|
static constexpr size_t MODPLUG_PREALLOC_BLOCK = 256 * 1024;
|
2014-08-19 22:29:52 +02:00
|
|
|
static constexpr offset_type MODPLUG_FILE_LIMIT = 100 * 1024 * 1024;
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2013-10-19 15:39:04 +02:00
|
|
|
static int modplug_loop_count;
|
|
|
|
|
|
|
|
static bool
|
2015-01-21 22:13:44 +01:00
|
|
|
modplug_decoder_init(const ConfigBlock &block)
|
2013-10-19 15:39:04 +02:00
|
|
|
{
|
2015-01-21 22:13:44 +01:00
|
|
|
modplug_loop_count = block.GetBlockValue("loop_count", 0);
|
2013-10-19 15:39:04 +02:00
|
|
|
if (modplug_loop_count < -1)
|
2016-11-24 14:09:58 +01:00
|
|
|
throw FormatRuntimeError("Invalid loop count in line %d: %i",
|
|
|
|
block.line, modplug_loop_count);
|
2013-10-19 15:39:04 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-21 18:26:14 +02:00
|
|
|
static WritableBuffer<uint8_t>
|
2016-11-18 07:13:35 +01:00
|
|
|
mod_loadfile(DecoderClient *client, InputStream &is)
|
2008-12-28 17:11:18 +01:00
|
|
|
{
|
2014-08-19 20:53:02 +02:00
|
|
|
//known/unknown size, preallocate array, lets read in chunks
|
2013-01-24 19:14:40 +01:00
|
|
|
|
2014-08-19 20:53:02 +02:00
|
|
|
const bool is_stream = !is.KnownSize();
|
2009-01-24 19:16:07 +01:00
|
|
|
|
2014-08-19 20:53:02 +02:00
|
|
|
WritableBuffer<uint8_t> buffer;
|
|
|
|
if (is_stream)
|
|
|
|
buffer.size = MODPLUG_PREALLOC_BLOCK;
|
|
|
|
else {
|
|
|
|
const auto size = is.GetSize();
|
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
LogWarning(modplug_domain, "file is empty");
|
2017-11-21 12:43:09 +01:00
|
|
|
return nullptr;
|
2014-08-19 20:53:02 +02:00
|
|
|
}
|
2009-01-24 19:16:07 +01:00
|
|
|
|
2014-08-19 20:53:02 +02:00
|
|
|
if (size > MODPLUG_FILE_LIMIT) {
|
|
|
|
LogWarning(modplug_domain, "file too large");
|
2017-11-21 12:43:09 +01:00
|
|
|
return nullptr;
|
2014-08-19 20:53:02 +02:00
|
|
|
}
|
2009-01-24 19:16:07 +01:00
|
|
|
|
2014-08-19 20:53:02 +02:00
|
|
|
buffer.size = size;
|
|
|
|
}
|
2013-10-21 18:26:14 +02:00
|
|
|
|
|
|
|
buffer.data = new uint8_t[buffer.size];
|
|
|
|
|
|
|
|
uint8_t *const end = buffer.end();
|
|
|
|
uint8_t *p = buffer.begin();
|
2009-01-24 20:02:06 +01:00
|
|
|
|
|
|
|
while (true) {
|
2016-11-18 07:13:35 +01:00
|
|
|
size_t ret = decoder_read(client, is, p, end - p);
|
2009-01-24 19:16:20 +01:00
|
|
|
if (ret == 0) {
|
2013-10-23 22:08:59 +02:00
|
|
|
if (is.LockIsEOF())
|
2009-01-24 19:16:33 +01:00
|
|
|
/* end of file */
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* I/O error - skip this song */
|
2013-10-21 18:26:14 +02:00
|
|
|
delete[] buffer.data;
|
|
|
|
buffer.data = nullptr;
|
|
|
|
return buffer;
|
2008-12-28 17:11:18 +01:00
|
|
|
}
|
2009-01-24 19:16:10 +01:00
|
|
|
|
2013-10-21 18:26:14 +02:00
|
|
|
p += ret;
|
|
|
|
if (p == end) {
|
|
|
|
if (!is_stream)
|
|
|
|
break;
|
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(modplug_domain, "stream too large");
|
2013-10-21 18:26:14 +02:00
|
|
|
delete[] buffer.data;
|
|
|
|
buffer.data = nullptr;
|
|
|
|
return buffer;
|
2008-12-28 17:11:18 +01:00
|
|
|
}
|
2009-01-24 20:02:06 +01:00
|
|
|
}
|
|
|
|
|
2013-10-21 18:26:14 +02:00
|
|
|
buffer.size = p - buffer.data;
|
|
|
|
return buffer;
|
2008-12-28 17:11:18 +01:00
|
|
|
}
|
|
|
|
|
2013-10-21 20:19:27 +02:00
|
|
|
static ModPlugFile *
|
2016-11-18 07:13:35 +01:00
|
|
|
LoadModPlugFile(DecoderClient *client, InputStream &is)
|
2013-10-21 20:19:27 +02:00
|
|
|
{
|
2016-11-18 07:13:35 +01:00
|
|
|
const auto buffer = mod_loadfile(client, is);
|
2013-10-21 18:26:14 +02:00
|
|
|
if (buffer.IsNull()) {
|
2013-10-21 20:19:27 +02:00
|
|
|
LogWarning(modplug_domain, "could not load stream");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-21 18:26:14 +02:00
|
|
|
ModPlugFile *f = ModPlug_Load(buffer.data, buffer.size);
|
|
|
|
delete[] buffer.data;
|
2013-10-21 20:19:27 +02:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2008-12-28 17:11:18 +01:00
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
mod_decode(DecoderClient &client, InputStream &is)
|
2008-12-28 17:11:18 +01:00
|
|
|
{
|
|
|
|
ModPlug_Settings settings;
|
2009-11-14 22:30:57 +01:00
|
|
|
int ret;
|
2008-12-28 17:11:18 +01:00
|
|
|
char audio_buffer[MODPLUG_FRAME_SIZE];
|
|
|
|
|
|
|
|
ModPlug_GetSettings(&settings);
|
|
|
|
/* alter setting */
|
|
|
|
settings.mResamplingMode = MODPLUG_RESAMPLE_FIR; /* RESAMP */
|
|
|
|
settings.mChannels = 2;
|
|
|
|
settings.mBits = 16;
|
|
|
|
settings.mFrequency = 44100;
|
2013-10-19 15:39:04 +02:00
|
|
|
settings.mLoopCount = modplug_loop_count;
|
2008-12-28 17:11:18 +01:00
|
|
|
/* insert more setting changes here */
|
|
|
|
ModPlug_SetSettings(&settings);
|
|
|
|
|
2016-11-18 07:13:35 +01:00
|
|
|
ModPlugFile *f = LoadModPlugFile(&client, is);
|
2013-10-21 20:19:27 +02:00
|
|
|
if (f == nullptr) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(modplug_domain, "could not decode stream");
|
2009-01-23 18:50:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
static constexpr AudioFormat audio_format(44100, SampleFormat::S16, 2);
|
|
|
|
assert(audio_format.IsValid());
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2016-11-18 07:59:01 +01:00
|
|
|
client.Ready(audio_format, is.IsSeekable(),
|
|
|
|
SongTime::FromMS(ModPlug_GetLength(f)));
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand cmd;
|
2008-12-28 17:11:18 +01:00
|
|
|
do {
|
|
|
|
ret = ModPlug_Read(f, audio_buffer, MODPLUG_FRAME_SIZE);
|
2009-11-14 22:27:04 +01:00
|
|
|
if (ret <= 0)
|
2008-12-28 17:11:18 +01:00
|
|
|
break;
|
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
cmd = client.SubmitData(nullptr,
|
|
|
|
audio_buffer, ret,
|
|
|
|
0);
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
if (cmd == DecoderCommand::SEEK) {
|
2016-11-18 08:15:07 +01:00
|
|
|
ModPlug_Seek(f, client.GetSeekTime().ToMS());
|
|
|
|
client.CommandFinished();
|
2008-12-28 17:11:18 +01:00
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
} while (cmd != DecoderCommand::STOP);
|
2008-12-28 17:11:18 +01:00
|
|
|
|
|
|
|
ModPlug_Unload(f);
|
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
2018-07-05 19:07:05 +02:00
|
|
|
modplug_scan_stream(InputStream &is, TagHandler &handler) noexcept
|
2008-12-28 17:11:18 +01:00
|
|
|
{
|
2013-10-21 20:19:27 +02:00
|
|
|
ModPlugFile *f = LoadModPlugFile(nullptr, is);
|
2013-07-28 12:56:35 +02:00
|
|
|
if (f == nullptr)
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2009-12-31 18:20:08 +01:00
|
|
|
|
2018-07-05 19:07:05 +02:00
|
|
|
handler.OnDuration(SongTime::FromMS(ModPlug_GetLength(f)));
|
2008-12-28 17:11:18 +01:00
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
const char *title = ModPlug_GetName(f);
|
2013-07-28 12:56:35 +02:00
|
|
|
if (title != nullptr)
|
2018-07-05 19:07:05 +02:00
|
|
|
handler.OnTag(TAG_TITLE, title);
|
2008-12-28 17:11:18 +01:00
|
|
|
|
|
|
|
ModPlug_Unload(f);
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
return true;
|
2008-12-28 17:11:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const mod_suffixes[] = {
|
|
|
|
"669", "amf", "ams", "dbm", "dfm", "dsm", "far", "it",
|
|
|
|
"med", "mdl", "mod", "mtm", "mt2", "okt", "s3m", "stm",
|
|
|
|
"ult", "umx", "xm",
|
2013-07-28 12:56:35 +02:00
|
|
|
nullptr
|
2008-12-28 17:11:18 +01:00
|
|
|
};
|
|
|
|
|
2019-06-15 14:44:37 +02:00
|
|
|
constexpr DecoderPlugin modplug_decoder_plugin =
|
|
|
|
DecoderPlugin("modplug", mod_decode, modplug_scan_stream)
|
|
|
|
.WithInit(modplug_decoder_init)
|
|
|
|
.WithSuffixes(mod_suffixes);
|