1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-26 19:01:44 +02:00

avfilter/af_acrossover: add per output band gain

This commit is contained in:
Paul B Mahol 2020-12-02 13:48:32 +01:00
parent 13df9bfbcb
commit 0c8a0d3a56
2 changed files with 55 additions and 8 deletions

View File

@ -554,6 +554,9 @@ Default is @var{4th}.
@item level @item level
Set input gain level. Allowed range is from 0 to 1. Default value is 1. Set input gain level. Allowed range is from 0 to 1. Default value is 1.
@item gains
Set output gain for each band. Default value is 1 for all bands.
@end table @end table
@subsection Examples @subsection Examples

View File

@ -54,6 +54,7 @@ typedef struct AudioCrossoverContext {
const AVClass *class; const AVClass *class;
char *splits_str; char *splits_str;
char *gains_str;
int order_opt; int order_opt;
float level_in; float level_in;
@ -64,6 +65,8 @@ typedef struct AudioCrossoverContext {
int nb_splits; int nb_splits;
float splits[MAX_SPLITS]; float splits[MAX_SPLITS];
float gains[MAX_BANDS];
BiquadCoeffs lp[MAX_BANDS][20]; BiquadCoeffs lp[MAX_BANDS][20];
BiquadCoeffs hp[MAX_BANDS][20]; BiquadCoeffs hp[MAX_BANDS][20];
BiquadCoeffs ap[MAX_BANDS][20]; BiquadCoeffs ap[MAX_BANDS][20];
@ -95,11 +98,47 @@ static const AVOption acrossover_options[] = {
{ "18th", "18th order (108 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, AF, "m" }, { "18th", "18th order (108 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, AF, "m" },
{ "20th", "20th order (120 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, AF, "m" }, { "20th", "20th order (120 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, AF, "m" },
{ "level", "set input gain", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF }, { "level", "set input gain", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
{ "gain", "set output bands gain", OFFSET(gains_str), AV_OPT_TYPE_STRING, {.str="1.f"}, 0, 0, AF },
{ NULL } { NULL }
}; };
AVFILTER_DEFINE_CLASS(acrossover); AVFILTER_DEFINE_CLASS(acrossover);
static int parse_gains(AVFilterContext *ctx)
{
AudioCrossoverContext *s = ctx->priv;
char *p, *arg, *saveptr = NULL;
int i, ret = 0;
saveptr = NULL;
p = s->gains_str;
for (i = 0; i < MAX_BANDS; i++) {
float gain;
char c[3] = { 0 };
if (!(arg = av_strtok(p, " |", &saveptr)))
break;
p = NULL;
if (av_sscanf(arg, "%f%2s", &gain, c) < 1) {
av_log(ctx, AV_LOG_ERROR, "Invalid syntax for gain[%d].\n", i);
ret = AVERROR(EINVAL);
break;
}
if (c[0] == 'd' && c[1] == 'B')
s->gains[i] = expf(gain * M_LN10 / 20.f);
else
s->gains[i] = gain;
}
for (; i < MAX_BANDS; i++)
s->gains[i] = 1.f;
return ret;
}
static av_cold int init(AVFilterContext *ctx) static av_cold int init(AVFilterContext *ctx)
{ {
AudioCrossoverContext *s = ctx->priv; AudioCrossoverContext *s = ctx->priv;
@ -138,6 +177,10 @@ static av_cold int init(AVFilterContext *ctx)
s->nb_splits = i; s->nb_splits = i;
ret = parse_gains(ctx);
if (ret < 0)
return ret;
for (i = 0; i <= s->nb_splits; i++) { for (i = 0; i <= s->nb_splits; i++) {
AVFilterPad pad = { 0 }; AVFilterPad pad = { 0 };
char *name; char *name;
@ -349,6 +392,7 @@ static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, i
const int end = (in->channels * (jobnr+1)) / nb_jobs; \ const int end = (in->channels * (jobnr+1)) / nb_jobs; \
const int nb_samples = in->nb_samples; \ const int nb_samples = in->nb_samples; \
const int nb_outs = ctx->nb_outputs; \ const int nb_outs = ctx->nb_outputs; \
const int first_order = s->first_order; \
\ \
for (int ch = start; ch < end; ch++) { \ for (int ch = start; ch < end; ch++) { \
const type *src = (const type *)in->extended_data[ch]; \ const type *src = (const type *)in->extended_data[ch]; \
@ -378,7 +422,7 @@ static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, i
} \ } \
\ \
for (int aband = band + 1; aband + 1 < nb_outs; aband++) { \ for (int aband = band + 1; aband + 1 < nb_outs; aband++) { \
if (s->first_order) { \ if (first_order) { \
const type *asrc = (const type *)frames[band]->extended_data[ch]; \ const type *asrc = (const type *)frames[band]->extended_data[ch]; \
type *dst = (type *)frames[band]->extended_data[ch]; \ type *dst = (type *)frames[band]->extended_data[ch]; \
type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20; \ type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20; \
@ -387,7 +431,7 @@ static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, i
biquad_process_## name(apc, ap, dst, asrc, nb_samples); \ biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
} \ } \
\ \
for (int f = s->first_order; f < s->ap_filter_count; f++) { \ for (int f = first_order; f < s->ap_filter_count; f++) { \
const type *asrc = (const type *)frames[band]->extended_data[ch]; \ const type *asrc = (const type *)frames[band]->extended_data[ch]; \
type *dst = (type *)frames[band]->extended_data[ch]; \ type *dst = (type *)frames[band]->extended_data[ch]; \
type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20 + f * 2;\ type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20 + f * 2;\
@ -398,12 +442,12 @@ static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, i
} \ } \
} \ } \
\ \
for (int band = 0; band < nb_outs && s->first_order; band++) { \ for (int band = 0; band < nb_outs; band++) { \
if (band & 1) { \ const type gain = s->gains[band] * ((band & 1 && first_order) ? -one : one); \
type *dst = (type *)frames[band]->extended_data[ch]; \ type *dst = (type *)frames[band]->extended_data[ch]; \
s->fdsp->vector_## ff ##mul_scalar(dst, dst, -one, \ \
FFALIGN(nb_samples, sizeof(type))); \ s->fdsp->vector_## ff ##mul_scalar(dst, dst, gain, \
} \ FFALIGN(nb_samples, sizeof(type))); \
} \ } \
} \ } \
\ \