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 * Audio (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 {
43 double attack, attack_coeff;
44 double release, release_coeff;
54 double lin_knee_start;
55 double adj_knee_start;
56 double compressed_knee_stop;
60 AVFrame *input_frame[2];
61 } SidechainCompressContext;
63 #define OFFSET(x) offsetof(SidechainCompressContext, x)
64 #define A AV_OPT_FLAG_AUDIO_PARAM
65 #define F AV_OPT_FLAG_FILTERING_PARAM
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 },
86 #define sidechaincompress_options options
87 AVFILTER_DEFINE_CLASS(sidechaincompress);
89 // A fake infinity value (because real infinity may break some hosts)
90 #define FAKE_INFINITY (65536.0 * 65536.0)
92 // Check for infinity (with appropriate-ish tolerance)
93 #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
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)
99 double slope = log(lin_slope);
106 if (IS_FAKE_INFINITY(ratio)) {
110 gain = (slope - thres) / ratio + thres;
114 if (knee > 1.0 && slope < knee_stop)
115 gain = hermite_interpolation(slope, knee_start, knee_stop,
116 knee_start, compressed_knee_stop,
119 return exp(gain - slope);
122 static int compressor_config_output(AVFilterLink *outlink)
124 AVFilterContext *ctx = outlink->src;
125 SidechainCompressContext *s = ctx->priv;
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;
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.));
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)
145 const double makeup = s->makeup;
146 const double mix = s->mix;
149 for (i = 0; i < nb_samples; i++) {
150 double abs_sample, gain = 1.0;
152 abs_sample = fabs(scsrc[0] * level_sc);
155 for (c = 1; c < sclink->channels; c++)
156 abs_sample = FFMAX(fabs(scsrc[c] * level_sc), abs_sample);
158 for (c = 1; c < sclink->channels; c++)
159 abs_sample += fabs(scsrc[c] * level_sc);
161 abs_sample /= sclink->channels;
165 abs_sample *= abs_sample;
167 s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
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);
174 for (c = 0; c < inlink->channels; c++)
175 dst[c] = src[c] * level_in * (gain * makeup * mix + (1. - mix));
177 src += inlink->channels;
178 dst += inlink->channels;
179 scsrc += sclink->channels;
183 #if CONFIG_SIDECHAINCOMPRESS_FILTER
184 static int filter_frame(AVFilterLink *link, AVFrame *frame)
186 AVFilterContext *ctx = link->dst;
187 SidechainCompressContext *s = ctx->priv;
188 AVFilterLink *outlink = ctx->outputs[0];
194 for (i = 0; i < 2; i++)
195 if (link == ctx->inputs[i])
197 av_assert0(i < 2 && !s->input_frame[i]);
198 s->input_frame[i] = frame;
200 if (!s->input_frame[0] || !s->input_frame[1])
203 nb_samples = FFMIN(s->input_frame[0]->nb_samples,
204 s->input_frame[1]->nb_samples);
206 sample = (double *)s->input_frame[0]->data[0];
207 scsrc = (const double *)s->input_frame[1]->data[0];
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]);
214 s->input_frame[0] = NULL;
215 av_frame_free(&s->input_frame[1]);
220 static int request_frame(AVFilterLink *outlink)
222 AVFilterContext *ctx = outlink->src;
223 SidechainCompressContext *s = ctx->priv;
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)
233 /* request the same number of samples on all inputs */
235 ctx->inputs[1]->request_samples = s->input_frame[0]->nb_samples;
241 static int query_formats(AVFilterContext *ctx)
243 AVFilterFormats *formats;
244 AVFilterChannelLayouts *layouts = NULL;
245 static const enum AVSampleFormat sample_fmts[] = {
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);
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)
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)
268 formats = ff_make_format_list(sample_fmts);
269 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
272 formats = ff_all_samplerates();
273 return ff_set_common_samplerates(ctx, formats);
276 static int config_output(AVFilterLink *outlink)
278 AVFilterContext *ctx = outlink->src;
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);
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;
293 compressor_config_output(outlink);
298 static const AVFilterPad sidechaincompress_inputs[] = {
301 .type = AVMEDIA_TYPE_AUDIO,
302 .filter_frame = filter_frame,
307 .type = AVMEDIA_TYPE_AUDIO,
308 .filter_frame = filter_frame,
314 static const AVFilterPad sidechaincompress_outputs[] = {
317 .type = AVMEDIA_TYPE_AUDIO,
318 .config_props = config_output,
319 .request_frame = request_frame,
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,
333 #endif /* CONFIG_SIDECHAINCOMPRESS_FILTER */
335 #if CONFIG_ACOMPRESSOR_FILTER
336 static int acompressor_filter_frame(AVFilterLink *inlink, AVFrame *in)
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];
345 if (av_frame_is_writable(in)) {
348 out = ff_get_audio_buffer(inlink, in->nb_samples);
351 return AVERROR(ENOMEM);
353 av_frame_copy_props(out, in);
355 dst = (double *)out->data[0];
357 compressor(s, src, dst, src, in->nb_samples,
358 s->level_in, s->level_in,
363 return ff_filter_frame(outlink, out);
366 static int acompressor_query_formats(AVFilterContext *ctx)
368 AVFilterFormats *formats;
369 AVFilterChannelLayouts *layouts;
370 static const enum AVSampleFormat sample_fmts[] = {
376 layouts = ff_all_channel_counts();
378 return AVERROR(ENOMEM);
379 ret = ff_set_common_channel_layouts(ctx, layouts);
383 formats = ff_make_format_list(sample_fmts);
385 return AVERROR(ENOMEM);
386 ret = ff_set_common_formats(ctx, formats);
390 formats = ff_all_samplerates();
392 return AVERROR(ENOMEM);
393 return ff_set_common_samplerates(ctx, formats);
396 #define acompressor_options options
397 AVFILTER_DEFINE_CLASS(acompressor);
399 static const AVFilterPad acompressor_inputs[] = {
402 .type = AVMEDIA_TYPE_AUDIO,
403 .filter_frame = acompressor_filter_frame,
408 static const AVFilterPad acompressor_outputs[] = {
411 .type = AVMEDIA_TYPE_AUDIO,
412 .config_props = compressor_config_output,
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,
426 #endif /* CONFIG_ACOMPRESSOR_FILTER */