]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_sidechaincompress.c
Merge commit '1339009c4924a20e872aa62897097bf5d071157c'
[ffmpeg] / libavfilter / af_sidechaincompress.c
1 /*
2  * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
3  * Copyright (c) 2015 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Sidechain compressor filter
25  */
26
27 #include "libavutil/avassert.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/common.h"
30 #include "libavutil/opt.h"
31
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "hermite.h"
36 #include "internal.h"
37
38 typedef struct SidechainCompressContext {
39     const AVClass *class;
40
41     double attack, attack_coeff;
42     double release, release_coeff;
43     double lin_slope;
44     double ratio;
45     double threshold;
46     double makeup;
47     double thres;
48     double knee;
49     double knee_start;
50     double knee_stop;
51     double lin_knee_start;
52     double compressed_knee_stop;
53     int link;
54     int detection;
55
56     AVFrame *input_frame[2];
57 } SidechainCompressContext;
58
59 #define OFFSET(x) offsetof(SidechainCompressContext, x)
60 #define A AV_OPT_FLAG_AUDIO_PARAM
61 #define F AV_OPT_FLAG_FILTERING_PARAM
62
63 static const AVOption sidechaincompress_options[] = {
64     { "threshold", "set threshold",    OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0.000976563,    1, A|F },
65     { "ratio",     "set ratio",        OFFSET(ratio),     AV_OPT_TYPE_DOUBLE, {.dbl=2},               1,   20, A|F },
66     { "attack",    "set attack",       OFFSET(attack),    AV_OPT_TYPE_DOUBLE, {.dbl=20},           0.01, 2000, A|F },
67     { "release",   "set release",      OFFSET(release),   AV_OPT_TYPE_DOUBLE, {.dbl=250},          0.01, 9000, A|F },
68     { "makeup",    "set make up gain", OFFSET(makeup),    AV_OPT_TYPE_DOUBLE, {.dbl=2},               1,   64, A|F },
69     { "knee",      "set knee",         OFFSET(knee),      AV_OPT_TYPE_DOUBLE, {.dbl=2.82843},         1,    8, A|F },
70     { "link",      "set link type",    OFFSET(link),      AV_OPT_TYPE_INT,    {.i64=0},               0,    1, A|F, "link" },
71     {   "average", 0,                  0,                 AV_OPT_TYPE_CONST,  {.i64=0},               0,    0, A|F, "link" },
72     {   "maximum", 0,                  0,                 AV_OPT_TYPE_CONST,  {.i64=1},               0,    0, A|F, "link" },
73     { "detection", "set detection",    OFFSET(detection), AV_OPT_TYPE_INT,    {.i64=1},               0,    1, A|F, "detection" },
74     {   "peak",    0,                  0,                 AV_OPT_TYPE_CONST,  {.i64=0},               0,    0, A|F, "detection" },
75     {   "rms",     0,                  0,                 AV_OPT_TYPE_CONST,  {.i64=1},               0,    0, A|F, "detection" },
76     { NULL }
77 };
78
79 AVFILTER_DEFINE_CLASS(sidechaincompress);
80
81 static av_cold int init(AVFilterContext *ctx)
82 {
83     SidechainCompressContext *s = ctx->priv;
84
85     s->thres = log(s->threshold);
86     s->lin_knee_start = s->threshold / sqrt(s->knee);
87     s->knee_start = log(s->lin_knee_start);
88     s->knee_stop = log(s->threshold * sqrt(s->knee));
89     s->compressed_knee_stop = (s->knee_stop - s->thres) / s->ratio + s->thres;
90
91     return 0;
92 }
93
94 // A fake infinity value (because real infinity may break some hosts)
95 #define FAKE_INFINITY (65536.0 * 65536.0)
96
97 // Check for infinity (with appropriate-ish tolerance)
98 #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
99
100 static double output_gain(double lin_slope, double ratio, double thres,
101                           double knee, double knee_start, double knee_stop,
102                           double compressed_knee_stop, int detection)
103 {
104     double slope = log(lin_slope);
105     double gain = 0.0;
106     double delta = 0.0;
107
108     if (detection)
109         slope *= 0.5;
110
111     if (IS_FAKE_INFINITY(ratio)) {
112         gain = thres;
113         delta = 0.0;
114     } else {
115         gain = (slope - thres) / ratio + thres;
116         delta = 1.0 / ratio;
117     }
118
119     if (knee > 1.0 && slope < knee_stop)
120         gain = hermite_interpolation(slope, knee_start, knee_stop,
121                                      knee_start, compressed_knee_stop,
122                                      1.0, delta);
123
124     return exp(gain - slope);
125 }
126
127 static int filter_frame(AVFilterLink *link, AVFrame *frame)
128 {
129     AVFilterContext *ctx = link->dst;
130     SidechainCompressContext *s = ctx->priv;
131     AVFilterLink *sclink = ctx->inputs[1];
132     AVFilterLink *outlink = ctx->outputs[0];
133     const double makeup = s->makeup;
134     const double *scsrc;
135     double *sample;
136     int nb_samples;
137     int ret, i, c;
138
139     for (i = 0; i < 2; i++)
140         if (link == ctx->inputs[i])
141             break;
142     av_assert0(i < 2 && !s->input_frame[i]);
143     s->input_frame[i] = frame;
144
145     if (!s->input_frame[0] || !s->input_frame[1])
146         return 0;
147
148     nb_samples = FFMIN(s->input_frame[0]->nb_samples,
149                        s->input_frame[1]->nb_samples);
150
151     sample = (double *)s->input_frame[0]->data[0];
152     scsrc = (const double *)s->input_frame[1]->data[0];
153
154     for (i = 0; i < nb_samples; i++) {
155         double abs_sample, gain = 1.0;
156
157         abs_sample = fabs(scsrc[0]);
158
159         if (s->link == 1) {
160             for (c = 1; c < sclink->channels; c++)
161                 abs_sample = FFMAX(fabs(scsrc[c]), abs_sample);
162         } else {
163             for (c = 1; c < sclink->channels; c++)
164                 abs_sample += fabs(scsrc[c]);
165
166             abs_sample /= sclink->channels;
167         }
168
169         if (s->detection)
170             abs_sample *= abs_sample;
171
172         s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
173
174         if (s->lin_slope > 0.0 && s->lin_slope > s->lin_knee_start)
175             gain = output_gain(s->lin_slope, s->ratio, s->thres, s->knee,
176                                s->knee_start, s->knee_stop,
177                                s->compressed_knee_stop, s->detection);
178
179         for (c = 0; c < outlink->channels; c++)
180             sample[c] *= gain * makeup;
181
182         sample += outlink->channels;
183         scsrc += sclink->channels;
184     }
185
186     ret = ff_filter_frame(outlink, s->input_frame[0]);
187
188     s->input_frame[0] = NULL;
189     av_frame_free(&s->input_frame[1]);
190
191     return ret;
192 }
193
194 static int request_frame(AVFilterLink *outlink)
195 {
196     AVFilterContext *ctx = outlink->src;
197     SidechainCompressContext *s = ctx->priv;
198     int i, ret;
199
200     /* get a frame on each input */
201     for (i = 0; i < 2; i++) {
202         AVFilterLink *inlink = ctx->inputs[i];
203         if (!s->input_frame[i] &&
204             (ret = ff_request_frame(inlink)) < 0)
205             return ret;
206
207         /* request the same number of samples on all inputs */
208         if (i == 0)
209             ctx->inputs[1]->request_samples = s->input_frame[0]->nb_samples;
210     }
211
212     return 0;
213 }
214
215 static int query_formats(AVFilterContext *ctx)
216 {
217     AVFilterFormats *formats;
218     AVFilterChannelLayouts *layouts = NULL;
219     static const enum AVSampleFormat sample_fmts[] = {
220         AV_SAMPLE_FMT_DBL,
221         AV_SAMPLE_FMT_NONE
222     };
223     int ret, i;
224
225     if (!ctx->inputs[0]->in_channel_layouts ||
226         !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
227         av_log(ctx, AV_LOG_WARNING,
228                "No channel layout for input 1\n");
229             return AVERROR(EAGAIN);
230     }
231
232     if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
233         (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
234         return ret;
235
236     for (i = 0; i < 2; i++) {
237         layouts = ff_all_channel_counts();
238         if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
239             return ret;
240     }
241
242     formats = ff_make_format_list(sample_fmts);
243     if ((ret = ff_set_common_formats(ctx, formats)) < 0)
244         return ret;
245
246     formats = ff_all_samplerates();
247     return ff_set_common_samplerates(ctx, formats);
248 }
249
250 static int config_output(AVFilterLink *outlink)
251 {
252     AVFilterContext *ctx = outlink->src;
253     SidechainCompressContext *s = ctx->priv;
254
255     if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
256         av_log(ctx, AV_LOG_ERROR,
257                "Inputs must have the same sample rate "
258                "%d for in0 vs %d for in1\n",
259                ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
260         return AVERROR(EINVAL);
261     }
262
263     outlink->sample_rate = ctx->inputs[0]->sample_rate;
264     outlink->time_base   = ctx->inputs[0]->time_base;
265     outlink->channel_layout = ctx->inputs[0]->channel_layout;
266     outlink->channels = ctx->inputs[0]->channels;
267
268     s->attack_coeff = FFMIN(1., 1. / (s->attack * outlink->sample_rate / 4000.));
269     s->release_coeff = FFMIN(1., 1. / (s->release * outlink->sample_rate / 4000.));
270
271     return 0;
272 }
273
274 static const AVFilterPad sidechaincompress_inputs[] = {
275     {
276         .name           = "main",
277         .type           = AVMEDIA_TYPE_AUDIO,
278         .filter_frame   = filter_frame,
279         .needs_writable = 1,
280         .needs_fifo     = 1,
281     },{
282         .name           = "sidechain",
283         .type           = AVMEDIA_TYPE_AUDIO,
284         .filter_frame   = filter_frame,
285         .needs_fifo     = 1,
286     },
287     { NULL }
288 };
289
290 static const AVFilterPad sidechaincompress_outputs[] = {
291     {
292         .name          = "default",
293         .type          = AVMEDIA_TYPE_AUDIO,
294         .config_props  = config_output,
295         .request_frame = request_frame,
296     },
297     { NULL }
298 };
299
300 AVFilter ff_af_sidechaincompress = {
301     .name           = "sidechaincompress",
302     .description    = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
303     .priv_size      = sizeof(SidechainCompressContext),
304     .priv_class     = &sidechaincompress_class,
305     .init           = init,
306     .query_formats  = query_formats,
307     .inputs         = sidechaincompress_inputs,
308     .outputs        = sidechaincompress_outputs,
309 };