ModplugDecoderPlugin.cxx 4.91 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "Log.hxx"
28

29 30
#include <libmodplug/modplug.h>

31

32
#include <assert.h>
33

34
static constexpr Domain modplug_domain("modplug");
35

36 37
static constexpr size_t MODPLUG_FRAME_SIZE = 4096;
static constexpr size_t MODPLUG_PREALLOC_BLOCK = 256 * 1024;
38
static constexpr offset_type MODPLUG_FILE_LIMIT = 100 * 1024 * 1024;
39

40 41 42
static int modplug_loop_count;

static bool
43
modplug_decoder_init(const ConfigBlock &block)
44
{
45
	modplug_loop_count = block.GetBlockValue("loop_count", 0);
46
	if (modplug_loop_count < -1)
47 48
		throw FormatRuntimeError("Invalid loop count in line %d: %i",
					 block.line, modplug_loop_count);
49 50 51 52

	return true;
}

53
static WritableBuffer<uint8_t>
54
mod_loadfile(DecoderClient *client, InputStream &is)
55
{
56
	//known/unknown size, preallocate array, lets read in chunks
57

58
	const bool is_stream = !is.KnownSize();
59

60 61 62 63 64 65 66 67
	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");
68
			return nullptr;
69
		}
70

71 72
		if (size > MODPLUG_FILE_LIMIT) {
			LogWarning(modplug_domain, "file too large");
73
			return nullptr;
74
		}
75

76 77
		buffer.size = size;
	}
78 79 80 81 82

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

	uint8_t *const end = buffer.end();
	uint8_t *p = buffer.begin();
83 84

	while (true) {
85
		size_t ret = decoder_read(client, is, p, end - p);
86
		if (ret == 0) {
87
			if (is.LockIsEOF())
88 89 90 91
				/* end of file */
				break;

			/* I/O error - skip this song */
92 93 94
			delete[] buffer.data;
			buffer.data = nullptr;
			return buffer;
95
		}
96

97 98 99 100 101
		p += ret;
		if (p == end) {
			if (!is_stream)
				break;

102
			LogWarning(modplug_domain, "stream too large");
103 104 105
			delete[] buffer.data;
			buffer.data = nullptr;
			return buffer;
106
		}
107 108
	}

109 110
	buffer.size = p - buffer.data;
	return buffer;
111 112
}

113
static ModPlugFile *
114
LoadModPlugFile(DecoderClient *client, InputStream &is)
115
{
116
	const auto buffer = mod_loadfile(client, is);
117
	if (buffer.IsNull()) {
118 119 120 121
		LogWarning(modplug_domain, "could not load stream");
		return nullptr;
	}

122 123
	ModPlugFile *f = ModPlug_Load(buffer.data, buffer.size);
	delete[] buffer.data;
124 125 126
	return f;
}

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

144
	ModPlugFile *f = LoadModPlugFile(&client, is);
145
	if (f == nullptr) {
146
		LogWarning(modplug_domain, "could not decode stream");
147 148 149
		return;
	}

150 151
	static constexpr AudioFormat audio_format(44100, SampleFormat::S16, 2);
	assert(audio_format.IsValid());
152

153 154
	client.Ready(audio_format, is.IsSeekable(),
		     SongTime::FromMS(ModPlug_GetLength(f)));
155

156
	DecoderCommand cmd;
157 158
	do {
		ret = ModPlug_Read(f, audio_buffer, MODPLUG_FRAME_SIZE);
159
		if (ret <= 0)
160 161
			break;

162 163 164
		cmd = client.SubmitData(nullptr,
					audio_buffer, ret,
					0);
165

166
		if (cmd == DecoderCommand::SEEK) {
167 168
			ModPlug_Seek(f, client.GetSeekTime().ToMS());
			client.CommandFinished();
169 170
		}

171
	} while (cmd != DecoderCommand::STOP);
172 173 174 175

	ModPlug_Unload(f);
}

176
static bool
177
modplug_scan_stream(InputStream &is, TagHandler &handler) noexcept
178
{
179
	ModPlugFile *f = LoadModPlugFile(nullptr, is);
180
	if (f == nullptr)
181
		return false;
182

183
	handler.OnDuration(SongTime::FromMS(ModPlug_GetLength(f)));
184

185
	const char *title = ModPlug_GetName(f);
186
	if (title != nullptr)
187
		handler.OnTag(TAG_TITLE, title);
188 189 190

	ModPlug_Unload(f);

191
	return true;
192 193 194 195 196 197
}

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",
198
	nullptr
199 200
};

201
const struct DecoderPlugin modplug_decoder_plugin = {
202
	"modplug",
203
	modplug_decoder_init,
204 205 206 207 208 209 210 211
	nullptr,
	mod_decode,
	nullptr,
	nullptr,
	modplug_scan_stream,
	nullptr,
	mod_suffixes,
	nullptr,
212
};