1 /*
2 * Copyright (c) 2019 The FFmpeg Project
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/opt.h"
24 #include "libswresample/swresample.h"
25 #include "avfilter.h"
26 #include "audio.h"
27 #include "formats.h"
28
29 enum ASoftClipTypes {
30 ASC_HARD = -1,
31 ASC_TANH,
32 ASC_ATAN,
33 ASC_CUBIC,
34 ASC_EXP,
35 ASC_ALG,
36 ASC_QUINTIC,
37 ASC_SIN,
38 ASC_ERF,
39 NB_TYPES,
40 };
41
42 typedef struct ASoftClipContext {
43 const AVClass *class;
44
45 int type;
46 int oversample;
47 int64_t delay;
48 double threshold;
49 double output;
50 double param;
51
52 SwrContext *up_ctx;
53 SwrContext *down_ctx;
54
55 AVFrame *frame;
56
57 void (*filter)(struct ASoftClipContext *s, void **dst, const void **src,
58 int nb_samples, int channels, int start, int end);
59 } ASoftClipContext;
60
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
64
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 },
80 { NULL }
81 };
82
83 AVFILTER_DEFINE_CLASS(asoftclip);
84
query_formats(AVFilterContext * ctx)85 static int query_formats(AVFilterContext *ctx)
86 {
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,
92 AV_SAMPLE_FMT_NONE
93 };
94 int ret;
95
96 formats = ff_make_format_list(sample_fmts);
97 if (!formats)
98 return AVERROR(ENOMEM);
99 ret = ff_set_common_formats(ctx, formats);
100 if (ret < 0)
101 return ret;
102
103 layouts = ff_all_channel_counts();
104 if (!layouts)
105 return AVERROR(ENOMEM);
106
107 ret = ff_set_common_channel_layouts(ctx, layouts);
108 if (ret < 0)
109 return ret;
110
111 formats = ff_all_samplerates();
112 return ff_set_common_samplerates(ctx, formats);
113 }
114
filter_flt(ASoftClipContext * s,void ** dptr,const void ** sptr,int nb_samples,int channels,int start,int end)115 static void filter_flt(ASoftClipContext *s,
116 void **dptr, const void **sptr,
117 int nb_samples, int channels,
118 int start, int end)
119 {
120 float threshold = s->threshold;
121 float gain = s->output * threshold;
122 float factor = 1.f / threshold;
123 float param = s->param;
124
125 for (int c = start; c < end; c++) {
126 const float *src = sptr[c];
127 float *dst = dptr[c];
128
129 switch (s->type) {
130 case ASC_HARD:
131 for (int n = 0; n < nb_samples; n++) {
132 dst[n] = av_clipf(src[n] * factor, -1.f, 1.f);
133 dst[n] *= gain;
134 }
135 break;
136 case ASC_TANH:
137 for (int n = 0; n < nb_samples; n++) {
138 dst[n] = tanhf(src[n] * factor * param);
139 dst[n] *= gain;
140 }
141 break;
142 case ASC_ATAN:
143 for (int n = 0; n < nb_samples; n++) {
144 dst[n] = 2.f / M_PI * atanf(src[n] * factor * param);
145 dst[n] *= gain;
146 }
147 break;
148 case ASC_CUBIC:
149 for (int n = 0; n < nb_samples; n++) {
150 float sample = src[n] * factor;
151
152 if (FFABS(sample) >= 1.5f)
153 dst[n] = FFSIGN(sample);
154 else
155 dst[n] = sample - 0.1481f * powf(sample, 3.f);
156 dst[n] *= gain;
157 }
158 break;
159 case ASC_EXP:
160 for (int n = 0; n < nb_samples; n++) {
161 dst[n] = 2.f / (1.f + expf(-2.f * src[n] * factor)) - 1.;
162 dst[n] *= gain;
163 }
164 break;
165 case ASC_ALG:
166 for (int n = 0; n < nb_samples; n++) {
167 float sample = src[n] * factor;
168
169 dst[n] = sample / (sqrtf(param + sample * sample));
170 dst[n] *= gain;
171 }
172 break;
173 case ASC_QUINTIC:
174 for (int n = 0; n < nb_samples; n++) {
175 float sample = src[n] * factor;
176
177 if (FFABS(sample) >= 1.25)
178 dst[n] = FFSIGN(sample);
179 else
180 dst[n] = sample - 0.08192f * powf(sample, 5.f);
181 dst[n] *= gain;
182 }
183 break;
184 case ASC_SIN:
185 for (int n = 0; n < nb_samples; n++) {
186 float sample = src[n] * factor;
187
188 if (FFABS(sample) >= M_PI_2)
189 dst[n] = FFSIGN(sample);
190 else
191 dst[n] = sinf(sample);
192 dst[n] *= gain;
193 }
194 break;
195 case ASC_ERF:
196 for (int n = 0; n < nb_samples; n++) {
197 dst[n] = erff(src[n] * factor);
198 dst[n] *= gain;
199 }
200 break;
201 default:
202 av_assert0(0);
203 }
204 }
205 }
206
filter_dbl(ASoftClipContext * s,void ** dptr,const void ** sptr,int nb_samples,int channels,int start,int end)207 static void filter_dbl(ASoftClipContext *s,
208 void **dptr, const void **sptr,
209 int nb_samples, int channels,
210 int start, int end)
211 {
212 double threshold = s->threshold;
213 double gain = s->output * threshold;
214 double factor = 1. / threshold;
215 double param = s->param;
216
217 for (int c = start; c < end; c++) {
218 const double *src = sptr[c];
219 double *dst = dptr[c];
220
221 switch (s->type) {
222 case ASC_HARD:
223 for (int n = 0; n < nb_samples; n++) {
224 dst[n] = av_clipd(src[n] * factor, -1., 1.);
225 dst[n] *= gain;
226 }
227 break;
228 case ASC_TANH:
229 for (int n = 0; n < nb_samples; n++) {
230 dst[n] = tanh(src[n] * factor * param);
231 dst[n] *= gain;
232 }
233 break;
234 case ASC_ATAN:
235 for (int n = 0; n < nb_samples; n++) {
236 dst[n] = 2. / M_PI * atan(src[n] * factor * param);
237 dst[n] *= gain;
238 }
239 break;
240 case ASC_CUBIC:
241 for (int n = 0; n < nb_samples; n++) {
242 double sample = src[n] * factor;
243
244 if (FFABS(sample) >= 1.5)
245 dst[n] = FFSIGN(sample);
246 else
247 dst[n] = sample - 0.1481 * pow(sample, 3.);
248 dst[n] *= gain;
249 }
250 break;
251 case ASC_EXP:
252 for (int n = 0; n < nb_samples; n++) {
253 dst[n] = 2. / (1. + exp(-2. * src[n] * factor)) - 1.;
254 dst[n] *= gain;
255 }
256 break;
257 case ASC_ALG:
258 for (int n = 0; n < nb_samples; n++) {
259 double sample = src[n] * factor;
260
261 dst[n] = sample / (sqrt(param + sample * sample));
262 dst[n] *= gain;
263 }
264 break;
265 case ASC_QUINTIC:
266 for (int n = 0; n < nb_samples; n++) {
267 double sample = src[n] * factor;
268
269 if (FFABS(sample) >= 1.25)
270 dst[n] = FFSIGN(sample);
271 else
272 dst[n] = sample - 0.08192 * pow(sample, 5.);
273 dst[n] *= gain;
274 }
275 break;
276 case ASC_SIN:
277 for (int n = 0; n < nb_samples; n++) {
278 double sample = src[n] * factor;
279
280 if (FFABS(sample) >= M_PI_2)
281 dst[n] = FFSIGN(sample);
282 else
283 dst[n] = sin(sample);
284 dst[n] *= gain;
285 }
286 break;
287 case ASC_ERF:
288 for (int n = 0; n < nb_samples; n++) {
289 dst[n] = erf(src[n] * factor);
290 dst[n] *= gain;
291 }
292 break;
293 default:
294 av_assert0(0);
295 }
296 }
297 }
298
config_input(AVFilterLink * inlink)299 static int config_input(AVFilterLink *inlink)
300 {
301 AVFilterContext *ctx = inlink->dst;
302 ASoftClipContext *s = ctx->priv;
303 int ret;
304
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);
311 }
312
313 if (s->oversample <= 1)
314 return 0;
315
316 s->up_ctx = swr_alloc();
317 s->down_ctx = swr_alloc();
318 if (!s->up_ctx || !s->down_ctx)
319 return AVERROR(ENOMEM);
320
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);
324
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);
328
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);
332
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);
336
337 ret = swr_init(s->up_ctx);
338 if (ret < 0)
339 return ret;
340
341 ret = swr_init(s->down_ctx);
342 if (ret < 0)
343 return ret;
344
345 return 0;
346 }
347
348 typedef struct ThreadData {
349 AVFrame *in, *out;
350 int nb_samples;
351 int channels;
352 } ThreadData;
353
filter_channels(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)354 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
355 {
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;
364
365 s->filter(s, (void **)out->extended_data, (const void **)in->extended_data,
366 nb_samples, channels, start, end);
367
368 return 0;
369 }
370
filter_frame(AVFilterLink * inlink,AVFrame * in)371 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
372 {
373 AVFilterContext *ctx = inlink->dst;
374 ASoftClipContext *s = ctx->priv;
375 AVFilterLink *outlink = ctx->outputs[0];
376 int ret, nb_samples, channels;
377 ThreadData td;
378 AVFrame *out;
379
380 if (av_frame_is_writable(in)) {
381 out = in;
382 } else {
383 out = ff_get_audio_buffer(outlink, in->nb_samples);
384 if (!out) {
385 av_frame_free(&in);
386 return AVERROR(ENOMEM);
387 }
388 av_frame_copy_props(out, in);
389 }
390
391 if (av_sample_fmt_is_planar(in->format)) {
392 nb_samples = in->nb_samples;
393 channels = in->channels;
394 } else {
395 nb_samples = in->channels * in->nb_samples;
396 channels = 1;
397 }
398
399 if (s->oversample > 1) {
400 s->frame = ff_get_audio_buffer(outlink, in->nb_samples * s->oversample);
401 if (!s->frame) {
402 ret = AVERROR(ENOMEM);
403 goto fail;
404 }
405
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);
408 if (ret < 0)
409 goto fail;
410
411 td.in = s->frame;
412 td.out = s->frame;
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)));
417
418 ret = swr_convert(s->down_ctx, (uint8_t**)out->extended_data, out->nb_samples,
419 (const uint8_t **)s->frame->extended_data, ret);
420 if (ret < 0)
421 goto fail;
422
423 if (out->pts)
424 out->pts -= s->delay;
425 s->delay += in->nb_samples - ret;
426 out->nb_samples = ret;
427
428 av_frame_free(&s->frame);
429 } else {
430 td.in = in;
431 td.out = out;
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)));
436 }
437
438 if (out != in)
439 av_frame_free(&in);
440
441 return ff_filter_frame(outlink, out);
442 fail:
443 if (out != in)
444 av_frame_free(&out);
445 av_frame_free(&in);
446 av_frame_free(&s->frame);
447
448 return ret;
449 }
450
uninit(AVFilterContext * ctx)451 static av_cold void uninit(AVFilterContext *ctx)
452 {
453 ASoftClipContext *s = ctx->priv;
454
455 swr_free(&s->up_ctx);
456 swr_free(&s->down_ctx);
457 }
458
459 static const AVFilterPad inputs[] = {
460 {
461 .name = "default",
462 .type = AVMEDIA_TYPE_AUDIO,
463 .filter_frame = filter_frame,
464 .config_props = config_input,
465 },
466 { NULL }
467 };
468
469 static const AVFilterPad outputs[] = {
470 {
471 .name = "default",
472 .type = AVMEDIA_TYPE_AUDIO,
473 },
474 { NULL }
475 };
476
477 AVFilter ff_af_asoftclip = {
478 .name = "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,
483 .inputs = inputs,
484 .outputs = outputs,
485 .uninit = uninit,
486 .process_command = ff_filter_process_command,
487 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
488 AVFILTER_FLAG_SLICE_THREADS,
489 };
490