FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
graphdump.c
Go to the documentation of this file.
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 
25 #include "libavutil/bprint.h"
26 #include "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "avfiltergraph.h"
29 #include "internal.h"
30 
31 static int print_link_prop(AVBPrint *buf, AVFilterLink *link)
32 {
33  char *format;
34  char layout[64];
35  AVBPrint dummy_buffer = { 0 };
36 
37  if (!buf)
38  buf = &dummy_buffer;
39  switch (link->type) {
40  case AVMEDIA_TYPE_VIDEO:
41  format = av_x_if_null(av_get_pix_fmt_name(link->format), "?");
42  av_bprintf(buf, "[%dx%d %d:%d %s]", link->w, link->h,
45  format);
46  break;
47 
48  case AVMEDIA_TYPE_AUDIO:
49  av_get_channel_layout_string(layout, sizeof(layout),
50  link->channels, link->channel_layout);
51  format = av_x_if_null(av_get_sample_fmt_name(link->format), "?");
52  av_bprintf(buf, "[%dHz %s:%s]",
53  (int)link->sample_rate, format, layout);
54  break;
55 
56  default:
57  av_bprintf(buf, "?");
58  break;
59  }
60  return buf->len;
61 }
62 
63 static void avfilter_graph_dump_to_buf(AVBPrint *buf, AVFilterGraph *graph)
64 {
65  unsigned i, j, x, e;
66 
67  for (i = 0; i < graph->nb_filters; i++) {
68  AVFilterContext *filter = graph->filters[i];
69  unsigned max_src_name = 0, max_dst_name = 0;
70  unsigned max_in_name = 0, max_out_name = 0;
71  unsigned max_in_fmt = 0, max_out_fmt = 0;
72  unsigned width, height, in_indent;
73  unsigned lname = strlen(filter->name);
74  unsigned ltype = strlen(filter->filter->name);
75 
76  for (j = 0; j < filter->nb_inputs; j++) {
77  AVFilterLink *l = filter->inputs[j];
78  unsigned ln = strlen(l->src->name) + 1 + strlen(l->srcpad->name);
79  max_src_name = FFMAX(max_src_name, ln);
80  max_in_name = FFMAX(max_in_name, strlen(l->dstpad->name));
81  max_in_fmt = FFMAX(max_in_fmt, print_link_prop(NULL, l));
82  }
83  for (j = 0; j < filter->nb_outputs; j++) {
84  AVFilterLink *l = filter->outputs[j];
85  unsigned ln = strlen(l->dst->name) + 1 + strlen(l->dstpad->name);
86  max_dst_name = FFMAX(max_dst_name, ln);
87  max_out_name = FFMAX(max_out_name, strlen(l->srcpad->name));
88  max_out_fmt = FFMAX(max_out_fmt, print_link_prop(NULL, l));
89  }
90  in_indent = max_src_name + max_in_name + max_in_fmt;
91  in_indent += in_indent ? 4 : 0;
92  width = FFMAX(lname + 2, ltype + 4);
93  height = FFMAX3(2, filter->nb_inputs, filter->nb_outputs);
94  av_bprint_chars(buf, ' ', in_indent);
95  av_bprintf(buf, "+");
96  av_bprint_chars(buf, '-', width);
97  av_bprintf(buf, "+\n");
98  for (j = 0; j < height; j++) {
99  unsigned in_no = j - (height - filter->nb_inputs ) / 2;
100  unsigned out_no = j - (height - filter->nb_outputs) / 2;
101 
102  /* Input link */
103  if (in_no < filter->nb_inputs) {
104  AVFilterLink *l = filter->inputs[in_no];
105  e = buf->len + max_src_name + 2;
106  av_bprintf(buf, "%s:%s", l->src->name, l->srcpad->name);
107  av_bprint_chars(buf, '-', e - buf->len);
108  e = buf->len + max_in_fmt + 2 +
109  max_in_name - strlen(l->dstpad->name);
110  print_link_prop(buf, l);
111  av_bprint_chars(buf, '-', e - buf->len);
112  av_bprintf(buf, "%s", l->dstpad->name);
113  } else {
114  av_bprint_chars(buf, ' ', in_indent);
115  }
116 
117  /* Filter */
118  av_bprintf(buf, "|");
119  if (j == (height - 2) / 2) {
120  x = (width - lname) / 2;
121  av_bprintf(buf, "%*s%-*s", x, "", width - x, filter->name);
122  } else if (j == (height - 2) / 2 + 1) {
123  x = (width - ltype - 2) / 2;
124  av_bprintf(buf, "%*s(%s)%*s", x, "", filter->filter->name,
125  width - ltype - 2 - x, "");
126  } else {
127  av_bprint_chars(buf, ' ', width);
128  }
129  av_bprintf(buf, "|");
130 
131  /* Output link */
132  if (out_no < filter->nb_outputs) {
133  AVFilterLink *l = filter->outputs[out_no];
134  unsigned ln = strlen(l->dst->name) + 1 +
135  strlen(l->dstpad->name);
136  e = buf->len + max_out_name + 2;
137  av_bprintf(buf, "%s", l->srcpad->name);
138  av_bprint_chars(buf, '-', e - buf->len);
139  e = buf->len + max_out_fmt + 2 +
140  max_dst_name - ln;
141  print_link_prop(buf, l);
142  av_bprint_chars(buf, '-', e - buf->len);
143  av_bprintf(buf, "%s:%s", l->dst->name, l->dstpad->name);
144  }
145  av_bprintf(buf, "\n");
146  }
147  av_bprint_chars(buf, ' ', in_indent);
148  av_bprintf(buf, "+");
149  av_bprint_chars(buf, '-', width);
150  av_bprintf(buf, "+\n");
151  av_bprintf(buf, "\n");
152  }
153 }
154 
155 char *avfilter_graph_dump(AVFilterGraph *graph, const char *options)
156 {
157  AVBPrint buf;
158  char *dump;
159 
160  av_bprint_init(&buf, 0, 0);
161  avfilter_graph_dump_to_buf(&buf, graph);
162  av_bprint_init(&buf, buf.len + 1, buf.len + 1);
163  avfilter_graph_dump_to_buf(&buf, graph);
164  av_bprint_finalize(&buf, &dump);
165  return dump;
166 }
AVFilterContext ** filters
Definition: avfilter.h:1177
#define NULL
Definition: coverity.c:32
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
Main libavfilter public API header.
int num
numerator
Definition: rational.h:44
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
const char * name
Pad name.
Definition: internal.h:69
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:641
char * name
name of this filter instance
Definition: avfilter.h:638
static void avfilter_graph_dump_to_buf(AVBPrint *buf, AVFilterGraph *graph)
Definition: graphdump.c:63
const OptionDef options[]
Definition: ffserver.c:3807
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition: avutil.h:300
static int print_link_prop(AVBPrint *buf, AVFilterLink *link)
Definition: graphdump.c:31
unsigned nb_outputs
number of output pads
Definition: avfilter.h:652
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:47
#define FFMAX(a, b)
Definition: common.h:79
audio channel layout utility functions
unsigned nb_inputs
number of input pads
Definition: avfilter.h:645
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
void * buf
Definition: avisynth_c.h:553
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
const char * name
Filter name.
Definition: avfilter.h:474
unsigned nb_filters
Definition: avfilter.h:1179
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
char * avfilter_graph_dump(AVFilterGraph *graph, const char *options)
Dump a graph into a human-readable string representation.
Definition: graphdump.c:155
int den
denominator
Definition: rational.h:45
uint64_t layout
An instance of a filter.
Definition: avfilter.h:633
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2050
internal API functions
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:636
#define FFMAX3(a, b, c)
Definition: common.h:80
static int width
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140