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