2 * Copyright (c) 2003 Daniel Moreno <comac AT comac DOT darktech DOT org>
3 * Copyright (c) 2010 Baptiste Coudurier
5 * This file is part of Libav, ported from MPlayer.
7 * Libav is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with Libav; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * high quality 3d video denoiser, ported from MPlayer
25 * libmpcodecs/vf_hqdn3d.c.
28 #include "libavutil/pixdesc.h"
37 uint16_t *frame_prev[3];
41 static inline uint32_t lowpass(unsigned int prev, unsigned int cur, int *coef)
44 unsigned int d = (dmul+0x10007FF)>>12; // 0x1000 to convert to unsigned, 7FF for rounding
48 static void denoise_temporal(uint8_t *src, uint8_t *dst,
50 int w, int h, int sstride, int dstride,
56 for (y = 0; y < h; y++) {
57 for (x = 0; x < w; x++) {
58 pixel = lowpass(frame_ant[x]<<8, src[x]<<16, temporal);
59 frame_ant[x] = ((pixel+0x1000007F)>>8);
60 dst[x]= ((pixel+0x10007FFF)>>16);
68 static void denoise_spatial(uint8_t *src, uint8_t *dst,
70 int w, int h, int sstride, int dstride,
71 int *horizontal, int *vertical)
74 long sline_offs = 0, dline_offs = 0;
78 /* First pixel has no left nor top neighbor. */
79 pixel = line_ant[0] = pixel_ant = src[0]<<16;
80 dst[0]= ((pixel+0x10007FFF)>>16);
82 /* First line has no top neighbor, only left. */
83 for (x = 1; x < w; x++) {
84 pixel = line_ant[x] = lowpass(pixel_ant, src[x]<<16, horizontal);
85 dst[x]= ((pixel+0x10007FFF)>>16);
88 for (y = 1; y < h; y++) {
90 sline_offs += sstride, dline_offs += dstride;
91 /* First pixel on each line doesn't have previous pixel */
92 pixel_ant = src[sline_offs]<<16;
93 pixel = line_ant[0] = lowpass(line_ant[0], pixel_ant, vertical);
94 dst[dline_offs]= ((pixel+0x10007FFF)>>16);
96 for (x = 1; x < w; x++) {
98 /* The rest are normal */
99 pixel_ant = lowpass(pixel_ant, src[sline_offs+x]<<16, horizontal);
100 pixel = line_ant[x] = lowpass(line_ant[x], pixel_ant, vertical);
101 dst[dline_offs+x]= ((pixel+0x10007FFF)>>16);
106 static void denoise(uint8_t *src, uint8_t *dst,
107 uint32_t *line_ant, uint16_t **frame_ant_ptr,
108 int w, int h, int sstride, int dstride,
109 int *horizontal, int *vertical, int *temporal)
112 long sline_offs = 0, dline_offs = 0;
115 uint16_t *frame_ant = *frame_ant_ptr;
118 *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
119 for (y = 0; y < h; y++) {
120 uint16_t *frame_dst = frame_ant+y*w;
121 uint8_t *frame_src = src+y*sstride;
122 for (x = 0; x < w; x++)
123 frame_dst[x] = frame_src[x]<<8;
127 if (!horizontal[0] && !vertical[0]) {
128 denoise_temporal(src, dst, frame_ant,
129 w, h, sstride, dstride, temporal);
133 denoise_spatial(src, dst, line_ant,
134 w, h, sstride, dstride, horizontal, vertical);
138 /* First pixel has no left nor top neighbor. Only previous frame */
139 line_ant[0] = pixel_ant = src[0]<<16;
140 pixel = lowpass(frame_ant[0]<<8, pixel_ant, temporal);
141 frame_ant[0] = ((pixel+0x1000007F)>>8);
142 dst[0]= ((pixel+0x10007FFF)>>16);
144 /* First line has no top neighbor. Only left one for each pixel and
146 for (x = 1; x < w; x++) {
147 line_ant[x] = pixel_ant = lowpass(pixel_ant, src[x]<<16, horizontal);
148 pixel = lowpass(frame_ant[x]<<8, pixel_ant, temporal);
149 frame_ant[x] = ((pixel+0x1000007F)>>8);
150 dst[x]= ((pixel+0x10007FFF)>>16);
153 for (y = 1; y < h; y++) {
155 uint16_t *line_prev = frame_ant+y*w;
156 sline_offs += sstride, dline_offs += dstride;
157 /* First pixel on each line doesn't have previous pixel */
158 pixel_ant = src[sline_offs]<<16;
159 line_ant[0] = lowpass(line_ant[0], pixel_ant, vertical);
160 pixel = lowpass(line_prev[0]<<8, line_ant[0], temporal);
161 line_prev[0] = ((pixel+0x1000007F)>>8);
162 dst[dline_offs]= ((pixel+0x10007FFF)>>16);
164 for (x = 1; x < w; x++) {
166 /* The rest are normal */
167 pixel_ant = lowpass(pixel_ant, src[sline_offs+x]<<16, horizontal);
168 line_ant[x] = lowpass(line_ant[x], pixel_ant, vertical);
169 pixel = lowpass(line_prev[x]<<8, line_ant[x], temporal);
170 line_prev[x] = ((pixel+0x1000007F)>>8);
171 dst[dline_offs+x]= ((pixel+0x10007FFF)>>16);
176 static void precalc_coefs(int *ct, double dist25)
179 double gamma, simil, C;
181 gamma = log(0.25) / log(1.0 - dist25/255.0 - 0.00001);
183 for (i = -255*16; i <= 255*16; i++) {
184 simil = 1.0 - FFABS(i) / (16*255.0);
185 C = pow(simil, gamma) * 65536.0 * i / 16.0;
186 ct[16*256+i] = lrint(C);
192 #define PARAM1_DEFAULT 4.0
193 #define PARAM2_DEFAULT 3.0
194 #define PARAM3_DEFAULT 6.0
196 static int init(AVFilterContext *ctx, const char *args)
198 HQDN3DContext *hqdn3d = ctx->priv;
199 double lum_spac, lum_tmp, chrom_spac, chrom_tmp;
200 double param1, param2, param3, param4;
202 lum_spac = PARAM1_DEFAULT;
203 chrom_spac = PARAM2_DEFAULT;
204 lum_tmp = PARAM3_DEFAULT;
205 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
208 switch (sscanf(args, "%lf:%lf:%lf:%lf",
209 ¶m1, ¶m2, ¶m3, ¶m4)) {
212 chrom_spac = PARAM2_DEFAULT * param1 / PARAM1_DEFAULT;
213 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
214 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
219 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
220 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
226 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
237 av_log(ctx, AV_LOG_VERBOSE, "ls:%lf cs:%lf lt:%lf ct:%lf\n",
238 lum_spac, chrom_spac, lum_tmp, chrom_tmp);
239 if (lum_spac < 0 || chrom_spac < 0 || isnan(chrom_tmp)) {
240 av_log(ctx, AV_LOG_ERROR,
241 "Invalid negative value for luma or chroma spatial strength, "
242 "or resulting value for chroma temporal strength is nan.\n");
243 return AVERROR(EINVAL);
246 precalc_coefs(hqdn3d->coefs[0], lum_spac);
247 precalc_coefs(hqdn3d->coefs[1], lum_tmp);
248 precalc_coefs(hqdn3d->coefs[2], chrom_spac);
249 precalc_coefs(hqdn3d->coefs[3], chrom_tmp);
254 static void uninit(AVFilterContext *ctx)
256 HQDN3DContext *hqdn3d = ctx->priv;
258 av_freep(&hqdn3d->line);
259 av_freep(&hqdn3d->frame_prev[0]);
260 av_freep(&hqdn3d->frame_prev[1]);
261 av_freep(&hqdn3d->frame_prev[2]);
264 static int query_formats(AVFilterContext *ctx)
266 static const enum PixelFormat pix_fmts[] = {
267 PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_NONE
270 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
275 static int config_input(AVFilterLink *inlink)
277 HQDN3DContext *hqdn3d = inlink->dst->priv;
279 hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
280 hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
282 hqdn3d->line = av_malloc(inlink->w * sizeof(*hqdn3d->line));
284 return AVERROR(ENOMEM);
289 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
294 static int end_frame(AVFilterLink *inlink)
296 HQDN3DContext *hqdn3d = inlink->dst->priv;
297 AVFilterLink *outlink = inlink->dst->outputs[0];
298 AVFilterBufferRef *inpic = inlink ->cur_buf;
299 AVFilterBufferRef *outpic = outlink->out_buf;
300 int cw = inpic->video->w >> hqdn3d->hsub;
301 int ch = inpic->video->h >> hqdn3d->vsub;
304 denoise(inpic->data[0], outpic->data[0],
305 hqdn3d->line, &hqdn3d->frame_prev[0], inpic->video->w, inpic->video->h,
306 inpic->linesize[0], outpic->linesize[0],
310 denoise(inpic->data[1], outpic->data[1],
311 hqdn3d->line, &hqdn3d->frame_prev[1], cw, ch,
312 inpic->linesize[1], outpic->linesize[1],
316 denoise(inpic->data[2], outpic->data[2],
317 hqdn3d->line, &hqdn3d->frame_prev[2], cw, ch,
318 inpic->linesize[2], outpic->linesize[2],
323 if ((ret = ff_draw_slice(outlink, 0, inpic->video->h, 1)) < 0 ||
324 (ret = ff_end_frame(outlink)) < 0)
329 AVFilter avfilter_vf_hqdn3d = {
331 .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
333 .priv_size = sizeof(HQDN3DContext),
336 .query_formats = query_formats,
338 .inputs = (const AVFilterPad[]) {{ .name = "default",
339 .type = AVMEDIA_TYPE_VIDEO,
340 .draw_slice = null_draw_slice,
341 .config_props = config_input,
342 .end_frame = end_frame },
345 .outputs = (const AVFilterPad[]) {{ .name = "default",
346 .type = AVMEDIA_TYPE_VIDEO },