2 * Copyright (c) 2008-2010 Stefano Sabatini
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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 */
25 #include "libavutil/mem.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/audioconvert.h"
28 #include "libavfilter/avfiltergraph.h"
30 static void usage(void)
32 printf("Convert a libavfilter graph to a dot file\n");
33 printf("Usage: graph2dot [OPTIONS]\n");
36 "-i INFILE set INFILE as input file, stdin if omitted\n"
37 "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
38 "-h print this help\n");
46 static void print_digraph(FILE *outfile
, AVFilterGraph
*graph
)
50 fprintf(outfile
, "digraph G {\n");
51 fprintf(outfile
, "node [shape=box]\n");
52 fprintf(outfile
, "rankdir=LR\n");
54 for (i
= 0; i
< graph
->filter_count
; i
++) {
55 char filter_ctx_label
[128];
56 const AVFilterContext
*filter_ctx
= graph
->filters
[i
];
58 snprintf(filter_ctx_label
, sizeof(filter_ctx_label
), "%s (%s)",
60 filter_ctx
->filter
->name
);
62 for (j
= 0; j
< filter_ctx
->output_count
; j
++) {
63 AVFilterLink
*link
= filter_ctx
->outputs
[j
];
65 char dst_filter_ctx_label
[128];
66 const AVFilterContext
*dst_filter_ctx
= link
->dst
;
68 snprintf(dst_filter_ctx_label
, sizeof(dst_filter_ctx_label
),
71 dst_filter_ctx
->filter
->name
);
73 fprintf(outfile
, "\"%s\" -> \"%s\"",
74 filter_ctx_label
, dst_filter_ctx_label
);
75 if (link
->type
== AVMEDIA_TYPE_VIDEO
) {
77 " [ label= \"fmt:%s w:%d h:%d tb:%d/%d\" ]",
78 av_pix_fmt_descriptors
[link
->format
].name
,
79 link
->w
, link
->h
, link
->time_base
.num
,
81 } else if (link
->type
== AVMEDIA_TYPE_AUDIO
) {
83 av_get_channel_layout_string(buf
, sizeof(buf
), -1,
84 link
->channel_layout
);
86 " [ label= \"fmt:%s sr:%d cl:%s\" ]",
87 av_get_sample_fmt_name(link
->format
),
88 link
->sample_rate
, buf
);
90 fprintf(outfile
, ";\n");
94 fprintf(outfile
, "}\n");
97 int main(int argc
, char **argv
)
99 const char *outfilename
= NULL
;
100 const char *infilename
= NULL
;
101 FILE *outfile
= NULL
;
103 char *graph_string
= NULL
;
104 AVFilterGraph
*graph
= av_mallocz(sizeof(AVFilterGraph
));
107 av_log_set_level(AV_LOG_DEBUG
);
109 while ((c
= getopt(argc
, argv
, "hi:o:")) != -1) {
118 outfilename
= optarg
;
125 if (!infilename
|| !strcmp(infilename
, "-"))
126 infilename
= "/dev/stdin";
127 infile
= fopen(infilename
, "r");
129 fprintf(stderr
, "Impossible to open input file '%s': %s\n",
130 infilename
, strerror(errno
));
134 if (!outfilename
|| !strcmp(outfilename
, "-"))
135 outfilename
= "/dev/stdout";
136 outfile
= fopen(outfilename
, "w");
138 fprintf(stderr
, "Impossible to open output file '%s': %s\n",
139 outfilename
, strerror(errno
));
143 /* read from infile and put it in a buffer */
145 unsigned int count
= 0;
146 struct line
*line
, *last_line
, *first_line
;
148 last_line
= first_line
= av_malloc(sizeof(struct line
));
150 while (fgets(last_line
->data
, sizeof(last_line
->data
), infile
)) {
151 struct line
*new_line
= av_malloc(sizeof(struct line
));
152 count
+= strlen(last_line
->data
);
153 last_line
->next
= new_line
;
154 last_line
= new_line
;
156 last_line
->next
= NULL
;
158 graph_string
= av_malloc(count
+ 1);
160 for (line
= first_line
; line
->next
; line
= line
->next
) {
161 unsigned int l
= strlen(line
->data
);
162 memcpy(p
, line
->data
, l
);
168 avfilter_register_all();
170 if (avfilter_graph_parse(graph
, graph_string
, NULL
, NULL
, NULL
) < 0) {
171 fprintf(stderr
, "Impossible to parse the graph description\n");
175 if (avfilter_graph_config(graph
, NULL
) < 0)
178 print_digraph(outfile
, graph
);