ModplugDecoderPlugin.cxx 4.93 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "config.h"
21
#include "ModplugDecoderPlugin.hxx"
22
#include "../DecoderAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "input/InputStream.hxx"
24
#include "tag/Handler.hxx"
25
#include "util/WritableBuffer.hxx"
26
#include "util/Domain.hxx"
27
#include "util/RuntimeError.hxx"
28
#include "Log.hxx"
29

30 31
#include <libmodplug/modplug.h>

32

33
#include <assert.h>
34

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

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

41 42 43
static int modplug_loop_count;

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

	return true;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	ModPlug_Unload(f);
}

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

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

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

	ModPlug_Unload(f);

192
	return true;
193 194 195 196 197 198
}

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

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