fftools/textformat: stop using char * as identifiers

Add an enum instead.
As a result, fix honor -byte_binary_prefix.
In value_string(), add support for float values (beyond seconds),
and allow unit to be null or empty.
Also remove unused variable unit_second_str in ffprobe.

Signed-off-by: Nicolas Gaullier <nicolas.gaullier@cji.paris>
This commit is contained in:
Nicolas Gaullier
2026-05-04 18:32:10 +02:00
committed by Jun Zhao
parent 8cd7465565
commit 0099a5fbdd
4 changed files with 26 additions and 18 deletions

View File

@@ -344,7 +344,6 @@ static const char *print_input_filename;
static const AVInputFormat *iformat = NULL;
static const char *output_filename = NULL;
static const char unit_second_str[] = "s" ;
static const char unit_hertz_str[] = "Hz" ;
static const char unit_byte_str[] = "byte" ;
static const char unit_bit_per_second_str[] = "bit/s";
@@ -455,7 +454,8 @@ static void log_callback(void *ptr, int level, const char *fmt, va_list vl)
#define print_ts(k, v) avtext_print_ts(tfc, k, v, 0)
#define print_duration_time(k, v, tb) avtext_print_time(tfc, k, v, tb, 1)
#define print_duration_ts(k, v) avtext_print_ts(tfc, k, v, 1)
#define print_val(k, v, u) avtext_print_unit_integer(tfc, k, v, u)
#define print_val(k, v, u) avtext_print_unit_integer(tfc, k, v, AV_TEXTFORMAT_VALUE_FMT_INT, u)
#define print_int_fmt(k, v, f, u) avtext_print_unit_integer(tfc, k, v, f, u)
static void print_integers(AVTextFormatContext *tfc, const char *key,
const void *data, int size, const char *format,
@@ -1342,7 +1342,7 @@ static void show_packet(AVTextFormatContext *tfc, InputFile *ifile, AVPacket *pk
print_time("dts_time", pkt->dts, &st->time_base);
print_duration_ts("duration", pkt->duration);
print_duration_time("duration_time", pkt->duration, &st->time_base);
print_val("size", pkt->size, unit_byte_str);
print_int_fmt("size", pkt->size, AV_TEXTFORMAT_VALUE_FMT_BYTE, unit_byte_str);
if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
else print_str_opt("pos", "N/A");
print_fmt("flags", "%c%c%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_',
@@ -1505,7 +1505,7 @@ static void show_frame(AVTextFormatContext *tfc, AVFrame *frame, AVStream *strea
print_duration_time("duration_time", frame->duration, &stream->time_base);
if (fd && fd->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, fd->pkt_pos);
else print_str_opt("pkt_pos", "N/A");
if (fd && fd->pkt_size != -1) print_val ("pkt_size", fd->pkt_size, unit_byte_str);
if (fd && fd->pkt_size != -1) print_int_fmt("pkt_size", fd->pkt_size, AV_TEXTFORMAT_VALUE_FMT_BYTE, unit_byte_str);
else print_str_opt("pkt_size", "N/A");
switch (stream->codecpar->codec_type) {
@@ -2448,7 +2448,7 @@ static int show_format(AVTextFormatContext *tfc, InputFile *ifile)
}
print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
if (size >= 0) print_val ("size", size, unit_byte_str);
if (size >= 0) print_int_fmt("size", size, AV_TEXTFORMAT_VALUE_FMT_BYTE, unit_byte_str);
else print_str_opt("size", "N/A");
if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
else print_str_opt("bit_rate", "N/A");

View File

@@ -141,7 +141,7 @@ typedef struct GraphPrintContext {
#define print_q(k, v, s) avtext_print_rational(tfc, k, v, s)
#define print_str(k, v) avtext_print_string(tfc, k, v, 0)
#define print_str_opt(k, v) avtext_print_string(tfc, k, v, gpc->opt_flags)
#define print_val(k, v, u) avtext_print_unit_integer(tfc, k, v, u)
#define print_val(k, v, u) avtext_print_unit_integer(tfc, k, v, AV_TEXTFORMAT_VALUE_FMT_INT, u)
#define print_fmt(k, f, ...) do { \
av_bprint_clear(&gpc->pbuf); \

View File

@@ -238,12 +238,7 @@ fail:
return ret;
}
/* Temporary definitions during refactoring */
static const char unit_second_str[] = "s";
static const char unit_hertz_str[] = "Hz";
static const char unit_byte_str[] = "byte";
static const char unit_bit_per_second_str[] = "bit/s";
void avtext_print_section_header(AVTextFormatContext *tctx, const void *data, int section_id)
{
@@ -367,6 +362,7 @@ struct unit_value {
int64_t i;
} val;
AVTextFormatValueFormat fmt;
const char *unit;
};
@@ -376,7 +372,7 @@ static char *value_string(const AVTextFormatContext *tctx, char *buf, int buf_si
int64_t vali = 0;
int show_float = 0;
if (uv.unit == unit_second_str) {
if (uv.fmt >= AV_TEXTFORMAT_VALUE_FMT_DOUBLE) {
vald = uv.val.d;
show_float = 1;
} else {
@@ -384,7 +380,7 @@ static char *value_string(const AVTextFormatContext *tctx, char *buf, int buf_si
vali = uv.val.i;
}
if (uv.unit == unit_second_str && tctx->opts.use_value_sexagesimal_format) {
if (uv.fmt == AV_TEXTFORMAT_VALUE_FMT_SECOND && tctx->opts.use_value_sexagesimal_format) {
double secs;
int hours, mins;
secs = vald;
@@ -399,7 +395,7 @@ static char *value_string(const AVTextFormatContext *tctx, char *buf, int buf_si
if (tctx->opts.use_value_prefix && vald > 1) {
int64_t index;
if (uv.unit == unit_byte_str && tctx->opts.use_byte_value_binary_prefix) {
if (uv.fmt == AV_TEXTFORMAT_VALUE_FMT_BYTE && 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,19 +414,23 @@ static char *value_string(const AVTextFormatContext *tctx, char *buf, int buf_si
else
snprintf(buf, buf_size, "%"PRId64, vali);
av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || tctx->opts.show_value_unit ? " " : "",
prefix_string, tctx->opts.show_value_unit ? uv.unit : "");
av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || tctx->opts.show_value_unit && uv.unit && *uv.unit ? " " : "",
prefix_string, tctx->opts.show_value_unit && uv.unit ? uv.unit : "");
}
return buf;
}
void avtext_print_unit_integer(AVTextFormatContext *tctx, const char *key, int64_t val, const char *unit)
void avtext_print_unit_integer(AVTextFormatContext *tctx, const char *key, int64_t val, AVTextFormatValueFormat fmt, const char *unit)
{
char val_str[128];
struct unit_value uv;
av_assert0(fmt < AV_TEXTFORMAT_VALUE_FMT_DOUBLE);
uv.val.i = val;
uv.fmt = fmt;
uv.unit = unit;
avtext_print_string(tctx, key, value_string(tctx, val_str, sizeof(val_str), uv), 0);
}
@@ -495,6 +495,7 @@ void avtext_print_time(AVTextFormatContext *tctx, const char *key,
double d = av_q2d(*time_base) * ts;
struct unit_value uv;
uv.val.d = d;
uv.fmt = AV_TEXTFORMAT_VALUE_FMT_SECOND;
uv.unit = unit_second_str;
value_string(tctx, buf, sizeof(buf), uv);
avtext_print_string(tctx, key, buf, 0);

View File

@@ -161,6 +161,13 @@ struct AVTextFormatContext {
unsigned int string_validation_utf8_flags;
};
typedef enum {
AV_TEXTFORMAT_VALUE_FMT_INT,
AV_TEXTFORMAT_VALUE_FMT_BYTE,
AV_TEXTFORMAT_VALUE_FMT_DOUBLE = 0x100,
AV_TEXTFORMAT_VALUE_FMT_SECOND,
} AVTextFormatValueFormat;
#define AV_TEXTFORMAT_PRINT_STRING_OPTIONAL 1
#define AV_TEXTFORMAT_PRINT_STRING_VALIDATE 2
@@ -178,7 +185,7 @@ void avtext_print_integer(AVTextFormatContext *tctx, const char *key, int64_t va
int avtext_print_string(AVTextFormatContext *tctx, const char *key, const char *val, int flags);
void avtext_print_unit_integer(AVTextFormatContext *tctx, const char *key, int64_t val, const char *unit);
void avtext_print_unit_integer(AVTextFormatContext *tctx, const char *key, int64_t val, AVTextFormatValueFormat fmt, const char *unit);
void avtext_print_rational(AVTextFormatContext *tctx, const char *key, AVRational q, char sep);