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