2 * Copyright (c) 2003 Daniel Moreno <comac AT comac DOT darktech DOT org>
3 * Copyright (c) 2010 Baptiste Coudurier
4 * Copyright (c) 2012 Loren Merritt
6 * This file is part of Libav, ported from MPlayer.
8 * Libav is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * high quality 3d video denoiser, ported from MPlayer
26 * libmpcodecs/vf_hqdn3d.c.
32 #include "libavutil/common.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/opt.h"
41 #include "vf_hqdn3d.h"
43 #define LUT_BITS (depth==16 ? 8 : 4)
44 #define LOAD(x) (((depth == 8 ? src[x] : AV_RN16A(src + (x) * 2)) << (16 - depth))\
45 + (((1 << (16 - depth)) - 1) >> 1))
46 #define STORE(x,val) (depth == 8 ? dst[x] = (val) >> (16 - depth) : \
47 AV_WN16A(dst + (x) * 2, (val) >> (16 - depth)))
50 static uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
52 int d = (prev - cur) >> (8 - LUT_BITS);
57 static void denoise_temporal(uint8_t *src, uint8_t *dst,
59 int w, int h, int sstride, int dstride,
60 int16_t *temporal, int depth)
65 temporal += 256 << LUT_BITS;
67 for (y = 0; y < h; y++) {
68 for (x = 0; x < w; x++) {
69 frame_ant[x] = tmp = lowpass(frame_ant[x], LOAD(x), temporal, depth);
79 static void denoise_spatial(HQDN3DContext *hqdn3d,
80 uint8_t *src, uint8_t *dst,
81 uint16_t *line_ant, uint16_t *frame_ant,
82 int w, int h, int sstride, int dstride,
83 int16_t *spatial, int16_t *temporal, int depth)
89 spatial += 256 << LUT_BITS;
90 temporal += 256 << LUT_BITS;
92 /* First line has no top neighbor. Only left one for each tmp and
95 for (x = 0; x < w; x++) {
96 line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial, depth);
97 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
101 for (y = 1; y < h; y++) {
105 if (hqdn3d->denoise_row[depth]) {
106 hqdn3d->denoise_row[depth](src, dst, line_ant, frame_ant, w, spatial, temporal);
110 for (x = 0; x < w-1; x++) {
111 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
112 pixel_ant = lowpass(pixel_ant, LOAD(x+1), spatial, depth);
113 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
116 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
117 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
123 static void denoise_depth(HQDN3DContext *hqdn3d,
124 uint8_t *src, uint8_t *dst,
125 uint16_t *line_ant, uint16_t **frame_ant_ptr,
126 int w, int h, int sstride, int dstride,
127 int16_t *spatial, int16_t *temporal, int depth)
129 // FIXME: For 16bit depth, frame_ant could be a pointer to the previous
130 // filtered frame rather than a separate buffer.
132 uint16_t *frame_ant = *frame_ant_ptr;
134 uint8_t *frame_src = src;
135 *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
136 for (y = 0; y < h; y++, src += sstride, frame_ant += w)
137 for (x = 0; x < w; x++)
138 frame_ant[x] = LOAD(x);
140 frame_ant = *frame_ant_ptr;
144 denoise_spatial(hqdn3d, src, dst, line_ant, frame_ant,
145 w, h, sstride, dstride, spatial, temporal, depth);
147 denoise_temporal(src, dst, frame_ant,
148 w, h, sstride, dstride, temporal, depth);
151 #define denoise(...) \
152 switch (hqdn3d->depth) {\
153 case 8: denoise_depth(__VA_ARGS__, 8); break;\
154 case 9: denoise_depth(__VA_ARGS__, 9); break;\
155 case 10: denoise_depth(__VA_ARGS__, 10); break;\
156 case 16: denoise_depth(__VA_ARGS__, 16); break;\
159 static int16_t *precalc_coefs(double dist25, int depth)
162 double gamma, simil, C;
163 int16_t *ct = av_malloc((512<<LUT_BITS)*sizeof(int16_t));
167 gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
169 for (i = -255<<LUT_BITS; i <= 255<<LUT_BITS; i++) {
170 double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
171 simil = 1.0 - FFABS(f) / 255.0;
172 C = pow(simil, gamma) * 256.0 * f;
173 ct[(256<<LUT_BITS)+i] = lrint(C);
180 #define PARAM1_DEFAULT 4.0
181 #define PARAM2_DEFAULT 3.0
182 #define PARAM3_DEFAULT 6.0
184 static int init(AVFilterContext *ctx)
186 HQDN3DContext *hqdn3d = ctx->priv;
188 if (!hqdn3d->strength[LUMA_SPATIAL])
189 hqdn3d->strength[LUMA_SPATIAL] = PARAM1_DEFAULT;
190 if (!hqdn3d->strength[CHROMA_SPATIAL])
191 hqdn3d->strength[CHROMA_SPATIAL] = PARAM2_DEFAULT * hqdn3d->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
192 if (!hqdn3d->strength[LUMA_TMP])
193 hqdn3d->strength[LUMA_TMP] = PARAM3_DEFAULT * hqdn3d->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
194 if (!hqdn3d->strength[CHROMA_TMP])
195 hqdn3d->strength[CHROMA_TMP] = hqdn3d->strength[LUMA_TMP] * hqdn3d->strength[CHROMA_SPATIAL] / hqdn3d->strength[LUMA_SPATIAL];
197 av_log(ctx, AV_LOG_VERBOSE, "ls:%f cs:%f lt:%f ct:%f\n",
198 hqdn3d->strength[LUMA_SPATIAL], hqdn3d->strength[CHROMA_SPATIAL],
199 hqdn3d->strength[LUMA_TMP], hqdn3d->strength[CHROMA_TMP]);
204 static void uninit(AVFilterContext *ctx)
206 HQDN3DContext *hqdn3d = ctx->priv;
208 av_freep(&hqdn3d->coefs[0]);
209 av_freep(&hqdn3d->coefs[1]);
210 av_freep(&hqdn3d->coefs[2]);
211 av_freep(&hqdn3d->coefs[3]);
212 av_freep(&hqdn3d->line);
213 av_freep(&hqdn3d->frame_prev[0]);
214 av_freep(&hqdn3d->frame_prev[1]);
215 av_freep(&hqdn3d->frame_prev[2]);
218 static int query_formats(AVFilterContext *ctx)
220 static const enum AVPixelFormat pix_fmts[] = {
231 AV_NE( AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUV420P9LE ),
232 AV_NE( AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUV422P9LE ),
233 AV_NE( AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUV444P9LE ),
234 AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
235 AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
236 AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
237 AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
238 AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
239 AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
243 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
248 static int config_input(AVFilterLink *inlink)
250 HQDN3DContext *hqdn3d = inlink->dst->priv;
251 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
254 hqdn3d->hsub = desc->log2_chroma_w;
255 hqdn3d->vsub = desc->log2_chroma_h;
256 hqdn3d->depth = desc->comp[0].depth_minus1+1;
258 hqdn3d->line = av_malloc(inlink->w * sizeof(*hqdn3d->line));
260 return AVERROR(ENOMEM);
262 for (i = 0; i < 4; i++) {
263 hqdn3d->coefs[i] = precalc_coefs(hqdn3d->strength[i], hqdn3d->depth);
264 if (!hqdn3d->coefs[i])
265 return AVERROR(ENOMEM);
269 ff_hqdn3d_init_x86(hqdn3d);
274 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
276 HQDN3DContext *hqdn3d = inlink->dst->priv;
277 AVFilterLink *outlink = inlink->dst->outputs[0];
281 if (av_frame_is_writable(in)) {
286 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
289 return AVERROR(ENOMEM);
292 av_frame_copy_props(out, in);
293 out->width = outlink->w;
294 out->height = outlink->h;
297 for (c = 0; c < 3; c++) {
298 denoise(hqdn3d, in->data[c], out->data[c],
299 hqdn3d->line, &hqdn3d->frame_prev[c],
300 in->width >> (!!c * hqdn3d->hsub),
301 in->height >> (!!c * hqdn3d->vsub),
302 in->linesize[c], out->linesize[c],
303 hqdn3d->coefs[c?2:0], hqdn3d->coefs[c?3:1]);
309 return ff_filter_frame(outlink, out);
312 #define OFFSET(x) offsetof(HQDN3DContext, x)
313 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
314 static const AVOption options[] = {
315 { "luma_spatial", "spatial luma strength", OFFSET(strength[LUMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
316 { "chroma_spatial", "spatial chroma strength", OFFSET(strength[CHROMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
317 { "luma_tmp", "temporal luma strength", OFFSET(strength[LUMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
318 { "chroma_tmp", "temporal chroma strength", OFFSET(strength[CHROMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
322 static const AVClass hqdn3d_class = {
323 .class_name = "hqdn3d",
324 .item_name = av_default_item_name,
326 .version = LIBAVUTIL_VERSION_INT,
329 static const AVFilterPad avfilter_vf_hqdn3d_inputs[] = {
332 .type = AVMEDIA_TYPE_VIDEO,
333 .config_props = config_input,
334 .filter_frame = filter_frame,
339 static const AVFilterPad avfilter_vf_hqdn3d_outputs[] = {
342 .type = AVMEDIA_TYPE_VIDEO
347 AVFilter avfilter_vf_hqdn3d = {
349 .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
351 .priv_size = sizeof(HQDN3DContext),
352 .priv_class = &hqdn3d_class,
355 .query_formats = query_formats,
357 .inputs = avfilter_vf_hqdn3d_inputs,
359 .outputs = avfilter_vf_hqdn3d_outputs,