FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
20 /**
21  * @file
22  * Audio join filter
23  *
24  * Join multiple audio inputs as different channels in
25  * a single output
26  */
27 
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; ///< input stream index
40  int in_channel_idx; ///< index of in_channel in the input stream data
41  uint64_t in_channel; ///< layout describing the input channel
42  uint64_t out_channel; ///< layout describing the output 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 
56  /**
57  * Temporary storage for input frames, until we get one on each input.
58  */
60 
61  /**
62  * Temporary storage for buffer references, for assembling the output frame.
63  */
65 } JoinContext;
66 
67 #define OFFSET(x) offsetof(JoinContext, x)
68 #define A AV_OPT_FLAG_AUDIO_PARAM
69 #define F AV_OPT_FLAG_FILTERING_PARAM
70 static const AVOption join_options[] = {
71  { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A|F },
72  { "channel_layout", "Channel layout of the "
73  "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A|F },
74  { "map", "A comma-separated list of channels maps in the format "
75  "'input_stream.input_channel-output_channel.",
76  OFFSET(map), AV_OPT_TYPE_STRING, .flags = A|F },
77  { NULL },
78 };
79 
80 static const AVClass join_class = {
81  .class_name = "join filter",
82  .item_name = av_default_item_name,
83  .option = join_options,
84  .version = LIBAVUTIL_VERSION_INT,
85 };
86 
88 {
89  AVFilterContext *ctx = link->dst;
90  JoinContext *s = ctx->priv;
91  int i;
92 
93  for (i = 0; i < ctx->nb_inputs; i++)
94  if (link == ctx->inputs[i])
95  break;
96  av_assert0(i < ctx->nb_inputs);
97  av_assert0(!s->input_frames[i]);
98  s->input_frames[i] = frame;
99 
100  return 0;
101 }
102 
103 static int parse_maps(AVFilterContext *ctx)
104 {
105  JoinContext *s = ctx->priv;
106  char separator = '|';
107  char *cur = s->map;
108 
109 #if FF_API_OLD_FILTER_OPTS
110  if (cur && strchr(cur, ',')) {
111  av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "
112  "separate the mappings.\n");
113  separator = ',';
114  }
115 #endif
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, separator);
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 
191 {
192  JoinContext *s = ctx->priv;
193  int ret, i;
194 
196  av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
197  s->channel_layout_str);
198  ret = AVERROR(EINVAL);
199  goto fail;
200  }
201 
203  s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
204  s->buffers = av_mallocz(sizeof(*s->buffers) * s->nb_channels);
205  s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
206  if (!s->channels || !s->buffers|| !s->input_frames) {
207  ret = AVERROR(ENOMEM);
208  goto fail;
209  }
210 
211  for (i = 0; i < s->nb_channels; i++) {
213  s->channels[i].input = -1;
214  }
215 
216  if ((ret = parse_maps(ctx)) < 0)
217  goto fail;
218 
219  for (i = 0; i < s->inputs; i++) {
220  char name[32];
221  AVFilterPad pad = { 0 };
222 
223  snprintf(name, sizeof(name), "input%d", i);
224  pad.type = AVMEDIA_TYPE_AUDIO;
225  pad.name = av_strdup(name);
227 
228  pad.needs_fifo = 1;
229 
230  ff_insert_inpad(ctx, i, &pad);
231  }
232 
233 fail:
234  av_opt_free(s);
235  return ret;
236 }
237 
239 {
240  JoinContext *s = ctx->priv;
241  int i;
242 
243  for (i = 0; i < ctx->nb_inputs; i++) {
244  av_freep(&ctx->input_pads[i].name);
245  av_frame_free(&s->input_frames[i]);
246  }
247 
248  av_freep(&s->channels);
249  av_freep(&s->buffers);
250  av_freep(&s->input_frames);
251 }
252 
254 {
255  JoinContext *s = ctx->priv;
257  int i;
258 
261 
262  for (i = 0; i < ctx->nb_inputs; i++)
264  &ctx->inputs[i]->out_channel_layouts);
265 
268 
269  return 0;
270 }
271 
273  uint64_t *inputs)
274 {
275  int i;
276 
277  for (i = 0; i < ctx->nb_inputs; i++) {
278  AVFilterLink *link = ctx->inputs[i];
279 
280  if (ch->out_channel & link->channel_layout &&
281  !(ch->out_channel & inputs[i])) {
282  ch->input = i;
283  ch->in_channel = ch->out_channel;
284  inputs[i] |= ch->out_channel;
285  return;
286  }
287  }
288 }
289 
291  uint64_t *inputs)
292 {
293  int i;
294 
295  for (i = 0; i < ctx->nb_inputs; i++) {
296  AVFilterLink *link = ctx->inputs[i];
297 
298  if ((inputs[i] & link->channel_layout) != link->channel_layout) {
299  uint64_t unused = link->channel_layout & ~inputs[i];
300 
301  ch->input = i;
303  inputs[i] |= ch->in_channel;
304  return;
305  }
306  }
307 }
308 
309 static int join_config_output(AVFilterLink *outlink)
310 {
311  AVFilterContext *ctx = outlink->src;
312  JoinContext *s = ctx->priv;
313  uint64_t *inputs; // nth element tracks which channels are used from nth input
314  int i, ret = 0;
315 
316  /* initialize inputs to user-specified mappings */
317  if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
318  return AVERROR(ENOMEM);
319  for (i = 0; i < s->nb_channels; i++) {
320  ChannelMap *ch = &s->channels[i];
321  AVFilterLink *inlink;
322 
323  if (ch->input < 0)
324  continue;
325 
326  inlink = ctx->inputs[ch->input];
327 
328  if (!ch->in_channel)
330  ch->in_channel_idx);
331 
332  if (!(ch->in_channel & inlink->channel_layout)) {
333  av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
334  "input stream #%d.\n", av_get_channel_name(ch->in_channel),
335  ch->input);
336  ret = AVERROR(EINVAL);
337  goto fail;
338  }
339 
340  inputs[ch->input] |= ch->in_channel;
341  }
342 
343  /* guess channel maps when not explicitly defined */
344  /* first try unused matching channels */
345  for (i = 0; i < s->nb_channels; i++) {
346  ChannelMap *ch = &s->channels[i];
347 
348  if (ch->input < 0)
349  guess_map_matching(ctx, ch, inputs);
350  }
351 
352  /* if the above failed, try to find _any_ unused input channel */
353  for (i = 0; i < s->nb_channels; i++) {
354  ChannelMap *ch = &s->channels[i];
355 
356  if (ch->input < 0)
357  guess_map_any(ctx, ch, inputs);
358 
359  if (ch->input < 0) {
360  av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
361  "output channel '%s'.\n",
363  goto fail;
364  }
365 
367  ch->in_channel);
368  }
369 
370  /* print mappings */
371  av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
372  for (i = 0; i < s->nb_channels; i++) {
373  ChannelMap *ch = &s->channels[i];
374  av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
377  }
378  av_log(ctx, AV_LOG_VERBOSE, "\n");
379 
380  for (i = 0; i < ctx->nb_inputs; i++) {
381  if (!inputs[i])
382  av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
383  "stream %d.\n", i);
384  }
385 
386 fail:
387  av_freep(&inputs);
388  return ret;
389 }
390 
391 static int join_request_frame(AVFilterLink *outlink)
392 {
393  AVFilterContext *ctx = outlink->src;
394  JoinContext *s = ctx->priv;
395  AVFrame *frame;
396  int linesize = INT_MAX;
397  int nb_samples = 0;
398  int nb_buffers = 0;
399  int i, j, ret;
400 
401  /* get a frame on each input */
402  for (i = 0; i < ctx->nb_inputs; i++) {
403  AVFilterLink *inlink = ctx->inputs[i];
404 
405  if (!s->input_frames[i] &&
406  (ret = ff_request_frame(inlink)) < 0)
407  return ret;
408 
409  /* request the same number of samples on all inputs */
410  if (i == 0) {
411  nb_samples = s->input_frames[0]->nb_samples;
412 
413  for (j = 1; !i && j < ctx->nb_inputs; j++)
414  ctx->inputs[j]->request_samples = nb_samples;
415  }
416  }
417 
418  /* setup the output frame */
419  frame = av_frame_alloc();
420  if (!frame)
421  return AVERROR(ENOMEM);
422  if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
423  frame->extended_data = av_mallocz(s->nb_channels *
424  sizeof(*frame->extended_data));
425  if (!frame->extended_data) {
426  ret = AVERROR(ENOMEM);
427  goto fail;
428  }
429  }
430 
431  /* copy the data pointers */
432  for (i = 0; i < s->nb_channels; i++) {
433  ChannelMap *ch = &s->channels[i];
434  AVFrame *cur = s->input_frames[ch->input];
435  AVBufferRef *buf;
436 
437  frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
438  linesize = FFMIN(linesize, cur->linesize[0]);
439 
440  /* add the buffer where this plan is stored to the list if it's
441  * not already there */
443  if (!buf) {
444  ret = AVERROR(EINVAL);
445  goto fail;
446  }
447  for (j = 0; j < nb_buffers; j++)
448  if (s->buffers[j]->buffer == buf->buffer)
449  break;
450  if (j == i)
451  s->buffers[nb_buffers++] = buf;
452  }
453 
454  /* create references to the buffers we copied to output */
455  if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
456  frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
457  frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
458  frame->nb_extended_buf);
459  if (!frame->extended_buf) {
460  frame->nb_extended_buf = 0;
461  ret = AVERROR(ENOMEM);
462  goto fail;
463  }
464  }
465  for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
466  frame->buf[i] = av_buffer_ref(s->buffers[i]);
467  if (!frame->buf[i]) {
468  ret = AVERROR(ENOMEM);
469  goto fail;
470  }
471  }
472  for (i = 0; i < frame->nb_extended_buf; i++) {
473  frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
474  FF_ARRAY_ELEMS(frame->buf)]);
475  if (!frame->extended_buf[i]) {
476  ret = AVERROR(ENOMEM);
477  goto fail;
478  }
479  }
480 
481  frame->nb_samples = nb_samples;
482  frame->channel_layout = outlink->channel_layout;
483  av_frame_set_channels(frame, outlink->channels);
484  frame->format = outlink->format;
485  frame->sample_rate = outlink->sample_rate;
486  frame->pts = s->input_frames[0]->pts;
487  frame->linesize[0] = linesize;
488  if (frame->data != frame->extended_data) {
489  memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
490  FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
491  }
492 
493  ret = ff_filter_frame(outlink, frame);
494 
495  for (i = 0; i < ctx->nb_inputs; i++)
496  av_frame_free(&s->input_frames[i]);
497 
498  return ret;
499 
500 fail:
501  av_frame_free(&frame);
502  return ret;
503 }
504 
506  {
507  .name = "default",
508  .type = AVMEDIA_TYPE_AUDIO,
509  .config_props = join_config_output,
510  .request_frame = join_request_frame,
511  },
512  { NULL }
513 };
514 
516  .name = "join",
517  .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
518  "multi-channel output."),
519  .priv_size = sizeof(JoinContext),
520  .priv_class = &join_class,
521 
522  .init = join_init,
523  .uninit = join_uninit,
525 
526  .inputs = NULL,
527  .outputs = avfilter_af_join_outputs,
528 
530 };