aphaser filter
[ffmpeg.git] / libavfilter / af_aphaser.c
1 /*
2  * Copyright (c) 2013 Paul B Mahol
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 /**
22  * @file
23  * phaser audio filter
24  */
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/opt.h"
28 #include "audio.h"
29 #include "avfilter.h"
30 #include "internal.h"
31
32 enum WaveType {
33     WAVE_SIN,
34     WAVE_TRI,
35     WAVE_NB,
36 };
37
38 typedef struct AudioPhaserContext {
39     const AVClass *class;
40     double in_gain, out_gain;
41     double delay;
42     double decay;
43     double speed;
44
45     enum WaveType type;
46
47     int delay_buffer_length;
48     double *delay_buffer;
49
50     int modulation_buffer_length;
51     int32_t *modulation_buffer;
52
53     int delay_pos, modulation_pos;
54
55     void (*phaser)(struct AudioPhaserContext *p,
56                    uint8_t * const *src, uint8_t **dst,
57                    int nb_samples, int channels);
58 } AudioPhaserContext;
59
60 #define OFFSET(x) offsetof(AudioPhaserContext, x)
61 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
62
63 static const AVOption aphaser_options[] = {
64     { "in_gain",  "set input gain",            OFFSET(in_gain),  AV_OPT_TYPE_DOUBLE, {.dbl=.4},  0,  1,   FLAGS },
65     { "out_gain", "set output gain",           OFFSET(out_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.74}, 0,  1e9, FLAGS },
66     { "delay",    "set delay in milliseconds", OFFSET(delay),    AV_OPT_TYPE_DOUBLE, {.dbl=3.},  0,  5,   FLAGS },
67     { "decay",    "set decay",                 OFFSET(decay),    AV_OPT_TYPE_DOUBLE, {.dbl=.4},  0, .99,  FLAGS },
68     { "speed",    "set modulation speed",      OFFSET(speed),    AV_OPT_TYPE_DOUBLE, {.dbl=.5}, .1,  2,   FLAGS },
69     { "type",     "set modulation type",       OFFSET(type),     AV_OPT_TYPE_INT,    {.i64=WAVE_TRI}, 0, WAVE_NB-1, FLAGS, "type" },
70     { "triangular",  NULL, 0, AV_OPT_TYPE_CONST,  {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
71     { "t",           NULL, 0, AV_OPT_TYPE_CONST,  {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
72     { "sinusoidal",  NULL, 0, AV_OPT_TYPE_CONST,  {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
73     { "s",           NULL, 0, AV_OPT_TYPE_CONST,  {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
74     { NULL },
75 };
76
77 AVFILTER_DEFINE_CLASS(aphaser);
78
79 static av_cold int init(AVFilterContext *ctx, const char *args)
80 {
81     AudioPhaserContext *p = ctx->priv;
82
83     if (p->in_gain > (1 - p->decay * p->decay))
84         av_log(ctx, AV_LOG_WARNING, "in_gain may cause clipping\n");
85     if (p->in_gain / (1 - p->decay) > 1 / p->out_gain)
86         av_log(ctx, AV_LOG_WARNING, "out_gain may cause clipping\n");
87
88     return 0;
89 }
90
91 static int query_formats(AVFilterContext *ctx)
92 {
93     AVFilterFormats *formats;
94     AVFilterChannelLayouts *layouts;
95     static const enum AVSampleFormat sample_fmts[] = {
96         AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
97         AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
98         AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
99         AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
100         AV_SAMPLE_FMT_NONE
101     };
102
103     layouts = ff_all_channel_layouts();
104     if (!layouts)
105         return AVERROR(ENOMEM);
106     ff_set_common_channel_layouts(ctx, layouts);
107
108     formats = ff_make_format_list(sample_fmts);
109     if (!formats)
110         return AVERROR(ENOMEM);
111     ff_set_common_formats(ctx, formats);
112
113     formats = ff_all_samplerates();
114     if (!formats)
115         return AVERROR(ENOMEM);
116     ff_set_common_samplerates(ctx, formats);
117
118     return 0;
119 }
120
121 static void generate_wave_table(enum WaveType wave_type, enum AVSampleFormat sample_fmt,
122                                 void *table, int table_size,
123                                 double min, double max, double phase)
124 {
125     uint32_t i, phase_offset = phase / M_PI / 2 * table_size + 0.5;
126
127     for (i = 0; i < table_size; i++) {
128         uint32_t point = (i + phase_offset) % table_size;
129         double d;
130
131         switch (wave_type) {
132         case WAVE_SIN:
133             d = (sin((double)point / table_size * 2 * M_PI) + 1) / 2;
134             break;
135         case WAVE_TRI:
136             d = (double)point * 2 / table_size;
137             switch (4 * point / table_size) {
138             case 0: d = d + 0.5; break;
139             case 1:
140             case 2: d = 1.5 - d; break;
141             case 3: d = d - 1.5; break;
142             }
143             break;
144         default:
145             av_assert0(0);
146         }
147
148         d  = d * (max - min) + min;
149         switch (sample_fmt) {
150         case AV_SAMPLE_FMT_FLT: {
151             float *fp = (float *)table;
152             *fp++ = (float)d;
153             table = fp;
154             continue; }
155         case AV_SAMPLE_FMT_DBL: {
156             double *dp = (double *)table;
157             *dp++ = d;
158             table = dp;
159             continue; }
160         }
161
162         d += d < 0 ? -0.5 : 0.5;
163         switch (sample_fmt) {
164         case AV_SAMPLE_FMT_S16: {
165             int16_t *sp = table;
166             *sp++ = (int16_t)d;
167             table = sp;
168             continue; }
169         case AV_SAMPLE_FMT_S32: {
170             int32_t *ip = table;
171             *ip++ = (int32_t)d;
172             table = ip;
173             continue; }
174         default:
175             av_assert0(0);
176         }
177     }
178 }
179
180 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
181
182 #define PHASER_PLANAR(name, type)                                      \
183 static void phaser_## name ##p(AudioPhaserContext *p,                  \
184                                uint8_t * const *src, uint8_t **dst,    \
185                                int nb_samples, int channels)           \
186 {                                                                      \
187     int i, c, delay_pos, modulation_pos;                               \
188                                                                        \
189     for (c = 0; c < channels; c++) {                                   \
190         type *s = (type *)src[c];                                      \
191         type *d = (type *)dst[c];                                      \
192         double *buffer = p->delay_buffer +                             \
193                          c * p->delay_buffer_length;                   \
194                                                                        \
195         delay_pos      = p->delay_pos;                                 \
196         modulation_pos = p->modulation_pos;                            \
197                                                                        \
198         for (i = 0; i < nb_samples; i++, s++, d++) {                   \
199             double v = *s * p->in_gain + buffer[                       \
200                        MOD(delay_pos + p->modulation_buffer[           \
201                        modulation_pos],                                \
202                        p->delay_buffer_length)] * p->decay;            \
203                                                                        \
204             modulation_pos = MOD(modulation_pos + 1,                   \
205                              p->modulation_buffer_length);             \
206             delay_pos = MOD(delay_pos + 1, p->delay_buffer_length);    \
207             buffer[delay_pos] = v;                                     \
208                                                                        \
209             *d = v * p->out_gain;                                      \
210         }                                                              \
211     }                                                                  \
212                                                                        \
213     p->delay_pos      = delay_pos;                                     \
214     p->modulation_pos = modulation_pos;                                \
215 }
216
217 #define PHASER(name, type)                                              \
218 static void phaser_## name (AudioPhaserContext *p,                      \
219                             uint8_t * const *src, uint8_t **dst,        \
220                             int nb_samples, int channels)               \
221 {                                                                       \
222     int i, c, delay_pos, modulation_pos;                                \
223     type *s = (type *)src[0];                                           \
224     type *d = (type *)dst[0];                                           \
225     double *buffer = p->delay_buffer;                                   \
226                                                                         \
227     delay_pos      = p->delay_pos;                                      \
228     modulation_pos = p->modulation_pos;                                 \
229                                                                         \
230     for (i = 0; i < nb_samples; i++) {                                  \
231         int pos = MOD(delay_pos + p->modulation_buffer[modulation_pos], \
232                    p->delay_buffer_length) * channels;                  \
233         int npos;                                                       \
234                                                                         \
235         delay_pos = MOD(delay_pos + 1, p->delay_buffer_length);         \
236         npos = delay_pos * channels;                                    \
237         for (c = 0; c < channels; c++, s++, d++) {                      \
238             double v = *s * p->in_gain + buffer[pos + c] * p->decay;    \
239                                                                         \
240             buffer[npos + c] = v;                                       \
241                                                                         \
242             *d = v * p->out_gain;                                       \
243         }                                                               \
244                                                                         \
245         modulation_pos = MOD(modulation_pos + 1,                        \
246                          p->modulation_buffer_length);                  \
247     }                                                                   \
248                                                                         \
249     p->delay_pos      = delay_pos;                                      \
250     p->modulation_pos = modulation_pos;                                 \
251 }
252
253 PHASER_PLANAR(dbl, double)
254 PHASER_PLANAR(flt, float)
255 PHASER_PLANAR(s16, int16_t)
256 PHASER_PLANAR(s32, int32_t)
257
258 PHASER(dbl, double)
259 PHASER(flt, float)
260 PHASER(s16, int16_t)
261 PHASER(s32, int32_t)
262
263 static int config_output(AVFilterLink *outlink)
264 {
265     AudioPhaserContext *p = outlink->src->priv;
266     AVFilterLink *inlink = outlink->src->inputs[0];
267
268     p->delay_buffer_length = p->delay * 0.001 * inlink->sample_rate + 0.5;
269     p->delay_buffer = av_calloc(p->delay_buffer_length, sizeof(*p->delay_buffer) * inlink->channels);
270     p->modulation_buffer_length = inlink->sample_rate / p->speed + 0.5;
271     p->modulation_buffer = av_malloc(p->modulation_buffer_length * sizeof(*p->modulation_buffer));
272
273     if (!p->modulation_buffer || !p->delay_buffer)
274         return AVERROR(ENOMEM);
275
276     generate_wave_table(p->type, AV_SAMPLE_FMT_S32,
277                         p->modulation_buffer, p->modulation_buffer_length,
278                         1., p->delay_buffer_length, M_PI / 2.0);
279
280     p->delay_pos = p->modulation_pos = 0;
281
282     switch (inlink->format) {
283     case AV_SAMPLE_FMT_DBL:  p->phaser = phaser_dbl;  break;
284     case AV_SAMPLE_FMT_DBLP: p->phaser = phaser_dblp; break;
285     case AV_SAMPLE_FMT_FLT:  p->phaser = phaser_flt;  break;
286     case AV_SAMPLE_FMT_FLTP: p->phaser = phaser_fltp; break;
287     case AV_SAMPLE_FMT_S16:  p->phaser = phaser_s16;  break;
288     case AV_SAMPLE_FMT_S16P: p->phaser = phaser_s16p; break;
289     case AV_SAMPLE_FMT_S32:  p->phaser = phaser_s32;  break;
290     case AV_SAMPLE_FMT_S32P: p->phaser = phaser_s32p; break;
291     default: av_assert0(0);
292     }
293
294     return 0;
295 }
296
297 static int filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
298 {
299     AudioPhaserContext *p = inlink->dst->priv;
300     AVFilterLink *outlink = inlink->dst->outputs[0];
301     AVFrame *outbuf;
302
303     if (av_frame_is_writable(inbuf)) {
304         outbuf = inbuf;
305     } else {
306         outbuf = ff_get_audio_buffer(inlink, inbuf->nb_samples);
307         if (!outbuf)
308             return AVERROR(ENOMEM);
309         av_frame_copy_props(outbuf, inbuf);
310     }
311
312     p->phaser(p, inbuf->extended_data, outbuf->extended_data,
313               outbuf->nb_samples, av_frame_get_channels(outbuf));
314
315     if (inbuf != outbuf)
316         av_frame_free(&inbuf);
317
318     return ff_filter_frame(outlink, outbuf);
319 }
320
321 static av_cold void uninit(AVFilterContext *ctx)
322 {
323     AudioPhaserContext *p = ctx->priv;
324
325     av_freep(&p->delay_buffer);
326     av_freep(&p->modulation_buffer);
327 }
328
329 static const AVFilterPad aphaser_inputs[] = {
330     {
331         .name         = "default",
332         .type         = AVMEDIA_TYPE_AUDIO,
333         .filter_frame = filter_frame,
334     },
335     { NULL }
336 };
337
338 static const AVFilterPad aphaser_outputs[] = {
339     {
340         .name         = "default",
341         .type         = AVMEDIA_TYPE_AUDIO,
342         .config_props = config_output,
343     },
344     { NULL }
345 };
346
347 static const char *const shorthand[] = { "in_gain", "out_gain", "delay", "decay", "speed", "type", NULL };
348
349 AVFilter avfilter_af_aphaser = {
350     .name          = "aphaser",
351     .description   = NULL_IF_CONFIG_SMALL("Add a phasing effect to the audio."),
352     .query_formats = query_formats,
353     .priv_size     = sizeof(AudioPhaserContext),
354     .init          = init,
355     .uninit        = uninit,
356     .inputs        = aphaser_inputs,
357     .outputs       = aphaser_outputs,
358     .priv_class    = &aphaser_class,
359     .shorthand     = shorthand,
360 };