]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/af_aiir.c
avcodec/mjpegdec: fix SOF check in EOI
[ffmpeg] / libavfilter / af_aiir.c
index 4b6e867819ab8b6434bfdac6f540f216657f0933..a95f66d6bcf8742fd7b18227a81e652837cf6055 100644 (file)
@@ -40,8 +40,7 @@ typedef struct Pair {
 typedef struct BiquadContext {
     double a[3];
     double b[3];
-    double i1, i2;
-    double o1, o2;
+    double w1, w2;
 } BiquadContext;
 
 typedef struct IIRChannel {
@@ -177,8 +176,9 @@ IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
 IIR_CH(fltp, float,         -1.,        1., 0)
 IIR_CH(dblp, double,        -1.,        1., 0)
 
-#define SERIAL_IIR_CH(name, type, min, max, need_clipping)                  \
-static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)  \
+#define SERIAL_IIR_CH(name, type, min, max, need_clipping)              \
+static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg,       \
+                                 int ch, int nb_jobs)                   \
 {                                                                       \
     AudioIIRContext *s = ctx->priv;                                     \
     const double ig = s->dry_gain;                                      \
@@ -195,25 +195,21 @@ static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg, int ch, int nb
     int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2;     \
     int n, i;                                                           \
                                                                         \
-    for (i = 0; i < nb_biquads; i++) {                                  \
+    for (i = nb_biquads - 1; i >= 0; i--) {                             \
         const double a1 = -iir->biquads[i].a[1];                        \
         const double a2 = -iir->biquads[i].a[2];                        \
         const double b0 = iir->biquads[i].b[0];                         \
         const double b1 = iir->biquads[i].b[1];                         \
         const double b2 = iir->biquads[i].b[2];                         \
-        double i1 = iir->biquads[i].i1;                                 \
-        double i2 = iir->biquads[i].i2;                                 \
-        double o1 = iir->biquads[i].o1;                                 \
-        double o2 = iir->biquads[i].o2;                                 \
+        double w1 = iir->biquads[i].w1;                                 \
+        double w2 = iir->biquads[i].w2;                                 \
                                                                         \
         for (n = 0; n < in->nb_samples; n++) {                          \
             double i0 = ig * (i ? dst[n] : src[n]);                     \
-            double o0 = i0 * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
+            double o0 = i0 * b0 + w1;                                   \
                                                                         \
-            i2 = i1;                                                    \
-            i1 = i0;                                                    \
-            o2 = o1;                                                    \
-            o1 = o0;                                                    \
+            w1 = b1 * i0 + w2 + a1 * o0;                                \
+            w2 = b2 * i0 + a2 * o0;                                     \
             o0 *= og * g;                                               \
                                                                         \
             o0 = o0 * mix + imix * i0;                                  \
@@ -227,10 +223,8 @@ static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg, int ch, int nb
                 dst[n] = o0;                                            \
             }                                                           \
         }                                                               \
-        iir->biquads[i].i1 = i1;                                        \
-        iir->biquads[i].i2 = i2;                                        \
-        iir->biquads[i].o1 = o1;                                        \
-        iir->biquads[i].o2 = o2;                                        \
+        iir->biquads[i].w1 = w1;                                        \
+        iir->biquads[i].w2 = w2;                                        \
     }                                                                   \
                                                                         \
     return 0;                                                           \
@@ -266,19 +260,15 @@ static int iir_ch_parallel_## name(AVFilterContext *ctx, void *arg,     \
         const double a2 = -iir->biquads[i].a[2];                        \
         const double b1 = iir->biquads[i].b[1];                         \
         const double b2 = iir->biquads[i].b[2];                         \
-        double i1 = iir->biquads[i].i1;                                 \
-        double i2 = iir->biquads[i].i2;                                 \
-        double o1 = iir->biquads[i].o1;                                 \
-        double o2 = iir->biquads[i].o2;                                 \
+        double w1 = iir->biquads[i].w1;                                 \
+        double w2 = iir->biquads[i].w2;                                 \
                                                                         \
         for (n = 0; n < in->nb_samples; n++) {                          \
             double i0 = ig * src[n];                                    \
-            double o0 = i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2;          \
+            double o0 = w1;                                             \
                                                                         \
-            i2 = i1;                                                    \
-            i1 = i0;                                                    \
-            o2 = o1;                                                    \
-            o1 = o0;                                                    \
+            w1 = b1 * i0 + w2 + a1 * o0;                                \
+            w2 = b2 * i0 + a2 * o0;                                     \
             o0 *= og * g;                                               \
             o0 += dst[n];                                               \
                                                                         \
@@ -292,10 +282,8 @@ static int iir_ch_parallel_## name(AVFilterContext *ctx, void *arg,     \
                 dst[n] = o0;                                            \
             }                                                           \
         }                                                               \
