Browse code

afade filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2013/01/18 06:53:27
Showing 6 changed files
... ...
@@ -8,6 +8,7 @@ version <next>:
8 8
 - Chained Ogg support
9 9
 - Theora Midstream reconfiguration support
10 10
 - EVRC decoder
11
+- audio fade filter
11 12
 
12 13
 
13 14
 version 1.1:
... ...
@@ -282,6 +282,83 @@ aconvert=u8:auto
282 282
 @end example
283 283
 @end itemize
284 284
 
285
+@section afade
286
+
287
+Apply fade-in/out effect to input audio.
288
+
289
+The filter accepts parameters as a list of @var{key}=@var{value}
290
+pairs, separated by ":".
291
+
292
+A description of the accepted parameters follows.
293
+
294
+@table @option
295
+@item type, t
296
+Specify the effect type, can be either @code{in} for fade-in, or
297
+@code{out} for a fade-out effect. Default is @code{in}.
298
+
299
+@item start_sample, ss
300
+Specify the number of the start sample for starting to apply the fade
301
+effect. Default is 0.
302
+
303
+@item nb_samples, ns
304
+Specify the number of samples for which the fade effect has to last. At
305
+the end of the fade-in effect the output audio will have the same
306
+volume as the input audio, at the end of the fade-out transition
307
+the output audio will be silence. Default is 44100.
308
+
309
+@item start_time, st
310
+Specify time in seconds for starting to apply the fade
311
+effect. Default is 0.
312
+If set this option is used instead of @var{start_sample} one.
313
+
314
+@item duration, d
315
+Specify the number of seconds for which the fade effect has to last. At
316
+the end of the fade-in effect the output audio will have the same
317
+volume as the input audio, at the end of the fade-out transition
318
+the output audio will be silence. Default is 0.
319
+If set this option is used instead of @var{nb_samples} one.
320
+
321
+@item curve
322
+Set cuve for fade transition.
323
+@table @option
324
+@item @var{triangular, linear slope (default)}
325
+@code{tri}
326
+@item @var{quarter of sine wave}
327
+@code{qsin}
328
+@item @var{half of sine wave}
329
+@code{esin}
330
+@item @var{exponential sine wave}
331
+@code{hsin}
332
+@item @var{logarithmic}
333
+@code{log}
334
+@item @var{inverted parabola}
335
+@code{par}
336
+@item @var{quadratic}
337
+@code{qua}
338
+@item @var{cubic}
339
+@code{cub}
340
+@item @var{square root}
341
+@code{squ}
342
+@item @var{cubic root}
343
+@code{cbr}
344
+@end table
345
+@end table
346
+
347
+@subsection Examples
348
+@itemize
349
+@item
350
+Fade in first 15 seconds of audio:
351
+@example
352
+afade=t=in:ss=0:d=15
353
+@end example
354
+
355
+@item
356
+Fade out last 25 seconds of a 900 seconds audio:
357
+@example
358
+afade=t=out:ss=875:d=25
359
+@end example
360
+@end itemize
361
+
285 362
 @section aformat
286 363
 
287 364
 Set output format constraints for the input audio. The framework will
... ...
@@ -51,6 +51,7 @@ OBJS-$(CONFIG_AVFORMAT)                      += lavfutils.o
51 51
 OBJS-$(CONFIG_SWSCALE)                       += lswsutils.o
52 52
 
53 53
 OBJS-$(CONFIG_ACONVERT_FILTER)               += af_aconvert.o
54
+OBJS-$(CONFIG_AFADE_FILTER)                  += af_afade.o
54 55
 OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
55 56
 OBJS-$(CONFIG_AMERGE_FILTER)                 += af_amerge.o
56 57
 OBJS-$(CONFIG_AMIX_FILTER)                   += af_amix.o
