]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/af_biquads.c
avfilter/af_biquads: add slice threading
[ffmpeg] / libavfilter / af_biquads.c
index d5c3823e6446ccf52fd0045da5c207d4e6be997c..56d3035d77418383ed405da543db812012653143 100644 (file)
@@ -78,6 +78,8 @@ enum FilterType {
     allpass,
     highpass,
     lowpass,
+    lowshelf,
+    highshelf,
 };
 
 enum WidthType {
@@ -93,6 +95,7 @@ enum WidthType {
 typedef struct ChanCache {
     double i1, i2;
     double o1, o2;
+    int clippings;
 } ChanCache;
 
 typedef struct BiquadsContext {
@@ -112,12 +115,11 @@ typedef struct BiquadsContext {
     double b0, b1, b2;
 
     ChanCache *cache;
-    int clippings;
     int block_align;
 
     void (*filter)(struct BiquadsContext *s, const void *ibuf, void *obuf, int len,
                    double *i1, double *i2, double *o1, double *o2,
-                   double b0, double b1, double b2, double a1, double a2);
+                   double b0, double b1, double b2, double a1, double a2, int *clippings);
 } BiquadsContext;
 
 static av_cold int init(AVFilterContext *ctx)
@@ -174,7 +176,7 @@ static void biquad_## name (BiquadsContext *s,                                \
                             double *in1, double *in2,                         \
                             double *out1, double *out2,                       \
                             double b0, double b1, double b2,                  \
-                            double a1, double a2)                             \
+                            double a1, double a2, int *clippings)             \
 {                                                                             \
     const type *ibuf = input;                                                 \
     type *obuf = output;                                                      \
@@ -190,10 +192,10 @@ static void biquad_## name (BiquadsContext *s,                                \
         o2 = i2 * b2 + i1 * b1 + ibuf[i] * b0 + o2 * a2 + o1 * a1;            \
         i2 = ibuf[i];                                                         \
         if (need_clipping && o2 < min) {                                      \
-            s->clippings++;                                                   \
+            (*clippings)++;                                                   \
             obuf[i] = min;                                                    \
         } else if (need_clipping && o2 > max) {                               \
-            s->clippings++;                                                   \
+            (*clippings)++;                                                   \
             obuf[i] = max;                                                    \
         } else {                                                              \
             obuf[i] = o2;                                                     \
@@ -202,10 +204,10 @@ static void biquad_## name (BiquadsContext *s,                                \
         o1 = i1 * b2 + i2 * b1 + ibuf[i] * b0 + o1 * a2 + o2 * a1;            \
         i1 = ibuf[i];                                                         \
         if (need_clipping && o1 < min) {                                      \
-            s->clippings++;                                                   \
+            (*clippings)++;                                                   \
             obuf[i] = min;                                                    \
         } else if (need_clipping && o1 > max) {                               \
-            s->clippings++;                                                   \
+            (*clippings)++;                                                   \
             obuf[i] = max;                                                    \
         } else {                                                              \
             obuf[i] = o1;                                                     \
@@ -218,10 +220,10 @@ static void biquad_## name (BiquadsContext *s,                                \
         o2 = o1;                                                              \
         o1 = o0;                                                              \
         if (need_clipping && o0 < min) {                                      \
-            s->clippings++;                                                   \
+            (*clippings)++;                                                   \
             obuf[i] = min;                                                    \
         } else if (need_clipping && o0 > max) {                               \
-            s->clippings++;                                                   \
+            (*clippings)++;                                                   \
             obuf[i] = max;                                                    \
         } else {                                                              \
             obuf[i] = o0;                                                     \
@@ -245,7 +247,7 @@ static int config_filter(AVFilterLink *outlink, int reset)
     AVFilterLink *inlink    = ctx->inputs[0];
     double A = exp(s->gain / 40 * log(10.));
     double w0 = 2 * M_PI * s->frequency / inlink->sample_rate;
-    double alpha;
+    double alpha, beta;
 
     if (w0 > M_PI) {
         av_log(ctx, AV_LOG_ERROR,
@@ -277,6 +279,8 @@ static int config_filter(AVFilterLink *outlink, int reset)
         av_assert0(0);
     }
 
+    beta = 2 * sqrt(A);
+
     switch (s->filter_type) {
     case biquad:
         break;
@@ -289,20 +293,24 @@ static int config_filter(AVFilterLink *outlink, int reset)
         s->b2 =   1 - alpha * A;
         break;
     case bass:
-        s->a0 =          (A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
+        beta = sqrt((A * A + 1) - (A - 1) * (A - 1));
+    case lowshelf:
+        s->a0 =          (A + 1) + (A - 1) * cos(w0) + beta * alpha;
         s->a1 =    -2 * ((A - 1) + (A + 1) * cos(w0));
-        s->a2 =          (A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
-        s->b0 =     A * ((A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
+        s->a2 =          (A + 1) + (A - 1) * cos(w0) - beta * alpha;
+        s->b0 =     A * ((A + 1) - (A - 1) * cos(w0) + beta * alpha);
         s->b1 = 2 * A * ((A - 1) - (A + 1) * cos(w0));
-        s->b2 =     A * ((A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
+        s->b2 =     A * ((A + 1) - (A - 1) * cos(w0) - beta * alpha);
         break;
     case treble:
-        s->a0 =          (A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
+        beta = sqrt((A * A + 1) - (A - 1) * (A - 1));
+    case highshelf:
+        s->a0 =          (A + 1) - (A - 1) * cos(w0) + beta * alpha;
         s->a1 =     2 * ((A - 1) - (A + 1) * cos(w0));
-        s->a2 =          (A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
-        s->b0 =     A * ((A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
+        s->a2 =          (A + 1) - (A - 1) * cos(w0) - beta * alpha;
+        s->b0 =     A * ((A + 1) + (A - 1) * cos(w0) + beta * alpha);
         s->b1 =-2 * A * ((A - 1) + (A + 1) * cos(w0));
-        s->b2 =     A * ((A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
+        s->b2 =     A * ((A + 1) + (A - 1) * cos(w0) - beta * alpha);
         break;
     case bandpass:
         if (s->csg) {
@@ -408,19 +416,50 @@ static int config_output(AVFilterLink *outlink)
     return config_filter(outlink, 1);
 }
 
+typedef struct ThreadData {
+    AVFrame *in, *out;
+} ThreadData;
+
+static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    AVFilterLink *inlink = ctx->inputs[0];
+    ThreadData *td = arg;
+    AVFrame *buf = td->in;
+    AVFrame *out_buf = td->out;
+    BiquadsContext *s = ctx->priv;
+    const int start = (buf->channels * jobnr) / nb_jobs;
+    const int end = (buf->channels * (jobnr+1)) / nb_jobs;
+    int ch;
+
+    for (ch = start; ch < end; ch++) {
+        if (!((av_channel_layout_extract_channel(inlink->channel_layout, ch) & s->channels))) {
+            if (buf != out_buf)
+                memcpy(out_buf->extended_data[ch], buf->extended_data[ch],
+                       buf->nb_samples * s->block_align);
+            continue;
+        }
+
+        s->filter(s, buf->extended_data[ch], out_buf->extended_data[ch], buf->nb_samples,
+                  &s->cache[ch].i1, &s->cache[ch].i2, &s->cache[ch].o1, &s->cache[ch].o2,
+                  s->b0, s->b1, s->b2, s->a1, s->a2, &s->cache[ch].clippings);
+    }
+
+    return 0;
+}
+
 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     AVFilterContext  *ctx = inlink->dst;
     BiquadsContext *s     = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
     AVFrame *out_buf;
-    int nb_samples = buf->nb_samples;
+    ThreadData td;
     int ch;
 
     if (av_frame_is_writable(buf)) {
         out_buf = buf;
     } else {
-        out_buf = ff_get_audio_buffer(outlink, nb_samples);
+        out_buf = ff_get_audio_buffer(outlink, buf->nb_samples);
         if (!out_buf) {
             av_frame_free(&buf);
             return AVERROR(ENOMEM);
@@ -428,22 +467,16 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
         av_frame_copy_props(out_buf, buf);
     }
 
-    for (ch = 0; ch < buf->channels; ch++) {
-        if (!((av_channel_layout_extract_channel(inlink->channel_layout, ch) & s->channels))) {
-            if (buf != out_buf)
-                memcpy(out_buf->extended_data[ch], buf->extended_data[ch], nb_samples * s->block_align);
-            continue;
-        }
-        s->filter(s, buf->extended_data[ch],
-                  out_buf->extended_data[ch], nb_samples,
-                  &s->cache[ch].i1, &s->cache[ch].i2,
-                  &s->cache[ch].o1, &s->cache[ch].o2,
-                  s->b0, s->b1, s->b2, s->a1, s->a2);
-    }
+    td.in = buf;
+    td.out = out_buf;
+    ctx->internal->execute(ctx, filter_channel, &td, NULL, FFMIN(outlink->channels, ff_filter_get_nb_threads(ctx)));
 
-    if (s->clippings > 0)
-        av_log(ctx, AV_LOG_WARNING, "clipping %d times. Please reduce gain.\n", s->clippings);
-    s->clippings = 0;
+    for (ch = 0; ch < outlink->channels; ch++) {
+        if (s->cache[ch].clippings > 0)
+            av_log(ctx, AV_LOG_WARNING, "Channel %d clipping %d times. Please reduce gain.\n",
+                   ch, s->cache[ch].clippings);
+        s->cache[ch].clippings = 0;
+    }
 
     if (buf != out_buf)
         av_frame_free(&buf);
@@ -459,6 +492,8 @@ static int process_command(AVFilterContext *ctx, const char *cmd, const char *ar
 
     if ((!strcmp(cmd, "frequency") || !strcmp(cmd, "f")) &&
         (s->filter_type == equalizer ||
+         s->filter_type == lowshelf  ||
+         s->filter_type == highshelf ||
          s->filter_type == bass      ||
          s->filter_type == treble    ||
          s->filter_type == bandpass  ||
@@ -476,6 +511,8 @@ static int process_command(AVFilterContext *ctx, const char *cmd, const char *ar
         s->frequency = freq;
     } else if ((!strcmp(cmd, "gain") || !strcmp(cmd, "g")) &&
         (s->filter_type == equalizer ||
+         s->filter_type == lowshelf  ||
+         s->filter_type == highshelf ||
          s->filter_type == bass      ||
          s->filter_type == treble)) {
         double gain;
@@ -488,6 +525,8 @@ static int process_command(AVFilterContext *ctx, const char *cmd, const char *ar
         s->gain = gain;
     } else if ((!strcmp(cmd, "width") || !strcmp(cmd, "w")) &&
         (s->filter_type == equalizer ||
+         s->filter_type == lowshelf  ||
+         s->filter_type == highshelf ||
          s->filter_type == bass      ||
          s->filter_type == treble    ||
          s->filter_type == bandpass  ||
@@ -505,6 +544,8 @@ static int process_command(AVFilterContext *ctx, const char *cmd, const char *ar
         s->width = width;
     } else if ((!strcmp(cmd, "width_type") || !strcmp(cmd, "t")) &&
         (s->filter_type == equalizer ||
+         s->filter_type == lowshelf  ||
+         s->filter_type == highshelf ||
          s->filter_type == bass      ||
          s->filter_type == treble    ||
          s->filter_type == bandpass  ||
@@ -611,6 +652,7 @@ AVFilter ff_af_##name_ = {                         \
     .outputs       = outputs,                            \
     .priv_class    = &name_##_class,                     \
     .process_command = process_command,                  \
+    .flags         = AVFILTER_FLAG_SLICE_THREADS,        \
 }
 
 #if CONFIG_EQUALIZER_FILTER
@@ -784,6 +826,50 @@ static const AVOption allpass_options[] = {
 
 DEFINE_BIQUAD_FILTER(allpass, "Apply a two-pole all-pass filter.");
 #endif  /* CONFIG_ALLPASS_FILTER */
+#if CONFIG_LOWSHELF_FILTER
+static const AVOption lowshelf_options[] = {
+    {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
+    {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
+    {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, NB_WTYPE-1, FLAGS, "width_type"},
+    {"t",          "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, NB_WTYPE-1, FLAGS, "width_type"},
+    {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
+    {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
+    {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
+    {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
+    {"k", "kHz", 0, AV_OPT_TYPE_CONST, {.i64=KHERTZ}, 0, 0, FLAGS, "width_type"},
+    {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
+    {"w",     "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
+    {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
+    {"g",    "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
+    {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
+    {"c",        "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
+    {NULL}
+};
+
+DEFINE_BIQUAD_FILTER(lowshelf, "Apply a low shelf filter.");
+#endif  /* CONFIG_LOWSHELF_FILTER */
+#if CONFIG_HIGHSHELF_FILTER
+static const AVOption highshelf_options[] = {
+    {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
+    {"f",         "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
+    {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, NB_WTYPE-1, FLAGS, "width_type"},
+    {"t",          "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, NB_WTYPE-1, FLAGS, "width_type"},
+    {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
+    {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
+    {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
+    {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
+    {"k", "kHz", 0, AV_OPT_TYPE_CONST, {.i64=KHERTZ}, 0, 0, FLAGS, "width_type"},
+    {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
+    {"w",     "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
+    {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
+    {"g",    "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
+    {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
+    {"c",        "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
+    {NULL}
+};
+
+DEFINE_BIQUAD_FILTER(highshelf, "Apply a high shelf filter.");
+#endif  /* CONFIG_HIGHSHELF_FILTER */
 #if CONFIG_BIQUAD_FILTER
 static const AVOption biquad_options[] = {
     {"a0", NULL, OFFSET(a0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT32_MIN, INT32_MAX, FLAGS},