graph2dot.c 6.4 KB

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