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