graph2dot.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "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\\n(%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\\n(%s)",
  66. dst_filter_ctx->name,
  67. dst_filter_ctx->filter->name);
  68. fprintf(outfile, "\"%s\" -> \"%s\" [ label= \"inpad:%s -> outpad:%s\\n",
  69. filter_ctx_label, dst_filter_ctx_label,
  70. avfilter_pad_get_name(link->srcpad, 0),
  71. avfilter_pad_get_name(link->dstpad, 0));
  72. if (link->type == AVMEDIA_TYPE_VIDEO) {
  73. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  74. fprintf(outfile,
  75. "fmt:%s w:%d h:%d tb:%d/%d",
  76. desc->name,
  77. link->w, link->h,
  78. link->time_base.num, link->time_base.den);
  79. } else if (link->type == AVMEDIA_TYPE_AUDIO) {
  80. char buf[255];
  81. av_channel_layout_describe(&link->ch_layout, buf, sizeof(buf));
  82. fprintf(outfile,
  83. "fmt:%s sr:%d cl:%s tb:%d/%d",
  84. av_get_sample_fmt_name(link->format),
  85. link->sample_rate, buf,
  86. link->time_base.num, link->time_base.den);
  87. }
  88. fprintf(outfile, "\" ];\n");
  89. }
  90. }
  91. }
  92. fprintf(outfile, "}\n");
  93. }
  94. int main(int argc, char **argv)
  95. {
  96. const char *outfilename = NULL;
  97. const char *infilename = NULL;
  98. FILE *outfile = NULL;
  99. FILE *infile = NULL;
  100. char *graph_string = NULL;
  101. AVFilterGraph *graph = NULL;
  102. char c;
  103. av_log_set_level(AV_LOG_DEBUG);
  104. while ((c = getopt(argc, argv, "hi:o:")) != -1) {
  105. switch (c) {
  106. case 'h':
  107. usage();
  108. return 0;
  109. case 'i':
  110. infilename = optarg;
  111. break;
  112. case 'o':
  113. outfilename = optarg;
  114. break;
  115. case '?':
  116. return 1;
  117. }
  118. }
  119. if (!infilename || !strcmp(infilename, "-"))
  120. infilename = "/dev/stdin";
  121. infile = fopen(infilename, "r");
  122. if (!infile) {
  123. fprintf(stderr, "Failed to open input file '%s': %s\n",
  124. infilename, strerror(errno));
  125. return 1;
  126. }
  127. if (!outfilename || !strcmp(outfilename, "-"))
  128. outfilename = "/dev/stdout";
  129. outfile = fopen(outfilename, "w");
  130. if (!outfile) {
  131. fprintf(stderr, "Failed to open output file '%s': %s\n",
  132. outfilename, strerror(errno));
  133. return 1;
  134. }
  135. /* read from infile and put it in a buffer */
  136. {
  137. int64_t count = 0;
  138. struct line *line, *last_line, *first_line;
  139. char *p;
  140. last_line = first_line = av_malloc(sizeof(struct line));
  141. if (!last_line) {
  142. fprintf(stderr, "Memory allocation failure\n");
  143. return 1;
  144. }
  145. while (fgets(last_line->data, sizeof(last_line->data), infile)) {
  146. struct line *new_line = av_malloc(sizeof(struct line));
  147. if (!new_line) {
  148. fprintf(stderr, "Memory allocation failure\n");
  149. return 1;
  150. }
  151. count += strlen(last_line->data);
  152. last_line->next = new_line;
  153. last_line = new_line;
  154. }
  155. last_line->next = NULL;
  156. graph_string = av_malloc(count + 1);
  157. if (!graph_string) {
  158. fprintf(stderr, "Memory allocation failure\n");
  159. return 1;
  160. }
  161. p = graph_string;
  162. for (line = first_line; line->next; line = line->next) {
  163. size_t l = strlen(line->data);
  164. memcpy(p, line->data, l);
  165. p += l;
  166. }
  167. *p = '\0';
  168. }
  169. graph = avfilter_graph_alloc();
  170. if (!graph) {
  171. fprintf(stderr, "Memory allocation failure\n");
  172. return 1;
  173. }
  174. if (avfilter_graph_parse(graph, graph_string, NULL, NULL, NULL) < 0) {
  175. fprintf(stderr, "Failed to parse the graph description\n");
  176. return 1;
  177. }
  178. if (avfilter_graph_config(graph, NULL) < 0)
  179. return 1;
  180. print_digraph(outfile, graph);
  181. fflush(outfile);
  182. return 0;
  183. }