DecoderList.cxx 5.76 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
21
#include "DecoderList.hxx"
22
#include "DecoderPlugin.hxx"
23 24
#include "ConfigGlobal.hxx"
#include "ConfigData.hxx"
25
#include "decoder/AudiofileDecoderPlugin.hxx"
26
#include "decoder/PcmDecoderPlugin.hxx"
27 28
#include "decoder/DsdiffDecoderPlugin.hxx"
#include "decoder/DsfDecoderPlugin.hxx"
29
#include "decoder/FlacDecoderPlugin.h"
30
#include "decoder/OpusDecoderPlugin.h"
31
#include "decoder/VorbisDecoderPlugin.h"
32
#include "decoder/AdPlugDecoderPlugin.h"
33
#include "decoder/WavpackDecoderPlugin.hxx"
34
#include "decoder/FfmpegDecoderPlugin.hxx"
35
#include "decoder/GmeDecoderPlugin.hxx"
36
#include "decoder/FaadDecoderPlugin.hxx"
37
#include "decoder/MadDecoderPlugin.hxx"
38
#include "decoder/SndfileDecoderPlugin.hxx"
39
#include "decoder/Mpg123DecoderPlugin.hxx"
40
#include "decoder/WildmidiDecoderPlugin.hxx"
41
#include "decoder/MikmodDecoderPlugin.hxx"
42
#include "decoder/ModplugDecoderPlugin.hxx"
43
#include "decoder/MpcdecDecoderPlugin.hxx"
44
#include "decoder/FluidsynthDecoderPlugin.hxx"
45
#include "decoder/SidplayDecoderPlugin.hxx"
46
#include "system/FatalError.hxx"
47
#include "util/Macros.hxx"
48

49 50
#include <string.h>

51
const struct DecoderPlugin *const decoder_plugins[] = {
52
#ifdef HAVE_MAD
53
	&mad_decoder_plugin,
54
#endif
55 56 57
#ifdef HAVE_MPG123
	&mpg123_decoder_plugin,
#endif
58
#ifdef ENABLE_VORBIS_DECODER
Max Kellermann's avatar
Max Kellermann committed
59
	&vorbis_decoder_plugin,
60
#endif
61
#if defined(HAVE_FLAC)
Max Kellermann's avatar
Max Kellermann committed
62
	&oggflac_decoder_plugin,
63 64
#endif
#ifdef HAVE_FLAC
Max Kellermann's avatar
Max Kellermann committed
65
	&flac_decoder_plugin,
66
#endif
67 68 69
#ifdef HAVE_OPUS
	&opus_decoder_plugin,
#endif
70 71 72
#ifdef ENABLE_SNDFILE
	&sndfile_decoder_plugin,
#endif
73
#ifdef HAVE_AUDIOFILE
74
	&audiofile_decoder_plugin,
75
#endif
76
	&dsdiff_decoder_plugin,
77
	&dsf_decoder_plugin,
78
#ifdef HAVE_FAAD
79
	&faad_decoder_plugin,
80 81
#endif
#ifdef HAVE_MPCDEC
82
	&mpcdec_decoder_plugin,
83 84
#endif
#ifdef HAVE_WAVPACK
85
	&wavpack_decoder_plugin,
86
#endif
87
#ifdef HAVE_MODPLUG
88
	&modplug_decoder_plugin,
89
#endif
90
#ifdef ENABLE_MIKMOD_DECODER
91
	&mikmod_decoder_plugin,
92
#endif
93 94 95
#ifdef ENABLE_SIDPLAY
	&sidplay_decoder_plugin,
#endif
96 97 98
#ifdef ENABLE_WILDMIDI
	&wildmidi_decoder_plugin,
#endif
99 100 101
#ifdef ENABLE_FLUIDSYNTH
	&fluidsynth_decoder_plugin,
#endif
102 103 104
#ifdef HAVE_ADPLUG
	&adplug_decoder_plugin,
#endif
105
#ifdef HAVE_FFMPEG
106
	&ffmpeg_decoder_plugin,
107 108 109
#endif
#ifdef HAVE_GME
	&gme_decoder_plugin,
110
#endif
111
	&pcm_decoder_plugin,
112
	nullptr
113
};
114

115 116
static constexpr unsigned num_decoder_plugins =
	ARRAY_SIZE(decoder_plugins) - 1;
