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