[FFmpeg-devel] [PATCH v2 09/10] fftools/graphprint: Add execution graph printing
softworkz
ffmpegagent at gmail.com
Wed Apr 16 13:12:19 EEST 2025
From: softworkz <softworkz at hotmail.com>
The key benefits are:
- Different to other graph printing methods, this is outputting:
- all graphs with runtime state
(including auto-inserted filters)
- each graph with its inputs and outputs
- all filters with their in- and output pads
- all connections between all input- and output pads
- for each connection:
- the runtime-negotiated format and media type
- the hw context
- if video hw context, both: hw pixfmt + sw pixfmt
- Output can either be printed to stdout or written to specified file
- Output is machine-readable
- Use the same output implementation as ffprobe, supporting multiple
formats
Signed-off-by: softworkz <softworkz at hotmail.com>
---
doc/ffmpeg.texi | 10 +
fftools/Makefile | 17 +
fftools/ffmpeg.c | 4 +
fftools/ffmpeg.h | 3 +
fftools/ffmpeg_filter.c | 5 +
fftools/ffmpeg_opt.c | 13 +
fftools/graph/graphprint.c | 1102 +++++++++++++++++++++++++++++
fftools/graph/graphprint.h | 30 +
fftools/textformat/avtextformat.c | 2 +
fftools/textformat/avtextformat.h | 29 +
fftools/textformat/tf_mermaid.c | 655 +++++++++++++++++
fftools/textformat/tf_mermaid.h | 41 ++
12 files changed, 1911 insertions(+)
create mode 100644 fftools/graph/graphprint.c
create mode 100644 fftools/graph/graphprint.h
create mode 100644 fftools/textformat/tf_mermaid.c
create mode 100644 fftools/textformat/tf_mermaid.h
diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index 17ba876ea3..35675b5309 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -1394,6 +1394,16 @@ It is on by default, to explicitly disable it you need to specify @code{-nostats
@item -stats_period @var{time} (@emph{global})
Set period at which encoding progress/statistics are updated. Default is 0.5 seconds.
+ at item -print_graphs (@emph{global})
+Prints execution graph details to stderr in the format set via -print_graphs_format.
+
+ at item -print_graphs_file @var{filename} (@emph{global})
+Writes execution graph details to the specified file in the format set via -print_graphs_format.
+
+ at item -print_graphs_format @var{format} (@emph{global})
+Sets the output format (available formats are: default, compact, csv, flat, ini, json, xml, mermaid, mermaidhtml)
+The default format is json.
+
@item -progress @var{url} (@emph{global})
Send program-friendly progress information to @var{url}.
diff --git a/fftools/Makefile b/fftools/Makefile
index e9c9891c34..8d87ea8255 100644
--- a/fftools/Makefile
+++ b/fftools/Makefile
@@ -9,6 +9,8 @@ AVBASENAMES = ffmpeg ffplay ffprobe
ALLAVPROGS = $(AVBASENAMES:%=%$(PROGSSUF)$(EXESUF))
ALLAVPROGS_G = $(AVBASENAMES:%=%$(PROGSSUF)_g$(EXESUF))
+include $(SRC_PATH)/fftools/resources/Makefile
+
OBJS-ffmpeg += \
fftools/ffmpeg_dec.o \
fftools/ffmpeg_demux.o \
@@ -19,8 +21,21 @@ OBJS-ffmpeg += \
fftools/ffmpeg_mux_init.o \
fftools/ffmpeg_opt.o \
fftools/ffmpeg_sched.o \
+ fftools/graph/graphprint.o \
fftools/sync_queue.o \
fftools/thread_queue.o \
+ fftools/textformat/avtextformat.o \
+ fftools/textformat/tf_compact.o \
+ fftools/textformat/tf_default.o \
+ fftools/textformat/tf_flat.o \
+ fftools/textformat/tf_ini.o \
+ fftools/textformat/tf_json.o \
+ fftools/textformat/tf_mermaid.o \
+ fftools/textformat/tf_xml.o \
+ fftools/textformat/tw_avio.o \
+ fftools/textformat/tw_buffer.o \
+ fftools/textformat/tw_stdout.o \
+ $(OBJS-resman) \
OBJS-ffprobe += \
fftools/textformat/avtextformat.o \
@@ -29,10 +44,12 @@ OBJS-ffprobe += \
fftools/textformat/tf_flat.o \
fftools/textformat/tf_ini.o \
fftools/textformat/tf_json.o \
+ fftools/textformat/tf_mermaid.o \
fftools/textformat/tf_xml.o \
fftools/textformat/tw_avio.o \
fftools/textformat/tw_buffer.o \
fftools/textformat/tw_stdout.o \
+ $(OBJS-resman) \
OBJS-ffplay += fftools/ffplay_renderer.o
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index dc321fb4a2..6766ec209c 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -81,6 +81,7 @@
#include "ffmpeg.h"
#include "ffmpeg_sched.h"
#include "ffmpeg_utils.h"
+#include "graph/graphprint.h"
const char program_name[] = "ffmpeg";
const int program_birth_year = 2000;
@@ -308,6 +309,9 @@ const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
static void ffmpeg_cleanup(int ret)
{
+ if (print_graphs || print_graphs_file)
+ print_filtergraphs(filtergraphs, nb_filtergraphs, input_files, nb_input_files, output_files, nb_output_files);
+
if (do_benchmark) {
int64_t maxrss = getmaxrss() / 1024;
av_log(NULL, AV_LOG_INFO, "bench: maxrss=%"PRId64"KiB\n", maxrss);
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 5869979214..7fbf0ad532 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -717,6 +717,9 @@ extern float max_error_rate;
extern char *filter_nbthreads;
extern int filter_complex_nbthreads;
extern int vstats_version;
+extern int print_graphs;
+extern char *print_graphs_file;
+extern char *print_graphs_format;
extern int auto_conversion_filters;
extern const AVIOInterruptCB int_cb;
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index eab9487f97..b774606562 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -22,6 +22,7 @@
#include "ffmpeg.h"
#include "ffmpeg_filter.h"
+#include "graph/graphprint.h"
#include "libavfilter/avfilter.h"
#include "libavfilter/buffersink.h"
@@ -2983,6 +2984,10 @@ read_frames:
}
finish:
+
+ if (print_graphs || print_graphs_file)
+ print_filtergraph(fg, fgt.graph);
+
// EOF is normal termination
if (ret == AVERROR_EOF)
ret = 0;
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 6ec325f51e..3d1efe32f9 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -47,6 +47,7 @@
#include "libavutil/opt.h"
#include "libavutil/parseutils.h"
#include "libavutil/stereo3d.h"
+#include "graph/graphprint.h"
HWDevice *filter_hw_device;
@@ -75,6 +76,9 @@ float max_error_rate = 2.0/3;
char *filter_nbthreads;
int filter_complex_nbthreads = 0;
int vstats_version = 2;
+int print_graphs = 0;
+char *print_graphs_file = NULL;
+char *print_graphs_format = NULL;
int auto_conversion_filters = 1;
int64_t stats_period = 500000;
@@ -1735,6 +1739,15 @@ const OptionDef options[] = {
{ .func_arg = opt_filter_complex_script },
"deprecated, use -/filter_complex instead", "filename" },
#endif
+ { "print_graphs", OPT_TYPE_BOOL, 0,
+ { &print_graphs },
+ "print execution graph data to stderr" },
+ { "print_graphs_file", OPT_TYPE_STRING, 0,
+ { &print_graphs_file },
+ "write execution graph data to the specified file", "filename" },
+ { "print_graphs_format", OPT_TYPE_STRING, 0,
+ { &print_graphs_format },
+ "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml, mermaid, mermaidhtml)", "format" },
{ "auto_conversion_filters", OPT_TYPE_BOOL, OPT_EXPERT,
{ &auto_conversion_filters },
"enable automatic conversion filters globally" },
diff --git a/fftools/graph/graphprint.c b/fftools/graph/graphprint.c
new file mode 100644
index 0000000000..89c38d2e36
--- /dev/null
+++ b/fftools/graph/graphprint.c
@@ -0,0 +1,1102 @@
+/*
+ * Copyright (c) 2018-2025 - softworkz
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * output writers for filtergraph details
+ */
+
+#include <string.h>
+#include <stdatomic.h>
+
+#include "graphprint.h"
+
+#include <libavformat/url.h>
+
+#include "fftools/ffmpeg_filter.h"
+#include "fftools/ffmpeg_mux.h"
+
+#include "libavutil/avassert.h"
+#include "libavutil/avstring.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/dict.h"
+#include "libavutil/common.h"
+#include "libavfilter/avfilter.h"
+#include "libavutil/buffer.h"
+#include "libavutil/hwcontext.h"
+#include "fftools/textformat/avtextformat.h"
+#include "fftools/textformat/tf_mermaid.h"
+#include "fftools/resources/resman.h"
+
+typedef enum {
+ SECTION_ID_ROOT,
+ SECTION_ID_FILTERGRAPHS,
+ SECTION_ID_FILTERGRAPH,
+ SECTION_ID_GRAPH_INPUTS,
+ SECTION_ID_GRAPH_INPUT,
+ SECTION_ID_GRAPH_OUTPUTS,
+ SECTION_ID_GRAPH_OUTPUT,
+ SECTION_ID_FILTERS,
+ SECTION_ID_FILTER,
+ SECTION_ID_FILTER_INPUTS,
+ SECTION_ID_FILTER_INPUT,
+ SECTION_ID_FILTER_OUTPUTS,
+ SECTION_ID_FILTER_OUTPUT,
+ SECTION_ID_HWFRAMESCONTEXT,
+ SECTION_ID_INPUTFILES,
+ SECTION_ID_INPUTFILE,
+ SECTION_ID_INPUTSTREAMS,
+ SECTION_ID_INPUTSTREAM,
+ SECTION_ID_OUTPUTFILES,
+ SECTION_ID_OUTPUTFILE,
+ SECTION_ID_OUTPUTSTREAMS,
+ SECTION_ID_OUTPUTSTREAM,
+ SECTION_ID_STREAMLINKS,
+ SECTION_ID_STREAMLINK,
+ SECTION_ID_DECODERS,
+ SECTION_ID_DECODER,
+ SECTION_ID_ENCODERS,
+ SECTION_ID_ENCODER,
+} SectionID;
+
+static struct AVTextFormatSection sections[] = {
+ [SECTION_ID_ROOT] = { SECTION_ID_ROOT, "root", AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER, { SECTION_ID_FILTERGRAPHS, SECTION_ID_INPUTFILES, SECTION_ID_OUTPUTFILES, SECTION_ID_DECODERS, SECTION_ID_ENCODERS, SECTION_ID_STREAMLINKS, -1 } },
+
+ [SECTION_ID_FILTERGRAPHS] = { SECTION_ID_FILTERGRAPHS, "graphs", AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY, { SECTION_ID_FILTERGRAPH, -1 } },
+ [SECTION_ID_FILTERGRAPH] = { SECTION_ID_FILTERGRAPH, "graph", AV_TEXTFORMAT_SECTION_FLAG_HAS_VARIABLE_FIELDS, { SECTION_ID_GRAPH_INPUTS, SECTION_ID_GRAPH_OUTPUTS, SECTION_ID_FILTERS, -1 }, .element_name = "graph_info" },
+
+ [SECTION_ID_GRAPH_INPUTS] = { SECTION_ID_GRAPH_INPUTS, "graph_inputs", AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY, { SECTION_ID_GRAPH_INPUT, -1 }, .id_key = "id" },
+ [SECTION_ID_GRAPH_INPUT] = { SECTION_ID_GRAPH_INPUT, "graph_input", 0, { -1 }, .id_key = "filter_id" },
+
+ [SECTION_ID_GRAPH_OUTPUTS] = { SECTION_ID_GRAPH_OUTPUTS, "graph_outputs", AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY, { SECTION_ID_GRAPH_OUTPUT, -1 }, .id_key = "id" },
+ [SECTION_ID_GRAPH_OUTPUT] = { SECTION_ID_GRAPH_OUTPUT, "graph_output", 0, { -1 }, .id_key = "filter_id" },
+
+ [SECTION_ID_FILTERS] = { SECTION_ID_FILTERS, "filters", AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH, { SECTION_ID_FILTER, -1 }, .id_key = "graph_id" },
+ [SECTION_ID_FILTER] = { SECTION_ID_FILTER, "filter", AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE | AV_TEXTFORMAT_SECTION_PRINT_TAGS, { SECTION_ID_FILTER_INPUTS, SECTION_ID_FILTER_OUTPUTS, -1 }, .id_key = "filter_id" },
+
+ [SECTION_ID_FILTER_INPUTS] = { SECTION_ID_FILTER_INPUTS, "filter_inputs", AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY, { SECTION_ID_FILTER_INPUT, -1 } },
+ [SECTION_ID_FILTER_INPUT] = { SECTION_ID_FILTER_INPUT, "filter_input", AV_TEXTFORMAT_SECTION_FLAG_HAS_LINKS, { SECTION_ID_HWFRAMESCONTEXT, -1 }, .id_key = "filter_id", .src_id_key = "source_filter_id", .dest_id_key = "filter_id" },
+
+ [SECTION_ID_FILTER_OUTPUTS] = { SECTION_ID_FILTER_OUTPUTS, "filter_outputs", AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY, { SECTION_ID_FILTER_OUTPUT, -1 } },
+ [SECTION_ID_FILTER_OUTPUT] = { SECTION_ID_FILTER_OUTPUT, "filter_output", AV_TEXTFORMAT_SECTION_FLAG_HAS_LINKS, { SECTION_ID_HWFRAMESCONTEXT, -1 }, .id_key = "filter_id", .src_id_key = "filter_id", .dest_id_key = "dest_filter_id" },
+
+ [SECTION_ID_HWFRAMESCONTEXT] = { SECTION_ID_HWFRAMESCONTEXT, "hw_frames_context", 0, { -1 }, },
+
+ [SECTION_ID_INPUTFILES] = { SECTION_ID_INPUTFILES, "inputfiles", AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH, { SECTION_ID_INPUTFILE, -1 }, .id_key = "id" },
+ [SECTION_ID_INPUTFILE] = { SECTION_ID_INPUTFILE, "inputfile", AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH, { SECTION_ID_INPUTSTREAMS, -1 }, .id_key = "id" },
+
+ [SECTION_ID_INPUTSTREAMS] = { SECTION_ID_INPUTSTREAMS, "inputstreams", AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH, { SECTION_ID_INPUTSTREAM, -1 }, .id_key = "id" },
+ [SECTION_ID_INPUTSTREAM] = { SECTION_ID_INPUTSTREAM, "inputstream", AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE | AV_TEXTFORMAT_SECTION_PRINT_TAGS, { -1 }, .id_key = "id" },
+
+ [SECTION_ID_OUTPUTFILES] = { SECTION_ID_OUTPUTFILES, "outputfiles", AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH, { SECTION_ID_OUTPUTFILE, -1 }, .id_key = "id" },
+ [SECTION_ID_OUTPUTFILE] = { SECTION_ID_OUTPUTFILE, "outputfile", AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH, { SECTION_ID_OUTPUTSTREAMS, -1 }, .id_key = "id" },
+
+ [SECTION_ID_OUTPUTSTREAMS] = { SECTION_ID_OUTPUTSTREAMS, "outputstreams", AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH, { SECTION_ID_OUTPUTSTREAM, -1 }, .id_key = "id" },
+ [SECTION_ID_OUTPUTSTREAM] = { SECTION_ID_OUTPUTSTREAM, "outputstream", AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE | AV_TEXTFORMAT_SECTION_PRINT_TAGS, { -1 }, .id_key = "id", },
+
+ [SECTION_ID_STREAMLINKS] = { SECTION_ID_STREAMLINKS, "streamlinks", AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAMLINK, -1 } },
+ [SECTION_ID_STREAMLINK] = { SECTION_ID_STREAMLINK, "streamlink", AV_TEXTFORMAT_SECTION_FLAG_HAS_LINKS, { -1 }, .src_id_key = "source_stream_id", .dest_id_key = "dest_stream_id" },
+
+ [SECTION_ID_DECODERS] = { SECTION_ID_DECODERS, "decoders", AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH, { SECTION_ID_DECODER, -1 } },
+ [SECTION_ID_DECODER] = { SECTION_ID_DECODER, "decoder", AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE | AV_TEXTFORMAT_SECTION_PRINT_TAGS | AV_TEXTFORMAT_SECTION_FLAG_HAS_LINKS, { -1 }, .id_key = "id", .src_id_key = "source_id", .dest_id_key = "id" },
+
+ [SECTION_ID_ENCODERS] = { SECTION_ID_ENCODERS, "encoders", AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH, { SECTION_ID_ENCODER, -1 } },
+ [SECTION_ID_ENCODER] = { SECTION_ID_ENCODER, "encoder", AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE | AV_TEXTFORMAT_SECTION_PRINT_TAGS | AV_TEXTFORMAT_SECTION_FLAG_HAS_LINKS, { -1 }, .id_key = "id", .src_id_key = "id", .dest_id_key = "dest_id" },
+};
+
+typedef struct GraphPrintContext {
+ AVTextFormatContext *tfc;
+ AVTextWriterContext *wctx;
+ AVDiagramConfig diagram_config;
+
+ int id_prefix_num;
+ int is_diagram;
+ int opt_flags;
+ int skip_buffer_filters;
+ AVBPrint pbuf;
+
+} GraphPrintContext;
+
+/* Text Format API Shortcuts */
+#define print_id(k, v) print_sanizied_id(gpc, k, v, 0)
+#define print_id_noprefix(k, v) print_sanizied_id(gpc, k, v, 1)
+#define print_int(k, v) avtext_print_integer(tfc, k, v)
+#define print_int_opt(k, v) avtext_print_integer_flags(tfc, k, v, gpc->opt_flags)
+#define print_q(k, v, s) avtext_print_rational(tfc, k, v, s)
+#define print_str(k, v) avtext_print_string(tfc, k, v, 0)
+#define print_str_opt(k, v) avtext_print_string(tfc, k, v, gpc->opt_flags)
+#define print_val(k, v, u) avtext_print_unit_int(tfc, k, v, u)
+
+#define print_fmt(k, f, ...) do { \
+ av_bprint_clear(&gpc->pbuf); \
+ av_bprintf(&gpc->pbuf, f, __VA_ARGS__); \
+ avtext_print_string(tfc, k, gpc->pbuf.str, 0); \
+} while (0)
+
+#define print_fmt_opt(k, f, ...) do { \
+ av_bprint_clear(&gpc->pbuf); \
+ av_bprintf(&gpc->pbuf, f, __VA_ARGS__); \
+ avtext_print_string(tfc, k, gpc->pbuf.str, gpc->opt_flags); \
+} while (0)
+
+
+static atomic_int prefix_num = 0;
+
+static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
+{
+ unsigned i;
+ for (i = 0; src[i] && i < dst_size - 1; i++)
+ dst[i] = (char)av_toupper(src[i]);
+ dst[i] = 0;
+ return dst;
+}
+
+static char *get_extension(const char *url)
+{
+ const char *ext;
+ URLComponents uc;
+ int ret;
+ char scratchpad[128];
+
+ if (!url)
+ return 0;
+
+ ret = ff_url_decompose(&uc, url, NULL);
+ if (ret < 0)
+ return NULL;
+ for (ext = uc.query; *ext != '.' && ext > uc.path; ext--) {
+ }
+
+ if (*ext != '.')
+ return 0;
+ if (uc.query - ext > sizeof(scratchpad))
+ return NULL; //not enough memory in our scratchpad
+ av_strlcpy(scratchpad, ext + 1, uc.query - ext);
+
+ return av_strdup(scratchpad);
+}
+
+static void print_hwdevicecontext(const GraphPrintContext *gpc, const AVHWDeviceContext *hw_device_context)
+{
+ AVTextFormatContext *tfc = gpc->tfc;
+
+ if (!hw_device_context)
+ return;
+
+ print_int_opt("has_hw_device_context", 1);
+ print_str_opt("hw_device_type", av_hwdevice_get_type_name(hw_device_context->type));
+}
+
+static void print_hwframescontext(const GraphPrintContext *gpc, const AVHWFramesContext *hw_frames_context)
+{
+ AVTextFormatContext *tfc = gpc->tfc;
+ const AVPixFmtDescriptor *pix_desc_hw;
+ const AVPixFmtDescriptor *pix_desc_sw;
+
+ if (!hw_frames_context || !hw_frames_context->device_ctx)
+ return;
+
+ avtext_print_section_header(tfc, NULL, SECTION_ID_HWFRAMESCONTEXT);
+
+ print_int_opt("has_hw_frames_context", 1);
+ print_str("hw_device_type", av_hwdevice_get_type_name(hw_frames_context->device_ctx->type));
+
+ pix_desc_hw = av_pix_fmt_desc_get(hw_frames_context->format);
+ if (pix_desc_hw) {
+ print_str("hw_pixel_format", pix_desc_hw->name);
+ if (pix_desc_hw->alias)
+ print_str_opt("hw_pixel_format_alias", pix_desc_hw->alias);
+ }
+
+ pix_desc_sw = av_pix_fmt_desc_get(hw_frames_context->sw_format);
+ if (pix_desc_sw) {
+ print_str("sw_pixel_format", pix_desc_sw->name);
+ if (pix_desc_sw->alias)
+ print_str_opt("sw_pixel_format_alias", pix_desc_sw->alias);
+ }
+
+ print_int_opt("width", hw_frames_context->width);
+ print_int_opt("height", hw_frames_context->height);
+ print_int_opt("initial_pool_size", hw_frames_context->initial_pool_size);
+
+ avtext_print_section_footer(tfc); // SECTION_ID_HWFRAMESCONTEXT
+}
+
+static void print_link(GraphPrintContext *gpc, AVFilterLink *link)
+{
+ AVTextFormatContext *tfc = gpc->tfc;
+ AVBufferRef *hw_frames_ctx;
+ char layout_string[64];
+
+ if (!link)
+ return;
+
+ hw_frames_ctx = avfilter_link_get_hw_frames_ctx(link);
+
+ print_str_opt("media_type", av_get_media_type_string(link->type));
+
+ switch (link->type) {
+ case AVMEDIA_TYPE_VIDEO:
+
+ if (hw_frames_ctx && hw_frames_ctx->data) {
+ AVHWFramesContext * hwfctx = (AVHWFramesContext *)hw_frames_ctx->data;
+ const AVPixFmtDescriptor *pix_desc_hw = av_pix_fmt_desc_get(hwfctx->format);
+ const AVPixFmtDescriptor *pix_desc_sw = av_pix_fmt_desc_get(hwfctx->sw_format);
+ if (pix_desc_hw && pix_desc_sw)
+ print_fmt("format", "%s | %s", pix_desc_hw->name, pix_desc_sw->name);
+ } else {
+ print_str("format", av_x_if_null(av_get_pix_fmt_name(link->format), "?"));
+ }
+
+ if (link->w && link->h) {
+ if (tfc->show_value_unit) {
+ print_fmt("size", "%dx%d", link->w, link->h);
+ } else {
+ print_int("width", link->w);
+ print_int("height", link->h);
+ }
+ }
+
+ print_q("sar", link->sample_aspect_ratio, ':');
+
+ if (link->color_range != AVCOL_RANGE_UNSPECIFIED)
+ print_str_opt("color_range", av_color_range_name(link->color_range));
+
+ if (link->colorspace != AVCOL_SPC_UNSPECIFIED)
+ print_str("color_space", av_color_space_name(link->colorspace));
+ break;
+
+ case AVMEDIA_TYPE_SUBTITLE:
+ ////print_str("format", av_x_if_null(av_get_subtitle_fmt_name(link->format), "?"));
+
+ if (link->w && link->h) {
+ if (tfc->show_value_unit) {
+ print_fmt("size", "%dx%d", link->w, link->h);
+ } else {
+ print_int("width", link->w);
+ print_int("height", link->h);
+ }
+ }
+
+ break;
+
+ case AVMEDIA_TYPE_AUDIO:
+ av_channel_layout_describe(&link->ch_layout, layout_string, sizeof(layout_string));
+ print_str("channel_layout", layout_string);
+ print_val("channels", link->ch_layout.nb_channels, "ch");
+ if (tfc->show_value_unit)
+ print_fmt("sample_rate", "%d.1 kHz", link->sample_rate / 1000);
+ else
+ print_val("sample_rate", link->sample_rate, "Hz");
+
+ break;
+ }
+
+ print_fmt_opt("sample_rate", "%d/%d", link->time_base.num, link->time_base.den);
+
+ if (hw_frames_ctx && hw_frames_ctx->data)
+ print_hwframescontext(gpc, (AVHWFramesContext *)hw_frames_ctx->data);
+}
+
+static char sanitize_char(const char c)
+{
+ if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
+ return c;
+ return '_';
+}
+
+static void print_sanizied_id(const GraphPrintContext *gpc, const char *key, const char *id_str, int skip_prefix)
+{
+ AVTextFormatContext *tfc = gpc->tfc;
+ AVBPrint buf;
+
+ if (!key || !id_str)
+ return;
+
+ av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
+
+ if (!skip_prefix)
+ av_bprintf(&buf, "G%d_", gpc->id_prefix_num);
+
+ // sanizize section id
+ for (const char *p = id_str; *p; p++)
+ av_bprint_chars(&buf, sanitize_char(*p), 1);
+
+ print_str(key, buf.str);
+
+ av_bprint_finalize(&buf, NULL);
+}
+
+static void print_section_header_id(const GraphPrintContext *gpc, int section_id, const char *id_str, int skip_prefix)
+{
+ AVTextFormatContext *tfc = gpc->tfc;
+ AVTextFormatSectionContext sec_ctx = { 0 };
+ AVBPrint buf;
+
+ if (!id_str)
+ return;
+
+ av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
+
+ if (!skip_prefix)
+ av_bprintf(&buf, "G%d_", gpc->id_prefix_num);
+
+ // sanizize section id
+ for (const char *p = id_str; *p; p++)
+ av_bprint_chars(&buf, sanitize_char(*p), 1);
+
+ sec_ctx.context_id = buf.str;
+
+ avtext_print_section_header(tfc, &sec_ctx, section_id);
+
+ av_bprint_finalize(&buf, NULL);
+}
+
+static const char *get_filterpad_name(const AVFilterPad *pad)
+{
+ return pad ? avfilter_pad_get_name(pad, 0) : "pad";
+}
+
+static void print_filter(GraphPrintContext *gpc, const AVFilterContext *filter, AVDictionary *input_map, AVDictionary *output_map)
+{
+ AVTextFormatContext *tfc = gpc->tfc;
+ AVTextFormatSectionContext sec_ctx = { 0 };
+
+ print_section_header_id(gpc, SECTION_ID_FILTER, filter->name, 0);
+
+ ////print_id("filter_id", filter->name);
+
+ if (filter->filter) {
+ print_str("filter_name", filter->filter->name);
+ print_str_opt("description", filter->filter->description);
+ print_int_opt("nb_inputs", filter->nb_inputs);
+ print_int_opt("nb_outputs", filter->nb_outputs);
+ }
+
+ if (filter->hw_device_ctx) {
+ AVHWDeviceContext *device_context = (AVHWDeviceContext *)filter->hw_device_ctx->data;
+ print_hwdevicecontext(gpc, device_context);
+ if (filter->extra_hw_frames > 0)
+ print_int("extra_hw_frames", filter->extra_hw_frames);
+ }
+
+ avtext_print_section_header(tfc, NULL, SECTION_ID_FILTER_INPUTS);
+
+ for (unsigned i = 0; i < filter->nb_inputs; i++) {
+ AVDictionaryEntry *dic_entry;
+ AVFilterLink *link = filter->inputs[i];
+
+ sec_ctx.context_type = av_get_media_type_string(link->type);
+ avtext_print_section_header(tfc, &sec_ctx, SECTION_ID_FILTER_INPUT);
+ sec_ctx.context_type = NULL;
+
+ print_int_opt("input_index", i);
+ print_str_opt("pad_name", get_filterpad_name(link->dstpad));;
+
+ dic_entry = av_dict_get(input_map, link->src->name, NULL, 0);
+ if (dic_entry) {
+ char buf[256];
+ (void)snprintf(buf, sizeof(buf), "in_%s", dic_entry->value);
+ print_id_noprefix("source_filter_id", buf);
+ } else {
+ print_id("source_filter_id", link->src->name);
+ }
+
+ print_str_opt("source_pad_name", get_filterpad_name(link->srcpad));
+ print_id("filter_id", filter->name);
+
+ print_link(gpc, link);
+
+ avtext_print_section_footer(tfc); // SECTION_ID_FILTER_INPUT
+ }
+
+ avtext_print_section_footer(tfc); // SECTION_ID_FILTER_INPUTS
+
+ avtext_print_section_header(tfc, NULL, SECTION_ID_FILTER_OUTPUTS);
+
+ for (unsigned i = 0; i < filter->nb_outputs; i++) {
+ AVDictionaryEntry *dic_entry;
+ AVFilterLink *link = filter->outputs[i];
+ char buf[256];
+
+ sec_ctx.context_type = av_get_media_type_string(link->type);
+ avtext_print_section_header(tfc, &sec_ctx, SECTION_ID_FILTER_OUTPUT);
+ sec_ctx.context_type = NULL;
+
+ dic_entry = av_dict_get(output_map, link->dst->name, NULL, 0);
+ if (dic_entry) {
+ (void)snprintf(buf, sizeof(buf), "out_%s", dic_entry->value);
+ print_id_noprefix("dest_filter_id", buf);
+ } else {
+ print_id("dest_filter_id", link->dst->name);
+ }
+
+ print_int_opt("output_index", i);
+ print_str_opt("pad_name", get_filterpad_name(link->srcpad));
+ ////print_id("dest_filter_id", link->dst->name);
+ print_str_opt("dest_pad_name", get_filterpad_name(link->dstpad));
+ print_id("filter_id", filter->name);
+
+ print_link(gpc, link);
+
+ avtext_print_section_footer(tfc); // SECTION_ID_FILTER_OUTPUT
+ }
+
+ avtext_print_section_footer(tfc); // SECTION_ID_FILTER_OUTPUTS
+
+ avtext_print_section_footer(tfc); // SECTION_ID_FILTER
+}
+
+static void init_sections(void)
+{
+ for (unsigned i = 0; i < FF_ARRAY_ELEMS(sections); i++)
+ sections[i].show_all_entries = 1;
+}
+
+static void print_filtergraph_single(GraphPrintContext *gpc, FilterGraph *fg, AVFilterGraph *graph)
+{
+ AVTextFormatContext *tfc = gpc->tfc;
+ FilterGraphPriv *fgp = fgp_from_fg(fg);
+ AVDictionary *input_map = NULL;
+ AVDictionary *output_map = NULL;
+
+ print_int("graph_index", fg->index);
+ print_fmt("name", "Graph %d.%d", gpc->id_prefix_num, fg->index);
+ print_fmt("id", "Graph_%d_%d", gpc->id_prefix_num, fg->index);
+ print_str("description", fgp->graph_desc);
+
+ print_section_header_id(gpc, SECTION_ID_GRAPH_INPUTS, "Input_File", 0);
+
+ for (int i = 0; i < fg->nb_inputs; i++) {
+ InputFilterPriv *ifilter = ifp_from_ifilter(fg->inputs[i]);
+ enum AVMediaType media_type = ifilter->type;
+
+ avtext_print_section_header(tfc, NULL, SECTION_ID_GRAPH_INPUT);
+
+ print_int("input_index", ifilter->index);
+
+ if (ifilter->linklabel)
+ print_str("link_label", (const char*)ifilter->linklabel);
+
+ if (ifilter->filter) {
+ print_id("filter_id", ifilter->filter->name);
+ print_str("filter_name", ifilter->filter->filter->name);
+ }
+
+ if (ifilter->linklabel && ifilter->filter)
+ av_dict_set(&input_map, ifilter->filter->name, (const char *)ifilter->linklabel, 0);
+ else if (ifilter->opts.name && ifilter->filter)
+ av_dict_set(&input_map, ifilter->filter->name, (const char *)ifilter->opts.name, 0);
+
+ print_str("media_type", av_get_media_type_string(media_type));
+
+ avtext_print_section_footer(tfc); // SECTION_ID_GRAPH_INPUT
+ }
+
+ avtext_print_section_footer(tfc); // SECTION_ID_GRAPH_INPUTS
+
+ print_section_header_id(gpc, SECTION_ID_GRAPH_OUTPUTS, "Output_File", 0);
+
+ for (int i = 0; i < fg->nb_outputs; i++) {
+ OutputFilterPriv *ofilter = ofp_from_ofilter(fg->outputs[i]);
+
+ avtext_print_section_header(tfc, NULL, SECTION_ID_GRAPH_OUTPUT);
+
+ print_int("output_index", ofilter->index);
+
+ print_str("name", ofilter->name);
+
+ if (fg->outputs[i]->linklabel)
+ print_str("link_label", (const char*)fg->outputs[i]->linklabel);
+
+ if (ofilter->filter) {
+ print_id("filter_id", ofilter->filter->name);
+ print_str("filter_name", ofilter->filter->filter->name);
+ }
+
+ if (ofilter->name && ofilter->filter)
+ av_dict_set(&output_map, ofilter->filter->name, ofilter->name, 0);
+
+
+ print_str("media_type", av_get_media_type_string(fg->outputs[i]->type));
+
+ avtext_print_section_footer(tfc); // SECTION_ID_GRAPH_OUTPUT
+ }
+
+ avtext_print_section_footer(tfc); // SECTION_ID_GRAPH_OUTPUTS
+
+ if (graph) {
+ AVTextFormatSectionContext sec_ctx = { 0 };
+
+ sec_ctx.context_id = av_asprintf("Graph_%d_%d", gpc->id_prefix_num, fg->index);
+
+ avtext_print_section_header(tfc, &sec_ctx, SECTION_ID_FILTERS);
+
+ if (gpc->is_diagram) {
+ print_fmt("name", "Graph %d.%d", gpc->id_prefix_num, fg->index);
+ print_str("description", fgp->graph_desc);
+ print_str("id", sec_ctx.context_id);
+ }
+
+ av_freep(&sec_ctx.context_id);
+
+ for (unsigned i = 0; i < graph->nb_filters; i++) {
+ AVFilterContext *filter = graph->filters[i];
+
+ if (gpc->skip_buffer_filters) {
+ if (av_dict_get(input_map, filter->name, NULL, 0))
+ continue;
+ if (av_dict_get(output_map, filter->name, NULL, 0))
+ continue;
+ }
+
+ sec_ctx.context_id = filter->name;
+
+ print_filter(gpc, filter, input_map, output_map);
+ }
+
+ avtext_print_section_footer(tfc); // SECTION_ID_FILTERS
+ }
+
+ // Clean up dictionaries
+ av_dict_free(&input_map);
+ av_dict_free(&output_map);
+}
+
+static int print_streams(GraphPrintContext *gpc, InputFile **ifiles, int nb_ifiles, OutputFile **ofiles, int nb_ofiles)
+{
+ AVTextFormatContext *tfc = gpc->tfc;
+ AVBPrint buf;
+ AVTextFormatSectionContext sec_ctx = { 0 };
+
+ sec_ctx.context_id = "Inputs";
+
+ av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
+
+ print_section_header_id(gpc, SECTION_ID_INPUTFILES, "Inputs", 0);
+
+ for (int n = nb_ifiles - 1; n >= 0; n--) {
+ InputFile *ifi = ifiles[n];
+ AVFormatContext *fc = ifi->ctx;
+
+ sec_ctx.context_id = av_asprintf("Input_%d", n);
+ avtext_print_section_header(tfc, &sec_ctx, SECTION_ID_INPUTFILE);
+ av_freep(&sec_ctx.context_id);
+
+ print_fmt("index", "%d", ifi->index);
+
+ if (fc) {
+ print_str("demuxer_name", fc->iformat->name);
+ if (fc->url) {
+ char *extension = get_extension(fc->url);
+ if (extension) {
+ print_str("file_extension", extension);
+ av_freep(&extension);
+ }
+ print_str("url", fc->url);
+ }
+ }
+
+ sec_ctx.context_id = av_asprintf("InputStreams_%d", n);
+
+ avtext_print_section_header(tfc, &sec_ctx, SECTION_ID_INPUTSTREAMS);
+
+ av_freep(&sec_ctx.context_id);
+
+ for (int i = 0; i < ifi->nb_streams; i++) {
+ InputStream *ist = ifi->streams[i];
+ const AVCodecDescriptor *codec_desc;
+
+ if (!ist || !ist->par)
+ continue;
+
+ codec_desc = avcodec_descriptor_get(ist->par->codec_id);
+
+ sec_ctx.context_id = av_asprintf("r_in_%d_%d", n, i);
+
+ sec_ctx.context_type = av_get_media_type_string(ist->par->codec_type);
+
+ avtext_print_section_header(tfc, &sec_ctx, SECTION_ID_INPUTSTREAM);
+ av_freep(&sec_ctx.context_id);
+ sec_ctx.context_type = NULL;
+
+ av_bprint_clear(&buf);
+
+ print_fmt("id", "r_in_%d_%d", n, i);
+
+ if (codec_desc && codec_desc->name) {
+ ////av_bprintf(&buf, "%s", upcase_string(char_buf, sizeof(char_buf), codec_desc->long_name));
+ av_bprintf(&buf, "%s", codec_desc->long_name);
+ } else if (ist->dec) {
+ char char_buf[256];
+ av_bprintf(&buf, "%s", upcase_string(char_buf, sizeof(char_buf), ist->dec->name));
+ } else if (ist->par->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
+ av_bprintf(&buf, "%s", "Attachment");
+ } else if (ist->par->codec_type == AVMEDIA_TYPE_DATA) {
+ av_bprintf(&buf, "%s", "Data");
+ }
+
+ print_fmt("name", "%s", buf.str);
+ print_fmt("index", "%d", ist->index);
+
+ if (ist->dec)
+ print_str_opt("media_type", av_get_media_type_string(ist->par->codec_type));
+
+ avtext_print_section_footer(tfc); // SECTION_ID_INPUTSTREAM
+ }
+
+ avtext_print_section_footer(tfc); // SECTION_ID_INPUTSTREAMS
+ avtext_print_section_footer(tfc); // SECTION_ID_INPUTFILE
+ }
+
+ avtext_print_section_footer(tfc); // SECTION_ID_INPUTFILES
+
+
+ print_section_header_id(gpc, SECTION_ID_DECODERS, "Decoders", 0);
+
+ for (int n = 0; n < nb_ifiles; n++) {
+ InputFile *ifi = ifiles[n];
+
+ for (int i = 0; i < ifi->nb_streams; i++) {
+ InputStream *ist = ifi->streams[i];
+
+ if (!ist->decoder)
+ continue;
+
+ sec_ctx.context_id = av_asprintf("in_%d_%d", n, i);
+ sec_ctx.context_type = av_get_media_type_string(ist->par->codec_type);
+ sec_ctx.context_flags = 2;
+
+ avtext_print_section_header(tfc, &sec_ctx, SECTION_ID_DECODER);
+ av_freep(&sec_ctx.context_id);
+ sec_ctx.context_type = NULL;
+ sec_ctx.context_flags = 0;
+
+ av_bprint_clear(&buf);
+
+ print_fmt("source_id", "r_in_%d_%d", n, i);
+ print_fmt("id", "in_%d_%d", n, i);
+
+ ////av_bprintf(&buf, "%s", upcase_string(char_buf, sizeof(char_buf), ist->dec->name));
+ print_fmt("name", "%s", ist->dec->name);
+
+ print_str_opt("media_type", av_get_media_type_string(ist->par->codec_type));
+
+ avtext_print_section_footer(tfc); // SECTION_ID_DECODER
+ }
+ }
+
+ avtext_print_section_footer(tfc); // SECTION_ID_DECODERS
+
+
+ print_section_header_id(gpc, SECTION_ID_ENCODERS, "Encoders", 0);
+
+ for (int n = 0; n < nb_ofiles; n++) {
+ OutputFile *of = ofiles[n];
+
+ for (int i = 0; i < of->nb_streams; i++) {
+ OutputStream *ost = of->streams[i];
+ ////const AVCodecDescriptor *codec_desc;
+
+ if (!ost || !ost->st || !ost->st->codecpar || !ost->enc)
+ continue;
+
+ ////codec_desc = avcodec_descriptor_get(ost->st->codecpar->codec_id);
+
+ sec_ctx.context_id = av_asprintf("out__%d_%d", n, i);
+ sec_ctx.context_type = av_get_media_type_string(ost->type);
+ sec_ctx.context_flags = 2;
+
+ avtext_print_section_header(tfc, &sec_ctx, SECTION_ID_ENCODER);
+ av_freep(&sec_ctx.context_id);
+ sec_ctx.context_type = NULL;
+ sec_ctx.context_flags = 0;
+
+ av_bprint_clear(&buf);
+
+ print_fmt("id", "out__%d_%d", n, i);
+ print_fmt("dest_id", "r_out__%d_%d", n, i);
+
+ print_fmt("name", "%s", ost->enc->enc_ctx->av_class->item_name(ost->enc->enc_ctx));
+
+ print_str_opt("media_type", av_get_media_type_string(ost->type));
+
+ avtext_print_section_footer(tfc); // SECTION_ID_ENCODER
+ }
+ }
+
+ avtext_print_section_footer(tfc); // SECTION_ID_ENCODERS
+
+
+ print_section_header_id(gpc, SECTION_ID_OUTPUTFILES, "Outputs", 0);
+
+ for (int n = nb_ofiles - 1; n >= 0; n--) {
+ OutputFile *of = ofiles[n];
+ Muxer *muxer = (Muxer *)of;
+
+ if (!muxer->fc)
+ continue;
+
+ sec_ctx.context_id = av_asprintf("Output_%d", n);
+
+ avtext_print_section_header(tfc, &sec_ctx, SECTION_ID_OUTPUTFILE);
+
+ av_freep(&sec_ctx.context_id);
+
+ ////print_str_opt("index", av_get_media_type_string(of->index));
+ print_fmt("index", "%d", of->index);
+ ////print_str("url", of->url);
+ print_str("muxer_name", muxer->fc->oformat->name);
+ if (of->url) {
+ char *extension = get_extension(of->url);
+ if (extension) {
+ print_str("file_extension", extension);
+ av_freep(&extension);
+ }
+ print_str("url", of->url);
+ }
+
+ sec_ctx.context_id = av_asprintf("OutputStreams_%d", n);
+
+ avtext_print_section_header(tfc, &sec_ctx, SECTION_ID_OUTPUTSTREAMS);
+
+ for (int i = 0; i < of->nb_streams; i++) {
+ OutputStream *ost = of->streams[i];
+ const AVCodecDescriptor *codec_desc = avcodec_descriptor_get(ost->st->codecpar->codec_id);
+
+ sec_ctx.context_id = av_asprintf("r_out__%d_%d", n, i);
+ sec_ctx.context_type = av_get_media_type_string(ost->type);
+ avtext_print_section_header(tfc, &sec_ctx, SECTION_ID_OUTPUTSTREAM);
+ av_freep(&sec_ctx.context_id);
+ sec_ctx.context_type = NULL;
+
+ av_bprint_clear(&buf);
+
+ print_fmt("id", "r_out__%d_%d", n, i);
+
+ if (codec_desc && codec_desc->name) {
+ av_bprintf(&buf, "%s", codec_desc->long_name);
+ } else {
+ av_bprintf(&buf, "%s", "unknown");
+ }
+
+ print_fmt("name", "%s", buf.str);
+ print_fmt("index", "%d", ost->index);
+
+ print_str_opt("media_type", av_get_media_type_string(ost->type));
+
+ avtext_print_section_footer(tfc); // SECTION_ID_OUTPUTSTREAM
+ }
+
+ avtext_print_section_footer(tfc); // SECTION_ID_OUTPUTSTREAMS
+ avtext_print_section_footer(tfc); // SECTION_ID_OUTPUTFILE
+ }
+
+ avtext_print_section_footer(tfc); // SECTION_ID_OUTPUTFILES
+
+
+ avtext_print_section_header(tfc, NULL, SECTION_ID_STREAMLINKS);
+
+ for (int n = 0; n < nb_ofiles; n++) {
+ OutputFile *of = ofiles[n];
+
+ for (int i = 0; i < of->nb_streams; i++) {
+ OutputStream *ost = of->streams[i];
+
+ if (ost->ist && !ost->filter) {
+ sec_ctx.context_type = av_get_media_type_string(ost->type);
+ avtext_print_section_header(tfc, &sec_ctx, SECTION_ID_STREAMLINK);
+ sec_ctx.context_type = NULL;
+
+ if (ost->enc) {
+ print_fmt("dest_stream_id", "out__%d_%d", n, i);
+ print_fmt("source_stream_id", "in_%d_%d", ost->ist->file->index, ost->ist->index);
+ print_str("operation", "Transcode");
+ } else {
+ print_fmt("dest_stream_id", "r_out__%d_%d", n, i);
+ print_fmt("source_stream_id", "r_in_%d_%d", ost->ist->file->index, ost->ist->index);
+ print_str("operation", "Stream Copy");
+ }
+
+ print_str_opt("media_type", av_get_media_type_string(ost->type));
+
+ avtext_print_section_footer(tfc); // SECTION_ID_STREAMLINK
+ }
+ }
+ }
+
+ avtext_print_section_footer(tfc); // SECTION_ID_STREAMLINKS
+
+ return 0;
+}
+
+
+static void uninit_graphprint(GraphPrintContext *gpc)
+{
+ if (gpc->tfc)
+ avtext_context_close(&gpc->tfc);
+
+ if (gpc->wctx)
+ avtextwriter_context_close(&gpc->wctx);
+
+ // Finalize the print buffer if it was initialized
+ av_bprint_finalize(&gpc->pbuf, NULL);
+}
+
+static int init_graphprint(GraphPrintContext **pgpc, AVBPrint *target_buf)
+{
+ const AVTextFormatter *text_formatter;
+ AVTextFormatContext *tfc = NULL;
+ AVTextWriterContext *wctx = NULL;
+ GraphPrintContext *gpc = NULL;
+ char *w_args = NULL;
+ char *w_name;
+ int ret;
+
+ init_sections();
+ *pgpc = NULL;
+
+ av_bprint_init(target_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
+
+ if (!print_graphs_format)
+ print_graphs_format = av_strdup("json");
+ if (!print_graphs_format) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ w_name = av_strtok(print_graphs_format, "=", &w_args);
+ if (!w_name) {
+ av_log(NULL, AV_LOG_ERROR, "No name specified for the filter graph output format\n");
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+
+ text_formatter = avtext_get_formatter_by_name(w_name);
+ if (!text_formatter) {
+ av_log(NULL, AV_LOG_ERROR, "Unknown filter graph output format with name '%s'\n", w_name);
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+
+ ret = avtextwriter_create_buffer(&wctx, target_buf);
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_ERROR, "avtextwriter_create_buffer failed. Error code %d\n", ret);
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+
+ ret = avtext_context_open(&tfc, text_formatter, wctx, w_args, sections, FF_ARRAY_ELEMS(sections), 0, 0, 0, 0, -1, NULL);
+ if (ret < 0) {
+ goto fail;
+ }
+
+ gpc = av_mallocz(sizeof(GraphPrintContext));
+ if (!gpc) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ gpc->wctx = wctx;
+ gpc->tfc = tfc;
+ av_bprint_init(&gpc->pbuf, 0, AV_BPRINT_SIZE_UNLIMITED);
+
+ gpc->id_prefix_num = atomic_fetch_add(&prefix_num, 1);
+ gpc->is_diagram = !!(tfc->formatter->flags & AV_TEXTFORMAT_FLAG_IS_DIAGRAM_FORMATTER);
+ if (gpc->is_diagram) {
+ tfc->show_value_unit = 1;
+ tfc->show_optional_fields = -1;
+ gpc->opt_flags = AV_TEXTFORMAT_PRINT_STRING_OPTIONAL;
+ gpc->skip_buffer_filters = 1;
+ ////} else {
+ //// gpc->opt_flags = AV_TEXTFORMAT_PRINT_STRING_OPTIONAL;
+ }
+
+ if (!strcmp(text_formatter->name, "mermaid") || !strcmp(text_formatter->name, "mermaidhtml")) {
+ gpc->diagram_config.diagram_css = ff_resman_get_string(FF_RESOURCE_GRAPH_CSS);
+
+ if (!strcmp(text_formatter->name, "mermaidhtml"))
+ gpc->diagram_config.html_template = ff_resman_get_string(FF_RESOURCE_GRAPH_HTML);
+
+ av_diagram_init(tfc, &gpc->diagram_config);
+ }
+
+ *pgpc = gpc;
+
+ return 0;
+
+fail:
+ if (tfc)
+ avtext_context_close(&tfc);
+ if (wctx && !tfc) // Only free wctx if tfc didn't take ownership of it
+ avtextwriter_context_close(&wctx);
+ av_freep(&gpc);
+
+ return ret;
+}
+
+
+int print_filtergraph(FilterGraph *fg, AVFilterGraph *graph)
+{
+ GraphPrintContext *gpc = NULL;
+ AVTextFormatContext *tfc;
+ FilterGraphPriv *fgp = fgp_from_fg(fg);
+ AVBPrint *target_buf = &fgp->graph_print_buf;
+ int ret;
+
+ if (!fg || !fgp) {
+ av_log(NULL, AV_LOG_ERROR, "Invalid filter graph provided\n");
+ return AVERROR(EINVAL);
+ }
+
+ if (target_buf->len)
+ av_bprint_finalize(target_buf, NULL);
+
+ ret = init_graphprint(&gpc, target_buf);
+ if (ret)
+ return ret;
+
+ if (!gpc) {
+ av_log(NULL, AV_LOG_ERROR, "Failed to initialize graph print context\n");
+ return AVERROR(ENOMEM);
+ }
+
+ tfc = gpc->tfc;
+
+ // Due to the threading model each graph needs to print itself into a buffer
+ // from its own thread. The actual printing happens short before cleanup in ffmpeg.c
+ // where all graphs are assembled together. To make this work, we need to put the
+ // formatting context into the same state like it would be when printing all at once,
+ // so here we print the section headers and clear the buffer to get into the right state.
+ avtext_print_section_header(tfc, NULL, SECTION_ID_ROOT);
+ avtext_print_section_header(tfc, NULL, SECTION_ID_FILTERGRAPHS);
+ avtext_print_section_header(tfc, NULL, SECTION_ID_FILTERGRAPH);
+
+ av_bprint_clear(target_buf);
+
+ print_filtergraph_single(gpc, fg, graph);
+
+ if (gpc->is_diagram) {
+ avtext_print_section_footer(tfc); // SECTION_ID_FILTERGRAPH
+ avtext_print_section_footer(tfc); // SECTION_ID_FILTERGRAPHS
+ }
+
+ uninit_graphprint(gpc);
+
+ return 0;
+}
+
+static int print_filtergraphs_priv(FilterGraph **graphs, int nb_graphs, InputFile **ifiles, int nb_ifiles, OutputFile **ofiles, int nb_ofiles)
+{
+ GraphPrintContext *gpc = NULL;
+ AVTextFormatContext *tfc;
+ AVBPrint target_buf;
+ int ret;
+
+ ret = init_graphprint(&gpc, &target_buf);
+ if (ret)
+ goto cleanup;
+
+ if (!gpc) {
+ ret = AVERROR(ENOMEM);
+ goto cleanup;
+ }
+
+ tfc = gpc->tfc;
+
+ avtext_print_section_header(tfc, NULL, SECTION_ID_ROOT);
+ avtext_print_section_header(tfc, NULL, SECTION_ID_FILTERGRAPHS);
+
+ for (int i = 0; i < nb_graphs; i++) {
+ FilterGraphPriv *fgp = fgp_from_fg(graphs[i]);
+ AVBPrint *graph_buf = &fgp->graph_print_buf;
+
+ if (graph_buf->len > 0) {
+ avtext_print_section_header(tfc, NULL, SECTION_ID_FILTERGRAPH);
+ av_bprint_append_data(&target_buf, graph_buf->str, graph_buf->len);
+ av_bprint_finalize(graph_buf, NULL);
+ avtext_print_section_footer(tfc); // SECTION_ID_FILTERGRAPH
+ }
+ }
+
+ for (int n = 0; n < nb_ofiles; n++) {
+ OutputFile *of = ofiles[n];
+
+ for (int i = 0; i < of->nb_streams; i++) {
+ OutputStream *ost = of->streams[i];
+
+ if (ost->fg_simple) {
+ FilterGraphPriv *fgp = fgp_from_fg(ost->fg_simple);
+ AVBPrint *graph_buf = &fgp->graph_print_buf;
+
+ if (graph_buf->len > 0) {
+ avtext_print_section_header(tfc, NULL, SECTION_ID_FILTERGRAPH);
+ av_bprint_append_data(&target_buf, graph_buf->str, graph_buf->len);
+ av_bprint_finalize(graph_buf, NULL);
+ avtext_print_section_footer(tfc); // SECTION_ID_FILTERGRAPH
+ }
+ }
+ }
+ }
+
+ avtext_print_section_footer(tfc); // SECTION_ID_FILTERGRAPHS
+
+ print_streams(gpc, ifiles, nb_ifiles, ofiles, nb_ofiles);
+
+ avtext_print_section_footer(tfc); // SECTION_ID_ROOT
+
+ if (print_graphs_file) {
+ AVIOContext *avio = NULL;
+
+ if (!strcmp(print_graphs_file, "-")) {
+ printf("%s", target_buf.str);
+ } else {
+ ret = avio_open2(&avio, print_graphs_file, AVIO_FLAG_WRITE, NULL, NULL);
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_ERROR, "Failed to open graph output file, \"%s\": %s\n", print_graphs_file, av_err2str(ret));
+ goto cleanup;
+ }
+
+ avio_write(avio, (const unsigned char *)target_buf.str, FFMIN(target_buf.len, target_buf.size - 1));
+ avio_flush(avio);
+
+ if ((ret = avio_closep(&avio)) < 0)
+ av_log(NULL, AV_LOG_ERROR, "Error closing graph output file, loss of information possible: %s\n", av_err2str(ret));
+ }
+ }
+
+ if (print_graphs)
+ av_log(NULL, AV_LOG_INFO, "%s %c", target_buf.str, '\n');
+
+cleanup:
+ // Properly clean up resources
+ if (gpc)
+ uninit_graphprint(gpc);
+
+ // Ensure the target buffer is properly finalized
+ av_bprint_finalize(&target_buf, NULL);
+
+ return ret;
+}
+
+int print_filtergraphs(FilterGraph **graphs, int nb_graphs, InputFile **ifiles, int nb_ifiles, OutputFile **ofiles, int nb_ofiles)
+{
+ return print_filtergraphs_priv(graphs, nb_graphs, ifiles, nb_ifiles, ofiles, nb_ofiles);
+}
diff --git a/fftools/graph/graphprint.h b/fftools/graph/graphprint.h
new file mode 100644
index 0000000000..9f043cc273
--- /dev/null
+++ b/fftools/graph/graphprint.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2018-2025 - softworkz
+ *
+ * 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
+ */
+
+#ifndef FFTOOLS_GRAPH_GRAPHPRINT_H
+#define FFTOOLS_GRAPH_GRAPHPRINT_H
+
+#include "fftools/ffmpeg.h"
+
+int print_filtergraphs(FilterGraph **graphs, int nb_graphs, InputFile **ifiles, int nb_ifiles, OutputFile **ofiles, int nb_ofiles);
+
+int print_filtergraph(FilterGraph *fg, AVFilterGraph *graph);
+
+#endif /* FFTOOLS_GRAPH_GRAPHPRINT_H */
diff --git a/fftools/textformat/avtextformat.c b/fftools/textformat/avtextformat.c
index d96ced9b7d..f40a667843 100644
--- a/fftools/textformat/avtextformat.c
+++ b/fftools/textformat/avtextformat.c
@@ -710,6 +710,8 @@ static void formatters_register_all(void)
registered_formatters[4] = &avtextformatter_ini;
registered_formatters[5] = &avtextformatter_json;
registered_formatters[6] = &avtextformatter_xml;
+ registered_formatters[7] = &avtextformatter_mermaid;
+ registered_formatters[8] = &avtextformatter_mermaidhtml;
}
const AVTextFormatter *avtext_get_formatter_by_name(const char *name)
diff --git a/fftools/textformat/avtextformat.h b/fftools/textformat/avtextformat.h
index 16cd9b214f..4890cb846d 100644
--- a/fftools/textformat/avtextformat.h
+++ b/fftools/textformat/avtextformat.h
@@ -31,6 +31,12 @@
#define SECTION_MAX_NB_CHILDREN 11
+typedef struct AVTextFormatSectionContext {
+ char *context_id;
+ const char *context_type;
+ int context_flags;
+} AVTextFormatSectionContext;
+
typedef struct AVTextFormatSection {
int id; ///< unique id identifying a section
@@ -42,6 +48,10 @@ typedef struct AVTextFormatSection {
/// For these sections the element_name field is mandatory.
#define AV_TEXTFORMAT_SECTION_FLAG_HAS_TYPE 8 ///< the section contains a type to distinguish multiple nested elements
#define AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE 16 ///< the items in this array section should be numbered individually by type
+#define AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE 32 ///< ...
+#define AV_TEXTFORMAT_SECTION_FLAG_HAS_LINKS 64 ///< ...
+#define AV_TEXTFORMAT_SECTION_PRINT_TAGS 128 ///< ...
+#define AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH 256 ///< ...
int flags;
const int children_ids[SECTION_MAX_NB_CHILDREN+1]; ///< list of children section IDS, terminated by -1
@@ -50,12 +60,17 @@ typedef struct AVTextFormatSection {
AVDictionary *entries_to_show;
const char *(* get_type)(const void *data); ///< function returning a type if defined, must be defined when SECTION_FLAG_HAS_TYPE is defined
int show_all_entries;
+ const char *id_key; ///< name of the key to be used as the id
+ const char *src_id_key; ///< name of the key to be used as the source id for diagram connections
+ const char *dest_id_key; ///< name of the key to be used as the target id for diagram connections
+ const char *linktype_key; ///< name of the key to be used as the link type for diagram connections (AVTextFormatLinkType)
} AVTextFormatSection;
typedef struct AVTextFormatContext AVTextFormatContext;
#define AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS 1
#define AV_TEXTFORMAT_FLAG_SUPPORTS_MIXED_ARRAY_CONTENT 2
+#define AV_TEXTFORMAT_FLAG_IS_DIAGRAM_FORMATTER 4
typedef enum {
AV_TEXTFORMAT_STRING_VALIDATION_FAIL,
@@ -64,6 +79,18 @@ typedef enum {
AV_TEXTFORMAT_STRING_VALIDATION_NB
} StringValidation;
+typedef enum {
+ AV_TEXTFORMAT_LINKTYPE_SRCDEST,
+ AV_TEXTFORMAT_LINKTYPE_DESTSRC,
+ AV_TEXTFORMAT_LINKTYPE_BIDIR,
+ AV_TEXTFORMAT_LINKTYPE_NONDIR,
+ AV_TEXTFORMAT_LINKTYPE_HIDDEN,
+ AV_TEXTFORMAT_LINKTYPE_ONETOMANY = AV_TEXTFORMAT_LINKTYPE_SRCDEST,
+ AV_TEXTFORMAT_LINKTYPE_MANYTOONE = AV_TEXTFORMAT_LINKTYPE_DESTSRC,
+ AV_TEXTFORMAT_LINKTYPE_ONETOONE = AV_TEXTFORMAT_LINKTYPE_BIDIR,
+ AV_TEXTFORMAT_LINKTYPE_MANYTOMANY = AV_TEXTFORMAT_LINKTYPE_NONDIR,
+} AVTextFormatLinkType;
+
typedef struct AVTextFormatter {
const AVClass *priv_class; ///< private class of the formatter, if any
int priv_size; ///< private size for the formatter context
@@ -167,5 +194,7 @@ extern const AVTextFormatter avtextformatter_flat;
extern const AVTextFormatter avtextformatter_ini;
extern const AVTextFormatter avtextformatter_json;
extern const AVTextFormatter avtextformatter_xml;
+extern const AVTextFormatter avtextformatter_mermaid;
+extern const AVTextFormatter avtextformatter_mermaidhtml;
#endif /* FFTOOLS_TEXTFORMAT_AVTEXTFORMAT_H */
diff --git a/fftools/textformat/tf_mermaid.c b/fftools/textformat/tf_mermaid.c
new file mode 100644
index 0000000000..e0ea89b02a
--- /dev/null
+++ b/fftools/textformat/tf_mermaid.c
@@ -0,0 +1,655 @@
+/*
+ * Copyright (c) The FFmpeg developers
+ *
+ * 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 <limits.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "avtextformat.h"
+#include "tf_internal.h"
+#include "tf_mermaid.h"
+#include <libavutil/mem.h>
+#include <libavutil/avassert.h>
+#include <libavutil/bprint.h>
+#include <libavutil/opt.h>
+
+
+static const char *init_directive = ""
+ "%%{init: {"
+ "\"theme\": \"base\","
+ "\"curve\": \"monotoneX\","
+ "\"rankSpacing\": 10,"
+ "\"nodeSpacing\": 10,"
+ "\"themeCSS\": \"__###__\","
+ "\"fontFamily\": \"Roboto,Segoe UI,sans-serif\","
+ "\"themeVariables\": { "
+ "\"clusterBkg\": \"white\", "
+ "\"primaryBorderColor\": \"gray\", "
+ "\"lineColor\": \"gray\", "
+ "\"secondaryTextColor\": \"gray\", "
+ "\"tertiaryBorderColor\": \"gray\", "
+ "\"primaryTextColor\": \"#666\", "
+ "\"secondaryTextColor\": \"red\" "
+ "},"
+ "\"flowchart\": { "
+ "\"subGraphTitleMargin\": { \"top\": -15, \"bottom\": 20 }, "
+ "\"diagramPadding\": 20, "
+ "\"curve\": \"monotoneX\" "
+ "}"
+ " }}%%\n\n";
+
+static const char* init_directive_er = ""
+ "%%{init: {"
+ "\"theme\": \"base\","
+ "\"layout\": \"elk\","
+ "\"curve\": \"monotoneX\","
+ "\"rankSpacing\": 65,"
+ "\"nodeSpacing\": 60,"
+ "\"themeCSS\": \"__###__\","
+ "\"fontFamily\": \"Roboto,Segoe UI,sans-serif\","
+ "\"themeVariables\": { "
+ "\"clusterBkg\": \"white\", "
+ "\"primaryBorderColor\": \"gray\", "
+ "\"lineColor\": \"gray\", "
+ "\"secondaryTextColor\": \"gray\", "
+ "\"tertiaryBorderColor\": \"gray\", "
+ "\"primaryTextColor\": \"#666\", "
+ "\"secondaryTextColor\": \"red\" "
+ "},"
+ "\"er\": { "
+ "\"diagramPadding\": 12, "
+ "\"entityPadding\": 4, "
+ "\"minEntityWidth\": 150, "
+ "\"minEntityHeight\": 20, "
+ "\"curve\": \"monotoneX\" "
+ "}"
+ " }}%%\n\n";
+
+static const char *theme_css_er = ""
+
+ // Variables
+ ".root { "
+ "--ff-colvideo: #6eaa7b; "
+ "--ff-colaudio: #477fb3; "
+ "--ff-colsubtitle: #ad76ab; "
+ "--ff-coltext: #666; "
+ "} "
+ " g.nodes g.node.default rect.basic.label-container, "
+ " g.nodes g.node.default path { "
+ " rx: 1; "
+ " ry: 1; "
+ " stroke-width: 1px !important; "
+ " stroke: #e9e9e9 !important; "
+ " fill: url(#ff-filtergradient) !important; "
+ " filter: drop-shadow(0px 0px 5.5px rgba(0, 0, 0, 0.05)); "
+ " fill: white !important; "
+ " } "
+ " "
+ " .relationshipLine { "
+ " stroke: gray; "
+ " stroke-width: 1; "
+ " fill: none; "
+ " filter: drop-shadow(0px 0px 3px rgba(0, 0, 0, 0.2)); "
+ " } "
+ " "
+ " g.node.default g.label.name foreignObject > div > span > p, "
+ " g.nodes g.node.default g.label:not(.attribute-name, .attribute-keys, .attribute-type, .attribute-comment) foreignObject > div > span > p { "
+ " font-size: 0.95rem; "
+ " font-weight: 500; "
+ " text-transform: uppercase; "
+ " min-width: 5.5rem; "
+ " margin-bottom: 0.5rem; "
+ " "
+ " } "
+ " "
+ " .edgePaths path { "
+ " marker-end: none; "
+ " marker-start: none; "
+ " "
+ "} ";
+
+
+/* Mermaid Graph output */
+
+typedef struct MermaidContext {
+ const AVClass *class;
+ AVDiagramConfig *diagram_config;
+ int subgraph_count;
+ int within_tag;
+ int indent_level;
+ int create_html;
+
+ // Options
+ int enable_link_colors; // Requires Mermaid 11.5
+
+ struct section_data {
+ const char *section_id;
+ const char *section_type;
+ const char *src_id;
+ const char *dest_id;
+ AVTextFormatLinkType link_type;
+ int current_is_textblock;
+ int current_is_stadium;
+ int subgraph_start_incomplete;
+ } section_data[SECTION_MAX_NB_LEVELS];
+
+ unsigned nb_link_captions[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
+ AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
+ AVBPrint link_buf; ///< print buffer for writing diagram links
+ AVDictionary *link_dict;
+} MermaidContext;
+
+#undef OFFSET
+#define OFFSET(x) offsetof(MermaidContext, x)
+
+static const AVOption mermaid_options[] = {
+ { "link_coloring", "enable colored links (requires Mermaid >= 11.5)", OFFSET(enable_link_colors), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
+ ////{"diagram_css", "CSS for the diagram", OFFSET(diagram_css), AV_OPT_TYPE_STRING, {.i64=0}, 0, 1 },
+ ////{"html_template", "Template HTML", OFFSET(html_template), AV_OPT_TYPE_STRING, {.i64=0}, 0, 1 },
+ { NULL },
+};
+
+DEFINE_FORMATTER_CLASS(mermaid);
+
+void av_diagram_init(AVTextFormatContext *tfc, AVDiagramConfig *diagram_config)
+{
+ MermaidContext *mmc = tfc->priv;
+ mmc->diagram_config = diagram_config;
+}
+
+static av_cold int has_link_pair(const AVTextFormatContext *tfc, const char *src, const char *dest)
+{
+ MermaidContext *mmc = tfc->priv;
+ AVBPrint buf;
+
+ av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
+ av_bprintf(&buf, "%s--%s", src, dest);
+
+ if (mmc->link_dict && av_dict_get(mmc->link_dict, buf.str, NULL, 0))
+ return 1;
+
+ av_dict_set(&mmc->link_dict, buf.str, buf.str, 0);
+
+ return 0;
+}
+
+static av_cold int mermaid_init(AVTextFormatContext *tfc)
+{
+ MermaidContext *mmc = tfc->priv;
+
+ av_bprint_init(&mmc->link_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
+
+ ////mmc->enable_link_colors = 1; // Requires Mermaid 11.5
+ return 0;
+}
+
+static av_cold int mermaid_init_html(AVTextFormatContext *tfc)
+{
+ MermaidContext *mmc = tfc->priv;
+
+ int ret = mermaid_init(tfc);
+
+ if (ret < 0)
+ return ret;
+
+ mmc->create_html = 1;
+
+ return 0;
+}
+
+#define MM_INDENT() writer_printf(tfc, "%*c", mmc->indent_level * 2, ' ')
+
+static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *data)
+{
+ const AVTextFormatSection *section = tf_get_section(tfc, tfc->level);
+ const AVTextFormatSection *parent_section = tf_get_parent_section(tfc, tfc->level);
+
+ if (!section)
+ return;
+ AVBPrint *buf = &tfc->section_pbuf[tfc->level];
+ MermaidContext *mmc = tfc->priv;
+ const AVTextFormatSectionContext *sec_ctx = data;
+
+ if (tfc->level == 0) {
+ char *directive;
+ AVBPrint css_buf;
+ const char *diag_directive = mmc->diagram_config->diagram_type == AV_DIAGRAMTYPE_ENTITYRELATIONSHIP ? init_directive_er : init_directive;
+ char *single_line_css = av_strireplace(mmc->diagram_config->diagram_css, "\n", " ");
+ ////char *single_line_css = av_strireplace(theme_css_er, "\n", " ");
+ av_bprint_init(&css_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
+ av_bprint_escape(&css_buf, single_line_css, "'\\", AV_ESCAPE_MODE_BACKSLASH, AV_ESCAPE_FLAG_STRICT);
+ av_freep(&single_line_css);
+
+ directive = av_strireplace(diag_directive, "__###__", css_buf.str);
+ if (mmc->create_html) {
+ uint64_t length;
+ char *token_pos = av_stristr(mmc->diagram_config->html_template, "__###__");
+ if (!token_pos) {
+ av_log(tfc, AV_LOG_ERROR, "Unable to locate the required token (__###__) in the html template.");
+ return;
+ }
+
+ length = token_pos - mmc->diagram_config->html_template;
+ for (uint64_t i = 0; i < length; i++)
+ writer_w8(tfc, mmc->diagram_config->html_template[i]);
+ }
+
+ writer_put_str(tfc, directive);
+ switch (mmc->diagram_config->diagram_type) {
+ case AV_DIAGRAMTYPE_GRAPH:
+ writer_put_str(tfc, "flowchart LR\n");
+ ////writer_put_str(tfc, " gradient_def@{ shape: text, label: \"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1\" height=\"1\"><defs><linearGradient id=\"ff-filtergradient\" x1=\"0%\" y1=\"0%\" x2=\"0%\" y2=\"100%\"><stop offset=\"0%\" style=\"stop-color:hsla(0, 0%, 30%, 0.02);\"/><stop offset=\"50%\" style=\"stop-color:hsla(0, 0%, 30%, 0);\"/><stop offset=\"100%\" style=\"stop-color:hsla(0, 0%, 30%, 0.05);\"/></linearGradient></defs></svg>\" }\n");
+ writer_put_str(tfc, " gradient_def@{ shape: text, label: \"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1\" height=\"1\"><defs><linearGradient id=\"ff-filtergradient\" x1=\"0%\" y1=\"0%\" x2=\"0%\" y2=\"100%\"><stop offset=\"0%\" style=\"stop-color:hsl(0, 0%, 98.6%); \"/><stop offset=\"50%\" style=\"stop-color:hsl(0, 0%, 100%); \"/><stop offset=\"100%\" style=\"stop-color:hsl(0, 0%, 96.5%); \"/></linearGradient><radialGradient id=\"ff-radgradient\" cx=\"50%\" cy=\"50%\" r=\"100%\" fx=\"45%\" fy=\"40%\"><stop offset=\"25%\" stop-color=\"hsl(0, 0%, 100%)\" /><stop offset=\"100%\" stop-color=\"hsl(0, 0%, 96%)\" /></radialGradient></defs></svg>\" }\n");
+ break;
+ case AV_DIAGRAMTYPE_ENTITYRELATIONSHIP:
+ writer_put_str(tfc, "erDiagram\n");
+ break;
+ }
+
+ return;
+ }
+
+ if (parent_section && parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH) {
+
+ struct section_data parent_sec_data = mmc->section_data[tfc->level - 1];
+ AVBPrint *parent_buf = &tfc->section_pbuf[tfc->level - 1];
+
+ if (parent_sec_data.subgraph_start_incomplete) {
+
+ if (parent_buf->len > 0)
+ writer_printf(tfc, "%s", parent_buf->str);
+
+ writer_put_str(tfc, "</div>\"]\n");
+
+ mmc->section_data[tfc->level - 1].subgraph_start_incomplete = 0;
+ }
+ }
+
+ av_freep(&mmc->section_data[tfc->level].section_id);
+ av_freep(&mmc->section_data[tfc->level].section_type);
+ av_freep(&mmc->section_data[tfc->level].src_id);
+ av_freep(&mmc->section_data[tfc->level].dest_id);
+ mmc->section_data[tfc->level].current_is_textblock = 0;
+ mmc->section_data[tfc->level].current_is_stadium = 0;
+ mmc->section_data[tfc->level].subgraph_start_incomplete = 0;
+ mmc->section_data[tfc->level].link_type = AV_TEXTFORMAT_LINKTYPE_SRCDEST;
+
+ // NOTE: av_strdup() allocations aren't checked
+ if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH) {
+
+ av_bprint_clear(buf);
+ writer_put_str(tfc, "\n");
+
+ mmc->indent_level++;
+
+ if (sec_ctx->context_id) {
+ MM_INDENT();
+ writer_printf(tfc, "subgraph %s[\"<div class=\"ff-%s\">", sec_ctx->context_id, section->name);
+ } else {
+ av_log(tfc, AV_LOG_ERROR, "Unable to write subgraph start. Missing id field. Section: %s", section->name);
+ }
+
+ mmc->section_data[tfc->level].subgraph_start_incomplete = 1;
+ mmc->section_data[tfc->level].section_id = av_strdup(sec_ctx->context_id);
+ }
+
+ if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE) {
+
+ av_bprint_clear(buf);
+ writer_put_str(tfc, "\n");
+
+ mmc->indent_level++;
+
+ if (sec_ctx->context_id) {
+
+ mmc->section_data[tfc->level].section_id = av_strdup(sec_ctx->context_id);
+
+ switch (mmc->diagram_config->diagram_type) {
+ case AV_DIAGRAMTYPE_GRAPH:
+ if (sec_ctx->context_flags & 1) {
+
+ MM_INDENT();
+ writer_printf(tfc, "%s@{ shape: text, label: \"", sec_ctx->context_id);
+ mmc->section_data[tfc->level].current_is_textblock = 1;
+ } else if (sec_ctx->context_flags & 2) {
+
+ MM_INDENT();
+ writer_printf(tfc, "%s([\"", sec_ctx->context_id);
+ mmc->section_data[tfc->level].current_is_stadium = 1;
+ } else {
+ MM_INDENT();
+ writer_printf(tfc, "%s(\"", sec_ctx->context_id);
+ }
+
+ break;
+ case AV_DIAGRAMTYPE_ENTITYRELATIONSHIP:
+ MM_INDENT();
+ writer_printf(tfc, "%s {\n", sec_ctx->context_id);
+ break;
+ }
+
+ } else {
+ av_log(tfc, AV_LOG_ERROR, "Unable to write shape start. Missing id field. Section: %s", section->name);
+ }
+
+ mmc->section_data[tfc->level].section_id = av_strdup(sec_ctx->context_id);
+ }
+
+
+ if (section->flags & AV_TEXTFORMAT_SECTION_PRINT_TAGS) {
+
+ if (sec_ctx && sec_ctx->context_type)
+ writer_printf(tfc, "<div class=\"ff-%s %s\">", section->name, sec_ctx->context_type);
+ else
+ writer_printf(tfc, "<div class=\"ff-%s\">", section->name);
+ }
+
+
+ if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_LINKS) {
+
+ av_bprint_clear(buf);
+ mmc->nb_link_captions[tfc->level] = 0;
+
+ if (sec_ctx && sec_ctx->context_type)
+ mmc->section_data[tfc->level].section_type = av_strdup(sec_ctx->context_type);
+
+ ////if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_TYPE) {
+ //// AVBPrint buf;
+ //// av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
+ //// av_bprint_escape(&buf, section->get_type(data), NULL,
+ //// AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
+ //// writer_printf(tfc, " type=\"%s\"", buf.str);
+ }
+}
+
+static void mermaid_print_section_footer(AVTextFormatContext *tfc)
+{
+ MermaidContext *mmc = tfc->priv;
+ const AVTextFormatSection *section = tf_get_section(tfc, tfc->level);
+
+ if (!section)
+ return;
+ AVBPrint *buf = &tfc->section_pbuf[tfc->level];
+ struct section_data sec_data = mmc->section_data[tfc->level];
+
+ if (section->flags & AV_TEXTFORMAT_SECTION_PRINT_TAGS)
+ writer_put_str(tfc, "</div>");
+
+ if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE) {
+
+ switch (mmc->diagram_config->diagram_type) {
+ case AV_DIAGRAMTYPE_GRAPH:
+
+ if (sec_data.current_is_textblock) {
+ writer_printf(tfc, "\"}\n", section->name);
+
+ if (sec_data.section_id) {
+ MM_INDENT();
+ writer_put_str(tfc, "class ");
+ writer_put_str(tfc, sec_data.section_id);
+ writer_put_str(tfc, " ff-");
+ writer_put_str(tfc, section->name);
+ writer_put_str(tfc, "\n");
+ }
+ } else if (sec_data.current_is_stadium) {
+ writer_printf(tfc, "\"]):::ff-%s\n", section->name);
+ } else {
+ writer_printf(tfc, "\"):::ff-%s\n", section->name);
+ }
+
+ break;
+ case AV_DIAGRAMTYPE_ENTITYRELATIONSHIP:
+ MM_INDENT();
+ writer_put_str(tfc, "}\n\n");
+ break;
+ }
+
+ mmc->indent_level--;
+
+ } else if ((section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH)) {
+
+ MM_INDENT();
+ writer_put_str(tfc, "end\n");
+
+ if (sec_data.section_id) {
+ MM_INDENT();
+ writer_put_str(tfc, "class ");
+ writer_put_str(tfc, sec_data.section_id);
+ writer_put_str(tfc, " ff-");
+ writer_put_str(tfc, section->name);
+ writer_put_str(tfc, "\n");
+ }
+
+ mmc->indent_level--;
+ }
+
+ if ((section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_LINKS))
+ if (sec_data.src_id && sec_data.dest_id
+ && !has_link_pair(tfc, sec_data.src_id, sec_data.dest_id))
+ switch (mmc->diagram_config->diagram_type) {
+ case AV_DIAGRAMTYPE_GRAPH:
+
+ if (sec_data.section_type && mmc->enable_link_colors)
+ av_bprintf(&mmc->link_buf, "\n %s %s-%s-%s@==", sec_data.src_id, sec_data.section_type, sec_data.src_id, sec_data.dest_id);
+ else
+ av_bprintf(&mmc->link_buf, "\n %s ==", sec_data.src_id);
+
+ if (buf->len > 0) {
+ av_bprintf(&mmc->link_buf, " \"%s", buf->str);
+
+ for (unsigned i = 0; i < mmc->nb_link_captions[tfc->level]; i++)
+ av_bprintf(&mmc->link_buf, "<br> ");
+
+ av_bprintf(&mmc->link_buf, "\" ==");
+ }
+
+ av_bprintf(&mmc->link_buf, "> %s", sec_data.dest_id);
+
+ break;
+ case AV_DIAGRAMTYPE_ENTITYRELATIONSHIP:
+
+
+ av_bprintf(&mmc->link_buf, "\n %s", sec_data.src_id);
+
+ switch (sec_data.link_type) {
+ case AV_TEXTFORMAT_LINKTYPE_ONETOMANY:
+ av_bprintf(&mmc->link_buf, "%s", " ||--o{ ");
+ break;
+ case AV_TEXTFORMAT_LINKTYPE_MANYTOONE:
+ av_bprintf(&mmc->link_buf, "%s", " }o--|| ");
+ break;
+ case AV_TEXTFORMAT_LINKTYPE_ONETOONE:
+ av_bprintf(&mmc->link_buf, "%s", " ||--|| ");
+ break;
+ case AV_TEXTFORMAT_LINKTYPE_MANYTOMANY:
+ av_bprintf(&mmc->link_buf, "%s", " }o--o{ ");
+ break;
+ default:
+ av_bprintf(&mmc->link_buf, "%s", " ||--|| ");
+ break;
+ }
+
+ av_bprintf(&mmc->link_buf, "%s : \"\"", sec_data.dest_id);
+
+ break;
+ }
+
+ if (tfc->level == 0) {
+
+ writer_put_str(tfc, "\n");
+ if (mmc->create_html) {
+ char *token_pos = av_stristr(mmc->diagram_config->html_template, "__###__");
+ if (!token_pos) {
+ av_log(tfc, AV_LOG_ERROR, "Unable to locate the required token (__###__) in the html template.");
+ return;
+ }
+ token_pos += strlen("__###__");
+ writer_put_str(tfc, token_pos);
+ }
+ }
+
+ if (tfc->level == 1) {
+
+ if (mmc->link_buf.len > 0) {
+ writer_put_str(tfc, mmc->link_buf.str);
+ av_bprint_clear(&mmc->link_buf);
+ }
+
+ writer_put_str(tfc, "\n");
+ }
+}
+
+static void mermaid_print_value(AVTextFormatContext *tfc, const char *key,
+ const char *str, int64_t num, const int is_int)
+{
+ MermaidContext *mmc = tfc->priv;
+ const AVTextFormatSection *section = tf_get_section(tfc, tfc->level);
+
+ if (!section)
+ return;
+
+ AVBPrint *buf = &tfc->section_pbuf[tfc->level];
+ struct section_data sec_data = mmc->section_data[tfc->level];
+ int exit = 0;
+
+ if (section->id_key && !strcmp(section->id_key, key)) {
+ mmc->section_data[tfc->level].section_id = av_strdup(str);
+ exit = 1;
+ }
+
+ if (section->dest_id_key && !strcmp(section->dest_id_key, key)) {
+ mmc->section_data[tfc->level].dest_id = av_strdup(str);
+ exit = 1;
+ }
+
+ if (section->src_id_key && !strcmp(section->src_id_key, key)) {
+ mmc->section_data[tfc->level].src_id = av_strdup(str);
+ exit = 1;
+ }
+
+ if (section->linktype_key && !strcmp(section->linktype_key, key)) {
+ mmc->section_data[tfc->level].link_type = (AVTextFormatLinkType)num;;
+ exit = 1;
+ }
+
+ //if (exit)
+ // return;
+
+ if ((section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE | AV_TEXTFORMAT_SECTION_PRINT_TAGS))
+ || (section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH && sec_data.subgraph_start_incomplete)) {
+
+ if (exit)
+ return;
+
+ switch (mmc->diagram_config->diagram_type) {
+ case AV_DIAGRAMTYPE_GRAPH:
+
+ if (is_int) {
+ writer_printf(tfc, "<span class=\"%s\">%s: %"PRId64"</span>", key, key, num);
+ } else {
+ ////AVBPrint b;
+ ////av_bprint_init(&b, 0, AV_BPRINT_SIZE_UNLIMITED);
+ const char *tmp = av_strireplace(str, "\"", "'");
+ ////av_bprint_escape(&b, str, NULL, AV_ESCAPE_MODE_AUTO, AV_ESCAPE_FLAG_STRICT);
+ writer_printf(tfc, "<span class=\"%s\">%s</span>", key, tmp);
+ av_freep(&tmp);
+ }
+
+ break;
+ case AV_DIAGRAMTYPE_ENTITYRELATIONSHIP:
+ {
+ char *col_type;
+
+ if (key[0] == '_')
+ return;
+
+ if (sec_data.section_id && !strcmp(str, sec_data.section_id))
+ col_type = "PK";
+ else if (sec_data.dest_id && !strcmp(str, sec_data.dest_id))
+ col_type = "FK";
+ else if (sec_data.src_id && !strcmp(str, sec_data.src_id))
+ col_type = "FK";
+ else
+ col_type = "";
+
+ MM_INDENT();
+
+ if (is_int)
+ writer_printf(tfc, " %s %"PRId64" %s\n", key, num, col_type);
+ else
+ writer_printf(tfc, " %s %s %s\n", key, str, col_type);
+ }
+ break;
+ }
+
+ } else if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_LINKS) {
+
+ if (exit)
+ return;
+
+ if (buf->len > 0)
+ av_bprintf(buf, "%s", "<br>");
+
+ av_bprintf(buf, "");
+ if (is_int)
+ av_bprintf(buf, "<span>%s: %"PRId64"</span>", key, num);
+ else
+ av_bprintf(buf, "<span>%s</span>", str);
+
+ mmc->nb_link_captions[tfc->level]++;
+ }
+}
+
+static inline void mermaid_print_str(AVTextFormatContext *tfc, const char *key, const char *value)
+{
+ mermaid_print_value(tfc, key, value, 0, 0);
+}
+
+static void mermaid_print_int(AVTextFormatContext *tfc, const char *key, int64_t value)
+{
+ mermaid_print_value(tfc, key, NULL, value, 1);
+}
+
+const AVTextFormatter avtextformatter_mermaid = {
+ .name = "mermaid",
+ .priv_size = sizeof(MermaidContext),
+ .init = mermaid_init,
+ .print_section_header = mermaid_print_section_header,
+ .print_section_footer = mermaid_print_section_footer,
+ .print_integer = mermaid_print_int,
+ .print_string = mermaid_print_str,
+ .flags = AV_TEXTFORMAT_FLAG_IS_DIAGRAM_FORMATTER,
+ .priv_class = &mermaid_class,
+};
+
+
+const AVTextFormatter avtextformatter_mermaidhtml = {
+ .name = "mermaidhtml",
+ .priv_size = sizeof(MermaidContext),
+ .init = mermaid_init_html,
+ .print_section_header = mermaid_print_section_header,
+ .print_section_footer = mermaid_print_section_footer,
+ .print_integer = mermaid_print_int,
+ .print_string = mermaid_print_str,
+ .flags = AV_TEXTFORMAT_FLAG_IS_DIAGRAM_FORMATTER,
+ .priv_class = &mermaid_class,
+};
diff --git a/fftools/textformat/tf_mermaid.h b/fftools/textformat/tf_mermaid.h
new file mode 100644
index 0000000000..aff73bf9f3
--- /dev/null
+++ b/fftools/textformat/tf_mermaid.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) The FFmpeg developers
+ *
+ * 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
+ */
+
+#ifndef FFTOOLS_TEXTFORMAT_TF_MERMAID_H
+#define FFTOOLS_TEXTFORMAT_TF_MERMAID_H
+
+typedef enum {
+ AV_DIAGRAMTYPE_GRAPH,
+ AV_DIAGRAMTYPE_ENTITYRELATIONSHIP,
+} AVDiagramType;
+
+typedef struct AVDiagramConfig {
+ AVDiagramType diagram_type;
+ const char *diagram_css;
+ const char *html_template;
+} AVDiagramConfig;
+
+
+void av_diagram_init(AVTextFormatContext *tfc, AVDiagramConfig *diagram_config);
+
+void av_mermaid_set_html_template(AVTextFormatContext *tfc, const char *html_template);
+
+
+#endif /* FFTOOLS_TEXTFORMAT_TF_MERMAID_H */
\ No newline at end of file
--
ffmpeg-codebot
More information about the ffmpeg-devel
mailing list