FFmpeg
af_afade.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015 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 "filters.h"
30 #include "internal.h"
31 
32 typedef struct AudioFadeContext {
33  const AVClass *class;
34  int type;
35  int curve, curve2;
36  int64_t nb_samples;
37  int64_t start_sample;
38  int64_t duration;
39  int64_t start_time;
40  int overlap;
41  int cf0_eof;
43  int64_t pts;
44 
45  void (*fade_samples)(uint8_t **dst, uint8_t * const *src,
46  int nb_samples, int channels, int direction,
47  int64_t start, int64_t range, int curve);
48  void (*crossfade_samples)(uint8_t **dst, uint8_t * const *cf0,
49  uint8_t * const *cf1,
50  int nb_samples, int channels,
51  int curve0, int curve1);
53 
55 
56 #define OFFSET(x) offsetof(AudioFadeContext, x)
57 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58 
60 {
63  static const enum AVSampleFormat sample_fmts[] = {
69  };
70  int ret;
71 
72  layouts = ff_all_channel_counts();
73  if (!layouts)
74  return AVERROR(ENOMEM);
75  ret = ff_set_common_channel_layouts(ctx, layouts);
76  if (ret < 0)
77  return ret;
78 
79  formats = ff_make_format_list(sample_fmts);
80  if (!formats)
81  return AVERROR(ENOMEM);
82  ret = ff_set_common_formats(ctx, formats);
83  if (ret < 0)
84  return ret;
85 
86  formats = ff_all_samplerates();
87  if (!formats)
88  return AVERROR(ENOMEM);
89  return ff_set_common_samplerates(ctx, formats);
90 }
91 
92 static double fade_gain(int curve, int64_t index, int64_t range)
93 {
94 #define CUBE(a) ((a)*(a)*(a))
95  double gain;
96 
97  gain = av_clipd(1.0 * index / range, 0, 1.0);
98 
99  switch (curve) {
100  case QSIN:
101  gain = sin(gain * M_PI / 2.0);
102  break;
103  case IQSIN:
104  /* 0.6... = 2 / M_PI */
105  gain = 0.6366197723675814 * asin(gain);
106  break;
107  case ESIN:
108  gain = 1.0 - cos(M_PI / 4.0 * (CUBE(2.0*gain - 1) + 1));
109  break;
110  case HSIN:
111  gain = (1.0 - cos(gain * M_PI)) / 2.0;
112  break;
113  case IHSIN:
114  /* 0.3... = 1 / M_PI */
115  gain = 0.3183098861837907 * acos(1 - 2 * gain);
116  break;
117  case EXP:
118  /* -11.5... = 5*ln(0.1) */
119  gain = exp(-11.512925464970227 * (1 - gain));
120  break;
121  case LOG:
122  gain = av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
123  break;
124  case PAR:
125  gain = 1 - sqrt(1 - gain);
126  break;
127  case IPAR:
128  gain = (1 - (1 - gain) * (1 - gain));
129  break;
130  case QUA:
131  gain *= gain;
132  break;
133  case CUB:
134  gain = CUBE(gain);
135  break;
136  case SQU:
137  gain = sqrt(gain);
138  break;
139  case CBR:
140  gain = cbrt(gain);
141  break;
142  case DESE:
143  gain = gain <= 0.5 ? cbrt(2 * gain) / 2: 1 - cbrt(2 * (1 - gain)) / 2;
144  break;
145  case DESI:
146  gain = gain <= 0.5 ? CUBE(2 * gain) / 2: 1 - CUBE(2 * (1 - gain)) / 2;
147  break;
148  case LOSI: {
149  const double a = 1. / (1. - 0.787) - 1;
150  double A = 1. / (1.0 + exp(0 -((gain-0.5) * a * 2.0)));
151  double B = 1. / (1.0 + exp(a));
152  double C = 1. / (1.0 + exp(0-a));
153  gain = (A - B) / (C - B);
154  }
155  break;
156  case SINC:
157  gain = gain >= 1.0 ? 1.0 : sin(M_PI * (1.0 - gain)) / (M_PI * (1.0 - gain));
158  break;
159  case ISINC:
160  gain = gain <= 0.0 ? 0.0 : 1.0 - sin(M_PI * gain) / (M_PI * gain);
161  break;
162  case NONE:
163  gain = 1.0;
164  break;
165  }
166 
167  return gain;
168 }
169 
170 #define FADE_PLANAR(name, type) \
171 static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
172  int nb_samples, int channels, int dir, \
173  int64_t start, int64_t range, int curve) \
174 { \
175  int i, c; \
176  \
177  for (i = 0; i < nb_samples; i++) { \
178  double gain = fade_gain(curve, start + i * dir, range); \
179  for (c = 0; c < channels; c++) { \
180  type *d = (type *)dst[c]; \
181  const type *s = (type *)src[c]; \
182  \
183  d[i] = s[i] * gain; \
184  } \
185  } \
186 }
187 
188 #define FADE(name, type) \
189 static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
190  int nb_samples, int channels, int dir, \
191  int64_t start, int64_t range, int curve) \
192 { \
193  type *d = (type *)dst[0]; \
194  const type *s = (type *)src[0]; \
195  int i, c, k = 0; \
196  \
197  for (i = 0; i < nb_samples; i++) { \
198  double gain = fade_gain(curve, start + i * dir, range); \
199  for (c = 0; c < channels; c++, k++) \
200  d[k] = s[k] * gain; \
201  } \
202 }
203 
204 FADE_PLANAR(dbl, double)
205 FADE_PLANAR(flt, float)
206 FADE_PLANAR(s16, int16_t)
207 FADE_PLANAR(s32, int32_t)
208 
209 FADE(dbl, double)
210 FADE(flt, float)
211 FADE(s16, int16_t)
212 FADE(s32, int32_t)
213 
214 static int config_output(AVFilterLink *outlink)
215 {
216  AVFilterContext *ctx = outlink->src;
217  AudioFadeContext *s = ctx->priv;
218 
219  switch (outlink->format) {
220  case AV_SAMPLE_FMT_DBL: s->fade_samples = fade_samples_dbl; break;
221  case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break;
222  case AV_SAMPLE_FMT_FLT: s->fade_samples = fade_samples_flt; break;
223  case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break;
224  case AV_SAMPLE_FMT_S16: s->fade_samples = fade_samples_s16; break;
225  case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break;
226  case AV_SAMPLE_FMT_S32: s->fade_samples = fade_samples_s32; break;
227  case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break;
228  }
229 
230  if (s->duration)
231  s->nb_samples = av_rescale(s->duration, outlink->sample_rate, AV_TIME_BASE);
232  if (s->start_time)
233  s->start_sample = av_rescale(s->start_time, outlink->sample_rate, AV_TIME_BASE);
234 
235  return 0;
236 }
237 
238 #if CONFIG_AFADE_FILTER
239 
240 static const AVOption afade_options[] = {
241  { "type", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
242  { "t", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
243  { "in", "fade-in", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "type" },
244  { "out", "fade-out", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "type" },
245  { "start_sample", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
246  { "ss", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
247  { "nb_samples", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT64_MAX, FLAGS },
248  { "ns", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT64_MAX, FLAGS },
249  { "start_time", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
250  { "st", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
251  { "duration", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
252  { "d", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
253  { "curve", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
254  { "c", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
255  { "nofade", "no fade; keep audio as-is", 0, AV_OPT_TYPE_CONST, {.i64 = NONE }, 0, 0, FLAGS, "curve" },
256  { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
257  { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
258  { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
259  { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
260  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
261  { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
262  { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
263  { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
264  { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
265  { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
266  { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
267  { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve" },
268  { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
269  { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
270  { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve" },
271  { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve" },
272  { "losi", "logistic sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = LOSI }, 0, 0, FLAGS, "curve" },
273  { "sinc", "sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = SINC }, 0, 0, FLAGS, "curve" },
274  { "isinc", "inverted sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = ISINC}, 0, 0, FLAGS, "curve" },
275  { NULL }
276 };
277 
278 AVFILTER_DEFINE_CLASS(afade);
279 
280 static av_cold int init(AVFilterContext *ctx)
281 {
282  AudioFadeContext *s = ctx->priv;
283 
284  if (INT64_MAX - s->nb_samples < s->start_sample)
285  return AVERROR(EINVAL);
286 
287  return 0;
288 }
289 
290 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
291 {
292  AudioFadeContext *s = inlink->dst->priv;
293  AVFilterLink *outlink = inlink->dst->outputs[0];
294  int nb_samples = buf->nb_samples;
295  AVFrame *out_buf;
296  int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
297 
298  if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
299  ( s->type && (cur_sample + nb_samples < s->start_sample)))
300  return ff_filter_frame(outlink, buf);
301 
302  if (av_frame_is_writable(buf)) {
303  out_buf = buf;
304  } else {
305  out_buf = ff_get_audio_buffer(outlink, nb_samples);
306  if (!out_buf)
307  return AVERROR(ENOMEM);
308  av_frame_copy_props(out_buf, buf);
309  }
310 
311  if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
312  ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
313  av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
314  out_buf->channels, out_buf->format);
315  } else {
316  int64_t start;
317 
318  if (!s->type)
319  start = cur_sample - s->start_sample;
320  else
321  start = s->start_sample + s->nb_samples - cur_sample;
322 
323  s->fade_samples(out_buf->extended_data, buf->extended_data,
324  nb_samples, buf->channels,
325  s->type ? -1 : 1, start,
326  s->nb_samples, s->curve);
327  }
328 
329  if (buf != out_buf)
330  av_frame_free(&buf);
331 
332  return ff_filter_frame(outlink, out_buf);
333 }
334 
335 static const AVFilterPad avfilter_af_afade_inputs[] = {
336  {
337  .name = "default",
338  .type = AVMEDIA_TYPE_AUDIO,
339  .filter_frame = filter_frame,
340  },
341  { NULL }
342 };
343 
344 static const AVFilterPad avfilter_af_afade_outputs[] = {
345  {
346  .name = "default",
347  .type = AVMEDIA_TYPE_AUDIO,
348  .config_props = config_output,
349  },
350  { NULL }
351 };
352 
354  .name = "afade",
355  .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
356  .query_formats = query_formats,
357  .priv_size = sizeof(AudioFadeContext),
358  .init = init,
359  .inputs = avfilter_af_afade_inputs,
360  .outputs = avfilter_af_afade_outputs,
361  .priv_class = &afade_class,
363 };
364 
365 #endif /* CONFIG_AFADE_FILTER */
366 
367 #if CONFIG_ACROSSFADE_FILTER
368 
369 static const AVOption acrossfade_options[] = {
370  { "nb_samples", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
371  { "ns", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
372  { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60000000, FLAGS },
373  { "d", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60000000, FLAGS },
374  { "overlap", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
375  { "o", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
376  { "curve1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
377  { "c1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
378  { "nofade", "no fade; keep audio as-is", 0, AV_OPT_TYPE_CONST, {.i64 = NONE }, 0, 0, FLAGS, "curve" },
379  { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
380  { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
381  { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
382  { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
383  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
384  { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
385  { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
386  { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
387  { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
388  { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
389  { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
390  { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve" },
391  { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
392  { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
393  { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve" },
394  { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve" },
395  { "losi", "logistic sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = LOSI }, 0, 0, FLAGS, "curve" },
396  { "sinc", "sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = SINC }, 0, 0, FLAGS, "curve" },
397  { "isinc", "inverted sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = ISINC}, 0, 0, FLAGS, "curve" },
398  { "curve2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
399  { "c2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
400  { NULL }
401 };
402 
403 AVFILTER_DEFINE_CLASS(acrossfade);
404 
405 #define CROSSFADE_PLANAR(name, type) \
406 static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
407  uint8_t * const *cf1, \
408  int nb_samples, int channels, \
409  int curve0, int curve1) \
410 { \
411  int i, c; \
412  \
413  for (i = 0; i < nb_samples; i++) { \
414  double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
415  double gain1 = fade_gain(curve1, i, nb_samples); \
416  for (c = 0; c < channels; c++) { \
417  type *d = (type *)dst[c]; \
418  const type *s0 = (type *)cf0[c]; \
419  const type *s1 = (type *)cf1[c]; \
420  \
421  d[i] = s0[i] * gain0 + s1[i] * gain1; \
422  } \
423  } \
424 }
425 
426 #define CROSSFADE(name, type) \
427 static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
428  uint8_t * const *cf1, \
429  int nb_samples, int channels, \
430  int curve0, int curve1) \
431 { \
432  type *d = (type *)dst[0]; \
433  const type *s0 = (type *)cf0[0]; \
434  const type *s1 = (type *)cf1[0]; \
435  int i, c, k = 0; \
436  \
437  for (i = 0; i < nb_samples; i++) { \
438  double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
439  double gain1 = fade_gain(curve1, i, nb_samples); \
440  for (c = 0; c < channels; c++, k++) \
441  d[k] = s0[k] * gain0 + s1[k] * gain1; \
442  } \
443 }
444 
445 CROSSFADE_PLANAR(dbl, double)
446 CROSSFADE_PLANAR(flt, float)
447 CROSSFADE_PLANAR(s16, int16_t)
448 CROSSFADE_PLANAR(s32, int32_t)
449 
450 CROSSFADE(dbl, double)
451 CROSSFADE(flt, float)
452 CROSSFADE(s16, int16_t)
453 CROSSFADE(s32, int32_t)
454 
455 static int activate(AVFilterContext *ctx)
456 {
457  AudioFadeContext *s = ctx->priv;
458  AVFilterLink *outlink = ctx->outputs[0];
459  AVFrame *in = NULL, *out, *cf[2] = { NULL };
460  int ret = 0, nb_samples, status;
461  int64_t pts;
462 
463  FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
464 
465  if (s->crossfade_is_over) {
466  ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
467  if (ret > 0) {
468  in->pts = s->pts;
469  s->pts += av_rescale_q(in->nb_samples,
470  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
471  return ff_filter_frame(outlink, in);
472  } else if (ret < 0) {
473  return ret;
474  } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
475  ff_outlink_set_status(ctx->outputs[0], status, pts);
476  return 0;
477  } else if (!ret) {
478  if (ff_outlink_frame_wanted(ctx->outputs[0])) {
480  return 0;
481  }
482  }
483  }
484 
485  if (ff_inlink_queued_samples(ctx->inputs[0]) > s->nb_samples) {
487  if (nb_samples > 0) {
489  if (ret < 0) {
490  return ret;
491  }
492  }
493  in->pts = s->pts;
494  s->pts += av_rescale_q(in->nb_samples,
495  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
496  return ff_filter_frame(outlink, in);
497  } else if (ff_inlink_queued_samples(ctx->inputs[0]) >= s->nb_samples &&
498  ff_inlink_queued_samples(ctx->inputs[1]) >= s->nb_samples && s->cf0_eof) {
499  if (s->overlap) {
500  out = ff_get_audio_buffer(outlink, s->nb_samples);
501  if (!out)
502  return AVERROR(ENOMEM);
503 
504  ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
505  if (ret < 0) {
506  av_frame_free(&out);
507  return ret;
508  }
509 
510  ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
511  if (ret < 0) {
512  av_frame_free(&out);
513  return ret;
514  }
515 
516  s->crossfade_samples(out->extended_data, cf[0]->extended_data,
517  cf[1]->extended_data,
518  s->nb_samples, out->channels,
519  s->curve, s->curve2);
520  out->pts = s->pts;
521  s->pts += av_rescale_q(s->nb_samples,
522  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
523  s->crossfade_is_over = 1;
524  av_frame_free(&cf[0]);
525  av_frame_free(&cf[1]);
526  return ff_filter_frame(outlink, out);
527  } else {
528  out = ff_get_audio_buffer(outlink, s->nb_samples);
529  if (!out)
530  return AVERROR(ENOMEM);
531 
532  ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
533  if (ret < 0) {
534  av_frame_free(&out);
535  return ret;
536  }
537 
538  s->fade_samples(out->extended_data, cf[0]->extended_data, s->nb_samples,
539  outlink->channels, -1, s->nb_samples - 1, s->nb_samples, s->curve);
540  out->pts = s->pts;
541  s->pts += av_rescale_q(s->nb_samples,
542  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
543  av_frame_free(&cf[0]);
544  ret = ff_filter_frame(outlink, out);
545  if (ret < 0)
546  return ret;
547 
548  out = ff_get_audio_buffer(outlink, s->nb_samples);
549  if (!out)
550  return AVERROR(ENOMEM);
551 
552  ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
553  if (ret < 0) {
554  av_frame_free(&out);
555  return ret;
556  }
557 
558  s->fade_samples(out->extended_data, cf[1]->extended_data, s->nb_samples,
559  outlink->channels, 1, 0, s->nb_samples, s->curve2);
560  out->pts = s->pts;
561  s->pts += av_rescale_q(s->nb_samples,
562  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
563  s->crossfade_is_over = 1;
564  av_frame_free(&cf[1]);
565  return ff_filter_frame(outlink, out);
566  }
567  } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
568  if (!s->cf0_eof && ff_outlink_get_status(ctx->inputs[0])) {
569  s->cf0_eof = 1;
570  }
571  if (ff_outlink_get_status(ctx->inputs[1])) {
573  return 0;
574  }
575  if (!s->cf0_eof)
577  else
579  return 0;
580  }
581 
582  return ret;
583 }
584 
585 static int acrossfade_config_output(AVFilterLink *outlink)
586 {
587  AVFilterContext *ctx = outlink->src;
588  AudioFadeContext *s = ctx->priv;
589 
590  if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
591  av_log(ctx, AV_LOG_ERROR,
592  "Inputs must have the same sample rate "
593  "%d for in0 vs %d for in1\n",
594  ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
595  return AVERROR(EINVAL);
596  }
597 
598  outlink->sample_rate = ctx->inputs[0]->sample_rate;
599  outlink->time_base = ctx->inputs[0]->time_base;
600  outlink->channel_layout = ctx->inputs[0]->channel_layout;
601  outlink->channels = ctx->inputs[0]->channels;
602 
603  switch (outlink->format) {
604  case AV_SAMPLE_FMT_DBL: s->crossfade_samples = crossfade_samples_dbl; break;
605  case AV_SAMPLE_FMT_DBLP: s->crossfade_samples = crossfade_samples_dblp; break;
606  case AV_SAMPLE_FMT_FLT: s->crossfade_samples = crossfade_samples_flt; break;
607  case AV_SAMPLE_FMT_FLTP: s->crossfade_samples = crossfade_samples_fltp; break;
608  case AV_SAMPLE_FMT_S16: s->crossfade_samples = crossfade_samples_s16; break;
609  case AV_SAMPLE_FMT_S16P: s->crossfade_samples = crossfade_samples_s16p; break;
610  case AV_SAMPLE_FMT_S32: s->crossfade_samples = crossfade_samples_s32; break;
611  case AV_SAMPLE_FMT_S32P: s->crossfade_samples = crossfade_samples_s32p; break;
612  }
613 
614  config_output(outlink);
615 
616  return 0;
617 }
618 
619 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
620  {
621  .name = "crossfade0",
622  .type = AVMEDIA_TYPE_AUDIO,
623  },
624  {
625  .name = "crossfade1",
626  .type = AVMEDIA_TYPE_AUDIO,
627  },
628  { NULL }
629 };
630 
631 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
632  {
633  .name = "default",
634  .type = AVMEDIA_TYPE_AUDIO,
635  .config_props = acrossfade_config_output,
636  },
637  { NULL }
638 };
639 
641  .name = "acrossfade",
642  .description = NULL_IF_CONFIG_SMALL("Cross fade two input audio streams."),
643  .query_formats = query_formats,
644  .priv_size = sizeof(AudioFadeContext),
645  .activate = activate,
646  .priv_class = &acrossfade_class,
647  .inputs = avfilter_af_acrossfade_inputs,
648  .outputs = avfilter_af_acrossfade_outputs,
649 };
650 
651 #endif /* CONFIG_ACROSSFADE_FILTER */
float, planar
Definition: samplefmt.h:69
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link&#39;s FIFO and update the link&#39;s stats.
Definition: avfilter.c:1489
#define NULL
Definition: coverity.c:32
#define CUBE(a)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
AVOption.
Definition: opt.h:248
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:212
Main libavfilter public API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static int config_output(AVFilterLink *outlink)
Definition: af_afade.c:214
double, planar
Definition: samplefmt.h:70
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:36
GLint GLenum type
Definition: opengl_enc.c:104
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:569
Definition: af_afade.c:54
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
Definition: af_afade.c:54
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1615
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:287
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:126
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:349
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1091
uint8_t
#define av_cold
Definition: attributes.h:88
AVOptions.
filter_frame For filters that do not use the activate() callback
static int query_formats(AVFilterContext *ctx)
Definition: af_afade.c:59
#define FADE_PLANAR(name, type)
Definition: af_afade.c:170
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:407
void(* fade_samples)(uint8_t **dst, uint8_t *const *src, int nb_samples, int channels, int direction, int64_t start, int64_t range, int curve)
Definition: af_afade.c:45
Definition: af_afade.c:54
#define AVERROR_EOF
End of file.
Definition: error.h:55
channels
Definition: aptx.h:33
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function.If this function returns true
signed 32 bits
Definition: samplefmt.h:62
#define A(x)
Definition: vp56_arith.h:28
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define src
Definition: vp8dsp.c:254
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1444
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:588
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition: samplefmt.c:237
CurveType
Definition: af_afade.c:54
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define B
Definition: huffyuvdsp.h:32
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
void * priv
private data for use by the filter
Definition: avfilter.h:356
#define cbrt
Definition: tablegen.h:35
int8_t exp
Definition: eval.c:72
void(* crossfade_samples)(uint8_t **dst, uint8_t *const *cf0, uint8_t *const *cf1, int nb_samples, int channels, int curve0, int curve1)
Definition: af_afade.c:48
Definition: af_afade.c:54
int channels
number of audio channels, only used for audio.
Definition: frame.h:620
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
signed 32 bits, planar
Definition: samplefmt.h:68
Definition: af_afade.c:54
int64_t start_sample
Definition: af_afade.c:37
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
int ff_inlink_queued_samples(AVFilterLink *link)
Definition: avfilter.c:1469
Definition: af_afade.c:54
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
Definition: af_afade.c:54
#define s(width, name)
Definition: cbs_vp9.c:257
int64_t pts
Definition: af_afade.c:43
int crossfade_is_over
Definition: af_afade.c:42
int64_t duration
Definition: af_afade.c:38
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
A list of supported channel layouts.
Definition: formats.h:86
s EdgeDetect Foobar g libavfilter vf_edgedetect c libavfilter vf_foobar c edit libavfilter and add an entry for foobar following the pattern of the other filters edit libavfilter allfilters and add an entry for foobar following the pattern of the other filters configure make j< whatever > ffmpeg ffmpeg i you should get a foobar png with Lena edge detected That s your new playground is ready Some little details about what s going which in turn will define variables for the build system and the C
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:387
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:594
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link&#39;s FIFO and update the link&#39;s stats.
Definition: avfilter.c:1508
Definition: af_afade.c:54
Definition: af_afade.c:54
int64_t start_time
Definition: af_afade.c:39
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: af_afade.c:54
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:145
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
Definition: avfilter.c:1638
int index
Definition: gxfenc.c:89
Rational number (pair of numerator and denominator).
Definition: rational.h:58
Definition: af_afade.c:54
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
#define FLAGS
Definition: af_afade.c:57
const char * name
Filter name.
Definition: avfilter.h:149
Definition: af_afade.c:54
Definition: af_afade.c:54
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:353
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:422
#define flags(name, subs,...)
Definition: cbs_av1.c:560
static int filter_frame(DBEContext *s, AVFrame *frame)
Definition: dolby_e.c:565
Definition: af_afade.c:54
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: af_afade.c:54
Definition: af_afade.c:54
signed 16 bits
Definition: samplefmt.h:61
static double fade_gain(int curve, int64_t index, int64_t range)
Definition: af_afade.c:92
#define OFFSET(x)
Definition: af_afade.c:56
Definition: af_afade.c:54
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:288
A list of supported formats for one end of a filter link.
Definition: formats.h:65
int64_t nb_samples
Definition: af_afade.c:36
An instance of a filter.
Definition: avfilter.h:341
AVFilter ff_af_afade
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:940
FILE * out
Definition: movenc.c:54
signed 16 bits, planar
Definition: samplefmt.h:67
#define M_PI
Definition: mathematics.h:52
formats
Definition: signature.h:48
AVFilter ff_af_acrossfade
Definition: af_afade.c:54
Definition: af_afade.c:54
internal API functions
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition...
Definition: formats.c:437
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:361
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:380
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:576
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:658
#define FADE(name, type)
Definition: af_afade.c:188
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
Definition: af_afade.c:54