1 /*
2 * Audio Mix Filter
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Audio Mix Filter
25 *
26 * Mixes audio from multiple sources into a single output. The channel layout,
27 * sample rate, and sample format will be the same for all inputs and the
28 * output.
29 */
30
31 #include "libavutil/attributes.h"
32 #include "libavutil/audio_fifo.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/channel_layout.h"
36 #include "libavutil/common.h"
37 #include "libavutil/eval.h"
38 #include "libavutil/float_dsp.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/samplefmt.h"
42
43 #include "audio.h"
44 #include "avfilter.h"
45 #include "filters.h"
46 #include "formats.h"
47 #include "internal.h"
48
49 #define INPUT_ON 1 /**< input is active */
50 #define INPUT_EOF 2 /**< input has reached EOF (may still be active) */
51
52 #define DURATION_LONGEST 0
53 #define DURATION_SHORTEST 1
54 #define DURATION_FIRST 2
55
56
57 typedef struct FrameInfo {
58 int nb_samples;
59 int64_t pts;
60 struct FrameInfo *next;
61 } FrameInfo;
62
63 /**
64 * Linked list used to store timestamps and frame sizes of all frames in the
65 * FIFO for the first input.
66 *
67 * This is needed to keep timestamps synchronized for the case where multiple
68 * input frames are pushed to the filter for processing before a frame is
69 * requested by the output link.
70 */
71 typedef struct FrameList {
72 int nb_frames;
73 int nb_samples;
74 FrameInfo *list;
75 FrameInfo *end;
76 } FrameList;
77
frame_list_clear(FrameList * frame_list)78 static void frame_list_clear(FrameList *frame_list)
79 {
80 if (frame_list) {
81 while (frame_list->list) {
82 FrameInfo *info = frame_list->list;
83 frame_list->list = info->next;
84 av_free(info);
85 }
86 frame_list->nb_frames = 0;
87 frame_list->nb_samples = 0;
88 frame_list->end = NULL;
89 }
90 }
91
frame_list_next_frame_size(FrameList * frame_list)92 static int frame_list_next_frame_size(FrameList *frame_list)
93 {
94 if (!frame_list->list)
95 return 0;
96 return frame_list->list->nb_samples;
97 }
98
frame_list_next_pts(FrameList * frame_list)99 static int64_t frame_list_next_pts(FrameList *frame_list)
100 {
101 if (!frame_list->list)
102 return AV_NOPTS_VALUE;
103 return frame_list->list->pts;
104 }
105
frame_list_remove_samples(FrameList * frame_list,int nb_samples)106 static void frame_list_remove_samples(FrameList *frame_list, int nb_samples)
107 {
108 if (nb_samples >= frame_list->nb_samples) {
109 frame_list_clear(frame_list);
110 } else {
111 int samples = nb_samples;
112 while (samples > 0) {
113 FrameInfo *info = frame_list->list;
114 av_assert0(info);
115 if (info->nb_samples <= samples) {
116 samples -= info->nb_samples;
117 frame_list->list = info->next;
118 if (!frame_list->list)
119 frame_list->end = NULL;
120 frame_list->nb_frames--;
121 frame_list->nb_samples -= info->nb_samples;
122 av_free(info);
123 } else {
124 info->nb_samples -= samples;
125 info->pts += samples;
126 frame_list->nb_samples -= samples;
127 samples = 0;
128 }
129 }
130 }
131 }
132
frame_list_add_frame(FrameList * frame_list,int nb_samples,int64_t pts)133 static int frame_list_add_frame(FrameList *frame_list, int nb_samples, int64_t pts)
134 {
135 FrameInfo *info = av_malloc(sizeof(*info));
136 if (!info)
137 return AVERROR(ENOMEM);
138 info->nb_samples = nb_samples;
139 info->pts = pts;
140 info->next = NULL;
141
142 if (!frame_list->list) {
143 frame_list->list = info;
144 frame_list->end = info;
145 } else {
146 av_assert0(frame_list->end);
147 frame_list->end->next = info;
148 frame_list->end = info;
149 }
150 frame_list->nb_frames++;
151 frame_list->nb_samples += nb_samples;
152
153 return 0;
154 }
155
156 /* FIXME: use directly links fifo */
157
158 typedef struct MixContext {
159 const AVClass *class; /**< class for AVOptions */
160 AVFloatDSPContext *fdsp;
161
162 int nb_inputs; /**< number of inputs */
163 int active_inputs; /**< number of input currently active */
164 int duration_mode; /**< mode for determining duration */
165 float dropout_transition; /**< transition time when an input drops out */
166 char *weights_str; /**< string for custom weights for every input */
167 int normalize; /**< if inputs are scaled */
168
169 int nb_channels; /**< number of channels */
170 int sample_rate; /**< sample rate */
171 int planar;
172 AVAudioFifo **fifos; /**< audio fifo for each input */
173 uint8_t *input_state; /**< current state of each input */
174 float *input_scale; /**< mixing scale factor for each input */
175 float *weights; /**< custom weights for every input */
176 float weight_sum; /**< sum of custom weights for every input */
177 float *scale_norm; /**< normalization factor for every input */
178 int64_t next_pts; /**< calculated pts for next output frame */
179 FrameList *frame_list; /**< list of frame info for the first input */
180 } MixContext;
181
182 #define OFFSET(x) offsetof(MixContext, x)
183 #define A AV_OPT_FLAG_AUDIO_PARAM
184 #define F AV_OPT_FLAG_FILTERING_PARAM
185 #define T AV_OPT_FLAG_RUNTIME_PARAM
186 static const AVOption amix_options[] = {
187 { "inputs", "Number of inputs.",
188 OFFSET(nb_inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT16_MAX, A|F },
189 { "duration", "How to determine the end-of-stream.",
190 OFFSET(duration_mode), AV_OPT_TYPE_INT, { .i64 = DURATION_LONGEST }, 0, 2, A|F, "duration" },
191 { "longest", "Duration of longest input.", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_LONGEST }, 0, 0, A|F, "duration" },
192 { "shortest", "Duration of shortest input.", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_SHORTEST }, 0, 0, A|F, "duration" },
193 { "first", "Duration of first input.", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_FIRST }, 0, 0, A|F, "duration" },
194 { "dropout_transition", "Transition time, in seconds, for volume "
195 "renormalization when an input stream ends.",
196 OFFSET(dropout_transition), AV_OPT_TYPE_FLOAT, { .dbl = 2.0 }, 0, INT_MAX, A|F },
197 { "weights", "Set weight for each input.",
198 OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, A|F|T },
199 { "normalize", "Scale inputs",
200 OFFSET(normalize), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A|F|T },
201 { NULL }
202 };
203
204 AVFILTER_DEFINE_CLASS(amix);
205
206 /**
207 * Update the scaling factors to apply to each input during mixing.
208 *
209 * This balances the full volume range between active inputs and handles
210 * volume transitions when EOF is encountered on an input but mixing continues
211 * with the remaining inputs.
212 */
calculate_scales(MixContext * s,int nb_samples)213 static void calculate_scales(MixContext *s, int nb_samples)
214 {
215 float weight_sum = 0.f;
216 int i;
217
218 for (i = 0; i < s->nb_inputs; i++)
219 if (s->input_state[i] & INPUT_ON)
220 weight_sum += FFABS(s->weights[i]);
221
222 for (i = 0; i < s->nb_inputs; i++) {
223 if (s->input_state[i] & INPUT_ON) {
224 if (s->scale_norm[i] > weight_sum / FFABS(s->weights[i])) {
225 s->scale_norm[i] -= ((s->weight_sum / FFABS(s->weights[i])) / s->nb_inputs) *
226 nb_samples / (s->dropout_transition * s->sample_rate);
227 s->scale_norm[i] = FFMAX(s->scale_norm[i], weight_sum / FFABS(s->weights[i]));
228 }
229 }
230 }
231
232 for (i = 0; i < s->nb_inputs; i++) {
233 if (s->input_state[i] & INPUT_ON) {
234 if (!s->normalize)
235 s->input_scale[i] = FFABS(s->weights[i]);
236 else
237 s->input_scale[i] = 1.0f / s->scale_norm[i] * FFSIGN(s->weights[i]);
238 } else {
239 s->input_scale[i] = 0.0f;
240 }
241 }
242 }
243
config_output(AVFilterLink * outlink)244 static int config_output(AVFilterLink *outlink)
245 {
246 AVFilterContext *ctx = outlink->src;
247 MixContext *s = ctx->priv;
248 int i;
249 char buf[64];
250
251 s->planar = av_sample_fmt_is_planar(outlink->format);
252 s->sample_rate = outlink->sample_rate;
253 outlink->time_base = (AVRational){ 1, outlink->sample_rate };
254 s->next_pts = AV_NOPTS_VALUE;
255
256 s->frame_list = av_mallocz(sizeof(*s->frame_list));
257 if (!s->frame_list)
258 return AVERROR(ENOMEM);
259
260 s->fifos = av_mallocz_array(s->nb_inputs, sizeof(*s->fifos));
261 if (!s->fifos)
262 return AVERROR(ENOMEM);
263
264 s->nb_channels = outlink->channels;
265 for (i = 0; i < s->nb_inputs; i++) {
266 s->fifos[i] = av_audio_fifo_alloc(outlink->format, s->nb_channels, 1024);
267 if (!s->fifos[i])
268 return AVERROR(ENOMEM);
269 }
270
271 s->input_state = av_malloc(s->nb_inputs);
272 if (!s->input_state)
273 return AVERROR(ENOMEM);
274 memset(s->input_state, INPUT_ON, s->nb_inputs);
275 s->active_inputs = s->nb_inputs;
276
277 s->input_scale = av_mallocz_array(s->nb_inputs, sizeof(*s->input_scale));
278 s->scale_norm = av_mallocz_array(s->nb_inputs, sizeof(*s->scale_norm));
279 if (!s->input_scale || !s->scale_norm)
280 return AVERROR(ENOMEM);
281 for (i = 0; i < s->nb_inputs; i++)
282 s->scale_norm[i] = s->weight_sum / FFABS(s->weights[i]);
283 calculate_scales(s, 0);
284
285 av_get_channel_layout_string(buf, sizeof(buf), -1, outlink->channel_layout);
286
287 av_log(ctx, AV_LOG_VERBOSE,
288 "inputs:%d fmt:%s srate:%d cl:%s\n", s->nb_inputs,
289 av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf);
290
291 return 0;
292 }
293
294 /**
295 * Read samples from the input FIFOs, mix, and write to the output link.
296 */
output_frame(AVFilterLink * outlink)297 static int output_frame(AVFilterLink *outlink)
298 {
299 AVFilterContext *ctx = outlink->src;
300 MixContext *s = ctx->priv;
301 AVFrame *out_buf, *in_buf;
302 int nb_samples, ns, i;
303
304 if (s->input_state[0] & INPUT_ON) {
305 /* first input live: use the corresponding frame size */
306 nb_samples = frame_list_next_frame_size(s->frame_list);
307 for (i = 1; i < s->nb_inputs; i++) {
308 if (s->input_state[i] & INPUT_ON) {
309 ns = av_audio_fifo_size(s->fifos[i]);
310 if (ns < nb_samples) {
311 if (!(s->input_state[i] & INPUT_EOF))
312 /* unclosed input with not enough samples */
313 return 0;
314 /* closed input to drain */
315 nb_samples = ns;
316 }
317 }
318 }
319
320 s->next_pts = frame_list_next_pts(s->frame_list);
321 } else {
322 /* first input closed: use the available samples */
323 nb_samples = INT_MAX;
324 for (i = 1; i < s->nb_inputs; i++) {
325 if (s->input_state[i] & INPUT_ON) {
326 ns = av_audio_fifo_size(s->fifos[i]);
327 nb_samples = FFMIN(nb_samples, ns);
328 }
329 }
330 if (nb_samples == INT_MAX) {
331 ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
332 return 0;
333 }
334 }
335
336 frame_list_remove_samples(s->frame_list, nb_samples);
337
338 calculate_scales(s, nb_samples);
339
340 if (nb_samples == 0)
341 return 0;
342
343 out_buf = ff_get_audio_buffer(outlink, nb_samples);
344 if (!out_buf)
345 return AVERROR(ENOMEM);
346
347 in_buf = ff_get_audio_buffer(outlink, nb_samples);
348 if (!in_buf) {
349 av_frame_free(&out_buf);
350 return AVERROR(ENOMEM);
351 }
352
353 for (i = 0; i < s->nb_inputs; i++) {
354 if (s->input_state[i] & INPUT_ON) {
355 int planes, plane_size, p;
356
357 av_audio_fifo_read(s->fifos[i], (void **)in_buf->extended_data,
358 nb_samples);
359
360 planes = s->planar ? s->nb_channels : 1;
361 plane_size = nb_samples * (s->planar ? 1 : s->nb_channels);
362 plane_size = FFALIGN(plane_size, 16);
363
364 if (out_buf->format == AV_SAMPLE_FMT_FLT ||
365 out_buf->format == AV_SAMPLE_FMT_FLTP) {
366 for (p = 0; p < planes; p++) {
367 s->fdsp->vector_fmac_scalar((float *)out_buf->extended_data[p],
368 (float *) in_buf->extended_data[p],
369 s->input_scale[i], plane_size);
370 }
371 } else {
372 for (p = 0; p < planes; p++) {
373 s->fdsp->vector_dmac_scalar((double *)out_buf->extended_data[p],
374 (double *) in_buf->extended_data[p],
375 s->input_scale[i], plane_size);
376 }
377 }
378 }
379 }
380 av_frame_free(&in_buf);
381
382 out_buf->pts = s->next_pts;
383 if (s->next_pts != AV_NOPTS_VALUE)
384 s->next_pts += nb_samples;
385
386 return ff_filter_frame(outlink, out_buf);
387 }
388
389 /**
390 * Requests a frame, if needed, from each input link other than the first.
391 */
request_samples(AVFilterContext * ctx,int min_samples)392 static int request_samples(AVFilterContext *ctx, int min_samples)
393 {
394 MixContext *s = ctx->priv;
395 int i;
396
397 av_assert0(s->nb_inputs > 1);
398
399 for (i = 1; i < s->nb_inputs; i++) {
400 if (!(s->input_state[i] & INPUT_ON) ||
401 (s->input_state[i] & INPUT_EOF))
402 continue;
403 if (av_audio_fifo_size(s->fifos[i]) >= min_samples)
404 continue;
405 ff_inlink_request_frame(ctx->inputs[i]);
406 }
407 return output_frame(ctx->outputs[0]);
408 }
409
410 /**
411 * Calculates the number of active inputs and determines EOF based on the
412 * duration option.
413 *
414 * @return 0 if mixing should continue, or AVERROR_EOF if mixing should stop.
415 */
calc_active_inputs(MixContext * s)416 static int calc_active_inputs(MixContext *s)
417 {
418 int i;
419 int active_inputs = 0;
420 for (i = 0; i < s->nb_inputs; i++)
421 active_inputs += !!(s->input_state[i] & INPUT_ON);
422 s->active_inputs = active_inputs;
423
424 if (!active_inputs ||
425 (s->duration_mode == DURATION_FIRST && !(s->input_state[0] & INPUT_ON)) ||
426 (s->duration_mode == DURATION_SHORTEST && active_inputs != s->nb_inputs))
427 return AVERROR_EOF;
428 return 0;
429 }
430
activate(AVFilterContext * ctx)431 static int activate(AVFilterContext *ctx)
432 {
433 AVFilterLink *outlink = ctx->outputs[0];
434 MixContext *s = ctx->priv;
435 AVFrame *buf = NULL;
436 int i, ret;
437
438 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
439
440 for (i = 0; i < s->nb_inputs; i++) {
441 AVFilterLink *inlink = ctx->inputs[i];
442
443 if ((ret = ff_inlink_consume_frame(ctx->inputs[i], &buf)) > 0) {
444 if (i == 0) {
445 int64_t pts = av_rescale_q(buf->pts, inlink->time_base,
446 outlink->time_base);
447 ret = frame_list_add_frame(s->frame_list, buf->nb_samples, pts);
448 if (ret < 0) {
449 av_frame_free(&buf);
450 return ret;
451 }
452 }
453
454 ret = av_audio_fifo_write(s->fifos[i], (void **)buf->extended_data,
455 buf->nb_samples);
456 if (ret < 0) {
457 av_frame_free(&buf);
458 return ret;
459 }
460
461 av_frame_free(&buf);
462
463 ret = output_frame(outlink);
464 if (ret < 0)
465 return ret;
466 }
467 }
468
469 for (i = 0; i < s->nb_inputs; i++) {
470 int64_t pts;
471 int status;
472
473 if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
474 if (status == AVERROR_EOF) {
475 if (i == 0) {
476 s->input_state[i] = 0;
477 if (s->nb_inputs == 1) {
478 ff_outlink_set_status(outlink, status, pts);
479 return 0;
480 }
481 } else {
482 s->input_state[i] |= INPUT_EOF;
483 if (av_audio_fifo_size(s->fifos[i]) == 0) {
484 s->input_state[i] = 0;
485 }
486 }
487 }
488 }
489 }
490
491 if (calc_active_inputs(s)) {
492 ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
493 return 0;
494 }
495
496 if (ff_outlink_frame_wanted(outlink)) {
497 int wanted_samples;
498
499 if (!(s->input_state[0] & INPUT_ON))
500 return request_samples(ctx, 1);
501
502 if (s->frame_list->nb_frames == 0) {
503 ff_inlink_request_frame(ctx->inputs[0]);
504 return 0;
505 }
506 av_assert0(s->frame_list->nb_frames > 0);
507
508 wanted_samples = frame_list_next_frame_size(s->frame_list);
509
510 return request_samples(ctx, wanted_samples);
511 }
512
513 return 0;
514 }
515
parse_weights(AVFilterContext * ctx)516 static void parse_weights(AVFilterContext *ctx)
517 {
518 MixContext *s = ctx->priv;
519 float last_weight = 1.f;
520 char *p;
521 int i;
522
523 s->weight_sum = 0.f;
524 p = s->weights_str;
525 for (i = 0; i < s->nb_inputs; i++) {
526 last_weight = av_strtod(p, &p);
527 s->weights[i] = last_weight;
528 s->weight_sum += FFABS(last_weight);
529 if (p && *p) {
530 p++;
531 } else {
532 i++;
533 break;
534 }
535 }
536
537 for (; i < s->nb_inputs; i++) {
538 s->weights[i] = last_weight;
539 s->weight_sum += FFABS(last_weight);
540 }
541 }
542
init(AVFilterContext * ctx)543 static av_cold int init(AVFilterContext *ctx)
544 {
545 MixContext *s = ctx->priv;
546 int i, ret;
547
548 for (i = 0; i < s->nb_inputs; i++) {
549 AVFilterPad pad = { 0 };
550
551 pad.type = AVMEDIA_TYPE_AUDIO;
552 pad.name = av_asprintf("input%d", i);
553 if (!pad.name)
554 return AVERROR(ENOMEM);
555
556 if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
557 av_freep(&pad.name);
558 return ret;
559 }
560 }
561
562 s->fdsp = avpriv_float_dsp_alloc(0);
563 if (!s->fdsp)
564 return AVERROR(ENOMEM);
565
566 s->weights = av_mallocz_array(s->nb_inputs, sizeof(*s->weights));
567 if (!s->weights)
568 return AVERROR(ENOMEM);
569
570 parse_weights(ctx);
571
572 return 0;
573 }
574
uninit(AVFilterContext * ctx)575 static av_cold void uninit(AVFilterContext *ctx)
576 {
577 int i;
578 MixContext *s = ctx->priv;
579
580 if (s->fifos) {
581 for (i = 0; i < s->nb_inputs; i++)
582 av_audio_fifo_free(s->fifos[i]);
583 av_freep(&s->fifos);
584 }
585 frame_list_clear(s->frame_list);
586 av_freep(&s->frame_list);
587 av_freep(&s->input_state);
588 av_freep(&s->input_scale);
589 av_freep(&s->scale_norm);
590 av_freep(&s->weights);
591 av_freep(&s->fdsp);
592
593 for (i = 0; i < ctx->nb_inputs; i++)
594 av_freep(&ctx->input_pads[i].name);
595 }
596
query_formats(AVFilterContext * ctx)597 static int query_formats(AVFilterContext *ctx)
598 {
599 static const enum AVSampleFormat sample_fmts[] = {
600 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
601 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
602 AV_SAMPLE_FMT_NONE
603 };
604 int ret;
605
606 if ((ret = ff_set_common_formats(ctx, ff_make_format_list(sample_fmts))) < 0 ||
607 (ret = ff_set_common_samplerates(ctx, ff_all_samplerates())) < 0)
608 return ret;
609
610 return ff_set_common_channel_layouts(ctx, ff_all_channel_counts());
611 }
612
process_command(AVFilterContext * ctx,const char * cmd,const char * args,char * res,int res_len,int flags)613 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
614 char *res, int res_len, int flags)
615 {
616 MixContext *s = ctx->priv;
617 int ret;
618
619 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
620 if (ret < 0)
621 return ret;
622
623 parse_weights(ctx);
624 for (int i = 0; i < s->nb_inputs; i++)
625 s->scale_norm[i] = s->weight_sum / FFABS(s->weights[i]);
626 calculate_scales(s, 0);
627
628 return 0;
629 }
630
631 static const AVFilterPad avfilter_af_amix_outputs[] = {
632 {
633 .name = "default",
634 .type = AVMEDIA_TYPE_AUDIO,
635 .config_props = config_output,
636 },
637 { NULL }
638 };
639
640 AVFilter ff_af_amix = {
641 .name = "amix",
642 .description = NULL_IF_CONFIG_SMALL("Audio mixing."),
643 .priv_size = sizeof(MixContext),
644 .priv_class = &amix_class,
645 .init = init,
646 .uninit = uninit,
647 .activate = activate,
648 .query_formats = query_formats,
649 .inputs = NULL,
650 .outputs = avfilter_af_amix_outputs,
651 .process_command = process_command,
652 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
653 };
654