ConvertFilterPlugin.cxx 3.54 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 31 32
#include "poison.h"

#include <assert.h>

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

	/**
	 * The output audio format; the consumer of this plugin
42 43 44 45 46
	 * expects PCM data in this format.
	 *
	 * If this is AudioFormat::Undefined(), then the #PcmConvert
	 * attribute is not open.  This can mean that Set() has failed
	 * or that no conversion is necessary.
47
	 */
48
	AudioFormat out_audio_format;
49

50
	Manual<PcmConvert> state;
51

52
public:
53
	bool Set(const AudioFormat &_out_audio_format, Error &error);
54

55 56
	virtual AudioFormat Open(AudioFormat &af, Error &error) override;
	virtual void Close() override;
57 58
	virtual ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
					    Error &error) override;
59 60
};

61
static Filter *
62
convert_filter_init(gcc_unused const ConfigBlock &block,
63
		    gcc_unused Error &error)
64
{
65
	return new ConvertFilter();
66 67
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
bool
ConvertFilter::Set(const AudioFormat &_out_audio_format, Error &error)
{
	assert(in_audio_format.IsValid());
	assert(_out_audio_format.IsValid());

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

	if (out_audio_format.IsValid()) {
		out_audio_format.Clear();
		state->Close();
	}

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

	if (!state->Open(in_audio_format, _out_audio_format, error))
		return false;

	out_audio_format = _out_audio_format;
	return true;
}

94
AudioFormat
95
ConvertFilter::Open(AudioFormat &audio_format, gcc_unused Error &error)
96
{
97
	assert(audio_format.IsValid());
98

99 100 101
	in_audio_format = audio_format;
	out_audio_format.Clear();

102
	state.Construct();
103

104
	return in_audio_format;
105 106
}

107 108
void
ConvertFilter::Close()
109
{
110 111 112 113 114
	assert(in_audio_format.IsValid());

	if (out_audio_format.IsValid())
		state->Close();

115
	state.Destruct();
116

117 118
	poison_undefined(&in_audio_format, sizeof(in_audio_format));
	poison_undefined(&out_audio_format, sizeof(out_audio_format));
119 120
}

121 122
ConstBuffer<void>
ConvertFilter::FilterPCM(ConstBuffer<void> src, Error &error)
123
{
124 125
	assert(in_audio_format.IsValid());

126
	if (!out_audio_format.IsValid())
127 128 129
		/* optimized special case: no-op */
		return src;

130
	return state->Convert(src, error);
131 132 133
}

const struct filter_plugin convert_filter_plugin = {
134 135
	"convert",
	convert_filter_init,
136 137
};

138 139 140
bool
convert_filter_set(Filter *_filter, AudioFormat out_audio_format,
		   Error &error)
141
{
142
	ConvertFilter *filter = (ConvertFilter *)_filter;
143

144
	return filter->Set(out_audio_format, error);
145
}