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 {
43 AVRational frame_rate;
55 #define OFFSET(x) offsetof(DrawGraphContext, x)
56 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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 },
87 static const char *const var_names[] = { "MAX", "MIN", "VAL", NULL };
88 enum { VAR_MAX, VAR_MIN, VAR_VAL, VAR_VARS_NB };
90 static av_cold int init(AVFilterContext *ctx)
92 DrawGraphContext *s = ctx->priv;
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);
100 for (i = 0; i < 4; i++) {
102 ret = av_expr_parse(&s->fg_expr[i], s->fg_str[i], var_names,
103 NULL, NULL, NULL, NULL, 0, ctx);
110 s->first[0] = s->first[1] = s->first[2] = s->first[3] = 1;
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);
118 if (!s->values[0] || !s->values[1] ||
119 !s->values[2] || !s->values[3]) {
120 return AVERROR(ENOMEM);
127 static int query_formats(AVFilterContext *ctx)
129 AVFilterLink *outlink = ctx->outputs[0];
130 static const enum AVPixelFormat pix_fmts[] = {
136 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
137 if ((ret = ff_formats_ref(fmts_list, &outlink->in_formats)) < 0)
143 static void clear_image(DrawGraphContext *s, AVFrame *out, AVFilterLink *outlink)
146 int bg = AV_RN32(s->bg);
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);
153 static inline void draw_dot(int fg, int x, int y, AVFrame *out)
155 AV_WN32(out->data[0] + y * out->linesize[0] + x * 4, fg);
158 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
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;
170 if (s->slide == 4 && s->nb_values >= s->values_size[0] / sizeof(float)) {
173 ptr = av_fast_realloc(s->values[0], &s->values_size[0], s->values_size[0] * 2);
175 return AVERROR(ENOMEM);
178 ptr = av_fast_realloc(s->values[1], &s->values_size[1], s->values_size[1] * 2);
180 return AVERROR(ENOMEM);
183 ptr = av_fast_realloc(s->values[2], &s->values_size[2], s->values_size[2] * 2);
185 return AVERROR(ENOMEM);
188 ptr = av_fast_realloc(s->values[3], &s->values_size[3], s->values_size[3] * 2);
190 return AVERROR(ENOMEM);
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);
202 return AVERROR(ENOMEM);
205 clear_image(s, out, outlink);
207 av_frame_copy_props(out, in);
210 metadata = in->metadata;
212 for (i = 0; i < 4; i++) {
213 double values[VAR_VARS_NB];
219 s->values[i][s->nb_values] = NAN;
221 e = av_dict_get(metadata, s->key[i], NULL, 0);
225 if (av_sscanf(e->value, "%f", &vf) != 1)
228 vf = av_clipf(vf, s->min, s->max);
231 s->values[i][s->nb_values] = vf;
235 values[VAR_MIN] = s->min;
236 values[VAR_MAX] = s->max;
237 values[VAR_VAL] = vf;
239 fg = av_expr_eval(s->fg_expr[i], values, NULL);
242 if (i == 0 && (s->x >= outlink->w || s->slide == 3)) {
243 if (s->slide == 0 || s->slide == 1)
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);
253 } else if (s->slide == 3) {
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);
260 } else if (s->slide == 0) {
261 clear_image(s, out, outlink);
266 y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
270 if (i == 0 && (s->slide > 0))
271 for (j = 0; j < outlink->h; j++)
272 draw_dot(bg, x, j, out);
274 old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
275 for (j = y; j < outlink->h; j++) {
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);
282 draw_dot(fg, x, j, out);
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);
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);
303 if (y <= s->prev_y[i]) {
304 for (j = y; j <= s->prev_y[i]; j++)
305 draw_dot(fg, x, j, out);
307 for (j = s->prev_y[i]; j <= y; j++)
308 draw_dot(fg, x, j, out);
325 out_pts = av_rescale_q(in_pts, inlink->time_base, outlink->time_base);
327 if (out_pts == s->prev_pts)
330 clone = av_frame_clone(s->out);
332 return AVERROR(ENOMEM);
334 clone->pts = s->prev_pts = out_pts;
335 return ff_filter_frame(outlink, clone);
338 static int request_frame(AVFilterLink *outlink)
340 AVFilterContext *ctx = outlink->src;
341 DrawGraphContext *s = ctx->priv;
342 AVFrame *out = s->out;
343 int ret, i, k, step, l;
345 ret = ff_request_frame(ctx->inputs[0]);
347 if (s->slide == 4 && ret == AVERROR_EOF && s->nb_values > 0) {
349 step = ceil(s->nb_values / (float)s->w);
351 for (k = 0; k < s->nb_values; k++) {
352 for (i = 0; i < 4; i++) {
353 double values[VAR_VARS_NB];
356 float vf = s->values[i][k];
361 values[VAR_MIN] = s->min;
362 values[VAR_MAX] = s->max;
363 values[VAR_VAL] = vf;
365 fg = av_expr_eval(s->fg_expr[i], values, NULL);
369 y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
373 old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
374 for (j = y; j < outlink->h; j++) {
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);
381 draw_dot(fg, x, j, out);
385 draw_dot(fg, x, y, out);
393 if (y <= s->prev_y[i]) {
394 for (j = y; j <= s->prev_y[i]; j++)
395 draw_dot(fg, x, j, out);
397 for (j = s->prev_y[i]; j <= y; j++)
398 draw_dot(fg, x, j, out);
414 ret = ff_filter_frame(ctx->outputs[0], s->out);
420 static int config_output(AVFilterLink *outlink)
422 DrawGraphContext *s = outlink->src->priv;
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;
434 static av_cold void uninit(AVFilterContext *ctx)
436 DrawGraphContext *s = ctx->priv;
439 for (i = 0; i < 4; i++)
440 av_expr_free(s->fg_expr[i]);
443 av_frame_free(&s->out);
445 av_freep(&s->values[0]);
446 av_freep(&s->values[1]);
447 av_freep(&s->values[2]);
448 av_freep(&s->values[3]);
451 #if CONFIG_DRAWGRAPH_FILTER
453 AVFILTER_DEFINE_CLASS(drawgraph);
455 static const AVFilterPad drawgraph_inputs[] = {
458 .type = AVMEDIA_TYPE_VIDEO,
459 .filter_frame = filter_frame,
464 static const AVFilterPad drawgraph_outputs[] = {
467 .type = AVMEDIA_TYPE_VIDEO,
468 .config_props = config_output,
469 .request_frame = request_frame,
474 AVFilter ff_vf_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,
482 .inputs = drawgraph_inputs,
483 .outputs = drawgraph_outputs,
486 #endif // CONFIG_DRAWGRAPH_FILTER
488 #if CONFIG_ADRAWGRAPH_FILTER
490 #define adrawgraph_options drawgraph_options
491 AVFILTER_DEFINE_CLASS(adrawgraph);
493 static const AVFilterPad adrawgraph_inputs[] = {
496 .type = AVMEDIA_TYPE_AUDIO,
497 .filter_frame = filter_frame,
502 static const AVFilterPad adrawgraph_outputs[] = {
505 .type = AVMEDIA_TYPE_VIDEO,
506 .config_props = config_output,
507 .request_frame = request_frame,
512 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,
520 .inputs = adrawgraph_inputs,
521 .outputs = adrawgraph_outputs,
523 #endif // CONFIG_ADRAWGRAPH_FILTER