00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022 #if HAVE_UNISTD_H
00023 #include <unistd.h>
00024 #endif
00025 #include <stdio.h>
00026 #include <string.h>
00027
00028 #include "libavutil/mem.h"
00029 #include "libavutil/pixdesc.h"
00030 #include "libavutil/audioconvert.h"
00031 #include "libavfilter/avfiltergraph.h"
00032
00033 #if !HAVE_GETOPT
00034 #include "compat/getopt.c"
00035 #endif
00036
00037 static void usage(void)
00038 {
00039 printf("Convert a libavfilter graph to a dot file\n");
00040 printf("Usage: graph2dot [OPTIONS]\n");
00041 printf("\n"
00042 "Options:\n"
00043 "-i INFILE set INFILE as input file, stdin if omitted\n"
00044 "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
00045 "-h print this help\n");
00046 }
00047
00048 struct line {
00049 char data[256];
00050 struct line *next;
00051 };
00052
00053 static void print_digraph(FILE *outfile, AVFilterGraph *graph)
00054 {
00055 int i, j;
00056
00057 fprintf(outfile, "digraph G {\n");
00058 fprintf(outfile, "node [shape=box]\n");
00059 fprintf(outfile, "rankdir=LR\n");
00060
00061 for (i = 0; i < graph->filter_count; i++) {
00062 char filter_ctx_label[128];
00063 const AVFilterContext *filter_ctx = graph->filters[i];
00064
00065 snprintf(filter_ctx_label, sizeof(filter_ctx_label), "%s\\n(%s)",
00066 filter_ctx->name,
00067 filter_ctx->filter->name);
00068
00069 for (j = 0; j < filter_ctx->output_count; j++) {
00070 AVFilterLink *link = filter_ctx->outputs[j];
00071 if (link) {
00072 char dst_filter_ctx_label[128];
00073 const AVFilterContext *dst_filter_ctx = link->dst;
00074
00075 snprintf(dst_filter_ctx_label, sizeof(dst_filter_ctx_label),
00076 "%s\\n(%s)",
00077 dst_filter_ctx->name,
00078 dst_filter_ctx->filter->name);
00079
00080 fprintf(outfile, "\"%s\" -> \"%s\" [ label= \"inpad:%s -> outpad:%s\\n",
00081 filter_ctx_label, dst_filter_ctx_label,
00082 link->srcpad->name, link->dstpad->name);
00083
00084 if (link->type == AVMEDIA_TYPE_VIDEO) {
00085 fprintf(outfile,
00086 "fmt:%s w:%d h:%d tb:%d/%d",
00087 av_pix_fmt_descriptors[link->format].name,
00088 link->w, link->h,
00089 link->time_base.num, link->time_base.den);
00090 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
00091 char buf[255];
00092 av_get_channel_layout_string(buf, sizeof(buf), -1,
00093 link->channel_layout);
00094 fprintf(outfile,
00095 "fmt:%s sr:%d cl:%s tb:%d/%d",
00096 av_get_sample_fmt_name(link->format),
00097 link->sample_rate, buf,
00098 link->time_base.num, link->time_base.den);
00099 }
00100 fprintf(outfile, "\" ];\n");
00101 }
00102 }
00103 }
00104 fprintf(outfile, "}\n");
00105 }
00106
00107 int main(int argc, char **argv)
00108 {
00109 const char *outfilename = NULL;
00110 const char *infilename = NULL;
00111 FILE *outfile = NULL;
00112 FILE *infile = NULL;
00113 char *graph_string = NULL;
00114 AVFilterGraph *graph = av_mallocz(sizeof(AVFilterGraph));
00115 char c;
00116
00117 av_log_set_level(AV_LOG_DEBUG);
00118
00119 while ((c = getopt(argc, argv, "hi:o:")) != -1) {
00120 switch (c) {
00121 case 'h':
00122 usage();
00123 return 0;
00124 case 'i':
00125 infilename = optarg;
00126 break;
00127 case 'o':
00128 outfilename = optarg;
00129 break;
00130 case '?':
00131 return 1;
00132 }
00133 }
00134
00135 if (!infilename || !strcmp(infilename, "-"))
00136 infilename = "/dev/stdin";
00137 infile = fopen(infilename, "r");
00138 if (!infile) {
00139 fprintf(stderr, "Impossible to open input file '%s': %s\n",
00140 infilename, strerror(errno));
00141 return 1;
00142 }
00143
00144 if (!outfilename || !strcmp(outfilename, "-"))
00145 outfilename = "/dev/stdout";
00146 outfile = fopen(outfilename, "w");
00147 if (!outfile) {
00148 fprintf(stderr, "Impossible to open output file '%s': %s\n",
00149 outfilename, strerror(errno));
00150 return 1;
00151 }
00152
00153
00154 {
00155 unsigned int count = 0;
00156 struct line *line, *last_line, *first_line;
00157 char *p;
00158 last_line = first_line = av_malloc(sizeof(struct line));
00159
00160 while (fgets(last_line->data, sizeof(last_line->data), infile)) {
00161 struct line *new_line = av_malloc(sizeof(struct line));
00162 count += strlen(last_line->data);
00163 last_line->next = new_line;
00164 last_line = new_line;
00165 }
00166 last_line->next = NULL;
00167
00168 graph_string = av_malloc(count + 1);
00169 p = graph_string;
00170 for (line = first_line; line->next; line = line->next) {
00171 unsigned int l = strlen(line->data);
00172 memcpy(p, line->data, l);
00173 p += l;
00174 }
00175 *p = '\0';
00176 }
00177
00178 avfilter_register_all();
00179
00180 if (avfilter_graph_parse(graph, graph_string, NULL, NULL, NULL) < 0) {
00181 fprintf(stderr, "Impossible to parse the graph description\n");
00182 return 1;
00183 }
00184
00185 if (avfilter_graph_config(graph, NULL) < 0)
00186 return 1;
00187
00188 print_digraph(outfile, graph);
00189 fflush(outfile);
00190
00191 return 0;
00192 }