decoder_list.c 5.92 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2012 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 "decoder_list.h"
22
#include "decoder_plugin.h"
23
#include "conf.h"
24
#include "mpd_error.h"
25
#include "decoder/pcm_decoder_plugin.h"
26
#include "decoder/dsdiff_decoder_plugin.h"
27
#include "decoder/dsf_decoder_plugin.h"
28
#include "decoder/FLACDecoderPlugin.h"
29
#include "decoder/OpusDecoderPlugin.h"
30
#include "decoder/VorbisDecoderPlugin.h"
31
#include "decoder/AdPlugDecoderPlugin.h"
32
#include "decoder/WavpackDecoderPlugin.hxx"
33
#include "decoder/FfmpegDecoderPlugin.hxx"
34 35

#include <glib.h>
36

37 38
#include <string.h>

39
extern const struct decoder_plugin mad_decoder_plugin;
40
extern const struct decoder_plugin mpg123_decoder_plugin;
41
extern const struct decoder_plugin sndfile_decoder_plugin;
42
extern const struct decoder_plugin audiofile_decoder_plugin;
43
extern const struct decoder_plugin mp4ff_decoder_plugin;
44
extern const struct decoder_plugin faad_decoder_plugin;
45
extern const struct decoder_plugin mpcdec_decoder_plugin;
46
extern const struct decoder_plugin modplug_decoder_plugin;
47
extern const struct decoder_plugin mikmod_decoder_plugin;
48
extern const struct decoder_plugin sidplay_decoder_plugin;
49
extern const struct decoder_plugin wildmidi_decoder_plugin;
50
extern const struct decoder_plugin fluidsynth_decoder_plugin;
51
extern const struct decoder_plugin gme_decoder_plugin;
52

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

120
enum {
121
	num_decoder_plugins = G_N_ELEMENTS(decoder_plugins) - 1,
122
};
123

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

127 128
static unsigned
decoder_plugin_index(const struct decoder_plugin *plugin)
Avuton Olrich's avatar
Avuton Olrich committed
129
{
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	unsigned i = 0;

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

	return i;
}

static unsigned
decoder_plugin_next_index(const struct decoder_plugin *plugin)
{
	return plugin == 0
		? 0 /* start with first plugin */
		: decoder_plugin_index(plugin) + 1;
}
Avuton Olrich's avatar
Avuton Olrich committed
145

146 147 148 149
const struct decoder_plugin *
decoder_plugin_from_suffix(const char *suffix,
			   const struct decoder_plugin *plugin)
{
Avuton Olrich's avatar
Avuton Olrich committed
150 151
	if (suffix == NULL)
		return NULL;
152

153 154 155
	for (unsigned i = decoder_plugin_next_index(plugin);
	     decoder_plugins[i] != NULL; ++i) {
		plugin = decoder_plugins[i];
156
		if (decoder_plugins_enabled[i] &&
157
		    decoder_plugin_supports_suffix(plugin, suffix))
158 159 160 161 162 163
			return plugin;
	}

	return NULL;
}

164 165
const struct decoder_plugin *
decoder_plugin_from_mime_type(const char *mimeType, unsigned int next)
Avuton Olrich's avatar
Avuton Olrich committed
166
{
167
	static unsigned i = num_decoder_plugins;
Avuton Olrich's avatar
Avuton Olrich committed
168 169 170

	if (mimeType == NULL)
		return NULL;
171

172 173
	if (!next)
		i = 0;
174
	for (; decoder_plugins[i] != NULL; ++i) {
175
		const struct decoder_plugin *plugin = decoder_plugins[i];
176
		if (decoder_plugins_enabled[i] &&
177
		    decoder_plugin_supports_mime_type(plugin, mimeType)) {
178
			++i;
179 180 181 182 183 184 185
			return plugin;
		}
	}

	return NULL;
}

186 187
const struct decoder_plugin *
decoder_plugin_from_name(const char *name)
Avuton Olrich's avatar
Avuton Olrich committed
188
{
189 190
	decoder_plugins_for_each_enabled(plugin)
		if (strcmp(plugin->name, name) == 0)
191
			return plugin;
192

193
	return NULL;
194 195
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/**
 * Find the "decoder" configuration block for the specified plugin.
 *
 * @param plugin_name the name of the decoder plugin
 * @return the configuration block, or NULL if none was configured
 */
static const struct config_param *
decoder_plugin_config(const char *plugin_name)
{
	const struct config_param *param = NULL;

	while ((param = config_get_next_param(CONF_DECODER, param)) != NULL) {
		const char *name =
			config_get_block_string(param, "plugin", NULL);
		if (name == NULL)
211 212
			MPD_ERROR("decoder configuration without 'plugin' name in line %d",
				  param->line);
213 214 215 216 217 218 219 220

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

	return NULL;
}

221
void decoder_plugin_init_all(void)
Avuton Olrich's avatar
Avuton Olrich committed
222
{
223
	for (unsigned i = 0; decoder_plugins[i] != NULL; ++i) {
224
		const struct decoder_plugin *plugin = decoder_plugins[i];
225 226
		const struct config_param *param =
			decoder_plugin_config(plugin->name);
227

228 229 230 231
		if (!config_get_block_bool(param, "enabled", true))
			/* the plugin is disabled in mpd.conf */
			continue;

232
		if (decoder_plugin_init(plugin, param))
233 234
			decoder_plugins_enabled[i] = true;
	}
235 236
}

237
void decoder_plugin_deinit_all(void)
Avuton Olrich's avatar
Avuton Olrich committed
238
{
239 240
	decoder_plugins_for_each_enabled(plugin)
		decoder_plugin_finish(plugin);
241
}