libavfilter/graphdump.c
0c3577bf
 /*
  * Filter graphs to bad ASCII-art
  * Copyright (c) 2012 Nicolas George
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include <string.h>
 
1acd2f6b
 #include "libavutil/channel_layout.h"
6cf53927
 #include "libavutil/bprint.h"
0c3577bf
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
e66a43f6
 #include "internal.h"
0c3577bf
 
6cf53927
 static int print_link_prop(AVBPrint *buf, AVFilterLink *link)
0c3577bf
 {
6cf53927
     char *format;
0c3577bf
     char layout[64];
18af0ce6
     AVBPrint dummy_buffer = { 0 };
0c3577bf
 
6cf53927
     if (!buf)
18af0ce6
         buf = &dummy_buffer;
0c3577bf
     switch (link->type) {
         case AVMEDIA_TYPE_VIDEO:
             format = av_x_if_null(av_get_pix_fmt_name(link->format), "?");
6cf53927
             av_bprintf(buf, "[%dx%d %d:%d %s]", link->w, link->h,
0c3577bf
                     link->sample_aspect_ratio.num,
                     link->sample_aspect_ratio.den,
                     format);
             break;
 
         case AVMEDIA_TYPE_AUDIO:
             av_get_channel_layout_string(layout, sizeof(layout),
7a71544f
                                          link->channels, link->channel_layout);
0c3577bf
             format = av_x_if_null(av_get_sample_fmt_name(link->format), "?");
4d4098da
             av_bprintf(buf, "[%dHz %s:%s]",
                        (int)link->sample_rate, format, layout);
0c3577bf
             break;
 
         default:
6cf53927
             av_bprintf(buf, "?");
0c3577bf
             break;
     }
6cf53927
     return buf->len;
0c3577bf
 }
 
6cf53927
 static void avfilter_graph_dump_to_buf(AVBPrint *buf, AVFilterGraph *graph)
0c3577bf
 {
6cf53927
     unsigned i, j, x, e;
0c3577bf
 
ecade984
     for (i = 0; i < graph->nb_filters; i++) {
0c3577bf
         AVFilterContext *filter = graph->filters[i];
         unsigned max_src_name = 0, max_dst_name = 0;
         unsigned max_in_name  = 0, max_out_name = 0;
         unsigned max_in_fmt   = 0, max_out_fmt  = 0;
         unsigned width, height, in_indent;
         unsigned lname = strlen(filter->name);
         unsigned ltype = strlen(filter->filter->name);
 
7659ee35
         for (j = 0; j < filter->nb_inputs; j++) {
0c3577bf
             AVFilterLink *l = filter->inputs[j];
             unsigned ln = strlen(l->src->name) + 1 + strlen(l->srcpad->name);
             max_src_name = FFMAX(max_src_name, ln);
             max_in_name = FFMAX(max_in_name, strlen(l->dstpad->name));
6cf53927
             max_in_fmt = FFMAX(max_in_fmt, print_link_prop(NULL, l));
0c3577bf
         }
7659ee35
         for (j = 0; j < filter->nb_outputs; j++) {
0c3577bf
             AVFilterLink *l = filter->outputs[j];
             unsigned ln = strlen(l->dst->name) + 1 + strlen(l->dstpad->name);
             max_dst_name = FFMAX(max_dst_name, ln);
             max_out_name = FFMAX(max_out_name, strlen(l->srcpad->name));
6cf53927
             max_out_fmt = FFMAX(max_out_fmt, print_link_prop(NULL, l));
0c3577bf
         }
         in_indent = max_src_name + max_in_name + max_in_fmt;
         in_indent += in_indent ? 4 : 0;
         width = FFMAX(lname + 2, ltype + 4);
7659ee35
         height = FFMAX3(2, filter->nb_inputs, filter->nb_outputs);
6cf53927
         av_bprint_chars(buf, ' ', in_indent);
         av_bprintf(buf, "+");
         av_bprint_chars(buf, '-', width);
         av_bprintf(buf, "+\n");
0c3577bf
         for (j = 0; j < height; j++) {
7659ee35
             unsigned in_no  = j - (height - filter->nb_inputs ) / 2;
             unsigned out_no = j - (height - filter->nb_outputs) / 2;
0c3577bf
 
             /* Input link */
7659ee35
             if (in_no < filter->nb_inputs) {
0c3577bf
                 AVFilterLink *l = filter->inputs[in_no];
6cf53927
                 e = buf->len + max_src_name + 2;
                 av_bprintf(buf, "%s:%s", l->src->name, l->srcpad->name);
                 av_bprint_chars(buf, '-', e - buf->len);
                 e = buf->len + max_in_fmt + 2 +
0c3577bf
                     max_in_name - strlen(l->dstpad->name);
6cf53927
                 print_link_prop(buf, l);
                 av_bprint_chars(buf, '-', e - buf->len);
                 av_bprintf(buf, "%s", l->dstpad->name);
0c3577bf
             } else {
6cf53927
                 av_bprint_chars(buf, ' ', in_indent);
0c3577bf
             }
 
             /* Filter */
6cf53927
             av_bprintf(buf, "|");
0c3577bf
             if (j == (height - 2) / 2) {
                 x = (width - lname) / 2;
6cf53927
                 av_bprintf(buf, "%*s%-*s", x, "", width - x, filter->name);
0c3577bf
             } else if (j == (height - 2) / 2 + 1) {
                 x = (width - ltype - 2) / 2;
6cf53927
                 av_bprintf(buf, "%*s(%s)%*s", x, "", filter->filter->name,
0c3577bf
                         width - ltype - 2 - x, "");
             } else {
6cf53927
                 av_bprint_chars(buf, ' ', width);
0c3577bf
             }
6cf53927
             av_bprintf(buf, "|");
0c3577bf
 
             /* Output link */
7659ee35
             if (out_no < filter->nb_outputs) {
0c3577bf
                 AVFilterLink *l = filter->outputs[out_no];
                 unsigned ln = strlen(l->dst->name) + 1 +
                               strlen(l->dstpad->name);
6cf53927
                 e = buf->len + max_out_name + 2;
                 av_bprintf(buf, "%s", l->srcpad->name);
                 av_bprint_chars(buf, '-', e - buf->len);
                 e = buf->len + max_out_fmt + 2 +
0c3577bf
                     max_dst_name - ln;
6cf53927
                 print_link_prop(buf, l);
                 av_bprint_chars(buf, '-', e - buf->len);
                 av_bprintf(buf, "%s:%s", l->dst->name, l->dstpad->name);
0c3577bf
             }
6cf53927
             av_bprintf(buf, "\n");
0c3577bf
         }
6cf53927
         av_bprint_chars(buf, ' ', in_indent);
         av_bprintf(buf, "+");
         av_bprint_chars(buf, '-', width);
         av_bprintf(buf, "+\n");
         av_bprintf(buf, "\n");
0c3577bf
     }
 }
 
 char *avfilter_graph_dump(AVFilterGraph *graph, const char *options)
 {
6cf53927
     AVBPrint buf;
     char *dump;
 
     av_bprint_init(&buf, 0, 0);
     avfilter_graph_dump_to_buf(&buf, graph);
     av_bprint_init(&buf, buf.len + 1, buf.len + 1);
     avfilter_graph_dump_to_buf(&buf, graph);
     av_bprint_finalize(&buf, &dump);
     return dump;
0c3577bf
 }