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

20
#include "ModplugDecoderPlugin.hxx"
21
#include "../DecoderAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "input/InputStream.hxx"
23
#include "tag/Handler.hxx"
24
#include "util/WritableBuffer.hxx"
25
#include "util/Domain.hxx"
26
#include "util/RuntimeError.hxx"
27
#include "util/StringView.hxx"
28
#include "Log.hxx"
29

30 31 32 33 34
#ifdef _WIN32
/* assume ModPlug is built as static library on Windows; without
   this, linking to the static library would fail */
#define MODPLUG_STATIC
#endif
35 36 37

#include <libmodplug/modplug.h>

38
#include <cassert>
39

40
static constexpr Domain modplug_domain("modplug");
41

42 43
static constexpr size_t MODPLUG_FRAME_SIZE = 4096;
static constexpr size_t MODPLUG_PREALLOC_BLOCK = 256 * 1024;
44
static constexpr offset_type MODPLUG_FILE_LIMIT = 100 * 1024 * 1024;
45

46 47 48
static int modplug_loop_count;

static bool
49
modplug_decoder_init(const ConfigBlock &block)
50
{
51
	modplug_loop_count = block.GetBlockValue("loop_count", 0);
52
	if (modplug_loop_count < -1)
53 54
		throw FormatRuntimeError("Invalid loop count in line %d: %i",
					 block.line, modplug_loop_count);
55 56 57 58

	return true;
}

59
static WritableBuffer<uint8_t>
60
mod_loadfile(DecoderClient *client, InputStream &is)
61
{
62
	//known/unknown size, preallocate array, lets read in chunks
63

64
	const bool is_stream = !is.KnownSize();
65

66 67 68 69 70 71 72 73
	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");
74
			return nullptr;
75
		}
76

77 78
		if (size > MODPLUG_FILE_LIMIT) {
			LogWarning(modplug_domain, "file too large");
79
			return nullptr;
80
		}
81

82 83
		buffer.size = size;
	}
84 85 86 87 88

	buffer.data = new uint8_t[buffer.size];

	uint8_t *const end = buffer.end();
	uint8_t *p = buffer.begin();
89 90

	while (true) {
91
		size_t ret = decoder_read(client, is, p, end - p);
92
		if (ret == 0) {
93
			if (is.LockIsEOF())
94 95 96 97
				/* end of file */
				break;

			/* I/O error - skip this song */
98 99 100
			delete[] buffer.data;
			buffer.data = nullptr;
			return buffer;
101
		}
102

103 104 105 106 107
		p += ret;
		if (p == end) {
			if (!is_stream)
				break;

108
			LogWarning(modplug_domain, "stream too large");
109 110 111
			delete[] buffer.data;
			buffer.data = nullptr;
			return buffer;
112
		}
113 114
	}

115 116
	buffer.size = p - buffer.data;
	return buffer;
117 118
}

119
static ModPlugFile *
120
LoadModPlugFile(DecoderClient *client, InputStream &is)
121
{
122
	const auto buffer = mod_loadfile(client, is);
123
	if (buffer.IsNull()) {
124 125 126 127
		LogWarning(modplug_domain, "could not load stream");
		return nullptr;
	}

128 129
	ModPlugFile *f = ModPlug_Load(buffer.data, buffer.size);
	delete[] buffer.data;
130 131 132
	return f;
}

133
static void
134
mod_decode(DecoderClient &client, InputStream &is)
135 136
{
	ModPlug_Settings settings;
137
	int ret;
138 139 140 141 142 143 144 145
	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;
146
	settings.mLoopCount = modplug_loop_count;
147 148 149
	/* insert more setting changes here */
	ModPlug_SetSettings(&settings);

150
	ModPlugFile *f = LoadModPlugFile(&client, is);
151
	if (f == nullptr) {
152
		LogWarning(modplug_domain, "could not decode stream");
153 154 155
		return;
	}

156 157
	static constexpr AudioFormat audio_format(44100, SampleFormat::S16, 2);
	assert(audio_format.IsValid());
158

159 160
	client.Ready(audio_format, is.IsSeekable(),
		     SongTime::FromMS(ModPlug_GetLength(f)));
161

162
	DecoderCommand cmd;
163 164
	do {
		ret = ModPlug_Read(f, audio_buffer, MODPLUG_FRAME_SIZE);
165
		if (ret <= 0)
166 167
			break;

168 169 170
		cmd = client.SubmitData(nullptr,
					audio_buffer, ret,
					0);
171

172
		if (cmd == DecoderCommand::SEEK) {
173 174
			ModPlug_Seek(f, client.GetSeekTime().ToMS());
			client.CommandFinished();
175 176
		}

177
	} while (cmd != DecoderCommand::STOP);
178 179 180 181

	ModPlug_Unload(f);
}

182
static bool
183
modplug_scan_stream(InputStream &is, TagHandler &handler) noexcept
184
{
185
	ModPlugFile *f = LoadModPlugFile(nullptr, is);
186
	if (f == nullptr)
187
		return false;
188

189
	handler.OnDuration(SongTime::FromMS(ModPlug_GetLength(f)));
190

191
	const char *title = ModPlug_GetName(f);
192
	if (title != nullptr)
193
		handler.OnTag(TAG_TITLE, title);
194 195 196

	ModPlug_Unload(f);

197
	return true;
198 199 200 201 202 203
}

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",
204
	nullptr
205 206
};

207 208 209 210
constexpr DecoderPlugin modplug_decoder_plugin =
	DecoderPlugin("modplug", mod_decode, modplug_scan_stream)
	.WithInit(modplug_decoder_init)
	.WithSuffixes(mod_suffixes);