-        iir->biquads[i].i1 = i1;                                        \
-        iir->biquads[i].i2 = i2;                                        \
-        iir->biquads[i].o1 = o1;                                        \
-        iir->biquads[i].o2 = o2;                                        \
+        iir->biquads[i].w1 = w1;                                        \
+        iir->biquads[i].w2 = w2;                                        \
     }                                                                   \
                                                                         \
     for (n = 0; n < in->nb_samples; n++) {                              \
@@ -311,6 +299,63 @@ PARALLEL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
 PARALLEL_IIR_CH(fltp, float,         -1.,        1., 0)
 PARALLEL_IIR_CH(dblp, double,        -1.,        1., 0)
 
+#define LATTICE_IIR_CH(name, type, min, max, need_clipping)             \
+static int iir_ch_lattice_## name(AVFilterContext *ctx, void *arg,      \
+                                  int ch, int nb_jobs)                  \
+{                                                                       \
+    AudioIIRContext *s = ctx->priv;                                     \
+    const double ig = s->dry_gain;                                      \
+    const double og = s->wet_gain;                                      \
+    const double mix = s->mix;                                          \
+    ThreadData *td = arg;                                               \
+    AVFrame *in = td->in, *out = td->out;                               \
+    const type *src = (const type *)in->extended_data[ch];              \
+    double n0, n1, p0, *x = (double *)s->iir[ch].cache[0];              \
+    const int nb_stages = s->iir[ch].nb_ab[1];                          \
+    const double *v = s->iir[ch].ab[0];                                 \
+    const double *k = s->iir[ch].ab[1];                                 \
+    const double g = s->iir[ch].g;                                      \
+    int *clippings = &s->iir[ch].clippings;                             \
+    type *dst = (type *)out->extended_data[ch];                         \
+    int n;                                                              \
+                                                                        \
+    for (n = 0; n < in->nb_samples; n++) {                              \
+        const double in = src[n] * ig;                                  \
+        double out = 0.;                                                \
+                                                                        \
+        n1 = in;                                                        \
+        for (int i = nb_stages - 1; i >= 0; i--) {                      \
+            n0 = n1 - k[i] * x[i];                                      \
+            p0 = n0 * k[i] + x[i];                                      \
+            out += p0 * v[i+1];                                         \
+            x[i] = p0;                                                  \
+            n1 = n0;                                                    \
+        }                                                               \
+                                                                        \
+        out += n1 * v[0];                                               \
+        memmove(&x[1], &x[0], nb_stages * sizeof(*x));                  \
+        x[0] = n1;                                                      \
+        out *= og * g;                                                  \
+        out = out * mix + in * (1. - mix);                              \
+        if (need_clipping && out < min) {                               \
+            (*clippings)++;                                             \
+            dst[n] = min;                                               \
+        } else if (need_clipping && out > max) {                        \
+            (*clippings)++;                                             \
+            dst[n] = max;                                               \
+        } else {                                                        \
+            dst[n] = out;                                               \
+        }                                                               \
+    }                                                                   \
+                                                                        \
+    return 0;                                                           \
+}
+
+LATTICE_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
+LATTICE_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
+LATTICE_IIR_CH(fltp, float,         -1.,        1., 0)
+LATTICE_IIR_CH(dblp, double,        -1.,        1., 0)
+
 static void count_coefficients(char *item_str, int *nb_items)
 {
     char *p;
@@ -344,7 +389,7 @@ static int read_gains(AVFilterContext *ctx, char *item_str, int nb_items)
         }
 
         p = NULL;
