FFmpeg  4.3.6
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 "avfilter.h"
31 #include "filters.h"
32 #include "audio.h"
33 #include "internal.h"
34 
35 #define SWR_CH_MAX 64
36 
37 typedef struct AMergeContext {
38  const AVClass *class;
39  int nb_inputs;
40  int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
41  int bps;
42  struct amerge_input {
43  int nb_ch; /**< number of channels for the input */
44  } *in;
46 
47 #define OFFSET(x) offsetof(AMergeContext, x)
48 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
49 
50 static const AVOption amerge_options[] = {
51  { "inputs", "specify the number of inputs", OFFSET(nb_inputs),
52  AV_OPT_TYPE_INT, { .i64 = 2 }, 1, SWR_CH_MAX, FLAGS },
53  { NULL }
54 };
55 
56 AVFILTER_DEFINE_CLASS(amerge);
57 
59 {
60  AMergeContext *s = ctx->priv;
61 
62  av_freep(&s->in);
63  for (unsigned i = 0; i < ctx->nb_inputs; i++)
64  av_freep(&ctx->input_pads[i].name);
65 }
66 
68 {
69  AMergeContext *s = ctx->priv;
70  int64_t inlayout[SWR_CH_MAX], outlayout = 0;
73  int i, ret, overlap = 0, nb_ch = 0;
74 
75  for (i = 0; i < s->nb_inputs; i++) {
76  if (!ctx->inputs[i]->in_channel_layouts ||
79  "No channel layout for input %d\n", i + 1);
80  return AVERROR(EAGAIN);
81  }
82  inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
83  if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
84  char buf[256];
85  av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
86  av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
87  }
88  s->in[i].nb_ch = FF_LAYOUT2COUNT(inlayout[i]);
89  if (s->in[i].nb_ch) {
90  overlap++;
91  } else {
92  s->in[i].nb_ch = av_get_channel_layout_nb_channels(inlayout[i]);
93  if (outlayout & inlayout[i])
94  overlap++;
95  outlayout |= inlayout[i];
96  }
97  nb_ch += s->in[i].nb_ch;
98  }
99  if (nb_ch > SWR_CH_MAX) {
100  av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
101  return AVERROR(EINVAL);
102  }
103  if (overlap) {
104  av_log(ctx, AV_LOG_WARNING,
105  "Input channel layouts overlap: "
106  "output layout will be determined by the number of distinct input channels\n");
107  for (i = 0; i < nb_ch; i++)
108  s->route[i] = i;
109  outlayout = av_get_default_channel_layout(nb_ch);
110  if (!outlayout && nb_ch)
111  outlayout = 0xFFFFFFFFFFFFFFFFULL >> (64 - nb_ch);
112  } else {
113  int *route[SWR_CH_MAX];
114  int c, out_ch_number = 0;
115 
116  route[0] = s->route;
117  for (i = 1; i < s->nb_inputs; i++)
118  route[i] = route[i - 1] + s->in[i - 1].nb_ch;
119  for (c = 0; c < 64; c++)
120  for (i = 0; i < s->nb_inputs; i++)
121  if ((inlayout[i] >> c) & 1)
122  *(route[i]++) = out_ch_number++;
123  }
125  if ((ret = ff_set_common_formats(ctx, formats)) < 0)
126  return ret;
127  for (i = 0; i < s->nb_inputs; i++) {
128  layouts = NULL;
129  if ((ret = ff_add_channel_layout(&layouts, inlayout[i])) < 0)
130  return ret;
131  if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
132  return ret;
133  }
134  layouts = NULL;
135  if ((ret = ff_add_channel_layout(&layouts, outlayout)) < 0)
136  return ret;
137  if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
138  return ret;
139 
141 }
142 
143 static int config_output(AVFilterLink *outlink)
144 {
145  AVFilterContext *ctx = outlink->src;
146  AMergeContext *s = ctx->priv;
147  AVBPrint bp;
148  int i;
149 
150  for (i = 1; i < s->nb_inputs; i++) {
151  if (ctx->inputs[i]->sample_rate != ctx->inputs[0]->sample_rate) {
152  av_log(ctx, AV_LOG_ERROR,
153  "Inputs must have the same sample rate "
154  "%d for in%d vs %d\n",
155  ctx->inputs[i]->sample_rate, i, ctx->inputs[0]->sample_rate);
156  return AVERROR(EINVAL);
157  }
158  }
159  s->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
160  outlink->sample_rate = ctx->inputs[0]->sample_rate;
161  outlink->time_base = ctx->inputs[0]->time_base;
162 
164  for (i = 0; i < s->nb_inputs; i++) {
165  av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
167  }
168  av_bprintf(&bp, " -> out:");
170  av_log(ctx, AV_LOG_VERBOSE, "%s\n", bp.str);
171 
172  return 0;
173 }
174 
175 /**
176  * Copy samples from several input streams to one output stream.
177  * @param nb_inputs number of inputs
178  * @param in inputs; used only for the nb_ch field;
179  * @param route routing values;
180  * input channel i goes to output channel route[i];
181  * i < in[0].nb_ch are the channels from the first output;
182  * i >= in[0].nb_ch are the channels from the second output
183  * @param ins pointer to the samples of each inputs, in packed format;
184  * will be left at the end of the copied samples
185  * @param outs pointer to the samples of the output, in packet format;
186  * must point to a buffer big enough;
187  * will be left at the end of the copied samples
188  * @param ns number of samples to copy
189  * @param bps bytes per sample
190  */
191 static inline void copy_samples(int nb_inputs, struct amerge_input in[],
192  int *route, uint8_t *ins[],
193  uint8_t **outs, int ns, int bps)
194 {
195  int *route_cur;
196  int i, c, nb_ch = 0;
197 
198  for (i = 0; i < nb_inputs; i++)
199  nb_ch += in[i].nb_ch;
200  while (ns--) {
201  route_cur = route;
202  for (i = 0; i < nb_inputs; i++) {
203  for (c = 0; c < in[i].nb_ch; c++) {
204  memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
205  ins[i] += bps;
206  }
207  }
208  *outs += nb_ch * bps;
209  }
210 }
211 
212 static void free_frames(int nb_inputs, AVFrame **input_frames)
213 {
214  int i;
215  for (i = 0; i < nb_inputs; i++)
216  av_frame_free(&input_frames[i]);
217 }
218 
219 static int try_push_frame(AVFilterContext *ctx, int nb_samples)
220 {
221  AMergeContext *s = ctx->priv;
222  AVFilterLink *outlink = ctx->outputs[0];
223  int i, ret;
224  AVFrame *outbuf, *inbuf[SWR_CH_MAX] = { NULL };
225  uint8_t *outs, *ins[SWR_CH_MAX];
226 
227  for (i = 0; i < ctx->nb_inputs; i++) {
228  ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &inbuf[i]);
229  if (ret < 0) {
230  free_frames(i, inbuf);
231  return ret;
232  }
233  ins[i] = inbuf[i]->data[0];
234  }
235 
236  outbuf = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
237  if (!outbuf) {
238  free_frames(s->nb_inputs, inbuf);
239  return AVERROR(ENOMEM);
240  }
241 
242  outs = outbuf->data[0];
243  outbuf->pts = inbuf[0]->pts;
244 
245  outbuf->nb_samples = nb_samples;
246  outbuf->channel_layout = outlink->channel_layout;
247  outbuf->channels = outlink->channels;
248 
249  while (nb_samples) {
250  /* Unroll the most common sample formats: speed +~350% for the loop,
251  +~13% overall (including two common decoders) */
252  switch (s->bps) {
253  case 1:
254  copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 1);
255  break;
256  case 2:
257  copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 2);
258  break;
259  case 4:
260  copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 4);
261  break;
262  default:
263  copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, s->bps);
264  break;
265  }
266 
267  nb_samples = 0;
268  }
269 
270  free_frames(s->nb_inputs, inbuf);
271  return ff_filter_frame(ctx->outputs[0], outbuf);
272 }
273 
275 {
276  int i, status;
277  int ret, nb_samples;
278  int64_t pts;
279 
281 
282  nb_samples = ff_inlink_queued_samples(ctx->inputs[0]);
283  for (i = 1; i < ctx->nb_inputs && nb_samples > 0; i++) {
284  nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[i]), nb_samples);
285  }
286 
287  if (nb_samples) {
288  ret = try_push_frame(ctx, nb_samples);
289  if (ret < 0)
290  return ret;
291  }
292 
293  for (i = 0; i < ctx->nb_inputs; i++) {
294  if (ff_inlink_queued_samples(ctx->inputs[i]))
295  continue;
296 
297  if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
298  ff_outlink_set_status(ctx->outputs[0], status, pts);
299  return 0;
300  } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
302  return 0;
303  }
304  }
305 
306  return 0;
307 }
308 
310 {
311  AMergeContext *s = ctx->priv;
312  int i, ret;
313 
314  s->in = av_calloc(s->nb_inputs, sizeof(*s->in));
315  if (!s->in)
316  return AVERROR(ENOMEM);
317  for (i = 0; i < s->nb_inputs; i++) {
318  char *name = av_asprintf("in%d", i);
319  AVFilterPad pad = {
320  .name = name,
321  .type = AVMEDIA_TYPE_AUDIO,
322  };
323  if (!name)
324  return AVERROR(ENOMEM);
325  if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
326  av_freep(&pad.name);
327  return ret;
328  }
329  }
330  return 0;
331 }
332 
333 static const AVFilterPad amerge_outputs[] = {
334  {
335  .name = "default",
336  .type = AVMEDIA_TYPE_AUDIO,
337  .config_props = config_output,
338  },
339  { NULL }
340 };
341 
343  .name = "amerge",
344  .description = NULL_IF_CONFIG_SMALL("Merge two or more audio streams into "
345  "a single multi-channel stream."),
346  .priv_size = sizeof(AMergeContext),
347  .init = init,
348  .uninit = uninit,
350  .activate = activate,
351  .inputs = NULL,
352  .outputs = amerge_outputs,
353  .priv_class = &amerge_class,
355 };
struct AMergeContext::amerge_input * in
#define NULL
Definition: coverity.c:32
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:43
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
AVOption.
Definition: opt.h:246
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:212
Main libavfilter public API header.
#define SWR_CH_MAX
Definition: af_amerge.c:35
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:105
AVFILTER_DEFINE_CLASS(amerge)
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1602
static int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition: filters.h:172
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:300
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
static int activate(AVFilterContext *ctx)
Definition: af_amerge.c:274
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:479
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1075
uint8_t
#define av_cold
Definition: attributes.h:88
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:393
static av_cold int init(AVFilterContext *ctx)
Definition: af_amerge.c:309
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define av_log(a,...)
static void free_frames(int nb_inputs, AVFrame **input_frames)
Definition: af_amerge.c:212
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_amerge.c:58
A filter pad used for either input or output.
Definition: internal.h:54
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1431
static const AVFilterPad amerge_outputs[]
Definition: af_amerge.c:333
AVFilterPad * input_pads
array of input pads
Definition: avfilter.h:345
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
#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:605
static const AVOption amerge_options[]
Definition: af_amerge.c:50
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:356
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:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
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:477
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
static int config_output(AVFilterLink *outlink)
Definition: af_amerge.c:143
const char * name
Definition: qsvenc.c:46
int channels
number of audio channels, only used for audio.
Definition: frame.h:606
audio channel layout utility functions
unsigned nb_inputs
number of input pads
Definition: avfilter.h:347
#define FFMIN(a, b)
Definition: common.h:96
int ff_inlink_queued_samples(AVFilterLink *link)
Definition: avfilter.c:1456
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
static int try_push_frame(AVFilterContext *ctx, int nb_samples)
Definition: af_amerge.c:219
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
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:48
static int query_formats(AVFilterContext *ctx)
Definition: af_amerge.c:67
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
#define AV_BPRINT_SIZE_AUTOMATIC
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link&#39;s FIFO and update the link&#39;s stats.
Definition: avfilter.c:1495
AVFilter ff_af_amerge
Definition: af_amerge.c:342
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:439
static int64_t pts
#define flags(name, subs,...)
Definition: cbs_av1.c:565
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
#define ns(max_value, name, subs,...)
Definition: cbs_av1.c:686
int nb_channel_layouts
number of channel layouts
Definition: formats.h:87
#define OFFSET(x)
Definition: af_amerge.c:47
static double c[64]
int route[SWR_CH_MAX]
channels routing, see copy_samples
Definition: af_amerge.c:40
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:191
#define av_freep(p)
formats
Definition: signature.h:48
internal API functions
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:366
for(j=16;j >0;--j)
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:593
static int ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.
Definition: internal.h:266