]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_asoftclip.c
avformat/rtsp: check return value of ffurl_read_complete
[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 "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 param;
49
50     SwrContext *up_ctx;
51     SwrContext *down_ctx;
52
53     AVFrame *frame;
54
55     void (*filter)(struct ASoftClipContext *s, void **dst, const void **src,
56                    int nb_samples, int channels, int start, int end);
57 } ASoftClipContext;
58
59 #define OFFSET(x) offsetof(ASoftClipContext, x)
60 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
61 #define F AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
62
63 static const AVOption asoftclip_options[] = {
64     { "type", "set softclip type", OFFSET(type), AV_OPT_TYPE_INT,    {.i64=0},         -1, NB_TYPES-1, A, "types" },
65     { "hard",                NULL,            0, AV_OPT_TYPE_CONST,  {.i64=ASC_HARD},   0,          0, A, "types" },
66     { "tanh",                NULL,            0, AV_OPT_TYPE_CONST,  {.i64=ASC_TANH},   0,          0, A, "types" },
67     { "atan",                NULL,            0, AV_OPT_TYPE_CONST,  {.i64=ASC_ATAN},   0,          0, A, "types" },
68     { "cubic",               NULL,            0, AV_OPT_TYPE_CONST,  {.i64=ASC_CUBIC},  0,          0, A, "types" },
69     { "exp",                 NULL,            0, AV_OPT_TYPE_CONST,  {.i64=ASC_EXP},    0,          0, A, "types" },
70     { "alg",                 NULL,            0, AV_OPT_TYPE_CONST,  {.i64=ASC_ALG},    0,          0, A, "types" },
71     { "quintic",             NULL,            0, AV_OPT_TYPE_CONST,  {.i64=ASC_QUINTIC},0,          0, A, "types" },
72     { "sin",                 NULL,            0, AV_OPT_TYPE_CONST,  {.i64=ASC_SIN},    0,          0, A, "types" },
73     { "erf",                 NULL,            0, AV_OPT_TYPE_CONST,  {.i64=ASC_ERF},    0,          0, A, "types" },
74     { "param", "set softclip parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01,        3, A },
75     { "oversample", "set oversample factor", OFFSET(oversample), AV_OPT_TYPE_INT, {.i64=1}, 1, 32, F },
76     { NULL }
77 };
78
79 AVFILTER_DEFINE_CLASS(asoftclip);
80
81 static int query_formats(AVFilterContext *ctx)
82 {
83     AVFilterFormats *formats = NULL;
84     AVFilterChannelLayouts *layouts = NULL;
85     static const enum AVSampleFormat sample_fmts[] = {
86         AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
87         AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
88         AV_SAMPLE_FMT_NONE
89     };
90     int ret;
91
92     formats = ff_make_format_list(sample_fmts);
93     if (!formats)
94         return AVERROR(ENOMEM);
95     ret = ff_set_common_formats(ctx, formats);
96     if (ret < 0)
97         return ret;
98
99     layouts = ff_all_channel_counts();
100     if (!layouts)
101         return AVERROR(ENOMEM);
102
103     ret = ff_set_common_channel_layouts(ctx, layouts);
104     if (ret < 0)
105         return ret;
106
107     formats = ff_all_samplerates();
108     return ff_set_common_samplerates(ctx, formats);
109 }
110
111 #define SQR(x) ((x) * (x))
112
113 static void filter_flt(ASoftClipContext *s,
114                        void **dptr, const void **sptr,
115                        int nb_samples, int channels,
116                        int start, int end)
117 {
118     float param = s->param;
119
120     for (int c = start; c < end; c++) {
121         const float *src = sptr[c];
122         float *dst = dptr[c];
123
124         switch (s->type) {
125         case ASC_HARD:
126             for (int n = 0; n < nb_samples; n++) {
127                 dst[n] = av_clipf(src[n], -1.f, 1.f);
128             }
129             break;
130         case ASC_TANH:
131             for (int n = 0; n < nb_samples; n++) {
132                 dst[n] = tanhf(src[n] * param);
133             }
134             break;
135         case ASC_ATAN:
136             for (int n = 0; n < nb_samples; n++)
137                 dst[n] = 2.f / M_PI * atanf(src[n] * param);
138             break;
139         case ASC_CUBIC:
140             for (int n = 0; n < nb_samples; n++) {
141                 if (FFABS(src[n]) >= 1.5f)
142                     dst[n] = FFSIGN(src[n]);
143                 else
144                     dst[n] = src[n] - 0.1481f * powf(src[n], 3.f);
145             }
146             break;
147         case ASC_EXP:
148             for (int n = 0; n < nb_samples; n++)
149                 dst[n] = 2.f / (1.f + expf(-2.f * src[n])) - 1.;
150             break;
151         case ASC_ALG:
152             for (int n = 0; n < nb_samples; n++)
153                 dst[n] = src[n] / (sqrtf(param + src[n] * src[n]));
154             break;
155         case ASC_QUINTIC:
156             for (int n = 0; n < nb_samples; n++) {
157                 if (FFABS(src[n]) >= 1.25)
158                     dst[n] = FFSIGN(src[n]);
159                 else
160                     dst[n] = src[n] - 0.08192f * powf(src[n], 5.f);
161             }
162             break;
163         case ASC_SIN:
164             for (int n = 0; n < nb_samples; n++) {
165                 if (FFABS(src[n]) >= M_PI_2)
166                     dst[n] = FFSIGN(src[n]);
167                 else
168                     dst[n] = sinf(src[n]);
169             }
170             break;
171         case ASC_ERF:
172             for (int n = 0; n < nb_samples; n++) {
173                 dst[n] = erff(src[n]);
174             }
175             break;
176         default:
177             av_assert0(0);
178         }
179     }
180 }
181
182 static void filter_dbl(ASoftClipContext *s,
183                        void **dptr, const void **sptr,
184                        int nb_samples, int channels,
185                        int start, int end)
186 {
187     double param = s->param;
188
189     for (int c = start; c < end; c++) {
190         const double *src = sptr[c];
191         double *dst = dptr[c];
192
193         switch (s->type) {
194         case ASC_HARD:
195             for (int n = 0; n < nb_samples; n++) {
196                 dst[n] = av_clipd(src[n], -1., 1.);
197             }
198             break;
199         case ASC_TANH:
200             for (int n = 0; n < nb_samples; n++) {
201                 dst[n] = tanh(src[n] * param);
202             }
203             break;
204         case ASC_ATAN:
205             for (int n = 0; n < nb_samples; n++)
206                 dst[n] = 2. / M_PI * atan(src[n] * param);
207             break;
208         case ASC_CUBIC:
209             for (int n = 0; n < nb_samples; n++) {
210                 if (FFABS(src[n]) >= 1.5)
211                     dst[n] = FFSIGN(src[n]);
212                 else
213                     dst[n] = src[n] - 0.1481 * pow(src[n], 3.);
214             }
215             break;
216         case ASC_EXP:
217             for (int n = 0; n < nb_samples; n++)
218                 dst[n] = 2. / (1. + exp(-2. * src[n])) - 1.;
219             break;
220         case ASC_ALG:
221             for (int n = 0; n < nb_samples; n++)
222                 dst[n] = src[n] / (sqrt(param + src[n] * src[n]));
223             break;
224         case ASC_QUINTIC:
225             for (int n = 0; n < nb_samples; n++) {
226                 if (FFABS(src[n]) >= 1.25)
227                     dst[n] = FFSIGN(src[n]);
228                 else
229                     dst[n] = src[n] - 0.08192 * pow(src[n], 5.);
230             }
231             break;
232         case ASC_SIN:
233             for (int n = 0; n < nb_samples; n++) {
234                 if (FFABS(src[n]) >= M_PI_2)
235                     dst[n] = FFSIGN(src[n]);
236                 else
237                     dst[n] = sin(src[n]);
238             }
239             break;
240         case ASC_ERF:
241             for (int n = 0; n < nb_samples; n++) {
242                 dst[n] = erf(src[n]);
243             }
244             break;
245         default:
246             av_assert0(0);
247         }
248     }
249 }
250
251 static int config_input(AVFilterLink *inlink)
252 {
253     AVFilterContext *ctx = inlink->dst;
254     ASoftClipContext *s = ctx->priv;
255     int ret;
256
257     switch (inlink->format) {
258     case AV_SAMPLE_FMT_FLT:
259     case AV_SAMPLE_FMT_FLTP: s->filter = filter_flt; break;
260     case AV_SAMPLE_FMT_DBL:
261     case AV_SAMPLE_FMT_DBLP: s->filter = filter_dbl; break;
262     default: av_assert0(0);
263     }
264
265     if (s->oversample <= 1)
266         return 0;
267
268     s->up_ctx = swr_alloc();
269     s->down_ctx = swr_alloc();
270     if (!s->up_ctx || !s->down_ctx)
271         return AVERROR(ENOMEM);
272
273     av_opt_set_int(s->up_ctx, "in_channel_layout",    inlink->channel_layout, 0);
274     av_opt_set_int(s->up_ctx, "in_sample_rate",       inlink->sample_rate, 0);
275     av_opt_set_sample_fmt(s->up_ctx, "in_sample_fmt", inlink->format, 0);
276
277     av_opt_set_int(s->up_ctx, "out_channel_layout",    inlink->channel_layout, 0);
278     av_opt_set_int(s->up_ctx, "out_sample_rate",       inlink->sample_rate * s->oversample, 0);
279     av_opt_set_sample_fmt(s->up_ctx, "out_sample_fmt", inlink->format, 0);
280
281     av_opt_set_int(s->down_ctx, "in_channel_layout",    inlink->channel_layout, 0);
282     av_opt_set_int(s->down_ctx, "in_sample_rate",       inlink->sample_rate * s->oversample, 0);
283     av_opt_set_sample_fmt(s->down_ctx, "in_sample_fmt", inlink->format, 0);
284
285     av_opt_set_int(s->down_ctx, "out_channel_layout",    inlink->channel_layout, 0);
286     av_opt_set_int(s->down_ctx, "out_sample_rate",       inlink->sample_rate, 0);
287     av_opt_set_sample_fmt(s->down_ctx, "out_sample_fmt", inlink->format, 0);
288
289     ret = swr_init(s->up_ctx);
290     if (ret < 0)
291         return ret;
292
293     ret = swr_init(s->down_ctx);
294     if (ret < 0)
295         return ret;
296
297     return 0;
298 }
299
300 typedef struct ThreadData {
301     AVFrame *in, *out;
302     int nb_samples;
303     int channels;
304 } ThreadData;
305
306 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
307 {
308     ASoftClipContext *s = ctx->priv;
309     ThreadData *td = arg;
310     AVFrame *out = td->out;
311     AVFrame *in = td->in;
312     const int channels = td->channels;
313     const int nb_samples = td->nb_samples;
314     const int start = (channels * jobnr) / nb_jobs;
315     const int end = (channels * (jobnr+1)) / nb_jobs;
316
317     s->filter(s, (void **)out->extended_data, (const void **)in->extended_data,
318               nb_samples, channels, start, end);
319
320     return 0;
321 }
322
323 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
324 {
325     AVFilterContext *ctx = inlink->dst;
326     ASoftClipContext *s = ctx->priv;
327     AVFilterLink *outlink = ctx->outputs[0];
328     int ret, nb_samples, channels;
329     ThreadData td;
330     AVFrame *out;
331
332     if (av_frame_is_writable(in)) {
333         out = in;
334     } else {
335         out = ff_get_audio_buffer(outlink, in->nb_samples);
336         if (!out) {
337             av_frame_free(&in);
338             return AVERROR(ENOMEM);
339         }
340         av_frame_copy_props(out, in);
341     }
342
343     if (av_sample_fmt_is_planar(in->format)) {
344         nb_samples = in->nb_samples;
345         channels = in->channels;
346     } else {
347         nb_samples = in->channels * in->nb_samples;
348         channels = 1;
349     }
350
351     if (s->oversample > 1) {
352         s->frame = ff_get_audio_buffer(outlink, in->nb_samples * s->oversample);
353         if (!s->frame) {
354             ret = AVERROR(ENOMEM);
355             goto fail;
356         }
357
358         ret = swr_convert(s->up_ctx, (uint8_t**)s->frame->extended_data, in->nb_samples * s->oversample,
359                           (const uint8_t **)in->extended_data, in->nb_samples);
360         if (ret < 0)
361             goto fail;
362
363         td.in = s->frame;
364         td.out = s->frame;
365         td.nb_samples = av_sample_fmt_is_planar(in->format) ? ret : ret * in->channels;
366         td.channels = channels;
367         ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
368                                                                 ff_filter_get_nb_threads(ctx)));
369
370         ret = swr_convert(s->down_ctx, (uint8_t**)out->extended_data, out->nb_samples,
371                           (const uint8_t **)s->frame->extended_data, ret);
372         if (ret < 0)
373             goto fail;
374
375         if (out->pts)
376             out->pts -= s->delay;
377         s->delay += in->nb_samples - ret;
378         out->nb_samples = ret;
379
380         av_frame_free(&s->frame);
381     } else {
382         td.in = in;
383         td.out = out;
384         td.nb_samples = nb_samples;
385         td.channels = channels;
386         ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
387                                                                 ff_filter_get_nb_threads(ctx)));
388     }
389
390     if (out != in)
391         av_frame_free(&in);
392
393     return ff_filter_frame(outlink, out);
394 fail:
395     if (out != in)
396         av_frame_free(&out);
397     av_frame_free(&in);
398     av_frame_free(&s->frame);
399
400     return ret;
401 }
402
403 static av_cold void uninit(AVFilterContext *ctx)
404 {
405     ASoftClipContext *s = ctx->priv;
406
407     swr_free(&s->up_ctx);
408     swr_free(&s->down_ctx);
409 }
410
411 static const AVFilterPad inputs[] = {
412     {
413         .name         = "default",
414         .type         = AVMEDIA_TYPE_AUDIO,
415         .filter_frame = filter_frame,
416         .config_props = config_input,
417     },
418     { NULL }
419 };
420
421 static const AVFilterPad outputs[] = {
422     {
423         .name = "default",
424         .type = AVMEDIA_TYPE_AUDIO,
425     },
426     { NULL }
427 };
428
429 AVFilter ff_af_asoftclip = {
430     .name           = "asoftclip",
431     .description    = NULL_IF_CONFIG_SMALL("Audio Soft Clipper."),
432     .query_formats  = query_formats,
433     .priv_size      = sizeof(ASoftClipContext),
434     .priv_class     = &asoftclip_class,
435     .inputs         = inputs,
436     .outputs        = outputs,
437     .uninit         = uninit,
438     .process_command = ff_filter_process_command,
439     .flags          = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
440                       AVFILTER_FLAG_SLICE_THREADS,
441 };