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"
38 typedef struct ASoftClipContext {
44 void (*filter)(struct ASoftClipContext *s, void **dst, const void **src,
45 int nb_samples, int channels, int start, int end);
48 #define OFFSET(x) offsetof(ASoftClipContext, x)
49 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51 static const AVOption asoftclip_options[] = {
52 { "type", "set softclip type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_TYPES-1, A, "types" },
53 { "tanh", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_TANH}, 0, 0, A, "types" },
54 { "atan", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ATAN}, 0, 0, A, "types" },
55 { "cubic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_CUBIC}, 0, 0, A, "types" },
56 { "exp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_EXP}, 0, 0, A, "types" },
57 { "alg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ALG}, 0, 0, A, "types" },
58 { "quintic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_QUINTIC},0, 0, A, "types" },
59 { "sin", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_SIN}, 0, 0, A, "types" },
60 { "param", "set softclip parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 3, A },
64 AVFILTER_DEFINE_CLASS(asoftclip);
66 static int query_formats(AVFilterContext *ctx)
68 AVFilterFormats *formats = NULL;
69 AVFilterChannelLayouts *layouts = NULL;
70 static const enum AVSampleFormat sample_fmts[] = {
71 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
72 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
77 formats = ff_make_format_list(sample_fmts);
79 return AVERROR(ENOMEM);
80 ret = ff_set_common_formats(ctx, formats);
84 layouts = ff_all_channel_counts();
86 return AVERROR(ENOMEM);
88 ret = ff_set_common_channel_layouts(ctx, layouts);
92 formats = ff_all_samplerates();
93 return ff_set_common_samplerates(ctx, formats);
96 #define SQR(x) ((x) * (x))
98 static void filter_flt(ASoftClipContext *s,
99 void **dptr, const void **sptr,
100 int nb_samples, int channels,
103 float param = s->param;
105 for (int c = start; c < end; c++) {
106 const float *src = sptr[c];
107 float *dst = dptr[c];
111 for (int n = 0; n < nb_samples; n++) {
112 dst[n] = tanhf(src[n] * param);
116 for (int n = 0; n < nb_samples; n++)
117 dst[n] = 2.f / M_PI * atanf(src[n] * param);
120 for (int n = 0; n < nb_samples; n++) {
121 if (FFABS(src[n]) >= 1.5f)
122 dst[n] = FFSIGN(src[n]);
124 dst[n] = src[n] - 0.1481f * powf(src[n], 3.f);
128 for (int n = 0; n < nb_samples; n++)
129 dst[n] = 2.f / (1.f + expf(-2.f * src[n])) - 1.;
132 for (int n = 0; n < nb_samples; n++)
133 dst[n] = src[n] / (sqrtf(param + src[n] * src[n]));
136 for (int n = 0; n < nb_samples; n++) {
137 if (FFABS(src[n]) >= 1.25)
138 dst[n] = FFSIGN(src[n]);
140 dst[n] = src[n] - 0.08192f * powf(src[n], 5.f);
144 for (int n = 0; n < nb_samples; n++) {
145 if (FFABS(src[n]) >= M_PI_2)
146 dst[n] = FFSIGN(src[n]);
148 dst[n] = sinf(src[n]);
155 static void filter_dbl(ASoftClipContext *s,
156 void **dptr, const void **sptr,
157 int nb_samples, int channels,
160 double param = s->param;
162 for (int c = start; c < end; c++) {
163 const double *src = sptr[c];
164 double *dst = dptr[c];
168 for (int n = 0; n < nb_samples; n++) {
169 dst[n] = tanh(src[n] * param);
173 for (int n = 0; n < nb_samples; n++)
174 dst[n] = 2. / M_PI * atan(src[n] * param);
177 for (int n = 0; n < nb_samples; n++) {
178 if (FFABS(src[n]) >= 1.5)
179 dst[n] = FFSIGN(src[n]);
181 dst[n] = src[n] - 0.1481 * pow(src[n], 3.);
185 for (int n = 0; n < nb_samples; n++)
186 dst[n] = 2. / (1. + exp(-2. * src[n])) - 1.;
189 for (int n = 0; n < nb_samples; n++)
190 dst[n] = src[n] / (sqrt(param + src[n] * src[n]));
193 for (int n = 0; n < nb_samples; n++) {
194 if (FFABS(src[n]) >= 1.25)
195 dst[n] = FFSIGN(src[n]);
197 dst[n] = src[n] - 0.08192 * pow(src[n], 5.);
201 for (int n = 0; n < nb_samples; n++) {
202 if (FFABS(src[n]) >= M_PI_2)
203 dst[n] = FFSIGN(src[n]);
205 dst[n] = sin(src[n]);
212 static int config_input(AVFilterLink *inlink)
214 AVFilterContext *ctx = inlink->dst;
215 ASoftClipContext *s = ctx->priv;
217 switch (inlink->format) {
218 case AV_SAMPLE_FMT_FLT:
219 case AV_SAMPLE_FMT_FLTP: s->filter = filter_flt; break;
220 case AV_SAMPLE_FMT_DBL:
221 case AV_SAMPLE_FMT_DBLP: s->filter = filter_dbl; break;
227 typedef struct ThreadData {
233 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
235 ASoftClipContext *s = ctx->priv;
236 ThreadData *td = arg;
237 AVFrame *out = td->out;
238 AVFrame *in = td->in;
239 const int channels = td->channels;
240 const int nb_samples = td->nb_samples;
241 const int start = (channels * jobnr) / nb_jobs;
242 const int end = (channels * (jobnr+1)) / nb_jobs;
244 s->filter(s, (void **)out->extended_data, (const void **)in->extended_data,
245 nb_samples, channels, start, end);
250 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
252 AVFilterContext *ctx = inlink->dst;
253 AVFilterLink *outlink = ctx->outputs[0];
254 int nb_samples, channels;
258 if (av_frame_is_writable(in)) {
261 out = ff_get_audio_buffer(outlink, in->nb_samples);
264 return AVERROR(ENOMEM);
266 av_frame_copy_props(out, in);
269 if (av_sample_fmt_is_planar(in->format)) {
270 nb_samples = in->nb_samples;
271 channels = in->channels;
273 nb_samples = in->channels * in->nb_samples;
279 td.nb_samples = nb_samples;
280 td.channels = channels;
281 ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
282 ff_filter_get_nb_threads(ctx)));
287 return ff_filter_frame(outlink, out);
290 static const AVFilterPad inputs[] = {
293 .type = AVMEDIA_TYPE_AUDIO,
294 .filter_frame = filter_frame,
295 .config_props = config_input,
300 static const AVFilterPad outputs[] = {
303 .type = AVMEDIA_TYPE_AUDIO,
308 AVFilter ff_af_asoftclip = {
310 .description = NULL_IF_CONFIG_SMALL("Audio Soft Clipper."),
311 .query_formats = query_formats,
312 .priv_size = sizeof(ASoftClipContext),
313 .priv_class = &asoftclip_class,
316 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
317 AVFILTER_FLAG_SLICE_THREADS,