57 58
new file mode 100644
... ...
@@ -0,0 +1,307 @@
0
+/*
1
+ * Copyright (c) 2013 Paul B Mahol
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * fade audio filter
23
+ */
24
+
25
+#include "libavutil/opt.h"
26
+#include "audio.h"
27
+#include "avfilter.h"
28
+#include "internal.h"
29
+
30
+typedef struct {
31
+    const AVClass *class;
32
+    int type;
33
+    int curve;
34
+    int nb_samples;
35
+    int64_t start_sample;
36
+    double duration;
37
+    double start_time;
38
+
39
+    void (*fade_samples)(uint8_t **dst, uint8_t * const *src,
40
+                         int nb_samples, int channels, int direction,
41
+                         int64_t start, int range, int curve);
42
+} AudioFadeContext;
43
+
44
+enum CurveType { TRI, QSIN, ESIN, HSIN, LOG, PAR, QUA, CUB, SQU, CBR };
45
+
46
+#define OFFSET(x) offsetof(AudioFadeContext, x)
47
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
48
+
49
+static const AVOption afade_options[] = {
50
+    { "type",         "set the fade direction",                      OFFSET(type),         AV_OPT_TYPE_INT,    {.i64 = 0    }, 0, 1, FLAGS, "type" },
51
+    { "t",            "set the fade direction",                      OFFSET(type),         AV_OPT_TYPE_INT,    {.i64 = 0    }, 0, 1, FLAGS, "type" },
52
+    { "in",           NULL,                                          0,                    AV_OPT_TYPE_CONST,  {.i64 = 0    }, 0, 0, FLAGS, "type" },
53
+    { "out",          NULL,                                          0,                    AV_OPT_TYPE_CONST,  {.i64 = 1    }, 0, 0, FLAGS, "type" },
54
+    { "start_sample", "set expression of sample to start fading",    OFFSET(start_sample), AV_OPT_TYPE_INT64,  {.i64 = 0    }, 0, INT64_MAX, FLAGS },
55
+    { "ss",           "set expression of sample to start fading",    OFFSET(start_sample), AV_OPT_TYPE_INT64,  {.i64 = 0    }, 0, INT64_MAX, FLAGS },
56
+    { "nb_samples",   "set expression for fade duration in samples", OFFSET(nb_samples),   AV_OPT_TYPE_INT,    {.i64 = 44100}, 1, INT32_MAX, FLAGS },
57
+    { "ns",           "set expression for fade duration in samples", OFFSET(nb_samples),   AV_OPT_TYPE_INT,    {.i64 = 44100}, 1, INT32_MAX, FLAGS },
58
+    { "start_time",   "set expression of second to start fading",    OFFSET(start_time),   AV_OPT_TYPE_DOUBLE, {.dbl = 0.   }, 0, 7*24*60*60,FLAGS },
59
+    { "st",           "set expression of second to start fading",    OFFSET(start_time),   AV_OPT_TYPE_DOUBLE, {.dbl = 0.   }, 0, 7*24*60*60,FLAGS },
60
+    { "duration",     "set expression for fade duration in seconds", OFFSET(duration),     AV_OPT_TYPE_DOUBLE, {.dbl = 0.   }, 0, 24*60*60,  FLAGS },
61
+    { "d",            "set expression for fade duration in seconds", OFFSET(duration),     AV_OPT_TYPE_DOUBLE, {.dbl = 0.   }, 0, 24*60*60,  FLAGS },
62
+    { "curve",        "set expression for fade curve",               OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, TRI, CBR, FLAGS, "curve" },
63
+    { "c",            "set expression for fade curve",               OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, TRI, CBR, FLAGS, "curve" },
64
+    { "tri",          "linear slope",                                0,                    AV_OPT_TYPE_CONST,  {.i64 = TRI  }, 0, 0, FLAGS, "curve" },
65
+    { "qsin",         "quarter of sine wave",                        0,                    AV_OPT_TYPE_CONST,  {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
66
+    { "esin",         "exponential sine wave",                       0,                    AV_OPT_TYPE_CONST,  {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
67
+    { "hsin",         "half of sine wave",                           0,                    AV_OPT_TYPE_CONST,  {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
68
+    { "log",          "logarithmic",                                 0,                    AV_OPT_TYPE_CONST,  {.i64 = LOG  }, 0, 0, FLAGS, "curve" },
69
+    { "par",          "inverted parabola",                           0,                    AV_OPT_TYPE_CONST,  {.i64 = PAR  }, 0, 0, FLAGS, "curve" },
70
+    { "qua",          "quadratic",                                   0,                    AV_OPT_TYPE_CONST,  {.i64 = QUA  }, 0, 0, FLAGS, "curve" },
71
+    { "cub",          "cubic",                                       0,                    AV_OPT_TYPE_CONST,  {.i64 = CUB  }, 0, 0, FLAGS, "curve" },
72
+    { "squ",          "square root",                                 0,                    AV_OPT_TYPE_CONST,  {.i64 = SQU  }, 0, 0, FLAGS, "curve" },
73
+    { "cbr",          "cubic root",                                  0,                    AV_OPT_TYPE_CONST,  {.i64 = CBR  }, 0, 0, FLAGS, "curve" },
74
+    {NULL},
75
+};
76
+
77
+AVFILTER_DEFINE_CLASS(afade);
78
+
79
+static av_cold int init(AVFilterContext *ctx, const char *args)
80
+{
81
+    AudioFadeContext *afade = ctx->priv;
82
+    int ret;
83
+
84
+    afade->class = &afade_class;
85
+    av_opt_set_defaults(afade);
86
+
87
+    if ((ret = av_set_options_string(afade, args, "=", ":")) < 0)
88
+        return ret;
89
+
90
+    if (INT64_MAX - afade->nb_samples < afade->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
+
108
+    layouts = ff_all_channel_layouts();
109
+    if (!layouts)
110
+        return AVERROR(ENOMEM);
111
+    ff_set_common_channel_layouts(ctx, layouts);
112
+
113
+    formats = ff_make_format_list(sample_fmts);
114
+    if (!formats)
115
+        return AVERROR(ENOMEM);
116
+    ff_set_common_formats(ctx, formats);
117
+
118
+    formats = ff_all_samplerates();
119
+    if (!formats)
120
+        return AVERROR(ENOMEM);
121
+    ff_set_common_samplerates(ctx, formats);
122
+
123
+    return 0;
124
+}
125
+
126
+static double fade_gain(int curve, int64_t index, int range)
127
+{
128
+    double gain;
129
+
130
+    gain = FFMAX(0.0, FFMIN(1.0, 1.0 * index / range));
131
+
132
+    switch (curve) {
133
+    case QSIN:
134
+        gain = sin(gain * M_PI / 2.0);
135
+        break;
136
+    case ESIN:
137
+        gain = 1.0 - cos(M_PI / 4.0 * (pow(2.0*gain - 1, 3) + 1));
138
+        break;
139
+    case HSIN:
140
+        gain = (1.0 - cos(gain * M_PI)) / 2.0;
141
+        break;
142
+    case LOG:
143
+        gain = pow(0.1, (1 - gain) * 5.0);
144
+        break;
145
+    case PAR:
146
+        gain = (1 - (1 - gain) * (1 - gain));
147
+        break;
148
+    case QUA:
149
+        gain *= gain;
150
+        break;
151
+    case CUB:
152
+        gain = gain * gain * gain;
153
+        break;
154
+    case SQU:
155
+        gain = sqrt(gain);
156
+        break;
157
+    case CBR:
158
+        gain = cbrt(gain);
159
+        break;
160
+    }
161
+
162
+    return gain;
163
+}
164
+
165
+#define FADE_PLANAR(name, type)                                             \
166
+static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src,   \
167
+                                     int nb_samples, int channels, int dir, \
168
+                                     int64_t start, int range, int curve)   \
169
+{                                                                           \
170
+    int i, c;                                                               \
171
+                                                                            \
172
+    for (i = 0; i < nb_samples; i++) {                                      \
173
+        double gain = fade_gain(curve, start + i * dir, range);             \
174
+        for (c = 0; c < channels; c++) {                                    \
175
+            type *d = (type *)dst[c];                                       \
176
+            const type *s = (type *)src[c];                                 \
177
+                                                                            \
178
+            d[i] = s[i] * gain;                                             \
179
+        }                                                                   \
180
+    }                                                                       \
181
+}
182
+
183
+#define FADE(name, type)                                                    \
184
+static void fade_samples_## name (uint8_t **dst, uint8_t * const *src,      \
185
+                                  int nb_samples, int channels, int dir,    \
186
+                                  int64_t start, int range, int curve)      \
187
+{                                                                           \
188
+    type *d = (type *)dst[0];                                               \
189
+    const type *s = (type *)src[0];                                         \
190
+    int i, c, k = 0;                                                        \
191
+                                                                            \
192
+    for (i = 0; i < nb_samples; i++) {                                      \
193
+        double gain = fade_gain(curve, start + i * dir, range);             \
194
+        for (c = 0; c < channels; c++, k++)                                 \
195
+            d[k] = s[k] * gain;                                             \
196
+    }                                                                       \
197
+}
198
+
199
+FADE_PLANAR(dbl, double)
200
+FADE_PLANAR(flt, float)
201
+FADE_PLANAR(s16, int16_t)
202
+FADE_PLANAR(s32, int32_t)
203
+
204
+FADE(dbl, double)
205
+FADE(flt, float)
206
+FADE(s16, int16_t)
207
+FADE(s32, int32_t)
208
+
209
+static int config_output(AVFilterLink *outlink)
210
+{
211
+    AVFilterContext *ctx    = outlink->src;
212
+    AudioFadeContext *afade = ctx->priv;
213
+    AVFilterLink *inlink    = ctx->inputs[0];
214
+
215
+    switch (inlink->format) {
216
+    case AV_SAMPLE_FMT_DBL:  afade->fade_samples = fade_samples_dbl;  break;
217
+    case AV_SAMPLE_FMT_DBLP: afade->fade_samples = fade_samples_dblp; break;
218
+    case AV_SAMPLE_FMT_FLT:  afade->fade_samples = fade_samples_flt;  break;
219
+    case AV_SAMPLE_FMT_FLTP: afade->fade_samples = fade_samples_fltp; break;
220
+    case AV_SAMPLE_FMT_S16:  afade->fade_samples = fade_samples_s16;  break;
221
+    case AV_SAMPLE_FMT_S16P: afade->fade_samples = fade_samples_s16p; break;
222
+    case AV_SAMPLE_FMT_S32:  afade->fade_samples = fade_samples_s32;  break;
223
+    case AV_SAMPLE_FMT_S32P: afade->fade_samples = fade_samples_s32p; break;
224
+    }
225
+
226
+    if (afade->duration)
227
+        afade->nb_samples = afade->duration * inlink->sample_rate;
228
+    if (afade->start_time)
229
+        afade->start_sample = afade->start_time * inlink->sample_rate;
230
+
231
+    return 0;
232
+}
233
+
234
+static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
235
+{
236
+    AudioFadeContext *afade = inlink->dst->priv;
237
+    AVFilterLink *outlink   = inlink->dst->outputs[0];
238
+    int nb_samples          = buf->audio->nb_samples;
239
+    AVFilterBufferRef *out_buf;
240
+    int64_t cur_sample = av_rescale_q(buf->pts, (AVRational){1, outlink->sample_rate}, outlink->time_base);
241
+
242
+    if ((!afade->type && (afade->start_sample + afade->nb_samples < cur_sample)) ||
243
+        ( afade->type && (cur_sample + afade->nb_samples < afade->start_sample)))
244
+        return ff_filter_frame(outlink, buf);
245
+
246
+    if (buf->perms & AV_PERM_WRITE) {
247
+        out_buf = buf;
248
+    } else {
249
+        out_buf = ff_get_audio_buffer(inlink, AV_PERM_WRITE, nb_samples);
250
+        if (!out_buf)
251
+            return AVERROR(ENOMEM);
252
+        out_buf->pts = buf->pts;
253
+    }
254
+
255
+    if ((!afade->type && (cur_sample + nb_samples < afade->start_sample)) ||
256
+        ( afade->type && (afade->start_sample + afade->nb_samples < cur_sample))) {
257
+        av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
258
+                               out_buf->audio->channels, out_buf->format);
259
+    } else {
260
+        int64_t start;
261
+
262
+        if (!afade->type)
263
+            start = cur_sample - afade->start_sample;
264
+        else
265
+            start = afade->start_sample + afade->nb_samples - cur_sample;
266
+
267
+        afade->fade_samples(out_buf->extended_data, buf->extended_data,
268
+                            nb_samples, buf->audio->channels,
269
+                            afade->type ? -1 : 1, start,
270
+                            afade->nb_samples, afade->curve);
271
+    }
272
+
273
+    if (buf != out_buf)
274
+        avfilter_unref_buffer(buf);
275
+
276
+    return ff_filter_frame(outlink, out_buf);
277
+}
278
+
279
+static const AVFilterPad avfilter_af_afade_inputs[] = {
280
+    {
281
+        .name         = "default",
282
+        .type         = AVMEDIA_TYPE_AUDIO,
283
+        .filter_frame = filter_frame,
284
+    },
285
+    { NULL }
286
+};
287
+
288
+static const AVFilterPad avfilter_af_afade_outputs[] = {
289
+    {
290
+        .name         = "default",
291
+        .type         = AVMEDIA_TYPE_AUDIO,
292
+        .config_props = config_output,
293
+    },
294
+    { NULL }
295
+};
296
+
297
+AVFilter avfilter_af_afade = {
298
+    .name          = "afade",
299
+    .description   = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
300
+    .query_formats = query_formats,
301
+    .priv_size     = sizeof(AudioFadeContext),
302
+    .init          = init,
303
+    .inputs        = avfilter_af_afade_inputs,
304
+    .outputs       = avfilter_af_afade_outputs,
305
+    .priv_class    = &afade_class,
306
+};
... ...
@@ -45,6 +45,7 @@ void avfilter_register_all(void)
45 45
     initialized = 1;
46 46
 
47 47
     REGISTER_FILTER(ACONVERT,       aconvert,       af);
48
+    REGISTER_FILTER(AFADE,          afade,          af);
48 49
     REGISTER_FILTER(AFORMAT,        aformat,        af);
49 50
     REGISTER_FILTER(AMERGE,         amerge,         af);
50 51
     REGISTER_FILTER(AMIX,           amix,           af);
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  32
33
-#define LIBAVFILTER_VERSION_MICRO 101
32
+#define LIBAVFILTER_VERSION_MINOR  33
33
+#define LIBAVFILTER_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \