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