2011-11-05 16:48:41 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2002 Anders Johansson <ajh@atri.curtin.edu.au>
|
2013-09-03 21:42:24 +03:00
|
|
|
* Copyright (c) 2011 Clément Bœsch <u pkh me>
|
2011-11-05 16:48:41 +03:00
|
|
|
* Copyright (c) 2011 Nicolas George <nicolas.george@normalesup.org>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg 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
|
2012-08-02 20:28:55 +03:00
|
|
|
* GNU Lesser General Public License for more details.
|
2011-11-05 16:48:41 +03:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Audio panning filter (channels mixing)
|
|
|
|
* Original code written by Anders Johansson for MPlayer,
|
|
|
|
* reimplemented for FFmpeg.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "libavutil/avstring.h"
|
2012-11-13 16:16:48 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2012-01-18 14:00:16 +03:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libswresample/swresample.h"
|
2012-05-12 18:58:14 +03:00
|
|
|
#include "audio.h"
|
2011-11-05 16:48:41 +03:00
|
|
|
#include "avfilter.h"
|
2012-05-16 03:27:31 +03:00
|
|
|
#include "formats.h"
|
2012-05-17 20:32:17 +03:00
|
|
|
#include "internal.h"
|
2011-11-05 16:48:41 +03:00
|
|
|
|
2015-07-17 00:17:38 +02:00
|
|
|
#define MAX_CHANNELS 64
|
2011-11-05 16:48:41 +03:00
|
|
|
|
2012-01-18 14:00:16 +03:00
|
|
|
typedef struct PanContext {
|
2013-04-12 11:32:41 +03:00
|
|
|
const AVClass *class;
|
|
|
|
char *args;
|
2021-08-31 16:03:14 +02:00
|
|
|
AVChannelLayout out_channel_layout;
|
2012-02-20 23:25:12 +03:00
|
|
|
double gain[MAX_CHANNELS][MAX_CHANNELS];
|
2011-11-05 16:48:41 +03:00
|
|
|
int64_t need_renorm;
|
|
|
|
int need_renumber;
|
|
|
|
int nb_output_channels;
|
2012-01-18 14:00:16 +03:00
|
|
|
|
|
|
|
int pure_gains;
|
|
|
|
/* channel mapping specific */
|
2014-08-10 02:07:20 +03:00
|
|
|
int channel_map[MAX_CHANNELS];
|
2012-01-18 14:00:16 +03:00
|
|
|
struct SwrContext *swr;
|
2011-11-05 16:48:41 +03:00
|
|
|
} PanContext;
|
|
|
|
|
2013-04-12 11:45:11 +03:00
|
|
|
static void skip_spaces(char **arg)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
sscanf(*arg, " %n", &len);
|
|
|
|
*arg += len;
|
|
|
|
}
|
|
|
|
|
2011-11-05 16:48:41 +03:00
|
|
|
static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
|
|
|
|
{
|
|
|
|
char buf[8];
|
2021-08-31 16:03:14 +02:00
|
|
|
int len, channel_id = 0;
|
2011-11-05 16:48:41 +03:00
|
|
|
|
2013-04-12 11:45:11 +03:00
|
|
|
skip_spaces(arg);
|
2012-02-15 21:41:41 +03:00
|
|
|
/* try to parse a channel name, e.g. "FL" */
|
2013-04-12 11:45:11 +03:00
|
|
|
if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
|
2021-08-31 16:03:14 +02:00
|
|
|
channel_id = av_channel_from_string(buf);
|
|
|
|
if (channel_id < 0)
|
|
|
|
return channel_id;
|
|
|
|
|
2011-11-05 16:48:41 +03:00
|
|
|
*rchannel = channel_id;
|
|
|
|
*rnamed = 1;
|
|
|
|
*arg += len;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-15 21:41:41 +03:00
|
|
|
/* try to parse a channel number, e.g. "c2" */
|
2012-09-14 19:45:34 +03:00
|
|
|
if (sscanf(*arg, "c%d%n", &channel_id, &len) &&
|
2011-11-05 16:48:41 +03:00
|
|
|
channel_id >= 0 && channel_id < MAX_CHANNELS) {
|
|
|
|
*rchannel = channel_id;
|
|
|
|
*rnamed = 0;
|
|
|
|
*arg += len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2013-04-12 12:13:33 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2011-11-05 16:48:41 +03:00
|
|
|
{
|
|
|
|
PanContext *const pan = ctx->priv;
|
2013-04-12 11:32:41 +03:00
|
|
|
char *arg, *arg0, *tokenizer, *args = av_strdup(pan->args);
|
2016-10-28 14:13:25 +02:00
|
|
|
int out_ch_id, in_ch_id, len, named, ret, sign = 1;
|
2011-11-05 16:48:41 +03:00
|
|
|
int nb_in_channels[2] = { 0, 0 }; // number of unnamed and named input channels
|
2018-03-24 23:57:34 +02:00
|
|
|
int used_out_ch[MAX_CHANNELS] = {0};
|
2011-11-05 16:48:41 +03:00
|
|
|
double gain;
|
|
|
|
|
2013-04-12 11:32:41 +03:00
|
|
|
if (!pan->args) {
|
2012-01-17 13:01:37 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"pan filter needs a channel layout and a set "
|
2016-10-09 12:57:00 +02:00
|
|
|
"of channel definitions as parameter\n");
|
2012-01-17 13:01:37 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2011-11-05 16:48:41 +03:00
|
|
|
if (!args)
|
|
|
|
return AVERROR(ENOMEM);
|
2013-04-12 11:32:41 +03:00
|
|
|
arg = av_strtok(args, "|", &tokenizer);
|
2017-02-05 03:25:04 +02:00
|
|
|
if (!arg) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Channel layout not specified\n");
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-10-25 17:02:04 +03:00
|
|
|
ret = ff_parse_channel_layout(&pan->out_channel_layout,
|
|
|
|
&pan->nb_output_channels, arg, ctx);
|
2012-05-17 20:32:17 +03:00
|
|
|
if (ret < 0)
|
2012-10-13 02:32:50 +03:00
|
|
|
goto fail;
|
2011-11-05 16:48:41 +03:00
|
|
|
|
|
|
|
/* parse channel specifications */
|
2013-04-12 11:32:41 +03:00
|
|
|
while ((arg = arg0 = av_strtok(NULL, "|", &tokenizer))) {
|
2018-03-24 23:57:34 +02:00
|
|
|
int used_in_ch[MAX_CHANNELS] = {0};
|
2011-11-05 16:48:41 +03:00
|
|
|
/* channel name */
|
|
|
|
if (parse_channel_name(&arg, &out_ch_id, &named)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Expected out channel name, got \"%.8s\"\n", arg);
|
2012-10-13 02:32:50 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2011-11-05 16:48:41 +03:00
|
|
|
}
|
|
|
|
if (named) {
|
2021-08-31 16:03:14 +02:00
|
|
|
if ((out_ch_id = av_channel_layout_index_from_channel(&pan->out_channel_layout, out_ch_id)) < 0) {
|
2011-11-05 16:48:41 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
|
2012-10-13 02:32:50 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2011-11-05 16:48:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Invalid out channel name \"%.8s\"\n", arg0);
|
2012-10-13 02:32:50 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2011-11-05 16:48:41 +03:00
|
|
|
}
|
2018-03-24 23:57:34 +02:00
|
|
|
if (used_out_ch[out_ch_id]) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Can not reference out channel %d twice\n", out_ch_id);
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
used_out_ch[out_ch_id] = 1;
|
2012-09-14 19:45:34 +03:00
|
|
|
skip_spaces(&arg);
|
2011-11-05 16:48:41 +03:00
|
|
|
if (*arg == '=') {
|
|
|
|
arg++;
|
|
|
|
} else if (*arg == '<') {
|
|
|
|
pan->need_renorm |= (int64_t)1 << out_ch_id;
|
|
|
|
arg++;
|
|
|
|
} else {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Syntax error after channel name in \"%.8s\"\n", arg0);
|
2012-10-13 02:32:50 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2011-11-05 16:48:41 +03:00
|
|
|
}
|
|
|
|
/* gains */
|
2017-11-18 14:33:08 +02:00
|
|
|
sign = 1;
|
2011-11-05 16:48:41 +03:00
|
|
|
while (1) {
|
|
|
|
gain = 1;
|
2012-09-14 19:45:34 +03:00
|
|
|
if (sscanf(arg, "%lf%n *%n", &gain, &len, &len))
|
2011-11-05 16:48:41 +03:00
|
|
|
arg += len;
|
|
|
|
if (parse_channel_name(&arg, &in_ch_id, &named)){
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Expected in channel name, got \"%.8s\"\n", arg);
|
2012-10-13 02:32:50 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2011-11-05 16:48:41 +03:00
|
|
|
}
|
|
|
|
nb_in_channels[named]++;
|
|
|
|
if (nb_in_channels[!named]) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Can not mix named and numbered channels\n");
|
2012-10-13 02:32:50 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2011-11-05 16:48:41 +03:00
|
|
|
}
|
2018-03-24 23:57:34 +02:00
|
|
|
if (used_in_ch[in_ch_id]) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Can not reference in channel %d twice\n", in_ch_id);
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
used_in_ch[in_ch_id] = 1;
|
2016-10-28 14:13:25 +02:00
|
|
|
pan->gain[out_ch_id][in_ch_id] = sign * gain;
|
2012-09-14 19:45:34 +03:00
|
|
|
skip_spaces(&arg);
|
2011-11-05 16:48:41 +03:00
|
|
|
if (!*arg)
|
|
|
|
break;
|
2016-10-28 14:13:25 +02:00
|
|
|
if (*arg == '-') {
|
|
|
|
sign = -1;
|
|
|
|
} else if (*arg != '+') {
|
2011-11-05 16:48:41 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
|
2012-10-13 02:32:50 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2016-10-28 14:13:25 +02:00
|
|
|
} else {
|
|
|
|
sign = 1;
|
2011-11-05 16:48:41 +03:00
|
|
|
}
|
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pan->need_renumber = !!nb_in_channels[1];
|
|
|
|
|
2012-10-13 02:32:50 +03:00
|
|
|
ret = 0;
|
|
|
|
fail:
|
2011-11-05 16:48:41 +03:00
|
|
|
av_free(args);
|
2012-10-13 02:32:50 +03:00
|
|
|
return ret;
|
2011-11-05 16:48:41 +03:00
|
|
|
}
|
|
|
|
|
2012-01-18 14:00:16 +03:00
|
|
|
static int are_gains_pure(const PanContext *pan)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_CHANNELS; i++) {
|
|
|
|
int nb_gain = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < MAX_CHANNELS; j++) {
|
2012-02-20 23:25:12 +03:00
|
|
|
double gain = pan->gain[i][j];
|
2012-01-18 14:00:16 +03:00
|
|
|
|
|
|
|
/* channel mapping is effective only if 0% or 100% of a channel is
|
|
|
|
* selected... */
|
|
|
|
if (gain != 0. && gain != 1.)
|
|
|
|
return 0;
|
|
|
|
/* ...and if the output channel is only composed of one input */
|
|
|
|
if (gain && nb_gain++)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-20 23:45:18 +03:00
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
PanContext *pan = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2012-05-16 03:27:31 +03:00
|
|
|
AVFilterChannelLayouts *layouts;
|
2015-10-05 05:39:25 +02:00
|
|
|
int ret;
|
2012-02-20 23:45:18 +03:00
|
|
|
|
|
|
|
pan->pure_gains = are_gains_pure(pan);
|
|
|
|
/* libswr supports any sample and packing formats */
|
2015-10-05 05:39:25 +02:00
|
|
|
if ((ret = ff_set_common_formats(ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO))) < 0)
|
|
|
|
return ret;
|
2012-02-20 23:45:18 +03:00
|
|
|
|
2021-08-10 01:25:31 +02:00
|
|
|
if ((ret = ff_set_common_all_samplerates(ctx)) < 0)
|
2015-10-05 05:39:25 +02:00
|
|
|
return ret;
|
2012-05-17 17:31:52 +03:00
|
|
|
|
2012-02-20 23:45:18 +03:00
|
|
|
// inlink supports any channel layout
|
2013-10-25 17:12:06 +03:00
|
|
|
layouts = ff_all_channel_counts();
|
2020-08-24 12:16:34 +02:00
|
|
|
if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
|
2015-10-05 05:39:25 +02:00
|
|
|
return ret;
|
2012-02-20 23:45:18 +03:00
|
|
|
|
|
|
|
// outlink supports only requested output channel layout
|
2012-05-16 03:27:31 +03:00
|
|
|
layouts = NULL;
|
2021-08-31 16:03:14 +02:00
|
|
|
if ((ret = ff_add_channel_layout(&layouts, &pan->out_channel_layout)) < 0)
|
2015-10-05 05:39:25 +02:00
|
|
|
return ret;
|
2020-08-24 12:16:34 +02:00
|
|
|
return ff_channel_layouts_ref(layouts, &outlink->incfg.channel_layouts);
|
2012-02-20 23:45:18 +03:00
|
|
|
}
|
|
|
|
|
2011-11-05 16:48:41 +03:00
|
|
|
static int config_props(AVFilterLink *link)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = link->dst;
|
|
|
|
PanContext *pan = ctx->priv;
|
|
|
|
char buf[1024], *cur;
|
2021-08-31 16:03:14 +02:00
|
|
|
int i, j, k, r, ret;
|
2011-11-05 16:48:41 +03:00
|
|
|
double t;
|
|
|
|
|
|
|
|
if (pan->need_renumber) {
|
|
|
|
// input channels were given by their name: renumber them
|
|
|
|
for (i = j = 0; i < MAX_CHANNELS; i++) {
|
2021-08-31 16:03:14 +02:00
|
|
|
if (av_channel_layout_index_from_channel(&link->ch_layout, i) >= 0) {
|
2011-11-05 16:48:41 +03:00
|
|
|
for (k = 0; k < pan->nb_output_channels; k++)
|
2012-02-20 23:25:12 +03:00
|
|
|
pan->gain[k][j] = pan->gain[k][i];
|
2011-11-05 16:48:41 +03:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-18 14:00:16 +03:00
|
|
|
|
2012-02-20 22:15:09 +03:00
|
|
|
// sanity check; can't be done in query_formats since the inlink
|
|
|
|
// channel layout is unknown at that time
|
2021-08-31 16:03:14 +02:00
|
|
|
if (link->ch_layout.nb_channels > MAX_CHANNELS ||
|
2014-08-10 02:07:20 +03:00
|
|
|
pan->nb_output_channels > MAX_CHANNELS) {
|
2012-02-20 22:15:09 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
2016-10-09 12:57:00 +02:00
|
|
|
"af_pan supports a maximum of %d channels. "
|
2014-08-10 02:07:20 +03:00
|
|
|
"Feel free to ask for a higher limit.\n", MAX_CHANNELS);
|
2012-02-20 22:15:09 +03:00
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
2012-01-18 14:00:16 +03:00
|
|
|
|
2012-02-20 22:15:09 +03:00
|
|
|
// init libswresample context
|
2021-08-31 16:03:14 +02:00
|
|
|
ret = swr_alloc_set_opts2(&pan->swr,
|
|
|
|
&pan->out_channel_layout, link->format, link->sample_rate,
|
|
|
|
&link->ch_layout, link->format, link->sample_rate,
|
|
|
|
0, ctx);
|
|
|
|
if (ret < 0)
|
2012-02-20 22:15:09 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2012-02-16 14:34:39 +03:00
|
|
|
|
|
|
|
// gains are pure, init the channel mapping
|
|
|
|
if (pan->pure_gains) {
|
|
|
|
|
2012-01-18 14:00:16 +03:00
|
|
|
// get channel map from the pure gains
|
|
|
|
for (i = 0; i < pan->nb_output_channels; i++) {
|
|
|
|
int ch_id = -1;
|
2021-08-31 16:03:14 +02:00
|
|
|
for (j = 0; j < link->ch_layout.nb_channels; j++) {
|
2012-02-20 23:25:12 +03:00
|
|
|
if (pan->gain[i][j]) {
|
2012-01-18 14:00:16 +03:00
|
|
|
ch_id = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pan->channel_map[i] = ch_id;
|
|
|
|
}
|
|
|
|
|
2023-02-17 20:43:30 +02:00
|
|
|
av_opt_set_chlayout(pan->swr, "uchl", &pan->out_channel_layout, 0);
|
2012-01-18 14:00:16 +03:00
|
|
|
swr_set_channel_mapping(pan->swr, pan->channel_map);
|
|
|
|
} else {
|
2012-01-23 13:27:11 +03:00
|
|
|
// renormalize
|
|
|
|
for (i = 0; i < pan->nb_output_channels; i++) {
|
|
|
|
if (!((pan->need_renorm >> i) & 1))
|
|
|
|
continue;
|
|
|
|
t = 0;
|
2021-08-31 16:03:14 +02:00
|
|
|
for (j = 0; j < link->ch_layout.nb_channels; j++)
|
2016-10-09 03:49:15 +02:00
|
|
|
t += fabs(pan->gain[i][j]);
|
2012-01-23 13:27:11 +03:00
|
|
|
if (t > -1E-5 && t < 1E-5) {
|
|
|
|
// t is almost 0 but not exactly, this is probably a mistake
|
|
|
|
if (t)
|
|
|
|
av_log(ctx, AV_LOG_WARNING,
|
|
|
|
"Degenerate coefficients while renormalizing\n");
|
|
|
|
continue;
|
|
|
|
}
|
2021-08-31 16:03:14 +02:00
|
|
|
for (j = 0; j < link->ch_layout.nb_channels; j++)
|
2012-02-20 23:25:12 +03:00
|
|
|
pan->gain[i][j] /= t;
|
2011-11-05 16:48:41 +03:00
|
|
|
}
|
2012-02-20 23:25:12 +03:00
|
|
|
swr_set_matrix(pan->swr, pan->gain[0], pan->gain[1] - pan->gain[0]);
|
2012-01-18 14:00:16 +03:00
|
|
|
}
|
2012-02-16 14:34:39 +03:00
|
|
|
|
|
|
|
r = swr_init(pan->swr);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2011-11-05 16:48:41 +03:00
|
|
|
// summary
|
|
|
|
for (i = 0; i < pan->nb_output_channels; i++) {
|
|
|
|
cur = buf;
|
2021-08-31 16:03:14 +02:00
|
|
|
for (j = 0; j < link->ch_layout.nb_channels; j++) {
|
2011-11-05 16:48:41 +03:00
|
|
|
r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
|
2012-02-20 23:25:12 +03:00
|
|
|
j ? " + " : "", pan->gain[i][j], j);
|
2011-11-05 16:48:41 +03:00
|
|
|
cur += FFMIN(buf + sizeof(buf) - cur, r);
|
|
|
|
}
|
2012-07-14 19:26:04 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "o%d = %s\n", i, buf);
|
2011-11-05 16:48:41 +03:00
|
|
|
}
|
2012-01-18 14:00:16 +03:00
|
|
|
// add channel mapping summary if possible
|
|
|
|
if (pan->pure_gains) {
|
|
|
|
av_log(ctx, AV_LOG_INFO, "Pure channel mapping detected:");
|
|
|
|
for (i = 0; i < pan->nb_output_channels; i++)
|
|
|
|
if (pan->channel_map[i] < 0)
|
|
|
|
av_log(ctx, AV_LOG_INFO, " M");
|
|
|
|
else
|
|
|
|
av_log(ctx, AV_LOG_INFO, " %d", pan->channel_map[i]);
|
|
|
|
av_log(ctx, AV_LOG_INFO, "\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2011-11-05 16:48:41 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
|
2012-01-18 14:00:16 +03:00
|
|
|
{
|
2012-07-09 23:10:38 +03:00
|
|
|
int ret;
|
2013-03-10 03:30:30 +03:00
|
|
|
int n = insamples->nb_samples;
|
2012-01-18 14:00:16 +03:00
|
|
|
AVFilterLink *const outlink = inlink->dst->outputs[0];
|
2013-03-10 03:30:30 +03:00
|
|
|
AVFrame *outsamples = ff_get_audio_buffer(outlink, n);
|
2012-01-18 14:00:16 +03:00
|
|
|
PanContext *pan = inlink->dst->priv;
|
|
|
|
|
2017-06-24 18:36:46 +02:00
|
|
|
if (!outsamples) {
|
|
|
|
av_frame_free(&insamples);
|
2013-04-08 21:48:16 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2017-06-24 18:36:46 +02:00
|
|
|
}
|
2014-02-11 17:15:58 +03:00
|
|
|
swr_convert(pan->swr, outsamples->extended_data, n,
|
|
|
|
(void *)insamples->extended_data, n);
|
2013-03-10 03:30:30 +03:00
|
|
|
av_frame_copy_props(outsamples, insamples);
|
2021-08-31 16:03:14 +02:00
|
|
|
#if FF_API_OLD_CHANNEL_LAYOUT
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2013-03-10 03:30:30 +03:00
|
|
|
outsamples->channel_layout = outlink->channel_layout;
|
2021-08-31 16:03:14 +02:00
|
|
|
outsamples->channels = outlink->ch_layout.nb_channels;
|
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
|
|
|
if ((ret = av_channel_layout_copy(&outsamples->ch_layout, &outlink->ch_layout)) < 0)
|
|
|
|
return ret;
|
2012-01-31 10:20:42 +03:00
|
|
|
|
2012-11-28 15:53:48 +03:00
|
|
|
ret = ff_filter_frame(outlink, outsamples);
|
2013-03-10 03:30:30 +03:00
|
|
|
av_frame_free(&insamples);
|
2012-07-09 23:10:38 +03:00
|
|
|
return ret;
|
2011-11-05 16:48:41 +03:00
|
|
|
}
|
|
|
|
|
2012-01-18 14:00:16 +03:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
PanContext *pan = ctx->priv;
|
|
|
|
swr_free(&pan->swr);
|
|
|
|
}
|
|
|
|
|
2013-04-12 11:32:41 +03:00
|
|
|
#define OFFSET(x) offsetof(PanContext, x)
|
|
|
|
|
|
|
|
static const AVOption pan_options[] = {
|
2020-03-17 23:46:36 +02:00
|
|
|
{ "args", NULL, OFFSET(args), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
|
2013-04-12 11:32:41 +03:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(pan);
|
|
|
|
|
2012-11-28 22:01:59 +03:00
|
|
|
static const AVFilterPad pan_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_props,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad pan_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_af_pan = {
|
2011-11-05 16:48:41 +03:00
|
|
|
.name = "pan",
|
2012-01-20 02:16:27 +03:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
|
2011-11-05 16:48:41 +03:00
|
|
|
.priv_size = sizeof(PanContext),
|
2013-04-12 11:32:41 +03:00
|
|
|
.priv_class = &pan_class,
|
2011-11-05 16:48:41 +03:00
|
|
|
.init = init,
|
2012-01-18 14:00:16 +03:00
|
|
|
.uninit = uninit,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(pan_inputs),
|
|
|
|
FILTER_OUTPUTS(pan_outputs),
|
2021-09-27 12:07:35 +02:00
|
|
|
FILTER_QUERY_FUNC(query_formats),
|
2011-11-05 16:48:41 +03:00
|
|
|
};
|