]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_fftdnoiz.c
Merge commit 'eec93e57096aa4804862d62760442380c70d489b'
[ffmpeg] / libavfilter / vf_fftdnoiz.c
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/avassert.h"
22 #include "libavutil/common.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
26 #include "internal.h"
27 #include "libavcodec/avfft.h"
28
29 enum BufferTypes {
30     CURRENT,
31     PREV,
32     NEXT,
33     BSIZE
34 };
35
36 typedef struct PlaneContext {
37     int planewidth, planeheight;
38     int nox, noy;
39     int b;
40     int o;
41     float n;
42
43     float *buffer[BSIZE];
44     FFTComplex *hdata, *vdata;
45     int data_linesize;
46     int buffer_linesize;
47
48     FFTContext *fft, *ifft;
49 } PlaneContext;
50
51 typedef struct FFTdnoizContext {
52     const AVClass *class;
53
54     float sigma;
55     float amount;
56     int   block_bits;
57     float overlap;
58     int   nb_prev;
59     int   nb_next;
60     int   planesf;
61
62     AVFrame *prev, *cur, *next;
63
64     int depth;
65     int nb_planes;
66     PlaneContext planes[4];
67
68     void (*import_row)(FFTComplex *dst, uint8_t *src, int rw);
69     void (*export_row)(FFTComplex *src, uint8_t *dst, int rw, float scale, int depth);
70 } FFTdnoizContext;
71
72 #define OFFSET(x) offsetof(FFTdnoizContext, x)
73 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
74 static const AVOption fftdnoiz_options[] = {
75     { "sigma",   "set denoise strength",
76         OFFSET(sigma),      AV_OPT_TYPE_FLOAT, {.dbl=1},        0,  30, .flags = FLAGS },
77     { "amount",  "set amount of denoising",
78         OFFSET(amount),     AV_OPT_TYPE_FLOAT, {.dbl=1},     0.01,   1, .flags = FLAGS },
79     { "block",   "set block log2(size)",
80         OFFSET(block_bits), AV_OPT_TYPE_INT,   {.i64=4},        3,   6, .flags = FLAGS },
81     { "overlap", "set block overlap",
82         OFFSET(overlap),    AV_OPT_TYPE_FLOAT, {.dbl=0.5},    0.2, 0.8, .flags = FLAGS },
83     { "prev",    "set number of previous frames for temporal denoising",
84         OFFSET(nb_prev),    AV_OPT_TYPE_INT,   {.i64=0},        0,   1, .flags = FLAGS },
85     { "next",    "set number of next frames for temporal denoising",
86         OFFSET(nb_next),    AV_OPT_TYPE_INT,   {.i64=0},        0,   1, .flags = FLAGS },
87     { "planes",  "set planes to filter",
88         OFFSET(planesf),    AV_OPT_TYPE_INT,   {.i64=7},        0,  15, .flags = FLAGS },
89     { NULL }
90 };
91
92 AVFILTER_DEFINE_CLASS(fftdnoiz);
93
94 static av_cold int init(AVFilterContext *ctx)
95 {
96     FFTdnoizContext *s = ctx->priv;
97     int i;
98
99     for (i = 0; i < 4; i++) {
100         PlaneContext *p = &s->planes[i];
101
102         p->fft  = av_fft_init(s->block_bits, 0);
103         p->ifft = av_fft_init(s->block_bits, 1);
104         if (!p->fft || !p->ifft)
105             return AVERROR(ENOMEM);
106     }
107
108     return 0;
109 }
110
111 static int query_formats(AVFilterContext *ctx)
112 {
113     static const enum AVPixelFormat pix_fmts[] = {
114         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
115         AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
116         AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
117         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
118         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
119         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
120         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
121         AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
122         AV_PIX_FMT_YUVJ411P,
123         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
124         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
125         AV_PIX_FMT_YUV440P10,
126         AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
127         AV_PIX_FMT_YUV440P12,
128         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
129         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
130         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
131         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
132         AV_PIX_FMT_NONE
133     };
134     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
135     if (!fmts_list)
136         return AVERROR(ENOMEM);
137     return ff_set_common_formats(ctx, fmts_list);
138 }
139
140 typedef struct ThreadData {
141     float *src, *dst;
142 } ThreadData;
143
144 static void import_row8(FFTComplex *dst, uint8_t *src, int rw)
145 {
146     int j;
147
148     for (j = 0; j < rw; j++) {
149         dst[j].re = src[j];
150         dst[j].im = 0;
151     }
152 }
153
154 static void export_row8(FFTComplex *src, uint8_t *dst, int rw, float scale, int depth)
155 {
156     int j;
157
158     for (j = 0; j < rw; j++)
159         dst[j] = av_clip_uint8(src[j].re * scale);
160 }
161
162 static void import_row16(FFTComplex *dst, uint8_t *srcp, int rw)
163 {
164     uint16_t *src = (uint16_t *)srcp;
165     int j;
166
167     for (j = 0; j < rw; j++) {
168         dst[j].re = src[j];
169         dst[j].im = 0;
170     }
171 }
172
173 static void export_row16(FFTComplex *src, uint8_t *dstp, int rw, float scale, int depth)
174 {
175     uint16_t *dst = (uint16_t *)dstp;
176     int j;
177
178     for (j = 0; j < rw; j++)
179         dst[j] = av_clip_uintp2_c(src[j].re * scale, depth);
180 }
181
182 static int config_input(AVFilterLink *inlink)
183 {
184     AVFilterContext *ctx = inlink->dst;
185     const AVPixFmtDescriptor *desc;
186     FFTdnoizContext *s = ctx->priv;
187     int i;
188
189     desc = av_pix_fmt_desc_get(inlink->format);
190     s->depth = desc->comp[0].depth;
191
192     if (s->depth <= 8) {
193         s->import_row = import_row8;
194         s->export_row = export_row8;
195     } else {
196         s->import_row = import_row16;
197         s->export_row = export_row16;
198         s->sigma *= 1 << (s->depth - 8) * (1 + s->nb_prev + s->nb_next);
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
208     for (i = 0; i < s->nb_planes; i++) {
209         PlaneContext *p = &s->planes[i];
210         int size;
211
212         p->b = 1 << s->block_bits;
213         p->n = 1.f / (p->b * p->b);
214         p->o = p->b * s->overlap;
215         size = p->b - p->o;
216         p->nox = (p->planewidth  + (size - 1)) / size;
217         p->noy = (p->planeheight + (size - 1)) / size;
218
219         av_log(ctx, AV_LOG_DEBUG, "nox:%d noy:%d size:%d\n", p->nox, p->noy, size);
220
221         p->buffer_linesize = p->b * p->nox * sizeof(FFTComplex);
222         p->buffer[CURRENT] = av_calloc(p->b * p->noy, p->buffer_linesize);
223         if (!p->buffer[CURRENT])
224             return AVERROR(ENOMEM);
225         if (s->nb_prev > 0) {
226             p->buffer[PREV] = av_calloc(p->b * p->noy, p->buffer_linesize);
227             if (!p->buffer[PREV])
228                 return AVERROR(ENOMEM);
229         }
230         if (s->nb_next > 0) {
231             p->buffer[NEXT] = av_calloc(p->b * p->noy, p->buffer_linesize);
232             if (!p->buffer[NEXT])
233                 return AVERROR(ENOMEM);
234         }
235         p->data_linesize = 2 * p->b * sizeof(float);
236         p->hdata = av_calloc(p->b, p->data_linesize);
237         p->vdata = av_calloc(p->b, p->data_linesize);
238         if (!p->hdata || !p->vdata)
239             return AVERROR(ENOMEM);
240     }
241
242     return 0;
243 }
244
245 static void import_plane(FFTdnoizContext *s,
246                          uint8_t *srcp, int src_linesize,
247                          float *buffer, int buffer_linesize, int plane)
248 {
249     PlaneContext *p = &s->planes[plane];
250     const int width = p->planewidth;
251     const int height = p->planeheight;
252     const int block = p->b;
253     const int overlap = p->o;
254     const int size = block - overlap;
255     const int nox = p->nox;
256     const int noy = p->noy;
257     const int bpp = (s->depth + 7) / 8;
258     const int data_linesize = p->data_linesize / sizeof(FFTComplex);
259     FFTComplex *hdata = p->hdata;
260     FFTComplex *vdata = p->vdata;
261     int x, y, i, j;
262
263     buffer_linesize /= sizeof(float);
264     for (y = 0; y < noy; y++) {
265         for (x = 0; x < nox; x++) {
266             const int rh = FFMIN(block, height - y * size);
267             const int rw = FFMIN(block, width  - x * size);
268             uint8_t *src = srcp + src_linesize * y * size + x * size * bpp;
269             float *bdst = buffer + buffer_linesize * y * block + x * block * 2;
270             FFTComplex *ssrc, *dst = hdata;
271
272             for (i = 0; i < rh; i++) {
273                 s->import_row(dst, src, rw);
274                 for (j = rw; j < block; j++) {
275                     dst[j].re = dst[block - j - 1].re;
276                     dst[j].im = 0;
277                 }
278                 av_fft_permute(p->fft, dst);
279                 av_fft_calc(p->fft, dst);
280
281                 src += src_linesize;
282                 dst += data_linesize;
283             }
284
285             dst = hdata;
286             for (; i < block; i++) {
287                 for (j = 0; j < block; j++) {
288                     dst[j].re = dst[(block - i - 1) * data_linesize + j].re;
289                     dst[j].im = dst[(block - i - 1) * data_linesize + j].im;
290                 }
291             }
292
293             ssrc = hdata;
294             dst = vdata;
295             for (i = 0; i < block; i++) {
296                 for (j = 0; j < block; j++)
297                     dst[j] = ssrc[j * data_linesize + i];
298                 av_fft_permute(p->fft, dst);
299                 av_fft_calc(p->fft, dst);
300                 memcpy(bdst, dst, block * sizeof(FFTComplex));
301
302                 dst += data_linesize;
303                 bdst += buffer_linesize;
304             }
305         }
306     }
307 }
308
309 static void export_plane(FFTdnoizContext *s,
310                          uint8_t *dstp, int dst_linesize,
311                          float *buffer, int buffer_linesize, int plane)
312 {
313     PlaneContext *p = &s->planes[plane];
314     const int depth = s->depth;
315     const int bpp = (depth + 7) / 8;
316     const int width = p->planewidth;
317     const int height = p->planeheight;
318     const int block = p->b;
319     const int overlap = p->o;
320     const int hoverlap = overlap / 2;
321     const int size = block - overlap;
322     const int nox = p->nox;
323     const int noy = p->noy;
324     const int data_linesize = p->data_linesize / sizeof(FFTComplex);
325     const float scale = 1.f / (block * block);
326     FFTComplex *hdata = p->hdata;
327     FFTComplex *vdata = p->vdata;
328     int x, y, i, j;
329
330     buffer_linesize /= sizeof(float);
331     for (y = 0; y < noy; y++) {
332         for (x = 0; x < nox; x++) {
333             const int woff = x == 0 ? 0 : hoverlap;
334             const int hoff = y == 0 ? 0 : hoverlap;
335             const int rw = x == 0 ? block : FFMIN(size, width  - x * size - woff);
336             const int rh = y == 0 ? block : FFMIN(size, height - y * size - hoff);
337             float *bsrc = buffer + buffer_linesize * y * block + x * block * 2;
338             uint8_t *dst = dstp + dst_linesize * (y * size + hoff) + (x * size + woff) * bpp;
339             FFTComplex *hdst, *ddst = vdata;
340
341             hdst = hdata;
342             for (i = 0; i < block; i++) {
343                 memcpy(ddst, bsrc, block * sizeof(FFTComplex));
344                 av_fft_permute(p->ifft, ddst);
345                 av_fft_calc(p->ifft, ddst);
346                 for (j = 0; j < block; j++) {
347                     hdst[j * data_linesize + i] = ddst[j];
348                 }
349
350                 ddst += data_linesize;
351                 bsrc += buffer_linesize;
352             }
353
354             hdst = hdata + hoff * data_linesize;
355             for (i = 0; i < rh; i++) {
356                 av_fft_permute(p->ifft, hdst);
357                 av_fft_calc(p->ifft, hdst);
358                 s->export_row(hdst + woff, dst, rw, scale, depth);
359
360                 hdst += data_linesize;
361                 dst += dst_linesize;
362             }
363         }
364     }
365 }
366
367 static void filter_plane3d2(FFTdnoizContext *s, int plane, float *pbuffer, float *nbuffer)
368 {
369     PlaneContext *p = &s->planes[plane];
370     const int block = p->b;
371     const int nox = p->nox;
372     const int noy = p->noy;
373     const int buffer_linesize = p->buffer_linesize / sizeof(float);
374     const float sigma = s->sigma * s->sigma * block * block;
375     const float limit = 1.f - s->amount;
376     float *cbuffer = p->buffer[CURRENT];
377     const float cfactor = sqrtf(3.f) * 0.5f;
378     const float scale = 1.f / 3.f;
379     int y, x, i, j;
380
381     for (y = 0; y < noy; y++) {
382         for (x = 0; x < nox; x++) {
383             float *cbuff = cbuffer + buffer_linesize * y * block + x * block * 2;
384             float *pbuff = pbuffer + buffer_linesize * y * block + x * block * 2;
385             float *nbuff = nbuffer + buffer_linesize * y * block + x * block * 2;
386
387             for (i = 0; i < block; i++) {
388                 for (j = 0; j < block; j++) {
389                     float sumr, sumi, difr, difi, mpr, mpi, mnr, mni;
390                     float factor, power, sumpnr, sumpni;
391
392                     sumpnr = pbuff[2 * j    ] + nbuff[2 * j    ];
393                     sumpni = pbuff[2 * j + 1] + nbuff[2 * j + 1];
394                     sumr = cbuff[2 * j    ] + sumpnr;
395                     sumi = cbuff[2 * j + 1] + sumpni;
396                     difr = cfactor * (nbuff[2 * j    ] - pbuff[2 * j    ]);
397                     difi = cfactor * (pbuff[2 * j + 1] - nbuff[2 * j + 1]);
398                     mpr = cbuff[2 * j    ] - 0.5f * sumpnr + difi;
399                     mnr = mpr - difi - difi;
400                     mpi = cbuff[2 * j + 1] - 0.5f * sumpni + difr;
401                     mni = mpi - difr - difr;
402                     power = sumr * sumr + sumi * sumi + 1e-15f;
403                     factor = FFMAX((power - sigma) / power, limit);
404                     sumr *= factor;
405                     sumi *= factor;
406                     power = mpr * mpr + mpi * mpi + 1e-15f;
407                     factor = FFMAX((power - sigma) / power, limit);
408                     mpr *= factor;
409                     mpi *= factor;
410                     power = mnr * mnr + mni * mni + 1e-15f;
411                     factor = FFMAX((power - sigma) / power, limit);
412                     mnr *= factor;
413                     mni *= factor;
414                     cbuff[2 * j    ] = (sumr + mpr + mnr) * scale;
415                     cbuff[2 * j + 1] = (sumi + mpi + mni) * scale;
416
417                 }
418
419                 cbuff += buffer_linesize;
420                 pbuff += buffer_linesize;
421                 nbuff += buffer_linesize;
422             }
423         }
424     }
425 }
426
427 static void filter_plane3d1(FFTdnoizContext *s, int plane, float *pbuffer)
428 {
429     PlaneContext *p = &s->planes[plane];
430     const int block = p->b;
431     const int nox = p->nox;
432     const int noy = p->noy;
433     const int buffer_linesize = p->buffer_linesize / sizeof(float);
434     const float sigma = s->sigma * s->sigma * block * block;
435     const float limit = 1.f - s->amount;
436     float *cbuffer = p->buffer[CURRENT];
437     int y, x, i, j;
438
439     for (y = 0; y < noy; y++) {
440         for (x = 0; x < nox; x++) {
441             float *cbuff = cbuffer + buffer_linesize * y * block + x * block * 2;
442             float *pbuff = pbuffer + buffer_linesize * y * block + x * block * 2;
443
444             for (i = 0; i < block; i++) {
445                 for (j = 0; j < block; j++) {
446                     float factor, power, re, im, pre, pim;
447                     float sumr, sumi, difr, difi;
448
449                     re = cbuff[j * 2    ];
450                     pre = pbuff[j * 2    ];
451                     im = cbuff[j * 2 + 1];
452                     pim = pbuff[j * 2 + 1];
453
454                     sumr = re + pre;
455                     sumi = im + pim;
456                     difr = re - pre;
457                     difi = im - pim;
458
459                     power = sumr * sumr + sumi * sumi + 1e-15f;
460                     factor = FFMAX(limit, (power - sigma) / power);
461                     sumr *= factor;
462                     sumi *= factor;
463                     power = difr * difr + difi * difi + 1e-15f;
464                     factor = FFMAX(limit, (power - sigma) / power);
465                     difr *= factor;
466                     difi *= factor;
467
468                     cbuff[j * 2    ] = (sumr + difr) * 0.5f;
469                     cbuff[j * 2 + 1] = (sumi + difi) * 0.5f;
470                 }
471
472                 cbuff += buffer_linesize;
473                 pbuff += buffer_linesize;
474             }
475         }
476     }
477 }
478
479 static void filter_plane2d(FFTdnoizContext *s, int plane)
480 {
481     PlaneContext *p = &s->planes[plane];
482     const int block = p->b;
483     const int nox = p->nox;
484     const int noy = p->noy;
485     const int buffer_linesize = p->buffer_linesize / 4;
486     const float sigma = s->sigma * s->sigma * block * block;
487     const float limit = 1.f - s->amount;
488     float *buffer = p->buffer[CURRENT];
489     int y, x, i, j;
490
491     for (y = 0; y < noy; y++) {
492         for (x = 0; x < nox; x++) {
493             float *buff = buffer + buffer_linesize * y * block + x * block * 2;
494
495             for (i = 0; i < block; i++) {
496                 for (j = 0; j < block; j++) {
497                     float factor, power, re, im;
498
499                     re = buff[j * 2    ];
500                     im = buff[j * 2 + 1];
501                     power = re * re + im * im + 1e-15f;
502                     factor = FFMAX(limit, (power - sigma) / power);
503                     buff[j * 2    ] *= factor;
504                     buff[j * 2 + 1] *= factor;
505                 }
506
507                 buff += buffer_linesize;
508             }
509         }
510     }
511 }
512
513 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
514 {
515     AVFilterContext *ctx = inlink->dst;
516     FFTdnoizContext *s = ctx->priv;
517     AVFilterLink *outlink = ctx->outputs[0];
518     int direct, plane;
519     AVFrame *out;
520
521     if (s->nb_next > 0 && s->nb_prev > 0) {
522         av_frame_free(&s->prev);
523         s->prev = s->cur;
524         s->cur = s->next;
525         s->next = in;
526
527         if (!s->prev && s->cur) {
528             s->prev = av_frame_clone(s->cur);
529             if (!s->prev)
530                 return AVERROR(ENOMEM);
531         }
532         if (!s->cur)
533             return 0;
534     } else if (s->nb_next > 0) {
535         av_frame_free(&s->cur);
536         s->cur = s->next;
537         s->next = in;
538
539         if (!s->cur)
540             return 0;
541     } else if (s->nb_prev > 0) {
542         av_frame_free(&s->prev);
543         s->prev = s->cur;
544         s->cur = in;
545
546         if (!s->prev)
547             s->prev = av_frame_clone(s->cur);
548         if (!s->prev)
549             return AVERROR(ENOMEM);
550     } else {
551         s->cur = in;
552     }
553
554     if (av_frame_is_writable(in) && s->nb_next == 0 && s->nb_prev == 0) {
555         direct = 1;
556         out = in;
557     } else {
558         direct = 0;
559         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
560         if (!out)
561             return AVERROR(ENOMEM);
562         av_frame_copy_props(out, s->cur);
563     }
564
565     for (plane = 0; plane < s->nb_planes; plane++) {
566         PlaneContext *p = &s->planes[plane];
567
568         if (!((1 << plane) & s->planesf) || ctx->is_disabled) {
569             if (!direct)
570                 av_image_copy_plane(out->data[plane], out->linesize[plane],
571                                     s->cur->data[plane], s->cur->linesize[plane],
572                                     p->planewidth, p->planeheight);
573             continue;
574         }
575
576         if (s->next) {
577             import_plane(s, s->next->data[plane], s->next->linesize[plane],
578                          p->buffer[NEXT], p->buffer_linesize, plane);
579         }
580
581         if (s->prev) {
582             import_plane(s, s->prev->data[plane], s->prev->linesize[plane],
583                          p->buffer[PREV], p->buffer_linesize, plane);
584         }
585
586         import_plane(s, s->cur->data[plane], s->cur->linesize[plane],
587                      p->buffer[CURRENT], p->buffer_linesize, plane);
588
589         if (s->next && s->prev) {
590             filter_plane3d2(s, plane, p->buffer[PREV], p->buffer[NEXT]);
591         } else if (s->next) {
592             filter_plane3d1(s, plane, p->buffer[NEXT]);
593         } else  if (s->prev) {
594             filter_plane3d1(s, plane, p->buffer[PREV]);
595         } else {
596             filter_plane2d(s, plane);
597         }
598
599         export_plane(s, out->data[plane], out->linesize[plane],
600                      p->buffer[CURRENT], p->buffer_linesize, plane);
601     }
602
603     if (s->nb_next == 0 && s->nb_prev == 0) {
604         if (direct) {
605             s->cur = NULL;
606         } else {
607             av_frame_free(&s->cur);
608         }
609     }
610     return ff_filter_frame(outlink, out);
611 }
612
613 static int request_frame(AVFilterLink *outlink)
614 {
615     AVFilterContext *ctx = outlink->src;
616     FFTdnoizContext *s = ctx->priv;
617     int ret = 0;
618
619     ret = ff_request_frame(ctx->inputs[0]);
620
621     if (ret == AVERROR_EOF && (s->nb_next > 0)) {
622         AVFrame *buf;
623
624         if (s->next && s->nb_next > 0)
625             buf = av_frame_clone(s->next);
626         else if (s->cur)
627             buf = av_frame_clone(s->cur);
628         else
629             buf = av_frame_clone(s->prev);
630         if (!buf)
631             return AVERROR(ENOMEM);
632
633         ret = filter_frame(ctx->inputs[0], buf);
634         if (ret < 0)
635             return ret;
636         ret = AVERROR_EOF;
637     }
638
639     return ret;
640 }
641
642 static av_cold void uninit(AVFilterContext *ctx)
643 {
644     FFTdnoizContext *s = ctx->priv;
645     int i;
646
647     for (i = 0; i < 4; i++) {
648         PlaneContext *p = &s->planes[i];
649
650         av_freep(&p->hdata);
651         av_freep(&p->vdata);
652         av_freep(&p->buffer[PREV]);
653         av_freep(&p->buffer[CURRENT]);
654         av_freep(&p->buffer[NEXT]);
655         av_fft_end(p->fft);
656         av_fft_end(p->ifft);
657     }
658
659     av_frame_free(&s->prev);
660     av_frame_free(&s->cur);
661     av_frame_free(&s->next);
662 }
663
664 static const AVFilterPad fftdnoiz_inputs[] = {
665     {
666         .name         = "default",
667         .type         = AVMEDIA_TYPE_VIDEO,
668         .filter_frame = filter_frame,
669         .config_props = config_input,
670     },
671     { NULL }
672 };
673
674 static const AVFilterPad fftdnoiz_outputs[] = {
675     {
676         .name          = "default",
677         .type          = AVMEDIA_TYPE_VIDEO,
678         .request_frame = request_frame,
679     },
680     { NULL }
681 };
682
683 AVFilter ff_vf_fftdnoiz = {
684     .name          = "fftdnoiz",
685     .description   = NULL_IF_CONFIG_SMALL("Denoise frames using 3D FFT."),
686     .priv_size     = sizeof(FFTdnoizContext),
687     .init          = init,
688     .uninit        = uninit,
689     .query_formats = query_formats,
690     .inputs        = fftdnoiz_inputs,
691     .outputs       = fftdnoiz_outputs,
692     .priv_class    = &fftdnoiz_class,
693     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
694 };