graphdump.c 6.1 KB

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