graph2dot.c 5.5 KB

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