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