ConvertFilterPlugin.cxx 3.47 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
 * 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
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src) override;
56 57
};

58 59
class PreparedConvertFilter final : public PreparedFilter {
public:
60
	void Set(const AudioFormat &_out_audio_format);
61

62
	Filter *Open(AudioFormat &af) override;
63 64 65
};

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

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

	if (_out_audio_format == out_audio_format)
		/* no change */
79
		return;
80

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

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

90
	state.Open(in_audio_format, _out_audio_format);
91 92 93 94

	out_audio_format = _out_audio_format;
}

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

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

105
	return new ConvertFilter(audio_format);
106 107
}

108
ConvertFilter::~ConvertFilter()
109
{
110 111
	assert(in_audio_format.IsValid());

112
	if (out_audio_format != in_audio_format)
113
		state.Close();
114 115
}

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

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

125
	return state.Convert(src);
126 127 128
}

const struct filter_plugin convert_filter_plugin = {
129 130
	"convert",
	convert_filter_init,
131 132
};

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

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

147
	filter->Set(out_audio_format);
148
}