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.

652 lines
31KB

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