AutoConvertFilterPlugin.cxx 2.88 KB
Newer Older
1
/*
2
 * Copyright 2003-2019 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 21
#include "AutoConvertFilterPlugin.hxx"
#include "ConvertFilterPlugin.hxx"
22
#include "filter/FilterPlugin.hxx"
23 24
#include "filter/Filter.hxx"
#include "filter/Prepared.hxx"
25
#include "AudioFormat.hxx"
26
#include "util/ConstBuffer.hxx"
27

28 29
#include <memory>

30 31
#include <assert.h>

32
class AutoConvertFilter final : public Filter {
33 34 35
	/**
	 * The underlying filter.
	 */
36
	std::unique_ptr<Filter> filter;
37 38

	/**
39
	 * A convert_filter, just in case conversion is needed.  nullptr
40 41
	 * if unused.
	 */
42
	std::unique_ptr<Filter> convert;
43

44
public:
45 46
	AutoConvertFilter(std::unique_ptr<Filter> &&_filter,
			  std::unique_ptr<Filter> &&_convert)
47 48
		:Filter(_filter->GetOutAudioFormat()),
		 filter(std::move(_filter)), convert(std::move(_convert)) {}
49

50
	void Reset() noexcept override {
51 52 53 54 55 56
		filter->Reset();

		if (convert)
			convert->Reset();
	}

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

60 61 62 63
class PreparedAutoConvertFilter final : public PreparedFilter {
	/**
	 * The underlying filter.
	 */
64
	std::unique_ptr<PreparedFilter> filter;
65 66

public:
67 68
	PreparedAutoConvertFilter(std::unique_ptr<PreparedFilter> _filter) noexcept
		:filter(std::move(_filter)) {}
69

70
	std::unique_ptr<Filter> Open(AudioFormat &af) override;
71 72
};

73
std::unique_ptr<Filter>
74
PreparedAutoConvertFilter::Open(AudioFormat &in_audio_format)
75
{
76
	assert(in_audio_format.IsValid());
77 78 79

	/* open the "real" filter */

80
	AudioFormat child_audio_format = in_audio_format;
81
	auto new_filter = filter->Open(child_audio_format);
82 83 84

	/* need to convert? */

85
	std::unique_ptr<Filter> convert;
86
	if (in_audio_format != child_audio_format) {
87 88
		/* yes - create a convert_filter */

89
		convert.reset(convert_filter_new(in_audio_format,
90
						 child_audio_format));
91 92
	}

93 94
	return std::make_unique<AutoConvertFilter>(std::move(new_filter),
						   std::move(convert));
95 96
}

97
ConstBuffer<void>
98
AutoConvertFilter::FilterPCM(ConstBuffer<void> src)
99
{
100
	if (convert != nullptr)
101
		src = convert->FilterPCM(src);
102

103
	return filter->FilterPCM(src);
104 105
}

106 107
std::unique_ptr<PreparedFilter>
autoconvert_filter_new(std::unique_ptr<PreparedFilter> filter) noexcept
108
{
109
	return std::make_unique<PreparedAutoConvertFilter>(std::move(filter));
110
}