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