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