diff mbox series

[FFmpeg-devel] avfilter/af_amix: add sum option

Message ID 20210203194025.3558-1-onemda@gmail.com
State Accepted
Headers show
Series [FFmpeg-devel] avfilter/af_amix: add sum option | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Paul B Mahol Feb. 3, 2021, 7:40 p.m. UTC
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 doc/filters.texi      |  6 ++++++
 libavfilter/af_amix.c | 13 ++++++++++---
 2 files changed, 16 insertions(+), 3 deletions(-)

Comments

Paul B Mahol Feb. 4, 2021, 11:43 a.m. UTC | #1
Will apply soon.
diff mbox series

Patch

diff --git a/doc/filters.texi b/doc/filters.texi
index 2d85a414ec..b4c4e330f4 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -1879,6 +1879,11 @@  stream ends. The default value is 2 seconds.
 @item weights
 Specify weight of each input audio stream as sequence.
 Each weight is separated by space. By default all inputs have same weight.
+
+@item sum
+Do not scale inputs but instead do only summation of samples.
+Beware of heavy clipping if inputs are not normalized prior to filtering.
+By default is disabled.
 @end table
 
 @subsection Commands
@@ -1886,6 +1891,7 @@  Each weight is separated by space. By default all inputs have same weight.
 This filter supports the following commands:
 @table @option
 @item weights
+@item sum
 Syntax is same as option with same name.
 @end table
 
diff --git a/libavfilter/af_amix.c b/libavfilter/af_amix.c
index c4d8916a57..beaf7bcada 100644
--- a/libavfilter/af_amix.c
+++ b/libavfilter/af_amix.c
@@ -164,6 +164,7 @@  typedef struct MixContext {
     int duration_mode;          /**< mode for determining duration */
     float dropout_transition;   /**< transition time when an input drops out */
     char *weights_str;          /**< string for custom weights for every input */
+    int sum;                    /**< inputs are not scaled, only added */
 
     int nb_channels;            /**< number of channels */
     int sample_rate;            /**< sample rate */
@@ -195,6 +196,8 @@  static const AVOption amix_options[] = {
             OFFSET(dropout_transition), AV_OPT_TYPE_FLOAT, { .dbl = 2.0 }, 0, INT_MAX, A|F },
     { "weights", "Set weight for each input.",
             OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, A|F|T },
+    { "sum", "Do not scale inputs instead do only sum",
+            OFFSET(sum), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A|F|T },
     { NULL }
 };
 
@@ -227,10 +230,14 @@  static void calculate_scales(MixContext *s, int nb_samples)
     }
 
     for (i = 0; i < s->nb_inputs; i++) {
-        if (s->input_state[i] & INPUT_ON)
-            s->input_scale[i] = 1.0f / s->scale_norm[i] * FFSIGN(s->weights[i]);
-        else
+        if (s->input_state[i] & INPUT_ON) {
+            if (s->sum)
+                s->input_scale[i] = 1.0f;
+            else
+                s->input_scale[i] = 1.0f / s->scale_norm[i] * FFSIGN(s->weights[i]);
+        } else {
             s->input_scale[i] = 0.0f;
+        }
     }
 }