PR #26101 opened by Forgejo_Fairy
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26101
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26101.patch

Binary subtraction currently passes its operator into the unary-sign parser. 
For dB literals this changes the exponent instead of subtracting the converted 
amplitude: `20dB - 20dB` evaluates to approximately `10.1`, and `0dB - 0dB` 
evaluates to `2`.

Consume binary `+` and `-` before parsing the right operand and introduce an 
explicit subtraction node. Both expressions then evaluate to zero, while unary 
negative dB literals retain their existing meaning. Explicit subtraction also 
avoids depending on the right operand's sign multiplier, which some evaluator 
functions do not apply.

Add 29 FATE expression cases covering dB literals, signed operands, precedence, 
chained subtraction, and function operands. Expected results are stored in 
`tests/ref/fate/eval`.

Validation: all 73 available `fate-libavutil` tests passed on Linux AArch64; 
`fate-eval` passed on Linux x86-64; the reported `aevalsrc` commands were 
checked before and after the patch; `git diff --check` passed.

The introducing commit 5312268b34dc and its parent were built and tested. All 
eight tested release branches from 4.4 through 9.1 also reproduce the defect.

Fixes #26064.


>From 34599b9005f131db9f80d7e47d14f75951f1dc0c Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Mon, 16 Nov 2026 12:00:29 +0000
Subject: [PATCH] avutil/eval: distinguish binary subtraction from unary signs

Consume binary '+' and '-' in parse_subexpr() before parsing the right
operand. Otherwise parse_dB() interprets the subtraction operator as part
of a negative dB literal, making 20dB - 20dB evaluate to 10.1.

Represent subtraction with an explicit e_sub node instead of changing
the sign of the right operand. This also works for evaluator nodes that
do not apply their value multiplier, such as random() and while(). Unary
negative dB literals retain their existing meaning.

Add FATE coverage for dB subtraction, unary signs, precedence, chained
subtraction and function operands, with expected output in the reference
file.

Fixes: 5312268b34dc948e87934ae665a79a57c2fe2648
Fixes: #26064

Assisted-by: Fairy
---
 libavutil/eval.c       |  6 ++-
 libavutil/tests/eval.c | 29 ++++++++++++++
 tests/ref/fate/eval    | 87 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 120 insertions(+), 2 deletions(-)

