graphdump.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Filter graphs to bad ASCII-art
  3. * Copyright (c) 2012 Nicolas George
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include "libavutil/channel_layout.h"
  23. #include "libavutil/bprint.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "avfiltergraph.h"
  27. #include "internal.h"
  28. static int print_link_prop(AVBPrint *buf, AVFilterLink *link)
  29. {
  30. char *format;
  31. char layout[64];
  32. AVBPrint dummy_buffer = { 0 };
  33. if (!buf)
  34. buf = &dummy_buffer;
  35. switch (link->type) {
  36. case AVMEDIA_TYPE_VIDEO:
  37. format = av_x_if_null(av_get_pix_fmt_name(link->format), "?");
  38. av_bprintf(buf, "[%dx%d %d:%d %s]", link->w, link->h,
  39. link->sample_aspect_ratio.num,
  40. link->sample_aspect_ratio.den,
  41. format);
  42. break;
  43. case AVMEDIA_TYPE_AUDIO:
  44. av_get_channel_layout_string(layout, sizeof(layout),
  45. link->channels, link->channel_layout);
  46. format = av_x_if_null(av_get_sample_fmt_name(link->format), "?");
  47. av_bprintf(buf, "[%dHz %s:%s]",
  48. (int)link->sample_rate, format, layout);
  49. break;
  50. default:
  51. av_bprintf(buf, "?");
  52. break;
  53. }
  54. return buf->len;
  55. }
  56. static void avfilter_graph_dump_to_buf(AVBPrint *buf, AVFilterGraph *graph)
  57. {
  58. unsigned i, j, x, e;
  59. for (i = 0; i < graph->nb_filters; i++) {
  60. AVFilterContext *filter = graph->filters[i];
  61. unsigned max_src_name = 0, max_dst_name = 0;
  62. unsigned max_in_name = 0, max_out_name = 0;
  63. unsigned max_in_fmt = 0, max_out_fmt = 0;
  64. unsigned width, height, in_indent;
  65. unsigned lname = strlen(filter->name);
  66. unsigned ltype = strlen(filter->filter->name);
  67. for (j = 0; j < filter->nb_inputs; j++) {
  68. AVFilterLink *l = filter->inputs[j];
  69. unsigned ln = strlen(l->src->name) + 1 + strlen(l->srcpad->name);
  70. max_src_name = FFMAX(max_src_name, ln);
  71. max_in_name = FFMAX(max_in_name, strlen(l->dstpad->name));
  72. max_in_fmt = FFMAX(max_in_fmt, print_link_prop(NULL, l));
  73. }
  74. for (j = 0; j < filter->nb_outputs; j++) {
  75. AVFilterLink *l = filter->outputs[j];
  76. unsigned ln = strlen(l->dst->name) + 1 + strlen(l->dstpad->name);
  77. max_dst_name = FFMAX(max_dst_name, ln);
  78. max_out_name = FFMAX(max_out_name, strlen(l->srcpad->name));
  79. max_out_fmt = FFMAX(max_out_fmt, print_link_prop(NULL, l));
  80. }
  81. in_indent = max_src_name + max_in_name + max_in_fmt;
  82. in_indent += in_indent ? 4 : 0;
  83. width = FFMAX(lname + 2, ltype + 4);
  84. height = FFMAX3(2, filter->nb_inputs, filter->nb_outputs);
  85. av_bprint_chars(buf, ' ', in_indent);
  86. av_bprintf(buf, "+");
  87. av_bprint_chars(buf, '-', width);
  88. av_bprintf(buf, "+\n");
  89. for (j = 0; j < height; j++) {
  90. unsigned in_no = j - (height - filter->nb_inputs ) / 2;
  91. unsigned out_no = j - (height - filter->nb_outputs) / 2;
  92. /* Input link */
  93. if (in_no < filter->nb_inputs) {
  94. AVFilterLink *l = filter->inputs[in_no];
  95. e = buf->len + max_src_name + 2;
  96. av_bprintf(buf, "%s:%s", l->src->name, l->srcpad->name);
  97. av_bprint_chars(buf, '-', e - buf->len);
  98. e = buf->len + max_in_fmt + 2 +
  99. max_in_name - strlen(l->dstpad->name);
  100. print_link_prop(buf, l);
  101. av_bprint_chars(buf, '-', e - buf->len);
  102. av_bprintf(buf, "%s", l->dstpad->name);
  103. } else {
  104. av_bprint_chars(buf, ' ', in_indent);
  105. }
  106. /* Filter */
  107. av_bprintf(buf, "|");
  108. if (j == (height - 2) / 2) {
  109. x = (width - lname) / 2;
  110. av_bprintf(buf, "%*s%-*s", x, "", width - x, filter->name);
  111. } else if (j == (height - 2) / 2 + 1) {
  112. x = (width - ltype - 2) / 2;
  113. av_bprintf(buf, "%*s(%s)%*s", x, "", filter->filter->name,
  114. width - ltype - 2 - x, "");
  115. } else {
  116. av_bprint_chars(buf, ' ', width);
  117. }
  118. av_bprintf(buf, "|");
  119. /* Output link */
  120. if (out_no < filter->nb_outputs) {
  121. AVFilterLink *l = filter->outputs[out_no];
  122. unsigned ln = strlen(l->dst->name) + 1 +
  123. strlen(l->dstpad->name);
  124. e = buf->len + max_out_name + 2;
  125. av_bprintf(buf, "%s", l->srcpad->name);
  126. av_bprint_chars(buf, '-', e - buf->len);
  127. e = buf->len + max_out_fmt + 2 +
  128. max_dst_name - ln;
  129. print_link_prop(buf, l);
  130. av_bprint_chars(buf, '-', e - buf->len);
  131. av_bprintf(buf, "%s:%s", l->dst->name, l->dstpad->name);
  132. }
  133. av_bprintf(buf, "\n");
  134. }
  135. av_bprint_chars(buf, ' ', in_indent);
  136. av_bprintf(buf, "+");
  137. av_bprint_chars(buf, '-', width);
  138. av_bprintf(buf, "+\n");
  139. av_bprintf(buf, "\n");
  140. }
  141. }
  142. char *avfilter_graph_dump(AVFilterGraph *graph, const char *options)
  143. {
  144. AVBPrint buf;
  145. char *dump;
  146. av_bprint_init(&buf, 0, 0);
  147. avfilter_graph_dump_to_buf(&buf, graph);
  148. av_bprint_init(&buf, buf.len + 1, buf.len + 1);
  149. avfilter_graph_dump_to_buf(&buf, graph);
  150. av_bprint_finalize(&buf, &dump);
  151. return dump;
  152. }