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/channel_layout.h"
22 #include "libavutil/opt.h"
40 typedef struct ASoftClipContext {
46 void (*filter)(struct ASoftClipContext *s, void **dst, const void **src,
47 int nb_samples, int channels, int start, int end);
50 #define OFFSET(x) offsetof(ASoftClipContext, x)
51 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
53 static const AVOption asoftclip_options[] = {
54 { "type", "set softclip type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, -1, NB_TYPES-1, A, "types" },
55 { "hard", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_HARD}, 0, 0, A, "types" },
56 { "tanh", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_TANH}, 0, 0, A, "types" },
57 { "atan", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ATAN}, 0, 0, A, "types" },
58 { "cubic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_CUBIC}, 0, 0, A, "types" },
59 { "exp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_EXP}, 0, 0, A, "types" },
60 { "alg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ALG}, 0, 0, A, "types" },
61 { "quintic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_QUINTIC},0, 0, A, "types" },
62 { "sin", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_SIN}, 0, 0, A, "types" },
63 { "erf", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ERF}, 0, 0, A, "types" },
64 { "param", "set softclip parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 3, A },
68 AVFILTER_DEFINE_CLASS(asoftclip);
70 static int query_formats(AVFilterContext *ctx)
72 AVFilterFormats *formats = NULL;
73 AVFilterChannelLayouts *layouts = NULL;
74 static const enum AVSampleFormat sample_fmts[] = {
75 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
76 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
81 formats = ff_make_format_list(sample_fmts);
83 return AVERROR(ENOMEM);
84 ret = ff_set_common_formats(ctx, formats);
88 layouts = ff_all_channel_counts();
90 return AVERROR(ENOMEM);
92 ret = ff_set_common_channel_layouts(ctx, layouts);
96 formats = ff_all_samplerates();
97 return ff_set_common_samplerates(ctx, formats);
100 #define SQR(x) ((x) * (x))
102 static void filter_flt(ASoftClipContext *s,
103 void **dptr, const void **sptr,
104 int nb_samples, int channels,
107 float param = s->param;
109 for (int c = start; c < end; c++) {
110 const float *src = sptr[c];
111 float *dst = dptr[c];
115 for (int n = 0; n < nb_samples; n++) {
116 dst[n] = av_clipf(src[n], -1.f, 1.f);
120 for (int n = 0; n < nb_samples; n++) {
121 dst[n] = tanhf(src[n] * param);
125 for (int n = 0; n < nb_samples; n++)
126 dst[n] = 2.f / M_PI * atanf(src[n] * param);
129 for (int n = 0; n < nb_samples; n++) {
130 if (FFABS(src[n]) >= 1.5f)
131 dst[n] = FFSIGN(src[n]);
133 dst[n] = src[n] - 0.1481f * powf(src[n], 3.f);
137 for (int n = 0; n < nb_samples; n++)
138 dst[n] = 2.f / (1.f + expf(-2.f * src[n])) - 1.;
141 for (int n = 0; n < nb_samples; n++)
142 dst[n] = src[n] / (sqrtf(param + src[n] * src[n]));
145 for (int n = 0; n < nb_samples; n++) {
146 if (FFABS(src[n]) >= 1.25)
147 dst[n] = FFSIGN(src[n]);
149 dst[n] = src[n] - 0.08192f * powf(src[n], 5.f);
153 for (int n = 0; n < nb_samples; n++) {
154 if (FFABS(src[n]) >= M_PI_2)
155 dst[n] = FFSIGN(src[n]);
157 dst[n] = sinf(src[n]);
161 for (int n = 0; n < nb_samples; n++) {
162 dst[n] = erff(src[n]);
169 static void filter_dbl(ASoftClipContext *s,
170 void **dptr, const void **sptr,
171 int nb_samples, int channels,
174 double param = s->param;
176 for (int c = start; c < end; c++) {
177 const double *src = sptr[c];
178 double *dst = dptr[c];
182 for (int n = 0; n < nb_samples; n++) {
183 dst[n] = av_clipd(src[n], -1., 1.);
187 for (int n = 0; n < nb_samples; n++) {
188 dst[n] = tanh(src[n] * param);
192 for (int n = 0; n < nb_samples; n++)
193 dst[n] = 2. / M_PI * atan(src[n] * param);
196 for (int n = 0; n < nb_samples; n++) {
197 if (FFABS(src[n]) >= 1.5)
198 dst[n] = FFSIGN(src[n]);
200 dst[n] = src[n] - 0.1481 * pow(src[n], 3.);
204 for (int n = 0; n < nb_samples; n++)
205 dst[n] = 2. / (1. + exp(-2. * src[n])) - 1.;
208 for (int n = 0; n < nb_samples; n++)
209 dst[n] = src[n] / (sqrt(param + src[n] * src[n]));
212 for (int n = 0; n < nb_samples; n++) {
213 if (FFABS(src[n]) >= 1.25)
214 dst[n] = FFSIGN(src[n]);
216 dst[n] = src[n] - 0.08192 * pow(src[n], 5.);
220 for (int n = 0; n < nb_samples; n++) {
221 if (FFABS(src[n]) >= M_PI_2)
222 dst[n] = FFSIGN(src[n]);
224 dst[n] = sin(src[n]);
228 for (int n = 0; n < nb_samples; n++) {
229 dst[n] = erf(src[n]);
236 static int config_input(AVFilterLink *inlink)
238 AVFilterContext *ctx = inlink->dst;
239 ASoftClipContext *s = ctx->priv;
241 switch (inlink->format) {
242 case AV_SAMPLE_FMT_FLT:
243 case AV_SAMPLE_FMT_FLTP: s->filter = filter_flt; break;
244 case AV_SAMPLE_FMT_DBL:
245 case AV_SAMPLE_FMT_DBLP: s->filter = filter_dbl; break;
251 typedef struct ThreadData {
257 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
259 ASoftClipContext *s = ctx->priv;
260 ThreadData *td = arg;
261 AVFrame *out = td->out;
262 AVFrame *in = td->in;
263 const int channels = td->channels;
264 const int nb_samples = td->nb_samples;
265 const int start = (channels * jobnr) / nb_jobs;
266 const int end = (channels * (jobnr+1)) / nb_jobs;
268 s->filter(s, (void **)out->extended_data, (const void **)in->extended_data,
269 nb_samples, channels, start, end);
274 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
276 AVFilterContext *ctx = inlink->dst;
277 AVFilterLink *outlink = ctx->outputs[0];
278 int nb_samples, channels;
282 if (av_frame_is_writable(in)) {
285 out = ff_get_audio_buffer(outlink, in->nb_samples);
288 return AVERROR(ENOMEM);
290 av_frame_copy_props(out, in);
293 if (av_sample_fmt_is_planar(in->format)) {
294 nb_samples = in->nb_samples;
295 channels = in->channels;
297 nb_samples = in->channels * in->nb_samples;
303 td.nb_samples = nb_samples;
304 td.channels = channels;
305 ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
306 ff_filter_get_nb_threads(ctx)));
311 return ff_filter_frame(outlink, out);
314 static const AVFilterPad inputs[] = {
317 .type = AVMEDIA_TYPE_AUDIO,
318 .filter_frame = filter_frame,
319 .config_props = config_input,
324 static const AVFilterPad outputs[] = {
327 .type = AVMEDIA_TYPE_AUDIO,
332 AVFilter ff_af_asoftclip = {
334 .description = NULL_IF_CONFIG_SMALL("Audio Soft Clipper."),
335 .query_formats = query_formats,
336 .priv_size = sizeof(ASoftClipContext),
337 .priv_class = &asoftclip_class,
340 .process_command = ff_filter_process_command,
341 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
342 AVFILTER_FLAG_SLICE_THREADS,