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