You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
12KB

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