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