ConvertFilterPlugin.cxx 3.21 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.
 */

20
#include "ConvertFilterPlugin.hxx"
21 22
#include "filter/Filter.hxx"
#include "filter/Prepared.hxx"
23
#include "pcm/AudioFormat.hxx"
24
#include "pcm/Convert.hxx"
25
#include "util/ConstBuffer.hxx"
26

27 28
#include <memory>

29 30
#include <assert.h>

31
class ConvertFilter final : public Filter {
32 33 34 35
	/**
	 * The input audio format; PCM data is passed to the filter()
	 * method in this format.
	 */
36
	const AudioFormat in_audio_format;
37 38

	/**
39 40
	 * This object is only "open" if #in_audio_format !=
	 * #out_audio_format.
41
	 */
42
	std::unique_ptr<PcmConvert> state;
43

44
public:
Max Kellermann's avatar
Max Kellermann committed
45
	explicit ConvertFilter(const AudioFormat &audio_format);
46

47
	void Set(const AudioFormat &_out_audio_format);
48

49
	void Reset() noexcept override {
50 51
		if (state)
			state->Reset();
52 53
	}

54
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src) override;
55 56

	ConstBuffer<void> Flush() override {
57 58
		return state
			? state->Flush()
59
			: nullptr;
60
	}
61 62
};

63 64
class PreparedConvertFilter final : public PreparedFilter {
public:
65
	std::unique_ptr<Filter> Open(AudioFormat &af) override;
66 67
};

68 69
void
ConvertFilter::Set(const AudioFormat &_out_audio_format)
70 71 72 73 74
{
	assert(_out_audio_format.IsValid());

	if (_out_audio_format == out_audio_format)
		/* no change */
75
		return;
76

77
	if (state) {
78
		out_audio_format = in_audio_format;
79
		state.reset();
80 81 82 83
	}

	if (_out_audio_format == in_audio_format)
		/* optimized special case: no-op */
84
		return;
85

86 87
	state = std::make_unique<PcmConvert>(in_audio_format,
					     _out_audio_format);
88 89 90 91

	out_audio_format = _out_audio_format;
}

92 93
ConvertFilter::ConvertFilter(const AudioFormat &audio_format)
	:Filter(audio_format), in_audio_format(audio_format)
94
{
95
	assert(in_audio_format.IsValid());
96
}
97

98
std::unique_ptr<Filter>
99
PreparedConvertFilter::Open(AudioFormat &audio_format)
100 101
{
	assert(audio_format.IsValid());
102

103
	return std::make_unique<ConvertFilter>(audio_format);
104 105
}

106
ConstBuffer<void>
107
ConvertFilter::FilterPCM(ConstBuffer<void> src)
108
{
109 110
	return state
		? state->Convert(src)
111
		/* optimized special case: no-op */
112
		: src;
113 114
}

115
std::unique_ptr<PreparedFilter>
116 117
convert_filter_prepare() noexcept
{
118
	return std::make_unique<PreparedConvertFilter>();
119 120
}

121 122
Filter *
convert_filter_new(const AudioFormat in_audio_format,
123
		   const AudioFormat out_audio_format)
124
{
125 126 127
	std::unique_ptr<ConvertFilter> filter(new ConvertFilter(in_audio_format));
	filter->Set(out_audio_format);
	return filter.release();
128 129
}

130 131
void
convert_filter_set(Filter *_filter, AudioFormat out_audio_format)
132
{
Max Kellermann's avatar
Max Kellermann committed
133
	auto *filter = (ConvertFilter *)_filter;
134

135
	filter->Set(out_audio_format);
136
}