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);
182 const uint8_t v1 = src[s1y*linesize + s1x];
183 const uint8_t v2 = src[s2y*linesize + s2x];
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);
311 s->
ii_w = inlink->
w + e*2;
312 s->
ii_h = inlink->
h + e*2;
350 const int slice_start = (process_h * jobnr ) / nb_jobs;
351 const int slice_end = (process_h * (jobnr+1)) / nb_jobs;
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;
360 for (y = starty; y <
endy; y++) {
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) {
402 wa[x].
sum += weight * src[x];
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;
444 for (offy = -r; offy <=
r; offy++) {
445 for (offx = -r; offx <=
r; offx++) {
448 .
src = src + offy*src_linesize + offx,
450 .startx =
FFMAX(0, -offx),
451 .starty =
FFMAX(0, -offy),
452 .endx =
FFMIN(w, w - offx),
453 .endy =
FFMIN(h, h - offy),
454 .ii_start = centered_ii + offy*s->
ii_lz_32 + offx,
460 offx, offy, e, w, h);
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.;
545 av_log(ctx,
AV_LOG_INFO,
"Research window: %dx%d / %dx%d, patch size: %dx%d / %dx%d\n",
589 .priv_class = &nlmeans_class,
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
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)
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[]
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)
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
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.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
#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.
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[]
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 outputs[]
int format
agreed upon media format
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...
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
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
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.
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
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)
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(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)