libavfilter/vf_hqdn3d.c
a4dc7aa5
 /*
  * Copyright (c) 2003 Daniel Moreno <comac AT comac DOT darktech DOT org>
  * Copyright (c) 2010 Baptiste Coudurier
85e228c7
  * Copyright (c) 2012 Loren Merritt
a4dc7aa5
  *
  * This file is part of FFmpeg, ported from MPlayer.
  *
  * FFmpeg is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License along
  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 /**
  * @file
  * high quality 3d video denoiser, ported from MPlayer
  * libmpcodecs/vf_hqdn3d.c.
  */
 
8c747d46
 #include <float.h>
 
76d90125
 #include "config.h"
093804a9
 #include "libavutil/attributes.h"
1d9c2dc8
 #include "libavutil/common.h"
a4dc7aa5
 #include "libavutil/pixdesc.h"
1ad715db
 #include "libavutil/intreadwrite.h"
8c747d46
 #include "libavutil/opt.h"
 
a4dc7aa5
 #include "avfilter.h"
b74a1da4
 #include "formats.h"
9d0bfc50
 #include "internal.h"
803391f7
 #include "video.h"
76d90125
 #include "vf_hqdn3d.h"
7a1944b9
 
566858a7
 #define LUT_BITS (depth==16 ? 8 : 4)
5b3c1aec
 #define LOAD(x) (((depth == 8 ? src[x] : AV_RN16A(src + (x) * 2)) << (16 - depth))\
                  + (((1 << (16 - depth)) - 1) >> 1))
 #define STORE(x,val) (depth == 8 ? dst[x] = (val) >> (16 - depth) : \
                                    AV_WN16A(dst + (x) * 2, (val) >> (16 - depth)))
1ad715db
 
566858a7
 av_always_inline
4f92d31a
 static uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
a4dc7aa5
 {
566858a7
     int d = (prev - cur) >> (8 - LUT_BITS);
60b97855
     return cur + coef[d];
a4dc7aa5
 }
 
1ad715db
 av_always_inline
60b97855
 static void denoise_temporal(uint8_t *src, uint8_t *dst,
                              uint16_t *frame_ant,
                              int w, int h, int sstride, int dstride,
1ad715db
                              int16_t *temporal, int depth)
a4dc7aa5
 {
60b97855
     long x, y;
85e228c7
     uint32_t tmp;
60b97855
 
566858a7
     temporal += 256 << LUT_BITS;
0f583e6c
 
60b97855
     for (y = 0; y < h; y++) {
         for (x = 0; x < w; x++) {
566858a7
             frame_ant[x] = tmp = lowpass(frame_ant[x], LOAD(x), temporal, depth);
1ad715db
             STORE(x, tmp);
a4dc7aa5
         }
60b97855
         src += sstride;
         dst += dstride;
         frame_ant += w;
a4dc7aa5
     }
 }
 
1ad715db
 av_always_inline
56e4ce0d
 static void denoise_spatial(HQDN3DContext *s,
7a1944b9
                             uint8_t *src, uint8_t *dst,
0f583e6c
                             uint16_t *line_ant, uint16_t *frame_ant,
60b97855
                             int w, int h, int sstride, int dstride,
1ad715db
                             int16_t *spatial, int16_t *temporal, int depth)
a4dc7aa5
 {
60b97855
     long x, y;
     uint32_t pixel_ant;
85e228c7
     uint32_t tmp;
a4dc7aa5
 
566858a7
     spatial  += 256 << LUT_BITS;
     temporal += 256 << LUT_BITS;
0f583e6c
 
85e228c7
     /* First line has no top neighbor. Only left one for each tmp and
      * last frame */
1ad715db
     pixel_ant = LOAD(0);
85e228c7
     for (x = 0; x < w; x++) {
566858a7
         line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial, depth);
         frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
1ad715db
         STORE(x, tmp);
a4dc7aa5
     }
 
