History

afade filter

Paul B Mahol committed 13 years ago

Commit 6ea8a831 parent 5f61e096 files · +389 −2Browse files

Unified · Split

6 files
  1. Changelog +1 −0
  2. doc/filters.texi +77 −0
  3. libavfilter/Makefile +1 −0
  4. libavfilter/af_afade.c +307 −0
  5. libavfilter/allfilters.c +1 −0
  6. libavfilter/version.h +2 −2
Changelog+1−0
@@ -8,6 +8,7 @@ version <next>: · 7 unmodified lines
88 - Chained Ogg support
99 - Theora Midstream reconfiguration support
1010 - EVRC decoder
11+- audio fade filter
1112
1213
1314 version 1.1:
doc/filters.texi+77−0
@@ -282,6 +282,83 @@ aconvert=u8:auto · 281 unmodified lines
282282 @end example
283283 @end itemize
284284
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+
285362 @section aformat
286363
287364 Set output format constraints for the input audio. The framework will
libavfilter/Makefile+1−0
@@ -51,6 +51,7 @@ OBJS-$(CONFIG_AVFORMAT) += lavfutils.o · 50 unmodified lines
5151 OBJS-$(CONFIG_SWSCALE) += lswsutils.o
5252
5353 OBJS-$(CONFIG_ACONVERT_FILTER) += af_aconvert.o
54+OBJS-$(CONFIG_AFADE_FILTER) += af_afade.o
5455 OBJS-$(CONFIG_AFORMAT_FILTER) += af_aformat.o
5556 OBJS-$(CONFIG_AMERGE_FILTER) += af_amerge.o
5657 OBJS-$(CONFIG_AMIX_FILTER) += af_amix.o
libavfilter/af_afade.c+307−0
@@ -0,0 +1,307 @@
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+ double duration;
38+ double 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, PAR, QUA, CUB, SQU, CBR };
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", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "type" },
54+ { "out", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "type" },
55+ { "start_sample", "set expression of sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
56+ { "ss", "set expression of sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
57+ { "nb_samples", "set expression for fade duration in samples", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
58+ { "ns", "set expression for fade duration in samples", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
59+ { "start_time", "set expression of second to start fading", OFFSET(start_time), AV_OPT_TYPE_DOUBLE, {.dbl = 0. }, 0, 7*24*60*60,FLAGS },
60+ { "st", "set expression of second to start fading", OFFSET(start_time), AV_OPT_TYPE_DOUBLE, {.dbl = 0. }, 0, 7*24*60*60,FLAGS },
61+ { "duration", "set expression for fade duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl = 0. }, 0, 24*60*60, FLAGS },
62+ { "d", "set expression for fade duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl = 0. }, 0, 24*60*60, FLAGS },
63+ { "curve", "set expression for fade curve", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, TRI, CBR, FLAGS, "curve" },
64+ { "c", "set expression for fade curve", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, TRI, CBR, 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+ { "par", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 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+ {NULL},
76+};
77+
78+AVFILTER_DEFINE_CLASS(afade);
79+
80+static av_cold int init(AVFilterContext *ctx, const char *args)
81+{
82+ AudioFadeContext *afade = ctx->priv;
83+ int ret;
84+
85+ afade->class = &afade_class;
86+ av_opt_set_defaults(afade);
87+
88+ if ((ret = av_set_options_string(afade, args, "=", ":")) < 0)
89+ return ret;
90+
91+ if (INT64_MAX - afade->nb_samples < afade->start_sample)
92+ return AVERROR(EINVAL);
93+
94+ return 0;
95+}
96+
97+static int query_formats(AVFilterContext *ctx)
98+{
99+ AVFilterFormats *formats;
100+ AVFilterChannelLayouts *layouts;
101+ static const enum AVSampleFormat sample_fmts[] = {
102+ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
103+ AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
104+ AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
105+ AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
106+ AV_SAMPLE_FMT_NONE
107+ };
108+
109+ layouts = ff_all_channel_layouts();
110+ if (!layouts)
111+ return AVERROR(ENOMEM);
112+ ff_set_common_channel_layouts(ctx, layouts);
113+
114+ formats = ff_make_format_list(sample_fmts);
115+ if (!formats)
116+ return AVERROR(ENOMEM);
117+ ff_set_common_formats(ctx, formats);
118+
119+ formats = ff_all_samplerates();
120+ if (!formats)
121+ return AVERROR(ENOMEM);
122+ ff_set_common_samplerates(ctx, formats);
123+
124+ return 0;
125+}
126+
127+static double fade_gain(int curve, int64_t index, int range)
128+{
129+ double gain;
130+
131+ gain = FFMAX(0.0, FFMIN(1.0, 1.0 * index / range));
132+
133+ switch (curve) {
134+ case QSIN:
135+ gain = sin(gain * M_PI / 2.0);
136+ break;
137+ case ESIN:
138+ gain = 1.0 - cos(M_PI / 4.0 * (pow(2.0*gain - 1, 3) + 1));
139+ break;
140+ case HSIN:
141+ gain = (1.0 - cos(gain * M_PI)) / 2.0;
142+ break;
143+ case LOG:
144+ gain = pow(0.1, (1 - gain) * 5.0);
145+ break;
146+ case PAR:
147+ gain = (1 - (1 - gain) * (1 - gain));
148+ break;
149+ case QUA:
150+ gain *= gain;
151+ break;
152+ case CUB:
153+ gain = gain * gain * gain;
154+ break;
155+ case SQU:
156+ gain = sqrt(gain);
157+ break;
158+ case CBR:
159+ gain = cbrt(gain);
160+ break;
161+ }
162+
163+ return gain;
164+}
165+
166+#define FADE_PLANAR(name, type) \
167+static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
168+ int nb_samples, int channels, int dir, \
169+ int64_t start, int range, int curve) \
170+{ \
171+ int i, c; \
172+ \
173+ for (i = 0; i < nb_samples; i++) { \
174+ double gain = fade_gain(curve, start + i * dir, range); \
175+ for (c = 0; c < channels; c++) { \
176+ type *d = (type *)dst[c]; \
177+ const type *s = (type *)src[c]; \
178+ \
179+ d[i] = s[i] * gain; \
180+ } \
181+ } \
182+}
183+
184+#define FADE(name, type) \
185+static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
186+ int nb_samples, int channels, int dir, \
187+ int64_t start, int range, int curve) \
188+{ \
189+ type *d = (type *)dst[0]; \
190+ const type *s = (type *)src[0]; \
191+ int i, c, k = 0; \
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++, k++) \
196+ d[k] = s[k] * gain; \
197+ } \
198+}
199+
200+FADE_PLANAR(dbl, double)
201+FADE_PLANAR(flt, float)
202+FADE_PLANAR(s16, int16_t)
203+FADE_PLANAR(s32, int32_t)
204+
205+FADE(dbl, double)
206+FADE(flt, float)
207+FADE(s16, int16_t)
208+FADE(s32, int32_t)
209+
210+static int config_output(AVFilterLink *outlink)
211+{
212+ AVFilterContext *ctx = outlink->src;
213+ AudioFadeContext *afade = ctx->priv;
214+ AVFilterLink *inlink = ctx->inputs[0];
215+
216+ switch (inlink->format) {
217+ case AV_SAMPLE_FMT_DBL: afade->fade_samples = fade_samples_dbl; break;
218+ case AV_SAMPLE_FMT_DBLP: afade->fade_samples = fade_samples_dblp; break;
219+ case AV_SAMPLE_FMT_FLT: afade->fade_samples = fade_samples_flt; break;
220+ case AV_SAMPLE_FMT_FLTP: afade->fade_samples = fade_samples_fltp; break;
221+ case AV_SAMPLE_FMT_S16: afade->fade_samples = fade_samples_s16; break;
222+ case AV_SAMPLE_FMT_S16P: afade->fade_samples = fade_samples_s16p; break;
223+ case AV_SAMPLE_FMT_S32: afade->fade_samples = fade_samples_s32; break;
224+ case AV_SAMPLE_FMT_S32P: afade->fade_samples = fade_samples_s32p; break;
225+ }
226+
227+ if (afade->duration)
228+ afade->nb_samples = afade->duration * inlink->sample_rate;
229+ if (afade->start_time)
230+ afade->start_sample = afade->start_time * inlink->sample_rate;
231+
232+ return 0;
233+}
234+
235+static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
236+{
237+ AudioFadeContext *afade = inlink->dst->priv;
238+ AVFilterLink *outlink = inlink->dst->outputs[0];
239+ int nb_samples = buf->audio->nb_samples;
240+ AVFilterBufferRef *out_buf;
241+ int64_t cur_sample = av_rescale_q(buf->pts, (AVRational){1, outlink->sample_rate}, outlink->time_base);
242+
243+ if ((!afade->type && (afade->start_sample + afade->nb_samples < cur_sample)) ||
244+ ( afade->type && (cur_sample + afade->nb_samples < afade->start_sample)))
245+ return ff_filter_frame(outlink, buf);
246+
247+ if (buf->perms & AV_PERM_WRITE) {
248+ out_buf = buf;
249+ } else {
250+ out_buf = ff_get_audio_buffer(inlink, AV_PERM_WRITE, nb_samples);
251+ if (!out_buf)
252+ return AVERROR(ENOMEM);
253+ out_buf->pts = buf->pts;
254+ }
255+
256+ if ((!afade->type && (cur_sample + nb_samples < afade->start_sample)) ||
257+ ( afade->type && (afade->start_sample + afade->nb_samples < cur_sample))) {
258+ av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
259+ out_buf->audio->channels, out_buf->format);
260+ } else {
261+ int64_t start;
262+
263+ if (!afade->type)
264+ start = cur_sample - afade->start_sample;
265+ else
266+ start = afade->start_sample + afade->nb_samples - cur_sample;
267+
268+ afade->fade_samples(out_buf->extended_data, buf->extended_data,
269+ nb_samples, buf->audio->channels,
270+ afade->type ? -1 : 1, start,
271+ afade->nb_samples, afade->curve);
272+ }
273+
274+ if (buf != out_buf)
275+ avfilter_unref_buffer(buf);
276+
277+ return ff_filter_frame(outlink, out_buf);
278+}
279+
280+static const AVFilterPad avfilter_af_afade_inputs[] = {
281+ {
282+ .name = "default",
283+ .type = AVMEDIA_TYPE_AUDIO,
284+ .filter_frame = filter_frame,
285+ },
286+ { NULL }
287+};
288+
289+static const AVFilterPad avfilter_af_afade_outputs[] = {
290+ {
291+ .name = "default",
292+ .type = AVMEDIA_TYPE_AUDIO,
293+ .config_props = config_output,
294+ },
295+ { NULL }
296+};
297+
298+AVFilter avfilter_af_afade = {
299+ .name = "afade",
300+ .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
301+ .query_formats = query_formats,
302+ .priv_size = sizeof(AudioFadeContext),
303+ .init = init,
304+ .inputs = avfilter_af_afade_inputs,
305+ .outputs = avfilter_af_afade_outputs,
306+ .priv_class = &afade_class,
307+};
libavfilter/allfilters.c+1−0
@@ -45,6 +45,7 @@ void avfilter_register_all(void) · 44 unmodified lines
4545 initialized = 1;
4646
4747 REGISTER_FILTER(ACONVERT, aconvert, af);
48+ REGISTER_FILTER(AFADE, afade, af);
4849 REGISTER_FILTER(AFORMAT, aformat, af);
4950 REGISTER_FILTER(AMERGE, amerge, af);
5051 REGISTER_FILTER(AMIX, amix, af);
libavfilter/version.h+2−2
@@ -29,8 +29,8 @@ · 28 unmodified lines
2929 #include "libavutil/avutil.h"
3030
3131 #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
3434
3535 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
3636 LIBAVFILTER_VERSION_MINOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t