FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_channelmap.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 111 204 54.4%
Functions: 7 7 100.0%
Branches: 57 137 41.6%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012 Google, Inc.
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * audio channel mapping filter
24 */
25
26 #include <ctype.h>
27
28 #include "libavutil/avstring.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/common.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/samplefmt.h"
34
35 #include "audio.h"
36 #include "avfilter.h"
37 #include "formats.h"
38 #include "internal.h"
39
40 struct ChannelMap {
41 int in_channel;
42 int out_channel;
43 int in_channel_idx;
44 int out_channel_idx;
45 };
46
47 enum MappingMode {
48 MAP_NONE,
49 MAP_ONE_INT,
50 MAP_ONE_STR,
51 MAP_PAIR_INT_INT,
52 MAP_PAIR_INT_STR,
53 MAP_PAIR_STR_INT,
54 MAP_PAIR_STR_STR
55 };
56
57 #define MAX_CH 64
58 typedef struct ChannelMapContext {
59 const AVClass *class;
60 char *mapping_str;
61 char *channel_layout_str;
62 AVChannelLayout output_layout;
63 struct ChannelMap map[MAX_CH];
64 int nch;
65 enum MappingMode mode;
66 } ChannelMapContext;
67
68 #define OFFSET(x) offsetof(ChannelMapContext, x)
69 #define A AV_OPT_FLAG_AUDIO_PARAM
70 #define F AV_OPT_FLAG_FILTERING_PARAM
71 static const AVOption channelmap_options[] = {
72 { "map", "A comma-separated list of input channel numbers in output order.",
73 OFFSET(mapping_str), AV_OPT_TYPE_STRING, .flags = A|F },
74 { "channel_layout", "Output channel layout.",
75 OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A|F },
76 { NULL }
77 };
78
79 AVFILTER_DEFINE_CLASS(channelmap);
80
81 16 static char* split(char *message, char delim) {
82 16 char *next = strchr(message, delim);
83
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 if (next)
84 12 *next++ = '\0';
85 16 return next;
86 }
87
88 12 static int get_channel_idx(char **map, int *ch, char delim, int max_ch)
89 {
90 char *next;
91 int len;
92 12 int n = 0;
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!*map)
94 return AVERROR(EINVAL);
95 12 next = split(*map, delim);
96
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
12 if (!next && delim == '-')
97 return AVERROR(EINVAL);
98 12 len = strlen(*map);
99 12 sscanf(*map, "%d%n", ch, &n);
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (n != len)
101 return AVERROR(EINVAL);
102
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
12 if (*ch < 0 || *ch > max_ch)
103 return AVERROR(EINVAL);
104 12 *map = next;
105 12 return 0;
106 }
107
108 4 static int get_channel(char **map, int *ch, char delim)
109 {
110 4 char *next = split(*map, delim);
111
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
4 if (!next && delim == '-')
112 return AVERROR(EINVAL);
113 4 *ch = av_channel_from_string(*map);
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (*ch < 0)
115 return AVERROR(EINVAL);
116 4 *map = next;
117 4 return 0;
118 }
119
120 6 static av_cold int channelmap_init(AVFilterContext *ctx)
121 {
122 6 ChannelMapContext *s = ctx->priv;
123 6 char *mapping, separator = '|';
124 6 int map_entries = 0;
125 char buf[256];
126 enum MappingMode mode;
127 6 uint64_t out_ch_mask = 0;
128 int i;
129
130 6 mapping = s->mapping_str;
131
132
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (!mapping) {
133 2 mode = MAP_NONE;
134 } else {
135 4 char *dash = strchr(mapping, '-');
136
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!dash) { // short mapping
137
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (av_isdigit(*mapping))
138 2 mode = MAP_ONE_INT;
139 else
140 2 mode = MAP_ONE_STR;
141 } else if (av_isdigit(*mapping)) {
142 if (av_isdigit(*(dash+1)))
143 mode = MAP_PAIR_INT_INT;
144 else
145 mode = MAP_PAIR_INT_STR;
146 } else {
147 if (av_isdigit(*(dash+1)))
148 mode = MAP_PAIR_STR_INT;
149 else
150 mode = MAP_PAIR_STR_STR;
151 }
152 }
153
154
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (mode != MAP_NONE) {
155 4 char *sep = mapping;
156 4 map_entries = 1;
157
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 while ((sep = strchr(sep, separator))) {
158
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (*++sep) // Allow trailing comma
159 12 map_entries++;
160 }
161 }
162
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (map_entries > MAX_CH) {
164 av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
165 return AVERROR(EINVAL);
166 }
167
168
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6 times.
22 for (i = 0; i < map_entries; i++) {
169 16 int in_ch_idx = -1, out_ch_idx = -1;
170 16 int in_ch = 0, out_ch = 0;
171 static const char err[] = "Failed to parse channel map\n";
172
2/7
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
16 switch (mode) {
173 12 case MAP_ONE_INT:
174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (get_channel_idx(&mapping, &in_ch_idx, separator, MAX_CH) < 0) {
175 av_log(ctx, AV_LOG_ERROR, err);
176 return AVERROR(EINVAL);
177 }
178 12 s->map[i].in_channel_idx = in_ch_idx;
179 12 s->map[i].out_channel_idx = i;
180 12 break;
181 4 case MAP_ONE_STR:
182
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (get_channel(&mapping, &in_ch, separator) < 0) {
183 av_log(ctx, AV_LOG_ERROR, err);
184 return AVERROR(EINVAL);
185 }
186 4 s->map[i].in_channel = in_ch;
187 4 s->map[i].out_channel_idx = i;
188 4 break;
189 case MAP_PAIR_INT_INT:
190 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
191 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
192 av_log(ctx, AV_LOG_ERROR, err);
193 return AVERROR(EINVAL);
194 }
195 s->map[i].in_channel_idx = in_ch_idx;
196 s->map[i].out_channel_idx = out_ch_idx;
197 break;
198 case MAP_PAIR_INT_STR:
199 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
200 get_channel(&mapping, &out_ch, separator) < 0 ||
201 (1ULL << out_ch) & out_ch_mask) {
202 av_log(ctx, AV_LOG_ERROR, err);
203 return AVERROR(EINVAL);
204 }
205 s->map[i].in_channel_idx = in_ch_idx;
206 s->map[i].out_channel = out_ch;
207 out_ch_mask |= 1ULL << out_ch;
208 break;
209 case MAP_PAIR_STR_INT:
210 if (get_channel(&mapping, &in_ch, '-') < 0 ||
211 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
212 av_log(ctx, AV_LOG_ERROR, err);
213 return AVERROR(EINVAL);
214 }
215 s->map[i].in_channel = in_ch;
216 s->map[i].out_channel_idx = out_ch_idx;
217 break;
218 case MAP_PAIR_STR_STR:
219 if (get_channel(&mapping, &in_ch, '-') < 0 ||
220 get_channel(&mapping, &out_ch, separator) < 0 ||
221 (1ULL << out_ch) & out_ch_mask) {
222 av_log(ctx, AV_LOG_ERROR, err);
223 return AVERROR(EINVAL);
224 }
225 s->map[i].in_channel = in_ch;
226 s->map[i].out_channel = out_ch;
227 out_ch_mask |= 1ULL << out_ch;
228 break;
229 }
230 }
231 6 s->mode = mode;
232 6 s->nch = map_entries;
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (out_ch_mask)
234 av_channel_layout_from_mask(&s->output_layout, out_ch_mask);
235 else
236 6 av_channel_layout_default(&s->output_layout, map_entries);
237
238
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (s->channel_layout_str) {
239 6 AVChannelLayout fmt = { 0 };
240 int ret;
241
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if ((ret = av_channel_layout_from_string(&fmt, s->channel_layout_str)) < 0) {
242 #if FF_API_OLD_CHANNEL_LAYOUT
243 uint64_t mask;
244 FF_DISABLE_DEPRECATION_WARNINGS
245 if ((mask = av_get_channel_layout(s->channel_layout_str)) == 0) {
246 #endif
247 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
248 s->channel_layout_str);
249 return AVERROR(EINVAL);
250 #if FF_API_OLD_CHANNEL_LAYOUT
251 }
252 FF_ENABLE_DEPRECATION_WARNINGS
253 av_log(ctx, AV_LOG_WARNING, "Channel layout '%s' uses a deprecated syntax.\n",
254 s->channel_layout_str);
255 av_channel_layout_from_mask(&fmt, mask);
256 #endif
257 }
258
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (mode == MAP_NONE) {
259 int i;
260 2 s->nch = fmt.nb_channels;
261
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i = 0; i < s->nch; i++) {
262 8 s->map[i].in_channel_idx = i;
263 8 s->map[i].out_channel_idx = i;
264 }
265
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
4 } else if (out_ch_mask && av_channel_layout_compare(&s->output_layout, &fmt)) {
266 av_channel_layout_describe(&s->output_layout, buf, sizeof(buf));
267 av_log(ctx, AV_LOG_ERROR,
268 "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
269 s->channel_layout_str, buf);
270 return AVERROR(EINVAL);
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 } else if (s->nch != fmt.nb_channels) {
272 av_log(ctx, AV_LOG_ERROR,
273 "Output channel layout %s does not match the number of channels mapped %d.\n",
274 s->channel_layout_str, s->nch);
275 return AVERROR(EINVAL);
276 }
277 6 s->output_layout = fmt;
278 }
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!s->output_layout.nb_channels) {
280 av_log(ctx, AV_LOG_ERROR, "Output channel layout is not set and "
281 "cannot be guessed from the maps.\n");
282 return AVERROR(EINVAL);
283 }
284
285
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
286 for (i = 0; i < s->nch; i++) {
287 s->map[i].out_channel_idx = av_channel_layout_index_from_channel(
288 &s->output_layout, s->map[i].out_channel);
289 }
290 }
291
292 6 return 0;
293 }
294
295 4 static int channelmap_query_formats(AVFilterContext *ctx)
296 {
297 4 ChannelMapContext *s = ctx->priv;
298 4 AVFilterChannelLayouts *channel_layouts = NULL;
299 int ret;
300
301
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
8 if ((ret = ff_set_common_formats (ctx, ff_planar_sample_fmts())) < 0 ||
302
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 (ret = ff_set_common_all_samplerates(ctx )) < 0 ||
303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
8 (ret = ff_add_channel_layout(&channel_layouts, &s->output_layout)) < 0 ||
304 4 (ret = ff_channel_layouts_ref(channel_layouts,
305 4 &ctx->outputs[0]->incfg.channel_layouts)) < 0)
306 return ret;
307
308 4 return ff_channel_layouts_ref(ff_all_channel_counts(),
309 4 &ctx->inputs[0]->outcfg.channel_layouts);
310 }
311
312 1304 static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf)
313 {
314 1304 AVFilterContext *ctx = inlink->dst;
315 1304 AVFilterLink *outlink = ctx->outputs[0];
316 1304 const ChannelMapContext *s = ctx->priv;
317 1304 const int nch_in = inlink->ch_layout.nb_channels;
318 1304 const int nch_out = s->nch;
319 int ch, ret;
320 uint8_t *source_planes[MAX_CH];
321
322 1304 memcpy(source_planes, buf->extended_data,
323 nch_in * sizeof(source_planes[0]));
324
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1304 times.
1304 if (nch_out > nch_in) {
326 if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
327 uint8_t **new_extended_data =
328 av_calloc(nch_out, sizeof(*buf->extended_data));
329 if (!new_extended_data) {
330 av_frame_free(&buf);
331 return AVERROR(ENOMEM);
332 }
333 if (buf->extended_data == buf->data) {
334 buf->extended_data = new_extended_data;
335 } else {
336 av_free(buf->extended_data);
337 buf->extended_data = new_extended_data;
338 }
339 } else if (buf->extended_data != buf->data) {
340 av_free(buf->extended_data);
341 buf->extended_data = buf->data;
342 }
343 }
344
345
2/2
✓ Branch 0 taken 6276 times.
✓ Branch 1 taken 1304 times.
7580 for (ch = 0; ch < nch_out; ch++) {
346 6276 buf->extended_data[s->map[ch].out_channel_idx] =
347 6276 source_planes[s->map[ch].in_channel_idx];
348 }
349
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1304 times.
1304 if (buf->data != buf->extended_data)
351 memcpy(buf->data, buf->extended_data,
352 FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
353
354 #if FF_API_OLD_CHANNEL_LAYOUT
355 FF_DISABLE_DEPRECATION_WARNINGS
356 1304 buf->channels = outlink->ch_layout.nb_channels;
357 1304 buf->channel_layout = outlink->channel_layout;
358 FF_ENABLE_DEPRECATION_WARNINGS
359 #endif
360
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1304 times.
1304 if ((ret = av_channel_layout_copy(&buf->ch_layout, &outlink->ch_layout)) < 0)
361 return ret;
362
363 1304 return ff_filter_frame(outlink, buf);
364 }
365
366 4 static int channelmap_config_input(AVFilterLink *inlink)
367 {
368 4 AVFilterContext *ctx = inlink->dst;
369 4 ChannelMapContext *s = ctx->priv;
370 4 int nb_channels = inlink->ch_layout.nb_channels;
371 4 int i, err = 0;
372 char channel_name[64];
373 char layout_name[256];
374
375
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (i = 0; i < s->nch; i++) {
376 16 struct ChannelMap *m = &s->map[i];
377
378
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
379 m->in_channel_idx = av_channel_layout_index_from_channel(
380 &inlink->ch_layout, m->in_channel);
381 }
382
383
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 if (m->in_channel_idx < 0 || m->in_channel_idx >= nb_channels) {
384 av_channel_layout_describe(&inlink->ch_layout, layout_name, sizeof(layout_name));
385 if (m->in_channel) {
386 av_channel_name(channel_name, sizeof(channel_name), m->in_channel);
387 av_log(ctx, AV_LOG_ERROR,
388 "input channel '%s' not available from input layout '%s'\n",
389 channel_name, layout_name);
390 } else {
391 av_log(ctx, AV_LOG_ERROR,
392 "input channel #%d not available from input layout '%s'\n",
393 m->in_channel_idx, layout_name);
394 }
395 err = AVERROR(EINVAL);
396 }
397 }
398
399 4 return err;
400 }
401
402 static const AVFilterPad avfilter_af_channelmap_inputs[] = {
403 {
404 .name = "default",
405 .type = AVMEDIA_TYPE_AUDIO,
406 .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
407 .filter_frame = channelmap_filter_frame,
408 .config_props = channelmap_config_input,
409 },
410 };
411
412 static const AVFilterPad avfilter_af_channelmap_outputs[] = {
413 {
414 .name = "default",
415 .type = AVMEDIA_TYPE_AUDIO
416 },
417 };
418
419 const AVFilter ff_af_channelmap = {
420 .name = "channelmap",
421 .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
422 .init = channelmap_init,
423 .priv_size = sizeof(ChannelMapContext),
424 .priv_class = &channelmap_class,
425 FILTER_INPUTS(avfilter_af_channelmap_inputs),
426 FILTER_OUTPUTS(avfilter_af_channelmap_outputs),
427 FILTER_QUERY_FUNC(channelmap_query_formats),
428 };
429