char *a_str, *b_str, *g_str;
double dry_gain, wet_gain;
double mix;
+ int normalize;
int format;
int process;
int precision;
AVFilterLink *videolink = ctx->outputs[1];
formats = ff_make_format_list(pix_fmts);
- if ((ret = ff_formats_ref(formats, &videolink->in_formats)) < 0)
+ if ((ret = ff_formats_ref(formats, &videolink->incfg.formats)) < 0)
return ret;
}
return 0;
}
-static const char *format[] = { "%lf", "%lf %lfi", "%lf %lfr", "%lf %lfd" };
+static const char *format[] = { "%lf", "%lf %lfi", "%lf %lfr", "%lf %lfd", "%lf %lfi" };
static int read_channels(AVFilterContext *ctx, int channels, uint8_t *item_str, int ab)
{
return 0;
}
-static void multiply(double wre, double wim, int npz, double *coeffs)
+static void cmul(double re, double im, double re2, double im2, double *RE, double *IM)
{
- double nwre = -wre, nwim = -wim;
- double cre, cim;
- int i;
+ *RE = re * re2 - im * im2;
+ *IM = re * im2 + re2 * im;
+}
+
+static int expand(AVFilterContext *ctx, double *pz, int n, double *coefs)
+{
+ coefs[2 * n] = 1.0;
- for (i = npz; i >= 1; i--) {
- cre = coeffs[2 * i + 0];
- cim = coeffs[2 * i + 1];
+ for (int i = 1; i <= n; i++) {
+ for (int j = n - i; j < n; j++) {
+ double re, im;
+
+ cmul(coefs[2 * (j + 1)], coefs[2 * (j + 1) + 1],
+ pz[2 * (i - 1)], pz[2 * (i - 1) + 1], &re, &im);
+
+ coefs[2 * j] -= re;
+ coefs[2 * j + 1] -= im;
+ }
+ }
- coeffs[2 * i + 0] = (nwre * cre - nwim * cim) + coeffs[2 * (i - 1) + 0];
- coeffs[2 * i + 1] = (nwre * cim + nwim * cre) + coeffs[2 * (i - 1) + 1];
+ for (int i = 0; i < n + 1; i++) {
+ if (fabs(coefs[2 * i + 1]) > FLT_EPSILON) {
+ av_log(ctx, AV_LOG_ERROR, "coefs: %f of z^%d is not real; poles/zeros are not complex conjugates.\n",
+ coefs[2 * i + 1], i);
+ return AVERROR(EINVAL);
+ }
}
- cre = coeffs[0];
- cim = coeffs[1];
- coeffs[0] = nwre * cre - nwim * cim;
- coeffs[1] = nwre * cim + nwim * cre;
+ return 0;
}
-static int expand(AVFilterContext *ctx, double *pz, int nb, double *coeffs)
+static void normalize_coeffs(AVFilterContext *ctx, int ch)
{
- int i;
+ AudioIIRContext *s = ctx->priv;
+ IIRChannel *iir = &s->iir[ch];
+ double sum_den = 0.;
- coeffs[0] = 1.0;
- coeffs[1] = 0.0;
+ if (!s->normalize)
+ return;
- for (i = 0; i < nb; i++) {
- coeffs[2 * (i + 1) ] = 0.0;
- coeffs[2 * (i + 1) + 1] = 0.0;
+ for (int i = 0; i < iir->nb_ab[1]; i++) {
+ sum_den += iir->ab[1][i];
}
- for (i = 0; i < nb; i++)
- multiply(pz[2 * i], pz[2 * i + 1], nb, coeffs);
+ if (sum_den > 1e-6) {
+ double factor, sum_num = 0.;
- for (i = 0; i < nb + 1; i++) {
- if (fabs(coeffs[2 * i + 1]) > FLT_EPSILON) {
- av_log(ctx, AV_LOG_ERROR, "coeff: %f of z^%d is not real; poles/zeros are not complex conjugates.\n",
- coeffs[2 * i + 1], i);
- return AVERROR(EINVAL);
+ for (int i = 0; i < iir->nb_ab[0]; i++) {
+ sum_num += iir->ab[0][i];
}
- }
- return 0;
+ factor = sum_num / sum_den;
+
+ for (int i = 0; i < iir->nb_ab[1]; i++) {
+ iir->ab[1][i] *= factor;
+ }
+ }
}
static int convert_zp2tf(AVFilterContext *ctx, int channels)
IIRChannel *iir = &s->iir[ch];
double *topc, *botc;
- topc = av_calloc((iir->nb_ab[0] + 1) * 2, sizeof(*topc));
- botc = av_calloc((iir->nb_ab[1] + 1) * 2, sizeof(*botc));
+ topc = av_calloc((iir->nb_ab[1] + 1) * 2, sizeof(*topc));
+ botc = av_calloc((iir->nb_ab[0] + 1) * 2, sizeof(*botc));
if (!topc || !botc) {
ret = AVERROR(ENOMEM);
goto fail;
}
iir->nb_ab[0]++;
+ normalize_coeffs(ctx, ch);
+
fail:
av_free(topc);
av_free(botc);
iir->biquads[current_biquad].b[1] = b[2] / a[4];
iir->biquads[current_biquad].b[2] = b[0] / a[4];
- if (fabs(iir->biquads[current_biquad].b[0] +
+ if (s->normalize &&
+ fabs(iir->biquads[current_biquad].b[0] +
iir->biquads[current_biquad].b[1] +
iir->biquads[current_biquad].b[2]) > 1e-6) {
factor = (iir->biquads[current_biquad].a[0] +
}
}
+static void convert_sp2zp(AVFilterContext *ctx, int channels)
+{
+ AudioIIRContext *s = ctx->priv;
+ int ch;
+
+ for (ch = 0; ch < channels; ch++) {
+ IIRChannel *iir = &s->iir[ch];
+ int n;
+
+ for (n = 0; n < iir->nb_ab[0]; n++) {
+ double sr = iir->ab[0][2*n];
+ double si = iir->ab[0][2*n+1];
+ double snr = 1. + sr;
+ double sdr = 1. - sr;
+ double div = sdr * sdr + si * si;
+
+ iir->ab[0][2*n] = (snr * sdr - si * si) / div;
+ iir->ab[0][2*n+1] = (sdr * si + snr * si) / div;
+ }
+
+ for (n = 0; n < iir->nb_ab[1]; n++) {
+ double sr = iir->ab[1][2*n];
+ double si = iir->ab[1][2*n+1];
+ double snr = 1. + sr;
+ double sdr = 1. - sr;
+ double div = sdr * sdr + si * si;
+
+ iir->ab[1][2*n] = (snr * sdr - si * si) / div;
+ iir->ab[1][2*n+1] = (sdr * si + snr * si) / div;
+ }
+ }
+}
+
static void convert_pd2zp(AVFilterContext *ctx, int channels)
{
AudioIIRContext *s = ctx->priv;
}
}
+static double distance(double x0, double x1, double y0, double y1)
+{
+ return hypot(x0 - x1, y0 - y1);
+}
+
static void get_response(int channel, int format, double w,
const double *b, const double *a,
- int nb_b, int nb_a, double *r, double *i)
+ int nb_b, int nb_a, double *magnitude, double *phase)
{
double realz, realp;
double imagz, imagp;
div = realp * realp + imagp * imagp;
real = (realz * realp + imagz * imagp) / div;
imag = (imagz * realp - imagp * realz) / div;
- } else {
- real = 1;
- imag = 0;
- for (int x = 0; x < nb_a; x++) {
- double ore, oim, re, im;
- re = cos(w) - a[2 * x];
- im = sin(w) - a[2 * x + 1];
-
- ore = real;
- oim = imag;
+ *magnitude = hypot(real, imag);
+ *phase = atan2(imag, real);
+ } else {
+ double p = 1., z = 1.;
+ double acc = 0.;
- real = ore * re - oim * im;
- imag = ore * im + oim * re;
+ for (int x = 0; x < nb_a; x++) {
+ z *= distance(cos(w), a[2 * x], sin(w), a[2 * x + 1]);
+ acc += atan2(sin(w) - a[2 * x + 1], cos(w) - a[2 * x]);
}
for (int x = 0; x < nb_b; x++) {
- double ore, oim, re, im;
-
- re = cos(w) - b[2 * x];
- im = sin(w) - b[2 * x + 1];
-
- ore = real;
- oim = imag;
- div = re * re + im * im;
-
- real = (ore * re + oim * im) / div;
- imag = (oim * re - ore * im) / div;
+ p *= distance(cos(w), b[2 * x], sin(w), b[2 * x + 1]);
+ acc -= atan2(sin(w) - b[2 * x + 1], cos(w) - b[2 * x]);
}
- }
- *r = real;
- *i = imag;
+ *magnitude = z / p;
+ *phase = acc;
+ }
}
static void draw_response(AVFilterContext *ctx, AVFrame *out, int sample_rate)
{
AudioIIRContext *s = ctx->priv;
double *mag, *phase, *temp, *delay, min = DBL_MAX, max = -DBL_MAX;
- double min_delay, max_delay, min_phase, max_phase;
+ double min_delay = DBL_MAX, max_delay = -DBL_MAX, min_phase, max_phase;
int prev_ymag = -1, prev_yphase = -1, prev_ydelay = -1;
char text[32];
int ch, i;
const int nb_b = s->iir[ch].nb_ab[0];
const int nb_a = s->iir[ch].nb_ab[1];
double w = i * M_PI / (s->w - 1);
- double real, imag;
+ double m, p;
- get_response(ch, s->format, w, b, a, nb_b, nb_a, &real, &imag);
+ get_response(ch, s->format, w, b, a, nb_b, nb_a, &m, &p);
- mag[i] = s->iir[ch].g * hypot(real, imag);
- phase[i] = atan2(imag, real);
+ mag[i] = s->iir[ch].g * m;
+ phase[i] = p;
min = fmin(min, mag[i]);
max = fmax(max, mag[i]);
}
max_phase = fmax(max_phase, phase[i]);
}
- delay[0] = 0.;
- min_delay = 0.;
- max_delay = 0.;
for (i = 0; i < s->w - 1; i++) {
double div = s->w / (double)sample_rate;
min_delay = fmin(min_delay, delay[i + 1]);
max_delay = fmax(max_delay, delay[i + 1]);
}
-
- delay[i] = delay[i - 1];
+ delay[0] = delay[1];
for (i = 0; i < s->w; i++) {
int ymag = mag[i] / max * (s->h - 1);
convert_pr2zp(ctx, inlink->channels);
} else if (s->format == 3) {
convert_pd2zp(ctx, inlink->channels);
+ } else if (s->format == 4) {
+ convert_sp2zp(ctx, inlink->channels);
}
if (s->format > 0) {
check_stability(ctx, inlink->channels);
iir->ab[0][i] /= iir->ab[0][0];
}
+ iir->ab[0][0] = 1.0;
for (i = 0; i < iir->nb_ab[1]; i++) {
- iir->ab[1][i] *= iir->g / iir->ab[0][0];
+ iir->ab[1][i] *= iir->g;
}
+
+ normalize_coeffs(ctx, ch);
}
switch (inlink->format) {
}
pad = (AVFilterPad){
- .name = av_strdup("default"),
+ .name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.config_props = config_output,
};
- if (!pad.name)
- return AVERROR(ENOMEM);
+ ret = ff_insert_outpad(ctx, 0, &pad);
+ if (ret < 0)
+ return ret;
if (s->response) {
vpad = (AVFilterPad){
- .name = av_strdup("filter_response"),
+ .name = "filter_response",
.type = AVMEDIA_TYPE_VIDEO,
.config_props = config_video,
};
- if (!vpad.name)
- return AVERROR(ENOMEM);
- }
-
- ret = ff_insert_outpad(ctx, 0, &pad);
- if (ret < 0)
- return ret;
- if (s->response) {
ret = ff_insert_outpad(ctx, 1, &vpad);
if (ret < 0)
return ret;
}
av_freep(&s->iir);
- av_freep(&ctx->output_pads[0].name);
- if (s->response)
- av_freep(&ctx->output_pads[1].name);
av_frame_free(&s->video);
}
#define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
static const AVOption aiir_options[] = {
+ { "zeros", "set B/numerator/zeros coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
{ "z", "set B/numerator/zeros coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
+ { "poles", "set A/denominator/poles coefficients", OFFSET(a_str),AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
{ "p", "set A/denominator/poles coefficients", OFFSET(a_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
+ { "gains", "set channels gains", OFFSET(g_str), AV_OPT_TYPE_STRING, {.str="1|1"}, 0, 0, AF },
{ "k", "set channels gains", OFFSET(g_str), AV_OPT_TYPE_STRING, {.str="1|1"}, 0, 0, AF },
{ "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
{ "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
- { "f", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, 0, 3, AF, "format" },
- { "tf", "transfer function", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "format" },
+ { "format", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, 0, 4, AF, "format" },
+ { "f", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, 0, 4, AF, "format" },
+ { "tf", "digital transfer function", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "format" },
{ "zp", "Z-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "format" },
{ "pr", "Z-plane zeros/poles (polar radians)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "format" },
{ "pd", "Z-plane zeros/poles (polar degrees)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "format" },
+ { "sp", "S-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "format" },
+ { "process", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "process" },
{ "r", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "process" },
{ "d", "direct", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "process" },
{ "s", "serial cascading", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "process" },
+ { "precision", "set filtering precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" },
{ "e", "set precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" },
{ "dbl", "double-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "precision" },
{ "flt", "single-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "precision" },
{ "i32", "32-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "precision" },
{ "i16", "16-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "precision" },
+ { "normalize", "normalize coefficients", OFFSET(normalize),AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
+ { "n", "normalize coefficients", OFFSET(normalize),AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
{ "mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
{ "response", "show IR frequency response", OFFSET(response), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF },
{ "channel", "set IR channel to display frequency response", OFFSET(ir_channel), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, VF },