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.

641 lines
30KB

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