FFmpeg: libavfilter/af_anlmdn.c Source File
Go to the documentation of this file. 1 /*
2 * Copyright (c) 2019 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
22
28
30
31 #define WEIGHT_LUT_NBITS 20
32 #define WEIGHT_LUT_SIZE (1<<WEIGHT_LUT_NBITS)
33
36
42
45
50
54
57
63 };
64
65 #define OFFSET(x) offsetof(AudioNLMeansContext, x)
66 #define AFT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
67
83 };
84
86
87 static inline float sqrdiff(
float x,
float y)
88 {
89 const float diff = x - y;
90
92 }
93
95 {
97
98 for (
int k = -
K; k <=
K; k++)
100
102 }
103
105 ptrdiff_t
S, ptrdiff_t
K,
106 ptrdiff_t
i, ptrdiff_t jj)
107 {
108 int v = 0;
109
110 for (
int j = jj; j < jj +
S; j++, v++)
112 }
113
115 {
118
119 #if ARCH_X86
121 #endif
122 }
123
125 {
128 int newK, newS, newH, newN;
129
132
133 newH = newK * 2 + 1;
134 newN = newH + (newK + newS) * 2;
135
137
138 if (!
s->cache ||
s->cache->nb_samples < newS * 2) {
140 if (new_cache) {
145 s->cache = new_cache;
146 } else {
148 }
149 }
152
153 if (!
s->window ||
s->window->nb_samples < newN) {
155 if (new_window) {
160 s->window = new_window;
161 } else {
163 }
164 }
167
170 float w = -
i /
s->pdiff_lut_scale;
171
173 }
174
179
180 return 0;
181 }
182
184 {
188
192
194
195 return 0;
196 }
197
199 {
206 const int om =
s->om;
207 const float *
f = (
const float *)(
s->window->extended_data[ch]) +
K;
208 float *cache = (
float *)
s->cache->extended_data[ch];
209 const float sw = (65536.f / (4 *
K + 2)) /
sqrtf(
s->a);
210 float *
dst = (
float *)
out->extended_data[ch];
211 const float *
const weight_lut =
s->weight_lut;
212 const float pdiff_lut_scale =
s->pdiff_lut_scale;
215 float *
src = (
float *)
s->window->extended_data[ch];
217
219 memcpy(&
src[
offset], in->extended_data[ch], in->nb_samples *
sizeof(
float));
220 memset(&
src[
offset + in->nb_samples], 0, (
H - in->nb_samples) *
sizeof(
float));
221
222 for (
int i =
S;
i <
H +
S;
i++) {
223 float P = 0.f,
Q = 0.f;
224 int v = 0;
225
227 for (
int j =
i -
S; j <=
i +
S; j++) {
229 continue;
230 cache[v++] =
s->dsp.compute_distance_ssd(
f +
i,
f + j,
K);
231 }
232 } else {
233 s->dsp.compute_cache(cache,
f,
S,
K,
i,
i -
S);
234 s->dsp.compute_cache(cache +
S,
f,
S,
K,
i,
i + 1);
235 }
236
237 for (
int j = 0; j < 2 *
S && !
ctx->is_disabled; j++) {
239 unsigned weight_lut_idx;
241
246 continue;
247 weight_lut_idx =
w * pdiff_lut_scale;
249 w = weight_lut[weight_lut_idx];
250 P +=
w *
f[
i -
S + j + (j >=
S)];
252 }
253
256
257 switch (om) {
261 }
262 }
263
264 return 0;
265 }
266
268 {
273
276 } else {
281 }
282
284 }
285
288
292 }
293
295 {
302
304
308
313 return 0;
314 } else {
319 }
320 return 0;
321 }
322 }
323
325 char *res,
int res_len,
int flags)
326 {
328
332
334 }
335
337 {
339
342 }
343
345 {
349 },
350 };
351
354 .p.description =
NULL_IF_CONFIG_SMALL(
"Reduce broadband noise from stream using Non-Local Means."),
355 .p.priv_class = &anlmdn_class,
365 };
int av_samples_copy(uint8_t *const *dst, uint8_t *const *src, int dst_offset, int src_offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Copy samples from src to dst.
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
@ AV_SAMPLE_FMT_FLTP
float, planar
void ff_anlmdn_init(AudioNLMDNDSPContext *dsp)
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
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
float(* compute_distance_ssd)(const float *f1, const float *f2, ptrdiff_t K)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
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.
#define FILTER_INPUTS(array)
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
static int config_output(AVFilterLink *outlink)
const char * name
Filter name.
int nb_channels
Number of channels in this layout.
A link between two filters.
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
static const AVFilterPad outputs[]
static int config_filter(AVFilterContext *ctx)
AVChannelLayout ch_layout
Channel layout of the audio data.
const FFFilter ff_af_anlmdn
A filter pad used for either input or output.
static const AVOption anlmdn_options[]
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
float fminf(float, float)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define FILTER_OUTPUTS(array)
Describe the class of an AVClass context structure.
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
AVFILTER_DEFINE_CLASS(anlmdn)
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
static __device__ float sqrtf(float a)
static float sqrdiff(float x, float y)
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
void ff_anlmdn_init_x86(AudioNLMDNDSPContext *s)
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static av_cold void uninit(AVFilterContext *ctx)
static float compute_distance_ssd_c(const float *f1, const float *f2, ptrdiff_t K)
void(* compute_cache)(float *cache, const float *f, ptrdiff_t S, ptrdiff_t K, ptrdiff_t i, ptrdiff_t jj)
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
AVFilterContext * src
source filter
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
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
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
float weight_lut[WEIGHT_LUT_SIZE]
int sample_rate
samples per second
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
int nb_samples
number of audio samples (per channel) described by this frame
#define i(width, name, range_min, range_max)
#define AV_TIME_BASE
Internal time base represented as integer.
uint8_t ** extended_data
pointers to the data planes/channels.
static int activate(AVFilterContext *ctx)
const char * name
Pad name.
int ff_inlink_queued_samples(AVFilterLink *link)
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
@ AV_OPT_TYPE_INT
Underlying C type is int.
static void compute_cache_c(float *cache, const float *f, ptrdiff_t S, ptrdiff_t K, ptrdiff_t i, ptrdiff_t jj)
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
AVFilter p
The public AVFilter.
static float smooth(DeshakeOpenCLContext *deshake_ctx, float *gauss_kernel, int length, float max_val, AVFifo *values)
static float distance(float x, float y, int band)
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Generated on Sat Oct 18 2025 19:22:53 for FFmpeg by
doxygen
1.8.17