*/
#include "libavutil/avassert.h"
+#include "libavutil/ffmath.h"
#include "libavutil/opt.h"
#include "audio.h"
#include "avfilter.h"
allpass,
highpass,
lowpass,
+ lowshelf,
+ highshelf,
};
enum WidthType {
typedef struct ChanCache {
double i1, i2;
double o1, o2;
+ int clippings;
} ChanCache;
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)
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; \
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; \
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; \
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; \
AVFilterContext *ctx = outlink->src;
BiquadsContext *s = ctx->priv;
AVFilterLink *inlink = ctx->inputs[0];
- double A = exp(s->gain / 40 * log(10.));
+ double A = ff_exp10(s->gain / 40);
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,
av_assert0(0);
}
+ beta = 2 * sqrt(A);
+
switch (s->filter_type) {
case biquad:
break;
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) {
av_assert0(0);
}
- av_log(ctx, AV_LOG_VERBOSE, "a=%lf %lf %lf:b=%lf %lf %lf\n", s->a0, s->a1, s->a2, s->b0, s->b1, s->b2);
+ av_log(ctx, AV_LOG_VERBOSE, "a=%f %f %f:b=%f %f %f\n", s->a0, s->a1, s->a2, s->b0, s->b1, s->b2);
s->a1 /= s->a0;
s->a2 /= s->a0;
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);
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);
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 ||
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;
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 ||
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 ||
.outputs = outputs, \
.priv_class = &name_##_class, \
.process_command = process_command, \
+ .flags = AVFILTER_FLAG_SLICE_THREADS, \
}
#if CONFIG_EQUALIZER_FILTER
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},