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.

308 lines
13KB

  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. double duration;
  35. double 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", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "type" },
  47. { "out", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "type" },
  48. { "start_sample", "set expression of sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
  49. { "ss", "set expression of sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
  50. { "nb_samples", "set expression for fade duration in samples", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
  51. { "ns", "set expression for fade duration in samples", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
  52. { "start_time", "set expression of second to start fading", OFFSET(start_time), AV_OPT_TYPE_DOUBLE, {.dbl = 0. }, 0, 7*24*60*60,FLAGS },
  53. { "st", "set expression of second to start fading", OFFSET(start_time), AV_OPT_TYPE_DOUBLE, {.dbl = 0. }, 0, 7*24*60*60,FLAGS },
  54. { "duration", "set expression for fade duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl = 0. }, 0, 24*60*60, FLAGS },
  55. { "d", "set expression for fade duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl = 0. }, 0, 24*60*60, FLAGS },
  56. { "curve", "set expression for fade curve", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, TRI, CBR, FLAGS, "curve" },
  57. { "c", "set expression for fade curve", 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, const char *args)
  72. {
  73. AudioFadeContext *afade = ctx->priv;
  74. int ret;
  75. afade->class = &afade_class;
  76. av_opt_set_defaults(afade);
  77. if ((ret = av_set_options_string(afade, args, "=", ":")) < 0)
  78. return ret;
  79. if (INT64_MAX - afade->nb_samples < afade->start_sample)
  80. return AVERROR(EINVAL);
  81. return 0;
  82. }
  83. static int query_formats(AVFilterContext *ctx)
  84. {
  85. AVFilterFormats *formats;
  86. AVFilterChannelLayouts *layouts;
  87. static const enum AVSampleFormat sample_fmts[] = {
  88. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
  89. AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
  90. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
  91. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  92. AV_SAMPLE_FMT_NONE
  93. };
  94. layouts = ff_all_channel_layouts();
  95. if (!layouts)
  96. return AVERROR(ENOMEM);
  97. ff_set_common_channel_layouts(ctx, layouts);
  98. formats = ff_make_format_list(sample_fmts);
  99. if (!formats)
  100. return AVERROR(ENOMEM);
  101. ff_set_common_formats(ctx, formats);
  102. formats = ff_all_samplerates();
  103. if (!formats)
  104. return AVERROR(ENOMEM);
  105. ff_set_common_samplerates(ctx, formats);
  106. return 0;
  107. }
  108. static double fade_gain(int curve, int64_t index, int range)
  109. {
  110. double gain;
  111. gain = FFMAX(0.0, FFMIN(1.0, 1.0 * index / range));
  112. switch (curve) {
  113. case QSIN:
  114. gain = sin(gain * M_PI / 2.0);
  115. break;
  116. case ESIN:
  117. gain = 1.0 - cos(M_PI / 4.0 * (pow(2.0*gain - 1, 3) + 1));
  118. break;
  119. case HSIN:
  120. gain = (1.0 - cos(gain * M_PI)) / 2.0;
  121. break;
  122. case LOG:
  123. gain = pow(0.1, (1 - gain) * 5.0);
  124. break;
  125. case PAR:
  126. gain = (1 - (1 - gain) * (1 - gain));
  127. break;
  128. case QUA:
  129. gain *= gain;
  130. break;
  131. case CUB:
  132. gain = gain * gain * gain;
  133. break;
  134. case SQU:
  135. gain = sqrt(gain);
  136. break;
  137. case CBR:
  138. gain = cbrt(gain);
  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, int 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, int 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 *afade = ctx->priv;
  187. AVFilterLink *inlink = ctx->inputs[0];
  188. switch (inlink->format) {
  189. case AV_SAMPLE_FMT_DBL: afade->fade_samples = fade_samples_dbl; break;
  190. case AV_SAMPLE_FMT_DBLP: afade->fade_samples = fade_samples_dblp; break;
  191. case AV_SAMPLE_FMT_FLT: afade->fade_samples = fade_samples_flt; break;
  192. case AV_SAMPLE_FMT_FLTP: afade->fade_samples = fade_samples_fltp; break;
  193. case AV_SAMPLE_FMT_S16: afade->fade_samples = fade_samples_s16; break;
  194. case AV_SAMPLE_FMT_S16P: afade->fade_samples = fade_samples_s16p; break;
  195. case AV_SAMPLE_FMT_S32: afade->fade_samples = fade_samples_s32; break;
  196. case AV_SAMPLE_FMT_S32P: afade->fade_samples = fade_samples_s32p; break;
  197. }
  198. if (afade->duration)
  199. afade->nb_samples = afade->duration * inlink->sample_rate;
  200. if (afade->start_time)
  201. afade->start_sample = afade->start_time * inlink->sample_rate;
  202. return 0;
  203. }
  204. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
  205. {
  206. AudioFadeContext *afade = inlink->dst->priv;
  207. AVFilterLink *outlink = inlink->dst->outputs[0];
  208. int nb_samples = buf->audio->nb_samples;
  209. AVFilterBufferRef *out_buf;
  210. int64_t cur_sample = av_rescale_q(buf->pts, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  211. if ((!afade->type && (afade->start_sample + afade->nb_samples < cur_sample)) ||
  212. ( afade->type && (cur_sample + afade->nb_samples < afade->start_sample)))
  213. return ff_filter_frame(outlink, buf);
  214. if (buf->perms & AV_PERM_WRITE) {
  215. out_buf = buf;
  216. } else {
  217. out_buf = ff_get_audio_buffer(inlink, AV_PERM_WRITE, nb_samples);
  218. if (!out_buf)
  219. return AVERROR(ENOMEM);
  220. out_buf->pts = buf->pts;
  221. }
  222. if ((!afade->type && (cur_sample + nb_samples < afade->start_sample)) ||
  223. ( afade->type && (afade->start_sample + afade->nb_samples < cur_sample))) {
  224. av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
  225. out_buf->audio->channels, out_buf->format);
  226. } else {
  227. int64_t start;
  228. if (!afade->type)
  229. start = cur_sample - afade->start_sample;
  230. else
  231. start = afade->start_sample + afade->nb_samples - cur_sample;
  232. afade->fade_samples(out_buf->extended_data, buf->extended_data,
  233. nb_samples, buf->audio->channels,
  234. afade->type ? -1 : 1, start,
  235. afade->nb_samples, afade->curve);
  236. }
  237. if (buf != out_buf)
  238. avfilter_unref_buffer(buf);
  239. return ff_filter_frame(outlink, out_buf);
  240. }
  241. static const AVFilterPad avfilter_af_afade_inputs[] = {
  242. {
  243. .name = "default",
  244. .type = AVMEDIA_TYPE_AUDIO,
  245. .filter_frame = filter_frame,
  246. },
  247. { NULL }
  248. };
  249. static const AVFilterPad avfilter_af_afade_outputs[] = {
  250. {
  251. .name = "default",
  252. .type = AVMEDIA_TYPE_AUDIO,
  253. .config_props = config_output,
  254. },
  255. { NULL }
  256. };
  257. AVFilter avfilter_af_afade = {
  258. .name = "afade",
  259. .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
  260. .query_formats = query_formats,
  261. .priv_size = sizeof(AudioFadeContext),
  262. .init = init,
  263. .inputs = avfilter_af_afade_inputs,
  264. .outputs = avfilter_af_afade_outputs,
  265. .priv_class = &afade_class,
  266. };