FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_anlmdn.c
Date: 2024-12-12 01:08:13
Exec Total Coverage
Lines: 0 160 0.0%
Functions: 0 11 0.0%
Branches: 0 68 0.0%

Line Branch Exec Source
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
34 typedef struct AudioNLMeansContext {
35 const AVClass *class;
36
37 float a;
38 int64_t pd;
39 int64_t rd;
40 float m;
41 int om;
42
43 float pdiff_lut_scale;
44 float weight_lut[WEIGHT_LUT_SIZE];
45
46 int K;
47 int S;
48 int N;
49 int H;
50
51 AVFrame *in;
52 AVFrame *cache;
53 AVFrame *window;
54
55 AudioNLMDNDSPContext dsp;
56 } AudioNLMeansContext;
57
58 enum OutModes {
59 IN_MODE,
60 OUT_MODE,
61 NOISE_MODE,
62 NB_MODES
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
68 static 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
85 AVFILTER_DEFINE_CLASS(anlmdn);
86
87 static inline float sqrdiff(float x, float y)
88 {
89 const float diff = x - y;
90
91 return diff * diff;
92 }
93
94 static 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
104 static 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
114 void ff_anlmdn_init(AudioNLMDNDSPContext *dsp)
115 {
116 dsp->compute_distance_ssd = compute_distance_ssd_c;
117 dsp->compute_cache = compute_cache_c;
118
119 #if ARCH_X86
120 ff_anlmdn_init_x86(dsp);
121 #endif
122 }
123
124 static int config_filter(AVFilterContext *ctx)
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
183 static 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
198 static 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
267 static 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;
287 ff_filter_execute(ctx, filter_channel, out, NULL, inlink->ch_layout.nb_channels);
288
289 if (out != in)
290 av_frame_free(&in);
291 return ff_filter_frame(outlink, out);
292 }
293
294 static int activate(AVFilterContext *ctx)
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) {
316 ff_filter_set_ready(ctx, 10);
317 } else if (ff_outlink_frame_wanted(outlink)) {
318 ff_inlink_request_frame(inlink);
319 }
320 return 0;
321 }
322 }
323
324 static 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
336 static av_cold void uninit(AVFilterContext *ctx)
337 {
338 AudioNLMeansContext *s = ctx->priv;
339
340 av_frame_free(&s->cache);
341 av_frame_free(&s->window);
342 }
343
344 static const AVFilterPad outputs[] = {
345 {
346 .name = "default",
347 .type = AVMEDIA_TYPE_AUDIO,
348 .config_props = config_output,
349 },
350 };
351
352 const AVFilter ff_af_anlmdn = {
353 .name = "anlmdn",
354 .description = NULL_IF_CONFIG_SMALL("Reduce broadband noise from stream using Non-Local Means."),
355 .priv_size = sizeof(AudioNLMeansContext),
356 .priv_class = &anlmdn_class,
357 .activate = activate,
358 .uninit = uninit,
359 FILTER_INPUTS(ff_audio_default_filterpad),
360 FILTER_OUTPUTS(outputs),
361 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP),
362 .process_command = process_command,
363 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
364 AVFILTER_FLAG_SLICE_THREADS,
365 };
366