Browse code

lavfi: add (a)drawgraph filter

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

Paul B Mahol authored on 2015/06/25 00:48:57
Showing 6 changed files
... ...
@@ -12,6 +12,7 @@ version <next>:
12 12
 - showvolume filter
13 13
 - Many improvements to the JPEG 2000 decoder
14 14
 - Go2Meeting decoding support
15
+- adrawgraph audio and drawgraph video filter
15 16
 
16 17
 
17 18
 version 2.7:
... ...
@@ -4000,6 +4000,105 @@ drawbox=x=-t:y=0.5*(ih-iw/2.4)-t:w=iw+t*2:h=iw/2.4+t*2:t=2:c=red
4000 4000
 @end example
4001 4001
 @end itemize
4002 4002
 
4003
+@section drawgraph, adrawgraph
4004
+
4005
+Draw a graph using input video or audio metadata.
4006
+
4007
+It accepts the following parameters:
4008
+
4009
+@table @option
4010
+@item m1
4011
+Set 1st frame metadata key from which metadata values will be used to draw a graph.
4012
+
4013
+@item fg1
4014
+Set 1st foreground color expression.
4015
+
4016
+@item m2
4017
+Set 2nd frame metadata key from which metadata values will be used to draw a graph.
4018
+
4019
+@item fg2
4020
+Set 2nd foreground color expression.
4021
+
4022
+@item m3
4023
+Set 3rd frame metadata key from which metadata values will be used to draw a graph.
4024
+
4025
+@item fg3
4026
+Set 3rd foreground color expression.
4027
+
4028
+@item m4
4029
+Set 4th frame metadata key from which metadata values will be used to draw a graph.
4030
+
4031
+@item fg4
4032
+Set 4th foreground color expression.
4033
+
4034
+@item min
4035
+Set minimal value of metadata value.
4036
+
4037
+@item max
4038
+Set maximal value of metadata value.
4039
+
4040
+@item bg
4041
+Set graph background color. Default is white.
4042
+
4043
+@item mode
4044
+Set graph mode.
4045
+
4046
+Available values for mode is:
4047
+@table @samp
4048
+@item bar
4049
+@item dot
4050
+@item line
4051
+@end table
4052
+
4053
+Default is @code{line}.
4054
+
4055
+@item slide
4056
+Set slide mode.
4057
+
4058
+Available values for slide is:
4059
+@table @samp
4060
+@item frame
4061
+Draw new frame when right border is reached.
4062
+
4063
+@item replace
4064
+Replace old columns with new ones.
4065
+
4066
+@item scroll
4067
+Scroll from right to left.
4068
+@end table
4069
+
4070
+Default is @code{frame}.
4071
+
4072
+@item size
4073
+Set size of graph video. For the syntax of this option, check the
4074
+@ref{video size syntax,,"Video size" section in the ffmpeg-utils manual,ffmpeg-utils}.
4075
+The default value is @code{900x256}.
4076
+
4077
+The foreground color expressions can use the following variables:
4078
+@table @option
4079
+@item MIN
4080
+Minimal value of metadata value.
4081
+
4082
+@item MAX
4083
+Maximal value of metadata value.
4084
+
4085
+@item VAL
4086
+Current metadata key value.
4087
+@end table
4088
+
4089
+The color is defined as 0xAABBGGRR.
4090
+@end table
4091
+
4092
+Example using metadata from @ref{signalstats} filter:
4093
+@example
4094
+signalstats,drawgraph=lavfi.signalstats.YAVG:min=0:max=255
4095
+@end example
4096
+
4097
+Example using metadata from @ref{ebur128} filter:
4098
+@example
4099
+ebur128=metadata=1,adrawgraph=lavfi.r128.M:min=-120:max=5
4100
+@end example
4101
+
4003 4102
 @section drawgrid
4004 4103
 
4005 4104
 Draw a grid on the input image.
... ...
@@ -8639,6 +8738,7 @@ Swap the second and third planes of the input:
8639 8639
 ffmpeg -i INPUT -vf shuffleplanes=0:2:1:3 OUTPUT
8640 8640
 @end example
8641 8641
 
8642
+@anchor{signalstats}
8642 8643
 @section signalstats
8643 8644
 Evaluate various visual metrics that assist in determining issues associated
