ChainFilterPlugin.cxx 4.58 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

Max Kellermann's avatar
Max Kellermann committed
20
#include "ChainFilterPlugin.hxx"
21 22
#include "filter/Filter.hxx"
#include "filter/Prepared.hxx"
23
#include "AudioFormat.hxx"
24
#include "util/ConstBuffer.hxx"
25
#include "util/StringBuffer.hxx"
26
#include "util/RuntimeError.hxx"
27

28
#include <memory>
29 30
#include <list>

31 32
#include <assert.h>

33 34 35
class ChainFilter final : public Filter {
	struct Child {
		const char *name;
36
		std::unique_ptr<Filter> filter;
37

38 39
		Child(const char *_name,
		      std::unique_ptr<Filter> _filter) noexcept
40
			:name(_name), filter(std::move(_filter)) {}
41
	};
Max Kellermann's avatar
Max Kellermann committed
42

43
	std::list<Child> children;
44

45 46 47 48 49
	/**
	 * The child which will be flushed in the next Flush() call.
	 */
	std::list<Child>::iterator flushing = children.end();

50
public:
51 52 53
	explicit ChainFilter(AudioFormat _audio_format)
		:Filter(_audio_format) {}

54 55
	void Append(const char *name,
		    std::unique_ptr<Filter> filter) noexcept {
56 57 58 59
		assert(out_audio_format.IsValid());
		out_audio_format = filter->GetOutAudioFormat();
		assert(out_audio_format.IsValid());

60
		children.emplace_back(name, std::move(filter));
61 62

		RewindFlush();
63
	}
64

65
	/* virtual methods from class Filter */
66
	void Reset() noexcept override;
67
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src) override;
68 69 70 71 72 73 74
	ConstBuffer<void> Flush() override;

private:
	void RewindFlush() {
		flushing = children.begin();
	}

75 76 77 78 79
};

class PreparedChainFilter final : public PreparedFilter {
	struct Child {
		const char *name;
80
		std::unique_ptr<PreparedFilter> filter;
81

82 83 84
		Child(const char *_name,
		      std::unique_ptr<PreparedFilter> _filter)
			:name(_name), filter(std::move(_filter)) {}
85 86 87 88

		Child(const Child &) = delete;
		Child &operator=(const Child &) = delete;

89
		std::unique_ptr<Filter> Open(const AudioFormat &prev_audio_format);
90 91 92 93 94
	};

	std::list<Child> children;

public:
95 96 97
	void Append(const char *name,
		    std::unique_ptr<PreparedFilter> filter) noexcept {
		children.emplace_back(name, std::move(filter));
98
	}
99

100
	/* virtual methods from class PreparedFilter */
101
	std::unique_ptr<Filter> Open(AudioFormat &af) override;
102 103
};

104
std::unique_ptr<Filter>
105
PreparedChainFilter::Child::Open(const AudioFormat &prev_audio_format)
106
{
107
	AudioFormat conv_audio_format = prev_audio_format;
108
	auto new_filter = filter->Open(conv_audio_format);
109

110
	if (conv_audio_format != prev_audio_format)
111 112
		throw FormatRuntimeError("Audio format not supported by filter '%s': %s",
					 name,
113
					 ToString(prev_audio_format).c_str());
114

115
	return new_filter;
116 117
}

118
std::unique_ptr<Filter>
119
PreparedChainFilter::Open(AudioFormat &in_audio_format)
120
{
121
	auto chain = std::make_unique<ChainFilter>(in_audio_format);
122

123
	for (auto &child : children) {
124
		AudioFormat audio_format = chain->GetOutAudioFormat();
125
		chain->Append(child.name, child.Open(audio_format));
126
	}
127

128
	return chain;
129 130
}

131
void
132
ChainFilter::Reset() noexcept
133
{
134 135
	RewindFlush();

136 137 138 139
	for (auto &child : children)
		child.filter->Reset();
}

140 141 142
template<typename I>
static ConstBuffer<void>
ApplyFilterChain(I begin, I end, ConstBuffer<void> src)
143
{
144
	for (auto i = begin; i != end; ++i)
145 146
		/* feed the output of the previous filter as input
		   into the current one */
147
		src = i->filter->FilterPCM(src);
148 149 150 151

	return src;
}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
ConstBuffer<void>
ChainFilter::FilterPCM(ConstBuffer<void> src)
{
	RewindFlush();

	/* return the output of the last filter */
	return ApplyFilterChain(children.begin(), children.end(), src);
}

ConstBuffer<void>
ChainFilter::Flush()
{
	for (auto end = children.end(); flushing != end; ++flushing) {
		auto data = flushing->filter->Flush();
		if (!data.IsNull())
			return ApplyFilterChain(std::next(flushing), end,
						data);
	}

	return nullptr;
}

174 175
std::unique_ptr<PreparedFilter>
filter_chain_new() noexcept
176
{
177
	return std::make_unique<PreparedChainFilter>();
178 179 180
}

void
181
filter_chain_append(PreparedFilter &_chain, const char *name,
182
		    std::unique_ptr<PreparedFilter> filter) noexcept
183
{
184
	PreparedChainFilter &chain = (PreparedChainFilter &)_chain;
185

186
	chain.Append(name, std::move(filter));
187
}