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.

165 lines
6.2KB

  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/audioconvert.h"
  23. #include "libavutil/bprint.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "avfiltergraph.h"
  27. static int print_link_prop(AVBPrint *buf, AVFilterLink *link)
  28. {
  29. char *format;
  30. char layout[64];
  31. if (!buf)
  32. buf = &(AVBPrint){ 0 }; /* dummy buffer */
  33. switch (link->type) {
  34. case AVMEDIA_TYPE_VIDEO:
  35. format = av_x_if_null(av_get_pix_fmt_name(link->format), "?");
  36. av_bprintf(buf, "[%dx%d %d:%d %s]", link->w, link->h,
  37. link->sample_aspect_ratio.num,
  38. link->sample_aspect_ratio.den,
  39. format);
  40. break;
  41. case AVMEDIA_TYPE_AUDIO:
  42. av_get_channel_layout_string(layout, sizeof(layout),
  43. -1, link->channel_layout);
  44. format = av_x_if_null(av_get_sample_fmt_name(link->format), "?");
  45. av_bprintf(buf, "[%dHz %s:%s]",
  46. (int)link->sample_rate, format, layout);
  47. break;
  48. default:
  49. av_bprintf(buf, "?");
  50. break;
  51. }
  52. return buf->len;
  53. }
  54. static void avfilter_graph_dump_to_buf(AVBPrint *buf, AVFilterGraph *graph)
  55. {
  56. unsigned i, j, x, e;
  57. for (i = 0; i < graph->filter_count; i++) {
  58. AVFilterContext *filter = graph->filters[i];
  59. unsigned max_src_name = 0, max_dst_name = 0;
  60. unsigned max_in_name = 0, max_out_name = 0;
  61. unsigned max_in_fmt = 0, max_out_fmt = 0;
  62. unsigned width, height, in_indent;
  63. unsigned lname = strlen(filter->name);
  64. unsigned ltype = strlen(filter->filter->name);
  65. for (j = 0; j < filter->input_count; j++) {
  66. AVFilterLink *l = filter->inputs[j];
  67. unsigned ln = strlen(l->src->name) + 1 + strlen(l->srcpad->name);
  68. max_src_name = FFMAX(max_src_name, ln);
  69. max_in_name = FFMAX(max_in_name, strlen(l->dstpad->name));
  70. max_in_fmt = FFMAX(max_in_fmt, print_link_prop(NULL, l));
  71. }
  72. for (j = 0; j < filter->output_count; j++) {
  73. AVFilterLink *l = filter->outputs[j];
  74. unsigned ln = strlen(l->dst->name) + 1 + strlen(l->dstpad->name);
  75. max_dst_name = FFMAX(max_dst_name, ln);
  76. max_out_name = FFMAX(max_out_name, strlen(l->srcpad->name));
  77. max_out_fmt = FFMAX(max_out_fmt, print_link_prop(NULL, l));
  78. }
  79. in_indent = max_src_name + max_in_name + max_in_fmt;
  80. in_indent += in_indent ? 4 : 0;
  81. width = FFMAX(lname + 2, ltype + 4);
  82. height = FFMAX3(2, filter->input_count, filter->output_count);
  83. av_bprint_chars(buf, ' ', in_indent);
  84. av_bprintf(buf, "+");
  85. av_bprint_chars(buf, '-', width);
  86. av_bprintf(buf, "+\n");
  87. for (j = 0; j < height; j++) {
  88. unsigned in_no = j - (height - filter->input_count ) / 2;
  89. unsigned out_no = j - (height - filter->output_count) / 2;
  90. /* Input link */
  91. if (in_no < filter->input_count) {
  92. AVFilterLink *l = filter->inputs[in_no];
  93. e = buf->len + max_src_name + 2;
  94. av_bprintf(buf, "%s:%s", l->src->name, l->srcpad->name);
  95. av_bprint_chars(buf, '-', e - buf->len);
  96. e = buf->len + max_in_fmt + 2 +
  97. max_in_name - strlen(l->dstpad->name);
  98. print_link_prop(buf, l);
  99. av_bprint_chars(buf, '-', e - buf->len);
  100. av_bprintf(buf, "%s", l->dstpad->name);
  101. } else {
  102. av_bprint_chars(buf, ' ', in_indent);
  103. }
  104. /* Filter */
  105. av_bprintf(buf, "|");
  106. if (j == (height - 2) / 2) {
  107. x = (width - lname) / 2;
  108. av_bprintf(buf, "%*s%-*s", x, "", width - x, filter->name);
  109. } else if (j == (height - 2) / 2 + 1) {
  110. x = (width - ltype - 2) / 2;
  111. av_bprintf(buf, "%*s(%s)%*s", x, "", filter->filter->name,
  112. width - ltype - 2 - x, "");
  113. } else {
  114. av_bprint_chars(buf, ' ', width);
  115. }
  116. av_bprintf(buf, "|");
  117. /* Output link */
  118. if (out_no < filter->output_count) {
  119. AVFilterLink *l = filter->outputs[out_no];
  120. unsigned ln = strlen(l->dst->name) + 1 +
  121. strlen(l->dstpad->name);
  122. e = buf->len + max_out_name + 2;
  123. av_bprintf(buf, "%s", l->srcpad->name);
  124. av_bprint_chars(buf, '-', e - buf->len);
  125. e = buf->len + max_out_fmt + 2 +
  126. max_dst_name - ln;
  127. print_link_prop(buf, l);
  128. av_bprint_chars(buf, '-', e - buf->len);
  129. av_bprintf(buf, "%s:%s", l->dst->name, l->dstpad->name);
  130. }
  131. av_bprintf(buf, "\n");
  132. }
  133. av_bprint_chars(buf, ' ', in_indent);
  134. av_bprintf(buf, "+");
  135. av_bprint_chars(buf, '-', width);
  136. av_bprintf(buf, "+\n");
  137. av_bprintf(buf, "\n");
  138. }
  139. }
  140. char *avfilter_graph_dump(AVFilterGraph *graph, const char *options)
  141. {
  142. AVBPrint buf;
  143. char *dump;
  144. av_bprint_init(&buf, 0, 0);
  145. avfilter_graph_dump_to_buf(&buf, graph);
  146. av_bprint_init(&buf, buf.len + 1, buf.len + 1);
  147. avfilter_graph_dump_to_buf(&buf, graph);
  148. av_bprint_finalize(&buf, &dump);
  149. return dump;
  150. }