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 {
43 void (*filter_channel)(AVFilterContext *ctx,
46 const double *src, double *dst,
47 double *i1, double *o1,
48 double *i2, double *o2);
51 static int query_formats(AVFilterContext *ctx)
53 AVFilterFormats *formats = NULL;
54 AVFilterChannelLayouts *layouts = NULL;
55 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 static void pfilter_channel(AVFilterContext *ctx,
83 const double *src, double *dst,
84 double *i1, double *o1,
85 double *i2, double *o2)
87 AFreqShift *s = ctx->priv;
89 double shift = s->shift * M_PI;
90 double cos_theta = cos(shift);
91 double sin_theta = sin(shift);
93 for (int n = 0; n < nb_samples; n++) {
94 double xn1 = src[n], xn2 = src[n];
97 for (int j = 0; j < NB_COEFS / 2; j++) {
98 I = c[j] * (xn1 + o2[j]) - i2[j];
106 for (int j = NB_COEFS / 2; j < NB_COEFS; j++) {
107 Q = c[j] * (xn2 + o2[j]) - i2[j];
114 Q = o2[NB_COEFS - 1];
116 dst[n] = I * cos_theta - Q * sin_theta;
120 static void ffilter_channel(AVFilterContext *ctx,
123 const double *src, double *dst,
124 double *i1, double *o1,
125 double *i2, double *o2)
127 AFreqShift *s = ctx->priv;
129 double ts = 1. / sample_rate;
130 double shift = s->shift;
131 int64_t N = s->in_samples;
133 for (int n = 0; n < nb_samples; n++) {
134 double xn1 = src[n], xn2 = src[n];
137 for (int j = 0; j < NB_COEFS / 2; j++) {
138 I = c[j] * (xn1 + o2[j]) - i2[j];
146 for (int j = NB_COEFS / 2; j < NB_COEFS; j++) {
147 Q = c[j] * (xn2 + o2[j]) - i2[j];
154 Q = o2[NB_COEFS - 1];
156 theta = 2. * M_PI * fmod(shift * (N + n) * ts, 1.);
157 dst[n] = I * cos(theta) - Q * sin(theta);
161 static void compute_transition_param(double *K, double *Q, double transition)
163 double kksqrt, e, e2, e4, k, q;
165 k = tan((1. - transition * 2.) * M_PI / 4.);
167 kksqrt = pow(1 - k * k, 0.25);
168 e = 0.5 * (1. - kksqrt) / (1. + kksqrt);
171 q = e * (1. + e4 * (2. + e4 * (15. + 150. * e4)));
177 static double ipowp(double x, int64_t n)
191 static double compute_acc_num(double q, int order, int c)
199 q_ii1 = ipowp(q, i * (i + 1));
200 q_ii1 *= sin((i * 2 + 1) * c * M_PI / order) * j;
205 } while (fabs(q_ii1) > 1e-100);
210 static double compute_acc_den(double q, int order, int c)
218 q_i2 = ipowp(q, i * i);
219 q_i2 *= cos(i * 2 * c * M_PI / order) * j;
224 } while (fabs(q_i2) > 1e-100);
229 static double compute_coef(int index, double k, double q, int order)
231 const int c = index + 1;
232 const double num = compute_acc_num(q, order, c) * pow(q, 0.25);
233 const double den = compute_acc_den(q, order, c) + 0.5;
234 const double ww = num / den;
235 const double wwsq = ww * ww;
237 const double x = sqrt((1 - wwsq * k) * (1 - wwsq / k)) / (1 + wwsq);
238 const double coef = (1 - x) / (1 + x);
243 static void compute_coefs(double *coef_arr, int nbr_coefs, double transition)
245 const int order = nbr_coefs * 2 + 1;
248 compute_transition_param(&k, &q, transition);
250 for (int n = 0; n < nbr_coefs; n++)
251 coef_arr[(n / 2) + (n & 1) * nbr_coefs / 2] = compute_coef(n, k, q, order);
254 static int config_input(AVFilterLink *inlink)
256 AVFilterContext *ctx = inlink->dst;
257 AFreqShift *s = ctx->priv;
259 compute_coefs(s->c, NB_COEFS, 2. * 20. / inlink->sample_rate);
261 s->i1 = ff_get_audio_buffer(inlink, NB_COEFS);
262 s->o1 = ff_get_audio_buffer(inlink, NB_COEFS);
263 s->i2 = ff_get_audio_buffer(inlink, NB_COEFS);
264 s->o2 = ff_get_audio_buffer(inlink, NB_COEFS);
265 if (!s->i1 || !s->o1 || !s->i2 || !s->o2)
266 return AVERROR(ENOMEM);
268 if (!strcmp(ctx->filter->name, "afreqshift"))
269 s->filter_channel = ffilter_channel;
271 s->filter_channel = pfilter_channel;
276 typedef struct ThreadData {
280 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
282 AFreqShift *s = ctx->priv;
283 ThreadData *td = arg;
284 AVFrame *out = td->out;
285 AVFrame *in = td->in;
286 const int start = (in->channels * jobnr) / nb_jobs;
287 const int end = (in->channels * (jobnr+1)) / nb_jobs;
289 for (int ch = start; ch < end; ch++) {
290 s->filter_channel(ctx, in->nb_samples,
292 (const double *)in->extended_data[ch],
293 (double *)out->extended_data[ch],
294 (double *)s->i1->extended_data[ch],
295 (double *)s->o1->extended_data[ch],
296 (double *)s->i2->extended_data[ch],
297 (double *)s->o2->extended_data[ch]);
303 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
305 AVFilterContext *ctx = inlink->dst;
306 AVFilterLink *outlink = ctx->outputs[0];
307 AFreqShift *s = ctx->priv;
311 if (av_frame_is_writable(in)) {
314 out = ff_get_audio_buffer(outlink, in->nb_samples);
317 return AVERROR(ENOMEM);
319 av_frame_copy_props(out, in);
322 td.in = in; td.out = out;
323 ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(inlink->channels,
324 ff_filter_get_nb_threads(ctx)));
326 s->in_samples += in->nb_samples;
330 return ff_filter_frame(outlink, out);
333 static av_cold void uninit(AVFilterContext *ctx)
335 AFreqShift *s = ctx->priv;
337 av_frame_free(&s->i1);
338 av_frame_free(&s->o1);
339 av_frame_free(&s->i2);
340 av_frame_free(&s->o2);
343 #define OFFSET(x) offsetof(AFreqShift, x)
344 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
346 static const AVOption afreqshift_options[] = {
347 { "shift", "set frequency shift", OFFSET(shift), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -INT_MAX, INT_MAX, FLAGS },
351 AVFILTER_DEFINE_CLASS(afreqshift);
353 static const AVFilterPad inputs[] = {
356 .type = AVMEDIA_TYPE_AUDIO,
357 .filter_frame = filter_frame,
358 .config_props = config_input,
363 static const AVFilterPad outputs[] = {
366 .type = AVMEDIA_TYPE_AUDIO,
371 AVFilter ff_af_afreqshift = {
372 .name = "afreqshift",
373 .description = NULL_IF_CONFIG_SMALL("Apply frequency shifting to input audio."),
374 .query_formats = query_formats,
375 .priv_size = sizeof(AFreqShift),
376 .priv_class = &afreqshift_class,
380 .process_command = ff_filter_process_command,
381 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
382 AVFILTER_FLAG_SLICE_THREADS,
385 static const AVOption aphaseshift_options[] = {
386 { "shift", "set phase shift", OFFSET(shift), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1.0, 1.0, FLAGS },
390 AVFILTER_DEFINE_CLASS(aphaseshift);
392 AVFilter ff_af_aphaseshift = {
393 .name = "aphaseshift",
394 .description = NULL_IF_CONFIG_SMALL("Apply phase shifting to input audio."),
395 .query_formats = query_formats,
396 .priv_size = sizeof(AFreqShift),
397 .priv_class = &aphaseshift_class,
401 .process_command = ff_filter_process_command,
402 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
403 AVFILTER_FLAG_SLICE_THREADS,