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 #include "libavutil/pixdesc.h"
00024 #include "libavutil/audioconvert.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),
00066 "%s (%s)",
00067 dst_filter_ctx->name,
00068 dst_filter_ctx->filter->name);
00069
00070 fprintf(outfile, "\"%s\" -> \"%s\"",
00071 filter_ctx_label, dst_filter_ctx_label);
00072 if (link->type == AVMEDIA_TYPE_VIDEO) {
00073 fprintf(outfile,
00074 " [ label= \"fmt:%s w:%d h:%d tb:%d/%d\" ]",
00075 av_pix_fmt_descriptors[link->format].name,
00076 link->w, link->h, link->time_base.num,
00077 link->time_base.den);
00078 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
00079 char buf[255];
00080 av_get_channel_layout_string(buf, sizeof(buf), -1,
00081 link->channel_layout);
00082 fprintf(outfile,
00083 " [ label= \"fmt:%s sr:%"PRId64" cl:%s tb:%d/%d\" ]",
00084 av_get_sample_fmt_name(link->format),
00085 link->sample_rate, buf,
00086 link->time_base.num, link->time_base.den);
00087 }
00088 fprintf(outfile, ";\n");
00089 }
00090 }
00091 }
00092 fprintf(outfile, "}\n");
00093 }
00094
00095 int main(int argc, char **argv)
00096 {
00097 const char *outfilename = NULL;
00098 const char *infilename = NULL;
00099 FILE *outfile = NULL;
00100 FILE *infile = NULL;
00101 char *graph_string = NULL;
00102 AVFilterGraph *graph = av_mallocz(sizeof(AVFilterGraph));
00103 char c;
00104
00105 av_log_set_level(AV_LOG_DEBUG);
00106
00107 while ((c = getopt(argc, argv, "hi:o:")) != -1) {
00108 switch (c) {
00109 case 'h':
00110 usage();
00111 return 0;
00112 case 'i':
00113 infilename = optarg;
00114 break;
00115 case 'o':
00116 outfilename = optarg;
00117 break;
00118 case '?':
00119 return 1;
00120 }
00121 }
00122
00123 if (!infilename || !strcmp(infilename, "-"))
00124 infilename = "/dev/stdin";
00125 infile = fopen(infilename, "r");
00126 if (!infile) {
00127 fprintf(stderr, "Impossible to open input file '%s': %s\n",
00128 infilename, strerror(errno));
00129 return 1;
00130 }
00131
00132 if (!outfilename || !strcmp(outfilename, "-"))
00133 outfilename = "/dev/stdout";
00134 outfile = fopen(outfilename, "w");
00135 if (!outfile) {
00136 fprintf(stderr, "Impossible to open output file '%s': %s\n",
00137 outfilename, strerror(errno));
00138 return 1;
00139 }
00140
00141
00142 {
00143 unsigned int count = 0;
00144 struct line *line, *last_line, *first_line;
00145 char *p;
00146 last_line = first_line = av_malloc(sizeof(struct line));
00147
00148 while (fgets(last_line->data, sizeof(last_line->data), infile)) {
00149 struct line *new_line = av_malloc(sizeof(struct line));
00150 count += strlen(last_line->data);
00151 last_line->next = new_line;
00152 last_line = new_line;
00153 }
00154 last_line->next = NULL;
00155
00156 graph_string = av_malloc(count + 1);
00157 p = graph_string;
00158 for (line = first_line; line->next; line = line->next) {
00159 unsigned int l = strlen(line->data);
00160 memcpy(p, line->data, l);
00161 p += l;
00162 }
00163 *p = '\0';
00164 }
00165
00166 avfilter_register_all();
00167
00168 if (avfilter_graph_parse(graph, graph_string, NULL, NULL, NULL) < 0) {
00169 fprintf(stderr, "Impossible to parse the graph description\n");
00170 return 1;
00171 }
00172
00173 if (avfilter_graph_config(graph, NULL) < 0)
00174 return 1;
00175
00176 print_digraph(outfile, graph);
00177 fflush(outfile);
00178
00179 return 0;
00180 }