2 * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
3 * Copyright (c) 2015 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
24 * Sidechain compressor filter
27 #include "libavutil/avassert.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/common.h"
30 #include "libavutil/opt.h"
38 typedef struct SidechainCompressContext {
41 double attack, attack_coeff;
42 double release, release_coeff;
51 double lin_knee_start;
52 double compressed_knee_stop;
56 AVFrame *input_frame[2];
57 } SidechainCompressContext;
59 #define OFFSET(x) offsetof(SidechainCompressContext, x)
60 #define A AV_OPT_FLAG_AUDIO_PARAM
61 #define F AV_OPT_FLAG_FILTERING_PARAM
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" },
79 AVFILTER_DEFINE_CLASS(sidechaincompress);
81 static av_cold int init(AVFilterContext *ctx)
83 SidechainCompressContext *s = ctx->priv;
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;
94 // A fake infinity value (because real infinity may break some hosts)
95 #define FAKE_INFINITY (65536.0 * 65536.0)
97 // Check for infinity (with appropriate-ish tolerance)
98 #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
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)
104 double slope = log(lin_slope);
111 if (IS_FAKE_INFINITY(ratio)) {
115 gain = (slope - thres) / ratio + thres;
119 if (knee > 1.0 && slope < knee_stop)
120 gain = hermite_interpolation(slope, knee_start, knee_stop,
121 knee_start, compressed_knee_stop,
124 return exp(gain - slope);
127 static int filter_frame(AVFilterLink *link, AVFrame *frame)
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;
139 for (i = 0; i < 2; i++)
140 if (link == ctx->inputs[i])
142 av_assert0(i < 2 && !s->input_frame[i]);
143 s->input_frame[i] = frame;
145 if (!s->input_frame[0] || !s->input_frame[1])
148 nb_samples = FFMIN(s->input_frame[0]->nb_samples,
149 s->input_frame[1]->nb_samples);
151 sample = (double *)s->input_frame[0]->data[0];
152 scsrc = (const double *)s->input_frame[1]->data[0];
154 for (i = 0; i < nb_samples; i++) {
155 double abs_sample, gain = 1.0;
157 abs_sample = fabs(scsrc[0]);
160 for (c = 1; c < sclink->channels; c++)
161 abs_sample = FFMAX(fabs(scsrc[c]), abs_sample);
163 for (c = 1; c < sclink->channels; c++)
164 abs_sample += fabs(scsrc[c]);
166 abs_sample /= sclink->channels;
170 abs_sample *= abs_sample;
172 s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
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);
179 for (c = 0; c < outlink->channels; c++)
180 sample[c] *= gain * makeup;
182 sample += outlink->channels;
183 scsrc += sclink->channels;
186 ret = ff_filter_frame(outlink, s->input_frame[0]);
188 s->input_frame[0] = NULL;
189 av_frame_free(&s->input_frame[1]);
194 static int request_frame(AVFilterLink *outlink)
196 AVFilterContext *ctx = outlink->src;
197 SidechainCompressContext *s = ctx->priv;
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)
207 /* request the same number of samples on all inputs */
209 ctx->inputs[1]->request_samples = s->input_frame[0]->nb_samples;
215 static int query_formats(AVFilterContext *ctx)
217 AVFilterFormats *formats;
218 AVFilterChannelLayouts *layouts = NULL;
219 static const enum AVSampleFormat sample_fmts[] = {
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);
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)
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)
242 formats = ff_make_format_list(sample_fmts);
243 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
246 formats = ff_all_samplerates();
247 return ff_set_common_samplerates(ctx, formats);
250 static int config_output(AVFilterLink *outlink)
252 AVFilterContext *ctx = outlink->src;
253 SidechainCompressContext *s = ctx->priv;
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);
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;
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.));
274 static const AVFilterPad sidechaincompress_inputs[] = {
277 .type = AVMEDIA_TYPE_AUDIO,
278 .filter_frame = filter_frame,
283 .type = AVMEDIA_TYPE_AUDIO,
284 .filter_frame = filter_frame,
290 static const AVFilterPad sidechaincompress_outputs[] = {
293 .type = AVMEDIA_TYPE_AUDIO,
294 .config_props = config_output,
295 .request_frame = request_frame,
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,
306 .query_formats = query_formats,
307 .inputs = sidechaincompress_inputs,
308 .outputs = sidechaincompress_outputs,