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
23 #include <unistd.h> /* getopt */
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavfilter/avfilter.h"
34 #include "compat/getopt.c"
37 static void usage(void)
39 printf("Convert a libavfilter graph to a dot file.\n");
40 printf("Usage: graph2dot [OPTIONS]\n");
43 "-i INFILE set INFILE as input file, stdin if omitted\n"
44 "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
45 "-h print this help\n");
53 static void print_digraph(FILE *outfile
, AVFilterGraph
*graph
)
57 fprintf(outfile
, "digraph G {\n");
58 fprintf(outfile
, "node [shape=box]\n");
59 fprintf(outfile
, "rankdir=LR\n");
61 for (i
= 0; i
< graph
->nb_filters
; i
++) {
62 char filter_ctx_label
[128];
63 const AVFilterContext
*filter_ctx
= graph
->filters
[i
];
65 snprintf(filter_ctx_label
, sizeof(filter_ctx_label
), "%s\\n(%s)",
67 filter_ctx
->filter
->name
);
69 for (j
= 0; j
< filter_ctx
->nb_outputs
; j
++) {
70 AVFilterLink
*link
= filter_ctx
->outputs
[j
];
72 char dst_filter_ctx_label
[128];
73 const AVFilterContext
*dst_filter_ctx
= link
->dst
;
75 snprintf(dst_filter_ctx_label
, sizeof(dst_filter_ctx_label
),
78 dst_filter_ctx
->filter
->name
);
80 fprintf(outfile
, "\"%s\" -> \"%s\" [ label= \"inpad:%s -> outpad:%s\\n",
81 filter_ctx_label
, dst_filter_ctx_label
,
82 avfilter_pad_get_name(link
->srcpad
, 0),
83 avfilter_pad_get_name(link
->dstpad
, 0));
85 if (link
->type
== AVMEDIA_TYPE_VIDEO
) {
86 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(link
->format
);
88 "fmt:%s w:%d h:%d tb:%d/%d",
91 link
->time_base
.num
, link
->time_base
.den
);
92 } else if (link
->type
== AVMEDIA_TYPE_AUDIO
) {
94 av_get_channel_layout_string(buf
, sizeof(buf
), -1,
95 link
->channel_layout
);
97 "fmt:%s sr:%d cl:%s tb:%d/%d",
98 av_get_sample_fmt_name(link
->format
),
99 link
->sample_rate
, buf
,
100 link
->time_base
.num
, link
->time_base
.den
);
102 fprintf(outfile
, "\" ];\n");
106 fprintf(outfile
, "}\n");
109 int main(int argc
, char **argv
)
111 const char *outfilename
= NULL
;
112 const char *infilename
= NULL
;
113 FILE *outfile
= NULL
;
115 char *graph_string
= NULL
;
116 AVFilterGraph
*graph
= av_mallocz(sizeof(AVFilterGraph
));
119 av_log_set_level(AV_LOG_DEBUG
);
121 while ((c
= getopt(argc
, argv
, "hi:o:")) != -1) {
130 outfilename
= optarg
;
137 if (!infilename
|| !strcmp(infilename
, "-"))
138 infilename
= "/dev/stdin";
139 infile
= fopen(infilename
, "r");
141 fprintf(stderr
, "Failed to open input file '%s': %s\n",
142 infilename
, strerror(errno
));
146 if (!outfilename
|| !strcmp(outfilename
, "-"))
147 outfilename
= "/dev/stdout";
148 outfile
= fopen(outfilename
, "w");
150 fprintf(stderr
, "Failed to open output file '%s': %s\n",
151 outfilename
, strerror(errno
));
155 /* read from infile and put it in a buffer */
158 struct line
*line
, *last_line
, *first_line
;
160 last_line
= first_line
= av_malloc(sizeof(struct line
));
162 fprintf(stderr
, "Memory allocation failure\n");
166 while (fgets(last_line
->data
, sizeof(last_line
->data
), infile
)) {
167 struct line
*new_line
= av_malloc(sizeof(struct line
));
169 fprintf(stderr
, "Memory allocation failure\n");
172 count
+= strlen(last_line
->data
);
173 last_line
->next
= new_line
;
174 last_line
= new_line
;
176 last_line
->next
= NULL
;
178 graph_string
= av_malloc(count
+ 1);
180 fprintf(stderr
, "Memory allocation failure\n");
184 for (line
= first_line
; line
->next
; line
= line
->next
) {
185 size_t l
= strlen(line
->data
);
186 memcpy(p
, line
->data
, l
);
192 avfilter_register_all();
194 if (avfilter_graph_parse(graph
, graph_string
, NULL
, NULL
, NULL
) < 0) {
195 fprintf(stderr
, "Failed to parse the graph description\n");
199 if (avfilter_graph_config(graph
, NULL
) < 0)
202 print_digraph(outfile
, graph
);