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.
30 #include "libavutil/common.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/intreadwrite.h"
37 #include "vf_hqdn3d.h"
39 #define LUT_BITS (depth==16 ? 8 : 4)
40 #define LOAD(x) (((depth==8 ? src[x] : AV_RN16A(src+(x)*2)) << (16-depth)) + (((1<<(16-depth))-1)>>1))
41 #define STORE(x,val) (depth==8 ? dst[x] = (val) >> (16-depth)\
42 : AV_WN16A(dst+(x)*2, (val) >> (16-depth)))
45 static uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
47 int d = (prev - cur) >> (8 - LUT_BITS);
52 static void denoise_temporal(uint8_t *src, uint8_t *dst,
54 int w, int h, int sstride, int dstride,
55 int16_t *temporal, int depth)
60 temporal += 256 << LUT_BITS;
62 for (y = 0; y < h; y++) {
63 for (x = 0; x < w; x++) {
64 frame_ant[x] = tmp = lowpass(frame_ant[x], LOAD(x), temporal, depth);
74 static void denoise_spatial(HQDN3DContext *hqdn3d,
75 uint8_t *src, uint8_t *dst,
76 uint16_t *line_ant, uint16_t *frame_ant,
77 int w, int h, int sstride, int dstride,
78 int16_t *spatial, int16_t *temporal, int depth)
84 spatial += 256 << LUT_BITS;
85 temporal += 256 << LUT_BITS;
87 /* First line has no top neighbor. Only left one for each tmp and
90 for (x = 0; x < w; x++) {
91 line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial, depth);
92 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
96 for (y = 1; y < h; y++) {
100 if (hqdn3d->denoise_row[depth]) {
101 hqdn3d->denoise_row[depth](src, dst, line_ant, frame_ant, w, spatial, temporal);
105 for (x = 0; x < w-1; x++) {
106 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
107 pixel_ant = lowpass(pixel_ant, LOAD(x+1), spatial, depth);
108 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
111 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
112 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
118 static void denoise_depth(HQDN3DContext *hqdn3d,
119 uint8_t *src, uint8_t *dst,
120 uint16_t *line_ant, uint16_t **frame_ant_ptr,
121 int w, int h, int sstride, int dstride,
122 int16_t *spatial, int16_t *temporal, int depth)
124 // FIXME: For 16bit depth, frame_ant could be a pointer to the previous
125 // filtered frame rather than a separate buffer.
127 uint16_t *frame_ant = *frame_ant_ptr;
129 uint8_t *frame_src = src;
130 *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
131 for (y = 0; y < h; y++, src += sstride, frame_ant += w)
132 for (x = 0; x < w; x++)
133 frame_ant[x] = LOAD(x);
135 frame_ant = *frame_ant_ptr;
139 denoise_spatial(hqdn3d, src, dst, line_ant, frame_ant,
140 w, h, sstride, dstride, spatial, temporal, depth);
142 denoise_temporal(src, dst, frame_ant,
143 w, h, sstride, dstride, temporal, depth);
146 #define denoise(...) \
147 switch (hqdn3d->depth) {\
148 case 8: denoise_depth(__VA_ARGS__, 8); break;\
149 case 9: denoise_depth(__VA_ARGS__, 9); break;\
150 case 10: denoise_depth(__VA_ARGS__, 10); break;\
151 case 16: denoise_depth(__VA_ARGS__, 16); break;\
154 static int16_t *precalc_coefs(double dist25, int depth)
157 double gamma, simil, C;
158 int16_t *ct = av_malloc((512<<LUT_BITS)*sizeof(int16_t));
162 gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
164 for (i = -255<<LUT_BITS; i <= 255<<LUT_BITS; i++) {
165 double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
166 simil = 1.0 - FFABS(f) / 255.0;
167 C = pow(simil, gamma) * 256.0 * f;
168 ct[(256<<LUT_BITS)+i] = lrint(C);
175 #define PARAM1_DEFAULT 4.0
176 #define PARAM2_DEFAULT 3.0
177 #define PARAM3_DEFAULT 6.0
179 static int init(AVFilterContext *ctx, const char *args)
181 HQDN3DContext *hqdn3d = ctx->priv;
182 double lum_spac, lum_tmp, chrom_spac, chrom_tmp;
183 double param1, param2, param3, param4;
185 lum_spac = PARAM1_DEFAULT;
186 chrom_spac = PARAM2_DEFAULT;
187 lum_tmp = PARAM3_DEFAULT;
188 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
191 switch (sscanf(args, "%lf:%lf:%lf:%lf",
192 ¶m1, ¶m2, ¶m3, ¶m4)) {
195 chrom_spac = PARAM2_DEFAULT * param1 / PARAM1_DEFAULT;
196 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
197 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
202 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
203 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
209 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
220 hqdn3d->strength[0] = lum_spac;
221 hqdn3d->strength[1] = lum_tmp;
222 hqdn3d->strength[2] = chrom_spac;
223 hqdn3d->strength[3] = chrom_tmp;
225 av_log(ctx, AV_LOG_VERBOSE, "ls:%f cs:%f lt:%f ct:%f\n",
226 lum_spac, chrom_spac, lum_tmp, chrom_tmp);
227 if (lum_spac < 0 || chrom_spac < 0 || isnan(chrom_tmp)) {
228 av_log(ctx, AV_LOG_ERROR,
229 "Invalid negative value for luma or chroma spatial strength, "
230 "or resulting value for chroma temporal strength is nan.\n");
231 return AVERROR(EINVAL);
237 static void uninit(AVFilterContext *ctx)
239 HQDN3DContext *hqdn3d = ctx->priv;
241 av_freep(&hqdn3d->coefs[0]);
242 av_freep(&hqdn3d->coefs[1]);
243 av_freep(&hqdn3d->coefs[2]);
244 av_freep(&hqdn3d->coefs[3]);
245 av_freep(&hqdn3d->line);
246 av_freep(&hqdn3d->frame_prev[0]);
247 av_freep(&hqdn3d->frame_prev[1]);
248 av_freep(&hqdn3d->frame_prev[2]);
251 static int query_formats(AVFilterContext *ctx)
253 static const enum AVPixelFormat pix_fmts[] = {
264 AV_NE( AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUV420P9LE ),
265 AV_NE( AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUV422P9LE ),
266 AV_NE( AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUV444P9LE ),
267 AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
268 AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
269 AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
270 AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
271 AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
272 AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
276 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
281 static int config_input(AVFilterLink *inlink)
283 HQDN3DContext *hqdn3d = inlink->dst->priv;
284 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
287 hqdn3d->hsub = desc->log2_chroma_w;
288 hqdn3d->vsub = desc->log2_chroma_h;
289 hqdn3d->depth = desc->comp[0].depth_minus1+1;
291 hqdn3d->line = av_malloc(inlink->w * sizeof(*hqdn3d->line));
293 return AVERROR(ENOMEM);
295 for (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);
302 ff_hqdn3d_init_x86(hqdn3d);
307 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
309 HQDN3DContext *hqdn3d = inlink->dst->priv;
310 AVFilterLink *outlink = inlink->dst->outputs[0];
312 AVFilterBufferRef *out;
315 if (in->perms & AV_PERM_WRITE) {
319 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
321 avfilter_unref_bufferp(&in);
322 return AVERROR(ENOMEM);
324 avfilter_copy_buffer_ref_props(out, in);
327 for (c = 0; c < 3; c++) {
328 denoise(hqdn3d, in->data[c], out->data[c],
329 hqdn3d->line, &hqdn3d->frame_prev[c],
330 in->video->w >> (!!c * hqdn3d->hsub),
331 in->video->h >> (!!c * hqdn3d->vsub),
332 in->linesize[c], out->linesize[c],
333 hqdn3d->coefs[c?2:0], hqdn3d->coefs[c?3:1]);
337 avfilter_unref_bufferp(&in);
339 return ff_filter_frame(outlink, out);
342 static const AVFilterPad avfilter_vf_hqdn3d_inputs[] = {
345 .type = AVMEDIA_TYPE_VIDEO,
346 .config_props = config_input,
347 .filter_frame = filter_frame,
353 static const AVFilterPad avfilter_vf_hqdn3d_outputs[] = {
356 .type = AVMEDIA_TYPE_VIDEO
361 AVFilter avfilter_vf_hqdn3d = {
363 .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
365 .priv_size = sizeof(HQDN3DContext),
368 .query_formats = query_formats,
370 .inputs = avfilter_vf_hqdn3d_inputs,
372 .outputs = avfilter_vf_hqdn3d_outputs,