0b8dd03b83825c460d167347ba183729a8a67bfa
[ffmpeg.git] / tools / graph2dot.c
1 /*
2  * Copyright (c) 2008-2010 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <unistd.h>             /* getopt */
22
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/audioconvert.h"
25 #include "libavfilter/avfiltergraph.h"
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
65                 snprintf(dst_filter_ctx_label, sizeof(dst_filter_ctx_label),
66                          "%s (%s)",
67                          dst_filter_ctx->name,
68                          dst_filter_ctx->filter->name);
69
70                 fprintf(outfile, "\"%s\" -> \"%s\"",
71                         filter_ctx_label, dst_filter_ctx_label);
72                 if (link->type == AVMEDIA_TYPE_VIDEO) {
73                     fprintf(outfile,
74                             " [ label= \"inpad:%s fmt:%s w:%d h:%d tb:%d/%d outpad:%s\" ]",
75                             link->srcpad->name,
76                             av_pix_fmt_descriptors[link->format].name,
77                             link->w, link->h, link->time_base.num,
78                             link->time_base.den,
79                             link->dstpad->name);
80                 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
81                     char buf[255];
82                     av_get_channel_layout_string(buf, sizeof(buf), -1,
83                                                  link->channel_layout);
84                     fprintf(outfile,
85                             " [ label= \"inpad:%s fmt:%s sr:%"PRId64" cl:%s tb:%d/%d outpad:%s\" ]",
86                             link->srcpad->name,
87                             av_get_sample_fmt_name(link->format),
88                             link->sample_rate, buf,
89                             link->time_base.num, link->time_base.den,
90                             link->dstpad->name);
91                 }
92                 fprintf(outfile, ";\n");
93             }
94         }
95     }
96     fprintf(outfile, "}\n");
97 }
98
99 int main(int argc, char **argv)
100 {
101     const char *outfilename = NULL;
102     const char *infilename  = NULL;
103     FILE *outfile           = NULL;
104     FILE *infile            = NULL;
105     char *graph_string      = NULL;
106     AVFilterGraph *graph = av_mallocz(sizeof(AVFilterGraph));
107     char c;
108
109     av_log_set_level(AV_LOG_DEBUG);
110
111     while ((c = getopt(argc, argv, "hi:o:")) != -1) {
112         switch (c) {
113         case 'h':
114             usage();
115             return 0;
116         case 'i':
117             infilename = optarg;
118             break;
119         case 'o':
120             outfilename = optarg;
121             break;
122         case '?':
123             return 1;
124         }
125     }
126
127     if (!infilename || !strcmp(infilename, "-"))
128         infilename = "/dev/stdin";
129     infile = fopen(infilename, "r");
130     if (!infile) {
131         fprintf(stderr, "Impossible to open input file '%s': %s\n",
132                 infilename, strerror(errno));
133         return 1;
134     }
135
136     if (!outfilename || !strcmp(outfilename, "-"))
137         outfilename = "/dev/stdout";
138     outfile = fopen(outfilename, "w");
139     if (!outfile) {
140         fprintf(stderr, "Impossible to open output file '%s': %s\n",
141                 outfilename, strerror(errno));
142         return 1;
143     }
144
145     /* read from infile and put it in a buffer */
146     {
147         unsigned int count = 0;
148         struct line *line, *last_line, *first_line;
149         char *p;
150         last_line = first_line = av_malloc(sizeof(struct line));
151
152         while (fgets(last_line->data, sizeof(last_line->data), infile)) {
153             struct line *new_line = av_malloc(sizeof(struct line));
154             count += strlen(last_line->data);
155             last_line->next = new_line;
156             last_line       = new_line;
157         }
158         last_line->next = NULL;
159
160         graph_string = av_malloc(count + 1);
161         p = graph_string;
162         for (line = first_line; line->next; line = line->next) {
163             unsigned int l = strlen(line->data);
164             memcpy(p, line->data, l);
165             p += l;
166         }
167         *p = '\0';
168     }
169
170     avfilter_register_all();
171
172     if (avfilter_graph_parse(graph, graph_string, NULL, NULL, NULL) < 0) {
173         fprintf(stderr, "Impossible to parse the graph description\n");
174         return 1;
175     }
176
177     if (avfilter_graph_config(graph, NULL) < 0)
178         return 1;
179
180     print_digraph(outfile, graph);
181     fflush(outfile);
182
183     return 0;
184 }