af_afade.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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, "curve" },
  315. { "c1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
  316. { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
  317. { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
  318. { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
  319. { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
  320. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
  321. { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
  322. { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
  323. { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
  324. { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
  325. { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
  326. { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
  327. { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve" },
  328. { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
  329. { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
  330. { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve" },
  331. { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve" },
  332. { "curve2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
  333. { "c2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
  334. { NULL }
  335. };
  336. AVFILTER_DEFINE_CLASS(acrossfade);
  337. #define CROSSFADE_PLANAR(name, type) \
  338. static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
  339. uint8_t * const *cf1, \
  340. int nb_samples, int channels, \
  341. int curve0, int curve1) \
  342. { \
  343. int i, c; \
  344. \
  345. for (i = 0; i < nb_samples; i++) { \
  346. double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
  347. double gain1 = fade_gain(curve1, i, nb_samples); \
  348. for (c = 0; c < channels; c++) { \
  349. type *d = (type *)dst[c]; \
  350. const type *s0 = (type *)cf0[c]; \
  351. const type *s1 = (type *)cf1[c]; \
  352. \
  353. d[i] = s0[i] * gain0 + s1[i] * gain1; \
  354. } \
  355. } \
  356. }
  357. #define CROSSFADE(name, type) \
  358. static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
  359. uint8_t * const *cf1, \
  360. int nb_samples, int channels, \
  361. int curve0, int curve1) \
  362. { \
  363. type *d = (type *)dst[0]; \
  364. const type *s0 = (type *)cf0[0]; \
  365. const type *s1 = (type *)cf1[0]; \
  366. int i, c, k = 0; \
  367. \
  368. for (i = 0; i < nb_samples; i++) { \
  369. double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
  370. double gain1 = fade_gain(curve1, i, nb_samples); \
  371. for (c = 0; c < channels; c++, k++) \
  372. d[k] = s0[k] * gain0 + s1[k] * gain1; \
  373. } \
  374. }
  375. CROSSFADE_PLANAR(dbl, double)
  376. CROSSFADE_PLANAR(flt, float)
  377. CROSSFADE_PLANAR(s16, int16_t)
  378. CROSSFADE_PLANAR(s32, int32_t)
  379. CROSSFADE(dbl, double)
  380. CROSSFADE(flt, float)
  381. CROSSFADE(s16, int16_t)
  382. CROSSFADE(s32, int32_t)
  383. static int acrossfade_filter_frame(AVFilterLink *inlink, AVFrame *in)
  384. {
  385. AVFilterContext *ctx = inlink->dst;
  386. AudioFadeContext *s = ctx->priv;
  387. AVFilterLink *outlink = ctx->outputs[0];
  388. AVFrame *out, *cf[2] = { NULL };
  389. int ret = 0, nb_samples;
  390. if (s->crossfade_is_over) {
  391. in->pts = s->pts;
  392. s->pts += av_rescale_q(in->nb_samples,
  393. (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
  394. return ff_filter_frame(outlink, in);
  395. } else if (inlink == ctx->inputs[0]) {
  396. av_audio_fifo_write(s->fifo[0], (void **)in->extended_data, in->nb_samples);
  397. nb_samples = av_audio_fifo_size(s->fifo[0]) - s->nb_samples;
  398. if (nb_samples > 0) {
  399. out = ff_get_audio_buffer(outlink, nb_samples);
  400. if (!out) {
  401. ret = AVERROR(ENOMEM);
  402. goto fail;
  403. }
  404. av_audio_fifo_read(s->fifo[0], (void **)out->extended_data, nb_samples);
  405. out->pts = s->pts;
  406. s->pts += av_rescale_q(nb_samples,
  407. (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
  408. ret = ff_filter_frame(outlink, out);
  409. }
  410. } else if (av_audio_fifo_size(s->fifo[1]) < s->nb_samples) {
  411. if (!s->overlap && av_audio_fifo_size(s->fifo[0]) > 0) {
  412. nb_samples = av_audio_fifo_size(s->fifo[0]);
  413. cf[0] = ff_get_audio_buffer(outlink, nb_samples);
  414. out = ff_get_audio_buffer(outlink, nb_samples);
  415. if (!out || !cf[0]) {
  416. ret = AVERROR(ENOMEM);
  417. goto fail;
  418. }
  419. av_audio_fifo_read(s->fifo[0], (void **)cf[0]->extended_data, nb_samples);
  420. s->fade_samples(out->extended_data, cf[0]->extended_data, nb_samples,
  421. outlink->channels, -1, nb_samples - 1, nb_samples, s->curve);
  422. out->pts = s->pts;
  423. s->pts += av_rescale_q(nb_samples,
  424. (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
  425. ret = ff_filter_frame(outlink, out);
  426. if (ret < 0)
  427. goto fail;
  428. }
  429. av_audio_fifo_write(s->fifo[1], (void **)in->extended_data, in->nb_samples);
  430. } else if (av_audio_fifo_size(s->fifo[1]) >= s->nb_samples) {
  431. av_audio_fifo_write(s->fifo[1], (void **)in->extended_data, in->nb_samples);
  432. if (s->overlap) {
  433. cf[0] = ff_get_audio_buffer(outlink, s->nb_samples);
  434. cf[1] = ff_get_audio_buffer(outlink, s->nb_samples);
  435. out = ff_get_audio_buffer(outlink, s->nb_samples);
  436. if (!out || !cf[0] || !cf[1]) {
  437. av_frame_free(&out);
  438. ret = AVERROR(ENOMEM);
  439. goto fail;
  440. }
  441. av_audio_fifo_read(s->fifo[0], (void **)cf[0]->extended_data, s->nb_samples);
  442. av_audio_fifo_read(s->fifo[1], (void **)cf[1]->extended_data, s->nb_samples);
  443. s->crossfade_samples(out->extended_data, cf[0]->extended_data,
  444. cf[1]->extended_data,
  445. s->nb_samples, av_frame_get_channels(in),
  446. s->curve, s->curve2);
  447. out->pts = s->pts;
  448. s->pts += av_rescale_q(s->nb_samples,
  449. (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
  450. ret = ff_filter_frame(outlink, out);
  451. if (ret < 0)
  452. goto fail;
  453. } else {
  454. out = ff_get_audio_buffer(outlink, s->nb_samples);
  455. cf[1] = ff_get_audio_buffer(outlink, s->nb_samples);
  456. if (!out || !cf[1]) {
  457. ret = AVERROR(ENOMEM);
  458. av_frame_free(&out);
  459. goto fail;
  460. }
  461. av_audio_fifo_read(s->fifo[1], (void **)cf[1]->extended_data, s->nb_samples);
  462. s->fade_samples(out->extended_data, cf[1]->extended_data, s->nb_samples,
  463. outlink->channels, 1, 0, s->nb_samples, s->curve2);
  464. out->pts = s->pts;
  465. s->pts += av_rescale_q(s->nb_samples,
  466. (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
  467. ret = ff_filter_frame(outlink, out);
  468. if (ret < 0)
  469. goto fail;
  470. }
  471. nb_samples = av_audio_fifo_size(s->fifo[1]);
  472. if (nb_samples > 0) {
  473. out = ff_get_audio_buffer(outlink, nb_samples);
  474. if (!out) {
  475. ret = AVERROR(ENOMEM);
  476. goto fail;
  477. }
  478. av_audio_fifo_read(s->fifo[1], (void **)out->extended_data, nb_samples);
  479. out->pts = s->pts;
  480. s->pts += av_rescale_q(nb_samples,
  481. (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
  482. ret = ff_filter_frame(outlink, out);
  483. }
  484. s->crossfade_is_over = 1;
  485. }
  486. fail:
  487. av_frame_free(&in);
  488. av_frame_free(&cf[0]);
  489. av_frame_free(&cf[1]);
  490. return ret;
  491. }
  492. static int acrossfade_request_frame(AVFilterLink *outlink)
  493. {
  494. AVFilterContext *ctx = outlink->src;
  495. AudioFadeContext *s = ctx->priv;
  496. int ret = 0;
  497. if (!s->cf0_eof) {
  498. AVFilterLink *cf0 = ctx->inputs[0];
  499. ret = ff_request_frame(cf0);
  500. if (ret < 0 && ret != AVERROR_EOF)
  501. return ret;
  502. if (ret == AVERROR_EOF) {
  503. s->cf0_eof = 1;
  504. ret = 0;
  505. }
  506. } else {
  507. AVFilterLink *cf1 = ctx->inputs[1];
  508. int nb_samples = av_audio_fifo_size(s->fifo[1]);
  509. ret = ff_request_frame(cf1);
  510. if (ret == AVERROR_EOF && nb_samples > 0) {
  511. AVFrame *out = ff_get_audio_buffer(outlink, nb_samples);
  512. if (!out)
  513. return AVERROR(ENOMEM);
  514. av_audio_fifo_read(s->fifo[1], (void **)out->extended_data, nb_samples);
  515. ret = ff_filter_frame(outlink, out);
  516. }
  517. }
  518. return ret;
  519. }
  520. static int acrossfade_config_output(AVFilterLink *outlink)
  521. {
  522. AVFilterContext *ctx = outlink->src;
  523. AudioFadeContext *s = ctx->priv;
  524. if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
  525. av_log(ctx, AV_LOG_ERROR,
  526. "Inputs must have the same sample rate "
  527. "%d for in0 vs %d for in1\n",
  528. ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
  529. return AVERROR(EINVAL);
  530. }
  531. outlink->sample_rate = ctx->inputs[0]->sample_rate;
  532. outlink->time_base = ctx->inputs[0]->time_base;
  533. outlink->channel_layout = ctx->inputs[0]->channel_layout;
  534. outlink->channels = ctx->inputs[0]->channels;
  535. switch (outlink->format) {
  536. case AV_SAMPLE_FMT_DBL: s->crossfade_samples = crossfade_samples_dbl; break;
  537. case AV_SAMPLE_FMT_DBLP: s->crossfade_samples = crossfade_samples_dblp; break;
  538. case AV_SAMPLE_FMT_FLT: s->crossfade_samples = crossfade_samples_flt; break;
  539. case AV_SAMPLE_FMT_FLTP: s->crossfade_samples = crossfade_samples_fltp; break;
  540. case AV_SAMPLE_FMT_S16: s->crossfade_samples = crossfade_samples_s16; break;
  541. case AV_SAMPLE_FMT_S16P: s->crossfade_samples = crossfade_samples_s16p; break;
  542. case AV_SAMPLE_FMT_S32: s->crossfade_samples = crossfade_samples_s32; break;
  543. case AV_SAMPLE_FMT_S32P: s->crossfade_samples = crossfade_samples_s32p; break;
  544. }
  545. config_output(outlink);
  546. s->fifo[0] = av_audio_fifo_alloc(outlink->format, outlink->channels, s->nb_samples);
  547. s->fifo[1] = av_audio_fifo_alloc(outlink->format, outlink->channels, s->nb_samples);
  548. if (!s->fifo[0] || !s->fifo[1])
  549. return AVERROR(ENOMEM);
  550. return 0;
  551. }
  552. static av_cold void uninit(AVFilterContext *ctx)
  553. {
  554. AudioFadeContext *s = ctx->priv;
  555. av_audio_fifo_free(s->fifo[0]);
  556. av_audio_fifo_free(s->fifo[1]);
  557. }
  558. static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
  559. {
  560. .name = "crossfade0",
  561. .type = AVMEDIA_TYPE_AUDIO,
  562. .filter_frame = acrossfade_filter_frame,
  563. },
  564. {
  565. .name = "crossfade1",
  566. .type = AVMEDIA_TYPE_AUDIO,
  567. .filter_frame = acrossfade_filter_frame,
  568. },
  569. { NULL }
  570. };
  571. static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
  572. {
  573. .name = "default",
  574. .type = AVMEDIA_TYPE_AUDIO,
  575. .request_frame = acrossfade_request_frame,
  576. .config_props = acrossfade_config_output,
  577. },
  578. { NULL }
  579. };
  580. AVFilter ff_af_acrossfade = {
  581. .name = "acrossfade",
  582. .description = NULL_IF_CONFIG_SMALL("Cross fade two input audio streams."),
  583. .query_formats = query_formats,
  584. .priv_size = sizeof(AudioFadeContext),
  585. .uninit = uninit,
  586. .priv_class = &acrossfade_class,
  587. .inputs = avfilter_af_acrossfade_inputs,
  588. .outputs = avfilter_af_acrossfade_outputs,
  589. };
  590. #endif /* CONFIG_ACROSSFADE_FILTER */