Go to the documentation of this file.
67 #define OFFSET(x) offsetof(NLMeansContext, x)
68 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
113 const uint32_t *dst_top = dst - dst_linesize_32;
118 for (y = 0; y <
h; y++) {
119 for (x = 0; x <
w; x += 4) {
120 const int d0 =
s1[x ] -
s2[x ];
121 const int d1 =
s1[x + 1] -
s2[x + 1];
122 const int d2 =
s1[x + 2] -
s2[x + 2];
123 const int d3 =
s1[x + 3] -
s2[x + 3];
125 dst[x ] = dst_top[x ] - dst_top[x - 1] + d0*d0;
126 dst[x + 1] = dst_top[x + 1] - dst_top[x ] + d1*d1;
127 dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2;
128 dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3;
130 dst[x ] += dst[x - 1];
131 dst[x + 1] += dst[x ];
132 dst[x + 2] += dst[x + 1];
133 dst[x + 3] += dst[x + 2];
137 dst += dst_linesize_32;
138 dst_top += dst_linesize_32;
167 int startx,
int starty,
169 int offx,
int offy,
int r,
int sw,
int sh,
174 for (y = starty; y < starty +
h; y++) {
175 uint32_t
acc = dst[y*dst_linesize_32 + startx - 1] - dst[(y-1)*dst_linesize_32 + startx - 1];
176 const int s1y = av_clip(y -
r, 0, sh - 1);
177 const int s2y = av_clip(y - (
r + offy), 0, sh - 1);
179 for (x = startx; x < startx +
w; x++) {
180 const int s1x = av_clip(x -
r, 0, sw - 1);
181 const int s2x = av_clip(x - (
r + offx), 0, sw - 1);
184 const int d = v1 - v2;
186 dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] +
acc;
209 uint32_t *ii, ptrdiff_t ii_linesize_32,
210 const uint8_t *
src, ptrdiff_t linesize,
int offx,
int offy,
214 const int ii_w =
w + e*2;
215 const int ii_h =
h + e*2;
222 const int s2x = e + offx;
223 const int s2y = e + offy;
227 const int startx_safe =
FFMAX(s1x, s2x);
228 const int starty_safe =
FFMAX(s1y, s2y);
229 const int u_endx_safe =
FFMIN(s1x +
w, s2x +
w);
230 const int endy_safe =
FFMIN(s1y +
h, s2y +
h);
233 const int safe_pw = (u_endx_safe - startx_safe) & ~0
xf;
234 const int safe_ph = endy_safe - starty_safe;
237 const int endx_safe = startx_safe + safe_pw;
252 startx_safe, safe_ph);
259 if (safe_pw && safe_ph)
261 src + (starty_safe - s1y) * linesize + (startx_safe - s1x), linesize,
262 src + (starty_safe - s2y) * linesize + (startx_safe - s2x), linesize,
267 endx_safe, starty_safe,
270 ii_w - endx_safe, safe_ph);
277 ii_w, ii_h - endy_safe);
285 const int e =
FFMAX(
s->research_hsize,
s->research_hsize_uv)
286 +
FFMAX(
s->patch_hsize,
s->patch_hsize_uv);
323 s->ii =
s->ii_orig +
s->ii_lz_32 + 1;
349 const int process_h =
td->endy -
td->starty;
350 const int slice_start = (process_h * jobnr ) / nb_jobs;
351 const int slice_end = (process_h * (jobnr+1)) / nb_jobs;
352 const int starty =
td->starty + slice_start;
355 const uint32_t *ii =
td->ii_start + (
starty -
p - 1) *
s->ii_lz_32 -
p - 1;
356 const int dist_b = 2*
p + 1;
357 const int dist_d = dist_b *
s->ii_lz_32;
358 const int dist_e = dist_d + dist_b;
363 for (x =
td->startx; x < td->endx; x++) {
393 const uint32_t
a = ii[x];
394 const uint32_t
b = ii[x + dist_b];
395 const uint32_t d = ii[x + dist_d];
396 const uint32_t e = ii[x + dist_e];
397 const uint32_t patch_diff_sq = e - d -
b +
a;
399 if (patch_diff_sq < s->max_meaningful_diff) {
400 const float weight =
s->weight_lut[patch_diff_sq];
417 for (y = 0; y <
h; y++) {
418 for (x = 0; x <
w; x++) {
421 wa[x].
sum += 1.f *
src[x];
431 uint8_t *dst, ptrdiff_t dst_linesize,
440 const uint32_t *centered_ii =
s->ii + e*
s->ii_lz_32 + e;
442 memset(
s->wa, 0,
s->wa_linesize *
h *
sizeof(*
s->wa));
444 for (offy = -
r; offy <=
r; offy++) {
445 for (offx = -
r; offx <=
r; offx++) {
450 .startx =
FFMAX(0, -offx),
451 .starty =
FFMAX(0, -offy),
454 .ii_start = centered_ii + offy*
s->ii_lz_32 + offx,
460 offx, offy, e,
w,
h);
468 s->wa,
s->wa_linesize,
w,
h);
487 for (
i = 0;
i <
s->nb_planes;
i++) {
488 const int w =
i ?
s->chroma_w :
inlink->w;
489 const int h =
i ?
s->chroma_h :
inlink->h;
490 const int p =
i ?
s->patch_hsize_uv :
s->patch_hsize;
491 const int r =
i ?
s->research_hsize_uv :
s->research_hsize;
494 in->data[
i],
in->linesize[
i]);
501 #define CHECK_ODD_FIELD(field, name) do { \
502 if (!(s->field & 1)) { \
504 av_log(ctx, AV_LOG_WARNING, name " size must be odd, " \
505 "setting it to %d\n", s->field); \
521 const double h =
s->sigma * 10.;
523 s->pdiff_scale = 1. / (
h *
h);
524 s->max_meaningful_diff = log(255.) /
s->pdiff_scale;
525 s->weight_lut =
av_calloc(
s->max_meaningful_diff,
sizeof(*
s->weight_lut));
528 for (
i = 0;
i <
s->max_meaningful_diff;
i++)
529 s->weight_lut[
i] =
exp(-
i *
s->pdiff_scale);
534 if (!
s->research_size_uv)
s->research_size_uv =
s->research_size;
535 if (!
s->patch_size_uv)
s->patch_size_uv =
s->patch_size;
540 s->research_hsize =
s->research_size / 2;
541 s->research_hsize_uv =
s->research_size_uv / 2;
542 s->patch_hsize =
s->patch_size / 2;
543 s->patch_hsize_uv =
s->patch_size_uv / 2;
546 s->research_size,
s->research_size,
s->research_size_uv,
s->research_size_uv,
547 s->patch_size,
s->patch_size,
s->patch_size_uv,
s->patch_size_uv);
589 .priv_class = &nlmeans_class,
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
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
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)
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.
This structure describes decoded (raw) audio or video data.
uint32_t max_meaningful_diff
void * av_mallocz_array(size_t nmemb, size_t size)
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
const char * name
Filter name.
static void compute_safe_ssd_integral_image_c(uint32_t *dst, ptrdiff_t dst_linesize_32, const uint8_t *s1, ptrdiff_t linesize1, const uint8_t *s2, ptrdiff_t linesize2, int w, int h)
Compute squared difference of the safe area (the zone where s1 and s2 overlap).
AVFormatInternal * internal
An opaque field for libavformat internal usage.
A link between two filters.
#define CHECK_ODD_FIELD(field, name)
static int query_formats(AVFilterContext *ctx)
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
static const AVFilterPad nlmeans_outputs[]
static void compute_ssd_integral_image(const NLMeansDSPContext *dsp, uint32_t *ii, ptrdiff_t ii_linesize_32, const uint8_t *src, ptrdiff_t linesize, int offx, int offy, int e, int w, int h)
static const AVFilterPad nlmeans_inputs[]
void(* compute_safe_ssd_integral_image)(uint32_t *dst, ptrdiff_t dst_linesize_32, const uint8_t *s1, ptrdiff_t linesize1, const uint8_t *s2, ptrdiff_t linesize2, int w, int h)
static void weight_averages(uint8_t *dst, ptrdiff_t dst_linesize, const uint8_t *src, ptrdiff_t src_linesize, struct weighted_avg *wa, ptrdiff_t wa_linesize, int w, int h)
A filter pad used for either input or output.
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
static void compute_unsafe_ssd_integral_image(uint32_t *dst, ptrdiff_t dst_linesize_32, int startx, int starty, const uint8_t *src, ptrdiff_t linesize, int offx, int offy, int r, int sw, int sh, int w, int h)
Compute squared difference of an unsafe area (the zone nor s1 nor s2 could be readable).
@ 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_CEIL_RSHIFT(a, b)
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
static const AVFilterPad outputs[]
static enum AVPixelFormat pix_fmts[]
@ 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...
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...
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 weight(int i, int blen, int offset)
static av_cold void uninit(AVFilterContext *ctx)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int config_input(AVFilterLink *inlink)
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
#define AV_LOG_INFO
Standard information.
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
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 av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
#define i(width, name, range_min, range_max)
int w
agreed upon image width
#define av_malloc_array(a, b)
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define xf(width, name, var, range_min, range_max, subs,...)
@ 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.
static av_cold int init(AVFilterContext *ctx)
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
int h
agreed upon image height
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
AVFILTER_DEFINE_CLASS(nlmeans)
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
static int nlmeans_plane(AVFilterContext *ctx, int w, int h, int p, int r, uint8_t *dst, ptrdiff_t dst_linesize, const uint8_t *src, ptrdiff_t src_linesize)
void ff_nlmeans_init(NLMeansDSPContext *dsp)
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
av_cold void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp)
static const AVOption nlmeans_options[]
#define flags(name, subs,...)
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
const uint32_t * ii_start