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;
52 double lin_knee_start;
53 double compressed_knee_stop;
57 AVFrame *input_frame[2];
58 } SidechainCompressContext;
60 #define OFFSET(x) offsetof(SidechainCompressContext, x)
61 #define A AV_OPT_FLAG_AUDIO_PARAM
62 #define F AV_OPT_FLAG_FILTERING_PARAM
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 },
81 AVFILTER_DEFINE_CLASS(sidechaincompress);
83 static av_cold int init(AVFilterContext *ctx)
85 SidechainCompressContext *s = ctx->priv;
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;
96 // A fake infinity value (because real infinity may break some hosts)
97 #define FAKE_INFINITY (65536.0 * 65536.0)
99 // Check for infinity (with appropriate-ish tolerance)
100 #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
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)
106 double slope = log(lin_slope);
113 if (IS_FAKE_INFINITY(ratio)) {
117 gain = (slope - thres) / ratio + thres;
121 if (knee > 1.0 && slope < knee_stop)
122 gain = hermite_interpolation(slope, knee_start, knee_stop,
123 knee_start, compressed_knee_stop,
126 return exp(gain - slope);
129 static int filter_frame(AVFilterLink *link, AVFrame *frame)
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;
142 for (i = 0; i < 2; i++)
143 if (link == ctx->inputs[i])
145 av_assert0(i < 2 && !s->input_frame[i]);
146 s->input_frame[i] = frame;
148 if (!s->input_frame[0] || !s->input_frame[1])
151 nb_samples = FFMIN(s->input_frame[0]->nb_samples,
152 s->input_frame[1]->nb_samples);
154 sample = (double *)s->input_frame[0]->data[0];
155 scsrc = (const double *)s->input_frame[1]->data[0];
157 for (i = 0; i < nb_samples; i++) {
158 double abs_sample, gain = 1.0;
160 abs_sample = fabs(scsrc[0]);
163 for (c = 1; c < sclink->channels; c++)
164 abs_sample = FFMAX(fabs(scsrc[c]), abs_sample);
166 for (c = 1; c < sclink->channels; c++)
167 abs_sample += fabs(scsrc[c]);
169 abs_sample /= sclink->channels;
173 abs_sample *= abs_sample;
175 s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
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);
182 for (c = 0; c < outlink->channels; c++)
183 sample[c] *= (gain * makeup * mix + (1. - mix));
185 sample += outlink->channels;
186 scsrc += sclink->channels;
189 ret = ff_filter_frame(outlink, s->input_frame[0]);
191 s->input_frame[0] = NULL;
192 av_frame_free(&s->input_frame[1]);
197 static int request_frame(AVFilterLink *outlink)
199 AVFilterContext *ctx = outlink->src;
200 SidechainCompressContext *s = ctx->priv;
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)
210 /* request the same number of samples on all inputs */
212 ctx->inputs[1]->request_samples = s->input_frame[0]->nb_samples;
218 static int query_formats(AVFilterContext *ctx)
220 AVFilterFormats *formats;
221 AVFilterChannelLayouts *layouts = NULL;
222 static const enum AVSampleFormat sample_fmts[] = {
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);
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)
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)
245 formats = ff_make_format_list(sample_fmts);
246 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
249 formats = ff_all_samplerates();
250 return ff_set_common_samplerates(ctx, formats);
253 static int config_output(AVFilterLink *outlink)
255 AVFilterContext *ctx = outlink->src;
256 SidechainCompressContext *s = ctx->priv;
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);
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;
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.));
277 static const AVFilterPad sidechaincompress_inputs[] = {
280 .type = AVMEDIA_TYPE_AUDIO,
281 .filter_frame = filter_frame,
286 .type = AVMEDIA_TYPE_AUDIO,
287 .filter_frame = filter_frame,
293 static const AVFilterPad sidechaincompress_outputs[] = {
296 .type = AVMEDIA_TYPE_AUDIO,
297 .config_props = config_output,
298 .request_frame = request_frame,
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,
309 .query_formats = query_formats,
310 .inputs = sidechaincompress_inputs,
311 .outputs = sidechaincompress_outputs,