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/pixdesc.h"
30 #include "libavutil/intreadwrite.h"
37 int16_t coefs[4][512*16];
39 uint16_t *frame_prev[3];
44 #define RIGHTSHIFT(a,b) (((a)+(((1<<(b))-1)>>1))>>(b))
45 #define LOAD(x) ((depth==8 ? src[x] : AV_RN16A(src+(x)*2)) << (16-depth))
46 #define STORE(x,val) (depth==8 ? dst[x] = RIGHTSHIFT(val, 16-depth)\
47 : AV_WN16A(dst+(x)*2, RIGHTSHIFT(val, 16-depth)))
49 static inline uint32_t lowpass(int prev, int cur, int16_t *coef)
51 int d = (prev-cur)>>4;
56 static void denoise_temporal(uint8_t *src, uint8_t *dst,
58 int w, int h, int sstride, int dstride,
59 int16_t *temporal, int depth)
66 for (y = 0; y < h; y++) {
67 for (x = 0; x < w; x++) {
68 frame_ant[x] = tmp = lowpass(frame_ant[x], LOAD(x), temporal);
78 static void denoise_spatial(uint8_t *src, uint8_t *dst,
79 uint16_t *line_ant, uint16_t *frame_ant,
80 int w, int h, int sstride, int dstride,
81 int16_t *spatial, int16_t *temporal, int depth)
90 /* First line has no top neighbor. Only left one for each tmp and
93 for (x = 0; x < w; x++) {
94 line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial);
95 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal);
99 for (y = 1; y < h; y++) {
104 for (x = 0; x < w-1; x++) {
105 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial);
106 pixel_ant = lowpass(pixel_ant, LOAD(x+1), spatial);
107 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal);
110 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial);
111 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal);
117 static void denoise_depth(uint8_t *src, uint8_t *dst,
118 uint16_t *line_ant, uint16_t **frame_ant_ptr,
119 int w, int h, int sstride, int dstride,
120 int16_t *spatial, int16_t *temporal, int depth)
123 uint16_t *frame_ant = *frame_ant_ptr;
125 uint8_t *frame_src = src;
126 *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
127 for (y = 0; y < h; y++, src += sstride, frame_ant += w)
128 for (x = 0; x < w; x++)
129 frame_ant[x] = LOAD(x);
131 frame_ant = *frame_ant_ptr;
135 denoise_spatial(src, dst, line_ant, frame_ant,
136 w, h, sstride, dstride, spatial, temporal, depth);
138 denoise_temporal(src, dst, frame_ant,
139 w, h, sstride, dstride, temporal, depth);
142 #define denoise(...) \
143 switch (hqdn3d->depth) {\
144 case 8: denoise_depth(__VA_ARGS__, 8); break;\
145 case 9: denoise_depth(__VA_ARGS__, 9); break;\
146 case 10: denoise_depth(__VA_ARGS__, 10); break;\
149 static void precalc_coefs(int16_t *ct, double dist25)
152 double gamma, simil, C;
154 gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
156 for (i = -255*16; i <= 255*16; i++) {
157 // lowpass() truncates (not rounds) the diff, so +15/32 for the midpoint of the bin.
158 double f = (i + 15.0/32.0) / 16.0;
159 simil = 1.0 - FFABS(f) / 255.0;
160 C = pow(simil, gamma) * 256.0 * f;
161 ct[16*256+i] = lrint(C);
167 #define PARAM1_DEFAULT 4.0
168 #define PARAM2_DEFAULT 3.0
169 #define PARAM3_DEFAULT 6.0
171 static int init(AVFilterContext *ctx, const char *args)
173 HQDN3DContext *hqdn3d = ctx->priv;
174 double lum_spac, lum_tmp, chrom_spac, chrom_tmp;
175 double param1, param2, param3, param4;
177 lum_spac = PARAM1_DEFAULT;
178 chrom_spac = PARAM2_DEFAULT;
179 lum_tmp = PARAM3_DEFAULT;
180 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
183 switch (sscanf(args, "%lf:%lf:%lf:%lf",
184 ¶m1, ¶m2, ¶m3, ¶m4)) {
187 chrom_spac = PARAM2_DEFAULT * param1 / PARAM1_DEFAULT;
188 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
189 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
194 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
195 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
201 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
212 av_log(ctx, AV_LOG_VERBOSE, "ls:%lf cs:%lf lt:%lf ct:%lf\n",
213 lum_spac, chrom_spac, lum_tmp, chrom_tmp);
214 if (lum_spac < 0 || chrom_spac < 0 || isnan(chrom_tmp)) {
215 av_log(ctx, AV_LOG_ERROR,
216 "Invalid negative value for luma or chroma spatial strength, "
217 "or resulting value for chroma temporal strength is nan.\n");
218 return AVERROR(EINVAL);
221 precalc_coefs(hqdn3d->coefs[0], lum_spac);
222 precalc_coefs(hqdn3d->coefs[1], lum_tmp);
223 precalc_coefs(hqdn3d->coefs[2], chrom_spac);
224 precalc_coefs(hqdn3d->coefs[3], chrom_tmp);
229 static void uninit(AVFilterContext *ctx)
231 HQDN3DContext *hqdn3d = ctx->priv;
233 av_freep(&hqdn3d->line);
234 av_freep(&hqdn3d->frame_prev[0]);
235 av_freep(&hqdn3d->frame_prev[1]);
236 av_freep(&hqdn3d->frame_prev[2]);
239 static int query_formats(AVFilterContext *ctx)
241 static const enum PixelFormat pix_fmts[] = {
252 AV_NE( PIX_FMT_YUV420P9BE, PIX_FMT_YUV420P9LE ),
253 AV_NE( PIX_FMT_YUV422P9BE, PIX_FMT_YUV422P9LE ),
254 AV_NE( PIX_FMT_YUV444P9BE, PIX_FMT_YUV444P9LE ),
255 AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
256 AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
257 AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
261 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
266 static int config_input(AVFilterLink *inlink)
268 HQDN3DContext *hqdn3d = inlink->dst->priv;
270 hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
271 hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
272 hqdn3d->depth = av_pix_fmt_descriptors[inlink->format].comp[0].depth_minus1+1;
274 hqdn3d->line = av_malloc(inlink->w * sizeof(*hqdn3d->line));
276 return AVERROR(ENOMEM);
281 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
286 static int end_frame(AVFilterLink *inlink)
288 HQDN3DContext *hqdn3d = inlink->dst->priv;
289 AVFilterLink *outlink = inlink->dst->outputs[0];
290 AVFilterBufferRef *inpic = inlink ->cur_buf;
291 AVFilterBufferRef *outpic = outlink->out_buf;
294 for (c = 0; c < 3; c++) {
295 denoise(inpic->data[c], outpic->data[c],
296 hqdn3d->line, &hqdn3d->frame_prev[c],
297 inpic->video->w >> (!!c * hqdn3d->hsub),
298 inpic->video->h >> (!!c * hqdn3d->vsub),
299 inpic->linesize[c], outpic->linesize[c],
300 hqdn3d->coefs[c?2:0], hqdn3d->coefs[c?3:1]);
303 if ((ret = ff_draw_slice(outlink, 0, inpic->video->h, 1)) < 0 ||
304 (ret = ff_end_frame(outlink)) < 0)
309 AVFilter avfilter_vf_hqdn3d = {
311 .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
313 .priv_size = sizeof(HQDN3DContext),
316 .query_formats = query_formats,
318 .inputs = (const AVFilterPad[]) {{ .name = "default",
319 .type = AVMEDIA_TYPE_VIDEO,
320 .start_frame = ff_inplace_start_frame,
321 .draw_slice = null_draw_slice,
322 .config_props = config_input,
323 .end_frame = end_frame },
326 .outputs = (const AVFilterPad[]) {{ .name = "default",
327 .type = AVMEDIA_TYPE_VIDEO },