]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_asoftclip.c
avfitler/af_asoftclip: add hard type
[ffmpeg] / libavfilter / af_asoftclip.c
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/channel_layout.h"
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "audio.h"
25 #include "formats.h"
26
27 enum ASoftClipTypes {
28     ASC_HARD = -1,
29     ASC_TANH,
30     ASC_ATAN,
31     ASC_CUBIC,
32     ASC_EXP,
33     ASC_ALG,
34     ASC_QUINTIC,
35     ASC_SIN,
36     ASC_ERF,
37     NB_TYPES,
38 };
39
40 typedef struct ASoftClipContext {
41     const AVClass *class;
42
43     int type;
44     double param;
45
46     void (*filter)(struct ASoftClipContext *s, void **dst, const void **src,
47                    int nb_samples, int channels, int start, int end);
48 } ASoftClipContext;
49
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
52
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 },
65     { NULL }
66 };
67
68 AVFILTER_DEFINE_CLASS(asoftclip);
69
70 static int query_formats(AVFilterContext *ctx)
71 {
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,
77         AV_SAMPLE_FMT_NONE
78     };
79     int ret;
80
81     formats = ff_make_format_list(sample_fmts);
82     if (!formats)
83         return AVERROR(ENOMEM);
84     ret = ff_set_common_formats(ctx, formats);
85     if (ret < 0)
86         return ret;
87
88     layouts = ff_all_channel_counts();
89     if (!layouts)
90         return AVERROR(ENOMEM);
91
92     ret = ff_set_common_channel_layouts(ctx, layouts);
93     if (ret < 0)
94         return ret;
95
96     formats = ff_all_samplerates();
97     return ff_set_common_samplerates(ctx, formats);
98 }
99
100 #define SQR(x) ((x) * (x))
101
102 static void filter_flt(ASoftClipContext *s,
103                        void **dptr, const void **sptr,
104                        int nb_samples, int channels,
105                        int start, int end)
106 {
107     float param = s->param;
108
109     for (int c = start; c < end; c++) {
110         const float *src = sptr[c];
111         float *dst = dptr[c];
112
113         switch (s->type) {
114         case ASC_HARD:
115             for (int n = 0; n < nb_samples; n++) {
116                 dst[n] = av_clipf(src[n], -1.f, 1.f);
117             }
118             break;
119         case ASC_TANH:
120             for (int n = 0; n < nb_samples; n++) {
121                 dst[n] = tanhf(src[n] * param);
122             }
123             break;
124         case ASC_ATAN:
125             for (int n = 0; n < nb_samples; n++)
126                 dst[n] = 2.f / M_PI * atanf(src[n] * param);
127             break;
128         case ASC_CUBIC:
129             for (int n = 0; n < nb_samples; n++) {
130                 if (FFABS(src[n]) >= 1.5f)
131                     dst[n] = FFSIGN(src[n]);
132                 else
133                     dst[n] = src[n] - 0.1481f * powf(src[n], 3.f);
134             }
135             break;
136         case ASC_EXP:
137             for (int n = 0; n < nb_samples; n++)
138                 dst[n] = 2.f / (1.f + expf(-2.f * src[n])) - 1.;
139             break;
140         case ASC_ALG:
141             for (int n = 0; n < nb_samples; n++)
142                 dst[n] = src[n] / (sqrtf(param + src[n] * src[n]));
143             break;
144         case ASC_QUINTIC:
145             for (int n = 0; n < nb_samples; n++) {
146                 if (FFABS(src[n]) >= 1.25)
147                     dst[n] = FFSIGN(src[n]);
148                 else
149                     dst[n] = src[n] - 0.08192f * powf(src[n], 5.f);
150             }
151             break;
152         case ASC_SIN:
153             for (int n = 0; n < nb_samples; n++) {
154                 if (FFABS(src[n]) >= M_PI_2)
155                     dst[n] = FFSIGN(src[n]);
156                 else
157                     dst[n] = sinf(src[n]);
158             }
159             break;
160         case ASC_ERF:
161             for (int n = 0; n < nb_samples; n++) {
162                 dst[n] = erff(src[n]);
163             }
164             break;
165         }
166     }
167 }
168
169 static void filter_dbl(ASoftClipContext *s,
170                        void **dptr, const void **sptr,
171                        int nb_samples, int channels,
172                        int start, int end)
173 {
174     double param = s->param;
175
176     for (int c = start; c < end; c++) {
177         const double *src = sptr[c];
178         double *dst = dptr[c];
179
180         switch (s->type) {
181         case ASC_HARD:
182             for (int n = 0; n < nb_samples; n++) {
183                 dst[n] = av_clipd(src[n], -1., 1.);
184             }
185             break;
186         case ASC_TANH:
187             for (int n = 0; n < nb_samples; n++) {
188                 dst[n] = tanh(src[n] * param);
189             }
190             break;
191         case ASC_ATAN:
192             for (int n = 0; n < nb_samples; n++)
193                 dst[n] = 2. / M_PI * atan(src[n] * param);
194             break;
195         case ASC_CUBIC:
196             for (int n = 0; n < nb_samples; n++) {
197                 if (FFABS(src[n]) >= 1.5)
198                     dst[n] = FFSIGN(src[n]);
199                 else
200                     dst[n] = src[n] - 0.1481 * pow(src[n], 3.);
201             }
202             break;
203         case ASC_EXP:
204             for (int n = 0; n < nb_samples; n++)
205                 dst[n] = 2. / (1. + exp(-2. * src[n])) - 1.;
206             break;
207         case ASC_ALG:
208             for (int n = 0; n < nb_samples; n++)
209                 dst[n] = src[n] / (sqrt(param + src[n] * src[n]));
210             break;
211         case ASC_QUINTIC:
212             for (int n = 0; n < nb_samples; n++) {
213                 if (FFABS(src[n]) >= 1.25)
214                     dst[n] = FFSIGN(src[n]);
215                 else
216                     dst[n] = src[n] - 0.08192 * pow(src[n], 5.);
217             }
218             break;
219         case ASC_SIN:
220             for (int n = 0; n < nb_samples; n++) {
221                 if (FFABS(src[n]) >= M_PI_2)
222                     dst[n] = FFSIGN(src[n]);
223                 else
224                     dst[n] = sin(src[n]);
225             }
226             break;
227         case ASC_ERF:
228             for (int n = 0; n < nb_samples; n++) {
229                 dst[n] = erf(src[n]);
230             }
231             break;
232         }
233     }
234 }
235
236 static int config_input(AVFilterLink *inlink)
237 {
238     AVFilterContext *ctx = inlink->dst;
239     ASoftClipContext *s = ctx->priv;
240
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;
246     }
247
248     return 0;
249 }
250
251 typedef struct ThreadData {
252     AVFrame *in, *out;
253     int nb_samples;
254     int channels;
255 } ThreadData;
256
257 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
258 {
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;
267
268     s->filter(s, (void **)out->extended_data, (const void **)in->extended_data,
269               nb_samples, channels, start, end);
270
271     return 0;
272 }
273
274 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
275 {
276     AVFilterContext *ctx = inlink->dst;
277     AVFilterLink *outlink = ctx->outputs[0];
278     int nb_samples, channels;
279     ThreadData td;
280     AVFrame *out;
281
282     if (av_frame_is_writable(in)) {
283         out = in;
284     } else {
285         out = ff_get_audio_buffer(outlink, in->nb_samples);
286         if (!out) {
287             av_frame_free(&in);
288             return AVERROR(ENOMEM);
289         }
290         av_frame_copy_props(out, in);
291     }
292
293     if (av_sample_fmt_is_planar(in->format)) {
294         nb_samples = in->nb_samples;
295         channels = in->channels;
296     } else {
297         nb_samples = in->channels * in->nb_samples;
298         channels = 1;
299     }
300
301     td.in = in;
302     td.out = out;
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)));
307
308     if (out != in)
309         av_frame_free(&in);
310
311     return ff_filter_frame(outlink, out);
312 }
313
314 static const AVFilterPad inputs[] = {
315     {
316         .name         = "default",
317         .type         = AVMEDIA_TYPE_AUDIO,
318         .filter_frame = filter_frame,
319         .config_props = config_input,
320     },
321     { NULL }
322 };
323
324 static const AVFilterPad outputs[] = {
325     {
326         .name = "default",
327         .type = AVMEDIA_TYPE_AUDIO,
328     },
329     { NULL }
330 };
331
332 AVFilter ff_af_asoftclip = {
333     .name           = "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,
338     .inputs         = inputs,
339     .outputs        = outputs,
340     .process_command = ff_filter_process_command,
341     .flags          = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
342                       AVFILTER_FLAG_SLICE_THREADS,
343 };