FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_afreqshift.c
Date: 2024-04-24 18:52:15
Exec Total Coverage
Lines: 0 120 0.0%
Functions: 0 14 0.0%
Branches: 0 62 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) Paul B Mahol
3 * Copyright (c) Laurent de Soras, 2005
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/ffmath.h"
24 #include "libavutil/opt.h"
25 #include "avfilter.h"
26 #include "audio.h"
27
28 #define MAX_NB_COEFFS 16
29
30 typedef struct AFreqShift {
31 const AVClass *class;
32
33 double shift;
34 double level;
35 int nb_coeffs;
36 int old_nb_coeffs;
37
38 double cd[MAX_NB_COEFFS * 2];
39 float cf[MAX_NB_COEFFS * 2];
40
41 int64_t in_samples;
42
43 AVFrame *i1, *o1;
44 AVFrame *i2, *o2;
45
46 void (*filter_channel)(AVFilterContext *ctx,
47 int channel,
48 AVFrame *in, AVFrame *out);
49 } AFreqShift;
50
51 static const enum AVSampleFormat sample_fmts[] = {
52 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE
53 };
54
55 #define PFILTER(name, type, sin, cos, cc) \
56 static void pfilter_channel_## name(AVFilterContext *ctx, \
57 int ch, \
58 AVFrame *in, AVFrame *out) \
59 { \
60 AFreqShift *s = ctx->priv; \
61 const int nb_samples = in->nb_samples; \
62 const type *src = (const type *)in->extended_data[ch]; \
63 type *dst = (type *)out->extended_data[ch]; \
64 type *i1 = (type *)s->i1->extended_data[ch]; \
65 type *o1 = (type *)s->o1->extended_data[ch]; \
66 type *i2 = (type *)s->i2->extended_data[ch]; \
67 type *o2 = (type *)s->o2->extended_data[ch]; \
68 const int nb_coeffs = s->nb_coeffs; \
69 const type *c = s->cc; \
70 const type level = s->level; \
71 type shift = s->shift * M_PI; \
72 type cos_theta = cos(shift); \
73 type sin_theta = sin(shift); \
74 \
75 for (int n = 0; n < nb_samples; n++) { \
76 type xn1 = src[n], xn2 = src[n]; \
77 type I, Q; \
78 \
79 for (int j = 0; j < nb_coeffs; j++) { \
80 I = c[j] * (xn1 + o2[j]) - i2[j]; \
81 i2[j] = i1[j]; \
82 i1[j] = xn1; \
83 o2[j] = o1[j]; \
84 o1[j] = I; \
85 xn1 = I; \
86 } \
87 \
88 for (int j = nb_coeffs; j < nb_coeffs*2; j++) { \
89 Q = c[j] * (xn2 + o2[j]) - i2[j]; \
90 i2[j] = i1[j]; \
91 i1[j] = xn2; \
92 o2[j] = o1[j]; \
93 o1[j] = Q; \
94 xn2 = Q; \
95 } \
96 Q = o2[nb_coeffs * 2 - 1]; \
97 \
98 dst[n] = (I * cos_theta - Q * sin_theta) * level; \
99 } \
100 }
101
102 PFILTER(flt, float, sin, cos, cf)
103 PFILTER(dbl, double, sin, cos, cd)
104
105 #define FFILTER(name, type, sin, cos, fmod, cc) \
106 static void ffilter_channel_## name(AVFilterContext *ctx, \
107 int ch, \
108 AVFrame *in, AVFrame *out) \
109 { \
110 AFreqShift *s = ctx->priv; \
111 const int nb_samples = in->nb_samples; \
112 const type *src = (const type *)in->extended_data[ch]; \
113 type *dst = (type *)out->extended_data[ch]; \
114 type *i1 = (type *)s->i1->extended_data[ch]; \
115 type *o1 = (type *)s->o1->extended_data[ch]; \
116 type *i2 = (type *)s->i2->extended_data[ch]; \
117 type *o2 = (type *)s->o2->extended_data[ch]; \
118 const int nb_coeffs = s->nb_coeffs; \
119 const type *c = s->cc; \
120 const type level = s->level; \
121 type ts = 1. / in->sample_rate; \
122 type shift = s->shift; \
123 int64_t N = s->in_samples; \
124 \
125 for (int n = 0; n < nb_samples; n++) { \
126 type xn1 = src[n], xn2 = src[n]; \
127 type I, Q, theta; \
128 \
129 for (int j = 0; j < nb_coeffs; j++) { \
130 I = c[j] * (xn1 + o2[j]) - i2[j]; \
131 i2[j] = i1[j]; \
132 i1[j] = xn1; \
133 o2[j] = o1[j]; \
134 o1[j] = I; \
135 xn1 = I; \
136 } \
137 \
138 for (int j = nb_coeffs; j < nb_coeffs*2; j++) { \
139 Q = c[j] * (xn2 + o2[j]) - i2[j]; \
140 i2[j] = i1[j]; \
141 i1[j] = xn2; \
142 o2[j] = o1[j]; \
143 o1[j] = Q; \
144 xn2 = Q; \
145 } \
146 Q = o2[nb_coeffs * 2 - 1]; \
147 \
148 theta = 2. * M_PI * fmod(shift * (N + n) * ts, 1.); \
149 dst[n] = (I * cos(theta) - Q * sin(theta)) * level; \
150 } \
151 }
152
153 FFILTER(flt, float, sinf, cosf, fmodf, cf)
154 FFILTER(dbl, double, sin, cos, fmod, cd)
155
156 static void compute_transition_param(double *K, double *Q, double transition)
157 {
158 double kksqrt, e, e2, e4, k, q;
159
160 k = tan((1. - transition * 2.) * M_PI / 4.);
161 k *= k;
162 kksqrt = pow(1 - k * k, 0.25);
163 e = 0.5 * (1. - kksqrt) / (1. + kksqrt);
164 e2 = e * e;
165 e4 = e2 * e2;
166 q = e * (1. + e4 * (2. + e4 * (15. + 150. * e4)));
167
168 *Q = q;
169 *K = k;
170 }
171
172 static double ipowp(double x, int64_t n)
173 {
174 double z = 1.;
175
176 while (n != 0) {
177 if (n & 1)
178 z *= x;
179 n >>= 1;
180 x *= x;
181 }
182
183 return z;
184 }
185
186 static double compute_acc_num(double q, int order, int c)
187 {
188 int64_t i = 0;
189 int j = 1;
190 double acc = 0.;
191 double q_ii1;
192
193 do {
194 q_ii1 = ipowp(q, i * (i + 1));
195 q_ii1 *= sin((i * 2 + 1) * c * M_PI / order) * j;
196 acc += q_ii1;
197
198 j = -j;
199 i++;
200 } while (fabs(q_ii1) > 1e-100);
201
202 return acc;
203 }
204
205 static double compute_acc_den(double q, int order, int c)
206 {
207 int64_t i = 1;
208 int j = -1;
209 double acc = 0.;
210 double q_i2;
211
212 do {
213 q_i2 = ipowp(q, i * i);
214 q_i2 *= cos(i * 2 * c * M_PI / order) * j;
215 acc += q_i2;
216
217 j = -j;
218 i++;
219 } while (fabs(q_i2) > 1e-100);
220
221 return acc;
222 }
223
224 static double compute_coef(int index, double k, double q, int order)
225 {
226 const int c = index + 1;
227 const double num = compute_acc_num(q, order, c) * pow(q, 0.25);
228 const double den = compute_acc_den(q, order, c) + 0.5;
229 const double ww = num / den;
230 const double wwsq = ww * ww;
231
232 const double x = sqrt((1 - wwsq * k) * (1 - wwsq / k)) / (1 + wwsq);
233 const double coef = (1 - x) / (1 + x);
234
235 return coef;
236 }
237
238 static void compute_coefs(double *coef_arrd, float *coef_arrf, int nbr_coefs, double transition)
239 {
240 const int order = nbr_coefs * 2 + 1;
241 double k, q;
242
243 compute_transition_param(&k, &q, transition);
244
245 for (int n = 0; n < nbr_coefs; n++) {
246 const int idx = (n / 2) + (n & 1) * nbr_coefs / 2;
247
248 coef_arrd[idx] = compute_coef(n, k, q, order);
249 coef_arrf[idx] = coef_arrd[idx];
250 }
251 }
252
253 static int config_input(AVFilterLink *inlink)
254 {
255 AVFilterContext *ctx = inlink->dst;
256 AFreqShift *s = ctx->priv;
257
258 if (s->old_nb_coeffs != s->nb_coeffs)
259 compute_coefs(s->cd, s->cf, s->nb_coeffs * 2, 2. * 20. / inlink->sample_rate);
260 s->old_nb_coeffs = s->nb_coeffs;
261
262 s->i1 = ff_get_audio_buffer(inlink, MAX_NB_COEFFS * 2);
263 s->o1 = ff_get_audio_buffer(inlink, MAX_NB_COEFFS * 2);
264 s->i2 = ff_get_audio_buffer(inlink, MAX_NB_COEFFS * 2);
265 s->o2 = ff_get_audio_buffer(inlink, MAX_NB_COEFFS * 2);
266 if (!s->i1 || !s->o1 || !s->i2 || !s->o2)
267 return AVERROR(ENOMEM);
268
269 if (inlink->format == AV_SAMPLE_FMT_DBLP) {
270 if (!strcmp(ctx->filter->name, "afreqshift"))
271 s->filter_channel = ffilter_channel_dbl;
272 else
273 s->filter_channel = pfilter_channel_dbl;
274 } else {
275 if (!strcmp(ctx->filter->name, "afreqshift"))
276 s->filter_channel = ffilter_channel_flt;
277 else
278 s->filter_channel = pfilter_channel_flt;
279 }
280
281 return 0;
282 }
283
284 typedef struct ThreadData {
285 AVFrame *in, *out;
286 } ThreadData;
287
288 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
289 {
290 AFreqShift *s = ctx->priv;
291 ThreadData *td = arg;
292 AVFrame *out = td->out;
293 AVFrame *in = td->in;
294 const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs;
295 const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
296
297 for (int ch = start; ch < end; ch++)
298 s->filter_channel(ctx, ch, in, out);
299
300 return 0;
301 }
302
303 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
304 {
305 AVFilterContext *ctx = inlink->dst;
306 AVFilterLink *outlink = ctx->outputs[0];
307 AFreqShift *s = ctx->priv;
308 AVFrame *out;
309 ThreadData td;
310
311 if (s->old_nb_coeffs != s->nb_coeffs)
312 compute_coefs(s->cd, s->cf, s->nb_coeffs * 2, 2. * 20. / inlink->sample_rate);
313 s->old_nb_coeffs = s->nb_coeffs;
314
315 if (av_frame_is_writable(in)) {
316 out = in;
317 } else {
318 out = ff_get_audio_buffer(outlink, in->nb_samples);
319 if (!out) {
320 av_frame_free(&in);
321 return AVERROR(ENOMEM);
322 }
323 av_frame_copy_props(out, in);
324 }
325
326 td.in = in; td.out = out;
327 ff_filter_execute(ctx, filter_channels, &td, NULL,
328 FFMIN(inlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
329
330 s->in_samples += in->nb_samples;
331
332 if (out != in)
333 av_frame_free(&in);
334 return ff_filter_frame(outlink, out);
335 }
336
337 static av_cold void uninit(AVFilterContext *ctx)
338 {
339 AFreqShift *s = ctx->priv;
340
341 av_frame_free(&s->i1);
342 av_frame_free(&s->o1);
343 av_frame_free(&s->i2);
344 av_frame_free(&s->o2);
345 }
346
347 #define OFFSET(x) offsetof(AFreqShift, x)
348 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
349
350 static const AVOption afreqshift_options[] = {
351 { "shift", "set frequency shift", OFFSET(shift), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -INT_MAX, INT_MAX, FLAGS },
352 { "level", "set output level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, 1.0, FLAGS },
353 { "order", "set filter order", OFFSET(nb_coeffs),AV_OPT_TYPE_INT, {.i64=8}, 1, MAX_NB_COEFFS, FLAGS },
354 { NULL }
355 };
356
357 AVFILTER_DEFINE_CLASS(afreqshift);
358
359 static const AVFilterPad inputs[] = {
360 {
361 .name = "default",
362 .type = AVMEDIA_TYPE_AUDIO,
363 .filter_frame = filter_frame,
364 .config_props = config_input,
365 },
366 };
367
368 const AVFilter ff_af_afreqshift = {
369 .name = "afreqshift",
370 .description = NULL_IF_CONFIG_SMALL("Apply frequency shifting to input audio."),
371 .priv_size = sizeof(AFreqShift),
372 .priv_class = &afreqshift_class,
373 .uninit = uninit,
374 FILTER_INPUTS(inputs),
375 FILTER_OUTPUTS(ff_audio_default_filterpad),
376 FILTER_SAMPLEFMTS_ARRAY(sample_fmts),
377 .process_command = ff_filter_process_command,
378 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
379 AVFILTER_FLAG_SLICE_THREADS,
380 };
381
382 static const AVOption aphaseshift_options[] = {
383 { "shift", "set phase shift", OFFSET(shift), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1.0, 1.0, FLAGS },
384 { "level", "set output level",OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, 1.0, FLAGS },
385 { "order", "set filter order",OFFSET(nb_coeffs), AV_OPT_TYPE_INT,{.i64=8}, 1, MAX_NB_COEFFS, FLAGS },
386 { NULL }
387 };
388
389 AVFILTER_DEFINE_CLASS(aphaseshift);
390
391 const AVFilter ff_af_aphaseshift = {
392 .name = "aphaseshift",
393 .description = NULL_IF_CONFIG_SMALL("Apply phase shifting to input audio."),
394 .priv_size = sizeof(AFreqShift),
395 .priv_class = &aphaseshift_class,
396 .uninit = uninit,
397 FILTER_INPUTS(inputs),
398 FILTER_OUTPUTS(ff_audio_default_filterpad),
399 FILTER_SAMPLEFMTS_ARRAY(sample_fmts),
400 .process_command = ff_filter_process_command,
401 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
402 AVFILTER_FLAG_SLICE_THREADS,
403 };
404