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