2 * Copyright (c) 2019 The FFmpeg Project
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/avassert.h"
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/opt.h"
24 #include "libswresample/swresample.h"
42 typedef struct ASoftClipContext {
57 void (*filter)(struct ASoftClipContext *s, void **dst, const void **src,
58 int nb_samples, int channels, int start, int end);
61 #define OFFSET(x) offsetof(ASoftClipContext, x)
62 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
63 #define F AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
65 static const AVOption asoftclip_options[] = {
66 { "type", "set softclip type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, -1, NB_TYPES-1, A, "types" },
67 { "hard", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_HARD}, 0, 0, A, "types" },
68 { "tanh", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_TANH}, 0, 0, A, "types" },
69 { "atan", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ATAN}, 0, 0, A, "types" },
70 { "cubic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_CUBIC}, 0, 0, A, "types" },
71 { "exp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_EXP}, 0, 0, A, "types" },
72 { "alg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ALG}, 0, 0, A, "types" },
73 { "quintic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_QUINTIC},0, 0, A, "types" },
74 { "sin", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_SIN}, 0, 0, A, "types" },
75 { "erf", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ERF}, 0, 0, A, "types" },
76 { "threshold", "set softclip threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.000001, 1, A },
77 { "output", "set softclip output gain", OFFSET(output), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.000001, 16, A },
78 { "param", "set softclip parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 3, A },
79 { "oversample", "set oversample factor", OFFSET(oversample), AV_OPT_TYPE_INT, {.i64=1}, 1, 32, F },
83 AVFILTER_DEFINE_CLASS(asoftclip);
85 static int query_formats(AVFilterContext *ctx)
87 AVFilterFormats *formats = NULL;
88 AVFilterChannelLayouts *layouts = NULL;
89 static const enum AVSampleFormat sample_fmts[] = {
90 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
91 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
96 formats = ff_make_format_list(sample_fmts);
98 return AVERROR(ENOMEM);
99 ret = ff_set_common_formats(ctx, formats);
103 layouts = ff_all_channel_counts();
105 return AVERROR(ENOMEM);
107 ret = ff_set_common_channel_layouts(ctx, layouts);
111 formats = ff_all_samplerates();
112 return ff_set_common_samplerates(ctx, formats);
115 static void filter_flt(ASoftClipContext *s,
116 void **dptr, const void **sptr,
117 int nb_samples, int channels,
120 float threshold = s->threshold;
121 float gain = s->output * threshold;
122 float factor = 1.f / threshold;
123 float param = s->param;
125 for (int c = start; c < end; c++) {
126 const float *src = sptr[c];
127 float *dst = dptr[c];
131 for (int n = 0; n < nb_samples; n++) {
132 dst[n] = av_clipf(src[n] * factor, -1.f, 1.f);
137 for (int n = 0; n < nb_samples; n++) {
138 dst[n] = tanhf(src[n] * factor * param);
143 for (int n = 0; n < nb_samples; n++) {
144 dst[n] = 2.f / M_PI * atanf(src[n] * factor * param);
149 for (int n = 0; n < nb_samples; n++) {
150 float sample = src[n] * factor;
152 if (FFABS(sample) >= 1.5f)
153 dst[n] = FFSIGN(sample);
155 dst[n] = sample - 0.1481f * powf(sample, 3.f);
160 for (int n = 0; n < nb_samples; n++) {
161 dst[n] = 2.f / (1.f + expf(-2.f * src[n] * factor)) - 1.;
166 for (int n = 0; n < nb_samples; n++) {
167 float sample = src[n] * factor;
169 dst[n] = sample / (sqrtf(param + sample * sample));
174 for (int n = 0; n < nb_samples; n++) {
175 float sample = src[n] * factor;
177 if (FFABS(sample) >= 1.25)
178 dst[n] = FFSIGN(sample);
180 dst[n] = sample - 0.08192f * powf(sample, 5.f);
185 for (int n = 0; n < nb_samples; n++) {
186 float sample = src[n] * factor;
188 if (FFABS(sample) >= M_PI_2)
189 dst[n] = FFSIGN(sample);
191 dst[n] = sinf(sample);
196 for (int n = 0; n < nb_samples; n++) {
197 dst[n] = erff(src[n] * factor);
207 static void filter_dbl(ASoftClipContext *s,
208 void **dptr, const void **sptr,
209 int nb_samples, int channels,
212 double threshold = s->threshold;
213 double gain = s->output * threshold;
214 double factor = 1. / threshold;
215 double param = s->param;
217 for (int c = start; c < end; c++) {
218 const double *src = sptr[c];
219 double *dst = dptr[c];
223 for (int n = 0; n < nb_samples; n++) {
224 dst[n] = av_clipd(src[n] * factor, -1., 1.);
229 for (int n = 0; n < nb_samples; n++) {
230 dst[n] = tanh(src[n] * factor * param);
235 for (int n = 0; n < nb_samples; n++) {
236 dst[n] = 2. / M_PI * atan(src[n] * factor * param);
241 for (int n = 0; n < nb_samples; n++) {
242 double sample = src[n] * factor;
244 if (FFABS(sample) >= 1.5)
245 dst[n] = FFSIGN(sample);
247 dst[n] = sample - 0.1481 * pow(sample, 3.);
252 for (int n = 0; n < nb_samples; n++) {
253 dst[n] = 2. / (1. + exp(-2. * src[n] * factor)) - 1.;
258 for (int n = 0; n < nb_samples; n++) {
259 double sample = src[n] * factor;
261 dst[n] = sample / (sqrt(param + sample * sample));
266 for (int n = 0; n < nb_samples; n++) {
267 double sample = src[n] * factor;
269 if (FFABS(sample) >= 1.25)
270 dst[n] = FFSIGN(sample);
272 dst[n] = sample - 0.08192 * pow(sample, 5.);
277 for (int n = 0; n < nb_samples; n++) {
278 double sample = src[n] * factor;
280 if (FFABS(sample) >= M_PI_2)
281 dst[n] = FFSIGN(sample);
283 dst[n] = sin(sample);
288 for (int n = 0; n < nb_samples; n++) {
289 dst[n] = erf(src[n] * factor);
299 static int config_input(AVFilterLink *inlink)
301 AVFilterContext *ctx = inlink->dst;
302 ASoftClipContext *s = ctx->priv;
305 switch (inlink->format) {
306 case AV_SAMPLE_FMT_FLT:
307 case AV_SAMPLE_FMT_FLTP: s->filter = filter_flt; break;
308 case AV_SAMPLE_FMT_DBL:
309 case AV_SAMPLE_FMT_DBLP: s->filter = filter_dbl; break;
310 default: av_assert0(0);
313 if (s->oversample <= 1)
316 s->up_ctx = swr_alloc();
317 s->down_ctx = swr_alloc();
318 if (!s->up_ctx || !s->down_ctx)
319 return AVERROR(ENOMEM);
321 av_opt_set_int(s->up_ctx, "in_channel_layout", inlink->channel_layout, 0);
322 av_opt_set_int(s->up_ctx, "in_sample_rate", inlink->sample_rate, 0);
323 av_opt_set_sample_fmt(s->up_ctx, "in_sample_fmt", inlink->format, 0);
325 av_opt_set_int(s->up_ctx, "out_channel_layout", inlink->channel_layout, 0);
326 av_opt_set_int(s->up_ctx, "out_sample_rate", inlink->sample_rate * s->oversample, 0);
327 av_opt_set_sample_fmt(s->up_ctx, "out_sample_fmt", inlink->format, 0);
329 av_opt_set_int(s->down_ctx, "in_channel_layout", inlink->channel_layout, 0);
330 av_opt_set_int(s->down_ctx, "in_sample_rate", inlink->sample_rate * s->oversample, 0);
331 av_opt_set_sample_fmt(s->down_ctx, "in_sample_fmt", inlink->format, 0);
333 av_opt_set_int(s->down_ctx, "out_channel_layout", inlink->channel_layout, 0);
334 av_opt_set_int(s->down_ctx, "out_sample_rate", inlink->sample_rate, 0);
335 av_opt_set_sample_fmt(s->down_ctx, "out_sample_fmt", inlink->format, 0);
337 ret = swr_init(s->up_ctx);
341 ret = swr_init(s->down_ctx);
348 typedef struct ThreadData {
354 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
356 ASoftClipContext *s = ctx->priv;
357 ThreadData *td = arg;
358 AVFrame *out = td->out;
359 AVFrame *in = td->in;
360 const int channels = td->channels;
361 const int nb_samples = td->nb_samples;
362 const int start = (channels * jobnr) / nb_jobs;
363 const int end = (channels * (jobnr+1)) / nb_jobs;
365 s->filter(s, (void **)out->extended_data, (const void **)in->extended_data,
366 nb_samples, channels, start, end);
371 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
373 AVFilterContext *ctx = inlink->dst;
374 ASoftClipContext *s = ctx->priv;
375 AVFilterLink *outlink = ctx->outputs[0];
376 int ret, nb_samples, channels;
380 if (av_frame_is_writable(in)) {
383 out = ff_get_audio_buffer(outlink, in->nb_samples);
386 return AVERROR(ENOMEM);
388 av_frame_copy_props(out, in);
391 if (av_sample_fmt_is_planar(in->format)) {
392 nb_samples = in->nb_samples;
393 channels = in->channels;
395 nb_samples = in->channels * in->nb_samples;
399 if (s->oversample > 1) {
400 s->frame = ff_get_audio_buffer(outlink, in->nb_samples * s->oversample);
402 ret = AVERROR(ENOMEM);
406 ret = swr_convert(s->up_ctx, (uint8_t**)s->frame->extended_data, in->nb_samples * s->oversample,
407 (const uint8_t **)in->extended_data, in->nb_samples);
413 td.nb_samples = av_sample_fmt_is_planar(in->format) ? ret : ret * in->channels;
414 td.channels = channels;
415 ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
416 ff_filter_get_nb_threads(ctx)));
418 ret = swr_convert(s->down_ctx, (uint8_t**)out->extended_data, out->nb_samples,
419 (const uint8_t **)s->frame->extended_data, ret);
424 out->pts -= s->delay;
425 s->delay += in->nb_samples - ret;
426 out->nb_samples = ret;
428 av_frame_free(&s->frame);
432 td.nb_samples = nb_samples;
433 td.channels = channels;
434 ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
435 ff_filter_get_nb_threads(ctx)));
441 return ff_filter_frame(outlink, out);
446 av_frame_free(&s->frame);
451 static av_cold void uninit(AVFilterContext *ctx)
453 ASoftClipContext *s = ctx->priv;
455 swr_free(&s->up_ctx);
456 swr_free(&s->down_ctx);
459 static const AVFilterPad inputs[] = {
462 .type = AVMEDIA_TYPE_AUDIO,
463 .filter_frame = filter_frame,
464 .config_props = config_input,
469 static const AVFilterPad outputs[] = {
472 .type = AVMEDIA_TYPE_AUDIO,
477 AVFilter ff_af_asoftclip = {
479 .description = NULL_IF_CONFIG_SMALL("Audio Soft Clipper."),
480 .query_formats = query_formats,
481 .priv_size = sizeof(ASoftClipContext),
482 .priv_class = &asoftclip_class,
486 .process_command = ff_filter_process_command,
487 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
488 AVFILTER_FLAG_SLICE_THREADS,