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 #include "libavutil/audio_fifo.h"
27 #include "libavutil/opt.h"
28 #include "audio.h"
29 #include "avfilter.h"
30 #include "internal.h"
31 
32 typedef struct {
33  const AVClass *class;
34  int type;
35  int curve, curve2;
37  int64_t start_sample;
38  int64_t duration;
39  int64_t start_time;
40  int overlap;
41  int cf0_eof;
43  AVAudioFifo *fifo[2];
44  int64_t pts;
45 
46  void (*fade_samples)(uint8_t **dst, uint8_t * const *src,
47  int nb_samples, int channels, int direction,
48  int64_t start, int range, int curve);
49  void (*crossfade_samples)(uint8_t **dst, uint8_t * const *cf0,
50  uint8_t * const *cf1,
51  int nb_samples, int channels,
52  int curve0, int curve1);
54 
56 
57 #define OFFSET(x) offsetof(AudioFadeContext, x)
58 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59 
61 {
64  static const enum AVSampleFormat sample_fmts[] = {
70  };
71  int ret;
72 
73  layouts = ff_all_channel_layouts();
74  if (!layouts)
75  return AVERROR(ENOMEM);
76  ret = ff_set_common_channel_layouts(ctx, layouts);
77  if (ret < 0)
78  return ret;
79 
80  formats = ff_make_format_list(sample_fmts);
81  if (!formats)
82  return AVERROR(ENOMEM);
83  ret = ff_set_common_formats(ctx, formats);
84  if (ret < 0)
85  return ret;
86 
87  formats = ff_all_samplerates();
88  if (!formats)
89  return AVERROR(ENOMEM);
90  return ff_set_common_samplerates(ctx, formats);
91 }
92 
93 static double fade_gain(int curve, int64_t index, int range)
94 {
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  gain = 0.636943 * asin(gain);
105  break;
106  case ESIN:
107  gain = 1.0 - cos(M_PI / 4.0 * (pow(2.0*gain - 1, 3) + 1));
108  break;
109  case HSIN:
110  gain = (1.0 - cos(gain * M_PI)) / 2.0;
111  break;
112  case IHSIN:
113  gain = 0.318471 * acos(1 - 2 * gain);
114  break;
115  case EXP:
116  gain = pow(0.1, (1 - gain) * 5.0);
117  break;
118  case LOG:
119  gain = av_clipd(0.0868589 * log(100000 * gain), 0, 1.0);
120  break;
121  case PAR:
122  gain = 1 - sqrt(1 - gain);
123  break;
124  case IPAR:
125  gain = (1 - (1 - gain) * (1 - gain));
126  break;
127  case QUA:
128  gain *= gain;
129  break;
130  case CUB:
131  gain = gain * gain * gain;
132  break;
133  case SQU:
134  gain = sqrt(gain);
135  break;
136  case CBR:
137  gain = cbrt(gain);
138  break;
139  case DESE:
140  gain = gain <= 0.5 ? pow(2 * gain, 1/3.) / 2: 1 - pow(2 * (1 - gain), 1/3.) / 2;
141  break;
142  case DESI:
143  gain = gain <= 0.5 ? pow(2 * gain, 3) / 2: 1 - pow(2 * (1 - gain), 3) / 2;
144  break;
145  }
146 
147  return gain;
148 }
149 
150 #define FADE_PLANAR(name, type) \
151 static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
152  int nb_samples, int channels, int dir, \
153  int64_t start, int range, int curve) \
154 { \
155  int i, c; \
156  \
157  for (i = 0; i < nb_samples; i++) { \
158  double gain = fade_gain(curve, start + i * dir, range); \
159  for (c = 0; c < channels; c++) { \
160  type *d = (type *)dst[c]; \
161  const type *s = (type *)src[c]; \
162  \
163  d[i] = s[i] * gain; \
164  } \
165  } \
166 }
167 
168 #define FADE(name, type) \
169 static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
170  int nb_samples, int channels, int dir, \
171  int64_t start, int range, int curve) \
172 { \
173  type *d = (type *)dst[0]; \
174  const type *s = (type *)src[0]; \
175  int i, c, k = 0; \
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++, k++) \
180  d[k] = s[k] * gain; \
181  } \
182 }
183 
184 FADE_PLANAR(dbl, double)
185 FADE_PLANAR(flt, float)
186 FADE_PLANAR(s16, int16_t)
187 FADE_PLANAR(s32, int32_t)
188 
189 FADE(dbl, double)
190 FADE(flt, float)
191 FADE(s16, int16_t)
192 FADE(s32, int32_t)
193 
194 static int config_output(AVFilterLink *outlink)
195 {
196  AVFilterContext *ctx = outlink->src;
197  AudioFadeContext *s = ctx->priv;
198 
199  switch (outlink->format) {
200  case AV_SAMPLE_FMT_DBL: s->fade_samples = fade_samples_dbl; break;
201  case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break;
202  case AV_SAMPLE_FMT_FLT: s->fade_samples = fade_samples_flt; break;
203  case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break;
204  case AV_SAMPLE_FMT_S16: s->fade_samples = fade_samples_s16; break;
205  case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break;
206  case AV_SAMPLE_FMT_S32: s->fade_samples = fade_samples_s32; break;
207  case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break;
208  }
209 
210  if (s->duration)
211  s->nb_samples = av_rescale(s->duration, outlink->sample_rate, AV_TIME_BASE);
212  if (s->start_time)
213  s->start_sample = av_rescale(s->start_time, outlink->sample_rate, AV_TIME_BASE);
214 
215  return 0;
216 }
217 
218 #if CONFIG_AFADE_FILTER
219 
220 static const AVOption afade_options[] = {
221  { "type", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
222  { "t", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
223  { "in", "fade-in", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "type" },
224  { "out", "fade-out", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "type" },
225  { "start_sample", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
226  { "ss", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
227  { "nb_samples", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
228  { "ns", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
229  { "start_time", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
230  { "st", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
231  { "duration", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
232  { "d", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
233  { "curve", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
234  { "c", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
235  { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
236  { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
237  { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
238  { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
239  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
240  { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
241  { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
242  { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
243  { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
244  { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
245  { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
246  { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve" },
247  { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
248  { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
249  { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve" },
250  { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve" },
251  { NULL }
252 };
253 
254 AVFILTER_DEFINE_CLASS(afade);
255 
256 static av_cold int init(AVFilterContext *ctx)
257 {
258  AudioFadeContext *s = ctx->priv;
259 
260  if (INT64_MAX - s->nb_samples < s->start_sample)
261  return AVERROR(EINVAL);
262 
263  return 0;
264 }
265 
266 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
267 {
268  AudioFadeContext *s = inlink->dst->priv;
269  AVFilterLink *outlink = inlink->dst->outputs[0];
270  int nb_samples = buf->nb_samples;
271  AVFrame *out_buf;
272  int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
273 
274  if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
275  ( s->type && (cur_sample + s->nb_samples < s->start_sample)))
276  return ff_filter_frame(outlink, buf);
277 
278  if (av_frame_is_writable(buf)) {
279  out_buf = buf;
280  } else {
281  out_buf = ff_get_audio_buffer(inlink, nb_samples);
282  if (!out_buf)
283  return AVERROR(ENOMEM);
284  av_frame_copy_props(out_buf, buf);
285  }
286 
287  if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
288  ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
289  av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
290  av_frame_get_channels(out_buf), out_buf->format);
291  } else {
292  int64_t start;
293 
294  if (!s->type)
295  start = cur_sample - s->start_sample;
296  else
297  start = s->start_sample + s->nb_samples - cur_sample;
298 
299  s->fade_samples(out_buf->extended_data, buf->extended_data,
300  nb_samples, av_frame_get_channels(buf),
301  s->type ? -1 : 1, start,
302  s->nb_samples, s->curve);
303  }
304 
305  if (buf != out_buf)
306  av_frame_free(&buf);
307 
308  return ff_filter_frame(outlink, out_buf);
309 }
310 
311 static const AVFilterPad avfilter_af_afade_inputs[] = {
312  {
313  .name = "default",
314  .type = AVMEDIA_TYPE_AUDIO,
315  .filter_frame = filter_frame,
316  },
317  { NULL }
318 };
319 
320 static const AVFilterPad avfilter_af_afade_outputs[] = {
321  {
322  .name = "default",
323  .type = AVMEDIA_TYPE_AUDIO,
324  .config_props = config_output,
325  },
326  { NULL }
327 };
328 
329 AVFilter ff_af_afade = {
330  .name = "afade",
331  .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
332  .query_formats = query_formats,
333  .priv_size = sizeof(AudioFadeContext),
334  .init = init,
335  .inputs = avfilter_af_afade_inputs,
336  .outputs = avfilter_af_afade_outputs,
337  .priv_class = &afade_class,
339 };
340 
341 #endif /* CONFIG_AFADE_FILTER */
342 
343 #if CONFIG_ACROSSFADE_FILTER
344 
345 static const AVOption acrossfade_options[] = {
346  { "nb_samples", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
347  { "ns", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
348  { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60, FLAGS },
349  { "d", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60, FLAGS },
350  { "overlap", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, FLAGS },
351  { "o", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, FLAGS },
352  { "curve1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve1" },
353  { "c1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve1" },
354  { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve1" },
355  { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve1" },
356  { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve1" },
357  { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve1" },
358  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve1" },
359  { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve1" },
360  { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve1" },
361  { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve1" },
362  { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve1" },
363  { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve1" },
364  { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve1" },
365  { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve1" },
366  { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve1" },
367  { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve1" },
368  { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve1" },
369  { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve1" },
370  { "curve2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve2" },
371  { "c2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve2" },
372  { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve2" },
373  { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve2" },
374  { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve2" },
375  { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve2" },
376  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve2" },
377  { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve2" },
378  { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve2" },
379  { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve2" },
380  { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve2" },
381  { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve2" },
382  { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve2" },
383  { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve2" },
384  { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve2" },
385  { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve2" },
386  { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve2" },
387  { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve2" },
388  { NULL }
389 };
390 
391 AVFILTER_DEFINE_CLASS(acrossfade);
392 
393 #define CROSSFADE_PLANAR(name, type) \
394 static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
395  uint8_t * const *cf1, \
396  int nb_samples, int channels, \
397  int curve0, int curve1) \
398 { \
399  int i, c; \
400  \
401  for (i = 0; i < nb_samples; i++) { \
402  double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
403  double gain1 = fade_gain(curve1, i, nb_samples); \
404  for (c = 0; c < channels; c++) { \
405  type *d = (type *)dst[c]; \
406  const type *s0 = (type *)cf0[c]; \
407  const type *s1 = (type *)cf1[c]; \
408  \
409  d[i] = s0[i] * gain0 + s1[i] * gain1; \
410  } \
411  } \
412 }
413 
414 #define CROSSFADE(name, type) \
415 static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
416  uint8_t * const *cf1, \
417  int nb_samples, int channels, \
418  int curve0, int curve1) \
419 { \
420  type *d = (type *)dst[0]; \
421  const type *s0 = (type *)cf0[0]; \
422  const type *s1 = (type *)cf1[0]; \
423  int i, c, k = 0; \
424  \
425  for (i = 0; i < nb_samples; i++) { \
426  double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
427  double gain1 = fade_gain(curve1, i, nb_samples); \
428  for (c = 0; c < channels; c++, k++) \
429  d[k] = s0[k] * gain0 + s1[k] * gain1; \
430  } \
431 }
432 
433 CROSSFADE_PLANAR(dbl, double)
434 CROSSFADE_PLANAR(flt, float)
435 CROSSFADE_PLANAR(s16, int16_t)
436 CROSSFADE_PLANAR(s32, int32_t)
437 
438 CROSSFADE(dbl, double)
439 CROSSFADE(flt, float)
440 CROSSFADE(s16, int16_t)
441 CROSSFADE(s32, int32_t)
442 
443 static int acrossfade_filter_frame(AVFilterLink *inlink, AVFrame *in)
444 {
445  AVFilterContext *ctx = inlink->dst;
446  AudioFadeContext *s = ctx->priv;
447  AVFilterLink *outlink = ctx->outputs[0];
448  AVFrame *out, *cf[2] = { NULL };
449  int ret = 0, nb_samples;
450 
451  if (s->crossfade_is_over) {
452  in->pts = s->pts;
453  s->pts += av_rescale_q(in->nb_samples,
454  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
455  return ff_filter_frame(outlink, in);
456  } else if (inlink == ctx->inputs[0]) {
457  av_audio_fifo_write(s->fifo[0], (void **)in->extended_data, in->nb_samples);
458 
459  nb_samples = av_audio_fifo_size(s->fifo[0]) - s->nb_samples;
460  if (nb_samples > 0) {
461  out = ff_get_audio_buffer(outlink, nb_samples);
462  if (!out) {
463  ret = AVERROR(ENOMEM);
464  goto fail;
465  }
466  av_audio_fifo_read(s->fifo[0], (void **)out->extended_data, nb_samples);
467  out->pts = s->pts;
468  s->pts += av_rescale_q(nb_samples,
469  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
470  ret = ff_filter_frame(outlink, out);
471  }
472  } else if (av_audio_fifo_size(s->fifo[1]) < s->nb_samples) {
473  if (!s->overlap && av_audio_fifo_size(s->fifo[0]) > 0) {
474  nb_samples = av_audio_fifo_size(s->fifo[0]);
475 
476  cf[0] = ff_get_audio_buffer(outlink, nb_samples);
477  out = ff_get_audio_buffer(outlink, nb_samples);
478  if (!out || !cf[0]) {
479  ret = AVERROR(ENOMEM);
480  goto fail;
481  }
482  av_audio_fifo_read(s->fifo[0], (void **)cf[0]->extended_data, nb_samples);
483 
484  s->fade_samples(out->extended_data, cf[0]->extended_data, nb_samples,
485  outlink->channels, -1, nb_samples - 1, nb_samples, s->curve);
486  out->pts = s->pts;
487  s->pts += av_rescale_q(nb_samples,
488  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
489  ret = ff_filter_frame(outlink, out);
490  if (ret < 0)
491  goto fail;
492  }
493 
494  av_audio_fifo_write(s->fifo[1], (void **)in->extended_data, in->nb_samples);
495  } else if (av_audio_fifo_size(s->fifo[1]) >= s->nb_samples) {
496  if (s->overlap) {
497  cf[0] = ff_get_audio_buffer(outlink, s->nb_samples);
498  cf[1] = ff_get_audio_buffer(outlink, s->nb_samples);
499  out = ff_get_audio_buffer(outlink, s->nb_samples);
500  if (!out || !cf[0] || !cf[1]) {
501  av_frame_free(&out);
502  ret = AVERROR(ENOMEM);
503  goto fail;
504  }
505 
506  av_audio_fifo_read(s->fifo[0], (void **)cf[0]->extended_data, s->nb_samples);
507  av_audio_fifo_read(s->fifo[1], (void **)cf[1]->extended_data, s->nb_samples);
508 
510  cf[1]->extended_data,
512  s->curve, s->curve2);
513  out->pts = s->pts;
514  s->pts += av_rescale_q(s->nb_samples,
515  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
516  ret = ff_filter_frame(outlink, out);
517  if (ret < 0)
518  goto fail;
519  } else {
520  out = ff_get_audio_buffer(outlink, s->nb_samples);
521  cf[1] = ff_get_audio_buffer(outlink, s->nb_samples);
522  if (!out || !cf[1]) {
523  ret = AVERROR(ENOMEM);
524  av_frame_free(&out);
525  goto fail;
526  }
527 
528  av_audio_fifo_read(s->fifo[1], (void **)cf[1]->extended_data, s->nb_samples);
529 
531  outlink->channels, 1, 0, s->nb_samples, s->curve2);
532  out->pts = s->pts;
533  s->pts += av_rescale_q(s->nb_samples,
534  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
535  ret = ff_filter_frame(outlink, out);
536  if (ret < 0)
537  goto fail;
538  }
539 
540  nb_samples = av_audio_fifo_size(s->fifo[1]);
541  if (nb_samples > 0) {
542  out = ff_get_audio_buffer(outlink, nb_samples);
543  if (!out) {
544  ret = AVERROR(ENOMEM);
545  goto fail;
546  }
547 
548  av_audio_fifo_read(s->fifo[1], (void **)out->extended_data, nb_samples);
549  out->pts = s->pts;
550  s->pts += av_rescale_q(nb_samples,
551  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
552  ret = ff_filter_frame(outlink, out);
553  }
554  s->crossfade_is_over = 1;
555  }
556 
557 fail:
558  av_frame_free(&in);
559  av_frame_free(&cf[0]);
560  av_frame_free(&cf[1]);
561  return ret;
562 }
563 
564 static int acrossfade_request_frame(AVFilterLink *outlink)
565 {
566  AVFilterContext *ctx = outlink->src;
567  AudioFadeContext *s = ctx->priv;
568  int ret = 0;
569 
570  if (!s->cf0_eof) {
571  AVFilterLink *cf0 = ctx->inputs[0];
572  ret = ff_request_frame(cf0);
573  if (ret < 0 && ret != AVERROR_EOF)
574  return ret;
575  if (ret == AVERROR_EOF) {
576  s->cf0_eof = 1;
577  ret = 0;
578  }
579  } else {
580  AVFilterLink *cf1 = ctx->inputs[1];
581  int nb_samples = av_audio_fifo_size(s->fifo[1]);
582 
583  ret = ff_request_frame(cf1);
584  if (ret == AVERROR_EOF && nb_samples > 0) {
585  AVFrame *out = ff_get_audio_buffer(outlink, nb_samples);
586  if (!out)
587  return AVERROR(ENOMEM);
588 
589  av_audio_fifo_read(s->fifo[1], (void **)out->extended_data, nb_samples);
590  ret = ff_filter_frame(outlink, out);
591  }
592  }
593 
594  return ret;
595 }
596 
597 static int acrossfade_config_output(AVFilterLink *outlink)
598 {
599  AVFilterContext *ctx = outlink->src;
600  AudioFadeContext *s = ctx->priv;
601 
602  if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
603  av_log(ctx, AV_LOG_ERROR,
604  "Inputs must have the same sample rate "
605  "%d for in0 vs %d for in1\n",
606  ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
607  return AVERROR(EINVAL);
608  }
609 
610  outlink->sample_rate = ctx->inputs[0]->sample_rate;
611  outlink->time_base = ctx->inputs[0]->time_base;
612  outlink->channel_layout = ctx->inputs[0]->channel_layout;
613  outlink->channels = ctx->inputs[0]->channels;
614  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
615 
616  switch (outlink->format) {
617  case AV_SAMPLE_FMT_DBL: s->crossfade_samples = crossfade_samples_dbl; break;
618  case AV_SAMPLE_FMT_DBLP: s->crossfade_samples = crossfade_samples_dblp; break;
619  case AV_SAMPLE_FMT_FLT: s->crossfade_samples = crossfade_samples_flt; break;
620  case AV_SAMPLE_FMT_FLTP: s->crossfade_samples = crossfade_samples_fltp; break;
621  case AV_SAMPLE_FMT_S16: s->crossfade_samples = crossfade_samples_s16; break;
622  case AV_SAMPLE_FMT_S16P: s->crossfade_samples = crossfade_samples_s16p; break;
623  case AV_SAMPLE_FMT_S32: s->crossfade_samples = crossfade_samples_s32; break;
624  case AV_SAMPLE_FMT_S32P: s->crossfade_samples = crossfade_samples_s32p; break;
625  }
626 
627  config_output(outlink);
628 
629  s->fifo[0] = av_audio_fifo_alloc(outlink->format, outlink->channels, s->nb_samples);
630  s->fifo[1] = av_audio_fifo_alloc(outlink->format, outlink->channels, s->nb_samples);
631  if (!s->fifo[0] || !s->fifo[1])
632  return AVERROR(ENOMEM);
633 
634  return 0;
635 }
636 
637 static av_cold void uninit(AVFilterContext *ctx)
638 {
639  AudioFadeContext *s = ctx->priv;
640 
641  av_audio_fifo_free(s->fifo[0]);
642  av_audio_fifo_free(s->fifo[1]);
643 }
644 
645 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
646  {
647  .name = "crossfade0",
648  .type = AVMEDIA_TYPE_AUDIO,
649  .filter_frame = acrossfade_filter_frame,
650  },
651  {
652  .name = "crossfade1",
653  .type = AVMEDIA_TYPE_AUDIO,
654  .filter_frame = acrossfade_filter_frame,
655  },
656  { NULL }
657 };
658 
659 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
660  {
661  .name = "default",
662  .type = AVMEDIA_TYPE_AUDIO,
663  .request_frame = acrossfade_request_frame,
664  .config_props = acrossfade_config_output,
665  },
666  { NULL }
667 };
668 
669 AVFilter ff_af_acrossfade = {
670  .name = "acrossfade",
671  .description = NULL_IF_CONFIG_SMALL("Cross fade two input audio streams."),
672  .query_formats = query_formats,
673  .priv_size = sizeof(AudioFadeContext),
674  .uninit = uninit,
675  .priv_class = &acrossfade_class,
676  .inputs = avfilter_af_acrossfade_inputs,
677  .outputs = avfilter_af_acrossfade_outputs,
678 };
679 
680 #endif /* CONFIG_ACROSSFADE_FILTER */
float, planar
Definition: samplefmt.h:70
#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:523
const char * s
Definition: avisynth_c.h:631
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:60
static double fade_gain(int curve, int64_t index, int range)
Definition: af_afade.c:93
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:158
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
Main libavfilter public API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
static int config_output(AVFilterLink *outlink)
Definition: af_afade.c:194
double, planar
Definition: samplefmt.h:71
static enum AVSampleFormat formats[]
Definition: af_afade.c:55
Definition: af_afade.c:55
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:451
const char * name
Pad name.
Definition: internal.h:69
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:641
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1158
static int64_t start_time
Definition: ffplay.c:325
uint8_t
#define av_cold
Definition: attributes.h:74
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:337
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:49
static int query_formats(AVFilterContext *ctx)
Definition: af_afade.c:60
#define FADE_PLANAR(name, type)
Definition: af_afade.c:150
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
void(* fade_samples)(uint8_t **dst, uint8_t *const *src, int nb_samples, int channels, int direction, int64_t start, int range, int curve)
Definition: af_afade.c:46
#define AVERROR_EOF
End of file.
Definition: error.h:55
static int64_t duration
Definition: ffplay.c:326
signed 32 bits
Definition: samplefmt.h:63
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:63
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:140
#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:542
static av_always_inline double cbrt(double x)
Definition: libm.h:52
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:235
CurveType
Definition: af_afade.c:55
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:74
#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:148
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
#define fail()
Definition: checkasm.h:57
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
Definition: af_afade.c:55
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:205
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:127
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:246
signed 32 bits, planar
Definition: samplefmt.h:69
int64_t start_sample
Definition: af_afade.c:37
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
Definition: af_afade.c:55
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: aeval.c:415
int32_t
Definition: af_afade.c:55
int64_t pts
Definition: af_afade.c:44
int crossfade_is_over
Definition: af_afade.c:42
Frame requests may need to loop in order to be fulfilled.
Definition: internal.h:374
int64_t duration
Definition: af_afade.c:38
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition: formats.c:385
A list of supported channel layouts.
Definition: formats.h:85
int nb_samples
number of samples currently in the FIFO
Definition: audio_fifo.c:37
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
AVS_Value src
Definition: avisynth_c.h:482
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:493
AVAudioFifo * fifo[2]
Definition: af_afade.c:43
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:553
Definition: af_afade.c:55
int64_t start_time
Definition: af_afade.c:39
GLint GLenum type
Definition: opengl_enc.c:105
Definition: af_afade.c:55
Describe the class of an AVClass context structure.
Definition: log.h:67
int av_frame_get_channels(const AVFrame *frame)
Filter definition.
Definition: avfilter.h:470
int index
Definition: gxfenc.c:89
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
rational number numerator/denominator
Definition: rational.h:43
Definition: af_afade.c:55
#define FLAGS
Definition: af_afade.c:58
const char * name
Filter name.
Definition: avfilter.h:474
Definition: af_afade.c:55
Definition: af_afade.c:55
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:379
static int flags
Definition: cpu.c:47
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:113
Definition: af_afade.c:55
Definition: af_afade.c:55
Definition: af_afade.c:55
signed 16 bits
Definition: samplefmt.h:62
#define OFFSET(x)
Definition: af_afade.c:57
Definition: af_afade.c:55
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
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-> out
An instance of a filter.
Definition: avfilter.h:633
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
void INT64 start
Definition: avisynth_c.h:553
signed 16 bits, planar
Definition: samplefmt.h:68
#define M_PI
Definition: mathematics.h:46
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:343
Definition: af_afade.c:55
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:530
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:553
#define FADE(name, type)
Definition: af_afade.c:168
Definition: af_afade.c:55