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