FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #define FF_INTERNAL_FIELDS 1
27 #include "framequeue.h"
28 
29 #include "libavutil/audio_fifo.h"
30 #include "libavutil/opt.h"
31 #include "audio.h"
32 #include "avfilter.h"
33 #include "filters.h"
34 #include "internal.h"
35 
36 typedef struct AudioFadeContext {
37  const AVClass *class;
38  int type;
39  int curve, curve2;
40  int64_t nb_samples;
41  int64_t start_sample;
42  int64_t duration;
43  int64_t start_time;
44  int overlap;
45  int cf0_eof;
46  int prev_size;
49  int64_t pts;
50 
51  void (*fade_samples)(uint8_t **dst, uint8_t * const *src,
52  int nb_samples, int channels, int direction,
53  int64_t start, int64_t range, int curve);
54  void (*crossfade_samples)(uint8_t **dst, uint8_t * const *cf0,
55  uint8_t * const *cf1,
56  int nb_samples, int channels,
57  int curve0, int curve1);
59 
61 
62 #define OFFSET(x) offsetof(AudioFadeContext, x)
63 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
64 
66 {
69  static const enum AVSampleFormat sample_fmts[] = {
75  };
76  int ret;
77 
78  layouts = ff_all_channel_counts();
79  if (!layouts)
80  return AVERROR(ENOMEM);
81  ret = ff_set_common_channel_layouts(ctx, layouts);
82  if (ret < 0)
83  return ret;
84 
85  formats = ff_make_format_list(sample_fmts);
86  if (!formats)
87  return AVERROR(ENOMEM);
88  ret = ff_set_common_formats(ctx, formats);
89  if (ret < 0)
90  return ret;
91 
92  formats = ff_all_samplerates();
93  if (!formats)
94  return AVERROR(ENOMEM);
95  return ff_set_common_samplerates(ctx, formats);
96 }
97 
98 static double fade_gain(int curve, int64_t index, int64_t range)
99 {
100 #define CUBE(a) ((a)*(a)*(a))
101  double gain;
102 
103  gain = av_clipd(1.0 * index / range, 0, 1.0);
104 
105  switch (curve) {
106  case QSIN:
107  gain = sin(gain * M_PI / 2.0);
108  break;
109  case IQSIN:
110  /* 0.6... = 2 / M_PI */
111  gain = 0.6366197723675814 * asin(gain);
112  break;
113  case ESIN:
114  gain = 1.0 - cos(M_PI / 4.0 * (CUBE(2.0*gain - 1) + 1));
115  break;
116  case HSIN:
117  gain = (1.0 - cos(gain * M_PI)) / 2.0;
118  break;
119  case IHSIN:
120  /* 0.3... = 1 / M_PI */
121  gain = 0.3183098861837907 * acos(1 - 2 * gain);
122  break;
123  case EXP:
124  /* -11.5... = 5*ln(0.1) */
125  gain = exp(-11.512925464970227 * (1 - gain));
126  break;
127  case LOG:
128  gain = av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
129  break;
130  case PAR:
131  gain = 1 - sqrt(1 - gain);
132  break;
133  case IPAR:
134  gain = (1 - (1 - gain) * (1 - gain));
135  break;
136  case QUA:
137  gain *= gain;
138  break;
139  case CUB:
140  gain = CUBE(gain);
141  break;
142  case SQU:
143  gain = sqrt(gain);
144  break;
145  case CBR:
146  gain = cbrt(gain);
147  break;
148  case DESE:
149  gain = gain <= 0.5 ? cbrt(2 * gain) / 2: 1 - cbrt(2 * (1 - gain)) / 2;
150  break;
151  case DESI:
152  gain = gain <= 0.5 ? CUBE(2 * gain) / 2: 1 - CUBE(2 * (1 - gain)) / 2;
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, int64_t 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, int64_t 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_output(AVFilterLink *outlink)
204 {
205  AVFilterContext *ctx = outlink->src;
206  AudioFadeContext *s = ctx->priv;
207 
208  switch (outlink->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, outlink->sample_rate, AV_TIME_BASE);
221  if (s->start_time)
222  s->start_sample = av_rescale(s->start_time, outlink->sample_rate, AV_TIME_BASE);
223 
224  return 0;
225 }
226 
227 #if CONFIG_AFADE_FILTER
228 
229 static const AVOption afade_options[] = {
230  { "type", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
231  { "t", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
232  { "in", "fade-in", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "type" },
233  { "out", "fade-out", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "type" },
234  { "start_sample", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
235  { "ss", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
236  { "nb_samples", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT64_MAX, FLAGS },
237  { "ns", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT64_MAX, FLAGS },
238  { "start_time", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
239  { "st", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
240  { "duration", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
241  { "d", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
242  { "curve", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
243  { "c", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
244  { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
245  { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
246  { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
247  { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
248  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
249  { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
250  { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
251  { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
252  { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
253  { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
254  { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
255  { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve" },
256  { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
257  { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
258  { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve" },
259  { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve" },
260  { NULL }
261 };
262 
263 AVFILTER_DEFINE_CLASS(afade);
264 
265 static av_cold int init(AVFilterContext *ctx)
266 {
267  AudioFadeContext *s = ctx->priv;
268 
269  if (INT64_MAX - s->nb_samples < s->start_sample)
270  return AVERROR(EINVAL);
271 
272  return 0;
273 }
274 
275 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
276 {
277  AudioFadeContext *s = inlink->dst->priv;
278  AVFilterLink *outlink = inlink->dst->outputs[0];
279  int nb_samples = buf->nb_samples;
280  AVFrame *out_buf;
281  int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
282 
283  if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
284  ( s->type && (cur_sample + nb_samples < s->start_sample)))
285  return ff_filter_frame(outlink, buf);
286 
287  if (av_frame_is_writable(buf)) {
288  out_buf = buf;
289  } else {
290  out_buf = ff_get_audio_buffer(outlink, nb_samples);
291  if (!out_buf)
292  return AVERROR(ENOMEM);
293  av_frame_copy_props(out_buf, buf);
294  }
295 
296  if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
297  ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
298  av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
299  out_buf->channels, out_buf->format);
300  } else {
301  int64_t start;
302 
303  if (!s->type)
304  start = cur_sample - s->start_sample;
305  else
306  start = s->start_sample + s->nb_samples - cur_sample;
307 
308  s->fade_samples(out_buf->extended_data, buf->extended_data,
309  nb_samples, buf->channels,
310  s->type ? -1 : 1, start,
311  s->nb_samples, s->curve);
312  }
313 
314  if (buf != out_buf)
315  av_frame_free(&buf);
316 
317  return ff_filter_frame(outlink, out_buf);
318 }
319 
320 static const AVFilterPad avfilter_af_afade_inputs[] = {
321  {
322  .name = "default",
323  .type = AVMEDIA_TYPE_AUDIO,
324  .filter_frame = filter_frame,
325  },
326  { NULL }
327 };
328 
329 static const AVFilterPad avfilter_af_afade_outputs[] = {
330  {
331  .name = "default",
332  .type = AVMEDIA_TYPE_AUDIO,
333  .config_props = config_output,
334  },
335  { NULL }
336 };
337 
339  .name = "afade",
340  .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
341  .query_formats = query_formats,
342  .priv_size = sizeof(AudioFadeContext),
343  .init = init,
344  .inputs = avfilter_af_afade_inputs,
345  .outputs = avfilter_af_afade_outputs,
346  .priv_class = &afade_class,
348 };
349 
350 #endif /* CONFIG_AFADE_FILTER */
351 
352 #if CONFIG_ACROSSFADE_FILTER
353 
354 static const AVOption acrossfade_options[] = {
355  { "nb_samples", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
356  { "ns", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
357  { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60, FLAGS },
358  { "d", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60, FLAGS },
359  { "overlap", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
360  { "o", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
361  { "curve1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
362  { "c1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
363  { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
364  { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
365  { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
366  { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
367  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
368  { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
369  { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
370  { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
371  { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
372  { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
373  { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
374  { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve" },
375  { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
376  { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
377  { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve" },
378  { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve" },
379  { "curve2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
380  { "c2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
381  { NULL }
382 };
383 
384 AVFILTER_DEFINE_CLASS(acrossfade);
385 
386 #define CROSSFADE_PLANAR(name, type) \
387 static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
388  uint8_t * const *cf1, \
389  int nb_samples, int channels, \
390  int curve0, int curve1) \
391 { \
392  int i, c; \
393  \
394  for (i = 0; i < nb_samples; i++) { \
395  double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
396  double gain1 = fade_gain(curve1, i, nb_samples); \
397  for (c = 0; c < channels; c++) { \
398  type *d = (type *)dst[c]; \
399  const type *s0 = (type *)cf0[c]; \
400  const type *s1 = (type *)cf1[c]; \
401  \
402  d[i] = s0[i] * gain0 + s1[i] * gain1; \
403  } \
404  } \
405 }
406 
407 #define CROSSFADE(name, type) \
408 static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
409  uint8_t * const *cf1, \
410  int nb_samples, int channels, \
411  int curve0, int curve1) \
412 { \
413  type *d = (type *)dst[0]; \
414  const type *s0 = (type *)cf0[0]; \
415  const type *s1 = (type *)cf1[0]; \
416  int i, c, k = 0; \
417  \
418  for (i = 0; i < nb_samples; i++) { \
419  double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
420  double gain1 = fade_gain(curve1, i, nb_samples); \
421  for (c = 0; c < channels; c++, k++) \
422  d[k] = s0[k] * gain0 + s1[k] * gain1; \
423  } \
424 }
425 
426 CROSSFADE_PLANAR(dbl, double)
427 CROSSFADE_PLANAR(flt, float)
428 CROSSFADE_PLANAR(s16, int16_t)
429 CROSSFADE_PLANAR(s32, int32_t)
430 
431 CROSSFADE(dbl, double)
432 CROSSFADE(flt, float)
433 CROSSFADE(s16, int16_t)
434 CROSSFADE(s32, int32_t)
435 
436 static int activate(AVFilterContext *ctx)
437 {
438  AudioFadeContext *s = ctx->priv;
439  AVFilterLink *outlink = ctx->outputs[0];
440  AVFrame *in = NULL, *out, *cf[2] = { NULL };
441  int ret = 0, nb_samples, status;
442  int64_t pts;
443 
444  if (s->crossfade_is_over) {
445  ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
446  if (ret < 0) {
447  return ret;
448  } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
449  ff_outlink_set_status(ctx->outputs[0], status, pts);
450  return 0;
451  } else {
452  if (ff_outlink_frame_wanted(ctx->outputs[0]) && !in) {
453  ff_inlink_request_frame(ctx->inputs[1]);
454  return 0;
455  }
456  }
457  in->pts = s->pts;
458  s->pts += av_rescale_q(in->nb_samples,
459  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
460  return ff_filter_frame(outlink, in);
461  }
462 
463  if (ff_framequeue_queued_samples(&ctx->inputs[0]->fifo) > s->nb_samples) {
464  nb_samples = ff_framequeue_queued_samples(&ctx->inputs[0]->fifo) - s->nb_samples;
465  if (nb_samples > 0) {
466  ret = ff_inlink_consume_samples(ctx->inputs[0], nb_samples, nb_samples, &in);
467  if (ret < 0) {
468  return ret;
469  }
470  }
471  in->pts = s->pts;
472  s->pts += av_rescale_q(in->nb_samples,
473  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
474  return ff_filter_frame(outlink, in);
475  } else if (ff_framequeue_queued_samples(&ctx->inputs[1]->fifo) >= s->nb_samples) {
476  if (s->overlap) {
477  out = ff_get_audio_buffer(outlink, s->nb_samples);
478  if (!out)
479  return AVERROR(ENOMEM);
480 
481  ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
482  if (ret < 0) {
483  av_frame_free(&out);
484  return ret;
485  }
486 
487  ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
488  if (ret < 0) {
489  av_frame_free(&out);
490  return ret;
491  }
492 
493  s->crossfade_samples(out->extended_data, cf[0]->extended_data,
494  cf[1]->extended_data,
495  s->nb_samples, out->channels,
496  s->curve, s->curve2);
497  out->pts = s->pts;
498  s->pts += av_rescale_q(s->nb_samples,
499  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
500  s->crossfade_is_over = 1;
501  av_frame_free(&cf[0]);
502  av_frame_free(&cf[1]);
503  return ff_filter_frame(outlink, out);
504  } else {
505  out = ff_get_audio_buffer(outlink, s->nb_samples);
506  if (!out)
507  return AVERROR(ENOMEM);
508 
509  ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
510  if (ret < 0) {
511  av_frame_free(&out);
512  return ret;
513  }
514 
515  s->fade_samples(out->extended_data, cf[0]->extended_data, s->nb_samples,
516  outlink->channels, -1, s->nb_samples - 1, s->nb_samples, s->curve);
517  out->pts = s->pts;
518  s->pts += av_rescale_q(s->nb_samples,
519  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
520  av_frame_free(&cf[0]);
521  ret = ff_filter_frame(outlink, out);
522  if (ret < 0)
523  return ret;
524 
525  out = ff_get_audio_buffer(outlink, s->nb_samples);
526  if (!out)
527  return AVERROR(ENOMEM);
528 
529  ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
530  if (ret < 0) {
531  av_frame_free(&out);
532  return ret;
533  }
534 
535  s->fade_samples(out->extended_data, cf[1]->extended_data, s->nb_samples,
536  outlink->channels, 1, 0, s->nb_samples, s->curve2);
537  out->pts = s->pts;
538  s->pts += av_rescale_q(s->nb_samples,
539  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
540  s->crossfade_is_over = 1;
541  av_frame_free(&cf[1]);
542  return ff_filter_frame(outlink, out);
543  }
544  } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
545  if (!s->cf0_eof && ctx->inputs[0]->status_in) {
546  s->cf0_eof = 1;
547  }
548  if (ctx->inputs[1]->status_in) {
550  return 0;
551  }
552  if (!s->cf0_eof)
553  ff_inlink_request_frame(ctx->inputs[0]);
554  else
555  ff_inlink_request_frame(ctx->inputs[1]);
556  return 0;
557  }
558 
559  return ret;
560 }
561 
562 static int acrossfade_config_output(AVFilterLink *outlink)
563 {
564  AVFilterContext *ctx = outlink->src;
565  AudioFadeContext *s = ctx->priv;
566 
567  if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
568  av_log(ctx, AV_LOG_ERROR,
569  "Inputs must have the same sample rate "
570  "%d for in0 vs %d for in1\n",
571  ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
572  return AVERROR(EINVAL);
573  }
574 
575  outlink->sample_rate = ctx->inputs[0]->sample_rate;
576  outlink->time_base = ctx->inputs[0]->time_base;
577  outlink->channel_layout = ctx->inputs[0]->channel_layout;
578  outlink->channels = ctx->inputs[0]->channels;
579 
580  switch (outlink->format) {
581  case AV_SAMPLE_FMT_DBL: s->crossfade_samples = crossfade_samples_dbl; break;
582  case AV_SAMPLE_FMT_DBLP: s->crossfade_samples = crossfade_samples_dblp; break;
583  case AV_SAMPLE_FMT_FLT: s->crossfade_samples = crossfade_samples_flt; break;
584  case AV_SAMPLE_FMT_FLTP: s->crossfade_samples = crossfade_samples_fltp; break;
585  case AV_SAMPLE_FMT_S16: s->crossfade_samples = crossfade_samples_s16; break;
586  case AV_SAMPLE_FMT_S16P: s->crossfade_samples = crossfade_samples_s16p; break;
587  case AV_SAMPLE_FMT_S32: s->crossfade_samples = crossfade_samples_s32; break;
588  case AV_SAMPLE_FMT_S32P: s->crossfade_samples = crossfade_samples_s32p; break;
589  }
590 
591  config_output(outlink);
592 
593  return 0;
594 }
595 
596 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
597  {
598  .name = "crossfade0",
599  .type = AVMEDIA_TYPE_AUDIO,
600  },
601  {
602  .name = "crossfade1",
603  .type = AVMEDIA_TYPE_AUDIO,
604  },
605  { NULL }
606 };
607 
608 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
609  {
610  .name = "default",
611  .type = AVMEDIA_TYPE_AUDIO,
612  .config_props = acrossfade_config_output,
613  },
614  { NULL }
615 };
616 
618  .name = "acrossfade",
619  .description = NULL_IF_CONFIG_SMALL("Cross fade two input audio streams."),
620  .query_formats = query_formats,
621  .priv_size = sizeof(AudioFadeContext),
622  .activate = activate,
623  .priv_class = &acrossfade_class,
624  .inputs = avfilter_af_acrossfade_inputs,
625  .outputs = avfilter_af_acrossfade_outputs,
626 };
627 
628 #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's FIFO and update the link's stats.
Definition: avfilter.c:1471
#define NULL
Definition: coverity.c:32
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:549
const char * s
Definition: avisynth_c.h:768
#define CUBE(a)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
AVOption.
Definition: opt.h:246
Main libavfilter public API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
channels
Definition: aptx.c:30
static int config_output(AVFilterLink *outlink)
Definition: af_afade.c:203
double, planar
Definition: samplefmt.h:70
Definition: af_afade.c:60
#define src
Definition: vp8dsp.c:254
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:169
Definition: af_afade.c:60
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1592
static int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition: filters.h:152
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
#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:125
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
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:51
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
static int activate(AVFilterContext *ctx)
Definition: af_amix.c:421
static int64_t start_time
Definition: ffplay.c:327
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
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:54
static int query_formats(AVFilterContext *ctx)
Definition: af_afade.c:65
#define FADE_PLANAR(name, type)
Definition: af_afade.c:159
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:311
int64_t duration
Definition: movenc.c:63
static int flags
Definition: log.c:55
#define AVERROR_EOF
End of file.
Definition: error.h:55
signed 32 bits
Definition: samplefmt.h:62
#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
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:1436
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:568
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:60
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
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define cbrt
Definition: tablegen.h:35
int8_t exp
Definition: eval.c:72
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
Definition: af_afade.c:60
int channels
number of audio channels, only used for audio.
Definition: frame.h:523
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
int64_t start_sample
Definition: af_afade.c:41
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
Definition: af_afade.c:60
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
Definition: af_afade.c:60
int64_t pts
Definition: af_afade.c:49
int crossfade_is_over
Definition: af_afade.c:47
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
int64_t duration
Definition: af_afade.c:42
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
A list of supported channel layouts.
Definition: formats.h:85
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:291
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:592
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition: avfilter.c:1490
AVAudioFifo * fifo[2]
Definition: af_afade.c:48
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
void * buf
Definition: avisynth_c.h:690
Definition: af_afade.c:60
int64_t start_time
Definition: af_afade.c:43
GLint GLenum type
Definition: opengl_enc.c:105
Definition: af_afade.c:60
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
int index
Definition: gxfenc.c:89
Rational number (pair of numerator and denominator).
Definition: rational.h:58
Definition: af_afade.c:60
#define FLAGS
Definition: af_afade.c:63
const char * name
Filter name.
Definition: avfilter.h:148
Definition: af_afade.c:60
Definition: af_afade.c:60
static uint64_t ff_framequeue_queued_samples(const FFFrameQueue *fq)
Get the number of queued samples.
Definition: framequeue.h:154
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
static int64_t pts
static int filter_frame(DBEContext *s, AVFrame *frame)
Definition: dolby_e.c:565
Definition: af_afade.c:60
Definition: af_afade.c:60
Definition: af_afade.c:60
signed 16 bits
Definition: samplefmt.h:61
static double fade_gain(int curve, int64_t index, int64_t range)
Definition: af_afade.c:98
#define OFFSET(x)
Definition: af_afade.c:62
Definition: af_afade.c:60
Audio FIFO Buffer.
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:334
A list of supported formats for one end of a filter link.
Definition: formats.h:64
int64_t nb_samples
Definition: af_afade.c:40
An instance of a filter.
Definition: avfilter.h:338
AVFilter ff_af_afade
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
FILE * out
Definition: movenc.c:54
void INT64 start
Definition: avisynth_c.h:690
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:60
internal API functions
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition...
Definition: formats.c:410
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:265
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:284
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:652
#define FADE(name, type)
Definition: af_afade.c:177
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
Definition: af_afade.c:60