Go to the documentation of this file.
28 #define MAX_THREADS 32
74 #define OFFSET(x) offsetof(FFTdnoizContext, x)
75 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
76 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
78 {
"sigma",
"set denoise strength",
80 {
"amount",
"set amount of denoising",
82 {
"block",
"set block log2(size)",
84 {
"overlap",
"set block overlap",
86 {
"prev",
"set number of previous frames for temporal denoising",
88 {
"next",
"set number of next frames for temporal denoising",
90 {
"planes",
"set planes to filter",
132 for (j = 0; j < rw; j++) {
142 for (j = 0; j < rw; j++)
148 uint16_t *
src = (uint16_t *)srcp;
151 for (j = 0; j < rw; j++) {
159 uint16_t *dst = (uint16_t *)dstp;
162 for (j = 0; j < rw; j++)
174 s->depth =
desc->comp[0].depth;
182 s->sigma *= 1 << (
s->depth - 8) * (1 +
s->nb_prev +
s->nb_next);
186 s->planes[0].planewidth =
s->planes[3].planewidth =
inlink->w;
188 s->planes[0].planeheight =
s->planes[3].planeheight =
inlink->h;
193 for (
int i = 0;
i <
s->nb_threads;
i++) {
196 if (!
s->fft[
i] || !
s->ifft[
i])
200 for (
i = 0;
i <
s->nb_planes;
i++) {
204 p->
b = 1 <<
s->block_bits;
205 p->
n = 1.f / (p->
b * p->
b);
206 p->
o = p->
b *
s->overlap;
217 if (
s->nb_prev > 0) {
222 if (
s->nb_next > 0) {
228 for (
int j = 0; j <
s->nb_threads; j++) {
240 uint8_t *srcp,
int src_linesize,
241 float *
buffer,
int buffer_linesize,
int plane,
242 int jobnr,
int nb_jobs)
248 const int overlap = p->
o;
250 const int nox = p->
nox;
251 const int noy = p->
noy;
252 const int bpp = (
s->depth + 7) / 8;
254 const int slice_start = (noy * jobnr) / nb_jobs;
255 const int slice_end = (noy * (jobnr+1)) / nb_jobs;
260 buffer_linesize /=
sizeof(float);
261 for (y = slice_start; y <
slice_end; y++) {
262 for (x = 0; x < nox; x++) {
265 uint8_t *
src = srcp + src_linesize * y *
size + x *
size * bpp;
269 for (
i = 0;
i < rh;
i++) {
270 s->import_row(dst,
src, rw);
271 for (j = rw; j <
block; j++) {
279 dst += data_linesize;
284 for (j = 0; j <
block; j++) {
285 dst[j].
re = dst[(
block -
i - 1) * data_linesize + j].
re;
286 dst[j].
im = dst[(
block -
i - 1) * data_linesize + j].
im;
293 for (j = 0; j <
block; j++)
294 dst[j] = ssrc[j * data_linesize +
i];
299 dst += data_linesize;
300 bdst += buffer_linesize;
307 uint8_t *dstp,
int dst_linesize,
308 float *
buffer,
int buffer_linesize,
int plane,
309 int jobnr,
int nb_jobs)
312 const int depth =
s->depth;
313 const int bpp = (depth + 7) / 8;
317 const int overlap = p->
o;
318 const int hoverlap = overlap / 2;
320 const int nox = p->
nox;
321 const int noy = p->
noy;
324 const int slice_start = (noy * jobnr) / nb_jobs;
325 const int slice_end = (noy * (jobnr+1)) / nb_jobs;
330 buffer_linesize /=
sizeof(float);
331 for (y = slice_start; y <
slice_end; y++) {
332 for (x = 0; x < nox; x++) {
333 const int woff = x == 0 ? 0 : hoverlap;
334 const int hoff = y == 0 ? 0 : hoverlap;
338 uint8_t *dst = dstp + dst_linesize * (y *
size + hoff) + (x *
size + woff) * bpp;
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++) {
358 s->export_row(hdst + woff, dst, rw,
scale, depth);
360 hdst += data_linesize;
368 int jobnr,
int nb_jobs)
372 const int nox = p->
nox;
373 const int noy = p->
noy;
375 const float sigma =
s->sigma *
s->sigma *
block *
block;
376 const int slice_start = (noy * jobnr) / nb_jobs;
377 const int slice_end = (noy * (jobnr+1)) / nb_jobs;
378 const float limit = 1.f -
s->amount;
380 const float cfactor = sqrtf(3.
f) * 0.5f;
381 const float scale = 1.f / 3.f;
384 for (y = slice_start; y <
slice_end; y++) {
385 for (x = 0; x < nox; x++) {
386 float *cbuff = cbuffer + buffer_linesize * y *
block + x *
block * 2;
387 float *pbuff = pbuffer + buffer_linesize * y *
block + x *
block * 2;
388 float *nbuff = nbuffer + buffer_linesize * y *
block + x *
block * 2;
391 for (j = 0; j <
block; j++) {
392 float sumr, sumi, difr, difi, mpr, mpi, mnr, mni;
395 sumpnr = pbuff[2 * j ] + nbuff[2 * j ];
396 sumpni = pbuff[2 * j + 1] + nbuff[2 * j + 1];
397 sumr = cbuff[2 * j ] + sumpnr;
398 sumi = cbuff[2 * j + 1] + sumpni;
399 difr = cfactor * (nbuff[2 * j ] - pbuff[2 * j ]);
400 difi = cfactor * (pbuff[2 * j + 1] - nbuff[2 * j + 1]);
401 mpr = cbuff[2 * j ] - 0.5f * sumpnr + difi;
402 mnr = mpr - difi - difi;
403 mpi = cbuff[2 * j + 1] - 0.5f * sumpni + difr;
404 mni = mpi - difr - difr;
405 power = sumr * sumr + sumi * sumi + 1e-15
f;
409 power = mpr * mpr + mpi * mpi + 1e-15
f;
413 power = mnr * mnr + mni * mni + 1e-15
f;
417 cbuff[2 * j ] = (sumr + mpr + mnr) *
scale;
418 cbuff[2 * j + 1] = (sumi + mpi + mni) *
scale;
422 cbuff += buffer_linesize;
423 pbuff += buffer_linesize;
424 nbuff += buffer_linesize;
431 int jobnr,
int nb_jobs)
435 const int nox = p->
nox;
436 const int noy = p->
noy;
438 const float sigma =
s->sigma *
s->sigma *
block *
block;
439 const int slice_start = (noy * jobnr) / nb_jobs;
440 const int slice_end = (noy * (jobnr+1)) / nb_jobs;
441 const float limit = 1.f -
s->amount;
445 for (y = slice_start; y <
slice_end; y++) {
446 for (x = 0; x < nox; x++) {
447 float *cbuff = cbuffer + buffer_linesize * y *
block + x *
block * 2;
448 float *pbuff = pbuffer + buffer_linesize * y *
block + x *
block * 2;
451 for (j = 0; j <
block; j++) {
453 float sumr, sumi, difr, difi;
457 im = cbuff[j * 2 + 1];
458 pim = pbuff[j * 2 + 1];
465 power = sumr * sumr + sumi * sumi + 1e-15
f;
469 power = difr * difr + difi * difi + 1e-15
f;
474 cbuff[j * 2 ] = (sumr + difr) * 0.5
f;
475 cbuff[j * 2 + 1] = (sumi + difi) * 0.5
f;
478 cbuff += buffer_linesize;
479 pbuff += buffer_linesize;
486 int jobnr,
int nb_jobs)
490 const int nox = p->
nox;
491 const int noy = p->
noy;
493 const float sigma =
s->sigma *
s->sigma *
block *
block;
494 const float limit = 1.f -
s->amount;
495 const int slice_start = (noy * jobnr) / nb_jobs;
496 const int slice_end = (noy * (jobnr+1)) / nb_jobs;
499 for (
int y = slice_start; y <
slice_end; y++) {
500 for (
int x = 0; x < nox; x++) {
504 for (
int j = 0; j <
block; j++) {
508 im = buff[j * 2 + 1];
512 buff[j * 2 + 1] *=
factor;
515 buff += buffer_linesize;
522 int jobnr,
int nb_jobs)
526 for (
int plane = 0; plane <
s->nb_planes; plane++) {
529 if (!((1 << plane) &
s->planesf) ||
ctx->is_disabled)
553 int jobnr,
int nb_jobs)
557 for (
int plane = 0; plane <
s->nb_planes; plane++) {
560 if (!((1 << plane) &
s->planesf) ||
ctx->is_disabled)
563 if (
s->next &&
s->prev) {
565 }
else if (
s->next) {
567 }
else if (
s->prev) {
578 int jobnr,
int nb_jobs)
583 for (
int plane = 0; plane <
s->nb_planes; plane++) {
586 if (!((1 << plane) &
s->planesf) ||
ctx->is_disabled)
605 if (
s->nb_next > 0 &&
s->nb_prev > 0) {
611 if (!
s->prev &&
s->cur) {
618 }
else if (
s->nb_next > 0) {
625 }
else if (
s->nb_prev > 0) {
650 FFMIN(
s->planes[0].noy,
s->nb_threads));
653 FFMIN(
s->planes[0].noy,
s->nb_threads));
656 FFMIN(
s->planes[0].noy,
s->nb_threads));
658 for (plane = 0; plane <
s->nb_planes; plane++) {
661 if (!((1 << plane) &
s->planesf) ||
ctx->is_disabled) {
664 s->cur->data[plane],
s->cur->linesize[plane],
670 if (
s->nb_next == 0 &&
s->nb_prev == 0) {
691 if (
s->next &&
s->nb_next > 0)
714 for (
i = 0;
i < 4;
i++) {
717 for (
int j = 0; j <
s->nb_threads; j++) {
727 for (
i = 0;
i <
s->nb_threads;
i++) {
762 .priv_class = &fftdnoiz_class,
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
#define AV_PIX_FMT_YUVA422P16
av_cold void av_fft_end(FFTContext *s)
#define AV_PIX_FMT_GBRAP16
static void direct(const float *in, const FFTComplex *ir, int len, float *out)
AVPixelFormat
Pixel format.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
static int request_frame(AVFilterLink *outlink)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
#define AVERROR_EOF
End of file.
#define FILTER_PIXFMTS_ARRAY(array)
static const uint8_t block_bits[]
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define AV_PIX_FMT_YUVA422P9
This structure describes decoded (raw) audio or video data.
static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
Clip a signed integer to an unsigned power of two range.
#define AV_PIX_FMT_YUVA420P16
static void export_plane(FFTdnoizContext *s, uint8_t *dstp, int dst_linesize, float *buffer, int buffer_linesize, int plane, int jobnr, int nb_jobs)
#define AV_PIX_FMT_YUVA420P10
static const AVFilterPad fftdnoiz_outputs[]
#define AV_PIX_FMT_YUV420P10
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
void av_fft_permute(FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling ff_fft_calc().
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
const char * name
Filter name.
A link between two filters.
#define AV_PIX_FMT_YUVA422P10
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
#define AV_PIX_FMT_YUVA420P9
#define AV_PIX_FMT_GBRP14
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
#define AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUV422P9
static av_always_inline float scale(float x, float s)
#define AV_PIX_FMT_GRAY16
A filter pad used for either input or output.
#define AV_PIX_FMT_YUV444P10
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
#define AV_PIX_FMT_YUV422P16
static void filter_plane3d1(FFTdnoizContext *s, int plane, float *pbuffer, int jobnr, int nb_jobs)
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
#define AV_PIX_FMT_GBRAP10
#define AV_PIX_FMT_GBRAP12
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
#define AV_PIX_FMT_YUV444P16
#define AV_CEIL_RSHIFT(a, b)
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
#define AV_PIX_FMT_YUVA444P12
#define AV_PIX_FMT_YUV420P9
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_GRAY14
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
static enum AVPixelFormat pix_fmts[]
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define FILTER_INPUTS(array)
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
#define AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GBRP16
Describe the class of an AVClass context structure.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
#define AV_PIX_FMT_YUV440P10
#define AV_PIX_FMT_YUV422P10
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
static int config_input(AVFilterLink *inlink)
static void filter_plane3d2(FFTdnoizContext *s, int plane, float *pbuffer, float *nbuffer, int jobnr, int nb_jobs)
FFTContext * ifft[MAX_THREADS]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
#define AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV444P12
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
AVFilterContext * src
source filter
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
void(* import_row)(FFTComplex *dst, uint8_t *src, int rw)
FFTComplex * vdata[MAX_THREADS]
static void export_row16(FFTComplex *src, uint8_t *dstp, int rw, float scale, int depth)
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
#define AV_PIX_FMT_YUVA444P10
#define i(width, name, range_min, range_max)
static void export_row8(FFTComplex *src, uint8_t *dst, int rw, float scale, int depth)
int w
agreed upon image width
static void import_row8(FFTComplex *dst, uint8_t *src, int rw)
#define AV_PIX_FMT_GBRP12
static int import_pass(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Used for passing data between threads.
static void import_row16(FFTComplex *dst, uint8_t *srcp, int rw)
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
const char * name
Pad name.
void * av_calloc(size_t nmemb, size_t size)
#define AV_PIX_FMT_YUV444P9
static void filter_plane2d(FFTdnoizContext *s, int plane, int jobnr, int nb_jobs)
static double limit(double x)
static av_cold void uninit(AVFilterContext *ctx)
#define AV_PIX_FMT_YUVA444P9
static const AVFilterPad fftdnoiz_inputs[]
#define AV_PIX_FMT_YUV420P12
static void import_plane(FFTdnoizContext *s, uint8_t *srcp, int src_linesize, float *buffer, int buffer_linesize, int plane, int jobnr, int nb_jobs)
#define AV_PIX_FMT_YUV422P14
FFTContext * av_fft_init(int nbits, int inverse)
Set up a complex FFT.
static int export_pass(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static float power(float r, float g, float b, float max)
const AVFilter ff_vf_fftdnoiz
int h
agreed upon image height
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
#define AV_PIX_FMT_YUVA422P12
FFTContext * fft[MAX_THREADS]
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
static const int factor[16]
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
FFTComplex * hdata[MAX_THREADS]
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
#define FILTER_OUTPUTS(array)
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
static int filter_pass(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
The exact code depends on how similar the blocks are and how related they are to the block
#define AV_PIX_FMT_YUV440P12
void(* export_row)(FFTComplex *src, uint8_t *dst, int rw, float scale, int depth)
#define AV_PIX_FMT_YUV444P14
AVFILTER_DEFINE_CLASS(fftdnoiz)
static const AVOption fftdnoiz_options[]
#define AV_PIX_FMT_GRAY12
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
void av_fft_calc(FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in av_fft_init().
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
#define AV_PIX_FMT_YUV420P14