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.

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