981 lines · 34.7 KB · C
RawDownload
/* * 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/opt.h"#include "libavutil/eval.h"#include "libavutil/avassert.h"#include "libavcodec/avfft.h"#include "avfilter.h"#include "internal.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;     RDFTContext   *analysis_rdft;    RDFTContext   *analysis_irdft;    RDFTContext   *rdft;    RDFTContext   *irdft;    FFTContext    *fft_ctx;    RDFTContext   *cepstrum_rdft;    RDFTContext   *cepstrum_irdft;    int           analysis_rdft_len;    int           rdft_len;    int           cepstrum_len;     float         *analysis_buf;    float         *dump_buf;    float         *kernel_tmp_buf;    float         *kernel_buf;    float         *cepstrum_buf;    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, "wfunc" },        { "rectangular", "rectangular window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_RECTANGULAR }, 0, 0, FLAGS, "wfunc" },        { "hann", "hann window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HANN }, 0, 0, FLAGS, "wfunc" },        { "hamming", "hamming window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HAMMING }, 0, 0, FLAGS, "wfunc" },        { "blackman", "blackman window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BLACKMAN }, 0, 0, FLAGS, "wfunc" },        { "nuttall3", "3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL3 }, 0, 0, FLAGS, "wfunc" },        { "mnuttall3", "minimum 3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_MNUTTALL3 }, 0, 0, FLAGS, "wfunc" },        { "nuttall", "nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL }, 0, 0, FLAGS, "wfunc" },        { "bnuttall", "blackman-nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BNUTTALL }, 0, 0, FLAGS, "wfunc" },        { "bharris", "blackman-harris window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BHARRIS }, 0, 0, FLAGS, "wfunc" },        { "tukey", "tukey window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_TUKEY }, 0, 0, FLAGS, "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, "scale" },        { "linlin", "linear-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLIN }, 0, 0, FLAGS, "scale" },        { "linlog", "linear-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLOG }, 0, 0, FLAGS, "scale" },        { "loglin", "logarithmic-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLIN }, 0, 0, FLAGS, "scale" },        { "loglog", "logarithmic-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLOG }, 0, 0, FLAGS, "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, "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_rdft_end(s->analysis_rdft);    av_rdft_end(s->analysis_irdft);    av_rdft_end(s->rdft);    av_rdft_end(s->irdft);    av_fft_end(s->fft_ctx);    av_rdft_end(s->cepstrum_rdft);    av_rdft_end(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->dump_buf);    av_freep(&s->kernel_tmp_buf);    av_freep(&s->kernel_buf);    av_freep(&s->cepstrum_buf);    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 int query_formats(AVFilterContext *ctx){    AVFilterChannelLayouts *layouts;    AVFilterFormats *formats;    static const enum AVSampleFormat sample_fmts[] = {        AV_SAMPLE_FMT_FLTP,        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 void fast_convolute(FIREqualizerContext *av_restrict s, const float *av_restrict kernel_buf, float *av_restrict conv_buf,                           OverlapIndex *av_restrict idx, float *av_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;        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));        av_rdft_calc(s->rdft, buf);         buf[0] *= kernel_buf[0];        buf[1] *= kernel_buf[s->rdft_len/2];        for (k = 1; k < s->rdft_len/2; k++) {            buf[2*k] *= kernel_buf[k];            buf[2*k+1] *= kernel_buf[k];        }         av_rdft_calc(s->irdft, buf);        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 *av_restrict s, const float *av_restrict kernel_buf,                                     float *av_restrict conv_buf, OverlapIndex *av_restrict idx,                                     float *av_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;        int k;         memcpy(buf, data, nsamples * sizeof(*data));        memset(buf + nsamples, 0, (s->rdft_len - nsamples) * sizeof(*data));        av_rdft_calc(s->rdft, buf);         buf[0] *= kernel_buf[0];        buf[1] *= kernel_buf[1];        for (k = 2; k < s->rdft_len; k += 2) {            float re, im;            re = buf[k] * kernel_buf[k] - buf[k+1] * kernel_buf[k+1];            im = buf[k] * kernel_buf[k+1] + buf[k+1] * kernel_buf[k];            buf[k] = re;            buf[k+1] = im;        }         av_rdft_calc(s->irdft, buf);        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 *av_restrict s, const float *av_restrict kernel_buf, FFTComplex *av_restrict conv_buf,                            OverlapIndex *av_restrict idx, float *av_restrict data0, float *av_restrict data1, int nsamples){    if (nsamples <= s->nsamples_max) {        FFTComplex *buf = conv_buf + idx->buf_idx * s->rdft_len;        FFTComplex *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx;        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));        av_fft_permute(s->fft_ctx, buf);        av_fft_calc(s->fft_ctx, buf);         /* swap re <-> im, do backward fft using forward fft_ctx */        /* normalize with 0.5f */        tmp = buf[0].re;        buf[0].re = 0.5f * kernel_buf[0] * buf[0].im;        buf[0].im = 0.5f * kernel_buf[0] * tmp;        for (k = 1; k < s->rdft_len/2; k++) {            int m = s->rdft_len - k;            tmp = buf[k].re;            buf[k].re = 0.5f * kernel_buf[k] * buf[k].im;            buf[k].im = 0.5f * kernel_buf[k] * tmp;            tmp = buf[m].re;            buf[m].re = 0.5f * kernel_buf[k] * buf[m].im;            buf[m].im = 0.5f * kernel_buf[k] * tmp;        }        tmp = buf[k].re;        buf[k].re = 0.5f * kernel_buf[k] * buf[k].im;        buf[k].im = 0.5f * kernel_buf[k] * tmp;         av_fft_permute(s->fft_ctx, buf);        av_fft_calc(s->fft_ctx, buf);         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]);    }     av_rdft_calc(s->analysis_rdft, s->analysis_buf);     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 = (x == s->analysis_rdft_len/2) ? 1 : 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 && (i > 1) ? hypotf(s->analysis_buf[i], s->analysis_buf[i+1]) : s->analysis_buf[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; 

Lines 1–500 of 981.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t