• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

tools/graph2dot.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008-2010 Stefano Sabatini
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * Libav is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with Libav; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include <unistd.h>             /* getopt */
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\" ]",
00079                             av_get_sample_fmt_name(link->format),
00080                             link->sample_rate, buf);
00081                 }
00082                 fprintf(outfile, ";\n");
00083             }
00084         }
00085     }
00086     fprintf(outfile, "}\n");
00087 }
00088 
00089 int main(int argc, char **argv)
00090 {
00091     const char *outfilename = NULL;
00092     const char *infilename = NULL;
00093     FILE *outfile = NULL;
00094     FILE *infile = NULL;
00095     char *graph_string = NULL;
00096     AVFilterGraph *graph = av_mallocz(sizeof(AVFilterGraph));
00097     char c;
00098 
00099     av_log_set_level(AV_LOG_DEBUG);
00100 
00101     while ((c = getopt(argc, argv, "hi:o:")) != -1) {
00102         switch(c) {
00103         case 'h':
00104             usage();
00105             return 0;
00106         case 'i':
00107             infilename = optarg;
00108             break;
00109         case 'o':
00110             outfilename = optarg;
00111             break;
00112         case '?':
00113             return 1;
00114         }
00115     }
00116 
00117     if (!infilename || !strcmp(infilename, "-"))
00118         infilename = "/dev/stdin";
00119     infile = fopen(infilename, "r");
00120     if (!infile) {
00121         fprintf(stderr, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
00122         return 1;
00123     }
00124 
00125     if (!outfilename || !strcmp(outfilename, "-"))
00126         outfilename = "/dev/stdout";
00127     outfile = fopen(outfilename, "w");
00128     if (!outfile) {
00129         fprintf(stderr, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
00130         return 1;
00131     }
00132 
00133     /* read from infile and put it in a buffer */
00134     {
00135         unsigned int count = 0;
00136         struct line *line, *last_line, *first_line;
00137         char *p;
00138         last_line = first_line = av_malloc(sizeof(struct line));
00139 
00140         while (fgets(last_line->data, sizeof(last_line->data), infile)) {
00141             struct line *new_line = av_malloc(sizeof(struct line));
00142             count += strlen(last_line->data);
00143             last_line->next = new_line;
00144             last_line = new_line;
00145         }
00146         last_line->next = NULL;
00147 
00148         graph_string = av_malloc(count + 1);
00149         p = graph_string;
00150         for (line = first_line; line->next; line = line->next) {
00151             unsigned int l = strlen(line->data);
00152             memcpy(p, line->data, l);
00153             p += l;
00154         }
00155         *p = '\0';
00156     }
00157 
00158     avfilter_register_all();
00159 
00160     if (avfilter_graph_parse(graph, graph_string, NULL, NULL, NULL) < 0) {
00161         fprintf(stderr, "Impossible to parse the graph description\n");
00162         return 1;
00163     }
00164 
00165     if (avfilter_graph_config(graph, NULL) < 0)
00166         return 1;
00167 
00168     print_digraph(outfile, graph);
00169     fflush(outfile);
00170 
00171     return 0;
00172 }
Generated on Sun Apr 22 2012 21:54:09 for Libav by doxygen 1.7.1