summaryrefslogtreecommitdiff
path: root/src/decoder/plugins/ModplugDecoderPlugin.cxx
blob: fd1217e8f8c899f7688be98ca459b17d5695ab13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright 2003-2021 The Music Player Daemon Project
 * http://www.musicpd.org
 *
 * 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.
 *
 * 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.
 */

#include "ModplugDecoderPlugin.hxx"
#include "../DecoderAPI.hxx"
#include "input/InputStream.hxx"
#include "tag/Handler.hxx"
#include "util/WritableBuffer.hxx"
#include "util/Domain.hxx"
#include "util/RuntimeError.hxx"
#include "util/StringView.hxx"
#include "Log.hxx"

#ifdef _WIN32
/* assume ModPlug is built as static library on Windows; without
   this, linking to the static library would fail */
#define MODPLUG_STATIC
#endif

#include <libmodplug/modplug.h>

#include <cassert>

static constexpr Domain modplug_domain("modplug");

static constexpr size_t MODPLUG_FRAME_SIZE = 4096;
static constexpr size_t MODPLUG_PREALLOC_BLOCK = 256 * 1024;
static constexpr offset_type MODPLUG_FILE_LIMIT = 100 * 1024 * 1024;

static int modplug_loop_count;
static unsigned char modplug_resampling_mode;

static bool
modplug_decoder_init(const ConfigBlock &block)
{
	const char* modplug_resampling_mode_value = block.GetBlockValue("resampling_mode", "fir");
	if (strcmp(modplug_resampling_mode_value, "nearest") == 0) {
		modplug_resampling_mode = MODPLUG_RESAMPLE_NEAREST;
	} else if (strcmp(modplug_resampling_mode_value, "linear") == 0) {
		modplug_resampling_mode = MODPLUG_RESAMPLE_LINEAR;
	} else if (strcmp(modplug_resampling_mode_value, "spline") == 0) {
		modplug_resampling_mode = MODPLUG_RESAMPLE_SPLINE;
	} else if (strcmp(modplug_resampling_mode_value, "fir") == 0) {
		modplug_resampling_mode = MODPLUG_RESAMPLE_FIR;
	} else {
		throw FormatRuntimeError("Invalid resampling mode in line %d: %s",
				block.line, modplug_resampling_mode_value);
	}

	modplug_loop_count = block.GetBlockValue("loop_count", 0);
	if (modplug_loop_count < -1)
		throw FormatRuntimeError("Invalid loop count in line %d: %i",
					 block.line, modplug_loop_count);

	return true;
}

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

	const bool is_stream = !is.KnownSize();

	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");
			return nullptr;
		}

		if (size > MODPLUG_FILE_LIMIT) {
			LogWarning(modplug_domain, "file too large");
			return nullptr;
		}

		buffer.size = size;
	}

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

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

	while (true) {
		size_t ret = decoder_read(client, is, p, end - p);
		if (ret == 0) {
			if (is.LockIsEOF())
				/* end of file */
				break;

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

		p += ret;
		if (p == end) {
			if (!is_stream)
				break;

			LogWarning(modplug_domain, "stream too large");
			delete[] buffer.data;
			buffer.data = nullptr;
			return buffer;
		}
	}

	buffer.size = p - buffer.data;
	return buffer;
}

static ModPlugFile *
LoadModPlugFile(DecoderClient *client, InputStream &is)
{
	const auto buffer = mod_loadfile(client, is);
	if (buffer.IsNull()) {
		LogWarning(modplug_domain, "could not load stream");
		return nullptr;
	}

	ModPlugFile *f = ModPlug_Load(buffer.data, buffer.size);
	delete[] buffer.data;
	return f;
}

static void
mod_decode(DecoderClient &client, InputStream &is)
{
	ModPlug_Settings settings;
	int ret;
	char audio_buffer[MODPLUG_FRAME_SIZE];

	ModPlug_GetSettings(&settings);
	/* alter setting */
	settings.mResamplingMode = modplug_resampling_mode;
	settings.mChannels = 2;
	settings.mBits = 16;
	settings.mFrequency = 44100;
	settings.mLoopCount = modplug_loop_count;
	/* insert more setting changes here */
	ModPlug_SetSettings(&settings);

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

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

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

	DecoderCommand cmd;
	do {
		ret = ModPlug_Read(f, audio_buffer, MODPLUG_FRAME_SIZE);
		if (ret <= 0)
			break;

		cmd = client.SubmitData(nullptr,
					audio_buffer, ret,
					0);

		if (cmd == DecoderCommand::SEEK) {
			ModPlug_Seek(f, client.GetSeekTime().ToMS());
			client.CommandFinished();
		}

	} while (cmd != DecoderCommand::STOP);

	ModPlug_Unload(f);
}

static bool
modplug_scan_stream(InputStream &is, TagHandler &handler) noexcept
{
	ModPlugFile *f = LoadModPlugFile(nullptr, is);
	if (f == nullptr)
		return false;

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

	const char *title = ModPlug_GetName(f);
	if (title != nullptr)
		handler.OnTag(TAG_TITLE, title);

	ModPlug_Unload(f);

	return true;
}

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

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