blob: bf75d53d927afe4ffded2d626ea3e833462dba1f
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 "formats.h" |
37 | #include "internal.h" |
38 | #include "video.h" |
39 | |
40 | #define SIZE FF_BUFQUEUE_SIZE |
41 | |
42 | typedef struct ATADenoiseContext { |
43 | const AVClass *class; |
44 | |
45 | float fthra[4], fthrb[4]; |
46 | int thra[4], thrb[4]; |
47 | |
48 | int planes; |
49 | int nb_planes; |
50 | int planewidth[4]; |
51 | int planeheight[4]; |
52 | |
53 | struct FFBufQueue q; |
54 | void *data[4][SIZE]; |
55 | int linesize[4][SIZE]; |
56 | int size, mid; |
57 | int available; |
58 | |
59 | int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); |
60 | } ATADenoiseContext; |
61 | |
62 | #define OFFSET(x) offsetof(ATADenoiseContext, x) |
63 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
64 | |
65 | static const AVOption atadenoise_options[] = { |
66 | { "0a", "set threshold A for 1st plane", OFFSET(fthra[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0, 0.3, FLAGS }, |
67 | { "0b", "set threshold B for 1st plane", OFFSET(fthrb[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 5.0, FLAGS }, |
68 | { "1a", "set threshold A for 2nd plane", OFFSET(fthra[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0, 0.3, FLAGS }, |
69 | { "1b", "set threshold B for 2nd plane", OFFSET(fthrb[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 5.0, FLAGS }, |
70 | { "2a", "set threshold A for 3rd plane", OFFSET(fthra[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0, 0.3, FLAGS }, |
71 | { "2b", "set threshold B for 3rd plane", OFFSET(fthrb[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 5.0, FLAGS }, |
72 | { "s", "set how many frames to use", OFFSET(size), AV_OPT_TYPE_INT, {.i64=9}, 5, SIZE, FLAGS }, |
73 | { "p", "set what planes to filter", OFFSET(planes), AV_OPT_TYPE_FLAGS, {.i64=7}, 0, 15, FLAGS }, |
74 | { NULL } |
75 | }; |
76 | |
77 | AVFILTER_DEFINE_CLASS(atadenoise); |
78 | |
79 | static int query_formats(AVFilterContext *ctx) |
80 | { |
81 | static const enum AVPixelFormat pixel_fmts[] = { |
82 | AV_PIX_FMT_GRAY8, |
83 | AV_PIX_FMT_GRAY16, |
84 | AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, |
85 | AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, |
86 | AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, |
87 | AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, |
88 | AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, |
89 | AV_PIX_FMT_YUVJ411P, |
90 | AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, |
91 | AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, |
92 | AV_PIX_FMT_YUV440P10, |
93 | AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, |
94 | AV_PIX_FMT_YUV440P12, |
95 | AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14, |
96 | AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, |
97 | AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, |
98 | AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, |
99 | AV_PIX_FMT_NONE |
100 | }; |
101 | AVFilterFormats *formats = ff_make_format_list(pixel_fmts); |
102 | if (!formats) |
103 | return AVERROR(ENOMEM); |
104 | return ff_set_common_formats(ctx, formats); |
105 | } |
106 | |
107 | static av_cold int init(AVFilterContext *ctx) |
108 | { |
109 | ATADenoiseContext *s = ctx->priv; |
110 | |
111 | if (!(s->size & 1)) { |
112 | av_log(ctx, AV_LOG_ERROR, "size %d is invalid. Must be an odd value.\n", s->size); |
113 | return AVERROR(EINVAL); |
114 | } |
115 | s->mid = s->size / 2 + 1; |
116 | |
117 | return 0; |
118 | } |
119 | |
120 | typedef struct ThreadData { |
121 | AVFrame *in, *out; |
122 | } ThreadData; |
123 | |
124 | static int filter_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
125 | { |
126 | ATADenoiseContext *s = ctx->priv; |
127 | ThreadData *td = arg; |
128 | AVFrame *in = td->in; |
129 | AVFrame *out = td->out; |
130 | const int size = s->size; |
131 | const int mid = s->mid; |
132 | int p, x, y, i, j; |
133 | |
134 | for (p = 0; p < s->nb_planes; p++) { |
135 | const int h = s->planeheight[p]; |
136 | const int w = s->planewidth[p]; |
137 | const int slice_start = (h * jobnr) / nb_jobs; |
138 | const int slice_end = (h * (jobnr+1)) / nb_jobs; |
139 | const uint8_t *src = in->data[p] + slice_start * in->linesize[p]; |
140 | uint8_t *dst = out->data[p] + slice_start * out->linesize[p]; |
141 | const int thra = s->thra[p]; |
142 | const int thrb = s->thrb[p]; |
143 | const uint8_t **data = (const uint8_t **)s->data[p]; |
144 | const int *linesize = (const int *)s->linesize[p]; |
145 | const uint8_t *srcf[SIZE]; |
146 | |
147 | if (!((1 << p) & s->planes)) { |
148 | av_image_copy_plane(dst, out->linesize[p], src, in->linesize[p], |
149 | w, slice_end - slice_start); |
150 | continue; |
151 | } |
152 | |
153 | for (i = 0; i < size; i++) |
154 | srcf[i] = data[i] + slice_start * linesize[i]; |
155 | |
156 | for (y = slice_start; y < slice_end; y++) { |
157 | for (x = 0; x < w; x++) { |
158 | const int srcx = src[x]; |
159 | unsigned lsumdiff = 0, rsumdiff = 0; |
160 | unsigned ldiff, rdiff; |
161 | unsigned sum = srcx; |
162 | int l = 0, r = 0; |
163 | int srcjx, srcix; |
164 | |
165 | for (j = mid - 1, i = mid + 1; j >= 0 && i < size; j--, i++) { |
166 | srcjx = srcf[j][x]; |
167 | |
168 | ldiff = FFABS(srcx - srcjx); |
169 | lsumdiff += ldiff; |
170 | if (ldiff > thra || |
171 | lsumdiff > thrb) |
172 | break; |
173 | l++; |
174 | sum += srcjx; |
175 | |
176 | srcix = srcf[i][x]; |
177 | |
178 | rdiff = FFABS(srcx - srcix); |
179 | rsumdiff += rdiff; |
180 | if (rdiff > thra || |
181 | rsumdiff > thrb) |
182 | break; |
183 | r++; |
184 | sum += srcix; |
185 | } |
186 | |
187 | dst[x] = sum / (r + l + 1); |
188 | } |
189 | |
190 | dst += out->linesize[p]; |
191 | src += in->linesize[p]; |
192 | |
193 | for (i = 0; i < size; i++) |
194 | srcf[i] += linesize[i]; |
195 | } |
196 | } |
197 | |
198 | return 0; |
199 | } |
200 | |
201 | static int filter_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
202 | { |
203 | ATADenoiseContext *s = ctx->priv; |
204 | ThreadData *td = arg; |
205 | AVFrame *in = td->in; |
206 | AVFrame *out = td->out; |
207 | const int size = s->size; |
208 | const int mid = s->mid; |
209 | int p, x, y, i, j; |
210 | |
211 | for (p = 0; p < s->nb_planes; p++) { |
212 | const int h = s->planeheight[p]; |
213 | const int w = s->planewidth[p]; |
214 | const int slice_start = (h * jobnr) / nb_jobs; |
215 | const int slice_end = (h * (jobnr+1)) / nb_jobs; |
216 | const uint16_t *src = (uint16_t *)(in->data[p] + slice_start * in->linesize[p]); |
217 | uint16_t *dst = (uint16_t *)(out->data[p] + slice_start * out->linesize[p]); |
218 | const int thra = s->thra[p]; |
219 | const int thrb = s->thrb[p]; |
220 | const uint8_t **data = (const uint8_t **)s->data[p]; |
221 | const int *linesize = (const int *)s->linesize[p]; |
222 | const uint16_t *srcf[SIZE]; |
223 | |
224 | if (!((1 << p) & s->planes)) { |
225 | av_image_copy_plane((uint8_t *)dst, out->linesize[p], (uint8_t *)src, in->linesize[p], |
226 | w * 2, slice_end - slice_start); |
227 | continue; |
228 | } |
229 | |
230 | for (i = 0; i < s->size; i++) |
231 | srcf[i] = (const uint16_t *)(data[i] + slice_start * linesize[i]); |
232 | |
233 | for (y = slice_start; y < slice_end; y++) { |
234 | for (x = 0; x < w; x++) { |
235 | const int srcx = src[x]; |
236 | unsigned lsumdiff = 0, rsumdiff = 0; |
237 | unsigned ldiff, rdiff; |
238 | unsigned sum = srcx; |
239 | int l = 0, r = 0; |
240 | int srcjx, srcix; |
241 | |
242 | for (j = mid - 1, i = mid + 1; j >= 0 && i < size; j--, i++) { |
243 | srcjx = srcf[j][x]; |
244 | |
245 | ldiff = FFABS(srcx - srcjx); |
246 | lsumdiff += ldiff; |
247 | if (ldiff > thra || |
248 | lsumdiff > thrb) |
249 | break; |
250 | l++; |
251 | sum += srcjx; |
252 | |
253 | srcix = srcf[i][x]; |
254 | |
255 | rdiff = FFABS(srcx - srcix); |
256 | rsumdiff += rdiff; |
257 | if (rdiff > thra || |
258 | rsumdiff > thrb) |
259 | break; |
260 | r++; |
261 | sum += srcix; |
262 | } |
263 | |
264 | dst[x] = sum / (r + l + 1); |
265 | } |
266 | |
267 | dst += out->linesize[p] / 2; |
268 | src += in->linesize[p] / 2; |
269 | |
270 | for (i = 0; i < size; i++) |
271 | srcf[i] += linesize[i] / 2; |
272 | } |
273 | } |
274 | |
275 | return 0; |
276 | } |
277 | |
278 | static int config_input(AVFilterLink *inlink) |
279 | { |
280 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
281 | AVFilterContext *ctx = inlink->dst; |
282 | ATADenoiseContext *s = ctx->priv; |
283 | int depth; |
284 | |
285 | s->nb_planes = desc->nb_components; |
286 | |
287 | s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); |
288 | s->planeheight[0] = s->planeheight[3] = inlink->h; |
289 | s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); |
290 | s->planewidth[0] = s->planewidth[3] = inlink->w; |
291 | |
292 | depth = desc->comp[0].depth; |
293 | if (depth == 8) |
294 | s->filter_slice = filter_slice8; |
295 | else |
296 | s->filter_slice = filter_slice16; |
297 | |
298 | s->thra[0] = s->fthra[0] * (1 << depth) - 1; |
299 | s->thra[1] = s->fthra[1] * (1 << depth) - 1; |
300 | s->thra[2] = s->fthra[2] * (1 << depth) - 1; |
301 | s->thrb[0] = s->fthrb[0] * (1 << depth) - 1; |
302 | s->thrb[1] = s->fthrb[1] * (1 << depth) - 1; |
303 | s->thrb[2] = s->fthrb[2] * (1 << depth) - 1; |
304 | |
305 | return 0; |
306 | } |
307 | |
308 | static int filter_frame(AVFilterLink *inlink, AVFrame *buf) |
309 | { |
310 | AVFilterContext *ctx = inlink->dst; |
311 | AVFilterLink *outlink = ctx->outputs[0]; |
312 | ATADenoiseContext *s = ctx->priv; |
313 | AVFrame *out, *in; |
314 | int i; |
315 | |
316 | if (s->q.available != s->size) { |
317 | if (s->q.available < s->mid) { |
318 | for (i = 0; i < s->mid; i++) { |
319 | out = av_frame_clone(buf); |
320 | if (!out) { |
321 | av_frame_free(&buf); |
322 | return AVERROR(ENOMEM); |
323 | } |
324 | ff_bufqueue_add(ctx, &s->q, out); |
325 | } |
326 | } |
327 | if (s->q.available < s->size) { |
328 | ff_bufqueue_add(ctx, &s->q, buf); |
329 | s->available++; |
330 | } |
331 | return 0; |
332 | } |
333 | |
334 | in = ff_bufqueue_peek(&s->q, s->mid); |
335 | |
336 | if (!ctx->is_disabled) { |
337 | ThreadData td; |
338 | |
339 | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
340 | if (!out) { |
341 | av_frame_free(&buf); |
342 | return AVERROR(ENOMEM); |
343 | } |
344 | |
345 | for (i = 0; i < s->size; i++) { |
346 | AVFrame *frame = ff_bufqueue_peek(&s->q, i); |
347 | |
348 | s->data[0][i] = frame->data[0]; |
349 | s->data[1][i] = frame->data[1]; |
350 | s->data[2][i] = frame->data[2]; |
351 | s->linesize[0][i] = frame->linesize[0]; |
352 | s->linesize[1][i] = frame->linesize[1]; |
353 | s->linesize[2][i] = frame->linesize[2]; |
354 | } |
355 | |
356 | td.in = in; td.out = out; |
357 | ctx->internal->execute(ctx, s->filter_slice, &td, NULL, |
358 | FFMIN3(s->planeheight[1], |
359 | s->planeheight[2], |
360 | ff_filter_get_nb_threads(ctx))); |
361 | av_frame_copy_props(out, in); |
362 | } else { |
363 | out = av_frame_clone(in); |
364 | if (!out) { |
365 | av_frame_free(&buf); |
366 | return AVERROR(ENOMEM); |
367 | } |
368 | } |
369 | |
370 | in = ff_bufqueue_get(&s->q); |
371 | av_frame_free(&in); |
372 | ff_bufqueue_add(ctx, &s->q, buf); |
373 | |
374 | return ff_filter_frame(outlink, out); |
375 | } |
376 | |
377 | static int request_frame(AVFilterLink *outlink) |
378 | { |
379 | AVFilterContext *ctx = outlink->src; |
380 | ATADenoiseContext *s = ctx->priv; |
381 | int ret = 0; |
382 | |
383 | ret = ff_request_frame(ctx->inputs[0]); |
384 | |
385 | if (ret == AVERROR_EOF && !ctx->is_disabled && s->available) { |
386 | AVFrame *buf = av_frame_clone(ff_bufqueue_peek(&s->q, s->available)); |
387 | if (!buf) |
388 | return AVERROR(ENOMEM); |
389 | |
390 | ret = filter_frame(ctx->inputs[0], buf); |
391 | s->available--; |
392 | } |
393 | |
394 | return ret; |
395 | } |
396 | |
397 | static av_cold void uninit(AVFilterContext *ctx) |
398 | { |
399 | ATADenoiseContext *s = ctx->priv; |
400 | |
401 | ff_bufqueue_discard_all(&s->q); |
402 | } |
403 | |
404 | static const AVFilterPad inputs[] = { |
405 | { |
406 | .name = "default", |
407 | .type = AVMEDIA_TYPE_VIDEO, |
408 | .filter_frame = filter_frame, |
409 | .config_props = config_input, |
410 | }, |
411 | { NULL } |
412 | }; |
413 | |
414 | static const AVFilterPad outputs[] = { |
415 | { |
416 | .name = "default", |
417 | .type = AVMEDIA_TYPE_VIDEO, |
418 | .request_frame = request_frame, |
419 | }, |
420 | { NULL } |
421 | }; |
422 | |
423 | AVFilter ff_vf_atadenoise = { |
424 | .name = "atadenoise", |
425 | .description = NULL_IF_CONFIG_SMALL("Apply an Adaptive Temporal Averaging Denoiser."), |
426 | .priv_size = sizeof(ATADenoiseContext), |
427 | .priv_class = &atadenoise_class, |
428 | .init = init, |
429 | .uninit = uninit, |
430 | .query_formats = query_formats, |
431 | .inputs = inputs, |
432 | .outputs = outputs, |
433 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, |
434 | }; |
435 |