2 * Copyright (c) 2015 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/avstring.h"
24 #include "libavutil/eval.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/opt.h"
32 typedef struct DrawGraphContext {
53 #define OFFSET(x) offsetof(DrawGraphContext, x)
54 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
56 static const AVOption drawgraph_options[] = {
57 { "m1", "set 1st metadata key", OFFSET(key[0]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
58 { "fg1", "set 1st foreground color expression", OFFSET(fg_str[0]), AV_OPT_TYPE_STRING, {.str="0xffff0000"}, CHAR_MIN, CHAR_MAX, FLAGS },
59 { "m2", "set 2nd metadata key", OFFSET(key[1]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
60 { "fg2", "set 2nd foreground color expression", OFFSET(fg_str[1]), AV_OPT_TYPE_STRING, {.str="0xff00ff00"}, CHAR_MIN, CHAR_MAX, FLAGS },
61 { "m3", "set 3rd metadata key", OFFSET(key[2]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
62 { "fg3", "set 3rd foreground color expression", OFFSET(fg_str[2]), AV_OPT_TYPE_STRING, {.str="0xffff00ff"}, CHAR_MIN, CHAR_MAX, FLAGS },
63 { "m4", "set 4th metadata key", OFFSET(key[3]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
64 { "fg4", "set 4th foreground color expression", OFFSET(fg_str[3]), AV_OPT_TYPE_STRING, {.str="0xffffff00"}, CHAR_MIN, CHAR_MAX, FLAGS },
65 { "bg", "set background color", OFFSET(bg), AV_OPT_TYPE_COLOR, {.str="white"}, CHAR_MIN, CHAR_MAX, FLAGS },
66 { "min", "set minimal value", OFFSET(min), AV_OPT_TYPE_FLOAT, {.dbl=-1.}, INT_MIN, INT_MAX, FLAGS },
67 { "max", "set maximal value", OFFSET(max), AV_OPT_TYPE_FLOAT, {.dbl=1.}, INT_MIN, INT_MAX, FLAGS },
68 { "mode", "set graph mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "mode" },
69 {"bar", "draw bars", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode"},
70 {"dot", "draw dots", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode"},
71 {"line", "draw lines", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mode"},
72 { "slide", "set slide mode", OFFSET(slide), AV_OPT_TYPE_INT, {.i64=0}, 0, 4, FLAGS, "slide" },
73 {"frame", "draw new frames", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "slide"},
74 {"replace", "replace old columns with new", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "slide"},
75 {"scroll", "scroll from right to left", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "slide"},
76 {"rscroll", "scroll from left to right", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "slide"},
77 {"picture", "display graph in single frame", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, FLAGS, "slide"},
78 { "size", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
79 { "s", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
83 static const char *const var_names[] = { "MAX", "MIN", "VAL", NULL };
84 enum { VAR_MAX, VAR_MIN, VAR_VAL, VAR_VARS_NB };
86 static av_cold int init(AVFilterContext *ctx)
88 DrawGraphContext *s = ctx->priv;
91 if (s->max <= s->min) {
92 av_log(ctx, AV_LOG_ERROR, "max is same or lower than min\n");
93 return AVERROR(EINVAL);
96 for (i = 0; i < 4; i++) {
98 ret = av_expr_parse(&s->fg_expr[i], s->fg_str[i], var_names,
99 NULL, NULL, NULL, NULL, 0, ctx);
106 s->first[0] = s->first[1] = s->first[2] = s->first[3] = 1;
109 s->values[0] = av_fast_realloc(NULL, &s->values_size[0], 2000);
110 s->values[1] = av_fast_realloc(NULL, &s->values_size[1], 2000);
111 s->values[2] = av_fast_realloc(NULL, &s->values_size[2], 2000);
112 s->values[3] = av_fast_realloc(NULL, &s->values_size[3], 2000);
114 if (!s->values[0] || !s->values[1] ||
115 !s->values[2] || !s->values[3]) {
116 return AVERROR(ENOMEM);
123 static int query_formats(AVFilterContext *ctx)
125 AVFilterLink *outlink = ctx->outputs[0];
126 static const enum AVPixelFormat pix_fmts[] = {
132 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
133 if ((ret = ff_formats_ref(fmts_list, &outlink->in_formats)) < 0)
139 static void clear_image(DrawGraphContext *s, AVFrame *out, AVFilterLink *outlink)
142 int bg = AV_RN32(s->bg);
144 for (i = 0; i < out->height; i++)
145 for (j = 0; j < out->width; j++)
146 AV_WN32(out->data[0] + i * out->linesize[0] + j * 4, bg);
149 static inline void draw_dot(int fg, int x, int y, AVFrame *out)
151 AV_WN32(out->data[0] + y * out->linesize[0] + x * 4, fg);
154 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
156 AVFilterContext *ctx = inlink->dst;
157 DrawGraphContext *s = ctx->priv;
158 AVFilterLink *outlink = ctx->outputs[0];
159 AVDictionary *metadata;
160 AVDictionaryEntry *e;
161 AVFrame *out = s->out;
164 if (s->slide == 4 && s->nb_values >= s->values_size[0] / sizeof(float)) {
167 ptr = av_fast_realloc(s->values[0], &s->values_size[0], s->values_size[0] * 2);
169 return AVERROR(ENOMEM);
172 ptr = av_fast_realloc(s->values[1], &s->values_size[1], s->values_size[1] * 2);
174 return AVERROR(ENOMEM);
177 ptr = av_fast_realloc(s->values[2], &s->values_size[2], s->values_size[2] * 2);
179 return AVERROR(ENOMEM);
182 ptr = av_fast_realloc(s->values[3], &s->values_size[3], s->values_size[3] * 2);
184 return AVERROR(ENOMEM);
188 if (s->slide != 4 || s->nb_values == 0) {
189 if (!s->out || s->out->width != outlink->w ||
190 s->out->height != outlink->h) {
191 av_frame_free(&s->out);
192 s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
196 return AVERROR(ENOMEM);
199 clear_image(s, out, outlink);
201 av_frame_copy_props(out, in);
204 metadata = in->metadata;
206 for (i = 0; i < 4; i++) {
207 double values[VAR_VARS_NB];
213 s->values[i][s->nb_values] = NAN;
215 e = av_dict_get(metadata, s->key[i], NULL, 0);
219 if (av_sscanf(e->value, "%f", &vf) != 1)
222 vf = av_clipf(vf, s->min, s->max);
225 s->values[i][s->nb_values] = vf;
229 values[VAR_MIN] = s->min;
230 values[VAR_MAX] = s->max;
231 values[VAR_VAL] = vf;
233 fg = av_expr_eval(s->fg_expr[i], values, NULL);
236 if (i == 0 && (s->x >= outlink->w || s->slide == 3)) {
237 if (s->slide == 0 || s->slide == 1)
241 s->x = outlink->w - 1;
242 for (j = 0; j < outlink->h; j++) {
243 memmove(out->data[0] + j * out->linesize[0] ,
244 out->data[0] + j * out->linesize[0] + 4,
245 (outlink->w - 1) * 4);
247 } else if (s->slide == 3) {
249 for (j = 0; j < outlink->h; j++) {
250 memmove(out->data[0] + j * out->linesize[0] + 4,
251 out->data[0] + j * out->linesize[0],
252 (outlink->w - 1) * 4);
254 } else if (s->slide == 0) {
255 clear_image(s, out, outlink);
260 y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
264 if (i == 0 && (s->slide > 0))
265 for (j = 0; j < outlink->h; j++)
266 draw_dot(bg, x, j, out);
268 old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
269 for (j = y; j < outlink->h; j++) {
271 (AV_RN32(out->data[0] + j * out->linesize[0] + x * 4) != old) ||
272 AV_RN32(out->data[0] + FFMIN(j+1, outlink->h - 1) * out->linesize[0] + x * 4) != old) {
273 draw_dot(fg, x, j, out);
276 draw_dot(fg, x, j, out);
280 if (i == 0 && (s->slide > 0))
281 for (j = 0; j < outlink->h; j++)
282 draw_dot(bg, x, j, out);
283 draw_dot(fg, x, y, out);
291 if (i == 0 && (s->slide > 0)) {
292 for (j = 0; j < y; j++)
293 draw_dot(bg, x, j, out);
294 for (j = outlink->h - 1; j > y; j--)
295 draw_dot(bg, x, j, out);
297 if (y <= s->prev_y[i]) {
298 for (j = y; j <= s->prev_y[i]; j++)
299 draw_dot(fg, x, j, out);
301 for (j = s->prev_y[i]; j <= y; j++)
302 draw_dot(fg, x, j, out);
317 return ff_filter_frame(outlink, av_frame_clone(s->out));
320 static int request_frame(AVFilterLink *outlink)
322 AVFilterContext *ctx = outlink->src;
323 DrawGraphContext *s = ctx->priv;
324 AVFrame *out = s->out;
325 int ret, i, k, step, l;
327 ret = ff_request_frame(ctx->inputs[0]);
329 if (s->slide == 4 && ret == AVERROR_EOF && s->nb_values > 0) {
331 step = ceil(s->nb_values / (float)s->w);
333 for (k = 0; k < s->nb_values; k++) {
334 for (i = 0; i < 4; i++) {
335 double values[VAR_VARS_NB];
338 float vf = s->values[i][k];
343 values[VAR_MIN] = s->min;
344 values[VAR_MAX] = s->max;
345 values[VAR_VAL] = vf;
347 fg = av_expr_eval(s->fg_expr[i], values, NULL);
351 y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
355 old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
356 for (j = y; j < outlink->h; j++) {
358 (AV_RN32(out->data[0] + j * out->linesize[0] + x * 4) != old) ||
359 AV_RN32(out->data[0] + FFMIN(j+1, outlink->h - 1) * out->linesize[0] + x * 4) != old) {
360 draw_dot(fg, x, j, out);
363 draw_dot(fg, x, j, out);
367 draw_dot(fg, x, y, out);
375 if (y <= s->prev_y[i]) {
376 for (j = y; j <= s->prev_y[i]; j++)
377 draw_dot(fg, x, j, out);
379 for (j = s->prev_y[i]; j <= y; j++)
380 draw_dot(fg, x, j, out);
396 ret = ff_filter_frame(ctx->outputs[0], s->out);
402 static int config_output(AVFilterLink *outlink)
404 DrawGraphContext *s = outlink->src->priv;
408 outlink->sample_aspect_ratio = (AVRational){1,1};
413 static av_cold void uninit(AVFilterContext *ctx)
415 DrawGraphContext *s = ctx->priv;
418 for (i = 0; i < 4; i++)
419 av_expr_free(s->fg_expr[i]);
422 av_frame_free(&s->out);
424 av_freep(&s->values[0]);
425 av_freep(&s->values[1]);
426 av_freep(&s->values[2]);
427 av_freep(&s->values[3]);
430 #if CONFIG_DRAWGRAPH_FILTER
432 AVFILTER_DEFINE_CLASS(drawgraph);
434 static const AVFilterPad drawgraph_inputs[] = {
437 .type = AVMEDIA_TYPE_VIDEO,
438 .filter_frame = filter_frame,
443 static const AVFilterPad drawgraph_outputs[] = {
446 .type = AVMEDIA_TYPE_VIDEO,
447 .config_props = config_output,
448 .request_frame = request_frame,
453 AVFilter ff_vf_drawgraph = {
455 .description = NULL_IF_CONFIG_SMALL("Draw a graph using input video metadata."),
456 .priv_size = sizeof(DrawGraphContext),
457 .priv_class = &drawgraph_class,
458 .query_formats = query_formats,
461 .inputs = drawgraph_inputs,
462 .outputs = drawgraph_outputs,
465 #endif // CONFIG_DRAWGRAPH_FILTER
467 #if CONFIG_ADRAWGRAPH_FILTER
469 #define adrawgraph_options drawgraph_options
470 AVFILTER_DEFINE_CLASS(adrawgraph);
472 static const AVFilterPad adrawgraph_inputs[] = {
475 .type = AVMEDIA_TYPE_AUDIO,
476 .filter_frame = filter_frame,
481 static const AVFilterPad adrawgraph_outputs[] = {
484 .type = AVMEDIA_TYPE_VIDEO,
485 .config_props = config_output,
486 .request_frame = request_frame,
491 AVFilter ff_avf_adrawgraph = {
492 .name = "adrawgraph",
493 .description = NULL_IF_CONFIG_SMALL("Draw a graph using input audio metadata."),
494 .priv_size = sizeof(DrawGraphContext),
495 .priv_class = &adrawgraph_class,
496 .query_formats = query_formats,
499 .inputs = adrawgraph_inputs,
500 .outputs = adrawgraph_outputs,
502 #endif // CONFIG_ADRAWGRAPH_FILTER