Go to the documentation of this file.
72 #define OFFSET(x) offsetof(FFTdnoizContext, x)
73 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
75 {
"sigma",
"set denoise strength",
77 {
"amount",
"set amount of denoising",
79 {
"block",
"set block log2(size)",
81 {
"overlap",
"set block overlap",
83 {
"prev",
"set number of previous frames for temporal denoising",
85 {
"next",
"set number of next frames for temporal denoising",
87 {
"planes",
"set planes to filter",
99 for (
i = 0;
i < 4;
i++) {
148 for (j = 0; j < rw; j++) {
158 for (j = 0; j < rw; j++)
159 dst[j] = av_clip_uint8(
src[j].
re * scale);
164 uint16_t *
src = (uint16_t *)
srcp;
167 for (j = 0; j < rw; j++) {
175 uint16_t *dst = (uint16_t *)
dstp;
178 for (j = 0; j < rw; j++)
190 s->depth =
desc->comp[0].depth;
198 s->sigma *= 1 << (
s->depth - 8) * (1 +
s->nb_prev +
s->nb_next);
202 s->planes[0].planewidth =
s->planes[3].planewidth =
inlink->w;
204 s->planes[0].planeheight =
s->planes[3].planeheight =
inlink->h;
208 for (
i = 0;
i <
s->nb_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;
225 if (
s->nb_prev > 0) {
230 if (
s->nb_next > 0) {
253 const int overlap = p->
o;
255 const int nox = p->
nox;
256 const int noy = p->
noy;
257 const int bpp = (
s->depth + 7) / 8;
263 buffer_linesize /=
sizeof(float);
264 for (y = 0; y < noy; y++) {
265 for (x = 0; x < nox; x++) {
272 for (
i = 0;
i < rh;
i++) {
273 s->import_row(dst,
src, rw);
274 for (j = rw; j <
block; j++) {
282 dst += data_linesize;
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;
296 for (j = 0; j <
block; j++)
297 dst[j] = ssrc[j * data_linesize +
i];
302 dst += data_linesize;
303 bdst += buffer_linesize;
314 const int depth =
s->depth;
315 const int bpp = (depth + 7) / 8;
319 const int overlap = p->
o;
320 const int hoverlap = overlap / 2;
322 const int nox = p->
nox;
323 const int noy = p->
noy;
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;
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;
371 const int nox = p->
nox;
372 const int noy = p->
noy;
374 const float sigma =
s->sigma *
s->sigma *
block *
block;
375 const float limit = 1.f -
s->amount;
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;
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-15
f;
406 power = mpr * mpr + mpi * mpi + 1e-15
f;
410 power = mnr * mnr + mni * mni + 1e-15
f;
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;
431 const int nox = p->
nox;
432 const int noy = p->
noy;
434 const float sigma =
s->sigma *
s->sigma *
block *
block;
435 const float limit = 1.f -
s->amount;
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;
445 for (j = 0; j <
block; j++) {
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-15
f;
463 power = difr * difr + difi * difi + 1e-15
f;
468 cbuff[j * 2 ] = (sumr + difr) * 0.5
f;
469 cbuff[j * 2 + 1] = (sumi + difi) * 0.5
f;
472 cbuff += buffer_linesize;
473 pbuff += buffer_linesize;
483 const int nox = p->
nox;
484 const int noy = p->
noy;
486 const float sigma =
s->sigma *
s->sigma *
block *
block;
487 const float limit = 1.f -
s->amount;
491 for (y = 0; y < noy; y++) {
492 for (x = 0; x < nox; x++) {
496 for (j = 0; j <
block; j++) {
500 im = buff[j * 2 + 1];
504 buff[j * 2 + 1] *=
factor;
507 buff += buffer_linesize;
521 if (
s->nb_next > 0 &&
s->nb_prev > 0) {
527 if (!
s->prev &&
s->cur) {
534 }
else if (
s->nb_next > 0) {
541 }
else if (
s->nb_prev > 0) {
568 if (!((1 <<
plane) &
s->planesf) ||
ctx->is_disabled) {
589 if (
s->next &&
s->prev) {
591 }
else if (
s->next) {
593 }
else if (
s->prev) {
603 if (
s->nb_next == 0 &&
s->nb_prev == 0) {
624 if (
s->next &&
s->nb_next > 0)
647 for (
i = 0;
i < 4;
i++) {
692 .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.
av_cold void av_fft_end(FFTContext *s)
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.
static const uint8_t block_bits[]
AVComplexFloat FFTComplex
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.
static void filter_plane2d(FFTdnoizContext *s, int plane)
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.
static void export_plane(FFTdnoizContext *s, uint8_t *dstp, int dst_linesize, float *buffer, int buffer_linesize, int plane)
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.
BYTE int const BYTE * srcp
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_GBRP14
#define AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_YUV422P9
#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
@ 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_YUV444P16
#define AV_CEIL_RSHIFT(a, b)
static const AVFilterPad outputs[]
static enum AVPixelFormat pix_fmts[]
#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.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ 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
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
static int config_input(AVFilterLink *inlink)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static av_cold int init(AVFilterContext *ctx)
#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
void(* import_row)(FFTComplex *dst, uint8_t *src, int rw)
static void export_row16(FFTComplex *src, uint8_t *dstp, int rw, float scale, int depth)
static int query_formats(AVFilterContext *ctx)
static void filter_plane3d2(FFTdnoizContext *s, int plane, float *pbuffer, float *nbuffer)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
#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
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.
#define AV_PIX_FMT_YUV444P9
static av_cold void uninit(AVFilterContext *ctx)
static const AVFilterPad fftdnoiz_inputs[]
#define AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV422P14
FFTContext * av_fft_init(int nbits, int inverse)
Set up a complex FFT.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
static void import_plane(FFTdnoizContext *s, uint8_t *srcp, int src_linesize, float *buffer, int buffer_linesize, int plane)
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
@ 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
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
static void filter_plane3d1(FFTdnoizContext *s, int plane, float *pbuffer)
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)
@ 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...
#define flags(name, subs,...)
@ 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
void av_fft_calc(FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in av_fft_init().
#define AV_PIX_FMT_YUV420P14