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