Go to the documentation of this file.
63 #define OFFSET(x) offsetof(NLMeansContext, x)
64 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
96 const uint8_t *
s1, ptrdiff_t linesize1,
97 const uint8_t *
s2, ptrdiff_t linesize2,
100 const uint32_t *dst_top = dst - dst_linesize_32;
105 for (
int y = 0; y <
h; y++) {
106 for (
int x = 0; x <
w; x += 4) {
107 const int d0 =
s1[x ] -
s2[x ];
108 const int d1 =
s1[x + 1] -
s2[x + 1];
109 const int d2 =
s1[x + 2] -
s2[x + 2];
110 const int d3 =
s1[x + 3] -
s2[x + 3];
112 dst[x ] = dst_top[x ] - dst_top[x - 1] + d0*d0;
113 dst[x + 1] = dst_top[x + 1] - dst_top[x ] + d1*d1;
114 dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2;
115 dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3;
117 dst[x ] += dst[x - 1];
118 dst[x + 1] += dst[x ];
119 dst[x + 2] += dst[x + 1];
120 dst[x + 3] += dst[x + 2];
124 dst += dst_linesize_32;
125 dst_top += dst_linesize_32;
154 int startx,
int starty,
155 const uint8_t *
src, ptrdiff_t linesize,
156 int offx,
int offy,
int r,
int sw,
int sh,
159 for (
int y = starty; y < starty +
h; y++) {
160 uint32_t
acc = dst[y*dst_linesize_32 + startx - 1] - dst[(y-1)*dst_linesize_32 + startx - 1];
161 const int s1y =
av_clip(y -
r, 0, sh - 1);
162 const int s2y =
av_clip(y - (
r + offy), 0, sh - 1);
164 for (
int x = startx; x < startx +
w; x++) {
165 const int s1x =
av_clip(x -
r, 0, sw - 1);
166 const int s2x =
av_clip(x - (
r + offx), 0, sw - 1);
167 const uint8_t v1 =
src[s1y*linesize + s1x];
168 const uint8_t v2 =
src[s2y*linesize + s2x];
169 const int d = v1 - v2;
171 dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] +
acc;
194 uint32_t *ii, ptrdiff_t ii_linesize_32,
195 const uint8_t *
src, ptrdiff_t linesize,
int offx,
int offy,
199 const int ii_w =
w + e*2;
200 const int ii_h =
h + e*2;
207 const int s2x = e + offx;
208 const int s2y = e + offy;
212 const int startx_safe =
FFMAX(s1x, s2x);
213 const int starty_safe =
FFMAX(s1y, s2y);
214 const int u_endx_safe =
FFMIN(s1x +
w, s2x +
w);
215 const int endy_safe =
FFMIN(s1y +
h, s2y +
h);
218 const int safe_pw = (u_endx_safe - startx_safe) & ~0
xf;
219 const int safe_ph = endy_safe - starty_safe;
222 const int endx_safe = startx_safe + safe_pw;
237 startx_safe, safe_ph);
244 if (safe_pw && safe_ph)
246 src + (starty_safe - s1y) * linesize + (startx_safe - s1x), linesize,
247 src + (starty_safe - s2y) * linesize + (startx_safe - s2x), linesize,
252 endx_safe, starty_safe,
255 ii_w - endx_safe, safe_ph);
262 ii_w, ii_h - endy_safe);
270 const int e =
FFMAX(
s->research_hsize,
s->research_hsize_uv)
271 +
FFMAX(
s->patch_hsize,
s->patch_hsize_uv);
303 s->ii_orig =
av_calloc(
s->ii_h + 1,
s->ii_lz_32 *
sizeof(*
s->ii_orig));
308 s->ii =
s->ii_orig +
s->ii_lz_32 + 1;
314 if (!
s->total_weight || !
s->sum)
330 const uint32_t *
const iib,
331 const uint32_t *
const iid,
332 const uint32_t *
const iie,
333 const uint8_t *
const src,
336 const float *
const weight_lut,
337 int max_meaningful_diff,
338 int startx,
int endx)
340 for (
int x = startx; x < endx; x++) {
370 const uint32_t
a = iia[x];
371 const uint32_t
b = iib[x];
372 const uint32_t
d = iid[x];
373 const uint32_t e = iie[x];
374 const uint32_t patch_diff_sq =
FFMIN(e -
d -
b +
a, max_meaningful_diff);
375 const float weight = weight_lut[patch_diff_sq];
377 total_weight[x] +=
weight;
385 const uint32_t max_meaningful_diff =
s->max_meaningful_diff;
388 const int process_h =
td->endy -
td->starty;
389 const int slice_start = (process_h * jobnr ) / nb_jobs;
390 const int slice_end = (process_h * (jobnr+1)) / nb_jobs;
391 const int starty =
td->starty + slice_start;
394 const uint32_t *ii =
td->ii_start + (
starty -
p - 1) *
s->ii_lz_32 -
p - 1;
395 const int dist_b = 2*
p + 1;
396 const int dist_d = dist_b *
s->ii_lz_32;
397 const int dist_e = dist_d + dist_b;
398 const float *
const weight_lut =
s->weight_lut;
403 float *total_weight =
s->total_weight + y*
s->linesize;
404 float *sum =
s->sum + y*
s->linesize;
405 const uint32_t *
const iia = ii;
406 const uint32_t *
const iib = ii + dist_b;
407 const uint32_t *
const iid = ii + dist_d;
408 const uint32_t *
const iie = ii + dist_e;
410 dsp->compute_weights_line(iia, iib, iid, iie,
src, total_weight, sum,
411 weight_lut, max_meaningful_diff,
412 td->startx,
td->endx);
420 float *total_weight,
float *sum, ptrdiff_t linesize,
423 for (
int y = 0; y <
h; y++) {
424 for (
int x = 0; x <
w; x++) {
426 total_weight[x] += 1.f;
427 sum[x] += 1.f *
src[x];
432 total_weight += linesize;
438 uint8_t *dst, ptrdiff_t dst_linesize,
446 const uint32_t *centered_ii =
s->ii + e*
s->ii_lz_32 + e;
448 memset(
s->total_weight, 0,
s->linesize *
h *
sizeof(*
s->total_weight));
449 memset(
s->sum, 0,
s->linesize *
h *
sizeof(*
s->sum));
451 for (
int offy = -
r; offy <=
r; offy++) {
452 for (
int offx = -
r; offx <=
r; offx++) {
457 .startx =
FFMAX(0, -offx),
458 .starty =
FFMAX(0, -offy),
461 .ii_start = centered_ii + offy*
s->ii_lz_32 + offx,
467 offx, offy, e,
w,
h);
475 s->total_weight,
s->sum,
s->linesize,
w,
h);
493 for (
int i = 0;
i <
s->nb_planes;
i++) {
494 const int w =
i ?
s->chroma_w :
inlink->w;
495 const int h =
i ?
s->chroma_h :
inlink->h;
496 const int p =
i ?
s->patch_hsize_uv :
s->patch_hsize;
497 const int r =
i ?
s->research_hsize_uv :
s->research_hsize;
507 #define CHECK_ODD_FIELD(field, name) do { \
508 if (!(s->field & 1)) { \
510 av_log(ctx, AV_LOG_WARNING, name " size must be odd, " \
511 "setting it to %d\n", s->field); \
530 const double h =
s->sigma * 10.;
532 s->pdiff_scale = 1. / (
h *
h);
533 s->max_meaningful_diff = log(255.) /
s->pdiff_scale;
534 s->weight_lut =
av_calloc(
s->max_meaningful_diff + 1,
sizeof(*
s->weight_lut));
537 for (
int i = 0;
i <
s->max_meaningful_diff;
i++)
538 s->weight_lut[
i] =
exp(-
i *
s->pdiff_scale);
543 if (!
s->research_size_uv)
s->research_size_uv =
s->research_size;
544 if (!
s->patch_size_uv)
s->patch_size_uv =
s->patch_size;
549 s->research_hsize =
s->research_size / 2;
550 s->research_hsize_uv =
s->research_size_uv / 2;
551 s->patch_hsize =
s->patch_size / 2;
552 s->patch_hsize_uv =
s->patch_size_uv / 2;
555 s->research_size,
s->research_size,
s->research_size_uv,
s->research_size_uv,
556 s->patch_size,
s->patch_size,
s->patch_size_uv,
s->patch_size_uv);
597 .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.
static enum AVPixelFormat pix_fmts[]
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
void ff_nlmeans_init_x86(NLMeansDSPContext *dsp)
av_cold void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp)
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 FILTER_PIXFMTS_ARRAY(array)
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
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
static void compute_weights_line_c(const uint32_t *const iia, const uint32_t *const iib, const uint32_t *const iid, const uint32_t *const iie, const uint8_t *const src, float *total_weight, float *sum, const float *const weight_lut, int max_meaningful_diff, int startx, int endx)
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).
A link between two filters.
#define CHECK_ODD_FIELD(field, name)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
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)
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.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
@ 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...
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...
const AVFilter ff_vf_nlmeans
static void weight_averages(uint8_t *dst, ptrdiff_t dst_linesize, const uint8_t *src, ptrdiff_t src_linesize, float *total_weight, float *sum, ptrdiff_t linesize, int w, int h)
@ 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 AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
#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.
void * av_calloc(size_t nmemb, size_t size)
static av_cold int init(AVFilterContext *ctx)
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)
#define FILTER_OUTPUTS(array)
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
static const AVOption nlmeans_options[]
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
void(* compute_weights_line)(const uint32_t *const iia, const uint32_t *const iib, const uint32_t *const iid, const uint32_t *const iie, const uint8_t *const src, float *total_weight, float *sum, const float *const weight_lut, int max_meaningful_diff, int startx, int endx)
const uint32_t * ii_start
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)