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.

328 lines
14KB

  1. /*
  2. * Copyright (c) 2013 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 "internal.h"
  28. typedef struct {
  29. const AVClass *class;
  30. int type;
  31. int curve;
  32. int nb_samples;
  33. int64_t start_sample;
  34. int64_t duration;
  35. int64_t start_time;
  36. void (*fade_samples)(uint8_t **dst, uint8_t * const *src,
  37. int nb_samples, int channels, int direction,
  38. int64_t start, int range, int curve);
  39. } AudioFadeContext;
  40. enum CurveType { TRI, QSIN, ESIN, HSIN, LOG, IPAR, QUA, CUB, SQU, CBR, PAR, EXP, IQSIN, IHSIN, DESE, DESI, NB_CURVES };
  41. #define OFFSET(x) offsetof(AudioFadeContext, x)
  42. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  43. static const AVOption afade_options[] = {
  44. { "type", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
  45. { "t", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
  46. { "in", "fade-in", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "type" },
  47. { "out", "fade-out", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "type" },
  48. { "start_sample", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
  49. { "ss", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
  50. { "nb_samples", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
  51. { "ns", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
  52. { "start_time", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  53. { "st", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  54. { "duration", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  55. { "d", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
  56. { "curve", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
  57. { "c", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
  58. { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
  59. { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
  60. { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
  61. { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
  62. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
  63. { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
  64. { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
  65. { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
  66. { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
  67. { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
  68. { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
  69. { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve" },
  70. { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
  71. { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
  72. { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve" },
  73. { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve" },
  74. { NULL }
  75. };
  76. AVFILTER_DEFINE_CLASS(afade);
  77. static av_cold int init(AVFilterContext *ctx)
  78. {
  79. AudioFadeContext *s = ctx->priv;
  80. if (INT64_MAX - s->nb_samples < s->start_sample)
  81. return AVERROR(EINVAL);
  82. return 0;
  83. }
  84. static int query_formats(AVFilterContext *ctx)
  85. {
  86. AVFilterFormats *formats;
  87. AVFilterChannelLayouts *layouts;
  88. static const enum AVSampleFormat sample_fmts[] = {
  89. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
  90. AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
  91. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
  92. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  93. AV_SAMPLE_FMT_NONE
  94. };
  95. int ret;
  96. layouts = ff_all_channel_layouts();
  97. if (!layouts)
  98. return AVERROR(ENOMEM);
  99. ret = ff_set_common_channel_layouts(ctx, layouts);
  100. if (ret < 0)
  101. return ret;
  102. formats = ff_make_format_list(sample_fmts);
  103. if (!formats)
  104. return AVERROR(ENOMEM);
  105. ret = ff_set_common_formats(ctx, formats);
  106. if (ret < 0)
  107. return ret;
  108. formats = ff_all_samplerates();
  109. if (!formats)
  110. return AVERROR(ENOMEM);
  111. return ff_set_common_samplerates(ctx, formats);
  112. }
  113. static double fade_gain(int curve, int64_t index, int range)
  114. {
  115. double gain;
  116. gain = av_clipd(1.0 * index / range, 0, 1.0);
  117. switch (curve) {
  118. case QSIN:
  119. gain = sin(gain * M_PI / 2.0);
  120. break;
  121. case IQSIN:
  122. gain = 0.636943 * asin(gain);
  123. break;
  124. case ESIN:
  125. gain = 1.0 - cos(M_PI / 4.0 * (pow(2.0*gain - 1, 3) + 1));
  126. break;
  127. case HSIN:
  128. gain = (1.0 - cos(gain * M_PI)) / 2.0;
  129. break;
  130. case IHSIN:
  131. gain = 0.318471 * acos(1 - 2 * gain);
  132. break;
  133. case EXP:
  134. gain = pow(0.1, (1 - gain) * 5.0);
  135. break;
  136. case LOG:
  137. gain = av_clipd(0.0868589 * log(100000 * gain), 0, 1.0);
  138. break;
  139. case PAR:
  140. gain = 1 - sqrt(1 - gain);
  141. break;
  142. case IPAR:
  143. gain = (1 - (1 - gain) * (1 - gain));
  144. break;
  145. case QUA:
  146. gain *= gain;
  147. break;
  148. case CUB:
  149. gain = gain * gain * gain;
  150. break;
  151. case SQU:
  152. gain = sqrt(gain);
  153. break;
  154. case CBR:
  155. gain = cbrt(gain);
  156. break;
  157. case DESE:
  158. gain = gain <= 0.5 ? pow(2 * gain, 1/3.) / 2: 1 - pow(2 * (1 - gain), 1/3.) / 2;
  159. break;
  160. case DESI:
  161. gain = gain <= 0.5 ? pow(2 * gain, 3) / 2: 1 - pow(2 * (1 - gain), 3) / 2;
  162. break;
  163. }
  164. return gain;
  165. }
  166. #define FADE_PLANAR(name, type) \
  167. static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
  168. int nb_samples, int channels, int dir, \
  169. int64_t start, int range, int curve) \
  170. { \
  171. int i, c; \
  172. \
  173. for (i = 0; i < nb_samples; i++) { \
  174. double gain = fade_gain(curve, start + i * dir, range); \
  175. for (c = 0; c < channels; c++) { \
  176. type *d = (type *)dst[c]; \
  177. const type *s = (type *)src[c]; \
  178. \
  179. d[i] = s[i] * gain; \
  180. } \
  181. } \
  182. }
  183. #define FADE(name, type) \
  184. static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
  185. int nb_samples, int channels, int dir, \
  186. int64_t start, int range, int curve) \
  187. { \
  188. type *d = (type *)dst[0]; \
  189. const type *s = (type *)src[0]; \
  190. int i, c, k = 0; \
  191. \
  192. for (i = 0; i < nb_samples; i++) { \
  193. double gain = fade_gain(curve, start + i * dir, range); \
  194. for (c = 0; c < channels; c++, k++) \
  195. d[k] = s[k] * gain; \
  196. } \
  197. }
  198. FADE_PLANAR(dbl, double)
  199. FADE_PLANAR(flt, float)
  200. FADE_PLANAR(s16, int16_t)
  201. FADE_PLANAR(s32, int32_t)
  202. FADE(dbl, double)
  203. FADE(flt, float)
  204. FADE(s16, int16_t)
  205. FADE(s32, int32_t)
  206. static int config_input(AVFilterLink *inlink)
  207. {
  208. AVFilterContext *ctx = inlink->dst;
  209. AudioFadeContext *s = ctx->priv;
  210. switch (inlink->format) {
  211. case AV_SAMPLE_FMT_DBL: s->fade_samples = fade_samples_dbl; break;
  212. case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break;
  213. case AV_SAMPLE_FMT_FLT: s->fade_samples = fade_samples_flt; break;
  214. case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break;
  215. case AV_SAMPLE_FMT_S16: s->fade_samples = fade_samples_s16; break;
  216. case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break;
  217. case AV_SAMPLE_FMT_S32: s->fade_samples = fade_samples_s32; break;
  218. case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break;
  219. }
  220. if (s->duration)
  221. s->nb_samples = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE);
  222. if (s->start_time)
  223. s->start_sample = av_rescale(s->start_time, inlink->sample_rate, AV_TIME_BASE);
  224. return 0;
  225. }
  226. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  227. {
  228. AudioFadeContext *s = inlink->dst->priv;
  229. AVFilterLink *outlink = inlink->dst->outputs[0];
  230. int nb_samples = buf->nb_samples;
  231. AVFrame *out_buf;
  232. int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
  233. if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
  234. ( s->type && (cur_sample + s->nb_samples < s->start_sample)))
  235. return ff_filter_frame(outlink, buf);
  236. if (av_frame_is_writable(buf)) {
  237. out_buf = buf;
  238. } else {
  239. out_buf = ff_get_audio_buffer(inlink, nb_samples);
  240. if (!out_buf)
  241. return AVERROR(ENOMEM);
  242. av_frame_copy_props(out_buf, buf);
  243. }
  244. if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
  245. ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
  246. av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
  247. av_frame_get_channels(out_buf), out_buf->format);
  248. } else {
  249. int64_t start;
  250. if (!s->type)
  251. start = cur_sample - s->start_sample;
  252. else
  253. start = s->start_sample + s->nb_samples - cur_sample;
  254. s->fade_samples(out_buf->extended_data, buf->extended_data,
  255. nb_samples, av_frame_get_channels(buf),
  256. s->type ? -1 : 1, start,
  257. s->nb_samples, s->curve);
  258. }
  259. if (buf != out_buf)
  260. av_frame_free(&buf);
  261. return ff_filter_frame(outlink, out_buf);
  262. }
  263. static const AVFilterPad avfilter_af_afade_inputs[] = {
  264. {
  265. .name = "default",
  266. .type = AVMEDIA_TYPE_AUDIO,
  267. .filter_frame = filter_frame,
  268. .config_props = config_input,
  269. },
  270. { NULL }
  271. };
  272. static const AVFilterPad avfilter_af_afade_outputs[] = {
  273. {
  274. .name = "default",
  275. .type = AVMEDIA_TYPE_AUDIO,
  276. },
  277. { NULL }
  278. };
  279. AVFilter ff_af_afade = {
  280. .name = "afade",
  281. .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
  282. .query_formats = query_formats,
  283. .priv_size = sizeof(AudioFadeContext),
  284. .init = init,
  285. .inputs = avfilter_af_afade_inputs,
  286. .outputs = avfilter_af_afade_outputs,
  287. .priv_class = &afade_class,
  288. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  289. };