DecoderList.cxx 4.66 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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
#include "decoder/Features.h"
24 25
#include "PluginUnavailable.hxx"
#include "Log.hxx"
26
#include "config/Data.hxx"
27
#include "config/Block.hxx"
28 29 30 31
#include "plugins/AudiofileDecoderPlugin.hxx"
#include "plugins/PcmDecoderPlugin.hxx"
#include "plugins/DsdiffDecoderPlugin.hxx"
#include "plugins/DsfDecoderPlugin.hxx"
32
#include "plugins/HybridDsdDecoderPlugin.hxx"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include "plugins/FlacDecoderPlugin.h"
#include "plugins/OpusDecoderPlugin.h"
#include "plugins/VorbisDecoderPlugin.h"
#include "plugins/AdPlugDecoderPlugin.h"
#include "plugins/WavpackDecoderPlugin.hxx"
#include "plugins/FfmpegDecoderPlugin.hxx"
#include "plugins/GmeDecoderPlugin.hxx"
#include "plugins/FaadDecoderPlugin.hxx"
#include "plugins/MadDecoderPlugin.hxx"
#include "plugins/SndfileDecoderPlugin.hxx"
#include "plugins/Mpg123DecoderPlugin.hxx"
#include "plugins/WildmidiDecoderPlugin.hxx"
#include "plugins/MikmodDecoderPlugin.hxx"
#include "plugins/ModplugDecoderPlugin.hxx"
#include "plugins/MpcdecDecoderPlugin.hxx"
#include "plugins/FluidsynthDecoderPlugin.hxx"
#include "plugins/SidplayDecoderPlugin.hxx"
50
#include "util/RuntimeError.hxx"
51

52 53
#include <iterator>

54 55
#include <string.h>

56
const struct DecoderPlugin *const decoder_plugins[] = {
57
#ifdef ENABLE_MAD
58
	&mad_decoder_plugin,
59
#endif
60
#ifdef ENABLE_MPG123
61 62
	&mpg123_decoder_plugin,
#endif
63
#ifdef ENABLE_VORBIS_DECODER
Max Kellermann's avatar
Max Kellermann committed
64
	&vorbis_decoder_plugin,
65
#endif
66
#ifdef ENABLE_FLAC
Max Kellermann's avatar
Max Kellermann committed
67 68
	&oggflac_decoder_plugin,
	&flac_decoder_plugin,
69
#endif
70
#ifdef ENABLE_OPUS
71 72
	&opus_decoder_plugin,
#endif
73 74 75
#ifdef ENABLE_SNDFILE
	&sndfile_decoder_plugin,
#endif
76
#ifdef ENABLE_AUDIOFILE
77
	&audiofile_decoder_plugin,
78
#endif
79
#ifdef ENABLE_DSD
80
	&dsdiff_decoder_plugin,
81
	&dsf_decoder_plugin,
82
	&hybrid_dsd_decoder_plugin,
83
#endif
84
#ifdef ENABLE_FAAD
85
	&faad_decoder_plugin,
86
#endif
87
#ifdef ENABLE_MPCDEC
88
	&mpcdec_decoder_plugin,
89
#endif
90
#ifdef ENABLE_WAVPACK
91
	&wavpack_decoder_plugin,
92
#endif
93
#ifdef ENABLE_MODPLUG
94
	&modplug_decoder_plugin,
95
#endif
96
#ifdef ENABLE_LIBMIKMOD
97
	&mikmod_decoder_plugin,
98
#endif
99 100 101
#ifdef ENABLE_SIDPLAY
	&sidplay_decoder_plugin,
#endif
102 103 104
#ifdef ENABLE_WILDMIDI
	&wildmidi_decoder_plugin,
#endif
105 106 107
#ifdef ENABLE_FLUIDSYNTH
	&fluidsynth_decoder_plugin,
#endif
108
#ifdef ENABLE_ADPLUG
109 110
	&adplug_decoder_plugin,
#endif
111
#ifdef ENABLE_FFMPEG
112
	&ffmpeg_decoder_plugin,
113
#endif
114
#ifdef ENABLE_GME
115
	&gme_decoder_plugin,
116
#endif
117
	&pcm_decoder_plugin,
118
	nullptr
119
};
120

121
static constexpr unsigned num_decoder_plugins =
122
	std::size(decoder_plugins) - 1;
123

124
/** which plugins have been initialized successfully? */
125
bool decoder_plugins_enabled[num_decoder_plugins];
126

127
const struct DecoderPlugin *
128
decoder_plugin_from_name(const char *name) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
129
{
130
	return decoder_plugins_find([=](const DecoderPlugin &plugin){
131 132
			return strcmp(plugin.name, name) == 0;
		});
133 134
}

135 136
void
decoder_plugin_init_all(const ConfigData &config)
Avuton Olrich's avatar
Avuton Olrich committed
137
{
138
	ConfigBlock empty;
139

140
	for (unsigned i = 0; decoder_plugins[i] != nullptr; ++i) {
141
		const DecoderPlugin &plugin = *decoder_plugins[i];
142
		const auto *param =
143 144
			config.FindBlock(ConfigBlockOption::DECODER, "plugin",
					 plugin.name);
145

146 147 148
		if (param == nullptr)
			param = &empty;
		else if (!param->GetBlockValue("enabled", true))
149 150 151
			/* the plugin is disabled in mpd.conf */
			continue;

152 153 154
		if (param != nullptr)
			param->SetUsed();

155 156 157
		try {
			if (plugin.Init(*param))
				decoder_plugins_enabled[i] = true;
158 159 160 161
		} catch (const PluginUnavailable &e) {
			FormatError(e,
				    "Decoder plugin '%s' is unavailable",
				    plugin.name);
162 163 164 165
		} catch (...) {
			std::throw_with_nested(FormatRuntimeError("Failed to initialize decoder plugin '%s'",
								  plugin.name));
		}
166
	}
167 168
}

169 170
void
decoder_plugin_deinit_all() noexcept
Avuton Olrich's avatar
Avuton Olrich committed
171
{
172 173 174
	decoder_plugins_for_each_enabled([=](const DecoderPlugin &plugin){
			plugin.Finish();
		});
175
}
176 177

bool
178
decoder_plugins_supports_suffix(std::string_view suffix) noexcept
179 180 181 182 183
{
	return decoder_plugins_try([suffix](const DecoderPlugin &plugin){
			return plugin.SupportsSuffix(suffix);
		});
}