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.7KB

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