]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/af_anlmdn.c
avfilter/buffersink: deprecate AVBufferSinkParams and AVABufferSinkParams
[ffmpeg] / libavfilter / af_anlmdn.c
index 87c49c63b1c6a7db379efcf8fbe7ac85b21a1411..b8aef31c359e5ace79decbae067431ccea561f4e 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/audio_fifo.h"
+#include "libavutil/avstring.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "audio.h"
@@ -29,7 +30,6 @@
 
 #include "af_anlmdndsp.h"
 
-#define MAX_DIFF         11.f
 #define WEIGHT_LUT_NBITS 20
 #define WEIGHT_LUT_SIZE  (1<<WEIGHT_LUT_NBITS)
 
@@ -41,6 +41,7 @@ typedef struct AudioNLMeansContext {
     float a;
     int64_t pd;
     int64_t rd;
+    float m;
     int om;
 
     float pdiff_lut_scale;
@@ -72,15 +73,17 @@ enum OutModes {
 
 #define OFFSET(x) offsetof(AudioNLMeansContext, x)
 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+#define AFT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
 
 static const AVOption anlmdn_options[] = {
-    { "s", "set denoising strength", OFFSET(a),  AV_OPT_TYPE_FLOAT,    {.dbl=0.00001},0.00001, 10, AF },
+    { "s", "set denoising strength", OFFSET(a),  AV_OPT_TYPE_FLOAT,    {.dbl=0.00001},0.00001, 10, AFT },
     { "p", "set patch duration",     OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AF },
     { "r", "set research duration",  OFFSET(rd), AV_OPT_TYPE_DURATION, {.i64=6000}, 2000, 300000, AF },
-    { "o", "set output mode",        OFFSET(om), AV_OPT_TYPE_INT,      {.i64=OUT_MODE},  0, NB_MODES-1, AF, "mode" },
-    {  "i", "input",                 0,          AV_OPT_TYPE_CONST,    {.i64=IN_MODE},   0,  0, AF, "mode" },
-    {  "o", "output",                0,          AV_OPT_TYPE_CONST,    {.i64=OUT_MODE},  0,  0, AF, "mode" },
-    {  "n", "noise",                 0,          AV_OPT_TYPE_CONST,    {.i64=NOISE_MODE},0,  0, AF, "mode" },
+    { "o", "set output mode",        OFFSET(om), AV_OPT_TYPE_INT,      {.i64=OUT_MODE},  0, NB_MODES-1, AFT, "mode" },
+    {  "i", "input",                 0,          AV_OPT_TYPE_CONST,    {.i64=IN_MODE},   0,  0, AFT, "mode" },
+    {  "o", "output",                0,          AV_OPT_TYPE_CONST,    {.i64=OUT_MODE},  0,  0, AFT, "mode" },
+    {  "n", "noise",                 0,          AV_OPT_TYPE_CONST,    {.i64=NOISE_MODE},0,  0, AFT, "mode" },
+    { "m", "set smooth factor",      OFFSET(m),  AV_OPT_TYPE_FLOAT,    {.dbl=11.},       1, 15, AF },
     { NULL }
 };
 
@@ -178,7 +181,7 @@ static int config_output(AVFilterLink *outlink)
     if (ret < 0)
         return ret;
 
-    s->pdiff_lut_scale = 1.f / MAX_DIFF * WEIGHT_LUT_SIZE;
+    s->pdiff_lut_scale = 1.f / s->m * WEIGHT_LUT_SIZE;
     for (int i = 0; i < WEIGHT_LUT_SIZE; i++) {
         float w = -i / s->pdiff_lut_scale;
 
@@ -201,6 +204,7 @@ static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
     float *cache = (float *)s->cache->extended_data[ch];
     const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a);
     float *dst = (float *)out->extended_data[ch] + s->offset;
+    const float smooth = s->m;
 
     for (int i = S; i < s->H + S; i++) {
         float P = 0.f, Q = 0.f;
@@ -222,9 +226,12 @@ static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
             unsigned weight_lut_idx;
             float w;
 
-            av_assert2(distance >= 0.f);
+            if (distance < 0.f) {
+                cache[j] = 0.f;
+                continue;
+            }
             w = distance * sw;
-            if (w >= MAX_DIFF)
+            if (w >= smooth)
                 continue;
             weight_lut_idx = w * s->pdiff_lut_scale;
             av_assert2(weight_lut_idx < WEIGHT_LUT_SIZE);
@@ -291,7 +298,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
             out->nb_samples = FFMIN(s->eof_left, s->offset);
             s->eof_left -= out->nb_samples;
         }
-        s->pts += s->offset;
+        s->pts += av_rescale_q(s->offset, (AVRational){1, outlink->sample_rate}, outlink->time_base);
 
         return ff_filter_frame(outlink, out);
     }
@@ -312,7 +319,7 @@ static int request_frame(AVFilterLink *outlink)
 
         if (s->eof_left < 0)
             s->eof_left = av_audio_fifo_size(s->fifo) - (s->S + s->K);
-        if (s->eof_left < 0)
+        if (s->eof_left <= 0)
             return AVERROR_EOF;
         in = ff_get_audio_buffer(outlink, s->H);
         if (!in)
@@ -361,6 +368,7 @@ AVFilter ff_af_anlmdn = {
     .uninit        = uninit,
     .inputs        = inputs,
     .outputs       = outputs,
+    .process_command = ff_filter_process_command,
     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
                      AVFILTER_FLAG_SLICE_THREADS,
 };