RouteFilterPlugin.cxx 8.17 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 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
#include "config/ConfigError.hxx"
44
#include "config/Block.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 "pcm/Silence.hxx"
51
#include "util/StringUtil.hxx"
52
#include "util/RuntimeError.hxx"
53
#include "util/ConstBuffer.hxx"
54
#include "util/WritableBuffer.hxx"
55

56
#include <algorithm>
57
#include <array>
58

59
#include <string.h>
60
#include <stdint.h>
61
#include <stdlib.h>
62

63
class RouteFilter final : public Filter {
64 65 66 67 68 69
	/**
	 * 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"
	 */
70
	const std::array<int8_t, MAX_CHANNELS> sources;
71 72 73 74

	/**
	 * The actual input format of our signal, once opened
	 */
75
	const AudioFormat input_format;
76 77

	/**
78
	 * The size, in bytes, of each multichannel frame in the
79 80
	 * input buffer
	 */
81
	const size_t input_frame_size;
82 83

	/**
84
	 * The size, in bytes, of each multichannel frame in the
85 86
	 * output buffer
	 */
87
	size_t output_frame_size;
88 89 90 91

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

94 95 96 97 98
public:
	RouteFilter(const AudioFormat &audio_format, unsigned out_channels,
		    const std::array<int8_t, MAX_CHANNELS> &_sources);

	/* virtual methods from class Filter */
99
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src) override;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
};

class PreparedRouteFilter final : public PreparedFilter {
	/**
	 * The minimum number of channels we need for output
	 * to be able to perform all the copies the user has specified
	 */
	unsigned min_output_channels;

	/**
	 * 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.
	 */
	unsigned min_input_channels;

	/**
	 * 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"
	 */
	std::array<int8_t, MAX_CHANNELS> sources;

125 126 127 128 129 130
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.
131
	 * @param block the configuration block to read
132 133
	 * @param filter a route_filter whose min_channels and sources[] to set
	 */
134
	PreparedRouteFilter(const ConfigBlock &block);
135

136
	/* virtual methods from class PreparedFilter */
137
	Filter *Open(AudioFormat &af) override;
138 139
};

140
PreparedRouteFilter::PreparedRouteFilter(const ConfigBlock &block)
141
{
142 143 144
	/* 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
145
	 * dynamic realloc() instead of one count run and one malloc().
146 147
	 */

148
	sources.fill(-1);
149

150 151
	min_input_channels = 0;
	min_output_channels = 0;
152

153
	// A cowardly default, just passthrough stereo
154
	const char *routes = block.GetBlockValue("routes", "0>0, 1>1");
155
	while (true) {
156
		routes = StripLeft(routes);
157 158 159

		char *endptr;
		const unsigned source = strtoul(routes, &endptr, 10);
160
		endptr = StripLeft(endptr);
161 162 163 164 165 166
		if (endptr == routes || *endptr != '>')
			throw std::runtime_error("Malformed 'routes' specification");

		if (source >= MAX_CHANNELS)
			throw FormatRuntimeError("Invalid source channel number: %u",
						 source);
167

168 169
		if (source >= min_input_channels)
			min_input_channels = source + 1;
170

171
		routes = StripLeft(endptr + 1);
172

173
		unsigned dest = strtoul(routes, &endptr, 10);
174
		endptr = StripLeft(endptr);
175 176 177 178 179 180
		if (endptr == routes)
			throw std::runtime_error("Malformed 'routes' specification");

		if (dest >= MAX_CHANNELS)
			throw FormatRuntimeError("Invalid destination channel number: %u",
						 dest);
181

182 183
		if (dest >= min_output_channels)
			min_output_channels = dest + 1;
184

185
		sources[dest] = source;
186

187 188 189 190 191
		routes = endptr;

		if (*routes == 0)
			break;

192 193
		if (*routes != ',')
			throw std::runtime_error("Malformed 'routes' specification");
194

195 196
		++routes;
	}
197 198
}

199
static PreparedFilter *
200
route_filter_init(const ConfigBlock &block)
201
{
202
	return new PreparedRouteFilter(block);
203 204
}

205 206 207 208 209
RouteFilter::RouteFilter(const AudioFormat &audio_format,
			 unsigned out_channels,
			 const std::array<int8_t, MAX_CHANNELS> &_sources)
	:Filter(audio_format), sources(_sources), input_format(audio_format),
	 input_frame_size(input_format.GetFrameSize())
210 211 212
{
	// Decide on an output format which has enough channels,
	// and is otherwise identical
213
	out_audio_format.channels = out_channels;
214 215

	// Precalculate this simple value, to speed up allocation later
216
	output_frame_size = out_audio_format.GetFrameSize();
217 218
}

219
Filter *
220
PreparedRouteFilter::Open(AudioFormat &audio_format)
221
{
222
	return new RouteFilter(audio_format, min_output_channels, sources);
223 224
}

225
ConstBuffer<void>
226
RouteFilter::FilterPCM(ConstBuffer<void> src)
227
{
228
	size_t number_of_frames = src.size / input_frame_size;
229

230
	const size_t bytes_per_frame_per_channel = input_format.GetSampleSize();
231

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

235
	// Grow our reusable buffer, if needed, and set the moving pointer
236 237
	const size_t result_size = number_of_frames * output_frame_size;
	void *const result = output_buffer.Get(result_size);
238 239 240

	// 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;
241 242

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

		// Need to perform one copy per output channel
246
		for (unsigned c = 0; c < out_audio_format.channels; ++c) {
247 248
			if (sources[c] == -1 ||
			    (unsigned)sources[c] >= input_format.channels) {
249 250
				// No source for this destination output,
				// give it zeroes as input
251 252
				PcmSilence({chan_destination, bytes_per_frame_per_channel},
					   input_format.format);
253 254 255 256
			} else {
				// Get the data from channel sources[c]
				// and copy it to the output
				const uint8_t *data = base_source +
257
					(sources[c] * bytes_per_frame_per_channel);
258
				memcpy(chan_destination,
259
					  data,
260
					  bytes_per_frame_per_channel);
261 262
			}
			// Move on to the next output channel
263
			chan_destination += bytes_per_frame_per_channel;
264 265 266 267
		}


		// Go on to the next N input samples
268
		base_source += input_frame_size;
269 270 271
	}

	// Here it is, ladies and gentlemen! Rerouted data!
272
	return { result, result_size };
273 274
}

275
const FilterPlugin route_filter_plugin = {
Max Kellermann's avatar
Max Kellermann committed
276 277
	"route",
	route_filter_init,
278
};