2018-05-31 17:24:23 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Crossover filter
|
|
|
|
*
|
|
|
|
* Split an audio stream into several bands.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/attributes.h"
|
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/channel_layout.h"
|
2018-11-18 21:13:55 +02:00
|
|
|
#include "libavutil/eval.h"
|
2020-11-28 16:54:10 +02:00
|
|
|
#include "libavutil/float_dsp.h"
|
2018-05-31 17:24:23 +02:00
|
|
|
#include "libavutil/internal.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
|
|
|
#include "audio.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "formats.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
#define MAX_SPLITS 16
|
|
|
|
#define MAX_BANDS MAX_SPLITS + 1
|
|
|
|
|
2020-11-29 11:12:26 +02:00
|
|
|
typedef struct BiquadCoeffs {
|
2020-11-26 12:53:17 +02:00
|
|
|
double b0, b1, b2;
|
|
|
|
double a1, a2;
|
2020-11-29 11:12:26 +02:00
|
|
|
} BiquadCoeffs;
|
|
|
|
|
|
|
|
typedef struct BiquadContext {
|
2020-11-25 23:23:50 +02:00
|
|
|
double z1, z2;
|
2018-05-31 17:24:23 +02:00
|
|
|
} BiquadContext;
|
|
|
|
|
|
|
|
typedef struct CrossoverChannel {
|
2020-11-26 12:19:51 +02:00
|
|
|
BiquadContext lp[MAX_BANDS][20];
|
|
|
|
BiquadContext hp[MAX_BANDS][20];
|
2020-11-26 16:36:16 +02:00
|
|
|
BiquadContext ap[MAX_BANDS][MAX_BANDS][20];
|
2018-05-31 17:24:23 +02:00
|
|
|
} CrossoverChannel;
|
|
|
|
|
|
|
|
typedef struct AudioCrossoverContext {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
char *splits_str;
|
2020-11-25 21:56:17 +02:00
|
|
|
int order_opt;
|
2020-11-28 16:54:10 +02:00
|
|
|
float level_in;
|
2018-05-31 17:24:23 +02:00
|
|
|
|
2020-11-25 21:56:17 +02:00
|
|
|
int order;
|
2018-05-31 17:24:23 +02:00
|
|
|
int filter_count;
|
2020-11-27 13:42:58 +02:00
|
|
|
int first_order;
|
|
|
|
int ap_filter_count;
|
2018-05-31 17:24:23 +02:00
|
|
|
int nb_splits;
|
|
|
|
float *splits;
|
|
|
|
|
2020-11-29 11:12:26 +02:00
|
|
|
BiquadCoeffs lp[MAX_BANDS][20];
|
|
|
|
BiquadCoeffs hp[MAX_BANDS][20];
|
|
|
|
BiquadCoeffs ap[MAX_BANDS][20];
|
|
|
|
|
2018-05-31 17:24:23 +02:00
|
|
|
CrossoverChannel *xover;
|
2020-02-07 13:06:06 +02:00
|
|
|
|
|
|
|
AVFrame *input_frame;
|
|
|
|
AVFrame *frames[MAX_BANDS];
|
2020-11-27 19:32:56 +02:00
|
|
|
|
|
|
|
int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
|
2020-11-28 16:54:10 +02:00
|
|
|
|
|
|
|
AVFloatDSPContext *fdsp;
|
2018-05-31 17:24:23 +02:00
|
|
|
} AudioCrossoverContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(AudioCrossoverContext, x)
|
|
|
|
#define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
|
|
|
|
static const AVOption acrossover_options[] = {
|
|
|
|
{ "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
|
2020-11-26 12:19:51 +02:00
|
|
|
{ "order", "set order", OFFSET(order_opt), AV_OPT_TYPE_INT, {.i64=1}, 0, 9, AF, "m" },
|
2018-05-31 17:24:23 +02:00
|
|
|
{ "2nd", "2nd order", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
|
|
|
|
{ "4th", "4th order", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
|
2020-11-26 12:19:51 +02:00
|
|
|
{ "6th", "6th order", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "m" },
|
|
|
|
{ "8th", "8th order", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "m" },
|
|
|
|
{ "10th", "10th order", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "m" },
|
|
|
|
{ "12th", "12th order", 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, AF, "m" },
|
|
|
|
{ "14th", "14th order", 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, AF, "m" },
|
|
|
|
{ "16th", "16th order", 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, AF, "m" },
|
|
|
|
{ "18th", "18th order", 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, AF, "m" },
|
|
|
|
{ "20th", "20th order", 0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, AF, "m" },
|
2020-11-28 16:54:10 +02:00
|
|
|
{ "level", "set input gain", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
|
2018-05-31 17:24:23 +02:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(acrossover);
|
|
|
|
|
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AudioCrossoverContext *s = ctx->priv;
|
|
|
|
char *p, *arg, *saveptr = NULL;
|
|
|
|
int i, ret = 0;
|
|
|
|
|
2020-11-28 16:54:10 +02:00
|
|
|
s->fdsp = avpriv_float_dsp_alloc(0);
|
|
|
|
if (!s->fdsp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2018-05-31 17:24:23 +02:00
|
|
|
s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
|
|
|
|
if (!s->splits)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
p = s->splits_str;
|
|
|
|
for (i = 0; i < MAX_SPLITS; i++) {
|
|
|
|
float freq;
|
|
|
|
|
|
|
|
if (!(arg = av_strtok(p, " |", &saveptr)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
p = NULL;
|
|
|
|
|
2020-03-29 02:08:04 +02:00
|
|
|
if (av_sscanf(arg, "%f", &freq) != 1) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid syntax for frequency[%d].\n", i);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2018-05-31 17:24:23 +02:00
|
|
|
if (freq <= 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > 0 && freq <= s->splits[i-1]) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->splits[i] = freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->nb_splits = i;
|
|
|
|
|
|
|
|
for (i = 0; i <= s->nb_splits; i++) {
|
|
|
|
AVFilterPad pad = { 0 };
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
pad.type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
name = av_asprintf("out%d", ctx->nb_outputs);
|
|
|
|
if (!name)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
pad.name = name;
|
|
|
|
|
|
|
|
if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
|
|
|
|
av_freep(&pad.name);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-11-29 11:12:26 +02:00
|
|
|
static void set_lp(BiquadCoeffs *b, double fc, double q, double sr)
|
2018-05-31 17:24:23 +02:00
|
|
|
{
|
2020-11-26 12:53:17 +02:00
|
|
|
double omega = M_PI * fc / sr;
|
|
|
|
double cosine = cos(omega);
|
|
|
|
double alpha = sin(omega) / (2. * q);
|
|
|
|
|
|
|
|
double b0 = (1. - cosine) / 2.;
|
|
|
|
double b1 = 1. - cosine;
|
|
|
|
double b2 = (1. - cosine) / 2.;
|
|
|
|
double a0 = 1. + alpha;
|
|
|
|
double a1 = -2. * cosine;
|
|
|
|
double a2 = 1. - alpha;
|
|
|
|
|
|
|
|
b->b0 = b0 / a0;
|
|
|
|
b->b1 = b1 / a0;
|
|
|
|
b->b2 = b2 / a0;
|
|
|
|
b->a1 = -a1 / a0;
|
|
|
|
b->a2 = -a2 / a0;
|
2018-05-31 17:24:23 +02:00
|
|
|
}
|
|
|
|
|
2020-11-29 11:12:26 +02:00
|
|
|
static void set_hp(BiquadCoeffs *b, double fc, double q, double sr)
|
2020-03-26 12:45:32 +02:00
|
|
|
{
|
2020-11-26 12:53:17 +02:00
|
|
|
double omega = M_PI * fc / sr;
|
|
|
|
double cosine = cos(omega);
|
|
|
|
double alpha = sin(omega) / (2. * q);
|
|
|
|
|
|
|
|
double b0 = (1. + cosine) / 2.;
|
|
|
|
double b1 = -1. - cosine;
|
|
|
|
double b2 = (1. + cosine) / 2.;
|
|
|
|
double a0 = 1. + alpha;
|
|
|
|
double a1 = -2. * cosine;
|
|
|
|
double a2 = 1. - alpha;
|
|
|
|
|
|
|
|
b->b0 = b0 / a0;
|
|
|
|
b->b1 = b1 / a0;
|
|
|
|
b->b2 = b2 / a0;
|
|
|
|
b->a1 = -a1 / a0;
|
|
|
|
b->a2 = -a2 / a0;
|
2020-11-25 21:56:17 +02:00
|
|
|
}
|
|
|
|
|
2020-11-29 11:12:26 +02:00
|
|
|
static void set_ap(BiquadCoeffs *b, double fc, double q, double sr)
|
2020-11-26 16:36:16 +02:00
|
|
|
{
|
|
|
|
double omega = M_PI * fc / sr;
|
|
|
|
double cosine = cos(omega);
|
|
|
|
double alpha = sin(omega) / (2. * q);
|
|
|
|
|
|
|
|
double a0 = 1. + alpha;
|
|
|
|
double a1 = -2. * cosine;
|
|
|
|
double a2 = 1. - alpha;
|
|
|
|
double b0 = a2;
|
|
|
|
double b1 = a1;
|
|
|
|
double b2 = a0;
|
|
|
|
|
|
|
|
b->b0 = b0 / a0;
|
|
|
|
b->b1 = b1 / a0;
|
|
|
|
b->b2 = b2 / a0;
|
|
|
|
b->a1 = -a1 / a0;
|
|
|
|
b->a2 = -a2 / a0;
|
|
|
|
}
|
|
|
|
|
2020-11-29 11:12:26 +02:00
|
|
|
static void set_ap1(BiquadCoeffs *b, double fc, double sr)
|
2020-11-27 13:42:58 +02:00
|
|
|
{
|
2020-11-28 15:37:06 +02:00
|
|
|
double omega = M_PI * fc / sr;
|
2020-11-27 13:42:58 +02:00
|
|
|
|
2020-11-28 15:37:06 +02:00
|
|
|
b->a1 = exp(-omega);
|
2020-11-27 13:42:58 +02:00
|
|
|
b->a2 = 0.;
|
|
|
|
b->b0 = -b->a1;
|
|
|
|
b->b1 = 1.;
|
|
|
|
b->b2 = 0.;
|
|
|
|
}
|
|
|
|
|
2020-11-25 21:56:17 +02:00
|
|
|
static void calc_q_factors(int order, double *q)
|
|
|
|
{
|
2020-11-26 12:19:51 +02:00
|
|
|
double n = order / 2.;
|
2020-11-25 21:56:17 +02:00
|
|
|
|
2020-11-26 12:19:51 +02:00
|
|
|
for (int i = 0; i < n / 2; i++)
|
|
|
|
q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
|
2020-03-26 12:45:32 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 17:24:23 +02:00
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterFormats *formats;
|
|
|
|
AVFilterChannelLayouts *layouts;
|
|
|
|
static const enum AVSampleFormat sample_fmts[] = {
|
2020-11-27 19:32:56 +02:00
|
|
|
AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
|
2018-05-31 17:24:23 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-11-27 19:32:56 +02:00
|
|
|
#define BIQUAD_PROCESS(name, type) \
|
2020-11-29 11:12:26 +02:00
|
|
|
static void biquad_process_## name(const BiquadCoeffs *const c,\
|
|
|
|
BiquadContext *b, \
|
2020-11-27 19:32:56 +02:00
|
|
|
type *dst, const type *src, \
|
|
|
|
int nb_samples) \
|
|
|
|
{ \
|
2020-11-29 11:12:26 +02:00
|
|
|
const type b0 = c->b0; \
|
|
|
|
const type b1 = c->b1; \
|
|
|
|
const type b2 = c->b2; \
|
|
|
|
const type a1 = c->a1; \
|
|
|
|
const type a2 = c->a2; \
|
2020-11-27 19:32:56 +02:00
|
|
|
type z1 = b->z1; \
|
|
|
|
type z2 = b->z2; \
|
|
|
|
\
|
|
|
|
for (int n = 0; n < nb_samples; n++) { \
|
|
|
|
const type in = src[n]; \
|
|
|
|
type out; \
|
|
|
|
\
|
|
|
|
out = in * b0 + z1; \
|
|
|
|
z1 = b1 * in + z2 + a1 * out; \
|
|
|
|
z2 = b2 * in + a2 * out; \
|
|
|
|
dst[n] = out; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
b->z1 = z1; \
|
|
|
|
b->z2 = z2; \
|
|
|
|
}
|
2018-05-31 17:24:23 +02:00
|
|
|
|
2020-11-27 19:32:56 +02:00
|
|
|
BIQUAD_PROCESS(fltp, float)
|
|
|
|
BIQUAD_PROCESS(dblp, double)
|
|
|
|
|
2020-11-28 16:54:10 +02:00
|
|
|
#define XOVER_PROCESS(name, type, one, ff) \
|
2020-11-27 19:32:56 +02:00
|
|
|
static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
|
|
|
|
{ \
|
|
|
|
AudioCrossoverContext *s = ctx->priv; \
|
|
|
|
AVFrame *in = s->input_frame; \
|
|
|
|
AVFrame **frames = s->frames; \
|
|
|
|
const int start = (in->channels * jobnr) / nb_jobs; \
|
|
|
|
const int end = (in->channels * (jobnr+1)) / nb_jobs; \
|
|
|
|
const int nb_samples = in->nb_samples; \
|
|
|
|
\
|
|
|
|
for (int ch = start; ch < end; ch++) { \
|
2020-11-28 16:54:10 +02:00
|
|
|
const type *src = (const type *)in->extended_data[ch]; \
|
2020-11-27 19:32:56 +02:00
|
|
|
CrossoverChannel *xover = &s->xover[ch]; \
|
|
|
|
\
|
2020-11-28 16:54:10 +02:00
|
|
|
s->fdsp->vector_## ff ##mul_scalar((type *)frames[0]->extended_data[ch], src, \
|
|
|
|
s->level_in, nb_samples); \
|
|
|
|
emms_c(); \
|
|
|
|
\
|
2020-11-27 19:32:56 +02:00
|
|
|
for (int band = 0; band < ctx->nb_outputs; band++) { \
|
|
|
|
for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \
|
2020-11-28 13:49:25 +02:00
|
|
|
const type *prv = (const type *)frames[band]->extended_data[ch]; \
|
2020-11-27 19:32:56 +02:00
|
|
|
type *dst = (type *)frames[band + 1]->extended_data[ch]; \
|
2020-11-28 16:54:10 +02:00
|
|
|
const type *hsrc = f == 0 ? prv : dst; \
|
2020-11-27 19:32:56 +02:00
|
|
|
BiquadContext *hp = &xover->hp[band][f]; \
|
2020-11-29 11:12:26 +02:00
|
|
|
BiquadCoeffs *hpc = &s->hp[band][f]; \
|
2020-11-27 19:32:56 +02:00
|
|
|
\
|
2020-11-29 11:12:26 +02:00
|
|
|
biquad_process_## name(hpc, hp, dst, hsrc, nb_samples); \
|
2020-11-27 19:32:56 +02:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \
|
|
|
|
type *dst = (type *)frames[band]->extended_data[ch]; \
|
2020-11-28 16:54:10 +02:00
|
|
|
const type *lsrc = dst; \
|
2020-11-27 19:32:56 +02:00
|
|
|
BiquadContext *lp = &xover->lp[band][f]; \
|
2020-11-29 11:12:26 +02:00
|
|
|
BiquadCoeffs *lpc = &s->lp[band][f]; \
|
2020-11-27 19:32:56 +02:00
|
|
|
\
|
2020-11-29 11:12:26 +02:00
|
|
|
biquad_process_## name(lpc, lp, dst, lsrc, nb_samples); \
|
2020-11-27 19:32:56 +02:00
|
|
|
} \
|
|
|
|
\
|
2020-11-28 13:49:25 +02:00
|
|
|
for (int aband = band + 1; aband + 1 < ctx->nb_outputs; aband++) { \
|
2020-11-27 19:32:56 +02:00
|
|
|
if (s->first_order) { \
|
2020-11-28 13:49:25 +02:00
|
|
|
const type *asrc = (const type *)frames[band]->extended_data[ch]; \
|
2020-11-27 19:32:56 +02:00
|
|
|
type *dst = (type *)frames[band]->extended_data[ch]; \
|
|
|
|
BiquadContext *ap = &xover->ap[band][aband][0]; \
|
2020-11-29 11:12:26 +02:00
|
|
|
BiquadCoeffs *apc = &s->ap[aband][0]; \
|
2020-11-27 19:32:56 +02:00
|
|
|
\
|
2020-11-29 11:12:26 +02:00
|
|
|
biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
|
2020-11-27 19:32:56 +02:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
for (int f = s->first_order; f < s->ap_filter_count; f++) { \
|
2020-11-28 13:49:25 +02:00
|
|
|
const type *asrc = (const type *)frames[band]->extended_data[ch]; \
|
2020-11-27 19:32:56 +02:00
|
|
|
type *dst = (type *)frames[band]->extended_data[ch]; \
|
|
|
|
BiquadContext *ap = &xover->ap[band][aband][f]; \
|
2020-11-29 11:12:26 +02:00
|
|
|
BiquadCoeffs *apc = &s->ap[aband][f]; \
|
2020-11-27 19:32:56 +02:00
|
|
|
\
|
2020-11-29 11:12:26 +02:00
|
|
|
biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
|
2020-11-27 19:32:56 +02:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) { \
|
|
|
|
if (band & 1) { \
|
|
|
|
type *dst = (type *)frames[band]->extended_data[ch]; \
|
|
|
|
\
|
|
|
|
for (int n = 0; n < nb_samples; n++) \
|
|
|
|
dst[n] *= -one; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
return 0; \
|
2018-05-31 17:24:23 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 16:54:10 +02:00
|
|
|
XOVER_PROCESS(fltp, float, 1.f, f)
|
|
|
|
XOVER_PROCESS(dblp, double, 1.0, d)
|
2020-11-27 19:32:56 +02:00
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
2018-05-31 17:24:23 +02:00
|
|
|
{
|
2020-11-27 19:32:56 +02:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2018-05-31 17:24:23 +02:00
|
|
|
AudioCrossoverContext *s = ctx->priv;
|
2020-11-27 19:32:56 +02:00
|
|
|
int sample_rate = inlink->sample_rate;
|
|
|
|
double q[16];
|
2018-05-31 17:24:23 +02:00
|
|
|
|
2020-11-27 19:32:56 +02:00
|
|
|
s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
|
|
|
|
if (!s->xover)
|
|
|
|
return AVERROR(ENOMEM);
|
2018-05-31 17:24:23 +02:00
|
|
|
|
2020-11-27 19:32:56 +02:00
|
|
|
s->order = (s->order_opt + 1) * 2;
|
|
|
|
s->filter_count = s->order / 2;
|
|
|
|
s->first_order = s->filter_count & 1;
|
|
|
|
s->ap_filter_count = s->filter_count / 2 + s->first_order;
|
|
|
|
calc_q_factors(s->order, q);
|
2018-05-31 17:24:23 +02:00
|
|
|
|
2020-11-29 11:12:26 +02:00
|
|
|
for (int band = 0; band <= s->nb_splits; band++) {
|
|
|
|
if (s->first_order) {
|
|
|
|
set_lp(&s->lp[band][0], s->splits[band], 0.5, sample_rate);
|
|
|
|
set_hp(&s->hp[band][0], s->splits[band], 0.5, sample_rate);
|
|
|
|
}
|
2019-04-14 19:03:39 +02:00
|
|
|
|
2020-11-29 11:12:26 +02:00
|
|
|
for (int n = s->first_order; n < s->filter_count; n++) {
|
|
|
|
const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
|
2019-04-14 19:03:39 +02:00
|
|
|
|
2020-11-29 11:12:26 +02:00
|
|
|
set_lp(&s->lp[band][n], s->splits[band], q[idx], sample_rate);
|
|
|
|
set_hp(&s->hp[band][n], s->splits[band], q[idx], sample_rate);
|
|
|
|
}
|
2020-11-26 16:36:16 +02:00
|
|
|
|
2020-11-29 11:12:26 +02:00
|
|
|
if (s->first_order)
|
|
|
|
set_ap1(&s->ap[band][0], s->splits[band], sample_rate);
|
2020-11-27 13:42:58 +02:00
|
|
|
|
2020-11-29 11:12:26 +02:00
|
|
|
for (int n = s->first_order; n < s->ap_filter_count; n++) {
|
|
|
|
const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
|
2020-11-26 16:36:16 +02:00
|
|
|
|
2020-11-29 11:12:26 +02:00
|
|
|
set_ap(&s->ap[band][n], s->splits[band], q[idx], sample_rate);
|
2020-11-26 14:13:42 +02:00
|
|
|
}
|
2020-11-27 19:32:56 +02:00
|
|
|
}
|
2020-11-26 14:13:42 +02:00
|
|
|
|
2020-11-27 19:32:56 +02:00
|
|
|
switch (inlink->format) {
|
|
|
|
case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
|
|
|
|
case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
|
2018-05-31 17:24:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-07 13:06:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AudioCrossoverContext *s = ctx->priv;
|
|
|
|
AVFrame **frames = s->frames;
|
|
|
|
int i, ret = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->nb_outputs; i++) {
|
|
|
|
frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
|
|
|
|
|
|
|
|
if (!frames[i]) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
frames[i]->pts = in->pts;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
s->input_frame = in;
|
2020-11-27 19:32:56 +02:00
|
|
|
ctx->internal->execute(ctx, s->filter_channels, NULL, NULL, FFMIN(inlink->channels,
|
|
|
|
ff_filter_get_nb_threads(ctx)));
|
2020-02-07 13:06:06 +02:00
|
|
|
|
2018-05-31 17:24:23 +02:00
|
|
|
for (i = 0; i < ctx->nb_outputs; i++) {
|
|
|
|
ret = ff_filter_frame(ctx->outputs[i], frames[i]);
|
2020-02-07 13:06:06 +02:00
|
|
|
frames[i] = NULL;
|
2018-05-31 17:24:23 +02:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
2020-02-07 13:09:24 +02:00
|
|
|
for (i = 0; i < ctx->nb_outputs; i++)
|
|
|
|
av_frame_free(&frames[i]);
|
2018-05-31 17:24:23 +02:00
|
|
|
av_frame_free(&in);
|
2020-02-07 13:06:06 +02:00
|
|
|
s->input_frame = NULL;
|
2018-05-31 17:24:23 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AudioCrossoverContext *s = ctx->priv;
|
|
|
|
int i;
|
|
|
|
|
2020-11-28 16:54:10 +02:00
|
|
|
av_freep(&s->fdsp);
|
2018-05-31 17:24:23 +02:00
|
|
|
av_freep(&s->splits);
|
2019-10-18 10:33:54 +02:00
|
|
|
av_freep(&s->xover);
|
2018-05-31 17:24:23 +02:00
|
|
|
|
|
|
|
for (i = 0; i < ctx->nb_outputs; i++)
|
|
|
|
av_freep(&ctx->output_pads[i].name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
.config_props = config_input,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFilter ff_af_acrossover = {
|
|
|
|
.name = "acrossover",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
|
|
|
|
.priv_size = sizeof(AudioCrossoverContext),
|
|
|
|
.priv_class = &acrossover_class,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.inputs = inputs,
|
|
|
|
.outputs = NULL,
|
2020-02-07 13:06:06 +02:00
|
|
|
.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
|
|
|
|
AVFILTER_FLAG_SLICE_THREADS,
|
2018-05-31 17:24:23 +02:00
|
|
|
};
|