]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_sidechaincompress.c
avfilter/af_sidechaincompress: add forgotten option
[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 mix;
48     double thres;
49     double knee;
50     double knee_start;
51     double knee_stop;
52     double lin_knee_start;
53     double compressed_knee_stop;
54     int link;
55     int detection;
56
57     AVFrame *input_frame[2];
58 } SidechainCompressContext;
59
60 #define OFFSET(x) offsetof(SidechainCompressContext, x)
61 #define A AV_OPT_FLAG_AUDIO_PARAM
62 #define F AV_OPT_FLAG_FILTERING_PARAM
63
64 static const AVOption sidechaincompress_options[] = {
65     { "threshold", "set threshold",    OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0.000976563,    1, A|F },
66     { "ratio",     "set ratio",        OFFSET(ratio),     AV_OPT_TYPE_DOUBLE, {.dbl=2},               1,   20, A|F },
67     { "attack",    "set attack",       OFFSET(attack),    AV_OPT_TYPE_DOUBLE, {.dbl=20},           0.01, 2000, A|F },
68     { "release",   "set release",      OFFSET(release),   AV_OPT_TYPE_DOUBLE, {.dbl=250},          0.01, 9000, A|F },
69     { "makeup",    "set make up gain", OFFSET(makeup),    AV_OPT_TYPE_DOUBLE, {.dbl=2},               1,   64, A|F },
70     { "knee",      "set knee",         OFFSET(knee),      AV_OPT_TYPE_DOUBLE, {.dbl=2.82843},         1,    8, A|F },
71     { "link",      "set link type",    OFFSET(link),      AV_OPT_TYPE_INT,    {.i64=0},               0,    1, A|F, "link" },
72     {   "average", 0,                  0,                 AV_OPT_TYPE_CONST,  {.i64=0},               0,    0, A|F, "link" },
73     {   "maximum", 0,                  0,                 AV_OPT_TYPE_CONST,  {.i64=1},               0,    0, A|F, "link" },
74     { "detection", "set detection",    OFFSET(detection), AV_OPT_TYPE_INT,    {.i64=1},               0,    1, A|F, "detection" },
75     {   "peak",    0,                  0,                 AV_OPT_TYPE_CONST,  {.i64=0},               0,    0, A|F, "detection" },
76     {   "rms",     0,                  0,                 AV_OPT_TYPE_CONST,  {.i64=1},               0,    0, A|F, "detection" },
77     { "mix",       "set mix",          OFFSET(mix),       AV_OPT_TYPE_DOUBLE, {.dbl=1},               0,    1, A|F },
78     { NULL }
79 };
80
81 AVFILTER_DEFINE_CLASS(sidechaincompress);
82
83 static av_cold int init(AVFilterContext *ctx)
84 {
85     SidechainCompressContext *s = ctx->priv;
86
87     s->thres = log(s->threshold);
88     s->lin_knee_start = s->threshold / sqrt(s->knee);
89     s->knee_start = log(s->lin_knee_start);
90     s->knee_stop = log(s->threshold * sqrt(s->knee));
91     s->compressed_knee_stop = (s->knee_stop - s->thres) / s->ratio + s->thres;
92
93     return 0;
94 }
95
96 // A fake infinity value (because real infinity may break some hosts)
97 #define FAKE_INFINITY (65536.0 * 65536.0)
98
99 // Check for infinity (with appropriate-ish tolerance)
100 #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
101
102 static double output_gain(double lin_slope, double ratio, double thres,
103                           double knee, double knee_start, double knee_stop,
104                           double compressed_knee_stop, int detection)
105 {
106     double slope = log(lin_slope);
107     double gain = 0.0;
108     double delta = 0.0;
109
110     if (detection)
111         slope *= 0.5;
112
113     if (IS_FAKE_INFINITY(ratio)) {
114         gain = thres;
115         delta = 0.0;
116     } else {
117         gain = (slope - thres) / ratio + thres;
118         delta = 1.0 / ratio;
119     }
120
121     if (knee > 1.0 && slope < knee_stop)
122         gain = hermite_interpolation(slope, knee_start, knee_stop,
123                                      knee_start, compressed_knee_stop,
124                                      1.0, delta);
125
126     return exp(gain - slope);
127 }
128
129 static int filter_frame(AVFilterLink *link, AVFrame *frame)
130 {
131     AVFilterContext *ctx = link->dst;
132     SidechainCompressContext *s = ctx->priv;
133     AVFilterLink *sclink = ctx->inputs[1];
134     AVFilterLink *outlink = ctx->outputs[0];
135     const double makeup = s->makeup;
136     const double mix = s->mix;
137     const double *scsrc;
138     double *sample;
139     int nb_samples;
140     int ret, i, c;
141
142     for (i = 0; i < 2; i++)
143         if (link == ctx->inputs[i])
144             break;
145     av_assert0(i < 2 && !s->input_frame[i]);
146     s->input_frame[i] = frame;
147
148     if (!s->input_frame[0] || !s->input_frame[1])
149         return 0;
150
151     nb_samples = FFMIN(s->input_frame[0]->nb_samples,
152                        s->input_frame[1]->nb_samples);
153
154     sample = (double *)s->input_frame[0]->data[0];
155     scsrc = (const double *)s->input_frame[1]->data[0];
156
157     for (i = 0; i < nb_samples; i++) {
158         double abs_sample, gain = 1.0;
159
160         abs_sample = fabs(scsrc[0]);
161
162         if (s->link == 1) {
163             for (c = 1; c < sclink->channels; c++)
164                 abs_sample = FFMAX(fabs(scsrc[c]), abs_sample);
165         } else {
166             for (c = 1; c < sclink->channels; c++)
167                 abs_sample += fabs(scsrc[c]);
168
169             abs_sample /= sclink->channels;
170         }
171
172         if (s->detection)
173             abs_sample *= abs_sample;
174
175         s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
176
177         if (s->lin_slope > 0.0 && s->lin_slope > s->lin_knee_start)
178             gain = output_gain(s->lin_slope, s->ratio, s->thres, s->knee,
179                                s->knee_start, s->knee_stop,
180                                s->compressed_knee_stop, s->detection);
181
182         for (c = 0; c < outlink->channels; c++)
183             sample[c] *= (gain * makeup * mix + (1. - mix));
184
185         sample += outlink->channels;
186         scsrc += sclink->channels;
187     }
188
189     ret = ff_filter_frame(outlink, s->input_frame[0]);
190
191     s->input_frame[0] = NULL;
192     av_frame_free(&s->input_frame[1]);
193
194     return ret;
195 }
196
197 static int request_frame(AVFilterLink *outlink)
198 {
199     AVFilterContext *ctx = outlink->src;
200     SidechainCompressContext *s = ctx->priv;
201     int i, ret;
202
203     /* get a frame on each input */
204     for (i = 0; i < 2; i++) {
205         AVFilterLink *inlink = ctx->inputs[i];
206         if (!s->input_frame[i] &&
207             (ret = ff_request_frame(inlink)) < 0)
208             return ret;
209
210         /* request the same number of samples on all inputs */
211         if (i == 0)
212             ctx->inputs[1]->request_samples = s->input_frame[0]->nb_samples;
213     }
214
215     return 0;
216 }
217
218 static int query_formats(AVFilterContext *ctx)
219 {
220     AVFilterFormats *formats;
221     AVFilterChannelLayouts *layouts = NULL;
222     static const enum AVSampleFormat sample_fmts[] = {
223         AV_SAMPLE_FMT_DBL,
224         AV_SAMPLE_FMT_NONE
225     };
226     int ret, i;
227
228     if (!ctx->inputs[0]->in_channel_layouts ||
229         !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
230         av_log(ctx, AV_LOG_WARNING,
231                "No channel layout for input 1\n");
232             return AVERROR(EAGAIN);
233     }
234
235     if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
236         (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
237         return ret;
238
239     for (i = 0; i < 2; i++) {
240         layouts = ff_all_channel_counts();
241         if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
242             return ret;
243     }
244
245     formats = ff_make_format_list(sample_fmts);
246     if ((ret = ff_set_common_formats(ctx, formats)) < 0)
247         return ret;
248
249     formats = ff_all_samplerates();
250     return ff_set_common_samplerates(ctx, formats);
251 }
252
253 static int config_output(AVFilterLink *outlink)
254 {
255     AVFilterContext *ctx = outlink->src;
256     SidechainCompressContext *s = ctx->priv;
257
258     if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
259         av_log(ctx, AV_LOG_ERROR,
260                "Inputs must have the same sample rate "
261                "%d for in0 vs %d for in1\n",
262                ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
263         return AVERROR(EINVAL);
264     }
265
266     outlink->sample_rate = ctx->inputs[0]->sample_rate;
267     outlink->time_base   = ctx->inputs[0]->time_base;
268     outlink->channel_layout = ctx->inputs[0]->channel_layout;
269     outlink->channels = ctx->inputs[0]->channels;
270
271     s->attack_coeff = FFMIN(1., 1. / (s->attack * outlink->sample_rate / 4000.));
272     s->release_coeff = FFMIN(1., 1. / (s->release * outlink->sample_rate / 4000.));
273
274     return 0;
275 }
276
277 static const AVFilterPad sidechaincompress_inputs[] = {
278     {
279         .name           = "main",
280         .type           = AVMEDIA_TYPE_AUDIO,
281         .filter_frame   = filter_frame,
282         .needs_writable = 1,
283         .needs_fifo     = 1,
284     },{
285         .name           = "sidechain",
286         .type           = AVMEDIA_TYPE_AUDIO,
287         .filter_frame   = filter_frame,
288         .needs_fifo     = 1,
289     },
290     { NULL }
291 };
292
293 static const AVFilterPad sidechaincompress_outputs[] = {
294     {
295         .name          = "default",
296         .type          = AVMEDIA_TYPE_AUDIO,
297         .config_props  = config_output,
298         .request_frame = request_frame,
299     },
300     { NULL }
301 };
302
303 AVFilter ff_af_sidechaincompress = {
304     .name           = "sidechaincompress",
305     .description    = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
306     .priv_size      = sizeof(SidechainCompressContext),
307     .priv_class     = &sidechaincompress_class,
308     .init           = init,
309     .query_formats  = query_formats,
310     .inputs         = sidechaincompress_inputs,
311     .outputs        = sidechaincompress_outputs,
312 };