8644 8645
 with the digitization of analog video media.
... ...
@@ -11069,6 +11169,7 @@ do not have exactly the same duration in the first file.
11069 11069
 
11070 11070
 @end itemize
11071 11071
 
11072
+@anchor{ebur128}
11072 11073
 @section ebur128
11073 11074
 
11074 11075
 EBU R128 scanner filter. This filter takes an audio stream as input and outputs
... ...
@@ -117,6 +117,7 @@ OBJS-$(CONFIG_DELOGO_FILTER)                 += vf_delogo.o
117 117
 OBJS-$(CONFIG_DESHAKE_FILTER)                += vf_deshake.o
118 118
 OBJS-$(CONFIG_DETELECINE_FILTER)             += vf_detelecine.o
119 119
 OBJS-$(CONFIG_DRAWBOX_FILTER)                += vf_drawbox.o
120
+OBJS-$(CONFIG_DRAWGRAPH_FILTER)              += f_drawgraph.o
120 121
 OBJS-$(CONFIG_DRAWGRID_FILTER)               += vf_drawbox.o
121 122
 OBJS-$(CONFIG_DRAWTEXT_FILTER)               += vf_drawtext.o
122 123
 OBJS-$(CONFIG_ELBG_FILTER)                   += vf_elbg.o
... ...
@@ -237,6 +238,7 @@ OBJS-$(CONFIG_TESTSRC_FILTER)                += vsrc_testsrc.o
237 237
 OBJS-$(CONFIG_NULLSINK_FILTER)               += vsink_nullsink.o
238 238
 
239 239
 # multimedia filters
240
+OBJS-$(CONFIG_ADRAWGRAPH_FILTER)             += f_drawgraph.o
240 241
 OBJS-$(CONFIG_AVECTORSCOPE_FILTER)           += avf_avectorscope.o
241 242
 OBJS-$(CONFIG_CONCAT_FILTER)                 += avf_concat.o
242 243
 OBJS-$(CONFIG_SHOWCQT_FILTER)                += avf_showcqt.o
... ...
@@ -133,6 +133,7 @@ void avfilter_register_all(void)
133 133
     REGISTER_FILTER(DESHAKE,        deshake,        vf);
134 134
     REGISTER_FILTER(DETELECINE,     detelecine,     vf);
135 135
     REGISTER_FILTER(DRAWBOX,        drawbox,        vf);
136
+    REGISTER_FILTER(DRAWGRAPH,      drawgraph,      vf);
136 137
     REGISTER_FILTER(DRAWGRID,       drawgrid,       vf);
137 138
     REGISTER_FILTER(DRAWTEXT,       drawtext,       vf);
138 139
     REGISTER_FILTER(EDGEDETECT,     edgedetect,     vf);
... ...
@@ -252,6 +253,7 @@ void avfilter_register_all(void)
252 252
     REGISTER_FILTER(NULLSINK,       nullsink,       vsink);
253 253
 
254 254
     /* multimedia filters */
255
+    REGISTER_FILTER(ADRAWGRAPH,     adrawgraph,     avf);
255 256
     REGISTER_FILTER(AVECTORSCOPE,   avectorscope,   avf);
256 257
     REGISTER_FILTER(CONCAT,         concat,         avf);
257 258
     REGISTER_FILTER(SHOWCQT,        showcqt,        avf);
