]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_hqdn3d.c
vf_hqdn3d: fix uninitialized variable use
[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 "config.h"
30 #include "libavutil/common.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/intreadwrite.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "video.h"
37 #include "vf_hqdn3d.h"
38
39 #define LUT_BITS (depth==16 ? 8 : 4)
40 #define RIGHTSHIFT(a,b) (((a)+(((1<<(b))-1)>>1))>>(b))
41 #define LOAD(x) ((depth==8 ? src[x] : AV_RN16A(src+(x)*2)) << (16-depth))
42 #define STORE(x,val) (depth==8 ? dst[x] = RIGHTSHIFT(val, 16-depth)\
43                     : AV_WN16A(dst+(x)*2, RIGHTSHIFT(val, 16-depth)))
44
45 av_always_inline
46 static uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
47 {
48     int d = (prev - cur) >> (8 - LUT_BITS);
49     return cur + coef[d];
50 }
51
52 av_always_inline
53 static void denoise_temporal(uint8_t *src, uint8_t *dst,
54                              uint16_t *frame_ant,
55                              int w, int h, int sstride, int dstride,
56                              int16_t *temporal, int depth)
57 {
58     long x, y;
59     uint32_t tmp;
60
61     temporal += 256 << LUT_BITS;
62
63     for (y = 0; y < h; y++) {
64         for (x = 0; x < w; x++) {
65             frame_ant[x] = tmp = lowpass(frame_ant[x], LOAD(x), temporal, depth);
66             STORE(x, tmp);
67         }
68         src += sstride;
69         dst += dstride;
70         frame_ant += w;
71     }
72 }
73
74 av_always_inline
75 static void denoise_spatial(HQDN3DContext *hqdn3d,
76                             uint8_t *src, uint8_t *dst,
77                             uint16_t *line_ant, uint16_t *frame_ant,
78                             int w, int h, int sstride, int dstride,
79                             int16_t *spatial, int16_t *temporal, int depth)
80 {
81     long x, y;
82     uint32_t pixel_ant;
83     uint32_t tmp;
84
85     spatial  += 256 << LUT_BITS;
86     temporal += 256 << LUT_BITS;
87
88     /* First line has no top neighbor. Only left one for each tmp and
89      * last frame */
90     pixel_ant = LOAD(0);
91     for (x = 0; x < w; x++) {
92         line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial, depth);
93         frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
94         STORE(x, tmp);
95     }
96
97     for (y = 1; y < h; y++) {
98         src += sstride;
99         dst += dstride;
100         frame_ant += w;
101         if (hqdn3d->denoise_row[depth]) {
102             hqdn3d->denoise_row[depth](src, dst, line_ant, frame_ant, w, spatial, temporal);
103             continue;
104         }
105         pixel_ant = LOAD(0);
106         for (x = 0; x < w-1; x++) {
107             line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
108             pixel_ant = lowpass(pixel_ant, LOAD(x+1), spatial, depth);
109             frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
110             STORE(x, tmp);
111         }
112         line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
113         frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
114         STORE(x, tmp);
115     }
116 }
117
118 av_always_inline
119 static void denoise_depth(HQDN3DContext *hqdn3d,
120                           uint8_t *src, uint8_t *dst,
121                           uint16_t *line_ant, uint16_t **frame_ant_ptr,
122                           int w, int h, int sstride, int dstride,
123                           int16_t *spatial, int16_t *temporal, int depth)
124 {
125     // FIXME: For 16bit depth, frame_ant could be a pointer to the previous
126     // filtered frame rather than a separate buffer.
127     long x, y;
128     uint16_t *frame_ant = *frame_ant_ptr;
129     if (!frame_ant) {
130         uint8_t *frame_src = src;
131         *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
132         for (y = 0; y < h; y++, src += sstride, frame_ant += w)
133             for (x = 0; x < w; x++)
134                 frame_ant[x] = LOAD(x);
135         src = frame_src;
136         frame_ant = *frame_ant_ptr;
137     }
138
139     if (spatial[0])
140         denoise_spatial(hqdn3d, src, dst, line_ant, frame_ant,
141                         w, h, sstride, dstride, spatial, temporal, depth);
142     else
143         denoise_temporal(src, dst, frame_ant,
144                          w, h, sstride, dstride, temporal, depth);
145 }
146
147 #define denoise(...) \
148     switch (hqdn3d->depth) {\
149         case  8: denoise_depth(__VA_ARGS__,  8); break;\
150         case  9: denoise_depth(__VA_ARGS__,  9); break;\
151         case 10: denoise_depth(__VA_ARGS__, 10); break;\
152         case 16: denoise_depth(__VA_ARGS__, 16); break;\
153     }
154
155 static int16_t *precalc_coefs(double dist25, int depth)
156 {
157     int i;
158     double gamma, simil, C;
159     int16_t *ct = av_malloc((512<<LUT_BITS)*sizeof(int16_t));
160     if (!ct)
161         return NULL;
162
163     gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
164
165     for (i = -255<<LUT_BITS; i <= 255<<LUT_BITS; i++) {
166         double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
167         simil = 1.0 - FFABS(f) / 255.0;
168         C = pow(simil, gamma) * 256.0 * f;
169         ct[(256<<LUT_BITS)+i] = lrint(C);
170     }
171
172     ct[0] = !!dist25;
173     return ct;
174 }
175
176 #define PARAM1_DEFAULT 4.0
177 #define PARAM2_DEFAULT 3.0
178 #define PARAM3_DEFAULT 6.0
179
180 static int init(AVFilterContext *ctx, const char *args)
181 {
182     HQDN3DContext *hqdn3d = ctx->priv;
183     double lum_spac, lum_tmp, chrom_spac, chrom_tmp;
184     double param1, param2, param3, param4;
185
186     lum_spac   = PARAM1_DEFAULT;
187     chrom_spac = PARAM2_DEFAULT;
188     lum_tmp    = PARAM3_DEFAULT;
189     chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
190
191     if (args) {
192         switch (sscanf(args, "%lf:%lf:%lf:%lf",
193                        &param1, &param2, &param3, &param4)) {
194         case 1:
195             lum_spac   = param1;
196             chrom_spac = PARAM2_DEFAULT * param1 / PARAM1_DEFAULT;
197             lum_tmp    = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
198             chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
199             break;
200         case 2:
201             lum_spac   = param1;
202             chrom_spac = param2;
203             lum_tmp    = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
204             chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
205             break;
206         case 3:
207             lum_spac   = param1;
208             chrom_spac = param2;
209             lum_tmp    = param3;
210             chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
211             break;
212         case 4:
213             lum_spac   = param1;
214             chrom_spac = param2;
215             lum_tmp    = param3;
216             chrom_tmp  = param4;
217             break;
218         }
219     }
220
221     hqdn3d->strength[0] = lum_spac;
222     hqdn3d->strength[1] = lum_tmp;
223     hqdn3d->strength[2] = chrom_spac;
224     hqdn3d->strength[3] = chrom_tmp;
225
226     av_log(ctx, AV_LOG_VERBOSE, "ls:%f cs:%f lt:%f ct:%f\n",
227            lum_spac, chrom_spac, lum_tmp, chrom_tmp);
228     if (lum_spac < 0 || chrom_spac < 0 || isnan(chrom_tmp)) {
229         av_log(ctx, AV_LOG_ERROR,
230                "Invalid negative value for luma or chroma spatial strength, "
231                "or resulting value for chroma temporal strength is nan.\n");
232         return AVERROR(EINVAL);
233     }
234
235     return 0;
236 }
237
238 static void uninit(AVFilterContext *ctx)
239 {
240     HQDN3DContext *hqdn3d = ctx->priv;
241
242     av_freep(&hqdn3d->coefs[0]);
243     av_freep(&hqdn3d->coefs[1]);
244     av_freep(&hqdn3d->coefs[2]);
245     av_freep(&hqdn3d->coefs[3]);
246     av_freep(&hqdn3d->line);
247     av_freep(&hqdn3d->frame_prev[0]);
248     av_freep(&hqdn3d->frame_prev[1]);
249     av_freep(&hqdn3d->frame_prev[2]);
250 }
251
252 static int query_formats(AVFilterContext *ctx)
253 {
254     static const enum AVPixelFormat pix_fmts[] = {
255         AV_PIX_FMT_YUV420P,
256         AV_PIX_FMT_YUV422P,
257         AV_PIX_FMT_YUV444P,
258         AV_PIX_FMT_YUV410P,
259         AV_PIX_FMT_YUV411P,
260         AV_PIX_FMT_YUV440P,
261         AV_PIX_FMT_YUVJ420P,
262         AV_PIX_FMT_YUVJ422P,
263         AV_PIX_FMT_YUVJ444P,
264         AV_PIX_FMT_YUVJ440P,
265         AV_NE( AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUV420P9LE ),
266         AV_NE( AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUV422P9LE ),
267         AV_NE( AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUV444P9LE ),
268         AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
269         AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
270         AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
271         AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
272         AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
273         AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
274         AV_PIX_FMT_NONE
275     };
276
277     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
278
279     return 0;
280 }
281
282 static int config_input(AVFilterLink *inlink)
283 {
284     HQDN3DContext *hqdn3d = inlink->dst->priv;
285     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
286     int i;
287
288     hqdn3d->hsub  = desc->log2_chroma_w;
289     hqdn3d->vsub  = desc->log2_chroma_h;
290     hqdn3d->depth = desc->comp[0].depth_minus1+1;
291
292     hqdn3d->line = av_malloc(inlink->w * sizeof(*hqdn3d->line));
293     if (!hqdn3d->line)
294         return AVERROR(ENOMEM);
295
296     for (i = 0; i < 4; i++) {
297         hqdn3d->coefs[i] = precalc_coefs(hqdn3d->strength[i], hqdn3d->depth);
298         if (!hqdn3d->coefs[i])
299             return AVERROR(ENOMEM);
300     }
301
302     if (ARCH_X86)
303         ff_hqdn3d_init_x86(hqdn3d);
304
305     return 0;
306 }
307
308 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
309 {
310     HQDN3DContext *hqdn3d = inlink->dst->priv;
311     AVFilterLink *outlink = inlink->dst->outputs[0];
312     AVFrame *out;
313     int direct, c;
314
315     if (av_frame_is_writable(in)) {
316         direct = 1;
317         out = in;
318     } else {
319         direct = 0;
320         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
321         if (!out) {
322             av_frame_free(&in);
323             return AVERROR(ENOMEM);
324         }
325
326         av_frame_copy_props(out, in);
327         out->width  = outlink->w;
328         out->height = outlink->h;
329     }
330
331     for (c = 0; c < 3; c++) {
332         denoise(hqdn3d, in->data[c], out->data[c],
333                 hqdn3d->line, &hqdn3d->frame_prev[c],
334                 in->width  >> (!!c * hqdn3d->hsub),
335                 in->height >> (!!c * hqdn3d->vsub),
336                 in->linesize[c], out->linesize[c],
337                 hqdn3d->coefs[c?2:0], hqdn3d->coefs[c?3:1]);
338     }
339
340     if (!direct)
341         av_frame_free(&in);
342
343     return ff_filter_frame(outlink, out);
344 }
345
346 static const AVFilterPad avfilter_vf_hqdn3d_inputs[] = {
347     {
348         .name         = "default",
349         .type         = AVMEDIA_TYPE_VIDEO,
350         .config_props = config_input,
351         .filter_frame = filter_frame,
352     },
353     { NULL }
354 };
355
356 static const AVFilterPad avfilter_vf_hqdn3d_outputs[] = {
357     {
358         .name = "default",
359         .type = AVMEDIA_TYPE_VIDEO
360     },
361     { NULL }
362 };
363
364 AVFilter avfilter_vf_hqdn3d = {
365     .name          = "hqdn3d",
366     .description   = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
367
368     .priv_size     = sizeof(HQDN3DContext),
369     .init          = init,
370     .uninit        = uninit,
371     .query_formats = query_formats,
372
373     .inputs    = avfilter_vf_hqdn3d_inputs,
374
375     .outputs   = avfilter_vf_hqdn3d_outputs,
376 };