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
58
65
66#define OFFSET(x) offsetof(AudioNLMeansContext, x)
67#define AFT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
68
69static const AVOption anlmdn_options[] = {
70 { "strength", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10000, AFT },
71 { "s", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10000, AFT },
72 { "patch", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AFT },
73 { "p", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AFT },
74 { "research", "set research duration", OFFSET(rd), AV_OPT_TYPE_DURATION, {.i64=6000}, 2000, 300000, AFT },
75 { "r", "set research duration", OFFSET(rd), AV_OPT_TYPE_DURATION, {.i64=6000}, 2000, 300000, AFT },
76 { "output", "set output mode", OFFSET(om), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_MODES-1, AFT, .unit = "mode" },
77 { "o", "set output mode", OFFSET(om), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_MODES-1, AFT, .unit = "mode" },
78 { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AFT, .unit = "mode" },
79 { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AFT, .unit = "mode" },
80 { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE},0, 0, AFT, .unit = "mode" },
81 { "smooth", "set smooth factor", OFFSET(m), AV_OPT_TYPE_FLOAT, {.dbl=11.}, 1, 1000, AFT },
82 { "m", "set smooth factor", OFFSET(m), AV_OPT_TYPE_FLOAT, {.dbl=11.}, 1, 1000, AFT },
83 { NULL }
84};
85
87
88static inline float sqrdiff(float x, float y)
89{
90 const float diff = x - y;
91
92 return diff * diff;
93}
94
95static float compute_distance_ssd_c(const float *f1, const float *f2, ptrdiff_t K)
96{
97 float distance = 0.;
98
99 for (int k = -K; k <= K; k++)
100 distance += sqrdiff(f1[k], f2[k]);
101
102 return distance;
103}
104
105static void compute_cache_c(float *cache, const float *f,
106 ptrdiff_t S, ptrdiff_t K,
107 ptrdiff_t i, ptrdiff_t jj)
108{
109 int v = 0;
110
111 for (int j = jj; j < jj + S; j++, v++)
112 cache[v] += -sqrdiff(f[i - K - 1], f[j - K - 1]) + sqrdiff(f[i + K], f[j + K]);
113}
114
116{
119
120#if ARCH_X86 && HAVE_X86ASM
122#endif
123}
124
126{
127 AudioNLMeansContext *s = ctx->priv;
128 AVFilterLink *outlink = ctx->outputs[0];
129 int newK, newS, newH, newN;
130
131 newK = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE);
132 newS = av_rescale(s->rd, outlink->sample_rate, AV_TIME_BASE);
133
134 newH = newK * 2 + 1;
135 newN = newH + (newK + newS) * 2;
136
137 av_log(ctx, AV_LOG_DEBUG, "K:%d S:%d H:%d N:%d\n", newK, newS, newH, newN);
138
139 if (!s->cache || s->cache->nb_samples < newS * 2) {
140 AVFrame *new_cache = ff_get_audio_buffer(outlink, newS * 2);
141 if (new_cache) {
142 if (s->cache)
143 av_samples_copy(new_cache->extended_data, s->cache->extended_data, 0, 0,
144 s->cache->nb_samples, new_cache->ch_layout.nb_channels, new_cache->format);
145 av_frame_free(&s->cache);
146 s->cache = new_cache;
147 } else {
148 return AVERROR(ENOMEM);
149 }
150 }
151 if (!s->cache)
152 return AVERROR(ENOMEM);
153
154 if (!s->window || s->window->nb_samples < newN) {
155 AVFrame *new_window = ff_get_audio_buffer(outlink, newN);
156 if (new_window) {
157 if (s->window)
158 av_samples_copy(new_window->extended_data, s->window->extended_data, 0, 0,
159 s->window->nb_samples, new_window->ch_layout.nb_channels, new_window->format);
160 av_frame_free(&s->window);
161 s->window = new_window;
162 } else {
163 return AVERROR(ENOMEM);
164 }
165 }
166 if (!s->window)
167 return AVERROR(ENOMEM);
168
169 s->pdiff_lut_scale = 1.f / s->m * WEIGHT_LUT_SIZE;
170 for (int i = 0; i < WEIGHT_LUT_SIZE; i++) {
171 float w = -i / s->pdiff_lut_scale;
172
173 s->weight_lut[i] = expf(w);
174 }
175
176 s->K = newK;
177 s->S = newS;
178 s->H = newH;
179 s->N = newN;
180
181 return 0;
182}
183
184static int config_output(AVFilterLink *outlink)
185{
186 AVFilterContext *ctx = outlink->src;
187 AudioNLMeansContext *s = ctx->priv;
188 int ret;
189
191 if (ret < 0)
192 return ret;
193
194 ff_anlmdn_init(&s->dsp);
195
196 return 0;
197}
198
199static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
200{
201 AudioNLMeansContext *s = ctx->priv;
202 AVFrame *out = arg;
203 const int S = s->S;
204 const int K = s->K;
205 const int N = s->N;
206 const int H = s->H;
207 const int om = s->om;
208 const float *f = (const float *)(s->window->extended_data[ch]) + K;
209 float *cache = (float *)s->cache->extended_data[ch];
210 const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a);
211 float *dst = (float *)out->extended_data[ch];
212 const float *const weight_lut = s->weight_lut;
213 const float pdiff_lut_scale = s->pdiff_lut_scale;
214 const float smooth = fminf(s->m, WEIGHT_LUT_SIZE / pdiff_lut_scale);
215 const int offset = N - H;
216 float *src = (float *)s->window->extended_data[ch];
217 const AVFrame *const in = s->in;
218
219 memmove(src, &src[H], offset * sizeof(float));
220 memcpy(&src[offset], in->extended_data[ch], in->nb_samples * sizeof(float));
221 memset(&src[offset + in->nb_samples], 0, (H - in->nb_samples) * sizeof(float));
222
223 for (int i = S; i < in->nb_samples + S; i++) {
224 float P = 0.f, Q = 0.f;
225 int v = 0;
226
227 if (i == S) {
228 for (int j = i - S; j <= i + S; j++) {
229 if (i == j)
230 continue;
231 cache[v++] = s->dsp.compute_distance_ssd(f + i, f + j, K);
232 }
233 } else {
234 s->dsp.compute_cache(cache, f, S, K, i, i - S);
235 s->dsp.compute_cache(cache + S, f, S, K, i, i + 1);
236 }
237
238 for (int j = 0; j < 2 * S && !ctx->is_disabled; j++) {
239 float distance = cache[j];
240 unsigned weight_lut_idx;
241 float w;
242
243 if (distance < 0.f)
244 cache[j] = distance = 0.f;
245 w = distance * sw;
246 if (w >= smooth)
247 continue;
248 weight_lut_idx = w * pdiff_lut_scale;
249 av_assert2(weight_lut_idx < WEIGHT_LUT_SIZE);
250 w = weight_lut[weight_lut_idx];
251 P += w * f[i - S + j + (j >= S)];
252 Q += w;
253 }
254
255 P += f[i];
256 Q += 1.f;
257
258 switch (om) {
259 case IN_MODE: dst[i - S] = f[i]; break;
260 case OUT_MODE: dst[i - S] = P / Q; break;
261 case NOISE_MODE: dst[i - S] = f[i] - (P / Q); break;
262 }
263 }
264
265 return 0;
266}
267
268static int filter_frame(AVFilterLink *inlink, AVFrame *in)
269{
270 AVFilterContext *ctx = inlink->dst;
271 AVFilterLink *outlink = ctx->outputs[0];
272 AudioNLMeansContext *s = ctx->priv;
273 AVFrame *out;
274
275 if (av_frame_is_writable(in)) {
276 out = in;
277 } else {
278 out = ff_get_audio_buffer(outlink, in->nb_samples);
279 if (!out) {
280 av_frame_free(&in);
281 return AVERROR(ENOMEM);
282 }
283
284 out->pts = in->pts;
285 }
286
287 s->in = in;
289
290 if (out != in)
291 av_frame_free(&in);
292 return ff_filter_frame(outlink, out);
293}
294
296{
297 AVFilterLink *inlink = ctx->inputs[0];
298 AVFilterLink *outlink = ctx->outputs[0];
299 AudioNLMeansContext *s = ctx->priv;
300 AVFrame *in = NULL;
301 int ret = 0, status;
302 int64_t pts;
303
304 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
305
306 if (s->reconfigure) {
307 s->reconfigure = 0;
309 if (ret < 0)
310 return ret;
311 }
312
313 ret = ff_inlink_consume_samples(inlink, s->H, s->H, &in);
314 if (ret < 0)
315 return ret;
316
317 if (ret > 0) {
318 return filter_frame(inlink, in);
319 } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
320 ff_outlink_set_status(outlink, status, pts);
321 return 0;
322 } else {
323 if (ff_inlink_queued_samples(inlink) >= s->H) {
325 } else if (ff_outlink_frame_wanted(outlink)) {
327 }
328 return 0;
329 }
330}
331
332static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
333 char *res, int res_len, int flags)
334{
335 AudioNLMeansContext *s = ctx->priv;
336 int ret;
337
338 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
339 if (ret < 0)
340 return ret;
341
342 s->reconfigure = 1;
343 return 0;
344}
345
347{
348 AudioNLMeansContext *s = ctx->priv;
349
350 av_frame_free(&s->cache);
351 av_frame_free(&s->window);
352}
353
354static const AVFilterPad outputs[] = {
355 {
356 .name = "default",
357 .type = AVMEDIA_TYPE_AUDIO,
358 .config_props = config_output,
359 },
360};
361
363 .p.name = "anlmdn",
364 .p.description = NULL_IF_CONFIG_SMALL("Reduce broadband noise from stream using Non-Local Means."),
365 .p.priv_class = &anlmdn_class,
368 .priv_size = sizeof(AudioNLMeansContext),
370 .uninit = uninit,
374 .process_command = process_command,
375};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
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:125
static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
Definition af_anlmdn.c:199
#define AFT
Definition af_anlmdn.c:67
const FFFilter ff_af_anlmdn
Definition af_anlmdn.c:362
static const AVOption anlmdn_options[]
Definition af_anlmdn.c:69
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition af_anlmdn.c:268
static float compute_distance_ssd_c(const float *f1, const float *f2, ptrdiff_t K)
Definition af_anlmdn.c:95
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:105
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition af_anlmdn.c:332
static int activate(AVFilterContext *ctx)
Definition af_anlmdn.c:295
#define WEIGHT_LUT_SIZE
Definition af_anlmdn.c:32
static av_cold void uninit(AVFilterContext *ctx)
Definition af_anlmdn.c:346
#define OFFSET(x)
Definition af_anlmdn.c:66
static int config_output(AVFilterLink *outlink)
Definition af_anlmdn.c:184
void ff_anlmdn_init(AudioNLMDNDSPContext *dsp)
Definition af_anlmdn.c:115
static float sqrdiff(float x, float y)
Definition af_anlmdn.c:88
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:1474
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1079
int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition avfilter.c:1715
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:917
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1721
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:1547
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition avfilter.c:232
int ff_inlink_queued_samples(AVFilterLink *link)
Definition avfilter.c:1502
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition avfilter.c:1648
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)
int ret
Definition ffplay.c:391
#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:172
#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:210
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:538
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
#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:223
#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:66
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:279
#define FILTER_OUTPUTS(array)
Definition filters.h:280
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:655
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:665
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition filters.h:272
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:493
#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:97
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:279
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:479
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:559
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:581
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition frame.h:825
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:566
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:540
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:56
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)