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"
39 typedef struct SidechainCompressContext {
44 double attack, attack_coeff;
45 double release, release_coeff;
55 double lin_knee_start;
56 double adj_knee_start;
57 double compressed_knee_stop;
63 } SidechainCompressContext;
65 #define OFFSET(x) offsetof(SidechainCompressContext, x)
66 #define A AV_OPT_FLAG_AUDIO_PARAM
67 #define F AV_OPT_FLAG_FILTERING_PARAM
69 static const AVOption options[] = {
70 { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A|F },
71 { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0.000976563, 1, A|F },
72 { "ratio", "set ratio", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 20, A|F },
73 { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 2000, A|F },
74 { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=250}, 0.01, 9000, A|F },
75 { "makeup", "set make up gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 64, A|F },
76 { "knee", "set knee", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=2.82843}, 1, 8, A|F },
77 { "link", "set link type", OFFSET(link), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A|F, "link" },
78 { "average", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F, "link" },
79 { "maximum", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F, "link" },
80 { "detection", "set detection", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, A|F, "detection" },
81 { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F, "detection" },
82 { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F, "detection" },
83 { "level_sc", "set sidechain gain", OFFSET(level_sc), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A|F },
84 { "mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, A|F },
88 #define sidechaincompress_options options
89 AVFILTER_DEFINE_CLASS(sidechaincompress);
91 // A fake infinity value (because real infinity may break some hosts)
92 #define FAKE_INFINITY (65536.0 * 65536.0)
94 // Check for infinity (with appropriate-ish tolerance)
95 #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
97 static double output_gain(double lin_slope, double ratio, double thres,
98 double knee, double knee_start, double knee_stop,
99 double compressed_knee_stop, int detection)
101 double slope = log(lin_slope);
108 if (IS_FAKE_INFINITY(ratio)) {
112 gain = (slope - thres) / ratio + thres;
116 if (knee > 1.0 && slope < knee_stop)
117 gain = hermite_interpolation(slope, knee_start, knee_stop,
118 knee_start, compressed_knee_stop,
121 return exp(gain - slope);
124 static int compressor_config_output(AVFilterLink *outlink)
126 AVFilterContext *ctx = outlink->src;
127 SidechainCompressContext *s = ctx->priv;
129 s->thres = log(s->threshold);
130 s->lin_knee_start = s->threshold / sqrt(s->knee);
131 s->adj_knee_start = s->lin_knee_start * s->lin_knee_start;
132 s->knee_start = log(s->lin_knee_start);
133 s->knee_stop = log(s->threshold * sqrt(s->knee));
134 s->compressed_knee_stop = (s->knee_stop - s->thres) / s->ratio + s->thres;
136 s->attack_coeff = FFMIN(1., 1. / (s->attack * outlink->sample_rate / 4000.));
137 s->release_coeff = FFMIN(1., 1. / (s->release * outlink->sample_rate / 4000.));
142 static void compressor(SidechainCompressContext *s,
143 const double *src, double *dst, const double *scsrc, int nb_samples,
144 double level_in, double level_sc,
145 AVFilterLink *inlink, AVFilterLink *sclink)
147 const double makeup = s->makeup;
148 const double mix = s->mix;
151 for (i = 0; i < nb_samples; i++) {
152 double abs_sample, gain = 1.0;
154 abs_sample = fabs(scsrc[0] * level_sc);
157 for (c = 1; c < sclink->channels; c++)
158 abs_sample = FFMAX(fabs(scsrc[c] * level_sc), abs_sample);
160 for (c = 1; c < sclink->channels; c++)
161 abs_sample += fabs(scsrc[c] * level_sc);
163 abs_sample /= sclink->channels;
167 abs_sample *= abs_sample;
169 s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
171 if (s->lin_slope > 0.0 && s->lin_slope > (s->detection ? s->adj_knee_start : s->lin_knee_start))
172 gain = output_gain(s->lin_slope, s->ratio, s->thres, s->knee,
173 s->knee_start, s->knee_stop,
174 s->compressed_knee_stop, s->detection);
176 for (c = 0; c < inlink->channels; c++)
177 dst[c] = src[c] * level_in * (gain * makeup * mix + (1. - mix));
179 src += inlink->channels;
180 dst += inlink->channels;
181 scsrc += sclink->channels;
185 #if CONFIG_SIDECHAINCOMPRESS_FILTER
186 static int filter_frame(AVFilterLink *link, AVFrame *frame)
188 AVFilterContext *ctx = link->dst;
189 SidechainCompressContext *s = ctx->priv;
190 AVFilterLink *outlink = ctx->outputs[0];
191 AVFrame *out, *in[2] = { NULL };
196 for (i = 0; i < 2; i++)
197 if (link == ctx->inputs[i])
200 av_audio_fifo_write(s->fifo[i], (void **)frame->extended_data,
202 av_frame_free(&frame);
204 nb_samples = FFMIN(av_audio_fifo_size(s->fifo[0]), av_audio_fifo_size(s->fifo[1]));
208 out = ff_get_audio_buffer(outlink, nb_samples);
210 return AVERROR(ENOMEM);
211 for (i = 0; i < 2; i++) {
212 in[i] = ff_get_audio_buffer(ctx->inputs[i], nb_samples);
214 av_frame_free(&in[0]);
215 av_frame_free(&in[1]);
216 return AVERROR(ENOMEM);
218 av_audio_fifo_read(s->fifo[i], (void **)in[i]->data, nb_samples);
221 dst = (double *)out->data[0];
223 s->pts += nb_samples;
225 compressor(s, (double *)in[0]->data[0], dst,
226 (double *)in[1]->data[0], nb_samples,
227 s->level_in, s->level_sc,
228 ctx->inputs[0], ctx->inputs[1]);
230 av_frame_free(&in[0]);
231 av_frame_free(&in[1]);
233 return ff_filter_frame(outlink, out);
236 static int request_frame(AVFilterLink *outlink)
238 AVFilterContext *ctx = outlink->src;
239 SidechainCompressContext *s = ctx->priv;
242 /* get a frame on each input */
243 for (i = 0; i < 2; i++) {
244 AVFilterLink *inlink = ctx->inputs[i];
245 if (!av_audio_fifo_size(s->fifo[i]))
246 return ff_request_frame(inlink);
252 static int query_formats(AVFilterContext *ctx)
254 AVFilterFormats *formats;
255 AVFilterChannelLayouts *layouts = NULL;
256 static const enum AVSampleFormat sample_fmts[] = {
262 if (!ctx->inputs[0]->in_channel_layouts ||
263 !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
264 av_log(ctx, AV_LOG_WARNING,
265 "No channel layout for input 1\n");
266 return AVERROR(EAGAIN);
269 if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
270 (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
273 for (i = 0; i < 2; i++) {
274 layouts = ff_all_channel_counts();
275 if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
279 formats = ff_make_format_list(sample_fmts);
280 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
283 formats = ff_all_samplerates();
284 return ff_set_common_samplerates(ctx, formats);
287 static int config_output(AVFilterLink *outlink)
289 AVFilterContext *ctx = outlink->src;
290 SidechainCompressContext *s = ctx->priv;
292 if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
293 av_log(ctx, AV_LOG_ERROR,
294 "Inputs must have the same sample rate "
295 "%d for in0 vs %d for in1\n",
296 ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
297 return AVERROR(EINVAL);
300 outlink->sample_rate = ctx->inputs[0]->sample_rate;
301 outlink->time_base = ctx->inputs[0]->time_base;
302 outlink->channel_layout = ctx->inputs[0]->channel_layout;
303 outlink->channels = ctx->inputs[0]->channels;
305 s->fifo[0] = av_audio_fifo_alloc(ctx->inputs[0]->format, ctx->inputs[0]->channels, 1024);
306 s->fifo[1] = av_audio_fifo_alloc(ctx->inputs[1]->format, ctx->inputs[1]->channels, 1024);
307 if (!s->fifo[0] || !s->fifo[1])
308 return AVERROR(ENOMEM);
310 compressor_config_output(outlink);
315 static av_cold void uninit(AVFilterContext *ctx)
317 SidechainCompressContext *s = ctx->priv;
319 av_audio_fifo_free(s->fifo[0]);
320 av_audio_fifo_free(s->fifo[1]);
323 static const AVFilterPad sidechaincompress_inputs[] = {
326 .type = AVMEDIA_TYPE_AUDIO,
327 .filter_frame = filter_frame,
330 .type = AVMEDIA_TYPE_AUDIO,
331 .filter_frame = filter_frame,
336 static const AVFilterPad sidechaincompress_outputs[] = {
339 .type = AVMEDIA_TYPE_AUDIO,
340 .config_props = config_output,
341 .request_frame = request_frame,
346 AVFilter ff_af_sidechaincompress = {
347 .name = "sidechaincompress",
348 .description = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
349 .priv_size = sizeof(SidechainCompressContext),
350 .priv_class = &sidechaincompress_class,
351 .query_formats = query_formats,
353 .inputs = sidechaincompress_inputs,
354 .outputs = sidechaincompress_outputs,
356 #endif /* CONFIG_SIDECHAINCOMPRESS_FILTER */
358 #if CONFIG_ACOMPRESSOR_FILTER
359 static int acompressor_filter_frame(AVFilterLink *inlink, AVFrame *in)
361 const double *src = (const double *)in->data[0];
362 AVFilterContext *ctx = inlink->dst;
363 SidechainCompressContext *s = ctx->priv;
364 AVFilterLink *outlink = ctx->outputs[0];
368 if (av_frame_is_writable(in)) {
371 out = ff_get_audio_buffer(inlink, in->nb_samples);
374 return AVERROR(ENOMEM);
376 av_frame_copy_props(out, in);
378 dst = (double *)out->data[0];
380 compressor(s, src, dst, src, in->nb_samples,
381 s->level_in, s->level_in,
386 return ff_filter_frame(outlink, out);
389 static int acompressor_query_formats(AVFilterContext *ctx)
391 AVFilterFormats *formats;
392 AVFilterChannelLayouts *layouts;
393 static const enum AVSampleFormat sample_fmts[] = {
399 layouts = ff_all_channel_counts();
401 return AVERROR(ENOMEM);
402 ret = ff_set_common_channel_layouts(ctx, layouts);
406 formats = ff_make_format_list(sample_fmts);
408 return AVERROR(ENOMEM);
409 ret = ff_set_common_formats(ctx, formats);
413 formats = ff_all_samplerates();
415 return AVERROR(ENOMEM);
416 return ff_set_common_samplerates(ctx, formats);
419 #define acompressor_options options
420 AVFILTER_DEFINE_CLASS(acompressor);
422 static const AVFilterPad acompressor_inputs[] = {
425 .type = AVMEDIA_TYPE_AUDIO,
426 .filter_frame = acompressor_filter_frame,
431 static const AVFilterPad acompressor_outputs[] = {
434 .type = AVMEDIA_TYPE_AUDIO,
435 .config_props = compressor_config_output,
440 AVFilter ff_af_acompressor = {
441 .name = "acompressor",
442 .description = NULL_IF_CONFIG_SMALL("Audio compressor."),
443 .priv_size = sizeof(SidechainCompressContext),
444 .priv_class = &acompressor_class,
445 .query_formats = acompressor_query_formats,
446 .inputs = acompressor_inputs,
447 .outputs = acompressor_outputs,
449 #endif /* CONFIG_ACOMPRESSOR_FILTER */