matroskadec: fix crash when parsing invalid mkv
[platform/upstream/libav.git] / tools / graph2dot.c
1 /*
2  * Copyright (c) 2008-2010 Stefano Sabatini
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "config.h"
22 #if HAVE_UNISTD_H
23 #include <unistd.h>             /* getopt */
24 #endif
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/audioconvert.h"
32 #include "libavfilter/avfilter.h"
33
34 #if !HAVE_GETOPT
35 #include "compat/getopt.c"
36 #endif
37
38 static void usage(void)
39 {
40     printf("Convert a libavfilter graph to a dot file.\n");
41     printf("Usage: graph2dot [OPTIONS]\n");
42     printf("\n"
43            "Options:\n"
44            "-i INFILE         set INFILE as input file, stdin if omitted\n"
45            "-o OUTFILE        set OUTFILE as output file, stdout if omitted\n"
46            "-h                print this help\n");
47 }
48
49 struct line {
50     char data[256];
51     struct line *next;
52 };
53
54 static void print_digraph(FILE *outfile, AVFilterGraph *graph)
55 {
56     int i, j;
57
58     fprintf(outfile, "digraph G {\n");
59     fprintf(outfile, "node [shape=box]\n");
60     fprintf(outfile, "rankdir=LR\n");
61
62     for (i = 0; i < graph->nb_filters; i++) {
63         char filter_ctx_label[128];
64         const AVFilterContext *filter_ctx = graph->filters[i];
65
66         snprintf(filter_ctx_label, sizeof(filter_ctx_label), "%s (%s)",
67                  filter_ctx->name,
68                  filter_ctx->filter->name);
69
70         for (j = 0; j < filter_ctx->nb_outputs; j++) {
71             AVFilterLink *link = filter_ctx->outputs[j];
72             if (link) {
73                 char dst_filter_ctx_label[128];
74                 const AVFilterContext *dst_filter_ctx = link->dst;
75
76                 snprintf(dst_filter_ctx_label, sizeof(dst_filter_ctx_label),
77                          "%s (%s)",
78                          dst_filter_ctx->name,
79                          dst_filter_ctx->filter->name);
80
81                 fprintf(outfile, "\"%s\" -> \"%s\"",
82                         filter_ctx_label, dst_filter_ctx_label);
83                 if (link->type == AVMEDIA_TYPE_VIDEO) {
84                     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
85                     fprintf(outfile,
86                             " [ label= \"fmt:%s w:%d h:%d tb:%d/%d\" ]",
87                             desc->name, link->w, link->h, link->time_base.num,
88                             link->time_base.den);
89                 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
90                     char buf[255];
91                     av_get_channel_layout_string(buf, sizeof(buf), -1,
92                                                  link->channel_layout);
93                     fprintf(outfile,
94                             " [ label= \"fmt:%s sr:%d cl:%s\" ]",
95                             av_get_sample_fmt_name(link->format),
96                             link->sample_rate, buf);
97                 }
98                 fprintf(outfile, ";\n");
99             }
100         }
101     }
102     fprintf(outfile, "}\n");
103 }
104
105 int main(int argc, char **argv)
106 {
107     const char *outfilename = NULL;
108     const char *infilename  = NULL;
109     FILE *outfile           = NULL;
110     FILE *infile            = NULL;
111     char *graph_string      = NULL;
112     AVFilterGraph *graph = av_mallocz(sizeof(AVFilterGraph));
113     char c;
114
115     av_log_set_level(AV_LOG_DEBUG);
116
117     while ((c = getopt(argc, argv, "hi:o:")) != -1) {
118         switch (c) {
119         case 'h':
120             usage();
121             return 0;
122         case 'i':
123             infilename = optarg;
124             break;
125         case 'o':
126             outfilename = optarg;
127             break;
128         case '?':
129             return 1;
130         }
131     }
132
133     if (!infilename || !strcmp(infilename, "-"))
134         infilename = "/dev/stdin";
135     infile = fopen(infilename, "r");
136     if (!infile) {
137         fprintf(stderr, "Failed to open input file '%s': %s\n",
138                 infilename, strerror(errno));
139         return 1;
140     }
141
142     if (!outfilename || !strcmp(outfilename, "-"))
143         outfilename = "/dev/stdout";
144     outfile = fopen(outfilename, "w");
145     if (!outfile) {
146         fprintf(stderr, "Failed to open output file '%s': %s\n",
147                 outfilename, strerror(errno));
148         return 1;
149     }
150
151     /* read from infile and put it in a buffer */
152     {
153         unsigned int count = 0;
154         struct line *line, *last_line, *first_line;
155         char *p;
156         last_line = first_line = av_malloc(sizeof(struct line));
157
158         while (fgets(last_line->data, sizeof(last_line->data), infile)) {
159             struct line *new_line = av_malloc(sizeof(struct line));
160             count += strlen(last_line->data);
161             last_line->next = new_line;
162             last_line       = new_line;
163         }
164         last_line->next = NULL;
165
166         graph_string = av_malloc(count + 1);
167         p = graph_string;
168         for (line = first_line; line->next; line = line->next) {
169             unsigned int l = strlen(line->data);
170             memcpy(p, line->data, l);
171             p += l;
172         }
173         *p = '\0';
174     }
175
176     avfilter_register_all();
177
178     if (avfilter_graph_parse(graph, graph_string, NULL, NULL, NULL) < 0) {
179         fprintf(stderr, "Failed to parse the graph description\n");
180         return 1;
181     }
182
183     if (avfilter_graph_config(graph, NULL) < 0)
184         return 1;
185
186     print_digraph(outfile, graph);
187     fflush(outfile);
188
189     return 0;
190 }