68 #define OFFSET(x) offsetof(NLMeansContext, x)
69 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
114 const uint32_t *dst_top = dst - dst_linesize_32;
119 for (y = 0; y <
h; y++) {
120 for (x = 0; x <
w; x += 4) {
121 const int d0 =
s1[x ] -
s2[x ];
122 const int d1 =
s1[x + 1] -
s2[x + 1];
123 const int d2 =
s1[x + 2] -
s2[x + 2];
124 const int d3 =
s1[x + 3] -
s2[x + 3];
126 dst[x ] = dst_top[x ] - dst_top[x - 1] + d0*d0;
127 dst[x + 1] = dst_top[x + 1] - dst_top[x ] + d1*d1;
128 dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2;
129 dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3;
131 dst[x ] += dst[x - 1];
132 dst[x + 1] += dst[x ];
133 dst[x + 2] += dst[x + 1];
134 dst[x + 3] += dst[x + 2];
138 dst += dst_linesize_32;
139 dst_top += dst_linesize_32;
168 int startx,
int starty,
170 int offx,
int offy,
int r,
int sw,
int sh,
175 for (y = starty; y < starty +
h; y++) {
176 uint32_t
acc = dst[y*dst_linesize_32 + startx - 1] - dst[(y-1)*dst_linesize_32 + startx - 1];
177 const int s1y =
av_clip(y -
r, 0, sh - 1);
178 const int s2y =
av_clip(y - (
r + offy), 0, sh - 1);
180 for (x = startx; x < startx +
w; x++) {
181 const int s1x =
av_clip(x -
r, 0, sw - 1);
182 const int s2x =
av_clip(x - (
r + offx), 0, sw - 1);
185 const int d = v1 - v2;
187 dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] +
acc;
210 uint32_t *ii, ptrdiff_t ii_linesize_32,
211 const uint8_t *
src, ptrdiff_t linesize,
int offx,
int offy,
215 const int ii_w =
w + e*2;
216 const int ii_h =
h + e*2;
223 const int s2x = e + offx;
224 const int s2y = e + offy;
228 const int startx_safe =
FFMAX(s1x, s2x);
229 const int starty_safe =
FFMAX(s1y, s2y);
230 const int u_endx_safe =
FFMIN(s1x +
w, s2x +
w);
231 const int endy_safe =
FFMIN(s1y +
h, s2y +
h);
234 const int safe_pw = (u_endx_safe - startx_safe) & ~0xf;
235 const int safe_ph = endy_safe - starty_safe;
238 const int endx_safe = startx_safe + safe_pw;
253 startx_safe, safe_ph);
260 if (safe_pw && safe_ph)
262 src + (starty_safe - s1y) * linesize + (startx_safe - s1x), linesize,
263 src + (starty_safe - s2y) * linesize + (startx_safe - s2x), linesize,
268 endx_safe, starty_safe,
271 ii_w - endx_safe, safe_ph);
278 ii_w, ii_h - endy_safe);
286 const int e =
FFMAX(
s->research_hsize,
s->research_hsize_uv)
287 +
FFMAX(
s->patch_hsize,
s->patch_hsize_uv);
312 s->ii_w = inlink->
w + e*2;
313 s->ii_h = inlink->
h + e*2;
324 s->ii =
s->ii_orig +
s->ii_lz_32 + 1;
327 s->wa_linesize = inlink->
w;
350 const int process_h =
td->endy -
td->starty;
351 const int slice_start =
ff_slice_pos(process_h, jobnr, nb_jobs);
353 const int starty =
td->starty + slice_start;
356 const uint32_t *ii =
td->ii_start + (
starty -
p - 1) *
s->ii_lz_32 -
p - 1;
357 const int dist_b = 2*
p + 1;
358 const int dist_d = dist_b *
s->ii_lz_32;
359 const int dist_e = dist_d + dist_b;
364 for (x =
td->startx; x < td->endx; x++) {
394 const uint32_t
a = ii[x];
395 const uint32_t
b = ii[x + dist_b];
396 const uint32_t d = ii[x + dist_d];
397 const uint32_t e = ii[x + dist_e];
398 const uint32_t patch_diff_sq = e - d -
b +
a;
400 if (patch_diff_sq < s->max_meaningful_diff) {
401 const float weight =
s->weight_lut[patch_diff_sq];
418 for (y = 0; y <
h; y++) {
419 for (x = 0; x <
w; x++) {
422 wa[x].
sum += 1.f *
src[x];
432 uint8_t *dst, ptrdiff_t dst_linesize,
441 const uint32_t *centered_ii =
s->ii + e*
s->ii_lz_32 + e;
443 memset(
s->wa, 0,
s->wa_linesize *
h *
sizeof(*
s->wa));
445 for (offy = -
r; offy <=
r; offy++) {
446 for (offx = -
r; offx <=
r; offx++) {
451 .startx =
FFMAX(0, -offx),
452 .starty =
FFMAX(0, -offy),
455 .ii_start = centered_ii + offy*
s->ii_lz_32 + offx,
461 offx, offy, e,
w,
h);
469 s->wa,
s->wa_linesize,
w,
h);
488 for (
i = 0;
i <
s->nb_planes;
i++) {
489 const int w =
i ?
s->chroma_w : inlink->
w;
490 const int h =
i ?
s->chroma_h : inlink->
h;
491 const int p =
i ?
s->patch_hsize_uv :
s->patch_hsize;
492 const int r =
i ?
s->research_hsize_uv :
s->research_hsize;
495 in->data[
i],
in->linesize[
i]);
502 #define CHECK_ODD_FIELD(field, name) do { \
503 if (!(s->field & 1)) { \
505 av_log(ctx, AV_LOG_WARNING, name " size must be odd, " \
506 "setting it to %d\n", s->field); \
522 const double h =
s->sigma * 10.;
524 s->pdiff_scale = 1. / (
h *
h);
525 s->max_meaningful_diff = log(255.) /
s->pdiff_scale;
526 s->weight_lut =
av_calloc(
s->max_meaningful_diff,
sizeof(*
s->weight_lut));
529 for (
i = 0;
i <
s->max_meaningful_diff;
i++)
530 s->weight_lut[
i] =
exp(-
i *
s->pdiff_scale);
535 if (!
s->research_size_uv)
s->research_size_uv =
s->research_size;
536 if (!
s->patch_size_uv)
s->patch_size_uv =
s->patch_size;
541 s->research_hsize =
s->research_size / 2;
542 s->research_hsize_uv =
s->research_size_uv / 2;
543 s->patch_hsize =
s->patch_size / 2;
544 s->patch_hsize_uv =
s->patch_size_uv / 2;
547 s->research_size,
s->research_size,
s->research_size_uv,
s->research_size_uv,
548 s->patch_size,
s->patch_size,
s->patch_size_uv,
s->patch_size_uv);
590 .priv_class = &nlmeans_class,
static const AVFilterPad inputs[]
static const AVFilterPad outputs[]
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
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
#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_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Main libavfilter public API header.
#define flags(name, subs,...)
#define AV_CEIL_RSHIFT(a, b)
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 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_INFO
Standard information.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
static int weight(int i, int blen, int offset)
AVFILTER_DEFINE_CLASS(nlmeans)
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)
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 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)
static const AVFilterPad nlmeans_inputs[]
static int config_input(AVFilterLink *inlink)
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 const AVFilterPad nlmeans_outputs[]
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
#define CHECK_ODD_FIELD(field, name)
void ff_nlmeans_init(NLMeansDSPContext *dsp)
static av_cold int init(AVFilterContext *ctx)
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)
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static enum AVPixelFormat pix_fmts[]
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
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.
const char * name
Pad name.
const char * name
Filter name.
AVFormatInternal * internal
An opaque field for libavformat internal usage.
This structure describes decoded (raw) audio or video data.
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)
const uint32_t * ii_start
#define av_malloc_array(a, b)
av_cold void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.