-        if (sscanf(arg, "%lf", &s->iir[i].g) != 1) {
+        if (av_sscanf(arg, "%lf", &s->iir[i].g) != 1) {
             av_log(ctx, AV_LOG_ERROR, "Invalid gains supplied: %s\n", arg);
             av_freep(&old_str);
             return AVERROR(EINVAL);
@@ -371,7 +416,7 @@ static int read_tf_coefficients(AVFilterContext *ctx, char *item_str, int nb_ite
             break;
 
         p = NULL;
-        if (sscanf(arg, "%lf", &dst[i]) != 1) {
+        if (av_sscanf(arg, "%lf", &dst[i]) != 1) {
             av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
             av_freep(&old_str);
             return AVERROR(EINVAL);
@@ -396,7 +441,7 @@ static int read_zp_coefficients(AVFilterContext *ctx, char *item_str, int nb_ite
             break;
 
         p = NULL;
-        if (sscanf(arg, format, &dst[i*2], &dst[i*2+1]) != 2) {
+        if (av_sscanf(arg, format, &dst[i*2], &dst[i*2+1]) != 2) {
             av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg);
             av_freep(&old_str);
             return AVERROR(EINVAL);
@@ -408,7 +453,7 @@ static int read_zp_coefficients(AVFilterContext *ctx, char *item_str, int nb_ite
     return 0;
 }
 
-static const char *format[] = { "%lf", "%lf %lfi", "%lf %lfr", "%lf %lfd", "%lf %lfi" };
+static const char *const format[] = { "%lf", "%lf %lfi", "%lf %lfr", "%lf %lfd", "%lf %lfi" };
 
 static int read_channels(AVFilterContext *ctx, int channels, uint8_t *item_str, int ab)
 {
@@ -440,7 +485,7 @@ static int read_channels(AVFilterContext *ctx, int channels, uint8_t *item_str,
             return AVERROR(ENOMEM);
         }
 
-        if (s->format) {
+        if (s->format > 0) {
             ret = read_zp_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab], format[s->format]);
         } else {
             ret = read_tf_coefficients(ctx, arg, iir->nb_ab[ab], iir->ab[ab]);
@@ -889,27 +934,75 @@ static void convert_sp2zp(AVFilterContext *ctx, int channels)
         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;
+            iir->ab[0][2*n]   = exp(sr) * cos(si);
+            iir->ab[0][2*n+1] = exp(sr) * sin(si);
         }
 
         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;
+            iir->ab[1][2*n]   = exp(sr) * cos(si);
+            iir->ab[1][2*n+1] = exp(sr) * sin(si);
         }
     }
 }
 
+static double fact(double i)
+{
+    if (i <= 0.)
+        return 1.;
+    return i * fact(i - 1.);
+}
+
+static double coef_sf2zf(double *a, int N, int n)
+{
+    double z = 0.;
+
+    for (int i = 0; i <= N; i++) {
+        double acc = 0.;
+
+        for (int k = FFMAX(n - N + i, 0); k <= FFMIN(i, n); k++) {
+            acc += ((fact(i) * fact(N - i)) /
+                    (fact(k) * fact(i - k) * fact(n - k) * fact(N - i - n + k))) *
+                   ((k & 1) ? -1. : 1.);
+        }
+
+        z += a[i] * pow(2., i) * acc;
+    }
+
+    return z;
+}
+
+static void convert_sf2tf(AVFilterContext *ctx, int channels)
+{
+    AudioIIRContext *s = ctx->priv;
+    int ch;
+
+    for (ch = 0; ch < channels; ch++) {
+        IIRChannel *iir = &s->iir[ch];
+        double *temp0 = av_calloc(iir->nb_ab[0], sizeof(*temp0));
+        double *temp1 = av_calloc(iir->nb_ab[1], sizeof(*temp1));
+
+        if (!temp0 || !temp1)
+            goto next;
+
+        memcpy(temp0, iir->ab[0], iir->nb_ab[0] * sizeof(*temp0));
+        memcpy(temp1, iir->ab[1], iir->nb_ab[1] * sizeof(*temp1));
+
+        for (int n = 0; n < iir->nb_ab[0]; n++)
+            iir->ab[0][n] = coef_sf2zf(temp0, iir->nb_ab[0] - 1, n);
+
+        for (int n = 0; n < iir->nb_ab[1]; n++)
+            iir->ab[1][n] = coef_sf2zf(temp1, iir->nb_ab[1] - 1, n);
+
+next:
+        av_free(temp0);
+        av_free(temp1);
+    }
+}
+
 static void convert_pd2zp(AVFilterContext *ctx, int channels)
 {
     AudioIIRContext *s = ctx->priv;
@@ -1198,7 +1291,10 @@ static int config_output(AVFilterLink *outlink)
     if (ret < 0)
         return ret;
 
-    if (s->format == 2) {
+    if (s->format == -1) {
+        convert_sf2tf(ctx, inlink->channels);
+        s->format = 0;
+    } else if (s->format == 2) {
         convert_pr2zp(ctx, inlink->channels);
     } else if (s->format == 3) {
         convert_pd2zp(ctx, inlink->channels);
@@ -1219,7 +1315,7 @@ static int config_output(AVFilterLink *outlink)
     }
 
     if (s->format == 0)
-        av_log(ctx, AV_LOG_WARNING, "tf coefficients format is not recommended for too high number of zeros/poles.\n");
+        av_log(ctx, AV_LOG_WARNING, "transfer function coefficients format is not recommended for too high number of zeros/poles.\n");
 
     if (s->format > 0 && s->process == 0) {
         av_log(ctx, AV_LOG_WARNING, "Direct processsing is not recommended for zp coefficients format.\n");
@@ -1227,10 +1323,13 @@ static int config_output(AVFilterLink *outlink)
         ret = convert_zp2tf(ctx, inlink->channels);
         if (ret < 0)
             return ret;
-    } else if (s->format == 0 && s->process == 1) {
+    } else if (s->format == -2 && s->process > 0) {
+        av_log(ctx, AV_LOG_ERROR, "Only direct processing is implemented for lattice-ladder function.\n");
+        return AVERROR_PATCHWELCOME;
+    } else if (s->format <= 0 && s->process == 1) {
         av_log(ctx, AV_LOG_ERROR, "Serial processing is not implemented for transfer function.\n");
         return AVERROR_PATCHWELCOME;
-    } else if (s->format == 0 && s->process == 2) {
+    } else if (s->format <= 0 && s->process == 2) {
         av_log(ctx, AV_LOG_ERROR, "Parallel processing is not implemented for transfer function.\n");
         return AVERROR_PATCHWELCOME;
     } else if (s->format > 0 && s->process == 1) {
@@ -1238,6 +1337,8 @@ static int config_output(AVFilterLink *outlink)
         if (ret < 0)
             return ret;
     } else if (s->format > 0 && s->process == 2) {
+        if (s->precision > 1)
+            av_log(ctx, AV_LOG_WARNING, "Parallel processing is not recommended for fixed-point precisions.\n");
         ret = decompose_zp2biquads(ctx, inlink->channels);
         if (ret < 0)
             return ret;
@@ -1246,6 +1347,15 @@ static int config_output(AVFilterLink *outlink)
             return ret;
     }
 
+    for (ch = 0; s->format == -2 && ch < inlink->channels; ch++) {
+        IIRChannel *iir = &s->iir[ch];
+
+        if (iir->nb_ab[0] != iir->nb_ab[1] + 1) {
+            av_log(ctx, AV_LOG_ERROR, "Number of ladder coefficients must be one more than number of reflection coefficients.\n");
+            return AVERROR(EINVAL);
+        }
+    }
+
     for (ch = 0; s->format == 0 && ch < inlink->channels; ch++) {
         IIRChannel *iir = &s->iir[ch];
 
@@ -1268,6 +1378,15 @@ static int config_output(AVFilterLink *outlink)
     case AV_SAMPLE_FMT_S16P: s->iir_channel = s->process == 2 ? iir_ch_parallel_s16p : s->process == 1 ? iir_ch_serial_s16p : iir_ch_s16p; break;
     }
 
+    if (s->format == -2) {
+        switch (inlink->format) {
+        case AV_SAMPLE_FMT_DBLP: s->iir_channel = iir_ch_lattice_dblp; break;
+        case AV_SAMPLE_FMT_FLTP: s->iir_channel = iir_ch_lattice_fltp; break;
+        case AV_SAMPLE_FMT_S32P: s->iir_channel = iir_ch_lattice_s32p; break;
+        case AV_SAMPLE_FMT_S16P: s->iir_channel = iir_ch_lattice_s16p; break;
+        }
+    }
+
     return 0;
 }
 
@@ -1418,16 +1537,18 @@ static const AVFilterPad inputs[] = {
 #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 },
+    { "zeros", "set B/numerator/zeros/reflection coefficients", OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
+    { "z", "set B/numerator/zeros/reflection coefficients",     OFFSET(b_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
+    { "poles", "set A/denominator/poles/ladder coefficients",   OFFSET(a_str), AV_OPT_TYPE_STRING, {.str="1+0i 1-0i"}, 0, 0, AF },
+    { "p", "set A/denominator/poles/ladder 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 },
-    { "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" },
+    { "format", "set coefficients format",         OFFSET(format),   AV_OPT_TYPE_INT,    {.i64=1},    -2, 4, AF, "format" },
+    { "f", "set coefficients format",              OFFSET(format),   AV_OPT_TYPE_INT,    {.i64=1},    -2, 4, AF, "format" },
+    { "ll", "lattice-ladder function",             0,                AV_OPT_TYPE_CONST,  {.i64=-2},    0, 0, AF, "format" },
+    { "sf", "analog transfer function",            0,                AV_OPT_TYPE_CONST,  {.i64=-1},    0, 0, 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" },
@@ -1456,7 +1577,7 @@ static const AVOption aiir_options[] = {
 
 AVFILTER_DEFINE_CLASS(aiir);
 
-AVFilter ff_af_aiir = {
+const AVFilter ff_af_aiir = {
     .name          = "aiir",
     .description   = NULL_IF_CONFIG_SMALL("Apply Infinite Impulse Response filter with supplied coefficients."),
     .priv_size     = sizeof(AudioIIRContext),