46 #define WEIGHT_LUT_NBITS 9
47 #define WEIGHT_LUT_SIZE (1<<WEIGHT_LUT_NBITS)
71 #define OFFSET(x) offsetof(NLMeansContext, x)
72 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
117 const uint32_t *dst_top = dst - dst_linesize_32;
122 for (y = 0; y <
h; y++) {
123 for (x = 0; x <
w; x += 4) {
124 const int d0 = s1[x ] - s2[x ];
125 const int d1 = s1[x + 1] - s2[x + 1];
126 const int d2 = s1[x + 2] - s2[x + 2];
127 const int d3 = s1[x + 3] - s2[x + 3];
129 dst[x ] = dst_top[x ] - dst_top[x - 1] + d0*d0;
130 dst[x + 1] = dst_top[x + 1] - dst_top[x ] + d1*d1;
131 dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2;
132 dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3;
134 dst[x ] += dst[x - 1];
135 dst[x + 1] += dst[x ];
136 dst[x + 2] += dst[x + 1];
137 dst[x + 3] += dst[x + 2];
141 dst += dst_linesize_32;
142 dst_top += dst_linesize_32;
171 int startx,
int starty,
173 int offx,
int offy,
int r,
int sw,
int sh,
178 for (y = starty; y < starty +
h; y++) {
179 uint32_t
acc = dst[y*dst_linesize_32 + startx - 1] - dst[(y-1)*dst_linesize_32 + startx - 1];
180 const int s1y = av_clip(y - r, 0, sh - 1);
181 const int s2y = av_clip(y - (r + offy), 0, sh - 1);
183 for (x = startx; x < startx +
w; x++) {
184 const int s1x = av_clip(x - r, 0, sw - 1);
185 const int s2x = av_clip(x - (r + offx), 0, sw - 1);
186 const uint8_t v1 = src[s1y*linesize + s1x];
187 const uint8_t v2 = src[s2y*linesize + s2x];
188 const int d = v1 - v2;
190 dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] + acc;
213 uint32_t *ii, ptrdiff_t ii_linesize_32,
214 const uint8_t *
src, ptrdiff_t linesize,
int offx,
int offy,
218 const int ii_w = w + e*2;
219 const int ii_h = h + e*2;
226 const int s2x = e + offx;
227 const int s2y = e + offy;
231 const int startx_safe =
FFMAX(s1x, s2x);
232 const int starty_safe =
FFMAX(s1y, s2y);
233 const int u_endx_safe =
FFMIN(s1x + w, s2x + w);
234 const int endy_safe =
FFMIN(s1y + h, s2y + h);
237 const int safe_pw = (u_endx_safe - startx_safe) & ~0
xf;
238 const int safe_ph = endy_safe - starty_safe;
241 const int endx_safe = startx_safe + safe_pw;
256 startx_safe, safe_ph);
263 if (safe_pw && safe_ph)
265 src + (starty_safe - s1y) * linesize + (startx_safe - s1x), linesize,
266 src + (starty_safe - s2y) * linesize + (startx_safe - s2x), linesize,
271 endx_safe, starty_safe,
274 ii_w - endx_safe, safe_ph);
281 ii_w, ii_h - endy_safe);
315 s->
ii_w = inlink->
w + e*2;
316 s->
ii_h = inlink->
h + e*2;
354 const int slice_start = (process_h * jobnr ) / nb_jobs;
355 const int slice_end = (process_h * (jobnr+1)) / nb_jobs;
360 const int dist_b = 2*p + 1;
361 const int dist_d = dist_b * s->
ii_lz_32;
362 const int dist_e = dist_d + dist_b;
364 for (y = starty; y <
endy; y++) {
367 for (x = td->
startx; x < td->endx; x++) {
397 const uint32_t
a = ii[x];
398 const uint32_t
b = ii[x + dist_b];
399 const uint32_t d = ii[x + dist_d];
400 const uint32_t e = ii[x + dist_e];
401 const uint32_t patch_diff_sq = e - d - b +
a;
403 if (patch_diff_sq < s->max_meaningful_diff) {
407 wa[x].
sum += weight * src[x];
422 for (y = 0; y <
h; y++) {
423 for (x = 0; x <
w; x++) {
426 wa[x].
sum += 1.f * src[x];
436 uint8_t *dst, ptrdiff_t dst_linesize,
445 const uint32_t *centered_ii = s->
ii + e*s->
ii_lz_32 + e;
449 for (offy = -r; offy <=
r; offy++) {
450 for (offx = -r; offx <=
r; offx++) {
453 .
src = src + offy*src_linesize + offx,
455 .startx =
FFMAX(0, -offx),
456 .starty =
FFMAX(0, -offy),
457 .endx =
FFMIN(w, w - offx),
458 .endy =
FFMIN(h, h - offy),
459 .ii_start = centered_ii + offy*s->
ii_lz_32 + offx,
465 offx, offy, e, w, h);
506 #define CHECK_ODD_FIELD(field, name) do { \
507 if (!(s->field & 1)) { \
509 av_log(ctx, AV_LOG_WARNING, name " size must be odd, " \
510 "setting it to %d\n", s->field); \
526 const double h = s->
sigma * 10.;
549 av_log(ctx,
AV_LOG_INFO,
"Research window: %dx%d / %dx%d, patch size: %dx%d / %dx%d\n",
592 .priv_class = &nlmeans_class,
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Main libavfilter public API header.
static const AVFilterPad nlmeans_outputs[]
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 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)
int h
agreed upon image height
#define CHECK_ODD_FIELD(field, name)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
const char * name
Pad name.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
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).
static int query_formats(AVFilterContext *ctx)
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
A filter pad used for either input or output.
A link between two filters.
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)
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
uint32_t max_meaningful_diff
void * priv
private data for use by the filter
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
simple assert() macros that are a bit more flexible than ISO C assert().
static const AVFilterPad nlmeans_inputs[]
float weight_lut[WEIGHT_LUT_SIZE]
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
int w
agreed upon image width
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_cold void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp)
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.
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
static int config_input(AVFilterLink *inlink)
static const AVFilterPad inputs[]
static const AVFilterPad outputs[]
int format
agreed upon media format
#define FF_ARRAY_ELEMS(a)
static av_cold int init(AVFilterContext *ctx)
#define AV_LOG_INFO
Standard information.
static av_cold void uninit(AVFilterContext *ctx)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Describe the class of an AVClass context structure.
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
const char * name
Filter name.
AVFilterLink ** outputs
array of pointers to output links
static int weight(int i, int blen, int offset)
static enum AVPixelFormat pix_fmts[]
#define flags(name, subs,...)
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
avfilter_execute_func * execute
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
static const AVOption nlmeans_options[]
#define xf(width, name, var, range_min, range_max, subs,...)
AVFilterContext * dst
dest filter
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
AVFILTER_DEFINE_CLASS(nlmeans)
#define av_malloc_array(a, b)
void ff_nlmeans_init(NLMeansDSPContext *dsp)
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)
AVPixelFormat
Pixel format.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
const uint32_t * ii_start
void * av_mallocz_array(size_t nmemb, size_t size)
#define AV_CEIL_RSHIFT(a, b)