Browse code

avfilter: add crystalizer audio filter

Signed-off-by: Vitaly Ostrosablin <tmp6154@yandex.ru>

Vitaly Ostrosablin authored on 2016/08/06 16:50:35
Showing 6 changed files
... ...
@@ -13,6 +13,7 @@ version <next>:
13 13
 - OpenH264 decoder wrapper
14 14
 - MediaCodec hwaccel
15 15
 - True Audio (TTA) muxer
16
+- crystalizer audio filter
16 17
 
17 18
 
18 19
 version 3.1:
... ...
@@ -2110,6 +2110,20 @@ Set temperature degree in Celsius. This is the temperature of the environment.
2110 2110
 Default is 20.
2111 2111
 @end table
2112 2112
 
2113
+@section crystalizer
2114
+Simple algorithm to expand audio dynamic range.
2115
+
2116
+The filter accepts the following options:
2117
+
2118
+@table @option
2119
+@item i
2120
+Sets the intensity of effect (default: 2.0). Must be in range between 0.0
2121
+(unchanged sound) to 10.0 (maximum effect).
2122
+
2123
+@item c
2124
+Enable clipping. By default is enabled.
2125
+@end table
2126
+
2113 2127
 @section dcshift
2114 2128
 Apply a DC shift to the audio.
2115 2129
 
... ...
@@ -78,6 +78,7 @@ OBJS-$(CONFIG_CHANNELSPLIT_FILTER)           += af_channelsplit.o
78 78
 OBJS-$(CONFIG_CHORUS_FILTER)                 += af_chorus.o generate_wave_table.o
79 79
 OBJS-$(CONFIG_COMPAND_FILTER)                += af_compand.o
80 80
 OBJS-$(CONFIG_COMPENSATIONDELAY_FILTER)      += af_compensationdelay.o
81
+OBJS-$(CONFIG_CRYSTALIZER_FILTER)            += af_crystalizer.o
81 82
 OBJS-$(CONFIG_DCSHIFT_FILTER)                += af_dcshift.o
82 83
 OBJS-$(CONFIG_DYNAUDNORM_FILTER)             += af_dynaudnorm.o
83 84
 OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
84 85
new file mode 100644
... ...
@@ -0,0 +1,153 @@
0
+/*
1
+ * Copyright (c) 2016 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 CrystalizerContext {
27
+    const AVClass *class;
28
+    float mult;
29
+    int clip;
30
+    AVFrame *prev;
31
+} CrystalizerContext;
32
+
33
+#define OFFSET(x) offsetof(CrystalizerContext, x)
34
+#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
35
+
36
+static const AVOption crystalizer_options[] = {
37
+    { "i", "set intensity",    OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.0}, 0, 10, A },
38
+    { "c", "enable clipping",  OFFSET(clip), AV_OPT_TYPE_BOOL,  {.i64=1},   0,  1, A },
39
+    { NULL }
40
+};
41
+
42
+AVFILTER_DEFINE_CLASS(crystalizer);
43
+
44
+static int query_formats(AVFilterContext *ctx)
45
+{
46
+    AVFilterFormats *formats = NULL;
47
+    AVFilterChannelLayouts *layouts = NULL;
48
+    int ret;
49
+
50
+    if ((ret = ff_add_format        (&formats, AV_SAMPLE_FMT_FLT )) < 0 ||
51
+        (ret = ff_set_common_formats(ctx     , formats           )) < 0)
52
+        return ret;
53
+
54
+    layouts = ff_all_channel_counts();
55
+    if (!layouts)
56
+        return AVERROR(ENOMEM);
57
+
58
+    ret = ff_set_common_channel_layouts(ctx, layouts);
59
+    if (ret < 0)
60
+        return ret;
61
+
62
+    formats = ff_all_samplerates();
63
+    return ff_set_common_samplerates(ctx, formats);
64
+}
65
+
66
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
67
+{
68
+    AVFilterContext *ctx = inlink->dst;
69
+    AVFilterLink *outlink = ctx->outputs[0];
70
+    CrystalizerContext *s = ctx->priv;
71
+    const float *src = (const float *)in->data[0];
72
+    const float mult = s->mult;
73
+    AVFrame *out;
74
+    float *dst, *prv;
75
+    int n, c;
76
+
77
+    if (!s->prev) {
78
+        s->prev = ff_get_audio_buffer(inlink, 1);
79
+        if (!s->prev) {
80
+            av_frame_free(&in);
81
+            return AVERROR(ENOMEM);
82
+        }
83
+    }
84
+
85
+    if (av_frame_is_writable(in)) {
86
+        out = in;
87
+    } else {
88
+        out = ff_get_audio_buffer(inlink, in->nb_samples);
89
+        if (!out) {
90
+            av_frame_free(&in);
91
+            return AVERROR(ENOMEM);
92
+        }
93
+        av_frame_copy_props(out, in);
94
+    }
95
+
96
+    dst = (float *)out->data[0];
97
+    prv = (float *)s->prev->data[0];
98
+
99
+    for (n = 0; n < in->nb_samples; n++) {
100
+        for (c = 0; c < in->channels; c++) {
101
+            float current = src[c];
102
+
103
+            dst[c] = current + (current - prv[c]) * mult;
104
+            prv[c] = current;
105
+            if (s->clip) {
106
+                dst[c] = av_clipf(dst[c], -1, 1);
107
+            }
108
+        }
109
+        dst += c;
110
+        src += c;
111
+    }
112
+
113
+    if (out != in)
114
+        av_frame_free(&in);
115
+
116
+    return ff_filter_frame(outlink, out);
117
+}
118
+
119
+static av_cold void uninit(AVFilterContext *ctx)
120
+{
121
+    CrystalizerContext *s = ctx->priv;
122
+
123
+    av_frame_free(&s->prev);
124
+}
125
+
126
+static const AVFilterPad inputs[] = {
127
+    {
128
+        .name         = "default",
129
+        .type         = AVMEDIA_TYPE_AUDIO,
130
+        .filter_frame = filter_frame,
131
+    },
132
+    { NULL }
133
+};
134
+
135
+static const AVFilterPad outputs[] = {
136
+    {
137
+        .name = "default",
138
+        .type = AVMEDIA_TYPE_AUDIO,
139
+    },
140
+    { NULL }
141
+};
142
+
143
+AVFilter ff_af_crystalizer = {
144
+    .name           = "crystalizer",
145
+    .description    = NULL_IF_CONFIG_SMALL("Simple expand audio dynamic range filter."),
146
+    .query_formats  = query_formats,
147
+    .priv_size      = sizeof(CrystalizerContext),
148
+    .priv_class     = &crystalizer_class,
149
+    .uninit         = uninit,
150
+    .inputs         = inputs,
151
+    .outputs        = outputs,
152
+};
... ...
@@ -96,6 +96,7 @@ void avfilter_register_all(void)
96 96
     REGISTER_FILTER(CHORUS,         chorus,         af);
97 97
     REGISTER_FILTER(COMPAND,        compand,        af);
98 98
     REGISTER_FILTER(COMPENSATIONDELAY, compensationdelay, af);
99
+    REGISTER_FILTER(CRYSTALIZER,    crystalizer,    af);
99 100
     REGISTER_FILTER(DCSHIFT,        dcshift,        af);
100 101
     REGISTER_FILTER(DYNAUDNORM,     dynaudnorm,     af);
101 102
     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  49
33
+#define LIBAVFILTER_VERSION_MINOR  50
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \