FFmpeg
Loading...
Searching...
No Matches
af_sidechaincompress.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
3 * Copyright (c) 2015 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22/**
23 * @file
24 * Audio (Sidechain) Compressor filter
25 */
26
27#include "config_components.h"
28
31#include "libavutil/common.h"
32#include "libavutil/opt.h"
33
34#include "audio.h"
35#include "avfilter.h"
36#include "filters.h"
37#include "formats.h"
38#include "hermite.h"
39
69
70#define OFFSET(x) offsetof(SidechainCompressContext, x)
71#define A AV_OPT_FLAG_AUDIO_PARAM
72#define F AV_OPT_FLAG_FILTERING_PARAM
73#define R AV_OPT_FLAG_RUNTIME_PARAM
74
75static const AVOption options[] = {
76 { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A|F|R },
77 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A|F|R, .unit = "mode" },
78 { "downward",0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F|R, .unit = "mode" },
79 { "upward", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F|R, .unit = "mode" },
80 { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0.000976563, 1, A|F|R },
81 { "ratio", "set ratio", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 20, A|F|R },
82 { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 2000, A|F|R },
83 { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=250}, 0.01, 9000, A|F|R },
84 { "makeup", "set make up gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 64, A|F|R },
85 { "knee", "set knee", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=2.82843}, 1, 8, A|F|R },
86 { "link", "set link type", OFFSET(link), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A|F|R, .unit = "link" },
87 { "average", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F|R, .unit = "link" },
88 { "maximum", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F|R, .unit = "link" },
89 { "detection", "set detection", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, A|F|R, .unit = "detection" },
90 { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F|R, .unit = "detection" },
91 { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F|R, .unit = "detection" },
92 { "level_sc", "set sidechain gain", OFFSET(level_sc), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A|F|R },
93 { "mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, A|F|R },
94 { NULL }
95};
96
97AVFILTER_DEFINE_CLASS_EXT(sidechaincompress_acompressor,
98 "acompressor/sidechaincompress",
99 options);
100
101// A fake infinity value (because real infinity may break some hosts)
102#define FAKE_INFINITY (65536.0 * 65536.0)
103
104// Check for infinity (with appropriate-ish tolerance)
105#define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
106
107static double output_gain(double lin_slope, double ratio, double thres,
108 double knee, double knee_start, double knee_stop,
109 double compressed_knee_start,
110 double compressed_knee_stop,
111 int detection, int mode)
112{
113 double slope = log(lin_slope);
114 double gain = 0.0;
115 double delta = 0.0;
116
117 if (detection)
118 slope *= 0.5;
119
120 if (IS_FAKE_INFINITY(ratio)) {
121 gain = thres;
122 delta = 0.0;
123 } else {
124 gain = (slope - thres) / ratio + thres;
125 delta = 1.0 / ratio;
126 }
127
128 if (mode) {
129 if (knee > 1.0 && slope > knee_start)
130 gain = hermite_interpolation(slope, knee_stop, knee_start,
131 knee_stop, compressed_knee_start,
132 1.0, delta);
133 } else {
134 if (knee > 1.0 && slope < knee_stop)
135 gain = hermite_interpolation(slope, knee_start, knee_stop,
136 knee_start, compressed_knee_stop,
137 1.0, delta);
138 }
139
140 return exp(gain - slope);
141}
142
144{
145 AVFilterContext *ctx = outlink->src;
147
148 s->thres = log(s->threshold);
149 s->lin_knee_start = s->threshold / sqrt(s->knee);
150 s->lin_knee_stop = s->threshold * sqrt(s->knee);
151 s->adj_knee_start = s->lin_knee_start * s->lin_knee_start;
152 s->adj_knee_stop = s->lin_knee_stop * s->lin_knee_stop;
153 s->knee_start = log(s->lin_knee_start);
154 s->knee_stop = log(s->lin_knee_stop);
155 s->compressed_knee_start = (s->knee_start - s->thres) / s->ratio + s->thres;
156 s->compressed_knee_stop = (s->knee_stop - s->thres) / s->ratio + s->thres;
157
158 s->attack_coeff = FFMIN(1., 1. / (s->attack * outlink->sample_rate / 4000.));
159 s->release_coeff = FFMIN(1., 1. / (s->release * outlink->sample_rate / 4000.));
160
161 return 0;
162}
163
165 const double *src, double *dst, const double *scsrc, int nb_samples,
166 double level_in, double level_sc,
167 AVFilterLink *inlink, AVFilterLink *sclink)
168{
169 const double makeup = s->makeup;
170 const double mix = s->mix;
171 int i, c;
172
173 for (i = 0; i < nb_samples; i++) {
174 double abs_sample, gain = 1.0;
175 double detector;
176 int detected;
177
178 abs_sample = fabs(scsrc[0] * level_sc);
179
180 if (s->link == 1) {
181 for (c = 1; c < sclink->ch_layout.nb_channels; c++)
182 abs_sample = FFMAX(fabs(scsrc[c] * level_sc), abs_sample);
183 } else {
184 for (c = 1; c < sclink->ch_layout.nb_channels; c++)
185 abs_sample += fabs(scsrc[c] * level_sc);
186
187 abs_sample /= sclink->ch_layout.nb_channels;
188 }
189
190 if (s->detection)
191 abs_sample *= abs_sample;
192
193 s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
194
195 if (s->mode) {
196 detector = (s->detection ? s->adj_knee_stop : s->lin_knee_stop);
197 detected = s->lin_slope < detector;
198 } else {
199 detector = (s->detection ? s->adj_knee_start : s->lin_knee_start);
200 detected = s->lin_slope > detector;
201 }
202
203 if (s->lin_slope > 0.0 && detected)
204 gain = output_gain(s->lin_slope, s->ratio, s->thres, s->knee,
205 s->knee_start, s->knee_stop,
206 s->compressed_knee_start,
207 s->compressed_knee_stop,
208 s->detection, s->mode);
209
210 for (c = 0; c < inlink->ch_layout.nb_channels; c++)
211 dst[c] = src[c] * level_in * (gain * makeup * mix + (1. - mix));
212
213 src += inlink->ch_layout.nb_channels;
214 dst += inlink->ch_layout.nb_channels;
215 scsrc += sclink->ch_layout.nb_channels;
216 }
217}
218
219static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
220 char *res, int res_len, int flags)
221{
222 int ret;
223
224 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
225 if (ret < 0)
226 return ret;
227
228 compressor_config_output(ctx->outputs[0]);
229
230 return 0;
231}
232
233#if CONFIG_SIDECHAINCOMPRESS_FILTER
234static int activate(AVFilterContext *ctx)
235{
237 AVFrame *out = NULL, *in[2] = { NULL };
238 int ret, i, nb_samples;
239 double *dst;
240
242 if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &in[0])) > 0) {
243 av_audio_fifo_write(s->fifo[0], (void **)in[0]->extended_data,
244 in[0]->nb_samples);
245 av_frame_free(&in[0]);
246 }
247 if (ret < 0)
248 return ret;
249 if ((ret = ff_inlink_consume_frame(ctx->inputs[1], &in[1])) > 0) {
250 av_audio_fifo_write(s->fifo[1], (void **)in[1]->extended_data,
251 in[1]->nb_samples);
252 av_frame_free(&in[1]);
253 }
254 if (ret < 0)
255 return ret;
256
257 nb_samples = FFMIN(av_audio_fifo_size(s->fifo[0]), av_audio_fifo_size(s->fifo[1]));
258 if (nb_samples) {
259 out = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
260 if (!out)
261 return AVERROR(ENOMEM);
262 for (i = 0; i < 2; i++) {
263 in[i] = ff_get_audio_buffer(ctx->inputs[i], nb_samples);
264 if (!in[i]) {
265 av_frame_free(&in[0]);
266 av_frame_free(&in[1]);
268 return AVERROR(ENOMEM);
269 }
270 av_audio_fifo_read(s->fifo[i], (void **)in[i]->data, nb_samples);
271 }
272
273 dst = (double *)out->data[0];
274 out->pts = s->pts;
275 s->pts += av_rescale_q(nb_samples, (AVRational){1, ctx->outputs[0]->sample_rate}, ctx->outputs[0]->time_base);
276
277 compressor(s, (double *)in[0]->data[0], dst,
278 (double *)in[1]->data[0], nb_samples,
279 s->level_in, s->level_sc,
280 ctx->inputs[0], ctx->inputs[1]);
281
282 av_frame_free(&in[0]);
283 av_frame_free(&in[1]);
284
285 ret = ff_filter_frame(ctx->outputs[0], out);
286 if (ret < 0)
287 return ret;
288 }
289 FF_FILTER_FORWARD_STATUS(ctx->inputs[0], ctx->outputs[0]);
290 FF_FILTER_FORWARD_STATUS(ctx->inputs[1], ctx->outputs[0]);
291 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
292 if (!av_audio_fifo_size(s->fifo[0]))
293 ff_inlink_request_frame(ctx->inputs[0]);
294 if (!av_audio_fifo_size(s->fifo[1]))
295 ff_inlink_request_frame(ctx->inputs[1]);
296 }
297 return 0;
298}
299
300static int query_formats(const AVFilterContext *ctx,
301 AVFilterFormatsConfig **cfg_in,
302 AVFilterFormatsConfig **cfg_out)
303{
304 static const enum AVSampleFormat sample_fmts[] = {
307 };
308 int ret;
309
310 /* Generic code will link the channel properties of the main input and the output;
311 * it won't touch the second input as its channel_layouts is already set. */
313 &cfg_in[1]->channel_layouts);
314 if (ret < 0)
315 return ret;
316
317 if ((ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts)) < 0)
318 return ret;
319
320 return 0;
321}
322
323static int config_output(AVFilterLink *outlink)
324{
325 AVFilterContext *ctx = outlink->src;
327
328 outlink->time_base = ctx->inputs[0]->time_base;
329
330 s->fifo[0] = av_audio_fifo_alloc(ctx->inputs[0]->format, ctx->inputs[0]->ch_layout.nb_channels, 1024);
331 s->fifo[1] = av_audio_fifo_alloc(ctx->inputs[1]->format, ctx->inputs[1]->ch_layout.nb_channels, 1024);
332 if (!s->fifo[0] || !s->fifo[1])
333 return AVERROR(ENOMEM);
334
336
337 return 0;
338}
339
340static av_cold void uninit(AVFilterContext *ctx)
341{
343
344 av_audio_fifo_free(s->fifo[0]);
345 av_audio_fifo_free(s->fifo[1]);
346}
347
348static const AVFilterPad sidechaincompress_inputs[] = {
349 {
350 .name = "main",
351 .type = AVMEDIA_TYPE_AUDIO,
352 },{
353 .name = "sidechain",
354 .type = AVMEDIA_TYPE_AUDIO,
355 },
356};
357
358static const AVFilterPad sidechaincompress_outputs[] = {
359 {
360 .name = "default",
361 .type = AVMEDIA_TYPE_AUDIO,
362 .config_props = config_output,
363 },
364};
365
367 .p.name = "sidechaincompress",
368 .p.description = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
369 .p.priv_class = &sidechaincompress_acompressor_class,
370 .priv_size = sizeof(SidechainCompressContext),
372 .uninit = uninit,
373 FILTER_INPUTS(sidechaincompress_inputs),
374 FILTER_OUTPUTS(sidechaincompress_outputs),
376 .process_command = process_command,
377};
378#endif /* CONFIG_SIDECHAINCOMPRESS_FILTER */
379
380#if CONFIG_ACOMPRESSOR_FILTER
381static int acompressor_filter_frame(AVFilterLink *inlink, AVFrame *in)
382{
383 const double *src = (const double *)in->data[0];
384 AVFilterContext *ctx = inlink->dst;
386 AVFilterLink *outlink = ctx->outputs[0];
387 AVFrame *out;
388 double *dst;
389
390 if (av_frame_is_writable(in)) {
391 out = in;
392 } else {
393 out = ff_get_audio_buffer(outlink, in->nb_samples);
394 if (!out) {
395 av_frame_free(&in);
396 return AVERROR(ENOMEM);
397 }
399 }
400 dst = (double *)out->data[0];
401
402 compressor(s, src, dst, src, in->nb_samples,
403 s->level_in, s->level_in,
404 inlink, inlink);
405
406 if (out != in)
407 av_frame_free(&in);
408 return ff_filter_frame(outlink, out);
409}
410
411static const AVFilterPad acompressor_inputs[] = {
412 {
413 .name = "default",
414 .type = AVMEDIA_TYPE_AUDIO,
415 .filter_frame = acompressor_filter_frame,
416 },
417};
418
419static const AVFilterPad acompressor_outputs[] = {
420 {
421 .name = "default",
422 .type = AVMEDIA_TYPE_AUDIO,
423 .config_props = compressor_config_output,
424 },
425};
426
428 .p.name = "acompressor",
429 .p.description = NULL_IF_CONFIG_SMALL("Audio compressor."),
430 .p.priv_class = &sidechaincompress_acompressor_class,
431 .priv_size = sizeof(SidechainCompressContext),
432 FILTER_INPUTS(acompressor_inputs),
433 FILTER_OUTPUTS(acompressor_outputs),
435 .process_command = process_command,
436};
437#endif /* CONFIG_ACOMPRESSOR_FILTER */
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static enum AVSampleFormat sample_fmts[]
Definition adpcmenc.c:933
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static double output_gain(double lin_slope, double ratio, double thres, double knee, double knee_start, double knee_stop, double compressed_knee_start, double compressed_knee_stop, int detection, int mode)
static void compressor(SidechainCompressContext *s, const double *src, double *dst, const double *scsrc, int nb_samples, double level_in, double level_sc, AVFilterLink *inlink, AVFilterLink *sclink)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static int compressor_config_output(AVFilterLink *outlink)
#define OFFSET(x)
#define IS_FAKE_INFINITY(value)
const FFFilter ff_af_acompressor
const FFFilter ff_af_sidechaincompress
#define A(x)
Definition vpx_arith.h:28
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
Audio FIFO Buffer.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition avfilter.c:1690
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition avfilter.c:1520
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition avfilter.c:1623
Main libavfilter public API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
common internal and external API header
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float fabs(float a)
static const uint16_t channel_layouts[7]
Definition dca_lbr.c:112
#define F(x)
int8_t exp
Definition eval.c:76
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition formats.c:688
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition formats.c:751
int ff_set_sample_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVSampleFormat *fmts)
Definition formats.c:1154
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition audio_fifo.c:62
int av_audio_fifo_write(AVAudioFifo *af, void *const *data, int nb_samples)
Write data to an AVAudioFifo.
Definition audio_fifo.c:119
int av_audio_fifo_read(AVAudioFifo *af, void *const *data, int nb_samples)
Read data from an AVAudioFifo.
Definition audio_fifo.c:175
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition audio_fifo.c:48
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition audio_fifo.c:222
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
@ AV_SAMPLE_FMT_DBL
double
Definition samplefmt.h:61
static double hermite_interpolation(double x, double x0, double x1, double p0, double p1, double m0, double m1)
Definition hermite.h:22
#define R
Definition huffyuv.h:44
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
static int mix(int c0, int c1)
Definition 4xm.c:717
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition filters.h:666
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition filters.h:652
#define AVFILTER_DEFINE_CLASS_EXT(name, desc, options)
Definition filters.h:470
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition filters.h:257
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
const char data[16]
Definition mxf.c:149
AVOptions.
Context for an Audio FIFO Buffer.
Definition audio_fifo.c:37
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
AVOption.
Definition opt.h:428
Rational number (pair of numerator and denominator).
Definition rational.h:58
Definition swscale.c:71
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
float delta
static double c[64]