Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6148 | serge | 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, 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", "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 }, TRI, CBR, FLAGS, "curve" }, |
||
64 | { "c", "set fade curve type", 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) |
||
81 | { |
||
82 | AudioFadeContext *s = ctx->priv; |
||
83 | |||
84 | if (INT64_MAX - s->nb_samples < s->start_sample) |
||
85 | return AVERROR(EINVAL); |
||
86 | |||
87 | return 0; |
||
88 | } |
||
89 | |||
90 | static int query_formats(AVFilterContext *ctx) |
||
91 | { |
||
92 | AVFilterFormats *formats; |
||
93 | AVFilterChannelLayouts *layouts; |
||
94 | static const enum AVSampleFormat sample_fmts[] = { |
||
95 | AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P, |
||
96 | AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P, |
||
97 | AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP, |
||
98 | AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP, |
||
99 | AV_SAMPLE_FMT_NONE |
||
100 | }; |
||
101 | |||
102 | layouts = ff_all_channel_layouts(); |
||
103 | if (!layouts) |
||
104 | return AVERROR(ENOMEM); |
||
105 | ff_set_common_channel_layouts(ctx, layouts); |
||
106 | |||
107 | formats = ff_make_format_list(sample_fmts); |
||
108 | if (!formats) |
||
109 | return AVERROR(ENOMEM); |
||
110 | ff_set_common_formats(ctx, formats); |
||
111 | |||
112 | formats = ff_all_samplerates(); |
||
113 | if (!formats) |
||
114 | return AVERROR(ENOMEM); |
||
115 | ff_set_common_samplerates(ctx, formats); |
||
116 | |||
117 | return 0; |
||
118 | } |
||
119 | |||
120 | static double fade_gain(int curve, int64_t index, int range) |
||
121 | { |
||
122 | double gain; |
||
123 | |||
124 | gain = FFMAX(0.0, FFMIN(1.0, 1.0 * index / range)); |
||
125 | |||
126 | switch (curve) { |
||
127 | case QSIN: |
||
128 | gain = sin(gain * M_PI / 2.0); |
||
129 | break; |
||
130 | case ESIN: |
||
131 | gain = 1.0 - cos(M_PI / 4.0 * (pow(2.0*gain - 1, 3) + 1)); |
||
132 | break; |
||
133 | case HSIN: |
||
134 | gain = (1.0 - cos(gain * M_PI)) / 2.0; |
||
135 | break; |
||
136 | case LOG: |
||
137 | gain = pow(0.1, (1 - gain) * 5.0); |
||
138 | break; |
||
139 | case PAR: |
||
140 | gain = (1 - (1 - gain) * (1 - gain)); |
||
141 | break; |
||
142 | case QUA: |
||
143 | gain *= gain; |
||
144 | break; |
||
145 | case CUB: |
||
146 | gain = gain * gain * gain; |
||
147 | break; |
||
148 | case SQU: |
||
149 | gain = sqrt(gain); |
||
150 | break; |
||
151 | case CBR: |
||
152 | gain = cbrt(gain); |
||
153 | break; |
||
154 | } |
||
155 | |||
156 | return gain; |
||
157 | } |
||
158 | |||
159 | #define FADE_PLANAR(name, type) \ |
||
160 | static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \ |
||
161 | int nb_samples, int channels, int dir, \ |
||
162 | int64_t start, int range, int curve) \ |
||
163 | { \ |
||
164 | int i, c; \ |
||
165 | \ |
||
166 | for (i = 0; i < nb_samples; i++) { \ |
||
167 | double gain = fade_gain(curve, start + i * dir, range); \ |
||
168 | for (c = 0; c < channels; c++) { \ |
||
169 | type *d = (type *)dst[c]; \ |
||
170 | const type *s = (type *)src[c]; \ |
||
171 | \ |
||
172 | d[i] = s[i] * gain; \ |
||
173 | } \ |
||
174 | } \ |
||
175 | } |
||
176 | |||
177 | #define FADE(name, type) \ |
||
178 | static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \ |
||
179 | int nb_samples, int channels, int dir, \ |
||
180 | int64_t start, int range, int curve) \ |
||
181 | { \ |
||
182 | type *d = (type *)dst[0]; \ |
||
183 | const type *s = (type *)src[0]; \ |
||
184 | int i, c, k = 0; \ |
||
185 | \ |
||
186 | for (i = 0; i < nb_samples; i++) { \ |
||
187 | double gain = fade_gain(curve, start + i * dir, range); \ |
||
188 | for (c = 0; c < channels; c++, k++) \ |
||
189 | d[k] = s[k] * gain; \ |
||
190 | } \ |
||
191 | } |
||
192 | |||
193 | FADE_PLANAR(dbl, double) |
||
194 | FADE_PLANAR(flt, float) |
||
195 | FADE_PLANAR(s16, int16_t) |
||
196 | FADE_PLANAR(s32, int32_t) |
||
197 | |||
198 | FADE(dbl, double) |
||
199 | FADE(flt, float) |
||
200 | FADE(s16, int16_t) |
||
201 | FADE(s32, int32_t) |
||
202 | |||
203 | static int config_input(AVFilterLink *inlink) |
||
204 | { |
||
205 | AVFilterContext *ctx = inlink->dst; |
||
206 | AudioFadeContext *s = ctx->priv; |
||
207 | |||
208 | switch (inlink->format) { |
||
209 | case AV_SAMPLE_FMT_DBL: s->fade_samples = fade_samples_dbl; break; |
||
210 | case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break; |
||
211 | case AV_SAMPLE_FMT_FLT: s->fade_samples = fade_samples_flt; break; |
||
212 | case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break; |
||
213 | case AV_SAMPLE_FMT_S16: s->fade_samples = fade_samples_s16; break; |
||
214 | case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break; |
||
215 | case AV_SAMPLE_FMT_S32: s->fade_samples = fade_samples_s32; break; |
||
216 | case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break; |
||
217 | } |
||
218 | |||
219 | if (s->duration) |
||
220 | s->nb_samples = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE); |
||
221 | if (s->start_time) |
||
222 | s->start_sample = av_rescale(s->start_time, inlink->sample_rate, AV_TIME_BASE); |
||
223 | |||
224 | return 0; |
||
225 | } |
||
226 | |||
227 | static int filter_frame(AVFilterLink *inlink, AVFrame *buf) |
||
228 | { |
||
229 | AudioFadeContext *s = inlink->dst->priv; |
||
230 | AVFilterLink *outlink = inlink->dst->outputs[0]; |
||
231 | int nb_samples = buf->nb_samples; |
||
232 | AVFrame *out_buf; |
||
233 | int64_t cur_sample = av_rescale_q(buf->pts, (AVRational){1, outlink->sample_rate}, outlink->time_base); |
||
234 | |||
235 | if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) || |
||
236 | ( s->type && (cur_sample + s->nb_samples < s->start_sample))) |
||
237 | return ff_filter_frame(outlink, buf); |
||
238 | |||
239 | if (av_frame_is_writable(buf)) { |
||
240 | out_buf = buf; |
||
241 | } else { |
||
242 | out_buf = ff_get_audio_buffer(inlink, nb_samples); |
||
243 | if (!out_buf) |
||
244 | return AVERROR(ENOMEM); |
||
245 | av_frame_copy_props(out_buf, buf); |
||
246 | } |
||
247 | |||
248 | if ((!s->type && (cur_sample + nb_samples < s->start_sample)) || |
||
249 | ( s->type && (s->start_sample + s->nb_samples < cur_sample))) { |
||
250 | av_samples_set_silence(out_buf->extended_data, 0, nb_samples, |
||
251 | av_frame_get_channels(out_buf), out_buf->format); |
||
252 | } else { |
||
253 | int64_t start; |
||
254 | |||
255 | if (!s->type) |
||
256 | start = cur_sample - s->start_sample; |
||
257 | else |
||
258 | start = s->start_sample + s->nb_samples - cur_sample; |
||
259 | |||
260 | s->fade_samples(out_buf->extended_data, buf->extended_data, |
||
261 | nb_samples, av_frame_get_channels(buf), |
||
262 | s->type ? -1 : 1, start, |
||
263 | s->nb_samples, s->curve); |
||
264 | } |
||
265 | |||
266 | if (buf != out_buf) |
||
267 | av_frame_free(&buf); |
||
268 | |||
269 | return ff_filter_frame(outlink, out_buf); |
||
270 | } |
||
271 | |||
272 | static const AVFilterPad avfilter_af_afade_inputs[] = { |
||
273 | { |
||
274 | .name = "default", |
||
275 | .type = AVMEDIA_TYPE_AUDIO, |
||
276 | .filter_frame = filter_frame, |
||
277 | .config_props = config_input, |
||
278 | }, |
||
279 | { NULL } |
||
280 | }; |
||
281 | |||
282 | static const AVFilterPad avfilter_af_afade_outputs[] = { |
||
283 | { |
||
284 | .name = "default", |
||
285 | .type = AVMEDIA_TYPE_AUDIO, |
||
286 | }, |
||
287 | { NULL } |
||
288 | }; |
||
289 | |||
290 | AVFilter avfilter_af_afade = { |
||
291 | .name = "afade", |
||
292 | .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."), |
||
293 | .query_formats = query_formats, |
||
294 | .priv_size = sizeof(AudioFadeContext), |
||
295 | .init = init, |
||
296 | .inputs = avfilter_af_afade_inputs, |
||
297 | .outputs = avfilter_af_afade_outputs, |
||
298 | .priv_class = &afade_class, |
||
299 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, |
||
300 | };>>>>>>>>> |