]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_atadenoise.c
avfilter/vf_adadenoise: add x86 SIMD
[ffmpeg] / libavfilter / vf_atadenoise.c
1 /*
2  * Copyright (c) 2015 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * Adaptive Temporal Averaging Denoiser,
24  * based on paper "Video Denoising Based on Adaptive Temporal Averaging" by
25  * David Bartovčak and Miroslav Vrankić
26  */
27
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "avfilter.h"
32
33 #define FF_BUFQUEUE_SIZE 129
34 #include "bufferqueue.h"
35
36 #include "atadenoise.h"
37 #include "formats.h"
38 #include "internal.h"
39 #include "video.h"
40
41 #define SIZE FF_BUFQUEUE_SIZE
42
43 typedef struct ATADenoiseContext {
44     const AVClass *class;
45
46     float fthra[4], fthrb[4];
47     int thra[4], thrb[4];
48
49     int planes;
50     int nb_planes;
51     int planewidth[4];
52     int planeheight[4];
53
54     struct FFBufQueue q;
55     void *data[4][SIZE];
56     int linesize[4][SIZE];
57     int size, mid;
58     int available;
59
60     int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
61
62     ATADenoiseDSPContext dsp;
63 } ATADenoiseContext;
64
65 #define OFFSET(x) offsetof(ATADenoiseContext, x)
66 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
67
68 static const AVOption atadenoise_options[] = {
69     { "0a", "set threshold A for 1st plane", OFFSET(fthra[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0, 0.3, FLAGS },
70     { "0b", "set threshold B for 1st plane", OFFSET(fthrb[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 5.0, FLAGS },
71     { "1a", "set threshold A for 2nd plane", OFFSET(fthra[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0, 0.3, FLAGS },
72     { "1b", "set threshold B for 2nd plane", OFFSET(fthrb[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 5.0, FLAGS },
73     { "2a", "set threshold A for 3rd plane", OFFSET(fthra[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0, 0.3, FLAGS },
74     { "2b", "set threshold B for 3rd plane", OFFSET(fthrb[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 5.0, FLAGS },
75     { "s",  "set how many frames to use",    OFFSET(size),     AV_OPT_TYPE_INT,   {.i64=9},   5, SIZE, FLAGS },
76     { "p",  "set what planes to filter",     OFFSET(planes),   AV_OPT_TYPE_FLAGS, {.i64=7},    0, 15,  FLAGS },
77     { NULL }
78 };
79
80 AVFILTER_DEFINE_CLASS(atadenoise);
81
82 static int query_formats(AVFilterContext *ctx)
83 {
84     static const enum AVPixelFormat pixel_fmts[] = {
85         AV_PIX_FMT_GRAY8,
86         AV_PIX_FMT_GRAY9,
87         AV_PIX_FMT_GRAY10,
88         AV_PIX_FMT_GRAY12,
89         AV_PIX_FMT_GRAY14,
90         AV_PIX_FMT_GRAY16,
91         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
92         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
93         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
94         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
95         AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
96         AV_PIX_FMT_YUVJ411P,
97         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
98         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
99         AV_PIX_FMT_YUV440P10,
100         AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
101         AV_PIX_FMT_YUV440P12,
102         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
103         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
104         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
105         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
106         AV_PIX_FMT_NONE
107     };
108     AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
109     if (!formats)
110         return AVERROR(ENOMEM);
111     return ff_set_common_formats(ctx, formats);
112 }
113
114 static av_cold int init(AVFilterContext *ctx)
115 {
116     ATADenoiseContext *s = ctx->priv;
117
118     if (!(s->size & 1)) {
119         av_log(ctx, AV_LOG_WARNING, "size %d is invalid. Must be an odd value, setting it to %d.\n", s->size, s->size|1);
120         s->size |= 1;
121     }
122     s->mid = s->size / 2 + 1;
123
124     return 0;
125 }
126
127 typedef struct ThreadData {
128     AVFrame *in, *out;
129 } ThreadData;
130
131 #define FILTER_ROW(type, name)                                              \
132 static void filter_row##name(const uint8_t *ssrc, uint8_t *ddst,            \
133                              const uint8_t *ssrcf[SIZE],                    \
134                              int w, int mid, int size,                      \
135                              int thra, int thrb)                            \
136 {                                                                           \
137     const type *src = (const type *)ssrc;                                   \
138     const type **srcf = (const type **)ssrcf;                               \
139     type *dst = (type *)ddst;                                               \
140                                                                             \
141     for (int x = 0; x < w; x++) {                                           \
142        const int srcx = src[x];                                             \
143        unsigned lsumdiff = 0, rsumdiff = 0;                                 \
144        unsigned ldiff, rdiff;                                               \
145        unsigned sum = srcx;                                                 \
146        int l = 0, r = 0;                                                    \
147        int srcjx, srcix;                                                    \
148                                                                             \
149        for (int j = mid - 1, i = mid + 1; j >= 0 && i < size; j--, i++) {   \
150            srcjx = srcf[j][x];                                              \
151                                                                             \
152            ldiff = FFABS(srcx - srcjx);                                     \
153            lsumdiff += ldiff;                                               \
154            if (ldiff > thra ||                                              \
155                lsumdiff > thrb)                                             \
156                break;                                                       \
157            l++;                                                             \
158            sum += srcjx;                                                    \
159                                                                             \
160            srcix = srcf[i][x];                                              \
161                                                                             \
162            rdiff = FFABS(srcx - srcix);                                     \
163            rsumdiff += rdiff;                                               \
164            if (rdiff > thra ||                                              \
165                rsumdiff > thrb)                                             \
166                break;                                                       \
167            r++;                                                             \
168            sum += srcix;                                                    \
169        }                                                                    \
170                                                                             \
171        dst[x] = (sum + ((r + l + 1) >> 1)) / (r + l + 1);                   \
172    }                                                                        \
173 }
174
175 FILTER_ROW(uint8_t, 8)
176 FILTER_ROW(uint16_t, 16)
177
178 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
179 {
180     ATADenoiseContext *s = ctx->priv;
181     ThreadData *td = arg;
182     AVFrame *in = td->in;
183     AVFrame *out = td->out;
184     const int size = s->size;
185     const int mid = s->mid;
186     int p, y, i;
187
188     for (p = 0; p < s->nb_planes; p++) {
189         const int h = s->planeheight[p];
190         const int w = s->planewidth[p];
191         const int slice_start = (h * jobnr) / nb_jobs;
192         const int slice_end = (h * (jobnr+1)) / nb_jobs;
193         const uint8_t *src = in->data[p] + slice_start * in->linesize[p];
194         uint8_t *dst = out->data[p] + slice_start * out->linesize[p];
195         const int thra = s->thra[p];
196         const int thrb = s->thrb[p];
197         const uint8_t **data = (const uint8_t **)s->data[p];
198         const int *linesize = (const int *)s->linesize[p];
199         const uint8_t *srcf[SIZE];
200
201         if (!((1 << p) & s->planes)) {
202             av_image_copy_plane(dst, out->linesize[p], src, in->linesize[p],
203                                 w, slice_end - slice_start);
204             continue;
205         }
206
207         for (i = 0; i < size; i++)
208             srcf[i] = data[i] + slice_start * linesize[i];
209
210         for (y = slice_start; y < slice_end; y++) {
211             s->dsp.filter_row(src, dst, srcf, w, mid, size, thra, thrb);
212
213             dst += out->linesize[p];
214             src += in->linesize[p];
215
216             for (i = 0; i < size; i++)
217                 srcf[i] += linesize[i];
218         }
219     }
220
221     return 0;
222 }
223
224 static int config_input(AVFilterLink *inlink)
225 {
226     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
227     AVFilterContext *ctx = inlink->dst;
228     ATADenoiseContext *s = ctx->priv;
229     int depth;
230
231     s->nb_planes = desc->nb_components;
232
233     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
234     s->planeheight[0] = s->planeheight[3] = inlink->h;
235     s->planewidth[1]  = s->planewidth[2]  = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
236     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
237
238     depth = desc->comp[0].depth;
239     s->filter_slice = filter_slice;
240     if (depth == 8)
241         s->dsp.filter_row = filter_row8;
242     else
243         s->dsp.filter_row = filter_row16;
244
245     s->thra[0] = s->fthra[0] * (1 << depth) - 1;
246     s->thra[1] = s->fthra[1] * (1 << depth) - 1;
247     s->thra[2] = s->fthra[2] * (1 << depth) - 1;
248     s->thrb[0] = s->fthrb[0] * (1 << depth) - 1;
249     s->thrb[1] = s->fthrb[1] * (1 << depth) - 1;
250     s->thrb[2] = s->fthrb[2] * (1 << depth) - 1;
251
252     if (ARCH_X86)
253         ff_atadenoise_init_x86(&s->dsp, depth);
254
255     return 0;
256 }
257
258 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
259 {
260     AVFilterContext *ctx = inlink->dst;
261     AVFilterLink *outlink = ctx->outputs[0];
262     ATADenoiseContext *s = ctx->priv;
263     AVFrame *out, *in;
264     int i;
265
266     if (s->q.available != s->size) {
267         if (s->q.available < s->mid) {
268             for (i = 0; i < s->mid; i++) {
269                 out = av_frame_clone(buf);
270                 if (!out) {
271                     av_frame_free(&buf);
272                     return AVERROR(ENOMEM);
273                 }
274                 ff_bufqueue_add(ctx, &s->q, out);
275             }
276         }
277         if (s->q.available < s->size) {
278             ff_bufqueue_add(ctx, &s->q, buf);
279             s->available++;
280         }
281         return 0;
282     }
283
284     in = ff_bufqueue_peek(&s->q, s->mid);
285
286     if (!ctx->is_disabled) {
287         ThreadData td;
288
289         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
290         if (!out) {
291             av_frame_free(&buf);
292             return AVERROR(ENOMEM);
293         }
294
295         for (i = 0; i < s->size; i++) {
296             AVFrame *frame = ff_bufqueue_peek(&s->q, i);
297
298             s->data[0][i] = frame->data[0];
299             s->data[1][i] = frame->data[1];
300             s->data[2][i] = frame->data[2];
301             s->linesize[0][i] = frame->linesize[0];
302             s->linesize[1][i] = frame->linesize[1];
303             s->linesize[2][i] = frame->linesize[2];
304         }
305
306         td.in = in; td.out = out;
307         ctx->internal->execute(ctx, s->filter_slice, &td, NULL,
308                                FFMIN3(s->planeheight[1],
309                                       s->planeheight[2],
310                                       ff_filter_get_nb_threads(ctx)));
311         av_frame_copy_props(out, in);
312     } else {
313         out = av_frame_clone(in);
314         if (!out) {
315             av_frame_free(&buf);
316             return AVERROR(ENOMEM);
317         }
318     }
319
320     in = ff_bufqueue_get(&s->q);
321     av_frame_free(&in);
322     ff_bufqueue_add(ctx, &s->q, buf);
323
324     return ff_filter_frame(outlink, out);
325 }
326
327 static int request_frame(AVFilterLink *outlink)
328 {
329     AVFilterContext *ctx = outlink->src;
330     ATADenoiseContext *s = ctx->priv;
331     int ret = 0;
332
333     ret = ff_request_frame(ctx->inputs[0]);
334
335     if (ret == AVERROR_EOF && !ctx->is_disabled && s->available) {
336         AVFrame *buf = av_frame_clone(ff_bufqueue_peek(&s->q, s->available));
337         if (!buf)
338             return AVERROR(ENOMEM);
339
340         ret = filter_frame(ctx->inputs[0], buf);
341         s->available--;
342     }
343
344     return ret;
345 }
346
347 static av_cold void uninit(AVFilterContext *ctx)
348 {
349     ATADenoiseContext *s = ctx->priv;
350
351     ff_bufqueue_discard_all(&s->q);
352 }
353
354 static const AVFilterPad inputs[] = {
355     {
356         .name         = "default",
357         .type         = AVMEDIA_TYPE_VIDEO,
358         .filter_frame = filter_frame,
359         .config_props = config_input,
360     },
361     { NULL }
362 };
363
364 static const AVFilterPad outputs[] = {
365     {
366         .name          = "default",
367         .type          = AVMEDIA_TYPE_VIDEO,
368         .request_frame = request_frame,
369     },
370     { NULL }
371 };
372
373 AVFilter ff_vf_atadenoise = {
374     .name          = "atadenoise",
375     .description   = NULL_IF_CONFIG_SMALL("Apply an Adaptive Temporal Averaging Denoiser."),
376     .priv_size     = sizeof(ATADenoiseContext),
377     .priv_class    = &atadenoise_class,
378     .init          = init,
379     .uninit        = uninit,
380     .query_formats = query_formats,
381     .inputs        = inputs,
382     .outputs       = outputs,
383     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
384 };