| @@ -27,13 +27,9 @@ typedef struct BiquadCoeffs { · 26 unmodified lines |
| 27 | double a0, a1, a2, b1, b2; | 27 | double a0, a1, a2, b1, b2; |
| 28 | } BiquadCoeffs; | 28 | } BiquadCoeffs; |
| 29 | | 29 | |
| 30 | typedef struct BiquadD2 { | | |
| 31 | double a0, a1, a2, b1, b2, w1, w2; | | |
| 32 | } BiquadD2; | | |
| 33 | | | |
| 34 | typedef struct RIAACurve { | 30 | typedef struct RIAACurve { |
| 35 | BiquadD2 r1; | 31 | BiquadCoeffs r1; |
| 36 | BiquadD2 brickw; | 32 | BiquadCoeffs brickw; |
| 37 | int use_brickw; | 33 | int use_brickw; |
| 38 | } RIAACurve; | 34 | } RIAACurve; |
| 39 | | 35 | |
| @@ -42,11 +38,13 @@ typedef struct AudioEmphasisContext { · 2 unmodified lines |
| 42 | int mode, type; | 38 | int mode, type; |
| 43 | double level_in, level_out; | 39 | double level_in, level_out; |
| 44 | | 40 | |
| 45 | RIAACurve *rc; | 41 | RIAACurve rc; |
| | 42 | |
| | 43 | AVFrame *w; |
| 46 | } AudioEmphasisContext; | 44 | } AudioEmphasisContext; |
| 47 | | 45 | |
| 48 | #define OFFSET(x) offsetof(AudioEmphasisContext, x) | 46 | #define OFFSET(x) offsetof(AudioEmphasisContext, x) |
| 49 | #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | 47 | #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM |
| 50 | | 48 | |
| 51 | static const AVOption aemphasis_options[] = { | 49 | static const AVOption aemphasis_options[] = { |
| 52 | { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, FLAGS }, | 50 | { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, FLAGS }, |
| @@ -69,16 +67,16 @@ static const AVOption aemphasis_options[] = { · 16 unmodified lines |
| 69 | | 67 | |
| 70 | AVFILTER_DEFINE_CLASS(aemphasis); | 68 | AVFILTER_DEFINE_CLASS(aemphasis); |
| 71 | | 69 | |
| 72 | static inline void biquad_process(BiquadD2 *bq, double *dst, const double *src, int nb_samples, | 70 | static inline void biquad_process(BiquadCoeffs *bq, double *dst, const double *src, int nb_samples, |
| 73 | double level_in, double level_out) | 71 | double *w, double level_in, double level_out) |
| 74 | { | 72 | { |
| 75 | const double a0 = bq->a0; | 73 | const double a0 = bq->a0; |
| 76 | const double a1 = bq->a1; | 74 | const double a1 = bq->a1; |
| 77 | const double a2 = bq->a2; | 75 | const double a2 = bq->a2; |
| 78 | const double b1 = bq->b1; | 76 | const double b1 = bq->b1; |
| 79 | const double b2 = bq->b2; | 77 | const double b2 = bq->b2; |
| 80 | double w1 = bq->w1; | 78 | double w1 = w[0]; |
| 81 | double w2 = bq->w2; | 79 | double w2 = w[1]; |
| 82 | | 80 | |
| 83 | for (int i = 0; i < nb_samples; i++) { | 81 | for (int i = 0; i < nb_samples; i++) { |
| 84 | double n = src[i] * level_in; | 82 | double n = src[i] * level_in; |
| @@ -91,17 +89,46 @@ static inline void biquad_process(BiquadD2 *bq, double *dst, const double *src, · 6 unmodified lines |
| 91 | dst[i] = out * level_out; | 89 | dst[i] = out * level_out; |
| 92 | } | 90 | } |
| 93 | | 91 | |
| 94 | bq->w1 = w1; | 92 | w[0] = w1; |
| 95 | bq->w2 = w2; | 93 | w[1] = w2; |
| 96 | } | 94 | } |
| 97 | | 95 | |
| 98 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | 96 | typedef struct ThreadData { |
| | 97 | AVFrame *in, *out; |
| | 98 | } ThreadData; |
| | 99 | |
| | 100 | static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
| 99 | { | 101 | { |
| 100 | AVFilterContext *ctx = inlink->dst; | | |
| 101 | AVFilterLink *outlink = ctx->outputs[0]; | | |
| 102 | AudioEmphasisContext *s = ctx->priv; | 102 | AudioEmphasisContext *s = ctx->priv; |
| 103 | const double level_out = s->level_out; | 103 | const double level_out = s->level_out; |
| 104 | const double level_in = s->level_in; | 104 | const double level_in = s->level_in; |
| | 105 | ThreadData *td = arg; |
| | 106 | AVFrame *out = td->out; |
| | 107 | AVFrame *in = td->in; |
| | 108 | const int start = (in->channels * jobnr) / nb_jobs; |
| | 109 | const int end = (in->channels * (jobnr+1)) / nb_jobs; |
| | 110 | |
| | 111 | for (int ch = start; ch < end; ch++) { |
| | 112 | const double *src = (const double *)in->extended_data[ch]; |
| | 113 | double *w = (double *)s->w->extended_data[ch]; |
| | 114 | double *dst = (double *)out->extended_data[ch]; |
| | 115 | |
| | 116 | if (s->rc.use_brickw) { |
| | 117 | biquad_process(&s->rc.brickw, dst, src, in->nb_samples, w + 2, level_in, 1.); |
| | 118 | biquad_process(&s->rc.r1, dst, dst, in->nb_samples, w, 1., level_out); |
| | 119 | } else { |
| | 120 | biquad_process(&s->rc.r1, dst, src, in->nb_samples, w, level_in, level_out); |
| | 121 | } |
| | 122 | } |
| | 123 | |
| | 124 | return 0; |
| | 125 | } |
| | 126 | |
| | 127 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
| | 128 | { |
| | 129 | AVFilterContext *ctx = inlink->dst; |
| | 130 | AVFilterLink *outlink = ctx->outputs[0]; |
| | 131 | ThreadData td; |
| 105 | AVFrame *out; | 132 | AVFrame *out; |
| 106 | | 133 | |
| 107 | if (av_frame_is_writable(in)) { | 134 | if (av_frame_is_writable(in)) { |
| @@ -115,17 +142,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 7 unmodified lines |
| 115 | av_frame_copy_props(out, in); | 142 | av_frame_copy_props(out, in); |
| 116 | } | 143 | } |
| 117 | | 144 | |
| 118 | for (int ch = 0; ch < inlink->channels; ch++) { | 145 | td.in = in; td.out = out; |
| 119 | const double *src = (const double *)in->extended_data[ch]; | 146 | ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(inlink->channels, |
| 120 | double *dst = (double *)out->extended_data[ch]; | 147 | ff_filter_get_nb_threads(ctx))); |
| 121 | | | |
| 122 | if (s->rc[ch].use_brickw) { | | |
| 123 | biquad_process(&s->rc[ch].brickw, dst, src, in->nb_samples, level_in, 1.); | | |
| 124 | biquad_process(&s->rc[ch].r1, dst, dst, in->nb_samples, 1., level_out); | | |
| 125 | } else { | | |
| 126 | biquad_process(&s->rc[ch].r1, dst, src, in->nb_samples, level_in, level_out); | | |
| 127 | } | | |
| 128 | } | | |
| 129 | | 148 | |
| 130 | if (in != out) | 149 | if (in != out) |
| 131 | av_frame_free(&in); | 150 | av_frame_free(&in); |
| @@ -162,7 +181,7 @@ static int query_formats(AVFilterContext *ctx) · 30 unmodified lines |
| 162 | return ff_set_common_samplerates(ctx, formats); | 181 | return ff_set_common_samplerates(ctx, formats); |
| 163 | } | 182 | } |
| 164 | | 183 | |
| 165 | static inline void set_highshelf_rbj(BiquadD2 *bq, double freq, double q, double peak, double sr) | 184 | static inline void set_highshelf_rbj(BiquadCoeffs *bq, double freq, double q, double peak, double sr) |
| 166 | { | 185 | { |
| 167 | double A = sqrt(peak); | 186 | double A = sqrt(peak); |
| 168 | double w0 = freq * 2 * M_PI / sr; | 187 | double w0 = freq * 2 * M_PI / sr; |
| @@ -186,7 +205,7 @@ static inline void set_highshelf_rbj(BiquadD2 *bq, double freq, double q, double · 17 unmodified lines |
| 186 | bq->a2 *= ib0; | 205 | bq->a2 *= ib0; |
| 187 | } | 206 | } |
| 188 | | 207 | |
| 189 | static inline void set_lp_rbj(BiquadD2 *bq, double fc, double q, double sr, double gain) | 208 | static inline void set_lp_rbj(BiquadCoeffs *bq, double fc, double q, double sr, double gain) |
| 190 | { | 209 | { |
| 191 | double omega = 2.0 * M_PI * fc / sr; | 210 | double omega = 2.0 * M_PI * fc / sr; |
| 192 | double sn = sin(omega); | 211 | double sn = sin(omega); |
| @@ -220,10 +239,10 @@ static int config_input(AVFilterLink *inlink) · 27 unmodified lines |
| 220 | AVFilterContext *ctx = inlink->dst; | 239 | AVFilterContext *ctx = inlink->dst; |
| 221 | AudioEmphasisContext *s = ctx->priv; | 240 | AudioEmphasisContext *s = ctx->priv; |
| 222 | BiquadCoeffs coeffs; | 241 | BiquadCoeffs coeffs; |
| 223 | int ch; | | |
| 224 | | 242 | |
| 225 | s->rc = av_calloc(inlink->channels, sizeof(*s->rc)); | 243 | if (!s->w) |
| 226 | if (!s->rc) | 244 | s->w = ff_get_audio_buffer(inlink, 4); |
| | 245 | if (!s->w) |
| 227 | return AVERROR(ENOMEM); | 246 | return AVERROR(ENOMEM); |
| 228 | | 247 | |
| 229 | switch (s->type) { | 248 | switch (s->type) { |
| @@ -297,12 +316,12 @@ static int config_input(AVFilterLink *inlink) · 67 unmodified lines |
| 297 | if (s->type == 7) | 316 | if (s->type == 7) |
| 298 | q = pow((sr / 4750.0) + 19.5, -0.25); | 317 | q = pow((sr / 4750.0) + 19.5, -0.25); |
| 299 | if (s->mode == 0) | 318 | if (s->mode == 0) |
| 300 | set_highshelf_rbj(&s->rc[0].r1, cfreq, q, 1. / gain, sr); | 319 | set_highshelf_rbj(&s->rc.r1, cfreq, q, 1. / gain, sr); |
| 301 | else | 320 | else |
| 302 | set_highshelf_rbj(&s->rc[0].r1, cfreq, q, gain, sr); | 321 | set_highshelf_rbj(&s->rc.r1, cfreq, q, gain, sr); |
| 303 | s->rc[0].use_brickw = 0; | 322 | s->rc.use_brickw = 0; |
| 304 | } else { | 323 | } else { |
| 305 | s->rc[0].use_brickw = 1; | 324 | s->rc.use_brickw = 1; |
| 306 | if (s->mode == 0) { // Reproduction | 325 | if (s->mode == 0) { // Reproduction |
| 307 | g = 1. / (4.+2.*i*t+2.*k*t+i*k*t*t); | 326 | g = 1. / (4.+2.*i*t+2.*k*t+i*k*t*t); |
| 308 | a0 = (2.*t+j*t*t)*g; | 327 | a0 = (2.*t+j*t*t)*g; |
| @@ -331,27 +350,36 @@ static int config_input(AVFilterLink *inlink) · 22 unmodified lines |
| 331 | gain1kHz = freq_gain(&coeffs, 1000.0, sr); | 350 | gain1kHz = freq_gain(&coeffs, 1000.0, sr); |
| 332 | // divide one filter's x[n-m] coefficients by that value | 351 | // divide one filter's x[n-m] coefficients by that value |
| 333 | gc = 1.0 / gain1kHz; | 352 | gc = 1.0 / gain1kHz; |
| 334 | s->rc[0].r1.a0 = coeffs.a0 * gc; | 353 | s->rc.r1.a0 = coeffs.a0 * gc; |
| 335 | s->rc[0].r1.a1 = coeffs.a1 * gc; | 354 | s->rc.r1.a1 = coeffs.a1 * gc; |
| 336 | s->rc[0].r1.a2 = coeffs.a2 * gc; | 355 | s->rc.r1.a2 = coeffs.a2 * gc; |
| 337 | s->rc[0].r1.b1 = coeffs.b1; | 356 | s->rc.r1.b1 = coeffs.b1; |
| 338 | s->rc[0].r1.b2 = coeffs.b2; | 357 | s->rc.r1.b2 = coeffs.b2; |
| 339 | } | 358 | } |
| 340 | | 359 | |
| 341 | cutfreq = FFMIN(0.45 * sr, 21000.); | 360 | cutfreq = FFMIN(0.45 * sr, 21000.); |
| 342 | set_lp_rbj(&s->rc[0].brickw, cutfreq, 0.707, sr, 1.); | 361 | set_lp_rbj(&s->rc.brickw, cutfreq, 0.707, sr, 1.); |
| 343 | | | |
| 344 | for (ch = 1; ch < inlink->channels; ch++) { | | |
| 345 | memcpy(&s->rc[ch], &s->rc[0], sizeof(RIAACurve)); | | |
| 346 | } | | |
| 347 | | 362 | |
| 348 | return 0; | 363 | return 0; |
| 349 | } | 364 | } |
| 350 | | 365 | |
| | 366 | static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, |
| | 367 | char *res, int res_len, int flags) |
| | 368 | { |
| | 369 | int ret; |
| | 370 | |
| | 371 | ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags); |
| | 372 | if (ret < 0) |
| | 373 | return ret; |
| | 374 | |
| | 375 | return config_input(ctx->inputs[0]); |
| | 376 | } |
| | 377 | |
| 351 | static av_cold void uninit(AVFilterContext *ctx) | 378 | static av_cold void uninit(AVFilterContext *ctx) |
| 352 | { | 379 | { |
| 353 | AudioEmphasisContext *s = ctx->priv; | 380 | AudioEmphasisContext *s = ctx->priv; |
| 354 | av_freep(&s->rc); | 381 | |
| | 382 | av_frame_free(&s->w); |
| 355 | } | 383 | } |
| 356 | | 384 | |
| 357 | static const AVFilterPad avfilter_af_aemphasis_inputs[] = { | 385 | static const AVFilterPad avfilter_af_aemphasis_inputs[] = { |
| @@ -381,4 +409,7 @@ AVFilter ff_af_aemphasis = { · 23 unmodified lines |
| 381 | .query_formats = query_formats, | 409 | .query_formats = query_formats, |
| 382 | .inputs = avfilter_af_aemphasis_inputs, | 410 | .inputs = avfilter_af_aemphasis_inputs, |
| 383 | .outputs = avfilter_af_aemphasis_outputs, | 411 | .outputs = avfilter_af_aemphasis_outputs, |
| | 412 | .process_command = process_command, |
| | 413 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | |
| | 414 | AVFILTER_FLAG_SLICE_THREADS, |
| 384 | }; | 415 | }; |