Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 2/2] lavfi/setpts: introduce rand() function in expression
@ 2023-12-28  0:38 Stefano Sabatini
  2023-12-28 11:02 ` Andreas Rheinhardt
  0 siblings, 1 reply; 7+ messages in thread
From: Stefano Sabatini @ 2023-12-28  0:38 UTC (permalink / raw)
  To: FFmpeg development discussions and patches; +Cc: 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(-)

diff --git a/Changelog b/Changelog
index 424bfc11af..ed01c53264 100644
--- a/Changelog
+++ b/Changelog
@@ -15,6 +15,7 @@ version <next>:
 - tiltandshift filter
 - qrencode filter and qrencodesrc source
 - quirc filter
+- lavfi/setpts: introduce rand() function in expression
 
 version 6.1:
 - libaribcaption decoder
diff --git a/doc/filters.texi b/doc/filters.texi
index d1f95b9781..1d9a5d6c7d 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -30946,7 +30946,7 @@ The expression which is evaluated for each frame to construct its timestamp.
 @end table
 
 The expression is evaluated through the eval API and can contain the following
-constants:
+constants and functions:
 
 @table @option
 @item FRAME_RATE, FR
@@ -31010,6 +31010,8 @@ The timebase of the input timestamps.
 @item T_CHANGE
 Time of the first frame after command was applied or time of the first frame if no commands.
 
+@item rand(min, max)
+a random number included between min and max
 @end table
 
 @subsection Examples
@@ -31021,6 +31023,12 @@ Start counting PTS from zero
 setpts=PTS-STARTPTS
 @end example
 
+@item
+Apply a random jitter effect of +/-100 TB units:
+@example
+setpts=PTS+100rand(-100\,100)
+@end example
+
 @item
 Apply fast motion effect:
 @example
diff --git a/libavfilter/setpts.c b/libavfilter/setpts.c
index 88a8d6af86..0f24a900b3 100644
--- a/libavfilter/setpts.c
+++ b/libavfilter/setpts.c
@@ -32,6 +32,8 @@
 #include "libavutil/internal.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/opt.h"
+#include "libavutil/lfg.h"
+#include "libavutil/random_seed.h"
 #include "libavutil/time.h"
 #include "audio.h"
 #include "avfilter.h"
@@ -101,18 +103,39 @@ typedef struct SetPTSContext {
     AVExpr *expr;
     double var_values[VAR_VARS_NB];
     enum AVMediaType type;
+    AVLFG lfg;
 } SetPTSContext;
 
 #define V(name_) \
     setpts->var_values[VAR_##name_]
 
+static double drand(void *ctx, double min, double max)
+{
+    SetPTSContext *setpts = ((AVFilterContext *)ctx)->priv;
+
+    return min + (max-min) / UINT_MAX * av_lfg_get(&setpts->lfg);
+}
+
+typedef double (*eval_func2)(void *, double a, double b);
+
+static const eval_func2 fun2[] = {
+    drand,
+    NULL
+};
+
+static const char *const fun2_names[] = {
+    "rand"
+};
+
 static av_cold int init(AVFilterContext *ctx)
 {
     SetPTSContext *setpts = ctx->priv;
     int ret;
 
+    av_lfg_init(&setpts->lfg, av_get_random_seed());
+
     if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
-                             var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
+                             var_names, NULL, NULL, fun2_names, fun2, 0, ctx)) < 0) {
         av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
         return ret;
     }
@@ -126,6 +149,7 @@ static av_cold int init(AVFilterContext *ctx)
     V(STARTPTS)    = NAN;
     V(STARTT)      = NAN;
     V(T_CHANGE)    = NAN;
+
     return 0;
 }
 
@@ -159,8 +183,10 @@ static inline char *double2int64str(char *buf, double v)
     return buf;
 }
 
-static double eval_pts(SetPTSContext *setpts, AVFilterLink *inlink, AVFrame *frame, int64_t pts)
+static double eval_pts(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *frame, int64_t pts)
 {
+    SetPTSContext *setpts = ctx->priv;
+
     if (isnan(V(STARTPTS))) {
         V(STARTPTS) = TS2D(pts);
         V(STARTT  ) = TS2T(pts, inlink->time_base);
@@ -186,17 +212,18 @@ FF_ENABLE_DEPRECATION_WARNINGS
         }
     }
 
-    return av_expr_eval(setpts->expr, setpts->var_values, NULL);
+    return av_expr_eval(setpts->expr, setpts->var_values, ctx);
 }
 #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
 
 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
-    SetPTSContext *setpts = inlink->dst->priv;
+    AVFilterContext *ctx = inlink->dst;
+    SetPTSContext *setpts = ctx->priv;
     int64_t in_pts = frame->pts;
     double d;
 
-    d = eval_pts(setpts, inlink, frame, frame->pts);
+    d = eval_pts(ctx, inlink, frame, frame->pts);
     frame->pts = D2TS(d);
 
     av_log(inlink->dst, AV_LOG_TRACE,
@@ -250,7 +277,7 @@ static int activate(AVFilterContext *ctx)
         return filter_frame(inlink, in);
 
     if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
-        double d = eval_pts(setpts, inlink, NULL, pts);
+        double d = eval_pts(ctx, inlink, NULL, pts);
 
         av_log(ctx, AV_LOG_TRACE, "N:EOF PTS:%s T:%f -> PTS:%s T:%f\n",
                d2istr(V(PTS)), V(T), d2istr(d), TS2T(d, inlink->time_base));
-- 
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-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [FFmpeg-devel] [PATCH 2/2] lavfi/setpts: introduce rand() function in expression
  2023-12-28  0:38 [FFmpeg-devel] [PATCH 2/2] lavfi/setpts: introduce rand() function in expression Stefano Sabatini
@ 2023-12-28 11:02 ` Andreas Rheinhardt
  2023-12-28 15:00   ` Stefano Sabatini
  0 siblings, 1 reply; 7+ messages in thread
From: Andreas Rheinhardt @ 2023-12-28 11:02 UTC (permalink / raw)
  To: ffmpeg-devel

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(-)
> 
> diff --git a/Changelog b/Changelog
> index 424bfc11af..ed01c53264 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -15,6 +15,7 @@ version <next>:
>  - tiltandshift filter
>  - qrencode filter and qrencodesrc source
>  - quirc filter
> +- lavfi/setpts: introduce rand() function in expression
>  
>  version 6.1:
>  - libaribcaption decoder
> diff --git a/doc/filters.texi b/doc/filters.texi
> index d1f95b9781..1d9a5d6c7d 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -30946,7 +30946,7 @@ The expression which is evaluated for each frame to construct its timestamp.
>  @end table
>  
>  The expression is evaluated through the eval API and can contain the following
> -constants:
> +constants and functions:
>  
>  @table @option
>  @item FRAME_RATE, FR
> @@ -31010,6 +31010,8 @@ The timebase of the input timestamps.
>  @item T_CHANGE
>  Time of the first frame after command was applied or time of the first frame if no commands.
>  
> +@item rand(min, max)
> +a random number included between min and max
>  @end table
>  
>  @subsection Examples
> @@ -31021,6 +31023,12 @@ Start counting PTS from zero
>  setpts=PTS-STARTPTS
>  @end example
>  
> +@item
> +Apply a random jitter effect of +/-100 TB units:
> +@example
> +setpts=PTS+100rand(-100\,100)
> +@end example
> +
>  @item
>  Apply fast motion effect:
>  @example
> diff --git a/libavfilter/setpts.c b/libavfilter/setpts.c
> index 88a8d6af86..0f24a900b3 100644
> --- a/libavfilter/setpts.c
> +++ b/libavfilter/setpts.c
> @@ -32,6 +32,8 @@
>  #include "libavutil/internal.h"
>  #include "libavutil/mathematics.h"
>  #include "libavutil/opt.h"
> +#include "libavutil/lfg.h"
> +#include "libavutil/random_seed.h"
>  #include "libavutil/time.h"
>  #include "audio.h"
>  #include "avfilter.h"
> @@ -101,18 +103,39 @@ typedef struct SetPTSContext {
>      AVExpr *expr;
>      double var_values[VAR_VARS_NB];
>      enum AVMediaType type;
> +    AVLFG lfg;
>  } SetPTSContext;
>  
>  #define V(name_) \
>      setpts->var_values[VAR_##name_]
>  
> +static double drand(void *ctx, double min, double max)
> +{
> +    SetPTSContext *setpts = ((AVFilterContext *)ctx)->priv;
> +
> +    return min + (max-min) / UINT_MAX * av_lfg_get(&setpts->lfg);
> +}
> +
> +typedef double (*eval_func2)(void *, double a, double b);
> +
> +static const eval_func2 fun2[] = {
> +    drand,
> +    NULL
> +};
> +
> +static const char *const fun2_names[] = {
> +    "rand"
> +};
> +
>  static av_cold int init(AVFilterContext *ctx)
>  {
>      SetPTSContext *setpts = ctx->priv;
>      int ret;
>  
> +    av_lfg_init(&setpts->lfg, av_get_random_seed());
> +
>      if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
> -                             var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
> +                             var_names, NULL, NULL, fun2_names, fun2, 0, ctx)) < 0) {
>          av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
>          return ret;
>      }
> @@ -126,6 +149,7 @@ static av_cold int init(AVFilterContext *ctx)
>      V(STARTPTS)    = NAN;
>      V(STARTT)      = NAN;
>      V(T_CHANGE)    = NAN;
> +
>      return 0;
>  }
>  
> @@ -159,8 +183,10 @@ static inline char *double2int64str(char *buf, double v)
>      return buf;
>  }
>  
> -static double eval_pts(SetPTSContext *setpts, AVFilterLink *inlink, AVFrame *frame, int64_t pts)
> +static double eval_pts(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *frame, int64_t pts)
>  {
> +    SetPTSContext *setpts = ctx->priv;
> +
>      if (isnan(V(STARTPTS))) {
>          V(STARTPTS) = TS2D(pts);
>          V(STARTT  ) = TS2T(pts, inlink->time_base);
> @@ -186,17 +212,18 @@ FF_ENABLE_DEPRECATION_WARNINGS
>          }
>      }
>  
> -    return av_expr_eval(setpts->expr, setpts->var_values, NULL);
> +    return av_expr_eval(setpts->expr, setpts->var_values, ctx);
>  }
>  #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
>  
>  static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
>  {
> -    SetPTSContext *setpts = inlink->dst->priv;
> +    AVFilterContext *ctx = inlink->dst;
> +    SetPTSContext *setpts = ctx->priv;
>      int64_t in_pts = frame->pts;
>      double d;
>  
> -    d = eval_pts(setpts, inlink, frame, frame->pts);
> +    d = eval_pts(ctx, inlink, frame, frame->pts);
>      frame->pts = D2TS(d);
>  
>      av_log(inlink->dst, AV_LOG_TRACE,
> @@ -250,7 +277,7 @@ static int activate(AVFilterContext *ctx)
>          return filter_frame(inlink, in);
>  
>      if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
> -        double d = eval_pts(setpts, inlink, NULL, pts);
> +        double d = eval_pts(ctx, inlink, NULL, pts);
>  
>          av_log(ctx, AV_LOG_TRACE, "N:EOF PTS:%s T:%f -> PTS:%s T:%f\n",
>                 d2istr(V(PTS)), V(T), d2istr(d), TS2T(d, inlink->time_base));

Why is this added here and not in lavu/eval so that it is available with
all expressions?

- Andreas

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

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [FFmpeg-devel] [PATCH 2/2] lavfi/setpts: introduce rand() function in expression
  2023-12-28 11:02 ` Andreas Rheinhardt
@ 2023-12-28 15:00   ` Stefano Sabatini
  2023-12-28 18:49     ` Stefano Sabatini
  0 siblings, 1 reply; 7+ messages in thread
From: Stefano Sabatini @ 2023-12-28 15:00 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

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(-)
> > 
> > diff --git a/Changelog b/Changelog
> > index 424bfc11af..ed01c53264 100644
> > --- a/Changelog
> > +++ b/Changelog
> > @@ -15,6 +15,7 @@ version <next>:
> >  - tiltandshift filter
> >  - qrencode filter and qrencodesrc source
> >  - quirc filter
> > +- lavfi/setpts: introduce rand() function in expression
> >  
> >  version 6.1:
> >  - libaribcaption decoder
> > diff --git a/doc/filters.texi b/doc/filters.texi
> > index d1f95b9781..1d9a5d6c7d 100644
> > --- a/doc/filters.texi
> > +++ b/doc/filters.texi
> > @@ -30946,7 +30946,7 @@ The expression which is evaluated for each frame to construct its timestamp.
> >  @end table
> >  
> >  The expression is evaluated through the eval API and can contain the following
> > -constants:
> > +constants and functions:
> >  
> >  @table @option
> >  @item FRAME_RATE, FR
> > @@ -31010,6 +31010,8 @@ The timebase of the input timestamps.
> >  @item T_CHANGE
> >  Time of the first frame after command was applied or time of the first frame if no commands.
> >  
> > +@item rand(min, max)
> > +a random number included between min and max
> >  @end table
> >  
> >  @subsection Examples
> > @@ -31021,6 +31023,12 @@ Start counting PTS from zero
> >  setpts=PTS-STARTPTS
> >  @end example
> >  
> > +@item
> > +Apply a random jitter effect of +/-100 TB units:
> > +@example
> > +setpts=PTS+100rand(-100\,100)
> > +@end example
> > +
> >  @item
> >  Apply fast motion effect:
> >  @example
> > diff --git a/libavfilter/setpts.c b/libavfilter/setpts.c
> > index 88a8d6af86..0f24a900b3 100644
> > --- a/libavfilter/setpts.c
> > +++ b/libavfilter/setpts.c
> > @@ -32,6 +32,8 @@
> >  #include "libavutil/internal.h"
> >  #include "libavutil/mathematics.h"
> >  #include "libavutil/opt.h"
> > +#include "libavutil/lfg.h"
> > +#include "libavutil/random_seed.h"
> >  #include "libavutil/time.h"
> >  #include "audio.h"
> >  #include "avfilter.h"
> > @@ -101,18 +103,39 @@ typedef struct SetPTSContext {
> >      AVExpr *expr;
> >      double var_values[VAR_VARS_NB];
> >      enum AVMediaType type;
> > +    AVLFG lfg;
> >  } SetPTSContext;
> >  
> >  #define V(name_) \
> >      setpts->var_values[VAR_##name_]
> >  
> > +static double drand(void *ctx, double min, double max)
> > +{
> > +    SetPTSContext *setpts = ((AVFilterContext *)ctx)->priv;
> > +
> > +    return min + (max-min) / UINT_MAX * av_lfg_get(&setpts->lfg);
> > +}
> > +
[...]
> 

> 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.
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [FFmpeg-devel] [PATCH 2/2] lavfi/setpts: introduce rand() function in expression
  2023-12-28 15:00   ` Stefano Sabatini
@ 2023-12-28 18:49     ` Stefano Sabatini
  2023-12-28 21:25       ` Michael Niedermayer
  0 siblings, 1 reply; 7+ messages in thread
From: Stefano Sabatini @ 2023-12-28 18:49 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

[-- Attachment #1: Type: text/plain, Size: 976 bytes --]

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.

[-- Attachment #2: 0001-lavu-eval-add-randomi-function-to-compute-random-val.patch --]
[-- Type: text/x-diff, Size: 2679 bytes --]

From 4dc5213b0913a2585f75dfd03b497ca2efc093de Mon Sep 17 00:00:00 2001
From: Stefano Sabatini <stefasab@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


[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

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

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [FFmpeg-devel] [PATCH 2/2] lavfi/setpts: introduce rand() function in expression
  2023-12-28 18:49     ` Stefano Sabatini
@ 2023-12-28 21:25       ` Michael Niedermayer
  2023-12-29 11:51         ` Stefano Sabatini
  0 siblings, 1 reply; 7+ messages in thread
From: Michael Niedermayer @ 2023-12-28 21:25 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


[-- Attachment #1.1: Type: text/plain, Size: 1771 bytes --]

On Thu, Dec 28, 2023 at 07:49:18PM +0100, Stefano Sabatini wrote:
> 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.

>  eval.c |   18 +++++++++++++++---
>  1 file changed, 15 insertions(+), 3 deletions(-)
> 94413dcc88e3046b4987715f6c152aeb1c24c703  0001-lavu-eval-add-randomi-function-to-compute-random-val.patch
> From 4dc5213b0913a2585f75dfd03b497ca2efc093de Mon Sep 17 00:00:00 2001
> From: Stefano Sabatini <stefasab@gmail.com>
> Date: Thu, 28 Dec 2023 19:09:22 +0100
> Subject: [PATCH] lavu/eval: add randomi function to compute random value in
>  interval

probably ok if this is usefull

thx

[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Never trust a computer, one day, it may think you are the virus. -- Compn

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

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

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [FFmpeg-devel] [PATCH 2/2] lavfi/setpts: introduce rand() function in expression
  2023-12-28 21:25       ` Michael Niedermayer
@ 2023-12-29 11:51         ` Stefano Sabatini
  2024-01-02 21:12           ` Stefano Sabatini
  0 siblings, 1 reply; 7+ messages in thread
From: Stefano Sabatini @ 2023-12-29 11:51 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

[-- Attachment #1: Type: text/plain, Size: 1777 bytes --]

On date Thursday 2023-12-28 22:25:08 +0100, Michael Niedermayer wrote:
> On Thu, Dec 28, 2023 at 07:49:18PM +0100, Stefano Sabatini wrote:
> > 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.
> 
> >  eval.c |   18 +++++++++++++++---
> >  1 file changed, 15 insertions(+), 3 deletions(-)
> > 94413dcc88e3046b4987715f6c152aeb1c24c703  0001-lavu-eval-add-randomi-function-to-compute-random-val.patch
> > From 4dc5213b0913a2585f75dfd03b497ca2efc093de Mon Sep 17 00:00:00 2001
> > From: Stefano Sabatini <stefasab@gmail.com>
> > Date: Thu, 28 Dec 2023 19:09:22 +0100
> > Subject: [PATCH] lavu/eval: add randomi function to compute random value in
> >  interval
> 
> probably ok if this is usefull
> 
> thx

Will apply the edited patch in a few days if I see no comments.

[-- Attachment #2: 0001-lavu-eval-add-randomi-function-to-compute-random-val.patch --]
[-- Type: text/x-diff, Size: 4053 bytes --]

From c4c9eef66401f930a6428c6889020ff5e65c60d6 Mon Sep 17 00:00:00 2001
From: Stefano Sabatini <stefasab@gmail.com>
Date: Thu, 28 Dec 2023 19:09:22 +0100
Subject: [PATCH 1/2] lavu/eval: add randomi function to compute random value
 in interval

---
 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
 
-@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.
+@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.
+
+@item randomi(idx, min, max)
+Return a pseudo random value in the interval between @var{min} and
+@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]);
-- 
2.34.1


[-- Attachment #3: 0002-doc-filters-setpts-add-random-jitter-generation-exam.patch --]
[-- Type: text/x-diff, Size: 716 bytes --]

From 9f7bc9370ad853392622ac3008f14df99ed6210a Mon Sep 17 00:00:00 2001
From: Stefano Sabatini <stefasab@gmail.com>
Date: Fri, 29 Dec 2023 12:48:08 +0100
Subject: [PATCH 2/2] doc/filters/setpts: add random jitter generation example

---
 doc/filters.texi | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/doc/filters.texi b/doc/filters.texi
index d1f95b9781..6a9c7d532c 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -31039,6 +31039,12 @@ Set fixed rate of 25 frames per second:
 setpts=N/(25*TB)
 @end example
 
+@item
+Apply a random jitter effect of +/-100 TB units:
+@example
+setpts=PTS+randomi(0, -100\,100)
+@end example
+
 @item
 Set fixed rate 25 fps with some jitter:
 @example
-- 
2.34.1


[-- Attachment #4: Type: text/plain, Size: 251 bytes --]

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

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [FFmpeg-devel] [PATCH 2/2] lavfi/setpts: introduce rand() function in expression
  2023-12-29 11:51         ` Stefano Sabatini
@ 2024-01-02 21:12           ` Stefano Sabatini
  0 siblings, 0 replies; 7+ messages in thread
From: Stefano Sabatini @ 2024-01-02 21:12 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

On date Friday 2023-12-29 12:51:10 +0100, Stefano Sabatini wrote:
> On date Thursday 2023-12-28 22:25:08 +0100, Michael Niedermayer wrote:
> > On Thu, Dec 28, 2023 at 07:49:18PM +0100, Stefano Sabatini wrote:
> > > 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.
> > 
> > >  eval.c |   18 +++++++++++++++---
> > >  1 file changed, 15 insertions(+), 3 deletions(-)
> > > 94413dcc88e3046b4987715f6c152aeb1c24c703  0001-lavu-eval-add-randomi-function-to-compute-random-val.patch
> > > From 4dc5213b0913a2585f75dfd03b497ca2efc093de Mon Sep 17 00:00:00 2001
> > > From: Stefano Sabatini <stefasab@gmail.com>
> > > Date: Thu, 28 Dec 2023 19:09:22 +0100
> > > Subject: [PATCH] lavu/eval: add randomi function to compute random value in
> > >  interval
> > 
> > probably ok if this is usefull
> > 
> > thx
> 
> Will apply the edited patch in a few days if I see no comments.

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

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-01-02 21:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-28  0:38 [FFmpeg-devel] [PATCH 2/2] lavfi/setpts: introduce rand() function in expression Stefano Sabatini
2023-12-28 11:02 ` Andreas Rheinhardt
2023-12-28 15:00   ` Stefano Sabatini
2023-12-28 18:49     ` Stefano Sabatini
2023-12-28 21:25       ` Michael Niedermayer
2023-12-29 11:51         ` Stefano Sabatini
2024-01-02 21:12           ` Stefano Sabatini

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git