Signed-off-by: Paul B Mahol <one...@gmail.com> --- doc/filters.texi | 49 ++++ libavfilter/Makefile | 1 + libavfilter/af_anequalizer.c | 534 +++++++++++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 4 files changed, 585 insertions(+) create mode 100644 libavfilter/af_anequalizer.c
diff --git a/doc/filters.texi b/doc/filters.texi index a55cad4..49921e2 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -992,6 +992,55 @@ stream ends. The default value is 2 seconds. @end table +@section anequalizer + +Parametric equalizer with unlimited number of bands for each channel. + +It accepts single option string in format: +"c=@var{chn} f=@var{cf} w=@var{w} g=@var{g} t=@var{f} | ..." +Each equalization filter is separated by '|'. + +@table @option +@item chn +Set channel number to which equalization will be applied. +If input doesn't have that channel the entry is ignored. + +@item cf +Set central frequency for band. +If input doesn't have that frequency the entry is ignored. + +@item w +Set band width in hertz + +@item g +Set gain in dB + +@item f +Set filter type, can be: + +@table @samp +@item 0 +Butterworth. + +@item 1 +Chebyshev type 1. + +@item 2 +Chebyshev type 2. +@end table +@end table + +@subsection Examples + +@itemize +@item +Lower gain by 10 of central frequency 200Hz and width 100 Hz +for first 2 channels using Chebyshev type 1 filter: +@example +anequalizer=args=c=0 f=200 w=100 g=-10 t=1|c=0 f=200 w=100 g=-10 t=1 +@end example +@end itemize + @section anull Pass the audio source unchanged to the output. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index dea012a..adbbc39 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -29,6 +29,7 @@ OBJS-$(CONFIG_ACROSSFADE_FILTER) += af_afade.o OBJS-$(CONFIG_ADELAY_FILTER) += af_adelay.o OBJS-$(CONFIG_AECHO_FILTER) += af_aecho.o OBJS-$(CONFIG_AEMPHASIS_FILTER) += af_aemphasis.o +OBJS-$(CONFIG_ANEQUALIZER_FILTER) += af_anequalizer.o OBJS-$(CONFIG_AEVAL_FILTER) += aeval.o OBJS-$(CONFIG_AFADE_FILTER) += af_afade.o OBJS-$(CONFIG_AFORMAT_FILTER) += af_aformat.o diff --git a/libavfilter/af_anequalizer.c b/libavfilter/af_anequalizer.c new file mode 100644 index 0000000..31f4134 --- /dev/null +++ b/libavfilter/af_anequalizer.c @@ -0,0 +1,534 @@ +/* + * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others + * Copyright (c) 2015 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/avstring.h" +#include "libavutil/opt.h" +#include "avfilter.h" +#include "internal.h" +#include "audio.h" + +enum FilterType { + BUTTERWORTH, + CHEBYSHEV1, + CHEBYSHEV2, + NB_TYPES +}; + +typedef struct FoSection { + double a0, a1, a2, a3, a4; + double b0, b1, b2, b3, b4; + + double num[4]; + double denum[4]; +} FoSection; + +typedef struct EqualizatorFilter { + int ignore; + int channel; + int type; + + double freq; + double gain; + double width; + + FoSection section[2]; +} EqualizatorFilter; + +typedef struct AudioNEqualizerContext { + const AVClass *class; + char *args; + + int nb_filters; + int nb_allocated; + EqualizatorFilter *filters; +} AudioNEqualizerContext; + +#define OFFSET(x) offsetof(AudioNEqualizerContext, x) +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption anequalizer_options[] = { + { "args", NULL, OFFSET(args), AV_OPT_TYPE_STRING, {.str=""}, 0, 0, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(anequalizer); + +static int query_formats(AVFilterContext *ctx) +{ + AVFilterFormats *formats; + AVFilterChannelLayouts *layouts; + static const enum AVSampleFormat sample_fmts[] = { + AV_SAMPLE_FMT_DBLP, + AV_SAMPLE_FMT_NONE + }; + int ret; + + layouts = ff_all_channel_counts(); + if (!layouts) + return AVERROR(ENOMEM); + ret = ff_set_common_channel_layouts(ctx, layouts); + if (ret < 0) + return ret; + + formats = ff_make_format_list(sample_fmts); + if (!formats) + return AVERROR(ENOMEM); + + ret = ff_set_common_formats(ctx, formats); + if (ret < 0) + return ret; + + formats = ff_all_samplerates(); + if (!formats) + return AVERROR(ENOMEM); + return ff_set_common_samplerates(ctx, formats); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + AudioNEqualizerContext *s = ctx->priv; + + av_freep(&s->filters); + s->nb_filters = 0; + s->nb_allocated = 0; +} + +static void butterworth_fo_section(FoSection *S, double beta, + double s, double g, double g0, + double D, double c0) +{ + S->b0 = (g*g*beta*beta + 2*g*g0*s*beta + g0*g0)/D; + S->b1 = -4*c0*(g0*g0 + g*g0*s*beta)/D; + S->b2 = 2*(g0*g0*(1 + 2*c0*c0) - g*g*beta*beta)/D; + S->b3 = -4*c0*(g0*g0 - g*g0*s*beta)/D; + S->b4 = (g*g*beta*beta - 2*g*g0*s*beta + g0*g0)/D; + + S->a0 = 1; + S->a1 = -4*c0*(1 + s*beta)/D; + S->a2 = 2*(1 + 2*c0*c0 - beta*beta)/D; + S->a3 = -4*c0*(1 - s*beta)/D; + S->a4 = (beta*beta - 2*s*beta + 1)/D; +} + +static void butterworth_bp_filter(EqualizatorFilter *f, + int N, double w0, double wb, + double G, double Gb, double G0) +{ + double g, c0, g0, beta; + double epsilon; + int r = N % 2; + int L = (N - r) / 2; + int i; + + if (G == 0 && G0 == 0) { + f->section[0].a0 = 1; + f->section[0].b0 = 1; + f->section[1].a0 = 1; + f->section[1].b0 = 1; + return; + } + + G = pow(10, G/20); + Gb = pow(10, Gb/20); + G0 = pow(10, G0/20); + + epsilon = sqrt((G * G - Gb * Gb) / (Gb * Gb - G0 * G0)); + g = pow(G, 1.0 / (double)N); + g0 = pow(G0, 1.0 / (double)N); + beta = pow(epsilon, -1.0/(double)N) * tan(wb / 2.0); + + c0 = cos(w0); + if (w0 == 0) + c0 = 1; + if (w0 == M_PI/2) + c0 = 0; + if (w0 == M_PI) + c0 = -1; + + for (i = 1; i <= L; i++) { + double ui = (2.0 * i - 1) / N; + double si = sin(M_PI * ui / 2.0); + double Di = beta * beta + 2 * si * beta + 1; + + butterworth_fo_section(&f->section[i - 1], beta, si, g, g0, Di, c0); + } +} + +static void chebyshev1_fo_section(FoSection *S, double a, + double c, double tetta_b, + double g0, double s, double b, + double D, double c0) +{ + S->b0 = ((b*b + g0*g0*c*c)*tetta_b*tetta_b + 2*g0*b*s*tetta_b + g0*g0)/D; + S->b1 = -4*c0*(g0*g0 + g0*b*s*tetta_b)/D; + S->b2 = 2*(g0*g0*(1 + 2*c0*c0) - (b*b + g0*g0*c*c)*tetta_b*tetta_b)/D; + S->b3 = -4*c0*(g0*g0 - g0*b*s*tetta_b)/D; + S->b4 = ((b*b + g0*g0*c*c)*tetta_b*tetta_b - 2*g0*b*s*tetta_b + g0*g0)/D; + + S->a0 = 1; + S->a1 = -4*c0*(1 + a*s*tetta_b)/D; + S->a2 = 2*(1 + 2*c0*c0 - (a*a + c*c)*tetta_b*tetta_b)/D; + S->a3 = -4*c0*(1 - a*s*tetta_b)/D; + S->a4 = ((a*a + c*c)*tetta_b*tetta_b - 2*a*s*tetta_b + 1)/D; +} + +static void chebyshev1_bp_filter(EqualizatorFilter *f, + int N, double w0, double wb, + double G, double Gb, double G0) +{ + double a, b, c0, g0, alfa, beta, tetta_b; + double epsilon; + int r = N % 2; + int L = (N - r) / 2; + int i; + + if (G == 0 && G0 == 0) { + f->section[0].a0 = 1; + f->section[0].b0 = 1; + f->section[1].a0 = 1; + f->section[1].b0 = 1; + return; + } + + G = pow(10, G/20); + Gb = pow(10, Gb/20); + G0 = pow(10, G0/20); + + epsilon = sqrt((G*G - Gb*Gb) / (Gb*Gb - G0*G0)); + g0 = pow(G0,1.0/N); + alfa = pow(1.0/epsilon + sqrt(1 + pow(epsilon,-2.0)), 1.0/N); + beta = pow(G/epsilon + Gb * sqrt(1 + pow(epsilon,-2.0)), 1.0/N); + a = 0.5 * (alfa - 1.0/alfa); + b = 0.5 * (beta - g0*g0*(1/beta)); + tetta_b = tan(wb/2); + + c0 = cos(w0); + if (w0 == 0) + c0 = 1; + if (w0 == M_PI/2) + c0 = 0; + if (w0 == M_PI) + c0 = -1; + + for (i = 1; i <= L; i++) { + double ui = (2.0*i-1.0)/N; + double ci = cos(M_PI*ui/2.0); + double si = sin(M_PI*ui/2.0); + double Di = (a*a + ci*ci)*tetta_b*tetta_b + 2.0*a*si*tetta_b + 1; + + chebyshev1_fo_section(&f->section[i -1], a, ci, tetta_b, g0, si, b, Di, c0); + } +} + +static void chebyshev2_fo_section(FoSection *S, double a, + double c, double tetta_b, + double g, double s, double b, + double D, double c0) +{ + S->b0 = (g*g*tetta_b*tetta_b + 2*g*b*s*tetta_b + b*b + g*g*c*c)/D; + S->b1 = -4*c0*(b*b + g*g*c*c + g*b*s*tetta_b)/D; + S->b2 = 2*((b*b + g*g*c*c)*(1 + 2*c0*c0) - g*g*tetta_b*tetta_b)/D; + S->b3 = -4*c0*(b*b + g*g*c*c - g*b*s*tetta_b)/D; + S->b4 = (g*g*tetta_b*tetta_b - 2*g*b*s*tetta_b + b*b + g*g*c*c)/D; + + S->a0 = 1; + S->a1 = -4*c0*(a*a + c*c + a*s*tetta_b)/D; + S->a2 = 2*((a*a + c*c)*(1 + 2*c0*c0) - tetta_b*tetta_b)/D; + S->a3 = -4*c0*(a*a + c*c - a*s*tetta_b)/D; + S->a4 = (tetta_b*tetta_b - 2*a*s*tetta_b + a*a + c*c)/D; +} + +static void chebyshev2_bp_filter(EqualizatorFilter *f, + int N, double w0, double wb, + double G, double Gb, double G0) +{ + double a, b, c0, tetta_b; + double epsilon, g, eu, ew; + int r = N % 2; + int L = (N - r) / 2; + int i; + + if (G == 0 && G0 == 0) { + f->section[0].a0 = 1; + f->section[0].b0 = 1; + f->section[1].a0 = 1; + f->section[1].b0 = 1; + return; + } + + G = pow(10, G/20); + Gb = pow(10, Gb/20); + G0 = pow(10, G0/20); + + epsilon = sqrt((G*G - Gb*Gb) / (Gb*Gb - G0*G0)); + g = pow(G, 1.0 / N); + eu = pow(epsilon + sqrt(1 + epsilon*epsilon), 1.0/N); + ew = pow(G0*epsilon + Gb*sqrt(1 + epsilon*epsilon), 1.0/N); + a = (eu - 1.0/eu)/2.0; + b = (ew - g*g/ew)/2.0; + tetta_b = tan(wb/2); + + c0 = cos(w0); + if (w0 == 0) + c0 = 1; + if (w0 == M_PI/2) + c0 = 0; + if (w0 == M_PI) + c0 = -1; + + for (i = 1; i <= L; i++) { + double ui = (2.0 * i - 1.0)/N; + double ci = cos(M_PI * ui / 2.0); + double si = sin(M_PI * ui / 2.0); + double Di = tetta_b*tetta_b + 2*a*si*tetta_b + a*a + ci*ci; + + chebyshev2_fo_section(&f->section[i - 1], a, ci, tetta_b, g, si, b, Di, c0); + } +} + +static double butterworth_compute_bw_gain_db(double gain) +{ + double bw_gain = 0; + + if (gain <= -6) + bw_gain = gain + 3; + else if(gain > -6 && gain < 6) + bw_gain = gain * 0.5; + else if(gain >= 6) + bw_gain = gain - 3; + + return bw_gain; +} + +static double chebyshev1_compute_bw_gain_db(double gain) +{ + double bw_gain = 0; + + if (gain <= -6) + bw_gain = gain + 1; + else if(gain > -6 && gain < 6) + bw_gain = gain * 0.9; + else if(gain >= 6) + bw_gain = gain - 1; + + return bw_gain; +} + +static double chebyshev2_compute_bw_gain_db(double gain) +{ + double bw_gain = 0; + + if (gain <= -6) + bw_gain = -3; + else if(gain > -6 && gain < 6) + bw_gain = gain * 0.3; + else if(gain >= 6) + bw_gain = 3; + + return bw_gain; +} + +static inline double hz_2_rad(double x, double fs) +{ + return 2 * M_PI * x / fs; +} + +static void equalizer(EqualizatorFilter *f, double sample_rate) +{ + double w0 = hz_2_rad(f->freq, sample_rate); + double wb = hz_2_rad(f->width, sample_rate); + double bw_gain; + + switch (f->type) { + case BUTTERWORTH: + bw_gain = butterworth_compute_bw_gain_db(f->gain); + butterworth_bp_filter(f, 4, w0, wb, f->gain, bw_gain, 0); + break; + case CHEBYSHEV1: + bw_gain = chebyshev1_compute_bw_gain_db(f->gain); + chebyshev1_bp_filter(f, 4, w0, wb, f->gain, bw_gain, 0); + break; + case CHEBYSHEV2: + bw_gain = chebyshev2_compute_bw_gain_db(f->gain); + chebyshev2_bp_filter(f, 4, w0, wb, f->gain, bw_gain, 0); + break; + } + +} + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + AudioNEqualizerContext *s = ctx->priv; + char *args = av_strdup(s->args); + char *saveptr = NULL; + + if (!args) + return AVERROR(ENOMEM); + + s->nb_allocated = 32 * inlink->channels; + s->filters = av_calloc(inlink->channels, 32 * sizeof(*s->filters)); + if (!s->filters) { + s->nb_allocated = 0; + return AVERROR(ENOMEM); + } + + while (1) { + char *arg = av_strtok(s->nb_filters == 0 ? args : NULL, "|", &saveptr); + + if (!arg) + break; + + s->filters[s->nb_filters].type = 0; + if (sscanf(arg, "c=%d f=%lf w=%lf g=%lf t=%d", &s->filters[s->nb_filters].channel, + &s->filters[s->nb_filters].freq, + &s->filters[s->nb_filters].width, + &s->filters[s->nb_filters].gain, + &s->filters[s->nb_filters].type) != 5 && + sscanf(arg, "c=%d f=%lf w=%lf g=%lf", &s->filters[s->nb_filters].channel, + &s->filters[s->nb_filters].freq, + &s->filters[s->nb_filters].width, + &s->filters[s->nb_filters].gain) != 4 ) { + av_free(args); + return AVERROR(EINVAL); + } + + if (s->filters[s->nb_filters].freq < 0 || + s->filters[s->nb_filters].freq >= inlink->sample_rate / 2) + s->filters[s->nb_filters].ignore = 1; + + if (s->filters[s->nb_filters].channel < 0 || + s->filters[s->nb_filters].channel >= inlink->channels) + s->filters[s->nb_filters].ignore = 1; + + av_clip(s->filters[s->nb_filters].type, 0, NB_TYPES - 1); + equalizer(&s->filters[s->nb_filters], inlink->sample_rate); + s->nb_filters++; + if (s->nb_filters >= s->nb_allocated) { + EqualizatorFilter *filters; + + filters = av_calloc(s->nb_allocated, 2 * sizeof(*s->filters)); + if (!filters) { + av_free(args); + return AVERROR(ENOMEM); + } + memcpy(filters, s->filters, sizeof(*s->filters) * s->nb_allocated); + av_free(s->filters); + s->filters = filters; + s->nb_allocated *= 2; + } + } + + av_free(args); + + return 0; +} + +static inline double section_process(FoSection *S, double in) +{ + double out; + + out = S->b0 * in; + out+= S->b1 * S->num[0] - S->denum[0] * S->a1; + out+= S->b2 * S->num[1] - S->denum[1] * S->a2; + out+= S->b3 * S->num[2] - S->denum[2] * S->a3; + out+= S->b4 * S->num[3] - S->denum[3] * S->a4; + + S->num[3] = S->num[2]; + S->num[2] = S->num[1]; + S->num[1] = S->num[0]; + S->num[0] = in; + + S->denum[3] = S->denum[2]; + S->denum[2] = S->denum[1]; + S->denum[1] = S->denum[0]; + S->denum[0] = out; + + return out; +} + +static double process_sample(FoSection *s1, FoSection *s2, double in) +{ + double p0 = in, p1; + + p1 = section_process(s1, p0); + p1 = section_process(s2, p1); + + return p1; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *buf) +{ + AVFilterContext *ctx = inlink->dst; + AudioNEqualizerContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + double *bptr; + int i, n; + + for (i = 0; i < s->nb_filters; i++) { + EqualizatorFilter *f = &s->filters[i]; + + if (f->gain == 0. || f->ignore) + continue; + + bptr = (double *)buf->extended_data[f->channel]; + for (n = 0; n < buf->nb_samples; n++) { + double sample = bptr[n]; + + sample = process_sample(&f->section[0], + &f->section[1], + sample); + bptr[n] = sample; + } + } + + return ff_filter_frame(outlink, buf); +} + +static const AVFilterPad inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .config_props = config_input, + .filter_frame = filter_frame, + .needs_writable = 1, + }, + { NULL } +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + }, + { NULL } +}; + +AVFilter ff_af_anequalizer = { + .name = "anequalizer", + .description = NULL_IF_CONFIG_SMALL("Apply audio parametric N band equalizer."), + .priv_size = sizeof(AudioNEqualizerContext), + .priv_class = &anequalizer_class, + .uninit = uninit, + .query_formats = query_formats, + .inputs = inputs, + .outputs = outputs, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 131e067..a039a39 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -59,6 +59,7 @@ void avfilter_register_all(void) REGISTER_FILTER(ALLPASS, allpass, af); REGISTER_FILTER(AMERGE, amerge, af); REGISTER_FILTER(AMIX, amix, af); + REGISTER_FILTER(ANEQUALIZER, anequalizer, af); REGISTER_FILTER(ANULL, anull, af); REGISTER_FILTER(APAD, apad, af); REGISTER_FILTER(APERMS, aperms, af); -- 1.9.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel