FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_pan.c
Date: 2026-09-25 02:02:46
Exec Total Coverage
Lines: 163 215 75.8%
Functions: 8 8 100.0%
Branches: 95 128 74.2%

Line Branch Exec Source
1 /*
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>
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 /**
24 * @file
25 * Audio panning filter (channels mixing)
26 * Original code written by Anders Johansson for MPlayer,
27 * reimplemented for FFmpeg.
28 */
29
30 #include <stdio.h>
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"
36 #include "audio.h"
37 #include "avfilter.h"
38 #include "filters.h"
39 #include "formats.h"
40
41 #define MAX_CHANNELS 64
42
43 typedef struct PanContext {
44 const AVClass *class;
45 char *args;
46 AVChannelLayout out_channel_layout;
47 double gain[MAX_CHANNELS][MAX_CHANNELS];
48 int64_t need_renorm;
49 int need_renumber;
50 int nb_output_channels;
51
52 int pure_gains;
53 /* channel mapping specific */
54 int channel_map[MAX_CHANNELS];
55 struct SwrContext *swr;
56 } PanContext;
57
58 703 static void skip_spaces(char **arg)
59 {
60 703 int len = 0;
61
62 703 sscanf(*arg, " %n", &len);
63 703 *arg += len;
64 703 }
65
66 352 static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
67 {
68 char buf[8];
69 352 int len, channel_id = 0;
70
71 352 skip_spaces(arg);
72 /* try to parse a channel name, e.g. "FL" */
73
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 262 times.
352 if (sscanf(*arg, "%7[A-Z]%n", buf, &len) >= 1) {
74 90 channel_id = av_channel_from_string(buf);
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 if (channel_id < 0)
76 ✗ return channel_id;
77
78 90 *rchannel = channel_id;
79 90 *rnamed = 1;
80 90 *arg += len;
81 90 return 0;
82 }
83 /* try to parse a channel number, e.g. "c2" */
84
1/2
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
262 if (sscanf(*arg, "c%d%n", &channel_id, &len) >= 1 &&
85
2/4
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 262 times.
✗ Branch 3 not taken.
262 channel_id >= 0 && channel_id < MAX_CHANNELS) {
86 262 *rchannel = channel_id;
87 262 *rnamed = 0;
88 262 *arg += len;
89 262 return 0;
90 }
91 ✗ return AVERROR(EINVAL);
92 }
93
94 54 static int are_gains_pure(const PanContext *pan)
95 {
96 int i, j;
97
98
2/2
✓ Branch 0 taken 2448 times.
✓ Branch 1 taken 38 times.
2486 for (i = 0; i < MAX_CHANNELS; i++) {
99 2448 int nb_gain = 0;
100
101
2/2
✓ Branch 0 taken 155680 times.
✓ Branch 1 taken 2432 times.
158112 for (j = 0; j < MAX_CHANNELS; j++) {
102 155680 double gain = pan->gain[i][j];
103
104 /* channel mapping is effective only if 0% or 100% of a channel is
105 * selected... */
106
4/4
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 155556 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 110 times.
155680 if (gain != 0. && gain != 1.)
107 14 return 0;
108 /* ...and if the output channel is only composed of one input */
109
4/4
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 155556 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 108 times.
155666 if (gain && nb_gain++)
110 2 return 0;
111 }
112 }
113 38 return 1;
114 }
115
116 55 static av_cold int init(AVFilterContext *ctx)
117 {
118 55 PanContext *const pan = ctx->priv;
119 55 char *arg, *arg0, *tokenizer, *args = av_strdup(pan->args);
120 55 int out_ch_id, in_ch_id, len, named, ret, sign = 1;
121 55 int nb_in_channels[2] = { 0, 0 }; // number of unnamed and named input channels
122 55 int used_out_ch[MAX_CHANNELS] = {0};
123 double gain;
124
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (!pan->args) {
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);
130 }
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (!args)
132 ✗ return AVERROR(ENOMEM);
133 55 arg = av_strtok(args, "|", &tokenizer);
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (!arg) {
135 ✗ av_log(ctx, AV_LOG_ERROR, "Channel layout not specified\n");
136 ✗ ret = AVERROR(EINVAL);
137 ✗ goto fail;
138 }
139 55 ret = ff_parse_channel_layout(&pan->out_channel_layout,
140 &pan->nb_output_channels, arg, ctx);
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (ret < 0)
142 ✗ goto fail;
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 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;
149 ✗ goto fail;
150 }
151
152 /* parse channel specifications */
153
2/2
✓ Branch 1 taken 151 times.
✓ Branch 2 taken 54 times.
205 while ((arg = arg0 = av_strtok(NULL, "|", &tokenizer))) {
154 151 int used_in_ch[MAX_CHANNELS] = {0};
155 /* channel name */
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 151 times.
151 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);
160 1 goto fail;
161 }
162
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 110 times.
151 if (named) {
163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
41 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);
167 ✗ goto fail;
168 }
169 }
170
2/4
✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 151 times.
151 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);
174 ✗ goto fail;
175 }
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 151 times.
151 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);
180 ✗ goto fail;
181 }
182 151 used_out_ch[out_ch_id] = 1;
183 151 skip_spaces(&arg);
184
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 8 times.
151 if (*arg == '=') {
185 143 arg++;
186
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 } else if (*arg == '<') {
187 8 pan->need_renorm |= (int64_t)1 << out_ch_id;
188 8 arg++;
189 } else {
190 ✗ av_log(ctx, AV_LOG_ERROR,
191 "Syntax error after channel name in \"%.8s\"\n", arg0);
192 ✗ ret = AVERROR(EINVAL);
193 ✗ goto fail;
194 }
195 /* gains */
196 151 sign = 1;
197 while (1) {
198 201 gain = 1;
199
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 165 times.
201 if (sscanf(arg, "%lf%n *%n", &gain, &len, &len) >= 1)
200 36 arg += len;
201
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 201 times.
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);
205 ✗ goto fail;
206 }
207 201 nb_in_channels[named]++;
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
201 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);
212 ✗ goto fail;
213 }
214
3/4
✓ Branch 0 taken 201 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 200 times.
201 if (in_ch_id < 0 || in_ch_id >= MAX_CHANNELS) {
215 1 av_log(ctx, AV_LOG_ERROR,
216 "Input channel id %d is not supported\n", in_ch_id);
217 1 ret = AVERROR_PATCHWELCOME;
218 1 goto fail;
219 }
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (used_in_ch[in_ch_id]) {
221 ✗ av_log(ctx, AV_LOG_ERROR,
222 "Can not reference in channel %d twice\n", in_ch_id);
223 ✗ ret = AVERROR(EINVAL);
224 ✗ goto fail;
225 }
226 200 used_in_ch[in_ch_id] = 1;
227 200 pan->gain[out_ch_id][in_ch_id] = sign * gain;
228 200 skip_spaces(&arg);
229
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 50 times.
200 if (!*arg)
230 150 break;
231
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 32 times.
50 if (*arg == '-') {
232 18 sign = -1;
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 } else if (*arg != '+') {
234 ✗ av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
235 ✗ ret = AVERROR(EINVAL);
236 ✗ goto fail;
237 } else {
238 32 sign = 1;
239 }
240 50 arg++;
241 }
242 }
243 54 pan->need_renumber = !!nb_in_channels[1];
244 54 pan->pure_gains = are_gains_pure(pan);
245
246 54 ret = 0;
247 55 fail:
248 55 av_free(args);
249 55 return ret;
250 }
251
252 27 static int query_formats(const AVFilterContext *ctx,
253 AVFilterFormatsConfig **cfg_in,
254 AVFilterFormatsConfig **cfg_out)
255 {
256 27 const PanContext *pan = ctx->priv;
257 AVFilterChannelLayouts *layouts;
258 int ret;
259
260 // inlink supports any channel layout
261 27 layouts = ff_all_channel_counts();
262
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if ((ret = ff_channel_layouts_ref(layouts, &cfg_in[0]->channel_layouts)) < 0)
263 ✗ return ret;
264
265 // outlink supports only requested output channel layout
266 27 layouts = NULL;
267
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if ((ret = ff_add_channel_layout(&layouts, &pan->out_channel_layout)) < 0)
268 ✗ return ret;
269 27 return ff_channel_layouts_ref(layouts, &cfg_out[0]->channel_layouts);
270 }
271
272 27 static int config_props(AVFilterLink *link)
273 {
274 27 AVFilterContext *ctx = link->dst;
275 27 PanContext *pan = ctx->priv;
276 char buf[1024], *cur;
277 int i, j, k, r, ret;
278 double t;
279
280
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 22 times.
27 if (pan->need_renumber) {
281 // input channels were given by their name: renumber them
282
2/2
✓ Branch 0 taken 320 times.
✓ Branch 1 taken 5 times.
325 for (i = j = 0; i < MAX_CHANNELS; i++) {
283
2/2
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 297 times.
320 if (av_channel_layout_index_from_channel(&link->ch_layout, i) >= 0) {
284
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 23 times.
131 for (k = 0; k < pan->nb_output_channels; k++)
285 108 pan->gain[k][j] = pan->gain[k][i];
286 23 j++;
287 }
288 }
289 }
290
291 // sanity check; can't be done in query_formats since the inlink
292 // channel layout is unknown at that time
293
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (link->ch_layout.nb_channels > MAX_CHANNELS ||
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 pan->nb_output_channels > MAX_CHANNELS) {
295 ✗ av_log(ctx, AV_LOG_ERROR,
296 "af_pan supports a maximum of %d channels. "
297 "Feel free to ask for a higher limit.\n", MAX_CHANNELS);
298 ✗ return AVERROR_PATCHWELCOME;
299 }
300
301 // init libswresample context
302 27 ret = swr_alloc_set_opts2(&pan->swr,
303 27 &pan->out_channel_layout, link->format, link->sample_rate,
304 27 &link->ch_layout, link->format, link->sample_rate,
305 0, ctx);
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
307 ✗ return AVERROR(ENOMEM);
308
309 // gains are pure, init the channel mapping
310
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 8 times.
27 if (pan->pure_gains) {
311
312 // get channel map from the pure gains
313
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 19 times.
67 for (i = 0; i < pan->nb_output_channels; i++) {
314 48 int ch_id = -1;
315
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 7 times.
97 for (j = 0; j < link->ch_layout.nb_channels; j++) {
316
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 49 times.
90 if (pan->gain[i][j]) {
317 41 ch_id = j;
318 41 break;
319 }
320 }
321 48 pan->channel_map[i] = ch_id;
322 }
323
324 19 av_opt_set_chlayout(pan->swr, "uchl", &pan->out_channel_layout, 0);
325 19 swr_set_channel_mapping(pan->swr, pan->channel_map);
326 } else {
327 // renormalize
328
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 8 times.
35 for (i = 0; i < pan->nb_output_channels; i++) {
329
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 4 times.
27 if (!((pan->need_renorm >> i) & 1))
330 23 continue;
331 4 t = 0;
332
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (j = 0; j < link->ch_layout.nb_channels; j++)
333 12 t += fabs(pan->gain[i][j]);
334
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (t > -1E-5 && t < 1E-5) {
335 // t is almost 0 but not exactly, this is probably a mistake
336 ✗ if (t)
337 ✗ av_log(ctx, AV_LOG_WARNING,
338 "Degenerate coefficients while renormalizing\n");
339 ✗ continue;
340 }
341
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (j = 0; j < link->ch_layout.nb_channels; j++)
342 12 pan->gain[i][j] /= t;
343 }
344 8 swr_set_matrix(pan->swr, pan->gain[0], pan->gain[1] - pan->gain[0]);
345 }
346
347 27 r = swr_init(pan->swr);
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (r < 0)
349 ✗ return r;
350
351 // summary
352
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 27 times.
102 for (i = 0; i < pan->nb_output_channels; i++) {
353 75 cur = buf;
354
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 75 times.
331 for (j = 0; j < link->ch_layout.nb_channels; j++) {
355
2/2
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 75 times.
256 r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
356 j ? " + " : "", pan->gain[i][j], j);
357
1/2
✓ Branch 0 taken 256 times.
✗ Branch 1 not taken.
256 cur += FFMIN(buf + sizeof(buf) - cur, r);
358 }
359 75 av_log(ctx, AV_LOG_VERBOSE, "o%d = %s\n", i, buf);
360 }
361 // add channel mapping summary if possible
362
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 8 times.
27 if (pan->pure_gains) {
363 19 av_log(ctx, AV_LOG_INFO, "Pure channel mapping detected:");
364
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 19 times.
67 for (i = 0; i < pan->nb_output_channels; i++)
365
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 41 times.
48 if (pan->channel_map[i] < 0)
366 7 av_log(ctx, AV_LOG_INFO, " M");
367 else
368 41 av_log(ctx, AV_LOG_INFO, " %d", pan->channel_map[i]);
369 19 av_log(ctx, AV_LOG_INFO, "\n");
370 19 return 0;
371 }
372 8 return 0;
373 }
374
375 1380 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
376 {
377 int ret;
378 1380 int n = insamples->nb_samples;
379 1380 AVFilterLink *const outlink = inlink->dst->outputs[0];
380 1380 AVFrame *outsamples = ff_get_audio_buffer(outlink, n);
381 1380 PanContext *pan = inlink->dst->priv;
382
383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1380 times.
1380 if (!outsamples) {
384 ✗ av_frame_free(&insamples);
385 ✗ return AVERROR(ENOMEM);
386 }
387 1380 swr_convert(pan->swr, outsamples->extended_data, n,
388 1380 (void *)insamples->extended_data, n);
389 1380 av_frame_copy_props(outsamples, insamples);
390
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1380 times.
1380 if ((ret = av_channel_layout_copy(&outsamples->ch_layout, &outlink->ch_layout)) < 0) {
391 ✗ av_frame_free(&outsamples);
392 ✗ av_frame_free(&insamples);
393 ✗ return ret;
394 }
395
396 1380 av_frame_free(&insamples);
397 1380 return ff_filter_frame(outlink, outsamples);
398 }
399
400 55 static av_cold void uninit(AVFilterContext *ctx)
401 {
402 55 PanContext *pan = ctx->priv;
403 55 swr_free(&pan->swr);
404 55 av_channel_layout_uninit(&pan->out_channel_layout);
405 55 }
406
407 #define OFFSET(x) offsetof(PanContext, x)
408
409 static const AVOption pan_options[] = {
410 { "args", NULL, OFFSET(args), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
411 { NULL }
412 };
413
414 AVFILTER_DEFINE_CLASS(pan);
415
416 static const AVFilterPad pan_inputs[] = {
417 {
418 .name = "default",
419 .type = AVMEDIA_TYPE_AUDIO,
420 .config_props = config_props,
421 .filter_frame = filter_frame,
422 },
423 };
424
425 const FFFilter ff_af_pan = {
426 .p.name = "pan",
427 .p.description = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
428 .p.priv_class = &pan_class,
429 .priv_size = sizeof(PanContext),
430 .init = init,
431 .uninit = uninit,
432 FILTER_INPUTS(pan_inputs),
433 FILTER_OUTPUTS(ff_audio_default_filterpad),
434 FILTER_QUERY_FUNC2(query_formats),
435 };
436