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.

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