2 * Copyright (c) 2002 Anders Johansson <ajh@atri.curtin.edu.au>
3 * Copyright (c) 2011 Clément Bœsch <u pkh me>
4 * Copyright (c) 2011 Nicolas George <nicolas.george@normalesup.org>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Audio panning filter (channels mixing)
26 * Original code written by Anders Johansson for MPlayer,
27 * reimplemented for FFmpeg.
31 #include "libavutil/avstring.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/opt.h"
34 #include "libswresample/swresample.h"
40 #define MAX_CHANNELS 64
42 typedef struct PanContext {
45 int64_t out_channel_layout;
46 double gain[MAX_CHANNELS][MAX_CHANNELS];
49 int nb_output_channels;
52 /* channel mapping specific */
53 int channel_map[MAX_CHANNELS];
54 struct SwrContext *swr;
57 static void skip_spaces(char **arg)
61 sscanf(*arg, " %n", &len);
65 static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
68 int len, i, channel_id = 0;
69 int64_t layout, layout0;
72 /* try to parse a channel name, e.g. "FL" */
73 if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
74 layout0 = layout = av_get_channel_layout(buf);
75 /* channel_id <- first set bit in layout */
76 for (i = 32; i > 0; i >>= 1) {
77 if (layout >= (int64_t)1 << i) {
82 /* reject layouts that are not a single channel */
83 if (channel_id >= MAX_CHANNELS || layout0 != (int64_t)1 << channel_id)
84 return AVERROR(EINVAL);
85 *rchannel = channel_id;
90 /* try to parse a channel number, e.g. "c2" */
91 if (sscanf(*arg, "c%d%n", &channel_id, &len) &&
92 channel_id >= 0 && channel_id < MAX_CHANNELS) {
93 *rchannel = channel_id;
98 return AVERROR(EINVAL);
101 static av_cold int init(AVFilterContext *ctx)
103 PanContext *const pan = ctx->priv;
104 char *arg, *arg0, *tokenizer, *args = av_strdup(pan->args);
105 int out_ch_id, in_ch_id, len, named, ret, sign = 1;
106 int nb_in_channels[2] = { 0, 0 }; // number of unnamed and named input channels
107 int used_out_ch[MAX_CHANNELS] = {0};
111 av_log(ctx, AV_LOG_ERROR,
112 "pan filter needs a channel layout and a set "
113 "of channel definitions as parameter\n");
114 return AVERROR(EINVAL);
117 return AVERROR(ENOMEM);
118 arg = av_strtok(args, "|", &tokenizer);
120 av_log(ctx, AV_LOG_ERROR, "Channel layout not specified\n");
121 ret = AVERROR(EINVAL);
124 ret = ff_parse_channel_layout(&pan->out_channel_layout,
125 &pan->nb_output_channels, arg, ctx);
129 /* parse channel specifications */
130 while ((arg = arg0 = av_strtok(NULL, "|", &tokenizer))) {
131 int used_in_ch[MAX_CHANNELS] = {0};
133 if (parse_channel_name(&arg, &out_ch_id, &named)) {
134 av_log(ctx, AV_LOG_ERROR,
135 "Expected out channel name, got \"%.8s\"\n", arg);
136 ret = AVERROR(EINVAL);
140 if (!((pan->out_channel_layout >> out_ch_id) & 1)) {
141 av_log(ctx, AV_LOG_ERROR,
142 "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
143 ret = AVERROR(EINVAL);
146 /* get the channel number in the output channel layout:
147 * out_channel_layout & ((1 << out_ch_id) - 1) are all the
148 * channels that come before out_ch_id,
149 * so their count is the index of out_ch_id */
150 out_ch_id = av_get_channel_layout_nb_channels(pan->out_channel_layout & (((int64_t)1 << out_ch_id) - 1));
152 if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
153 av_log(ctx, AV_LOG_ERROR,
154 "Invalid out channel name \"%.8s\"\n", arg0);
155 ret = AVERROR(EINVAL);
158 if (used_out_ch[out_ch_id]) {
159 av_log(ctx, AV_LOG_ERROR,
160 "Can not reference out channel %d twice\n", out_ch_id);
161 ret = AVERROR(EINVAL);
164 used_out_ch[out_ch_id] = 1;
168 } else if (*arg == '<') {
169 pan->need_renorm |= (int64_t)1 << out_ch_id;
172 av_log(ctx, AV_LOG_ERROR,
173 "Syntax error after channel name in \"%.8s\"\n", arg0);
174 ret = AVERROR(EINVAL);
181 if (sscanf(arg, "%lf%n *%n", &gain, &len, &len))
183 if (parse_channel_name(&arg, &in_ch_id, &named)){
184 av_log(ctx, AV_LOG_ERROR,
185 "Expected in channel name, got \"%.8s\"\n", arg);
186 ret = AVERROR(EINVAL);
189 nb_in_channels[named]++;
190 if (nb_in_channels[!named]) {
191 av_log(ctx, AV_LOG_ERROR,
192 "Can not mix named and numbered channels\n");
193 ret = AVERROR(EINVAL);
196 if (used_in_ch[in_ch_id]) {
197 av_log(ctx, AV_LOG_ERROR,
198 "Can not reference in channel %d twice\n", in_ch_id);
199 ret = AVERROR(EINVAL);
202 used_in_ch[in_ch_id] = 1;
203 pan->gain[out_ch_id][in_ch_id] = sign * gain;
209 } else if (*arg != '+') {
210 av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
211 ret = AVERROR(EINVAL);
219 pan->need_renumber = !!nb_in_channels[1];
227 static int are_gains_pure(const PanContext *pan)
231 for (i = 0; i < MAX_CHANNELS; i++) {
234 for (j = 0; j < MAX_CHANNELS; j++) {
235 double gain = pan->gain[i][j];
237 /* channel mapping is effective only if 0% or 100% of a channel is
239 if (gain != 0. && gain != 1.)
241 /* ...and if the output channel is only composed of one input */
242 if (gain && nb_gain++)
249 static int query_formats(AVFilterContext *ctx)
251 PanContext *pan = ctx->priv;
252 AVFilterLink *inlink = ctx->inputs[0];
253 AVFilterLink *outlink = ctx->outputs[0];
254 AVFilterFormats *formats = NULL;
255 AVFilterChannelLayouts *layouts;
258 pan->pure_gains = are_gains_pure(pan);
259 /* libswr supports any sample and packing formats */
260 if ((ret = ff_set_common_formats(ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO))) < 0)
263 formats = ff_all_samplerates();
264 if ((ret = ff_set_common_samplerates(ctx, formats)) < 0)
267 // inlink supports any channel layout
268 layouts = ff_all_channel_counts();
269 if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
272 // outlink supports only requested output channel layout
274 if ((ret = ff_add_channel_layout(&layouts,
275 pan->out_channel_layout ? pan->out_channel_layout :
276 FF_COUNT2LAYOUT(pan->nb_output_channels))) < 0)
278 return ff_channel_layouts_ref(layouts, &outlink->incfg.channel_layouts);
281 static int config_props(AVFilterLink *link)
283 AVFilterContext *ctx = link->dst;
284 PanContext *pan = ctx->priv;
285 char buf[1024], *cur;
289 if (pan->need_renumber) {
290 // input channels were given by their name: renumber them
291 for (i = j = 0; i < MAX_CHANNELS; i++) {
292 if ((link->channel_layout >> i) & 1) {
293 for (k = 0; k < pan->nb_output_channels; k++)
294 pan->gain[k][j] = pan->gain[k][i];
300 // sanity check; can't be done in query_formats since the inlink
301 // channel layout is unknown at that time
302 if (link->channels > MAX_CHANNELS ||
303 pan->nb_output_channels > MAX_CHANNELS) {
304 av_log(ctx, AV_LOG_ERROR,
305 "af_pan supports a maximum of %d channels. "
306 "Feel free to ask for a higher limit.\n", MAX_CHANNELS);
307 return AVERROR_PATCHWELCOME;
310 // init libswresample context
311 pan->swr = swr_alloc_set_opts(pan->swr,
312 pan->out_channel_layout, link->format, link->sample_rate,
313 link->channel_layout, link->format, link->sample_rate,
316 return AVERROR(ENOMEM);
317 if (!link->channel_layout) {
318 if (av_opt_set_int(pan->swr, "ich", link->channels, 0) < 0)
319 return AVERROR(EINVAL);
321 if (!pan->out_channel_layout) {
322 if (av_opt_set_int(pan->swr, "och", pan->nb_output_channels, 0) < 0)
323 return AVERROR(EINVAL);
326 // gains are pure, init the channel mapping
327 if (pan->pure_gains) {
329 // get channel map from the pure gains
330 for (i = 0; i < pan->nb_output_channels; i++) {
332 for (j = 0; j < link->channels; j++) {
333 if (pan->gain[i][j]) {
338 pan->channel_map[i] = ch_id;
341 av_opt_set_int(pan->swr, "icl", pan->out_channel_layout, 0);
342 av_opt_set_int(pan->swr, "uch", pan->nb_output_channels, 0);
343 swr_set_channel_mapping(pan->swr, pan->channel_map);
346 for (i = 0; i < pan->nb_output_channels; i++) {
347 if (!((pan->need_renorm >> i) & 1))
350 for (j = 0; j < link->channels; j++)
351 t += fabs(pan->gain[i][j]);
352 if (t > -1E-5 && t < 1E-5) {
353 // t is almost 0 but not exactly, this is probably a mistake
355 av_log(ctx, AV_LOG_WARNING,
356 "Degenerate coefficients while renormalizing\n");
359 for (j = 0; j < link->channels; j++)
360 pan->gain[i][j] /= t;
362 av_opt_set_int(pan->swr, "icl", link->channel_layout, 0);
363 av_opt_set_int(pan->swr, "ocl", pan->out_channel_layout, 0);
364 swr_set_matrix(pan->swr, pan->gain[0], pan->gain[1] - pan->gain[0]);
367 r = swr_init(pan->swr);
372 for (i = 0; i < pan->nb_output_channels; i++) {
374 for (j = 0; j < link->channels; j++) {
375 r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
376 j ? " + " : "", pan->gain[i][j], j);
377 cur += FFMIN(buf + sizeof(buf) - cur, r);
379 av_log(ctx, AV_LOG_VERBOSE, "o%d = %s\n", i, buf);
381 // add channel mapping summary if possible
382 if (pan->pure_gains) {
383 av_log(ctx, AV_LOG_INFO, "Pure channel mapping detected:");
384 for (i = 0; i < pan->nb_output_channels; i++)
385 if (pan->channel_map[i] < 0)
386 av_log(ctx, AV_LOG_INFO, " M");
388 av_log(ctx, AV_LOG_INFO, " %d", pan->channel_map[i]);
389 av_log(ctx, AV_LOG_INFO, "\n");
395 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
398 int n = insamples->nb_samples;
399 AVFilterLink *const outlink = inlink->dst->outputs[0];
400 AVFrame *outsamples = ff_get_audio_buffer(outlink, n);
401 PanContext *pan = inlink->dst->priv;
404 av_frame_free(&insamples);
405 return AVERROR(ENOMEM);
407 swr_convert(pan->swr, outsamples->extended_data, n,
408 (void *)insamples->extended_data, n);
409 av_frame_copy_props(outsamples, insamples);
410 outsamples->channel_layout = outlink->channel_layout;
411 outsamples->channels = outlink->channels;
413 ret = ff_filter_frame(outlink, outsamples);
414 av_frame_free(&insamples);
418 static av_cold void uninit(AVFilterContext *ctx)
420 PanContext *pan = ctx->priv;
424 #define OFFSET(x) offsetof(PanContext, x)
426 static const AVOption pan_options[] = {
427 { "args", NULL, OFFSET(args), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
431 AVFILTER_DEFINE_CLASS(pan);
433 static const AVFilterPad pan_inputs[] = {
436 .type = AVMEDIA_TYPE_AUDIO,
437 .config_props = config_props,
438 .filter_frame = filter_frame,
443 static const AVFilterPad pan_outputs[] = {
446 .type = AVMEDIA_TYPE_AUDIO,
451 AVFilter ff_af_pan = {
453 .description = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
454 .priv_size = sizeof(PanContext),
455 .priv_class = &pan_class,
458 .query_formats = query_formats,
459 .inputs = pan_inputs,
460 .outputs = pan_outputs,