ModplugDecoderPlugin.cxx 5.08 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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/TagHandler.hxx"
25
#include "system/FatalError.hxx"
26
#include "util/WritableBuffer.hxx"
27 28
#include "util/Domain.hxx"
#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 InputStream::offset_type MODPLUG_FILE_LIMIT = 100 * 1024 * 1024;
40

41 42 43 44 45 46 47 48 49 50 51 52 53
static int modplug_loop_count;

static bool
modplug_decoder_init(const config_param &param)
{
	modplug_loop_count = param.GetBlockValue("loop_count", 0);
	if (modplug_loop_count < -1)
		FormatFatalError("Invalid loop count in line %d: %i",
				 param.line, modplug_loop_count);

	return true;
}

54
static WritableBuffer<uint8_t>
55
mod_loadfile(Decoder *decoder, InputStream &is)
56
{
57
	const InputStream::offset_type size = is.GetSize();
58 59

	if (size == 0) {
60
		LogWarning(modplug_domain, "file is empty");
61
		return { nullptr, 0 };
62 63
	}

64
	if (size > MODPLUG_FILE_LIMIT) {
65
		LogWarning(modplug_domain, "file too large");
66
		return { nullptr, 0 };
67 68
	}

69
	//known/unknown size, preallocate array, lets read in chunks
70

71 72 73 74 75 76 77 78
	const bool is_stream = size < 0;

	WritableBuffer<uint8_t> buffer;
	buffer.size = is_stream ? MODPLUG_PREALLOC_BLOCK : size;
	buffer.data = new uint8_t[buffer.size];

	uint8_t *const end = buffer.end();
	uint8_t *p = buffer.begin();
79 80

	while (true) {
81
		size_t ret = decoder_read(decoder, is, p, end - p);
82
		if (ret == 0) {
83
			if (is.LockIsEOF())
84 85 86 87
				/* end of file */
				break;

			/* I/O error - skip this song */
88 89 90
			delete[] buffer.data;
			buffer.data = nullptr;
			return buffer;
91
		}
92

93 94 95 96 97
		p += ret;
		if (p == end) {
			if (!is_stream)
				break;

98
			LogWarning(modplug_domain, "stream too large");
99 100 101
			delete[] buffer.data;
			buffer.data = nullptr;
			return buffer;
102
		}
103 104
	}

105 106
	buffer.size = p - buffer.data;
	return buffer;
107 108
}

109
static ModPlugFile *
110
LoadModPlugFile(Decoder *decoder, InputStream &is)
111
{
112 113
	const auto buffer = mod_loadfile(decoder, is);
	if (buffer.IsNull()) {
114 115 116 117
		LogWarning(modplug_domain, "could not load stream");
		return nullptr;
	}

118 119
	ModPlugFile *f = ModPlug_Load(buffer.data, buffer.size);
	delete[] buffer.data;
120 121 122
	return f;
}

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

140
	ModPlugFile *f = LoadModPlugFile(&decoder, is);
141
	if (f == nullptr) {
142
		LogWarning(modplug_domain, "could not decode stream");
143 144 145
		return;
	}

146 147
	static constexpr AudioFormat audio_format(44100, SampleFormat::S16, 2);
	assert(audio_format.IsValid());
148

149
	decoder_initialized(decoder, audio_format,
150
			    is.IsSeekable(),
151
			    ModPlug_GetLength(f) / 1000.0);
152

153
	DecoderCommand cmd;
154 155
	do {
		ret = ModPlug_Read(f, audio_buffer, MODPLUG_FRAME_SIZE);
156
		if (ret <= 0)
157 158
			break;

159
		cmd = decoder_data(decoder, nullptr,
160
				   audio_buffer, ret,
161
				   0);
162

163
		if (cmd == DecoderCommand::SEEK) {
164 165 166 167
			float where = decoder_seek_where(decoder);

			ModPlug_Seek(f, (int)(where * 1000.0));

168 169 170
			decoder_command_finished(decoder);
		}

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

	ModPlug_Unload(f);
}

176
static bool
177
modplug_scan_stream(InputStream &is,
178
		    const struct tag_handler *handler, void *handler_ctx)
179
{
180
	ModPlugFile *f = LoadModPlugFile(nullptr, is);
181
	if (f == nullptr)
182
		return false;
183

184 185
	tag_handler_invoke_duration(handler, handler_ctx,
				    ModPlug_GetLength(f) / 1000);
186

187
	const char *title = ModPlug_GetName(f);
188
	if (title != nullptr)
189 190
		tag_handler_invoke_tag(handler, handler_ctx,
				       TAG_TITLE, title);
191 192 193

	ModPlug_Unload(f);

194
	return true;
195 196 197 198 199 200
}

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",
201
	nullptr
202 203
};

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