]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_nlmeans.c
avfilter/vf_identity: remove unnecessary check
[ffmpeg] / libavfilter / vf_nlmeans.c
index 82e779ce854d519d693b36df746741350f0aca31..7451bec4ae6e294f622b4b28fb5e340f06427842 100644 (file)
@@ -43,9 +43,6 @@ struct weighted_avg {
     float sum;
 };
 
-#define WEIGHT_LUT_NBITS 9
-#define WEIGHT_LUT_SIZE  (1<<WEIGHT_LUT_NBITS)
-
 typedef struct NLMeansContext {
     const AVClass *class;
     int nb_planes;
@@ -62,8 +59,7 @@ typedef struct NLMeansContext {
     ptrdiff_t ii_lz_32;                         // linesize in 32-bit units of the integral image
     struct weighted_avg *wa;                    // weighted average of every pixel
     ptrdiff_t wa_linesize;                      // linesize for wa in struct size unit
-    float weight_lut[WEIGHT_LUT_SIZE];          // lookup table mapping (scaled) patch differences to their associated weights
-    float pdiff_lut_scale;                      // scale factor for patch differences before looking into the LUT
+    float *weight_lut;                          // lookup table mapping (scaled) patch differences to their associated weights
     uint32_t max_meaningful_diff;               // maximum difference considered (if the patch difference is too high we ignore the pixel)
     NLMeansDSPContext dsp;
 } NLMeansContext;
@@ -401,8 +397,7 @@ static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs
             const uint32_t patch_diff_sq = e - d - b + a;
 
             if (patch_diff_sq < s->max_meaningful_diff) {
-                const unsigned weight_lut_idx = patch_diff_sq * s->pdiff_lut_scale;
-                const float weight = s->weight_lut[weight_lut_idx]; // exp(-patch_diff_sq * s->pdiff_scale)
+                const float weight = s->weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale)
                 wa[x].total_weight += weight;
                 wa[x].sum += weight * src[x];
             }
@@ -424,7 +419,7 @@ static void weight_averages(uint8_t *dst, ptrdiff_t dst_linesize,
             // Also weight the centered pixel
             wa[x].total_weight += 1.f;
             wa[x].sum += 1.f * src[x];
-            dst[x] = av_clip_uint8(wa[x].sum / wa[x].total_weight);
+            dst[x] = av_clip_uint8(wa[x].sum / wa[x].total_weight + 0.5f);
         }
         dst += dst_linesize;
         src += src_linesize;
@@ -526,11 +521,12 @@ static av_cold int init(AVFilterContext *ctx)
     const double h = s->sigma * 10.;
 
     s->pdiff_scale = 1. / (h * h);
-    s->max_meaningful_diff = -log(1/255.) / s->pdiff_scale;
-    s->pdiff_lut_scale = 1./s->max_meaningful_diff * WEIGHT_LUT_SIZE;
-    av_assert0((s->max_meaningful_diff - 1) * s->pdiff_lut_scale < FF_ARRAY_ELEMS(s->weight_lut));
-    for (i = 0; i < WEIGHT_LUT_SIZE; i++)
-        s->weight_lut[i] = exp(-i / s->pdiff_lut_scale * s->pdiff_scale);
+    s->max_meaningful_diff = log(255.) / s->pdiff_scale;
+    s->weight_lut = av_calloc(s->max_meaningful_diff, sizeof(*s->weight_lut));
+    if (!s->weight_lut)
+        return AVERROR(ENOMEM);
+    for (i = 0; i < s->max_meaningful_diff; i++)
+        s->weight_lut[i] = exp(-i * s->pdiff_scale);
 
     CHECK_ODD_FIELD(research_size,   "Luma research window");
     CHECK_ODD_FIELD(patch_size,      "Luma patch");
@@ -558,6 +554,7 @@ static av_cold int init(AVFilterContext *ctx)
 static av_cold void uninit(AVFilterContext *ctx)
 {
     NLMeansContext *s = ctx->priv;
+    av_freep(&s->weight_lut);
     av_freep(&s->ii_orig);
     av_freep(&s->wa);
 }
@@ -580,7 +577,7 @@ static const AVFilterPad nlmeans_outputs[] = {
     { NULL }
 };
 
-AVFilter ff_vf_nlmeans = {
+const AVFilter ff_vf_nlmeans = {
     .name          = "nlmeans",
     .description   = NULL_IF_CONFIG_SMALL("Non-local means denoiser."),
     .priv_size     = sizeof(NLMeansContext),