af_vibrato.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include "generate_wave_table.h"
  25. typedef struct VibratoContext {
  26. const AVClass *class;
  27. double freq;
  28. double depth;
  29. int channels;
  30. double **buf;
  31. int buf_index;
  32. int buf_size;
  33. double *wave_table;
  34. int wave_table_index;
  35. int wave_table_size;
  36. } VibratoContext;
  37. #define OFFSET(x) offsetof(VibratoContext, x)
  38. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  39. static const AVOption vibrato_options[] = {
  40. { "f", "set frequency in hertz", OFFSET(freq), AV_OPT_TYPE_DOUBLE, {.dbl = 5.0}, 0.1, 20000.0, FLAGS },
  41. { "d", "set depth as percentage", OFFSET(depth), AV_OPT_TYPE_DOUBLE, {.dbl = 0.5}, 0.00, 1.0, FLAGS },
  42. { NULL }
  43. };
  44. AVFILTER_DEFINE_CLASS(vibrato);
  45. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  46. {
  47. AVFilterContext *ctx = inlink->dst;
  48. VibratoContext *s = ctx->priv;
  49. AVFilterLink *outlink = ctx->outputs[0];
  50. AVFrame *out;
  51. int n, c;
  52. const double *src;
  53. double *dst;
  54. if (av_frame_is_writable(in)) {
  55. out = in;
  56. } else {
  57. out = ff_get_audio_buffer(inlink, in->nb_samples);
  58. if (!out) {
  59. av_frame_free(&in);
  60. return AVERROR(ENOMEM);
  61. }
  62. av_frame_copy_props(out, in);
  63. }
  64. for (n = 0; n < in->nb_samples; n++) {
  65. double integer, decimal;
  66. decimal = modf(s->depth * s->wave_table[s->wave_table_index], &integer);
  67. s->wave_table_index++;
  68. if (s->wave_table_index >= s->wave_table_size)
  69. s->wave_table_index -= s->wave_table_size;
  70. for (c = 0; c < inlink->channels; c++) {
  71. int samp1_index, samp2_index;
  72. double *buf;
  73. double this_samp;
  74. src = (const double *)in->extended_data[c];
  75. dst = (double *)out->extended_data[c];
  76. buf = s->buf[c];
  77. samp1_index = s->buf_index + integer;
  78. if (samp1_index >= s->buf_size)
  79. samp1_index -= s->buf_size;
  80. samp2_index = samp1_index + 1;
  81. if (samp2_index >= s->buf_size)
  82. samp2_index -= s->buf_size;
  83. this_samp = src[n];
  84. dst[n] = buf[samp1_index] + (decimal * (buf[samp2_index] - buf[samp1_index]));
  85. buf[s->buf_index] = this_samp;
  86. }
  87. s->buf_index++;
  88. if (s->buf_index >= s->buf_size)
  89. s->buf_index -= s->buf_size;
  90. }
  91. if (in != out)
  92. av_frame_free(&in);
  93. return ff_filter_frame(outlink, out);
  94. }
  95. static int query_formats(AVFilterContext *ctx)
  96. {
  97. AVFilterFormats *formats;
  98. AVFilterChannelLayouts *layouts;
  99. static const enum AVSampleFormat sample_fmts[] = {
  100. AV_SAMPLE_FMT_DBLP,
  101. AV_SAMPLE_FMT_NONE
  102. };
  103. int ret;
  104. layouts = ff_all_channel_counts();
  105. if (!layouts)
  106. return AVERROR(ENOMEM);
  107. ret = ff_set_common_channel_layouts(ctx, layouts);
  108. if (ret < 0)
  109. return ret;
  110. formats = ff_make_format_list(sample_fmts);
  111. if (!formats)
  112. return AVERROR(ENOMEM);
  113. ret = ff_set_common_formats(ctx, formats);
  114. if (ret < 0)
  115. return ret;
  116. formats = ff_all_samplerates();
  117. if (!formats)
  118. return AVERROR(ENOMEM);
  119. return ff_set_common_samplerates(ctx, formats);
  120. }
  121. static av_cold void uninit(AVFilterContext *ctx)
  122. {
  123. VibratoContext *s = ctx->priv;
  124. int c;
  125. av_freep(&s->wave_table);
  126. for (c = 0; c < s->channels; c++)
  127. av_freep(&s->buf[c]);
  128. av_freep(&s->buf);
  129. }
  130. static int config_input(AVFilterLink *inlink)
  131. {
  132. int c;
  133. AVFilterContext *ctx = inlink->dst;
  134. VibratoContext *s = ctx->priv;
  135. s->channels = inlink->channels;
  136. s->buf = av_calloc(inlink->channels, sizeof(*s->buf));
  137. if (!s->buf)
  138. return AVERROR(ENOMEM);
  139. s->buf_size = inlink->sample_rate * 0.005;
  140. for (c = 0; c < s->channels; c++) {
  141. s->buf[c] = av_malloc_array(s->buf_size, sizeof(*s->buf[c]));
  142. if (!s->buf[c])
  143. return AVERROR(ENOMEM);
  144. }
  145. s->buf_index = 0;
  146. s->wave_table_size = inlink->sample_rate / s->freq;
  147. s->wave_table = av_malloc_array(s->wave_table_size, sizeof(*s->wave_table));
  148. if (!s->wave_table)
  149. return AVERROR(ENOMEM);
  150. ff_generate_wave_table(WAVE_SIN, AV_SAMPLE_FMT_DBL, s->wave_table, s->wave_table_size, 0.0, s->buf_size - 1, 3.0 * M_PI_2);
  151. s->wave_table_index = 0;
  152. return 0;
  153. }
  154. static const AVFilterPad avfilter_af_vibrato_inputs[] = {
  155. {
  156. .name = "default",
  157. .type = AVMEDIA_TYPE_AUDIO,
  158. .config_props = config_input,
  159. .filter_frame = filter_frame,
  160. },
  161. { NULL }
  162. };
  163. static const AVFilterPad avfilter_af_vibrato_outputs[] = {
  164. {
  165. .name = "default",
  166. .type = AVMEDIA_TYPE_AUDIO,
  167. },
  168. { NULL }
  169. };
  170. AVFilter ff_af_vibrato = {
  171. .name = "vibrato",
  172. .description = NULL_IF_CONFIG_SMALL("Apply vibrato effect."),
  173. .priv_size = sizeof(VibratoContext),
  174. .priv_class = &vibrato_class,
  175. .uninit = uninit,
  176. .query_formats = query_formats,
  177. .inputs = avfilter_af_vibrato_inputs,
  178. .outputs = avfilter_af_vibrato_outputs,
  179. };