diff --git a/libavutil/eval.c b/libavutil/eval.c
index 9b5ad218495..e167ec3bc4f 100644
--- a/libavutil/eval.c
+++ b/libavutil/eval.c
@@ -162,7 +162,7 @@ enum {
     e_value, e_const, e_func0, e_func1, e_func2,
     e_squish, e_gauss, e_ld, e_isnan, e_isinf,
     e_mod, e_max, e_min, e_eq, e_gt, e_gte, e_lte, e_lt,
-    e_pow, e_mul, e_div, e_add,
+    e_pow, e_mul, e_div, e_add, e_sub,
     e_last, e_st, e_while, e_taylor, e_root, e_floor, e_ceil, e_trunc, e_round,
     e_sqrt, e_not, e_random, e_hypot, e_gcd,
     e_if, e_ifnot, e_print, e_bitand, e_bitor, e_between, e_clip, e_atan2, 
e_lerp,
@@ -347,6 +347,7 @@ static double eval_expr(Parser *p, AVExpr *e)
                 case e_mul: return e->value * (d * d2);
                 case e_div: return e->value * (d2 ? (d / d2) : d * INFINITY);
                 case e_add: return e->value * (d + d2);
+                case e_sub: return e->value * (d - d2);
                 case e_last:return e->value * d2;
                 case e_st :  {
                     int index = av_clip(d, 0, VARS-1);
@@ -644,12 +645,13 @@ static int parse_subexpr(AVExpr **e, Parser *p)
     if ((ret = parse_term(&e0, p)) < 0)
         return ret;
     while (*p->s == '+' || *p->s == '-') {
+        int c = *p->s++;
         e1 = e0;
         if ((ret = parse_term(&e2, p)) < 0) {
             av_expr_free(e1);
             return ret;
         }
-        e0 = make_eval_expr(e_add, 1, e1, e2);
+        e0 = make_eval_expr(c == '+' ? e_add : e_sub, 1, e1, e2);
         if (!e0) {
             av_expr_free(e1);
             av_expr_free(e2);
diff --git a/libavutil/tests/eval.c b/libavutil/tests/eval.c
index c81aeb9ee70..0af0af69717 100644
--- a/libavutil/tests/eval.c
+++ b/libavutil/tests/eval.c
@@ -109,6 +109,35 @@ int main(int argc, char **argv)
         "not(0)",
         "6.0206dB",
         "-3.0103dB",
+        "20dB - 20dB",
+        "0dB - 0dB",
+        "1 - 0dB",
+        "1 - (0dB)",
+        "-20dB",
+        "-(20dB)",
+        "10 + -20dB",
+        "20dB - -20dB",
+        "20dB - +20dB",
+        "10 + +20dB",
+        "20dB - 20dB * 2",
+        "20dB - 20dB / 2",
+        "20dB - 20dB^2",
+        "20dB - -20dB^2",
+        "2^-20dB",
+        "20dB - 0dB - 0dB",
+        "-(20dB - 0dB)",
+        "10 - 2 * 3",
+        "10 - 2^3",
+        "10 - -2^3",
+        "10 - (2 - 3)",
+        "10 - sin(PI/2)",
+        "10 - st(0, 3); ld(0)",
+        "10 - (st(0, 3); ld(0))",
+        "10 - taylor(1, 0)",
+        "10 - root(ld(0)-2, 4)",
+        "st(0, 1); 10 - while(ld(0), st(0, 0); 3)",
+        "st(0, 42); st(1, random(0)); st(0, 42); ld(1) - random(0)",
+        "10 - randomi(0, 3, 3)",
         "pow(0,1.23)",
         "pow(PI,1.23)",
         "PI^1.23",
diff --git a/tests/ref/fate/eval b/tests/ref/fate/eval
index 196c1b5c86e..e307687f74b 100644
--- a/tests/ref/fate/eval
+++ b/tests/ref/fate/eval
@@ -210,6 +210,93 @@ Evaluating '6.0206dB'
 Evaluating '-3.0103dB'
 '-3.0103dB' -> 0.707107
 
+Evaluating '20dB - 20dB'
+'20dB - 20dB' -> 0.000000
+
+Evaluating '0dB - 0dB'
+'0dB - 0dB' -> 0.000000
+
+Evaluating '1 - 0dB'
+'1 - 0dB' -> 0.000000
+
+Evaluating '1 - (0dB)'
+'1 - (0dB)' -> 0.000000
+
+Evaluating '-20dB'
+'-20dB' -> 0.100000
+
+Evaluating '-(20dB)'
+'-(20dB)' -> -10.000000
+
+Evaluating '10 + -20dB'
+'10 + -20dB' -> 10.100000
+
+Evaluating '20dB - -20dB'
+'20dB - -20dB' -> 9.900000
+
+Evaluating '20dB - +20dB'
+'20dB - +20dB' -> 0.000000
+
+Evaluating '10 + +20dB'
+'10 + +20dB' -> 20.000000
+
+Evaluating '20dB - 20dB * 2'
+'20dB - 20dB * 2' -> -10.000000
+
+Evaluating '20dB - 20dB / 2'
+'20dB - 20dB / 2' -> 5.000000
+
+Evaluating '20dB - 20dB^2'
+'20dB - 20dB^2' -> -90.000000
+
+Evaluating '20dB - -20dB^2'
+'20dB - -20dB^2' -> 9.990000
+
+Evaluating '2^-20dB'
+'2^-20dB' -> 1.071773
+
+Evaluating '20dB - 0dB - 0dB'
+'20dB - 0dB - 0dB' -> 8.000000
+
+Evaluating '-(20dB - 0dB)'
+'-(20dB - 0dB)' -> -9.000000
+
+Evaluating '10 - 2 * 3'
+'10 - 2 * 3' -> 4.000000
+
+Evaluating '10 - 2^3'
+'10 - 2^3' -> 2.000000
+
+Evaluating '10 - -2^3'
+'10 - -2^3' -> 18.000000
+
+Evaluating '10 - (2 - 3)'
+'10 - (2 - 3)' -> 11.000000
+
+Evaluating '10 - sin(PI/2)'
+'10 - sin(PI/2)' -> 9.000000
+
+Evaluating '10 - st(0, 3); ld(0)'
+'10 - st(0, 3); ld(0)' -> 3.000000
+
+Evaluating '10 - (st(0, 3); ld(0))'
+'10 - (st(0, 3); ld(0))' -> 7.000000
+
+Evaluating '10 - taylor(1, 0)'
+'10 - taylor(1, 0)' -> 9.000000
+
+Evaluating '10 - root(ld(0)-2, 4)'
+'10 - root(ld(0)-2, 4)' -> 8.000000
+
+Evaluating 'st(0, 1); 10 - while(ld(0), st(0, 0); 3)'
+'st(0, 1); 10 - while(ld(0), st(0, 0); 3)' -> 7.000000
+
+Evaluating 'st(0, 42); st(1, random(0)); st(0, 42); ld(1) - random(0)'
+'st(0, 42); st(1, random(0)); st(0, 42); ld(1) - random(0)' -> 0.000000
+
+Evaluating '10 - randomi(0, 3, 3)'
+'10 - randomi(0, 3, 3)' -> 7.000000
+
 Evaluating 'pow(0,1.23)'
 'pow(0,1.23)' -> 0.000000
 
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to