graph2dot.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/avfiltergraph.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->filter_count; 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->output_count; 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. link->srcpad->name, link->dstpad->name);
  71. if (link->type == AVMEDIA_TYPE_VIDEO) {
  72. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  73. fprintf(outfile,
  74. "fmt:%s w:%d h:%d tb:%d/%d",
  75. desc->name,
  76. link->w, link->h,
  77. link->time_base.num, link->time_base.den);
  78. } else if (link->type == AVMEDIA_TYPE_AUDIO) {
  79. char buf[255];
  80. av_get_channel_layout_string(buf, sizeof(buf), -1,
  81. link->channel_layout);
  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 = av_mallocz(sizeof(AVFilterGraph));
  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, "Impossible 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, "Impossible 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. unsigned int count = 0;
  138. struct line *line, *last_line, *first_line;
  139. char *p;
  140. last_line = first_line = av_malloc(sizeof(struct line));
  141. while (fgets(last_line->data, sizeof(last_line->data), infile)) {
  142. struct line *new_line = av_malloc(sizeof(struct line));
  143. count += strlen(last_line->data);
  144. last_line->next = new_line;
  145. last_line = new_line;
  146. }
  147. last_line->next = NULL;
  148. graph_string = av_malloc(count + 1);
  149. p = graph_string;
  150. for (line = first_line; line->next; line = line->next) {
  151. unsigned int l = strlen(line->data);
  152. memcpy(p, line->data, l);
  153. p += l;
  154. }
  155. *p = '\0';
  156. }
  157. avfilter_register_all();
  158. if (avfilter_graph_parse(graph, graph_string, NULL, NULL, NULL) < 0) {
  159. fprintf(stderr, "Impossible to parse the graph description\n");
  160. return 1;
  161. }
  162. if (avfilter_graph_config(graph, NULL) < 0)
  163. return 1;
  164. print_digraph(outfile, graph);
  165. fflush(outfile);
  166. return 0;
  167. }