FFmpeg
Loading...
Searching...
No Matches
af_anlmdn.c
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
21#include <float.h>
22
23#include "libavutil/avassert.h"
24#include "libavutil/opt.h"
25#include "avfilter.h"
26#include "audio.h"
27#include "filters.h"
28
29#include "af_anlmdndsp.h"
30
31#define WEIGHT_LUT_NBITS 20
32#define WEIGHT_LUT_SIZE (1<<WEIGHT_LUT_NBITS)
33
57
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
68static const AVOption anlmdn_options[] = {
69 { "strength", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10000, AFT },
70 { "s", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10000, AFT },
71 { "patch", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AFT },
72 { "p", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AFT },
73 { "research", "set research duration", OFFSET(rd), AV_OPT_TYPE_DURATION, {.i64=6000}, 2000, 300000, AFT },
74 { "r", "set research duration", OFFSET(rd), AV_OPT_TYPE_DURATION, {.i64=6000}, 2000, 300000, AFT },
75 { "output", "set output mode", OFFSET(om), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_MODES-1, AFT, .unit = "mode" },
76 { "o", "set output mode", OFFSET(om), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_MODES-1, AFT, .unit = "mode" },
77 { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AFT, .unit = "mode" },
78 { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AFT, .unit = "mode" },
79 { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE},0, 0, AFT, .unit = "mode" },
80 { "smooth", "set smooth factor", OFFSET(m), AV_OPT_TYPE_FLOAT, {.dbl=11.}, 1, 1000, AFT },
81 { "m", "set smooth factor", OFFSET(m), AV_OPT_TYPE_FLOAT, {.dbl=11.}, 1, 1000, AFT },
82 { NULL }
83};
84
86
87static inline float sqrdiff(float x, float y)
88{
89 const float diff = x - y;
90
91 return diff * diff;
92}
93
94static float compute_distance_ssd_c(const float *f1, const float *f2, ptrdiff_t K)
95{
96 float distance = 0.;
97
98 for (int k = -K; k <= K; k++)
99 distance += sqrdiff(f1[k], f2[k]);
100
101 return distance;
102}
103
104static void compute_cache_c(float *cache, const float *f,
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++)
111 cache[v] += -sqrdiff(f[i - K - 1], f[j - K - 1]) + sqrdiff(f[i + K], f[j + K]);
112}
113
115{
118
119#if ARCH_X86 && HAVE_X86ASM
121#endif
122}
123
125{
126 AudioNLMeansContext *s = ctx->priv;
127 AVFilterLink *outlink = ctx->outputs[0];
128 int newK, newS, newH, newN;
129
130 newK = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE);
131 newS = av_rescale(s->rd, outlink->sample_rate, AV_TIME_BASE);
132
133 newH = newK * 2 + 1;
134 newN = newH + (newK + newS) * 2;
135
136 av_log(ctx, AV_LOG_DEBUG, "K:%d S:%d H:%d N:%d\n", newK, newS, newH, newN);
137
138 if (!s->cache || s->cache->nb_samples < newS * 2) {
139 AVFrame *new_cache = ff_get_audio_buffer(outlink, newS * 2);
140 if (new_cache) {
141 if (s->cache)
142 av_samples_copy(new_cache->extended_data, s->cache->extended_data, 0, 0,
143 s->cache->nb_samples, new_cache->ch_layout.nb_channels, new_cache->format);
144 av_frame_free(&s->cache);
145 s->cache = new_cache;
146 } else {
147 return AVERROR(ENOMEM);
148 }
149 }
150 if (!s->cache)
151 return AVERROR(ENOMEM);
152
153 if (!s->window || s->window->nb_samples < newN) {
154 AVFrame *new_window = ff_get_audio_buffer(outlink, newN);
155 if (new_window) {
156 if (s->window)
157 av_samples_copy(new_window->extended_data, s->window->extended_data, 0, 0,
158 s->window->nb_samples, new_window->ch_layout.nb_channels, new_window->format);
159 av_frame_free(&s->window);
160 s->window = new_window;
161 } else {
162 return AVERROR(ENOMEM);
163 }
164 }
165 if (!s->window)
166 return AVERROR(ENOMEM);
167
168 s->pdiff_lut_scale = 1.f / s->m * WEIGHT_LUT_SIZE;
169 for (int i = 0; i < WEIGHT_LUT_SIZE; i++) {
170 float w = -i / s->pdiff_lut_scale;
171
172 s->weight_lut[i] = expf(w);
173 }
174
175 s->K = newK;
176 s->S = newS;
177 s->H = newH;
178 s->N = newN;
179
180 return 0;
181}
182
183static int config_output(AVFilterLink *outlink)
184{
185 AVFilterContext *ctx = outlink->src;
186 AudioNLMeansContext *s = ctx->priv;
187 int ret;
188
189 ret = config_filter(ctx);
190 if (ret < 0)
191 return ret;
192
193 ff_anlmdn_init(&s->dsp);
194
195 return 0;
196}
197
198static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
199{
200 AudioNLMeansContext *s = ctx->priv;
201 AVFrame *out = arg;
202 const int S = s->S;
203 const int K = s->K;
204 const int N = s->N;
205 const int H = s->H;
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;
213 const float smooth = fminf(s->m, WEIGHT_LUT_SIZE / pdiff_lut_scale);
214 const int offset = N - H;
215 float *src = (float *)s->window->extended_data[ch];
216 const AVFrame *const in = s->in;
217
218 memmove(src, &src[H], offset * sizeof(float));
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
226 if (i == S) {
227 for (int j = i - S; j <= i + S; j++) {
228 if (i == 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++) {
238 float distance = cache[j];
239 unsigned weight_lut_idx;
240 float w;
241
242 if (distance < 0.f)
243 cache[j] = distance = 0.f;
244 w = distance * sw;
245 if (w >= smooth)
246 continue;
247 weight_lut_idx = w * pdiff_lut_scale;
248 av_assert2(weight_lut_idx < WEIGHT_LUT_SIZE);
249 w = weight_lut[weight_lut_idx];
250 P += w * f[i - S + j + (j >= S)];
251 Q += w;
252 }
253
254 P += f[i];
255 Q += 1.f;
256
257 switch (om) {
258 case IN_MODE: dst[i - S] = f[i]; break;
259 case OUT_MODE: dst[i - S] = P / Q; break;
260 case NOISE_MODE: dst[i - S] = f[i] - (P / Q); break;
261 }
262 }
263
264 return 0;
265}
266
267static int filter_frame(AVFilterLink *inlink, AVFrame *in)
268{
269 AVFilterContext *ctx = inlink->dst;
270 AVFilterLink *outlink = ctx->outputs[0];
271 AudioNLMeansContext *s = ctx->priv;
272 AVFrame *out;
273
274 if (av_frame_is_writable(in)) {
275 out = in;
276 } else {
277 out = ff_get_audio_buffer(outlink, in->nb_samples);
278 if (!out) {
279 av_frame_free(&in);
280 return AVERROR(ENOMEM);
281 }
282
283 out->pts = in->pts;
284 }
285
286 s->in = in;
288
289 if (out != in)
290 av_frame_free(&in);
291 return ff_filter_frame(outlink, out);
292}
293
295{
296 AVFilterLink *inlink = ctx->inputs[0];
297 AVFilterLink *outlink = ctx->outputs[0];
298 AudioNLMeansContext *s = ctx->priv;
299 AVFrame *in = NULL;
300 int ret = 0, status;
301 int64_t pts;
302
303 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
304
305 ret = ff_inlink_consume_samples(inlink, s->H, s->H, &in);
306 if (ret < 0)
307 return ret;
308
309 if (ret > 0) {
310 return filter_frame(inlink, in);
311 } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
312 ff_outlink_set_status(outlink, status, pts);
313 return 0;
314 } else {
315 if (ff_inlink_queued_samples(inlink) >= s->H) {
317 } else if (ff_outlink_frame_wanted(outlink)) {
319 }
320 return 0;
321 }
322}
323
324static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
325 char *res, int res_len, int flags)
326{
327 int ret;
328
329 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
330 if (ret < 0)
331 return ret;
332
333 return config_filter(ctx);
334}
335
337{
338 AudioNLMeansContext *s = ctx->priv;
339
340 av_frame_free(&s->cache);
341 av_frame_free(&s->window);
342}
343
344static const AVFilterPad outputs[] = {
345 {
346 .name = "default",
347 .type = AVMEDIA_TYPE_AUDIO,
348 .config_props = config_output,
349 },
350};
351
353 .p.name = "anlmdn",
354 .p.description = NULL_IF_CONFIG_SMALL("Reduce broadband noise from stream using Non-Local Means."),
355 .p.priv_class = &anlmdn_class,
358 .priv_size = sizeof(AudioNLMeansContext),
360 .uninit = uninit,
364 .process_command = process_command,
365};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
OutModes
Definition af_aap.c:32
@ NOISE_MODE
Definition af_aap.c:36
@ IN_MODE
Definition af_aap.c:33
@ OUT_MODE
Definition af_aap.c:35
static const AVFilterPad outputs[]
Definition af_aap.c:310
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
@ NB_MODES
Definition af_afftdn.c:48
static int config_filter(AVFilterContext *ctx)
Definition af_anlmdn.c:124
static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
Definition af_anlmdn.c:198
#define AFT
Definition af_anlmdn.c:66
const FFFilter ff_af_anlmdn
Definition af_anlmdn.c:352
static const AVOption anlmdn_options[]
Definition af_anlmdn.c:68
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition af_anlmdn.c:267
static float compute_distance_ssd_c(const float *f1, const float *f2, ptrdiff_t K)
Definition af_anlmdn.c:94
static void compute_cache_c(float *cache, const float *f, ptrdiff_t S, ptrdiff_t K, ptrdiff_t i, ptrdiff_t jj)
Definition af_anlmdn.c:104
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition af_anlmdn.c:324
static int activate(AVFilterContext *ctx)
Definition af_anlmdn.c:294
#define WEIGHT_LUT_SIZE
Definition af_anlmdn.c:32
static av_cold void uninit(AVFilterContext *ctx)
Definition af_anlmdn.c:336
#define OFFSET(x)
Definition af_anlmdn.c:65
static int config_output(AVFilterLink *outlink)
Definition af_anlmdn.c:183
void ff_anlmdn_init(AudioNLMDNDSPContext *dsp)
Definition af_anlmdn.c:114
static float sqrdiff(float x, float y)
Definition af_anlmdn.c:87
void ff_anlmdn_init_x86(AudioNLMDNDSPContext *s)
#define N
Definition af_mcompand.c:54
static FILE * out
static AVFormatContext * ctx
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
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.
Definition avassert.h:68
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition avfilter.c:1467
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition avfilter.c:1690
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.
Definition avfilter.c:906
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
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.
Definition avfilter.c:1540
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition avfilter.c:229
int ff_inlink_queued_samples(AVFilterLink *link)
Definition avfilter.c:1495
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition avfilter.c:1623
Main libavfilter public API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float sqrtf(float a)
float fminf(float, float)
#define S(s, c, i)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition opt.h:318
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:204
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
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.
Definition samplefmt.c:222
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
int a
#define Q(q)
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
unsigned offset
Definition libaomenc.c:763
const char * arg
Definition jacosubdec.c:65
static void filter_channel(MLPDecodeContext *m, unsigned int substr, unsigned int channel)
Generate PCM samples using the prediction filters and residual values read from the data stream,...
Definition mlpdec.c:972
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition filters.h:629
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:639
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition filters.h:257
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define expf(x)
Definition libm.h:285
uint8_t w
Definition llvidencdsp.c:39
#define P
static float distance(float x, float y, int band)
AVOptions.
#define K
Definition palette.c:25
#define H
Definition pixlet.c:39
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition frame.h:815
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
AVOption.
Definition opt.h:428
float(* compute_distance_ssd)(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)
AudioNLMDNDSPContext dsp
Definition af_anlmdn.c:55
float weight_lut[WEIGHT_LUT_SIZE]
Definition af_anlmdn.c:44
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static int64_t pts
static float smooth(DeshakeOpenCLContext *deshake_ctx, float *gauss_kernel, int length, float max_val, AVFifo *values)
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)