51a1ab109c7256aea7ba8148a4fda13a33da2ecf
[libav.git] / tools / graph2dot.c
1 /*
2 * Copyright (c) 2008-2010 Stefano Sabatini
3 *
4 * This file is part of Libav.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <unistd.h> /* getopt */
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "libavutil/mem.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/audioconvert.h"
28 #include "libavfilter/avfiltergraph.h"
29
30 static void usage(void)
31 {
32 printf("Convert a libavfilter graph to a dot file\n");
33 printf("Usage: graph2dot [OPTIONS]\n");
34 printf("\n"
35 "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");
39 }
40
41 struct line {
42 char data[256];
43 struct line *next;
44 };
45
46 static void print_digraph(FILE *outfile, AVFilterGraph *graph)
47 {
48 int i, j;
49
50 fprintf(outfile, "digraph G {\n");
51 fprintf(outfile, "node [shape=box]\n");
52 fprintf(outfile, "rankdir=LR\n");
53
54 for (i = 0; i < graph->filter_count; i++) {
55 char filter_ctx_label[128];
56 const AVFilterContext *filter_ctx = graph->filters[i];
57
58 snprintf(filter_ctx_label, sizeof(filter_ctx_label), "%s (%s)",
59 filter_ctx->name,
60 filter_ctx->filter->name);
61
62 for (j = 0; j < filter_ctx->output_count; j++) {
63 AVFilterLink *link = filter_ctx->outputs[j];
64 if (link) {
65 char dst_filter_ctx_label[128];
66 const AVFilterContext *dst_filter_ctx = link->dst;
67
68 snprintf(dst_filter_ctx_label, sizeof(dst_filter_ctx_label),
69 "%s (%s)",
70 dst_filter_ctx->name,
71 dst_filter_ctx->filter->name);
72
73 fprintf(outfile, "\"%s\" -> \"%s\"",
74 filter_ctx_label, dst_filter_ctx_label);
75 if (link->type == AVMEDIA_TYPE_VIDEO) {
76 fprintf(outfile,
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,
80 link->time_base.den);
81 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
82 char buf[255];
83 av_get_channel_layout_string(buf, sizeof(buf), -1,
84 link->channel_layout);
85 fprintf(outfile,
86 " [ label= \"fmt:%s sr:%d cl:%s\" ]",
87 av_get_sample_fmt_name(link->format),
88 link->sample_rate, buf);
89 }
90 fprintf(outfile, ";\n");
91 }
92 }
93 }
94 fprintf(outfile, "}\n");
95 }
96
97 int main(int argc, char **argv)
98 {
99 const char *outfilename = NULL;
100 const char *infilename = NULL;
101 FILE *outfile = NULL;
102 FILE *infile = NULL;
103 char *graph_string = NULL;
104 AVFilterGraph *graph = av_mallocz(sizeof(AVFilterGraph));
105 char c;
106
107 av_log_set_level(AV_LOG_DEBUG);
108
109 while ((c = getopt(argc, argv, "hi:o:")) != -1) {
110 switch (c) {
111 case 'h':
112 usage();
113 return 0;
114 case 'i':
115 infilename = optarg;
116 break;
117 case 'o':
118 outfilename = optarg;
119 break;
120 case '?':
121 return 1;
122 }
123 }
124
125 if (!infilename || !strcmp(infilename, "-"))
126 infilename = "/dev/stdin";
127 infile = fopen(infilename, "r");
128 if (!infile) {
129 fprintf(stderr, "Impossible to open input file '%s': %s\n",
130 infilename, strerror(errno));
131 return 1;
132 }
133
134 if (!outfilename || !strcmp(outfilename, "-"))
135 outfilename = "/dev/stdout";
136 outfile = fopen(outfilename, "w");
137 if (!outfile) {
138 fprintf(stderr, "Impossible to open output file '%s': %s\n",
139 outfilename, strerror(errno));
140 return 1;
141 }
142
143 /* read from infile and put it in a buffer */
144 {
145 unsigned int count = 0;
146 struct line *line, *last_line, *first_line;
147 char *p;
148 last_line = first_line = av_malloc(sizeof(struct line));
149
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;
155 }
156 last_line->next = NULL;
157
158 graph_string = av_malloc(count + 1);
159 p = graph_string;
160 for (line = first_line; line->next; line = line->next) {
161 unsigned int l = strlen(line->data);
162 memcpy(p, line->data, l);
163 p += l;
164 }
165 *p = '\0';
166 }
167
168 avfilter_register_all();
169
170 if (avfilter_graph_parse(graph, graph_string, NULL, NULL, NULL) < 0) {
171 fprintf(stderr, "Impossible to parse the graph description\n");
172 return 1;
173 }
174
175 if (avfilter_graph_config(graph, NULL) < 0)
176 return 1;
177
178 print_digraph(outfile, graph);
179 fflush(outfile);
180
181 return 0;
182 }