Commit 6cf53927 authored by Nicolas George's avatar Nicolas George

graphdump: use av_bprintf API.

parent b75c67dc
...@@ -21,27 +21,22 @@ ...@@ -21,27 +21,22 @@
#include <string.h> #include <string.h>
#include "libavutil/bprint.h"
#include "libavutil/pixdesc.h" #include "libavutil/pixdesc.h"
#include "avfilter.h" #include "avfilter.h"
#include "avfiltergraph.h" #include "avfiltergraph.h"
#define BPRINTF(...) \ static int print_link_prop(AVBPrint *buf, AVFilterLink *link)
cur += snprintf(cur, buf_end - FFMIN(cur, buf_end), __VA_ARGS__)
#define BPAD(c, l) \
do { \
if (cur < buf_end) memset(cur, c, FFMIN(l, buf_end - cur)); cur += l; \
} while (0)
static int snprint_link_prop(char *buf, char *buf_end, AVFilterLink *link)
{ {
char *cur = buf, *format; char *format;
char layout[64]; char layout[64];
if (!buf)
buf = &(AVBPrint){ 0 }; /* dummy buffer */
switch (link->type) { switch (link->type) {
case AVMEDIA_TYPE_VIDEO: case AVMEDIA_TYPE_VIDEO:
format = av_x_if_null(av_get_pix_fmt_name(link->format), "?"); format = av_x_if_null(av_get_pix_fmt_name(link->format), "?");
BPRINTF("[%dx%d %d:%d %s]", link->w, link->h, av_bprintf(buf, "[%dx%d %d:%d %s]", link->w, link->h,
link->sample_aspect_ratio.num, link->sample_aspect_ratio.num,
link->sample_aspect_ratio.den, link->sample_aspect_ratio.den,
format); format);
...@@ -51,23 +46,21 @@ static int snprint_link_prop(char *buf, char *buf_end, AVFilterLink *link) ...@@ -51,23 +46,21 @@ static int snprint_link_prop(char *buf, char *buf_end, AVFilterLink *link)
av_get_channel_layout_string(layout, sizeof(layout), av_get_channel_layout_string(layout, sizeof(layout),
-1, link->channel_layout); -1, link->channel_layout);
format = av_x_if_null(av_get_sample_fmt_name(link->format), "?"); format = av_x_if_null(av_get_sample_fmt_name(link->format), "?");
BPRINTF("[%dHz %s:%s:%s]", av_bprintf(buf, "[%dHz %s:%s:%s]",
(int)link->sample_rate, format, layout, (int)link->sample_rate, format, layout,
link->planar ? "planar" : "packed"); link->planar ? "planar" : "packed");
break; break;
default: default:
BPRINTF("?"); av_bprintf(buf, "?");
break; break;
} }
return cur - buf; return buf->len;
} }
static size_t avfilter_graph_dump_to_buf(AVFilterGraph *graph, static void avfilter_graph_dump_to_buf(AVBPrint *buf, AVFilterGraph *graph)
char *buf, char *buf_end)
{ {
char *cur = buf, *e; unsigned i, j, x, e;
unsigned i, j, x;
for (i = 0; i < graph->filter_count; i++) { for (i = 0; i < graph->filter_count; i++) {
AVFilterContext *filter = graph->filters[i]; AVFilterContext *filter = graph->filters[i];
...@@ -83,23 +76,23 @@ static size_t avfilter_graph_dump_to_buf(AVFilterGraph *graph, ...@@ -83,23 +76,23 @@ static size_t avfilter_graph_dump_to_buf(AVFilterGraph *graph,
unsigned ln = strlen(l->src->name) + 1 + strlen(l->srcpad->name); unsigned ln = strlen(l->src->name) + 1 + strlen(l->srcpad->name);
max_src_name = FFMAX(max_src_name, ln); max_src_name = FFMAX(max_src_name, ln);
max_in_name = FFMAX(max_in_name, strlen(l->dstpad->name)); max_in_name = FFMAX(max_in_name, strlen(l->dstpad->name));
max_in_fmt = FFMAX(max_in_fmt, snprint_link_prop(NULL, NULL, l)); max_in_fmt = FFMAX(max_in_fmt, print_link_prop(NULL, l));
} }
for (j = 0; j < filter->output_count; j++) { for (j = 0; j < filter->output_count; j++) {
AVFilterLink *l = filter->outputs[j]; AVFilterLink *l = filter->outputs[j];
unsigned ln = strlen(l->dst->name) + 1 + strlen(l->dstpad->name); unsigned ln = strlen(l->dst->name) + 1 + strlen(l->dstpad->name);
max_dst_name = FFMAX(max_dst_name, ln); max_dst_name = FFMAX(max_dst_name, ln);
max_out_name = FFMAX(max_out_name, strlen(l->srcpad->name)); max_out_name = FFMAX(max_out_name, strlen(l->srcpad->name));
max_out_fmt = FFMAX(max_out_fmt, snprint_link_prop(NULL, NULL, l)); max_out_fmt = FFMAX(max_out_fmt, print_link_prop(NULL, l));
} }
in_indent = max_src_name + max_in_name + max_in_fmt; in_indent = max_src_name + max_in_name + max_in_fmt;
in_indent += in_indent ? 4 : 0; in_indent += in_indent ? 4 : 0;
width = FFMAX(lname + 2, ltype + 4); width = FFMAX(lname + 2, ltype + 4);
height = FFMAX3(2, filter->input_count, filter->output_count); height = FFMAX3(2, filter->input_count, filter->output_count);
BPAD(' ', in_indent); av_bprint_chars(buf, ' ', in_indent);
BPRINTF("+"); av_bprintf(buf, "+");
BPAD('-', width); av_bprint_chars(buf, '-', width);
BPRINTF("+\n"); av_bprintf(buf, "+\n");
for (j = 0; j < height; j++) { for (j = 0; j < height; j++) {
unsigned in_no = j - (height - filter->input_count ) / 2; unsigned in_no = j - (height - filter->input_count ) / 2;
unsigned out_no = j - (height - filter->output_count) / 2; unsigned out_no = j - (height - filter->output_count) / 2;
...@@ -107,65 +100,65 @@ static size_t avfilter_graph_dump_to_buf(AVFilterGraph *graph, ...@@ -107,65 +100,65 @@ static size_t avfilter_graph_dump_to_buf(AVFilterGraph *graph,
/* Input link */ /* Input link */
if (in_no < filter->input_count) { if (in_no < filter->input_count) {
AVFilterLink *l = filter->inputs[in_no]; AVFilterLink *l = filter->inputs[in_no];
e = cur + max_src_name + 2; e = buf->len + max_src_name + 2;
BPRINTF("%s:%s", l->src->name, l->srcpad->name); av_bprintf(buf, "%s:%s", l->src->name, l->srcpad->name);
BPAD('-', e - cur); av_bprint_chars(buf, '-', e - buf->len);
e = cur + max_in_fmt + 2 + e = buf->len + max_in_fmt + 2 +
max_in_name - strlen(l->dstpad->name); max_in_name - strlen(l->dstpad->name);
cur += snprint_link_prop(cur, buf_end, l); print_link_prop(buf, l);
BPAD('-', e - cur); av_bprint_chars(buf, '-', e - buf->len);
BPRINTF("%s", l->dstpad->name); av_bprintf(buf, "%s", l->dstpad->name);
} else { } else {
BPAD(' ', in_indent); av_bprint_chars(buf, ' ', in_indent);
} }
/* Filter */ /* Filter */
BPRINTF("|"); av_bprintf(buf, "|");
if (j == (height - 2) / 2) { if (j == (height - 2) / 2) {
x = (width - lname) / 2; x = (width - lname) / 2;
BPRINTF("%*s%-*s", x, "", width - x, filter->name); av_bprintf(buf, "%*s%-*s", x, "", width - x, filter->name);
} else if (j == (height - 2) / 2 + 1) { } else if (j == (height - 2) / 2 + 1) {
x = (width - ltype - 2) / 2; x = (width - ltype - 2) / 2;
BPRINTF("%*s(%s)%*s", x, "", filter->filter->name, av_bprintf(buf, "%*s(%s)%*s", x, "", filter->filter->name,
width - ltype - 2 - x, ""); width - ltype - 2 - x, "");
} else { } else {
BPAD(' ', width); av_bprint_chars(buf, ' ', width);
} }
BPRINTF("|"); av_bprintf(buf, "|");
/* Output link */ /* Output link */
if (out_no < filter->output_count) { if (out_no < filter->output_count) {
AVFilterLink *l = filter->outputs[out_no]; AVFilterLink *l = filter->outputs[out_no];
unsigned ln = strlen(l->dst->name) + 1 + unsigned ln = strlen(l->dst->name) + 1 +
strlen(l->dstpad->name); strlen(l->dstpad->name);
e = cur + max_out_name + 2; e = buf->len + max_out_name + 2;
BPRINTF("%s", l->srcpad->name); av_bprintf(buf, "%s", l->srcpad->name);
BPAD('-', e - cur); av_bprint_chars(buf, '-', e - buf->len);
e = cur + max_out_fmt + 2 + e = buf->len + max_out_fmt + 2 +
max_dst_name - ln; max_dst_name - ln;
cur += snprint_link_prop(cur, buf_end, l); print_link_prop(buf, l);
BPAD('-', e - cur); av_bprint_chars(buf, '-', e - buf->len);
BPRINTF("%s:%s", l->dst->name, l->dstpad->name); av_bprintf(buf, "%s:%s", l->dst->name, l->dstpad->name);
} }
BPRINTF("\n"); av_bprintf(buf, "\n");
} }
BPAD(' ', in_indent); av_bprint_chars(buf, ' ', in_indent);
BPRINTF("+"); av_bprintf(buf, "+");
BPAD('-', width); av_bprint_chars(buf, '-', width);
BPRINTF("+\n"); av_bprintf(buf, "+\n");
BPRINTF("\n"); av_bprintf(buf, "\n");
} }
if (cur < buf_end)
*(cur++) = 0;
return cur - buf;
} }
char *avfilter_graph_dump(AVFilterGraph *graph, const char *options) char *avfilter_graph_dump(AVFilterGraph *graph, const char *options)
{ {
size_t buf_size = avfilter_graph_dump_to_buf(graph, NULL, NULL); AVBPrint buf;
char *buf = av_malloc(buf_size); char *dump;
if (!buf)
return NULL; av_bprint_init(&buf, 0, 0);
avfilter_graph_dump_to_buf(graph, buf, buf + buf_size); avfilter_graph_dump_to_buf(&buf, graph);
return buf; av_bprint_init(&buf, buf.len + 1, buf.len + 1);
avfilter_graph_dump_to_buf(&buf, graph);
av_bprint_finalize(&buf, &dump);
return dump;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment