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"
37 typedef struct SidechainCompressContext {
40 double attack, attack_coeff;
41 double release, release_coeff;
50 double lin_knee_start;
51 double compressed_knee_stop;
55 AVFrame *input_frame[2];
56 } SidechainCompressContext;
58 #define OFFSET(x) offsetof(SidechainCompressContext, x)
59 #define A AV_OPT_FLAG_AUDIO_PARAM
60 #define F AV_OPT_FLAG_FILTERING_PARAM
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" },
78 AVFILTER_DEFINE_CLASS(sidechaincompress);
80 static av_cold int init(AVFilterContext *ctx)
82 SidechainCompressContext *s = ctx->priv;
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;
93 static inline float hermite_interpolation(float x, float x0, float x1,
97 float width = x1 - x0;
98 float t = (x - x0) / width;
100 float ct0, ct1, ct2, ct3;
110 ct2 = -3 * p0 - 2 * m0 + 3 * p1 - m1;
111 ct3 = 2 * p0 + m0 - 2 * p1 + m1;
113 return ct3 * t3 + ct2 * t2 + ct1 * t + ct0;
116 // A fake infinity value (because real infinity may break some hosts)
117 #define FAKE_INFINITY (65536.0 * 65536.0)
119 // Check for infinity (with appropriate-ish tolerance)
120 #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
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)
126 double slope = log(lin_slope);
133 if (IS_FAKE_INFINITY(ratio)) {
137 gain = (slope - thres) / ratio + thres;
141 if (knee > 1.0 && slope < knee_stop)
142 gain = hermite_interpolation(slope, knee_start, knee_stop,
143 knee_start, compressed_knee_stop,
146 return exp(gain - slope);
149 static int filter_frame(AVFilterLink *link, AVFrame *frame)
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;
161 for (i = 0; i < 2; i++)
162 if (link == ctx->inputs[i])
164 av_assert0(i < 2 && !s->input_frame[i]);
165 s->input_frame[i] = frame;
167 if (!s->input_frame[0] || !s->input_frame[1])
170 nb_samples = FFMIN(s->input_frame[0]->nb_samples,
171 s->input_frame[1]->nb_samples);
173 sample = (double *)s->input_frame[0]->data[0];
174 scsrc = (const double *)s->input_frame[1]->data[0];
176 for (i = 0; i < nb_samples; i++) {
177 double abs_sample, gain = 1.0;
179 abs_sample = FFABS(scsrc[0]);
182 for (c = 1; c < sclink->channels; c++)
183 abs_sample = FFMAX(FFABS(scsrc[c]), abs_sample);
185 for (c = 1; c < sclink->channels; c++)
186 abs_sample += FFABS(scsrc[c]);
188 abs_sample /= sclink->channels;
192 abs_sample *= abs_sample;
194 s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
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);
201 for (c = 0; c < outlink->channels; c++)
202 sample[c] *= gain * makeup;
204 sample += outlink->channels;
205 scsrc += sclink->channels;
208 ret = ff_filter_frame(outlink, s->input_frame[0]);
210 s->input_frame[0] = NULL;
211 av_frame_free(&s->input_frame[1]);
216 static int request_frame(AVFilterLink *outlink)
218 AVFilterContext *ctx = outlink->src;
219 SidechainCompressContext *s = ctx->priv;
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)
229 /* request the same number of samples on all inputs */
231 ctx->inputs[1]->request_samples = s->input_frame[0]->nb_samples;
237 static int query_formats(AVFilterContext *ctx)
239 AVFilterFormats *formats;
240 AVFilterChannelLayouts *layouts = NULL;
241 static const enum AVSampleFormat sample_fmts[] = {
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);
254 ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0]);
256 return AVERROR(ENOMEM);
257 ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
259 for (i = 0; i < 2; i++) {
260 layouts = ff_all_channel_counts();
262 return AVERROR(ENOMEM);
263 ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
266 formats = ff_make_format_list(sample_fmts);
268 return AVERROR(ENOMEM);
269 ret = ff_set_common_formats(ctx, formats);
273 formats = ff_all_samplerates();
275 return AVERROR(ENOMEM);
276 return ff_set_common_samplerates(ctx, formats);
279 static int config_output(AVFilterLink *outlink)
281 AVFilterContext *ctx = outlink->src;
282 SidechainCompressContext *s = ctx->priv;
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);
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;
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));
303 static const AVFilterPad sidechaincompress_inputs[] = {
306 .type = AVMEDIA_TYPE_AUDIO,
307 .filter_frame = filter_frame,
312 .type = AVMEDIA_TYPE_AUDIO,
313 .filter_frame = filter_frame,
319 static const AVFilterPad sidechaincompress_outputs[] = {
322 .type = AVMEDIA_TYPE_AUDIO,
323 .config_props = config_output,
324 .request_frame = request_frame,
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,
335 .query_formats = query_formats,
336 .inputs = sidechaincompress_inputs,
337 .outputs = sidechaincompress_outputs,