[FFmpeg-cvslog] lavu/eval: add randomi function to compute random value in interval

Stefano Sabatini git at videolan.org
Tue Jan 2 23:05:54 EET 2024


ffmpeg | branch: master | Stefano Sabatini <stefasab at gmail.com> | Thu Dec 28 19:09:22 2023 +0100| [7769afcab4c2160d39058b21bcfb7923f4b036eb] | committer: Stefano Sabatini

lavu/eval: add randomi function to compute random value in interval

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=7769afcab4c2160d39058b21bcfb7923f4b036eb
---

 Changelog        |  1 +
 doc/utils.texi   | 12 +++++++++---
 libavutil/eval.c | 26 +++++++++++++++++++-------
 3 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/Changelog b/Changelog
index 424bfc11af..419075dc56 100644
--- a/Changelog
+++ b/Changelog
@@ -15,6 +15,7 @@ version <next>:
 - tiltandshift filter
 - qrencode filter and qrencodesrc source
 - quirc filter
+- lavu/eval: introduce randomi() function in expressions
 
 version 6.1:
 - libaribcaption decoder
diff --git a/doc/utils.texi b/doc/utils.texi
index a0b8d4b62d..0c4f146f4f 100644
--- a/doc/utils.texi
+++ b/doc/utils.texi
@@ -939,9 +939,15 @@ Returns the value of the expression printed.
 
 Prints t with loglevel l
 
- at item random(x)
-Return a pseudo random value between 0.0 and 1.0. @var{x} is the index of the
-internal variable which will be used to save the seed/state.
+ at item random(idx)
+Return a pseudo random value between 0.0 and 1.0. @var{idx} is the
+index of the internal variable which will be used to save the
+seed/state.
+
+ at item randomi(idx, min, max)
+Return a pseudo random value in the interval between @var{min} and
+ at var{max}. @var{idx} is the index of the internal variable which will
+be used to save the seed/state.
 
 @item root(expr, max)
 Find an input value for which the function represented by @var{expr}
diff --git a/libavutil/eval.c b/libavutil/eval.c
index bad9e4ecb8..dc6b3697bc 100644
--- a/libavutil/eval.c
+++ b/libavutil/eval.c
@@ -162,7 +162,7 @@ struct AVExpr {
         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,
-        e_sgn,
+        e_sgn, e_randomi
     } type;
     double value; // is sign in other types
     int const_index;
@@ -228,12 +228,22 @@ static double eval_expr(Parser *p, AVExpr *e)
             av_log(p, level, "%f\n", x);
             return x;
         }
-        case e_random:{
-            int idx= av_clip(eval_expr(p, e->param[0]), 0, VARS-1);
-            uint64_t r= isnan(p->var[idx]) ? 0 : p->var[idx];
-            r= r*1664525+1013904223;
-            p->var[idx]= r;
-            return e->value * (r * (1.0/UINT64_MAX));
+
+#define COMPUTE_NEXT_RANDOM()                                        \
+            int idx = av_clip(eval_expr(p, e->param[0]), 0, VARS-1); \
+            uint64_t r = isnan(p->var[idx]) ? 0 : p->var[idx];       \
+            r = r * 1664525 + 1013904223;                            \
+            p->var[idx] = r;                                         \
+
+        case e_random: {
+            COMPUTE_NEXT_RANDOM();
+            return r * (1.0/UINT64_MAX);
+        }
+        case e_randomi: {
+            double min = eval_expr(p, e->param[1]);
+            double max = eval_expr(p, e->param[2]);
+            COMPUTE_NEXT_RANDOM();
+            return min + (max - min) * r / UINT64_MAX;
         }
         case e_while: {
             double d = NAN;
@@ -461,6 +471,7 @@ static int parse_primary(AVExpr **e, Parser *p)
     else if (strmatch(next, "pow"   )) d->type = e_pow;
     else if (strmatch(next, "print" )) d->type = e_print;
     else if (strmatch(next, "random")) d->type = e_random;
+    else if (strmatch(next, "randomi")) d->type = e_randomi;
     else if (strmatch(next, "hypot" )) d->type = e_hypot;
     else if (strmatch(next, "gcd"   )) d->type = e_gcd;
     else if (strmatch(next, "if"    )) d->type = e_if;
@@ -674,6 +685,7 @@ static int verify_expr(AVExpr *e)
         case e_between:
         case e_clip:
         case e_lerp:
+        case e_randomi:
             return verify_expr(e->param[0]) &&
                    verify_expr(e->param[1]) &&
                    verify_expr(e->param[2]);



More information about the ffmpeg-cvslog mailing list