FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_anlms.c
Date: 2024-07-14 13:34:57
Exec Total Coverage
Lines: 0 82 0.0%
Functions: 0 5 0.0%
Branches: 0 65 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 "libavutil/channel_layout.h"
22 #include "libavutil/common.h"
23 #include "libavutil/float_dsp.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/opt.h"
26
27 #include "audio.h"
28 #include "avfilter.h"
29 #include "filters.h"
30 #include "formats.h"
31 #include "internal.h"
32
33 enum OutModes {
34 IN_MODE,
35 DESIRED_MODE,
36 OUT_MODE,
37 NOISE_MODE,
38 ERROR_MODE,
39 NB_OMODES
40 };
41
42 typedef struct AudioNLMSContext {
43 const AVClass *class;
44
45 int order;
46 float mu;
47 float eps;
48 float leakage;
49 int output_mode;
50 int precision;
51
52 int kernel_size;
53 AVFrame *offset;
54 AVFrame *delay;
55 AVFrame *coeffs;
56 AVFrame *tmp;
57
58 AVFrame *frame[2];
59
60 int anlmf;
61
62 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
63
64 AVFloatDSPContext *fdsp;
65 } AudioNLMSContext;
66
67 #define OFFSET(x) offsetof(AudioNLMSContext, x)
68 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
69 #define AT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
70
71 static const AVOption anlms_options[] = {
72 { "order", "set the filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=256}, 1, INT16_MAX, A },
73 { "mu", "set the filter mu", OFFSET(mu), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 2, AT },
74 { "eps", "set the filter eps", OFFSET(eps), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AT },
75 { "leakage", "set the filter leakage", OFFSET(leakage), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, AT },
76 { "out_mode", "set output mode", OFFSET(output_mode), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_OMODES-1, AT, .unit = "mode" },
77 { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AT, .unit = "mode" },
78 { "d", "desired", 0, AV_OPT_TYPE_CONST, {.i64=DESIRED_MODE}, 0, 0, AT, .unit = "mode" },
79 { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AT, .unit = "mode" },
80 { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE}, 0, 0, AT, .unit = "mode" },
81 { "e", "error", 0, AV_OPT_TYPE_CONST, {.i64=ERROR_MODE}, 0, 0, AT, .unit = "mode" },
82 { "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, A, .unit = "precision" },
83 { "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, .unit = "precision" },
84 { "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, .unit = "precision" },
85 { "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, .unit = "precision" },
86 { NULL }
87 };
88
89 AVFILTER_DEFINE_CLASS_EXT(anlms, "anlm(f|s)", anlms_options);
90
91 static int query_formats(AVFilterContext *ctx)
92 {
93 AudioNLMSContext *s = ctx->priv;
94 static const enum AVSampleFormat sample_fmts[3][3] = {
95 { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
96 { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
97 { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
98 };
99 int ret;
100
101 if ((ret = ff_set_common_all_channel_counts(ctx)) < 0)
102 return ret;
103
104 if ((ret = ff_set_common_formats_from_list(ctx, sample_fmts[s->precision])) < 0)
105 return ret;
106
107 return ff_set_common_all_samplerates(ctx);
108 }
109
110 static int activate(AVFilterContext *ctx)
111 {
112 AudioNLMSContext *s = ctx->priv;
113 int i, ret, status;
114 int nb_samples;
115 int64_t pts;
116
117 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
118
119 nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]),
120 ff_inlink_queued_samples(ctx->inputs[1]));
121 for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
122 if (s->frame[i])
123 continue;
124
125 if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
126 ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frame[i]);
127 if (ret < 0)
128 return ret;
129 }
130 }
131
132 if (s->frame[0] && s->frame[1]) {
133 AVFrame *out;
134
135 out = ff_get_audio_buffer(ctx->outputs[0], s->frame[0]->nb_samples);
136 if (!out) {
137 av_frame_free(&s->frame[0]);
138 av_frame_free(&s->frame[1]);
139 return AVERROR(ENOMEM);
140 }
141
142 ff_filter_execute(ctx, s->filter_channels, out, NULL,
143 FFMIN(ctx->outputs[0]->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
144
145 out->pts = s->frame[0]->pts;
146 out->duration = s->frame[0]->duration;
147
148 av_frame_free(&s->frame[0]);
149 av_frame_free(&s->frame[1]);
150
151 ret = ff_filter_frame(ctx->outputs[0], out);
152 if (ret < 0)
153 return ret;
154 }
155
156 if (!nb_samples) {
157 for (i = 0; i < 2; i++) {
158 if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
159 ff_outlink_set_status(ctx->outputs[0], status, pts);
160 return 0;
161 }
162 }
163 }
164
165 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
166 for (i = 0; i < 2; i++) {
167 if (s->frame[i] || ff_inlink_queued_samples(ctx->inputs[i]) > 0)
168 continue;
169 ff_inlink_request_frame(ctx->inputs[i]);
170 return 0;
171 }
172 }
173 return 0;
174 }
175
176 #define DEPTH 32
177 #include "anlms_template.c"
178
179 #undef DEPTH
180 #define DEPTH 64
181 #include "anlms_template.c"
182
183 static int config_output(AVFilterLink *outlink)
184 {
185 AVFilterContext *ctx = outlink->src;
186 AudioNLMSContext *s = ctx->priv;
187
188 s->anlmf = !strcmp(ctx->filter->name, "anlmf");
189 s->kernel_size = FFALIGN(s->order, 16);
190
191 if (!s->offset)
192 s->offset = ff_get_audio_buffer(outlink, 1);
193 if (!s->delay)
194 s->delay = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
195 if (!s->coeffs)
196 s->coeffs = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
197 if (!s->tmp)
198 s->tmp = ff_get_audio_buffer(outlink, s->kernel_size);
199 if (!s->delay || !s->coeffs || !s->offset || !s->tmp)
200 return AVERROR(ENOMEM);
201
202 switch (outlink->format) {
203 case AV_SAMPLE_FMT_DBLP:
204 s->filter_channels = filter_channels_double;
205 break;
206 case AV_SAMPLE_FMT_FLTP:
207 s->filter_channels = filter_channels_float;
208 break;
209 }
210
211 return 0;
212 }
213
214 static av_cold int init(AVFilterContext *ctx)
215 {
216 AudioNLMSContext *s = ctx->priv;
217
218 s->fdsp = avpriv_float_dsp_alloc(0);
219 if (!s->fdsp)
220 return AVERROR(ENOMEM);
221
222 return 0;
223 }
224
225 static av_cold void uninit(AVFilterContext *ctx)
226 {
227 AudioNLMSContext *s = ctx->priv;
228
229 av_freep(&s->fdsp);
230 av_frame_free(&s->delay);
231 av_frame_free(&s->coeffs);
232 av_frame_free(&s->offset);
233 av_frame_free(&s->tmp);
234 }
235
236 static const AVFilterPad inputs[] = {
237 {
238 .name = "input",
239 .type = AVMEDIA_TYPE_AUDIO,
240 },
241 {
242 .name = "desired",
243 .type = AVMEDIA_TYPE_AUDIO,
244 },
245 };
246
247 static const AVFilterPad outputs[] = {
248 {
249 .name = "default",
250 .type = AVMEDIA_TYPE_AUDIO,
251 .config_props = config_output,
252 },
253 };
254
255 const AVFilter ff_af_anlms = {
256 .name = "anlms",
257 .description = NULL_IF_CONFIG_SMALL("Apply Normalized Least-Mean-Squares algorithm to first audio stream."),
258 .priv_size = sizeof(AudioNLMSContext),
259 .priv_class = &anlms_class,
260 .init = init,
261 .uninit = uninit,
262 .activate = activate,
263 FILTER_INPUTS(inputs),
264 FILTER_OUTPUTS(outputs),
265 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP),
266 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
267 AVFILTER_FLAG_SLICE_THREADS,
268 .process_command = ff_filter_process_command,
269 };
270
271 const AVFilter ff_af_anlmf = {
272 .name = "anlmf",
273 .description = NULL_IF_CONFIG_SMALL("Apply Normalized Least-Mean-Fourth algorithm to first audio stream."),
274 .priv_size = sizeof(AudioNLMSContext),
275 .priv_class = &anlms_class,
276 .init = init,
277 .uninit = uninit,
278 .activate = activate,
279 FILTER_INPUTS(inputs),
280 FILTER_OUTPUTS(outputs),
281 FILTER_QUERY_FUNC(query_formats),
282 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
283 AVFILTER_FLAG_SLICE_THREADS,
284 .process_command = ff_filter_process_command,
285 };
286