FFmpeg  4.0
af_amerge.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Nicolas George <nicolas.george@normalesup.org>
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
14  * GNU 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 merging filter
24  */
25 
26 #define FF_INTERNAL_FIELDS 1
27 #include "framequeue.h"
28 
29 #include "libavutil/avstring.h"
30 #include "libavutil/bprint.h"
32 #include "libavutil/opt.h"
33 #include "avfilter.h"
34 #include "audio.h"
35 #include "bufferqueue.h"
36 #include "internal.h"
37 
38 #define SWR_CH_MAX 64
39 
40 typedef struct AMergeContext {
41  const AVClass *class;
42  int nb_inputs;
43  int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
44  int bps;
45  struct amerge_input {
46  struct FFBufQueue queue;
47  int nb_ch; /**< number of channels for the input */
49  int pos;
50  } *in;
52 
53 #define OFFSET(x) offsetof(AMergeContext, x)
54 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
55 
56 static const AVOption amerge_options[] = {
57  { "inputs", "specify the number of inputs", OFFSET(nb_inputs),
58  AV_OPT_TYPE_INT, { .i64 = 2 }, 1, SWR_CH_MAX, FLAGS },
59  { NULL }
60 };
61 
62 AVFILTER_DEFINE_CLASS(amerge);
63 
65 {
66  AMergeContext *s = ctx->priv;
67  int i;
68 
69  for (i = 0; i < s->nb_inputs; i++) {
70  if (s->in)
72  if (ctx->input_pads)
73  av_freep(&ctx->input_pads[i].name);
74  }
75  av_freep(&s->in);
76 }
77 
79 {
80  AMergeContext *s = ctx->priv;
81  int64_t inlayout[SWR_CH_MAX], outlayout = 0;
84  int i, ret, overlap = 0, nb_ch = 0;
85 
86  for (i = 0; i < s->nb_inputs; i++) {
87  if (!ctx->inputs[i]->in_channel_layouts ||
90  "No channel layout for input %d\n", i + 1);
91  return AVERROR(EAGAIN);
92  }
93  inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
94  if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
95  char buf[256];
96  av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
97  av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
98  }
99  s->in[i].nb_ch = FF_LAYOUT2COUNT(inlayout[i]);
100  if (s->in[i].nb_ch) {
101  overlap++;
102  } else {
103  s->in[i].nb_ch = av_get_channel_layout_nb_channels(inlayout[i]);
104  if (outlayout & inlayout[i])
105  overlap++;
106  outlayout |= inlayout[i];
107  }
108  nb_ch += s->in[i].nb_ch;
109  }
110  if (nb_ch > SWR_CH_MAX) {
111  av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
112  return AVERROR(EINVAL);
113  }
114  if (overlap) {
115  av_log(ctx, AV_LOG_WARNING,
116  "Input channel layouts overlap: "
117  "output layout will be determined by the number of distinct input channels\n");
118  for (i = 0; i < nb_ch; i++)
119  s->route[i] = i;
120  outlayout = av_get_default_channel_layout(nb_ch);
121  if (!outlayout && nb_ch)
122  outlayout = 0xFFFFFFFFFFFFFFFFULL >> (64 - nb_ch);
123  } else {
124  int *route[SWR_CH_MAX];
125  int c, out_ch_number = 0;
126 
127  route[0] = s->route;
128  for (i = 1; i < s->nb_inputs; i++)
129  route[i] = route[i - 1] + s->in[i - 1].nb_ch;
130  for (c = 0; c < 64; c++)
131  for (i = 0; i < s->nb_inputs; i++)
132  if ((inlayout[i] >> c) & 1)
133  *(route[i]++) = out_ch_number++;
134  }
136  if ((ret = ff_set_common_formats(ctx, formats)) < 0)
137  return ret;
138  for (i = 0; i < s->nb_inputs; i++) {
139  layouts = NULL;
140  if ((ret = ff_add_channel_layout(&layouts, inlayout[i])) < 0)
141  return ret;
142  if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
143  return ret;
144  }
145  layouts = NULL;
146  if ((ret = ff_add_channel_layout(&layouts, outlayout)) < 0)
147  return ret;
148  if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
149  return ret;
150 
152 }
153 
154 static int config_output(AVFilterLink *outlink)
155 {
156  AVFilterContext *ctx = outlink->src;
157  AMergeContext *s = ctx->priv;
158  AVBPrint bp;
159  int i;
160 
161  for (i = 1; i < s->nb_inputs; i++) {
162  if (ctx->inputs[i]->sample_rate != ctx->inputs[0]->sample_rate) {
163  av_log(ctx, AV_LOG_ERROR,
164  "Inputs must have the same sample rate "
165  "%d for in%d vs %d\n",
166  ctx->inputs[i]->sample_rate, i, ctx->inputs[0]->sample_rate);
167  return AVERROR(EINVAL);
168  }
169  }
170  s->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
171  outlink->sample_rate = ctx->inputs[0]->sample_rate;
172  outlink->time_base = ctx->inputs[0]->time_base;
173 
174  av_bprint_init(&bp, 0, 1);
175  for (i = 0; i < s->nb_inputs; i++) {
176  av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
178  }
179  av_bprintf(&bp, " -> out:");
181  av_log(ctx, AV_LOG_VERBOSE, "%s\n", bp.str);
182 
183  return 0;
184 }
185 
186 static int request_frame(AVFilterLink *outlink)
187 {
188  AVFilterContext *ctx = outlink->src;
189  AMergeContext *s = ctx->priv;
190  int i, ret;
191 
192  for (i = 0; i < s->nb_inputs; i++)
193  if (!s->in[i].nb_samples ||
194  /* detect EOF immediately */
195  (ctx->inputs[i]->status_in && !ctx->inputs[i]->status_out))
196  if ((ret = ff_request_frame(ctx->inputs[i])) < 0)
197  return ret;
198  return 0;
199 }
200 
201 /**
202  * Copy samples from several input streams to one output stream.
203  * @param nb_inputs number of inputs
204  * @param in inputs; used only for the nb_ch field;
205  * @param route routing values;
206  * input channel i goes to output channel route[i];
207  * i < in[0].nb_ch are the channels from the first output;
208  * i >= in[0].nb_ch are the channels from the second output
209  * @param ins pointer to the samples of each inputs, in packed format;
210  * will be left at the end of the copied samples
211  * @param outs pointer to the samples of the output, in packet format;
212  * must point to a buffer big enough;
213  * will be left at the end of the copied samples
214  * @param ns number of samples to copy
215  * @param bps bytes per sample
216  */
217 static inline void copy_samples(int nb_inputs, struct amerge_input in[],
218  int *route, uint8_t *ins[],
219  uint8_t **outs, int ns, int bps)
220 {
221  int *route_cur;
222  int i, c, nb_ch = 0;
223 
224  for (i = 0; i < nb_inputs; i++)
225  nb_ch += in[i].nb_ch;
226  while (ns--) {
227  route_cur = route;
228  for (i = 0; i < nb_inputs; i++) {
229  for (c = 0; c < in[i].nb_ch; c++) {
230  memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
231  ins[i] += bps;
232  }
233  }
234  *outs += nb_ch * bps;
235  }
236 }
237 
238 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
239 {
240  AVFilterContext *ctx = inlink->dst;
241  AMergeContext *s = ctx->priv;
242  AVFilterLink *const outlink = ctx->outputs[0];
243  int input_number;
244  int nb_samples, ns, i;
245  AVFrame *outbuf, *inbuf[SWR_CH_MAX];
246  uint8_t *ins[SWR_CH_MAX], *outs;
247 
248  for (input_number = 0; input_number < s->nb_inputs; input_number++)
249  if (inlink == ctx->inputs[input_number])
250  break;
251  av_assert1(input_number < s->nb_inputs);
252  if (ff_bufqueue_is_full(&s->in[input_number].queue)) {
253  av_frame_free(&insamples);
254  return AVERROR(ENOMEM);
255  }
256  ff_bufqueue_add(ctx, &s->in[input_number].queue, av_frame_clone(insamples));
257  s->in[input_number].nb_samples += insamples->nb_samples;
258  av_frame_free(&insamples);
259  nb_samples = s->in[0].nb_samples;
260  for (i = 1; i < s->nb_inputs; i++)
261  nb_samples = FFMIN(nb_samples, s->in[i].nb_samples);
262  if (!nb_samples)
263  return 0;
264 
265  outbuf = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
266  if (!outbuf)
267  return AVERROR(ENOMEM);
268  outs = outbuf->data[0];
269  for (i = 0; i < s->nb_inputs; i++) {
270  inbuf[i] = ff_bufqueue_peek(&s->in[i].queue, 0);
271  ins[i] = inbuf[i]->data[0] +
272  s->in[i].pos * s->in[i].nb_ch * s->bps;
273  }
274  av_frame_copy_props(outbuf, inbuf[0]);
275  outbuf->pts = inbuf[0]->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
276  inbuf[0]->pts +
277  av_rescale_q(s->in[0].pos,
278  av_make_q(1, ctx->inputs[0]->sample_rate),
279  ctx->outputs[0]->time_base);
280 
281  outbuf->nb_samples = nb_samples;
282  outbuf->channel_layout = outlink->channel_layout;
283  outbuf->channels = outlink->channels;
284 
285  while (nb_samples) {
286  ns = nb_samples;
287  for (i = 0; i < s->nb_inputs; i++)
288  ns = FFMIN(ns, inbuf[i]->nb_samples - s->in[i].pos);
289  /* Unroll the most common sample formats: speed +~350% for the loop,
290  +~13% overall (including two common decoders) */
291  switch (s->bps) {
292  case 1:
293  copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, ns, 1);
294  break;
295  case 2:
296  copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, ns, 2);
297  break;
298  case 4:
299  copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, ns, 4);
300  break;
301  default:
302  copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, ns, s->bps);
303  break;
304  }
305 
306  nb_samples -= ns;
307  for (i = 0; i < s->nb_inputs; i++) {
308  s->in[i].nb_samples -= ns;
309  s->in[i].pos += ns;
310  if (s->in[i].pos == inbuf[i]->nb_samples) {
311  s->in[i].pos = 0;
312  av_frame_free(&inbuf[i]);
313  ff_bufqueue_get(&s->in[i].queue);
314  inbuf[i] = ff_bufqueue_peek(&s->in[i].queue, 0);
315  ins[i] = inbuf[i] ? inbuf[i]->data[0] : NULL;
316  }
317  }
318  }
319  return ff_filter_frame(ctx->outputs[0], outbuf);
320 }
321 
323 {
324  AMergeContext *s = ctx->priv;
325  int i, ret;
326 
327  s->in = av_calloc(s->nb_inputs, sizeof(*s->in));
328  if (!s->in)
329  return AVERROR(ENOMEM);
330  for (i = 0; i < s->nb_inputs; i++) {
331  char *name = av_asprintf("in%d", i);
332  AVFilterPad pad = {
333  .name = name,
334  .type = AVMEDIA_TYPE_AUDIO,
335  .filter_frame = filter_frame,
336  };
337  if (!name)
338  return AVERROR(ENOMEM);
339  if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
340  av_freep(&pad.name);
341  return ret;
342  }
343  }
344  return 0;
345 }
346 
347 static const AVFilterPad amerge_outputs[] = {
348  {
349  .name = "default",
350  .type = AVMEDIA_TYPE_AUDIO,
351  .config_props = config_output,
352  .request_frame = request_frame,
353  },
354  { NULL }
355 };
356 
358  .name = "amerge",
359  .description = NULL_IF_CONFIG_SMALL("Merge two or more audio streams into "
360  "a single multi-channel stream."),
361  .priv_size = sizeof(AMergeContext),
362  .init = init,
363  .uninit = uninit,
365  .inputs = NULL,
366  .outputs = amerge_outputs,
367  .priv_class = &amerge_class,
369 };
const char * name
Definition: avisynth_c.h:775
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
Definition: bufferqueue.h:98
struct AMergeContext::amerge_input * in
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
int nb_ch
number of channels for the input
Definition: af_amerge.c:47
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
AVOption.
Definition: opt.h:246
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Main libavfilter public API header.
#define SWR_CH_MAX
Definition: af_amerge.c:38
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:105
struct FFBufQueue queue
Definition: af_amerge.c:46
AVFILTER_DEFINE_CLASS(amerge)
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:244
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Structure holding the queue.
Definition: bufferqueue.h:49
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:435
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
#define FF_LAYOUT2COUNT(l)
Decode a channel count encoded as a channel layout.
Definition: formats.h:108
static enum AVSampleFormat ff_packed_sample_fmts_array[]
Definition: audio.h:28
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:311
static int flags
Definition: log.c:55
static av_cold int init(AVFilterContext *ctx)
Definition: af_amerge.c:322
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define av_log(a,...)
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
Definition: af_amerge.c:238
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_amerge.c:64
A filter pad used for either input or output.
Definition: internal.h:54
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
static const AVFilterPad amerge_outputs[]
Definition: af_amerge.c:347
AVFilterPad * input_pads
array of input pads
Definition: avfilter.h:345
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int 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:568
static const AVOption amerge_options[]
Definition: af_amerge.c:56
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:343
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
void * priv
private data for use by the filter
Definition: avfilter.h:353
uint64_t * channel_layouts
list of channel layouts
Definition: formats.h:86
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:396
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
static int request_frame(AVFilterLink *outlink)
Definition: af_amerge.c:186
static int config_output(AVFilterLink *outlink)
Definition: af_amerge.c:154
int channels
number of audio channels, only used for audio.
Definition: frame.h:523
audio channel layout utility functions
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define FFMIN(a, b)
Definition: common.h:96
static int ff_bufqueue_is_full(struct FFBufQueue *queue)
Test if a buffer queue is full.
Definition: bufferqueue.h:60
AVFormatContext * ctx
Definition: movenc.c:48
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
Definition: bufferqueue.h:111
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:538
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
A list of supported channel layouts.
Definition: formats.h:85
void av_bprint_channel_layout(struct AVBPrint *bp, int nb_channels, uint64_t channel_layout)
Append a description of a channel layout to a bprint buffer.
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
#define FLAGS
Definition: af_amerge.c:54
static int query_formats(AVFilterContext *ctx)
Definition: af_amerge.c:78
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
AVFilter ff_af_amerge
Definition: af_amerge.c:357
void * buf
Definition: avisynth_c.h:690
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
int nb_channel_layouts
number of channel layouts
Definition: formats.h:87
#define OFFSET(x)
Definition: af_amerge.c:53
static double c[64]
int route[SWR_CH_MAX]
channels routing, see copy_samples
Definition: af_amerge.c:43
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
static void copy_samples(int nb_inputs, struct amerge_input in[], int *route, uint8_t *ins[], uint8_t **outs, int ns, int bps)
Copy samples from several input streams to one output stream.
Definition: af_amerge.c:217
#define av_freep(p)
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
Definition: bufferqueue.h:71
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:407
formats
Definition: signature.h:48
internal API functions
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:284
for(j=16;j >0;--j)
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:652
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
static int ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.
Definition: internal.h:277
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.
Definition: bufferqueue.h:87