af_aecho.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. int ret;
  135. layouts = ff_all_channel_counts();
  136. if (!layouts)
  137. return AVERROR(ENOMEM);
  138. ret = ff_set_common_channel_layouts(ctx, layouts);
  139. if (ret < 0)
  140. return ret;
  141. formats = ff_make_format_list(sample_fmts);
  142. if (!formats)
  143. return AVERROR(ENOMEM);
  144. ret = ff_set_common_formats(ctx, formats);
  145. if (ret < 0)
  146. return ret;
  147. formats = ff_all_samplerates();
  148. if (!formats)
  149. return AVERROR(ENOMEM);
  150. return ff_set_common_samplerates(ctx, formats);
  151. }
  152. #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
  153. #define ECHO(name, type, min, max) \
  154. static void echo_samples_## name ##p(AudioEchoContext *ctx, \
  155. uint8_t **delayptrs, \
  156. uint8_t * const *src, uint8_t **dst, \
  157. int nb_samples, int channels) \
  158. { \
  159. const double out_gain = ctx->out_gain; \
  160. const double in_gain = ctx->in_gain; \
  161. const int nb_echoes = ctx->nb_echoes; \
  162. const int max_samples = ctx->max_samples; \
  163. int i, j, chan, av_uninit(index); \
  164. \
  165. av_assert1(channels > 0); /* would corrupt delay_index */ \
  166. \
  167. for (chan = 0; chan < channels; chan++) { \
  168. const type *s = (type *)src[chan]; \
  169. type *d = (type *)dst[chan]; \
  170. type *dbuf = (type *)delayptrs[chan]; \
  171. \
  172. index = ctx->delay_index; \
  173. for (i = 0; i < nb_samples; i++, s++, d++) { \
  174. double out, in; \
  175. \
  176. in = *s; \
  177. out = in * in_gain; \
  178. for (j = 0; j < nb_echoes; j++) { \
  179. int ix = index + max_samples - ctx->samples[j]; \
  180. ix = MOD(ix, max_samples); \
  181. out += dbuf[ix] * ctx->decay[j]; \
  182. } \
  183. out *= out_gain; \
  184. \
  185. *d = av_clipd(out, min, max); \
  186. dbuf[index] = in; \
  187. \
  188. index = MOD(index + 1, max_samples); \
  189. } \
  190. } \
  191. ctx->delay_index = index; \
  192. }
  193. ECHO(dbl, double, -1.0, 1.0 )
  194. ECHO(flt, float, -1.0, 1.0 )
  195. ECHO(s16, int16_t, INT16_MIN, INT16_MAX)
  196. ECHO(s32, int32_t, INT32_MIN, INT32_MAX)
  197. static int config_output(AVFilterLink *outlink)
  198. {
  199. AVFilterContext *ctx = outlink->src;
  200. AudioEchoContext *s = ctx->priv;
  201. float volume = 1.0;
  202. int i;
  203. for (i = 0; i < s->nb_echoes; i++) {
  204. s->samples[i] = s->delay[i] * outlink->sample_rate / 1000.0;
  205. s->max_samples = FFMAX(s->max_samples, s->samples[i]);
  206. volume += s->decay[i];
  207. }
  208. if (s->max_samples <= 0) {
  209. av_log(ctx, AV_LOG_ERROR, "Nothing to echo - missing delay samples.\n");
  210. return AVERROR(EINVAL);
  211. }
  212. s->fade_out = s->max_samples;
  213. if (volume * s->in_gain * s->out_gain > 1.0)
  214. av_log(ctx, AV_LOG_WARNING,
  215. "out_gain %f can cause saturation of output\n", s->out_gain);
  216. switch (outlink->format) {
  217. case AV_SAMPLE_FMT_DBLP: s->echo_samples = echo_samples_dblp; break;
  218. case AV_SAMPLE_FMT_FLTP: s->echo_samples = echo_samples_fltp; break;
  219. case AV_SAMPLE_FMT_S16P: s->echo_samples = echo_samples_s16p; break;
  220. case AV_SAMPLE_FMT_S32P: s->echo_samples = echo_samples_s32p; break;
  221. }
  222. if (s->delayptrs)
  223. av_freep(&s->delayptrs[0]);
  224. av_freep(&s->delayptrs);
  225. return av_samples_alloc_array_and_samples(&s->delayptrs, NULL,
  226. outlink->channels,
  227. s->max_samples,
  228. outlink->format, 0);
  229. }
  230. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  231. {
  232. AVFilterContext *ctx = inlink->dst;
  233. AudioEchoContext *s = ctx->priv;
  234. AVFrame *out_frame;
  235. if (av_frame_is_writable(frame)) {
  236. out_frame = frame;
  237. } else {
  238. out_frame = ff_get_audio_buffer(inlink, frame->nb_samples);
  239. if (!out_frame) {
  240. av_frame_free(&frame);
  241. return AVERROR(ENOMEM);
  242. }
  243. av_frame_copy_props(out_frame, frame);
  244. }
  245. s->echo_samples(s, s->delayptrs, frame->extended_data, out_frame->extended_data,
  246. frame->nb_samples, inlink->channels);
  247. s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
  248. if (frame != out_frame)
  249. av_frame_free(&frame);
  250. return ff_filter_frame(ctx->outputs[0], out_frame);
  251. }
  252. static int request_frame(AVFilterLink *outlink)
  253. {
  254. AVFilterContext *ctx = outlink->src;
  255. AudioEchoContext *s = ctx->priv;
  256. int ret;
  257. ret = ff_request_frame(ctx->inputs[0]);
  258. if (ret == AVERROR_EOF && !ctx->is_disabled && s->fade_out) {
  259. int nb_samples = FFMIN(s->fade_out, 2048);
  260. AVFrame *frame;
  261. frame = ff_get_audio_buffer(outlink, nb_samples);
  262. if (!frame)
  263. return AVERROR(ENOMEM);
  264. s->fade_out -= nb_samples;
  265. av_samples_set_silence(frame->extended_data, 0,
  266. frame->nb_samples,
  267. outlink->channels,
  268. frame->format);
  269. s->echo_samples(s, s->delayptrs, frame->extended_data, frame->extended_data,
  270. frame->nb_samples, outlink->channels);
  271. frame->pts = s->next_pts;
  272. if (s->next_pts != AV_NOPTS_VALUE)
  273. s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  274. return ff_filter_frame(outlink, frame);
  275. }
  276. return ret;
  277. }
  278. static const AVFilterPad aecho_inputs[] = {
  279. {
  280. .name = "default",
  281. .type = AVMEDIA_TYPE_AUDIO,
  282. .filter_frame = filter_frame,
  283. },
  284. { NULL }
  285. };
  286. static const AVFilterPad aecho_outputs[] = {
  287. {
  288. .name = "default",
  289. .request_frame = request_frame,
  290. .config_props = config_output,
  291. .type = AVMEDIA_TYPE_AUDIO,
  292. },
  293. { NULL }
  294. };
  295. AVFilter ff_af_aecho = {
  296. .name = "aecho",
  297. .description = NULL_IF_CONFIG_SMALL("Add echoing to the audio."),
  298. .query_formats = query_formats,
  299. .priv_size = sizeof(AudioEchoContext),
  300. .priv_class = &aecho_class,
  301. .init = init,
  302. .uninit = uninit,
  303. .inputs = aecho_inputs,
  304. .outputs = aecho_outputs,
  305. };