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

20
#include "ConvertFilterPlugin.hxx"
21 22
#include "filter/Filter.hxx"
#include "filter/Prepared.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "pcm/PcmConvert.hxx"
24
#include "util/Manual.hxx"
25
#include "util/ConstBuffer.hxx"
26
#include "AudioFormat.hxx"
27

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

31 32
#include <assert.h>

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

	/**
41 42
	 * This object is only "open" if #in_audio_format !=
	 * #out_audio_format.
43
	 */
44
	PcmConvert state;
45

46
public:
47 48 49
	ConvertFilter(const AudioFormat &audio_format);
	~ConvertFilter();

50
	void Set(const AudioFormat &_out_audio_format);
51

52
	void Reset() noexcept override {
53 54
		if (IsActive())
			state.Reset();
55 56
	}

57
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src) override;
58 59

	ConstBuffer<void> Flush() override {
60 61 62
		return IsActive()
			? state.Flush()
			: nullptr;
63
	}
64 65 66 67 68

private:
	bool IsActive() const noexcept {
		return out_audio_format != in_audio_format;
	}
69 70
};

71 72
class PreparedConvertFilter final : public PreparedFilter {
public:
73
	std::unique_ptr<Filter> Open(AudioFormat &af) override;
74 75
};

76 77
void
ConvertFilter::Set(const AudioFormat &_out_audio_format)
78 79 80 81 82 83
{
	assert(in_audio_format.IsValid());
	assert(_out_audio_format.IsValid());

	if (_out_audio_format == out_audio_format)
		/* no change */
84
		return;
85

86
	if (IsActive()) {
87 88
		out_audio_format = in_audio_format;
		state.Close();
89 90 91 92
	}

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

95
	state.Open(in_audio_format, _out_audio_format);
96 97 98 99

	out_audio_format = _out_audio_format;
}

100 101
ConvertFilter::ConvertFilter(const AudioFormat &audio_format)
	:Filter(audio_format), in_audio_format(audio_format)
102
{
103
}
104

105
std::unique_ptr<Filter>
106
PreparedConvertFilter::Open(AudioFormat &audio_format)
107 108
{
	assert(audio_format.IsValid());
109

110
	return std::make_unique<ConvertFilter>(audio_format);
111 112
}

113
ConvertFilter::~ConvertFilter()
114
{
115 116
	assert(in_audio_format.IsValid());

117
	if (IsActive())
118
		state.Close();
119 120
}

121
ConstBuffer<void>
122
ConvertFilter::FilterPCM(ConstBuffer<void> src)
123
{
124 125
	assert(in_audio_format.IsValid());

126 127
	return IsActive()
		? state.Convert(src)
128
		/* optimized special case: no-op */
129
		: src;
130 131
}

132
std::unique_ptr<PreparedFilter>
133 134
convert_filter_prepare() noexcept
{
135
	return std::make_unique<PreparedConvertFilter>();
136 137
}

138 139
Filter *
convert_filter_new(const AudioFormat in_audio_format,
140
		   const AudioFormat out_audio_format)
141
{
142 143 144
	std::unique_ptr<ConvertFilter> filter(new ConvertFilter(in_audio_format));
	filter->Set(out_audio_format);
	return filter.release();
145 146
}

147 148
void
convert_filter_set(Filter *_filter, AudioFormat out_audio_format)
149
{
150
	ConvertFilter *filter = (ConvertFilter *)_filter;
151

152
	filter->Set(out_audio_format);
153
}