graph2dot.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2008-2010 Stefano Sabatini
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "config.h"
  21. #if HAVE_UNISTD_H
  22. #include <unistd.h> /* getopt */
  23. #endif
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavfilter/avfilter.h"
  30. #if !HAVE_GETOPT
  31. #include "compat/getopt.c"
  32. #endif
  33. static void usage(void)
  34. {
  35. printf("Convert a libavfilter graph to a dot file.\n");
  36. printf("Usage: graph2dot [OPTIONS]\n");
  37. printf("\n"
  38. "Options:\n"
  39. "-i INFILE set INFILE as input file, stdin if omitted\n"
  40. "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
  41. "-h print this help\n");
  42. }
  43. struct line {
  44. char data[256];
  45. struct line *next;
  46. };
  47. static void print_digraph(FILE *outfile, AVFilterGraph *graph)
  48. {
  49. int i, j;
  50. fprintf(outfile, "digraph G {\n");
  51. fprintf(outfile, "node [shape=box]\n");
  52. fprintf(outfile, "rankdir=LR\n");
  53. for (i = 0; i < graph->nb_filters; i++) {
  54. char filter_ctx_label[128];
  55. const AVFilterContext *filter_ctx = graph->filters[i];
  56. snprintf(filter_ctx_label, sizeof(filter_ctx_label), "%s (%s)",
  57. filter_ctx->name,
  58. filter_ctx->filter->name);
  59. for (j = 0; j < filter_ctx->nb_outputs; j++) {
  60. AVFilterLink *link = filter_ctx->outputs[j];
  61. if (link) {
  62. char dst_filter_ctx_label[128];
  63. const AVFilterContext *dst_filter_ctx = link->dst;
  64. snprintf(dst_filter_ctx_label, sizeof(dst_filter_ctx_label),
  65. "%s (%s)",
  66. dst_filter_ctx->name,
  67. dst_filter_ctx->filter->name);
  68. fprintf(outfile, "\"%s\" -> \"%s\"",
  69. filter_ctx_label, dst_filter_ctx_label);
  70. if (link->type == AVMEDIA_TYPE_VIDEO) {
  71. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  72. fprintf(outfile,
  73. " [ label= \"fmt:%s w:%d h:%d tb:%d/%d\" ]",
  74. desc->name, link->w, link->h, link->time_base.num,
  75. link->time_base.den);
  76. } else if (link->type == AVMEDIA_TYPE_AUDIO) {
  77. char buf[255];
  78. av_get_channel_layout_string(buf, sizeof(buf), -1,
  79. link->channel_layout);
  80. fprintf(outfile,
  81. " [ label= \"fmt:%s sr:%d cl:%s\" ]",
  82. av_get_sample_fmt_name(link->format),
  83. link->sample_rate, buf);
  84. }
  85. fprintf(outfile, ";\n");
  86. }
  87. }
  88. }
  89. fprintf(outfile, "}\n");
  90. }
  91. int main(int argc, char **argv)
  92. {
  93. const char *outfilename = NULL;
  94. const char *infilename = NULL;
  95. FILE *outfile = NULL;
  96. FILE *infile = NULL;
  97. char *graph_string = NULL;
  98. AVFilterGraph *graph = av_mallocz(sizeof(AVFilterGraph));
  99. char c;
  100. av_log_set_level(AV_LOG_DEBUG);
  101. while ((c = getopt(argc, argv, "hi:o:")) != -1) {
  102. switch (c) {
  103. case 'h':
  104. usage();
  105. return 0;
  106. case 'i':
  107. infilename = optarg;
  108. break;
  109. case 'o':
  110. outfilename = optarg;
  111. break;
  112. case '?':
  113. return 1;
  114. }
  115. }
  116. if (!infilename || !strcmp(infilename, "-"))
  117. infilename = "/dev/stdin";
  118. infile = fopen(infilename, "r");
  119. if (!infile) {
  120. fprintf(stderr, "Failed to open input file '%s': %s\n",
  121. infilename, strerror(errno));
  122. return 1;
  123. }
  124. if (!outfilename || !strcmp(outfilename, "-"))
  125. outfilename = "/dev/stdout";
  126. outfile = fopen(outfilename, "w");
  127. if (!outfile) {
  128. fprintf(stderr, "Failed to open output file '%s': %s\n",
  129. outfilename, strerror(errno));
  130. return 1;
  131. }
  132. /* read from infile and put it in a buffer */
  133. {
  134. unsigned int count = 0;
  135. struct line *line, *last_line, *first_line;
  136. char *p;
  137. last_line = first_line = av_malloc(sizeof(struct line));
  138. while (fgets(last_line->data, sizeof(last_line->data), infile)) {
  139. struct line *new_line = av_malloc(sizeof(struct line));
  140. count += strlen(last_line->data);
  141. last_line->next = new_line;
  142. last_line = new_line;
  143. }
  144. last_line->next = NULL;
  145. graph_string = av_malloc(count + 1);
  146. p = graph_string;
  147. for (line = first_line; line->next; line = line->next) {
  148. unsigned int l = strlen(line->data);
  149. memcpy(p, line->data, l);
  150. p += l;
  151. }
  152. *p = '\0';
  153. }
  154. avfilter_register_all();
  155. if (avfilter_graph_parse(graph, graph_string, NULL, NULL, NULL) < 0) {
  156. fprintf(stderr, "Failed to parse the graph description\n");
  157. return 1;
  158. }
  159. if (avfilter_graph_config(graph, NULL) < 0)
  160. return 1;
  161. print_digraph(outfile, graph);
  162. fflush(outfile);
  163. return 0;
  164. }