Browse code

avfilter: add compensation delay line filter

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

Paul B Mahol authored on 2015/11/24 19:14:36
Showing 6 changed files
... ...
@@ -34,6 +34,7 @@ version <next>:
34 34
 - realtime filter
35 35
 - anoisesrc audio filter source
36 36
 - IVR demuxer
37
+- compensationdelay filter
37 38
 
38 39
 
39 40
 version 2.8:
... ...
@@ -1628,6 +1628,54 @@ compand=.1|.1:.1|.1:-45.1/-45.1|-45/-900|0/-900:.01:45:-90:.1
1628 1628
 @end example
1629 1629
 @end itemize
1630 1630
 
1631
+@section compensationdelay
1632
+
1633
+Compensation Delay Line is a metric based delay to compensate differing
1634
+positions of microphones or speakers.
1635
+
1636
+For example, you have recorded guitar with two microphones placed in
1637
+different location. Because the front of sound wave has fixed speed in
1638
+normal conditions, the phasing of microphones can vary and depends on
1639
+their location and interposition. The best sound mix can be achieved when
1640
+these microphones are in phase (synchronized). Note that distance of
1641
+~30 cm between microphones makes one microphone to capture signal in
1642
+antiphase to another microphone. That makes the final mix sounding moody.
1643
+This filter helps to solve phasing problems by adding different delays
1644
+to each microphone track and make them synchronized.
1645
+
1646
+The best result can be reached when you take one track as base and
1647
+synchronize other tracks one by one with it.
1648
+Remember that synchronization/delay tolerance depends on sample rate, too.
1649
+Higher sample rates will give more tolerance.
1650
+
1651
+It accepts the following parameters:
1652
+
1653
+@table @option
1654
+@item mm
1655
+Set millimeters distance. This is compensation distance for fine tuning.
1656
+Default is 0.
1657
+
1658
+@item cm
1659
+Set cm distance. This is compensation distance for tightening distance setup.
1660
+Default is 0.
1661
+
1662
+@item m
1663
+Set meters distance. This is compensation distance for hard distance setup.
1664
+Default is 0.
1665
+
1666
+@item dry
1667
+Set dry amount. Amount of unprocessed (dry) signal.
1668
+Default is 0.
1669
+
1670
+@item wet
1671
+Set wet amount. Amount of processed (wet) signal.
1672
+Default is 1.
1673
+
1674
+@item temp
1675
+Set temperature degree in Celsius. This is the temperature of the environment.
1676
+Default is 20.
1677
+@end table
1678
+
1631 1679
 @section dcshift
1632 1680
 Apply a DC shift to the audio.
1633 1681
 
... ...
@@ -64,6 +64,7 @@ OBJS-$(CONFIG_CHANNELMAP_FILTER)             += af_channelmap.o
64 64
 OBJS-$(CONFIG_CHANNELSPLIT_FILTER)           += af_channelsplit.o
65 65
 OBJS-$(CONFIG_CHORUS_FILTER)                 += af_chorus.o generate_wave_table.o
66 66
 OBJS-$(CONFIG_COMPAND_FILTER)                += af_compand.o
67
+OBJS-$(CONFIG_COMPENSATIONDELAY_FILTER)      += af_compensationdelay.o
67 68
 OBJS-$(CONFIG_DCSHIFT_FILTER)                += af_dcshift.o
68 69
 OBJS-$(CONFIG_DYNAUDNORM_FILTER)             += af_dynaudnorm.o
69 70
 OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
