ffmpeg/libavfilter/af_agate.c

393 lines
14 KiB
C
Raw Permalink Normal View History

/*
* Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen, Damien Zammit
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Audio (Sidechain) Gate filter
*/
#include "config_components.h"
#include "libavutil/audio_fifo.h"
#include "libavutil/channel_layout.h"
#include "libavutil/opt.h"
#include "avfilter.h"
#include "audio.h"
#include "filters.h"
#include "formats.h"
#include "hermite.h"
typedef struct AudioGateContext {
const AVClass *class;
double level_in;
double level_sc;
double attack;
double release;
double threshold;
double ratio;
double knee;
double makeup;
double range;
int link;
int detection;
int mode;
double thres;
double knee_start;
double knee_stop;
double lin_knee_start;
double lin_knee_stop;
double lin_slope;
double attack_coeff;
double release_coeff;
AVAudioFifo *fifo[2];
int64_t pts;
} AudioGateContext;
#define OFFSET(x) offsetof(AudioGateContext, x)
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
static const AVOption options[] = {
{ "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
{ "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, .unit = "mode" },
{ "downward",0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, .unit = "mode" },
{ "upward", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, .unit = "mode" },
{ "range", "set max gain reduction", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=0.06125}, 0, 1, A },
{ "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0, 1, A },
{ "ratio", "set ratio", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 9000, A },
{ "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 9000, A },
{ "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=250}, 0.01, 9000, A },
{ "makeup", "set makeup gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 64, A },
{ "knee", "set knee", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=2.828427125}, 1, 8, A },
{ "detection", "set detection", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, A, .unit = "detection" },
{ "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, .unit = "detection" },
{ "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, .unit = "detection" },
{ "link", "set link", OFFSET(link), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, .unit = "link" },
{ "average", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, .unit = "link" },
{ "maximum", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, .unit = "link" },
{ "level_sc", "set sidechain gain", OFFSET(level_sc), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
{ NULL }
};
AVFILTER_DEFINE_CLASS_EXT(agate_sidechaingate, "agate/sidechaingate", options);
static int agate_config_input(AVFilterLink *inlink)
{
AVFilterContext *ctx = inlink->dst;
AudioGateContext *s = ctx->priv;
double lin_threshold = s->threshold;
double lin_knee_sqrt = sqrt(s->knee);
if (s->detection)
lin_threshold *= lin_threshold;
s->attack_coeff = FFMIN(1., 1. / (s->attack * inlink->sample_rate / 4000.));
s->release_coeff = FFMIN(1., 1. / (s->release * inlink->sample_rate / 4000.));
s->lin_knee_stop = lin_threshold * lin_knee_sqrt;
s->lin_knee_start = lin_threshold / lin_knee_sqrt;
s->thres = log(lin_threshold);
s->knee_start = log(s->lin_knee_start);
s->knee_stop = log(s->lin_knee_stop);
return 0;
}
// A fake infinity value (because real infinity may break some hosts)
#define FAKE_INFINITY (65536.0 * 65536.0)
// Check for infinity (with appropriate-ish tolerance)
#define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
static double output_gain(double lin_slope, double ratio, double thres,
double knee, double knee_start, double knee_stop,
double range, int mode)
{
double slope = log(lin_slope);
double tratio = ratio;
double gain = 0.;
double delta = 0.;
if (IS_FAKE_INFINITY(ratio))
tratio = 1000.;
gain = (slope - thres) * tratio + thres;
delta = tratio;
if (mode) {
if (knee > 1. && slope < knee_stop)
gain = hermite_interpolation(slope, knee_stop, knee_start, ((knee_stop - thres) * tratio + thres), knee_start, delta, 1.);
} else {
if (knee > 1. && slope > knee_start)
gain = hermite_interpolation(slope, knee_start, knee_stop, ((knee_start - thres) * tratio + thres), knee_stop, delta, 1.);
}
return FFMAX(range, exp(gain - slope));
}
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)
{
AVFilterContext *ctx = inlink->dst;
const double makeup = s->makeup;
const double attack_coeff = s->attack_coeff;
const double release_coeff = s->release_coeff;
int n, c;
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) {
double abs_sample = fabs(scsrc[0] * level_sc), gain = 1.0;
double factor;
int detected;
if (s->link == 1) {
for (c = 1; c < sclink->ch_layout.nb_channels; c++)
abs_sample = FFMAX(fabs(scsrc[c] * level_sc), abs_sample);
} else {
for (c = 1; c < sclink->ch_layout.nb_channels; c++)
abs_sample += fabs(scsrc[c] * level_sc);
abs_sample /= sclink->ch_layout.nb_channels;
}
if (s->detection)
abs_sample *= abs_sample;
s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? attack_coeff : release_coeff);
if (s->mode)
detected = s->lin_slope > s->lin_knee_start;
else
detected = s->lin_slope < s->lin_knee_stop;
if (s->lin_slope > 0.0 && detected)
gain = output_gain(s->lin_slope, s->ratio, s->thres,
s->knee, s->knee_start, s->knee_stop,
s->range, s->mode);
factor = ctx->is_disabled ? 1.f : level_in * gain * makeup;
for (c = 0; c < inlink->ch_layout.nb_channels; c++)
dst[c] = src[c] * factor;
}
}
#if CONFIG_AGATE_FILTER
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
{
const double *src = (const double *)in->data[0];
AVFilterContext *ctx = inlink->dst;
AVFilterLink *outlink = ctx->outputs[0];
AudioGateContext *s = ctx->priv;
AVFrame *out;
double *dst;
if (av_frame_is_writable(in)) {
out = in;
} else {
out = ff_get_audio_buffer(outlink, in->nb_samples);
if (!out) {
av_frame_free(&in);
return AVERROR(ENOMEM);
}
av_frame_copy_props(out, in);
}
dst = (double *)out->data[0];
gate(s, src, dst, src, in->nb_samples,
s->level_in, s->level_in, inlink, inlink);
if (out != in)
av_frame_free(&in);
return ff_filter_frame(outlink, out);
}
static const AVFilterPad inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.filter_frame = filter_frame,
.config_props = agate_config_input,
},
};
const AVFilter ff_af_agate = {
.name = "agate",
.description = NULL_IF_CONFIG_SMALL("Audio gate."),
.priv_class = &agate_sidechaingate_class,
.priv_size = sizeof(AudioGateContext),
2021-08-12 13:05:31 +02:00
FILTER_INPUTS(inputs),
FILTER_OUTPUTS(ff_audio_default_filterpad),
FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBL),
.process_command = ff_filter_process_command,
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
};
#endif /* CONFIG_AGATE_FILTER */
#if CONFIG_SIDECHAINGATE_FILTER
static int activate(AVFilterContext *ctx)
{
AudioGateContext *s = ctx->priv;
AVFrame *out = NULL, *in[2] = { NULL };
2017-08-31 16:53:35 +02:00
int ret, i, nb_samples;
double *dst;
2017-08-31 16:53:35 +02:00
FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &in[0])) > 0) {
av_audio_fifo_write(s->fifo[0], (void **)in[0]->extended_data,
in[0]->nb_samples);
av_frame_free(&in[0]);
}
if (ret < 0)
return ret;
if ((ret = ff_inlink_consume_frame(ctx->inputs[1], &in[1])) > 0) {
av_audio_fifo_write(s->fifo[1], (void **)in[1]->extended_data,
in[1]->nb_samples);
av_frame_free(&in[1]);
}
if (ret < 0)
return ret;
nb_samples = FFMIN(av_audio_fifo_size(s->fifo[0]), av_audio_fifo_size(s->fifo[1]));
if (nb_samples) {
out = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
if (!out)
return AVERROR(ENOMEM);
for (i = 0; i < 2; i++) {
in[i] = ff_get_audio_buffer(ctx->inputs[i], nb_samples);
if (!in[i]) {
av_frame_free(&in[0]);
av_frame_free(&in[1]);
av_frame_free(&out);
return AVERROR(ENOMEM);
}
av_audio_fifo_read(s->fifo[i], (void **)in[i]->data, nb_samples);
}
dst = (double *)out->data[0];
out->pts = s->pts;
s->pts += av_rescale_q(nb_samples, (AVRational){1, ctx->outputs[0]->sample_rate}, ctx->outputs[0]->time_base);
gate(s, (double *)in[0]->data[0], dst,
(double *)in[1]->data[0], nb_samples,
s->level_in, s->level_sc,
ctx->inputs[0], ctx->inputs[1]);
av_frame_free(&in[0]);
av_frame_free(&in[1]);
ret = ff_filter_frame(ctx->outputs[0], out);
if (ret < 0)
return ret;
}
2017-08-31 16:53:35 +02:00
FF_FILTER_FORWARD_STATUS(ctx->inputs[0], ctx->outputs[0]);
FF_FILTER_FORWARD_STATUS(ctx->inputs[1], ctx->outputs[0]);
if (ff_outlink_frame_wanted(ctx->outputs[0])) {
if (!av_audio_fifo_size(s->fifo[0]))
ff_inlink_request_frame(ctx->inputs[0]);
if (!av_audio_fifo_size(s->fifo[1]))
ff_inlink_request_frame(ctx->inputs[1]);
}
return 0;
}
static int scquery_formats(AVFilterContext *ctx)
{
static const enum AVSampleFormat sample_fmts[] = {
AV_SAMPLE_FMT_DBL,
AV_SAMPLE_FMT_NONE
};
avfilter/af_agate: Honour query_formats API, fix segfault The sidechaingate filter wants its main input and its (only) output to have the same channel layout and number of channels; yet it does not link them in its query_formats callback. Instead it sets the outlink to only accept the first offered choice for the main input's channel layout and then sets both inputs to independently accept any channel counts. The config_output callback then overwrote the outlink's channel layout and channels properties with the main input's, even though they may differ in case the first offered choice for the main input's channel layout turns out not to be the final one. Consider e.g. the following filtergraph: [in]aformat=channel_layouts=mono,aformat=channel_layouts=stereo|mono[out];\ [out][in2]sidechaingate,stereotools The two aformats ensure that the first offered channel layout (stereo) will not be chosen for the input; yet it is the only offered channel layout for the output of sidechaingate and will therefore be chosen by the query_formats framework. Because the sidechaingate outputs interleaved doubles which stereotools expects the output of sidechaingate appears to be suitable as input for stereotools without further conversions. Yet stereotools actually only receives a mono frame and therefore overreads its input buffer which leads to segfaults; it can also lead to heap corruption because there can be writes beyond the end of the buffer, too. Fix this by linking the channel layouts of the main input and the output in query_formats and remove the code overwriting it in config_output. Reviewed-by: Paul B Mahol <onemda@gmail.com> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-21 05:18:19 +02:00
int ret = ff_channel_layouts_ref(ff_all_channel_counts(),
&ctx->inputs[1]->outcfg.channel_layouts);
if (ret < 0)
return ret;
avfilter/af_agate: Honour query_formats API, fix segfault The sidechaingate filter wants its main input and its (only) output to have the same channel layout and number of channels; yet it does not link them in its query_formats callback. Instead it sets the outlink to only accept the first offered choice for the main input's channel layout and then sets both inputs to independently accept any channel counts. The config_output callback then overwrote the outlink's channel layout and channels properties with the main input's, even though they may differ in case the first offered choice for the main input's channel layout turns out not to be the final one. Consider e.g. the following filtergraph: [in]aformat=channel_layouts=mono,aformat=channel_layouts=stereo|mono[out];\ [out][in2]sidechaingate,stereotools The two aformats ensure that the first offered channel layout (stereo) will not be chosen for the input; yet it is the only offered channel layout for the output of sidechaingate and will therefore be chosen by the query_formats framework. Because the sidechaingate outputs interleaved doubles which stereotools expects the output of sidechaingate appears to be suitable as input for stereotools without further conversions. Yet stereotools actually only receives a mono frame and therefore overreads its input buffer which leads to segfaults; it can also lead to heap corruption because there can be writes beyond the end of the buffer, too. Fix this by linking the channel layouts of the main input and the output in query_formats and remove the code overwriting it in config_output. Reviewed-by: Paul B Mahol <onemda@gmail.com> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-21 05:18:19 +02:00
/* This will link the channel properties of the main input and the output;
* it won't touch the second input as its channel_layouts is already set. */
if ((ret = ff_set_common_all_channel_counts(ctx)) < 0)
return ret;
if ((ret = ff_set_common_formats_from_list(ctx, sample_fmts)) < 0)
return ret;
return ff_set_common_all_samplerates(ctx);
}
static int scconfig_output(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
AudioGateContext *s = ctx->priv;
outlink->time_base = ctx->inputs[0]->time_base;
s->fifo[0] = av_audio_fifo_alloc(ctx->inputs[0]->format, ctx->inputs[0]->ch_layout.nb_channels, 1024);
s->fifo[1] = av_audio_fifo_alloc(ctx->inputs[1]->format, ctx->inputs[1]->ch_layout.nb_channels, 1024);
if (!s->fifo[0] || !s->fifo[1])
return AVERROR(ENOMEM);
agate_config_input(ctx->inputs[0]);
return 0;
}
static av_cold void uninit(AVFilterContext *ctx)
{
AudioGateContext *s = ctx->priv;
av_audio_fifo_free(s->fifo[0]);
av_audio_fifo_free(s->fifo[1]);
}
static const AVFilterPad sidechaingate_inputs[] = {
{
.name = "main",
.type = AVMEDIA_TYPE_AUDIO,
},{
.name = "sidechain",
.type = AVMEDIA_TYPE_AUDIO,
},
};
static const AVFilterPad sidechaingate_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.config_props = scconfig_output,
},
};
const AVFilter ff_af_sidechaingate = {
.name = "sidechaingate",
.description = NULL_IF_CONFIG_SMALL("Audio sidechain gate."),
.priv_class = &agate_sidechaingate_class,
.priv_size = sizeof(AudioGateContext),
.activate = activate,
.uninit = uninit,
2021-08-12 13:05:31 +02:00
FILTER_INPUTS(sidechaingate_inputs),
FILTER_OUTPUTS(sidechaingate_outputs),
avfilter: Replace query_formats callback with union of list and callback If one looks at the many query_formats callbacks in existence, one will immediately recognize that there is one type of default callback for video and a slightly different default callback for audio: It is "return ff_set_common_formats_from_list(ctx, pix_fmts);" for video with a filter-specific pix_fmts list. For audio, it is the same with a filter-specific sample_fmts list together with ff_set_common_all_samplerates() and ff_set_common_all_channel_counts(). This commit allows to remove the boilerplate query_formats callbacks by replacing said callback with a union consisting the old callback and pointers for pixel and sample format arrays. For the not uncommon case in which these lists only contain a single entry (besides the sentinel) enum AVPixelFormat and enum AVSampleFormat fields are also added to the union to store them directly in the AVFilter, thereby avoiding a relocation. The state of said union will be contained in a new, dedicated AVFilter field (the nb_inputs and nb_outputs fields have been shrunk to uint8_t in order to create a hole for this new field; this is no problem, as the maximum of all the nb_inputs is four; for nb_outputs it is only two). The state's default value coincides with the earlier default of query_formats being unset, namely that the filter accepts all formats (and also sample rates and channel counts/layouts for audio) provided that these properties agree coincide for all inputs and outputs. By using different union members for audio and video filters the type-unsafety of using the same functions for audio and video lists will furthermore be more confined to formats.c than before. When the new fields are used, they will also avoid allocations: Currently something nearly equivalent to ff_default_query_formats() is called after every successful call to a query_formats callback; yet in the common case that the newly allocated AVFilterFormats are not used at all (namely if there are no free links) these newly allocated AVFilterFormats are freed again without ever being used. Filters no longer using the callback will not exhibit this any more. Reviewed-by: Paul B Mahol <onemda@gmail.com> Reviewed-by: Nicolas George <george@nsup.org> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-27 12:07:35 +02:00
FILTER_QUERY_FUNC(scquery_formats),
.process_command = ff_filter_process_command,
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
};
#endif /* CONFIG_SIDECHAINGATE_FILTER */