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