FFmpeg coverage


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