af_afade.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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, PAR, QUA, CUB, SQU, CBR };
  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 }, TRI, CBR, FLAGS, "curve" },
  57. { "c", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, TRI, CBR, 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. { "par", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 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. { NULL }
  69. };
  70. AVFILTER_DEFINE_CLASS(afade);
  71. static av_cold int init(AVFilterContext *ctx)
  72. {
  73. AudioFadeContext *s = ctx->priv;
  74. if (INT64_MAX - s->nb_samples < s->start_sample)
  75. return AVERROR(EINVAL);
  76. return 0;
  77. }
  78. static int query_formats(AVFilterContext *ctx)
  79. {
  80. AVFilterFormats *formats;
  81. AVFilterChannelLayouts *layouts;
  82. static const enum AVSampleFormat sample_fmts[] = {
  83. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
  84. AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
  85. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
  86. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  87. AV_SAMPLE_FMT_NONE
  88. };
  89. layouts = ff_all_channel_layouts();
  90. if (!layouts)
  91. return AVERROR(ENOMEM);
  92. ff_set_common_channel_layouts(ctx, layouts);
  93. formats = ff_make_format_list(sample_fmts);
  94. if (!formats)
  95. return AVERROR(ENOMEM);
  96. ff_set_common_formats(ctx, formats);
  97. formats = ff_all_samplerates();
  98. if (!formats)
  99. return AVERROR(ENOMEM);
  100. ff_set_common_samplerates(ctx, formats);
  101. return 0;
  102. }
  103. static double fade_gain(int curve, int64_t index, int range)
  104. {
  105. double gain;
  106. gain = FFMAX(0.0, FFMIN(1.0, 1.0 * index / range));
  107. switch (curve) {
  108. case QSIN:
  109. gain = sin(gain * M_PI / 2.0);
  110. break;
  111. case ESIN:
  112. gain = 1.0 - cos(M_PI / 4.0 * (pow(2.0*gain - 1, 3) + 1));
  113. break;
  114. case HSIN:
  115. gain = (1.0 - cos(gain * M_PI)) / 2.0;
  116. break;
  117. case LOG:
  118. gain = pow(0.1, (1 - gain) * 5.0);
  119. break;
  120. case PAR:
  121. gain = (1 - (1 - gain) * (1 - gain));
  122. break;
  123. case QUA:
  124. gain *= gain;
  125. break;
  126. case CUB:
  127. gain = gain * gain * gain;
  128. break;
  129. case SQU:
  130. gain = sqrt(gain);
  131. break;
  132. case CBR:
  133. gain = cbrt(gain);
  134. break;
  135. }
  136. return gain;
  137. }
  138. #define FADE_PLANAR(name, type) \
  139. static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
  140. int nb_samples, int channels, int dir, \
  141. int64_t start, int range, int curve) \
  142. { \
  143. int i, c; \
  144. \
  145. for (i = 0; i < nb_samples; i++) { \
  146. double gain = fade_gain(curve, start + i * dir, range); \
  147. for (c = 0; c < channels; c++) { \
  148. type *d = (type *)dst[c]; \
  149. const type *s = (type *)src[c]; \
  150. \
  151. d[i] = s[i] * gain; \
  152. } \
  153. } \
  154. }
  155. #define FADE(name, type) \
  156. static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
  157. int nb_samples, int channels, int dir, \
  158. int64_t start, int range, int curve) \
  159. { \
  160. type *d = (type *)dst[0]; \
  161. const type *s = (type *)src[0]; \
  162. int i, c, k = 0; \
  163. \
  164. for (i = 0; i < nb_samples; i++) { \
  165. double gain = fade_gain(curve, start + i * dir, range); \
  166. for (c = 0; c < channels; c++, k++) \
  167. d[k] = s[k] * gain; \
  168. } \
  169. }
  170. FADE_PLANAR(dbl, double)
  171. FADE_PLANAR(flt, float)
  172. FADE_PLANAR(s16, int16_t)
  173. FADE_PLANAR(s32, int32_t)
  174. FADE(dbl, double)
  175. FADE(flt, float)
  176. FADE(s16, int16_t)
  177. FADE(s32, int32_t)
  178. static int config_input(AVFilterLink *inlink)
  179. {
  180. AVFilterContext *ctx = inlink->dst;
  181. AudioFadeContext *s = ctx->priv;
  182. switch (inlink->format) {
  183. case AV_SAMPLE_FMT_DBL: s->fade_samples = fade_samples_dbl; break;
  184. case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break;
  185. case AV_SAMPLE_FMT_FLT: s->fade_samples = fade_samples_flt; break;
  186. case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break;
  187. case AV_SAMPLE_FMT_S16: s->fade_samples = fade_samples_s16; break;
  188. case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break;
  189. case AV_SAMPLE_FMT_S32: s->fade_samples = fade_samples_s32; break;
  190. case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break;
  191. }
  192. if (s->duration)
  193. s->nb_samples = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE);
  194. if (s->start_time)
  195. s->start_sample = av_rescale(s->start_time, inlink->sample_rate, AV_TIME_BASE);
  196. return 0;
  197. }
  198. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  199. {
  200. AudioFadeContext *s = inlink->dst->priv;
  201. AVFilterLink *outlink = inlink->dst->outputs[0];
  202. int nb_samples = buf->nb_samples;
  203. AVFrame *out_buf;
  204. int64_t cur_sample = av_rescale_q(buf->pts, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  205. if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
  206. ( s->type && (cur_sample + s->nb_samples < s->start_sample)))
  207. return ff_filter_frame(outlink, buf);
  208. if (av_frame_is_writable(buf)) {
  209. out_buf = buf;
  210. } else {
  211. out_buf = ff_get_audio_buffer(inlink, nb_samples);
  212. if (!out_buf)
  213. return AVERROR(ENOMEM);
  214. av_frame_copy_props(out_buf, buf);
  215. }
  216. if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
  217. ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
  218. av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
  219. av_frame_get_channels(out_buf), out_buf->format);
  220. } else {
  221. int64_t start;
  222. if (!s->type)
  223. start = cur_sample - s->start_sample;
  224. else
  225. start = s->start_sample + s->nb_samples - cur_sample;
  226. s->fade_samples(out_buf->extended_data, buf->extended_data,
  227. nb_samples, av_frame_get_channels(buf),
  228. s->type ? -1 : 1, start,
  229. s->nb_samples, s->curve);
  230. }
  231. if (buf != out_buf)
  232. av_frame_free(&buf);
  233. return ff_filter_frame(outlink, out_buf);
  234. }
  235. static const AVFilterPad avfilter_af_afade_inputs[] = {
  236. {
  237. .name = "default",
  238. .type = AVMEDIA_TYPE_AUDIO,
  239. .filter_frame = filter_frame,
  240. .config_props = config_input,
  241. },
  242. { NULL }
  243. };
  244. static const AVFilterPad avfilter_af_afade_outputs[] = {
  245. {
  246. .name = "default",
  247. .type = AVMEDIA_TYPE_AUDIO,
  248. },
  249. { NULL }
  250. };
  251. AVFilter ff_af_afade = {
  252. .name = "afade",
  253. .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
  254. .query_formats = query_formats,
  255. .priv_size = sizeof(AudioFadeContext),
  256. .init = init,
  257. .inputs = avfilter_af_afade_inputs,
  258. .outputs = avfilter_af_afade_outputs,
  259. .priv_class = &afade_class,
  260. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  261. };