FFmpeg
Loading...
Searching...
No Matches
af_flanger.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006 Rob Sykes <robs@users.sourceforge.net>
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#include "libavutil/mem.h"
22#include "libavutil/opt.h"
23#include "libavutil/samplefmt.h"
24#include "avfilter.h"
25#include "audio.h"
26#include "filters.h"
27#include "generate_wave_table.h"
28
29#define INTERPOLATION_LINEAR 0
30#define INTERPOLATION_QUADRATIC 1
31
32typedef struct FlangerContext {
33 const AVClass *class;
34 double delay_min;
37 double delay_gain;
38 double speed;
42 double in_gain;
44 uint8_t **delay_buffer;
46 double *delay_last;
47 float *lfo;
51
52#define OFFSET(x) offsetof(FlangerContext, x)
53#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
54
55static const AVOption flanger_options[] = {
56 { "delay", "base delay in milliseconds", OFFSET(delay_min), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 30, A },
57 { "depth", "added swept delay in milliseconds", OFFSET(delay_depth), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 10, A },
58 { "regen", "percentage regeneration (delayed signal feedback)", OFFSET(feedback_gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -95, 95, A },
59 { "width", "percentage of delayed signal mixed with original", OFFSET(delay_gain), AV_OPT_TYPE_DOUBLE, {.dbl=71}, 0, 100, A },
60 { "speed", "sweeps per second (Hz)", OFFSET(speed), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.1, 10, A },
61 { "shape", "swept wave shape", OFFSET(wave_shape), AV_OPT_TYPE_INT, {.i64=WAVE_SIN}, WAVE_SIN, WAVE_NB-1, A, .unit = "type" },
62 { "triangular", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, A, .unit = "type" },
63 { "t", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, A, .unit = "type" },
64 { "sinusoidal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, A, .unit = "type" },
65 { "s", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, A, .unit = "type" },
66 { "phase", "swept wave percentage phase-shift for multi-channel", OFFSET(channel_phase), AV_OPT_TYPE_DOUBLE, {.dbl=25}, 0, 100, A },
67 { "interp", "delay-line interpolation", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, .unit = "itype" },
68 { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATION_LINEAR}, 0, 0, A, .unit = "itype" },
69 { "quadratic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATION_QUADRATIC}, 0, 0, A, .unit = "itype" },
70 { NULL }
71};
72
74
76{
77 FlangerContext *s = ctx->priv;
78
79 s->feedback_gain /= 100;
80 s->delay_gain /= 100;
81 s->channel_phase /= 100;
82 s->delay_min /= 1000;
83 s->delay_depth /= 1000;
84 s->in_gain = 1 / (1 + s->delay_gain);
85 s->delay_gain /= 1 + s->delay_gain;
86 s->delay_gain *= 1 - fabs(s->feedback_gain);
87
88 return 0;
89}
90
91static int config_input(AVFilterLink *inlink)
92{
93 AVFilterContext *ctx = inlink->dst;
94 FlangerContext *s = ctx->priv;
95
96 s->max_samples = (s->delay_min + s->delay_depth) * inlink->sample_rate + 2.5;
97 s->lfo_length = inlink->sample_rate / s->speed;
98 s->delay_last = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->delay_last));
99 s->lfo = av_calloc(s->lfo_length, sizeof(*s->lfo));
100 if (!s->lfo || !s->delay_last)
101 return AVERROR(ENOMEM);
102
103 ff_generate_wave_table(s->wave_shape, AV_SAMPLE_FMT_FLT, s->lfo, s->lfo_length,
104 rint(s->delay_min * inlink->sample_rate),
105 s->max_samples - 2., 3 * M_PI_2);
106
107 return av_samples_alloc_array_and_samples(&s->delay_buffer, NULL,
108 inlink->ch_layout.nb_channels, s->max_samples,
109 inlink->format, 0);
110}
111
113{
114 AVFilterContext *ctx = inlink->dst;
115 FlangerContext *s = ctx->priv;
116 AVFrame *out_frame;
117 int chan, i;
118
120 out_frame = frame;
121 } else {
122 out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
123 if (!out_frame) {
125 return AVERROR(ENOMEM);
126 }
127 av_frame_copy_props(out_frame, frame);
128 }
129
130 for (i = 0; i < frame->nb_samples; i++) {
131
132 s->delay_buf_pos = (s->delay_buf_pos + s->max_samples - 1) % s->max_samples;
133
134 for (chan = 0; chan < inlink->ch_layout.nb_channels; chan++) {
135 double *src = (double *)frame->extended_data[chan];
136 double *dst = (double *)out_frame->extended_data[chan];
137 double delayed_0, delayed_1;
138 double delayed;
139 double in, out;
140 int channel_phase = chan * s->lfo_length * s->channel_phase + .5;
141 double delay = s->lfo[(s->lfo_pos + channel_phase) % s->lfo_length];
142 int int_delay = (int)delay;
143 double frac_delay = modf(delay, &delay);
144 double *delay_buffer = (double *)s->delay_buffer[chan];
145
146 in = src[i];
147 delay_buffer[s->delay_buf_pos] = in + s->delay_last[chan] *
148 s->feedback_gain;
149 delayed_0 = delay_buffer[(s->delay_buf_pos + int_delay++) % s->max_samples];
150 delayed_1 = delay_buffer[(s->delay_buf_pos + int_delay++) % s->max_samples];
151
152 if (s->interpolation == INTERPOLATION_LINEAR) {
153 delayed = delayed_0 + (delayed_1 - delayed_0) * frac_delay;
154 } else {
155 double a, b;
156 double delayed_2 = delay_buffer[(s->delay_buf_pos + int_delay++) % s->max_samples];
157 delayed_2 -= delayed_0;
158 delayed_1 -= delayed_0;
159 a = delayed_2 * .5 - delayed_1;
160 b = delayed_1 * 2 - delayed_2 *.5;
161 delayed = delayed_0 + (a * frac_delay + b) * frac_delay;
162 }
163
164 s->delay_last[chan] = delayed;
165 out = in * s->in_gain + delayed * s->delay_gain;
166 dst[i] = out;
167 }
168 s->lfo_pos = (s->lfo_pos + 1) % s->lfo_length;
169 }
170
171 if (frame != out_frame)
173
174 return ff_filter_frame(ctx->outputs[0], out_frame);
175}
176
178{
179 FlangerContext *s = ctx->priv;
180
181 av_freep(&s->lfo);
182 av_freep(&s->delay_last);
183
184 if (s->delay_buffer)
185 av_freep(&s->delay_buffer[0]);
186 av_freep(&s->delay_buffer);
187}
188
189static const AVFilterPad flanger_inputs[] = {
190 {
191 .name = "default",
192 .type = AVMEDIA_TYPE_AUDIO,
193 .config_props = config_input,
194 .filter_frame = filter_frame,
195 },
196};
197
199 .p.name = "flanger",
200 .p.description = NULL_IF_CONFIG_SMALL("Apply a flanging effect to the audio."),
201 .p.priv_class = &flanger_class,
202 .priv_size = sizeof(FlangerContext),
203 .init = init,
204 .uninit = uninit,
208};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int config_input(AVFilterLink *inlink)
static int interpolation(DeclickChannel *c, const double *src, int ar_order, double *acoefficients, int *index, int nb_errors, double *auxiliary, double *interpolated)
static const AVOption flanger_options[]
Definition af_flanger.c:55
#define INTERPOLATION_LINEAR
Definition af_flanger.c:29
#define INTERPOLATION_QUADRATIC
Definition af_flanger.c:30
static int config_input(AVFilterLink *inlink)
Definition af_flanger.c:91
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition af_flanger.c:112
static const AVFilterPad flanger_inputs[]
Definition af_flanger.c:189
static av_cold void uninit(AVFilterContext *ctx)
Definition af_flanger.c:177
#define OFFSET(x)
Definition af_flanger.c:52
const FFFilter ff_af_flanger
Definition af_flanger.c:198
#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
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
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
#define NULL
Definition coverity.c:32
static __device__ float fabs(float a)
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
void ff_generate_wave_table(enum WaveType wave_type, enum AVSampleFormat sample_fmt, void *table, int table_size, double min, double max, double phase)
@ 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 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
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
int av_samples_alloc_array_and_samples(uint8_t ***audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Allocate a data pointers array, samples buffer for nb_samples samples, and fill data pointers and lin...
Definition samplefmt.c:207
int a
#define b
Definition input.c:43
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition filters.h:257
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#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 M_PI_2
Definition mathematics.h:73
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
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
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 ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
AVOption.
Definition opt.h:428
double delay_min
Definition af_flanger.c:34
uint8_t ** delay_buffer
Definition af_flanger.c:44
double delay_depth
Definition af_flanger.c:35
double feedback_gain
Definition af_flanger.c:36
double channel_phase
Definition af_flanger.c:40
double * delay_last
Definition af_flanger.c:46
double delay_gain
Definition af_flanger.c:37
#define rint
Definition tablegen.h:41
#define av_freep(p)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49