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 };
uint8_t * data[8]
buffer data for each plane/channel
Definition: avfilter.h:63
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: avfilter.h:156
static void uninit(AVFilterContext *ctx)
Definition: af_amix.c:522
uint64_t in_channel
layout describing the input channel
Definition: af_channelmap.c:41
int nb_samples
number of audio samples
Definition: avfilter.h:111
AVOption.
Definition: opt.h:233
AVFilterBufferRefAudioProps * audio
audio buffer specific properties
Definition: avfilter.h:160
int linesize[8]
number of bytes per line
Definition: avfilter.h:157
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
#define A
Definition: af_join.c:78
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:505
int nb_in_buffers
Definition: af_join.c:74
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:459
struct ChannelMap ChannelMap
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
Definition: opt.c:587
void * priv
private data to be used by a custom free function
Definition: avfilter.h:83
int(* filter_frame)(AVFilterLink *link, AVFilterBufferRef *frame)
Filtering callback.
Definition: internal.h:93
static int query_formats(AVFilterContext *ctx)
Definition: af_aformat.c:121
A reference-counted buffer data type used by the filter system.
Definition: avfilter.h:62
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:44
static void join_free_buffer(AVFilterBuffer *buf)
Definition: af_join.c:398
char * map
Definition: af_join.c:49
void avfilter_unref_buffer(AVFilterBufferRef *ref)
Remove a reference to a buffer.
Definition: buffer.c:75
To avoid copying the data from input buffers, this filter creates a custom output buffer that stores ...
Definition: af_join.c:72
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
const char * name
Pad name.
Definition: internal.h:39
int nb_channels
Definition: af_join.c:53
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:426
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
AVOptions.
uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
Get the channel with the given index in channel_layout.
void avfilter_unref_bufferp(AVFilterBufferRef **ref)
Remove a reference to a buffer and set the pointer to NULL.
Definition: buffer.c:88
const char * name
void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:257
static void join_uninit(AVFilterContext *ctx)
Definition: af_join.c:245
static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch, uint64_t *inputs)
Definition: af_join.c:279
int av_get_channel_layout_channel_index(uint64_t channel_layout, uint64_t channel)
Get the index of a channel in channel_layout.
int input
input stream index
Definition: af_join.c:39
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:375
AVFilterBufferRef ** in_buffers
Definition: af_join.c:73
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
int64_t pts
presentation timestamp.
Definition: avfilter.h:167
static int join_query_formats(AVFilterContext *ctx)
Definition: af_join.c:260
A filter pad used for either input or output.
Definition: internal.h:33
AVFilterPad * input_pads
array of input pads
Definition: avfilter.h:425
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:204
AVFilterBuffer * buf
the buffer that this is a reference to
Definition: avfilter.h:140
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void * priv
private data for use by the filter
Definition: avfilter.h:439
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * av_get_channel_name(uint64_t channel)
Get the name of a given channel.
const AVClass * class
Definition: af_join.c:46
static int join_init(AVFilterContext *ctx, const char *args)
Definition: af_join.c:190
static const AVOption join_options[]
Definition: af_join.c:79
audio channel layout utility functions
unsigned nb_inputs
number of input pads
Definition: avfilter.h:430
static const AVClass join_class
Definition: af_join.c:89
AVFilterBufferRef ** input_frames
Temporary storage for input frames, until we get one on each input.
Definition: af_join.c:59
struct JoinContext JoinContext
AVFilterFormats * ff_planar_sample_fmts(void)
Construct a formats list containing all planar sample formats.
Definition: formats.c:226
LIBAVUTIL_VERSION_INT
Definition: eval.c:52
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout/...
Definition: formats.c:244
static const AVFilterPad avfilter_af_join_outputs[]
Definition: af_join.c:488
int in_channel_idx
index of in_channel in the input stream data
Definition: af_channelmap.c:43
void(* free)(struct AVFilterBuffer *buf)
A pointer to the function to deallocate this buffer if the default function is not sufficient...
Definition: avfilter.h:90
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
struct JoinBufferPriv JoinBufferPriv
To avoid copying the data from input buffers, this filter creates a custom output buffer that stores ...
NULL
Definition: eval.c:52
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:166
int perms
permissions, see the AV_PERM_* flags
Definition: avfilter.h:172
av_default_item_name
Definition: dnxhdenc.c:43
int inputs
Definition: af_join.c:48
out nb_samples
Describe the class of an AVClass context structure.
Definition: log.h:33
#define PARSE_CHANNEL(str, var, inout)
Filter definition.
Definition: avfilter.h:371
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
uint64_t out_channel
layout describing the output channel
Definition: af_channelmap.c:42
const char * name
filter name
Definition: avfilter.h:372
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: avfilter.h:79
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:433
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:238
AVFilterBufferRef * avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data, int linesize, int perms, int nb_samples, enum AVSampleFormat sample_fmt, uint64_t channel_layout)
Create an audio buffer reference wrapped around an already allocated samples buffer.
Definition: audio.c:80
ChannelMap * channels
Definition: af_join.c:54
uint8_t ** data
Temporary storage for data pointers, for assembling the output buffer.
Definition: af_join.c:64
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:363
#define OFFSET(x)
Definition: af_join.c:77
void av_opt_free(void *obj)
Free all string and binary options in obj.
Definition: opt.c:607
static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch, uint64_t *inputs)
Definition: af_join.c:297
common internal and external API header
static int parse_maps(AVFilterContext *ctx)
Definition: af_join.c:112
static int join_request_frame(AVFilterLink *outlink)
Definition: af_join.c:417
char * channel_layout_str
Definition: af_join.c:50
AVFilter avfilter_af_join
Definition: af_join.c:498
static void ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.
Definition: internal.h:169
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
An instance of a filter.
Definition: avfilter.h:418
static int join_config_output(AVFilterLink *outlink)
Definition: af_join.c:316
int needs_fifo
The filter expects a fifo to be inserted on its input link, typically because it has a delay...
Definition: internal.h:137
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:239
internal API functions
uint64_t channel_layout
Definition: af_join.c:51
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
static int filter_frame(AVFilterLink *link, AVFilterBufferRef *buf)
Definition: af_join.c:96