Commit abbe8ec0e5 for ffmpeg
commit abbe8ec0e51bd005050074d32718faf1ad7cd9af
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Fri Nov 27 08:53:53 2026 -0800
avutil/opt: preserve 64-bit decimal literals
Bare integer options such as end_pts=9007199254740993 lose
precision in the floating-point expression evaluator. Store
in-range decimal literals directly and keep the evaluator for
expressions, suffixes and out-of-range values.
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
diff --git a/libavutil/opt.c b/libavutil/opt.c
index 45c9105b90..43002f3d2b 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -425,6 +425,44 @@ static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **d
? opt->default_val.i64 \
: opt->default_val.dbl)
+/* The expression evaluator rounds to double, so store bare decimal 64-bit
+ * literals within [min, max] directly. Leave anything else to the evaluator. */
+static int set_decimal_integer(const AVOption *o, enum AVOptionType type,
+ const char *val, void *dst)
+{
+ uint64_t u = 0;
+ double min, max;
+ int neg;
+
+ while (av_isspace(*val))
+ val++;
+ neg = *val == '-';
+ if (*val == '+' || neg)
+ val++;
+ if (!av_isdigit(*val) || (neg && type == AV_OPT_TYPE_UINT64))
+ return 0;
+ for (; av_isdigit(*val); val++) {
+ if (u > (UINT64_MAX - (*val - '0')) / 10)
+ return 0;
+ u = 10 * u + *val - '0';
+ }
+ while (av_isspace(*val))
+ val++;
+ if (*val || (type == AV_OPT_TYPE_INT64 && u > (uint64_t)INT64_MAX + neg))
+ return 0;
+
+ /* Compare the magnitude with the bounds without rounding it */
+ min = neg ? -o->max : o->min;
+ max = neg ? -o->min : o->max;
+ if ((min > 0 && (min >= 0x1p64 || u < (uint64_t)ceil(min))) ||
+ max < 0 || (max < 0x1p64 && u > (uint64_t)max))
+ return 0;
+
+ if (dst)
+ *(uint64_t *)dst = neg ? -u : u;
+ return 1;
+}
+
static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
{
void *logctx = dst ? obj : NULL;
@@ -469,6 +507,9 @@ static int set_string_number(void *obj, void *target_obj, const AVOption *o, con
av_log(logctx, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n",
o_named->name, o_named->help);
} else {
+ if ((type == AV_OPT_TYPE_INT64 || type == AV_OPT_TYPE_UINT64) &&
+ set_decimal_integer(o, type, val, dst))
+ return 0;
if (o->unit) {
for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) {
if (o_named->type == AV_OPT_TYPE_CONST &&
diff --git a/libavutil/tests/opt.c b/libavutil/tests/opt.c
index 55ec7100c1..0f335fef16 100644
--- a/libavutil/tests/opt.c
+++ b/libavutil/tests/opt.c
@@ -51,6 +51,7 @@ typedef struct TestContext {
void *binary2;
int binary_size2;
int64_t num64;
+ uint64_t unum64;
float flt;
double dbl;
char *escape;
@@ -183,6 +184,34 @@ int main(void)
av_log_set_level(AV_LOG_DEBUG);
av_log_set_callback(log_callback_help);
+ {
+ AVOption options[] = {
+ { "value", NULL, offsetof(TestContext, num64), AV_OPT_TYPE_INT64,
+ { .i64 = 0 }, INT64_MIN, (double)INT64_MAX },
+ { NULL }
+ };
+ AVClass class = test_class;
+ TestContext test_ctx = { .class = &class };
+ int ret;
+
+ class.option = options;
+ ret = av_opt_set(&test_ctx, "value", "9007199254740993", 0);
+ printf("int64 literal: %d %"PRId64"\n", ret, test_ctx.num64);
+ ret = av_opt_set(&test_ctx, "value", "-9223372036854775807", 0);
+ printf("negative int64 literal: %d %"PRId64"\n", ret, test_ctx.num64);
+ options[0].max = 9007199254740992.0;
+ ret = av_opt_set(&test_ctx, "value", "9007199254740993", 0);
+ printf("int64 rounded endpoint: %d %"PRId64"\n", ret, test_ctx.num64);
+ options[0].offset = offsetof(TestContext, unum64);
+ options[0].type = AV_OPT_TYPE_UINT64;
+ options[0].min = 0;
+ options[0].max = (double)UINT64_MAX;
+ ret = av_opt_set(&test_ctx, "value", "18446744073709551613", 0);
+ printf("uint64 literal: %d %"PRIu64"\n", ret, test_ctx.unum64);
+ ret = av_opt_set(&test_ctx, "value", "18446744073709551616", 0);
+ printf("uint64 literal overflow: %d %"PRIu64"\n", ret, test_ctx.unum64);
+ }
+
printf("Testing default values\n");
{
TestContext test_ctx = { 0 };
diff --git a/tests/ref/fate/opt b/tests/ref/fate/opt
index e2be9f9663..e5204d8390 100644
--- a/tests/ref/fate/opt
+++ b/tests/ref/fate/opt
@@ -1,3 +1,8 @@
+int64 literal: 0 9007199254740993
+negative int64 literal: 0 -9223372036854775807
+int64 rounded endpoint: 0 9007199254740992
+uint64 literal: 0 18446744073709551613
+uint64 literal overflow: 0 18446744073709551615
Testing default values
num=0
unum=2147483648