graph2dot.c
Go to the documentation of this file.
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/mem.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/audioconvert.h"
32 
33 #if !HAVE_GETOPT
34 #include "compat/getopt.c"
35 #endif
36 
37 static void usage(void)
38 {
39  printf("Convert a libavfilter graph to a dot file\n");
40  printf("Usage: graph2dot [OPTIONS]\n");
41  printf("\n"
42  "Options:\n"
43  "-i INFILE set INFILE as input file, stdin if omitted\n"
44  "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
45  "-h print this help\n");
46 }
47 
48 struct line {
49  char data[256];
50  struct line *next;
51 };
52 
53 static void print_digraph(FILE *outfile, AVFilterGraph *graph)
54 {
55  int i, j;
56 
57  fprintf(outfile, "digraph G {\n");
58  fprintf(outfile, "node [shape=box]\n");
59  fprintf(outfile, "rankdir=LR\n");
60 
61  for (i = 0; i < graph->filter_count; i++) {
62  char filter_ctx_label[128];
63  const AVFilterContext *filter_ctx = graph->filters[i];
64 
65  snprintf(filter_ctx_label, sizeof(filter_ctx_label), "%s (%s)",
66  filter_ctx->name,
67  filter_ctx->filter->name);
68 
69  for (j = 0; j < filter_ctx->output_count; j++) {
70  AVFilterLink *link = filter_ctx->outputs[j];
71  if (link) {
72  char dst_filter_ctx_label[128];
73  const AVFilterContext *dst_filter_ctx = link->dst;
74 
75  snprintf(dst_filter_ctx_label, sizeof(dst_filter_ctx_label),
76  "%s (%s)",
77  dst_filter_ctx->name,
78  dst_filter_ctx->filter->name);
79 
80  fprintf(outfile, "\"%s\" -> \"%s\"",
81  filter_ctx_label, dst_filter_ctx_label);
82  if (link->type == AVMEDIA_TYPE_VIDEO) {
83  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
84  fprintf(outfile,
85  " [ label= \"fmt:%s w:%d h:%d tb:%d/%d\" ]",
86  desc->name, link->w, link->h, link->time_base.num,
87  link->time_base.den);
88  } else if (link->type == AVMEDIA_TYPE_AUDIO) {
89  char buf[255];
90  av_get_channel_layout_string(buf, sizeof(buf), -1,
91  link->channel_layout);
92  fprintf(outfile,
93  " [ label= \"fmt:%s sr:%d cl:%s\" ]",
95  link->sample_rate, buf);
96  }
97  fprintf(outfile, ";\n");
98  }
99  }
100  }
101  fprintf(outfile, "}\n");
102 }
103 
104 int main(int argc, char **argv)
105 {
106  const char *outfilename = NULL;
107  const char *infilename = NULL;
108  FILE *outfile = NULL;
109  FILE *infile = NULL;
110  char *graph_string = NULL;
111  AVFilterGraph *graph = av_mallocz(sizeof(AVFilterGraph));
112  char c;
113 
115 
116  while ((c = getopt(argc, argv, "hi:o:")) != -1) {
117  switch (c) {
118  case 'h':
119  usage();
120  return 0;
121  case 'i':
122  infilename = optarg;
123  break;
124  case 'o':
125  outfilename = optarg;
126  break;
127  case '?':
128  return 1;
129  }
130  }
131 
132  if (!infilename || !strcmp(infilename, "-"))
133  infilename = "/dev/stdin";
134  infile = fopen(infilename, "r");
135  if (!infile) {
136  fprintf(stderr, "Impossible to open input file '%s': %s\n",
137  infilename, strerror(errno));
138  return 1;
139  }
140 
141  if (!outfilename || !strcmp(outfilename, "-"))
142  outfilename = "/dev/stdout";
143  outfile = fopen(outfilename, "w");
144  if (!outfile) {
145  fprintf(stderr, "Impossible to open output file '%s': %s\n",
146  outfilename, strerror(errno));
147  return 1;
148  }
149 
150  /* read from infile and put it in a buffer */
151  {
152  unsigned int count = 0;
153  struct line *line, *last_line, *first_line;
154  char *p;
155  last_line = first_line = av_malloc(sizeof(struct line));
156 
157  while (fgets(last_line->data, sizeof(last_line->data), infile)) {
158  struct line *new_line = av_malloc(sizeof(struct line));
159  count += strlen(last_line->data);
160  last_line->next = new_line;
161  last_line = new_line;
162  }
163  last_line->next = NULL;
164 
165  graph_string = av_malloc(count + 1);
166  p = graph_string;
167  for (line = first_line; line->next; line = line->next) {
168  unsigned int l = strlen(line->data);
169  memcpy(p, line->data, l);
170  p += l;
171  }
172  *p = '\0';
173  }
174 
176 
177  if (avfilter_graph_parse(graph, graph_string, NULL, NULL, NULL) < 0) {
178  fprintf(stderr, "Impossible to parse the graph description\n");
179  return 1;
180  }
181 
182  if (avfilter_graph_config(graph, NULL) < 0)
183  return 1;
184 
185  print_digraph(outfile, graph);
186  fflush(outfile);
187 
188  return 0;
189 }
AVFilterContext ** filters
Definition: avfiltergraph.h:31
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
void avfilter_register_all(void)
Initialize the filter system.
Definition: allfilters.c:39
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1435
memory handling functions
int num
numerator
Definition: rational.h:44
struct line * next
Definition: graph2dot.c:50
char data[256]
Definition: graph2dot.c:49
char * name
name of this filter instance
Definition: avfilter.h:423
unsigned filter_count
Definition: avfiltergraph.h:30
static void print_digraph(FILE *outfile, AVFilterGraph *graph)
Definition: graph2dot.c:53
int main(int argc, char **argv)
Definition: graph2dot.c:104
const char * name
Definition: pixdesc.h:56
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
Definition: graph2dot.c:48
void av_log_set_level(int level)
Definition: log.c:168
static void usage(void)
Definition: graph2dot.c:37
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:47
NULL
Definition: eval.c:52
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
static int getopt(int argc, char *argv[], char *opts)
Definition: getopt.c:43
const char * name
filter name
Definition: avfilter.h:372
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
int den
denominator
Definition: rational.h:45
static char * optarg
Definition: getopt.c:39
AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:421
An instance of a filter.
Definition: avfilter.h:418
int avfilter_graph_parse(AVFilterGraph *graph, const char *filters, AVFilterInOut *inputs, AVFilterInOut *outputs, void *log_ctx)
Add a graph described by a string to a graph.
Definition: graphparser.c:453
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
FILE * outfile
Definition: audiogen.c:96
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.