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"
41 typedef struct ASoftClipContext {
47 void (*filter)(struct ASoftClipContext *s, void **dst, const void **src,
48 int nb_samples, int channels, int start, int end);
51 #define OFFSET(x) offsetof(ASoftClipContext, x)
52 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
54 static const AVOption asoftclip_options[] = {
55 { "type", "set softclip type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, -1, NB_TYPES-1, A, "types" },
56 { "hard", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_HARD}, 0, 0, A, "types" },
57 { "tanh", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_TANH}, 0, 0, A, "types" },
58 { "atan", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ATAN}, 0, 0, A, "types" },
59 { "cubic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_CUBIC}, 0, 0, A, "types" },
60 { "exp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_EXP}, 0, 0, A, "types" },
61 { "alg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ALG}, 0, 0, A, "types" },
62 { "quintic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_QUINTIC},0, 0, A, "types" },
63 { "sin", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_SIN}, 0, 0, A, "types" },
64 { "erf", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ERF}, 0, 0, A, "types" },
65 { "param", "set softclip parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 3, A },
69 AVFILTER_DEFINE_CLASS(asoftclip);
71 static int query_formats(AVFilterContext *ctx)
73 AVFilterFormats *formats = NULL;
74 AVFilterChannelLayouts *layouts = NULL;
75 static const enum AVSampleFormat sample_fmts[] = {
76 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
77 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
82 formats = ff_make_format_list(sample_fmts);
84 return AVERROR(ENOMEM);
85 ret = ff_set_common_formats(ctx, formats);
89 layouts = ff_all_channel_counts();
91 return AVERROR(ENOMEM);
93 ret = ff_set_common_channel_layouts(ctx, layouts);
97 formats = ff_all_samplerates();
98 return ff_set_common_samplerates(ctx, formats);
101 #define SQR(x) ((x) * (x))
103 static void filter_flt(ASoftClipContext *s,
104 void **dptr, const void **sptr,
105 int nb_samples, int channels,
108 float param = s->param;
110 for (int c = start; c < end; c++) {
111 const float *src = sptr[c];
112 float *dst = dptr[c];
116 for (int n = 0; n < nb_samples; n++) {
117 dst[n] = av_clipf(src[n], -1.f, 1.f);
121 for (int n = 0; n < nb_samples; n++) {
122 dst[n] = tanhf(src[n] * param);
126 for (int n = 0; n < nb_samples; n++)
127 dst[n] = 2.f / M_PI * atanf(src[n] * param);
130 for (int n = 0; n < nb_samples; n++) {
131 if (FFABS(src[n]) >= 1.5f)
132 dst[n] = FFSIGN(src[n]);
134 dst[n] = src[n] - 0.1481f * powf(src[n], 3.f);
138 for (int n = 0; n < nb_samples; n++)
139 dst[n] = 2.f / (1.f + expf(-2.f * src[n])) - 1.;
142 for (int n = 0; n < nb_samples; n++)
143 dst[n] = src[n] / (sqrtf(param + src[n] * src[n]));
146 for (int n = 0; n < nb_samples; n++) {
147 if (FFABS(src[n]) >= 1.25)
148 dst[n] = FFSIGN(src[n]);
150 dst[n] = src[n] - 0.08192f * powf(src[n], 5.f);
154 for (int n = 0; n < nb_samples; n++) {
155 if (FFABS(src[n]) >= M_PI_2)
156 dst[n] = FFSIGN(src[n]);
158 dst[n] = sinf(src[n]);
162 for (int n = 0; n < nb_samples; n++) {
163 dst[n] = erff(src[n]);
172 static void filter_dbl(ASoftClipContext *s,
173 void **dptr, const void **sptr,
174 int nb_samples, int channels,
177 double param = s->param;
179 for (int c = start; c < end; c++) {
180 const double *src = sptr[c];
181 double *dst = dptr[c];
185 for (int n = 0; n < nb_samples; n++) {
186 dst[n] = av_clipd(src[n], -1., 1.);
190 for (int n = 0; n < nb_samples; n++) {
191 dst[n] = tanh(src[n] * param);
195 for (int n = 0; n < nb_samples; n++)
196 dst[n] = 2. / M_PI * atan(src[n] * param);
199 for (int n = 0; n < nb_samples; n++) {
200 if (FFABS(src[n]) >= 1.5)
201 dst[n] = FFSIGN(src[n]);
203 dst[n] = src[n] - 0.1481 * pow(src[n], 3.);
207 for (int n = 0; n < nb_samples; n++)
208 dst[n] = 2. / (1. + exp(-2. * src[n])) - 1.;
211 for (int n = 0; n < nb_samples; n++)
212 dst[n] = src[n] / (sqrt(param + src[n] * src[n]));
215 for (int n = 0; n < nb_samples; n++) {
216 if (FFABS(src[n]) >= 1.25)
217 dst[n] = FFSIGN(src[n]);
219 dst[n] = src[n] - 0.08192 * pow(src[n], 5.);
223 for (int n = 0; n < nb_samples; n++) {
224 if (FFABS(src[n]) >= M_PI_2)
225 dst[n] = FFSIGN(src[n]);
227 dst[n] = sin(src[n]);
231 for (int n = 0; n < nb_samples; n++) {
232 dst[n] = erf(src[n]);
241 static int config_input(AVFilterLink *inlink)
243 AVFilterContext *ctx = inlink->dst;
244 ASoftClipContext *s = ctx->priv;
246 switch (inlink->format) {
247 case AV_SAMPLE_FMT_FLT:
248 case AV_SAMPLE_FMT_FLTP: s->filter = filter_flt; break;
249 case AV_SAMPLE_FMT_DBL:
250 case AV_SAMPLE_FMT_DBLP: s->filter = filter_dbl; break;
251 default: av_assert0(0);
257 typedef struct ThreadData {
263 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
265 ASoftClipContext *s = ctx->priv;
266 ThreadData *td = arg;
267 AVFrame *out = td->out;
268 AVFrame *in = td->in;
269 const int channels = td->channels;
270 const int nb_samples = td->nb_samples;
271 const int start = (channels * jobnr) / nb_jobs;
272 const int end = (channels * (jobnr+1)) / nb_jobs;
274 s->filter(s, (void **)out->extended_data, (const void **)in->extended_data,
275 nb_samples, channels, start, end);
280 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
282 AVFilterContext *ctx = inlink->dst;
283 AVFilterLink *outlink = ctx->outputs[0];
284 int nb_samples, channels;
288 if (av_frame_is_writable(in)) {
291 out = ff_get_audio_buffer(outlink, in->nb_samples);
294 return AVERROR(ENOMEM);
296 av_frame_copy_props(out, in);
299 if (av_sample_fmt_is_planar(in->format)) {
300 nb_samples = in->nb_samples;
301 channels = in->channels;
303 nb_samples = in->channels * in->nb_samples;
309 td.nb_samples = nb_samples;
310 td.channels = channels;
311 ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
312 ff_filter_get_nb_threads(ctx)));
317 return ff_filter_frame(outlink, out);
320 static const AVFilterPad inputs[] = {
323 .type = AVMEDIA_TYPE_AUDIO,
324 .filter_frame = filter_frame,
325 .config_props = config_input,
330 static const AVFilterPad outputs[] = {
333 .type = AVMEDIA_TYPE_AUDIO,
338 AVFilter ff_af_asoftclip = {
340 .description = NULL_IF_CONFIG_SMALL("Audio Soft Clipper."),
341 .query_formats = query_formats,
342 .priv_size = sizeof(ASoftClipContext),
343 .priv_class = &asoftclip_class,
346 .process_command = ff_filter_process_command,
347 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
348 AVFILTER_FLAG_SLICE_THREADS,