1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-01-24 13:56:33 +02:00

avfilter/af_asoftclip: add oversampling support

This commit is contained in:
Paul B Mahol 2020-11-05 13:33:32 +01:00
parent 7e8306dd2d
commit ee4a046540
3 changed files with 103 additions and 7 deletions

1
configure vendored
View File

@ -3501,6 +3501,7 @@ afir_filter_deps="avcodec"
afir_filter_select="rdft" afir_filter_select="rdft"
amovie_filter_deps="avcodec avformat" amovie_filter_deps="avcodec avformat"
aresample_filter_deps="swresample" aresample_filter_deps="swresample"
asoftclip_filter_deps="swresample"
asr_filter_deps="pocketsphinx" asr_filter_deps="pocketsphinx"
ass_filter_deps="libass" ass_filter_deps="libass"
atempo_filter_deps="avcodec" atempo_filter_deps="avcodec"

View File

@ -2356,6 +2356,9 @@ It accepts the following values:
@item param @item param
Set additional parameter which controls sigmoid function. Set additional parameter which controls sigmoid function.
@item oversample
Set oversampling factor.
@end table @end table
@subsection Commands @subsection Commands

View File

@ -21,6 +21,7 @@
#include "libavutil/avassert.h" #include "libavutil/avassert.h"
#include "libavutil/channel_layout.h" #include "libavutil/channel_layout.h"
#include "libavutil/opt.h" #include "libavutil/opt.h"
#include "libswresample/swresample.h"
#include "avfilter.h" #include "avfilter.h"
#include "audio.h" #include "audio.h"
#include "formats.h" #include "formats.h"
@ -42,14 +43,22 @@ typedef struct ASoftClipContext {
const AVClass *class; const AVClass *class;
int type; int type;
int oversample;
int64_t delay;
double param; double param;
SwrContext *up_ctx;
SwrContext *down_ctx;
AVFrame *frame;
void (*filter)(struct ASoftClipContext *s, void **dst, const void **src, void (*filter)(struct ASoftClipContext *s, void **dst, const void **src,
int nb_samples, int channels, int start, int end); int nb_samples, int channels, int start, int end);
} ASoftClipContext; } ASoftClipContext;
#define OFFSET(x) offsetof(ASoftClipContext, x) #define OFFSET(x) offsetof(ASoftClipContext, x)
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
#define F AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
static const AVOption asoftclip_options[] = { static const AVOption asoftclip_options[] = {
{ "type", "set softclip type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, -1, NB_TYPES-1, A, "types" }, { "type", "set softclip type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, -1, NB_TYPES-1, A, "types" },
@ -63,6 +72,7 @@ static const AVOption asoftclip_options[] = {
{ "sin", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_SIN}, 0, 0, A, "types" }, { "sin", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_SIN}, 0, 0, A, "types" },
{ "erf", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ERF}, 0, 0, A, "types" }, { "erf", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ERF}, 0, 0, A, "types" },
{ "param", "set softclip parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 3, A }, { "param", "set softclip parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 3, A },
{ "oversample", "set oversample factor", OFFSET(oversample), AV_OPT_TYPE_INT, {.i64=1}, 1, 32, F },
{ NULL } { NULL }
}; };
@ -242,6 +252,7 @@ static int config_input(AVFilterLink *inlink)
{ {
AVFilterContext *ctx = inlink->dst; AVFilterContext *ctx = inlink->dst;
ASoftClipContext *s = ctx->priv; ASoftClipContext *s = ctx->priv;
int ret;
switch (inlink->format) { switch (inlink->format) {
case AV_SAMPLE_FMT_FLT: case AV_SAMPLE_FMT_FLT:
@ -251,6 +262,38 @@ static int config_input(AVFilterLink *inlink)
default: av_assert0(0); default: av_assert0(0);
} }
if (s->oversample <= 1)
return 0;
s->up_ctx = swr_alloc();
s->down_ctx = swr_alloc();
if (!s->up_ctx || !s->down_ctx)
return AVERROR(ENOMEM);
av_opt_set_int(s->up_ctx, "in_channel_layout", inlink->channel_layout, 0);
av_opt_set_int(s->up_ctx, "in_sample_rate", inlink->sample_rate, 0);
av_opt_set_sample_fmt(s->up_ctx, "in_sample_fmt", inlink->format, 0);
av_opt_set_int(s->up_ctx, "out_channel_layout", inlink->channel_layout, 0);
av_opt_set_int(s->up_ctx, "out_sample_rate", inlink->sample_rate * s->oversample, 0);
av_opt_set_sample_fmt(s->up_ctx, "out_sample_fmt", inlink->format, 0);
av_opt_set_int(s->down_ctx, "in_channel_layout", inlink->channel_layout, 0);
av_opt_set_int(s->down_ctx, "in_sample_rate", inlink->sample_rate * s->oversample, 0);
av_opt_set_sample_fmt(s->down_ctx, "in_sample_fmt", inlink->format, 0);
av_opt_set_int(s->down_ctx, "out_channel_layout", inlink->channel_layout, 0);
av_opt_set_int(s->down_ctx, "out_sample_rate", inlink->sample_rate, 0);
av_opt_set_sample_fmt(s->down_ctx, "out_sample_fmt", inlink->format, 0);
ret = swr_init(s->up_ctx);
if (ret < 0)
return ret;
ret = swr_init(s->down_ctx);
if (ret < 0)
return ret;
return 0; return 0;
} }
@ -280,8 +323,9 @@ static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jo
static int filter_frame(AVFilterLink *inlink, AVFrame *in) static int filter_frame(AVFilterLink *inlink, AVFrame *in)
{ {
AVFilterContext *ctx = inlink->dst; AVFilterContext *ctx = inlink->dst;
ASoftClipContext *s = ctx->priv;
AVFilterLink *outlink = ctx->outputs[0]; AVFilterLink *outlink = ctx->outputs[0];
int nb_samples, channels; int ret, nb_samples, channels;
ThreadData td; ThreadData td;
AVFrame *out; AVFrame *out;
@ -304,17 +348,64 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
channels = 1; channels = 1;
} }
td.in = in; if (s->oversample > 1) {
td.out = out; s->frame = ff_get_audio_buffer(outlink, in->nb_samples * s->oversample);
td.nb_samples = nb_samples; if (!s->frame) {
td.channels = channels; ret = AVERROR(ENOMEM);
ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels, goto fail;
ff_filter_get_nb_threads(ctx))); }
ret = swr_convert(s->up_ctx, (uint8_t**)s->frame->extended_data, in->nb_samples * s->oversample,
(const uint8_t **)in->extended_data, in->nb_samples);
if (ret < 0)
goto fail;
td.in = s->frame;
td.out = s->frame;
td.nb_samples = av_sample_fmt_is_planar(in->format) ? ret : ret * in->channels;
td.channels = channels;
ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
ff_filter_get_nb_threads(ctx)));
ret = swr_convert(s->down_ctx, (uint8_t**)out->extended_data, out->nb_samples,
(const uint8_t **)s->frame->extended_data, ret);
if (ret < 0)
goto fail;
if (out->pts)
out->pts -= s->delay;
s->delay += in->nb_samples - ret;
out->nb_samples = ret;
av_frame_free(&s->frame);
} else {
td.in = in;
td.out = out;
td.nb_samples = nb_samples;
td.channels = channels;
ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
ff_filter_get_nb_threads(ctx)));
}
if (out != in) if (out != in)
av_frame_free(&in); av_frame_free(&in);
return ff_filter_frame(outlink, out); return ff_filter_frame(outlink, out);
fail:
if (out != in)
av_frame_free(&out);
av_frame_free(&in);
av_frame_free(&s->frame);
return ret;
}
static av_cold void uninit(AVFilterContext *ctx)
{
ASoftClipContext *s = ctx->priv;
swr_free(&s->up_ctx);
swr_free(&s->down_ctx);
} }
static const AVFilterPad inputs[] = { static const AVFilterPad inputs[] = {
@ -343,6 +434,7 @@ AVFilter ff_af_asoftclip = {
.priv_class = &asoftclip_class, .priv_class = &asoftclip_class,
.inputs = inputs, .inputs = inputs,
.outputs = outputs, .outputs = outputs,
.uninit = uninit,
.process_command = ff_filter_process_command, .process_command = ff_filter_process_command,
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
AVFILTER_FLAG_SLICE_THREADS, AVFILTER_FLAG_SLICE_THREADS,