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.

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