From 2d9531580d7fab72188f7f1a4004aec7deccd7ca Mon Sep 17 00:00:00 2001
From: Matthieu Bouron <matthieu.bouron@gmail.com>
Date: Sat, 10 Dec 2011 12:31:32 +0100
Subject: [PATCH] af_mix: mix two audio streams.

---
 Changelog                |    1 +
 libavfilter/Makefile     |    1 +
 libavfilter/af_mix.c     |  231 ++++++++++++++++++++++++++++++++++++++++++++++
 libavfilter/allfilters.c |    1 +
 libavfilter/avfilter.h   |    2 +-
 5 files changed, 235 insertions(+), 1 deletions(-)
 create mode 100644 libavfilter/af_mix.c

diff --git a/Changelog b/Changelog
index dc6481d..9d88239 100644
--- a/Changelog
+++ b/Changelog
@@ -136,6 +136,7 @@ easier to use. The changes are:
 - Dxtory capture format decoder
 - cellauto source
 - v410 Quicktime Uncompressed 4:4:4 10-bit encoder and decoder
+- af_mix filter added
 
 
 version 0.8:
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index a6fd18f..9995034 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -31,6 +31,7 @@ OBJS-$(CONFIG_ASHOWINFO_FILTER)              += af_ashowinfo.o
 OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
 OBJS-$(CONFIG_PAN_FILTER)                    += af_pan.o
 OBJS-$(CONFIG_VOLUME_FILTER)                 += af_volume.o
+OBJS-$(CONFIG_MIX_FILTER)                    += af_mix.o
 
 OBJS-$(CONFIG_ABUFFER_FILTER)                += asrc_abuffer.o
 OBJS-$(CONFIG_AEVALSRC_FILTER)               += asrc_aevalsrc.o