117

118
/** which plugins have been initialized successfully? */
119
bool decoder_plugins_enabled[num_decoder_plugins];
120

121
static unsigned
122
decoder_plugin_index(const struct DecoderPlugin *plugin)
Avuton Olrich's avatar
Avuton Olrich committed
123
{
124 125 126 127 128 129 130 131 132
	unsigned i = 0;

	while (decoder_plugins[i] != plugin)
		++i;

	return i;
}

static unsigned
133
decoder_plugin_next_index(const struct DecoderPlugin *plugin)
134 135 136 137 138
{
	return plugin == 0
		? 0 /* start with first plugin */
		: decoder_plugin_index(plugin) + 1;
}
Avuton Olrich's avatar
Avuton Olrich committed
139

140
const struct DecoderPlugin *
141
decoder_plugin_from_suffix(const char *suffix,
142
			   const struct DecoderPlugin *plugin)
143
{
144 145
	if (suffix == nullptr)
		return nullptr;
146

147
	for (unsigned i = decoder_plugin_next_index(plugin);
148
	     decoder_plugins[i] != nullptr; ++i) {
149
		plugin = decoder_plugins[i];
150
		if (decoder_plugins_enabled[i] &&
151
		    plugin->SupportsSuffix(suffix))
152 153 154
			return plugin;
	}

155
	return nullptr;
156 157
}

158
const struct DecoderPlugin *
159
decoder_plugin_from_mime_type(const char *mimeType, unsigned int next)
Avuton Olrich's avatar
Avuton Olrich committed
160
{
161
	static unsigned i = num_decoder_plugins;
Avuton Olrich's avatar
Avuton Olrich committed
162

163 164
	if (mimeType == nullptr)
		return nullptr;
165

166 167
	if (!next)
		i = 0;
168
	for (; decoder_plugins[i] != nullptr; ++i) {
169
		const struct DecoderPlugin *plugin = decoder_plugins[i];
170
		if (decoder_plugins_enabled[i] &&
171
		    plugin->SupportsMimeType(mimeType)) {
172
			++i;
173 174 175 176
			return plugin;
		}
	}

177
	return nullptr;
178 179
}

180
const struct DecoderPlugin *
181
decoder_plugin_from_name(const char *name)
Avuton Olrich's avatar
Avuton Olrich committed
182
{
183
	return decoder_plugins_find([=](const DecoderPlugin &plugin){
184 185
			return strcmp(plugin.name, name) == 0;
		});
186 187
}

188 189 190 191
/**
 * Find the "decoder" configuration block for the specified plugin.
 *
 * @param plugin_name the name of the decoder plugin
192
 * @return the configuration block, or nullptr if none was configured
193 194 195 196
 */
static const struct config_param *
decoder_plugin_config(const char *plugin_name)
{
197
	const struct config_param *param = nullptr;
198

199
	while ((param = config_get_next_param(CONF_DECODER, param)) != nullptr) {
200
		const char *name = param->GetBlockValue("plugin");
201
		if (name == nullptr)
202 203
			FormatFatalError("decoder configuration without 'plugin' name in line %d",
					 param->line);
204 205 206 207 208

		if (strcmp(name, plugin_name) == 0)
			return param;
	}

209
	return nullptr;
210 211
}

212
void decoder_plugin_init_all(void)
Avuton Olrich's avatar
Avuton Olrich committed
213
{
214 215
	struct config_param empty;

216
	for (unsigned i = 0; decoder_plugins[i] != nullptr; ++i) {
217
		const DecoderPlugin &plugin = *decoder_plugins[i];
218
		const struct config_param *param =
219
			decoder_plugin_config(plugin.name);
220

221 222 223
		if (param == nullptr)
			param = &empty;
		else if (!param->GetBlockValue("enabled", true))
224 225 226
			/* the plugin is disabled in mpd.conf */
			continue;

227
		if (plugin.Init(*param))
228 229
			decoder_plugins_enabled[i] = true;
	}
230 231
}

232
void decoder_plugin_deinit_all(void)
Avuton Olrich's avatar
Avuton Olrich committed
233
{
234 235 236
	decoder_plugins_for_each_enabled([=](const DecoderPlugin &plugin){
			plugin.Finish();
		});
237
}