af_join.c
Go to the documentation of this file.
1 /*
2  *
3  * This file is part of Libav.
4  *
5  * Libav is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * Libav is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with Libav; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
28 #include "libavutil/avassert.h"
30 #include "libavutil/common.h"
31 #include "libavutil/opt.h"
32 
33 #include "audio.h"
34 #include "avfilter.h"
35 #include "formats.h"
36 #include "internal.h"
37 
38 typedef struct ChannelMap {
39  int input;
40  int in_channel_idx;
41  uint64_t in_channel;
42  uint64_t out_channel;
43 } ChannelMap;
44 
45 typedef struct JoinContext {
46  const AVClass *class;
47 
48  int inputs;
49  char *map;
51  uint64_t channel_layout;
52 
55 
60 
65 } JoinContext;
66 
72 typedef struct JoinBufferPriv {
76 
77 #define OFFSET(x) offsetof(JoinContext, x)
78 #define A AV_OPT_FLAG_AUDIO_PARAM
79 static const AVOption join_options[] = {
80  { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A },
81  { "channel_layout", "Channel layout of the "
82  "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A },
83  { "map", "A comma-separated list of channels maps in the format "
84  "'input_stream.input_channel-output_channel.",
85  OFFSET(map), AV_OPT_TYPE_STRING, .flags = A },
86  { NULL },
87 };
88 
89 static const AVClass join_class = {
90  .class_name = "join filter",
91  .item_name = av_default_item_name,
92  .option = join_options,
93  .version = LIBAVUTIL_VERSION_INT,
94 };
95 
97 {
98  AVFilterContext *ctx = link->dst;
99  JoinContext *s = ctx->priv;
100  int i;
101 
102  for (i = 0; i < ctx->nb_inputs; i++)
103  if (link == ctx->inputs[i])
104  break;
105  av_assert0(i < ctx->nb_inputs);
106  av_assert0(!s->input_frames[i]);
107  s->input_frames[i] = buf;
108 
109  return 0;
110 }
111 
112 static int parse_maps(AVFilterContext *ctx)
113 {
114  JoinContext *s = ctx->priv;
115  char *cur = s->map;
116 
117  while (cur && *cur) {
118  char *sep, *next, *p;
119  uint64_t in_channel = 0, out_channel = 0;
120  int input_idx, out_ch_idx, in_ch_idx;
121 
122  next = strchr(cur, ',');
123  if (next)
124  *next++ = 0;
125 
126  /* split the map into input and output parts */
127  if (!(sep = strchr(cur, '-'))) {
128  av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
129  "map '%s'\n", cur);
130  return AVERROR(EINVAL);
131  }
132  *sep++ = 0;
133 
134 #define PARSE_CHANNEL(str, var, inout) \
135  if (!(var = av_get_channel_layout(str))) { \
136  av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
137  return AVERROR(EINVAL); \
138  } \
139  if (av_get_channel_layout_nb_channels(var) != 1) { \
140  av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
141  inout " channel.\n"); \
142  return AVERROR(EINVAL); \
143  }
144 
145  /* parse output channel */
146  PARSE_CHANNEL(sep, out_channel, "output");
147  if (!(out_channel & s->channel_layout)) {
148  av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
149  "requested channel layout.\n", sep);
150  return AVERROR(EINVAL);
151  }
152 
154  out_channel);
155  if (s->channels[out_ch_idx].input >= 0) {
156  av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
157  "'%s'.\n", sep);
158  return AVERROR(EINVAL);
159  }
160 
161  /* parse input channel */
162  input_idx = strtol(cur, &cur, 0);
163  if (input_idx < 0 || input_idx >= s->inputs) {
164  av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
165  input_idx);
166  return AVERROR(EINVAL);
167  }
168 
169  if (*cur)
170  cur++;
171 
172  in_ch_idx = strtol(cur, &p, 0);
173  if (p == cur) {
174  /* channel specifier is not a number,
175  * try to parse as channel name */
176  PARSE_CHANNEL(cur, in_channel, "input");
177  }
178 
179  s->channels[out_ch_idx].input = input_idx;
180  if (in_channel)
181  s->channels[out_ch_idx].in_channel = in_channel;
182  else
183  s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
184 
185  cur = next;
186  }
187  return 0;
188 }
189 
190 static int join_init(AVFilterContext *ctx, const char *args)
191 {
192  JoinContext *s = ctx->priv;
193  int ret, i;
194 
195  s->class = &join_class;
197  if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
198  av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
199  return ret;
200  }
201 
203  av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
204  s->channel_layout_str);
205  ret = AVERROR(EINVAL);
206  goto fail;
207  }
208 
210  s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
211  s->data = av_mallocz(sizeof(*s->data) * s->nb_channels);
212  s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
213  if (!s->channels || !s->data || !s->input_frames) {
214  ret = AVERROR(ENOMEM);
215  goto fail;
216  }
217 
218  for (i = 0; i < s->nb_channels; i++) {
220  s->channels[i].input = -1;
221  }
222 
223  if ((ret = parse_maps(ctx)) < 0)
224  goto fail;
225 
226  for (i = 0; i < s->inputs; i++) {
227  char name[32];
228  AVFilterPad pad = { 0 };
229 
230  snprintf(name, sizeof(name), "input%d", i);
231  pad.type = AVMEDIA_TYPE_AUDIO;
232  pad.name = av_strdup(name);
234 
235  pad.needs_fifo = 1;
236 
237  ff_insert_inpad(ctx, i, &pad);
238  }
239 
240 fail:
241  av_opt_free(s);
242  return ret;
243 }
244 
245 static void join_uninit(AVFilterContext *ctx)
246 {
247  JoinContext *s = ctx->priv;
248  int i;
249 
250  for (i = 0; i < ctx->nb_inputs; i++) {
251  av_freep(&ctx->input_pads[i].name);
253  }
254 
255  av_freep(&s->channels);
256  av_freep(&s->data);
257  av_freep(&s->input_frames);
258 }
259 
261 {
262  JoinContext *s = ctx->priv;
264  int i;
265 
268 
269  for (i = 0; i < ctx->nb_inputs; i++)
271  &ctx->inputs[i]->out_channel_layouts);
272 
275 
276  return 0;
277 }
278 
280  uint64_t *inputs)
281 {
282  int i;
283 
284  for (i = 0; i < ctx->nb_inputs; i++) {
285  AVFilterLink *link = ctx->inputs[i];
286 
287  if (ch->out_channel & link->channel_layout &&
288  !(ch->out_channel & inputs[i])) {
289  ch->input = i;
290  ch->in_channel = ch->out_channel;
291  inputs[i] |= ch->out_channel;
292  return;
293  }
294  }
295 }
296 
298  uint64_t *inputs)
299 {
300  int i;
301 
302  for (i = 0; i < ctx->nb_inputs; i++) {
303  AVFilterLink *link = ctx->inputs[i];
304 
305  if ((inputs[i] & link->channel_layout) != link->channel_layout) {
306  uint64_t unused = link->channel_layout & ~inputs[i];
307 
308  ch->input = i;
310  inputs[i] |= ch->in_channel;
311  return;
312  }
313  }
314 }
315 
316 static int join_config_output(AVFilterLink *outlink)
317 {
318  AVFilterContext *ctx = outlink->src;
319  JoinContext *s = ctx->priv;
320  uint64_t *inputs; // nth element tracks which channels are used from nth input
321  int i, ret = 0;
322 
323  /* initialize inputs to user-specified mappings */
324  if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
325  return AVERROR(ENOMEM);
326  for (i = 0; i < s->nb_channels; i++) {
327  ChannelMap *ch = &s->channels[i];
328  AVFilterLink *inlink;
329 
330  if (ch->input < 0)
331  continue;
332 
333  inlink = ctx->inputs[ch->input];
334 
335  if (!ch->in_channel)
337  ch->in_channel_idx);
338 
339  if (!(ch->in_channel & inlink->channel_layout)) {
340  av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
341  "input stream #%d.\n", av_get_channel_name(ch->in_channel),
342  ch->input);
343  ret = AVERROR(EINVAL);
344  goto fail;
345  }
346 
347  inputs[ch->input] |= ch->in_channel;
348  }
349 
350  /* guess channel maps when not explicitly defined */
351  /* first try unused matching channels */
352  for (i = 0; i < s->nb_channels; i++) {
353  ChannelMap *ch = &s->channels[i];
354 
355  if (ch->input < 0)
356  guess_map_matching(ctx, ch, inputs);
357  }
358 
359  /* if the above failed, try to find _any_ unused input channel */
360  for (i = 0; i < s->nb_channels; i++) {
361  ChannelMap *ch = &s->channels[i];
362 
363  if (ch->input < 0)
364  guess_map_any(ctx, ch, inputs);
365 
366  if (ch->input < 0) {
367  av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
368  "output channel '%s'.\n",
370  goto fail;
371  }
372 
374  ch->in_channel);
375  }
376 
377  /* print mappings */
378  av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
379  for (i = 0; i < s->nb_channels; i++) {
380  ChannelMap *ch = &s->channels[i];
381  av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
384  }
385  av_log(ctx, AV_LOG_VERBOSE, "\n");
386 
387  for (i = 0; i < ctx->nb_inputs; i++) {
388  if (!inputs[i])
389  av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
390  "stream %d.\n", i);
391  }
392 
393 fail:
394  av_freep(&inputs);
395  return ret;
396 }
397 
399 {
400  JoinBufferPriv *priv = buf->priv;
401 
402  if (priv) {
403  int i;
404 
405  for (i = 0; i < priv->nb_in_buffers; i++)
407 
408  av_freep(&priv->in_buffers);
409  av_freep(&buf->priv);
410  }
411 
412  if (buf->extended_data != buf->data)
413  av_freep(&buf->extended_data);
414  av_freep(&buf);
415 }
416 
417 static int join_request_frame(AVFilterLink *outlink)
418 {
419  AVFilterContext *ctx = outlink->src;
420  JoinContext *s = ctx->priv;
421  AVFilterBufferRef *buf;
422  JoinBufferPriv *priv;
423  int linesize = INT_MAX;
424  int perms = ~0;
425  int nb_samples = 0;
426  int i, j, ret;
427 
428  /* get a frame on each input */
429  for (i = 0; i < ctx->nb_inputs; i++) {
430  AVFilterLink *inlink = ctx->inputs[i];
431 
432  if (!s->input_frames[i] &&
433  (ret = ff_request_frame(inlink)) < 0)
434  return ret;
435 
436  /* request the same number of samples on all inputs */
437  if (i == 0) {
438  nb_samples = s->input_frames[0]->audio->nb_samples;
439 
440  for (j = 1; !i && j < ctx->nb_inputs; j++)
441  ctx->inputs[j]->request_samples = nb_samples;
442  }
443  }
444 
445  for (i = 0; i < s->nb_channels; i++) {
446  ChannelMap *ch = &s->channels[i];
447  AVFilterBufferRef *cur_buf = s->input_frames[ch->input];
448 
449  s->data[i] = cur_buf->extended_data[ch->in_channel_idx];
450  linesize = FFMIN(linesize, cur_buf->linesize[0]);
451  perms &= cur_buf->perms;
452  }
453 
454  av_assert0(nb_samples > 0);
455  buf = avfilter_get_audio_buffer_ref_from_arrays(s->data, linesize, perms,
456  nb_samples, outlink->format,
457  outlink->channel_layout);
458  if (!buf)
459  return AVERROR(ENOMEM);
460 
461  buf->buf->free = join_free_buffer;
462  buf->pts = s->input_frames[0]->pts;
463 
464  if (!(priv = av_mallocz(sizeof(*priv))))
465  goto fail;
466  if (!(priv->in_buffers = av_mallocz(sizeof(*priv->in_buffers) * ctx->nb_inputs)))
467  goto fail;
468 
469  for (i = 0; i < ctx->nb_inputs; i++)
470  priv->in_buffers[i] = s->input_frames[i];
471  priv->nb_in_buffers = ctx->nb_inputs;
472  buf->buf->priv = priv;
473 
474  ret = ff_filter_frame(outlink, buf);
475 
476  memset(s->input_frames, 0, sizeof(*s->input_frames) * ctx->nb_inputs);
477 
478  return ret;
479 
480 fail:
482  if (priv)
483  av_freep(&priv->in_buffers);
484  av_freep(&priv);
485  return AVERROR(ENOMEM);
486 }
487 
489  {
490  .name = "default",
491  .type = AVMEDIA_TYPE_AUDIO,
492  .config_props = join_config_output,
493  .request_frame = join_request_frame,
494  },
495  { NULL }
496 };
497 
499  .name = "join",
500  .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
501  "multi-channel output"),
502  .priv_size = sizeof(JoinContext),
503 
504  .init = join_init,
505  .uninit = join_uninit,
507 
508  .inputs = NULL,
509  .outputs = avfilter_af_join_outputs,
510 };