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