| 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 | 122 | static void channelmap_uninit(AVFilterContext *ctx) | |
| 83 | { | ||
| 84 | 122 | ChannelMapContext *s = ctx->priv; | |
| 85 | 122 | av_freep(&s->map); | |
| 86 | 122 | av_freep(&s->source_planes); | |
| 87 | 122 | } | |
| 88 | |||
| 89 | 468 | static char* split(char *message, char delim) { | |
| 90 | 468 | char *next = strchr(message, delim); | |
| 91 |
2/2✓ Branch 0 taken 350 times.
✓ Branch 1 taken 118 times.
|
468 | if (next) |
| 92 | 350 | *next++ = '\0'; | |
| 93 | 468 | return next; | |
| 94 | } | ||
| 95 | |||
| 96 | 200 | static int get_channel_idx(char **map, int *ch, char delim) | |
| 97 | { | ||
| 98 | char *next; | ||
| 99 | int len; | ||
| 100 | 200 | int n = 0; | |
| 101 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
|
200 | if (!*map) |
| 102 | ✗ | return AVERROR(EINVAL); | |
| 103 | 200 | next = split(*map, delim); | |
| 104 |
3/4✓ Branch 0 taken 114 times.
✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 114 times.
|
200 | if (!next && delim == '-') |
| 105 | ✗ | return AVERROR(EINVAL); | |
| 106 | 200 | len = strlen(*map); | |
| 107 | 200 | sscanf(*map, "%d%n", ch, &n); | |
| 108 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
|
200 | if (n != len) |
| 109 | ✗ | return AVERROR(EINVAL); | |
| 110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
|
200 | if (*ch < 0) |
| 111 | ✗ | return AVERROR(EINVAL); | |
| 112 | 200 | *map = next; | |
| 113 | 200 | 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 | 528 | 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 | 528 | int nb_channels = ch_layout->nb_channels; | |
| 133 | |||
| 134 |
2/4✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 528 times.
|
528 | 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 | 528 | return 0; | |
| 150 | } | ||
| 151 | |||
| 152 | 122 | static av_cold int channelmap_init(AVFilterContext *ctx) | |
| 153 | { | ||
| 154 | 122 | ChannelMapContext *s = ctx->priv; | |
| 155 | 122 | char *mapping, separator = '|'; | |
| 156 | 122 | int map_entries = 0; | |
| 157 | enum MappingMode mode; | ||
| 158 | 122 | int64_t out_ch_mask = 0; | |
| 159 | 122 | uint8_t *presence_map = NULL; | |
| 160 | 122 | int ret = 0; | |
| 161 | int i; | ||
| 162 | |||
| 163 | 122 | mapping = s->mapping_str; | |
| 164 | |||
| 165 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 118 times.
|
122 | if (!mapping) { |
| 166 | 4 | mode = MAP_NONE; | |
| 167 | } else { | ||
| 168 | 118 | char *dash = strchr(mapping, '-'); | |
| 169 |
2/2✓ Branch 0 taken 116 times.
✓ Branch 1 taken 2 times.
|
118 | if (!dash) { // short mapping |
| 170 |
2/2✓ Branch 0 taken 114 times.
✓ Branch 1 taken 2 times.
|
116 | if (av_isdigit(*mapping)) |
| 171 | 114 | 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 118 times.
✓ Branch 1 taken 4 times.
|
122 | if (mode != MAP_NONE) { |
| 188 | 118 | char *sep = mapping; | |
| 189 | 118 | map_entries = 1; | |
| 190 |
2/2✓ Branch 0 taken 218 times.
✓ Branch 1 taken 118 times.
|
336 | while ((sep = strchr(sep, separator))) { |
| 191 |
1/2✓ Branch 0 taken 218 times.
✗ Branch 1 not taken.
|
218 | if (*++sep) // Allow trailing comma |
| 192 | 218 | map_entries++; | |
| 193 | } | ||
| 194 | |||
| 195 | 118 | s->map = av_malloc_array(map_entries, sizeof(*s->map)); | |
| 196 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
|
118 | if (!s->map) |
| 197 | ✗ | return AVERROR(ENOMEM); | |
| 198 | } | ||
| 199 | |||
| 200 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 122 times.
|
458 | for (i = 0; i < map_entries; i++) { |
| 201 | 336 | int in_ch_idx = -1, out_ch_idx = -1; | |
| 202 | 336 | int in_ch = -1, out_ch = -1; | |
| 203 | static const char err[] = "Failed to parse channel map\n"; | ||
| 204 | |||
| 205 | 336 | s->map[i].in_channel_idx = -1; | |
| 206 | 336 | s->map[i].out_channel_idx = -1; | |
| 207 | 336 | s->map[i].in_channel = -1; | |
| 208 | 336 | s->map[i].out_channel = -1; | |
| 209 | |||
| 210 |
3/7✓ Branch 0 taken 200 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.
|
336 | switch (mode) { |
| 211 | 200 | case MAP_ONE_INT: | |
| 212 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
|
200 | if (get_channel_idx(&mapping, &in_ch_idx, separator) < 0) { |
| 213 | ✗ | av_log(ctx, AV_LOG_ERROR, err); | |
| 214 | ✗ | return AVERROR(EINVAL); | |
| 215 | } | ||
| 216 | 200 | s->map[i].in_channel_idx = in_ch_idx; | |
| 217 | 200 | s->map[i].out_channel_idx = i; | |
| 218 | 200 | 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 | 122 | s->mode = mode; | |
| 274 | 122 | s->nch = map_entries; | |
| 275 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 122 times.
|
122 | 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 118 times.
|
122 | 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 | 16 | s->map[i].out_channel = av_channel_layout_channel_from_index(&s->output_layout, i); | |
| 294 | } | ||
| 295 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
|
118 | } else if (s->nch != s->output_layout.nb_channels) { |
| 296 | char buf[256]; | ||
| 297 | ✗ | av_channel_layout_describe(&s->output_layout, buf, sizeof(buf)); | |
| 298 | ✗ | av_log(ctx, AV_LOG_ERROR, | |
| 299 | "Output channel layout %s does not match the number of channels mapped %d.\n", | ||
| 300 | buf, s->nch); | ||
| 301 | ✗ | return AVERROR(EINVAL); | |
| 302 | } | ||
| 303 | |||
| 304 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 122 times.
|
122 | if (!s->output_layout.nb_channels) { |
| 305 | ✗ | av_log(ctx, AV_LOG_ERROR, "Output channel layout is not set and " | |
| 306 | "cannot be guessed from the maps.\n"); | ||
| 307 | ✗ | return AVERROR(EINVAL); | |
| 308 | } | ||
| 309 | |||
| 310 |
3/4✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 120 times.
|
122 | if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) { |
| 311 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 2 times.
|
134 | for (i = 0; i < s->nch; i++) { |
| 312 | 132 | s->map[i].out_channel_idx = av_channel_layout_index_from_channel( | |
| 313 | 132 | &s->output_layout, s->map[i].out_channel); | |
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | 122 | presence_map = av_calloc(s->nch, sizeof(*presence_map)); | |
| 318 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 122 times.
|
474 | for (i = 0; i < s->nch; i++) { |
| 319 | 352 | const int out_idx = s->map[i].out_channel_idx; | |
| 320 | 352 | ret = check_idx_and_id(ctx, out_idx, s->map[i].out_channel, &s->output_layout, "out"); | |
| 321 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 352 times.
|
352 | if (ret < 0) |
| 322 | ✗ | break; | |
| 323 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 352 times.
|
352 | if (presence_map[out_idx]) { |
| 324 | char layout_name[256]; | ||
| 325 | ✗ | av_channel_layout_describe(&s->output_layout, layout_name, sizeof(layout_name)); | |
| 326 | ✗ | av_log(ctx, AV_LOG_ERROR, "Mapping %d assigns channel #%d twice in output layout '%s'.\n", | |
| 327 | ✗ | i + 1, s->map[i].out_channel_idx, layout_name); | |
| 328 | ✗ | ret = AVERROR(EINVAL); | |
| 329 | ✗ | break; | |
| 330 | } | ||
| 331 | 352 | presence_map[out_idx] = 1; | |
| 332 | } | ||
| 333 | 122 | av_freep(&presence_map); | |
| 334 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 122 times.
|
122 | if (ret < 0) |
| 335 | ✗ | return ret; | |
| 336 | |||
| 337 | 122 | return 0; | |
| 338 | } | ||
| 339 | |||
| 340 | 61 | static int channelmap_query_formats(const AVFilterContext *ctx, | |
| 341 | AVFilterFormatsConfig **cfg_in, | ||
| 342 | AVFilterFormatsConfig **cfg_out) | ||
| 343 | { | ||
| 344 | 61 | const ChannelMapContext *s = ctx->priv; | |
| 345 | 61 | AVFilterChannelLayouts *channel_layouts = NULL; | |
| 346 | |||
| 347 | int ret; | ||
| 348 | |||
| 349 | 61 | ret = ff_set_common_formats2(ctx, cfg_in, cfg_out, ff_planar_sample_fmts()); | |
| 350 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
|
61 | if (ret < 0) |
| 351 | ✗ | return ret; | |
| 352 | |||
| 353 | 61 | ret = ff_add_channel_layout(&channel_layouts, &s->output_layout); | |
| 354 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
|
61 | if (ret < 0) |
| 355 | ✗ | return ret; | |
| 356 | |||
| 357 | 61 | ret = ff_channel_layouts_ref(channel_layouts, &cfg_out[0]->channel_layouts); | |
| 358 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
|
61 | if (ret < 0) |
| 359 | ✗ | return ret; | |
| 360 | |||
| 361 | 61 | return 0; | |
| 362 | } | ||
| 363 | |||
| 364 | 874 | static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf) | |
| 365 | { | ||
| 366 | 874 | AVFilterContext *ctx = inlink->dst; | |
| 367 | 874 | AVFilterLink *outlink = ctx->outputs[0]; | |
| 368 | 874 | const ChannelMapContext *s = ctx->priv; | |
| 369 | 874 | const int nch_in = inlink->ch_layout.nb_channels; | |
| 370 | 874 | const int nch_out = s->nch; | |
| 371 | int ch, ret; | ||
| 372 | |||
| 373 | 874 | memcpy(s->source_planes, buf->extended_data, | |
| 374 | nch_in * sizeof(s->source_planes[0])); | ||
| 375 | |||
| 376 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 874 times.
|
874 | if (nch_out > nch_in) { |
| 377 | ✗ | if (nch_out > FF_ARRAY_ELEMS(buf->data)) { | |
| 378 | uint8_t **new_extended_data = | ||
| 379 | ✗ | av_calloc(nch_out, sizeof(*buf->extended_data)); | |
| 380 | ✗ | if (!new_extended_data) { | |
| 381 | ✗ | av_frame_free(&buf); | |
| 382 | ✗ | return AVERROR(ENOMEM); | |
| 383 | } | ||
| 384 | ✗ | if (buf->extended_data == buf->data) { | |
| 385 | ✗ | buf->extended_data = new_extended_data; | |
| 386 | } else { | ||
| 387 | ✗ | av_free(buf->extended_data); | |
| 388 | ✗ | buf->extended_data = new_extended_data; | |
| 389 | } | ||
| 390 | ✗ | } else if (buf->extended_data != buf->data) { | |
| 391 | ✗ | av_free(buf->extended_data); | |
| 392 | ✗ | buf->extended_data = buf->data; | |
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 |
2/2✓ Branch 0 taken 2222 times.
✓ Branch 1 taken 874 times.
|
3096 | for (ch = 0; ch < nch_out; ch++) { |
| 397 | 2222 | buf->extended_data[s->map[ch].out_channel_idx] = | |
| 398 | 2222 | s->source_planes[s->map[ch].in_channel_idx]; | |
| 399 | } | ||
| 400 | |||
| 401 |
2/2✓ Branch 0 taken 517 times.
✓ Branch 1 taken 357 times.
|
874 | if (buf->data != buf->extended_data) |
| 402 | 517 | memcpy(buf->data, buf->extended_data, | |
| 403 |
2/2✓ Branch 0 taken 516 times.
✓ Branch 1 taken 1 times.
|
517 | FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0])); |
| 404 | |||
| 405 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 874 times.
|
874 | if ((ret = av_channel_layout_copy(&buf->ch_layout, &outlink->ch_layout)) < 0) |
| 406 | ✗ | return ret; | |
| 407 | |||
| 408 | 874 | return ff_filter_frame(outlink, buf); | |
| 409 | } | ||
| 410 | |||
| 411 | 61 | static int channelmap_config_input(AVFilterLink *inlink) | |
| 412 | { | ||
| 413 | 61 | AVFilterContext *ctx = inlink->dst; | |
| 414 | 61 | ChannelMapContext *s = ctx->priv; | |
| 415 | 61 | int i, err = 0; | |
| 416 | |||
| 417 |
2/2✓ Branch 0 taken 176 times.
✓ Branch 1 taken 61 times.
|
237 | for (i = 0; i < s->nch; i++) { |
| 418 | 176 | struct ChannelMap *m = &s->map[i]; | |
| 419 | |||
| 420 |
5/6✓ Branch 0 taken 176 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 66 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 108 times.
|
176 | if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR || s->mode == MAP_ONE_STR) { |
| 421 | 68 | m->in_channel_idx = av_channel_layout_index_from_channel( | |
| 422 | 68 | &inlink->ch_layout, m->in_channel); | |
| 423 | } | ||
| 424 | |||
| 425 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
|
176 | if (check_idx_and_id(ctx, m->in_channel_idx, m->in_channel, &inlink->ch_layout, "in") < 0) |
| 426 | ✗ | err = AVERROR(EINVAL); | |
| 427 | } | ||
| 428 | |||
| 429 | 61 | av_freep(&s->source_planes); | |
| 430 | 61 | s->source_planes = av_calloc(inlink->ch_layout.nb_channels, | |
| 431 | sizeof(*s->source_planes)); | ||
| 432 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
|
61 | if (!s->source_planes) |
| 433 | ✗ | return AVERROR(ENOMEM); | |
| 434 | |||
| 435 | 61 | return err; | |
| 436 | } | ||
| 437 | |||
| 438 | static const AVFilterPad avfilter_af_channelmap_inputs[] = { | ||
| 439 | { | ||
| 440 | .name = "default", | ||
| 441 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 442 | .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE, | ||
| 443 | .filter_frame = channelmap_filter_frame, | ||
| 444 | .config_props = channelmap_config_input, | ||
| 445 | }, | ||
| 446 | }; | ||
| 447 | |||
| 448 | const FFFilter ff_af_channelmap = { | ||
| 449 | .p.name = "channelmap", | ||
| 450 | .p.description = NULL_IF_CONFIG_SMALL("Remap audio channels."), | ||
| 451 | .p.priv_class = &channelmap_class, | ||
| 452 | .init = channelmap_init, | ||
| 453 | .uninit = channelmap_uninit, | ||
| 454 | .priv_size = sizeof(ChannelMapContext), | ||
| 455 | FILTER_INPUTS(avfilter_af_channelmap_inputs), | ||
| 456 | FILTER_OUTPUTS(ff_audio_default_filterpad), | ||
| 457 | FILTER_QUERY_FUNC2(channelmap_query_formats), | ||
| 458 | }; | ||
| 459 |