60b97855
     for (y = 1; y < h; y++) {
85e228c7
         src += sstride;
         dst += dstride;
         frame_ant += w;
56e4ce0d
         if (s->denoise_row[depth]) {
             s->denoise_row[depth](src, dst, line_ant, frame_ant, w, spatial, temporal);
7a1944b9
             continue;
         }
1ad715db
         pixel_ant = LOAD(0);
85e228c7
         for (x = 0; x < w-1; x++) {
566858a7
             line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
             pixel_ant = lowpass(pixel_ant, LOAD(x+1), spatial, depth);
             frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
1ad715db
             STORE(x, tmp);
a4dc7aa5
         }
566858a7
         line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
         frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
1ad715db
         STORE(x, tmp);
a4dc7aa5
     }
 }
 
1ad715db
 av_always_inline
22b985d5
 static int denoise_depth(HQDN3DContext *s,
                          uint8_t *src, uint8_t *dst,
                          uint16_t *line_ant, uint16_t **frame_ant_ptr,
                          int w, int h, int sstride, int dstride,
                          int16_t *spatial, int16_t *temporal, int depth)
a4dc7aa5
 {
41ed7ab4
     // FIXME: For 16-bit depth, frame_ant could be a pointer to the previous
566858a7
     // filtered frame rather than a separate buffer.
60b97855
     long x, y;
     uint16_t *frame_ant = *frame_ant_ptr;
     if (!frame_ant) {
85e228c7
         uint8_t *frame_src = src;
14e2e40f
         *frame_ant_ptr = frame_ant = av_malloc_array(w, h*sizeof(uint16_t));
22b985d5
         if (!frame_ant)
             return AVERROR(ENOMEM);
85e228c7
         for (y = 0; y < h; y++, src += sstride, frame_ant += w)
60b97855
             for (x = 0; x < w; x++)
1ad715db
                 frame_ant[x] = LOAD(x);
85e228c7
         src = frame_src;
         frame_ant = *frame_ant_ptr;
a4dc7aa5
     }
 
85e228c7
     if (spatial[0])
56e4ce0d
         denoise_spatial(s, src, dst, line_ant, frame_ant,
1ad715db
                         w, h, sstride, dstride, spatial, temporal, depth);
85e228c7
     else
60b97855
         denoise_temporal(src, dst, frame_ant,
1ad715db
                          w, h, sstride, dstride, temporal, depth);
e995cf1b
     emms_c();
22b985d5
     return 0;
a4dc7aa5
 }
 
22b985d5
 #define denoise(...)                                                          \
     do {                                                                      \
55feff57
         int ret = AVERROR_BUG;                                                \
22b985d5
         switch (s->depth) {                                                   \
             case  8: ret = denoise_depth(__VA_ARGS__,  8); break;             \
             case  9: ret = denoise_depth(__VA_ARGS__,  9); break;             \
             case 10: ret = denoise_depth(__VA_ARGS__, 10); break;             \
             case 16: ret = denoise_depth(__VA_ARGS__, 16); break;             \
         }                                                                     \
         if (ret < 0) {                                                        \
             av_frame_free(&out);                                              \
             if (!direct)                                                      \
                 av_frame_free(&in);                                           \
             return ret;                                                       \
         }                                                                     \
     } while (0)
a4dc7aa5
 
566858a7
 static int16_t *precalc_coefs(double dist25, int depth)
a4dc7aa5
 {
     int i;
60b97855
     double gamma, simil, C;
566858a7
     int16_t *ct = av_malloc((512<<LUT_BITS)*sizeof(int16_t));
     if (!ct)
         return NULL;
a4dc7aa5
 
0f583e6c
     gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
a4dc7aa5
 
4240e6a9
     for (i = -256<<LUT_BITS; i < 256<<LUT_BITS; i++) {
566858a7
         double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
8507b98c
         simil = FFMAX(0, 1.0 - fabs(f) / 255.0);
0f583e6c
         C = pow(simil, gamma) * 256.0 * f;
566858a7
         ct[(256<<LUT_BITS)+i] = lrint(C);
a4dc7aa5
     }
 
60b97855
     ct[0] = !!dist25;
566858a7
     return ct;
a4dc7aa5
 }
 
 #define PARAM1_DEFAULT 4.0
 #define PARAM2_DEFAULT 3.0
 #define PARAM3_DEFAULT 6.0
 
