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