Browse code

avfilter: add showvolume filter

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

Paul B Mahol authored on 2015/02/20 19:28:36
Showing 6 changed files
... ...
@@ -9,6 +9,7 @@ version <next>:
9 9
 - DirectDraw Surface image/texture decoder
10 10
 - ssim filter
11 11
 - rewritten ASF demuxer
12
+- showvolume filter
12 13
 
13 14
 
14 15
 version 2.7:
... ...
@@ -11992,6 +11992,39 @@ ffplay -f lavfi 'amovie=input.mp3, asplit [a][out1];
11992 11992
 @end example
11993 11993
 @end itemize
11994 11994
 
11995
+@section showvolume
11996
+
11997
+Convert input audio volume to a video output.
11998
+
11999
+The filter accepts the following options:
12000
+
12001
+@table @option
12002
+@item rate, r
12003
+Set video rate.
12004
+
12005
+@item b
12006
+Set border width, allowed range is [0, 5]. Default is 1.
12007
+
12008
+@item w
12009
+Set channel width, allowed range is [40, 1080]. Default is 400.
12010
+
12011
+@item h
12012
+Set channel height, allowed range is [1, 100]. Default is 20.
12013
+
12014
+@item f
12015
+Set fade, allowed range is [1, 255]. Default is 20.
12016
+
12017
+@item c
12018
+Set volume color expression.
12019
+
12020
+The expression can use the following variables:
12021
+
12022
+@table @option
12023
+@item VOLUME
12024
+Current max volume of channel in dB.
12025
+@end table
12026
+@end table
12027
+
11995 12028
 @section showwaves
11996 12029
 
11997 12030
 Convert input audio to a video output, representing the samples waves.
... ...
@@ -241,6 +241,7 @@ OBJS-$(CONFIG_AVECTORSCOPE_FILTER)           += avf_avectorscope.o
241 241
 OBJS-$(CONFIG_CONCAT_FILTER)                 += avf_concat.o
242 242
 OBJS-$(CONFIG_SHOWCQT_FILTER)                += avf_showcqt.o
243 243
 OBJS-$(CONFIG_SHOWSPECTRUM_FILTER)           += avf_showspectrum.o
244
+OBJS-$(CONFIG_SHOWVOLUME_FILTER)             += avf_showvolume.o
244 245
 OBJS-$(CONFIG_SHOWWAVES_FILTER)              += avf_showwaves.o
245 246
 OBJS-$(CONFIG_SHOWWAVESPIC_FILTER)           += avf_showwaves.o
246 247
 
... ...
@@ -256,6 +256,7 @@ void avfilter_register_all(void)
256 256
     REGISTER_FILTER(CONCAT,         concat,         avf);
257 257
     REGISTER_FILTER(SHOWCQT,        showcqt,        avf);
258 258
     REGISTER_FILTER(SHOWSPECTRUM,   showspectrum,   avf);
259
+    REGISTER_FILTER(SHOWVOLUME,     showvolume,     avf);
259 260
     REGISTER_FILTER(SHOWWAVES,      showwaves,      avf);
260 261
     REGISTER_FILTER(SHOWWAVESPIC,   showwavespic,   avf);
261 262
 
