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