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.

635 lines
29KB

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