AutoConvertFilterPlugin.cxx 2.92 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
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 21
#include "AutoConvertFilterPlugin.hxx"
#include "ConvertFilterPlugin.hxx"
22
#include "filter/FilterPlugin.hxx"
23 24
#include "filter/Filter.hxx"
#include "filter/Prepared.hxx"
25
#include "filter/FilterRegistry.hxx"
26
#include "AudioFormat.hxx"
27
#include "util/ConstBuffer.hxx"
28

29 30
#include <memory>

31 32
#include <assert.h>

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

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

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

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

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

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

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

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

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

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

	/* open the "real" filter */

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

	/* need to convert? */

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

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

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

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

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

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