vf_hqdn3d.c 11.3 KB
Newer Older
1 2 3
/*
 * Copyright (c) 2003 Daniel Moreno <comac AT comac DOT darktech DOT org>
 * Copyright (c) 2010 Baptiste Coudurier
4
 * Copyright (c) 2012 Loren Merritt
5
 *
6
 * This file is part of Libav, ported from MPlayer.
7
 *
8
 * Libav is free software; you can redistribute it and/or modify
9 10 11 12
 * 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.
 *
13
 * Libav is distributed in the hope that it will be useful,
14 15 16 17 18
 * 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
19
 * with Libav; if not, write to the Free Software Foundation, Inc.,
20 21 22 23 24 25 26 27 28
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * @file
 * high quality 3d video denoiser, ported from MPlayer
 * libmpcodecs/vf_hqdn3d.c.
 */

29 30
#include <float.h>

31
#include "config.h"
32
#include "libavutil/attributes.h"
33
#include "libavutil/common.h"
34
#include "libavutil/pixdesc.h"
35
#include "libavutil/intreadwrite.h"
36 37
#include "libavutil/opt.h"

38
#include "avfilter.h"
39
#include "formats.h"
40
#include "internal.h"
41
#include "video.h"
42
#include "vf_hqdn3d.h"
Loren Merritt's avatar
Loren Merritt committed
43

44
#define LUT_BITS (depth==16 ? 8 : 4)
45 46 47 48
#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)))
49

50
av_always_inline
51
static uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
52
{
53
    int d = (prev - cur) >> (8 - LUT_BITS);
Loren Merritt's avatar
Loren Merritt committed
54
    return cur + coef[d];
55 56
}

57
av_always_inline
Loren Merritt's avatar
Loren Merritt committed
58 59 60
static void denoise_temporal(uint8_t *src, uint8_t *dst,
                             uint16_t *frame_ant,
                             int w, int h, int sstride, int dstride,
61
                             int16_t *temporal, int depth)
62
{
Loren Merritt's avatar
Loren Merritt committed
63
    long x, y;
64
    uint32_t tmp;
Loren Merritt's avatar
Loren Merritt committed
65

66
    temporal += 256 << LUT_BITS;
67

Loren Merritt's avatar
Loren Merritt committed
68 69
    for (y = 0; y < h; y++) {
        for (x = 0; x < w; x++) {
70
            frame_ant[x] = tmp = lowpass(frame_ant[x], LOAD(x), temporal, depth);
71
            STORE(x, tmp);
72
        }
Loren Merritt's avatar
Loren Merritt committed
73 74 75
        src += sstride;
        dst += dstride;
        frame_ant += w;
76 77 78
    }
}

79
av_always_inline
80
static void denoise_spatial(HQDN3DContext *s,
Loren Merritt's avatar
Loren Merritt committed
81
                            uint8_t *src, uint8_t *dst,
82
                            uint16_t *line_ant, uint16_t *frame_ant,
Loren Merritt's avatar
Loren Merritt committed
83
                            int w, int h, int sstride, int dstride,
84
                            int16_t *spatial, int16_t *temporal, int depth)
85
{
Loren Merritt's avatar
Loren Merritt committed
86 87
    long x, y;
    uint32_t pixel_ant;
88
    uint32_t tmp;
89

90 91
    spatial  += 256 << LUT_BITS;
    temporal += 256 << LUT_BITS;
92

93 94
    /* First line has no top neighbor. Only left one for each tmp and
     * last frame */
95
    pixel_ant = LOAD(0);
96
    for (x = 0; x < w; x++) {
97 98
        line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial, depth);
        frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
99
        STORE(x, tmp);
100 101
    }

Loren Merritt's avatar
Loren Merritt committed
102
    for (y = 1; y < h; y++) {
103 104 105
        src += sstride;
        dst += dstride;
        frame_ant += w;
106 107
        if (s->denoise_row[depth]) {
            s->denoise_row[depth](src, dst, line_ant, frame_ant, w, spatial, temporal);
Loren Merritt's avatar
Loren Merritt committed
108 109
            continue;
        }
110
        pixel_ant = LOAD(0);
111
        for (x = 0; x < w-1; x++) {
112 113 114
            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);
115
            STORE(x, tmp);
116
        }
117 118
        line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
        frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
119
        STORE(x, tmp);
120 121 122
    }
}

123
av_always_inline
124
static void denoise_depth(HQDN3DContext *s,
Loren Merritt's avatar
Loren Merritt committed
125
                          uint8_t *src, uint8_t *dst,
126 127 128
                          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)
129
{
130 131
    // FIXME: For 16bit depth, frame_ant could be a pointer to the previous
    // filtered frame rather than a separate buffer.
Loren Merritt's avatar
Loren Merritt committed
132 133 134
    long x, y;
    uint16_t *frame_ant = *frame_ant_ptr;
    if (!frame_ant) {
135
        uint8_t *frame_src = src;
Loren Merritt's avatar
Loren Merritt committed
136
        *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
137
        for (y = 0; y < h; y++, src += sstride, frame_ant += w)
Loren Merritt's avatar
Loren Merritt committed
138
            for (x = 0; x < w; x++)
139
                frame_ant[x] = LOAD(x);
140 141
        src = frame_src;
        frame_ant = *frame_ant_ptr;
142 143
    }

144
    if (spatial[0])
145
        denoise_spatial(s, src, dst, line_ant, frame_ant,
146
                        w, h, sstride, dstride, spatial, temporal, depth);
147
    else
Loren Merritt's avatar
Loren Merritt committed
148
        denoise_temporal(src, dst, frame_ant,
149
                         w, h, sstride, dstride, temporal, depth);
150
    emms_c();
151 152
}

