graph2dot.c
Go to the documentation of this file.
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 "config.h"
22 #if HAVE_UNISTD_H
23 #include <unistd.h> /* getopt */
24 #endif
25 #include <stdio.h>
26 #include <string.h>
27 
29 #include "libavutil/mem.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavfilter/avfilter.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->nb_filters; 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\\n(%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\\n(%s)",
77  dst_filter_ctx->name,
78  dst_filter_ctx->filter->name);
79 
80  fprintf(outfile, "\"%s\" -> \"%s\" [ label= \"inpad:%s -> outpad:%s\\n",
81  filter_ctx_label, dst_filter_ctx_label,
82  link->srcpad->name, link->dstpad->name);
83 
84  if (link->type == AVMEDIA_TYPE_VIDEO) {
85  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
86  fprintf(outfile,
87  "fmt:%s w:%d h:%d tb:%d/%d",
88  desc->name,
89  link->w, link->h,
90  link->time_base.num, link->time_base.den);
91  } else if (link->type == AVMEDIA_TYPE_AUDIO) {
92  char buf[255];
93  av_get_channel_layout_string(buf, sizeof(buf), -1,
94  link->channel_layout);
95  fprintf(outfile,
96  "fmt:%s sr:%d cl:%s tb:%d/%d",
98  link->sample_rate, buf,
99  link->time_base.num, link->time_base.den);
100  }
101  fprintf(outfile, "\" ];\n");
102  }
103  }
104  }
105  fprintf(outfile, "}\n");
106 }
107 
108 int main(int argc, char **argv)
109 {
110  const char *outfilename = NULL;
111  const char *infilename = NULL;
112  FILE *outfile = NULL;
113  FILE *infile = NULL;
114  char *graph_string = NULL;
115  AVFilterGraph *graph = av_mallocz(sizeof(AVFilterGraph));
116  char c;
117 
119 
120  while ((c = getopt(argc, argv, "hi:o:")) != -1) {
121  switch (c) {
122  case 'h':
123  usage();
124  return 0;
125  case 'i':
126  infilename = optarg;
127  break;
128  case 'o':
129  outfilename = optarg;
130  break;
131  case '?':
132  return 1;
133  }
134  }
135 
136  if (!infilename || !strcmp(infilename, "-"))
137  infilename = "/dev/stdin";
138  infile = fopen(infilename, "r");
139  if (!infile) {
140  fprintf(stderr, "Impossible to open input file '%s': %s\n",
141  infilename, strerror(errno));
142  return 1;
143  }
144 
145  if (!outfilename || !strcmp(outfilename, "-"))
146  outfilename = "/dev/stdout";
147  outfile = fopen(outfilename, "w");
148  if (!outfile) {
149  fprintf(stderr, "Impossible to open output file '%s': %s\n",
150  outfilename, strerror(errno));
151  return 1;
152  }
153 
154  /* read from infile and put it in a buffer */
155  {
156  unsigned int count = 0;
157  struct line *line, *last_line, *first_line;
158  char *p;
159  last_line = first_line = av_malloc(sizeof(struct line));
160 
161  while (fgets(last_line->data, sizeof(last_line->data), infile)) {
162  struct line *new_line = av_malloc(sizeof(struct line));
163  count += strlen(last_line->data);
164  last_line->next = new_line;
165  last_line = new_line;
166  }
167  last_line->next = NULL;
168 
169  graph_string = av_malloc(count + 1);
170  p = graph_string;
171  for (line = first_line; line->next; line = line->next) {
172  unsigned int l = strlen(line->data);
173  memcpy(p, line->data, l);
174  p += l;
175  }
176  *p = '\0';
177  }
178 
180 
181  if (avfilter_graph_parse(graph, graph_string, NULL, NULL, NULL) < 0) {
182  fprintf(stderr, "Impossible to parse the graph description\n");
183  return 1;
184  }
185 
186  if (avfilter_graph_config(graph, NULL) < 0)
187  return 1;
188 
189  print_digraph(outfile, graph);
190  fflush(outfile);
191 
192  return 0;
193 }
AVFilterContext ** filters
Definition: avfilter.h:997
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:205
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
external API header
memory handling functions
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
int num
numerator
Definition: rational.h:44
struct line * next
Definition: graph2dot.c:50
char data[256]
Definition: graph2dot.c:49
const char * name
Pad name.
char * name
name of this filter instance
Definition: avfilter.h:529
static void print_digraph(FILE *outfile, AVFilterGraph *graph)
Definition: graph2dot.c:53
void avfilter_register_all(void)
Initialize the filter system.
Definition: allfilters.c:40
int main(int argc, char **argv)
Definition: graph2dot.c:108
const char * name
Definition: pixdesc.h:56
Definition: graph2dot.c:48
void av_log_set_level(int level)
Definition: log.c:269
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
static void usage(void)
Definition: graph2dot.c:37
audio channel layout utility functions
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:55
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
void * buf
Definition: avisynth_c.h:594
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:73
synthesis window for stochastic i
const char * name
filter name
Definition: avfilter.h:437
unsigned nb_filters
Definition: avfilter.h:999
#define snprintf
Definition: snprintf.h:34
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
static double c[64]
int den
denominator
Definition: rational.h:45
static char * optarg
Definition: getopt.c:39
printf("static const uint8_t my_array[100] = {\n")
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip please avoid problems through this extra level of scrutiny For cosmetics only commits you should e g by running git config global user name My Name git config global user email my email which is either set in your personal configuration file through git config core editor or set by one of the following environment VISUAL or EDITOR Log messages should be concise but descriptive Explain why you made a what you did will be obvious from the changes themselves most of the time Saying just bug fix or is bad Remember that people of varying skill levels look at and educate themselves while reading through your code Don t include filenames in log Git provides that information Possibly make the commit message have a descriptive first line
Definition: git-howto.txt:153
An instance of a filter.
Definition: avfilter.h:524
void INT64 INT64 count
Definition: avisynth_c.h:594
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:527
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.
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:447