]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_drawgraph.c
Merge commit 'ebaf571aca2dd6ce3caeeeec4210a3fccd47e7db'
[ffmpeg] / libavfilter / f_drawgraph.c
1 /*
2  * Copyright (c) 2015 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 "float.h"
22
23 #include "libavutil/eval.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/opt.h"
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30
31 typedef struct DrawGraphContext {
32     const AVClass *class;
33
34     char          *key[4];
35     float         min, max;
36     char          *fg_str[4];
37     AVExpr        *fg_expr[4];
38     uint8_t       bg[4];
39     int           mode;
40     int           slide;
41     int           w, h;
42
43     AVFrame       *out;
44     int           x;
45     int           prev_y[4];
46     int           first;
47 } DrawGraphContext;
48
49 #define OFFSET(x) offsetof(DrawGraphContext, x)
50 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51
52 static const AVOption drawgraph_options[] = {
53     { "m1", "set 1st metadata key", OFFSET(key[0]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
54     { "fg1", "set 1st foreground color expression", OFFSET(fg_str[0]), AV_OPT_TYPE_STRING, {.str="0xff0000"}, CHAR_MIN, CHAR_MAX, FLAGS },
55     { "m2", "set 2nd metadata key", OFFSET(key[1]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
56     { "fg2", "set 2nd foreground color expression", OFFSET(fg_str[1]), AV_OPT_TYPE_STRING, {.str="0x00ff00"}, CHAR_MIN, CHAR_MAX, FLAGS },
57     { "m3", "set 3rd metadata key", OFFSET(key[2]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
58     { "fg3", "set 3rd foreground color expression", OFFSET(fg_str[2]), AV_OPT_TYPE_STRING, {.str="0xff00ff"}, CHAR_MIN, CHAR_MAX, FLAGS },
59     { "m4", "set 4th metadata key", OFFSET(key[3]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
60     { "fg4", "set 4th foreground color expression", OFFSET(fg_str[3]), AV_OPT_TYPE_STRING, {.str="0xffff00"}, CHAR_MIN, CHAR_MAX, FLAGS },
61     { "bg", "set background color", OFFSET(bg), AV_OPT_TYPE_COLOR, {.str="white"}, CHAR_MIN, CHAR_MAX, FLAGS },
62     { "min", "set minimal value", OFFSET(min), AV_OPT_TYPE_FLOAT, {.dbl=-1.}, INT_MIN, INT_MAX, FLAGS },
63     { "max", "set maximal value", OFFSET(max), AV_OPT_TYPE_FLOAT, {.dbl=1.}, INT_MIN, INT_MAX, FLAGS },
64     { "mode", "set graph mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "mode" },
65         {"bar", "draw bars", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode"},
66         {"dot", "draw dots", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode"},
67         {"line", "draw lines", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mode"},
68     { "slide", "set slide mode", OFFSET(slide), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "slide" },
69         {"frame", "draw new frames", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "slide"},
70         {"replace", "replace old columns with new", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "slide"},
71         {"scroll", "scroll from right to left", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "slide"},
72     { "size", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
73     { "s", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
74     { NULL }
75 };
76
77 static const char *const var_names[] = {   "MAX",   "MIN",   "VAL", NULL };
78 enum                                   { VAR_MAX, VAR_MIN, VAR_VAL, VAR_VARS_NB };
79
80 static av_cold int init(AVFilterContext *ctx)
81 {
82     DrawGraphContext *s = ctx->priv;
83     int ret, i;
84
85     if (s->max <= s->min) {
86         av_log(ctx, AV_LOG_ERROR, "max is same or lower than min\n");
87         return AVERROR(EINVAL);
88     }
89
90     for (i = 0; i < 4; i++) {
91         if (s->fg_str[i]) {
92             ret = av_expr_parse(&s->fg_expr[i], s->fg_str[i], var_names,
93                                 NULL, NULL, NULL, NULL, 0, ctx);
94
95             if (ret < 0)
96                 return ret;
97         }
98     }
99
100     s->first = 1;
101
102     return 0;
103 }
104
105 static int query_formats(AVFilterContext *ctx)
106 {
107     AVFilterLink *outlink = ctx->outputs[0];
108     static const enum AVPixelFormat pix_fmts[] = {
109         AV_PIX_FMT_RGBA,
110         AV_PIX_FMT_NONE
111     };
112
113     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
114     if (!fmts_list)
115         return AVERROR(ENOMEM);
116     ff_formats_ref(fmts_list, &outlink->in_formats);
117
118     return 0;
119 }
120
121 static void clear_image(DrawGraphContext *s, AVFrame *out, AVFilterLink *outlink)
122 {
123     int i, j;
124     int bg = AV_RN32(s->bg);
125
126     for (i = 0; i < out->height; i++)
127         for (j = 0; j < out->width; j++)
128             AV_WN32(out->data[0] + i * out->linesize[0] + j * 4, bg);
129 }
130
131 static inline void draw_dot(int fg, int x, int y, AVFrame *out)
132 {
133     AV_WN32(out->data[0] + y * out->linesize[0] + x * 4, fg);
134 }
135
136 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
137 {
138     AVFilterContext *ctx = inlink->dst;
139     DrawGraphContext *s = ctx->priv;
140     AVFilterLink *outlink = ctx->outputs[0];
141     AVDictionary *metadata;
142     AVDictionaryEntry *e;
143     AVFrame *out = s->out;
144     int i;
145
146     if (!s->out || s->out->width  != outlink->w ||
147                    s->out->height != outlink->h) {
148         av_frame_free(&s->out);
149         s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
150         out = s->out;
151         if (!s->out) {
152             av_frame_free(&in);
153             return AVERROR(ENOMEM);
154         }
155
156         clear_image(s, out, outlink);
157     }
158     av_frame_copy_props(out, in);
159
160     metadata = av_frame_get_metadata(in);
161
162     for (i = 0; i < 4; i++) {
163         double values[VAR_VARS_NB];
164         int j, y, x, fg, bg, old;
165         float vf;
166
167         e = av_dict_get(metadata, s->key[i], NULL, 0);
168         if (!e || !e->value)
169             continue;
170
171         if (sscanf(e->value, "%f", &vf) != 1)
172             continue;
173
174         vf = av_clipf(vf, s->min, s->max);
175
176         values[VAR_MIN] = s->min;
177         values[VAR_MAX] = s->max;
178         values[VAR_VAL] = vf;
179
180         fg = av_expr_eval(s->fg_expr[i], values, NULL);
181         bg = AV_RN32(s->bg);
182
183         if (i == 0 && s->x >= outlink->w) {
184             if (s->slide == 0 || s->slide == 1)
185                 s->x = 0;
186
187             if (s->slide == 2) {
188                 s->x = outlink->w - 1;
189                 for (j = 0; j < outlink->h; j++) {
190                     memmove(out->data[0] + j * out->linesize[0] ,
191                             out->data[0] + j * out->linesize[0] + 4,
192                             (outlink->w - 1) * 4);
193                 }
194             } else if (s->slide == 0) {
195                 clear_image(s, out, outlink);
196             }
197         }
198
199         x = s->x;
200         y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
201
202         switch (s->mode) {
203         case 0:
204             if (i == 0 && (s->slide == 1 || s->slide == 2))
205                 for (j = 0; j < outlink->h; j++)
206                     draw_dot(bg, x, j, out);
207
208             old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
209             for (j = y; j < outlink->h; j++) {
210                 if (old != bg &&
211                     (AV_RN32(out->data[0] + j * out->linesize[0] + x * 4) != old) ||
212                     AV_RN32(out->data[0] + FFMIN(j+1, outlink->h - 1) * out->linesize[0] + x * 4) != old) {
213                     draw_dot(fg, x, j, out);
214                     break;
215                 }
216                 draw_dot(fg, x, j, out);
217             }
218             break;
219         case 1:
220             if (i == 0 && (s->slide == 1 || s->slide == 2))
221                 for (j = 0; j < outlink->h; j++)
222                     draw_dot(bg, x, j, out);
223             draw_dot(fg, x, y, out);
224             break;
225         case 2:
226             if (s->first) {
227                 s->first = 0;
228                 s->prev_y[i] = y;
229             }
230
231             if (i == 0 && (s->slide == 1 || s->slide == 2)) {
232                 for (j = 0; j < y; j++)
233                     draw_dot(bg, x, j, out);
234                 for (j = outlink->h - 1; j > y; j--)
235                     draw_dot(bg, x, j, out);
236             }
237             if (y <= s->prev_y[i]) {
238                 for (j = y; j <= s->prev_y[i]; j++)
239                     draw_dot(fg, x, j, out);
240             } else {
241                 for (j = s->prev_y[i]; j <= y; j++)
242                     draw_dot(fg, x, j, out);
243             }
244             s->prev_y[i] = y;
245             break;
246         }
247     }
248
249     s->x++;
250
251     av_frame_free(&in);
252     return ff_filter_frame(outlink, av_frame_clone(s->out));
253 }
254
255 static int config_output(AVFilterLink *outlink)
256 {
257     DrawGraphContext *s = outlink->src->priv;
258
259     outlink->w = s->w;
260     outlink->h = s->h;
261     outlink->sample_aspect_ratio = (AVRational){1,1};
262
263     return 0;
264 }
265
266 static av_cold void uninit(AVFilterContext *ctx)
267 {
268     DrawGraphContext *s = ctx->priv;
269     int i;
270
271     for (i = 0; i < 4; i++)
272         av_expr_free(s->fg_expr[i]);
273     av_frame_free(&s->out);
274 }
275
276 #if CONFIG_DRAWGRAPH_FILTER
277
278 AVFILTER_DEFINE_CLASS(drawgraph);
279
280 static const AVFilterPad drawgraph_inputs[] = {
281     {
282         .name         = "default",
283         .type         = AVMEDIA_TYPE_VIDEO,
284         .filter_frame = filter_frame,
285     },
286     { NULL }
287 };
288
289 static const AVFilterPad drawgraph_outputs[] = {
290     {
291         .name         = "default",
292         .type         = AVMEDIA_TYPE_VIDEO,
293         .config_props = config_output,
294     },
295     { NULL }
296 };
297
298 AVFilter ff_vf_drawgraph = {
299     .name          = "drawgraph",
300     .description   = NULL_IF_CONFIG_SMALL("Draw a graph using input video metadata."),
301     .priv_size     = sizeof(DrawGraphContext),
302     .priv_class    = &drawgraph_class,
303     .query_formats = query_formats,
304     .init          = init,
305     .uninit        = uninit,
306     .inputs        = drawgraph_inputs,
307     .outputs       = drawgraph_outputs,
308 };
309
310 #endif // CONFIG_DRAWGRAPH_FILTER
311
312 #if CONFIG_ADRAWGRAPH_FILTER
313
314 #define adrawgraph_options drawgraph_options
315 AVFILTER_DEFINE_CLASS(adrawgraph);
316
317 static const AVFilterPad adrawgraph_inputs[] = {
318     {
319         .name         = "default",
320         .type         = AVMEDIA_TYPE_AUDIO,
321         .filter_frame = filter_frame,
322     },
323     { NULL }
324 };
325
326 static const AVFilterPad adrawgraph_outputs[] = {
327     {
328         .name         = "default",
329         .type         = AVMEDIA_TYPE_VIDEO,
330         .config_props = config_output,
331     },
332     { NULL }
333 };
334
335 AVFilter ff_avf_adrawgraph = {
336     .name          = "adrawgraph",
337     .description   = NULL_IF_CONFIG_SMALL("Draw a graph using input audio metadata."),
338     .priv_size     = sizeof(DrawGraphContext),
339     .priv_class    = &adrawgraph_class,
340     .query_formats = query_formats,
341     .init          = init,
342     .uninit        = uninit,
343     .inputs        = adrawgraph_inputs,
344     .outputs       = adrawgraph_outputs,
345 };
346 #endif // CONFIG_ADRAWGRAPH_FILTER