AutoConvertFilterPlugin.cxx 2.82 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 20
 * 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.
 */

#include "config.h"
21 22
#include "AutoConvertFilterPlugin.hxx"
#include "ConvertFilterPlugin.hxx"
23 24 25
#include "filter/FilterPlugin.hxx"
#include "filter/FilterInternal.hxx"
#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 52 53 54 55 56 57
	void Reset() override {
		filter->Reset();

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

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

61 62 63 64 65 66 67 68 69 70 71 72
class PreparedAutoConvertFilter final : public PreparedFilter {
	/**
	 * The underlying filter.
	 */
	PreparedFilter *const filter;

public:
	PreparedAutoConvertFilter(PreparedFilter *_filter):filter(_filter) {}
	~PreparedAutoConvertFilter() {
		delete filter;
	}

73
	Filter *Open(AudioFormat &af) override;
74 75 76
};

Filter *
77
PreparedAutoConvertFilter::Open(AudioFormat &in_audio_format)
78
{
79
	assert(in_audio_format.IsValid());
80 81 82

	/* open the "real" filter */

83
	AudioFormat child_audio_format = in_audio_format;
84
	std::unique_ptr<Filter> new_filter(filter->Open(child_audio_format));
85 86 87

	/* need to convert? */

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

92
		convert.reset(convert_filter_new(in_audio_format,
93
						 child_audio_format));
94 95
	}

96 97
	return new AutoConvertFilter(std::move(new_filter),
				     std::move(convert));
98 99
}

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

106
	return filter->FilterPCM(src);
107 108
}

109 110
PreparedFilter *
autoconvert_filter_new(PreparedFilter *filter)
111
{
112
	return new PreparedAutoConvertFilter(filter);
113
}