262 263
new file mode 100644
... ...
@@ -0,0 +1,230 @@
0
+/*
1
+ * Copyright (c) 2015 Paul B Mahol
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/eval.h"
22
+#include "libavutil/intreadwrite.h"
23
+#include "libavutil/opt.h"
24
+#include "libavutil/parseutils.h"
25
+#include "avfilter.h"
26
+#include "formats.h"
27
+#include "audio.h"
28
+#include "video.h"
29
+#include "internal.h"
30
+
31
+typedef struct ShowVolumeContext {
32
+    const AVClass *class;
33
+    int w, h;
34
+    int f, b;
35
+    AVRational frame_rate;
36
+    char *color;
37
+
38
+    AVFrame *out;
39
+    AVExpr *c_expr;
40
+} ShowVolumeContext;
41
+
42
+#define OFFSET(x) offsetof(ShowVolumeContext, x)
43
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
44
+
45
+static const AVOption showvolume_options[] = {
46
+    { "rate", "set video rate",  OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
47
+    { "r",    "set video rate",  OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
48
+    { "b", "set border width",   OFFSET(b), AV_OPT_TYPE_INT, {.i64=1}, 0, 5, FLAGS },
49
+    { "w", "set channel width",  OFFSET(w), AV_OPT_TYPE_INT, {.i64=400}, 40, 1080, FLAGS },
50
+    { "h", "set channel height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=20}, 1, 100, FLAGS },
51
+    { "f", "set fade",           OFFSET(f), AV_OPT_TYPE_INT, {.i64=20}, 1, 255, FLAGS },
52
+    { "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="if(gte(VOLUME,-2), if(gte(VOLUME,-1),0xff, 0xffff),0xff00)"}, 0, 0, FLAGS },
53
+    { NULL }
54
+};
55
+
56
+AVFILTER_DEFINE_CLASS(showvolume);
57
+
58
+static const char *const var_names[] = {   "VOLUME", NULL };
59
+enum                                   { VAR_VOLUME, VAR_VARS_NB };
60
+
61
+static av_cold int init(AVFilterContext *ctx)
62
+{
63
+    ShowVolumeContext *s = ctx->priv;
64
+    int ret;
65
+
66
+    if (s->color) {
67
+        ret = av_expr_parse(&s->c_expr, s->color, var_names,
68
+                            NULL, NULL, NULL, NULL, 0, ctx);
69
+        if (ret < 0)
70
+            return ret;
71
+    }
72
+
73
+    return 0;
74
+}
75
+
76
+static int query_formats(AVFilterContext *ctx)
77
+{
78
+    AVFilterFormats *formats = NULL;
79
+    AVFilterChannelLayouts *layouts = NULL;
80
+    AVFilterLink *inlink = ctx->inputs[0];
81
+    AVFilterLink *outlink = ctx->outputs[0];
82
+    static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
83
+    static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
84
+
85
+    formats = ff_make_format_list(sample_fmts);
86
+    if (!formats)
87
+        return AVERROR(ENOMEM);
88
+    ff_formats_ref(formats, &inlink->out_formats);
89
+
90
+    layouts = ff_all_channel_layouts();
91
+    if (!layouts)
92
+        return AVERROR(ENOMEM);
93
+    ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
94
+
95
+    formats = ff_all_samplerates();
96
+    if (!formats)
97
+        return AVERROR(ENOMEM);
98
+    ff_formats_ref(formats, &inlink->out_samplerates);
99
+
100
+    formats = ff_make_format_list(pix_fmts);
101
+    if (!formats)
102
+        return AVERROR(ENOMEM);
103
+    ff_formats_ref(formats, &outlink->in_formats);
104
+
105
+    return 0;
106
+}
107
+
108
+static int config_input(AVFilterLink *inlink)
109
+{
110
+    AVFilterContext *ctx = inlink->dst;
111
+    ShowVolumeContext *s = ctx->priv;
112
+    int nb_samples;
113
+
114
+    nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
115
+    inlink->partial_buf_size =
116
+    inlink->min_samples =
117
+    inlink->max_samples = nb_samples;
118
+
119
+    return 0;
120
+}
121
+
122
+static int config_output(AVFilterLink *outlink)
123
+{
124
+    ShowVolumeContext *s = outlink->src->priv;
125
+    AVFilterLink *inlink = outlink->src->inputs[0];
126
+
127
+    outlink->w = s->w;
128
+    outlink->h = s->h * inlink->channels + (inlink->channels - 1) * s->b;
129
+    outlink->sample_aspect_ratio = (AVRational){1,1};
130
+    outlink->frame_rate = s->frame_rate;
131
+
132
+    return 0;
133
+}
134
+
135
+static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
136
+{
137
+    AVFilterContext *ctx = inlink->dst;
138
+    AVFilterLink *outlink = ctx->outputs[0];
139
+    ShowVolumeContext *s = ctx->priv;
140
+    int c, i, j, k;
141
+    double values[VAR_VARS_NB];
142
+
143
+    if (!s->out || s->out->width  != outlink->w ||
144
+                   s->out->height != outlink->h) {
145
+        av_frame_free(&s->out);
146
+        s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
147
+        if (!s->out) {
148
+            av_frame_free(&insamples);
149
+            return AVERROR(ENOMEM);
150
+        }
151
+
152
+        for (i = 0; i < outlink->h; i++)
153
+            memset(s->out->data[0] + i * s->out->linesize[0], 0, outlink->w * 4);
154
+    }
155
+    s->out->pts = insamples->pts;
156
+
157
+    for (j = 0; j < outlink->h; j++) {
158
+        uint8_t *dst = s->out->data[0] + j * s->out->linesize[0];
159
+        for (k = 0; k < s->w; k++) {
160
+            dst[k * 4 + 0] = FFMAX(dst[k * 4 + 0] - s->f, 0);
161
+            dst[k * 4 + 1] = FFMAX(dst[k * 4 + 1] - s->f, 0);
162
+            dst[k * 4 + 2] = FFMAX(dst[k * 4 + 2] - s->f, 0);
163
+            dst[k * 4 + 3] = FFMAX(dst[k * 4 + 3] - s->f, 0);
164
+        }
165
+    }
166
+
167
+    for (c = 0; c < inlink->channels; c++) {
168
+        float *src = (float *)insamples->extended_data[c];
169
+        float max = 0;
170
+        int color;
171
+
172
+        for (i = 0; i < insamples->nb_samples; i++)
173
+            max = FFMAX(max, src[i]);
174
+
175
+        max = av_clipf(max, 0, 1);
176
+        values[VAR_VOLUME] = 20.0 * log(max) / M_LN10;
177
+        color = av_expr_eval(s->c_expr, values, NULL);
178
+
179
+        for (j = 0; j < s->h; j++) {
180
+            uint8_t *dst = s->out->data[0] + (c * s->h + c * s->b + j) * s->out->linesize[0];
181
+
182
+            for (k = 0; k < s->w * max; k++)
183
+                AV_WN32A(dst + k * 4, color);
184
+        }
185
+    }
186
+
187
+    av_frame_free(&insamples);
188
+
189
+    return ff_filter_frame(outlink, av_frame_clone(s->out));
190
+}
191
+
192
+static av_cold void uninit(AVFilterContext *ctx)
193
+{
194
+    ShowVolumeContext *s = ctx->priv;
195
+
196
+    av_frame_free(&s->out);
197
+    av_expr_free(s->c_expr);
198
+}
199
+
200
+static const AVFilterPad showvolume_inputs[] = {
201
+    {
202
+        .name         = "default",
203
+        .type         = AVMEDIA_TYPE_AUDIO,
204
+        .config_props = config_input,
205
+        .filter_frame = filter_frame,
206
+    },
207
+    { NULL }
208
+};
209
+
210
+static const AVFilterPad showvolume_outputs[] = {
211
+    {
212
+        .name         = "default",
213
+        .type         = AVMEDIA_TYPE_VIDEO,
214
+        .config_props = config_output,
215
+    },
216
+    { NULL }
217
+};
218
+
219
+AVFilter ff_avf_showvolume = {
220
+    .name          = "showvolume",
221
+    .description   = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."),
222
+    .init          = init,
223
+    .uninit        = uninit,
224
+    .query_formats = query_formats,
225
+    .priv_size     = sizeof(ShowVolumeContext),
226
+    .inputs        = showvolume_inputs,
227
+    .outputs       = showvolume_outputs,
228
+    .priv_class    = &showvolume_class,
229
+};
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR  5
33
-#define LIBAVFILTER_VERSION_MINOR  18
33
+#define LIBAVFILTER_VERSION_MINOR  19
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \