Browse code

avfilter: add vibrato filter

Signed-off-by: Kyle Swanson <k@ylo.ph>

Kyle Swanson authored on 2015/10/24 14:08:19
Showing 6 changed files
... ...
@@ -26,6 +26,7 @@ version <next>:
26 26
 - zero-copy Intel QSV transcoding in ffmpeg
27 27
 - shuffleframes filter
28 28
 - SDX2 DPCM decoder
29
+- vibrato filter
29 30
 
30 31
 
31 32
 version 2.8:
... ...
@@ -2719,6 +2719,22 @@ Depth of modulation as a percentage. Range is 0.0 - 1.0.
2719 2719
 Default value is 0.5.
2720 2720
 @end table
2721 2721
 
2722
+@section vibrato
2723
+
2724
+Sinusoidal phase modulation.
2725
+
2726
+The filter accepts the following options:
2727
+
2728
+@table @option
2729
+@item f
2730
+Modulation frequency in Hertz.
2731
+Range is 0.1 - 20000.0. Default value is 5.0 Hz.
2732
+
2733
+@item d
2734
+Depth of modulation as a percentage. Range is 0.0 - 1.0.
2735
+Default value is 0.5.
2736
+@end table
2737
+
2722 2738
 @section volume
2723 2739
 
2724 2740
 Adjust the input audio volume.
... ...
@@ -86,6 +86,7 @@ OBJS-$(CONFIG_STEREOTOOLS_FILTER)            += af_stereotools.o
86 86
 OBJS-$(CONFIG_STEREOWIDEN_FILTER)            += af_stereowiden.o
87 87
 OBJS-$(CONFIG_TREBLE_FILTER)                 += af_biquads.o
88 88
 OBJS-$(CONFIG_TREMOLO_FILTER)                += af_tremolo.o
89
+OBJS-$(CONFIG_VIBRATO_FILTER)                += af_vibrato.o generate_wave_table.o
89 90
 OBJS-$(CONFIG_VOLUME_FILTER)                 += af_volume.o
90 91
 OBJS-$(CONFIG_VOLUMEDETECT_FILTER)           += af_volumedetect.o
91 92
 
92 93
new file mode 100644
... ...
@@ -0,0 +1,210 @@
0
+/*
1
+ * Copyright (c) 2015 Kyle Swanson <k@ylo.ph>.
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
+#include "libavutil/opt.h"
21
+#include "avfilter.h"
22
+#include "internal.h"
23
+#include "audio.h"
24
+#include "generate_wave_table.h"
25
+
26
+typedef struct VibratoContext {
27
+    const AVClass *class;
28
+    double freq;
29
+    double depth;
30
+    int channels;
31
+
32
+    double **buf;
33
+    int buf_index;
34
+    int buf_size;
35
+
36
+    double *wave_table;
37
+    int wave_table_index;
38
+    int wave_table_size;
39
+} VibratoContext;
40
+
41
+#define OFFSET(x) offsetof(VibratoContext, x)
42
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
43
+
44
+static const AVOption vibrato_options[] = {
45
+    { "f", "set frequency in hertz",    OFFSET(freq),    AV_OPT_TYPE_DOUBLE,   {.dbl = 5.0},   0.1,   20000.0, FLAGS },
46
+    { "d", "set depth as percentage",   OFFSET(depth),   AV_OPT_TYPE_DOUBLE,   {.dbl = 0.5},   0.00,  1.0,     FLAGS },
47
+    { NULL }
48
+};
49
+
50
+AVFILTER_DEFINE_CLASS(vibrato);
51
+
52
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
53
+{
54
+    AVFilterContext *ctx = inlink->dst;
55
+    VibratoContext *s = ctx->priv;
56
+    AVFilterLink *outlink = ctx->outputs[0];
57
+    AVFrame *out;
58
+    int n, c;
59
+    const double *src;
60
+    double *dst;
61
+
62
+    if (av_frame_is_writable(in)) {
63
+        out = in;
64
+    } else {
65
+        out = ff_get_audio_buffer(inlink, in->nb_samples);
66
+        if (!out) {
67
+            av_frame_free(&in);
68
+            return AVERROR(ENOMEM);
69
+        }
70
+        av_frame_copy_props(out, in);
71
+    }
72
+
73
+
74
+    for (n = 0; n < in->nb_samples; n++) {
75
+        double integer, decimal;
76
+        decimal = modf(s->depth * s->wave_table[s->wave_table_index], &integer);
77
+
78
+        s->wave_table_index++;
79
+        if (s->wave_table_index >= s->wave_table_size)
80
+            s->wave_table_index -= s->wave_table_size;
81
+
82
+        for (c = 0; c < inlink->channels; c++) {
83
+            int samp1_index, samp2_index;
84
+            double *buf;
85
+            double this_samp;
86
+
87
+            src = (const double *)in->extended_data[c];
88
+            dst = (double *)out->extended_data[c];
89
+            buf = s->buf[c];
90
+
91
+            samp1_index = s->buf_index + integer;
92
+            if (samp1_index >= s->buf_size)
93
+                samp1_index -= s->buf_size;
94
+            samp2_index = samp1_index + 1;
95
+            if (samp2_index >= s->buf_size)
96
+                samp2_index -= s->buf_size;
97
+
98
+            this_samp = src[n];
99
+            dst[n] = buf[samp1_index] + (decimal * (buf[samp2_index] - buf[samp1_index]));
100
+            buf[s->buf_index] = this_samp;
101
+        }
102
+        s->buf_index++;
103
+        if (s->buf_index >= s->buf_size)
104
+            s->buf_index -= s->buf_size;
105
+    }
106
+
107
+    if (in != out)
108
+        av_frame_free(&in);
109
+
110
+    return ff_filter_frame(outlink, out);
111
+}
112
+
113
+static int query_formats(AVFilterContext *ctx)
114
+{
115
+    AVFilterFormats *formats;
116
+    AVFilterChannelLayouts *layouts;
117
+    static const enum AVSampleFormat sample_fmts[] = {
118
+        AV_SAMPLE_FMT_DBLP,
119
+        AV_SAMPLE_FMT_NONE
120
+    };
121
+    int ret;
122
+
123
+    layouts = ff_all_channel_counts();
124
+    if (!layouts)
125
+        return AVERROR(ENOMEM);
126
+    ret = ff_set_common_channel_layouts(ctx, layouts);
127
+    if (ret < 0)
128
+        return ret;
129
+
130
+    formats = ff_make_format_list(sample_fmts);
131
+    if (!formats)
132
+        return AVERROR(ENOMEM);
133
+    ret = ff_set_common_formats(ctx, formats);
134
+    if (ret < 0)
135
+        return ret;
136
+
137
+    formats = ff_all_samplerates();
138
+    if (!formats)
139
+        return AVERROR(ENOMEM);
140
+    return ff_set_common_samplerates(ctx, formats);
141
+}
142
+
143
+static av_cold void uninit(AVFilterContext *ctx)
144
+{
145
+    VibratoContext *s = ctx->priv;
146
+    int c;
147
+
148
+    av_freep(&s->wave_table);
149
+    for (c = 0; c < s->channels; c++)
150
+        av_freep(&s->buf[c]);
151
+    av_freep(&s->buf);
152
+}
153
+
154
+static int config_input(AVFilterLink *inlink)
155
+{
156
+    int c;
157
+    AVFilterContext *ctx = inlink->dst;
158
+    VibratoContext *s = ctx->priv;
159
+    s->channels = inlink->channels;
160
+
161
+    s->buf = av_calloc(inlink->channels, sizeof(*s->buf));
162
+    if (!s->buf)
163
+        return AVERROR(ENOMEM);
164
+    s->buf_size = inlink->sample_rate * 0.005;
165
+    for (c = 0; c < s->channels; c++) {
166
+        s->buf[c] = av_malloc_array(s->buf_size, sizeof(*s->buf[c]));
167
+        if (!s->buf[c])
168
+            return AVERROR(ENOMEM);
169
+    }
170
+    s->buf_index = 0;
171
+
172
+    s->wave_table_size = inlink->sample_rate / s->freq;
173
+    s->wave_table = av_malloc_array(s->wave_table_size, sizeof(*s->wave_table));
174
+    if (!s->wave_table)
175
+        return AVERROR(ENOMEM);
176
+    ff_generate_wave_table(WAVE_SIN, AV_SAMPLE_FMT_DBL, s->wave_table, s->wave_table_size, 0.0, s->buf_size - 1, 3.0 * M_PI_2);
177
+    s->wave_table_index = 0;
178
+
179
+    return 0;
180
+}
181
+
182
+static const AVFilterPad avfilter_af_vibrato_inputs[] = {
183
+    {
184
+        .name         = "default",
185
+        .type         = AVMEDIA_TYPE_AUDIO,
186
+        .config_props = config_input,
187
+        .filter_frame = filter_frame,
188
+    },
189
+    { NULL }
190
+};
191
+
192
+static const AVFilterPad avfilter_af_vibrato_outputs[] = {
193
+    {
194
+        .name = "default",
195
+        .type = AVMEDIA_TYPE_AUDIO,
196
+    },
197
+    { NULL }
198
+};
199
+
200
+AVFilter ff_af_vibrato = {
201
+    .name          = "vibrato",
202
+    .description   = NULL_IF_CONFIG_SMALL("Apply vibrato effect."),
203
+    .priv_size     = sizeof(VibratoContext),
204
+    .priv_class    = &vibrato_class,
205
+    .uninit        = uninit,
206
+    .query_formats = query_formats,
207
+    .inputs        = avfilter_af_vibrato_inputs,
208
+    .outputs       = avfilter_af_vibrato_outputs,
209
+};
... ...
@@ -108,6 +108,7 @@ void avfilter_register_all(void)
108 108
     REGISTER_FILTER(STEREOWIDEN,    stereowiden,    af);
109 109
     REGISTER_FILTER(TREBLE,         treble,         af);
110 110
     REGISTER_FILTER(TREMOLO,        tremolo,        af);
111
+    REGISTER_FILTER(VIBRATO,        vibrato,        af);
111 112
     REGISTER_FILTER(VOLUME,         volume,         af);
112 113
     REGISTER_FILTER(VOLUMEDETECT,   volumedetect,   af);
113 114
 
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  13
33
+#define LIBAVFILTER_VERSION_MINOR  14
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \