FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/f_graphmonitor.c
Date: 2024-04-19 07:31:02
Exec Total Coverage
Lines: 0 288 0.0%
Functions: 0 11 0.0%
Branches: 0 216 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2018 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/mem.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/timestamp.h"
28 #include "libavutil/xga_font_data.h"
29 #include "audio.h"
30 #include "avfilter.h"
31 #include "filters.h"
32 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35
36 typedef struct CacheItem {
37 int64_t previous_pts_us;
38 } CacheItem;
39
40 typedef struct GraphMonitorContext {
41 const AVClass *class;
42
43 int w, h;
44 float opacity;
45 int mode;
46 int flags;
47 AVRational frame_rate;
48
49 int eof;
50 int eof_frames;
51 int64_t pts;
52 int64_t next_pts;
53 uint8_t white[4];
54 uint8_t yellow[4];
55 uint8_t red[4];
56 uint8_t green[4];
57 uint8_t blue[4];
58 uint8_t gray[4];
59 uint8_t bg[4];
60
61 CacheItem *cache;
62 unsigned int cache_size;
63 unsigned int cache_index;
64 } GraphMonitorContext;
65
66 enum {
67 MODE_FULL = 0,
68 MODE_COMPACT = 1,
69 MODE_NOZERO = 2,
70 MODE_NOEOF = 4,
71 MODE_NODISABLED = 8,
72 MODE_MAX = 15
73 };
74
75 enum {
76 FLAG_NONE = 0 << 0,
77 FLAG_QUEUE = 1 << 0,
78 FLAG_FCIN = 1 << 1,
79 FLAG_FCOUT = 1 << 2,
80 FLAG_PTS = 1 << 3,
81 FLAG_TIME = 1 << 4,
82 FLAG_TB = 1 << 5,
83 FLAG_FMT = 1 << 6,
84 FLAG_SIZE = 1 << 7,
85 FLAG_RATE = 1 << 8,
86 FLAG_EOF = 1 << 9,
87 FLAG_SCIN = 1 << 10,
88 FLAG_SCOUT = 1 << 11,
89 FLAG_PTS_DELTA = 1 << 12,
90 FLAG_TIME_DELTA = 1 << 13,
91 FLAG_FC_DELTA = 1 << 14,
92 FLAG_SC_DELTA = 1 << 15,
93 FLAG_DISABLED = 1 << 16,
94 };
95
96 #define OFFSET(x) offsetof(GraphMonitorContext, x)
97 #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
98 #define VFR AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
99
100 static const AVOption graphmonitor_options[] = {
101 { "size", "set monitor size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, VF },
102 { "s", "set monitor size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, VF },
103 { "opacity", "set video opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=.9}, 0, 1, VFR },
104 { "o", "set video opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=.9}, 0, 1, VFR },
105 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, MODE_MAX, VFR, .unit = "mode" },
106 { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, MODE_MAX, VFR, .unit = "mode" },
107 { "full", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_FULL}, 0, 0, VFR, .unit = "mode" },
108 { "compact", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_COMPACT},0, 0, VFR, .unit = "mode" },
109 { "nozero", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NOZERO}, 0, 0, VFR, .unit = "mode" },
110 { "noeof", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NOEOF}, 0, 0, VFR, .unit = "mode" },
111 { "nodisabled",NULL,0,AV_OPT_TYPE_CONST, {.i64=MODE_NODISABLED},0,0,VFR,.unit = "mode" },
112 { "flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=FLAG_QUEUE}, 0, INT_MAX, VFR, .unit = "flags" },
113 { "f", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=FLAG_QUEUE}, 0, INT_MAX, VFR, .unit = "flags" },
114 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_NONE}, 0, 0, VFR, .unit = "flags" },
115 { "all", NULL, 0, AV_OPT_TYPE_CONST, {.i64=INT_MAX}, 0, 0, VFR, .unit = "flags" },
116 { "queue", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_QUEUE}, 0, 0, VFR, .unit = "flags" },
117 { "frame_count_in", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_FCOUT}, 0, 0, VFR, .unit = "flags" },
118 { "frame_count_out", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_FCIN}, 0, 0, VFR, .unit = "flags" },
119 { "frame_count_delta",NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_FC_DELTA},0, 0, VFR, .unit = "flags" },
120 { "pts", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_PTS}, 0, 0, VFR, .unit = "flags" },
121 { "pts_delta", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_PTS_DELTA},0,0, VFR, .unit = "flags" },
122 { "time", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_TIME}, 0, 0, VFR, .unit = "flags" },
123 { "time_delta", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_TIME_DELTA},0,0,VFR, .unit = "flags" },
124 { "timebase", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_TB}, 0, 0, VFR, .unit = "flags" },
125 { "format", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_FMT}, 0, 0, VFR, .unit = "flags" },
126 { "size", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_SIZE}, 0, 0, VFR, .unit = "flags" },
127 { "rate", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_RATE}, 0, 0, VFR, .unit = "flags" },
128 { "eof", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_EOF}, 0, 0, VFR, .unit = "flags" },
129 { "sample_count_in", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_SCOUT}, 0, 0, VFR, .unit = "flags" },
130 { "sample_count_out", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_SCIN}, 0, 0, VFR, .unit = "flags" },
131 { "sample_count_delta",NULL,0, AV_OPT_TYPE_CONST, {.i64=FLAG_SC_DELTA},0, 0, VFR, .unit = "flags" },
132 { "disabled", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAG_DISABLED},0, 0, VFR, .unit = "flags" },
133 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, VF },
134 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, VF },
135 { NULL }
136 };
137
138 static av_cold int init(AVFilterContext *ctx)
139 {
140 GraphMonitorContext *s = ctx->priv;
141
142 s->cache = av_fast_realloc(NULL, &s->cache_size,
143 8192 * sizeof(*(s->cache)));
144 if (!s->cache)
145 return AVERROR(ENOMEM);
146
147 return 0;
148 }
149
150 static int query_formats(AVFilterContext *ctx)
151 {
152 AVFilterLink *outlink = ctx->outputs[0];
153 static const enum AVPixelFormat pix_fmts[] = {
154 AV_PIX_FMT_RGBA,
155 AV_PIX_FMT_NONE
156 };
157 int ret;
158
159 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
160 if ((ret = ff_formats_ref(fmts_list, &outlink->incfg.formats)) < 0)
161 return ret;
162
163 return 0;
164 }
165
166 static void clear_image(GraphMonitorContext *s, AVFrame *out, AVFilterLink *outlink)
167 {
168 const int h = out->height;
169 const int w = out->width;
170 uint8_t *dst = out->data[0];
171 int bg = AV_RN32(s->bg);
172
173 for (int j = 0; j < w; j++)
174 AV_WN32(dst + j * 4, bg);
175 dst += out->linesize[0];
176 for (int i = 1; i < h; i++) {
177 memcpy(dst, out->data[0], w * 4);
178 dst += out->linesize[0];
179 }
180 }
181
182 static void drawtext(AVFrame *pic, int x, int y, const char *txt,
183 const int len, uint8_t *color)
184 {
185 const uint8_t *font;
186 int font_height;
187 int i;
188
189 font = avpriv_cga_font, font_height = 8;
190
191 if (y + 8 >= pic->height ||
192 x + len * 8 >= pic->width)
193 return;
194
195 for (i = 0; txt[i]; i++) {
196 int char_y, mask;
197
198 uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8)*4;
199 for (char_y = 0; char_y < font_height; char_y++) {
200 for (mask = 0x80; mask; mask >>= 1) {
201 if (font[txt[i] * font_height + char_y] & mask) {
202 p[0] = color[0];
203 p[1] = color[1];
204 p[2] = color[2];
205 }
206 p += 4;
207 }
208 p += pic->linesize[0] - 8 * 4;
209 }
210 }
211 }
212
213 static int filter_have_eof(AVFilterContext *filter)
214 {
215 for (int j = 0; j < filter->nb_inputs; j++) {
216 AVFilterLink *l = filter->inputs[j];
217
218 if (!ff_outlink_get_status(l))
219 return 0;
220 }
221
222 for (int j = 0; j < filter->nb_outputs; j++) {
223 AVFilterLink *l = filter->outputs[j];
224
225 if (!ff_outlink_get_status(l))
226 return 0;
227 }
228
229 return 1;
230 }
231
232 static int filter_have_queued(AVFilterContext *filter)
233 {
234 for (int j = 0; j < filter->nb_inputs; j++) {
235 AVFilterLink *l = filter->inputs[j];
236 size_t frames = ff_inlink_queued_frames(l);
237
238 if (frames)
239 return 1;
240 }
241
242 for (int j = 0; j < filter->nb_outputs; j++) {
243 AVFilterLink *l = filter->outputs[j];
244 size_t frames = ff_inlink_queued_frames(l);
245
246 if (frames)
247 return 1;
248 }
249
250 return 0;
251 }
252
253 static int draw_items(AVFilterContext *ctx,
254 AVFilterContext *filter,
255 AVFrame *out,
256 int xpos, int ypos,
257 AVFilterLink *l,
258 size_t frames)
259 {
260 GraphMonitorContext *s = ctx->priv;
261 int64_t previous_pts_us = s->cache[s->cache_index].previous_pts_us;
262 int64_t current_pts_us = l->current_pts_us;
263 const int flags = s->flags;
264 const int mode = s->mode;
265 char buffer[1024] = { 0 };
266 int len = 0;
267
268 if (flags & FLAG_FMT) {
269 if (l->type == AVMEDIA_TYPE_VIDEO) {
270 len = snprintf(buffer, sizeof(buffer)-1, " | format: %s",
271 av_get_pix_fmt_name(l->format));
272 } else if (l->type == AVMEDIA_TYPE_AUDIO) {
273 len = snprintf(buffer, sizeof(buffer)-1, " | format: %s",
274 av_get_sample_fmt_name(l->format));
275 }
276 drawtext(out, xpos, ypos, buffer, len, s->white);
277 xpos += len * 8;
278 }
279 if (flags & FLAG_SIZE) {
280 if (l->type == AVMEDIA_TYPE_VIDEO) {
281 len = snprintf(buffer, sizeof(buffer)-1, " | size: %dx%d", l->w, l->h);
282 } else if (l->type == AVMEDIA_TYPE_AUDIO) {
283 len = snprintf(buffer, sizeof(buffer)-1, " | channels: %d", l->ch_layout.nb_channels);
284 }
285 drawtext(out, xpos, ypos, buffer, len, s->white);
286 xpos += len * 8;
287 }
288 if (flags & FLAG_RATE) {
289 if (l->type == AVMEDIA_TYPE_VIDEO) {
290 len = snprintf(buffer, sizeof(buffer)-1, " | fps: %d/%d", l->frame_rate.num, l->frame_rate.den);
291 } else if (l->type == AVMEDIA_TYPE_AUDIO) {
292 len = snprintf(buffer, sizeof(buffer)-1, " | samplerate: %d", l->sample_rate);
293 }
294 drawtext(out, xpos, ypos, buffer, len, s->white);
295 xpos += len * 8;
296 }
297 if (flags & FLAG_TB) {
298 len = snprintf(buffer, sizeof(buffer)-1, " | tb: %d/%d", l->time_base.num, l->time_base.den);
299 drawtext(out, xpos, ypos, buffer, len, s->white);
300 xpos += len * 8;
301 }
302 if ((flags & FLAG_QUEUE) && (!(mode & MODE_NOZERO) || frames)) {
303 len = snprintf(buffer, sizeof(buffer)-1, " | queue: ");
304 drawtext(out, xpos, ypos, buffer, len, s->white);
305 xpos += len * 8;
306 len = snprintf(buffer, sizeof(buffer)-1, "%"SIZE_SPECIFIER, frames);
307 drawtext(out, xpos, ypos, buffer, len, frames > 0 ? frames >= 10 ? frames >= 50 ? s->red : s->yellow : s->green : s->white);
308 xpos += len * 8;
309 }
310 if ((flags & FLAG_FCIN) && (!(mode & MODE_NOZERO) || l->frame_count_in)) {
311 len = snprintf(buffer, sizeof(buffer)-1, " | in: %"PRId64, l->frame_count_in);
312 drawtext(out, xpos, ypos, buffer, len, s->white);
313 xpos += len * 8;
314 }
315 if ((flags & FLAG_FCOUT) && (!(mode & MODE_NOZERO) || l->frame_count_out)) {
316 len = snprintf(buffer, sizeof(buffer)-1, " | out: %"PRId64, l->frame_count_out);
317 drawtext(out, xpos, ypos, buffer, len, s->white);
318 xpos += len * 8;
319 }
320 if ((flags & FLAG_FC_DELTA) && (!(mode & MODE_NOZERO) || (l->frame_count_in - l->frame_count_out))) {
321 len = snprintf(buffer, sizeof(buffer)-1, " | delta: %"PRId64, l->frame_count_in - l->frame_count_out);
322 drawtext(out, xpos, ypos, buffer, len, s->white);
323 xpos += len * 8;
324 }
325 if ((flags & FLAG_SCIN) && (!(mode & MODE_NOZERO) || l->sample_count_in)) {
326 len = snprintf(buffer, sizeof(buffer)-1, " | sin: %"PRId64, l->sample_count_in);
327 drawtext(out, xpos, ypos, buffer, len, s->white);
328 xpos += len * 8;
329 }
330 if ((flags & FLAG_SCOUT) && (!(mode & MODE_NOZERO) || l->sample_count_out)) {
331 len = snprintf(buffer, sizeof(buffer)-1, " | sout: %"PRId64, l->sample_count_out);
332 drawtext(out, xpos, ypos, buffer, len, s->white);
333 xpos += len * 8;
334 }
335 if ((flags & FLAG_SC_DELTA) && (!(mode & MODE_NOZERO) || (l->sample_count_in - l->sample_count_out))) {
336 len = snprintf(buffer, sizeof(buffer)-1, " | sdelta: %"PRId64, l->sample_count_in - l->sample_count_out);
337 drawtext(out, xpos, ypos, buffer, len, s->white);
338 xpos += len * 8;
339 }
340 if ((flags & FLAG_PTS) && (!(mode & MODE_NOZERO) || current_pts_us)) {
341 len = snprintf(buffer, sizeof(buffer)-1, " | pts: %s", av_ts2str(current_pts_us));
342 drawtext(out, xpos, ypos, buffer, len, s->white);
343 xpos += len * 8;
344 }
345 if ((flags & FLAG_PTS_DELTA) && (!(mode & MODE_NOZERO) || (current_pts_us - previous_pts_us))) {
346 len = snprintf(buffer, sizeof(buffer)-1, " | pts_delta: %s", av_ts2str(current_pts_us - previous_pts_us));
347 drawtext(out, xpos, ypos, buffer, len, s->white);
348 xpos += len * 8;
349 }
350 if ((flags & FLAG_TIME) && (!(mode & MODE_NOZERO) || current_pts_us)) {
351 len = snprintf(buffer, sizeof(buffer)-1, " | time: %s", av_ts2timestr(current_pts_us, &AV_TIME_BASE_Q));
352 drawtext(out, xpos, ypos, buffer, len, s->white);
353 xpos += len * 8;
354 }
355 if ((flags & FLAG_TIME_DELTA) && (!(mode & MODE_NOZERO) || (current_pts_us - previous_pts_us))) {
356 len = snprintf(buffer, sizeof(buffer)-1, " | time_delta: %s", av_ts2timestr(current_pts_us - previous_pts_us, &AV_TIME_BASE_Q));
357 drawtext(out, xpos, ypos, buffer, len, s->white);
358 xpos += len * 8;
359 }
360 if ((flags & FLAG_EOF) && ff_outlink_get_status(l)) {
361 len = snprintf(buffer, sizeof(buffer)-1, " | eof");
362 drawtext(out, xpos, ypos, buffer, len, s->blue);
363 xpos += len * 8;
364 }
365 if ((flags & FLAG_DISABLED) && filter->is_disabled) {
366 len = snprintf(buffer, sizeof(buffer)-1, " | off");
367 drawtext(out, xpos, ypos, buffer, len, s->gray);
368 xpos += len * 8;
369 }
370
371 s->cache[s->cache_index].previous_pts_us = l->current_pts_us;
372
373 if (s->cache_index + 1 >= s->cache_size / sizeof(*(s->cache))) {
374 void *ptr = av_fast_realloc(s->cache, &s->cache_size, s->cache_size * 2);
375
376 if (!ptr)
377 return AVERROR(ENOMEM);
378 s->cache = ptr;
379 }
380 s->cache_index++;
381
382 return 0;
383 }
384
385 static int create_frame(AVFilterContext *ctx, int64_t pts)
386 {
387 GraphMonitorContext *s = ctx->priv;
388 AVFilterLink *outlink = ctx->outputs[0];
389 int ret, len, xpos, ypos = 0;
390 char buffer[1024];
391 AVFrame *out;
392
393 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
394 if (!out)
395 return AVERROR(ENOMEM);
396
397 s->bg[3] = 255 * s->opacity;
398 clear_image(s, out, outlink);
399
400 s->cache_index = 0;
401
402 for (int i = 0; i < ctx->graph->nb_filters; i++) {
403 AVFilterContext *filter = ctx->graph->filters[i];
404
405 if ((s->mode & MODE_COMPACT) && !filter_have_queued(filter))
406 continue;
407
408 if ((s->mode & MODE_NOEOF) && filter_have_eof(filter))
409 continue;
410
411 if ((s->mode & MODE_NODISABLED) && filter->is_disabled)
412 continue;
413
414 xpos = 0;
415 len = strlen(filter->name);
416 drawtext(out, xpos, ypos, filter->name, len, s->white);
417 xpos += len * 8 + 10;
418 len = strlen(filter->filter->name);
419 drawtext(out, xpos, ypos, filter->filter->name, len, s->white);
420 ypos += 10;
421 for (int j = 0; j < filter->nb_inputs; j++) {
422 AVFilterLink *l = filter->inputs[j];
423 size_t frames = ff_inlink_queued_frames(l);
424
425 if ((s->mode & MODE_COMPACT) && !frames)
426 continue;
427
428 if ((s->mode & MODE_NOEOF) && ff_outlink_get_status(l))
429 continue;
430
431 xpos = 10;
432 len = snprintf(buffer, sizeof(buffer)-1, "in%d: ", j);
433 drawtext(out, xpos, ypos, buffer, len, s->white);
434 xpos += len * 8;
435 len = strlen(l->src->name);
436 drawtext(out, xpos, ypos, l->src->name, len, s->white);
437 xpos += len * 8 + 10;
438 ret = draw_items(ctx, filter, out, xpos, ypos, l, frames);
439 if (ret < 0)
440 goto error;
441 ypos += 10;
442 }
443
444 ypos += 2;
445 for (int j = 0; j < filter->nb_outputs; j++) {
446 AVFilterLink *l = filter->outputs[j];
447 size_t frames = ff_inlink_queued_frames(l);
448
449 if ((s->mode & MODE_COMPACT) && !frames)
450 continue;
451
452 if ((s->mode & MODE_NOEOF) && ff_outlink_get_status(l))
453 continue;
454
455 xpos = 10;
456 len = snprintf(buffer, sizeof(buffer)-1, "out%d: ", j);
457 drawtext(out, xpos, ypos, buffer, len, s->white);
458 xpos += len * 8;
459 len = strlen(l->dst->name);
460 drawtext(out, xpos, ypos, l->dst->name, len, s->white);
461 xpos += len * 8 + 10;
462 ret = draw_items(ctx, filter, out, xpos, ypos, l, frames);
463 if (ret < 0)
464 goto error;
465 ypos += 10;
466 }
467 ypos += 5;
468 }
469
470 out->pts = pts;
471 out->duration = 1;
472 s->pts = pts + 1;
473 if (s->eof_frames)
474 s->eof_frames = 0;
475 return ff_filter_frame(outlink, out);
476 error:
477 av_frame_free(&out);
478 return ret;
479 }
480
481 static int activate(AVFilterContext *ctx)
482 {
483 GraphMonitorContext *s = ctx->priv;
484 AVFilterLink *inlink = ctx->inputs[0];
485 AVFilterLink *outlink = ctx->outputs[0];
486 int64_t pts = AV_NOPTS_VALUE;
487 int status;
488
489 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
490
491 if (!s->eof && ff_inlink_queued_frames(inlink)) {
492 AVFrame *frame = NULL;
493 int ret;
494
495 ret = ff_inlink_consume_frame(inlink, &frame);
496 if (ret < 0)
497 return ret;
498 if (ret > 0) {
499 pts = frame->pts;
500 av_frame_free(&frame);
501 }
502 }
503
504 if (pts != AV_NOPTS_VALUE) {
505 pts = av_rescale_q(pts, inlink->time_base, outlink->time_base);
506 if (s->pts == AV_NOPTS_VALUE)
507 s->pts = pts;
508 s->next_pts = pts;
509 } else if (s->eof) {
510 s->next_pts = s->pts + 1;
511 }
512
513 if (s->eof && s->eof_frames == 0) {
514 ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
515 return 0;
516 }
517
518 if (s->eof || (s->pts < s->next_pts && ff_outlink_frame_wanted(outlink)))
519 return create_frame(ctx, s->pts);
520
521 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
522 s->eof = 1;
523 s->eof_frames = 1;
524 ff_filter_set_ready(ctx, 100);
525 return 0;
526 }
527
528 if (!s->eof) {
529 FF_FILTER_FORWARD_WANTED(outlink, inlink);
530 } else {
531 ff_filter_set_ready(ctx, 100);
532 return 0;
533 }
534
535 return FFERROR_NOT_READY;
536 }
537
538 static int config_output(AVFilterLink *outlink)
539 {
540 GraphMonitorContext *s = outlink->src->priv;
541
542 s->white[0] = s->white[1] = s->white[2] = 255;
543 s->yellow[0] = s->yellow[1] = 255;
544 s->red[0] = 255;
545 s->green[1] = 255;
546 s->blue[2] = 255;
547 s->gray[0] = s->gray[1] = s->gray[2] = 128;
548 s->pts = AV_NOPTS_VALUE;
549 s->next_pts = AV_NOPTS_VALUE;
550 outlink->w = s->w;
551 outlink->h = s->h;
552 outlink->sample_aspect_ratio = (AVRational){1,1};
553 outlink->frame_rate = s->frame_rate;
554 outlink->time_base = av_inv_q(s->frame_rate);
555
556 return 0;
557 }
558
559 static av_cold void uninit(AVFilterContext *ctx)
560 {
561 GraphMonitorContext *s = ctx->priv;
562
563 av_freep(&s->cache);
564 s->cache_size = s->cache_index = 0;
565 }
566
567 AVFILTER_DEFINE_CLASS_EXT(graphmonitor, "(a)graphmonitor", graphmonitor_options);
568
569 static const AVFilterPad graphmonitor_outputs[] = {
570 {
571 .name = "default",
572 .type = AVMEDIA_TYPE_VIDEO,
573 .config_props = config_output,
574 },
575 };
576
577 #if CONFIG_GRAPHMONITOR_FILTER
578
579 const AVFilter ff_vf_graphmonitor = {
580 .name = "graphmonitor",
581 .description = NULL_IF_CONFIG_SMALL("Show various filtergraph stats."),
582 .priv_size = sizeof(GraphMonitorContext),
583 .priv_class = &graphmonitor_class,
584 .init = init,
585 .uninit = uninit,
586 .activate = activate,
587 FILTER_INPUTS(ff_video_default_filterpad),
588 FILTER_OUTPUTS(graphmonitor_outputs),
589 FILTER_QUERY_FUNC(query_formats),
590 .process_command = ff_filter_process_command,
591 };
592
593 #endif // CONFIG_GRAPHMONITOR_FILTER
594
595 #if CONFIG_AGRAPHMONITOR_FILTER
596
597 const AVFilter ff_avf_agraphmonitor = {
598 .name = "agraphmonitor",
599 .description = NULL_IF_CONFIG_SMALL("Show various filtergraph stats."),
600 .priv_class = &graphmonitor_class,
601 .priv_size = sizeof(GraphMonitorContext),
602 .init = init,
603 .uninit = uninit,
604 .activate = activate,
605 FILTER_INPUTS(ff_audio_default_filterpad),
606 FILTER_OUTPUTS(graphmonitor_outputs),
607 FILTER_QUERY_FUNC(query_formats),
608 .process_command = ff_filter_process_command,
609 };
610 #endif // CONFIG_AGRAPHMONITOR_FILTER
611