RouteFilterPlugin.cxx 8.03 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 * 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.
 */

/** \file
 *
 * This filter copies audio data between channels. Useful for
 * upmixing mono/stereo audio to surround speaker configurations.
 *
 * Its configuration consists of a "filter" section with a single
 * "routes" entry, formatted as: \\
 * routes "0>1, 1>0, 2>2, 3>3, 3>4" \\
 * where each pair of numbers signifies a set of channels.
 * Each source>dest pair leads to the data from channel #source
 * being copied to channel #dest in the output.
 *
 * Example: \\
 * routes "0>0, 1>1, 0>2, 1>3"\\
 * upmixes stereo audio to a 4-speaker system, copying the front-left
 * (0) to front left (0) and rear left (2), copying front-right (1) to
 * front-right (1) and rear-right (3).
 *
 * If multiple sources are copied to the same destination channel, only
 * one of them takes effect.
 */

#include "config.h"
43 44
#include "config/ConfigError.hxx"
#include "config/ConfigData.hxx"
45
#include "AudioFormat.hxx"
46 47 48
#include "filter/FilterPlugin.hxx"
#include "filter/FilterInternal.hxx"
#include "filter/FilterRegistry.hxx"
49
#include "pcm/PcmBuffer.hxx"
50
#include "util/StringUtil.hxx"
51
#include "util/Error.hxx"
52
#include "util/ConstBuffer.hxx"
53

54 55
#include <algorithm>

56 57
#include <assert.h>
#include <string.h>
58
#include <stdint.h>
59
#include <stdlib.h>
60

61
class RouteFilter final : public Filter {
62 63 64 65
	/**
	 * The minimum number of channels we need for output
	 * to be able to perform all the copies the user has specified
	 */
66
	unsigned min_output_channels;
67 68 69 70 71 72 73

	/**
	 * The minimum number of input channels we need to
	 * copy all the data the user has requested. If fewer
	 * than this many are supplied by the input, undefined
	 * copy operations are given zeroed sources in stead.
	 */
74
	unsigned min_input_channels;
75 76 77 78 79 80 81

	/**
	 * The set of copy operations to perform on each sample
	 * The index is an output channel to use, the value is
	 * a corresponding input channel from which to take the
	 * data. A -1 means "no source"
	 */
82
	int8_t sources[MAX_CHANNELS];
83 84 85 86

	/**
	 * The actual input format of our signal, once opened
	 */
87
	AudioFormat input_format;
88 89 90 91

	/**
	 * The decided upon output format, once opened
	 */
92
	AudioFormat output_format;
93 94

	/**
95
	 * The size, in bytes, of each multichannel frame in the
96 97
	 * input buffer
	 */
98
	size_t input_frame_size;
99 100

	/**
101
	 * The size, in bytes, of each multichannel frame in the
102 103
	 * output buffer
	 */
104
	size_t output_frame_size;
105 106 107 108

	/**
	 * The output buffer used last time around, can be reused if the size doesn't differ.
	 */
109
	PcmBuffer output_buffer;
110

111 112 113 114 115 116 117 118 119 120
public:
	/**
	 * Parse the "routes" section, a string on the form
	 *  a>b, c>d, e>f, ...
	 * where a... are non-unique, non-negative integers
	 * and input channel a gets copied to output channel b, etc.
	 * @param param the configuration block to read
	 * @param filter a route_filter whose min_channels and sources[] to set
	 * @return true on success, false on error
	 */
121
	bool Configure(const config_param &param, Error &error);
122

123 124 125
	/* virtual methods from class Filter */
	AudioFormat Open(AudioFormat &af, Error &error) override;
	void Close() override;
126 127
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
				    Error &error) override;
128 129
};

