ChainFilterPlugin.cxx 4.53 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 "pcm/AudioFormat.hxx"
24
#include "util/ConstBuffer.hxx"
25
#include "util/StringBuffer.hxx"
26
#include "util/RuntimeError.hxx"
27

28
#include <cassert>
29
#include <list>
30
#include <memory>
31
#include <string>
32

33 34
class ChainFilter final : public Filter {
	struct Child {
35
		std::unique_ptr<Filter> filter;
36

37 38
		explicit Child(std::unique_ptr<Filter> &&_filter) noexcept
			:filter(std::move(_filter)) {}
39
	};
Max Kellermann's avatar
Max Kellermann committed
40

41
	std::list<Child> children;
42

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

48
public:
49 50 51
	explicit ChainFilter(AudioFormat _audio_format)
		:Filter(_audio_format) {}

52
	void Append(std::unique_ptr<Filter> filter) noexcept {
53 54 55 56
		assert(out_audio_format.IsValid());
		out_audio_format = filter->GetOutAudioFormat();
		assert(out_audio_format.IsValid());

57
		children.emplace_back(std::move(filter));
58 59

		RewindFlush();
60
	}
61

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

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

72 73 74 75
};

class PreparedChainFilter final : public PreparedFilter {
	struct Child {
76
		const std::string name;
77
		std::unique_ptr<PreparedFilter> filter;
78

79
		Child(std::string_view _name,
80 81
		      std::unique_ptr<PreparedFilter> _filter)
			:name(_name), filter(std::move(_filter)) {}
82 83 84 85

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

86
		std::unique_ptr<Filter> Open(const AudioFormat &prev_audio_format);
87 88 89 90 91
	};

	std::list<Child> children;

public:
92
	void Append(std::string_view name,
93 94
		    std::unique_ptr<PreparedFilter> filter) noexcept {
		children.emplace_back(name, std::move(filter));
95
	}
96

97
	/* virtual methods from class PreparedFilter */
98
	std::unique_ptr<Filter> Open(AudioFormat &af) override;
99 100
};

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

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

112
	return new_filter;
113 114
}

115
std::unique_ptr<Filter>
116
PreparedChainFilter::Open(AudioFormat &in_audio_format)
117
{
118
	auto chain = std::make_unique<ChainFilter>(in_audio_format);
119

120
	for (auto &child : children) {
121
		AudioFormat audio_format = chain->GetOutAudioFormat();
122
		chain->Append(child.Open(audio_format));
123
	}
124

125
	return chain;
126 127
}

128
void
129
ChainFilter::Reset() noexcept
130
{
131 132
	RewindFlush();

133 134 135 136
	for (auto &child : children)
		child.filter->Reset();
}

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

	return src;
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
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;
}

171 172
std::unique_ptr<PreparedFilter>
filter_chain_new() noexcept
173
{
174
	return std::make_unique<PreparedChainFilter>();
175 176 177
}

void
178
filter_chain_append(PreparedFilter &_chain, std::string_view name,
179
		    std::unique_ptr<PreparedFilter> filter) noexcept
180
{
Max Kellermann's avatar
Max Kellermann committed
181
	auto &chain = (PreparedChainFilter &)_chain;
182

183
	chain.Append(name, std::move(filter));
184
}