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