af_aecho.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #include "libavutil/avassert.h"
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/samplefmt.h"
  24. #include "avfilter.h"
  25. #include "audio.h"
  26. #include "internal.h"
  27. typedef struct AudioEchoContext {
  28. const AVClass *class;
  29. float in_gain, out_gain;
  30. char *delays, *decays;
  31. float *delay, *decay;
  32. int nb_echoes;
  33. int delay_index;
  34. uint8_t **delayptrs;
  35. int max_samples, fade_out;
  36. int *samples;
  37. int64_t next_pts;
  38. void (*echo_samples)(struct AudioEchoContext *ctx, uint8_t **delayptrs,
  39. uint8_t * const *src, uint8_t **dst,
  40. int nb_samples, int channels);
  41. } AudioEchoContext;
  42. #define OFFSET(x) offsetof(AudioEchoContext, x)
  43. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  44. static const AVOption aecho_options[] = {
  45. { "in_gain", "set signal input gain", OFFSET(in_gain), AV_OPT_TYPE_FLOAT, {.dbl=0.6}, 0, 1, A },
  46. { "out_gain", "set signal output gain", OFFSET(out_gain), AV_OPT_TYPE_FLOAT, {.dbl=0.3}, 0, 1, A },
  47. { "delays", "set list of signal delays", OFFSET(delays), AV_OPT_TYPE_STRING, {.str="1000"}, 0, 0, A },
  48. { "decays", "set list of signal decays", OFFSET(decays), AV_OPT_TYPE_STRING, {.str="0.5"}, 0, 0, A },
  49. { NULL }
  50. };
  51. AVFILTER_DEFINE_CLASS(aecho);
  52. static void count_items(char *item_str, int *nb_items)
  53. {
  54. char *p;
  55. *nb_items = 1;
  56. for (p = item_str; *p; p++) {
  57. if (*p == '|')
  58. (*nb_items)++;
  59. }
  60. }
  61. static void fill_items(char *item_str, int *nb_items, float *items)
  62. {
  63. char *p, *saveptr = NULL;
  64. int i, new_nb_items = 0;
  65. p = item_str;
  66. for (i = 0; i < *nb_items; i++) {
  67. char *tstr = av_strtok(p, "|", &saveptr);
  68. p = NULL;
  69. new_nb_items += sscanf(tstr, "%f", &items[i]) == 1;
  70. }
  71. *nb_items = new_nb_items;
  72. }
  73. static av_cold void uninit(AVFilterContext *ctx)
  74. {
  75. AudioEchoContext *s = ctx->priv;
  76. av_freep(&s->delay);
  77. av_freep(&s->decay);
  78. av_freep(&s->samples);
  79. if (s->delayptrs)
  80. av_freep(&s->delayptrs[0]);
  81. av_freep(&s->delayptrs);
  82. }
  83. static av_cold int init(AVFilterContext *ctx)
  84. {
  85. AudioEchoContext *s = ctx->priv;
  86. int nb_delays, nb_decays, i;
  87. if (!s->delays || !s->decays) {
  88. av_log(ctx, AV_LOG_ERROR, "Missing delays and/or decays.\n");
  89. return AVERROR(EINVAL);
  90. }
  91. count_items(s->delays, &nb_delays);
  92. count_items(s->decays, &nb_decays);
  93. s->delay = av_realloc_f(s->delay, nb_delays, sizeof(*s->delay));
  94. s->decay = av_realloc_f(s->decay, nb_decays, sizeof(*s->decay));
  95. if (!s->delay || !s->decay)
  96. return AVERROR(ENOMEM);
  97. fill_items(s->delays, &nb_delays, s->delay);
  98. fill_items(s->decays, &nb_decays, s->decay);
  99. if (nb_delays != nb_decays) {
  100. av_log(ctx, AV_LOG_ERROR, "Number of delays %d differs from number of decays %d.\n", nb_delays, nb_decays);
  101. return AVERROR(EINVAL);
  102. }
  103. s->nb_echoes = nb_delays;
  104. if (!s->nb_echoes) {
  105. av_log(ctx, AV_LOG_ERROR, "At least one decay & delay must be set.\n");
  106. return AVERROR(EINVAL);
  107. }
  108. s->samples = av_realloc_f(s->samples, nb_delays, sizeof(*s->samples));
  109. if (!s->samples)
  110. return AVERROR(ENOMEM);
  111. for (i = 0; i < nb_delays; i++) {
  112. if (s->delay[i] <= 0 || s->delay[i] > 90000) {
  113. av_log(ctx, AV_LOG_ERROR, "delay[%d]: %f is out of allowed range: (0, 90000]\n", i, s->delay[i]);
  114. return AVERROR(EINVAL);
  115. }
  116. if (s->decay[i] <= 0 || s->decay[i] > 1) {
  117. av_log(ctx, AV_LOG_ERROR, "decay[%d]: %f is out of allowed range: (0, 1]\n", i, s->decay[i]);
  118. return AVERROR(EINVAL);
  119. }
  120. }
  121. s->next_pts = AV_NOPTS_VALUE;
  122. av_log(ctx, AV_LOG_DEBUG, "nb_echoes:%d\n", s->nb_echoes);
  123. return 0;
  124. }
  125. static int query_formats(AVFilterContext *ctx)
  126. {
  127. AVFilterChannelLayouts *layouts;
  128. AVFilterFormats *formats;
  129. static const enum AVSampleFormat sample_fmts[] = {
  130. AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
  131. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
  132. AV_SAMPLE_FMT_NONE
  133. };
  134. layouts = ff_all_channel_layouts();
  135. if (!layouts)
  136. return AVERROR(ENOMEM);
  137. ff_set_common_channel_layouts(ctx, layouts);
  138. formats = ff_make_format_list(sample_fmts);
  139. if (!formats)
  140. return AVERROR(ENOMEM);
  141. ff_set_common_formats(ctx, formats);
  142. formats = ff_all_samplerates();
  143. if (!formats)
  144. return AVERROR(ENOMEM);
  145. ff_set_common_samplerates(ctx, formats);
  146. return 0;
  147. }
  148. #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
  149. #define ECHO(name, type, min, max) \
  150. static void echo_samples_## name ##p(AudioEchoContext *ctx, \
  151. uint8_t **delayptrs, \
  152. uint8_t * const *src, uint8_t **dst, \
  153. int nb_samples, int channels) \
  154. { \
  155. const double out_gain = ctx->out_gain; \
  156. const double in_gain = ctx->in_gain; \
  157. const int nb_echoes = ctx->nb_echoes; \
  158. const int max_samples = ctx->max_samples; \
  159. int i, j, chan, av_uninit(index); \
  160. \
  161. av_assert1(channels > 0); /* would corrupt delay_index */ \
  162. \
  163. for (chan = 0; chan < channels; chan++) { \
  164. const type *s = (type *)src[chan]; \
  165. type *d = (type *)dst[chan]; \
  166. type *dbuf = (type *)delayptrs[chan]; \
  167. \
  168. index = ctx->delay_index; \
  169. for (i = 0; i < nb_samples; i++, s++, d++) { \
  170. double out, in; \
  171. \
  172. in = *s; \
  173. out = in * in_gain; \
  174. for (j = 0; j < nb_echoes; j++) { \
  175. int ix = index + max_samples - ctx->samples[j]; \
  176. ix = MOD(ix, max_samples); \
  177. out += dbuf[ix] * ctx->decay[j]; \
  178. } \
  179. out *= out_gain; \
  180. \
  181. *d = av_clipd(out, min, max); \
  182. dbuf[index] = in; \
  183. \
  184. index = MOD(index + 1, max_samples); \
  185. } \
  186. } \
  187. ctx->delay_index = index; \
  188. }
  189. ECHO(dbl, double, -1.0, 1.0 )
  190. ECHO(flt, float, -1.0, 1.0 )
  191. ECHO(s16, int16_t, INT16_MIN, INT16_MAX)
  192. ECHO(s32, int32_t, INT32_MIN, INT32_MAX)
  193. static int config_output(AVFilterLink *outlink)
  194. {
  195. AVFilterContext *ctx = outlink->src;
  196. AudioEchoContext *s = ctx->priv;
  197. float volume = 1.0;
  198. int i;
  199. for (i = 0; i < s->nb_echoes; i++) {
  200. s->samples[i] = s->delay[i] * outlink->sample_rate / 1000.0;
  201. s->max_samples = FFMAX(s->max_samples, s->samples[i]);
  202. volume += s->decay[i];
  203. }
  204. if (s->max_samples <= 0) {
  205. av_log(ctx, AV_LOG_ERROR, "Nothing to echo - missing delay samples.\n");
  206. return AVERROR(EINVAL);
  207. }
  208. s->fade_out = s->max_samples;
  209. if (volume * s->in_gain * s->out_gain > 1.0)
  210. av_log(ctx, AV_LOG_WARNING,
  211. "out_gain %f can cause saturation of output\n", s->out_gain);
  212. switch (outlink->format) {
  213. case AV_SAMPLE_FMT_DBLP: s->echo_samples = echo_samples_dblp; break;
  214. case AV_SAMPLE_FMT_FLTP: s->echo_samples = echo_samples_fltp; break;
  215. case AV_SAMPLE_FMT_S16P: s->echo_samples = echo_samples_s16p; break;
  216. case AV_SAMPLE_FMT_S32P: s->echo_samples = echo_samples_s32p; break;
  217. }
  218. if (s->delayptrs)
  219. av_freep(&s->delayptrs[0]);
  220. av_freep(&s->delayptrs);
  221. return av_samples_alloc_array_and_samples(&s->delayptrs, NULL,
  222. outlink->channels,
  223. s->max_samples,
  224. outlink->format, 0);
  225. }
  226. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  227. {
  228. AVFilterContext *ctx = inlink->dst;
  229. AudioEchoContext *s = ctx->priv;
  230. AVFrame *out_frame;
  231. if (av_frame_is_writable(frame)) {
  232. out_frame = frame;
  233. } else {
  234. out_frame = ff_get_audio_buffer(inlink, frame->nb_samples);
  235. if (!out_frame)
  236. return AVERROR(ENOMEM);
  237. av_frame_copy_props(out_frame, frame);
  238. }
  239. s->echo_samples(s, s->delayptrs, frame->extended_data, out_frame->extended_data,
  240. frame->nb_samples, inlink->channels);
  241. s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
  242. if (frame != out_frame)
  243. av_frame_free(&frame);
  244. return ff_filter_frame(ctx->outputs[0], out_frame);
  245. }
  246. static int request_frame(AVFilterLink *outlink)
  247. {
  248. AVFilterContext *ctx = outlink->src;
  249. AudioEchoContext *s = ctx->priv;
  250. int ret;
  251. ret = ff_request_frame(ctx->inputs[0]);
  252. if (ret == AVERROR_EOF && !ctx->is_disabled && s->fade_out) {
  253. int nb_samples = FFMIN(s->fade_out, 2048);
  254. AVFrame *frame;
  255. frame = ff_get_audio_buffer(outlink, nb_samples);
  256. if (!frame)
  257. return AVERROR(ENOMEM);
  258. s->fade_out -= nb_samples;
  259. av_samples_set_silence(frame->extended_data, 0,
  260. frame->nb_samples,
  261. outlink->channels,
  262. frame->format);
  263. s->echo_samples(s, s->delayptrs, frame->extended_data, frame->extended_data,
  264. frame->nb_samples, outlink->channels);
  265. frame->pts = s->next_pts;
  266. if (s->next_pts != AV_NOPTS_VALUE)
  267. s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  268. return ff_filter_frame(outlink, frame);
  269. }
  270. return ret;
  271. }
  272. static const AVFilterPad aecho_inputs[] = {
  273. {
  274. .name = "default",
  275. .type = AVMEDIA_TYPE_AUDIO,
  276. .filter_frame = filter_frame,
  277. },
  278. { NULL }
  279. };
  280. static const AVFilterPad aecho_outputs[] = {
  281. {
  282. .name = "default",
  283. .request_frame = request_frame,
  284. .config_props = config_output,
  285. .type = AVMEDIA_TYPE_AUDIO,
  286. },
  287. { NULL }
  288. };
  289. AVFilter ff_af_aecho = {
  290. .name = "aecho",
  291. .description = NULL_IF_CONFIG_SMALL("Add echoing to the audio."),
  292. .query_formats = query_formats,
  293. .priv_size = sizeof(AudioEchoContext),
  294. .priv_class = &aecho_class,
  295. .init = init,
  296. .uninit = uninit,
  297. .inputs = aecho_inputs,
  298. .outputs = aecho_outputs,
  299. };