2 * Copyright (c) 2008-2010 Stefano Sabatini
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
21 #include <unistd.h> /* getopt */
23 #undef HAVE_AV_CONFIG_H
24 #include "libavutil/pixdesc.h"
25 #include "libavfilter/avfiltergraph.h"
27 static void usage(void)
29 printf("Convert a libavfilter graph to a dot file\n");
30 printf("Usage: graph2dot [OPTIONS]\n");
33 "-i INFILE set INFILE as input file, stdin if omitted\n"
34 "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
35 "-h print this help\n");
43 static void print_digraph(FILE *outfile
, AVFilterGraph
*graph
)
47 fprintf(outfile
, "digraph G {\n");
48 fprintf(outfile
, "node [shape=box]\n");
49 fprintf(outfile
, "rankdir=LR\n");
51 for (i
= 0; i
< graph
->filter_count
; i
++) {
52 char filter_ctx_label
[128];
53 const AVFilterContext
*filter_ctx
= graph
->filters
[i
];
55 snprintf(filter_ctx_label
, sizeof(filter_ctx_label
), "%s (%s)",
57 filter_ctx
->filter
->name
);
59 for (j
= 0; j
< filter_ctx
->output_count
; j
++) {
60 AVFilterLink
*link
= filter_ctx
->outputs
[j
];
62 char dst_filter_ctx_label
[128];
63 const AVFilterContext
*dst_filter_ctx
= link
->dst
;
65 snprintf(dst_filter_ctx_label
, sizeof(dst_filter_ctx_label
), "%s (%s)",
67 dst_filter_ctx
->filter
->name
);
69 fprintf(outfile
, "\"%s\" -> \"%s\"", filter_ctx_label
, dst_filter_ctx_label
);
70 fprintf(outfile
, " [ label= \"fmt:%s w:%d h:%d tb:%d/%d\" ];\n",
71 link
->type
== AVMEDIA_TYPE_VIDEO ? av_pix_fmt_descriptors
[link
->format
].name
:
72 link
->type
== AVMEDIA_TYPE_AUDIO ?
av_get_sample_fmt_name(link
->format
) : "unknown",
73 link
->w
, link
->h
, link
->time_base
.num
, link
->time_base
.den
);
77 fprintf(outfile
, "}\n");
80 int main(int argc
, char **argv
)
82 const char *outfilename
= NULL
;
83 const char *infilename
= NULL
;
86 char *graph_string
= NULL
;
87 AVFilterGraph
*graph
= av_mallocz(sizeof(AVFilterGraph
));
90 av_log_set_level(AV_LOG_DEBUG
);
92 while ((c
= getopt(argc
, argv
, "hi:o:")) != -1) {
101 outfilename
= optarg
;
108 if (!infilename
|| !strcmp(infilename
, "-"))
109 infilename
= "/dev/stdin";
110 infile
= fopen(infilename
, "r");
112 fprintf(stderr
, "Impossible to open input file '%s': %s\n", infilename
, strerror(errno
));
116 if (!outfilename
|| !strcmp(outfilename
, "-"))
117 outfilename
= "/dev/stdout";
118 outfile
= fopen(outfilename
, "w");
120 fprintf(stderr
, "Impossible to open output file '%s': %s\n", outfilename
, strerror(errno
));
124 /* read from infile and put it in a buffer */
126 unsigned int count
= 0;
127 struct line
*line
, *last_line
, *first_line
;
129 last_line
= first_line
= av_malloc(sizeof(struct line
));
131 while (fgets(last_line
->data
, sizeof(last_line
->data
), infile
)) {
132 struct line
*new_line
= av_malloc(sizeof(struct line
));
133 count
+= strlen(last_line
->data
);
134 last_line
->next
= new_line
;
135 last_line
= new_line
;
137 last_line
->next
= NULL
;
139 graph_string
= av_malloc(count
+ 1);
141 for (line
= first_line
; line
->next
; line
= line
->next
) {
142 unsigned int l
= strlen(line
->data
);
143 memcpy(p
, line
->data
, l
);
149 avfilter_register_all();
151 if (avfilter_graph_parse(graph
, graph_string
, NULL
, NULL
, NULL
) < 0) {
152 fprintf(stderr
, "Impossible to parse the graph description\n");
156 if (avfilter_graph_config(graph
, NULL
) < 0)
159 print_digraph(outfile
, graph
);