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/audio_fifo.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/common.h"
31 #include "libavutil/opt.h"
40 typedef struct SidechainCompressContext {
45 double attack, attack_coeff;
46 double release, release_coeff;
56 double lin_knee_start;
58 double adj_knee_start;
60 double compressed_knee_start;
61 double compressed_knee_stop;
68 } SidechainCompressContext;
70 #define OFFSET(x) offsetof(SidechainCompressContext, x)
71 #define A AV_OPT_FLAG_AUDIO_PARAM
72 #define F AV_OPT_FLAG_FILTERING_PARAM
74 static const AVOption options[] = {
75 { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A|F },
76 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A|F, "mode" },
77 { "downward",0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F, "mode" },
78 { "upward", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F, "mode" },
79 { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0.000976563, 1, A|F },
80 { "ratio", "set ratio", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 20, A|F },
81 { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 2000, A|F },
82 { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=250}, 0.01, 9000, A|F },
83 { "makeup", "set make up gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 64, A|F },
84 { "knee", "set knee", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=2.82843}, 1, 8, A|F },
85 { "link", "set link type", OFFSET(link), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A|F, "link" },
86 { "average", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F, "link" },
87 { "maximum", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F, "link" },
88 { "detection", "set detection", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, A|F, "detection" },
89 { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F, "detection" },
90 { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F, "detection" },
91 { "level_sc", "set sidechain gain", OFFSET(level_sc), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A|F },
92 { "mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, A|F },
96 #define sidechaincompress_options options
97 AVFILTER_DEFINE_CLASS(sidechaincompress);
99 // A fake infinity value (because real infinity may break some hosts)
100 #define FAKE_INFINITY (65536.0 * 65536.0)
102 // Check for infinity (with appropriate-ish tolerance)
103 #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
105 static double output_gain(double lin_slope, double ratio, double thres,
106 double knee, double knee_start, double knee_stop,
107 double compressed_knee_start,
108 double compressed_knee_stop,
109 int detection, int mode)
111 double slope = log(lin_slope);
118 if (IS_FAKE_INFINITY(ratio)) {
122 gain = (slope - thres) / ratio + thres;
127 if (knee > 1.0 && slope > knee_start)
128 gain = hermite_interpolation(slope, knee_stop, knee_start,
129 knee_stop, compressed_knee_start,
132 if (knee > 1.0 && slope < knee_stop)
133 gain = hermite_interpolation(slope, knee_start, knee_stop,
134 knee_start, compressed_knee_stop,
138 return exp(gain - slope);
141 static int compressor_config_output(AVFilterLink *outlink)
143 AVFilterContext *ctx = outlink->src;
144 SidechainCompressContext *s = ctx->priv;
146 s->thres = log(s->threshold);
147 s->lin_knee_start = s->threshold / sqrt(s->knee);
148 s->lin_knee_stop = s->threshold * sqrt(s->knee);
149 s->adj_knee_start = s->lin_knee_start * s->lin_knee_start;
150 s->adj_knee_stop = s->lin_knee_stop * s->lin_knee_stop;
151 s->knee_start = log(s->lin_knee_start);
152 s->knee_stop = log(s->lin_knee_stop);
153 s->compressed_knee_start = (s->knee_start - s->thres) / s->ratio + s->thres;
154 s->compressed_knee_stop = (s->knee_stop - s->thres) / s->ratio + s->thres;
156 s->attack_coeff = FFMIN(1., 1. / (s->attack * outlink->sample_rate / 4000.));
157 s->release_coeff = FFMIN(1., 1. / (s->release * outlink->sample_rate / 4000.));
162 static void compressor(SidechainCompressContext *s,
163 const double *src, double *dst, const double *scsrc, int nb_samples,
164 double level_in, double level_sc,
165 AVFilterLink *inlink, AVFilterLink *sclink)
167 const double makeup = s->makeup;
168 const double mix = s->mix;
171 for (i = 0; i < nb_samples; i++) {
172 double abs_sample, gain = 1.0;
176 abs_sample = fabs(scsrc[0] * level_sc);
179 for (c = 1; c < sclink->channels; c++)
180 abs_sample = FFMAX(fabs(scsrc[c] * level_sc), abs_sample);
182 for (c = 1; c < sclink->channels; c++)
183 abs_sample += fabs(scsrc[c] * level_sc);
185 abs_sample /= sclink->channels;
189 abs_sample *= abs_sample;
191 s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
194 detector = (s->detection ? s->adj_knee_stop : s->lin_knee_stop);
195 detected = s->lin_slope < detector;
197 detector = (s->detection ? s->adj_knee_start : s->lin_knee_start);
198 detected = s->lin_slope > detector;
201 if (s->lin_slope > 0.0 && detected)
202 gain = output_gain(s->lin_slope, s->ratio, s->thres, s->knee,
203 s->knee_start, s->knee_stop,
204 s->compressed_knee_start,
205 s->compressed_knee_stop,
206 s->detection, s->mode);
208 for (c = 0; c < inlink->channels; c++)
209 dst[c] = src[c] * level_in * (gain * makeup * mix + (1. - mix));
211 src += inlink->channels;
212 dst += inlink->channels;
213 scsrc += sclink->channels;
217 #if CONFIG_SIDECHAINCOMPRESS_FILTER
218 static int activate(AVFilterContext *ctx)
220 SidechainCompressContext *s = ctx->priv;
221 AVFrame *out = NULL, *in[2] = { NULL };
222 int ret, i, nb_samples;
225 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
226 if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &in[0])) > 0) {
227 av_audio_fifo_write(s->fifo[0], (void **)in[0]->extended_data,
229 av_frame_free(&in[0]);
233 if ((ret = ff_inlink_consume_frame(ctx->inputs[1], &in[1])) > 0) {
234 av_audio_fifo_write(s->fifo[1], (void **)in[1]->extended_data,
236 av_frame_free(&in[1]);
241 nb_samples = FFMIN(av_audio_fifo_size(s->fifo[0]), av_audio_fifo_size(s->fifo[1]));
243 out = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
245 return AVERROR(ENOMEM);
246 for (i = 0; i < 2; i++) {
247 in[i] = ff_get_audio_buffer(ctx->inputs[i], nb_samples);
249 av_frame_free(&in[0]);
250 av_frame_free(&in[1]);
252 return AVERROR(ENOMEM);
254 av_audio_fifo_read(s->fifo[i], (void **)in[i]->data, nb_samples);
257 dst = (double *)out->data[0];
259 s->pts += nb_samples;
261 compressor(s, (double *)in[0]->data[0], dst,
262 (double *)in[1]->data[0], nb_samples,
263 s->level_in, s->level_sc,
264 ctx->inputs[0], ctx->inputs[1]);
266 av_frame_free(&in[0]);
267 av_frame_free(&in[1]);
269 ret = ff_filter_frame(ctx->outputs[0], out);
273 FF_FILTER_FORWARD_STATUS(ctx->inputs[0], ctx->outputs[0]);
274 FF_FILTER_FORWARD_STATUS(ctx->inputs[1], ctx->outputs[0]);
275 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
276 if (!av_audio_fifo_size(s->fifo[0]))
277 ff_inlink_request_frame(ctx->inputs[0]);
278 if (!av_audio_fifo_size(s->fifo[1]))
279 ff_inlink_request_frame(ctx->inputs[1]);
284 static int query_formats(AVFilterContext *ctx)
286 AVFilterFormats *formats;
287 AVFilterChannelLayouts *layouts = NULL;
288 static const enum AVSampleFormat sample_fmts[] = {
294 if (!ctx->inputs[0]->in_channel_layouts ||
295 !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
296 av_log(ctx, AV_LOG_WARNING,
297 "No channel layout for input 1\n");
298 return AVERROR(EAGAIN);
301 if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
302 (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
305 for (i = 0; i < 2; i++) {
306 layouts = ff_all_channel_counts();
307 if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
311 formats = ff_make_format_list(sample_fmts);
312 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
315 formats = ff_all_samplerates();
316 return ff_set_common_samplerates(ctx, formats);
319 static int config_output(AVFilterLink *outlink)
321 AVFilterContext *ctx = outlink->src;
322 SidechainCompressContext *s = ctx->priv;
324 if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
325 av_log(ctx, AV_LOG_ERROR,
326 "Inputs must have the same sample rate "
327 "%d for in0 vs %d for in1\n",
328 ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
329 return AVERROR(EINVAL);
332 outlink->sample_rate = ctx->inputs[0]->sample_rate;
333 outlink->time_base = ctx->inputs[0]->time_base;
334 outlink->channel_layout = ctx->inputs[0]->channel_layout;
335 outlink->channels = ctx->inputs[0]->channels;
337 s->fifo[0] = av_audio_fifo_alloc(ctx->inputs[0]->format, ctx->inputs[0]->channels, 1024);
338 s->fifo[1] = av_audio_fifo_alloc(ctx->inputs[1]->format, ctx->inputs[1]->channels, 1024);
339 if (!s->fifo[0] || !s->fifo[1])
340 return AVERROR(ENOMEM);
342 compressor_config_output(outlink);
347 static av_cold void uninit(AVFilterContext *ctx)
349 SidechainCompressContext *s = ctx->priv;
351 av_audio_fifo_free(s->fifo[0]);
352 av_audio_fifo_free(s->fifo[1]);
355 static const AVFilterPad sidechaincompress_inputs[] = {
358 .type = AVMEDIA_TYPE_AUDIO,
361 .type = AVMEDIA_TYPE_AUDIO,
366 static const AVFilterPad sidechaincompress_outputs[] = {
369 .type = AVMEDIA_TYPE_AUDIO,
370 .config_props = config_output,
375 AVFilter ff_af_sidechaincompress = {
376 .name = "sidechaincompress",
377 .description = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
378 .priv_size = sizeof(SidechainCompressContext),
379 .priv_class = &sidechaincompress_class,
380 .query_formats = query_formats,
381 .activate = activate,
383 .inputs = sidechaincompress_inputs,
384 .outputs = sidechaincompress_outputs,
386 #endif /* CONFIG_SIDECHAINCOMPRESS_FILTER */
388 #if CONFIG_ACOMPRESSOR_FILTER
389 static int acompressor_filter_frame(AVFilterLink *inlink, AVFrame *in)
391 const double *src = (const double *)in->data[0];
392 AVFilterContext *ctx = inlink->dst;
393 SidechainCompressContext *s = ctx->priv;
394 AVFilterLink *outlink = ctx->outputs[0];
398 if (av_frame_is_writable(in)) {
401 out = ff_get_audio_buffer(outlink, in->nb_samples);
404 return AVERROR(ENOMEM);
406 av_frame_copy_props(out, in);
408 dst = (double *)out->data[0];
410 compressor(s, src, dst, src, in->nb_samples,
411 s->level_in, s->level_in,
416 return ff_filter_frame(outlink, out);
419 static int acompressor_query_formats(AVFilterContext *ctx)
421 AVFilterFormats *formats;
422 AVFilterChannelLayouts *layouts;
423 static const enum AVSampleFormat sample_fmts[] = {
429 layouts = ff_all_channel_counts();
431 return AVERROR(ENOMEM);
432 ret = ff_set_common_channel_layouts(ctx, layouts);
436 formats = ff_make_format_list(sample_fmts);
438 return AVERROR(ENOMEM);
439 ret = ff_set_common_formats(ctx, formats);
443 formats = ff_all_samplerates();
445 return AVERROR(ENOMEM);
446 return ff_set_common_samplerates(ctx, formats);
449 #define acompressor_options options
450 AVFILTER_DEFINE_CLASS(acompressor);
452 static const AVFilterPad acompressor_inputs[] = {
455 .type = AVMEDIA_TYPE_AUDIO,
456 .filter_frame = acompressor_filter_frame,
461 static const AVFilterPad acompressor_outputs[] = {
464 .type = AVMEDIA_TYPE_AUDIO,
465 .config_props = compressor_config_output,
470 AVFilter ff_af_acompressor = {
471 .name = "acompressor",
472 .description = NULL_IF_CONFIG_SMALL("Audio compressor."),
473 .priv_size = sizeof(SidechainCompressContext),
474 .priv_class = &acompressor_class,
475 .query_formats = acompressor_query_formats,
476 .inputs = acompressor_inputs,
477 .outputs = acompressor_outputs,
479 #endif /* CONFIG_ACOMPRESSOR_FILTER */