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/mem.h"
34 #include "libavutil/opt.h"
35 #include "libswresample/swresample.h"
41 #define MAX_CHANNELS 64
43 typedef struct PanContext
{
46 AVChannelLayout out_channel_layout
;
47 double gain
[MAX_CHANNELS
][MAX_CHANNELS
];
50 int nb_output_channels
;
53 /* channel mapping specific */
54 int channel_map
[MAX_CHANNELS
];
55 struct SwrContext
*swr
;
58 static void skip_spaces(char **arg
)
62 sscanf(*arg
, " %n", &len
);
66 static int parse_channel_name(char **arg
, int *rchannel
, int *rnamed
)
69 int len
, channel_id
= 0;
72 /* try to parse a channel name, e.g. "FL" */
73 if (sscanf(*arg
, "%7[A-Z]%n", buf
, &len
)) {
74 channel_id
= av_channel_from_string(buf
);
78 *rchannel
= channel_id
;
83 /* try to parse a channel number, e.g. "c2" */
84 if (sscanf(*arg
, "c%d%n", &channel_id
, &len
) &&
85 channel_id
>= 0 && channel_id
< MAX_CHANNELS
) {
86 *rchannel
= channel_id
;
91 return AVERROR(EINVAL
);
94 static int are_gains_pure(const PanContext
*pan
)
98 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
101 for (j
= 0; j
< MAX_CHANNELS
; j
++) {
102 double gain
= pan
->gain
[i
][j
];
104 /* channel mapping is effective only if 0% or 100% of a channel is
106 if (gain
!= 0. && gain
!= 1.)
108 /* ...and if the output channel is only composed of one input */
109 if (gain
&& nb_gain
++)
116 static av_cold
int init(AVFilterContext
*ctx
)
118 PanContext
*const pan
= ctx
->priv
;
119 char *arg
, *arg0
, *tokenizer
, *args
= av_strdup(pan
->args
);
120 int out_ch_id
, in_ch_id
, len
, named
, ret
, sign
= 1;
121 int nb_in_channels
[2] = { 0, 0 }; // number of unnamed and named input channels
122 int used_out_ch
[MAX_CHANNELS
] = {0};
126 av_log(ctx
, AV_LOG_ERROR
,
127 "pan filter needs a channel layout and a set "
128 "of channel definitions as parameter\n");
129 return AVERROR(EINVAL
);
132 return AVERROR(ENOMEM
);
133 arg
= av_strtok(args
, "|", &tokenizer
);
135 av_log(ctx
, AV_LOG_ERROR
, "Channel layout not specified\n");
136 ret
= AVERROR(EINVAL
);
139 ret
= ff_parse_channel_layout(&pan
->out_channel_layout
,
140 &pan
->nb_output_channels
, arg
, ctx
);
144 if (pan
->nb_output_channels
> MAX_CHANNELS
) {
145 av_log(ctx
, AV_LOG_ERROR
,
146 "af_pan supports a maximum of %d channels. "
147 "Feel free to ask for a higher limit.\n", MAX_CHANNELS
);
148 ret
= AVERROR_PATCHWELCOME
;
152 /* parse channel specifications */
153 while ((arg
= arg0
= av_strtok(NULL
, "|", &tokenizer
))) {
154 int used_in_ch
[MAX_CHANNELS
] = {0};
156 if (parse_channel_name(&arg
, &out_ch_id
, &named
)) {
157 av_log(ctx
, AV_LOG_ERROR
,
158 "Expected out channel name, got \"%.8s\"\n", arg
);
159 ret
= AVERROR(EINVAL
);
163 if ((out_ch_id
= av_channel_layout_index_from_channel(&pan
->out_channel_layout
, out_ch_id
)) < 0) {
164 av_log(ctx
, AV_LOG_ERROR
,
165 "Channel \"%.8s\" does not exist in the chosen layout\n", arg0
);
166 ret
= AVERROR(EINVAL
);
170 if (out_ch_id
< 0 || out_ch_id
>= pan
->nb_output_channels
) {
171 av_log(ctx
, AV_LOG_ERROR
,
172 "Invalid out channel name \"%.8s\"\n", arg0
);
173 ret
= AVERROR(EINVAL
);
176 if (used_out_ch
[out_ch_id
]) {
177 av_log(ctx
, AV_LOG_ERROR
,
178 "Can not reference out channel %d twice\n", out_ch_id
);
179 ret
= AVERROR(EINVAL
);
182 used_out_ch
[out_ch_id
] = 1;
186 } else if (*arg
== '<') {
187 pan
->need_renorm
|= (int64_t)1 << out_ch_id
;
190 av_log(ctx
, AV_LOG_ERROR
,
191 "Syntax error after channel name in \"%.8s\"\n", arg0
);
192 ret
= AVERROR(EINVAL
);
199 if (sscanf(arg
, "%lf%n *%n", &gain
, &len
, &len
) >= 1)
201 if (parse_channel_name(&arg
, &in_ch_id
, &named
)){
202 av_log(ctx
, AV_LOG_ERROR
,
203 "Expected in channel name, got \"%.8s\"\n", arg
);
204 ret
= AVERROR(EINVAL
);
207 nb_in_channels
[named
]++;
208 if (nb_in_channels
[!named
]) {
209 av_log(ctx
, AV_LOG_ERROR
,
210 "Can not mix named and numbered channels\n");
211 ret
= AVERROR(EINVAL
);
214 if (used_in_ch
[in_ch_id
]) {
215 av_log(ctx
, AV_LOG_ERROR
,
216 "Can not reference in channel %d twice\n", in_ch_id
);
217 ret
= AVERROR(EINVAL
);
220 used_in_ch
[in_ch_id
] = 1;
221 pan
->gain
[out_ch_id
][in_ch_id
] = sign
* gain
;
227 } else if (*arg
!= '+') {
228 av_log(ctx
, AV_LOG_ERROR
, "Syntax error near \"%.8s\"\n", arg
);
229 ret
= AVERROR(EINVAL
);
237 pan
->need_renumber
= !!nb_in_channels
[1];
238 pan
->pure_gains
= are_gains_pure(pan
);
246 static int query_formats(const AVFilterContext
*ctx
,
247 AVFilterFormatsConfig
**cfg_in
,
248 AVFilterFormatsConfig
**cfg_out
)
250 const PanContext
*pan
= ctx
->priv
;
251 AVFilterChannelLayouts
*layouts
;
254 // inlink supports any channel layout
255 layouts
= ff_all_channel_counts();
256 if ((ret
= ff_channel_layouts_ref(layouts
, &cfg_in
[0]->channel_layouts
)) < 0)
259 // outlink supports only requested output channel layout
261 if ((ret
= ff_add_channel_layout(&layouts
, &pan
->out_channel_layout
)) < 0)
263 return ff_channel_layouts_ref(layouts
, &cfg_out
[0]->channel_layouts
);
266 static int config_props(AVFilterLink
*link
)
268 AVFilterContext
*ctx
= link
->dst
;
269 PanContext
*pan
= ctx
->priv
;
270 char buf
[1024], *cur
;
274 if (pan
->need_renumber
) {
275 // input channels were given by their name: renumber them
276 for (i
= j
= 0; i
< MAX_CHANNELS
; i
++) {
277 if (av_channel_layout_index_from_channel(&link
->ch_layout
, i
) >= 0) {
278 for (k
= 0; k
< pan
->nb_output_channels
; k
++)
279 pan
->gain
[k
][j
] = pan
->gain
[k
][i
];
285 // sanity check; can't be done in query_formats since the inlink
286 // channel layout is unknown at that time
287 if (link
->ch_layout
.nb_channels
> MAX_CHANNELS
||
288 pan
->nb_output_channels
> MAX_CHANNELS
) {
289 av_log(ctx
, AV_LOG_ERROR
,
290 "af_pan supports a maximum of %d channels. "
291 "Feel free to ask for a higher limit.\n", MAX_CHANNELS
);
292 return AVERROR_PATCHWELCOME
;
295 // init libswresample context
296 ret
= swr_alloc_set_opts2(&pan
->swr
,
297 &pan
->out_channel_layout
, link
->format
, link
->sample_rate
,
298 &link
->ch_layout
, link
->format
, link
->sample_rate
,
301 return AVERROR(ENOMEM
);
303 // gains are pure, init the channel mapping
304 if (pan
->pure_gains
) {
306 // get channel map from the pure gains
307 for (i
= 0; i
< pan
->nb_output_channels
; i
++) {
309 for (j
= 0; j
< link
->ch_layout
.nb_channels
; j
++) {
310 if (pan
->gain
[i
][j
]) {
315 pan
->channel_map
[i
] = ch_id
;
318 av_opt_set_chlayout(pan
->swr
, "uchl", &pan
->out_channel_layout
, 0);
319 swr_set_channel_mapping(pan
->swr
, pan
->channel_map
);
322 for (i
= 0; i
< pan
->nb_output_channels
; i
++) {
323 if (!((pan
->need_renorm
>> i
) & 1))
326 for (j
= 0; j
< link
->ch_layout
.nb_channels
; j
++)
327 t
+= fabs(pan
->gain
[i
][j
]);
328 if (t
> -1E-5 && t
< 1E-5) {
329 // t is almost 0 but not exactly, this is probably a mistake
331 av_log(ctx
, AV_LOG_WARNING
,
332 "Degenerate coefficients while renormalizing\n");
335 for (j
= 0; j
< link
->ch_layout
.nb_channels
; j
++)
336 pan
->gain
[i
][j
] /= t
;
338 swr_set_matrix(pan
->swr
, pan
->gain
[0], pan
->gain
[1] - pan
->gain
[0]);
341 r
= swr_init(pan
->swr
);
346 for (i
= 0; i
< pan
->nb_output_channels
; i
++) {
348 for (j
= 0; j
< link
->ch_layout
.nb_channels
; j
++) {
349 r
= snprintf(cur
, buf
+ sizeof(buf
) - cur
, "%s%.3g i%d",
350 j
? " + " : "", pan
->gain
[i
][j
], j
);
351 cur
+= FFMIN(buf
+ sizeof(buf
) - cur
, r
);
353 av_log(ctx
, AV_LOG_VERBOSE
, "o%d = %s\n", i
, buf
);
355 // add channel mapping summary if possible
356 if (pan
->pure_gains
) {
357 av_log(ctx
, AV_LOG_INFO
, "Pure channel mapping detected:");
358 for (i
= 0; i
< pan
->nb_output_channels
; i
++)
359 if (pan
->channel_map
[i
] < 0)
360 av_log(ctx
, AV_LOG_INFO
, " M");
362 av_log(ctx
, AV_LOG_INFO
, " %d", pan
->channel_map
[i
]);
363 av_log(ctx
, AV_LOG_INFO
, "\n");
369 static int filter_frame(AVFilterLink
*inlink
, AVFrame
*insamples
)
372 int n
= insamples
->nb_samples
;
373 AVFilterLink
*const outlink
= inlink
->dst
->outputs
[0];
374 AVFrame
*outsamples
= ff_get_audio_buffer(outlink
, n
);
375 PanContext
*pan
= inlink
->dst
->priv
;
378 av_frame_free(&insamples
);
379 return AVERROR(ENOMEM
);
381 swr_convert(pan
->swr
, outsamples
->extended_data
, n
,
382 (void *)insamples
->extended_data
, n
);
383 av_frame_copy_props(outsamples
, insamples
);
384 if ((ret
= av_channel_layout_copy(&outsamples
->ch_layout
, &outlink
->ch_layout
)) < 0) {
385 av_frame_free(&outsamples
);
386 av_frame_free(&insamples
);
390 av_frame_free(&insamples
);
391 return ff_filter_frame(outlink
, outsamples
);
394 static av_cold
void uninit(AVFilterContext
*ctx
)
396 PanContext
*pan
= ctx
->priv
;
398 av_channel_layout_uninit(&pan
->out_channel_layout
);
401 #define OFFSET(x) offsetof(PanContext, x)
403 static const AVOption pan_options
[] = {
404 { "args", NULL
, OFFSET(args
), AV_OPT_TYPE_STRING
, { .str
= NULL
}, 0, 0, AV_OPT_FLAG_AUDIO_PARAM
| AV_OPT_FLAG_FILTERING_PARAM
},
408 AVFILTER_DEFINE_CLASS(pan
);
410 static const AVFilterPad pan_inputs
[] = {
413 .type
= AVMEDIA_TYPE_AUDIO
,
414 .config_props
= config_props
,
415 .filter_frame
= filter_frame
,
419 const FFFilter ff_af_pan
= {
421 .p
.description
= NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
422 .p
.priv_class
= &pan_class
,
423 .priv_size
= sizeof(PanContext
),
426 FILTER_INPUTS(pan_inputs
),
427 FILTER_OUTPUTS(ff_audio_default_filterpad
),
428 FILTER_QUERY_FUNC2(query_formats
),