Browse code

avfilter: add sidechaingate filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2015/11/30 07:22:47
Showing 6 changed files
... ...
@@ -38,6 +38,7 @@ version <next>:
38 38
 - acompressor filter
39 39
 - support encoding 16-bit RLE SGI images
40 40
 - apulsator filter
41
+- sidechaingate audio filter
41 42
 
42 43
 
43 44
 version 2.8:
... ...
@@ -2597,6 +2597,67 @@ ffmpeg -i main.flac -i sidechain.flac -filter_complex "[1:a]asplit=2[sc][mix];[0
2597 2597
 @end example
2598 2598
 @end itemize
2599 2599
 
2600
+@section sidechaingate
2601
+
2602
+A sidechain gate acts like a normal (wideband) gate but has the ability to
2603
+filter the detected signal before sending it to the gain reduction stage.
2604
+Normally a gate uses the full range signal to detect a level above the
2605
+threshold.
2606
+For example: If you cut all lower frequencies from your sidechain signal
2607
+the gate will decrease the volume of your track only if not enough highs
2608
+appear. With this technique you are able to reduce the resonation of a
2609
+natural drum or remove "rumbling" of muted strokes from a heavily distorted
2610
+guitar.
2611
+It needs two input streams and returns one output stream.
2612
+First input stream will be processed depending on second stream signal.
2613
+
2614
+The filter accepts the following options:
2615
+
2616
+@table @option
2617
+@item level_in
2618
+Set input level before filtering.
2619
+Default is 1. Allowed range is from 0.015625 to 64.
2620
+
2621
+@item range
2622
+Set the level of gain reduction when the signal is below the threshold.
2623
+Default is 0.06125. Allowed range is from 0 to 1.
2624
+
2625
+@item threshold
2626
+If a signal rises above this level the gain reduction is released.
2627
+Default is 0.125. Allowed range is from 0 to 1.
2628
+
2629
+@item ratio
2630
+Set a ratio about which the signal is reduced.
2631
+Default is 2. Allowed range is from 1 to 9000.
2632
+
2633
+@item attack
2634
+Amount of milliseconds the signal has to rise above the threshold before gain
2635
+reduction stops.
2636
+Default is 20 milliseconds. Allowed range is from 0.01 to 9000.
2637
+
2638
+@item release
2639
+Amount of milliseconds the signal has to fall below the threshold before the
2640
+reduction is increased again. Default is 250 milliseconds.
2641
+Allowed range is from 0.01 to 9000.
2642
+
2643
+@item makeup
2644
+Set amount of amplification of signal after processing.
2645
+Default is 1. Allowed range is from 1 to 64.
2646
+
2647
+@item knee
2648
+Curve the sharp knee around the threshold to enter gain reduction more softly.
2649
+Default is 2.828427125. Allowed range is from 1 to 8.
2650
+
2651
+@item detection
2652
+Choose if exact signal should be taken for detection or an RMS like one.
2653
+Default is peak. Can be peak or rms.
2654
+
2655
+@item link
2656
+Choose if the average level between all channels or the louder channel affects
2657
+the reduction.
2658
+Default is average. Can be average or maximum.
2659
+@end table
2660
+
2600 2661
 @section silencedetect
2601 2662
 
2602 2663
 Detect silence in an audio stream.
... ...
@@ -83,6 +83,7 @@ OBJS-$(CONFIG_REPLAYGAIN_FILTER)             += af_replaygain.o
83 83
 OBJS-$(CONFIG_RESAMPLE_FILTER)               += af_resample.o
84 84
 OBJS-$(CONFIG_RUBBERBAND_FILTER)             += af_rubberband.o
85 85
 OBJS-$(CONFIG_SIDECHAINCOMPRESS_FILTER)      += af_sidechaincompress.o
86
+OBJS-$(CONFIG_SIDECHAINGATE_FILTER)          += af_agate.o
86 87
 OBJS-$(CONFIG_SILENCEDETECT_FILTER)          += af_silencedetect.o
87 88
 OBJS-$(CONFIG_SILENCEREMOVE_FILTER)          += af_silenceremove.o
88 89
 OBJS-$(CONFIG_STEREOTOOLS_FILTER)            += af_stereotools.o
... ...
@@ -18,6 +18,12 @@
18 18
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 19
  */
20 20
 
21
+/**
22
+ * @file
23
+ * Audio (Sidechain) Gate filter
24
+ */
25
+
26
+#include "libavutil/avassert.h"
21 27
 #include "libavutil/channel_layout.h"
