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 {
55 void (*filter)(struct ASoftClipContext *s, void **dst, const void **src,
56 int nb_samples, int channels, int start, int end);
59 #define OFFSET(x) offsetof(ASoftClipContext, x)
60 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
61 #define F AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
63 static const AVOption asoftclip_options[] = {
64 { "type", "set softclip type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, -1, NB_TYPES-1, A, "types" },
65 { "hard", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_HARD}, 0, 0, A, "types" },
66 { "tanh", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_TANH}, 0, 0, A, "types" },
67 { "atan", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ATAN}, 0, 0, A, "types" },
68 { "cubic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_CUBIC}, 0, 0, A, "types" },
69 { "exp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_EXP}, 0, 0, A, "types" },
70 { "alg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ALG}, 0, 0, A, "types" },
71 { "quintic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_QUINTIC},0, 0, A, "types" },
72 { "sin", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_SIN}, 0, 0, A, "types" },
73 { "erf", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ERF}, 0, 0, A, "types" },
74 { "param", "set softclip parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 3, A },
75 { "oversample", "set oversample factor", OFFSET(oversample), AV_OPT_TYPE_INT, {.i64=1}, 1, 32, F },
79 AVFILTER_DEFINE_CLASS(asoftclip);
81 static int query_formats(AVFilterContext *ctx)
83 AVFilterFormats *formats = NULL;
84 AVFilterChannelLayouts *layouts = NULL;
85 static const enum AVSampleFormat sample_fmts[] = {
86 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
87 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
92 formats = ff_make_format_list(sample_fmts);
94 return AVERROR(ENOMEM);
95 ret = ff_set_common_formats(ctx, formats);
99 layouts = ff_all_channel_counts();
101 return AVERROR(ENOMEM);
103 ret = ff_set_common_channel_layouts(ctx, layouts);
107 formats = ff_all_samplerates();
108 return ff_set_common_samplerates(ctx, formats);
111 #define SQR(x) ((x) * (x))
113 static void filter_flt(ASoftClipContext *s,
114 void **dptr, const void **sptr,
115 int nb_samples, int channels,
118 float param = s->param;
120 for (int c = start; c < end; c++) {
121 const float *src = sptr[c];
122 float *dst = dptr[c];
126 for (int n = 0; n < nb_samples; n++) {
127 dst[n] = av_clipf(src[n], -1.f, 1.f);
131 for (int n = 0; n < nb_samples; n++) {
132 dst[n] = tanhf(src[n] * param);
136 for (int n = 0; n < nb_samples; n++)
137 dst[n] = 2.f / M_PI * atanf(src[n] * param);
140 for (int n = 0; n < nb_samples; n++) {
141 if (FFABS(src[n]) >= 1.5f)
142 dst[n] = FFSIGN(src[n]);
144 dst[n] = src[n] - 0.1481f * powf(src[n], 3.f);
148 for (int n = 0; n < nb_samples; n++)
149 dst[n] = 2.f / (1.f + expf(-2.f * src[n])) - 1.;
152 for (int n = 0; n < nb_samples; n++)
153 dst[n] = src[n] / (sqrtf(param + src[n] * src[n]));
156 for (int n = 0; n < nb_samples; n++) {
157 if (FFABS(src[n]) >= 1.25)
158 dst[n] = FFSIGN(src[n]);
160 dst[n] = src[n] - 0.08192f * powf(src[n], 5.f);
164 for (int n = 0; n < nb_samples; n++) {
165 if (FFABS(src[n]) >= M_PI_2)
166 dst[n] = FFSIGN(src[n]);
168 dst[n] = sinf(src[n]);
172 for (int n = 0; n < nb_samples; n++) {
173 dst[n] = erff(src[n]);
182 static void filter_dbl(ASoftClipContext *s,
183 void **dptr, const void **sptr,
184 int nb_samples, int channels,
187 double param = s->param;
189 for (int c = start; c < end; c++) {
190 const double *src = sptr[c];
191 double *dst = dptr[c];
195 for (int n = 0; n < nb_samples; n++) {
196 dst[n] = av_clipd(src[n], -1., 1.);
200 for (int n = 0; n < nb_samples; n++) {
201 dst[n] = tanh(src[n] * param);
205 for (int n = 0; n < nb_samples; n++)
206 dst[n] = 2. / M_PI * atan(src[n] * param);
209 for (int n = 0; n < nb_samples; n++) {
210 if (FFABS(src[n]) >= 1.5)
211 dst[n] = FFSIGN(src[n]);
213 dst[n] = src[n] - 0.1481 * pow(src[n], 3.);
217 for (int n = 0; n < nb_samples; n++)
218 dst[n] = 2. / (1. + exp(-2. * src[n])) - 1.;
221 for (int n = 0; n < nb_samples; n++)
222 dst[n] = src[n] / (sqrt(param + src[n] * src[n]));
225 for (int n = 0; n < nb_samples; n++) {
226 if (FFABS(src[n]) >= 1.25)
227 dst[n] = FFSIGN(src[n]);
229 dst[n] = src[n] - 0.08192 * pow(src[n], 5.);
233 for (int n = 0; n < nb_samples; n++) {
234 if (FFABS(src[n]) >= M_PI_2)
235 dst[n] = FFSIGN(src[n]);
237 dst[n] = sin(src[n]);
241 for (int n = 0; n < nb_samples; n++) {
242 dst[n] = erf(src[n]);
251 static int config_input(AVFilterLink *inlink)
253 AVFilterContext *ctx = inlink->dst;
254 ASoftClipContext *s = ctx->priv;
257 switch (inlink->format) {
258 case AV_SAMPLE_FMT_FLT:
259 case AV_SAMPLE_FMT_FLTP: s->filter = filter_flt; break;
260 case AV_SAMPLE_FMT_DBL:
261 case AV_SAMPLE_FMT_DBLP: s->filter = filter_dbl; break;
262 default: av_assert0(0);
265 if (s->oversample <= 1)
268 s->up_ctx = swr_alloc();
269 s->down_ctx = swr_alloc();
270 if (!s->up_ctx || !s->down_ctx)
271 return AVERROR(ENOMEM);
273 av_opt_set_int(s->up_ctx, "in_channel_layout", inlink->channel_layout, 0);
274 av_opt_set_int(s->up_ctx, "in_sample_rate", inlink->sample_rate, 0);
275 av_opt_set_sample_fmt(s->up_ctx, "in_sample_fmt", inlink->format, 0);
277 av_opt_set_int(s->up_ctx, "out_channel_layout", inlink->channel_layout, 0);
278 av_opt_set_int(s->up_ctx, "out_sample_rate", inlink->sample_rate * s->oversample, 0);
279 av_opt_set_sample_fmt(s->up_ctx, "out_sample_fmt", inlink->format, 0);
281 av_opt_set_int(s->down_ctx, "in_channel_layout", inlink->channel_layout, 0);
282 av_opt_set_int(s->down_ctx, "in_sample_rate", inlink->sample_rate * s->oversample, 0);
283 av_opt_set_sample_fmt(s->down_ctx, "in_sample_fmt", inlink->format, 0);
285 av_opt_set_int(s->down_ctx, "out_channel_layout", inlink->channel_layout, 0);
286 av_opt_set_int(s->down_ctx, "out_sample_rate", inlink->sample_rate, 0);
287 av_opt_set_sample_fmt(s->down_ctx, "out_sample_fmt", inlink->format, 0);
289 ret = swr_init(s->up_ctx);
293 ret = swr_init(s->down_ctx);
300 typedef struct ThreadData {
306 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
308 ASoftClipContext *s = ctx->priv;
309 ThreadData *td = arg;
310 AVFrame *out = td->out;
311 AVFrame *in = td->in;
312 const int channels = td->channels;
313 const int nb_samples = td->nb_samples;
314 const int start = (channels * jobnr) / nb_jobs;
315 const int end = (channels * (jobnr+1)) / nb_jobs;
317 s->filter(s, (void **)out->extended_data, (const void **)in->extended_data,
318 nb_samples, channels, start, end);
323 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
325 AVFilterContext *ctx = inlink->dst;
326 ASoftClipContext *s = ctx->priv;
327 AVFilterLink *outlink = ctx->outputs[0];
328 int ret, nb_samples, channels;
332 if (av_frame_is_writable(in)) {
335 out = ff_get_audio_buffer(outlink, in->nb_samples);
338 return AVERROR(ENOMEM);
340 av_frame_copy_props(out, in);
343 if (av_sample_fmt_is_planar(in->format)) {
344 nb_samples = in->nb_samples;
345 channels = in->channels;
347 nb_samples = in->channels * in->nb_samples;
351 if (s->oversample > 1) {
352 s->frame = ff_get_audio_buffer(outlink, in->nb_samples * s->oversample);
354 ret = AVERROR(ENOMEM);
358 ret = swr_convert(s->up_ctx, (uint8_t**)s->frame->extended_data, in->nb_samples * s->oversample,
359 (const uint8_t **)in->extended_data, in->nb_samples);
365 td.nb_samples = av_sample_fmt_is_planar(in->format) ? ret : ret * in->channels;
366 td.channels = channels;
367 ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
368 ff_filter_get_nb_threads(ctx)));
370 ret = swr_convert(s->down_ctx, (uint8_t**)out->extended_data, out->nb_samples,
371 (const uint8_t **)s->frame->extended_data, ret);
376 out->pts -= s->delay;
377 s->delay += in->nb_samples - ret;
378 out->nb_samples = ret;
380 av_frame_free(&s->frame);
384 td.nb_samples = nb_samples;
385 td.channels = channels;
386 ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
387 ff_filter_get_nb_threads(ctx)));
393 return ff_filter_frame(outlink, out);
398 av_frame_free(&s->frame);
403 static av_cold void uninit(AVFilterContext *ctx)
405 ASoftClipContext *s = ctx->priv;
407 swr_free(&s->up_ctx);
408 swr_free(&s->down_ctx);
411 static const AVFilterPad inputs[] = {
414 .type = AVMEDIA_TYPE_AUDIO,
415 .filter_frame = filter_frame,
416 .config_props = config_input,
421 static const AVFilterPad outputs[] = {
424 .type = AVMEDIA_TYPE_AUDIO,
429 AVFilter ff_af_asoftclip = {
431 .description = NULL_IF_CONFIG_SMALL("Audio Soft Clipper."),
432 .query_formats = query_formats,
433 .priv_size = sizeof(ASoftClipContext),
434 .priv_class = &asoftclip_class,
438 .process_command = ff_filter_process_command,
439 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
440 AVFILTER_FLAG_SLICE_THREADS,