FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_channelmap.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 151 236 64.0%
Functions: 9 9 100.0%
Branches: 77 143 53.8%

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