]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_afade.c
Merge commit '10a9149de242c7bbc4e130d3d7c593b89e20f80e'
[ffmpeg] / libavfilter / af_afade.c
1 /*
2  * Copyright (c) 2013 Paul B Mahol
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 /**
22  * @file
23  * fade audio filter
24  */
25
26 #include "libavutil/opt.h"
27 #include "audio.h"
28 #include "avfilter.h"
29 #include "internal.h"
30
31 typedef struct {
32     const AVClass *class;
33     int type;
34     int curve;
35     int nb_samples;
36     int64_t start_sample;
37     int64_t duration;
38     int64_t start_time;
39
40     void (*fade_samples)(uint8_t **dst, uint8_t * const *src,
41                          int nb_samples, int channels, int direction,
42                          int64_t start, int range, int curve);
43 } AudioFadeContext;
44
45 enum CurveType { TRI, QSIN, ESIN, HSIN, LOG, IPAR, QUA, CUB, SQU, CBR, PAR, EXP, IQSIN, IHSIN, DESE, DESI, NB_CURVES };
46
47 #define OFFSET(x) offsetof(AudioFadeContext, x)
48 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
49
50 static const AVOption afade_options[] = {
51     { "type",         "set the fade direction",                      OFFSET(type),         AV_OPT_TYPE_INT,    {.i64 = 0    }, 0, 1, FLAGS, "type" },
52     { "t",            "set the fade direction",                      OFFSET(type),         AV_OPT_TYPE_INT,    {.i64 = 0    }, 0, 1, FLAGS, "type" },
53     { "in",           "fade-in",                                     0,                    AV_OPT_TYPE_CONST,  {.i64 = 0    }, 0, 0, FLAGS, "type" },
54     { "out",          "fade-out",                                    0,                    AV_OPT_TYPE_CONST,  {.i64 = 1    }, 0, 0, FLAGS, "type" },
55     { "start_sample", "set number of first sample to start fading",  OFFSET(start_sample), AV_OPT_TYPE_INT64,  {.i64 = 0    }, 0, INT64_MAX, FLAGS },
56     { "ss",           "set number of first sample to start fading",  OFFSET(start_sample), AV_OPT_TYPE_INT64,  {.i64 = 0    }, 0, INT64_MAX, FLAGS },
57     { "nb_samples",   "set number of samples for fade duration",     OFFSET(nb_samples),   AV_OPT_TYPE_INT,    {.i64 = 44100}, 1, INT32_MAX, FLAGS },
58     { "ns",           "set number of samples for fade duration",     OFFSET(nb_samples),   AV_OPT_TYPE_INT,    {.i64 = 44100}, 1, INT32_MAX, FLAGS },
59     { "start_time",   "set time to start fading",                    OFFSET(start_time),   AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
60     { "st",           "set time to start fading",                    OFFSET(start_time),   AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
61     { "duration",     "set fade duration",                           OFFSET(duration),     AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
62     { "d",            "set fade duration",                           OFFSET(duration),     AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
63     { "curve",        "set fade curve type",                         OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
64     { "c",            "set fade curve type",                         OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
65     { "tri",          "linear slope",                                0,                    AV_OPT_TYPE_CONST,  {.i64 = TRI  }, 0, 0, FLAGS, "curve" },
66     { "qsin",         "quarter of sine wave",                        0,                    AV_OPT_TYPE_CONST,  {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
67     { "esin",         "exponential sine wave",                       0,                    AV_OPT_TYPE_CONST,  {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
68     { "hsin",         "half of sine wave",                           0,                    AV_OPT_TYPE_CONST,  {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
69     { "log",          "logarithmic",                                 0,                    AV_OPT_TYPE_CONST,  {.i64 = LOG  }, 0, 0, FLAGS, "curve" },
70     { "ipar",         "inverted parabola",                           0,                    AV_OPT_TYPE_CONST,  {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
71     { "qua",          "quadratic",                                   0,                    AV_OPT_TYPE_CONST,  {.i64 = QUA  }, 0, 0, FLAGS, "curve" },
72     { "cub",          "cubic",                                       0,                    AV_OPT_TYPE_CONST,  {.i64 = CUB  }, 0, 0, FLAGS, "curve" },
73     { "squ",          "square root",                                 0,                    AV_OPT_TYPE_CONST,  {.i64 = SQU  }, 0, 0, FLAGS, "curve" },
74     { "cbr",          "cubic root",                                  0,                    AV_OPT_TYPE_CONST,  {.i64 = CBR  }, 0, 0, FLAGS, "curve" },
75     { "par",          "parabola",                                    0,                    AV_OPT_TYPE_CONST,  {.i64 = PAR  }, 0, 0, FLAGS, "curve" },
76     { "exp",          "exponential",                                 0,                    AV_OPT_TYPE_CONST,  {.i64 = EXP  }, 0, 0, FLAGS, "curve" },
77     { "iqsin",        "inverted quarter of sine wave",               0,                    AV_OPT_TYPE_CONST,  {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
78     { "ihsin",        "inverted half of sine wave",                  0,                    AV_OPT_TYPE_CONST,  {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
79     { "dese",         "double-exponential seat",                     0,                    AV_OPT_TYPE_CONST,  {.i64 = DESE }, 0, 0, FLAGS, "curve" },
80     { "desi",         "double-exponential sigmoid",                  0,                    AV_OPT_TYPE_CONST,  {.i64 = DESI }, 0, 0, FLAGS, "curve" },
81     { NULL }
82 };
83
84 AVFILTER_DEFINE_CLASS(afade);
85
86 static av_cold int init(AVFilterContext *ctx)
87 {
88     AudioFadeContext *s = ctx->priv;
89
90     if (INT64_MAX - s->nb_samples < s->start_sample)
91         return AVERROR(EINVAL);
92
93     return 0;
94 }
95
96 static int query_formats(AVFilterContext *ctx)
97 {
98     AVFilterFormats *formats;
99     AVFilterChannelLayouts *layouts;
100     static const enum AVSampleFormat sample_fmts[] = {
101         AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
102         AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
103         AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
104         AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
105         AV_SAMPLE_FMT_NONE
106     };
107     int ret;
108
109     layouts = ff_all_channel_layouts();
110     if (!layouts)
111         return AVERROR(ENOMEM);
112     ret = ff_set_common_channel_layouts(ctx, layouts);
113     if (ret < 0)
114         return ret;
115
116     formats = ff_make_format_list(sample_fmts);
117     if (!formats)
118         return AVERROR(ENOMEM);
119     ret = ff_set_common_formats(ctx, formats);
120     if (ret < 0)
121         return ret;
122
123     formats = ff_all_samplerates();
124     if (!formats)
125         return AVERROR(ENOMEM);
126     return ff_set_common_samplerates(ctx, formats);
127 }
128
129 static double fade_gain(int curve, int64_t index, int range)
130 {
131     double gain;
132
133     gain = av_clipd(1.0 * index / range, 0, 1.0);
134
135     switch (curve) {
136     case QSIN:
137         gain = sin(gain * M_PI / 2.0);
138         break;
139     case IQSIN:
140         gain = 0.636943 * asin(gain);
141         break;
142     case ESIN:
143         gain = 1.0 - cos(M_PI / 4.0 * (pow(2.0*gain - 1, 3) + 1));
144         break;
145     case HSIN:
146         gain = (1.0 - cos(gain * M_PI)) / 2.0;
147         break;
148     case IHSIN:
149         gain = 0.318471 * acos(1 - 2 * gain);
150         break;
151     case EXP:
152         gain = pow(0.1, (1 - gain) * 5.0);
153         break;
154     case LOG:
155         gain = av_clipd(0.0868589 * log(100000 * gain), 0, 1.0);
156         break;
157     case PAR:
158         gain = 1 - sqrt(1 - gain);
159         break;
160     case IPAR:
161         gain = (1 - (1 - gain) * (1 - gain));
162         break;
163     case QUA:
164         gain *= gain;
165         break;
166     case CUB:
167         gain = gain * gain * gain;
168         break;
169     case SQU:
170         gain = sqrt(gain);
171         break;
172     case CBR:
173         gain = cbrt(gain);
174         break;
175     case DESE:
176         gain = gain <= 0.5 ? pow(2 * gain, 1/3.) / 2: 1 - pow(2 * (1 - gain), 1/3.) / 2;
177         break;
178     case DESI:
179         gain = gain <= 0.5 ? pow(2 * gain, 3) / 2: 1 - pow(2 * (1 - gain), 3) / 2;
180         break;
181     }
182
183     return gain;
184 }
185
186 #define FADE_PLANAR(name, type)                                             \
187 static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src,   \
188                                      int nb_samples, int channels, int dir, \
189                                      int64_t start, int range, int curve)   \
190 {                                                                           \
191     int i, c;                                                               \
192                                                                             \
193     for (i = 0; i < nb_samples; i++) {                                      \
194         double gain = fade_gain(curve, start + i * dir, range);             \
195         for (c = 0; c < channels; c++) {                                    \
196             type *d = (type *)dst[c];                                       \
197             const type *s = (type *)src[c];                                 \
198                                                                             \
199             d[i] = s[i] * gain;                                             \
200         }                                                                   \
201     }                                                                       \
202 }
203
204 #define FADE(name, type)                                                    \
205 static void fade_samples_## name (uint8_t **dst, uint8_t * const *src,      \
206                                   int nb_samples, int channels, int dir,    \
207                                   int64_t start, int range, int curve)      \
208 {                                                                           \
209     type *d = (type *)dst[0];                                               \
210     const type *s = (type *)src[0];                                         \
211     int i, c, k = 0;                                                        \
212                                                                             \
213     for (i = 0; i < nb_samples; i++) {                                      \
214         double gain = fade_gain(curve, start + i * dir, range);             \
215         for (c = 0; c < channels; c++, k++)                                 \
216             d[k] = s[k] * gain;                                             \
217     }                                                                       \
218 }
219
220 FADE_PLANAR(dbl, double)
221 FADE_PLANAR(flt, float)
222 FADE_PLANAR(s16, int16_t)
223 FADE_PLANAR(s32, int32_t)
224
225 FADE(dbl, double)
226 FADE(flt, float)
227 FADE(s16, int16_t)
228 FADE(s32, int32_t)
229
230 static int config_input(AVFilterLink *inlink)
231 {
232     AVFilterContext *ctx = inlink->dst;
233     AudioFadeContext *s  = ctx->priv;
234
235     switch (inlink->format) {
236     case AV_SAMPLE_FMT_DBL:  s->fade_samples = fade_samples_dbl;  break;
237     case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break;
238     case AV_SAMPLE_FMT_FLT:  s->fade_samples = fade_samples_flt;  break;
239     case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break;
240     case AV_SAMPLE_FMT_S16:  s->fade_samples = fade_samples_s16;  break;
241     case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break;
242     case AV_SAMPLE_FMT_S32:  s->fade_samples = fade_samples_s32;  break;
243     case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break;
244     }
245
246     if (s->duration)
247         s->nb_samples = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE);
248     if (s->start_time)
249         s->start_sample = av_rescale(s->start_time, inlink->sample_rate, AV_TIME_BASE);
250
251     return 0;
252 }
253
254 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
255 {
256     AudioFadeContext *s     = inlink->dst->priv;
257     AVFilterLink *outlink   = inlink->dst->outputs[0];
258     int nb_samples          = buf->nb_samples;
259     AVFrame *out_buf;
260     int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
261
262     if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
263         ( s->type && (cur_sample + s->nb_samples < s->start_sample)))
264         return ff_filter_frame(outlink, buf);
265
266     if (av_frame_is_writable(buf)) {
267         out_buf = buf;
268     } else {
269         out_buf = ff_get_audio_buffer(inlink, nb_samples);
270         if (!out_buf)
271             return AVERROR(ENOMEM);
272         av_frame_copy_props(out_buf, buf);
273     }
274
275     if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
276         ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
277         av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
278                                av_frame_get_channels(out_buf), out_buf->format);
279     } else {
280         int64_t start;
281
282         if (!s->type)
283             start = cur_sample - s->start_sample;
284         else
285             start = s->start_sample + s->nb_samples - cur_sample;
286
287         s->fade_samples(out_buf->extended_data, buf->extended_data,
288                         nb_samples, av_frame_get_channels(buf),
289                         s->type ? -1 : 1, start,
290                         s->nb_samples, s->curve);
291     }
292
293     if (buf != out_buf)
294         av_frame_free(&buf);
295
296     return ff_filter_frame(outlink, out_buf);
297 }
298
299 static const AVFilterPad avfilter_af_afade_inputs[] = {
300     {
301         .name         = "default",
302         .type         = AVMEDIA_TYPE_AUDIO,
303         .filter_frame = filter_frame,
304         .config_props = config_input,
305     },
306     { NULL }
307 };
308
309 static const AVFilterPad avfilter_af_afade_outputs[] = {
310     {
311         .name = "default",
312         .type = AVMEDIA_TYPE_AUDIO,
313     },
314     { NULL }
315 };
316
317 AVFilter ff_af_afade = {
318     .name          = "afade",
319     .description   = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
320     .query_formats = query_formats,
321     .priv_size     = sizeof(AudioFadeContext),
322     .init          = init,
323     .inputs        = avfilter_af_afade_inputs,
324     .outputs       = avfilter_af_afade_outputs,
325     .priv_class    = &afade_class,
326     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
327 };