X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Faf_crystalizer.c;h=f4ad326ede7af843083e6ce45db70a27a3bce452;hb=2606c48391377681541111263de41c79adeffa49;hp=5b27e1fb797dba29fa97fa5a36170c8377e8e49b;hpb=a024c3ce9a502849013a4aa2c0a6de0c9270261c;p=ffmpeg diff --git a/libavfilter/af_crystalizer.c b/libavfilter/af_crystalizer.c index 5b27e1fb797..f4ad326ede7 100644 --- a/libavfilter/af_crystalizer.c +++ b/libavfilter/af_crystalizer.c @@ -29,15 +29,14 @@ typedef struct CrystalizerContext { float mult; int clip; AVFrame *prev; - void (*filter)(void **dst, void **prv, const void **src, - int nb_samples, int channels, float mult, int clip); + int (*filter)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); } CrystalizerContext; #define OFFSET(x) offsetof(CrystalizerContext, x) -#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM +#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM static const AVOption crystalizer_options[] = { - { "i", "set intensity", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.0}, 0, 10, A }, + { "i", "set intensity", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.0},-10, 10, A }, { "c", "enable clipping", OFFSET(clip), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A }, { NULL } }; @@ -74,42 +73,71 @@ static int query_formats(AVFilterContext *ctx) return ff_set_common_samplerates(ctx, formats); } -static void filter_flt(void **d, void **p, const void **s, - int nb_samples, int channels, - float mult, int clip) +typedef struct ThreadData { + void **d; + void **p; + const void **s; + int nb_samples; + int channels; + float mult; + int clip; +} ThreadData; + +static int filter_flt(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) { - const float *src = s[0]; - float *dst = d[0]; + ThreadData *td = arg; + void **d = td->d; + void **p = td->p; + const void **s = td->s; + const int nb_samples = td->nb_samples; + const int channels = td->channels; + const float mult = td->mult; + const int clip = td->clip; + const int start = (channels * jobnr) / nb_jobs; + const int end = (channels * (jobnr+1)) / nb_jobs; float *prv = p[0]; int n, c; - for (n = 0; n < nb_samples; n++) { - for (c = 0; c < channels; c++) { - float current = src[c]; + for (c = start; c < end; c++) { + const float *src = s[0]; + float *dst = d[0]; + for (n = 0; n < nb_samples; n++) { + float current = src[c]; dst[c] = current + (current - prv[c]) * mult; prv[c] = current; if (clip) { dst[c] = av_clipf(dst[c], -1, 1); } - } - dst += c; - src += c; + dst += channels; + src += channels; + } } + + return 0; } -static void filter_dbl(void **d, void **p, const void **s, - int nb_samples, int channels, - float mult, int clip) +static int filter_dbl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) { - const double *src = s[0]; - double *dst = d[0]; + ThreadData *td = arg; + void **d = td->d; + void **p = td->p; + const void **s = td->s; + const int nb_samples = td->nb_samples; + const int channels = td->channels; + double mult = td->mult; + const int clip = td->clip; + const int start = (channels * jobnr) / nb_jobs; + const int end = (channels * (jobnr+1)) / nb_jobs; double *prv = p[0]; int n, c; - for (n = 0; n < nb_samples; n++) { - for (c = 0; c < channels; c++) { + for (c = start; c < end; c++) { + const double *src = s[0]; + double *dst = d[0]; + + for (n = 0; n < nb_samples; n++) { double current = src[c]; dst[c] = current + (current - prv[c]) * mult; @@ -117,20 +145,30 @@ static void filter_dbl(void **d, void **p, const void **s, if (clip) { dst[c] = av_clipd(dst[c], -1, 1); } - } - dst += c; - src += c; + dst += channels; + src += channels; + } } + + return 0; } -static void filter_fltp(void **d, void **p, const void **s, - int nb_samples, int channels, - float mult, int clip) +static int filter_fltp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) { + ThreadData *td = arg; + void **d = td->d; + void **p = td->p; + const void **s = td->s; + const int nb_samples = td->nb_samples; + const int channels = td->channels; + float mult = td->mult; + const int clip = td->clip; + const int start = (channels * jobnr) / nb_jobs; + const int end = (channels * (jobnr+1)) / nb_jobs; int n, c; - for (c = 0; c < channels; c++) { + for (c = start; c < end; c++) { const float *src = s[c]; float *dst = d[c]; float *prv = p[c]; @@ -145,15 +183,25 @@ static void filter_fltp(void **d, void **p, const void **s, } } } + + return 0; } -static void filter_dblp(void **d, void **p, const void **s, - int nb_samples, int channels, - float mult, int clip) +static int filter_dblp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) { + ThreadData *td = arg; + void **d = td->d; + void **p = td->p; + const void **s = td->s; + const int nb_samples = td->nb_samples; + const int channels = td->channels; + const double mult = td->mult; + const int clip = td->clip; + const int start = (channels * jobnr) / nb_jobs; + const int end = (channels * (jobnr+1)) / nb_jobs; int n, c; - for (c = 0; c < channels; c++) { + for (c = start; c < end; c++) { const double *src = s[c]; double *dst = d[c]; double *prv = p[c]; @@ -168,6 +216,149 @@ static void filter_dblp(void **d, void **p, const void **s, } } } + + return 0; +} + +static int ifilter_flt(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ThreadData *td = arg; + void **d = td->d; + void **p = td->p; + const void **s = td->s; + const int nb_samples = td->nb_samples; + const int channels = td->channels; + const float mult = -td->mult; + const float div = -td->mult + 1.f; + const int clip = td->clip; + const int start = (channels * jobnr) / nb_jobs; + const int end = (channels * (jobnr+1)) / nb_jobs; + float *prv = p[0]; + int n, c; + + for (c = start; c < end; c++) { + const float *src = s[0]; + float *dst = d[0]; + + for (n = 0; n < nb_samples; n++) { + float current = src[c]; + dst[c] = (current + prv[c] * mult) / div; + prv[c] = dst[c]; + if (clip) { + dst[c] = av_clipf(dst[c], -1, 1); + } + + dst += channels; + src += channels; + } + } + + return 0; +} + +static int ifilter_dbl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ThreadData *td = arg; + void **d = td->d; + void **p = td->p; + const void **s = td->s; + const int nb_samples = td->nb_samples; + const int channels = td->channels; + const double mult = -td->mult; + const double div = -td->mult + 1.f; + const int clip = td->clip; + const int start = (channels * jobnr) / nb_jobs; + const int end = (channels * (jobnr+1)) / nb_jobs; + double *prv = p[0]; + int n, c; + + for (c = start; c < end; c++) { + const double *src = s[0]; + double *dst = d[0]; + + for (n = 0; n < nb_samples; n++) { + double current = src[c]; + + dst[c] = (current + prv[c] * mult) / div; + prv[c] = dst[c]; + if (clip) { + dst[c] = av_clipd(dst[c], -1, 1); + } + + dst += channels; + src += channels; + } + } + + return 0; +} + +static int ifilter_fltp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ThreadData *td = arg; + void **d = td->d; + void **p = td->p; + const void **s = td->s; + const int nb_samples = td->nb_samples; + const int channels = td->channels; + const float mult = -td->mult; + const float div = -td->mult + 1.f; + const int clip = td->clip; + const int start = (channels * jobnr) / nb_jobs; + const int end = (channels * (jobnr+1)) / nb_jobs; + int n, c; + + for (c = start; c < end; c++) { + const float *src = s[c]; + float *dst = d[c]; + float *prv = p[c]; + + for (n = 0; n < nb_samples; n++) { + float current = src[n]; + + dst[n] = (current + prv[0] * mult) / div; + prv[0] = dst[n]; + if (clip) { + dst[n] = av_clipf(dst[n], -1, 1); + } + } + } + + return 0; +} + +static int ifilter_dblp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ThreadData *td = arg; + void **d = td->d; + void **p = td->p; + const void **s = td->s; + const int nb_samples = td->nb_samples; + const int channels = td->channels; + const double mult = -td->mult; + const double div = -td->mult + 1.f; + const int clip = td->clip; + const int start = (channels * jobnr) / nb_jobs; + const int end = (channels * (jobnr+1)) / nb_jobs; + int n, c; + + for (c = start; c < end; c++) { + const double *src = s[c]; + double *dst = d[c]; + double *prv = p[c]; + + for (n = 0; n < nb_samples; n++) { + double current = src[n]; + + dst[n] = (current + prv[0] * mult) / div; + prv[0] = dst[n]; + if (clip) { + dst[n] = av_clipd(dst[n], -1, 1); + } + } + } + + return 0; } static int config_input(AVFilterLink *inlink) @@ -176,10 +367,10 @@ static int config_input(AVFilterLink *inlink) CrystalizerContext *s = ctx->priv; switch (inlink->format) { - case AV_SAMPLE_FMT_FLT: s->filter = filter_flt; break; - case AV_SAMPLE_FMT_DBL: s->filter = filter_dbl; break; - case AV_SAMPLE_FMT_FLTP: s->filter = filter_fltp; break; - case AV_SAMPLE_FMT_DBLP: s->filter = filter_dblp; break; + case AV_SAMPLE_FMT_FLT: s->filter = s->mult >= 0.f ? filter_flt : ifilter_flt; break; + case AV_SAMPLE_FMT_DBL: s->filter = s->mult >= 0.f ? filter_dbl : ifilter_dbl; break; + case AV_SAMPLE_FMT_FLTP: s->filter = s->mult >= 0.f ? filter_fltp : ifilter_fltp; break; + case AV_SAMPLE_FMT_DBLP: s->filter = s->mult >= 0.f ? filter_dblp : ifilter_dblp; break; } return 0; @@ -191,6 +382,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) AVFilterLink *outlink = ctx->outputs[0]; CrystalizerContext *s = ctx->priv; AVFrame *out; + ThreadData td; if (!s->prev) { s->prev = ff_get_audio_buffer(inlink, 1); @@ -211,8 +403,15 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) av_frame_copy_props(out, in); } - s->filter((void **)out->extended_data, (void **)s->prev->extended_data, (const void **)in->extended_data, - in->nb_samples, in->channels, s->mult, s->clip); + td.d = (void **)out->extended_data; + td.s = (const void **)in->extended_data; + td.p = (void **)s->prev->extended_data; + td.nb_samples = in->nb_samples; + td.channels = in->channels; + td.mult = ctx->is_disabled ? 0.f : s->mult; + td.clip = s->clip; + ctx->internal->execute(ctx, s->filter, &td, NULL, FFMIN(inlink->channels, + ff_filter_get_nb_threads(ctx))); if (out != in) av_frame_free(&in); @@ -227,6 +426,18 @@ static av_cold void uninit(AVFilterContext *ctx) av_frame_free(&s->prev); } +static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, + char *res, int res_len, int flags) +{ + int ret; + + ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags); + if (ret < 0) + return ret; + + return config_input(ctx->inputs[0]); +} + static const AVFilterPad inputs[] = { { .name = "default", @@ -245,13 +456,16 @@ static const AVFilterPad outputs[] = { { NULL } }; -AVFilter ff_af_crystalizer = { +const AVFilter ff_af_crystalizer = { .name = "crystalizer", - .description = NULL_IF_CONFIG_SMALL("Simple expand audio dynamic range filter."), + .description = NULL_IF_CONFIG_SMALL("Simple audio noise sharpening filter."), .query_formats = query_formats, .priv_size = sizeof(CrystalizerContext), .priv_class = &crystalizer_class, .uninit = uninit, .inputs = inputs, .outputs = outputs, + .process_command = process_command, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | + AVFILTER_FLAG_SLICE_THREADS, };