AutoConvertFilterPlugin.cxx 2.74 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 48
	AutoConvertFilter(std::unique_ptr<Filter> &&_filter,
			  std::unique_ptr<Filter> &&_convert)
		:filter(std::move(_filter)), convert(std::move(_convert)) {}
49

50
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src) override;
51
};
52

53 54 55 56 57 58 59 60 61 62 63 64
class PreparedAutoConvertFilter final : public PreparedFilter {
	/**
	 * The underlying filter.
	 */
	PreparedFilter *const filter;

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

65
	Filter *Open(AudioFormat &af) override;
66 67 68
};

Filter *
69
PreparedAutoConvertFilter::Open(AudioFormat &in_audio_format)
70
{
71
	assert(in_audio_format.IsValid());
72 73 74

	/* open the "real" filter */

75
	AudioFormat child_audio_format = in_audio_format;
76
	std::unique_ptr<Filter> new_filter(filter->Open(child_audio_format));
77 78 79

	/* need to convert? */

80
	std::unique_ptr<Filter> convert;
81
	if (in_audio_format != child_audio_format) {
82 83
		/* yes - create a convert_filter */

84
		convert.reset(convert_filter_new(in_audio_format,
85
						 child_audio_format));
86 87
	}

88 89
	return new AutoConvertFilter(std::move(new_filter),
				     std::move(convert));
90 91
}

92
ConstBuffer<void>
93
AutoConvertFilter::FilterPCM(ConstBuffer<void> src)
94
{
95
	if (convert != nullptr) {
96
		src = convert->FilterPCM(src);
97
		if (src.IsNull())
98
			return nullptr;
99 100
	}

101
	return filter->FilterPCM(src);
102 103
}

104 105
PreparedFilter *
autoconvert_filter_new(PreparedFilter *filter)
106
{
107
	return new PreparedAutoConvertFilter(filter);
108
}