]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_sidechaincompress.c
Merge commit '2080bea4a74b6b1598caa806a11ea807ba546f94'
[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  * Audio (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 level_in;
42     double level_sc;
43     double attack, attack_coeff;
44     double release, release_coeff;
45     double lin_slope;
46     double ratio;
47     double threshold;
48     double makeup;
49     double mix;
50     double thres;
51     double knee;
52     double knee_start;
53     double knee_stop;
54     double lin_knee_start;
55     double adj_knee_start;
56     double compressed_knee_stop;
57     int link;
58     int detection;
59
60     AVFrame *input_frame[2];
61 } SidechainCompressContext;
62
63 #define OFFSET(x) offsetof(SidechainCompressContext, x)
64 #define A AV_OPT_FLAG_AUDIO_PARAM
65 #define F AV_OPT_FLAG_FILTERING_PARAM
66
67 static const AVOption options[] = {
68     { "level_in",  "set input gain",     OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=1},        0.015625,   64, A|F },
69     { "threshold", "set threshold",      OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0.000976563,    1, A|F },
70     { "ratio",     "set ratio",          OFFSET(ratio),     AV_OPT_TYPE_DOUBLE, {.dbl=2},               1,   20, A|F },
71     { "attack",    "set attack",         OFFSET(attack),    AV_OPT_TYPE_DOUBLE, {.dbl=20},           0.01, 2000, A|F },
72     { "release",   "set release",        OFFSET(release),   AV_OPT_TYPE_DOUBLE, {.dbl=250},          0.01, 9000, A|F },
73     { "makeup",    "set make up gain",   OFFSET(makeup),    AV_OPT_TYPE_DOUBLE, {.dbl=2},               1,   64, A|F },
74     { "knee",      "set knee",           OFFSET(knee),      AV_OPT_TYPE_DOUBLE, {.dbl=2.82843},         1,    8, A|F },
75     { "link",      "set link type",      OFFSET(link),      AV_OPT_TYPE_INT,    {.i64=0},               0,    1, A|F, "link" },
76     {   "average", 0,                    0,                 AV_OPT_TYPE_CONST,  {.i64=0},               0,    0, A|F, "link" },
77     {   "maximum", 0,                    0,                 AV_OPT_TYPE_CONST,  {.i64=1},               0,    0, A|F, "link" },
78     { "detection", "set detection",      OFFSET(detection), AV_OPT_TYPE_INT,    {.i64=1},               0,    1, A|F, "detection" },
79     {   "peak",    0,                    0,                 AV_OPT_TYPE_CONST,  {.i64=0},               0,    0, A|F, "detection" },
80     {   "rms",     0,                    0,                 AV_OPT_TYPE_CONST,  {.i64=1},               0,    0, A|F, "detection" },
81     { "level_sc",  "set sidechain gain", OFFSET(level_sc),  AV_OPT_TYPE_DOUBLE, {.dbl=1},        0.015625,   64, A|F },
82     { "mix",       "set mix",            OFFSET(mix),       AV_OPT_TYPE_DOUBLE, {.dbl=1},               0,    1, A|F },
83     { NULL }
84 };
85
86 #define sidechaincompress_options options
87 AVFILTER_DEFINE_CLASS(sidechaincompress);
88
89 // A fake infinity value (because real infinity may break some hosts)
90 #define FAKE_INFINITY (65536.0 * 65536.0)
91
92 // Check for infinity (with appropriate-ish tolerance)
93 #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
94
95 static double output_gain(double lin_slope, double ratio, double thres,
96                           double knee, double knee_start, double knee_stop,
97                           double compressed_knee_stop, int detection)
98 {
99     double slope = log(lin_slope);
100     double gain = 0.0;
101     double delta = 0.0;
102
103     if (detection)
104         slope *= 0.5;
105
106     if (IS_FAKE_INFINITY(ratio)) {
107         gain = thres;
108         delta = 0.0;
109     } else {
110         gain = (slope - thres) / ratio + thres;
111         delta = 1.0 / ratio;
112     }
113
114     if (knee > 1.0 && slope < knee_stop)
115         gain = hermite_interpolation(slope, knee_start, knee_stop,
116                                      knee_start, compressed_knee_stop,
117                                      1.0, delta);
118
119     return exp(gain - slope);
120 }
121
122 static int compressor_config_output(AVFilterLink *outlink)
123 {
124     AVFilterContext *ctx = outlink->src;
125     SidechainCompressContext *s = ctx->priv;
126
127     s->thres = log(s->threshold);
128     s->lin_knee_start = s->threshold / sqrt(s->knee);
129     s->adj_knee_start = s->lin_knee_start * s->lin_knee_start;
130     s->knee_start = log(s->lin_knee_start);
131     s->knee_stop = log(s->threshold * sqrt(s->knee));
132     s->compressed_knee_stop = (s->knee_stop - s->thres) / s->ratio + s->thres;
133
134     s->attack_coeff = FFMIN(1., 1. / (s->attack * outlink->sample_rate / 4000.));
135     s->release_coeff = FFMIN(1., 1. / (s->release * outlink->sample_rate / 4000.));
136
137     return 0;
138 }
139
140 static void compressor(SidechainCompressContext *s,
141                        const double *src, double *dst, const double *scsrc, int nb_samples,
142                        double level_in, double level_sc,
143                        AVFilterLink *inlink, AVFilterLink *sclink)
144 {
145     const double makeup = s->makeup;
146     const double mix = s->mix;
147     int i, c;
148
149     for (i = 0; i < nb_samples; i++) {
150         double abs_sample, gain = 1.0;
151
152         abs_sample = fabs(scsrc[0] * level_sc);
153
154         if (s->link == 1) {
155             for (c = 1; c < sclink->channels; c++)
156                 abs_sample = FFMAX(fabs(scsrc[c] * level_sc), abs_sample);
157         } else {
158             for (c = 1; c < sclink->channels; c++)
159                 abs_sample += fabs(scsrc[c] * level_sc);
160
161             abs_sample /= sclink->channels;
162         }
163
164         if (s->detection)
165             abs_sample *= abs_sample;
166
167         s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
168
169         if (s->lin_slope > 0.0 && s->lin_slope > (s->detection ? s->adj_knee_start : s->lin_knee_start))
170             gain = output_gain(s->lin_slope, s->ratio, s->thres, s->knee,
171                                s->knee_start, s->knee_stop,
172                                s->compressed_knee_stop, s->detection);
173
174         for (c = 0; c < inlink->channels; c++)
175             dst[c] = src[c] * level_in * (gain * makeup * mix + (1. - mix));
176
177         src += inlink->channels;
178         dst += inlink->channels;
179         scsrc += sclink->channels;
180     }
181 }
182
183 #if CONFIG_SIDECHAINCOMPRESS_FILTER
184 static int filter_frame(AVFilterLink *link, AVFrame *frame)
185 {
186     AVFilterContext *ctx = link->dst;
187     SidechainCompressContext *s = ctx->priv;
188     AVFilterLink *outlink = ctx->outputs[0];
189     const double *scsrc;
190     double *sample;
191     int nb_samples;
192     int ret, i;
193
194     for (i = 0; i < 2; i++)
195         if (link == ctx->inputs[i])
196             break;
197     av_assert0(i < 2 && !s->input_frame[i]);
198     s->input_frame[i] = frame;
199
200     if (!s->input_frame[0] || !s->input_frame[1])
201         return 0;
202
203     nb_samples = FFMIN(s->input_frame[0]->nb_samples,
204                        s->input_frame[1]->nb_samples);
205
206     sample = (double *)s->input_frame[0]->data[0];
207     scsrc = (const double *)s->input_frame[1]->data[0];
208
209     compressor(s, sample, sample, scsrc, nb_samples,
210                s->level_in, s->level_sc,
211                ctx->inputs[0], ctx->inputs[1]);
212     ret = ff_filter_frame(outlink, s->input_frame[0]);
213
214     s->input_frame[0] = NULL;
215     av_frame_free(&s->input_frame[1]);
216
217     return ret;
218 }
219
220 static int request_frame(AVFilterLink *outlink)
221 {
222     AVFilterContext *ctx = outlink->src;
223     SidechainCompressContext *s = ctx->priv;
224     int i, ret;
225
226     /* get a frame on each input */
227     for (i = 0; i < 2; i++) {
228         AVFilterLink *inlink = ctx->inputs[i];
229         if (!s->input_frame[i] &&
230             (ret = ff_request_frame(inlink)) < 0)
231             return ret;
232
233         /* request the same number of samples on all inputs */
234         if (i == 0)
235             ctx->inputs[1]->request_samples = s->input_frame[0]->nb_samples;
236     }
237
238     return 0;
239 }
240
241 static int query_formats(AVFilterContext *ctx)
242 {
243     AVFilterFormats *formats;
244     AVFilterChannelLayouts *layouts = NULL;
245     static const enum AVSampleFormat sample_fmts[] = {
246         AV_SAMPLE_FMT_DBL,
247         AV_SAMPLE_FMT_NONE
248     };
249     int ret, i;
250
251     if (!ctx->inputs[0]->in_channel_layouts ||
252         !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
253         av_log(ctx, AV_LOG_WARNING,
254                "No channel layout for input 1\n");
255             return AVERROR(EAGAIN);
256     }
257
258     if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
259         (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
260         return ret;
261
262     for (i = 0; i < 2; i++) {
263         layouts = ff_all_channel_counts();
264         if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
265             return ret;
266     }
267
268     formats = ff_make_format_list(sample_fmts);
269     if ((ret = ff_set_common_formats(ctx, formats)) < 0)
270         return ret;
271
272     formats = ff_all_samplerates();
273     return ff_set_common_samplerates(ctx, formats);
274 }
275
276 static int config_output(AVFilterLink *outlink)
277 {
278     AVFilterContext *ctx = outlink->src;
279
280     if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
281         av_log(ctx, AV_LOG_ERROR,
282                "Inputs must have the same sample rate "
283                "%d for in0 vs %d for in1\n",
284                ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
285         return AVERROR(EINVAL);
286     }
287
288     outlink->sample_rate = ctx->inputs[0]->sample_rate;
289     outlink->time_base   = ctx->inputs[0]->time_base;
290     outlink->channel_layout = ctx->inputs[0]->channel_layout;
291     outlink->channels = ctx->inputs[0]->channels;
292
293     compressor_config_output(outlink);
294
295     return 0;
296 }
297
298 static const AVFilterPad sidechaincompress_inputs[] = {
299     {
300         .name           = "main",
301         .type           = AVMEDIA_TYPE_AUDIO,
302         .filter_frame   = filter_frame,
303         .needs_writable = 1,
304         .needs_fifo     = 1,
305     },{
306         .name           = "sidechain",
307         .type           = AVMEDIA_TYPE_AUDIO,
308         .filter_frame   = filter_frame,
309         .needs_fifo     = 1,
310     },
311     { NULL }
312 };
313
314 static const AVFilterPad sidechaincompress_outputs[] = {
315     {
316         .name          = "default",
317         .type          = AVMEDIA_TYPE_AUDIO,
318         .config_props  = config_output,
319         .request_frame = request_frame,
320     },
321     { NULL }
322 };
323
324 AVFilter ff_af_sidechaincompress = {
325     .name           = "sidechaincompress",
326     .description    = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
327     .priv_size      = sizeof(SidechainCompressContext),
328     .priv_class     = &sidechaincompress_class,
329     .query_formats  = query_formats,
330     .inputs         = sidechaincompress_inputs,
331     .outputs        = sidechaincompress_outputs,
332 };
333 #endif  /* CONFIG_SIDECHAINCOMPRESS_FILTER */
334
335 #if CONFIG_ACOMPRESSOR_FILTER
336 static int acompressor_filter_frame(AVFilterLink *inlink, AVFrame *in)
337 {
338     const double *src = (const double *)in->data[0];
339     AVFilterContext *ctx = inlink->dst;
340     SidechainCompressContext *s = ctx->priv;
341     AVFilterLink *outlink = ctx->outputs[0];
342     AVFrame *out;
343     double *dst;
344
345     if (av_frame_is_writable(in)) {
346         out = in;
347     } else {
348         out = ff_get_audio_buffer(inlink, in->nb_samples);
349         if (!out) {
350             av_frame_free(&in);
351             return AVERROR(ENOMEM);
352         }
353         av_frame_copy_props(out, in);
354     }
355     dst = (double *)out->data[0];
356
357     compressor(s, src, dst, src, in->nb_samples,
358                s->level_in, s->level_in,
359                inlink, inlink);
360
361     if (out != in)
362         av_frame_free(&in);
363     return ff_filter_frame(outlink, out);
364 }
365
366 static int acompressor_query_formats(AVFilterContext *ctx)
367 {
368     AVFilterFormats *formats;
369     AVFilterChannelLayouts *layouts;
370     static const enum AVSampleFormat sample_fmts[] = {
371         AV_SAMPLE_FMT_DBL,
372         AV_SAMPLE_FMT_NONE
373     };
374     int ret;
375
376     layouts = ff_all_channel_counts();
377     if (!layouts)
378         return AVERROR(ENOMEM);
379     ret = ff_set_common_channel_layouts(ctx, layouts);
380     if (ret < 0)
381         return ret;
382
383     formats = ff_make_format_list(sample_fmts);
384     if (!formats)
385         return AVERROR(ENOMEM);
386     ret = ff_set_common_formats(ctx, formats);
387     if (ret < 0)
388         return ret;
389
390     formats = ff_all_samplerates();
391     if (!formats)
392         return AVERROR(ENOMEM);
393     return ff_set_common_samplerates(ctx, formats);
394 }
395
396 #define acompressor_options options
397 AVFILTER_DEFINE_CLASS(acompressor);
398
399 static const AVFilterPad acompressor_inputs[] = {
400     {
401         .name           = "default",
402         .type           = AVMEDIA_TYPE_AUDIO,
403         .filter_frame   = acompressor_filter_frame,
404     },
405     { NULL }
406 };
407
408 static const AVFilterPad acompressor_outputs[] = {
409     {
410         .name          = "default",
411         .type          = AVMEDIA_TYPE_AUDIO,
412         .config_props  = compressor_config_output,
413     },
414     { NULL }
415 };
416
417 AVFilter ff_af_acompressor = {
418     .name           = "acompressor",
419     .description    = NULL_IF_CONFIG_SMALL("Audio compressor."),
420     .priv_size      = sizeof(SidechainCompressContext),
421     .priv_class     = &acompressor_class,
422     .query_formats  = acompressor_query_formats,
423     .inputs         = acompressor_inputs,
424     .outputs        = acompressor_outputs,
425 };
426 #endif  /* CONFIG_ACOMPRESSOR_FILTER */