af_chorus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright (c) 1998 Juergen Mueller And Sundry Contributors
  3. * This source code is freely redistributable and may be used for
  4. * any purpose. This copyright notice must be maintained.
  5. * Juergen Mueller And Sundry Contributors are not responsible for
  6. * the consequences of using this software.
  7. *
  8. * Copyright (c) 2015 Paul B Mahol
  9. *
  10. * This file is part of FFmpeg.
  11. *
  12. * FFmpeg is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * FFmpeg is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with FFmpeg; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. /**
  27. * @file
  28. * chorus audio filter
  29. */
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/opt.h"
  32. #include "audio.h"
  33. #include "avfilter.h"
  34. #include "internal.h"
  35. #include "generate_wave_table.h"
  36. typedef struct ChorusContext {
  37. const AVClass *class;
  38. float in_gain, out_gain;
  39. char *delays_str;
  40. char *decays_str;
  41. char *speeds_str;
  42. char *depths_str;
  43. float *delays;
  44. float *decays;
  45. float *speeds;
  46. float *depths;
  47. uint8_t **chorusbuf;
  48. int **phase;
  49. int *length;
  50. int32_t **lookup_table;
  51. int *counter;
  52. int num_chorus;
  53. int max_samples;
  54. int channels;
  55. int modulation;
  56. int fade_out;
  57. int64_t next_pts;
  58. } ChorusContext;
  59. #define OFFSET(x) offsetof(ChorusContext, x)
  60. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  61. static const AVOption chorus_options[] = {
  62. { "in_gain", "set input gain", OFFSET(in_gain), AV_OPT_TYPE_FLOAT, {.dbl=.4}, 0, 1, A },
  63. { "out_gain", "set output gain", OFFSET(out_gain), AV_OPT_TYPE_FLOAT, {.dbl=.4}, 0, 1, A },
  64. { "delays", "set delays", OFFSET(delays_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
  65. { "decays", "set decays", OFFSET(decays_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
  66. { "speeds", "set speeds", OFFSET(speeds_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
  67. { "depths", "set depths", OFFSET(depths_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
  68. { NULL }
  69. };
  70. AVFILTER_DEFINE_CLASS(chorus);
  71. static void count_items(char *item_str, int *nb_items)
  72. {
  73. char *p;
  74. *nb_items = 1;
  75. for (p = item_str; *p; p++) {
  76. if (*p == '|')
  77. (*nb_items)++;
  78. }
  79. }
  80. static void fill_items(char *item_str, int *nb_items, float *items)
  81. {
  82. char *p, *saveptr = NULL;
  83. int i, new_nb_items = 0;
  84. p = item_str;
  85. for (i = 0; i < *nb_items; i++) {
  86. char *tstr = av_strtok(p, "|", &saveptr);
  87. p = NULL;
  88. new_nb_items += sscanf(tstr, "%f", &items[i]) == 1;
  89. }
  90. *nb_items = new_nb_items;
  91. }
  92. static av_cold int init(AVFilterContext *ctx)
  93. {
  94. ChorusContext *s = ctx->priv;
  95. int nb_delays, nb_decays, nb_speeds, nb_depths;
  96. if (!s->delays_str || !s->decays_str || !s->speeds_str || !s->depths_str) {
  97. av_log(ctx, AV_LOG_ERROR, "Both delays & decays & speeds & depths must be set.\n");
  98. return AVERROR(EINVAL);
  99. }
  100. count_items(s->delays_str, &nb_delays);
  101. count_items(s->decays_str, &nb_decays);
  102. count_items(s->speeds_str, &nb_speeds);
  103. count_items(s->depths_str, &nb_depths);
  104. s->delays = av_realloc_f(s->delays, nb_delays, sizeof(*s->delays));
  105. s->decays = av_realloc_f(s->decays, nb_decays, sizeof(*s->decays));
  106. s->speeds = av_realloc_f(s->speeds, nb_speeds, sizeof(*s->speeds));
  107. s->depths = av_realloc_f(s->depths, nb_depths, sizeof(*s->depths));
  108. if (!s->delays || !s->decays || !s->speeds || !s->depths)
  109. return AVERROR(ENOMEM);
  110. fill_items(s->delays_str, &nb_delays, s->delays);
  111. fill_items(s->decays_str, &nb_decays, s->decays);
  112. fill_items(s->speeds_str, &nb_speeds, s->speeds);
  113. fill_items(s->depths_str, &nb_depths, s->depths);
  114. if (nb_delays != nb_decays && nb_delays != nb_speeds && nb_delays != nb_depths) {
  115. av_log(ctx, AV_LOG_ERROR, "Number of delays & decays & speeds & depths given must be same.\n");
  116. return AVERROR(EINVAL);
  117. }
  118. s->num_chorus = nb_delays;
  119. if (s->num_chorus < 1) {
  120. av_log(ctx, AV_LOG_ERROR, "At least one delay & decay & speed & depth must be set.\n");
  121. return AVERROR(EINVAL);
  122. }
  123. s->length = av_calloc(s->num_chorus, sizeof(*s->length));
  124. s->lookup_table = av_calloc(s->num_chorus, sizeof(*s->lookup_table));
  125. if (!s->length || !s->lookup_table)
  126. return AVERROR(ENOMEM);
  127. s->next_pts = AV_NOPTS_VALUE;
  128. return 0;
  129. }
  130. static int query_formats(AVFilterContext *ctx)
  131. {
  132. AVFilterFormats *formats;
  133. AVFilterChannelLayouts *layouts;
  134. static const enum AVSampleFormat sample_fmts[] = {
  135. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
  136. };
  137. int ret;
  138. layouts = ff_all_channel_counts();
  139. if (!layouts)
  140. return AVERROR(ENOMEM);
  141. ret = ff_set_common_channel_layouts(ctx, layouts);
  142. if (ret < 0)
  143. return ret;
  144. formats = ff_make_format_list(sample_fmts);
  145. if (!formats)
  146. return AVERROR(ENOMEM);
  147. ret = ff_set_common_formats(ctx, formats);
  148. if (ret < 0)
  149. return ret;
  150. formats = ff_all_samplerates();
  151. if (!formats)
  152. return AVERROR(ENOMEM);
  153. return ff_set_common_samplerates(ctx, formats);
  154. }
  155. static int config_output(AVFilterLink *outlink)
  156. {
  157. AVFilterContext *ctx = outlink->src;
  158. ChorusContext *s = ctx->priv;
  159. float sum_in_volume = 1.0;
  160. int n;
  161. s->channels = outlink->channels;
  162. for (n = 0; n < s->num_chorus; n++) {
  163. int samples = (int) ((s->delays[n] + s->depths[n]) * outlink->sample_rate / 1000.0);
  164. int depth_samples = (int) (s->depths[n] * outlink->sample_rate / 1000.0);
  165. s->length[n] = outlink->sample_rate / s->speeds[n];
  166. s->lookup_table[n] = av_malloc(sizeof(int32_t) * s->length[n]);
  167. if (!s->lookup_table[n])
  168. return AVERROR(ENOMEM);
  169. ff_generate_wave_table(WAVE_SIN, AV_SAMPLE_FMT_S32, s->lookup_table[n],
  170. s->length[n], 0., depth_samples, 0);
  171. s->max_samples = FFMAX(s->max_samples, samples);
  172. }
  173. for (n = 0; n < s->num_chorus; n++)
  174. sum_in_volume += s->decays[n];
  175. if (s->in_gain * (sum_in_volume) > 1.0 / s->out_gain)
  176. av_log(ctx, AV_LOG_WARNING, "output gain can cause saturation or clipping of output\n");
  177. s->counter = av_calloc(outlink->channels, sizeof(*s->counter));
  178. if (!s->counter)
  179. return AVERROR(ENOMEM);
  180. s->phase = av_calloc(outlink->channels, sizeof(*s->phase));
  181. if (!s->phase)
  182. return AVERROR(ENOMEM);
  183. for (n = 0; n < outlink->channels; n++) {
  184. s->phase[n] = av_calloc(s->num_chorus, sizeof(int));
  185. if (!s->phase[n])
  186. return AVERROR(ENOMEM);
  187. }
  188. s->fade_out = s->max_samples;
  189. return av_samples_alloc_array_and_samples(&s->chorusbuf, NULL,
  190. outlink->channels,
  191. s->max_samples,
  192. outlink->format, 0);
  193. }
  194. #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
  195. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  196. {
  197. AVFilterContext *ctx = inlink->dst;
  198. ChorusContext *s = ctx->priv;
  199. AVFrame *out_frame;
  200. int c, i, n;
  201. if (av_frame_is_writable(frame)) {
  202. out_frame = frame;
  203. } else {
  204. out_frame = ff_get_audio_buffer(inlink, frame->nb_samples);
  205. if (!out_frame) {
  206. av_frame_free(&frame);
  207. return AVERROR(ENOMEM);
  208. }
  209. av_frame_copy_props(out_frame, frame);
  210. }
  211. for (c = 0; c < inlink->channels; c++) {
  212. const float *src = (const float *)frame->extended_data[c];
  213. float *dst = (float *)out_frame->extended_data[c];
  214. float *chorusbuf = (float *)s->chorusbuf[c];
  215. int *phase = s->phase[c];
  216. for (i = 0; i < frame->nb_samples; i++) {
  217. float out, in = src[i];
  218. out = in * s->in_gain;
  219. for (n = 0; n < s->num_chorus; n++) {
  220. out += chorusbuf[MOD(s->max_samples + s->counter[c] -
  221. s->lookup_table[n][phase[n]],
  222. s->max_samples)] * s->decays[n];
  223. phase[n] = MOD(phase[n] + 1, s->length[n]);
  224. }
  225. out *= s->out_gain;
  226. dst[i] = out;
  227. chorusbuf[s->counter[c]] = in;
  228. s->counter[c] = MOD(s->counter[c] + 1, s->max_samples);
  229. }
  230. }
  231. s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
  232. if (frame != out_frame)
  233. av_frame_free(&frame);
  234. return ff_filter_frame(ctx->outputs[0], out_frame);
  235. }
  236. static int request_frame(AVFilterLink *outlink)
  237. {
  238. AVFilterContext *ctx = outlink->src;
  239. ChorusContext *s = ctx->priv;
  240. int ret;
  241. ret = ff_request_frame(ctx->inputs[0]);
  242. if (ret == AVERROR_EOF && !ctx->is_disabled && s->fade_out) {
  243. int nb_samples = FFMIN(s->fade_out, 2048);
  244. AVFrame *frame;
  245. frame = ff_get_audio_buffer(outlink, nb_samples);
  246. if (!frame)
  247. return AVERROR(ENOMEM);
  248. s->fade_out -= nb_samples;
  249. av_samples_set_silence(frame->extended_data, 0,
  250. frame->nb_samples,
  251. outlink->channels,
  252. frame->format);
  253. frame->pts = s->next_pts;
  254. if (s->next_pts != AV_NOPTS_VALUE)
  255. s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  256. ret = filter_frame(ctx->inputs[0], frame);
  257. }
  258. return ret;
  259. }
  260. static av_cold void uninit(AVFilterContext *ctx)
  261. {
  262. ChorusContext *s = ctx->priv;
  263. int n;
  264. av_freep(&s->delays);
  265. av_freep(&s->decays);
  266. av_freep(&s->speeds);
  267. av_freep(&s->depths);
  268. if (s->chorusbuf)
  269. av_freep(&s->chorusbuf[0]);
  270. av_freep(&s->chorusbuf);
  271. if (s->phase)
  272. for (n = 0; n < s->channels; n++)
  273. av_freep(&s->phase[n]);
  274. av_freep(&s->phase);
  275. av_freep(&s->counter);
  276. av_freep(&s->length);
  277. if (s->lookup_table)
  278. for (n = 0; n < s->num_chorus; n++)
  279. av_freep(&s->lookup_table[n]);
  280. av_freep(&s->lookup_table);
  281. }
  282. static const AVFilterPad chorus_inputs[] = {
  283. {
  284. .name = "default",
  285. .type = AVMEDIA_TYPE_AUDIO,
  286. .filter_frame = filter_frame,
  287. },
  288. { NULL }
  289. };
  290. static const AVFilterPad chorus_outputs[] = {
  291. {
  292. .name = "default",
  293. .type = AVMEDIA_TYPE_AUDIO,
  294. .request_frame = request_frame,
  295. .config_props = config_output,
  296. },
  297. { NULL }
  298. };
  299. AVFilter ff_af_chorus = {
  300. .name = "chorus",
  301. .description = NULL_IF_CONFIG_SMALL("Add a chorus effect to the audio."),
  302. .query_formats = query_formats,
  303. .priv_size = sizeof(ChorusContext),
  304. .priv_class = &chorus_class,
  305. .init = init,
  306. .uninit = uninit,
  307. .inputs = chorus_inputs,
  308. .outputs = chorus_outputs,
  309. };