64#define OFFSET(x) offsetof(NLMeansContext, x)
65#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
113 int startx,
int starty,
114 const uint8_t *
src, ptrdiff_t linesize,
115 int offx,
int offy,
int r,
int sw,
int sh,
118 for (
int y = starty; y < starty +
h; y++) {
119 uint32_t acc =
dst[y*dst_linesize_32 + startx - 1] -
dst[(y-1)*dst_linesize_32 + startx - 1];
120 const int s1y =
av_clip(y -
r, 0, sh - 1);
121 const int s2y =
av_clip(y - (
r + offy), 0, sh - 1);
123 for (
int x = startx; x < startx +
w; x++) {
124 const int s1x =
av_clip(x -
r, 0, sw - 1);
125 const int s2x =
av_clip(x - (
r + offx), 0, sw - 1);
126 const uint8_t v1 =
src[s1y*linesize + s1x];
127 const uint8_t v2 =
src[s2y*linesize + s2x];
128 const int d = v1 - v2;
130 dst[y*dst_linesize_32 + x] =
dst[(y-1)*dst_linesize_32 + x] + acc;
153 uint32_t *ii, ptrdiff_t ii_linesize_32,
154 const uint8_t *
src, ptrdiff_t linesize,
int offx,
int offy,
158 const int ii_w =
w + e*2;
159 const int ii_h =
h + e*2;
166 const int s2x = e + offx;
167 const int s2y = e + offy;
171 const int startx_safe =
FFMAX(s1x, s2x);
172 const int starty_safe =
FFMAX(s1y, s2y);
173 const int u_endx_safe =
FFMIN(s1x +
w, s2x +
w);
174 const int endy_safe =
FFMIN(s1y +
h, s2y +
h);
177 const int safe_pw = (u_endx_safe - startx_safe) & ~0xf;
178 const int safe_ph = endy_safe - starty_safe;
181 const int endx_safe = startx_safe + safe_pw;
196 startx_safe, safe_ph);
203 if (safe_pw && safe_ph)
205 src + (starty_safe - s1y) * linesize + (startx_safe - s1x), linesize,
206 src + (starty_safe - s2y) * linesize + (startx_safe - s2x), linesize,
211 endx_safe, starty_safe,
214 ii_w - endx_safe, safe_ph);
221 ii_w, ii_h - endy_safe);
229 const int e =
FFMAX(
s->research_hsize,
s->research_hsize_uv)
230 +
FFMAX(
s->patch_hsize,
s->patch_hsize_uv);
255 s->ii_w = inlink->
w + e*2;
256 s->ii_h = inlink->
h + e*2;
262 s->ii_orig =
av_calloc(
s->ii_h + 1,
s->ii_lz_32 *
sizeof(*
s->ii_orig));
267 s->ii =
s->ii_orig +
s->ii_lz_32 + 1;
270 s->linesize = inlink->
w + 100;
273 if (!
s->total_weight || !
s->sum)
291 const uint32_t max_meaningful_diff =
s->max_meaningful_diff;
301 const int dist_b = 2*
p + 1;
302 const int dist_d = dist_b *
s->ii_lz_32;
303 const int dist_e = dist_d + dist_b;
304 const float *
const weight_lut =
s->weight_lut;
309 float *total_weight =
s->total_weight + y*
s->linesize;
310 float *sum =
s->sum + y*
s->linesize;
311 const uint32_t *
const iia = ii;
312 const uint32_t *
const iib = ii + dist_b;
313 const uint32_t *
const iid = ii + dist_d;
314 const uint32_t *
const iie = ii + dist_e;
317 weight_lut, max_meaningful_diff,
326 float *total_weight,
float *sum, ptrdiff_t linesize,
329 for (
int y = 0; y <
h; y++) {
330 for (
int x = 0; x <
w; x++) {
332 total_weight[x] += 1.f;
333 sum[x] += 1.f *
src[x];
338 total_weight += linesize;
344 uint8_t *
dst, ptrdiff_t dst_linesize,
352 const uint32_t *centered_ii =
s->ii + e*
s->ii_lz_32 + e;
354 memset(
s->total_weight, 0,
s->linesize *
h *
sizeof(*
s->total_weight));
355 memset(
s->sum, 0,
s->linesize *
h *
sizeof(*
s->sum));
357 for (
int offy = -
r; offy <=
r; offy++) {
358 for (
int offx = -
r; offx <=
r; offx++) {
363 .startx =
FFMAX(0, -offx),
364 .starty =
FFMAX(0, -offy),
367 .ii_start = centered_ii + offy*
s->ii_lz_32 + offx,
373 offx, offy, e,
w,
h);
381 s->total_weight,
s->sum,
s->linesize,
w,
h);
399 for (
int i = 0;
i <
s->nb_planes;
i++) {
400 const int w =
i ?
s->chroma_w : inlink->
w;
401 const int h =
i ?
s->chroma_h : inlink->
h;
402 const int p =
i ?
s->patch_hsize_uv :
s->patch_hsize;
403 const int r =
i ?
s->research_hsize_uv :
s->research_hsize;
413#define CHECK_ODD_FIELD(field, name) do { \
414 if (!(s->field & 1)) { \
416 av_log(ctx, AV_LOG_WARNING, name " size must be odd, " \
417 "setting it to %d\n", s->field); \
424 const double h =
s->sigma * 10.;
426 s->pdiff_scale = 1. / (
h *
h);
427 s->max_meaningful_diff = log(255.) /
s->pdiff_scale;
428 s->weight_lut =
av_calloc(
s->max_meaningful_diff + 1,
sizeof(*
s->weight_lut));
431 for (
int i = 0;
i <
s->max_meaningful_diff;
i++)
432 s->weight_lut[
i] =
exp(-
i *
s->pdiff_scale);
437 if (!
s->research_size_uv)
s->research_size_uv =
s->research_size;
438 if (!
s->patch_size_uv)
s->patch_size_uv =
s->patch_size;
443 s->research_hsize =
s->research_size / 2;
444 s->research_hsize_uv =
s->research_size_uv / 2;
445 s->patch_hsize =
s->patch_size / 2;
446 s->patch_hsize_uv =
s->patch_size_uv / 2;
449 s->research_size,
s->research_size,
s->research_size_uv,
s->research_size_uv,
450 s->patch_size,
s->patch_size,
s->patch_size_uv,
s->patch_size_uv);
478 .p.priv_class = &nlmeans_class,
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_nlmeans
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
#define AV_CEIL_RSHIFT(a, b)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
int(* init)(AVBSFContext *ctx)
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
#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 AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
#define FILTER_PIXFMTS_ARRAY(array)
#define AVFILTER_DEFINE_CLASS(fname)
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 int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static const AVFilterPad nlmeans_inputs[]
static int config_input(AVFilterLink *inlink)
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)
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).
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
#define CHECK_ODD_FIELD(field, name)
static av_cold void uninit(AVFilterContext *ctx)
static const AVOption nlmeans_options[]
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)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static enum AVPixelFormat pix_fmts[]
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Describe the class of an AVClass context structure.
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVFilterContext * dst
dest filter
int format
agreed upon media format
A filter pad used for either input or output.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
uint32_t max_meaningful_diff
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)
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, ptrdiff_t max_meaningful_diff, ptrdiff_t startx, ptrdiff_t endx)
const uint32_t * ii_start
#define av_malloc_array(a, b)
static AVFormatContext * ctx
static av_unused void ff_nlmeans_init(NLMeansDSPContext *dsp)
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)