2 * Copyright (c) 2018 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/avassert.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/xga_font_data.h"
32 typedef struct ThreadData {
40 typedef struct BiquadContext {
47 typedef struct IIRChannel {
52 BiquadContext *biquads;
56 typedef struct AudioIIRContext {
58 char *a_str, *b_str, *g_str;
59 double dry_gain, wet_gain;
71 enum AVSampleFormat sample_format;
73 int (*iir_channel)(AVFilterContext *ctx, void *arg, int ch, int nb_jobs);
76 static int query_formats(AVFilterContext *ctx)
78 AudioIIRContext *s = ctx->priv;
79 AVFilterFormats *formats;
80 AVFilterChannelLayouts *layouts;
81 enum AVSampleFormat sample_fmts[] = {
85 static const enum AVPixelFormat pix_fmts[] = {
92 AVFilterLink *videolink = ctx->outputs[1];
94 formats = ff_make_format_list(pix_fmts);
95 if ((ret = ff_formats_ref(formats, &videolink->in_formats)) < 0)
99 layouts = ff_all_channel_counts();
101 return AVERROR(ENOMEM);
102 ret = ff_set_common_channel_layouts(ctx, layouts);
106 sample_fmts[0] = s->sample_format;
107 formats = ff_make_format_list(sample_fmts);
109 return AVERROR(ENOMEM);
110 ret = ff_set_common_formats(ctx, formats);
114 formats = ff_all_samplerates();
116 return AVERROR(ENOMEM);
117 return ff_set_common_samplerates(ctx, formats);
120 #define IIR_CH(name, type, min, max, need_clipping) \
121 static int iir_ch_## name(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) \
123 AudioIIRContext *s = ctx->priv; \
124 const double ig = s->dry_gain; \
125 const double og = s->wet_gain; \
126 ThreadData *td = arg; \
127 AVFrame *in = td->in, *out = td->out; \
128 const type *src = (const type *)in->extended_data[ch]; \
129 double *ic = (double *)s->iir[ch].cache[0]; \
130 double *oc = (double *)s->iir[ch].cache[1]; \
131 const int nb_a = s->iir[ch].nb_ab[0]; \
132 const int nb_b = s->iir[ch].nb_ab[1]; \
133 const double *a = s->iir[ch].ab[0]; \
134 const double *b = s->iir[ch].ab[1]; \
135 int *clippings = &s->iir[ch].clippings; \
136 type *dst = (type *)out->extended_data[ch]; \
139 for (n = 0; n < in->nb_samples; n++) { \
140 double sample = 0.; \
143 memmove(&ic[1], &ic[0], (nb_b - 1) * sizeof(*ic)); \
144 memmove(&oc[1], &oc[0], (nb_a - 1) * sizeof(*oc)); \
145 ic[0] = src[n] * ig; \
146 for (x = 0; x < nb_b; x++) \
147 sample += b[x] * ic[x]; \
149 for (x = 1; x < nb_a; x++) \
150 sample -= a[x] * oc[x]; \
154 if (need_clipping && sample < min) { \
157 } else if (need_clipping && sample > max) { \
168 IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
169 IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
170 IIR_CH(fltp, float, -1., 1., 0)
171 IIR_CH(dblp, double, -1., 1., 0)
173 #define SERIAL_IIR_CH(name, type, min, max, need_clipping) \
174 static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg, int ch, int nb_jobs) \
176 AudioIIRContext *s = ctx->priv; \
177 const double ig = s->dry_gain; \
178 const double og = s->wet_gain; \
179 ThreadData *td = arg; \
180 AVFrame *in = td->in, *out = td->out; \
181 const type *src = (const type *)in->extended_data[ch]; \
182 type *dst = (type *)out->extended_data[ch]; \
183 IIRChannel *iir = &s->iir[ch]; \
184 int *clippings = &iir->clippings; \
185 int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2; \
188 for (i = 0; i < nb_biquads; i++) { \
189 const double a1 = -iir->biquads[i].a1; \
190 const double a2 = -iir->biquads[i].a2; \
191 const double b0 = iir->biquads[i].b0; \
192 const double b1 = iir->biquads[i].b1; \
193 const double b2 = iir->biquads[i].b2; \
194 double i1 = iir->biquads[i].i1; \
195 double i2 = iir->biquads[i].i2; \
196 double o1 = iir->biquads[i].o1; \
197 double o2 = iir->biquads[i].o2; \
199 for (n = 0; n < in->nb_samples; n++) { \
200 double sample = ig * (i ? dst[n] : src[n]); \
201 double o0 = sample * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
209 if (need_clipping && o0 < min) { \
212 } else if (need_clipping && o0 > max) { \
219 iir->biquads[i].i1 = i1; \
220 iir->biquads[i].i2 = i2; \
221 iir->biquads[i].o1 = o1; \
222 iir->biquads[i].o2 = o2; \
228 SERIAL_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
229 SERIAL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
230 SERIAL_IIR_CH(fltp, float, -1., 1., 0)
231 SERIAL_IIR_CH(dblp, double, -1., 1., 0)
233 static void count_coefficients(char *item_str, int *nb_items)
241 for (p = item_str; *p && *p != '|'; p++) {
247 static int read_gains(AVFilterContext *ctx, char *item_str, int nb_items)
249 AudioIIRContext *s = ctx->priv;
250 char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
253 p = old_str = av_strdup(item_str);
255 return AVERROR(ENOMEM);
256 for (i = 0; i < nb_items; i++) {
257 if (!(arg = av_strtok(p, "|", &saveptr)))
262 return AVERROR(EINVAL);
266 if (sscanf(arg, "%lf", &s->iir[i].g) != 1) {
267 av_log(ctx, AV_LOG_ERROR, "Invalid gains supplied: %s\n", arg);
269 return AVERROR(EINVAL);
280 static int read_tf_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst)
282 char *p, *arg, *old_str, *saveptr = NULL;
285 p = old_str = av_strdup(item_str);
287 return AVERROR(ENOMEM);
288 for (i = 0; i < nb_items; i++) {
289 if (!(arg = av_strtok(p, " ", &saveptr)))
293 if (sscanf(arg, "%lf", &dst[i]) != 1) {
294 av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
296 return AVERROR(EINVAL);
305 static int read_zp_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, double *dst, const char *format)
307 char *p, *arg, *old_str, *saveptr = NULL;
310 p = old_str = av_strdup(item_str);
312 return AVERROR(ENOMEM);
313 for (i = 0; i < nb_items; i++) {
314 if (!(arg = av_strtok(p, " ", &saveptr)))
318 if (sscanf(arg, format, &dst[i*2], &dst[i*2+1]) != 2) {
319 av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
321 return AVERROR(EINVAL);
330 static const char *format[] = { "%lf", "%lf %lfi", "%lf %lfr", "%lf %lfd" };
332 static int read_channels(AVFilterContext *ctx, int channels, uint8_t *item_str, int ab)
334 AudioIIRContext *s = ctx->priv;
335 char *p, *arg, *old_str, *prev_arg = NULL, *saveptr = NULL;
338 p = old_str = av_strdup(item_str);
340 return AVERROR(ENOMEM);
341 for (i = 0; i < channels; i++) {
342 IIRChannel *iir = &s->iir[i];
344 if (!(arg = av_strtok(p, "|", &saveptr)))
349 return AVERROR(EINVAL);
352 count_coefficients(arg, &iir->nb_ab[ab]);
355 iir->cache[ab] = av_calloc(iir->nb_ab[ab] + 1, sizeof(double));
356 iir->ab[ab] = av_calloc(iir->nb_ab[ab] * (!!s->format + 1), sizeof(double));
357 if (!iir->ab[ab] || !iir->cache[ab]) {
359 return AVERROR(ENOMEM);
363 ret = read_zp_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab], format[s->format]);
365 ret = read_tf_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab]);
379 static void multiply(double wre, double wim, int npz, double *coeffs)
381 double nwre = -wre, nwim = -wim;
385 for (i = npz; i >= 1; i--) {
386 cre = coeffs[2 * i + 0];
387 cim = coeffs[2 * i + 1];
389 coeffs[2 * i + 0] = (nwre * cre - nwim * cim) + coeffs[2 * (i - 1) + 0];
390 coeffs[2 * i + 1] = (nwre * cim + nwim * cre) + coeffs[2 * (i - 1) + 1];
395 coeffs[0] = nwre * cre - nwim * cim;
396 coeffs[1] = nwre * cim + nwim * cre;
399 static int expand(AVFilterContext *ctx, double *pz, int nb, double *coeffs)
406 for (i = 0; i < nb; i++) {
407 coeffs[2 * (i + 1) ] = 0.0;
408 coeffs[2 * (i + 1) + 1] = 0.0;
411 for (i = 0; i < nb; i++)
412 multiply(pz[2 * i], pz[2 * i + 1], nb, coeffs);
414 for (i = 0; i < nb + 1; i++) {
415 if (fabs(coeffs[2 * i + 1]) > FLT_EPSILON) {
416 av_log(ctx, AV_LOG_ERROR, "coeff: %lf of z^%d is not real; poles/zeros are not complex conjugates.\n",
417 coeffs[2 * i + 1], i);
418 return AVERROR(EINVAL);
425 static int convert_zp2tf(AVFilterContext *ctx, int channels)
427 AudioIIRContext *s = ctx->priv;
428 int ch, i, j, ret = 0;
430 for (ch = 0; ch < channels; ch++) {
431 IIRChannel *iir = &s->iir[ch];
434 topc = av_calloc((iir->nb_ab[0] + 1) * 2, sizeof(*topc));
435 botc = av_calloc((iir->nb_ab[1] + 1) * 2, sizeof(*botc));
436 if (!topc || !botc) {
437 ret = AVERROR(ENOMEM);
441 ret = expand(ctx, iir->ab[0], iir->nb_ab[0], botc);
446 ret = expand(ctx, iir->ab[1], iir->nb_ab[1], topc);
451 for (j = 0, i = iir->nb_ab[1]; i >= 0; j++, i--) {
452 iir->ab[1][j] = topc[2 * i];
456 for (j = 0, i = iir->nb_ab[0]; i >= 0; j++, i--) {
457 iir->ab[0][j] = botc[2 * i];
471 static int decompose_zp2biquads(AVFilterContext *ctx, int channels)
473 AudioIIRContext *s = ctx->priv;
476 for (ch = 0; ch < channels; ch++) {
477 IIRChannel *iir = &s->iir[ch];
478 int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2;
479 int current_biquad = 0;
481 iir->biquads = av_calloc(nb_biquads, sizeof(BiquadContext));
483 return AVERROR(ENOMEM);
485 while (nb_biquads--) {
486 Pair outmost_pole = { -1, -1 };
487 Pair nearest_zero = { -1, -1 };
488 double zeros[4] = { 0 };
489 double poles[4] = { 0 };
492 double min_distance = DBL_MAX;
496 for (i = 0; i < iir->nb_ab[0]; i++) {
499 if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
501 mag = hypot(iir->ab[0][2 * i], iir->ab[0][2 * i + 1]);
509 for (i = 0; i < iir->nb_ab[1]; i++) {
510 if (isnan(iir->ab[0][2 * i]) || isnan(iir->ab[0][2 * i + 1]))
513 if (iir->ab[0][2 * i ] == iir->ab[0][2 * outmost_pole.a ] &&
514 iir->ab[0][2 * i + 1] == -iir->ab[0][2 * outmost_pole.a + 1]) {
520 av_log(ctx, AV_LOG_VERBOSE, "outmost_pole is %d.%d\n", outmost_pole.a, outmost_pole.b);
522 if (outmost_pole.a < 0 || outmost_pole.b < 0)
523 return AVERROR(EINVAL);
525 for (i = 0; i < iir->nb_ab[1]; i++) {
528 if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
530 distance = hypot(iir->ab[0][2 * outmost_pole.a ] - iir->ab[1][2 * i ],
531 iir->ab[0][2 * outmost_pole.a + 1] - iir->ab[1][2 * i + 1]);
533 if (distance < min_distance) {
534 min_distance = distance;
539 for (i = 0; i < iir->nb_ab[1]; i++) {
540 if (isnan(iir->ab[1][2 * i]) || isnan(iir->ab[1][2 * i + 1]))
543 if (iir->ab[1][2 * i ] == iir->ab[1][2 * nearest_zero.a ] &&
544 iir->ab[1][2 * i + 1] == -iir->ab[1][2 * nearest_zero.a + 1]) {
550 av_log(ctx, AV_LOG_VERBOSE, "nearest_zero is %d.%d\n", nearest_zero.a, nearest_zero.b);
552 if (nearest_zero.a < 0 || nearest_zero.b < 0)
553 return AVERROR(EINVAL);
555 poles[0] = iir->ab[0][2 * outmost_pole.a ];
556 poles[1] = iir->ab[0][2 * outmost_pole.a + 1];
558 zeros[0] = iir->ab[1][2 * nearest_zero.a ];
559 zeros[1] = iir->ab[1][2 * nearest_zero.a + 1];
561 if (nearest_zero.a == nearest_zero.b && outmost_pole.a == outmost_pole.b) {
568 poles[2] = iir->ab[0][2 * outmost_pole.b ];
569 poles[3] = iir->ab[0][2 * outmost_pole.b + 1];
571 zeros[2] = iir->ab[1][2 * nearest_zero.b ];
572 zeros[3] = iir->ab[1][2 * nearest_zero.b + 1];
575 ret = expand(ctx, zeros, 2, b);
579 ret = expand(ctx, poles, 2, a);
583 iir->ab[0][2 * outmost_pole.a] = iir->ab[0][2 * outmost_pole.a + 1] = NAN;
584 iir->ab[0][2 * outmost_pole.b] = iir->ab[0][2 * outmost_pole.b + 1] = NAN;
585 iir->ab[1][2 * nearest_zero.a] = iir->ab[1][2 * nearest_zero.a + 1] = NAN;
586 iir->ab[1][2 * nearest_zero.b] = iir->ab[1][2 * nearest_zero.b + 1] = NAN;
588 iir->biquads[current_biquad].a0 = 1.0;
589 iir->biquads[current_biquad].a1 = a[2] / a[4];
590 iir->biquads[current_biquad].a2 = a[0] / a[4];
591 iir->biquads[current_biquad].b0 = b[4] / a[4] * (current_biquad ? 1.0 : iir->g);
592 iir->biquads[current_biquad].b1 = b[2] / a[4] * (current_biquad ? 1.0 : iir->g);
593 iir->biquads[current_biquad].b2 = b[0] / a[4] * (current_biquad ? 1.0 : iir->g);
595 av_log(ctx, AV_LOG_VERBOSE, "a=%lf %lf %lf:b=%lf %lf %lf\n",
596 iir->biquads[current_biquad].a0,
597 iir->biquads[current_biquad].a1,
598 iir->biquads[current_biquad].a2,
599 iir->biquads[current_biquad].b0,
600 iir->biquads[current_biquad].b1,
601 iir->biquads[current_biquad].b2);
610 static void convert_pr2zp(AVFilterContext *ctx, int channels)
612 AudioIIRContext *s = ctx->priv;
615 for (ch = 0; ch < channels; ch++) {
616 IIRChannel *iir = &s->iir[ch];
619 for (n = 0; n < iir->nb_ab[0]; n++) {
620 double r = iir->ab[0][2*n];
621 double angle = iir->ab[0][2*n+1];
623 iir->ab[0][2*n] = r * cos(angle);
624 iir->ab[0][2*n+1] = r * sin(angle);
627 for (n = 0; n < iir->nb_ab[1]; n++) {
628 double r = iir->ab[1][2*n];
629 double angle = iir->ab[1][2*n+1];
631 iir->ab[1][2*n] = r * cos(angle);
632 iir->ab[1][2*n+1] = r * sin(angle);
637 static void convert_pd2zp(AVFilterContext *ctx, int channels)
639 AudioIIRContext *s = ctx->priv;
642 for (ch = 0; ch < channels; ch++) {
643 IIRChannel *iir = &s->iir[ch];
646 for (n = 0; n < iir->nb_ab[0]; n++) {
647 double r = iir->ab[0][2*n];
648 double angle = M_PI*iir->ab[0][2*n+1]/180.;
650 iir->ab[0][2*n] = r * cos(angle);
651 iir->ab[0][2*n+1] = r * sin(angle);
654 for (n = 0; n < iir->nb_ab[1]; n++) {
655 double r = iir->ab[1][2*n];
656 double angle = M_PI*iir->ab[1][2*n+1]/180.;
658 iir->ab[1][2*n] = r * cos(angle);
659 iir->ab[1][2*n+1] = r * sin(angle);
664 static void drawtext(AVFrame *pic, int x, int y, const char *txt, uint32_t color)
670 font = avpriv_cga_font, font_height = 8;
672 for (i = 0; txt[i]; i++) {
675 uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
676 for (char_y = 0; char_y < font_height; char_y++) {
677 for (mask = 0x80; mask; mask >>= 1) {
678 if (font[txt[i] * font_height + char_y] & mask)
682 p += pic->linesize[0] - 8 * 4;
687 static void draw_line(AVFrame *out, int x0, int y0, int x1, int y1, uint32_t color)
689 int dx = FFABS(x1-x0);
690 int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
691 int err = (dx>dy ? dx : -dy) / 2, e2;
694 AV_WL32(out->data[0] + y0 * out->linesize[0] + x0 * 4, color);
696 if (x0 == x1 && y0 == y1)
713 static void draw_response(AVFilterContext *ctx, AVFrame *out)
715 AudioIIRContext *s = ctx->priv;
716 float *mag, *phase, min = FLT_MAX, max = FLT_MIN;
717 int prev_ymag = -1, prev_yphase = -1;
721 memset(out->data[0], 0, s->h * out->linesize[0]);
723 phase = av_malloc_array(s->w, sizeof(*phase));
724 mag = av_malloc_array(s->w, sizeof(*mag));
728 ch = av_clip(s->ir_channel, 0, s->channels - 1);
729 for (i = 0; i < s->w; i++) {
730 const double *b = s->iir[ch].ab[0];
731 const double *a = s->iir[ch].ab[1];
732 double w = i * M_PI / (s->w - 1);
735 double real, imag, div;
737 if (s->format == 0) {
738 realz = 0., realp = 0.;
739 imagz = 0., imagp = 0.;
740 for (x = 0; x < s->iir[ch].nb_ab[1]; x++) {
741 realz += cos(-x * w) * a[x];
742 imagz += sin(-x * w) * a[x];
745 for (x = 0; x < s->iir[ch].nb_ab[0]; x++) {
746 realp += cos(-x * w) * b[x];
747 imagp += sin(-x * w) * b[x];
750 div = realp * realp + imagp * imagp;
751 real = (realz * realp + imagz * imagp) / div;
752 imag = (imagz * realp - imagp * realz) / div;
756 for (x = 0; x < s->iir[ch].nb_ab[1]; x++) {
757 double ore, oim, re, im;
759 re = cos(w) - a[2 * x];
760 im = sin(w) - a[2 * x + 1];
765 real = ore * re - oim * im;
766 imag = ore * im + oim * re;
769 for (x = 0; x < s->iir[ch].nb_ab[0]; x++) {
770 double ore, oim, re, im;
772 re = cos(w) - b[2 * x];
773 im = sin(w) - b[2 * x + 1];
777 div = re * re + im * im;
779 real = (ore * re + oim * im) / div;
780 imag = (oim * re - ore * im) / div;
784 mag[i] = s->iir[ch].g * hypot(real, imag);
785 phase[i] = atan2(imag, real);
786 min = fminf(min, mag[i]);
787 max = fmaxf(max, mag[i]);
790 for (i = 0; i < s->w; i++) {
791 int ymag = mag[i] / max * (s->h - 1);
792 int yphase = (0.5 * (1. + phase[i] / M_PI)) * (s->h - 1);
794 ymag = s->h - 1 - av_clip(ymag, 0, s->h - 1);
795 yphase = s->h - 1 - av_clip(yphase, 0, s->h - 1);
800 prev_yphase = yphase;
802 draw_line(out, i, ymag, FFMAX(i - 1, 0), prev_ymag, 0xFFFF00FF);
803 draw_line(out, i, yphase, FFMAX(i - 1, 0), prev_yphase, 0xFF00FF00);
806 prev_yphase = yphase;
809 if (s->w > 400 && s->h > 100) {
810 drawtext(out, 2, 2, "Max Magnitude:", 0xDDDDDDDD);
811 snprintf(text, sizeof(text), "%.2f", max);
812 drawtext(out, 15 * 8 + 2, 2, text, 0xDDDDDDDD);
814 drawtext(out, 2, 12, "Min Magnitude:", 0xDDDDDDDD);
815 snprintf(text, sizeof(text), "%.2f", min);
816 drawtext(out, 15 * 8 + 2, 12, text, 0xDDDDDDDD);
824 static int config_output(AVFilterLink *outlink)
826 AVFilterContext *ctx = outlink->src;
827 AudioIIRContext *s = ctx->priv;
828 AVFilterLink *inlink = ctx->inputs[0];
831 s->channels = inlink->channels;
832 s->iir = av_calloc(s->channels, sizeof(*s->iir));
834 return AVERROR(ENOMEM);
836 ret = read_gains(ctx, s->g_str, inlink->channels);
840 ret = read_channels(ctx, inlink->channels, s->a_str, 0);
844 ret = read_channels(ctx, inlink->channels, s->b_str, 1);
848 if (s->format == 2) {
849 convert_pr2zp(ctx, inlink->channels);
850 } else if (s->format == 3) {
851 convert_pd2zp(ctx, inlink->channels);
854 av_frame_free(&s->video);
856 s->video = ff_get_video_buffer(ctx->outputs[1], s->w, s->h);
858 return AVERROR(ENOMEM);
860 draw_response(ctx, s->video);
864 av_log(ctx, AV_LOG_WARNING, "tf coefficients format is not recommended for too high number of zeros/poles.\n");
866 if (s->format > 0 && s->process == 0) {
867 av_log(ctx, AV_LOG_WARNING, "Direct processsing is not recommended for zp coefficients format.\n");
869 ret = convert_zp2tf(ctx, inlink->channels);
872 } else if (s->format == 0 && s->process == 1) {
873 av_log(ctx, AV_LOG_ERROR, "Serial cascading is not implemented for transfer function.\n");
874 return AVERROR_PATCHWELCOME;
875 } else if (s->format > 0 && s->process == 1) {
876 if (inlink->format == AV_SAMPLE_FMT_S16P)
877 av_log(ctx, AV_LOG_WARNING, "Serial cascading is not recommended for i16 precision.\n");
879 ret = decompose_zp2biquads(ctx, inlink->channels);
884 for (ch = 0; s->format == 0 && ch < inlink->channels; ch++) {
885 IIRChannel *iir = &s->iir[ch];
887 for (i = 1; i < iir->nb_ab[0]; i++) {
888 iir->ab[0][i] /= iir->ab[0][0];
891 for (i = 0; i < iir->nb_ab[1]; i++) {
892 iir->ab[1][i] *= iir->g / iir->ab[0][0];
896 switch (inlink->format) {
897 case AV_SAMPLE_FMT_DBLP: s->iir_channel = s->process == 1 ? iir_ch_serial_dblp : iir_ch_dblp; break;
898 case AV_SAMPLE_FMT_FLTP: s->iir_channel = s->process == 1 ? iir_ch_serial_fltp : iir_ch_fltp; break;
899 case AV_SAMPLE_FMT_S32P: s->iir_channel = s->process == 1 ? iir_ch_serial_s32p : iir_ch_s32p; break;
900 case AV_SAMPLE_FMT_S16P: s->iir_channel = s->process == 1 ? iir_ch_serial_s16p : iir_ch_s16p; break;
906 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
908 AVFilterContext *ctx = inlink->dst;
909 AudioIIRContext *s = ctx->priv;
910 AVFilterLink *outlink = ctx->outputs[0];
915 if (av_frame_is_writable(in)) {
918 out = ff_get_audio_buffer(outlink, in->nb_samples);
921 return AVERROR(ENOMEM);
923 av_frame_copy_props(out, in);
928 ctx->internal->execute(ctx, s->iir_channel, &td, NULL, outlink->channels);
930 for (ch = 0; ch < outlink->channels; ch++) {
931 if (s->iir[ch].clippings > 0)
932 av_log(ctx, AV_LOG_WARNING, "Channel %d clipping %d times. Please reduce gain.\n",
933 ch, s->iir[ch].clippings);
934 s->iir[ch].clippings = 0;
941 AVFilterLink *outlink = ctx->outputs[1];
943 s->video->pts = out->pts;
944 ret = ff_filter_frame(outlink, av_frame_clone(s->video));
949 return ff_filter_frame(outlink, out);
952 static int config_video(AVFilterLink *outlink)
954 AVFilterContext *ctx = outlink->src;
955 AudioIIRContext *s = ctx->priv;
957 outlink->sample_aspect_ratio = (AVRational){1,1};
964 static av_cold int init(AVFilterContext *ctx)
966 AudioIIRContext *s = ctx->priv;
967 AVFilterPad pad, vpad;
970 if (!s->a_str || !s->b_str || !s->g_str) {
971 av_log(ctx, AV_LOG_ERROR, "Valid coefficients are mandatory.\n");
972 return AVERROR(EINVAL);
975 switch (s->precision) {
976 case 0: s->sample_format = AV_SAMPLE_FMT_DBLP; break;
977 case 1: s->sample_format = AV_SAMPLE_FMT_FLTP; break;
978 case 2: s->sample_format = AV_SAMPLE_FMT_S32P; break;
979 case 3: s->sample_format = AV_SAMPLE_FMT_S16P; break;
980 default: return AVERROR_BUG;
984 .name = av_strdup("default"),
985 .type = AVMEDIA_TYPE_AUDIO,
986 .config_props = config_output,
990 return AVERROR(ENOMEM);
993 vpad = (AVFilterPad){
994 .name = av_strdup("filter_response"),
995 .type = AVMEDIA_TYPE_VIDEO,
996 .config_props = config_video,
999 return AVERROR(ENOMEM);
1002 ret = ff_insert_outpad(ctx, 0, &pad);
1007 ret = ff_insert_outpad(ctx, 1, &vpad);
1015 static av_cold void uninit(AVFilterContext *ctx)
1017 AudioIIRContext *s = ctx->priv;
1021 for (ch = 0; ch < s->channels; ch++) {
1022 IIRChannel *iir = &s->iir[ch];
1023 av_freep(&iir->ab[0]);
1024 av_freep(&iir->ab[1]);
1025 av_freep(&iir->cache[0]);
1026 av_freep(&iir->cache[1]);
1027 av_freep(&iir->biquads);
1032 av_freep(&ctx->output_pads[0].name);
1034 av_freep(&ctx->output_pads[1].name);
1035 av_frame_free(&s->video);
1038 static const AVFilterPad inputs[] = {
1041 .type = AVMEDIA_TYPE_AUDIO,
1042 .filter_frame = filter_frame,
1047 #define OFFSET(x) offsetof(AudioIIRContext, x)
1048 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1049 #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1051 static const AVOption aiir_options[] = {
1052 { "z", "set B/numerator/zeros coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
1053 { "p", "set A/denominator/poles coefficients", OFFSET(a_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
1054 { "k", "set channels gains", OFFSET(g_str), AV_OPT_TYPE_STRING, {.str="1|1"}, 0, 0, AF },
1055 { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
1056 { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, AF },
1057 { "f", "set coefficients format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=1}, 0, 3, AF, "format" },
1058 { "tf", "transfer function", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "format" },
1059 { "zp", "Z-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "format" },
1060 { "pr", "Z-plane zeros/poles (polar radians)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "format" },
1061 { "pd", "Z-plane zeros/poles (polar degrees)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "format" },
1062 { "r", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "process" },
1063 { "d", "direct", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "process" },
1064 { "s", "serial cascading", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "process" },
1065 { "e", "set precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" },
1066 { "dbl", "double-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "precision" },
1067 { "flt", "single-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "precision" },
1068 { "i32", "32-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "precision" },
1069 { "i16", "16-bit integers", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "precision" },
1070 { "response", "show IR frequency response", OFFSET(response), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF },
1071 { "channel", "set IR channel to display frequency response", OFFSET(ir_channel), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, VF },
1072 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, VF },
1076 AVFILTER_DEFINE_CLASS(aiir);
1078 AVFilter ff_af_aiir = {
1080 .description = NULL_IF_CONFIG_SMALL("Apply Infinite Impulse Response filter with supplied coefficients."),
1081 .priv_size = sizeof(AudioIIRContext),
1082 .priv_class = &aiir_class,
1085 .query_formats = query_formats,
1087 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
1088 AVFILTER_FLAG_SLICE_THREADS,