22 28
 #include "libavutil/opt.h"
23 29
 #include "avfilter.h"
... ...
@@ -46,12 +52,14 @@ typedef struct AudioGateContext {
46 46
     double lin_slope;
47 47
     double attack_coeff;
48 48
     double release_coeff;
49
+
50
+    AVFrame *input_frame[2];
49 51
 } AudioGateContext;
50 52
 
51 53
 #define OFFSET(x) offsetof(AudioGateContext, x)
52 54
 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
53 55
 
54
-static const AVOption agate_options[] = {
56
+static const AVOption options[] = {
55 57
     { "level_in",  "set input level",        OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=1},           0.015625,   64, A },
56 58
     { "range",     "set max gain reduction", OFFSET(range),     AV_OPT_TYPE_DOUBLE, {.dbl=0.06125},     0, 1, A },
57 59
     { "threshold", "set threshold",          OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125},       0, 1, A },
... ...
@@ -69,6 +77,7 @@ static const AVOption agate_options[] = {
69 69
     { NULL }
70 70
 };
71 71
 
72
+#define agate_options options
72 73
 AVFILTER_DEFINE_CLASS(agate);
73 74
 
74 75
 static int query_formats(AVFilterContext *ctx)
... ...
@@ -97,7 +106,7 @@ static int query_formats(AVFilterContext *ctx)
97 97
     return ff_set_common_samplerates(ctx, formats);
98 98
 }
99 99
 
100
-static int config_input(AVFilterLink *inlink)
100
+static int agate_config_input(AVFilterLink *inlink)
101 101
 {
102 102
     AVFilterContext *ctx = inlink->dst;
103 103
     AudioGateContext *s = ctx->priv;
... ...
@@ -221,7 +230,7 @@ static const AVFilterPad inputs[] = {
221 221
         .name         = "default",
222 222
         .type         = AVMEDIA_TYPE_AUDIO,
223 223
         .filter_frame = filter_frame,
224
-        .config_props = config_input,
224
+        .config_props = agate_config_input,
225 225
     },
226 226
     { NULL }
227 227
 };
... ...
@@ -243,3 +252,158 @@ AVFilter ff_af_agate = {
243 243
     .inputs         = inputs,
244 244
     .outputs        = outputs,
245 245
 };
