2 * Copyright (c) 2013 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
26 #include "libavutil/avassert.h"
27 #include "libavutil/opt.h"
31 #include "generate_wave_table.h"
33 typedef struct AudioPhaserContext {
35 double in_gain, out_gain;
42 int delay_buffer_length;
45 int modulation_buffer_length;
46 int32_t *modulation_buffer;
48 int delay_pos, modulation_pos;
50 void (*phaser)(struct AudioPhaserContext *p,
51 uint8_t * const *src, uint8_t **dst,
52 int nb_samples, int channels);
55 #define OFFSET(x) offsetof(AudioPhaserContext, x)
56 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58 static const AVOption aphaser_options[] = {
59 { "in_gain", "set input gain", OFFSET(in_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.4}, 0, 1, FLAGS },
60 { "out_gain", "set output gain", OFFSET(out_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.74}, 0, 1e9, FLAGS },
61 { "delay", "set delay in milliseconds", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=3.}, 0, 5, FLAGS },
62 { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl=.4}, 0, .99, FLAGS },
63 { "speed", "set modulation speed", OFFSET(speed), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, .1, 2, FLAGS },
64 { "type", "set modulation type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=WAVE_TRI}, 0, WAVE_NB-1, FLAGS, "type" },
65 { "triangular", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
66 { "t", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
67 { "sinusoidal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
68 { "s", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
72 AVFILTER_DEFINE_CLASS(aphaser);
74 static av_cold int init(AVFilterContext *ctx)
76 AudioPhaserContext *p = ctx->priv;
78 if (p->in_gain > (1 - p->decay * p->decay))
79 av_log(ctx, AV_LOG_WARNING, "in_gain may cause clipping\n");
80 if (p->in_gain / (1 - p->decay) > 1 / p->out_gain)
81 av_log(ctx, AV_LOG_WARNING, "out_gain may cause clipping\n");
86 static int query_formats(AVFilterContext *ctx)
88 AVFilterFormats *formats;
89 AVFilterChannelLayouts *layouts;
90 static const enum AVSampleFormat sample_fmts[] = {
91 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
92 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
93 AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
94 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
99 layouts = ff_all_channel_layouts();
101 return AVERROR(ENOMEM);
102 ret = ff_set_common_channel_layouts(ctx, layouts);
106 formats = ff_make_format_list(sample_fmts);
108 return AVERROR(ENOMEM);
109 ret = ff_set_common_formats(ctx, formats);
113 formats = ff_all_samplerates();
115 return AVERROR(ENOMEM);
116 return ff_set_common_samplerates(ctx, formats);
119 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
121 #define PHASER_PLANAR(name, type) \
122 static void phaser_## name ##p(AudioPhaserContext *p, \
123 uint8_t * const *src, uint8_t **dst, \
124 int nb_samples, int channels) \
126 int i, c, delay_pos, modulation_pos; \
128 av_assert0(channels > 0); \
129 for (c = 0; c < channels; c++) { \
130 type *s = (type *)src[c]; \
131 type *d = (type *)dst[c]; \
132 double *buffer = p->delay_buffer + \
133 c * p->delay_buffer_length; \
135 delay_pos = p->delay_pos; \
136 modulation_pos = p->modulation_pos; \
138 for (i = 0; i < nb_samples; i++, s++, d++) { \
139 double v = *s * p->in_gain + buffer[ \
140 MOD(delay_pos + p->modulation_buffer[ \
142 p->delay_buffer_length)] * p->decay; \
144 modulation_pos = MOD(modulation_pos + 1, \
145 p->modulation_buffer_length); \
146 delay_pos = MOD(delay_pos + 1, p->delay_buffer_length); \
147 buffer[delay_pos] = v; \
149 *d = v * p->out_gain; \
153 p->delay_pos = delay_pos; \
154 p->modulation_pos = modulation_pos; \
157 #define PHASER(name, type) \
158 static void phaser_## name (AudioPhaserContext *p, \
159 uint8_t * const *src, uint8_t **dst, \
160 int nb_samples, int channels) \
162 int i, c, delay_pos, modulation_pos; \
163 type *s = (type *)src[0]; \
164 type *d = (type *)dst[0]; \
165 double *buffer = p->delay_buffer; \
167 delay_pos = p->delay_pos; \
168 modulation_pos = p->modulation_pos; \
170 for (i = 0; i < nb_samples; i++) { \
171 int pos = MOD(delay_pos + p->modulation_buffer[modulation_pos], \
172 p->delay_buffer_length) * channels; \
175 delay_pos = MOD(delay_pos + 1, p->delay_buffer_length); \
176 npos = delay_pos * channels; \
177 for (c = 0; c < channels; c++, s++, d++) { \
178 double v = *s * p->in_gain + buffer[pos + c] * p->decay; \
180 buffer[npos + c] = v; \
182 *d = v * p->out_gain; \
185 modulation_pos = MOD(modulation_pos + 1, \
186 p->modulation_buffer_length); \
189 p->delay_pos = delay_pos; \
190 p->modulation_pos = modulation_pos; \
193 PHASER_PLANAR(dbl, double)
194 PHASER_PLANAR(flt, float)
195 PHASER_PLANAR(s16, int16_t)
196 PHASER_PLANAR(s32, int32_t)
203 static int config_output(AVFilterLink *outlink)
205 AudioPhaserContext *p = outlink->src->priv;
206 AVFilterLink *inlink = outlink->src->inputs[0];
208 p->delay_buffer_length = p->delay * 0.001 * inlink->sample_rate + 0.5;
209 p->delay_buffer = av_calloc(p->delay_buffer_length, sizeof(*p->delay_buffer) * inlink->channels);
210 p->modulation_buffer_length = inlink->sample_rate / p->speed + 0.5;
211 p->modulation_buffer = av_malloc_array(p->modulation_buffer_length, sizeof(*p->modulation_buffer));
213 if (!p->modulation_buffer || !p->delay_buffer)
214 return AVERROR(ENOMEM);
216 ff_generate_wave_table(p->type, AV_SAMPLE_FMT_S32,
217 p->modulation_buffer, p->modulation_buffer_length,
218 1., p->delay_buffer_length, M_PI / 2.0);
220 p->delay_pos = p->modulation_pos = 0;
222 switch (inlink->format) {
223 case AV_SAMPLE_FMT_DBL: p->phaser = phaser_dbl; break;
224 case AV_SAMPLE_FMT_DBLP: p->phaser = phaser_dblp; break;
225 case AV_SAMPLE_FMT_FLT: p->phaser = phaser_flt; break;
226 case AV_SAMPLE_FMT_FLTP: p->phaser = phaser_fltp; break;
227 case AV_SAMPLE_FMT_S16: p->phaser = phaser_s16; break;
228 case AV_SAMPLE_FMT_S16P: p->phaser = phaser_s16p; break;
229 case AV_SAMPLE_FMT_S32: p->phaser = phaser_s32; break;
230 case AV_SAMPLE_FMT_S32P: p->phaser = phaser_s32p; break;
231 default: av_assert0(0);
237 static int filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
239 AudioPhaserContext *p = inlink->dst->priv;
240 AVFilterLink *outlink = inlink->dst->outputs[0];
243 if (av_frame_is_writable(inbuf)) {
246 outbuf = ff_get_audio_buffer(inlink, inbuf->nb_samples);
248 return AVERROR(ENOMEM);
249 av_frame_copy_props(outbuf, inbuf);
252 p->phaser(p, inbuf->extended_data, outbuf->extended_data,
253 outbuf->nb_samples, av_frame_get_channels(outbuf));
256 av_frame_free(&inbuf);
258 return ff_filter_frame(outlink, outbuf);
261 static av_cold void uninit(AVFilterContext *ctx)
263 AudioPhaserContext *p = ctx->priv;
265 av_freep(&p->delay_buffer);
266 av_freep(&p->modulation_buffer);
269 static const AVFilterPad aphaser_inputs[] = {
272 .type = AVMEDIA_TYPE_AUDIO,
273 .filter_frame = filter_frame,
278 static const AVFilterPad aphaser_outputs[] = {
281 .type = AVMEDIA_TYPE_AUDIO,
282 .config_props = config_output,
287 AVFilter ff_af_aphaser = {
289 .description = NULL_IF_CONFIG_SMALL("Add a phasing effect to the audio."),
290 .query_formats = query_formats,
291 .priv_size = sizeof(AudioPhaserContext),
294 .inputs = aphaser_inputs,
295 .outputs = aphaser_outputs,
296 .priv_class = &aphaser_class,