2 * Filter graphs to bad ASCII-art
3 * Copyright (c) 2012 Nicolas George
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/pixdesc.h"
26 #include "avfiltergraph.h"
28 #define BPRINTF(...) \
29 cur += snprintf(cur, buf_end - FFMIN(cur, buf_end), __VA_ARGS__)
33 if (cur < buf_end) memset(cur, c, FFMIN(l, buf_end - cur)); cur += l; \
36 static int snprint_link_prop(char *buf, char *buf_end, AVFilterLink *link)
38 char *cur = buf, *format;
42 case AVMEDIA_TYPE_VIDEO:
43 format = av_x_if_null(av_get_pix_fmt_name(link->format), "?");
44 BPRINTF("[%dx%d %d:%d %s]", link->w, link->h,
45 link->sample_aspect_ratio.num,
46 link->sample_aspect_ratio.den,
50 case AVMEDIA_TYPE_AUDIO:
51 av_get_channel_layout_string(layout, sizeof(layout),
52 -1, link->channel_layout);
53 format = av_x_if_null(av_get_sample_fmt_name(link->format), "?");
54 BPRINTF("[%dHz %s:%s:%s]",
55 (int)link->sample_rate, format, layout,
56 link->planar ? "planar" : "packed");
66 static size_t avfilter_graph_dump_to_buf(AVFilterGraph *graph,
67 char *buf, char *buf_end)
72 for (i = 0; i < graph->filter_count; i++) {
73 AVFilterContext *filter = graph->filters[i];
74 unsigned max_src_name = 0, max_dst_name = 0;
75 unsigned max_in_name = 0, max_out_name = 0;
76 unsigned max_in_fmt = 0, max_out_fmt = 0;
77 unsigned width, height, in_indent;
78 unsigned lname = strlen(filter->name);
79 unsigned ltype = strlen(filter->filter->name);
81 for (j = 0; j < filter->input_count; j++) {
82 AVFilterLink *l = filter->inputs[j];
83 unsigned ln = strlen(l->src->name) + 1 + strlen(l->srcpad->name);
84 max_src_name = FFMAX(max_src_name, ln);
85 max_in_name = FFMAX(max_in_name, strlen(l->dstpad->name));
86 max_in_fmt = FFMAX(max_in_fmt, snprint_link_prop(NULL, NULL, l));
88 for (j = 0; j < filter->output_count; j++) {
89 AVFilterLink *l = filter->outputs[j];
90 unsigned ln = strlen(l->dst->name) + 1 + strlen(l->dstpad->name);
91 max_dst_name = FFMAX(max_dst_name, ln);
92 max_out_name = FFMAX(max_out_name, strlen(l->srcpad->name));
93 max_out_fmt = FFMAX(max_out_fmt, snprint_link_prop(NULL, NULL, l));
95 in_indent = max_src_name + max_in_name + max_in_fmt;
96 in_indent += in_indent ? 4 : 0;
97 width = FFMAX(lname + 2, ltype + 4);
98 height = FFMAX3(2, filter->input_count, filter->output_count);
103 for (j = 0; j < height; j++) {
104 unsigned in_no = j - (height - filter->input_count ) / 2;
105 unsigned out_no = j - (height - filter->output_count) / 2;
108 if (in_no < filter->input_count) {
109 AVFilterLink *l = filter->inputs[in_no];
110 e = cur + max_src_name + 2;
111 BPRINTF("%s:%s", l->src->name, l->srcpad->name);
113 e = cur + max_in_fmt + 2 +
114 max_in_name - strlen(l->dstpad->name);
115 cur += snprint_link_prop(cur, buf_end, l);
117 BPRINTF("%s", l->dstpad->name);
119 BPAD(' ', in_indent);
124 if (j == (height - 2) / 2) {
125 x = (width - lname) / 2;
126 BPRINTF("%*s%-*s", x, "", width - x, filter->name);
127 } else if (j == (height - 2) / 2 + 1) {
128 x = (width - ltype - 2) / 2;
129 BPRINTF("%*s(%s)%*s", x, "", filter->filter->name,
130 width - ltype - 2 - x, "");
137 if (out_no < filter->output_count) {
138 AVFilterLink *l = filter->outputs[out_no];
139 unsigned ln = strlen(l->dst->name) + 1 +
140 strlen(l->dstpad->name);
141 e = cur + max_out_name + 2;
142 BPRINTF("%s", l->srcpad->name);
144 e = cur + max_out_fmt + 2 +
146 cur += snprint_link_prop(cur, buf_end, l);
148 BPRINTF("%s:%s", l->dst->name, l->dstpad->name);
152 BPAD(' ', in_indent);
163 char *avfilter_graph_dump(AVFilterGraph *graph, const char *options)
165 size_t buf_size = avfilter_graph_dump_to_buf(graph, NULL, NULL);
166 char *buf = av_malloc(buf_size);
169 avfilter_graph_dump_to_buf(graph, buf, buf + buf_size);