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;
51 } GraphMonitorContext;
65 #define OFFSET(x) offsetof(GraphMonitorContext, x)
66 #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
68 static const AVOption graphmonitor_options[] = {
69 { "size", "set monitor size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, VF },
70 { "s", "set monitor size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, VF },
71 { "opacity", "set video opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=.9}, 0, 1, VF },
72 { "o", "set video opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=.9}, 0, 1, VF },
73 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, VF, "mode" },
74 { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, VF, "mode" },
75 { "full", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, VF, "mode" },
76 { "compact", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, VF, "mode" },
77 { "flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=MODE_QUEUE}, 0, INT_MAX, VF, "flags" },
78 { "f", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=MODE_QUEUE}, 0, INT_MAX, VF, "flags" },
79 { "queue", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_QUEUE}, 0, 0, VF, "flags" },
80 { "frame_count_in", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_FCOUT}, 0, 0, VF, "flags" },
81 { "frame_count_out", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_FCIN}, 0, 0, VF, "flags" },
82 { "pts", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_PTS}, 0, 0, VF, "flags" },
83 { "time", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_TIME}, 0, 0, VF, "flags" },
84 { "timebase", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_TB}, 0, 0, VF, "flags" },
85 { "format", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_FMT}, 0, 0, VF, "flags" },
86 { "size", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_SIZE}, 0, 0, VF, "flags" },
87 { "rate", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_RATE}, 0, 0, VF, "flags" },
88 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, VF },
89 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, VF },
93 static int query_formats(AVFilterContext *ctx)
95 AVFilterLink *outlink = ctx->outputs[0];
96 static const enum AVPixelFormat pix_fmts[] = {
102 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
103 if ((ret = ff_formats_ref(fmts_list, &outlink->in_formats)) < 0)
109 static void clear_image(GraphMonitorContext *s, AVFrame *out, AVFilterLink *outlink)
111 int bg = AV_RN32(s->bg);
113 for (int i = 0; i < out->height; i++)
114 for (int j = 0; j < out->width; j++)
115 AV_WN32(out->data[0] + i * out->linesize[0] + j * 4, bg);
118 static void drawtext(AVFrame *pic, int x, int y, const char *txt, uint8_t *color)
124 font = avpriv_cga_font, font_height = 8;
126 if (y + 8 >= pic->height ||
127 x + strlen(txt) * 8 >= pic->width)
130 for (i = 0; txt[i]; i++) {
133 uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8)*4;
134 for (char_y = 0; char_y < font_height; char_y++) {
135 for (mask = 0x80; mask; mask >>= 1) {
136 if (font[txt[i] * font_height + char_y] & mask) {
143 p += pic->linesize[0] - 8 * 4;
148 static int filter_have_queued(AVFilterContext *filter)
150 for (int j = 0; j < filter->nb_inputs; j++) {
151 AVFilterLink *l = filter->inputs[j];
152 size_t frames = ff_inlink_queued_frames(l);
158 for (int j = 0; j < filter->nb_outputs; j++) {
159 AVFilterLink *l = filter->outputs[j];
160 size_t frames = ff_inlink_queued_frames(l);
169 static void draw_items(AVFilterContext *ctx, AVFrame *out,
174 GraphMonitorContext *s = ctx->priv;
175 char buffer[1024] = { 0 };
177 if (s->flags & MODE_FMT) {
178 if (l->type == AVMEDIA_TYPE_VIDEO) {
179 snprintf(buffer, sizeof(buffer)-1, " | format: %s",
180 av_get_pix_fmt_name(l->format));
181 } else if (l->type == AVMEDIA_TYPE_AUDIO) {
182 snprintf(buffer, sizeof(buffer)-1, " | format: %s",
183 av_get_sample_fmt_name(l->format));
185 drawtext(out, xpos, ypos, buffer, s->white);
186 xpos += strlen(buffer) * 8;
188 if (s->flags & MODE_SIZE) {
189 if (l->type == AVMEDIA_TYPE_VIDEO) {
190 snprintf(buffer, sizeof(buffer)-1, " | size: %dx%d", l->w, l->h);
191 } else if (l->type == AVMEDIA_TYPE_AUDIO) {
192 snprintf(buffer, sizeof(buffer)-1, " | channels: %d", l->channels);
194 drawtext(out, xpos, ypos, buffer, s->white);
195 xpos += strlen(buffer) * 8;
197 if (s->flags & MODE_RATE) {
198 if (l->type == AVMEDIA_TYPE_VIDEO) {
199 snprintf(buffer, sizeof(buffer)-1, " | fps: %d/%d", l->frame_rate.num, l->frame_rate.den);
200 } else if (l->type == AVMEDIA_TYPE_AUDIO) {
201 snprintf(buffer, sizeof(buffer)-1, " | samplerate: %d", l->sample_rate);
203 drawtext(out, xpos, ypos, buffer, s->white);
204 xpos += strlen(buffer) * 8;
206 if (s->flags & MODE_TB) {
207 snprintf(buffer, sizeof(buffer)-1, " | tb: %d/%d", l->time_base.num, l->time_base.den);
208 drawtext(out, xpos, ypos, buffer, s->white);
209 xpos += strlen(buffer) * 8;
211 if (s->flags & MODE_QUEUE) {
212 snprintf(buffer, sizeof(buffer)-1, " | queue: ");
213 drawtext(out, xpos, ypos, buffer, s->white);
214 xpos += strlen(buffer) * 8;
215 snprintf(buffer, sizeof(buffer)-1, "%"SIZE_SPECIFIER, frames);
216 drawtext(out, xpos, ypos, buffer, frames > 0 ? frames >= 10 ? frames >= 50 ? s->red : s->yellow : s->green : s->white);
217 xpos += strlen(buffer) * 8;
219 if (s->flags & MODE_FCIN) {
220 snprintf(buffer, sizeof(buffer)-1, " | in: %"PRId64, l->frame_count_in);
221 drawtext(out, xpos, ypos, buffer, s->white);
222 xpos += strlen(buffer) * 8;
224 if (s->flags & MODE_FCOUT) {
225 snprintf(buffer, sizeof(buffer)-1, " | out: %"PRId64, l->frame_count_out);
226 drawtext(out, xpos, ypos, buffer, s->white);
227 xpos += strlen(buffer) * 8;
229 if (s->flags & MODE_PTS) {
230 snprintf(buffer, sizeof(buffer)-1, " | pts: %s", av_ts2str(l->current_pts_us));
231 drawtext(out, xpos, ypos, buffer, s->white);
232 xpos += strlen(buffer) * 8;
234 if (s->flags & MODE_TIME) {
235 snprintf(buffer, sizeof(buffer)-1, " | time: %s", av_ts2timestr(l->current_pts_us, &AV_TIME_BASE_Q));
236 drawtext(out, xpos, ypos, buffer, s->white);
237 xpos += strlen(buffer) * 8;
241 static int create_frame(AVFilterContext *ctx, int64_t pts)
243 GraphMonitorContext *s = ctx->priv;
244 AVFilterLink *outlink = ctx->outputs[0];
248 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
250 return AVERROR(ENOMEM);
252 clear_image(s, out, outlink);
254 for (int i = 0; i < ctx->graph->nb_filters; i++) {
255 AVFilterContext *filter = ctx->graph->filters[i];
256 char buffer[1024] = { 0 };
258 if (s->mode && !filter_have_queued(filter))
262 drawtext(out, xpos, ypos, filter->name, s->white);
263 xpos += strlen(filter->name) * 8 + 10;
264 drawtext(out, xpos, ypos, filter->filter->name, s->white);
266 for (int j = 0; j < filter->nb_inputs; j++) {
267 AVFilterLink *l = filter->inputs[j];
268 size_t frames = ff_inlink_queued_frames(l);
270 if (s->mode && !frames)
274 snprintf(buffer, sizeof(buffer)-1, "in%d: ", j);
275 drawtext(out, xpos, ypos, buffer, s->white);
276 xpos += strlen(buffer) * 8;
277 drawtext(out, xpos, ypos, l->src->name, s->white);
278 xpos += strlen(l->src->name) * 8 + 10;
279 draw_items(ctx, out, xpos, ypos, l, frames);
284 for (int j = 0; j < filter->nb_outputs; j++) {
285 AVFilterLink *l = filter->outputs[j];
286 size_t frames = ff_inlink_queued_frames(l);
288 if (s->mode && !frames)
292 snprintf(buffer, sizeof(buffer)-1, "out%d: ", j);
293 drawtext(out, xpos, ypos, buffer, s->white);
294 xpos += strlen(buffer) * 8;
295 drawtext(out, xpos, ypos, l->dst->name, s->white);
296 xpos += strlen(l->dst->name) * 8 + 10;
297 draw_items(ctx, out, xpos, ypos, l, frames);
305 return ff_filter_frame(outlink, out);
308 static int activate(AVFilterContext *ctx)
310 GraphMonitorContext *s = ctx->priv;
311 AVFilterLink *inlink = ctx->inputs[0];
312 AVFilterLink *outlink = ctx->outputs[0];
313 int64_t pts = AV_NOPTS_VALUE;
315 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
317 if (ff_inlink_queued_frames(inlink)) {
318 AVFrame *frame = NULL;
321 ret = ff_inlink_consume_frame(inlink, &frame);
326 av_frame_free(&frame);
330 if (pts != AV_NOPTS_VALUE) {
331 pts = av_rescale_q(pts, inlink->time_base, outlink->time_base);
332 if (s->pts == AV_NOPTS_VALUE)
337 if (s->pts < s->next_pts && ff_outlink_frame_wanted(outlink))
338 return create_frame(ctx, s->pts);
340 FF_FILTER_FORWARD_STATUS(inlink, outlink);
341 FF_FILTER_FORWARD_WANTED(outlink, inlink);
343 return FFERROR_NOT_READY;
346 static int config_output(AVFilterLink *outlink)
348 GraphMonitorContext *s = outlink->src->priv;
350 s->bg[3] = 255 * s->opacity;
351 s->white[0] = s->white[1] = s->white[2] = 255;
352 s->yellow[0] = s->yellow[1] = 255;
355 s->pts = AV_NOPTS_VALUE;
356 s->next_pts = AV_NOPTS_VALUE;
359 outlink->sample_aspect_ratio = (AVRational){1,1};
360 outlink->frame_rate = s->frame_rate;
361 outlink->time_base = av_inv_q(s->frame_rate);
366 #if CONFIG_GRAPHMONITOR_FILTER
368 AVFILTER_DEFINE_CLASS(graphmonitor);
370 static const AVFilterPad graphmonitor_inputs[] = {
373 .type = AVMEDIA_TYPE_VIDEO,
378 static const AVFilterPad graphmonitor_outputs[] = {
381 .type = AVMEDIA_TYPE_VIDEO,
382 .config_props = config_output,
387 AVFilter ff_vf_graphmonitor = {
388 .name = "graphmonitor",
389 .description = NULL_IF_CONFIG_SMALL("Show various filtergraph stats."),
390 .priv_size = sizeof(GraphMonitorContext),
391 .priv_class = &graphmonitor_class,
392 .query_formats = query_formats,
393 .activate = activate,
394 .inputs = graphmonitor_inputs,
395 .outputs = graphmonitor_outputs,
398 #endif // CONFIG_GRAPHMONITOR_FILTER
400 #if CONFIG_AGRAPHMONITOR_FILTER
402 #define agraphmonitor_options graphmonitor_options
403 AVFILTER_DEFINE_CLASS(agraphmonitor);
405 static const AVFilterPad agraphmonitor_inputs[] = {
408 .type = AVMEDIA_TYPE_AUDIO,
413 static const AVFilterPad agraphmonitor_outputs[] = {
416 .type = AVMEDIA_TYPE_VIDEO,
417 .config_props = config_output,
422 AVFilter ff_avf_agraphmonitor = {
423 .name = "agraphmonitor",
424 .description = NULL_IF_CONFIG_SMALL("Show various filtergraph stats."),
425 .priv_size = sizeof(GraphMonitorContext),
426 .priv_class = &agraphmonitor_class,
427 .query_formats = query_formats,
428 .activate = activate,
429 .inputs = agraphmonitor_inputs,
430 .outputs = agraphmonitor_outputs,
432 #endif // CONFIG_AGRAPHMONITOR_FILTER