avfilter/af_asubboost: add timeline and slice threading support
This commit is contained in:
parent
052a5993ad
commit
ee8ecc2730
1 changed files with 59 additions and 33 deletions
|
|
@ -37,7 +37,7 @@ typedef struct ASubBoostContext {
|
||||||
double a0, a1, a2;
|
double a0, a1, a2;
|
||||||
double b0, b1, b2;
|
double b0, b1, b2;
|
||||||
|
|
||||||
int write_pos;
|
int *write_pos;
|
||||||
int buffer_samples;
|
int buffer_samples;
|
||||||
|
|
||||||
AVFrame *w;
|
AVFrame *w;
|
||||||
|
|
@ -105,24 +105,67 @@ static int config_input(AVFilterLink *inlink)
|
||||||
|
|
||||||
s->buffer = ff_get_audio_buffer(inlink, inlink->sample_rate / 10);
|
s->buffer = ff_get_audio_buffer(inlink, inlink->sample_rate / 10);
|
||||||
s->w = ff_get_audio_buffer(inlink, 2);
|
s->w = ff_get_audio_buffer(inlink, 2);
|
||||||
if (!s->buffer || !s->w)
|
s->write_pos = av_calloc(inlink->channels, sizeof(*s->write_pos));
|
||||||
|
if (!s->buffer || !s->w || !s->write_pos)
|
||||||
return AVERROR(ENOMEM);
|
return AVERROR(ENOMEM);
|
||||||
|
|
||||||
return get_coeffs(ctx);
|
return get_coeffs(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef struct ThreadData {
|
||||||
|
AVFrame *in, *out;
|
||||||
|
} ThreadData;
|
||||||
|
|
||||||
|
static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
||||||
|
{
|
||||||
|
ASubBoostContext *s = ctx->priv;
|
||||||
|
ThreadData *td = arg;
|
||||||
|
AVFrame *out = td->out;
|
||||||
|
AVFrame *in = td->in;
|
||||||
|
const double wet = ctx->is_disabled ? 0. : s->wet_gain;
|
||||||
|
const double dry = ctx->is_disabled ? 1. : s->dry_gain;
|
||||||
|
const double feedback = s->feedback, decay = s->decay;
|
||||||
|
const double b0 = s->b0;
|
||||||
|
const double b1 = s->b1;
|
||||||
|
const double b2 = s->b2;
|
||||||
|
const double a1 = -s->a1;
|
||||||
|
const double a2 = -s->a2;
|
||||||
|
const int start = (in->channels * jobnr) / nb_jobs;
|
||||||
|
const int end = (in->channels * (jobnr+1)) / nb_jobs;
|
||||||
|
const int buffer_samples = s->buffer_samples;
|
||||||
|
|
||||||
|
for (int ch = start; ch < end; ch++) {
|
||||||
|
const double *src = (const double *)in->extended_data[ch];
|
||||||
|
double *dst = (double *)out->extended_data[ch];
|
||||||
|
double *buffer = (double *)s->buffer->extended_data[ch];
|
||||||
|
double *w = (double *)s->w->extended_data[ch];
|
||||||
|
int write_pos = s->write_pos[ch];
|
||||||
|
|
||||||
|
for (int n = 0; n < in->nb_samples; n++) {
|
||||||
|
double out_sample;
|
||||||
|
|
||||||
|
out_sample = src[n] * b0 + w[0];
|
||||||
|
w[0] = b1 * src[n] + w[1] + a1 * out_sample;
|
||||||
|
w[1] = b2 * src[n] + a2 * out_sample;
|
||||||
|
|
||||||
|
buffer[write_pos] = buffer[write_pos] * decay + out_sample * feedback;
|
||||||
|
dst[n] = src[n] * dry + buffer[write_pos] * wet;
|
||||||
|
|
||||||
|
if (++write_pos >= buffer_samples)
|
||||||
|
write_pos = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
s->write_pos[ch] = write_pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
||||||
{
|
{
|
||||||
AVFilterContext *ctx = inlink->dst;
|
AVFilterContext *ctx = inlink->dst;
|
||||||
AVFilterLink *outlink = ctx->outputs[0];
|
AVFilterLink *outlink = ctx->outputs[0];
|
||||||
ASubBoostContext *s = ctx->priv;
|
ThreadData td;
|
||||||
const double wet = s->wet_gain, dry = s->dry_gain, feedback = s->feedback, decay = s->decay;
|
|
||||||
const double b0 = s->b0;
|
|
||||||
const double b1 = s->b1;
|
|
||||||
const double b2 = s->b2;
|
|
||||||
const double a1 = -s->a1;
|
|
||||||
const double a2 = -s->a2;
|
|
||||||
int write_pos;
|
|
||||||
AVFrame *out;
|
AVFrame *out;
|
||||||
|
|
||||||
if (av_frame_is_writable(in)) {
|
if (av_frame_is_writable(in)) {
|
||||||
|
|
@ -136,29 +179,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
||||||
av_frame_copy_props(out, in);
|
av_frame_copy_props(out, in);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int ch = 0; ch < in->channels; ch++) {
|
td.in = in; td.out = out;
|
||||||
const double *src = (const double *)in->extended_data[ch];
|
ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(inlink->channels,
|
||||||
double *dst = (double *)out->extended_data[ch];
|
ff_filter_get_nb_threads(ctx)));
|
||||||
double *buffer = (double *)s->buffer->extended_data[ch];
|
|
||||||
double *w = (double *)s->w->extended_data[ch];
|
|
||||||
|
|
||||||
write_pos = s->write_pos;
|
|
||||||
for (int n = 0; n < in->nb_samples; n++) {
|
|
||||||
double out_sample;
|
|
||||||
|
|
||||||
out_sample = src[n] * b0 + w[0];
|
|
||||||
w[0] = b1 * src[n] + w[1] + a1 * out_sample;
|
|
||||||
w[1] = b2 * src[n] + a2 * out_sample;
|
|
||||||
|
|
||||||
buffer[write_pos] = buffer[write_pos] * decay + out_sample * feedback;
|
|
||||||
dst[n] = src[n] * dry + buffer[write_pos] * wet;
|
|
||||||
|
|
||||||
if (++write_pos >= s->buffer_samples)
|
|
||||||
write_pos = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s->write_pos = write_pos;
|
|
||||||
|
|
||||||
if (out != in)
|
if (out != in)
|
||||||
av_frame_free(&in);
|
av_frame_free(&in);
|
||||||
|
|
@ -171,6 +194,7 @@ static av_cold void uninit(AVFilterContext *ctx)
|
||||||
|
|
||||||
av_frame_free(&s->buffer);
|
av_frame_free(&s->buffer);
|
||||||
av_frame_free(&s->w);
|
av_frame_free(&s->w);
|
||||||
|
av_freep(&s->write_pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
|
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
|
||||||
|
|
@ -229,4 +253,6 @@ AVFilter ff_af_asubboost = {
|
||||||
.inputs = inputs,
|
.inputs = inputs,
|
||||||
.outputs = outputs,
|
.outputs = outputs,
|
||||||
.process_command = process_command,
|
.process_command = process_command,
|
||||||
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
|
||||||
|
AVFILTER_FLAG_SLICE_THREADS,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue