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.

173 lines
4.7KB

  1. /*
  2. * Copyright (c) 2015 Kyle Swanson <k@ylo.ph>.
  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. #include "libavutil/opt.h"
  21. #include "avfilter.h"
  22. #include "internal.h"
  23. #include "audio.h"
  24. typedef struct TremoloContext {
  25. const AVClass *class;
  26. double freq;
  27. double depth;
  28. double *table;
  29. int table_size;
  30. int index;
  31. } TremoloContext;
  32. #define OFFSET(x) offsetof(TremoloContext, x)
  33. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  34. static const AVOption tremolo_options[] = {
  35. { "f", "set frequency in hertz", OFFSET(freq), AV_OPT_TYPE_DOUBLE, {.dbl = 5.0}, 0.1, 20000.0, FLAGS },
  36. { "d", "set depth as percentage", OFFSET(depth), AV_OPT_TYPE_DOUBLE, {.dbl = 0.5}, 0.0, 1.0, FLAGS },
  37. { NULL }
  38. };
  39. AVFILTER_DEFINE_CLASS(tremolo);
  40. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  41. {
  42. AVFilterContext *ctx = inlink->dst;
  43. AVFilterLink *outlink = ctx->outputs[0];
  44. TremoloContext *s = ctx->priv;
  45. const double *src = (const double *)in->data[0];
  46. const int channels = inlink->channels;
  47. const int nb_samples = in->nb_samples;
  48. AVFrame *out;
  49. double *dst;
  50. int n, c;
  51. if (av_frame_is_writable(in)) {
  52. out = in;
  53. } else {
  54. out = ff_get_audio_buffer(outlink, in->nb_samples);
  55. if (!out) {
  56. av_frame_free(&in);
  57. return AVERROR(ENOMEM);
  58. }
  59. av_frame_copy_props(out, in);
  60. }
  61. dst = (double *)out->data[0];
  62. for (n = 0; n < nb_samples; n++) {
  63. for (c = 0; c < channels; c++)
  64. dst[c] = src[c] * s->table[s->index];
  65. dst += channels;
  66. src += channels;
  67. s->index++;
  68. if (s->index >= s->table_size)
  69. s->index = 0;
  70. }
  71. if (in != out)
  72. av_frame_free(&in);
  73. return ff_filter_frame(outlink, out);
  74. }
  75. static int query_formats(AVFilterContext *ctx)
  76. {
  77. AVFilterFormats *formats;
  78. AVFilterChannelLayouts *layouts;
  79. static const enum AVSampleFormat sample_fmts[] = {
  80. AV_SAMPLE_FMT_DBL,
  81. AV_SAMPLE_FMT_NONE
  82. };
  83. int ret;
  84. layouts = ff_all_channel_counts();
  85. if (!layouts)
  86. return AVERROR(ENOMEM);
  87. ret = ff_set_common_channel_layouts(ctx, layouts);
  88. if (ret < 0)
  89. return ret;
  90. formats = ff_make_format_list(sample_fmts);
  91. if (!formats)
  92. return AVERROR(ENOMEM);
  93. ret = ff_set_common_formats(ctx, formats);
  94. if (ret < 0)
  95. return ret;
  96. formats = ff_all_samplerates();
  97. if (!formats)
  98. return AVERROR(ENOMEM);
  99. return ff_set_common_samplerates(ctx, formats);
  100. }
  101. static av_cold void uninit(AVFilterContext *ctx)
  102. {
  103. TremoloContext *s = ctx->priv;
  104. av_freep(&s->table);
  105. }
  106. static int config_input(AVFilterLink *inlink)
  107. {
  108. AVFilterContext *ctx = inlink->dst;
  109. TremoloContext *s = ctx->priv;
  110. const double offset = 1. - s->depth / 2.;
  111. int i;
  112. s->table_size = inlink->sample_rate / s->freq;
  113. s->table = av_malloc_array(s->table_size, sizeof(*s->table));
  114. if (!s->table)
  115. return AVERROR(ENOMEM);
  116. for (i = 0; i < s->table_size; i++) {
  117. double env = s->freq * i / inlink->sample_rate;
  118. env = sin(2 * M_PI * fmod(env + 0.25, 1.0));
  119. s->table[i] = env * (1 - fabs(offset)) + offset;
  120. }
  121. s->index = 0;
  122. return 0;
  123. }
  124. static const AVFilterPad avfilter_af_tremolo_inputs[] = {
  125. {
  126. .name = "default",
  127. .type = AVMEDIA_TYPE_AUDIO,
  128. .config_props = config_input,
  129. .filter_frame = filter_frame,
  130. },
  131. { NULL }
  132. };
  133. static const AVFilterPad avfilter_af_tremolo_outputs[] = {
  134. {
  135. .name = "default",
  136. .type = AVMEDIA_TYPE_AUDIO,
  137. },
  138. { NULL }
  139. };
  140. AVFilter ff_af_tremolo = {
  141. .name = "tremolo",
  142. .description = NULL_IF_CONFIG_SMALL("Apply tremolo effect."),
  143. .priv_size = sizeof(TremoloContext),
  144. .priv_class = &tremolo_class,
  145. .uninit = uninit,
  146. .query_formats = query_formats,
  147. .inputs = avfilter_af_tremolo_inputs,
  148. .outputs = avfilter_af_tremolo_outputs,
  149. };