FFmpeg  4.4.9
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 "internal.h"
29 
30 static int print_link_prop(AVBPrint *buf, AVFilterLink *link)
31 {
32  char *format;
33  char layout[64];
34  AVBPrint dummy_buffer;
35 
36  if (!buf) {
37  buf = &dummy_buffer;
39  }
40  switch (link->type) {
41  case AVMEDIA_TYPE_VIDEO:
43  av_bprintf(buf, "[%dx%d %d:%d %s]", link->w, link->h,
46  format);
47  break;
48 
49  case AVMEDIA_TYPE_AUDIO:
51  link->channels, link->channel_layout);
53  av_bprintf(buf, "[%dHz %s:%s]",
54  (int)link->sample_rate, format, layout);
55  break;
56 
57  default:
58  av_bprintf(buf, "?");
59  break;
60  }
61  return buf->len;
62 }
63 
64 static void avfilter_graph_dump_to_buf(AVBPrint *buf, AVFilterGraph *graph)
65 {
66  unsigned i, j, x, e;
67 
68  for (i = 0; i < graph->nb_filters; i++) {
69  AVFilterContext *filter = graph->filters[i];
70  unsigned max_src_name = 0, max_dst_name = 0;
71  unsigned max_in_name = 0, max_out_name = 0;
72  unsigned max_in_fmt = 0, max_out_fmt = 0;
73  unsigned width, height, in_indent;
74  unsigned lname = strlen(filter->name);
75  unsigned ltype = strlen(filter->filter->name);
76 
77  for (j = 0; j < filter->nb_inputs; j++) {
78  AVFilterLink *l = filter->inputs[j];
79  unsigned ln = strlen(l->src->name) + 1 + strlen(l->srcpad->name);
80  max_src_name = FFMAX(max_src_name, ln);
81  max_in_name = FFMAX(max_in_name, strlen(l->dstpad->name));
82  max_in_fmt = FFMAX(max_in_fmt, print_link_prop(NULL, l));
83  }
84  for (j = 0; j < filter->nb_outputs; j++) {
85  AVFilterLink *l = filter->outputs[j];
86  unsigned ln = strlen(l->dst->name) + 1 + strlen(l->dstpad->name);
87  max_dst_name = FFMAX(max_dst_name, ln);
88  max_out_name = FFMAX(max_out_name, strlen(l->srcpad->name));
89  max_out_fmt = FFMAX(max_out_fmt, print_link_prop(NULL, l));
90  }
91  in_indent = max_src_name + max_in_name + max_in_fmt;
92  in_indent += in_indent ? 4 : 0;
93  width = FFMAX(lname + 2, ltype + 4);
94  height = FFMAX3(2, filter->nb_inputs, filter->nb_outputs);
95  av_bprint_chars(buf, ' ', in_indent);
96  av_bprintf(buf, "+");
97  av_bprint_chars(buf, '-', width);
98  av_bprintf(buf, "+\n");
99  for (j = 0; j < height; j++) {
100  unsigned in_no = j - (height - filter->nb_inputs ) / 2;
101  unsigned out_no = j - (height - filter->nb_outputs) / 2;
102 
103  /* Input link */
104  if (in_no < filter->nb_inputs) {
105  AVFilterLink *l = filter->inputs[in_no];
106  e = buf->len + max_src_name + 2;
107  av_bprintf(buf, "%s:%s", l->src->name, l->srcpad->name);
108  av_bprint_chars(buf, '-', e - buf->len);
109  e = buf->len + max_in_fmt + 2 +
110  max_in_name - strlen(l->dstpad->name);
111  print_link_prop(buf, l);
112  av_bprint_chars(buf, '-', e - buf->len);
113  av_bprintf(buf, "%s", l->dstpad->name);
114  } else {
115  av_bprint_chars(buf, ' ', in_indent);
116  }
117 
118  /* Filter */
119  av_bprintf(buf, "|");
120  if (j == (height - 2) / 2) {
121  x = (width - lname) / 2;
122  av_bprintf(buf, "%*s%-*s", x, "", width - x, filter->name);
123  } else if (j == (height - 2) / 2 + 1) {
124  x = (width - ltype - 2) / 2;
125  av_bprintf(buf, "%*s(%s)%*s", x, "", filter->filter->name,
126  width - ltype - 2 - x, "");
127  } else {
128  av_bprint_chars(buf, ' ', width);
129  }
130  av_bprintf(buf, "|");
131 
132  /* Output link */
133  if (out_no < filter->nb_outputs) {
134  AVFilterLink *l = filter->outputs[out_no];
135  unsigned ln = strlen(l->dst->name) + 1 +
136  strlen(l->dstpad->name);
137  e = buf->len + max_out_name + 2;
138  av_bprintf(buf, "%s", l->srcpad->name);
139  av_bprint_chars(buf, '-', e - buf->len);
140  e = buf->len + max_out_fmt + 2 +
141  max_dst_name - ln;
142  print_link_prop(buf, l);
143  av_bprint_chars(buf, '-', e - buf->len);
144  av_bprintf(buf, "%s:%s", l->dst->name, l->dstpad->name);
145  }
146  av_bprintf(buf, "\n");
147  }
148  av_bprint_chars(buf, ' ', in_indent);
149  av_bprintf(buf, "+");
150  av_bprint_chars(buf, '-', width);
151  av_bprintf(buf, "+\n");
152  av_bprintf(buf, "\n");
153  }
154 }
155 
156 char *avfilter_graph_dump(AVFilterGraph *graph, const char *options)
157 {
158  AVBPrint buf;
159  char *dump = NULL;
160 
162  avfilter_graph_dump_to_buf(&buf, graph);
163  dump = av_malloc(buf.len + 1);
164  if (!dump)
165  return NULL;
166  av_bprint_init_for_buffer(&buf, dump, buf.len + 1);
167  avfilter_graph_dump_to_buf(&buf, graph);
168  return dump;
169 }
static const char *const format[]
Definition: af_aiir.c:457
Main libavfilter public API header.
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140
void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
Init a print buffer using a pre-existing buffer.
Definition: bprint.c:85
#define AV_BPRINT_SIZE_COUNT_ONLY
static av_always_inline void filter(int16_t *output, ptrdiff_t out_stride, const int16_t *low, ptrdiff_t low_stride, const int16_t *high, ptrdiff_t high_stride, int len, int clip)
Definition: cfhddsp.c:27
uint64_t layout
audio channel layout utility functions
#define FFMAX3(a, b, c)
Definition: common.h:104
#define FFMAX(a, b)
Definition: common.h:103
#define NULL
Definition: coverity.c:32
const OptionDef options[]
static void avfilter_graph_dump_to_buf(AVBPrint *buf, AVFilterGraph *graph)
Definition: graphdump.c:64
static int print_link_prop(AVBPrint *buf, AVFilterLink *link)
Definition: graphdump.c:30
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.
char * avfilter_graph_dump(AVFilterGraph *graph, const char *options)
Dump a graph into a human-readable string representation.
Definition: graphdump.c:156
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition: avutil.h:308
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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:49
int i
Definition: input.c:407
common internal API header
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:2489
An instance of a filter.
Definition: avfilter.h:341
char * name
name of this filter instance
Definition: avfilter.h:346
unsigned nb_filters
Definition: avfilter.h:858
AVFilterContext ** filters
Definition: avfilter.h:857
const char * name
Pad name.
Definition: internal.h:60
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
#define av_malloc(s)
#define height
#define width