FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_channelmap.c
Date: 2024-04-18 10:05:09
Exec Total Coverage
Lines: 122 216 56.5%
Functions: 8 8 100.0%
Branches: 65 143 45.5%

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/mem.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/samplefmt.h"
35
36 #include "audio.h"
37 #include "avfilter.h"
38 #include "formats.h"
39 #include "internal.h"
40
41 struct ChannelMap {
42 int in_channel;
43 int out_channel;
44 int in_channel_idx;
45 int out_channel_idx;
46 };
47
48 enum MappingMode {
49 MAP_NONE,
50 MAP_ONE_INT,
51 MAP_ONE_STR,
52 MAP_PAIR_INT_INT,
53 MAP_PAIR_INT_STR,
54 MAP_PAIR_STR_INT,
55 MAP_PAIR_STR_STR
56 };
57
58 #define MAX_CH 64
59 typedef struct ChannelMapContext {
60 const AVClass *class;
61 char *mapping_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(output_layout), AV_OPT_TYPE_CHLAYOUT, .flags = A|F },
76 { NULL }
77 };
78
79 AVFILTER_DEFINE_CLASS(channelmap);
80
81 120 static char* split(char *message, char delim) {
82 120 char *next = strchr(message, delim);
83
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 72 times.
120 if (next)
84 48 *next++ = '\0';
85 120 return next;
86 }
87
88 116 static int get_channel_idx(char **map, int *ch, char delim, int max_nb_channels)
89 {
90 char *next;
91 int len;
92 116 int n = 0;
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
116 if (!*map)
94 return AVERROR(EINVAL);
95 116 next = split(*map, delim);
96
3/4
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 70 times.
116 if (!next && delim == '-')
97 return AVERROR(EINVAL);
98 116 len = strlen(*map);
99 116 sscanf(*map, "%d%n", ch, &n);
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
116 if (n != len)
101 return AVERROR(EINVAL);
102
2/4
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 116 times.
116 if (*ch < 0 || *ch >= max_nb_channels)
103 return AVERROR(EINVAL);
104 116 *map = next;
105 116 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 204 static int check_idx_and_id(AVFilterContext *ctx, int channel_idx, int channel, AVChannelLayout *ch_layout, const char *io)
121 {
122 char channel_name[64];
123 char layout_name[256];
124 204 int nb_channels = ch_layout->nb_channels;
125
126
2/4
✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 204 times.
204 if (channel_idx < 0 || channel_idx >= nb_channels) {
127 av_channel_layout_describe(ch_layout, layout_name, sizeof(layout_name));
128 if (channel >= 0) {
129 av_channel_name(channel_name, sizeof(channel_name), channel);
130 av_log(ctx, AV_LOG_ERROR,
131 "%sput channel '%s' not available from %sput layout '%s'\n",
132 io, channel_name, io, layout_name);
133 } else {
134 av_log(ctx, AV_LOG_ERROR,
135 "%sput channel #%d not available from %sput layout '%s'\n",
136 io, channel_idx, io, layout_name);
137 }
138 return AVERROR(EINVAL);
139 }
140
141 204 return 0;
142 }
143
144 76 static av_cold int channelmap_init(AVFilterContext *ctx)
145 {
146 76 ChannelMapContext *s = ctx->priv;
147 76 char *mapping, separator = '|';
148 76 int map_entries = 0;
149 enum MappingMode mode;
150 76 int64_t out_ch_mask = 0;
151 uint64_t presence_mask;
152 int i;
153
154 76 mapping = s->mapping_str;
155
156
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 72 times.
76 if (!mapping) {
157 4 mode = MAP_NONE;
158 } else {
159 72 char *dash = strchr(mapping, '-');
160
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 if (!dash) { // short mapping
161
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 2 times.
72 if (av_isdigit(*mapping))
162 70 mode = MAP_ONE_INT;
163 else
164 2 mode = MAP_ONE_STR;
165 } else if (av_isdigit(*mapping)) {
166 if (av_isdigit(*(dash+1)))
167 mode = MAP_PAIR_INT_INT;
168 else
169 mode = MAP_PAIR_INT_STR;
170 } else {
171 if (av_isdigit(*(dash+1)))
172 mode = MAP_PAIR_STR_INT;
173 else
174 mode = MAP_PAIR_STR_STR;
175 }
176 }
177
178
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 4 times.
76 if (mode != MAP_NONE) {
179 72 char *sep = mapping;
180 72 map_entries = 1;
181
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 72 times.
120 while ((sep = strchr(sep, separator))) {
182
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 if (*++sep) // Allow trailing comma
183 48 map_entries++;
184 }
185 }
186
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (map_entries > MAX_CH) {
188 av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
189 return AVERROR(EINVAL);
190 }
191
192
2/2
✓ Branch 0 taken 4864 times.
✓ Branch 1 taken 76 times.
4940 for (i = 0; i < MAX_CH; i++) {
193 4864 s->map[i].in_channel_idx = -1;
194 4864 s->map[i].out_channel_idx = -1;
195 4864 s->map[i].in_channel = -1;
196 4864 s->map[i].out_channel = -1;
197 }
198
199
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 76 times.
196 for (i = 0; i < map_entries; i++) {
200 120 int in_ch_idx = -1, out_ch_idx = -1;
201 120 int in_ch = -1, out_ch = -1;
202 static const char err[] = "Failed to parse channel map\n";
203
2/7
✓ Branch 0 taken 116 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.
120 switch (mode) {
204 116 case MAP_ONE_INT:
205
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 116 times.
116 if (get_channel_idx(&mapping, &in_ch_idx, separator, MAX_CH) < 0) {
206 av_log(ctx, AV_LOG_ERROR, err);
207 return AVERROR(EINVAL);
208 }
209 116 s->map[i].in_channel_idx = in_ch_idx;
210 116 s->map[i].out_channel_idx = i;
211 116 break;
212 4 case MAP_ONE_STR:
213
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (get_channel(&mapping, &in_ch, separator) < 0) {
214 av_log(ctx, AV_LOG_ERROR, err);
215 return AVERROR(EINVAL);
216 }
217 4 s->map[i].in_channel = in_ch;
218 4 s->map[i].out_channel_idx = i;
219 4 break;
220 case MAP_PAIR_INT_INT:
221 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
222 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
223 av_log(ctx, AV_LOG_ERROR, err);
224 return AVERROR(EINVAL);
225 }
226 s->map[i].in_channel_idx = in_ch_idx;
227 s->map[i].out_channel_idx = out_ch_idx;
228 break;
229 case MAP_PAIR_INT_STR:
230 if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
231 get_channel(&mapping, &out_ch, separator) < 0) {
232 av_log(ctx, AV_LOG_ERROR, err);
233 return AVERROR(EINVAL);
234 }
235 s->map[i].in_channel_idx = in_ch_idx;
236 s->map[i].out_channel = out_ch;
237 if (out_ch < 63)
238 out_ch_mask |= 1ULL << out_ch;
239 else
240 out_ch_mask = -1;
241 break;
242 case MAP_PAIR_STR_INT:
243 if (get_channel(&mapping, &in_ch, '-') < 0 ||
244 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
245 av_log(ctx, AV_LOG_ERROR, err);
246 return AVERROR(EINVAL);
247 }
248 s->map[i].in_channel = in_ch;
249 s->map[i].out_channel_idx = out_ch_idx;
250 break;
251 case MAP_PAIR_STR_STR:
252 if (get_channel(&mapping, &in_ch, '-') < 0 ||
253 get_channel(&mapping, &out_ch, separator) < 0) {
254 av_log(ctx, AV_LOG_ERROR, err);
255 return AVERROR(EINVAL);
256 }
257 s->map[i].in_channel = in_ch;
258 s->map[i].out_channel = out_ch;
259 if (out_ch < 63)
260 out_ch_mask |= 1ULL << out_ch;
261 else
262 out_ch_mask = -1;
263 break;
264 }
265 }
266 76 s->mode = mode;
267 76 s->nch = map_entries;
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (s->output_layout.nb_channels == 0) {
269 if (out_ch_mask > 0)
270 av_channel_layout_from_mask(&s->output_layout, out_ch_mask);
271 else if (map_entries)
272 av_channel_layout_default(&s->output_layout, map_entries);
273 }
274
275
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 72 times.
76 if (mode == MAP_NONE) {
276 int i;
277 4 s->nch = s->output_layout.nb_channels;
278
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (i = 0; i < s->nch; i++) {
279 16 s->map[i].in_channel_idx = i;
280 16 s->map[i].out_channel_idx = i;
281 }
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
72 } else if (s->nch != s->output_layout.nb_channels) {
283 char buf[256];
284 av_channel_layout_describe(&s->output_layout, buf, sizeof(buf));
285 av_log(ctx, AV_LOG_ERROR,
286 "Output channel layout %s does not match the number of channels mapped %d.\n",
287 buf, s->nch);
288 return AVERROR(EINVAL);
289 }
290
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (!s->output_layout.nb_channels) {
292 av_log(ctx, AV_LOG_ERROR, "Output channel layout is not set and "
293 "cannot be guessed from the maps.\n");
294 return AVERROR(EINVAL);
295 }
296
297
2/4
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 76 times.
76 if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
298 for (i = 0; i < s->nch; i++) {
299 s->map[i].out_channel_idx = av_channel_layout_index_from_channel(
300 &s->output_layout, s->map[i].out_channel);
301 }
302 }
303
304 76 presence_mask = 0;
305
2/2
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 76 times.
212 for (i = 0; i < s->nch; i++) {
306 uint64_t idx_mask;
307 136 int ret = check_idx_and_id(ctx, s->map[i].out_channel_idx, s->map[i].out_channel, &s->output_layout, "out");
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (ret < 0)
309 return ret;
310 136 idx_mask = (1ULL << s->map[i].out_channel_idx);
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (presence_mask & idx_mask) {
312 char layout_name[256];
313 av_channel_layout_describe(&s->output_layout, layout_name, sizeof(layout_name));
314 av_log(ctx, AV_LOG_ERROR, "Mapping %d assigns channel #%d twice in output layout '%s'.\n",
315 i + 1, s->map[i].out_channel_idx, layout_name);
316 return AVERROR(EINVAL);
317 }
318 136 presence_mask |= idx_mask;
319 }
320
321 76 return 0;
322 }
323
324 38 static int channelmap_query_formats(AVFilterContext *ctx)
325 {
326 38 ChannelMapContext *s = ctx->priv;
327 38 AVFilterChannelLayouts *channel_layouts = NULL;
328 int ret;
329
330
2/4
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
76 if ((ret = ff_set_common_formats (ctx, ff_planar_sample_fmts())) < 0 ||
331
1/2
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
76 (ret = ff_set_common_all_samplerates(ctx )) < 0 ||
332
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
76 (ret = ff_add_channel_layout(&channel_layouts, &s->output_layout)) < 0 ||
333 38 (ret = ff_channel_layouts_ref(channel_layouts,
334 38 &ctx->outputs[0]->incfg.channel_layouts)) < 0)
335 return ret;
336
337 38 return ff_channel_layouts_ref(ff_all_channel_counts(),
338 38 &ctx->inputs[0]->outcfg.channel_layouts);
339 }
340
341 556 static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf)
342 {
343 556 AVFilterContext *ctx = inlink->dst;
344 556 AVFilterLink *outlink = ctx->outputs[0];
345 556 const ChannelMapContext *s = ctx->priv;
346 556 const int nch_in = inlink->ch_layout.nb_channels;
347 556 const int nch_out = s->nch;
348 int ch, ret;
349 uint8_t *source_planes[MAX_CH];
350
351 556 memcpy(source_planes, buf->extended_data,
352 nch_in * sizeof(source_planes[0]));
353
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 556 times.
556 if (nch_out > nch_in) {
355 if (nch_out > FF_ARRAY_ELEMS(buf->data)) {
356 uint8_t **new_extended_data =
357 av_calloc(nch_out, sizeof(*buf->extended_data));
358 if (!new_extended_data) {
359 av_frame_free(&buf);
360 return AVERROR(ENOMEM);
361 }
362 if (buf->extended_data == buf->data) {
363 buf->extended_data = new_extended_data;
364 } else {
365 av_free(buf->extended_data);
366 buf->extended_data = new_extended_data;
367 }
368 } else if (buf->extended_data != buf->data) {
369 av_free(buf->extended_data);
370 buf->extended_data = buf->data;
371 }
372 }
373
374
2/2
✓ Branch 0 taken 1228 times.
✓ Branch 1 taken 556 times.
1784 for (ch = 0; ch < nch_out; ch++) {
375 1228 buf->extended_data[s->map[ch].out_channel_idx] =
376 1228 source_planes[s->map[ch].in_channel_idx];
377 }
378
379
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 244 times.
556 if (buf->data != buf->extended_data)
380 312 memcpy(buf->data, buf->extended_data,
381
1/2
✓ Branch 0 taken 312 times.
✗ Branch 1 not taken.
312 FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
382
383
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 556 times.
556 if ((ret = av_channel_layout_copy(&buf->ch_layout, &outlink->ch_layout)) < 0)
384 return ret;
385
386 556 return ff_filter_frame(outlink, buf);
387 }
388
389 38 static int channelmap_config_input(AVFilterLink *inlink)
390 {
391 38 AVFilterContext *ctx = inlink->dst;
392 38 ChannelMapContext *s = ctx->priv;
393 38 int i, err = 0;
394
395
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 38 times.
106 for (i = 0; i < s->nch; i++) {
396 68 struct ChannelMap *m = &s->map[i];
397
398
4/6
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 66 times.
68 if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR || s->mode == MAP_ONE_STR) {
399 2 m->in_channel_idx = av_channel_layout_index_from_channel(
400 2 &inlink->ch_layout, m->in_channel);
401 }
402
403
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
68 if (check_idx_and_id(ctx, m->in_channel_idx, m->in_channel, &inlink->ch_layout, "in") < 0)
404 err = AVERROR(EINVAL);
405 }
406
407 38 return err;
408 }
409
410 static const AVFilterPad avfilter_af_channelmap_inputs[] = {
411 {
412 .name = "default",
413 .type = AVMEDIA_TYPE_AUDIO,
414 .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
415 .filter_frame = channelmap_filter_frame,
416 .config_props = channelmap_config_input,
417 },
418 };
419
420 const AVFilter ff_af_channelmap = {
421 .name = "channelmap",
422 .description = NULL_IF_CONFIG_SMALL("Remap audio channels."),
423 .init = channelmap_init,
424 .priv_size = sizeof(ChannelMapContext),
425 .priv_class = &channelmap_class,
426 FILTER_INPUTS(avfilter_af_channelmap_inputs),
427 FILTER_OUTPUTS(ff_audio_default_filterpad),
428 FILTER_QUERY_FUNC(channelmap_query_formats),
429 };
430