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.

668 lines
31KB

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