258 259
new file mode 100644
... ...
@@ -0,0 +1,346 @@
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 "float.h"
21
+
22
+#include "libavutil/eval.h"
23
+#include "libavutil/intreadwrite.h"
24
+#include "libavutil/opt.h"
25
+#include "avfilter.h"
26
+#include "formats.h"
27
+#include "internal.h"
28
+#include "video.h"
29
+
30
+typedef struct DrawGraphContext {
31
+    const AVClass *class;
32
+
33
+    char          *key[4];
34
+    float         min, max;
35
+    char          *fg_str[4];
36
+    AVExpr        *fg_expr[4];
37
+    uint8_t       bg[4];
38
+    int           mode;
39
+    int           slide;
40
+    int           w, h;
41
+
42
+    AVFrame       *out;
43
+    int           x;
44
+    int           prev_y[4];
45
+    int           first;
46
+} DrawGraphContext;
47
+
48
+#define OFFSET(x) offsetof(DrawGraphContext, x)
49
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50
+
51
+static const AVOption drawgraph_options[] = {
52
+    { "m1", "set 1st metadata key", OFFSET(key[0]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
53
+    { "fg1", "set 1st foreground color expression", OFFSET(fg_str[0]), AV_OPT_TYPE_STRING, {.str="0xff0000"}, CHAR_MIN, CHAR_MAX, FLAGS },
54
+    { "m2", "set 2nd metadata key", OFFSET(key[1]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
55
+    { "fg2", "set 2nd foreground color expression", OFFSET(fg_str[1]), AV_OPT_TYPE_STRING, {.str="0x00ff00"}, CHAR_MIN, CHAR_MAX, FLAGS },
56
+    { "m3", "set 3rd metadata key", OFFSET(key[2]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
57
+    { "fg3", "set 3rd foreground color expression", OFFSET(fg_str[2]), AV_OPT_TYPE_STRING, {.str="0xff00ff"}, CHAR_MIN, CHAR_MAX, FLAGS },
58
+    { "m4", "set 4th metadata key", OFFSET(key[3]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
59
+    { "fg4", "set 4th foreground color expression", OFFSET(fg_str[3]), AV_OPT_TYPE_STRING, {.str="0xffff00"}, CHAR_MIN, CHAR_MAX, FLAGS },
60
+    { "bg", "set background color", OFFSET(bg), AV_OPT_TYPE_COLOR, {.str="white"}, CHAR_MIN, CHAR_MAX, FLAGS },
61
+    { "min", "set minimal value", OFFSET(min), AV_OPT_TYPE_FLOAT, {.dbl=-1.}, INT_MIN, INT_MAX, FLAGS },
62
+    { "max", "set maximal value", OFFSET(max), AV_OPT_TYPE_FLOAT, {.dbl=1.}, INT_MIN, INT_MAX, FLAGS },
63
+    { "mode", "set graph mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "mode" },
64
+        {"bar", "draw bars", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode"},
65
+        {"dot", "draw dots", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode"},
66
+        {"line", "draw lines", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mode"},
67
+    { "slide", "set slide mode", OFFSET(slide), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "slide" },
68
+        {"frame", "draw new frames", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "slide"},
69
+        {"replace", "replace old columns with new", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "slide"},
70
+        {"scroll", "scroll from right to left", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "slide"},
71
+    { "size", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
72
+    { "s", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
73
+    { NULL }
74
+};
75
+
76
+static const char *const var_names[] = {   "MAX",   "MIN",   "VAL", NULL };
77
+enum                                   { VAR_MAX, VAR_MIN, VAR_VAL, VAR_VARS_NB };
78
+
79
+static av_cold int init(AVFilterContext *ctx)
80
+{
81
+    DrawGraphContext *s = ctx->priv;
82
+    int ret, i;
83
+
84
+    if (s->max <= s->min) {
85
+        av_log(ctx, AV_LOG_ERROR, "max is same or lower than min\n");
86
+        return AVERROR(EINVAL);
87
+    }
88
+
89
+    for (i = 0; i < 4; i++) {
90
+        if (s->fg_str[i]) {
91
+            ret = av_expr_parse(&s->fg_expr[i], s->fg_str[i], var_names,
92
+                                NULL, NULL, NULL, NULL, 0, ctx);
93
+
94
+            if (ret < 0)
95
+                return ret;
96
+        }
97
+    }
98
+
99
+    s->first = 1;
100
+
101
+    return 0;
102
+}
103
+
104
+static int query_formats(AVFilterContext *ctx)
105
+{
106
+    AVFilterLink *outlink = ctx->outputs[0];
107
+    static const enum AVPixelFormat pix_fmts[] = {
108
+        AV_PIX_FMT_RGBA,
109
+        AV_PIX_FMT_NONE
110
+    };
111
+
112
+    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
113
+    if (!fmts_list)
114
+        return AVERROR(ENOMEM);
115
+    ff_formats_ref(fmts_list, &outlink->in_formats);
116
+
117
+    return 0;
118
+}
119
+
120
+static void clear_image(DrawGraphContext *s, AVFrame *out, AVFilterLink *outlink)
121
+{
122
+    int i, j;
123
+    int bg = AV_RN32(s->bg);
124
+
125
+    for (i = 0; i < out->height; i++)
126
+        for (j = 0; j < out->width; j++)
127
+            AV_WN32(out->data[0] + i * out->linesize[0] + j * 4, bg);
128
+}
129
+
130
+static inline void draw_dot(int fg, int x, int y, AVFrame *out)
131
+{
132
+    AV_WN32(out->data[0] + y * out->linesize[0] + x * 4, fg);
133
+}
134
+
135
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
136
+{
137
+    AVFilterContext *ctx = inlink->dst;
138
+    DrawGraphContext *s = ctx->priv;
139
+    AVFilterLink *outlink = ctx->outputs[0];
140
+    AVDictionary *metadata;
141
+    AVDictionaryEntry *e;
142
+    AVFrame *out = s->out;
143
+    int i;
144
+
145
+    if (!s->out || s->out->width  != outlink->w ||
146
+                   s->out->height != outlink->h) {
147
+        av_frame_free(&s->out);
148
+        s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
149
+        out = s->out;
150
+        if (!s->out) {
151
+            av_frame_free(&in);
152
+            return AVERROR(ENOMEM);
153
+        }
154
+
155
+        clear_image(s, out, outlink);
156
+    }
157
+    av_frame_copy_props(out, in);
158
+
159
+    metadata = av_frame_get_metadata(in);
160
+
161
+    for (i = 0; i < 4; i++) {
162
+        double values[VAR_VARS_NB];
163
+        int j, y, x, fg, bg, old;
164
+        float vf;
165
+
166
+        e = av_dict_get(metadata, s->key[i], NULL, 0);
167
+        if (!e || !e->value)
168
+            continue;
169
+
170
+        if (sscanf(e->value, "%f", &vf) != 1)
171
+            continue;
172
+
173
+        vf = av_clipf(vf, s->min, s->max);
174
+
175
+        values[VAR_MIN] = s->min;
176
+        values[VAR_MAX] = s->max;
177
+        values[VAR_VAL] = vf;
178
+
179
+        fg = av_expr_eval(s->fg_expr[i], values, NULL);
180
+        bg = AV_RN32(s->bg);
181
+
182
+        if (i == 0 && s->x >= outlink->w) {
183
+            if (s->slide == 0 || s->slide == 1)
184
+                s->x = 0;
185
+
186
+            if (s->slide == 2) {
187
+                s->x = outlink->w - 1;
188
+                for (j = 0; j < outlink->h; j++) {
189
+                    memmove(out->data[0] + j * out->linesize[0] ,
190
+                            out->data[0] + j * out->linesize[0] + 4,
191
+                            (outlink->w - 1) * 4);
192
+                }
193
+            } else if (s->slide == 0) {
194
+                clear_image(s, out, outlink);
195
+            }
196
+        }
197
+
198
+        x = s->x;
199
+        y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
200
+
201
+        switch (s->mode) {
202
+        case 0:
203
+            if (i == 0 && (s->slide == 1 || s->slide == 2))
204
+                for (j = 0; j < outlink->h; j++)
205
+                    draw_dot(bg, x, j, out);
206
+
207
+            old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
208
+            for (j = y; j < outlink->h; j++) {
209
+                if (old != bg &&
210
+                    (AV_RN32(out->data[0] + j * out->linesize[0] + x * 4) != old) ||
211
+                    AV_RN32(out->data[0] + FFMIN(j+1, outlink->h - 1) * out->linesize[0] + x * 4) != old) {
212
+                    draw_dot(fg, x, j, out);
213
+                    break;
214
+                }
215
+                draw_dot(fg, x, j, out);
216
+            }
217
+            break;
218
+        case 1:
219
+            if (i == 0 && (s->slide == 1 || s->slide == 2))
220
+                for (j = 0; j < outlink->h; j++)
221
+                    draw_dot(bg, x, j, out);
222
+            draw_dot(fg, x, y, out);
223
+            break;
224
+        case 2:
225
+            if (s->first) {
226
+                s->first = 0;
227
+                s->prev_y[i] = y;
228
+            }
229
+
230
+            if (i == 0 && (s->slide == 1 || s->slide == 2)) {
231
+                for (j = 0; j < y; j++)
232
+                    draw_dot(bg, x, j, out);
233
+                for (j = outlink->h - 1; j > y; j--)
234
+                    draw_dot(bg, x, j, out);
235
+            }
236
+            if (y <= s->prev_y[i]) {
237
+                for (j = y; j <= s->prev_y[i]; j++)
238
+                    draw_dot(fg, x, j, out);
239
+            } else {
240
+                for (j = s->prev_y[i]; j <= y; j++)
241
+                    draw_dot(fg, x, j, out);
242
+            }
243
+            s->prev_y[i] = y;
244
+            break;
245
+        }
246
+    }
247
+
248
+    s->x++;
249
+
250
+    av_frame_free(&in);
251
+    return ff_filter_frame(outlink, av_frame_clone(s->out));
252
+}
253
+
254
+static int config_output(AVFilterLink *outlink)
255
+{
256
+    DrawGraphContext *s = outlink->src->priv;
257
+
258
+    outlink->w = s->w;
259
+    outlink->h = s->h;
260
+    outlink->sample_aspect_ratio = (AVRational){1,1};
261
+
262
+    return 0;
263
+}
264
+
265
+static av_cold void uninit(AVFilterContext *ctx)
266
+{
267
+    DrawGraphContext *s = ctx->priv;
268
+    int i;
269
+
270
+    for (i = 0; i < 4; i++)
271
+        av_expr_free(s->fg_expr[i]);
272
+    av_frame_free(&s->out);
273
+}
274
+
275
+#if CONFIG_DRAWGRAPH_FILTER
276
+
277
+AVFILTER_DEFINE_CLASS(drawgraph);
278
+
279
+static const AVFilterPad drawgraph_inputs[] = {
280
+    {
281
+        .name         = "default",
282
+        .type         = AVMEDIA_TYPE_VIDEO,
283
+        .filter_frame = filter_frame,
284
+    },
285
+    { NULL }
286
+};
287
+
288
+static const AVFilterPad drawgraph_outputs[] = {
289
+    {
290
+        .name         = "default",
291
+        .type         = AVMEDIA_TYPE_VIDEO,
292
+        .config_props = config_output,
293
+    },
294
+    { NULL }
295
+};
296
+
297
+AVFilter ff_vf_drawgraph = {
298
+    .name          = "drawgraph",
299
+    .description   = NULL_IF_CONFIG_SMALL("Draw a graph using input video metadata."),
300
+    .priv_size     = sizeof(DrawGraphContext),
301
+    .priv_class    = &drawgraph_class,
302
+    .query_formats = query_formats,
303
+    .init          = init,
304
+    .uninit        = uninit,
305
+    .inputs        = drawgraph_inputs,
306
+    .outputs       = drawgraph_outputs,
307
+};
308
+
309
+#endif // CONFIG_DRAWGRAPH_FILTER
310
+
311
+#if CONFIG_ADRAWGRAPH_FILTER
312
+
313
+#define adrawgraph_options drawgraph_options
314
+AVFILTER_DEFINE_CLASS(adrawgraph);
315
+
316
+static const AVFilterPad adrawgraph_inputs[] = {
317
+    {
318
+        .name         = "default",
319
+        .type         = AVMEDIA_TYPE_AUDIO,
320
+        .filter_frame = filter_frame,
321
+    },
322
+    { NULL }
323
+};
324
+
325
+static const AVFilterPad adrawgraph_outputs[] = {
326
+    {
327
+        .name         = "default",
328
+        .type         = AVMEDIA_TYPE_VIDEO,
329
+        .config_props = config_output,
330
+    },
331
+    { NULL }
332
+};
333
+
334
+AVFilter ff_avf_adrawgraph = {
335
+    .name          = "adrawgraph",
336
+    .description   = NULL_IF_CONFIG_SMALL("Draw a graph using input audio metadata."),
337
+    .priv_size     = sizeof(DrawGraphContext),
338
+    .priv_class    = &adrawgraph_class,
339
+    .query_formats = query_formats,
340
+    .init          = init,
341
+    .uninit        = uninit,
342
+    .inputs        = adrawgraph_inputs,
343
+    .outputs       = adrawgraph_outputs,
344
+};
345
+#endif // CONFIG_ADRAWGRAPH_FILTER
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR  5
33
-#define LIBAVFILTER_VERSION_MINOR  19
33
+#define LIBAVFILTER_VERSION_MINOR  20
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \