Browse code

avfilter: add stereo tools filter

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

Paul B Mahol authored on 2015/09/15 23:23:04
Showing 5 changed files
... ...
@@ -2408,6 +2408,103 @@ silenceremove=1:5:0.02
2408 2408
 @end example
2409 2409
 @end itemize
2410 2410
 
2411
+@section stereotools
2412
+
2413
+This filter has some handy utilities to manage stereo signals, for converting
2414
+M/S stereo recordings to L/R signal while having control over the parameters
2415
+or spreading the stereo image of master track.
2416
+
2417
+The filter accepts the following options:
2418
+
2419
+@table @option
2420
+@table level_in
2421
+Set input level before filtering for both channels. Defaults is 1.
2422
+Allowed range is from 0.015625 to 64.
2423
+
2424
+@table level_out
2425
+Set output level after filtering for both channels. Defaults is 1.
2426
+Allowed range is from 0.015625 to 64.
2427
+
2428
+@item balance_in
2429
+Set input balance between both channels. Default is 0.
2430
+Allowed range is from -1 to 1.
2431
+
2432
+@item balance_out
2433
+Set output balance between both channels. Default is 0.
2434
+Allowed range is from -1 to 1.
2435
+
2436
+@item softclip
2437
+Enable softclipping. Results in analog distortion instead of harsh digital 0dB
2438
+clipping. Disabled by default.
2439
+
2440
+@item mutel
2441
+Mute the left channel. Disabled by default.
2442
+
2443
+@item muter
2444
+Mute the right channel. Disabled by default.
2445
+
2446
+@item phasel
2447
+Change the phase of the left channel. Disabled by default.
2448
+
2449
+@item phaser
2450
+Change the phase of the right channel. Disabled by default.
2451
+
2452
+@item mode
2453
+Set stereo mode. Available values are:
2454
+
2455
+@table @samp
2456
+@item lr>lr
2457
+Left/Right to Left/Right, this is default.
2458
+
2459
+@item lr>ms
2460
+Left/Right to Mid/Side.
2461
+
2462
+@item ms>lr
2463
+Mid/Side to Left/Right.
2464
+
2465
+@item lr>ll
2466
+Left/Right to Left/Left.
2467
+
2468
+@item lr>rr
2469
+Left/Right to Right/Right.
2470
+
2471
+@item lr>l+r
2472
+Left/Right to Left + Right.
2473
+
2474
+@item lr>rl
2475
+Left/Right to Right/Left.
2476
+@end table
2477
+
2478
+@item slev
2479
+Set level of side signal. Default is 1.
2480
+Allowed range is from 0.015625 to 64.
2481
+
2482
+@item sbal
2483
+Set balance of side signal. Default is 0.
2484
+Allowed range is from -1 to 1.
2485
+
2486
+@item mlev
2487
+Set level of the middle signal. Default is 1.
2488
+Allowed range is from 0.015625 to 64.
2489
+
2490
+@item mpan
2491
+Set middle signal pan. Default is 0. Allowed range is from -1 to 1.
2492
+
2493
+@item base
2494
+Set stereo base between mono and inversed channels. Default is 0.
2495
+Allowed range is from -1 to 1.
2496
+
2497
+@item delay
2498
+Set delay in milliseconds how much to delay left from right channel and
2499
+vice versa. Default is 0. Allowed range is from -20 to 20.
2500
+
2501
+@item sclevel
2502
+Set S/C level. Default is 1. Allowed range is from 1 to 100.
2503
+
2504
+@item phase
2505
+Set the stereo phase in degrees. Default is 0. Allowed range is from 0 to 360.
2506
+@end table
2507
+
2411 2508
 @section stereowiden
2412 2509
 
2413 2510
 This filter enhance the stereo effect by suppressing signal common to both
... ...
@@ -80,6 +80,7 @@ OBJS-$(CONFIG_RESAMPLE_FILTER)               += af_resample.o
80 80
 OBJS-$(CONFIG_SIDECHAINCOMPRESS_FILTER)      += af_sidechaincompress.o
81 81
 OBJS-$(CONFIG_SILENCEDETECT_FILTER)          += af_silencedetect.o
82 82
 OBJS-$(CONFIG_SILENCEREMOVE_FILTER)          += af_silenceremove.o
83
+OBJS-$(CONFIG_STEREOTOOLS_FILTER)            += af_stereotools.o
83 84
 OBJS-$(CONFIG_STEREOWIDEN_FILTER)            += af_stereowiden.o
84 85
 OBJS-$(CONFIG_TREBLE_FILTER)                 += af_biquads.o
85 86
 OBJS-$(CONFIG_VOLUME_FILTER)                 += af_volume.o