70 71
new file mode 100644
... ...
@@ -0,0 +1,198 @@
0
+/*
1
+ * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen, Vladimir Sadovnikov and others
2
+ * Copyright (c) 2015 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
+#include "libavutil/opt.h"
22
+#include "libavutil/samplefmt.h"
23
+#include "avfilter.h"
24
+#include "audio.h"
25
+#include "internal.h"
26
+
27
+typedef struct CompensationDelayContext {
28
+    const AVClass *class;
29
+    int distance_mm;
30
+    int distance_cm;
31
+    int distance_m;
32
+    double dry, wet;
33
+    int temp;
34
+
35
+    unsigned delay;
36
+    unsigned w_ptr;
37
+    unsigned buf_size;
38
+    AVFrame *delay_frame;
39
+} CompensationDelayContext;
40
+
41
+#define OFFSET(x) offsetof(CompensationDelayContext, x)
42
+#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
43
+
44
+static const AVOption compensationdelay_options[] = {
45
+    { "mm",   "set mm distance",    OFFSET(distance_mm), AV_OPT_TYPE_INT,    {.i64=0},    0,  10, A },
46
+    { "cm",   "set cm distance",    OFFSET(distance_cm), AV_OPT_TYPE_INT,    {.i64=0},    0, 100, A },
47
+    { "m",    "set meter distance", OFFSET(distance_m),  AV_OPT_TYPE_INT,    {.i64=0},    0, 100, A },
48
+    { "dry",  "set dry amount",     OFFSET(dry),         AV_OPT_TYPE_DOUBLE, {.dbl=0},    0,   1, A },
49
+    { "wet",  "set wet amount",     OFFSET(wet),         AV_OPT_TYPE_DOUBLE, {.dbl=1},    0,   1, A },
50
+    { "temp", "set temperature °C", OFFSET(temp),        AV_OPT_TYPE_INT,    {.i64=20}, -50,  50, A },
51
+    { NULL }
52
+};
53
+
54
+AVFILTER_DEFINE_CLASS(compensationdelay);
55
+
56
+// The maximum distance for options
57
+#define COMP_DELAY_MAX_DISTANCE            (100.0 * 100.0 + 100.0 * 1.0 + 1.0)
58
+// The actual speed of sound in normal conditions
59
+#define COMP_DELAY_SOUND_SPEED_KM_H(temp)  1.85325 * (643.95 * pow(((temp + 273.15) / 273.15), 0.5))
60
+#define COMP_DELAY_SOUND_SPEED_CM_S(temp)  (COMP_DELAY_SOUND_SPEED_KM_H(temp) * (1000.0 * 100.0) /* cm/km */ / (60.0 * 60.0) /* s/h */)
61
+#define COMP_DELAY_SOUND_FRONT_DELAY(temp) (1.0 / COMP_DELAY_SOUND_SPEED_CM_S(temp))
62
+// The maximum delay may be reached by this filter
63
+#define COMP_DELAY_MAX_DELAY               (COMP_DELAY_MAX_DISTANCE * COMP_DELAY_SOUND_FRONT_DELAY(50))
64
+
65
+static int query_formats(AVFilterContext *ctx)
66
+{
67
+    AVFilterChannelLayouts *layouts;
68
+    AVFilterFormats *formats;
69
+    static const enum AVSampleFormat sample_fmts[] = {
70
+        AV_SAMPLE_FMT_DBLP,
71
+        AV_SAMPLE_FMT_NONE
72
+    };
73
+    int ret;
74
+
75
+    layouts = ff_all_channel_counts();
76
+    if (!layouts)
77
+        return AVERROR(ENOMEM);
78
+    ret = ff_set_common_channel_layouts(ctx, layouts);
79
+    if (ret < 0)
80
+        return ret;
81
+
82
+    formats = ff_make_format_list(sample_fmts);
83
+    if (!formats)
84
+        return AVERROR(ENOMEM);
85
+    ret = ff_set_common_formats(ctx, formats);
86
+    if (ret < 0)
87
+        return ret;
88
+
89
+    formats = ff_all_samplerates();
90
+    if (!formats)
91
+        return AVERROR(ENOMEM);
92
+    return ff_set_common_samplerates(ctx, formats);
93
+}
94
+
95
+static int config_input(AVFilterLink *inlink)
96
+{
97
+    AVFilterContext *ctx = inlink->dst;
98
+    CompensationDelayContext *s = ctx->priv;
99
+    unsigned min_size, new_size = 1;
100
+
101
+    s->delay = (s->distance_m * 100. + s->distance_cm * 1. + s->distance_mm * .1) *
102
+               COMP_DELAY_SOUND_FRONT_DELAY(s->temp) * inlink->sample_rate;
103
+    min_size = inlink->sample_rate * COMP_DELAY_MAX_DELAY;
104
+
105
+    while (new_size < min_size)
106
+        new_size <<= 1;
107
+
108
+    s->delay_frame = av_frame_alloc();
109
+    if (!s->delay_frame)
110
+        return AVERROR(ENOMEM);
111
+
112
+    s->buf_size                    = new_size;
113
+    s->delay_frame->format         = inlink->format;
114
+    s->delay_frame->nb_samples     = new_size;
115
+    s->delay_frame->channel_layout = inlink->channel_layout;
116
+
117
+    return av_frame_get_buffer(s->delay_frame, 32);
118
+}
119
+
120
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
121
+{
122
+    AVFilterContext *ctx = inlink->dst;
123
+    CompensationDelayContext *s = ctx->priv;
124
+    const unsigned b_mask = s->buf_size - 1;
125
+    const unsigned buf_size = s->buf_size;
126
+    const unsigned delay = s->delay;
127
+    const double dry = s->dry;
128
+    const double wet = s->wet;
129
+    unsigned r_ptr, w_ptr;
130
+    AVFrame *out;
131
+    int n, ch;
132
+
133
+    out = ff_get_audio_buffer(inlink, in->nb_samples);
134
+    if (!out) {
135
+        av_frame_free(&in);
136
+        return AVERROR(ENOMEM);
137
+    }
138
+    av_frame_copy_props(out, in);
139
+
140
+    for (ch = 0; ch < inlink->channels; ch++) {
141
+        const double *src = (const double *)in->extended_data[ch];
142
+        double *dst = (double *)out->extended_data[ch];
143
+        double *buffer = (double *)s->delay_frame->extended_data[ch];
144
+
145
+        w_ptr =  s->w_ptr;
146
+        r_ptr = (w_ptr + buf_size - delay) & b_mask;
147
+
148
+        for (n = 0; n < in->nb_samples; n++) {
149
+            const double sample = src[n];
150
+
151
+            buffer[w_ptr] = sample;
152
+            dst[n] = dry * sample + wet * buffer[r_ptr];
153
+            w_ptr = (w_ptr + 1) & b_mask;
154
+            r_ptr = (r_ptr + 1) & b_mask;
155
+        }
156
+    }
157
+    s->w_ptr = w_ptr;
158
+
159
+    av_frame_free(&in);
160
+    return ff_filter_frame(ctx->outputs[0], out);
161
+}
162
+
163
+static av_cold void uninit(AVFilterContext *ctx)
164
+{
165
+    CompensationDelayContext *s = ctx->priv;
166
+
167
+    av_frame_free(&s->delay_frame);
168
+}
169
+
170
+static const AVFilterPad compensationdelay_inputs[] = {
171
+    {
172
+        .name         = "default",
173
+        .type         = AVMEDIA_TYPE_AUDIO,
174
+        .config_props = config_input,
175
+        .filter_frame = filter_frame,
176
+    },
177
+    { NULL }
178
+};
179
+
180
+static const AVFilterPad compensationdelay_outputs[] = {
181
+    {
182
+        .name = "default",
183
+        .type = AVMEDIA_TYPE_AUDIO,
184
+    },
185
+    { NULL }
186
+};
187
+
188
+AVFilter ff_af_compensationdelay = {
189
+    .name          = "compensationdelay",
190
+    .description   = NULL_IF_CONFIG_SMALL("Audio Compensation Delay Line."),
191
+    .query_formats = query_formats,
192
+    .priv_size     = sizeof(CompensationDelayContext),
193
+    .priv_class    = &compensationdelay_class,
194
+    .uninit        = uninit,
195
+    .inputs        = compensationdelay_inputs,
196
+    .outputs       = compensationdelay_outputs,
197
+};
... ...
@@ -86,6 +86,7 @@ void avfilter_register_all(void)
86 86
     REGISTER_FILTER(CHANNELSPLIT,   channelsplit,   af);
87 87
     REGISTER_FILTER(CHORUS,         chorus,         af);
88 88
     REGISTER_FILTER(COMPAND,        compand,        af);
89
+    REGISTER_FILTER(COMPENSATIONDELAY, compensationdelay, af);
89 90
     REGISTER_FILTER(DCSHIFT,        dcshift,        af);
90 91
     REGISTER_FILTER(DYNAUDNORM,     dynaudnorm,     af);
91 92
     REGISTER_FILTER(EARWAX,         earwax,         af);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  15
33
+#define LIBAVFILTER_VERSION_MINOR  16
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \