]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/af_anlmdn.c
Merge commit '58d154922707bfeb873cb3a7476e0f94b17463dd'
[ffmpeg] / libavfilter / af_anlmdn.c
index 6d9e89f44c015b6a22b13c093ca19e2e9c1d8c1a..87c49c63b1c6a7db379efcf8fbe7ac85b21a1411 100644 (file)
@@ -41,6 +41,7 @@ typedef struct AudioNLMeansContext {
     float a;
     int64_t pd;
     int64_t rd;
+    int om;
 
     float pdiff_lut_scale;
     float weight_lut[WEIGHT_LUT_SIZE];
@@ -57,17 +58,29 @@ typedef struct AudioNLMeansContext {
     int64_t pts;
 
     AVAudioFifo *fifo;
+    int eof_left;
 
     AudioNLMDNDSPContext dsp;
 } AudioNLMeansContext;
 
+enum OutModes {
+    IN_MODE,
+    OUT_MODE,
+    NOISE_MODE,
+    NB_MODES
+};
+
 #define OFFSET(x) offsetof(AudioNLMeansContext, x)
 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
 
 static const AVOption anlmdn_options[] = {
-    { "s", "set denoising strength", OFFSET(a),  AV_OPT_TYPE_FLOAT,    {.dbl=1},       1,   9999, AF },
+    { "s", "set denoising strength", OFFSET(a),  AV_OPT_TYPE_FLOAT,    {.dbl=0.00001},0.00001, 10, AF },
     { "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" },
     { NULL }
 };
 
@@ -135,14 +148,18 @@ static int config_output(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
     AudioNLMeansContext *s = ctx->priv;
+    int ret;
 
     s->K = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE);
     s->S = av_rescale(s->rd, outlink->sample_rate, AV_TIME_BASE);
 
+    s->eof_left = -1;
     s->pts = AV_NOPTS_VALUE;
     s->H = s->K * 2 + 1;
     s->N = s->H + (s->K + s->S) * 2;
 
+    av_log(ctx, AV_LOG_DEBUG, "K:%d S:%d H:%d N:%d\n", s->K, s->S, s->H, s->N);
+
     av_frame_free(&s->in);
     av_frame_free(&s->cache);
     s->in = ff_get_audio_buffer(outlink, s->N);
@@ -157,6 +174,10 @@ static int config_output(AVFilterLink *outlink)
     if (!s->fifo)
         return AVERROR(ENOMEM);
 
+    ret = av_audio_fifo_write(s->fifo, (void **)s->in->extended_data, s->K + s->S);
+    if (ret < 0)
+        return ret;
+
     s->pdiff_lut_scale = 1.f / MAX_DIFF * WEIGHT_LUT_SIZE;
     for (int i = 0; i < WEIGHT_LUT_SIZE; i++) {
         float w = -i / s->pdiff_lut_scale;
@@ -175,9 +196,10 @@ static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
     AVFrame *out = arg;
     const int S = s->S;
     const int K = s->K;
+    const int om = s->om;
     const float *f = (const float *)(s->in->extended_data[ch]) + K;
     float *cache = (float *)s->cache->extended_data[ch];
-    const float sw = 32768.f / s->a;
+    const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a);
     float *dst = (float *)out->extended_data[ch] + s->offset;
 
     for (int i = S; i < s->H + S; i++) {
@@ -195,7 +217,7 @@ static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
             s->dsp.compute_cache(cache + S, f, S, K, i, i + 1);
         }
 
-        for (int j = 0; j < 2 * S; j++) {
+        for (int j = 0; j < 2 * S && !ctx->is_disabled; j++) {
             const float distance = cache[j];
             unsigned weight_lut_idx;
             float w;
@@ -214,7 +236,11 @@ static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
         P += f[i];
         Q += 1;
 
-        dst[i - S] = P / Q;
+        switch (om) {
+        case IN_MODE:    dst[i - S] = f[i];           break;
+        case OUT_MODE:   dst[i - S] = P / Q;          break;
+        case NOISE_MODE: dst[i - S] = f[i] - (P / Q); break;
+        }
     }
 
     return 0;
@@ -261,6 +287,10 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     if (out) {
         out->pts = s->pts;
         out->nb_samples = s->offset;
+        if (s->eof_left >= 0) {
+            out->nb_samples = FFMIN(s->eof_left, s->offset);
+            s->eof_left -= out->nb_samples;
+        }
         s->pts += s->offset;
 
         return ff_filter_frame(outlink, out);
@@ -269,6 +299,31 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     return ret;
 }
 
+static int request_frame(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AudioNLMeansContext *s = ctx->priv;
+    int ret;
+
+    ret = ff_request_frame(ctx->inputs[0]);
+
+    if (ret == AVERROR_EOF && s->eof_left != 0) {
+        AVFrame *in;
+
+        if (s->eof_left < 0)
+            s->eof_left = av_audio_fifo_size(s->fifo) - (s->S + s->K);
+        if (s->eof_left < 0)
+            return AVERROR_EOF;
+        in = ff_get_audio_buffer(outlink, s->H);
+        if (!in)
+            return AVERROR(ENOMEM);
+
+        return filter_frame(ctx->inputs[0], in);
+    }
+
+    return ret;
+}
+
 static av_cold void uninit(AVFilterContext *ctx)
 {
     AudioNLMeansContext *s = ctx->priv;
@@ -292,6 +347,7 @@ static const AVFilterPad outputs[] = {
         .name          = "default",
         .type          = AVMEDIA_TYPE_AUDIO,
         .config_props  = config_output,
+        .request_frame = request_frame,
     },
     { NULL }
 };
@@ -305,5 +361,6 @@ AVFilter ff_af_anlmdn = {
     .uninit        = uninit,
     .inputs        = inputs,
     .outputs       = outputs,
-    .flags         = AVFILTER_FLAG_SLICE_THREADS,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
+                     AVFILTER_FLAG_SLICE_THREADS,
 };