86 87
new file mode 100644
... ...
@@ -0,0 +1,302 @@
0
+/*
1
+ * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen
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/channel_layout.h"
21
+#include "libavutil/opt.h"
22
+#include "avfilter.h"
23
+#include "audio.h"
24
+#include "formats.h"
25
+
26
+typedef struct StereoToolsContext {
27
+    const AVClass *class;
28
+
29
+    int softclip;
30
+    int mute_l;
31
+    int mute_r;
32
+    int phase_l;
33
+    int phase_r;
34
+    int mode;
35
+    double slev;
36
+    double sbal;
37
+    double mlev;
38
+    double mpan;
39
+    double phase;
40
+    double base;
41
+    double delay;
42
+    double balance_in;
43
+    double balance_out;
44
+    double phase_sin_coef;
45
+    double phase_cos_coef;
46
+    double sc_level;
47
+    double inv_atan_shape;
48
+    double level_in;
49
+    double level_out;
50
+
51
+    double *buffer;
52
+    int length;
53
+    int pos;
54
+} StereoToolsContext;
55
+
56
+#define OFFSET(x) offsetof(StereoToolsContext, x)
57
+#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58
+
59
+static const AVOption stereotools_options[] = {
60
+    { "level_in",    "set level in",     OFFSET(level_in),    AV_OPT_TYPE_DOUBLE, {.dbl=1},   0.015625,  64, A },
61
+    { "level_out",   "set level out",    OFFSET(level_out),   AV_OPT_TYPE_DOUBLE, {.dbl=1},   0.015625,  64, A },
62
+    { "balance_in",  "set balance in",   OFFSET(balance_in),  AV_OPT_TYPE_DOUBLE, {.dbl=0},  -1,          1, A },
63
+    { "balance_out", "set balance out",  OFFSET(balance_out), AV_OPT_TYPE_DOUBLE, {.dbl=0},  -1,          1, A },
64
+    { "softclip",    "enable softclip",  OFFSET(softclip),    AV_OPT_TYPE_BOOL,   {.i64=0},   0,          1, A },
65
+    { "mutel",       "mute L",           OFFSET(mute_l),      AV_OPT_TYPE_BOOL,   {.i64=0},   0,          1, A },
66
+    { "muter",       "mute R",           OFFSET(mute_r),      AV_OPT_TYPE_BOOL,   {.i64=0},   0,          1, A },
67
+    { "phasel",      "phase L",          OFFSET(phase_l),     AV_OPT_TYPE_BOOL,   {.i64=0},   0,          1, A },
68
+    { "phaser",      "phase R",          OFFSET(phase_r),     AV_OPT_TYPE_BOOL,   {.i64=0},   0,          1, A },
69
+    { "mode",        "set stereo mode",  OFFSET(mode),        AV_OPT_TYPE_INT,    {.i64=0},   0,          6, A, "mode" },
70
+    {     "lr>lr",   0,                  0,                   AV_OPT_TYPE_CONST,  {.i64=0},   0,          0, A, "mode" },
71
+    {     "lr>ms",   0,                  0,                   AV_OPT_TYPE_CONST,  {.i64=1},   0,          0, A, "mode" },
72
+    {     "ms>lr",   0,                  0,                   AV_OPT_TYPE_CONST,  {.i64=2},   0,          0, A, "mode" },
73
+    {     "lr>ll",   0,                  0,                   AV_OPT_TYPE_CONST,  {.i64=3},   0,          0, A, "mode" },
74
+    {     "lr>rr",   0,                  0,                   AV_OPT_TYPE_CONST,  {.i64=4},   0,          0, A, "mode" },
75
+    {     "lr>l+r",  0,                  0,                   AV_OPT_TYPE_CONST,  {.i64=5},   0,          0, A, "mode" },
76
+    {     "lr>rl",   0,                  0,                   AV_OPT_TYPE_CONST,  {.i64=6},   0,          0, A, "mode" },
77
+    { "slev",        "set side level",   OFFSET(slev),        AV_OPT_TYPE_DOUBLE, {.dbl=1},   0.015625,  64, A },
78
+    { "sbal",        "set side balance", OFFSET(sbal),        AV_OPT_TYPE_DOUBLE, {.dbl=0},  -1,          1, A },
79
+    { "mlev",        "set middle level", OFFSET(mlev),        AV_OPT_TYPE_DOUBLE, {.dbl=1},   0.015625,  64, A },
80
+    { "mpan",        "set middle pan",   OFFSET(mpan),        AV_OPT_TYPE_DOUBLE, {.dbl=0},  -1,          1, A },
81
+    { "base",        "set stereo base",  OFFSET(base),        AV_OPT_TYPE_DOUBLE, {.dbl=0},  -1,          1, A },
82
+    { "delay",       "set delay",        OFFSET(delay),       AV_OPT_TYPE_DOUBLE, {.dbl=0}, -20,         20, A },
83
+    { "sclevel",     "set S/C level",    OFFSET(sc_level),    AV_OPT_TYPE_DOUBLE, {.dbl=1},   1,        100, A },
84
+    { "phase",       "set stereo phase", OFFSET(phase),       AV_OPT_TYPE_DOUBLE, {.dbl=0},   0,        360, A },
85
+    { NULL }
86
+};
87
+
88
+AVFILTER_DEFINE_CLASS(stereotools);
89
+
90
+static int query_formats(AVFilterContext *ctx)
91
+{
92
+    AVFilterFormats *formats = NULL;
93
+    AVFilterChannelLayouts *layout = NULL;
94
+
95
+    ff_add_format(&formats, AV_SAMPLE_FMT_DBL);
96
+    ff_set_common_formats(ctx, formats);
97
+    ff_add_channel_layout(&layout, AV_CH_LAYOUT_STEREO);
98
+    ff_set_common_channel_layouts(ctx, layout);
99
+
100
+    formats = ff_all_samplerates();
101
+    if (!formats)
102
+        return AVERROR(ENOMEM);
103
+
104
+    return ff_set_common_samplerates(ctx, formats);
105
+}
106
+
107
+static int config_input(AVFilterLink *inlink)
108
+{
109
+    AVFilterContext *ctx = inlink->dst;
110
+    StereoToolsContext *s = ctx->priv;
111
+
112
+    s->length = 2 * inlink->sample_rate * 0.05;
113
+    s->buffer = av_calloc(s->length, sizeof(*s->buffer));
114
+    if (!s->buffer)
115
+        return AVERROR(ENOMEM);
116
+
117
+    s->inv_atan_shape = 1.0 / atan(s->sc_level);
118
+    s->phase_cos_coef = cos(s->phase / 180 * M_PI);
119
+    s->phase_sin_coef = sin(s->phase / 180 * M_PI);
120
+
121
+    return 0;
122
+}
123
+
124
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
125
+{
126
+    AVFilterContext *ctx = inlink->dst;
127
+    AVFilterLink *outlink = ctx->outputs[0];
128
+    StereoToolsContext *s = ctx->priv;
129
+    const double *src = (const double *)in->data[0];
130
+    const double sb = s->base < 0 ? s->base * 0.5 : s->base;
131
+    const double sbal = 1 + s->sbal;
132
+    const double mpan = 1 + s->mpan;
133
+    const double slev = s->slev;
134
+    const double mlev = s->mlev;
135
+    const double balance_in = s->balance_in;
136
+    const double balance_out = s->balance_out;
137
+    const double level_in = s->level_in;
138
+    const double level_out = s->level_out;
139
+    const double sc_level = s->sc_level;
140
+    const double delay = s->delay;
141
+    const int length = s->length;
142
+    const int mute_l = floor(s->mute_l + 0.5);
143
+    const int mute_r = floor(s->mute_r + 0.5);
144
+    const int phase_l = floor(s->phase_l + 0.5);
145
+    const int phase_r = floor(s->phase_r + 0.5);
146
+    double *buffer = s->buffer;
147
+    AVFrame *out = NULL;
148
+    double *dst;
149
+    int nbuf = inlink->sample_rate * (FFABS(delay) / 1000.);
150
+    int n;
151
+
152
+    nbuf -= nbuf % 2;
153
+    if (av_frame_is_writable(in)) {
154
+        out = in;
155
+    } else {
156
+        AVFrame *out = ff_get_audio_buffer(inlink, in->nb_samples);
157
+        if (!out) {
158
+            av_frame_free(&in);
159
+            return AVERROR(ENOMEM);
160
+        }
161
+        av_frame_copy_props(out, in);
162
+    }
163
+    dst = (double *)out->data[0];
164
+
165
+    for (n = 0; n < in->nb_samples; n++, src += 2, dst += 2) {
166
+        double L = src[0], R = src[1], l, r, m, S;
167
+
168
+        L *= level_in;
169
+        R *= level_in;
170
+
171
+        L *= 1. - FFMAX(0., balance_in);
172
+        R *= 1. + FFMIN(0., balance_in);
173
+
174
+        if (s->softclip) {
175
+            R = s->inv_atan_shape * atan(R * sc_level);
176
+            L = s->inv_atan_shape * atan(L * sc_level);
177
+        }
178
+
179
+        switch (s->mode) {
180
+        case 0:
181
+            m = (L + R) * 0.5;
182
+            S = (L - R) * 0.5;
183
+            l = m * mlev * FFMIN(1., 2. - mpan) + S * slev * FFMIN(1., 2. - sbal);
184
+            r = m * mlev * FFMIN(1., mpan)      - S * slev * FFMIN(1., sbal);
185
+            L = l;
186
+            R = r;
187
+            break;
188
+        case 1:
189
+            l = L * FFMIN(1., 2. - sbal);
190
+            r = R * FFMIN(1., sbal);
191
+            L = 0.5 * (l + r) * mlev;
192
+            R = 0.5 * (l - r) * slev;
193
+            break;
194
+        case 2:
195
+            l = L * mlev * FFMIN(1., 2. - mpan) + R * slev * FFMIN(1., 2. - sbal);
196
+            r = L * mlev * FFMIN(1., mpan)      - R * slev * FFMIN(1., sbal);
197
+            L = l;
198
+            R = r;
199
+            break;
200
+        case 3:
201
+            R = L;
202
+            break;
203
+        case 4:
204
+            L = R;
205
+            break;
206
+        case 5:
207
+            L = (L + R) / 2;
208
+            R = L;
209
+            break;
210
+        case 6:
211
+            l = L;
212
+            L = R;
213
+            R = l;
214
+            m = (L + R) * 0.5;
215
+            S = (L - R) * 0.5;
216
+            l = m * mlev * FFMIN(1., 2. - mpan) + S * slev * FFMIN(1., 2. - sbal);
217
+            r = m * mlev * FFMIN(1., mpan)      - S * slev * FFMIN(1., sbal);
218
+            L = l;
219
+            R = r;
220
+            break;
221
+        }
222
+
223
+        L *= 1. - mute_l;
224
+        R *= 1. - mute_r;
225
+
226
+        L *= (2. * (1. - phase_l)) - 1.;
227
+        R *= (2. * (1. - phase_r)) - 1.;
228
+
229
+        buffer[s->pos  ] = L;
230
+        buffer[s->pos+1] = R;
231
+
232
+        if (delay > 0.) {
233
+            R = buffer[(s->pos - (int)nbuf + 1 + length) % length];
234
+        } else if (delay < 0.) {
235
+            L = buffer[(s->pos - (int)nbuf + length)     % length];
236
+        }
237
+
238
+        l = L + sb * L - sb * R;
239
+        r = R + sb * R - sb * L;
240
+
241
+        L = l;
242
+        R = r;
243
+
244
+        l = L * s->phase_cos_coef - R * s->phase_sin_coef;
245
+        r = L * s->phase_sin_coef + R * s->phase_cos_coef;
246
+
247
+        L = l;
248
+        R = r;
249
+
250
+        s->pos = (s->pos + 2) % s->length;
251
+
252
+        L *= 1. - FFMAX(0., balance_out);
253
+        R *= 1. + FFMIN(0., balance_out);
254
+
255
+        L *= level_out;
256
+        R *= level_out;
257
+
258
+        dst[0] = L;
259
+        dst[1] = R;
260
+    }
261
+
262
+    if (out != in)
263
+        av_frame_free(&in);
264
+    return ff_filter_frame(outlink, out);
265
+}
266
+
267
+static av_cold void uninit(AVFilterContext *ctx)
268
+{
269
+    StereoToolsContext *s = ctx->priv;
270
+
271
+    av_freep(&s->buffer);
272
+}
273
+
274
+static const AVFilterPad inputs[] = {
275
+    {
276
+        .name         = "default",
277
+        .type         = AVMEDIA_TYPE_AUDIO,
278
+        .filter_frame = filter_frame,
279
+        .config_props = config_input,
280
+    },
281
+    { NULL }
282
+};
283
+
284
+static const AVFilterPad outputs[] = {
285
+    {
286
+        .name = "default",
287
+        .type = AVMEDIA_TYPE_AUDIO,
288
+    },
289
+    { NULL }
290
+};
291
+
292
+AVFilter ff_af_stereotools = {
293
+    .name           = "stereotools",
294
+    .description    = NULL_IF_CONFIG_SMALL("Apply various stereo tools."),
295
+    .query_formats  = query_formats,
296
+    .priv_size      = sizeof(StereoToolsContext),
297
+    .priv_class     = &stereotools_class,
298
+    .uninit         = uninit,
299
+    .inputs         = inputs,
300
+    .outputs        = outputs,
301
+};
... ...
@@ -102,6 +102,7 @@ void avfilter_register_all(void)
102 102
     REGISTER_FILTER(SIDECHAINCOMPRESS, sidechaincompress, af);
103 103
     REGISTER_FILTER(SILENCEDETECT,  silencedetect,  af);
104 104
     REGISTER_FILTER(SILENCEREMOVE,  silenceremove,  af);
105
+    REGISTER_FILTER(STEREOTOOLS,    stereotools,    af);
105 106
     REGISTER_FILTER(STEREOWIDEN,    stereowiden,    af);
106 107
     REGISTER_FILTER(TREBLE,         treble,         af);
107 108
     REGISTER_FILTER(VOLUME,         volume,         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   4
33
+#define LIBAVFILTER_VERSION_MINOR   5
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \