2 * Copyright (c) 2015 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
23 * Adaptive Temporal Averaging Denoiser,
24 * based on paper "Video Denoising Based on Adaptive Temporal Averaging" by
25 * David Bartovčak and Miroslav Vrankić
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
33 #define FF_BUFQUEUE_SIZE 129
34 #include "bufferqueue.h"
36 #include "atadenoise.h"
41 #define SIZE FF_BUFQUEUE_SIZE
43 typedef struct ATADenoiseContext {
46 float fthra[4], fthrb[4];
56 int linesize[4][SIZE];
60 int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
62 ATADenoiseDSPContext dsp;
65 #define OFFSET(x) offsetof(ATADenoiseContext, x)
66 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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 },
80 AVFILTER_DEFINE_CLASS(atadenoise);
82 static int query_formats(AVFilterContext *ctx)
84 static const enum AVPixelFormat pixel_fmts[] = {
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,
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,
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,
108 AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
110 return AVERROR(ENOMEM);
111 return ff_set_common_formats(ctx, formats);
114 static av_cold int init(AVFilterContext *ctx)
116 ATADenoiseContext *s = ctx->priv;
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);
122 s->mid = s->size / 2 + 1;
127 typedef struct ThreadData {
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) \
137 const type *src = (const type *)ssrc; \
138 const type **srcf = (const type **)ssrcf; \
139 type *dst = (type *)ddst; \
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; \
149 for (int j = mid - 1, i = mid + 1; j >= 0 && i < size; j--, i++) { \
150 srcjx = srcf[j][x]; \
152 ldiff = FFABS(srcx - srcjx); \
154 if (ldiff > thra || \
160 srcix = srcf[i][x]; \
162 rdiff = FFABS(srcx - srcix); \
164 if (rdiff > thra || \
171 dst[x] = (sum + ((r + l + 1) >> 1)) / (r + l + 1); \
175 FILTER_ROW(uint8_t, 8)
176 FILTER_ROW(uint16_t, 16)
178 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
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;
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];
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);
207 for (i = 0; i < size; i++)
208 srcf[i] = data[i] + slice_start * linesize[i];
210 for (y = slice_start; y < slice_end; y++) {
211 s->dsp.filter_row(src, dst, srcf, w, mid, size, thra, thrb);
213 dst += out->linesize[p];
214 src += in->linesize[p];
216 for (i = 0; i < size; i++)
217 srcf[i] += linesize[i];
224 static int config_input(AVFilterLink *inlink)
226 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
227 AVFilterContext *ctx = inlink->dst;
228 ATADenoiseContext *s = ctx->priv;
231 s->nb_planes = desc->nb_components;
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;
238 depth = desc->comp[0].depth;
239 s->filter_slice = filter_slice;
241 s->dsp.filter_row = filter_row8;
243 s->dsp.filter_row = filter_row16;
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;
253 ff_atadenoise_init_x86(&s->dsp, depth);
258 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
260 AVFilterContext *ctx = inlink->dst;
261 AVFilterLink *outlink = ctx->outputs[0];
262 ATADenoiseContext *s = ctx->priv;
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);
272 return AVERROR(ENOMEM);
274 ff_bufqueue_add(ctx, &s->q, out);
277 if (s->q.available < s->size) {
278 ff_bufqueue_add(ctx, &s->q, buf);
284 in = ff_bufqueue_peek(&s->q, s->mid);
286 if (!ctx->is_disabled) {
289 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
292 return AVERROR(ENOMEM);
295 for (i = 0; i < s->size; i++) {
296 AVFrame *frame = ff_bufqueue_peek(&s->q, i);
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];
306 td.in = in; td.out = out;
307 ctx->internal->execute(ctx, s->filter_slice, &td, NULL,
308 FFMIN3(s->planeheight[1],
310 ff_filter_get_nb_threads(ctx)));
311 av_frame_copy_props(out, in);
313 out = av_frame_clone(in);
316 return AVERROR(ENOMEM);
320 in = ff_bufqueue_get(&s->q);
322 ff_bufqueue_add(ctx, &s->q, buf);
324 return ff_filter_frame(outlink, out);
327 static int request_frame(AVFilterLink *outlink)
329 AVFilterContext *ctx = outlink->src;
330 ATADenoiseContext *s = ctx->priv;
333 ret = ff_request_frame(ctx->inputs[0]);
335 if (ret == AVERROR_EOF && !ctx->is_disabled && s->available) {
336 AVFrame *buf = av_frame_clone(ff_bufqueue_peek(&s->q, s->available));
338 return AVERROR(ENOMEM);
340 ret = filter_frame(ctx->inputs[0], buf);
347 static av_cold void uninit(AVFilterContext *ctx)
349 ATADenoiseContext *s = ctx->priv;
351 ff_bufqueue_discard_all(&s->q);
354 static const AVFilterPad inputs[] = {
357 .type = AVMEDIA_TYPE_VIDEO,
358 .filter_frame = filter_frame,
359 .config_props = config_input,
364 static const AVFilterPad outputs[] = {
367 .type = AVMEDIA_TYPE_VIDEO,
368 .request_frame = request_frame,
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,
380 .query_formats = query_formats,
383 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,