| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of FFmpeg. | ||
| 3 | * | ||
| 4 | * FFmpeg is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU Lesser General Public | ||
| 6 | * License as published by the Free Software Foundation; either | ||
| 7 | * version 2.1 of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | * Lesser General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU Lesser General Public | ||
| 15 | * License along with FFmpeg; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 17 | */ | ||
| 18 | |||
| 19 | /** | ||
| 20 | * @file | ||
| 21 | * Audio join filter | ||
| 22 | * | ||
| 23 | * Join multiple audio inputs as different channels in | ||
| 24 | * a single output | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include "libavutil/avassert.h" | ||
| 28 | #include "libavutil/avstring.h" | ||
| 29 | #include "libavutil/channel_layout.h" | ||
| 30 | #include "libavutil/common.h" | ||
| 31 | #include "libavutil/mem.h" | ||
| 32 | #include "libavutil/opt.h" | ||
| 33 | |||
| 34 | #include "audio.h" | ||
| 35 | #include "avfilter.h" | ||
| 36 | #include "formats.h" | ||
| 37 | #include "filters.h" | ||
| 38 | |||
| 39 | typedef struct ChannelMap { | ||
| 40 | int input; ///< input stream index | ||
| 41 | int in_channel_idx; ///< index of in_channel in the input stream data | ||
| 42 | enum AVChannel in_channel; | ||
| 43 | enum AVChannel out_channel; | ||
| 44 | } ChannelMap; | ||
| 45 | |||
| 46 | typedef struct JoinContext { | ||
| 47 | const AVClass *class; | ||
| 48 | |||
| 49 | int inputs; | ||
| 50 | char *map; | ||
| 51 | AVChannelLayout ch_layout; | ||
| 52 | |||
| 53 | int64_t eof_pts; | ||
| 54 | int eof; | ||
| 55 | |||
| 56 | ChannelMap *channels; | ||
| 57 | |||
| 58 | /** | ||
| 59 | * Temporary storage for input frames, until we get one on each input. | ||
| 60 | */ | ||
| 61 | AVFrame **input_frames; | ||
| 62 | |||
| 63 | /** | ||
| 64 | * Temporary storage for buffer references, for assembling the output frame. | ||
| 65 | */ | ||
| 66 | AVBufferRef **buffers; | ||
| 67 | } JoinContext; | ||
| 68 | |||
| 69 | #define OFFSET(x) offsetof(JoinContext, x) | ||
| 70 | #define A AV_OPT_FLAG_AUDIO_PARAM | ||
| 71 | #define F AV_OPT_FLAG_FILTERING_PARAM | ||
| 72 | static const AVOption join_options[] = { | ||
| 73 | { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A|F }, | ||
| 74 | { "channel_layout", "Channel layout of the " | ||
| 75 | "output stream.", OFFSET(ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = "stereo"}, 0, 0, A|F }, | ||
| 76 | { "map", "A comma-separated list of channels maps in the format " | ||
| 77 | "'input_stream.input_channel-output_channel.", | ||
| 78 | OFFSET(map), AV_OPT_TYPE_STRING, .flags = A|F }, | ||
| 79 | { NULL } | ||
| 80 | }; | ||
| 81 | |||
| 82 | #define MAP_SEPARATOR '|' | ||
| 83 | |||
| 84 | AVFILTER_DEFINE_CLASS(join); | ||
| 85 | |||
| 86 | 4 | static int parse_maps(AVFilterContext *ctx) | |
| 87 | { | ||
| 88 | 4 | JoinContext *s = ctx->priv; | |
| 89 | 4 | char *cur = s->map; | |
| 90 | |||
| 91 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
4 | while (cur && *cur) { |
| 92 | ChannelMap *map; | ||
| 93 | char *sep, *next, *p; | ||
| 94 | int input_idx, out_ch_idx; | ||
| 95 | |||
| 96 | ✗ | next = strchr(cur, MAP_SEPARATOR); | |
| 97 | ✗ | if (next) | |
| 98 | ✗ | *next++ = 0; | |
| 99 | |||
| 100 | /* split the map into input and output parts */ | ||
| 101 | ✗ | if (!(sep = strchr(cur, '-'))) { | |
| 102 | ✗ | av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel " | |
| 103 | "map '%s'\n", cur); | ||
| 104 | ✗ | return AVERROR(EINVAL); | |
| 105 | } | ||
| 106 | ✗ | *sep++ = 0; | |
| 107 | |||
| 108 | /* parse output channel */ | ||
| 109 | ✗ | out_ch_idx = av_channel_layout_index_from_string(&s->ch_layout, sep); | |
| 110 | ✗ | if (out_ch_idx < 0) { | |
| 111 | ✗ | av_log(ctx, AV_LOG_ERROR, "Invalid output channel: %s.\n", sep); | |
| 112 | ✗ | return AVERROR(EINVAL); | |
| 113 | } | ||
| 114 | |||
| 115 | ✗ | map = &s->channels[out_ch_idx]; | |
| 116 | |||
| 117 | ✗ | if (map->input >= 0) { | |
| 118 | ✗ | av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel " | |
| 119 | "'%s'.\n", sep); | ||
| 120 | ✗ | return AVERROR(EINVAL); | |
| 121 | } | ||
| 122 | |||
| 123 | /* parse input channel */ | ||
| 124 | ✗ | input_idx = strtol(cur, &cur, 0); | |
| 125 | ✗ | if (input_idx < 0 || input_idx >= s->inputs) { | |
| 126 | ✗ | av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n", | |
| 127 | input_idx); | ||
| 128 | ✗ | return AVERROR(EINVAL); | |
| 129 | } | ||
| 130 | |||
| 131 | ✗ | if (*cur) | |
| 132 | ✗ | cur++; | |
| 133 | |||
| 134 | ✗ | map->input = input_idx; | |
| 135 | ✗ | map->in_channel = AV_CHAN_NONE; | |
| 136 | ✗ | map->in_channel_idx = strtol(cur, &p, 0); | |
| 137 | ✗ | if (p == cur) { | |
| 138 | /* channel specifier is not a number, handle as channel name */ | ||
| 139 | ✗ | map->in_channel = av_channel_from_string(cur); | |
| 140 | ✗ | if (map->in_channel < 0) { | |
| 141 | ✗ | av_log(ctx, AV_LOG_ERROR, "Invalid input channel: %s.\n", cur); | |
| 142 | ✗ | return AVERROR(EINVAL); | |
| 143 | } | ||
| 144 | ✗ | } else if (map->in_channel_idx < 0) { | |
| 145 | ✗ | av_log(ctx, AV_LOG_ERROR, "Invalid input channel index: %d\n", map->in_channel_idx); | |
| 146 | ✗ | return AVERROR(EINVAL); | |
| 147 | } | ||
| 148 | |||
| 149 | ✗ | cur = next; | |
| 150 | } | ||
| 151 | 4 | return 0; | |
| 152 | } | ||
| 153 | |||
| 154 | 4 | static av_cold int join_init(AVFilterContext *ctx) | |
| 155 | { | ||
| 156 | 4 | JoinContext *s = ctx->priv; | |
| 157 | int ret, i; | ||
| 158 | |||
| 159 | 4 | s->channels = av_calloc(s->ch_layout.nb_channels, sizeof(*s->channels)); | |
| 160 | 4 | s->buffers = av_calloc(s->ch_layout.nb_channels, sizeof(*s->buffers)); | |
| 161 | 4 | s->input_frames = av_calloc(s->inputs, sizeof(*s->input_frames)); | |
| 162 |
3/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
|
4 | if (!s->channels || !s->buffers|| !s->input_frames) |
| 163 | ✗ | return AVERROR(ENOMEM); | |
| 164 | |||
| 165 |
2/2✓ Branch 0 taken 142 times.
✓ Branch 1 taken 4 times.
|
146 | for (i = 0; i < s->ch_layout.nb_channels; i++) { |
| 166 | 142 | s->channels[i].out_channel = av_channel_layout_channel_from_index(&s->ch_layout, i); | |
| 167 | 142 | s->channels[i].input = -1; | |
| 168 | 142 | s->channels[i].in_channel_idx = -1; | |
| 169 | 142 | s->channels[i].in_channel = AV_CHAN_NONE; | |
| 170 | } | ||
| 171 | |||
| 172 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if ((ret = parse_maps(ctx)) < 0) |
| 173 | ✗ | return ret; | |
| 174 | |||
| 175 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 4 times.
|
140 | for (i = 0; i < s->inputs; i++) { |
| 176 | 136 | AVFilterPad pad = { 0 }; | |
| 177 | |||
| 178 | 136 | pad.type = AVMEDIA_TYPE_AUDIO; | |
| 179 | 136 | pad.name = av_asprintf("input%d", i); | |
| 180 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | if (!pad.name) |
| 181 | ✗ | return AVERROR(ENOMEM); | |
| 182 | |||
| 183 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
|
136 | if ((ret = ff_append_inpad_free_name(ctx, &pad)) < 0) |
| 184 | ✗ | return ret; | |
| 185 | } | ||
| 186 | |||
| 187 | 4 | return 0; | |
| 188 | } | ||
| 189 | |||
| 190 | 4 | static av_cold void join_uninit(AVFilterContext *ctx) | |
| 191 | { | ||
| 192 | 4 | JoinContext *s = ctx->priv; | |
| 193 | int i; | ||
| 194 | |||
| 195 |
3/4✓ Branch 0 taken 136 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 136 times.
✗ Branch 3 not taken.
|
140 | for (i = 0; i < s->inputs && s->input_frames; i++) { |
| 196 | 136 | av_frame_free(&s->input_frames[i]); | |
| 197 | } | ||
| 198 | |||
| 199 | 4 | av_freep(&s->channels); | |
| 200 | 4 | av_freep(&s->buffers); | |
| 201 | 4 | av_freep(&s->input_frames); | |
| 202 | 4 | } | |
| 203 | |||
| 204 | 2 | static int join_query_formats(const AVFilterContext *ctx, | |
| 205 | AVFilterFormatsConfig **cfg_in, | ||
| 206 | AVFilterFormatsConfig **cfg_out) | ||
| 207 | { | ||
| 208 | 2 | const JoinContext *s = ctx->priv; | |
| 209 | 2 | AVFilterChannelLayouts *layouts = NULL; | |
| 210 | int i, ret; | ||
| 211 | |||
| 212 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
|
4 | if ((ret = ff_add_channel_layout(&layouts, &s->ch_layout)) < 0 || |
| 213 | 2 | (ret = ff_channel_layouts_ref(layouts, &cfg_out[0]->channel_layouts)) < 0) | |
| 214 | ✗ | return ret; | |
| 215 | |||
| 216 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 2 times.
|
70 | for (i = 0; i < ctx->nb_inputs; i++) { |
| 217 | 68 | layouts = ff_all_channel_layouts(); | |
| 218 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
|
68 | if ((ret = ff_channel_layouts_ref(layouts, &cfg_in[i]->channel_layouts)) < 0) |
| 219 | ✗ | return ret; | |
| 220 | } | ||
| 221 | |||
| 222 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if ((ret = ff_set_common_formats2(ctx, cfg_in, cfg_out, ff_planar_sample_fmts())) < 0) |
| 223 | ✗ | return ret; | |
| 224 | |||
| 225 | 2 | return 0; | |
| 226 | } | ||
| 227 | |||
| 228 | typedef struct ChannelList { | ||
| 229 | enum AVChannel *ch; | ||
| 230 | int nb_ch; | ||
| 231 | } ChannelList; | ||
| 232 | |||
| 233 | 71 | static enum AVChannel channel_list_pop(ChannelList *chl, int idx) | |
| 234 | { | ||
| 235 | 71 | enum AVChannel ret = chl->ch[idx]; | |
| 236 | 71 | memmove(chl->ch + idx, chl->ch + idx + 1, | |
| 237 | 71 | (chl->nb_ch - idx - 1) * sizeof(*chl->ch)); | |
| 238 | 71 | chl->nb_ch--; | |
| 239 | 71 | return ret; | |
| 240 | } | ||
| 241 | |||
| 242 | /* | ||
| 243 | * If ch is present in chl, remove it from the list and return it. | ||
| 244 | * Otherwise return AV_CHAN_NONE. | ||
| 245 | */ | ||
| 246 | 4364 | static enum AVChannel channel_list_pop_ch(ChannelList *chl, enum AVChannel ch) | |
| 247 | { | ||
| 248 |
2/2✓ Branch 0 taken 4367 times.
✓ Branch 1 taken 4362 times.
|
8729 | for (int i = 0; i < chl->nb_ch; i++) |
| 249 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4365 times.
|
4367 | if (chl->ch[i] == ch) |
| 250 | 2 | return channel_list_pop(chl, i); | |
| 251 | 4362 | return AV_CHAN_NONE; | |
| 252 | } | ||
| 253 | |||
| 254 | 71 | static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch, | |
| 255 | ChannelList *inputs) | ||
| 256 | { | ||
| 257 | int i; | ||
| 258 | |||
| 259 |
2/2✓ Branch 0 taken 4364 times.
✓ Branch 1 taken 69 times.
|
4433 | for (i = 0; i < ctx->nb_inputs; i++) { |
| 260 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4362 times.
|
4364 | if (channel_list_pop_ch(&inputs[i], ch->out_channel) != AV_CHAN_NONE) { |
| 261 | 2 | ch->input = i; | |
| 262 | 2 | ch->in_channel = ch->out_channel; | |
| 263 | 2 | return; | |
| 264 | } | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | 69 | static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch, | |
| 269 | ChannelList *inputs) | ||
| 270 | { | ||
| 271 | int i; | ||
| 272 | |||
| 273 |
1/2✓ Branch 0 taken 2217 times.
✗ Branch 1 not taken.
|
2217 | for (i = 0; i < ctx->nb_inputs; i++) { |
| 274 |
2/2✓ Branch 0 taken 69 times.
✓ Branch 1 taken 2148 times.
|
2217 | if (inputs[i].nb_ch) { |
| 275 | 69 | ch->input = i; | |
| 276 | 69 | ch->in_channel = channel_list_pop(&inputs[i], 0); | |
| 277 | 69 | return; | |
| 278 | } | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | 2 | static int join_config_output(AVFilterLink *outlink) | |
| 283 | { | ||
| 284 | 2 | AVFilterContext *ctx = outlink->src; | |
| 285 | 2 | JoinContext *s = ctx->priv; | |
| 286 | // unused channels from each input | ||
| 287 | ChannelList *inputs_unused; | ||
| 288 | char inbuf[64], outbuf[64]; | ||
| 289 | 2 | int i, ret = 0; | |
| 290 | |||
| 291 | /* initialize unused channel list for each input */ | ||
| 292 | 2 | inputs_unused = av_calloc(ctx->nb_inputs, sizeof(*inputs_unused)); | |
| 293 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!inputs_unused) |
| 294 | ✗ | return AVERROR(ENOMEM); | |
| 295 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 2 times.
|
70 | for (i = 0; i < ctx->nb_inputs; i++) { |
| 296 | 68 | AVFilterLink *inlink = ctx->inputs[i]; | |
| 297 | 68 | AVChannelLayout *chl = &inlink->ch_layout; | |
| 298 | 68 | ChannelList *iu = &inputs_unused[i]; | |
| 299 | |||
| 300 | 68 | iu->nb_ch = chl->nb_channels; | |
| 301 | 68 | iu->ch = av_malloc_array(iu->nb_ch, sizeof(*iu->ch)); | |
| 302 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
|
68 | if (!iu->ch) { |
| 303 | ✗ | ret = AVERROR(ENOMEM); | |
| 304 | ✗ | goto fail; | |
| 305 | } | ||
| 306 | |||
| 307 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 68 times.
|
139 | for (int ch_idx = 0; ch_idx < iu->nb_ch; ch_idx++) { |
| 308 | 71 | iu->ch[ch_idx] = av_channel_layout_channel_from_index(chl, ch_idx); | |
| 309 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (iu->ch[ch_idx] < 0) { |
| 310 | /* no channel ordering information in this input, | ||
| 311 | * so don't auto-map from it */ | ||
| 312 | ✗ | iu->nb_ch = 0; | |
| 313 | ✗ | break; | |
| 314 | } | ||
| 315 | } | ||
| 316 | } | ||
| 317 | |||
| 318 | /* process user-specified maps */ | ||
| 319 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 2 times.
|
73 | for (i = 0; i < s->ch_layout.nb_channels; i++) { |
| 320 | 71 | ChannelMap *ch = &s->channels[i]; | |
| 321 | AVFilterLink *inlink; | ||
| 322 | AVChannelLayout *ichl; | ||
| 323 | ChannelList *iu; | ||
| 324 | |||
| 325 |
1/2✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
|
71 | if (ch->input < 0) |
| 326 | 71 | continue; | |
| 327 | |||
| 328 | ✗ | inlink = ctx->inputs[ch->input]; | |
| 329 | ✗ | ichl = &inlink->ch_layout; | |
| 330 | ✗ | iu = &inputs_unused[ch->input]; | |
| 331 | |||
| 332 | /* get the index for the channels defined by name */ | ||
| 333 | ✗ | if (ch->in_channel != AV_CHAN_NONE) { | |
| 334 | ✗ | ch->in_channel_idx = av_channel_layout_index_from_channel(ichl, ch->in_channel); | |
| 335 | ✗ | if (ch->in_channel_idx < 0) { | |
| 336 | ✗ | av_channel_name(inbuf, sizeof(inbuf), ch->in_channel); | |
| 337 | ✗ | av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in " | |
| 338 | "input stream #%d.\n", inbuf, | ||
| 339 | ch->input); | ||
| 340 | ✗ | ret = AVERROR(EINVAL); | |
| 341 | ✗ | goto fail; | |
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | /* make sure channels specified by index actually exist */ | ||
| 346 | ✗ | if (ch->in_channel_idx >= ichl->nb_channels) { | |
| 347 | ✗ | av_log(ctx, AV_LOG_ERROR, "Requested channel with index %d is not " | |
| 348 | "present in input stream #%d.\n", ch->in_channel_idx, ch->input); | ||
| 349 | ✗ | ret = AVERROR(EINVAL); | |
| 350 | ✗ | goto fail; | |
| 351 | } | ||
| 352 | |||
| 353 | ✗ | channel_list_pop_ch(iu, av_channel_layout_channel_from_index(ichl, ch->in_channel_idx)); | |
| 354 | } | ||
| 355 | |||
| 356 | /* guess channel maps when not explicitly defined */ | ||
| 357 | /* first try unused matching channels */ | ||
| 358 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 2 times.
|
73 | for (i = 0; i < s->ch_layout.nb_channels; i++) { |
| 359 | 71 | ChannelMap *ch = &s->channels[i]; | |
| 360 | |||
| 361 |
1/2✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
|
71 | if (ch->input < 0) |
| 362 | 71 | guess_map_matching(ctx, ch, inputs_unused); | |
| 363 | } | ||
| 364 | |||
| 365 | /* if the above failed, try to find _any_ unused input channel */ | ||
| 366 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 2 times.
|
73 | for (i = 0; i < s->ch_layout.nb_channels; i++) { |
| 367 | 71 | ChannelMap *ch = &s->channels[i]; | |
| 368 | |||
| 369 |
2/2✓ Branch 0 taken 69 times.
✓ Branch 1 taken 2 times.
|
71 | if (ch->input < 0) |
| 370 | 69 | guess_map_any(ctx, ch, inputs_unused); | |
| 371 | |||
| 372 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (ch->input < 0) { |
| 373 | ✗ | av_channel_name(outbuf, sizeof(outbuf), ch->out_channel); | |
| 374 | ✗ | av_log(ctx, AV_LOG_ERROR, "Could not find input channel for " | |
| 375 | "output channel '%s'.\n", | ||
| 376 | outbuf); | ||
| 377 | ✗ | ret = AVERROR(EINVAL); | |
| 378 | ✗ | goto fail; | |
| 379 | } | ||
| 380 | |||
| 381 |
1/2✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
|
71 | if (ch->in_channel != AV_CHAN_NONE) { |
| 382 | 71 | ch->in_channel_idx = av_channel_layout_index_from_channel( | |
| 383 | 71 | &ctx->inputs[ch->input]->ch_layout, ch->in_channel); | |
| 384 | } | ||
| 385 | |||
| 386 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | av_assert0(ch->in_channel_idx >= 0); |
| 387 | } | ||
| 388 | |||
| 389 | /* print mappings */ | ||
| 390 | 2 | av_log(ctx, AV_LOG_VERBOSE, "mappings: "); | |
| 391 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 2 times.
|
73 | for (i = 0; i < s->ch_layout.nb_channels; i++) { |
| 392 | 71 | ChannelMap *ch = &s->channels[i]; | |
| 393 | 71 | AVFilterLink *inlink = ctx->inputs[ch->input]; | |
| 394 | 71 | AVChannelLayout *ichl = &inlink->ch_layout; | |
| 395 | 71 | enum AVChannel in_ch = av_channel_layout_channel_from_index( | |
| 396 | 71 | ichl, ch->in_channel_idx); | |
| 397 | |||
| 398 | 71 | av_channel_name(inbuf, sizeof(inbuf), in_ch); | |
| 399 | 71 | av_channel_name(outbuf, sizeof(outbuf), ch->out_channel); | |
| 400 | 71 | av_log(ctx, AV_LOG_VERBOSE, "%d.%s(%d) => %s(%d) ", ch->input, | |
| 401 | inbuf, ch->in_channel_idx, | ||
| 402 | outbuf, i); | ||
| 403 | } | ||
| 404 | 2 | av_log(ctx, AV_LOG_VERBOSE, "\n"); | |
| 405 | |||
| 406 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 2 times.
|
70 | for (i = 0; i < ctx->nb_inputs; i++) { |
| 407 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
|
68 | if (inputs_unused[i].nb_ch == ctx->inputs[i]->ch_layout.nb_channels) |
| 408 | ✗ | av_log(ctx, AV_LOG_WARNING, "No channels are used from input " | |
| 409 | "stream %d.\n", i); | ||
| 410 | } | ||
| 411 | |||
| 412 | 2 | fail: | |
| 413 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 2 times.
|
70 | for (i = 0; i < ctx->nb_inputs; i++) |
| 414 | 68 | av_freep(&inputs_unused[i].ch); | |
| 415 | 2 | av_freep(&inputs_unused); | |
| 416 | 2 | return ret; | |
| 417 | } | ||
| 418 | |||
| 419 | 68 | static int try_push_frame(AVFilterContext *ctx) | |
| 420 | { | ||
| 421 | 68 | AVFilterLink *outlink = ctx->outputs[0]; | |
| 422 | 68 | JoinContext *s = ctx->priv; | |
| 423 | AVFrame *frame; | ||
| 424 | 68 | int linesize = INT_MAX; | |
| 425 | 68 | int nb_samples = INT_MAX; | |
| 426 | 68 | int nb_buffers = 0; | |
| 427 | int i, j, ret; | ||
| 428 | |||
| 429 |
2/2✓ Branch 0 taken 263 times.
✓ Branch 1 taken 67 times.
|
330 | for (i = 0; i < ctx->nb_inputs; i++) { |
| 430 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 262 times.
|
263 | if (!s->input_frames[i]) { |
| 431 | 1 | nb_samples = 0; | |
| 432 | 1 | break; | |
| 433 | } else { | ||
| 434 | 262 | nb_samples = FFMIN(nb_samples, s->input_frames[i]->nb_samples); | |
| 435 | } | ||
| 436 | } | ||
| 437 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 67 times.
|
68 | if (!nb_samples) |
| 438 | 1 | goto eof; | |
| 439 | |||
| 440 | /* setup the output frame */ | ||
| 441 | 67 | frame = av_frame_alloc(); | |
| 442 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
|
67 | if (!frame) |
| 443 | ✗ | return AVERROR(ENOMEM); | |
| 444 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 65 times.
|
67 | if (s->ch_layout.nb_channels > FF_ARRAY_ELEMS(frame->data)) { |
| 445 | 2 | frame->extended_data = av_calloc(s->ch_layout.nb_channels, | |
| 446 | sizeof(*frame->extended_data)); | ||
| 447 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!frame->extended_data) { |
| 448 | ✗ | ret = AVERROR(ENOMEM); | |
| 449 | ✗ | goto fail; | |
| 450 | } | ||
| 451 | } | ||
| 452 | |||
| 453 | /* copy the data pointers */ | ||
| 454 |
2/2✓ Branch 0 taken 457 times.
✓ Branch 1 taken 67 times.
|
524 | for (i = 0; i < s->ch_layout.nb_channels; i++) { |
| 455 | 457 | ChannelMap *ch = &s->channels[i]; | |
| 456 | 457 | AVFrame *cur = s->input_frames[ch->input]; | |
| 457 | AVBufferRef *buf; | ||
| 458 | |||
| 459 | 457 | frame->extended_data[i] = cur->extended_data[ch->in_channel_idx]; | |
| 460 | 457 | linesize = FFMIN(linesize, cur->linesize[0]); | |
| 461 | |||
| 462 | /* add the buffer where this plan is stored to the list if it's | ||
| 463 | * not already there */ | ||
| 464 | 457 | buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx); | |
| 465 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 457 times.
|
457 | if (!buf) { |
| 466 | ✗ | ret = AVERROR(EINVAL); | |
| 467 | ✗ | goto fail; | |
| 468 | } | ||
| 469 |
2/2✓ Branch 0 taken 4940 times.
✓ Branch 1 taken 457 times.
|
5397 | for (j = 0; j < nb_buffers; j++) |
| 470 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4940 times.
|
4940 | if (s->buffers[j]->buffer == buf->buffer) |
| 471 | ✗ | break; | |
| 472 |
1/2✓ Branch 0 taken 457 times.
✗ Branch 1 not taken.
|
457 | if (j == i) |
| 473 | 457 | s->buffers[nb_buffers++] = buf; | |
| 474 | } | ||
| 475 | |||
| 476 | /* create references to the buffers we copied to output */ | ||
| 477 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 65 times.
|
67 | if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) { |
| 478 | 2 | frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf); | |
| 479 | 2 | frame->extended_buf = av_calloc(frame->nb_extended_buf, | |
| 480 | sizeof(*frame->extended_buf)); | ||
| 481 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!frame->extended_buf) { |
| 482 | ✗ | frame->nb_extended_buf = 0; | |
| 483 | ✗ | ret = AVERROR(ENOMEM); | |
| 484 | ✗ | goto fail; | |
| 485 | } | ||
| 486 | } | ||
| 487 |
4/4✓ Branch 0 taken 390 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 341 times.
✓ Branch 3 taken 67 times.
|
408 | for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) { |
| 488 | 341 | frame->buf[i] = av_buffer_ref(s->buffers[i]); | |
| 489 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 341 times.
|
341 | if (!frame->buf[i]) { |
| 490 | ✗ | ret = AVERROR(ENOMEM); | |
| 491 | ✗ | goto fail; | |
| 492 | } | ||
| 493 | } | ||
| 494 |
2/2✓ Branch 0 taken 116 times.
✓ Branch 1 taken 67 times.
|
183 | for (i = 0; i < frame->nb_extended_buf; i++) { |
| 495 | 116 | frame->extended_buf[i] = av_buffer_ref(s->buffers[i + | |
| 496 | FF_ARRAY_ELEMS(frame->buf)]); | ||
| 497 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
|
116 | if (!frame->extended_buf[i]) { |
| 498 | ✗ | ret = AVERROR(ENOMEM); | |
| 499 | ✗ | goto fail; | |
| 500 | } | ||
| 501 | } | ||
| 502 | |||
| 503 | 67 | frame->nb_samples = nb_samples; | |
| 504 | 67 | frame->duration = av_rescale_q(frame->nb_samples, | |
| 505 | av_make_q(1, outlink->sample_rate), | ||
| 506 | outlink->time_base); | ||
| 507 | |||
| 508 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
|
67 | if ((ret = av_channel_layout_copy(&frame->ch_layout, &outlink->ch_layout)) < 0) |
| 509 | ✗ | goto fail; | |
| 510 | 67 | frame->sample_rate = outlink->sample_rate; | |
| 511 | 67 | frame->format = outlink->format; | |
| 512 | 67 | frame->pts = s->input_frames[0]->pts; | |
| 513 | 67 | frame->linesize[0] = linesize; | |
| 514 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 65 times.
|
67 | if (frame->data != frame->extended_data) { |
| 515 | 2 | memcpy(frame->data, frame->extended_data, sizeof(*frame->data) * | |
| 516 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | FFMIN(FF_ARRAY_ELEMS(frame->data), s->ch_layout.nb_channels)); |
| 517 | } | ||
| 518 | |||
| 519 | 67 | s->eof_pts = frame->pts + av_rescale_q(frame->nb_samples, | |
| 520 | av_make_q(1, outlink->sample_rate), | ||
| 521 | outlink->time_base); | ||
| 522 | 67 | ret = ff_filter_frame(outlink, frame); | |
| 523 | |||
| 524 |
2/2✓ Branch 0 taken 262 times.
✓ Branch 1 taken 67 times.
|
329 | for (i = 0; i < ctx->nb_inputs; i++) |
| 525 | 262 | av_frame_free(&s->input_frames[i]); | |
| 526 | |||
| 527 | 67 | return ret; | |
| 528 | |||
| 529 | ✗ | fail: | |
| 530 | ✗ | av_frame_free(&frame); | |
| 531 | ✗ | return ret; | |
| 532 | 1 | eof: | |
| 533 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | for (i = 0; i < ctx->nb_inputs; i++) { |
| 534 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | if (s->eof && |
| 535 | 1 | ff_inlink_queued_samples(ctx->inputs[i]) <= 0 && | |
| 536 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | !s->input_frames[i]) { |
| 537 | 1 | ff_outlink_set_status(outlink, AVERROR_EOF, s->eof_pts); | |
| 538 | 1 | break; | |
| 539 | } | ||
| 540 | } | ||
| 541 | |||
| 542 | 1 | return 0; | |
| 543 | } | ||
| 544 | |||
| 545 | 395 | static int activate(AVFilterContext *ctx) | |
| 546 | { | ||
| 547 | 395 | JoinContext *s = ctx->priv; | |
| 548 | int i, ret, status; | ||
| 549 | 395 | int nb_samples = 0; | |
| 550 | int64_t pts; | ||
| 551 | |||
| 552 |
4/4✓ Branch 1 taken 1 times.
✓ Branch 2 taken 394 times.
✓ Branch 4 taken 66 times.
✓ Branch 5 taken 1 times.
|
461 | FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx); |
| 553 | |||
| 554 |
2/2✓ Branch 0 taken 165 times.
✓ Branch 1 taken 229 times.
|
394 | if (!s->input_frames[0]) { |
| 555 | 165 | ret = ff_inlink_consume_frame(ctx->inputs[0], &s->input_frames[0]); | |
| 556 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
|
165 | if (ret < 0) { |
| 557 | ✗ | return ret; | |
| 558 |
4/4✓ Branch 0 taken 98 times.
✓ Branch 1 taken 67 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 97 times.
|
165 | } else if (ret == 0 && ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) { |
| 559 | 1 | s->eof |= status == AVERROR_EOF; | |
| 560 | } | ||
| 561 | |||
| 562 |
5/6✓ Branch 0 taken 164 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 97 times.
✓ Branch 3 taken 67 times.
✓ Branch 5 taken 97 times.
✗ Branch 6 not taken.
|
165 | if (!s->eof && !s->input_frames[0] && ff_outlink_frame_wanted(ctx->outputs[0])) { |
| 563 | 97 | ff_inlink_request_frame(ctx->inputs[0]); | |
| 564 | 97 | return 0; | |
| 565 | } | ||
| 566 | } | ||
| 567 | |||
| 568 |
2/2✓ Branch 0 taken 296 times.
✓ Branch 1 taken 1 times.
|
297 | if (s->input_frames[0]) |
| 569 | 296 | nb_samples = s->input_frames[0]->nb_samples; | |
| 570 | |||
| 571 |
4/4✓ Branch 0 taken 4585 times.
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 4584 times.
✓ Branch 3 taken 1 times.
|
4652 | for (i = 1; i < ctx->nb_inputs && nb_samples > 0; i++) { |
| 572 |
2/2✓ Branch 0 taken 4160 times.
✓ Branch 1 taken 424 times.
|
4584 | if (s->input_frames[i]) |
| 573 | 4160 | continue; | |
| 574 | 424 | ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->input_frames[i]); | |
| 575 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 424 times.
|
424 | if (ret < 0) { |
| 576 | ✗ | return ret; | |
| 577 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 424 times.
|
424 | } else if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) { |
| 578 | ✗ | s->eof |= status == AVERROR_EOF; | |
| 579 | } | ||
| 580 | |||
| 581 |
3/4✓ Branch 0 taken 424 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 229 times.
✓ Branch 3 taken 195 times.
|
424 | if (!s->eof && !s->input_frames[i]) { |
| 582 | 229 | ff_inlink_request_frame(ctx->inputs[i]); | |
| 583 | 229 | return 0; | |
| 584 | } | ||
| 585 | } | ||
| 586 | |||
| 587 | 68 | return try_push_frame(ctx); | |
| 588 | } | ||
| 589 | |||
| 590 | static const AVFilterPad avfilter_af_join_outputs[] = { | ||
| 591 | { | ||
| 592 | .name = "default", | ||
| 593 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 594 | .config_props = join_config_output, | ||
| 595 | }, | ||
| 596 | }; | ||
| 597 | |||
| 598 | const FFFilter ff_af_join = { | ||
| 599 | .p.name = "join", | ||
| 600 | .p.description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into " | ||
| 601 | "multi-channel output."), | ||
| 602 | .p.priv_class = &join_class, | ||
| 603 | .p.flags = AVFILTER_FLAG_DYNAMIC_INPUTS, | ||
| 604 | .priv_size = sizeof(JoinContext), | ||
| 605 | .init = join_init, | ||
| 606 | .uninit = join_uninit, | ||
| 607 | .activate = activate, | ||
| 608 | FILTER_OUTPUTS(avfilter_af_join_outputs), | ||
| 609 | FILTER_QUERY_FUNC2(join_query_formats), | ||
| 610 | }; | ||
| 611 |