| @@ -43,242 +43,82 @@ typedef struct AudioDynamicEqualizerContext { · 42 unmodified lines |
| 43 | int detection; | 43 | int detection; |
| 44 | int tftype; | 44 | int tftype; |
| 45 | int dftype; | 45 | int dftype; |
| | 46 | int precision; |
| | 47 | int format; |
| | 48 | |
| | 49 | int (*filter_prepare)(AVFilterContext *ctx); |
| | 50 | int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); |
| | 51 | |
| | 52 | double da_double[3], dm_double[3]; |
| | 53 | float da_float[3], dm_float[3]; |
| 46 | | 54 | |
| 47 | double da[3], dm[3]; | | |
| 48 | AVFrame *state; | 55 | AVFrame *state; |
| 49 | } AudioDynamicEqualizerContext; | 56 | } AudioDynamicEqualizerContext; |
| 50 | | 57 | |
| 51 | static int config_input(AVFilterLink *inlink) | 58 | static int query_formats(AVFilterContext *ctx) |
| 52 | { | 59 | { |
| 53 | AVFilterContext *ctx = inlink->dst; | | |
| 54 | AudioDynamicEqualizerContext *s = ctx->priv; | 60 | AudioDynamicEqualizerContext *s = ctx->priv; |
| | 61 | static const enum AVSampleFormat sample_fmts[3][3] = { |
| | 62 | { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE }, |
| | 63 | { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE }, |
| | 64 | { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE }, |
| | 65 | }; |
| | 66 | int ret; |
| 55 | | 67 | |
| 56 | s->state = ff_get_audio_buffer(inlink, 8); | 68 | if ((ret = ff_set_common_all_channel_counts(ctx)) < 0) |
| 57 | if (!s->state) | 69 | return ret; |
| 58 | return AVERROR(ENOMEM); | | |
| 59 | | | |
| 60 | for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { | | |
| 61 | double *state = (double *)s->state->extended_data[ch]; | | |
| 62 | | 70 | |
| 63 | state[4] = 1.; | 71 | if ((ret = ff_set_common_formats_from_list(ctx, sample_fmts[s->precision])) < 0) |
| 64 | } | 72 | return ret; |
| 65 | | 73 | |
| 66 | return 0; | 74 | return ff_set_common_all_samplerates(ctx); |
| 67 | } | 75 | } |
| 68 | | 76 | |
| 69 | static double get_svf(double in, const double *m, const double *a, double *b) | 77 | static double get_coef(double x, double sr) |
| 70 | { | 78 | { |
| 71 | const double v0 = in; | 79 | return exp(-1000. / (x * sr)); |
| 72 | const double v3 = v0 - b[1]; | | |
| 73 | const double v1 = a[0] * b[0] + a[1] * v3; | | |
| 74 | const double v2 = b[1] + a[1] * b[0] + a[2] * v3; | | |
| 75 | | | |
| 76 | b[0] = 2. * v1 - b[0]; | | |
| 77 | b[1] = 2. * v2 - b[1]; | | |
| 78 | | | |
| 79 | return m[0] * v0 + m[1] * v1 + m[2] * v2; | | |
| 80 | } | 80 | } |
| 81 | | 81 | |
| 82 | typedef struct ThreadData { | 82 | typedef struct ThreadData { |
| 83 | AVFrame *in, *out; | 83 | AVFrame *in, *out; |
| 84 | } ThreadData; | 84 | } ThreadData; |
| 85 | | 85 | |
| 86 | static double get_coef(double x, double sr) | 86 | #define DEPTH 32 |
| 87 | { | 87 | #include "adynamicequalizer_template.c" |
| 88 | return exp(-1000. / (x * sr)); | | |
| 89 | } | | |
| 90 | | 88 | |
| 91 | static int filter_prepare(AVFilterContext *ctx) | 89 | #undef DEPTH |
| | 90 | #define DEPTH 64 |
| | 91 | #include "adynamicequalizer_template.c" |
| | 92 | |
| | 93 | static int config_input(AVFilterLink *inlink) |
| 92 | { | 94 | { |
| | 95 | AVFilterContext *ctx = inlink->dst; |
| 93 | AudioDynamicEqualizerContext *s = ctx->priv; | 96 | AudioDynamicEqualizerContext *s = ctx->priv; |
| 94 | const double sample_rate = ctx->inputs[0]->sample_rate; | | |
| 95 | const double dfrequency = fmin(s->dfrequency, sample_rate * 0.5); | | |
| 96 | const double dg = tan(M_PI * dfrequency / sample_rate); | | |
| 97 | const double dqfactor = s->dqfactor; | | |
| 98 | const int dftype = s->dftype; | | |
| 99 | double *da = s->da; | | |
| 100 | double *dm = s->dm; | | |
| 101 | double k; | | |
| 102 | | | |
| 103 | s->attack_coef = get_coef(s->attack, sample_rate); | | |
| 104 | s->release_coef = get_coef(s->release, sample_rate); | | |
| 105 | | | |
| 106 | switch (dftype) { | | |
| 107 | case 0: | | |
| 108 | k = 1. / dqfactor; | | |
| 109 | | | |
| 110 | da[0] = 1. / (1. + dg * (dg + k)); | | |
| 111 | da[1] = dg * da[0]; | | |
| 112 | da[2] = dg * da[1]; | | |
| 113 | | | |
| 114 | dm[0] = 0.; | | |
| 115 | dm[1] = k; | | |
| 116 | dm[2] = 0.; | | |
| 117 | break; | | |
| 118 | case 1: | | |
| 119 | k = 1. / dqfactor; | | |
| 120 | | | |
| 121 | da[0] = 1. / (1. + dg * (dg + k)); | | |
| 122 | da[1] = dg * da[0]; | | |
| 123 | da[2] = dg * da[1]; | | |
| 124 | | | |
| 125 | dm[0] = 0.; | | |
| 126 | dm[1] = 0.; | | |
| 127 | dm[2] = 1.; | | |
| 128 | break; | | |
| 129 | case 2: | | |
| 130 | k = 1. / dqfactor; | | |
| 131 | | 97 | |
| 132 | da[0] = 1. / (1. + dg * (dg + k)); | 98 | s->format = inlink->format; |
| 133 | da[1] = dg * da[0]; | 99 | s->state = ff_get_audio_buffer(inlink, 8); |
| 134 | da[2] = dg * da[1]; | 100 | if (!s->state) |
| 135 | | 101 | return AVERROR(ENOMEM); |
| 136 | dm[0] = 0.; | | |
| 137 | dm[1] = -k; | | |
| 138 | dm[2] = -1.; | | |
| 139 | break; | | |
| 140 | case 3: | | |
| 141 | k = 1. / dqfactor; | | |
| 142 | | 102 | |
| 143 | da[0] = 1. / (1. + dg * (dg + k)); | 103 | switch (s->format) { |
| 144 | da[1] = dg * da[0]; | 104 | case AV_SAMPLE_FMT_DBLP: |
| 145 | da[2] = dg * da[1]; | 105 | for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { |
| | 106 | double *state = (double *)s->state->extended_data[ch]; |
| 146 | | 107 | |
| 147 | dm[0] = 0.; | 108 | state[4] = 1.; |
| 148 | dm[1] = -k; | 109 | } |
| 149 | dm[2] = -2.; | 110 | s->filter_prepare = filter_prepare_double; |
| | 111 | s->filter_channels = filter_channels_double; |
| 150 | break; | 112 | break; |
| 151 | } | 113 | case AV_SAMPLE_FMT_FLTP: |
| 152 | | 114 | for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { |
| 153 | return 0; | 115 | float *state = (float *)s->state->extended_data[ch]; |
| 154 | } | | |
| 155 | | 116 | |
| 156 | static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | 117 | state[4] = 1.; |
| 157 | { | | |
| 158 | AudioDynamicEqualizerContext *s = ctx->priv; | | |
| 159 | ThreadData *td = arg; | | |
| 160 | AVFrame *in = td->in; | | |
| 161 | AVFrame *out = td->out; | | |
| 162 | const double sample_rate = in->sample_rate; | | |
| 163 | const double makeup = s->makeup; | | |
| 164 | const double ratio = s->ratio; | | |
| 165 | const double range = s->range; | | |
| 166 | const double tfrequency = fmin(s->tfrequency, sample_rate * 0.5); | | |
| 167 | const double release = s->release_coef; | | |
| 168 | const double irelease = 1. - release; | | |
| 169 | const double attack = s->attack_coef; | | |
| 170 | const double iattack = 1. - attack; | | |
| 171 | const double tqfactor = s->tqfactor; | | |
| 172 | const double fg = tan(M_PI * tfrequency / sample_rate); | | |
| 173 | const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs; | | |
| 174 | const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs; | | |
| 175 | const int detection = s->detection; | | |
| 176 | const int direction = s->direction; | | |
| 177 | const int tftype = s->tftype; | | |
| 178 | const int mode = s->mode; | | |
| 179 | const double *da = s->da; | | |
| 180 | const double *dm = s->dm; | | |
| 181 | | | |
| 182 | for (int ch = start; ch < end; ch++) { | | |
| 183 | const double *src = (const double *)in->extended_data[ch]; | | |
| 184 | double *dst = (double *)out->extended_data[ch]; | | |
| 185 | double *state = (double *)s->state->extended_data[ch]; | | |
| 186 | const double threshold = detection == 0 ? state[5] : s->threshold; | | |
| 187 | | | |
| 188 | if (detection < 0) | | |
| 189 | state[5] = threshold; | | |
| 190 | | | |
| 191 | for (int n = 0; n < out->nb_samples; n++) { | | |
| 192 | double detect, gain, v, listen; | | |
| 193 | double fa[3], fm[3]; | | |
| 194 | double k, g; | | |
| 195 | | | |
| 196 | detect = listen = get_svf(src[n], dm, da, state); | | |
| 197 | detect = fabs(detect); | | |
| 198 | | | |
| 199 | if (detection > 0) | | |
| 200 | state[5] = fmax(state[5], detect); | | |
| 201 | | | |
| 202 | if (direction == 0) { | | |
| 203 | if (detect < threshold) { | | |
| 204 | if (mode == 0) | | |
| 205 | detect = 1. / av_clipd(1. + makeup + (threshold - detect) * ratio, 1., range); | | |
| 206 | else | | |
| 207 | detect = av_clipd(1. + makeup + (threshold - detect) * ratio, 1., range); | | |
| 208 | } else { | | |
| 209 | detect = 1.; | | |
| 210 | } | | |
| 211 | } else { | | |
| 212 | if (detect > threshold) { | | |
| 213 | if (mode == 0) | | |
| 214 | detect = 1. / av_clipd(1. + makeup + (detect - threshold) * ratio, 1., range); | | |
| 215 | else | | |
| 216 | detect = av_clipd(1. + makeup + (detect - threshold) * ratio, 1., range); | | |
| 217 | } else { | | |
| 218 | detect = 1.; | | |
| 219 | } | | |
| 220 | } | | |
| 221 | | | |
| 222 | if (direction == 0) { | | |
| 223 | if (detect > state[4]) { | | |
| 224 | detect = iattack * detect + attack * state[4]; | | |
| 225 | } else { | | |
| 226 | detect = irelease * detect + release * state[4]; | | |
| 227 | } | | |
| 228 | } else { | | |
| 229 | if (detect < state[4]) { | | |
| 230 | detect = iattack * detect + attack * state[4]; | | |
| 231 | } else { | | |
| 232 | detect = irelease * detect + release * state[4]; | | |
| 233 | } | | |
| 234 | } | | |
| 235 | | | |
| 236 | if (state[4] != detect || n == 0) { | | |
| 237 | state[4] = gain = detect; | | |
| 238 | | | |
| 239 | switch (tftype) { | | |
| 240 | case 0: | | |
| 241 | k = 1. / (tqfactor * gain); | | |
| 242 | | | |
| 243 | fa[0] = 1. / (1. + fg * (fg + k)); | | |
| 244 | fa[1] = fg * fa[0]; | | |
| 245 | fa[2] = fg * fa[1]; | | |
| 246 | | | |
| 247 | fm[0] = 1.; | | |
| 248 | fm[1] = k * (gain * gain - 1.); | | |
| 249 | fm[2] = 0.; | | |
| 250 | break; | | |
| 251 | case 1: | | |
| 252 | k = 1. / tqfactor; | | |
| 253 | g = fg / sqrt(gain); | | |
| 254 | | | |
| 255 | fa[0] = 1. / (1. + g * (g + k)); | | |
| 256 | fa[1] = g * fa[0]; | | |
| 257 | fa[2] = g * fa[1]; | | |
| 258 | | | |
| 259 | fm[0] = 1.; | | |
| 260 | fm[1] = k * (gain - 1.); | | |
| 261 | fm[2] = gain * gain - 1.; | | |
| 262 | break; | | |
| 263 | case 2: | | |
| 264 | k = 1. / tqfactor; | | |
| 265 | g = fg / sqrt(gain); | | |
| 266 | | | |
| 267 | fa[0] = 1. / (1. + g * (g + k)); | | |
| 268 | fa[1] = g * fa[0]; | | |
| 269 | fa[2] = g * fa[1]; | | |
| 270 | | | |
| 271 | fm[0] = gain * gain; | | |
| 272 | fm[1] = k * (1. - gain) * gain; | | |
| 273 | fm[2] = 1. - gain * gain; | | |
| 274 | break; | | |
| 275 | } | | |
| 276 | } | | |
| 277 | | | |
| 278 | v = get_svf(src[n], fm, fa, &state[2]); | | |
| 279 | v = mode == -1 ? listen : v; | | |
| 280 | dst[n] = ctx->is_disabled ? src[n] : v; | | |
| 281 | } | 118 | } |
| | 119 | s->filter_prepare = filter_prepare_float; |
| | 120 | s->filter_channels = filter_channels_float; |
| | 121 | break; |
| 282 | } | 122 | } |
| 283 | | 123 | |
| 284 | return 0; | 124 | return 0; |
| @@ -288,6 +128,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 3 unmodified lines |
| 288 | { | 128 | { |
| 289 | AVFilterContext *ctx = inlink->dst; | 129 | AVFilterContext *ctx = inlink->dst; |
| 290 | AVFilterLink *outlink = ctx->outputs[0]; | 130 | AVFilterLink *outlink = ctx->outputs[0]; |
| | 131 | AudioDynamicEqualizerContext *s = ctx->priv; |
| 291 | ThreadData td; | 132 | ThreadData td; |
| 292 | AVFrame *out; | 133 | AVFrame *out; |
| 293 | | 134 | |
| @@ -304,8 +145,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 10 unmodified lines |
| 304 | | 145 | |
| 305 | td.in = in; | 146 | td.in = in; |
| 306 | td.out = out; | 147 | td.out = out; |
| 307 | filter_prepare(ctx); | 148 | s->filter_prepare(ctx); |
| 308 | ff_filter_execute(ctx, filter_channels, &td, NULL, | 149 | ff_filter_execute(ctx, s->filter_channels, &td, NULL, |
| 309 | FFMIN(outlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx))); | 150 | FFMIN(outlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx))); |
| 310 | | 151 | |
| 311 | if (out != in) | 152 | if (out != in) |
| @@ -321,6 +162,7 @@ static av_cold void uninit(AVFilterContext *ctx) · 9 unmodified lines |
| 321 | } | 162 | } |
| 322 | | 163 | |
| 323 | #define OFFSET(x) offsetof(AudioDynamicEqualizerContext, x) | 164 | #define OFFSET(x) offsetof(AudioDynamicEqualizerContext, x) |
| | 165 | #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
| 324 | #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM | 166 | #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM |
| 325 | | 167 | |
| 326 | static const AVOption adynamicequalizer_options[] = { | 168 | static const AVOption adynamicequalizer_options[] = { |
| @@ -354,6 +196,10 @@ static const AVOption adynamicequalizer_options[] = { · 27 unmodified lines |
| 354 | { "disabled", 0, 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, FLAGS, "auto" }, | 196 | { "disabled", 0, 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, FLAGS, "auto" }, |
| 355 | { "off", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "auto" }, | 197 | { "off", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "auto" }, |
| 356 | { "on", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "auto" }, | 198 | { "on", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "auto" }, |
| | 199 | { "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, AF, "precision" }, |
| | 200 | { "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "precision" }, |
| | 201 | { "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "precision" }, |
| | 202 | { "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "precision" }, |
| 357 | { NULL } | 203 | { NULL } |
| 358 | }; | 204 | }; |
| 359 | | 205 | |
| @@ -383,7 +229,7 @@ const AVFilter ff_af_adynamicequalizer = { · 23 unmodified lines |
| 383 | .uninit = uninit, | 229 | .uninit = uninit, |
| 384 | FILTER_INPUTS(inputs), | 230 | FILTER_INPUTS(inputs), |
| 385 | FILTER_OUTPUTS(outputs), | 231 | FILTER_OUTPUTS(outputs), |
| 386 | FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP), | 232 | FILTER_QUERY_FUNC(query_formats), |
| 387 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | | 233 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | |
| 388 | AVFILTER_FLAG_SLICE_THREADS, | 234 | AVFILTER_FLAG_SLICE_THREADS, |
| 389 | .process_command = ff_filter_process_command, | 235 | .process_command = ff_filter_process_command, |