FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/asrc_afirsrc.c
Date: 2022-12-06 04:36:11
Exec Total Coverage
Lines: 0 117 0.0%
Functions: 0 7 0.0%
Branches: 0 68 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2020 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 License
8 * 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
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/channel_layout.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/tx.h"
25 #include "audio.h"
26 #include "avfilter.h"
27 #include "filters.h"
28 #include "internal.h"
29 #include "window_func.h"
30
31 typedef struct AudioFIRSourceContext {
32 const AVClass *class;
33
34 char *freq_points_str;
35 char *magnitude_str;
36 char *phase_str;
37 int nb_taps;
38 int sample_rate;
39 int nb_samples;
40 int win_func;
41
42 AVComplexFloat *complexf;
43 float *freq;
44 float *magnitude;
45 float *phase;
46 int freq_size;
47 int magnitude_size;
48 int phase_size;
49 int nb_freq;
50 int nb_magnitude;
51 int nb_phase;
52
53 float *taps;
54 float *win;
55 int64_t pts;
56
57 AVTXContext *tx_ctx;
58 av_tx_fn tx_fn;
59 } AudioFIRSourceContext;
60
61 #define OFFSET(x) offsetof(AudioFIRSourceContext, x)
62 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
63
64 static const AVOption afirsrc_options[] = {
65 { "taps", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=1025}, 9, UINT16_MAX, FLAGS },
66 { "t", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=1025}, 9, UINT16_MAX, FLAGS },
67 { "frequency", "set frequency points", OFFSET(freq_points_str), AV_OPT_TYPE_STRING, {.str="0 1"}, 0, 0, FLAGS },
68 { "f", "set frequency points", OFFSET(freq_points_str), AV_OPT_TYPE_STRING, {.str="0 1"}, 0, 0, FLAGS },
69 { "magnitude", "set magnitude values", OFFSET(magnitude_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, FLAGS },
70 { "m", "set magnitude values", OFFSET(magnitude_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, FLAGS },
71 { "phase", "set phase values", OFFSET(phase_str), AV_OPT_TYPE_STRING, {.str="0 0"}, 0, 0, FLAGS },
72 { "p", "set phase values", OFFSET(phase_str), AV_OPT_TYPE_STRING, {.str="0 0"}, 0, 0, FLAGS },
73 { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
74 { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
75 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
76 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
77 WIN_FUNC_OPTION("win_func", OFFSET(win_func), FLAGS, WFUNC_BLACKMAN),
78 WIN_FUNC_OPTION("w", OFFSET(win_func), FLAGS, WFUNC_BLACKMAN),
79 {NULL}
80 };
81
82 AVFILTER_DEFINE_CLASS(afirsrc);
83
84 static av_cold int init(AVFilterContext *ctx)
85 {
86 AudioFIRSourceContext *s = ctx->priv;
87
88 if (!(s->nb_taps & 1)) {
89 av_log(s, AV_LOG_WARNING, "Number of taps %d must be odd length.\n", s->nb_taps);
90 s->nb_taps |= 1;
91 }
92
93 return 0;
94 }
95
96 static av_cold void uninit(AVFilterContext *ctx)
97 {
98 AudioFIRSourceContext *s = ctx->priv;
99
100 av_freep(&s->win);
101 av_freep(&s->taps);
102 av_freep(&s->freq);
103 av_freep(&s->magnitude);
104 av_freep(&s->phase);
105 av_freep(&s->complexf);
106 av_tx_uninit(&s->tx_ctx);
107 }
108
109 static av_cold int query_formats(AVFilterContext *ctx)
110 {
111 AudioFIRSourceContext *s = ctx->priv;
112 static const AVChannelLayout chlayouts[] = { AV_CHANNEL_LAYOUT_MONO, { 0 } };
113 int sample_rates[] = { s->sample_rate, -1 };
114 static const enum AVSampleFormat sample_fmts[] = {
115 AV_SAMPLE_FMT_FLT,
116 AV_SAMPLE_FMT_NONE
117 };
118 int ret = ff_set_common_formats_from_list(ctx, sample_fmts);
119 if (ret < 0)
120 return ret;
121
122 ret = ff_set_common_channel_layouts_from_list(ctx, chlayouts);
123 if (ret < 0)
124 return ret;
125
126 return ff_set_common_samplerates_from_list(ctx, sample_rates);
127 }
128
129 static int parse_string(char *str, float **items, int *nb_items, int *items_size)
130 {
131 float *new_items;
132 char *tail;
133
134 new_items = av_fast_realloc(NULL, items_size, 1 * sizeof(float));
135 if (!new_items)
136 return AVERROR(ENOMEM);
137 *items = new_items;
138
139 tail = str;
140 if (!tail)
141 return AVERROR(EINVAL);
142
143 do {
144 (*items)[(*nb_items)++] = av_strtod(tail, &tail);
145 new_items = av_fast_realloc(*items, items_size, (*nb_items + 1) * sizeof(float));
146 if (!new_items)
147 return AVERROR(ENOMEM);
148 *items = new_items;
149 if (tail && *tail)
150 tail++;
151 } while (tail && *tail);
152
153 return 0;
154 }
155
156 static void lininterp(AVComplexFloat *complexf,
157 const float *freq,
158 const float *magnitude,
159 const float *phase,
160 int m, int minterp)
161 {
162 for (int i = 0; i < minterp; i++) {
163 for (int j = 1; j < m; j++) {
164 const float x = i / (float)minterp;
165
166 if (x <= freq[j]) {
167 const float mg = (x - freq[j-1]) / (freq[j] - freq[j-1]) * (magnitude[j] - magnitude[j-1]) + magnitude[j-1];
168 const float ph = (x - freq[j-1]) / (freq[j] - freq[j-1]) * (phase[j] - phase[j-1]) + phase[j-1];
169
170 complexf[i].re = mg * cosf(ph);
171 complexf[i].im = mg * sinf(ph);
172 break;
173 }
174 }
175 }
176 }
177
178 static av_cold int config_output(AVFilterLink *outlink)
179 {
180 AVFilterContext *ctx = outlink->src;
181 AudioFIRSourceContext *s = ctx->priv;
182 float overlap, scale = 1.f, compensation;
183 int fft_size, middle, ret;
184
185 s->nb_freq = s->nb_magnitude = s->nb_phase = 0;
186
187 ret = parse_string(s->freq_points_str, &s->freq, &s->nb_freq, &s->freq_size);
188 if (ret < 0)
189 return ret;
190
191 ret = parse_string(s->magnitude_str, &s->magnitude, &s->nb_magnitude, &s->magnitude_size);
192 if (ret < 0)
193 return ret;
194
195 ret = parse_string(s->phase_str, &s->phase, &s->nb_phase, &s->phase_size);
196 if (ret < 0)
197 return ret;
198
199 if (s->nb_freq != s->nb_magnitude && s->nb_freq != s->nb_phase && s->nb_freq >= 2) {
200 av_log(ctx, AV_LOG_ERROR, "Number of frequencies, magnitudes and phases must be same and >= 2.\n");
201 return AVERROR(EINVAL);
202 }
203
204 for (int i = 0; i < s->nb_freq; i++) {
205 if (i == 0 && s->freq[i] != 0.f) {
206 av_log(ctx, AV_LOG_ERROR, "First frequency must be 0.\n");
207 return AVERROR(EINVAL);
208 }
209
210 if (i == s->nb_freq - 1 && s->freq[i] != 1.f) {
211 av_log(ctx, AV_LOG_ERROR, "Last frequency must be 1.\n");
212 return AVERROR(EINVAL);
213 }
214
215 if (i && s->freq[i] < s->freq[i-1]) {
216 av_log(ctx, AV_LOG_ERROR, "Frequencies must be in increasing order.\n");
217 return AVERROR(EINVAL);
218 }
219 }
220
221 fft_size = 1 << (av_log2(s->nb_taps) + 1);
222 s->complexf = av_calloc(fft_size * 2, sizeof(*s->complexf));
223 if (!s->complexf)
224 return AVERROR(ENOMEM);
225
226 ret = av_tx_init(&s->tx_ctx, &s->tx_fn, AV_TX_FLOAT_FFT, 1, fft_size, &scale, 0);
227 if (ret < 0)
228 return ret;
229
230 s->taps = av_calloc(s->nb_taps, sizeof(*s->taps));
231 if (!s->taps)
232 return AVERROR(ENOMEM);
233
234 s->win = av_calloc(s->nb_taps, sizeof(*s->win));
235 if (!s->win)
236 return AVERROR(ENOMEM);
237
238 generate_window_func(s->win, s->nb_taps, s->win_func, &overlap);
239
240 lininterp(s->complexf, s->freq, s->magnitude, s->phase, s->nb_freq, fft_size / 2);
241
242 s->tx_fn(s->tx_ctx, s->complexf + fft_size, s->complexf, sizeof(*s->complexf));
243
244 compensation = 2.f / fft_size;
245 middle = s->nb_taps / 2;
246
247 for (int i = 0; i <= middle; i++) {
248 s->taps[ i] = s->complexf[fft_size + middle - i].re * compensation * s->win[i];
249 s->taps[middle + i] = s->complexf[fft_size + i].re * compensation * s->win[middle + i];
250 }
251
252 s->pts = 0;
253
254 return 0;
255 }
256
257 static int activate(AVFilterContext *ctx)
258 {
259 AVFilterLink *outlink = ctx->outputs[0];
260 AudioFIRSourceContext *s = ctx->priv;
261 AVFrame *frame;
262 int nb_samples;
263
264 if (!ff_outlink_frame_wanted(outlink))
265 return FFERROR_NOT_READY;
266
267 nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
268 if (nb_samples <= 0) {
269 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
270 return 0;
271 }
272
273 if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
274 return AVERROR(ENOMEM);
275
276 memcpy(frame->data[0], s->taps + s->pts, nb_samples * sizeof(float));
277
278 frame->pts = s->pts;
279 s->pts += nb_samples;
280 return ff_filter_frame(outlink, frame);
281 }
282
283 static const AVFilterPad afirsrc_outputs[] = {
284 {
285 .name = "default",
286 .type = AVMEDIA_TYPE_AUDIO,
287 .config_props = config_output,
288 },
289 };
290
291 const AVFilter ff_asrc_afirsrc = {
292 .name = "afirsrc",
293 .description = NULL_IF_CONFIG_SMALL("Generate a FIR coefficients audio stream."),
294 .init = init,
295 .uninit = uninit,
296 .activate = activate,
297 .priv_size = sizeof(AudioFIRSourceContext),
298 .inputs = NULL,
299 FILTER_OUTPUTS(afirsrc_outputs),
300 FILTER_QUERY_FUNC(query_formats),
301 .priv_class = &afirsrc_class,
302 };
303