246
+
247
+#if CONFIG_SIDECHAINGATE_FILTER
248
+
249
+#define sidechaingate_options options
250
+AVFILTER_DEFINE_CLASS(sidechaingate);
251
+
252
+static int scfilter_frame(AVFilterLink *link, AVFrame *in)
253
+{
254
+    AVFilterContext *ctx = link->dst;
255
+    AudioGateContext *s = ctx->priv;
256
+    AVFilterLink *outlink = ctx->outputs[0];
257
+    const double *scsrc;
258
+    double *sample;
259
+    int nb_samples;
260
+    int ret, i;
261
+
262
+    for (i = 0; i < 2; i++)
263
+        if (link == ctx->inputs[i])
264
+            break;
265
+    av_assert0(i < 2 && !s->input_frame[i]);
266
+    s->input_frame[i] = in;
267
+
268
+    if (!s->input_frame[0] || !s->input_frame[1])
269
+        return 0;
270
+
271
+    nb_samples = FFMIN(s->input_frame[0]->nb_samples,
272
+                       s->input_frame[1]->nb_samples);
273
+
274
+    sample = (double *)s->input_frame[0]->data[0];
275
+    scsrc = (const double *)s->input_frame[1]->data[0];
276
+
277
+    gate(s, sample, sample, scsrc, nb_samples,
278
+         ctx->inputs[0], ctx->inputs[1]);
279
+    ret = ff_filter_frame(outlink, s->input_frame[0]);
280
+
281
+    s->input_frame[0] = NULL;
282
+    av_frame_free(&s->input_frame[1]);
283
+
284
+    return ret;
285
+}
286
+
287
+static int screquest_frame(AVFilterLink *outlink)
288
+{
289
+    AVFilterContext *ctx = outlink->src;
290
+    AudioGateContext *s = ctx->priv;
291
+    int i, ret;
292
+
293
+    /* get a frame on each input */
294
+    for (i = 0; i < 2; i++) {
295
+        AVFilterLink *inlink = ctx->inputs[i];
296
+        if (!s->input_frame[i] &&
297
+            (ret = ff_request_frame(inlink)) < 0)
298
+            return ret;
299
+
300
+        /* request the same number of samples on all inputs */
301
+        if (i == 0)
302
+            ctx->inputs[1]->request_samples = s->input_frame[0]->nb_samples;
303
+    }
304
+
305
+    return 0;
306
+}
307
+
308
+static int scquery_formats(AVFilterContext *ctx)
309
+{
310
+    AVFilterFormats *formats;
311
+    AVFilterChannelLayouts *layouts = NULL;
312
+    static const enum AVSampleFormat sample_fmts[] = {
313
+        AV_SAMPLE_FMT_DBL,
314
+        AV_SAMPLE_FMT_NONE
315
+    };
316
+    int ret, i;
317
+
318
+    if (!ctx->inputs[0]->in_channel_layouts ||
319
+        !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
320
+        av_log(ctx, AV_LOG_WARNING,
321
+               "No channel layout for input 1\n");
322
+            return AVERROR(EAGAIN);
323
+    }
324
+
325
+    if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
326
+        (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
327
+        return ret;
328
+
329
+    for (i = 0; i < 2; i++) {
330
+        layouts = ff_all_channel_counts();
331
+        if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
332
+            return ret;
333
+    }
334
+
335
+    formats = ff_make_format_list(sample_fmts);
336
+    if ((ret = ff_set_common_formats(ctx, formats)) < 0)
337
+        return ret;
338
+
339
+    formats = ff_all_samplerates();
340
+    return ff_set_common_samplerates(ctx, formats);
341
+}
342
+
343
+static int scconfig_output(AVFilterLink *outlink)
344
+{
345
+    AVFilterContext *ctx = outlink->src;
346
+
347
+    if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
348
+        av_log(ctx, AV_LOG_ERROR,
349
+               "Inputs must have the same sample rate "
350
+               "%d for in0 vs %d for in1\n",
351
+               ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
352
+        return AVERROR(EINVAL);
353
+    }
354
+
355
+    outlink->sample_rate = ctx->inputs[0]->sample_rate;
356
+    outlink->time_base   = ctx->inputs[0]->time_base;
357
+    outlink->channel_layout = ctx->inputs[0]->channel_layout;
358
+    outlink->channels = ctx->inputs[0]->channels;
359
+
360
+    agate_config_input(ctx->inputs[0]);
361
+
362
+    return 0;
363
+}
364
+
365
+static const AVFilterPad sidechaingate_inputs[] = {
366
+    {
367
+        .name           = "main",
368
+        .type           = AVMEDIA_TYPE_AUDIO,
369
+        .filter_frame   = scfilter_frame,
370
+        .needs_writable = 1,
371
+        .needs_fifo     = 1,
372
+    },{
373
+        .name           = "sidechain",
374
+        .type           = AVMEDIA_TYPE_AUDIO,
375
+        .filter_frame   = scfilter_frame,
376
+        .needs_fifo     = 1,
377
+    },
378
+    { NULL }
379
+};
380
+
381
+static const AVFilterPad sidechaingate_outputs[] = {
382
+    {
383
+        .name          = "default",
384
+        .type          = AVMEDIA_TYPE_AUDIO,
385
+        .config_props  = scconfig_output,
386
+        .request_frame = screquest_frame,
387
+    },
388
+    { NULL }
389
+};
390
+
391
+AVFilter ff_af_sidechaingate = {
392
+    .name           = "sidechaingate",
393
+    .description    = NULL_IF_CONFIG_SMALL("Audio sidechain gate."),
394
+    .priv_size      = sizeof(AudioGateContext),
395
+    .priv_class     = &sidechaingate_class,
396
+    .query_formats  = scquery_formats,
397
+    .inputs         = sidechaingate_inputs,
398
+    .outputs        = sidechaingate_outputs,
399
+};
400
+#endif  /* CONFIG_SIDECHAINGATE_FILTER */
... ...
@@ -105,6 +105,7 @@ void avfilter_register_all(void)
105 105
     REGISTER_FILTER(RESAMPLE,       resample,       af);
106 106
     REGISTER_FILTER(RUBBERBAND,     rubberband,     af);
107 107
     REGISTER_FILTER(SIDECHAINCOMPRESS, sidechaincompress, af);
108
+    REGISTER_FILTER(SIDECHAINGATE,  sidechaingate,  af);
108 109
     REGISTER_FILTER(SILENCEDETECT,  silencedetect,  af);
109 110
     REGISTER_FILTER(SILENCEREMOVE,  silenceremove,  af);
110 111
     REGISTER_FILTER(STEREOTOOLS,    stereotools,    af);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  18
33
+#define LIBAVFILTER_VERSION_MINOR  19
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \