| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others | ||
| 3 | * Copyright (c) 2015 Paul B Mahol | ||
| 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/intreadwrite.h" | ||
| 23 | #include "libavutil/avstring.h" | ||
| 24 | #include "libavutil/ffmath.h" | ||
| 25 | #include "libavutil/mem.h" | ||
| 26 | #include "libavutil/opt.h" | ||
| 27 | #include "libavutil/parseutils.h" | ||
| 28 | #include "avfilter.h" | ||
| 29 | #include "filters.h" | ||
| 30 | #include "formats.h" | ||
| 31 | #include "audio.h" | ||
| 32 | #include "video.h" | ||
| 33 | |||
| 34 | #define FILTER_ORDER 4 | ||
| 35 | |||
| 36 | enum FilterType { | ||
| 37 | BUTTERWORTH, | ||
| 38 | CHEBYSHEV1, | ||
| 39 | CHEBYSHEV2, | ||
| 40 | NB_TYPES | ||
| 41 | }; | ||
| 42 | |||
| 43 | typedef struct FoSection { | ||
| 44 | double a0, a1, a2, a3, a4; | ||
| 45 | double b0, b1, b2, b3, b4; | ||
| 46 | |||
| 47 | double num[4]; | ||
| 48 | double denum[4]; | ||
| 49 | } FoSection; | ||
| 50 | |||
| 51 | typedef struct EqualizatorFilter { | ||
| 52 | int ignore; | ||
| 53 | int channel; | ||
| 54 | int type; | ||
| 55 | |||
| 56 | double freq; | ||
| 57 | double gain; | ||
| 58 | double width; | ||
| 59 | |||
| 60 | FoSection section[2]; | ||
| 61 | } EqualizatorFilter; | ||
| 62 | |||
| 63 | typedef struct AudioNEqualizerContext { | ||
| 64 | const AVClass *class; | ||
| 65 | char *args; | ||
| 66 | char *colors; | ||
| 67 | int draw_curves; | ||
| 68 | int w, h; | ||
| 69 | |||
| 70 | double mag; | ||
| 71 | int fscale; | ||
| 72 | int nb_filters; | ||
| 73 | int nb_allocated; | ||
| 74 | EqualizatorFilter *filters; | ||
| 75 | AVFrame *video; | ||
| 76 | } AudioNEqualizerContext; | ||
| 77 | |||
| 78 | #define OFFSET(x) offsetof(AudioNEqualizerContext, x) | ||
| 79 | #define A AV_OPT_FLAG_AUDIO_PARAM | ||
| 80 | #define V AV_OPT_FLAG_VIDEO_PARAM | ||
| 81 | #define F AV_OPT_FLAG_FILTERING_PARAM | ||
| 82 | |||
| 83 | static const AVOption anequalizer_options[] = { | ||
| 84 | { "params", NULL, OFFSET(args), AV_OPT_TYPE_STRING, {.str=""}, 0, 0, A|F }, | ||
| 85 | { "curves", "draw frequency response curves", OFFSET(draw_curves), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, V|F }, | ||
| 86 | { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, V|F }, | ||
| 87 | { "mgain", "set max gain", OFFSET(mag), AV_OPT_TYPE_DOUBLE, {.dbl=60}, -900, 900, V|F }, | ||
| 88 | { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, V|F, .unit = "fscale" }, | ||
| 89 | { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, V|F, .unit = "fscale" }, | ||
| 90 | { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, V|F, .unit = "fscale" }, | ||
| 91 | { "colors", "set channels curves colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, V|F }, | ||
| 92 | { NULL } | ||
| 93 | }; | ||
| 94 | |||
| 95 | AVFILTER_DEFINE_CLASS(anequalizer); | ||
| 96 | |||
| 97 | ✗ | static void draw_curves(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *out) | |
| 98 | { | ||
| 99 | ✗ | AudioNEqualizerContext *s = ctx->priv; | |
| 100 | ✗ | char *colors, *color, *saveptr = NULL; | |
| 101 | int ch, i, n; | ||
| 102 | |||
| 103 | ✗ | colors = av_strdup(s->colors); | |
| 104 | ✗ | if (!colors) | |
| 105 | ✗ | return; | |
| 106 | |||
| 107 | ✗ | memset(out->data[0], 0, s->h * out->linesize[0]); | |
| 108 | |||
| 109 | ✗ | for (ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { | |
| 110 | ✗ | uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff }; | |
| 111 | ✗ | int prev_v = -1; | |
| 112 | double f; | ||
| 113 | |||
| 114 | ✗ | color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr); | |
| 115 | ✗ | if (color) | |
| 116 | ✗ | av_parse_color(fg, color, -1, ctx); | |
| 117 | |||
| 118 | ✗ | for (f = 0; f < s->w; f++) { | |
| 119 | double zr, zi, zr2, zi2; | ||
| 120 | double Hr, Hi; | ||
| 121 | ✗ | double Hmag = 1; | |
| 122 | double w; | ||
| 123 | int v, y, x; | ||
| 124 | |||
| 125 | ✗ | w = M_PI * (s->fscale ? pow(s->w - 1, f / s->w) : f) / (s->w - 1); | |
| 126 | ✗ | zr = cos(w); | |
| 127 | ✗ | zr2 = zr * zr; | |
| 128 | ✗ | zi = -sin(w); | |
| 129 | ✗ | zi2 = zi * zi; | |
| 130 | |||
| 131 | ✗ | for (n = 0; n < s->nb_filters; n++) { | |
| 132 | ✗ | if (s->filters[n].channel != ch || | |
| 133 | ✗ | s->filters[n].ignore) | |
| 134 | ✗ | continue; | |
| 135 | |||
| 136 | ✗ | for (i = 0; i < FILTER_ORDER / 2; i++) { | |
| 137 | ✗ | FoSection *S = &s->filters[n].section[i]; | |
| 138 | |||
| 139 | /* H *= (((((S->b4 * z + S->b3) * z + S->b2) * z + S->b1) * z + S->b0) / | ||
| 140 | ((((S->a4 * z + S->a3) * z + S->a2) * z + S->a1) * z + S->a0)); */ | ||
| 141 | |||
| 142 | ✗ | Hr = S->b4*(1-8*zr2*zi2) + S->b2*(zr2-zi2) + zr*(S->b1+S->b3*(zr2-3*zi2))+ S->b0; | |
| 143 | ✗ | Hi = zi*(S->b3*(3*zr2-zi2) + S->b1 + 2*zr*(2*S->b4*(zr2-zi2) + S->b2)); | |
| 144 | ✗ | Hmag *= hypot(Hr, Hi); | |
| 145 | ✗ | Hr = S->a4*(1-8*zr2*zi2) + S->a2*(zr2-zi2) + zr*(S->a1+S->a3*(zr2-3*zi2))+ S->a0; | |
| 146 | ✗ | Hi = zi*(S->a3*(3*zr2-zi2) + S->a1 + 2*zr*(2*S->a4*(zr2-zi2) + S->a2)); | |
| 147 | ✗ | Hmag /= hypot(Hr, Hi); | |
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | ✗ | v = av_clip((1. + -20 * log10(Hmag) / s->mag) * s->h / 2, 0, s->h - 1); | |
| 152 | ✗ | x = lrint(f); | |
| 153 | ✗ | if (prev_v == -1) | |
| 154 | ✗ | prev_v = v; | |
| 155 | ✗ | if (v <= prev_v) { | |
| 156 | ✗ | for (y = v; y <= prev_v; y++) | |
| 157 | ✗ | AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg)); | |
| 158 | } else { | ||
| 159 | ✗ | for (y = prev_v; y <= v; y++) | |
| 160 | ✗ | AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg)); | |
| 161 | } | ||
| 162 | |||
| 163 | ✗ | prev_v = v; | |
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | ✗ | av_free(colors); | |
| 168 | } | ||
| 169 | |||
| 170 | ✗ | static int config_video(AVFilterLink *outlink) | |
| 171 | { | ||
| 172 | ✗ | AVFilterContext *ctx = outlink->src; | |
| 173 | ✗ | AudioNEqualizerContext *s = ctx->priv; | |
| 174 | ✗ | AVFilterLink *inlink = ctx->inputs[0]; | |
| 175 | AVFrame *out; | ||
| 176 | |||
| 177 | ✗ | outlink->w = s->w; | |
| 178 | ✗ | outlink->h = s->h; | |
| 179 | |||
| 180 | ✗ | av_frame_free(&s->video); | |
| 181 | ✗ | s->video = out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
| 182 | ✗ | if (!out) | |
| 183 | ✗ | return AVERROR(ENOMEM); | |
| 184 | ✗ | outlink->sample_aspect_ratio = (AVRational){1,1}; | |
| 185 | |||
| 186 | ✗ | draw_curves(ctx, inlink, out); | |
| 187 | |||
| 188 | ✗ | return 0; | |
| 189 | } | ||
| 190 | |||
| 191 | 2 | static av_cold int init(AVFilterContext *ctx) | |
| 192 | { | ||
| 193 | 2 | AudioNEqualizerContext *s = ctx->priv; | |
| 194 | AVFilterPad pad, vpad; | ||
| 195 | int ret; | ||
| 196 | |||
| 197 | 2 | pad = (AVFilterPad){ | |
| 198 | .name = "out0", | ||
| 199 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 200 | }; | ||
| 201 | |||
| 202 | 2 | ret = ff_append_outpad(ctx, &pad); | |
| 203 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
| 204 | ✗ | return ret; | |
| 205 | |||
| 206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (s->draw_curves) { |
| 207 | ✗ | vpad = (AVFilterPad){ | |
| 208 | .name = "out1", | ||
| 209 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 210 | .config_props = config_video, | ||
| 211 | }; | ||
| 212 | ✗ | ret = ff_append_outpad(ctx, &vpad); | |
| 213 | ✗ | if (ret < 0) | |
| 214 | ✗ | return ret; | |
| 215 | } | ||
| 216 | |||
| 217 | 2 | return 0; | |
| 218 | } | ||
| 219 | |||
| 220 | 1 | static int query_formats(const AVFilterContext *ctx, | |
| 221 | AVFilterFormatsConfig **cfg_in, | ||
| 222 | AVFilterFormatsConfig **cfg_out) | ||
| 223 | { | ||
| 224 | 1 | const AudioNEqualizerContext *s = ctx->priv; | |
| 225 | static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE }; | ||
| 226 | static const enum AVSampleFormat sample_fmts[] = { | ||
| 227 | AV_SAMPLE_FMT_DBLP, | ||
| 228 | AV_SAMPLE_FMT_NONE | ||
| 229 | }; | ||
| 230 | int ret; | ||
| 231 | |||
| 232 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (s->draw_curves) { |
| 233 | ✗ | ret = ff_set_pixel_formats_from_list2(ctx, cfg_in, cfg_out, pix_fmts); | |
| 234 | ✗ | if (ret < 0) | |
| 235 | ✗ | return ret; | |
| 236 | } | ||
| 237 | |||
| 238 | 1 | ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts); | |
| 239 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
| 240 | ✗ | return ret; | |
| 241 | |||
| 242 | 1 | return 0; | |
| 243 | } | ||
| 244 | |||
| 245 | 2 | static av_cold void uninit(AVFilterContext *ctx) | |
| 246 | { | ||
| 247 | 2 | AudioNEqualizerContext *s = ctx->priv; | |
| 248 | |||
| 249 | 2 | av_frame_free(&s->video); | |
| 250 | 2 | av_freep(&s->filters); | |
| 251 | 2 | s->nb_filters = 0; | |
| 252 | 2 | s->nb_allocated = 0; | |
| 253 | 2 | } | |
| 254 | |||
| 255 | ✗ | static void butterworth_fo_section(FoSection *S, double beta, | |
| 256 | double si, double g, double g0, | ||
| 257 | double D, double c0) | ||
| 258 | { | ||
| 259 | ✗ | if (c0 == 1 || c0 == -1) { | |
| 260 | ✗ | S->b0 = (g*g*beta*beta + 2*g*g0*si*beta + g0*g0)/D; | |
| 261 | ✗ | S->b1 = 2*c0*(g*g*beta*beta - g0*g0)/D; | |
| 262 | ✗ | S->b2 = (g*g*beta*beta - 2*g0*g*beta*si + g0*g0)/D; | |
| 263 | ✗ | S->b3 = 0; | |
| 264 | ✗ | S->b4 = 0; | |
| 265 | |||
| 266 | ✗ | S->a0 = 1; | |
| 267 | ✗ | S->a1 = 2*c0*(beta*beta - 1)/D; | |
| 268 | ✗ | S->a2 = (beta*beta - 2*beta*si + 1)/D; | |
| 269 | ✗ | S->a3 = 0; | |
| 270 | ✗ | S->a4 = 0; | |
| 271 | } else { | ||
| 272 | ✗ | S->b0 = (g*g*beta*beta + 2*g*g0*si*beta + g0*g0)/D; | |
| 273 | ✗ | S->b1 = -4*c0*(g0*g0 + g*g0*si*beta)/D; | |
| 274 | ✗ | S->b2 = 2*(g0*g0*(1 + 2*c0*c0) - g*g*beta*beta)/D; | |
| 275 | ✗ | S->b3 = -4*c0*(g0*g0 - g*g0*si*beta)/D; | |
| 276 | ✗ | S->b4 = (g*g*beta*beta - 2*g*g0*si*beta + g0*g0)/D; | |
| 277 | |||
| 278 | ✗ | S->a0 = 1; | |
| 279 | ✗ | S->a1 = -4*c0*(1 + si*beta)/D; | |
| 280 | ✗ | S->a2 = 2*(1 + 2*c0*c0 - beta*beta)/D; | |
| 281 | ✗ | S->a3 = -4*c0*(1 - si*beta)/D; | |
| 282 | ✗ | S->a4 = (beta*beta - 2*si*beta + 1)/D; | |
| 283 | } | ||
| 284 | ✗ | } | |
| 285 | |||
| 286 | ✗ | static void butterworth_bp_filter(EqualizatorFilter *f, | |
| 287 | int N, double w0, double wb, | ||
| 288 | double G, double Gb, double G0) | ||
| 289 | { | ||
| 290 | double g, c0, g0, beta; | ||
| 291 | double epsilon; | ||
| 292 | ✗ | int r = N % 2; | |
| 293 | ✗ | int L = (N - r) / 2; | |
| 294 | int i; | ||
| 295 | |||
| 296 | ✗ | if (G == 0 && G0 == 0) { | |
| 297 | ✗ | f->section[0].a0 = 1; | |
| 298 | ✗ | f->section[0].b0 = 1; | |
| 299 | ✗ | f->section[1].a0 = 1; | |
| 300 | ✗ | f->section[1].b0 = 1; | |
| 301 | ✗ | return; | |
| 302 | } | ||
| 303 | |||
| 304 | ✗ | G = ff_exp10(G/20); | |
| 305 | ✗ | Gb = ff_exp10(Gb/20); | |
| 306 | ✗ | G0 = ff_exp10(G0/20); | |
| 307 | |||
| 308 | ✗ | epsilon = sqrt((G * G - Gb * Gb) / (Gb * Gb - G0 * G0)); | |
| 309 | ✗ | g = pow(G, 1.0 / N); | |
| 310 | ✗ | g0 = pow(G0, 1.0 / N); | |
| 311 | ✗ | beta = pow(epsilon, -1.0 / N) * tan(wb/2); | |
| 312 | ✗ | c0 = cos(w0); | |
| 313 | |||
| 314 | ✗ | for (i = 1; i <= L; i++) { | |
| 315 | ✗ | double ui = (2.0 * i - 1) / N; | |
| 316 | ✗ | double si = sin(M_PI * ui / 2.0); | |
| 317 | ✗ | double Di = beta * beta + 2 * si * beta + 1; | |
| 318 | |||
| 319 | ✗ | butterworth_fo_section(&f->section[i - 1], beta, si, g, g0, Di, c0); | |
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | 2 | static void chebyshev1_fo_section(FoSection *S, double a, | |
| 324 | double c, double tetta_b, | ||
| 325 | double g0, double si, double b, | ||
| 326 | double D, double c0) | ||
| 327 | { | ||
| 328 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (c0 == 1 || c0 == -1) { |
| 329 | ✗ | S->b0 = (tetta_b*tetta_b*(b*b+g0*g0*c*c) + 2*g0*b*si*tetta_b*tetta_b + g0*g0)/D; | |
| 330 | ✗ | S->b1 = 2*c0*(tetta_b*tetta_b*(b*b+g0*g0*c*c) - g0*g0)/D; | |
| 331 | ✗ | S->b2 = (tetta_b*tetta_b*(b*b+g0*g0*c*c) - 2*g0*b*si*tetta_b + g0*g0)/D; | |
| 332 | ✗ | S->b3 = 0; | |
| 333 | ✗ | S->b4 = 0; | |
| 334 | |||
| 335 | ✗ | S->a0 = 1; | |
| 336 | ✗ | S->a1 = 2*c0*(tetta_b*tetta_b*(a*a+c*c) - 1)/D; | |
| 337 | ✗ | S->a2 = (tetta_b*tetta_b*(a*a+c*c) - 2*a*si*tetta_b + 1)/D; | |
| 338 | ✗ | S->a3 = 0; | |
| 339 | ✗ | S->a4 = 0; | |
| 340 | } else { | ||
| 341 | 2 | S->b0 = ((b*b + g0*g0*c*c)*tetta_b*tetta_b + 2*g0*b*si*tetta_b + g0*g0)/D; | |
| 342 | 2 | S->b1 = -4*c0*(g0*g0 + g0*b*si*tetta_b)/D; | |
| 343 | 2 | S->b2 = 2*(g0*g0*(1 + 2*c0*c0) - (b*b + g0*g0*c*c)*tetta_b*tetta_b)/D; | |
| 344 | 2 | S->b3 = -4*c0*(g0*g0 - g0*b*si*tetta_b)/D; | |
| 345 | 2 | S->b4 = ((b*b + g0*g0*c*c)*tetta_b*tetta_b - 2*g0*b*si*tetta_b + g0*g0)/D; | |
| 346 | |||
| 347 | 2 | S->a0 = 1; | |
| 348 | 2 | S->a1 = -4*c0*(1 + a*si*tetta_b)/D; | |
| 349 | 2 | S->a2 = 2*(1 + 2*c0*c0 - (a*a + c*c)*tetta_b*tetta_b)/D; | |
| 350 | 2 | S->a3 = -4*c0*(1 - a*si*tetta_b)/D; | |
| 351 | 2 | S->a4 = ((a*a + c*c)*tetta_b*tetta_b - 2*a*si*tetta_b + 1)/D; | |
| 352 | } | ||
| 353 | 2 | } | |
| 354 | |||
| 355 | 1 | static void chebyshev1_bp_filter(EqualizatorFilter *f, | |
| 356 | int N, double w0, double wb, | ||
| 357 | double G, double Gb, double G0) | ||
| 358 | { | ||
| 359 | double a, b, c0, g0, alfa, beta, tetta_b; | ||
| 360 | double epsilon; | ||
| 361 | 1 | int r = N % 2; | |
| 362 | 1 | int L = (N - r) / 2; | |
| 363 | int i; | ||
| 364 | |||
| 365 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if (G == 0 && G0 == 0) { |
| 366 | ✗ | f->section[0].a0 = 1; | |
| 367 | ✗ | f->section[0].b0 = 1; | |
| 368 | ✗ | f->section[1].a0 = 1; | |
| 369 | ✗ | f->section[1].b0 = 1; | |
| 370 | ✗ | return; | |
| 371 | } | ||
| 372 | |||
| 373 | 1 | G = ff_exp10(G/20); | |
| 374 | 1 | Gb = ff_exp10(Gb/20); | |
| 375 | 1 | G0 = ff_exp10(G0/20); | |
| 376 | |||
| 377 | 1 | epsilon = sqrt((G*G - Gb*Gb) / (Gb*Gb - G0*G0)); | |
| 378 | 1 | g0 = pow(G0,1.0/N); | |
| 379 | 1 | alfa = pow(1.0/epsilon + sqrt(1 + 1/(epsilon*epsilon)), 1.0/N); | |
| 380 | 1 | beta = pow(G/epsilon + Gb * sqrt(1 + 1/(epsilon*epsilon)), 1.0/N); | |
| 381 | 1 | a = 0.5 * (alfa - 1.0/alfa); | |
| 382 | 1 | b = 0.5 * (beta - g0*g0*(1/beta)); | |
| 383 | 1 | tetta_b = tan(wb/2); | |
| 384 | 1 | c0 = cos(w0); | |
| 385 | |||
| 386 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (i = 1; i <= L; i++) { |
| 387 | 2 | double ui = (2.0*i-1.0)/N; | |
| 388 | 2 | double ci = cos(M_PI*ui/2.0); | |
| 389 | 2 | double si = sin(M_PI*ui/2.0); | |
| 390 | 2 | double Di = (a*a + ci*ci)*tetta_b*tetta_b + 2.0*a*si*tetta_b + 1; | |
| 391 | |||
| 392 | 2 | chebyshev1_fo_section(&f->section[i - 1], a, ci, tetta_b, g0, si, b, Di, c0); | |
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | ✗ | static void chebyshev2_fo_section(FoSection *S, double a, | |
| 397 | double c, double tetta_b, | ||
| 398 | double g, double si, double b, | ||
| 399 | double D, double c0) | ||
| 400 | { | ||
| 401 | ✗ | if (c0 == 1 || c0 == -1) { | |
| 402 | ✗ | S->b0 = (g*g*tetta_b*tetta_b + 2*tetta_b*g*b*si + b*b + g*g*c*c)/D; | |
| 403 | ✗ | S->b1 = 2*c0*(g*g*tetta_b*tetta_b - b*b - g*g*c*c)/D; | |
| 404 | ✗ | S->b2 = (g*g*tetta_b*tetta_b - 2*tetta_b*g*b*si + b*b + g*g*c*c)/D; | |
| 405 | ✗ | S->b3 = 0; | |
| 406 | ✗ | S->b4 = 0; | |
| 407 | |||
| 408 | ✗ | S->a0 = 1; | |
| 409 | ✗ | S->a1 = 2*c0*(tetta_b*tetta_b - a*a - c*c)/D; | |
| 410 | ✗ | S->a2 = (tetta_b*tetta_b - 2*tetta_b*a*si + a*a + c*c)/D; | |
| 411 | ✗ | S->a3 = 0; | |
| 412 | ✗ | S->a4 = 0; | |
| 413 | } else { | ||
| 414 | ✗ | S->b0 = (g*g*tetta_b*tetta_b + 2*g*b*si*tetta_b + b*b + g*g*c*c)/D; | |
| 415 | ✗ | S->b1 = -4*c0*(b*b + g*g*c*c + g*b*si*tetta_b)/D; | |
| 416 | ✗ | S->b2 = 2*((b*b + g*g*c*c)*(1 + 2*c0*c0) - g*g*tetta_b*tetta_b)/D; | |
| 417 | ✗ | S->b3 = -4*c0*(b*b + g*g*c*c - g*b*si*tetta_b)/D; | |
| 418 | ✗ | S->b4 = (g*g*tetta_b*tetta_b - 2*g*b*si*tetta_b + b*b + g*g*c*c)/D; | |
| 419 | |||
| 420 | ✗ | S->a0 = 1; | |
| 421 | ✗ | S->a1 = -4*c0*(a*a + c*c + a*si*tetta_b)/D; | |
| 422 | ✗ | S->a2 = 2*((a*a + c*c)*(1 + 2*c0*c0) - tetta_b*tetta_b)/D; | |
| 423 | ✗ | S->a3 = -4*c0*(a*a + c*c - a*si*tetta_b)/D; | |
| 424 | ✗ | S->a4 = (tetta_b*tetta_b - 2*a*si*tetta_b + a*a + c*c)/D; | |
| 425 | } | ||
| 426 | ✗ | } | |
| 427 | |||
| 428 | ✗ | static void chebyshev2_bp_filter(EqualizatorFilter *f, | |
| 429 | int N, double w0, double wb, | ||
| 430 | double G, double Gb, double G0) | ||
| 431 | { | ||
| 432 | double a, b, c0, tetta_b; | ||
| 433 | double epsilon, g, eu, ew; | ||
| 434 | ✗ | int r = N % 2; | |
| 435 | ✗ | int L = (N - r) / 2; | |
| 436 | int i; | ||
| 437 | |||
| 438 | ✗ | if (G == 0 && G0 == 0) { | |
| 439 | ✗ | f->section[0].a0 = 1; | |
| 440 | ✗ | f->section[0].b0 = 1; | |
| 441 | ✗ | f->section[1].a0 = 1; | |
| 442 | ✗ | f->section[1].b0 = 1; | |
| 443 | ✗ | return; | |
| 444 | } | ||
| 445 | |||
| 446 | ✗ | G = ff_exp10(G/20); | |
| 447 | ✗ | Gb = ff_exp10(Gb/20); | |
| 448 | ✗ | G0 = ff_exp10(G0/20); | |
| 449 | |||
| 450 | ✗ | epsilon = sqrt((G*G - Gb*Gb) / (Gb*Gb - G0*G0)); | |
| 451 | ✗ | g = pow(G, 1.0 / N); | |
| 452 | ✗ | eu = pow(epsilon + sqrt(1 + epsilon*epsilon), 1.0/N); | |
| 453 | ✗ | ew = pow(G0*epsilon + Gb*sqrt(1 + epsilon*epsilon), 1.0/N); | |
| 454 | ✗ | a = (eu - 1.0/eu)/2.0; | |
| 455 | ✗ | b = (ew - g*g/ew)/2.0; | |
| 456 | ✗ | tetta_b = tan(wb/2); | |
| 457 | ✗ | c0 = cos(w0); | |
| 458 | |||
| 459 | ✗ | for (i = 1; i <= L; i++) { | |
| 460 | ✗ | double ui = (2.0 * i - 1.0)/N; | |
| 461 | ✗ | double ci = cos(M_PI * ui / 2.0); | |
| 462 | ✗ | double si = sin(M_PI * ui / 2.0); | |
| 463 | ✗ | double Di = tetta_b*tetta_b + 2*a*si*tetta_b + a*a + ci*ci; | |
| 464 | |||
| 465 | ✗ | chebyshev2_fo_section(&f->section[i - 1], a, ci, tetta_b, g, si, b, Di, c0); | |
| 466 | } | ||
| 467 | } | ||
| 468 | |||
| 469 | ✗ | static double butterworth_compute_bw_gain_db(double gain) | |
| 470 | { | ||
| 471 | ✗ | double bw_gain = 0; | |
| 472 | |||
| 473 | ✗ | if (gain <= -6) | |
| 474 | ✗ | bw_gain = gain + 3; | |
| 475 | ✗ | else if(gain > -6 && gain < 6) | |
| 476 | ✗ | bw_gain = gain * 0.5; | |
| 477 | ✗ | else if(gain >= 6) | |
| 478 | ✗ | bw_gain = gain - 3; | |
| 479 | |||
| 480 | ✗ | return bw_gain; | |
| 481 | } | ||
| 482 | |||
| 483 | 1 | static double chebyshev1_compute_bw_gain_db(double gain) | |
| 484 | { | ||
| 485 | 1 | double bw_gain = 0; | |
| 486 | |||
| 487 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (gain <= -6) |
| 488 | 1 | bw_gain = gain + 1; | |
| 489 | ✗ | else if(gain > -6 && gain < 6) | |
| 490 | ✗ | bw_gain = gain * 0.9; | |
| 491 | ✗ | else if(gain >= 6) | |
| 492 | ✗ | bw_gain = gain - 1; | |
| 493 | |||
| 494 | 1 | return bw_gain; | |
| 495 | } | ||
| 496 | |||
| 497 | ✗ | static double chebyshev2_compute_bw_gain_db(double gain) | |
| 498 | { | ||
| 499 | ✗ | double bw_gain = 0; | |
| 500 | |||
| 501 | ✗ | if (gain <= -6) | |
| 502 | ✗ | bw_gain = -3; | |
| 503 | ✗ | else if(gain > -6 && gain < 6) | |
| 504 | ✗ | bw_gain = gain * 0.3; | |
| 505 | ✗ | else if(gain >= 6) | |
| 506 | ✗ | bw_gain = 3; | |
| 507 | |||
| 508 | ✗ | return bw_gain; | |
| 509 | } | ||
| 510 | |||
| 511 | 2 | static inline double hz_2_rad(double x, double fs) | |
| 512 | { | ||
| 513 | 2 | return 2 * M_PI * x / fs; | |
| 514 | } | ||
| 515 | |||
| 516 | 1 | static void equalizer(EqualizatorFilter *f, double sample_rate) | |
| 517 | { | ||
| 518 | 1 | double w0 = hz_2_rad(f->freq, sample_rate); | |
| 519 | 1 | double wb = hz_2_rad(f->width, sample_rate); | |
| 520 | double bw_gain; | ||
| 521 | |||
| 522 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | switch (f->type) { |
| 523 | ✗ | case BUTTERWORTH: | |
| 524 | ✗ | bw_gain = butterworth_compute_bw_gain_db(f->gain); | |
| 525 | ✗ | butterworth_bp_filter(f, FILTER_ORDER, w0, wb, f->gain, bw_gain, 0); | |
| 526 | ✗ | break; | |
| 527 | 1 | case CHEBYSHEV1: | |
| 528 | 1 | bw_gain = chebyshev1_compute_bw_gain_db(f->gain); | |
| 529 | 1 | chebyshev1_bp_filter(f, FILTER_ORDER, w0, wb, f->gain, bw_gain, 0); | |
| 530 | 1 | break; | |
| 531 | ✗ | case CHEBYSHEV2: | |
| 532 | ✗ | bw_gain = chebyshev2_compute_bw_gain_db(f->gain); | |
| 533 | ✗ | chebyshev2_bp_filter(f, FILTER_ORDER, w0, wb, f->gain, bw_gain, 0); | |
| 534 | ✗ | break; | |
| 535 | } | ||
| 536 | |||
| 537 | 1 | } | |
| 538 | |||
| 539 | 1 | static int add_filter(AudioNEqualizerContext *s, AVFilterLink *inlink) | |
| 540 | { | ||
| 541 | 1 | equalizer(&s->filters[s->nb_filters], inlink->sample_rate); | |
| 542 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (s->nb_filters >= s->nb_allocated - 1) { |
| 543 | EqualizatorFilter *filters; | ||
| 544 | |||
| 545 | ✗ | filters = av_calloc(s->nb_allocated, 2 * sizeof(*s->filters)); | |
| 546 | ✗ | if (!filters) | |
| 547 | ✗ | return AVERROR(ENOMEM); | |
| 548 | ✗ | memcpy(filters, s->filters, sizeof(*s->filters) * s->nb_allocated); | |
| 549 | ✗ | av_free(s->filters); | |
| 550 | ✗ | s->filters = filters; | |
| 551 | ✗ | s->nb_allocated *= 2; | |
| 552 | } | ||
| 553 | 1 | s->nb_filters++; | |
| 554 | |||
| 555 | 1 | return 0; | |
| 556 | } | ||
| 557 | |||
| 558 | 1 | static int config_input(AVFilterLink *inlink) | |
| 559 | { | ||
| 560 | 1 | AVFilterContext *ctx = inlink->dst; | |
| 561 | 1 | AudioNEqualizerContext *s = ctx->priv; | |
| 562 | 1 | char *args = av_strdup(s->args); | |
| 563 | 1 | char *saveptr = NULL; | |
| 564 | 1 | int ret = 0; | |
| 565 | |||
| 566 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!args) |
| 567 | ✗ | return AVERROR(ENOMEM); | |
| 568 | |||
| 569 | 1 | s->nb_allocated = 32 * inlink->ch_layout.nb_channels; | |
| 570 | 1 | s->filters = av_calloc(inlink->ch_layout.nb_channels, 32 * sizeof(*s->filters)); | |
| 571 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!s->filters) { |
| 572 | ✗ | s->nb_allocated = 0; | |
| 573 | ✗ | av_free(args); | |
| 574 | ✗ | return AVERROR(ENOMEM); | |
| 575 | } | ||
| 576 | |||
| 577 | 1 | while (1) { | |
| 578 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | char *arg = av_strtok(s->nb_filters == 0 ? args : NULL, "|", &saveptr); |
| 579 | |||
| 580 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (!arg) |
| 581 | 1 | break; | |
| 582 | |||
| 583 | 1 | s->filters[s->nb_filters].type = 0; | |
| 584 | 1 | if (sscanf(arg, "c%d f=%lf w=%lf g=%lf t=%d", &s->filters[s->nb_filters].channel, | |
| 585 | 1 | &s->filters[s->nb_filters].freq, | |
| 586 | 1 | &s->filters[s->nb_filters].width, | |
| 587 | 1 | &s->filters[s->nb_filters].gain, | |
| 588 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | &s->filters[s->nb_filters].type) != 5 && |
| 589 | ✗ | sscanf(arg, "c%d f=%lf w=%lf g=%lf", &s->filters[s->nb_filters].channel, | |
| 590 | ✗ | &s->filters[s->nb_filters].freq, | |
| 591 | ✗ | &s->filters[s->nb_filters].width, | |
| 592 | ✗ | &s->filters[s->nb_filters].gain) != 4 ) { | |
| 593 | ✗ | av_free(args); | |
| 594 | ✗ | return AVERROR(EINVAL); | |
| 595 | } | ||
| 596 | |||
| 597 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (s->filters[s->nb_filters].freq < 0 || |
| 598 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | s->filters[s->nb_filters].freq > inlink->sample_rate / 2.0) |
| 599 | ✗ | s->filters[s->nb_filters].ignore = 1; | |
| 600 | |||
| 601 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (s->filters[s->nb_filters].channel < 0 || |
| 602 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | s->filters[s->nb_filters].channel >= inlink->ch_layout.nb_channels) |
| 603 | ✗ | s->filters[s->nb_filters].ignore = 1; | |
| 604 | |||
| 605 | 1 | s->filters[s->nb_filters].type = av_clip(s->filters[s->nb_filters].type, 0, NB_TYPES - 1); | |
| 606 | 1 | ret = add_filter(s, inlink); | |
| 607 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
| 608 | ✗ | break; | |
| 609 | } | ||
| 610 | |||
| 611 | 1 | av_free(args); | |
| 612 | |||
| 613 | 1 | return ret; | |
| 614 | } | ||
| 615 | |||
| 616 | ✗ | static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, | |
| 617 | char *res, int res_len, int flags) | ||
| 618 | { | ||
| 619 | ✗ | AudioNEqualizerContext *s = ctx->priv; | |
| 620 | ✗ | AVFilterLink *inlink = ctx->inputs[0]; | |
| 621 | ✗ | int ret = AVERROR(ENOSYS); | |
| 622 | |||
| 623 | ✗ | if (!strcmp(cmd, "change")) { | |
| 624 | double freq, width, gain; | ||
| 625 | int filter; | ||
| 626 | |||
| 627 | ✗ | if (sscanf(args, "%d|f=%lf|w=%lf|g=%lf", &filter, &freq, &width, &gain) != 4) | |
| 628 | ✗ | return AVERROR(EINVAL); | |
| 629 | |||
| 630 | ✗ | if (filter < 0 || filter >= s->nb_filters) | |
| 631 | ✗ | return AVERROR(EINVAL); | |
| 632 | |||
| 633 | ✗ | if (freq < 0 || freq > inlink->sample_rate / 2.0) | |
| 634 | ✗ | return AVERROR(EINVAL); | |
| 635 | |||
| 636 | ✗ | s->filters[filter].freq = freq; | |
| 637 | ✗ | s->filters[filter].width = width; | |
| 638 | ✗ | s->filters[filter].gain = gain; | |
| 639 | ✗ | equalizer(&s->filters[filter], inlink->sample_rate); | |
| 640 | ✗ | if (s->draw_curves) | |
| 641 | ✗ | draw_curves(ctx, inlink, s->video); | |
| 642 | |||
| 643 | ✗ | ret = 0; | |
| 644 | } | ||
| 645 | |||
| 646 | ✗ | return ret; | |
| 647 | } | ||
| 648 | |||
| 649 | 529200 | static inline double section_process(FoSection *S, double in) | |
| 650 | { | ||
| 651 | double out; | ||
| 652 | |||
| 653 | 529200 | out = S->b0 * in; | |
| 654 | 529200 | out+= S->b1 * S->num[0] - S->denum[0] * S->a1; | |
| 655 | 529200 | out+= S->b2 * S->num[1] - S->denum[1] * S->a2; | |
| 656 | 529200 | out+= S->b3 * S->num[2] - S->denum[2] * S->a3; | |
| 657 | 529200 | out+= S->b4 * S->num[3] - S->denum[3] * S->a4; | |
| 658 | |||
| 659 | 529200 | S->num[3] = S->num[2]; | |
| 660 | 529200 | S->num[2] = S->num[1]; | |
| 661 | 529200 | S->num[1] = S->num[0]; | |
| 662 | 529200 | S->num[0] = in; | |
| 663 | |||
| 664 | 529200 | S->denum[3] = S->denum[2]; | |
| 665 | 529200 | S->denum[2] = S->denum[1]; | |
| 666 | 529200 | S->denum[1] = S->denum[0]; | |
| 667 | 529200 | S->denum[0] = out; | |
| 668 | |||
| 669 | 529200 | return out; | |
| 670 | } | ||
| 671 | |||
| 672 | 264600 | static double process_sample(FoSection *s1, double in) | |
| 673 | { | ||
| 674 | 264600 | double p0 = in, p1; | |
| 675 | int i; | ||
| 676 | |||
| 677 |
2/2✓ Branch 0 taken 529200 times.
✓ Branch 1 taken 264600 times.
|
793800 | for (i = 0; i < FILTER_ORDER / 2; i++) { |
| 678 | 529200 | p1 = section_process(&s1[i], p0); | |
| 679 | 529200 | p0 = p1; | |
| 680 | } | ||
| 681 | |||
| 682 | 264600 | return p1; | |
| 683 | } | ||
| 684 | |||
| 685 | 130 | static int filter_channels(AVFilterContext *ctx, void *arg, | |
| 686 | int jobnr, int nb_jobs) | ||
| 687 | { | ||
| 688 | 130 | AudioNEqualizerContext *s = ctx->priv; | |
| 689 | 130 | AVFrame *buf = arg; | |
| 690 | 130 | const int start = (buf->ch_layout.nb_channels * jobnr) / nb_jobs; | |
| 691 | 130 | const int end = (buf->ch_layout.nb_channels * (jobnr+1)) / nb_jobs; | |
| 692 | |||
| 693 |
2/2✓ Branch 0 taken 130 times.
✓ Branch 1 taken 130 times.
|
260 | for (int i = 0; i < s->nb_filters; i++) { |
| 694 | 130 | EqualizatorFilter *f = &s->filters[i]; | |
| 695 | double *bptr; | ||
| 696 | |||
| 697 |
2/4✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 130 times.
|
130 | if (f->gain == 0. || f->ignore) |
| 698 | ✗ | continue; | |
| 699 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 65 times.
|
130 | if (f->channel < start || |
| 700 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | f->channel >= end) |
| 701 | 65 | continue; | |
| 702 | |||
| 703 | 65 | bptr = (double *)buf->extended_data[f->channel]; | |
| 704 |
2/2✓ Branch 0 taken 264600 times.
✓ Branch 1 taken 65 times.
|
264665 | for (int n = 0; n < buf->nb_samples; n++) { |
| 705 | 264600 | double sample = bptr[n]; | |
| 706 | |||
| 707 | 264600 | sample = process_sample(f->section, sample); | |
| 708 | 264600 | bptr[n] = sample; | |
| 709 | } | ||
| 710 | } | ||
| 711 | |||
| 712 | 130 | return 0; | |
| 713 | } | ||
| 714 | |||
| 715 | 65 | static int filter_frame(AVFilterLink *inlink, AVFrame *buf) | |
| 716 | { | ||
| 717 | 65 | AVFilterContext *ctx = inlink->dst; | |
| 718 | 65 | AudioNEqualizerContext *s = ctx->priv; | |
| 719 | 65 | AVFilterLink *outlink = ctx->outputs[0]; | |
| 720 | |||
| 721 |
1/2✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
|
65 | if (!ctx->is_disabled) |
| 722 | 65 | ff_filter_execute(ctx, filter_channels, buf, NULL, | |
| 723 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | FFMIN(inlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx))); |
| 724 | |||
| 725 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (s->draw_curves) { |
| 726 | AVFrame *clone; | ||
| 727 | |||
| 728 | ✗ | const int64_t pts = buf->pts + | |
| 729 | ✗ | av_rescale_q(buf->nb_samples, (AVRational){ 1, inlink->sample_rate }, | |
| 730 | outlink->time_base); | ||
| 731 | int ret; | ||
| 732 | |||
| 733 | ✗ | s->video->pts = pts; | |
| 734 | ✗ | clone = av_frame_clone(s->video); | |
| 735 | ✗ | if (!clone) | |
| 736 | ✗ | return AVERROR(ENOMEM); | |
| 737 | ✗ | ret = ff_filter_frame(ctx->outputs[1], clone); | |
| 738 | ✗ | if (ret < 0) | |
| 739 | ✗ | return ret; | |
| 740 | } | ||
| 741 | |||
| 742 | 65 | return ff_filter_frame(outlink, buf); | |
| 743 | } | ||
| 744 | |||
| 745 | static const AVFilterPad inputs[] = { | ||
| 746 | { | ||
| 747 | .name = "default", | ||
| 748 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 749 | .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE, | ||
| 750 | .config_props = config_input, | ||
| 751 | .filter_frame = filter_frame, | ||
| 752 | }, | ||
| 753 | }; | ||
| 754 | |||
| 755 | const FFFilter ff_af_anequalizer = { | ||
| 756 | .p.name = "anequalizer", | ||
| 757 | .p.description = NULL_IF_CONFIG_SMALL("Apply high-order audio parametric multi band equalizer."), | ||
| 758 | .p.priv_class = &anequalizer_class, | ||
| 759 | .p.outputs = NULL, | ||
| 760 | .p.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS | | ||
| 761 | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | | ||
| 762 | AVFILTER_FLAG_SLICE_THREADS, | ||
| 763 | .priv_size = sizeof(AudioNEqualizerContext), | ||
| 764 | .init = init, | ||
| 765 | .uninit = uninit, | ||
| 766 | FILTER_INPUTS(inputs), | ||
| 767 | FILTER_QUERY_FUNC2(query_formats), | ||
| 768 | .process_command = process_command, | ||
| 769 | }; | ||
| 770 |