annotate ffmpeg/tools/graph2dot.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * Copyright (c) 2008-2010 Stefano Sabatini
yading@11 3 *
yading@11 4 * This file is part of FFmpeg.
yading@11 5 *
yading@11 6 * FFmpeg is free software; you can redistribute it and/or
yading@11 7 * modify it under the terms of the GNU Lesser General Public
yading@11 8 * License as published by the Free Software Foundation; either
yading@11 9 * version 2.1 of the License, or (at your option) any later version.
yading@11 10 *
yading@11 11 * FFmpeg is distributed in the hope that it will be useful,
yading@11 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 14 * Lesser General Public License for more details.
yading@11 15 *
yading@11 16 * You should have received a copy of the GNU Lesser General Public
yading@11 17 * License along with FFmpeg; if not, write to the Free Software
yading@11 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 19 */
yading@11 20
yading@11 21 #include "config.h"
yading@11 22 #if HAVE_UNISTD_H
yading@11 23 #include <unistd.h> /* getopt */
yading@11 24 #endif
yading@11 25 #include <stdio.h>
yading@11 26 #include <string.h>
yading@11 27
yading@11 28 #include "libavutil/channel_layout.h"
yading@11 29 #include "libavutil/mem.h"
yading@11 30 #include "libavutil/pixdesc.h"
yading@11 31 #include "libavfilter/avfilter.h"
yading@11 32
yading@11 33 #if !HAVE_GETOPT
yading@11 34 #include "compat/getopt.c"
yading@11 35 #endif
yading@11 36
yading@11 37 static void usage(void)
yading@11 38 {
yading@11 39 printf("Convert a libavfilter graph to a dot file\n");
yading@11 40 printf("Usage: graph2dot [OPTIONS]\n");
yading@11 41 printf("\n"
yading@11 42 "Options:\n"
yading@11 43 "-i INFILE set INFILE as input file, stdin if omitted\n"
yading@11 44 "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
yading@11 45 "-h print this help\n");
yading@11 46 }
yading@11 47
yading@11 48 struct line {
yading@11 49 char data[256];
yading@11 50 struct line *next;
yading@11 51 };
yading@11 52
yading@11 53 static void print_digraph(FILE *outfile, AVFilterGraph *graph)
yading@11 54 {
yading@11 55 int i, j;
yading@11 56
yading@11 57 fprintf(outfile, "digraph G {\n");
yading@11 58 fprintf(outfile, "node [shape=box]\n");
yading@11 59 fprintf(outfile, "rankdir=LR\n");
yading@11 60
yading@11 61 for (i = 0; i < graph->nb_filters; i++) {
yading@11 62 char filter_ctx_label[128];
yading@11 63 const AVFilterContext *filter_ctx = graph->filters[i];
yading@11 64
yading@11 65 snprintf(filter_ctx_label, sizeof(filter_ctx_label), "%s\\n(%s)",
yading@11 66 filter_ctx->name,
yading@11 67 filter_ctx->filter->name);
yading@11 68
yading@11 69 for (j = 0; j < filter_ctx->output_count; j++) {
yading@11 70 AVFilterLink *link = filter_ctx->outputs[j];
yading@11 71 if (link) {
yading@11 72 char dst_filter_ctx_label[128];
yading@11 73 const AVFilterContext *dst_filter_ctx = link->dst;
yading@11 74
yading@11 75 snprintf(dst_filter_ctx_label, sizeof(dst_filter_ctx_label),
yading@11 76 "%s\\n(%s)",
yading@11 77 dst_filter_ctx->name,
yading@11 78 dst_filter_ctx->filter->name);
yading@11 79
yading@11 80 fprintf(outfile, "\"%s\" -> \"%s\" [ label= \"inpad:%s -> outpad:%s\\n",
yading@11 81 filter_ctx_label, dst_filter_ctx_label,
yading@11 82 link->srcpad->name, link->dstpad->name);
yading@11 83
yading@11 84 if (link->type == AVMEDIA_TYPE_VIDEO) {
yading@11 85 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
yading@11 86 fprintf(outfile,
yading@11 87 "fmt:%s w:%d h:%d tb:%d/%d",
yading@11 88 desc->name,
yading@11 89 link->w, link->h,
yading@11 90 link->time_base.num, link->time_base.den);
yading@11 91 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
yading@11 92 char buf[255];
yading@11 93 av_get_channel_layout_string(buf, sizeof(buf), -1,
yading@11 94 link->channel_layout);
yading@11 95 fprintf(outfile,
yading@11 96 "fmt:%s sr:%d cl:%s tb:%d/%d",
yading@11 97 av_get_sample_fmt_name(link->format),
yading@11 98 link->sample_rate, buf,
yading@11 99 link->time_base.num, link->time_base.den);
yading@11 100 }
yading@11 101 fprintf(outfile, "\" ];\n");
yading@11 102 }
yading@11 103 }
yading@11 104 }
yading@11 105 fprintf(outfile, "}\n");
yading@11 106 }
yading@11 107
yading@11 108 int main(int argc, char **argv)
yading@11 109 {
yading@11 110 const char *outfilename = NULL;
yading@11 111 const char *infilename = NULL;
yading@11 112 FILE *outfile = NULL;
yading@11 113 FILE *infile = NULL;
yading@11 114 char *graph_string = NULL;
yading@11 115 AVFilterGraph *graph = av_mallocz(sizeof(AVFilterGraph));
yading@11 116 char c;
yading@11 117
yading@11 118 av_log_set_level(AV_LOG_DEBUG);
yading@11 119
yading@11 120 while ((c = getopt(argc, argv, "hi:o:")) != -1) {
yading@11 121 switch (c) {
yading@11 122 case 'h':
yading@11 123 usage();
yading@11 124 return 0;
yading@11 125 case 'i':
yading@11 126 infilename = optarg;
yading@11 127 break;
yading@11 128 case 'o':
yading@11 129 outfilename = optarg;
yading@11 130 break;
yading@11 131 case '?':
yading@11 132 return 1;
yading@11 133 }
yading@11 134 }
yading@11 135
yading@11 136 if (!infilename || !strcmp(infilename, "-"))
yading@11 137 infilename = "/dev/stdin";
yading@11 138 infile = fopen(infilename, "r");
yading@11 139 if (!infile) {
yading@11 140 fprintf(stderr, "Impossible to open input file '%s': %s\n",
yading@11 141 infilename, strerror(errno));
yading@11 142 return 1;
yading@11 143 }
yading@11 144
yading@11 145 if (!outfilename || !strcmp(outfilename, "-"))
yading@11 146 outfilename = "/dev/stdout";
yading@11 147 outfile = fopen(outfilename, "w");
yading@11 148 if (!outfile) {
yading@11 149 fprintf(stderr, "Impossible to open output file '%s': %s\n",
yading@11 150 outfilename, strerror(errno));
yading@11 151 return 1;
yading@11 152 }
yading@11 153
yading@11 154 /* read from infile and put it in a buffer */
yading@11 155 {
yading@11 156 unsigned int count = 0;
yading@11 157 struct line *line, *last_line, *first_line;
yading@11 158 char *p;
yading@11 159 last_line = first_line = av_malloc(sizeof(struct line));
yading@11 160
yading@11 161 while (fgets(last_line->data, sizeof(last_line->data), infile)) {
yading@11 162 struct line *new_line = av_malloc(sizeof(struct line));
yading@11 163 count += strlen(last_line->data);
yading@11 164 last_line->next = new_line;
yading@11 165 last_line = new_line;
yading@11 166 }
yading@11 167 last_line->next = NULL;
yading@11 168
yading@11 169 graph_string = av_malloc(count + 1);
yading@11 170 p = graph_string;
yading@11 171 for (line = first_line; line->next; line = line->next) {
yading@11 172 unsigned int l = strlen(line->data);
yading@11 173 memcpy(p, line->data, l);
yading@11 174 p += l;
yading@11 175 }
yading@11 176 *p = '\0';
yading@11 177 }
yading@11 178
yading@11 179 avfilter_register_all();
yading@11 180
yading@11 181 if (avfilter_graph_parse(graph, graph_string, NULL, NULL, NULL) < 0) {
yading@11 182 fprintf(stderr, "Impossible to parse the graph description\n");
yading@11 183 return 1;
yading@11 184 }
yading@11 185
yading@11 186 if (avfilter_graph_config(graph, NULL) < 0)
yading@11 187 return 1;
yading@11 188
yading@11 189 print_digraph(outfile, graph);
yading@11 190 fflush(outfile);
yading@11 191
yading@11 192 return 0;
yading@11 193 }