/* * Copyright (c) 2016 Muhammad Faiz <mfcc64@gmail.com> * * 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/channel_layout.h"#include "libavutil/file_open.h"#include "libavutil/mem.h"#include "libavutil/opt.h"#include "libavutil/eval.h"#include "libavutil/avassert.h"#include "libavutil/tx.h"#include "avfilter.h"#include "filters.h"#include "audio.h" #define RDFT_BITS_MIN 4#define RDFT_BITS_MAX 16 enum WindowFunc { WFUNC_RECTANGULAR, WFUNC_HANN, WFUNC_HAMMING, WFUNC_BLACKMAN, WFUNC_NUTTALL3, WFUNC_MNUTTALL3, WFUNC_NUTTALL, WFUNC_BNUTTALL, WFUNC_BHARRIS, WFUNC_TUKEY, NB_WFUNC}; enum Scale { SCALE_LINLIN, SCALE_LINLOG, SCALE_LOGLIN, SCALE_LOGLOG, NB_SCALE}; #define NB_GAIN_ENTRY_MAX 4096typedef struct GainEntry { double freq; double gain;} GainEntry; typedef struct OverlapIndex { int buf_idx; int overlap_idx;} OverlapIndex; typedef struct FIREqualizerContext { const AVClass *class; AVTXContext *analysis_rdft; av_tx_fn analysis_rdft_fn; AVTXContext *analysis_irdft; av_tx_fn analysis_irdft_fn; AVTXContext *rdft; av_tx_fn rdft_fn; AVTXContext *irdft; av_tx_fn irdft_fn; AVTXContext *fft_ctx; av_tx_fn fft_fn; AVTXContext *cepstrum_rdft; av_tx_fn cepstrum_rdft_fn; AVTXContext *cepstrum_irdft; av_tx_fn cepstrum_irdft_fn; int analysis_rdft_len; int rdft_len; int cepstrum_len; float *analysis_buf; float *analysis_tbuf; float *dump_buf; float *kernel_tmp_buf; float *kernel_tmp_tbuf; float *kernel_buf; float *tx_buf; float *cepstrum_buf; float *cepstrum_tbuf; float *conv_buf; OverlapIndex *conv_idx; int fir_len; int nsamples_max; int64_t next_pts; int frame_nsamples_max; int remaining; char *gain_cmd; char *gain_entry_cmd; const char *gain; const char *gain_entry; double delay; double accuracy; int wfunc; int fixed; int multi; int zero_phase; int scale; char *dumpfile; int dumpscale; int fft2; int min_phase; int nb_gain_entry; int gain_entry_err; GainEntry gain_entry_tbl[NB_GAIN_ENTRY_MAX];} FIREqualizerContext; #define OFFSET(x) offsetof(FIREqualizerContext, x)#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM#define TFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM static const AVOption firequalizer_options[] = { { "gain", "set gain curve", OFFSET(gain), AV_OPT_TYPE_STRING, { .str = "gain_interpolate(f)" }, 0, 0, TFLAGS }, { "gain_entry", "set gain entry", OFFSET(gain_entry), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, TFLAGS }, { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, { .dbl = 0.01 }, 0.0, 1e10, FLAGS }, { "accuracy", "set accuracy", OFFSET(accuracy), AV_OPT_TYPE_DOUBLE, { .dbl = 5.0 }, 0.0, 1e10, FLAGS }, { "wfunc", "set window function", OFFSET(wfunc), AV_OPT_TYPE_INT, { .i64 = WFUNC_HANN }, 0, NB_WFUNC-1, FLAGS, .unit = "wfunc" }, { "rectangular", "rectangular window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_RECTANGULAR }, 0, 0, FLAGS, .unit = "wfunc" }, { "hann", "hann window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HANN }, 0, 0, FLAGS, .unit = "wfunc" }, { "hamming", "hamming window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HAMMING }, 0, 0, FLAGS, .unit = "wfunc" }, { "blackman", "blackman window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BLACKMAN }, 0, 0, FLAGS, .unit = "wfunc" }, { "nuttall3", "3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL3 }, 0, 0, FLAGS, .unit = "wfunc" }, { "mnuttall3", "minimum 3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_MNUTTALL3 }, 0, 0, FLAGS, .unit = "wfunc" }, { "nuttall", "nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL }, 0, 0, FLAGS, .unit = "wfunc" }, { "bnuttall", "blackman-nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BNUTTALL }, 0, 0, FLAGS, .unit = "wfunc" }, { "bharris", "blackman-harris window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BHARRIS }, 0, 0, FLAGS, .unit = "wfunc" }, { "tukey", "tukey window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_TUKEY }, 0, 0, FLAGS, .unit = "wfunc" }, { "fixed", "set fixed frame samples", OFFSET(fixed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { "multi", "set multi channels mode", OFFSET(multi), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { "zero_phase", "set zero phase mode", OFFSET(zero_phase), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { "scale", "set gain scale", OFFSET(scale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, .unit = "scale" }, { "linlin", "linear-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLIN }, 0, 0, FLAGS, .unit = "scale" }, { "linlog", "linear-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLOG }, 0, 0, FLAGS, .unit = "scale" }, { "loglin", "logarithmic-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLIN }, 0, 0, FLAGS, .unit = "scale" }, { "loglog", "logarithmic-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLOG }, 0, 0, FLAGS, .unit = "scale" }, { "dumpfile", "set dump file", OFFSET(dumpfile), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS }, { "dumpscale", "set dump scale", OFFSET(dumpscale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, .unit = "scale" }, { "fft2", "set 2-channels fft", OFFSET(fft2), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { "min_phase", "set minimum phase mode", OFFSET(min_phase), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { NULL }}; AVFILTER_DEFINE_CLASS(firequalizer); static void common_uninit(FIREqualizerContext *s){ av_tx_uninit(&s->analysis_rdft); av_tx_uninit(&s->analysis_irdft); av_tx_uninit(&s->rdft); av_tx_uninit(&s->irdft); av_tx_uninit(&s->fft_ctx); av_tx_uninit(&s->cepstrum_rdft); av_tx_uninit(&s->cepstrum_irdft); s->analysis_rdft = s->analysis_irdft = s->rdft = s->irdft = NULL; s->fft_ctx = NULL; s->cepstrum_rdft = NULL; s->cepstrum_irdft = NULL; av_freep(&s->analysis_buf); av_freep(&s->analysis_tbuf); av_freep(&s->dump_buf); av_freep(&s->kernel_tmp_buf); av_freep(&s->kernel_tmp_tbuf); av_freep(&s->kernel_buf); av_freep(&s->tx_buf); av_freep(&s->cepstrum_buf); av_freep(&s->cepstrum_tbuf); av_freep(&s->conv_buf); av_freep(&s->conv_idx);} static av_cold void uninit(AVFilterContext *ctx){ FIREqualizerContext *s = ctx->priv; common_uninit(s); av_freep(&s->gain_cmd); av_freep(&s->gain_entry_cmd);} static void fast_convolute(FIREqualizerContext *restrict s, const float *restrict kernel_buf, float *restrict conv_buf, OverlapIndex *restrict idx, float *restrict data, int nsamples){ if (nsamples <= s->nsamples_max) { float *buf = conv_buf + idx->buf_idx * s->rdft_len; float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx; float *tbuf = s->tx_buf; int center = s->fir_len/2; int k; memset(buf, 0, center * sizeof(*data)); memcpy(buf + center, data, nsamples * sizeof(*data)); memset(buf + center + nsamples, 0, (s->rdft_len - nsamples - center) * sizeof(*data)); s->rdft_fn(s->rdft, tbuf, buf, sizeof(float)); for (k = 0; k <= s->rdft_len/2; k++) { tbuf[2*k] *= kernel_buf[k]; tbuf[2*k+1] *= kernel_buf[k]; } s->irdft_fn(s->irdft, buf, tbuf, sizeof(AVComplexFloat)); for (k = 0; k < s->rdft_len - idx->overlap_idx; k++) buf[k] += obuf[k]; memcpy(data, buf, nsamples * sizeof(*data)); idx->buf_idx = !idx->buf_idx; idx->overlap_idx = nsamples; } else { while (nsamples > s->nsamples_max * 2) { fast_convolute(s, kernel_buf, conv_buf, idx, data, s->nsamples_max); data += s->nsamples_max; nsamples -= s->nsamples_max; } fast_convolute(s, kernel_buf, conv_buf, idx, data, nsamples/2); fast_convolute(s, kernel_buf, conv_buf, idx, data + nsamples/2, nsamples - nsamples/2); }} static void fast_convolute_nonlinear(FIREqualizerContext *restrict s, const float *restrict kernel_buf, float *restrict conv_buf, OverlapIndex *restrict idx, float *restrict data, int nsamples){ if (nsamples <= s->nsamples_max) { float *buf = conv_buf + idx->buf_idx * s->rdft_len; float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx; float *tbuf = s->tx_buf; int k; memcpy(buf, data, nsamples * sizeof(*data)); memset(buf + nsamples, 0, (s->rdft_len - nsamples) * sizeof(*data)); s->rdft_fn(s->rdft, tbuf, buf, sizeof(float)); for (k = 0; k < s->rdft_len + 2; k += 2) { float re, im; re = tbuf[k] * kernel_buf[k] - tbuf[k+1] * kernel_buf[k+1]; im = tbuf[k] * kernel_buf[k+1] + tbuf[k+1] * kernel_buf[k]; tbuf[k] = re; tbuf[k+1] = im; } s->irdft_fn(s->irdft, buf, tbuf, sizeof(AVComplexFloat)); for (k = 0; k < s->rdft_len - idx->overlap_idx; k++) buf[k] += obuf[k]; memcpy(data, buf, nsamples * sizeof(*data)); idx->buf_idx = !idx->buf_idx; idx->overlap_idx = nsamples; } else { while (nsamples > s->nsamples_max * 2) { fast_convolute_nonlinear(s, kernel_buf, conv_buf, idx, data, s->nsamples_max); data += s->nsamples_max; nsamples -= s->nsamples_max; } fast_convolute_nonlinear(s, kernel_buf, conv_buf, idx, data, nsamples/2); fast_convolute_nonlinear(s, kernel_buf, conv_buf, idx, data + nsamples/2, nsamples - nsamples/2); }} static void fast_convolute2(FIREqualizerContext *restrict s, const float *restrict kernel_buf, AVComplexFloat *restrict conv_buf, OverlapIndex *restrict idx, float *restrict data0, float *restrict data1, int nsamples){ if (nsamples <= s->nsamples_max) { AVComplexFloat *buf = conv_buf + idx->buf_idx * s->rdft_len; AVComplexFloat *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx; AVComplexFloat *tbuf = (AVComplexFloat *)s->tx_buf; int center = s->fir_len/2; int k; float tmp; memset(buf, 0, center * sizeof(*buf)); for (k = 0; k < nsamples; k++) { buf[center+k].re = data0[k]; buf[center+k].im = data1[k]; } memset(buf + center + nsamples, 0, (s->rdft_len - nsamples - center) * sizeof(*buf)); s->fft_fn(s->fft_ctx, tbuf, buf, sizeof(AVComplexFloat)); /* swap re <-> im, do backward fft using forward fft_ctx */ /* normalize with 0.5f */ tmp = tbuf[0].re; tbuf[0].re = 0.5f * kernel_buf[0] * tbuf[0].im; tbuf[0].im = 0.5f * kernel_buf[0] * tmp; for (k = 1; k < s->rdft_len/2; k++) { int m = s->rdft_len - k; tmp = tbuf[k].re; tbuf[k].re = 0.5f * kernel_buf[k] * tbuf[k].im; tbuf[k].im = 0.5f * kernel_buf[k] * tmp; tmp = tbuf[m].re; tbuf[m].re = 0.5f * kernel_buf[k] * tbuf[m].im; tbuf[m].im = 0.5f * kernel_buf[k] * tmp; } tmp = tbuf[k].re; tbuf[k].re = 0.5f * kernel_buf[k] * tbuf[k].im; tbuf[k].im = 0.5f * kernel_buf[k] * tmp; s->fft_fn(s->fft_ctx, buf, tbuf, sizeof(AVComplexFloat)); for (k = 0; k < s->rdft_len - idx->overlap_idx; k++) { buf[k].re += obuf[k].re; buf[k].im += obuf[k].im; } /* swapped re <-> im */ for (k = 0; k < nsamples; k++) { data0[k] = buf[k].im; data1[k] = buf[k].re; } idx->buf_idx = !idx->buf_idx; idx->overlap_idx = nsamples; } else { while (nsamples > s->nsamples_max * 2) { fast_convolute2(s, kernel_buf, conv_buf, idx, data0, data1, s->nsamples_max); data0 += s->nsamples_max; data1 += s->nsamples_max; nsamples -= s->nsamples_max; } fast_convolute2(s, kernel_buf, conv_buf, idx, data0, data1, nsamples/2); fast_convolute2(s, kernel_buf, conv_buf, idx, data0 + nsamples/2, data1 + nsamples/2, nsamples - nsamples/2); }} static void dump_fir(AVFilterContext *ctx, FILE *fp, int ch){ FIREqualizerContext *s = ctx->priv; int rate = ctx->inputs[0]->sample_rate; int xlog = s->dumpscale == SCALE_LOGLIN || s->dumpscale == SCALE_LOGLOG; int ylog = s->dumpscale == SCALE_LINLOG || s->dumpscale == SCALE_LOGLOG; int x; int center = s->fir_len / 2; double delay = s->zero_phase ? 0.0 : (double) center / rate; double vx, ya, yb; if (!s->min_phase) { s->analysis_buf[0] *= s->rdft_len/2; for (x = 1; x <= center; x++) { s->analysis_buf[x] *= s->rdft_len/2; s->analysis_buf[s->analysis_rdft_len - x] *= s->rdft_len/2; } } else { for (x = 0; x < s->fir_len; x++) s->analysis_buf[x] *= s->rdft_len/2; } if (ch) fprintf(fp, "\n\n"); fprintf(fp, "# time[%d] (time amplitude)\n", ch); if (!s->min_phase) { for (x = center; x > 0; x--) fprintf(fp, "%15.10f %15.10f\n", delay - (double) x / rate, (double) s->analysis_buf[s->analysis_rdft_len - x]); for (x = 0; x <= center; x++) fprintf(fp, "%15.10f %15.10f\n", delay + (double)x / rate , (double) s->analysis_buf[x]); } else { for (x = 0; x < s->fir_len; x++) fprintf(fp, "%15.10f %15.10f\n", (double)x / rate, (double) s->analysis_buf[x]); } s->analysis_rdft_fn(s->analysis_rdft, s->analysis_tbuf, s->analysis_buf, sizeof(float)); fprintf(fp, "\n\n# freq[%d] (frequency desired_gain actual_gain)\n", ch); for (x = 0; x <= s->analysis_rdft_len/2; x++) { int i = 2 * x; vx = (double)x * rate / s->analysis_rdft_len; if (xlog) vx = log2(0.05*vx); ya = s->dump_buf[i]; yb = s->min_phase ? hypotf(s->analysis_tbuf[i], s->analysis_tbuf[i+1]) : s->analysis_tbuf[i]; if (s->min_phase) yb = fabs(yb); if (ylog) { ya = 20.0 * log10(fabs(ya)); yb = 20.0 * log10(fabs(yb)); } fprintf(fp, "%17.10f %17.10f %17.10f\n", vx, ya, yb); }} static double entry_func(void *p, double freq, double gain){ AVFilterContext *ctx = p; FIREqualizerContext *s = ctx->priv; if (s->nb_gain_entry >= NB_GAIN_ENTRY_MAX) { av_log(ctx, AV_LOG_ERROR, "entry table overflow.\n"); s->gain_entry_err = AVERROR(EINVAL); return 0; } if (isnan(freq)) { av_log(ctx, AV_LOG_ERROR, "nan frequency (%g, %g).\n", freq, gain); s->gain_entry_err = AVERROR(EINVAL); return 0; } if (s->nb_gain_entry > 0 && freq <= s->gain_entry_tbl[s->nb_gain_entry - 1].freq) { av_log(ctx, AV_LOG_ERROR, "unsorted frequency (%g, %g).\n", freq, gain); s->gain_entry_err = AVERROR(EINVAL); return 0; } s->gain_entry_tbl[s->nb_gain_entry].freq = freq; s->gain_entry_tbl[s->nb_gain_entry].gain = gain; s->nb_gain_entry++; return 0;} static int gain_entry_compare(const void *key, const void *memb){ const double *freq = key; const GainEntry *entry = memb; if (*freq < entry[0].freq) return -1; if (*freq > entry[1].freq) return 1; return 0;} static double gain_interpolate_func(void *p, double freq){ AVFilterContext *ctx = p; FIREqualizerContext *s = ctx->priv; GainEntry *res; double d0, d1, d; if (isnan(freq)) return freq; if (!s->nb_gain_entry) return 0; if (freq <= s->gain_entry_tbl[0].freq) return s->gain_entry_tbl[0].gain; if (freq >= s->gain_entry_tbl[s->nb_gain_entry-1].freq) return s->gain_entry_tbl[s->nb_gain_entry-1].gain; res = bsearch(&freq, &s->gain_entry_tbl, s->nb_gain_entry - 1, sizeof(*res), gain_entry_compare); av_assert0(res); d = res[1].freq - res[0].freq; d0 = freq - res[0].freq; d1 = res[1].freq - freq; if (d0 && d1) return (d0 * res[1].gain + d1 * res[0].gain) / d; if (d0) return res[1].gain; return res[0].gain;} static double cubic_interpolate_func(void *p, double freq){ AVFilterContext *ctx = p; FIREqualizerContext *s = ctx->priv; GainEntry *res; double x, x2, x3; double a, b, c, d; double m0, m1, m2, msum, unit; if (!s->nb_gain_entry) return 0; if (freq <= s->gain_entry_tbl[0].freq) return s->gain_entry_tbl[0].gain; if (freq >= s->gain_entry_tbl[s->nb_gain_entry-1].freq) return s->gain_entry_tbl[s->nb_gain_entry-1].gain; res = bsearch(&freq, &s->gain_entry_tbl, s->nb_gain_entry - 1, sizeof(*res), gain_entry_compare); av_assert0(res); unit = res[1].freq - res[0].freq; m0 = res != s->gain_entry_tbl ? unit * (res[0].gain - res[-1].gain) / (res[0].freq - res[-1].freq) : 0; m1 = res[1].gain - res[0].gain; m2 = res != s->gain_entry_tbl + s->nb_gain_entry - 2 ? unit * (res[2].gain - res[1].gain) / (res[2].freq - res[1].freq) : 0;Lines 1–500 of 968. Load more
/* * Copyright (c) 2016 Muhammad Faiz <mfcc64@gmail.com> * * 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/channel_layout.h" #include "libavutil/file_open.h" #include "libavutil/mem.h" #include "libavutil/opt.h" #include "libavutil/eval.h" #include "libavutil/avassert.h" #include "libavutil/tx.h" #include "avfilter.h" #include "filters.h" #include "audio.h" #define RDFT_BITS_MIN 4 #define RDFT_BITS_MAX 16 enum WindowFunc { WFUNC_RECTANGULAR, WFUNC_HANN, WFUNC_HAMMING, WFUNC_BLACKMAN, WFUNC_NUTTALL3, WFUNC_MNUTTALL3, WFUNC_NUTTALL, WFUNC_BNUTTALL, WFUNC_BHARRIS, WFUNC_TUKEY, NB_WFUNC }; enum Scale { SCALE_LINLIN, SCALE_LINLOG, SCALE_LOGLIN, SCALE_LOGLOG, NB_SCALE }; #define NB_GAIN_ENTRY_MAX 4096 typedef struct GainEntry { double freq; double gain; } GainEntry; typedef struct OverlapIndex { int buf_idx; int overlap_idx; } OverlapIndex; typedef struct FIREqualizerContext { const AVClass *class; AVTXContext *analysis_rdft; av_tx_fn analysis_rdft_fn; AVTXContext *analysis_irdft; av_tx_fn analysis_irdft_fn; AVTXContext *rdft; av_tx_fn rdft_fn; AVTXContext *irdft; av_tx_fn irdft_fn; AVTXContext *fft_ctx; av_tx_fn fft_fn; AVTXContext *cepstrum_rdft; av_tx_fn cepstrum_rdft_fn; AVTXContext *cepstrum_irdft; av_tx_fn cepstrum_irdft_fn; int analysis_rdft_len; int rdft_len; int cepstrum_len; float *analysis_buf; float *analysis_tbuf; float *dump_buf; float *kernel_tmp_buf; float *kernel_tmp_tbuf; float *kernel_buf; float *tx_buf; float *cepstrum_buf; float *cepstrum_tbuf; float *conv_buf; OverlapIndex *conv_idx; int fir_len; int nsamples_max; int64_t next_pts; int frame_nsamples_max; int remaining; char *gain_cmd; char *gain_entry_cmd; const char *gain; const char *gain_entry; double delay; double accuracy; int wfunc; int fixed; int multi; int zero_phase; int scale; char *dumpfile; int dumpscale; int fft2; int min_phase; int nb_gain_entry; int gain_entry_err; GainEntry gain_entry_tbl[NB_GAIN_ENTRY_MAX]; } FIREqualizerContext; #define OFFSET(x) offsetof(FIREqualizerContext, x) #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM static const AVOption firequalizer_options[] = { { "gain", "set gain curve", OFFSET(gain), AV_OPT_TYPE_STRING, { .str = "gain_interpolate(f)" }, 0, 0, TFLAGS }, { "gain_entry", "set gain entry", OFFSET(gain_entry), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, TFLAGS }, { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, { .dbl = 0.01 }, 0.0, 1e10, FLAGS }, { "accuracy", "set accuracy", OFFSET(accuracy), AV_OPT_TYPE_DOUBLE, { .dbl = 5.0 }, 0.0, 1e10, FLAGS }, { "wfunc", "set window function", OFFSET(wfunc), AV_OPT_TYPE_INT, { .i64 = WFUNC_HANN }, 0, NB_WFUNC-1, FLAGS, .unit = "wfunc" }, { "rectangular", "rectangular window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_RECTANGULAR }, 0, 0, FLAGS, .unit = "wfunc" }, { "hann", "hann window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HANN }, 0, 0, FLAGS, .unit = "wfunc" }, { "hamming", "hamming window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HAMMING }, 0, 0, FLAGS, .unit = "wfunc" }, { "blackman", "blackman window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BLACKMAN }, 0, 0, FLAGS, .unit = "wfunc" }, { "nuttall3", "3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL3 }, 0, 0, FLAGS, .unit = "wfunc" }, { "mnuttall3", "minimum 3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_MNUTTALL3 }, 0, 0, FLAGS, .unit = "wfunc" }, { "nuttall", "nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL }, 0, 0, FLAGS, .unit = "wfunc" }, { "bnuttall", "blackman-nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BNUTTALL }, 0, 0, FLAGS, .unit = "wfunc" }, { "bharris", "blackman-harris window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BHARRIS }, 0, 0, FLAGS, .unit = "wfunc" }, { "tukey", "tukey window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_TUKEY }, 0, 0, FLAGS, .unit = "wfunc" }, { "fixed", "set fixed frame samples", OFFSET(fixed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { "multi", "set multi channels mode", OFFSET(multi), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { "zero_phase", "set zero phase mode", OFFSET(zero_phase), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { "scale", "set gain scale", OFFSET(scale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, .unit = "scale" }, { "linlin", "linear-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLIN }, 0, 0, FLAGS, .unit = "scale" }, { "linlog", "linear-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLOG }, 0, 0, FLAGS, .unit = "scale" }, { "loglin", "logarithmic-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLIN }, 0, 0, FLAGS, .unit = "scale" }, { "loglog", "logarithmic-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLOG }, 0, 0, FLAGS, .unit = "scale" }, { "dumpfile", "set dump file", OFFSET(dumpfile), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS }, { "dumpscale", "set dump scale", OFFSET(dumpscale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, .unit = "scale" }, { "fft2", "set 2-channels fft", OFFSET(fft2), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { "min_phase", "set minimum phase mode", OFFSET(min_phase), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { NULL } }; AVFILTER_DEFINE_CLASS(firequalizer); static void common_uninit(FIREqualizerContext *s) { av_tx_uninit(&s->analysis_rdft); av_tx_uninit(&s->analysis_irdft); av_tx_uninit(&s->rdft); av_tx_uninit(&s->irdft); av_tx_uninit(&s->fft_ctx); av_tx_uninit(&s->cepstrum_rdft); av_tx_uninit(&s->cepstrum_irdft); s->analysis_rdft = s->analysis_irdft = s->rdft = s->irdft = NULL; s->fft_ctx = NULL; s->cepstrum_rdft = NULL; s->cepstrum_irdft = NULL; av_freep(&s->analysis_buf); av_freep(&s->analysis_tbuf); av_freep(&s->dump_buf); av_freep(&s->kernel_tmp_buf); av_freep(&s->kernel_tmp_tbuf); av_freep(&s->kernel_buf); av_freep(&s->tx_buf); av_freep(&s->cepstrum_buf); av_freep(&s->cepstrum_tbuf); av_freep(&s->conv_buf); av_freep(&s->conv_idx); } static av_cold void uninit(AVFilterContext *ctx) { FIREqualizerContext *s = ctx->priv; common_uninit(s); av_freep(&s->gain_cmd); av_freep(&s->gain_entry_cmd); } static void fast_convolute(FIREqualizerContext *restrict s, const float *restrict kernel_buf, float *restrict conv_buf, OverlapIndex *restrict idx, float *restrict data, int nsamples) { if (nsamples <= s->nsamples_max) { float *buf = conv_buf + idx->buf_idx * s->rdft_len; float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx; float *tbuf = s->tx_buf; int center = s->fir_len/2; int k; memset(buf, 0, center * sizeof(*data)); memcpy(buf + center, data, nsamples * sizeof(*data)); memset(buf + center + nsamples, 0, (s->rdft_len - nsamples - center) * sizeof(*data)); s->rdft_fn(s->rdft, tbuf, buf, sizeof(float)); for (k = 0; k <= s->rdft_len/2; k++) { tbuf[2*k] *= kernel_buf[k]; tbuf[2*k+1] *= kernel_buf[k]; } s->irdft_fn(s->irdft, buf, tbuf, sizeof(AVComplexFloat)); for (k = 0; k < s->rdft_len - idx->overlap_idx; k++) buf[k] += obuf[k]; memcpy(data, buf, nsamples * sizeof(*data)); idx->buf_idx = !idx->buf_idx; idx->overlap_idx = nsamples; } else { while (nsamples > s->nsamples_max * 2) { fast_convolute(s, kernel_buf, conv_buf, idx, data, s->nsamples_max); data += s->nsamples_max; nsamples -= s->nsamples_max; } fast_convolute(s, kernel_buf, conv_buf, idx, data, nsamples/2); fast_convolute(s, kernel_buf, conv_buf, idx, data + nsamples/2, nsamples - nsamples/2); } } static void fast_convolute_nonlinear(FIREqualizerContext *restrict s, const float *restrict kernel_buf, float *restrict conv_buf, OverlapIndex *restrict idx, float *restrict data, int nsamples) { if (nsamples <= s->nsamples_max) { float *buf = conv_buf + idx->buf_idx * s->rdft_len; float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx; float *tbuf = s->tx_buf; int k; memcpy(buf, data, nsamples * sizeof(*data)); memset(buf + nsamples, 0, (s->rdft_len - nsamples) * sizeof(*data)); s->rdft_fn(s->rdft, tbuf, buf, sizeof(float)); for (k = 0; k < s->rdft_len + 2; k += 2) { float re, im; re = tbuf[k] * kernel_buf[k] - tbuf[k+1] * kernel_buf[k+1]; im = tbuf[k] * kernel_buf[k+1] + tbuf[k+1] * kernel_buf[k]; tbuf[k] = re; tbuf[k+1] = im; } s->irdft_fn(s->irdft, buf, tbuf, sizeof(AVComplexFloat)); for (k = 0; k < s->rdft_len - idx->overlap_idx; k++) buf[k] += obuf[k]; memcpy(data, buf, nsamples * sizeof(*data)); idx->buf_idx = !idx->buf_idx; idx->overlap_idx = nsamples; } else { while (nsamples > s->nsamples_max * 2) { fast_convolute_nonlinear(s, kernel_buf, conv_buf, idx, data, s->nsamples_max); data += s->nsamples_max; nsamples -= s->nsamples_max; } fast_convolute_nonlinear(s, kernel_buf, conv_buf, idx, data, nsamples/2); fast_convolute_nonlinear(s, kernel_buf, conv_buf, idx, data + nsamples/2, nsamples - nsamples/2); } } static void fast_convolute2(FIREqualizerContext *restrict s, const float *restrict kernel_buf, AVComplexFloat *restrict conv_buf, OverlapIndex *restrict idx, float *restrict data0, float *restrict data1, int nsamples) { if (nsamples <= s->nsamples_max) { AVComplexFloat *buf = conv_buf + idx->buf_idx * s->rdft_len; AVComplexFloat *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx; AVComplexFloat *tbuf = (AVComplexFloat *)s->tx_buf; int center = s->fir_len/2; int k; float tmp; memset(buf, 0, center * sizeof(*buf)); for (k = 0; k < nsamples; k++) { buf[center+k].re = data0[k]; buf[center+k].im = data1[k]; } memset(buf + center + nsamples, 0, (s->rdft_len - nsamples - center) * sizeof(*buf)); s->fft_fn(s->fft_ctx, tbuf, buf, sizeof(AVComplexFloat)); /* swap re <-> im, do backward fft using forward fft_ctx */ /* normalize with 0.5f */ tmp = tbuf[0].re; tbuf[0].re = 0.5f * kernel_buf[0] * tbuf[0].im; tbuf[0].im = 0.5f * kernel_buf[0] * tmp; for (k = 1; k < s->rdft_len/2; k++) { int m = s->rdft_len - k; tmp = tbuf[k].re; tbuf[k].re = 0.5f * kernel_buf[k] * tbuf[k].im; tbuf[k].im = 0.5f * kernel_buf[k] * tmp; tmp = tbuf[m].re; tbuf[m].re = 0.5f * kernel_buf[k] * tbuf[m].im; tbuf[m].im = 0.5f * kernel_buf[k] * tmp; } tmp = tbuf[k].re; tbuf[k].re = 0.5f * kernel_buf[k] * tbuf[k].im; tbuf[k].im = 0.5f * kernel_buf[k] * tmp; s->fft_fn(s->fft_ctx, buf, tbuf, sizeof(AVComplexFloat)); for (k = 0; k < s->rdft_len - idx->overlap_idx; k++) { buf[k].re += obuf[k].re; buf[k].im += obuf[k].im; } /* swapped re <-> im */ for (k = 0; k < nsamples; k++) { data0[k] = buf[k].im; data1[k] = buf[k].re; } idx->buf_idx = !idx->buf_idx; idx->overlap_idx = nsamples; } else { while (nsamples > s->nsamples_max * 2) { fast_convolute2(s, kernel_buf, conv_buf, idx, data0, data1, s->nsamples_max); data0 += s->nsamples_max; data1 += s->nsamples_max; nsamples -= s->nsamples_max; } fast_convolute2(s, kernel_buf, conv_buf, idx, data0, data1, nsamples/2); fast_convolute2(s, kernel_buf, conv_buf, idx, data0 + nsamples/2, data1 + nsamples/2, nsamples - nsamples/2); } } static void dump_fir(AVFilterContext *ctx, FILE *fp, int ch) { FIREqualizerContext *s = ctx->priv; int rate = ctx->inputs[0]->sample_rate; int xlog = s->dumpscale == SCALE_LOGLIN || s->dumpscale == SCALE_LOGLOG; int ylog = s->dumpscale == SCALE_LINLOG || s->dumpscale == SCALE_LOGLOG; int x; int center = s->fir_len / 2; double delay = s->zero_phase ? 0.0 : (double) center / rate; double vx, ya, yb; if (!s->min_phase) { s->analysis_buf[0] *= s->rdft_len/2; for (x = 1; x <= center; x++) { s->analysis_buf[x] *= s->rdft_len/2; s->analysis_buf[s->analysis_rdft_len - x] *= s->rdft_len/2; } } else { for (x = 0; x < s->fir_len; x++) s->analysis_buf[x] *= s->rdft_len/2; } if (ch) fprintf(fp, "\n\n"); fprintf(fp, "# time[%d] (time amplitude)\n", ch); if (!s->min_phase) { for (x = center; x > 0; x--) fprintf(fp, "%15.10f %15.10f\n", delay - (double) x / rate, (double) s->analysis_buf[s->analysis_rdft_len - x]); for (x = 0; x <= center; x++) fprintf(fp, "%15.10f %15.10f\n", delay + (double)x / rate , (double) s->analysis_buf[x]); } else { for (x = 0; x < s->fir_len; x++) fprintf(fp, "%15.10f %15.10f\n", (double)x / rate, (double) s->analysis_buf[x]); } s->analysis_rdft_fn(s->analysis_rdft, s->analysis_tbuf, s->analysis_buf, sizeof(float)); fprintf(fp, "\n\n# freq[%d] (frequency desired_gain actual_gain)\n", ch); for (x = 0; x <= s->analysis_rdft_len/2; x++) { int i = 2 * x; vx = (double)x * rate / s->analysis_rdft_len; if (xlog) vx = log2(0.05*vx); ya = s->dump_buf[i]; yb = s->min_phase ? hypotf(s->analysis_tbuf[i], s->analysis_tbuf[i+1]) : s->analysis_tbuf[i]; if (s->min_phase) yb = fabs(yb); if (ylog) { ya = 20.0 * log10(fabs(ya)); yb = 20.0 * log10(fabs(yb)); } fprintf(fp, "%17.10f %17.10f %17.10f\n", vx, ya, yb); } } static double entry_func(void *p, double freq, double gain) { AVFilterContext *ctx = p; FIREqualizerContext *s = ctx->priv; if (s->nb_gain_entry >= NB_GAIN_ENTRY_MAX) { av_log(ctx, AV_LOG_ERROR, "entry table overflow.\n"); s->gain_entry_err = AVERROR(EINVAL); return 0; } if (isnan(freq)) { av_log(ctx, AV_LOG_ERROR, "nan frequency (%g, %g).\n", freq, gain); s->gain_entry_err = AVERROR(EINVAL); return 0; } if (s->nb_gain_entry > 0 && freq <= s->gain_entry_tbl[s->nb_gain_entry - 1].freq) { av_log(ctx, AV_LOG_ERROR, "unsorted frequency (%g, %g).\n", freq, gain); s->gain_entry_err = AVERROR(EINVAL); return 0; } s->gain_entry_tbl[s->nb_gain_entry].freq = freq; s->gain_entry_tbl[s->nb_gain_entry].gain = gain; s->nb_gain_entry++; return 0; } static int gain_entry_compare(const void *key, const void *memb) { const double *freq = key; const GainEntry *entry = memb; if (*freq < entry[0].freq) return -1; if (*freq > entry[1].freq) return 1; return 0; } static double gain_interpolate_func(void *p, double freq) { AVFilterContext *ctx = p; FIREqualizerContext *s = ctx->priv; GainEntry *res; double d0, d1, d; if (isnan(freq)) return freq; if (!s->nb_gain_entry) return 0; if (freq <= s->gain_entry_tbl[0].freq) return s->gain_entry_tbl[0].gain; if (freq >= s->gain_entry_tbl[s->nb_gain_entry-1].freq) return s->gain_entry_tbl[s->nb_gain_entry-1].gain; res = bsearch(&freq, &s->gain_entry_tbl, s->nb_gain_entry - 1, sizeof(*res), gain_entry_compare); av_assert0(res); d = res[1].freq - res[0].freq; d0 = freq - res[0].freq; d1 = res[1].freq - freq; if (d0 && d1) return (d0 * res[1].gain + d1 * res[0].gain) / d; if (d0) return res[1].gain; return res[0].gain; } static double cubic_interpolate_func(void *p, double freq) { AVFilterContext *ctx = p; FIREqualizerContext *s = ctx->priv; GainEntry *res; double x, x2, x3; double a, b, c, d; double m0, m1, m2, msum, unit; if (!s->nb_gain_entry) return 0; if (freq <= s->gain_entry_tbl[0].freq) return s->gain_entry_tbl[0].gain; if (freq >= s->gain_entry_tbl[s->nb_gain_entry-1].freq) return s->gain_entry_tbl[s->nb_gain_entry-1].gain; res = bsearch(&freq, &s->gain_entry_tbl, s->nb_gain_entry - 1, sizeof(*res), gain_entry_compare); av_assert0(res); unit = res[1].freq - res[0].freq; m0 = res != s->gain_entry_tbl ? unit * (res[0].gain - res[-1].gain) / (res[0].freq - res[-1].freq) : 0; m1 = res[1].gain - res[0].gain; m2 = res != s->gain_entry_tbl + s->nb_gain_entry - 2 ? unit * (res[2].gain - res[1].gain) / (res[2].freq - res[1].freq) : 0;