[FFmpeg-cvslog] avfilter/asrc_anoisesrc: improve velvet noise output

Paul B Mahol git at videolan.org
Tue May 9 01:43:39 EEST 2023


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Mon May  8 22:23:35 2023 +0200| [086a0f3e5edee4d5f97c5759fbf5c893f58fb6e5] | committer: Paul B Mahol

avfilter/asrc_anoisesrc: improve velvet noise output

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

 doc/filters.texi             |  3 +++
 libavfilter/asrc_anoisesrc.c | 25 +++++++++++++++----------
 2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 284b6d798a..b965f05e60 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -7951,6 +7951,9 @@ Specify a value used to seed the PRNG.
 
 @item nb_samples, n
 Set the number of samples per each output frame, default is 1024.
+
+ at item density
+Set the density (0.0 - 1.0) for the velvet noise generator, default is 0.05.
 @end table
 
 @subsection Examples
diff --git a/libavfilter/asrc_anoisesrc.c b/libavfilter/asrc_anoisesrc.c
index 96a8fd63e9..1c8dccf689 100644
--- a/libavfilter/asrc_anoisesrc.c
+++ b/libavfilter/asrc_anoisesrc.c
@@ -31,6 +31,7 @@ typedef struct ANoiseSrcContext {
     const AVClass *class;
     int sample_rate;
     double amplitude;
+    double density;
     int64_t duration;
     int color;
     int64_t seed;
@@ -38,7 +39,7 @@ typedef struct ANoiseSrcContext {
 
     int64_t pts;
     int infinite;
-    double (*filter)(double white, double *buf, double half_amplitude);
+    double (*filter)(double white, double *buf);
     double buf[7];
     AVLFG c;
 } ANoiseSrcContext;
@@ -76,6 +77,7 @@ static const AVOption anoisesrc_options[] = {
     { "s",            "set random seed",  OFFSET(seed),         AV_OPT_TYPE_INT64,     {.i64 = -1},        -1,  UINT_MAX,   FLAGS },
     { "nb_samples",   "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
     { "n",            "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
+    { "density",      "set density",      OFFSET(density),      AV_OPT_TYPE_DOUBLE,    {.dbl = 0.05},       0., 1.,         FLAGS },
     {NULL}
 };
 
@@ -101,12 +103,12 @@ static av_cold int query_formats(AVFilterContext *ctx)
     return ff_set_common_samplerates_from_list(ctx, sample_rates);
 }
 
-static double white_filter(double white, double *buf, double ha)
+static double white_filter(double white, double *buf)
 {
     return white;
 }
 
-static double pink_filter(double white, double *buf, double ha)
+static double pink_filter(double white, double *buf)
 {
     double pink;
 
@@ -122,7 +124,7 @@ static double pink_filter(double white, double *buf, double ha)
     return pink * 0.11;
 }
 
-static double blue_filter(double white, double *buf, double ha)
+static double blue_filter(double white, double *buf)
 {
     double blue;
 
@@ -138,7 +140,7 @@ static double blue_filter(double white, double *buf, double ha)
     return blue * 0.11;
 }
 
-static double brown_filter(double white, double *buf, double ha)
+static double brown_filter(double white, double *buf)
 {
     double brown;
 
@@ -147,7 +149,7 @@ static double brown_filter(double white, double *buf, double ha)
     return brown * 3.5;
 }
 
-static double violet_filter(double white, double *buf, double ha)
+static double violet_filter(double white, double *buf)
 {
     double violet;
 
@@ -156,9 +158,10 @@ static double violet_filter(double white, double *buf, double ha)
     return violet * 3.5;
 }
 
-static double velvet_filter(double white, double *buf, double ha)
+static double velvet_filter(double white, double *buf)
 {
-    return 2. * ha * ((white > ha) - (white < -ha));
+    double awhite = fabs(white);
+    return FFDIFFSIGN(white, 0.0) * buf[1] * (awhite < buf[0]);
 }
 
 static av_cold int config_props(AVFilterLink *outlink)
@@ -180,7 +183,9 @@ static av_cold int config_props(AVFilterLink *outlink)
     case NM_BROWN:  s->filter = brown_filter;  break;
     case NM_BLUE:   s->filter = blue_filter;   break;
     case NM_VIOLET: s->filter = violet_filter; break;
-    case NM_VELVET: s->filter = velvet_filter; break;
+    case NM_VELVET: s->buf[0] = s->amplitude * s->density;
+                    s->buf[1] = s->amplitude;
+                    s->filter = velvet_filter; break;
     }
 
     return 0;
@@ -213,7 +218,7 @@ static int activate(AVFilterContext *ctx)
     for (i = 0; i < nb_samples; i++) {
         double white;
         white = s->amplitude * ((2 * ((double) av_lfg_get(&s->c) / 0xffffffff)) - 1);
-        dst[i] = s->filter(white, s->buf, s->amplitude * 0.5);
+        dst[i] = s->filter(white, s->buf);
     }
 
     if (!s->infinite)



More information about the ffmpeg-cvslog mailing list