153
#define denoise(...) \
154
    switch (s->depth) {\
155 156 157
        case  8: denoise_depth(__VA_ARGS__,  8); break;\
        case  9: denoise_depth(__VA_ARGS__,  9); break;\
        case 10: denoise_depth(__VA_ARGS__, 10); break;\
158
        case 16: denoise_depth(__VA_ARGS__, 16); break;\
159 160
    }

161
static int16_t *precalc_coefs(double dist25, int depth)
162 163
{
    int i;
Loren Merritt's avatar
Loren Merritt committed
164
    double gamma, simil, C;
165 166 167
    int16_t *ct = av_malloc((512<<LUT_BITS)*sizeof(int16_t));
    if (!ct)
        return NULL;
168

169
    gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
170

171 172
    for (i = -255<<LUT_BITS; i <= 255<<LUT_BITS; i++) {
        double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
173 174
        simil = 1.0 - FFABS(f) / 255.0;
        C = pow(simil, gamma) * 256.0 * f;
175
        ct[(256<<LUT_BITS)+i] = lrint(C);
176 177
    }

Loren Merritt's avatar
Loren Merritt committed
178
    ct[0] = !!dist25;
179
    return ct;
180 181 182 183 184 185
}

#define PARAM1_DEFAULT 4.0
#define PARAM2_DEFAULT 3.0
#define PARAM3_DEFAULT 6.0

186
static av_cold int init(AVFilterContext *ctx)
187
{
188
    HQDN3DContext *s = ctx->priv;
189

190 191 192 193 194 195 196 197
    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];
198

199
    av_log(ctx, AV_LOG_VERBOSE, "ls:%f cs:%f lt:%f ct:%f\n",
200 201
           s->strength[LUMA_SPATIAL], s->strength[CHROMA_SPATIAL],
           s->strength[LUMA_TMP], s->strength[CHROMA_TMP]);
202 203 204 205

    return 0;
}

206
static av_cold void uninit(AVFilterContext *ctx)
207
{
208 209 210 211 212 213 214 215 216 217
    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]);
218 219 220 221
}

static int query_formats(AVFilterContext *ctx)
{
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    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,
        AV_NE( AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUV420P9LE ),
        AV_NE( AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUV422P9LE ),
        AV_NE( AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUV444P9LE ),
        AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
        AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
        AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
        AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
        AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
        AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
        AV_PIX_FMT_NONE
243 244
    };

245
    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
246 247 248 249 250 251

    return 0;
}

static int config_input(AVFilterLink *inlink)
{
252
    HQDN3DContext *s = inlink->dst->priv;
253
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
254
    int i;
255

256 257
    uninit(inlink->dst);

258 259 260
    s->hsub  = desc->log2_chroma_w;
    s->vsub  = desc->log2_chroma_h;
    s->depth = desc->comp[0].depth_minus1+1;
261

262 263
    s->line = av_malloc(inlink->w * sizeof(*s->line));
    if (!s->line)
264 265
        return AVERROR(ENOMEM);

266
    for (i = 0; i < 4; i++) {
267 268
        s->coefs[i] = precalc_coefs(s->strength[i], s->depth);
        if (!s->coefs[i])
269 270 271
            return AVERROR(ENOMEM);
    }

272
    if (ARCH_X86)
273
        ff_hqdn3d_init_x86(s);
Loren Merritt's avatar
Loren Merritt committed
274

275 276 277
    return 0;
}

278
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
279
{
280
    HQDN3DContext *s = inlink->dst->priv;
281
    AVFilterLink *outlink = inlink->dst->outputs[0];
282
    AVFrame *out;
283 284
    int direct, c;

285
    if (av_frame_is_writable(in)) {
286 287 288
        direct = 1;
        out = in;
    } else {
289
        direct = 0;
290
        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
291
        if (!out) {
292
            av_frame_free(&in);
293 294 295
            return AVERROR(ENOMEM);
        }

296 297 298
        av_frame_copy_props(out, in);
        out->width  = outlink->w;
        out->height = outlink->h;
299
    }
300 301

    for (c = 0; c < 3; c++) {
302 303 304 305
        denoise(s, in->data[c], out->data[c],
                s->line, &s->frame_prev[c],
                in->width  >> (!!c * s->hsub),
                in->height >> (!!c * s->vsub),
306
                in->linesize[c], out->linesize[c],
307
                s->coefs[c?2:0], s->coefs[c?3:1]);
308
    }
309

310
    if (!direct)
311
        av_frame_free(&in);
312 313

    return ff_filter_frame(outlink, out);
314 315
}

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
#define OFFSET(x) offsetof(HQDN3DContext, x)
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
static const AVOption options[] = {
    { "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 },
    { NULL },
};

static const AVClass hqdn3d_class = {
    .class_name = "hqdn3d",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

333 334 335 336 337
static const AVFilterPad avfilter_vf_hqdn3d_inputs[] = {
    {
        .name         = "default",
        .type         = AVMEDIA_TYPE_VIDEO,
        .config_props = config_input,
338
        .filter_frame = filter_frame,
339 340 341 342 343 344 345 346 347 348 349 350
    },
    { NULL }
};

static const AVFilterPad avfilter_vf_hqdn3d_outputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_VIDEO
    },
    { NULL }
};

351
AVFilter ff_vf_hqdn3d = {
352 353 354 355
    .name          = "hqdn3d",
    .description   = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),

    .priv_size     = sizeof(HQDN3DContext),
356
    .priv_class    = &hqdn3d_class,
357 358 359 360
    .init          = init,
    .uninit        = uninit,
    .query_formats = query_formats,

361 362 363
    .inputs    = avfilter_vf_hqdn3d_inputs,

    .outputs   = avfilter_vf_hqdn3d_outputs,
364
};