]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_hqdn3d.c
vf_hqdn3d: cosmetics
[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  *
5  * This file is part of Libav, ported from MPlayer.
6  *
7  * Libav is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with Libav; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 /**
23  * @file
24  * high quality 3d video denoiser, ported from MPlayer
25  * libmpcodecs/vf_hqdn3d.c.
26  */
27
28 #include "libavutil/pixdesc.h"
29 #include "avfilter.h"
30 #include "formats.h"
31 #include "internal.h"
32 #include "video.h"
33
34 typedef struct {
35     int coefs[4][512*16];
36     uint32_t *line;
37     uint16_t *frame_prev[3];
38     int hsub, vsub;
39 } HQDN3DContext;
40
41 static inline uint32_t lowpass(unsigned int prev, unsigned int cur, int *coef)
42 {
43     int dmul = prev-cur;
44     unsigned int d = (dmul+0x10007FF)>>12; // 0x1000 to convert to unsigned, 7FF for rounding
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                              int *temporal)
52 {
53     long x, y;
54     uint32_t pixel;
55
56     for (y = 0; y < h; y++) {
57         for (x = 0; x < w; x++) {
58             pixel = lowpass(frame_ant[x]<<8, src[x]<<16, temporal);
59             frame_ant[x] = ((pixel+0x1000007F)>>8);
60             dst[x]= ((pixel+0x10007FFF)>>16);
61         }
62         src += sstride;
63         dst += dstride;
64         frame_ant += w;
65     }
66 }
67
68 static void denoise_spatial(uint8_t *src, uint8_t *dst,
69                             uint32_t *line_ant,
70                             int w, int h, int sstride, int dstride,
71                             int *horizontal, int *vertical)
72 {
73     long x, y;
74     long sline_offs = 0, dline_offs = 0;
75     uint32_t pixel_ant;
76     uint32_t pixel;
77
78     /* First pixel has no left nor top neighbor. */
79     pixel = line_ant[0] = pixel_ant = src[0]<<16;
80     dst[0]= ((pixel+0x10007FFF)>>16);
81
82     /* First line has no top neighbor, only left. */
83     for (x = 1; x < w; x++) {
84         pixel = line_ant[x] = lowpass(pixel_ant, src[x]<<16, horizontal);
85         dst[x]= ((pixel+0x10007FFF)>>16);
86     }
87
88     for (y = 1; y < h; y++) {
89         uint32_t pixel_ant;
90         sline_offs += sstride, dline_offs += dstride;
91         /* First pixel on each line doesn't have previous pixel */
92         pixel_ant = src[sline_offs]<<16;
93         pixel = line_ant[0] = lowpass(line_ant[0], pixel_ant, vertical);
94         dst[dline_offs]= ((pixel+0x10007FFF)>>16);
95
96         for (x = 1; x < w; x++) {
97             uint32_t pixel;
98             /* The rest are normal */
99             pixel_ant = lowpass(pixel_ant, src[sline_offs+x]<<16, horizontal);
100             pixel = line_ant[x] = lowpass(line_ant[x], pixel_ant, vertical);
101             dst[dline_offs+x]= ((pixel+0x10007FFF)>>16);
102         }
103     }
104 }
105
106 static void denoise(uint8_t *src, uint8_t *dst,
107                     uint32_t *line_ant, uint16_t **frame_ant_ptr,
108                     int w, int h, int sstride, int dstride,
109                     int *horizontal, int *vertical, int *temporal)
110 {
111     long x, y;
112     long sline_offs = 0, dline_offs = 0;
113     uint32_t pixel_ant;
114     uint32_t pixel;
115     uint16_t *frame_ant = *frame_ant_ptr;
116
117     if (!frame_ant) {
118         *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
119         for (y = 0; y < h; y++) {
120             uint16_t *frame_dst = frame_ant+y*w;
121             uint8_t *frame_src = src+y*sstride;
122             for (x = 0; x < w; x++)
123                 frame_dst[x] = frame_src[x]<<8;
124         }
125     }
126
127     if (!horizontal[0] && !vertical[0]) {
128         denoise_temporal(src, dst, frame_ant,
129                          w, h, sstride, dstride, temporal);
130         return;
131     }
132     if (!temporal[0]) {
133         denoise_spatial(src, dst, line_ant,
134                         w, h, sstride, dstride, horizontal, vertical);
135         return;
136     }
137
138     /* First pixel has no left nor top neighbor. Only previous frame */
139     line_ant[0] = pixel_ant = src[0]<<16;
140     pixel = lowpass(frame_ant[0]<<8, pixel_ant, temporal);
141     frame_ant[0] = ((pixel+0x1000007F)>>8);
142     dst[0]= ((pixel+0x10007FFF)>>16);
143
144     /* First line has no top neighbor. Only left one for each pixel and
145      * last frame */
146     for (x = 1; x < w; x++) {
147         line_ant[x] = pixel_ant = lowpass(pixel_ant, src[x]<<16, horizontal);
148         pixel = lowpass(frame_ant[x]<<8, pixel_ant, temporal);
149         frame_ant[x] = ((pixel+0x1000007F)>>8);
150         dst[x]= ((pixel+0x10007FFF)>>16);
151     }
152
153     for (y = 1; y < h; y++) {
154         uint32_t pixel_ant;
155         uint16_t *line_prev = frame_ant+y*w;
156         sline_offs += sstride, dline_offs += dstride;
157         /* First pixel on each line doesn't have previous pixel */
158         pixel_ant = src[sline_offs]<<16;
159         line_ant[0] = lowpass(line_ant[0], pixel_ant, vertical);
160         pixel = lowpass(line_prev[0]<<8, line_ant[0], temporal);
161         line_prev[0] = ((pixel+0x1000007F)>>8);
162         dst[dline_offs]= ((pixel+0x10007FFF)>>16);
163
164         for (x = 1; x < w; x++) {
165             uint32_t pixel;
166             /* The rest are normal */
167             pixel_ant = lowpass(pixel_ant, src[sline_offs+x]<<16, horizontal);
168             line_ant[x] = lowpass(line_ant[x], pixel_ant, vertical);
169             pixel = lowpass(line_prev[x]<<8, line_ant[x], temporal);
170             line_prev[x] = ((pixel+0x1000007F)>>8);
171             dst[dline_offs+x]= ((pixel+0x10007FFF)>>16);
172         }
173     }
174 }
175
176 static void precalc_coefs(int *ct, double dist25)
177 {
178     int i;
179     double gamma, simil, C;
180
181     gamma = log(0.25) / log(1.0 - dist25/255.0 - 0.00001);
182
183     for (i = -255*16; i <= 255*16; i++) {
184         simil = 1.0 - FFABS(i) / (16*255.0);
185         C = pow(simil, gamma) * 65536.0 * i / 16.0;
186         ct[16*256+i] = lrint(C);
187     }
188
189     ct[0] = !!dist25;
190 }
191
192 #define PARAM1_DEFAULT 4.0
193 #define PARAM2_DEFAULT 3.0
194 #define PARAM3_DEFAULT 6.0
195
196 static int init(AVFilterContext *ctx, const char *args)
197 {
198     HQDN3DContext *hqdn3d = ctx->priv;
199     double lum_spac, lum_tmp, chrom_spac, chrom_tmp;
200     double param1, param2, param3, param4;
201
202     lum_spac   = PARAM1_DEFAULT;
203     chrom_spac = PARAM2_DEFAULT;
204     lum_tmp    = PARAM3_DEFAULT;
205     chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
206
207     if (args) {
208         switch (sscanf(args, "%lf:%lf:%lf:%lf",
209                        &param1, &param2, &param3, &param4)) {
210         case 1:
211             lum_spac   = param1;
212             chrom_spac = PARAM2_DEFAULT * param1 / PARAM1_DEFAULT;
213             lum_tmp    = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
214             chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
215             break;
216         case 2:
217             lum_spac   = param1;
218             chrom_spac = param2;
219             lum_tmp    = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
220             chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
221             break;
222         case 3:
223             lum_spac   = param1;
224             chrom_spac = param2;
225             lum_tmp    = param3;
226             chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
227             break;
228         case 4:
229             lum_spac   = param1;
230             chrom_spac = param2;
231             lum_tmp    = param3;
232             chrom_tmp  = param4;
233             break;
234         }
235     }
236
237     av_log(ctx, AV_LOG_VERBOSE, "ls:%lf cs:%lf lt:%lf ct:%lf\n",
238            lum_spac, chrom_spac, lum_tmp, chrom_tmp);
239     if (lum_spac < 0 || chrom_spac < 0 || isnan(chrom_tmp)) {
240         av_log(ctx, AV_LOG_ERROR,
241                "Invalid negative value for luma or chroma spatial strength, "
242                "or resulting value for chroma temporal strength is nan.\n");
243         return AVERROR(EINVAL);
244     }
245
246     precalc_coefs(hqdn3d->coefs[0], lum_spac);
247     precalc_coefs(hqdn3d->coefs[1], lum_tmp);
248     precalc_coefs(hqdn3d->coefs[2], chrom_spac);
249     precalc_coefs(hqdn3d->coefs[3], chrom_tmp);
250
251     return 0;
252 }
253
254 static void uninit(AVFilterContext *ctx)
255 {
256     HQDN3DContext *hqdn3d = ctx->priv;
257
258     av_freep(&hqdn3d->line);
259     av_freep(&hqdn3d->frame_prev[0]);
260     av_freep(&hqdn3d->frame_prev[1]);
261     av_freep(&hqdn3d->frame_prev[2]);
262 }
263
264 static int query_formats(AVFilterContext *ctx)
265 {
266     static const enum PixelFormat pix_fmts[] = {
267         PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_NONE
268     };
269
270     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
271
272     return 0;
273 }
274
275 static int config_input(AVFilterLink *inlink)
276 {
277     HQDN3DContext *hqdn3d = inlink->dst->priv;
278
279     hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
280     hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
281
282     hqdn3d->line = av_malloc(inlink->w * sizeof(*hqdn3d->line));
283     if (!hqdn3d->line)
284         return AVERROR(ENOMEM);
285
286     return 0;
287 }
288
289 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
290 {
291     return 0;
292 }
293
294 static int end_frame(AVFilterLink *inlink)
295 {
296     HQDN3DContext *hqdn3d = inlink->dst->priv;
297     AVFilterLink *outlink = inlink->dst->outputs[0];
298     AVFilterBufferRef *inpic  = inlink ->cur_buf;
299     AVFilterBufferRef *outpic = outlink->out_buf;
300     int cw = inpic->video->w >> hqdn3d->hsub;
301     int ch = inpic->video->h >> hqdn3d->vsub;
302     int ret;
303
304     denoise(inpic->data[0], outpic->data[0],
305             hqdn3d->line, &hqdn3d->frame_prev[0], inpic->video->w, inpic->video->h,
306             inpic->linesize[0], outpic->linesize[0],
307             hqdn3d->coefs[0],
308             hqdn3d->coefs[0],
309             hqdn3d->coefs[1]);
310     denoise(inpic->data[1], outpic->data[1],
311             hqdn3d->line, &hqdn3d->frame_prev[1], cw, ch,
312             inpic->linesize[1], outpic->linesize[1],
313             hqdn3d->coefs[2],
314             hqdn3d->coefs[2],
315             hqdn3d->coefs[3]);
316     denoise(inpic->data[2], outpic->data[2],
317             hqdn3d->line, &hqdn3d->frame_prev[2], cw, ch,
318             inpic->linesize[2], outpic->linesize[2],
319             hqdn3d->coefs[2],
320             hqdn3d->coefs[2],
321             hqdn3d->coefs[3]);
322
323     if ((ret = ff_draw_slice(outlink, 0, inpic->video->h, 1)) < 0 ||
324         (ret = ff_end_frame(outlink)) < 0)
325         return ret;
326     return 0;
327 }
328
329 AVFilter avfilter_vf_hqdn3d = {
330     .name          = "hqdn3d",
331     .description   = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
332
333     .priv_size     = sizeof(HQDN3DContext),
334     .init          = init,
335     .uninit        = uninit,
336     .query_formats = query_formats,
337
338     .inputs    = (const AVFilterPad[]) {{ .name             = "default",
339                                           .type             = AVMEDIA_TYPE_VIDEO,
340                                           .draw_slice       = null_draw_slice,
341                                           .config_props     = config_input,
342                                           .end_frame        = end_frame },
343                                         { .name = NULL}},
344
345     .outputs   = (const AVFilterPad[]) {{ .name             = "default",
346                                           .type             = AVMEDIA_TYPE_VIDEO },
347                                         { .name = NULL}},
348 };