FFmpeg
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/avstring.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/samplefmt.h"
24 #include "avfilter.h"
25 #include "audio.h"
26 #include "internal.h"
27 #include "generate_wave_table.h"
28 
29 #define INTERPOLATION_LINEAR 0
30 #define INTERPOLATION_QUADRATIC 1
31 
32 typedef struct FlangerContext {
33  const AVClass *class;
34  double delay_min;
35  double delay_depth;
36  double feedback_gain;
37  double delay_gain;
38  double speed;
40  double channel_phase;
42  double in_gain;
44  uint8_t **delay_buffer;
46  double *delay_last;
47  float *lfo;
49  int lfo_pos;
51 
52 #define OFFSET(x) offsetof(FlangerContext, x)
53 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
54 
55 static 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, "type" },
62  { "triangular", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, A, "type" },
63  { "t", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, A, "type" },
64  { "sinusoidal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, A, "type" },
65  { "s", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, A, "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, "itype" },
68  { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATION_LINEAR}, 0, 0, A, "itype" },
69  { "quadratic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATION_QUADRATIC}, 0, 0, A, "itype" },
70  { NULL }
71 };
72 
73 AVFILTER_DEFINE_CLASS(flanger);
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 
92 {
95  static const enum AVSampleFormat sample_fmts[] = {
97  };
98  int ret;
99 
101  if (!layouts)
102  return AVERROR(ENOMEM);
104  if (ret < 0)
105  return ret;
106 
108  if (!formats)
109  return AVERROR(ENOMEM);
111  if (ret < 0)
112  return ret;
113 
115  if (!formats)
116  return AVERROR(ENOMEM);
118 }
119 
121 {
122  AVFilterContext *ctx = inlink->dst;
123  FlangerContext *s = ctx->priv;
124 
125  s->max_samples = (s->delay_min + s->delay_depth) * inlink->sample_rate + 2.5;
126  s->lfo_length = inlink->sample_rate / s->speed;
127  s->delay_last = av_calloc(inlink->channels, sizeof(*s->delay_last));
128  s->lfo = av_calloc(s->lfo_length, sizeof(*s->lfo));
129  if (!s->lfo || !s->delay_last)
130  return AVERROR(ENOMEM);
131 
132  ff_generate_wave_table(s->wave_shape, AV_SAMPLE_FMT_FLT, s->lfo, s->lfo_length,
133  rint(s->delay_min * inlink->sample_rate),
134  s->max_samples - 2., 3 * M_PI_2);
135 
136  return av_samples_alloc_array_and_samples(&s->delay_buffer, NULL,
137  inlink->channels, s->max_samples,
138  inlink->format, 0);
139 }
140 
142 {
143  AVFilterContext *ctx = inlink->dst;
144  FlangerContext *s = ctx->priv;
145  AVFrame *out_frame;
146  int chan, i;
147 
149  out_frame = frame;
150  } else {
151  out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
152  if (!out_frame) {
154  return AVERROR(ENOMEM);
155  }
156  av_frame_copy_props(out_frame, frame);
157  }
158 
159  for (i = 0; i < frame->nb_samples; i++) {
160 
161  s->delay_buf_pos = (s->delay_buf_pos + s->max_samples - 1) % s->max_samples;
162 
163  for (chan = 0; chan < inlink->channels; chan++) {
164  double *src = (double *)frame->extended_data[chan];
165  double *dst = (double *)out_frame->extended_data[chan];
166  double delayed_0, delayed_1;
167  double delayed;
168  double in, out;
169  int channel_phase = chan * s->lfo_length * s->channel_phase + .5;
170  double delay = s->lfo[(s->lfo_pos + channel_phase) % s->lfo_length];
171  int int_delay = (int)delay;
172  double frac_delay = modf(delay, &delay);
173  double *delay_buffer = (double *)s->delay_buffer[chan];
174 
175  in = src[i];
176  delay_buffer[s->delay_buf_pos] = in + s->delay_last[chan] *
177  s->feedback_gain;
178  delayed_0 = delay_buffer[(s->delay_buf_pos + int_delay++) % s->max_samples];
179  delayed_1 = delay_buffer[(s->delay_buf_pos + int_delay++) % s->max_samples];
180 
181  if (s->interpolation == INTERPOLATION_LINEAR) {
182  delayed = delayed_0 + (delayed_1 - delayed_0) * frac_delay;
183  } else {
184  double a, b;
185  double delayed_2 = delay_buffer[(s->delay_buf_pos + int_delay++) % s->max_samples];
186  delayed_2 -= delayed_0;
187  delayed_1 -= delayed_0;
188  a = delayed_2 * .5 - delayed_1;
189  b = delayed_1 * 2 - delayed_2 *.5;
190  delayed = delayed_0 + (a * frac_delay + b) * frac_delay;
191  }
192 
193  s->delay_last[chan] = delayed;
194  out = in * s->in_gain + delayed * s->delay_gain;
195  dst[i] = out;
196  }
197  s->lfo_pos = (s->lfo_pos + 1) % s->lfo_length;
198  }
199 
200  if (frame != out_frame)
202 
203  return ff_filter_frame(ctx->outputs[0], out_frame);
204 }
205 
207 {
208  FlangerContext *s = ctx->priv;
209 
210  av_freep(&s->lfo);
211  av_freep(&s->delay_last);
212 
213  if (s->delay_buffer)
214  av_freep(&s->delay_buffer[0]);
215  av_freep(&s->delay_buffer);
216 }
217 
218 static const AVFilterPad flanger_inputs[] = {
219  {
220  .name = "default",
221  .type = AVMEDIA_TYPE_AUDIO,
222  .config_props = config_input,
223  .filter_frame = filter_frame,
224  },
225  { NULL }
226 };
227 
228 static const AVFilterPad flanger_outputs[] = {
229  {
230  .name = "default",
231  .type = AVMEDIA_TYPE_AUDIO,
232  },
233  { NULL }
234 };
235 
237  .name = "flanger",
238  .description = NULL_IF_CONFIG_SMALL("Apply a flanging effect to the audio."),
239  .query_formats = query_formats,
240  .priv_size = sizeof(FlangerContext),
241  .priv_class = &flanger_class,
242  .init = init,
243  .uninit = uninit,
246 };
formats
formats
Definition: signature.h:48
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:86
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_flanger.c:120
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:953
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_flanger.c:91
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_flanger.c:206
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:111
ff_all_channel_counts
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:429
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
M_PI_2
#define M_PI_2
Definition: mathematics.h:55
AVOption
AVOption.
Definition: opt.h:248
b
#define b
Definition: input.c:41
FlangerContext::interpolation
int interpolation
Definition: af_flanger.c:41
FlangerContext::max_samples
int max_samples
Definition: af_flanger.c:43
FlangerContext::delay_gain
double delay_gain
Definition: af_flanger.c:37
flanger_options
static const AVOption flanger_options[]
Definition: af_flanger.c:55
INTERPOLATION_QUADRATIC
#define INTERPOLATION_QUADRATIC
Definition: af_flanger.c:30
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
OFFSET
#define OFFSET(x)
Definition: af_flanger.c:52
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:65
FlangerContext::lfo_length
int lfo_length
Definition: af_flanger.c:48
WAVE_TRI
@ WAVE_TRI
Definition: generate_wave_table.h:26
samplefmt.h
FlangerContext::channel_phase
double channel_phase
Definition: af_flanger.c:40
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
A
#define A
Definition: af_flanger.c:53
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_flanger.c:75
av_cold
#define av_cold
Definition: attributes.h:90
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:580
FlangerContext::lfo
float * lfo
Definition: af_flanger.c:47
FlangerContext::in_gain
double in_gain
Definition: af_flanger.c:42
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
ctx
AVFormatContext * ctx
Definition: movenc.c:48
WAVE_SIN
@ WAVE_SIN
Definition: generate_wave_table.h:25
if
if(ret)
Definition: filter_design.txt:179
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
rint
#define rint
Definition: tablegen.h:41
NULL
#define NULL
Definition: coverity.c:32
FlangerContext::speed
double speed
Definition: af_flanger.c:38
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:536
WAVE_NB
@ WAVE_NB
Definition: generate_wave_table.h:27
FlangerContext::feedback_gain
double feedback_gain
Definition: af_flanger.c:36
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_flanger.c:141
src
#define src
Definition: vp8dsp.c:255
flanger_outputs
static const AVFilterPad flanger_outputs[]
Definition: af_flanger.c:228
FlangerContext
Definition: af_flanger.c:32
ff_generate_wave_table
void ff_generate_wave_table(enum WaveType wave_type, enum AVSampleFormat sample_fmt, void *table, int table_size, double min, double max, double phase)
Definition: generate_wave_table.c:24
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
ff_af_flanger
const AVFilter ff_af_flanger
Definition: af_flanger.c:236
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
FlangerContext::delay_depth
double delay_depth
Definition: af_flanger.c:35
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:472
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
interpolation
static int interpolation(DeclickChannel *c, const double *src, int ar_order, double *acoefficients, int *index, int nb_errors, double *auxiliary, double *interpolated)
Definition: af_adeclick.c:365
internal.h
i
int i
Definition: input.c:407
FlangerContext::delay_buffer
uint8_t ** delay_buffer
Definition: af_flanger.c:44
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:350
FlangerContext::delay_buf_pos
int delay_buf_pos
Definition: af_flanger.c:45
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(flanger)
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
FlangerContext::wave_shape
int wave_shape
Definition: af_flanger.c:39
AVFilter
Filter definition.
Definition: avfilter.h:145
ret
ret
Definition: filter_design.txt:187
INTERPOLATION_LINEAR
#define INTERPOLATION_LINEAR
Definition: af_flanger.c:29
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
flanger_inputs
static const AVFilterPad flanger_inputs[]
Definition: af_flanger.c:218
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:414
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:252
generate_wave_table.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:70
FlangerContext::delay_last
double * delay_last
Definition: af_flanger.c:46
AVFilterContext
An instance of a filter.
Definition: avfilter.h:333
audio.h
FlangerContext::lfo_pos
int lfo_pos
Definition: af_flanger.c:49
av_samples_alloc_array_and_samples
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:205
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
FlangerContext::delay_min
double delay_min
Definition: af_flanger.c:34
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:568
avstring.h
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates.
Definition: formats.c:561