ChainFilterPlugin.cxx 4.6 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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.
 */

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

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

32 33
#include <assert.h>

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

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

44
	std::list<Child> children;
45

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

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

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

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

		RewindFlush();
64
	}
65

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

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

76 77 78 79 80
};

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

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

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

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

	std::list<Child> children;

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

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

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

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

116
	return new_filter;
117 118
}

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

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

129
	return chain;
130 131
}

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

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

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

	return src;
}

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
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;
}

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

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

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