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.
29 #include "libavutil/common.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/intreadwrite.h"
40 uint16_t *frame_prev[3];
46 #define LUT_BITS (depth==16 ? 8 : 4)
47 #define RIGHTSHIFT(a,b) (((a)+(((1<<(b))-1)>>1))>>(b))
48 #define LOAD(x) ((depth==8 ? src[x] : AV_RN16A(src+(x)*2)) << (16-depth))
49 #define STORE(x,val) (depth==8 ? dst[x] = RIGHTSHIFT(val, 16-depth)\
50 : AV_WN16A(dst+(x)*2, RIGHTSHIFT(val, 16-depth)))
53 static inline uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
55 int d = (prev - cur) >> (8 - LUT_BITS);
60 static void denoise_temporal(uint8_t *src, uint8_t *dst,
62 int w, int h, int sstride, int dstride,
63 int16_t *temporal, int depth)
68 temporal += 256 << LUT_BITS;
70 for (y = 0; y < h; y++) {
71 for (x = 0; x < w; x++) {
72 frame_ant[x] = tmp = lowpass(frame_ant[x], LOAD(x), temporal, depth);
82 static void denoise_spatial(uint8_t *src, uint8_t *dst,
83 uint16_t *line_ant, uint16_t *frame_ant,
84 int w, int h, int sstride, int dstride,
85 int16_t *spatial, int16_t *temporal, int depth)
91 spatial += 256 << LUT_BITS;
92 temporal += 256 << LUT_BITS;
94 /* First line has no top neighbor. Only left one for each tmp and
97 for (x = 0; x < w; x++) {
98 line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial, depth);
99 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
103 for (y = 1; y < h; y++) {
108 for (x = 0; x < w-1; x++) {
109 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
110 pixel_ant = lowpass(pixel_ant, LOAD(x+1), spatial, depth);
111 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
114 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
115 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
121 static void denoise_depth(uint8_t *src, uint8_t *dst,
122 uint16_t *line_ant, uint16_t **frame_ant_ptr,
123 int w, int h, int sstride, int dstride,
124 int16_t *spatial, int16_t *temporal, int depth)
126 // FIXME: For 16bit depth, frame_ant could be a pointer to the previous
127 // filtered frame rather than a separate buffer.
129 uint16_t *frame_ant = *frame_ant_ptr;
131 uint8_t *frame_src = src;
132 *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
133 for (y = 0; y < h; y++, src += sstride, frame_ant += w)
134 for (x = 0; x < w; x++)
135 frame_ant[x] = LOAD(x);
137 frame_ant = *frame_ant_ptr;
141 denoise_spatial(src, dst, line_ant, frame_ant,
142 w, h, sstride, dstride, spatial, temporal, depth);
144 denoise_temporal(src, dst, frame_ant,
145 w, h, sstride, dstride, temporal, depth);
148 #define denoise(...) \
149 switch (hqdn3d->depth) {\
150 case 8: denoise_depth(__VA_ARGS__, 8); break;\
151 case 9: denoise_depth(__VA_ARGS__, 9); break;\
152 case 10: denoise_depth(__VA_ARGS__, 10); break;\
153 case 16: denoise_depth(__VA_ARGS__, 16); break;\
156 static int16_t *precalc_coefs(double dist25, int depth)
159 double gamma, simil, C;
160 int16_t *ct = av_malloc((512<<LUT_BITS)*sizeof(int16_t));
164 gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
166 for (i = -255<<LUT_BITS; i <= 255<<LUT_BITS; i++) {
167 double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
168 simil = 1.0 - FFABS(f) / 255.0;
169 C = pow(simil, gamma) * 256.0 * f;
170 ct[(256<<LUT_BITS)+i] = lrint(C);
177 #define PARAM1_DEFAULT 4.0
178 #define PARAM2_DEFAULT 3.0
179 #define PARAM3_DEFAULT 6.0
181 static int init(AVFilterContext *ctx, const char *args)
183 HQDN3DContext *hqdn3d = ctx->priv;
184 double lum_spac, lum_tmp, chrom_spac, chrom_tmp;
185 double param1, param2, param3, param4;
187 lum_spac = PARAM1_DEFAULT;
188 chrom_spac = PARAM2_DEFAULT;
189 lum_tmp = PARAM3_DEFAULT;
190 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
193 switch (sscanf(args, "%lf:%lf:%lf:%lf",
194 ¶m1, ¶m2, ¶m3, ¶m4)) {
197 chrom_spac = PARAM2_DEFAULT * param1 / PARAM1_DEFAULT;
198 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
199 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
204 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
205 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
211 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
222 hqdn3d->strength[0] = lum_spac;
223 hqdn3d->strength[1] = lum_tmp;
224 hqdn3d->strength[2] = chrom_spac;
225 hqdn3d->strength[3] = chrom_tmp;
227 av_log(ctx, AV_LOG_VERBOSE, "ls:%lf cs:%lf lt:%lf ct:%lf\n",
228 lum_spac, chrom_spac, lum_tmp, chrom_tmp);
229 if (lum_spac < 0 || chrom_spac < 0 || isnan(chrom_tmp)) {
230 av_log(ctx, AV_LOG_ERROR,
231 "Invalid negative value for luma or chroma spatial strength, "
232 "or resulting value for chroma temporal strength is nan.\n");
233 return AVERROR(EINVAL);
239 static void uninit(AVFilterContext *ctx)
241 HQDN3DContext *hqdn3d = ctx->priv;
243 av_freep(&hqdn3d->coefs[0]);
244 av_freep(&hqdn3d->coefs[1]);
245 av_freep(&hqdn3d->coefs[2]);
246 av_freep(&hqdn3d->coefs[3]);
247 av_freep(&hqdn3d->line);
248 av_freep(&hqdn3d->frame_prev[0]);
249 av_freep(&hqdn3d->frame_prev[1]);
250 av_freep(&hqdn3d->frame_prev[2]);
253 static int query_formats(AVFilterContext *ctx)
255 static const enum PixelFormat pix_fmts[] = {
266 AV_NE( PIX_FMT_YUV420P9BE, PIX_FMT_YUV420P9LE ),
267 AV_NE( PIX_FMT_YUV422P9BE, PIX_FMT_YUV422P9LE ),
268 AV_NE( PIX_FMT_YUV444P9BE, PIX_FMT_YUV444P9LE ),
269 AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
270 AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
271 AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
272 AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
273 AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
274 AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
278 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
283 static int config_input(AVFilterLink *inlink)
285 HQDN3DContext *hqdn3d = inlink->dst->priv;
287 hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
288 hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
289 hqdn3d->depth = av_pix_fmt_descriptors[inlink->format].comp[0].depth_minus1+1;
291 hqdn3d->line = av_malloc(inlink->w * sizeof(*hqdn3d->line));
293 return AVERROR(ENOMEM);
295 for (int i=0; i<4; i++) {
296 hqdn3d->coefs[i] = precalc_coefs(hqdn3d->strength[i], hqdn3d->depth);
297 if (!hqdn3d->coefs[i])
298 return AVERROR(ENOMEM);
304 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
309 static int end_frame(AVFilterLink *inlink)
311 HQDN3DContext *hqdn3d = inlink->dst->priv;
312 AVFilterLink *outlink = inlink->dst->outputs[0];
313 AVFilterBufferRef *inpic = inlink ->cur_buf;
314 AVFilterBufferRef *outpic = outlink->out_buf;
317 for (c = 0; c < 3; c++) {
318 denoise(inpic->data[c], outpic->data[c],
319 hqdn3d->line, &hqdn3d->frame_prev[c],
320 inpic->video->w >> (!!c * hqdn3d->hsub),
321 inpic->video->h >> (!!c * hqdn3d->vsub),
322 inpic->linesize[c], outpic->linesize[c],
323 hqdn3d->coefs[c?2:0], hqdn3d->coefs[c?3:1]);
326 if ((ret = ff_draw_slice(outlink, 0, inpic->video->h, 1)) < 0 ||
327 (ret = ff_end_frame(outlink)) < 0)
332 AVFilter avfilter_vf_hqdn3d = {
334 .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
336 .priv_size = sizeof(HQDN3DContext),
339 .query_formats = query_formats,
341 .inputs = (const AVFilterPad[]) {{ .name = "default",
342 .type = AVMEDIA_TYPE_VIDEO,
343 .start_frame = ff_inplace_start_frame,
344 .draw_slice = null_draw_slice,
345 .config_props = config_input,
346 .end_frame = end_frame },
349 .outputs = (const AVFilterPad[]) {{ .name = "default",
350 .type = AVMEDIA_TYPE_VIDEO },