093804a9
 static av_cold int init(AVFilterContext *ctx)
a4dc7aa5
 {
56e4ce0d
     HQDN3DContext *s = ctx->priv;
a4dc7aa5
 
56e4ce0d
     if (!s->strength[LUMA_SPATIAL])
         s->strength[LUMA_SPATIAL] = PARAM1_DEFAULT;
     if (!s->strength[CHROMA_SPATIAL])
         s->strength[CHROMA_SPATIAL] = PARAM2_DEFAULT * s->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
     if (!s->strength[LUMA_TMP])
         s->strength[LUMA_TMP]   = PARAM3_DEFAULT * s->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
     if (!s->strength[CHROMA_TMP])
         s->strength[CHROMA_TMP] = s->strength[LUMA_TMP] * s->strength[CHROMA_SPATIAL] / s->strength[LUMA_SPATIAL];
566858a7
 
bcb8d9eb
     av_log(ctx, AV_LOG_VERBOSE, "ls:%f cs:%f lt:%f ct:%f\n",
56e4ce0d
            s->strength[LUMA_SPATIAL], s->strength[CHROMA_SPATIAL],
            s->strength[LUMA_TMP], s->strength[CHROMA_TMP]);
a4dc7aa5
 
     return 0;
 }
 
093804a9
 static av_cold void uninit(AVFilterContext *ctx)
a4dc7aa5
 {
56e4ce0d
     HQDN3DContext *s = ctx->priv;
 
     av_freep(&s->coefs[0]);
     av_freep(&s->coefs[1]);
     av_freep(&s->coefs[2]);
     av_freep(&s->coefs[3]);
     av_freep(&s->line);
     av_freep(&s->frame_prev[0]);
     av_freep(&s->frame_prev[1]);
     av_freep(&s->frame_prev[2]);
a4dc7aa5
 }
 
 static int query_formats(AVFilterContext *ctx)
 {
716d413c
     static const enum AVPixelFormat pix_fmts[] = {
         AV_PIX_FMT_YUV420P,
         AV_PIX_FMT_YUV422P,
         AV_PIX_FMT_YUV444P,
         AV_PIX_FMT_YUV410P,
         AV_PIX_FMT_YUV411P,
         AV_PIX_FMT_YUV440P,
         AV_PIX_FMT_YUVJ420P,
         AV_PIX_FMT_YUVJ422P,
         AV_PIX_FMT_YUVJ444P,
         AV_PIX_FMT_YUVJ440P,
a8e00cf9
         AV_PIX_FMT_YUV420P9,
         AV_PIX_FMT_YUV422P9,
         AV_PIX_FMT_YUV444P9,
         AV_PIX_FMT_YUV420P10,
         AV_PIX_FMT_YUV422P10,
         AV_PIX_FMT_YUV444P10,
         AV_PIX_FMT_YUV420P16,
         AV_PIX_FMT_YUV422P16,
         AV_PIX_FMT_YUV444P16,
716d413c
         AV_PIX_FMT_NONE
a4dc7aa5
     };
fd682b18
     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     if (!fmts_list)
         return AVERROR(ENOMEM);
     return ff_set_common_formats(ctx, fmts_list);
a4dc7aa5
 }
 
 static int config_input(AVFilterLink *inlink)
 {
56e4ce0d
     HQDN3DContext *s = inlink->dst->priv;
59ee9f78
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
851bac4b
     int i;
a4dc7aa5
 
3ba35a34
     uninit(inlink->dst);
 
56e4ce0d
     s->hsub  = desc->log2_chroma_w;
     s->vsub  = desc->log2_chroma_h;
2268db2c
     s->depth = desc->comp[0].depth;
a4dc7aa5
 
14e2e40f
     s->line = av_malloc_array(inlink->w, sizeof(*s->line));
56e4ce0d
     if (!s->line)
a4dc7aa5
         return AVERROR(ENOMEM);
 
2bd67175
     for (i = 0; i < 4; i++) {
56e4ce0d
         s->coefs[i] = precalc_coefs(s->strength[i], s->depth);
         if (!s->coefs[i])
566858a7
             return AVERROR(ENOMEM);
     }
 
76d90125
     if (ARCH_X86)
56e4ce0d
         ff_hqdn3d_init_x86(s);
7a1944b9
 
a4dc7aa5
     return 0;
 }
 
