FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/f_drawgraph.c
Date: 2026-09-24 20:08:24
Exec Total Coverage
Lines: 0 231 0.0%
Functions: 0 8 0.0%
Branches: 0 168 0.0%

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