X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Fvf_nlmeans.c;h=06233b0dd46a0cdbb9bc423bbc95467c728c68b9;hb=4b4282bc57b63de2780333d37487a38f88fb48c7;hp=82e779ce854d519d693b36df746741350f0aca31;hpb=aebc5b2284db1f40a5b3e2e9a2bf406f606436c7;p=ffmpeg diff --git a/libavfilter/vf_nlmeans.c b/libavfilter/vf_nlmeans.c index 82e779ce854..06233b0dd46 100644 --- a/libavfilter/vf_nlmeans.c +++ b/libavfilter/vf_nlmeans.c @@ -43,9 +43,6 @@ struct weighted_avg { float sum; }; -#define WEIGHT_LUT_NBITS 9 -#define WEIGHT_LUT_SIZE (1<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); }