7e350379
 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
a4dc7aa5
 {
0122300c
     AVFilterContext *ctx  = inlink->dst;
df003cbb
     HQDN3DContext *s = ctx->priv;
0122300c
     AVFilterLink *outlink = ctx->outputs[0];
1b43fc12
 
7e350379
     AVFrame *out;
ccccfb59
     int c, direct = av_frame_is_writable(in) && !ctx->is_disabled;
1b43fc12
 
22b985d5
     if (direct) {
1b43fc12
         out = in;
     } else {
7e350379
         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1b43fc12
         if (!out) {
7e350379
             av_frame_free(&in);
1b43fc12
             return AVERROR(ENOMEM);
         }
565e4993
 
7e350379
         av_frame_copy_props(out, in);
1b43fc12
     }
85e228c7
 
     for (c = 0; c < 3; c++) {
56e4ce0d
         denoise(s, in->data[c], out->data[c],
                 s->line, &s->frame_prev[c],
21f94684
                 AV_CEIL_RSHIFT(in->width,  (!!c * s->hsub)),
                 AV_CEIL_RSHIFT(in->height, (!!c * s->vsub)),
1b43fc12
                 in->linesize[c], out->linesize[c],
df003cbb
                 s->coefs[c ? CHROMA_SPATIAL : LUMA_SPATIAL],
                 s->coefs[c ? CHROMA_TMP     : LUMA_TMP]);
85e228c7
     }
a4dc7aa5
 
0122300c
     if (ctx->is_disabled) {
         av_frame_free(&out);
         return ff_filter_frame(outlink, in);
     }
 
1b43fc12
     if (!direct)
7e350379
         av_frame_free(&in);
1b43fc12
 
     return ff_filter_frame(outlink, out);
a4dc7aa5
 }
 
8c747d46
 #define OFFSET(x) offsetof(HQDN3DContext, x)
3dedcef8
 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
622c9774
 static const AVOption hqdn3d_options[] = {
8c747d46
     { "luma_spatial",   "spatial luma strength",    OFFSET(strength[LUMA_SPATIAL]),   AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
     { "chroma_spatial", "spatial chroma strength",  OFFSET(strength[CHROMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
     { "luma_tmp",       "temporal luma strength",   OFFSET(strength[LUMA_TMP]),       AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
     { "chroma_tmp",     "temporal chroma strength", OFFSET(strength[CHROMA_TMP]),     AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
b211607b
     { NULL }
8c747d46
 };
 
622c9774
 AVFILTER_DEFINE_CLASS(hqdn3d);
8c747d46
 
568c70e7
 static const AVFilterPad avfilter_vf_hqdn3d_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = config_input,
1b43fc12
         .filter_frame = filter_frame,
568c70e7
     },
     { NULL }
 };
 
1b43fc12
 
568c70e7
 static const AVFilterPad avfilter_vf_hqdn3d_outputs[] = {
     {
         .name = "default",
         .type = AVMEDIA_TYPE_VIDEO
     },
     { NULL }
 };
 
cd43ca04
 AVFilter ff_vf_hqdn3d = {
a4dc7aa5
     .name          = "hqdn3d",
     .description   = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
     .priv_size     = sizeof(HQDN3DContext),
8c747d46
     .priv_class    = &hqdn3d_class,
a4dc7aa5
     .init          = init,
     .uninit        = uninit,
     .query_formats = query_formats,
b211607b
     .inputs        = avfilter_vf_hqdn3d_inputs,
     .outputs       = avfilter_vf_hqdn3d_outputs,
     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
a4dc7aa5
 };