*/
#include "libavutil/avassert.h"
+#include "libavutil/ffmath.h"
#include "libavutil/opt.h"
#include "audio.h"
#include "avfilter.h"
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, beta;
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);
.outputs = outputs, \
.priv_class = &name_##_class, \
.process_command = process_command, \
+ .flags = AVFILTER_FLAG_SLICE_THREADS, \
}
#if CONFIG_EQUALIZER_FILTER