aboutsummaryrefslogtreecommitdiffstats
path: root/libavfilter/af_arls.c
diff options
context:
space:
mode:
authorPaul B Mahol <onemda@gmail.com>2023-11-27 20:13:31 +0100
committerPaul B Mahol <onemda@gmail.com>2023-11-27 20:27:27 +0100
commit3bca828d397251ea363988842a9df4a75df83135 (patch)
tree1c85fd8ac918579fbd4442723120c488737974b9 /libavfilter/af_arls.c
parent42e45ea8ff30608fb4a86f247a2e4553ff6bf8fe (diff)
downloadffmpeg-3bca828d397251ea363988842a9df4a75df83135.tar.gz
avfilter/af_arls: add double sample format support
Diffstat (limited to 'libavfilter/af_arls.c')
-rw-r--r--libavfilter/af_arls.c158
1 files changed, 50 insertions, 108 deletions
diff --git a/libavfilter/af_arls.c b/libavfilter/af_arls.c
index 4f2eddffc4..85e4f92425 100644
--- a/libavfilter/af_arls.c
+++ b/libavfilter/af_arls.c
@@ -24,6 +24,7 @@
#include "audio.h"
#include "avfilter.h"
+#include "formats.h"
#include "filters.h"
#include "internal.h"
@@ -43,6 +44,7 @@ typedef struct AudioRLSContext {
float lambda;
float delta;
int output_mode;
+ int precision;
int kernel_size;
AVFrame *offset;
@@ -54,6 +56,8 @@ typedef struct AudioRLSContext {
AVFrame *frame[2];
+ int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
+
AVFloatDSPContext *fdsp;
} AudioRLSContext;
@@ -71,117 +75,32 @@ static const AVOption arls_options[] = {
{ "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AT, "mode" },
{ "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE}, 0, 0, AT, "mode" },
{ "e", "error", 0, AV_OPT_TYPE_CONST, {.i64=ERROR_MODE}, 0, 0, AT, "mode" },
+ { "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, A, "precision" },
+ { "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "precision" },
+ { "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "precision" },
+ { "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, "precision" },
{ NULL }
};
AVFILTER_DEFINE_CLASS(arls);
-static float fir_sample(AudioRLSContext *s, float sample, float *delay,
- float *coeffs, float *tmp, int *offset)
-{
- const int order = s->order;
- float output;
-
- delay[*offset] = sample;
-
- memcpy(tmp, coeffs + order - *offset, order * sizeof(float));
-
- output = s->fdsp->scalarproduct_float(delay, tmp, s->kernel_size);
-
- if (--(*offset) < 0)
- *offset = order - 1;
-
- return output;
-}
-
-static float process_sample(AudioRLSContext *s, float input, float desired, int ch)
+static int query_formats(AVFilterContext *ctx)
{
- float *coeffs = (float *)s->coeffs->extended_data[ch];
- float *delay = (float *)s->delay->extended_data[ch];
- float *gains = (float *)s->gains->extended_data[ch];
- float *tmp = (float *)s->tmp->extended_data[ch];
- float *u = (float *)s->u->extended_data[ch];
- float *p = (float *)s->p->extended_data[ch];
- float *dp = (float *)s->dp->extended_data[ch];
- int *offsetp = (int *)s->offset->extended_data[ch];
- const int kernel_size = s->kernel_size;
- const int order = s->order;
- const float lambda = s->lambda;
- int offset = *offsetp;
- float g = lambda;
- float output, e;
-
- delay[offset + order] = input;
-
- output = fir_sample(s, input, delay, coeffs, tmp, offsetp);
- e = desired - output;
-
- for (int i = 0, pos = offset; i < order; i++, pos++) {
- const int ikernel_size = i * kernel_size;
-
- u[i] = 0.f;
- for (int k = 0, pos = offset; k < order; k++, pos++)
- u[i] += p[ikernel_size + k] * delay[pos];
-
- g += u[i] * delay[pos];
- }
-
- g = 1.f / g;
-
- for (int i = 0; i < order; i++) {
- const int ikernel_size = i * kernel_size;
-
- gains[i] = u[i] * g;
- coeffs[i] = coeffs[order + i] = coeffs[i] + gains[i] * e;
- tmp[i] = 0.f;
- for (int k = 0, pos = offset; k < order; k++, pos++)
- tmp[i] += p[ikernel_size + k] * delay[pos];
- }
-
- for (int i = 0; i < order; i++) {
- const int ikernel_size = i * kernel_size;
-
- for (int k = 0; k < order; k++)
- dp[ikernel_size + k] = gains[i] * tmp[k];
- }
-
- for (int i = 0; i < order; i++) {
- const int ikernel_size = i * kernel_size;
-
- for (int k = 0; k < order; k++)
- p[ikernel_size + k] = (p[ikernel_size + k] - (dp[ikernel_size + k] + dp[kernel_size * k + i]) * 0.5f) * lambda;
- }
+ AudioRLSContext *s = ctx->priv;
+ static const enum AVSampleFormat sample_fmts[3][3] = {
+ { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
+ { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
+ { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
+ };
+ int ret;
- switch (s->output_mode) {
- case IN_MODE: output = input; break;
- case DESIRED_MODE: output = desired; break;
- case OUT_MODE: output = desired - output; break;
- case NOISE_MODE: output = input - output; break;
- case ERROR_MODE: break;
- }
- return output;
-}
+ if ((ret = ff_set_common_all_channel_counts(ctx)) < 0)
+ return ret;
-static int process_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
-{
- AudioRLSContext *s = ctx->priv;
- AVFrame *out = arg;
- const int start = (out->ch_layout.nb_channels * jobnr) / nb_jobs;
- const int end = (out->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
-
- for (int c = start; c < end; c++) {
- const float *input = (const float *)s->frame[0]->extended_data[c];
- const float *desired = (const float *)s->frame[1]->extended_data[c];
- float *output = (float *)out->extended_data[c];
-
- for (int n = 0; n < out->nb_samples; n++) {
- output[n] = process_sample(s, input[n], desired[n], c);
- if (ctx->is_disabled)
- output[n] = input[n];
- }
- }
+ if ((ret = ff_set_common_formats_from_list(ctx, sample_fmts[s->precision])) < 0)
+ return ret;
- return 0;
+ return ff_set_common_all_samplerates(ctx);
}
static int activate(AVFilterContext *ctx)
@@ -216,7 +135,7 @@ static int activate(AVFilterContext *ctx)
return AVERROR(ENOMEM);
}
- ff_filter_execute(ctx, process_channels, out, NULL,
+ ff_filter_execute(ctx, s->filter_channels, out, NULL,
FFMIN(ctx->outputs[0]->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
out->pts = s->frame[0]->pts;
@@ -249,6 +168,13 @@ static int activate(AVFilterContext *ctx)
return 0;
}
+#define DEPTH 32
+#include "arls_template.c"
+
+#undef DEPTH
+#define DEPTH 64
+#include "arls_template.c"
+
static int config_output(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
@@ -283,11 +209,27 @@ static int config_output(AVFilterLink *outlink)
dst[0] = s->kernel_size - 1;
}
- for (int ch = 0; ch < s->p->ch_layout.nb_channels; ch++) {
- float *dst = (float *)s->p->extended_data[ch];
+ switch (outlink->format) {
+ case AV_SAMPLE_FMT_DBLP:
+ for (int ch = 0; ch < s->p->ch_layout.nb_channels; ch++) {
+ double *dst = (double *)s->p->extended_data[ch];
- for (int i = 0; i < s->kernel_size; i++)
- dst[i * s->kernel_size + i] = s->delta;
+ for (int i = 0; i < s->kernel_size; i++)
+ dst[i * s->kernel_size + i] = s->delta;
+ }
+
+ s->filter_channels = filter_channels_double;
+ break;
+ case AV_SAMPLE_FMT_FLTP:
+ for (int ch = 0; ch < s->p->ch_layout.nb_channels; ch++) {
+ float *dst = (float *)s->p->extended_data[ch];
+
+ for (int i = 0; i < s->kernel_size; i++)
+ dst[i * s->kernel_size + i] = s->delta;
+ }
+
+ s->filter_channels = filter_channels_float;
+ break;
}
return 0;
@@ -348,7 +290,7 @@ const AVFilter ff_af_arls = {
.activate = activate,
FILTER_INPUTS(inputs),
FILTER_OUTPUTS(outputs),
- FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP),
+ FILTER_QUERY_FUNC(query_formats),
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
AVFILTER_FLAG_SLICE_THREADS,
.process_command = ff_filter_process_command,