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