FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/f_drawgraph.c
Date: 2024-04-19 17:50:32
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 "formats.h"
30 #include "internal.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(AVFilterContext *ctx)
131 {
132 AVFilterLink *outlink = ctx->outputs[0];
133 static const enum AVPixelFormat pix_fmts[] = {
134 AV_PIX_FMT_RGBA,
135 AV_PIX_FMT_NONE
136 };
137 int ret;
138
139 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
140 if ((ret = ff_formats_ref(fmts_list, &outlink->incfg.formats)) < 0)
141 return ret;
142
143 return 0;
144 }
145
146 static void clear_image(DrawGraphContext *s, AVFrame *out, AVFilterLink *outlink)
147 {
148 int i, j;
149 int bg = AV_RN32(s->bg);
150
151 for (i = 0; i < out->height; i++)
152 for (j = 0; j < out->width; j++)
153 AV_WN32(out->data[0] + i * out->linesize[0] + j * 4, bg);
154 }
155
156 static inline void draw_dot(int fg, int x, int y, AVFrame *out)
157 {
158 AV_WN32(out->data[0] + y * out->linesize[0] + x * 4, fg);
159 }
160
161 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
162 {
163 AVFilterContext *ctx = inlink->dst;
164 DrawGraphContext *s = ctx->priv;
165 AVFilterLink *outlink = ctx->outputs[0];
166 AVDictionary *metadata;
167 AVDictionaryEntry *e;
168 AVFrame *out = s->out;
169 AVFrame *clone = NULL;
170 int64_t in_pts, out_pts, in_duration;
171 int i;
172
173 if (s->slide == 4 && s->nb_values >= s->values_size[0] / sizeof(float)) {
174 float *ptr;
175
176 ptr = av_fast_realloc(s->values[0], &s->values_size[0], s->values_size[0] * 2);
177 if (!ptr)
178 return AVERROR(ENOMEM);
179 s->values[0] = ptr;
180
181 ptr = av_fast_realloc(s->values[1], &s->values_size[1], s->values_size[1] * 2);
182 if (!ptr)
183 return AVERROR(ENOMEM);
184 s->values[1] = ptr;
185
186 ptr = av_fast_realloc(s->values[2], &s->values_size[2], s->values_size[2] * 2);
187 if (!ptr)
188 return AVERROR(ENOMEM);
189 s->values[2] = ptr;
190
191 ptr = av_fast_realloc(s->values[3], &s->values_size[3], s->values_size[3] * 2);
192 if (!ptr)
193 return AVERROR(ENOMEM);
194 s->values[3] = ptr;
195 }
196
197 if (s->slide != 4 || s->nb_values == 0) {
198 if (!s->out || s->out->width != outlink->w ||
199 s->out->height != outlink->h) {
200 av_frame_free(&s->out);
201 s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
202 out = s->out;
203 if (!s->out) {
204 av_frame_free(&in);
205 return AVERROR(ENOMEM);
206 }
207
208 clear_image(s, out, outlink);
209 }
210 av_frame_copy_props(out, in);
211 }
212
213 metadata = in->metadata;
214
215 for (i = 0; i < 4; i++) {
216 double values[VAR_VARS_NB];
217 int j, y, x, old;
218 uint32_t fg, bg;
219 float vf;
220
221 if (s->slide == 4)
222 s->values[i][s->nb_values] = NAN;
223
224 e = av_dict_get(metadata, s->key[i], NULL, 0);
225 if (!e || !e->value)
226 continue;
227
228 if (av_sscanf(e->value, "%f", &vf) != 1)
229 continue;
230
231 vf = av_clipf(vf, s->min, s->max);
232
233 if (s->slide == 4) {
234 s->values[i][s->nb_values] = vf;
235 continue;
236 }
237
238 values[VAR_MIN] = s->min;
239 values[VAR_MAX] = s->max;
240 values[VAR_VAL] = vf;
241
242 fg = av_expr_eval(s->fg_expr[i], values, NULL);
243 bg = AV_RN32(s->bg);
244
245 if (i == 0 && (s->x >= outlink->w || s->slide == 3)) {
246 if (s->slide == 0 || s->slide == 1)
247 s->x = 0;
248
249 if (s->slide == 2) {
250 s->x = outlink->w - 1;
251 for (j = 0; j < outlink->h; j++) {
252 memmove(out->data[0] + j * out->linesize[0] ,
253 out->data[0] + j * out->linesize[0] + 4,
254 (outlink->w - 1) * 4);
255 }
256 } else if (s->slide == 3) {
257 s->x = 0;
258 for (j = 0; j < outlink->h; j++) {
259 memmove(out->data[0] + j * out->linesize[0] + 4,
260 out->data[0] + j * out->linesize[0],
261 (outlink->w - 1) * 4);
262 }
263 } else if (s->slide == 0) {
264 clear_image(s, out, outlink);
265 }
266 }
267
268 x = s->x;
269 y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
270
271 switch (s->mode) {
272 case 0:
273 if (i == 0 && (s->slide > 0))
274 for (j = 0; j < outlink->h; j++)
275 draw_dot(bg, x, j, out);
276
277 old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
278 for (j = y; j < outlink->h; j++) {
279 if (old != bg &&
280 (AV_RN32(out->data[0] + j * out->linesize[0] + x * 4) != old) ||
281 AV_RN32(out->data[0] + FFMIN(j+1, outlink->h - 1) * out->linesize[0] + x * 4) != old) {
282 draw_dot(fg, x, j, out);
283 break;
284 }
285 draw_dot(fg, x, j, out);
286 }
287 break;
288 case 1:
289 if (i == 0 && (s->slide > 0))
290 for (j = 0; j < outlink->h; j++)
291 draw_dot(bg, x, j, out);
292 draw_dot(fg, x, y, out);
293 break;
294 case 2:
295 if (s->first[i]) {
296 s->first[i] = 0;
297 s->prev_y[i] = y;
298 }
299
300 if (i == 0 && (s->slide > 0)) {
301 for (j = 0; j < y; j++)
302 draw_dot(bg, x, j, out);
303 for (j = outlink->h - 1; j > y; j--)
304 draw_dot(bg, x, j, out);
305 }
306 if (y <= s->prev_y[i]) {
307 for (j = y; j <= s->prev_y[i]; j++)
308 draw_dot(fg, x, j, out);
309 } else {
310 for (j = s->prev_y[i]; j <= y; j++)
311 draw_dot(fg, x, j, out);
312 }
313 s->prev_y[i] = y;
314 break;
315 }
316 }
317
318 s->nb_values++;
319 s->x++;
320
321 in_pts = in->pts;
322 in_duration = in->duration;
323
324 av_frame_free(&in);
325
326 if (s->slide == 4)
327 return 0;
328
329 out_pts = av_rescale_q(in_pts, inlink->time_base, outlink->time_base);
330
331 if (out_pts == s->prev_pts)
332 return 0;
333
334 clone = av_frame_clone(s->out);
335 if (!clone)
336 return AVERROR(ENOMEM);
337
338 clone->pts = s->prev_pts = out_pts;
339 clone->duration = av_rescale_q(in_duration, inlink->time_base, outlink->time_base);
340 return ff_filter_frame(outlink, clone);
341 }
342
343 static int request_frame(AVFilterLink *outlink)
344 {
345 AVFilterContext *ctx = outlink->src;
346 DrawGraphContext *s = ctx->priv;
347 AVFrame *out = s->out;
348 int ret, i, k, step, l;
349
350 ret = ff_request_frame(ctx->inputs[0]);
351
352 if (s->slide == 4 && ret == AVERROR_EOF && s->nb_values > 0) {
353 s->x = l = 0;
354 step = ceil(s->nb_values / (float)s->w);
355
356 for (k = 0; k < s->nb_values; k++) {
357 for (i = 0; i < 4; i++) {
358 double values[VAR_VARS_NB];
359 int j, y, x, old;
360 uint32_t fg, bg;
361 float vf = s->values[i][k];
362
363 if (isnan(vf))
364 continue;
365
366 values[VAR_MIN] = s->min;
367 values[VAR_MAX] = s->max;
368 values[VAR_VAL] = vf;
369
370 fg = av_expr_eval(s->fg_expr[i], values, NULL);
371 bg = AV_RN32(s->bg);
372
373 x = s->x;
374 y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
375
376 switch (s->mode) {
377 case 0:
378 old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
379 for (j = y; j < outlink->h; j++) {
380 if (old != bg &&
381 (AV_RN32(out->data[0] + j * out->linesize[0] + x * 4) != old) ||
382 AV_RN32(out->data[0] + FFMIN(j+1, outlink->h - 1) * out->linesize[0] + x * 4) != old) {
383 draw_dot(fg, x, j, out);
384 break;
385 }
386 draw_dot(fg, x, j, out);
387 }
388 break;
389 case 1:
390 draw_dot(fg, x, y, out);
391 break;
392 case 2:
393 if (s->first[i]) {
394 s->first[i] = 0;
395 s->prev_y[i] = y;
396 }
397
398 if (y <= s->prev_y[i]) {
399 for (j = y; j <= s->prev_y[i]; j++)
400 draw_dot(fg, x, j, out);
401 } else {
402 for (j = s->prev_y[i]; j <= y; j++)
403 draw_dot(fg, x, j, out);
404 }
405 s->prev_y[i] = y;
406 break;
407 }
408 }
409
410 l++;
411 if (l >= step) {
412 l = 0;
413 s->x++;
414 }
415 }
416
417 s->nb_values = 0;
418 out->pts = 0;
419 ret = ff_filter_frame(ctx->outputs[0], s->out);
420 }
421
422 return ret;
423 }
424
425 static int config_output(AVFilterLink *outlink)
426 {
427 DrawGraphContext *s = outlink->src->priv;
428
429 outlink->w = s->w;
430 outlink->h = s->h;
431 outlink->sample_aspect_ratio = (AVRational){1,1};
432 outlink->frame_rate = s->frame_rate;
433 outlink->time_base = av_inv_q(outlink->frame_rate);
434 s->prev_pts = AV_NOPTS_VALUE;
435
436 return 0;
437 }
438
439 static av_cold void uninit(AVFilterContext *ctx)
440 {
441 DrawGraphContext *s = ctx->priv;
442 int i;
443
444 for (i = 0; i < 4; i++)
445 av_expr_free(s->fg_expr[i]);
446
447 if (s->slide != 4)
448 av_frame_free(&s->out);
449
450 av_freep(&s->values[0]);
451 av_freep(&s->values[1]);
452 av_freep(&s->values[2]);
453 av_freep(&s->values[3]);
454 }
455
456 static const AVFilterPad drawgraph_outputs[] = {
457 {
458 .name = "default",
459 .type = AVMEDIA_TYPE_VIDEO,
460 .config_props = config_output,
461 .request_frame = request_frame,
462 },
463 };
464
465 #if CONFIG_DRAWGRAPH_FILTER
466
467 static const AVFilterPad drawgraph_inputs[] = {
468 {
469 .name = "default",
470 .type = AVMEDIA_TYPE_VIDEO,
471 .filter_frame = filter_frame,
472 },
473 };
474
475 const AVFilter ff_vf_drawgraph = {
476 .name = "drawgraph",
477 .description = NULL_IF_CONFIG_SMALL("Draw a graph using input video metadata."),
478 .priv_size = sizeof(DrawGraphContext),
479 .priv_class = &drawgraph_class,
480 .init = init,
481 .uninit = uninit,
482 FILTER_INPUTS(drawgraph_inputs),
483 FILTER_OUTPUTS(drawgraph_outputs),
484 FILTER_QUERY_FUNC(query_formats),
485 };
486
487 #endif // CONFIG_DRAWGRAPH_FILTER
488
489 #if CONFIG_ADRAWGRAPH_FILTER
490
491 static const AVFilterPad adrawgraph_inputs[] = {
492 {
493 .name = "default",
494 .type = AVMEDIA_TYPE_AUDIO,
495 .filter_frame = filter_frame,
496 },
497 };
498
499 const AVFilter ff_avf_adrawgraph = {
500 .name = "adrawgraph",
501 .description = NULL_IF_CONFIG_SMALL("Draw a graph using input audio metadata."),
502 .priv_class = &drawgraph_class,
503 .priv_size = sizeof(DrawGraphContext),
504 .init = init,
505 .uninit = uninit,
506 FILTER_INPUTS(adrawgraph_inputs),
507 FILTER_OUTPUTS(drawgraph_outputs),
508 FILTER_QUERY_FUNC(query_formats),
509 };
510 #endif // CONFIG_ADRAWGRAPH_FILTER
511