FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #include "libavutil/avstring.h"
27 #include "libavutil/bprint.h"
29 #include "libavutil/opt.h"
30 #include "libswresample/swresample.h" // only for SWR_CH_MAX
31 #include "avfilter.h"
32 #include "audio.h"
33 #include "bufferqueue.h"
34 #include "internal.h"
35 
36 typedef struct {
37  const AVClass *class;
38  int nb_inputs;
39  int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
40  int bps;
41  struct amerge_input {
42  struct FFBufQueue queue;
43  int nb_ch; /**< number of channels for the input */
45  int pos;
46  } *in;
48 
49 #define OFFSET(x) offsetof(AMergeContext, x)
50 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51 
52 static const AVOption amerge_options[] = {
53  { "inputs", "specify the number of inputs", OFFSET(nb_inputs),
54  AV_OPT_TYPE_INT, { .i64 = 2 }, 2, SWR_CH_MAX, FLAGS },
55  {0}
56 };
57 
58 AVFILTER_DEFINE_CLASS(amerge);
59 
60 static av_cold void uninit(AVFilterContext *ctx)
61 {
62  AMergeContext *am = ctx->priv;
63  int i;
64 
65  for (i = 0; i < am->nb_inputs; i++) {
66  if (am->in)
68  if (ctx->input_pads)
69  av_freep(&ctx->input_pads[i].name);
70  }
71  av_freep(&am->in);
72 }
73 
75 {
76  AMergeContext *am = ctx->priv;
77  int64_t inlayout[SWR_CH_MAX], outlayout = 0;
80  int i, overlap = 0, nb_ch = 0;
81 
82  for (i = 0; i < am->nb_inputs; i++) {
83  if (!ctx->inputs[i]->in_channel_layouts ||
85  av_log(ctx, AV_LOG_ERROR,
86  "No channel layout for input %d\n", i + 1);
87  return AVERROR(EINVAL);
88  }
89  inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
90  if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
91  char buf[256];
92  av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
93  av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
94  }
95  am->in[i].nb_ch = av_get_channel_layout_nb_channels(inlayout[i]);
96  if (outlayout & inlayout[i])
97  overlap++;
98  outlayout |= inlayout[i];
99  nb_ch += am->in[i].nb_ch;
100  }
101  if (nb_ch > SWR_CH_MAX) {
102  av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
103  return AVERROR(EINVAL);
104  }
105  if (overlap) {
106  av_log(ctx, AV_LOG_WARNING,
107  "Input channel layouts overlap: "
108  "output layout will be determined by the number of distinct input channels\n");
109  for (i = 0; i < nb_ch; i++)
110  am->route[i] = i;
111  outlayout = av_get_default_channel_layout(nb_ch);
112  if (!outlayout)
113  outlayout = ((int64_t)1 << nb_ch) - 1;
114  } else {
115  int *route[SWR_CH_MAX];
116  int c, out_ch_number = 0;
117 
118  route[0] = am->route;
119  for (i = 1; i < am->nb_inputs; i++)
120  route[i] = route[i - 1] + am->in[i - 1].nb_ch;
121  for (c = 0; c < 64; c++)
122  for (i = 0; i < am->nb_inputs; i++)
123  if ((inlayout[i] >> c) & 1)
124  *(route[i]++) = out_ch_number++;
125  }
127  ff_set_common_formats(ctx, formats);
128  for (i = 0; i < am->nb_inputs; i++) {
129  layouts = NULL;
130  ff_add_channel_layout(&layouts, inlayout[i]);
132  }
133  layouts = NULL;
134  ff_add_channel_layout(&layouts, outlayout);
137  return 0;
138 }
139 
140 static int config_output(AVFilterLink *outlink)
141 {
142  AVFilterContext *ctx = outlink->src;
143  AMergeContext *am = ctx->priv;
144  AVBPrint bp;
145  int i;
146 
147  for (i = 1; i < am->nb_inputs; i++) {
148  if (ctx->inputs[i]->sample_rate != ctx->inputs[0]->sample_rate) {
149  av_log(ctx, AV_LOG_ERROR,
150  "Inputs must have the same sample rate "
151  "%d for in%d vs %d\n",
152  ctx->inputs[i]->sample_rate, i, ctx->inputs[0]->sample_rate);
153  return AVERROR(EINVAL);
154  }
155  }
156  am->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
157  outlink->sample_rate = ctx->inputs[0]->sample_rate;
158  outlink->time_base = ctx->inputs[0]->time_base;
159 
160  av_bprint_init(&bp, 0, 1);
161  for (i = 0; i < am->nb_inputs; i++) {
162  av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
164  }
165  av_bprintf(&bp, " -> out:");
167  av_log(ctx, AV_LOG_VERBOSE, "%s\n", bp.str);
168 
169  return 0;
170 }
171 
172 static int request_frame(AVFilterLink *outlink)
173 {
174  AVFilterContext *ctx = outlink->src;
175  AMergeContext *am = ctx->priv;
176  int i, ret;
177 
178  for (i = 0; i < am->nb_inputs; i++)
179  if (!am->in[i].nb_samples)
180  if ((ret = ff_request_frame(ctx->inputs[i])) < 0)
181  return ret;
182  return 0;
183 }
184 
185 /**
186  * Copy samples from several input streams to one output stream.
187  * @param nb_inputs number of inputs
188  * @param in inputs; used only for the nb_ch field;
189  * @param route routing values;
190  * input channel i goes to output channel route[i];
191  * i < in[0].nb_ch are the channels from the first output;
192  * i >= in[0].nb_ch are the channels from the second output
193  * @param ins pointer to the samples of each inputs, in packed format;
194  * will be left at the end of the copied samples
195  * @param outs pointer to the samples of the output, in packet format;
196  * must point to a buffer big enough;
197  * will be left at the end of the copied samples
198  * @param ns number of samples to copy
199  * @param bps bytes per sample
200  */
201 static inline void copy_samples(int nb_inputs, struct amerge_input in[],
202  int *route, uint8_t *ins[],
203  uint8_t **outs, int ns, int bps)
204 {
205  int *route_cur;
206  int i, c, nb_ch = 0;
207 
208  for (i = 0; i < nb_inputs; i++)
209  nb_ch += in[i].nb_ch;
210  while (ns--) {
211  route_cur = route;
212  for (i = 0; i < nb_inputs; i++) {
213  for (c = 0; c < in[i].nb_ch; c++) {
214  memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
215  ins[i] += bps;
216  }
217  }
218  *outs += nb_ch * bps;
219  }
220 }
221 
222 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
223 {
224  AVFilterContext *ctx = inlink->dst;
225  AMergeContext *am = ctx->priv;
226  AVFilterLink *const outlink = ctx->outputs[0];
227  int input_number;
228  int nb_samples, ns, i;
229  AVFilterBufferRef *outbuf, *inbuf[SWR_CH_MAX];
230  uint8_t *ins[SWR_CH_MAX], *outs;
231 
232  for (input_number = 0; input_number < am->nb_inputs; input_number++)
233  if (inlink == ctx->inputs[input_number])
234  break;
235  av_assert1(input_number < am->nb_inputs);
236  if (ff_bufqueue_is_full(&am->in[input_number].queue)) {
237  av_log(ctx, AV_LOG_ERROR, "Buffer queue overflow\n");
238  avfilter_unref_buffer(insamples);
239  return AVERROR(ENOMEM);
240  }
241  ff_bufqueue_add(ctx, &am->in[input_number].queue, insamples);
242  am->in[input_number].nb_samples += insamples->audio->nb_samples;
243  nb_samples = am->in[0].nb_samples;
244  for (i = 1; i < am->nb_inputs; i++)
245  nb_samples = FFMIN(nb_samples, am->in[i].nb_samples);
246  if (!nb_samples)
247  return 0;
248 
249  outbuf = ff_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE, nb_samples);
250  outs = outbuf->data[0];
251  for (i = 0; i < am->nb_inputs; i++) {
252  inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
253  ins[i] = inbuf[i]->data[0] +
254  am->in[i].pos * am->in[i].nb_ch * am->bps;
255  }
256  avfilter_copy_buffer_ref_props(outbuf, inbuf[0]);
257  outbuf->pts = inbuf[0]->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
258  inbuf[0]->pts +
259  av_rescale_q(am->in[0].pos,
260  (AVRational){ 1, ctx->inputs[0]->sample_rate },
261  ctx->outputs[0]->time_base);
262 
263  outbuf->audio->nb_samples = nb_samples;
264  outbuf->audio->channel_layout = outlink->channel_layout;
265  outbuf->audio->channels = outlink->channels;
266 
267  while (nb_samples) {
268  ns = nb_samples;
269  for (i = 0; i < am->nb_inputs; i++)
270  ns = FFMIN(ns, inbuf[i]->audio->nb_samples - am->in[i].pos);
271  /* Unroll the most common sample formats: speed +~350% for the loop,
272  +~13% overall (including two common decoders) */
273  switch (am->bps) {
274  case 1:
275  copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 1);
276  break;
277  case 2:
278  copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 2);
279  break;
280  case 4:
281  copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 4);
282  break;
283  default:
284  copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, am->bps);
285  break;
286  }
287 
288  nb_samples -= ns;
289  for (i = 0; i < am->nb_inputs; i++) {
290  am->in[i].nb_samples -= ns;
291  am->in[i].pos += ns;
292  if (am->in[i].pos == inbuf[i]->audio->nb_samples) {
293  am->in[i].pos = 0;
294  avfilter_unref_buffer(inbuf[i]);
295  ff_bufqueue_get(&am->in[i].queue);
296  inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
297  ins[i] = inbuf[i] ? inbuf[i]->data[0] : NULL;
298  }
299  }
300  }
301  return ff_filter_frame(ctx->outputs[0], outbuf);
302 }
303 
304 static av_cold int init(AVFilterContext *ctx, const char *args)
305 {
306  AMergeContext *am = ctx->priv;
307  int ret, i;
308 
309  am->class = &amerge_class;
311  ret = av_set_options_string(am, args, "=", ":");
312  if (ret < 0) {
313  av_log(ctx, AV_LOG_ERROR, "Error parsing options: '%s'\n", args);
314  return ret;
315  }
316  am->in = av_calloc(am->nb_inputs, sizeof(*am->in));
317  if (!am->in)
318  return AVERROR(ENOMEM);
319  for (i = 0; i < am->nb_inputs; i++) {
320  char *name = av_asprintf("in%d", i);
321  AVFilterPad pad = {
322  .name = name,
323  .type = AVMEDIA_TYPE_AUDIO,
324  .filter_frame = filter_frame,
325  .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
326  };
327  if (!name)
328  return AVERROR(ENOMEM);
329  ff_insert_inpad(ctx, i, &pad);
330  }
331  return 0;
332 }
333 
334 static const AVFilterPad amerge_outputs[] = {
335  {
336  .name = "default",
337  .type = AVMEDIA_TYPE_AUDIO,
338  .config_props = config_output,
339  .request_frame = request_frame,
340  },
341  { NULL }
342 };
343 
345  .name = "amerge",
346  .description = NULL_IF_CONFIG_SMALL("Merge two or more audio streams into "
347  "a single multi-channel stream."),
348  .priv_size = sizeof(AMergeContext),
349  .init = init,
350  .uninit = uninit,
352  .inputs = NULL,
353  .outputs = amerge_outputs,
354  .priv_class = &amerge_class,
355 };