mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-02-14 22:22:59 +02:00
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>
232 lines
8.2 KiB
C
232 lines
8.2 KiB
C
/*
|
|
* Copyright (c) 2006 Rob Sykes <robs@users.sourceforge.net>
|
|
*
|
|
* 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
|
|
*/
|
|
|
|
#include "libavutil/avstring.h"
|
|
#include "libavutil/opt.h"
|
|
#include "libavutil/samplefmt.h"
|
|
#include "avfilter.h"
|
|
#include "audio.h"
|
|
#include "internal.h"
|
|
#include "generate_wave_table.h"
|
|
|
|
#define INTERPOLATION_LINEAR 0
|
|
#define INTERPOLATION_QUADRATIC 1
|
|
|
|
typedef struct FlangerContext {
|
|
const AVClass *class;
|
|
double delay_min;
|
|
double delay_depth;
|
|
double feedback_gain;
|
|
double delay_gain;
|
|
double speed;
|
|
int wave_shape;
|
|
double channel_phase;
|
|
int interpolation;
|
|
double in_gain;
|
|
int max_samples;
|
|
uint8_t **delay_buffer;
|
|
int delay_buf_pos;
|
|
double *delay_last;
|
|
float *lfo;
|
|
int lfo_length;
|
|
int lfo_pos;
|
|
} FlangerContext;
|
|
|
|
#define OFFSET(x) offsetof(FlangerContext, x)
|
|
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
static const AVOption flanger_options[] = {
|
|
{ "delay", "base delay in milliseconds", OFFSET(delay_min), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 30, A },
|
|
{ "depth", "added swept delay in milliseconds", OFFSET(delay_depth), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 10, A },
|
|
{ "regen", "percentage regeneration (delayed signal feedback)", OFFSET(feedback_gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -95, 95, A },
|
|
{ "width", "percentage of delayed signal mixed with original", OFFSET(delay_gain), AV_OPT_TYPE_DOUBLE, {.dbl=71}, 0, 100, A },
|
|
{ "speed", "sweeps per second (Hz)", OFFSET(speed), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.1, 10, A },
|
|
{ "shape", "swept wave shape", OFFSET(wave_shape), AV_OPT_TYPE_INT, {.i64=WAVE_SIN}, WAVE_SIN, WAVE_NB-1, A, "type" },
|
|
{ "triangular", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, A, "type" },
|
|
{ "t", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, A, "type" },
|
|
{ "sinusoidal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, A, "type" },
|
|
{ "s", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, A, "type" },
|
|
{ "phase", "swept wave percentage phase-shift for multi-channel", OFFSET(channel_phase), AV_OPT_TYPE_DOUBLE, {.dbl=25}, 0, 100, A },
|
|
{ "interp", "delay-line interpolation", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, "itype" },
|
|
{ "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATION_LINEAR}, 0, 0, A, "itype" },
|
|
{ "quadratic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATION_QUADRATIC}, 0, 0, A, "itype" },
|
|
{ NULL }
|
|
};
|
|
|
|
AVFILTER_DEFINE_CLASS(flanger);
|
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
{
|
|
FlangerContext *s = ctx->priv;
|
|
|
|
s->feedback_gain /= 100;
|
|
s->delay_gain /= 100;
|
|
s->channel_phase /= 100;
|
|
s->delay_min /= 1000;
|
|
s->delay_depth /= 1000;
|
|
s->in_gain = 1 / (1 + s->delay_gain);
|
|
s->delay_gain /= 1 + s->delay_gain;
|
|
s->delay_gain *= 1 - fabs(s->feedback_gain);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
{
|
|
static const enum AVSampleFormat sample_fmts[] = {
|
|
AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE
|
|
};
|
|
int ret = ff_set_common_all_channel_counts(ctx);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
ret = ff_set_common_formats_from_list(ctx, sample_fmts);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
return ff_set_common_all_samplerates(ctx);
|
|
}
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
{
|
|
AVFilterContext *ctx = inlink->dst;
|
|
FlangerContext *s = ctx->priv;
|
|
|
|
s->max_samples = (s->delay_min + s->delay_depth) * inlink->sample_rate + 2.5;
|
|
s->lfo_length = inlink->sample_rate / s->speed;
|
|
s->delay_last = av_calloc(inlink->channels, sizeof(*s->delay_last));
|
|
s->lfo = av_calloc(s->lfo_length, sizeof(*s->lfo));
|
|
if (!s->lfo || !s->delay_last)
|
|
return AVERROR(ENOMEM);
|
|
|
|
ff_generate_wave_table(s->wave_shape, AV_SAMPLE_FMT_FLT, s->lfo, s->lfo_length,
|
|
rint(s->delay_min * inlink->sample_rate),
|
|
s->max_samples - 2., 3 * M_PI_2);
|
|
|
|
return av_samples_alloc_array_and_samples(&s->delay_buffer, NULL,
|
|
inlink->channels, s->max_samples,
|
|
inlink->format, 0);
|
|
}
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
|
{
|
|
AVFilterContext *ctx = inlink->dst;
|
|
FlangerContext *s = ctx->priv;
|
|
AVFrame *out_frame;
|
|
int chan, i;
|
|
|
|
if (av_frame_is_writable(frame)) {
|
|
out_frame = frame;
|
|
} else {
|
|
out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
|
|
if (!out_frame) {
|
|
av_frame_free(&frame);
|
|
return AVERROR(ENOMEM);
|
|
}
|
|
av_frame_copy_props(out_frame, frame);
|
|
}
|
|
|
|
for (i = 0; i < frame->nb_samples; i++) {
|
|
|
|
s->delay_buf_pos = (s->delay_buf_pos + s->max_samples - 1) % s->max_samples;
|
|
|
|
for (chan = 0; chan < inlink->channels; chan++) {
|
|
double *src = (double *)frame->extended_data[chan];
|
|
double *dst = (double *)out_frame->extended_data[chan];
|
|
double delayed_0, delayed_1;
|
|
double delayed;
|
|
double in, out;
|
|
int channel_phase = chan * s->lfo_length * s->channel_phase + .5;
|
|
double delay = s->lfo[(s->lfo_pos + channel_phase) % s->lfo_length];
|
|
int int_delay = (int)delay;
|
|
double frac_delay = modf(delay, &delay);
|
|
double *delay_buffer = (double *)s->delay_buffer[chan];
|
|
|
|
in = src[i];
|
|
delay_buffer[s->delay_buf_pos] = in + s->delay_last[chan] *
|
|
s->feedback_gain;
|
|
delayed_0 = delay_buffer[(s->delay_buf_pos + int_delay++) % s->max_samples];
|
|
delayed_1 = delay_buffer[(s->delay_buf_pos + int_delay++) % s->max_samples];
|
|
|
|
if (s->interpolation == INTERPOLATION_LINEAR) {
|
|
delayed = delayed_0 + (delayed_1 - delayed_0) * frac_delay;
|
|
} else {
|
|
double a, b;
|
|
double delayed_2 = delay_buffer[(s->delay_buf_pos + int_delay++) % s->max_samples];
|
|
delayed_2 -= delayed_0;
|
|
delayed_1 -= delayed_0;
|
|
a = delayed_2 * .5 - delayed_1;
|
|
b = delayed_1 * 2 - delayed_2 *.5;
|
|
delayed = delayed_0 + (a * frac_delay + b) * frac_delay;
|
|
}
|
|
|
|
s->delay_last[chan] = delayed;
|
|
out = in * s->in_gain + delayed * s->delay_gain;
|
|
dst[i] = out;
|
|
}
|
|
s->lfo_pos = (s->lfo_pos + 1) % s->lfo_length;
|
|
}
|
|
|
|
if (frame != out_frame)
|
|
av_frame_free(&frame);
|
|
|
|
return ff_filter_frame(ctx->outputs[0], out_frame);
|
|
}
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
{
|
|
FlangerContext *s = ctx->priv;
|
|
|
|
av_freep(&s->lfo);
|
|
av_freep(&s->delay_last);
|
|
|
|
if (s->delay_buffer)
|
|
av_freep(&s->delay_buffer[0]);
|
|
av_freep(&s->delay_buffer);
|
|
}
|
|
|
|
static const AVFilterPad flanger_inputs[] = {
|
|
{
|
|
.name = "default",
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
.config_props = config_input,
|
|
.filter_frame = filter_frame,
|
|
},
|
|
};
|
|
|
|
static const AVFilterPad flanger_outputs[] = {
|
|
{
|
|
.name = "default",
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
},
|
|
};
|
|
|
|
const AVFilter ff_af_flanger = {
|
|
.name = "flanger",
|
|
.description = NULL_IF_CONFIG_SMALL("Apply a flanging effect to the audio."),
|
|
.priv_size = sizeof(FlangerContext),
|
|
.priv_class = &flanger_class,
|
|
.init = init,
|
|
.uninit = uninit,
|
|
FILTER_INPUTS(flanger_inputs),
|
|
FILTER_OUTPUTS(flanger_outputs),
|
|
FILTER_QUERY_FUNC(query_formats),
|
|
};
|