You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.8KB

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