GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/af_agate.c Lines: 69 174 39.7 %
Date: 2020-09-25 23:16:12 Branches: 28 112 25.0 %

Line Branch Exec Source
1
/*
2
 * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen, Damien Zammit
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 GNU
14
 * 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 (Sidechain) Gate filter
24
 */
25
26
#include "libavutil/audio_fifo.h"
27
#include "libavutil/avassert.h"
28
#include "libavutil/channel_layout.h"
29
#include "libavutil/opt.h"
30
#include "avfilter.h"
31
#include "audio.h"
32
#include "filters.h"
33
#include "formats.h"
34
#include "hermite.h"
35
36
typedef struct AudioGateContext {
37
    const AVClass *class;
38
39
    double level_in;
40
    double level_sc;
41
    double attack;
42
    double release;
43
    double threshold;
44
    double ratio;
45
    double knee;
46
    double makeup;
47
    double range;
48
    int link;
49
    int detection;
50
    int mode;
51
52
    double thres;
53
    double knee_start;
54
    double knee_stop;
55
    double lin_knee_start;
56
    double lin_knee_stop;
57
    double lin_slope;
58
    double attack_coeff;
59
    double release_coeff;
60
61
    AVAudioFifo *fifo[2];
62
    int64_t pts;
63
} AudioGateContext;
64
65
#define OFFSET(x) offsetof(AudioGateContext, x)
66
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
67
68
static const AVOption options[] = {
69
    { "level_in",  "set input level",        OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=1},           0.015625,   64, A },
70
    { "mode",      "set mode",               OFFSET(mode),      AV_OPT_TYPE_INT,    {.i64=0},           0, 1, A, "mode" },
71
    {   "downward",0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=0},           0, 0, A, "mode" },
72
    {   "upward",  0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=1},           0, 0, A, "mode" },
73
    { "range",     "set max gain reduction", OFFSET(range),     AV_OPT_TYPE_DOUBLE, {.dbl=0.06125},     0, 1, A },
74
    { "threshold", "set threshold",          OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125},       0, 1, A },
75
    { "ratio",     "set ratio",              OFFSET(ratio),     AV_OPT_TYPE_DOUBLE, {.dbl=2},           1,  9000, A },
76
    { "attack",    "set attack",             OFFSET(attack),    AV_OPT_TYPE_DOUBLE, {.dbl=20},          0.01, 9000, A },
77
    { "release",   "set release",            OFFSET(release),   AV_OPT_TYPE_DOUBLE, {.dbl=250},         0.01, 9000, A },
78
    { "makeup",    "set makeup gain",        OFFSET(makeup),    AV_OPT_TYPE_DOUBLE, {.dbl=1},           1,   64, A },
79
    { "knee",      "set knee",               OFFSET(knee),      AV_OPT_TYPE_DOUBLE, {.dbl=2.828427125}, 1,    8, A },
80
    { "detection", "set detection",          OFFSET(detection), AV_OPT_TYPE_INT,    {.i64=1},           0,    1, A, "detection" },
81
    {   "peak",    0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=0},           0,    0, A, "detection" },
82
    {   "rms",     0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=1},           0,    0, A, "detection" },
83
    { "link",      "set link",               OFFSET(link),      AV_OPT_TYPE_INT,    {.i64=0},           0,    1, A, "link" },
84
    {   "average", 0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=0},           0,    0, A, "link" },
85
    {   "maximum", 0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=1},           0,    0, A, "link" },
86
    { "level_sc",  "set sidechain gain",     OFFSET(level_sc),  AV_OPT_TYPE_DOUBLE, {.dbl=1},           0.015625,   64, A },
87
    { NULL }
88
};
89
90
1
static int agate_config_input(AVFilterLink *inlink)
91
{
92
1
    AVFilterContext *ctx = inlink->dst;
93
1
    AudioGateContext *s = ctx->priv;
94
1
    double lin_threshold = s->threshold;
95
1
    double lin_knee_sqrt = sqrt(s->knee);
96
97
1
    if (s->detection)
98
1
        lin_threshold *= lin_threshold;
99
100
1
    s->attack_coeff  = FFMIN(1., 1. / (s->attack * inlink->sample_rate / 4000.));
101
1
    s->release_coeff = FFMIN(1., 1. / (s->release * inlink->sample_rate / 4000.));
102
1
    s->lin_knee_stop = lin_threshold * lin_knee_sqrt;
103
1
    s->lin_knee_start = lin_threshold / lin_knee_sqrt;
104
1
    s->thres = log(lin_threshold);
105
1
    s->knee_start = log(s->lin_knee_start);
106
1
    s->knee_stop = log(s->lin_knee_stop);
107
108
1
    return 0;
109
}
110
111
// A fake infinity value (because real infinity may break some hosts)
112
#define FAKE_INFINITY (65536.0 * 65536.0)
113
114
// Check for infinity (with appropriate-ish tolerance)
115
#define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
116
117
1
static double output_gain(double lin_slope, double ratio, double thres,
118
                          double knee, double knee_start, double knee_stop,
119
                          double range, int mode)
120
{
121
1
    double slope = log(lin_slope);
122
1
    double tratio = ratio;
123
1
    double gain = 0.;
124
1
    double delta = 0.;
125
126
1
    if (IS_FAKE_INFINITY(ratio))
127
        tratio = 1000.;
128
1
    gain = (slope - thres) * tratio + thres;
129
1
    delta = tratio;
130
131
1
    if (mode) {
132
        if (knee > 1. && slope < knee_stop)
133
            gain = hermite_interpolation(slope, knee_stop, knee_start, ((knee_stop - thres) * tratio  + thres), knee_start, delta, 1.);
134
    } else {
135

1
        if (knee > 1. && slope > knee_start)
136
            gain = hermite_interpolation(slope, knee_start, knee_stop, ((knee_start - thres) * tratio  + thres), knee_stop, delta, 1.);
137
    }
138
1
    return FFMAX(range, exp(gain - slope));
139
}
140
141
259
static void gate(AudioGateContext *s,
142
                 const double *src, double *dst, const double *scsrc,
143
                 int nb_samples, double level_in, double level_sc,
144
                 AVFilterLink *inlink, AVFilterLink *sclink)
145
{
146
259
    const double makeup = s->makeup;
147
259
    const double attack_coeff = s->attack_coeff;
148
259
    const double release_coeff = s->release_coeff;
149
    int n, c;
150
151
264859
    for (n = 0; n < nb_samples; n++, src += inlink->channels, dst += inlink->channels, scsrc += sclink->channels) {
152
264600
        double abs_sample = fabs(scsrc[0] * level_sc), gain = 1.0;
153
        int detected;
154
155
264600
        if (s->link == 1) {
156
            for (c = 1; c < sclink->channels; c++)
157
                abs_sample = FFMAX(fabs(scsrc[c] * level_sc), abs_sample);
158
        } else {
159
529200
            for (c = 1; c < sclink->channels; c++)
160
264600
                abs_sample += fabs(scsrc[c] * level_sc);
161
162
264600
            abs_sample /= sclink->channels;
163
        }
164
165
264600
        if (s->detection)
166
264600
            abs_sample *= abs_sample;
167
168
264600
        s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? attack_coeff : release_coeff);
169
170
264600
        if (s->mode)
171
            detected = s->lin_slope > s->lin_knee_start;
172
        else
173
264600
            detected = s->lin_slope < s->lin_knee_stop;
174
175

264600
        if (s->lin_slope > 0.0 && detected)
176
1
            gain = output_gain(s->lin_slope, s->ratio, s->thres,
177
                               s->knee, s->knee_start, s->knee_stop,
178
                               s->range, s->mode);
179
180
793800
        for (c = 0; c < inlink->channels; c++)
181
529200
            dst[c] = src[c] * level_in * gain * makeup;
182
    }
183
259
}
184
185
#if CONFIG_AGATE_FILTER
186
187
#define agate_options options
188
AVFILTER_DEFINE_CLASS(agate);
189
190
1
static int query_formats(AVFilterContext *ctx)
191
{
192
1
    AVFilterFormats *formats = NULL;
193
    AVFilterChannelLayouts *layouts;
194
    int ret;
195
196
1
    if ((ret = ff_add_format(&formats, AV_SAMPLE_FMT_DBL)) < 0)
197
        return ret;
198
1
    ret = ff_set_common_formats(ctx, formats);
199
1
    if (ret < 0)
200
        return ret;
201
202
1
    layouts = ff_all_channel_counts();
203
1
    if (!layouts)
204
        return AVERROR(ENOMEM);
205
1
    ret = ff_set_common_channel_layouts(ctx, layouts);
206
1
    if (ret < 0)
207
        return ret;
208
209
1
    formats = ff_all_samplerates();
210
1
    if (!formats)
211
        return AVERROR(ENOMEM);
212
213
1
    return ff_set_common_samplerates(ctx, formats);
214
}
215
216
259
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
217
{
218
259
    const double *src = (const double *)in->data[0];
219
259
    AVFilterContext *ctx = inlink->dst;
220
259
    AVFilterLink *outlink = ctx->outputs[0];
221
259
    AudioGateContext *s = ctx->priv;
222
    AVFrame *out;
223
    double *dst;
224
225
259
    if (av_frame_is_writable(in)) {
226
259
        out = in;
227
    } else {
228
        out = ff_get_audio_buffer(outlink, in->nb_samples);
229
        if (!out) {
230
            av_frame_free(&in);
231
            return AVERROR(ENOMEM);
232
        }
233
        av_frame_copy_props(out, in);
234
    }
235
259
    dst = (double *)out->data[0];
236
237
259
    gate(s, src, dst, src, in->nb_samples,
238
         s->level_in, s->level_in, inlink, inlink);
239
240
259
    if (out != in)
241
        av_frame_free(&in);
242
259
    return ff_filter_frame(outlink, out);
243
}
244
245
static const AVFilterPad inputs[] = {
246
    {
247
        .name         = "default",
248
        .type         = AVMEDIA_TYPE_AUDIO,
249
        .filter_frame = filter_frame,
250
        .config_props = agate_config_input,
251
    },
252
    { NULL }
253
};
254
255
static const AVFilterPad outputs[] = {
256
    {
257
        .name = "default",
258
        .type = AVMEDIA_TYPE_AUDIO,
259
    },
260
    { NULL }
261
};
262
263
AVFilter ff_af_agate = {
264
    .name           = "agate",
265
    .description    = NULL_IF_CONFIG_SMALL("Audio gate."),
266
    .query_formats  = query_formats,
267
    .priv_size      = sizeof(AudioGateContext),
268
    .priv_class     = &agate_class,
269
    .inputs         = inputs,
270
    .outputs        = outputs,
271
};
272
273
#endif /* CONFIG_AGATE_FILTER */
274
275
#if CONFIG_SIDECHAINGATE_FILTER
276
277
#define sidechaingate_options options
278
AVFILTER_DEFINE_CLASS(sidechaingate);
279
280
static int activate(AVFilterContext *ctx)
281
{
282
    AudioGateContext *s = ctx->priv;
283
    AVFrame *out = NULL, *in[2] = { NULL };
284
    int ret, i, nb_samples;
285
    double *dst;
286
287
    FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
288
    if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &in[0])) > 0) {
289
        av_audio_fifo_write(s->fifo[0], (void **)in[0]->extended_data,
290
                            in[0]->nb_samples);
291
        av_frame_free(&in[0]);
292
    }
293
    if (ret < 0)
294
        return ret;
295
    if ((ret = ff_inlink_consume_frame(ctx->inputs[1], &in[1])) > 0) {
296
        av_audio_fifo_write(s->fifo[1], (void **)in[1]->extended_data,
297
                            in[1]->nb_samples);
298
        av_frame_free(&in[1]);
299
    }
300
    if (ret < 0)
301
        return ret;
302
303
    nb_samples = FFMIN(av_audio_fifo_size(s->fifo[0]), av_audio_fifo_size(s->fifo[1]));
304
    if (nb_samples) {
305
        out = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
306
        if (!out)
307
            return AVERROR(ENOMEM);
308
        for (i = 0; i < 2; i++) {
309
            in[i] = ff_get_audio_buffer(ctx->inputs[i], nb_samples);
310
            if (!in[i]) {
311
                av_frame_free(&in[0]);
312
                av_frame_free(&in[1]);
313
                av_frame_free(&out);
314
                return AVERROR(ENOMEM);
315
            }
316
            av_audio_fifo_read(s->fifo[i], (void **)in[i]->data, nb_samples);
317
        }
318
319
        dst = (double *)out->data[0];
320
        out->pts = s->pts;
321
        s->pts += av_rescale_q(nb_samples, (AVRational){1, ctx->outputs[0]->sample_rate}, ctx->outputs[0]->time_base);
322
323
        gate(s, (double *)in[0]->data[0], dst,
324
             (double *)in[1]->data[0], nb_samples,
325
             s->level_in, s->level_sc,
326
             ctx->inputs[0], ctx->inputs[1]);
327
328
        av_frame_free(&in[0]);
329
        av_frame_free(&in[1]);
330
331
        ret = ff_filter_frame(ctx->outputs[0], out);
332
        if (ret < 0)
333
            return ret;
334
    }
335
    FF_FILTER_FORWARD_STATUS(ctx->inputs[0], ctx->outputs[0]);
336
    FF_FILTER_FORWARD_STATUS(ctx->inputs[1], ctx->outputs[0]);
337
    if (ff_outlink_frame_wanted(ctx->outputs[0])) {
338
        if (!av_audio_fifo_size(s->fifo[0]))
339
            ff_inlink_request_frame(ctx->inputs[0]);
340
        if (!av_audio_fifo_size(s->fifo[1]))
341
            ff_inlink_request_frame(ctx->inputs[1]);
342
    }
343
    return 0;
344
}
345
346
static int scquery_formats(AVFilterContext *ctx)
347
{
348
    AVFilterFormats *formats;
349
    AVFilterChannelLayouts *layouts = NULL;
350
    static const enum AVSampleFormat sample_fmts[] = {
351
        AV_SAMPLE_FMT_DBL,
352
        AV_SAMPLE_FMT_NONE
353
    };
354
    int ret, i;
355
356
    if (!ctx->inputs[0]->incfg.channel_layouts ||
357
        !ctx->inputs[0]->incfg.channel_layouts->nb_channel_layouts) {
358
        av_log(ctx, AV_LOG_WARNING,
359
               "No channel layout for input 1\n");
360
            return AVERROR(EAGAIN);
361
    }
362
363
    if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->incfg.channel_layouts->channel_layouts[0])) < 0 ||
364
        (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->incfg.channel_layouts)) < 0)
365
        return ret;
366
367
    for (i = 0; i < 2; i++) {
368
        layouts = ff_all_channel_counts();
369
        if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->outcfg.channel_layouts)) < 0)
370
            return ret;
371
    }
372
373
    formats = ff_make_format_list(sample_fmts);
374
    if ((ret = ff_set_common_formats(ctx, formats)) < 0)
375
        return ret;
376
377
    formats = ff_all_samplerates();
378
    return ff_set_common_samplerates(ctx, formats);
379
}
380
381
static int scconfig_output(AVFilterLink *outlink)
382
{
383
    AVFilterContext *ctx = outlink->src;
384
    AudioGateContext *s = ctx->priv;
385
386
    if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
387
        av_log(ctx, AV_LOG_ERROR,
388
               "Inputs must have the same sample rate "
389
               "%d for in0 vs %d for in1\n",
390
               ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
391
        return AVERROR(EINVAL);
392
    }
393
394
    outlink->sample_rate = ctx->inputs[0]->sample_rate;
395
    outlink->time_base   = ctx->inputs[0]->time_base;
396
    outlink->channel_layout = ctx->inputs[0]->channel_layout;
397
    outlink->channels = ctx->inputs[0]->channels;
398
399
    s->fifo[0] = av_audio_fifo_alloc(ctx->inputs[0]->format, ctx->inputs[0]->channels, 1024);
400
    s->fifo[1] = av_audio_fifo_alloc(ctx->inputs[1]->format, ctx->inputs[1]->channels, 1024);
401
    if (!s->fifo[0] || !s->fifo[1])
402
        return AVERROR(ENOMEM);
403
404
405
    agate_config_input(ctx->inputs[0]);
406
407
    return 0;
408
}
409
410
static av_cold void uninit(AVFilterContext *ctx)
411
{
412
    AudioGateContext *s = ctx->priv;
413
414
    av_audio_fifo_free(s->fifo[0]);
415
    av_audio_fifo_free(s->fifo[1]);
416
}
417
418
static const AVFilterPad sidechaingate_inputs[] = {
419
    {
420
        .name           = "main",
421
        .type           = AVMEDIA_TYPE_AUDIO,
422
    },{
423
        .name           = "sidechain",
424
        .type           = AVMEDIA_TYPE_AUDIO,
425
    },
426
    { NULL }
427
};
428
429
static const AVFilterPad sidechaingate_outputs[] = {
430
    {
431
        .name          = "default",
432
        .type          = AVMEDIA_TYPE_AUDIO,
433
        .config_props  = scconfig_output,
434
    },
435
    { NULL }
436
};
437
438
AVFilter ff_af_sidechaingate = {
439
    .name           = "sidechaingate",
440
    .description    = NULL_IF_CONFIG_SMALL("Audio sidechain gate."),
441
    .priv_size      = sizeof(AudioGateContext),
442
    .priv_class     = &sidechaingate_class,
443
    .query_formats  = scquery_formats,
444
    .activate       = activate,
445
    .uninit         = uninit,
446
    .inputs         = sidechaingate_inputs,
447
    .outputs        = sidechaingate_outputs,
448
};
449
#endif  /* CONFIG_SIDECHAINGATE_FILTER */