af_chorus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. if (tstr)
  89. new_nb_items += sscanf(tstr, "%f", &items[new_nb_items]) == 1;
  90. }
  91. *nb_items = new_nb_items;
  92. }
  93. static av_cold int init(AVFilterContext *ctx)
  94. {
  95. ChorusContext *s = ctx->priv;
  96. int nb_delays, nb_decays, nb_speeds, nb_depths;
  97. if (!s->delays_str || !s->decays_str || !s->speeds_str || !s->depths_str) {
  98. av_log(ctx, AV_LOG_ERROR, "Both delays & decays & speeds & depths must be set.\n");
  99. return AVERROR(EINVAL);
  100. }
  101. count_items(s->delays_str, &nb_delays);
  102. count_items(s->decays_str, &nb_decays);
  103. count_items(s->speeds_str, &nb_speeds);
  104. count_items(s->depths_str, &nb_depths);
  105. s->delays = av_realloc_f(s->delays, nb_delays, sizeof(*s->delays));
  106. s->decays = av_realloc_f(s->decays, nb_decays, sizeof(*s->decays));
  107. s->speeds = av_realloc_f(s->speeds, nb_speeds, sizeof(*s->speeds));
  108. s->depths = av_realloc_f(s->depths, nb_depths, sizeof(*s->depths));
  109. if (!s->delays || !s->decays || !s->speeds || !s->depths)
  110. return AVERROR(ENOMEM);
  111. fill_items(s->delays_str, &nb_delays, s->delays);
  112. fill_items(s->decays_str, &nb_decays, s->decays);
  113. fill_items(s->speeds_str, &nb_speeds, s->speeds);
  114. fill_items(s->depths_str, &nb_depths, s->depths);
  115. if (nb_delays != nb_decays && nb_delays != nb_speeds && nb_delays != nb_depths) {
  116. av_log(ctx, AV_LOG_ERROR, "Number of delays & decays & speeds & depths given must be same.\n");
  117. return AVERROR(EINVAL);
  118. }
  119. s->num_chorus = nb_delays;
  120. if (s->num_chorus < 1) {
  121. av_log(ctx, AV_LOG_ERROR, "At least one delay & decay & speed & depth must be set.\n");
  122. return AVERROR(EINVAL);
  123. }
  124. s->length = av_calloc(s->num_chorus, sizeof(*s->length));
  125. s->lookup_table = av_calloc(s->num_chorus, sizeof(*s->lookup_table));
  126. if (!s->length || !s->lookup_table)
  127. return AVERROR(ENOMEM);
  128. s->next_pts = AV_NOPTS_VALUE;
  129. return 0;
  130. }
  131. static int query_formats(AVFilterContext *ctx)
  132. {
  133. AVFilterFormats *formats;
  134. AVFilterChannelLayouts *layouts;
  135. static const enum AVSampleFormat sample_fmts[] = {
  136. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
  137. };
  138. int ret;
  139. layouts = ff_all_channel_counts();
  140. if (!layouts)
  141. return AVERROR(ENOMEM);
  142. ret = ff_set_common_channel_layouts(ctx, layouts);
  143. if (ret < 0)
  144. return ret;
  145. formats = ff_make_format_list(sample_fmts);
  146. if (!formats)
  147. return AVERROR(ENOMEM);
  148. ret = ff_set_common_formats(ctx, formats);
  149. if (ret < 0)
  150. return ret;
  151. formats = ff_all_samplerates();
  152. if (!formats)
  153. return AVERROR(ENOMEM);
  154. return ff_set_common_samplerates(ctx, formats);
  155. }
  156. static int config_output(AVFilterLink *outlink)
  157. {
  158. AVFilterContext *ctx = outlink->src;
  159. ChorusContext *s = ctx->priv;
  160. float sum_in_volume = 1.0;
  161. int n;
  162. s->channels = outlink->channels;
  163. for (n = 0; n < s->num_chorus; n++) {
  164. int samples = (int) ((s->delays[n] + s->depths[n]) * outlink->sample_rate / 1000.0);
  165. int depth_samples = (int) (s->depths[n] * outlink->sample_rate / 1000.0);
  166. s->length[n] = outlink->sample_rate / s->speeds[n];
  167. s->lookup_table[n] = av_malloc(sizeof(int32_t) * s->length[n]);
  168. if (!s->lookup_table[n])
  169. return AVERROR(ENOMEM);
  170. ff_generate_wave_table(WAVE_SIN, AV_SAMPLE_FMT_S32, s->lookup_table[n],
  171. s->length[n], 0., depth_samples, 0);
  172. s->max_samples = FFMAX(s->max_samples, samples);
  173. }
  174. for (n = 0; n < s->num_chorus; n++)
  175. sum_in_volume += s->decays[n];
  176. if (s->in_gain * (sum_in_volume) > 1.0 / s->out_gain)
  177. av_log(ctx, AV_LOG_WARNING, "output gain can cause saturation or clipping of output\n");
  178. s->counter = av_calloc(outlink->channels, sizeof(*s->counter));
  179. if (!s->counter)
  180. return AVERROR(ENOMEM);
  181. s->phase = av_calloc(outlink->channels, sizeof(*s->phase));
  182. if (!s->phase)
  183. return AVERROR(ENOMEM);
  184. for (n = 0; n < outlink->channels; n++) {
  185. s->phase[n] = av_calloc(s->num_chorus, sizeof(int));
  186. if (!s->phase[n])
  187. return AVERROR(ENOMEM);
  188. }
  189. s->fade_out = s->max_samples;
  190. return av_samples_alloc_array_and_samples(&s->chorusbuf, NULL,
  191. outlink->channels,
  192. s->max_samples,
  193. outlink->format, 0);
  194. }
  195. #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
  196. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  197. {
  198. AVFilterContext *ctx = inlink->dst;
  199. ChorusContext *s = ctx->priv;
  200. AVFrame *out_frame;
  201. int c, i, n;
  202. if (av_frame_is_writable(frame)) {
  203. out_frame = frame;
  204. } else {
  205. out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
  206. if (!out_frame) {
  207. av_frame_free(&frame);
  208. return AVERROR(ENOMEM);
  209. }
  210. av_frame_copy_props(out_frame, frame);
  211. }
  212. for (c = 0; c < inlink->channels; c++) {
  213. const float *src = (const float *)frame->extended_data[c];
  214. float *dst = (float *)out_frame->extended_data[c];
  215. float *chorusbuf = (float *)s->chorusbuf[c];
  216. int *phase = s->phase[c];
  217. for (i = 0; i < frame->nb_samples; i++) {
  218. float out, in = src[i];
  219. out = in * s->in_gain;
  220. for (n = 0; n < s->num_chorus; n++) {
  221. out += chorusbuf[MOD(s->max_samples + s->counter[c] -
  222. s->lookup_table[n][phase[n]],
  223. s->max_samples)] * s->decays[n];
  224. phase[n] = MOD(phase[n] + 1, s->length[n]);
  225. }
  226. out *= s->out_gain;
  227. dst[i] = out;
  228. chorusbuf[s->counter[c]] = in;
  229. s->counter[c] = MOD(s->counter[c] + 1, s->max_samples);
  230. }
  231. }
  232. s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
  233. if (frame != out_frame)
  234. av_frame_free(&frame);
  235. return ff_filter_frame(ctx->outputs[0], out_frame);
  236. }
  237. static int request_frame(AVFilterLink *outlink)
  238. {
  239. AVFilterContext *ctx = outlink->src;
  240. ChorusContext *s = ctx->priv;
  241. int ret;
  242. ret = ff_request_frame(ctx->inputs[0]);
  243. if (ret == AVERROR_EOF && !ctx->is_disabled && s->fade_out) {
  244. int nb_samples = FFMIN(s->fade_out, 2048);
  245. AVFrame *frame;
  246. frame = ff_get_audio_buffer(outlink, nb_samples);
  247. if (!frame)
  248. return AVERROR(ENOMEM);
  249. s->fade_out -= nb_samples;
  250. av_samples_set_silence(frame->extended_data, 0,
  251. frame->nb_samples,
  252. outlink->channels,
  253. frame->format);
  254. frame->pts = s->next_pts;
  255. if (s->next_pts != AV_NOPTS_VALUE)
  256. s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  257. ret = filter_frame(ctx->inputs[0], frame);
  258. }
  259. return ret;
  260. }
  261. static av_cold void uninit(AVFilterContext *ctx)
  262. {
  263. ChorusContext *s = ctx->priv;
  264. int n;
  265. av_freep(&s->delays);
  266. av_freep(&s->decays);
  267. av_freep(&s->speeds);
  268. av_freep(&s->depths);
  269. if (s->chorusbuf)
  270. av_freep(&s->chorusbuf[0]);
  271. av_freep(&s->chorusbuf);
  272. if (s->phase)
  273. for (n = 0; n < s->channels; n++)
  274. av_freep(&s->phase[n]);
  275. av_freep(&s->phase);
  276. av_freep(&s->counter);
  277. av_freep(&s->length);
  278. if (s->lookup_table)
  279. for (n = 0; n < s->num_chorus; n++)
  280. av_freep(&s->lookup_table[n]);
  281. av_freep(&s->lookup_table);
  282. }
  283. static const AVFilterPad chorus_inputs[] = {
  284. {
  285. .name = "default",
  286. .type = AVMEDIA_TYPE_AUDIO,
  287. .filter_frame = filter_frame,
  288. },
  289. { NULL }
  290. };
  291. static const AVFilterPad chorus_outputs[] = {
  292. {
  293. .name = "default",
  294. .type = AVMEDIA_TYPE_AUDIO,
  295. .request_frame = request_frame,
  296. .config_props = config_output,
  297. },
  298. { NULL }
  299. };
  300. AVFilter ff_af_chorus = {
  301. .name = "chorus",
  302. .description = NULL_IF_CONFIG_SMALL("Add a chorus effect to the audio."),
  303. .query_formats = query_formats,
  304. .priv_size = sizeof(ChorusContext),
  305. .priv_class = &chorus_class,
  306. .init = init,
  307. .uninit = uninit,
  308. .inputs = chorus_inputs,
  309. .outputs = chorus_outputs,
  310. };