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 <unistd.h> /* getopt */
22 
23 #undef HAVE_AV_CONFIG_H
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/audioconvert.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\" ]",
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\" ]",
80  link->sample_rate, buf);
81  }
82  fprintf(outfile, ";\n");
83  }
84  }
85  }
86  fprintf(outfile, "}\n");
87 }
88 
89 int main(int argc, char **argv)
90 {
91  const char *outfilename = NULL;
92  const char *infilename = NULL;
93  FILE *outfile = NULL;
94  FILE *infile = NULL;
95  char *graph_string = NULL;
96  AVFilterGraph *graph = av_mallocz(sizeof(AVFilterGraph));
97  char c;
98 
100 
101  while ((c = getopt(argc, argv, "hi:o:")) != -1) {
102  switch(c) {
103  case 'h':
104  usage();
105  return 0;
106  case 'i':
107  infilename = optarg;
108  break;
109  case 'o':
110  outfilename = optarg;
111  break;
112  case '?':
113  return 1;
114  }
115  }
116 
117  if (!infilename || !strcmp(infilename, "-"))
118  infilename = "/dev/stdin";
119  infile = fopen(infilename, "r");
120  if (!infile) {
121  fprintf(stderr, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
122  return 1;
123  }
124 
125  if (!outfilename || !strcmp(outfilename, "-"))
126  outfilename = "/dev/stdout";
127  outfile = fopen(outfilename, "w");
128  if (!outfile) {
129  fprintf(stderr, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
130  return 1;
131  }
132 
133  /* read from infile and put it in a buffer */
134  {
135  unsigned int count = 0;
136  struct line *line, *last_line, *first_line;
137  char *p;
138  last_line = first_line = av_malloc(sizeof(struct line));
139 
140  while (fgets(last_line->data, sizeof(last_line->data), infile)) {
141  struct line *new_line = av_malloc(sizeof(struct line));
142  count += strlen(last_line->data);
143  last_line->next = new_line;
144  last_line = new_line;
145  }
146  last_line->next = NULL;
147 
148  graph_string = av_malloc(count + 1);
149  p = graph_string;
150  for (line = first_line; line->next; line = line->next) {
151  unsigned int l = strlen(line->data);
152  memcpy(p, line->data, l);
153  p += l;
154  }
155  *p = '\0';
156  }
157 
159 
160  if (avfilter_graph_parse(graph, graph_string, NULL, NULL, NULL) < 0) {
161  fprintf(stderr, "Impossible to parse the graph description\n");
162  return 1;
163  }
164 
165  if (avfilter_graph_config(graph, NULL) < 0)
166  return 1;
167 
168  print_digraph(outfile, graph);
169  fflush(outfile);
170 
171  return 0;
172 }
AVFilterContext ** filters
Definition: avfiltergraph.h:29
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:154
void avfilter_register_all(void)
Initialize the filter system.
Definition: allfilters.c:29
int num
numerator
Definition: rational.h:44
struct line * next
Definition: graph2dot.c:41
char data[256]
Definition: graph2dot.c:40
char * name
name of this filter instance
Definition: avfilter.h:543
unsigned filter_count
Definition: avfiltergraph.h:28
static void print_digraph(FILE *outfile, AVFilterGraph *graph)
Definition: graph2dot.c:44
int main(int argc, char **argv)
Definition: graph2dot.c:89
const char * name
Definition: pixdesc.h:56
audio conversion routines
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
Definition: graph2dot.c:39
void av_log_set_level(int level)
Definition: log.c:162
const AVPixFmtDescriptor av_pix_fmt_descriptors[PIX_FMT_NB]
The array of all the pixel format descriptors.
Definition: pixdesc.c:119
static void usage(void)
Definition: graph2dot.c:28
unsigned output_count
number of output pads
Definition: avfilter.h:549
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:45
NULL
Definition: eval.c:50
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:64
#define printf
Definition: internal.h:151
const char * name
filter name
Definition: avfilter.h:498
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:551
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:125
int den
denominator
Definition: rational.h:45
AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:541
An instance of a filter.
Definition: avfilter.h:538
#define fprintf
Definition: internal.h:153
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:333
FILE * outfile
Definition: audiogen.c:94
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.
Definition: audioconvert.c:92