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 FFmpeg, ported from MPlayer.
8 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
38 int16_t coefs[4][512*16];
40 uint16_t *frame_prev[3];
45 #define RIGHTSHIFT(a,b) (((a)+(((1<<(b))-1)>>1))>>(b))
46 #define LOAD(x) ((depth==8 ? src[x] : AV_RN16A(src+(x)*2)) << (16-depth))
47 #define STORE(x,val) (depth==8 ? dst[x] = RIGHTSHIFT(val, 16-depth)\
48 : AV_WN16A(dst+(x)*2, RIGHTSHIFT(val, 16-depth)))
50 static inline uint32_t lowpass(int prev, int cur, int16_t *coef)
52 int d = (prev-cur)>>4;
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)
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);
79 static void denoise_spatial(uint8_t *src, uint8_t *dst,
80 uint16_t *line_ant, uint16_t *frame_ant,
81 int w, int h, int sstride, int dstride,
82 int16_t *spatial, int16_t *temporal, int depth)
91 /* First line has no top neighbor. Only left one for each tmp and
94 for (x = 0; x < w; x++) {
95 line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial);
96 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal);
100 for (y = 1; y < h; y++) {
105 for (x = 0; x < w-1; x++) {
106 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial);
107 pixel_ant = lowpass(pixel_ant, LOAD(x+1), spatial);
108 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal);
111 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial);
112 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal);
118 static void denoise_depth(uint8_t *src, uint8_t *dst,
119 uint16_t *line_ant, uint16_t **frame_ant_ptr,
120 int w, int h, int sstride, int dstride,
121 int16_t *spatial, int16_t *temporal, int depth)
124 uint16_t *frame_ant = *frame_ant_ptr;
126 uint8_t *frame_src = src;
127 *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
128 for (y = 0; y < h; y++, src += sstride, frame_ant += w)
129 for (x = 0; x < w; x++)
130 frame_ant[x] = LOAD(x);
132 frame_ant = *frame_ant_ptr;
136 denoise_spatial(src, dst, line_ant, frame_ant,
137 w, h, sstride, dstride, spatial, temporal, depth);
139 denoise_temporal(src, dst, frame_ant,
140 w, h, sstride, dstride, temporal, depth);
143 #define denoise(...) \
144 switch (hqdn3d->depth) {\
145 case 8: denoise_depth(__VA_ARGS__, 8); break;\
146 case 9: denoise_depth(__VA_ARGS__, 9); break;\
147 case 10: denoise_depth(__VA_ARGS__, 10); break;\
150 static void precalc_coefs(int16_t *ct, double dist25)
153 double gamma, simil, C;
155 gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
157 for (i = -255*16; i <= 255*16; i++) {
158 // lowpass() truncates (not rounds) the diff, so +15/32 for the midpoint of the bin.
159 double f = (i + 15.0/32.0) / 16.0;
160 simil = 1.0 - FFABS(f) / 255.0;
161 C = pow(simil, gamma) * 256.0 * f;
162 ct[16*256+i] = lrint(C);
168 #define PARAM1_DEFAULT 4.0
169 #define PARAM2_DEFAULT 3.0
170 #define PARAM3_DEFAULT 6.0
172 static int init(AVFilterContext *ctx, const char *args)
174 HQDN3DContext *hqdn3d = ctx->priv;
175 double lum_spac, lum_tmp, chrom_spac, chrom_tmp;
176 double param1, param2, param3, param4;
178 lum_spac = PARAM1_DEFAULT;
179 chrom_spac = PARAM2_DEFAULT;
180 lum_tmp = PARAM3_DEFAULT;
181 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
184 switch (sscanf(args, "%lf:%lf:%lf:%lf",
185 ¶m1, ¶m2, ¶m3, ¶m4)) {
188 chrom_spac = PARAM2_DEFAULT * param1 / PARAM1_DEFAULT;
189 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
190 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
195 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
196 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
202 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
213 av_log(ctx, AV_LOG_VERBOSE, "ls:%lf cs:%lf lt:%lf ct:%lf\n",
214 lum_spac, chrom_spac, lum_tmp, chrom_tmp);
215 if (lum_spac < 0 || chrom_spac < 0 || isnan(chrom_tmp)) {
216 av_log(ctx, AV_LOG_ERROR,
217 "Invalid negative value for luma or chroma spatial strength, "
218 "or resulting value for chroma temporal strength is nan.\n");
219 return AVERROR(EINVAL);
222 precalc_coefs(hqdn3d->coefs[0], lum_spac);
223 precalc_coefs(hqdn3d->coefs[1], lum_tmp);
224 precalc_coefs(hqdn3d->coefs[2], chrom_spac);
225 precalc_coefs(hqdn3d->coefs[3], chrom_tmp);
230 static void uninit(AVFilterContext *ctx)
232 HQDN3DContext *hqdn3d = ctx->priv;
234 av_freep(&hqdn3d->line);
235 av_freep(&hqdn3d->frame_prev[0]);
236 av_freep(&hqdn3d->frame_prev[1]);
237 av_freep(&hqdn3d->frame_prev[2]);
240 static int query_formats(AVFilterContext *ctx)
242 static const enum PixelFormat pix_fmts[] = {
253 AV_NE( PIX_FMT_YUV420P9BE, PIX_FMT_YUV420P9LE ),
254 AV_NE( PIX_FMT_YUV422P9BE, PIX_FMT_YUV422P9LE ),
255 AV_NE( PIX_FMT_YUV444P9BE, PIX_FMT_YUV444P9LE ),
256 AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
257 AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
258 AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
262 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
267 static int config_input(AVFilterLink *inlink)
269 HQDN3DContext *hqdn3d = inlink->dst->priv;
271 hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
272 hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
273 hqdn3d->depth = av_pix_fmt_descriptors[inlink->format].comp[0].depth_minus1+1;
275 hqdn3d->line = av_malloc(inlink->w * sizeof(*hqdn3d->line));
277 return AVERROR(ENOMEM);
282 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
287 static int end_frame(AVFilterLink *inlink)
289 HQDN3DContext *hqdn3d = inlink->dst->priv;
290 AVFilterLink *outlink = inlink->dst->outputs[0];
291 AVFilterBufferRef *inpic = inlink ->cur_buf;
292 AVFilterBufferRef *outpic = outlink->out_buf;
295 for (c = 0; c < 3; c++) {
296 denoise(inpic->data[c], outpic->data[c],
297 hqdn3d->line, &hqdn3d->frame_prev[c],
298 inpic->video->w >> (!!c * hqdn3d->hsub),
299 inpic->video->h >> (!!c * hqdn3d->vsub),
300 inpic->linesize[c], outpic->linesize[c],
301 hqdn3d->coefs[c?2:0], hqdn3d->coefs[c?3:1]);
304 if ((ret = ff_draw_slice(outlink, 0, inpic->video->h, 1)) < 0 ||
305 (ret = ff_end_frame(outlink)) < 0)
310 AVFilter avfilter_vf_hqdn3d = {
312 .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
314 .priv_size = sizeof(HQDN3DContext),
317 .query_formats = query_formats,
319 .inputs = (const AVFilterPad[]) {{ .name = "default",
320 .type = AVMEDIA_TYPE_VIDEO,
321 .start_frame = ff_inplace_start_frame,
322 .draw_slice = null_draw_slice,
323 .config_props = config_input,
324 .end_frame = end_frame },
327 .outputs = (const AVFilterPad[]) {{ .name = "default",
328 .type = AVMEDIA_TYPE_VIDEO },