]> git.sesse.net Git - ffmpeg/blob - libavfilter/graphdump.c
lavu: introduce av_parse_ratio() and use it in ffmpeg and lavfi/aspect
[ffmpeg] / libavfilter / graphdump.c
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/pixdesc.h"
25 #include "avfilter.h"
26 #include "avfiltergraph.h"
27
28 #define BPRINTF(...) \
29     cur += snprintf(cur, buf_end - FFMIN(cur, buf_end), __VA_ARGS__)
30
31 #define BPAD(c, l) \
32     do { \
33         if (cur < buf_end) memset(cur, c, FFMIN(l, buf_end - cur)); cur += l; \
34     } while (0)
35
36 static int snprint_link_prop(char *buf, char *buf_end, AVFilterLink *link)
37 {
38     char *cur = buf, *format;
39     char layout[64];
40
41     switch (link->type) {
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,
47                     format);
48             break;
49
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");
57             break;
58
59         default:
60             BPRINTF("?");
61             break;
62     }
63     return cur - buf;
64 }
65
66 static size_t avfilter_graph_dump_to_buf(AVFilterGraph *graph,
67                                          char *buf, char *buf_end)
68 {
69     char *cur = buf, *e;
70     unsigned i, j, x;
71
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);
80
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));
87         }
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));
94         }
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);
99         BPAD(' ', in_indent);
100         BPRINTF("+");
101         BPAD('-', width);
102         BPRINTF("+\n");
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;
106
107             /* Input link */
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);
112                 BPAD('-', e - cur);
113                 e = cur + max_in_fmt + 2 +
114                     max_in_name - strlen(l->dstpad->name);
115                 cur += snprint_link_prop(cur, buf_end, l);
116                 BPAD('-', e - cur);
117                 BPRINTF("%s", l->dstpad->name);
118             } else {
119                 BPAD(' ', in_indent);
120             }
121
122             /* Filter */
123             BPRINTF("|");
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, "");
131             } else {
132                 BPAD(' ', width);
133             }
134             BPRINTF("|");
135
136             /* Output link */
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);
143                 BPAD('-', e - cur);
144                 e = cur + max_out_fmt + 2 +
145                     max_dst_name - ln;
146                 cur += snprint_link_prop(cur, buf_end, l);
147                 BPAD('-', e - cur);
148                 BPRINTF("%s:%s", l->dst->name, l->dstpad->name);
149             }
150             BPRINTF("\n");
151         }
152         BPAD(' ', in_indent);
153         BPRINTF("+");
154         BPAD('-', width);
155         BPRINTF("+\n");
156         BPRINTF("\n");
157     }
158     if (cur < buf_end)
159         *(cur++) = 0;
160     return cur - buf;
161 }
162
163 char *avfilter_graph_dump(AVFilterGraph *graph, const char *options)
164 {
165     size_t buf_size = avfilter_graph_dump_to_buf(graph, NULL, NULL);
166     char *buf = av_malloc(buf_size);
167     if (!buf)
168         return NULL;
169     avfilter_graph_dump_to_buf(graph, buf, buf + buf_size);
170     return buf;
171 }