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/eval.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/opt.h"
31 typedef struct DrawGraphContext {
49 #define OFFSET(x) offsetof(DrawGraphContext, x)
50 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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="0xffff0000"}, 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="0xff00ff00"}, 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="0xffff00ff"}, 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="0xffffff00"}, 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, 3, 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 {"rscroll", "scroll from left to right", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "slide"},
73 { "size", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
74 { "s", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
78 static const char *const var_names[] = { "MAX", "MIN", "VAL", NULL };
79 enum { VAR_MAX, VAR_MIN, VAR_VAL, VAR_VARS_NB };
81 static av_cold int init(AVFilterContext *ctx)
83 DrawGraphContext *s = ctx->priv;
86 if (s->max <= s->min) {
87 av_log(ctx, AV_LOG_ERROR, "max is same or lower than min\n");
88 return AVERROR(EINVAL);
91 for (i = 0; i < 4; i++) {
93 ret = av_expr_parse(&s->fg_expr[i], s->fg_str[i], var_names,
94 NULL, NULL, NULL, NULL, 0, ctx);
106 static int query_formats(AVFilterContext *ctx)
108 AVFilterLink *outlink = ctx->outputs[0];
109 static const enum AVPixelFormat pix_fmts[] = {
115 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
116 if ((ret = ff_formats_ref(fmts_list, &outlink->in_formats)) < 0)
122 static void clear_image(DrawGraphContext *s, AVFrame *out, AVFilterLink *outlink)
125 int bg = AV_RN32(s->bg);
127 for (i = 0; i < out->height; i++)
128 for (j = 0; j < out->width; j++)
129 AV_WN32(out->data[0] + i * out->linesize[0] + j * 4, bg);
132 static inline void draw_dot(int fg, int x, int y, AVFrame *out)
134 AV_WN32(out->data[0] + y * out->linesize[0] + x * 4, fg);
137 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
139 AVFilterContext *ctx = inlink->dst;
140 DrawGraphContext *s = ctx->priv;
141 AVFilterLink *outlink = ctx->outputs[0];
142 AVDictionary *metadata;
143 AVDictionaryEntry *e;
144 AVFrame *out = s->out;
147 if (!s->out || s->out->width != outlink->w ||
148 s->out->height != outlink->h) {
149 av_frame_free(&s->out);
150 s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
154 return AVERROR(ENOMEM);
157 clear_image(s, out, outlink);
159 av_frame_copy_props(out, in);
161 metadata = av_frame_get_metadata(in);
163 for (i = 0; i < 4; i++) {
164 double values[VAR_VARS_NB];
169 e = av_dict_get(metadata, s->key[i], NULL, 0);
173 if (sscanf(e->value, "%f", &vf) != 1)
176 vf = av_clipf(vf, s->min, s->max);
178 values[VAR_MIN] = s->min;
179 values[VAR_MAX] = s->max;
180 values[VAR_VAL] = vf;
182 fg = av_expr_eval(s->fg_expr[i], values, NULL);
185 if (i == 0 && (s->x >= outlink->w || s->slide == 3)) {
186 if (s->slide == 0 || s->slide == 1)
190 s->x = outlink->w - 1;
191 for (j = 0; j < outlink->h; j++) {
192 memmove(out->data[0] + j * out->linesize[0] ,
193 out->data[0] + j * out->linesize[0] + 4,
194 (outlink->w - 1) * 4);
196 } else if (s->slide == 3) {
198 for (j = 0; j < outlink->h; j++) {
199 memmove(out->data[0] + j * out->linesize[0] + 4,
200 out->data[0] + j * out->linesize[0],
201 (outlink->w - 1) * 4);
203 } else if (s->slide == 0) {
204 clear_image(s, out, outlink);
209 y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
213 if (i == 0 && (s->slide > 0))
214 for (j = 0; j < outlink->h; j++)
215 draw_dot(bg, x, j, out);
217 old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
218 for (j = y; j < outlink->h; j++) {
220 (AV_RN32(out->data[0] + j * out->linesize[0] + x * 4) != old) ||
221 AV_RN32(out->data[0] + FFMIN(j+1, outlink->h - 1) * out->linesize[0] + x * 4) != old) {
222 draw_dot(fg, x, j, out);
225 draw_dot(fg, x, j, out);
229 if (i == 0 && (s->slide > 0))
230 for (j = 0; j < outlink->h; j++)
231 draw_dot(bg, x, j, out);
232 draw_dot(fg, x, y, out);
240 if (i == 0 && (s->slide > 0)) {
241 for (j = 0; j < y; j++)
242 draw_dot(bg, x, j, out);
243 for (j = outlink->h - 1; j > y; j--)
244 draw_dot(bg, x, j, out);
246 if (y <= s->prev_y[i]) {
247 for (j = y; j <= s->prev_y[i]; j++)
248 draw_dot(fg, x, j, out);
250 for (j = s->prev_y[i]; j <= y; j++)
251 draw_dot(fg, x, j, out);
261 return ff_filter_frame(outlink, av_frame_clone(s->out));
264 static int config_output(AVFilterLink *outlink)
266 DrawGraphContext *s = outlink->src->priv;
270 outlink->sample_aspect_ratio = (AVRational){1,1};
275 static av_cold void uninit(AVFilterContext *ctx)
277 DrawGraphContext *s = ctx->priv;
280 for (i = 0; i < 4; i++)
281 av_expr_free(s->fg_expr[i]);
282 av_frame_free(&s->out);
285 #if CONFIG_DRAWGRAPH_FILTER
287 AVFILTER_DEFINE_CLASS(drawgraph);
289 static const AVFilterPad drawgraph_inputs[] = {
292 .type = AVMEDIA_TYPE_VIDEO,
293 .filter_frame = filter_frame,
298 static const AVFilterPad drawgraph_outputs[] = {
301 .type = AVMEDIA_TYPE_VIDEO,
302 .config_props = config_output,
307 AVFilter ff_vf_drawgraph = {
309 .description = NULL_IF_CONFIG_SMALL("Draw a graph using input video metadata."),
310 .priv_size = sizeof(DrawGraphContext),
311 .priv_class = &drawgraph_class,
312 .query_formats = query_formats,
315 .inputs = drawgraph_inputs,
316 .outputs = drawgraph_outputs,
319 #endif // CONFIG_DRAWGRAPH_FILTER
321 #if CONFIG_ADRAWGRAPH_FILTER
323 #define adrawgraph_options drawgraph_options
324 AVFILTER_DEFINE_CLASS(adrawgraph);
326 static const AVFilterPad adrawgraph_inputs[] = {
329 .type = AVMEDIA_TYPE_AUDIO,
330 .filter_frame = filter_frame,
335 static const AVFilterPad adrawgraph_outputs[] = {
338 .type = AVMEDIA_TYPE_VIDEO,
339 .config_props = config_output,
344 AVFilter ff_avf_adrawgraph = {
345 .name = "adrawgraph",
346 .description = NULL_IF_CONFIG_SMALL("Draw a graph using input audio metadata."),
347 .priv_size = sizeof(DrawGraphContext),
348 .priv_class = &adrawgraph_class,
349 .query_formats = query_formats,
352 .inputs = adrawgraph_inputs,
353 .outputs = adrawgraph_outputs,
355 #endif // CONFIG_ADRAWGRAPH_FILTER