Browse code

avfilter: add extrastereo filter

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

Paul B Mahol authored on 2015/09/05 12:55:47
Showing 6 changed files
... ...
@@ -3,6 +3,7 @@ releases are sorted from youngest to oldest.
3 3
 
4 4
 version <next>:
5 5
 - DXV decoding
6
+- extrastereo filter
6 7
 
7 8
 
8 9
 version 2.8:
... ...
@@ -1817,6 +1817,23 @@ equalizer=f=1000:width_type=q:width=1:g=2,equalizer=f=100:width_type=q:width=2:g
1817 1817
 @end example
1818 1818
 @end itemize
1819 1819
 
1820
+@section extrastereo
1821
+
1822
+Linearly increases the difference between left and right channels which
1823
+adds some sort of "live" effect to playback.
1824
+
1825
+The filter accepts the following option:
1826
+
1827
+@table @option
1828
+@item m
1829
+Sets the difference coefficient (default: 2.5). 0.0 means mono sound
1830
+(average of both channels), with 1.0 sound will be unchanged, with
1831
+-1.0 left and right channels will be swapped.
1832
+
1833
+@item c
1834
+Enable clipping. By default is enabled.
1835
+@end table
1836
+
1820 1837
 @section flanger
1821 1838
 Apply a flanging effect to the audio.
1822 1839
 
... ...
@@ -67,6 +67,7 @@ OBJS-$(CONFIG_DYNAUDNORM_FILTER)             += af_dynaudnorm.o
67 67
 OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
68 68
 OBJS-$(CONFIG_EBUR128_FILTER)                += f_ebur128.o
69 69
 OBJS-$(CONFIG_EQUALIZER_FILTER)              += af_biquads.o
70
+OBJS-$(CONFIG_EXTRASTEREO_FILTER)            += af_extrastereo.o
70 71
 OBJS-$(CONFIG_FLANGER_FILTER)                += af_flanger.o generate_wave_table.o
71 72
 OBJS-$(CONFIG_HIGHPASS_FILTER)               += af_biquads.o
72 73
 OBJS-$(CONFIG_JOIN_FILTER)                   += af_join.o
73 74
new file mode 100644
... ...
@@ -0,0 +1,128 @@
0
+/*
1
+ * Copyright (c) 2015 The FFmpeg Project
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 ExtraStereoContext {
27
+    const AVClass *class;
28
+    float mult;
29
+    int clip;
30
+} ExtraStereoContext;
31
+
32
+#define OFFSET(x) offsetof(ExtraStereoContext, x)
33
+#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
34
+
35
+static const AVOption extrastereo_options[] = {
36
+    { "m", "set the difference coefficient", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.5}, -10, 10, A },
37
+    { "c", "enable clipping",                OFFSET(clip), AV_OPT_TYPE_INT,   {.i64=1},     0,  1, A },
38
+    { NULL }
39
+};
40
+
41
+AVFILTER_DEFINE_CLASS(extrastereo);
42
+
43
+static int query_formats(AVFilterContext *ctx)
44
+{
45
+    AVFilterFormats *formats = NULL;
46
+    AVFilterChannelLayouts *layout = NULL;
47
+
48
+    ff_add_format(&formats, AV_SAMPLE_FMT_FLT);
49
+    ff_set_common_formats(ctx, formats);
50
+    ff_add_channel_layout(&layout, AV_CH_LAYOUT_STEREO);
51
+    ff_set_common_channel_layouts(ctx, layout);
52
+
53
+    formats = ff_all_samplerates();
54
+    if (!formats)
55
+        return AVERROR(ENOMEM);
56
+    return ff_set_common_samplerates(ctx, formats);
57
+}
58
+
59
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
60
+{
61
+    AVFilterContext *ctx = inlink->dst;
62
+    AVFilterLink *outlink = ctx->outputs[0];
63
+    ExtraStereoContext *s = ctx->priv;
64
+    const float *src = (const float *)in->data[0];
65
+    const float mult = s->mult;
66
+    AVFrame *out = NULL;
67
+    float *dst;
68
+    int n;
69
+
70
+    if (av_frame_is_writable(in)) {
71
+        out = in;
72
+    } else {
73
+        AVFrame *out = ff_get_audio_buffer(inlink, in->nb_samples);
74
+        if (!out) {
75
+            av_frame_free(&in);
76
+            return AVERROR(ENOMEM);
77
+        }
78
+        av_frame_copy_props(out, in);
79
+    }
80
+    dst = (float *)out->data[0];
81
+
82
+    for (n = 0; n < in->nb_samples; n++) {
83
+        float average, left, right;
84
+
85
+        left    = src[n * 2    ];
86
+        right   = src[n * 2 + 1];
87
+        average = (left + right) / 2.;
88
+        left    = average + mult * (left  - average);
89
+        right   = average + mult * (right - average);
90
+
91
+        if (s->clip) {
92
+            dst[n * 2    ] = av_clipf(left,  -1, 1);
93
+            dst[n * 2 + 1] = av_clipf(right, -1, 1);
94
+        }
95
+    }
96
+
97
+    if (out != in)
98
+        av_frame_free(&in);
99
+    return ff_filter_frame(outlink, out);
100
+}
101
+
102
+static const AVFilterPad inputs[] = {
103
+    {
104
+        .name         = "default",
105
+        .type         = AVMEDIA_TYPE_AUDIO,
106
+        .filter_frame = filter_frame,
107
+    },
108
+    { NULL }
109
+};
110
+
111
+static const AVFilterPad outputs[] = {
112
+    {
113
+        .name = "default",
114
+        .type = AVMEDIA_TYPE_AUDIO,
115
+    },
116
+    { NULL }
117
+};
118
+
119
+AVFilter ff_af_extrastereo = {
120
+    .name           = "extrastereo",
121
+    .description    = NULL_IF_CONFIG_SMALL("Increase difference between stereo audio channels."),
122
+    .query_formats  = query_formats,
123
+    .priv_size      = sizeof(ExtraStereoContext),
124
+    .priv_class     = &extrastereo_class,
125
+    .inputs         = inputs,
126
+    .outputs        = outputs,
127
+};
... ...
@@ -89,6 +89,7 @@ void avfilter_register_all(void)
89 89
     REGISTER_FILTER(EARWAX,         earwax,         af);
90 90
     REGISTER_FILTER(EBUR128,        ebur128,        af);
91 91
     REGISTER_FILTER(EQUALIZER,      equalizer,      af);
92
+    REGISTER_FILTER(EXTRASTEREO,    extrastereo,    af);
92 93
     REGISTER_FILTER(FLANGER,        flanger,        af);
93 94
     REGISTER_FILTER(HIGHPASS,       highpass,       af);
94 95
     REGISTER_FILTER(JOIN,           join,           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   0
33
+#define LIBAVFILTER_VERSION_MINOR   1
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \