]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_agate.c
Merge commit 'c1aac39eaccd32dc3b74ccfcce701d3d888fbc6b'
[ffmpeg] / libavfilter / af_agate.c
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 #include "libavutil/channel_layout.h"
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "audio.h"
25 #include "formats.h"
26 #include "hermite.h"
27
28 typedef struct AudioGateContext {
29     const AVClass *class;
30
31     double level_in;
32     double attack;
33     double release;
34     double threshold;
35     double ratio;
36     double knee;
37     double makeup;
38     double range;
39     int link;
40     int detection;
41
42     double thres;
43     double knee_start;
44     double lin_knee_stop;
45     double knee_stop;
46     double lin_slope;
47     double attack_coeff;
48     double release_coeff;
49 } AudioGateContext;
50
51 #define OFFSET(x) offsetof(AudioGateContext, x)
52 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
53
54 static const AVOption agate_options[] = {
55     { "level_in",  "set input level",        OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=1},           0.015625,   64, A },
56     { "range",     "set max gain reduction", OFFSET(range),     AV_OPT_TYPE_DOUBLE, {.dbl=0.06125},     0, 1, A },
57     { "threshold", "set threshold",          OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125},       0, 1, A },
58     { "ratio",     "set ratio",              OFFSET(ratio),     AV_OPT_TYPE_DOUBLE, {.dbl=2},           1,  9000, A },
59     { "attack",    "set attack",             OFFSET(attack),    AV_OPT_TYPE_DOUBLE, {.dbl=20},          0.01, 9000, A },
60     { "release",   "set release",            OFFSET(release),   AV_OPT_TYPE_DOUBLE, {.dbl=250},         0.01, 9000, A },
61     { "makeup",    "set makeup gain",        OFFSET(makeup),    AV_OPT_TYPE_DOUBLE, {.dbl=1},           1,   64, A },
62     { "knee",      "set knee",               OFFSET(knee),      AV_OPT_TYPE_DOUBLE, {.dbl=2.828427125}, 1,    8, A },
63     { "detection", "set detection",          OFFSET(detection), AV_OPT_TYPE_INT,    {.i64=0},           0,    1, A, "detection" },
64     {   "peak",    0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=0},           0,    0, A, "detection" },
65     {   "rms",     0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=1},           0,    0, A, "detection" },
66     { "link",      "set link",               OFFSET(link),      AV_OPT_TYPE_INT,    {.i64=0},           0,    1, A, "link" },
67     {   "average", 0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=0},           0,    0, A, "link" },
68     {   "maximum", 0,                        0,                 AV_OPT_TYPE_CONST,  {.i64=1},           0,    0, A, "link" },
69     { NULL }
70 };
71
72 AVFILTER_DEFINE_CLASS(agate);
73
74 static int query_formats(AVFilterContext *ctx)
75 {
76     AVFilterFormats *formats = NULL;
77     AVFilterChannelLayouts *layouts;
78     int ret;
79
80     ff_add_format(&formats, AV_SAMPLE_FMT_DBL);
81     ret = ff_set_common_formats(ctx, formats);
82     if (ret < 0)
83         return ret;
84
85     layouts = ff_all_channel_counts();
86     if (!layouts)
87         return AVERROR(ENOMEM);
88     ret = ff_set_common_channel_layouts(ctx, layouts);
89     if (ret < 0)
90         return ret;
91
92     formats = ff_all_samplerates();
93     if (!formats)
94         return AVERROR(ENOMEM);
95
96     return ff_set_common_samplerates(ctx, formats);
97 }
98
99 static int config_input(AVFilterLink *inlink)
100 {
101     AVFilterContext *ctx = inlink->dst;
102     AudioGateContext *s = ctx->priv;
103     double lin_threshold = s->threshold;
104     double lin_knee_sqrt = sqrt(s->knee);
105     double lin_knee_start;
106
107     if (s->detection)
108         lin_threshold *= lin_threshold;
109
110     s->attack_coeff  = FFMIN(1., 1. / (s->attack * inlink->sample_rate / 4000.));
111     s->release_coeff = FFMIN(1., 1. / (s->release * inlink->sample_rate / 4000.));
112     s->lin_knee_stop = lin_threshold * lin_knee_sqrt;
113     lin_knee_start = lin_threshold / lin_knee_sqrt;
114     s->thres = log(lin_threshold);
115     s->knee_start = log(lin_knee_start);
116     s->knee_stop = log(s->lin_knee_stop);
117
118     return 0;
119 }
120
121 // A fake infinity value (because real infinity may break some hosts)
122 #define FAKE_INFINITY (65536.0 * 65536.0)
123
124 // Check for infinity (with appropriate-ish tolerance)
125 #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
126
127 static double output_gain(double lin_slope, double ratio, double thres,
128                           double knee, double knee_start, double knee_stop,
129                           double lin_knee_stop, double range)
130 {
131     if (lin_slope < lin_knee_stop) {
132         double slope = log(lin_slope);
133         double tratio = ratio;
134         double gain = 0.;
135         double delta = 0.;
136
137         if (IS_FAKE_INFINITY(ratio))
138             tratio = 1000.;
139         gain = (slope - thres) * tratio + thres;
140         delta = tratio;
141
142         if (knee > 1. && slope > knee_start) {
143             gain = hermite_interpolation(slope, knee_start, knee_stop, ((knee_start - thres) * tratio  + thres), knee_stop, delta, 1.);
144         }
145         return FFMAX(range, exp(gain - slope));
146     }
147
148     return 1.;
149 }
150
151 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
152 {
153     AVFilterContext *ctx = inlink->dst;
154     AVFilterLink *outlink = ctx->outputs[0];
155     AudioGateContext *s = ctx->priv;
156     const double *src = (const double *)in->data[0];
157     const double makeup = s->makeup;
158     const double attack_coeff = s->attack_coeff;
159     const double release_coeff = s->release_coeff;
160     const double level_in = s->level_in;
161     AVFrame *out;
162     double *dst;
163     int n, c;
164
165     if (av_frame_is_writable(in)) {
166         out = in;
167     } else {
168         out = ff_get_audio_buffer(inlink, in->nb_samples);
169         if (!out) {
170             av_frame_free(&in);
171             return AVERROR(ENOMEM);
172         }
173         av_frame_copy_props(out, in);
174     }
175     dst = (double *)out->data[0];
176
177     for (n = 0; n < in->nb_samples; n++, src += inlink->channels, dst += inlink->channels) {
178         double abs_sample = fabs(src[0]), gain = 1.0;
179
180         for (c = 0; c < inlink->channels; c++)
181             dst[c] = src[c] * level_in;
182
183         if (s->link == 1) {
184             for (c = 1; c < inlink->channels; c++)
185                 abs_sample = FFMAX(fabs(src[c]), abs_sample);
186         } else {
187             for (c = 1; c < inlink->channels; c++)
188                 abs_sample += fabs(src[c]);
189
190             abs_sample /= inlink->channels;
191         }
192
193         if (s->detection)
194             abs_sample *= abs_sample;
195
196         s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? attack_coeff : release_coeff);
197         if (s->lin_slope > 0.0)
198             gain = output_gain(s->lin_slope, s->ratio, s->thres,
199                                s->knee, s->knee_start, s->knee_stop,
200                                s->lin_knee_stop, s->range);
201
202         for (c = 0; c < inlink->channels; c++)
203             dst[c] *= gain * makeup;
204     }
205
206     if (out != in)
207         av_frame_free(&in);
208     return ff_filter_frame(outlink, out);
209 }
210
211 static const AVFilterPad inputs[] = {
212     {
213         .name         = "default",
214         .type         = AVMEDIA_TYPE_AUDIO,
215         .filter_frame = filter_frame,
216         .config_props = config_input,
217     },
218     { NULL }
219 };
220
221 static const AVFilterPad outputs[] = {
222     {
223         .name = "default",
224         .type = AVMEDIA_TYPE_AUDIO,
225     },
226     { NULL }
227 };
228
229 AVFilter ff_af_agate = {
230     .name           = "agate",
231     .description    = NULL_IF_CONFIG_SMALL("Audio gate."),
232     .query_formats  = query_formats,
233     .priv_size      = sizeof(AudioGateContext),
234     .priv_class     = &agate_class,
235     .inputs         = inputs,
236     .outputs        = outputs,
237 };