]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_drawgraph.c
avfilter: Constify all AVFilters
[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/avstring.h"
24 #include "libavutil/eval.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/opt.h"
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31
32 typedef struct DrawGraphContext {
33     const AVClass *class;
34
35     char          *key[4];
36     float         min, max;
37     char          *fg_str[4];
38     AVExpr        *fg_expr[4];
39     uint8_t       bg[4];
40     int           mode;
41     int           slide;
42     int           w, h;
43     AVRational    frame_rate;
44
45     AVFrame       *out;
46     int           x;
47     int           prev_y[4];
48     int           first[4];
49     float         *values[4];
50     int           values_size[4];
51     int           nb_values;
52     int64_t       prev_pts;
53 } DrawGraphContext;
54
55 #define OFFSET(x) offsetof(DrawGraphContext, x)
56 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57
58 static const AVOption drawgraph_options[] = {
59     { "m1", "set 1st metadata key", OFFSET(key[0]), AV_OPT_TYPE_STRING, {.str=""}, 0, 0, FLAGS },
60     { "fg1", "set 1st foreground color expression", OFFSET(fg_str[0]), AV_OPT_TYPE_STRING, {.str="0xffff0000"}, 0, 0, FLAGS },
61     { "m2", "set 2nd metadata key", OFFSET(key[1]), AV_OPT_TYPE_STRING, {.str=""}, 0, 0, FLAGS },
62     { "fg2", "set 2nd foreground color expression", OFFSET(fg_str[1]), AV_OPT_TYPE_STRING, {.str="0xff00ff00"}, 0, 0, FLAGS },
63     { "m3", "set 3rd metadata key", OFFSET(key[2]), AV_OPT_TYPE_STRING, {.str=""}, 0, 0, FLAGS },
64     { "fg3", "set 3rd foreground color expression", OFFSET(fg_str[2]), AV_OPT_TYPE_STRING, {.str="0xffff00ff"}, 0, 0, FLAGS },
65     { "m4", "set 4th metadata key", OFFSET(key[3]), AV_OPT_TYPE_STRING, {.str=""}, 0, 0, FLAGS },
66     { "fg4", "set 4th foreground color expression", OFFSET(fg_str[3]), AV_OPT_TYPE_STRING, {.str="0xffffff00"}, 0, 0, FLAGS },
67     { "bg", "set background color", OFFSET(bg), AV_OPT_TYPE_COLOR, {.str="white"}, 0, 0, FLAGS },
68     { "min", "set minimal value", OFFSET(min), AV_OPT_TYPE_FLOAT, {.dbl=-1.}, INT_MIN, INT_MAX, FLAGS },
69     { "max", "set maximal value", OFFSET(max), AV_OPT_TYPE_FLOAT, {.dbl=1.}, INT_MIN, INT_MAX, FLAGS },
70     { "mode", "set graph mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "mode" },
71         {"bar", "draw bars", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode"},
72         {"dot", "draw dots", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode"},
73         {"line", "draw lines", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mode"},
74     { "slide", "set slide mode", OFFSET(slide), AV_OPT_TYPE_INT, {.i64=0}, 0, 4, FLAGS, "slide" },
75         {"frame", "draw new frames", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "slide"},
76         {"replace", "replace old columns with new", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "slide"},
77         {"scroll", "scroll from right to left", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "slide"},
78         {"rscroll", "scroll from left to right", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "slide"},
79         {"picture", "display graph in single frame", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, FLAGS, "slide"},
80     { "size", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
81     { "s", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
82     { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
83     { "r",    "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
84     { NULL }
85 };
86
87 static const char *const var_names[] = {   "MAX",   "MIN",   "VAL", NULL };
88 enum                                   { VAR_MAX, VAR_MIN, VAR_VAL, VAR_VARS_NB };
89
90 static av_cold int init(AVFilterContext *ctx)
91 {
92     DrawGraphContext *s = ctx->priv;
93     int ret, i;
94
95     if (s->max <= s->min) {
96         av_log(ctx, AV_LOG_ERROR, "max is same or lower than min\n");
97         return AVERROR(EINVAL);
98     }
99
100     for (i = 0; i < 4; i++) {
101         if (s->fg_str[i]) {
102             ret = av_expr_parse(&s->fg_expr[i], s->fg_str[i], var_names,
103                                 NULL, NULL, NULL, NULL, 0, ctx);
104
105             if (ret < 0)
106                 return ret;
107         }
108     }
109
110     s->first[0] = s->first[1] = s->first[2] = s->first[3] = 1;
111
112     if (s->slide == 4) {
113         s->values[0] = av_fast_realloc(NULL, &s->values_size[0], 2000);
114         s->values[1] = av_fast_realloc(NULL, &s->values_size[1], 2000);
115         s->values[2] = av_fast_realloc(NULL, &s->values_size[2], 2000);
116         s->values[3] = av_fast_realloc(NULL, &s->values_size[3], 2000);
117
118         if (!s->values[0] || !s->values[1] ||
119             !s->values[2] || !s->values[3]) {
120             return AVERROR(ENOMEM);
121         }
122     }
123
124     return 0;
125 }
126
127 static int query_formats(AVFilterContext *ctx)
128 {
129     AVFilterLink *outlink = ctx->outputs[0];
130     static const enum AVPixelFormat pix_fmts[] = {
131         AV_PIX_FMT_RGBA,
132         AV_PIX_FMT_NONE
133     };
134     int ret;
135
136     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
137     if ((ret = ff_formats_ref(fmts_list, &outlink->incfg.formats)) < 0)
138         return ret;
139
140     return 0;
141 }
142
143 static void clear_image(DrawGraphContext *s, AVFrame *out, AVFilterLink *outlink)
144 {
145     int i, j;
146     int bg = AV_RN32(s->bg);
147
148     for (i = 0; i < out->height; i++)
149         for (j = 0; j < out->width; j++)
150             AV_WN32(out->data[0] + i * out->linesize[0] + j * 4, bg);
151 }
152
153 static inline void draw_dot(int fg, int x, int y, AVFrame *out)
154 {
155     AV_WN32(out->data[0] + y * out->linesize[0] + x * 4, fg);
156 }
157
158 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
159 {
160     AVFilterContext *ctx = inlink->dst;
161     DrawGraphContext *s = ctx->priv;
162     AVFilterLink *outlink = ctx->outputs[0];
163     AVDictionary *metadata;
164     AVDictionaryEntry *e;
165     AVFrame *out = s->out;
166     AVFrame *clone = NULL;
167     int64_t in_pts, out_pts;
168     int i;
169
170     if (s->slide == 4 && s->nb_values >= s->values_size[0] / sizeof(float)) {
171         float *ptr;
172
173         ptr = av_fast_realloc(s->values[0], &s->values_size[0], s->values_size[0] * 2);
174         if (!ptr)
175             return AVERROR(ENOMEM);
176         s->values[0] = ptr;
177
178         ptr = av_fast_realloc(s->values[1], &s->values_size[1], s->values_size[1] * 2);
179         if (!ptr)
180             return AVERROR(ENOMEM);
181         s->values[1] = ptr;
182
183         ptr = av_fast_realloc(s->values[2], &s->values_size[2], s->values_size[2] * 2);
184         if (!ptr)
185             return AVERROR(ENOMEM);
186         s->values[2] = ptr;
187
188         ptr = av_fast_realloc(s->values[3], &s->values_size[3], s->values_size[3] * 2);
189         if (!ptr)
190             return AVERROR(ENOMEM);
191         s->values[3] = ptr;
192     }
193
194     if (s->slide != 4 || s->nb_values == 0) {
195         if (!s->out || s->out->width  != outlink->w ||
196                        s->out->height != outlink->h) {
197             av_frame_free(&s->out);
198             s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
199             out = s->out;
200             if (!s->out) {
201                 av_frame_free(&in);
202                 return AVERROR(ENOMEM);
203             }
204
205             clear_image(s, out, outlink);
206         }
207         av_frame_copy_props(out, in);
208     }
209
210     metadata = in->metadata;
211
212     for (i = 0; i < 4; i++) {
213         double values[VAR_VARS_NB];
214         int j, y, x, old;
215         uint32_t fg, bg;
216         float vf;
217
218         if (s->slide == 4)
219             s->values[i][s->nb_values] = NAN;
220
221         e = av_dict_get(metadata, s->key[i], NULL, 0);
222         if (!e || !e->value)
223             continue;
224
225         if (av_sscanf(e->value, "%f", &vf) != 1)
226             continue;
227
228         vf = av_clipf(vf, s->min, s->max);
229
230         if (s->slide == 4) {
231             s->values[i][s->nb_values] = vf;
232             continue;
233         }
234
235         values[VAR_MIN] = s->min;
236         values[VAR_MAX] = s->max;
237         values[VAR_VAL] = vf;
238
239         fg = av_expr_eval(s->fg_expr[i], values, NULL);
240         bg = AV_RN32(s->bg);
241
242         if (i == 0 && (s->x >= outlink->w || s->slide == 3)) {
243             if (s->slide == 0 || s->slide == 1)
244                 s->x = 0;
245
246             if (s->slide == 2) {
247                 s->x = outlink->w - 1;
248                 for (j = 0; j < outlink->h; j++) {
249                     memmove(out->data[0] + j * out->linesize[0] ,
250                             out->data[0] + j * out->linesize[0] + 4,
251                             (outlink->w - 1) * 4);
252                 }
253             } else if (s->slide == 3) {
254                 s->x = 0;
255                 for (j = 0; j < outlink->h; j++) {
256                     memmove(out->data[0] + j * out->linesize[0] + 4,
257                             out->data[0] + j * out->linesize[0],
258                             (outlink->w - 1) * 4);
259                 }
260             } else if (s->slide == 0) {
261                 clear_image(s, out, outlink);
262             }
263         }
264
265         x = s->x;
266         y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
267
268         switch (s->mode) {
269         case 0:
270             if (i == 0 && (s->slide > 0))
271                 for (j = 0; j < outlink->h; j++)
272                     draw_dot(bg, x, j, out);
273
274             old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
275             for (j = y; j < outlink->h; j++) {
276                 if (old != bg &&
277                     (AV_RN32(out->data[0] + j * out->linesize[0] + x * 4) != old) ||
278                     AV_RN32(out->data[0] + FFMIN(j+1, outlink->h - 1) * out->linesize[0] + x * 4) != old) {
279                     draw_dot(fg, x, j, out);
280                     break;
281                 }
282                 draw_dot(fg, x, j, out);
283             }
284             break;
285         case 1:
286             if (i == 0 && (s->slide > 0))
287                 for (j = 0; j < outlink->h; j++)
288                     draw_dot(bg, x, j, out);
289             draw_dot(fg, x, y, out);
290             break;
291         case 2:
292             if (s->first[i]) {
293                 s->first[i] = 0;
294                 s->prev_y[i] = y;
295             }
296
297             if (i == 0 && (s->slide > 0)) {
298                 for (j = 0; j < y; j++)
299                     draw_dot(bg, x, j, out);
300                 for (j = outlink->h - 1; j > y; j--)
301                     draw_dot(bg, x, j, out);
302             }
303             if (y <= s->prev_y[i]) {
304                 for (j = y; j <= s->prev_y[i]; j++)
305                     draw_dot(fg, x, j, out);
306             } else {
307                 for (j = s->prev_y[i]; j <= y; j++)
308                     draw_dot(fg, x, j, out);
309             }
310             s->prev_y[i] = y;
311             break;
312         }
313     }
314
315     s->nb_values++;
316     s->x++;
317
318     in_pts = in->pts;
319
320     av_frame_free(&in);
321
322     if (s->slide == 4)
323         return 0;
324
325     out_pts = av_rescale_q(in_pts, inlink->time_base, outlink->time_base);
326
327     if (out_pts == s->prev_pts)
328         return 0;
329
330     clone = av_frame_clone(s->out);
331     if (!clone)
332         return AVERROR(ENOMEM);
333
334     clone->pts = s->prev_pts = out_pts;
335     return ff_filter_frame(outlink, clone);
336 }
337
338 static int request_frame(AVFilterLink *outlink)
339 {
340     AVFilterContext *ctx = outlink->src;
341     DrawGraphContext *s = ctx->priv;
342     AVFrame *out = s->out;
343     int ret, i, k, step, l;
344
345     ret = ff_request_frame(ctx->inputs[0]);
346
347     if (s->slide == 4 && ret == AVERROR_EOF && s->nb_values > 0) {
348         s->x = l = 0;
349         step = ceil(s->nb_values / (float)s->w);
350
351         for (k = 0; k < s->nb_values; k++) {
352             for (i = 0; i < 4; i++) {
353                 double values[VAR_VARS_NB];
354                 int j, y, x, old;
355                 uint32_t fg, bg;
356                 float vf = s->values[i][k];
357
358                 if (isnan(vf))
359                     continue;
360
361                 values[VAR_MIN] = s->min;
362                 values[VAR_MAX] = s->max;
363                 values[VAR_VAL] = vf;
364
365                 fg = av_expr_eval(s->fg_expr[i], values, NULL);
366                 bg = AV_RN32(s->bg);
367
368                 x = s->x;
369                 y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
370
371                 switch (s->mode) {
372                 case 0:
373                     old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
374                     for (j = y; j < outlink->h; j++) {
375                         if (old != bg &&
376                             (AV_RN32(out->data[0] + j * out->linesize[0] + x * 4) != old) ||
377                             AV_RN32(out->data[0] + FFMIN(j+1, outlink->h - 1) * out->linesize[0] + x * 4) != old) {
378                             draw_dot(fg, x, j, out);
379                             break;
380                         }
381                         draw_dot(fg, x, j, out);
382                     }
383                     break;
384                 case 1:
385                     draw_dot(fg, x, y, out);
386                     break;
387                 case 2:
388                     if (s->first[i]) {
389                         s->first[i] = 0;
390                         s->prev_y[i] = y;
391                     }
392
393                     if (y <= s->prev_y[i]) {
394                         for (j = y; j <= s->prev_y[i]; j++)
395                             draw_dot(fg, x, j, out);
396                     } else {
397                         for (j = s->prev_y[i]; j <= y; j++)
398                             draw_dot(fg, x, j, out);
399                     }
400                     s->prev_y[i] = y;
401                     break;
402                 }
403             }
404
405             l++;
406             if (l >= step) {
407                 l = 0;
408                 s->x++;
409             }
410         }
411
412         s->nb_values = 0;
413         out->pts = 0;
414         ret = ff_filter_frame(ctx->outputs[0], s->out);
415     }
416
417     return ret;
418 }
419
420 static int config_output(AVFilterLink *outlink)
421 {
422     DrawGraphContext *s = outlink->src->priv;
423
424     outlink->w = s->w;
425     outlink->h = s->h;
426     outlink->sample_aspect_ratio = (AVRational){1,1};
427     outlink->frame_rate = s->frame_rate;
428     outlink->time_base = av_inv_q(outlink->frame_rate);
429     s->prev_pts = AV_NOPTS_VALUE;
430
431     return 0;
432 }
433
434 static av_cold void uninit(AVFilterContext *ctx)
435 {
436     DrawGraphContext *s = ctx->priv;
437     int i;
438
439     for (i = 0; i < 4; i++)
440         av_expr_free(s->fg_expr[i]);
441
442     if (s->slide != 4)
443         av_frame_free(&s->out);
444
445     av_freep(&s->values[0]);
446     av_freep(&s->values[1]);
447     av_freep(&s->values[2]);
448     av_freep(&s->values[3]);
449 }
450
451 #if CONFIG_DRAWGRAPH_FILTER
452
453 AVFILTER_DEFINE_CLASS(drawgraph);
454
455 static const AVFilterPad drawgraph_inputs[] = {
456     {
457         .name         = "default",
458         .type         = AVMEDIA_TYPE_VIDEO,
459         .filter_frame = filter_frame,
460     },
461     { NULL }
462 };
463
464 static const AVFilterPad drawgraph_outputs[] = {
465     {
466         .name         = "default",
467         .type         = AVMEDIA_TYPE_VIDEO,
468         .config_props = config_output,
469         .request_frame = request_frame,
470     },
471     { NULL }
472 };
473
474 const AVFilter ff_vf_drawgraph = {
475     .name          = "drawgraph",
476     .description   = NULL_IF_CONFIG_SMALL("Draw a graph using input video metadata."),
477     .priv_size     = sizeof(DrawGraphContext),
478     .priv_class    = &drawgraph_class,
479     .query_formats = query_formats,
480     .init          = init,
481     .uninit        = uninit,
482     .inputs        = drawgraph_inputs,
483     .outputs       = drawgraph_outputs,
484 };
485
486 #endif // CONFIG_DRAWGRAPH_FILTER
487
488 #if CONFIG_ADRAWGRAPH_FILTER
489
490 #define adrawgraph_options drawgraph_options
491 AVFILTER_DEFINE_CLASS(adrawgraph);
492
493 static const AVFilterPad adrawgraph_inputs[] = {
494     {
495         .name         = "default",
496         .type         = AVMEDIA_TYPE_AUDIO,
497         .filter_frame = filter_frame,
498     },
499     { NULL }
500 };
501
502 static const AVFilterPad adrawgraph_outputs[] = {
503     {
504         .name         = "default",
505         .type         = AVMEDIA_TYPE_VIDEO,
506         .config_props = config_output,
507         .request_frame = request_frame,
508     },
509     { NULL }
510 };
511
512 const AVFilter ff_avf_adrawgraph = {
513     .name          = "adrawgraph",
514     .description   = NULL_IF_CONFIG_SMALL("Draw a graph using input audio metadata."),
515     .priv_size     = sizeof(DrawGraphContext),
516     .priv_class    = &adrawgraph_class,
517     .query_formats = query_formats,
518     .init          = init,
519     .uninit        = uninit,
520     .inputs        = adrawgraph_inputs,
521     .outputs       = adrawgraph_outputs,
522 };
523 #endif // CONFIG_ADRAWGRAPH_FILTER