| @@ -1,4 +1,7 @@ |
| 1 | 1 | /* |
| 2 | + * Copyright (c) 2005 Boðaç Topaktaþ |
| 3 | + * Copyright (c) 2020 Paul B Mahol |
| 4 | + * |
| 2 | 5 | * This file is part of FFmpeg. |
| 3 | 6 | * |
| 4 | 7 | * FFmpeg is free software; you can redistribute it and/or |
| @@ -33,6 +36,7 @@ typedef struct ASuperCutContext { · 28 unmodified lines |
| 33 | 36 | |
| 34 | 37 | double cutoff; |
| 35 | 38 | double level; |
| 39 | + double qfactor; |
| 36 | 40 | int order; |
| 37 | 41 | |
| 38 | 42 | int filter_count; |
| @@ -93,10 +97,11 @@ static int get_coeffs(AVFilterContext *ctx) · 54 unmodified lines |
| 93 | 97 | if (s->bypass) |
| 94 | 98 | return 0; |
| 95 | 99 | |
| 96 | | − s->filter_count = s->order / 2 + (s->order & 1); |
| 97 | | − calc_q_factors(s->order, q); |
| 98 | | − |
| 99 | 100 | if (!strcmp(ctx->filter->name, "asubcut")) { |
| 101 | + s->filter_count = s->order / 2 + (s->order & 1); |
| 102 | + |
| 103 | + calc_q_factors(s->order, q); |
| 104 | + |
| 100 | 105 | if (s->order & 1) { |
| 101 | 106 | BiquadCoeffs *coeffs = &s->coeffs[0]; |
| 102 | 107 | double omega = 2. * tan(M_PI * w0); |
| @@ -119,7 +124,11 @@ static int get_coeffs(AVFilterContext *ctx) · 16 unmodified lines |
| 119 | 124 | coeffs->a1 = -2.0 * (K * K - 1.0) * norm; |
| 120 | 125 | coeffs->a2 = -(1.0 - K / q[idx] + K * K) * norm; |
| 121 | 126 | } |
| 122 | | − } else { |
| 127 | + } else if (!strcmp(ctx->filter->name, "asupercut")) { |
| 128 | + s->filter_count = s->order / 2 + (s->order & 1); |
| 129 | + |
| 130 | + calc_q_factors(s->order, q); |
| 131 | + |
| 123 | 132 | if (s->order & 1) { |
| 124 | 133 | BiquadCoeffs *coeffs = &s->coeffs[0]; |
| 125 | 134 | double omega = 2. * tan(M_PI * w0); |
| @@ -142,6 +151,74 @@ static int get_coeffs(AVFilterContext *ctx) · 16 unmodified lines |
| 142 | 151 | coeffs->a1 = -2.0 * (K * K - 1.0) * norm; |
| 143 | 152 | coeffs->a2 = -(1.0 - K / q[idx] + K * K) * norm; |
| 144 | 153 | } |
| 154 | + } else if (!strcmp(ctx->filter->name, "asuperpass")) { |
| 155 | + double alpha, beta, gamma, theta; |
| 156 | + double theta_0 = 2. * M_PI * (s->cutoff / inlink->sample_rate); |
| 157 | + double d_E; |
| 158 | + |
| 159 | + s->filter_count = s->order / 2; |
| 160 | + d_E = (2. * tan(theta_0 / (2. * s->qfactor))) / sin(theta_0); |
| 161 | + |
| 162 | + for (int b = 0; b < s->filter_count; b += 2) { |
| 163 | + double D = 2. * sin(((b + 1) * M_PI) / (2. * s->filter_count)); |
| 164 | + double A = (1. + pow((d_E / 2.), 2)) / (D * d_E / 2.); |
| 165 | + double d = sqrt((d_E * D) / (A + sqrt(A * A - 1.))); |
| 166 | + double B = D * (d_E / 2.) / d; |
| 167 | + double W = B + sqrt(B * B - 1.); |
| 168 | + |
| 169 | + for (int j = 0; j < 2; j++) { |
| 170 | + BiquadCoeffs *coeffs = &s->coeffs[b + j]; |
| 171 | + |
| 172 | + if (j == 1) |
| 173 | + theta = 2. * atan(tan(theta_0 / 2.) / W); |
| 174 | + else |
| 175 | + theta = 2. * atan(W * tan(theta_0 / 2.)); |
| 176 | + |
| 177 | + beta = 0.5 * ((1. - (d / 2.) * sin(theta)) / (1. + (d / 2.) * sin(theta))); |
| 178 | + gamma = (0.5 + beta) * cos(theta); |
| 179 | + alpha = 0.5 * (0.5 - beta) * sqrt(1. + pow((W - (1. / W)) / d, 2.)); |
| 180 | + |
| 181 | + coeffs->a1 = 2. * gamma; |
| 182 | + coeffs->a2 = -2. * beta; |
| 183 | + coeffs->b0 = 2. * alpha; |
| 184 | + coeffs->b1 = 0.; |
| 185 | + coeffs->b2 = -2. * alpha; |
| 186 | + } |
| 187 | + } |
| 188 | + } else if (!strcmp(ctx->filter->name, "asuperstop")) { |
| 189 | + double alpha, beta, gamma, theta; |
| 190 | + double theta_0 = 2. * M_PI * (s->cutoff / inlink->sample_rate); |
| 191 | + double d_E; |
| 192 | + |
| 193 | + s->filter_count = s->order / 2; |
| 194 | + d_E = (2. * tan(theta_0 / (2. * s->qfactor))) / sin(theta_0); |
| 195 | + |
| 196 | + for (int b = 0; b < s->filter_count; b += 2) { |
| 197 | + double D = 2. * sin(((b + 1) * M_PI) / (2. * s->filter_count)); |
| 198 | + double A = (1. + pow((d_E / 2.), 2)) / (D * d_E / 2.); |
| 199 | + double d = sqrt((d_E * D) / (A + sqrt(A * A - 1.))); |
| 200 | + double B = D * (d_E / 2.) / d; |
| 201 | + double W = B + sqrt(B * B - 1.); |
| 202 | + |
| 203 | + for (int j = 0; j < 2; j++) { |
| 204 | + BiquadCoeffs *coeffs = &s->coeffs[b + j]; |
| 205 | + |
| 206 | + if (j == 1) |
| 207 | + theta = 2. * atan(tan(theta_0 / 2.) / W); |
| 208 | + else |
| 209 | + theta = 2. * atan(W * tan(theta_0 / 2.)); |
| 210 | + |
| 211 | + beta = 0.5 * ((1. - (d / 2.) * sin(theta)) / (1. + (d / 2.) * sin(theta))); |
| 212 | + gamma = (0.5 + beta) * cos(theta); |
| 213 | + alpha = 0.5 * (0.5 + beta) * ((1. - cos(theta)) / (1. - cos(theta_0))); |
| 214 | + |
| 215 | + coeffs->a1 = 2. * gamma; |
| 216 | + coeffs->a2 = -2. * beta; |
| 217 | + coeffs->b0 = 2. * alpha; |
| 218 | + coeffs->b1 = -4. * alpha * cos(theta_0); |
| 219 | + coeffs->b2 = 2. * alpha; |
| 220 | + } |
| 221 | + } |
| 145 | 222 | } |
| 146 | 223 | |
| 147 | 224 | return 0; |
| @@ -327,3 +404,51 @@ AVFilter ff_af_asubcut = { · 179 unmodified lines |
| 327 | 404 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | |
| 328 | 405 | AVFILTER_FLAG_SLICE_THREADS, |
| 329 | 406 | }; |
| 407 | + |
| 408 | +static const AVOption asuperpass_options[] = { |
| 409 | + { "centerf","set center frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 999999, FLAGS }, |
| 410 | + { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=4}, 4, 20, FLAGS }, |
| 411 | + { "qfactor","set Q-factor", OFFSET(qfactor),AV_OPT_TYPE_DOUBLE, {.dbl=1.},0.01, 100., FLAGS }, |
| 412 | + { "level", "set input level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0., 2., FLAGS }, |
| 413 | + { NULL } |
| 414 | +}; |
| 415 | + |
| 416 | +AVFILTER_DEFINE_CLASS(asuperpass); |
| 417 | + |
| 418 | +AVFilter ff_af_asuperpass = { |
| 419 | + .name = "asuperpass", |
| 420 | + .description = NULL_IF_CONFIG_SMALL("Apply high order Butterworth band-pass filter."), |
| 421 | + .query_formats = query_formats, |
| 422 | + .priv_size = sizeof(ASuperCutContext), |
| 423 | + .priv_class = &asuperpass_class, |
| 424 | + .uninit = uninit, |
| 425 | + .inputs = inputs, |
| 426 | + .outputs = outputs, |
| 427 | + .process_command = process_command, |
| 428 | + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | |
| 429 | + AVFILTER_FLAG_SLICE_THREADS, |
| 430 | +}; |
| 431 | + |
| 432 | +static const AVOption asuperstop_options[] = { |
| 433 | + { "centerf","set center frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 999999, FLAGS }, |
| 434 | + { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=4}, 4, 20, FLAGS }, |
| 435 | + { "qfactor","set Q-factor", OFFSET(qfactor),AV_OPT_TYPE_DOUBLE, {.dbl=1.},0.01, 100., FLAGS }, |
| 436 | + { "level", "set input level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0., 2., FLAGS }, |
| 437 | + { NULL } |
| 438 | +}; |
| 439 | + |
| 440 | +AVFILTER_DEFINE_CLASS(asuperstop); |
| 441 | + |
| 442 | +AVFilter ff_af_asuperstop = { |
| 443 | + .name = "asuperstop", |
| 444 | + .description = NULL_IF_CONFIG_SMALL("Apply high order Butterworth band-stop filter."), |
| 445 | + .query_formats = query_formats, |
| 446 | + .priv_size = sizeof(ASuperCutContext), |
| 447 | + .priv_class = &asuperstop_class, |
| 448 | + .uninit = uninit, |
| 449 | + .inputs = inputs, |
| 450 | + .outputs = outputs, |
| 451 | + .process_command = process_command, |
| 452 | + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | |
| 453 | + AVFILTER_FLAG_SLICE_THREADS, |
| 454 | +}; |