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;
57 double adj_knee_start;
58 double compressed_knee_stop;
64 } SidechainCompressContext;
66 #define OFFSET(x) offsetof(SidechainCompressContext, x)
67 #define A AV_OPT_FLAG_AUDIO_PARAM
68 #define F AV_OPT_FLAG_FILTERING_PARAM
70 static const AVOption options[] = {
71 { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A|F },
72 { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0.000976563, 1, A|F },
73 { "ratio", "set ratio", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 20, A|F },
74 { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 2000, A|F },
75 { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=250}, 0.01, 9000, A|F },
76 { "makeup", "set make up gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 64, A|F },
77 { "knee", "set knee", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=2.82843}, 1, 8, A|F },
78 { "link", "set link type", OFFSET(link), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A|F, "link" },
79 { "average", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F, "link" },
80 { "maximum", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F, "link" },
81 { "detection", "set detection", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, A|F, "detection" },
82 { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F, "detection" },
83 { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F, "detection" },
84 { "level_sc", "set sidechain gain", OFFSET(level_sc), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A|F },
85 { "mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, A|F },
89 #define sidechaincompress_options options
90 AVFILTER_DEFINE_CLASS(sidechaincompress);
92 // A fake infinity value (because real infinity may break some hosts)
93 #define FAKE_INFINITY (65536.0 * 65536.0)
95 // Check for infinity (with appropriate-ish tolerance)
96 #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
98 static double output_gain(double lin_slope, double ratio, double thres,
99 double knee, double knee_start, double knee_stop,
100 double compressed_knee_stop, int detection)
102 double slope = log(lin_slope);
109 if (IS_FAKE_INFINITY(ratio)) {
113 gain = (slope - thres) / ratio + thres;
117 if (knee > 1.0 && slope < knee_stop)
118 gain = hermite_interpolation(slope, knee_start, knee_stop,
119 knee_start, compressed_knee_stop,
122 return exp(gain - slope);
125 static int compressor_config_output(AVFilterLink *outlink)
127 AVFilterContext *ctx = outlink->src;
128 SidechainCompressContext *s = ctx->priv;
130 s->thres = log(s->threshold);
131 s->lin_knee_start = s->threshold / sqrt(s->knee);
132 s->adj_knee_start = s->lin_knee_start * s->lin_knee_start;
133 s->knee_start = log(s->lin_knee_start);
134 s->knee_stop = log(s->threshold * sqrt(s->knee));
135 s->compressed_knee_stop = (s->knee_stop - s->thres) / s->ratio + s->thres;
137 s->attack_coeff = FFMIN(1., 1. / (s->attack * outlink->sample_rate / 4000.));
138 s->release_coeff = FFMIN(1., 1. / (s->release * outlink->sample_rate / 4000.));
143 static void compressor(SidechainCompressContext *s,
144 const double *src, double *dst, const double *scsrc, int nb_samples,
145 double level_in, double level_sc,
146 AVFilterLink *inlink, AVFilterLink *sclink)
148 const double makeup = s->makeup;
149 const double mix = s->mix;
152 for (i = 0; i < nb_samples; i++) {
153 double abs_sample, gain = 1.0;
155 abs_sample = fabs(scsrc[0] * level_sc);
158 for (c = 1; c < sclink->channels; c++)
159 abs_sample = FFMAX(fabs(scsrc[c] * level_sc), abs_sample);
161 for (c = 1; c < sclink->channels; c++)
162 abs_sample += fabs(scsrc[c] * level_sc);
164 abs_sample /= sclink->channels;
168 abs_sample *= abs_sample;
170 s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
172 if (s->lin_slope > 0.0 && s->lin_slope > (s->detection ? s->adj_knee_start : s->lin_knee_start))
173 gain = output_gain(s->lin_slope, s->ratio, s->thres, s->knee,
174 s->knee_start, s->knee_stop,
175 s->compressed_knee_stop, s->detection);
177 for (c = 0; c < inlink->channels; c++)
178 dst[c] = src[c] * level_in * (gain * makeup * mix + (1. - mix));
180 src += inlink->channels;
181 dst += inlink->channels;
182 scsrc += sclink->channels;
186 #if CONFIG_SIDECHAINCOMPRESS_FILTER
187 static int activate(AVFilterContext *ctx)
189 SidechainCompressContext *s = ctx->priv;
190 AVFrame *out = NULL, *in[2] = { NULL };
191 int ret, i, status, nb_samples;
195 if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &in[0])) > 0) {
196 av_audio_fifo_write(s->fifo[0], (void **)in[0]->extended_data,
198 av_frame_free(&in[0]);
202 if ((ret = ff_inlink_consume_frame(ctx->inputs[1], &in[1])) > 0) {
203 av_audio_fifo_write(s->fifo[1], (void **)in[1]->extended_data,
205 av_frame_free(&in[1]);
210 nb_samples = FFMIN(av_audio_fifo_size(s->fifo[0]), av_audio_fifo_size(s->fifo[1]));
212 out = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
214 return AVERROR(ENOMEM);
215 for (i = 0; i < 2; i++) {
216 in[i] = ff_get_audio_buffer(ctx->inputs[i], nb_samples);
218 av_frame_free(&in[0]);
219 av_frame_free(&in[1]);
221 return AVERROR(ENOMEM);
223 av_audio_fifo_read(s->fifo[i], (void **)in[i]->data, nb_samples);
226 dst = (double *)out->data[0];
228 s->pts += nb_samples;
230 compressor(s, (double *)in[0]->data[0], dst,
231 (double *)in[1]->data[0], nb_samples,
232 s->level_in, s->level_sc,
233 ctx->inputs[0], ctx->inputs[1]);
235 av_frame_free(&in[0]);
236 av_frame_free(&in[1]);
238 ret = ff_filter_frame(ctx->outputs[0], out);
242 if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
243 ff_outlink_set_status(ctx->outputs[0], status, pts);
245 } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
246 ff_outlink_set_status(ctx->outputs[0], status, pts);
249 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
250 if (!av_audio_fifo_size(s->fifo[0]))
251 ff_inlink_request_frame(ctx->inputs[0]);
252 if (!av_audio_fifo_size(s->fifo[1]))
253 ff_inlink_request_frame(ctx->inputs[1]);
259 static int query_formats(AVFilterContext *ctx)
261 AVFilterFormats *formats;
262 AVFilterChannelLayouts *layouts = NULL;
263 static const enum AVSampleFormat sample_fmts[] = {
269 if (!ctx->inputs[0]->in_channel_layouts ||
270 !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
271 av_log(ctx, AV_LOG_WARNING,
272 "No channel layout for input 1\n");
273 return AVERROR(EAGAIN);
276 if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
277 (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
280 for (i = 0; i < 2; i++) {
281 layouts = ff_all_channel_counts();
282 if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
286 formats = ff_make_format_list(sample_fmts);
287 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
290 formats = ff_all_samplerates();
291 return ff_set_common_samplerates(ctx, formats);
294 static int config_output(AVFilterLink *outlink)
296 AVFilterContext *ctx = outlink->src;
297 SidechainCompressContext *s = ctx->priv;
299 if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
300 av_log(ctx, AV_LOG_ERROR,
301 "Inputs must have the same sample rate "
302 "%d for in0 vs %d for in1\n",
303 ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
304 return AVERROR(EINVAL);
307 outlink->sample_rate = ctx->inputs[0]->sample_rate;
308 outlink->time_base = ctx->inputs[0]->time_base;
309 outlink->channel_layout = ctx->inputs[0]->channel_layout;
310 outlink->channels = ctx->inputs[0]->channels;
312 s->fifo[0] = av_audio_fifo_alloc(ctx->inputs[0]->format, ctx->inputs[0]->channels, 1024);
313 s->fifo[1] = av_audio_fifo_alloc(ctx->inputs[1]->format, ctx->inputs[1]->channels, 1024);
314 if (!s->fifo[0] || !s->fifo[1])
315 return AVERROR(ENOMEM);
317 compressor_config_output(outlink);
322 static av_cold void uninit(AVFilterContext *ctx)
324 SidechainCompressContext *s = ctx->priv;
326 av_audio_fifo_free(s->fifo[0]);
327 av_audio_fifo_free(s->fifo[1]);
330 static const AVFilterPad sidechaincompress_inputs[] = {
333 .type = AVMEDIA_TYPE_AUDIO,
336 .type = AVMEDIA_TYPE_AUDIO,
341 static const AVFilterPad sidechaincompress_outputs[] = {
344 .type = AVMEDIA_TYPE_AUDIO,
345 .config_props = config_output,
350 AVFilter ff_af_sidechaincompress = {
351 .name = "sidechaincompress",
352 .description = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
353 .priv_size = sizeof(SidechainCompressContext),
354 .priv_class = &sidechaincompress_class,
355 .query_formats = query_formats,
356 .activate = activate,
358 .inputs = sidechaincompress_inputs,
359 .outputs = sidechaincompress_outputs,
361 #endif /* CONFIG_SIDECHAINCOMPRESS_FILTER */
363 #if CONFIG_ACOMPRESSOR_FILTER
364 static int acompressor_filter_frame(AVFilterLink *inlink, AVFrame *in)
366 const double *src = (const double *)in->data[0];
367 AVFilterContext *ctx = inlink->dst;
368 SidechainCompressContext *s = ctx->priv;
369 AVFilterLink *outlink = ctx->outputs[0];
373 if (av_frame_is_writable(in)) {
376 out = ff_get_audio_buffer(inlink, in->nb_samples);
379 return AVERROR(ENOMEM);
381 av_frame_copy_props(out, in);
383 dst = (double *)out->data[0];
385 compressor(s, src, dst, src, in->nb_samples,
386 s->level_in, s->level_in,
391 return ff_filter_frame(outlink, out);
394 static int acompressor_query_formats(AVFilterContext *ctx)
396 AVFilterFormats *formats;
397 AVFilterChannelLayouts *layouts;
398 static const enum AVSampleFormat sample_fmts[] = {
404 layouts = ff_all_channel_counts();
406 return AVERROR(ENOMEM);
407 ret = ff_set_common_channel_layouts(ctx, layouts);
411 formats = ff_make_format_list(sample_fmts);
413 return AVERROR(ENOMEM);
414 ret = ff_set_common_formats(ctx, formats);
418 formats = ff_all_samplerates();
420 return AVERROR(ENOMEM);
421 return ff_set_common_samplerates(ctx, formats);
424 #define acompressor_options options
425 AVFILTER_DEFINE_CLASS(acompressor);
427 static const AVFilterPad acompressor_inputs[] = {
430 .type = AVMEDIA_TYPE_AUDIO,
431 .filter_frame = acompressor_filter_frame,
436 static const AVFilterPad acompressor_outputs[] = {
439 .type = AVMEDIA_TYPE_AUDIO,
440 .config_props = compressor_config_output,
445 AVFilter ff_af_acompressor = {
446 .name = "acompressor",
447 .description = NULL_IF_CONFIG_SMALL("Audio compressor."),
448 .priv_size = sizeof(SidechainCompressContext),
449 .priv_class = &acompressor_class,
450 .query_formats = acompressor_query_formats,
451 .inputs = acompressor_inputs,
452 .outputs = acompressor_outputs,
454 #endif /* CONFIG_ACOMPRESSOR_FILTER */