2 * This file is part of FFmpeg.
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.
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.
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
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"
27 #include "libavcodec/avfft.h"
36 typedef struct PlaneContext {
37 int planewidth, planeheight;
44 FFTComplex *hdata, *vdata;
48 FFTContext *fft, *ifft;
51 typedef struct FFTdnoizContext {
62 AVFrame *prev, *cur, *next;
66 PlaneContext planes[4];
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);
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 },
92 AVFILTER_DEFINE_CLASS(fftdnoiz);
94 static av_cold int init(AVFilterContext *ctx)
96 FFTdnoizContext *s = ctx->priv;
99 for (i = 0; i < 4; i++) {
100 PlaneContext *p = &s->planes[i];
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);
111 static int query_formats(AVFilterContext *ctx)
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,
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,
134 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
136 return AVERROR(ENOMEM);
137 return ff_set_common_formats(ctx, fmts_list);
140 typedef struct ThreadData {
144 static void import_row8(FFTComplex *dst, uint8_t *src, int rw)
148 for (j = 0; j < rw; j++) {
154 static void export_row8(FFTComplex *src, uint8_t *dst, int rw, float scale, int depth)
158 for (j = 0; j < rw; j++)
159 dst[j] = av_clip_uint8(src[j].re * scale);
162 static void import_row16(FFTComplex *dst, uint8_t *srcp, int rw)
164 uint16_t *src = (uint16_t *)srcp;
167 for (j = 0; j < rw; j++) {
173 static void export_row16(FFTComplex *src, uint8_t *dstp, int rw, float scale, int depth)
175 uint16_t *dst = (uint16_t *)dstp;
178 for (j = 0; j < rw; j++)
179 dst[j] = av_clip_uintp2_c(src[j].re * scale, depth);
182 static int config_input(AVFilterLink *inlink)
184 AVFilterContext *ctx = inlink->dst;
185 const AVPixFmtDescriptor *desc;
186 FFTdnoizContext *s = ctx->priv;
189 desc = av_pix_fmt_desc_get(inlink->format);
190 s->depth = desc->comp[0].depth;
193 s->import_row = import_row8;
194 s->export_row = export_row8;
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);
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;
206 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
208 for (i = 0; i < s->nb_planes; i++) {
209 PlaneContext *p = &s->planes[i];
212 p->b = 1 << s->block_bits;
213 p->n = 1.f / (p->b * p->b);
214 p->o = p->b * s->overlap;
216 p->nox = (p->planewidth + (size - 1)) / size;
217 p->noy = (p->planeheight + (size - 1)) / size;
219 av_log(ctx, AV_LOG_DEBUG, "nox:%d noy:%d size:%d\n", p->nox, p->noy, size);
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);
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);
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);
245 static void import_plane(FFTdnoizContext *s,
246 uint8_t *srcp, int src_linesize,
247 float *buffer, int buffer_linesize, int plane)
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;
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;
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;
278 av_fft_permute(p->fft, dst);
279 av_fft_calc(p->fft, dst);
282 dst += data_linesize;
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;
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));
302 dst += data_linesize;
303 bdst += buffer_linesize;
309 static void export_plane(FFTdnoizContext *s,
310 uint8_t *dstp, int dst_linesize,
311 float *buffer, int buffer_linesize, int plane)
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;
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;
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];
350 ddst += data_linesize;
351 bsrc += buffer_linesize;
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);
360 hdst += data_linesize;
367 static void filter_plane3d2(FFTdnoizContext *s, int plane, float *pbuffer, float *nbuffer)
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;
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;
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;
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);
406 power = mpr * mpr + mpi * mpi + 1e-15f;
407 factor = FFMAX((power - sigma) / power, limit);
410 power = mnr * mnr + mni * mni + 1e-15f;
411 factor = FFMAX((power - sigma) / power, limit);
414 cbuff[2 * j ] = (sumr + mpr + mnr) * scale;
415 cbuff[2 * j + 1] = (sumi + mpi + mni) * scale;
419 cbuff += buffer_linesize;
420 pbuff += buffer_linesize;
421 nbuff += buffer_linesize;
427 static void filter_plane3d1(FFTdnoizContext *s, int plane, float *pbuffer)
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];
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;
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;
451 im = cbuff[j * 2 + 1];
452 pim = pbuff[j * 2 + 1];
459 power = sumr * sumr + sumi * sumi + 1e-15f;
460 factor = FFMAX(limit, (power - sigma) / power);
463 power = difr * difr + difi * difi + 1e-15f;
464 factor = FFMAX(limit, (power - sigma) / power);
468 cbuff[j * 2 ] = (sumr + difr) * 0.5f;
469 cbuff[j * 2 + 1] = (sumi + difi) * 0.5f;
472 cbuff += buffer_linesize;
473 pbuff += buffer_linesize;
479 static void filter_plane2d(FFTdnoizContext *s, int plane)
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];
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;
495 for (i = 0; i < block; i++) {
496 for (j = 0; j < block; j++) {
497 float factor, power, re, im;
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;
507 buff += buffer_linesize;
513 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
515 AVFilterContext *ctx = inlink->dst;
516 FFTdnoizContext *s = ctx->priv;
517 AVFilterLink *outlink = ctx->outputs[0];
521 if (s->nb_next > 0 && s->nb_prev > 0) {
522 av_frame_free(&s->prev);
527 if (!s->prev && s->cur) {
528 s->prev = av_frame_clone(s->cur);
530 return AVERROR(ENOMEM);
534 } else if (s->nb_next > 0) {
535 av_frame_free(&s->cur);
541 } else if (s->nb_prev > 0) {
542 av_frame_free(&s->prev);
547 s->prev = av_frame_clone(s->cur);
549 return AVERROR(ENOMEM);
554 if (av_frame_is_writable(in) && s->nb_next == 0 && s->nb_prev == 0) {
559 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
561 return AVERROR(ENOMEM);
562 av_frame_copy_props(out, s->cur);
565 for (plane = 0; plane < s->nb_planes; plane++) {
566 PlaneContext *p = &s->planes[plane];
568 if (!((1 << plane) & s->planesf) || ctx->is_disabled) {
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);
577 import_plane(s, s->next->data[plane], s->next->linesize[plane],
578 p->buffer[NEXT], p->buffer_linesize, plane);
582 import_plane(s, s->prev->data[plane], s->prev->linesize[plane],
583 p->buffer[PREV], p->buffer_linesize, plane);
586 import_plane(s, s->cur->data[plane], s->cur->linesize[plane],
587 p->buffer[CURRENT], p->buffer_linesize, plane);
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]);
596 filter_plane2d(s, plane);
599 export_plane(s, out->data[plane], out->linesize[plane],
600 p->buffer[CURRENT], p->buffer_linesize, plane);
603 if (s->nb_next == 0 && s->nb_prev == 0) {
607 av_frame_free(&s->cur);
610 return ff_filter_frame(outlink, out);
613 static int request_frame(AVFilterLink *outlink)
615 AVFilterContext *ctx = outlink->src;
616 FFTdnoizContext *s = ctx->priv;
619 ret = ff_request_frame(ctx->inputs[0]);
621 if (ret == AVERROR_EOF && (s->nb_next > 0)) {
624 if (s->next && s->nb_next > 0)
625 buf = av_frame_clone(s->next);
627 buf = av_frame_clone(s->cur);
629 buf = av_frame_clone(s->prev);
631 return AVERROR(ENOMEM);
633 ret = filter_frame(ctx->inputs[0], buf);
642 static av_cold void uninit(AVFilterContext *ctx)
644 FFTdnoizContext *s = ctx->priv;
647 for (i = 0; i < 4; i++) {
648 PlaneContext *p = &s->planes[i];
652 av_freep(&p->buffer[PREV]);
653 av_freep(&p->buffer[CURRENT]);
654 av_freep(&p->buffer[NEXT]);
659 av_frame_free(&s->prev);
660 av_frame_free(&s->cur);
661 av_frame_free(&s->next);
664 static const AVFilterPad fftdnoiz_inputs[] = {
667 .type = AVMEDIA_TYPE_VIDEO,
668 .filter_frame = filter_frame,
669 .config_props = config_input,
674 static const AVFilterPad fftdnoiz_outputs[] = {
677 .type = AVMEDIA_TYPE_VIDEO,
678 .request_frame = request_frame,
683 AVFilter ff_vf_fftdnoiz = {
685 .description = NULL_IF_CONFIG_SMALL("Denoise frames using 3D FFT."),
686 .priv_size = sizeof(FFTdnoizContext),
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,