On date Thursday 2023-12-28 16:00:57 +0100, Stefano Sabatini wrote:
> On date Thursday 2023-12-28 12:02:59 +0100, Andreas Rheinhardt wrote:
> > Stefano Sabatini:
> > > This is useful to simulate random jitter.
> > > ---
> > >  Changelog            |  1 +
> > >  doc/filters.texi     | 10 +++++++++-
> > >  libavfilter/setpts.c | 39 +++++++++++++++++++++++++++++++++------
> > >  3 files changed, 43 insertions(+), 7 deletions(-)
[...]
> > Why is this added here and not in lavu/eval so that it is available with
> > all expressions?
> 
> There is no specific reason. Sometimes you need to have a control over
> the seed, in this case you need to provide the PRNG context.
> 
> For the general case, probably we can place a global LFG in the eval
> module and fetch its generated values.

Another possible solution in attachment, leveraging the same affine
PRNG used in random(). For other use cases you might need to use a
high-quality PRNG and this might not be good enough.
>From 4dc5213b0913a2585f75dfd03b497ca2efc093de Mon Sep 17 00:00:00 2001
From: Stefano Sabatini <stefa...@gmail.com>
Date: Thu, 28 Dec 2023 19:09:22 +0100
Subject: [PATCH] lavu/eval: add randomi function to compute random value in
 interval

---
 libavutil/eval.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/libavutil/eval.c b/libavutil/eval.c
index 344bebbf69..e94597ccf4 100644
--- a/libavutil/eval.c
+++ b/libavutil/eval.c
@@ -164,7 +164,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;
@@ -230,13 +230,23 @@ static double eval_expr(Parser *p, AVExpr *e)
             av_log(p, level, "%f\n", x);
             return x;
         }
-        case e_random:{
+#define COMPUTE_RANDOM(seed_) (seed_)*1664525+1013904223
+    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;
+            r = COMPUTE_RANDOM(r);
             p->var[idx]= r;
             return e->value * (r * (1.0/UINT64_MAX));
         }
+        case e_randomi: {
+            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 = COMPUTE_RANDOM(r);
+            p->var[idx] = r;
+            double min = eval_expr(p, e->param[1]);
+            double max = eval_expr(p, e->param[2]);
+            return min + (max - min) * r / UINT64_MAX;
+        }
         case e_while: {
             double d = NAN;
             while (eval_expr(p, e->param[0]))
@@ -463,6 +473,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;
@@ -676,6 +687,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]);
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to