tools: Include io.h for open/read/write/close if unistd.h doesn't exist
[libav.git] / tools / graph2dot.c
CommitLineData
11ab237e
SS
1/*
2 * Copyright (c) 2008-2010 Stefano Sabatini
3 *
2912e87a 4 * This file is part of Libav.
11ab237e 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
11ab237e
SS
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 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
11ab237e
SS
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
2912e87a 17 * License along with Libav; if not, write to the Free Software
11ab237e
SS
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <unistd.h> /* getopt */
1d9c2dc8
MS
22#include <stdio.h>
23#include <string.h>
11ab237e 24
1d9c2dc8 25#include "libavutil/mem.h"
11ab237e 26#include "libavutil/pixdesc.h"
009026ef 27#include "libavutil/audioconvert.h"
1e80a0ea 28#include "libavfilter/avfiltergraph.h"
11ab237e
SS
29
30static 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
41struct line {
42 char data[256];
43 struct line *next;
44};
45
46static 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 (%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
4e81b5f5
DB
68 snprintf(dst_filter_ctx_label, sizeof(dst_filter_ctx_label),
69 "%s (%s)",
11ab237e
SS
70 dst_filter_ctx->name,
71 dst_filter_ctx->filter->name);
72
4e81b5f5
DB
73 fprintf(outfile, "\"%s\" -> \"%s\"",
74 filter_ctx_label, dst_filter_ctx_label);
009026ef 75 if (link->type == AVMEDIA_TYPE_VIDEO) {
4e81b5f5
DB
76 fprintf(outfile,
77 " [ label= \"fmt:%s w:%d h:%d tb:%d/%d\" ]",
009026ef 78 av_pix_fmt_descriptors[link->format].name,
4e81b5f5
DB
79 link->w, link->h, link->time_base.num,
80 link->time_base.den);
009026ef
SS
81 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
82 char buf[255];
4e81b5f5
DB
83 av_get_channel_layout_string(buf, sizeof(buf), -1,
84 link->channel_layout);
85 fprintf(outfile,
fa2d7473 86 " [ label= \"fmt:%s sr:%d cl:%s\" ]",
009026ef
SS
87 av_get_sample_fmt_name(link->format),
88 link->sample_rate, buf);
89 }
90 fprintf(outfile, ";\n");
11ab237e
SS
91 }
92 }
93 }
94 fprintf(outfile, "}\n");
95}
96
97int main(int argc, char **argv)
98{
99 const char *outfilename = NULL;
4e81b5f5
DB
100 const char *infilename = NULL;
101 FILE *outfile = NULL;
102 FILE *infile = NULL;
103 char *graph_string = NULL;
11ab237e
SS
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) {
4e81b5f5 110 switch (c) {
11ab237e
SS
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) {
4e81b5f5
DB
129 fprintf(stderr, "Impossible to open input file '%s': %s\n",
130 infilename, strerror(errno));
11ab237e
SS
131 return 1;
132 }
133
134 if (!outfilename || !strcmp(outfilename, "-"))
135 outfilename = "/dev/stdout";
136 outfile = fopen(outfilename, "w");
137 if (!outfile) {
4e81b5f5
DB
138 fprintf(stderr, "Impossible to open output file '%s': %s\n",
139 outfilename, strerror(errno));
11ab237e
SS
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;
4e81b5f5 154 last_line = new_line;
11ab237e
SS
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
2a24df93 175 if (avfilter_graph_config(graph, NULL) < 0)
11ab237e
SS
176 return 1;
177
178 print_digraph(outfile, graph);
179 fflush(outfile);
180
181 return 0;
182}