2 * Copyright (c) 2018 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/pixdesc.h"
24 #include "libavutil/eval.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/timestamp.h"
28 #include "libavutil/xga_font_data.h"
35 typedef struct GraphMonitorContext {
42 AVRational frame_rate;
50 } GraphMonitorContext;
64 #define OFFSET(x) offsetof(GraphMonitorContext, x)
65 #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
67 static const AVOption graphmonitor_options[] = {
68 { "size", "set monitor size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, VF },
69 { "s", "set monitor size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, VF },
70 { "opacity", "set video opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=.9}, 0, 1, VF },
71 { "o", "set video opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=.9}, 0, 1, VF },
72 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, VF, "mode" },
73 { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, VF, "mode" },
74 { "full", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, VF, "mode" },
75 { "compact", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, VF, "mode" },
76 { "flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=MODE_QUEUE}, 0, INT_MAX, VF, "flags" },
77 { "f", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=MODE_QUEUE}, 0, INT_MAX, VF, "flags" },
78 { "queue", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_QUEUE}, 0, 0, VF, "flags" },
79 { "frame_count_in", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_FCOUT}, 0, 0, VF, "flags" },
80 { "frame_count_out", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_FCIN}, 0, 0, VF, "flags" },
81 { "pts", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_PTS}, 0, 0, VF, "flags" },
82 { "time", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_TIME}, 0, 0, VF, "flags" },
83 { "timebase", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_TB}, 0, 0, VF, "flags" },
84 { "format", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_FMT}, 0, 0, VF, "flags" },
85 { "size", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_SIZE}, 0, 0, VF, "flags" },
86 { "rate", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_RATE}, 0, 0, VF, "flags" },
87 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, VF },
88 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, VF },
92 static int query_formats(AVFilterContext *ctx)
94 AVFilterLink *outlink = ctx->outputs[0];
95 static const enum AVPixelFormat pix_fmts[] = {
101 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
102 if ((ret = ff_formats_ref(fmts_list, &outlink->in_formats)) < 0)
108 static void clear_image(GraphMonitorContext *s, AVFrame *out, AVFilterLink *outlink)
110 int bg = AV_RN32(s->bg);
112 for (int i = 0; i < out->height; i++)
113 for (int j = 0; j < out->width; j++)
114 AV_WN32(out->data[0] + i * out->linesize[0] + j * 4, bg);
117 static void drawtext(AVFrame *pic, int x, int y, const char *txt, uint8_t *color)
123 font = avpriv_cga_font, font_height = 8;
125 if (y + 8 >= pic->height ||
126 x + strlen(txt) * 8 >= pic->width)
129 for (i = 0; txt[i]; i++) {
132 uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8)*4;
133 for (char_y = 0; char_y < font_height; char_y++) {
134 for (mask = 0x80; mask; mask >>= 1) {
135 if (font[txt[i] * font_height + char_y] & mask) {
142 p += pic->linesize[0] - 8 * 4;
147 static int filter_have_queued(AVFilterContext *filter)
149 for (int j = 0; j < filter->nb_inputs; j++) {
150 AVFilterLink *l = filter->inputs[j];
151 size_t frames = ff_inlink_queued_frames(l);
157 for (int j = 0; j < filter->nb_outputs; j++) {
158 AVFilterLink *l = filter->outputs[j];
159 size_t frames = ff_inlink_queued_frames(l);
168 static void draw_items(AVFilterContext *ctx, AVFrame *out,
173 GraphMonitorContext *s = ctx->priv;
174 char buffer[1024] = { 0 };
176 if (s->flags & MODE_FMT) {
177 if (l->type == AVMEDIA_TYPE_VIDEO) {
178 snprintf(buffer, sizeof(buffer)-1, " | format: %s",
179 av_get_pix_fmt_name(l->format));
180 } else if (l->type == AVMEDIA_TYPE_AUDIO) {
181 snprintf(buffer, sizeof(buffer)-1, " | format: %s",
182 av_get_sample_fmt_name(l->format));
184 drawtext(out, xpos, ypos, buffer, s->white);
185 xpos += strlen(buffer) * 8;
187 if (s->flags & MODE_SIZE) {
188 if (l->type == AVMEDIA_TYPE_VIDEO) {
189 snprintf(buffer, sizeof(buffer)-1, " | size: %dx%d", l->w, l->h);
190 } else if (l->type == AVMEDIA_TYPE_AUDIO) {
191 snprintf(buffer, sizeof(buffer)-1, " | channels: %d", l->channels);
193 drawtext(out, xpos, ypos, buffer, s->white);
194 xpos += strlen(buffer) * 8;
196 if (s->flags & MODE_RATE) {
197 if (l->type == AVMEDIA_TYPE_VIDEO) {
198 snprintf(buffer, sizeof(buffer)-1, " | fps: %d/%d", l->frame_rate.num, l->frame_rate.den);
199 } else if (l->type == AVMEDIA_TYPE_AUDIO) {
200 snprintf(buffer, sizeof(buffer)-1, " | samplerate: %d", l->sample_rate);
202 drawtext(out, xpos, ypos, buffer, s->white);
203 xpos += strlen(buffer) * 8;
205 if (s->flags & MODE_TB) {
206 snprintf(buffer, sizeof(buffer)-1, " | tb: %d/%d", l->time_base.num, l->time_base.den);
207 drawtext(out, xpos, ypos, buffer, s->white);
208 xpos += strlen(buffer) * 8;
210 if (s->flags & MODE_QUEUE) {
211 snprintf(buffer, sizeof(buffer)-1, " | queue: ");
212 drawtext(out, xpos, ypos, buffer, s->white);
213 xpos += strlen(buffer) * 8;
214 snprintf(buffer, sizeof(buffer)-1, "%"PRId64, frames);
215 drawtext(out, xpos, ypos, buffer, frames > 0 ? frames >= 10 ? frames >= 50 ? s->red : s->yellow : s->green : s->white);
216 xpos += strlen(buffer) * 8;
218 if (s->flags & MODE_FCIN) {
219 snprintf(buffer, sizeof(buffer)-1, " | in: %"PRId64, l->frame_count_in);
220 drawtext(out, xpos, ypos, buffer, s->white);
221 xpos += strlen(buffer) * 8;
223 if (s->flags & MODE_FCOUT) {
224 snprintf(buffer, sizeof(buffer)-1, " | out: %"PRId64, l->frame_count_out);
225 drawtext(out, xpos, ypos, buffer, s->white);
226 xpos += strlen(buffer) * 8;
228 if (s->flags & MODE_PTS) {
229 snprintf(buffer, sizeof(buffer)-1, " | pts: %s", av_ts2str(l->current_pts_us));
230 drawtext(out, xpos, ypos, buffer, s->white);
231 xpos += strlen(buffer) * 8;
233 if (s->flags & MODE_TIME) {
234 snprintf(buffer, sizeof(buffer)-1, " | time: %s", av_ts2timestr(l->current_pts_us, &AV_TIME_BASE_Q));
235 drawtext(out, xpos, ypos, buffer, s->white);
236 xpos += strlen(buffer) * 8;
240 static int create_frame(AVFilterContext *ctx, int64_t pts)
242 GraphMonitorContext *s = ctx->priv;
243 AVFilterLink *outlink = ctx->outputs[0];
247 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
249 return AVERROR(ENOMEM);
251 clear_image(s, out, outlink);
253 for (int i = 0; i < ctx->graph->nb_filters; i++) {
254 AVFilterContext *filter = ctx->graph->filters[i];
255 char buffer[1024] = { 0 };
257 if (s->mode && !filter_have_queued(filter))
261 drawtext(out, xpos, ypos, filter->name, s->white);
262 xpos += strlen(filter->name) * 8 + 10;
263 drawtext(out, xpos, ypos, filter->filter->name, s->white);
265 for (int j = 0; j < filter->nb_inputs; j++) {
266 AVFilterLink *l = filter->inputs[j];
267 size_t frames = ff_inlink_queued_frames(l);
269 if (s->mode && !frames)
273 snprintf(buffer, sizeof(buffer)-1, "in%d: ", j);
274 drawtext(out, xpos, ypos, buffer, s->white);
275 xpos += strlen(buffer) * 8;
276 drawtext(out, xpos, ypos, l->src->name, s->white);
277 xpos += strlen(l->src->name) * 8 + 10;
278 draw_items(ctx, out, xpos, ypos, l, frames);
283 for (int j = 0; j < filter->nb_outputs; j++) {
284 AVFilterLink *l = filter->outputs[j];
285 size_t frames = ff_inlink_queued_frames(l);
287 if (s->mode && !frames)
291 snprintf(buffer, sizeof(buffer)-1, "out%d: ", j);
292 drawtext(out, xpos, ypos, buffer, s->white);
293 xpos += strlen(buffer) * 8;
294 drawtext(out, xpos, ypos, l->dst->name, s->white);
295 xpos += strlen(l->dst->name) * 8 + 10;
296 draw_items(ctx, out, xpos, ypos, l, frames);
304 return ff_filter_frame(outlink, out);
307 static int activate(AVFilterContext *ctx)
309 GraphMonitorContext *s = ctx->priv;
310 AVFilterLink *inlink = ctx->inputs[0];
311 AVFilterLink *outlink = ctx->outputs[0];
312 int64_t pts = AV_NOPTS_VALUE;
314 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
316 if (ff_inlink_queued_frames(inlink)) {
317 AVFrame *frame = NULL;
320 ret = ff_inlink_consume_frame(inlink, &frame);
325 av_frame_free(&frame);
329 if (pts != AV_NOPTS_VALUE) {
330 pts = av_rescale_q(pts, inlink->time_base, outlink->time_base);
331 if (s->pts < pts && ff_outlink_frame_wanted(outlink))
332 return create_frame(ctx, pts);
335 FF_FILTER_FORWARD_STATUS(inlink, outlink);
336 FF_FILTER_FORWARD_WANTED(outlink, inlink);
338 return FFERROR_NOT_READY;
341 static int config_output(AVFilterLink *outlink)
343 GraphMonitorContext *s = outlink->src->priv;
345 s->bg[3] = 255 * s->opacity;
346 s->white[0] = s->white[1] = s->white[2] = 255;
347 s->yellow[0] = s->yellow[1] = 255;
352 outlink->sample_aspect_ratio = (AVRational){1,1};
353 outlink->frame_rate = s->frame_rate;
354 outlink->time_base = av_inv_q(s->frame_rate);
359 #if CONFIG_GRAPHMONITOR_FILTER
361 AVFILTER_DEFINE_CLASS(graphmonitor);
363 static const AVFilterPad graphmonitor_inputs[] = {
366 .type = AVMEDIA_TYPE_VIDEO,
371 static const AVFilterPad graphmonitor_outputs[] = {
374 .type = AVMEDIA_TYPE_VIDEO,
375 .config_props = config_output,
380 AVFilter ff_vf_graphmonitor = {
381 .name = "graphmonitor",
382 .description = NULL_IF_CONFIG_SMALL("Show various filtergraph stats."),
383 .priv_size = sizeof(GraphMonitorContext),
384 .priv_class = &graphmonitor_class,
385 .query_formats = query_formats,
386 .activate = activate,
387 .inputs = graphmonitor_inputs,
388 .outputs = graphmonitor_outputs,
391 #endif // CONFIG_GRAPHMONITOR_FILTER
393 #if CONFIG_AGRAPHMONITOR_FILTER
395 #define agraphmonitor_options graphmonitor_options
396 AVFILTER_DEFINE_CLASS(agraphmonitor);
398 static const AVFilterPad agraphmonitor_inputs[] = {
401 .type = AVMEDIA_TYPE_AUDIO,
406 static const AVFilterPad agraphmonitor_outputs[] = {
409 .type = AVMEDIA_TYPE_VIDEO,
410 .config_props = config_output,
415 AVFilter ff_avf_agraphmonitor = {
416 .name = "agraphmonitor",
417 .description = NULL_IF_CONFIG_SMALL("Show various filtergraph stats."),
418 .priv_size = sizeof(GraphMonitorContext),
419 .priv_class = &agraphmonitor_class,
420 .query_formats = query_formats,
421 .activate = activate,
422 .inputs = agraphmonitor_inputs,
423 .outputs = agraphmonitor_outputs,
425 #endif // CONFIG_AGRAPHMONITOR_FILTER