]> git.sesse.net Git - ffmpeg/blob - tools/graph2dot.c
Merge remote-tracking branch 'qatar/master'
[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
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\\n(%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\\n(%s)",
67                          dst_filter_ctx->name,
68                          dst_filter_ctx->filter->name);
69
70                 fprintf(outfile, "\"%s\" -> \"%s\" [ label= \"inpad:%s -> outpad:%s\\n",
71                         filter_ctx_label, dst_filter_ctx_label,
72                         link->srcpad->name, link->dstpad->name);
73
74                 if (link->type == AVMEDIA_TYPE_VIDEO) {
75                     fprintf(outfile,
76                             "fmt:%s w:%d h:%d tb:%d/%d",
77                             av_pix_fmt_descriptors[link->format].name,
78                             link->w, link->h,
79                             link->time_base.num, link->time_base.den);
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                             "fmt:%s sr:%d cl:%s tb:%d/%d",
86                             av_get_sample_fmt_name(link->format),
87                             link->sample_rate, buf,
88                             link->time_base.num, link->time_base.den);
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 }