130
bool
131
RouteFilter::Configure(const config_param &param, Error &error) {
132 133 134 135

	/* TODO:
	 * With a more clever way of marking "don't copy to output N",
	 * This could easily be merged into a single loop with some
136
	 * dynamic realloc() instead of one count run and one malloc().
137 138
	 */

139 140
	std::fill_n(sources, MAX_CHANNELS, -1);

141 142
	min_input_channels = 0;
	min_output_channels = 0;
143

144 145 146
	// A cowardly default, just passthrough stereo
	const char *routes = param.GetBlockValue("routes", "0>0, 1>1");
	while (true) {
147
		routes = StripLeft(routes);
148 149 150

		char *endptr;
		const unsigned source = strtoul(routes, &endptr, 10);
151
		endptr = StripLeft(endptr);
152 153 154 155 156
		if (endptr == routes || *endptr != '>') {
			error.Set(config_domain,
				  "Malformed 'routes' specification");
			return false;
		}
157

158
		if (source >= MAX_CHANNELS) {
159
			error.Format(config_domain,
160 161
				     "Invalid source channel number: %u",
				     source);
162
			return false;
163 164
		}

165 166
		if (source >= min_input_channels)
			min_input_channels = source + 1;
167

168
		routes = StripLeft(endptr + 1);
169

170
		unsigned dest = strtoul(routes, &endptr, 10);
171
		endptr = StripLeft(endptr);
172 173 174 175 176
		if (endptr == routes) {
			error.Set(config_domain,
				  "Malformed 'routes' specification");
			return false;
		}
177

178
		if (dest >= MAX_CHANNELS) {
179
			error.Format(config_domain,
180 181
				     "Invalid destination channel number: %u",
				     dest);
182
			return false;
183 184
		}

185 186
		if (dest >= min_output_channels)
			min_output_channels = dest + 1;
187

188
		sources[dest] = source;
189

190 191 192 193 194 195 196 197 198 199
		routes = endptr;

		if (*routes == 0)
			break;

		if (*routes != ',') {
			error.Set(config_domain,
				  "Malformed 'routes' specification");
			return false;
		}
200

201 202
		++routes;
	}
203 204

	return true;
205 206
}

207
static Filter *
208
route_filter_init(const config_param &param, Error &error)
209
{
210
	RouteFilter *filter = new RouteFilter();
211
	if (!filter->Configure(param, error)) {
212 213 214
		delete filter;
		return nullptr;
	}
215

216
	return filter;
217 218
}

219
AudioFormat
220
RouteFilter::Open(AudioFormat &audio_format, gcc_unused Error &error)
221 222
{
	// Copy the input format for later reference
223
	input_format = audio_format;
224
	input_frame_size = input_format.GetFrameSize();
225

226 227
	// Decide on an output format which has enough channels,
	// and is otherwise identical
228 229
	output_format = audio_format;
	output_format.channels = min_output_channels;
230 231

	// Precalculate this simple value, to speed up allocation later
232
	output_frame_size = output_format.GetFrameSize();
233

234
	return output_format;
235 236
}

237 238
void
RouteFilter::Close()
239
{
240
	output_buffer.Clear();
241 242
}

243 244
ConstBuffer<void>
RouteFilter::FilterPCM(ConstBuffer<void> src, gcc_unused Error &error)
245
{
246
	size_t number_of_frames = src.size / input_frame_size;
247

248
	const size_t bytes_per_frame_per_channel = input_format.GetSampleSize();
249

250
	// A moving pointer that always refers to channel 0 in the input, at the currently handled frame
251
	const uint8_t *base_source = (const uint8_t *)src.data;
252

253
	// Grow our reusable buffer, if needed, and set the moving pointer
254 255
	const size_t result_size = number_of_frames * output_frame_size;
	void *const result = output_buffer.Get(result_size);
256 257 258

	// A moving pointer that always refers to the currently filled channel of the currently handled frame, in the output
	uint8_t *chan_destination = (uint8_t *)result;
259 260

	// Perform our copy operations, with N input channels and M output channels
261
	for (unsigned int s=0; s<number_of_frames; ++s) {
262 263

		// Need to perform one copy per output channel
264 265 266
		for (unsigned int c=0; c<min_output_channels; ++c) {
			if (sources[c] == -1 ||
			    (unsigned)sources[c] >= input_format.channels) {
267 268 269 270
				// No source for this destination output,
				// give it zeroes as input
				memset(chan_destination,
					0x00,
271
					bytes_per_frame_per_channel);
272 273 274 275
			} else {
				// Get the data from channel sources[c]
				// and copy it to the output
				const uint8_t *data = base_source +
276
					(sources[c] * bytes_per_frame_per_channel);
277
				memcpy(chan_destination,
278
					  data,
279
					  bytes_per_frame_per_channel);
280 281
			}
			// Move on to the next output channel
282
			chan_destination += bytes_per_frame_per_channel;
283 284 285 286
		}


		// Go on to the next N input samples
287
		base_source += input_frame_size;
288 289 290
	}

	// Here it is, ladies and gentlemen! Rerouted data!
291
	return { result, result_size };
292 293 294
}

const struct filter_plugin route_filter_plugin = {
Max Kellermann's avatar
Max Kellermann committed
295 296
	"route",
	route_filter_init,
297
};