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 #include "formats.h"
28
29 #define NB_COEFS 16
30
31 typedef struct AFreqShift {
32 const AVClass *class;
33
34 double shift;
35 double level;
36
37 double cd[NB_COEFS];
38 float cf[NB_COEFS];
39
40 int64_t in_samples;
41
42 AVFrame *i1, *o1;
43 AVFrame *i2, *o2;
44
45 void (*filter_channel)(AVFilterContext *ctx,
46 int channel,
47 AVFrame *in, AVFrame *out);
48 } AFreqShift;
49
query_formats(AVFilterContext * ctx)50 static int query_formats(AVFilterContext *ctx)
51 {
52 AVFilterFormats *formats = NULL;
53 AVFilterChannelLayouts *layouts = NULL;
54 static const enum AVSampleFormat sample_fmts[] = {
55 AV_SAMPLE_FMT_FLTP,
56 AV_SAMPLE_FMT_DBLP,
57 AV_SAMPLE_FMT_NONE
58 };
59 int ret;
60
61 formats = ff_make_format_list(sample_fmts);
62 if (!formats)
63 return AVERROR(ENOMEM);
64 ret = ff_set_common_formats(ctx, formats);
65 if (ret < 0)
66 return ret;
67
68 layouts = ff_all_channel_counts();
69 if (!layouts)
70 return AVERROR(ENOMEM);
71
72 ret = ff_set_common_channel_layouts(ctx, layouts);
73 if (ret < 0)
74 return ret;
75
76 formats = ff_all_samplerates();
77 return ff_set_common_samplerates(ctx, formats);
78 }
79
80 #define PFILTER(name, type, sin, cos, cc) \
81 static void pfilter_channel_## name(AVFilterContext *ctx, \
82 int ch, \
83 AVFrame *in, AVFrame *out) \
84 { \
85 AFreqShift *s = ctx->priv; \
86 const int nb_samples = in->nb_samples; \
87 const type *src = (const type *)in->extended_data[ch]; \
88 type *dst = (type *)out->extended_data[ch]; \
89 type *i1 = (type *)s->i1->extended_data[ch]; \
90 type *o1 = (type *)s->o1->extended_data[ch]; \
91 type *i2 = (type *)s->i2->extended_data[ch]; \
92 type *o2 = (type *)s->o2->extended_data[ch]; \
93 const type *c = s->cc; \
94 const type level = s->level; \
95 type shift = s->shift * M_PI; \
96 type cos_theta = cos(shift); \
97 type sin_theta = sin(shift); \
98 \
99 for (int n = 0; n < nb_samples; n++) { \
100 type xn1 = src[n], xn2 = src[n]; \
101 type I, Q; \
102 \
103 for (int j = 0; j < NB_COEFS / 2; j++) { \
104 I = c[j] * (xn1 + o2[j]) - i2[j]; \
105 i2[j] = i1[j]; \
106 i1[j] = xn1; \
107 o2[j] = o1[j]; \
108 o1[j] = I; \
109 xn1 = I; \
110 } \
111 \
112 for (int j = NB_COEFS / 2; j < NB_COEFS; j++) { \
113 Q = c[j] * (xn2 + o2[j]) - i2[j]; \
114 i2[j] = i1[j]; \
115 i1[j] = xn2; \
116 o2[j] = o1[j]; \
117 o1[j] = Q; \
118 xn2 = Q; \
119 } \
120 Q = o2[NB_COEFS - 1]; \
121 \
122 dst[n] = (I * cos_theta - Q * sin_theta) * level; \
123 } \
124 }
125
PFILTER(flt,float,sin,cos,cf)126 PFILTER(flt, float, sin, cos, cf)
127 PFILTER(dbl, double, sin, cos, cd)
128
129 #define FFILTER(name, type, sin, cos, fmod, cc) \
130 static void ffilter_channel_## name(AVFilterContext *ctx, \
131 int ch, \
132 AVFrame *in, AVFrame *out) \
133 { \
134 AFreqShift *s = ctx->priv; \
135 const int nb_samples = in->nb_samples; \
136 const type *src = (const type *)in->extended_data[ch]; \
137 type *dst = (type *)out->extended_data[ch]; \
138 type *i1 = (type *)s->i1->extended_data[ch]; \
139 type *o1 = (type *)s->o1->extended_data[ch]; \
140 type *i2 = (type *)s->i2->extended_data[ch]; \
141 type *o2 = (type *)s->o2->extended_data[ch]; \
142 const type *c = s->cc; \
143 const type level = s->level; \
144 type ts = 1. / in->sample_rate; \
145 type shift = s->shift; \
146 int64_t N = s->in_samples; \
147 \
148 for (int n = 0; n < nb_samples; n++) { \
149 type xn1 = src[n], xn2 = src[n]; \
150 type I, Q, theta; \
151 \
152 for (int j = 0; j < NB_COEFS / 2; j++) { \
153 I = c[j] * (xn1 + o2[j]) - i2[j]; \
154 i2[j] = i1[j]; \
155 i1[j] = xn1; \
156 o2[j] = o1[j]; \
157 o1[j] = I; \
158 xn1 = I; \
159 } \
160 \
161 for (int j = NB_COEFS / 2; j < NB_COEFS; j++) { \
162 Q = c[j] * (xn2 + o2[j]) - i2[j]; \
163 i2[j] = i1[j]; \
164 i1[j] = xn2; \
165 o2[j] = o1[j]; \
166 o1[j] = Q; \
167 xn2 = Q; \
168 } \
169 Q = o2[NB_COEFS - 1]; \
170 \
171 theta = 2. * M_PI * fmod(shift * (N + n) * ts, 1.); \
172 dst[n] = (I * cos(theta) - Q * sin(theta)) * level; \
173 } \
174 }
175
176 FFILTER(flt, float, sinf, cosf, fmodf, cf)
177 FFILTER(dbl, double, sin, cos, fmod, cd)
178
179 static void compute_transition_param(double *K, double *Q, double transition)
180 {
181 double kksqrt, e, e2, e4, k, q;
182
183 k = tan((1. - transition * 2.) * M_PI / 4.);
184 k *= k;
185 kksqrt = pow(1 - k * k, 0.25);
186 e = 0.5 * (1. - kksqrt) / (1. + kksqrt);
187 e2 = e * e;
188 e4 = e2 * e2;
189 q = e * (1. + e4 * (2. + e4 * (15. + 150. * e4)));
190
191 *Q = q;
192 *K = k;
193 }
194
ipowp(double x,int64_t n)195 static double ipowp(double x, int64_t n)
196 {
197 double z = 1.;
198
199 while (n != 0) {
200 if (n & 1)
201 z *= x;
202 n >>= 1;
203 x *= x;
204 }
205
206 return z;
207 }
208
compute_acc_num(double q,int order,int c)209 static double compute_acc_num(double q, int order, int c)
210 {
211 int64_t i = 0;
212 int j = 1;
213 double acc = 0.;
214 double q_ii1;
215
216 do {
217 q_ii1 = ipowp(q, i * (i + 1));
218 q_ii1 *= sin((i * 2 + 1) * c * M_PI / order) * j;
219 acc += q_ii1;
220
221 j = -j;
222 i++;
223 } while (fabs(q_ii1) > 1e-100);
224
225 return acc;
226 }
227
compute_acc_den(double q,int order,int c)228 static double compute_acc_den(double q, int order, int c)
229 {
230 int64_t i = 1;
231 int j = -1;
232 double acc = 0.;
233 double q_i2;
234
235 do {
236 q_i2 = ipowp(q, i * i);
237 q_i2 *= cos(i * 2 * c * M_PI / order) * j;
238 acc += q_i2;
239
240 j = -j;
241 i++;
242 } while (fabs(q_i2) > 1e-100);
243
244 return acc;
245 }
246
compute_coef(int index,double k,double q,int order)247 static double compute_coef(int index, double k, double q, int order)
248 {
249 const int c = index + 1;
250 const double num = compute_acc_num(q, order, c) * pow(q, 0.25);
251 const double den = compute_acc_den(q, order, c) + 0.5;
252 const double ww = num / den;
253 const double wwsq = ww * ww;
254
255 const double x = sqrt((1 - wwsq * k) * (1 - wwsq / k)) / (1 + wwsq);
256 const double coef = (1 - x) / (1 + x);
257
258 return coef;
259 }
260
compute_coefs(double * coef_arrd,float * coef_arrf,int nbr_coefs,double transition)261 static void compute_coefs(double *coef_arrd, float *coef_arrf, int nbr_coefs, double transition)
262 {
263 const int order = nbr_coefs * 2 + 1;
264 double k, q;
265
266 compute_transition_param(&k, &q, transition);
267
268 for (int n = 0; n < nbr_coefs; n++) {
269 const int idx = (n / 2) + (n & 1) * nbr_coefs / 2;
270
271 coef_arrd[idx] = compute_coef(n, k, q, order);
272 coef_arrf[idx] = coef_arrd[idx];
273 }
274 }
275
config_input(AVFilterLink * inlink)276 static int config_input(AVFilterLink *inlink)
277 {
278 AVFilterContext *ctx = inlink->dst;
279 AFreqShift *s = ctx->priv;
280
281 compute_coefs(s->cd, s->cf, NB_COEFS, 2. * 20. / inlink->sample_rate);
282
283 s->i1 = ff_get_audio_buffer(inlink, NB_COEFS);
284 s->o1 = ff_get_audio_buffer(inlink, NB_COEFS);
285 s->i2 = ff_get_audio_buffer(inlink, NB_COEFS);
286 s->o2 = ff_get_audio_buffer(inlink, NB_COEFS);
287 if (!s->i1 || !s->o1 || !s->i2 || !s->o2)
288 return AVERROR(ENOMEM);
289
290 if (inlink->format == AV_SAMPLE_FMT_DBLP) {
291 if (!strcmp(ctx->filter->name, "afreqshift"))
292 s->filter_channel = ffilter_channel_dbl;
293 else
294 s->filter_channel = pfilter_channel_dbl;
295 } else {
296 if (!strcmp(ctx->filter->name, "afreqshift"))
297 s->filter_channel = ffilter_channel_flt;
298 else
299 s->filter_channel = pfilter_channel_flt;
300 }
301
302 return 0;
303 }
304
305 typedef struct ThreadData {
306 AVFrame *in, *out;
307 } ThreadData;
308
filter_channels(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)309 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
310 {
311 AFreqShift *s = ctx->priv;
312 ThreadData *td = arg;
313 AVFrame *out = td->out;
314 AVFrame *in = td->in;
315 const int start = (in->channels * jobnr) / nb_jobs;
316 const int end = (in->channels * (jobnr+1)) / nb_jobs;
317
318 for (int ch = start; ch < end; ch++)
319 s->filter_channel(ctx, ch, in, out);
320
321 return 0;
322 }
323
filter_frame(AVFilterLink * inlink,AVFrame * in)324 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
325 {
326 AVFilterContext *ctx = inlink->dst;
327 AVFilterLink *outlink = ctx->outputs[0];
328 AFreqShift *s = ctx->priv;
329 AVFrame *out;
330 ThreadData td;
331
332 if (av_frame_is_writable(in)) {
333 out = in;
334 } else {
335 out = ff_get_audio_buffer(outlink, in->nb_samples);
336 if (!out) {
337 av_frame_free(&in);
338 return AVERROR(ENOMEM);
339 }
340 av_frame_copy_props(out, in);
341 }
342
343 td.in = in; td.out = out;
344 ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(inlink->channels,
345 ff_filter_get_nb_threads(ctx)));
346
347 s->in_samples += in->nb_samples;
348
349 if (out != in)
350 av_frame_free(&in);
351 return ff_filter_frame(outlink, out);
352 }
353
uninit(AVFilterContext * ctx)354 static av_cold void uninit(AVFilterContext *ctx)
355 {
356 AFreqShift *s = ctx->priv;
357
358 av_frame_free(&s->i1);
359 av_frame_free(&s->o1);
360 av_frame_free(&s->i2);
361 av_frame_free(&s->o2);
362 }
363
364 #define OFFSET(x) offsetof(AFreqShift, x)
365 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
366
367 static const AVOption afreqshift_options[] = {
368 { "shift", "set frequency shift", OFFSET(shift), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -INT_MAX, INT_MAX, FLAGS },
369 { "level", "set output level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, 1.0, FLAGS },
370 { NULL }
371 };
372
373 AVFILTER_DEFINE_CLASS(afreqshift);
374
375 static const AVFilterPad inputs[] = {
376 {
377 .name = "default",
378 .type = AVMEDIA_TYPE_AUDIO,
379 .filter_frame = filter_frame,
380 .config_props = config_input,
381 },
382 { NULL }
383 };
384
385 static const AVFilterPad outputs[] = {
386 {
387 .name = "default",
388 .type = AVMEDIA_TYPE_AUDIO,
389 },
390 { NULL }
391 };
392
393 AVFilter ff_af_afreqshift = {
394 .name = "afreqshift",
395 .description = NULL_IF_CONFIG_SMALL("Apply frequency shifting to input audio."),
396 .query_formats = query_formats,
397 .priv_size = sizeof(AFreqShift),
398 .priv_class = &afreqshift_class,
399 .uninit = uninit,
400 .inputs = inputs,
401 .outputs = outputs,
402 .process_command = ff_filter_process_command,
403 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
404 AVFILTER_FLAG_SLICE_THREADS,
405 };
406
407 static const AVOption aphaseshift_options[] = {
408 { "shift", "set phase shift", OFFSET(shift), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1.0, 1.0, FLAGS },
409 { "level", "set output level",OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, 1.0, FLAGS },
410 { NULL }
411 };
412
413 AVFILTER_DEFINE_CLASS(aphaseshift);
414
415 AVFilter ff_af_aphaseshift = {
416 .name = "aphaseshift",
417 .description = NULL_IF_CONFIG_SMALL("Apply phase shifting to input audio."),
418 .query_formats = query_formats,
419 .priv_size = sizeof(AFreqShift),
420 .priv_class = &aphaseshift_class,
421 .uninit = uninit,
422 .inputs = inputs,
423 .outputs = outputs,
424 .process_command = ff_filter_process_command,
425 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
426 AVFILTER_FLAG_SLICE_THREADS,
427 };
428