FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_aap.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 125 0.0%
Functions: 0 5 0.0%
Branches: 0 107 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2023 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 "formats.h"
30 #include "filters.h"
31
32 enum OutModes {
33 IN_MODE,
34 DESIRED_MODE,
35 OUT_MODE,
36 NOISE_MODE,
37 ERROR_MODE,
38 NB_OMODES
39 };
40
41 typedef struct AudioAPContext {
42 const AVClass *class;
43
44 int order;
45 int projection;
46 float mu;
47 float delta;
48 int output_mode;
49 int precision;
50
51 int kernel_size;
52 AVFrame *offset;
53 AVFrame *delay;
54 AVFrame *coeffs;
55 AVFrame *e;
56 AVFrame *p;
57 AVFrame *x;
58 AVFrame *w;
59 AVFrame *dcoeffs;
60 AVFrame *tmp;
61 AVFrame *tmpm;
62 AVFrame *itmpm;
63
64 void **tmpmp;
65 void **itmpmp;
66
67 AVFrame *frame[2];
68
69 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
70
71 AVFloatDSPContext *fdsp;
72 } AudioAPContext;
73
74 #define OFFSET(x) offsetof(AudioAPContext, x)
75 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
76 #define AT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
77
78 static const AVOption aap_options[] = {
79 { "order", "set the filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=16}, 1, INT16_MAX, A },
80 { "projection", "set the filter projection", OFFSET(projection), AV_OPT_TYPE_INT, {.i64=2}, 1, 256, A },
81 { "mu", "set the filter mu", OFFSET(mu), AV_OPT_TYPE_FLOAT, {.dbl=0.0001},0,1, AT },
82 { "delta", "set the filter delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0.001},0, 1, AT },
83 { "out_mode", "set output mode", OFFSET(output_mode), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_OMODES-1, AT, .unit = "mode" },
84 { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AT, .unit = "mode" },
85 { "d", "desired", 0, AV_OPT_TYPE_CONST, {.i64=DESIRED_MODE}, 0, 0, AT, .unit = "mode" },
86 { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AT, .unit = "mode" },
87 { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE}, 0, 0, AT, .unit = "mode" },
88 { "e", "error", 0, AV_OPT_TYPE_CONST, {.i64=ERROR_MODE}, 0, 0, AT, .unit = "mode" },
89 { "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, A, .unit = "precision" },
90 { "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, .unit = "precision" },
91 { "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, .unit = "precision" },
92 { "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, .unit = "precision" },
93 { NULL }
94 };
95
96 AVFILTER_DEFINE_CLASS(aap);
97
98 static int query_formats(const AVFilterContext *ctx,
99 AVFilterFormatsConfig **cfg_in,
100 AVFilterFormatsConfig **cfg_out)
101 {
102 const AudioAPContext *s = ctx->priv;
103 static const enum AVSampleFormat sample_fmts[3][3] = {
104 { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
105 { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
106 { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
107 };
108 int ret;
109
110 if ((ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out,
111 sample_fmts[s->precision])) < 0)
112 return ret;
113
114 return 0;
115 }
116
117 static int activate(AVFilterContext *ctx)
118 {
119 AudioAPContext *s = ctx->priv;
120 int i, ret, status;
121 int nb_samples;
122 int64_t pts;
123
124 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
125
126 nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]),
127 ff_inlink_queued_samples(ctx->inputs[1]));
128 for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
129 if (s->frame[i])
130 continue;
131
132 if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
133 ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frame[i]);
134 if (ret < 0)
135 return ret;
136 }
137 }
138
139 if (s->frame[0] && s->frame[1]) {
140 AVFrame *out;
141
142 out = ff_get_audio_buffer(ctx->outputs[0], s->frame[0]->nb_samples);
143 if (!out) {
144 av_frame_free(&s->frame[0]);
145 av_frame_free(&s->frame[1]);
146 return AVERROR(ENOMEM);
147 }
148
149 ff_filter_execute(ctx, s->filter_channels, out, NULL,
150 FFMIN(ctx->outputs[0]->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
151
152 out->pts = s->frame[0]->pts;
153 out->duration = s->frame[0]->duration;
154
155 av_frame_free(&s->frame[0]);
156 av_frame_free(&s->frame[1]);
157
158 ret = ff_filter_frame(ctx->outputs[0], out);
159 if (ret < 0)
160 return ret;
161 }
162
163 if (!nb_samples) {
164 for (i = 0; i < 2; i++) {
165 if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
166 ff_outlink_set_status(ctx->outputs[0], status, pts);
167 return 0;
168 }
169 }
170 }
171
172 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
173 for (i = 0; i < 2; i++) {
174 if (s->frame[i] || ff_inlink_queued_samples(ctx->inputs[i]) > 0)
175 continue;
176 ff_inlink_request_frame(ctx->inputs[i]);
177 return 0;
178 }
179 }
180 return 0;
181 }
182
183 #define DEPTH 32
184 #include "aap_template.c"
185
186 #undef DEPTH
187 #define DEPTH 64
188 #include "aap_template.c"
189
190 static int config_output(AVFilterLink *outlink)
191 {
192 const int channels = outlink->ch_layout.nb_channels;
193 AVFilterContext *ctx = outlink->src;
194 AudioAPContext *s = ctx->priv;
195
196 s->kernel_size = FFALIGN(s->order, 16);
197
198 if (!s->offset)
199 s->offset = ff_get_audio_buffer(outlink, 3);
200 if (!s->delay)
201 s->delay = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
202 if (!s->dcoeffs)
203 s->dcoeffs = ff_get_audio_buffer(outlink, s->kernel_size);
204 if (!s->coeffs)
205 s->coeffs = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
206 if (!s->e)
207 s->e = ff_get_audio_buffer(outlink, 2 * s->projection);
208 if (!s->p)
209 s->p = ff_get_audio_buffer(outlink, s->projection + 1);
210 if (!s->x)
211 s->x = ff_get_audio_buffer(outlink, 2 * (s->projection + s->order));
212 if (!s->w)
213 s->w = ff_get_audio_buffer(outlink, s->projection);
214 if (!s->tmp)
215 s->tmp = ff_get_audio_buffer(outlink, s->kernel_size);
216 if (!s->tmpm)
217 s->tmpm = ff_get_audio_buffer(outlink, s->projection * s->projection);
218 if (!s->itmpm)
219 s->itmpm = ff_get_audio_buffer(outlink, s->projection * s->projection);
220
221 if (!s->tmpmp)
222 s->tmpmp = av_calloc(s->projection * channels, sizeof(*s->tmpmp));
223 if (!s->itmpmp)
224 s->itmpmp = av_calloc(s->projection * channels, sizeof(*s->itmpmp));
225
226 if (!s->offset || !s->delay || !s->dcoeffs || !s->coeffs || !s->tmpmp || !s->itmpmp ||
227 !s->e || !s->p || !s->x || !s->w || !s->tmp || !s->tmpm || !s->itmpm)
228 return AVERROR(ENOMEM);
229
230 switch (outlink->format) {
231 case AV_SAMPLE_FMT_DBLP:
232 for (int ch = 0; ch < channels; ch++) {
233 double *itmpm = (double *)s->itmpm->extended_data[ch];
234 double *tmpm = (double *)s->tmpm->extended_data[ch];
235 double **itmpmp = (double **)&s->itmpmp[s->projection * ch];
236 double **tmpmp = (double **)&s->tmpmp[s->projection * ch];
237
238 for (int i = 0; i < s->projection; i++) {
239 itmpmp[i] = &itmpm[i * s->projection];
240 tmpmp[i] = &tmpm[i * s->projection];
241 }
242 }
243
244 s->filter_channels = filter_channels_double;
245 break;
246 case AV_SAMPLE_FMT_FLTP:
247 for (int ch = 0; ch < channels; ch++) {
248 float *itmpm = (float *)s->itmpm->extended_data[ch];
249 float *tmpm = (float *)s->tmpm->extended_data[ch];
250 float **itmpmp = (float **)&s->itmpmp[s->projection * ch];
251 float **tmpmp = (float **)&s->tmpmp[s->projection * ch];
252
253 for (int i = 0; i < s->projection; i++) {
254 itmpmp[i] = &itmpm[i * s->projection];
255 tmpmp[i] = &tmpm[i * s->projection];
256 }
257 }
258
259 s->filter_channels = filter_channels_float;
260 break;
261 }
262
263 return 0;
264 }
265
266 static av_cold int init(AVFilterContext *ctx)
267 {
268 AudioAPContext *s = ctx->priv;
269
270 s->fdsp = avpriv_float_dsp_alloc(0);
271 if (!s->fdsp)
272 return AVERROR(ENOMEM);
273
274 return 0;
275 }
276
277 static av_cold void uninit(AVFilterContext *ctx)
278 {
279 AudioAPContext *s = ctx->priv;
280
281 av_freep(&s->fdsp);
282
283 av_frame_free(&s->offset);
284 av_frame_free(&s->delay);
285 av_frame_free(&s->dcoeffs);
286 av_frame_free(&s->coeffs);
287 av_frame_free(&s->e);
288 av_frame_free(&s->p);
289 av_frame_free(&s->w);
290 av_frame_free(&s->x);
291 av_frame_free(&s->tmp);
292 av_frame_free(&s->tmpm);
293 av_frame_free(&s->itmpm);
294
295 av_freep(&s->tmpmp);
296 av_freep(&s->itmpmp);
297 }
298
299 static const AVFilterPad inputs[] = {
300 {
301 .name = "input",
302 .type = AVMEDIA_TYPE_AUDIO,
303 },
304 {
305 .name = "desired",
306 .type = AVMEDIA_TYPE_AUDIO,
307 },
308 };
309
310 static const AVFilterPad outputs[] = {
311 {
312 .name = "default",
313 .type = AVMEDIA_TYPE_AUDIO,
314 .config_props = config_output,
315 },
316 };
317
318 const AVFilter ff_af_aap = {
319 .name = "aap",
320 .description = NULL_IF_CONFIG_SMALL("Apply Affine Projection algorithm to first audio stream."),
321 .priv_size = sizeof(AudioAPContext),
322 .priv_class = &aap_class,
323 .init = init,
324 .uninit = uninit,
325 .activate = activate,
326 FILTER_INPUTS(inputs),
327 FILTER_OUTPUTS(outputs),
328 FILTER_QUERY_FUNC2(query_formats),
329 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
330 AVFILTER_FLAG_SLICE_THREADS,
331 .process_command = ff_filter_process_command,
332 };
333