Browse code

avfilter: add acontrast filter

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

Paul B Mahol authored on 2017/11/18 18:28:27
Showing 6 changed files
... ...
@@ -16,6 +16,7 @@ version <next>:
16 16
 - NVIDIA NVDEC-accelerated H.264, HEVC, MPEG-2, VC1 and VP9 hwaccel decoding
17 17
 - Intel QSV-accelerated overlay filter
18 18
 - mcompand audio filter
19
+- acontrast audio filter
19 20
 
20 21
 
21 22
 version 3.4:
... ...
@@ -429,6 +429,16 @@ How much to use compressed signal in output. Default is 1.
429 429
 Range is between 0 and 1.
430 430
 @end table
431 431
 
432
+@section acontrast
433
+Simple audio dynamic range commpression/expansion filter.
434
+
435
+The filter accepts the following options:
436
+
437
+@table @option
438
+@item contrast
439
+Set contrast. Default is 33. Allowed range is between 0 and 100.
440
+@end table
441
+
432 442
 @section acopy
433 443
 
434 444
 Copy the input audio source unchanged to the output. This is mainly useful for
... ...
@@ -31,6 +31,7 @@ OBJS-$(CONFIG_QSVVPP)                        += qsvvpp.o
31 31
 # audio filters
32 32
 OBJS-$(CONFIG_ABENCH_FILTER)                 += f_bench.o
33 33
 OBJS-$(CONFIG_ACOMPRESSOR_FILTER)            += af_sidechaincompress.o
34
+OBJS-$(CONFIG_ACONTRAST_FILTER)              += af_acontrast.o
34 35
 OBJS-$(CONFIG_ACOPY_FILTER)                  += af_acopy.o
35 36
 OBJS-$(CONFIG_ACROSSFADE_FILTER)             += af_afade.o
36 37
 OBJS-$(CONFIG_ACRUSHER_FILTER)               += af_acrusher.o
