]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_hqdn3d.c
vf_hqdn3d: support 16bit colordepth
[ffmpeg] / libavfilter / vf_hqdn3d.c
1 /*
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
5  *
6  * This file is part of Libav, ported from MPlayer.
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 /**
24  * @file
25  * high quality 3d video denoiser, ported from MPlayer
26  * libmpcodecs/vf_hqdn3d.c.
27  */
28
29 #include "libavutil/common.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/intreadwrite.h"
32 #include "avfilter.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36
37 typedef struct {
38     int16_t *coefs[4];
39     uint16_t *line;
40     uint16_t *frame_prev[3];
41     double strength[4];
42     int hsub, vsub;
43     int depth;
44 } HQDN3DContext;
45
46 #define LUT_BITS (depth==16 ? 8 : 4)
47 #define RIGHTSHIFT(a,b) (((a)+(((1<<(b))-1)>>1))>>(b))
48 #define LOAD(x) ((depth==8 ? src[x] : AV_RN16A(src+(x)*2)) << (16-depth))
49 #define STORE(x,val) (depth==8 ? dst[x] = RIGHTSHIFT(val, 16-depth)\
50                     : AV_WN16A(dst+(x)*2, RIGHTSHIFT(val, 16-depth)))
51
52 av_always_inline
53 static inline uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
54 {
55     int d = (prev - cur) >> (8 - LUT_BITS);
56     return cur + coef[d];
57 }
58
59 av_always_inline
60 static void denoise_temporal(uint8_t *src, uint8_t *dst,
61                              uint16_t *frame_ant,
62                              int w, int h, int sstride, int dstride,
63                              int16_t *temporal, int depth)
64 {
65     long x, y;
66     uint32_t tmp;
67
68     temporal += 256 << LUT_BITS;
69
70     for (y = 0; y < h; y++) {
71         for (x = 0; x < w; x++) {
72             frame_ant[x] = tmp = lowpass(frame_ant[x], LOAD(x), temporal, depth);
73             STORE(x, tmp);
74         }
75         src += sstride;
76         dst += dstride;
77         frame_ant += w;
78     }
79 }
80
81 av_always_inline
82 static void denoise_spatial(uint8_t *src, uint8_t *dst,
83                             uint16_t *line_ant, uint16_t *frame_ant,
84                             int w, int h, int sstride, int dstride,
85                             int16_t *spatial, int16_t *temporal, int depth)
86 {
87     long x, y;
88     uint32_t pixel_ant;
89     uint32_t tmp;
90
91     spatial  += 256 << LUT_BITS;
92     temporal += 256 << LUT_BITS;
93
94     /* First line has no top neighbor. Only left one for each tmp and
95      * last frame */
96     pixel_ant = LOAD(0);
97     for (x = 0; x < w; x++) {
98         line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial, depth);
99         frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
100         STORE(x, tmp);
101     }
102
103     for (y = 1; y < h; y++) {
104         src += sstride;
105         dst += dstride;
106         frame_ant += w;
107         pixel_ant = LOAD(0);
108         for (x = 0; x < w-1; x++) {
109             line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
110             pixel_ant = lowpass(pixel_ant, LOAD(x+1), spatial, depth);
111             frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
112             STORE(x, tmp);
113         }
114         line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
115         frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
116         STORE(x, tmp);
117     }
118 }
119
120 av_always_inline
121 static void denoise_depth(uint8_t *src, uint8_t *dst,
122                           uint16_t *line_ant, uint16_t **frame_ant_ptr,
123                           int w, int h, int sstride, int dstride,
124                           int16_t *spatial, int16_t *temporal, int depth)
125 {
126     // FIXME: For 16bit depth, frame_ant could be a pointer to the previous
127     // filtered frame rather than a separate buffer.
128     long x, y;
129     uint16_t *frame_ant = *frame_ant_ptr;
130     if (!frame_ant) {
131         uint8_t *frame_src = src;
132         *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
133         for (y = 0; y < h; y++, src += sstride, frame_ant += w)
134             for (x = 0; x < w; x++)
135                 frame_ant[x] = LOAD(x);
136         src = frame_src;
137         frame_ant = *frame_ant_ptr;
138     }
139
140     if (spatial[0])
141         denoise_spatial(src, dst, line_ant, frame_ant,
142                         w, h, sstride, dstride, spatial, temporal, depth);
143     else
144         denoise_temporal(src, dst, frame_ant,
145                          w, h, sstride, dstride, temporal, depth);
146 }
147
148 #define denoise(...) \
149     switch (hqdn3d->depth) {\
150         case  8: denoise_depth(__VA_ARGS__,  8); break;\
151         case  9: denoise_depth(__VA_ARGS__,  9); break;\
152         case 10: denoise_depth(__VA_ARGS__, 10); break;\
153         case 16: denoise_depth(__VA_ARGS__, 16); break;\
154     }
155
156 static int16_t *precalc_coefs(double dist25, int depth)
157 {
158     int i;
159     double gamma, simil, C;
160     int16_t *ct = av_malloc((512<<LUT_BITS)*sizeof(int16_t));
161     if (!ct)
162         return NULL;
163
164     gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
165
166     for (i = -255<<LUT_BITS; i <= 255<<LUT_BITS; i++) {
167         double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
168         simil = 1.0 - FFABS(f) / 255.0;
169         C = pow(simil, gamma) * 256.0 * f;
170         ct[(256<<LUT_BITS)+i] = lrint(C);
171     }
172
173     ct[0] = !!dist25;
174     return ct;
175 }
176
177 #define PARAM1_DEFAULT 4.0
178 #define PARAM2_DEFAULT 3.0
179 #define PARAM3_DEFAULT 6.0
180
181 static int init(AVFilterContext *ctx, const char *args)
182 {
183     HQDN3DContext *hqdn3d = ctx->priv;
184     double lum_spac, lum_tmp, chrom_spac, chrom_tmp;
185     double param1, param2, param3, param4;
186
187     lum_spac   = PARAM1_DEFAULT;
188     chrom_spac = PARAM2_DEFAULT;
189     lum_tmp    = PARAM3_DEFAULT;
190     chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
191
192     if (args) {
193         switch (sscanf(args, "%lf:%lf:%lf:%lf",
194                        &param1, &param2, &param3, &param4)) {
195         case 1:
196             lum_spac   = param1;
197             chrom_spac = PARAM2_DEFAULT * param1 / PARAM1_DEFAULT;
198             lum_tmp    = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
199             chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
200             break;
201         case 2:
202             lum_spac   = param1;
203             chrom_spac = param2;
204             lum_tmp    = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
205             chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
206             break;
207         case 3:
208             lum_spac   = param1;
209             chrom_spac = param2;
210             lum_tmp    = param3;
211             chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
212             break;
213         case 4:
214             lum_spac   = param1;
215             chrom_spac = param2;
216             lum_tmp    = param3;
217             chrom_tmp  = param4;
218             break;
219         }
220     }
221
222     hqdn3d->strength[0] = lum_spac;
223     hqdn3d->strength[1] = lum_tmp;
224     hqdn3d->strength[2] = chrom_spac;
225     hqdn3d->strength[3] = chrom_tmp;
226
227     av_log(ctx, AV_LOG_VERBOSE, "ls:%lf cs:%lf lt:%lf ct:%lf\n",
228            lum_spac, chrom_spac, lum_tmp, chrom_tmp);
229     if (lum_spac < 0 || chrom_spac < 0 || isnan(chrom_tmp)) {
230         av_log(ctx, AV_LOG_ERROR,
231                "Invalid negative value for luma or chroma spatial strength, "
232                "or resulting value for chroma temporal strength is nan.\n");
233         return AVERROR(EINVAL);
234     }
235
236     return 0;
237 }
238
239 static void uninit(AVFilterContext *ctx)
240 {
241     HQDN3DContext *hqdn3d = ctx->priv;
242
243     av_freep(&hqdn3d->coefs[0]);
244     av_freep(&hqdn3d->coefs[1]);
245     av_freep(&hqdn3d->coefs[2]);
246     av_freep(&hqdn3d->coefs[3]);
247     av_freep(&hqdn3d->line);
248     av_freep(&hqdn3d->frame_prev[0]);
249     av_freep(&hqdn3d->frame_prev[1]);
250     av_freep(&hqdn3d->frame_prev[2]);
251 }
252
253 static int query_formats(AVFilterContext *ctx)
254 {
255     static const enum PixelFormat pix_fmts[] = {
256         PIX_FMT_YUV420P,
257         PIX_FMT_YUV422P,
258         PIX_FMT_YUV444P,
259         PIX_FMT_YUV410P,
260         PIX_FMT_YUV411P,
261         PIX_FMT_YUV440P,
262         PIX_FMT_YUVJ420P,
263         PIX_FMT_YUVJ422P,
264         PIX_FMT_YUVJ444P,
265         PIX_FMT_YUVJ440P,
266         AV_NE( PIX_FMT_YUV420P9BE, PIX_FMT_YUV420P9LE ),
267         AV_NE( PIX_FMT_YUV422P9BE, PIX_FMT_YUV422P9LE ),
268         AV_NE( PIX_FMT_YUV444P9BE, PIX_FMT_YUV444P9LE ),
269         AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
270         AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
271         AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
272         AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
273         AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
274         AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
275         PIX_FMT_NONE
276     };
277
278     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
279
280     return 0;
281 }
282
283 static int config_input(AVFilterLink *inlink)
284 {
285     HQDN3DContext *hqdn3d = inlink->dst->priv;
286
287     hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
288     hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
289     hqdn3d->depth = av_pix_fmt_descriptors[inlink->format].comp[0].depth_minus1+1;
290
291     hqdn3d->line = av_malloc(inlink->w * sizeof(*hqdn3d->line));
292     if (!hqdn3d->line)
293         return AVERROR(ENOMEM);
294
295     for (int 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);
299     }
300
301     return 0;
302 }
303
304 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
305 {
306     return 0;
307 }
308
309 static int end_frame(AVFilterLink *inlink)
310 {
311     HQDN3DContext *hqdn3d = inlink->dst->priv;
312     AVFilterLink *outlink = inlink->dst->outputs[0];
313     AVFilterBufferRef *inpic  = inlink ->cur_buf;
314     AVFilterBufferRef *outpic = outlink->out_buf;
315     int ret, c;
316
317     for (c = 0; c < 3; c++) {
318         denoise(inpic->data[c], outpic->data[c],
319                 hqdn3d->line, &hqdn3d->frame_prev[c],
320                 inpic->video->w >> (!!c * hqdn3d->hsub),
321                 inpic->video->h >> (!!c * hqdn3d->vsub),
322                 inpic->linesize[c], outpic->linesize[c],
323                 hqdn3d->coefs[c?2:0], hqdn3d->coefs[c?3:1]);
324     }
325
326     if ((ret = ff_draw_slice(outlink, 0, inpic->video->h, 1)) < 0 ||
327         (ret = ff_end_frame(outlink)) < 0)
328         return ret;
329     return 0;
330 }
331
332 AVFilter avfilter_vf_hqdn3d = {
333     .name          = "hqdn3d",
334     .description   = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
335
336     .priv_size     = sizeof(HQDN3DContext),
337     .init          = init,
338     .uninit        = uninit,
339     .query_formats = query_formats,
340
341     .inputs    = (const AVFilterPad[]) {{ .name             = "default",
342                                           .type             = AVMEDIA_TYPE_VIDEO,
343                                           .start_frame      = ff_inplace_start_frame,
344                                           .draw_slice       = null_draw_slice,
345                                           .config_props     = config_input,
346                                           .end_frame        = end_frame },
347                                         { .name = NULL}},
348
349     .outputs   = (const AVFilterPad[]) {{ .name             = "default",
350                                           .type             = AVMEDIA_TYPE_VIDEO },
351                                         { .name = NULL}},
352 };