FFmpeg
Loading...
Searching...
No Matches
af_agate.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen, Damien Zammit
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file
23 * Audio (Sidechain) Gate filter
24 */
25
26#include "config_components.h"
27
30#include "libavutil/opt.h"
31#include "avfilter.h"
32#include "audio.h"
33#include "filters.h"
34#include "formats.h"
35#include "hermite.h"
36
37typedef struct AudioGateContext {
38 const AVClass *class;
39
40 double level_in;
41 double level_sc;
42 double attack;
43 double release;
44 double threshold;
45 double ratio;
46 double knee;
47 double makeup;
48 double range;
49 int link;
51 int mode;
52
53 double thres;
54 double knee_start;
55 double knee_stop;
58 double lin_slope;
61
65
66#define OFFSET(x) offsetof(AudioGateContext, x)
67#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
68
69static const AVOption options[] = {
70 { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
71 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, .unit = "mode" },
72 { "downward",0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, .unit = "mode" },
73 { "upward", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, .unit = "mode" },
74 { "range", "set max gain reduction", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=0.06125}, 0, 1, A },
75 { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0, 1, A },
76 { "ratio", "set ratio", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 9000, A },
77 { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 9000, A },
78 { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=250}, 0.01, 9000, A },
79 { "makeup", "set makeup gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 64, A },
80 { "knee", "set knee", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=2.828427125}, 1, 8, A },
81 { "detection", "set detection", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, A, .unit = "detection" },
82 { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, .unit = "detection" },
83 { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, .unit = "detection" },
84 { "link", "set link", OFFSET(link), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, .unit = "link" },
85 { "average", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, .unit = "link" },
86 { "maximum", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, .unit = "link" },
87 { "level_sc", "set sidechain gain", OFFSET(level_sc), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
88 { NULL }
89};
90
91AVFILTER_DEFINE_CLASS_EXT(agate_sidechaingate, "agate/sidechaingate", options);
92
94{
95 AVFilterContext *ctx = inlink->dst;
96 AudioGateContext *s = ctx->priv;
97 double lin_threshold = s->threshold;
98 double lin_knee_sqrt = sqrt(s->knee);
99
100 if (s->detection)
101 lin_threshold *= lin_threshold;
102
103 s->attack_coeff = FFMIN(1., 1. / (s->attack * inlink->sample_rate / 4000.));
104 s->release_coeff = FFMIN(1., 1. / (s->release * inlink->sample_rate / 4000.));
105 s->lin_knee_stop = lin_threshold * lin_knee_sqrt;
106 s->lin_knee_start = lin_threshold / lin_knee_sqrt;
107 s->thres = log(lin_threshold);
108 s->knee_start = log(s->lin_knee_start);
109 s->knee_stop = log(s->lin_knee_stop);
110
111 return 0;
112}
113
114// A fake infinity value (because real infinity may break some hosts)
115#define FAKE_INFINITY (65536.0 * 65536.0)
116
117// Check for infinity (with appropriate-ish tolerance)
118#define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
119
120static double output_gain(double lin_slope, double ratio, double thres,
121 double knee, double knee_start, double knee_stop,
122 double range, int mode)
123{
124 double slope = log(lin_slope);
125 double tratio = ratio;
126 double gain = 0.;
127 double delta = 0.;
128
129 if (IS_FAKE_INFINITY(ratio))
130 tratio = 1000.;
131 gain = (slope - thres) * tratio + thres;
132 delta = tratio;
133
134 if (mode) {
135 if (knee > 1. && slope < knee_stop)
136 gain = hermite_interpolation(slope, knee_stop, knee_start, ((knee_stop - thres) * tratio + thres), knee_start, delta, 1.);
137 } else {
138 if (knee > 1. && slope > knee_start)
139 gain = hermite_interpolation(slope, knee_start, knee_stop, ((knee_start - thres) * tratio + thres), knee_stop, delta, 1.);
140 }
141 return FFMAX(range, exp(gain - slope));
142}
143
145 const double *src, double *dst, const double *scsrc,
146 int nb_samples, double level_in, double level_sc,
147 AVFilterLink *inlink, AVFilterLink *sclink)
148{
149 AVFilterContext *ctx = inlink->dst;
150 const double makeup = s->makeup;
151 const double attack_coeff = s->attack_coeff;
152 const double release_coeff = s->release_coeff;
153 int n, c;
154
155 for (n = 0; n < nb_samples; n++, src += inlink->ch_layout.nb_channels, dst += inlink->ch_layout.nb_channels, scsrc += sclink->ch_layout.nb_channels) {
156 double abs_sample = fabs(scsrc[0] * level_sc), gain = 1.0;
157 double factor;
158 int detected;
159
160 if (s->link == 1) {
161 for (c = 1; c < sclink->ch_layout.nb_channels; c++)
162 abs_sample = FFMAX(fabs(scsrc[c] * level_sc), abs_sample);
163 } else {
164 for (c = 1; c < sclink->ch_layout.nb_channels; c++)
165 abs_sample += fabs(scsrc[c] * level_sc);
166
167 abs_sample /= sclink->ch_layout.nb_channels;
168 }
169
170 if (s->detection)
171 abs_sample *= abs_sample;
172
173 s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? attack_coeff : release_coeff);
174
175 if (s->mode)
176 detected = s->lin_slope > s->lin_knee_start;
177 else
178 detected = s->lin_slope < s->lin_knee_stop;
179
180 if (s->lin_slope > 0.0 && detected)
181 gain = output_gain(s->lin_slope, s->ratio, s->thres,
182 s->knee, s->knee_start, s->knee_stop,
183 s->range, s->mode);
184
185 factor = ctx->is_disabled ? 1.f : level_in * gain * makeup;
186 for (c = 0; c < inlink->ch_layout.nb_channels; c++)
187 dst[c] = src[c] * factor;
188 }
189}
190
191#if CONFIG_AGATE_FILTER
192
193static int filter_frame(AVFilterLink *inlink, AVFrame *in)
194{
195 const double *src = (const double *)in->data[0];
196 AVFilterContext *ctx = inlink->dst;
197 AVFilterLink *outlink = ctx->outputs[0];
198 AudioGateContext *s = ctx->priv;
199 AVFrame *out;
200 double *dst;
201
202 if (av_frame_is_writable(in)) {
203 out = in;
204 } else {
205 out = ff_get_audio_buffer(outlink, in->nb_samples);
206 if (!out) {
207 av_frame_free(&in);
208 return AVERROR(ENOMEM);
209 }
211 }
212 dst = (double *)out->data[0];
213
214 gate(s, src, dst, src, in->nb_samples,
215 s->level_in, s->level_in, inlink, inlink);
216
217 if (out != in)
218 av_frame_free(&in);
219 return ff_filter_frame(outlink, out);
220}
221
222static const AVFilterPad inputs[] = {
223 {
224 .name = "default",
225 .type = AVMEDIA_TYPE_AUDIO,
226 .filter_frame = filter_frame,
227 .config_props = agate_config_input,
228 },
229};
230
231const FFFilter ff_af_agate = {
232 .p.name = "agate",
233 .p.description = NULL_IF_CONFIG_SMALL("Audio gate."),
234 .p.priv_class = &agate_sidechaingate_class,
236 .priv_size = sizeof(AudioGateContext),
240 .process_command = ff_filter_process_command,
241};
242
243#endif /* CONFIG_AGATE_FILTER */
244
245#if CONFIG_SIDECHAINGATE_FILTER
246
247static int activate(AVFilterContext *ctx)
248{
249 AudioGateContext *s = ctx->priv;
250 AVFrame *out = NULL, *in[2] = { NULL };
251 int ret, i, nb_samples;
252 double *dst;
253
255 if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &in[0])) > 0) {
256 av_audio_fifo_write(s->fifo[0], (void **)in[0]->extended_data,
257 in[0]->nb_samples);
258 av_frame_free(&in[0]);
259 }
260 if (ret < 0)
261 return ret;
262 if ((ret = ff_inlink_consume_frame(ctx->inputs[1], &in[1])) > 0) {
263 av_audio_fifo_write(s->fifo[1], (void **)in[1]->extended_data,
264 in[1]->nb_samples);
265 av_frame_free(&in[1]);
266 }
267 if (ret < 0)
268 return ret;
269
270 nb_samples = FFMIN(av_audio_fifo_size(s->fifo[0]), av_audio_fifo_size(s->fifo[1]));
271 if (nb_samples) {
272 out = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
273 if (!out)
274 return AVERROR(ENOMEM);
275 for (i = 0; i < 2; i++) {
276 in[i] = ff_get_audio_buffer(ctx->inputs[i], nb_samples);
277 if (!in[i]) {
278 av_frame_free(&in[0]);
279 av_frame_free(&in[1]);
281 return AVERROR(ENOMEM);
282 }
283 av_audio_fifo_read(s->fifo[i], (void **)in[i]->data, nb_samples);
284 }
285
286 dst = (double *)out->data[0];
287 out->pts = s->pts;
288 s->pts += av_rescale_q(nb_samples, (AVRational){1, ctx->outputs[0]->sample_rate}, ctx->outputs[0]->time_base);
289
290 gate(s, (double *)in[0]->data[0], dst,
291 (double *)in[1]->data[0], nb_samples,
292 s->level_in, s->level_sc,
293 ctx->inputs[0], ctx->inputs[1]);
294
295 av_frame_free(&in[0]);
296 av_frame_free(&in[1]);
297
298 ret = ff_filter_frame(ctx->outputs[0], out);
299 if (ret < 0)
300 return ret;
301 }
302 FF_FILTER_FORWARD_STATUS(ctx->inputs[0], ctx->outputs[0]);
303 FF_FILTER_FORWARD_STATUS(ctx->inputs[1], ctx->outputs[0]);
304 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
305 if (!av_audio_fifo_size(s->fifo[0]))
306 ff_inlink_request_frame(ctx->inputs[0]);
307 if (!av_audio_fifo_size(s->fifo[1]))
308 ff_inlink_request_frame(ctx->inputs[1]);
309 }
310 return 0;
311}
312
313static int scquery_formats(const AVFilterContext *ctx,
314 AVFilterFormatsConfig **cfg_in,
315 AVFilterFormatsConfig **cfg_out)
316{
317 static const enum AVSampleFormat sample_fmts[] = {
320 };
321 int ret;
322
323 /* Generic code will link the channel properties of the main input and the
324 * output; it won't touch the second input as its channel_layouts is already
325 * set. */
327 &cfg_in[1]->channel_layouts);
328 if (ret < 0)
329 return ret;
330
331 if ((ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts)) < 0)
332 return ret;
333
334 return 0;
335}
336
337static int scconfig_output(AVFilterLink *outlink)
338{
339 AVFilterContext *ctx = outlink->src;
340 AudioGateContext *s = ctx->priv;
341
342 outlink->time_base = ctx->inputs[0]->time_base;
343
344 s->fifo[0] = av_audio_fifo_alloc(ctx->inputs[0]->format, ctx->inputs[0]->ch_layout.nb_channels, 1024);
345 s->fifo[1] = av_audio_fifo_alloc(ctx->inputs[1]->format, ctx->inputs[1]->ch_layout.nb_channels, 1024);
346 if (!s->fifo[0] || !s->fifo[1])
347 return AVERROR(ENOMEM);
348
349
350 agate_config_input(ctx->inputs[0]);
351
352 return 0;
353}
354
355static av_cold void uninit(AVFilterContext *ctx)
356{
357 AudioGateContext *s = ctx->priv;
358
359 av_audio_fifo_free(s->fifo[0]);
360 av_audio_fifo_free(s->fifo[1]);
361}
362
363static const AVFilterPad sidechaingate_inputs[] = {
364 {
365 .name = "main",
366 .type = AVMEDIA_TYPE_AUDIO,
367 },{
368 .name = "sidechain",
369 .type = AVMEDIA_TYPE_AUDIO,
370 },
371};
372
373static const AVFilterPad sidechaingate_outputs[] = {
374 {
375 .name = "default",
376 .type = AVMEDIA_TYPE_AUDIO,
377 .config_props = scconfig_output,
378 },
379};
380
382 .p.name = "sidechaingate",
383 .p.description = NULL_IF_CONFIG_SMALL("Audio sidechain gate."),
384 .p.priv_class = &agate_sidechaingate_class,
386 .priv_size = sizeof(AudioGateContext),
388 .uninit = uninit,
389 FILTER_INPUTS(sidechaingate_inputs),
390 FILTER_OUTPUTS(sidechaingate_outputs),
391 FILTER_QUERY_FUNC2(scquery_formats),
392 .process_command = ff_filter_process_command,
393};
394#endif /* CONFIG_SIDECHAINGATE_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 const AVFilterPad inputs[]
Definition af_aap.c:299
static double output_gain(double lin_slope, double ratio, double thres, double knee, double knee_start, double knee_stop, double range, int mode)
Definition af_agate.c:120
static int agate_config_input(AVFilterLink *inlink)
Definition af_agate.c:93
static void gate(AudioGateContext *s, const double *src, double *dst, const double *scsrc, int nb_samples, double level_in, double level_sc, AVFilterLink *inlink, AVFilterLink *sclink)
Definition af_agate.c:144
#define OFFSET(x)
Definition af_agate.c:66
#define IS_FAKE_INFINITY(value)
Definition af_agate.c:118
const FFFilter ff_af_agate
const FFFilter ff_af_sidechaingate
static FILE * out
static AVFormatContext * ctx
#define A(x)
Definition vpx_arith.h:28
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
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 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.
#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
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
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
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:204
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
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
#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
static const int factor[16]
Definition vf_pp7.c:98
#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
enum AVColorRange range
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
double lin_slope
Definition af_agate.c:58
double knee_stop
Definition af_agate.c:55
double threshold
Definition af_agate.c:44
double lin_knee_stop
Definition af_agate.c:57
double level_in
Definition af_agate.c:40
double attack_coeff
Definition af_agate.c:59
AVAudioFifo * fifo[2]
Definition af_agate.c:62
double release_coeff
Definition af_agate.c:60
double knee_start
Definition af_agate.c:54
double level_sc
Definition af_agate.c:41
double lin_knee_start
Definition af_agate.c:56
Definition swscale.c:71
#define src
Definition vp8dsp.c:248
float delta
static double c[64]