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