2 * Copyright (c) Paul B Mahol
3 * Copyright (c) Laurent de Soras, 2005
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/ffmath.h"
24 #include "libavutil/opt.h"
31 typedef struct AFreqShift {
45 void (*filter_channel)(AVFilterContext *ctx,
47 AVFrame *in, AVFrame *out);
50 static int query_formats(AVFilterContext *ctx)
52 AVFilterFormats *formats = NULL;
53 AVFilterChannelLayouts *layouts = NULL;
54 static const enum AVSampleFormat sample_fmts[] = {
61 formats = ff_make_format_list(sample_fmts);
63 return AVERROR(ENOMEM);
64 ret = ff_set_common_formats(ctx, formats);
68 layouts = ff_all_channel_counts();
70 return AVERROR(ENOMEM);
72 ret = ff_set_common_channel_layouts(ctx, layouts);
76 formats = ff_all_samplerates();
77 return ff_set_common_samplerates(ctx, formats);
80 #define PFILTER(name, type, sin, cos, cc) \
81 static void pfilter_channel_## name(AVFilterContext *ctx, \
83 AVFrame *in, AVFrame *out) \
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); \
99 for (int n = 0; n < nb_samples; n++) { \
100 type xn1 = src[n], xn2 = src[n]; \
103 for (int j = 0; j < NB_COEFS / 2; j++) { \
104 I = c[j] * (xn1 + o2[j]) - i2[j]; \
112 for (int j = NB_COEFS / 2; j < NB_COEFS; j++) { \
113 Q = c[j] * (xn2 + o2[j]) - i2[j]; \
120 Q = o2[NB_COEFS - 1]; \
122 dst[n] = (I * cos_theta - Q * sin_theta) * level; \
126 PFILTER(flt, float, sin, cos, cf)
127 PFILTER(dbl, double, sin, cos, cd)
129 #define FFILTER(name, type, sin, cos, fmod, cc) \
130 static void ffilter_channel_## name(AVFilterContext *ctx, \
132 AVFrame *in, AVFrame *out) \
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; \
148 for (int n = 0; n < nb_samples; n++) { \
149 type xn1 = src[n], xn2 = src[n]; \
152 for (int j = 0; j < NB_COEFS / 2; j++) { \
153 I = c[j] * (xn1 + o2[j]) - i2[j]; \
161 for (int j = NB_COEFS / 2; j < NB_COEFS; j++) { \
162 Q = c[j] * (xn2 + o2[j]) - i2[j]; \
169 Q = o2[NB_COEFS - 1]; \
171 theta = 2. * M_PI * fmod(shift * (N + n) * ts, 1.); \
172 dst[n] = (I * cos(theta) - Q * sin(theta)) * level; \
176 FFILTER(flt, float, sinf, cosf, fmodf, cf)
177 FFILTER(dbl, double, sin, cos, fmod, cd)
179 static void compute_transition_param(double *K, double *Q, double transition)
181 double kksqrt, e, e2, e4, k, q;
183 k = tan((1. - transition * 2.) * M_PI / 4.);
185 kksqrt = pow(1 - k * k, 0.25);
186 e = 0.5 * (1. - kksqrt) / (1. + kksqrt);
189 q = e * (1. + e4 * (2. + e4 * (15. + 150. * e4)));
195 static double ipowp(double x, int64_t n)
209 static double compute_acc_num(double q, int order, int c)
217 q_ii1 = ipowp(q, i * (i + 1));
218 q_ii1 *= sin((i * 2 + 1) * c * M_PI / order) * j;
223 } while (fabs(q_ii1) > 1e-100);
228 static double compute_acc_den(double q, int order, int c)
236 q_i2 = ipowp(q, i * i);
237 q_i2 *= cos(i * 2 * c * M_PI / order) * j;
242 } while (fabs(q_i2) > 1e-100);
247 static double compute_coef(int index, double k, double q, int order)
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;
255 const double x = sqrt((1 - wwsq * k) * (1 - wwsq / k)) / (1 + wwsq);
256 const double coef = (1 - x) / (1 + x);
261 static void compute_coefs(double *coef_arrd, float *coef_arrf, int nbr_coefs, double transition)
263 const int order = nbr_coefs * 2 + 1;
266 compute_transition_param(&k, &q, transition);
268 for (int n = 0; n < nbr_coefs; n++) {
269 const int idx = (n / 2) + (n & 1) * nbr_coefs / 2;
271 coef_arrd[idx] = compute_coef(n, k, q, order);
272 coef_arrf[idx] = coef_arrd[idx];
276 static int config_input(AVFilterLink *inlink)
278 AVFilterContext *ctx = inlink->dst;
279 AFreqShift *s = ctx->priv;
281 compute_coefs(s->cd, s->cf, NB_COEFS, 2. * 20. / inlink->sample_rate);
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);
290 if (inlink->format == AV_SAMPLE_FMT_DBLP) {
291 if (!strcmp(ctx->filter->name, "afreqshift"))
292 s->filter_channel = ffilter_channel_dbl;
294 s->filter_channel = pfilter_channel_dbl;
296 if (!strcmp(ctx->filter->name, "afreqshift"))
297 s->filter_channel = ffilter_channel_flt;
299 s->filter_channel = pfilter_channel_flt;
305 typedef struct ThreadData {
309 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
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;
318 for (int ch = start; ch < end; ch++)
319 s->filter_channel(ctx, ch, in, out);
324 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
326 AVFilterContext *ctx = inlink->dst;
327 AVFilterLink *outlink = ctx->outputs[0];
328 AFreqShift *s = ctx->priv;
332 if (av_frame_is_writable(in)) {
335 out = ff_get_audio_buffer(outlink, in->nb_samples);
338 return AVERROR(ENOMEM);
340 av_frame_copy_props(out, in);
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)));
347 s->in_samples += in->nb_samples;
351 return ff_filter_frame(outlink, out);
354 static av_cold void uninit(AVFilterContext *ctx)
356 AFreqShift *s = ctx->priv;
358 av_frame_free(&s->i1);
359 av_frame_free(&s->o1);
360 av_frame_free(&s->i2);
361 av_frame_free(&s->o2);
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
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 },
373 AVFILTER_DEFINE_CLASS(afreqshift);
375 static const AVFilterPad inputs[] = {
378 .type = AVMEDIA_TYPE_AUDIO,
379 .filter_frame = filter_frame,
380 .config_props = config_input,
385 static const AVFilterPad outputs[] = {
388 .type = AVMEDIA_TYPE_AUDIO,
393 const 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,
402 .process_command = ff_filter_process_command,
403 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
404 AVFILTER_FLAG_SLICE_THREADS,
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 },
413 AVFILTER_DEFINE_CLASS(aphaseshift);
415 const 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,
424 .process_command = ff_filter_process_command,
425 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
426 AVFILTER_FLAG_SLICE_THREADS,