FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_fftdnoiz.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 0 376 0.0%
Functions: 0 14 0.0%
Branches: 0 187 0.0%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <float.h>
20
21 #include "libavutil/common.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/tx.h"
26 #include "internal.h"
27 #include "window_func.h"
28
29 #define MAX_BLOCK 256
30 #define MAX_THREADS 32
31
32 enum BufferTypes {
33 CURRENT,
34 PREV,
35 NEXT,
36 BSIZE
37 };
38
39 typedef struct PlaneContext {
40 int planewidth, planeheight;
41 int nox, noy;
42 int b;
43 int o;
44 float n;
45
46 float *buffer[MAX_THREADS][BSIZE];
47 AVComplexFloat *hdata[MAX_THREADS], *vdata[MAX_THREADS];
48 AVComplexFloat *hdata_out[MAX_THREADS], *vdata_out[MAX_THREADS];
49 int data_linesize;
50 int buffer_linesize;
51 } PlaneContext;
52
53 typedef struct FFTdnoizContext {
54 const AVClass *class;
55
56 float sigma;
57 float amount;
58 int block_size;
59 float overlap;
60 int method;
61 int window;
62 int nb_prev;
63 int nb_next;
64 int planesf;
65
66 AVFrame *prev, *cur, *next;
67
68 int depth;
69 int nb_planes;
70 int nb_threads;
71 PlaneContext planes[4];
72 float win[MAX_BLOCK][MAX_BLOCK];
73
74 AVTXContext *fft[MAX_THREADS], *ifft[MAX_THREADS];
75 AVTXContext *fft_r[MAX_THREADS], *ifft_r[MAX_THREADS];
76
77 av_tx_fn tx_fn, itx_fn;
78 av_tx_fn tx_r_fn, itx_r_fn;
79
80 void (*import_row)(AVComplexFloat *dst, uint8_t *src, int rw, float scale, float *win, int off);
81 void (*export_row)(AVComplexFloat *src, uint8_t *dst, int rw, int depth, float *win);
82 } FFTdnoizContext;
83
84 #define OFFSET(x) offsetof(FFTdnoizContext, x)
85 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
86 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
87 static const AVOption fftdnoiz_options[] = {
88 { "sigma", "set denoise strength",
89 OFFSET(sigma), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 100, .flags = TFLAGS },
90 { "amount", "set amount of denoising",
91 OFFSET(amount), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0.01, 1, .flags = TFLAGS },
92 { "block", "set block size",
93 OFFSET(block_size), AV_OPT_TYPE_INT, {.i64=32}, 8, MAX_BLOCK, .flags = FLAGS },
94 { "overlap", "set block overlap",
95 OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0.2, 0.8, .flags = FLAGS },
96 { "method", "set method of denoising",
97 OFFSET(method), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags = TFLAGS, "method" },
98 { "wiener", "wiener method",
99 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, .flags = TFLAGS, "method" },
100 { "hard", "hard thresholding",
101 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, .flags = TFLAGS, "method" },
102 { "prev", "set number of previous frames for temporal denoising",
103 OFFSET(nb_prev), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags = FLAGS },
104 { "next", "set number of next frames for temporal denoising",
105 OFFSET(nb_next), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags = FLAGS },
106 { "planes", "set planes to filter",
107 OFFSET(planesf), AV_OPT_TYPE_INT, {.i64=7}, 0, 15, .flags = TFLAGS },
108 WIN_FUNC_OPTION("window", OFFSET(window), FLAGS, WFUNC_HANNING),
109 { NULL }
110 };
111
112 AVFILTER_DEFINE_CLASS(fftdnoiz);
113
114 static const enum AVPixelFormat pix_fmts[] = {
115 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
116 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
117 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
118 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
119 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
120 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
121 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
122 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
123 AV_PIX_FMT_YUVJ411P,
124 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
125 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
126 AV_PIX_FMT_YUV440P10,
127 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
128 AV_PIX_FMT_YUV440P12,
129 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
130 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
131 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
132 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
133 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
134 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
135 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
136 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
137 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
138 AV_PIX_FMT_NONE
139 };
140
141 typedef struct ThreadData {
142 float *src, *dst;
143 } ThreadData;
144
145 static void import_row8(AVComplexFloat *dst, uint8_t *src, int rw,
146 float scale, float *win, int off)
147 {
148 for (int j = 0; j < rw; j++) {
149 const int i = abs(j + off);
150 dst[j].re = src[i] * scale * win[j];
151 dst[j].im = 0.f;
152 }
153 }
154
155 static void export_row8(AVComplexFloat *src, uint8_t *dst, int rw, int depth, float *win)
156 {
157 for (int j = 0; j < rw; j++)
158 dst[j] = av_clip_uint8(lrintf(src[j].re / win[j]));
159 }
160
161 static void import_row16(AVComplexFloat *dst, uint8_t *srcp, int rw,
162 float scale, float *win, int off)
163 {
164 uint16_t *src = (uint16_t *)srcp;
165
166 for (int j = 0; j < rw; j++) {
167 const int i = abs(j + off);
168 dst[j].re = src[i] * scale * win[j];
169 dst[j].im = 0;
170 }
171 }
172
173 static void export_row16(AVComplexFloat *src, uint8_t *dstp, int rw, int depth, float *win)
174 {
175 uint16_t *dst = (uint16_t *)dstp;
176
177 for (int j = 0; j < rw; j++)
178 dst[j] = av_clip_uintp2_c(lrintf(src[j].re / win[j]), depth);
179 }
180
181 static int config_input(AVFilterLink *inlink)
182 {
183 AVFilterContext *ctx = inlink->dst;
184 const AVPixFmtDescriptor *desc;
185 FFTdnoizContext *s = ctx->priv;
186 float lut[MAX_BLOCK + 1];
187 float overlap;
188 int i;
189
190 desc = av_pix_fmt_desc_get(inlink->format);
191 s->depth = desc->comp[0].depth;
192
193 if (s->depth <= 8) {
194 s->import_row = import_row8;
195 s->export_row = export_row8;
196 } else {
197 s->import_row = import_row16;
198 s->export_row = export_row16;
199 }
200
201 s->planes[1].planewidth = s->planes[2].planewidth = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
202 s->planes[0].planewidth = s->planes[3].planewidth = inlink->w;
203 s->planes[1].planeheight = s->planes[2].planeheight = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
204 s->planes[0].planeheight = s->planes[3].planeheight = inlink->h;
205
206 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
207 s->nb_threads = FFMIN(ff_filter_get_nb_threads(ctx), MAX_THREADS);
208
209 for (int i = 0; i < s->nb_threads; i++) {
210 float scale = 1.f, iscale = 1.f;
211 int ret;
212
213 if ((ret = av_tx_init(&s->fft[i], &s->tx_fn, AV_TX_FLOAT_FFT,
214 0, s->block_size, &scale, 0)) < 0 ||
215 (ret = av_tx_init(&s->ifft[i], &s->itx_fn, AV_TX_FLOAT_FFT,
216 1, s->block_size, &iscale, 0)) < 0 ||
217 (ret = av_tx_init(&s->fft_r[i], &s->tx_r_fn, AV_TX_FLOAT_FFT,
218 0, 1 + s->nb_prev + s->nb_next, &scale, 0)) < 0 ||
219 (ret = av_tx_init(&s->ifft_r[i], &s->itx_r_fn, AV_TX_FLOAT_FFT,
220 1, 1 + s->nb_prev + s->nb_next, &iscale, 0)) < 0)
221 return ret;
222 }
223
224 for (i = 0; i < s->nb_planes; i++) {
225 PlaneContext *p = &s->planes[i];
226 int size;
227
228 p->b = s->block_size;
229 p->n = 1.f / (p->b * p->b);
230 p->o = lrintf(p->b * s->overlap);
231 size = p->b - p->o;
232 p->nox = (p->planewidth + (size - 1)) / size;
233 p->noy = (p->planeheight + (size - 1)) / size;
234
235 av_log(ctx, AV_LOG_DEBUG, "nox:%d noy:%d size:%d\n", p->nox, p->noy, size);
236
237 p->buffer_linesize = p->b * sizeof(AVComplexFloat);
238 p->data_linesize = 2 * p->b * sizeof(float);
239 for (int j = 0; j < s->nb_threads; j++) {
240 p->hdata[j] = av_calloc(p->b, p->data_linesize);
241 p->hdata_out[j] = av_calloc(p->b, p->data_linesize);
242 p->vdata[j] = av_calloc(p->b, p->data_linesize);
243 p->vdata_out[j] = av_calloc(p->b, p->data_linesize);
244 p->buffer[j][CURRENT] = av_calloc(p->b, p->buffer_linesize);
245 if (!p->buffer[j][CURRENT])
246 return AVERROR(ENOMEM);
247 if (s->nb_prev > 0) {
248 p->buffer[j][PREV] = av_calloc(p->b, p->buffer_linesize);
249 if (!p->buffer[j][PREV])
250 return AVERROR(ENOMEM);
251 }
252 if (s->nb_next > 0) {
253 p->buffer[j][NEXT] = av_calloc(p->b, p->buffer_linesize);
254 if (!p->buffer[j][NEXT])
255 return AVERROR(ENOMEM);
256 }
257 if (!p->hdata[j] || !p->vdata[j] ||
258 !p->hdata_out[j] || !p->vdata_out[j])
259 return AVERROR(ENOMEM);
260 }
261 }
262
263 generate_window_func(lut, s->block_size + 1, s->window, &overlap);
264
265 for (int y = 0; y < s->block_size; y++) {
266 for (int x = 0; x < s->block_size; x++)
267 s->win[y][x] = lut[y] * lut[x];
268 }
269
270 return 0;
271 }
272
273 static void import_block(FFTdnoizContext *s,
274 uint8_t *srcp, int src_linesize,
275 float *buffer, int buffer_linesize, int plane,
276 int jobnr, int y, int x)
277 {
278 PlaneContext *p = &s->planes[plane];
279 const int width = p->planewidth;
280 const int height = p->planeheight;
281 const int block = p->b;
282 const int overlap = p->o;
283 const int hoverlap = overlap / 2;
284 const int size = block - overlap;
285 const int bpp = (s->depth + 7) / 8;
286 const int data_linesize = p->data_linesize / sizeof(AVComplexFloat);
287 const float scale = 1.f / ((1.f + s->nb_prev + s->nb_next) * s->block_size * s->block_size);
288 AVComplexFloat *hdata = p->hdata[jobnr];
289 AVComplexFloat *hdata_out = p->hdata_out[jobnr];
290 AVComplexFloat *vdata_out = p->vdata_out[jobnr];
291 const int woff = -hoverlap;
292 const int hoff = -hoverlap;
293 const int rh = FFMIN(block, height - y * size + hoverlap);
294 const int rw = FFMIN(block, width - x * size + hoverlap);
295 AVComplexFloat *ssrc, *ddst, *dst = hdata, *dst_out = hdata_out;
296 float *bdst = buffer;
297
298 buffer_linesize /= sizeof(float);
299
300 for (int i = 0; i < rh; i++) {
301 uint8_t *src = srcp + src_linesize * abs(y * size + i + hoff) + x * size * bpp;
302
303 s->import_row(dst, src, rw, scale, s->win[i], woff);
304 for (int j = rw; j < block; j++) {
305 dst[j].re = dst[rw - 1].re;
306 dst[j].im = 0.f;
307 }
308 s->tx_fn(s->fft[jobnr], dst_out, dst, sizeof(AVComplexFloat));
309
310 ddst = dst_out;
311 dst += data_linesize;
312 dst_out += data_linesize;
313 }
314
315 dst = dst_out;
316 for (int i = rh; i < block; i++) {
317 for (int j = 0; j < block; j++) {
318 dst[j].re = ddst[j].re;
319 dst[j].im = ddst[j].im;
320 }
321
322 dst += data_linesize;
323 }
324
325 ssrc = hdata_out;
326 dst = vdata_out;
327 for (int i = 0; i < block; i++) {
328 for (int j = 0; j < block; j++)
329 dst[j] = ssrc[j * data_linesize + i];
330 s->tx_fn(s->fft[jobnr], bdst, dst, sizeof(AVComplexFloat));
331
332 dst += data_linesize;
333 bdst += buffer_linesize;
334 }
335 }
336
337 static void export_block(FFTdnoizContext *s,
338 uint8_t *dstp, int dst_linesize,
339 float *buffer, int buffer_linesize, int plane,
340 int jobnr, int y, int x)
341 {
342 PlaneContext *p = &s->planes[plane];
343 const int depth = s->depth;
344 const int bpp = (depth + 7) / 8;
345 const int width = p->planewidth;
346 const int height = p->planeheight;
347 const int block = p->b;
348 const int overlap = p->o;
349 const int hoverlap = overlap / 2;
350 const int size = block - overlap;
351 const int data_linesize = p->data_linesize / sizeof(AVComplexFloat);
352 AVComplexFloat *hdata = p->hdata[jobnr];
353 AVComplexFloat *hdata_out = p->hdata_out[jobnr];
354 AVComplexFloat *vdata_out = p->vdata_out[jobnr];
355 const int rw = FFMIN(size, width - x * size);
356 const int rh = FFMIN(size, height - y * size);
357 AVComplexFloat *hdst, *vdst = vdata_out, *hdst_out = hdata_out;
358 float *bsrc = buffer;
359
360 hdst = hdata;
361 buffer_linesize /= sizeof(float);
362
363 for (int i = 0; i < block; i++) {
364 s->itx_fn(s->ifft[jobnr], vdst, bsrc, sizeof(AVComplexFloat));
365 for (int j = 0; j < block; j++)
366 hdst[j * data_linesize + i] = vdst[j];
367
368 vdst += data_linesize;
369 bsrc += buffer_linesize;
370 }
371
372 hdst = hdata + hoverlap * data_linesize;
373 for (int i = 0; i < rh && (y * size + i) < height; i++) {
374 uint8_t *dst = dstp + dst_linesize * (y * size + i) + x * size * bpp;
375
376 s->itx_fn(s->ifft[jobnr], hdst_out, hdst, sizeof(AVComplexFloat));
377 s->export_row(hdst_out + hoverlap, dst, rw, depth, s->win[i + hoverlap] + hoverlap);
378
379 hdst += data_linesize;
380 hdst_out += data_linesize;
381 }
382 }
383
384 static void filter_block3d2(FFTdnoizContext *s, int plane, float *pbuffer, float *nbuffer,
385 int jobnr)
386 {
387 PlaneContext *p = &s->planes[plane];
388 const int block = p->b;
389 const int buffer_linesize = p->buffer_linesize / sizeof(float);
390 const float depthx = (1 << (s->depth - 8)) * (1 << (s->depth - 8));
391 const float sigma = s->sigma * depthx / (3.f * s->block_size * s->block_size);
392 const float limit = 1.f - s->amount;
393 float *cbuffer = p->buffer[jobnr][CURRENT];
394 const int method = s->method;
395 float *cbuff = cbuffer;
396 float *pbuff = pbuffer;
397 float *nbuff = nbuffer;
398
399 for (int i = 0; i < block; i++) {
400 for (int j = 0; j < block; j++) {
401 AVComplexFloat buffer[BSIZE];
402 AVComplexFloat outbuffer[BSIZE];
403
404 buffer[0].re = pbuff[2 * j ];
405 buffer[0].im = pbuff[2 * j + 1];
406
407 buffer[1].re = cbuff[2 * j ];
408 buffer[1].im = cbuff[2 * j + 1];
409
410 buffer[2].re = nbuff[2 * j ];
411 buffer[2].im = nbuff[2 * j + 1];
412
413 s->tx_r_fn(s->fft_r[jobnr], outbuffer, buffer, sizeof(AVComplexFloat));
414
415 for (int z = 0; z < 3; z++) {
416 const float re = outbuffer[z].re;
417 const float im = outbuffer[z].im;
418 const float power = re * re + im * im;
419 float factor;
420
421 switch (method) {
422 case 0:
423 factor = fmaxf(limit, (power - sigma) / (power + 1e-15f));
424 break;
425 case 1:
426 factor = power < sigma ? limit : 1.f;
427 break;
428 }
429
430 outbuffer[z].re *= factor;
431 outbuffer[z].im *= factor;
432 }
433
434 s->itx_r_fn(s->ifft_r[jobnr], buffer, outbuffer, sizeof(AVComplexFloat));
435
436 cbuff[2 * j + 0] = buffer[1].re;
437 cbuff[2 * j + 1] = buffer[1].im;
438 }
439
440 cbuff += buffer_linesize;
441 pbuff += buffer_linesize;
442 nbuff += buffer_linesize;
443 }
444 }
445
446 static void filter_block3d1(FFTdnoizContext *s, int plane, float *pbuffer,
447 int jobnr)
448 {
449 PlaneContext *p = &s->planes[plane];
450 const int block = p->b;
451 const int buffer_linesize = p->buffer_linesize / sizeof(float);
452 const float depthx = (1 << (s->depth - 8)) * (1 << (s->depth - 8));
453 const float sigma = s->sigma * depthx / (2.f * s->block_size * s->block_size);
454 const float limit = 1.f - s->amount;
455 float *cbuffer = p->buffer[jobnr][CURRENT];
456 const int method = s->method;
457 float *cbuff = cbuffer;
458 float *pbuff = pbuffer;
459
460 for (int i = 0; i < block; i++) {
461 for (int j = 0; j < block; j++) {
462 AVComplexFloat buffer[BSIZE];
463 AVComplexFloat outbuffer[BSIZE];
464
465 buffer[0].re = pbuff[2 * j ];
466 buffer[0].im = pbuff[2 * j + 1];
467
468 buffer[1].re = cbuff[2 * j ];
469 buffer[1].im = cbuff[2 * j + 1];
470
471 s->tx_r_fn(s->fft_r[jobnr], outbuffer, buffer, sizeof(AVComplexFloat));
472
473 for (int z = 0; z < 2; z++) {
474 const float re = outbuffer[z].re;
475 const float im = outbuffer[z].im;
476 const float power = re * re + im * im;
477 float factor;
478
479 switch (method) {
480 case 0:
481 factor = fmaxf(limit, (power - sigma) / (power + 1e-15f));
482 break;
483 case 1:
484 factor = power < sigma ? limit : 1.f;
485 break;
486 }
487
488 outbuffer[z].re *= factor;
489 outbuffer[z].im *= factor;
490 }
491
492 s->itx_r_fn(s->ifft_r[jobnr], buffer, outbuffer, sizeof(AVComplexFloat));
493
494 cbuff[2 * j + 0] = buffer[1].re;
495 cbuff[2 * j + 1] = buffer[1].im;
496 }
497
498 cbuff += buffer_linesize;
499 pbuff += buffer_linesize;
500 }
501 }
502
503 static void filter_block2d(FFTdnoizContext *s, int plane,
504 int jobnr)
505 {
506 PlaneContext *p = &s->planes[plane];
507 const int block = p->b;
508 const int method = s->method;
509 const int buffer_linesize = p->buffer_linesize / sizeof(float);
510 const float depthx = (1 << (s->depth - 8)) * (1 << (s->depth - 8));
511 const float sigma = s->sigma * depthx / (s->block_size * s->block_size);
512 const float limit = 1.f - s->amount;
513 float *buff = p->buffer[jobnr][CURRENT];
514
515 for (int i = 0; i < block; i++) {
516 for (int j = 0; j < block; j++) {
517 float factor, power, re, im;
518
519 re = buff[j * 2 ];
520 im = buff[j * 2 + 1];
521 power = re * re + im * im;
522 switch (method) {
523 case 0:
524 factor = fmaxf(limit, (power - sigma) / (power + 1e-15f));
525 break;
526 case 1:
527 factor = power < sigma ? limit : 1.f;
528 break;
529 }
530
531 buff[j * 2 ] *= factor;
532 buff[j * 2 + 1] *= factor;
533 }
534
535 buff += buffer_linesize;
536 }
537 }
538
539 static int denoise(AVFilterContext *ctx, void *arg,
540 int jobnr, int nb_jobs)
541 {
542 FFTdnoizContext *s = ctx->priv;
543 AVFrame *out = arg;
544
545 for (int plane = 0; plane < s->nb_planes; plane++) {
546 PlaneContext *p = &s->planes[plane];
547 const int nox = p->nox;
548 const int noy = p->noy;
549 const int slice_start = (noy * jobnr) / nb_jobs;
550 const int slice_end = (noy * (jobnr+1)) / nb_jobs;
551
552 if (!((1 << plane) & s->planesf) || ctx->is_disabled)
553 continue;
554
555 for (int y = slice_start; y < slice_end; y++) {
556 for (int x = 0; x < nox; x++) {
557 if (s->next) {
558 import_block(s, s->next->data[plane], s->next->linesize[plane],
559 p->buffer[jobnr][NEXT], p->buffer_linesize, plane,
560 jobnr, y, x);
561 }
562
563 if (s->prev) {
564 import_block(s, s->prev->data[plane], s->prev->linesize[plane],
565 p->buffer[jobnr][PREV], p->buffer_linesize, plane,
566 jobnr, y, x);
567 }
568
569 import_block(s, s->cur->data[plane], s->cur->linesize[plane],
570 p->buffer[jobnr][CURRENT], p->buffer_linesize, plane,
571 jobnr, y, x);
572
573 if (s->next && s->prev) {
574 filter_block3d2(s, plane, p->buffer[jobnr][PREV], p->buffer[jobnr][NEXT], jobnr);
575 } else if (s->next) {
576 filter_block3d1(s, plane, p->buffer[jobnr][NEXT], jobnr);
577 } else if (s->prev) {
578 filter_block3d1(s, plane, p->buffer[jobnr][PREV], jobnr);
579 } else {
580 filter_block2d(s, plane, jobnr);
581 }
582
583 export_block(s, out->data[plane], out->linesize[plane],
584 p->buffer[jobnr][CURRENT], p->buffer_linesize, plane,
585 jobnr, y, x);
586 }
587 }
588 }
589
590 return 0;
591 }
592
593 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
594 {
595 AVFilterContext *ctx = inlink->dst;
596 FFTdnoizContext *s = ctx->priv;
597 AVFilterLink *outlink = ctx->outputs[0];
598 int direct, plane;
599 AVFrame *out;
600
601 if (s->nb_next > 0 && s->nb_prev > 0) {
602 av_frame_free(&s->prev);
603 s->prev = s->cur;
604 s->cur = s->next;
605 s->next = in;
606
607 if (!s->prev && s->cur) {
608 s->prev = av_frame_clone(s->cur);
609 if (!s->prev)
610 return AVERROR(ENOMEM);
611 }
612 if (!s->cur)
613 return 0;
614 } else if (s->nb_next > 0) {
615 av_frame_free(&s->cur);
616 s->cur = s->next;
617 s->next = in;
618
619 if (!s->cur)
620 return 0;
621 } else if (s->nb_prev > 0) {
622 av_frame_free(&s->prev);
623 s->prev = s->cur;
624 s->cur = in;
625
626 if (!s->prev)
627 s->prev = av_frame_clone(s->cur);
628 if (!s->prev)
629 return AVERROR(ENOMEM);
630 } else {
631 s->cur = in;
632 }
633
634 if (av_frame_is_writable(in) && s->nb_next == 0 && s->nb_prev == 0) {
635 direct = 1;
636 out = in;
637 } else {
638 direct = 0;
639 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
640 if (!out)
641 return AVERROR(ENOMEM);
642 av_frame_copy_props(out, s->cur);
643 }
644
645 ff_filter_execute(ctx, denoise, out, NULL,
646 FFMIN(s->planes[0].noy, s->nb_threads));
647
648 for (plane = 0; plane < s->nb_planes; plane++) {
649 PlaneContext *p = &s->planes[plane];
650
651 if (!((1 << plane) & s->planesf) || ctx->is_disabled) {
652 if (!direct)
653 av_image_copy_plane(out->data[plane], out->linesize[plane],
654 s->cur->data[plane], s->cur->linesize[plane],
655 p->planewidth * (1 + (s->depth > 8)), p->planeheight);
656 continue;
657 }
658 }
659
660 if (s->nb_next == 0 && s->nb_prev == 0) {
661 if (direct) {
662 s->cur = NULL;
663 } else {
664 av_frame_free(&s->cur);
665 }
666 }
667 return ff_filter_frame(outlink, out);
668 }
669
670 static int request_frame(AVFilterLink *outlink)
671 {
672 AVFilterContext *ctx = outlink->src;
673 FFTdnoizContext *s = ctx->priv;
674 int ret = 0;
675
676 ret = ff_request_frame(ctx->inputs[0]);
677
678 if (ret == AVERROR_EOF && (s->nb_next > 0)) {
679 AVFrame *buf;
680
681 if (s->next && s->nb_next > 0)
682 buf = av_frame_clone(s->next);
683 else if (s->cur)
684 buf = av_frame_clone(s->cur);
685 else
686 buf = av_frame_clone(s->prev);
687 if (!buf)
688 return AVERROR(ENOMEM);
689
690 ret = filter_frame(ctx->inputs[0], buf);
691 if (ret < 0)
692 return ret;
693 ret = AVERROR_EOF;
694 }
695
696 return ret;
697 }
698
699 static av_cold void uninit(AVFilterContext *ctx)
700 {
701 FFTdnoizContext *s = ctx->priv;
702 int i;
703
704 for (i = 0; i < 4; i++) {
705 PlaneContext *p = &s->planes[i];
706
707 for (int j = 0; j < s->nb_threads; j++) {
708 av_freep(&p->hdata[j]);
709 av_freep(&p->vdata[j]);
710 av_freep(&p->hdata_out[j]);
711 av_freep(&p->vdata_out[j]);
712 av_freep(&p->buffer[j][PREV]);
713 av_freep(&p->buffer[j][CURRENT]);
714 av_freep(&p->buffer[j][NEXT]);
715 }
716 }
717
718 for (i = 0; i < s->nb_threads; i++) {
719 av_tx_uninit(&s->fft[i]);
720 av_tx_uninit(&s->ifft[i]);
721 av_tx_uninit(&s->fft_r[i]);
722 av_tx_uninit(&s->ifft_r[i]);
723 }
724
725 av_frame_free(&s->prev);
726 av_frame_free(&s->cur);
727 av_frame_free(&s->next);
728 }
729
730 static const AVFilterPad fftdnoiz_inputs[] = {
731 {
732 .name = "default",
733 .type = AVMEDIA_TYPE_VIDEO,
734 .filter_frame = filter_frame,
735 .config_props = config_input,
736 },
737 };
738
739 static const AVFilterPad fftdnoiz_outputs[] = {
740 {
741 .name = "default",
742 .type = AVMEDIA_TYPE_VIDEO,
743 .request_frame = request_frame,
744 },
745 };
746
747 const AVFilter ff_vf_fftdnoiz = {
748 .name = "fftdnoiz",
749 .description = NULL_IF_CONFIG_SMALL("Denoise frames using 3D FFT."),
750 .priv_size = sizeof(FFTdnoizContext),
751 .uninit = uninit,
752 FILTER_INPUTS(fftdnoiz_inputs),
753 FILTER_OUTPUTS(fftdnoiz_outputs),
754 FILTER_PIXFMTS_ARRAY(pix_fmts),
755 .priv_class = &fftdnoiz_class,
756 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
757 AVFILTER_FLAG_SLICE_THREADS,
758 .process_command = ff_filter_process_command,
759 };
760