ConvertFilterPlugin.cxx 3.46 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
 * 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 "config.h"
21
#include "ConvertFilterPlugin.hxx"
22 23 24
#include "filter/FilterPlugin.hxx"
#include "filter/FilterInternal.hxx"
#include "filter/FilterRegistry.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "pcm/PcmConvert.hxx"
26
#include "util/Manual.hxx"
27
#include "util/ConstBuffer.hxx"
28
#include "AudioFormat.hxx"
29 30
#include "poison.h"

31 32 33
#include <stdexcept>
#include <memory>

34 35
#include <assert.h>

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

	/**
44 45
	 * This object is only "open" if #in_audio_format !=
	 * #out_audio_format.
46
	 */
47
	PcmConvert state;
48

49
public:
50 51 52
	ConvertFilter(const AudioFormat &audio_format);
	~ConvertFilter();

53
	void Set(const AudioFormat &_out_audio_format);
54

55 56 57 58
	void Reset() override {
		state.Reset();
	}

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

62 63
class PreparedConvertFilter final : public PreparedFilter {
public:
64
	Filter *Open(AudioFormat &af) override;
65 66 67
};

static PreparedFilter *
68
convert_filter_init(gcc_unused const ConfigBlock &block)
69
{
70
	return new PreparedConvertFilter();
71 72
}

73 74
void
ConvertFilter::Set(const AudioFormat &_out_audio_format)
75 76 77 78 79 80
{
	assert(in_audio_format.IsValid());
	assert(_out_audio_format.IsValid());

	if (_out_audio_format == out_audio_format)
		/* no change */
81
		return;
82

83 84 85
	if (out_audio_format != in_audio_format) {
		out_audio_format = in_audio_format;
		state.Close();
86 87 88 89
	}

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

92
	state.Open(in_audio_format, _out_audio_format);
93 94 95 96

	out_audio_format = _out_audio_format;
}

97 98
ConvertFilter::ConvertFilter(const AudioFormat &audio_format)
	:Filter(audio_format), in_audio_format(audio_format)
99
{
100
}
101

102
Filter *
103
PreparedConvertFilter::Open(AudioFormat &audio_format)
104 105
{
	assert(audio_format.IsValid());
106

107
	return new ConvertFilter(audio_format);
108 109
}

110
ConvertFilter::~ConvertFilter()
111
{
112 113
	assert(in_audio_format.IsValid());

114
	if (out_audio_format != in_audio_format)
115
		state.Close();
116 117
}

118
ConstBuffer<void>
119
ConvertFilter::FilterPCM(ConstBuffer<void> src)
120
{
121 122
	assert(in_audio_format.IsValid());

123
	if (out_audio_format == in_audio_format)
124 125 126
		/* optimized special case: no-op */
		return src;

127
	return state.Convert(src);
128 129
}

130
const FilterPlugin convert_filter_plugin = {
131 132
	"convert",
	convert_filter_init,
133 134
};

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

144 145
void
convert_filter_set(Filter *_filter, AudioFormat out_audio_format)
146
{
147
	ConvertFilter *filter = (ConvertFilter *)_filter;
148

149
	filter->Set(out_audio_format);
150
}