37 38
new file mode 100644
... ...
@@ -0,0 +1,219 @@
0
+/*
1
+ * Copyright (c) 2008 Rob Sykes
2
+ * Copyright (c) 2017 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/channel_layout.h"
22
+#include "libavutil/opt.h"
23
+#include "avfilter.h"
24
+#include "audio.h"
25
+#include "formats.h"
26
+
27
+typedef struct AudioContrastContext {
28
+    const AVClass *class;
29
+    float contrast;
30
+    void (*filter)(void **dst, const void **src,
31
+                   int nb_samples, int channels, float contrast);
32
+} AudioContrastContext;
33
+
34
+#define OFFSET(x) offsetof(AudioContrastContext, x)
35
+#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
36
+
37
+static const AVOption acontrast_options[] = {
38
+    { "contrast", "set contrast", OFFSET(contrast), AV_OPT_TYPE_FLOAT, {.dbl=33}, 0, 100, A },
39
+    { NULL }
40
+};
41
+
42
+AVFILTER_DEFINE_CLASS(acontrast);
43
+
44
+static int query_formats(AVFilterContext *ctx)
45
+{
46
+    AVFilterFormats *formats = NULL;
47
+    AVFilterChannelLayouts *layouts = NULL;
48
+    static const enum AVSampleFormat sample_fmts[] = {
49
+        AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
50
+        AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
51
+        AV_SAMPLE_FMT_NONE
52
+    };
53
+    int ret;
54
+
55
+    formats = ff_make_format_list(sample_fmts);
56
+    if (!formats)
57
+        return AVERROR(ENOMEM);
58
+    ret = ff_set_common_formats(ctx, formats);
59
+    if (ret < 0)
60
+        return ret;
61
+
62
+    layouts = ff_all_channel_counts();
63
+    if (!layouts)
64
+        return AVERROR(ENOMEM);
65
+
66
+    ret = ff_set_common_channel_layouts(ctx, layouts);
67
+    if (ret < 0)
68
+        return ret;
69
+
70
+    formats = ff_all_samplerates();
71
+    return ff_set_common_samplerates(ctx, formats);
72
+}
73
+
74
+static void filter_flt(void **d, const void **s,
75
+                       int nb_samples, int channels,
76
+                       float contrast)
77
+{
78
+    const float *src = s[0];
79
+    float *dst = d[0];
80
+    int n, c;
81
+
82
+    for (n = 0; n < nb_samples; n++) {
83
+        for (c = 0; c < channels; c++) {
84
+            float d = src[c] * M_PI_2;
85
+
86
+            dst[c] = sinf(d + contrast * sinf(d * 4));
87
+        }
88
+
89
+        dst += c;
90
+        src += c;
91
+    }
92
+}
93
+
94
+static void filter_dbl(void **d, const void **s,
95
+                       int nb_samples, int channels,
96
+                       float contrast)
97
+{
98
+    const double *src = s[0];
99
+    double *dst = d[0];
100
+    int n, c;
101
+
102
+    for (n = 0; n < nb_samples; n++) {
103
+        for (c = 0; c < channels; c++) {
104
+            double d = src[c] * M_PI_2;
105
+
106
+            dst[c] = sin(d + contrast * sin(d * 4));
107
+        }
108
+
109
+        dst += c;
110
+        src += c;
111
+    }
112
+}
113
+
114
+static void filter_fltp(void **d, const void **s,
115
+                        int nb_samples, int channels,
116
+                        float contrast)
117
+{
118
+    int n, c;
119
+
120
+    for (c = 0; c < channels; c++) {
121
+        const float *src = s[c];
122
+        float *dst = d[c];
123
+
124
+        for (n = 0; n < nb_samples; n++) {
125
+            float d = src[n] * M_PI_2;
126
+
127
+            dst[n] = sinf(d + contrast * sinf(d * 4));
128
+        }
129
+    }
130
+}
131
+
132
+static void filter_dblp(void **d, const void **s,
133
+                        int nb_samples, int channels,
134
+                        float contrast)
135
+{
136
+    int n, c;
137
+
138
+    for (c = 0; c < channels; c++) {
139
+        const double *src = s[c];
140
+        double *dst = d[c];
141
+
142
+        for (n = 0; n < nb_samples; n++) {
143
+            double d = src[n] * M_PI_2;
144
+
145
+            dst[n] = sin(d + contrast * sin(d * 4));
146
+        }
147
+    }
148
+}
149
+
150
+static int config_input(AVFilterLink *inlink)
151
+{
152
+    AVFilterContext *ctx = inlink->dst;
153
+    AudioContrastContext *s    = ctx->priv;
154
+
155
+    switch (inlink->format) {
156
+    case AV_SAMPLE_FMT_FLT:  s->filter = filter_flt;  break;
157
+    case AV_SAMPLE_FMT_DBL:  s->filter = filter_dbl;  break;
158
+    case AV_SAMPLE_FMT_FLTP: s->filter = filter_fltp; break;
159
+    case AV_SAMPLE_FMT_DBLP: s->filter = filter_dblp; break;
160
+    }
161
+
162
+    return 0;
163
+}
164
+
165
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
166
+{
167
+    AVFilterContext *ctx = inlink->dst;
168
+    AVFilterLink *outlink = ctx->outputs[0];
169
+    AudioContrastContext *s = ctx->priv;
170
+    AVFrame *out;
171
+
172
+    if (av_frame_is_writable(in)) {
173
+        out = in;
174
+    } else {
175
+        out = ff_get_audio_buffer(inlink, in->nb_samples);
176
+        if (!out) {
177
+            av_frame_free(&in);
178
+            return AVERROR(ENOMEM);
179
+        }
180
+        av_frame_copy_props(out, in);
181
+    }
182
+
183
+    s->filter((void **)out->extended_data, (const void **)in->extended_data,
184
+              in->nb_samples, in->channels, s->contrast / 750);
185
+
186
+    if (out != in)
187
+        av_frame_free(&in);
188
+
189
+    return ff_filter_frame(outlink, out);
190
+}
191
+
192
+static const AVFilterPad inputs[] = {
193
+    {
194
+        .name         = "default",
195
+        .type         = AVMEDIA_TYPE_AUDIO,
196
+        .filter_frame = filter_frame,
197
+        .config_props = config_input,
198
+    },
199
+    { NULL }
200
+};
201
+
202
+static const AVFilterPad outputs[] = {
203
+    {
204
+        .name = "default",
205
+        .type = AVMEDIA_TYPE_AUDIO,
206
+    },
207
+    { NULL }
208
+};
209
+
210
+AVFilter ff_af_acontrast = {
211
+    .name           = "acontrast",
212
+    .description    = NULL_IF_CONFIG_SMALL("Simple audio dynamic range compression/expansion filter."),
213
+    .query_formats  = query_formats,
214
+    .priv_size      = sizeof(AudioContrastContext),
215
+    .priv_class     = &acontrast_class,
216
+    .inputs         = inputs,
217
+    .outputs        = outputs,
218
+};
... ...
@@ -42,6 +42,7 @@ static void register_all(void)
42 42
 {
43 43
     REGISTER_FILTER(ABENCH,         abench,         af);
44 44
     REGISTER_FILTER(ACOMPRESSOR,    acompressor,    af);
45
+    REGISTER_FILTER(ACONTRAST,      acontrast,      af);
45 46
     REGISTER_FILTER(ACOPY,          acopy,          af);
46 47
     REGISTER_FILTER(ACROSSFADE,     acrossfade,     af);
47 48
     REGISTER_FILTER(ACRUSHER,       acrusher,       af);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   7
33
-#define LIBAVFILTER_VERSION_MINOR   1
33
+#define LIBAVFILTER_VERSION_MINOR   2
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \