fftools/ffprobe: use an AVTextFormatOptions struct in AVTextFormatContext

Signed-off-by: Marton Balint <cus@passwd.hu>
This commit is contained in:
Marton Balint
2026-01-14 00:58:15 +01:00
parent b1f14cbd0e
commit ca6f3f3c17
4 changed files with 40 additions and 53 deletions

View File

@@ -271,7 +271,7 @@ static void print_link(GraphPrintContext *gpc, AVFilterLink *link)
}
if (link->w && link->h) {
if (tfc->show_value_unit) {
if (tfc->opts.show_value_unit) {
print_fmt("size", "%dx%d", link->w, link->h);
} else {
print_int("width", link->w);
@@ -292,7 +292,7 @@ static void print_link(GraphPrintContext *gpc, AVFilterLink *link)
////print_str("format", av_x_if_null(av_get_subtitle_fmt_name(link->format), "?"));
if (link->w && link->h) {
if (tfc->show_value_unit) {
if (tfc->opts.show_value_unit) {
print_fmt("size", "%dx%d", link->w, link->h);
} else {
print_int("width", link->w);
@@ -306,7 +306,7 @@ static void print_link(GraphPrintContext *gpc, AVFilterLink *link)
av_channel_layout_describe(&link->ch_layout, layout_string, sizeof(layout_string));
print_str("channel_layout", layout_string);
print_val("channels", link->ch_layout.nb_channels, "ch");
if (tfc->show_value_unit)
if (tfc->opts.show_value_unit)
print_fmt("sample_rate", "%d.1 kHz", link->sample_rate / 1000);
else
print_val("sample_rate", link->sample_rate, "Hz");
@@ -912,8 +912,8 @@ static int init_graphprint(GraphPrintContext **pgpc, AVBPrint *target_buf)
gpc->id_prefix_num = atomic_fetch_add(&prefix_num, 1);
gpc->is_diagram = !!(tfc->formatter->flags & AV_TEXTFORMAT_FLAG_IS_DIAGRAM_FORMATTER);
if (gpc->is_diagram) {
tfc->show_value_unit = 1;
tfc->show_optional_fields = -1;
tfc->opts.show_value_unit = 1;
tfc->opts.show_optional_fields = -1;
gpc->opt_flags = AV_TEXTFORMAT_PRINT_STRING_OPTIONAL;
gpc->skip_buffer_filters = 1;
////} else {

View File

@@ -147,13 +147,7 @@ int avtext_context_open(AVTextFormatContext **ptctx, const AVTextFormatter *form
goto fail;
}
tctx->is_key_selected = options.is_key_selected;
tctx->show_value_unit = options.show_value_unit;
tctx->use_value_prefix = options.use_value_prefix;
tctx->use_byte_value_binary_prefix = options.use_byte_value_binary_prefix;
tctx->use_value_sexagesimal_format = options.use_value_sexagesimal_format;
tctx->show_optional_fields = options.show_optional_fields;
tctx->data_dump_format = options.data_dump_format;
tctx->opts = options;
if (nb_sections > SECTION_MAX_NB_SECTIONS) {
av_log(tctx, AV_LOG_ERROR, "The number of section definitions (%d) is larger than the maximum allowed (%d)\n", nb_sections, SECTION_MAX_NB_SECTIONS);
@@ -293,17 +287,17 @@ void avtext_print_integer(AVTextFormatContext *tctx, const char *key, int64_t va
{
av_assert0(tctx);
if (tctx->show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER)
if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER)
return;
if (tctx->show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO
if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO
&& (flags & AV_TEXTFORMAT_PRINT_STRING_OPTIONAL)
&& !(tctx->formatter->flags & AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS))
return;
av_assert0(key && tctx->level >= 0 && tctx->level < SECTION_MAX_NB_LEVELS);
if (!tctx->is_key_selected || tctx->is_key_selected(tctx, key)) {
if (!tctx->opts.is_key_selected || tctx->opts.is_key_selected(tctx, key)) {
tctx->formatter->print_integer(tctx, key, val);
tctx->nb_item[tctx->level]++;
}
@@ -389,7 +383,7 @@ static char *value_string(const AVTextFormatContext *tctx, char *buf, int buf_si
vali = uv.val.i;
}
if (uv.unit == unit_second_str && tctx->use_value_sexagesimal_format) {
if (uv.unit == unit_second_str && tctx->opts.use_value_sexagesimal_format) {
double secs;
int hours, mins;
secs = vald;
@@ -401,10 +395,10 @@ static char *value_string(const AVTextFormatContext *tctx, char *buf, int buf_si
} else {
const char *prefix_string = "";
if (tctx->use_value_prefix && vald > 1) {
if (tctx->opts.use_value_prefix && vald > 1) {
int64_t index;
if (uv.unit == unit_byte_str && tctx->use_byte_value_binary_prefix) {
if (uv.unit == unit_byte_str && tctx->opts.use_byte_value_binary_prefix) {
index = (int64_t)(log2(vald) / 10);
index = av_clip64(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1);
vald /= si_prefixes[index].bin_val;
@@ -418,13 +412,13 @@ static char *value_string(const AVTextFormatContext *tctx, char *buf, int buf_si
vali = (int64_t)vald;
}
if (show_float || (tctx->use_value_prefix && vald != (int64_t)vald))
if (show_float || (tctx->opts.use_value_prefix && vald != (int64_t)vald))
snprintf(buf, buf_size, "%f", vald);
else
snprintf(buf, buf_size, "%"PRId64, vali);
av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || tctx->show_value_unit ? " " : "",
prefix_string, tctx->show_value_unit ? uv.unit : "");
av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || tctx->opts.show_value_unit ? " " : "",
prefix_string, tctx->opts.show_value_unit ? uv.unit : "");
}
return buf;
@@ -450,15 +444,15 @@ int avtext_print_string(AVTextFormatContext *tctx, const char *key, const char *
section = tctx->section[tctx->level];
if (tctx->show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER)
if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER)
return 0;
if (tctx->show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO
if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO
&& (flags & AV_TEXTFORMAT_PRINT_STRING_OPTIONAL)
&& !(tctx->formatter->flags & AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS))
return 0;
if (!tctx->is_key_selected || tctx->is_key_selected(tctx, key)) {
if (!tctx->opts.is_key_selected || tctx->opts.is_key_selected(tctx, key)) {
if (flags & AV_TEXTFORMAT_PRINT_STRING_VALIDATE) {
char *key1 = NULL, *val1 = NULL;
ret = validate_string(tctx, &key1, key);
@@ -543,7 +537,7 @@ void avtext_print_data(AVTextFormatContext *tctx, const char *key,
{
AVBPrint bp;
av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
switch (tctx->data_dump_format) {
switch (tctx->opts.data_dump_format) {
case AV_TEXTFORMAT_DATADUMP_XXD:
print_data_xxd(&bp, data, size);
break;

View File

@@ -111,6 +111,24 @@ typedef struct AVTextFormatter {
#define SECTION_MAX_NB_LEVELS 12
#define SECTION_MAX_NB_SECTIONS 100
typedef struct AVTextFormatOptions {
/**
* Callback to discard certain elements based upon the key used.
* It is called before any element with a key is printed.
* If this callback is unset, all elements are printed.
*
* @retval 1 if the element is supposed to be printed
* @retval 0 if the element is supposed to be discarded
*/
int (*is_key_selected)(struct AVTextFormatContext *tctx, const char *key);
int show_optional_fields;
int show_value_unit;
int use_value_prefix;
int use_byte_value_binary_prefix;
int use_value_sexagesimal_format;
AVTextFormatDataDump data_dump_format;
} AVTextFormatOptions;
struct AVTextFormatContext {
const AVClass *class; ///< class of the formatter
const AVTextFormatter *formatter; ///< the AVTextFormatter of which this is an instance
@@ -133,22 +151,7 @@ struct AVTextFormatContext {
AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
/// used by various formatters
/**
* Callback to discard certain elements based upon the key used.
* It is called before any element with a key is printed.
* If this callback is unset, all elements are printed.
*
* @retval 1 if the element is supposed to be printed
* @retval 0 if the element is supposed to be discarded
*/
int (*is_key_selected)(struct AVTextFormatContext *tctx, const char *key);
int show_optional_fields;
int show_value_unit;
int use_value_prefix;
int use_byte_value_binary_prefix;
int use_value_sexagesimal_format;
AVTextFormatDataDump data_dump_format;
AVTextFormatOptions opts;
struct AVHashContext *hash;
@@ -157,16 +160,6 @@ struct AVTextFormatContext {
unsigned int string_validation_utf8_flags;
};
typedef struct AVTextFormatOptions {
int (*is_key_selected)(struct AVTextFormatContext *tctx, const char *key);
int show_optional_fields;
int show_value_unit;
int use_value_prefix;
int use_byte_value_binary_prefix;
int use_value_sexagesimal_format;
AVTextFormatDataDump data_dump_format;
} AVTextFormatOptions;
#define AV_TEXTFORMAT_PRINT_STRING_OPTIONAL 1
#define AV_TEXTFORMAT_PRINT_STRING_VALIDATE 2

View File

@@ -64,8 +64,8 @@ static av_cold int xml_init(AVTextFormatContext *wctx)
return AVERROR(EINVAL); \
}
////CHECK_COMPLIANCE(show_private_data, "private");
CHECK_COMPLIANCE(wctx->show_value_unit, "unit");
CHECK_COMPLIANCE(wctx->use_value_prefix, "prefix");
CHECK_COMPLIANCE(wctx->opts.show_value_unit, "unit");
CHECK_COMPLIANCE(wctx->opts.use_value_prefix, "prefix");
}
return 0;