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"
36 int16_t coefs[4][512*16];
38 uint16_t *frame_prev[3];
42 static inline uint32_t lowpass(int prev, int cur, int16_t *coef)
44 int d = (prev-cur)>>4;
48 static void denoise_temporal(uint8_t *src, uint8_t *dst,
50 int w, int h, int sstride, int dstride,
58 for (y = 0; y < h; y++) {
59 for (x = 0; x < w; x++) {
60 frame_ant[x] = tmp = lowpass(frame_ant[x], src[x]<<8, temporal);
61 dst[x] = (tmp+0x7F)>>8;
69 static void denoise_spatial(uint8_t *src, uint8_t *dst,
70 uint16_t *line_ant, uint16_t *frame_ant,
71 int w, int h, int sstride, int dstride,
72 int16_t *spatial, int16_t *temporal)
81 /* First line has no top neighbor. Only left one for each tmp and
83 pixel_ant = src[0]<<8;
84 for (x = 0; x < w; x++) {
85 line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, src[x]<<8, spatial);
86 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal);
87 dst[x] = (tmp+0x7F)>>8;
90 for (y = 1; y < h; y++) {
94 pixel_ant = src[0]<<8;
95 for (x = 0; x < w-1; x++) {
96 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial);
97 pixel_ant = lowpass(pixel_ant, src[x+1]<<8, spatial);
98 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal);
99 dst[x] = (tmp+0x7F)>>8;
101 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial);
102 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal);
103 dst[x] = (tmp+0x7F)>>8;
107 static void denoise(uint8_t *src, uint8_t *dst,
108 uint16_t *line_ant, uint16_t **frame_ant_ptr,
109 int w, int h, int sstride, int dstride,
110 int16_t *spatial, int16_t *temporal)
113 uint16_t *frame_ant = *frame_ant_ptr;
115 uint8_t *frame_src = src;
116 *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
117 for (y = 0; y < h; y++, src += sstride, frame_ant += w)
118 for (x = 0; x < w; x++)
119 frame_ant[x] = src[x]<<8;
121 frame_ant = *frame_ant_ptr;
125 denoise_spatial(src, dst, line_ant, frame_ant,
126 w, h, sstride, dstride, spatial, temporal);
128 denoise_temporal(src, dst, frame_ant,
129 w, h, sstride, dstride, temporal);
132 static void precalc_coefs(int16_t *ct, double dist25)
135 double gamma, simil, C;
137 gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
139 for (i = -255*16; i <= 255*16; i++) {
140 // lowpass() truncates (not rounds) the diff, so +15/32 for the midpoint of the bin.
141 double f = (i + 15.0/32.0) / 16.0;
142 simil = 1.0 - FFABS(f) / 255.0;
143 C = pow(simil, gamma) * 256.0 * f;
144 ct[16*256+i] = lrint(C);
150 #define PARAM1_DEFAULT 4.0
151 #define PARAM2_DEFAULT 3.0
152 #define PARAM3_DEFAULT 6.0
154 static int init(AVFilterContext *ctx, const char *args)
156 HQDN3DContext *hqdn3d = ctx->priv;
157 double lum_spac, lum_tmp, chrom_spac, chrom_tmp;
158 double param1, param2, param3, param4;
160 lum_spac = PARAM1_DEFAULT;
161 chrom_spac = PARAM2_DEFAULT;
162 lum_tmp = PARAM3_DEFAULT;
163 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
166 switch (sscanf(args, "%lf:%lf:%lf:%lf",
167 ¶m1, ¶m2, ¶m3, ¶m4)) {
170 chrom_spac = PARAM2_DEFAULT * param1 / PARAM1_DEFAULT;
171 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
172 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
177 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
178 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
184 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
195 av_log(ctx, AV_LOG_VERBOSE, "ls:%lf cs:%lf lt:%lf ct:%lf\n",
196 lum_spac, chrom_spac, lum_tmp, chrom_tmp);
197 if (lum_spac < 0 || chrom_spac < 0 || isnan(chrom_tmp)) {
198 av_log(ctx, AV_LOG_ERROR,
199 "Invalid negative value for luma or chroma spatial strength, "
200 "or resulting value for chroma temporal strength is nan.\n");
201 return AVERROR(EINVAL);
204 precalc_coefs(hqdn3d->coefs[0], lum_spac);
205 precalc_coefs(hqdn3d->coefs[1], lum_tmp);
206 precalc_coefs(hqdn3d->coefs[2], chrom_spac);
207 precalc_coefs(hqdn3d->coefs[3], chrom_tmp);
212 static void uninit(AVFilterContext *ctx)
214 HQDN3DContext *hqdn3d = ctx->priv;
216 av_freep(&hqdn3d->line);
217 av_freep(&hqdn3d->frame_prev[0]);
218 av_freep(&hqdn3d->frame_prev[1]);
219 av_freep(&hqdn3d->frame_prev[2]);
222 static int query_formats(AVFilterContext *ctx)
224 static const enum PixelFormat pix_fmts[] = {
225 PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_NONE
228 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
233 static int config_input(AVFilterLink *inlink)
235 HQDN3DContext *hqdn3d = inlink->dst->priv;
237 hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
238 hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
240 hqdn3d->line = av_malloc(inlink->w * sizeof(*hqdn3d->line));
242 return AVERROR(ENOMEM);
247 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
252 static int end_frame(AVFilterLink *inlink)
254 HQDN3DContext *hqdn3d = inlink->dst->priv;
255 AVFilterLink *outlink = inlink->dst->outputs[0];
256 AVFilterBufferRef *inpic = inlink ->cur_buf;
257 AVFilterBufferRef *outpic = outlink->out_buf;
260 for (c = 0; c < 3; c++) {
261 denoise(inpic->data[c], outpic->data[c],
262 hqdn3d->line, &hqdn3d->frame_prev[c],
263 inpic->video->w >> (!!c * hqdn3d->hsub),
264 inpic->video->h >> (!!c * hqdn3d->vsub),
265 inpic->linesize[c], outpic->linesize[c],
266 hqdn3d->coefs[c?2:0], hqdn3d->coefs[c?3:1]);
269 if ((ret = ff_draw_slice(outlink, 0, inpic->video->h, 1)) < 0 ||
270 (ret = ff_end_frame(outlink)) < 0)
275 AVFilter avfilter_vf_hqdn3d = {
277 .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
279 .priv_size = sizeof(HQDN3DContext),
282 .query_formats = query_formats,
284 .inputs = (const AVFilterPad[]) {{ .name = "default",
285 .type = AVMEDIA_TYPE_VIDEO,
286 .start_frame = ff_inplace_start_frame,
287 .draw_slice = null_draw_slice,
288 .config_props = config_input,
289 .end_frame = end_frame },
292 .outputs = (const AVFilterPad[]) {{ .name = "default",
293 .type = AVMEDIA_TYPE_VIDEO },