diff --git a/libavfilter/af_mix.c b/libavfilter/af_mix.c
new file mode 100644
index 0000000..3c89393
--- /dev/null
+++ b/libavfilter/af_mix.c
@@ -0,0 +1,231 @@
+/*
+ * Copyright (c) 2011 Matthieu Bouron
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * audio mix filter
+ */
+
+#include "libavutil/audioconvert.h"
+#include "libavutil/eval.h"
+#include "avfilter.h"
+#include "internal.h"
+
+typedef struct {
+    double vol[2];
+    int vol_i[2];
+    int ended[2];
+    int nb_channels;
+    AVFilterBufferRef *buf[2];
+    int buf_pos[2];
+} MixContext;
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    int i, n;
+    double vol1, vol2;
+    MixContext *mix = ctx->priv;
+
+    mix->vol[0] = 0.5;
+    mix->vol[1] = 0.5;
+
+    if (args) {
+        n = sscanf(args, "%lf:%lf", &vol1, &vol2);
+        switch (n) {
+        case 0:
+            mix->vol[0] = 0.5;
+            mix->vol[1] = 0.5;
+            break;
+        case 1:
+            mix->vol[0] = vol1;
+            mix->vol[1] = 0.5;
+            break;
+        case 2:
+            mix->vol[0] = vol1;
+            mix->vol[1] = vol2;
+            break;
+        default:
+            av_log(ctx, AV_LOG_ERROR, "sscanf failed to read input");
+            return 1;
+        }
+    }
+
+    for (i = 0; i < 2; i++) {
+        mix->vol_i[i] = (int)(mix->vol[i] * 256 + 0.5);
+    }
+
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    int i;
+    MixContext *mix = ctx->priv;
+    AVFilterFormats *formats = NULL;
+    const enum AVSampleFormat sample_fmts[] = {
+        AV_SAMPLE_FMT_S16,
+        AV_SAMPLE_FMT_NONE
+    };
+    const int packing_fmts[] = { AVFILTER_PACKED, -1 };
+    int layout_fmts[] = {0, -1};
+
+    for (i = 0; i < 2; i++) {
+        if (!ctx->inputs[i]->in_chlayouts->format_count) {
+            av_log(ctx, AV_LOG_ERROR,
+                   "could not find format for input %d", i);
+            return 1;
+        }
+    }
+
+    layout_fmts[0] = FFMAX(ctx->inputs[0]->in_chlayouts->formats[0],
+                           ctx->inputs[1]->in_chlayouts->formats[0]);
+
+    mix->nb_channels = av_get_channel_layout_nb_channels(layout_fmts[0]);
+
+    formats = avfilter_make_format_list(layout_fmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    avfilter_set_common_channel_layouts(ctx, formats);
+
+    formats = avfilter_make_format_list(sample_fmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    avfilter_set_common_sample_formats(ctx, formats);
+
+    formats = avfilter_make_format_list(packing_fmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    avfilter_set_common_packing_formats(ctx, formats);
+
+    return 0;
+}
+
+static int config_output(AVFilterLink *samples)
+{
+    return 0;
+}
+
+static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+{
+    AVFilterContext *ctx = inlink->dst;
+    MixContext *mix = inlink->dst->priv;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    AVFilterBufferRef *outsamples;
+    int i, pos;
+    int nb_samples;
+    int nb_samples_per_channel;
+    int nb_samples_input1 = INT_MAX;
+    int nb_samples_input2 = INT_MAX;
+
+    pos = inlink == ctx->inputs[1];
+    mix->buf[pos] = avfilter_ref_buffer(insamples, AV_PERM_READ);
+    mix->buf_pos[pos] = 0;
+
+    // If opposite buffer is empty, wait for it.
+    if (!mix->buf[!pos] && !mix->ended[!pos])
+        return;
+
+    if (!mix->ended[0])
+        nb_samples_input1 = mix->buf[0]->audio->nb_samples - mix->buf_pos[0];
+    if (!mix->ended[1])
+        nb_samples_input2 = mix->buf[1]->audio->nb_samples - mix->buf_pos[1];
+
+    nb_samples_per_channel = FFMIN(nb_samples_input1, nb_samples_input2);
+    nb_samples = nb_samples_per_channel * mix->nb_channels;
+
+    outsamples = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples_per_channel);
+    avfilter_copy_buffer_ref_props(outsamples, insamples);
+
+    if (insamples->format == AV_SAMPLE_FMT_S16) {
+        const int16_t *in1 = NULL;
+        const int16_t *in2 = NULL;
+        int16_t *out = NULL;
+
+        if (!mix->ended[0])
+            in1 = (int16_t*)mix->buf[0]->data[0];
+        if (!mix->ended[1])
+            in2 = (int16_t*)mix->buf[1]->data[0];
+        out = (int16_t*)outsamples->data[0];
+
+        for (i = 0; i < nb_samples; i++) {
+            int64_t v1 = 0;
+            int64_t v2 = 0;
+            if (!mix->ended[0])
+                v1 = (((int64_t)*in1++ * mix->vol_i[0] + 128) >> 8);
+            if (!mix->ended[1])
+                v2 = (((int64_t)*in2++ * mix->vol_i[1] + 128) >> 8);
+            *out++ = av_clip_int16(v1 + v2);
+        }
+
+        for (i = 0; i < 2; i++) {
+            if (!mix->ended[i]) {
+                mix->buf_pos[i] += nb_samples;
+                if (mix->buf_pos[i] == nb_samples) {
+                    avfilter_unref_buffer(mix->buf[i]);
+                    mix->buf[i] = 0;
+                    mix->buf_pos[i] = 0;
+                }
+            }
+        }
+    }
+
+    avfilter_filter_samples(outlink, outsamples);
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    MixContext *mix = ctx->priv;
+    int i;
+
+    for (i = 0; i < 2; i++) {
+        if (!mix->buf_pos[i]) {
+            if (avfilter_request_frame(ctx->inputs[i]) != 0) {
+                mix->ended[i] = 1;
+            }
+        }
+    }
+    return 0;
+}
+
+AVFilter avfilter_af_mix = {
+    .name           = "mix",
+    .description    = NULL_IF_CONFIG_SMALL("Mix two audio streams"),
+    .query_formats  = query_formats,
+    .priv_size      = sizeof(MixContext),
+    .init           = init,
+
+    .inputs  = (const AVFilterPad[])  {{ .name     = "in1",
+                                   .type           = AVMEDIA_TYPE_AUDIO,
+                                   .filter_samples = filter_samples,
+                                   .min_perms      = AV_PERM_READ},
+                                 { .name           = "in2",
+                                   .type           = AVMEDIA_TYPE_AUDIO,
+                                   .filter_samples = filter_samples,
+                                   .min_perms      = AV_PERM_READ},
+
+                                 { .name = NULL}},
+
+    .outputs = (const AVFilterPad[])  {{ .name     = "default",
+                                   .type           = AVMEDIA_TYPE_AUDIO,
+                                   .config_props   = config_output,
+                                   .request_frame  = request_frame},
+                                 { .name = NULL}},
+};
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index fb50a3a..b36f63f 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -42,6 +42,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (EARWAX,      earwax,      af);
     REGISTER_FILTER (PAN,         pan,         af);
     REGISTER_FILTER (VOLUME,      volume,      af);
+    REGISTER_FILTER (MIX,         mix,         af);
 
     REGISTER_FILTER (ABUFFER,     abuffer,     asrc);
     REGISTER_FILTER (AEVALSRC,    aevalsrc,    asrc);
diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h
index 4aceebd..c827458 100644
--- a/libavfilter/avfilter.h
+++ b/libavfilter/avfilter.h
@@ -29,7 +29,7 @@
 #include "libavutil/rational.h"
 
 #define LIBAVFILTER_VERSION_MAJOR  2
-#define LIBAVFILTER_VERSION_MINOR 53
+#define LIBAVFILTER_VERSION_MINOR 54
 #define LIBAVFILTER_VERSION_MICRO  0
 
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
-- 
1.7.7.3

