Browse code

lavfi: add earwax audio filter, ported from Sox

Signed-off-by: Stefano Sabatini <stefasab@gmail.com>

Mina Nagy Zaki authored on 2011/03/27 05:02:50
Showing 6 changed files
... ...
@@ -74,6 +74,7 @@ easier to use. The changes are:
74 74
 - replacement Indeo 3 decoder
75 75
 - new ffmpeg option: -map_channel
76 76
 - volume audio filter added
77
+- earwax audio filter added
77 78
 
78 79
 
79 80
 version 0.8:
... ...
@@ -224,6 +224,17 @@ expressed in the form "[@var{c0} @var{c1} @var{c2} @var{c3} @var{c4} @var{c5}
224 224
 @var{c6} @var{c7}]"
225 225
 @end table
226 226
 
227
+@section earwax
228
+
229
+Make audio easier to listen to on headphones.
230
+
231
+This filter adds `cues' to 44.1kHz stereo (i.e. audio CD format) audio
232
+so that when listened to on headphones the stereo image is moved from
233
+inside your head (standard for headphones) to outside and in front of
234
+the listener (standard for speakers).
235
+
236
+Ported from SoX.
237
+
227 238
 @section volume
228 239
 
229 240
 Adjust the input audio volume.
... ...
@@ -28,6 +28,7 @@ OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
28 28
 OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
29 29
 OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
30 30
 OBJS-$(CONFIG_ASHOWINFO_FILTER)              += af_ashowinfo.o
31
+OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
31 32
 OBJS-$(CONFIG_VOLUME_FILTER)                 += af_volume.o
32 33
 
33 34
 OBJS-$(CONFIG_ABUFFER_FILTER)                += asrc_abuffer.o
34 35
new file mode 100644
... ...
@@ -0,0 +1,161 @@
0
+/*
1
+ * Copyright (c) 2011 Mina Nagy Zaki
2
+ * Copyright (c) 2000 Edward Beingessner And Sundry Contributors.
3
+ * This source code is freely redistributable and may be used for any purpose.
4
+ * This copyright notice must be maintained.  Edward Beingessner And Sundry
5
+ * Contributors are not responsible for the consequences of using this
6
+ * software.
7
+ *
8
+ * This file is part of FFmpeg.
9
+ *
10
+ * FFmpeg is free software; you can redistribute it and/or
11
+ * modify it under the terms of the GNU Lesser General Public
12
+ * License as published by the Free Software Foundation; either
13
+ * version 2.1 of the License, or (at your option) any later version.
14
+ *
15
+ * FFmpeg is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
+ * Lesser General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU Lesser General Public
21
+ * License along with FFmpeg; if not, write to the Free Software
22
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
+ */
24
+
25
+/**
26
+ * @file
27
+ * Stereo Widening Effect. Adds audio cues to move stereo image in
28
+ * front of the listener. Adapted from the libsox earwax effect.
29
+ */
30
+
31
+#include "libavutil/audioconvert.h"
32
+#include "avfilter.h"
33
+
34
+#define NUMTAPS 64
35
+
36
+static const int8_t filt[NUMTAPS] = {
37
+/* 30°  330° */
38
+    4,   -6,     /* 32 tap stereo FIR filter. */
39
+    4,  -11,     /* One side filters as if the */
40
+   -1,   -5,     /* signal was from 30 degrees */
41
+    3,    3,     /* from the ear, the other as */
42
+   -2,    5,     /* if 330 degrees. */
43
+   -5,    0,
44
+    9,    1,
45
+    6,    3,     /*                         Input                         */
46
+   -4,   -1,     /*                   Left         Right                  */
47
+   -5,   -3,     /*                __________   __________                */
48
+   -2,   -5,     /*               |          | |          |               */
49
+   -7,    1,     /*           .---|  Hh,0(f) | |  Hh,0(f) |---.           */
50
+    6,   -7,     /*          /    |__________| |__________|    \          */
51
+   30,  -29,     /*         /                \ /                \         */
52
+   12,   -3,     /*        /                  X                  \        */
53
+  -11,    4,     /*       /                  / \                  \       */
54
+   -3,    7,     /*  ____V_____   __________V   V__________   _____V____  */
55
+  -20,   23,     /* |          | |          |   |          | |          | */
56
+    2,    0,     /* | Hh,30(f) | | Hh,330(f)|   | Hh,330(f)| | Hh,30(f) | */
57
+    1,   -6,     /* |__________| |__________|   |__________| |__________| */
58
+  -14,   -5,     /*      \     ___      /           \      ___     /      */
59
+   15,  -18,     /*       \   /   \    /    _____    \    /   \   /       */
60
+    6,    7,     /*        `->| + |<--'    /     \    `-->| + |<-'        */
61
+   15,  -10,     /*           \___/      _/       \_      \___/           */
62
+  -14,   22,     /*               \     / \       / \     /               */
63
+   -7,   -2,     /*                `--->| |       | |<---'                */
64
+   -4,    9,     /*                     \_/       \_/                     */
65
+    6,  -12,     /*                                                       */
66
+    6,   -6,     /*                       Headphones                      */
67
+    0,  -11,
68
+    0,   -5,
69
+    4,    0};
70
+
71
+typedef struct {
72
+    int16_t taps[NUMTAPS * 2];
73
+} EarwaxContext;
74
+
75
+static int query_formats(AVFilterContext *ctx)
76
+{
77
+    AVFilterFormats *formats = NULL;
78
+    avfilter_add_format(&formats, AV_SAMPLE_FMT_S16);
79
+    avfilter_set_common_sample_formats(ctx, formats);
80
+    formats = NULL;
81
+    avfilter_add_format(&formats, AV_CH_LAYOUT_STEREO);
82
+    avfilter_set_common_channel_layouts(ctx, formats);
83
+    formats = NULL;
84
+    avfilter_add_format(&formats, AVFILTER_PACKED);
85
+    avfilter_set_common_packing_formats(ctx, formats);
86
+
87
+    return 0;
88
+}
89
+
90
+static int config_input(AVFilterLink *inlink)
91
+{
92
+    if (inlink->sample_rate != 44100) {
93
+        av_log(inlink->dst, AV_LOG_ERROR,
94
+               "The earwax filter only works for 44.1kHz audio. Insert "
95
+               "a resample filter before this\n");
96
+        return AVERROR(EINVAL);
97
+    }
98
+    return 0;
99
+}
100
+
101
+//FIXME: replace with DSPContext.scalarproduct_int16
102
+static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, int16_t *out)
103
+{
104
+    int32_t sample;
105
+    int16_t j;
106
+
107
+    while (in < endin) {
108
+        sample = 32;
109
+        for (j = 0; j < NUMTAPS; j++)
110
+            sample += in[j] * filt[j];
111
+        *out = sample >> 6;
112
+        out++;
113
+        in++;
114
+    }
115
+
116
+    return out;
117
+}
118
+
119
+static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
120
+{
121
+    AVFilterLink *outlink = inlink->dst->outputs[0];
122
+    int16_t *taps, *endin, *in, *out;
123
+    AVFilterBufferRef *outsamples =
124
+        avfilter_get_audio_buffer(inlink, AV_PERM_WRITE,
125
+                                  insamples->audio->nb_samples);
126
+    taps  = ((EarwaxContext *)inlink->dst->priv)->taps;
127
+    out   = (int16_t *)outsamples->data[0];
128
+    in    = (int16_t *)insamples ->data[0];
129
+
130
+    // copy part of new input and process with saved input
131
+    memcpy(taps+NUMTAPS, in, NUMTAPS * sizeof(*taps));
132
+    out   = scalarproduct(taps, taps + NUMTAPS, out);
133
+
134
+    // process current input
135
+    endin = in + insamples->audio->nb_samples * 2 - NUMTAPS;
136
+    out   = scalarproduct(in, endin, out);
137
+
138
+    // save part of input for next round
139
+    memcpy(taps, endin, NUMTAPS * sizeof(*taps));
140
+
141
+    avfilter_filter_samples(outlink, outsamples);
142
+    avfilter_unref_buffer(insamples);
143
+}
144
+
145
+AVFilter avfilter_af_earwax = {
146
+    .name           = "earwax",
147
+    .description    = NULL_IF_CONFIG_SMALL("Widen the stereo image."),
148
+    .query_formats  = query_formats,
149
+    .priv_size      = sizeof(EarwaxContext),
150
+    .inputs  = (AVFilterPad[])  {{  .name           = "default",
151
+                                    .type           = AVMEDIA_TYPE_AUDIO,
152
+                                    .filter_samples = filter_samples,
153
+                                    .config_props   = config_input,
154
+                                    .min_perms      = AV_PERM_READ, },
155
+                                 {  .name = NULL}},
156
+
157
+    .outputs = (AVFilterPad[])  {{  .name           = "default",
158
+                                    .type           = AVMEDIA_TYPE_AUDIO, },
159
+                                 {  .name = NULL}},
160
+};
... ...
@@ -39,6 +39,7 @@ void avfilter_register_all(void)
39 39
     REGISTER_FILTER (ANULL,       anull,       af);
40 40
     REGISTER_FILTER (ARESAMPLE,   aresample,   af);
41 41
     REGISTER_FILTER (ASHOWINFO,   ashowinfo,   af);
42
+    REGISTER_FILTER (EARWAX,      earwax,      af);
42 43
     REGISTER_FILTER (VOLUME,      volume,      af);
43 44
 
44 45
     REGISTER_FILTER (ABUFFER,     abuffer,     asrc);
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/rational.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  2
32
-#define LIBAVFILTER_VERSION_MINOR 46
32
+#define LIBAVFILTER_VERSION_MINOR 47
33 33
 #define LIBAVFILTER_VERSION_MICRO  0
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \