From patchwork Sat Oct 27 19:25:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 10801 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 54A0744D7EB for ; Sat, 27 Oct 2018 22:25:35 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B69E2689EAB; Sat, 27 Oct 2018 22:25:06 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E0BF6688287 for ; Sat, 27 Oct 2018 22:24:59 +0300 (EEST) Received: by mail-ed1-f43.google.com with SMTP id t10-v6so4012421eds.12 for ; Sat, 27 Oct 2018 12:25:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=ur0Zjdu0ruDDiFjIPdE0gJZYMasfk5XUybRfAFV6vEA=; b=DwRn02lmhsYEpqzq/34PqBGtAdOtS2FScYj0LW1xXfO/vnSHq2HhBGTEwHCQeDs5fl sxpqgQ70TabRg+EurKH5f6bPj4x0GfX/dMUPVdGgHkKgF5QQO9ESs7TL1gGZjFBHH4oE ji0TbvCWpHjkiIXb2kkjkIeCnPM6Jm1p1vnnS/LMXOLVLWRg/oK5Oo/MTBqxA+EMKSiA 7J2GX7ehlpjEgbFTUV1ccjET1cdqkkapoO4VdOjA/EixSImOGiZLothEskQodZl20L9d rKcoNPG1ZEHQlAYQN/fp2jE4KaMjDJIiBYlhyT3548k1MK9gQN4c8F5cV15pZhu9xcli kKYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=ur0Zjdu0ruDDiFjIPdE0gJZYMasfk5XUybRfAFV6vEA=; b=Xh4xOGQzsfSh0HQRYzr26vLCcT8yLKgR1IBE0tX0ua810owaSkrqjTFHksrdb2TnRH 4SxWhu1m0DHd7FZxSOnCqWU2V9USpE/HJ1+OER75pEMPodDVRtendzLaub0ZwjClh9tX 4ahXguPL5mCzl9CAfpZBa/xby2hTHtVXDxMilZOGM7KL1uA1ktxY0U2AKcUcLJQqvktQ 8BQ6FsUBJb49WE1XTP+S60CgFxZaOJy4foiZXzsFC4zWwR6KuiArQQwy1D48jdnkHQsE 3f/kSm5/ebkrgO8kzO5geVL0xdUXCg01J/oyEcS53qqzEdG8V0NQUbPaI4tfyC74viKJ PzYQ== X-Gm-Message-State: AGRZ1gIpM9+Z9gHYHCeGwKuEOcLVKpyGz9QVDiHICoY3FwGs+SnrtApe WypoTReWaBBS8hun3apWa2NJUnJjBUY= X-Google-Smtp-Source: AJdET5c6c2ifaraPPniRUlljYigm6YeZIGZnKI3R/ACNxoh31ep8bTPY58+d7Y0mdBDmYtqkKQE2Zw== X-Received: by 2002:a17:906:a295:: with SMTP id i21-v6mr6086309ejz.86.1540668328908; Sat, 27 Oct 2018 12:25:28 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id j2-v6sm5056890eda.89.2018.10.27.12.25.27 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 27 Oct 2018 12:25:28 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sat, 27 Oct 2018 21:25:18 +0200 Message-Id: <20181027192518.21978-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add (a)graphmonitor filter(s) X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- libavfilter/Makefile | 2 + libavfilter/allfilters.c | 2 + libavfilter/f_graphmonitor.c | 272 +++++++++++++++++++++++++++++++++++ 3 files changed, 276 insertions(+) create mode 100644 libavfilter/f_graphmonitor.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index a98c64b7ce..c35cd8f422 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -240,6 +240,7 @@ OBJS-$(CONFIG_FSPP_FILTER) += vf_fspp.o OBJS-$(CONFIG_GBLUR_FILTER) += vf_gblur.o OBJS-$(CONFIG_GEQ_FILTER) += vf_geq.o OBJS-$(CONFIG_GRADFUN_FILTER) += vf_gradfun.o +OBJS-$(CONFIG_GRAPHMONITOR_FILTER) += f_graphmonitor.o OBJS-$(CONFIG_GREYEDGE_FILTER) += vf_colorconstancy.o OBJS-$(CONFIG_HALDCLUT_FILTER) += vf_lut3d.o framesync.o OBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o @@ -437,6 +438,7 @@ OBJS-$(CONFIG_NULLSINK_FILTER) += vsink_nullsink.o # multimedia filters OBJS-$(CONFIG_ABITSCOPE_FILTER) += avf_abitscope.o OBJS-$(CONFIG_ADRAWGRAPH_FILTER) += f_drawgraph.o +OBJS-$(CONFIG_AGRAPHMONITOR_FILTER) += f_graphmonitor.o OBJS-$(CONFIG_AHISTOGRAM_FILTER) += avf_ahistogram.o OBJS-$(CONFIG_APHASEMETER_FILTER) += avf_aphasemeter.o OBJS-$(CONFIG_AVECTORSCOPE_FILTER) += avf_avectorscope.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index b2cb58fc38..d5a211bda5 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -227,6 +227,7 @@ extern AVFilter ff_vf_fspp; extern AVFilter ff_vf_gblur; extern AVFilter ff_vf_geq; extern AVFilter ff_vf_gradfun; +extern AVFilter ff_vf_graphmonitor; extern AVFilter ff_vf_greyedge; extern AVFilter ff_vf_haldclut; extern AVFilter ff_vf_hflip; @@ -418,6 +419,7 @@ extern AVFilter ff_vsink_nullsink; /* multimedia filters */ extern AVFilter ff_avf_abitscope; extern AVFilter ff_avf_adrawgraph; +extern AVFilter ff_avf_agraphmonitor; extern AVFilter ff_avf_ahistogram; extern AVFilter ff_avf_aphasemeter; extern AVFilter ff_avf_avectorscope; diff --git a/libavfilter/f_graphmonitor.c b/libavfilter/f_graphmonitor.c new file mode 100644 index 0000000000..9d9d41fde7 --- /dev/null +++ b/libavfilter/f_graphmonitor.c @@ -0,0 +1,272 @@ +/* + * Copyright (c) 2018 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "float.h" + +#include "libavutil/pixdesc.h" +#include "libavutil/eval.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/opt.h" +#include "libavutil/xga_font_data.h" +#include "avfilter.h" +#include "filters.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct GraphMonitorContext { + const AVClass *class; + + int w, h; + float opacity; + + AVFrame *out; + uint8_t white[4]; + uint8_t yellow[4]; + uint8_t red[4]; + uint8_t bg[4]; +} GraphMonitorContext; + +#define OFFSET(x) offsetof(GraphMonitorContext, x) +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption graphmonitor_options[] = { + { "size", "set monitor size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS }, + { "s", "set monitor size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS }, + { "opacity", "set video opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=.9}, 0, 1, FLAGS }, + { "o", "set video opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=.9}, 0, 1, FLAGS }, + { NULL } +}; + +static int query_formats(AVFilterContext *ctx) +{ + AVFilterLink *outlink = ctx->outputs[0]; + static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_RGBA, + AV_PIX_FMT_NONE + }; + int ret; + + AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); + if ((ret = ff_formats_ref(fmts_list, &outlink->in_formats)) < 0) + return ret; + + return 0; +} + +static void clear_image(GraphMonitorContext *s, AVFrame *out, AVFilterLink *outlink) +{ + int bg = AV_RN32(s->bg); + + for (int i = 0; i < out->height; i++) + for (int j = 0; j < out->width; j++) + AV_WN32(out->data[0] + i * out->linesize[0] + j * 4, bg); +} + +static void drawtext(AVFrame *pic, int x, int y, const char *txt, uint8_t *color) +{ + const uint8_t *font; + int font_height; + int i; + + font = avpriv_cga_font, font_height = 8; + + if (y + 8 >= pic->height || + x + strlen(txt) * 8 >= pic->width) + return; + + for (i = 0; txt[i]; i++) { + int char_y, mask; + + uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8)*4; + for (char_y = 0; char_y < font_height; char_y++) { + for (mask = 0x80; mask; mask >>= 1) { + if (font[txt[i] * font_height + char_y] & mask) { + p[0] = color[0]; + p[1] = color[1]; + p[2] = color[2]; + } + p += 4; + } + p += pic->linesize[0] - 8 * 4; + } + } +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + GraphMonitorContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + AVFrame *out; + int xpos, ypos = 0; + int64_t pts = in->pts; + + av_frame_free(&in); + + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + clear_image(s, out, outlink); + + for (int i = 0; i < ctx->graph->nb_filters; i++) { + AVFilterContext *filter = ctx->graph->filters[i]; + char buffer[1024] = { 0 }; + + xpos = 0; + drawtext(out, xpos, ypos, filter->name, s->white); + xpos += strlen(filter->name) * 8 + 10; + drawtext(out, xpos, ypos, filter->filter->name, s->white); + ypos += 10; + for (int j = 0; j < filter->nb_inputs; j++) { + AVFilterLink *l = filter->inputs[j]; + size_t frames = ff_inlink_queued_frames(l); + + xpos = 10; + snprintf(buffer, sizeof(buffer)-1, "in%d: ", j); + drawtext(out, xpos, ypos, buffer, s->white); + xpos += strlen(buffer) * 10; + drawtext(out, xpos, ypos, l->src->name, s->white); + xpos += strlen(l->src->name) * 8 + 10; + if (l->type == AVMEDIA_TYPE_VIDEO) { + snprintf(buffer, sizeof(buffer)-1, "size: %dx%d | format: %s | queue: ", + l->w, l->h, av_get_pix_fmt_name(l->format)); + } else if (l->type == AVMEDIA_TYPE_AUDIO) { + snprintf(buffer, sizeof(buffer)-1, "channels: %d | format: %s | queue: ", + l->channels, av_get_sample_fmt_name(l->format)); + } + drawtext(out, xpos, ypos, buffer, s->white); + xpos += strlen(buffer) * 8; + snprintf(buffer, sizeof(buffer)-1, "%"PRId64, frames); + drawtext(out, xpos, ypos, buffer, frames >= 10 ? frames >= 50 ? s->red : s->yellow : s->white); + ypos += 10; + } + + ypos += 2; + for (int j = 0; j < filter->nb_outputs; j++) { + AVFilterLink *l = filter->outputs[j]; + size_t frames = ff_inlink_queued_frames(l); + + xpos = 10; + snprintf(buffer, sizeof(buffer)-1, "out%d: ", j); + drawtext(out, xpos, ypos, buffer, s->white); + xpos += strlen(buffer) * 10; + drawtext(out, xpos, ypos, l->dst->name, s->white); + xpos += strlen(l->dst->name) * 8 + 10; + if (l->type == AVMEDIA_TYPE_VIDEO) { + snprintf(buffer, sizeof(buffer)-1, "size: %dx%d | format: %s | queue: ", + l->w, l->h, av_get_pix_fmt_name(l->format)); + } else if (l->type == AVMEDIA_TYPE_AUDIO) { + snprintf(buffer, sizeof(buffer)-1, "channels: %d | format: %s | queue: ", + l->channels, av_get_sample_fmt_name(l->format)); + } + drawtext(out, xpos, ypos, buffer, s->white); + xpos += strlen(buffer) * 8; + snprintf(buffer, sizeof(buffer)-1, "%"PRId64, frames); + drawtext(out, xpos, ypos, buffer, frames >= 10 ? frames >= 50 ? s->red : s->yellow : s->white); + ypos += 10; + } + ypos += 5; + } + + out->pts = pts; + return ff_filter_frame(outlink, out); +} + +static int config_output(AVFilterLink *outlink) +{ + GraphMonitorContext *s = outlink->src->priv; + + s->bg[3] = 255 * s->opacity; + s->white[0] = s->white[1] = s->white[2] = 255; + s->yellow[0] = s->yellow[1] = 255; + s->red[0] = 255; + outlink->w = s->w; + outlink->h = s->h; + outlink->sample_aspect_ratio = (AVRational){1,1}; + + return 0; +} + +#if CONFIG_GRAPHMONITOR_FILTER + +AVFILTER_DEFINE_CLASS(graphmonitor); + +static const AVFilterPad graphmonitor_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + }, + { NULL } +}; + +static const AVFilterPad graphmonitor_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + }, + { NULL } +}; + +AVFilter ff_vf_graphmonitor = { + .name = "graphmonitor", + .description = NULL_IF_CONFIG_SMALL("Show various filtergraph stats."), + .priv_size = sizeof(GraphMonitorContext), + .priv_class = &graphmonitor_class, + .query_formats = query_formats, + .inputs = graphmonitor_inputs, + .outputs = graphmonitor_outputs, +}; + +#endif // CONFIG_GRAPHMONITOR_FILTER + +#if CONFIG_AGRAPHMONITOR_FILTER + +#define agraphmonitor_options graphmonitor_options +AVFILTER_DEFINE_CLASS(agraphmonitor); + +static const AVFilterPad agraphmonitor_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .filter_frame = filter_frame, + }, + { NULL } +}; + +static const AVFilterPad agraphmonitor_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + }, + { NULL } +}; + +AVFilter ff_avf_agraphmonitor = { + .name = "agraphmonitor", + .description = NULL_IF_CONFIG_SMALL("Show various filtergraph stats."), + .priv_size = sizeof(GraphMonitorContext), + .priv_class = &agraphmonitor_class, + .query_formats = query_formats, + .inputs = agraphmonitor_inputs, + .outputs = agraphmonitor_outputs, +}; +#endif // CONFIG_AGRAPHMONITOR_FILTER