Browse code

aphaser filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2013/03/30 13:03:39
Showing 6 changed files
... ...
@@ -15,6 +15,7 @@ version <next>:
15 15
 - new ffmpeg options -filter_script and -filter_complex_script, which allow a
16 16
   filtergraph description to be read from a file
17 17
 - OpenCL support
18
+- audio phaser filter
18 19
 
19 20
 
20 21
 version 1.2:
... ...
@@ -6266,6 +6266,43 @@ following one, the permission might not be received as expected in that
6266 6266
 following filter. Inserting a @ref{format} or @ref{aformat} filter before the
6267 6267
 perms/aperms filter can avoid this problem.
6268 6268
 
6269
+@section aphaser
6270
+Add a phasing effect to the input audio.
6271
+
6272
+A phaser filter creates series of peaks and troughs in the frequency spectrum.
6273
+The position of the peaks and troughs are modulated so that they vary over time, creating a sweeping effect.
6274
+
6275
+The filter accepts parameters as a list of @var{key}=@var{value}
6276
+pairs, separated by ":".
6277
+
6278
+A description of the accepted parameters follows.
6279
+
6280
+@table @option
6281
+@item in_gain
6282
+Set input gain. Default is 0.4.
6283
+
6284
+@item out_gain
6285
+Set output gain. Default is 0.74
6286
+
6287
+@item delay
6288
+Set delay in milliseconds. Default is 3.0.
6289
+
6290
+@item decay
6291
+Set decay. Default is 0.4.
6292
+
6293
+@item speed
6294
+Set modulation speed in Hz. Default is 0.5.
6295
+
6296
+@item type
6297
+Set modulation type. Default is triangular.
6298
+
6299
+It accepts the following values:
6300
+@table @samp
6301
+@item triangular, t
6302
+@item sinusoidal, s
6303
+@end table
6304
+@end table
6305
+
6269 6306
 @section aselect, select
6270 6307
 Select frames to pass in output.
6271 6308
 
... ...
@@ -58,6 +58,7 @@ OBJS-$(CONFIG_AMIX_FILTER)                   += af_amix.o
58 58
 OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
59 59
 OBJS-$(CONFIG_APAD_FILTER)                   += af_apad.o
60 60
 OBJS-$(CONFIG_APERMS_FILTER)                 += f_perms.o
61
+OBJS-$(CONFIG_APHASER_FILTER)                += af_aphaser.o
61 62
 OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
62 63
 OBJS-$(CONFIG_ASELECT_FILTER)                += f_select.o
63 64
 OBJS-$(CONFIG_ASENDCMD_FILTER)               += f_sendcmd.o
64 65
new file mode 100644
... ...
@@ -0,0 +1,360 @@
0
+/*
1
+ * Copyright (c) 2013 Paul B Mahol
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * phaser audio filter
23
+ */
24
+
25
+#include "libavutil/avassert.h"
26
+#include "libavutil/opt.h"
27
+#include "audio.h"
28
+#include "avfilter.h"
29
+#include "internal.h"
30
+
31
+enum WaveType {
32
+    WAVE_SIN,
33
+    WAVE_TRI,
34
+    WAVE_NB,
35
+};
36
+
37
+typedef struct AudioPhaserContext {
38
+    const AVClass *class;
39
+    double in_gain, out_gain;
40
+    double delay;
41
+    double decay;
42
+    double speed;
43
+
44
+    enum WaveType type;
45
+
46
+    int delay_buffer_length;
47
+    double *delay_buffer;
48
+
49
+    int modulation_buffer_length;
50
+    int32_t *modulation_buffer;
51
+
52
+    int delay_pos, modulation_pos;
53
+
54
+    void (*phaser)(struct AudioPhaserContext *p,
55
+                   uint8_t * const *src, uint8_t **dst,
56
+                   int nb_samples, int channels);
57
+} AudioPhaserContext;
58
+
59
+#define OFFSET(x) offsetof(AudioPhaserContext, x)
60
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
61
+
62
+static const AVOption aphaser_options[] = {
63
+    { "in_gain",  "set input gain",            OFFSET(in_gain),  AV_OPT_TYPE_DOUBLE, {.dbl=.4},  0,  1,   FLAGS },
64
+    { "out_gain", "set output gain",           OFFSET(out_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.74}, 0,  1e9, FLAGS },
65
+    { "delay",    "set delay in milliseconds", OFFSET(delay),    AV_OPT_TYPE_DOUBLE, {.dbl=3.},  0,  5,   FLAGS },
66
+    { "decay",    "set decay",                 OFFSET(decay),    AV_OPT_TYPE_DOUBLE, {.dbl=.4},  0, .99,  FLAGS },
67
+    { "speed",    "set modulation speed",      OFFSET(speed),    AV_OPT_TYPE_DOUBLE, {.dbl=.5}, .1,  2,   FLAGS },
68
+    { "type",     "set modulation type",       OFFSET(type),     AV_OPT_TYPE_INT,    {.i64=WAVE_TRI}, 0, WAVE_NB-1, FLAGS, "type" },
69
+    { "triangular",  NULL, 0, AV_OPT_TYPE_CONST,  {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
70
+    { "t",           NULL, 0, AV_OPT_TYPE_CONST,  {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
71
+    { "sinusoidal",  NULL, 0, AV_OPT_TYPE_CONST,  {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
72
+    { "s",           NULL, 0, AV_OPT_TYPE_CONST,  {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
73
+    { NULL },
74
+};
75
+
76
+AVFILTER_DEFINE_CLASS(aphaser);
77
+
78
+static av_cold int init(AVFilterContext *ctx, const char *args)
79
+{
80
+    AudioPhaserContext *p = ctx->priv;
81
+
82
+    if (p->in_gain > (1 - p->decay * p->decay))
83
+        av_log(ctx, AV_LOG_WARNING, "in_gain may cause clipping\n");
84
+    if (p->in_gain / (1 - p->decay) > 1 / p->out_gain)
85
+        av_log(ctx, AV_LOG_WARNING, "out_gain may cause clipping\n");
86
+
87
+    return 0;
88
+}
89
+
90
+static int query_formats(AVFilterContext *ctx)
91
+{
92
+    AVFilterFormats *formats;
93
+    AVFilterChannelLayouts *layouts;
94
+    static const enum AVSampleFormat sample_fmts[] = {
95
+        AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
96
+        AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
97
+        AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
98
+        AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
99
+        AV_SAMPLE_FMT_NONE
100
+    };
101
+
102
+    layouts = ff_all_channel_layouts();
103
+    if (!layouts)
104
+        return AVERROR(ENOMEM);
105
+    ff_set_common_channel_layouts(ctx, layouts);
106
+
107
+    formats = ff_make_format_list(sample_fmts);
108
+    if (!formats)
109
+        return AVERROR(ENOMEM);
110
+    ff_set_common_formats(ctx, formats);
111
+
112
+    formats = ff_all_samplerates();
113
+    if (!formats)
114
+        return AVERROR(ENOMEM);
115
+    ff_set_common_samplerates(ctx, formats);
116
+
117
+    return 0;
118
+}
119
+
120
+static void generate_wave_table(enum WaveType wave_type, enum AVSampleFormat sample_fmt,
121
+                                void *table, int table_size,
122
+                                double min, double max, double phase)
123
+{
124
+    uint32_t i, phase_offset = phase / M_PI / 2 * table_size + 0.5;
125
+
126
+    for (i = 0; i < table_size; i++) {
127
+        uint32_t point = (i + phase_offset) % table_size;
128
+        double d;
129
+
130
+        switch (wave_type) {
131
+        case WAVE_SIN:
132
+            d = (sin((double)point / table_size * 2 * M_PI) + 1) / 2;
133
+            break;
134
+        case WAVE_TRI:
135
+            d = (double)point * 2 / table_size;
136
+            switch (4 * point / table_size) {
137
+            case 0: d = d + 0.5; break;
138
+            case 1:
139
+            case 2: d = 1.5 - d; break;
140
+            case 3: d = d - 1.5; break;
141
+            }
142
+            break;
143
+        default:
144
+            av_assert0(0);
145
+        }
146
+
147
+        d  = d * (max - min) + min;
148
+        switch (sample_fmt) {
149
+        case AV_SAMPLE_FMT_FLT: {
150
+            float *fp = (float *)table;
151
+            *fp++ = (float)d;
152
+            table = fp;
153
+            continue; }
154
+        case AV_SAMPLE_FMT_DBL: {
155
+            double *dp = (double *)table;
156
+            *dp++ = d;
157
+            table = dp;
158
+            continue; }
159
+        }
160
+
161
+        d += d < 0 ? -0.5 : 0.5;
162
+        switch (sample_fmt) {
163
+        case AV_SAMPLE_FMT_S16: {
164
+            int16_t *sp = table;
165
+            *sp++ = (int16_t)d;
166
+            table = sp;
167
+            continue; }
168
+        case AV_SAMPLE_FMT_S32: {
169
+            int32_t *ip = table;
170
+            *ip++ = (int32_t)d;
171
+            table = ip;
172
+            continue; }
173
+        default:
174
+            av_assert0(0);
175
+        }
176
+    }
177
+}
178
+
179
+#define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
180
+
181
+#define PHASER_PLANAR(name, type)                                      \
182
+static void phaser_## name ##p(AudioPhaserContext *p,                  \
183
+                               uint8_t * const *src, uint8_t **dst,    \
184
+                               int nb_samples, int channels)           \
185
+{                                                                      \
186
+    int i, c, delay_pos, modulation_pos;                               \
187
+                                                                       \
188
+    for (c = 0; c < channels; c++) {                                   \
189
+        type *s = (type *)src[c];                                      \
190
+        type *d = (type *)dst[c];                                      \
191
+        double *buffer = p->delay_buffer +                             \
192
+                         c * p->delay_buffer_length;                   \
193
+                                                                       \
194
+        delay_pos      = p->delay_pos;                                 \
195
+        modulation_pos = p->modulation_pos;                            \
196
+                                                                       \
197
+        for (i = 0; i < nb_samples; i++, s++, d++) {                   \
198
+            double v = *s * p->in_gain + buffer[                       \
199
+                       MOD(delay_pos + p->modulation_buffer[           \
200
+                       modulation_pos],                                \
201
+                       p->delay_buffer_length)] * p->decay;            \
202
+                                                                       \
203
+            modulation_pos = MOD(modulation_pos + 1,                   \
204
+                             p->modulation_buffer_length);             \
205
+            delay_pos = MOD(delay_pos + 1, p->delay_buffer_length);    \
206
+            buffer[delay_pos] = v;                                     \
207
+                                                                       \
208
+            *d = v * p->out_gain;                                      \
209
+        }                                                              \
210
+    }                                                                  \
211
+                                                                       \
212
+    p->delay_pos      = delay_pos;                                     \
213
+    p->modulation_pos = modulation_pos;                                \
214
+}
215
+
216
+#define PHASER(name, type)                                              \
217
+static void phaser_## name (AudioPhaserContext *p,                      \
218
+                            uint8_t * const *src, uint8_t **dst,        \
219
+                            int nb_samples, int channels)               \
220
+{                                                                       \
221
+    int i, c, delay_pos, modulation_pos;                                \
222
+    type *s = (type *)src[0];                                           \
223
+    type *d = (type *)dst[0];                                           \
224
+    double *buffer = p->delay_buffer;                                   \
225
+                                                                        \
226
+    delay_pos      = p->delay_pos;                                      \
227
+    modulation_pos = p->modulation_pos;                                 \
228
+                                                                        \
229
+    for (i = 0; i < nb_samples; i++) {                                  \
230
+        int pos = MOD(delay_pos + p->modulation_buffer[modulation_pos], \
231
+                   p->delay_buffer_length) * channels;                  \
232
+        int npos;                                                       \
233
+                                                                        \
234
+        delay_pos = MOD(delay_pos + 1, p->delay_buffer_length);         \
235
+        npos = delay_pos * channels;                                    \
236
+        for (c = 0; c < channels; c++, s++, d++) {                      \
237
+            double v = *s * p->in_gain + buffer[pos + c] * p->decay;    \
238
+                                                                        \
239
+            buffer[npos + c] = v;                                       \
240
+                                                                        \
241
+            *d = v * p->out_gain;                                       \
242
+        }                                                               \
243
+                                                                        \
244
+        modulation_pos = MOD(modulation_pos + 1,                        \
245
+                         p->modulation_buffer_length);                  \
246
+    }                                                                   \
247
+                                                                        \
248
+    p->delay_pos      = delay_pos;                                      \
249
+    p->modulation_pos = modulation_pos;                                 \
250
+}
251
+
252
+PHASER_PLANAR(dbl, double)
253
+PHASER_PLANAR(flt, float)
254
+PHASER_PLANAR(s16, int16_t)
255
+PHASER_PLANAR(s32, int32_t)
256
+
257
+PHASER(dbl, double)
258
+PHASER(flt, float)
259
+PHASER(s16, int16_t)
260
+PHASER(s32, int32_t)
261
+
262
+static int config_output(AVFilterLink *outlink)
263
+{
264
+    AudioPhaserContext *p = outlink->src->priv;
265
+    AVFilterLink *inlink = outlink->src->inputs[0];
266
+
267
+    p->delay_buffer_length = p->delay * 0.001 * inlink->sample_rate + 0.5;
268
+    p->delay_buffer = av_calloc(p->delay_buffer_length, sizeof(*p->delay_buffer) * inlink->channels);
269
+    p->modulation_buffer_length = inlink->sample_rate / p->speed + 0.5;
270
+    p->modulation_buffer = av_malloc(p->modulation_buffer_length * sizeof(*p->modulation_buffer));
271
+
272
+    if (!p->modulation_buffer || !p->delay_buffer)
273
+        return AVERROR(ENOMEM);
274
+
275
+    generate_wave_table(p->type, AV_SAMPLE_FMT_S32,
276
+                        p->modulation_buffer, p->modulation_buffer_length,
277
+                        1., p->delay_buffer_length, M_PI / 2.0);
278
+
279
+    p->delay_pos = p->modulation_pos = 0;
280
+
281
+    switch (inlink->format) {
282
+    case AV_SAMPLE_FMT_DBL:  p->phaser = phaser_dbl;  break;
283
+    case AV_SAMPLE_FMT_DBLP: p->phaser = phaser_dblp; break;
284
+    case AV_SAMPLE_FMT_FLT:  p->phaser = phaser_flt;  break;
285
+    case AV_SAMPLE_FMT_FLTP: p->phaser = phaser_fltp; break;
286
+    case AV_SAMPLE_FMT_S16:  p->phaser = phaser_s16;  break;
287
+    case AV_SAMPLE_FMT_S16P: p->phaser = phaser_s16p; break;
288
+    case AV_SAMPLE_FMT_S32:  p->phaser = phaser_s32;  break;
289
+    case AV_SAMPLE_FMT_S32P: p->phaser = phaser_s32p; break;
290
+    default: av_assert0(0);
291
+    }
292
+
293
+    return 0;
294
+}
295
+
296
+static int filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
297
+{
298
+    AudioPhaserContext *p = inlink->dst->priv;
299
+    AVFilterLink *outlink = inlink->dst->outputs[0];
300
+    AVFrame *outbuf;
301
+
302
+    if (av_frame_is_writable(inbuf)) {
303
+        outbuf = inbuf;
304
+    } else {
305
+        outbuf = ff_get_audio_buffer(inlink, inbuf->nb_samples);
306
+        if (!outbuf)
307
+            return AVERROR(ENOMEM);
308
+        av_frame_copy_props(outbuf, inbuf);
309
+    }
310
+
311
+    p->phaser(p, inbuf->extended_data, outbuf->extended_data,
312
+              outbuf->nb_samples, av_frame_get_channels(outbuf));
313
+
314
+    if (inbuf != outbuf)
315
+        av_frame_free(&inbuf);
316
+
317
+    return ff_filter_frame(outlink, outbuf);
318
+}
319
+
320
+static av_cold void uninit(AVFilterContext *ctx)
321
+{
322
+    AudioPhaserContext *p = ctx->priv;
323
+
324
+    av_freep(&p->delay_buffer);
325
+    av_freep(&p->modulation_buffer);
326
+}
327
+
328
+static const AVFilterPad aphaser_inputs[] = {
329
+    {
330
+        .name         = "default",
331
+        .type         = AVMEDIA_TYPE_AUDIO,
332
+        .filter_frame = filter_frame,
333
+    },
334
+    { NULL }
335
+};
336
+
337
+static const AVFilterPad aphaser_outputs[] = {
338
+    {
339
+        .name         = "default",
340
+        .type         = AVMEDIA_TYPE_AUDIO,
341
+        .config_props = config_output,
342
+    },
343
+    { NULL }
344
+};
345
+
346
+static const char *const shorthand[] = { "in_gain", "out_gain", "delay", "decay", "speed", "type", NULL };
347
+
348
+AVFilter avfilter_af_aphaser = {
349
+    .name          = "aphaser",
350
+    .description   = NULL_IF_CONFIG_SMALL("Add a phasing effect to the audio."),
351
+    .query_formats = query_formats,
352
+    .priv_size     = sizeof(AudioPhaserContext),
353
+    .init          = init,
354
+    .uninit        = uninit,
355
+    .inputs        = aphaser_inputs,
356
+    .outputs       = aphaser_outputs,
357
+    .priv_class    = &aphaser_class,
358
+    .shorthand     = shorthand,
359
+};
... ...
@@ -54,6 +54,7 @@ void avfilter_register_all(void)
54 54
     REGISTER_FILTER(ANULL,          anull,          af);
55 55
     REGISTER_FILTER(APAD,           apad,           af);
56 56
     REGISTER_FILTER(APERMS,         aperms,         af);
57
+    REGISTER_FILTER(APHASER,        aphaser,        af);
57 58
     REGISTER_FILTER(ARESAMPLE,      aresample,      af);
58 59
     REGISTER_FILTER(ASELECT,        aselect,        af);
59 60
     REGISTER_FILTER(ASENDCMD,       asendcmd,       af);
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  48
33
-#define LIBAVFILTER_VERSION_MICRO 105
32
+#define LIBAVFILTER_VERSION_MINOR  49
33
+#define LIBAVFILTER_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \