You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

686 lines
33KB

  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. * @file
  22. * fade audio filter
  23. */
  24. #include "libavutil/audio_fifo.h"
  25. #include "libavutil/opt.h"
  26. #include "audio.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. typedef struct {
  30. const AVClass *class;
  31. int type;
  32. int curve, curve2;
  33. int nb_samples;
  34. int64_t start_sample;
  35. int64_t duration;
  36. int64_t start_time;
  37. int overlap;
  38. int cf0_eof;
  39. int crossfade_is_over;
  40. AVAudioFifo *fifo[2];
  41. int64_t pts;
  42. void (*fade_samples)(uint8_t **dst, uint8_t * const *src,
  43. int nb_samples, int channels, int direction,
  44. int64_t start, int range, int curve);
  45. void (*crossfade_samples)(uint8_t **dst, uint8_t * const *cf0,
  46. uint8_t * const *cf1,
  47. int nb_samples, int channels,
  48. int curve0, int curve1);
  49. } AudioFadeContext;
  50. enum CurveType { TRI, QSIN, ESIN, HSIN, LOG, IPAR, QUA, CUB, SQU, CBR, PAR, EXP, IQSIN, IHSIN, DESE, DESI, NB_CURVES };
  51. #define OFFSET(x) offsetof(AudioFadeContext, x)
  52. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  53. static int query_formats(AVFilterContext *ctx)
  54. {
  55. AVFilterFormats *formats;
  56. AVFilterChannelLayouts *layouts;
  57. static const enum AVSampleFormat sample_fmts[] = {
  58. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
  59. AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
  60. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
  61. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  62. AV_SAMPLE_FMT_NONE
  63. };
  64. int ret;
  65. layouts = ff_all_channel_counts();
  66. if (!layouts)
  67. return AVERROR(ENOMEM);
  68. ret = ff_set_common_channel_layouts(ctx, layouts);
  69. if (ret < 0)
  70. return ret;
  71. formats = ff_make_format_list(sample_fmts);
  72. if (!formats)
  73. return AVERROR(ENOMEM);
  74. ret = ff_set_common_formats(ctx, formats);
  75. if (ret < 0)
  76. return ret;
  77. formats = ff_all_samplerates();
  78. if (!formats)
  79. return AVERROR(ENOMEM);
  80. return ff_set_common_samplerates(ctx, formats);
  81. }
  82. static double fade_gain(int curve, int64_t index, int range)
  83. {
  84. #define CUBE(a) ((a)*(a)*(a))
  85. double gain;
  86. gain = av_clipd(1.0 * index / range, 0, 1.0);
  87. switch (curve) {
  88. case QSIN:
  89. gain = sin(gain * M_PI / 2.0);
  90. break;
  91. case IQSIN:
  92. /* 0.6... = 2 / M_PI */
  93. gain = 0.6366197723675814 * asin(gain);
  94. break;
  95. case ESIN:
  96. gain = 1.0 - cos(M_PI / 4.0 * (CUBE(2.0*gain - 1) + 1));
  97. break;
  98. case HSIN:
  99. gain = (1.0 - cos(gain * M_PI)) / 2.0;
  100. break;
  101. case IHSIN:
  102. /* 0.3... = 1 / M_PI */
  103. gain = 0.3183098861837907 * acos(1 - 2 * gain);
  104. break;
  105. case EXP:
  106. /* -11.5... = 5*ln(0.1) */
  107. gain = exp(-11.512925464970227 * (1 - gain));
  108. break;
  109. case LOG:
  110. gain = av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
  111. break;
  112. case PAR:
  113. gain = 1 - sqrt(1 - gain);
  114. break;
  115. case IPAR:
  116. gain = (1 - (1 - gain) * (1 - gain));
  117. break;
  118. case QUA:
  119. gain *= gain;
  120. break;
  121. case CUB:
  122. gain = CUBE(gain);
  123. break;
  124. case SQU:
  125. gain = sqrt(gain);
  126. break;
  127. case CBR:
  128. gain = cbrt(gain);
  129. break;
  130. case DESE:
  131. gain = gain <= 0.5 ? cbrt(2 * gain) / 2: 1 - cbrt(2 * (1 - gain)) / 2;
  132. break;
  133. case DESI:
  134. gain = gain <= 0.5 ? CUBE(2 * gain) / 2: 1 - CUBE(2 * (1 - gain)) / 2;
  135. break;
  136. }
  137. return gain;
  138. }
  139. #define FADE_PLANAR(name, type) \
  140. static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
  141. int nb_samples, int channels, int dir, \
  142. int64_t start, int range, int curve) \
  143. { \
  144. int i, c; \
  145. \
  146. for (i = 0; i < nb_samples; i++) { \
  147. double gain = fade_gain(curve, start + i * dir, range); \
  148. for (c = 0; c < channels; c++) { \
  149. type *d = (type *)dst[c]; \
  150. const type *s = (type *)src[c]; \
  151. \
  152. d[i] = s[i] * gain; \
  153. } \
  154. } \
  155. }
  156. #define FADE(name, type) \
  157. static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
  158. int nb_samples, int channels, int dir, \
  159. int64_t start, int range, int curve) \
  160. { \
  161. type *d = (type *)dst[0]; \
  162. const type *s = (type *)src[0]; \
  163. int i, c, k = 0; \
  164. \
  165. for (i = 0; i < nb_samples; i++) { \
  166. double gain = fade_gain(curve, start + i * dir, range); \
  167. for (c = 0; c < channels; c++, k++) \
  168. d[k] = s[k] * gain; \
  169. } \
  170. }
  171. FADE_PLANAR(dbl, double)
  172. FADE_PLANAR(flt, float)
  173. FADE_PLANAR(s16, int16_t)
  174. FADE_PLANAR(s32, int32_t)
  175. FADE(dbl, double)
  176. FADE(flt, float)
  177. FADE(s16, int16_t)
  178. FADE(s32, int32_t)
  179. static int config_output(AVFilterLink *outlink)
  180. {
  181. AVFilterContext *ctx = outlink->src;
  182. AudioFadeContext *s = ctx->priv;
  183. switch (outlink->format) {
  184. case AV_SAMPLE_FMT_DBL: s->fade_samples = fade_samples_dbl; break;
  185. case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break;
  186. case AV_SAMPLE_FMT_FLT: s->fade_samples = fade_samples_flt; break;
  187. case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break;
  188. case AV_SAMPLE_FMT_S16: s->fade_samples = fade_samples_s16; break;
  189. case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break;
  190. case AV_SAMPLE_FMT_S32: s->fade_samples = fade_samples_s32; break;
  191. case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break;
  192. }
  193. if (s->duration)
  194. s->nb_samples = av_rescale(s->duration, outlink->sample_rate, AV_TIME_BASE);
  195. if (s->start_time)
  196. s->start_sample = av_rescale(s->start_time, outlink->sample_rate, AV_TIME_BASE);
  197. return 0;
  198. }
  199. #if CONFIG_AFADE_FILTER
  200. static const AVOption afade_options[] = {
  201. { "type", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
  202. { "t", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
  203. { "in", "fade-in", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "type" },
  204. { "out", "fade-out", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "type" },
  205. { "start_sample", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
  206. { "ss", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
  207. { "nb_samples", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
  208. { "ns", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
  209. { "start_time", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  210. { "st", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  211. { "duration", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  212. { "d", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  213. { "curve", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
  214. { "c", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
  215. { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
  216. { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
  217. { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
  218. { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
  219. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
  220. { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
  221. { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
  222. { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
  223. { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
  224. { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
  225. { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
  226. { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve" },
  227. { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
  228. { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
  229. { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve" },
  230. { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve" },
  231. { NULL }
  232. };
  233. AVFILTER_DEFINE_CLASS(afade);
  234. static av_cold int init(AVFilterContext *ctx)
  235. {
  236. AudioFadeContext *s = ctx->priv;
  237. if (INT64_MAX - s->nb_samples < s->start_sample)
  238. return AVERROR(EINVAL);
  239. return 0;
  240. }
  241. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  242. {
  243. AudioFadeContext *s = inlink->dst->priv;
  244. AVFilterLink *outlink = inlink->dst->outputs[0];
  245. int nb_samples = buf->nb_samples;
  246. AVFrame *out_buf;
  247. int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
  248. if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
  249. ( s->type && (cur_sample + nb_samples < s->start_sample)))
  250. return ff_filter_frame(outlink, buf);
  251. if (av_frame_is_writable(buf)) {
  252. out_buf = buf;
  253. } else {
  254. out_buf = ff_get_audio_buffer(inlink, nb_samples);
  255. if (!out_buf)
  256. return AVERROR(ENOMEM);
  257. av_frame_copy_props(out_buf, buf);
  258. }
  259. if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
  260. ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
  261. av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
  262. av_frame_get_channels(out_buf), out_buf->format);
  263. } else {
  264. int64_t start;
  265. if (!s->type)
  266. start = cur_sample - s->start_sample;
  267. else
  268. start = s->start_sample + s->nb_samples - cur_sample;
  269. s->fade_samples(out_buf->extended_data, buf->extended_data,
  270. nb_samples, av_frame_get_channels(buf),
  271. s->type ? -1 : 1, start,
  272. s->nb_samples, s->curve);
  273. }
  274. if (buf != out_buf)
  275. av_frame_free(&buf);
  276. return ff_filter_frame(outlink, out_buf);
  277. }
  278. static const AVFilterPad avfilter_af_afade_inputs[] = {
  279. {
  280. .name = "default",
  281. .type = AVMEDIA_TYPE_AUDIO,
  282. .filter_frame = filter_frame,
  283. },
  284. { NULL }
  285. };
  286. static const AVFilterPad avfilter_af_afade_outputs[] = {
  287. {
  288. .name = "default",
  289. .type = AVMEDIA_TYPE_AUDIO,
  290. .config_props = config_output,
  291. },
  292. { NULL }
  293. };
  294. AVFilter ff_af_afade = {
  295. .name = "afade",
  296. .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
  297. .query_formats = query_formats,
  298. .priv_size = sizeof(AudioFadeContext),
  299. .init = init,
  300. .inputs = avfilter_af_afade_inputs,
  301. .outputs = avfilter_af_afade_outputs,
  302. .priv_class = &afade_class,
  303. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  304. };
  305. #endif /* CONFIG_AFADE_FILTER */
  306. #if CONFIG_ACROSSFADE_FILTER
  307. static const AVOption acrossfade_options[] = {
  308. { "nb_samples", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
  309. { "ns", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
  310. { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60, FLAGS },
  311. { "d", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60, FLAGS },
  312. { "overlap", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
  313. { "o", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
  314. { "curve1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve1" },
  315. { "c1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve1" },
  316. { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve1" },
  317. { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve1" },
  318. { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve1" },
  319. { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve1" },
  320. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve1" },
  321. { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve1" },
  322. { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve1" },
  323. { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve1" },
  324. { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve1" },
  325. { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve1" },
  326. { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve1" },
  327. { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve1" },
  328. { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve1" },
  329. { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve1" },
  330. { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve1" },
  331. { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve1" },
  332. { "curve2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve2" },
  333. { "c2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve2" },
  334. { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve2" },
  335. { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve2" },
  336. { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve2" },
  337. { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve2" },
  338. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve2" },
  339. { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve2" },
  340. { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve2" },
  341. { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve2" },
  342. { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve2" },
  343. { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve2" },
  344. { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve2" },
  345. { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve2" },
  346. { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve2" },
  347. { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve2" },
  348. { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve2" },
  349. { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve2" },
  350. { NULL }
  351. };
  352. AVFILTER_DEFINE_CLASS(acrossfade);
  353. #define CROSSFADE_PLANAR(name, type) \
  354. static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
  355. uint8_t * const *cf1, \
  356. int nb_samples, int channels, \
  357. int curve0, int curve1) \
  358. { \
  359. int i, c; \
  360. \
  361. for (i = 0; i < nb_samples; i++) { \
  362. double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
  363. double gain1 = fade_gain(curve1, i, nb_samples); \
  364. for (c = 0; c < channels; c++) { \
  365. type *d = (type *)dst[c]; \
  366. const type *s0 = (type *)cf0[c]; \
  367. const type *s1 = (type *)cf1[c]; \
  368. \
  369. d[i] = s0[i] * gain0 + s1[i] * gain1; \
  370. } \
  371. } \
  372. }
  373. #define CROSSFADE(name, type) \
  374. static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
  375. uint8_t * const *cf1, \
  376. int nb_samples, int channels, \
  377. int curve0, int curve1) \
  378. { \
  379. type *d = (type *)dst[0]; \
  380. const type *s0 = (type *)cf0[0]; \
  381. const type *s1 = (type *)cf1[0]; \
  382. int i, c, k = 0; \
  383. \
  384. for (i = 0; i < nb_samples; i++) { \
  385. double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
  386. double gain1 = fade_gain(curve1, i, nb_samples); \
  387. for (c = 0; c < channels; c++, k++) \
  388. d[k] = s0[k] * gain0 + s1[k] * gain1; \
  389. } \
  390. }
  391. CROSSFADE_PLANAR(dbl, double)
  392. CROSSFADE_PLANAR(flt, float)
  393. CROSSFADE_PLANAR(s16, int16_t)
  394. CROSSFADE_PLANAR(s32, int32_t)
  395. CROSSFADE(dbl, double)
  396. CROSSFADE(flt, float)
  397. CROSSFADE(s16, int16_t)
  398. CROSSFADE(s32, int32_t)
  399. static int acrossfade_filter_frame(AVFilterLink *inlink, AVFrame *in)
  400. {
  401. AVFilterContext *ctx = inlink->dst;
  402. AudioFadeContext *s = ctx->priv;
  403. AVFilterLink *outlink = ctx->outputs[0];
  404. AVFrame *out, *cf[2] = { NULL };
  405. int ret = 0, nb_samples;
  406. if (s->crossfade_is_over) {
  407. in->pts = s->pts;
  408. s->pts += av_rescale_q(in->nb_samples,
  409. (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
  410. return ff_filter_frame(outlink, in);
  411. } else if (inlink == ctx->inputs[0]) {
  412. av_audio_fifo_write(s->fifo[0], (void **)in->extended_data, in->nb_samples);
  413. nb_samples = av_audio_fifo_size(s->fifo[0]) - s->nb_samples;
  414. if (nb_samples > 0) {
  415. out = ff_get_audio_buffer(outlink, nb_samples);
  416. if (!out) {
  417. ret = AVERROR(ENOMEM);
  418. goto fail;
  419. }
  420. av_audio_fifo_read(s->fifo[0], (void **)out->extended_data, nb_samples);
  421. out->pts = s->pts;
  422. s->pts += av_rescale_q(nb_samples,
  423. (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
  424. ret = ff_filter_frame(outlink, out);
  425. }
  426. } else if (av_audio_fifo_size(s->fifo[1]) < s->nb_samples) {
  427. if (!s->overlap && av_audio_fifo_size(s->fifo[0]) > 0) {
  428. nb_samples = av_audio_fifo_size(s->fifo[0]);
  429. cf[0] = ff_get_audio_buffer(outlink, nb_samples);
  430. out = ff_get_audio_buffer(outlink, nb_samples);
  431. if (!out || !cf[0]) {
  432. ret = AVERROR(ENOMEM);
  433. goto fail;
  434. }
  435. av_audio_fifo_read(s->fifo[0], (void **)cf[0]->extended_data, nb_samples);
  436. s->fade_samples(out->extended_data, cf[0]->extended_data, nb_samples,
  437. outlink->channels, -1, nb_samples - 1, nb_samples, s->curve);
  438. out->pts = s->pts;
  439. s->pts += av_rescale_q(nb_samples,
  440. (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
  441. ret = ff_filter_frame(outlink, out);
  442. if (ret < 0)
  443. goto fail;
  444. }
  445. av_audio_fifo_write(s->fifo[1], (void **)in->extended_data, in->nb_samples);
  446. } else if (av_audio_fifo_size(s->fifo[1]) >= s->nb_samples) {
  447. av_audio_fifo_write(s->fifo[1], (void **)in->extended_data, in->nb_samples);
  448. if (s->overlap) {
  449. cf[0] = ff_get_audio_buffer(outlink, s->nb_samples);
  450. cf[1] = ff_get_audio_buffer(outlink, s->nb_samples);
  451. out = ff_get_audio_buffer(outlink, s->nb_samples);
  452. if (!out || !cf[0] || !cf[1]) {
  453. av_frame_free(&out);
  454. ret = AVERROR(ENOMEM);
  455. goto fail;
  456. }
  457. av_audio_fifo_read(s->fifo[0], (void **)cf[0]->extended_data, s->nb_samples);
  458. av_audio_fifo_read(s->fifo[1], (void **)cf[1]->extended_data, s->nb_samples);
  459. s->crossfade_samples(out->extended_data, cf[0]->extended_data,
  460. cf[1]->extended_data,
  461. s->nb_samples, av_frame_get_channels(in),
  462. s->curve, s->curve2);
  463. out->pts = s->pts;
  464. s->pts += av_rescale_q(s->nb_samples,
  465. (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
  466. ret = ff_filter_frame(outlink, out);
  467. if (ret < 0)
  468. goto fail;
  469. } else {
  470. out = ff_get_audio_buffer(outlink, s->nb_samples);
  471. cf[1] = ff_get_audio_buffer(outlink, s->nb_samples);
  472. if (!out || !cf[1]) {
  473. ret = AVERROR(ENOMEM);
  474. av_frame_free(&out);
  475. goto fail;
  476. }
  477. av_audio_fifo_read(s->fifo[1], (void **)cf[1]->extended_data, s->nb_samples);
  478. s->fade_samples(out->extended_data, cf[1]->extended_data, s->nb_samples,
  479. outlink->channels, 1, 0, s->nb_samples, s->curve2);
  480. out->pts = s->pts;
  481. s->pts += av_rescale_q(s->nb_samples,
  482. (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
  483. ret = ff_filter_frame(outlink, out);
  484. if (ret < 0)
  485. goto fail;
  486. }
  487. nb_samples = av_audio_fifo_size(s->fifo[1]);
  488. if (nb_samples > 0) {
  489. out = ff_get_audio_buffer(outlink, nb_samples);
  490. if (!out) {
  491. ret = AVERROR(ENOMEM);
  492. goto fail;
  493. }
  494. av_audio_fifo_read(s->fifo[1], (void **)out->extended_data, nb_samples);
  495. out->pts = s->pts;
  496. s->pts += av_rescale_q(nb_samples,
  497. (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
  498. ret = ff_filter_frame(outlink, out);
  499. }
  500. s->crossfade_is_over = 1;
  501. }
  502. fail:
  503. av_frame_free(&in);
  504. av_frame_free(&cf[0]);
  505. av_frame_free(&cf[1]);
  506. return ret;
  507. }
  508. static int acrossfade_request_frame(AVFilterLink *outlink)
  509. {
  510. AVFilterContext *ctx = outlink->src;
  511. AudioFadeContext *s = ctx->priv;
  512. int ret = 0;
  513. if (!s->cf0_eof) {
  514. AVFilterLink *cf0 = ctx->inputs[0];
  515. ret = ff_request_frame(cf0);
  516. if (ret < 0 && ret != AVERROR_EOF)
  517. return ret;
  518. if (ret == AVERROR_EOF) {
  519. s->cf0_eof = 1;
  520. ret = 0;
  521. }
  522. } else {
  523. AVFilterLink *cf1 = ctx->inputs[1];
  524. int nb_samples = av_audio_fifo_size(s->fifo[1]);
  525. ret = ff_request_frame(cf1);
  526. if (ret == AVERROR_EOF && nb_samples > 0) {
  527. AVFrame *out = ff_get_audio_buffer(outlink, nb_samples);
  528. if (!out)
  529. return AVERROR(ENOMEM);
  530. av_audio_fifo_read(s->fifo[1], (void **)out->extended_data, nb_samples);
  531. ret = ff_filter_frame(outlink, out);
  532. }
  533. }
  534. return ret;
  535. }
  536. static int acrossfade_config_output(AVFilterLink *outlink)
  537. {
  538. AVFilterContext *ctx = outlink->src;
  539. AudioFadeContext *s = ctx->priv;
  540. if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
  541. av_log(ctx, AV_LOG_ERROR,
  542. "Inputs must have the same sample rate "
  543. "%d for in0 vs %d for in1\n",
  544. ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
  545. return AVERROR(EINVAL);
  546. }
  547. outlink->sample_rate = ctx->inputs[0]->sample_rate;
  548. outlink->time_base = ctx->inputs[0]->time_base;
  549. outlink->channel_layout = ctx->inputs[0]->channel_layout;
  550. outlink->channels = ctx->inputs[0]->channels;
  551. switch (outlink->format) {
  552. case AV_SAMPLE_FMT_DBL: s->crossfade_samples = crossfade_samples_dbl; break;
  553. case AV_SAMPLE_FMT_DBLP: s->crossfade_samples = crossfade_samples_dblp; break;
  554. case AV_SAMPLE_FMT_FLT: s->crossfade_samples = crossfade_samples_flt; break;
  555. case AV_SAMPLE_FMT_FLTP: s->crossfade_samples = crossfade_samples_fltp; break;
  556. case AV_SAMPLE_FMT_S16: s->crossfade_samples = crossfade_samples_s16; break;
  557. case AV_SAMPLE_FMT_S16P: s->crossfade_samples = crossfade_samples_s16p; break;
  558. case AV_SAMPLE_FMT_S32: s->crossfade_samples = crossfade_samples_s32; break;
  559. case AV_SAMPLE_FMT_S32P: s->crossfade_samples = crossfade_samples_s32p; break;
  560. }
  561. config_output(outlink);
  562. s->fifo[0] = av_audio_fifo_alloc(outlink->format, outlink->channels, s->nb_samples);
  563. s->fifo[1] = av_audio_fifo_alloc(outlink->format, outlink->channels, s->nb_samples);
  564. if (!s->fifo[0] || !s->fifo[1])
  565. return AVERROR(ENOMEM);
  566. return 0;
  567. }
  568. static av_cold void uninit(AVFilterContext *ctx)
  569. {
  570. AudioFadeContext *s = ctx->priv;
  571. av_audio_fifo_free(s->fifo[0]);
  572. av_audio_fifo_free(s->fifo[1]);
  573. }
  574. static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
  575. {
  576. .name = "crossfade0",
  577. .type = AVMEDIA_TYPE_AUDIO,
  578. .filter_frame = acrossfade_filter_frame,
  579. },
  580. {
  581. .name = "crossfade1",
  582. .type = AVMEDIA_TYPE_AUDIO,
  583. .filter_frame = acrossfade_filter_frame,
  584. },
  585. { NULL }
  586. };
  587. static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
  588. {
  589. .name = "default",
  590. .type = AVMEDIA_TYPE_AUDIO,
  591. .request_frame = acrossfade_request_frame,
  592. .config_props = acrossfade_config_output,
  593. },
  594. { NULL }
  595. };
  596. AVFilter ff_af_acrossfade = {
  597. .name = "acrossfade",
  598. .description = NULL_IF_CONFIG_SMALL("Cross fade two input audio streams."),
  599. .query_formats = query_formats,
  600. .priv_size = sizeof(AudioFadeContext),
  601. .uninit = uninit,
  602. .priv_class = &acrossfade_class,
  603. .inputs = avfilter_af_acrossfade_inputs,
  604. .outputs = avfilter_af_acrossfade_outputs,
  605. };
  606. #endif /* CONFIG_ACROSSFADE_FILTER */